]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - net/ipv4/ip_gre.c
[GRE]: Use proper net in hash-lookup functions.
[net-next-2.6.git] / net / ipv4 / ip_gre.c
index 63f691719353dbc98eb34211b86d9492b8d58190..1a17c5beffdc64143467b815507145e8d161904f 100644 (file)
@@ -39,6 +39,8 @@
 #include <net/dsfield.h>
 #include <net/inet_ecn.h>
 #include <net/xfrm.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
 
 #ifdef CONFIG_IPV6
 #include <net/ipv6.h>
@@ -122,6 +124,10 @@ static void ipgre_tunnel_setup(struct net_device *dev);
 
 static int ipgre_fb_tunnel_init(struct net_device *dev);
 
+static int ipgre_net_id;
+struct ipgre_net {
+};
+
 static struct net_device *ipgre_fb_tunnel_dev;
 
 /* Tunnel hash table */
@@ -156,7 +162,8 @@ static DEFINE_RWLOCK(ipgre_lock);
 
 /* Given src, dst and key, find appropriate for input tunnel. */
 
-static struct ip_tunnel * ipgre_tunnel_lookup(__be32 remote, __be32 local, __be32 key)
+static struct ip_tunnel * ipgre_tunnel_lookup(struct net *net,
+               __be32 remote, __be32 local, __be32 key)
 {
        unsigned h0 = HASH(remote);
        unsigned h1 = HASH(key);
@@ -192,7 +199,8 @@ static struct ip_tunnel * ipgre_tunnel_lookup(__be32 remote, __be32 local, __be3
        return NULL;
 }
 
-static struct ip_tunnel **__ipgre_bucket(struct ip_tunnel_parm *parms)
+static struct ip_tunnel **__ipgre_bucket(struct ipgre_net *ign,
+               struct ip_tunnel_parm *parms)
 {
        __be32 remote = parms->iph.daddr;
        __be32 local = parms->iph.saddr;
@@ -210,14 +218,15 @@ static struct ip_tunnel **__ipgre_bucket(struct ip_tunnel_parm *parms)
        return &tunnels[prio][h];
 }
 
-static inline struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t)
+static inline struct ip_tunnel **ipgre_bucket(struct ipgre_net *ign,
+               struct ip_tunnel *t)
 {
-       return __ipgre_bucket(&t->parms);
+       return __ipgre_bucket(ign, &t->parms);
 }
 
-static void ipgre_tunnel_link(struct ip_tunnel *t)
+static void ipgre_tunnel_link(struct ipgre_net *ign, struct ip_tunnel *t)
 {
-       struct ip_tunnel **tp = ipgre_bucket(t);
+       struct ip_tunnel **tp = ipgre_bucket(ign, t);
 
        t->next = *tp;
        write_lock_bh(&ipgre_lock);
@@ -225,11 +234,11 @@ static void ipgre_tunnel_link(struct ip_tunnel *t)
        write_unlock_bh(&ipgre_lock);
 }
 
