ip6_vti.c 27 KB

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