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/config.h>
23 #include <linux/module.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/protocol.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
52 MODULE_AUTHOR("Ville Nuorvala");
53 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
54 MODULE_LICENSE("GPL");
56 #define IPV6_TLV_TEL_DST_SIZE 8
59 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
61 #define IP6_TNL_TRACE(x...) do {;} while(0)
64 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
68 #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
69 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
72 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
73 static int ip6ip6_tnl_dev_init(struct net_device *dev);
74 static void ip6ip6_tnl_dev_setup(struct net_device *dev);
76 /* the IPv6 tunnel fallback device */
77 static struct net_device *ip6ip6_fb_tnl_dev;
80 /* lists for storing tunnels in use */
81 static struct ip6_tnl *tnls_r_l[HASH_SIZE];
82 static struct ip6_tnl *tnls_wc[1];
83 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
85 /* lock for the tunnel lists */
86 static rwlock_t ip6ip6_lock = RW_LOCK_UNLOCKED;
88 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
90 struct dst_entry *dst = t->dst_cache;
92 if (dst && dst->obsolete &&
93 dst->ops->check(dst, t->dst_cookie) == NULL) {
101 static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
103 dst_release(t->dst_cache);
107 static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
109 struct rt6_info *rt = (struct rt6_info *) dst;
110 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
111 dst_release(t->dst_cache);
116 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
117 * @remote: the address of the tunnel exit-point
118 * @local: the address of the tunnel entry-point
121 * tunnel matching given end-points if found,
122 * else fallback tunnel if its device is up,
127 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
129 unsigned h0 = HASH(remote);
130 unsigned h1 = HASH(local);
133 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
134 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
135 !ipv6_addr_cmp(remote, &t->parms.raddr) &&
136 (t->dev->flags & IFF_UP))
139 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
146 * ip6ip6_bucket - get head of list matching given tunnel parameters
147 * @p: parameters containing tunnel end-points
150 * ip6ip6_bucket() returns the head of the list matching the
151 * &struct in6_addr entries laddr and raddr in @p.
153 * Return: head of IPv6 tunnel list
156 static struct ip6_tnl **
157 ip6ip6_bucket(struct ip6_tnl_parm *p)
159 struct in6_addr *remote = &p->raddr;
160 struct in6_addr *local = &p->laddr;
164 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
166 h = HASH(remote) ^ HASH(local);
168 return &tnls[prio][h];
172 * ip6ip6_tnl_link - add tunnel to hash table
173 * @t: tunnel to be added
177 ip6ip6_tnl_link(struct ip6_tnl *t)
179 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
181 write_lock_bh(&ip6ip6_lock);
183 write_unlock_bh(&ip6ip6_lock);
188 * ip6ip6_tnl_unlink - remove tunnel from hash table
189 * @t: tunnel to be removed
193 ip6ip6_tnl_unlink(struct ip6_tnl *t)
197 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
199 write_lock_bh(&ip6ip6_lock);
201 write_unlock_bh(&ip6ip6_lock);
208 * ip6_tnl_create() - create a new tunnel
209 * @p: tunnel parameters
210 * @pt: pointer to new tunnel
213 * Create tunnel matching given parameters.
220 ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
222 struct net_device *dev;
228 strlcpy(name, p->name, IFNAMSIZ);
231 for (i = 1; i < IP6_TNL_MAX; i++) {
232 sprintf(name, "ip6tnl%d", i);
233 if (__dev_get_by_name(name) == NULL)
236 if (i == IP6_TNL_MAX)
239 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
244 dev->init = ip6ip6_tnl_dev_init;
247 if ((err = register_netdevice(dev)) < 0) {
259 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
260 * @p: tunnel parameters
261 * @create: != 0 if allowed to create new tunnel if no match found
264 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
265 * based on @parms. If this is unsuccessful, but @create is set a new
266 * tunnel device is created and registered for use.
269 * 0 if tunnel located or created,
270 * -EINVAL if parameters incorrect,
271 * -ENODEV if no matching tunnel available
275 ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
277 struct in6_addr *remote = &p->raddr;
278 struct in6_addr *local = &p->laddr;
281 if (p->proto != IPPROTO_IPV6)
284 for (t = *ip6ip6_bucket(p); t; t = t->next) {
285 if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
286 !ipv6_addr_cmp(remote, &t->parms.raddr)) {
288 return (create ? -EEXIST : 0);
294 return ip6_tnl_create(p, pt);
298 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
299 * @dev: the device to be destroyed
302 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
306 ip6ip6_tnl_dev_uninit(struct net_device *dev)
308 struct ip6_tnl *t = dev->priv;
310 if (dev == ip6ip6_fb_tnl_dev) {
311 write_lock_bh(&ip6ip6_lock);
313 write_unlock_bh(&ip6ip6_lock);
315 ip6ip6_tnl_unlink(t);
317 ip6_tnl_dst_reset(t);
322 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
323 * @skb: received socket buffer
326 * 0 if none was found,
327 * else index to encapsulation limit
331 parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
333 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
334 __u8 nexthdr = ipv6h->nexthdr;
335 __u16 off = sizeof (*ipv6h);
337 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
339 struct ipv6_opt_hdr *hdr;
340 if (raw + off + sizeof (*hdr) > skb->data &&
341 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
344 hdr = (struct ipv6_opt_hdr *) (raw + off);
345 if (nexthdr == NEXTHDR_FRAGMENT) {
346 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
347 if (frag_hdr->frag_off)
350 } else if (nexthdr == NEXTHDR_AUTH) {
351 optlen = (hdr->hdrlen + 2) << 2;
353 optlen = ipv6_optlen(hdr);
355 if (nexthdr == NEXTHDR_DEST) {
358 struct ipv6_tlv_tnl_enc_lim *tel;
360 /* No more room for encapsulation limit */
361 if (i + sizeof (*tel) > off + optlen)
364 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
365 /* return index of option if found and valid */
366 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
369 /* else jump to next option */
371 i += tel->length + 2;
376 nexthdr = hdr->nexthdr;
383 * ip6ip6_err - tunnel error handler
386 * ip6ip6_err() should handle errors in the tunnel according
387 * to the specifications in RFC 2473.
390 void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
391 int type, int code, int offset, __u32 info)
393 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
396 int rel_type = ICMPV6_DEST_UNREACH;
397 int rel_code = ICMPV6_ADDR_UNREACH;
401 /* If the packet doesn't contain the original IPv6 header we are
402 in trouble since we might need the source address for further
403 processing of the error. */
405 read_lock(&ip6ip6_lock);
406 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
411 struct ipv6_tlv_tnl_enc_lim *tel;
413 case ICMPV6_DEST_UNREACH:
416 "%s: Path to destination invalid "
417 "or inactive!\n", t->parms.name);
420 case ICMPV6_TIME_EXCEED:
421 if (code == ICMPV6_EXC_HOPLIMIT) {
424 "%s: Too small hop limit or "
425 "routing loop in tunnel!\n",
430 case ICMPV6_PARAMPROB:
431 /* ignore if parameter problem not caused by a tunnel
432 encapsulation limit sub-option */
433 if (code != ICMPV6_HDR_FIELD) {
436 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
438 if (teli && teli == ntohl(info) - 2) {
439 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
440 if (tel->encap_limit == 0) {
443 "%s: Too small encapsulation "
444 "limit or routing loop in "
445 "tunnel!\n", t->parms.name);
450 case ICMPV6_PKT_TOOBIG:
451 mtu = ntohl(info) - offset;
452 if (mtu < IPV6_MIN_MTU)
456 if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
457 rel_type = ICMPV6_PKT_TOOBIG;
464 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
466 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
470 dst_release(skb2->dst);
472 skb_pull(skb2, offset);
473 skb2->nh.raw = skb2->data;
475 /* Try to guess incoming interface */
476 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
478 if (rt && rt->rt6i_dev)
479 skb2->dev = rt->rt6i_dev;
481 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
484 dst_release(&rt->u.dst);
489 read_unlock(&ip6ip6_lock);
493 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
494 * @skb: received socket buffer
499 int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
501 struct sk_buff *skb = *pskb;
502 struct ipv6hdr *ipv6h;
505 if (!pskb_may_pull(skb, sizeof (*ipv6h)))
508 ipv6h = skb->nh.ipv6h;
510 read_lock(&ip6ip6_lock);
512 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
513 if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
514 t->stat.rx_dropped++;
515 read_unlock(&ip6ip6_lock);
519 skb->mac.raw = skb->nh.raw;
520 skb->nh.raw = skb->data;
521 skb->protocol = htons(ETH_P_IPV6);
522 skb->pkt_type = PACKET_HOST;
523 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
525 dst_release(skb->dst);
527 t->stat.rx_packets++;
528 t->stat.rx_bytes += skb->len;
530 read_unlock(&ip6ip6_lock);
533 read_unlock(&ip6ip6_lock);
534 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
540 static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
542 struct ipv6_tlv_tnl_enc_lim *tel;
543 struct ipv6_txoptions *opt;
546 int opt_len = sizeof(*opt) + 8;
548 if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) {
551 memset(opt, 0, opt_len);
552 opt->tot_len = opt_len;
553 opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
556 tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
557 tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
559 tel->encap_limit = encap_limit;
561 raw = (__u8 *) opt->dst0opt;
562 raw[5] = IPV6_TLV_PADN;
569 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
570 * @t: the outgoing tunnel device
571 * @hdr: IPv6 header from the incoming packet
574 * Avoid trivial tunneling loop by checking that tunnel exit-point
575 * doesn't match source of incoming packet.
583 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
585 return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);
589 * ip6ip6_tnl_xmit - encapsulate packet and send
590 * @skb: the outgoing socket buffer
591 * @dev: the outgoing tunnel device
594 * Build new header and do some sanity checks on the packet before sending
601 int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
603 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
604 struct net_device_stats *stats = &t->stat;
605 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
606 struct ipv6_txoptions *opt = NULL;
607 int encap_limit = -1;
610 struct dst_entry *dst;
611 struct net_device *tdev;
613 int max_headroom = sizeof(struct ipv6hdr);
618 if (t->recursion++) {
622 if (skb->protocol != htons(ETH_P_IPV6) ||
623 !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
624 ip6ip6_tnl_addr_conflict(t, ipv6h)) {
627 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
628 struct ipv6_tlv_tnl_enc_lim *tel;
629 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
630 if (tel->encap_limit == 0) {
631 icmpv6_send(skb, ICMPV6_PARAMPROB,
632 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
635 encap_limit = tel->encap_limit - 1;
636 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
637 encap_limit = t->parms.encap_limit;
639 memcpy(&fl, &t->fl, sizeof (fl));
642 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
643 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
644 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
645 fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
647 if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
650 if ((dst = ip6_tnl_dst_check(t)) != NULL)
653 dst = ip6_route_output(NULL, &fl);
655 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
656 goto tx_err_link_failure;
664 "%s: Local routing loop detected!\n",
666 goto tx_err_dst_release;
668 mtu = dst_pmtu(dst) - sizeof (*ipv6h);
673 if (mtu < IPV6_MIN_MTU)
675 if (skb->dst && mtu < dst_pmtu(skb->dst)) {
676 struct rt6_info *rt = (struct rt6_info *) skb->dst;
677 rt->rt6i_flags |= RTF_MODIFIED;
678 rt->u.dst.metrics[RTAX_MTU-1] = mtu;
680 if (skb->len > mtu) {
681 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
682 goto tx_err_dst_release;
686 * Okay, now see if we can stuff it in the buffer as-is.
688 max_headroom += LL_RESERVED_SPACE(tdev);
690 if (skb_headroom(skb) < max_headroom ||
691 skb_cloned(skb) || skb_shared(skb)) {
692 struct sk_buff *new_skb;
694 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
695 goto tx_err_dst_release;
698 skb_set_owner_w(new_skb, skb->sk);
702 dst_release(skb->dst);
703 skb->dst = dst_clone(dst);
705 skb->h.raw = skb->nh.raw;
708 ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
710 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
711 ipv6h = skb->nh.ipv6h;
712 *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
713 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
714 ipv6h->hop_limit = t->parms.hop_limit;
715 ipv6h->nexthdr = proto;
716 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
717 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
718 #ifdef CONFIG_NETFILTER
719 nf_conntrack_put(skb->nfct);
721 #ifdef CONFIG_NETFILTER_DEBUG
726 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
727 skb->dst->dev, dst_output);
729 if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
730 stats->tx_bytes += pkt_len;
734 stats->tx_aborted_errors++;
736 ip6_tnl_dst_store(t, dst);
744 stats->tx_carrier_errors++;
745 dst_link_failure(skb);
758 static void ip6_tnl_set_cap(struct ip6_tnl *t)
760 struct ip6_tnl_parm *p = &t->parms;
761 struct in6_addr *laddr = &p->laddr;
762 struct in6_addr *raddr = &p->raddr;
763 int ltype = ipv6_addr_type(laddr);
764 int rtype = ipv6_addr_type(raddr);
766 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
768 if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
771 IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
772 IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
773 struct net_device *ldev = NULL;
778 ldev = dev_get_by_index(p->link);
780 if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
783 if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
787 if (ltype&IPV6_ADDR_UNICAST)
788 p->flags |= IP6_TNL_F_CAP_XMIT;
789 if (rtype&IPV6_ADDR_UNICAST)
790 p->flags |= IP6_TNL_F_CAP_RCV;
797 static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
799 struct net_device *dev = t->dev;
800 struct ip6_tnl_parm *p = &t->parms;
801 struct flowi *fl = &t->fl;
803 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
804 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
806 /* Set up flowi template */
807 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
808 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
810 fl->fl6_flowlabel = 0;
812 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
813 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
814 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
815 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
819 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
820 dev->flags |= IFF_POINTOPOINT;
822 dev->flags &= ~IFF_POINTOPOINT;
824 dev->iflink = p->link;
826 if (p->flags & IP6_TNL_F_CAP_XMIT) {
827 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
834 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
835 sizeof (struct ipv6hdr);
837 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
839 if (dev->mtu < IPV6_MIN_MTU)
840 dev->mtu = IPV6_MIN_MTU;
842 dst_release(&rt->u.dst);
847 * ip6ip6_tnl_change - update the tunnel parameters
848 * @t: tunnel to be changed
849 * @p: tunnel configuration parameters
850 * @active: != 0 if tunnel is ready for use
853 * ip6ip6_tnl_change() updates the tunnel parameters
857 ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
859 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
860 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
861 t->parms.flags = p->flags;
862 t->parms.hop_limit = p->hop_limit;
863 t->parms.encap_limit = p->encap_limit;
864 t->parms.flowinfo = p->flowinfo;
865 ip6ip6_tnl_link_config(t);
870 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
871 * @dev: virtual device associated with tunnel
872 * @ifr: parameters passed from userspace
873 * @cmd: command to be performed
876 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
879 * The possible commands are the following:
880 * %SIOCGETTUNNEL: get tunnel parameters for device
881 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
882 * %SIOCCHGTUNNEL: change tunnel parameters to those given
883 * %SIOCDELTUNNEL: delete tunnel
885 * The fallback device "ip6tnl0", created during module
886 * initialization, can be used for creating other tunnel devices.
890 * %-EFAULT if unable to copy data to or from userspace,
891 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
892 * %-EINVAL if passed tunnel parameters are invalid,
893 * %-EEXIST if changing a tunnel's parameters would cause a conflict
894 * %-ENODEV if attempting to change or delete a nonexisting device
898 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
902 struct ip6_tnl_parm p;
903 struct ip6_tnl *t = NULL;
907 if (dev == ip6ip6_fb_tnl_dev) {
908 if (copy_from_user(&p,
909 ifr->ifr_ifru.ifru_data,
914 if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
915 t = (struct ip6_tnl *) dev->priv;
919 t = (struct ip6_tnl *) dev->priv;
921 memcpy(&p, &t->parms, sizeof (p));
922 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
929 create = (cmd == SIOCADDTUNNEL);
930 if (!capable(CAP_NET_ADMIN))
932 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
936 if (!create && dev != ip6ip6_fb_tnl_dev) {
937 t = (struct ip6_tnl *) dev->priv;
939 if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
942 if (cmd == SIOCCHGTUNNEL) {
947 ip6ip6_tnl_unlink(t);
948 err = ip6ip6_tnl_change(t, &p);
950 netdev_state_change(dev);
952 if (copy_to_user(ifr->ifr_ifru.ifru_data,
953 &t->parms, sizeof (p))) {
961 if (!capable(CAP_NET_ADMIN))
964 if (dev == ip6ip6_fb_tnl_dev) {
965 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
970 err = ip6ip6_tnl_locate(&p, &t, 0);
973 if (t == ip6ip6_fb_tnl_dev->priv) {
978 t = (struct ip6_tnl *) dev->priv;
980 err = unregister_netdevice(t->dev);
989 * ip6ip6_tnl_get_stats - return the stats for tunnel device
990 * @dev: virtual device associated with tunnel
992 * Return: stats for device
995 static struct net_device_stats *
996 ip6ip6_tnl_get_stats(struct net_device *dev)
998 return &(((struct ip6_tnl *) dev->priv)->stat);
1002 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1003 * @dev: virtual device associated with tunnel
1004 * @new_mtu: the new mtu
1008 * %-EINVAL if mtu too small
1012 ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1014 if (new_mtu < IPV6_MIN_MTU) {
1022 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1023 * @dev: virtual device associated with tunnel
1026 * Initialize function pointers and device parameters
1029 static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1031 SET_MODULE_OWNER(dev);
1032 dev->uninit = ip6ip6_tnl_dev_uninit;
1033 dev->destructor = free_netdev;
1034 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1035 dev->get_stats = ip6ip6_tnl_get_stats;
1036 dev->do_ioctl = ip6ip6_tnl_ioctl;
1037 dev->change_mtu = ip6ip6_tnl_change_mtu;
1039 dev->type = ARPHRD_TUNNEL6;
1040 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1041 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1042 dev->flags |= IFF_NOARP;
1043 dev->addr_len = sizeof(struct in6_addr);
1048 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1049 * @dev: virtual device associated with tunnel
1053 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1055 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1056 t->fl.proto = IPPROTO_IPV6;
1058 strcpy(t->parms.name, dev->name);
1062 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1063 * @dev: virtual device associated with tunnel
1067 ip6ip6_tnl_dev_init(struct net_device *dev)
1069 struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
1070 ip6ip6_tnl_dev_init_gen(dev);
1071 ip6ip6_tnl_link_config(t);
1076 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1077 * @dev: fallback device
1082 int ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1084 struct ip6_tnl *t = dev->priv;
1085 ip6ip6_tnl_dev_init_gen(dev);
1091 static struct inet6_protocol ip6ip6_protocol = {
1092 .handler = ip6ip6_rcv,
1093 .err_handler = ip6ip6_err,
1094 .flags = INET6_PROTO_FINAL
1098 * ip6_tunnel_init - register protocol and reserve needed resources
1100 * Return: 0 on success
1103 static int __init ip6_tunnel_init(void)
1107 if ((err = inet6_add_protocol(&ip6ip6_protocol, IPPROTO_IPV6)) < 0) {
1108 printk(KERN_ERR "Failed to register IPv6 protocol\n");
1111 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1112 ip6ip6_tnl_dev_setup);
1114 if (!ip6ip6_fb_tnl_dev) {
1118 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1120 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1121 free_netdev(ip6ip6_fb_tnl_dev);
1126 inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
1131 * ip6_tunnel_cleanup - free resources and unregister protocol
1134 static void __exit ip6_tunnel_cleanup(void)
1136 unregister_netdev(ip6ip6_fb_tnl_dev);
1137 inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
1140 module_init(ip6_tunnel_init);
1141 module_exit(ip6_tunnel_cleanup);