patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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  * 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  *
17  */
18
19 #include <linux/config.h>
20 #include <linux/module.h>
21
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/major.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/socket.h>
31 #include <linux/un.h>
32 #include <linux/fcntl.h>
33 #include <linux/termios.h>
34 #include <linux/sockios.h>
35 #include <linux/net.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <asm/uaccess.h>
39 #include <linux/skbuff.h>
40 #include <linux/netdevice.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <linux/smp_lock.h>
45 #include <linux/notifier.h>
46 #include <linux/security.h>
47 #include <net/sock.h>
48 #include <net/scm.h>
49
50 #define Nprintk(a...)
51
52 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
53 #define NL_EMULATE_DEV
54 #endif
55
56 struct netlink_opt
57 {
58         u32                     pid;
59         unsigned                groups;
60         u32                     dst_pid;
61         unsigned                dst_groups;
62         unsigned long           state;
63         int                     (*handler)(int unit, struct sk_buff *skb);
64         wait_queue_head_t       wait;
65         struct netlink_callback *cb;
66         spinlock_t              cb_lock;
67         void                    (*data_ready)(struct sock *sk, int bytes);
68 };
69
70 #define nlk_sk(__sk) ((struct netlink_opt *)(__sk)->sk_protinfo)
71
72 static struct hlist_head nl_table[MAX_LINKS];
73 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
74 static unsigned nl_nonroot[MAX_LINKS];
75
76 #ifdef NL_EMULATE_DEV
77 static struct socket *netlink_kernel[MAX_LINKS];
78 #endif
79
80 static int netlink_dump(struct sock *sk);
81 static void netlink_destroy_callback(struct netlink_callback *cb);
82
83 atomic_t netlink_sock_nr;
84
85 static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;
86 static atomic_t nl_table_users = ATOMIC_INIT(0);
87
88 static struct notifier_block *netlink_chain;
89
90 static void netlink_sock_destruct(struct sock *sk)
91 {
92         skb_queue_purge(&sk->sk_receive_queue);
93
94         if (!sock_flag(sk, SOCK_DEAD)) {
95                 printk("Freeing alive netlink socket %p\n", sk);
96                 return;
97         }
98         BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
99         BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
100         BUG_TRAP(!nlk_sk(sk)->cb);
101
102         kfree(nlk_sk(sk));
103
104         atomic_dec(&netlink_sock_nr);
105 #ifdef NETLINK_REFCNT_DEBUG
106         printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));
107 #endif
108 }
109
110 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
111  * Look, when several writers sleep and reader wakes them up, all but one
112  * immediately hit write lock and grab all the cpus. Exclusive sleep solves
113  * this, _but_ remember, it adds useless work on UP machines.
114  */
115
116 static void netlink_table_grab(void)
117 {
118         write_lock_bh(&nl_table_lock);
119
120         if (atomic_read(&nl_table_users)) {
121                 DECLARE_WAITQUEUE(wait, current);
122
123                 add_wait_queue_exclusive(&nl_table_wait, &wait);
124                 for(;;) {
125                         set_current_state(TASK_UNINTERRUPTIBLE);
126                         if (atomic_read(&nl_table_users) == 0)
127                                 break;
128                         write_unlock_bh(&nl_table_lock);
129                         schedule();
130                         write_lock_bh(&nl_table_lock);
131                 }
132
133                 __set_current_state(TASK_RUNNING);
134                 remove_wait_queue(&nl_table_wait, &wait);
135         }
136 }
137
138 static __inline__ void netlink_table_ungrab(void)
139 {
140         write_unlock_bh(&nl_table_lock);
141         wake_up(&nl_table_wait);
142 }
143
144 static __inline__ void
145 netlink_lock_table(void)
146 {
147         /* read_lock() synchronizes us to netlink_table_grab */
148
149         read_lock(&nl_table_lock);
150         atomic_inc(&nl_table_users);
151         read_unlock(&nl_table_lock);
152 }
153
154 static __inline__ void
155 netlink_unlock_table(void)
156 {
157         if (atomic_dec_and_test(&nl_table_users))
158                 wake_up(&nl_table_wait);
159 }
160
161 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
162 {
163         struct sock *sk;
164         struct hlist_node *node;
165
166         read_lock(&nl_table_lock);
167         sk_for_each(sk, node, &nl_table[protocol]) {
168                 if (nlk_sk(sk)->pid == pid) {
169                         sock_hold(sk);
170                         goto found;
171                 }
172         }
173         sk = NULL;
174 found:
175         read_unlock(&nl_table_lock);
176         return sk;
177 }
178
179 extern struct proto_ops netlink_ops;
180
181 static int netlink_insert(struct sock *sk, u32 pid)
182 {
183         int err = -EADDRINUSE;
184         struct sock *osk;
185         struct hlist_node *node;
186
187         netlink_table_grab();
188         sk_for_each(osk, node, &nl_table[sk->sk_protocol]) {
189                 if (nlk_sk(osk)->pid == pid)
190                         break;
191         }
192         if (!node) {
193                 err = -EBUSY;
194                 if (nlk_sk(sk)->pid == 0) {
195                         nlk_sk(sk)->pid = pid;
196                         sk_add_node(sk, &nl_table[sk->sk_protocol]);
197                         err = 0;
198                 }
199         }
200         netlink_table_ungrab();
201         return err;
202 }
203
204 static void netlink_remove(struct sock *sk)
205 {
206         netlink_table_grab();
207         sk_del_node_init(sk);
208         netlink_table_ungrab();
209 }
210
211 static int netlink_create(struct socket *sock, int protocol)
212 {
213         struct sock *sk;
214         struct netlink_opt *nlk;
215
216         sock->state = SS_UNCONNECTED;
217
218         if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
219                 return -ESOCKTNOSUPPORT;
220
221         if (protocol<0 || protocol >= MAX_LINKS)
222                 return -EPROTONOSUPPORT;
223
224         sock->ops = &netlink_ops;
225
226         sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1, NULL);
227         if (!sk)
228                 return -ENOMEM;
229
230         sock_init_data(sock,sk);
231         sk_set_owner(sk, THIS_MODULE);
232
233         nlk = sk->sk_protinfo = kmalloc(sizeof(*nlk), GFP_KERNEL);
234         if (!nlk) {
235                 sk_free(sk);
236                 return -ENOMEM;
237         }
238         memset(nlk, 0, sizeof(*nlk));
239
240         spin_lock_init(&nlk->cb_lock);
241         init_waitqueue_head(&nlk->wait);
242         sk->sk_destruct = netlink_sock_destruct;
243         atomic_inc(&netlink_sock_nr);
244
245         sk->sk_protocol = protocol;
246         return 0;
247 }
248
249 static int netlink_release(struct socket *sock)
250 {
251         struct sock *sk = sock->sk;
252         struct netlink_opt *nlk;
253
254         if (!sk)
255                 return 0;
256
257         netlink_remove(sk);
258         nlk = nlk_sk(sk);
259
260         spin_lock(&nlk->cb_lock);
261         if (nlk->cb) {
262                 nlk->cb->done(nlk->cb);
263                 netlink_destroy_callback(nlk->cb);
264                 nlk->cb = NULL;
265                 __sock_put(sk);
266         }
267         spin_unlock(&nlk->cb_lock);
268
269         /* OK. Socket is unlinked, and, therefore,
270            no new packets will arrive */
271
272         sock_orphan(sk);
273         sock->sk = NULL;
274         wake_up_interruptible_all(&nlk->wait);
275
276         skb_queue_purge(&sk->sk_write_queue);
277
278         if (nlk->pid && !nlk->groups) {
279                 struct netlink_notify n = {
280                                                 .protocol = sk->sk_protocol,
281                                                 .pid = nlk->pid,
282                                           };
283                 notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
284         }       
285         
286         sock_put(sk);
287         return 0;
288 }
289
290 static int netlink_autobind(struct socket *sock)
291 {
292         struct sock *sk = sock->sk;
293         struct sock *osk;
294         struct hlist_node *node;
295         s32 pid = current->pid;
296         int err;
297
298 retry:
299         netlink_table_grab();
300         sk_for_each(osk, node, &nl_table[sk->sk_protocol]) {
301                 if (nlk_sk(osk)->pid == pid) {
302                         /* Bind collision, search negative pid values. */
303                         if (pid > 0)
304                                 pid = -4096;
305                         pid--;
306                         netlink_table_ungrab();
307                         goto retry;
308                 }
309         }
310         netlink_table_ungrab();
311
312         err = netlink_insert(sk, pid);
313         if (err == -EADDRINUSE)
314                 goto retry;
315         nlk_sk(sk)->groups = 0;
316         return 0;
317 }
318
319 static inline int netlink_capable(struct socket *sock, unsigned flag) 
320
321         return (nl_nonroot[sock->sk->sk_protocol] & flag) ||
322                capable(CAP_NET_ADMIN);
323
324
325 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
326 {
327         struct sock *sk = sock->sk;
328         struct netlink_opt *nlk = nlk_sk(sk);
329         struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
330         int err;
331         
332         if (nladdr->nl_family != AF_NETLINK)
333                 return -EINVAL;
334
335         /* Only superuser is allowed to listen multicasts */
336         if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
337                 return -EPERM;
338
339         if (nlk->pid) {
340                 if (nladdr->nl_pid != nlk->pid)
341                         return -EINVAL;
342                 nlk->groups = nladdr->nl_groups;
343                 return 0;
344         }
345
346         if (nladdr->nl_pid == 0) {
347                 err = netlink_autobind(sock);
348                 if (err == 0)
349                         nlk->groups = nladdr->nl_groups;
350                 return err;
351         }
352
353         err = netlink_insert(sk, nladdr->nl_pid);
354         if (err == 0)
355                 nlk->groups = nladdr->nl_groups;
356         return err;
357 }
358
359 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
360                            int alen, int flags)
361 {
362         int err = 0;
363         struct sock *sk = sock->sk;
364         struct netlink_opt *nlk = nlk_sk(sk);
365         struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
366
367         if (addr->sa_family == AF_UNSPEC) {
368                 nlk->dst_pid    = 0;
369                 nlk->dst_groups = 0;
370                 return 0;
371         }
372         if (addr->sa_family != AF_NETLINK)
373                 return -EINVAL;
374
375         /* Only superuser is allowed to send multicasts */
376         if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
377                 return -EPERM;
378
379         if (!nlk->pid)
380                 err = netlink_autobind(sock);
381
382         if (err == 0) {
383                 nlk->dst_pid    = nladdr->nl_pid;
384                 nlk->dst_groups = nladdr->nl_groups;
385         }
386
387         return 0;
388 }
389
390 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
391 {
392         struct sock *sk = sock->sk;
393         struct netlink_opt *nlk = nlk_sk(sk);
394         struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
395         
396         nladdr->nl_family = AF_NETLINK;
397         nladdr->nl_pad = 0;
398         *addr_len = sizeof(*nladdr);
399
400         if (peer) {
401                 nladdr->nl_pid = nlk->dst_pid;
402                 nladdr->nl_groups = nlk->dst_groups;
403         } else {
404                 nladdr->nl_pid = nlk->pid;
405                 nladdr->nl_groups = nlk->groups;
406         }
407         return 0;
408 }
409
410 static void netlink_overrun(struct sock *sk)
411 {
412         if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
413                 sk->sk_err = ENOBUFS;
414                 sk->sk_error_report(sk);
415         }
416 }
417
418 struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
419 {
420         int protocol = ssk->sk_protocol;
421         struct sock *sock;
422         struct netlink_opt *nlk;
423
424         sock = netlink_lookup(protocol, pid);
425         if (!sock)
426                 return ERR_PTR(-ECONNREFUSED);
427
428         /* Don't bother queuing skb if kernel socket has no input function */
429         nlk = nlk_sk(sock);
430         if (nlk->pid == 0 && !nlk->data_ready) {
431                 sock_put(sock);
432                 return ERR_PTR(-ECONNREFUSED);
433         }
434         return sock;
435 }
436
437 struct sock *netlink_getsockbyfilp(struct file *filp)
438 {
439         struct inode *inode = filp->f_dentry->d_inode;
440         struct socket *socket;
441         struct sock *sock;
442
443         if (!inode->i_sock || !(socket = SOCKET_I(inode)))
444                 return ERR_PTR(-ENOTSOCK);
445
446         sock = socket->sk;
447         if (sock->sk_family != AF_NETLINK)
448                 return ERR_PTR(-EINVAL);
449
450         sock_hold(sock);
451         return sock;
452 }
453
454 /*
455  * Attach a skb to a netlink socket.
456  * The caller must hold a reference to the destination socket. On error, the
457  * reference is dropped. The skb is not send to the destination, just all
458  * all error checks are performed and memory in the queue is reserved.
459  * Return values:
460  * < 0: error. skb freed, reference to sock dropped.
461  * 0: continue
462  * 1: repeat lookup - reference dropped while waiting for socket memory.
463  */
464 int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
465 {
466         struct netlink_opt *nlk;
467
468         nlk = nlk_sk(sk);
469
470 #ifdef NL_EMULATE_DEV
471         if (nlk->handler)
472                 return 0;
473 #endif
474         if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
475             test_bit(0, &nlk->state)) {
476                 DECLARE_WAITQUEUE(wait, current);
477                 if (!timeo) {
478                         if (!nlk->pid)
479                                 netlink_overrun(sk);
480                         sock_put(sk);
481                         kfree_skb(skb);
482                         return -EAGAIN;
483                 }
484
485                 __set_current_state(TASK_INTERRUPTIBLE);
486                 add_wait_queue(&nlk->wait, &wait);
487
488                 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
489                      test_bit(0, &nlk->state)) &&
490                     !sock_flag(sk, SOCK_DEAD))
491                         timeo = schedule_timeout(timeo);
492
493                 __set_current_state(TASK_RUNNING);
494                 remove_wait_queue(&nlk->wait, &wait);
495                 sock_put(sk);
496
497                 if (signal_pending(current)) {
498                         kfree_skb(skb);
499                         return sock_intr_errno(timeo);
500                 }
501                 return 1;
502         }
503         skb_orphan(skb);
504         skb_set_owner_r(skb, sk);
505         return 0;
506 }
507
508 int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
509 {
510         struct netlink_opt *nlk;
511         int len = skb->len;
512
513         nlk = nlk_sk(sk);
514 #ifdef NL_EMULATE_DEV
515         if (nlk->handler) {
516                 skb_orphan(skb);
517                 len = nlk->handler(protocol, skb);
518                 sock_put(sk);
519                 return len;
520         }
521 #endif
522
523         skb_queue_tail(&sk->sk_receive_queue, skb);
524         sk->sk_data_ready(sk, len);
525         sock_put(sk);
526         return len;
527 }
528
529 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
530 {
531         kfree_skb(skb);
532         sock_put(sk);
533 }
534
535 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
536 {
537         struct sock *sk;
538         int err;
539         long timeo;
540
541         timeo = sock_sndtimeo(ssk, nonblock);
542 retry:
543         sk = netlink_getsockbypid(ssk, pid);
544         if (IS_ERR(sk)) {
545                 kfree_skb(skb);
546                 return PTR_ERR(sk);
547         }
548         err = netlink_attachskb(sk, skb, nonblock, timeo);
549         if (err == 1)
550                 goto retry;
551         if (err)
552                 return err;
553
554         return netlink_sendskb(sk, skb, ssk->sk_protocol);
555 }
556
557 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
558 {
559         struct netlink_opt *nlk = nlk_sk(sk);
560 #ifdef NL_EMULATE_DEV
561         if (nlk->handler) {
562                 skb_orphan(skb);
563                 nlk->handler(sk->sk_protocol, skb);
564                 return 0;
565         } else
566 #endif
567         if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
568             !test_bit(0, &nlk->state)) {
569                 skb_orphan(skb);
570                 skb_set_owner_r(skb, sk);
571                 skb_queue_tail(&sk->sk_receive_queue, skb);
572                 sk->sk_data_ready(sk, skb->len);
573                 return 0;
574         }
575         return -1;
576 }
577
578 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
579                       u32 group, int allocation)
580 {
581         struct sock *sk;
582         struct hlist_node *node;
583         struct sk_buff *skb2 = NULL;
584         int protocol = ssk->sk_protocol;
585         int failure = 0, delivered = 0;
586
587         /* While we sleep in clone, do not allow to change socket list */
588
589         netlink_lock_table();
590
591         sk_for_each(sk, node, &nl_table[protocol]) {
592                 struct netlink_opt *nlk = nlk_sk(sk);
593
594                 if (ssk == sk)
595                         continue;
596
597                 if (nlk->pid == pid || !(nlk->groups & group))
598                         continue;
599
600                 if (failure) {
601                         netlink_overrun(sk);
602                         continue;
603                 }
604
605                 sock_hold(sk);
606                 if (skb2 == NULL) {
607                         if (atomic_read(&skb->users) != 1) {
608                                 skb2 = skb_clone(skb, allocation);
609                         } else {
610                                 skb2 = skb;
611                                 atomic_inc(&skb->users);
612                         }
613                 }
614                 if (skb2 == NULL) {
615                         netlink_overrun(sk);
616                         /* Clone failed. Notify ALL listeners. */
617                         failure = 1;
618                 } else if (netlink_broadcast_deliver(sk, skb2)) {
619                         netlink_overrun(sk);
620                 } else {
621                         delivered = 1;
622                         skb2 = NULL;
623                 }
624                 sock_put(sk);
625         }
626
627         netlink_unlock_table();
628
629         if (skb2)
630                 kfree_skb(skb2);
631         kfree_skb(skb);
632
633         if (delivered)
634                 return 0;
635         if (failure)
636                 return -ENOBUFS;
637         return -ESRCH;
638 }
639
640 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
641 {
642         struct sock *sk;
643         struct hlist_node *node;
644         int protocol = ssk->sk_protocol;
645
646         read_lock(&nl_table_lock);
647         sk_for_each(sk, node, &nl_table[protocol]) {
648                 struct netlink_opt *nlk = nlk_sk(sk);
649                 if (ssk == sk)
650                         continue;
651
652                 if (nlk->pid == pid || !(nlk->groups & group))
653                         continue;
654
655                 sk->sk_err = code;
656                 sk->sk_error_report(sk);
657         }
658         read_unlock(&nl_table_lock);
659 }
660
661 static inline void netlink_rcv_wake(struct sock *sk)
662 {
663         struct netlink_opt *nlk = nlk_sk(sk);
664
665         if (!skb_queue_len(&sk->sk_receive_queue))
666                 clear_bit(0, &nlk->state);
667         if (!test_bit(0, &nlk->state))
668                 wake_up_interruptible(&nlk->wait);
669 }
670
671 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
672                            struct msghdr *msg, size_t len)
673 {
674         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
675         struct sock *sk = sock->sk;
676         struct netlink_opt *nlk = nlk_sk(sk);
677         struct sockaddr_nl *addr=msg->msg_name;
678         u32 dst_pid;
679         u32 dst_groups;
680         struct sk_buff *skb;
681         int err;
682         struct scm_cookie scm;
683
684         if (msg->msg_flags&MSG_OOB)
685                 return -EOPNOTSUPP;
686
687         if (NULL == siocb->scm)
688                 siocb->scm = &scm;
689         err = scm_send(sock, msg, siocb->scm);
690         if (err < 0)
691                 return err;
692
693         if (msg->msg_namelen) {
694                 if (addr->nl_family != AF_NETLINK)
695                         return -EINVAL;
696                 dst_pid = addr->nl_pid;
697                 dst_groups = addr->nl_groups;
698                 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
699                         return -EPERM;
700         } else {
701                 dst_pid = nlk->dst_pid;
702                 dst_groups = nlk->dst_groups;
703         }
704
705         if (!nlk->pid) {
706                 err = netlink_autobind(sock);
707                 if (err)
708                         goto out;
709         }
710
711         err = -EMSGSIZE;
712         if (len > sk->sk_sndbuf - 32)
713                 goto out;
714         err = -ENOBUFS;
715         skb = alloc_skb(len, GFP_KERNEL);
716         if (skb==NULL)
717                 goto out;
718
719         NETLINK_CB(skb).pid     = nlk->pid;
720         NETLINK_CB(skb).groups  = nlk->groups;
721         NETLINK_CB(skb).dst_pid = dst_pid;
722         NETLINK_CB(skb).dst_groups = dst_groups;
723         memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
724
725         /* What can I do? Netlink is asynchronous, so that
726            we will have to save current capabilities to
727            check them, when this message will be delivered
728            to corresponding kernel module.   --ANK (980802)
729          */
730
731         err = security_netlink_send(skb);
732         if (err) {
733                 kfree_skb(skb);
734                 goto out;
735         }
736
737         err = -EFAULT;
738         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
739                 kfree_skb(skb);
740                 goto out;
741         }
742
743         if (dst_groups) {
744                 atomic_inc(&skb->users);
745                 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
746         }
747         err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
748
749 out:
750         return err;
751 }
752
753 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
754                            struct msghdr *msg, size_t len,
755                            int flags)
756 {
757         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
758         struct scm_cookie scm;
759         struct sock *sk = sock->sk;
760         struct netlink_opt *nlk = nlk_sk(sk);
761         int noblock = flags&MSG_DONTWAIT;
762         size_t copied;
763         struct sk_buff *skb;
764         int err;
765
766         if (flags&MSG_OOB)
767                 return -EOPNOTSUPP;
768
769         copied = 0;
770
771         skb = skb_recv_datagram(sk,flags,noblock,&err);
772         if (skb==NULL)
773                 goto out;
774
775         msg->msg_namelen = 0;
776
777         copied = skb->len;
778         if (len < copied) {
779                 msg->msg_flags |= MSG_TRUNC;
780                 copied = len;
781         }
782
783         skb->h.raw = skb->data;
784         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
785
786         if (msg->msg_name) {
787                 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
788                 addr->nl_family = AF_NETLINK;
789                 addr->nl_pad    = 0;
790                 addr->nl_pid    = NETLINK_CB(skb).pid;
791                 addr->nl_groups = NETLINK_CB(skb).dst_groups;
792                 msg->msg_namelen = sizeof(*addr);
793         }
794
795         if (NULL == siocb->scm) {
796                 memset(&scm, 0, sizeof(scm));
797                 siocb->scm = &scm;
798         }
799         siocb->scm->creds = *NETLINK_CREDS(skb);
800         skb_free_datagram(sk, skb);
801
802         if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
803                 netlink_dump(sk);
804
805         scm_recv(sock, msg, siocb->scm, flags);
806
807 out:
808         netlink_rcv_wake(sk);
809         return err ? : copied;
810 }
811
812 static void netlink_data_ready(struct sock *sk, int len)
813 {
814         struct netlink_opt *nlk = nlk_sk(sk);
815
816         if (nlk->data_ready)
817                 nlk->data_ready(sk, len);
818         netlink_rcv_wake(sk);
819 }
820
821 /*
822  *      We export these functions to other modules. They provide a 
823  *      complete set of kernel non-blocking support for message
824  *      queueing.
825  */
826
827 struct sock *
828 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
829 {
830         struct socket *sock;
831         struct sock *sk;
832
833         if (unit<0 || unit>=MAX_LINKS)
834                 return NULL;
835
836         if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
837                 return NULL;
838
839         if (netlink_create(sock, unit) < 0) {
840                 sock_release(sock);
841                 return NULL;
842         }
843         sk = sock->sk;
844         sk->sk_data_ready = netlink_data_ready;
845         if (input)
846                 nlk_sk(sk)->data_ready = input;
847
848         netlink_insert(sk, 0);
849         return sk;
850 }
851
852 void netlink_set_nonroot(int protocol, unsigned flags)
853
854         if ((unsigned)protocol < MAX_LINKS) 
855                 nl_nonroot[protocol] = flags;
856
857
858 static void netlink_destroy_callback(struct netlink_callback *cb)
859 {
860         if (cb->skb)
861                 kfree_skb(cb->skb);
862         kfree(cb);
863 }
864
865 /*
866  * It looks a bit ugly.
867  * It would be better to create kernel thread.
868  */
869
870 static int netlink_dump(struct sock *sk)
871 {
872         struct netlink_opt *nlk = nlk_sk(sk);
873         struct netlink_callback *cb;
874         struct sk_buff *skb;
875         struct nlmsghdr *nlh;
876         int len;
877         
878         skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
879         if (!skb)
880                 return -ENOBUFS;
881
882         spin_lock(&nlk->cb_lock);
883
884         cb = nlk->cb;
885         if (cb == NULL) {
886                 spin_unlock(&nlk->cb_lock);
887                 kfree_skb(skb);
888                 return -EINVAL;
889         }
890
891         len = cb->dump(skb, cb);
892
893         if (len > 0) {
894                 spin_unlock(&nlk->cb_lock);
895                 skb_queue_tail(&sk->sk_receive_queue, skb);
896                 sk->sk_data_ready(sk, len);
897                 return 0;
898         }
899
900         nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
901         nlh->nlmsg_flags |= NLM_F_MULTI;
902         memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
903         skb_queue_tail(&sk->sk_receive_queue, skb);
904         sk->sk_data_ready(sk, skb->len);
905
906         cb->done(cb);
907         nlk->cb = NULL;
908         spin_unlock(&nlk->cb_lock);
909
910         netlink_destroy_callback(cb);
911         sock_put(sk);
912         return 0;
913 }
914
915 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
916                        struct nlmsghdr *nlh,
917                        int (*dump)(struct sk_buff *skb, struct netlink_callback*),
918                        int (*done)(struct netlink_callback*))
919 {
920         struct netlink_callback *cb;
921         struct sock *sk;
922         struct netlink_opt *nlk;
923
924         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
925         if (cb == NULL)
926                 return -ENOBUFS;
927
928         memset(cb, 0, sizeof(*cb));
929         cb->dump = dump;
930         cb->done = done;
931         cb->nlh = nlh;
932         atomic_inc(&skb->users);
933         cb->skb = skb;
934
935         sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
936         if (sk == NULL) {
937                 netlink_destroy_callback(cb);
938                 return -ECONNREFUSED;
939         }
940         nlk = nlk_sk(sk);
941         /* A dump is in progress... */
942         spin_lock(&nlk->cb_lock);
943         if (nlk->cb) {
944                 spin_unlock(&nlk->cb_lock);
945                 netlink_destroy_callback(cb);
946                 sock_put(sk);
947                 return -EBUSY;
948         }
949         nlk->cb = cb;
950         spin_unlock(&nlk->cb_lock);
951
952         netlink_dump(sk);
953         return 0;
954 }
955
956 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
957 {
958         struct sk_buff *skb;
959         struct nlmsghdr *rep;
960         struct nlmsgerr *errmsg;
961         int size;
962
963         if (err == 0)
964                 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
965         else
966                 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
967
968         skb = alloc_skb(size, GFP_KERNEL);
969         if (!skb) {
970                 struct sock *sk;
971
972                 sk = netlink_lookup(in_skb->sk->sk_protocol,
973                                     NETLINK_CB(in_skb).pid);
974                 if (sk) {
975                         sk->sk_err = ENOBUFS;
976                         sk->sk_error_report(sk);
977                         sock_put(sk);
978                 }
979                 return;
980         }
981
982         rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
983                           NLMSG_ERROR, sizeof(struct nlmsgerr));
984         errmsg = NLMSG_DATA(rep);
985         errmsg->error = err;
986         memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
987         netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
988 }
989
990
991 #ifdef NL_EMULATE_DEV
992
993 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
994
995 /*
996  *      Backward compatibility.
997  */     
998  
999 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
1000 {
1001         struct sock *sk = netlink_kernel_create(unit, NULL);
1002         if (sk == NULL)
1003                 return -ENOBUFS;
1004         nlk_sk(sk)->handler = function;
1005         write_lock_bh(&nl_emu_lock);
1006         netlink_kernel[unit] = sk->sk_socket;
1007         write_unlock_bh(&nl_emu_lock);
1008         return 0;
1009 }
1010
1011 void netlink_detach(int unit)
1012 {
1013         struct socket *sock;
1014
1015         write_lock_bh(&nl_emu_lock);
1016         sock = netlink_kernel[unit];
1017         netlink_kernel[unit] = NULL;
1018         write_unlock_bh(&nl_emu_lock);
1019
1020         sock_release(sock);
1021 }
1022
1023 int netlink_post(int unit, struct sk_buff *skb)
1024 {
1025         struct socket *sock;
1026
1027         read_lock(&nl_emu_lock);
1028         sock = netlink_kernel[unit];
1029         if (sock) {
1030                 struct sock *sk = sock->sk;
1031                 memset(skb->cb, 0, sizeof(skb->cb));
1032                 sock_hold(sk);
1033                 read_unlock(&nl_emu_lock);
1034
1035                 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
1036
1037                 sock_put(sk);
1038                 return 0;
1039         }
1040         read_unlock(&nl_emu_lock);
1041         return -EUNATCH;
1042 }
1043
1044 #endif
1045
1046 #ifdef CONFIG_PROC_FS
1047 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1048 {
1049         long i;
1050         struct sock *s;
1051         struct hlist_node *node;
1052         loff_t off = 0;
1053
1054         for (i=0; i<MAX_LINKS; i++) {
1055                 sk_for_each(s, node, &nl_table[i]) {
1056                         if (off == pos) {
1057                                 seq->private = (void *) i;
1058                                 return s;
1059                         }
1060                         ++off;
1061                 }
1062         }
1063         return NULL;
1064 }
1065
1066 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1067 {
1068         read_lock(&nl_table_lock);
1069         return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1070 }
1071
1072 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1073 {
1074         struct sock *s;
1075
1076         ++*pos;
1077
1078         if (v == SEQ_START_TOKEN)
1079                 return netlink_seq_socket_idx(seq, 0);
1080                 
1081         s = sk_next(v);
1082         if (!s) {
1083                 long i = (long)seq->private;
1084
1085                 while (++i < MAX_LINKS) {
1086                         s = sk_head(&nl_table[i]);
1087                         if (s) {
1088                                 seq->private = (void *) i;
1089                                 break;
1090                         }
1091                 }
1092         }
1093         return s;
1094 }
1095
1096 static void netlink_seq_stop(struct seq_file *seq, void *v)
1097 {
1098         read_unlock(&nl_table_lock);
1099 }
1100
1101
1102 static int netlink_seq_show(struct seq_file *seq, void *v)
1103 {
1104         if (v == SEQ_START_TOKEN)
1105                 seq_puts(seq,
1106                          "sk       Eth Pid    Groups   "
1107                          "Rmem     Wmem     Dump     Locks\n");
1108         else {
1109                 struct sock *s = v;
1110                 struct netlink_opt *nlk = nlk_sk(s);
1111
1112                 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1113                            s,
1114                            s->sk_protocol,
1115                            nlk->pid,
1116                            nlk->groups,
1117                            atomic_read(&s->sk_rmem_alloc),
1118                            atomic_read(&s->sk_wmem_alloc),
1119                            nlk->cb,
1120                            atomic_read(&s->sk_refcnt)
1121                         );
1122
1123         }
1124         return 0;
1125 }
1126
1127 static struct seq_operations netlink_seq_ops = {
1128         .start  = netlink_seq_start,
1129         .next   = netlink_seq_next,
1130         .stop   = netlink_seq_stop,
1131         .show   = netlink_seq_show,
1132 };
1133
1134
1135 static int netlink_seq_open(struct inode *inode, struct file *file)
1136 {
1137         return seq_open(file, &netlink_seq_ops);
1138 }
1139
1140 static struct file_operations netlink_seq_fops = {
1141         .owner          = THIS_MODULE,
1142         .open           = netlink_seq_open,
1143         .read           = seq_read,
1144         .llseek         = seq_lseek,
1145         .release        = seq_release,
1146 };
1147
1148 #endif
1149
1150 int netlink_register_notifier(struct notifier_block *nb)
1151 {
1152         return notifier_chain_register(&netlink_chain, nb);
1153 }
1154
1155 int netlink_unregister_notifier(struct notifier_block *nb)
1156 {
1157         return notifier_chain_unregister(&netlink_chain, nb);
1158 }
1159                 
1160 static struct proto_ops netlink_ops = {
1161         .family =       PF_NETLINK,
1162         .owner =        THIS_MODULE,
1163         .release =      netlink_release,
1164         .bind =         netlink_bind,
1165         .connect =      netlink_connect,
1166         .socketpair =   sock_no_socketpair,
1167         .accept =       sock_no_accept,
1168         .getname =      netlink_getname,
1169         .poll =         datagram_poll,
1170         .ioctl =        sock_no_ioctl,
1171         .listen =       sock_no_listen,
1172         .shutdown =     sock_no_shutdown,
1173         .setsockopt =   sock_no_setsockopt,
1174         .getsockopt =   sock_no_getsockopt,
1175         .sendmsg =      netlink_sendmsg,
1176         .recvmsg =      netlink_recvmsg,
1177         .mmap =         sock_no_mmap,
1178         .sendpage =     sock_no_sendpage,
1179 };
1180
1181 static struct net_proto_family netlink_family_ops = {
1182         .family = PF_NETLINK,
1183         .create = netlink_create,
1184         .owner  = THIS_MODULE,  /* for consistency 8) */
1185 };
1186
1187 static int __init netlink_proto_init(void)
1188 {
1189         struct sk_buff *dummy_skb;
1190
1191         if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) {
1192                 printk(KERN_CRIT "netlink_init: panic\n");
1193                 return -1;
1194         }
1195         sock_register(&netlink_family_ops);
1196 #ifdef CONFIG_PROC_FS
1197         proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1198 #endif
1199         /* The netlink device handler may be needed early. */ 
1200         rtnetlink_init();
1201         return 0;
1202 }
1203
1204 static void __exit netlink_proto_exit(void)
1205 {
1206        sock_unregister(PF_NETLINK);
1207        proc_net_remove("netlink");
1208 }
1209
1210 core_initcall(netlink_proto_init);
1211 module_exit(netlink_proto_exit);
1212
1213 MODULE_LICENSE("GPL");
1214
1215 MODULE_ALIAS_NETPROTO(PF_NETLINK);
1216
1217 EXPORT_SYMBOL(netlink_ack);
1218 EXPORT_SYMBOL(netlink_broadcast);
1219 EXPORT_SYMBOL(netlink_broadcast_deliver);
1220 EXPORT_SYMBOL(netlink_dump_start);
1221 EXPORT_SYMBOL(netlink_kernel_create);
1222 EXPORT_SYMBOL(netlink_register_notifier);
1223 EXPORT_SYMBOL(netlink_set_err);
1224 EXPORT_SYMBOL(netlink_set_nonroot);
1225 EXPORT_SYMBOL(netlink_unicast);
1226 EXPORT_SYMBOL(netlink_unregister_notifier);
1227
1228 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
1229 EXPORT_SYMBOL(netlink_attach);
1230 EXPORT_SYMBOL(netlink_detach);
1231 EXPORT_SYMBOL(netlink_post);
1232 #endif