ip6_vti.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 virtual tunneling interface
  4. *
  5. * Copyright (C) 2013 secunet Security Networks AG
  6. *
  7. * Author:
  8. * Steffen Klassert <steffen.klassert@secunet.com>
  9. *
  10. * Based on:
  11. * net/ipv6/ip6_tunnel.c
  12. */
  13. #include <linux/module.h>
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/types.h>
  17. #include <linux/sockios.h>
  18. #include <linux/icmp.h>
  19. #include <linux/if.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/net.h>
  23. #include <linux/in6.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/icmpv6.h>
  27. #include <linux/init.h>
  28. #include <linux/route.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/netfilter_ipv6.h>
  31. #include <linux/slab.h>
  32. #include <linux/hash.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/atomic.h>
  35. #include <net/icmp.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/ipv6.h>
  39. #include <net/ip6_route.h>
  40. #include <net/addrconf.h>
  41. #include <net/ip6_tunnel.h>
  42. #include <net/xfrm.h>
  43. #include <net/net_namespace.h>
  44. #include <net/netns/generic.h>
  45. #include <linux/etherdevice.h>
  46. #define IP6_VTI_HASH_SIZE_SHIFT 5
  47. #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
  48. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  49. {
  50. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  51. return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
  52. }
  53. static int vti6_dev_init(struct net_device *dev);
  54. static void vti6_dev_setup(struct net_device *dev);
  55. static struct rtnl_link_ops vti6_link_ops __read_mostly;
  56. static unsigned int vti6_net_id __read_mostly;
  57. struct vti6_net {
  58. /* the vti6 tunnel fallback device */
  59. struct net_device *fb_tnl_dev;
  60. /* lists for storing tunnels in use */
  61. struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
  62. struct ip6_tnl __rcu *tnls_wc[1];
  63. struct ip6_tnl __rcu **tnls[2];
  64. };
  65. #define for_each_vti6_tunnel_rcu(start) \
  66. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  67. /**
  68. * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
  69. * @net: network namespace
  70. * @remote: the address of the tunnel exit-point
  71. * @local: the address of the tunnel entry-point
  72. *
  73. * Return:
  74. * tunnel matching given end-points if found,
  75. * else fallback tunnel if its device is up,
  76. * else %NULL
  77. **/
  78. static struct ip6_tnl *
  79. vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
  80. const struct in6_addr *local)
  81. {
  82. unsigned int hash = HASH(remote, local);
  83. struct ip6_tnl *t;
  84. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  85. struct in6_addr any;
  86. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  87. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  88. ipv6_addr_equal(remote, &t->parms.raddr) &&
  89. (t->dev->flags & IFF_UP))
  90. return t;
  91. }
  92. memset(&any, 0, sizeof(any));
  93. hash = HASH(&any, local);
  94. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  95. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  96. (t->dev->flags & IFF_UP))
  97. return t;
  98. }
  99. hash = HASH(remote, &any);
  100. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  101. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  102. (t->dev->flags & IFF_UP))
  103. return t;
  104. }
  105. t = rcu_dereference(ip6n->tnls_wc[0]);
  106. if (t && (t->dev->flags & IFF_UP))
  107. return t;
  108. return NULL;
  109. }
  110. /**
  111. * vti6_tnl_bucket - get head of list matching given tunnel parameters
  112. * @ip6n: the private data for ip6_vti in the netns
  113. * @p: parameters containing tunnel end-points
  114. *
  115. * Description:
  116. * vti6_tnl_bucket() returns the head of the list matching the
  117. * &struct in6_addr entries laddr and raddr in @p.
  118. *
  119. * Return: head of IPv6 tunnel list
  120. **/
  121. static struct ip6_tnl __rcu **
  122. vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
  123. {
  124. const struct in6_addr *remote = &p->raddr;
  125. const struct in6_addr *local = &p->laddr;
  126. unsigned int h = 0;
  127. int prio = 0;
  128. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  129. prio = 1;
  130. h = HASH(remote, local);
  131. }
  132. return &ip6n->tnls[prio][h];
  133. }
  134. static void
  135. vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
  136. {
  137. struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
  138. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  139. rcu_assign_pointer(*tp, t);
  140. }
  141. static void
  142. vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
  143. {
  144. struct ip6_tnl __rcu **tp;
  145. struct ip6_tnl *iter;
  146. for (tp = vti6_tnl_bucket(ip6n, &t->parms);
  147. (iter = rtnl_dereference(*tp)) != NULL;
  148. tp = &iter->next) {
  149. if (t == iter) {
  150. rcu_assign_pointer(*tp, t->next);
  151. break;
  152. }
  153. }
  154. }
  155. static int vti6_tnl_create2(struct net_device *dev)
  156. {
  157. struct ip6_tnl *t = netdev_priv(dev);
  158. struct net *net = dev_net(dev);
  159. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  160. int err;
  161. dev->rtnl_link_ops = &vti6_link_ops;
  162. err = register_netdevice(dev);
  163. if (err < 0)
  164. goto out;
  165. strcpy(t->parms.name, dev->name);
  166. vti6_tnl_link(ip6n, t);
  167. return 0;
  168. out:
  169. return err;
  170. }
  171. static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  172. {
  173. struct net_device *dev;
  174. struct ip6_tnl *t;
  175. char name[IFNAMSIZ];
  176. int err;
  177. if (p->name[0]) {
  178. if (!dev_valid_name(p->name))
  179. goto failed;
  180. strscpy(name, p->name, IFNAMSIZ);
  181. } else {
  182. sprintf(name, "ip6_vti%%d");
  183. }
  184. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
  185. if (!dev)
  186. goto failed;
  187. dev_net_set(dev, net);
  188. t = netdev_priv(dev);
  189. t->parms = *p;
  190. t->net = dev_net(dev);
  191. err = vti6_tnl_create2(dev);
  192. if (err < 0)
  193. goto failed_free;
  194. return t;
  195. failed_free:
  196. free_netdev(dev);
  197. failed:
  198. return NULL;
  199. }
  200. /**
  201. * vti6_locate - find or create tunnel matching given parameters
  202. * @net: network namespace
  203. * @p: tunnel parameters
  204. * @create: != 0 if allowed to create new tunnel if no match found
  205. *
  206. * Description:
  207. * vti6_locate() first tries to locate an existing tunnel
  208. * based on @parms. If this is unsuccessful, but @create is set a new
  209. * tunnel device is created and registered for use.
  210. *
  211. * Return:
  212. * matching tunnel or NULL
  213. **/
  214. static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
  215. int create)
  216. {
  217. const struct in6_addr *remote = &p->raddr;
  218. const struct in6_addr *local = &p->laddr;
  219. struct ip6_tnl __rcu **tp;
  220. struct ip6_tnl *t;
  221. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  222. for (tp = vti6_tnl_bucket(ip6n, p);
  223. (t = rtnl_dereference(*tp)) != NULL;
  224. tp = &t->next) {
  225. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  226. ipv6_addr_equal(remote, &t->parms.raddr)) {
  227. if (create)
  228. return NULL;
  229. return t;
  230. }
  231. }
  232. if (!create)
  233. return NULL;
  234. return vti6_tnl_create(net, p);
  235. }
  236. /**
  237. * vti6_dev_uninit - tunnel device uninitializer
  238. * @dev: the device to be destroyed
  239. *
  240. * Description:
  241. * vti6_dev_uninit() removes tunnel from its list
  242. **/
  243. static void vti6_dev_uninit(struct net_device *dev)
  244. {
  245. struct ip6_tnl *t = netdev_priv(dev);
  246. struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
  247. if (dev == ip6n->fb_tnl_dev)
  248. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  249. else
  250. vti6_tnl_unlink(ip6n, t);
  251. netdev_put(dev, &t->dev_tracker);
  252. }
  253. static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
  254. int encap_type)
  255. {
  256. struct ip6_tnl *t;
  257. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  258. rcu_read_lock();
  259. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  260. if (t) {
  261. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  262. rcu_read_unlock();
  263. goto discard;
  264. }
  265. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  266. rcu_read_unlock();
  267. goto discard;
  268. }
  269. ipv6h = ipv6_hdr(skb);
  270. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  271. DEV_STATS_INC(t->dev, rx_dropped);
  272. rcu_read_unlock();
  273. goto discard;
  274. }
  275. rcu_read_unlock();
  276. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  277. XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
  278. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  279. return xfrm_input(skb, nexthdr, spi, encap_type);
  280. }
  281. rcu_read_unlock();
  282. return -EINVAL;
  283. discard:
  284. kfree_skb(skb);
  285. return 0;
  286. }
  287. static int vti6_rcv(struct sk_buff *skb)
  288. {
  289. int nexthdr = skb_network_header(skb)[IP6CB(skb)->nhoff];
  290. return vti6_input_proto(skb, nexthdr, 0, 0);
  291. }
  292. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  293. {
  294. unsigned short family;
  295. struct net_device *dev;
  296. struct xfrm_state *x;
  297. const 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_INC(dev, rx_errors);
  306. DEV_STATS_INC(dev, 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->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. dev_sw_netstats_rx_add(dev, skb->len);
  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 dst_entry *dst = skb_dst(skb);
  377. struct net_device *tdev;
  378. struct xfrm_state *x;
  379. int pkt_len = skb->len;
  380. int err = -1;
  381. int mtu;
  382. if (!dst) {
  383. switch (skb->protocol) {
  384. case htons(ETH_P_IP): {
  385. struct rtable *rt;
  386. fl->u.ip4.flowi4_oif = dev->ifindex;
  387. fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
  388. rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
  389. if (IS_ERR(rt))
  390. goto tx_err_link_failure;
  391. dst = &rt->dst;
  392. skb_dst_set(skb, dst);
  393. break;
  394. }
  395. case htons(ETH_P_IPV6):
  396. fl->u.ip6.flowi6_oif = dev->ifindex;
  397. fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
  398. dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
  399. if (dst->error) {
  400. dst_release(dst);
  401. dst = NULL;
  402. goto tx_err_link_failure;
  403. }
  404. skb_dst_set(skb, dst);
  405. break;
  406. default:
  407. goto tx_err_link_failure;
  408. }
  409. }
  410. dst_hold(dst);
  411. dst = xfrm_lookup_route(t->net, dst, fl, NULL, 0);
  412. if (IS_ERR(dst)) {
  413. err = PTR_ERR(dst);
  414. dst = NULL;
  415. goto tx_err_link_failure;
  416. }
  417. if (dst->flags & DST_XFRM_QUEUE)
  418. goto xmit;
  419. x = dst->xfrm;
  420. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  421. goto tx_err_link_failure;
  422. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  423. (const struct in6_addr *)&x->id.daddr))
  424. goto tx_err_link_failure;
  425. tdev = dst->dev;
  426. if (tdev == dev) {
  427. DEV_STATS_INC(dev, collisions);
  428. net_warn_ratelimited("%s: Local routing loop detected!\n",
  429. t->parms.name);
  430. goto tx_err_dst_release;
  431. }
  432. mtu = dst_mtu(dst);
  433. if (skb->len > mtu) {
  434. skb_dst_update_pmtu_no_confirm(skb, mtu);
  435. if (skb->protocol == htons(ETH_P_IPV6)) {
  436. if (mtu < IPV6_MIN_MTU)
  437. mtu = IPV6_MIN_MTU;
  438. icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  439. } else {
  440. if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
  441. goto xmit;
  442. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  443. htonl(mtu));
  444. }
  445. err = -EMSGSIZE;
  446. goto tx_err_dst_release;
  447. }
  448. xmit:
  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. DEV_STATS_INC(dev, 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 flowi fl;
  469. int ret;
  470. if (!pskb_inet_may_pull(skb))
  471. goto tx_err;
  472. memset(&fl, 0, sizeof(fl));
  473. switch (skb->protocol) {
  474. case htons(ETH_P_IPV6):
  475. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  476. vti6_addr_conflict(t, ipv6_hdr(skb)))
  477. goto tx_err;
  478. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  479. xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET6);
  480. break;
  481. case htons(ETH_P_IP):
  482. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  483. xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET);
  484. break;
  485. default:
  486. goto tx_err;
  487. }
  488. /* override mark with tunnel output key */
  489. fl.flowi_mark = be32_to_cpu(t->parms.o_key);
  490. ret = vti6_xmit(skb, dev, &fl);
  491. if (ret < 0)
  492. goto tx_err;
  493. return NETDEV_TX_OK;
  494. tx_err:
  495. DEV_STATS_INC(dev, tx_errors);
  496. DEV_STATS_INC(dev, tx_dropped);
  497. kfree_skb(skb);
  498. return NETDEV_TX_OK;
  499. }
  500. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  501. u8 type, u8 code, int offset, __be32 info)
  502. {
  503. __be32 spi;
  504. __u32 mark;
  505. struct xfrm_state *x;
  506. struct ip6_tnl *t;
  507. struct ip_esp_hdr *esph;
  508. struct ip_auth_hdr *ah;
  509. struct ip_comp_hdr *ipch;
  510. struct net *net = dev_net(skb->dev);
  511. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  512. int protocol = iph->nexthdr;
  513. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  514. if (!t)
  515. return -1;
  516. mark = be32_to_cpu(t->parms.o_key);
  517. switch (protocol) {
  518. case IPPROTO_ESP:
  519. esph = (struct ip_esp_hdr *)(skb->data + offset);
  520. spi = esph->spi;
  521. break;
  522. case IPPROTO_AH:
  523. ah = (struct ip_auth_hdr *)(skb->data + offset);
  524. spi = ah->spi;
  525. break;
  526. case IPPROTO_COMP:
  527. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  528. spi = htonl(ntohs(ipch->cpi));
  529. break;
  530. default:
  531. return 0;
  532. }
  533. if (type != ICMPV6_PKT_TOOBIG &&
  534. type != NDISC_REDIRECT)
  535. return 0;
  536. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  537. spi, protocol, AF_INET6);
  538. if (!x)
  539. return 0;
  540. if (type == NDISC_REDIRECT)
  541. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  542. sock_net_uid(net, NULL));
  543. else
  544. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  545. xfrm_state_put(x);
  546. return 0;
  547. }
  548. static void vti6_link_config(struct ip6_tnl *t, bool keep_mtu)
  549. {
  550. struct net_device *dev = t->dev;
  551. struct __ip6_tnl_parm *p = &t->parms;
  552. struct net_device *tdev = NULL;
  553. int mtu;
  554. __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
  555. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  556. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  557. IP6_TNL_F_CAP_PER_PACKET);
  558. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  559. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  560. dev->flags |= IFF_POINTOPOINT;
  561. else
  562. dev->flags &= ~IFF_POINTOPOINT;
  563. if (keep_mtu && dev->mtu) {
  564. WRITE_ONCE(dev->mtu,
  565. 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_siocdevprivate - configure vti6 tunnels from userspace
  651. * @dev: virtual device associated with tunnel
  652. * @ifr: unused
  653. * @data: parameters passed from userspace
  654. * @cmd: command to be performed
  655. *
  656. * Description:
  657. * vti6_siocdevprivate() is used for managing vti6 tunnels
  658. * from userspace.
  659. *
  660. * The possible commands are the following:
  661. * %SIOCGETTUNNEL: get tunnel parameters for device
  662. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  663. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  664. * %SIOCDELTUNNEL: delete tunnel
  665. *
  666. * The fallback device "ip6_vti0", created during module
  667. * initialization, can be used for creating other tunnel devices.
  668. *
  669. * Return:
  670. * 0 on success,
  671. * %-EFAULT if unable to copy data to or from userspace,
  672. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  673. * %-EINVAL if passed tunnel parameters are invalid,
  674. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  675. * %-ENODEV if attempting to change or delete a nonexisting device
  676. **/
  677. static int
  678. vti6_siocdevprivate(struct net_device *dev, struct ifreq *ifr, void __user *data, int cmd)
  679. {
  680. int err = 0;
  681. struct ip6_tnl_parm2 p;
  682. struct __ip6_tnl_parm p1;
  683. struct ip6_tnl *t = NULL;
  684. struct net *net = dev_net(dev);
  685. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  686. memset(&p1, 0, sizeof(p1));
  687. switch (cmd) {
  688. case SIOCGETTUNNEL:
  689. if (dev == ip6n->fb_tnl_dev) {
  690. if (copy_from_user(&p, data, sizeof(p))) {
  691. err = -EFAULT;
  692. break;
  693. }
  694. vti6_parm_from_user(&p1, &p);
  695. t = vti6_locate(net, &p1, 0);
  696. } else {
  697. memset(&p, 0, sizeof(p));
  698. }
  699. if (!t)
  700. t = netdev_priv(dev);
  701. vti6_parm_to_user(&p, &t->parms);
  702. if (copy_to_user(data, &p, sizeof(p)))
  703. err = -EFAULT;
  704. break;
  705. case SIOCADDTUNNEL:
  706. case SIOCCHGTUNNEL:
  707. err = -EPERM;
  708. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  709. break;
  710. err = -EFAULT;
  711. if (copy_from_user(&p, data, sizeof(p)))
  712. break;
  713. err = -EINVAL;
  714. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  715. break;
  716. vti6_parm_from_user(&p1, &p);
  717. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  718. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  719. if (t) {
  720. if (t->dev != dev) {
  721. err = -EEXIST;
  722. break;
  723. }
  724. } else
  725. t = netdev_priv(dev);
  726. err = vti6_update(t, &p1, false);
  727. }
  728. if (t) {
  729. err = 0;
  730. vti6_parm_to_user(&p, &t->parms);
  731. if (copy_to_user(data, &p, sizeof(p)))
  732. err = -EFAULT;
  733. } else
  734. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  735. break;
  736. case SIOCDELTUNNEL:
  737. err = -EPERM;
  738. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  739. break;
  740. if (dev == ip6n->fb_tnl_dev) {
  741. err = -EFAULT;
  742. if (copy_from_user(&p, data, sizeof(p)))
  743. break;
  744. err = -ENOENT;
  745. vti6_parm_from_user(&p1, &p);
  746. t = vti6_locate(net, &p1, 0);
  747. if (!t)
  748. break;
  749. err = -EPERM;
  750. if (t->dev == ip6n->fb_tnl_dev)
  751. break;
  752. dev = t->dev;
  753. }
  754. err = 0;
  755. unregister_netdevice(dev);
  756. break;
  757. default:
  758. err = -EINVAL;
  759. }
  760. return err;
  761. }
  762. static const struct net_device_ops vti6_netdev_ops = {
  763. .ndo_init = vti6_dev_init,
  764. .ndo_uninit = vti6_dev_uninit,
  765. .ndo_start_xmit = vti6_tnl_xmit,
  766. .ndo_siocdevprivate = vti6_siocdevprivate,
  767. .ndo_get_iflink = ip6_tnl_get_iflink,
  768. };
  769. /**
  770. * vti6_dev_setup - setup virtual tunnel device
  771. * @dev: virtual device associated with tunnel
  772. *
  773. * Description:
  774. * Initialize function pointers and device parameters
  775. **/
  776. static void vti6_dev_setup(struct net_device *dev)
  777. {
  778. dev->netdev_ops = &vti6_netdev_ops;
  779. dev->header_ops = &ip_tunnel_header_ops;
  780. dev->needs_free_netdev = true;
  781. dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
  782. dev->type = ARPHRD_TUNNEL6;
  783. dev->min_mtu = IPV4_MIN_MTU;
  784. dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
  785. dev->flags |= IFF_NOARP;
  786. dev->addr_len = sizeof(struct in6_addr);
  787. netif_keep_dst(dev);
  788. /* This perm addr will be used as interface identifier by IPv6 */
  789. dev->addr_assign_type = NET_ADDR_RANDOM;
  790. eth_random_addr(dev->perm_addr);
  791. }
  792. /**
  793. * vti6_dev_init_gen - general initializer for all tunnel devices
  794. * @dev: virtual device associated with tunnel
  795. **/
  796. static inline int vti6_dev_init_gen(struct net_device *dev)
  797. {
  798. struct ip6_tnl *t = netdev_priv(dev);
  799. t->dev = dev;
  800. t->net = dev_net(dev);
  801. netdev_hold(dev, &t->dev_tracker, GFP_KERNEL);
  802. netdev_lockdep_set_classes(dev);
  803. return 0;
  804. }
  805. /**
  806. * vti6_dev_init - initializer for all non fallback tunnel devices
  807. * @dev: virtual device associated with tunnel
  808. **/
  809. static int vti6_dev_init(struct net_device *dev)
  810. {
  811. struct ip6_tnl *t = netdev_priv(dev);
  812. int err = vti6_dev_init_gen(dev);
  813. if (err)
  814. return err;
  815. vti6_link_config(t, true);
  816. return 0;
  817. }
  818. /**
  819. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  820. * @dev: fallback device
  821. *
  822. * Return: 0
  823. **/
  824. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  825. {
  826. struct ip6_tnl *t = netdev_priv(dev);
  827. struct net *net = dev_net(dev);
  828. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  829. t->parms.proto = IPPROTO_IPV6;
  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_rtnl(struct list_head *net_list,
  997. struct list_head *dev_to_kill)
  998. {
  999. struct vti6_net *ip6n;
  1000. struct net *net;
  1001. ASSERT_RTNL();
  1002. list_for_each_entry(net, net_list, exit_list) {
  1003. ip6n = net_generic(net, vti6_net_id);
  1004. vti6_destroy_tunnels(ip6n, dev_to_kill);
  1005. }
  1006. }
  1007. static struct pernet_operations vti6_net_ops = {
  1008. .init = vti6_init_net,
  1009. .exit_batch_rtnl = vti6_exit_batch_rtnl,
  1010. .id = &vti6_net_id,
  1011. .size = sizeof(struct vti6_net),
  1012. };
  1013. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  1014. .handler = vti6_rcv,
  1015. .input_handler = vti6_input_proto,
  1016. .cb_handler = vti6_rcv_cb,
  1017. .err_handler = vti6_err,
  1018. .priority = 100,
  1019. };
  1020. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  1021. .handler = vti6_rcv,
  1022. .input_handler = vti6_input_proto,
  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. .input_handler = vti6_input_proto,
  1030. .cb_handler = vti6_rcv_cb,
  1031. .err_handler = vti6_err,
  1032. .priority = 100,
  1033. };
  1034. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1035. static int vti6_rcv_tunnel(struct sk_buff *skb)
  1036. {
  1037. const xfrm_address_t *saddr;
  1038. __be32 spi;
  1039. saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
  1040. spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
  1041. return vti6_input_proto(skb, IPPROTO_IPV6, spi, 0);
  1042. }
  1043. static struct xfrm6_tunnel vti_ipv6_handler __read_mostly = {
  1044. .handler = vti6_rcv_tunnel,
  1045. .cb_handler = vti6_rcv_cb,
  1046. .err_handler = vti6_err,
  1047. .priority = 0,
  1048. };
  1049. static struct xfrm6_tunnel vti_ip6ip_handler __read_mostly = {
  1050. .handler = vti6_rcv_tunnel,
  1051. .cb_handler = vti6_rcv_cb,
  1052. .err_handler = vti6_err,
  1053. .priority = 0,
  1054. };
  1055. #endif
  1056. /**
  1057. * vti6_tunnel_init - register protocol and reserve needed resources
  1058. *
  1059. * Return: 0 on success
  1060. **/
  1061. static int __init vti6_tunnel_init(void)
  1062. {
  1063. const char *msg;
  1064. int err;
  1065. msg = "tunnel device";
  1066. err = register_pernet_device(&vti6_net_ops);
  1067. if (err < 0)
  1068. goto pernet_dev_failed;
  1069. msg = "tunnel protocols";
  1070. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1071. if (err < 0)
  1072. goto xfrm_proto_esp_failed;
  1073. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1074. if (err < 0)
  1075. goto xfrm_proto_ah_failed;
  1076. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1077. if (err < 0)
  1078. goto xfrm_proto_comp_failed;
  1079. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1080. msg = "ipv6 tunnel";
  1081. err = xfrm6_tunnel_register(&vti_ipv6_handler, AF_INET6);
  1082. if (err < 0)
  1083. goto vti_tunnel_ipv6_failed;
  1084. err = xfrm6_tunnel_register(&vti_ip6ip_handler, AF_INET);
  1085. if (err < 0)
  1086. goto vti_tunnel_ip6ip_failed;
  1087. #endif
  1088. msg = "netlink interface";
  1089. err = rtnl_link_register(&vti6_link_ops);
  1090. if (err < 0)
  1091. goto rtnl_link_failed;
  1092. return 0;
  1093. rtnl_link_failed:
  1094. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1095. err = xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1096. vti_tunnel_ip6ip_failed:
  1097. err = xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1098. vti_tunnel_ipv6_failed:
  1099. #endif
  1100. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1101. xfrm_proto_comp_failed:
  1102. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1103. xfrm_proto_ah_failed:
  1104. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1105. xfrm_proto_esp_failed:
  1106. unregister_pernet_device(&vti6_net_ops);
  1107. pernet_dev_failed:
  1108. pr_err("vti6 init: failed to register %s\n", msg);
  1109. return err;
  1110. }
  1111. /**
  1112. * vti6_tunnel_cleanup - free resources and unregister protocol
  1113. **/
  1114. static void __exit vti6_tunnel_cleanup(void)
  1115. {
  1116. rtnl_link_unregister(&vti6_link_ops);
  1117. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1118. xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1119. xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1120. #endif
  1121. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1122. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1123. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1124. unregister_pernet_device(&vti6_net_ops);
  1125. }
  1126. module_init(vti6_tunnel_init);
  1127. module_exit(vti6_tunnel_cleanup);
  1128. MODULE_LICENSE("GPL");
  1129. MODULE_ALIAS_RTNL_LINK("vti6");
  1130. MODULE_ALIAS_NETDEV("ip6_vti0");
  1131. MODULE_AUTHOR("Steffen Klassert");
  1132. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");