ip6_tunnel.c 27 KB

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