2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
11 * linux/net/ipv6/sit.c
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/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/sockios.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>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/ip6_tunnel.h>
50 #include <net/dsfield.h>
51 #include <net/inet_ecn.h>
53 MODULE_AUTHOR("Ville Nuorvala");
54 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
55 MODULE_LICENSE("GPL");
57 #define IPV6_TLV_TEL_DST_SIZE 8
60 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
62 #define IP6_TNL_TRACE(x...) do {;} while(0)
65 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
69 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
70 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
73 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
74 static int ip6ip6_tnl_dev_init(struct net_device *dev);
75 static void ip6ip6_tnl_dev_setup(struct net_device *dev);
77 /* the IPv6 tunnel fallback device */
78 static struct net_device *ip6ip6_fb_tnl_dev;
81 /* lists for storing tunnels in use */
82 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
83 static struct ip6_tnl *tnls_wc[1];
84 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
86 /* lock for the tunnel lists */
87 static DEFINE_RWLOCK(ip6ip6_lock);
89 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
91 struct dst_entry *dst = t->dst_cache;
93 if (dst && dst->obsolete &&
94 dst->ops->check(dst, t->dst_cookie) == NULL) {
103 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
105 dst_release(t->dst_cache);
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);
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
123 * tunnel matching given end-points if found,
124 * else fallback tunnel if its device is up,
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);
135 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
136 if (ipv6_addr_equal(local, &t->parms.laddr) &&
137 ipv6_addr_equal(remote, &t->parms.raddr) &&
138 (t->dev->flags & IFF_UP))
141 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
148 * ip6ip6_bucket - get head of list matching given tunnel parameters
149 * @p: parameters containing tunnel end-points
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;
166 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
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
179 ip6ip6_tnl_link(struct ip6_tnl *t)
181 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
184 write_lock_bh(&ip6ip6_lock);
186 write_unlock_bh(&ip6ip6_lock);
190 * ip6ip6_tnl_unlink - remove tunnel from hash table
191 * @t: tunnel to be removed
195 ip6ip6_tnl_unlink(struct ip6_tnl *t)
199 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
201 write_lock_bh(&ip6ip6_lock);
203 write_unlock_bh(&ip6ip6_lock);
210 * ip6_tnl_create() - create a new tunnel
211 * @p: tunnel parameters
212 * @pt: pointer to new tunnel
215 * Create tunnel matching given parameters.
218 * created tunnel or NULL
221 static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
223 struct net_device *dev;
229 strlcpy(name, p->name, IFNAMSIZ);
232 for (i = 1; i < IP6_TNL_MAX; i++) {
233 sprintf(name, "ip6tnl%d", i);
234 if (__dev_get_by_name(name) == NULL)
237 if (i == IP6_TNL_MAX)
240 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
244 t = netdev_priv(dev);
245 dev->init = ip6ip6_tnl_dev_init;
248 if ((err = register_netdevice(dev)) < 0) {
260 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
261 * @p: tunnel parameters
262 * @create: != 0 if allowed to create new tunnel if no match found
265 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
266 * based on @parms. If this is unsuccessful, but @create is set a new
267 * tunnel device is created and registered for use.
270 * matching tunnel or NULL
273 static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
275 struct in6_addr *remote = &p->raddr;
276 struct in6_addr *local = &p->laddr;
279 for (t = *ip6ip6_bucket(p); t; t = t->next) {
280 if (ipv6_addr_equal(local, &t->parms.laddr) &&
281 ipv6_addr_equal(remote, &t->parms.raddr))
286 return ip6_tnl_create(p);
290 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
291 * @dev: the device to be destroyed
294 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
298 ip6ip6_tnl_dev_uninit(struct net_device *dev)
300 struct ip6_tnl *t = netdev_priv(dev);
302 if (dev == ip6ip6_fb_tnl_dev) {
303 write_lock_bh(&ip6ip6_lock);
305 write_unlock_bh(&ip6ip6_lock);
307 ip6ip6_tnl_unlink(t);
309 ip6_tnl_dst_reset(t);
314 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
315 * @skb: received socket buffer
318 * 0 if none was found,
319 * else index to encapsulation limit
323 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
325 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
326 __u8 nexthdr = ipv6h->nexthdr;
327 __u16 off = sizeof (*ipv6h);
329 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
331 struct ipv6_opt_hdr *hdr;
332 if (raw + off + sizeof (*hdr) > skb->data &&
333 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
336 hdr = (struct ipv6_opt_hdr *) (raw + off);
337 if (nexthdr == NEXTHDR_FRAGMENT) {
338 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
339 if (frag_hdr->frag_off)
342 } else if (nexthdr == NEXTHDR_AUTH) {
343 optlen = (hdr->hdrlen + 2) << 2;
345 optlen = ipv6_optlen(hdr);
347 if (nexthdr == NEXTHDR_DEST) {
350 struct ipv6_tlv_tnl_enc_lim *tel;
352 /* No more room for encapsulation limit */
353 if (i + sizeof (*tel) > off + optlen)
356 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
357 /* return index of option if found and valid */
358 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
361 /* else jump to next option */
363 i += tel->length + 2;
368 nexthdr = hdr->nexthdr;
375 * ip6ip6_err - tunnel error handler
378 * ip6ip6_err() should handle errors in the tunnel according
379 * to the specifications in RFC 2473.
383 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
384 int type, int code, int offset, __be32 info)
386 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
389 int rel_type = ICMPV6_DEST_UNREACH;
390 int rel_code = ICMPV6_ADDR_UNREACH;
395 /* If the packet doesn't contain the original IPv6 header we are
396 in trouble since we might need the source address for further
397 processing of the error. */
399 read_lock(&ip6ip6_lock);
400 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
407 struct ipv6_tlv_tnl_enc_lim *tel;
409 case ICMPV6_DEST_UNREACH:
412 "%s: Path to destination invalid "
413 "or inactive!\n", t->parms.name);
416 case ICMPV6_TIME_EXCEED:
417 if (code == ICMPV6_EXC_HOPLIMIT) {
420 "%s: Too small hop limit or "
421 "routing loop in tunnel!\n",
426 case ICMPV6_PARAMPROB:
427 /* ignore if parameter problem not caused by a tunnel
428 encapsulation limit sub-option */
429 if (code != ICMPV6_HDR_FIELD) {
432 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
434 if (teli && teli == ntohl(info) - 2) {
435 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
436 if (tel->encap_limit == 0) {
439 "%s: Too small encapsulation "
440 "limit or routing loop in "
441 "tunnel!\n", t->parms.name);
446 case ICMPV6_PKT_TOOBIG:
447 mtu = ntohl(info) - offset;
448 if (mtu < IPV6_MIN_MTU)
452 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
453 rel_type = ICMPV6_PKT_TOOBIG;
460 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
462 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
467 dst_release(skb2->dst);
469 skb_pull(skb2, offset);
470 skb2->nh.raw = skb2->data;
472 /* Try to guess incoming interface */
473 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
475 if (rt && rt->rt6i_dev)
476 skb2->dev = rt->rt6i_dev;
478 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
481 dst_release(&rt->u.dst);
486 read_unlock(&ip6ip6_lock);
490 static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
493 struct ipv6hdr *inner_iph = skb->nh.ipv6h;
495 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
496 IP6_ECN_set_ce(inner_iph);
498 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
500 struct ip6_tnl_parm *p = &t->parms;
503 if (p->flags & IP6_TNL_F_CAP_RCV) {
504 struct net_device *ldev = NULL;
507 ldev = dev_get_by_index(p->link);
509 if ((ipv6_addr_is_multicast(&p->laddr) ||
510 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
511 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
521 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
522 * @skb: received socket buffer
528 ip6ip6_rcv(struct sk_buff *skb)
530 struct ipv6hdr *ipv6h;
533 ipv6h = skb->nh.ipv6h;
535 read_lock(&ip6ip6_lock);
537 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
538 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
539 read_unlock(&ip6ip6_lock);
543 if (!ip6_tnl_rcv_ctl(t)) {
544 t->stat.rx_dropped++;
545 read_unlock(&ip6ip6_lock);
549 skb->mac.raw = skb->nh.raw;
550 skb->nh.raw = skb->data;
551 skb->protocol = htons(ETH_P_IPV6);
552 skb->pkt_type = PACKET_HOST;
553 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
555 dst_release(skb->dst);
558 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
559 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
560 ip6ip6_ecn_decapsulate(ipv6h, skb);
561 t->stat.rx_packets++;
562 t->stat.rx_bytes += skb->len;
564 read_unlock(&ip6ip6_lock);
567 read_unlock(&ip6ip6_lock);
575 struct ipv6_tel_txoption {
576 struct ipv6_txoptions ops;
580 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
582 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
584 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
586 opt->dst_opt[4] = encap_limit;
587 opt->dst_opt[5] = IPV6_TLV_PADN;
590 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
591 opt->ops.opt_nflen = 8;
595 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
596 * @t: the outgoing tunnel device
597 * @hdr: IPv6 header from the incoming packet
600 * Avoid trivial tunneling loop by checking that tunnel exit-point
601 * doesn't match source of incoming packet.
609 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
611 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
614 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
616 struct ip6_tnl_parm *p = &t->parms;
619 if (p->flags & IP6_TNL_F_CAP_XMIT) {
620 struct net_device *ldev = NULL;
623 ldev = dev_get_by_index(p->link);
625 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
627 "%s xmit: Local address not yet configured!\n",
629 else if (!ipv6_addr_is_multicast(&p->raddr) &&
630 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
632 "%s xmit: Routing loop! "
633 "Remote address found on this node!\n",
643 * ip6ip6_tnl_xmit - encapsulate packet and send
644 * @skb: the outgoing socket buffer
645 * @dev: the outgoing tunnel device
648 * Build new header and do some sanity checks on the packet before sending
656 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
658 struct ip6_tnl *t = netdev_priv(dev);
659 struct net_device_stats *stats = &t->stat;
660 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
661 int encap_limit = -1;
662 struct ipv6_tel_txoption opt;
665 struct dst_entry *dst;
666 struct net_device *tdev;
668 int max_headroom = sizeof(struct ipv6hdr);
674 if (t->recursion++) {
678 if (skb->protocol != htons(ETH_P_IPV6) ||
679 !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
682 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
683 struct ipv6_tlv_tnl_enc_lim *tel;
684 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
685 if (tel->encap_limit == 0) {
686 icmpv6_send(skb, ICMPV6_PARAMPROB,
687 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
690 encap_limit = tel->encap_limit - 1;
691 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
692 encap_limit = t->parms.encap_limit;
694 memcpy(&fl, &t->fl, sizeof (fl));
697 dsfield = ipv6_get_dsfield(ipv6h);
698 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
699 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
700 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
701 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
703 if ((dst = ip6_tnl_dst_check(t)) != NULL)
706 dst = ip6_route_output(NULL, &fl);
708 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
709 goto tx_err_link_failure;
718 "%s: Local routing loop detected!\n",
720 goto tx_err_dst_release;
722 mtu = dst_mtu(dst) - sizeof (*ipv6h);
723 if (encap_limit >= 0) {
727 if (mtu < IPV6_MIN_MTU)
729 if (skb->dst && mtu < dst_mtu(skb->dst)) {
730 struct rt6_info *rt = (struct rt6_info *) skb->dst;
731 rt->rt6i_flags |= RTF_MODIFIED;
732 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
734 if (skb->len > mtu) {
735 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
736 goto tx_err_dst_release;
740 * Okay, now see if we can stuff it in the buffer as-is.
742 max_headroom += LL_RESERVED_SPACE(tdev);
744 if (skb_headroom(skb) < max_headroom ||
745 skb_cloned(skb) || skb_shared(skb)) {
746 struct sk_buff *new_skb;
748 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
749 goto tx_err_dst_release;
752 skb_set_owner_w(new_skb, skb->sk);
756 dst_release(skb->dst);
757 skb->dst = dst_clone(dst);
759 skb->h.raw = skb->nh.raw;
761 if (encap_limit >= 0) {
762 init_tel_txopt(&opt, encap_limit);
763 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
765 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
766 ipv6h = skb->nh.ipv6h;
767 *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
768 dsfield = INET_ECN_encapsulate(0, dsfield);
769 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
770 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
771 ipv6h->hop_limit = t->parms.hop_limit;
772 ipv6h->nexthdr = proto;
773 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
774 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
777 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
778 skb->dst->dev, dst_output);
780 if (net_xmit_eval(err) == 0) {
781 stats->tx_bytes += pkt_len;
785 stats->tx_aborted_errors++;
787 ip6_tnl_dst_store(t, dst);
791 stats->tx_carrier_errors++;
792 dst_link_failure(skb);
803 static void ip6_tnl_set_cap(struct ip6_tnl *t)
805 struct ip6_tnl_parm *p = &t->parms;
806 int ltype = ipv6_addr_type(&p->laddr);
807 int rtype = ipv6_addr_type(&p->raddr);
809 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
811 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
812 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
813 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
814 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
815 if (ltype&IPV6_ADDR_UNICAST)
816 p->flags |= IP6_TNL_F_CAP_XMIT;
817 if (rtype&IPV6_ADDR_UNICAST)
818 p->flags |= IP6_TNL_F_CAP_RCV;
822 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
824 struct net_device *dev = t->dev;
825 struct ip6_tnl_parm *p = &t->parms;
826 struct flowi *fl = &t->fl;
828 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
829 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
831 /* Set up flowi template */
832 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
833 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
835 fl->fl6_flowlabel = 0;
837 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
838 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
839 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
840 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
844 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
845 dev->flags |= IFF_POINTOPOINT;
847 dev->flags &= ~IFF_POINTOPOINT;
849 dev->iflink = p->link;
851 if (p->flags & IP6_TNL_F_CAP_XMIT) {
852 int strict = (ipv6_addr_type(&p->raddr) &
853 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
855 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
862 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
863 sizeof (struct ipv6hdr);
865 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
867 if (dev->mtu < IPV6_MIN_MTU)
868 dev->mtu = IPV6_MIN_MTU;
870 dst_release(&rt->u.dst);
875 * ip6ip6_tnl_change - update the tunnel parameters
876 * @t: tunnel to be changed
877 * @p: tunnel configuration parameters
878 * @active: != 0 if tunnel is ready for use
881 * ip6ip6_tnl_change() updates the tunnel parameters
885 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
887 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
888 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
889 t->parms.flags = p->flags;
890 t->parms.hop_limit = p->hop_limit;
891 t->parms.encap_limit = p->encap_limit;
892 t->parms.flowinfo = p->flowinfo;
893 t->parms.link = p->link;
894 ip6_tnl_dst_reset(t);
895 ip6ip6_tnl_link_config(t);
900 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
901 * @dev: virtual device associated with tunnel
902 * @ifr: parameters passed from userspace
903 * @cmd: command to be performed
906 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
909 * The possible commands are the following:
910 * %SIOCGETTUNNEL: get tunnel parameters for device
911 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
912 * %SIOCCHGTUNNEL: change tunnel parameters to those given
913 * %SIOCDELTUNNEL: delete tunnel
915 * The fallback device "ip6tnl0", created during module
916 * initialization, can be used for creating other tunnel devices.
920 * %-EFAULT if unable to copy data to or from userspace,
921 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
922 * %-EINVAL if passed tunnel parameters are invalid,
923 * %-EEXIST if changing a tunnel's parameters would cause a conflict
924 * %-ENODEV if attempting to change or delete a nonexisting device
928 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
931 struct ip6_tnl_parm p;
932 struct ip6_tnl *t = NULL;
936 if (dev == ip6ip6_fb_tnl_dev) {
937 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
941 t = ip6ip6_tnl_locate(&p, 0);
944 t = netdev_priv(dev);
945 memcpy(&p, &t->parms, sizeof (p));
946 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
953 if (!capable(CAP_NET_ADMIN))
956 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
959 if (p.proto != IPPROTO_IPV6)
961 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
962 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
969 t = netdev_priv(dev);
971 ip6ip6_tnl_unlink(t);
972 err = ip6ip6_tnl_change(t, &p);
974 netdev_state_change(dev);
978 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
982 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
986 if (!capable(CAP_NET_ADMIN))
989 if (dev == ip6ip6_fb_tnl_dev) {
991 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
994 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
997 if (t->dev == ip6ip6_fb_tnl_dev)
1001 err = unregister_netdevice(dev);
1010 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1011 * @dev: virtual device associated with tunnel
1013 * Return: stats for device
1016 static struct net_device_stats *
1017 ip6ip6_tnl_get_stats(struct net_device *dev)
1019 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1023 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1024 * @dev: virtual device associated with tunnel
1025 * @new_mtu: the new mtu
1029 * %-EINVAL if mtu too small
1033 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1035 if (new_mtu < IPV6_MIN_MTU) {
1043 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1044 * @dev: virtual device associated with tunnel
1047 * Initialize function pointers and device parameters
1050 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1052 SET_MODULE_OWNER(dev);
1053 dev->uninit = ip6ip6_tnl_dev_uninit;
1054 dev->destructor = free_netdev;
1055 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1056 dev->get_stats = ip6ip6_tnl_get_stats;
1057 dev->do_ioctl = ip6ip6_tnl_ioctl;
1058 dev->change_mtu = ip6ip6_tnl_change_mtu;
1060 dev->type = ARPHRD_TUNNEL6;
1061 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1062 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1063 dev->flags |= IFF_NOARP;
1064 dev->addr_len = sizeof(struct in6_addr);
1069 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1070 * @dev: virtual device associated with tunnel
1074 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1076 struct ip6_tnl *t = netdev_priv(dev);
1077 t->fl.proto = IPPROTO_IPV6;
1079 strcpy(t->parms.name, dev->name);
1083 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1084 * @dev: virtual device associated with tunnel
1088 ip6ip6_tnl_dev_init(struct net_device *dev)
1090 struct ip6_tnl *t = netdev_priv(dev);
1091 ip6ip6_tnl_dev_init_gen(dev);
1092 ip6ip6_tnl_link_config(t);
1097 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1098 * @dev: fallback device
1104 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1106 struct ip6_tnl *t = netdev_priv(dev);
1107 ip6ip6_tnl_dev_init_gen(dev);
1113 static struct xfrm6_tunnel ip6ip6_handler = {
1114 .handler = ip6ip6_rcv,
1115 .err_handler = ip6ip6_err,
1120 * ip6_tunnel_init - register protocol and reserve needed resources
1122 * Return: 0 on success
1125 static int __init ip6_tunnel_init(void)
1129 if (xfrm6_tunnel_register(&ip6ip6_handler)) {
1130 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1133 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1134 ip6ip6_tnl_dev_setup);
1136 if (!ip6ip6_fb_tnl_dev) {
1140 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1142 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1143 free_netdev(ip6ip6_fb_tnl_dev);
1148 xfrm6_tunnel_deregister(&ip6ip6_handler);
1152 static void __exit ip6ip6_destroy_tunnels(void)
1157 for (h = 0; h < HASH_SIZE; h++) {
1158 while ((t = tnls_r_l[h]) != NULL)
1159 unregister_netdevice(t->dev);
1163 unregister_netdevice(t->dev);
1167 * ip6_tunnel_cleanup - free resources and unregister protocol
1170 static void __exit ip6_tunnel_cleanup(void)
1172 if (xfrm6_tunnel_deregister(&ip6ip6_handler))
1173 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1176 ip6ip6_destroy_tunnels();
1180 module_init(ip6_tunnel_init);
1181 module_exit(ip6_tunnel_cleanup);