ip6_tunnel.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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 <asm/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. MODULE_AUTHOR("Ville Nuorvala");
  57. MODULE_DESCRIPTION("IPv6 tunneling device");
  58. MODULE_LICENSE("GPL");
  59. MODULE_ALIAS_NETDEV("ip6tnl0");
  60. #ifdef IP6_TNL_DEBUG
  61. #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
  62. #else
  63. #define IP6_TNL_TRACE(x...) do {;} while(0)
  64. #endif
  65. #define HASH_SIZE_SHIFT 5
  66. #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
  67. static bool log_ecn_error = true;
  68. module_param(log_ecn_error, bool, 0644);
  69. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  70. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  71. {
  72. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  73. return hash_32(hash, HASH_SIZE_SHIFT);
  74. }
  75. static int ip6_tnl_dev_init(struct net_device *dev);
  76. static void ip6_tnl_dev_setup(struct net_device *dev);
  77. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  78. static int ip6_tnl_net_id __read_mostly;
  79. struct ip6_tnl_net {
  80. /* the IPv6 tunnel fallback device */
  81. struct net_device *fb_tnl_dev;
  82. /* lists for storing tunnels in use */
  83. struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
  84. struct ip6_tnl __rcu *tnls_wc[1];
  85. struct ip6_tnl __rcu **tnls[2];
  86. };
  87. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  88. {
  89. struct pcpu_sw_netstats tmp, sum = { 0 };
  90. int i;
  91. for_each_possible_cpu(i) {
  92. unsigned int start;
  93. const struct pcpu_sw_netstats *tstats =
  94. per_cpu_ptr(dev->tstats, i);
  95. do {
  96. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  97. tmp.rx_packets = tstats->rx_packets;
  98. tmp.rx_bytes = tstats->rx_bytes;
  99. tmp.tx_packets = tstats->tx_packets;
  100. tmp.tx_bytes = tstats->tx_bytes;
  101. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  102. sum.rx_packets += tmp.rx_packets;
  103. sum.rx_bytes += tmp.rx_bytes;
  104. sum.tx_packets += tmp.tx_packets;
  105. sum.tx_bytes += tmp.tx_bytes;
  106. }
  107. dev->stats.rx_packets = sum.rx_packets;
  108. dev->stats.rx_bytes = sum.rx_bytes;
  109. dev->stats.tx_packets = sum.tx_packets;
  110. dev->stats.tx_bytes = sum.tx_bytes;
  111. return &dev->stats;
  112. }
  113. /*
  114. * Locking : hash tables are protected by RCU and RTNL
  115. */
  116. struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  117. {
  118. struct dst_entry *dst = t->dst_cache;
  119. if (dst && dst->obsolete &&
  120. dst->ops->check(dst, t->dst_cookie) == NULL) {
  121. t->dst_cache = NULL;
  122. dst_release(dst);
  123. return NULL;
  124. }
  125. return dst;
  126. }
  127. EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
  128. void ip6_tnl_dst_reset(struct ip6_tnl *t)
  129. {
  130. dst_release(t->dst_cache);
  131. t->dst_cache = NULL;
  132. }
  133. EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
  134. void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  135. {
  136. struct rt6_info *rt = (struct rt6_info *) dst;
  137. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  138. dst_release(t->dst_cache);
  139. t->dst_cache = dst;
  140. }
  141. EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
  142. /**
  143. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  144. * @remote: the address of the tunnel exit-point
  145. * @local: the address of the tunnel entry-point
  146. *
  147. * Return:
  148. * tunnel matching given end-points if found,
  149. * else fallback tunnel if its device is up,
  150. * else %NULL
  151. **/
  152. #define for_each_ip6_tunnel_rcu(start) \
  153. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  154. static struct ip6_tnl *
  155. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  156. {
  157. unsigned int hash = HASH(remote, local);
  158. struct ip6_tnl *t;
  159. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  160. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  161. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  162. ipv6_addr_equal(remote, &t->parms.raddr) &&
  163. (t->dev->flags & IFF_UP))
  164. return t;
  165. }
  166. t = rcu_dereference(ip6n->tnls_wc[0]);
  167. if (t && (t->dev->flags & IFF_UP))
  168. return t;
  169. return NULL;
  170. }
  171. /**
  172. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  173. * @p: parameters containing tunnel end-points
  174. *
  175. * Description:
  176. * ip6_tnl_bucket() returns the head of the list matching the
  177. * &struct in6_addr entries laddr and raddr in @p.
  178. *
  179. * Return: head of IPv6 tunnel list
  180. **/
  181. static struct ip6_tnl __rcu **
  182. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  183. {
  184. const struct in6_addr *remote = &p->raddr;
  185. const struct in6_addr *local = &p->laddr;
  186. unsigned int h = 0;
  187. int prio = 0;
  188. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  189. prio = 1;
  190. h = HASH(remote, local);
  191. }
  192. return &ip6n->tnls[prio][h];
  193. }
  194. /**
  195. * ip6_tnl_link - add tunnel to hash table
  196. * @t: tunnel to be added
  197. **/
  198. static void
  199. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  200. {
  201. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  202. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  203. rcu_assign_pointer(*tp, t);
  204. }
  205. /**
  206. * ip6_tnl_unlink - remove tunnel from hash table
  207. * @t: tunnel to be removed
  208. **/
  209. static void
  210. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  211. {
  212. struct ip6_tnl __rcu **tp;
  213. struct ip6_tnl *iter;
  214. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  215. (iter = rtnl_dereference(*tp)) != NULL;
  216. tp = &iter->next) {
  217. if (t == iter) {
  218. rcu_assign_pointer(*tp, t->next);
  219. break;
  220. }
  221. }
  222. }
  223. static void ip6_dev_free(struct net_device *dev)
  224. {
  225. free_percpu(dev->tstats);
  226. free_netdev(dev);
  227. }
  228. static int ip6_tnl_create2(struct net_device *dev)
  229. {
  230. struct ip6_tnl *t = netdev_priv(dev);
  231. struct net *net = dev_net(dev);
  232. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  233. int err;
  234. t = netdev_priv(dev);
  235. err = ip6_tnl_dev_init(dev);
  236. if (err < 0)
  237. goto out;
  238. err = register_netdevice(dev);
  239. if (err < 0)
  240. goto out;
  241. strcpy(t->parms.name, dev->name);
  242. dev->rtnl_link_ops = &ip6_link_ops;
  243. dev_hold(dev);
  244. ip6_tnl_link(ip6n, t);
  245. return 0;
  246. out:
  247. return err;
  248. }
  249. /**
  250. * ip6_tnl_create - create a new tunnel
  251. * @p: tunnel parameters
  252. * @pt: pointer to new tunnel
  253. *
  254. * Description:
  255. * Create tunnel matching given parameters.
  256. *
  257. * Return:
  258. * created tunnel or NULL
  259. **/
  260. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  261. {
  262. struct net_device *dev;
  263. struct ip6_tnl *t;
  264. char name[IFNAMSIZ];
  265. int err;
  266. if (p->name[0])
  267. strlcpy(name, p->name, IFNAMSIZ);
  268. else
  269. sprintf(name, "ip6tnl%%d");
  270. dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
  271. if (dev == NULL)
  272. goto failed;
  273. dev_net_set(dev, net);
  274. t = netdev_priv(dev);
  275. t->parms = *p;
  276. t->net = dev_net(dev);
  277. err = ip6_tnl_create2(dev);
  278. if (err < 0)
  279. goto failed_free;
  280. return t;
  281. failed_free:
  282. ip6_dev_free(dev);
  283. failed:
  284. return NULL;
  285. }
  286. /**
  287. * ip6_tnl_locate - find or create tunnel matching given parameters
  288. * @p: tunnel parameters
  289. * @create: != 0 if allowed to create new tunnel if no match found
  290. *
  291. * Description:
  292. * ip6_tnl_locate() first tries to locate an existing tunnel
  293. * based on @parms. If this is unsuccessful, but @create is set a new
  294. * tunnel device is created and registered for use.
  295. *
  296. * Return:
  297. * matching tunnel or NULL
  298. **/
  299. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  300. struct __ip6_tnl_parm *p, int create)
  301. {
  302. const struct in6_addr *remote = &p->raddr;
  303. const struct in6_addr *local = &p->laddr;
  304. struct ip6_tnl __rcu **tp;
  305. struct ip6_tnl *t;
  306. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  307. for (tp = ip6_tnl_bucket(ip6n, p);
  308. (t = rtnl_dereference(*tp)) != NULL;
  309. tp = &t->next) {
  310. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  311. ipv6_addr_equal(remote, &t->parms.raddr))
  312. return t;
  313. }
  314. if (!create)
  315. return NULL;
  316. return ip6_tnl_create(net, p);
  317. }
  318. /**
  319. * ip6_tnl_dev_uninit - tunnel device uninitializer
  320. * @dev: the device to be destroyed
  321. *
  322. * Description:
  323. * ip6_tnl_dev_uninit() removes tunnel from its list
  324. **/
  325. static void
  326. ip6_tnl_dev_uninit(struct net_device *dev)
  327. {
  328. struct ip6_tnl *t = netdev_priv(dev);
  329. struct net *net = t->net;
  330. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  331. if (dev == ip6n->fb_tnl_dev)
  332. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  333. else
  334. ip6_tnl_unlink(ip6n, t);
  335. ip6_tnl_dst_reset(t);
  336. dev_put(dev);
  337. }
  338. /**
  339. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  340. * @skb: received socket buffer
  341. *
  342. * Return:
  343. * 0 if none was found,
  344. * else index to encapsulation limit
  345. **/
  346. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  347. {
  348. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
  349. __u8 nexthdr = ipv6h->nexthdr;
  350. __u16 off = sizeof (*ipv6h);
  351. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  352. __u16 optlen = 0;
  353. struct ipv6_opt_hdr *hdr;
  354. if (raw + off + sizeof (*hdr) > skb->data &&
  355. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  356. break;
  357. hdr = (struct ipv6_opt_hdr *) (raw + off);
  358. if (nexthdr == NEXTHDR_FRAGMENT) {
  359. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  360. if (frag_hdr->frag_off)
  361. break;
  362. optlen = 8;
  363. } else if (nexthdr == NEXTHDR_AUTH) {
  364. optlen = (hdr->hdrlen + 2) << 2;
  365. } else {
  366. optlen = ipv6_optlen(hdr);
  367. }
  368. if (nexthdr == NEXTHDR_DEST) {
  369. __u16 i = off + 2;
  370. while (1) {
  371. struct ipv6_tlv_tnl_enc_lim *tel;
  372. /* No more room for encapsulation limit */
  373. if (i + sizeof (*tel) > off + optlen)
  374. break;
  375. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  376. /* return index of option if found and valid */
  377. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  378. tel->length == 1)
  379. return i;
  380. /* else jump to next option */
  381. if (tel->type)
  382. i += tel->length + 2;
  383. else
  384. i++;
  385. }
  386. }
  387. nexthdr = hdr->nexthdr;
  388. off += optlen;
  389. }
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  393. /**
  394. * ip6_tnl_err - tunnel error handler
  395. *
  396. * Description:
  397. * ip6_tnl_err() should handle errors in the tunnel according
  398. * to the specifications in RFC 2473.
  399. **/
  400. static int
  401. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  402. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  403. {
  404. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
  405. struct ip6_tnl *t;
  406. int rel_msg = 0;
  407. u8 rel_type = ICMPV6_DEST_UNREACH;
  408. u8 rel_code = ICMPV6_ADDR_UNREACH;
  409. __u32 rel_info = 0;
  410. __u16 len;
  411. int err = -ENOENT;
  412. /* If the packet doesn't contain the original IPv6 header we are
  413. in trouble since we might need the source address for further
  414. processing of the error. */
  415. rcu_read_lock();
  416. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
  417. &ipv6h->saddr)) == NULL)
  418. goto out;
  419. if (t->parms.proto != ipproto && t->parms.proto != 0)
  420. goto out;
  421. err = 0;
  422. switch (*type) {
  423. __u32 teli;
  424. struct ipv6_tlv_tnl_enc_lim *tel;
  425. __u32 mtu;
  426. case ICMPV6_DEST_UNREACH:
  427. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  428. t->parms.name);
  429. rel_msg = 1;
  430. break;
  431. case ICMPV6_TIME_EXCEED:
  432. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  433. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  434. t->parms.name);
  435. rel_msg = 1;
  436. }
  437. break;
  438. case ICMPV6_PARAMPROB:
  439. teli = 0;
  440. if ((*code) == ICMPV6_HDR_FIELD)
  441. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  442. if (teli && teli == *info - 2) {
  443. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  444. if (tel->encap_limit == 0) {
  445. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  446. t->parms.name);
  447. rel_msg = 1;
  448. }
  449. } else {
  450. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  451. t->parms.name);
  452. }
  453. break;
  454. case ICMPV6_PKT_TOOBIG:
  455. mtu = *info - offset;
  456. if (mtu < IPV6_MIN_MTU)
  457. mtu = IPV6_MIN_MTU;
  458. t->dev->mtu = mtu;
  459. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  460. rel_type = ICMPV6_PKT_TOOBIG;
  461. rel_code = 0;
  462. rel_info = mtu;
  463. rel_msg = 1;
  464. }
  465. break;
  466. }
  467. *type = rel_type;
  468. *code = rel_code;
  469. *info = rel_info;
  470. *msg = rel_msg;
  471. out:
  472. rcu_read_unlock();
  473. return err;
  474. }
  475. static int
  476. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  477. u8 type, u8 code, int offset, __be32 info)
  478. {
  479. int rel_msg = 0;
  480. u8 rel_type = type;
  481. u8 rel_code = code;
  482. __u32 rel_info = ntohl(info);
  483. int err;
  484. struct sk_buff *skb2;
  485. const struct iphdr *eiph;
  486. struct rtable *rt;
  487. struct flowi4 fl4;
  488. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  489. &rel_msg, &rel_info, offset);
  490. if (err < 0)
  491. return err;
  492. if (rel_msg == 0)
  493. return 0;
  494. switch (rel_type) {
  495. case ICMPV6_DEST_UNREACH:
  496. if (rel_code != ICMPV6_ADDR_UNREACH)
  497. return 0;
  498. rel_type = ICMP_DEST_UNREACH;
  499. rel_code = ICMP_HOST_UNREACH;
  500. break;
  501. case ICMPV6_PKT_TOOBIG:
  502. if (rel_code != 0)
  503. return 0;
  504. rel_type = ICMP_DEST_UNREACH;
  505. rel_code = ICMP_FRAG_NEEDED;
  506. break;
  507. case NDISC_REDIRECT:
  508. rel_type = ICMP_REDIRECT;
  509. rel_code = ICMP_REDIR_HOST;
  510. default:
  511. return 0;
  512. }
  513. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  514. return 0;
  515. skb2 = skb_clone(skb, GFP_ATOMIC);
  516. if (!skb2)
  517. return 0;
  518. skb_dst_drop(skb2);
  519. skb_pull(skb2, offset);
  520. skb_reset_network_header(skb2);
  521. eiph = ip_hdr(skb2);
  522. /* Try to guess incoming interface */
  523. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  524. eiph->saddr, 0,
  525. 0, 0,
  526. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  527. if (IS_ERR(rt))
  528. goto out;
  529. skb2->dev = rt->dst.dev;
  530. /* route "incoming" packet */
  531. if (rt->rt_flags & RTCF_LOCAL) {
  532. ip_rt_put(rt);
  533. rt = NULL;
  534. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  535. eiph->daddr, eiph->saddr,
  536. 0, 0,
  537. IPPROTO_IPIP,
  538. RT_TOS(eiph->tos), 0);
  539. if (IS_ERR(rt) ||
  540. rt->dst.dev->type != ARPHRD_TUNNEL) {
  541. if (!IS_ERR(rt))
  542. ip_rt_put(rt);
  543. goto out;
  544. }
  545. skb_dst_set(skb2, &rt->dst);
  546. } else {
  547. ip_rt_put(rt);
  548. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  549. skb2->dev) ||
  550. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  551. goto out;
  552. }
  553. /* change mtu on this route */
  554. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  555. if (rel_info > dst_mtu(skb_dst(skb2)))
  556. goto out;
  557. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
  558. }
  559. if (rel_type == ICMP_REDIRECT)
  560. skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
  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. int rel_msg = 0;
  571. u8 rel_type = type;
  572. u8 rel_code = code;
  573. __u32 rel_info = ntohl(info);
  574. int err;
  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, 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(net, laddr, ldev, 0))) &&
  653. likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
  654. ret = 1;
  655. }
  656. return ret;
  657. }
  658. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  659. /**
  660. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  661. * @skb: received socket buffer
  662. * @protocol: ethernet protocol ID
  663. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  664. *
  665. * Return: 0
  666. **/
  667. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  668. __u8 ipproto,
  669. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  670. const struct ipv6hdr *ipv6h,
  671. struct sk_buff *skb))
  672. {
  673. struct ip6_tnl *t;
  674. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  675. int err;
  676. rcu_read_lock();
  677. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  678. &ipv6h->daddr)) != NULL) {
  679. struct pcpu_sw_netstats *tstats;
  680. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  681. rcu_read_unlock();
  682. goto discard;
  683. }
  684. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  685. rcu_read_unlock();
  686. goto discard;
  687. }
  688. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  689. t->dev->stats.rx_dropped++;
  690. rcu_read_unlock();
  691. goto discard;
  692. }
  693. skb->mac_header = skb->network_header;
  694. skb_reset_network_header(skb);
  695. skb->protocol = htons(protocol);
  696. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  697. __skb_tunnel_rx(skb, t->dev, t->net);
  698. err = dscp_ecn_decapsulate(t, ipv6h, skb);
  699. if (unlikely(err)) {
  700. if (log_ecn_error)
  701. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  702. &ipv6h->saddr,
  703. ipv6_get_dsfield(ipv6h));
  704. if (err > 1) {
  705. ++t->dev->stats.rx_frame_errors;
  706. ++t->dev->stats.rx_errors;
  707. rcu_read_unlock();
  708. goto discard;
  709. }
  710. }
  711. tstats = this_cpu_ptr(t->dev->tstats);
  712. u64_stats_update_begin(&tstats->syncp);
  713. tstats->rx_packets++;
  714. tstats->rx_bytes += skb->len;
  715. u64_stats_update_end(&tstats->syncp);
  716. netif_rx(skb);
  717. rcu_read_unlock();
  718. return 0;
  719. }
  720. rcu_read_unlock();
  721. return 1;
  722. discard:
  723. kfree_skb(skb);
  724. return 0;
  725. }
  726. static int ip4ip6_rcv(struct sk_buff *skb)
  727. {
  728. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  729. ip4ip6_dscp_ecn_decapsulate);
  730. }
  731. static int ip6ip6_rcv(struct sk_buff *skb)
  732. {
  733. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  734. ip6ip6_dscp_ecn_decapsulate);
  735. }
  736. struct ipv6_tel_txoption {
  737. struct ipv6_txoptions ops;
  738. __u8 dst_opt[8];
  739. };
  740. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  741. {
  742. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  743. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  744. opt->dst_opt[3] = 1;
  745. opt->dst_opt[4] = encap_limit;
  746. opt->dst_opt[5] = IPV6_TLV_PADN;
  747. opt->dst_opt[6] = 1;
  748. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  749. opt->ops.opt_nflen = 8;
  750. }
  751. /**
  752. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  753. * @t: the outgoing tunnel device
  754. * @hdr: IPv6 header from the incoming packet
  755. *
  756. * Description:
  757. * Avoid trivial tunneling loop by checking that tunnel exit-point
  758. * doesn't match source of incoming packet.
  759. *
  760. * Return:
  761. * 1 if conflict,
  762. * 0 else
  763. **/
  764. static inline bool
  765. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  766. {
  767. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  768. }
  769. int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  770. {
  771. struct __ip6_tnl_parm *p = &t->parms;
  772. int ret = 0;
  773. struct net *net = t->net;
  774. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  775. struct net_device *ldev = NULL;
  776. rcu_read_lock();
  777. if (p->link)
  778. ldev = dev_get_by_index_rcu(net, p->link);
  779. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  780. pr_warn("%s xmit: Local address not yet configured!\n",
  781. p->name);
  782. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  783. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  784. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  785. p->name);
  786. else
  787. ret = 1;
  788. rcu_read_unlock();
  789. }
  790. return ret;
  791. }
  792. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  793. /**
  794. * ip6_tnl_xmit2 - encapsulate packet and send
  795. * @skb: the outgoing socket buffer
  796. * @dev: the outgoing tunnel device
  797. * @dsfield: dscp code for outer header
  798. * @fl: flow of tunneled packet
  799. * @encap_limit: encapsulation limit
  800. * @pmtu: Path MTU is stored if packet is too big
  801. *
  802. * Description:
  803. * Build new header and do some sanity checks on the packet before sending
  804. * it.
  805. *
  806. * Return:
  807. * 0 on success
  808. * -1 fail
  809. * %-EMSGSIZE message too big. return mtu in this case.
  810. **/
  811. static int ip6_tnl_xmit2(struct sk_buff *skb,
  812. struct net_device *dev,
  813. __u8 dsfield,
  814. struct flowi6 *fl6,
  815. int encap_limit,
  816. __u32 *pmtu)
  817. {
  818. struct ip6_tnl *t = netdev_priv(dev);
  819. struct net *net = t->net;
  820. struct net_device_stats *stats = &t->dev->stats;
  821. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  822. struct ipv6_tel_txoption opt;
  823. struct dst_entry *dst = NULL, *ndst = NULL;
  824. struct net_device *tdev;
  825. int mtu;
  826. unsigned int max_headroom = sizeof(struct ipv6hdr);
  827. u8 proto;
  828. int err = -1;
  829. if (!fl6->flowi6_mark)
  830. dst = ip6_tnl_dst_check(t);
  831. if (!dst) {
  832. ndst = ip6_route_output(net, NULL, fl6);
  833. if (ndst->error)
  834. goto tx_err_link_failure;
  835. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  836. if (IS_ERR(ndst)) {
  837. err = PTR_ERR(ndst);
  838. ndst = NULL;
  839. goto tx_err_link_failure;
  840. }
  841. dst = ndst;
  842. }
  843. tdev = dst->dev;
  844. if (tdev == dev) {
  845. stats->collisions++;
  846. net_warn_ratelimited("%s: Local routing loop detected!\n",
  847. t->parms.name);
  848. goto tx_err_dst_release;
  849. }
  850. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  851. if (encap_limit >= 0) {
  852. max_headroom += 8;
  853. mtu -= 8;
  854. }
  855. if (mtu < IPV6_MIN_MTU)
  856. mtu = IPV6_MIN_MTU;
  857. if (skb_dst(skb))
  858. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  859. if (skb->len > mtu) {
  860. *pmtu = mtu;
  861. err = -EMSGSIZE;
  862. goto tx_err_dst_release;
  863. }
  864. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  865. /*
  866. * Okay, now see if we can stuff it in the buffer as-is.
  867. */
  868. max_headroom += LL_RESERVED_SPACE(tdev);
  869. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  870. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  871. struct sk_buff *new_skb;
  872. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  873. goto tx_err_dst_release;
  874. if (skb->sk)
  875. skb_set_owner_w(new_skb, skb->sk);
  876. consume_skb(skb);
  877. skb = new_skb;
  878. }
  879. if (fl6->flowi6_mark) {
  880. skb_dst_set(skb, dst);
  881. ndst = NULL;
  882. } else {
  883. skb_dst_set_noref(skb, dst);
  884. }
  885. skb->transport_header = skb->network_header;
  886. proto = fl6->flowi6_proto;
  887. if (encap_limit >= 0) {
  888. init_tel_txopt(&opt, encap_limit);
  889. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  890. }
  891. if (likely(!skb->encapsulation)) {
  892. skb_reset_inner_headers(skb);
  893. skb->encapsulation = 1;
  894. }
  895. skb_push(skb, sizeof(struct ipv6hdr));
  896. skb_reset_network_header(skb);
  897. ipv6h = ipv6_hdr(skb);
  898. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
  899. ipv6h->hop_limit = t->parms.hop_limit;
  900. ipv6h->nexthdr = proto;
  901. ipv6h->saddr = fl6->saddr;
  902. ipv6h->daddr = fl6->daddr;
  903. ip6tunnel_xmit(skb, dev);
  904. if (ndst)
  905. ip6_tnl_dst_store(t, ndst);
  906. return 0;
  907. tx_err_link_failure:
  908. stats->tx_carrier_errors++;
  909. dst_link_failure(skb);
  910. tx_err_dst_release:
  911. dst_release(ndst);
  912. return err;
  913. }
  914. static inline int
  915. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  916. {
  917. struct ip6_tnl *t = netdev_priv(dev);
  918. const struct iphdr *iph = ip_hdr(skb);
  919. int encap_limit = -1;
  920. struct flowi6 fl6;
  921. __u8 dsfield;
  922. __u32 mtu;
  923. int err;
  924. if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
  925. !ip6_tnl_xmit_ctl(t))
  926. return -1;
  927. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  928. encap_limit = t->parms.encap_limit;
  929. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  930. fl6.flowi6_proto = IPPROTO_IPIP;
  931. dsfield = ipv4_get_dsfield(iph);
  932. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  933. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  934. & IPV6_TCLASS_MASK;
  935. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  936. fl6.flowi6_mark = skb->mark;
  937. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  938. if (err != 0) {
  939. /* XXX: send ICMP error even if DF is not set. */
  940. if (err == -EMSGSIZE)
  941. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  942. htonl(mtu));
  943. return -1;
  944. }
  945. return 0;
  946. }
  947. static inline int
  948. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  949. {
  950. struct ip6_tnl *t = netdev_priv(dev);
  951. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  952. int encap_limit = -1;
  953. __u16 offset;
  954. struct flowi6 fl6;
  955. __u8 dsfield;
  956. __u32 mtu;
  957. int err;
  958. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  959. !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
  960. return -1;
  961. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  962. if (offset > 0) {
  963. struct ipv6_tlv_tnl_enc_lim *tel;
  964. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  965. if (tel->encap_limit == 0) {
  966. icmpv6_send(skb, ICMPV6_PARAMPROB,
  967. ICMPV6_HDR_FIELD, offset + 2);
  968. return -1;
  969. }
  970. encap_limit = tel->encap_limit - 1;
  971. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  972. encap_limit = t->parms.encap_limit;
  973. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  974. fl6.flowi6_proto = IPPROTO_IPV6;
  975. dsfield = ipv6_get_dsfield(ipv6h);
  976. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  977. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  978. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  979. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  980. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  981. fl6.flowi6_mark = skb->mark;
  982. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  983. if (err != 0) {
  984. if (err == -EMSGSIZE)
  985. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  986. return -1;
  987. }
  988. return 0;
  989. }
  990. static netdev_tx_t
  991. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  992. {
  993. struct ip6_tnl *t = netdev_priv(dev);
  994. struct net_device_stats *stats = &t->dev->stats;
  995. int ret;
  996. switch (skb->protocol) {
  997. case htons(ETH_P_IP):
  998. ret = ip4ip6_tnl_xmit(skb, dev);
  999. break;
  1000. case htons(ETH_P_IPV6):
  1001. ret = ip6ip6_tnl_xmit(skb, dev);
  1002. break;
  1003. default:
  1004. goto tx_err;
  1005. }
  1006. if (ret < 0)
  1007. goto tx_err;
  1008. return NETDEV_TX_OK;
  1009. tx_err:
  1010. stats->tx_errors++;
  1011. stats->tx_dropped++;
  1012. kfree_skb(skb);
  1013. return NETDEV_TX_OK;
  1014. }
  1015. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1016. {
  1017. struct net_device *dev = t->dev;
  1018. struct __ip6_tnl_parm *p = &t->parms;
  1019. struct flowi6 *fl6 = &t->fl.u.ip6;
  1020. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1021. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1022. /* Set up flowi template */
  1023. fl6->saddr = p->laddr;
  1024. fl6->daddr = p->raddr;
  1025. fl6->flowi6_oif = p->link;
  1026. fl6->flowlabel = 0;
  1027. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1028. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1029. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1030. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1031. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1032. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1033. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1034. dev->flags |= IFF_POINTOPOINT;
  1035. else
  1036. dev->flags &= ~IFF_POINTOPOINT;
  1037. dev->iflink = p->link;
  1038. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1039. int strict = (ipv6_addr_type(&p->raddr) &
  1040. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1041. struct rt6_info *rt = rt6_lookup(t->net,
  1042. &p->raddr, &p->laddr,
  1043. p->link, strict);
  1044. if (rt == NULL)
  1045. return;
  1046. if (rt->dst.dev) {
  1047. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1048. sizeof (struct ipv6hdr);
  1049. dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
  1050. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1051. dev->mtu-=8;
  1052. if (dev->mtu < IPV6_MIN_MTU)
  1053. dev->mtu = IPV6_MIN_MTU;
  1054. }
  1055. ip6_rt_put(rt);
  1056. }
  1057. }
  1058. /**
  1059. * ip6_tnl_change - update the tunnel parameters
  1060. * @t: tunnel to be changed
  1061. * @p: tunnel configuration parameters
  1062. *
  1063. * Description:
  1064. * ip6_tnl_change() updates the tunnel parameters
  1065. **/
  1066. static int
  1067. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1068. {
  1069. t->parms.laddr = p->laddr;
  1070. t->parms.raddr = p->raddr;
  1071. t->parms.flags = p->flags;
  1072. t->parms.hop_limit = p->hop_limit;
  1073. t->parms.encap_limit = p->encap_limit;
  1074. t->parms.flowinfo = p->flowinfo;
  1075. t->parms.link = p->link;
  1076. t->parms.proto = p->proto;
  1077. ip6_tnl_dst_reset(t);
  1078. ip6_tnl_link_config(t);
  1079. return 0;
  1080. }
  1081. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1082. {
  1083. struct net *net = t->net;
  1084. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1085. int err;
  1086. ip6_tnl_unlink(ip6n, t);
  1087. synchronize_net();
  1088. err = ip6_tnl_change(t, p);
  1089. ip6_tnl_link(ip6n, t);
  1090. netdev_state_change(t->dev);
  1091. return err;
  1092. }
  1093. static void
  1094. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1095. {
  1096. p->laddr = u->laddr;
  1097. p->raddr = u->raddr;
  1098. p->flags = u->flags;
  1099. p->hop_limit = u->hop_limit;
  1100. p->encap_limit = u->encap_limit;
  1101. p->flowinfo = u->flowinfo;
  1102. p->link = u->link;
  1103. p->proto = u->proto;
  1104. memcpy(p->name, u->name, sizeof(u->name));
  1105. }
  1106. static void
  1107. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1108. {
  1109. u->laddr = p->laddr;
  1110. u->raddr = p->raddr;
  1111. u->flags = p->flags;
  1112. u->hop_limit = p->hop_limit;
  1113. u->encap_limit = p->encap_limit;
  1114. u->flowinfo = p->flowinfo;
  1115. u->link = p->link;
  1116. u->proto = p->proto;
  1117. memcpy(u->name, p->name, sizeof(u->name));
  1118. }
  1119. /**
  1120. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1121. * @dev: virtual device associated with tunnel
  1122. * @ifr: parameters passed from userspace
  1123. * @cmd: command to be performed
  1124. *
  1125. * Description:
  1126. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1127. * from userspace.
  1128. *
  1129. * The possible commands are the following:
  1130. * %SIOCGETTUNNEL: get tunnel parameters for device
  1131. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1132. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1133. * %SIOCDELTUNNEL: delete tunnel
  1134. *
  1135. * The fallback device "ip6tnl0", created during module
  1136. * initialization, can be used for creating other tunnel devices.
  1137. *
  1138. * Return:
  1139. * 0 on success,
  1140. * %-EFAULT if unable to copy data to or from userspace,
  1141. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1142. * %-EINVAL if passed tunnel parameters are invalid,
  1143. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1144. * %-ENODEV if attempting to change or delete a nonexisting device
  1145. **/
  1146. static int
  1147. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1148. {
  1149. int err = 0;
  1150. struct ip6_tnl_parm p;
  1151. struct __ip6_tnl_parm p1;
  1152. struct ip6_tnl *t = netdev_priv(dev);
  1153. struct net *net = t->net;
  1154. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1155. switch (cmd) {
  1156. case SIOCGETTUNNEL:
  1157. if (dev == ip6n->fb_tnl_dev) {
  1158. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1159. err = -EFAULT;
  1160. break;
  1161. }
  1162. ip6_tnl_parm_from_user(&p1, &p);
  1163. t = ip6_tnl_locate(net, &p1, 0);
  1164. if (t == NULL)
  1165. t = netdev_priv(dev);
  1166. } else {
  1167. memset(&p, 0, sizeof(p));
  1168. }
  1169. ip6_tnl_parm_to_user(&p, &t->parms);
  1170. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1171. err = -EFAULT;
  1172. }
  1173. break;
  1174. case SIOCADDTUNNEL:
  1175. case SIOCCHGTUNNEL:
  1176. err = -EPERM;
  1177. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1178. break;
  1179. err = -EFAULT;
  1180. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1181. break;
  1182. err = -EINVAL;
  1183. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1184. p.proto != 0)
  1185. break;
  1186. ip6_tnl_parm_from_user(&p1, &p);
  1187. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1188. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1189. if (t != NULL) {
  1190. if (t->dev != dev) {
  1191. err = -EEXIST;
  1192. break;
  1193. }
  1194. } else
  1195. t = netdev_priv(dev);
  1196. err = ip6_tnl_update(t, &p1);
  1197. }
  1198. if (t) {
  1199. err = 0;
  1200. ip6_tnl_parm_to_user(&p, &t->parms);
  1201. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1202. err = -EFAULT;
  1203. } else
  1204. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1205. break;
  1206. case SIOCDELTUNNEL:
  1207. err = -EPERM;
  1208. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1209. break;
  1210. if (dev == ip6n->fb_tnl_dev) {
  1211. err = -EFAULT;
  1212. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1213. break;
  1214. err = -ENOENT;
  1215. ip6_tnl_parm_from_user(&p1, &p);
  1216. t = ip6_tnl_locate(net, &p1, 0);
  1217. if (t == NULL)
  1218. break;
  1219. err = -EPERM;
  1220. if (t->dev == ip6n->fb_tnl_dev)
  1221. break;
  1222. dev = t->dev;
  1223. }
  1224. err = 0;
  1225. unregister_netdevice(dev);
  1226. break;
  1227. default:
  1228. err = -EINVAL;
  1229. }
  1230. return err;
  1231. }
  1232. /**
  1233. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1234. * @dev: virtual device associated with tunnel
  1235. * @new_mtu: the new mtu
  1236. *
  1237. * Return:
  1238. * 0 on success,
  1239. * %-EINVAL if mtu too small
  1240. **/
  1241. static int
  1242. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1243. {
  1244. struct ip6_tnl *tnl = netdev_priv(dev);
  1245. if (tnl->parms.proto == IPPROTO_IPIP) {
  1246. if (new_mtu < 68)
  1247. return -EINVAL;
  1248. } else {
  1249. if (new_mtu < IPV6_MIN_MTU)
  1250. return -EINVAL;
  1251. }
  1252. if (new_mtu > 0xFFF8 - dev->hard_header_len)
  1253. return -EINVAL;
  1254. dev->mtu = new_mtu;
  1255. return 0;
  1256. }
  1257. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1258. .ndo_uninit = ip6_tnl_dev_uninit,
  1259. .ndo_start_xmit = ip6_tnl_xmit,
  1260. .ndo_do_ioctl = ip6_tnl_ioctl,
  1261. .ndo_change_mtu = ip6_tnl_change_mtu,
  1262. .ndo_get_stats = ip6_get_stats,
  1263. };
  1264. /**
  1265. * ip6_tnl_dev_setup - setup virtual tunnel device
  1266. * @dev: virtual device associated with tunnel
  1267. *
  1268. * Description:
  1269. * Initialize function pointers and device parameters
  1270. **/
  1271. static void ip6_tnl_dev_setup(struct net_device *dev)
  1272. {
  1273. struct ip6_tnl *t;
  1274. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1275. dev->destructor = ip6_dev_free;
  1276. dev->type = ARPHRD_TUNNEL6;
  1277. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1278. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1279. t = netdev_priv(dev);
  1280. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1281. dev->mtu-=8;
  1282. dev->flags |= IFF_NOARP;
  1283. dev->addr_len = sizeof(struct in6_addr);
  1284. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1285. /* This perm addr will be used as interface identifier by IPv6 */
  1286. dev->addr_assign_type = NET_ADDR_RANDOM;
  1287. eth_random_addr(dev->perm_addr);
  1288. }
  1289. /**
  1290. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1291. * @dev: virtual device associated with tunnel
  1292. **/
  1293. static inline int
  1294. ip6_tnl_dev_init_gen(struct net_device *dev)
  1295. {
  1296. struct ip6_tnl *t = netdev_priv(dev);
  1297. t->dev = dev;
  1298. t->net = dev_net(dev);
  1299. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1300. if (!dev->tstats)
  1301. return -ENOMEM;
  1302. return 0;
  1303. }
  1304. /**
  1305. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1306. * @dev: virtual device associated with tunnel
  1307. **/
  1308. static int ip6_tnl_dev_init(struct net_device *dev)
  1309. {
  1310. struct ip6_tnl *t = netdev_priv(dev);
  1311. int err = ip6_tnl_dev_init_gen(dev);
  1312. if (err)
  1313. return err;
  1314. ip6_tnl_link_config(t);
  1315. return 0;
  1316. }
  1317. /**
  1318. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1319. * @dev: fallback device
  1320. *
  1321. * Return: 0
  1322. **/
  1323. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1324. {
  1325. struct ip6_tnl *t = netdev_priv(dev);
  1326. struct net *net = dev_net(dev);
  1327. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1328. int err = ip6_tnl_dev_init_gen(dev);
  1329. if (err)
  1330. return err;
  1331. t->parms.proto = IPPROTO_IPV6;
  1332. dev_hold(dev);
  1333. ip6_tnl_link_config(t);
  1334. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1335. return 0;
  1336. }
  1337. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1338. {
  1339. u8 proto;
  1340. if (!data)
  1341. return 0;
  1342. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1343. if (proto != IPPROTO_IPV6 &&
  1344. proto != IPPROTO_IPIP &&
  1345. proto != 0)
  1346. return -EINVAL;
  1347. return 0;
  1348. }
  1349. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1350. struct __ip6_tnl_parm *parms)
  1351. {
  1352. memset(parms, 0, sizeof(*parms));
  1353. if (!data)
  1354. return;
  1355. if (data[IFLA_IPTUN_LINK])
  1356. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1357. if (data[IFLA_IPTUN_LOCAL])
  1358. nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
  1359. sizeof(struct in6_addr));
  1360. if (data[IFLA_IPTUN_REMOTE])
  1361. nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
  1362. sizeof(struct in6_addr));
  1363. if (data[IFLA_IPTUN_TTL])
  1364. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1365. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1366. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1367. if (data[IFLA_IPTUN_FLOWINFO])
  1368. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1369. if (data[IFLA_IPTUN_FLAGS])
  1370. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1371. if (data[IFLA_IPTUN_PROTO])
  1372. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1373. }
  1374. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1375. struct nlattr *tb[], struct nlattr *data[])
  1376. {
  1377. struct net *net = dev_net(dev);
  1378. struct ip6_tnl *nt;
  1379. nt = netdev_priv(dev);
  1380. ip6_tnl_netlink_parms(data, &nt->parms);
  1381. if (ip6_tnl_locate(net, &nt->parms, 0))
  1382. return -EEXIST;
  1383. return ip6_tnl_create2(dev);
  1384. }
  1385. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1386. struct nlattr *data[])
  1387. {
  1388. struct ip6_tnl *t = netdev_priv(dev);
  1389. struct __ip6_tnl_parm p;
  1390. struct net *net = t->net;
  1391. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1392. if (dev == ip6n->fb_tnl_dev)
  1393. return -EINVAL;
  1394. ip6_tnl_netlink_parms(data, &p);
  1395. t = ip6_tnl_locate(net, &p, 0);
  1396. if (t) {
  1397. if (t->dev != dev)
  1398. return -EEXIST;
  1399. } else
  1400. t = netdev_priv(dev);
  1401. return ip6_tnl_update(t, &p);
  1402. }
  1403. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1404. {
  1405. struct net *net = dev_net(dev);
  1406. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1407. if (dev != ip6n->fb_tnl_dev)
  1408. unregister_netdevice_queue(dev, head);
  1409. }
  1410. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1411. {
  1412. return
  1413. /* IFLA_IPTUN_LINK */
  1414. nla_total_size(4) +
  1415. /* IFLA_IPTUN_LOCAL */
  1416. nla_total_size(sizeof(struct in6_addr)) +
  1417. /* IFLA_IPTUN_REMOTE */
  1418. nla_total_size(sizeof(struct in6_addr)) +
  1419. /* IFLA_IPTUN_TTL */
  1420. nla_total_size(1) +
  1421. /* IFLA_IPTUN_ENCAP_LIMIT */
  1422. nla_total_size(1) +
  1423. /* IFLA_IPTUN_FLOWINFO */
  1424. nla_total_size(4) +
  1425. /* IFLA_IPTUN_FLAGS */
  1426. nla_total_size(4) +
  1427. /* IFLA_IPTUN_PROTO */
  1428. nla_total_size(1) +
  1429. 0;
  1430. }
  1431. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1432. {
  1433. struct ip6_tnl *tunnel = netdev_priv(dev);
  1434. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1435. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1436. nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
  1437. &parm->laddr) ||
  1438. nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
  1439. &parm->raddr) ||
  1440. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1441. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1442. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1443. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1444. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1445. goto nla_put_failure;
  1446. return 0;
  1447. nla_put_failure:
  1448. return -EMSGSIZE;
  1449. }
  1450. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1451. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1452. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1453. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1454. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1455. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1456. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1457. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1458. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1459. };
  1460. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1461. .kind = "ip6tnl",
  1462. .maxtype = IFLA_IPTUN_MAX,
  1463. .policy = ip6_tnl_policy,
  1464. .priv_size = sizeof(struct ip6_tnl),
  1465. .setup = ip6_tnl_dev_setup,
  1466. .validate = ip6_tnl_validate,
  1467. .newlink = ip6_tnl_newlink,
  1468. .changelink = ip6_tnl_changelink,
  1469. .dellink = ip6_tnl_dellink,
  1470. .get_size = ip6_tnl_get_size,
  1471. .fill_info = ip6_tnl_fill_info,
  1472. };
  1473. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1474. .handler = ip4ip6_rcv,
  1475. .err_handler = ip4ip6_err,
  1476. .priority = 1,
  1477. };
  1478. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1479. .handler = ip6ip6_rcv,
  1480. .err_handler = ip6ip6_err,
  1481. .priority = 1,
  1482. };
  1483. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
  1484. {
  1485. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1486. struct net_device *dev, *aux;
  1487. int h;
  1488. struct ip6_tnl *t;
  1489. LIST_HEAD(list);
  1490. for_each_netdev_safe(net, dev, aux)
  1491. if (dev->rtnl_link_ops == &ip6_link_ops)
  1492. unregister_netdevice_queue(dev, &list);
  1493. for (h = 0; h < HASH_SIZE; h++) {
  1494. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1495. while (t != NULL) {
  1496. /* If dev is in the same netns, it has already
  1497. * been added to the list by the previous loop.
  1498. */
  1499. if (!net_eq(dev_net(t->dev), net))
  1500. unregister_netdevice_queue(t->dev, &list);
  1501. t = rtnl_dereference(t->next);
  1502. }
  1503. }
  1504. unregister_netdevice_many(&list);
  1505. }
  1506. static int __net_init ip6_tnl_init_net(struct net *net)
  1507. {
  1508. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1509. struct ip6_tnl *t = NULL;
  1510. int err;
  1511. ip6n->tnls[0] = ip6n->tnls_wc;
  1512. ip6n->tnls[1] = ip6n->tnls_r_l;
  1513. err = -ENOMEM;
  1514. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1515. ip6_tnl_dev_setup);
  1516. if (!ip6n->fb_tnl_dev)
  1517. goto err_alloc_dev;
  1518. dev_net_set(ip6n->fb_tnl_dev, net);
  1519. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1520. /* FB netdevice is special: we have one, and only one per netns.
  1521. * Allowing to move it to another netns is clearly unsafe.
  1522. */
  1523. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1524. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1525. if (err < 0)
  1526. goto err_register;
  1527. err = register_netdev(ip6n->fb_tnl_dev);
  1528. if (err < 0)
  1529. goto err_register;
  1530. t = netdev_priv(ip6n->fb_tnl_dev);
  1531. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1532. return 0;
  1533. err_register:
  1534. ip6_dev_free(ip6n->fb_tnl_dev);
  1535. err_alloc_dev:
  1536. return err;
  1537. }
  1538. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1539. {
  1540. rtnl_lock();
  1541. ip6_tnl_destroy_tunnels(net);
  1542. rtnl_unlock();
  1543. }
  1544. static struct pernet_operations ip6_tnl_net_ops = {
  1545. .init = ip6_tnl_init_net,
  1546. .exit = ip6_tnl_exit_net,
  1547. .id = &ip6_tnl_net_id,
  1548. .size = sizeof(struct ip6_tnl_net),
  1549. };
  1550. /**
  1551. * ip6_tunnel_init - register protocol and reserve needed resources
  1552. *
  1553. * Return: 0 on success
  1554. **/
  1555. static int __init ip6_tunnel_init(void)
  1556. {
  1557. int err;
  1558. err = register_pernet_device(&ip6_tnl_net_ops);
  1559. if (err < 0)
  1560. goto out_pernet;
  1561. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1562. if (err < 0) {
  1563. pr_err("%s: can't register ip4ip6\n", __func__);
  1564. goto out_ip4ip6;
  1565. }
  1566. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1567. if (err < 0) {
  1568. pr_err("%s: can't register ip6ip6\n", __func__);
  1569. goto out_ip6ip6;
  1570. }
  1571. err = rtnl_link_register(&ip6_link_ops);
  1572. if (err < 0)
  1573. goto rtnl_link_failed;
  1574. return 0;
  1575. rtnl_link_failed:
  1576. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1577. out_ip6ip6:
  1578. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1579. out_ip4ip6:
  1580. unregister_pernet_device(&ip6_tnl_net_ops);
  1581. out_pernet:
  1582. return err;
  1583. }
  1584. /**
  1585. * ip6_tunnel_cleanup - free resources and unregister protocol
  1586. **/
  1587. static void __exit ip6_tunnel_cleanup(void)
  1588. {
  1589. rtnl_link_unregister(&ip6_link_ops);
  1590. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1591. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1592. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1593. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1594. unregister_pernet_device(&ip6_tnl_net_ops);
  1595. }
  1596. module_init(ip6_tunnel_init);
  1597. module_exit(ip6_tunnel_cleanup);