Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-next-2.6
[safe/jmp/linux-2.6] / net / netlink / af_netlink.c
index dbd7cad..b73d4e6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * NETLINK      Kernel-user communication protocol.
  *
- *             Authors:        Alan Cox <alan@redhat.com>
+ *             Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
  *                             Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  *
  *             This program is free software; you can redistribute it and/or
@@ -54,7 +54,6 @@
 #include <linux/mm.h>
 #include <linux/types.h>
 #include <linux/audit.h>
-#include <linux/selinux.h>
 #include <linux/mutex.h>
 
 #include <net/net_namespace.h>
@@ -86,6 +85,7 @@ struct netlink_sock {
 
 #define NETLINK_KERNEL_SOCKET  0x1
 #define NETLINK_RECV_PKTINFO   0x2
+#define NETLINK_BROADCAST_SEND_ERROR   0x4
 
 static inline struct netlink_sock *nlk_sk(struct sock *sk)
 {
@@ -159,9 +159,10 @@ static void netlink_sock_destruct(struct sock *sk)
                printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
                return;
        }
-       BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
-       BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
-       BUG_TRAP(!nlk_sk(sk)->groups);
+
+       WARN_ON(atomic_read(&sk->sk_rmem_alloc));
+       WARN_ON(atomic_read(&sk->sk_wmem_alloc));
+       WARN_ON(nlk_sk(sk)->groups);
 }
 
 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
@@ -171,6 +172,7 @@ static void netlink_sock_destruct(struct sock *sk)
  */
 
 static void netlink_table_grab(void)
+       __acquires(nl_table_lock)
 {
        write_lock_irq(&nl_table_lock);
 
@@ -192,7 +194,8 @@ static void netlink_table_grab(void)
        }
 }
 
