[IPV6] SYSCTL: complete initialization for sysctl table in subsystem code.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / icmp.c
index 9bb031fa1c2f538c827402e3ecc8f90774fd6a55..12c0b85d6c46142b2d60ab97309623cc9a2de0f9 100644 (file)
@@ -63,6 +63,7 @@
 #include <net/ip6_route.h>
 #include <net/addrconf.h>
 #include <net/icmp.h>
+#include <net/xfrm.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -79,21 +80,23 @@ EXPORT_SYMBOL(icmpv6msg_statistics);
  *
  *     On SMP we have one ICMP socket per-cpu.
  */
-static DEFINE_PER_CPU(struct socket *, __icmpv6_socket) = NULL;
-#define icmpv6_socket  __get_cpu_var(__icmpv6_socket)
+static inline struct sock *icmpv6_sk(struct net *net)
+{
+       return net->ipv6.icmp_sk[smp_processor_id()];
+}
 
 static int icmpv6_rcv(struct sk_buff *skb);
 
 static struct inet6_protocol icmpv6_protocol = {
        .handler        =       icmpv6_rcv,
-       .flags          =       INET6_PROTO_FINAL,
+       .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
 };
 
-static __inline__ int icmpv6_xmit_lock(void)
+static __inline__ int icmpv6_xmit_lock(struct sock *sk)
 {
        local_bh_disable();
 
-       if (unlikely(!spin_trylock(&icmpv6_socket->sk->sk_lock.slock))) {
+       if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
                /* This can happen if the output path (f.e. SIT or
                 * ip6ip6 tunnel) signals dst_link_failure() for an
                 * outgoing ICMP6 packet.
@@ -104,9 +107,9 @@ static __inline__ int icmpv6_xmit_lock(void)
        return 0;
 }
 
-static __inline__ void icmpv6_xmit_unlock(void)
+static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
 {
-       spin_unlock_bh(&icmpv6_socket->sk->sk_lock.slock);
+       spin_unlock_bh(&sk->sk_lock.slock);
 }
 
 /*
@@ -153,8 +156,6 @@ static int is_ineligible(struct sk_buff *skb)
        return 0;
 }
 
-static int sysctl_icmpv6_time __read_mostly = 1*HZ;
-
 /*
  * Check the ICMP output rate limit
  */
@@ -185,7 +186,7 @@ static inline int icmpv6_xrlim_allow(struct sock *sk, int type,
                res = 1;
        } else {
                struct rt6_info *rt = (struct rt6_info *)dst;
-               int tmo = sysctl_icmpv6_time;
+               int tmo = init_net.ipv6.sysctl.icmpv6_time;
 
                /* Give more bandwidth to wider prefixes. */
                if (rt->rt6i_dst.plen < 128)
@@ -310,8 +311,10 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
        struct ipv6_pinfo *np;
        struct in6_addr *saddr = NULL;
        struct dst_entry *dst;
+       struct dst_entry *dst2;
        struct icmp6hdr tmp_hdr;
        struct flowi fl;
+       struct flowi fl2;
        struct icmpv6_msg msg;
        int iif = 0;
        int addr_type = 0;
@@ -331,7 +334,7 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
         */
        addr_type = ipv6_addr_type(&hdr->daddr);
 
-       if (ipv6_chk_addr(&hdr->daddr, skb->dev, 0))
+       if (ipv6_chk_addr(&init_net, &hdr->daddr, skb->dev, 0))
                saddr = &hdr->daddr;
 
        /*
@@ -388,12 +391,12 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
        fl.fl_icmp_code = code;
        security_skb_classify_flow(skb, &fl);
 
-       if (icmpv6_xmit_lock())
-               return;
-
-       sk = icmpv6_socket->sk;
+       sk = icmpv6_sk(&init_net);
        np = inet6_sk(sk);
 
+       if (icmpv6_xmit_lock(sk))
+               return;
+
        if (!icmpv6_xrlim_allow(sk, type, &fl))
                goto out;
 
@@ -418,9 +421,42 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
                goto out_dst_release;
        }
 
-       if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
+       /* No need to clone since we're just using its address. */
+       dst2 = dst;
+
+       err = xfrm_lookup(&dst, &fl, sk, 0);
+       switch (err) {
+       case 0:
+               if (dst != dst2)
+                       goto route_done;
+               break;
+       case -EPERM:
+               dst = NULL;
+               break;
+       default:
+               goto out;
+       }
+
+       if (xfrm_decode_session_reverse(skb, &fl2, AF_INET6))
+               goto out;
+
+       if (ip6_dst_lookup(sk, &dst2, &fl))
+               goto out;
+
+       err = xfrm_lookup(&dst2, &fl, sk, XFRM_LOOKUP_ICMP);
+       if (err == -ENOENT) {
+               if (!dst)
+                       goto out;
+               goto route_done;
+       }
+
+       dst_release(dst);
+       dst = dst2;
+
+       if (err)
                goto out;
 
+route_done:
        if (ipv6_addr_is_multicast(&fl.fl6_dst))
                hlimit = np->mcast_hops;
        else
@@ -458,15 +494,13 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
        }
        err = icmpv6_push_pending_frames(sk, &fl, &tmp_hdr, len + sizeof(struct icmp6hdr));
 
-       ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
-
 out_put:
        if (likely(idev != NULL))
                in6_dev_put(idev);
 out_dst_release:
        dst_release(dst);
 out:
-       icmpv6_xmit_unlock();
+       icmpv6_xmit_unlock(sk);
 }
 
 EXPORT_SYMBOL(icmpv6_send);
@@ -503,12 +537,12 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
        fl.fl_icmp_type = ICMPV6_ECHO_REPLY;
        security_skb_classify_flow(skb, &fl);
 
-       if (icmpv6_xmit_lock())
-               return;
-
-       sk = icmpv6_socket->sk;
+       sk = icmpv6_sk(&init_net);
        np = inet6_sk(sk);
 
+       if (icmpv6_xmit_lock(sk))
+               return;
+
        if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
                fl.oif = np->mcast_oif;
 
@@ -552,14 +586,12 @@ out_put:
                in6_dev_put(idev);
        dst_release(dst);
 out:
-       icmpv6_xmit_unlock();
+       icmpv6_xmit_unlock(sk);
 }
 
 static void icmpv6_notify(struct sk_buff *skb, int type, int code, __be32 info)
 {
-       struct in6_addr *saddr, *daddr;
        struct inet6_protocol *ipprot;
-       struct sock *sk;
        int inner_offset;
        int hash;
        u8 nexthdr;
@@ -581,9 +613,6 @@ static void icmpv6_notify(struct sk_buff *skb, int type, int code, __be32 info)
        if (!pskb_may_pull(skb, inner_offset+8))
                return;
 
-       saddr = &ipv6_hdr(skb)->saddr;
-       daddr = &ipv6_hdr(skb)->daddr;
-
        /* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
           Without this we will not able f.e. to make source routed
           pmtu discovery.
@@ -599,15 +628,7 @@ static void icmpv6_notify(struct sk_buff *skb, int type, int code, __be32 info)
                ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
        rcu_read_unlock();
 
-       read_lock(&raw_v6_lock);
-       if ((sk = sk_head(&raw_v6_htable[hash])) != NULL) {
-               while ((sk = __raw_v6_lookup(sk, nexthdr, saddr, daddr,
-                                           IP6CB(skb)->iif))) {
-                       rawv6_err(sk, skb, NULL, type, code, inner_offset, info);
-                       sk = sk_next(sk);
-               }
-       }
-       read_unlock(&raw_v6_lock);
+       raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
 }
 
 /*
@@ -623,6 +644,25 @@ static int icmpv6_rcv(struct sk_buff *skb)
        struct icmp6hdr *hdr;
        int type;
 
+       if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
+               int nh;
+
+               if (!(skb->sp && skb->sp->xvec[skb->sp->len - 1]->props.flags &
+                                XFRM_STATE_ICMP))
+                       goto drop_no_count;
+
+               if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(*orig_hdr)))
+                       goto drop_no_count;
+
+               nh = skb_network_offset(skb);
+               skb_set_network_header(skb, sizeof(*hdr));
+
+               if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
+                       goto drop_no_count;
+
+               skb_set_network_header(skb, nh);
+       }
+
        ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INMSGS);
 
        saddr = &ipv6_hdr(skb)->saddr;
@@ -645,7 +685,7 @@ static int icmpv6_rcv(struct sk_buff *skb)
                }
        }
 
-       if (!pskb_pull(skb, sizeof(struct icmp6hdr)))
+       if (!pskb_pull(skb, sizeof(*hdr)))
                goto discard_it;
 
        hdr = icmp6_hdr(skb);
@@ -732,23 +772,30 @@ static int icmpv6_rcv(struct sk_buff *skb)
 
 discard_it:
        ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
+drop_no_count:
        kfree_skb(skb);
        return 0;
 }
 
 /*
- * Special lock-class for __icmpv6_socket:
+ * Special lock-class for __icmpv6_sk:
  */
 static struct lock_class_key icmpv6_socket_sk_dst_lock_key;
 
-int __init icmpv6_init(struct net_proto_family *ops)
+static int __net_init icmpv6_sk_init(struct net *net)
 {
        struct sock *sk;
        int err, i, j;
 
+       net->ipv6.icmp_sk =
+               kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
+       if (net->ipv6.icmp_sk == NULL)
+               return -ENOMEM;
+
        for_each_possible_cpu(i) {
+               struct socket *sock;
                err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6,
-                                      &per_cpu(__icmpv6_socket, i));
+                                      &sock);
                if (err < 0) {
                        printk(KERN_ERR
                               "Failed to initialize the ICMP6 control socket "
@@ -757,12 +804,14 @@ int __init icmpv6_init(struct net_proto_family *ops)
                        goto fail;
                }
 
-               sk = per_cpu(__icmpv6_socket, i)->sk;
+               net->ipv6.icmp_sk[i] = sk = sock->sk;
+               sk_change_net(sk, net);
+
                sk->sk_allocation = GFP_ATOMIC;
                /*
                 * Split off their lock-class, because sk->sk_dst_lock
                 * gets used from softirqs, which is safe for
-                * __icmpv6_socket (because those never get directly used
+                * __icmpv6_sk (because those never get directly used
                 * via userspace syscalls), but unsafe for normal sockets.
                 */
                lockdep_set_class(&sk->sk_dst_lock,
@@ -776,36 +825,56 @@ int __init icmpv6_init(struct net_proto_family *ops)
 
                sk->sk_prot->unhash(sk);
        }
-
-
-       if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0) {
-               printk(KERN_ERR "Failed to register ICMP6 protocol\n");
-               err = -EAGAIN;
-               goto fail;
-       }
-
        return 0;
 
  fail:
-       for (j = 0; j < i; j++) {
-               if (!cpu_possible(j))
-                       continue;
-               sock_release(per_cpu(__icmpv6_socket, j));
-       }
-
+       for (j = 0; j < i; j++)
+               sk_release_kernel(net->ipv6.icmp_sk[j]);
+       kfree(net->ipv6.icmp_sk);
        return err;
 }
 
