ip6_tunnel.c 39 KB

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