File: /usr/src/linux/net/netlink/af_netlink.c
1 /*
2 * NETLINK Kernel-user communication protocol.
3 *
4 * Authors: Alan Cox <alan@redhat.com>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 *
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.
11 *
12 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 *
15 */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19
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>
29 #include <linux/un.h>
30 #include <linux/fcntl.h>
31 #include <linux/termios.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/fs.h>
35 #include <linux/slab.h>
36 #include <asm/uaccess.h>
37 #include <linux/skbuff.h>
38 #include <linux/netdevice.h>
39 #include <linux/netlink.h>
40 #include <linux/proc_fs.h>
41 #include <linux/smp_lock.h>
42 #include <net/sock.h>
43 #include <net/scm.h>
44
45 #define Nprintk(a...)
46
47 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
48 #define NL_EMULATE_DEV
49 #endif
50
51 #define BUG_TRAP(x) if (!(x)) { printk("Assertion (" #x ") failed at " __FILE__ "(%d):" __FUNCTION__ "\n", __LINE__); }
52
53 struct netlink_opt
54 {
55 u32 pid;
56 unsigned groups;
57 u32 dst_pid;
58 unsigned dst_groups;
59 unsigned long state;
60 int (*handler)(int unit, struct sk_buff *skb);
61 wait_queue_head_t wait;
62 struct netlink_callback *cb;
63 spinlock_t cb_lock;
64 void (*data_ready)(struct sock *sk, int bytes);
65 };
66
67 static struct sock *nl_table[MAX_LINKS];
68 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
69
70 #ifdef NL_EMULATE_DEV
71 static struct socket *netlink_kernel[MAX_LINKS];
72 #endif
73
74 static int netlink_dump(struct sock *sk);
75 static void netlink_destroy_callback(struct netlink_callback *cb);
76
77 atomic_t netlink_sock_nr;
78
79 static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;
80 static atomic_t nl_table_users = ATOMIC_INIT(0);
81
82 static void netlink_sock_destruct(struct sock *sk)
83 {
84 skb_queue_purge(&sk->receive_queue);
85
86 if (!sk->dead) {
87 printk("Freeing alive netlink socket %p\n", sk);
88 return;
89 }
90 BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
91 BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
92 BUG_TRAP(sk->protinfo.af_netlink->cb==NULL);
93
94 kfree(sk->protinfo.af_netlink);
95
96 atomic_dec(&netlink_sock_nr);
97 #ifdef NETLINK_REFCNT_DEBUG
98 printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));
99 #endif
100 }
101
102 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
103 * Look, when several writers sleep and reader wakes them up, all but one
104 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
105 * this, _but_ remember, it adds useless work on UP machines.
106 */
107
108 static void netlink_table_grab(void)
109 {
110 write_lock_bh(&nl_table_lock);
111
112 if (atomic_read(&nl_table_users)) {
113 DECLARE_WAITQUEUE(wait, current);
114
115 add_wait_queue_exclusive(&nl_table_wait, &wait);
116 for(;;) {
117 set_current_state(TASK_UNINTERRUPTIBLE);
118 if (atomic_read(&nl_table_users) == 0)
119 break;
120 write_unlock_bh(&nl_table_lock);
121 schedule();
122 write_lock_bh(&nl_table_lock);
123 }
124
125 __set_current_state(TASK_RUNNING);
126 remove_wait_queue(&nl_table_wait, &wait);
127 }
128 }
129
130 static __inline__ void netlink_table_ungrab(void)
131 {
132 write_unlock_bh(&nl_table_lock);
133 wake_up(&nl_table_wait);
134 }
135
136 static __inline__ void
137 netlink_lock_table(void)
138 {
139 /* read_lock() synchronizes us to netlink_table_grab */
140
141 read_lock(&nl_table_lock);
142 atomic_inc(&nl_table_users);
143 read_unlock(&nl_table_lock);
144 }
145
146 static __inline__ void
147 netlink_unlock_table(void)
148 {
149 if (atomic_dec_and_test(&nl_table_users))
150 wake_up(&nl_table_wait);
151 }
152
153 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
154 {
155 struct sock *sk;
156
157 read_lock(&nl_table_lock);
158 for (sk=nl_table[protocol]; sk; sk=sk->next) {
159 if (sk->protinfo.af_netlink->pid == pid) {
160 sock_hold(sk);
161 read_unlock(&nl_table_lock);
162 return sk;
163 }
164 }
165
166 read_unlock(&nl_table_lock);
167 return NULL;
168 }
169
170 extern struct proto_ops netlink_ops;
171
172 static int netlink_insert(struct sock *sk, u32 pid)
173 {
174 int err = -EADDRINUSE;
175 struct sock *osk;
176
177 netlink_table_grab();
178 for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
179 if (osk->protinfo.af_netlink->pid == pid)
180 break;
181 }
182 if (osk == NULL) {
183 err = -EBUSY;
184 if (sk->protinfo.af_netlink->pid == 0) {
185 sk->protinfo.af_netlink->pid = pid;
186 sk->next = nl_table[sk->protocol];
187 nl_table[sk->protocol] = sk;
188 sock_hold(sk);
189 err = 0;
190 }
191 }
192 netlink_table_ungrab();
193 return err;
194 }
195
196 static void netlink_remove(struct sock *sk)
197 {
198 struct sock **skp;
199
200 netlink_table_grab();
201 for (skp = &nl_table[sk->protocol]; *skp; skp = &((*skp)->next)) {
202 if (*skp == sk) {
203 *skp = sk->next;
204 __sock_put(sk);
205 break;
206 }
207 }
208 netlink_table_ungrab();
209 }
210
211 static int netlink_create(struct socket *sock, int protocol)
212 {
213 struct sock *sk;
214
215 sock->state = SS_UNCONNECTED;
216
217 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
218 return -ESOCKTNOSUPPORT;
219
220 if (protocol<0 || protocol >= MAX_LINKS)
221 return -EPROTONOSUPPORT;
222
223 sock->ops = &netlink_ops;
224
225 sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1);
226 if (!sk)
227 return -ENOMEM;
228
229 sock_init_data(sock,sk);
230
231 sk->protinfo.af_netlink = kmalloc(sizeof(struct netlink_opt), GFP_KERNEL);
232 if (sk->protinfo.af_netlink == NULL) {
233 sk_free(sk);
234 return -ENOMEM;
235 }
236 memset(sk->protinfo.af_netlink, 0, sizeof(struct netlink_opt));
237
238 spin_lock_init(&sk->protinfo.af_netlink->cb_lock);
239 init_waitqueue_head(&sk->protinfo.af_netlink->wait);
240 sk->destruct = netlink_sock_destruct;
241 atomic_inc(&netlink_sock_nr);
242
243 sk->protocol=protocol;
244 return 0;
245 }
246
247 static int netlink_release(struct socket *sock)
248 {
249 struct sock *sk = sock->sk;
250
251 if (!sk)
252 return 0;
253
254 netlink_remove(sk);
255
256 spin_lock(&sk->protinfo.af_netlink->cb_lock);
257 if (sk->protinfo.af_netlink->cb) {
258 sk->protinfo.af_netlink->cb->done(sk->protinfo.af_netlink->cb);
259 netlink_destroy_callback(sk->protinfo.af_netlink->cb);
260 sk->protinfo.af_netlink->cb = NULL;
261 __sock_put(sk);
262 }
263 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
264
265 /* OK. Socket is unlinked, and, therefore,
266 no new packets will arrive */
267
268 sock_orphan(sk);
269 sock->sk = NULL;
270 wake_up_interruptible_all(&sk->protinfo.af_netlink->wait);
271
272 skb_queue_purge(&sk->write_queue);
273
274 sock_put(sk);
275 return 0;
276 }
277
278 static int netlink_autobind(struct socket *sock)
279 {
280 struct sock *sk = sock->sk;
281 struct sock *osk;
282 s32 pid = current->pid;
283 int err;
284
285 retry:
286 netlink_table_grab();
287 for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
288 if (osk->protinfo.af_netlink->pid == pid) {
289 /* Bind collision, search negative pid values. */
290 if (pid > 0)
291 pid = -4096;
292 pid--;
293 netlink_table_ungrab();
294 goto retry;
295 }
296 }
297 netlink_table_ungrab();
298
299 err = netlink_insert(sk, pid);
300 if (err == -EADDRINUSE)
301 goto retry;
302 sk->protinfo.af_netlink->groups = 0;
303 return 0;
304 }
305
306 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
307 {
308 struct sock *sk = sock->sk;
309 int err;
310 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
311
312 if (nladdr->nl_family != AF_NETLINK)
313 return -EINVAL;
314
315 /* Only superuser is allowed to listen multicasts */
316 if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
317 return -EPERM;
318
319 if (sk->protinfo.af_netlink->pid) {
320 if (nladdr->nl_pid != sk->protinfo.af_netlink->pid)
321 return -EINVAL;
322 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
323 return 0;
324 }
325
326 if (nladdr->nl_pid == 0) {
327 err = netlink_autobind(sock);
328 if (err == 0)
329 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
330 return err;
331 }
332
333 err = netlink_insert(sk, nladdr->nl_pid);
334 if (err == 0)
335 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
336 return err;
337 }
338
339 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
340 int alen, int flags)
341 {
342 int err = 0;
343 struct sock *sk = sock->sk;
344 struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
345
346 if (addr->sa_family == AF_UNSPEC) {
347 sk->protinfo.af_netlink->dst_pid = 0;
348 sk->protinfo.af_netlink->dst_groups = 0;
349 return 0;
350 }
351 if (addr->sa_family != AF_NETLINK)
352 return -EINVAL;
353
354 /* Only superuser is allowed to send multicasts */
355 if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
356 return -EPERM;
357
358 if (!sk->protinfo.af_netlink->pid)
359 err = netlink_autobind(sock);
360
361 if (err == 0) {
362 sk->protinfo.af_netlink->dst_pid = nladdr->nl_pid;
363 sk->protinfo.af_netlink->dst_groups = nladdr->nl_groups;
364 }
365
366 return 0;
367 }
368
369 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
370 {
371 struct sock *sk = sock->sk;
372 struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
373
374 nladdr->nl_family = AF_NETLINK;
375 *addr_len = sizeof(*nladdr);
376
377 if (peer) {
378 nladdr->nl_pid = sk->protinfo.af_netlink->dst_pid;
379 nladdr->nl_groups = sk->protinfo.af_netlink->dst_groups;
380 } else {
381 nladdr->nl_pid = sk->protinfo.af_netlink->pid;
382 nladdr->nl_groups = sk->protinfo.af_netlink->groups;
383 }
384 return 0;
385 }
386
387 static void netlink_overrun(struct sock *sk)
388 {
389 if (!test_and_set_bit(0, &sk->protinfo.af_netlink->state)) {
390 sk->err = ENOBUFS;
391 sk->error_report(sk);
392 }
393 }
394
395 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
396 {
397 struct sock *sk;
398 int len = skb->len;
399 int protocol = ssk->protocol;
400 long timeo;
401 DECLARE_WAITQUEUE(wait, current);
402
403 timeo = sock_sndtimeo(ssk, nonblock);
404
405 retry:
406 sk = netlink_lookup(protocol, pid);
407 if (sk == NULL)
408 goto no_dst;
409
410 #ifdef NL_EMULATE_DEV
411 if (sk->protinfo.af_netlink->handler) {
412 skb_orphan(skb);
413 len = sk->protinfo.af_netlink->handler(protocol, skb);
414 sock_put(sk);
415 return len;
416 }
417 #endif
418
419 if (atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
420 test_bit(0, &sk->protinfo.af_netlink->state)) {
421 if (!timeo) {
422 if (ssk->protinfo.af_netlink->pid == 0)
423 netlink_overrun(sk);
424 sock_put(sk);
425 kfree_skb(skb);
426 return -EAGAIN;
427 }
428
429 __set_current_state(TASK_INTERRUPTIBLE);
430 add_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
431
432 if ((atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
433 test_bit(0, &sk->protinfo.af_netlink->state)) &&
434 !sk->dead)
435 timeo = schedule_timeout(timeo);
436
437 __set_current_state(TASK_RUNNING);
438 remove_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
439 sock_put(sk);
440
441 if (signal_pending(current)) {
442 kfree_skb(skb);
443 return sock_intr_errno(timeo);
444 }
445 goto retry;
446 }
447
448 skb_orphan(skb);
449 skb_set_owner_r(skb, sk);
450 skb_queue_tail(&sk->receive_queue, skb);
451 sk->data_ready(sk, len);
452 sock_put(sk);
453 return len;
454
455 no_dst:
456 kfree_skb(skb);
457 return -ECONNREFUSED;
458 }
459
460 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
461 {
462 #ifdef NL_EMULATE_DEV
463 if (sk->protinfo.af_netlink->handler) {
464 skb_orphan(skb);
465 sk->protinfo.af_netlink->handler(sk->protocol, skb);
466 return 0;
467 } else
468 #endif
469 if (atomic_read(&sk->rmem_alloc) <= sk->rcvbuf &&
470 !test_bit(0, &sk->protinfo.af_netlink->state)) {
471 skb_orphan(skb);
472 skb_set_owner_r(skb, sk);
473 skb_queue_tail(&sk->receive_queue, skb);
474 sk->data_ready(sk, skb->len);
475 return 0;
476 }
477 return -1;
478 }
479
480 void netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
481 u32 group, int allocation)
482 {
483 struct sock *sk;
484 struct sk_buff *skb2 = NULL;
485 int protocol = ssk->protocol;
486 int failure = 0;
487
488 /* While we sleep in clone, do not allow to change socket list */
489
490 netlink_lock_table();
491
492 for (sk = nl_table[protocol]; sk; sk = sk->next) {
493 if (ssk == sk)
494 continue;
495
496 if (sk->protinfo.af_netlink->pid == pid ||
497 !(sk->protinfo.af_netlink->groups&group))
498 continue;
499
500 if (failure) {
501 netlink_overrun(sk);
502 continue;
503 }
504
505 sock_hold(sk);
506 if (skb2 == NULL) {
507 if (atomic_read(&skb->users) != 1) {
508 skb2 = skb_clone(skb, allocation);
509 } else {
510 skb2 = skb;
511 atomic_inc(&skb->users);
512 }
513 }
514 if (skb2 == NULL) {
515 netlink_overrun(sk);
516 /* Clone failed. Notify ALL listeners. */
517 failure = 1;
518 } else if (netlink_broadcast_deliver(sk, skb2)) {
519 netlink_overrun(sk);
520 } else
521 skb2 = NULL;
522 sock_put(sk);
523 }
524
525 netlink_unlock_table();
526
527 if (skb2)
528 kfree_skb(skb2);
529 kfree_skb(skb);
530 }
531
532 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
533 {
534 struct sock *sk;
535 int protocol = ssk->protocol;
536
537 read_lock(&nl_table_lock);
538 for (sk = nl_table[protocol]; sk; sk = sk->next) {
539 if (ssk == sk)
540 continue;
541
542 if (sk->protinfo.af_netlink->pid == pid ||
543 !(sk->protinfo.af_netlink->groups&group))
544 continue;
545
546 sk->err = code;
547 sk->error_report(sk);
548 }
549 read_unlock(&nl_table_lock);
550 }
551
552 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, int len,
553 struct scm_cookie *scm)
554 {
555 struct sock *sk = sock->sk;
556 struct sockaddr_nl *addr=msg->msg_name;
557 u32 dst_pid;
558 u32 dst_groups;
559 struct sk_buff *skb;
560 int err;
561
562 if (msg->msg_flags&MSG_OOB)
563 return -EOPNOTSUPP;
564
565 if (msg->msg_namelen) {
566 if (addr->nl_family != AF_NETLINK)
567 return -EINVAL;
568 dst_pid = addr->nl_pid;
569 dst_groups = addr->nl_groups;
570 if (dst_groups && !capable(CAP_NET_ADMIN))
571 return -EPERM;
572 } else {
573 dst_pid = sk->protinfo.af_netlink->dst_pid;
574 dst_groups = sk->protinfo.af_netlink->dst_groups;
575 }
576
577 if (!sk->protinfo.af_netlink->pid) {
578 err = netlink_autobind(sock);
579 if (err)
580 goto out;
581 }
582
583 err = -EMSGSIZE;
584 if ((unsigned)len > sk->sndbuf-32)
585 goto out;
586 err = -ENOBUFS;
587 skb = alloc_skb(len, GFP_KERNEL);
588 if (skb==NULL)
589 goto out;
590
591 NETLINK_CB(skb).pid = sk->protinfo.af_netlink->pid;
592 NETLINK_CB(skb).groups = sk->protinfo.af_netlink->groups;
593 NETLINK_CB(skb).dst_pid = dst_pid;
594 NETLINK_CB(skb).dst_groups = dst_groups;
595 memcpy(NETLINK_CREDS(skb), &scm->creds, sizeof(struct ucred));
596
597 /* What can I do? Netlink is asynchronous, so that
598 we will have to save current capabilities to
599 check them, when this message will be delivered
600 to corresponding kernel module. --ANK (980802)
601 */
602 NETLINK_CB(skb).eff_cap = current->cap_effective;
603
604 err = -EFAULT;
605 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
606 kfree_skb(skb);
607 goto out;
608 }
609
610 if (dst_groups) {
611 atomic_inc(&skb->users);
612 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
613 }
614 err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
615
616 out:
617 return err;
618 }
619
620 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, int len,
621 int flags, struct scm_cookie *scm)
622 {
623 struct sock *sk = sock->sk;
624 int noblock = flags&MSG_DONTWAIT;
625 int copied;
626 struct sk_buff *skb;
627 int err;
628
629 if (flags&MSG_OOB)
630 return -EOPNOTSUPP;
631
632 copied = 0;
633
634 skb = skb_recv_datagram(sk,flags,noblock,&err);
635 if (skb==NULL)
636 goto out;
637
638 msg->msg_namelen = 0;
639
640 copied = skb->len;
641 if (len < copied) {
642 msg->msg_flags |= MSG_TRUNC;
643 copied = len;
644 }
645
646 skb->h.raw = skb->data;
647 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
648
649 if (msg->msg_name) {
650 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
651 addr->nl_family = AF_NETLINK;
652 addr->nl_pid = NETLINK_CB(skb).pid;
653 addr->nl_groups = NETLINK_CB(skb).dst_groups;
654 msg->msg_namelen = sizeof(*addr);
655 }
656
657 scm->creds = *NETLINK_CREDS(skb);
658 skb_free_datagram(sk, skb);
659
660 if (sk->protinfo.af_netlink->cb
661 && atomic_read(&sk->rmem_alloc) <= sk->rcvbuf/2)
662 netlink_dump(sk);
663
664 out:
665 if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
666 if (skb_queue_len(&sk->receive_queue) == 0)
667 clear_bit(0, &sk->protinfo.af_netlink->state);
668 if (!test_bit(0, &sk->protinfo.af_netlink->state))
669 wake_up_interruptible(&sk->protinfo.af_netlink->wait);
670 }
671 return err ? : copied;
672 }
673
674 void netlink_data_ready(struct sock *sk, int len)
675 {
676 if (sk->protinfo.af_netlink->data_ready)
677 sk->protinfo.af_netlink->data_ready(sk, len);
678
679 if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
680 if (skb_queue_len(&sk->receive_queue) == 0)
681 clear_bit(0, &sk->protinfo.af_netlink->state);
682 if (!test_bit(0, &sk->protinfo.af_netlink->state))
683 wake_up_interruptible(&sk->protinfo.af_netlink->wait);
684 }
685 }
686
687 /*
688 * We export these functions to other modules. They provide a
689 * complete set of kernel non-blocking support for message
690 * queueing.
691 */
692
693 struct sock *
694 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
695 {
696 struct socket *sock;
697 struct sock *sk;
698
699 if (unit<0 || unit>=MAX_LINKS)
700 return NULL;
701
702 if (!(sock = sock_alloc()))
703 return NULL;
704
705 sock->type = SOCK_RAW;
706
707 if (netlink_create(sock, unit) < 0) {
708 sock_release(sock);
709 return NULL;
710 }
711 sk = sock->sk;
712 sk->data_ready = netlink_data_ready;
713 if (input)
714 sk->protinfo.af_netlink->data_ready = input;
715
716 netlink_insert(sk, 0);
717 return sk;
718 }
719
720 static void netlink_destroy_callback(struct netlink_callback *cb)
721 {
722 if (cb->skb)
723 kfree_skb(cb->skb);
724 kfree(cb);
725 }
726
727 /*
728 * It looks a bit ugly.
729 * It would be better to create kernel thread.
730 */
731
732 static int netlink_dump(struct sock *sk)
733 {
734 struct netlink_callback *cb;
735 struct sk_buff *skb;
736 struct nlmsghdr *nlh;
737 int len;
738
739 skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
740 if (!skb)
741 return -ENOBUFS;
742
743 spin_lock(&sk->protinfo.af_netlink->cb_lock);
744
745 cb = sk->protinfo.af_netlink->cb;
746 if (cb == NULL) {
747 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
748 kfree_skb(skb);
749 return -EINVAL;
750 }
751
752 len = cb->dump(skb, cb);
753
754 if (len > 0) {
755 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
756 skb_queue_tail(&sk->receive_queue, skb);
757 sk->data_ready(sk, len);
758 return 0;
759 }
760
761 nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
762 nlh->nlmsg_flags |= NLM_F_MULTI;
763 memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
764 skb_queue_tail(&sk->receive_queue, skb);
765 sk->data_ready(sk, skb->len);
766
767 cb->done(cb);
768 sk->protinfo.af_netlink->cb = NULL;
769 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
770
771 netlink_destroy_callback(cb);
772 sock_put(sk);
773 return 0;
774 }
775
776 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
777 struct nlmsghdr *nlh,
778 int (*dump)(struct sk_buff *skb, struct netlink_callback*),
779 int (*done)(struct netlink_callback*))
780 {
781 struct netlink_callback *cb;
782 struct sock *sk;
783
784 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
785 if (cb == NULL)
786 return -ENOBUFS;
787
788 memset(cb, 0, sizeof(*cb));
789 cb->dump = dump;
790 cb->done = done;
791 cb->nlh = nlh;
792 atomic_inc(&skb->users);
793 cb->skb = skb;
794
795 sk = netlink_lookup(ssk->protocol, NETLINK_CB(skb).pid);
796 if (sk == NULL) {
797 netlink_destroy_callback(cb);
798 return -ECONNREFUSED;
799 }
800 /* A dump is in progress... */
801 spin_lock(&sk->protinfo.af_netlink->cb_lock);
802 if (sk->protinfo.af_netlink->cb) {
803 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
804 netlink_destroy_callback(cb);
805 sock_put(sk);
806 return -EBUSY;
807 }
808 sk->protinfo.af_netlink->cb = cb;
809 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
810
811 netlink_dump(sk);
812 return 0;
813 }
814
815 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
816 {
817 struct sk_buff *skb;
818 struct nlmsghdr *rep;
819 struct nlmsgerr *errmsg;
820 int size;
821
822 if (err == 0)
823 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
824 else
825 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
826
827 skb = alloc_skb(size, GFP_KERNEL);
828 if (!skb)
829 return;
830
831 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
832 NLMSG_ERROR, sizeof(struct nlmsgerr));
833 errmsg = NLMSG_DATA(rep);
834 errmsg->error = err;
835 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
836 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
837 }
838
839
840 #ifdef NL_EMULATE_DEV
841
842 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
843
844 /*
845 * Backward compatibility.
846 */
847
848 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
849 {
850 struct sock *sk = netlink_kernel_create(unit, NULL);
851 if (sk == NULL)
852 return -ENOBUFS;
853 sk->protinfo.af_netlink->handler = function;
854 write_lock_bh(&nl_emu_lock);
855 netlink_kernel[unit] = sk->socket;
856 write_unlock_bh(&nl_emu_lock);
857 return 0;
858 }
859
860 void netlink_detach(int unit)
861 {
862 struct socket *sock;
863
864 write_lock_bh(&nl_emu_lock);
865 sock = netlink_kernel[unit];
866 netlink_kernel[unit] = NULL;
867 write_unlock_bh(&nl_emu_lock);
868
869 sock_release(sock);
870 }
871
872 int netlink_post(int unit, struct sk_buff *skb)
873 {
874 struct socket *sock;
875
876 read_lock(&nl_emu_lock);
877 sock = netlink_kernel[unit];
878 if (sock) {
879 struct sock *sk = sock->sk;
880 memset(skb->cb, 0, sizeof(skb->cb));
881 sock_hold(sk);
882 read_unlock(&nl_emu_lock);
883
884 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
885
886 sock_put(sk);
887 return 0;
888 }
889 read_unlock(&nl_emu_lock);
890 return -EUNATCH;
891 }
892
893 #endif
894
895
896 #ifdef CONFIG_PROC_FS
897 static int netlink_read_proc(char *buffer, char **start, off_t offset,
898 int length, int *eof, void *data)
899 {
900 off_t pos=0;
901 off_t begin=0;
902 int len=0;
903 int i;
904 struct sock *s;
905
906 len+= sprintf(buffer,"sk Eth Pid Groups "
907 "Rmem Wmem Dump Locks\n");
908
909 for (i=0; i<MAX_LINKS; i++) {
910 read_lock(&nl_table_lock);
911 for (s = nl_table[i]; s; s = s->next) {
912 len+=sprintf(buffer+len,"%p %-3d %-6d %08x %-8d %-8d %p %d",
913 s,
914 s->protocol,
915 s->protinfo.af_netlink->pid,
916 s->protinfo.af_netlink->groups,
917 atomic_read(&s->rmem_alloc),
918 atomic_read(&s->wmem_alloc),
919 s->protinfo.af_netlink->cb,
920 atomic_read(&s->refcnt)
921 );
922
923 buffer[len++]='\n';
924
925 pos=begin+len;
926 if(pos<offset) {
927 len=0;
928 begin=pos;
929 }
930 if(pos>offset+length) {
931 read_unlock(&nl_table_lock);
932 goto done;
933 }
934 }
935 read_unlock(&nl_table_lock);
936 }
937 *eof = 1;
938
939 done:
940 *start=buffer+(offset-begin);
941 len-=(offset-begin);
942 if(len>length)
943 len=length;
944 if(len<0)
945 len=0;
946 return len;
947 }
948 #endif
949
950 struct proto_ops netlink_ops = {
951 family: PF_NETLINK,
952
953 release: netlink_release,
954 bind: netlink_bind,
955 connect: netlink_connect,
956 socketpair: sock_no_socketpair,
957 accept: sock_no_accept,
958 getname: netlink_getname,
959 poll: datagram_poll,
960 ioctl: sock_no_ioctl,
961 listen: sock_no_listen,
962 shutdown: sock_no_shutdown,
963 setsockopt: sock_no_setsockopt,
964 getsockopt: sock_no_getsockopt,
965 sendmsg: netlink_sendmsg,
966 recvmsg: netlink_recvmsg,
967 mmap: sock_no_mmap,
968 sendpage: sock_no_sendpage,
969 };
970
971 struct net_proto_family netlink_family_ops = {
972 PF_NETLINK,
973 netlink_create
974 };
975
976 static int __init netlink_proto_init(void)
977 {
978 struct sk_buff *dummy_skb;
979
980 if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) {
981 printk(KERN_CRIT "netlink_init: panic\n");
982 return -1;
983 }
984 sock_register(&netlink_family_ops);
985 #ifdef CONFIG_PROC_FS
986 create_proc_read_entry("net/netlink", 0, 0, netlink_read_proc, NULL);
987 #endif
988 return 0;
989 }
990
991 static void __exit netlink_proto_exit(void)
992 {
993 sock_unregister(PF_NETLINK);
994 remove_proc_entry("net/netlink", NULL);
995 }
996
997 module_init(netlink_proto_init);
998 module_exit(netlink_proto_exit);
999