ip6_vti.c 28 KB

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