ip6_tunnel.c 27 KB

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