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