Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / ipv4 / icmp.c
index 97d97ad..803bc9f 100644 (file)
@@ -188,29 +188,6 @@ struct icmp_err icmp_err_convert[] = {
        },
 };
 
-/* Control parameters for ECHO replies. */
-int sysctl_icmp_echo_ignore_all __read_mostly;
-int sysctl_icmp_echo_ignore_broadcasts __read_mostly = 1;
-
-/* Control parameter - ignore bogus broadcast responses? */
-int sysctl_icmp_ignore_bogus_error_responses __read_mostly = 1;
-
-/*
- *     Configurable global rate limit.
- *
- *     ratelimit defines tokens/packet consumed for dst->rate_token bucket
- *     ratemask defines which icmp types are ratelimited by setting
- *     it's bit position.
- *
- *     default:
- *     dest unreachable (3), source quench (4),
- *     time exceeded (11), parameter problem (12)
- */
-
-int sysctl_icmp_ratelimit __read_mostly = 1 * HZ;
-int sysctl_icmp_ratemask __read_mostly = 0x1818;
-int sysctl_icmp_errors_use_inbound_ifaddr __read_mostly;
-
 /*
  *     ICMP control array. This specifies what to do with each ICMP.
  */
@@ -229,8 +206,10 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  *
  *     On SMP we have one ICMP socket per-cpu.
  */
-static struct sock **__icmp_sk = NULL;
-#define icmp_sk                (__icmp_sk[smp_processor_id()])
+static struct sock *icmp_sk(struct net *net)
+{
+       return net->ipv4.icmp_sk[smp_processor_id()];
+}
 
 static inline int icmp_xmit_lock(struct sock *sk)
 {
@@ -291,7 +270,8 @@ int xrlim_allow(struct dst_entry *dst, int timeout)
        return rc;
 }
 
-static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
+static inline int icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
+               int type, int code)
 {
        struct dst_entry *dst = &rt->u.dst;
        int rc = 1;
@@ -308,8 +288,8 @@ static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
                goto out;
 
        /* Limit if icmp type is enabled in ratemask. */
-       if ((1 << type) & sysctl_icmp_ratemask)
-               rc = xrlim_allow(dst, sysctl_icmp_ratelimit);
+       if ((1 << type) & net->ipv4.sysctl_icmp_ratemask)
+               rc = xrlim_allow(dst, net->ipv4.sysctl_icmp_ratelimit);
 out:
        return rc;
 }
@@ -349,7 +329,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
        struct sock *sk;
        struct sk_buff *skb;
 
-       sk = icmp_sk;
+       sk = icmp_sk(dev_net(rt->u.dst.dev));
        if (ip_append_data(sk, icmp_glue_bits, icmp_param,
                           icmp_param->data_len+icmp_param->head_len,
                           icmp_param->head_len,
@@ -378,10 +358,11 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
 
 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
 {
-       struct sock *sk = icmp_sk;
-       struct inet_sock *inet = inet_sk(sk);
        struct ipcm_cookie ipc;
-       struct rtable *rt = (struct rtable *)skb->dst;
+       struct rtable *rt = skb->rtable;
+       struct net *net = dev_net(rt->u.dst.dev);
+       struct sock *sk = icmp_sk(net);
+       struct inet_sock *inet = inet_sk(sk);
        __be32 daddr;
 
        if (ip_options_echo(&icmp_param->replyopts, skb))
@@ -407,10 +388,10 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
                                                .tos = RT_TOS(ip_hdr(skb)->tos) } },
                                    .proto = IPPROTO_ICMP };
                security_skb_classify_flow(skb, &fl);
-               if (ip_route_output_key(rt->u.dst.dev->nd_net, &rt, &fl))
+               if (ip_route_output_key(net, &rt, &fl))
                        goto out_unlock;
        }
-       if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
+       if (icmpv4_xrlim_allow(net, rt, icmp_param->data.icmph.type,
                               icmp_param->data.icmph.code))
                icmp_push_reply(icmp_param, &ipc, rt);
        ip_rt_put(rt);
