ip6_tunnel.c 37 KB

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