ip6_tunnel.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * IPv6 over IPv6 tunnel device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. *
  8. * $Id$
  9. *
  10. * Based on:
  11. * linux/net/ipv6/sit.c
  12. *
  13. * RFC 2473
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  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/if.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/if_tunnel.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/atomic.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/ip6_route.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip6_tunnel.h>
  46. #include <net/xfrm.h>
  47. #include <net/dsfield.h>
  48. #include <net/inet_ecn.h>
  49. MODULE_AUTHOR("Ville Nuorvala");
  50. MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
  51. MODULE_LICENSE("GPL");
  52. #define IPV6_TLV_TEL_DST_SIZE 8
  53. #ifdef IP6_TNL_DEBUG
  54. #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
  55. #else
  56. #define IP6_TNL_TRACE(x...) do {;} while(0)
  57. #endif
  58. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  59. #define HASH_SIZE 32
  60. #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
  61. (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
  62. (HASH_SIZE - 1))
  63. static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
  64. static int ip6ip6_tnl_dev_init(struct net_device *dev);
  65. static void ip6ip6_tnl_dev_setup(struct net_device *dev);
  66. /* the IPv6 tunnel fallback device */
  67. static struct net_device *ip6ip6_fb_tnl_dev;
  68. /* lists for storing tunnels in use */
  69. static struct ip6_tnl *tnls_r_l[HASH_SIZE];
  70. static struct ip6_tnl *tnls_wc[1];
  71. static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
  72. /* lock for the tunnel lists */
  73. static DEFINE_RWLOCK(ip6ip6_lock);
  74. static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  75. {
  76. struct dst_entry *dst = t->dst_cache;
  77. if (dst && dst->obsolete &&
  78. dst->ops->check(dst, t->dst_cookie) == NULL) {
  79. t->dst_cache = NULL;
  80. dst_release(dst);
  81. return NULL;
  82. }
  83. return dst;
  84. }
  85. static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
  86. {
  87. dst_release(t->dst_cache);
  88. t->dst_cache = NULL;
  89. }
  90. static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  91. {
  92. struct rt6_info *rt = (struct rt6_info *) dst;
  93. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  94. dst_release(t->dst_cache);
  95. t->dst_cache = dst;
  96. }
  97. /**
  98. * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  99. * @remote: the address of the tunnel exit-point
  100. * @local: the address of the tunnel entry-point
  101. *
  102. * Return:
  103. * tunnel matching given end-points if found,
  104. * else fallback tunnel if its device is up,
  105. * else %NULL
  106. **/
  107. static struct ip6_tnl *
  108. ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
  109. {
  110. unsigned h0 = HASH(remote);
  111. unsigned h1 = HASH(local);
  112. struct ip6_tnl *t;
  113. for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
  114. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  115. ipv6_addr_equal(remote, &t->parms.raddr) &&
  116. (t->dev->flags & IFF_UP))
  117. return t;
  118. }
  119. if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
  120. return t;
  121. return NULL;
  122. }
  123. /**
  124. * ip6ip6_bucket - get head of list matching given tunnel parameters
  125. * @p: parameters containing tunnel end-points
  126. *
  127. * Description:
  128. * ip6ip6_bucket() returns the head of the list matching the
  129. * &struct in6_addr entries laddr and raddr in @p.
  130. *
  131. * Return: head of IPv6 tunnel list
  132. **/
  133. static struct ip6_tnl **
  134. ip6ip6_bucket(struct ip6_tnl_parm *p)
  135. {
  136. struct in6_addr *remote = &p->raddr;
  137. struct in6_addr *local = &p->laddr;
  138. unsigned h = 0;
  139. int prio = 0;
  140. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  141. prio = 1;
  142. h = HASH(remote) ^ HASH(local);
  143. }
  144. return &tnls[prio][h];
  145. }
  146. /**
  147. * ip6ip6_tnl_link - add tunnel to hash table
  148. * @t: tunnel to be added
  149. **/
  150. static void
  151. ip6ip6_tnl_link(struct ip6_tnl *t)
  152. {
  153. struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
  154. t->next = *tp;
  155. write_lock_bh(&ip6ip6_lock);
  156. *tp = t;
  157. write_unlock_bh(&ip6ip6_lock);
  158. }
  159. /**
  160. * ip6ip6_tnl_unlink - remove tunnel from hash table
  161. * @t: tunnel to be removed
  162. **/
  163. static void
  164. ip6ip6_tnl_unlink(struct ip6_tnl *t)
  165. {
  166. struct ip6_tnl **tp;
  167. for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
  168. if (t == *tp) {
  169. write_lock_bh(&ip6ip6_lock);
  170. *tp = t->next;
  171. write_unlock_bh(&ip6ip6_lock);
  172. break;
  173. }
  174. }
  175. }
  176. /**
  177. * ip6_tnl_create() - create a new tunnel
  178. * @p: tunnel parameters
  179. * @pt: pointer to new tunnel
  180. *
  181. * Description:
  182. * Create tunnel matching given parameters.
  183. *
  184. * Return:
  185. * created tunnel or NULL
  186. **/
  187. static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
  188. {
  189. struct net_device *dev;
  190. struct ip6_tnl *t;
  191. char name[IFNAMSIZ];
  192. int err;
  193. if (p->name[0]) {
  194. strlcpy(name, p->name, IFNAMSIZ);
  195. } else {
  196. int i;
  197. for (i = 1; i < IP6_TNL_MAX; i++) {
  198. sprintf(name, "ip6tnl%d", i);
  199. if (__dev_get_by_name(name) == NULL)
  200. break;
  201. }
  202. if (i == IP6_TNL_MAX)
  203. goto failed;
  204. }
  205. dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
  206. if (dev == NULL)
  207. goto failed;
  208. t = netdev_priv(dev);
  209. dev->init = ip6ip6_tnl_dev_init;
  210. t->parms = *p;
  211. if ((err = register_netdevice(dev)) < 0) {
  212. free_netdev(dev);
  213. goto failed;
  214. }
  215. dev_hold(dev);
  216. ip6ip6_tnl_link(t);
  217. return t;
  218. failed:
  219. return NULL;
  220. }
  221. /**
  222. * ip6ip6_tnl_locate - find or create tunnel matching given parameters
  223. * @p: tunnel parameters
  224. * @create: != 0 if allowed to create new tunnel if no match found
  225. *
  226. * Description:
  227. * ip6ip6_tnl_locate() first tries to locate an existing tunnel
  228. * based on @parms. If this is unsuccessful, but @create is set a new
  229. * tunnel device is created and registered for use.
  230. *
  231. * Return:
  232. * matching tunnel or NULL
  233. **/
  234. static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
  235. {
  236. struct in6_addr *remote = &p->raddr;
  237. struct in6_addr *local = &p->laddr;
  238. struct ip6_tnl *t;
  239. for (t = *ip6ip6_bucket(p); t; t = t->next) {
  240. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  241. ipv6_addr_equal(remote, &t->parms.raddr))
  242. return t;
  243. }
  244. if (!create)
  245. return NULL;
  246. return ip6_tnl_create(p);
  247. }
  248. /**
  249. * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
  250. * @dev: the device to be destroyed
  251. *
  252. * Description:
  253. * ip6ip6_tnl_dev_uninit() removes tunnel from its list
  254. **/
  255. static void
  256. ip6ip6_tnl_dev_uninit(struct net_device *dev)
  257. {
  258. struct ip6_tnl *t = netdev_priv(dev);
  259. if (dev == ip6ip6_fb_tnl_dev) {
  260. write_lock_bh(&ip6ip6_lock);
  261. tnls_wc[0] = NULL;
  262. write_unlock_bh(&ip6ip6_lock);
  263. } else {
  264. ip6ip6_tnl_unlink(t);
  265. }
  266. ip6_tnl_dst_reset(t);
  267. dev_put(dev);
  268. }
  269. /**
  270. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  271. * @skb: received socket buffer
  272. *
  273. * Return:
  274. * 0 if none was found,
  275. * else index to encapsulation limit
  276. **/
  277. static __u16
  278. parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
  279. {
  280. struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
  281. __u8 nexthdr = ipv6h->nexthdr;
  282. __u16 off = sizeof (*ipv6h);
  283. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  284. __u16 optlen = 0;
  285. struct ipv6_opt_hdr *hdr;
  286. if (raw + off + sizeof (*hdr) > skb->data &&
  287. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  288. break;
  289. hdr = (struct ipv6_opt_hdr *) (raw + off);
  290. if (nexthdr == NEXTHDR_FRAGMENT) {
  291. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  292. if (frag_hdr->frag_off)
  293. break;
  294. optlen = 8;
  295. } else if (nexthdr == NEXTHDR_AUTH) {
  296. optlen = (hdr->hdrlen + 2) << 2;
  297. } else {
  298. optlen = ipv6_optlen(hdr);
  299. }
  300. if (nexthdr == NEXTHDR_DEST) {
  301. __u16 i = off + 2;
  302. while (1) {
  303. struct ipv6_tlv_tnl_enc_lim *tel;
  304. /* No more room for encapsulation limit */
  305. if (i + sizeof (*tel) > off + optlen)
  306. break;
  307. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  308. /* return index of option if found and valid */
  309. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  310. tel->length == 1)
  311. return i;
  312. /* else jump to next option */
  313. if (tel->type)
  314. i += tel->length + 2;
  315. else
  316. i++;
  317. }
  318. }
  319. nexthdr = hdr->nexthdr;
  320. off += optlen;
  321. }
  322. return 0;
  323. }
  324. /**
  325. * ip6ip6_err - tunnel error handler
  326. *
  327. * Description:
  328. * ip6ip6_err() should handle errors in the tunnel according
  329. * to the specifications in RFC 2473.
  330. **/
  331. static int
  332. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  333. int type, int code, int offset, __be32 info)
  334. {
  335. struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
  336. struct ip6_tnl *t;
  337. int rel_msg = 0;
  338. int rel_type = ICMPV6_DEST_UNREACH;
  339. int rel_code = ICMPV6_ADDR_UNREACH;
  340. __u32 rel_info = 0;
  341. __u16 len;
  342. int err = -ENOENT;
  343. /* If the packet doesn't contain the original IPv6 header we are
  344. in trouble since we might need the source address for further
  345. processing of the error. */
  346. read_lock(&ip6ip6_lock);
  347. if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
  348. goto out;
  349. err = 0;
  350. switch (type) {
  351. __u32 teli;
  352. struct ipv6_tlv_tnl_enc_lim *tel;
  353. __u32 mtu;
  354. case ICMPV6_DEST_UNREACH:
  355. if (net_ratelimit())
  356. printk(KERN_WARNING
  357. "%s: Path to destination invalid "
  358. "or inactive!\n", t->parms.name);
  359. rel_msg = 1;
  360. break;
  361. case ICMPV6_TIME_EXCEED:
  362. if (code == ICMPV6_EXC_HOPLIMIT) {
  363. if (net_ratelimit())
  364. printk(KERN_WARNING
  365. "%s: Too small hop limit or "
  366. "routing loop in tunnel!\n",
  367. t->parms.name);
  368. rel_msg = 1;
  369. }
  370. break;
  371. case ICMPV6_PARAMPROB:
  372. /* ignore if parameter problem not caused by a tunnel
  373. encapsulation limit sub-option */
  374. if (code != ICMPV6_HDR_FIELD) {
  375. break;
  376. }
  377. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  378. if (teli && teli == ntohl(info) - 2) {
  379. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  380. if (tel->encap_limit == 0) {
  381. if (net_ratelimit())
  382. printk(KERN_WARNING
  383. "%s: Too small encapsulation "
  384. "limit or routing loop in "
  385. "tunnel!\n", t->parms.name);
  386. rel_msg = 1;
  387. }
  388. }
  389. break;
  390. case ICMPV6_PKT_TOOBIG:
  391. mtu = ntohl(info) - offset;
  392. if (mtu < IPV6_MIN_MTU)
  393. mtu = IPV6_MIN_MTU;
  394. t->dev->mtu = mtu;
  395. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  396. rel_type = ICMPV6_PKT_TOOBIG;
  397. rel_code = 0;
  398. rel_info = mtu;
  399. rel_msg = 1;
  400. }
  401. break;
  402. }
  403. if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
  404. struct rt6_info *rt;
  405. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  406. if (!skb2)
  407. goto out;
  408. dst_release(skb2->dst);
  409. skb2->dst = NULL;
  410. skb_pull(skb2, offset);
  411. skb2->nh.raw = skb2->data;
  412. /* Try to guess incoming interface */
  413. rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
  414. if (rt && rt->rt6i_dev)
  415. skb2->dev = rt->rt6i_dev;
  416. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  417. if (rt)
  418. dst_release(&rt->u.dst);
  419. kfree_skb(skb2);
  420. }
  421. out:
  422. read_unlock(&ip6ip6_lock);
  423. return err;
  424. }
  425. static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
  426. struct sk_buff *skb)
  427. {
  428. struct ipv6hdr *inner_iph = skb->nh.ipv6h;
  429. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  430. IP6_ECN_set_ce(inner_iph);
  431. }
  432. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  433. {
  434. struct ip6_tnl_parm *p = &t->parms;
  435. int ret = 0;
  436. if (p->flags & IP6_TNL_F_CAP_RCV) {
  437. struct net_device *ldev = NULL;
  438. if (p->link)
  439. ldev = dev_get_by_index(p->link);
  440. if ((ipv6_addr_is_multicast(&p->laddr) ||
  441. likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
  442. likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
  443. ret = 1;
  444. if (ldev)
  445. dev_put(ldev);
  446. }
  447. return ret;
  448. }
  449. /**
  450. * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
  451. * @skb: received socket buffer
  452. *
  453. * Return: 0
  454. **/
  455. static int
  456. ip6ip6_rcv(struct sk_buff *skb)
  457. {
  458. struct ipv6hdr *ipv6h;
  459. struct ip6_tnl *t;
  460. ipv6h = skb->nh.ipv6h;
  461. read_lock(&ip6ip6_lock);
  462. if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
  463. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  464. read_unlock(&ip6ip6_lock);
  465. goto discard;
  466. }
  467. if (!ip6_tnl_rcv_ctl(t)) {
  468. t->stat.rx_dropped++;
  469. read_unlock(&ip6ip6_lock);
  470. goto discard;
  471. }
  472. secpath_reset(skb);
  473. skb->mac.raw = skb->nh.raw;
  474. skb->nh.raw = skb->data;
  475. skb->protocol = htons(ETH_P_IPV6);
  476. skb->pkt_type = PACKET_HOST;
  477. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  478. skb->dev = t->dev;
  479. dst_release(skb->dst);
  480. skb->dst = NULL;
  481. nf_reset(skb);
  482. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  483. ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
  484. ip6ip6_ecn_decapsulate(ipv6h, skb);
  485. t->stat.rx_packets++;
  486. t->stat.rx_bytes += skb->len;
  487. netif_rx(skb);
  488. read_unlock(&ip6ip6_lock);
  489. return 0;
  490. }
  491. read_unlock(&ip6ip6_lock);
  492. return 1;
  493. discard:
  494. kfree_skb(skb);
  495. return 0;
  496. }
  497. static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
  498. {
  499. struct ipv6_tlv_tnl_enc_lim *tel;
  500. struct ipv6_txoptions *opt;
  501. __u8 *raw;
  502. int opt_len = sizeof(*opt) + 8;
  503. if (!(opt = kzalloc(opt_len, GFP_ATOMIC))) {
  504. return NULL;
  505. }
  506. opt->tot_len = opt_len;
  507. opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
  508. opt->opt_nflen = 8;
  509. tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
  510. tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
  511. tel->length = 1;
  512. tel->encap_limit = encap_limit;
  513. raw = (__u8 *) opt->dst0opt;
  514. raw[5] = IPV6_TLV_PADN;
  515. raw[6] = 1;
  516. return opt;
  517. }
  518. /**
  519. * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  520. * @t: the outgoing tunnel device
  521. * @hdr: IPv6 header from the incoming packet
  522. *
  523. * Description:
  524. * Avoid trivial tunneling loop by checking that tunnel exit-point
  525. * doesn't match source of incoming packet.
  526. *
  527. * Return:
  528. * 1 if conflict,
  529. * 0 else
  530. **/
  531. static inline int
  532. ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  533. {
  534. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  535. }
  536. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  537. {
  538. struct ip6_tnl_parm *p = &t->parms;
  539. int ret = 0;
  540. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  541. struct net_device *ldev = NULL;
  542. if (p->link)
  543. ldev = dev_get_by_index(p->link);
  544. if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
  545. printk(KERN_WARNING
  546. "%s xmit: Local address not yet configured!\n",
  547. p->name);
  548. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  549. unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
  550. printk(KERN_WARNING
  551. "%s xmit: Routing loop! "
  552. "Remote address found on this node!\n",
  553. p->name);
  554. else
  555. ret = 1;
  556. if (ldev)
  557. dev_put(ldev);
  558. }
  559. return ret;
  560. }
  561. /**
  562. * ip6ip6_tnl_xmit - encapsulate packet and send
  563. * @skb: the outgoing socket buffer
  564. * @dev: the outgoing tunnel device
  565. *
  566. * Description:
  567. * Build new header and do some sanity checks on the packet before sending
  568. * it.
  569. *
  570. * Return:
  571. * 0
  572. **/
  573. static int
  574. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  575. {
  576. struct ip6_tnl *t = netdev_priv(dev);
  577. struct net_device_stats *stats = &t->stat;
  578. struct ipv6hdr *ipv6h = skb->nh.ipv6h;
  579. struct ipv6_txoptions *opt = NULL;
  580. int encap_limit = -1;
  581. __u16 offset;
  582. struct flowi fl;
  583. struct dst_entry *dst;
  584. struct net_device *tdev;
  585. int mtu;
  586. int max_headroom = sizeof(struct ipv6hdr);
  587. u8 proto;
  588. int err;
  589. int pkt_len;
  590. int dsfield;
  591. if (t->recursion++) {
  592. stats->collisions++;
  593. goto tx_err;
  594. }
  595. if (skb->protocol != htons(ETH_P_IPV6) ||
  596. !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
  597. goto tx_err;
  598. if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
  599. struct ipv6_tlv_tnl_enc_lim *tel;
  600. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
  601. if (tel->encap_limit == 0) {
  602. icmpv6_send(skb, ICMPV6_PARAMPROB,
  603. ICMPV6_HDR_FIELD, offset + 2, skb->dev);
  604. goto tx_err;
  605. }
  606. encap_limit = tel->encap_limit - 1;
  607. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  608. encap_limit = t->parms.encap_limit;
  609. }
  610. memcpy(&fl, &t->fl, sizeof (fl));
  611. proto = fl.proto;
  612. dsfield = ipv6_get_dsfield(ipv6h);
  613. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  614. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  615. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  616. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  617. if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
  618. goto tx_err;
  619. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  620. dst_hold(dst);
  621. else {
  622. dst = ip6_route_output(NULL, &fl);
  623. if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
  624. goto tx_err_link_failure;
  625. }
  626. tdev = dst->dev;
  627. if (tdev == dev) {
  628. stats->collisions++;
  629. if (net_ratelimit())
  630. printk(KERN_WARNING
  631. "%s: Local routing loop detected!\n",
  632. t->parms.name);
  633. goto tx_err_dst_release;
  634. }
  635. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  636. if (opt) {
  637. max_headroom += 8;
  638. mtu -= 8;
  639. }
  640. if (mtu < IPV6_MIN_MTU)
  641. mtu = IPV6_MIN_MTU;
  642. if (skb->dst && mtu < dst_mtu(skb->dst)) {
  643. struct rt6_info *rt = (struct rt6_info *) skb->dst;
  644. rt->rt6i_flags |= RTF_MODIFIED;
  645. rt->u.dst.metrics[RTAX_MTU-1] = mtu;
  646. }
  647. if (skb->len > mtu) {
  648. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  649. goto tx_err_dst_release;
  650. }
  651. /*
  652. * Okay, now see if we can stuff it in the buffer as-is.
  653. */
  654. max_headroom += LL_RESERVED_SPACE(tdev);
  655. if (skb_headroom(skb) < max_headroom ||
  656. skb_cloned(skb) || skb_shared(skb)) {
  657. struct sk_buff *new_skb;
  658. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  659. goto tx_err_dst_release;
  660. if (skb->sk)
  661. skb_set_owner_w(new_skb, skb->sk);
  662. kfree_skb(skb);
  663. skb = new_skb;
  664. }
  665. dst_release(skb->dst);
  666. skb->dst = dst_clone(dst);
  667. skb->h.raw = skb->nh.raw;
  668. if (opt)
  669. ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
  670. skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
  671. ipv6h = skb->nh.ipv6h;
  672. *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
  673. dsfield = INET_ECN_encapsulate(0, dsfield);
  674. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  675. ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  676. ipv6h->hop_limit = t->parms.hop_limit;
  677. ipv6h->nexthdr = proto;
  678. ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
  679. ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
  680. nf_reset(skb);
  681. pkt_len = skb->len;
  682. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
  683. skb->dst->dev, dst_output);
  684. if (net_xmit_eval(err) == 0) {
  685. stats->tx_bytes += pkt_len;
  686. stats->tx_packets++;
  687. } else {
  688. stats->tx_errors++;
  689. stats->tx_aborted_errors++;
  690. }
  691. ip6_tnl_dst_store(t, dst);
  692. kfree(opt);
  693. t->recursion--;
  694. return 0;
  695. tx_err_link_failure:
  696. stats->tx_carrier_errors++;
  697. dst_link_failure(skb);
  698. tx_err_dst_release:
  699. dst_release(dst);
  700. kfree(opt);
  701. tx_err:
  702. stats->tx_errors++;
  703. stats->tx_dropped++;
  704. kfree_skb(skb);
  705. t->recursion--;
  706. return 0;
  707. }
  708. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  709. {
  710. struct ip6_tnl_parm *p = &t->parms;
  711. int ltype = ipv6_addr_type(&p->laddr);
  712. int rtype = ipv6_addr_type(&p->raddr);
  713. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  714. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  715. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  716. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  717. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  718. if (ltype&IPV6_ADDR_UNICAST)
  719. p->flags |= IP6_TNL_F_CAP_XMIT;
  720. if (rtype&IPV6_ADDR_UNICAST)
  721. p->flags |= IP6_TNL_F_CAP_RCV;
  722. }
  723. }
  724. static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
  725. {
  726. struct net_device *dev = t->dev;
  727. struct ip6_tnl_parm *p = &t->parms;
  728. struct flowi *fl = &t->fl;
  729. memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  730. memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  731. /* Set up flowi template */
  732. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  733. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  734. fl->oif = p->link;
  735. fl->fl6_flowlabel = 0;
  736. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  737. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  738. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  739. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  740. ip6_tnl_set_cap(t);
  741. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  742. dev->flags |= IFF_POINTOPOINT;
  743. else
  744. dev->flags &= ~IFF_POINTOPOINT;
  745. dev->iflink = p->link;
  746. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  747. int strict = (ipv6_addr_type(&p->raddr) &
  748. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  749. struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
  750. p->link, strict);
  751. if (rt == NULL)
  752. return;
  753. if (rt->rt6i_dev) {
  754. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  755. sizeof (struct ipv6hdr);
  756. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  757. if (dev->mtu < IPV6_MIN_MTU)
  758. dev->mtu = IPV6_MIN_MTU;
  759. }
  760. dst_release(&rt->u.dst);
  761. }
  762. }
  763. /**
  764. * ip6ip6_tnl_change - update the tunnel parameters
  765. * @t: tunnel to be changed
  766. * @p: tunnel configuration parameters
  767. * @active: != 0 if tunnel is ready for use
  768. *
  769. * Description:
  770. * ip6ip6_tnl_change() updates the tunnel parameters
  771. **/
  772. static int
  773. ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  774. {
  775. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  776. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  777. t->parms.flags = p->flags;
  778. t->parms.hop_limit = p->hop_limit;
  779. t->parms.encap_limit = p->encap_limit;
  780. t->parms.flowinfo = p->flowinfo;
  781. t->parms.link = p->link;
  782. ip6_tnl_dst_reset(t);
  783. ip6ip6_tnl_link_config(t);
  784. return 0;
  785. }
  786. /**
  787. * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  788. * @dev: virtual device associated with tunnel
  789. * @ifr: parameters passed from userspace
  790. * @cmd: command to be performed
  791. *
  792. * Description:
  793. * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
  794. * from userspace.
  795. *
  796. * The possible commands are the following:
  797. * %SIOCGETTUNNEL: get tunnel parameters for device
  798. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  799. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  800. * %SIOCDELTUNNEL: delete tunnel
  801. *
  802. * The fallback device "ip6tnl0", created during module
  803. * initialization, can be used for creating other tunnel devices.
  804. *
  805. * Return:
  806. * 0 on success,
  807. * %-EFAULT if unable to copy data to or from userspace,
  808. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  809. * %-EINVAL if passed tunnel parameters are invalid,
  810. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  811. * %-ENODEV if attempting to change or delete a nonexisting device
  812. **/
  813. static int
  814. ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  815. {
  816. int err = 0;
  817. struct ip6_tnl_parm p;
  818. struct ip6_tnl *t = NULL;
  819. switch (cmd) {
  820. case SIOCGETTUNNEL:
  821. if (dev == ip6ip6_fb_tnl_dev) {
  822. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  823. err = -EFAULT;
  824. break;
  825. }
  826. t = ip6ip6_tnl_locate(&p, 0);
  827. }
  828. if (t == NULL)
  829. t = netdev_priv(dev);
  830. memcpy(&p, &t->parms, sizeof (p));
  831. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  832. err = -EFAULT;
  833. }
  834. break;
  835. case SIOCADDTUNNEL:
  836. case SIOCCHGTUNNEL:
  837. err = -EPERM;
  838. if (!capable(CAP_NET_ADMIN))
  839. break;
  840. err = -EFAULT;
  841. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  842. break;
  843. err = -EINVAL;
  844. if (p.proto != IPPROTO_IPV6)
  845. break;
  846. t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
  847. if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  848. if (t != NULL) {
  849. if (t->dev != dev) {
  850. err = -EEXIST;
  851. break;
  852. }
  853. } else
  854. t = netdev_priv(dev);
  855. ip6ip6_tnl_unlink(t);
  856. err = ip6ip6_tnl_change(t, &p);
  857. ip6ip6_tnl_link(t);
  858. netdev_state_change(dev);
  859. }
  860. if (t) {
  861. err = 0;
  862. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  863. err = -EFAULT;
  864. } else
  865. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  866. break;
  867. case SIOCDELTUNNEL:
  868. err = -EPERM;
  869. if (!capable(CAP_NET_ADMIN))
  870. break;
  871. if (dev == ip6ip6_fb_tnl_dev) {
  872. err = -EFAULT;
  873. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  874. break;
  875. err = -ENOENT;
  876. if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
  877. break;
  878. err = -EPERM;
  879. if (t->dev == ip6ip6_fb_tnl_dev)
  880. break;
  881. dev = t->dev;
  882. }
  883. err = unregister_netdevice(dev);
  884. break;
  885. default:
  886. err = -EINVAL;
  887. }
  888. return err;
  889. }
  890. /**
  891. * ip6ip6_tnl_get_stats - return the stats for tunnel device
  892. * @dev: virtual device associated with tunnel
  893. *
  894. * Return: stats for device
  895. **/
  896. static struct net_device_stats *
  897. ip6ip6_tnl_get_stats(struct net_device *dev)
  898. {
  899. return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
  900. }
  901. /**
  902. * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
  903. * @dev: virtual device associated with tunnel
  904. * @new_mtu: the new mtu
  905. *
  906. * Return:
  907. * 0 on success,
  908. * %-EINVAL if mtu too small
  909. **/
  910. static int
  911. ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  912. {
  913. if (new_mtu < IPV6_MIN_MTU) {
  914. return -EINVAL;
  915. }
  916. dev->mtu = new_mtu;
  917. return 0;
  918. }
  919. /**
  920. * ip6ip6_tnl_dev_setup - setup virtual tunnel device
  921. * @dev: virtual device associated with tunnel
  922. *
  923. * Description:
  924. * Initialize function pointers and device parameters
  925. **/
  926. static void ip6ip6_tnl_dev_setup(struct net_device *dev)
  927. {
  928. SET_MODULE_OWNER(dev);
  929. dev->uninit = ip6ip6_tnl_dev_uninit;
  930. dev->destructor = free_netdev;
  931. dev->hard_start_xmit = ip6ip6_tnl_xmit;
  932. dev->get_stats = ip6ip6_tnl_get_stats;
  933. dev->do_ioctl = ip6ip6_tnl_ioctl;
  934. dev->change_mtu = ip6ip6_tnl_change_mtu;
  935. dev->type = ARPHRD_TUNNEL6;
  936. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  937. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  938. dev->flags |= IFF_NOARP;
  939. dev->addr_len = sizeof(struct in6_addr);
  940. }
  941. /**
  942. * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  943. * @dev: virtual device associated with tunnel
  944. **/
  945. static inline void
  946. ip6ip6_tnl_dev_init_gen(struct net_device *dev)
  947. {
  948. struct ip6_tnl *t = netdev_priv(dev);
  949. t->fl.proto = IPPROTO_IPV6;
  950. t->dev = dev;
  951. strcpy(t->parms.name, dev->name);
  952. }
  953. /**
  954. * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  955. * @dev: virtual device associated with tunnel
  956. **/
  957. static int
  958. ip6ip6_tnl_dev_init(struct net_device *dev)
  959. {
  960. struct ip6_tnl *t = netdev_priv(dev);
  961. ip6ip6_tnl_dev_init_gen(dev);
  962. ip6ip6_tnl_link_config(t);
  963. return 0;
  964. }
  965. /**
  966. * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  967. * @dev: fallback device
  968. *
  969. * Return: 0
  970. **/
  971. static int
  972. ip6ip6_fb_tnl_dev_init(struct net_device *dev)
  973. {
  974. struct ip6_tnl *t = netdev_priv(dev);
  975. ip6ip6_tnl_dev_init_gen(dev);
  976. dev_hold(dev);
  977. tnls_wc[0] = t;
  978. return 0;
  979. }
  980. static struct xfrm6_tunnel ip6ip6_handler = {
  981. .handler = ip6ip6_rcv,
  982. .err_handler = ip6ip6_err,
  983. .priority = 1,
  984. };
  985. /**
  986. * ip6_tunnel_init - register protocol and reserve needed resources
  987. *
  988. * Return: 0 on success
  989. **/
  990. static int __init ip6_tunnel_init(void)
  991. {
  992. int err;
  993. if (xfrm6_tunnel_register(&ip6ip6_handler)) {
  994. printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
  995. return -EAGAIN;
  996. }
  997. ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  998. ip6ip6_tnl_dev_setup);
  999. if (!ip6ip6_fb_tnl_dev) {
  1000. err = -ENOMEM;
  1001. goto fail;
  1002. }
  1003. ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
  1004. if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
  1005. free_netdev(ip6ip6_fb_tnl_dev);
  1006. goto fail;
  1007. }
  1008. return 0;
  1009. fail:
  1010. xfrm6_tunnel_deregister(&ip6ip6_handler);
  1011. return err;
  1012. }
  1013. static void __exit ip6ip6_destroy_tunnels(void)
  1014. {
  1015. int h;
  1016. struct ip6_tnl *t;
  1017. for (h = 0; h < HASH_SIZE; h++) {
  1018. while ((t = tnls_r_l[h]) != NULL)
  1019. unregister_netdevice(t->dev);
  1020. }
  1021. t = tnls_wc[0];
  1022. unregister_netdevice(t->dev);
  1023. }
  1024. /**
  1025. * ip6_tunnel_cleanup - free resources and unregister protocol
  1026. **/
  1027. static void __exit ip6_tunnel_cleanup(void)
  1028. {
  1029. if (xfrm6_tunnel_deregister(&ip6ip6_handler))
  1030. printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
  1031. rtnl_lock();
  1032. ip6ip6_destroy_tunnels();
  1033. rtnl_unlock();
  1034. }
  1035. module_init(ip6_tunnel_init);
  1036. module_exit(ip6_tunnel_cleanup);