ip6_tunnel.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. /*
  2. * IPv6 tunneling device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
  8. *
  9. * Based on:
  10. * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
  11. *
  12. * RFC 2473
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/capability.h>
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/sockios.h>
  26. #include <linux/icmp.h>
  27. #include <linux/if.h>
  28. #include <linux/in.h>
  29. #include <linux/ip.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <linux/slab.h>
  40. #include <linux/hash.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/atomic.h>
  44. #include <net/icmp.h>
  45. #include <net/ip.h>
  46. #include <net/ip_tunnels.h>
  47. #include <net/ipv6.h>
  48. #include <net/ip6_route.h>
  49. #include <net/addrconf.h>
  50. #include <net/ip6_tunnel.h>
  51. #include <net/xfrm.h>
  52. #include <net/dsfield.h>
  53. #include <net/inet_ecn.h>
  54. #include <net/net_namespace.h>
  55. #include <net/netns/generic.h>
  56. #include <net/dst_metadata.h>
  57. MODULE_AUTHOR("Ville Nuorvala");
  58. MODULE_DESCRIPTION("IPv6 tunneling device");
  59. MODULE_LICENSE("GPL");
  60. MODULE_ALIAS_RTNL_LINK("ip6tnl");
  61. MODULE_ALIAS_NETDEV("ip6tnl0");
  62. #define IP6_TUNNEL_HASH_SIZE_SHIFT 5
  63. #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
  64. static bool log_ecn_error = true;
  65. module_param(log_ecn_error, bool, 0644);
  66. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  67. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  68. {
  69. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  70. return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
  71. }
  72. static int ip6_tnl_dev_init(struct net_device *dev);
  73. static void ip6_tnl_dev_setup(struct net_device *dev);
  74. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  75. static unsigned int ip6_tnl_net_id __read_mostly;
  76. struct ip6_tnl_net {
  77. /* the IPv6 tunnel fallback device */
  78. struct net_device *fb_tnl_dev;
  79. /* lists for storing tunnels in use */
  80. struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
  81. struct ip6_tnl __rcu *tnls_wc[1];
  82. struct ip6_tnl __rcu **tnls[2];
  83. struct ip6_tnl __rcu *collect_md_tun;
  84. };
  85. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  86. {
  87. struct pcpu_sw_netstats tmp, sum = { 0 };
  88. int i;
  89. for_each_possible_cpu(i) {
  90. unsigned int start;
  91. const struct pcpu_sw_netstats *tstats =
  92. per_cpu_ptr(dev->tstats, i);
  93. do {
  94. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  95. tmp.rx_packets = tstats->rx_packets;
  96. tmp.rx_bytes = tstats->rx_bytes;
  97. tmp.tx_packets = tstats->tx_packets;
  98. tmp.tx_bytes = tstats->tx_bytes;
  99. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  100. sum.rx_packets += tmp.rx_packets;
  101. sum.rx_bytes += tmp.rx_bytes;
  102. sum.tx_packets += tmp.tx_packets;
  103. sum.tx_bytes += tmp.tx_bytes;
  104. }
  105. dev->stats.rx_packets = sum.rx_packets;
  106. dev->stats.rx_bytes = sum.rx_bytes;
  107. dev->stats.tx_packets = sum.tx_packets;
  108. dev->stats.tx_bytes = sum.tx_bytes;
  109. return &dev->stats;
  110. }
  111. /**
  112. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  113. * @remote: the address of the tunnel exit-point
  114. * @local: the address of the tunnel entry-point
  115. *
  116. * Return:
  117. * tunnel matching given end-points if found,
  118. * else fallback tunnel if its device is up,
  119. * else %NULL
  120. **/
  121. #define for_each_ip6_tunnel_rcu(start) \
  122. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  123. static struct ip6_tnl *
  124. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  125. {
  126. unsigned int hash = HASH(remote, local);
  127. struct ip6_tnl *t;
  128. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  129. struct in6_addr any;
  130. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  131. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  132. ipv6_addr_equal(remote, &t->parms.raddr) &&
  133. (t->dev->flags & IFF_UP))
  134. return t;
  135. }
  136. memset(&any, 0, sizeof(any));
  137. hash = HASH(&any, local);
  138. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  139. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  140. ipv6_addr_any(&t->parms.raddr) &&
  141. (t->dev->flags & IFF_UP))
  142. return t;
  143. }
  144. hash = HASH(remote, &any);
  145. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  146. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  147. ipv6_addr_any(&t->parms.laddr) &&
  148. (t->dev->flags & IFF_UP))
  149. return t;
  150. }
  151. t = rcu_dereference(ip6n->collect_md_tun);
  152. if (t && t->dev->flags & IFF_UP)
  153. return t;
  154. t = rcu_dereference(ip6n->tnls_wc[0]);
  155. if (t && (t->dev->flags & IFF_UP))
  156. return t;
  157. return NULL;
  158. }
  159. /**
  160. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  161. * @p: parameters containing tunnel end-points
  162. *
  163. * Description:
  164. * ip6_tnl_bucket() returns the head of the list matching the
  165. * &struct in6_addr entries laddr and raddr in @p.
  166. *
  167. * Return: head of IPv6 tunnel list
  168. **/
  169. static struct ip6_tnl __rcu **
  170. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  171. {
  172. const struct in6_addr *remote = &p->raddr;
  173. const struct in6_addr *local = &p->laddr;
  174. unsigned int h = 0;
  175. int prio = 0;
  176. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  177. prio = 1;
  178. h = HASH(remote, local);
  179. }
  180. return &ip6n->tnls[prio][h];
  181. }
  182. /**
  183. * ip6_tnl_link - add tunnel to hash table
  184. * @t: tunnel to be added
  185. **/
  186. static void
  187. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  188. {
  189. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  190. if (t->parms.collect_md)
  191. rcu_assign_pointer(ip6n->collect_md_tun, t);
  192. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  193. rcu_assign_pointer(*tp, t);
  194. }
  195. /**
  196. * ip6_tnl_unlink - remove tunnel from hash table
  197. * @t: tunnel to be removed
  198. **/
  199. static void
  200. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  201. {
  202. struct ip6_tnl __rcu **tp;
  203. struct ip6_tnl *iter;
  204. if (t->parms.collect_md)
  205. rcu_assign_pointer(ip6n->collect_md_tun, NULL);
  206. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  207. (iter = rtnl_dereference(*tp)) != NULL;
  208. tp = &iter->next) {
  209. if (t == iter) {
  210. rcu_assign_pointer(*tp, t->next);
  211. break;
  212. }
  213. }
  214. }
  215. static void ip6_dev_free(struct net_device *dev)
  216. {
  217. struct ip6_tnl *t = netdev_priv(dev);
  218. gro_cells_destroy(&t->gro_cells);
  219. dst_cache_destroy(&t->dst_cache);
  220. free_percpu(dev->tstats);
  221. }
  222. static int ip6_tnl_create2(struct net_device *dev)
  223. {
  224. struct ip6_tnl *t = netdev_priv(dev);
  225. struct net *net = dev_net(dev);
  226. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  227. int err;
  228. t = netdev_priv(dev);
  229. dev->rtnl_link_ops = &ip6_link_ops;
  230. err = register_netdevice(dev);
  231. if (err < 0)
  232. goto out;
  233. strcpy(t->parms.name, dev->name);
  234. dev_hold(dev);
  235. ip6_tnl_link(ip6n, t);
  236. return 0;
  237. out:
  238. return err;
  239. }
  240. /**
  241. * ip6_tnl_create - create a new tunnel
  242. * @p: tunnel parameters
  243. * @pt: pointer to new tunnel
  244. *
  245. * Description:
  246. * Create tunnel matching given parameters.
  247. *
  248. * Return:
  249. * created tunnel or error pointer
  250. **/
  251. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  252. {
  253. struct net_device *dev;
  254. struct ip6_tnl *t;
  255. char name[IFNAMSIZ];
  256. int err = -ENOMEM;
  257. if (p->name[0])
  258. strlcpy(name, p->name, IFNAMSIZ);
  259. else
  260. sprintf(name, "ip6tnl%%d");
  261. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  262. ip6_tnl_dev_setup);
  263. if (!dev)
  264. goto failed;
  265. dev_net_set(dev, net);
  266. t = netdev_priv(dev);
  267. t->parms = *p;
  268. t->net = dev_net(dev);
  269. err = ip6_tnl_create2(dev);
  270. if (err < 0)
  271. goto failed_free;
  272. return t;
  273. failed_free:
  274. free_netdev(dev);
  275. failed:
  276. return ERR_PTR(err);
  277. }
  278. /**
  279. * ip6_tnl_locate - find or create tunnel matching given parameters
  280. * @p: tunnel parameters
  281. * @create: != 0 if allowed to create new tunnel if no match found
  282. *
  283. * Description:
  284. * ip6_tnl_locate() first tries to locate an existing tunnel
  285. * based on @parms. If this is unsuccessful, but @create is set a new
  286. * tunnel device is created and registered for use.
  287. *
  288. * Return:
  289. * matching tunnel or error pointer
  290. **/
  291. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  292. struct __ip6_tnl_parm *p, int create)
  293. {
  294. const struct in6_addr *remote = &p->raddr;
  295. const struct in6_addr *local = &p->laddr;
  296. struct ip6_tnl __rcu **tp;
  297. struct ip6_tnl *t;
  298. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  299. for (tp = ip6_tnl_bucket(ip6n, p);
  300. (t = rtnl_dereference(*tp)) != NULL;
  301. tp = &t->next) {
  302. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  303. ipv6_addr_equal(remote, &t->parms.raddr)) {
  304. if (create)
  305. return ERR_PTR(-EEXIST);
  306. return t;
  307. }
  308. }
  309. if (!create)
  310. return ERR_PTR(-ENODEV);
  311. return ip6_tnl_create(net, p);
  312. }
  313. /**
  314. * ip6_tnl_dev_uninit - tunnel device uninitializer
  315. * @dev: the device to be destroyed
  316. *
  317. * Description:
  318. * ip6_tnl_dev_uninit() removes tunnel from its list
  319. **/
  320. static void
  321. ip6_tnl_dev_uninit(struct net_device *dev)
  322. {
  323. struct ip6_tnl *t = netdev_priv(dev);
  324. struct net *net = t->net;
  325. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  326. if (dev == ip6n->fb_tnl_dev)
  327. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  328. else
  329. ip6_tnl_unlink(ip6n, t);
  330. dst_cache_reset(&t->dst_cache);
  331. dev_put(dev);
  332. }
  333. /**
  334. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  335. * @skb: received socket buffer
  336. *
  337. * Return:
  338. * 0 if none was found,
  339. * else index to encapsulation limit
  340. **/
  341. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  342. {
  343. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
  344. unsigned int nhoff = raw - skb->data;
  345. unsigned int off = nhoff + sizeof(*ipv6h);
  346. u8 next, nexthdr = ipv6h->nexthdr;
  347. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  348. struct ipv6_opt_hdr *hdr;
  349. u16 optlen;
  350. if (!pskb_may_pull(skb, off + sizeof(*hdr)))
  351. break;
  352. hdr = (struct ipv6_opt_hdr *)(skb->data + off);
  353. if (nexthdr == NEXTHDR_FRAGMENT) {
  354. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  355. if (frag_hdr->frag_off)
  356. break;
  357. optlen = 8;
  358. } else if (nexthdr == NEXTHDR_AUTH) {
  359. optlen = (hdr->hdrlen + 2) << 2;
  360. } else {
  361. optlen = ipv6_optlen(hdr);
  362. }
  363. /* cache hdr->nexthdr, since pskb_may_pull() might
  364. * invalidate hdr
  365. */
  366. next = hdr->nexthdr;
  367. if (nexthdr == NEXTHDR_DEST) {
  368. u16 i = 2;
  369. /* Remember : hdr is no longer valid at this point. */
  370. if (!pskb_may_pull(skb, off + optlen))
  371. break;
  372. while (1) {
  373. struct ipv6_tlv_tnl_enc_lim *tel;
  374. /* No more room for encapsulation limit */
  375. if (i + sizeof(*tel) > optlen)
  376. break;
  377. tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
  378. /* return index of option if found and valid */
  379. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  380. tel->length == 1)
  381. return i + off - nhoff;
  382. /* else jump to next option */
  383. if (tel->type)
  384. i += tel->length + 2;
  385. else
  386. i++;
  387. }
  388. }
  389. nexthdr = next;
  390. off += optlen;
  391. }
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  395. /**
  396. * ip6_tnl_err - tunnel error handler
  397. *
  398. * Description:
  399. * ip6_tnl_err() should handle errors in the tunnel according
  400. * to the specifications in RFC 2473.
  401. **/
  402. static int
  403. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  404. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  405. {
  406. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
  407. struct ip6_tnl *t;
  408. int rel_msg = 0;
  409. u8 rel_type = ICMPV6_DEST_UNREACH;
  410. u8 rel_code = ICMPV6_ADDR_UNREACH;
  411. u8 tproto;
  412. __u32 rel_info = 0;
  413. __u16 len;
  414. int err = -ENOENT;
  415. /* If the packet doesn't contain the original IPv6 header we are
  416. in trouble since we might need the source address for further
  417. processing of the error. */
  418. rcu_read_lock();
  419. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
  420. if (!t)
  421. goto out;
  422. tproto = ACCESS_ONCE(t->parms.proto);
  423. if (tproto != ipproto && tproto != 0)
  424. goto out;
  425. err = 0;
  426. switch (*type) {
  427. __u32 teli;
  428. struct ipv6_tlv_tnl_enc_lim *tel;
  429. __u32 mtu;
  430. case ICMPV6_DEST_UNREACH:
  431. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  432. t->parms.name);
  433. rel_msg = 1;
  434. break;
  435. case ICMPV6_TIME_EXCEED:
  436. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  437. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  438. t->parms.name);
  439. rel_msg = 1;
  440. }
  441. break;
  442. case ICMPV6_PARAMPROB:
  443. teli = 0;
  444. if ((*code) == ICMPV6_HDR_FIELD)
  445. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  446. if (teli && teli == *info - 2) {
  447. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  448. if (tel->encap_limit == 0) {
  449. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  450. t->parms.name);
  451. rel_msg = 1;
  452. }
  453. } else {
  454. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  455. t->parms.name);
  456. }
  457. break;
  458. case ICMPV6_PKT_TOOBIG:
  459. mtu = *info - offset;
  460. if (mtu < IPV6_MIN_MTU)
  461. mtu = IPV6_MIN_MTU;
  462. t->dev->mtu = mtu;
  463. len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
  464. if (len > mtu) {
  465. rel_type = ICMPV6_PKT_TOOBIG;
  466. rel_code = 0;
  467. rel_info = mtu;
  468. rel_msg = 1;
  469. }
  470. break;
  471. }
  472. *type = rel_type;
  473. *code = rel_code;
  474. *info = rel_info;
  475. *msg = rel_msg;
  476. out:
  477. rcu_read_unlock();
  478. return err;
  479. }
  480. static int
  481. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  482. u8 type, u8 code, int offset, __be32 info)
  483. {
  484. int rel_msg = 0;
  485. u8 rel_type = type;
  486. u8 rel_code = code;
  487. __u32 rel_info = ntohl(info);
  488. int err;
  489. struct sk_buff *skb2;
  490. const struct iphdr *eiph;
  491. struct rtable *rt;
  492. struct flowi4 fl4;
  493. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  494. &rel_msg, &rel_info, offset);
  495. if (err < 0)
  496. return err;
  497. if (rel_msg == 0)
  498. return 0;
  499. switch (rel_type) {
  500. case ICMPV6_DEST_UNREACH:
  501. if (rel_code != ICMPV6_ADDR_UNREACH)
  502. return 0;
  503. rel_type = ICMP_DEST_UNREACH;
  504. rel_code = ICMP_HOST_UNREACH;
  505. break;
  506. case ICMPV6_PKT_TOOBIG:
  507. if (rel_code != 0)
  508. return 0;
  509. rel_type = ICMP_DEST_UNREACH;
  510. rel_code = ICMP_FRAG_NEEDED;
  511. break;
  512. case NDISC_REDIRECT:
  513. rel_type = ICMP_REDIRECT;
  514. rel_code = ICMP_REDIR_HOST;
  515. default:
  516. return 0;
  517. }
  518. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  519. return 0;
  520. skb2 = skb_clone(skb, GFP_ATOMIC);
  521. if (!skb2)
  522. return 0;
  523. skb_dst_drop(skb2);
  524. skb_pull(skb2, offset);
  525. skb_reset_network_header(skb2);
  526. eiph = ip_hdr(skb2);
  527. /* Try to guess incoming interface */
  528. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  529. eiph->saddr, 0,
  530. 0, 0,
  531. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  532. if (IS_ERR(rt))
  533. goto out;
  534. skb2->dev = rt->dst.dev;
  535. /* route "incoming" packet */
  536. if (rt->rt_flags & RTCF_LOCAL) {
  537. ip_rt_put(rt);
  538. rt = NULL;
  539. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  540. eiph->daddr, eiph->saddr,
  541. 0, 0,
  542. IPPROTO_IPIP,
  543. RT_TOS(eiph->tos), 0);
  544. if (IS_ERR(rt) ||
  545. rt->dst.dev->type != ARPHRD_TUNNEL) {
  546. if (!IS_ERR(rt))
  547. ip_rt_put(rt);
  548. goto out;
  549. }
  550. skb_dst_set(skb2, &rt->dst);
  551. } else {
  552. ip_rt_put(rt);
  553. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  554. skb2->dev) ||
  555. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  556. goto out;
  557. }
  558. /* change mtu on this route */
  559. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  560. if (rel_info > dst_mtu(skb_dst(skb2)))
  561. goto out;
  562. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
  563. }
  564. if (rel_type == ICMP_REDIRECT)
  565. skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
  566. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  567. out:
  568. kfree_skb(skb2);
  569. return 0;
  570. }
  571. static int
  572. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  573. u8 type, u8 code, int offset, __be32 info)
  574. {
  575. int rel_msg = 0;
  576. u8 rel_type = type;
  577. u8 rel_code = code;
  578. __u32 rel_info = ntohl(info);
  579. int err;
  580. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  581. &rel_msg, &rel_info, offset);
  582. if (err < 0)
  583. return err;
  584. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  585. struct rt6_info *rt;
  586. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  587. if (!skb2)
  588. return 0;
  589. skb_dst_drop(skb2);
  590. skb_pull(skb2, offset);
  591. skb_reset_network_header(skb2);
  592. /* Try to guess incoming interface */
  593. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  594. NULL, 0, 0);
  595. if (rt && rt->dst.dev)
  596. skb2->dev = rt->dst.dev;
  597. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  598. ip6_rt_put(rt);
  599. kfree_skb(skb2);
  600. }
  601. return 0;
  602. }
  603. static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  604. const struct ipv6hdr *ipv6h,
  605. struct sk_buff *skb)
  606. {
  607. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  608. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  609. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  610. return IP6_ECN_decapsulate(ipv6h, skb);
  611. }
  612. static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  613. const struct ipv6hdr *ipv6h,
  614. struct sk_buff *skb)
  615. {
  616. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  617. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  618. return IP6_ECN_decapsulate(ipv6h, skb);
  619. }
  620. __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
  621. const struct in6_addr *laddr,
  622. const struct in6_addr *raddr)
  623. {
  624. struct __ip6_tnl_parm *p = &t->parms;
  625. int ltype = ipv6_addr_type(laddr);
  626. int rtype = ipv6_addr_type(raddr);
  627. __u32 flags = 0;
  628. if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
  629. flags = IP6_TNL_F_CAP_PER_PACKET;
  630. } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  631. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  632. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  633. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  634. if (ltype&IPV6_ADDR_UNICAST)
  635. flags |= IP6_TNL_F_CAP_XMIT;
  636. if (rtype&IPV6_ADDR_UNICAST)
  637. flags |= IP6_TNL_F_CAP_RCV;
  638. }
  639. return flags;
  640. }
  641. EXPORT_SYMBOL(ip6_tnl_get_cap);
  642. /* called with rcu_read_lock() */
  643. int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
  644. const struct in6_addr *laddr,
  645. const struct in6_addr *raddr)
  646. {
  647. struct __ip6_tnl_parm *p = &t->parms;
  648. int ret = 0;
  649. struct net *net = t->net;
  650. if ((p->flags & IP6_TNL_F_CAP_RCV) ||
  651. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  652. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
  653. struct net_device *ldev = NULL;
  654. if (p->link)
  655. ldev = dev_get_by_index_rcu(net, p->link);
  656. if ((ipv6_addr_is_multicast(laddr) ||
  657. likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
  658. likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
  659. ret = 1;
  660. }
  661. return ret;
  662. }
  663. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  664. static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
  665. const struct tnl_ptk_info *tpi,
  666. struct metadata_dst *tun_dst,
  667. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  668. const struct ipv6hdr *ipv6h,
  669. struct sk_buff *skb),
  670. bool log_ecn_err)
  671. {
  672. struct pcpu_sw_netstats *tstats;
  673. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  674. int err;
  675. if ((!(tpi->flags & TUNNEL_CSUM) &&
  676. (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
  677. ((tpi->flags & TUNNEL_CSUM) &&
  678. !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
  679. tunnel->dev->stats.rx_crc_errors++;
  680. tunnel->dev->stats.rx_errors++;
  681. goto drop;
  682. }
  683. if (tunnel->parms.i_flags & TUNNEL_SEQ) {
  684. if (!(tpi->flags & TUNNEL_SEQ) ||
  685. (tunnel->i_seqno &&
  686. (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
  687. tunnel->dev->stats.rx_fifo_errors++;
  688. tunnel->dev->stats.rx_errors++;
  689. goto drop;
  690. }
  691. tunnel->i_seqno = ntohl(tpi->seq) + 1;
  692. }
  693. skb->protocol = tpi->proto;
  694. /* Warning: All skb pointers will be invalidated! */
  695. if (tunnel->dev->type == ARPHRD_ETHER) {
  696. if (!pskb_may_pull(skb, ETH_HLEN)) {
  697. tunnel->dev->stats.rx_length_errors++;
  698. tunnel->dev->stats.rx_errors++;
  699. goto drop;
  700. }
  701. ipv6h = ipv6_hdr(skb);
  702. skb->protocol = eth_type_trans(skb, tunnel->dev);
  703. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  704. } else {
  705. skb->dev = tunnel->dev;
  706. }
  707. skb_reset_network_header(skb);
  708. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  709. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  710. err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
  711. if (unlikely(err)) {
  712. if (log_ecn_err)
  713. net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
  714. &ipv6h->saddr,
  715. ipv6_get_dsfield(ipv6h));
  716. if (err > 1) {
  717. ++tunnel->dev->stats.rx_frame_errors;
  718. ++tunnel->dev->stats.rx_errors;
  719. goto drop;
  720. }
  721. }
  722. tstats = this_cpu_ptr(tunnel->dev->tstats);
  723. u64_stats_update_begin(&tstats->syncp);
  724. tstats->rx_packets++;
  725. tstats->rx_bytes += skb->len;
  726. u64_stats_update_end(&tstats->syncp);
  727. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
  728. if (tun_dst)
  729. skb_dst_set(skb, (struct dst_entry *)tun_dst);
  730. gro_cells_receive(&tunnel->gro_cells, skb);
  731. return 0;
  732. drop:
  733. if (tun_dst)
  734. dst_release((struct dst_entry *)tun_dst);
  735. kfree_skb(skb);
  736. return 0;
  737. }
  738. int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
  739. const struct tnl_ptk_info *tpi,
  740. struct metadata_dst *tun_dst,
  741. bool log_ecn_err)
  742. {
  743. return __ip6_tnl_rcv(t, skb, tpi, NULL, ip6ip6_dscp_ecn_decapsulate,
  744. log_ecn_err);
  745. }
  746. EXPORT_SYMBOL(ip6_tnl_rcv);
  747. static const struct tnl_ptk_info tpi_v6 = {
  748. /* no tunnel info required for ipxip6. */
  749. .proto = htons(ETH_P_IPV6),
  750. };
  751. static const struct tnl_ptk_info tpi_v4 = {
  752. /* no tunnel info required for ipxip6. */
  753. .proto = htons(ETH_P_IP),
  754. };
  755. static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
  756. const struct tnl_ptk_info *tpi,
  757. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  758. const struct ipv6hdr *ipv6h,
  759. struct sk_buff *skb))
  760. {
  761. struct ip6_tnl *t;
  762. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  763. struct metadata_dst *tun_dst = NULL;
  764. int ret = -1;
  765. rcu_read_lock();
  766. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  767. if (t) {
  768. u8 tproto = ACCESS_ONCE(t->parms.proto);
  769. if (tproto != ipproto && tproto != 0)
  770. goto drop;
  771. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  772. goto drop;
  773. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
  774. goto drop;
  775. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  776. goto drop;
  777. if (t->parms.collect_md) {
  778. tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
  779. if (!tun_dst)
  780. return 0;
  781. }
  782. ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
  783. log_ecn_error);
  784. }
  785. rcu_read_unlock();
  786. return ret;
  787. drop:
  788. rcu_read_unlock();
  789. kfree_skb(skb);
  790. return 0;
  791. }
  792. static int ip4ip6_rcv(struct sk_buff *skb)
  793. {
  794. return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
  795. ip4ip6_dscp_ecn_decapsulate);
  796. }
  797. static int ip6ip6_rcv(struct sk_buff *skb)
  798. {
  799. return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
  800. ip6ip6_dscp_ecn_decapsulate);
  801. }
  802. struct ipv6_tel_txoption {
  803. struct ipv6_txoptions ops;
  804. __u8 dst_opt[8];
  805. };
  806. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  807. {
  808. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  809. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  810. opt->dst_opt[3] = 1;
  811. opt->dst_opt[4] = encap_limit;
  812. opt->dst_opt[5] = IPV6_TLV_PADN;
  813. opt->dst_opt[6] = 1;
  814. opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  815. opt->ops.opt_nflen = 8;
  816. }
  817. /**
  818. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  819. * @t: the outgoing tunnel device
  820. * @hdr: IPv6 header from the incoming packet
  821. *
  822. * Description:
  823. * Avoid trivial tunneling loop by checking that tunnel exit-point
  824. * doesn't match source of incoming packet.
  825. *
  826. * Return:
  827. * 1 if conflict,
  828. * 0 else
  829. **/
  830. static inline bool
  831. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  832. {
  833. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  834. }
  835. int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
  836. const struct in6_addr *laddr,
  837. const struct in6_addr *raddr)
  838. {
  839. struct __ip6_tnl_parm *p = &t->parms;
  840. int ret = 0;
  841. struct net *net = t->net;
  842. if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
  843. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  844. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
  845. struct net_device *ldev = NULL;
  846. rcu_read_lock();
  847. if (p->link)
  848. ldev = dev_get_by_index_rcu(net, p->link);
  849. if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
  850. pr_warn("%s xmit: Local address not yet configured!\n",
  851. p->name);
  852. else if (!ipv6_addr_is_multicast(raddr) &&
  853. unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
  854. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  855. p->name);
  856. else
  857. ret = 1;
  858. rcu_read_unlock();
  859. }
  860. return ret;
  861. }
  862. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  863. /**
  864. * ip6_tnl_xmit - encapsulate packet and send
  865. * @skb: the outgoing socket buffer
  866. * @dev: the outgoing tunnel device
  867. * @dsfield: dscp code for outer header
  868. * @fl6: flow of tunneled packet
  869. * @encap_limit: encapsulation limit
  870. * @pmtu: Path MTU is stored if packet is too big
  871. * @proto: next header value
  872. *
  873. * Description:
  874. * Build new header and do some sanity checks on the packet before sending
  875. * it.
  876. *
  877. * Return:
  878. * 0 on success
  879. * -1 fail
  880. * %-EMSGSIZE message too big. return mtu in this case.
  881. **/
  882. int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
  883. struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
  884. __u8 proto)
  885. {
  886. struct ip6_tnl *t = netdev_priv(dev);
  887. struct net *net = t->net;
  888. struct net_device_stats *stats = &t->dev->stats;
  889. struct ipv6hdr *ipv6h;
  890. struct ipv6_tel_txoption opt;
  891. struct dst_entry *dst = NULL, *ndst = NULL;
  892. struct net_device *tdev;
  893. int mtu;
  894. unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
  895. unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
  896. unsigned int max_headroom = psh_hlen;
  897. bool use_cache = false;
  898. u8 hop_limit;
  899. int err = -1;
  900. if (t->parms.collect_md) {
  901. hop_limit = skb_tunnel_info(skb)->key.ttl;
  902. goto route_lookup;
  903. } else {
  904. hop_limit = t->parms.hop_limit;
  905. }
  906. /* NBMA tunnel */
  907. if (ipv6_addr_any(&t->parms.raddr)) {
  908. if (skb->protocol == htons(ETH_P_IPV6)) {
  909. struct in6_addr *addr6;
  910. struct neighbour *neigh;
  911. int addr_type;
  912. if (!skb_dst(skb))
  913. goto tx_err_link_failure;
  914. neigh = dst_neigh_lookup(skb_dst(skb),
  915. &ipv6_hdr(skb)->daddr);
  916. if (!neigh)
  917. goto tx_err_link_failure;
  918. addr6 = (struct in6_addr *)&neigh->primary_key;
  919. addr_type = ipv6_addr_type(addr6);
  920. if (addr_type == IPV6_ADDR_ANY)
  921. addr6 = &ipv6_hdr(skb)->daddr;
  922. memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
  923. neigh_release(neigh);
  924. }
  925. } else if (!(t->parms.flags &
  926. (IP6_TNL_F_USE_ORIG_TCLASS | IP6_TNL_F_USE_ORIG_FWMARK))) {
  927. /* enable the cache only only if the routing decision does
  928. * not depend on the current inner header value
  929. */
  930. use_cache = true;
  931. }
  932. if (use_cache)
  933. dst = dst_cache_get(&t->dst_cache);
  934. if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
  935. goto tx_err_link_failure;
  936. if (!dst) {
  937. route_lookup:
  938. /* add dsfield to flowlabel for route lookup */
  939. fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
  940. dst = ip6_route_output(net, NULL, fl6);
  941. if (dst->error)
  942. goto tx_err_link_failure;
  943. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  944. if (IS_ERR(dst)) {
  945. err = PTR_ERR(dst);
  946. dst = NULL;
  947. goto tx_err_link_failure;
  948. }
  949. if (t->parms.collect_md &&
  950. ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
  951. &fl6->daddr, 0, &fl6->saddr))
  952. goto tx_err_link_failure;
  953. ndst = dst;
  954. }
  955. tdev = dst->dev;
  956. if (tdev == dev) {
  957. stats->collisions++;
  958. net_warn_ratelimited("%s: Local routing loop detected!\n",
  959. t->parms.name);
  960. goto tx_err_dst_release;
  961. }
  962. mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
  963. if (encap_limit >= 0) {
  964. max_headroom += 8;
  965. mtu -= 8;
  966. }
  967. if (mtu < IPV6_MIN_MTU)
  968. mtu = IPV6_MIN_MTU;
  969. if (skb_dst(skb) && !t->parms.collect_md)
  970. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  971. if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
  972. *pmtu = mtu;
  973. err = -EMSGSIZE;
  974. goto tx_err_dst_release;
  975. }
  976. if (t->err_count > 0) {
  977. if (time_before(jiffies,
  978. t->err_time + IP6TUNNEL_ERR_TIMEO)) {
  979. t->err_count--;
  980. dst_link_failure(skb);
  981. } else {
  982. t->err_count = 0;
  983. }
  984. }
  985. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  986. /*
  987. * Okay, now see if we can stuff it in the buffer as-is.
  988. */
  989. max_headroom += LL_RESERVED_SPACE(tdev);
  990. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  991. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  992. struct sk_buff *new_skb;
  993. new_skb = skb_realloc_headroom(skb, max_headroom);
  994. if (!new_skb)
  995. goto tx_err_dst_release;
  996. if (skb->sk)
  997. skb_set_owner_w(new_skb, skb->sk);
  998. consume_skb(skb);
  999. skb = new_skb;
  1000. }
  1001. if (t->parms.collect_md) {
  1002. if (t->encap.type != TUNNEL_ENCAP_NONE)
  1003. goto tx_err_dst_release;
  1004. } else {
  1005. if (use_cache && ndst)
  1006. dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
  1007. }
  1008. skb_dst_set(skb, dst);
  1009. if (encap_limit >= 0) {
  1010. init_tel_txopt(&opt, encap_limit);
  1011. ipv6_push_frag_opts(skb, &opt.ops, &proto);
  1012. }
  1013. hop_limit = hop_limit ? : ip6_dst_hoplimit(dst);
  1014. /* Calculate max headroom for all the headers and adjust
  1015. * needed_headroom if necessary.
  1016. */
  1017. max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
  1018. + dst->header_len + t->hlen;
  1019. if (max_headroom > dev->needed_headroom)
  1020. dev->needed_headroom = max_headroom;
  1021. err = ip6_tnl_encap(skb, t, &proto, fl6);
  1022. if (err)
  1023. return err;
  1024. skb_push(skb, sizeof(struct ipv6hdr));
  1025. skb_reset_network_header(skb);
  1026. ipv6h = ipv6_hdr(skb);
  1027. ip6_flow_hdr(ipv6h, dsfield,
  1028. ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
  1029. ipv6h->hop_limit = hop_limit;
  1030. ipv6h->nexthdr = proto;
  1031. ipv6h->saddr = fl6->saddr;
  1032. ipv6h->daddr = fl6->daddr;
  1033. ip6tunnel_xmit(NULL, skb, dev);
  1034. return 0;
  1035. tx_err_link_failure:
  1036. stats->tx_carrier_errors++;
  1037. dst_link_failure(skb);
  1038. tx_err_dst_release:
  1039. dst_release(dst);
  1040. return err;
  1041. }
  1042. EXPORT_SYMBOL(ip6_tnl_xmit);
  1043. static inline int
  1044. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1045. {
  1046. struct ip6_tnl *t = netdev_priv(dev);
  1047. const struct iphdr *iph = ip_hdr(skb);
  1048. int encap_limit = -1;
  1049. struct flowi6 fl6;
  1050. __u8 dsfield;
  1051. __u32 mtu;
  1052. u8 tproto;
  1053. int err;
  1054. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1055. tproto = ACCESS_ONCE(t->parms.proto);
  1056. if (tproto != IPPROTO_IPIP && tproto != 0)
  1057. return -1;
  1058. if (t->parms.collect_md) {
  1059. struct ip_tunnel_info *tun_info;
  1060. const struct ip_tunnel_key *key;
  1061. tun_info = skb_tunnel_info(skb);
  1062. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1063. ip_tunnel_info_af(tun_info) != AF_INET6))
  1064. return -1;
  1065. key = &tun_info->key;
  1066. memset(&fl6, 0, sizeof(fl6));
  1067. fl6.flowi6_proto = IPPROTO_IPIP;
  1068. fl6.daddr = key->u.ipv6.dst;
  1069. fl6.flowlabel = key->label;
  1070. dsfield = key->tos;
  1071. } else {
  1072. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1073. encap_limit = t->parms.encap_limit;
  1074. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1075. fl6.flowi6_proto = IPPROTO_IPIP;
  1076. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1077. dsfield = ipv4_get_dsfield(iph);
  1078. else
  1079. dsfield = ip6_tclass(t->parms.flowinfo);
  1080. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1081. fl6.flowi6_mark = skb->mark;
  1082. else
  1083. fl6.flowi6_mark = t->parms.fwmark;
  1084. }
  1085. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  1086. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1087. return -1;
  1088. dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
  1089. skb_set_inner_ipproto(skb, IPPROTO_IPIP);
  1090. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1091. IPPROTO_IPIP);
  1092. if (err != 0) {
  1093. /* XXX: send ICMP error even if DF is not set. */
  1094. if (err == -EMSGSIZE)
  1095. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  1096. htonl(mtu));
  1097. return -1;
  1098. }
  1099. return 0;
  1100. }
  1101. static inline int
  1102. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1103. {
  1104. struct ip6_tnl *t = netdev_priv(dev);
  1105. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  1106. int encap_limit = -1;
  1107. __u16 offset;
  1108. struct flowi6 fl6;
  1109. __u8 dsfield;
  1110. __u32 mtu;
  1111. u8 tproto;
  1112. int err;
  1113. tproto = ACCESS_ONCE(t->parms.proto);
  1114. if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
  1115. ip6_tnl_addr_conflict(t, ipv6h))
  1116. return -1;
  1117. if (t->parms.collect_md) {
  1118. struct ip_tunnel_info *tun_info;
  1119. const struct ip_tunnel_key *key;
  1120. tun_info = skb_tunnel_info(skb);
  1121. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1122. ip_tunnel_info_af(tun_info) != AF_INET6))
  1123. return -1;
  1124. key = &tun_info->key;
  1125. memset(&fl6, 0, sizeof(fl6));
  1126. fl6.flowi6_proto = IPPROTO_IPV6;
  1127. fl6.daddr = key->u.ipv6.dst;
  1128. fl6.flowlabel = key->label;
  1129. dsfield = key->tos;
  1130. } else {
  1131. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  1132. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  1133. ipv6h = ipv6_hdr(skb);
  1134. if (offset > 0) {
  1135. struct ipv6_tlv_tnl_enc_lim *tel;
  1136. tel = (void *)&skb_network_header(skb)[offset];
  1137. if (tel->encap_limit == 0) {
  1138. icmpv6_send(skb, ICMPV6_PARAMPROB,
  1139. ICMPV6_HDR_FIELD, offset + 2);
  1140. return -1;
  1141. }
  1142. encap_limit = tel->encap_limit - 1;
  1143. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  1144. encap_limit = t->parms.encap_limit;
  1145. }
  1146. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1147. fl6.flowi6_proto = IPPROTO_IPV6;
  1148. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1149. dsfield = ipv6_get_dsfield(ipv6h);
  1150. else
  1151. dsfield = ip6_tclass(t->parms.flowinfo);
  1152. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  1153. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  1154. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1155. fl6.flowi6_mark = skb->mark;
  1156. else
  1157. fl6.flowi6_mark = t->parms.fwmark;
  1158. }
  1159. fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
  1160. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1161. return -1;
  1162. dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
  1163. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  1164. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1165. IPPROTO_IPV6);
  1166. if (err != 0) {
  1167. if (err == -EMSGSIZE)
  1168. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  1169. return -1;
  1170. }
  1171. return 0;
  1172. }
  1173. static netdev_tx_t
  1174. ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1175. {
  1176. struct ip6_tnl *t = netdev_priv(dev);
  1177. struct net_device_stats *stats = &t->dev->stats;
  1178. int ret;
  1179. switch (skb->protocol) {
  1180. case htons(ETH_P_IP):
  1181. ret = ip4ip6_tnl_xmit(skb, dev);
  1182. break;
  1183. case htons(ETH_P_IPV6):
  1184. ret = ip6ip6_tnl_xmit(skb, dev);
  1185. break;
  1186. default:
  1187. goto tx_err;
  1188. }
  1189. if (ret < 0)
  1190. goto tx_err;
  1191. return NETDEV_TX_OK;
  1192. tx_err:
  1193. stats->tx_errors++;
  1194. stats->tx_dropped++;
  1195. kfree_skb(skb);
  1196. return NETDEV_TX_OK;
  1197. }
  1198. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1199. {
  1200. struct net_device *dev = t->dev;
  1201. struct __ip6_tnl_parm *p = &t->parms;
  1202. struct flowi6 *fl6 = &t->fl.u.ip6;
  1203. int t_hlen;
  1204. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1205. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1206. /* Set up flowi template */
  1207. fl6->saddr = p->laddr;
  1208. fl6->daddr = p->raddr;
  1209. fl6->flowi6_oif = p->link;
  1210. fl6->flowlabel = 0;
  1211. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1212. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1213. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1214. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1215. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1216. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1217. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1218. dev->flags |= IFF_POINTOPOINT;
  1219. else
  1220. dev->flags &= ~IFF_POINTOPOINT;
  1221. t->tun_hlen = 0;
  1222. t->hlen = t->encap_hlen + t->tun_hlen;
  1223. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1224. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1225. int strict = (ipv6_addr_type(&p->raddr) &
  1226. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1227. struct rt6_info *rt = rt6_lookup(t->net,
  1228. &p->raddr, &p->laddr,
  1229. p->link, strict);
  1230. if (!rt)
  1231. return;
  1232. if (rt->dst.dev) {
  1233. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1234. t_hlen;
  1235. dev->mtu = rt->dst.dev->mtu - t_hlen;
  1236. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1237. dev->mtu -= 8;
  1238. if (dev->mtu < IPV6_MIN_MTU)
  1239. dev->mtu = IPV6_MIN_MTU;
  1240. }
  1241. ip6_rt_put(rt);
  1242. }
  1243. }
  1244. /**
  1245. * ip6_tnl_change - update the tunnel parameters
  1246. * @t: tunnel to be changed
  1247. * @p: tunnel configuration parameters
  1248. *
  1249. * Description:
  1250. * ip6_tnl_change() updates the tunnel parameters
  1251. **/
  1252. static int
  1253. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1254. {
  1255. t->parms.laddr = p->laddr;
  1256. t->parms.raddr = p->raddr;
  1257. t->parms.flags = p->flags;
  1258. t->parms.hop_limit = p->hop_limit;
  1259. t->parms.encap_limit = p->encap_limit;
  1260. t->parms.flowinfo = p->flowinfo;
  1261. t->parms.link = p->link;
  1262. t->parms.proto = p->proto;
  1263. t->parms.fwmark = p->fwmark;
  1264. dst_cache_reset(&t->dst_cache);
  1265. ip6_tnl_link_config(t);
  1266. return 0;
  1267. }
  1268. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1269. {
  1270. struct net *net = t->net;
  1271. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1272. int err;
  1273. ip6_tnl_unlink(ip6n, t);
  1274. synchronize_net();
  1275. err = ip6_tnl_change(t, p);
  1276. ip6_tnl_link(ip6n, t);
  1277. netdev_state_change(t->dev);
  1278. return err;
  1279. }
  1280. static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1281. {
  1282. /* for default tnl0 device allow to change only the proto */
  1283. t->parms.proto = p->proto;
  1284. netdev_state_change(t->dev);
  1285. return 0;
  1286. }
  1287. static void
  1288. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1289. {
  1290. p->laddr = u->laddr;
  1291. p->raddr = u->raddr;
  1292. p->flags = u->flags;
  1293. p->hop_limit = u->hop_limit;
  1294. p->encap_limit = u->encap_limit;
  1295. p->flowinfo = u->flowinfo;
  1296. p->link = u->link;
  1297. p->proto = u->proto;
  1298. memcpy(p->name, u->name, sizeof(u->name));
  1299. }
  1300. static void
  1301. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1302. {
  1303. u->laddr = p->laddr;
  1304. u->raddr = p->raddr;
  1305. u->flags = p->flags;
  1306. u->hop_limit = p->hop_limit;
  1307. u->encap_limit = p->encap_limit;
  1308. u->flowinfo = p->flowinfo;
  1309. u->link = p->link;
  1310. u->proto = p->proto;
  1311. memcpy(u->name, p->name, sizeof(u->name));
  1312. }
  1313. /**
  1314. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1315. * @dev: virtual device associated with tunnel
  1316. * @ifr: parameters passed from userspace
  1317. * @cmd: command to be performed
  1318. *
  1319. * Description:
  1320. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1321. * from userspace.
  1322. *
  1323. * The possible commands are the following:
  1324. * %SIOCGETTUNNEL: get tunnel parameters for device
  1325. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1326. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1327. * %SIOCDELTUNNEL: delete tunnel
  1328. *
  1329. * The fallback device "ip6tnl0", created during module
  1330. * initialization, can be used for creating other tunnel devices.
  1331. *
  1332. * Return:
  1333. * 0 on success,
  1334. * %-EFAULT if unable to copy data to or from userspace,
  1335. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1336. * %-EINVAL if passed tunnel parameters are invalid,
  1337. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1338. * %-ENODEV if attempting to change or delete a nonexisting device
  1339. **/
  1340. static int
  1341. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1342. {
  1343. int err = 0;
  1344. struct ip6_tnl_parm p;
  1345. struct __ip6_tnl_parm p1;
  1346. struct ip6_tnl *t = netdev_priv(dev);
  1347. struct net *net = t->net;
  1348. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1349. memset(&p1, 0, sizeof(p1));
  1350. switch (cmd) {
  1351. case SIOCGETTUNNEL:
  1352. if (dev == ip6n->fb_tnl_dev) {
  1353. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1354. err = -EFAULT;
  1355. break;
  1356. }
  1357. ip6_tnl_parm_from_user(&p1, &p);
  1358. t = ip6_tnl_locate(net, &p1, 0);
  1359. if (IS_ERR(t))
  1360. t = netdev_priv(dev);
  1361. } else {
  1362. memset(&p, 0, sizeof(p));
  1363. }
  1364. ip6_tnl_parm_to_user(&p, &t->parms);
  1365. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
  1366. err = -EFAULT;
  1367. }
  1368. break;
  1369. case SIOCADDTUNNEL:
  1370. case SIOCCHGTUNNEL:
  1371. err = -EPERM;
  1372. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1373. break;
  1374. err = -EFAULT;
  1375. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1376. break;
  1377. err = -EINVAL;
  1378. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1379. p.proto != 0)
  1380. break;
  1381. ip6_tnl_parm_from_user(&p1, &p);
  1382. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1383. if (cmd == SIOCCHGTUNNEL) {
  1384. if (!IS_ERR(t)) {
  1385. if (t->dev != dev) {
  1386. err = -EEXIST;
  1387. break;
  1388. }
  1389. } else
  1390. t = netdev_priv(dev);
  1391. if (dev == ip6n->fb_tnl_dev)
  1392. err = ip6_tnl0_update(t, &p1);
  1393. else
  1394. err = ip6_tnl_update(t, &p1);
  1395. }
  1396. if (!IS_ERR(t)) {
  1397. err = 0;
  1398. ip6_tnl_parm_to_user(&p, &t->parms);
  1399. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1400. err = -EFAULT;
  1401. } else {
  1402. err = PTR_ERR(t);
  1403. }
  1404. break;
  1405. case SIOCDELTUNNEL:
  1406. err = -EPERM;
  1407. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1408. break;
  1409. if (dev == ip6n->fb_tnl_dev) {
  1410. err = -EFAULT;
  1411. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1412. break;
  1413. err = -ENOENT;
  1414. ip6_tnl_parm_from_user(&p1, &p);
  1415. t = ip6_tnl_locate(net, &p1, 0);
  1416. if (IS_ERR(t))
  1417. break;
  1418. err = -EPERM;
  1419. if (t->dev == ip6n->fb_tnl_dev)
  1420. break;
  1421. dev = t->dev;
  1422. }
  1423. err = 0;
  1424. unregister_netdevice(dev);
  1425. break;
  1426. default:
  1427. err = -EINVAL;
  1428. }
  1429. return err;
  1430. }
  1431. /**
  1432. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1433. * @dev: virtual device associated with tunnel
  1434. * @new_mtu: the new mtu
  1435. *
  1436. * Return:
  1437. * 0 on success,
  1438. * %-EINVAL if mtu too small
  1439. **/
  1440. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1441. {
  1442. struct ip6_tnl *tnl = netdev_priv(dev);
  1443. if (tnl->parms.proto == IPPROTO_IPIP) {
  1444. if (new_mtu < ETH_MIN_MTU)
  1445. return -EINVAL;
  1446. } else {
  1447. if (new_mtu < IPV6_MIN_MTU)
  1448. return -EINVAL;
  1449. }
  1450. if (new_mtu > 0xFFF8 - dev->hard_header_len)
  1451. return -EINVAL;
  1452. dev->mtu = new_mtu;
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(ip6_tnl_change_mtu);
  1456. int ip6_tnl_get_iflink(const struct net_device *dev)
  1457. {
  1458. struct ip6_tnl *t = netdev_priv(dev);
  1459. return t->parms.link;
  1460. }
  1461. EXPORT_SYMBOL(ip6_tnl_get_iflink);
  1462. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  1463. unsigned int num)
  1464. {
  1465. if (num >= MAX_IPTUN_ENCAP_OPS)
  1466. return -ERANGE;
  1467. return !cmpxchg((const struct ip6_tnl_encap_ops **)
  1468. &ip6tun_encaps[num],
  1469. NULL, ops) ? 0 : -1;
  1470. }
  1471. EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
  1472. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  1473. unsigned int num)
  1474. {
  1475. int ret;
  1476. if (num >= MAX_IPTUN_ENCAP_OPS)
  1477. return -ERANGE;
  1478. ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
  1479. &ip6tun_encaps[num],
  1480. ops, NULL) == ops) ? 0 : -1;
  1481. synchronize_net();
  1482. return ret;
  1483. }
  1484. EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
  1485. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  1486. struct ip_tunnel_encap *ipencap)
  1487. {
  1488. int hlen;
  1489. memset(&t->encap, 0, sizeof(t->encap));
  1490. hlen = ip6_encap_hlen(ipencap);
  1491. if (hlen < 0)
  1492. return hlen;
  1493. t->encap.type = ipencap->type;
  1494. t->encap.sport = ipencap->sport;
  1495. t->encap.dport = ipencap->dport;
  1496. t->encap.flags = ipencap->flags;
  1497. t->encap_hlen = hlen;
  1498. t->hlen = t->encap_hlen + t->tun_hlen;
  1499. return 0;
  1500. }
  1501. EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
  1502. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1503. .ndo_init = ip6_tnl_dev_init,
  1504. .ndo_uninit = ip6_tnl_dev_uninit,
  1505. .ndo_start_xmit = ip6_tnl_start_xmit,
  1506. .ndo_do_ioctl = ip6_tnl_ioctl,
  1507. .ndo_change_mtu = ip6_tnl_change_mtu,
  1508. .ndo_get_stats = ip6_get_stats,
  1509. .ndo_get_iflink = ip6_tnl_get_iflink,
  1510. };
  1511. #define IPXIPX_FEATURES (NETIF_F_SG | \
  1512. NETIF_F_FRAGLIST | \
  1513. NETIF_F_HIGHDMA | \
  1514. NETIF_F_GSO_SOFTWARE | \
  1515. NETIF_F_HW_CSUM)
  1516. /**
  1517. * ip6_tnl_dev_setup - setup virtual tunnel device
  1518. * @dev: virtual device associated with tunnel
  1519. *
  1520. * Description:
  1521. * Initialize function pointers and device parameters
  1522. **/
  1523. static void ip6_tnl_dev_setup(struct net_device *dev)
  1524. {
  1525. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1526. dev->needs_free_netdev = true;
  1527. dev->priv_destructor = ip6_dev_free;
  1528. dev->type = ARPHRD_TUNNEL6;
  1529. dev->flags |= IFF_NOARP;
  1530. dev->addr_len = sizeof(struct in6_addr);
  1531. dev->features |= NETIF_F_LLTX;
  1532. netif_keep_dst(dev);
  1533. dev->features |= IPXIPX_FEATURES;
  1534. dev->hw_features |= IPXIPX_FEATURES;
  1535. /* This perm addr will be used as interface identifier by IPv6 */
  1536. dev->addr_assign_type = NET_ADDR_RANDOM;
  1537. eth_random_addr(dev->perm_addr);
  1538. }
  1539. /**
  1540. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1541. * @dev: virtual device associated with tunnel
  1542. **/
  1543. static inline int
  1544. ip6_tnl_dev_init_gen(struct net_device *dev)
  1545. {
  1546. struct ip6_tnl *t = netdev_priv(dev);
  1547. int ret;
  1548. int t_hlen;
  1549. t->dev = dev;
  1550. t->net = dev_net(dev);
  1551. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1552. if (!dev->tstats)
  1553. return -ENOMEM;
  1554. ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
  1555. if (ret)
  1556. goto free_stats;
  1557. ret = gro_cells_init(&t->gro_cells, dev);
  1558. if (ret)
  1559. goto destroy_dst;
  1560. t->tun_hlen = 0;
  1561. t->hlen = t->encap_hlen + t->tun_hlen;
  1562. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1563. dev->type = ARPHRD_TUNNEL6;
  1564. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1565. dev->mtu = ETH_DATA_LEN - t_hlen;
  1566. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1567. dev->mtu -= 8;
  1568. dev->min_mtu = ETH_MIN_MTU;
  1569. dev->max_mtu = 0xFFF8 - dev->hard_header_len;
  1570. return 0;
  1571. destroy_dst:
  1572. dst_cache_destroy(&t->dst_cache);
  1573. free_stats:
  1574. free_percpu(dev->tstats);
  1575. dev->tstats = NULL;
  1576. return ret;
  1577. }
  1578. /**
  1579. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1580. * @dev: virtual device associated with tunnel
  1581. **/
  1582. static int ip6_tnl_dev_init(struct net_device *dev)
  1583. {
  1584. struct ip6_tnl *t = netdev_priv(dev);
  1585. int err = ip6_tnl_dev_init_gen(dev);
  1586. if (err)
  1587. return err;
  1588. ip6_tnl_link_config(t);
  1589. if (t->parms.collect_md) {
  1590. dev->features |= NETIF_F_NETNS_LOCAL;
  1591. netif_keep_dst(dev);
  1592. }
  1593. return 0;
  1594. }
  1595. /**
  1596. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1597. * @dev: fallback device
  1598. *
  1599. * Return: 0
  1600. **/
  1601. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1602. {
  1603. struct ip6_tnl *t = netdev_priv(dev);
  1604. struct net *net = dev_net(dev);
  1605. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1606. t->parms.proto = IPPROTO_IPV6;
  1607. dev_hold(dev);
  1608. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1609. return 0;
  1610. }
  1611. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
  1612. struct netlink_ext_ack *extack)
  1613. {
  1614. u8 proto;
  1615. if (!data || !data[IFLA_IPTUN_PROTO])
  1616. return 0;
  1617. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1618. if (proto != IPPROTO_IPV6 &&
  1619. proto != IPPROTO_IPIP &&
  1620. proto != 0)
  1621. return -EINVAL;
  1622. return 0;
  1623. }
  1624. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1625. struct __ip6_tnl_parm *parms)
  1626. {
  1627. memset(parms, 0, sizeof(*parms));
  1628. if (!data)
  1629. return;
  1630. if (data[IFLA_IPTUN_LINK])
  1631. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1632. if (data[IFLA_IPTUN_LOCAL])
  1633. parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
  1634. if (data[IFLA_IPTUN_REMOTE])
  1635. parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
  1636. if (data[IFLA_IPTUN_TTL])
  1637. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1638. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1639. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1640. if (data[IFLA_IPTUN_FLOWINFO])
  1641. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1642. if (data[IFLA_IPTUN_FLAGS])
  1643. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1644. if (data[IFLA_IPTUN_PROTO])
  1645. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1646. if (data[IFLA_IPTUN_COLLECT_METADATA])
  1647. parms->collect_md = true;
  1648. if (data[IFLA_IPTUN_FWMARK])
  1649. parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
  1650. }
  1651. static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
  1652. struct ip_tunnel_encap *ipencap)
  1653. {
  1654. bool ret = false;
  1655. memset(ipencap, 0, sizeof(*ipencap));
  1656. if (!data)
  1657. return ret;
  1658. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1659. ret = true;
  1660. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1661. }
  1662. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1663. ret = true;
  1664. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1665. }
  1666. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1667. ret = true;
  1668. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1669. }
  1670. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1671. ret = true;
  1672. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1673. }
  1674. return ret;
  1675. }
  1676. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1677. struct nlattr *tb[], struct nlattr *data[],
  1678. struct netlink_ext_ack *extack)
  1679. {
  1680. struct net *net = dev_net(dev);
  1681. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1682. struct ip6_tnl *nt, *t;
  1683. struct ip_tunnel_encap ipencap;
  1684. nt = netdev_priv(dev);
  1685. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1686. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1687. if (err < 0)
  1688. return err;
  1689. }
  1690. ip6_tnl_netlink_parms(data, &nt->parms);
  1691. if (nt->parms.collect_md) {
  1692. if (rtnl_dereference(ip6n->collect_md_tun))
  1693. return -EEXIST;
  1694. } else {
  1695. t = ip6_tnl_locate(net, &nt->parms, 0);
  1696. if (!IS_ERR(t))
  1697. return -EEXIST;
  1698. }
  1699. return ip6_tnl_create2(dev);
  1700. }
  1701. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1702. struct nlattr *data[],
  1703. struct netlink_ext_ack *extack)
  1704. {
  1705. struct ip6_tnl *t = netdev_priv(dev);
  1706. struct __ip6_tnl_parm p;
  1707. struct net *net = t->net;
  1708. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1709. struct ip_tunnel_encap ipencap;
  1710. if (dev == ip6n->fb_tnl_dev)
  1711. return -EINVAL;
  1712. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1713. int err = ip6_tnl_encap_setup(t, &ipencap);
  1714. if (err < 0)
  1715. return err;
  1716. }
  1717. ip6_tnl_netlink_parms(data, &p);
  1718. if (p.collect_md)
  1719. return -EINVAL;
  1720. t = ip6_tnl_locate(net, &p, 0);
  1721. if (!IS_ERR(t)) {
  1722. if (t->dev != dev)
  1723. return -EEXIST;
  1724. } else
  1725. t = netdev_priv(dev);
  1726. return ip6_tnl_update(t, &p);
  1727. }
  1728. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1729. {
  1730. struct net *net = dev_net(dev);
  1731. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1732. if (dev != ip6n->fb_tnl_dev)
  1733. unregister_netdevice_queue(dev, head);
  1734. }
  1735. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1736. {
  1737. return
  1738. /* IFLA_IPTUN_LINK */
  1739. nla_total_size(4) +
  1740. /* IFLA_IPTUN_LOCAL */
  1741. nla_total_size(sizeof(struct in6_addr)) +
  1742. /* IFLA_IPTUN_REMOTE */
  1743. nla_total_size(sizeof(struct in6_addr)) +
  1744. /* IFLA_IPTUN_TTL */
  1745. nla_total_size(1) +
  1746. /* IFLA_IPTUN_ENCAP_LIMIT */
  1747. nla_total_size(1) +
  1748. /* IFLA_IPTUN_FLOWINFO */
  1749. nla_total_size(4) +
  1750. /* IFLA_IPTUN_FLAGS */
  1751. nla_total_size(4) +
  1752. /* IFLA_IPTUN_PROTO */
  1753. nla_total_size(1) +
  1754. /* IFLA_IPTUN_ENCAP_TYPE */
  1755. nla_total_size(2) +
  1756. /* IFLA_IPTUN_ENCAP_FLAGS */
  1757. nla_total_size(2) +
  1758. /* IFLA_IPTUN_ENCAP_SPORT */
  1759. nla_total_size(2) +
  1760. /* IFLA_IPTUN_ENCAP_DPORT */
  1761. nla_total_size(2) +
  1762. /* IFLA_IPTUN_COLLECT_METADATA */
  1763. nla_total_size(0) +
  1764. /* IFLA_IPTUN_FWMARK */
  1765. nla_total_size(4) +
  1766. 0;
  1767. }
  1768. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1769. {
  1770. struct ip6_tnl *tunnel = netdev_priv(dev);
  1771. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1772. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1773. nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
  1774. nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
  1775. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1776. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1777. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1778. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1779. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
  1780. nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
  1781. goto nla_put_failure;
  1782. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
  1783. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
  1784. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
  1785. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
  1786. goto nla_put_failure;
  1787. if (parm->collect_md)
  1788. if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
  1789. goto nla_put_failure;
  1790. return 0;
  1791. nla_put_failure:
  1792. return -EMSGSIZE;
  1793. }
  1794. struct net *ip6_tnl_get_link_net(const struct net_device *dev)
  1795. {
  1796. struct ip6_tnl *tunnel = netdev_priv(dev);
  1797. return tunnel->net;
  1798. }
  1799. EXPORT_SYMBOL(ip6_tnl_get_link_net);
  1800. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1801. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1802. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1803. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1804. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1805. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1806. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1807. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1808. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1809. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1810. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1811. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1812. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1813. [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
  1814. [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
  1815. };
  1816. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1817. .kind = "ip6tnl",
  1818. .maxtype = IFLA_IPTUN_MAX,
  1819. .policy = ip6_tnl_policy,
  1820. .priv_size = sizeof(struct ip6_tnl),
  1821. .setup = ip6_tnl_dev_setup,
  1822. .validate = ip6_tnl_validate,
  1823. .newlink = ip6_tnl_newlink,
  1824. .changelink = ip6_tnl_changelink,
  1825. .dellink = ip6_tnl_dellink,
  1826. .get_size = ip6_tnl_get_size,
  1827. .fill_info = ip6_tnl_fill_info,
  1828. .get_link_net = ip6_tnl_get_link_net,
  1829. };
  1830. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1831. .handler = ip4ip6_rcv,
  1832. .err_handler = ip4ip6_err,
  1833. .priority = 1,
  1834. };
  1835. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1836. .handler = ip6ip6_rcv,
  1837. .err_handler = ip6ip6_err,
  1838. .priority = 1,
  1839. };
  1840. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
  1841. {
  1842. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1843. struct net_device *dev, *aux;
  1844. int h;
  1845. struct ip6_tnl *t;
  1846. LIST_HEAD(list);
  1847. for_each_netdev_safe(net, dev, aux)
  1848. if (dev->rtnl_link_ops == &ip6_link_ops)
  1849. unregister_netdevice_queue(dev, &list);
  1850. for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
  1851. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1852. while (t) {
  1853. /* If dev is in the same netns, it has already
  1854. * been added to the list by the previous loop.
  1855. */
  1856. if (!net_eq(dev_net(t->dev), net))
  1857. unregister_netdevice_queue(t->dev, &list);
  1858. t = rtnl_dereference(t->next);
  1859. }
  1860. }
  1861. unregister_netdevice_many(&list);
  1862. }
  1863. static int __net_init ip6_tnl_init_net(struct net *net)
  1864. {
  1865. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1866. struct ip6_tnl *t = NULL;
  1867. int err;
  1868. ip6n->tnls[0] = ip6n->tnls_wc;
  1869. ip6n->tnls[1] = ip6n->tnls_r_l;
  1870. err = -ENOMEM;
  1871. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1872. NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
  1873. if (!ip6n->fb_tnl_dev)
  1874. goto err_alloc_dev;
  1875. dev_net_set(ip6n->fb_tnl_dev, net);
  1876. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1877. /* FB netdevice is special: we have one, and only one per netns.
  1878. * Allowing to move it to another netns is clearly unsafe.
  1879. */
  1880. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1881. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1882. if (err < 0)
  1883. goto err_register;
  1884. err = register_netdev(ip6n->fb_tnl_dev);
  1885. if (err < 0)
  1886. goto err_register;
  1887. t = netdev_priv(ip6n->fb_tnl_dev);
  1888. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1889. return 0;
  1890. err_register:
  1891. free_netdev(ip6n->fb_tnl_dev);
  1892. err_alloc_dev:
  1893. return err;
  1894. }
  1895. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1896. {
  1897. rtnl_lock();
  1898. ip6_tnl_destroy_tunnels(net);
  1899. rtnl_unlock();
  1900. }
  1901. static struct pernet_operations ip6_tnl_net_ops = {
  1902. .init = ip6_tnl_init_net,
  1903. .exit = ip6_tnl_exit_net,
  1904. .id = &ip6_tnl_net_id,
  1905. .size = sizeof(struct ip6_tnl_net),
  1906. };
  1907. /**
  1908. * ip6_tunnel_init - register protocol and reserve needed resources
  1909. *
  1910. * Return: 0 on success
  1911. **/
  1912. static int __init ip6_tunnel_init(void)
  1913. {
  1914. int err;
  1915. if (!ipv6_mod_enabled())
  1916. return -EOPNOTSUPP;
  1917. err = register_pernet_device(&ip6_tnl_net_ops);
  1918. if (err < 0)
  1919. goto out_pernet;
  1920. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1921. if (err < 0) {
  1922. pr_err("%s: can't register ip4ip6\n", __func__);
  1923. goto out_ip4ip6;
  1924. }
  1925. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1926. if (err < 0) {
  1927. pr_err("%s: can't register ip6ip6\n", __func__);
  1928. goto out_ip6ip6;
  1929. }
  1930. err = rtnl_link_register(&ip6_link_ops);
  1931. if (err < 0)
  1932. goto rtnl_link_failed;
  1933. return 0;
  1934. rtnl_link_failed:
  1935. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1936. out_ip6ip6:
  1937. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1938. out_ip4ip6:
  1939. unregister_pernet_device(&ip6_tnl_net_ops);
  1940. out_pernet:
  1941. return err;
  1942. }
  1943. /**
  1944. * ip6_tunnel_cleanup - free resources and unregister protocol
  1945. **/
  1946. static void __exit ip6_tunnel_cleanup(void)
  1947. {
  1948. rtnl_link_unregister(&ip6_link_ops);
  1949. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1950. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1951. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1952. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1953. unregister_pernet_device(&ip6_tnl_net_ops);
  1954. }
  1955. module_init(ip6_tunnel_init);
  1956. module_exit(ip6_tunnel_cleanup);