ip6_tunnel.c 27 KB

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