Merge git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-drm-fsl-dcu.git] / net / ipv6 / ip6_tunnel.c
index 84d7ebdb9d211103c678579009d6c24dc905379f..367b7483298610460e8449904200a1e5c3e0faee 100644 (file)
@@ -3,7 +3,7 @@
  *     Linux INET6 implementation
  *
  *     Authors:
- *     Ville Nuorvala          <vnuorval@tcs.hut.fi>   
+ *     Ville Nuorvala          <vnuorval@tcs.hut.fi>
  *
  *     $Id$
  *
@@ -66,9 +66,9 @@ MODULE_LICENSE("GPL");
 
 #define HASH_SIZE  32
 
-#define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
-                    (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
-                    (HASH_SIZE - 1))
+#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
+                    (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
+                   (HASH_SIZE - 1))
 
 static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
 static int ip6ip6_tnl_dev_init(struct net_device *dev);
@@ -90,7 +90,7 @@ static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
 {
        struct dst_entry *dst = t->dst_cache;
 
-       if (dst && dst->obsolete && 
+       if (dst && dst->obsolete &&
            dst->ops->check(dst, t->dst_cookie) == NULL) {
                t->dst_cache = NULL;
                dst_release(dst);
@@ -116,12 +116,12 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
 
 /**
  * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
- *   @remote: the address of the tunnel exit-point 
- *   @local: the address of the tunnel entry-point 
+ *   @remote: the address of the tunnel exit-point
+ *   @local: the address of the tunnel entry-point
  *
- * Return:  
+ * Return:
  *   tunnel matching given end-points if found,
- *   else fallback tunnel if its device is up, 
+ *   else fallback tunnel if its device is up,
  *   else %NULL
  **/
 
@@ -146,13 +146,13 @@ ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
 
 /**
  * ip6ip6_bucket - get head of list matching given tunnel parameters
- *   @p: parameters containing tunnel end-points 
+ *   @p: parameters containing tunnel end-points
  *
  * Description:
- *   ip6ip6_bucket() returns the head of the list matching the 
+ *   ip6ip6_bucket() returns the head of the list matching the
  *   &struct in6_addr entries laddr and raddr in @p.
  *
- * Return: head of IPv6 tunnel list 
+ * Return: head of IPv6 tunnel list
  **/
 
 static struct ip6_tnl **
@@ -213,13 +213,12 @@ ip6ip6_tnl_unlink(struct ip6_tnl *t)
  *
  * Description:
  *   Create tunnel matching given parameters.
- * 
- * Return: 
- *   0 on success
+ *
+ * Return:
+ *   created tunnel or NULL
  **/
 
-static int
-ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
+static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
 {
        struct net_device *dev;
        struct ip6_tnl *t;
@@ -235,12 +234,12 @@ ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
                        if (__dev_get_by_name(name) == NULL)
                                break;
                }
-               if (i == IP6_TNL_MAX) 
-                       return -ENOBUFS;
+               if (i == IP6_TNL_MAX)
+                       goto failed;
        }
        dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
        if (dev == NULL)
-               return -ENOMEM;
+               goto failed;
 
        t = netdev_priv(dev);
        dev->init = ip6ip6_tnl_dev_init;
@@ -248,18 +247,18 @@ ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
 
        if ((err = register_netdevice(dev)) < 0) {
                free_netdev(dev);
-               return err;
+               goto failed;
        }
        dev_hold(dev);
-
        ip6ip6_tnl_link(t);
-       *pt = t;
-       return 0;
+       return t;
+failed:
+       return NULL;
 }
 
 /**
  * ip6ip6_tnl_locate - find or create tunnel matching given parameters
- *   @p: tunnel parameters 
+ *   @p: tunnel parameters
  *   @create: != 0 if allowed to create new tunnel if no match found
  *
  * Description:
@@ -268,38 +267,29 @@ ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
  *   tunnel device is created and registered for use.
  *
  * Return:
- *   0 if tunnel located or created,
- *   -EINVAL if parameters incorrect,
- *   -ENODEV if no matching tunnel available
+ *   matching tunnel or NULL
  **/
 
-static int
-ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
+static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
 {
        struct in6_addr *remote = &p->raddr;
        struct in6_addr *local = &p->laddr;
        struct ip6_tnl *t;
 
-       if (p->proto != IPPROTO_IPV6)
-               return -EINVAL;
-
        for (t = *ip6ip6_bucket(p); t; t = t->next) {
                if (ipv6_addr_equal(local, &t->parms.laddr) &&
-                   ipv6_addr_equal(remote, &t->parms.raddr)) {
-                       *pt = t;
-                       return (create ? -EEXIST : 0);
-               }
+                   ipv6_addr_equal(remote, &t->parms.raddr))
+                       return t;
        }
        if (!create)
-               return -ENODEV;
-       
-       return ip6_tnl_create(p, pt);
+               return NULL;
+       return ip6_tnl_create(p);
 }
 
 /**
  * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
  *   @dev: the device to be destroyed
- *   
+ *
  * Description:
  *   ip6ip6_tnl_dev_uninit() removes tunnel from its list
  **/
