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