ip6_vti.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * IPv6 virtual tunneling interface
  3. *
  4. * Copyright (C) 2013 secunet Security Networks AG
  5. *
  6. * Author:
  7. * Steffen Klassert <steffen.klassert@secunet.com>
  8. *
  9. * Based on:
  10. * net/ipv6/ip6_tunnel.c
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/capability.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/sockios.h>
  22. #include <linux/icmp.h>
  23. #include <linux/if.h>
  24. #include <linux/in.h>
  25. #include <linux/ip.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmpv6.h>
  31. #include <linux/init.h>
  32. #include <linux/route.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/netfilter_ipv6.h>
  35. #include <linux/slab.h>
  36. #include <linux/hash.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/atomic.h>
  39. #include <net/icmp.h>
  40. #include <net/ip.h>
  41. #include <net/ip_tunnels.h>
  42. #include <net/ipv6.h>
  43. #include <net/ip6_route.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip6_tunnel.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #include <linux/etherdevice.h>
  50. #define IP6_VTI_HASH_SIZE_SHIFT 5
  51. #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
  52. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  53. {
  54. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  55. return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
  56. }
  57. static int vti6_dev_init(struct net_device *dev);
  58. static void vti6_dev_setup(struct net_device *dev);
  59. static struct rtnl_link_ops vti6_link_ops __read_mostly;
  60. static unsigned int vti6_net_id __read_mostly;
  61. struct vti6_net {
  62. /* the vti6 tunnel fallback device */
  63. struct net_device *fb_tnl_dev;
  64. /* lists for storing tunnels in use */
  65. struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
  66. struct ip6_tnl __rcu *tnls_wc[1];
  67. struct ip6_tnl __rcu **tnls[2];
  68. };
  69. #define for_each_vti6_tunnel_rcu(start) \
  70. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  71. /**
  72. * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
  73. * @net: network namespace
  74. * @remote: the address of the tunnel exit-point
  75. * @local: the address of the tunnel entry-point
  76. *
  77. * Return:
  78. * tunnel matching given end-points if found,
  79. * else fallback tunnel if its device is up,
  80. * else %NULL
  81. **/
  82. static struct ip6_tnl *
  83. vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
  84. const struct in6_addr *local)
  85. {
  86. unsigned int hash = HASH(remote, local);
  87. struct ip6_tnl *t;
  88. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  89. struct in6_addr any;
  90. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  91. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  92. ipv6_addr_equal(remote, &t->parms.raddr) &&
  93. (t->dev->flags & IFF_UP))
  94. return t;
  95. }
  96. memset(&any, 0, sizeof(any));
  97. hash = HASH(&any, local);
  98. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  99. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  100. (t->dev->flags & IFF_UP))
  101. return t;
  102. }
  103. hash = HASH(remote, &any);
  104. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  105. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  106. (t->dev->flags & IFF_UP))
  107. return t;
  108. }
  109. t = rcu_dereference(ip6n->tnls_wc[0]);
  110. if (t && (t->dev->flags & IFF_UP))
  111. return t;
  112. return NULL;
  113. }
  114. /**
  115. * vti6_tnl_bucket - get head of list matching given tunnel parameters
  116. * @p: parameters containing tunnel end-points
  117. *
  118. * Description:
  119. * vti6_tnl_bucket() returns the head of the list matching the
  120. * &struct in6_addr entries laddr and raddr in @p.
  121. *
  122. * Return: head of IPv6 tunnel list
  123. **/
  124. static struct ip6_tnl __rcu **
  125. vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
  126. {
  127. const struct in6_addr *remote = &p->raddr;
  128. const struct in6_addr *local = &p->laddr;
  129. unsigned int h = 0;
  130. int prio = 0;
  131. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  132. prio = 1;
  133. h = HASH(remote, local);
  134. }
  135. return &ip6n->tnls[prio][h];
  136. }
  137. static void
  138. vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
  139. {
  140. struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
  141. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  142. rcu_assign_pointer(*tp, t);
  143. }
  144. static void
  145. vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
  146. {
  147. struct ip6_tnl __rcu **tp;
  148. struct ip6_tnl *iter;
  149. for (tp = vti6_tnl_bucket(ip6n, &t->parms);
  150. (iter = rtnl_dereference(*tp)) != NULL;
  151. tp = &iter->next) {
  152. if (t == iter) {
  153. rcu_assign_pointer(*tp, t->next);
  154. break;
  155. }
  156. }
  157. }
  158. static void vti6_dev_free(struct net_device *dev)
  159. {
  160. free_percpu(dev->tstats);
  161. }
  162. static int vti6_tnl_create2(struct net_device *dev)
  163. {
  164. struct ip6_tnl *t = netdev_priv(dev);
  165. struct net *net = dev_net(dev);
  166. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  167. int err;
  168. dev->rtnl_link_ops = &vti6_link_ops;
  169. err = register_netdevice(dev);
  170. if (err < 0)
  171. goto out;
  172. strcpy(t->parms.name, dev->name);
  173. vti6_tnl_link(ip6n, t);
  174. return 0;
  175. out:
  176. return err;
  177. }
  178. static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  179. {
  180. struct net_device *dev;
  181. struct ip6_tnl *t;
  182. char name[IFNAMSIZ];
  183. int err;
  184. if (p->name[0]) {
  185. if (!dev_valid_name(p->name))
  186. goto failed;
  187. strlcpy(name, p->name, IFNAMSIZ);
  188. } else {
  189. sprintf(name, "ip6_vti%%d");
  190. }
  191. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
  192. if (!dev)
  193. goto failed;
  194. dev_net_set(dev, net);
  195. t = netdev_priv(dev);
  196. t->parms = *p;
  197. t->net = dev_net(dev);
  198. err = vti6_tnl_create2(dev);
  199. if (err < 0)
  200. goto failed_free;
  201. return t;
  202. failed_free:
  203. free_netdev(dev);
  204. failed:
  205. return NULL;
  206. }
  207. /**
  208. * vti6_locate - find or create tunnel matching given parameters
  209. * @net: network namespace
  210. * @p: tunnel parameters
  211. * @create: != 0 if allowed to create new tunnel if no match found
  212. *
  213. * Description:
  214. * vti6_locate() first tries to locate an existing tunnel
  215. * based on @parms. If this is unsuccessful, but @create is set a new
  216. * tunnel device is created and registered for use.
  217. *
  218. * Return:
  219. * matching tunnel or NULL
  220. **/
  221. static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
  222. int create)
  223. {
  224. const struct in6_addr *remote = &p->raddr;
  225. const struct in6_addr *local = &p->laddr;
  226. struct ip6_tnl __rcu **tp;
  227. struct ip6_tnl *t;
  228. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  229. for (tp = vti6_tnl_bucket(ip6n, p);
  230. (t = rtnl_dereference(*tp)) != NULL;
  231. tp = &t->next) {
  232. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  233. ipv6_addr_equal(remote, &t->parms.raddr)) {
  234. if (create)
  235. return NULL;
  236. return t;
  237. }
  238. }
  239. if (!create)
  240. return NULL;
  241. return vti6_tnl_create(net, p);
  242. }
  243. /**
  244. * vti6_dev_uninit - tunnel device uninitializer
  245. * @dev: the device to be destroyed
  246. *
  247. * Description:
  248. * vti6_dev_uninit() removes tunnel from its list
  249. **/
  250. static void vti6_dev_uninit(struct net_device *dev)
  251. {
  252. struct ip6_tnl *t = netdev_priv(dev);
  253. struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
  254. if (dev == ip6n->fb_tnl_dev)
  255. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  256. else
  257. vti6_tnl_unlink(ip6n, t);
  258. dev_put(dev);
  259. }
  260. static int vti6_rcv(struct sk_buff *skb)
  261. {
  262. struct ip6_tnl *t;
  263. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  264. rcu_read_lock();
  265. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  266. if (t) {
  267. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  268. rcu_read_unlock();
  269. goto discard;
  270. }
  271. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  272. rcu_read_unlock();
  273. goto discard;
  274. }
  275. ipv6h = ipv6_hdr(skb);
  276. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  277. t->dev->stats.rx_dropped++;
  278. rcu_read_unlock();
  279. goto discard;
  280. }
  281. rcu_read_unlock();
  282. return xfrm6_rcv_tnl(skb, t);
  283. }
  284. rcu_read_unlock();
  285. return -EINVAL;
  286. discard:
  287. kfree_skb(skb);
  288. return 0;
  289. }
  290. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  291. {
  292. unsigned short family;
  293. struct net_device *dev;
  294. struct pcpu_sw_netstats *tstats;
  295. struct xfrm_state *x;
  296. struct xfrm_mode *inner_mode;
  297. struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
  298. u32 orig_mark = skb->mark;
  299. int ret;
  300. if (!t)
  301. return 1;
  302. dev = t->dev;
  303. if (err) {
  304. dev->stats.rx_errors++;
  305. dev->stats.rx_dropped++;
  306. return 0;
  307. }
  308. x = xfrm_input_state(skb);
  309. inner_mode = x->inner_mode;
  310. if (x->sel.family == AF_UNSPEC) {
  311. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  312. if (inner_mode == NULL) {
  313. XFRM_INC_STATS(dev_net(skb->dev),
  314. LINUX_MIB_XFRMINSTATEMODEERROR);
  315. return -EINVAL;
  316. }
  317. }
  318. family = inner_mode->afinfo->family;
  319. skb->mark = be32_to_cpu(t->parms.i_key);
  320. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  321. skb->mark = orig_mark;
  322. if (!ret)
  323. return -EPERM;
  324. skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
  325. skb->dev = dev;
  326. tstats = this_cpu_ptr(dev->tstats);
  327. u64_stats_update_begin(&tstats->syncp);
  328. tstats->rx_packets++;
  329. tstats->rx_bytes += skb->len;
  330. u64_stats_update_end(&tstats->syncp);
  331. return 0;
  332. }
  333. /**
  334. * vti6_addr_conflict - compare packet addresses to tunnel's own
  335. * @t: the outgoing tunnel device
  336. * @hdr: IPv6 header from the incoming packet
  337. *
  338. * Description:
  339. * Avoid trivial tunneling loop by checking that tunnel exit-point
  340. * doesn't match source of incoming packet.
  341. *
  342. * Return:
  343. * 1 if conflict,
  344. * 0 else
  345. **/
  346. static inline bool
  347. vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  348. {
  349. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  350. }
  351. static bool vti6_state_check(const struct xfrm_state *x,
  352. const struct in6_addr *dst,
  353. const struct in6_addr *src)
  354. {
  355. xfrm_address_t *daddr = (xfrm_address_t *)dst;
  356. xfrm_address_t *saddr = (xfrm_address_t *)src;
  357. /* if there is no transform then this tunnel is not functional.
  358. * Or if the xfrm is not mode tunnel.
  359. */
  360. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  361. x->props.family != AF_INET6)
  362. return false;
  363. if (ipv6_addr_any(dst))
  364. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
  365. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
  366. return false;
  367. return true;
  368. }
  369. /**
  370. * vti6_xmit - send a packet
  371. * @skb: the outgoing socket buffer
  372. * @dev: the outgoing tunnel device
  373. * @fl: the flow informations for the xfrm_lookup
  374. **/
  375. static int
  376. vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
  377. {
  378. struct ip6_tnl *t = netdev_priv(dev);
  379. struct net_device_stats *stats = &t->dev->stats;
  380. struct dst_entry *dst = skb_dst(skb);
  381. struct net_device *tdev;
  382. struct xfrm_state *x;
  383. int pkt_len = skb->len;
  384. int err = -1;
  385. int mtu;
  386. if (!dst) {
  387. switch (skb->protocol) {
  388. case htons(ETH_P_IP): {
  389. struct rtable *rt;
  390. fl->u.ip4.flowi4_oif = dev->ifindex;
  391. fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
  392. rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
  393. if (IS_ERR(rt))
  394. goto tx_err_link_failure;
  395. dst = &rt->dst;
  396. skb_dst_set(skb, dst);
  397. break;
  398. }
  399. case htons(ETH_P_IPV6):
  400. fl->u.ip6.flowi6_oif = dev->ifindex;
  401. fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
  402. dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
  403. if (dst->error) {
  404. dst_release(dst);
  405. dst = NULL;
  406. goto tx_err_link_failure;
  407. }
  408. skb_dst_set(skb, dst);
  409. break;
  410. default:
  411. goto tx_err_link_failure;
  412. }
  413. }
  414. dst_hold(dst);
  415. dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
  416. if (IS_ERR(dst)) {
  417. err = PTR_ERR(dst);
  418. dst = NULL;
  419. goto tx_err_link_failure;
  420. }
  421. x = dst->xfrm;
  422. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  423. goto tx_err_link_failure;
  424. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  425. (const struct in6_addr *)&x->id.daddr))
  426. goto tx_err_link_failure;
  427. tdev = dst->dev;
  428. if (tdev == dev) {
  429. stats->collisions++;
  430. net_warn_ratelimited("%s: Local routing loop detected!\n",
  431. t->parms.name);
  432. goto tx_err_dst_release;
  433. }
  434. mtu = dst_mtu(dst);
  435. if (skb->len > mtu) {
  436. skb_dst_update_pmtu_no_confirm(skb, mtu);
  437. if (skb->protocol == htons(ETH_P_IPV6)) {
  438. if (mtu < IPV6_MIN_MTU)
  439. mtu = IPV6_MIN_MTU;
  440. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  441. } else {
  442. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  443. htonl(mtu));
  444. }
  445. err = -EMSGSIZE;
  446. goto tx_err_dst_release;
  447. }
  448. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  449. skb_dst_set(skb, dst);
  450. skb->dev = skb_dst(skb)->dev;
  451. err = dst_output(t->net, skb->sk, skb);
  452. if (net_xmit_eval(err) == 0)
  453. err = pkt_len;
  454. iptunnel_xmit_stats(dev, err);
  455. return 0;
  456. tx_err_link_failure:
  457. stats->tx_carrier_errors++;
  458. dst_link_failure(skb);
  459. tx_err_dst_release:
  460. dst_release(dst);
  461. return err;
  462. }
  463. static netdev_tx_t
  464. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  465. {
  466. struct ip6_tnl *t = netdev_priv(dev);
  467. struct net_device_stats *stats = &t->dev->stats;
  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. xfrm_decode_session(skb, &fl, AF_INET6);
  479. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  480. break;
  481. case htons(ETH_P_IP):
  482. xfrm_decode_session(skb, &fl, AF_INET);
  483. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  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. stats->tx_errors++;
  496. stats->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. memcpy(dev->dev_addr, &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. dev->mtu = clamp(dev->mtu, dev->min_mtu, dev->max_mtu);
  565. return;
  566. }
  567. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  568. int strict = (ipv6_addr_type(&p->raddr) &
  569. (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
  570. struct rt6_info *rt = rt6_lookup(t->net,
  571. &p->raddr, &p->laddr,
  572. p->link, NULL, strict);
  573. if (rt)
  574. tdev = rt->dst.dev;
  575. ip6_rt_put(rt);
  576. }
  577. if (!tdev && p->link)
  578. tdev = __dev_get_by_index(t->net, p->link);
  579. if (tdev)
  580. mtu = tdev->mtu - sizeof(struct ipv6hdr);
  581. else
  582. mtu = ETH_DATA_LEN - LL_MAX_HEADER - sizeof(struct ipv6hdr);
  583. dev->mtu = max_t(int, mtu, IPV4_MIN_MTU);
  584. }
  585. /**
  586. * vti6_tnl_change - update the tunnel parameters
  587. * @t: tunnel to be changed
  588. * @p: tunnel configuration parameters
  589. * @keep_mtu: MTU was set from userspace, don't re-compute it
  590. *
  591. * Description:
  592. * vti6_tnl_change() updates the tunnel parameters
  593. **/
  594. static int
  595. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
  596. bool keep_mtu)
  597. {
  598. t->parms.laddr = p->laddr;
  599. t->parms.raddr = p->raddr;
  600. t->parms.link = p->link;
  601. t->parms.i_key = p->i_key;
  602. t->parms.o_key = p->o_key;
  603. t->parms.proto = p->proto;
  604. t->parms.fwmark = p->fwmark;
  605. dst_cache_reset(&t->dst_cache);
  606. vti6_link_config(t, keep_mtu);
  607. return 0;
  608. }
  609. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p,
  610. bool keep_mtu)
  611. {
  612. struct net *net = dev_net(t->dev);
  613. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  614. int err;
  615. vti6_tnl_unlink(ip6n, t);
  616. synchronize_net();
  617. err = vti6_tnl_change(t, p, keep_mtu);
  618. vti6_tnl_link(ip6n, t);
  619. netdev_state_change(t->dev);
  620. return err;
  621. }
  622. static void
  623. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  624. {
  625. p->laddr = u->laddr;
  626. p->raddr = u->raddr;
  627. p->link = u->link;
  628. p->i_key = u->i_key;
  629. p->o_key = u->o_key;
  630. p->proto = u->proto;
  631. memcpy(p->name, u->name, sizeof(u->name));
  632. }
  633. static void
  634. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  635. {
  636. u->laddr = p->laddr;
  637. u->raddr = p->raddr;
  638. u->link = p->link;
  639. u->i_key = p->i_key;
  640. u->o_key = p->o_key;
  641. if (u->i_key)
  642. u->i_flags |= GRE_KEY;
  643. if (u->o_key)
  644. u->o_flags |= GRE_KEY;
  645. u->proto = p->proto;
  646. memcpy(u->name, p->name, sizeof(u->name));
  647. }
  648. /**
  649. * vti6_ioctl - configure vti6 tunnels from userspace
  650. * @dev: virtual device associated with tunnel
  651. * @ifr: parameters passed from userspace
  652. * @cmd: command to be performed
  653. *
  654. * Description:
  655. * vti6_ioctl() is used for managing vti6 tunnels
  656. * from userspace.
  657. *
  658. * The possible commands are the following:
  659. * %SIOCGETTUNNEL: get tunnel parameters for device
  660. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  661. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  662. * %SIOCDELTUNNEL: delete tunnel
  663. *
  664. * The fallback device "ip6_vti0", created during module
  665. * initialization, can be used for creating other tunnel devices.
  666. *
  667. * Return:
  668. * 0 on success,
  669. * %-EFAULT if unable to copy data to or from userspace,
  670. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  671. * %-EINVAL if passed tunnel parameters are invalid,
  672. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  673. * %-ENODEV if attempting to change or delete a nonexisting device
  674. **/
  675. static int
  676. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  677. {
  678. int err = 0;
  679. struct ip6_tnl_parm2 p;
  680. struct __ip6_tnl_parm p1;
  681. struct ip6_tnl *t = NULL;
  682. struct net *net = dev_net(dev);
  683. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  684. switch (cmd) {
  685. case SIOCGETTUNNEL:
  686. if (dev == ip6n->fb_tnl_dev) {
  687. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  688. err = -EFAULT;
  689. break;
  690. }
  691. vti6_parm_from_user(&p1, &p);
  692. t = vti6_locate(net, &p1, 0);
  693. } else {
  694. memset(&p, 0, sizeof(p));
  695. }
  696. if (!t)
  697. t = netdev_priv(dev);
  698. vti6_parm_to_user(&p, &t->parms);
  699. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  700. err = -EFAULT;
  701. break;
  702. case SIOCADDTUNNEL:
  703. case SIOCCHGTUNNEL:
  704. err = -EPERM;
  705. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  706. break;
  707. err = -EFAULT;
  708. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  709. break;
  710. err = -EINVAL;
  711. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  712. break;
  713. vti6_parm_from_user(&p1, &p);
  714. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  715. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  716. if (t) {
  717. if (t->dev != dev) {
  718. err = -EEXIST;
  719. break;
  720. }
  721. } else
  722. t = netdev_priv(dev);
  723. err = vti6_update(t, &p1, false);
  724. }
  725. if (t) {
  726. err = 0;
  727. vti6_parm_to_user(&p, &t->parms);
  728. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  729. err = -EFAULT;
  730. } else
  731. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  732. break;
  733. case SIOCDELTUNNEL:
  734. err = -EPERM;
  735. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  736. break;
  737. if (dev == ip6n->fb_tnl_dev) {
  738. err = -EFAULT;
  739. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  740. break;
  741. err = -ENOENT;
  742. vti6_parm_from_user(&p1, &p);
  743. t = vti6_locate(net, &p1, 0);
  744. if (!t)
  745. break;
  746. err = -EPERM;
  747. if (t->dev == ip6n->fb_tnl_dev)
  748. break;
  749. dev = t->dev;
  750. }
  751. err = 0;
  752. unregister_netdevice(dev);
  753. break;
  754. default:
  755. err = -EINVAL;
  756. }
  757. return err;
  758. }
  759. static const struct net_device_ops vti6_netdev_ops = {
  760. .ndo_init = vti6_dev_init,
  761. .ndo_uninit = vti6_dev_uninit,
  762. .ndo_start_xmit = vti6_tnl_xmit,
  763. .ndo_do_ioctl = vti6_ioctl,
  764. .ndo_get_stats64 = ip_tunnel_get_stats64,
  765. .ndo_get_iflink = ip6_tnl_get_iflink,
  766. };
  767. /**
  768. * vti6_dev_setup - setup virtual tunnel device
  769. * @dev: virtual device associated with tunnel
  770. *
  771. * Description:
  772. * Initialize function pointers and device parameters
  773. **/
  774. static void vti6_dev_setup(struct net_device *dev)
  775. {
  776. dev->netdev_ops = &vti6_netdev_ops;
  777. dev->needs_free_netdev = true;
  778. dev->priv_destructor = vti6_dev_free;
  779. dev->type = ARPHRD_TUNNEL6;
  780. dev->min_mtu = IPV4_MIN_MTU;
  781. dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
  782. dev->flags |= IFF_NOARP;
  783. dev->addr_len = sizeof(struct in6_addr);
  784. netif_keep_dst(dev);
  785. /* This perm addr will be used as interface identifier by IPv6 */
  786. dev->addr_assign_type = NET_ADDR_RANDOM;
  787. eth_random_addr(dev->perm_addr);
  788. }
  789. /**
  790. * vti6_dev_init_gen - general initializer for all tunnel devices
  791. * @dev: virtual device associated with tunnel
  792. **/
  793. static inline int vti6_dev_init_gen(struct net_device *dev)
  794. {
  795. struct ip6_tnl *t = netdev_priv(dev);
  796. t->dev = dev;
  797. t->net = dev_net(dev);
  798. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  799. if (!dev->tstats)
  800. return -ENOMEM;
  801. dev_hold(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->parms.proto = IPPROTO_IPV6;
  829. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  830. return 0;
  831. }
  832. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[],
  833. struct netlink_ext_ack *extack)
  834. {
  835. return 0;
  836. }
  837. static void vti6_netlink_parms(struct nlattr *data[],
  838. struct __ip6_tnl_parm *parms)
  839. {
  840. memset(parms, 0, sizeof(*parms));
  841. if (!data)
  842. return;
  843. if (data[IFLA_VTI_LINK])
  844. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  845. if (data[IFLA_VTI_LOCAL])
  846. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  847. if (data[IFLA_VTI_REMOTE])
  848. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  849. if (data[IFLA_VTI_IKEY])
  850. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  851. if (data[IFLA_VTI_OKEY])
  852. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  853. if (data[IFLA_VTI_FWMARK])
  854. parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
  855. }
  856. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  857. struct nlattr *tb[], struct nlattr *data[],
  858. struct netlink_ext_ack *extack)
  859. {
  860. struct net *net = dev_net(dev);
  861. struct ip6_tnl *nt;
  862. nt = netdev_priv(dev);
  863. vti6_netlink_parms(data, &nt->parms);
  864. nt->parms.proto = IPPROTO_IPV6;
  865. if (vti6_locate(net, &nt->parms, 0))
  866. return -EEXIST;
  867. return vti6_tnl_create2(dev);
  868. }
  869. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  870. {
  871. struct net *net = dev_net(dev);
  872. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  873. if (dev != ip6n->fb_tnl_dev)
  874. unregister_netdevice_queue(dev, head);
  875. }
  876. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  877. struct nlattr *data[],
  878. struct netlink_ext_ack *extack)
  879. {
  880. struct ip6_tnl *t;
  881. struct __ip6_tnl_parm p;
  882. struct net *net = dev_net(dev);
  883. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  884. if (dev == ip6n->fb_tnl_dev)
  885. return -EINVAL;
  886. vti6_netlink_parms(data, &p);
  887. t = vti6_locate(net, &p, 0);
  888. if (t) {
  889. if (t->dev != dev)
  890. return -EEXIST;
  891. } else
  892. t = netdev_priv(dev);
  893. return vti6_update(t, &p, tb && tb[IFLA_MTU]);
  894. }
  895. static size_t vti6_get_size(const struct net_device *dev)
  896. {
  897. return
  898. /* IFLA_VTI_LINK */
  899. nla_total_size(4) +
  900. /* IFLA_VTI_LOCAL */
  901. nla_total_size(sizeof(struct in6_addr)) +
  902. /* IFLA_VTI_REMOTE */
  903. nla_total_size(sizeof(struct in6_addr)) +
  904. /* IFLA_VTI_IKEY */
  905. nla_total_size(4) +
  906. /* IFLA_VTI_OKEY */
  907. nla_total_size(4) +
  908. /* IFLA_VTI_FWMARK */
  909. nla_total_size(4) +
  910. 0;
  911. }
  912. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  913. {
  914. struct ip6_tnl *tunnel = netdev_priv(dev);
  915. struct __ip6_tnl_parm *parm = &tunnel->parms;
  916. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  917. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  918. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  919. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  920. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
  921. nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
  922. goto nla_put_failure;
  923. return 0;
  924. nla_put_failure:
  925. return -EMSGSIZE;
  926. }
  927. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  928. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  929. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  930. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  931. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  932. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  933. [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
  934. };
  935. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  936. .kind = "vti6",
  937. .maxtype = IFLA_VTI_MAX,
  938. .policy = vti6_policy,
  939. .priv_size = sizeof(struct ip6_tnl),
  940. .setup = vti6_dev_setup,
  941. .validate = vti6_validate,
  942. .newlink = vti6_newlink,
  943. .dellink = vti6_dellink,
  944. .changelink = vti6_changelink,
  945. .get_size = vti6_get_size,
  946. .fill_info = vti6_fill_info,
  947. .get_link_net = ip6_tnl_get_link_net,
  948. };
  949. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n,
  950. struct list_head *list)
  951. {
  952. int h;
  953. struct ip6_tnl *t;
  954. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  955. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  956. while (t) {
  957. unregister_netdevice_queue(t->dev, list);
  958. t = rtnl_dereference(t->next);
  959. }
  960. }
  961. t = rtnl_dereference(ip6n->tnls_wc[0]);
  962. if (t)
  963. unregister_netdevice_queue(t->dev, list);
  964. }
  965. static int __net_init vti6_init_net(struct net *net)
  966. {
  967. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  968. struct ip6_tnl *t = NULL;
  969. int err;
  970. ip6n->tnls[0] = ip6n->tnls_wc;
  971. ip6n->tnls[1] = ip6n->tnls_r_l;
  972. if (!net_has_fallback_tunnels(net))
  973. return 0;
  974. err = -ENOMEM;
  975. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  976. NET_NAME_UNKNOWN, vti6_dev_setup);
  977. if (!ip6n->fb_tnl_dev)
  978. goto err_alloc_dev;
  979. dev_net_set(ip6n->fb_tnl_dev, net);
  980. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  981. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  982. if (err < 0)
  983. goto err_register;
  984. err = register_netdev(ip6n->fb_tnl_dev);
  985. if (err < 0)
  986. goto err_register;
  987. t = netdev_priv(ip6n->fb_tnl_dev);
  988. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  989. return 0;
  990. err_register:
  991. free_netdev(ip6n->fb_tnl_dev);
  992. err_alloc_dev:
  993. return err;
  994. }
  995. static void __net_exit vti6_exit_batch_net(struct list_head *net_list)
  996. {
  997. struct vti6_net *ip6n;
  998. struct net *net;
  999. LIST_HEAD(list);
  1000. rtnl_lock();
  1001. list_for_each_entry(net, net_list, exit_list) {
  1002. ip6n = net_generic(net, vti6_net_id);
  1003. vti6_destroy_tunnels(ip6n, &list);
  1004. }
  1005. unregister_netdevice_many(&list);
  1006. rtnl_unlock();
  1007. }
  1008. static struct pernet_operations vti6_net_ops = {
  1009. .init = vti6_init_net,
  1010. .exit_batch = vti6_exit_batch_net,
  1011. .id = &vti6_net_id,
  1012. .size = sizeof(struct vti6_net),
  1013. };
  1014. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  1015. .handler = vti6_rcv,
  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. .cb_handler = vti6_rcv_cb,
  1023. .err_handler = vti6_err,
  1024. .priority = 100,
  1025. };
  1026. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  1027. .handler = vti6_rcv,
  1028. .cb_handler = vti6_rcv_cb,
  1029. .err_handler = vti6_err,
  1030. .priority = 100,
  1031. };
  1032. /**
  1033. * vti6_tunnel_init - register protocol and reserve needed resources
  1034. *
  1035. * Return: 0 on success
  1036. **/
  1037. static int __init vti6_tunnel_init(void)
  1038. {
  1039. const char *msg;
  1040. int err;
  1041. msg = "tunnel device";
  1042. err = register_pernet_device(&vti6_net_ops);
  1043. if (err < 0)
  1044. goto pernet_dev_failed;
  1045. msg = "tunnel protocols";
  1046. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1047. if (err < 0)
  1048. goto xfrm_proto_esp_failed;
  1049. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1050. if (err < 0)
  1051. goto xfrm_proto_ah_failed;
  1052. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1053. if (err < 0)
  1054. goto xfrm_proto_comp_failed;
  1055. msg = "netlink interface";
  1056. err = rtnl_link_register(&vti6_link_ops);
  1057. if (err < 0)
  1058. goto rtnl_link_failed;
  1059. return 0;
  1060. rtnl_link_failed:
  1061. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1062. xfrm_proto_comp_failed:
  1063. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1064. xfrm_proto_ah_failed:
  1065. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1066. xfrm_proto_esp_failed:
  1067. unregister_pernet_device(&vti6_net_ops);
  1068. pernet_dev_failed:
  1069. pr_err("vti6 init: failed to register %s\n", msg);
  1070. return err;
  1071. }
  1072. /**
  1073. * vti6_tunnel_cleanup - free resources and unregister protocol
  1074. **/
  1075. static void __exit vti6_tunnel_cleanup(void)
  1076. {
  1077. rtnl_link_unregister(&vti6_link_ops);
  1078. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1079. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1080. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1081. unregister_pernet_device(&vti6_net_ops);
  1082. }
  1083. module_init(vti6_tunnel_init);
  1084. module_exit(vti6_tunnel_cleanup);
  1085. MODULE_LICENSE("GPL");
  1086. MODULE_ALIAS_RTNL_LINK("vti6");
  1087. MODULE_ALIAS_NETDEV("ip6_vti0");
  1088. MODULE_AUTHOR("Steffen Klassert");
  1089. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");