]> err.no Git - linux-2.6/blobdiff - net/ipv4/icmp.c
[ICMP]: Allocate data for __icmp(v6)_sk dynamically.
[linux-2.6] / net / ipv4 / icmp.c
index 98372db66c6698f498f6197e78a748120853a4c7..7c62a0da5264f447577fd8dff45bb6715bcd09de 100644 (file)
@@ -229,14 +229,14 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  *
  *     On SMP we have one ICMP socket per-cpu.
  */
-static DEFINE_PER_CPU(struct socket *, __icmp_socket) = NULL;
-#define icmp_socket    __get_cpu_var(__icmp_socket)
+static struct sock **__icmp_sk = NULL;
+#define icmp_sk                (__icmp_sk[smp_processor_id()])
 
-static inline int icmp_xmit_lock(void)
+static inline int icmp_xmit_lock(struct sock *sk)
 {
        local_bh_disable();
 
-       if (unlikely(!spin_trylock(&icmp_socket->sk->sk_lock.slock))) {
+       if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
                /* This can happen if the output path signals a
                 * dst_link_failure() for an outgoing ICMP packet.
                 */
@@ -246,9 +246,9 @@ static inline int icmp_xmit_lock(void)
        return 0;
 }
 
-static inline void icmp_xmit_unlock(void)
+static inline void icmp_xmit_unlock(struct sock *sk)
 {
-       spin_unlock_bh(&icmp_socket->sk->sk_lock.slock);
+       spin_unlock_bh(&sk->sk_lock.slock);
 }
 
 /*
@@ -346,19 +346,21 @@ static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
 static void icmp_push_reply(struct icmp_bxm *icmp_param,
                            struct ipcm_cookie *ipc, struct rtable *rt)
 {
+       struct sock *sk;
        struct sk_buff *skb;
 
-       if (ip_append_data(icmp_socket->sk, icmp_glue_bits, icmp_param,
+       sk = icmp_sk;
+       if (ip_append_data(sk, icmp_glue_bits, icmp_param,
                           icmp_param->data_len+icmp_param->head_len,
                           icmp_param->head_len,
                           ipc, rt, MSG_DONTWAIT) < 0)
-               ip_flush_pending_frames(icmp_socket->sk);
-       else if ((skb = skb_peek(&icmp_socket->sk->sk_write_queue)) != NULL) {
+               ip_flush_pending_frames(sk);
+       else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
                struct icmphdr *icmph = icmp_hdr(skb);
                __wsum csum = 0;
                struct sk_buff *skb1;
 
-               skb_queue_walk(&icmp_socket->sk->sk_write_queue, skb1) {
+               skb_queue_walk(&sk->sk_write_queue, skb1) {
                        csum = csum_add(csum, skb1->csum);
                }
                csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
@@ -366,7 +368,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
                                                 icmp_param->head_len, csum);
                icmph->checksum = csum_fold(csum);
                skb->ip_summed = CHECKSUM_NONE;
-               ip_push_pending_frames(icmp_socket->sk);
+               ip_push_pending_frames(sk);
        }
 }
 
@@ -376,7 +378,7 @@ 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_socket->sk;
+       struct sock *sk = icmp_sk;
        struct inet_sock *inet = inet_sk(sk);
        struct ipcm_cookie ipc;
        struct rtable *rt = (struct rtable *)skb->dst;
@@ -385,7 +387,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
        if (ip_options_echo(&icmp_param->replyopts, skb))
                return;
 
-       if (icmp_xmit_lock())
+       if (icmp_xmit_lock(sk))
                return;
 
        icmp_param->data.icmph.checksum = 0;
@@ -413,7 +415,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
                icmp_push_reply(icmp_param, &ipc, rt);
        ip_rt_put(rt);
 out_unlock:
-       icmp_xmit_unlock();
+       icmp_xmit_unlock(sk);
 }
 
 
@@ -438,6 +440,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
        __be32 saddr;
        u8  tos;
        struct net *net;
+       struct sock *sk = icmp_sk;
 
        if (!rt)
                goto out;
@@ -505,7 +508,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
                }
        }
 
-       if (icmp_xmit_lock())
+       if (icmp_xmit_lock(sk))
                return;
 
        /*
@@ -544,7 +547,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
        icmp_param.data.icmph.checksum   = 0;
        icmp_param.skb    = skb_in;
        icmp_param.offset = skb_network_offset(skb_in);
-       inet_sk(icmp_socket->sk)->tos = tos;
+       inet_sk(sk)->tos = tos;
        ipc.addr = iph->saddr;
        ipc.opt = &icmp_param.replyopts;
 
@@ -652,7 +655,7 @@ route_done:
 ende:
        ip_rt_put(rt);
 out_unlock:
-       icmp_xmit_unlock();
+       icmp_xmit_unlock(sk);
 out:;
 }
 
@@ -1139,29 +1142,49 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
        },
 };
 
-void __init icmp_init(void)
+static void __exit icmp_exit(void)
 {
-       struct inet_sock *inet;
        int i;
 
        for_each_possible_cpu(i) {
-               int err;
+               struct sock *sk;
+
+               sk = __icmp_sk[i];
+               if (sk == NULL)
+                       continue;
+               sock_release(sk->sk_socket);
+       }
+       kfree(__icmp_sk);
+       __icmp_sk = NULL;
+}
+
+int __init icmp_init(void)
+{
+       int i, err;
+
+       __icmp_sk = kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
+       if (__icmp_sk == NULL)
+               return -ENOMEM;
 
-               err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP,
-                                      &per_cpu(__icmp_socket, i));
+       for_each_possible_cpu(i) {
+               struct sock *sk;
+               struct socket *sock;
+               struct inet_sock *inet;
 
+               err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP, &sock);
                if (err < 0)
-                       panic("Failed to create the ICMP control socket.\n");
+                       goto fail;
 
-               per_cpu(__icmp_socket, i)->sk->sk_allocation = GFP_ATOMIC;
+               __icmp_sk[i] = sk = sock->sk;
+               sk->sk_allocation = GFP_ATOMIC;
 
                /* Enough space for 2 64K ICMP packets, including
                 * sk_buff struct overhead.
                 */
-               per_cpu(__icmp_socket, i)->sk->sk_sndbuf =
+               sk->sk_sndbuf =
                        (2 * ((64 * 1024) + sizeof(struct sk_buff)));
 
-               inet = inet_sk(per_cpu(__icmp_socket, i)->sk);
+               inet = inet_sk(sk);
                inet->uc_ttl = -1;
                inet->pmtudisc = IP_PMTUDISC_DONT;
 
@@ -1169,8 +1192,13 @@ void __init icmp_init(void)
                 * see it, we do not wish this socket to see incoming
                 * packets.
                 */
-               per_cpu(__icmp_socket, i)->sk->sk_prot->unhash(per_cpu(__icmp_socket, i)->sk);
+               sk->sk_prot->unhash(sk);
        }
+       return 0;
+
+fail:
+       icmp_exit();
+       return err;
 }
 
 EXPORT_SYMBOL(icmp_err_convert);