New maintainers for the x86 (32-bit and 64-bit) architecture
[safe/jmp/linux-2.6] / net / netlink / af_netlink.c
index 3ef3282..98e313e 100644 (file)
@@ -80,7 +80,7 @@ struct netlink_sock {
        struct netlink_callback *cb;
        struct mutex            *cb_mutex;
        struct mutex            cb_def_mutex;
-       void                    (*data_ready)(struct sock *sk, int bytes);
+       void                    (*netlink_rcv)(struct sk_buff *skb);
        struct module           *module;
 };
 
@@ -92,6 +92,11 @@ static inline struct netlink_sock *nlk_sk(struct sock *sk)
        return container_of(sk, struct netlink_sock, sk);
 }
 
+static inline int netlink_is_kernel(struct sock *sk)
+{
+       return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
+}
+
 struct nl_pid_hash {
        struct hlist_head *table;
        unsigned long rehash_time;
@@ -122,7 +127,6 @@ static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
 
 static int netlink_dump(struct sock *sk);
 static void netlink_destroy_callback(struct netlink_callback *cb);
-static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb);
 
 static DEFINE_RWLOCK(nl_table_lock);
 static atomic_t nl_table_users = ATOMIC_INIT(0);
@@ -489,7 +493,7 @@ static int netlink_release(struct socket *sock)
        module_put(nlk->module);
 
        netlink_table_grab();
-       if (nlk->flags & NETLINK_KERNEL_SOCKET) {
+       if (netlink_is_kernel(sk)) {
                kfree(nl_table[sk->sk_protocol].listeners);
                nl_table[sk->sk_protocol].module = NULL;
                nl_table[sk->sk_protocol].registered = 0;
@@ -704,21 +708,17 @@ static void netlink_overrun(struct sock *sk)
 
 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
 {
-       int protocol = ssk->sk_protocol;
-       struct net *net;
        struct sock *sock;
        struct netlink_sock *nlk;
 
-       net = ssk->sk_net;
-       sock = netlink_lookup(net, protocol, pid);
+       sock = netlink_lookup(ssk->sk_net, ssk->sk_protocol, pid);
        if (!sock)
                return ERR_PTR(-ECONNREFUSED);
 
        /* Don't bother queuing skb if kernel socket has no input function */
        nlk = nlk_sk(sock);
-       if ((nlk->pid == 0 && !nlk->data_ready) ||
-           (sock->sk_state == NETLINK_CONNECTED &&
-            nlk->dst_pid != nlk_sk(ssk)->pid)) {
+       if (sock->sk_state == NETLINK_CONNECTED &&
+           nlk->dst_pid != nlk_sk(ssk)->pid) {
                sock_put(sock);
                return ERR_PTR(-ECONNREFUSED);
        }
@@ -762,7 +762,7 @@ int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
            test_bit(0, &nlk->state)) {
                DECLARE_WAITQUEUE(wait, current);
                if (!timeo) {
-                       if (!ssk || nlk_sk(ssk)->pid == 0)
+                       if (!ssk || netlink_is_kernel(ssk))
                                netlink_overrun(sk);
                        sock_put(sk);
                        kfree_skb(skb);
@@ -791,7 +791,7 @@ int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
        return 0;
 }
 
-int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
+int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
 {
        int len = skb->len;
 
@@ -832,7 +832,34 @@ static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
        return skb;
 }
 
-int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
+static inline void netlink_rcv_wake(struct sock *sk)
+{
+       struct netlink_sock *nlk = nlk_sk(sk);
+
+       if (skb_queue_empty(&sk->sk_receive_queue))
+               clear_bit(0, &nlk->state);
+       if (!test_bit(0, &nlk->state))
+               wake_up_interruptible(&nlk->wait);
+}
+
+static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
+{
+       int ret;
+       struct netlink_sock *nlk = nlk_sk(sk);
+
+       ret = -ECONNREFUSED;
+       if (nlk->netlink_rcv != NULL) {
+               ret = skb->len;
+               skb_set_owner_r(skb, sk);
+               nlk->netlink_rcv(skb);
+       }
+       kfree_skb(skb);
+       sock_put(sk);
+       return ret;
+}
+
+int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
+                   u32 pid, int nonblock)
 {
        struct sock *sk;
        int err;
@@ -847,13 +874,16 @@ retry:
                kfree_skb(skb);
                return PTR_ERR(sk);
        }
+       if (netlink_is_kernel(sk))
+               return netlink_unicast_kernel(sk, skb);
+
        err = netlink_attachskb(sk, skb, nonblock, timeo, ssk);
        if (err == 1)
                goto retry;
        if (err)
                return err;
 
-       return netlink_sendskb(sk, skb, ssk->sk_protocol);
+       return netlink_sendskb(sk, skb);
 }
 
 int netlink_has_listeners(struct sock *sk, unsigned int group)
@@ -861,7 +891,7 @@ int netlink_has_listeners(struct sock *sk, unsigned int group)
        int res = 0;
        unsigned long *listeners;
 
-       BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET));
+       BUG_ON(!netlink_is_kernel(sk));
 
        rcu_read_lock();
        listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
