]> err.no Git - linux-2.6/blobdiff - net/ipv6/ip6_tunnel.c
[IP6TUNNEL]: Add (ip6_tnl_)net argument to some calls.
[linux-2.6] / net / ipv6 / ip6_tunnel.c
index 5383b33db8ca606cb23edfed9cdfad018ef9a74d..fad1af8ae1cf0af79c4514f9b5cd82ff9e81a128 100644 (file)
@@ -52,6 +52,8 @@
 #include <net/xfrm.h>
 #include <net/dsfield.h>
 #include <net/inet_ecn.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
 
 MODULE_AUTHOR("Ville Nuorvala");
 MODULE_DESCRIPTION("IPv6 tunneling device");
@@ -60,7 +62,7 @@ MODULE_LICENSE("GPL");
 #define IPV6_TLV_TEL_DST_SIZE 8
 
 #ifdef IP6_TNL_DEBUG
-#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
+#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
 #else
 #define IP6_TNL_TRACE(x...) do {;} while(0)
 #endif
@@ -78,6 +80,10 @@ static int ip6_fb_tnl_dev_init(struct net_device *dev);
 static int ip6_tnl_dev_init(struct net_device *dev);
 static void ip6_tnl_dev_setup(struct net_device *dev);
 
+static int ip6_tnl_net_id;
+struct ip6_tnl_net {
+};
+
 /* the IPv6 tunnel fallback device */
 static struct net_device *ip6_fb_tnl_dev;
 
@@ -130,7 +136,7 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  **/
 
 static struct ip6_tnl *
-ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
+ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
 {
        unsigned h0 = HASH(remote);
        unsigned h1 = HASH(local);
@@ -160,7 +166,7 @@ ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
  **/
 
 static struct ip6_tnl **
-ip6_tnl_bucket(struct ip6_tnl_parm *p)
+ip6_tnl_bucket(struct ip6_tnl_net *ip6n, struct ip6_tnl_parm *p)
 {
        struct in6_addr *remote = &p->raddr;
        struct in6_addr *local = &p->laddr;
@@ -180,9 +186,9 @@ ip6_tnl_bucket(struct ip6_tnl_parm *p)
  **/
 
 static void
-ip6_tnl_link(struct ip6_tnl *t)
+ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
 {
-       struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
+       struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
 
        t->next = *tp;
        write_lock_bh(&ip6_tnl_lock);
@@ -196,11 +202,11 @@ ip6_tnl_link(struct ip6_tnl *t)
  **/
 
 static void
-ip6_tnl_unlink(struct ip6_tnl *t)
+ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
 {
        struct ip6_tnl **tp;
 
-       for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
+       for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
                if (t == *tp) {
                        write_lock_bh(&ip6_tnl_lock);
                        *tp = t->next;
@@ -222,40 +228,41 @@ ip6_tnl_unlink(struct ip6_tnl *t)
  *   created tunnel or NULL
  **/
 
-static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
+static struct ip6_tnl *ip6_tnl_create(struct net *net, struct ip6_tnl_parm *p)
 {
        struct net_device *dev;
        struct ip6_tnl *t;
        char name[IFNAMSIZ];
        int err;
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
 
-       if (p->name[0]) {
+       if (p->name[0])
                strlcpy(name, p->name, IFNAMSIZ);
-       } else {
-               int i;
-               for (i = 1; i < IP6_TNL_MAX; i++) {
-                       sprintf(name, "ip6tnl%d", i);
-                       if (__dev_get_by_name(&init_net, name) == NULL)
-                               break;
-               }
-               if (i == IP6_TNL_MAX)
-                       goto failed;
-       }
+       else
+               sprintf(name, "ip6tnl%%d");
+
        dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
        if (dev == NULL)
                goto failed;
 
+       if (strchr(name, '%')) {
+               if (dev_alloc_name(dev, name) < 0)
+                       goto failed_free;
+       }
+
        t = netdev_priv(dev);
        dev->init = ip6_tnl_dev_init;
        t->parms = *p;
 
-       if ((err = register_netdevice(dev)) < 0) {
-               free_netdev(dev);
-               goto failed;
-       }
+       if ((err = register_netdevice(dev)) < 0)
+               goto failed_free;
+
        dev_hold(dev);
-       ip6_tnl_link(t);
+       ip6_tnl_link(ip6n, t);
        return t;
+
+failed_free:
+       free_netdev(dev);
 failed:
        return NULL;
 }
@@ -274,20 +281,22 @@ failed:
  *   matching tunnel or NULL
  **/
 
-static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
+static struct ip6_tnl *ip6_tnl_locate(struct net *net,
+               struct ip6_tnl_parm *p, int create)
 {
        struct in6_addr *remote = &p->raddr;
        struct in6_addr *local = &p->laddr;
        struct ip6_tnl *t;
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
 
-       for (t = *ip6_tnl_bucket(p); t; t = t->next) {
+       for (t = *ip6_tnl_bucket(ip6n, p); t; t = t->next) {
                if (ipv6_addr_equal(local, &t->parms.laddr) &&
                    ipv6_addr_equal(remote, &t->parms.raddr))
                        return t;
        }
        if (!create)
                return NULL;
-       return ip6_tnl_create(p);
+       return ip6_tnl_create(net, p);
 }
 
 /**
@@ -302,13 +311,15 @@ static void
 ip6_tnl_dev_uninit(struct net_device *dev)
 {
        struct ip6_tnl *t = netdev_priv(dev);
+       struct net *net = dev_net(dev);
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
 
        if (dev == ip6_fb_tnl_dev) {
                write_lock_bh(&ip6_tnl_lock);
                tnls_wc[0] = NULL;
                write_unlock_bh(&ip6_tnl_lock);
        } else {
-               ip6_tnl_unlink(t);
+               ip6_tnl_unlink(ip6n, t);
        }
        ip6_tnl_dst_reset(t);
        dev_put(dev);
@@ -401,7 +412,8 @@ ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
           processing of the error. */
 
        read_lock(&ip6_tnl_lock);
-       if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
+       if ((t = ip6_tnl_lookup(&init_net, &ipv6h->daddr,
+                                       &ipv6h->saddr)) == NULL)
                goto out;
 
        if (t->parms.proto != ipproto && t->parms.proto != 0)
@@ -533,7 +545,7 @@ ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        fl.fl4_dst = eiph->saddr;
        fl.fl4_tos = RT_TOS(eiph->tos);
        fl.proto = IPPROTO_IPIP;
-       if (ip_route_output_key(&rt, &fl))
+       if (ip_route_output_key(&init_net, &rt, &fl))
                goto out;
 
        skb2->dev = rt->u.dst.dev;
@@ -545,11 +557,12 @@ ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                fl.fl4_dst = eiph->daddr;
                fl.fl4_src = eiph->saddr;
                fl.fl4_tos = eiph->tos;
-               if (ip_route_output_key(&rt, &fl) ||
+               if (ip_route_output_key(&init_net, &rt, &fl) ||
                    rt->u.dst.dev->type != ARPHRD_TUNNEL) {
                        ip_rt_put(rt);
                        goto out;
                }
+               skb2->dst = (struct dst_entry *)rt;
        } else {
                ip_rt_put(rt);
                if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
@@ -601,7 +614,7 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                skb_reset_network_header(skb2);
 
                /* Try to guess incoming interface */
-               rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
+               rt = rt6_lookup(&init_net, &ipv6_hdr(skb2)->saddr, NULL, 0, 0);
 
                if (rt && rt->rt6i_dev)
                        skb2->dev = rt->rt6i_dev;
@@ -635,7 +648,7 @@ static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
                                        struct sk_buff *skb)
 {
        if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
-               ipv6_copy_dscp(ipv6h, ipv6_hdr(skb));
+               ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
 
        if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
                IP6_ECN_set_ce(ipv6_hdr(skb));
@@ -653,8 +666,8 @@ static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
                        ldev = dev_get_by_index(&init_net, 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)))
+                    likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
+                   likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
                        ret = 1;
 
                if (ldev)
@@ -683,7 +696,8 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
 
        read_lock(&ip6_tnl_lock);
 
-       if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
+       if ((t = ip6_tnl_lookup(&init_net, &ipv6h->saddr,
+                                       &ipv6h->daddr)) != NULL) {
                if (t->parms.proto != ipproto && t->parms.proto != 0) {
                        read_unlock(&ip6_tnl_lock);
                        goto discard;
@@ -788,12 +802,12 @@ static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
                if (p->link)
                        ldev = dev_get_by_index(&init_net, p->link);
 
-               if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
+               if (unlikely(!ipv6_chk_addr(&init_net, &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)))
+                        unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
                        printk(KERN_WARNING
                               "%s xmit: Routing loop! "
                               "Remote address found on this node!\n",
@@ -846,7 +860,7 @@ static int ip6_tnl_xmit2(struct sk_buff *skb,
        if ((dst = ip6_tnl_dst_check(t)) != NULL)
                dst_hold(dst);
        else {
-               dst = ip6_route_output(NULL, fl);
+               dst = ip6_route_output(&init_net, NULL, fl);
 
                if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
                        goto tx_err_link_failure;
@@ -910,15 +924,13 @@ static int ip6_tnl_xmit2(struct sk_buff *skb,
        *(__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));
        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);
        nf_reset(skb);
        pkt_len = skb->len;
-       err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
-                     skb->dst->dev, dst_output);
+       err = ip6_local_out(skb);
 
        if (net_xmit_eval(err) == 0) {
                stats->tx_bytes += pkt_len;
@@ -1113,7 +1125,7 @@ static void ip6_tnl_link_config(struct ip6_tnl *t)
                int strict = (ipv6_addr_type(&p->raddr) &
                              (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
 
-               struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
+               struct rt6_info *rt = rt6_lookup(&init_net, &p->raddr, &p->laddr,
                                                 p->link, strict);
 
                if (rt == NULL)
@@ -1192,6 +1204,8 @@ ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
        int err = 0;
        struct ip6_tnl_parm p;
        struct ip6_tnl *t = NULL;
+       struct net *net = dev_net(dev);
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
 
        switch (cmd) {
        case SIOCGETTUNNEL:
@@ -1200,7 +1214,7 @@ ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                                err = -EFAULT;
                                break;
                        }
-                       t = ip6_tnl_locate(&p, 0);
+                       t = ip6_tnl_locate(net, &p, 0);
                }
                if (t == NULL)
                        t = netdev_priv(dev);
@@ -1221,7 +1235,7 @@ ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
                    p.proto != 0)
                        break;
-               t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
+               t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
                if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
                        if (t != NULL) {
                                if (t->dev != dev) {
@@ -1231,9 +1245,9 @@ ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                        } else
                                t = netdev_priv(dev);
 
-                       ip6_tnl_unlink(t);
+                       ip6_tnl_unlink(ip6n, t);
                        err = ip6_tnl_change(t, &p);
-                       ip6_tnl_link(t);
+                       ip6_tnl_link(ip6n, t);
                        netdev_state_change(dev);
                }
                if (t) {
@@ -1254,7 +1268,7 @@ ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
                                break;
                        err = -ENOENT;
-                       if ((t = ip6_tnl_locate(&p, 0)) == NULL)
+                       if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
                                break;
                        err = -EPERM;
                        if (t->dev == ip6_fb_tnl_dev)
@@ -1385,6 +1399,41 @@ static struct xfrm6_tunnel ip6ip6_handler = {
        .priority       =       1,
 };
 
+static int ip6_tnl_init_net(struct net *net)
+{
+       int err;
+       struct ip6_tnl_net *ip6n;
+
+       err = -ENOMEM;
+       ip6n = kmalloc(sizeof(struct ip6_tnl_net), GFP_KERNEL);
+       if (ip6n == NULL)
+               goto err_alloc;
+
+       err = net_assign_generic(net, ip6_tnl_net_id, ip6n);
+       if (err < 0)
+               goto err_assign;
+
+       return 0;
+
+err_assign:
+       kfree(ip6n);
+err_alloc:
+       return err;
+}
+
+static void ip6_tnl_exit_net(struct net *net)
+{
+       struct ip6_tnl_net *ip6n;
+
+       ip6n = net_generic(net, ip6_tnl_net_id);
+       kfree(ip6n);
+}
+
+static struct pernet_operations ip6_tnl_net_ops = {
+       .init = ip6_tnl_init_net,
+       .exit = ip6_tnl_exit_net,
+};
+
 /**
  * ip6_tunnel_init - register protocol and reserve needed resources
  *
@@ -1419,7 +1468,13 @@ static int __init ip6_tunnel_init(void)
                free_netdev(ip6_fb_tnl_dev);
                goto fail;
        }
+
+       err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
+       if (err < 0)
+               goto err_pernet;
        return 0;
+err_pernet:
+       unregister_netdevice(ip6_fb_tnl_dev);
 fail:
        xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
 unreg_ip4ip6:
@@ -1457,6 +1512,8 @@ static void __exit ip6_tunnel_cleanup(void)
        rtnl_lock();
        ip6_tnl_destroy_tunnels();
        rtnl_unlock();
+
+       unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
 }
 
 module_init(ip6_tunnel_init);