ip6_vti.c 29 KB

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