@@ -324,8 +314,8 @@ ip6ip6_tnl_dev_uninit(struct net_device *dev)
  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  *   @skb: received socket buffer
  *
- * Return: 
- *   0 if none was found, 
+ * Return:
+ *   0 if none was found,
  *   else index to encapsulation limit
  **/
 
@@ -391,7 +381,7 @@ parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
 
 static int
 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
-          int type, int code, int offset, __u32 info)
+          int type, int code, int offset, __be32 info)
 {
        struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
        struct ip6_tnl *t;
@@ -402,8 +392,8 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        __u16 len;
        int err = -ENOENT;
 
-       /* If the packet doesn't contain the original IPv6 header we are 
-          in trouble since we might need the source address for further 
+       /* If the packet doesn't contain the original IPv6 header we are
+          in trouble since we might need the source address for further
           processing of the error. */
 
        read_lock(&ip6ip6_lock);
@@ -428,18 +418,15 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                        if (net_ratelimit())
                                printk(KERN_WARNING
                                       "%s: Too small hop limit or "
-                                      "routing loop in tunnel!\n", 
+                                      "routing loop in tunnel!\n",
                                       t->parms.name);
                        rel_msg = 1;
                }
                break;
        case ICMPV6_PARAMPROB:
-               /* ignore if parameter problem not caused by a tunnel
-                  encapsulation limit sub-option */
-               if (code != ICMPV6_HDR_FIELD) {
-                       break;
-               }
-               teli = parse_tlv_tnl_enc_lim(skb, skb->data);
+               teli = 0;
+               if (code == ICMPV6_HDR_FIELD)
+                       teli = parse_tlv_tnl_enc_lim(skb, skb->data);
 
                if (teli && teli == ntohl(info) - 2) {
                        tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
@@ -451,6 +438,10 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                                               "tunnel!\n", t->parms.name);
                                rel_msg = 1;
                        }
+               } else if (net_ratelimit()) {
+                       printk(KERN_WARNING
+                              "%s: Recipient unable to parse tunneled "
+                              "packet!\n ", t->parms.name);
                }
                break;
        case ICMPV6_PKT_TOOBIG:
@@ -470,6 +461,7 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        if (rel_msg &&  pskb_may_pull(skb, offset + sizeof (*ipv6h))) {
                struct rt6_info *rt;
                struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
+
                if (!skb2)
                        goto out;
 
@@ -504,6 +496,27 @@ static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
        if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
                IP6_ECN_set_ce(inner_iph);
 }
