]> Pileus Git - ~andy/linux/blobdiff - net/ipv4/ip_gre.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[~andy/linux] / net / ipv4 / ip_gre.c
index 1a17c5beffdc64143467b815507145e8d161904f..2ada033406de08b6e471287867001a63a33336e2 100644 (file)
@@ -124,11 +124,14 @@ static void ipgre_tunnel_setup(struct net_device *dev);
 
 static int ipgre_fb_tunnel_init(struct net_device *dev);
 
+#define HASH_SIZE  16
+
 static int ipgre_net_id;
 struct ipgre_net {
-};
+       struct ip_tunnel *tunnels[4][HASH_SIZE];
 
-static struct net_device *ipgre_fb_tunnel_dev;
+       struct net_device *fb_tunnel_dev;
+};
 
 /* Tunnel hash table */
 
@@ -148,15 +151,12 @@ static struct net_device *ipgre_fb_tunnel_dev;
    will match fallback tunnel.
  */
 
-#define HASH_SIZE  16
 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
 
-static struct ip_tunnel *tunnels[4][HASH_SIZE];
-
-#define tunnels_r_l    (tunnels[3])
-#define tunnels_r      (tunnels[2])
-#define tunnels_l      (tunnels[1])
-#define tunnels_wc     (tunnels[0])
+#define tunnels_r_l    tunnels[3]
+#define tunnels_r      tunnels[2]
+#define tunnels_l      tunnels[1]
+#define tunnels_wc     tunnels[0]
 
 static DEFINE_RWLOCK(ipgre_lock);
 
@@ -168,20 +168,21 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net *net,
        unsigned h0 = HASH(remote);
        unsigned h1 = HASH(key);
        struct ip_tunnel *t;
+       struct ipgre_net *ign = net_generic(net, ipgre_net_id);
 
-       for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
+       for (t = ign->tunnels_r_l[h0^h1]; t; t = t->next) {
                if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
                        if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
                                return t;
                }
        }
-       for (t = tunnels_r[h0^h1]; t; t = t->next) {
+       for (t = ign->tunnels_r[h0^h1]; t; t = t->next) {
                if (remote == t->parms.iph.daddr) {
                        if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
                                return t;
                }
        }
-       for (t = tunnels_l[h1]; t; t = t->next) {
+       for (t = ign->tunnels_l[h1]; t; t = t->next) {
                if (local == t->parms.iph.saddr ||
                     (local == t->parms.iph.daddr &&
                      ipv4_is_multicast(local))) {
@@ -189,13 +190,13 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net *net,
                                return t;
                }
        }
-       for (t = tunnels_wc[h1]; t; t = t->next) {
+       for (t = ign->tunnels_wc[h1]; t; t = t->next) {
                if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
                        return t;
        }
 
-       if (ipgre_fb_tunnel_dev->flags&IFF_UP)
-               return netdev_priv(ipgre_fb_tunnel_dev);
+       if (ign->fb_tunnel_dev->flags&IFF_UP)
+               return netdev_priv(ign->fb_tunnel_dev);
        return NULL;
 }
 
@@ -215,7 +216,7 @@ static struct ip_tunnel **__ipgre_bucket(struct ipgre_net *ign,
                h ^= HASH(remote);
        }
 
-       return &tunnels[prio][h];
+       return &ign->tunnels[prio][h];
 }
 
 static inline struct ip_tunnel **ipgre_bucket(struct ipgre_net *ign,
@@ -277,6 +278,8 @@ static struct ip_tunnel * ipgre_tunnel_locate(struct net *net,
        if (!dev)
          return NULL;
 
+       dev_net_set(dev, net);
+
        if (strchr(name, '%')) {
                if (dev_alloc_name(dev, name) < 0)
                        goto failed_free;
@@ -492,7 +495,7 @@ out:
        fl.fl4_dst = eiph->saddr;
        fl.fl4_tos = RT_TOS(eiph->tos);
        fl.proto = IPPROTO_GRE;
-       if (ip_route_output_key(&init_net, &rt, &fl)) {
+       if (ip_route_output_key(dev_net(skb->dev), &rt, &fl)) {
                kfree_skb(skb2);
                return;
        }
@@ -505,7 +508,7 @@ out:
                fl.fl4_dst = eiph->daddr;
                fl.fl4_src = eiph->saddr;
                fl.fl4_tos = eiph->tos;
-               if (ip_route_output_key(&init_net, &rt, &fl) ||
+               if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) ||
                    rt->u.dst.dev->type != ARPHRD_IPGRE) {
                        ip_rt_put(rt);
                        kfree_skb(skb2);
@@ -761,7 +764,7 @@ static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                                                .saddr = tiph->saddr,
                                                .tos = RT_TOS(tos) } },
                                    .proto = IPPROTO_GRE };
-               if (ip_route_output_key(&init_net, &rt, &fl)) {
+               if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
                        tunnel->stat.tx_carrier_errors++;
                        goto tx_error;
                }
@@ -934,7 +937,7 @@ static void ipgre_tunnel_bind_dev(struct net_device *dev)
                                                .tos = RT_TOS(iph->tos) } },
                                    .proto = IPPROTO_GRE };
                struct rtable *rt;
