ip6_tunnel.c 27 KB

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