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
17 #include <linux/config.h>
18 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/major.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/stat.h>
28 #include <linux/socket.h>
30 #include <linux/fcntl.h>
31 #include <linux/termios.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
35 #include <linux/slab.h>
36 #include <asm/uaccess.h>
37 #include <linux/skbuff.h>
38 #include <linux/netdevice.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/proc_fs.h>
41 #include <linux/smp_lock.h>
42 #include <linux/notifier.h>
43 #include <linux/jhash.h>
44 #include <linux/random.h>
45 #include <linux/bitops.h>
47 #include <linux/types.h>
53 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
54 #define NL_EMULATE_DEV
62 unsigned int dst_groups;
64 int (*handler)(int unit, struct sk_buff *skb);
65 wait_queue_head_t wait;
66 struct netlink_callback *cb;
68 void (*data_ready)(struct sock *sk, int bytes);
73 unsigned long rehash_time;
79 unsigned int max_shift;
84 struct netlink_table {
85 struct nl_pid_hash hash;
89 #define nlk_sk(__sk) ((__sk)->protinfo.af_netlink)
91 static struct netlink_table *nl_table;
93 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
94 static unsigned int nl_nonroot[MAX_LINKS];
97 static struct socket *netlink_kernel[MAX_LINKS];
100 static int netlink_dump(struct sock *sk);
101 static void netlink_destroy_callback(struct netlink_callback *cb);
103 atomic_t netlink_sock_nr;
105 static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;
106 static atomic_t nl_table_users = ATOMIC_INIT(0);
108 static struct notifier_block *netlink_chain;
110 static struct sock **nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
112 return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
115 static void netlink_sock_destruct(struct sock *sk)
117 skb_queue_purge(&sk->receive_queue);
120 printk("Freeing alive netlink socket %p\n", sk);
123 BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
124 BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
125 BUG_TRAP(sk->protinfo.af_netlink->cb==NULL);
127 kfree(sk->protinfo.af_netlink);
129 atomic_dec(&netlink_sock_nr);
130 #ifdef NETLINK_REFCNT_DEBUG
131 printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));
135 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
136 * Look, when several writers sleep and reader wakes them up, all but one
137 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
138 * this, _but_ remember, it adds useless work on UP machines.
141 static void netlink_table_grab(void)
143 write_lock_bh(&nl_table_lock);
145 if (atomic_read(&nl_table_users)) {
146 DECLARE_WAITQUEUE(wait, current);
148 add_wait_queue_exclusive(&nl_table_wait, &wait);
150 set_current_state(TASK_UNINTERRUPTIBLE);
151 if (atomic_read(&nl_table_users) == 0)
153 write_unlock_bh(&nl_table_lock);
155 write_lock_bh(&nl_table_lock);
158 __set_current_state(TASK_RUNNING);
159 remove_wait_queue(&nl_table_wait, &wait);
163 static __inline__ void netlink_table_ungrab(void)
165 write_unlock_bh(&nl_table_lock);
166 wake_up(&nl_table_wait);
169 static __inline__ void
170 netlink_lock_table(void)
172 /* read_lock() synchronizes us to netlink_table_grab */
174 read_lock(&nl_table_lock);
175 atomic_inc(&nl_table_users);
176 read_unlock(&nl_table_lock);
179 static __inline__ void
180 netlink_unlock_table(void)
182 if (atomic_dec_and_test(&nl_table_users))
183 wake_up(&nl_table_wait);
186 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
188 struct nl_pid_hash *hash = &nl_table[protocol].hash;
191 read_lock(&nl_table_lock);
192 for (sk = *nl_pid_hashfn(hash, pid); sk; sk = sk->next) {
193 if (sk->protinfo.af_netlink->pid == pid) {
195 read_unlock(&nl_table_lock);
200 read_unlock(&nl_table_lock);
204 static inline struct sock **nl_pid_hash_alloc(size_t size)
206 if (size <= PAGE_SIZE)
207 return kmalloc(size, GFP_ATOMIC);
209 return (struct sock **)
210 __get_free_pages(GFP_ATOMIC, get_order(size));
213 static inline void nl_pid_hash_free(struct sock **table, size_t size)
215 if (size <= PAGE_SIZE)
218 free_pages((unsigned long)table, get_order(size));
221 static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
223 unsigned int omask, mask, shift;
225 struct sock **otable, **table;
228 omask = mask = hash->mask;
229 osize = size = (mask + 1) * sizeof(*table);
233 if (++shift > hash->max_shift)
239 table = nl_pid_hash_alloc(size);
243 memset(table, 0, size);
244 otable = hash->table;
248 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
250 for (i = 0; i <= omask; i++) {
252 struct sock *tmp, **head;
254 for (sk = otable[i]; sk; sk = tmp) {
256 head = nl_pid_hashfn(hash, nlk_sk(sk)->pid);
262 nl_pid_hash_free(otable, osize);
263 hash->rehash_time = jiffies + 10 * 60 * HZ;
267 static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
269 int avg = hash->entries >> hash->shift;
271 if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
274 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
275 nl_pid_hash_rehash(hash, 0);
282 extern struct proto_ops netlink_ops;
284 static int netlink_insert(struct sock *sk, u32 pid)
286 struct nl_pid_hash *hash = &nl_table[sk->protocol].hash;
288 int err = -EADDRINUSE;
292 netlink_table_grab();
293 head = nl_pid_hashfn(hash, pid);
295 for (osk = *head; osk; osk = osk->next) {
296 if (osk->protinfo.af_netlink->pid == pid)
308 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
311 if (len && nl_pid_hash_dilute(hash, len))
312 head = nl_pid_hashfn(hash, pid);
314 nlk_sk(sk)->pid = pid;
321 netlink_table_ungrab();
325 static void netlink_remove(struct sock *sk)
328 struct netlink_table *table = &nl_table[sk->protocol];
329 struct nl_pid_hash *hash = &table->hash;
330 u32 pid = nlk_sk(sk)->pid;
332 netlink_table_grab();
333 for (skp = nl_pid_hashfn(hash, pid); *skp; skp = &((*skp)->next)) {
341 if (!nlk_sk(sk)->groups)
343 for (skp = &table->mc_list; *skp; skp = &((*skp)->bind_next)) {
345 *skp = sk->bind_next;
350 netlink_table_ungrab();
353 static int netlink_create(struct socket *sock, int protocol)
357 sock->state = SS_UNCONNECTED;
359 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
360 return -ESOCKTNOSUPPORT;
362 if (protocol<0 || protocol >= MAX_LINKS)
363 return -EPROTONOSUPPORT;
365 sock->ops = &netlink_ops;
367 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1);
371 sock_init_data(sock,sk);
373 sk->protinfo.af_netlink = kmalloc(sizeof(struct netlink_opt), GFP_KERNEL);
374 if (sk->protinfo.af_netlink == NULL) {
378 memset(sk->protinfo.af_netlink, 0, sizeof(struct netlink_opt));
380 spin_lock_init(&sk->protinfo.af_netlink->cb_lock);
381 init_waitqueue_head(&sk->protinfo.af_netlink->wait);
382 sk->destruct = netlink_sock_destruct;
383 atomic_inc(&netlink_sock_nr);
385 sk->protocol=protocol;
389 static int netlink_release(struct socket *sock)
391 struct sock *sk = sock->sk;
398 spin_lock(&sk->protinfo.af_netlink->cb_lock);
399 if (sk->protinfo.af_netlink->cb) {
400 sk->protinfo.af_netlink->cb->done(sk->protinfo.af_netlink->cb);
401 netlink_destroy_callback(sk->protinfo.af_netlink->cb);
402 sk->protinfo.af_netlink->cb = NULL;
405 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
407 /* OK. Socket is unlinked, and, therefore,
408 no new packets will arrive */
412 wake_up_interruptible_all(&sk->protinfo.af_netlink->wait);
414 skb_queue_purge(&sk->write_queue);
416 if (sk->protinfo.af_netlink->pid && !sk->protinfo.af_netlink->groups) {
417 struct netlink_notify n = { protocol:sk->protocol,
418 pid:sk->protinfo.af_netlink->pid };
419 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
426 static int netlink_autobind(struct socket *sock)
428 struct sock *sk = sock->sk;
429 struct nl_pid_hash *hash = &nl_table[sk->protocol].hash;
431 s32 pid = current->pid;
433 static s32 rover = -4097;
437 netlink_table_grab();
438 for (osk = *nl_pid_hashfn(hash, pid); osk; osk = osk->next) {
439 if (osk->protinfo.af_netlink->pid == pid) {
440 /* Bind collision, search negative pid values. */
444 netlink_table_ungrab();
448 netlink_table_ungrab();
450 err = netlink_insert(sk, pid);
451 if (err == -EADDRINUSE)
454 /* If 2 threads race to autobind, that is fine. */
461 static inline int netlink_capable(struct socket *sock, unsigned int flag)
463 return (nl_nonroot[sock->sk->protocol] & flag) || capable(CAP_NET_ADMIN);
466 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
468 struct sock *sk = sock->sk;
471 struct netlink_opt *nlk = nlk_sk(sk);
472 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
474 if (nladdr->nl_family != AF_NETLINK)
477 /* Only superuser is allowed to listen multicasts */
478 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
481 if (sk->protinfo.af_netlink->pid) {
482 if (nladdr->nl_pid != sk->protinfo.af_netlink->pid)
485 err = nladdr->nl_pid ?
486 netlink_insert(sk, nladdr->nl_pid) :
487 netlink_autobind(sock);
492 if (!nladdr->nl_groups && !nlk->groups)
495 netlink_table_grab();
496 skp = &nl_table[sk->protocol].mc_list;
497 if (nlk->groups && !nladdr->nl_groups) {
498 for (; *skp; skp = &((*skp)->bind_next)) {
500 *skp = sk->bind_next;
504 } else if (!nlk->groups && nladdr->nl_groups) {
505 sk->bind_next = *skp;
508 nlk->groups = nladdr->nl_groups;
509 netlink_table_ungrab();
514 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
518 struct sock *sk = sock->sk;
519 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
521 if (addr->sa_family == AF_UNSPEC) {
522 sk->protinfo.af_netlink->dst_pid = 0;
523 sk->protinfo.af_netlink->dst_groups = 0;
526 if (addr->sa_family != AF_NETLINK)
529 /* Only superuser is allowed to send multicasts */
530 if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
533 if (!sk->protinfo.af_netlink->pid)
534 err = netlink_autobind(sock);
537 sk->protinfo.af_netlink->dst_pid = nladdr->nl_pid;
538 sk->protinfo.af_netlink->dst_groups = nladdr->nl_groups;
544 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
546 struct sock *sk = sock->sk;
547 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
549 nladdr->nl_family = AF_NETLINK;
551 *addr_len = sizeof(*nladdr);
554 nladdr->nl_pid = sk->protinfo.af_netlink->dst_pid;
555 nladdr->nl_groups = sk->protinfo.af_netlink->dst_groups;
557 nladdr->nl_pid = sk->protinfo.af_netlink->pid;
558 nladdr->nl_groups = sk->protinfo.af_netlink->groups;
563 static void netlink_overrun(struct sock *sk)
565 if (!test_and_set_bit(0, &sk->protinfo.af_netlink->state)) {
567 sk->error_report(sk);
571 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
575 int protocol = ssk->protocol;
577 DECLARE_WAITQUEUE(wait, current);
579 timeo = sock_sndtimeo(ssk, nonblock);
582 sk = netlink_lookup(protocol, pid);
586 /* Don't bother queuing skb if kernel socket has no input function */
587 if (sk->protinfo.af_netlink->pid == 0 &&
588 !sk->protinfo.af_netlink->data_ready)
591 #ifdef NL_EMULATE_DEV
592 if (sk->protinfo.af_netlink->handler) {
594 len = sk->protinfo.af_netlink->handler(protocol, skb);
600 if (atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
601 test_bit(0, &sk->protinfo.af_netlink->state)) {
603 if (ssk->protinfo.af_netlink->pid == 0)
610 __set_current_state(TASK_INTERRUPTIBLE);
611 add_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
613 if ((atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
614 test_bit(0, &sk->protinfo.af_netlink->state)) &&
616 timeo = schedule_timeout(timeo);
618 __set_current_state(TASK_RUNNING);
619 remove_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
622 if (signal_pending(current)) {
624 return sock_intr_errno(timeo);
630 skb_set_owner_r(skb, sk);
631 skb_queue_tail(&sk->receive_queue, skb);
632 sk->data_ready(sk, len);
638 return -ECONNREFUSED;
641 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
643 #ifdef NL_EMULATE_DEV
644 if (sk->protinfo.af_netlink->handler) {
646 sk->protinfo.af_netlink->handler(sk->protocol, skb);
650 if (atomic_read(&sk->rmem_alloc) <= sk->rcvbuf &&
651 !test_bit(0, &sk->protinfo.af_netlink->state)) {
653 skb_set_owner_r(skb, sk);
654 skb_queue_tail(&sk->receive_queue, skb);
655 sk->data_ready(sk, skb->len);
661 struct netlink_broadcast_data {
662 struct sock *exclude_sk;
667 struct sk_buff *skb, *skb2;
670 static inline int do_one_broadcast(struct sock *sk,
671 struct netlink_broadcast_data *p)
673 struct netlink_opt *nlk = nlk_sk(sk);
676 if (p->exclude_sk == sk)
679 if (nlk->pid == p->pid || !(nlk->groups & p->group))
688 if (p->skb2 == NULL) {
689 if (atomic_read(&p->skb->users) != 1) {
690 p->skb2 = skb_clone(p->skb, p->allocation);
693 atomic_inc(&p->skb->users);
696 if (p->skb2 == NULL) {
698 /* Clone failed. Notify ALL listeners. */
700 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
710 void netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
711 u32 group, int allocation)
713 struct netlink_broadcast_data info;
716 info.exclude_sk = ssk;
720 info.allocation = allocation;
724 /* While we sleep in clone, do not allow to change socket list */
726 netlink_lock_table();
728 for (sk = nl_table[ssk->protocol].mc_list; sk; sk = sk->bind_next)
729 do_one_broadcast(sk, &info);
731 netlink_unlock_table();
734 kfree_skb(info.skb2);
738 struct netlink_set_err_data {
739 struct sock *exclude_sk;
745 static inline int do_one_set_err(struct sock *sk,
746 struct netlink_set_err_data *p)
748 struct netlink_opt *nlk = nlk_sk(sk);
750 if (sk == p->exclude_sk)
753 if (nlk->pid == p->pid || !(nlk->groups & p->group))
757 sk->error_report(sk);
762 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
764 struct netlink_set_err_data info;
767 info.exclude_sk = ssk;
772 read_lock(&nl_table_lock);
773 for (sk = nl_table[ssk->protocol].mc_list; sk; sk = sk->bind_next)
774 do_one_set_err(sk, &info);
775 read_unlock(&nl_table_lock);
778 static inline void netlink_rcv_wake(struct sock *sk)
780 if (skb_queue_len(&sk->receive_queue) == 0)
781 clear_bit(0, &sk->protinfo.af_netlink->state);
782 if (!test_bit(0, &sk->protinfo.af_netlink->state))
783 wake_up_interruptible(&sk->protinfo.af_netlink->wait);
786 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, int len,
787 struct scm_cookie *scm)
789 struct sock *sk = sock->sk;
790 struct sockaddr_nl *addr=msg->msg_name;
796 if (msg->msg_flags&MSG_OOB)
799 if (msg->msg_namelen) {
800 if (addr->nl_family != AF_NETLINK)
802 dst_pid = addr->nl_pid;
803 dst_groups = addr->nl_groups;
804 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
807 dst_pid = sk->protinfo.af_netlink->dst_pid;
808 dst_groups = sk->protinfo.af_netlink->dst_groups;
811 if (!sk->protinfo.af_netlink->pid) {
812 err = netlink_autobind(sock);
818 if ((unsigned)len > sk->sndbuf-32)
821 skb = alloc_skb(len, GFP_KERNEL);
825 NETLINK_CB(skb).pid = sk->protinfo.af_netlink->pid;
826 NETLINK_CB(skb).groups = sk->protinfo.af_netlink->groups;
827 NETLINK_CB(skb).dst_pid = dst_pid;
828 NETLINK_CB(skb).dst_groups = dst_groups;
829 memcpy(NETLINK_CREDS(skb), &scm->creds, sizeof(struct ucred));
831 /* What can I do? Netlink is asynchronous, so that
832 we will have to save current capabilities to
833 check them, when this message will be delivered
834 to corresponding kernel module. --ANK (980802)
836 NETLINK_CB(skb).eff_cap = current->cap_effective;
839 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
845 atomic_inc(&skb->users);
846 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
848 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
854 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, int len,
855 int flags, struct scm_cookie *scm)
857 struct sock *sk = sock->sk;
858 int noblock = flags&MSG_DONTWAIT;
868 skb = skb_recv_datagram(sk,flags,noblock,&err);
872 msg->msg_namelen = 0;
876 msg->msg_flags |= MSG_TRUNC;
880 skb->h.raw = skb->data;
881 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
884 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
885 addr->nl_family = AF_NETLINK;
887 addr->nl_pid = NETLINK_CB(skb).pid;
888 addr->nl_groups = NETLINK_CB(skb).dst_groups;
889 msg->msg_namelen = sizeof(*addr);
892 scm->creds = *NETLINK_CREDS(skb);
893 skb_free_datagram(sk, skb);
895 if (sk->protinfo.af_netlink->cb
896 && atomic_read(&sk->rmem_alloc) <= sk->rcvbuf/2)
900 netlink_rcv_wake(sk);
901 return err ? : copied;
904 void netlink_data_ready(struct sock *sk, int len)
906 if (sk->protinfo.af_netlink->data_ready)
907 sk->protinfo.af_netlink->data_ready(sk, len);
908 netlink_rcv_wake(sk);
912 * We export these functions to other modules. They provide a
913 * complete set of kernel non-blocking support for message
918 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
926 if (unit<0 || unit>=MAX_LINKS)
929 if (!(sock = sock_alloc()))
932 sock->type = SOCK_RAW;
934 if (netlink_create(sock, unit) < 0) {
939 sk->data_ready = netlink_data_ready;
941 sk->protinfo.af_netlink->data_ready = input;
943 netlink_insert(sk, 0);
947 void netlink_set_nonroot(int protocol, unsigned int flags)
949 if ((unsigned int)protocol < MAX_LINKS)
950 nl_nonroot[protocol] = flags;
953 static void netlink_destroy_callback(struct netlink_callback *cb)
961 * It looks a bit ugly.
962 * It would be better to create kernel thread.
965 static int netlink_dump(struct sock *sk)
967 struct netlink_callback *cb;
969 struct nlmsghdr *nlh;
972 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
976 spin_lock(&sk->protinfo.af_netlink->cb_lock);
978 cb = sk->protinfo.af_netlink->cb;
980 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
985 len = cb->dump(skb, cb);
989 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
990 skb_queue_tail(&sk->receive_queue, skb);
991 sk->data_ready(sk, len);
996 nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
997 nlh->nlmsg_flags |= NLM_F_MULTI;
998 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
999 skb_queue_tail(&sk->receive_queue, skb);
1000 sk->data_ready(sk, skb->len);
1003 sk->protinfo.af_netlink->cb = NULL;
1004 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
1006 netlink_destroy_callback(cb);
1011 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1012 struct nlmsghdr *nlh,
1013 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1014 int (*done)(struct netlink_callback*))
1016 struct netlink_callback *cb;
1019 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1023 memset(cb, 0, sizeof(*cb));
1027 atomic_inc(&skb->users);
1030 sk = netlink_lookup(ssk->protocol, NETLINK_CB(skb).pid);
1032 netlink_destroy_callback(cb);
1033 return -ECONNREFUSED;
1035 /* A dump is in progress... */
1036 spin_lock(&sk->protinfo.af_netlink->cb_lock);
1037 if (sk->protinfo.af_netlink->cb) {
1038 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
1039 netlink_destroy_callback(cb);
1043 sk->protinfo.af_netlink->cb = cb;
1044 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
1050 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1052 struct sk_buff *skb;
1053 struct nlmsghdr *rep;
1054 struct nlmsgerr *errmsg;
1058 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1060 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1062 skb = alloc_skb(size, GFP_KERNEL);
1066 sk = netlink_lookup(in_skb->sk->protocol,
1067 NETLINK_CB(in_skb).pid);
1070 sk->error_report(sk);
1075 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1076 NLMSG_ERROR, sizeof(struct nlmsgerr));
1077 errmsg = NLMSG_DATA(rep);
1078 errmsg->error = err;
1079 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1080 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1084 #ifdef NL_EMULATE_DEV
1086 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
1089 * Backward compatibility.
1092 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
1094 struct sock *sk = netlink_kernel_create(unit, NULL);
1097 sk->protinfo.af_netlink->handler = function;
1098 write_lock_bh(&nl_emu_lock);
1099 netlink_kernel[unit] = sk->socket;
1100 write_unlock_bh(&nl_emu_lock);
1104 void netlink_detach(int unit)
1106 struct socket *sock;
1108 write_lock_bh(&nl_emu_lock);
1109 sock = netlink_kernel[unit];
1110 netlink_kernel[unit] = NULL;
1111 write_unlock_bh(&nl_emu_lock);
1116 int netlink_post(int unit, struct sk_buff *skb)
1118 struct socket *sock;
1120 read_lock(&nl_emu_lock);
1121 sock = netlink_kernel[unit];
1123 struct sock *sk = sock->sk;
1124 memset(skb->cb, 0, sizeof(skb->cb));
1126 read_unlock(&nl_emu_lock);
1128 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
1133 read_unlock(&nl_emu_lock);
1140 #ifdef CONFIG_PROC_FS
1141 struct nl_seq_iter {
1146 static int netlink_read_proc(char *buffer, char **start, off_t offset,
1147 int length, int *eof, void *data)
1155 len+= sprintf(buffer,"sk Eth Pid Groups "
1156 "Rmem Wmem Dump Locks\n");
1158 for (i=0; i<MAX_LINKS; i++) {
1159 struct nl_pid_hash *hash = &nl_table[i].hash;
1161 read_lock(&nl_table_lock);
1162 for (j = 0; j <= hash->mask; j++) {
1163 for (s = hash->table[j]; s; s = s->next) {
1164 len += sprintf(buffer + len,
1165 "%p %-3d %-6d %08x %-8d %-8d %p %d",
1168 s->protinfo.af_netlink->pid,
1169 s->protinfo.af_netlink->groups,
1170 atomic_read(&s->rmem_alloc),
1171 atomic_read(&s->wmem_alloc),
1172 s->protinfo.af_netlink->cb,
1173 atomic_read(&s->refcnt));
1182 if (pos > offset + length) {
1183 read_unlock(&nl_table_lock);
1188 read_unlock(&nl_table_lock);
1193 *start=buffer+(offset-begin);
1194 len-=(offset-begin);
1203 int netlink_register_notifier(struct notifier_block *nb)
1205 return notifier_chain_register(&netlink_chain, nb);
1208 int netlink_unregister_notifier(struct notifier_block *nb)
1210 return notifier_chain_unregister(&netlink_chain, nb);
1213 struct proto_ops netlink_ops = {
1216 release: netlink_release,
1218 connect: netlink_connect,
1219 socketpair: sock_no_socketpair,
1220 accept: sock_no_accept,
1221 getname: netlink_getname,
1222 poll: datagram_poll,
1223 ioctl: sock_no_ioctl,
1224 listen: sock_no_listen,
1225 shutdown: sock_no_shutdown,
1226 setsockopt: sock_no_setsockopt,
1227 getsockopt: sock_no_getsockopt,
1228 sendmsg: netlink_sendmsg,
1229 recvmsg: netlink_recvmsg,
1231 sendpage: sock_no_sendpage,
1234 struct net_proto_family netlink_family_ops = {
1239 extern void netlink_skb_parms_too_large(void);
1241 int __init netlink_proto_init(void)
1243 struct sk_buff *dummy_skb;
1248 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1249 netlink_skb_parms_too_large();
1251 nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1254 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1258 memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1260 if (num_physpages >= (128 * 1024))
1261 max = num_physpages >> (21 - PAGE_SHIFT);
1263 max = num_physpages >> (23 - PAGE_SHIFT);
1265 for (order = 0; (1UL << order) < max + 1; order++)
1267 order += PAGE_SHIFT - 1;
1268 max = (1UL << order) / sizeof(struct sock *);
1271 for (order = 0; (1UL << order) < max + 1; order++)
1275 for (i = 0; i < MAX_LINKS; i++) {
1276 struct nl_pid_hash *hash = &nl_table[i].hash;
1278 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1281 nl_pid_hash_free(nl_table[i].hash.table,
1282 1 * sizeof(*hash->table));
1286 memset(hash->table, 0, 1 * sizeof(*hash->table));
1287 hash->max_shift = order;
1290 hash->rehash_time = jiffies;
1293 sock_register(&netlink_family_ops);
1294 #ifdef CONFIG_PROC_FS
1295 create_proc_read_entry("net/netlink", 0, 0, netlink_read_proc, NULL);
1300 static void __exit netlink_proto_exit(void)
1302 sock_unregister(PF_NETLINK);
1303 remove_proc_entry("net/netlink", NULL);
1309 module_init(netlink_proto_init);
1311 module_exit(netlink_proto_exit);