+static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
+{
+       struct ip6_tnl_parm *p = &t->parms;
+       int ret = 0;
+
+       if (p->flags & IP6_TNL_F_CAP_RCV) {
+               struct net_device *ldev = NULL;
+
+               if (p->link)
+                       ldev = dev_get_by_index(p->link);
+
+               if ((ipv6_addr_is_multicast(&p->laddr) ||
+                    likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
+                   likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
+                       ret = 1;
+
+               if (ldev)
+                       dev_put(ldev);
+       }
+       return ret;
+}
 
 /**
  * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
@@ -512,7 +525,7 @@ static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
  * Return: 0
  **/
 
-static int 
+static int
 ip6ip6_rcv(struct sk_buff *skb)
 {
        struct ipv6hdr *ipv6h;
@@ -528,7 +541,7 @@ ip6ip6_rcv(struct sk_buff *skb)
                        goto discard;
                }
 
-               if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
+               if (!ip6_tnl_rcv_ctl(t)) {
                        t->stat.rx_dropped++;
                        read_unlock(&ip6ip6_lock);
                        goto discard;
@@ -542,6 +555,7 @@ ip6ip6_rcv(struct sk_buff *skb)
                skb->dev = t->dev;
                dst_release(skb->dst);
                skb->dst = NULL;
+               nf_reset(skb);
                if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
                        ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
                ip6ip6_ecn_decapsulate(ipv6h, skb);
@@ -559,43 +573,35 @@ discard:
        return 0;
 }
 
-static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
-{
-       struct ipv6_tlv_tnl_enc_lim *tel;
-       struct ipv6_txoptions *opt;
-       __u8 *raw;
-
-       int opt_len = sizeof(*opt) + 8;
-
-       if (!(opt = kzalloc(opt_len, GFP_ATOMIC))) {
-               return NULL;
-       }
-       opt->tot_len = opt_len;
-       opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1);
-       opt->opt_nflen = 8;
+struct ipv6_tel_txoption {
+       struct ipv6_txoptions ops;
+       __u8 dst_opt[8];
+};
 
-       tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1);
-       tel->type = IPV6_TLV_TNL_ENCAP_LIMIT;
-       tel->length = 1;
-       tel->encap_limit = encap_limit;
+static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
+{
+       memset(opt, 0, sizeof(struct ipv6_tel_txoption));
 
-       raw = (__u8 *) opt->dst0opt;
-       raw[5] = IPV6_TLV_PADN;
-       raw[6] = 1;
+       opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
+       opt->dst_opt[3] = 1;
+       opt->dst_opt[4] = encap_limit;
+       opt->dst_opt[5] = IPV6_TLV_PADN;
+       opt->dst_opt[6] = 1;
 
-       return opt;
+       opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
+       opt->ops.opt_nflen = 8;
 }
 
 /**
  * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  *   @t: the outgoing tunnel device
- *   @hdr: IPv6 header from the incoming packet 
+ *   @hdr: IPv6 header from the incoming packet
  *
  * Description:
- *   Avoid trivial tunneling loop by checking that tunnel exit-point 
+ *   Avoid trivial tunneling loop by checking that tunnel exit-point
  *   doesn't match source of incoming packet.
  *
- * Return: 
+ * Return:
  *   1 if conflict,
  *   0 else
  **/
@@ -606,27 +612,55 @@ ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
        return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
 }
 
