ip6_tunnel.c 27 KB

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