-static void ipgre_tunnel_unlink(struct ip_tunnel *t)
+static void ipgre_tunnel_unlink(struct ipgre_net *ign, struct ip_tunnel *t)
 {
        struct ip_tunnel **tp;
 
-       for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) {
+       for (tp = ipgre_bucket(ign, t); *tp; tp = &(*tp)->next) {
                if (t == *tp) {
                        write_lock_bh(&ipgre_lock);
                        *tp = t->next;
@@ -239,7 +248,8 @@ static void ipgre_tunnel_unlink(struct ip_tunnel *t)
        }
 }
 
-static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create)
+static struct ip_tunnel * ipgre_tunnel_locate(struct net *net,
+               struct ip_tunnel_parm *parms, int create)
 {
        __be32 remote = parms->iph.daddr;
        __be32 local = parms->iph.saddr;
@@ -247,8 +257,9 @@ static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int
        struct ip_tunnel *t, **tp, *nt;
        struct net_device *dev;
        char name[IFNAMSIZ];
+       struct ipgre_net *ign = net_generic(net, ipgre_net_id);
 
-       for (tp = __ipgre_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
+       for (tp = __ipgre_bucket(ign, parms); (t = *tp) != NULL; tp = &t->next) {
                if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
                        if (key == t->parms.i_key)
                                return t;
@@ -259,41 +270,40 @@ static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int
 
        if (parms->name[0])
                strlcpy(name, parms->name, IFNAMSIZ);
-       else {
-               int i;
-               for (i=1; i<100; i++) {
-                       sprintf(name, "gre%d", i);
-                       if (__dev_get_by_name(&init_net, name) == NULL)
-                               break;
-               }
-               if (i==100)
-                       goto failed;
-       }
+       else
+               sprintf(name, "gre%%d");
 
        dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
        if (!dev)
          return NULL;
 
+       if (strchr(name, '%')) {
+               if (dev_alloc_name(dev, name) < 0)
+                       goto failed_free;
+       }
+
        dev->init = ipgre_tunnel_init;
        nt = netdev_priv(dev);
        nt->parms = *parms;
 
-       if (register_netdevice(dev) < 0) {
-               free_netdev(dev);
-               goto failed;
-       }
+       if (register_netdevice(dev) < 0)
+               goto failed_free;
 
        dev_hold(dev);
-       ipgre_tunnel_link(nt);
+       ipgre_tunnel_link(ign, nt);
        return nt;
 
-failed:
+failed_free:
+       free_netdev(dev);
        return NULL;
 }
 
 static void ipgre_tunnel_uninit(struct net_device *dev)
 {
-       ipgre_tunnel_unlink(netdev_priv(dev));
+       struct net *net = dev_net(dev);
+       struct ipgre_net *ign = net_generic(net, ipgre_net_id);
+
+       ipgre_tunnel_unlink(ign, netdev_priv(dev));
        dev_put(dev);
 }
 
@@ -367,7 +377,9 @@ static void ipgre_err(struct sk_buff *skb, u32 info)
        }
 
        read_lock(&ipgre_lock);
-       t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((__be32*)p) + (grehlen>>2) - 1) : 0);
+       t = ipgre_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr,
+                       (flags&GRE_KEY) ?
+                       *(((__be32*)p) + (grehlen>>2) - 1) : 0);
        if (t == NULL || t->parms.iph.daddr == 0 ||
            ipv4_is_multicast(t->parms.iph.daddr))
                goto out;
@@ -600,7 +612,8 @@ static int ipgre_rcv(struct sk_buff *skb)
        }
 
        read_lock(&ipgre_lock);
-       if ((tunnel = ipgre_tunnel_lookup(iph->saddr, iph->daddr, key)) != NULL) {
+       if ((tunnel = ipgre_tunnel_lookup(dev_net(skb->dev),
+                                       iph->saddr, iph->daddr, key)) != NULL) {
                secpath_reset(skb);
 
                skb->protocol = *(__be16*)(h + 2);
@@ -623,7 +636,7 @@ static int ipgre_rcv(struct sk_buff *skb)
 #ifdef CONFIG_NET_IPGRE_BROADCAST
                if (ipv4_is_multicast(iph->daddr)) {
                        /* Looped back packet, drop it! */
-                       if (((struct rtable*)skb->dst)->fl.iif == 0)
+                       if (skb->rtable->fl.iif == 0)
                                goto drop;
                        tunnel->stat.multicast++;
                        skb->pkt_type = PACKET_BROADCAST;
@@ -703,7 +716,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                }
 
                if (skb->protocol == htons(ETH_P_IP)) {
-                       rt = (struct rtable*)skb->dst;
+                       rt = skb->rtable;
                        if ((dst = rt->rt_gateway) == 0)
                                goto tx_error_icmp;
                }
@@ -958,6 +971,8 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
        int err = 0;
        struct ip_tunnel_parm p;
        struct ip_tunnel *t;
+       struct net *net = dev_net(dev);
+       struct ipgre_net *ign = net_generic(net, ipgre_net_id);
 
        switch (cmd) {
        case SIOCGETTUNNEL:
@@ -967,7 +982,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                                err = -EFAULT;
                                break;
                        }
-                       t = ipgre_tunnel_locate(&p, 0);
+                       t = ipgre_tunnel_locate(net, &p, 0);
                }
                if (t == NULL)
                        t = netdev_priv(dev);
@@ -999,7 +1014,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                if (!(p.o_flags&GRE_KEY))
                        p.o_key = 0;
 
-               t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
+               t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
 
                if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
                        if (t != NULL) {
@@ -1021,14 +1036,14 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                                        err = -EINVAL;
                                        break;
                                }
-                               ipgre_tunnel_unlink(t);
+                               ipgre_tunnel_unlink(ign, t);
                                t->parms.iph.saddr = p.iph.saddr;
                                t->parms.iph.daddr = p.iph.daddr;
                                t->parms.i_key = p.i_key;
                                t->parms.o_key = p.o_key;
                                memcpy(dev->dev_addr, &p.iph.saddr, 4);
                                memcpy(dev->broadcast, &p.iph.daddr, 4);
-                               ipgre_tunnel_link(t);
+                               ipgre_tunnel_link(ign, t);
                                netdev_state_change(dev);
                        }
                }