@@ -1146,16 +1176,6 @@ static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
        put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
 }
 
-static inline void netlink_rcv_wake(struct sock *sk)
-{
-       struct netlink_sock *nlk = nlk_sk(sk);
-
-       if (skb_queue_empty(&sk->sk_receive_queue))
-               clear_bit(0, &nlk->state);
-       if (!test_bit(0, &nlk->state))
-               wake_up_interruptible(&nlk->wait);
-}
-
 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
                           struct msghdr *msg, size_t len)
 {
@@ -1303,11 +1323,7 @@ out:
 
 static void netlink_data_ready(struct sock *sk, int len)
 {
-       struct netlink_sock *nlk = nlk_sk(sk);
-
-       if (nlk->data_ready)
-               nlk->data_ready(sk, len);
-       netlink_rcv_wake(sk);
+       BUG();
 }
 
 /*
@@ -1318,7 +1334,7 @@ static void netlink_data_ready(struct sock *sk, int len)
 
 struct sock *
 netlink_kernel_create(struct net *net, int unit, unsigned int groups,
-                     void (*input)(struct sock *sk, int len),
+                     void (*input)(struct sk_buff *skb),
                      struct mutex *cb_mutex, struct module *module)
 {
        struct socket *sock;
@@ -1347,7 +1363,7 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups,
        sk = sock->sk;
        sk->sk_data_ready = netlink_data_ready;
        if (input)
-               nlk_sk(sk)->data_ready = input;
+               nlk_sk(sk)->netlink_rcv = input;
 
        if (netlink_insert(sk, net, 0))
                goto out_sock_release;
@@ -1362,6 +1378,8 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups,
                nl_table[unit].cb_mutex = cb_mutex;
                nl_table[unit].module = module;
                nl_table[unit].registered = 1;
+       } else {
+               kfree(listeners);
        }
        netlink_table_ungrab();
 
@@ -1547,12 +1565,7 @@ int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
 
        netlink_dump(sk);
        sock_put(sk);
-
-       /* We successfully started a dump, by returning -EINTR we
-        * signal the queue mangement to interrupt processing of
-        * any netlink messages so userspace gets a chance to read
-        * the results. */
-       return -EINTR;
+       return 0;
 }
 
 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
@@ -1589,13 +1602,15 @@ void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
        netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
 }
 
