ip6_vti.c 30 KB

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