Linux v2.6.17-rc2
[linux-2.6/next.git] / net / ipv6 / ip6_tunnel.c
bloba995796b5a577fb7b0f4a947d54a85897143a1ab
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/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/if.h>
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/if_tunnel.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
42 #include <asm/uaccess.h>
43 #include <asm/atomic.h>
45 #include <net/ip.h>
46 #include <net/ipv6.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 DEFINE_RWLOCK(ip6ip6_lock);
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 dst_release(dst);
98 return NULL;
101 return dst;
104 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
106 dst_release(t->dst_cache);
107 t->dst_cache = NULL;
110 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
112 struct rt6_info *rt = (struct rt6_info *) dst;
113 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
114 dst_release(t->dst_cache);
115 t->dst_cache = dst;
119 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
120 * @remote: the address of the tunnel exit-point
121 * @local: the address of the tunnel entry-point
123 * Return:
124 * tunnel matching given end-points if found,
125 * else fallback tunnel if its device is up,
126 * else %NULL
129 static struct ip6_tnl *
130 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
132 unsigned h0 = HASH(remote);
133 unsigned h1 = HASH(local);
134 struct ip6_tnl *t;
136 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
137 if (ipv6_addr_equal(local, &t->parms.laddr) &&
138 ipv6_addr_equal(remote, &t->parms.raddr) &&
139 (t->dev->flags & IFF_UP))
140 return t;
142 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
143 return t;
145 return NULL;
149 * ip6ip6_bucket - get head of list matching given tunnel parameters
150 * @p: parameters containing tunnel end-points
152 * Description:
153 * ip6ip6_bucket() returns the head of the list matching the
154 * &struct in6_addr entries laddr and raddr in @p.
156 * Return: head of IPv6 tunnel list
159 static struct ip6_tnl **
160 ip6ip6_bucket(struct ip6_tnl_parm *p)
162 struct in6_addr *remote = &p->raddr;
163 struct in6_addr *local = &p->laddr;
164 unsigned h = 0;
165 int prio = 0;
167 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
168 prio = 1;
169 h = HASH(remote) ^ HASH(local);
171 return &tnls[prio][h];
175 * ip6ip6_tnl_link - add tunnel to hash table
176 * @t: tunnel to be added
179 static void
180 ip6ip6_tnl_link(struct ip6_tnl *t)
182 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
184 t->next = *tp;
185 write_lock_bh(&ip6ip6_lock);
186 *tp = t;
187 write_unlock_bh(&ip6ip6_lock);
191 * ip6ip6_tnl_unlink - remove tunnel from hash table
192 * @t: tunnel to be removed
195 static void
196 ip6ip6_tnl_unlink(struct ip6_tnl *t)
198 struct ip6_tnl **tp;
200 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
201 if (t == *tp) {
202 write_lock_bh(&ip6ip6_lock);
203 *tp = t->next;
204 write_unlock_bh(&ip6ip6_lock);
205 break;
211 * ip6_tnl_create() - create a new tunnel
212 * @p: tunnel parameters
213 * @pt: pointer to new tunnel
215 * Description:
216 * Create tunnel matching given parameters.
218 * Return:
219 * 0 on success
222 static int
223 ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
225 struct net_device *dev;
226 struct ip6_tnl *t;
227 char name[IFNAMSIZ];
228 int err;
230 if (p->name[0]) {
231 strlcpy(name, p->name, IFNAMSIZ);
232 } else {
233 int i;
234 for (i = 1; i < IP6_TNL_MAX; i++) {
235 sprintf(name, "ip6tnl%d", i);
236 if (__dev_get_by_name(name) == NULL)
237 break;
239 if (i == IP6_TNL_MAX)
240 return -ENOBUFS;
242 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
243 if (dev == NULL)
244 return -ENOMEM;
246 t = netdev_priv(dev);
247 dev->init = ip6ip6_tnl_dev_init;
248 t->parms = *p;
250 if ((err = register_netdevice(dev)) < 0) {
251 free_netdev(dev);
252 return err;
254 dev_hold(dev);
256 ip6ip6_tnl_link(t);
257 *pt = t;
258 return 0;
262 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
263 * @p: tunnel parameters
264 * @create: != 0 if allowed to create new tunnel if no match found
266 * Description:
267 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
268 * based on @parms. If this is unsuccessful, but @create is set a new
269 * tunnel device is created and registered for use.
271 * Return:
272 * 0 if tunnel located or created,
273 * -EINVAL if parameters incorrect,
274 * -ENODEV if no matching tunnel available
277 static int
278 ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
280 struct in6_addr *remote = &p->raddr;
281 struct in6_addr *local = &p->laddr;
282 struct ip6_tnl *t;
284 if (p->proto != IPPROTO_IPV6)
285 return -EINVAL;
287 for (t = *ip6ip6_bucket(p); t; t = t->next) {
288 if (ipv6_addr_equal(local, &t->parms.laddr) &&
289 ipv6_addr_equal(remote, &t->parms.raddr)) {
290 *pt = t;
291 return (create ? -EEXIST : 0);
294 if (!create)
295 return -ENODEV;
297 return ip6_tnl_create(p, pt);
301 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
302 * @dev: the device to be destroyed
304 * Description:
305 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
308 static void
309 ip6ip6_tnl_dev_uninit(struct net_device *dev)
311 struct ip6_tnl *t = netdev_priv(dev);
313 if (dev == ip6ip6_fb_tnl_dev) {
314 write_lock_bh(&ip6ip6_lock);
315 tnls_wc[0] = NULL;
316 write_unlock_bh(&ip6ip6_lock);
317 } else {
318 ip6ip6_tnl_unlink(t);
320 ip6_tnl_dst_reset(t);
321 dev_put(dev);
325 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
326 * @skb: received socket buffer
328 * Return:
329 * 0 if none was found,
330 * else index to encapsulation limit
333 static __u16
334 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
336 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
337 __u8 nexthdr = ipv6h->nexthdr;
338 __u16 off = sizeof (*ipv6h);
340 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
341 __u16 optlen = 0;
342 struct ipv6_opt_hdr *hdr;
343 if (raw + off + sizeof (*hdr) > skb->data &&
344 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
345 break;
347 hdr = (struct ipv6_opt_hdr *) (raw + off);
348 if (nexthdr == NEXTHDR_FRAGMENT) {
349 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
350 if (frag_hdr->frag_off)
351 break;
352 optlen = 8;
353 } else if (nexthdr == NEXTHDR_AUTH) {
354 optlen = (hdr->hdrlen + 2) << 2;
355 } else {
356 optlen = ipv6_optlen(hdr);
358 if (nexthdr == NEXTHDR_DEST) {
359 __u16 i = off + 2;
360 while (1) {
361 struct ipv6_tlv_tnl_enc_lim *tel;
363 /* No more room for encapsulation limit */
364 if (i + sizeof (*tel) > off + optlen)
365 break;
367 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
368 /* return index of option if found and valid */
369 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
370 tel->length == 1)
371 return i;
372 /* else jump to next option */
373 if (tel->type)
374 i += tel->length + 2;
375 else
376 i++;
379 nexthdr = hdr->nexthdr;
380 off += optlen;
382 return 0;
386 * ip6ip6_err - tunnel error handler
388 * Description:
389 * ip6ip6_err() should handle errors in the tunnel according
390 * to the specifications in RFC 2473.
393 static int
394 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
395 int type, int code, int offset, __u32 info)
397 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
398 struct ip6_tnl *t;
399 int rel_msg = 0;
400 int rel_type = ICMPV6_DEST_UNREACH;
401 int rel_code = ICMPV6_ADDR_UNREACH;
402 __u32 rel_info = 0;
403 __u16 len;
404 int err = -ENOENT;
406 /* If the packet doesn't contain the original IPv6 header we are
407 in trouble since we might need the source address for further
408 processing of the error. */
410 read_lock(&ip6ip6_lock);
411 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
412 goto out;
414 err = 0;
416 switch (type) {
417 __u32 teli;
418 struct ipv6_tlv_tnl_enc_lim *tel;
419 __u32 mtu;
420 case ICMPV6_DEST_UNREACH:
421 if (net_ratelimit())
422 printk(KERN_WARNING
423 "%s: Path to destination invalid "
424 "or inactive!\n", t->parms.name);
425 rel_msg = 1;
426 break;
427 case ICMPV6_TIME_EXCEED:
428 if (code == ICMPV6_EXC_HOPLIMIT) {
429 if (net_ratelimit())
430 printk(KERN_WARNING
431 "%s: Too small hop limit or "
432 "routing loop in tunnel!\n",
433 t->parms.name);
434 rel_msg = 1;
436 break;
437 case ICMPV6_PARAMPROB:
438 /* ignore if parameter problem not caused by a tunnel
439 encapsulation limit sub-option */
440 if (code != ICMPV6_HDR_FIELD) {
441 break;
443 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
445 if (teli && teli == ntohl(info) - 2) {
446 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
447 if (tel->encap_limit == 0) {
448 if (net_ratelimit())
449 printk(KERN_WARNING
450 "%s: Too small encapsulation "
451 "limit or routing loop in "
452 "tunnel!\n", t->parms.name);
453 rel_msg = 1;
456 break;
457 case ICMPV6_PKT_TOOBIG:
458 mtu = ntohl(info) - offset;
459 if (mtu < IPV6_MIN_MTU)
460 mtu = IPV6_MIN_MTU;
461 t->dev->mtu = mtu;
463 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
464 rel_type = ICMPV6_PKT_TOOBIG;
465 rel_code = 0;
466 rel_info = mtu;
467 rel_msg = 1;
469 break;
471 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
472 struct rt6_info *rt;
473 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
474 if (!skb2)
475 goto out;
477 dst_release(skb2->dst);
478 skb2->dst = NULL;
479 skb_pull(skb2, offset);
480 skb2->nh.raw = skb2->data;
482 /* Try to guess incoming interface */
483 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
485 if (rt && rt->rt6i_dev)
486 skb2->dev = rt->rt6i_dev;
488 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
490 if (rt)
491 dst_release(&rt->u.dst);
493 kfree_skb(skb2);
495 out:
496 read_unlock(&ip6ip6_lock);
497 return err;
500 static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
501 struct sk_buff *skb)
503 struct ipv6hdr *inner_iph = skb->nh.ipv6h;
505 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
506 IP6_ECN_set_ce(inner_iph);
510 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
511 * @skb: received socket buffer
513 * Return: 0
516 static int
517 ip6ip6_rcv(struct sk_buff *skb)
519 struct ipv6hdr *ipv6h;
520 struct ip6_tnl *t;
522 ipv6h = skb->nh.ipv6h;
524 read_lock(&ip6ip6_lock);
526 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
527 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
528 read_unlock(&ip6ip6_lock);
529 goto discard;
532 if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
533 t->stat.rx_dropped++;
534 read_unlock(&ip6ip6_lock);
535 goto discard;
537 secpath_reset(skb);
538 skb->mac.raw = skb->nh.raw;
539 skb->nh.raw = skb->data;
540 skb->protocol = htons(ETH_P_IPV6);
541 skb->pkt_type = PACKET_HOST;
542 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
543 skb->dev = t->dev;
544 dst_release(skb->dst);
545 skb->dst = NULL;
546 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
547 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
548 ip6ip6_ecn_decapsulate(ipv6h, skb);
549 t->stat.rx_packets++;
550 t->stat.rx_bytes += skb->len;
551 netif_rx(skb);
552 read_unlock(&ip6ip6_lock);
553 return 0;
555 read_unlock(&ip6ip6_lock);
556 return 1;
558 discard:
559 kfree_skb(skb);
560 return 0;
563 static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
565 struct ipv6_tlv_tnl_enc_lim *tel;
566 struct ipv6_txoptions *opt;
567 __u8 *raw;
569 int opt_len = sizeof(*opt) + 8;
571 if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
572 return NULL;
574 memset(opt, 0, opt_len);
575 opt->tot_len = opt_len;
576 opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
577 opt->opt_nflen = 8;
579 tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
580 tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
581 tel->length = 1;
582 tel->encap_limit = encap_limit;
584 raw = (__u8 *) opt->dst0opt;
585 raw[5] = IPV6_TLV_PADN;
586 raw[6] = 1;
588 return opt;
592 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
593 * @t: the outgoing tunnel device
594 * @hdr: IPv6 header from the incoming packet
596 * Description:
597 * Avoid trivial tunneling loop by checking that tunnel exit-point
598 * doesn't match source of incoming packet.
600 * Return:
601 * 1 if conflict,
602 * 0 else
605 static inline int
606 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
608 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
612 * ip6ip6_tnl_xmit - encapsulate packet and send
613 * @skb: the outgoing socket buffer
614 * @dev: the outgoing tunnel device
616 * Description:
617 * Build new header and do some sanity checks on the packet before sending
618 * it.
620 * Return:
624 static int
625 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
627 struct ip6_tnl *t = netdev_priv(dev);
628 struct net_device_stats *stats = &t->stat;
629 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
630 struct ipv6_txoptions *opt = NULL;
631 int encap_limit = -1;
632 __u16 offset;
633 struct flowi fl;
634 struct dst_entry *dst;
635 struct net_device *tdev;
636 int mtu;
637 int max_headroom = sizeof(struct ipv6hdr);
638 u8 proto;
639 int err;
640 int pkt_len;
641 int dsfield;
643 if (t->recursion++) {
644 stats->collisions++;
645 goto tx_err;
647 if (skb->protocol != htons(ETH_P_IPV6) ||
648 !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
649 ip6ip6_tnl_addr_conflict(t, ipv6h)) {
650 goto tx_err;
652 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
653 struct ipv6_tlv_tnl_enc_lim *tel;
654 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
655 if (tel->encap_limit == 0) {
656 icmpv6_send(skb, ICMPV6_PARAMPROB,
657 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
658 goto tx_err;
660 encap_limit = tel->encap_limit - 1;
661 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
662 encap_limit = t->parms.encap_limit;
664 memcpy(&fl, &t->fl, sizeof (fl));
665 proto = fl.proto;
667 dsfield = ipv6_get_dsfield(ipv6h);
668 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
669 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
670 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
671 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
673 if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
674 goto tx_err;
676 if ((dst = ip6_tnl_dst_check(t)) != NULL)
677 dst_hold(dst);
678 else {
679 dst = ip6_route_output(NULL, &fl);
681 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
682 goto tx_err_link_failure;
685 tdev = dst->dev;
687 if (tdev == dev) {
688 stats->collisions++;
689 if (net_ratelimit())
690 printk(KERN_WARNING
691 "%s: Local routing loop detected!\n",
692 t->parms.name);
693 goto tx_err_dst_release;
695 mtu = dst_mtu(dst) - sizeof (*ipv6h);
696 if (opt) {
697 max_headroom += 8;
698 mtu -= 8;
700 if (mtu < IPV6_MIN_MTU)
701 mtu = IPV6_MIN_MTU;
702 if (skb->dst && mtu < dst_mtu(skb->dst)) {
703 struct rt6_info *rt = (struct rt6_info *) skb->dst;
704 rt->rt6i_flags |= RTF_MODIFIED;
705 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
707 if (skb->len > mtu) {
708 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
709 goto tx_err_dst_release;
713 * Okay, now see if we can stuff it in the buffer as-is.
715 max_headroom += LL_RESERVED_SPACE(tdev);
717 if (skb_headroom(skb) < max_headroom ||
718 skb_cloned(skb) || skb_shared(skb)) {
719 struct sk_buff *new_skb;
721 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
722 goto tx_err_dst_release;
724 if (skb->sk)
725 skb_set_owner_w(new_skb, skb->sk);
726 kfree_skb(skb);
727 skb = new_skb;
729 dst_release(skb->dst);
730 skb->dst = dst_clone(dst);
732 skb->h.raw = skb->nh.raw;
734 if (opt)
735 ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
737 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
738 ipv6h = skb->nh.ipv6h;
739 *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
740 dsfield = INET_ECN_encapsulate(0, dsfield);
741 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
742 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
743 ipv6h->hop_limit = t->parms.hop_limit;
744 ipv6h->nexthdr = proto;
745 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
746 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
747 nf_reset(skb);
748 pkt_len = skb->len;
749 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
750 skb->dst->dev, dst_output);
752 if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
753 stats->tx_bytes += pkt_len;
754 stats->tx_packets++;
755 } else {
756 stats->tx_errors++;
757 stats->tx_aborted_errors++;
759 ip6_tnl_dst_store(t, dst);
761 kfree(opt);
763 t->recursion--;
764 return 0;
765 tx_err_link_failure:
766 stats->tx_carrier_errors++;
767 dst_link_failure(skb);
768 tx_err_dst_release:
769 dst_release(dst);
770 kfree(opt);
771 tx_err:
772 stats->tx_errors++;
773 stats->tx_dropped++;
774 kfree_skb(skb);
775 t->recursion--;
776 return 0;
779 static void ip6_tnl_set_cap(struct ip6_tnl *t)
781 struct ip6_tnl_parm *p = &t->parms;
782 struct in6_addr *laddr = &p->laddr;
783 struct in6_addr *raddr = &p->raddr;
784 int ltype = ipv6_addr_type(laddr);
785 int rtype = ipv6_addr_type(raddr);
787 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
789 if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
790 ((ltype|rtype) &
791 (IPV6_ADDR_UNICAST|
792 IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
793 IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
794 struct net_device *ldev = NULL;
795 int l_ok = 1;
796 int r_ok = 1;
798 if (p->link)
799 ldev = dev_get_by_index(p->link);
801 if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
802 l_ok = 0;
804 if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
805 r_ok = 0;
807 if (l_ok && r_ok) {
808 if (ltype&IPV6_ADDR_UNICAST)
809 p->flags |= IP6_TNL_F_CAP_XMIT;
810 if (rtype&IPV6_ADDR_UNICAST)
811 p->flags |= IP6_TNL_F_CAP_RCV;
813 if (ldev)
814 dev_put(ldev);
818 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
820 struct net_device *dev = t->dev;
821 struct ip6_tnl_parm *p = &t->parms;
822 struct flowi *fl = &t->fl;
824 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
825 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
827 /* Set up flowi template */
828 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
829 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
830 fl->oif = p->link;
831 fl->fl6_flowlabel = 0;
833 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
834 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
835 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
836 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
838 ip6_tnl_set_cap(t);
840 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
841 dev->flags |= IFF_POINTOPOINT;
842 else
843 dev->flags &= ~IFF_POINTOPOINT;
845 dev->iflink = p->link;
847 if (p->flags & IP6_TNL_F_CAP_XMIT) {
848 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
849 p->link, 0);
851 if (rt == NULL)
852 return;
854 if (rt->rt6i_dev) {
855 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
856 sizeof (struct ipv6hdr);
858 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
860 if (dev->mtu < IPV6_MIN_MTU)
861 dev->mtu = IPV6_MIN_MTU;
863 dst_release(&rt->u.dst);
868 * ip6ip6_tnl_change - update the tunnel parameters
869 * @t: tunnel to be changed
870 * @p: tunnel configuration parameters
871 * @active: != 0 if tunnel is ready for use
873 * Description:
874 * ip6ip6_tnl_change() updates the tunnel parameters
877 static int
878 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
880 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
881 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
882 t->parms.flags = p->flags;
883 t->parms.hop_limit = p->hop_limit;
884 t->parms.encap_limit = p->encap_limit;
885 t->parms.flowinfo = p->flowinfo;
886 t->parms.link = p->link;
887 ip6_tnl_dst_reset(t);
888 ip6ip6_tnl_link_config(t);
889 return 0;
893 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
894 * @dev: virtual device associated with tunnel
895 * @ifr: parameters passed from userspace
896 * @cmd: command to be performed
898 * Description:
899 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
900 * from userspace.
902 * The possible commands are the following:
903 * %SIOCGETTUNNEL: get tunnel parameters for device
904 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
905 * %SIOCCHGTUNNEL: change tunnel parameters to those given
906 * %SIOCDELTUNNEL: delete tunnel
908 * The fallback device "ip6tnl0", created during module
909 * initialization, can be used for creating other tunnel devices.
911 * Return:
912 * 0 on success,
913 * %-EFAULT if unable to copy data to or from userspace,
914 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
915 * %-EINVAL if passed tunnel parameters are invalid,
916 * %-EEXIST if changing a tunnel's parameters would cause a conflict
917 * %-ENODEV if attempting to change or delete a nonexisting device
920 static int
921 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
923 int err = 0;
924 int create;
925 struct ip6_tnl_parm p;
926 struct ip6_tnl *t = NULL;
928 switch (cmd) {
929 case SIOCGETTUNNEL:
930 if (dev == ip6ip6_fb_tnl_dev) {
931 if (copy_from_user(&p,
932 ifr->ifr_ifru.ifru_data,
933 sizeof (p))) {
934 err = -EFAULT;
935 break;
937 if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
938 t = netdev_priv(dev);
939 else if (err)
940 break;
941 } else
942 t = netdev_priv(dev);
944 memcpy(&p, &t->parms, sizeof (p));
945 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
946 err = -EFAULT;
948 break;
949 case SIOCADDTUNNEL:
950 case SIOCCHGTUNNEL:
951 err = -EPERM;
952 create = (cmd == SIOCADDTUNNEL);
953 if (!capable(CAP_NET_ADMIN))
954 break;
955 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
956 err = -EFAULT;
957 break;
959 if (!create && dev != ip6ip6_fb_tnl_dev) {
960 t = netdev_priv(dev);
962 if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
963 break;
965 if (cmd == SIOCCHGTUNNEL) {
966 if (t->dev != dev) {
967 err = -EEXIST;
968 break;
970 ip6ip6_tnl_unlink(t);
971 err = ip6ip6_tnl_change(t, &p);
972 ip6ip6_tnl_link(t);
973 netdev_state_change(dev);
975 if (copy_to_user(ifr->ifr_ifru.ifru_data,
976 &t->parms, sizeof (p))) {
977 err = -EFAULT;
978 } else {
979 err = 0;
981 break;
982 case SIOCDELTUNNEL:
983 err = -EPERM;
984 if (!capable(CAP_NET_ADMIN))
985 break;
987 if (dev == ip6ip6_fb_tnl_dev) {
988 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
989 sizeof (p))) {
990 err = -EFAULT;
991 break;
993 err = ip6ip6_tnl_locate(&p, &t, 0);
994 if (err)
995 break;
996 if (t == netdev_priv(ip6ip6_fb_tnl_dev)) {
997 err = -EPERM;
998 break;
1000 } else {
1001 t = netdev_priv(dev);
1003 err = unregister_netdevice(t->dev);
1004 break;
1005 default:
1006 err = -EINVAL;
1008 return err;
1012 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1013 * @dev: virtual device associated with tunnel
1015 * Return: stats for device
1018 static struct net_device_stats *
1019 ip6ip6_tnl_get_stats(struct net_device *dev)
1021 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1025 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1026 * @dev: virtual device associated with tunnel
1027 * @new_mtu: the new mtu
1029 * Return:
1030 * 0 on success,
1031 * %-EINVAL if mtu too small
1034 static int
1035 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1037 if (new_mtu < IPV6_MIN_MTU) {
1038 return -EINVAL;
1040 dev->mtu = new_mtu;
1041 return 0;
1045 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1046 * @dev: virtual device associated with tunnel
1048 * Description:
1049 * Initialize function pointers and device parameters
1052 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1054 SET_MODULE_OWNER(dev);
1055 dev->uninit = ip6ip6_tnl_dev_uninit;
1056 dev->destructor = free_netdev;
1057 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1058 dev->get_stats = ip6ip6_tnl_get_stats;
1059 dev->do_ioctl = ip6ip6_tnl_ioctl;
1060 dev->change_mtu = ip6ip6_tnl_change_mtu;
1062 dev->type = ARPHRD_TUNNEL6;
1063 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1064 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1065 dev->flags |= IFF_NOARP;
1066 dev->addr_len = sizeof(struct in6_addr);
1071 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1072 * @dev: virtual device associated with tunnel
1075 static inline void
1076 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1078 struct ip6_tnl *t = netdev_priv(dev);
1079 t->fl.proto = IPPROTO_IPV6;
1080 t->dev = dev;
1081 strcpy(t->parms.name, dev->name);
1085 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1086 * @dev: virtual device associated with tunnel
1089 static int
1090 ip6ip6_tnl_dev_init(struct net_device *dev)
1092 struct ip6_tnl *t = netdev_priv(dev);
1093 ip6ip6_tnl_dev_init_gen(dev);
1094 ip6ip6_tnl_link_config(t);
1095 return 0;
1099 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1100 * @dev: fallback device
1102 * Return: 0
1105 static int
1106 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1108 struct ip6_tnl *t = netdev_priv(dev);
1109 ip6ip6_tnl_dev_init_gen(dev);
1110 dev_hold(dev);
1111 tnls_wc[0] = t;
1112 return 0;
1115 static struct xfrm6_tunnel ip6ip6_handler = {
1116 .handler = ip6ip6_rcv,
1117 .err_handler = ip6ip6_err,
1118 .priority = 1,
1122 * ip6_tunnel_init - register protocol and reserve needed resources
1124 * Return: 0 on success
1127 static int __init ip6_tunnel_init(void)
1129 int err;
1131 if (xfrm6_tunnel_register(&ip6ip6_handler)) {
1132 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1133 return -EAGAIN;
1135 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1136 ip6ip6_tnl_dev_setup);
1138 if (!ip6ip6_fb_tnl_dev) {
1139 err = -ENOMEM;
1140 goto fail;
1142 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1144 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1145 free_netdev(ip6ip6_fb_tnl_dev);
1146 goto fail;
1148 return 0;
1149 fail:
1150 xfrm6_tunnel_deregister(&ip6ip6_handler);
1151 return err;
1155 * ip6_tunnel_cleanup - free resources and unregister protocol
1158 static void __exit ip6_tunnel_cleanup(void)
1160 if (xfrm6_tunnel_deregister(&ip6ip6_handler))
1161 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1163 unregister_netdev(ip6ip6_fb_tnl_dev);
1166 module_init(ip6_tunnel_init);
1167 module_exit(ip6_tunnel_cleanup);