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/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/protocol.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 = dev->priv;
  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 = dev->priv;
  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 void
  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. /* If the packet doesn't contain the original IPv6 header we are
  351. in trouble since we might need the source address for further
  352. processing of the error. */
  353. read_lock(&ip6ip6_lock);
  354. if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
  355. goto out;
  356. switch (type) {
  357. __u32 teli;
  358. struct ipv6_tlv_tnl_enc_lim *tel;
  359. __u32 mtu;
  360. case ICMPV6_DEST_UNREACH:
  361. if (net_ratelimit())
  362. printk(KERN_WARNING
  363. "%s: Path to destination invalid "
  364. "or inactive!\n", t->parms.name);
  365. rel_msg = 1;
  366. break;
  367. case ICMPV6_TIME_EXCEED:
  368. if (code == ICMPV6_EXC_HOPLIMIT) {
  369. if (net_ratelimit())
  370. printk(KERN_WARNING
  371. "%s: Too small hop limit or "
  372. "routing loop in tunnel!\n",
  373. t->parms.name);
  374. rel_msg = 1;
  375. }
  376. break;
  377. case ICMPV6_PARAMPROB:
  378. /* ignore if parameter problem not caused by a tunnel
  379. encapsulation limit sub-option */
  380. if (code != ICMPV6_HDR_FIELD) {
  381. break;
  382. }
  383. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  384. if (teli && teli == ntohl(info) - 2) {
  385. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  386. if (tel->encap_limit == 0) {
  387. if (net_ratelimit())
  388. printk(KERN_WARNING
  389. "%s: Too small encapsulation "
  390. "limit or routing loop in "
  391. "tunnel!\n", t->parms.name);
  392. rel_msg = 1;
  393. }
  394. }
  395. break;
  396. case ICMPV6_PKT_TOOBIG:
  397. mtu = ntohl(info) - offset;
  398. if (mtu < IPV6_MIN_MTU)
  399. mtu = IPV6_MIN_MTU;
  400. t->dev->mtu = mtu;
  401. if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
  402. rel_type = ICMPV6_PKT_TOOBIG;
  403. rel_code = 0;
  404. rel_info = mtu;
  405. rel_msg = 1;
  406. }
  407. break;
  408. }
  409. if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
  410. struct rt6_info *rt;
  411. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  412. if (!skb2)
  413. goto out;
  414. dst_release(skb2->dst);
  415. skb2->dst = NULL;
  416. skb_pull(skb2, offset);
  417. skb2->nh.raw = skb2->data;
  418. /* Try to guess incoming interface */
  419. rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
  420. if (rt && rt->rt6i_dev)
  421. skb2->dev = rt->rt6i_dev;
  422. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  423. if (rt)
  424. dst_release(&rt->u.dst);
  425. kfree_skb(skb2);
  426. }
  427. out:
  428. read_unlock(&ip6ip6_lock);
  429. }
  430. static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
  431. struct sk_buff *skb)
  432. {
  433. struct ipv6hdr *inner_iph = skb->nh.ipv6h;
  434. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  435. IP6_ECN_set_ce(inner_iph);
  436. }
  437. /**
  438. * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
  439. * @skb: received socket buffer
  440. *
  441. * Return: 0
  442. **/
  443. static int
  444. ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
  445. {
  446. struct sk_buff *skb = *pskb;
  447. struct ipv6hdr *ipv6h;
  448. struct ip6_tnl *t;
  449. if (!pskb_may_pull(skb, sizeof (*ipv6h)))
  450. goto discard;
  451. ipv6h = skb->nh.ipv6h;
  452. read_lock(&ip6ip6_lock);
  453. if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
  454. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  455. kfree_skb(skb);
  456. return 0;
  457. }
  458. if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
  459. t->stat.rx_dropped++;
  460. read_unlock(&ip6ip6_lock);
  461. goto discard;
  462. }
  463. secpath_reset(skb);
  464. skb->mac.raw = skb->nh.raw;
  465. skb->nh.raw = skb->data;
  466. skb->protocol = htons(ETH_P_IPV6);
  467. skb->pkt_type = PACKET_HOST;
  468. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  469. skb->dev = t->dev;
  470. dst_release(skb->dst);
  471. skb->dst = NULL;
  472. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  473. ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
  474. ip6ip6_ecn_decapsulate(ipv6h, skb);
  475. t->stat.rx_packets++;
  476. t->stat.rx_bytes += skb->len;
  477. netif_rx(skb);
  478. read_unlock(&ip6ip6_lock);
  479. return 0;
  480. }
  481. read_unlock(&ip6ip6_lock);
  482. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
  483. discard:
  484. return 1;
  485. }
  486. static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
  487. {
  488. struct ipv6_tlv_tnl_enc_lim *tel;
  489. struct ipv6_txoptions *opt;
  490. __u8 *raw;
  491. int opt_len = sizeof(*opt) + 8;
  492. if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
  493. return NULL;
  494. }
  495. memset(opt, 0, opt_len);
  496. opt->tot_len = opt_len;
  497. opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
  498. opt->opt_nflen = 8;
  499. tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
  500. tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
  501. tel->length = 1;
  502. tel->encap_limit = encap_limit;
  503. raw = (__u8 *) opt->dst0opt;
  504. raw[5] = IPV6_TLV_PADN;
  505. raw[6] = 1;
  506. return opt;
  507. }
  508. /**
  509. * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  510. * @t: the outgoing tunnel device
  511. * @hdr: IPv6 header from the incoming packet
  512. *
  513. * Description:
  514. * Avoid trivial tunneling loop by checking that tunnel exit-point
  515. * doesn't match source of incoming packet.
  516. *
  517. * Return:
  518. * 1 if conflict,
  519. * 0 else
  520. **/
  521. static inline int
  522. ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  523. {
  524. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  525. }
  526. /**
  527. * ip6ip6_tnl_xmit - encapsulate packet and send
  528. * @skb: the outgoing socket buffer
  529. * @dev: the outgoing tunnel device
  530. *
  531. * Description:
  532. * Build new header and do some sanity checks on the packet before sending
  533. * it.
  534. *
  535. * Return:
  536. * 0
  537. **/
  538. static int
  539. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  540. {
  541. struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
  542. struct net_device_stats *stats = &t->stat;
  543. struct ipv6hdr *ipv6h = skb->nh.ipv6h;
  544. struct ipv6_txoptions *opt = NULL;
  545. int encap_limit = -1;
  546. __u16 offset;
  547. struct flowi fl;
  548. struct dst_entry *dst;
  549. struct net_device *tdev;
  550. int mtu;
  551. int max_headroom = sizeof(struct ipv6hdr);
  552. u8 proto;
  553. int err;
  554. int pkt_len;
  555. int dsfield;
  556. if (t->recursion++) {
  557. stats->collisions++;
  558. goto tx_err;
  559. }
  560. if (skb->protocol != htons(ETH_P_IPV6) ||
  561. !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
  562. ip6ip6_tnl_addr_conflict(t, ipv6h)) {
  563. goto tx_err;
  564. }
  565. if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
  566. struct ipv6_tlv_tnl_enc_lim *tel;
  567. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
  568. if (tel->encap_limit == 0) {
  569. icmpv6_send(skb, ICMPV6_PARAMPROB,
  570. ICMPV6_HDR_FIELD, offset + 2, skb->dev);
  571. goto tx_err;
  572. }
  573. encap_limit = tel->encap_limit - 1;
  574. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  575. encap_limit = t->parms.encap_limit;
  576. }
  577. memcpy(&fl, &t->fl, sizeof (fl));
  578. proto = fl.proto;
  579. dsfield = ipv6_get_dsfield(ipv6h);
  580. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  581. fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
  582. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  583. fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  584. if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
  585. goto tx_err;
  586. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  587. dst_hold(dst);
  588. else {
  589. dst = ip6_route_output(NULL, &fl);
  590. if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
  591. goto tx_err_link_failure;
  592. }
  593. tdev = dst->dev;
  594. if (tdev == dev) {
  595. stats->collisions++;
  596. if (net_ratelimit())
  597. printk(KERN_WARNING
  598. "%s: Local routing loop detected!\n",
  599. t->parms.name);
  600. goto tx_err_dst_release;
  601. }
  602. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  603. if (opt) {
  604. max_headroom += 8;
  605. mtu -= 8;
  606. }
  607. if (mtu < IPV6_MIN_MTU)
  608. mtu = IPV6_MIN_MTU;
  609. if (skb->dst && mtu < dst_mtu(skb->dst)) {
  610. struct rt6_info *rt = (struct rt6_info *) skb->dst;
  611. rt->rt6i_flags |= RTF_MODIFIED;
  612. rt->u.dst.metrics[RTAX_MTU-1] = mtu;
  613. }
  614. if (skb->len > mtu) {
  615. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  616. goto tx_err_dst_release;
  617. }
  618. /*
  619. * Okay, now see if we can stuff it in the buffer as-is.
  620. */
  621. max_headroom += LL_RESERVED_SPACE(tdev);
  622. if (skb_headroom(skb) < max_headroom ||
  623. skb_cloned(skb) || skb_shared(skb)) {
  624. struct sk_buff *new_skb;
  625. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  626. goto tx_err_dst_release;
  627. if (skb->sk)
  628. skb_set_owner_w(new_skb, skb->sk);
  629. kfree_skb(skb);
  630. skb = new_skb;
  631. }
  632. dst_release(skb->dst);
  633. skb->dst = dst_clone(dst);
  634. skb->h.raw = skb->nh.raw;
  635. if (opt)
  636. ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
  637. skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
  638. ipv6h = skb->nh.ipv6h;
  639. *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
  640. dsfield = INET_ECN_encapsulate(0, dsfield);
  641. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  642. ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  643. ipv6h->hop_limit = t->parms.hop_limit;
  644. ipv6h->nexthdr = proto;
  645. ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
  646. ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
  647. nf_reset(skb);
  648. pkt_len = skb->len;
  649. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
  650. skb->dst->dev, dst_output);
  651. if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
  652. stats->tx_bytes += pkt_len;
  653. stats->tx_packets++;
  654. } else {
  655. stats->tx_errors++;
  656. stats->tx_aborted_errors++;
  657. }
  658. ip6_tnl_dst_store(t, dst);
  659. if (opt)
  660. kfree(opt);
  661. t->recursion--;
  662. return 0;
  663. tx_err_link_failure:
  664. stats->tx_carrier_errors++;
  665. dst_link_failure(skb);
  666. tx_err_dst_release:
  667. dst_release(dst);
  668. if (opt)
  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 = (struct ip6_tnl *) dev->priv;
  813. else if (err)
  814. break;
  815. } else
  816. t = (struct ip6_tnl *) dev->priv;
  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 = (struct ip6_tnl *) dev->priv;
  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 == ip6ip6_fb_tnl_dev->priv) {
  869. err = -EPERM;
  870. break;
  871. }
  872. } else {
  873. t = (struct ip6_tnl *) dev->priv;
  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 *) dev->priv)->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 = (struct ip6_tnl *) dev->priv;
  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 = (struct ip6_tnl *) dev->priv;
  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 = dev->priv;
  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);