ip6_vti.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * IPv6 virtual tunneling interface
  3. *
  4. * Copyright (C) 2013 secunet Security Networks AG
  5. *
  6. * Author:
  7. * Steffen Klassert <steffen.klassert@secunet.com>
  8. *
  9. * Based on:
  10. * net/ipv6/ip6_tunnel.c
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/capability.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/sockios.h>
  22. #include <linux/icmp.h>
  23. #include <linux/if.h>
  24. #include <linux/in.h>
  25. #include <linux/ip.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmpv6.h>
  31. #include <linux/init.h>
  32. #include <linux/route.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/netfilter_ipv6.h>
  35. #include <linux/slab.h>
  36. #include <linux/hash.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/atomic.h>
  39. #include <net/icmp.h>
  40. #include <net/ip.h>
  41. #include <net/ip_tunnels.h>
  42. #include <net/ipv6.h>
  43. #include <net/ip6_route.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip6_tunnel.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #define HASH_SIZE_SHIFT 5
  50. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  51. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  52. {
  53. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  54. return hash_32(hash, HASH_SIZE_SHIFT);
  55. }
  56. static int vti6_dev_init(struct net_device *dev);
  57. static void vti6_dev_setup(struct net_device *dev);
  58. static struct rtnl_link_ops vti6_link_ops __read_mostly;
  59. static int vti6_net_id __read_mostly;
  60. struct vti6_net {
  61. /* the vti6 tunnel fallback device */
  62. struct net_device *fb_tnl_dev;
  63. /* lists for storing tunnels in use */
  64. struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
  65. struct ip6_tnl __rcu *tnls_wc[1];
  66. struct ip6_tnl __rcu **tnls[2];
  67. };
  68. #define for_each_vti6_tunnel_rcu(start) \
  69. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  70. /**
  71. * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
  72. * @net: network namespace
  73. * @remote: the address of the tunnel exit-point
  74. * @local: the address of the tunnel entry-point
  75. *
  76. * Return:
  77. * tunnel matching given end-points if found,
  78. * else fallback tunnel if its device is up,
  79. * else %NULL
  80. **/
  81. static struct ip6_tnl *
  82. vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
  83. const struct in6_addr *local)
  84. {
  85. unsigned int hash = HASH(remote, local);
  86. struct ip6_tnl *t;
  87. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  88. struct in6_addr any;
  89. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  90. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  91. ipv6_addr_equal(remote, &t->parms.raddr) &&
  92. (t->dev->flags & IFF_UP))
  93. return t;
  94. }
  95. memset(&any, 0, sizeof(any));
  96. hash = HASH(&any, local);
  97. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  98. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  99. (t->dev->flags & IFF_UP))
  100. return t;
  101. }
  102. hash = HASH(remote, &any);
  103. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  104. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  105. (t->dev->flags & IFF_UP))
  106. return t;
  107. }
  108. t = rcu_dereference(ip6n->tnls_wc[0]);
  109. if (t && (t->dev->flags & IFF_UP))
  110. return t;
  111. return NULL;
  112. }
  113. /**
  114. * vti6_tnl_bucket - get head of list matching given tunnel parameters
  115. * @p: parameters containing tunnel end-points
  116. *
  117. * Description:
  118. * vti6_tnl_bucket() returns the head of the list matching the
  119. * &struct in6_addr entries laddr and raddr in @p.
  120. *
  121. * Return: head of IPv6 tunnel list
  122. **/
  123. static struct ip6_tnl __rcu **
  124. vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
  125. {
  126. const struct in6_addr *remote = &p->raddr;
  127. const struct in6_addr *local = &p->laddr;
  128. unsigned int h = 0;
  129. int prio = 0;
  130. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  131. prio = 1;
  132. h = HASH(remote, local);
  133. }
  134. return &ip6n->tnls[prio][h];
  135. }
  136. static void
  137. vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
  138. {
  139. struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
  140. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  141. rcu_assign_pointer(*tp, t);
  142. }
  143. static void
  144. vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
  145. {
  146. struct ip6_tnl __rcu **tp;
  147. struct ip6_tnl *iter;
  148. for (tp = vti6_tnl_bucket(ip6n, &t->parms);
  149. (iter = rtnl_dereference(*tp)) != NULL;
  150. tp = &iter->next) {
  151. if (t == iter) {
  152. rcu_assign_pointer(*tp, t->next);
  153. break;
  154. }
  155. }
  156. }
  157. static void vti6_dev_free(struct net_device *dev)
  158. {
  159. free_percpu(dev->tstats);
  160. free_netdev(dev);
  161. }
  162. static int vti6_tnl_create2(struct net_device *dev)
  163. {
  164. struct ip6_tnl *t = netdev_priv(dev);
  165. struct net *net = dev_net(dev);
  166. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  167. int err;
  168. err = register_netdevice(dev);
  169. if (err < 0)
  170. goto out;
  171. strcpy(t->parms.name, dev->name);
  172. dev->rtnl_link_ops = &vti6_link_ops;
  173. dev_hold(dev);
  174. vti6_tnl_link(ip6n, t);
  175. return 0;
  176. out:
  177. return err;
  178. }
  179. static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  180. {
  181. struct net_device *dev;
  182. struct ip6_tnl *t;
  183. char name[IFNAMSIZ];
  184. int err;
  185. if (p->name[0])
  186. strlcpy(name, p->name, IFNAMSIZ);
  187. else
  188. sprintf(name, "ip6_vti%%d");
  189. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
  190. if (dev == NULL)
  191. goto failed;
  192. dev_net_set(dev, net);
  193. t = netdev_priv(dev);
  194. t->parms = *p;
  195. t->net = dev_net(dev);
  196. err = vti6_tnl_create2(dev);
  197. if (err < 0)
  198. goto failed_free;
  199. return t;
  200. failed_free:
  201. vti6_dev_free(dev);
  202. failed:
  203. return NULL;
  204. }
  205. /**
  206. * vti6_locate - find or create tunnel matching given parameters
  207. * @net: network namespace
  208. * @p: tunnel parameters
  209. * @create: != 0 if allowed to create new tunnel if no match found
  210. *
  211. * Description:
  212. * vti6_locate() first tries to locate an existing tunnel
  213. * based on @parms. If this is unsuccessful, but @create is set a new
  214. * tunnel device is created and registered for use.
  215. *
  216. * Return:
  217. * matching tunnel or NULL
  218. **/
  219. static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
  220. int create)
  221. {
  222. const struct in6_addr *remote = &p->raddr;
  223. const struct in6_addr *local = &p->laddr;
  224. struct ip6_tnl __rcu **tp;
  225. struct ip6_tnl *t;
  226. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  227. for (tp = vti6_tnl_bucket(ip6n, p);
  228. (t = rtnl_dereference(*tp)) != NULL;
  229. tp = &t->next) {
  230. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  231. ipv6_addr_equal(remote, &t->parms.raddr)) {
  232. if (create)
  233. return NULL;
  234. return t;
  235. }
  236. }
  237. if (!create)
  238. return NULL;
  239. return vti6_tnl_create(net, p);
  240. }
  241. /**
  242. * vti6_dev_uninit - tunnel device uninitializer
  243. * @dev: the device to be destroyed
  244. *
  245. * Description:
  246. * vti6_dev_uninit() removes tunnel from its list
  247. **/
  248. static void vti6_dev_uninit(struct net_device *dev)
  249. {
  250. struct ip6_tnl *t = netdev_priv(dev);
  251. struct net *net = dev_net(dev);
  252. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  253. if (dev == ip6n->fb_tnl_dev)
  254. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  255. else
  256. vti6_tnl_unlink(ip6n, t);
  257. dev_put(dev);
  258. }
  259. static int vti6_rcv(struct sk_buff *skb)
  260. {
  261. struct ip6_tnl *t;
  262. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  263. rcu_read_lock();
  264. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  265. if (t != NULL) {
  266. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  267. rcu_read_unlock();
  268. goto discard;
  269. }
  270. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  271. rcu_read_unlock();
  272. return 0;
  273. }
  274. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  275. t->dev->stats.rx_dropped++;
  276. rcu_read_unlock();
  277. goto discard;
  278. }
  279. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  280. skb->mark = be32_to_cpu(t->parms.i_key);
  281. rcu_read_unlock();
  282. return xfrm6_rcv(skb);
  283. }
  284. rcu_read_unlock();
  285. return -EINVAL;
  286. discard:
  287. kfree_skb(skb);
  288. return 0;
  289. }
  290. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  291. {
  292. unsigned short family;
  293. struct net_device *dev;
  294. struct pcpu_sw_netstats *tstats;
  295. struct xfrm_state *x;
  296. struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
  297. if (!t)
  298. return 1;
  299. dev = t->dev;
  300. if (err) {
  301. dev->stats.rx_errors++;
  302. dev->stats.rx_dropped++;
  303. return 0;
  304. }
  305. x = xfrm_input_state(skb);
  306. family = x->inner_mode->afinfo->family;
  307. if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family))
  308. return -EPERM;
  309. skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
  310. skb->dev = dev;
  311. tstats = this_cpu_ptr(dev->tstats);
  312. u64_stats_update_begin(&tstats->syncp);
  313. tstats->rx_packets++;
  314. tstats->rx_bytes += skb->len;
  315. u64_stats_update_end(&tstats->syncp);
  316. return 0;
  317. }
  318. /**
  319. * vti6_addr_conflict - compare packet addresses to tunnel's own
  320. * @t: the outgoing tunnel device
  321. * @hdr: IPv6 header from the incoming packet
  322. *
  323. * Description:
  324. * Avoid trivial tunneling loop by checking that tunnel exit-point
  325. * doesn't match source of incoming packet.
  326. *
  327. * Return:
  328. * 1 if conflict,
  329. * 0 else
  330. **/
  331. static inline bool
  332. vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  333. {
  334. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  335. }
  336. static bool vti6_state_check(const struct xfrm_state *x,
  337. const struct in6_addr *dst,
  338. const struct in6_addr *src)
  339. {
  340. xfrm_address_t *daddr = (xfrm_address_t *)dst;
  341. xfrm_address_t *saddr = (xfrm_address_t *)src;
  342. /* if there is no transform then this tunnel is not functional.
  343. * Or if the xfrm is not mode tunnel.
  344. */
  345. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  346. x->props.family != AF_INET6)
  347. return false;
  348. if (ipv6_addr_any(dst))
  349. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
  350. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
  351. return false;
  352. return true;
  353. }
  354. /**
  355. * vti6_xmit - send a packet
  356. * @skb: the outgoing socket buffer
  357. * @dev: the outgoing tunnel device
  358. * @fl: the flow informations for the xfrm_lookup
  359. **/
  360. static int
  361. vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
  362. {
  363. struct ip6_tnl *t = netdev_priv(dev);
  364. struct net_device_stats *stats = &t->dev->stats;
  365. struct dst_entry *dst = skb_dst(skb);
  366. struct net_device *tdev;
  367. struct xfrm_state *x;
  368. int err = -1;
  369. if (!dst)
  370. goto tx_err_link_failure;
  371. dst_hold(dst);
  372. dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
  373. if (IS_ERR(dst)) {
  374. err = PTR_ERR(dst);
  375. dst = NULL;
  376. goto tx_err_link_failure;
  377. }
  378. x = dst->xfrm;
  379. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  380. goto tx_err_link_failure;
  381. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  382. (const struct in6_addr *)&x->id.daddr))
  383. goto tx_err_link_failure;
  384. tdev = dst->dev;
  385. if (tdev == dev) {
  386. stats->collisions++;
  387. net_warn_ratelimited("%s: Local routing loop detected!\n",
  388. t->parms.name);
  389. goto tx_err_dst_release;
  390. }
  391. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  392. skb_dst_set(skb, dst);
  393. skb->dev = skb_dst(skb)->dev;
  394. err = dst_output(skb);
  395. if (net_xmit_eval(err) == 0) {
  396. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  397. u64_stats_update_begin(&tstats->syncp);
  398. tstats->tx_bytes += skb->len;
  399. tstats->tx_packets++;
  400. u64_stats_update_end(&tstats->syncp);
  401. } else {
  402. stats->tx_errors++;
  403. stats->tx_aborted_errors++;
  404. }
  405. return 0;
  406. tx_err_link_failure:
  407. stats->tx_carrier_errors++;
  408. dst_link_failure(skb);
  409. tx_err_dst_release:
  410. dst_release(dst);
  411. return err;
  412. }
  413. static netdev_tx_t
  414. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  415. {
  416. struct ip6_tnl *t = netdev_priv(dev);
  417. struct net_device_stats *stats = &t->dev->stats;
  418. struct ipv6hdr *ipv6h;
  419. struct flowi fl;
  420. int ret;
  421. memset(&fl, 0, sizeof(fl));
  422. skb->mark = be32_to_cpu(t->parms.o_key);
  423. switch (skb->protocol) {
  424. case htons(ETH_P_IPV6):
  425. ipv6h = ipv6_hdr(skb);
  426. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  427. vti6_addr_conflict(t, ipv6h))
  428. goto tx_err;
  429. xfrm_decode_session(skb, &fl, AF_INET6);
  430. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  431. break;
  432. case htons(ETH_P_IP):
  433. xfrm_decode_session(skb, &fl, AF_INET);
  434. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  435. break;
  436. default:
  437. goto tx_err;
  438. }
  439. ret = vti6_xmit(skb, dev, &fl);
  440. if (ret < 0)
  441. goto tx_err;
  442. return NETDEV_TX_OK;
  443. tx_err:
  444. stats->tx_errors++;
  445. stats->tx_dropped++;
  446. kfree_skb(skb);
  447. return NETDEV_TX_OK;
  448. }
  449. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  450. u8 type, u8 code, int offset, __be32 info)
  451. {
  452. __be32 spi;
  453. __u32 mark;
  454. struct xfrm_state *x;
  455. struct ip6_tnl *t;
  456. struct ip_esp_hdr *esph;
  457. struct ip_auth_hdr *ah;
  458. struct ip_comp_hdr *ipch;
  459. struct net *net = dev_net(skb->dev);
  460. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  461. int protocol = iph->nexthdr;
  462. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  463. if (!t)
  464. return -1;
  465. mark = be32_to_cpu(t->parms.o_key);
  466. switch (protocol) {
  467. case IPPROTO_ESP:
  468. esph = (struct ip_esp_hdr *)(skb->data + offset);
  469. spi = esph->spi;
  470. break;
  471. case IPPROTO_AH:
  472. ah = (struct ip_auth_hdr *)(skb->data + offset);
  473. spi = ah->spi;
  474. break;
  475. case IPPROTO_COMP:
  476. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  477. spi = htonl(ntohs(ipch->cpi));
  478. break;
  479. default:
  480. return 0;
  481. }
  482. if (type != ICMPV6_PKT_TOOBIG &&
  483. type != NDISC_REDIRECT)
  484. return 0;
  485. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  486. spi, protocol, AF_INET6);
  487. if (!x)
  488. return 0;
  489. if (type == NDISC_REDIRECT)
  490. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  491. else
  492. ip6_update_pmtu(skb, net, info, 0, 0);
  493. xfrm_state_put(x);
  494. return 0;
  495. }
  496. static void vti6_link_config(struct ip6_tnl *t)
  497. {
  498. struct net_device *dev = t->dev;
  499. struct __ip6_tnl_parm *p = &t->parms;
  500. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  501. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  502. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  503. IP6_TNL_F_CAP_PER_PACKET);
  504. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  505. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  506. dev->flags |= IFF_POINTOPOINT;
  507. else
  508. dev->flags &= ~IFF_POINTOPOINT;
  509. dev->iflink = p->link;
  510. }
  511. /**
  512. * vti6_tnl_change - update the tunnel parameters
  513. * @t: tunnel to be changed
  514. * @p: tunnel configuration parameters
  515. *
  516. * Description:
  517. * vti6_tnl_change() updates the tunnel parameters
  518. **/
  519. static int
  520. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  521. {
  522. t->parms.laddr = p->laddr;
  523. t->parms.raddr = p->raddr;
  524. t->parms.link = p->link;
  525. t->parms.i_key = p->i_key;
  526. t->parms.o_key = p->o_key;
  527. t->parms.proto = p->proto;
  528. ip6_tnl_dst_reset(t);
  529. vti6_link_config(t);
  530. return 0;
  531. }
  532. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  533. {
  534. struct net *net = dev_net(t->dev);
  535. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  536. int err;
  537. vti6_tnl_unlink(ip6n, t);
  538. synchronize_net();
  539. err = vti6_tnl_change(t, p);
  540. vti6_tnl_link(ip6n, t);
  541. netdev_state_change(t->dev);
  542. return err;
  543. }
  544. static void
  545. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  546. {
  547. p->laddr = u->laddr;
  548. p->raddr = u->raddr;
  549. p->link = u->link;
  550. p->i_key = u->i_key;
  551. p->o_key = u->o_key;
  552. p->proto = u->proto;
  553. memcpy(p->name, u->name, sizeof(u->name));
  554. }
  555. static void
  556. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  557. {
  558. u->laddr = p->laddr;
  559. u->raddr = p->raddr;
  560. u->link = p->link;
  561. u->i_key = p->i_key;
  562. u->o_key = p->o_key;
  563. u->proto = p->proto;
  564. memcpy(u->name, p->name, sizeof(u->name));
  565. }
  566. /**
  567. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  568. * @dev: virtual device associated with tunnel
  569. * @ifr: parameters passed from userspace
  570. * @cmd: command to be performed
  571. *
  572. * Description:
  573. * vti6_ioctl() is used for managing vti6 tunnels
  574. * from userspace.
  575. *
  576. * The possible commands are the following:
  577. * %SIOCGETTUNNEL: get tunnel parameters for device
  578. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  579. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  580. * %SIOCDELTUNNEL: delete tunnel
  581. *
  582. * The fallback device "ip6_vti0", created during module
  583. * initialization, can be used for creating other tunnel devices.
  584. *
  585. * Return:
  586. * 0 on success,
  587. * %-EFAULT if unable to copy data to or from userspace,
  588. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  589. * %-EINVAL if passed tunnel parameters are invalid,
  590. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  591. * %-ENODEV if attempting to change or delete a nonexisting device
  592. **/
  593. static int
  594. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  595. {
  596. int err = 0;
  597. struct ip6_tnl_parm2 p;
  598. struct __ip6_tnl_parm p1;
  599. struct ip6_tnl *t = NULL;
  600. struct net *net = dev_net(dev);
  601. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  602. switch (cmd) {
  603. case SIOCGETTUNNEL:
  604. if (dev == ip6n->fb_tnl_dev) {
  605. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  606. err = -EFAULT;
  607. break;
  608. }
  609. vti6_parm_from_user(&p1, &p);
  610. t = vti6_locate(net, &p1, 0);
  611. } else {
  612. memset(&p, 0, sizeof(p));
  613. }
  614. if (t == NULL)
  615. t = netdev_priv(dev);
  616. vti6_parm_to_user(&p, &t->parms);
  617. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  618. err = -EFAULT;
  619. break;
  620. case SIOCADDTUNNEL:
  621. case SIOCCHGTUNNEL:
  622. err = -EPERM;
  623. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  624. break;
  625. err = -EFAULT;
  626. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  627. break;
  628. err = -EINVAL;
  629. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  630. break;
  631. vti6_parm_from_user(&p1, &p);
  632. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  633. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  634. if (t != NULL) {
  635. if (t->dev != dev) {
  636. err = -EEXIST;
  637. break;
  638. }
  639. } else
  640. t = netdev_priv(dev);
  641. err = vti6_update(t, &p1);
  642. }
  643. if (t) {
  644. err = 0;
  645. vti6_parm_to_user(&p, &t->parms);
  646. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  647. err = -EFAULT;
  648. } else
  649. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  650. break;
  651. case SIOCDELTUNNEL:
  652. err = -EPERM;
  653. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  654. break;
  655. if (dev == ip6n->fb_tnl_dev) {
  656. err = -EFAULT;
  657. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  658. break;
  659. err = -ENOENT;
  660. vti6_parm_from_user(&p1, &p);
  661. t = vti6_locate(net, &p1, 0);
  662. if (t == NULL)
  663. break;
  664. err = -EPERM;
  665. if (t->dev == ip6n->fb_tnl_dev)
  666. break;
  667. dev = t->dev;
  668. }
  669. err = 0;
  670. unregister_netdevice(dev);
  671. break;
  672. default:
  673. err = -EINVAL;
  674. }
  675. return err;
  676. }
  677. /**
  678. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  679. * @dev: virtual device associated with tunnel
  680. * @new_mtu: the new mtu
  681. *
  682. * Return:
  683. * 0 on success,
  684. * %-EINVAL if mtu too small
  685. **/
  686. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  687. {
  688. if (new_mtu < IPV6_MIN_MTU)
  689. return -EINVAL;
  690. dev->mtu = new_mtu;
  691. return 0;
  692. }
  693. static const struct net_device_ops vti6_netdev_ops = {
  694. .ndo_init = vti6_dev_init,
  695. .ndo_uninit = vti6_dev_uninit,
  696. .ndo_start_xmit = vti6_tnl_xmit,
  697. .ndo_do_ioctl = vti6_ioctl,
  698. .ndo_change_mtu = vti6_change_mtu,
  699. .ndo_get_stats64 = ip_tunnel_get_stats64,
  700. };
  701. /**
  702. * vti6_dev_setup - setup virtual tunnel device
  703. * @dev: virtual device associated with tunnel
  704. *
  705. * Description:
  706. * Initialize function pointers and device parameters
  707. **/
  708. static void vti6_dev_setup(struct net_device *dev)
  709. {
  710. dev->netdev_ops = &vti6_netdev_ops;
  711. dev->destructor = vti6_dev_free;
  712. dev->type = ARPHRD_TUNNEL6;
  713. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  714. dev->mtu = ETH_DATA_LEN;
  715. dev->flags |= IFF_NOARP;
  716. dev->addr_len = sizeof(struct in6_addr);
  717. netif_keep_dst(dev);
  718. }
  719. /**
  720. * vti6_dev_init_gen - general initializer for all tunnel devices
  721. * @dev: virtual device associated with tunnel
  722. **/
  723. static inline int vti6_dev_init_gen(struct net_device *dev)
  724. {
  725. struct ip6_tnl *t = netdev_priv(dev);
  726. t->dev = dev;
  727. t->net = dev_net(dev);
  728. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  729. if (!dev->tstats)
  730. return -ENOMEM;
  731. return 0;
  732. }
  733. /**
  734. * vti6_dev_init - initializer for all non fallback tunnel devices
  735. * @dev: virtual device associated with tunnel
  736. **/
  737. static int vti6_dev_init(struct net_device *dev)
  738. {
  739. struct ip6_tnl *t = netdev_priv(dev);
  740. int err = vti6_dev_init_gen(dev);
  741. if (err)
  742. return err;
  743. vti6_link_config(t);
  744. return 0;
  745. }
  746. /**
  747. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  748. * @dev: fallback device
  749. *
  750. * Return: 0
  751. **/
  752. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  753. {
  754. struct ip6_tnl *t = netdev_priv(dev);
  755. struct net *net = dev_net(dev);
  756. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  757. t->parms.proto = IPPROTO_IPV6;
  758. dev_hold(dev);
  759. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  760. return 0;
  761. }
  762. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  763. {
  764. return 0;
  765. }
  766. static void vti6_netlink_parms(struct nlattr *data[],
  767. struct __ip6_tnl_parm *parms)
  768. {
  769. memset(parms, 0, sizeof(*parms));
  770. if (!data)
  771. return;
  772. if (data[IFLA_VTI_LINK])
  773. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  774. if (data[IFLA_VTI_LOCAL])
  775. nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
  776. sizeof(struct in6_addr));
  777. if (data[IFLA_VTI_REMOTE])
  778. nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
  779. sizeof(struct in6_addr));
  780. if (data[IFLA_VTI_IKEY])
  781. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  782. if (data[IFLA_VTI_OKEY])
  783. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  784. }
  785. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  786. struct nlattr *tb[], struct nlattr *data[])
  787. {
  788. struct net *net = dev_net(dev);
  789. struct ip6_tnl *nt;
  790. nt = netdev_priv(dev);
  791. vti6_netlink_parms(data, &nt->parms);
  792. nt->parms.proto = IPPROTO_IPV6;
  793. if (vti6_locate(net, &nt->parms, 0))
  794. return -EEXIST;
  795. return vti6_tnl_create2(dev);
  796. }
  797. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  798. {
  799. struct net *net = dev_net(dev);
  800. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  801. if (dev != ip6n->fb_tnl_dev)
  802. unregister_netdevice_queue(dev, head);
  803. }
  804. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  805. struct nlattr *data[])
  806. {
  807. struct ip6_tnl *t;
  808. struct __ip6_tnl_parm p;
  809. struct net *net = dev_net(dev);
  810. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  811. if (dev == ip6n->fb_tnl_dev)
  812. return -EINVAL;
  813. vti6_netlink_parms(data, &p);
  814. t = vti6_locate(net, &p, 0);
  815. if (t) {
  816. if (t->dev != dev)
  817. return -EEXIST;
  818. } else
  819. t = netdev_priv(dev);
  820. return vti6_update(t, &p);
  821. }
  822. static size_t vti6_get_size(const struct net_device *dev)
  823. {
  824. return
  825. /* IFLA_VTI_LINK */
  826. nla_total_size(4) +
  827. /* IFLA_VTI_LOCAL */
  828. nla_total_size(sizeof(struct in6_addr)) +
  829. /* IFLA_VTI_REMOTE */
  830. nla_total_size(sizeof(struct in6_addr)) +
  831. /* IFLA_VTI_IKEY */
  832. nla_total_size(4) +
  833. /* IFLA_VTI_OKEY */
  834. nla_total_size(4) +
  835. 0;
  836. }
  837. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  838. {
  839. struct ip6_tnl *tunnel = netdev_priv(dev);
  840. struct __ip6_tnl_parm *parm = &tunnel->parms;
  841. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  842. nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
  843. &parm->laddr) ||
  844. nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
  845. &parm->raddr) ||
  846. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  847. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
  848. goto nla_put_failure;
  849. return 0;
  850. nla_put_failure:
  851. return -EMSGSIZE;
  852. }
  853. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  854. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  855. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  856. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  857. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  858. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  859. };
  860. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  861. .kind = "vti6",
  862. .maxtype = IFLA_VTI_MAX,
  863. .policy = vti6_policy,
  864. .priv_size = sizeof(struct ip6_tnl),
  865. .setup = vti6_dev_setup,
  866. .validate = vti6_validate,
  867. .newlink = vti6_newlink,
  868. .dellink = vti6_dellink,
  869. .changelink = vti6_changelink,
  870. .get_size = vti6_get_size,
  871. .fill_info = vti6_fill_info,
  872. .get_link_net = ip6_tnl_get_link_net,
  873. };
  874. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  875. {
  876. int h;
  877. struct ip6_tnl *t;
  878. LIST_HEAD(list);
  879. for (h = 0; h < HASH_SIZE; h++) {
  880. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  881. while (t != NULL) {
  882. unregister_netdevice_queue(t->dev, &list);
  883. t = rtnl_dereference(t->next);
  884. }
  885. }
  886. t = rtnl_dereference(ip6n->tnls_wc[0]);
  887. unregister_netdevice_queue(t->dev, &list);
  888. unregister_netdevice_many(&list);
  889. }
  890. static int __net_init vti6_init_net(struct net *net)
  891. {
  892. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  893. struct ip6_tnl *t = NULL;
  894. int err;
  895. ip6n->tnls[0] = ip6n->tnls_wc;
  896. ip6n->tnls[1] = ip6n->tnls_r_l;
  897. err = -ENOMEM;
  898. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  899. NET_NAME_UNKNOWN, vti6_dev_setup);
  900. if (!ip6n->fb_tnl_dev)
  901. goto err_alloc_dev;
  902. dev_net_set(ip6n->fb_tnl_dev, net);
  903. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  904. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  905. if (err < 0)
  906. goto err_register;
  907. err = register_netdev(ip6n->fb_tnl_dev);
  908. if (err < 0)
  909. goto err_register;
  910. t = netdev_priv(ip6n->fb_tnl_dev);
  911. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  912. return 0;
  913. err_register:
  914. vti6_dev_free(ip6n->fb_tnl_dev);
  915. err_alloc_dev:
  916. return err;
  917. }
  918. static void __net_exit vti6_exit_net(struct net *net)
  919. {
  920. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  921. rtnl_lock();
  922. vti6_destroy_tunnels(ip6n);
  923. rtnl_unlock();
  924. }
  925. static struct pernet_operations vti6_net_ops = {
  926. .init = vti6_init_net,
  927. .exit = vti6_exit_net,
  928. .id = &vti6_net_id,
  929. .size = sizeof(struct vti6_net),
  930. };
  931. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  932. .handler = vti6_rcv,
  933. .cb_handler = vti6_rcv_cb,
  934. .err_handler = vti6_err,
  935. .priority = 100,
  936. };
  937. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  938. .handler = vti6_rcv,
  939. .cb_handler = vti6_rcv_cb,
  940. .err_handler = vti6_err,
  941. .priority = 100,
  942. };
  943. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  944. .handler = vti6_rcv,
  945. .cb_handler = vti6_rcv_cb,
  946. .err_handler = vti6_err,
  947. .priority = 100,
  948. };
  949. /**
  950. * vti6_tunnel_init - register protocol and reserve needed resources
  951. *
  952. * Return: 0 on success
  953. **/
  954. static int __init vti6_tunnel_init(void)
  955. {
  956. const char *msg;
  957. int err;
  958. msg = "tunnel device";
  959. err = register_pernet_device(&vti6_net_ops);
  960. if (err < 0)
  961. goto pernet_dev_failed;
  962. msg = "tunnel protocols";
  963. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  964. if (err < 0)
  965. goto xfrm_proto_esp_failed;
  966. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  967. if (err < 0)
  968. goto xfrm_proto_ah_failed;
  969. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  970. if (err < 0)
  971. goto xfrm_proto_comp_failed;
  972. msg = "netlink interface";
  973. err = rtnl_link_register(&vti6_link_ops);
  974. if (err < 0)
  975. goto rtnl_link_failed;
  976. return 0;
  977. rtnl_link_failed:
  978. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  979. xfrm_proto_comp_failed:
  980. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  981. xfrm_proto_ah_failed:
  982. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  983. xfrm_proto_esp_failed:
  984. unregister_pernet_device(&vti6_net_ops);
  985. pernet_dev_failed:
  986. pr_err("vti6 init: failed to register %s\n", msg);
  987. return err;
  988. }
  989. /**
  990. * vti6_tunnel_cleanup - free resources and unregister protocol
  991. **/
  992. static void __exit vti6_tunnel_cleanup(void)
  993. {
  994. rtnl_link_unregister(&vti6_link_ops);
  995. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  996. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  997. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  998. unregister_pernet_device(&vti6_net_ops);
  999. }
  1000. module_init(vti6_tunnel_init);
  1001. module_exit(vti6_tunnel_cleanup);
  1002. MODULE_LICENSE("GPL");
  1003. MODULE_ALIAS_RTNL_LINK("vti6");
  1004. MODULE_ALIAS_NETDEV("ip6_vti0");
  1005. MODULE_AUTHOR("Steffen Klassert");
  1006. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");