ip6_vti.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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 <net/netdev_lock.h>
  46. #include <linux/etherdevice.h>
  47. #define IP6_VTI_HASH_SIZE_SHIFT 5
  48. #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
  49. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  50. {
  51. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  52. return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
  53. }
  54. static int vti6_dev_init(struct net_device *dev);
  55. static void vti6_dev_setup(struct net_device *dev);
  56. static struct rtnl_link_ops vti6_link_ops __read_mostly;
  57. static unsigned int vti6_net_id __read_mostly;
  58. struct vti6_net {
  59. /* the vti6 tunnel fallback device */
  60. struct net_device *fb_tnl_dev;
  61. /* lists for storing tunnels in use */
  62. struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
  63. struct ip6_tnl __rcu *tnls_wc[1];
  64. struct ip6_tnl __rcu **tnls[2];
  65. };
  66. #define for_each_vti6_tunnel_rcu(start) \
  67. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  68. /**
  69. * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
  70. * @net: network namespace
  71. * @remote: the address of the tunnel exit-point
  72. * @local: the address of the tunnel entry-point
  73. *
  74. * Return:
  75. * tunnel matching given end-points if found,
  76. * else fallback tunnel if its device is up,
  77. * else %NULL
  78. **/
  79. static struct ip6_tnl *
  80. vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
  81. const struct in6_addr *local)
  82. {
  83. unsigned int hash = HASH(remote, local);
  84. struct ip6_tnl *t;
  85. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  86. struct in6_addr any;
  87. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  88. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  89. ipv6_addr_equal(remote, &t->parms.raddr) &&
  90. (t->dev->flags & IFF_UP))
  91. return t;
  92. }
  93. memset(&any, 0, sizeof(any));
  94. hash = HASH(&any, local);
  95. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  96. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  97. (t->dev->flags & IFF_UP))
  98. return t;
  99. }
  100. hash = HASH(remote, &any);
  101. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  102. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  103. (t->dev->flags & IFF_UP))
  104. return t;
  105. }
  106. t = rcu_dereference(ip6n->tnls_wc[0]);
  107. if (t && (t->dev->flags & IFF_UP))
  108. return t;
  109. return NULL;
  110. }
  111. /**
  112. * vti6_tnl_bucket - get head of list matching given tunnel parameters
  113. * @ip6n: the private data for ip6_vti in the netns
  114. * @p: parameters containing tunnel end-points
  115. *
  116. * Description:
  117. * vti6_tnl_bucket() returns the head of the list matching the
  118. * &struct in6_addr entries laddr and raddr in @p.
  119. *
  120. * Return: head of IPv6 tunnel list
  121. **/
  122. static struct ip6_tnl __rcu **
  123. vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
  124. {
  125. const struct in6_addr *remote = &p->raddr;
  126. const struct in6_addr *local = &p->laddr;
  127. unsigned int h = 0;
  128. int prio = 0;
  129. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  130. prio = 1;
  131. h = HASH(remote, local);
  132. }
  133. return &ip6n->tnls[prio][h];
  134. }
  135. static void
  136. vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
  137. {
  138. struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
  139. rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  140. rcu_assign_pointer(*tp, t);
  141. }
  142. static void
  143. vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
  144. {
  145. struct ip6_tnl __rcu **tp;
  146. struct ip6_tnl *iter;
  147. for (tp = vti6_tnl_bucket(ip6n, &t->parms);
  148. (iter = rtnl_dereference(*tp)) != NULL;
  149. tp = &iter->next) {
  150. if (t == iter) {
  151. rcu_assign_pointer(*tp, t->next);
  152. break;
  153. }
  154. }
  155. }
  156. static int vti6_tnl_create2(struct net_device *dev)
  157. {
  158. struct ip6_tnl *t = netdev_priv(dev);
  159. struct vti6_net *ip6n = net_generic(t->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(dst);
  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 = dst_dev(dst);
  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. netdev_hold(dev, &t->dev_tracker, GFP_KERNEL);
  801. netdev_lockdep_set_classes(dev);
  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->net = net;
  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_device *dev,
  858. struct rtnl_newlink_params *params,
  859. struct netlink_ext_ack *extack)
  860. {
  861. struct nlattr **data = params->data;
  862. struct ip6_tnl *nt;
  863. struct net *net;
  864. net = params->link_net ? : dev_net(dev);
  865. nt = netdev_priv(dev);
  866. vti6_netlink_parms(data, &nt->parms);
  867. nt->parms.proto = IPPROTO_IPV6;
  868. nt->net = net;
  869. if (vti6_locate(net, &nt->parms, 0))
  870. return -EEXIST;
  871. return vti6_tnl_create2(dev);
  872. }
  873. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  874. {
  875. struct net *net = dev_net(dev);
  876. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  877. if (dev != ip6n->fb_tnl_dev)
  878. unregister_netdevice_queue(dev, head);
  879. }
  880. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  881. struct nlattr *data[],
  882. struct netlink_ext_ack *extack)
  883. {
  884. struct ip6_tnl *t;
  885. struct __ip6_tnl_parm p;
  886. struct net *net = dev_net(dev);
  887. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  888. if (dev == ip6n->fb_tnl_dev)
  889. return -EINVAL;
  890. vti6_netlink_parms(data, &p);
  891. t = vti6_locate(net, &p, 0);
  892. if (t) {
  893. if (t->dev != dev)
  894. return -EEXIST;
  895. } else
  896. t = netdev_priv(dev);
  897. return vti6_update(t, &p, tb && tb[IFLA_MTU]);
  898. }
  899. static size_t vti6_get_size(const struct net_device *dev)
  900. {
  901. return
  902. /* IFLA_VTI_LINK */
  903. nla_total_size(4) +
  904. /* IFLA_VTI_LOCAL */
  905. nla_total_size(sizeof(struct in6_addr)) +
  906. /* IFLA_VTI_REMOTE */
  907. nla_total_size(sizeof(struct in6_addr)) +
  908. /* IFLA_VTI_IKEY */
  909. nla_total_size(4) +
  910. /* IFLA_VTI_OKEY */
  911. nla_total_size(4) +
  912. /* IFLA_VTI_FWMARK */
  913. nla_total_size(4) +
  914. 0;
  915. }
  916. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  917. {
  918. struct ip6_tnl *tunnel = netdev_priv(dev);
  919. struct __ip6_tnl_parm *parm = &tunnel->parms;
  920. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  921. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  922. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  923. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  924. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
  925. nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
  926. goto nla_put_failure;
  927. return 0;
  928. nla_put_failure:
  929. return -EMSGSIZE;
  930. }
  931. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  932. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  933. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  934. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  935. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  936. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  937. [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
  938. };
  939. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  940. .kind = "vti6",
  941. .maxtype = IFLA_VTI_MAX,
  942. .policy = vti6_policy,
  943. .priv_size = sizeof(struct ip6_tnl),
  944. .setup = vti6_dev_setup,
  945. .validate = vti6_validate,
  946. .newlink = vti6_newlink,
  947. .dellink = vti6_dellink,
  948. .changelink = vti6_changelink,
  949. .get_size = vti6_get_size,
  950. .fill_info = vti6_fill_info,
  951. .get_link_net = ip6_tnl_get_link_net,
  952. };
  953. static void __net_exit vti6_exit_rtnl_net(struct net *net, struct list_head *list)
  954. {
  955. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  956. struct ip6_tnl *t;
  957. int h;
  958. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  959. t = rtnl_net_dereference(net, ip6n->tnls_r_l[h]);
  960. while (t) {
  961. unregister_netdevice_queue(t->dev, list);
  962. t = rtnl_net_dereference(net, t->next);
  963. }
  964. }
  965. t = rtnl_net_dereference(net, ip6n->tnls_wc[0]);
  966. if (t)
  967. unregister_netdevice_queue(t->dev, list);
  968. }
  969. static int __net_init vti6_init_net(struct net *net)
  970. {
  971. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  972. struct ip6_tnl *t = NULL;
  973. int err;
  974. ip6n->tnls[0] = ip6n->tnls_wc;
  975. ip6n->tnls[1] = ip6n->tnls_r_l;
  976. if (!net_has_fallback_tunnels(net))
  977. return 0;
  978. err = -ENOMEM;
  979. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  980. NET_NAME_UNKNOWN, vti6_dev_setup);
  981. if (!ip6n->fb_tnl_dev)
  982. goto err_alloc_dev;
  983. dev_net_set(ip6n->fb_tnl_dev, net);
  984. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  985. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  986. if (err < 0)
  987. goto err_register;
  988. err = register_netdev(ip6n->fb_tnl_dev);
  989. if (err < 0)
  990. goto err_register;
  991. t = netdev_priv(ip6n->fb_tnl_dev);
  992. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  993. return 0;
  994. err_register:
  995. free_netdev(ip6n->fb_tnl_dev);
  996. err_alloc_dev:
  997. return err;
  998. }
  999. static struct pernet_operations vti6_net_ops = {
  1000. .init = vti6_init_net,
  1001. .exit_rtnl = vti6_exit_rtnl_net,
  1002. .id = &vti6_net_id,
  1003. .size = sizeof(struct vti6_net),
  1004. };
  1005. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  1006. .handler = vti6_rcv,
  1007. .input_handler = vti6_input_proto,
  1008. .cb_handler = vti6_rcv_cb,
  1009. .err_handler = vti6_err,
  1010. .priority = 100,
  1011. };
  1012. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  1013. .handler = vti6_rcv,
  1014. .input_handler = vti6_input_proto,
  1015. .cb_handler = vti6_rcv_cb,
  1016. .err_handler = vti6_err,
  1017. .priority = 100,
  1018. };
  1019. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  1020. .handler = vti6_rcv,
  1021. .input_handler = vti6_input_proto,
  1022. .cb_handler = vti6_rcv_cb,
  1023. .err_handler = vti6_err,
  1024. .priority = 100,
  1025. };
  1026. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1027. static int vti6_rcv_tunnel(struct sk_buff *skb)
  1028. {
  1029. const xfrm_address_t *saddr;
  1030. __be32 spi;
  1031. saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
  1032. spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
  1033. return vti6_input_proto(skb, IPPROTO_IPV6, spi, 0);
  1034. }
  1035. static struct xfrm6_tunnel vti_ipv6_handler __read_mostly = {
  1036. .handler = vti6_rcv_tunnel,
  1037. .cb_handler = vti6_rcv_cb,
  1038. .err_handler = vti6_err,
  1039. .priority = 0,
  1040. };
  1041. static struct xfrm6_tunnel vti_ip6ip_handler __read_mostly = {
  1042. .handler = vti6_rcv_tunnel,
  1043. .cb_handler = vti6_rcv_cb,
  1044. .err_handler = vti6_err,
  1045. .priority = 0,
  1046. };
  1047. #endif
  1048. /**
  1049. * vti6_tunnel_init - register protocol and reserve needed resources
  1050. *
  1051. * Return: 0 on success
  1052. **/
  1053. static int __init vti6_tunnel_init(void)
  1054. {
  1055. const char *msg;
  1056. int err;
  1057. msg = "tunnel device";
  1058. err = register_pernet_device(&vti6_net_ops);
  1059. if (err < 0)
  1060. goto pernet_dev_failed;
  1061. msg = "tunnel protocols";
  1062. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1063. if (err < 0)
  1064. goto xfrm_proto_esp_failed;
  1065. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1066. if (err < 0)
  1067. goto xfrm_proto_ah_failed;
  1068. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1069. if (err < 0)
  1070. goto xfrm_proto_comp_failed;
  1071. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1072. msg = "ipv6 tunnel";
  1073. err = xfrm6_tunnel_register(&vti_ipv6_handler, AF_INET6);
  1074. if (err < 0)
  1075. goto vti_tunnel_ipv6_failed;
  1076. err = xfrm6_tunnel_register(&vti_ip6ip_handler, AF_INET);
  1077. if (err < 0)
  1078. goto vti_tunnel_ip6ip_failed;
  1079. #endif
  1080. msg = "netlink interface";
  1081. err = rtnl_link_register(&vti6_link_ops);
  1082. if (err < 0)
  1083. goto rtnl_link_failed;
  1084. return 0;
  1085. rtnl_link_failed:
  1086. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1087. err = xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1088. vti_tunnel_ip6ip_failed:
  1089. err = xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1090. vti_tunnel_ipv6_failed:
  1091. #endif
  1092. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1093. xfrm_proto_comp_failed:
  1094. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1095. xfrm_proto_ah_failed:
  1096. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1097. xfrm_proto_esp_failed:
  1098. unregister_pernet_device(&vti6_net_ops);
  1099. pernet_dev_failed:
  1100. pr_err("vti6 init: failed to register %s\n", msg);
  1101. return err;
  1102. }
  1103. /**
  1104. * vti6_tunnel_cleanup - free resources and unregister protocol
  1105. **/
  1106. static void __exit vti6_tunnel_cleanup(void)
  1107. {
  1108. rtnl_link_unregister(&vti6_link_ops);
  1109. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1110. xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1111. xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1112. #endif
  1113. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1114. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1115. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1116. unregister_pernet_device(&vti6_net_ops);
  1117. }
  1118. module_init(vti6_tunnel_init);
  1119. module_exit(vti6_tunnel_cleanup);
  1120. MODULE_LICENSE("GPL");
  1121. MODULE_ALIAS_RTNL_LINK("vti6");
  1122. MODULE_ALIAS_NETDEV("ip6_vti0");
  1123. MODULE_AUTHOR("Steffen Klassert");
  1124. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");