-               if (!ip_route_output_key(&init_net, &rt, &fl)) {
+               if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
                        tdev = rt->u.dst.dev;
                        ip_rt_put(rt);
                }
@@ -942,7 +945,7 @@ static void ipgre_tunnel_bind_dev(struct net_device *dev)
        }
 
        if (!tdev && tunnel->parms.link)
-               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
+               tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
 
        if (tdev) {
                hlen = tdev->hard_header_len;
@@ -977,7 +980,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
        switch (cmd) {
        case SIOCGETTUNNEL:
                t = NULL;
-               if (dev == ipgre_fb_tunnel_dev) {
+               if (dev == ign->fb_tunnel_dev) {
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
                                err = -EFAULT;
                                break;
@@ -1016,7 +1019,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
 
                t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
 
-               if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
+               if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
                        if (t != NULL) {
                                if (t->dev != dev) {
                                        err = -EEXIST;
@@ -1071,7 +1074,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                if (!capable(CAP_NET_ADMIN))
                        goto done;
 
-               if (dev == ipgre_fb_tunnel_dev) {
+               if (dev == ign->fb_tunnel_dev) {
                        err = -EFAULT;
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
                                goto done;
@@ -1079,7 +1082,7 @@ ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
                        if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL)
                                goto done;
                        err = -EPERM;
-                       if (t == netdev_priv(ipgre_fb_tunnel_dev))
+                       if (t == netdev_priv(ign->fb_tunnel_dev))
                                goto done;
                        dev = t->dev;
                }
@@ -1192,7 +1195,7 @@ static int ipgre_open(struct net_device *dev)
                                                .tos = RT_TOS(t->parms.iph.tos) } },
                                    .proto = IPPROTO_GRE };
                struct rtable *rt;
-               if (ip_route_output_key(&init_net, &rt, &fl))
+               if (ip_route_output_key(dev_net(dev), &rt, &fl))
                        return -EADDRNOTAVAIL;
                dev = rt->u.dst.dev;
                ip_rt_put(rt);
@@ -1235,6 +1238,7 @@ static void ipgre_tunnel_setup(struct net_device *dev)
        dev->flags              = IFF_NOARP;
        dev->iflink             = 0;
        dev->addr_len           = 4;
+       dev->features           |= NETIF_F_NETNS_LOCAL;
 }
 
 static int ipgre_tunnel_init(struct net_device *dev)
@@ -1270,10 +1274,11 @@ static int ipgre_tunnel_init(struct net_device *dev)
        return 0;
 }
 
-static int __init ipgre_fb_tunnel_init(struct net_device *dev)
+static int ipgre_fb_tunnel_init(struct net_device *dev)
 {
        struct ip_tunnel *tunnel = netdev_priv(dev);
        struct iphdr *iph = &tunnel->parms.iph;
+       struct ipgre_net *ign = net_generic(dev_net(dev), ipgre_net_id);
 
        tunnel->dev = dev;
        strcpy(tunnel->parms.name, dev->name);
@@ -1284,7 +1289,7 @@ static int __init ipgre_fb_tunnel_init(struct net_device *dev)
        tunnel->hlen            = sizeof(struct iphdr) + 4;
 
        dev_hold(dev);
-       tunnels_wc[0]           = tunnel;
+       ign->tunnels_wc[0]      = tunnel;
        return 0;
 }
 
@@ -1292,15 +1297,30 @@ static int __init ipgre_fb_tunnel_init(struct net_device *dev)
 static struct net_protocol ipgre_protocol = {
        .handler        =       ipgre_rcv,
        .err_handler    =       ipgre_err,
+       .netns_ok       =       1,
 };
 
+static void ipgre_destroy_tunnels(struct ipgre_net *ign)
+{
+       int prio;
+
+       for (prio = 0; prio < 4; prio++) {
+               int h;
+               for (h = 0; h < HASH_SIZE; h++) {
+                       struct ip_tunnel *t;
+                       while ((t = ign->tunnels[prio][h]) != NULL)
+                               unregister_netdevice(t->dev);
+               }
+       }
+}
+
 static int ipgre_init_net(struct net *net)
 {
        int err;
        struct ipgre_net *ign;
 
        err = -ENOMEM;
-       ign = kmalloc(sizeof(struct ipgre_net), GFP_KERNEL);
+       ign = kzalloc(sizeof(struct ipgre_net), GFP_KERNEL);
        if (ign == NULL)
                goto err_alloc;
 
@@ -1308,8 +1328,25 @@ static int ipgre_init_net(struct net *net)
        if (err < 0)
                goto err_assign;
 
+       ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
+                                          ipgre_tunnel_setup);
+       if (!ign->fb_tunnel_dev) {
+               err = -ENOMEM;
+               goto err_alloc_dev;
+       }
+
+       ign->fb_tunnel_dev->init = ipgre_fb_tunnel_init;
+       dev_net_set(ign->fb_tunnel_dev, net);
+
+       if ((err = register_netdev(ign->fb_tunnel_dev)))
+               goto err_reg_dev;
+
        return 0;
 
+err_reg_dev:
+       free_netdev(ign->fb_tunnel_dev);
+err_alloc_dev:
+       /* nothing */
 err_assign:
        kfree(ign);
 err_alloc:
@@ -1321,6 +1358,9 @@ static void ipgre_exit_net(struct net *net)
        struct ipgre_net *ign;
 
        ign = net_generic(net, ipgre_net_id);
+       rtnl_lock();
+       ipgre_destroy_tunnels(ign);
+       rtnl_unlock();
        kfree(ign);
 }
 
