net: Introduce skb_tunnel_rx() helper
[safe/jmp/linux-2.6] / net / ipv6 / ip6_tunnel.c
index 6c1b5c9..8f39893 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/route.h>
 #include <linux/rtnetlink.h>
 #include <linux/netfilter_ipv6.h>
+#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/atomic.h>
@@ -74,11 +75,10 @@ MODULE_LICENSE("GPL");
                     (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
                    (HASH_SIZE - 1))
 
-static void ip6_fb_tnl_dev_init(struct net_device *dev);
 static void ip6_tnl_dev_init(struct net_device *dev);
 static void ip6_tnl_dev_setup(struct net_device *dev);
 
-static int ip6_tnl_net_id;
+static int ip6_tnl_net_id __read_mostly;
 struct ip6_tnl_net {
        /* the IPv6 tunnel fallback device */
        struct net_device *fb_tnl_dev;
@@ -623,7 +623,7 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                if (rt && rt->rt6i_dev)
                        skb2->dev = rt->rt6i_dev;
 
-               icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
+               icmpv6_send(skb2, rel_type, rel_code, rel_info);
 
                if (rt)
                        dst_release(&rt->u.dst);
@@ -658,6 +658,7 @@ static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
                IP6_ECN_set_ce(ipv6_hdr(skb));
 }
 
+/* called with rcu_read_lock() */
 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
 {
        struct ip6_tnl_parm *p = &t->parms;
@@ -668,15 +669,13 @@ static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
                struct net_device *ldev = NULL;
 
                if (p->link)
-                       ldev = dev_get_by_index(net, p->link);
+                       ldev = dev_get_by_index_rcu(net, p->link);
 
                if ((ipv6_addr_is_multicast(&p->laddr) ||
                     likely(ipv6_chk_addr(net, &p->laddr, ldev, 0))) &&
                    likely(!ipv6_chk_addr(net, &p->raddr, NULL, 0)))
                        ret = 1;
 
-               if (ldev)
-                       dev_put(ldev);
        }
        return ret;
 }
@@ -724,14 +723,10 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
                skb->protocol = htons(protocol);
                skb->pkt_type = PACKET_HOST;
                memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
-               skb->dev = t->dev;
-               skb_dst_drop(skb);
-               nf_reset(skb);
 
-               dscp_ecn_decapsulate(t, ipv6h, skb);
+               skb_tunnel_rx(skb, t->dev);
 
-               t->dev->stats.rx_packets++;
-               t->dev->stats.rx_bytes += skb->len;
+               dscp_ecn_decapsulate(t, ipv6h, skb);
                netif_rx(skb);
                rcu_read_unlock();
                return 0;
@@ -804,8 +799,9 @@ static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
        if (p->flags & IP6_TNL_F_CAP_XMIT) {
                struct net_device *ldev = NULL;
 
+               rcu_read_lock();
                if (p->link)
-                       ldev = dev_get_by_index(net, p->link);
+                       ldev = dev_get_by_index_rcu(net, p->link);
 
                if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
                        printk(KERN_WARNING
@@ -819,8 +815,7 @@ static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
                               p->name);
                else
                        ret = 1;
-               if (ldev)
-                       dev_put(ldev);
+               rcu_read_unlock();
        }
        return ret;
 }
@@ -1016,7 +1011,7 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
                tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
                if (tel->encap_limit == 0) {
                        icmpv6_send(skb, ICMPV6_PARAMPROB,
-                                   ICMPV6_HDR_FIELD, offset + 2, skb->dev);
+                                   ICMPV6_HDR_FIELD, offset + 2);
                        return -1;
                }
                encap_limit = tel->encap_limit - 1;
@@ -1035,7 +1030,7 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
        err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
        if (err != 0) {
                if (err == -EMSGSIZE)
-                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
+                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
                return -1;
        }
 
@@ -1365,7 +1360,7 @@ static void ip6_tnl_dev_init(struct net_device *dev)
  * Return: 0
  **/
 
-static void ip6_fb_tnl_dev_init(struct net_device *dev)
+static void __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
 {
        struct ip6_tnl *t = netdev_priv(dev);
        struct net *net = dev_net(dev);
@@ -1389,7 +1384,7 @@ static struct xfrm6_tunnel ip6ip6_handler = {
        .priority       =       1,
 };
 
-static void ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
+static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
 {
        int h;
        struct ip6_tnl *t;
@@ -1408,19 +1403,10 @@ static void ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
        unregister_netdevice_many(&list);
 }
 
-static int ip6_tnl_init_net(struct net *net)
+static int __net_init ip6_tnl_init_net(struct net *net)
 {
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
        int err;
-       struct ip6_tnl_net *ip6n;
-
-       err = -ENOMEM;
-       ip6n = kzalloc(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;
 
        ip6n->tnls[0] = ip6n->tnls_wc;
        ip6n->tnls[1] = ip6n->tnls_r_l;
@@ -1443,27 +1429,23 @@ static int ip6_tnl_init_net(struct net *net)
 err_register:
        free_netdev(ip6n->fb_tnl_dev);
 err_alloc_dev:
-       /* nothing */
-err_assign:
-       kfree(ip6n);
-err_alloc:
        return err;
 }
 
-static void ip6_tnl_exit_net(struct net *net)
+static void __net_exit ip6_tnl_exit_net(struct net *net)
 {
-       struct ip6_tnl_net *ip6n;
+       struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
 
-       ip6n = net_generic(net, ip6_tnl_net_id);
        rtnl_lock();
        ip6_tnl_destroy_tunnels(ip6n);
        rtnl_unlock();
-       kfree(ip6n);
 }
 
 static struct pernet_operations ip6_tnl_net_ops = {
        .init = ip6_tnl_init_net,
        .exit = ip6_tnl_exit_net,
+       .id   = &ip6_tnl_net_id,
+       .size = sizeof(struct ip6_tnl_net),
 };
 
 /**
@@ -1476,27 +1458,29 @@ static int __init ip6_tunnel_init(void)
 {
        int  err;
 
-       if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
+       err = register_pernet_device(&ip6_tnl_net_ops);
+       if (err < 0)
+               goto out_pernet;
+
+       err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
+       if (err < 0) {
                printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
-               err = -EAGAIN;
-               goto out;
+               goto out_ip4ip6;
        }
 
-       if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
+       err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
+       if (err < 0) {
                printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
-               err = -EAGAIN;
-               goto unreg_ip4ip6;
+               goto out_ip6ip6;
        }
 
-       err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
-       if (err < 0)
-               goto err_pernet;
        return 0;
-err_pernet:
-       xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
-unreg_ip4ip6:
+
+out_ip6ip6:
        xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
-out:
+out_ip4ip6:
+       unregister_pernet_device(&ip6_tnl_net_ops);
+out_pernet:
        return err;
 }
 
@@ -1512,7 +1496,7 @@ static void __exit ip6_tunnel_cleanup(void)
        if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
                printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
 
-       unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
+       unregister_pernet_device(&ip6_tnl_net_ops);
 }
 
 module_init(ip6_tunnel_init);