ip6_tunnel.c 27 KB

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