ip6_tunnel.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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) (((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. * 0 on success
  186. **/
  187. static int
  188. ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
  189. {
  190. struct net_device *dev;
  191. struct ip6_tnl *t;
  192. char name[IFNAMSIZ];
  193. int err;
  194. if (p->name[0]) {
  195. strlcpy(name, p->name, IFNAMSIZ);
  196. } else {
  197. int i;
  198. for (i = 1; i < IP6_TNL_MAX; i++) {
  199. sprintf(name, "ip6tnl%d", i);
  200. if (__dev_get_by_name(name) == NULL)
  201. break;
  202. }
  203. if (i == IP6_TNL_MAX)
  204. return -ENOBUFS;
  205. }
  206. dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
  207. if (dev == NULL)
  208. return -ENOMEM;
  209. t = netdev_priv(dev);
  210. dev->init = ip6ip6_tnl_dev_init;
  211. t->parms = *p;
  212. if ((err = register_netdevice(dev)) < 0) {
  213. free_netdev(dev);
  214. return err;
  215. }
  216. dev_hold(dev);
  217. ip6ip6_tnl_link(t);
  218. *pt = t;
  219. return 0;
  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. * 0 if tunnel located or created,
  233. * -EINVAL if parameters incorrect,
  234. * -ENODEV if no matching tunnel available
  235. **/
  236. static int
  237. ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
  238. {
  239. struct in6_addr *remote = &p->raddr;
  240. struct in6_addr *local = &p->laddr;
  241. struct ip6_tnl *t;
  242. if (p->proto != IPPROTO_IPV6)
  243. return -EINVAL;
  244. for (t = *ip6ip6_bucket(p); t; t = t->next) {
  245. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  246. ipv6_addr_equal(remote, &t->parms.raddr)) {
  247. *pt = t;
  248. return (create ? -EEXIST : 0);
  249. }
  250. }
  251. if (!create)
  252. return -ENODEV;
  253. return ip6_tnl_create(p, pt);
  254. }
  255. /**
  256. * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
  257. * @dev: the device to be destroyed
  258. *
  259. * Description:
  260. * ip6ip6_tnl_dev_uninit() removes tunnel from its list
  261. **/
  262. static void
  263. ip6ip6_tnl_dev_uninit(struct net_device *dev)
  264. {
  265. struct ip6_tnl *t = netdev_priv(dev);
  266. if (dev == ip6ip6_fb_tnl_dev) {
  267. write_lock_bh(&ip6ip6_lock);
  268. tnls_wc[0] = NULL;
  269. write_unlock_bh(&ip6ip6_lock);
  270. } else {
  271. ip6ip6_tnl_unlink(t);
  272. }
  273. ip6_tnl_dst_reset(t);
  274. dev_put(dev);
  275. }
  276. /**
  277. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  278. * @skb: received socket buffer
  279. *
  280. * Return:
  281. * 0 if none was found,
  282. * else index to encapsulation limit
  283. **/
  284. static __u16
  285. parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
  286. {
  287. struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
  288. __u8 nexthdr = ipv6h->nexthdr;
  289. __u16 off = sizeof (*ipv6h);
  290. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  291. __u16 optlen = 0;
  292. struct ipv6_opt_hdr *hdr;
  293. if (raw + off + sizeof (*hdr) > skb->data &&
  294. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  295. break;
  296. hdr = (struct ipv6_opt_hdr *) (raw + off);
  297. if (nexthdr == NEXTHDR_FRAGMENT) {
  298. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  299. if (frag_hdr->frag_off)
  300. break;
  301. optlen = 8;
  302. } else if (nexthdr == NEXTHDR_AUTH) {
  303. optlen = (hdr->hdrlen + 2) << 2;
  304. } else {
  305. optlen = ipv6_optlen(hdr);
  306. }
  307. if (nexthdr == NEXTHDR_DEST) {
  308. __u16 i = off + 2;
  309. while (1) {
  310. struct ipv6_tlv_tnl_enc_lim *tel;
  311. /* No more room for encapsulation limit */
  312. if (i + sizeof (*tel) > off + optlen)
  313. break;
  314. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  315. /* return index of option if found and valid */
  316. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  317. tel->length == 1)
  318. return i;
  319. /* else jump to next option */
  320. if (tel->type)
  321. i += tel->length + 2;
  322. else
  323. i++;
  324. }
  325. }
  326. nexthdr = hdr->nexthdr;
  327. off += optlen;
  328. }
  329. return 0;
  330. }
  331. /**
  332. * ip6ip6_err - tunnel error handler
  333. *
  334. * Description:
  335. * ip6ip6_err() should handle errors in the tunnel according
  336. * to the specifications in RFC 2473.
  337. **/
  338. static int
  339. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  340. int type, int code, int offset, __u32 info)
  341. {
  342. struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
  343. struct ip6_tnl *t;
  344. int rel_msg = 0;
  345. int rel_type = ICMPV6_DEST_UNREACH;
  346. int rel_code = ICMPV6_ADDR_UNREACH;
  347. __u32 rel_info = 0;
  348. __u16 len;
  349. int err = -ENOENT;
  350. /* If the packet doesn't contain the original IPv6 header we are
  351. in trouble since we might need the source address for further
  352. processing of the error. */
  353. read_lock(&ip6ip6_lock);
  354. if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
  355. goto out;
  356. err = 0;
  357. switch (type) {
  358. __u32 teli;
  359. struct ipv6_tlv_tnl_enc_lim *tel;
  360. __u32 mtu;
  361. case ICMPV6_DEST_UNREACH:
  362. if (net_ratelimit())
  363. printk(KERN_WARNING
  364. "%s: Path to destination invalid "
  365. "or inactive!\n", t->parms.name);
  366. rel_msg = 1;
  367. break;
  368. case ICMPV6_TIME_EXCEED:
  369. if (code == ICMPV6_EXC_HOPLIMIT) {
  370. if (net_ratelimit())
  371. printk(KERN_WARNING
  372. "%s: Too small hop limit or "
  373. "routing loop in tunnel!\n",
  374. t->parms.name);
  375. rel_msg = 1;
  376. }
  377. break;
  378. case ICMPV6_PARAMPROB:
  379. /* ignore if parameter problem not caused by a tunnel
  380. encapsulation limit sub-option */
  381. if (code != ICMPV6_HDR_FIELD) {
  382. break;
  383. }
  384. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  385. if (teli && teli == ntohl(info) - 2) {
  386. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  387. if (tel->encap_limit == 0) {
  388. if (net_ratelimit())
  389. printk(KERN_WARNING
  390. "%s: Too small encapsulation "
  391. "limit or routing loop in "
  392. "tunnel!\n", t->parms.name);
  393. rel_msg = 1;
  394. }
  395. }
  396. break;
  397. case ICMPV6_PKT_TOOBIG:
  398. mtu = ntohl(info) - offset;
  399. if (mtu < IPV6_MIN_MTU)
  400. mtu = IPV6_MIN_MTU;
  401. t->dev->mtu = mtu;
  402. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  403. rel_type = ICMPV6_PKT_TOOBIG;
  404. rel_code = 0;
  405. rel_info = mtu;
  406. rel_msg = 1;
  407. }
  408. break;
  409. }
  410. if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
  411. struct rt6_info *rt;
  412. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  413. if (!skb2)
  414. goto out;
  415. dst_release(skb2->dst);
  416. skb2->dst = NULL;
  417. skb_pull(skb2, offset);
  418. skb2->nh.raw = skb2->data;
  419. /* Try to guess incoming interface */
  420. rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
  421. if (rt && rt->rt6i_dev)
  422. skb2->dev = rt->rt6i_dev;
  423. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  424. if (rt)
  425. dst_release(&rt->u.dst);
  426. kfree_skb(skb2);
  427. }
  428. out:
  429. read_unlock(&ip6ip6_lock);
  430. return err;
  431. }
  432. static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
  433. struct sk_buff *skb)
  434. {
  435. struct ipv6hdr *inner_iph = skb->nh.ipv6h;
  436. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  437. IP6_ECN_set_ce(inner_iph);
  438. }
  439. /**
  440. * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
  441. * @skb: received socket buffer
  442. *
  443. * Return: 0
  444. **/
  445. static int
  446. ip6ip6_rcv(struct sk_buff *skb)
  447. {
  448. struct ipv6hdr *ipv6h;
  449. struct ip6_tnl *t;
  450. ipv6h = skb->nh.ipv6h;
  451. read_lock(&ip6ip6_lock);
  452. if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
  453. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  454. read_unlock(&ip6ip6_lock);
  455. goto discard;
  456. }
  457. if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
  458. t->stat.rx_dropped++;
  459. read_unlock(&ip6ip6_lock);
  460. goto discard;
  461. }
  462. secpath_reset(skb);
  463. skb->mac.raw = skb->nh.raw;
  464. skb->nh.raw = skb->data;
  465. skb->protocol = htons(ETH_P_IPV6);
  466. skb->pkt_type = PACKET_HOST;
  467. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  468. skb->dev = t->dev;
  469. dst_release(skb->dst);
  470. skb->dst = NULL;
  471. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  472. ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
  473. ip6ip6_ecn_decapsulate(ipv6h, skb);
  474. t->stat.rx_packets++;
  475. t->stat.rx_bytes += skb->len;
  476. netif_rx(skb);
  477. read_unlock(&ip6ip6_lock);
  478. return 0;
  479. }
  480. read_unlock(&ip6ip6_lock);
  481. return 1;
  482. discard:
  483. kfree_skb(skb);
  484. return 0;
  485. }
  486. static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
  487. {
  488. struct ipv6_tlv_tnl_enc_lim *tel;
  489. struct ipv6_txoptions *opt;
  490. __u8 *raw;
  491. int opt_len = sizeof(*opt) + 8;
  492. if (!(opt = kzalloc(opt_len, GFP_ATOMIC))) {
  493. return NULL;
  494. }
  495. opt->tot_len = opt_len;
  496. opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
  497. opt->opt_nflen = 8;
  498. tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
  499. tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
  500. tel->length = 1;
  501. tel->encap_limit = encap_limit;
  502. raw = (__u8 *) opt->dst0opt;
  503. raw[5] = IPV6_TLV_PADN;
  504. raw[6] = 1;
  505. return opt;
  506. }
  507. /**
  508. * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  509. * @t: the outgoing tunnel device
  510. * @hdr: IPv6 header from the incoming packet
  511. *
  512. * Description:
  513. * Avoid trivial tunneling loop by checking that tunnel exit-point
  514. * doesn't match source of incoming packet.
  515. *
  516. * Return:
  517. * 1 if conflict,
  518. * 0 else
  519. **/
  520. static inline int
  521. ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  522. {
  523. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  524. }
  525. /**
  526. * ip6ip6_tnl_xmit - encapsulate packet and send
  527. * @skb: the outgoing socket buffer
  528. * @dev: the outgoing tunnel device
  529. *
  530. * Description:
  531. * Build new header and do some sanity checks on the packet before sending
  532. * it.
  533. *
  534. * Return:
  535. * 0
  536. **/
  537. static int
  538. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  539. {
  540. struct ip6_tnl *t = netdev_priv(dev);
  541. struct net_device_stats *stats = &t->stat;
  542. struct ipv6hdr *ipv6h = skb->nh.ipv6h;
  543. struct ipv6_txoptions *opt = NULL;
  544. int encap_limit = -1;
  545. __u16 offset;
  546. struct flowi fl;
  547. struct dst_entry *dst;
  548. struct net_device *tdev;
  549. int mtu;
  550. int max_headroom = sizeof(struct ipv6hdr);
  551. u8 proto;
  552. int err;
  553. int pkt_len;
  554. int dsfield;
  555. if (t->recursion++) {
  556. stats->collisions++;
  557. goto tx_err;
  558. }
  559. if (skb->protocol != htons(ETH_P_IPV6) ||
  560. !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
  561. ip6ip6_tnl_addr_conflict(t, ipv6h)) {
  562. goto tx_err;
  563. }
  564. if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
  565. struct ipv6_tlv_tnl_enc_lim *tel;
  566. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
  567. if (tel->encap_limit == 0) {
  568. icmpv6_send(skb, ICMPV6_PARAMPROB,
  569. ICMPV6_HDR_FIELD, offset + 2, skb->dev);
  570. goto tx_err;
  571. }
  572. encap_limit = tel->encap_limit - 1;
  573. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  574. encap_limit = t->parms.encap_limit;
  575. }
  576. memcpy(&fl, &t->fl, sizeof (fl));
  577. proto = fl.proto;
  578. dsfield = ipv6_get_dsfield(ipv6h);
  579. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  580. fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
  581. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  582. fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  583. if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
  584. goto tx_err;
  585. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  586. dst_hold(dst);
  587. else {
  588. dst = ip6_route_output(NULL, &fl);
  589. if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
  590. goto tx_err_link_failure;
  591. }
  592. tdev = dst->dev;
  593. if (tdev == dev) {
  594. stats->collisions++;
  595. if (net_ratelimit())
  596. printk(KERN_WARNING
  597. "%s: Local routing loop detected!\n",
  598. t->parms.name);
  599. goto tx_err_dst_release;
  600. }
  601. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  602. if (opt) {
  603. max_headroom += 8;
  604. mtu -= 8;
  605. }
  606. if (mtu < IPV6_MIN_MTU)
  607. mtu = IPV6_MIN_MTU;
  608. if (skb->dst && mtu < dst_mtu(skb->dst)) {
  609. struct rt6_info *rt = (struct rt6_info *) skb->dst;
  610. rt->rt6i_flags |= RTF_MODIFIED;
  611. rt->u.dst.metrics[RTAX_MTU-1] = mtu;
  612. }
  613. if (skb->len > mtu) {
  614. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  615. goto tx_err_dst_release;
  616. }
  617. /*
  618. * Okay, now see if we can stuff it in the buffer as-is.
  619. */
  620. max_headroom += LL_RESERVED_SPACE(tdev);
  621. if (skb_headroom(skb) < max_headroom ||
  622. skb_cloned(skb) || skb_shared(skb)) {
  623. struct sk_buff *new_skb;
  624. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  625. goto tx_err_dst_release;
  626. if (skb->sk)
  627. skb_set_owner_w(new_skb, skb->sk);
  628. kfree_skb(skb);
  629. skb = new_skb;
  630. }
  631. dst_release(skb->dst);
  632. skb->dst = dst_clone(dst);
  633. skb->h.raw = skb->nh.raw;
  634. if (opt)
  635. ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
  636. skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
  637. ipv6h = skb->nh.ipv6h;
  638. *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
  639. dsfield = INET_ECN_encapsulate(0, dsfield);
  640. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  641. ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  642. ipv6h->hop_limit = t->parms.hop_limit;
  643. ipv6h->nexthdr = proto;
  644. ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
  645. ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
  646. nf_reset(skb);
  647. pkt_len = skb->len;
  648. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
  649. skb->dst->dev, dst_output);
  650. if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
  651. stats->tx_bytes += pkt_len;
  652. stats->tx_packets++;
  653. } else {
  654. stats->tx_errors++;
  655. stats->tx_aborted_errors++;
  656. }
  657. ip6_tnl_dst_store(t, dst);
  658. kfree(opt);
  659. t->recursion--;
  660. return 0;
  661. tx_err_link_failure:
  662. stats->tx_carrier_errors++;
  663. dst_link_failure(skb);
  664. tx_err_dst_release:
  665. dst_release(dst);
  666. kfree(opt);
  667. tx_err:
  668. stats->tx_errors++;
  669. stats->tx_dropped++;
  670. kfree_skb(skb);
  671. t->recursion--;
  672. return 0;
  673. }
  674. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  675. {
  676. struct ip6_tnl_parm *p = &t->parms;
  677. struct in6_addr *laddr = &p->laddr;
  678. struct in6_addr *raddr = &p->raddr;
  679. int ltype = ipv6_addr_type(laddr);
  680. int rtype = ipv6_addr_type(raddr);
  681. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  682. if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
  683. ((ltype|rtype) &
  684. (IPV6_ADDR_UNICAST|
  685. IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
  686. IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
  687. struct net_device *ldev = NULL;
  688. int l_ok = 1;
  689. int r_ok = 1;
  690. if (p->link)
  691. ldev = dev_get_by_index(p->link);
  692. if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
  693. l_ok = 0;
  694. if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
  695. r_ok = 0;
  696. if (l_ok && r_ok) {
  697. if (ltype&IPV6_ADDR_UNICAST)
  698. p->flags |= IP6_TNL_F_CAP_XMIT;
  699. if (rtype&IPV6_ADDR_UNICAST)
  700. p->flags |= IP6_TNL_F_CAP_RCV;
  701. }
  702. if (ldev)
  703. dev_put(ldev);
  704. }
  705. }
  706. static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
  707. {
  708. struct net_device *dev = t->dev;
  709. struct ip6_tnl_parm *p = &t->parms;
  710. struct flowi *fl = &t->fl;
  711. memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  712. memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  713. /* Set up flowi template */
  714. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  715. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  716. fl->oif = p->link;
  717. fl->fl6_flowlabel = 0;
  718. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  719. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  720. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  721. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  722. ip6_tnl_set_cap(t);
  723. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  724. dev->flags |= IFF_POINTOPOINT;
  725. else
  726. dev->flags &= ~IFF_POINTOPOINT;
  727. dev->iflink = p->link;
  728. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  729. struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
  730. p->link, 0);
  731. if (rt == NULL)
  732. return;
  733. if (rt->rt6i_dev) {
  734. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  735. sizeof (struct ipv6hdr);
  736. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  737. if (dev->mtu < IPV6_MIN_MTU)
  738. dev->mtu = IPV6_MIN_MTU;
  739. }
  740. dst_release(&rt->u.dst);
  741. }
  742. }
  743. /**
  744. * ip6ip6_tnl_change - update the tunnel parameters
  745. * @t: tunnel to be changed
  746. * @p: tunnel configuration parameters
  747. * @active: != 0 if tunnel is ready for use
  748. *
  749. * Description:
  750. * ip6ip6_tnl_change() updates the tunnel parameters
  751. **/
  752. static int
  753. ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  754. {
  755. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  756. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  757. t->parms.flags = p->flags;
  758. t->parms.hop_limit = p->hop_limit;
  759. t->parms.encap_limit = p->encap_limit;
  760. t->parms.flowinfo = p->flowinfo;
  761. t->parms.link = p->link;
  762. ip6_tnl_dst_reset(t);
  763. ip6ip6_tnl_link_config(t);
  764. return 0;
  765. }
  766. /**
  767. * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  768. * @dev: virtual device associated with tunnel
  769. * @ifr: parameters passed from userspace
  770. * @cmd: command to be performed
  771. *
  772. * Description:
  773. * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
  774. * from userspace.
  775. *
  776. * The possible commands are the following:
  777. * %SIOCGETTUNNEL: get tunnel parameters for device
  778. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  779. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  780. * %SIOCDELTUNNEL: delete tunnel
  781. *
  782. * The fallback device "ip6tnl0", created during module
  783. * initialization, can be used for creating other tunnel devices.
  784. *
  785. * Return:
  786. * 0 on success,
  787. * %-EFAULT if unable to copy data to or from userspace,
  788. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  789. * %-EINVAL if passed tunnel parameters are invalid,
  790. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  791. * %-ENODEV if attempting to change or delete a nonexisting device
  792. **/
  793. static int
  794. ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  795. {
  796. int err = 0;
  797. int create;
  798. struct ip6_tnl_parm p;
  799. struct ip6_tnl *t = NULL;
  800. switch (cmd) {
  801. case SIOCGETTUNNEL:
  802. if (dev == ip6ip6_fb_tnl_dev) {
  803. if (copy_from_user(&p,
  804. ifr->ifr_ifru.ifru_data,
  805. sizeof (p))) {
  806. err = -EFAULT;
  807. break;
  808. }
  809. if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
  810. t = netdev_priv(dev);
  811. else if (err)
  812. break;
  813. } else
  814. t = netdev_priv(dev);
  815. memcpy(&p, &t->parms, sizeof (p));
  816. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  817. err = -EFAULT;
  818. }
  819. break;
  820. case SIOCADDTUNNEL:
  821. case SIOCCHGTUNNEL:
  822. err = -EPERM;
  823. create = (cmd == SIOCADDTUNNEL);
  824. if (!capable(CAP_NET_ADMIN))
  825. break;
  826. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  827. err = -EFAULT;
  828. break;
  829. }
  830. if (!create && dev != ip6ip6_fb_tnl_dev) {
  831. t = netdev_priv(dev);
  832. }
  833. if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
  834. break;
  835. }
  836. if (cmd == SIOCCHGTUNNEL) {
  837. if (t->dev != dev) {
  838. err = -EEXIST;
  839. break;
  840. }
  841. ip6ip6_tnl_unlink(t);
  842. err = ip6ip6_tnl_change(t, &p);
  843. ip6ip6_tnl_link(t);
  844. netdev_state_change(dev);
  845. }
  846. if (copy_to_user(ifr->ifr_ifru.ifru_data,
  847. &t->parms, sizeof (p))) {
  848. err = -EFAULT;
  849. } else {
  850. err = 0;
  851. }
  852. break;
  853. case SIOCDELTUNNEL:
  854. err = -EPERM;
  855. if (!capable(CAP_NET_ADMIN))
  856. break;
  857. if (dev == ip6ip6_fb_tnl_dev) {
  858. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  859. sizeof (p))) {
  860. err = -EFAULT;
  861. break;
  862. }
  863. err = ip6ip6_tnl_locate(&p, &t, 0);
  864. if (err)
  865. break;
  866. if (t == netdev_priv(ip6ip6_fb_tnl_dev)) {
  867. err = -EPERM;
  868. break;
  869. }
  870. } else {
  871. t = netdev_priv(dev);
  872. }
  873. err = unregister_netdevice(t->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)) {
  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);
  1001. return err;
  1002. }
  1003. /**
  1004. * ip6_tunnel_cleanup - free resources and unregister protocol
  1005. **/
  1006. static void __exit ip6_tunnel_cleanup(void)
  1007. {
  1008. if (xfrm6_tunnel_deregister(&ip6ip6_handler))
  1009. printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
  1010. unregister_netdev(ip6ip6_fb_tnl_dev);
  1011. }
  1012. module_init(ip6_tunnel_init);
  1013. module_exit(ip6_tunnel_cleanup);