2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@redhat.com>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
16 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
24 #include <linux/config.h>
25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/socket.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/smp_lock.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <linux/jhash.h>
52 #include <linux/jiffies.h>
53 #include <linux/random.h>
54 #include <linux/bitops.h>
56 #include <linux/types.h>
57 #include <linux/audit.h>
63 #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
66 /* struct sock has to be the first member of netlink_sock */
74 unsigned long *groups;
76 wait_queue_head_t wait;
77 struct netlink_callback *cb;
79 void (*data_ready)(struct sock *sk, int bytes);
80 struct module *module;
83 #define NETLINK_KERNEL_SOCKET 0x1
85 static inline struct netlink_sock *nlk_sk(struct sock *sk)
87 return (struct netlink_sock *)sk;
91 struct hlist_head *table;
92 unsigned long rehash_time;
98 unsigned int max_shift;
103 struct netlink_table {
104 struct nl_pid_hash hash;
105 struct hlist_head mc_list;
106 unsigned int nl_nonroot;
108 struct module *module;
112 static struct netlink_table *nl_table;
114 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
116 static int netlink_dump(struct sock *sk);
117 static void netlink_destroy_callback(struct netlink_callback *cb);
119 static DEFINE_RWLOCK(nl_table_lock);
120 static atomic_t nl_table_users = ATOMIC_INIT(0);
122 static struct notifier_block *netlink_chain;
124 static u32 netlink_group_mask(u32 group)
126 return group ? 1 << (group - 1) : 0;
129 static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
131 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
134 static void netlink_sock_destruct(struct sock *sk)
136 skb_queue_purge(&sk->sk_receive_queue);
138 if (!sock_flag(sk, SOCK_DEAD)) {
139 printk("Freeing alive netlink socket %p\n", sk);
142 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
143 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
144 BUG_TRAP(!nlk_sk(sk)->cb);
145 BUG_TRAP(!nlk_sk(sk)->groups);
148 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
149 * Look, when several writers sleep and reader wakes them up, all but one
150 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
151 * this, _but_ remember, it adds useless work on UP machines.
154 static void netlink_table_grab(void)
156 write_lock_bh(&nl_table_lock);
158 if (atomic_read(&nl_table_users)) {
159 DECLARE_WAITQUEUE(wait, current);
161 add_wait_queue_exclusive(&nl_table_wait, &wait);
163 set_current_state(TASK_UNINTERRUPTIBLE);
164 if (atomic_read(&nl_table_users) == 0)
166 write_unlock_bh(&nl_table_lock);
168 write_lock_bh(&nl_table_lock);
171 __set_current_state(TASK_RUNNING);
172 remove_wait_queue(&nl_table_wait, &wait);
176 static __inline__ void netlink_table_ungrab(void)
178 write_unlock_bh(&nl_table_lock);
179 wake_up(&nl_table_wait);
182 static __inline__ void
183 netlink_lock_table(void)
185 /* read_lock() synchronizes us to netlink_table_grab */
187 read_lock(&nl_table_lock);
188 atomic_inc(&nl_table_users);
189 read_unlock(&nl_table_lock);
192 static __inline__ void
193 netlink_unlock_table(void)
195 if (atomic_dec_and_test(&nl_table_users))
196 wake_up(&nl_table_wait);
199 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
201 struct nl_pid_hash *hash = &nl_table[protocol].hash;
202 struct hlist_head *head;
204 struct hlist_node *node;
206 read_lock(&nl_table_lock);
207 head = nl_pid_hashfn(hash, pid);
208 sk_for_each(sk, node, head) {
209 if (nlk_sk(sk)->pid == pid) {
216 read_unlock(&nl_table_lock);
220 static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
222 if (size <= PAGE_SIZE)
223 return kmalloc(size, GFP_ATOMIC);
225 return (struct hlist_head *)
226 __get_free_pages(GFP_ATOMIC, get_order(size));
229 static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
231 if (size <= PAGE_SIZE)
234 free_pages((unsigned long)table, get_order(size));
237 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
239 unsigned int omask, mask, shift;
241 struct hlist_head *otable, *table;
244 omask = mask = hash->mask;
245 osize = size = (mask + 1) * sizeof(*table);
249 if (++shift > hash->max_shift)
255 table = nl_pid_hash_alloc(size);
259 memset(table, 0, size);
260 otable = hash->table;
264 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
266 for (i = 0; i <= omask; i++) {
268 struct hlist_node *node, *tmp;
270 sk_for_each_safe(sk, node, tmp, &otable[i])
271 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
274 nl_pid_hash_free(otable, osize);
275 hash->rehash_time = jiffies + 10 * 60 * HZ;
279 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
281 int avg = hash->entries >> hash->shift;
283 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
286 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
287 nl_pid_hash_rehash(hash, 0);
294 static struct proto_ops netlink_ops;
296 static int netlink_insert(struct sock *sk, u32 pid)
298 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
299 struct hlist_head *head;
300 int err = -EADDRINUSE;
302 struct hlist_node *node;
305 netlink_table_grab();
306 head = nl_pid_hashfn(hash, pid);
308 sk_for_each(osk, node, head) {
309 if (nlk_sk(osk)->pid == pid)
321 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
324 if (len && nl_pid_hash_dilute(hash, len))
325 head = nl_pid_hashfn(hash, pid);
327 nlk_sk(sk)->pid = pid;
328 sk_add_node(sk, head);
332 netlink_table_ungrab();
336 static void netlink_remove(struct sock *sk)
338 netlink_table_grab();
339 if (sk_del_node_init(sk))
340 nl_table[sk->sk_protocol].hash.entries--;
341 if (nlk_sk(sk)->subscriptions)
342 __sk_del_bind_node(sk);
343 netlink_table_ungrab();
346 static struct proto netlink_proto = {
348 .owner = THIS_MODULE,
349 .obj_size = sizeof(struct netlink_sock),
352 static int __netlink_create(struct socket *sock, int protocol)
355 struct netlink_sock *nlk;
357 sock->ops = &netlink_ops;
359 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
363 sock_init_data(sock, sk);
366 spin_lock_init(&nlk->cb_lock);
367 init_waitqueue_head(&nlk->wait);
369 sk->sk_destruct = netlink_sock_destruct;
370 sk->sk_protocol = protocol;
374 static int netlink_create(struct socket *sock, int protocol)
376 struct module *module = NULL;
377 struct netlink_sock *nlk;
381 sock->state = SS_UNCONNECTED;
383 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
384 return -ESOCKTNOSUPPORT;
386 if (protocol<0 || protocol >= MAX_LINKS)
387 return -EPROTONOSUPPORT;
389 netlink_lock_table();
391 if (!nl_table[protocol].registered) {
392 netlink_unlock_table();
393 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
394 netlink_lock_table();
397 if (nl_table[protocol].registered &&
398 try_module_get(nl_table[protocol].module))
399 module = nl_table[protocol].module;
401 err = -EPROTONOSUPPORT;
402 groups = nl_table[protocol].groups;
403 netlink_unlock_table();
405 if (err || (err = __netlink_create(sock, protocol) < 0))
408 nlk = nlk_sk(sock->sk);
410 nlk->groups = kmalloc(NLGRPSZ(groups), GFP_KERNEL);
411 if (nlk->groups == NULL) {
415 memset(nlk->groups, 0, NLGRPSZ(groups));
416 nlk->ngroups = groups;
418 nlk->module = module;
427 static int netlink_release(struct socket *sock)
429 struct sock *sk = sock->sk;
430 struct netlink_sock *nlk;
438 spin_lock(&nlk->cb_lock);
440 nlk->cb->done(nlk->cb);
441 netlink_destroy_callback(nlk->cb);
444 spin_unlock(&nlk->cb_lock);
446 /* OK. Socket is unlinked, and, therefore,
447 no new packets will arrive */
451 wake_up_interruptible_all(&nlk->wait);
453 skb_queue_purge(&sk->sk_write_queue);
455 if (nlk->pid && !nlk->subscriptions) {
456 struct netlink_notify n = {
457 .protocol = sk->sk_protocol,
460 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
464 module_put(nlk->module);
466 if (nlk->flags & NETLINK_KERNEL_SOCKET) {
467 netlink_table_grab();
468 nl_table[sk->sk_protocol].module = NULL;
469 nl_table[sk->sk_protocol].registered = 0;
470 netlink_table_ungrab();
480 static int netlink_autobind(struct socket *sock)
482 struct sock *sk = sock->sk;
483 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
484 struct hlist_head *head;
486 struct hlist_node *node;
487 s32 pid = current->pid;
489 static s32 rover = -4097;
493 netlink_table_grab();
494 head = nl_pid_hashfn(hash, pid);
495 sk_for_each(osk, node, head) {
496 if (nlk_sk(osk)->pid == pid) {
497 /* Bind collision, search negative pid values. */
501 netlink_table_ungrab();
505 netlink_table_ungrab();
507 err = netlink_insert(sk, pid);
508 if (err == -EADDRINUSE)
511 /* If 2 threads race to autobind, that is fine. */
518 static inline int netlink_capable(struct socket *sock, unsigned int flag)
520 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
521 capable(CAP_NET_ADMIN);
525 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
527 struct netlink_sock *nlk = nlk_sk(sk);
529 if (nlk->subscriptions && !subscriptions)
530 __sk_del_bind_node(sk);
531 else if (!nlk->subscriptions && subscriptions)
532 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
533 nlk->subscriptions = subscriptions;
536 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
538 struct sock *sk = sock->sk;
539 struct netlink_sock *nlk = nlk_sk(sk);
540 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
543 if (nladdr->nl_family != AF_NETLINK)
546 /* Only superuser is allowed to listen multicasts */
547 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
551 if (nladdr->nl_pid != nlk->pid)
554 err = nladdr->nl_pid ?
555 netlink_insert(sk, nladdr->nl_pid) :
556 netlink_autobind(sock);
561 if (!nladdr->nl_groups && !(u32)nlk->groups[0])
564 netlink_table_grab();
565 netlink_update_subscriptions(sk, nlk->subscriptions +
566 hweight32(nladdr->nl_groups) -
567 hweight32(nlk->groups[0]));
568 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
569 netlink_table_ungrab();
574 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
578 struct sock *sk = sock->sk;
579 struct netlink_sock *nlk = nlk_sk(sk);
580 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
582 if (addr->sa_family == AF_UNSPEC) {
583 sk->sk_state = NETLINK_UNCONNECTED;
588 if (addr->sa_family != AF_NETLINK)
591 /* Only superuser is allowed to send multicasts */
592 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
596 err = netlink_autobind(sock);
599 sk->sk_state = NETLINK_CONNECTED;
600 nlk->dst_pid = nladdr->nl_pid;
601 nlk->dst_group = ffs(nladdr->nl_groups);
607 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
609 struct sock *sk = sock->sk;
610 struct netlink_sock *nlk = nlk_sk(sk);
611 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
613 nladdr->nl_family = AF_NETLINK;
615 *addr_len = sizeof(*nladdr);
618 nladdr->nl_pid = nlk->dst_pid;
619 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
621 nladdr->nl_pid = nlk->pid;
622 nladdr->nl_groups = nlk->groups[0];
627 static void netlink_overrun(struct sock *sk)
629 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
630 sk->sk_err = ENOBUFS;
631 sk->sk_error_report(sk);
635 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
637 int protocol = ssk->sk_protocol;
639 struct netlink_sock *nlk;
641 sock = netlink_lookup(protocol, pid);
643 return ERR_PTR(-ECONNREFUSED);
645 /* Don't bother queuing skb if kernel socket has no input function */
647 if ((nlk->pid == 0 && !nlk->data_ready) ||
648 (sock->sk_state == NETLINK_CONNECTED &&
649 nlk->dst_pid != nlk_sk(ssk)->pid)) {
651 return ERR_PTR(-ECONNREFUSED);
656 struct sock *netlink_getsockbyfilp(struct file *filp)
658 struct inode *inode = filp->f_dentry->d_inode;
661 if (!S_ISSOCK(inode->i_mode))
662 return ERR_PTR(-ENOTSOCK);
664 sock = SOCKET_I(inode)->sk;
665 if (sock->sk_family != AF_NETLINK)
666 return ERR_PTR(-EINVAL);
673 * Attach a skb to a netlink socket.
674 * The caller must hold a reference to the destination socket. On error, the
675 * reference is dropped. The skb is not send to the destination, just all
676 * all error checks are performed and memory in the queue is reserved.
678 * < 0: error. skb freed, reference to sock dropped.
680 * 1: repeat lookup - reference dropped while waiting for socket memory.
682 int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
684 struct netlink_sock *nlk;
688 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
689 test_bit(0, &nlk->state)) {
690 DECLARE_WAITQUEUE(wait, current);
699 __set_current_state(TASK_INTERRUPTIBLE);
700 add_wait_queue(&nlk->wait, &wait);
702 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
703 test_bit(0, &nlk->state)) &&
704 !sock_flag(sk, SOCK_DEAD))
705 timeo = schedule_timeout(timeo);
707 __set_current_state(TASK_RUNNING);
708 remove_wait_queue(&nlk->wait, &wait);
711 if (signal_pending(current)) {
713 return sock_intr_errno(timeo);
717 skb_set_owner_r(skb, sk);
721 int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
723 struct netlink_sock *nlk;
728 skb_queue_tail(&sk->sk_receive_queue, skb);
729 sk->sk_data_ready(sk, len);
734 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
740 static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
741 unsigned int __nocast allocation)
747 delta = skb->end - skb->tail;
748 if (delta * 2 < skb->truesize)
751 if (skb_shared(skb)) {
752 struct sk_buff *nskb = skb_clone(skb, allocation);
759 if (!pskb_expand_head(skb, 0, -delta, allocation))
760 skb->truesize -= delta;
765 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
771 skb = netlink_trim(skb, gfp_any());
773 timeo = sock_sndtimeo(ssk, nonblock);
775 sk = netlink_getsockbypid(ssk, pid);
780 err = netlink_attachskb(sk, skb, nonblock, timeo);
786 return netlink_sendskb(sk, skb, ssk->sk_protocol);
789 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
791 struct netlink_sock *nlk = nlk_sk(sk);
793 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
794 !test_bit(0, &nlk->state)) {
795 skb_set_owner_r(skb, sk);
796 skb_queue_tail(&sk->sk_receive_queue, skb);
797 sk->sk_data_ready(sk, skb->len);
798 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
803 struct netlink_broadcast_data {
804 struct sock *exclude_sk;
810 unsigned int allocation;
811 struct sk_buff *skb, *skb2;
814 static inline int do_one_broadcast(struct sock *sk,
815 struct netlink_broadcast_data *p)
817 struct netlink_sock *nlk = nlk_sk(sk);
820 if (p->exclude_sk == sk)
823 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
824 !test_bit(p->group - 1, nlk->groups))
833 if (p->skb2 == NULL) {
834 if (skb_shared(p->skb)) {
835 p->skb2 = skb_clone(p->skb, p->allocation);
837 p->skb2 = skb_get(p->skb);
839 * skb ownership may have been set when
840 * delivered to a previous socket.
845 if (p->skb2 == NULL) {
847 /* Clone failed. Notify ALL listeners. */
849 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
862 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
863 u32 group, int allocation)
865 struct netlink_broadcast_data info;
866 struct hlist_node *node;
869 skb = netlink_trim(skb, allocation);
871 info.exclude_sk = ssk;
877 info.allocation = allocation;
881 /* While we sleep in clone, do not allow to change socket list */
883 netlink_lock_table();
885 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
886 do_one_broadcast(sk, &info);
890 netlink_unlock_table();
893 kfree_skb(info.skb2);
895 if (info.delivered) {
896 if (info.congested && (allocation & __GFP_WAIT))
905 struct netlink_set_err_data {
906 struct sock *exclude_sk;
912 static inline int do_one_set_err(struct sock *sk,
913 struct netlink_set_err_data *p)
915 struct netlink_sock *nlk = nlk_sk(sk);
917 if (sk == p->exclude_sk)
920 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
921 !test_bit(p->group - 1, nlk->groups))
924 sk->sk_err = p->code;
925 sk->sk_error_report(sk);
930 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
932 struct netlink_set_err_data info;
933 struct hlist_node *node;
936 info.exclude_sk = ssk;
941 read_lock(&nl_table_lock);
943 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
944 do_one_set_err(sk, &info);
946 read_unlock(&nl_table_lock);
949 static inline void netlink_rcv_wake(struct sock *sk)
951 struct netlink_sock *nlk = nlk_sk(sk);
953 if (skb_queue_empty(&sk->sk_receive_queue))
954 clear_bit(0, &nlk->state);
955 if (!test_bit(0, &nlk->state))
956 wake_up_interruptible(&nlk->wait);
959 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
960 struct msghdr *msg, size_t len)
962 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
963 struct sock *sk = sock->sk;
964 struct netlink_sock *nlk = nlk_sk(sk);
965 struct sockaddr_nl *addr=msg->msg_name;
970 struct scm_cookie scm;
972 if (msg->msg_flags&MSG_OOB)
975 if (NULL == siocb->scm)
977 err = scm_send(sock, msg, siocb->scm);
981 if (msg->msg_namelen) {
982 if (addr->nl_family != AF_NETLINK)
984 dst_pid = addr->nl_pid;
985 dst_group = ffs(addr->nl_groups);
986 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
989 dst_pid = nlk->dst_pid;
990 dst_group = nlk->dst_group;
994 err = netlink_autobind(sock);
1000 if (len > sk->sk_sndbuf - 32)
1003 skb = alloc_skb(len, GFP_KERNEL);
1007 NETLINK_CB(skb).pid = nlk->pid;
1008 NETLINK_CB(skb).dst_pid = dst_pid;
1009 NETLINK_CB(skb).dst_group = dst_group;
1010 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
1011 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1013 /* What can I do? Netlink is asynchronous, so that
1014 we will have to save current capabilities to
1015 check them, when this message will be delivered
1016 to corresponding kernel module. --ANK (980802)
1020 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
1025 err = security_netlink_send(sk, skb);
1032 atomic_inc(&skb->users);
1033 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
1035 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1041 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1042 struct msghdr *msg, size_t len,
1045 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1046 struct scm_cookie scm;
1047 struct sock *sk = sock->sk;
1048 struct netlink_sock *nlk = nlk_sk(sk);
1049 int noblock = flags&MSG_DONTWAIT;
1051 struct sk_buff *skb;
1059 skb = skb_recv_datagram(sk,flags,noblock,&err);
1063 msg->msg_namelen = 0;
1067 msg->msg_flags |= MSG_TRUNC;
1071 skb->h.raw = skb->data;
1072 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1074 if (msg->msg_name) {
1075 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1076 addr->nl_family = AF_NETLINK;
1078 addr->nl_pid = NETLINK_CB(skb).pid;
1079 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1080 msg->msg_namelen = sizeof(*addr);
1083 if (NULL == siocb->scm) {
1084 memset(&scm, 0, sizeof(scm));
1087 siocb->scm->creds = *NETLINK_CREDS(skb);
1088 skb_free_datagram(sk, skb);
1090 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1093 scm_recv(sock, msg, siocb->scm, flags);
1096 netlink_rcv_wake(sk);
1097 return err ? : copied;
1100 static void netlink_data_ready(struct sock *sk, int len)
1102 struct netlink_sock *nlk = nlk_sk(sk);
1104 if (nlk->data_ready)
1105 nlk->data_ready(sk, len);
1106 netlink_rcv_wake(sk);
1110 * We export these functions to other modules. They provide a
1111 * complete set of kernel non-blocking support for message
1116 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len), struct module *module)
1118 struct socket *sock;
1120 struct netlink_sock *nlk;
1125 if (unit<0 || unit>=MAX_LINKS)
1128 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1131 if (__netlink_create(sock, unit) < 0)
1132 goto out_sock_release;
1135 sk->sk_data_ready = netlink_data_ready;
1137 nlk_sk(sk)->data_ready = input;
1139 if (netlink_insert(sk, 0))
1140 goto out_sock_release;
1143 nlk->flags |= NETLINK_KERNEL_SOCKET;
1145 netlink_table_grab();
1146 nl_table[unit].groups = 32;
1147 nl_table[unit].module = module;
1148 nl_table[unit].registered = 1;
1149 netlink_table_ungrab();
1158 void netlink_set_nonroot(int protocol, unsigned int flags)
1160 if ((unsigned int)protocol < MAX_LINKS)
1161 nl_table[protocol].nl_nonroot = flags;
1164 static void netlink_destroy_callback(struct netlink_callback *cb)
1172 * It looks a bit ugly.
1173 * It would be better to create kernel thread.
1176 static int netlink_dump(struct sock *sk)
1178 struct netlink_sock *nlk = nlk_sk(sk);
1179 struct netlink_callback *cb;
1180 struct sk_buff *skb;
1181 struct nlmsghdr *nlh;
1184 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1188 spin_lock(&nlk->cb_lock);
1192 spin_unlock(&nlk->cb_lock);
1197 len = cb->dump(skb, cb);
1200 spin_unlock(&nlk->cb_lock);
1201 skb_queue_tail(&sk->sk_receive_queue, skb);
1202 sk->sk_data_ready(sk, len);
1206 nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1207 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1208 skb_queue_tail(&sk->sk_receive_queue, skb);
1209 sk->sk_data_ready(sk, skb->len);
1213 spin_unlock(&nlk->cb_lock);
1215 netlink_destroy_callback(cb);
1222 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1223 struct nlmsghdr *nlh,
1224 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1225 int (*done)(struct netlink_callback*))
1227 struct netlink_callback *cb;
1229 struct netlink_sock *nlk;
1231 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1235 memset(cb, 0, sizeof(*cb));
1239 atomic_inc(&skb->users);
1242 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1244 netlink_destroy_callback(cb);
1245 return -ECONNREFUSED;
1248 /* A dump is in progress... */
1249 spin_lock(&nlk->cb_lock);
1251 spin_unlock(&nlk->cb_lock);
1252 netlink_destroy_callback(cb);
1257 spin_unlock(&nlk->cb_lock);
1264 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1266 struct sk_buff *skb;
1267 struct nlmsghdr *rep;
1268 struct nlmsgerr *errmsg;
1272 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1274 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1276 skb = alloc_skb(size, GFP_KERNEL);
1280 sk = netlink_lookup(in_skb->sk->sk_protocol,
1281 NETLINK_CB(in_skb).pid);
1283 sk->sk_err = ENOBUFS;
1284 sk->sk_error_report(sk);
1290 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1291 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
1292 errmsg = NLMSG_DATA(rep);
1293 errmsg->error = err;
1294 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1295 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1299 #ifdef CONFIG_PROC_FS
1300 struct nl_seq_iter {
1305 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1307 struct nl_seq_iter *iter = seq->private;
1310 struct hlist_node *node;
1313 for (i=0; i<MAX_LINKS; i++) {
1314 struct nl_pid_hash *hash = &nl_table[i].hash;
1316 for (j = 0; j <= hash->mask; j++) {
1317 sk_for_each(s, node, &hash->table[j]) {
1330 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1332 read_lock(&nl_table_lock);
1333 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1336 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1339 struct nl_seq_iter *iter;
1344 if (v == SEQ_START_TOKEN)
1345 return netlink_seq_socket_idx(seq, 0);
1351 iter = seq->private;
1353 j = iter->hash_idx + 1;
1356 struct nl_pid_hash *hash = &nl_table[i].hash;
1358 for (; j <= hash->mask; j++) {
1359 s = sk_head(&hash->table[j]);
1368 } while (++i < MAX_LINKS);
1373 static void netlink_seq_stop(struct seq_file *seq, void *v)
1375 read_unlock(&nl_table_lock);
1379 static int netlink_seq_show(struct seq_file *seq, void *v)
1381 if (v == SEQ_START_TOKEN)
1383 "sk Eth Pid Groups "
1384 "Rmem Wmem Dump Locks\n");
1387 struct netlink_sock *nlk = nlk_sk(s);
1389 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1393 nlk->flags & NETLINK_KERNEL_SOCKET ?
1394 0 : (unsigned int)nlk->groups[0],
1395 atomic_read(&s->sk_rmem_alloc),
1396 atomic_read(&s->sk_wmem_alloc),
1398 atomic_read(&s->sk_refcnt)
1405 static struct seq_operations netlink_seq_ops = {
1406 .start = netlink_seq_start,
1407 .next = netlink_seq_next,
1408 .stop = netlink_seq_stop,
1409 .show = netlink_seq_show,
1413 static int netlink_seq_open(struct inode *inode, struct file *file)
1415 struct seq_file *seq;
1416 struct nl_seq_iter *iter;
1419 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1423 err = seq_open(file, &netlink_seq_ops);
1429 memset(iter, 0, sizeof(*iter));
1430 seq = file->private_data;
1431 seq->private = iter;
1435 static struct file_operations netlink_seq_fops = {
1436 .owner = THIS_MODULE,
1437 .open = netlink_seq_open,
1439 .llseek = seq_lseek,
1440 .release = seq_release_private,
1445 int netlink_register_notifier(struct notifier_block *nb)
1447 return notifier_chain_register(&netlink_chain, nb);
1450 int netlink_unregister_notifier(struct notifier_block *nb)
1452 return notifier_chain_unregister(&netlink_chain, nb);
1455 static struct proto_ops netlink_ops = {
1456 .family = PF_NETLINK,
1457 .owner = THIS_MODULE,
1458 .release = netlink_release,
1459 .bind = netlink_bind,
1460 .connect = netlink_connect,
1461 .socketpair = sock_no_socketpair,
1462 .accept = sock_no_accept,
1463 .getname = netlink_getname,
1464 .poll = datagram_poll,
1465 .ioctl = sock_no_ioctl,
1466 .listen = sock_no_listen,
1467 .shutdown = sock_no_shutdown,
1468 .setsockopt = sock_no_setsockopt,
1469 .getsockopt = sock_no_getsockopt,
1470 .sendmsg = netlink_sendmsg,
1471 .recvmsg = netlink_recvmsg,
1472 .mmap = sock_no_mmap,
1473 .sendpage = sock_no_sendpage,
1476 static struct net_proto_family netlink_family_ops = {
1477 .family = PF_NETLINK,
1478 .create = netlink_create,
1479 .owner = THIS_MODULE, /* for consistency 8) */
1482 extern void netlink_skb_parms_too_large(void);
1484 static int __init netlink_proto_init(void)
1486 struct sk_buff *dummy_skb;
1490 int err = proto_register(&netlink_proto, 0);
1495 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1496 netlink_skb_parms_too_large();
1498 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1501 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1505 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1507 if (num_physpages >= (128 * 1024))
1508 max = num_physpages >> (21 - PAGE_SHIFT);
1510 max = num_physpages >> (23 - PAGE_SHIFT);
1512 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1513 max = (1UL << order) / sizeof(struct hlist_head);
1514 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1516 for (i = 0; i < MAX_LINKS; i++) {
1517 struct nl_pid_hash *hash = &nl_table[i].hash;
1519 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1522 nl_pid_hash_free(nl_table[i].hash.table,
1523 1 * sizeof(*hash->table));
1527 memset(hash->table, 0, 1 * sizeof(*hash->table));
1528 hash->max_shift = order;
1531 hash->rehash_time = jiffies;
1534 sock_register(&netlink_family_ops);
1535 #ifdef CONFIG_PROC_FS
1536 proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1538 /* The netlink device handler may be needed early. */
1544 core_initcall(netlink_proto_init);
1546 EXPORT_SYMBOL(netlink_ack);
1547 EXPORT_SYMBOL(netlink_broadcast);
1548 EXPORT_SYMBOL(netlink_dump_start);
1549 EXPORT_SYMBOL(netlink_kernel_create);
1550 EXPORT_SYMBOL(netlink_register_notifier);
1551 EXPORT_SYMBOL(netlink_set_err);
1552 EXPORT_SYMBOL(netlink_set_nonroot);
1553 EXPORT_SYMBOL(netlink_unicast);
1554 EXPORT_SYMBOL(netlink_unregister_notifier);