ip6_tunnel.c 57 KB

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