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