ip6_vti.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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)
  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 vti6_net *ip6n = net_generic(t->net, vti6_net_id);
  252. if (dev == ip6n->fb_tnl_dev)
  253. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  254. else
  255. vti6_tnl_unlink(ip6n, t);
  256. dev_put(dev);
  257. }
  258. static int vti6_rcv(struct sk_buff *skb)
  259. {
  260. struct ip6_tnl *t;
  261. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  262. rcu_read_lock();
  263. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  264. if (t) {
  265. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  266. rcu_read_unlock();
  267. goto discard;
  268. }
  269. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  270. rcu_read_unlock();
  271. return 0;
  272. }
  273. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  274. t->dev->stats.rx_dropped++;
  275. rcu_read_unlock();
  276. goto discard;
  277. }
  278. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  279. rcu_read_unlock();
  280. return xfrm6_rcv(skb);
  281. }
  282. rcu_read_unlock();
  283. return -EINVAL;
  284. discard:
  285. kfree_skb(skb);
  286. return 0;
  287. }
  288. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  289. {
  290. unsigned short family;
  291. struct net_device *dev;
  292. struct pcpu_sw_netstats *tstats;
  293. struct xfrm_state *x;
  294. struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
  295. u32 orig_mark = skb->mark;
  296. int ret;
  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. skb->mark = be32_to_cpu(t->parms.i_key);
  308. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  309. skb->mark = orig_mark;
  310. if (!ret)
  311. return -EPERM;
  312. skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
  313. skb->dev = dev;
  314. tstats = this_cpu_ptr(dev->tstats);
  315. u64_stats_update_begin(&tstats->syncp);
  316. tstats->rx_packets++;
  317. tstats->rx_bytes += skb->len;
  318. u64_stats_update_end(&tstats->syncp);
  319. return 0;
  320. }
  321. /**
  322. * vti6_addr_conflict - compare packet addresses to tunnel's own
  323. * @t: the outgoing tunnel device
  324. * @hdr: IPv6 header from the incoming packet
  325. *
  326. * Description:
  327. * Avoid trivial tunneling loop by checking that tunnel exit-point
  328. * doesn't match source of incoming packet.
  329. *
  330. * Return:
  331. * 1 if conflict,
  332. * 0 else
  333. **/
  334. static inline bool
  335. vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  336. {
  337. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  338. }
  339. static bool vti6_state_check(const struct xfrm_state *x,
  340. const struct in6_addr *dst,
  341. const struct in6_addr *src)
  342. {
  343. xfrm_address_t *daddr = (xfrm_address_t *)dst;
  344. xfrm_address_t *saddr = (xfrm_address_t *)src;
  345. /* if there is no transform then this tunnel is not functional.
  346. * Or if the xfrm is not mode tunnel.
  347. */
  348. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  349. x->props.family != AF_INET6)
  350. return false;
  351. if (ipv6_addr_any(dst))
  352. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
  353. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
  354. return false;
  355. return true;
  356. }
  357. /**
  358. * vti6_xmit - send a packet
  359. * @skb: the outgoing socket buffer
  360. * @dev: the outgoing tunnel device
  361. * @fl: the flow informations for the xfrm_lookup
  362. **/
  363. static int
  364. vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
  365. {
  366. struct ip6_tnl *t = netdev_priv(dev);
  367. struct net_device_stats *stats = &t->dev->stats;
  368. struct dst_entry *dst = skb_dst(skb);
  369. struct net_device *tdev;
  370. struct xfrm_state *x;
  371. int err = -1;
  372. int mtu;
  373. if (!dst)
  374. goto tx_err_link_failure;
  375. dst_hold(dst);
  376. dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
  377. if (IS_ERR(dst)) {
  378. err = PTR_ERR(dst);
  379. dst = NULL;
  380. goto tx_err_link_failure;
  381. }
  382. x = dst->xfrm;
  383. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  384. goto tx_err_link_failure;
  385. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  386. (const struct in6_addr *)&x->id.daddr))
  387. goto tx_err_link_failure;
  388. tdev = dst->dev;
  389. if (tdev == dev) {
  390. stats->collisions++;
  391. net_warn_ratelimited("%s: Local routing loop detected!\n",
  392. t->parms.name);
  393. goto tx_err_dst_release;
  394. }
  395. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  396. skb_dst_set(skb, dst);
  397. skb->dev = skb_dst(skb)->dev;
  398. mtu = dst_mtu(dst);
  399. if (!skb->ignore_df && skb->len > mtu) {
  400. skb_dst(skb)->ops->update_pmtu(dst, NULL, skb, mtu);
  401. if (skb->protocol == htons(ETH_P_IPV6))
  402. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  403. else
  404. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  405. htonl(mtu));
  406. return -EMSGSIZE;
  407. }
  408. err = dst_output(skb);
  409. if (net_xmit_eval(err) == 0) {
  410. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  411. u64_stats_update_begin(&tstats->syncp);
  412. tstats->tx_bytes += skb->len;
  413. tstats->tx_packets++;
  414. u64_stats_update_end(&tstats->syncp);
  415. } else {
  416. stats->tx_errors++;
  417. stats->tx_aborted_errors++;
  418. }
  419. return 0;
  420. tx_err_link_failure:
  421. stats->tx_carrier_errors++;
  422. dst_link_failure(skb);
  423. tx_err_dst_release:
  424. dst_release(dst);
  425. return err;
  426. }
  427. static netdev_tx_t
  428. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  429. {
  430. struct ip6_tnl *t = netdev_priv(dev);
  431. struct net_device_stats *stats = &t->dev->stats;
  432. struct ipv6hdr *ipv6h;
  433. struct flowi fl;
  434. int ret;
  435. memset(&fl, 0, sizeof(fl));
  436. switch (skb->protocol) {
  437. case htons(ETH_P_IPV6):
  438. ipv6h = ipv6_hdr(skb);
  439. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  440. vti6_addr_conflict(t, ipv6h))
  441. goto tx_err;
  442. xfrm_decode_session(skb, &fl, AF_INET6);
  443. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  444. break;
  445. case htons(ETH_P_IP):
  446. xfrm_decode_session(skb, &fl, AF_INET);
  447. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  448. break;
  449. default:
  450. goto tx_err;
  451. }
  452. /* override mark with tunnel output key */
  453. fl.flowi_mark = be32_to_cpu(t->parms.o_key);
  454. ret = vti6_xmit(skb, dev, &fl);
  455. if (ret < 0)
  456. goto tx_err;
  457. return NETDEV_TX_OK;
  458. tx_err:
  459. stats->tx_errors++;
  460. stats->tx_dropped++;
  461. kfree_skb(skb);
  462. return NETDEV_TX_OK;
  463. }
  464. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  465. u8 type, u8 code, int offset, __be32 info)
  466. {
  467. __be32 spi;
  468. __u32 mark;
  469. struct xfrm_state *x;
  470. struct ip6_tnl *t;
  471. struct ip_esp_hdr *esph;
  472. struct ip_auth_hdr *ah;
  473. struct ip_comp_hdr *ipch;
  474. struct net *net = dev_net(skb->dev);
  475. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  476. int protocol = iph->nexthdr;
  477. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  478. if (!t)
  479. return -1;
  480. mark = be32_to_cpu(t->parms.o_key);
  481. switch (protocol) {
  482. case IPPROTO_ESP:
  483. esph = (struct ip_esp_hdr *)(skb->data + offset);
  484. spi = esph->spi;
  485. break;
  486. case IPPROTO_AH:
  487. ah = (struct ip_auth_hdr *)(skb->data + offset);
  488. spi = ah->spi;
  489. break;
  490. case IPPROTO_COMP:
  491. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  492. spi = htonl(ntohs(ipch->cpi));
  493. break;
  494. default:
  495. return 0;
  496. }
  497. if (type != ICMPV6_PKT_TOOBIG &&
  498. type != NDISC_REDIRECT)
  499. return 0;
  500. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  501. spi, protocol, AF_INET6);
  502. if (!x)
  503. return 0;
  504. if (type == NDISC_REDIRECT)
  505. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  506. else
  507. ip6_update_pmtu(skb, net, info, 0, 0);
  508. xfrm_state_put(x);
  509. return 0;
  510. }
  511. static void vti6_link_config(struct ip6_tnl *t)
  512. {
  513. struct net_device *dev = t->dev;
  514. struct __ip6_tnl_parm *p = &t->parms;
  515. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  516. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  517. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  518. IP6_TNL_F_CAP_PER_PACKET);
  519. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  520. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  521. dev->flags |= IFF_POINTOPOINT;
  522. else
  523. dev->flags &= ~IFF_POINTOPOINT;
  524. }
  525. /**
  526. * vti6_tnl_change - update the tunnel parameters
  527. * @t: tunnel to be changed
  528. * @p: tunnel configuration parameters
  529. *
  530. * Description:
  531. * vti6_tnl_change() updates the tunnel parameters
  532. **/
  533. static int
  534. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  535. {
  536. t->parms.laddr = p->laddr;
  537. t->parms.raddr = p->raddr;
  538. t->parms.link = p->link;
  539. t->parms.i_key = p->i_key;
  540. t->parms.o_key = p->o_key;
  541. t->parms.proto = p->proto;
  542. ip6_tnl_dst_reset(t);
  543. vti6_link_config(t);
  544. return 0;
  545. }
  546. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  547. {
  548. struct net *net = dev_net(t->dev);
  549. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  550. int err;
  551. vti6_tnl_unlink(ip6n, t);
  552. synchronize_net();
  553. err = vti6_tnl_change(t, p);
  554. vti6_tnl_link(ip6n, t);
  555. netdev_state_change(t->dev);
  556. return err;
  557. }
  558. static void
  559. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  560. {
  561. p->laddr = u->laddr;
  562. p->raddr = u->raddr;
  563. p->link = u->link;
  564. p->i_key = u->i_key;
  565. p->o_key = u->o_key;
  566. p->proto = u->proto;
  567. memcpy(p->name, u->name, sizeof(u->name));
  568. }
  569. static void
  570. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  571. {
  572. u->laddr = p->laddr;
  573. u->raddr = p->raddr;
  574. u->link = p->link;
  575. u->i_key = p->i_key;
  576. u->o_key = p->o_key;
  577. u->proto = p->proto;
  578. memcpy(u->name, p->name, sizeof(u->name));
  579. }
  580. /**
  581. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  582. * @dev: virtual device associated with tunnel
  583. * @ifr: parameters passed from userspace
  584. * @cmd: command to be performed
  585. *
  586. * Description:
  587. * vti6_ioctl() is used for managing vti6 tunnels
  588. * from userspace.
  589. *
  590. * The possible commands are the following:
  591. * %SIOCGETTUNNEL: get tunnel parameters for device
  592. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  593. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  594. * %SIOCDELTUNNEL: delete tunnel
  595. *
  596. * The fallback device "ip6_vti0", created during module
  597. * initialization, can be used for creating other tunnel devices.
  598. *
  599. * Return:
  600. * 0 on success,
  601. * %-EFAULT if unable to copy data to or from userspace,
  602. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  603. * %-EINVAL if passed tunnel parameters are invalid,
  604. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  605. * %-ENODEV if attempting to change or delete a nonexisting device
  606. **/
  607. static int
  608. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  609. {
  610. int err = 0;
  611. struct ip6_tnl_parm2 p;
  612. struct __ip6_tnl_parm p1;
  613. struct ip6_tnl *t = NULL;
  614. struct net *net = dev_net(dev);
  615. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  616. switch (cmd) {
  617. case SIOCGETTUNNEL:
  618. if (dev == ip6n->fb_tnl_dev) {
  619. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  620. err = -EFAULT;
  621. break;
  622. }
  623. vti6_parm_from_user(&p1, &p);
  624. t = vti6_locate(net, &p1, 0);
  625. } else {
  626. memset(&p, 0, sizeof(p));
  627. }
  628. if (!t)
  629. t = netdev_priv(dev);
  630. vti6_parm_to_user(&p, &t->parms);
  631. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  632. err = -EFAULT;
  633. break;
  634. case SIOCADDTUNNEL:
  635. case SIOCCHGTUNNEL:
  636. err = -EPERM;
  637. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  638. break;
  639. err = -EFAULT;
  640. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  641. break;
  642. err = -EINVAL;
  643. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  644. break;
  645. vti6_parm_from_user(&p1, &p);
  646. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  647. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  648. if (t) {
  649. if (t->dev != dev) {
  650. err = -EEXIST;
  651. break;
  652. }
  653. } else
  654. t = netdev_priv(dev);
  655. err = vti6_update(t, &p1);
  656. }
  657. if (t) {
  658. err = 0;
  659. vti6_parm_to_user(&p, &t->parms);
  660. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  661. err = -EFAULT;
  662. } else
  663. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  664. break;
  665. case SIOCDELTUNNEL:
  666. err = -EPERM;
  667. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  668. break;
  669. if (dev == ip6n->fb_tnl_dev) {
  670. err = -EFAULT;
  671. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  672. break;
  673. err = -ENOENT;
  674. vti6_parm_from_user(&p1, &p);
  675. t = vti6_locate(net, &p1, 0);
  676. if (!t)
  677. break;
  678. err = -EPERM;
  679. if (t->dev == ip6n->fb_tnl_dev)
  680. break;
  681. dev = t->dev;
  682. }
  683. err = 0;
  684. unregister_netdevice(dev);
  685. break;
  686. default:
  687. err = -EINVAL;
  688. }
  689. return err;
  690. }
  691. /**
  692. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  693. * @dev: virtual device associated with tunnel
  694. * @new_mtu: the new mtu
  695. *
  696. * Return:
  697. * 0 on success,
  698. * %-EINVAL if mtu too small
  699. **/
  700. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  701. {
  702. if (new_mtu < IPV6_MIN_MTU)
  703. return -EINVAL;
  704. dev->mtu = new_mtu;
  705. return 0;
  706. }
  707. static const struct net_device_ops vti6_netdev_ops = {
  708. .ndo_init = vti6_dev_init,
  709. .ndo_uninit = vti6_dev_uninit,
  710. .ndo_start_xmit = vti6_tnl_xmit,
  711. .ndo_do_ioctl = vti6_ioctl,
  712. .ndo_change_mtu = vti6_change_mtu,
  713. .ndo_get_stats64 = ip_tunnel_get_stats64,
  714. .ndo_get_iflink = ip6_tnl_get_iflink,
  715. };
  716. /**
  717. * vti6_dev_setup - setup virtual tunnel device
  718. * @dev: virtual device associated with tunnel
  719. *
  720. * Description:
  721. * Initialize function pointers and device parameters
  722. **/
  723. static void vti6_dev_setup(struct net_device *dev)
  724. {
  725. dev->netdev_ops = &vti6_netdev_ops;
  726. dev->destructor = vti6_dev_free;
  727. dev->type = ARPHRD_TUNNEL6;
  728. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  729. dev->mtu = ETH_DATA_LEN;
  730. dev->flags |= IFF_NOARP;
  731. dev->addr_len = sizeof(struct in6_addr);
  732. netif_keep_dst(dev);
  733. }
  734. /**
  735. * vti6_dev_init_gen - general initializer for all tunnel devices
  736. * @dev: virtual device associated with tunnel
  737. **/
  738. static inline int vti6_dev_init_gen(struct net_device *dev)
  739. {
  740. struct ip6_tnl *t = netdev_priv(dev);
  741. t->dev = dev;
  742. t->net = dev_net(dev);
  743. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  744. if (!dev->tstats)
  745. return -ENOMEM;
  746. return 0;
  747. }
  748. /**
  749. * vti6_dev_init - initializer for all non fallback tunnel devices
  750. * @dev: virtual device associated with tunnel
  751. **/
  752. static int vti6_dev_init(struct net_device *dev)
  753. {
  754. struct ip6_tnl *t = netdev_priv(dev);
  755. int err = vti6_dev_init_gen(dev);
  756. if (err)
  757. return err;
  758. vti6_link_config(t);
  759. return 0;
  760. }
  761. /**
  762. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  763. * @dev: fallback device
  764. *
  765. * Return: 0
  766. **/
  767. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  768. {
  769. struct ip6_tnl *t = netdev_priv(dev);
  770. struct net *net = dev_net(dev);
  771. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  772. t->parms.proto = IPPROTO_IPV6;
  773. dev_hold(dev);
  774. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  775. return 0;
  776. }
  777. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  778. {
  779. return 0;
  780. }
  781. static void vti6_netlink_parms(struct nlattr *data[],
  782. struct __ip6_tnl_parm *parms)
  783. {
  784. memset(parms, 0, sizeof(*parms));
  785. if (!data)
  786. return;
  787. if (data[IFLA_VTI_LINK])
  788. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  789. if (data[IFLA_VTI_LOCAL])
  790. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  791. if (data[IFLA_VTI_REMOTE])
  792. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  793. if (data[IFLA_VTI_IKEY])
  794. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  795. if (data[IFLA_VTI_OKEY])
  796. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  797. }
  798. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  799. struct nlattr *tb[], struct nlattr *data[])
  800. {
  801. struct net *net = dev_net(dev);
  802. struct ip6_tnl *nt;
  803. nt = netdev_priv(dev);
  804. vti6_netlink_parms(data, &nt->parms);
  805. nt->parms.proto = IPPROTO_IPV6;
  806. if (vti6_locate(net, &nt->parms, 0))
  807. return -EEXIST;
  808. return vti6_tnl_create2(dev);
  809. }
  810. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  811. {
  812. struct net *net = dev_net(dev);
  813. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  814. if (dev != ip6n->fb_tnl_dev)
  815. unregister_netdevice_queue(dev, head);
  816. }
  817. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  818. struct nlattr *data[])
  819. {
  820. struct ip6_tnl *t;
  821. struct __ip6_tnl_parm p;
  822. struct net *net = dev_net(dev);
  823. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  824. if (dev == ip6n->fb_tnl_dev)
  825. return -EINVAL;
  826. vti6_netlink_parms(data, &p);
  827. t = vti6_locate(net, &p, 0);
  828. if (t) {
  829. if (t->dev != dev)
  830. return -EEXIST;
  831. } else
  832. t = netdev_priv(dev);
  833. return vti6_update(t, &p);
  834. }
  835. static size_t vti6_get_size(const struct net_device *dev)
  836. {
  837. return
  838. /* IFLA_VTI_LINK */
  839. nla_total_size(4) +
  840. /* IFLA_VTI_LOCAL */
  841. nla_total_size(sizeof(struct in6_addr)) +
  842. /* IFLA_VTI_REMOTE */
  843. nla_total_size(sizeof(struct in6_addr)) +
  844. /* IFLA_VTI_IKEY */
  845. nla_total_size(4) +
  846. /* IFLA_VTI_OKEY */
  847. nla_total_size(4) +
  848. 0;
  849. }
  850. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  851. {
  852. struct ip6_tnl *tunnel = netdev_priv(dev);
  853. struct __ip6_tnl_parm *parm = &tunnel->parms;
  854. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  855. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  856. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  857. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  858. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
  859. goto nla_put_failure;
  860. return 0;
  861. nla_put_failure:
  862. return -EMSGSIZE;
  863. }
  864. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  865. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  866. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  867. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  868. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  869. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  870. };
  871. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  872. .kind = "vti6",
  873. .maxtype = IFLA_VTI_MAX,
  874. .policy = vti6_policy,
  875. .priv_size = sizeof(struct ip6_tnl),
  876. .setup = vti6_dev_setup,
  877. .validate = vti6_validate,
  878. .newlink = vti6_newlink,
  879. .dellink = vti6_dellink,
  880. .changelink = vti6_changelink,
  881. .get_size = vti6_get_size,
  882. .fill_info = vti6_fill_info,
  883. .get_link_net = ip6_tnl_get_link_net,
  884. };
  885. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  886. {
  887. int h;
  888. struct ip6_tnl *t;
  889. LIST_HEAD(list);
  890. for (h = 0; h < HASH_SIZE; h++) {
  891. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  892. while (t) {
  893. unregister_netdevice_queue(t->dev, &list);
  894. t = rtnl_dereference(t->next);
  895. }
  896. }
  897. t = rtnl_dereference(ip6n->tnls_wc[0]);
  898. unregister_netdevice_queue(t->dev, &list);
  899. unregister_netdevice_many(&list);
  900. }
  901. static int __net_init vti6_init_net(struct net *net)
  902. {
  903. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  904. struct ip6_tnl *t = NULL;
  905. int err;
  906. ip6n->tnls[0] = ip6n->tnls_wc;
  907. ip6n->tnls[1] = ip6n->tnls_r_l;
  908. err = -ENOMEM;
  909. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  910. NET_NAME_UNKNOWN, vti6_dev_setup);
  911. if (!ip6n->fb_tnl_dev)
  912. goto err_alloc_dev;
  913. dev_net_set(ip6n->fb_tnl_dev, net);
  914. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  915. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  916. if (err < 0)
  917. goto err_register;
  918. err = register_netdev(ip6n->fb_tnl_dev);
  919. if (err < 0)
  920. goto err_register;
  921. t = netdev_priv(ip6n->fb_tnl_dev);
  922. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  923. return 0;
  924. err_register:
  925. vti6_dev_free(ip6n->fb_tnl_dev);
  926. err_alloc_dev:
  927. return err;
  928. }
  929. static void __net_exit vti6_exit_net(struct net *net)
  930. {
  931. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  932. rtnl_lock();
  933. vti6_destroy_tunnels(ip6n);
  934. rtnl_unlock();
  935. }
  936. static struct pernet_operations vti6_net_ops = {
  937. .init = vti6_init_net,
  938. .exit = vti6_exit_net,
  939. .id = &vti6_net_id,
  940. .size = sizeof(struct vti6_net),
  941. };
  942. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  943. .handler = vti6_rcv,
  944. .cb_handler = vti6_rcv_cb,
  945. .err_handler = vti6_err,
  946. .priority = 100,
  947. };
  948. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  949. .handler = vti6_rcv,
  950. .cb_handler = vti6_rcv_cb,
  951. .err_handler = vti6_err,
  952. .priority = 100,
  953. };
  954. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  955. .handler = vti6_rcv,
  956. .cb_handler = vti6_rcv_cb,
  957. .err_handler = vti6_err,
  958. .priority = 100,
  959. };
  960. /**
  961. * vti6_tunnel_init - register protocol and reserve needed resources
  962. *
  963. * Return: 0 on success
  964. **/
  965. static int __init vti6_tunnel_init(void)
  966. {
  967. const char *msg;
  968. int err;
  969. msg = "tunnel device";
  970. err = register_pernet_device(&vti6_net_ops);
  971. if (err < 0)
  972. goto pernet_dev_failed;
  973. msg = "tunnel protocols";
  974. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  975. if (err < 0)
  976. goto xfrm_proto_esp_failed;
  977. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  978. if (err < 0)
  979. goto xfrm_proto_ah_failed;
  980. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  981. if (err < 0)
  982. goto xfrm_proto_comp_failed;
  983. msg = "netlink interface";
  984. err = rtnl_link_register(&vti6_link_ops);
  985. if (err < 0)
  986. goto rtnl_link_failed;
  987. return 0;
  988. rtnl_link_failed:
  989. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  990. xfrm_proto_comp_failed:
  991. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  992. xfrm_proto_ah_failed:
  993. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  994. xfrm_proto_esp_failed:
  995. unregister_pernet_device(&vti6_net_ops);
  996. pernet_dev_failed:
  997. pr_err("vti6 init: failed to register %s\n", msg);
  998. return err;
  999. }
  1000. /**
  1001. * vti6_tunnel_cleanup - free resources and unregister protocol
  1002. **/
  1003. static void __exit vti6_tunnel_cleanup(void)
  1004. {
  1005. rtnl_link_unregister(&vti6_link_ops);
  1006. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1007. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1008. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1009. unregister_pernet_device(&vti6_net_ops);
  1010. }
  1011. module_init(vti6_tunnel_init);
  1012. module_exit(vti6_tunnel_cleanup);
  1013. MODULE_LICENSE("GPL");
  1014. MODULE_ALIAS_RTNL_LINK("vti6");
  1015. MODULE_ALIAS_NETDEV("ip6_vti0");
  1016. MODULE_AUTHOR("Steffen Klassert");
  1017. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");