@@ -435,16 +416,17 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
        struct iphdr *iph;
        int room;
        struct icmp_bxm icmp_param;
-       struct rtable *rt = (struct rtable *)skb_in->dst;
+       struct rtable *rt = skb_in->rtable;
        struct ipcm_cookie ipc;
        __be32 saddr;
        u8  tos;
        struct net *net;
-       struct sock *sk = icmp_sk;
+       struct sock *sk;
 
        if (!rt)
                goto out;
-       net = rt->u.dst.dev->nd_net;
+       net = dev_net(rt->u.dst.dev);
+       sk = icmp_sk(net);
 
        /*
         *      Find the original header. It is expected to be valid, of course.
@@ -519,7 +501,8 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
        if (!(rt->rt_flags & RTCF_LOCAL)) {
                struct net_device *dev = NULL;
 
-               if (rt->fl.iif && sysctl_icmp_errors_use_inbound_ifaddr)
+               if (rt->fl.iif &&
+                       net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
                        dev = dev_get_by_index(net, rt->fl.iif);
 
                if (dev) {
@@ -594,7 +577,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
                }
 
                if (xfrm_decode_session_reverse(skb_in, &fl, AF_INET))
-                       goto out_unlock;
+                       goto ende;
 
                if (inet_addr_type(net, fl.fl4_src) == RTN_LOCAL)
                        err = __ip_route_output_key(net, &rt2, &fl);
@@ -604,7 +587,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
 
                        fl2.fl4_dst = fl.fl4_src;
                        if (ip_route_output_key(net, &rt2, &fl2))
-                               goto out_unlock;
+                               goto ende;
 
                        /* Ugh! */
                        odst = skb_in->dst;
@@ -612,12 +595,12 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
                                             RT_TOS(tos), rt2->u.dst.dev);
 
                        dst_release(&rt2->u.dst);
-                       rt2 = (struct rtable *)skb_in->dst;
+                       rt2 = skb_in->rtable;
                        skb_in->dst = odst;
                }
 
                if (err)
-                       goto out_unlock;
+                       goto ende;
 
                err = xfrm_lookup((struct dst_entry **)&rt2, &fl, NULL,
                                  XFRM_LOOKUP_ICMP);
@@ -635,7 +618,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
        }
 
 route_done:
-       if (!icmpv4_xrlim_allow(rt, type, code))
+       if (!icmpv4_xrlim_allow(net, rt, type, code))
                goto ende;
 
        /* RFC says return as much as we can without exceeding 576 bytes. */
@@ -673,7 +656,7 @@ static void icmp_unreach(struct sk_buff *skb)
        u32 info = 0;
        struct net *net;
 
-       net = skb->dst->dev->nd_net;
+       net = dev_net(skb->dst->dev);
 
        /*
         *      Incomplete header ?
@@ -741,7 +724,7 @@ static void icmp_unreach(struct sk_buff *skb)
         *      get the other vendor to fix their kit.
         */
 
