Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / net / ipv6 / ip6_tunnel.c
index e815380..a995796 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <linux/config.h>
 #include <linux/module.h>
+#include <linux/capability.h>
 #include <linux/errno.h>
 #include <linux/types.h>
 #include <linux/sockios.h>
 
 #include <net/ip.h>
 #include <net/ipv6.h>
-#include <net/protocol.h>
 #include <net/ip6_route.h>
 #include <net/addrconf.h>
 #include <net/ip6_tunnel.h>
 #include <net/xfrm.h>
+#include <net/dsfield.h>
+#include <net/inet_ecn.h>
 
 MODULE_AUTHOR("Ville Nuorvala");
 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
@@ -83,7 +85,7 @@ static struct ip6_tnl *tnls_wc[1];
 static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
 
 /* lock for the tunnel lists */
-static rwlock_t ip6ip6_lock = RW_LOCK_UNLOCKED;
+static DEFINE_RWLOCK(ip6ip6_lock);
 
 static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
 {
@@ -92,6 +94,7 @@ static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
        if (dst && dst->obsolete && 
            dst->ops->check(dst, t->dst_cookie) == NULL) {
                t->dst_cache = NULL;
+               dst_release(dst);
                return NULL;
        }
 
@@ -123,7 +126,7 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  *   else %NULL
  **/
 
-struct ip6_tnl *
+static struct ip6_tnl *
 ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
 {
        unsigned h0 = HASH(remote);
@@ -131,8 +134,8 @@ ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
        struct ip6_tnl *t;
 
        for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
-               if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
-                   !ipv6_addr_cmp(remote, &t->parms.raddr) &&
+               if (ipv6_addr_equal(local, &t->parms.laddr) &&
+                   ipv6_addr_equal(remote, &t->parms.raddr) &&
                    (t->dev->flags & IFF_UP))
                        return t;
        }
@@ -178,10 +181,10 @@ ip6ip6_tnl_link(struct ip6_tnl *t)
 {
        struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
 
-       write_lock_bh(&ip6ip6_lock);
        t->next = *tp;
-       write_unlock_bh(&ip6ip6_lock);
+       write_lock_bh(&ip6ip6_lock);
        *tp = t;
+       write_unlock_bh(&ip6ip6_lock);
 }
 
 /**
@@ -240,7 +243,7 @@ ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt)
        if (dev == NULL)
                return -ENOMEM;
 
-       t = dev->priv;
+       t = netdev_priv(dev);
        dev->init = ip6ip6_tnl_dev_init;
        t->parms = *p;
 
@@ -282,8 +285,8 @@ ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
                return -EINVAL;
 
        for (t = *ip6ip6_bucket(p); t; t = t->next) {
-               if (!ipv6_addr_cmp(local, &t->parms.laddr) &&
-                   !ipv6_addr_cmp(remote, &t->parms.raddr)) {
+               if (ipv6_addr_equal(local, &t->parms.laddr) &&
+                   ipv6_addr_equal(remote, &t->parms.raddr)) {
                        *pt = t;
                        return (create ? -EEXIST : 0);
                }
@@ -305,7 +308,7 @@ ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create)
 static void
 ip6ip6_tnl_dev_uninit(struct net_device *dev)
 {
-       struct ip6_tnl *t = dev->priv;
+       struct ip6_tnl *t = netdev_priv(dev);
 
        if (dev == ip6ip6_fb_tnl_dev) {
                write_lock_bh(&ip6ip6_lock);
@@ -387,8 +390,9 @@ parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
  *   to the specifications in RFC 2473.
  **/
 
-void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
-                  int type, int code, int offset, __u32 info)
+static int
+ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
+          int type, int code, int offset, __u32 info)
 {
        struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
        struct ip6_tnl *t;
@@ -397,6 +401,7 @@ void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        int rel_code = ICMPV6_ADDR_UNREACH;
        __u32 rel_info = 0;
        __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 
@@ -406,6 +411,8 @@ void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
                goto out;
 
+       err = 0;
+
        switch (type) {
                __u32 teli;
                struct ipv6_tlv_tnl_enc_lim *tel;
@@ -453,7 +460,7 @@ void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                        mtu = IPV6_MIN_MTU;
                t->dev->mtu = mtu;
 
-               if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) {
+               if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
                        rel_type = ICMPV6_PKT_TOOBIG;
                        rel_code = 0;
                        rel_info = mtu;
@@ -487,6 +494,16 @@ void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        }
 out:
        read_unlock(&ip6ip6_lock);
+       return err;
+}
+
+static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph,
+                                         struct sk_buff *skb)
+{
+       struct ipv6hdr *inner_iph = skb->nh.ipv6h;
+
+       if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
+               IP6_ECN_set_ce(inner_iph);
 }
 
 /**
@@ -496,20 +513,22 @@ out:
  * Return: 0
  **/
 