-static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
+int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
                                                     struct nlmsghdr *))
 {
        struct nlmsghdr *nlh;
        int err;
 
        while (skb->len >= nlmsg_total_size(0)) {
+               int msglen;
+
                nlh = nlmsg_hdr(skb);
                err = 0;
 
@@ -1611,86 +1626,20 @@ static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
                        goto skip;
 
                err = cb(skb, nlh);
-               if (err == -EINTR) {
-                       /* Not an error, but we interrupt processing */
-                       netlink_queue_skip(nlh, skb);
-                       return err;
-               }
 skip:
                if (nlh->nlmsg_flags & NLM_F_ACK || err)
                        netlink_ack(skb, nlh, err);
 
-               netlink_queue_skip(nlh, skb);
+               msglen = NLMSG_ALIGN(nlh->nlmsg_len);
+               if (msglen > skb->len)
+                       msglen = skb->len;
+               skb_pull(skb, msglen);
        }
 
        return 0;
 }
 
 /**
- * nelink_run_queue - Process netlink receive queue.
- * @sk: Netlink socket containing the queue
- * @qlen: Initial queue length
- * @cb: Callback function invoked for each netlink message found
- *
- * Processes as much as there was in the queue upon entry and invokes
- * a callback function for each netlink message found. The callback
- * function may refuse a message by returning a negative error code
- * but setting the error pointer to 0 in which case this function
- * returns with a qlen != 0.
- *
- * qlen must be initialized to 0 before the initial entry, afterwards
- * the function may be called repeatedly until the returned qlen is 0.
- *
- * The callback function may return -EINTR to signal that processing
- * of netlink messages shall be interrupted. In this case the message
- * currently being processed will NOT be requeued onto the receive
- * queue.
- */
-unsigned int netlink_run_queue(struct sock *sk, unsigned int qlen,
-                              int (*cb)(struct sk_buff *, struct nlmsghdr *))
-{
-       struct sk_buff *skb;
-
-       if (!qlen || qlen > skb_queue_len(&sk->sk_receive_queue))
-               qlen = skb_queue_len(&sk->sk_receive_queue);
-
-       for (; qlen; qlen--) {
-               skb = skb_dequeue(&sk->sk_receive_queue);
-               if (netlink_rcv_skb(skb, cb)) {
-                       if (skb->len)
-                               skb_queue_head(&sk->sk_receive_queue, skb);
-                       else {
-                               kfree_skb(skb);
-                               qlen--;
-                       }
-                       break;
-               }
-
-               kfree_skb(skb);
-       }
-
-       return qlen;
-}
-
-/**
- * netlink_queue_skip - Skip netlink message while processing queue.
- * @nlh: Netlink message to be skipped
- * @skb: Socket buffer containing the netlink messages.
- *
- * Pulls the given netlink message off the socket buffer so the next
- * call to netlink_queue_run() will not reconsider the message.
- */
-static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb)
-{
-       int msglen = NLMSG_ALIGN(nlh->nlmsg_len);
-
-       if (msglen > skb->len)
-               msglen = skb->len;
-
-       skb_pull(skb, msglen);
-}
-
-/**
  * nlmsg_notify - send a notification netlink message
  * @sk: netlink socket to use
  * @skb: notification message
@@ -1845,27 +1794,18 @@ static const struct seq_operations netlink_seq_ops = {
 
 static int netlink_seq_open(struct inode *inode, struct file *file)
 {
-       struct seq_file *seq;
        struct nl_seq_iter *iter;
-       int err;
 
-       iter = kzalloc(sizeof(*iter), GFP_KERNEL);
+       iter = __seq_open_private(file, &netlink_seq_ops, sizeof(*iter));
        if (!iter)
                return -ENOMEM;
 
-       err = seq_open(file, &netlink_seq_ops);
-       if (err) {
-               kfree(iter);
-               return err;
-       }
-
-       seq = file->private_data;
-       seq->private = iter;
        iter->net = get_proc_net(inode);
        if (!iter->net) {
                seq_release_private(inode, file);
                return -ENXIO;
        }
+
        return 0;
 }
 
@@ -2002,7 +1942,7 @@ panic:
 core_initcall(netlink_proto_init);
 
 EXPORT_SYMBOL(netlink_ack);
-EXPORT_SYMBOL(netlink_run_queue);
+EXPORT_SYMBOL(netlink_rcv_skb);
 EXPORT_SYMBOL(netlink_broadcast);
 EXPORT_SYMBOL(netlink_dump_start);
 EXPORT_SYMBOL(netlink_kernel_create);