@@ -1061,7 +1076,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
                                goto done;
                        err = -ENOENT;
-                       if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
+                       if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL)
                                goto done;
                        err = -EPERM;
                        if (t == netdev_priv(ipgre_fb_tunnel_dev))
@@ -1194,7 +1209,7 @@ static int ipgre_close(struct net_device *dev)
        struct ip_tunnel *t = netdev_priv(dev);
        if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
                struct in_device *in_dev;
-               in_dev = inetdev_by_index(dev->nd_net, t->mlink);
+               in_dev = inetdev_by_index(dev_net(dev), t->mlink);
                if (in_dev) {
                        ip_mc_dec_group(in_dev, t->parms.iph.daddr);
                        in_dev_put(in_dev);
@@ -1279,6 +1294,40 @@ static struct net_protocol ipgre_protocol = {
        .err_handler    =       ipgre_err,
 };
 
+static int ipgre_init_net(struct net *net)
+{
+       int err;
+       struct ipgre_net *ign;
+
+       err = -ENOMEM;
+       ign = kmalloc(sizeof(struct ipgre_net), GFP_KERNEL);
+       if (ign == NULL)
+               goto err_alloc;
+
+       err = net_assign_generic(net, ipgre_net_id, ign);
+       if (err < 0)
+               goto err_assign;
+
+       return 0;
+
+err_assign:
+       kfree(ign);
+err_alloc:
+       return err;
+}
+
+static void ipgre_exit_net(struct net *net)
+{
+       struct ipgre_net *ign;
+
+       ign = net_generic(net, ipgre_net_id);
+       kfree(ign);
+}
+
+static struct pernet_operations ipgre_net_ops = {
+       .init = ipgre_init_net,
+       .exit = ipgre_exit_net,
+};
 
 /*
  *     And now the modules code and kernel interface.
@@ -1306,6 +1355,10 @@ static int __init ipgre_init(void)
 
        if ((err = register_netdev(ipgre_fb_tunnel_dev)))
                goto err2;
+
+       err = register_pernet_gen_device(&ipgre_net_id, &ipgre_net_ops);
+       if (err < 0)
+               goto err3;
 out:
        return err;
 err2:
@@ -1313,6 +1366,9 @@ err2:
 err1:
        inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
        goto out;
+err3:
+       unregister_netdevice(ipgre_fb_tunnel_dev);
+       goto err1;
 }
 
 static void __exit ipgre_destroy_tunnels(void)
@@ -1337,6 +1393,8 @@ static void __exit ipgre_fini(void)
        rtnl_lock();
        ipgre_destroy_tunnels();
        rtnl_unlock();
+
+       unregister_pernet_gen_device(ipgre_net_id, &ipgre_net_ops);
 }
 
 module_init(ipgre_init);