initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / ipv6 / ip6_tunnel.c
blobc75dc343bfa629742a03c89d1be451584363d907
1 /*
2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
8 * $Id$
10 * Based on:
11 * linux/net/ipv6/sit.c
13 * RFC 2473
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.
22 #include <linux/config.h>
23 #include <linux/module.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>
41 #include <asm/uaccess.h>
42 #include <asm/atomic.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/protocol.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/xfrm.h>
51 #include <net/dsfield.h>
52 #include <net/inet_ecn.h>
54 MODULE_AUTHOR("Ville Nuorvala");
55 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
56 MODULE_LICENSE("GPL");
58 #define IPV6_TLV_TEL_DST_SIZE 8
60 #ifdef IP6_TNL_DEBUG
61 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
62 #else
63 #define IP6_TNL_TRACE(x...) do {;} while(0)
64 #endif
66 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
68 #define HASH_SIZE 32
70 #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
71 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
72 (HASH_SIZE - 1))
74 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
75 static int ip6ip6_tnl_dev_init(struct net_device *dev);
76 static void ip6ip6_tnl_dev_setup(struct net_device *dev);
78 /* the IPv6 tunnel fallback device */
79 static struct net_device *ip6ip6_fb_tnl_dev;
82 /* lists for storing tunnels in use */
83 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
84 static struct ip6_tnl *tnls_wc[1];
85 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
87 /* lock for the tunnel lists */
88 static rwlock_t ip6ip6_lock = RW_LOCK_UNLOCKED;
90 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
92 struct dst_entry *dst = t->dst_cache;
94 if (dst && dst->obsolete &&
95 dst->ops->check(dst, t->dst_cookie) == NULL) {
96 t->dst_cache = NULL;
97 return NULL;
100 return dst;
103 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
105 dst_release(t->dst_cache);
106 t->dst_cache = NULL;
109 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
111 struct rt6_info *rt = (struct rt6_info *) dst;
112 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
113 dst_release(t->dst_cache);
114 t->dst_cache = dst;
118 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
119 * @remote: the address of the tunnel exit-point
120 * @local: the address of the tunnel entry-point
122 * Return:
123 * tunnel matching given end-points if found,
124 * else fallback tunnel if its device is up,
125 * else %NULL
128 static struct ip6_tnl *
129 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
131 unsigned h0 = HASH(remote);
132 unsigned h1 = HASH(local);
133 struct ip6_tnl *t;
135 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
136 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
137 !ipv6_addr_cmp(remote, &t->parms.raddr) &&
138 (t->dev->flags & IFF_UP))
139 return t;
141 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
142 return t;
144 return NULL;
148 * ip6ip6_bucket - get head of list matching given tunnel parameters
149 * @p: parameters containing tunnel end-points
151 * Description:
152 * ip6ip6_bucket() returns the head of the list matching the
153 * &struct in6_addr entries laddr and raddr in @p.
155 * Return: head of IPv6 tunnel list
158 static struct ip6_tnl **
159 ip6ip6_bucket(struct ip6_tnl_parm *p)
161 struct in6_addr *remote = &p->raddr;
162 struct in6_addr *local = &p->laddr;
163 unsigned h = 0;
164 int prio = 0;
166 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
167 prio = 1;
168 h = HASH(remote) ^ HASH(local);
170 return &tnls[prio][h];
174 * ip6ip6_tnl_link - add tunnel to hash table
175 * @t: tunnel to be added
178 static void
179 ip6ip6_tnl_link(struct ip6_tnl *t)
181 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
183 write_lock_bh(&ip6ip6_lock);
184 t->next = *tp;
185 write_unlock_bh(&ip6ip6_lock);
186 *tp = t;
190 * ip6ip6_tnl_unlink - remove tunnel from hash table
191 * @t: tunnel to be removed
194 static void
195 ip6ip6_tnl_unlink(struct ip6_tnl *t)
197 struct ip6_tnl **tp;
199 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
200 if (t == *tp) {
201 write_lock_bh(&ip6ip6_lock);
202 *tp = t->next;
203 write_unlock_bh(&ip6ip6_lock);
204 break;
210 * ip6_tnl_create() - create a new tunnel
211 * @p: tunnel parameters
212 * @pt: pointer to new tunnel
214 * Description:
215 * Create tunnel matching given parameters.
217 * Return:
218 * 0 on success
221 static int
222 ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
224 struct net_device *dev;
225 struct ip6_tnl *t;
226 char name[IFNAMSIZ];
227 int err;
229 if (p->name[0]) {
230 strlcpy(name, p->name, IFNAMSIZ);
231 } else {
232 int i;
233 for (i = 1; i < IP6_TNL_MAX; i++) {
234 sprintf(name, "ip6tnl%d", i);
235 if (__dev_get_by_name(name) == NULL)
236 break;
238 if (i == IP6_TNL_MAX)
239 return -ENOBUFS;
241 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
242 if (dev == NULL)
243 return -ENOMEM;
245 t = dev->priv;
246 dev->init = ip6ip6_tnl_dev_init;
247 t->parms = *p;
249 if ((err = register_netdevice(dev)) < 0) {
250 free_netdev(dev);
251 return err;
253 dev_hold(dev);
255 ip6ip6_tnl_link(t);
256 *pt = t;
257 return 0;
261 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
262 * @p: tunnel parameters
263 * @create: != 0 if allowed to create new tunnel if no match found
265 * Description:
266 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
267 * based on @parms. If this is unsuccessful, but @create is set a new
268 * tunnel device is created and registered for use.
270 * Return:
271 * 0 if tunnel located or created,
272 * -EINVAL if parameters incorrect,
273 * -ENODEV if no matching tunnel available
276 static int
277 ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
279 struct in6_addr *remote = &p->raddr;
280 struct in6_addr *local = &p->laddr;
281 struct ip6_tnl *t;
283 if (p->proto != IPPROTO_IPV6)
284 return -EINVAL;
286 for (t = *ip6ip6_bucket(p); t; t = t->next) {
287 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
288 !ipv6_addr_cmp(remote, &t->parms.raddr)) {
289 *pt = t;
290 return (create ? -EEXIST : 0);
293 if (!create)
294 return -ENODEV;
296 return ip6_tnl_create(p, pt);
300 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
301 * @dev: the device to be destroyed
303 * Description:
304 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
307 static void
308 ip6ip6_tnl_dev_uninit(struct net_device *dev)
310 struct ip6_tnl *t = dev->priv;
312 if (dev == ip6ip6_fb_tnl_dev) {
313 write_lock_bh(&ip6ip6_lock);
314 tnls_wc[0] = NULL;
315 write_unlock_bh(&ip6ip6_lock);
316 } else {
317 ip6ip6_tnl_unlink(t);
319 ip6_tnl_dst_reset(t);
320 dev_put(dev);
324 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
325 * @skb: received socket buffer
327 * Return:
328 * 0 if none was found,
329 * else index to encapsulation limit
332 static __u16
333 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
335 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
336 __u8 nexthdr = ipv6h->nexthdr;
337 __u16 off = sizeof (*ipv6h);
339 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
340 __u16 optlen = 0;
341 struct ipv6_opt_hdr *hdr;
342 if (raw + off + sizeof (*hdr) > skb->data &&
343 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
344 break;
346 hdr = (struct ipv6_opt_hdr *) (raw + off);
347 if (nexthdr == NEXTHDR_FRAGMENT) {
348 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
349 if (frag_hdr->frag_off)
350 break;
351 optlen = 8;
352 } else if (nexthdr == NEXTHDR_AUTH) {
353 optlen = (hdr->hdrlen + 2) << 2;
354 } else {
355 optlen = ipv6_optlen(hdr);
357 if (nexthdr == NEXTHDR_DEST) {
358 __u16 i = off + 2;
359 while (1) {
360 struct ipv6_tlv_tnl_enc_lim *tel;
362 /* No more room for encapsulation limit */
363 if (i + sizeof (*tel) > off + optlen)
364 break;
366 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
367 /* return index of option if found and valid */
368 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
369 tel->length == 1)
370 return i;
371 /* else jump to next option */
372 if (tel->type)
373 i += tel->length + 2;
374 else
375 i++;
378 nexthdr = hdr->nexthdr;
379 off += optlen;
381 return 0;
385 * ip6ip6_err - tunnel error handler
387 * Description:
388 * ip6ip6_err() should handle errors in the tunnel according
389 * to the specifications in RFC 2473.
392 static void
393 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
394 int type, int code, int offset, __u32 info)
396 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
397 struct ip6_tnl *t;
398 int rel_msg = 0;
399 int rel_type = ICMPV6_DEST_UNREACH;
400 int rel_code = ICMPV6_ADDR_UNREACH;
401 __u32 rel_info = 0;
402 __u16 len;
404 /* If the packet doesn't contain the original IPv6 header we are
405 in trouble since we might need the source address for further
406 processing of the error. */
408 read_lock(&ip6ip6_lock);
409 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
410 goto out;
412 switch (type) {
413 __u32 teli;
414 struct ipv6_tlv_tnl_enc_lim *tel;
415 __u32 mtu;
416 case ICMPV6_DEST_UNREACH:
417 if (net_ratelimit())
418 printk(KERN_WARNING
419 "%s: Path to destination invalid "
420 "or inactive!\n", t->parms.name);
421 rel_msg = 1;
422 break;
423 case ICMPV6_TIME_EXCEED:
424 if (code == ICMPV6_EXC_HOPLIMIT) {
425 if (net_ratelimit())
426 printk(KERN_WARNING
427 "%s: Too small hop limit or "
428 "routing loop in tunnel!\n",
429 t->parms.name);
430 rel_msg = 1;
432 break;
433 case ICMPV6_PARAMPROB:
434 /* ignore if parameter problem not caused by a tunnel
435 encapsulation limit sub-option */
436 if (code != ICMPV6_HDR_FIELD) {
437 break;
439 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
441 if (teli && teli == ntohl(info) - 2) {
442 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
443 if (tel->encap_limit == 0) {
444 if (net_ratelimit())
445 printk(KERN_WARNING
446 "%s: Too small encapsulation "
447 "limit or routing loop in "
448 "tunnel!\n", t->parms.name);
449 rel_msg = 1;
452 break;
453 case ICMPV6_PKT_TOOBIG:
454 mtu = ntohl(info) - offset;
455 if (mtu < IPV6_MIN_MTU)
456 mtu = IPV6_MIN_MTU;
457 t->dev->mtu = mtu;
459 if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
460 rel_type = ICMPV6_PKT_TOOBIG;
461 rel_code = 0;
462 rel_info = mtu;
463 rel_msg = 1;
465 break;
467 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
468 struct rt6_info *rt;
469 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
470 if (!skb2)
471 goto out;
473 dst_release(skb2->dst);
474 skb2->dst = NULL;
475 skb_pull(skb2, offset);
476 skb2->nh.raw = skb2->data;
478 /* Try to guess incoming interface */
479 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
481 if (rt && rt->rt6i_dev)
482 skb2->dev = rt->rt6i_dev;
484 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
486 if (rt)
487 dst_release(&rt->u.dst);
489 kfree_skb(skb2);
491 out:
492 read_unlock(&ip6ip6_lock);
495 static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
496 struct sk_buff *skb)
498 struct ipv6hdr *inner_iph = skb->nh.ipv6h;
500 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
501 IP6_ECN_set_ce(inner_iph);
505 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
506 * @skb: received socket buffer
508 * Return: 0
511 static int
512 ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
514 struct sk_buff *skb = *pskb;
515 struct ipv6hdr *ipv6h;
516 struct ip6_tnl *t;
518 if (!pskb_may_pull(skb, sizeof (*ipv6h)))
519 goto discard;
521 ipv6h = skb->nh.ipv6h;
523 read_lock(&ip6ip6_lock);
525 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
526 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
527 kfree_skb(skb);
528 return 0;
531 if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
532 t->stat.rx_dropped++;
533 read_unlock(&ip6ip6_lock);
534 goto discard;
536 secpath_reset(skb);
537 skb->mac.raw = skb->nh.raw;
538 skb->nh.raw = skb->data;
539 skb->protocol = htons(ETH_P_IPV6);
540 skb->pkt_type = PACKET_HOST;
541 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
542 skb->dev = t->dev;
543 dst_release(skb->dst);
544 skb->dst = NULL;
545 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
546 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
547 ip6ip6_ecn_decapsulate(ipv6h, skb);
548 t->stat.rx_packets++;
549 t->stat.rx_bytes += skb->len;
550 netif_rx(skb);
551 read_unlock(&ip6ip6_lock);
552 return 0;
554 read_unlock(&ip6ip6_lock);
555 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
556 discard:
557 return 1;
560 static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
562 struct ipv6_tlv_tnl_enc_lim *tel;
563 struct ipv6_txoptions *opt;
564 __u8 *raw;
566 int opt_len = sizeof(*opt) + 8;
568 if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
569 return NULL;
571 memset(opt, 0, opt_len);
572 opt->tot_len = opt_len;
573 opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
574 opt->opt_nflen = 8;
576 tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
577 tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
578 tel->length = 1;
579 tel->encap_limit = encap_limit;
581 raw = (__u8 *) opt->dst0opt;
582 raw[5] = IPV6_TLV_PADN;
583 raw[6] = 1;
585 return opt;
589 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
590 * @t: the outgoing tunnel device
591 * @hdr: IPv6 header from the incoming packet
593 * Description:
594 * Avoid trivial tunneling loop by checking that tunnel exit-point
595 * doesn't match source of incoming packet.
597 * Return:
598 * 1 if conflict,
599 * 0 else
602 static inline int
603 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
605 return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);
609 * ip6ip6_tnl_xmit - encapsulate packet and send
610 * @skb: the outgoing socket buffer
611 * @dev: the outgoing tunnel device
613 * Description:
614 * Build new header and do some sanity checks on the packet before sending
615 * it.
617 * Return:
621 static int
622 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
624 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
625 struct net_device_stats *stats = &t->stat;
626 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
627 struct ipv6_txoptions *opt = NULL;
628 int encap_limit = -1;
629 __u16 offset;
630 struct flowi fl;
631 struct dst_entry *dst;
632 struct net_device *tdev;
633 int mtu;
634 int max_headroom = sizeof(struct ipv6hdr);
635 u8 proto;
636 int err;
637 int pkt_len;
638 int dsfield;
640 if (t->recursion++) {
641 stats->collisions++;
642 goto tx_err;
644 if (skb->protocol != htons(ETH_P_IPV6) ||
645 !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
646 ip6ip6_tnl_addr_conflict(t, ipv6h)) {
647 goto tx_err;
649 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
650 struct ipv6_tlv_tnl_enc_lim *tel;
651 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
652 if (tel->encap_limit == 0) {
653 icmpv6_send(skb, ICMPV6_PARAMPROB,
654 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
655 goto tx_err;
657 encap_limit = tel->encap_limit - 1;
658 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
659 encap_limit = t->parms.encap_limit;
661 memcpy(&fl, &t->fl, sizeof (fl));
662 proto = fl.proto;
664 dsfield = ipv6_get_dsfield(ipv6h);
665 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
666 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
667 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
668 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
670 if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
671 goto tx_err;
673 if ((dst = ip6_tnl_dst_check(t)) != NULL)
674 dst_hold(dst);
675 else
676 dst = ip6_route_output(NULL, &fl);
678 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
679 goto tx_err_link_failure;
681 tdev = dst->dev;
683 if (tdev == dev) {
684 stats->collisions++;
685 if (net_ratelimit())
686 printk(KERN_WARNING
687 "%s: Local routing loop detected!\n",
688 t->parms.name);
689 goto tx_err_dst_release;
691 mtu = dst_pmtu(dst) - sizeof (*ipv6h);
692 if (opt) {
693 max_headroom += 8;
694 mtu -= 8;
696 if (mtu < IPV6_MIN_MTU)
697 mtu = IPV6_MIN_MTU;
698 if (skb->dst && mtu < dst_pmtu(skb->dst)) {
699 struct rt6_info *rt = (struct rt6_info *) skb->dst;
700 rt->rt6i_flags |= RTF_MODIFIED;
701 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
703 if (skb->len > mtu) {
704 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
705 goto tx_err_dst_release;
709 * Okay, now see if we can stuff it in the buffer as-is.
711 max_headroom += LL_RESERVED_SPACE(tdev);
713 if (skb_headroom(skb) < max_headroom ||
714 skb_cloned(skb) || skb_shared(skb)) {
715 struct sk_buff *new_skb;
717 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
718 goto tx_err_dst_release;
720 if (skb->sk)
721 skb_set_owner_w(new_skb, skb->sk);
722 kfree_skb(skb);
723 skb = new_skb;
725 dst_release(skb->dst);
726 skb->dst = dst_clone(dst);
728 skb->h.raw = skb->nh.raw;
730 if (opt)
731 ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
733 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
734 ipv6h = skb->nh.ipv6h;
735 *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
736 dsfield = INET_ECN_encapsulate(0, dsfield);
737 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
738 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
739 ipv6h->hop_limit = t->parms.hop_limit;
740 ipv6h->nexthdr = proto;
741 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
742 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
743 nf_reset(skb);
744 pkt_len = skb->len;
745 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
746 skb->dst->dev, dst_output);
748 if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
749 stats->tx_bytes += pkt_len;
750 stats->tx_packets++;
751 } else {
752 stats->tx_errors++;
753 stats->tx_aborted_errors++;
755 ip6_tnl_dst_store(t, dst);
757 if (opt)
758 kfree(opt);
760 t->recursion--;
761 return 0;
762 tx_err_link_failure:
763 stats->tx_carrier_errors++;
764 dst_link_failure(skb);
765 tx_err_dst_release:
766 dst_release(dst);
767 if (opt)
768 kfree(opt);
769 tx_err:
770 stats->tx_errors++;
771 stats->tx_dropped++;
772 kfree_skb(skb);
773 t->recursion--;
774 return 0;
777 static void ip6_tnl_set_cap(struct ip6_tnl *t)
779 struct ip6_tnl_parm *p = &t->parms;
780 struct in6_addr *laddr = &p->laddr;
781 struct in6_addr *raddr = &p->raddr;
782 int ltype = ipv6_addr_type(laddr);
783 int rtype = ipv6_addr_type(raddr);
785 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
787 if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
788 ((ltype|rtype) &
789 (IPV6_ADDR_UNICAST|
790 IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
791 IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
792 struct net_device *ldev = NULL;
793 int l_ok = 1;
794 int r_ok = 1;
796 if (p->link)
797 ldev = dev_get_by_index(p->link);
799 if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
800 l_ok = 0;
802 if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
803 r_ok = 0;
805 if (l_ok && r_ok) {
806 if (ltype&IPV6_ADDR_UNICAST)
807 p->flags |= IP6_TNL_F_CAP_XMIT;
808 if (rtype&IPV6_ADDR_UNICAST)
809 p->flags |= IP6_TNL_F_CAP_RCV;
811 if (ldev)
812 dev_put(ldev);
816 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
818 struct net_device *dev = t->dev;
819 struct ip6_tnl_parm *p = &t->parms;
820 struct flowi *fl = &t->fl;
822 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
823 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
825 /* Set up flowi template */
826 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
827 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
828 fl->oif = p->link;
829 fl->fl6_flowlabel = 0;
831 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
832 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
833 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
834 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
836 ip6_tnl_set_cap(t);
838 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
839 dev->flags |= IFF_POINTOPOINT;
840 else
841 dev->flags &= ~IFF_POINTOPOINT;
843 dev->iflink = p->link;
845 if (p->flags & IP6_TNL_F_CAP_XMIT) {
846 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
847 p->link, 0);
849 if (rt == NULL)
850 return;
852 if (rt->rt6i_dev) {
853 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
854 sizeof (struct ipv6hdr);
856 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
858 if (dev->mtu < IPV6_MIN_MTU)
859 dev->mtu = IPV6_MIN_MTU;
861 dst_release(&rt->u.dst);
866 * ip6ip6_tnl_change - update the tunnel parameters
867 * @t: tunnel to be changed
868 * @p: tunnel configuration parameters
869 * @active: != 0 if tunnel is ready for use
871 * Description:
872 * ip6ip6_tnl_change() updates the tunnel parameters
875 static int
876 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
878 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
879 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
880 t->parms.flags = p->flags;
881 t->parms.hop_limit = p->hop_limit;
882 t->parms.encap_limit = p->encap_limit;
883 t->parms.flowinfo = p->flowinfo;
884 ip6ip6_tnl_link_config(t);
885 return 0;
889 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
890 * @dev: virtual device associated with tunnel
891 * @ifr: parameters passed from userspace
892 * @cmd: command to be performed
894 * Description:
895 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
896 * from userspace.
898 * The possible commands are the following:
899 * %SIOCGETTUNNEL: get tunnel parameters for device
900 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
901 * %SIOCCHGTUNNEL: change tunnel parameters to those given
902 * %SIOCDELTUNNEL: delete tunnel
904 * The fallback device "ip6tnl0", created during module
905 * initialization, can be used for creating other tunnel devices.
907 * Return:
908 * 0 on success,
909 * %-EFAULT if unable to copy data to or from userspace,
910 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
911 * %-EINVAL if passed tunnel parameters are invalid,
912 * %-EEXIST if changing a tunnel's parameters would cause a conflict
913 * %-ENODEV if attempting to change or delete a nonexisting device
916 static int
917 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
919 int err = 0;
920 int create;
921 struct ip6_tnl_parm p;
922 struct ip6_tnl *t = NULL;
924 switch (cmd) {
925 case SIOCGETTUNNEL:
926 if (dev == ip6ip6_fb_tnl_dev) {
927 if (copy_from_user(&p,
928 ifr->ifr_ifru.ifru_data,
929 sizeof (p))) {
930 err = -EFAULT;
931 break;
933 if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
934 t = (struct ip6_tnl *) dev->priv;
935 else if (err)
936 break;
937 } else
938 t = (struct ip6_tnl *) dev->priv;
940 memcpy(&p, &t->parms, sizeof (p));
941 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
942 err = -EFAULT;
944 break;
945 case SIOCADDTUNNEL:
946 case SIOCCHGTUNNEL:
947 err = -EPERM;
948 create = (cmd == SIOCADDTUNNEL);
949 if (!capable(CAP_NET_ADMIN))
950 break;
951 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
952 err = -EFAULT;
953 break;
955 if (!create && dev != ip6ip6_fb_tnl_dev) {
956 t = (struct ip6_tnl *) dev->priv;
958 if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
959 break;
961 if (cmd == SIOCCHGTUNNEL) {
962 if (t->dev != dev) {
963 err = -EEXIST;
964 break;
966 ip6ip6_tnl_unlink(t);
967 err = ip6ip6_tnl_change(t, &p);
968 ip6ip6_tnl_link(t);
969 netdev_state_change(dev);
971 if (copy_to_user(ifr->ifr_ifru.ifru_data,
972 &t->parms, sizeof (p))) {
973 err = -EFAULT;
974 } else {
975 err = 0;
977 break;
978 case SIOCDELTUNNEL:
979 err = -EPERM;
980 if (!capable(CAP_NET_ADMIN))
981 break;
983 if (dev == ip6ip6_fb_tnl_dev) {
984 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
985 sizeof (p))) {
986 err = -EFAULT;
987 break;
989 err = ip6ip6_tnl_locate(&p, &t, 0);
990 if (err)
991 break;
992 if (t == ip6ip6_fb_tnl_dev->priv) {
993 err = -EPERM;
994 break;
996 } else {
997 t = (struct ip6_tnl *) dev->priv;
999 err = unregister_netdevice(t->dev);
1000 break;
1001 default:
1002 err = -EINVAL;
1004 return err;
1008 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1009 * @dev: virtual device associated with tunnel
1011 * Return: stats for device
1014 static struct net_device_stats *
1015 ip6ip6_tnl_get_stats(struct net_device *dev)
1017 return &(((struct ip6_tnl *) dev->priv)->stat);
1021 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1022 * @dev: virtual device associated with tunnel
1023 * @new_mtu: the new mtu
1025 * Return:
1026 * 0 on success,
1027 * %-EINVAL if mtu too small
1030 static int
1031 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1033 if (new_mtu < IPV6_MIN_MTU) {
1034 return -EINVAL;
1036 dev->mtu = new_mtu;
1037 return 0;
1041 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1042 * @dev: virtual device associated with tunnel
1044 * Description:
1045 * Initialize function pointers and device parameters
1048 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1050 SET_MODULE_OWNER(dev);
1051 dev->uninit = ip6ip6_tnl_dev_uninit;
1052 dev->destructor = free_netdev;
1053 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1054 dev->get_stats = ip6ip6_tnl_get_stats;
1055 dev->do_ioctl = ip6ip6_tnl_ioctl;
1056 dev->change_mtu = ip6ip6_tnl_change_mtu;
1058 dev->type = ARPHRD_TUNNEL6;
1059 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1060 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1061 dev->flags |= IFF_NOARP;
1062 dev->addr_len = sizeof(struct in6_addr);
1067 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1068 * @dev: virtual device associated with tunnel
1071 static inline void
1072 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1074 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1075 t->fl.proto = IPPROTO_IPV6;
1076 t->dev = dev;
1077 strcpy(t->parms.name, dev->name);
1081 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1082 * @dev: virtual device associated with tunnel
1085 static int
1086 ip6ip6_tnl_dev_init(struct net_device *dev)
1088 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1089 ip6ip6_tnl_dev_init_gen(dev);
1090 ip6ip6_tnl_link_config(t);
1091 return 0;
1095 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1096 * @dev: fallback device
1098 * Return: 0
1101 static int
1102 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1104 struct ip6_tnl *t = dev->priv;
1105 ip6ip6_tnl_dev_init_gen(dev);
1106 dev_hold(dev);
1107 tnls_wc[0] = t;
1108 return 0;
1111 static struct xfrm6_tunnel ip6ip6_handler = {
1112 .handler = ip6ip6_rcv,
1113 .err_handler = ip6ip6_err,
1117 * ip6_tunnel_init - register protocol and reserve needed resources
1119 * Return: 0 on success
1122 static int __init ip6_tunnel_init(void)
1124 int err;
1126 if (xfrm6_tunnel_register(&ip6ip6_handler) < 0) {
1127 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1128 return -EAGAIN;
1130 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1131 ip6ip6_tnl_dev_setup);
1133 if (!ip6ip6_fb_tnl_dev) {
1134 err = -ENOMEM;
1135 goto fail;
1137 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1139 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1140 free_netdev(ip6ip6_fb_tnl_dev);
1141 goto fail;
1143 return 0;
1144 fail:
1145 xfrm6_tunnel_deregister(&ip6ip6_handler);
1146 return err;
1150 * ip6_tunnel_cleanup - free resources and unregister protocol
1153 static void __exit ip6_tunnel_cleanup(void)
1155 if (xfrm6_tunnel_deregister(&ip6ip6_handler) < 0)
1156 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1158 unregister_netdev(ip6ip6_fb_tnl_dev);
1161 module_init(ip6_tunnel_init);
1162 module_exit(ip6_tunnel_cleanup);