Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / net / netlink / af_netlink.c
bloba8218d679558dc2aecb31b3519343911d5bf6383
1 /*
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.
14 #include <linux/config.h>
15 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/major.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/socket.h>
26 #include <linux/un.h>
27 #include <linux/fcntl.h>
28 #include <linux/termios.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/fs.h>
32 #include <linux/malloc.h>
33 #include <asm/uaccess.h>
34 #include <linux/skbuff.h>
35 #include <linux/netdevice.h>
36 #include <linux/netlink.h>
37 #include <linux/proc_fs.h>
38 #include <linux/smp_lock.h>
39 #include <net/sock.h>
40 #include <net/scm.h>
42 #define Nprintk(a...)
44 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
45 #define NL_EMULATE_DEV
46 #endif
48 #define BUG_TRAP(x) if (!(x)) { printk("Assertion (" #x ") failed at " __FILE__ "(%d):" __FUNCTION__ "\n", __LINE__); }
50 struct netlink_opt
52 u32 pid;
53 unsigned groups;
54 u32 dst_pid;
55 unsigned dst_groups;
56 unsigned long state;
57 int (*handler)(int unit, struct sk_buff *skb);
58 wait_queue_head_t wait;
59 struct netlink_callback *cb;
60 spinlock_t cb_lock;
61 void (*data_ready)(struct sock *sk, int bytes);
64 static struct sock *nl_table[MAX_LINKS];
65 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
67 #ifdef NL_EMULATE_DEV
68 static struct socket *netlink_kernel[MAX_LINKS];
69 #endif
71 static int netlink_dump(struct sock *sk);
72 static void netlink_destroy_callback(struct netlink_callback *cb);
74 atomic_t netlink_sock_nr;
76 static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;
77 static atomic_t nl_table_users = ATOMIC_INIT(0);
79 static void netlink_sock_destruct(struct sock *sk)
81 skb_queue_purge(&sk->receive_queue);
83 if (!sk->dead) {
84 printk("Freeing alive netlink socket %p\n", sk);
85 return;
87 BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
88 BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
89 BUG_TRAP(sk->protinfo.af_netlink->cb==NULL);
91 kfree(sk->protinfo.af_netlink);
93 atomic_dec(&netlink_sock_nr);
94 #ifdef NETLINK_REFCNT_DEBUG
95 printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));
96 #endif
99 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
100 * Look, when several writers sleep and reader wakes them up, all but one
101 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
102 * this, _but_ remember, it adds useless work on UP machines.
105 static void netlink_table_grab(void)
107 write_lock_bh(&nl_table_lock);
109 if (atomic_read(&nl_table_users)) {
110 DECLARE_WAITQUEUE(wait, current);
112 add_wait_queue_exclusive(&nl_table_wait, &wait);
113 for(;;) {
114 set_current_state(TASK_UNINTERRUPTIBLE);
115 if (atomic_read(&nl_table_users) == 0)
116 break;
117 write_unlock_bh(&nl_table_lock);
118 schedule();
119 write_lock_bh(&nl_table_lock);
122 __set_current_state(TASK_RUNNING);
123 remove_wait_queue(&nl_table_wait, &wait);
127 static __inline__ void netlink_table_ungrab(void)
129 write_unlock_bh(&nl_table_lock);
130 wake_up(&nl_table_wait);
133 static __inline__ void
134 netlink_lock_table(void)
136 /* read_lock() synchronizes us to netlink_table_grab */
138 read_lock(&nl_table_lock);
139 atomic_inc(&nl_table_users);
140 read_unlock(&nl_table_lock);
143 static __inline__ void
144 netlink_unlock_table(void)
146 if (atomic_dec_and_test(&nl_table_users))
147 wake_up(&nl_table_wait);
150 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
152 struct sock *sk;
154 read_lock(&nl_table_lock);
155 for (sk=nl_table[protocol]; sk; sk=sk->next) {
156 if (sk->protinfo.af_netlink->pid == pid) {
157 sock_hold(sk);
158 read_unlock(&nl_table_lock);
159 return sk;
163 read_unlock(&nl_table_lock);
164 return NULL;
167 extern struct proto_ops netlink_ops;
169 static int netlink_insert(struct sock *sk, u32 pid)
171 int err = -EADDRINUSE;
172 struct sock *osk;
174 netlink_table_grab();
175 for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
176 if (osk->protinfo.af_netlink->pid == pid)
177 break;
179 if (osk == NULL) {
180 err = -EBUSY;
181 if (sk->protinfo.af_netlink->pid == 0) {
182 sk->protinfo.af_netlink->pid = pid;
183 sk->next = nl_table[sk->protocol];
184 nl_table[sk->protocol] = sk;
185 sock_hold(sk);
186 err = 0;
189 netlink_table_ungrab();
190 return err;
193 static void netlink_remove(struct sock *sk)
195 struct sock **skp;
197 netlink_table_grab();
198 for (skp = &nl_table[sk->protocol]; *skp; skp = &((*skp)->next)) {
199 if (*skp == sk) {
200 *skp = sk->next;
201 __sock_put(sk);
202 break;
205 netlink_table_ungrab();
208 static int netlink_create(struct socket *sock, int protocol)
210 struct sock *sk;
212 sock->state = SS_UNCONNECTED;
214 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
215 return -ESOCKTNOSUPPORT;
217 if (protocol<0 || protocol >= MAX_LINKS)
218 return -EPROTONOSUPPORT;
220 sock->ops = &netlink_ops;
222 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1);
223 if (!sk)
224 return -ENOMEM;
226 sock_init_data(sock,sk);
228 sk->protinfo.af_netlink = kmalloc(sizeof(struct netlink_opt), GFP_KERNEL);
229 if (sk->protinfo.af_netlink == NULL) {
230 sk_free(sk);
231 return -ENOMEM;
233 memset(sk->protinfo.af_netlink, 0, sizeof(struct netlink_opt));
235 spin_lock_init(&sk->protinfo.af_netlink->cb_lock);
236 init_waitqueue_head(&sk->protinfo.af_netlink->wait);
237 sk->destruct = netlink_sock_destruct;
238 atomic_inc(&netlink_sock_nr);
240 sk->protocol=protocol;
241 return 0;
244 static int netlink_release(struct socket *sock)
246 struct sock *sk = sock->sk;
248 if (!sk)
249 return 0;
251 netlink_remove(sk);
253 spin_lock(&sk->protinfo.af_netlink->cb_lock);
254 if (sk->protinfo.af_netlink->cb) {
255 sk->protinfo.af_netlink->cb->done(sk->protinfo.af_netlink->cb);
256 netlink_destroy_callback(sk->protinfo.af_netlink->cb);
257 sk->protinfo.af_netlink->cb = NULL;
258 __sock_put(sk);
260 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
262 /* OK. Socket is unlinked, and, therefore,
263 no new packets will arrive */
265 sock_orphan(sk);
266 sock->sk = NULL;
267 wake_up_interruptible_all(&sk->protinfo.af_netlink->wait);
269 skb_queue_purge(&sk->write_queue);
271 sock_put(sk);
272 return 0;
275 static int netlink_autobind(struct socket *sock)
277 struct sock *sk = sock->sk;
278 struct sock *osk;
279 s32 pid = current->pid;
280 int err;
282 retry:
283 netlink_table_grab();
284 for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
285 if (osk->protinfo.af_netlink->pid == pid) {
286 /* Bind collision, search negative pid values. */
287 if (pid > 0)
288 pid = -4096;
289 pid--;
290 netlink_table_ungrab();
291 goto retry;
294 netlink_table_ungrab();
296 err = netlink_insert(sk, pid);
297 if (err == -EADDRINUSE)
298 goto retry;
299 sk->protinfo.af_netlink->groups = 0;
300 return 0;
303 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
305 struct sock *sk = sock->sk;
306 int err;
307 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
309 if (nladdr->nl_family != AF_NETLINK)
310 return -EINVAL;
312 /* Only superuser is allowed to listen multicasts */
313 if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
314 return -EPERM;
316 if (sk->protinfo.af_netlink->pid) {
317 if (nladdr->nl_pid != sk->protinfo.af_netlink->pid)
318 return -EINVAL;
319 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
320 return 0;
323 if (nladdr->nl_pid == 0) {
324 err = netlink_autobind(sock);
325 if (err == 0)
326 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
327 return err;
330 err = netlink_insert(sk, nladdr->nl_pid);
331 if (err == 0)
332 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
333 return err;
336 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
337 int alen, int flags)
339 int err = 0;
340 struct sock *sk = sock->sk;
341 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
343 if (addr->sa_family == AF_UNSPEC) {
344 sk->protinfo.af_netlink->dst_pid = 0;
345 sk->protinfo.af_netlink->dst_groups = 0;
346 return 0;
348 if (addr->sa_family != AF_NETLINK)
349 return -EINVAL;
351 /* Only superuser is allowed to send multicasts */
352 if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
353 return -EPERM;
355 if (!sk->protinfo.af_netlink->pid)
356 err = netlink_autobind(sock);
358 if (err == 0) {
359 sk->protinfo.af_netlink->dst_pid = nladdr->nl_pid;
360 sk->protinfo.af_netlink->dst_groups = nladdr->nl_groups;
363 return 0;
366 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
368 struct sock *sk = sock->sk;
369 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
371 nladdr->nl_family = AF_NETLINK;
372 *addr_len = sizeof(*nladdr);
374 if (peer) {
375 nladdr->nl_pid = sk->protinfo.af_netlink->dst_pid;
376 nladdr->nl_groups = sk->protinfo.af_netlink->dst_groups;
377 } else {
378 nladdr->nl_pid = sk->protinfo.af_netlink->pid;
379 nladdr->nl_groups = sk->protinfo.af_netlink->groups;
381 return 0;
384 static void netlink_overrun(struct sock *sk)
386 if (!test_and_set_bit(0, &sk->protinfo.af_netlink->state)) {
387 sk->err = ENOBUFS;
388 sk->error_report(sk);
392 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
394 struct sock *sk;
395 int len = skb->len;
396 int protocol = ssk->protocol;
397 long timeo;
398 DECLARE_WAITQUEUE(wait, current);
400 timeo = sock_sndtimeo(ssk, nonblock);
402 retry:
403 sk = netlink_lookup(protocol, pid);
404 if (sk == NULL)
405 goto no_dst;
407 #ifdef NL_EMULATE_DEV
408 if (sk->protinfo.af_netlink->handler) {
409 skb_orphan(skb);
410 len = sk->protinfo.af_netlink->handler(protocol, skb);
411 sock_put(sk);
412 return len;
414 #endif
416 if (atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
417 test_bit(0, &sk->protinfo.af_netlink->state)) {
418 if (!timeo) {
419 if (ssk->protinfo.af_netlink->pid == 0)
420 netlink_overrun(sk);
421 sock_put(sk);
422 kfree_skb(skb);
423 return -EAGAIN;
426 __set_current_state(TASK_INTERRUPTIBLE);
427 add_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
429 if ((atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
430 test_bit(0, &sk->protinfo.af_netlink->state)) &&
431 !sk->dead)
432 timeo = schedule_timeout(timeo);
434 __set_current_state(TASK_RUNNING);
435 remove_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
436 sock_put(sk);
438 if (signal_pending(current)) {
439 kfree_skb(skb);
440 return sock_intr_errno(timeo);
442 goto retry;
445 skb_orphan(skb);
446 skb_set_owner_r(skb, sk);
447 skb_queue_tail(&sk->receive_queue, skb);
448 sk->data_ready(sk, len);
449 sock_put(sk);
450 return len;
452 no_dst:
453 kfree_skb(skb);
454 return -ECONNREFUSED;
457 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
459 #ifdef NL_EMULATE_DEV
460 if (sk->protinfo.af_netlink->handler) {
461 skb_orphan(skb);
462 sk->protinfo.af_netlink->handler(sk->protocol, skb);
463 return 0;
464 } else
465 #endif
466 if (atomic_read(&sk->rmem_alloc) <= sk->rcvbuf &&
467 !test_bit(0, &sk->protinfo.af_netlink->state)) {
468 skb_orphan(skb);
469 skb_set_owner_r(skb, sk);
470 skb_queue_tail(&sk->receive_queue, skb);
471 sk->data_ready(sk, skb->len);
472 return 0;
474 return -1;
477 void netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
478 u32 group, int allocation)
480 struct sock *sk;
481 struct sk_buff *skb2 = NULL;
482 int protocol = ssk->protocol;
483 int failure = 0;
485 /* While we sleep in clone, do not allow to change socket list */
487 netlink_lock_table();
489 for (sk = nl_table[protocol]; sk; sk = sk->next) {
490 if (ssk == sk)
491 continue;
493 if (sk->protinfo.af_netlink->pid == pid ||
494 !(sk->protinfo.af_netlink->groups&group))
495 continue;
497 if (failure) {
498 netlink_overrun(sk);
499 continue;
502 sock_hold(sk);
503 if (skb2 == NULL) {
504 if (atomic_read(&skb->users) != 1) {
505 skb2 = skb_clone(skb, allocation);
506 } else {
507 skb2 = skb;
508 atomic_inc(&skb->users);
511 if (skb2 == NULL) {
512 netlink_overrun(sk);
513 /* Clone failed. Notify ALL listeners. */
514 failure = 1;
515 } else if (netlink_broadcast_deliver(sk, skb2)) {
516 netlink_overrun(sk);
517 } else
518 skb2 = NULL;
519 sock_put(sk);
522 netlink_unlock_table();
524 if (skb2)
525 kfree_skb(skb2);
526 kfree_skb(skb);
529 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
531 struct sock *sk;
532 int protocol = ssk->protocol;
534 read_lock(&nl_table_lock);
535 for (sk = nl_table[protocol]; sk; sk = sk->next) {
536 if (ssk == sk)
537 continue;
539 if (sk->protinfo.af_netlink->pid == pid ||
540 !(sk->protinfo.af_netlink->groups&group))
541 continue;
543 sk->err = code;
544 sk->error_report(sk);
546 read_unlock(&nl_table_lock);
549 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, int len,
550 struct scm_cookie *scm)
552 struct sock *sk = sock->sk;
553 struct sockaddr_nl *addr=msg->msg_name;
554 u32 dst_pid;
555 u32 dst_groups;
556 struct sk_buff *skb;
557 int err;
559 if (msg->msg_flags&MSG_OOB)
560 return -EOPNOTSUPP;
562 if (msg->msg_namelen) {
563 if (addr->nl_family != AF_NETLINK)
564 return -EINVAL;
565 dst_pid = addr->nl_pid;
566 dst_groups = addr->nl_groups;
567 if (dst_groups && !capable(CAP_NET_ADMIN))
568 return -EPERM;
569 } else {
570 dst_pid = sk->protinfo.af_netlink->dst_pid;
571 dst_groups = sk->protinfo.af_netlink->dst_groups;
574 if (!sk->protinfo.af_netlink->pid) {
575 err = netlink_autobind(sock);
576 if (err)
577 goto out;
580 err = -EMSGSIZE;
581 if ((unsigned)len > sk->sndbuf-32)
582 goto out;
583 err = -ENOBUFS;
584 skb = alloc_skb(len, GFP_KERNEL);
585 if (skb==NULL)
586 goto out;
588 NETLINK_CB(skb).pid = sk->protinfo.af_netlink->pid;
589 NETLINK_CB(skb).groups = sk->protinfo.af_netlink->groups;
590 NETLINK_CB(skb).dst_pid = dst_pid;
591 NETLINK_CB(skb).dst_groups = dst_groups;
592 memcpy(NETLINK_CREDS(skb), &scm->creds, sizeof(struct ucred));
594 /* What can I do? Netlink is asynchronous, so that
595 we will have to save current capabilities to
596 check them, when this message will be delivered
597 to corresponding kernel module. --ANK (980802)
599 NETLINK_CB(skb).eff_cap = current->cap_effective;
601 err = -EFAULT;
602 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
603 kfree_skb(skb);
604 goto out;
607 if (dst_groups) {
608 atomic_inc(&skb->users);
609 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
611 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
613 out:
614 return err;
617 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, int len,
618 int flags, struct scm_cookie *scm)
620 struct sock *sk = sock->sk;
621 int noblock = flags&MSG_DONTWAIT;
622 int copied;
623 struct sk_buff *skb;
624 int err;
626 if (flags&MSG_OOB)
627 return -EOPNOTSUPP;
629 copied = 0;
631 skb = skb_recv_datagram(sk,flags,noblock,&err);
632 if (skb==NULL)
633 goto out;
635 msg->msg_namelen = 0;
637 copied = skb->len;
638 if (len < copied) {
639 msg->msg_flags |= MSG_TRUNC;
640 copied = len;
643 skb->h.raw = skb->data;
644 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
646 if (msg->msg_name) {
647 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
648 addr->nl_family = AF_NETLINK;
649 addr->nl_pid = NETLINK_CB(skb).pid;
650 addr->nl_groups = NETLINK_CB(skb).dst_groups;
651 msg->msg_namelen = sizeof(*addr);
654 scm->creds = *NETLINK_CREDS(skb);
655 skb_free_datagram(sk, skb);
657 if (sk->protinfo.af_netlink->cb
658 && atomic_read(&sk->rmem_alloc) <= sk->rcvbuf/2)
659 netlink_dump(sk);
661 out:
662 if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
663 if (skb_queue_len(&sk->receive_queue) == 0)
664 clear_bit(0, &sk->protinfo.af_netlink->state);
665 if (!test_bit(0, &sk->protinfo.af_netlink->state))
666 wake_up_interruptible(&sk->protinfo.af_netlink->wait);
668 return err ? : copied;
671 void netlink_data_ready(struct sock *sk, int len)
673 if (sk->protinfo.af_netlink->data_ready)
674 sk->protinfo.af_netlink->data_ready(sk, len);
676 if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
677 if (skb_queue_len(&sk->receive_queue) == 0)
678 clear_bit(0, &sk->protinfo.af_netlink->state);
679 if (!test_bit(0, &sk->protinfo.af_netlink->state))
680 wake_up_interruptible(&sk->protinfo.af_netlink->wait);
685 * We export these functions to other modules. They provide a
686 * complete set of kernel non-blocking support for message
687 * queueing.
690 struct sock *
691 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
693 struct socket *sock;
694 struct sock *sk;
696 if (unit<0 || unit>=MAX_LINKS)
697 return NULL;
699 if (!(sock = sock_alloc()))
700 return NULL;
702 sock->type = SOCK_RAW;
704 if (netlink_create(sock, unit) < 0) {
705 sock_release(sock);
706 return NULL;
708 sk = sock->sk;
709 sk->data_ready = netlink_data_ready;
710 if (input)
711 sk->protinfo.af_netlink->data_ready = input;
713 netlink_insert(sk, 0);
714 return sk;
717 static void netlink_destroy_callback(struct netlink_callback *cb)
719 if (cb->skb)
720 kfree_skb(cb->skb);
721 kfree(cb);
725 * It looks a bit ugly.
726 * It would be better to create kernel thread.
729 static int netlink_dump(struct sock *sk)
731 struct netlink_callback *cb;
732 struct sk_buff *skb;
733 struct nlmsghdr *nlh;
734 int len;
736 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
737 if (!skb)
738 return -ENOBUFS;
740 spin_lock(&sk->protinfo.af_netlink->cb_lock);
742 cb = sk->protinfo.af_netlink->cb;
743 if (cb == NULL) {
744 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
745 kfree_skb(skb);
746 return -EINVAL;
749 len = cb->dump(skb, cb);
751 if (len > 0) {
752 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
753 skb_queue_tail(&sk->receive_queue, skb);
754 sk->data_ready(sk, len);
755 return 0;
758 nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
759 nlh->nlmsg_flags |= NLM_F_MULTI;
760 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
761 skb_queue_tail(&sk->receive_queue, skb);
762 sk->data_ready(sk, skb->len);
764 cb->done(cb);
765 sk->protinfo.af_netlink->cb = NULL;
766 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
768 netlink_destroy_callback(cb);
769 sock_put(sk);
770 return 0;
773 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
774 struct nlmsghdr *nlh,
775 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
776 int (*done)(struct netlink_callback*))
778 struct netlink_callback *cb;
779 struct sock *sk;
781 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
782 if (cb == NULL)
783 return -ENOBUFS;
785 memset(cb, 0, sizeof(*cb));
786 cb->dump = dump;
787 cb->done = done;
788 cb->nlh = nlh;
789 atomic_inc(&skb->users);
790 cb->skb = skb;
792 sk = netlink_lookup(ssk->protocol, NETLINK_CB(skb).pid);
793 if (sk == NULL) {
794 netlink_destroy_callback(cb);
795 return -ECONNREFUSED;
797 /* A dump is in progress... */
798 spin_lock(&sk->protinfo.af_netlink->cb_lock);
799 if (sk->protinfo.af_netlink->cb) {
800 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
801 netlink_destroy_callback(cb);
802 sock_put(sk);
803 return -EBUSY;
805 sk->protinfo.af_netlink->cb = cb;
806 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
808 netlink_dump(sk);
809 return 0;
812 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
814 struct sk_buff *skb;
815 struct nlmsghdr *rep;
816 struct nlmsgerr *errmsg;
817 int size;
819 if (err == 0)
820 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
821 else
822 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
824 skb = alloc_skb(size, GFP_KERNEL);
825 if (!skb)
826 return;
828 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
829 NLMSG_ERROR, sizeof(struct nlmsgerr));
830 errmsg = NLMSG_DATA(rep);
831 errmsg->error = err;
832 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
833 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
837 #ifdef NL_EMULATE_DEV
839 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
842 * Backward compatibility.
845 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
847 struct sock *sk = netlink_kernel_create(unit, NULL);
848 if (sk == NULL)
849 return -ENOBUFS;
850 sk->protinfo.af_netlink->handler = function;
851 write_lock_bh(&nl_emu_lock);
852 netlink_kernel[unit] = sk->socket;
853 write_unlock_bh(&nl_emu_lock);
854 return 0;
857 void netlink_detach(int unit)
859 struct socket *sock;
861 write_lock_bh(&nl_emu_lock);
862 sock = netlink_kernel[unit];
863 netlink_kernel[unit] = NULL;
864 write_unlock_bh(&nl_emu_lock);
866 sock_release(sock);
869 int netlink_post(int unit, struct sk_buff *skb)
871 struct socket *sock;
873 read_lock(&nl_emu_lock);
874 sock = netlink_kernel[unit];
875 if (sock) {
876 struct sock *sk = sock->sk;
877 memset(skb->cb, 0, sizeof(skb->cb));
878 sock_hold(sk);
879 read_unlock(&nl_emu_lock);
881 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
883 sock_put(sk);
884 return 0;
886 read_unlock(&nl_emu_lock);
887 return -EUNATCH;
890 #endif
893 #ifdef CONFIG_PROC_FS
894 static int netlink_read_proc(char *buffer, char **start, off_t offset,
895 int length, int *eof, void *data)
897 off_t pos=0;
898 off_t begin=0;
899 int len=0;
900 int i;
901 struct sock *s;
903 len+= sprintf(buffer,"sk Eth Pid Groups "
904 "Rmem Wmem Dump Locks\n");
906 for (i=0; i<MAX_LINKS; i++) {
907 read_lock(&nl_table_lock);
908 for (s = nl_table[i]; s; s = s->next) {
909 len+=sprintf(buffer+len,"%p %-3d %-6d %08x %-8d %-8d %p %d",
911 s->protocol,
912 s->protinfo.af_netlink->pid,
913 s->protinfo.af_netlink->groups,
914 atomic_read(&s->rmem_alloc),
915 atomic_read(&s->wmem_alloc),
916 s->protinfo.af_netlink->cb,
917 atomic_read(&s->refcnt)
920 buffer[len++]='\n';
922 pos=begin+len;
923 if(pos<offset) {
924 len=0;
925 begin=pos;
927 if(pos>offset+length) {
928 read_unlock(&nl_table_lock);
929 goto done;
932 read_unlock(&nl_table_lock);
934 *eof = 1;
936 done:
937 *start=buffer+(offset-begin);
938 len-=(offset-begin);
939 if(len>length)
940 len=length;
941 if(len<0)
942 len=0;
943 return len;
945 #endif
947 struct proto_ops netlink_ops = {
948 family: PF_NETLINK,
950 release: netlink_release,
951 bind: netlink_bind,
952 connect: netlink_connect,
953 socketpair: sock_no_socketpair,
954 accept: sock_no_accept,
955 getname: netlink_getname,
956 poll: datagram_poll,
957 ioctl: sock_no_ioctl,
958 listen: sock_no_listen,
959 shutdown: sock_no_shutdown,
960 setsockopt: sock_no_setsockopt,
961 getsockopt: sock_no_getsockopt,
962 sendmsg: netlink_sendmsg,
963 recvmsg: netlink_recvmsg,
964 mmap: sock_no_mmap,
967 struct net_proto_family netlink_family_ops = {
968 PF_NETLINK,
969 netlink_create
972 static int __init netlink_proto_init(void)
974 struct sk_buff *dummy_skb;
976 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) {
977 printk(KERN_CRIT "netlink_init: panic\n");
978 return -1;
980 sock_register(&netlink_family_ops);
981 #ifdef CONFIG_PROC_FS
982 create_proc_read_entry("net/netlink", 0, 0, netlink_read_proc, NULL);
983 #endif
984 return 0;
987 module_init(netlink_proto_init);