-static inline void netlink_table_ungrab(void)
+static void netlink_table_ungrab(void)
+       __releases(nl_table_lock)
 {
        write_unlock_irq(&nl_table_lock);
        wake_up(&nl_table_wait);
@@ -226,7 +229,7 @@ static inline struct sock *netlink_lookup(struct net *net, int protocol,
        read_lock(&nl_table_lock);
        head = nl_pid_hashfn(hash, pid);
        sk_for_each(sk, node, head) {
-               if ((sk->sk_net == net) && (nlk_sk(sk)->pid == pid)) {
+               if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
                        sock_hold(sk);
                        goto found;
                }
@@ -346,7 +349,7 @@ static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
        head = nl_pid_hashfn(hash, pid);
        len = 0;
        sk_for_each(osk, node, head) {
-               if ((osk->sk_net == net) && (nlk_sk(osk)->pid == pid))
+               if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
                        break;
                len++;
        }
@@ -433,7 +436,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol)
                return -EPROTONOSUPPORT;
 
        netlink_lock_table();
-#ifdef CONFIG_KMOD
+#ifdef CONFIG_MODULES
        if (!nl_table[protocol].registered) {
                netlink_unlock_table();
                request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
@@ -450,6 +453,10 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol)
        if (err < 0)
                goto out_module;
 
+       local_bh_disable();
+       sock_prot_inuse_add(net, &netlink_proto, 1);
+       local_bh_enable();
+
        nlk = nlk_sk(sock->sk);
        nlk->module = module;
 out:
@@ -484,7 +491,7 @@ static int netlink_release(struct socket *sock)
 
        if (nlk->pid && !nlk->subscriptions) {
                struct netlink_notify n = {
-                                               .net = sk->sk_net,
+                                               .net = sock_net(sk),
                                                .protocol = sk->sk_protocol,
                                                .pid = nlk->pid,
                                          };
@@ -496,9 +503,12 @@ static int netlink_release(struct socket *sock)
 
        netlink_table_grab();
        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;
+               BUG_ON(nl_table[sk->sk_protocol].registered == 0);
+               if (--nl_table[sk->sk_protocol].registered == 0) {
+                       kfree(nl_table[sk->sk_protocol].listeners);
+                       nl_table[sk->sk_protocol].module = NULL;
+                       nl_table[sk->sk_protocol].registered = 0;
+               }
        } else if (nlk->subscriptions)
                netlink_update_listeners(sk);
        netlink_table_ungrab();
@@ -506,6 +516,9 @@ static int netlink_release(struct socket *sock)
        kfree(nlk->groups);
        nlk->groups = NULL;
 
+       local_bh_disable();
+       sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
+       local_bh_enable();
        sock_put(sk);
        return 0;
 }
@@ -513,7 +526,7 @@ static int netlink_release(struct socket *sock)
 static int netlink_autobind(struct socket *sock)
 {
        struct sock *sk = sock->sk;
-       struct net *net = sk->sk_net;
+       struct net *net = sock_net(sk);
        struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
        struct hlist_head *head;
        struct sock *osk;
@@ -527,7 +540,7 @@ retry:
        netlink_table_grab();
        head = nl_pid_hashfn(hash, pid);
        sk_for_each(osk, node, head) {
-               if ((osk->sk_net != net))
+               if (!net_eq(sock_net(osk), net))
                        continue;
                if (nlk_sk(osk)->pid == pid) {
                        /* Bind collision, search negative pid values. */
@@ -606,7 +619,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
                        int addr_len)
 {
        struct sock *sk = sock->sk;
-       struct net *net = sk->sk_net;
+       struct net *net = sock_net(sk);
        struct netlink_sock *nlk = nlk_sk(sk);
        struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
        int err;
@@ -715,7 +728,7 @@ static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
        struct sock *sock;
        struct netlink_sock *nlk;
 
-       sock = netlink_lookup(ssk->sk_net, ssk->sk_protocol, pid);
+       sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
        if (!sock)
                return ERR_PTR(-ECONNREFUSED);
 
@@ -755,7 +768,7 @@ struct sock *netlink_getsockbyfilp(struct file *filp)
  * 0: continue
  * 1: repeat lookup - reference dropped while waiting for socket memory.
  */
-int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
+int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
                      long *timeo, struct sock *ssk)
 {
        struct netlink_sock *nlk;
@@ -881,7 +894,14 @@ retry:
        if (netlink_is_kernel(sk))
                return netlink_unicast_kernel(sk, skb);
 
-       err = netlink_attachskb(sk, skb, nonblock, &timeo, ssk);
+       if (sk_filter(sk, skb)) {
+               err = skb->len;
+               kfree_skb(skb);
+               sock_put(sk);
+               return err;
+       }
+
+       err = netlink_attachskb(sk, skb, &timeo, ssk);
        if (err == 1)
                goto retry;
        if (err)
@@ -931,6 +951,7 @@ struct netlink_broadcast_data {
        u32 pid;
        u32 group;
        int failure;
+       int delivery_failure;
        int congested;
        int delivered;
        gfp_t allocation;
@@ -950,7 +971,7 @@ static inline int do_one_broadcast(struct sock *sk,
            !test_bit(p->group - 1, nlk->groups))
                goto out;
 
-       if ((sk->sk_net != p->net))
+       if (!net_eq(sock_net(sk), p->net))
                goto out;
 
        if (p->failure) {
@@ -975,8 +996,15 @@ static inline int do_one_broadcast(struct sock *sk,
                netlink_overrun(sk);
                /* Clone failed. Notify ALL listeners. */
                p->failure = 1;
+               if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
+                       p->delivery_failure = 1;
+       } else if (sk_filter(sk, p->skb2)) {
+               kfree_skb(p->skb2);
+               p->skb2 = NULL;
        } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
                netlink_overrun(sk);
+               if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
+                       p->delivery_failure = 1;
        } else {
                p->congested |= val;
                p->delivered = 1;
@@ -991,7 +1019,7 @@ out:
 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
                      u32 group, gfp_t allocation)
 {
-       struct net *net = ssk->sk_net;
+       struct net *net = sock_net(ssk);
        struct netlink_broadcast_data info;
        struct hlist_node *node;
        struct sock *sk;
@@ -1003,6 +1031,7 @@ int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
        info.pid = pid;
        info.group = group;
        info.failure = 0;
+       info.delivery_failure = 0;
        info.congested = 0;
        info.delivered = 0;
        info.allocation = allocation;
@@ -1020,16 +1049,16 @@ int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
 
        netlink_unlock_table();
 
-       if (info.skb2)
-               kfree_skb(info.skb2);
+       kfree_skb(info.skb2);
+
+       if (info.delivery_failure)
+               return -ENOBUFS;
 
        if (info.delivered) {
                if (info.congested && (allocation & __GFP_WAIT))
                        yield();
                return 0;
        }
-       if (info.failure)
-               return -ENOBUFS;
        return -ESRCH;
 }
 EXPORT_SYMBOL(netlink_broadcast);
@@ -1049,7 +1078,7 @@ static inline int do_one_set_err(struct sock *sk,
        if (sk == p->exclude_sk)
                goto out;
 
-       if (sk->sk_net != p->exclude_sk->sk_net)
+       if (sock_net(sk) != sock_net(p->exclude_sk))
                goto out;
 
        if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
@@ -1062,6 +1091,13 @@ out:
        return 0;
 }
 
+/**
+ * netlink_set_err - report error to broadcast listeners
+ * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
+ * @pid: the PID of a process that we want to skip (if any)
+ * @groups: the broadcast group that will notice the error
+ * @code: error code, must be negative (as usual in kernelspace)
+ */
 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
 {
        struct netlink_set_err_data info;
@@ -1071,7 +1107,8 @@ void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
        info.exclude_sk = ssk;
        info.pid = pid;
        info.group = group;
-       info.code = code;
+       /* sk->sk_err wants a positive error value */
+       info.code = -code;
 
        read_lock(&nl_table_lock);
 
@@ -1080,6 +1117,7 @@ void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
 
        read_unlock(&nl_table_lock);
 }
+EXPORT_SYMBOL(netlink_set_err);
 
 /* must be called with netlink table grabbed */
 static void netlink_update_socket_mc(struct netlink_sock *nlk,
@@ -1137,6 +1175,13 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
                err = 0;
                break;
        }
+       case NETLINK_BROADCAST_ERROR:
+               if (val)
+                       nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
+               else
+                       nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
+               err = 0;
+               break;
        default:
                err = -ENOPROTOOPT;
        }
@@ -1169,6 +1214,16 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
                        return -EFAULT;
                err = 0;
                break;
+       case NETLINK_BROADCAST_ERROR:
+               if (len < sizeof(int))
+                       return -EINVAL;
+               len = sizeof(int);
+               val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
+               if (put_user(len, optlen) ||
+                   put_user(val, optval))
+                       return -EFAULT;
+               err = 0;
+               break;
        default:
                err = -ENOPROTOOPT;
        }
@@ -1233,8 +1288,9 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 
        NETLINK_CB(skb).pid     = nlk->pid;
        NETLINK_CB(skb).dst_group = dst_group;
-       NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
-       selinux_get_task_sid(current, &(NETLINK_CB(skb).sid));
+       NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
+       NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
+       security_task_getsecid(current, &(NETLINK_CB(skb).sid));
        memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
 
        /* What can I do? Netlink is asynchronous, so that
@@ -1357,8 +1413,17 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups,
        if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
                return NULL;
 
-       if (__netlink_create(net, sock, cb_mutex, unit) < 0)
-               goto out_sock_release;
+       /*
+        * We have to just have a reference on the net from sk, but don't
+        * get_net it. Besides, we cannot get and then put the net here.
+        * So we create one inside init_net and the move it to net.
+        */
+
+       if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
+               goto out_sock_release_nosk;
+
+       sk = sock->sk;
+       sk_change_net(sk, net);
 
        if (groups < 32)
                groups = 32;
@@ -1367,7 +1432,6 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups,
        if (!listeners)
                goto out_sock_release;
 
-       sk = sock->sk;
        sk->sk_data_ready = netlink_data_ready;
        if (input)
                nlk_sk(sk)->netlink_rcv = input;
@@ -1387,18 +1451,31 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups,
                nl_table[unit].registered = 1;
        } else {
                kfree(listeners);
+               nl_table[unit].registered++;
        }
        netlink_table_ungrab();
-
        return sk;
 
 out_sock_release:
        kfree(listeners);
+       netlink_kernel_release(sk);
+       return NULL;
+
+out_sock_release_nosk:
        sock_release(sock);
        return NULL;
 }
 EXPORT_SYMBOL(netlink_kernel_create);
 
+
+void
+netlink_kernel_release(struct sock *sk)
+{
+       sk_release_kernel(sk);
+}
+EXPORT_SYMBOL(netlink_kernel_release);
+
+
 /**
  * netlink_change_ngroups - change number of multicast groups
  *
@@ -1473,8 +1550,7 @@ EXPORT_SYMBOL(netlink_set_nonroot);
 
 static void netlink_destroy_callback(struct netlink_callback *cb)
 {
-       if (cb->skb)
-               kfree_skb(cb->skb);
+       kfree_skb(cb->skb);
        kfree(cb);
 }
 
@@ -1507,8 +1583,13 @@ static int netlink_dump(struct sock *sk)
 
        if (len > 0) {
                mutex_unlock(nlk->cb_mutex);
-               skb_queue_tail(&sk->sk_receive_queue, skb);
-               sk->sk_data_ready(sk, len);
+
+               if (sk_filter(sk, skb))
+                       kfree_skb(skb);
+               else {
+                       skb_queue_tail(&sk->sk_receive_queue, skb);
+                       sk->sk_data_ready(sk, skb->len);
+               }
                return 0;
        }
 
@@ -1518,8 +1599,12 @@ static int netlink_dump(struct sock *sk)
 
        memcpy(nlmsg_data(nlh), &len, sizeof(len));
 
-       skb_queue_tail(&sk->sk_receive_queue, skb);
-       sk->sk_data_ready(sk, skb->len);
+       if (sk_filter(sk, skb))
+               kfree_skb(skb);
+       else {
+               skb_queue_tail(&sk->sk_receive_queue, skb);
+               sk->sk_data_ready(sk, skb->len);
+       }
 
        if (cb->done)
                cb->done(cb);
@@ -1556,7 +1641,7 @@ int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
        atomic_inc(&skb->users);
        cb->skb = skb;
 
-       sk = netlink_lookup(ssk->sk_net, ssk->sk_protocol, NETLINK_CB(skb).pid);
+       sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
        if (sk == NULL) {
                netlink_destroy_callback(cb);
                return -ECONNREFUSED;
@@ -1598,7 +1683,7 @@ void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
        if (!skb) {
                struct sock *sk;
 
-               sk = netlink_lookup(in_skb->sk->sk_net,
+               sk = netlink_lookup(sock_net(in_skb->sk),
                                    in_skb->sk->sk_protocol,
                                    NETLINK_CB(in_skb).pid);
                if (sk) {
@@ -1682,12 +1767,18 @@ int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
                        exclude_pid = pid;
                }
 
-               /* errors reported via destination sk->sk_err */
-               nlmsg_multicast(sk, skb, exclude_pid, group, flags);
+               /* errors reported via destination sk->sk_err, but propagate
+                * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
+               err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
        }
 
-       if (report)
-               err = nlmsg_unicast(sk, skb, pid);
+       if (report) {
+               int err2;
+
+               err2 = nlmsg_unicast(sk, skb, pid);
+               if (!err || err == -ESRCH)
+                       err = err2;
+       }
 
        return err;
 }
@@ -1713,7 +1804,7 @@ static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
 
                for (j = 0; j <= hash->mask; j++) {
                        sk_for_each(s, node, &hash->table[j]) {
-                               if (iter->p.net != s->sk_net)
+                               if (sock_net(s) != seq_file_net(seq))
                                        continue;
                                if (off == pos) {
                                        iter->link = i;
@@ -1728,6 +1819,7 @@ static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
 }
 
 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
+       __acquires(nl_table_lock)
 {
        read_lock(&nl_table_lock);
        return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
@@ -1748,7 +1840,7 @@ static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
        s = v;
        do {
                s = sk_next(s);
-       } while (s && (iter->p.net != s->sk_net));
+       } while (s && sock_net(s) != seq_file_net(seq));
        if (s)
                return s;
 
@@ -1760,7 +1852,7 @@ static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 
                for (; j <= hash->mask; j++) {
                        s = sk_head(&hash->table[j]);
-                       while (s && (iter->p.net != s->sk_net))
+                       while (s && sock_net(s) != seq_file_net(seq))
                                s = sk_next(s);
                        if (s) {
                                iter->link = i;
@@ -1776,6 +1868,7 @@ static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 }
 
 static void netlink_seq_stop(struct seq_file *seq, void *v)
+       __releases(nl_table_lock)
 {
        read_unlock(&nl_table_lock);
 }