-       if (!sysctl_icmp_ignore_bogus_error_responses &&
+       if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
            inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
                if (net_ratelimit())
                        printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP "
@@ -836,7 +819,10 @@ out_err:
 
 static void icmp_echo(struct sk_buff *skb)
 {
-       if (!sysctl_icmp_echo_ignore_all) {
+       struct net *net;
+
+       net = dev_net(skb->dst->dev);
+       if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
                struct icmp_bxm icmp_param;
 
                icmp_param.data.icmph      = *icmp_hdr(skb);
@@ -939,7 +925,7 @@ static void icmp_address(struct sk_buff *skb)
 
 static void icmp_address_reply(struct sk_buff *skb)
 {
-       struct rtable *rt = (struct rtable *)skb->dst;
+       struct rtable *rt = skb->rtable;
        struct net_device *dev = skb->dev;
        struct in_device *in_dev;
        struct in_ifaddr *ifa;
@@ -984,7 +970,7 @@ static void icmp_discard(struct sk_buff *skb)
 int icmp_rcv(struct sk_buff *skb)
 {
        struct icmphdr *icmph;
-       struct rtable *rt = (struct rtable *)skb->dst;
+       struct rtable *rt = skb->rtable;
 
        if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
                int nh;
@@ -1039,6 +1025,9 @@ int icmp_rcv(struct sk_buff *skb)
         */
 
        if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
+               struct net *net;
+
+               net = dev_net(rt->u.dst.dev);
                /*
                 *      RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
                 *        silently ignored (we let user decide with a sysctl).
@@ -1047,7 +1036,7 @@ int icmp_rcv(struct sk_buff *skb)
                 */
                if ((icmph->type == ICMP_ECHO ||
                     icmph->type == ICMP_TIMESTAMP) &&
-                   sysctl_icmp_echo_ignore_broadcasts) {
+                   net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
                        goto error;
                }
                if (icmph->type != ICMP_ECHO &&
@@ -1142,22 +1131,23 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
        },
 };
 
-static void __exit icmp_exit(void)
+static void __net_exit icmp_sk_exit(struct net *net)
 {
        int i;
 
        for_each_possible_cpu(i)
-               sk_release_kernel(__icmp_sk[i]);
-       kfree(__icmp_sk);
-       __icmp_sk = NULL;
+               sk_release_kernel(net->ipv4.icmp_sk[i]);
+       kfree(net->ipv4.icmp_sk);
+       net->ipv4.icmp_sk = NULL;
 }
 
-int __init icmp_init(void)
+int __net_init icmp_sk_init(struct net *net)
 {
        int i, err;
 
-       __icmp_sk = kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
-       if (__icmp_sk == NULL)
+       net->ipv4.icmp_sk =
+               kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
+       if (net->ipv4.icmp_sk == NULL)
                return -ENOMEM;
 
        for_each_possible_cpu(i) {
@@ -1169,8 +1159,8 @@ int __init icmp_init(void)
                if (err < 0)
                        goto fail;
 
-               __icmp_sk[i] = sk = sock->sk;
-               sk_change_net(sk, &init_net);
+               net->ipv4.icmp_sk[i] = sk = sock->sk;
+               sk_change_net(sk, net);
 
                sk->sk_allocation = GFP_ATOMIC;
 
@@ -1190,13 +1180,49 @@ int __init icmp_init(void)
                 */
                sk->sk_prot->unhash(sk);
        }
+
+       /* Control parameters for ECHO replies. */
+       net->ipv4.sysctl_icmp_echo_ignore_all = 0;
+       net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
+
+       /* Control parameter - ignore bogus broadcast responses? */
+       net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
+
+       /*
+        *      Configurable global rate limit.
+        *
+        *      ratelimit defines tokens/packet consumed for dst->rate_token
+        *      bucket ratemask defines which icmp types are ratelimited by
+        *      setting it's bit position.
+        *
+        *      default:
+        *      dest unreachable (3), source quench (4),
+        *      time exceeded (11), parameter problem (12)
+        */
+
+       net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
+       net->ipv4.sysctl_icmp_ratemask = 0x1818;
+       net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
+
        return 0;
 
 fail:
-       icmp_exit();
+       for_each_possible_cpu(i)
+               sk_release_kernel(net->ipv4.icmp_sk[i]);
+       kfree(net->ipv4.icmp_sk);
        return err;
 }
 
+static struct pernet_operations __net_initdata icmp_sk_ops = {
+       .init = icmp_sk_init,
+       .exit = icmp_sk_exit,
+};
+
+int __init icmp_init(void)
+{
+       return register_pernet_device(&icmp_sk_ops);
+}
+
 EXPORT_SYMBOL(icmp_err_convert);
 EXPORT_SYMBOL(icmp_send);
 EXPORT_SYMBOL(icmp_statistics);