ip6_tunnel.c 46 KB

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