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>
65 /* struct sock has to be the first member of netlink_sock */
72 wait_queue_head_t wait;
73 struct netlink_callback *cb;
75 void (*data_ready)(struct sock *sk, int bytes);
76 struct module *module;
80 #define NETLINK_KERNEL_SOCKET 0x1
82 static inline struct netlink_sock *nlk_sk(struct sock *sk)
84 return (struct netlink_sock *)sk;
88 struct hlist_head *table;
89 unsigned long rehash_time;
95 unsigned int max_shift;
100 struct netlink_table {
101 struct nl_pid_hash hash;
102 struct hlist_head mc_list;
103 unsigned int nl_nonroot;
104 struct module *module;
108 static struct netlink_table *nl_table;
110 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
112 static int netlink_dump(struct sock *sk);
113 static void netlink_destroy_callback(struct netlink_callback *cb);
115 static DEFINE_RWLOCK(nl_table_lock);
116 static atomic_t nl_table_users = ATOMIC_INIT(0);
118 static struct notifier_block *netlink_chain;
120 static u32 netlink_group_mask(u32 group)
122 return group ? 1 << (group - 1) : 0;
125 static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
127 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
130 static void netlink_sock_destruct(struct sock *sk)
132 skb_queue_purge(&sk->sk_receive_queue);
134 if (!sock_flag(sk, SOCK_DEAD)) {
135 printk("Freeing alive netlink socket %p\n", sk);
138 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
139 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
140 BUG_TRAP(!nlk_sk(sk)->cb);
143 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
144 * Look, when several writers sleep and reader wakes them up, all but one
145 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
146 * this, _but_ remember, it adds useless work on UP machines.
149 static void netlink_table_grab(void)
151 write_lock_bh(&nl_table_lock);
153 if (atomic_read(&nl_table_users)) {
154 DECLARE_WAITQUEUE(wait, current);
156 add_wait_queue_exclusive(&nl_table_wait, &wait);
158 set_current_state(TASK_UNINTERRUPTIBLE);
159 if (atomic_read(&nl_table_users) == 0)
161 write_unlock_bh(&nl_table_lock);
163 write_lock_bh(&nl_table_lock);
166 __set_current_state(TASK_RUNNING);
167 remove_wait_queue(&nl_table_wait, &wait);
171 static __inline__ void netlink_table_ungrab(void)
173 write_unlock_bh(&nl_table_lock);
174 wake_up(&nl_table_wait);
177 static __inline__ void
178 netlink_lock_table(void)
180 /* read_lock() synchronizes us to netlink_table_grab */
182 read_lock(&nl_table_lock);
183 atomic_inc(&nl_table_users);
184 read_unlock(&nl_table_lock);
187 static __inline__ void
188 netlink_unlock_table(void)
190 if (atomic_dec_and_test(&nl_table_users))
191 wake_up(&nl_table_wait);
194 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
196 struct nl_pid_hash *hash = &nl_table[protocol].hash;
197 struct hlist_head *head;
199 struct hlist_node *node;
201 read_lock(&nl_table_lock);
202 head = nl_pid_hashfn(hash, pid);
203 sk_for_each(sk, node, head) {
204 if (nlk_sk(sk)->pid == pid) {
211 read_unlock(&nl_table_lock);
215 static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
217 if (size <= PAGE_SIZE)
218 return kmalloc(size, GFP_ATOMIC);
220 return (struct hlist_head *)
221 __get_free_pages(GFP_ATOMIC, get_order(size));
224 static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
226 if (size <= PAGE_SIZE)
229 free_pages((unsigned long)table, get_order(size));
232 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
234 unsigned int omask, mask, shift;
236 struct hlist_head *otable, *table;
239 omask = mask = hash->mask;
240 osize = size = (mask + 1) * sizeof(*table);
244 if (++shift > hash->max_shift)
250 table = nl_pid_hash_alloc(size);
254 memset(table, 0, size);
255 otable = hash->table;
259 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
261 for (i = 0; i <= omask; i++) {
263 struct hlist_node *node, *tmp;
265 sk_for_each_safe(sk, node, tmp, &otable[i])
266 __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
269 nl_pid_hash_free(otable, osize);
270 hash->rehash_time = jiffies + 10 * 60 * HZ;
274 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
276 int avg = hash->entries >> hash->shift;
278 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
281 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
282 nl_pid_hash_rehash(hash, 0);
289 static struct proto_ops netlink_ops;
291 static int netlink_insert(struct sock *sk, u32 pid)
293 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
294 struct hlist_head *head;
295 int err = -EADDRINUSE;
297 struct hlist_node *node;
300 netlink_table_grab();
301 head = nl_pid_hashfn(hash, pid);
303 sk_for_each(osk, node, head) {
304 if (nlk_sk(osk)->pid == pid)
316 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
319 if (len && nl_pid_hash_dilute(hash, len))
320 head = nl_pid_hashfn(hash, pid);
322 nlk_sk(sk)->pid = pid;
323 sk_add_node(sk, head);
327 netlink_table_ungrab();
331 static void netlink_remove(struct sock *sk)
333 netlink_table_grab();
334 if (sk_del_node_init(sk))
335 nl_table[sk->sk_protocol].hash.entries--;
336 if (nlk_sk(sk)->groups)
337 __sk_del_bind_node(sk);
338 netlink_table_ungrab();
341 static struct proto netlink_proto = {
343 .owner = THIS_MODULE,
344 .obj_size = sizeof(struct netlink_sock),
347 static int __netlink_create(struct socket *sock, int protocol)
350 struct netlink_sock *nlk;
352 sock->ops = &netlink_ops;
354 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
358 sock_init_data(sock, sk);
361 spin_lock_init(&nlk->cb_lock);
362 init_waitqueue_head(&nlk->wait);
364 sk->sk_destruct = netlink_sock_destruct;
365 sk->sk_protocol = protocol;
369 static int netlink_create(struct socket *sock, int protocol)
371 struct module *module = NULL;
374 sock->state = SS_UNCONNECTED;
376 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
377 return -ESOCKTNOSUPPORT;
379 if (protocol<0 || protocol >= MAX_LINKS)
380 return -EPROTONOSUPPORT;
382 netlink_lock_table();
384 if (!nl_table[protocol].registered) {
385 netlink_unlock_table();
386 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
387 netlink_lock_table();
390 if (nl_table[protocol].registered &&
391 try_module_get(nl_table[protocol].module))
392 module = nl_table[protocol].module;
394 err = -EPROTONOSUPPORT;
395 netlink_unlock_table();
400 if ((err = __netlink_create(sock, protocol) < 0))
403 nlk_sk(sock->sk)->module = module;
412 static int netlink_release(struct socket *sock)
414 struct sock *sk = sock->sk;
415 struct netlink_sock *nlk;
423 spin_lock(&nlk->cb_lock);
425 nlk->cb->done(nlk->cb);
426 netlink_destroy_callback(nlk->cb);
429 spin_unlock(&nlk->cb_lock);
431 /* OK. Socket is unlinked, and, therefore,
432 no new packets will arrive */
436 wake_up_interruptible_all(&nlk->wait);
438 skb_queue_purge(&sk->sk_write_queue);
440 if (nlk->pid && !nlk->groups) {
441 struct netlink_notify n = {
442 .protocol = sk->sk_protocol,
445 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
449 module_put(nlk->module);
451 if (nlk->flags & NETLINK_KERNEL_SOCKET) {
452 netlink_table_grab();
453 nl_table[sk->sk_protocol].module = NULL;
454 nl_table[sk->sk_protocol].registered = 0;
455 netlink_table_ungrab();
462 static int netlink_autobind(struct socket *sock)
464 struct sock *sk = sock->sk;
465 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
466 struct hlist_head *head;
468 struct hlist_node *node;
469 s32 pid = current->pid;
471 static s32 rover = -4097;
475 netlink_table_grab();
476 head = nl_pid_hashfn(hash, pid);
477 sk_for_each(osk, node, head) {
478 if (nlk_sk(osk)->pid == pid) {
479 /* Bind collision, search negative pid values. */
483 netlink_table_ungrab();
487 netlink_table_ungrab();
489 err = netlink_insert(sk, pid);
490 if (err == -EADDRINUSE)
493 /* If 2 threads race to autobind, that is fine. */
500 static inline int netlink_capable(struct socket *sock, unsigned int flag)
502 return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
503 capable(CAP_NET_ADMIN);
506 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
508 struct sock *sk = sock->sk;
509 struct netlink_sock *nlk = nlk_sk(sk);
510 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
513 if (nladdr->nl_family != AF_NETLINK)
516 /* Only superuser is allowed to listen multicasts */
517 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
521 if (nladdr->nl_pid != nlk->pid)
524 err = nladdr->nl_pid ?
525 netlink_insert(sk, nladdr->nl_pid) :
526 netlink_autobind(sock);
531 if (!nladdr->nl_groups && !nlk->groups)
534 netlink_table_grab();
535 if (nlk->groups && !nladdr->nl_groups)
536 __sk_del_bind_node(sk);
537 else if (!nlk->groups && nladdr->nl_groups)
538 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
539 nlk->groups = nladdr->nl_groups;
540 netlink_table_ungrab();
545 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
549 struct sock *sk = sock->sk;
550 struct netlink_sock *nlk = nlk_sk(sk);
551 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
553 if (addr->sa_family == AF_UNSPEC) {
554 sk->sk_state = NETLINK_UNCONNECTED;
559 if (addr->sa_family != AF_NETLINK)
562 /* Only superuser is allowed to send multicasts */
563 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
567 err = netlink_autobind(sock);
570 sk->sk_state = NETLINK_CONNECTED;
571 nlk->dst_pid = nladdr->nl_pid;
572 nlk->dst_group = ffs(nladdr->nl_groups);
578 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
580 struct sock *sk = sock->sk;
581 struct netlink_sock *nlk = nlk_sk(sk);
582 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
584 nladdr->nl_family = AF_NETLINK;
586 *addr_len = sizeof(*nladdr);
589 nladdr->nl_pid = nlk->dst_pid;
590 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
592 nladdr->nl_pid = nlk->pid;
593 nladdr->nl_groups = nlk->groups;
598 static void netlink_overrun(struct sock *sk)
600 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
601 sk->sk_err = ENOBUFS;
602 sk->sk_error_report(sk);
606 static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
608 int protocol = ssk->sk_protocol;
610 struct netlink_sock *nlk;
612 sock = netlink_lookup(protocol, pid);
614 return ERR_PTR(-ECONNREFUSED);
616 /* Don't bother queuing skb if kernel socket has no input function */
618 if ((nlk->pid == 0 && !nlk->data_ready) ||
619 (sock->sk_state == NETLINK_CONNECTED &&
620 nlk->dst_pid != nlk_sk(ssk)->pid)) {
622 return ERR_PTR(-ECONNREFUSED);
627 struct sock *netlink_getsockbyfilp(struct file *filp)
629 struct inode *inode = filp->f_dentry->d_inode;
632 if (!S_ISSOCK(inode->i_mode))
633 return ERR_PTR(-ENOTSOCK);
635 sock = SOCKET_I(inode)->sk;
636 if (sock->sk_family != AF_NETLINK)
637 return ERR_PTR(-EINVAL);
644 * Attach a skb to a netlink socket.
645 * The caller must hold a reference to the destination socket. On error, the
646 * reference is dropped. The skb is not send to the destination, just all
647 * all error checks are performed and memory in the queue is reserved.
649 * < 0: error. skb freed, reference to sock dropped.
651 * 1: repeat lookup - reference dropped while waiting for socket memory.
653 int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
655 struct netlink_sock *nlk;
659 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
660 test_bit(0, &nlk->state)) {
661 DECLARE_WAITQUEUE(wait, current);
670 __set_current_state(TASK_INTERRUPTIBLE);
671 add_wait_queue(&nlk->wait, &wait);
673 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
674 test_bit(0, &nlk->state)) &&
675 !sock_flag(sk, SOCK_DEAD))
676 timeo = schedule_timeout(timeo);
678 __set_current_state(TASK_RUNNING);
679 remove_wait_queue(&nlk->wait, &wait);
682 if (signal_pending(current)) {
684 return sock_intr_errno(timeo);
688 skb_set_owner_r(skb, sk);
692 int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
694 struct netlink_sock *nlk;
699 skb_queue_tail(&sk->sk_receive_queue, skb);
700 sk->sk_data_ready(sk, len);
705 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
711 static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
712 unsigned int __nocast allocation)
718 delta = skb->end - skb->tail;
719 if (delta * 2 < skb->truesize)
722 if (skb_shared(skb)) {
723 struct sk_buff *nskb = skb_clone(skb, allocation);
730 if (!pskb_expand_head(skb, 0, -delta, allocation))
731 skb->truesize -= delta;
736 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
742 skb = netlink_trim(skb, gfp_any());
744 timeo = sock_sndtimeo(ssk, nonblock);
746 sk = netlink_getsockbypid(ssk, pid);
751 err = netlink_attachskb(sk, skb, nonblock, timeo);
757 return netlink_sendskb(sk, skb, ssk->sk_protocol);
760 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
762 struct netlink_sock *nlk = nlk_sk(sk);
764 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
765 !test_bit(0, &nlk->state)) {
766 skb_set_owner_r(skb, sk);
767 skb_queue_tail(&sk->sk_receive_queue, skb);
768 sk->sk_data_ready(sk, skb->len);
769 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
774 struct netlink_broadcast_data {
775 struct sock *exclude_sk;
781 unsigned int allocation;
782 struct sk_buff *skb, *skb2;
785 static inline int do_one_broadcast(struct sock *sk,
786 struct netlink_broadcast_data *p)
788 struct netlink_sock *nlk = nlk_sk(sk);
791 if (p->exclude_sk == sk)
794 if (nlk->pid == p->pid || !(nlk->groups & netlink_group_mask(p->group)))
803 if (p->skb2 == NULL) {
804 if (skb_shared(p->skb)) {
805 p->skb2 = skb_clone(p->skb, p->allocation);
807 p->skb2 = skb_get(p->skb);
809 * skb ownership may have been set when
810 * delivered to a previous socket.
815 if (p->skb2 == NULL) {
817 /* Clone failed. Notify ALL listeners. */
819 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
832 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
833 u32 group, int allocation)
835 struct netlink_broadcast_data info;
836 struct hlist_node *node;
839 skb = netlink_trim(skb, allocation);
841 info.exclude_sk = ssk;
847 info.allocation = allocation;
851 /* While we sleep in clone, do not allow to change socket list */
853 netlink_lock_table();
855 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
856 do_one_broadcast(sk, &info);
860 netlink_unlock_table();
863 kfree_skb(info.skb2);
865 if (info.delivered) {
866 if (info.congested && (allocation & __GFP_WAIT))
875 struct netlink_set_err_data {
876 struct sock *exclude_sk;
882 static inline int do_one_set_err(struct sock *sk,
883 struct netlink_set_err_data *p)
885 struct netlink_sock *nlk = nlk_sk(sk);
887 if (sk == p->exclude_sk)
890 if (nlk->pid == p->pid || !(nlk->groups & netlink_group_mask(p->group)))
893 sk->sk_err = p->code;
894 sk->sk_error_report(sk);
899 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
901 struct netlink_set_err_data info;
902 struct hlist_node *node;
905 info.exclude_sk = ssk;
910 read_lock(&nl_table_lock);
912 sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
913 do_one_set_err(sk, &info);
915 read_unlock(&nl_table_lock);
918 static inline void netlink_rcv_wake(struct sock *sk)
920 struct netlink_sock *nlk = nlk_sk(sk);
922 if (skb_queue_empty(&sk->sk_receive_queue))
923 clear_bit(0, &nlk->state);
924 if (!test_bit(0, &nlk->state))
925 wake_up_interruptible(&nlk->wait);
928 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
929 struct msghdr *msg, size_t len)
931 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
932 struct sock *sk = sock->sk;
933 struct netlink_sock *nlk = nlk_sk(sk);
934 struct sockaddr_nl *addr=msg->msg_name;
939 struct scm_cookie scm;
941 if (msg->msg_flags&MSG_OOB)
944 if (NULL == siocb->scm)
946 err = scm_send(sock, msg, siocb->scm);
950 if (msg->msg_namelen) {
951 if (addr->nl_family != AF_NETLINK)
953 dst_pid = addr->nl_pid;
954 dst_group = ffs(addr->nl_groups);
955 if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
958 dst_pid = nlk->dst_pid;
959 dst_group = nlk->dst_group;
963 err = netlink_autobind(sock);
969 if (len > sk->sk_sndbuf - 32)
972 skb = alloc_skb(len, GFP_KERNEL);
976 NETLINK_CB(skb).pid = nlk->pid;
977 NETLINK_CB(skb).dst_pid = dst_pid;
978 NETLINK_CB(skb).dst_group = dst_group;
979 NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
980 memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
982 /* What can I do? Netlink is asynchronous, so that
983 we will have to save current capabilities to
984 check them, when this message will be delivered
985 to corresponding kernel module. --ANK (980802)
989 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
994 err = security_netlink_send(sk, skb);
1001 atomic_inc(&skb->users);
1002 netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
1004 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
1010 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1011 struct msghdr *msg, size_t len,
1014 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1015 struct scm_cookie scm;
1016 struct sock *sk = sock->sk;
1017 struct netlink_sock *nlk = nlk_sk(sk);
1018 int noblock = flags&MSG_DONTWAIT;
1020 struct sk_buff *skb;
1028 skb = skb_recv_datagram(sk,flags,noblock,&err);
1032 msg->msg_namelen = 0;
1036 msg->msg_flags |= MSG_TRUNC;
1040 skb->h.raw = skb->data;
1041 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1043 if (msg->msg_name) {
1044 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
1045 addr->nl_family = AF_NETLINK;
1047 addr->nl_pid = NETLINK_CB(skb).pid;
1048 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1049 msg->msg_namelen = sizeof(*addr);
1052 if (NULL == siocb->scm) {
1053 memset(&scm, 0, sizeof(scm));
1056 siocb->scm->creds = *NETLINK_CREDS(skb);
1057 skb_free_datagram(sk, skb);
1059 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1062 scm_recv(sock, msg, siocb->scm, flags);
1065 netlink_rcv_wake(sk);
1066 return err ? : copied;
1069 static void netlink_data_ready(struct sock *sk, int len)
1071 struct netlink_sock *nlk = nlk_sk(sk);
1073 if (nlk->data_ready)
1074 nlk->data_ready(sk, len);
1075 netlink_rcv_wake(sk);
1079 * We export these functions to other modules. They provide a
1080 * complete set of kernel non-blocking support for message
1085 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len), struct module *module)
1087 struct socket *sock;
1089 struct netlink_sock *nlk;
1094 if (unit<0 || unit>=MAX_LINKS)
1097 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1100 if (__netlink_create(sock, unit) < 0)
1101 goto out_sock_release;
1104 sk->sk_data_ready = netlink_data_ready;
1106 nlk_sk(sk)->data_ready = input;
1108 if (netlink_insert(sk, 0))
1109 goto out_sock_release;
1112 nlk->flags |= NETLINK_KERNEL_SOCKET;
1114 netlink_table_grab();
1115 nl_table[unit].module = module;
1116 nl_table[unit].registered = 1;
1117 netlink_table_ungrab();
1126 void netlink_set_nonroot(int protocol, unsigned int flags)
1128 if ((unsigned int)protocol < MAX_LINKS)
1129 nl_table[protocol].nl_nonroot = flags;
1132 static void netlink_destroy_callback(struct netlink_callback *cb)
1140 * It looks a bit ugly.
1141 * It would be better to create kernel thread.
1144 static int netlink_dump(struct sock *sk)
1146 struct netlink_sock *nlk = nlk_sk(sk);
1147 struct netlink_callback *cb;
1148 struct sk_buff *skb;
1149 struct nlmsghdr *nlh;
1152 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1156 spin_lock(&nlk->cb_lock);
1160 spin_unlock(&nlk->cb_lock);
1165 len = cb->dump(skb, cb);
1168 spin_unlock(&nlk->cb_lock);
1169 skb_queue_tail(&sk->sk_receive_queue, skb);
1170 sk->sk_data_ready(sk, len);
1174 nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1175 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1176 skb_queue_tail(&sk->sk_receive_queue, skb);
1177 sk->sk_data_ready(sk, skb->len);
1181 spin_unlock(&nlk->cb_lock);
1183 netlink_destroy_callback(cb);
1190 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1191 struct nlmsghdr *nlh,
1192 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1193 int (*done)(struct netlink_callback*))
1195 struct netlink_callback *cb;
1197 struct netlink_sock *nlk;
1199 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1203 memset(cb, 0, sizeof(*cb));
1207 atomic_inc(&skb->users);
1210 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1212 netlink_destroy_callback(cb);
1213 return -ECONNREFUSED;
1216 /* A dump is in progress... */
1217 spin_lock(&nlk->cb_lock);
1219 spin_unlock(&nlk->cb_lock);
1220 netlink_destroy_callback(cb);
1225 spin_unlock(&nlk->cb_lock);
1232 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1234 struct sk_buff *skb;
1235 struct nlmsghdr *rep;
1236 struct nlmsgerr *errmsg;
1240 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1242 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1244 skb = alloc_skb(size, GFP_KERNEL);
1248 sk = netlink_lookup(in_skb->sk->sk_protocol,
1249 NETLINK_CB(in_skb).pid);
1251 sk->sk_err = ENOBUFS;
1252 sk->sk_error_report(sk);
1258 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1259 NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
1260 errmsg = NLMSG_DATA(rep);
1261 errmsg->error = err;
1262 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1263 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1267 #ifdef CONFIG_PROC_FS
1268 struct nl_seq_iter {
1273 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1275 struct nl_seq_iter *iter = seq->private;
1278 struct hlist_node *node;
1281 for (i=0; i<MAX_LINKS; i++) {
1282 struct nl_pid_hash *hash = &nl_table[i].hash;
1284 for (j = 0; j <= hash->mask; j++) {
1285 sk_for_each(s, node, &hash->table[j]) {
1298 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1300 read_lock(&nl_table_lock);
1301 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1304 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1307 struct nl_seq_iter *iter;
1312 if (v == SEQ_START_TOKEN)
1313 return netlink_seq_socket_idx(seq, 0);
1319 iter = seq->private;
1321 j = iter->hash_idx + 1;
1324 struct nl_pid_hash *hash = &nl_table[i].hash;
1326 for (; j <= hash->mask; j++) {
1327 s = sk_head(&hash->table[j]);
1336 } while (++i < MAX_LINKS);
1341 static void netlink_seq_stop(struct seq_file *seq, void *v)
1343 read_unlock(&nl_table_lock);
1347 static int netlink_seq_show(struct seq_file *seq, void *v)
1349 if (v == SEQ_START_TOKEN)
1351 "sk Eth Pid Groups "
1352 "Rmem Wmem Dump Locks\n");
1355 struct netlink_sock *nlk = nlk_sk(s);
1357 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1362 atomic_read(&s->sk_rmem_alloc),
1363 atomic_read(&s->sk_wmem_alloc),
1365 atomic_read(&s->sk_refcnt)
1372 static struct seq_operations netlink_seq_ops = {
1373 .start = netlink_seq_start,
1374 .next = netlink_seq_next,
1375 .stop = netlink_seq_stop,
1376 .show = netlink_seq_show,
1380 static int netlink_seq_open(struct inode *inode, struct file *file)
1382 struct seq_file *seq;
1383 struct nl_seq_iter *iter;
1386 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1390 err = seq_open(file, &netlink_seq_ops);
1396 memset(iter, 0, sizeof(*iter));
1397 seq = file->private_data;
1398 seq->private = iter;
1402 static struct file_operations netlink_seq_fops = {
1403 .owner = THIS_MODULE,
1404 .open = netlink_seq_open,
1406 .llseek = seq_lseek,
1407 .release = seq_release_private,
1412 int netlink_register_notifier(struct notifier_block *nb)
1414 return notifier_chain_register(&netlink_chain, nb);
1417 int netlink_unregister_notifier(struct notifier_block *nb)
1419 return notifier_chain_unregister(&netlink_chain, nb);
1422 static struct proto_ops netlink_ops = {
1423 .family = PF_NETLINK,
1424 .owner = THIS_MODULE,
1425 .release = netlink_release,
1426 .bind = netlink_bind,
1427 .connect = netlink_connect,
1428 .socketpair = sock_no_socketpair,
1429 .accept = sock_no_accept,
1430 .getname = netlink_getname,
1431 .poll = datagram_poll,
1432 .ioctl = sock_no_ioctl,
1433 .listen = sock_no_listen,
1434 .shutdown = sock_no_shutdown,
1435 .setsockopt = sock_no_setsockopt,
1436 .getsockopt = sock_no_getsockopt,
1437 .sendmsg = netlink_sendmsg,
1438 .recvmsg = netlink_recvmsg,
1439 .mmap = sock_no_mmap,
1440 .sendpage = sock_no_sendpage,
1443 static struct net_proto_family netlink_family_ops = {
1444 .family = PF_NETLINK,
1445 .create = netlink_create,
1446 .owner = THIS_MODULE, /* for consistency 8) */
1449 extern void netlink_skb_parms_too_large(void);
1451 static int __init netlink_proto_init(void)
1453 struct sk_buff *dummy_skb;
1457 int err = proto_register(&netlink_proto, 0);
1462 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1463 netlink_skb_parms_too_large();
1465 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1468 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1472 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1474 if (num_physpages >= (128 * 1024))
1475 max = num_physpages >> (21 - PAGE_SHIFT);
1477 max = num_physpages >> (23 - PAGE_SHIFT);
1479 order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1480 max = (1UL << order) / sizeof(struct hlist_head);
1481 order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1483 for (i = 0; i < MAX_LINKS; i++) {
1484 struct nl_pid_hash *hash = &nl_table[i].hash;
1486 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1489 nl_pid_hash_free(nl_table[i].hash.table,
1490 1 * sizeof(*hash->table));
1494 memset(hash->table, 0, 1 * sizeof(*hash->table));
1495 hash->max_shift = order;
1498 hash->rehash_time = jiffies;
1501 sock_register(&netlink_family_ops);
1502 #ifdef CONFIG_PROC_FS
1503 proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1505 /* The netlink device handler may be needed early. */
1511 core_initcall(netlink_proto_init);
1513 EXPORT_SYMBOL(netlink_ack);
1514 EXPORT_SYMBOL(netlink_broadcast);
1515 EXPORT_SYMBOL(netlink_dump_start);
1516 EXPORT_SYMBOL(netlink_kernel_create);
1517 EXPORT_SYMBOL(netlink_register_notifier);
1518 EXPORT_SYMBOL(netlink_set_err);
1519 EXPORT_SYMBOL(netlink_set_nonroot);
1520 EXPORT_SYMBOL(netlink_unicast);
1521 EXPORT_SYMBOL(netlink_unregister_notifier);