+static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
+{
+       struct ip6_tnl_parm *p = &t->parms;
+       int ret = 0;
+
+       if (p->flags & IP6_TNL_F_CAP_XMIT) {
+               struct net_device *ldev = NULL;
+
+               if (p->link)
+                       ldev = dev_get_by_index(p->link);
+
+               if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
+                       printk(KERN_WARNING
+                              "%s xmit: Local address not yet configured!\n",
+                              p->name);
+               else if (!ipv6_addr_is_multicast(&p->raddr) &&
+                        unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
+                       printk(KERN_WARNING
+                              "%s xmit: Routing loop! "
+                              "Remote address found on this node!\n",
+                              p->name);
+               else
+                       ret = 1;
+               if (ldev)
+                       dev_put(ldev);
+       }
+       return ret;
+}
 /**
- * ip6ip6_tnl_xmit - encapsulate packet and send 
+ * ip6ip6_tnl_xmit - encapsulate packet and send
  *   @skb: the outgoing socket buffer
- *   @dev: the outgoing tunnel device 
+ *   @dev: the outgoing tunnel device
  *
  * Description:
  *   Build new header and do some sanity checks on the packet before sending
  *   it.
  *
- * Return: 
+ * Return:
  *   0
  **/
 
-static int 
+static int
 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct ip6_tnl *t = netdev_priv(dev);
        struct net_device_stats *stats = &t->stat;
        struct ipv6hdr *ipv6h = skb->nh.ipv6h;
-       struct ipv6_txoptions *opt = NULL;
        int encap_limit = -1;
+       struct ipv6_tel_txoption opt;
        __u16 offset;
        struct flowi fl;
        struct dst_entry *dst;
@@ -643,10 +677,9 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
                goto tx_err;
        }
        if (skb->protocol != htons(ETH_P_IPV6) ||
-           !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||
-           ip6ip6_tnl_addr_conflict(t, ipv6h)) {
+           !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
                goto tx_err;
-       }
+
        if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
                struct ipv6_tlv_tnl_enc_lim *tel;
                tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
@@ -656,20 +689,17 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
                        goto tx_err;
                }
                encap_limit = tel->encap_limit - 1;
-       } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
+       } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
                encap_limit = t->parms.encap_limit;
-       }
+
        memcpy(&fl, &t->fl, sizeof (fl));
        proto = fl.proto;
 
        dsfield = ipv6_get_dsfield(ipv6h);
        if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
-               fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);
+               fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
        if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
-               fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);
-
-       if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)
-               goto tx_err;
+               fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
 
        if ((dst = ip6_tnl_dst_check(t)) != NULL)
                dst_hold(dst);
