ip6_tunnel.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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 bool log_ecn_error = true;
  69. module_param(log_ecn_error, bool, 0644);
  70. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  71. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  72. {
  73. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  74. return hash_32(hash, HASH_SIZE_SHIFT);
  75. }
  76. static int ip6_tnl_dev_init(struct net_device *dev);
  77. static void ip6_tnl_dev_setup(struct net_device *dev);
  78. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  79. static int ip6_tnl_net_id __read_mostly;
  80. struct ip6_tnl_net {
  81. /* the IPv6 tunnel fallback device */
  82. struct net_device *fb_tnl_dev;
  83. /* lists for storing tunnels in use */
  84. struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
  85. struct ip6_tnl __rcu *tnls_wc[1];
  86. struct ip6_tnl __rcu **tnls[2];
  87. };
  88. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  89. {
  90. struct pcpu_tstats sum = { 0 };
  91. int i;
  92. for_each_possible_cpu(i) {
  93. const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
  94. sum.rx_packets += tstats->rx_packets;
  95. sum.rx_bytes += tstats->rx_bytes;
  96. sum.tx_packets += tstats->tx_packets;
  97. sum.tx_bytes += tstats->tx_bytes;
  98. }
  99. dev->stats.rx_packets = sum.rx_packets;
  100. dev->stats.rx_bytes = sum.rx_bytes;
  101. dev->stats.tx_packets = sum.tx_packets;
  102. dev->stats.tx_bytes = sum.tx_bytes;
  103. return &dev->stats;
  104. }
  105. /*
  106. * Locking : hash tables are protected by RCU and RTNL
  107. */
  108. struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  109. {
  110. struct dst_entry *dst = t->dst_cache;
  111. if (dst && dst->obsolete &&
  112. dst->ops->check(dst, t->dst_cookie) == NULL) {
  113. t->dst_cache = NULL;
  114. dst_release(dst);
  115. return NULL;
  116. }
  117. return dst;
  118. }
  119. EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
  120. void ip6_tnl_dst_reset(struct ip6_tnl *t)
  121. {
  122. dst_release(t->dst_cache);
  123. t->dst_cache = NULL;
  124. }
  125. EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
  126. void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  127. {
  128. struct rt6_info *rt = (struct rt6_info *) dst;
  129. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  130. dst_release(t->dst_cache);
  131. t->dst_cache = dst;
  132. }
  133. EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
  134. /**
  135. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  136. * @remote: the address of the tunnel exit-point
  137. * @local: the address of the tunnel entry-point
  138. *
  139. * Return:
  140. * tunnel matching given end-points if found,
  141. * else fallback tunnel if its device is up,
  142. * else %NULL
  143. **/
  144. #define for_each_ip6_tunnel_rcu(start) \
  145. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  146. static struct ip6_tnl *
  147. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  148. {
  149. unsigned int hash = HASH(remote, local);
  150. struct ip6_tnl *t;
  151. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  152. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  153. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  154. ipv6_addr_equal(remote, &t->parms.raddr) &&
  155. (t->dev->flags & IFF_UP))
  156. return t;
  157. }
  158. t = rcu_dereference(ip6n->tnls_wc[0]);
  159. if (t && (t->dev->flags & IFF_UP))
  160. return t;
  161. return NULL;
  162. }
  163. /**
  164. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  165. * @p: parameters containing tunnel end-points
  166. *
  167. * Description:
  168. * ip6_tnl_bucket() returns the head of the list matching the
  169. * &struct in6_addr entries laddr and raddr in @p.
  170. *
  171. * Return: head of IPv6 tunnel list
  172. **/
  173. static struct ip6_tnl __rcu **
  174. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  175. {
  176. const struct in6_addr *remote = &p->raddr;
  177. const struct in6_addr *local = &p->laddr;
  178. unsigned int h = 0;
  179. int prio = 0;
  180. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  181. prio = 1;
  182. h = HASH(remote, local);
  183. }
  184. return &ip6n->tnls[prio][h];
  185. }
  186. /**
  187. * ip6_tnl_link - add tunnel to hash table
  188. * @t: tunnel to be added
  189. **/
  190. static void
  191. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  192. {
  193. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  194. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  195. rcu_assign_pointer(*tp, t);
  196. }
  197. /**
  198. * ip6_tnl_unlink - remove tunnel from hash table
  199. * @t: tunnel to be removed
  200. **/
  201. static void
  202. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  203. {
  204. struct ip6_tnl __rcu **tp;
  205. struct ip6_tnl *iter;
  206. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  207. (iter = rtnl_dereference(*tp)) != NULL;
  208. tp = &iter->next) {
  209. if (t == iter) {
  210. rcu_assign_pointer(*tp, t->next);
  211. break;
  212. }
  213. }
  214. }
  215. static void ip6_dev_free(struct net_device *dev)
  216. {
  217. free_percpu(dev->tstats);
  218. free_netdev(dev);
  219. }
  220. static int ip6_tnl_create2(struct net_device *dev)
  221. {
  222. struct ip6_tnl *t = netdev_priv(dev);
  223. struct net *net = dev_net(dev);
  224. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  225. int err;
  226. t = netdev_priv(dev);
  227. err = ip6_tnl_dev_init(dev);
  228. if (err < 0)
  229. goto out;
  230. err = register_netdevice(dev);
  231. if (err < 0)
  232. goto out;
  233. strcpy(t->parms.name, dev->name);
  234. dev->rtnl_link_ops = &ip6_link_ops;
  235. dev_hold(dev);
  236. ip6_tnl_link(ip6n, t);
  237. return 0;
  238. out:
  239. return err;
  240. }
  241. /**
  242. * ip6_tnl_create - create a new tunnel
  243. * @p: tunnel parameters
  244. * @pt: pointer to new tunnel
  245. *
  246. * Description:
  247. * Create tunnel matching given parameters.
  248. *
  249. * Return:
  250. * created tunnel or NULL
  251. **/
  252. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  253. {
  254. struct net_device *dev;
  255. struct ip6_tnl *t;
  256. char name[IFNAMSIZ];
  257. int err;
  258. if (p->name[0])
  259. strlcpy(name, p->name, IFNAMSIZ);
  260. else
  261. sprintf(name, "ip6tnl%%d");
  262. dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
  263. if (dev == NULL)
  264. goto failed;
  265. dev_net_set(dev, net);
  266. t = netdev_priv(dev);
  267. t->parms = *p;
  268. err = ip6_tnl_create2(dev);
  269. if (err < 0)
  270. goto failed_free;
  271. return t;
  272. failed_free:
  273. ip6_dev_free(dev);
  274. failed:
  275. return NULL;
  276. }
  277. /**
  278. * ip6_tnl_locate - find or create tunnel matching given parameters
  279. * @p: tunnel parameters
  280. * @create: != 0 if allowed to create new tunnel if no match found
  281. *
  282. * Description:
  283. * ip6_tnl_locate() first tries to locate an existing tunnel
  284. * based on @parms. If this is unsuccessful, but @create is set a new
  285. * tunnel device is created and registered for use.
  286. *
  287. * Return:
  288. * matching tunnel or NULL
  289. **/
  290. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  291. struct __ip6_tnl_parm *p, int create)
  292. {
  293. const struct in6_addr *remote = &p->raddr;
  294. const struct in6_addr *local = &p->laddr;
  295. struct ip6_tnl __rcu **tp;
  296. struct ip6_tnl *t;
  297. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  298. for (tp = ip6_tnl_bucket(ip6n, p);
  299. (t = rtnl_dereference(*tp)) != NULL;
  300. tp = &t->next) {
  301. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  302. ipv6_addr_equal(remote, &t->parms.raddr))
  303. return t;
  304. }
  305. if (!create)
  306. return NULL;
  307. return ip6_tnl_create(net, p);
  308. }
  309. /**
  310. * ip6_tnl_dev_uninit - tunnel device uninitializer
  311. * @dev: the device to be destroyed
  312. *
  313. * Description:
  314. * ip6_tnl_dev_uninit() removes tunnel from its list
  315. **/
  316. static void
  317. ip6_tnl_dev_uninit(struct net_device *dev)
  318. {
  319. struct ip6_tnl *t = netdev_priv(dev);
  320. struct net *net = dev_net(dev);
  321. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  322. if (dev == ip6n->fb_tnl_dev)
  323. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  324. else
  325. ip6_tnl_unlink(ip6n, t);
  326. ip6_tnl_dst_reset(t);
  327. dev_put(dev);
  328. }
  329. /**
  330. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  331. * @skb: received socket buffer
  332. *
  333. * Return:
  334. * 0 if none was found,
  335. * else index to encapsulation limit
  336. **/
  337. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  338. {
  339. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
  340. __u8 nexthdr = ipv6h->nexthdr;
  341. __u16 off = sizeof (*ipv6h);
  342. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  343. __u16 optlen = 0;
  344. struct ipv6_opt_hdr *hdr;
  345. if (raw + off + sizeof (*hdr) > skb->data &&
  346. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  347. break;
  348. hdr = (struct ipv6_opt_hdr *) (raw + off);
  349. if (nexthdr == NEXTHDR_FRAGMENT) {
  350. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  351. if (frag_hdr->frag_off)
  352. break;
  353. optlen = 8;
  354. } else if (nexthdr == NEXTHDR_AUTH) {
  355. optlen = (hdr->hdrlen + 2) << 2;
  356. } else {
  357. optlen = ipv6_optlen(hdr);
  358. }
  359. if (nexthdr == NEXTHDR_DEST) {
  360. __u16 i = off + 2;
  361. while (1) {
  362. struct ipv6_tlv_tnl_enc_lim *tel;
  363. /* No more room for encapsulation limit */
  364. if (i + sizeof (*tel) > off + optlen)
  365. break;
  366. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  367. /* return index of option if found and valid */
  368. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  369. tel->length == 1)
  370. return i;
  371. /* else jump to next option */
  372. if (tel->type)
  373. i += tel->length + 2;
  374. else
  375. i++;
  376. }
  377. }
  378. nexthdr = hdr->nexthdr;
  379. off += optlen;
  380. }
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  384. /**
  385. * ip6_tnl_err - tunnel error handler
  386. *
  387. * Description:
  388. * ip6_tnl_err() should handle errors in the tunnel according
  389. * to the specifications in RFC 2473.
  390. **/
  391. static int
  392. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  393. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  394. {
  395. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
  396. struct ip6_tnl *t;
  397. int rel_msg = 0;
  398. u8 rel_type = ICMPV6_DEST_UNREACH;
  399. u8 rel_code = ICMPV6_ADDR_UNREACH;
  400. __u32 rel_info = 0;
  401. __u16 len;
  402. int err = -ENOENT;
  403. /* If the packet doesn't contain the original IPv6 header we are
  404. in trouble since we might need the source address for further
  405. processing of the error. */
  406. rcu_read_lock();
  407. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
  408. &ipv6h->saddr)) == NULL)
  409. goto out;
  410. if (t->parms.proto != ipproto && t->parms.proto != 0)
  411. goto out;
  412. err = 0;
  413. switch (*type) {
  414. __u32 teli;
  415. struct ipv6_tlv_tnl_enc_lim *tel;
  416. __u32 mtu;
  417. case ICMPV6_DEST_UNREACH:
  418. net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
  419. t->parms.name);
  420. rel_msg = 1;
  421. break;
  422. case ICMPV6_TIME_EXCEED:
  423. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  424. net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  425. t->parms.name);
  426. rel_msg = 1;
  427. }
  428. break;
  429. case ICMPV6_PARAMPROB:
  430. teli = 0;
  431. if ((*code) == ICMPV6_HDR_FIELD)
  432. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  433. if (teli && teli == *info - 2) {
  434. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  435. if (tel->encap_limit == 0) {
  436. net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  437. t->parms.name);
  438. rel_msg = 1;
  439. }
  440. } else {
  441. net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  442. t->parms.name);
  443. }
  444. break;
  445. case ICMPV6_PKT_TOOBIG:
  446. mtu = *info - offset;
  447. if (mtu < IPV6_MIN_MTU)
  448. mtu = IPV6_MIN_MTU;
  449. t->dev->mtu = mtu;
  450. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  451. rel_type = ICMPV6_PKT_TOOBIG;
  452. rel_code = 0;
  453. rel_info = mtu;
  454. rel_msg = 1;
  455. }
  456. break;
  457. }
  458. *type = rel_type;
  459. *code = rel_code;
  460. *info = rel_info;
  461. *msg = rel_msg;
  462. out:
  463. rcu_read_unlock();
  464. return err;
  465. }
  466. static int
  467. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  468. u8 type, u8 code, int offset, __be32 info)
  469. {
  470. int rel_msg = 0;
  471. u8 rel_type = type;
  472. u8 rel_code = code;
  473. __u32 rel_info = ntohl(info);
  474. int err;
  475. struct sk_buff *skb2;
  476. const struct iphdr *eiph;
  477. struct rtable *rt;
  478. struct flowi4 fl4;
  479. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  480. &rel_msg, &rel_info, offset);
  481. if (err < 0)
  482. return err;
  483. if (rel_msg == 0)
  484. return 0;
  485. switch (rel_type) {
  486. case ICMPV6_DEST_UNREACH:
  487. if (rel_code != ICMPV6_ADDR_UNREACH)
  488. return 0;
  489. rel_type = ICMP_DEST_UNREACH;
  490. rel_code = ICMP_HOST_UNREACH;
  491. break;
  492. case ICMPV6_PKT_TOOBIG:
  493. if (rel_code != 0)
  494. return 0;
  495. rel_type = ICMP_DEST_UNREACH;
  496. rel_code = ICMP_FRAG_NEEDED;
  497. break;
  498. case NDISC_REDIRECT:
  499. rel_type = ICMP_REDIRECT;
  500. rel_code = ICMP_REDIR_HOST;
  501. default:
  502. return 0;
  503. }
  504. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  505. return 0;
  506. skb2 = skb_clone(skb, GFP_ATOMIC);
  507. if (!skb2)
  508. return 0;
  509. skb_dst_drop(skb2);
  510. skb_pull(skb2, offset);
  511. skb_reset_network_header(skb2);
  512. eiph = ip_hdr(skb2);
  513. /* Try to guess incoming interface */
  514. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  515. eiph->saddr, 0,
  516. 0, 0,
  517. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  518. if (IS_ERR(rt))
  519. goto out;
  520. skb2->dev = rt->dst.dev;
  521. /* route "incoming" packet */
  522. if (rt->rt_flags & RTCF_LOCAL) {
  523. ip_rt_put(rt);
  524. rt = NULL;
  525. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  526. eiph->daddr, eiph->saddr,
  527. 0, 0,
  528. IPPROTO_IPIP,
  529. RT_TOS(eiph->tos), 0);
  530. if (IS_ERR(rt) ||
  531. rt->dst.dev->type != ARPHRD_TUNNEL) {
  532. if (!IS_ERR(rt))
  533. ip_rt_put(rt);
  534. goto out;
  535. }
  536. skb_dst_set(skb2, &rt->dst);
  537. } else {
  538. ip_rt_put(rt);
  539. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  540. skb2->dev) ||
  541. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  542. goto out;
  543. }
  544. /* change mtu on this route */
  545. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  546. if (rel_info > dst_mtu(skb_dst(skb2)))
  547. goto out;
  548. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
  549. }
  550. if (rel_type == ICMP_REDIRECT)
  551. skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
  552. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  553. out:
  554. kfree_skb(skb2);
  555. return 0;
  556. }
  557. static int
  558. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  559. u8 type, u8 code, int offset, __be32 info)
  560. {
  561. int rel_msg = 0;
  562. u8 rel_type = type;
  563. u8 rel_code = code;
  564. __u32 rel_info = ntohl(info);
  565. int err;
  566. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  567. &rel_msg, &rel_info, offset);
  568. if (err < 0)
  569. return err;
  570. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  571. struct rt6_info *rt;
  572. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  573. if (!skb2)
  574. return 0;
  575. skb_dst_drop(skb2);
  576. skb_pull(skb2, offset);
  577. skb_reset_network_header(skb2);
  578. /* Try to guess incoming interface */
  579. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  580. NULL, 0, 0);
  581. if (rt && rt->dst.dev)
  582. skb2->dev = rt->dst.dev;
  583. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  584. ip6_rt_put(rt);
  585. kfree_skb(skb2);
  586. }
  587. return 0;
  588. }
  589. static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  590. const struct ipv6hdr *ipv6h,
  591. struct sk_buff *skb)
  592. {
  593. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  594. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  595. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  596. return IP6_ECN_decapsulate(ipv6h, skb);
  597. }
  598. static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  599. const struct ipv6hdr *ipv6h,
  600. struct sk_buff *skb)
  601. {
  602. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  603. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  604. return IP6_ECN_decapsulate(ipv6h, skb);
  605. }
  606. __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
  607. const struct in6_addr *laddr,
  608. const struct in6_addr *raddr)
  609. {
  610. struct __ip6_tnl_parm *p = &t->parms;
  611. int ltype = ipv6_addr_type(laddr);
  612. int rtype = ipv6_addr_type(raddr);
  613. __u32 flags = 0;
  614. if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
  615. flags = IP6_TNL_F_CAP_PER_PACKET;
  616. } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  617. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  618. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  619. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  620. if (ltype&IPV6_ADDR_UNICAST)
  621. flags |= IP6_TNL_F_CAP_XMIT;
  622. if (rtype&IPV6_ADDR_UNICAST)
  623. flags |= IP6_TNL_F_CAP_RCV;
  624. }
  625. return flags;
  626. }
  627. EXPORT_SYMBOL(ip6_tnl_get_cap);
  628. /* called with rcu_read_lock() */
  629. int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
  630. const struct in6_addr *laddr,
  631. const struct in6_addr *raddr)
  632. {
  633. struct __ip6_tnl_parm *p = &t->parms;
  634. int ret = 0;
  635. struct net *net = dev_net(t->dev);
  636. if ((p->flags & IP6_TNL_F_CAP_RCV) ||
  637. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  638. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
  639. struct net_device *ldev = NULL;
  640. if (p->link)
  641. ldev = dev_get_by_index_rcu(net, p->link);
  642. if ((ipv6_addr_is_multicast(laddr) ||
  643. likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
  644. likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
  645. ret = 1;
  646. }
  647. return ret;
  648. }
  649. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  650. /**
  651. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  652. * @skb: received socket buffer
  653. * @protocol: ethernet protocol ID
  654. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  655. *
  656. * Return: 0
  657. **/
  658. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  659. __u8 ipproto,
  660. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  661. const struct ipv6hdr *ipv6h,
  662. struct sk_buff *skb))
  663. {
  664. struct ip6_tnl *t;
  665. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  666. int err;
  667. rcu_read_lock();
  668. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  669. &ipv6h->daddr)) != NULL) {
  670. struct pcpu_tstats *tstats;
  671. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  672. rcu_read_unlock();
  673. goto discard;
  674. }
  675. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  676. rcu_read_unlock();
  677. goto discard;
  678. }
  679. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  680. t->dev->stats.rx_dropped++;
  681. rcu_read_unlock();
  682. goto discard;
  683. }
  684. secpath_reset(skb);
  685. skb->mac_header = skb->network_header;
  686. skb_reset_network_header(skb);
  687. skb->protocol = htons(protocol);
  688. skb->pkt_type = PACKET_HOST;
  689. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  690. __skb_tunnel_rx(skb, t->dev);
  691. err = dscp_ecn_decapsulate(t, ipv6h, skb);
  692. if (unlikely(err)) {
  693. if (log_ecn_error)
  694. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  695. &ipv6h->saddr,
  696. ipv6_get_dsfield(ipv6h));
  697. if (err > 1) {
  698. ++t->dev->stats.rx_frame_errors;
  699. ++t->dev->stats.rx_errors;
  700. rcu_read_unlock();
  701. goto discard;
  702. }
  703. }
  704. tstats = this_cpu_ptr(t->dev->tstats);
  705. tstats->rx_packets++;
  706. tstats->rx_bytes += skb->len;
  707. netif_rx(skb);
  708. rcu_read_unlock();
  709. return 0;
  710. }
  711. rcu_read_unlock();
  712. return 1;
  713. discard:
  714. kfree_skb(skb);
  715. return 0;
  716. }
  717. static int ip4ip6_rcv(struct sk_buff *skb)
  718. {
  719. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  720. ip4ip6_dscp_ecn_decapsulate);
  721. }
  722. static int ip6ip6_rcv(struct sk_buff *skb)
  723. {
  724. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  725. ip6ip6_dscp_ecn_decapsulate);
  726. }
  727. struct ipv6_tel_txoption {
  728. struct ipv6_txoptions ops;
  729. __u8 dst_opt[8];
  730. };
  731. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  732. {
  733. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  734. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  735. opt->dst_opt[3] = 1;
  736. opt->dst_opt[4] = encap_limit;
  737. opt->dst_opt[5] = IPV6_TLV_PADN;
  738. opt->dst_opt[6] = 1;
  739. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  740. opt->ops.opt_nflen = 8;
  741. }
  742. /**
  743. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  744. * @t: the outgoing tunnel device
  745. * @hdr: IPv6 header from the incoming packet
  746. *
  747. * Description:
  748. * Avoid trivial tunneling loop by checking that tunnel exit-point
  749. * doesn't match source of incoming packet.
  750. *
  751. * Return:
  752. * 1 if conflict,
  753. * 0 else
  754. **/
  755. static inline bool
  756. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  757. {
  758. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  759. }
  760. int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  761. {
  762. struct __ip6_tnl_parm *p = &t->parms;
  763. int ret = 0;
  764. struct net *net = dev_net(t->dev);
  765. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  766. struct net_device *ldev = NULL;
  767. rcu_read_lock();
  768. if (p->link)
  769. ldev = dev_get_by_index_rcu(net, p->link);
  770. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  771. pr_warn("%s xmit: Local address not yet configured!\n",
  772. p->name);
  773. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  774. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  775. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  776. p->name);
  777. else
  778. ret = 1;
  779. rcu_read_unlock();
  780. }
  781. return ret;
  782. }
  783. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  784. /**
  785. * ip6_tnl_xmit2 - encapsulate packet and send
  786. * @skb: the outgoing socket buffer
  787. * @dev: the outgoing tunnel device
  788. * @dsfield: dscp code for outer header
  789. * @fl: flow of tunneled packet
  790. * @encap_limit: encapsulation limit
  791. * @pmtu: Path MTU is stored if packet is too big
  792. *
  793. * Description:
  794. * Build new header and do some sanity checks on the packet before sending
  795. * it.
  796. *
  797. * Return:
  798. * 0 on success
  799. * -1 fail
  800. * %-EMSGSIZE message too big. return mtu in this case.
  801. **/
  802. static int ip6_tnl_xmit2(struct sk_buff *skb,
  803. struct net_device *dev,
  804. __u8 dsfield,
  805. struct flowi6 *fl6,
  806. int encap_limit,
  807. __u32 *pmtu)
  808. {
  809. struct net *net = dev_net(dev);
  810. struct ip6_tnl *t = netdev_priv(dev);
  811. struct net_device_stats *stats = &t->dev->stats;
  812. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  813. struct ipv6_tel_txoption opt;
  814. struct dst_entry *dst = NULL, *ndst = NULL;
  815. struct net_device *tdev;
  816. int mtu;
  817. unsigned int max_headroom = sizeof(struct ipv6hdr);
  818. u8 proto;
  819. int err = -1;
  820. int pkt_len;
  821. if (!fl6->flowi6_mark)
  822. dst = ip6_tnl_dst_check(t);
  823. if (!dst) {
  824. ndst = ip6_route_output(net, NULL, fl6);
  825. if (ndst->error)
  826. goto tx_err_link_failure;
  827. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  828. if (IS_ERR(ndst)) {
  829. err = PTR_ERR(ndst);
  830. ndst = NULL;
  831. goto tx_err_link_failure;
  832. }
  833. dst = ndst;
  834. }
  835. tdev = dst->dev;
  836. if (tdev == dev) {
  837. stats->collisions++;
  838. net_warn_ratelimited("%s: Local routing loop detected!\n",
  839. t->parms.name);
  840. goto tx_err_dst_release;
  841. }
  842. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  843. if (encap_limit >= 0) {
  844. max_headroom += 8;
  845. mtu -= 8;
  846. }
  847. if (mtu < IPV6_MIN_MTU)
  848. mtu = IPV6_MIN_MTU;
  849. if (skb_dst(skb))
  850. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  851. if (skb->len > mtu) {
  852. *pmtu = mtu;
  853. err = -EMSGSIZE;
  854. goto tx_err_dst_release;
  855. }
  856. /*
  857. * Okay, now see if we can stuff it in the buffer as-is.
  858. */
  859. max_headroom += LL_RESERVED_SPACE(tdev);
  860. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  861. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  862. struct sk_buff *new_skb;
  863. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  864. goto tx_err_dst_release;
  865. if (skb->sk)
  866. skb_set_owner_w(new_skb, skb->sk);
  867. consume_skb(skb);
  868. skb = new_skb;
  869. }
  870. skb_dst_drop(skb);
  871. if (fl6->flowi6_mark) {
  872. skb_dst_set(skb, dst);
  873. ndst = NULL;
  874. } else {
  875. skb_dst_set_noref(skb, dst);
  876. }
  877. skb->transport_header = skb->network_header;
  878. proto = fl6->flowi6_proto;
  879. if (encap_limit >= 0) {
  880. init_tel_txopt(&opt, encap_limit);
  881. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  882. }
  883. skb_push(skb, sizeof(struct ipv6hdr));
  884. skb_reset_network_header(skb);
  885. ipv6h = ipv6_hdr(skb);
  886. *(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
  887. dsfield = INET_ECN_encapsulate(0, dsfield);
  888. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  889. ipv6h->hop_limit = t->parms.hop_limit;
  890. ipv6h->nexthdr = proto;
  891. ipv6h->saddr = fl6->saddr;
  892. ipv6h->daddr = fl6->daddr;
  893. nf_reset(skb);
  894. pkt_len = skb->len;
  895. err = ip6_local_out(skb);
  896. if (net_xmit_eval(err) == 0) {
  897. struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);
  898. tstats->tx_bytes += pkt_len;
  899. tstats->tx_packets++;
  900. } else {
  901. stats->tx_errors++;
  902. stats->tx_aborted_errors++;
  903. }
  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 |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  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(dev_net(dev),
  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 = dev_net(t->dev);
  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 = NULL;
  1153. struct net *net = dev_net(dev);
  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. } else {
  1165. memset(&p, 0, sizeof(p));
  1166. }
  1167. if (t == NULL)
  1168. t = netdev_priv(dev);
  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. if (new_mtu < IPV6_MIN_MTU) {
  1245. return -EINVAL;
  1246. }
  1247. dev->mtu = new_mtu;
  1248. return 0;
  1249. }
  1250. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1251. .ndo_uninit = ip6_tnl_dev_uninit,
  1252. .ndo_start_xmit = ip6_tnl_xmit,
  1253. .ndo_do_ioctl = ip6_tnl_ioctl,
  1254. .ndo_change_mtu = ip6_tnl_change_mtu,
  1255. .ndo_get_stats = ip6_get_stats,
  1256. };
  1257. /**
  1258. * ip6_tnl_dev_setup - setup virtual tunnel device
  1259. * @dev: virtual device associated with tunnel
  1260. *
  1261. * Description:
  1262. * Initialize function pointers and device parameters
  1263. **/
  1264. static void ip6_tnl_dev_setup(struct net_device *dev)
  1265. {
  1266. struct ip6_tnl *t;
  1267. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1268. dev->destructor = ip6_dev_free;
  1269. dev->type = ARPHRD_TUNNEL6;
  1270. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1271. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1272. t = netdev_priv(dev);
  1273. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1274. dev->mtu-=8;
  1275. dev->flags |= IFF_NOARP;
  1276. dev->addr_len = sizeof(struct in6_addr);
  1277. dev->features |= NETIF_F_NETNS_LOCAL;
  1278. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1279. }
  1280. /**
  1281. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1282. * @dev: virtual device associated with tunnel
  1283. **/
  1284. static inline int
  1285. ip6_tnl_dev_init_gen(struct net_device *dev)
  1286. {
  1287. struct ip6_tnl *t = netdev_priv(dev);
  1288. t->dev = dev;
  1289. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1290. if (!dev->tstats)
  1291. return -ENOMEM;
  1292. return 0;
  1293. }
  1294. /**
  1295. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1296. * @dev: virtual device associated with tunnel
  1297. **/
  1298. static int ip6_tnl_dev_init(struct net_device *dev)
  1299. {
  1300. struct ip6_tnl *t = netdev_priv(dev);
  1301. int err = ip6_tnl_dev_init_gen(dev);
  1302. if (err)
  1303. return err;
  1304. ip6_tnl_link_config(t);
  1305. return 0;
  1306. }
  1307. /**
  1308. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1309. * @dev: fallback device
  1310. *
  1311. * Return: 0
  1312. **/
  1313. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1314. {
  1315. struct ip6_tnl *t = netdev_priv(dev);
  1316. struct net *net = dev_net(dev);
  1317. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1318. int err = ip6_tnl_dev_init_gen(dev);
  1319. if (err)
  1320. return err;
  1321. t->parms.proto = IPPROTO_IPV6;
  1322. dev_hold(dev);
  1323. ip6_tnl_link_config(t);
  1324. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1325. return 0;
  1326. }
  1327. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1328. {
  1329. u8 proto;
  1330. if (!data)
  1331. return 0;
  1332. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1333. if (proto != IPPROTO_IPV6 &&
  1334. proto != IPPROTO_IPIP &&
  1335. proto != 0)
  1336. return -EINVAL;
  1337. return 0;
  1338. }
  1339. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1340. struct __ip6_tnl_parm *parms)
  1341. {
  1342. memset(parms, 0, sizeof(*parms));
  1343. if (!data)
  1344. return;
  1345. if (data[IFLA_IPTUN_LINK])
  1346. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1347. if (data[IFLA_IPTUN_LOCAL])
  1348. nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
  1349. sizeof(struct in6_addr));
  1350. if (data[IFLA_IPTUN_REMOTE])
  1351. nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
  1352. sizeof(struct in6_addr));
  1353. if (data[IFLA_IPTUN_TTL])
  1354. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1355. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1356. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1357. if (data[IFLA_IPTUN_FLOWINFO])
  1358. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1359. if (data[IFLA_IPTUN_FLAGS])
  1360. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1361. if (data[IFLA_IPTUN_PROTO])
  1362. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1363. }
  1364. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1365. struct nlattr *tb[], struct nlattr *data[])
  1366. {
  1367. struct net *net = dev_net(dev);
  1368. struct ip6_tnl *nt;
  1369. nt = netdev_priv(dev);
  1370. ip6_tnl_netlink_parms(data, &nt->parms);
  1371. if (ip6_tnl_locate(net, &nt->parms, 0))
  1372. return -EEXIST;
  1373. return ip6_tnl_create2(dev);
  1374. }
  1375. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1376. struct nlattr *data[])
  1377. {
  1378. struct ip6_tnl *t;
  1379. struct __ip6_tnl_parm p;
  1380. struct net *net = dev_net(dev);
  1381. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1382. if (dev == ip6n->fb_tnl_dev)
  1383. return -EINVAL;
  1384. ip6_tnl_netlink_parms(data, &p);
  1385. t = ip6_tnl_locate(net, &p, 0);
  1386. if (t) {
  1387. if (t->dev != dev)
  1388. return -EEXIST;
  1389. } else
  1390. t = netdev_priv(dev);
  1391. return ip6_tnl_update(t, &p);
  1392. }
  1393. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1394. {
  1395. return
  1396. /* IFLA_IPTUN_LINK */
  1397. nla_total_size(4) +
  1398. /* IFLA_IPTUN_LOCAL */
  1399. nla_total_size(sizeof(struct in6_addr)) +
  1400. /* IFLA_IPTUN_REMOTE */
  1401. nla_total_size(sizeof(struct in6_addr)) +
  1402. /* IFLA_IPTUN_TTL */
  1403. nla_total_size(1) +
  1404. /* IFLA_IPTUN_ENCAP_LIMIT */
  1405. nla_total_size(1) +
  1406. /* IFLA_IPTUN_FLOWINFO */
  1407. nla_total_size(4) +
  1408. /* IFLA_IPTUN_FLAGS */
  1409. nla_total_size(4) +
  1410. /* IFLA_IPTUN_PROTO */
  1411. nla_total_size(1) +
  1412. 0;
  1413. }
  1414. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1415. {
  1416. struct ip6_tnl *tunnel = netdev_priv(dev);
  1417. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1418. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1419. nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
  1420. &parm->raddr) ||
  1421. nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
  1422. &parm->laddr) ||
  1423. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1424. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1425. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1426. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1427. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1428. goto nla_put_failure;
  1429. return 0;
  1430. nla_put_failure:
  1431. return -EMSGSIZE;
  1432. }
  1433. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1434. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1435. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1436. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1437. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1438. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1439. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1440. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1441. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1442. };
  1443. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1444. .kind = "ip6tnl",
  1445. .maxtype = IFLA_IPTUN_MAX,
  1446. .policy = ip6_tnl_policy,
  1447. .priv_size = sizeof(struct ip6_tnl),
  1448. .setup = ip6_tnl_dev_setup,
  1449. .validate = ip6_tnl_validate,
  1450. .newlink = ip6_tnl_newlink,
  1451. .changelink = ip6_tnl_changelink,
  1452. .get_size = ip6_tnl_get_size,
  1453. .fill_info = ip6_tnl_fill_info,
  1454. };
  1455. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1456. .handler = ip4ip6_rcv,
  1457. .err_handler = ip4ip6_err,
  1458. .priority = 1,
  1459. };
  1460. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1461. .handler = ip6ip6_rcv,
  1462. .err_handler = ip6ip6_err,
  1463. .priority = 1,
  1464. };
  1465. static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1466. {
  1467. int h;
  1468. struct ip6_tnl *t;
  1469. LIST_HEAD(list);
  1470. for (h = 0; h < HASH_SIZE; h++) {
  1471. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1472. while (t != NULL) {
  1473. unregister_netdevice_queue(t->dev, &list);
  1474. t = rtnl_dereference(t->next);
  1475. }
  1476. }
  1477. t = rtnl_dereference(ip6n->tnls_wc[0]);
  1478. unregister_netdevice_queue(t->dev, &list);
  1479. unregister_netdevice_many(&list);
  1480. }
  1481. static int __net_init ip6_tnl_init_net(struct net *net)
  1482. {
  1483. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1484. struct ip6_tnl *t = NULL;
  1485. int err;
  1486. ip6n->tnls[0] = ip6n->tnls_wc;
  1487. ip6n->tnls[1] = ip6n->tnls_r_l;
  1488. err = -ENOMEM;
  1489. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1490. ip6_tnl_dev_setup);
  1491. if (!ip6n->fb_tnl_dev)
  1492. goto err_alloc_dev;
  1493. dev_net_set(ip6n->fb_tnl_dev, net);
  1494. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1495. if (err < 0)
  1496. goto err_register;
  1497. err = register_netdev(ip6n->fb_tnl_dev);
  1498. if (err < 0)
  1499. goto err_register;
  1500. t = netdev_priv(ip6n->fb_tnl_dev);
  1501. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1502. return 0;
  1503. err_register:
  1504. ip6_dev_free(ip6n->fb_tnl_dev);
  1505. err_alloc_dev:
  1506. return err;
  1507. }
  1508. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1509. {
  1510. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1511. rtnl_lock();
  1512. ip6_tnl_destroy_tunnels(ip6n);
  1513. rtnl_unlock();
  1514. }
  1515. static struct pernet_operations ip6_tnl_net_ops = {
  1516. .init = ip6_tnl_init_net,
  1517. .exit = ip6_tnl_exit_net,
  1518. .id = &ip6_tnl_net_id,
  1519. .size = sizeof(struct ip6_tnl_net),
  1520. };
  1521. /**
  1522. * ip6_tunnel_init - register protocol and reserve needed resources
  1523. *
  1524. * Return: 0 on success
  1525. **/
  1526. static int __init ip6_tunnel_init(void)
  1527. {
  1528. int err;
  1529. err = register_pernet_device(&ip6_tnl_net_ops);
  1530. if (err < 0)
  1531. goto out_pernet;
  1532. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1533. if (err < 0) {
  1534. pr_err("%s: can't register ip4ip6\n", __func__);
  1535. goto out_ip4ip6;
  1536. }
  1537. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1538. if (err < 0) {
  1539. pr_err("%s: can't register ip6ip6\n", __func__);
  1540. goto out_ip6ip6;
  1541. }
  1542. err = rtnl_link_register(&ip6_link_ops);
  1543. if (err < 0)
  1544. goto rtnl_link_failed;
  1545. return 0;
  1546. rtnl_link_failed:
  1547. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1548. out_ip6ip6:
  1549. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1550. out_ip4ip6:
  1551. unregister_pernet_device(&ip6_tnl_net_ops);
  1552. out_pernet:
  1553. return err;
  1554. }
  1555. /**
  1556. * ip6_tunnel_cleanup - free resources and unregister protocol
  1557. **/
  1558. static void __exit ip6_tunnel_cleanup(void)
  1559. {
  1560. rtnl_link_unregister(&ip6_link_ops);
  1561. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1562. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1563. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1564. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1565. unregister_pernet_device(&ip6_tnl_net_ops);
  1566. }
  1567. module_init(ip6_tunnel_init);
  1568. module_exit(ip6_tunnel_cleanup);