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