ip6_tunnel.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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) + 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. ip6ip6_tnl_link_config(t);
  766. return 0;
  767. }
  768. /**
  769. * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  770. * @dev: virtual device associated with tunnel
  771. * @ifr: parameters passed from userspace
  772. * @cmd: command to be performed
  773. *
  774. * Description:
  775. * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
  776. * from userspace.
  777. *
  778. * The possible commands are the following:
  779. * %SIOCGETTUNNEL: get tunnel parameters for device
  780. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  781. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  782. * %SIOCDELTUNNEL: delete tunnel
  783. *
  784. * The fallback device "ip6tnl0", created during module
  785. * initialization, can be used for creating other tunnel devices.
  786. *
  787. * Return:
  788. * 0 on success,
  789. * %-EFAULT if unable to copy data to or from userspace,
  790. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  791. * %-EINVAL if passed tunnel parameters are invalid,
  792. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  793. * %-ENODEV if attempting to change or delete a nonexisting device
  794. **/
  795. static int
  796. ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  797. {
  798. int err = 0;
  799. int create;
  800. struct ip6_tnl_parm p;
  801. struct ip6_tnl *t = NULL;
  802. switch (cmd) {
  803. case SIOCGETTUNNEL:
  804. if (dev == ip6ip6_fb_tnl_dev) {
  805. if (copy_from_user(&p,
  806. ifr->ifr_ifru.ifru_data,
  807. sizeof (p))) {
  808. err = -EFAULT;
  809. break;
  810. }
  811. if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
  812. t = netdev_priv(dev);
  813. else if (err)
  814. break;
  815. } else
  816. t = netdev_priv(dev);
  817. memcpy(&p, &t->parms, sizeof (p));
  818. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  819. err = -EFAULT;
  820. }
  821. break;
  822. case SIOCADDTUNNEL:
  823. case SIOCCHGTUNNEL:
  824. err = -EPERM;
  825. create = (cmd == SIOCADDTUNNEL);
  826. if (!capable(CAP_NET_ADMIN))
  827. break;
  828. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  829. err = -EFAULT;
  830. break;
  831. }
  832. if (!create && dev != ip6ip6_fb_tnl_dev) {
  833. t = netdev_priv(dev);
  834. }
  835. if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
  836. break;
  837. }
  838. if (cmd == SIOCCHGTUNNEL) {
  839. if (t->dev != dev) {
  840. err = -EEXIST;
  841. break;
  842. }
  843. ip6ip6_tnl_unlink(t);
  844. err = ip6ip6_tnl_change(t, &p);
  845. ip6ip6_tnl_link(t);
  846. netdev_state_change(dev);
  847. }
  848. if (copy_to_user(ifr->ifr_ifru.ifru_data,
  849. &t->parms, sizeof (p))) {
  850. err = -EFAULT;
  851. } else {
  852. err = 0;
  853. }
  854. break;
  855. case SIOCDELTUNNEL:
  856. err = -EPERM;
  857. if (!capable(CAP_NET_ADMIN))
  858. break;
  859. if (dev == ip6ip6_fb_tnl_dev) {
  860. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
  861. sizeof (p))) {
  862. err = -EFAULT;
  863. break;
  864. }
  865. err = ip6ip6_tnl_locate(&p, &t, 0);
  866. if (err)
  867. break;
  868. if (t == netdev_priv(ip6ip6_fb_tnl_dev)) {
  869. err = -EPERM;
  870. break;
  871. }
  872. } else {
  873. t = netdev_priv(dev);
  874. }
  875. err = unregister_netdevice(t->dev);
  876. break;
  877. default:
  878. err = -EINVAL;
  879. }
  880. return err;
  881. }
  882. /**
  883. * ip6ip6_tnl_get_stats - return the stats for tunnel device
  884. * @dev: virtual device associated with tunnel
  885. *
  886. * Return: stats for device
  887. **/
  888. static struct net_device_stats *
  889. ip6ip6_tnl_get_stats(struct net_device *dev)
  890. {
  891. return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
  892. }
  893. /**
  894. * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
  895. * @dev: virtual device associated with tunnel
  896. * @new_mtu: the new mtu
  897. *
  898. * Return:
  899. * 0 on success,
  900. * %-EINVAL if mtu too small
  901. **/
  902. static int
  903. ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  904. {
  905. if (new_mtu < IPV6_MIN_MTU) {
  906. return -EINVAL;
  907. }
  908. dev->mtu = new_mtu;
  909. return 0;
  910. }
  911. /**
  912. * ip6ip6_tnl_dev_setup - setup virtual tunnel device
  913. * @dev: virtual device associated with tunnel
  914. *
  915. * Description:
  916. * Initialize function pointers and device parameters
  917. **/
  918. static void ip6ip6_tnl_dev_setup(struct net_device *dev)
  919. {
  920. SET_MODULE_OWNER(dev);
  921. dev->uninit = ip6ip6_tnl_dev_uninit;
  922. dev->destructor = free_netdev;
  923. dev->hard_start_xmit = ip6ip6_tnl_xmit;
  924. dev->get_stats = ip6ip6_tnl_get_stats;
  925. dev->do_ioctl = ip6ip6_tnl_ioctl;
  926. dev->change_mtu = ip6ip6_tnl_change_mtu;
  927. dev->type = ARPHRD_TUNNEL6;
  928. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  929. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  930. dev->flags |= IFF_NOARP;
  931. dev->addr_len = sizeof(struct in6_addr);
  932. }
  933. /**
  934. * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  935. * @dev: virtual device associated with tunnel
  936. **/
  937. static inline void
  938. ip6ip6_tnl_dev_init_gen(struct net_device *dev)
  939. {
  940. struct ip6_tnl *t = netdev_priv(dev);
  941. t->fl.proto = IPPROTO_IPV6;
  942. t->dev = dev;
  943. strcpy(t->parms.name, dev->name);
  944. }
  945. /**
  946. * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  947. * @dev: virtual device associated with tunnel
  948. **/
  949. static int
  950. ip6ip6_tnl_dev_init(struct net_device *dev)
  951. {
  952. struct ip6_tnl *t = netdev_priv(dev);
  953. ip6ip6_tnl_dev_init_gen(dev);
  954. ip6ip6_tnl_link_config(t);
  955. return 0;
  956. }
  957. /**
  958. * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  959. * @dev: fallback device
  960. *
  961. * Return: 0
  962. **/
  963. static int
  964. ip6ip6_fb_tnl_dev_init(struct net_device *dev)
  965. {
  966. struct ip6_tnl *t = netdev_priv(dev);
  967. ip6ip6_tnl_dev_init_gen(dev);
  968. dev_hold(dev);
  969. tnls_wc[0] = t;
  970. return 0;
  971. }
  972. #ifdef CONFIG_INET6_TUNNEL
  973. static struct xfrm6_tunnel ip6ip6_handler = {
  974. .handler = ip6ip6_rcv,
  975. .err_handler = ip6ip6_err,
  976. };
  977. static inline int ip6ip6_register(void)
  978. {
  979. return xfrm6_tunnel_register(&ip6ip6_handler);
  980. }
  981. static inline int ip6ip6_unregister(void)
  982. {
  983. return xfrm6_tunnel_deregister(&ip6ip6_handler);
  984. }
  985. #else
  986. static struct inet6_protocol xfrm6_tunnel_protocol = {
  987. .handler = ip6ip6_rcv,
  988. .err_handler = ip6ip6_err,
  989. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  990. };
  991. static inline int ip6ip6_register(void)
  992. {
  993. return inet6_add_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6);
  994. }
  995. static inline int ip6ip6_unregister(void)
  996. {
  997. return inet6_del_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6);
  998. }
  999. #endif
  1000. /**
  1001. * ip6_tunnel_init - register protocol and reserve needed resources
  1002. *
  1003. * Return: 0 on success
  1004. **/
  1005. static int __init ip6_tunnel_init(void)
  1006. {
  1007. int err;
  1008. if (ip6ip6_register() < 0) {
  1009. printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
  1010. return -EAGAIN;
  1011. }
  1012. ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1013. ip6ip6_tnl_dev_setup);
  1014. if (!ip6ip6_fb_tnl_dev) {
  1015. err = -ENOMEM;
  1016. goto fail;
  1017. }
  1018. ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
  1019. if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
  1020. free_netdev(ip6ip6_fb_tnl_dev);
  1021. goto fail;
  1022. }
  1023. return 0;
  1024. fail:
  1025. ip6ip6_unregister();
  1026. return err;
  1027. }
  1028. /**
  1029. * ip6_tunnel_cleanup - free resources and unregister protocol
  1030. **/
  1031. static void __exit ip6_tunnel_cleanup(void)
  1032. {
  1033. if (ip6ip6_unregister() < 0)
  1034. printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
  1035. unregister_netdev(ip6ip6_fb_tnl_dev);
  1036. }
  1037. module_init(ip6_tunnel_init);
  1038. module_exit(ip6_tunnel_cleanup);