-int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
+static int 
+ip6ip6_rcv(struct sk_buff *skb)
 {
-       struct sk_buff *skb = *pskb;
        struct ipv6hdr *ipv6h;
        struct ip6_tnl *t;
 
-       if (!pskb_may_pull(skb, sizeof (*ipv6h)))
-               goto discard;
-
        ipv6h = skb->nh.ipv6h;
 
        read_lock(&ip6ip6_lock);
 
        if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
+               if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
+                       read_unlock(&ip6ip6_lock);
+                       goto discard;
+               }
+
                if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) {
                        t->stat.rx_dropped++;
                        read_unlock(&ip6ip6_lock);
@@ -524,6 +543,9 @@ int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
                skb->dev = t->dev;
                dst_release(skb->dst);
                skb->dst = NULL;
+               if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
+                       ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
+               ip6ip6_ecn_decapsulate(ipv6h, skb);
                t->stat.rx_packets++;
                t->stat.rx_bytes += skb->len;
                netif_rx(skb);
@@ -531,7 +553,8 @@ int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
                return 0;
        }
        read_unlock(&ip6ip6_lock);
-       icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
+       return 1;
+
 discard:
        kfree_skb(skb);
        return 0;
@@ -582,7 +605,7 @@ static inline struct ipv6_txoptions *create_tel(__u8 encap_limit)
 static inline int
 ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
 {
-       return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);
+       return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
 }
 
 /**
@@ -598,9 +621,10 @@ ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  *   0
  **/
 
-int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
+static int 
+ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-       struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
+       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;
@@ -614,6 +638,7 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        u8 proto;
        int err;
        int pkt_len;
+       int dsfield;
 
        if (t->recursion++) {
                stats->collisions++;
@@ -639,6 +664,7 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        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);
        if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
@@ -649,11 +675,12 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
 
        if ((dst = ip6_tnl_dst_check(t)) != NULL)
                dst_hold(dst);
-       else
+       else {
                dst = ip6_route_output(NULL, &fl);
 
-       if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
-               goto tx_err_link_failure;
+               if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
+                       goto tx_err_link_failure;
+       }
 
        tdev = dst->dev;
 
@@ -665,14 +692,14 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
                               t->parms.name);
                goto tx_err_dst_release;
        }
-       mtu = dst_pmtu(dst) - sizeof (*ipv6h);
+       mtu = dst_mtu(dst) - sizeof (*ipv6h);
        if (opt) {
                max_headroom += 8;
                mtu -= 8;
        }
        if (mtu < IPV6_MIN_MTU)
                mtu = IPV6_MIN_MTU;
-       if (skb->dst && mtu < dst_pmtu(skb->dst)) {
+       if (skb->dst && mtu < dst_mtu(skb->dst)) {
                struct rt6_info *rt = (struct rt6_info *) skb->dst;
                rt->rt6i_flags |= RTF_MODIFIED;
                rt->u.dst.metrics[RTAX_MTU-1] = mtu;
@@ -710,18 +737,14 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
        ipv6h = skb->nh.ipv6h;
        *(u32*)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));
        ipv6h->hop_limit = t->parms.hop_limit;
        ipv6h->nexthdr = proto;
        ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
        ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
-#ifdef CONFIG_NETFILTER
-       nf_conntrack_put(skb->nfct);
-       skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-       skb->nf_debug = 0;
-#endif
-#endif
+       nf_reset(skb);
        pkt_len = skb->len;
        err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 
                      skb->dst->dev, dst_output);
@@ -735,8 +758,7 @@ int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        }
        ip6_tnl_dst_store(t, dst);
 
-       if (opt)
-               kfree(opt);
+       kfree(opt);
 
        t->recursion--;
        return 0;
@@ -745,8 +767,7 @@ tx_err_link_failure:
        dst_link_failure(skb);
 tx_err_dst_release:
        dst_release(dst);
-       if (opt)
-               kfree(opt);
+       kfree(opt);
 tx_err:
        stats->tx_errors++;
        stats->tx_dropped++;
