This commit was manufactured by cvs2svn to create tag
[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         netlink_trim(skb, allocation);
804
805         /* While we sleep in clone, do not allow to change socket list */
806
807         netlink_lock_table();
808
809         sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
810                 do_one_broadcast(sk, &info);
811
812         netlink_unlock_table();
813
814         if (info.skb2)
815                 kfree_skb(info.skb2);
816         kfree_skb(skb);
817
818         if (info.delivered) {
819                 if (info.congested && (allocation & __GFP_WAIT))
820                         yield();
821                 return 0;
822         }
823         if (info.failure)
824                 return -ENOBUFS;
825         return -ESRCH;
826 }
827
828 struct netlink_set_err_data {
829         struct sock *exclude_sk;
830         u32 pid;
831         u32 group;
832         int code;
833 };
834
835 static inline int do_one_set_err(struct sock *sk,
836                                  struct netlink_set_err_data *p)
837 {
838         struct netlink_opt *nlk = nlk_sk(sk);
839
840         if (sk == p->exclude_sk)
841                 goto out;
842
843         if (nlk->pid == p->pid || !(nlk->groups & p->group))
844                 goto out;
845
846         sk->sk_err = p->code;
847         sk->sk_error_report(sk);
848 out:
849         return 0;
850 }
851
852 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
853 {
854         struct netlink_set_err_data info;
855         struct hlist_node *node;
856         struct sock *sk;
857
858         info.exclude_sk = ssk;
859         info.pid = pid;
860         info.group = group;
861         info.code = code;
862
863         read_lock(&nl_table_lock);
864
865         sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
866                 do_one_set_err(sk, &info);
867
868         read_unlock(&nl_table_lock);
869 }
870
871 static inline void netlink_rcv_wake(struct sock *sk)
872 {
873         struct netlink_opt *nlk = nlk_sk(sk);
874
875         if (!skb_queue_len(&sk->sk_receive_queue))
876                 clear_bit(0, &nlk->state);
877         if (!test_bit(0, &nlk->state))
878                 wake_up_interruptible(&nlk->wait);
879 }
880
881 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
882                            struct msghdr *msg, size_t len)
883 {
884         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
885         struct sock *sk = sock->sk;
886         struct netlink_opt *nlk = nlk_sk(sk);
887         struct sockaddr_nl *addr=msg->msg_name;
888         u32 dst_pid;
889         u32 dst_groups;
890         struct sk_buff *skb;
891         int err;
892         struct scm_cookie scm;
893
894         if (msg->msg_flags&MSG_OOB)
895                 return -EOPNOTSUPP;
896
897         if (NULL == siocb->scm)
898                 siocb->scm = &scm;
899         err = scm_send(sock, msg, siocb->scm);
900         if (err < 0)
901                 return err;
902
903         if (msg->msg_namelen) {
904                 if (addr->nl_family != AF_NETLINK)
905                         return -EINVAL;
906                 dst_pid = addr->nl_pid;
907                 dst_groups = addr->nl_groups;
908                 if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
909                         return -EPERM;
910         } else {
911                 dst_pid = nlk->dst_pid;
912                 dst_groups = nlk->dst_groups;
913         }
914
915         if (!nlk->pid) {
916                 err = netlink_autobind(sock);
917                 if (err)
918                         goto out;
919         }
920
921         err = -EMSGSIZE;
922         if (len > sk->sk_sndbuf - 32)
923                 goto out;
924         err = -ENOBUFS;
925         skb = alloc_skb(len, GFP_KERNEL);
926         if (skb==NULL)
927                 goto out;
928
929         NETLINK_CB(skb).pid     = nlk->pid;
930         NETLINK_CB(skb).groups  = nlk->groups;
931         NETLINK_CB(skb).dst_pid = dst_pid;
932         NETLINK_CB(skb).dst_groups = dst_groups;
933         memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
934
935         /* What can I do? Netlink is asynchronous, so that
936            we will have to save current capabilities to
937            check them, when this message will be delivered
938            to corresponding kernel module.   --ANK (980802)
939          */
940
941         err = -EFAULT;
942         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
943                 kfree_skb(skb);
944                 goto out;
945         }
946
947         err = security_netlink_send(sk, skb);
948         if (err) {
949                 kfree_skb(skb);
950                 goto out;
951         }
952
953         if (dst_groups) {
954                 atomic_inc(&skb->users);
955                 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
956         }
957         err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
958
959 out:
960         return err;
961 }
962
963 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
964                            struct msghdr *msg, size_t len,
965                            int flags)
966 {
967         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
968         struct scm_cookie scm;
969         struct sock *sk = sock->sk;
970         struct netlink_opt *nlk = nlk_sk(sk);
971         int noblock = flags&MSG_DONTWAIT;
972         size_t copied;
973         struct sk_buff *skb;
974         int err;
975
976         if (flags&MSG_OOB)
977                 return -EOPNOTSUPP;
978
979         copied = 0;
980
981         skb = skb_recv_datagram(sk,flags,noblock,&err);
982         if (skb==NULL)
983                 goto out;
984
985         msg->msg_namelen = 0;
986
987         copied = skb->len;
988         if (len < copied) {
989                 msg->msg_flags |= MSG_TRUNC;
990                 copied = len;
991         }
992
993         skb->h.raw = skb->data;
994         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
995
996         if (msg->msg_name) {
997                 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
998                 addr->nl_family = AF_NETLINK;
999                 addr->nl_pad    = 0;
1000                 addr->nl_pid    = NETLINK_CB(skb).pid;
1001                 addr->nl_groups = NETLINK_CB(skb).dst_groups;
1002                 msg->msg_namelen = sizeof(*addr);
1003         }
1004
1005         if (NULL == siocb->scm) {
1006                 memset(&scm, 0, sizeof(scm));
1007                 siocb->scm = &scm;
1008         }
1009         siocb->scm->creds = *NETLINK_CREDS(skb);
1010         skb_free_datagram(sk, skb);
1011
1012         if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
1013                 netlink_dump(sk);
1014
1015         scm_recv(sock, msg, siocb->scm, flags);
1016
1017 out:
1018         netlink_rcv_wake(sk);
1019         return err ? : copied;
1020 }
1021
1022 static void netlink_data_ready(struct sock *sk, int len)
1023 {
1024         struct netlink_opt *nlk = nlk_sk(sk);
1025
1026         if (nlk->data_ready)
1027                 nlk->data_ready(sk, len);
1028         netlink_rcv_wake(sk);
1029 }
1030
1031 /*
1032  *      We export these functions to other modules. They provide a 
1033  *      complete set of kernel non-blocking support for message
1034  *      queueing.
1035  */
1036
1037 struct sock *
1038 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
1039 {
1040         struct socket *sock;
1041         struct sock *sk;
1042
1043         if (!nl_table)
1044                 return NULL;
1045
1046         if (unit<0 || unit>=MAX_LINKS)
1047                 return NULL;
1048
1049         if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1050                 return NULL;
1051
1052         if (netlink_create(sock, unit) < 0) {
1053                 sock_release(sock);
1054                 return NULL;
1055         }
1056         sk = sock->sk;
1057         sk->sk_data_ready = netlink_data_ready;
1058         if (input)
1059                 nlk_sk(sk)->data_ready = input;
1060
1061         if (netlink_insert(sk, 0)) {
1062                 sock_release(sock);
1063                 return NULL;
1064         }
1065         return sk;
1066 }
1067
1068 void netlink_set_nonroot(int protocol, unsigned int flags)
1069
1070         if ((unsigned int)protocol < MAX_LINKS) 
1071                 nl_nonroot[protocol] = flags;
1072
1073
1074 static void netlink_destroy_callback(struct netlink_callback *cb)
1075 {
1076         if (cb->skb)
1077                 kfree_skb(cb->skb);
1078         kfree(cb);
1079 }
1080
1081 /*
1082  * It looks a bit ugly.
1083  * It would be better to create kernel thread.
1084  */
1085
1086 static int netlink_dump(struct sock *sk)
1087 {
1088         struct netlink_opt *nlk = nlk_sk(sk);
1089         struct netlink_callback *cb;
1090         struct sk_buff *skb;
1091         struct nlmsghdr *nlh;
1092         int len;
1093         
1094         skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
1095         if (!skb)
1096                 return -ENOBUFS;
1097
1098         spin_lock(&nlk->cb_lock);
1099
1100         cb = nlk->cb;
1101         if (cb == NULL) {
1102                 spin_unlock(&nlk->cb_lock);
1103                 kfree_skb(skb);
1104                 return -EINVAL;
1105         }
1106
1107         len = cb->dump(skb, cb);
1108
1109         if (len > 0) {
1110                 spin_unlock(&nlk->cb_lock);
1111                 skb_queue_tail(&sk->sk_receive_queue, skb);
1112                 sk->sk_data_ready(sk, len);
1113                 return 0;
1114         }
1115
1116         nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
1117         nlh->nlmsg_flags |= NLM_F_MULTI;
1118         memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
1119         skb_queue_tail(&sk->sk_receive_queue, skb);
1120         sk->sk_data_ready(sk, skb->len);
1121
1122         cb->done(cb);
1123         nlk->cb = NULL;
1124         spin_unlock(&nlk->cb_lock);
1125
1126         netlink_destroy_callback(cb);
1127         sock_put(sk);
1128         return 0;
1129 }
1130
1131 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1132                        struct nlmsghdr *nlh,
1133                        int (*dump)(struct sk_buff *skb, struct netlink_callback*),
1134                        int (*done)(struct netlink_callback*))
1135 {
1136         struct netlink_callback *cb;
1137         struct sock *sk;
1138         struct netlink_opt *nlk;
1139
1140         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1141         if (cb == NULL)
1142                 return -ENOBUFS;
1143
1144         memset(cb, 0, sizeof(*cb));
1145         cb->dump = dump;
1146         cb->done = done;
1147         cb->nlh = nlh;
1148         atomic_inc(&skb->users);
1149         cb->skb = skb;
1150
1151         sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1152         if (sk == NULL) {
1153                 netlink_destroy_callback(cb);
1154                 return -ECONNREFUSED;
1155         }
1156         nlk = nlk_sk(sk);
1157         /* A dump is in progress... */
1158         spin_lock(&nlk->cb_lock);
1159         if (nlk->cb) {
1160                 spin_unlock(&nlk->cb_lock);
1161                 netlink_destroy_callback(cb);
1162                 sock_put(sk);
1163                 return -EBUSY;
1164         }
1165         nlk->cb = cb;
1166         spin_unlock(&nlk->cb_lock);
1167
1168         netlink_dump(sk);
1169         return 0;
1170 }
1171
1172 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1173 {
1174         struct sk_buff *skb;
1175         struct nlmsghdr *rep;
1176         struct nlmsgerr *errmsg;
1177         int size;
1178
1179         if (err == 0)
1180                 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
1181         else
1182                 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
1183
1184         skb = alloc_skb(size, GFP_KERNEL);
1185         if (!skb) {
1186                 struct sock *sk;
1187
1188                 sk = netlink_lookup(in_skb->sk->sk_protocol,
1189                                     NETLINK_CB(in_skb).pid);
1190                 if (sk) {
1191                         sk->sk_err = ENOBUFS;
1192                         sk->sk_error_report(sk);
1193                         sock_put(sk);
1194                 }
1195                 return;
1196         }
1197
1198         rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
1199                           NLMSG_ERROR, sizeof(struct nlmsgerr));
1200         errmsg = NLMSG_DATA(rep);
1201         errmsg->error = err;
1202         memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
1203         netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1204 }
1205
1206
1207 #ifdef NL_EMULATE_DEV
1208
1209 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
1210
1211 /*
1212  *      Backward compatibility.
1213  */     
1214  
1215 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
1216 {
1217         struct sock *sk = netlink_kernel_create(unit, NULL);
1218         if (sk == NULL)
1219                 return -ENOBUFS;
1220         nlk_sk(sk)->handler = function;
1221         write_lock_bh(&nl_emu_lock);
1222         netlink_kernel[unit] = sk->sk_socket;
1223         write_unlock_bh(&nl_emu_lock);
1224         return 0;
1225 }
1226
1227 void netlink_detach(int unit)
1228 {
1229         struct socket *sock;
1230
1231         write_lock_bh(&nl_emu_lock);
1232         sock = netlink_kernel[unit];
1233         netlink_kernel[unit] = NULL;
1234         write_unlock_bh(&nl_emu_lock);
1235
1236         sock_release(sock);
1237 }
1238
1239 int netlink_post(int unit, struct sk_buff *skb)
1240 {
1241         struct socket *sock;
1242
1243         read_lock(&nl_emu_lock);
1244         sock = netlink_kernel[unit];
1245         if (sock) {
1246                 struct sock *sk = sock->sk;
1247                 memset(skb->cb, 0, sizeof(skb->cb));
1248                 sock_hold(sk);
1249                 read_unlock(&nl_emu_lock);
1250
1251                 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
1252
1253                 sock_put(sk);
1254                 return 0;
1255         }
1256         read_unlock(&nl_emu_lock);
1257         return -EUNATCH;
1258 }
1259
1260 #endif
1261
1262 #ifdef CONFIG_PROC_FS
1263 struct nl_seq_iter {
1264         int link;
1265         int hash_idx;
1266 };
1267
1268 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1269 {
1270         struct nl_seq_iter *iter = seq->private;
1271         int i, j;
1272         struct sock *s;
1273         struct hlist_node *node;
1274         loff_t off = 0;
1275
1276         for (i=0; i<MAX_LINKS; i++) {
1277                 struct nl_pid_hash *hash = &nl_table[i].hash;
1278
1279                 for (j = 0; j <= hash->mask; j++) {
1280                         sk_for_each(s, node, &hash->table[j]) {
1281                                 if (off == pos) {
1282                                         iter->link = i;
1283                                         iter->hash_idx = j;
1284                                         return s;
1285                                 }
1286                                 ++off;
1287                         }
1288                 }
1289         }
1290         return NULL;
1291 }
1292
1293 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1294 {
1295         read_lock(&nl_table_lock);
1296         return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1297 }
1298
1299 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1300 {
1301         struct sock *s;
1302         struct nl_seq_iter *iter;
1303         int i, j;
1304
1305         ++*pos;
1306
1307         if (v == SEQ_START_TOKEN)
1308                 return netlink_seq_socket_idx(seq, 0);
1309                 
1310         s = sk_next(v);
1311         if (s)
1312                 return s;
1313
1314         iter = seq->private;
1315         i = iter->link;
1316         j = iter->hash_idx + 1;
1317
1318         do {
1319                 struct nl_pid_hash *hash = &nl_table[i].hash;
1320
1321                 for (; j <= hash->mask; j++) {
1322                         s = sk_head(&hash->table[j]);
1323                         if (s) {
1324                                 iter->link = i;
1325                                 iter->hash_idx = j;
1326                                 return s;
1327                         }
1328                 }
1329
1330                 j = 0;
1331         } while (++i < MAX_LINKS);
1332
1333         return NULL;
1334 }
1335
1336 static void netlink_seq_stop(struct seq_file *seq, void *v)
1337 {
1338         read_unlock(&nl_table_lock);
1339 }
1340
1341
1342 static int netlink_seq_show(struct seq_file *seq, void *v)
1343 {
1344         if (v == SEQ_START_TOKEN)
1345                 seq_puts(seq,
1346                          "sk       Eth Pid    Groups   "
1347                          "Rmem     Wmem     Dump     Locks\n");
1348         else {
1349                 struct sock *s = v;
1350                 struct netlink_opt *nlk = nlk_sk(s);
1351
1352                 seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1353                            s,
1354                            s->sk_protocol,
1355                            nlk->pid,
1356                            nlk->groups,
1357                            atomic_read(&s->sk_rmem_alloc),
1358                            atomic_read(&s->sk_wmem_alloc),
1359                            nlk->cb,
1360                            atomic_read(&s->sk_refcnt)
1361                         );
1362
1363         }
1364         return 0;
1365 }
1366
1367 static struct seq_operations netlink_seq_ops = {
1368         .start  = netlink_seq_start,
1369         .next   = netlink_seq_next,
1370         .stop   = netlink_seq_stop,
1371         .show   = netlink_seq_show,
1372 };
1373
1374
1375 static int netlink_seq_open(struct inode *inode, struct file *file)
1376 {
1377         struct seq_file *seq;
1378         struct nl_seq_iter *iter;
1379         int err;
1380
1381         iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1382         if (!iter)
1383                 return -ENOMEM;
1384
1385         err = seq_open(file, &netlink_seq_ops);
1386         if (err) {
1387                 kfree(iter);
1388                 return err;
1389         }
1390
1391         memset(iter, 0, sizeof(*iter));
1392         seq = file->private_data;
1393         seq->private = iter;
1394         return 0;
1395 }
1396
1397 static struct file_operations netlink_seq_fops = {
1398         .owner          = THIS_MODULE,
1399         .open           = netlink_seq_open,
1400         .read           = seq_read,
1401         .llseek         = seq_lseek,
1402         .release        = seq_release_private,
1403 };
1404
1405 #endif
1406
1407 int netlink_register_notifier(struct notifier_block *nb)
1408 {
1409         return notifier_chain_register(&netlink_chain, nb);
1410 }
1411
1412 int netlink_unregister_notifier(struct notifier_block *nb)
1413 {
1414         return notifier_chain_unregister(&netlink_chain, nb);
1415 }
1416                 
1417 static struct proto_ops netlink_ops = {
1418         .family =       PF_NETLINK,
1419         .owner =        THIS_MODULE,
1420         .release =      netlink_release,
1421         .bind =         netlink_bind,
1422         .connect =      netlink_connect,
1423         .socketpair =   sock_no_socketpair,
1424         .accept =       sock_no_accept,
1425         .getname =      netlink_getname,
1426         .poll =         datagram_poll,
1427         .ioctl =        sock_no_ioctl,
1428         .listen =       sock_no_listen,
1429         .shutdown =     sock_no_shutdown,
1430         .setsockopt =   sock_no_setsockopt,
1431         .getsockopt =   sock_no_getsockopt,
1432         .sendmsg =      netlink_sendmsg,
1433         .recvmsg =      netlink_recvmsg,
1434         .mmap =         sock_no_mmap,
1435         .sendpage =     sock_no_sendpage,
1436 };
1437
1438 static struct net_proto_family netlink_family_ops = {
1439         .family = PF_NETLINK,
1440         .create = netlink_create,
1441         .owner  = THIS_MODULE,  /* for consistency 8) */
1442 };
1443
1444 extern void netlink_skb_parms_too_large(void);
1445
1446 static int __init netlink_proto_init(void)
1447 {
1448         struct sk_buff *dummy_skb;
1449         int i;
1450         unsigned long max;
1451         unsigned int order;
1452
1453         if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
1454                 netlink_skb_parms_too_large();
1455
1456         nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
1457         if (!nl_table) {
1458 enomem:
1459                 printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
1460                 return -ENOMEM;
1461         }
1462
1463         memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
1464
1465         if (num_physpages >= (128 * 1024))
1466                 max = num_physpages >> (21 - PAGE_SHIFT);
1467         else
1468                 max = num_physpages >> (23 - PAGE_SHIFT);
1469
1470         order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
1471         max = (1UL << order) / sizeof(struct hlist_head);
1472         order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
1473
1474         for (i = 0; i < MAX_LINKS; i++) {
1475                 struct nl_pid_hash *hash = &nl_table[i].hash;
1476
1477                 hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
1478                 if (!hash->table) {
1479                         while (i-- > 0)
1480                                 nl_pid_hash_free(nl_table[i].hash.table,
1481                                                  1 * sizeof(*hash->table));
1482                         kfree(nl_table);
1483                         goto enomem;
1484                 }
1485                 memset(hash->table, 0, 1 * sizeof(*hash->table));
1486                 hash->max_shift = order;
1487                 hash->shift = 0;
1488                 hash->mask = 0;
1489                 hash->rehash_time = jiffies;
1490         }
1491
1492         sock_register(&netlink_family_ops);
1493 #ifdef CONFIG_PROC_FS
1494         proc_net_fops_create("netlink", 0, &netlink_seq_fops);
1495 #endif
1496         /* The netlink device handler may be needed early. */ 
1497         rtnetlink_init();
1498         return 0;
1499 }
1500
1501 static void __exit netlink_proto_exit(void)
1502 {
1503        sock_unregister(PF_NETLINK);
1504        proc_net_remove("netlink");
1505        kfree(nl_table);
1506        nl_table = NULL;
1507 }
1508
1509 core_initcall(netlink_proto_init);
1510 module_exit(netlink_proto_exit);
1511
1512 MODULE_LICENSE("GPL");
1513
1514 MODULE_ALIAS_NETPROTO(PF_NETLINK);
1515
1516 EXPORT_SYMBOL(netlink_ack);
1517 EXPORT_SYMBOL(netlink_broadcast);
1518 EXPORT_SYMBOL(netlink_dump_start);
1519 EXPORT_SYMBOL(netlink_kernel_create);
1520 EXPORT_SYMBOL(netlink_register_notifier);
1521 EXPORT_SYMBOL(netlink_set_err);
1522 EXPORT_SYMBOL(netlink_set_nonroot);
1523 EXPORT_SYMBOL(netlink_unicast);
1524 EXPORT_SYMBOL(netlink_unregister_notifier);
1525
1526 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
1527 EXPORT_SYMBOL(netlink_attach);
1528 EXPORT_SYMBOL(netlink_detach);
1529 EXPORT_SYMBOL(netlink_post);
1530 #endif