ip6_tunnel.c 43 KB

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