@@ -1344,45 +1384,11 @@ static int __init ipgre_init(void)
                return -EAGAIN;
        }
 
-       ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
-                                          ipgre_tunnel_setup);
-       if (!ipgre_fb_tunnel_dev) {
-               err = -ENOMEM;
-               goto err1;
-       }
-
-       ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
-
-       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:
-       free_netdev(ipgre_fb_tunnel_dev);
-err1:
-       inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
-       goto out;
-err3:
-       unregister_netdevice(ipgre_fb_tunnel_dev);
-       goto err1;
-}
+               inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
 
-static void __exit ipgre_destroy_tunnels(void)
-{
-       int prio;
-
-       for (prio = 0; prio < 4; prio++) {
-               int h;
-               for (h = 0; h < HASH_SIZE; h++) {
-                       struct ip_tunnel *t;
-                       while ((t = tunnels[prio][h]) != NULL)
-                               unregister_netdevice(t->dev);
-               }
-       }
+       return err;
 }
 
 static void __exit ipgre_fini(void)
@@ -1390,10 +1396,6 @@ static void __exit ipgre_fini(void)
        if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
                printk(KERN_INFO "ipgre close: can't remove protocol\n");
 
-       rtnl_lock();
-       ipgre_destroy_tunnels();
-       rtnl_unlock();
-
        unregister_pernet_gen_device(ipgre_net_id, &ipgre_net_ops);
 }