]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - net/netlink/af_netlink.c
01e944a017a4db6be96c3d4a8e7adc8d7890b34e
[can-eth-gw-linux.git] / net / netlink / af_netlink.c
1 /*
2  * NETLINK      Kernel-user communication protocol.
3  *
4  *              Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
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  * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17  *                               - inc module use count of module that owns
18  *                                 the kernel socket in case userspace opens
19  *                                 socket of same protocol
20  *                               - remove all module support, since netlink is
21  *                                 mandatory if CONFIG_NET=y these days
22  */
23
24 #include <linux/module.h>
25
26 #include <linux/capability.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/socket.h>
35 #include <linux/un.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
40 #include <linux/fs.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/notifier.h>
49 #include <linux/security.h>
50 #include <linux/jhash.h>
51 #include <linux/jiffies.h>
52 #include <linux/random.h>
53 #include <linux/bitops.h>
54 #include <linux/mm.h>
55 #include <linux/types.h>
56 #include <linux/audit.h>
57 #include <linux/mutex.h>
58
59 #include <net/net_namespace.h>
60 #include <net/sock.h>
61 #include <net/scm.h>
62 #include <net/netlink.h>
63
64 #define NLGRPSZ(x)      (ALIGN(x, sizeof(unsigned long) * 8) / 8)
65 #define NLGRPLONGS(x)   (NLGRPSZ(x)/sizeof(unsigned long))
66
67 struct netlink_sock {
68         /* struct sock has to be the first member of netlink_sock */
69         struct sock             sk;
70         u32                     portid;
71         u32                     dst_portid;
72         u32                     dst_group;
73         u32                     flags;
74         u32                     subscriptions;
75         u32                     ngroups;
76         unsigned long           *groups;
77         unsigned long           state;
78         wait_queue_head_t       wait;
79         struct netlink_callback *cb;
80         struct mutex            *cb_mutex;
81         struct mutex            cb_def_mutex;
82         void                    (*netlink_rcv)(struct sk_buff *skb);
83         void                    (*netlink_bind)(int group);
84         struct module           *module;
85 };
86
87 struct listeners {
88         struct rcu_head         rcu;
89         unsigned long           masks[0];
90 };
91
92 #define NETLINK_KERNEL_SOCKET   0x1
93 #define NETLINK_RECV_PKTINFO    0x2
94 #define NETLINK_BROADCAST_SEND_ERROR    0x4
95 #define NETLINK_RECV_NO_ENOBUFS 0x8
96
97 static inline struct netlink_sock *nlk_sk(struct sock *sk)
98 {
99         return container_of(sk, struct netlink_sock, sk);
100 }
101
102 static inline int netlink_is_kernel(struct sock *sk)
103 {
104         return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
105 }
106
107 struct nl_portid_hash {
108         struct hlist_head       *table;
109         unsigned long           rehash_time;
110
111         unsigned int            mask;
112         unsigned int            shift;
113
114         unsigned int            entries;
115         unsigned int            max_shift;
116
117         u32                     rnd;
118 };
119
120 struct netlink_table {
121         struct nl_portid_hash   hash;
122         struct hlist_head       mc_list;
123         struct listeners __rcu  *listeners;
124         unsigned int            flags;
125         unsigned int            groups;
126         struct mutex            *cb_mutex;
127         struct module           *module;
128         void                    (*bind)(int group);
129         int                     registered;
130 };
131
132 static struct netlink_table *nl_table;
133
134 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
135
136 static int netlink_dump(struct sock *sk);
137
138 static DEFINE_RWLOCK(nl_table_lock);
139 static atomic_t nl_table_users = ATOMIC_INIT(0);
140
141 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
142
143 static inline u32 netlink_group_mask(u32 group)
144 {
145         return group ? 1 << (group - 1) : 0;
146 }
147
148 static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
149 {
150         return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
151 }
152
153 static void netlink_destroy_callback(struct netlink_callback *cb)
154 {
155         kfree_skb(cb->skb);
156         kfree(cb);
157 }
158
159 static void netlink_consume_callback(struct netlink_callback *cb)
160 {
161         consume_skb(cb->skb);
162         kfree(cb);
163 }
164
165 static void netlink_sock_destruct(struct sock *sk)
166 {
167         struct netlink_sock *nlk = nlk_sk(sk);
168
169         if (nlk->cb) {
170                 if (nlk->cb->done)
171                         nlk->cb->done(nlk->cb);
172
173                 module_put(nlk->cb->module);
174                 netlink_destroy_callback(nlk->cb);
175         }
176
177         skb_queue_purge(&sk->sk_receive_queue);
178
179         if (!sock_flag(sk, SOCK_DEAD)) {
180                 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
181                 return;
182         }
183
184         WARN_ON(atomic_read(&sk->sk_rmem_alloc));
185         WARN_ON(atomic_read(&sk->sk_wmem_alloc));
186         WARN_ON(nlk_sk(sk)->groups);
187 }
188
189 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
190  * SMP. Look, when several writers sleep and reader wakes them up, all but one
191  * immediately hit write lock and grab all the cpus. Exclusive sleep solves
192  * this, _but_ remember, it adds useless work on UP machines.
193  */
194
195 void netlink_table_grab(void)
196         __acquires(nl_table_lock)
197 {
198         might_sleep();
199
200         write_lock_irq(&nl_table_lock);
201
202         if (atomic_read(&nl_table_users)) {
203                 DECLARE_WAITQUEUE(wait, current);
204
205                 add_wait_queue_exclusive(&nl_table_wait, &wait);
206                 for (;;) {
207                         set_current_state(TASK_UNINTERRUPTIBLE);
208                         if (atomic_read(&nl_table_users) == 0)
209                                 break;
210                         write_unlock_irq(&nl_table_lock);
211                         schedule();
212                         write_lock_irq(&nl_table_lock);
213                 }
214
215                 __set_current_state(TASK_RUNNING);
216                 remove_wait_queue(&nl_table_wait, &wait);
217         }
218 }
219
220 void netlink_table_ungrab(void)
221         __releases(nl_table_lock)
222 {
223         write_unlock_irq(&nl_table_lock);
224         wake_up(&nl_table_wait);
225 }
226
227 static inline void
228 netlink_lock_table(void)
229 {
230         /* read_lock() synchronizes us to netlink_table_grab */
231
232         read_lock(&nl_table_lock);
233         atomic_inc(&nl_table_users);
234         read_unlock(&nl_table_lock);
235 }
236
237 static inline void
238 netlink_unlock_table(void)
239 {
240         if (atomic_dec_and_test(&nl_table_users))
241                 wake_up(&nl_table_wait);
242 }
243
244 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
245 {
246         struct nl_portid_hash *hash = &nl_table[protocol].hash;
247         struct hlist_head *head;
248         struct sock *sk;
249         struct hlist_node *node;
250
251         read_lock(&nl_table_lock);
252         head = nl_portid_hashfn(hash, portid);
253         sk_for_each(sk, node, head) {
254                 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
255                         sock_hold(sk);
256                         goto found;
257                 }
258         }
259         sk = NULL;
260 found:
261         read_unlock(&nl_table_lock);
262         return sk;
263 }
264
265 static struct hlist_head *nl_portid_hash_zalloc(size_t size)
266 {
267         if (size <= PAGE_SIZE)
268                 return kzalloc(size, GFP_ATOMIC);
269         else
270                 return (struct hlist_head *)
271                         __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
272                                          get_order(size));
273 }
274
275 static void nl_portid_hash_free(struct hlist_head *table, size_t size)
276 {
277         if (size <= PAGE_SIZE)
278                 kfree(table);
279         else
280                 free_pages((unsigned long)table, get_order(size));
281 }
282
283 static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
284 {
285         unsigned int omask, mask, shift;
286         size_t osize, size;
287         struct hlist_head *otable, *table;
288         int i;
289
290         omask = mask = hash->mask;
291         osize = size = (mask + 1) * sizeof(*table);
292         shift = hash->shift;
293
294         if (grow) {
295                 if (++shift > hash->max_shift)
296                         return 0;
297                 mask = mask * 2 + 1;
298                 size *= 2;
299         }
300
301         table = nl_portid_hash_zalloc(size);
302         if (!table)
303                 return 0;
304
305         otable = hash->table;
306         hash->table = table;
307         hash->mask = mask;
308         hash->shift = shift;
309         get_random_bytes(&hash->rnd, sizeof(hash->rnd));
310
311         for (i = 0; i <= omask; i++) {
312                 struct sock *sk;
313                 struct hlist_node *node, *tmp;
314
315                 sk_for_each_safe(sk, node, tmp, &otable[i])
316                         __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
317         }
318
319         nl_portid_hash_free(otable, osize);
320         hash->rehash_time = jiffies + 10 * 60 * HZ;
321         return 1;
322 }
323
324 static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
325 {
326         int avg = hash->entries >> hash->shift;
327
328         if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
329                 return 1;
330
331         if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
332                 nl_portid_hash_rehash(hash, 0);
333                 return 1;
334         }
335
336         return 0;
337 }
338
339 static const struct proto_ops netlink_ops;
340
341 static void
342 netlink_update_listeners(struct sock *sk)
343 {
344         struct netlink_table *tbl = &nl_table[sk->sk_protocol];
345         struct hlist_node *node;
346         unsigned long mask;
347         unsigned int i;
348
349         for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
350                 mask = 0;
351                 sk_for_each_bound(sk, node, &tbl->mc_list) {
352                         if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
353                                 mask |= nlk_sk(sk)->groups[i];
354                 }
355                 tbl->listeners->masks[i] = mask;
356         }
357         /* this function is only called with the netlink table "grabbed", which
358          * makes sure updates are visible before bind or setsockopt return. */
359 }
360
361 static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
362 {
363         struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
364         struct hlist_head *head;
365         int err = -EADDRINUSE;
366         struct sock *osk;
367         struct hlist_node *node;
368         int len;
369
370         netlink_table_grab();
371         head = nl_portid_hashfn(hash, portid);
372         len = 0;
373         sk_for_each(osk, node, head) {
374                 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
375                         break;
376                 len++;
377         }
378         if (node)
379                 goto err;
380
381         err = -EBUSY;
382         if (nlk_sk(sk)->portid)
383                 goto err;
384
385         err = -ENOMEM;
386         if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
387                 goto err;
388
389         if (len && nl_portid_hash_dilute(hash, len))
390                 head = nl_portid_hashfn(hash, portid);
391         hash->entries++;
392         nlk_sk(sk)->portid = portid;
393         sk_add_node(sk, head);
394         err = 0;
395
396 err:
397         netlink_table_ungrab();
398         return err;
399 }
400
401 static void netlink_remove(struct sock *sk)
402 {
403         netlink_table_grab();
404         if (sk_del_node_init(sk))
405                 nl_table[sk->sk_protocol].hash.entries--;
406         if (nlk_sk(sk)->subscriptions)
407                 __sk_del_bind_node(sk);
408         netlink_table_ungrab();
409 }
410
411 static struct proto netlink_proto = {
412         .name     = "NETLINK",
413         .owner    = THIS_MODULE,
414         .obj_size = sizeof(struct netlink_sock),
415 };
416
417 static int __netlink_create(struct net *net, struct socket *sock,
418                             struct mutex *cb_mutex, int protocol)
419 {
420         struct sock *sk;
421         struct netlink_sock *nlk;
422
423         sock->ops = &netlink_ops;
424
425         sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
426         if (!sk)
427                 return -ENOMEM;
428
429         sock_init_data(sock, sk);
430
431         nlk = nlk_sk(sk);
432         if (cb_mutex) {
433                 nlk->cb_mutex = cb_mutex;
434         } else {
435                 nlk->cb_mutex = &nlk->cb_def_mutex;
436                 mutex_init(nlk->cb_mutex);
437         }
438         init_waitqueue_head(&nlk->wait);
439
440         sk->sk_destruct = netlink_sock_destruct;
441         sk->sk_protocol = protocol;
442         return 0;
443 }
444
445 static int netlink_create(struct net *net, struct socket *sock, int protocol,
446                           int kern)
447 {
448         struct module *module = NULL;
449         struct mutex *cb_mutex;
450         struct netlink_sock *nlk;
451         void (*bind)(int group);
452         int err = 0;
453
454         sock->state = SS_UNCONNECTED;
455
456         if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
457                 return -ESOCKTNOSUPPORT;
458
459         if (protocol < 0 || protocol >= MAX_LINKS)
460                 return -EPROTONOSUPPORT;
461
462         netlink_lock_table();
463 #ifdef CONFIG_MODULES
464         if (!nl_table[protocol].registered) {
465                 netlink_unlock_table();
466                 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
467                 netlink_lock_table();
468         }
469 #endif
470         if (nl_table[protocol].registered &&
471             try_module_get(nl_table[protocol].module))
472                 module = nl_table[protocol].module;
473         else
474                 err = -EPROTONOSUPPORT;
475         cb_mutex = nl_table[protocol].cb_mutex;
476         bind = nl_table[protocol].bind;
477         netlink_unlock_table();
478
479         if (err < 0)
480                 goto out;
481
482         err = __netlink_create(net, sock, cb_mutex, protocol);
483         if (err < 0)
484                 goto out_module;
485
486         local_bh_disable();
487         sock_prot_inuse_add(net, &netlink_proto, 1);
488         local_bh_enable();
489
490         nlk = nlk_sk(sock->sk);
491         nlk->module = module;
492         nlk->netlink_bind = bind;
493 out:
494         return err;
495
496 out_module:
497         module_put(module);
498         goto out;
499 }
500
501 static int netlink_release(struct socket *sock)
502 {
503         struct sock *sk = sock->sk;
504         struct netlink_sock *nlk;
505
506         if (!sk)
507                 return 0;
508
509         netlink_remove(sk);
510         sock_orphan(sk);
511         nlk = nlk_sk(sk);
512
513         /*
514          * OK. Socket is unlinked, any packets that arrive now
515          * will be purged.
516          */
517
518         sock->sk = NULL;
519         wake_up_interruptible_all(&nlk->wait);
520
521         skb_queue_purge(&sk->sk_write_queue);
522
523         if (nlk->portid) {
524                 struct netlink_notify n = {
525                                                 .net = sock_net(sk),
526                                                 .protocol = sk->sk_protocol,
527                                                 .portid = nlk->portid,
528                                           };
529                 atomic_notifier_call_chain(&netlink_chain,
530                                 NETLINK_URELEASE, &n);
531         }
532
533         module_put(nlk->module);
534
535         netlink_table_grab();
536         if (netlink_is_kernel(sk)) {
537                 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
538                 if (--nl_table[sk->sk_protocol].registered == 0) {
539                         kfree(nl_table[sk->sk_protocol].listeners);
540                         nl_table[sk->sk_protocol].module = NULL;
541                         nl_table[sk->sk_protocol].bind = NULL;
542                         nl_table[sk->sk_protocol].flags = 0;
543                         nl_table[sk->sk_protocol].registered = 0;
544                 }
545         } else if (nlk->subscriptions) {
546                 netlink_update_listeners(sk);
547         }
548         netlink_table_ungrab();
549
550         kfree(nlk->groups);
551         nlk->groups = NULL;
552
553         local_bh_disable();
554         sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
555         local_bh_enable();
556         sock_put(sk);
557         return 0;
558 }
559
560 static int netlink_autobind(struct socket *sock)
561 {
562         struct sock *sk = sock->sk;
563         struct net *net = sock_net(sk);
564         struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
565         struct hlist_head *head;
566         struct sock *osk;
567         struct hlist_node *node;
568         s32 portid = task_tgid_vnr(current);
569         int err;
570         static s32 rover = -4097;
571
572 retry:
573         cond_resched();
574         netlink_table_grab();
575         head = nl_portid_hashfn(hash, portid);
576         sk_for_each(osk, node, head) {
577                 if (!net_eq(sock_net(osk), net))
578                         continue;
579                 if (nlk_sk(osk)->portid == portid) {
580                         /* Bind collision, search negative portid values. */
581                         portid = rover--;
582                         if (rover > -4097)
583                                 rover = -4097;
584                         netlink_table_ungrab();
585                         goto retry;
586                 }
587         }
588         netlink_table_ungrab();
589
590         err = netlink_insert(sk, net, portid);
591         if (err == -EADDRINUSE)
592                 goto retry;
593
594         /* If 2 threads race to autobind, that is fine.  */
595         if (err == -EBUSY)
596                 err = 0;
597
598         return err;
599 }
600
601 static inline int netlink_capable(const struct socket *sock, unsigned int flag)
602 {
603         return (nl_table[sock->sk->sk_protocol].flags & flag) ||
604                capable(CAP_NET_ADMIN);
605 }
606
607 static void
608 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
609 {
610         struct netlink_sock *nlk = nlk_sk(sk);
611
612         if (nlk->subscriptions && !subscriptions)
613                 __sk_del_bind_node(sk);
614         else if (!nlk->subscriptions && subscriptions)
615                 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
616         nlk->subscriptions = subscriptions;
617 }
618
619 static int netlink_realloc_groups(struct sock *sk)
620 {
621         struct netlink_sock *nlk = nlk_sk(sk);
622         unsigned int groups;
623         unsigned long *new_groups;
624         int err = 0;
625
626         netlink_table_grab();
627
628         groups = nl_table[sk->sk_protocol].groups;
629         if (!nl_table[sk->sk_protocol].registered) {
630                 err = -ENOENT;
631                 goto out_unlock;
632         }
633
634         if (nlk->ngroups >= groups)
635                 goto out_unlock;
636
637         new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
638         if (new_groups == NULL) {
639                 err = -ENOMEM;
640                 goto out_unlock;
641         }
642         memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
643                NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
644
645         nlk->groups = new_groups;
646         nlk->ngroups = groups;
647  out_unlock:
648         netlink_table_ungrab();
649         return err;
650 }
651
652 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
653                         int addr_len)
654 {
655         struct sock *sk = sock->sk;
656         struct net *net = sock_net(sk);
657         struct netlink_sock *nlk = nlk_sk(sk);
658         struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
659         int err;
660
661         if (nladdr->nl_family != AF_NETLINK)
662                 return -EINVAL;
663
664         /* Only superuser is allowed to listen multicasts */
665         if (nladdr->nl_groups) {
666                 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
667                         return -EPERM;
668                 err = netlink_realloc_groups(sk);
669                 if (err)
670                         return err;
671         }
672
673         if (nlk->portid) {
674                 if (nladdr->nl_pid != nlk->portid)
675                         return -EINVAL;
676         } else {
677                 err = nladdr->nl_pid ?
678                         netlink_insert(sk, net, nladdr->nl_pid) :
679                         netlink_autobind(sock);
680                 if (err)
681                         return err;
682         }
683
684         if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
685                 return 0;
686
687         netlink_table_grab();
688         netlink_update_subscriptions(sk, nlk->subscriptions +
689                                          hweight32(nladdr->nl_groups) -
690                                          hweight32(nlk->groups[0]));
691         nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
692         netlink_update_listeners(sk);
693         netlink_table_ungrab();
694
695         if (nlk->netlink_bind && nlk->groups[0]) {
696                 int i;
697
698                 for (i=0; i<nlk->ngroups; i++) {
699                         if (test_bit(i, nlk->groups))
700                                 nlk->netlink_bind(i);
701                 }
702         }
703
704         return 0;
705 }
706
707 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
708                            int alen, int flags)
709 {
710         int err = 0;
711         struct sock *sk = sock->sk;
712         struct netlink_sock *nlk = nlk_sk(sk);
713         struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
714
715         if (alen < sizeof(addr->sa_family))
716                 return -EINVAL;
717
718         if (addr->sa_family == AF_UNSPEC) {
719                 sk->sk_state    = NETLINK_UNCONNECTED;
720                 nlk->dst_portid = 0;
721                 nlk->dst_group  = 0;
722                 return 0;
723         }
724         if (addr->sa_family != AF_NETLINK)
725                 return -EINVAL;
726
727         /* Only superuser is allowed to send multicasts */
728         if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
729                 return -EPERM;
730
731         if (!nlk->portid)
732                 err = netlink_autobind(sock);
733
734         if (err == 0) {
735                 sk->sk_state    = NETLINK_CONNECTED;
736                 nlk->dst_portid = nladdr->nl_pid;
737                 nlk->dst_group  = ffs(nladdr->nl_groups);
738         }
739
740         return err;
741 }
742
743 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
744                            int *addr_len, int peer)
745 {
746         struct sock *sk = sock->sk;
747         struct netlink_sock *nlk = nlk_sk(sk);
748         DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
749
750         nladdr->nl_family = AF_NETLINK;
751         nladdr->nl_pad = 0;
752         *addr_len = sizeof(*nladdr);
753
754         if (peer) {
755                 nladdr->nl_pid = nlk->dst_portid;
756                 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
757         } else {
758                 nladdr->nl_pid = nlk->portid;
759                 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
760         }
761         return 0;
762 }
763
764 static void netlink_overrun(struct sock *sk)
765 {
766         struct netlink_sock *nlk = nlk_sk(sk);
767
768         if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
769                 if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
770                         sk->sk_err = ENOBUFS;
771                         sk->sk_error_report(sk);
772                 }
773         }
774         atomic_inc(&sk->sk_drops);
775 }
776
777 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
778 {
779         struct sock *sock;
780         struct netlink_sock *nlk;
781
782         sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
783         if (!sock)
784                 return ERR_PTR(-ECONNREFUSED);
785
786         /* Don't bother queuing skb if kernel socket has no input function */
787         nlk = nlk_sk(sock);
788         if (sock->sk_state == NETLINK_CONNECTED &&
789             nlk->dst_portid != nlk_sk(ssk)->portid) {
790                 sock_put(sock);
791                 return ERR_PTR(-ECONNREFUSED);
792         }
793         return sock;
794 }
795
796 struct sock *netlink_getsockbyfilp(struct file *filp)
797 {
798         struct inode *inode = filp->f_path.dentry->d_inode;
799         struct sock *sock;
800
801         if (!S_ISSOCK(inode->i_mode))
802                 return ERR_PTR(-ENOTSOCK);
803
804         sock = SOCKET_I(inode)->sk;
805         if (sock->sk_family != AF_NETLINK)
806                 return ERR_PTR(-EINVAL);
807
808         sock_hold(sock);
809         return sock;
810 }
811
812 /*
813  * Attach a skb to a netlink socket.
814  * The caller must hold a reference to the destination socket. On error, the
815  * reference is dropped. The skb is not send to the destination, just all
816  * all error checks are performed and memory in the queue is reserved.
817  * Return values:
818  * < 0: error. skb freed, reference to sock dropped.
819  * 0: continue
820  * 1: repeat lookup - reference dropped while waiting for socket memory.
821  */
822 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
823                       long *timeo, struct sock *ssk)
824 {
825         struct netlink_sock *nlk;
826
827         nlk = nlk_sk(sk);
828
829         if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
830             test_bit(0, &nlk->state)) {
831                 DECLARE_WAITQUEUE(wait, current);
832                 if (!*timeo) {
833                         if (!ssk || netlink_is_kernel(ssk))
834                                 netlink_overrun(sk);
835                         sock_put(sk);
836                         kfree_skb(skb);
837                         return -EAGAIN;
838                 }
839
840                 __set_current_state(TASK_INTERRUPTIBLE);
841                 add_wait_queue(&nlk->wait, &wait);
842
843                 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
844                      test_bit(0, &nlk->state)) &&
845                     !sock_flag(sk, SOCK_DEAD))
846                         *timeo = schedule_timeout(*timeo);
847
848                 __set_current_state(TASK_RUNNING);
849                 remove_wait_queue(&nlk->wait, &wait);
850                 sock_put(sk);
851
852                 if (signal_pending(current)) {
853                         kfree_skb(skb);
854                         return sock_intr_errno(*timeo);
855                 }
856                 return 1;
857         }
858         skb_set_owner_r(skb, sk);
859         return 0;
860 }
861
862 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
863 {
864         int len = skb->len;
865
866         skb_queue_tail(&sk->sk_receive_queue, skb);
867         sk->sk_data_ready(sk, len);
868         return len;
869 }
870
871 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
872 {
873         int len = __netlink_sendskb(sk, skb);
874
875         sock_put(sk);
876         return len;
877 }
878
879 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
880 {
881         kfree_skb(skb);
882         sock_put(sk);
883 }
884
885 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
886 {
887         int delta;
888
889         skb_orphan(skb);
890
891         delta = skb->end - skb->tail;
892         if (delta * 2 < skb->truesize)
893                 return skb;
894
895         if (skb_shared(skb)) {
896                 struct sk_buff *nskb = skb_clone(skb, allocation);
897                 if (!nskb)
898                         return skb;
899                 consume_skb(skb);
900                 skb = nskb;
901         }
902
903         if (!pskb_expand_head(skb, 0, -delta, allocation))
904                 skb->truesize -= delta;
905
906         return skb;
907 }
908
909 static void netlink_rcv_wake(struct sock *sk)
910 {
911         struct netlink_sock *nlk = nlk_sk(sk);
912
913         if (skb_queue_empty(&sk->sk_receive_queue))
914                 clear_bit(0, &nlk->state);
915         if (!test_bit(0, &nlk->state))
916                 wake_up_interruptible(&nlk->wait);
917 }
918
919 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
920                                   struct sock *ssk)
921 {
922         int ret;
923         struct netlink_sock *nlk = nlk_sk(sk);
924
925         ret = -ECONNREFUSED;
926         if (nlk->netlink_rcv != NULL) {
927                 ret = skb->len;
928                 skb_set_owner_r(skb, sk);
929                 NETLINK_CB(skb).ssk = ssk;
930                 nlk->netlink_rcv(skb);
931                 consume_skb(skb);
932         } else {
933                 kfree_skb(skb);
934         }
935         sock_put(sk);
936         return ret;
937 }
938
939 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
940                     u32 portid, int nonblock)
941 {
942         struct sock *sk;
943         int err;
944         long timeo;
945
946         skb = netlink_trim(skb, gfp_any());
947
948         timeo = sock_sndtimeo(ssk, nonblock);
949 retry:
950         sk = netlink_getsockbyportid(ssk, portid);
951         if (IS_ERR(sk)) {
952                 kfree_skb(skb);
953                 return PTR_ERR(sk);
954         }
955         if (netlink_is_kernel(sk))
956                 return netlink_unicast_kernel(sk, skb, ssk);
957
958         if (sk_filter(sk, skb)) {
959                 err = skb->len;
960                 kfree_skb(skb);
961                 sock_put(sk);
962                 return err;
963         }
964
965         err = netlink_attachskb(sk, skb, &timeo, ssk);
966         if (err == 1)
967                 goto retry;
968         if (err)
969                 return err;
970
971         return netlink_sendskb(sk, skb);
972 }
973 EXPORT_SYMBOL(netlink_unicast);
974
975 int netlink_has_listeners(struct sock *sk, unsigned int group)
976 {
977         int res = 0;
978         struct listeners *listeners;
979
980         BUG_ON(!netlink_is_kernel(sk));
981
982         rcu_read_lock();
983         listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
984
985         if (group - 1 < nl_table[sk->sk_protocol].groups)
986                 res = test_bit(group - 1, listeners->masks);
987
988         rcu_read_unlock();
989
990         return res;
991 }
992 EXPORT_SYMBOL_GPL(netlink_has_listeners);
993
994 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
995 {
996         struct netlink_sock *nlk = nlk_sk(sk);
997
998         if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
999             !test_bit(0, &nlk->state)) {
1000                 skb_set_owner_r(skb, sk);
1001                 __netlink_sendskb(sk, skb);
1002                 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1003         }
1004         return -1;
1005 }
1006
1007 struct netlink_broadcast_data {
1008         struct sock *exclude_sk;
1009         struct net *net;
1010         u32 portid;
1011         u32 group;
1012         int failure;
1013         int delivery_failure;
1014         int congested;
1015         int delivered;
1016         gfp_t allocation;
1017         struct sk_buff *skb, *skb2;
1018         int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1019         void *tx_data;
1020 };
1021
1022 static int do_one_broadcast(struct sock *sk,
1023                                    struct netlink_broadcast_data *p)
1024 {
1025         struct netlink_sock *nlk = nlk_sk(sk);
1026         int val;
1027
1028         if (p->exclude_sk == sk)
1029                 goto out;
1030
1031         if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1032             !test_bit(p->group - 1, nlk->groups))
1033                 goto out;
1034
1035         if (!net_eq(sock_net(sk), p->net))
1036                 goto out;
1037
1038         if (p->failure) {
1039                 netlink_overrun(sk);
1040                 goto out;
1041         }
1042
1043         sock_hold(sk);
1044         if (p->skb2 == NULL) {
1045                 if (skb_shared(p->skb)) {
1046                         p->skb2 = skb_clone(p->skb, p->allocation);
1047                 } else {
1048                         p->skb2 = skb_get(p->skb);
1049                         /*
1050                          * skb ownership may have been set when
1051                          * delivered to a previous socket.
1052                          */
1053                         skb_orphan(p->skb2);
1054                 }
1055         }
1056         if (p->skb2 == NULL) {
1057                 netlink_overrun(sk);
1058                 /* Clone failed. Notify ALL listeners. */
1059                 p->failure = 1;
1060                 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1061                         p->delivery_failure = 1;
1062         } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1063                 kfree_skb(p->skb2);
1064                 p->skb2 = NULL;
1065         } else if (sk_filter(sk, p->skb2)) {
1066                 kfree_skb(p->skb2);
1067                 p->skb2 = NULL;
1068         } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1069                 netlink_overrun(sk);
1070                 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1071                         p->delivery_failure = 1;
1072         } else {
1073                 p->congested |= val;
1074                 p->delivered = 1;
1075                 p->skb2 = NULL;
1076         }
1077         sock_put(sk);
1078
1079 out:
1080         return 0;
1081 }
1082
1083 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1084         u32 group, gfp_t allocation,
1085         int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1086         void *filter_data)
1087 {
1088         struct net *net = sock_net(ssk);
1089         struct netlink_broadcast_data info;
1090         struct hlist_node *node;
1091         struct sock *sk;
1092
1093         skb = netlink_trim(skb, allocation);
1094
1095         info.exclude_sk = ssk;
1096         info.net = net;
1097         info.portid = portid;
1098         info.group = group;
1099         info.failure = 0;
1100         info.delivery_failure = 0;
1101         info.congested = 0;
1102         info.delivered = 0;
1103         info.allocation = allocation;
1104         info.skb = skb;
1105         info.skb2 = NULL;
1106         info.tx_filter = filter;
1107         info.tx_data = filter_data;
1108
1109         /* While we sleep in clone, do not allow to change socket list */
1110
1111         netlink_lock_table();
1112
1113         sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1114                 do_one_broadcast(sk, &info);
1115
1116         consume_skb(skb);
1117
1118         netlink_unlock_table();
1119
1120         if (info.delivery_failure) {
1121                 kfree_skb(info.skb2);
1122                 return -ENOBUFS;
1123         }
1124         consume_skb(info.skb2);
1125
1126         if (info.delivered) {
1127                 if (info.congested && (allocation & __GFP_WAIT))
1128                         yield();
1129                 return 0;
1130         }
1131         return -ESRCH;
1132 }
1133 EXPORT_SYMBOL(netlink_broadcast_filtered);
1134
1135 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1136                       u32 group, gfp_t allocation)
1137 {
1138         return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1139                 NULL, NULL);
1140 }
1141 EXPORT_SYMBOL(netlink_broadcast);
1142
1143 struct netlink_set_err_data {
1144         struct sock *exclude_sk;
1145         u32 portid;
1146         u32 group;
1147         int code;
1148 };
1149
1150 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1151 {
1152         struct netlink_sock *nlk = nlk_sk(sk);
1153         int ret = 0;
1154
1155         if (sk == p->exclude_sk)
1156                 goto out;
1157
1158         if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1159                 goto out;
1160
1161         if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1162             !test_bit(p->group - 1, nlk->groups))
1163                 goto out;
1164
1165         if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1166                 ret = 1;
1167                 goto out;
1168         }
1169
1170         sk->sk_err = p->code;
1171         sk->sk_error_report(sk);
1172 out:
1173         return ret;
1174 }
1175
1176 /**
1177  * netlink_set_err - report error to broadcast listeners
1178  * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1179  * @portid: the PORTID of a process that we want to skip (if any)
1180  * @groups: the broadcast group that will notice the error
1181  * @code: error code, must be negative (as usual in kernelspace)
1182  *
1183  * This function returns the number of broadcast listeners that have set the
1184  * NETLINK_RECV_NO_ENOBUFS socket option.
1185  */
1186 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1187 {
1188         struct netlink_set_err_data info;
1189         struct hlist_node *node;
1190         struct sock *sk;
1191         int ret = 0;
1192
1193         info.exclude_sk = ssk;
1194         info.portid = portid;
1195         info.group = group;
1196         /* sk->sk_err wants a positive error value */
1197         info.code = -code;
1198
1199         read_lock(&nl_table_lock);
1200
1201         sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
1202                 ret += do_one_set_err(sk, &info);
1203
1204         read_unlock(&nl_table_lock);
1205         return ret;
1206 }
1207 EXPORT_SYMBOL(netlink_set_err);
1208
1209 /* must be called with netlink table grabbed */
1210 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1211                                      unsigned int group,
1212                                      int is_new)
1213 {
1214         int old, new = !!is_new, subscriptions;
1215
1216         old = test_bit(group - 1, nlk->groups);
1217         subscriptions = nlk->subscriptions - old + new;
1218         if (new)
1219                 __set_bit(group - 1, nlk->groups);
1220         else
1221                 __clear_bit(group - 1, nlk->groups);
1222         netlink_update_subscriptions(&nlk->sk, subscriptions);
1223         netlink_update_listeners(&nlk->sk);
1224 }
1225
1226 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1227                               char __user *optval, unsigned int optlen)
1228 {
1229         struct sock *sk = sock->sk;
1230         struct netlink_sock *nlk = nlk_sk(sk);
1231         unsigned int val = 0;
1232         int err;
1233
1234         if (level != SOL_NETLINK)
1235                 return -ENOPROTOOPT;
1236
1237         if (optlen >= sizeof(int) &&
1238             get_user(val, (unsigned int __user *)optval))
1239                 return -EFAULT;
1240
1241         switch (optname) {
1242         case NETLINK_PKTINFO:
1243                 if (val)
1244                         nlk->flags |= NETLINK_RECV_PKTINFO;
1245                 else
1246                         nlk->flags &= ~NETLINK_RECV_PKTINFO;
1247                 err = 0;
1248                 break;
1249         case NETLINK_ADD_MEMBERSHIP:
1250         case NETLINK_DROP_MEMBERSHIP: {
1251                 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
1252                         return -EPERM;
1253                 err = netlink_realloc_groups(sk);
1254                 if (err)
1255                         return err;
1256                 if (!val || val - 1 >= nlk->ngroups)
1257                         return -EINVAL;
1258                 netlink_table_grab();
1259                 netlink_update_socket_mc(nlk, val,
1260                                          optname == NETLINK_ADD_MEMBERSHIP);
1261                 netlink_table_ungrab();
1262
1263                 if (nlk->netlink_bind)
1264                         nlk->netlink_bind(val);
1265
1266                 err = 0;
1267                 break;
1268         }
1269         case NETLINK_BROADCAST_ERROR:
1270                 if (val)
1271                         nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1272                 else
1273                         nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1274                 err = 0;
1275                 break;
1276         case NETLINK_NO_ENOBUFS:
1277                 if (val) {
1278                         nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1279                         clear_bit(0, &nlk->state);
1280                         wake_up_interruptible(&nlk->wait);
1281                 } else {
1282                         nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
1283                 }
1284                 err = 0;
1285                 break;
1286         default:
1287                 err = -ENOPROTOOPT;
1288         }
1289         return err;
1290 }
1291
1292 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1293                               char __user *optval, int __user *optlen)
1294 {
1295         struct sock *sk = sock->sk;
1296         struct netlink_sock *nlk = nlk_sk(sk);
1297         int len, val, err;
1298
1299         if (level != SOL_NETLINK)
1300                 return -ENOPROTOOPT;
1301
1302         if (get_user(len, optlen))
1303                 return -EFAULT;
1304         if (len < 0)
1305                 return -EINVAL;
1306
1307         switch (optname) {
1308         case NETLINK_PKTINFO:
1309                 if (len < sizeof(int))
1310                         return -EINVAL;
1311                 len = sizeof(int);
1312                 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1313                 if (put_user(len, optlen) ||
1314                     put_user(val, optval))
1315                         return -EFAULT;
1316                 err = 0;
1317                 break;
1318         case NETLINK_BROADCAST_ERROR:
1319                 if (len < sizeof(int))
1320                         return -EINVAL;
1321                 len = sizeof(int);
1322                 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1323                 if (put_user(len, optlen) ||
1324                     put_user(val, optval))
1325                         return -EFAULT;
1326                 err = 0;
1327                 break;
1328         case NETLINK_NO_ENOBUFS:
1329                 if (len < sizeof(int))
1330                         return -EINVAL;
1331                 len = sizeof(int);
1332                 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1333                 if (put_user(len, optlen) ||
1334                     put_user(val, optval))
1335                         return -EFAULT;
1336                 err = 0;
1337                 break;
1338         default:
1339                 err = -ENOPROTOOPT;
1340         }
1341         return err;
1342 }
1343
1344 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1345 {
1346         struct nl_pktinfo info;
1347
1348         info.group = NETLINK_CB(skb).dst_group;
1349         put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1350 }
1351
1352 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1353                            struct msghdr *msg, size_t len)
1354 {
1355         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1356         struct sock *sk = sock->sk;
1357         struct netlink_sock *nlk = nlk_sk(sk);
1358         struct sockaddr_nl *addr = msg->msg_name;
1359         u32 dst_portid;
1360         u32 dst_group;
1361         struct sk_buff *skb;
1362         int err;
1363         struct scm_cookie scm;
1364
1365         if (msg->msg_flags&MSG_OOB)
1366                 return -EOPNOTSUPP;
1367
1368         if (NULL == siocb->scm)
1369                 siocb->scm = &scm;
1370
1371         err = scm_send(sock, msg, siocb->scm, true);
1372         if (err < 0)
1373                 return err;
1374
1375         if (msg->msg_namelen) {
1376                 err = -EINVAL;
1377                 if (addr->nl_family != AF_NETLINK)
1378                         goto out;
1379                 dst_portid = addr->nl_pid;
1380                 dst_group = ffs(addr->nl_groups);
1381                 err =  -EPERM;
1382                 if ((dst_group || dst_portid) &&
1383                     !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
1384                         goto out;
1385         } else {
1386                 dst_portid = nlk->dst_portid;
1387                 dst_group = nlk->dst_group;
1388         }
1389
1390         if (!nlk->portid) {
1391                 err = netlink_autobind(sock);
1392                 if (err)
1393                         goto out;
1394         }
1395
1396         err = -EMSGSIZE;
1397         if (len > sk->sk_sndbuf - 32)
1398                 goto out;
1399         err = -ENOBUFS;
1400         skb = alloc_skb(len, GFP_KERNEL);
1401         if (skb == NULL)
1402                 goto out;
1403
1404         NETLINK_CB(skb).portid  = nlk->portid;
1405         NETLINK_CB(skb).dst_group = dst_group;
1406         NETLINK_CB(skb).creds   = siocb->scm->creds;
1407
1408         err = -EFAULT;
1409         if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1410                 kfree_skb(skb);
1411                 goto out;
1412         }
1413
1414         err = security_netlink_send(sk, skb);
1415         if (err) {
1416                 kfree_skb(skb);
1417                 goto out;
1418         }
1419
1420         if (dst_group) {
1421                 atomic_inc(&skb->users);
1422                 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1423         }
1424         err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1425
1426 out:
1427         scm_destroy(siocb->scm);
1428         return err;
1429 }
1430
1431 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1432                            struct msghdr *msg, size_t len,
1433                            int flags)
1434 {
1435         struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1436         struct scm_cookie scm;
1437         struct sock *sk = sock->sk;
1438         struct netlink_sock *nlk = nlk_sk(sk);
1439         int noblock = flags&MSG_DONTWAIT;
1440         size_t copied;
1441         struct sk_buff *skb, *data_skb;
1442         int err, ret;
1443
1444         if (flags&MSG_OOB)
1445                 return -EOPNOTSUPP;
1446
1447         copied = 0;
1448
1449         skb = skb_recv_datagram(sk, flags, noblock, &err);
1450         if (skb == NULL)
1451                 goto out;
1452
1453         data_skb = skb;
1454
1455 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1456         if (unlikely(skb_shinfo(skb)->frag_list)) {
1457                 /*
1458                  * If this skb has a frag_list, then here that means that we
1459                  * will have to use the frag_list skb's data for compat tasks
1460                  * and the regular skb's data for normal (non-compat) tasks.
1461                  *
1462                  * If we need to send the compat skb, assign it to the
1463                  * 'data_skb' variable so that it will be used below for data
1464                  * copying. We keep 'skb' for everything else, including
1465                  * freeing both later.
1466                  */
1467                 if (flags & MSG_CMSG_COMPAT)
1468                         data_skb = skb_shinfo(skb)->frag_list;
1469         }
1470 #endif
1471
1472         msg->msg_namelen = 0;
1473
1474         copied = data_skb->len;
1475         if (len < copied) {
1476                 msg->msg_flags |= MSG_TRUNC;
1477                 copied = len;
1478         }
1479
1480         skb_reset_transport_header(data_skb);
1481         err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
1482
1483         if (msg->msg_name) {
1484                 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
1485                 addr->nl_family = AF_NETLINK;
1486                 addr->nl_pad    = 0;
1487                 addr->nl_pid    = NETLINK_CB(skb).portid;
1488                 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1489                 msg->msg_namelen = sizeof(*addr);
1490         }
1491
1492         if (nlk->flags & NETLINK_RECV_PKTINFO)
1493                 netlink_cmsg_recv_pktinfo(msg, skb);
1494
1495         if (NULL == siocb->scm) {
1496                 memset(&scm, 0, sizeof(scm));
1497                 siocb->scm = &scm;
1498         }
1499         siocb->scm->creds = *NETLINK_CREDS(skb);
1500         if (flags & MSG_TRUNC)
1501                 copied = data_skb->len;
1502
1503         skb_free_datagram(sk, skb);
1504
1505         if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1506                 ret = netlink_dump(sk);
1507                 if (ret) {
1508                         sk->sk_err = ret;
1509                         sk->sk_error_report(sk);
1510                 }
1511         }
1512
1513         scm_recv(sock, msg, siocb->scm, flags);
1514 out:
1515         netlink_rcv_wake(sk);
1516         return err ? : copied;
1517 }
1518
1519 static void netlink_data_ready(struct sock *sk, int len)
1520 {
1521         BUG();
1522 }
1523
1524 /*
1525  *      We export these functions to other modules. They provide a
1526  *      complete set of kernel non-blocking support for message
1527  *      queueing.
1528  */
1529
1530 struct sock *
1531 __netlink_kernel_create(struct net *net, int unit, struct module *module,
1532                         struct netlink_kernel_cfg *cfg)
1533 {
1534         struct socket *sock;
1535         struct sock *sk;
1536         struct netlink_sock *nlk;
1537         struct listeners *listeners = NULL;
1538         struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1539         unsigned int groups;
1540
1541         BUG_ON(!nl_table);
1542
1543         if (unit < 0 || unit >= MAX_LINKS)
1544                 return NULL;
1545
1546         if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1547                 return NULL;
1548
1549         /*
1550          * We have to just have a reference on the net from sk, but don't
1551          * get_net it. Besides, we cannot get and then put the net here.
1552          * So we create one inside init_net and the move it to net.
1553          */
1554
1555         if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1556                 goto out_sock_release_nosk;
1557
1558         sk = sock->sk;
1559         sk_change_net(sk, net);
1560
1561         if (!cfg || cfg->groups < 32)
1562                 groups = 32;
1563         else
1564                 groups = cfg->groups;
1565
1566         listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
1567         if (!listeners)
1568                 goto out_sock_release;
1569
1570         sk->sk_data_ready = netlink_data_ready;
1571         if (cfg && cfg->input)
1572                 nlk_sk(sk)->netlink_rcv = cfg->input;
1573
1574         if (netlink_insert(sk, net, 0))
1575                 goto out_sock_release;
1576
1577         nlk = nlk_sk(sk);
1578         nlk->flags |= NETLINK_KERNEL_SOCKET;
1579
1580         netlink_table_grab();
1581         if (!nl_table[unit].registered) {
1582                 nl_table[unit].groups = groups;
1583                 rcu_assign_pointer(nl_table[unit].listeners, listeners);
1584                 nl_table[unit].cb_mutex = cb_mutex;
1585                 nl_table[unit].module = module;
1586                 if (cfg) {
1587                         nl_table[unit].bind = cfg->bind;
1588                         nl_table[unit].flags = cfg->flags;
1589                 }
1590                 nl_table[unit].registered = 1;
1591         } else {
1592                 kfree(listeners);
1593                 nl_table[unit].registered++;
1594         }
1595         netlink_table_ungrab();
1596         return sk;
1597
1598 out_sock_release:
1599         kfree(listeners);
1600         netlink_kernel_release(sk);
1601         return NULL;
1602
1603 out_sock_release_nosk:
1604         sock_release(sock);
1605         return NULL;
1606 }
1607 EXPORT_SYMBOL(__netlink_kernel_create);
1608
1609 void
1610 netlink_kernel_release(struct sock *sk)
1611 {
1612         sk_release_kernel(sk);
1613 }
1614 EXPORT_SYMBOL(netlink_kernel_release);
1615
1616 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
1617 {
1618         struct listeners *new, *old;
1619         struct netlink_table *tbl = &nl_table[sk->sk_protocol];
1620
1621         if (groups < 32)
1622                 groups = 32;
1623
1624         if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
1625                 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1626                 if (!new)
1627                         return -ENOMEM;
1628                 old = rcu_dereference_protected(tbl->listeners, 1);
1629                 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1630                 rcu_assign_pointer(tbl->listeners, new);
1631
1632                 kfree_rcu(old, rcu);
1633         }
1634         tbl->groups = groups;
1635
1636         return 0;
1637 }
1638
1639 /**
1640  * netlink_change_ngroups - change number of multicast groups
1641  *
1642  * This changes the number of multicast groups that are available
1643  * on a certain netlink family. Note that it is not possible to
1644  * change the number of groups to below 32. Also note that it does
1645  * not implicitly call netlink_clear_multicast_users() when the
1646  * number of groups is reduced.
1647  *
1648  * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1649  * @groups: The new number of groups.
1650  */
1651 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1652 {
1653         int err;
1654
1655         netlink_table_grab();
1656         err = __netlink_change_ngroups(sk, groups);
1657         netlink_table_ungrab();
1658
1659         return err;
1660 }
1661
1662 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1663 {
1664         struct sock *sk;
1665         struct hlist_node *node;
1666         struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1667
1668         sk_for_each_bound(sk, node, &tbl->mc_list)
1669                 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1670 }
1671
1672 /**
1673  * netlink_clear_multicast_users - kick off multicast listeners
1674  *
1675  * This function removes all listeners from the given group.
1676  * @ksk: The kernel netlink socket, as returned by
1677  *      netlink_kernel_create().
1678  * @group: The multicast group to clear.
1679  */
1680 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1681 {
1682         netlink_table_grab();
1683         __netlink_clear_multicast_users(ksk, group);
1684         netlink_table_ungrab();
1685 }
1686
1687 struct nlmsghdr *
1688 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
1689 {
1690         struct nlmsghdr *nlh;
1691         int size = NLMSG_LENGTH(len);
1692
1693         nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1694         nlh->nlmsg_type = type;
1695         nlh->nlmsg_len = size;
1696         nlh->nlmsg_flags = flags;
1697         nlh->nlmsg_pid = portid;
1698         nlh->nlmsg_seq = seq;
1699         if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
1700                 memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
1701         return nlh;
1702 }
1703 EXPORT_SYMBOL(__nlmsg_put);
1704
1705 /*
1706  * It looks a bit ugly.
1707  * It would be better to create kernel thread.
1708  */
1709
1710 static int netlink_dump(struct sock *sk)
1711 {
1712         struct netlink_sock *nlk = nlk_sk(sk);
1713         struct netlink_callback *cb;
1714         struct sk_buff *skb = NULL;
1715         struct nlmsghdr *nlh;
1716         int len, err = -ENOBUFS;
1717         int alloc_size;
1718
1719         mutex_lock(nlk->cb_mutex);
1720
1721         cb = nlk->cb;
1722         if (cb == NULL) {
1723                 err = -EINVAL;
1724                 goto errout_skb;
1725         }
1726
1727         alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1728
1729         skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1730         if (!skb)
1731                 goto errout_skb;
1732
1733         len = cb->dump(skb, cb);
1734
1735         if (len > 0) {
1736                 mutex_unlock(nlk->cb_mutex);
1737
1738                 if (sk_filter(sk, skb))
1739                         kfree_skb(skb);
1740                 else
1741                         __netlink_sendskb(sk, skb);
1742                 return 0;
1743         }
1744
1745         nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1746         if (!nlh)
1747                 goto errout_skb;
1748
1749         nl_dump_check_consistent(cb, nlh);
1750
1751         memcpy(nlmsg_data(nlh), &len, sizeof(len));
1752
1753         if (sk_filter(sk, skb))
1754                 kfree_skb(skb);
1755         else
1756                 __netlink_sendskb(sk, skb);
1757
1758         if (cb->done)
1759                 cb->done(cb);
1760         nlk->cb = NULL;
1761         mutex_unlock(nlk->cb_mutex);
1762
1763         module_put(cb->module);
1764         netlink_consume_callback(cb);
1765         return 0;
1766
1767 errout_skb:
1768         mutex_unlock(nlk->cb_mutex);
1769         kfree_skb(skb);
1770         return err;
1771 }
1772
1773 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
1774                          const struct nlmsghdr *nlh,
1775                          struct netlink_dump_control *control)
1776 {
1777         struct netlink_callback *cb;
1778         struct sock *sk;
1779         struct netlink_sock *nlk;
1780         int ret;
1781
1782         cb = kzalloc(sizeof(*cb), GFP_KERNEL);
1783         if (cb == NULL)
1784                 return -ENOBUFS;
1785
1786         cb->dump = control->dump;
1787         cb->done = control->done;
1788         cb->nlh = nlh;
1789         cb->data = control->data;
1790         cb->module = control->module;
1791         cb->min_dump_alloc = control->min_dump_alloc;
1792         atomic_inc(&skb->users);
1793         cb->skb = skb;
1794
1795         sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
1796         if (sk == NULL) {
1797                 netlink_destroy_callback(cb);
1798                 return -ECONNREFUSED;
1799         }
1800         nlk = nlk_sk(sk);
1801
1802         mutex_lock(nlk->cb_mutex);
1803         /* A dump is in progress... */
1804         if (nlk->cb) {
1805                 mutex_unlock(nlk->cb_mutex);
1806                 netlink_destroy_callback(cb);
1807                 ret = -EBUSY;
1808                 goto out;
1809         }
1810         /* add reference of module which cb->dump belongs to */
1811         if (!try_module_get(cb->module)) {
1812                 mutex_unlock(nlk->cb_mutex);
1813                 netlink_destroy_callback(cb);
1814                 ret = -EPROTONOSUPPORT;
1815                 goto out;
1816         }
1817
1818         nlk->cb = cb;
1819         mutex_unlock(nlk->cb_mutex);
1820
1821         ret = netlink_dump(sk);
1822 out:
1823         sock_put(sk);
1824
1825         if (ret)
1826                 return ret;
1827
1828         /* We successfully started a dump, by returning -EINTR we
1829          * signal not to send ACK even if it was requested.
1830          */
1831         return -EINTR;
1832 }
1833 EXPORT_SYMBOL(__netlink_dump_start);
1834
1835 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
1836 {
1837         struct sk_buff *skb;
1838         struct nlmsghdr *rep;
1839         struct nlmsgerr *errmsg;
1840         size_t payload = sizeof(*errmsg);
1841
1842         /* error messages get the original request appened */
1843         if (err)
1844                 payload += nlmsg_len(nlh);
1845
1846         skb = nlmsg_new(payload, GFP_KERNEL);
1847         if (!skb) {
1848                 struct sock *sk;
1849
1850                 sk = netlink_lookup(sock_net(in_skb->sk),
1851                                     in_skb->sk->sk_protocol,
1852                                     NETLINK_CB(in_skb).portid);
1853                 if (sk) {
1854                         sk->sk_err = ENOBUFS;
1855                         sk->sk_error_report(sk);
1856                         sock_put(sk);
1857                 }
1858                 return;
1859         }
1860
1861         rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
1862                           NLMSG_ERROR, payload, 0);
1863         errmsg = nlmsg_data(rep);
1864         errmsg->error = err;
1865         memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
1866         netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1867 }
1868 EXPORT_SYMBOL(netlink_ack);
1869
1870 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1871                                                      struct nlmsghdr *))
1872 {
1873         struct nlmsghdr *nlh;
1874         int err;
1875
1876         while (skb->len >= nlmsg_total_size(0)) {
1877                 int msglen;
1878
1879                 nlh = nlmsg_hdr(skb);
1880                 err = 0;
1881
1882                 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
1883                         return 0;
1884
1885                 /* Only requests are handled by the kernel */
1886                 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1887                         goto ack;
1888
1889                 /* Skip control messages */
1890                 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
1891                         goto ack;
1892
1893                 err = cb(skb, nlh);
1894                 if (err == -EINTR)
1895                         goto skip;
1896
1897 ack:
1898                 if (nlh->nlmsg_flags & NLM_F_ACK || err)
1899                         netlink_ack(skb, nlh, err);
1900
1901 skip:
1902                 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
1903                 if (msglen > skb->len)
1904                         msglen = skb->len;
1905                 skb_pull(skb, msglen);
1906         }
1907
1908         return 0;
1909 }
1910 EXPORT_SYMBOL(netlink_rcv_skb);
1911
1912 /**
1913  * nlmsg_notify - send a notification netlink message
1914  * @sk: netlink socket to use
1915  * @skb: notification message
1916  * @portid: destination netlink portid for reports or 0
1917  * @group: destination multicast group or 0
1918  * @report: 1 to report back, 0 to disable
1919  * @flags: allocation flags
1920  */
1921 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
1922                  unsigned int group, int report, gfp_t flags)
1923 {
1924         int err = 0;
1925
1926         if (group) {
1927                 int exclude_portid = 0;
1928
1929                 if (report) {
1930                         atomic_inc(&skb->users);
1931                         exclude_portid = portid;
1932                 }
1933
1934                 /* errors reported via destination sk->sk_err, but propagate
1935                  * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
1936                 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
1937         }
1938
1939         if (report) {
1940                 int err2;
1941
1942                 err2 = nlmsg_unicast(sk, skb, portid);
1943                 if (!err || err == -ESRCH)
1944                         err = err2;
1945         }
1946
1947         return err;
1948 }
1949 EXPORT_SYMBOL(nlmsg_notify);
1950
1951 #ifdef CONFIG_PROC_FS
1952 struct nl_seq_iter {
1953         struct seq_net_private p;
1954         int link;
1955         int hash_idx;
1956 };
1957
1958 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1959 {
1960         struct nl_seq_iter *iter = seq->private;
1961         int i, j;
1962         struct sock *s;
1963         struct hlist_node *node;
1964         loff_t off = 0;
1965
1966         for (i = 0; i < MAX_LINKS; i++) {
1967                 struct nl_portid_hash *hash = &nl_table[i].hash;
1968
1969                 for (j = 0; j <= hash->mask; j++) {
1970                         sk_for_each(s, node, &hash->table[j]) {
1971                                 if (sock_net(s) != seq_file_net(seq))
1972                                         continue;
1973                                 if (off == pos) {
1974                                         iter->link = i;
1975                                         iter->hash_idx = j;
1976                                         return s;
1977                                 }
1978                                 ++off;
1979                         }
1980                 }
1981         }
1982         return NULL;
1983 }
1984
1985 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
1986         __acquires(nl_table_lock)
1987 {
1988         read_lock(&nl_table_lock);
1989         return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1990 }
1991
1992 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1993 {
1994         struct sock *s;
1995         struct nl_seq_iter *iter;
1996         int i, j;
1997
1998         ++*pos;
1999
2000         if (v == SEQ_START_TOKEN)
2001                 return netlink_seq_socket_idx(seq, 0);
2002
2003         iter = seq->private;
2004         s = v;
2005         do {
2006                 s = sk_next(s);
2007         } while (s && sock_net(s) != seq_file_net(seq));
2008         if (s)
2009                 return s;
2010
2011         i = iter->link;
2012         j = iter->hash_idx + 1;
2013
2014         do {
2015                 struct nl_portid_hash *hash = &nl_table[i].hash;
2016
2017                 for (; j <= hash->mask; j++) {
2018                         s = sk_head(&hash->table[j]);
2019                         while (s && sock_net(s) != seq_file_net(seq))
2020                                 s = sk_next(s);
2021                         if (s) {
2022                                 iter->link = i;
2023                                 iter->hash_idx = j;
2024                                 return s;
2025                         }
2026                 }
2027
2028                 j = 0;
2029         } while (++i < MAX_LINKS);
2030
2031         return NULL;
2032 }
2033
2034 static void netlink_seq_stop(struct seq_file *seq, void *v)
2035         __releases(nl_table_lock)
2036 {
2037         read_unlock(&nl_table_lock);
2038 }
2039
2040
2041 static int netlink_seq_show(struct seq_file *seq, void *v)
2042 {
2043         if (v == SEQ_START_TOKEN) {
2044                 seq_puts(seq,
2045                          "sk       Eth Pid    Groups   "
2046                          "Rmem     Wmem     Dump     Locks     Drops     Inode\n");
2047         } else {
2048                 struct sock *s = v;
2049                 struct netlink_sock *nlk = nlk_sk(s);
2050
2051                 seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2052                            s,
2053                            s->sk_protocol,
2054                            nlk->portid,
2055                            nlk->groups ? (u32)nlk->groups[0] : 0,
2056                            sk_rmem_alloc_get(s),
2057                            sk_wmem_alloc_get(s),
2058                            nlk->cb,
2059                            atomic_read(&s->sk_refcnt),
2060                            atomic_read(&s->sk_drops),
2061                            sock_i_ino(s)
2062                         );
2063
2064         }
2065         return 0;
2066 }
2067
2068 static const struct seq_operations netlink_seq_ops = {
2069         .start  = netlink_seq_start,
2070         .next   = netlink_seq_next,
2071         .stop   = netlink_seq_stop,
2072         .show   = netlink_seq_show,
2073 };
2074
2075
2076 static int netlink_seq_open(struct inode *inode, struct file *file)
2077 {
2078         return seq_open_net(inode, file, &netlink_seq_ops,
2079                                 sizeof(struct nl_seq_iter));
2080 }
2081
2082 static const struct file_operations netlink_seq_fops = {
2083         .owner          = THIS_MODULE,
2084         .open           = netlink_seq_open,
2085         .read           = seq_read,
2086         .llseek         = seq_lseek,
2087         .release        = seq_release_net,
2088 };
2089
2090 #endif
2091
2092 int netlink_register_notifier(struct notifier_block *nb)
2093 {
2094         return atomic_notifier_chain_register(&netlink_chain, nb);
2095 }
2096 EXPORT_SYMBOL(netlink_register_notifier);
2097
2098 int netlink_unregister_notifier(struct notifier_block *nb)
2099 {
2100         return atomic_notifier_chain_unregister(&netlink_chain, nb);
2101 }
2102 EXPORT_SYMBOL(netlink_unregister_notifier);
2103
2104 static const struct proto_ops netlink_ops = {
2105         .family =       PF_NETLINK,
2106         .owner =        THIS_MODULE,
2107         .release =      netlink_release,
2108         .bind =         netlink_bind,
2109         .connect =      netlink_connect,
2110         .socketpair =   sock_no_socketpair,
2111         .accept =       sock_no_accept,
2112         .getname =      netlink_getname,
2113         .poll =         datagram_poll,
2114         .ioctl =        sock_no_ioctl,
2115         .listen =       sock_no_listen,
2116         .shutdown =     sock_no_shutdown,
2117         .setsockopt =   netlink_setsockopt,
2118         .getsockopt =   netlink_getsockopt,
2119         .sendmsg =      netlink_sendmsg,
2120         .recvmsg =      netlink_recvmsg,
2121         .mmap =         sock_no_mmap,
2122         .sendpage =     sock_no_sendpage,
2123 };
2124
2125 static const struct net_proto_family netlink_family_ops = {
2126         .family = PF_NETLINK,
2127         .create = netlink_create,
2128         .owner  = THIS_MODULE,  /* for consistency 8) */
2129 };
2130
2131 static int __net_init netlink_net_init(struct net *net)
2132 {
2133 #ifdef CONFIG_PROC_FS
2134         if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
2135                 return -ENOMEM;
2136 #endif
2137         return 0;
2138 }
2139
2140 static void __net_exit netlink_net_exit(struct net *net)
2141 {
2142 #ifdef CONFIG_PROC_FS
2143         proc_net_remove(net, "netlink");
2144 #endif
2145 }
2146
2147 static void __init netlink_add_usersock_entry(void)
2148 {
2149         struct listeners *listeners;
2150         int groups = 32;
2151
2152         listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2153         if (!listeners)
2154                 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2155
2156         netlink_table_grab();
2157
2158         nl_table[NETLINK_USERSOCK].groups = groups;
2159         rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2160         nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2161         nl_table[NETLINK_USERSOCK].registered = 1;
2162         nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2163
2164         netlink_table_ungrab();
2165 }
2166
2167 static struct pernet_operations __net_initdata netlink_net_ops = {
2168         .init = netlink_net_init,
2169         .exit = netlink_net_exit,
2170 };
2171
2172 static int __init netlink_proto_init(void)
2173 {
2174         struct sk_buff *dummy_skb;
2175         int i;
2176         unsigned long limit;
2177         unsigned int order;
2178         int err = proto_register(&netlink_proto, 0);
2179
2180         if (err != 0)
2181                 goto out;
2182
2183         BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
2184
2185         nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2186         if (!nl_table)
2187                 goto panic;
2188
2189         if (totalram_pages >= (128 * 1024))
2190                 limit = totalram_pages >> (21 - PAGE_SHIFT);
2191         else
2192                 limit = totalram_pages >> (23 - PAGE_SHIFT);
2193
2194         order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2195         limit = (1UL << order) / sizeof(struct hlist_head);
2196         order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2197
2198         for (i = 0; i < MAX_LINKS; i++) {
2199                 struct nl_portid_hash *hash = &nl_table[i].hash;
2200
2201                 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
2202                 if (!hash->table) {
2203                         while (i-- > 0)
2204                                 nl_portid_hash_free(nl_table[i].hash.table,
2205                                                  1 * sizeof(*hash->table));
2206                         kfree(nl_table);
2207                         goto panic;
2208                 }
2209                 hash->max_shift = order;
2210                 hash->shift = 0;
2211                 hash->mask = 0;
2212                 hash->rehash_time = jiffies;
2213         }
2214
2215         netlink_add_usersock_entry();
2216
2217         sock_register(&netlink_family_ops);
2218         register_pernet_subsys(&netlink_net_ops);
2219         /* The netlink device handler may be needed early. */
2220         rtnetlink_init();
2221 out:
2222         return err;
2223 panic:
2224         panic("netlink_init: Cannot allocate nl_table\n");
2225 }
2226
2227 core_initcall(netlink_proto_init);