@@ -862,6 +883,8 @@ ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
        t->parms.hop_limit = p->hop_limit;
        t->parms.encap_limit = p->encap_limit;
        t->parms.flowinfo = p->flowinfo;
+       t->parms.link = p->link;
+       ip6_tnl_dst_reset(t);
        ip6ip6_tnl_link_config(t);
        return 0;
 }
@@ -912,11 +935,11 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                                break;
                        }
                        if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)
-                               t = (struct ip6_tnl *) dev->priv;
+                               t = netdev_priv(dev);
                        else if (err)
                                break;
                } else
-                       t = (struct ip6_tnl *) dev->priv;
+                       t = netdev_priv(dev);
 
                memcpy(&p, &t->parms, sizeof (p));
                if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
@@ -934,7 +957,7 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                        break;
                }
                if (!create && dev != ip6ip6_fb_tnl_dev) {
-                       t = (struct ip6_tnl *) dev->priv;
+                       t = netdev_priv(dev);
                }
                if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {
                        break;
@@ -970,12 +993,12 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                        err = ip6ip6_tnl_locate(&p, &t, 0);
                        if (err)
                                break;
-                       if (t == ip6ip6_fb_tnl_dev->priv) {
+                       if (t == netdev_priv(ip6ip6_fb_tnl_dev)) {
                                err = -EPERM;
                                break;
                        }
                } else {
-                       t = (struct ip6_tnl *) dev->priv;
+                       t = netdev_priv(dev);
                }
                err = unregister_netdevice(t->dev);
                break;
@@ -995,7 +1018,7 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 static struct net_device_stats *
 ip6ip6_tnl_get_stats(struct net_device *dev)
 {
-       return &(((struct ip6_tnl *) dev->priv)->stat);
+       return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
 }
 
 /**
@@ -1052,7 +1075,7 @@ static void ip6ip6_tnl_dev_setup(struct net_device *dev)
 static inline void
 ip6ip6_tnl_dev_init_gen(struct net_device *dev)
 {
-       struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
+       struct ip6_tnl *t = netdev_priv(dev);
        t->fl.proto = IPPROTO_IPV6;
        t->dev = dev;
        strcpy(t->parms.name, dev->name);
@@ -1066,7 +1089,7 @@ ip6ip6_tnl_dev_init_gen(struct net_device *dev)
 static int
 ip6ip6_tnl_dev_init(struct net_device *dev)
 {
-       struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;
+       struct ip6_tnl *t = netdev_priv(dev);
        ip6ip6_tnl_dev_init_gen(dev);
        ip6ip6_tnl_link_config(t);
        return 0;
@@ -1079,19 +1102,20 @@ ip6ip6_tnl_dev_init(struct net_device *dev)
  * Return: 0
  **/
 
-int ip6ip6_fb_tnl_dev_init(struct net_device *dev)
+static int 
+ip6ip6_fb_tnl_dev_init(struct net_device *dev)
 {
-       struct ip6_tnl *t = dev->priv;
+       struct ip6_tnl *t = netdev_priv(dev);
        ip6ip6_tnl_dev_init_gen(dev);
        dev_hold(dev);
        tnls_wc[0] = t;
        return 0;
 }
 
-static struct inet6_protocol ip6ip6_protocol = {
-       .handler = ip6ip6_rcv,
-       .err_handler = ip6ip6_err,
-       .flags = INET6_PROTO_FINAL
+static struct xfrm6_tunnel ip6ip6_handler = {
+       .handler        = ip6ip6_rcv,
+       .err_handler    = ip6ip6_err,
+       .priority       =       1,
 };
 
 /**
@@ -1104,9 +1128,9 @@ static int __init ip6_tunnel_init(void)
 {
        int  err;
 
-       if ((err = inet6_add_protocol(&ip6ip6_protocol, IPPROTO_IPV6)) < 0) {
-               printk(KERN_ERR "Failed to register IPv6 protocol\n");
-               return err;
+       if (xfrm6_tunnel_register(&ip6ip6_handler)) {
+               printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
+               return -EAGAIN;
        }
        ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
                                         ip6ip6_tnl_dev_setup);
@@ -1123,7 +1147,7 @@ static int __init ip6_tunnel_init(void)
        }
        return 0;
 fail:
-       inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
+       xfrm6_tunnel_deregister(&ip6ip6_handler);
        return err;
 }
 
@@ -1133,8 +1157,10 @@ fail:
 
 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);
-       inet6_del_protocol(&ip6ip6_protocol, IPPROTO_IPV6);
 }
 
 module_init(ip6_tunnel_init);