ip6_tunnel.c 57 KB

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