VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 static 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                 sk->sk_state    = NETLINK_UNCONNECTED;
369                 nlk->dst_pid    = 0;
370                 nlk->dst_groups = 0;
371                 return 0;
372         }
373         if (addr->sa_family != AF_NETLINK)
374                 return -EINVAL;
375
376         /* Only superuser is allowed to send multicasts */
377         if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
378                 return -EPERM;
379
380         if (!nlk->pid)
381                 err = netlink_autobind(sock);
382
383         if (err == 0) {
384                 sk->sk_state    = NETLINK_CONNECTED;
385                 nlk->dst_pid    = nladdr->nl_pid;
386                 nlk->dst_groups = nladdr->nl_groups;
387         }
388
389         return err;
390 }
391
392 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
393 {
394         struct sock *sk = sock->sk;
395         struct netlink_opt *nlk = nlk_sk(sk);
396         struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
397         
398         nladdr->nl_family = AF_NETLINK;
399         nladdr->nl_pad = 0;
400         *addr_len = sizeof(*nladdr);
401
402         if (peer) {
403                 nladdr->nl_pid = nlk->dst_pid;
404                 nladdr->nl_groups = nlk->dst_groups;
405         } else {
406                 nladdr->nl_pid = nlk->pid;
407                 nladdr->nl_groups = nlk->groups;
408         }
409         return 0;
410 }
411
412 static void netlink_overrun(struct sock *sk)
413 {
414         if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
415                 sk->sk_err = ENOBUFS;
416                 sk->sk_error_report(sk);
417         }
418 }
419
420 struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
421 {
422         int protocol = ssk->sk_protocol;
423         struct sock *sock;
424         struct netlink_opt *nlk;
425
426         sock = netlink_lookup(protocol, pid);
427         if (!sock)
428                 return ERR_PTR(-ECONNREFUSED);
429
430         /* Don't bother queuing skb if kernel socket has no input function */
431         nlk = nlk_sk(sock);
432         if ((nlk->pid == 0 && !nlk->data_ready) ||
433             (sock->sk_state == NETLINK_CONNECTED &&
434              nlk->dst_pid != nlk_sk(ssk)->pid)) {
435                 sock_put(sock);
436                 return ERR_PTR(-ECONNREFUSED);
437         }
438         return sock;
439 }
440
441 struct sock *netlink_getsockbyfilp(struct file *filp)
442 {
443         struct inode *inode = filp->f_dentry->d_inode;
444         struct socket *socket;
445         struct sock *sock;
446
447         if (!inode->i_sock || !(socket = SOCKET_I(inode)))
448                 return ERR_PTR(-ENOTSOCK);
449
450         sock = socket->sk;
451         if (sock->sk_family != AF_NETLINK)
452                 return ERR_PTR(-EINVAL);
453
454         sock_hold(sock);
455         return sock;
456 }
457
458 /*
459  * Attach a skb to a netlink socket.
460  * The caller must hold a reference to the destination socket. On error, the
461  * reference is dropped. The skb is not send to the destination, just all
462  * all error checks are performed and memory in the queue is reserved.
463  * Return values:
464  * < 0: error. skb freed, reference to sock dropped.
465  * 0: continue
466  * 1: repeat lookup - reference dropped while waiting for socket memory.
467  */
468 int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
469 {
470         struct netlink_opt *nlk;
471
472         nlk = nlk_sk(sk);
473
474 #ifdef NL_EMULATE_DEV
475         if (nlk->handler)
476                 return 0;
477 #endif
478         if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
479             test_bit(0, &nlk->state)) {
480                 DECLARE_WAITQUEUE(wait, current);
481                 if (!timeo) {
482                         if (!nlk->pid)
483                                 netlink_overrun(sk);
484                         sock_put(sk);
485                         kfree_skb(skb);
486                         return -EAGAIN;
487                 }
488
489                 __set_current_state(TASK_INTERRUPTIBLE);
490                 add_wait_queue(&nlk->wait, &wait);
491
492                 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
493                      test_bit(0, &nlk->state)) &&
494                     !sock_flag(sk, SOCK_DEAD))
495                         timeo = schedule_timeout(timeo);
496
497                 __set_current_state(TASK_RUNNING);
498                 remove_wait_queue(&nlk->wait, &wait);
499                 sock_put(sk);
500
501                 if (signal_pending(current)) {
502                         kfree_skb(skb);
503                         return sock_intr_errno(timeo);
504                 }
505                 return 1;
506         }
507         skb_orphan(skb);
508         skb_set_owner_r(skb, sk);
509         return 0;
510 }
511
512 int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
513 {
514         struct netlink_opt *nlk;
515         int len = skb->len;
516
517         nlk = nlk_sk(sk);
518 #ifdef NL_EMULATE_DEV
519         if (nlk->handler) {
520                 skb_orphan(skb);
521                 len = nlk->handler(protocol, skb);
522                 sock_put(sk);
523                 return len;
524         }
525 #endif
526
527         skb_queue_tail(&sk->sk_receive_queue, skb);
528         sk->sk_data_ready(sk, len);
529         sock_put(sk);
530         return len;
531 }
532
533 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
534 {
535         kfree_skb(skb);
536         sock_put(sk);
537 }
538
539 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
540 {
541         struct sock *sk;
542         int err;
543         long timeo;
544
545         timeo = sock_sndtimeo(ssk, nonblock);
546 retry:
547         sk = netlink_getsockbypid(ssk, pid);
548         if (IS_ERR(sk)) {
549                 kfree_skb(skb);
550                 return PTR_ERR(sk);
551         }
552         err = netlink_attachskb(sk, skb, nonblock, timeo);
553         if (err == 1)
554                 goto retry;
555         if (err)
556                 return err;
557
558         return netlink_sendskb(sk, skb, ssk->sk_protocol);
559 }
560
561 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
562 {
563         struct netlink_opt *nlk = nlk_sk(sk);
564 #ifdef NL_EMULATE_DEV
565         if (nlk->handler) {
566                 skb_orphan(skb);
567                 nlk->handler(sk->sk_protocol, skb);
568                 return 0;
569         } else
570 #endif
571         if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
572             !test_bit(0, &nlk->state)) {
573                 skb_orphan(skb);
574                 skb_set_owner_r(skb, sk);
575                 skb_queue_tail(&sk->sk_receive_queue, skb);
576                 sk->sk_data_ready(sk, skb->len);
577                 return 0;
578         }
579         return -1;
580 }
581
582 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
583                       u32 group, int allocation)
584 {
585         struct sock *sk;
586         struct hlist_node *node;
587         struct sk_buff *skb2 = NULL;
588         int protocol = ssk->sk_protocol;
589         int failure = 0, delivered = 0;
590
591         /* While we sleep in clone, do not allow to change socket list */
592
593         netlink_lock_table();
594
595         sk_for_each(sk, node, &nl_table[protocol]) {
596                 struct netlink_opt *nlk = nlk_sk(sk);
597
598                 if (ssk == sk)
599                         continue;
600
601                 if (nlk->pid == pid || !(nlk->groups & group))
602                         continue;
603
604                 if (failure) {
605                         netlink_overrun(sk);
606                         continue;
607                 }
608
609                 sock_hold(sk);
610                 if (skb2 == NULL) {
611                         if (atomic_read(&skb->users) != 1) {
612                                 skb2 = skb_clone(skb, allocation);
613                         } else {
614                                 skb2 = skb;
615                                 atomic_inc(&skb->users);
616                         }
617                 }
618                 if (skb2 == NULL) {
619                         netlink_overrun(sk);
620                         /* Clone failed. Notify ALL listeners. */
621                         failure = 1;
622                 } else if (netlink_broadcast_deliver(sk, skb2)) {
623                         netlink_overrun(sk);
624                 } else {
625                         delivered = 1;
626                         skb2 = NULL;
627                 }
628                 sock_put(sk);
629         }
630
631         netlink_unlock_table();
632
633         if (skb2)
634                 kfree_skb(skb2);
635         kfree_skb(skb);
636
637         if (delivered)
638                 return 0;
639         if (failure)
640                 return -ENOBUFS;
641         return -ESRCH;
642 }
643
644 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
645 {
646         struct sock *sk;
647         struct hlist_node *node;
648         int protocol = ssk->sk_protocol;
649
650         read_lock(&nl_table_lock);
651         sk_for_each(sk, node, &nl_table[protocol]) {
652                 struct netlink_opt *nlk = nlk_sk(sk);
653                 if (ssk == sk)
654                         continue;
655
656                 if (nlk->pid == pid || !(nlk->groups & group))
657                         continue;
658
659                 sk->sk_err = code;
660                 sk->sk_error_report(sk);
661         }
662         read_unlock(&nl_table_lock);
663 }
664
665 static inline void netlink_rcv_wake(struct sock *sk)
666 {
667         struct netlink_opt *nlk = nlk_sk(sk);
668
669         if (!skb_queue_len(&sk->sk_receive_queue))
670                 clear_bit(0, &nlk->state);
671         if (!test_bit(0, &nlk->state))
672                 wake_up_interruptible(&nlk->wait);
673 }
674
675 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
676                            struct msghdr *msg, size_t len)
677 {
678         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
679         struct sock *sk = sock->sk;
680         struct netlink_opt *nlk = nlk_sk(sk);
681         struct sockaddr_nl *addr=msg->msg_name;
682         u32 dst_pid;
683         u32 dst_groups;
684         struct sk_buff *skb;
685         int err;
686         struct scm_cookie scm;
687
688         if (msg->msg_flags&MSG_OOB)
689                 return -EOPNOTSUPP;
690
691         if (NULL == siocb->scm)
692                 siocb->scm = &scm;
693         err = scm_send(sock, msg, siocb->scm);
694         if (err < 0)
695                 return err;
696
697         if (msg->msg_namelen) {
698                 if (addr->nl_family != AF_NETLINK)
699                         return -EINVAL;
700                 dst_pid = addr->nl_pid;
701                 dst_groups = addr->nl_groups;
702                 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
703                         return -EPERM;
704         } else {
705                 dst_pid = nlk->dst_pid;
706                 dst_groups = nlk->dst_groups;
707         }
708
709         if (!nlk->pid) {
710                 err = netlink_autobind(sock);
711                 if (err)
712                         goto out;
713         }
714
715         err = -EMSGSIZE;
716         if (len > sk->sk_sndbuf - 32)
717                 goto out;
718         err = -ENOBUFS;
719         skb = alloc_skb(len, GFP_KERNEL);
720         if (skb==NULL)
721                 goto out;
722
723         NETLINK_CB(skb).pid     = nlk->pid;
724         NETLINK_CB(skb).groups  = nlk->groups;
725         NETLINK_CB(skb).dst_pid = dst_pid;
726         NETLINK_CB(skb).dst_groups = dst_groups;
727         memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
728
729         /* What can I do? Netlink is asynchronous, so that
730            we will have to save current capabilities to
731            check them, when this message will be delivered
732            to corresponding kernel module.   --ANK (980802)
733          */
734
735         err = -EFAULT;
736         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
737                 kfree_skb(skb);
738                 goto out;
739         }
740
741         err = security_netlink_send(sk, skb);
742         if (err) {
743                 kfree_skb(skb);
744                 goto out;
745         }
746
747         if (dst_groups) {
748                 atomic_inc(&skb->users);
749                 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
750         }
751         err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
752
753 out:
754         return err;
755 }
756
757 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
758                            struct msghdr *msg, size_t len,
759                            int flags)
760 {
761         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
762         struct scm_cookie scm;
763         struct sock *sk = sock->sk;
764         struct netlink_opt *nlk = nlk_sk(sk);
765         int noblock = flags&MSG_DONTWAIT;
766         size_t copied;
767         struct sk_buff *skb;
768         int err;
769
770         if (flags&MSG_OOB)
771                 return -EOPNOTSUPP;
772
773         copied = 0;
774
775         skb = skb_recv_datagram(sk,flags,noblock,&err);
776         if (skb==NULL)
777                 goto out;
778
779         msg->msg_namelen = 0;
780
781         copied = skb->len;
782         if (len < copied) {
783                 msg->msg_flags |= MSG_TRUNC;
784                 copied = len;
785         }
786
787         skb->h.raw = skb->data;
788         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
789
790         if (msg->msg_name) {
791                 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
792                 addr->nl_family = AF_NETLINK;
793                 addr->nl_pad    = 0;
794                 addr->nl_pid    = NETLINK_CB(skb).pid;
795                 addr->nl_groups = NETLINK_CB(skb).dst_groups;
796                 msg->msg_namelen = sizeof(*addr);
797         }
798
799         if (NULL == siocb->scm) {
800                 memset(&scm, 0, sizeof(scm));
801                 siocb->scm = &scm;
802         }
803         siocb->scm->creds = *NETLINK_CREDS(skb);
804         skb_free_datagram(sk, skb);
805
806         if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
807                 netlink_dump(sk);
808
809         scm_recv(sock, msg, siocb->scm, flags);
810
811 out:
812         netlink_rcv_wake(sk);
813         return err ? : copied;
814 }
815
816 static void netlink_data_ready(struct sock *sk, int len)
817 {
818         struct netlink_opt *nlk = nlk_sk(sk);
819
820         if (nlk->data_ready)
821                 nlk->data_ready(sk, len);
822         netlink_rcv_wake(sk);
823 }
824
825 /*
826  *      We export these functions to other modules. They provide a 
827  *      complete set of kernel non-blocking support for message
828  *      queueing.
829  */
830
831 struct sock *
832 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
833 {
834         struct socket *sock;
835         struct sock *sk;
836
837         if (unit<0 || unit>=MAX_LINKS)
838                 return NULL;
839
840         if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
841                 return NULL;
842
843         if (netlink_create(sock, unit) < 0) {
844                 sock_release(sock);
845                 return NULL;
846         }
847         sk = sock->sk;
848         sk->sk_data_ready = netlink_data_ready;
849         if (input)
850                 nlk_sk(sk)->data_ready = input;
851
852         netlink_insert(sk, 0);
853         return sk;
854 }
855
856 void netlink_set_nonroot(int protocol, unsigned flags)
857
858         if ((unsigned)protocol < MAX_LINKS) 
859                 nl_nonroot[protocol] = flags;
860
861
862 static void netlink_destroy_callback(struct netlink_callback *cb)
863 {
864         if (cb->skb)
865                 kfree_skb(cb->skb);
866         kfree(cb);
867 }
868
869 /*
870  * It looks a bit ugly.
871  * It would be better to create kernel thread.
872  */
873
874 static int netlink_dump(struct sock *sk)
875 {
876         struct netlink_opt *nlk = nlk_sk(sk);
877         struct netlink_callback *cb;
878         struct sk_buff *skb;
879         struct nlmsghdr *nlh;
880         int len;
881         
882         skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
883         if (!skb)
884                 return -ENOBUFS;
885
886         spin_lock(&nlk->cb_lock);
887
888         cb = nlk->cb;
889         if (cb == NULL) {
890                 spin_unlock(&nlk->cb_lock);
891                 kfree_skb(skb);
892                 return -EINVAL;
893         }
894
895         len = cb->dump(skb, cb);
896
897         if (len > 0) {
898                 spin_unlock(&nlk->cb_lock);
899                 skb_queue_tail(&sk->sk_receive_queue, skb);
900                 sk->sk_data_ready(sk, len);
901                 return 0;
902         }
903
904         nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
905         nlh->nlmsg_flags |= NLM_F_MULTI;
906         memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
907         skb_queue_tail(&sk->sk_receive_queue, skb);
908         sk->sk_data_ready(sk, skb->len);
909
910         cb->done(cb);
911         nlk->cb = NULL;
912         spin_unlock(&nlk->cb_lock);
913
914         netlink_destroy_callback(cb);
915         sock_put(sk);
916         return 0;
917 }
918
919 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
920                        struct nlmsghdr *nlh,
921                        int (*dump)(struct sk_buff *skb, struct netlink_callback*),
922                        int (*done)(struct netlink_callback*))
923 {
924         struct netlink_callback *cb;
925         struct sock *sk;
926         struct netlink_opt *nlk;
927
928         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
929         if (cb == NULL)
930                 return -ENOBUFS;
931
932         memset(cb, 0, sizeof(*cb));
933         cb->dump = dump;
934         cb->done = done;
935         cb->nlh = nlh;
936         atomic_inc(&skb->users);
937         cb->skb = skb;
938
939         sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
940         if (sk == NULL) {
941                 netlink_destroy_callback(cb);
942                 return -ECONNREFUSED;
943         }
944         nlk = nlk_sk(sk);
945         /* A dump is in progress... */
946         spin_lock(&nlk->cb_lock);
947         if (nlk->cb) {
948                 spin_unlock(&nlk->cb_lock);
949                 netlink_destroy_callback(cb);
950                 sock_put(sk);
951                 return -EBUSY;
952         }
953         nlk->cb = cb;
954         spin_unlock(&nlk->cb_lock);
955
956         netlink_dump(sk);
957         return 0;
958 }
959
960 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
961 {
962         struct sk_buff *skb;
963         struct nlmsghdr *rep;
964         struct nlmsgerr *errmsg;
965         int size;
966
967         if (err == 0)
968                 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
969         else
970                 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
971
972         skb = alloc_skb(size, GFP_KERNEL);
973         if (!skb) {
974                 struct sock *sk;
975
976                 sk = netlink_lookup(in_skb->sk->sk_protocol,
977                                     NETLINK_CB(in_skb).pid);
978                 if (sk) {
979                         sk->sk_err = ENOBUFS;
980                         sk->sk_error_report(sk);
981                         sock_put(sk);
982                 }
983                 return;
984         }
985
986         rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
987                           NLMSG_ERROR, sizeof(struct nlmsgerr));
988         errmsg = NLMSG_DATA(rep);
989         errmsg->error = err;
990         memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
991         netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
992 }
993
994
995 #ifdef NL_EMULATE_DEV
996
997 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
998
999 /*
1000  *      Backward compatibility.
1001  */     
1002  
1003 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
1004 {
1005         struct sock *sk = netlink_kernel_create(unit, NULL);
1006         if (sk == NULL)
1007                 return -ENOBUFS;
1008         nlk_sk(sk)->handler = function;
1009         write_lock_bh(&nl_emu_lock);
1010         netlink_kernel[unit] = sk->sk_socket;
1011         write_unlock_bh(&nl_emu_lock);
1012         return 0;
1013 }
1014
1015 void netlink_detach(int unit)
1016 {
1017         struct socket *sock;
1018
1019         write_lock_bh(&nl_emu_lock);
1020         sock = netlink_kernel[unit];
1021         netlink_kernel[unit] = NULL;
1022         write_unlock_bh(&nl_emu_lock);
1023
1024         sock_release(sock);
1025 }
1026
1027 int netlink_post(int unit, struct sk_buff *skb)
1028 {
1029         struct socket *sock;
1030
1031         read_lock(&nl_emu_lock);
1032         sock = netlink_kernel[unit];
1033         if (sock) {
1034                 struct sock *sk = sock->sk;
1035                 memset(skb->cb, 0, sizeof(skb->cb));
1036                 sock_hold(sk);
1037                 read_unlock(&nl_emu_lock);
1038
1039                 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
1040
1041                 sock_put(sk);
1042                 return 0;
1043         }
1044         read_unlock(&nl_emu_lock);
1045         return -EUNATCH;
1046 }
1047
1048 #endif
1049
1050 #ifdef CONFIG_PROC_FS
1051 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1052 {
1053         long i;
1054         struct sock *s;
1055         struct hlist_node *node;
1056         loff_t off = 0;
1057
1058         for (i=0; i<MAX_LINKS; i++) {
1059                 sk_for_each(s, node, &nl_table[i]) {
1060                         if (off == pos) {
1061                                 seq->private = (void *) i;
1062                                 return s;
1063                         }
1064                         ++off;
1065                 }
1066         }
1067         return NULL;
1068 }
1069
1070 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1071 {
1072         read_lock(&nl_table_lock);
1073         return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1074 }
1075
1076 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1077 {
1078         struct sock *s;
1079
1080         ++*pos;
1081
1082         if (v == SEQ_START_TOKEN)
1083                 return netlink_seq_socket_idx(seq, 0);
1084                 
1085         s = sk_next(v);
1086         if (!s) {
1087                 long i = (long)seq->private;
1088
1089                 while (++i < MAX_LINKS) {
1090                         s = sk_head(&nl_table[i]);
1091                         if (s) {
1092                                 seq->private = (void *) i;
1093                                 break;
1094                         }
1095                 }
1096         }
1097         return s;
1098 }
1099
1100 static void netlink_seq_stop(struct seq_file *seq, void *v)
1101 {
1102         read_unlock(&nl_table_lock);
1103 }
1104
1105
1106 static int netlink_seq_show(struct seq_file *seq, void *v)
1107 {
1108         if (v == SEQ_START_TOKEN)
1109                 seq_puts(seq,
1110                          "sk       Eth Pid    Groups   "
1111                          "Rmem     Wmem     Dump     Locks\n");
1112         else {
1113                 struct sock *s = v;
1114                 struct netlink_opt *nlk = nlk_sk(s);
1115
1116                 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1117                            s,
1118                            s->sk_protocol,
1119                            nlk->pid,
1120                            nlk->groups,
1121                            atomic_read(&s->sk_rmem_alloc),
1122                            atomic_read(&s->sk_wmem_alloc),
1123                            nlk->cb,
1124                            atomic_read(&s->sk_refcnt)
1125                         );
1126
1127         }
1128         return 0;
1129 }
1130
1131 static struct seq_operations netlink_seq_ops = {
1132         .start  = netlink_seq_start,
1133         .next   = netlink_seq_next,
1134         .stop   = netlink_seq_stop,
1135         .show   = netlink_seq_show,
1136 };
1137
1138
1139 static int netlink_seq_open(struct inode *inode, struct file *file)
1140 {
1141         return seq_open(file, &netlink_seq_ops);
1142 }
1143
1144 static struct file_operations netlink_seq_fops = {
1145         .owner          = THIS_MODULE,
1146         .open           = netlink_seq_open,
1147         .read           = seq_read,
1148         .llseek         = seq_lseek,
1149         .release        = seq_release,
1150 };
1151
1152 #endif
1153
1154 int netlink_register_notifier(struct notifier_block *nb)
1155 {
1156         return notifier_chain_register(&netlink_chain, nb);
1157 }
1158
1159 int netlink_unregister_notifier(struct notifier_block *nb)
1160 {
1161         return notifier_chain_unregister(&netlink_chain, nb);
1162 }
1163                 
1164 static struct proto_ops netlink_ops = {
1165         .family =       PF_NETLINK,
1166         .owner =        THIS_MODULE,
1167         .release =      netlink_release,
1168         .bind =         netlink_bind,
1169         .connect =      netlink_connect,
1170         .socketpair =   sock_no_socketpair,
1171         .accept =       sock_no_accept,
1172         .getname =      netlink_getname,
1173         .poll =         datagram_poll,
1174         .ioctl =        sock_no_ioctl,
1175         .listen =       sock_no_listen,
1176         .shutdown =     sock_no_shutdown,
1177         .setsockopt =   sock_no_setsockopt,
1178         .getsockopt =   sock_no_getsockopt,
1179         .sendmsg =      netlink_sendmsg,
1180         .recvmsg =      netlink_recvmsg,
1181         .mmap =         sock_no_mmap,
1182         .sendpage =     sock_no_sendpage,
1183 };
1184
1185 static struct net_proto_family netlink_family_ops = {
1186         .family = PF_NETLINK,
1187         .create = netlink_create,
1188         .owner  = THIS_MODULE,  /* for consistency 8) */
1189 };
1190
1191 static int __init netlink_proto_init(void)
1192 {
1193         struct sk_buff *dummy_skb;
1194
1195         if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) {
1196                 printk(KERN_CRIT "netlink_init: panic\n");
1197                 return -1;
1198         }
1199         sock_register(&netlink_family_ops);
1200 #ifdef CONFIG_PROC_FS
1201         proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1202 #endif
1203         /* The netlink device handler may be needed early. */ 
1204         rtnetlink_init();
1205         return 0;
1206 }
1207
1208 static void __exit netlink_proto_exit(void)
1209 {
1210        sock_unregister(PF_NETLINK);
1211        proc_net_remove("netlink");
1212 }
1213
1214 core_initcall(netlink_proto_init);
1215 module_exit(netlink_proto_exit);
1216
1217 MODULE_LICENSE("GPL");
1218
1219 MODULE_ALIAS_NETPROTO(PF_NETLINK);
1220
1221 EXPORT_SYMBOL(netlink_ack);
1222 EXPORT_SYMBOL(netlink_broadcast);
1223 EXPORT_SYMBOL(netlink_broadcast_deliver);
1224 EXPORT_SYMBOL(netlink_dump_start);
1225 EXPORT_SYMBOL(netlink_kernel_create);
1226 EXPORT_SYMBOL(netlink_register_notifier);
1227 EXPORT_SYMBOL(netlink_set_err);
1228 EXPORT_SYMBOL(netlink_set_nonroot);
1229 EXPORT_SYMBOL(netlink_unicast);
1230 EXPORT_SYMBOL(netlink_unregister_notifier);
1231
1232 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
1233 EXPORT_SYMBOL(netlink_attach);
1234 EXPORT_SYMBOL(netlink_detach);
1235 EXPORT_SYMBOL(netlink_post);
1236 #endif