-void icmpv6_cleanup(void)
+static void __net_exit icmpv6_sk_exit(struct net *net)
 {
        int i;
 
        for_each_possible_cpu(i) {
-               sock_release(per_cpu(__icmpv6_socket, i));
+               sk_release_kernel(net->ipv6.icmp_sk[i]);
        }
+       kfree(net->ipv6.icmp_sk);
+}
+
+static struct pernet_operations icmpv6_sk_ops = {
+       .init = icmpv6_sk_init,
+       .exit = icmpv6_sk_exit,
+};
+
+int __init icmpv6_init(void)
+{
+       int err;
+
+       err = register_pernet_subsys(&icmpv6_sk_ops);
+       if (err < 0)
+               return err;
+
+       err = -EAGAIN;
+       if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
+               goto fail;
+       return 0;
+
+fail:
+       printk(KERN_ERR "Failed to register ICMP6 protocol\n");
+       unregister_pernet_subsys(&icmpv6_sk_ops);
+       return err;
+}
+
+void icmpv6_cleanup(void)
+{
+       unregister_pernet_subsys(&icmpv6_sk_ops);
        inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
 }
 
+
 static const struct icmp6_err {
        int err;
        int fatal;
@@ -867,16 +936,30 @@ int icmpv6_err_convert(int type, int code, int *err)
 EXPORT_SYMBOL(icmpv6_err_convert);
 
 #ifdef CONFIG_SYSCTL
-ctl_table ipv6_icmp_table[] = {
+ctl_table ipv6_icmp_table_template[] = {
        {
                .ctl_name       = NET_IPV6_ICMP_RATELIMIT,
                .procname       = "ratelimit",
-               .data           = &sysctl_icmpv6_time,
+               .data           = &init_net.ipv6.sysctl.icmpv6_time,
                .maxlen         = sizeof(int),
                .mode           = 0644,
                .proc_handler   = &proc_dointvec
        },
        { .ctl_name = 0 },
 };
+
+struct ctl_table *ipv6_icmp_sysctl_init(struct net *net)
+{
+       struct ctl_table *table;
+
+       table = kmemdup(ipv6_icmp_table_template,
+                       sizeof(ipv6_icmp_table_template),
+                       GFP_KERNEL);
+
+       if (table)
+               table[0].data = &net->ipv6.sysctl.icmpv6_time;
+
+       return table;
+}
 #endif