ip6_vti.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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 <[email protected]>
  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 void vti6_dev_free(struct net_device *dev)
  156. {
  157. free_percpu(dev->tstats);
  158. }
  159. static int vti6_tnl_create2(struct net_device *dev)
  160. {
  161. struct ip6_tnl *t = netdev_priv(dev);
  162. struct net *net = dev_net(dev);
  163. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  164. int err;
  165. dev->rtnl_link_ops = &vti6_link_ops;
  166. err = register_netdevice(dev);
  167. if (err < 0)
  168. goto out;
  169. strcpy(t->parms.name, dev->name);
  170. vti6_tnl_link(ip6n, t);
  171. return 0;
  172. out:
  173. return err;
  174. }
  175. static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  176. {
  177. struct net_device *dev;
  178. struct ip6_tnl *t;
  179. char name[IFNAMSIZ];
  180. int err;
  181. if (p->name[0]) {
  182. if (!dev_valid_name(p->name))
  183. goto failed;
  184. strscpy(name, p->name, IFNAMSIZ);
  185. } else {
  186. sprintf(name, "ip6_vti%%d");
  187. }
  188. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
  189. if (!dev)
  190. goto failed;
  191. dev_net_set(dev, net);
  192. t = netdev_priv(dev);
  193. t->parms = *p;
  194. t->net = dev_net(dev);
  195. err = vti6_tnl_create2(dev);
  196. if (err < 0)
  197. goto failed_free;
  198. return t;
  199. failed_free:
  200. free_netdev(dev);
  201. failed:
  202. return NULL;
  203. }
  204. /**
  205. * vti6_locate - find or create tunnel matching given parameters
  206. * @net: network namespace
  207. * @p: tunnel parameters
  208. * @create: != 0 if allowed to create new tunnel if no match found
  209. *
  210. * Description:
  211. * vti6_locate() first tries to locate an existing tunnel
  212. * based on @parms. If this is unsuccessful, but @create is set a new
  213. * tunnel device is created and registered for use.
  214. *
  215. * Return:
  216. * matching tunnel or NULL
  217. **/
  218. static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
  219. int create)
  220. {
  221. const struct in6_addr *remote = &p->raddr;
  222. const struct in6_addr *local = &p->laddr;
  223. struct ip6_tnl __rcu **tp;
  224. struct ip6_tnl *t;
  225. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  226. for (tp = vti6_tnl_bucket(ip6n, p);
  227. (t = rtnl_dereference(*tp)) != NULL;
  228. tp = &t->next) {
  229. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  230. ipv6_addr_equal(remote, &t->parms.raddr)) {
  231. if (create)
  232. return NULL;
  233. return t;
  234. }
  235. }
  236. if (!create)
  237. return NULL;
  238. return vti6_tnl_create(net, p);
  239. }
  240. /**
  241. * vti6_dev_uninit - tunnel device uninitializer
  242. * @dev: the device to be destroyed
  243. *
  244. * Description:
  245. * vti6_dev_uninit() removes tunnel from its list
  246. **/
  247. static void vti6_dev_uninit(struct net_device *dev)
  248. {
  249. struct ip6_tnl *t = netdev_priv(dev);
  250. struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
  251. if (dev == ip6n->fb_tnl_dev)
  252. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  253. else
  254. vti6_tnl_unlink(ip6n, t);
  255. netdev_put(dev, &t->dev_tracker);
  256. }
  257. static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
  258. int encap_type)
  259. {
  260. struct ip6_tnl *t;
  261. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  262. rcu_read_lock();
  263. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  264. if (t) {
  265. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  266. rcu_read_unlock();
  267. goto discard;
  268. }
  269. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  270. rcu_read_unlock();
  271. goto discard;
  272. }
  273. ipv6h = ipv6_hdr(skb);
  274. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  275. t->dev->stats.rx_dropped++;
  276. rcu_read_unlock();
  277. goto discard;
  278. }
  279. rcu_read_unlock();
  280. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  281. XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
  282. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  283. return xfrm_input(skb, nexthdr, spi, encap_type);
  284. }
  285. rcu_read_unlock();
  286. return -EINVAL;
  287. discard:
  288. kfree_skb(skb);
  289. return 0;
  290. }
  291. static int vti6_rcv(struct sk_buff *skb)
  292. {
  293. int nexthdr = skb_network_header(skb)[IP6CB(skb)->nhoff];
  294. return vti6_input_proto(skb, nexthdr, 0, 0);
  295. }
  296. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  297. {
  298. unsigned short family;
  299. struct net_device *dev;
  300. struct xfrm_state *x;
  301. const struct xfrm_mode *inner_mode;
  302. struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
  303. u32 orig_mark = skb->mark;
  304. int ret;
  305. if (!t)
  306. return 1;
  307. dev = t->dev;
  308. if (err) {
  309. dev->stats.rx_errors++;
  310. dev->stats.rx_dropped++;
  311. return 0;
  312. }
  313. x = xfrm_input_state(skb);
  314. inner_mode = &x->inner_mode;
  315. if (x->sel.family == AF_UNSPEC) {
  316. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  317. if (inner_mode == NULL) {
  318. XFRM_INC_STATS(dev_net(skb->dev),
  319. LINUX_MIB_XFRMINSTATEMODEERROR);
  320. return -EINVAL;
  321. }
  322. }
  323. family = inner_mode->family;
  324. skb->mark = be32_to_cpu(t->parms.i_key);
  325. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  326. skb->mark = orig_mark;
  327. if (!ret)
  328. return -EPERM;
  329. skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
  330. skb->dev = dev;
  331. dev_sw_netstats_rx_add(dev, skb->len);
  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_route(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. if (dst->flags & DST_XFRM_QUEUE)
  423. goto xmit;
  424. x = dst->xfrm;
  425. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  426. goto tx_err_link_failure;
  427. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  428. (const struct in6_addr *)&x->id.daddr))
  429. goto tx_err_link_failure;
  430. tdev = dst->dev;
  431. if (tdev == dev) {
  432. stats->collisions++;
  433. net_warn_ratelimited("%s: Local routing loop detected!\n",
  434. t->parms.name);
  435. goto tx_err_dst_release;
  436. }
  437. mtu = dst_mtu(dst);
  438. if (skb->len > mtu) {
  439. skb_dst_update_pmtu_no_confirm(skb, mtu);
  440. if (skb->protocol == htons(ETH_P_IPV6)) {
  441. if (mtu < IPV6_MIN_MTU)
  442. mtu = IPV6_MIN_MTU;
  443. icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  444. } else {
  445. if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
  446. goto xmit;
  447. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  448. htonl(mtu));
  449. }
  450. err = -EMSGSIZE;
  451. goto tx_err_dst_release;
  452. }
  453. xmit:
  454. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  455. skb_dst_set(skb, dst);
  456. skb->dev = skb_dst(skb)->dev;
  457. err = dst_output(t->net, skb->sk, skb);
  458. if (net_xmit_eval(err) == 0)
  459. err = pkt_len;
  460. iptunnel_xmit_stats(dev, err);
  461. return 0;
  462. tx_err_link_failure:
  463. stats->tx_carrier_errors++;
  464. dst_link_failure(skb);
  465. tx_err_dst_release:
  466. dst_release(dst);
  467. return err;
  468. }
  469. static netdev_tx_t
  470. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  471. {
  472. struct ip6_tnl *t = netdev_priv(dev);
  473. struct net_device_stats *stats = &t->dev->stats;
  474. struct flowi fl;
  475. int ret;
  476. if (!pskb_inet_may_pull(skb))
  477. goto tx_err;
  478. memset(&fl, 0, sizeof(fl));
  479. switch (skb->protocol) {
  480. case htons(ETH_P_IPV6):
  481. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  482. vti6_addr_conflict(t, ipv6_hdr(skb)))
  483. goto tx_err;
  484. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  485. xfrm_decode_session(skb, &fl, AF_INET6);
  486. break;
  487. case htons(ETH_P_IP):
  488. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  489. xfrm_decode_session(skb, &fl, AF_INET);
  490. break;
  491. default:
  492. goto tx_err;
  493. }
  494. /* override mark with tunnel output key */
  495. fl.flowi_mark = be32_to_cpu(t->parms.o_key);
  496. ret = vti6_xmit(skb, dev, &fl);
  497. if (ret < 0)
  498. goto tx_err;
  499. return NETDEV_TX_OK;
  500. tx_err:
  501. stats->tx_errors++;
  502. stats->tx_dropped++;
  503. kfree_skb(skb);
  504. return NETDEV_TX_OK;
  505. }
  506. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  507. u8 type, u8 code, int offset, __be32 info)
  508. {
  509. __be32 spi;
  510. __u32 mark;
  511. struct xfrm_state *x;
  512. struct ip6_tnl *t;
  513. struct ip_esp_hdr *esph;
  514. struct ip_auth_hdr *ah;
  515. struct ip_comp_hdr *ipch;
  516. struct net *net = dev_net(skb->dev);
  517. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  518. int protocol = iph->nexthdr;
  519. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  520. if (!t)
  521. return -1;
  522. mark = be32_to_cpu(t->parms.o_key);
  523. switch (protocol) {
  524. case IPPROTO_ESP:
  525. esph = (struct ip_esp_hdr *)(skb->data + offset);
  526. spi = esph->spi;
  527. break;
  528. case IPPROTO_AH:
  529. ah = (struct ip_auth_hdr *)(skb->data + offset);
  530. spi = ah->spi;
  531. break;
  532. case IPPROTO_COMP:
  533. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  534. spi = htonl(ntohs(ipch->cpi));
  535. break;
  536. default:
  537. return 0;
  538. }
  539. if (type != ICMPV6_PKT_TOOBIG &&
  540. type != NDISC_REDIRECT)
  541. return 0;
  542. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  543. spi, protocol, AF_INET6);
  544. if (!x)
  545. return 0;
  546. if (type == NDISC_REDIRECT)
  547. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  548. sock_net_uid(net, NULL));
  549. else
  550. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  551. xfrm_state_put(x);
  552. return 0;
  553. }
  554. static void vti6_link_config(struct ip6_tnl *t, bool keep_mtu)
  555. {
  556. struct net_device *dev = t->dev;
  557. struct __ip6_tnl_parm *p = &t->parms;
  558. struct net_device *tdev = NULL;
  559. int mtu;
  560. __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
  561. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  562. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  563. IP6_TNL_F_CAP_PER_PACKET);
  564. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  565. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  566. dev->flags |= IFF_POINTOPOINT;
  567. else
  568. dev->flags &= ~IFF_POINTOPOINT;
  569. if (keep_mtu && dev->mtu) {
  570. dev->mtu = clamp(dev->mtu, dev->min_mtu, dev->max_mtu);
  571. return;
  572. }
  573. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  574. int strict = (ipv6_addr_type(&p->raddr) &
  575. (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
  576. struct rt6_info *rt = rt6_lookup(t->net,
  577. &p->raddr, &p->laddr,
  578. p->link, NULL, strict);
  579. if (rt)
  580. tdev = rt->dst.dev;
  581. ip6_rt_put(rt);
  582. }
  583. if (!tdev && p->link)
  584. tdev = __dev_get_by_index(t->net, p->link);
  585. if (tdev)
  586. mtu = tdev->mtu - sizeof(struct ipv6hdr);
  587. else
  588. mtu = ETH_DATA_LEN - LL_MAX_HEADER - sizeof(struct ipv6hdr);
  589. dev->mtu = max_t(int, mtu, IPV4_MIN_MTU);
  590. }
  591. /**
  592. * vti6_tnl_change - update the tunnel parameters
  593. * @t: tunnel to be changed
  594. * @p: tunnel configuration parameters
  595. * @keep_mtu: MTU was set from userspace, don't re-compute it
  596. *
  597. * Description:
  598. * vti6_tnl_change() updates the tunnel parameters
  599. **/
  600. static int
  601. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
  602. bool keep_mtu)
  603. {
  604. t->parms.laddr = p->laddr;
  605. t->parms.raddr = p->raddr;
  606. t->parms.link = p->link;
  607. t->parms.i_key = p->i_key;
  608. t->parms.o_key = p->o_key;
  609. t->parms.proto = p->proto;
  610. t->parms.fwmark = p->fwmark;
  611. dst_cache_reset(&t->dst_cache);
  612. vti6_link_config(t, keep_mtu);
  613. return 0;
  614. }
  615. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p,
  616. bool keep_mtu)
  617. {
  618. struct net *net = dev_net(t->dev);
  619. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  620. int err;
  621. vti6_tnl_unlink(ip6n, t);
  622. synchronize_net();
  623. err = vti6_tnl_change(t, p, keep_mtu);
  624. vti6_tnl_link(ip6n, t);
  625. netdev_state_change(t->dev);
  626. return err;
  627. }
  628. static void
  629. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  630. {
  631. p->laddr = u->laddr;
  632. p->raddr = u->raddr;
  633. p->link = u->link;
  634. p->i_key = u->i_key;
  635. p->o_key = u->o_key;
  636. p->proto = u->proto;
  637. memcpy(p->name, u->name, sizeof(u->name));
  638. }
  639. static void
  640. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  641. {
  642. u->laddr = p->laddr;
  643. u->raddr = p->raddr;
  644. u->link = p->link;
  645. u->i_key = p->i_key;
  646. u->o_key = p->o_key;
  647. if (u->i_key)
  648. u->i_flags |= GRE_KEY;
  649. if (u->o_key)
  650. u->o_flags |= GRE_KEY;
  651. u->proto = p->proto;
  652. memcpy(u->name, p->name, sizeof(u->name));
  653. }
  654. /**
  655. * vti6_siocdevprivate - configure vti6 tunnels from userspace
  656. * @dev: virtual device associated with tunnel
  657. * @ifr: unused
  658. * @data: parameters passed from userspace
  659. * @cmd: command to be performed
  660. *
  661. * Description:
  662. * vti6_siocdevprivate() is used for managing vti6 tunnels
  663. * from userspace.
  664. *
  665. * The possible commands are the following:
  666. * %SIOCGETTUNNEL: get tunnel parameters for device
  667. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  668. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  669. * %SIOCDELTUNNEL: delete tunnel
  670. *
  671. * The fallback device "ip6_vti0", created during module
  672. * initialization, can be used for creating other tunnel devices.
  673. *
  674. * Return:
  675. * 0 on success,
  676. * %-EFAULT if unable to copy data to or from userspace,
  677. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  678. * %-EINVAL if passed tunnel parameters are invalid,
  679. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  680. * %-ENODEV if attempting to change or delete a nonexisting device
  681. **/
  682. static int
  683. vti6_siocdevprivate(struct net_device *dev, struct ifreq *ifr, void __user *data, int cmd)
  684. {
  685. int err = 0;
  686. struct ip6_tnl_parm2 p;
  687. struct __ip6_tnl_parm p1;
  688. struct ip6_tnl *t = NULL;
  689. struct net *net = dev_net(dev);
  690. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  691. memset(&p1, 0, sizeof(p1));
  692. switch (cmd) {
  693. case SIOCGETTUNNEL:
  694. if (dev == ip6n->fb_tnl_dev) {
  695. if (copy_from_user(&p, data, sizeof(p))) {
  696. err = -EFAULT;
  697. break;
  698. }
  699. vti6_parm_from_user(&p1, &p);
  700. t = vti6_locate(net, &p1, 0);
  701. } else {
  702. memset(&p, 0, sizeof(p));
  703. }
  704. if (!t)
  705. t = netdev_priv(dev);
  706. vti6_parm_to_user(&p, &t->parms);
  707. if (copy_to_user(data, &p, sizeof(p)))
  708. err = -EFAULT;
  709. break;
  710. case SIOCADDTUNNEL:
  711. case SIOCCHGTUNNEL:
  712. err = -EPERM;
  713. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  714. break;
  715. err = -EFAULT;
  716. if (copy_from_user(&p, data, sizeof(p)))
  717. break;
  718. err = -EINVAL;
  719. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  720. break;
  721. vti6_parm_from_user(&p1, &p);
  722. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  723. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  724. if (t) {
  725. if (t->dev != dev) {
  726. err = -EEXIST;
  727. break;
  728. }
  729. } else
  730. t = netdev_priv(dev);
  731. err = vti6_update(t, &p1, false);
  732. }
  733. if (t) {
  734. err = 0;
  735. vti6_parm_to_user(&p, &t->parms);
  736. if (copy_to_user(data, &p, sizeof(p)))
  737. err = -EFAULT;
  738. } else
  739. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  740. break;
  741. case SIOCDELTUNNEL:
  742. err = -EPERM;
  743. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  744. break;
  745. if (dev == ip6n->fb_tnl_dev) {
  746. err = -EFAULT;
  747. if (copy_from_user(&p, data, sizeof(p)))
  748. break;
  749. err = -ENOENT;
  750. vti6_parm_from_user(&p1, &p);
  751. t = vti6_locate(net, &p1, 0);
  752. if (!t)
  753. break;
  754. err = -EPERM;
  755. if (t->dev == ip6n->fb_tnl_dev)
  756. break;
  757. dev = t->dev;
  758. }
  759. err = 0;
  760. unregister_netdevice(dev);
  761. break;
  762. default:
  763. err = -EINVAL;
  764. }
  765. return err;
  766. }
  767. static const struct net_device_ops vti6_netdev_ops = {
  768. .ndo_init = vti6_dev_init,
  769. .ndo_uninit = vti6_dev_uninit,
  770. .ndo_start_xmit = vti6_tnl_xmit,
  771. .ndo_siocdevprivate = vti6_siocdevprivate,
  772. .ndo_get_stats64 = dev_get_tstats64,
  773. .ndo_get_iflink = ip6_tnl_get_iflink,
  774. };
  775. /**
  776. * vti6_dev_setup - setup virtual tunnel device
  777. * @dev: virtual device associated with tunnel
  778. *
  779. * Description:
  780. * Initialize function pointers and device parameters
  781. **/
  782. static void vti6_dev_setup(struct net_device *dev)
  783. {
  784. dev->netdev_ops = &vti6_netdev_ops;
  785. dev->header_ops = &ip_tunnel_header_ops;
  786. dev->needs_free_netdev = true;
  787. dev->priv_destructor = vti6_dev_free;
  788. dev->type = ARPHRD_TUNNEL6;
  789. dev->min_mtu = IPV4_MIN_MTU;
  790. dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
  791. dev->flags |= IFF_NOARP;
  792. dev->addr_len = sizeof(struct in6_addr);
  793. netif_keep_dst(dev);
  794. /* This perm addr will be used as interface identifier by IPv6 */
  795. dev->addr_assign_type = NET_ADDR_RANDOM;
  796. eth_random_addr(dev->perm_addr);
  797. }
  798. /**
  799. * vti6_dev_init_gen - general initializer for all tunnel devices
  800. * @dev: virtual device associated with tunnel
  801. **/
  802. static inline int vti6_dev_init_gen(struct net_device *dev)
  803. {
  804. struct ip6_tnl *t = netdev_priv(dev);
  805. t->dev = dev;
  806. t->net = dev_net(dev);
  807. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  808. if (!dev->tstats)
  809. return -ENOMEM;
  810. netdev_hold(dev, &t->dev_tracker, GFP_KERNEL);
  811. return 0;
  812. }
  813. /**
  814. * vti6_dev_init - initializer for all non fallback tunnel devices
  815. * @dev: virtual device associated with tunnel
  816. **/
  817. static int vti6_dev_init(struct net_device *dev)
  818. {
  819. struct ip6_tnl *t = netdev_priv(dev);
  820. int err = vti6_dev_init_gen(dev);
  821. if (err)
  822. return err;
  823. vti6_link_config(t, true);
  824. return 0;
  825. }
  826. /**
  827. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  828. * @dev: fallback device
  829. *
  830. * Return: 0
  831. **/
  832. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  833. {
  834. struct ip6_tnl *t = netdev_priv(dev);
  835. struct net *net = dev_net(dev);
  836. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  837. t->parms.proto = IPPROTO_IPV6;
  838. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  839. return 0;
  840. }
  841. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[],
  842. struct netlink_ext_ack *extack)
  843. {
  844. return 0;
  845. }
  846. static void vti6_netlink_parms(struct nlattr *data[],
  847. struct __ip6_tnl_parm *parms)
  848. {
  849. memset(parms, 0, sizeof(*parms));
  850. if (!data)
  851. return;
  852. if (data[IFLA_VTI_LINK])
  853. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  854. if (data[IFLA_VTI_LOCAL])
  855. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  856. if (data[IFLA_VTI_REMOTE])
  857. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  858. if (data[IFLA_VTI_IKEY])
  859. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  860. if (data[IFLA_VTI_OKEY])
  861. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  862. if (data[IFLA_VTI_FWMARK])
  863. parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
  864. }
  865. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  866. struct nlattr *tb[], struct nlattr *data[],
  867. struct netlink_ext_ack *extack)
  868. {
  869. struct net *net = dev_net(dev);
  870. struct ip6_tnl *nt;
  871. nt = netdev_priv(dev);
  872. vti6_netlink_parms(data, &nt->parms);
  873. nt->parms.proto = IPPROTO_IPV6;
  874. if (vti6_locate(net, &nt->parms, 0))
  875. return -EEXIST;
  876. return vti6_tnl_create2(dev);
  877. }
  878. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  879. {
  880. struct net *net = dev_net(dev);
  881. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  882. if (dev != ip6n->fb_tnl_dev)
  883. unregister_netdevice_queue(dev, head);
  884. }
  885. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  886. struct nlattr *data[],
  887. struct netlink_ext_ack *extack)
  888. {
  889. struct ip6_tnl *t;
  890. struct __ip6_tnl_parm p;
  891. struct net *net = dev_net(dev);
  892. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  893. if (dev == ip6n->fb_tnl_dev)
  894. return -EINVAL;
  895. vti6_netlink_parms(data, &p);
  896. t = vti6_locate(net, &p, 0);
  897. if (t) {
  898. if (t->dev != dev)
  899. return -EEXIST;
  900. } else
  901. t = netdev_priv(dev);
  902. return vti6_update(t, &p, tb && tb[IFLA_MTU]);
  903. }
  904. static size_t vti6_get_size(const struct net_device *dev)
  905. {
  906. return
  907. /* IFLA_VTI_LINK */
  908. nla_total_size(4) +
  909. /* IFLA_VTI_LOCAL */
  910. nla_total_size(sizeof(struct in6_addr)) +
  911. /* IFLA_VTI_REMOTE */
  912. nla_total_size(sizeof(struct in6_addr)) +
  913. /* IFLA_VTI_IKEY */
  914. nla_total_size(4) +
  915. /* IFLA_VTI_OKEY */
  916. nla_total_size(4) +
  917. /* IFLA_VTI_FWMARK */
  918. nla_total_size(4) +
  919. 0;
  920. }
  921. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  922. {
  923. struct ip6_tnl *tunnel = netdev_priv(dev);
  924. struct __ip6_tnl_parm *parm = &tunnel->parms;
  925. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  926. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  927. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  928. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  929. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
  930. nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
  931. goto nla_put_failure;
  932. return 0;
  933. nla_put_failure:
  934. return -EMSGSIZE;
  935. }
  936. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  937. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  938. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  939. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  940. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  941. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  942. [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
  943. };
  944. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  945. .kind = "vti6",
  946. .maxtype = IFLA_VTI_MAX,
  947. .policy = vti6_policy,
  948. .priv_size = sizeof(struct ip6_tnl),
  949. .setup = vti6_dev_setup,
  950. .validate = vti6_validate,
  951. .newlink = vti6_newlink,
  952. .dellink = vti6_dellink,
  953. .changelink = vti6_changelink,
  954. .get_size = vti6_get_size,
  955. .fill_info = vti6_fill_info,
  956. .get_link_net = ip6_tnl_get_link_net,
  957. };
  958. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n,
  959. struct list_head *list)
  960. {
  961. int h;
  962. struct ip6_tnl *t;
  963. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  964. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  965. while (t) {
  966. unregister_netdevice_queue(t->dev, list);
  967. t = rtnl_dereference(t->next);
  968. }
  969. }
  970. t = rtnl_dereference(ip6n->tnls_wc[0]);
  971. if (t)
  972. unregister_netdevice_queue(t->dev, list);
  973. }
  974. static int __net_init vti6_init_net(struct net *net)
  975. {
  976. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  977. struct ip6_tnl *t = NULL;
  978. int err;
  979. ip6n->tnls[0] = ip6n->tnls_wc;
  980. ip6n->tnls[1] = ip6n->tnls_r_l;
  981. if (!net_has_fallback_tunnels(net))
  982. return 0;
  983. err = -ENOMEM;
  984. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  985. NET_NAME_UNKNOWN, vti6_dev_setup);
  986. if (!ip6n->fb_tnl_dev)
  987. goto err_alloc_dev;
  988. dev_net_set(ip6n->fb_tnl_dev, net);
  989. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  990. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  991. if (err < 0)
  992. goto err_register;
  993. err = register_netdev(ip6n->fb_tnl_dev);
  994. if (err < 0)
  995. goto err_register;
  996. t = netdev_priv(ip6n->fb_tnl_dev);
  997. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  998. return 0;
  999. err_register:
  1000. free_netdev(ip6n->fb_tnl_dev);
  1001. err_alloc_dev:
  1002. return err;
  1003. }
  1004. static void __net_exit vti6_exit_batch_net(struct list_head *net_list)
  1005. {
  1006. struct vti6_net *ip6n;
  1007. struct net *net;
  1008. LIST_HEAD(list);
  1009. rtnl_lock();
  1010. list_for_each_entry(net, net_list, exit_list) {
  1011. ip6n = net_generic(net, vti6_net_id);
  1012. vti6_destroy_tunnels(ip6n, &list);
  1013. }
  1014. unregister_netdevice_many(&list);
  1015. rtnl_unlock();
  1016. }
  1017. static struct pernet_operations vti6_net_ops = {
  1018. .init = vti6_init_net,
  1019. .exit_batch = vti6_exit_batch_net,
  1020. .id = &vti6_net_id,
  1021. .size = sizeof(struct vti6_net),
  1022. };
  1023. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  1024. .handler = vti6_rcv,
  1025. .input_handler = vti6_input_proto,
  1026. .cb_handler = vti6_rcv_cb,
  1027. .err_handler = vti6_err,
  1028. .priority = 100,
  1029. };
  1030. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  1031. .handler = vti6_rcv,
  1032. .input_handler = vti6_input_proto,
  1033. .cb_handler = vti6_rcv_cb,
  1034. .err_handler = vti6_err,
  1035. .priority = 100,
  1036. };
  1037. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  1038. .handler = vti6_rcv,
  1039. .input_handler = vti6_input_proto,
  1040. .cb_handler = vti6_rcv_cb,
  1041. .err_handler = vti6_err,
  1042. .priority = 100,
  1043. };
  1044. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1045. static int vti6_rcv_tunnel(struct sk_buff *skb)
  1046. {
  1047. const xfrm_address_t *saddr;
  1048. __be32 spi;
  1049. saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
  1050. spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
  1051. return vti6_input_proto(skb, IPPROTO_IPV6, spi, 0);
  1052. }
  1053. static struct xfrm6_tunnel vti_ipv6_handler __read_mostly = {
  1054. .handler = vti6_rcv_tunnel,
  1055. .cb_handler = vti6_rcv_cb,
  1056. .err_handler = vti6_err,
  1057. .priority = 0,
  1058. };
  1059. static struct xfrm6_tunnel vti_ip6ip_handler __read_mostly = {
  1060. .handler = vti6_rcv_tunnel,
  1061. .cb_handler = vti6_rcv_cb,
  1062. .err_handler = vti6_err,
  1063. .priority = 0,
  1064. };
  1065. #endif
  1066. /**
  1067. * vti6_tunnel_init - register protocol and reserve needed resources
  1068. *
  1069. * Return: 0 on success
  1070. **/
  1071. static int __init vti6_tunnel_init(void)
  1072. {
  1073. const char *msg;
  1074. int err;
  1075. msg = "tunnel device";
  1076. err = register_pernet_device(&vti6_net_ops);
  1077. if (err < 0)
  1078. goto pernet_dev_failed;
  1079. msg = "tunnel protocols";
  1080. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1081. if (err < 0)
  1082. goto xfrm_proto_esp_failed;
  1083. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1084. if (err < 0)
  1085. goto xfrm_proto_ah_failed;
  1086. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1087. if (err < 0)
  1088. goto xfrm_proto_comp_failed;
  1089. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1090. msg = "ipv6 tunnel";
  1091. err = xfrm6_tunnel_register(&vti_ipv6_handler, AF_INET6);
  1092. if (err < 0)
  1093. goto vti_tunnel_ipv6_failed;
  1094. err = xfrm6_tunnel_register(&vti_ip6ip_handler, AF_INET);
  1095. if (err < 0)
  1096. goto vti_tunnel_ip6ip_failed;
  1097. #endif
  1098. msg = "netlink interface";
  1099. err = rtnl_link_register(&vti6_link_ops);
  1100. if (err < 0)
  1101. goto rtnl_link_failed;
  1102. return 0;
  1103. rtnl_link_failed:
  1104. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1105. err = xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1106. vti_tunnel_ip6ip_failed:
  1107. err = xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1108. vti_tunnel_ipv6_failed:
  1109. #endif
  1110. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1111. xfrm_proto_comp_failed:
  1112. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1113. xfrm_proto_ah_failed:
  1114. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1115. xfrm_proto_esp_failed:
  1116. unregister_pernet_device(&vti6_net_ops);
  1117. pernet_dev_failed:
  1118. pr_err("vti6 init: failed to register %s\n", msg);
  1119. return err;
  1120. }
  1121. /**
  1122. * vti6_tunnel_cleanup - free resources and unregister protocol
  1123. **/
  1124. static void __exit vti6_tunnel_cleanup(void)
  1125. {
  1126. rtnl_link_unregister(&vti6_link_ops);
  1127. #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
  1128. xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
  1129. xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
  1130. #endif
  1131. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1132. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1133. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1134. unregister_pernet_device(&vti6_net_ops);
  1135. }
  1136. module_init(vti6_tunnel_init);
  1137. module_exit(vti6_tunnel_cleanup);
  1138. MODULE_LICENSE("GPL");
  1139. MODULE_ALIAS_RTNL_LINK("vti6");
  1140. MODULE_ALIAS_NETDEV("ip6_vti0");
  1141. MODULE_AUTHOR("Steffen Klassert");
  1142. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");