ip6_tunnel.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * IPv6 over IPv6 tunnel device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. *
  8. * $Id: ip6_tunnel.c,v 1.1.1.1 2007/06/12 07:27:14 eyryu Exp $
  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. teli = 0;
  373. if (code == ICMPV6_HDR_FIELD)
  374. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  375. if (teli && teli == ntohl(info) - 2) {
  376. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  377. if (tel->encap_limit == 0) {
  378. if (net_ratelimit())
  379. printk(KERN_WARNING
  380. "%s: Too small encapsulation "
  381. "limit or routing loop in "
  382. "tunnel!\n", t->parms.name);
  383. rel_msg = 1;
  384. }
  385. } else if (net_ratelimit()) {
  386. printk(KERN_WARNING
  387. "%s: Recipient unable to parse tunneled "
  388. "packet!\n ", t->parms.name);
  389. }
  390. break;
  391. case ICMPV6_PKT_TOOBIG:
  392. mtu = ntohl(info) - offset;
  393. if (mtu < IPV6_MIN_MTU)
  394. mtu = IPV6_MIN_MTU;
  395. t->dev->mtu = mtu;
  396. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  397. rel_type = ICMPV6_PKT_TOOBIG;
  398. rel_code = 0;
  399. rel_info = mtu;
  400. rel_msg = 1;
  401. }
  402. break;
  403. }
  404. if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
  405. struct rt6_info *rt;
  406. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  407. if (!skb2)
  408. goto out;
  409. dst_release(skb2->dst);
  410. skb2->dst = NULL;
  411. skb_pull(skb2, offset);
  412. skb2->nh.raw = skb2->data;
  413. /* Try to guess incoming interface */
  414. rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
  415. if (rt && rt->rt6i_dev)
  416. skb2->dev = rt->rt6i_dev;
  417. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  418. if (rt)
  419. dst_release(&rt->u.dst);
  420. kfree_skb(skb2);
  421. }
  422. out:
  423. read_unlock(&ip6ip6_lock);
  424. return err;
  425. }
  426. static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
  427. struct sk_buff *skb)
  428. {
  429. struct ipv6hdr *inner_iph = skb->nh.ipv6h;
  430. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  431. IP6_ECN_set_ce(inner_iph);
  432. }
  433. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  434. {
  435. struct ip6_tnl_parm *p = &t->parms;
  436. int ret = 0;
  437. if (p->flags & IP6_TNL_F_CAP_RCV) {
  438. struct net_device *ldev = NULL;
  439. if (p->link)
  440. ldev = dev_get_by_index(p->link);
  441. if ((ipv6_addr_is_multicast(&p->laddr) ||
  442. likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
  443. likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
  444. ret = 1;
  445. if (ldev)
  446. dev_put(ldev);
  447. }
  448. return ret;
  449. }
  450. /**
  451. * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
  452. * @skb: received socket buffer
  453. *
  454. * Return: 0
  455. **/
  456. static int
  457. ip6ip6_rcv(struct sk_buff *skb)
  458. {
  459. struct ipv6hdr *ipv6h;
  460. struct ip6_tnl *t;
  461. ipv6h = skb->nh.ipv6h;
  462. read_lock(&ip6ip6_lock);
  463. if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
  464. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  465. read_unlock(&ip6ip6_lock);
  466. goto discard;
  467. }
  468. if (!ip6_tnl_rcv_ctl(t)) {
  469. t->stat.rx_dropped++;
  470. read_unlock(&ip6ip6_lock);
  471. goto discard;
  472. }
  473. secpath_reset(skb);
  474. skb->mac.raw = skb->nh.raw;
  475. skb->nh.raw = skb->data;
  476. skb->protocol = htons(ETH_P_IPV6);
  477. skb->pkt_type = PACKET_HOST;
  478. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  479. skb->dev = t->dev;
  480. dst_release(skb->dst);
  481. skb->dst = NULL;
  482. nf_reset(skb);
  483. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  484. ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
  485. ip6ip6_ecn_decapsulate(ipv6h, skb);
  486. t->stat.rx_packets++;
  487. t->stat.rx_bytes += skb->len;
  488. netif_rx(skb);
  489. read_unlock(&ip6ip6_lock);
  490. return 0;
  491. }
  492. read_unlock(&ip6ip6_lock);
  493. return 1;
  494. discard:
  495. kfree_skb(skb);
  496. return 0;
  497. }
  498. struct ipv6_tel_txoption {
  499. struct ipv6_txoptions ops;
  500. __u8 dst_opt[8];
  501. };
  502. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  503. {
  504. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  505. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  506. opt->dst_opt[3] = 1;
  507. opt->dst_opt[4] = encap_limit;
  508. opt->dst_opt[5] = IPV6_TLV_PADN;
  509. opt->dst_opt[6] = 1;
  510. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  511. opt->ops.opt_nflen = 8;
  512. }
  513. /**
  514. * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  515. * @t: the outgoing tunnel device
  516. * @hdr: IPv6 header from the incoming packet
  517. *
  518. * Description:
  519. * Avoid trivial tunneling loop by checking that tunnel exit-point
  520. * doesn't match source of incoming packet.
  521. *
  522. * Return:
  523. * 1 if conflict,
  524. * 0 else
  525. **/
  526. static inline int
  527. ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  528. {
  529. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  530. }
  531. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  532. {
  533. struct ip6_tnl_parm *p = &t->parms;
  534. int ret = 0;
  535. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  536. struct net_device *ldev = NULL;
  537. if (p->link)
  538. ldev = dev_get_by_index(p->link);
  539. if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
  540. printk(KERN_WARNING
  541. "%s xmit: Local address not yet configured!\n",
  542. p->name);
  543. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  544. unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
  545. printk(KERN_WARNING
  546. "%s xmit: Routing loop! "
  547. "Remote address found on this node!\n",
  548. p->name);
  549. else
  550. ret = 1;
  551. if (ldev)
  552. dev_put(ldev);
  553. }
  554. return ret;
  555. }
  556. /**
  557. * ip6ip6_tnl_xmit - encapsulate packet and send
  558. * @skb: the outgoing socket buffer
  559. * @dev: the outgoing tunnel device
  560. *
  561. * Description:
  562. * Build new header and do some sanity checks on the packet before sending
  563. * it.
  564. *
  565. * Return:
  566. * 0
  567. **/
  568. static int
  569. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  570. {
  571. struct ip6_tnl *t = netdev_priv(dev);
  572. struct net_device_stats *stats = &t->stat;
  573. struct ipv6hdr *ipv6h = skb->nh.ipv6h;
  574. int encap_limit = -1;
  575. struct ipv6_tel_txoption opt;
  576. __u16 offset;
  577. struct flowi fl;
  578. struct dst_entry *dst;
  579. struct net_device *tdev;
  580. int mtu;
  581. int max_headroom = sizeof(struct ipv6hdr);
  582. u8 proto;
  583. int err;
  584. int pkt_len;
  585. int dsfield;
  586. if (t->recursion++) {
  587. stats->collisions++;
  588. goto tx_err;
  589. }
  590. if (skb->protocol != htons(ETH_P_IPV6) ||
  591. !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
  592. goto tx_err;
  593. if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
  594. struct ipv6_tlv_tnl_enc_lim *tel;
  595. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
  596. if (tel->encap_limit == 0) {
  597. icmpv6_send(skb, ICMPV6_PARAMPROB,
  598. ICMPV6_HDR_FIELD, offset + 2, skb->dev);
  599. goto tx_err;
  600. }
  601. encap_limit = tel->encap_limit - 1;
  602. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  603. encap_limit = t->parms.encap_limit;
  604. memcpy(&fl, &t->fl, sizeof (fl));
  605. proto = fl.proto;
  606. dsfield = ipv6_get_dsfield(ipv6h);
  607. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  608. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  609. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  610. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  611. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  612. dst_hold(dst);
  613. else {
  614. dst = ip6_route_output(NULL, &fl);
  615. if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
  616. goto tx_err_link_failure;
  617. }
  618. tdev = dst->dev;
  619. if (tdev == dev) {
  620. stats->collisions++;
  621. if (net_ratelimit())
  622. printk(KERN_WARNING
  623. "%s: Local routing loop detected!\n",
  624. t->parms.name);
  625. goto tx_err_dst_release;
  626. }
  627. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  628. if (encap_limit >= 0) {
  629. max_headroom += 8;
  630. mtu -= 8;
  631. }
  632. if (mtu < IPV6_MIN_MTU)
  633. mtu = IPV6_MIN_MTU;
  634. if (skb->dst)
  635. skb->dst->ops->update_pmtu(skb->dst, mtu);
  636. if (skb->len > mtu) {
  637. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  638. goto tx_err_dst_release;
  639. }
  640. /*
  641. * Okay, now see if we can stuff it in the buffer as-is.
  642. */
  643. max_headroom += LL_RESERVED_SPACE(tdev);
  644. if (skb_headroom(skb) < max_headroom ||
  645. skb_cloned(skb) || skb_shared(skb)) {
  646. struct sk_buff *new_skb;
  647. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  648. goto tx_err_dst_release;
  649. if (skb->sk)
  650. skb_set_owner_w(new_skb, skb->sk);
  651. kfree_skb(skb);
  652. skb = new_skb;
  653. }
  654. dst_release(skb->dst);
  655. skb->dst = dst_clone(dst);
  656. skb->h.raw = skb->nh.raw;
  657. if (encap_limit >= 0) {
  658. init_tel_txopt(&opt, encap_limit);
  659. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  660. }
  661. skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
  662. ipv6h = skb->nh.ipv6h;
  663. *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
  664. dsfield = INET_ECN_encapsulate(0, dsfield);
  665. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  666. ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  667. ipv6h->hop_limit = t->parms.hop_limit;
  668. ipv6h->nexthdr = proto;
  669. ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
  670. ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
  671. nf_reset(skb);
  672. pkt_len = skb->len;
  673. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
  674. skb->dst->dev, dst_output);
  675. if (net_xmit_eval(err) == 0) {
  676. stats->tx_bytes += pkt_len;
  677. stats->tx_packets++;
  678. } else {
  679. stats->tx_errors++;
  680. stats->tx_aborted_errors++;
  681. }
  682. ip6_tnl_dst_store(t, dst);
  683. t->recursion--;
  684. return 0;
  685. tx_err_link_failure:
  686. stats->tx_carrier_errors++;
  687. dst_link_failure(skb);
  688. tx_err_dst_release:
  689. dst_release(dst);
  690. tx_err:
  691. stats->tx_errors++;
  692. stats->tx_dropped++;
  693. kfree_skb(skb);
  694. t->recursion--;
  695. return 0;
  696. }
  697. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  698. {
  699. struct ip6_tnl_parm *p = &t->parms;
  700. int ltype = ipv6_addr_type(&p->laddr);
  701. int rtype = ipv6_addr_type(&p->raddr);
  702. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  703. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  704. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  705. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  706. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  707. if (ltype&IPV6_ADDR_UNICAST)
  708. p->flags |= IP6_TNL_F_CAP_XMIT;
  709. if (rtype&IPV6_ADDR_UNICAST)
  710. p->flags |= IP6_TNL_F_CAP_RCV;
  711. }
  712. }
  713. static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
  714. {
  715. struct net_device *dev = t->dev;
  716. struct ip6_tnl_parm *p = &t->parms;
  717. struct flowi *fl = &t->fl;
  718. memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  719. memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  720. /* Set up flowi template */
  721. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  722. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  723. fl->oif = p->link;
  724. fl->fl6_flowlabel = 0;
  725. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  726. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  727. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  728. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  729. ip6_tnl_set_cap(t);
  730. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  731. dev->flags |= IFF_POINTOPOINT;
  732. else
  733. dev->flags &= ~IFF_POINTOPOINT;
  734. dev->iflink = p->link;
  735. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  736. int strict = (ipv6_addr_type(&p->raddr) &
  737. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  738. struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
  739. p->link, strict);
  740. if (rt == NULL)
  741. return;
  742. if (rt->rt6i_dev) {
  743. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  744. sizeof (struct ipv6hdr);
  745. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  746. if (dev->mtu < IPV6_MIN_MTU)
  747. dev->mtu = IPV6_MIN_MTU;
  748. }
  749. dst_release(&rt->u.dst);
  750. }
  751. }
  752. /**
  753. * ip6ip6_tnl_change - update the tunnel parameters
  754. * @t: tunnel to be changed
  755. * @p: tunnel configuration parameters
  756. * @active: != 0 if tunnel is ready for use
  757. *
  758. * Description:
  759. * ip6ip6_tnl_change() updates the tunnel parameters
  760. **/
  761. static int
  762. ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  763. {
  764. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  765. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  766. t->parms.flags = p->flags;
  767. t->parms.hop_limit = p->hop_limit;
  768. t->parms.encap_limit = p->encap_limit;
  769. t->parms.flowinfo = p->flowinfo;
  770. t->parms.link = p->link;
  771. ip6_tnl_dst_reset(t);
  772. ip6ip6_tnl_link_config(t);
  773. return 0;
  774. }
  775. /**
  776. * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  777. * @dev: virtual device associated with tunnel
  778. * @ifr: parameters passed from userspace
  779. * @cmd: command to be performed
  780. *
  781. * Description:
  782. * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
  783. * from userspace.
  784. *
  785. * The possible commands are the following:
  786. * %SIOCGETTUNNEL: get tunnel parameters for device
  787. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  788. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  789. * %SIOCDELTUNNEL: delete tunnel
  790. *
  791. * The fallback device "ip6tnl0", created during module
  792. * initialization, can be used for creating other tunnel devices.
  793. *
  794. * Return:
  795. * 0 on success,
  796. * %-EFAULT if unable to copy data to or from userspace,
  797. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  798. * %-EINVAL if passed tunnel parameters are invalid,
  799. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  800. * %-ENODEV if attempting to change or delete a nonexisting device
  801. **/
  802. static int
  803. ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  804. {
  805. int err = 0;
  806. struct ip6_tnl_parm p;
  807. struct ip6_tnl *t = NULL;
  808. switch (cmd) {
  809. case SIOCGETTUNNEL:
  810. if (dev == ip6ip6_fb_tnl_dev) {
  811. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  812. err = -EFAULT;
  813. break;
  814. }
  815. t = ip6ip6_tnl_locate(&p, 0);
  816. }
  817. if (t == NULL)
  818. t = netdev_priv(dev);
  819. memcpy(&p, &t->parms, sizeof (p));
  820. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  821. err = -EFAULT;
  822. }
  823. break;
  824. case SIOCADDTUNNEL:
  825. case SIOCCHGTUNNEL:
  826. err = -EPERM;
  827. if (!capable(CAP_NET_ADMIN))
  828. break;
  829. err = -EFAULT;
  830. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  831. break;
  832. err = -EINVAL;
  833. if (p.proto != IPPROTO_IPV6)
  834. break;
  835. t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
  836. if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  837. if (t != NULL) {
  838. if (t->dev != dev) {
  839. err = -EEXIST;
  840. break;
  841. }
  842. } else
  843. t = netdev_priv(dev);
  844. ip6ip6_tnl_unlink(t);
  845. err = ip6ip6_tnl_change(t, &p);
  846. ip6ip6_tnl_link(t);
  847. netdev_state_change(dev);
  848. }
  849. if (t) {
  850. err = 0;
  851. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  852. err = -EFAULT;
  853. } else
  854. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  855. break;
  856. case SIOCDELTUNNEL:
  857. err = -EPERM;
  858. if (!capable(CAP_NET_ADMIN))
  859. break;
  860. if (dev == ip6ip6_fb_tnl_dev) {
  861. err = -EFAULT;
  862. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  863. break;
  864. err = -ENOENT;
  865. if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
  866. break;
  867. err = -EPERM;
  868. if (t->dev == ip6ip6_fb_tnl_dev)
  869. break;
  870. dev = t->dev;
  871. }
  872. err = 0;
  873. unregister_netdevice(dev);
  874. break;
  875. default:
  876. err = -EINVAL;
  877. }
  878. return err;
  879. }
  880. /**
  881. * ip6ip6_tnl_get_stats - return the stats for tunnel device
  882. * @dev: virtual device associated with tunnel
  883. *
  884. * Return: stats for device
  885. **/
  886. static struct net_device_stats *
  887. ip6ip6_tnl_get_stats(struct net_device *dev)
  888. {
  889. return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
  890. }
  891. /**
  892. * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
  893. * @dev: virtual device associated with tunnel
  894. * @new_mtu: the new mtu
  895. *
  896. * Return:
  897. * 0 on success,
  898. * %-EINVAL if mtu too small
  899. **/
  900. static int
  901. ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  902. {
  903. if (new_mtu < IPV6_MIN_MTU) {
  904. return -EINVAL;
  905. }
  906. dev->mtu = new_mtu;
  907. return 0;
  908. }
  909. /**
  910. * ip6ip6_tnl_dev_setup - setup virtual tunnel device
  911. * @dev: virtual device associated with tunnel
  912. *
  913. * Description:
  914. * Initialize function pointers and device parameters
  915. **/
  916. static void ip6ip6_tnl_dev_setup(struct net_device *dev)
  917. {
  918. SET_MODULE_OWNER(dev);
  919. dev->uninit = ip6ip6_tnl_dev_uninit;
  920. dev->destructor = free_netdev;
  921. dev->hard_start_xmit = ip6ip6_tnl_xmit;
  922. dev->get_stats = ip6ip6_tnl_get_stats;
  923. dev->do_ioctl = ip6ip6_tnl_ioctl;
  924. dev->change_mtu = ip6ip6_tnl_change_mtu;
  925. dev->type = ARPHRD_TUNNEL6;
  926. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  927. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  928. dev->flags |= IFF_NOARP;
  929. dev->addr_len = sizeof(struct in6_addr);
  930. }
  931. /**
  932. * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  933. * @dev: virtual device associated with tunnel
  934. **/
  935. static inline void
  936. ip6ip6_tnl_dev_init_gen(struct net_device *dev)
  937. {
  938. struct ip6_tnl *t = netdev_priv(dev);
  939. t->fl.proto = IPPROTO_IPV6;
  940. t->dev = dev;
  941. strcpy(t->parms.name, dev->name);
  942. }
  943. /**
  944. * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  945. * @dev: virtual device associated with tunnel
  946. **/
  947. static int
  948. ip6ip6_tnl_dev_init(struct net_device *dev)
  949. {
  950. struct ip6_tnl *t = netdev_priv(dev);
  951. ip6ip6_tnl_dev_init_gen(dev);
  952. ip6ip6_tnl_link_config(t);
  953. return 0;
  954. }
  955. /**
  956. * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  957. * @dev: fallback device
  958. *
  959. * Return: 0
  960. **/
  961. static int
  962. ip6ip6_fb_tnl_dev_init(struct net_device *dev)
  963. {
  964. struct ip6_tnl *t = netdev_priv(dev);
  965. ip6ip6_tnl_dev_init_gen(dev);
  966. dev_hold(dev);
  967. tnls_wc[0] = t;
  968. return 0;
  969. }
  970. static struct xfrm6_tunnel ip6ip6_handler = {
  971. .handler = ip6ip6_rcv,
  972. .err_handler = ip6ip6_err,
  973. .priority = 1,
  974. };
  975. /**
  976. * ip6_tunnel_init - register protocol and reserve needed resources
  977. *
  978. * Return: 0 on success
  979. **/
  980. static int __init ip6_tunnel_init(void)
  981. {
  982. int err;
  983. if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
  984. printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
  985. return -EAGAIN;
  986. }
  987. ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  988. ip6ip6_tnl_dev_setup);
  989. if (!ip6ip6_fb_tnl_dev) {
  990. err = -ENOMEM;
  991. goto fail;
  992. }
  993. ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
  994. if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
  995. free_netdev(ip6ip6_fb_tnl_dev);
  996. goto fail;
  997. }
  998. return 0;
  999. fail:
  1000. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1001. return err;
  1002. }
  1003. static void __exit ip6ip6_destroy_tunnels(void)
  1004. {
  1005. int h;
  1006. struct ip6_tnl *t;
  1007. for (h = 0; h < HASH_SIZE; h++) {
  1008. while ((t = tnls_r_l[h]) != NULL)
  1009. unregister_netdevice(t->dev);
  1010. }
  1011. t = tnls_wc[0];
  1012. unregister_netdevice(t->dev);
  1013. }
  1014. /**
  1015. * ip6_tunnel_cleanup - free resources and unregister protocol
  1016. **/
  1017. static void __exit ip6_tunnel_cleanup(void)
  1018. {
  1019. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1020. printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
  1021. rtnl_lock();
  1022. ip6ip6_destroy_tunnels();
  1023. rtnl_unlock();
  1024. }
  1025. module_init(ip6_tunnel_init);
  1026. module_exit(ip6_tunnel_cleanup);