@@ -685,13 +715,13 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        if (tdev == dev) {
                stats->collisions++;
                if (net_ratelimit())
-                       printk(KERN_WARNING 
+                       printk(KERN_WARNING
                               "%s: Local routing loop detected!\n",
                               t->parms.name);
                goto tx_err_dst_release;
        }
        mtu = dst_mtu(dst) - sizeof (*ipv6h);
-       if (opt) {
+       if (encap_limit >= 0) {
                max_headroom += 8;
                mtu -= 8;
        }
@@ -711,11 +741,11 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
         * Okay, now see if we can stuff it in the buffer as-is.
         */
        max_headroom += LL_RESERVED_SPACE(tdev);
-       
-       if (skb_headroom(skb) < max_headroom || 
+
+       if (skb_headroom(skb) < max_headroom ||
            skb_cloned(skb) || skb_shared(skb)) {
                struct sk_buff *new_skb;
-               
+
                if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
                        goto tx_err_dst_release;
 
@@ -729,12 +759,13 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
 
        skb->h.raw = skb->nh.raw;
 
-       if (opt)
-               ipv6_push_nfrag_opts(skb, opt, &proto, NULL);
-
+       if (encap_limit >= 0) {
+               init_tel_txopt(&opt, encap_limit);
+               ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
+       }
        skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
        ipv6h = skb->nh.ipv6h;
-       *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
+       *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
        dsfield = INET_ECN_encapsulate(0, dsfield);
        ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
        ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
@@ -744,10 +775,10 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
        nf_reset(skb);
        pkt_len = skb->len;
-       err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 
+       err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
                      skb->dst->dev, dst_output);
 
-       if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
+       if (net_xmit_eval(err) == 0) {
                stats->tx_bytes += pkt_len;
                stats->tx_packets++;
        } else {
@@ -755,9 +786,6 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
                stats->tx_aborted_errors++;
        }
        ip6_tnl_dst_store(t, dst);
-
-       kfree(opt);
-
        t->recursion--;
        return 0;
 tx_err_link_failure:
@@ -765,7 +793,6 @@ tx_err_link_failure:
        dst_link_failure(skb);
 tx_err_dst_release:
        dst_release(dst);
-       kfree(opt);
 tx_err:
        stats->tx_errors++;
        stats->tx_dropped++;
@@ -777,39 +804,19 @@ tx_err:
 static void ip6_tnl_set_cap(struct ip6_tnl *t)
 {
        struct ip6_tnl_parm *p = &t->parms;
-       struct in6_addr *laddr = &p->laddr;
-       struct in6_addr *raddr = &p->raddr;
-       int ltype = ipv6_addr_type(laddr);
-       int rtype = ipv6_addr_type(raddr);
+       int ltype = ipv6_addr_type(&p->laddr);
+       int rtype = ipv6_addr_type(&p->raddr);
 
        p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
 
-       if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&
-           ((ltype|rtype) &
-            (IPV6_ADDR_UNICAST|
-             IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|
-             IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {
-               struct net_device *ldev = NULL;
-               int l_ok = 1;
-               int r_ok = 1;
-
-               if (p->link)
-                       ldev = dev_get_by_index(p->link);
-               
-               if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))
-                       l_ok = 0;
-               
-               if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))
-                       r_ok = 0;
-               
-               if (l_ok && r_ok) {
-                       if (ltype&IPV6_ADDR_UNICAST)
-                               p->flags |= IP6_TNL_F_CAP_XMIT;
-                       if (rtype&IPV6_ADDR_UNICAST)
-                               p->flags |= IP6_TNL_F_CAP_RCV;
-               }
-               if (ldev)
-                       dev_put(ldev);
+       if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
+           rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
+           !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
+           (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
+               if (ltype&IPV6_ADDR_UNICAST)
+                       p->flags |= IP6_TNL_F_CAP_XMIT;
+               if (rtype&IPV6_ADDR_UNICAST)
+                       p->flags |= IP6_TNL_F_CAP_RCV;
        }
 }
 
@@ -843,8 +850,11 @@ static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
        dev->iflink = p->link;
 
        if (p->flags & IP6_TNL_F_CAP_XMIT) {
+               int strict = (ipv6_addr_type(&p->raddr) &
+                             (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
+
                struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
-                                                p->link, 0);
+                                                p->link, strict);
 
                if (rt == NULL)
                        return;
@@ -888,14 +898,14 @@ ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
 }
 
 /**
- * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace 
+ * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  *   @dev: virtual device associated with tunnel
  *   @ifr: parameters passed from userspace
  *   @cmd: command to be performed
  *
  * Description:
- *   ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels 
- *   from userspace. 
+ *   ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
+ *   from userspace.
  *
  *   The possible commands are the following:
  *     %SIOCGETTUNNEL: get tunnel parameters for device
@@ -903,7 +913,7 @@ ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
  *     %SIOCDELTUNNEL: delete tunnel
  *
- *   The fallback device "ip6tnl0", created during module 
+ *   The fallback device "ip6tnl0", created during module
  *   initialization, can be used for creating other tunnel devices.
  *
  * Return:
@@ -919,26 +929,20 @@ static int
 ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 {
        int err = 0;
-       int create;
        struct ip6_tnl_parm p;
        struct ip6_tnl *t = NULL;
 
        switch (cmd) {
        case SIOCGETTUNNEL:
                if (dev == ip6ip6_fb_tnl_dev) {
-                       if (copy_from_user(&p,
-                                          ifr->ifr_ifru.ifru_data,
-                                          sizeof (p))) {
+                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
                                err = -EFAULT;
                                break;
                        }
-                       if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
-                               t = netdev_priv(dev);
-                       else if (err)
-                               break;
-               } else
+                       t = ip6ip6_tnl_locate(&p, 0);
+               }
+               if (t == NULL)
                        t = netdev_priv(dev);
-
                memcpy(&p, &t->parms, sizeof (p));
                if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
                        err = -EFAULT;
@@ -947,35 +951,36 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
        case SIOCADDTUNNEL:
        case SIOCCHGTUNNEL:
                err = -EPERM;
-               create = (cmd == SIOCADDTUNNEL);
                if (!capable(CAP_NET_ADMIN))
                        break;
-               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
-                       err = -EFAULT;
+               err = -EFAULT;
+               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
                        break;
-               }
-               if (!create && dev != ip6ip6_fb_tnl_dev) {
-                       t = netdev_priv(dev);
-               }
-               if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
+               err = -EINVAL;
+               if (p.proto != IPPROTO_IPV6)
                        break;
-               }
-               if (cmd == SIOCCHGTUNNEL) {
-                       if (t->dev != dev) {
-                               err = -EEXIST;
-                               break;
-                       }
+               t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
+               if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
+                       if (t != NULL) {
+                               if (t->dev != dev) {
+                                       err = -EEXIST;
+                                       break;
+                               }
+                       } else
+                               t = netdev_priv(dev);
+
                        ip6ip6_tnl_unlink(t);
                        err = ip6ip6_tnl_change(t, &p);
                        ip6ip6_tnl_link(t);
                        netdev_state_change(dev);
                }
-               if (copy_to_user(ifr->ifr_ifru.ifru_data,
-                                &t->parms, sizeof (p))) {
-                       err = -EFAULT;
-               } else {
+               if (t) {
                        err = 0;
-               }
+                       if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
+                               err = -EFAULT;
+
+               } else
+                       err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
                break;
        case SIOCDELTUNNEL:
                err = -EPERM;
@@ -983,22 +988,19 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                        break;
 
                if (dev == ip6ip6_fb_tnl_dev) {
-                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
-                                          sizeof (p))) {
-                               err = -EFAULT;
+                       err = -EFAULT;
+                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
                                break;
-                       }
-                       err = ip6ip6_tnl_locate(&p, &t, 0);
-                       if (err)
+                       err = -ENOENT;
+                       if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
                                break;
-                       if (t == netdev_priv(ip6ip6_fb_tnl_dev)) {
-                               err = -EPERM;
+                       err = -EPERM;
+                       if (t->dev == ip6ip6_fb_tnl_dev)
                                break;
-                       }
-               } else {
-                       t = netdev_priv(dev);
+                       dev = t->dev;
                }
-               err = unregister_netdevice(t->dev);
+               err = 0;
+               unregister_netdevice(dev);
                break;
        default:
                err = -EINVAL;
@@ -1007,7 +1009,7 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 }
 
 /**
- * ip6ip6_tnl_get_stats - return the stats for tunnel device 
+ * ip6ip6_tnl_get_stats - return the stats for tunnel device
  *   @dev: virtual device associated with tunnel
  *
  * Return: stats for device
@@ -1100,7 +1102,7 @@ ip6ip6_tnl_dev_init(struct net_device *dev)
  * Return: 0
  **/
 
-static int 
+static int
 ip6ip6_fb_tnl_dev_init(struct net_device *dev)
 {
        struct ip6_tnl *t = netdev_priv(dev);
@@ -1149,6 +1151,20 @@ fail:
        return err;
 }
 
+static void __exit ip6ip6_destroy_tunnels(void)
+{
+       int h;
+       struct ip6_tnl *t;
+
+       for (h = 0; h < HASH_SIZE; h++) {
+               while ((t = tnls_r_l[h]) != NULL)
+                       unregister_netdevice(t->dev);
+       }
+
+       t = tnls_wc[0];
+       unregister_netdevice(t->dev);
+}
+
 /**
  * ip6_tunnel_cleanup - free resources and unregister protocol
  **/
@@ -1158,7 +1174,9 @@ static void __exit ip6_tunnel_cleanup(void)
        if (xfrm6_tunnel_deregister(&ip6ip6_handler))
                printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
 
-       unregister_netdev(ip6ip6_fb_tnl_dev);
+       rtnl_lock();
+       ip6ip6_destroy_tunnels();
+       rtnl_unlock();
 }
 
 module_init(ip6_tunnel_init);