rhashtable: Change rhashtable_walk_start to return void
[linux-block.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 5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
cd1df525 6 * Patrick McHardy <kaber@trash.net>
1da177e4
LT
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
746fac4d 12 *
1da177e4
LT
13 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
14 * added netlink_proto_exit
15 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
16 * use nlk_sk, as sk->protinfo is on a diet 8)
4fdb3bb7
HW
17 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
18 * - inc module use count of module that owns
19 * the kernel socket in case userspace opens
20 * socket of same protocol
21 * - remove all module support, since netlink is
22 * mandatory if CONFIG_NET=y these days
1da177e4
LT
23 */
24
1da177e4
LT
25#include <linux/module.h>
26
4fc268d2 27#include <linux/capability.h>
1da177e4
LT
28#include <linux/kernel.h>
29#include <linux/init.h>
1da177e4
LT
30#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/errno.h>
33#include <linux/string.h>
34#include <linux/stat.h>
35#include <linux/socket.h>
36#include <linux/un.h>
37#include <linux/fcntl.h>
38#include <linux/termios.h>
39#include <linux/sockios.h>
40#include <linux/net.h>
41#include <linux/fs.h>
42#include <linux/slab.h>
7c0f6ba6 43#include <linux/uaccess.h>
1da177e4
LT
44#include <linux/skbuff.h>
45#include <linux/netdevice.h>
46#include <linux/rtnetlink.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
1da177e4
LT
49#include <linux/notifier.h>
50#include <linux/security.h>
51#include <linux/jhash.h>
52#include <linux/jiffies.h>
53#include <linux/random.h>
54#include <linux/bitops.h>
55#include <linux/mm.h>
56#include <linux/types.h>
54e0f520 57#include <linux/audit.h>
af65bdfc 58#include <linux/mutex.h>
ccdfcc39 59#include <linux/vmalloc.h>
bcbde0d4 60#include <linux/if_arp.h>
e341694e 61#include <linux/rhashtable.h>
9652e931 62#include <asm/cacheflush.h>
e341694e 63#include <linux/hash.h>
ee1c2442 64#include <linux/genetlink.h>
7212462f 65#include <linux/net_namespace.h>
54e0f520 66
457c4cbc 67#include <net/net_namespace.h>
1da177e4
LT
68#include <net/sock.h>
69#include <net/scm.h>
82ace47a 70#include <net/netlink.h>
1da177e4 71
0f29c768 72#include "af_netlink.h"
1da177e4 73
5c398dc8
ED
74struct listeners {
75 struct rcu_head rcu;
76 unsigned long masks[0];
6c04bb18
JB
77};
78
cd967e05 79/* state bits */
cc3a572f 80#define NETLINK_S_CONGESTED 0x0
cd967e05 81
035c4c16 82static inline int netlink_is_kernel(struct sock *sk)
aed81560 83{
cc3a572f 84 return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
aed81560
DL
85}
86
91dd93f9 87struct netlink_table *nl_table __read_mostly;
0f29c768 88EXPORT_SYMBOL_GPL(nl_table);
1da177e4
LT
89
90static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
91
8a0f5ccf
HX
92static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
93
94static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
95 "nlk_cb_mutex-ROUTE",
96 "nlk_cb_mutex-1",
97 "nlk_cb_mutex-USERSOCK",
98 "nlk_cb_mutex-FIREWALL",
99 "nlk_cb_mutex-SOCK_DIAG",
100 "nlk_cb_mutex-NFLOG",
101 "nlk_cb_mutex-XFRM",
102 "nlk_cb_mutex-SELINUX",
103 "nlk_cb_mutex-ISCSI",
104 "nlk_cb_mutex-AUDIT",
105 "nlk_cb_mutex-FIB_LOOKUP",
106 "nlk_cb_mutex-CONNECTOR",
107 "nlk_cb_mutex-NETFILTER",
108 "nlk_cb_mutex-IP6_FW",
109 "nlk_cb_mutex-DNRTMSG",
110 "nlk_cb_mutex-KOBJECT_UEVENT",
111 "nlk_cb_mutex-GENERIC",
112 "nlk_cb_mutex-17",
113 "nlk_cb_mutex-SCSITRANSPORT",
114 "nlk_cb_mutex-ECRYPTFS",
115 "nlk_cb_mutex-RDMA",
116 "nlk_cb_mutex-CRYPTO",
117 "nlk_cb_mutex-SMC",
118 "nlk_cb_mutex-23",
119 "nlk_cb_mutex-24",
120 "nlk_cb_mutex-25",
121 "nlk_cb_mutex-26",
122 "nlk_cb_mutex-27",
123 "nlk_cb_mutex-28",
124 "nlk_cb_mutex-29",
125 "nlk_cb_mutex-30",
126 "nlk_cb_mutex-31",
127 "nlk_cb_mutex-MAX_LINKS"
128};
129
1da177e4 130static int netlink_dump(struct sock *sk);
1da177e4 131
78fd1d0a 132/* nl_table locking explained:
21e4902a 133 * Lookup and traversal are protected with an RCU read-side lock. Insertion
c5adde94 134 * and removal are protected with per bucket lock while using RCU list
21e4902a
TG
135 * modification primitives and may run in parallel to RCU protected lookups.
136 * Destruction of the Netlink socket may only occur *after* nl_table_lock has
137 * been acquired * either during or after the socket has been removed from
138 * the list and after an RCU grace period.
78fd1d0a 139 */
0f29c768
AV
140DEFINE_RWLOCK(nl_table_lock);
141EXPORT_SYMBOL_GPL(nl_table_lock);
1da177e4
LT
142static atomic_t nl_table_users = ATOMIC_INIT(0);
143
6d772ac5
ED
144#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
145
efa172f4 146static BLOCKING_NOTIFIER_HEAD(netlink_chain);
1da177e4 147
bcbde0d4
DB
148static DEFINE_SPINLOCK(netlink_tap_lock);
149static struct list_head netlink_tap_all __read_mostly;
150
c428ecd1
HX
151static const struct rhashtable_params netlink_rhashtable_params;
152
b57ef81f 153static inline u32 netlink_group_mask(u32 group)
d629b836
PM
154{
155 return group ? 1 << (group - 1) : 0;
156}
157
1853c949
DB
158static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
159 gfp_t gfp_mask)
160{
161 unsigned int len = skb_end_offset(skb);
162 struct sk_buff *new;
163
164 new = alloc_skb(len, gfp_mask);
165 if (new == NULL)
166 return NULL;
167
168 NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
169 NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
170 NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
171
59ae1d12 172 skb_put_data(new, skb->data, len);
1853c949
DB
173 return new;
174}
175
bcbde0d4
DB
176int netlink_add_tap(struct netlink_tap *nt)
177{
178 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
179 return -EINVAL;
180
181 spin_lock(&netlink_tap_lock);
182 list_add_rcu(&nt->list, &netlink_tap_all);
183 spin_unlock(&netlink_tap_lock);
184
fcd4d35e 185 __module_get(nt->module);
bcbde0d4
DB
186
187 return 0;
188}
189EXPORT_SYMBOL_GPL(netlink_add_tap);
190
2173f8d9 191static int __netlink_remove_tap(struct netlink_tap *nt)
bcbde0d4
DB
192{
193 bool found = false;
194 struct netlink_tap *tmp;
195
196 spin_lock(&netlink_tap_lock);
197
198 list_for_each_entry(tmp, &netlink_tap_all, list) {
199 if (nt == tmp) {
200 list_del_rcu(&nt->list);
201 found = true;
202 goto out;
203 }
204 }
205
206 pr_warn("__netlink_remove_tap: %p not found\n", nt);
207out:
208 spin_unlock(&netlink_tap_lock);
209
92b80eb3 210 if (found)
bcbde0d4
DB
211 module_put(nt->module);
212
213 return found ? 0 : -ENODEV;
214}
bcbde0d4
DB
215
216int netlink_remove_tap(struct netlink_tap *nt)
217{
218 int ret;
219
220 ret = __netlink_remove_tap(nt);
221 synchronize_net();
222
223 return ret;
224}
225EXPORT_SYMBOL_GPL(netlink_remove_tap);
226
5ffd5cdd
DB
227static bool netlink_filter_tap(const struct sk_buff *skb)
228{
229 struct sock *sk = skb->sk;
5ffd5cdd
DB
230
231 /* We take the more conservative approach and
232 * whitelist socket protocols that may pass.
233 */
234 switch (sk->sk_protocol) {
235 case NETLINK_ROUTE:
236 case NETLINK_USERSOCK:
237 case NETLINK_SOCK_DIAG:
238 case NETLINK_NFLOG:
239 case NETLINK_XFRM:
240 case NETLINK_FIB_LOOKUP:
241 case NETLINK_NETFILTER:
242 case NETLINK_GENERIC:
498044bb 243 return true;
5ffd5cdd
DB
244 }
245
498044bb 246 return false;
5ffd5cdd
DB
247}
248
bcbde0d4
DB
249static int __netlink_deliver_tap_skb(struct sk_buff *skb,
250 struct net_device *dev)
251{
252 struct sk_buff *nskb;
5ffd5cdd 253 struct sock *sk = skb->sk;
bcbde0d4
DB
254 int ret = -ENOMEM;
255
256 dev_hold(dev);
1853c949 257
d1b4c689 258 if (is_vmalloc_addr(skb->head))
1853c949
DB
259 nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
260 else
261 nskb = skb_clone(skb, GFP_ATOMIC);
bcbde0d4
DB
262 if (nskb) {
263 nskb->dev = dev;
5ffd5cdd 264 nskb->protocol = htons((u16) sk->sk_protocol);
604d13c9
DB
265 nskb->pkt_type = netlink_is_kernel(sk) ?
266 PACKET_KERNEL : PACKET_USER;
4e48ed88 267 skb_reset_network_header(nskb);
bcbde0d4
DB
268 ret = dev_queue_xmit(nskb);
269 if (unlikely(ret > 0))
270 ret = net_xmit_errno(ret);
271 }
272
273 dev_put(dev);
274 return ret;
275}
276
277static void __netlink_deliver_tap(struct sk_buff *skb)
278{
279 int ret;
280 struct netlink_tap *tmp;
281
5ffd5cdd
DB
282 if (!netlink_filter_tap(skb))
283 return;
284
bcbde0d4
DB
285 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
286 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
287 if (unlikely(ret))
288 break;
289 }
290}
291
292static void netlink_deliver_tap(struct sk_buff *skb)
293{
294 rcu_read_lock();
295
296 if (unlikely(!list_empty(&netlink_tap_all)))
297 __netlink_deliver_tap(skb);
298
299 rcu_read_unlock();
300}
301
73bfd370
DB
302static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
303 struct sk_buff *skb)
304{
305 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
306 netlink_deliver_tap(skb);
307}
308
cd1df525
PM
309static void netlink_overrun(struct sock *sk)
310{
311 struct netlink_sock *nlk = nlk_sk(sk);
312
cc3a572f
ND
313 if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
314 if (!test_and_set_bit(NETLINK_S_CONGESTED,
315 &nlk_sk(sk)->state)) {
cd1df525
PM
316 sk->sk_err = ENOBUFS;
317 sk->sk_error_report(sk);
318 }
319 }
320 atomic_inc(&sk->sk_drops);
321}
322
323static void netlink_rcv_wake(struct sock *sk)
324{
325 struct netlink_sock *nlk = nlk_sk(sk);
326
327 if (skb_queue_empty(&sk->sk_receive_queue))
cc3a572f
ND
328 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
329 if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
cd1df525
PM
330 wake_up_interruptible(&nlk->wait);
331}
332
cf0a018a
PM
333static void netlink_skb_destructor(struct sk_buff *skb)
334{
c05cdb1b 335 if (is_vmalloc_addr(skb->head)) {
3a36515f
PN
336 if (!skb->cloned ||
337 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
338 vfree(skb->head);
339
c05cdb1b
PNA
340 skb->head = NULL;
341 }
9652e931
PM
342 if (skb->sk != NULL)
343 sock_rfree(skb);
cf0a018a
PM
344}
345
346static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
347{
348 WARN_ON(skb->sk != NULL);
349 skb->sk = sk;
350 skb->destructor = netlink_skb_destructor;
351 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
352 sk_mem_charge(sk, skb->truesize);
353}
354
ed5d7788 355static void netlink_sock_destruct(struct sock *sk)
1da177e4 356{
3f660d66
HX
357 struct netlink_sock *nlk = nlk_sk(sk);
358
16b304f3 359 if (nlk->cb_running) {
ed5d7788
HX
360 if (nlk->cb.done)
361 nlk->cb.done(&nlk->cb);
16b304f3
PS
362 module_put(nlk->cb.module);
363 kfree_skb(nlk->cb.skb);
3f660d66
HX
364 }
365
1da177e4
LT
366 skb_queue_purge(&sk->sk_receive_queue);
367
368 if (!sock_flag(sk, SOCK_DEAD)) {
6ac552fd 369 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
1da177e4
LT
370 return;
371 }
547b792c
IJ
372
373 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
14afee4b 374 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
547b792c 375 WARN_ON(nlk_sk(sk)->groups);
1da177e4
LT
376}
377
707693c8
HX
378static void netlink_sock_destruct_work(struct work_struct *work)
379{
380 struct netlink_sock *nlk = container_of(work, struct netlink_sock,
381 work);
382
ed5d7788 383 sk_free(&nlk->sk);
707693c8
HX
384}
385
6ac552fd
PM
386/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
387 * SMP. Look, when several writers sleep and reader wakes them up, all but one
1da177e4
LT
388 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
389 * this, _but_ remember, it adds useless work on UP machines.
390 */
391
d136f1bd 392void netlink_table_grab(void)
9a429c49 393 __acquires(nl_table_lock)
1da177e4 394{
d136f1bd
JB
395 might_sleep();
396
6abd219c 397 write_lock_irq(&nl_table_lock);
1da177e4
LT
398
399 if (atomic_read(&nl_table_users)) {
400 DECLARE_WAITQUEUE(wait, current);
401
402 add_wait_queue_exclusive(&nl_table_wait, &wait);
6ac552fd 403 for (;;) {
1da177e4
LT
404 set_current_state(TASK_UNINTERRUPTIBLE);
405 if (atomic_read(&nl_table_users) == 0)
406 break;
6abd219c 407 write_unlock_irq(&nl_table_lock);
1da177e4 408 schedule();
6abd219c 409 write_lock_irq(&nl_table_lock);
1da177e4
LT
410 }
411
412 __set_current_state(TASK_RUNNING);
413 remove_wait_queue(&nl_table_wait, &wait);
414 }
415}
416
d136f1bd 417void netlink_table_ungrab(void)
9a429c49 418 __releases(nl_table_lock)
1da177e4 419{
6abd219c 420 write_unlock_irq(&nl_table_lock);
1da177e4
LT
421 wake_up(&nl_table_wait);
422}
423
6ac552fd 424static inline void
1da177e4
LT
425netlink_lock_table(void)
426{
427 /* read_lock() synchronizes us to netlink_table_grab */
428
429 read_lock(&nl_table_lock);
430 atomic_inc(&nl_table_users);
431 read_unlock(&nl_table_lock);
432}
433
6ac552fd 434static inline void
1da177e4
LT
435netlink_unlock_table(void)
436{
437 if (atomic_dec_and_test(&nl_table_users))
438 wake_up(&nl_table_wait);
439}
440
e341694e 441struct netlink_compare_arg
1da177e4 442{
c428ecd1 443 possible_net_t pnet;
e341694e
TG
444 u32 portid;
445};
1da177e4 446
8f2ddaac
HX
447/* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
448#define netlink_compare_arg_len \
449 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
c428ecd1
HX
450
451static inline int netlink_compare(struct rhashtable_compare_arg *arg,
452 const void *ptr)
1da177e4 453{
c428ecd1
HX
454 const struct netlink_compare_arg *x = arg->key;
455 const struct netlink_sock *nlk = ptr;
1da177e4 456
da314c99 457 return nlk->portid != x->portid ||
c428ecd1
HX
458 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
459}
460
461static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
462 struct net *net, u32 portid)
463{
464 memset(arg, 0, sizeof(*arg));
465 write_pnet(&arg->pnet, net);
466 arg->portid = portid;
1da177e4
LT
467}
468
e341694e
TG
469static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
470 struct net *net)
1da177e4 471{
c428ecd1 472 struct netlink_compare_arg arg;
1da177e4 473
c428ecd1
HX
474 netlink_compare_arg_init(&arg, net, portid);
475 return rhashtable_lookup_fast(&table->hash, &arg,
476 netlink_rhashtable_params);
1da177e4
LT
477}
478
c428ecd1 479static int __netlink_insert(struct netlink_table *table, struct sock *sk)
c5adde94 480{
c428ecd1 481 struct netlink_compare_arg arg;
c5adde94 482
da314c99 483 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
c428ecd1
HX
484 return rhashtable_lookup_insert_key(&table->hash, &arg,
485 &nlk_sk(sk)->node,
486 netlink_rhashtable_params);
c5adde94
YX
487}
488
e341694e 489static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
1da177e4 490{
e341694e
TG
491 struct netlink_table *table = &nl_table[protocol];
492 struct sock *sk;
1da177e4 493
e341694e
TG
494 rcu_read_lock();
495 sk = __netlink_lookup(table, portid, net);
496 if (sk)
497 sock_hold(sk);
498 rcu_read_unlock();
1da177e4 499
e341694e 500 return sk;
1da177e4
LT
501}
502
90ddc4f0 503static const struct proto_ops netlink_ops;
1da177e4 504
4277a083
PM
505static void
506netlink_update_listeners(struct sock *sk)
507{
508 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
4277a083
PM
509 unsigned long mask;
510 unsigned int i;
6d772ac5
ED
511 struct listeners *listeners;
512
513 listeners = nl_deref_protected(tbl->listeners);
514 if (!listeners)
515 return;
4277a083 516
b4ff4f04 517 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
4277a083 518 mask = 0;
b67bfe0d 519 sk_for_each_bound(sk, &tbl->mc_list) {
b4ff4f04
JB
520 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
521 mask |= nlk_sk(sk)->groups[i];
522 }
6d772ac5 523 listeners->masks[i] = mask;
4277a083
PM
524 }
525 /* this function is only called with the netlink table "grabbed", which
526 * makes sure updates are visible before bind or setsockopt return. */
527}
528
8ea65f4a 529static int netlink_insert(struct sock *sk, u32 portid)
1da177e4 530{
da12c90e 531 struct netlink_table *table = &nl_table[sk->sk_protocol];
919d9db9 532 int err;
1da177e4 533
c5adde94 534 lock_sock(sk);
1da177e4 535
da314c99
HX
536 err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
537 if (nlk_sk(sk)->bound)
1da177e4
LT
538 goto err;
539
540 err = -ENOMEM;
97defe1e
TG
541 if (BITS_PER_LONG > 32 &&
542 unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
1da177e4
LT
543 goto err;
544
da314c99 545 nlk_sk(sk)->portid = portid;
e341694e 546 sock_hold(sk);
919d9db9 547
c428ecd1
HX
548 err = __netlink_insert(table, sk);
549 if (err) {
4e7c1330
DB
550 /* In case the hashtable backend returns with -EBUSY
551 * from here, it must not escape to the caller.
552 */
553 if (unlikely(err == -EBUSY))
554 err = -EOVERFLOW;
c428ecd1
HX
555 if (err == -EEXIST)
556 err = -EADDRINUSE;
c5adde94 557 sock_put(sk);
1f770c0a 558 goto err;
919d9db9
HX
559 }
560
da314c99
HX
561 /* We need to ensure that the socket is hashed and visible. */
562 smp_wmb();
563 nlk_sk(sk)->bound = portid;
1f770c0a 564
1da177e4 565err:
c5adde94 566 release_sock(sk);
1da177e4
LT
567 return err;
568}
569
570static void netlink_remove(struct sock *sk)
571{
e341694e
TG
572 struct netlink_table *table;
573
e341694e 574 table = &nl_table[sk->sk_protocol];
c428ecd1
HX
575 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
576 netlink_rhashtable_params)) {
41c6d650 577 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
e341694e
TG
578 __sock_put(sk);
579 }
e341694e 580
1da177e4 581 netlink_table_grab();
b10dcb3b 582 if (nlk_sk(sk)->subscriptions) {
1da177e4 583 __sk_del_bind_node(sk);
b10dcb3b
JB
584 netlink_update_listeners(sk);
585 }
ee1c2442
JB
586 if (sk->sk_protocol == NETLINK_GENERIC)
587 atomic_inc(&genl_sk_destructing_cnt);
1da177e4
LT
588 netlink_table_ungrab();
589}
590
591static struct proto netlink_proto = {
592 .name = "NETLINK",
593 .owner = THIS_MODULE,
594 .obj_size = sizeof(struct netlink_sock),
595};
596
1b8d7ae4 597static int __netlink_create(struct net *net, struct socket *sock,
11aa9c28
EB
598 struct mutex *cb_mutex, int protocol,
599 int kern)
1da177e4
LT
600{
601 struct sock *sk;
602 struct netlink_sock *nlk;
ab33a171
PM
603
604 sock->ops = &netlink_ops;
605
11aa9c28 606 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
ab33a171
PM
607 if (!sk)
608 return -ENOMEM;
609
610 sock_init_data(sock, sk);
611
612 nlk = nlk_sk(sk);
658cb354 613 if (cb_mutex) {
ffa4d721 614 nlk->cb_mutex = cb_mutex;
658cb354 615 } else {
ffa4d721
PM
616 nlk->cb_mutex = &nlk->cb_def_mutex;
617 mutex_init(nlk->cb_mutex);
8a0f5ccf
HX
618 lockdep_set_class_and_name(nlk->cb_mutex,
619 nlk_cb_mutex_keys + protocol,
620 nlk_cb_mutex_key_strings[protocol]);
ffa4d721 621 }
ab33a171
PM
622 init_waitqueue_head(&nlk->wait);
623
624 sk->sk_destruct = netlink_sock_destruct;
625 sk->sk_protocol = protocol;
626 return 0;
627}
628
3f378b68
EP
629static int netlink_create(struct net *net, struct socket *sock, int protocol,
630 int kern)
ab33a171
PM
631{
632 struct module *module = NULL;
af65bdfc 633 struct mutex *cb_mutex;
f7fa9b10 634 struct netlink_sock *nlk;
023e2cfa
JB
635 int (*bind)(struct net *net, int group);
636 void (*unbind)(struct net *net, int group);
ab33a171 637 int err = 0;
1da177e4
LT
638
639 sock->state = SS_UNCONNECTED;
640
641 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
642 return -ESOCKTNOSUPPORT;
643
6ac552fd 644 if (protocol < 0 || protocol >= MAX_LINKS)
1da177e4
LT
645 return -EPROTONOSUPPORT;
646
77247bbb 647 netlink_lock_table();
95a5afca 648#ifdef CONFIG_MODULES
ab33a171 649 if (!nl_table[protocol].registered) {
77247bbb 650 netlink_unlock_table();
4fdb3bb7 651 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
77247bbb 652 netlink_lock_table();
4fdb3bb7 653 }
ab33a171
PM
654#endif
655 if (nl_table[protocol].registered &&
656 try_module_get(nl_table[protocol].module))
657 module = nl_table[protocol].module;
974c37e9
AD
658 else
659 err = -EPROTONOSUPPORT;
af65bdfc 660 cb_mutex = nl_table[protocol].cb_mutex;
03292745 661 bind = nl_table[protocol].bind;
4f520900 662 unbind = nl_table[protocol].unbind;
77247bbb 663 netlink_unlock_table();
4fdb3bb7 664
974c37e9
AD
665 if (err < 0)
666 goto out;
667
11aa9c28 668 err = __netlink_create(net, sock, cb_mutex, protocol, kern);
6ac552fd 669 if (err < 0)
f7fa9b10
PM
670 goto out_module;
671
6f756a8c 672 local_bh_disable();
c1fd3b94 673 sock_prot_inuse_add(net, &netlink_proto, 1);
6f756a8c
DM
674 local_bh_enable();
675
f7fa9b10 676 nlk = nlk_sk(sock->sk);
f7fa9b10 677 nlk->module = module;
03292745 678 nlk->netlink_bind = bind;
4f520900 679 nlk->netlink_unbind = unbind;
ab33a171
PM
680out:
681 return err;
1da177e4 682
ab33a171
PM
683out_module:
684 module_put(module);
685 goto out;
1da177e4
LT
686}
687
21e4902a
TG
688static void deferred_put_nlk_sk(struct rcu_head *head)
689{
690 struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
ed5d7788
HX
691 struct sock *sk = &nlk->sk;
692
be82485f
XL
693 kfree(nlk->groups);
694 nlk->groups = NULL;
695
41c6d650 696 if (!refcount_dec_and_test(&sk->sk_refcnt))
ed5d7788
HX
697 return;
698
699 if (nlk->cb_running && nlk->cb.done) {
700 INIT_WORK(&nlk->work, netlink_sock_destruct_work);
701 schedule_work(&nlk->work);
702 return;
703 }
21e4902a 704
ed5d7788 705 sk_free(sk);
21e4902a
TG
706}
707
1da177e4
LT
708static int netlink_release(struct socket *sock)
709{
710 struct sock *sk = sock->sk;
711 struct netlink_sock *nlk;
712
713 if (!sk)
714 return 0;
715
716 netlink_remove(sk);
ac57b3a9 717 sock_orphan(sk);
1da177e4
LT
718 nlk = nlk_sk(sk);
719
3f660d66
HX
720 /*
721 * OK. Socket is unlinked, any packets that arrive now
722 * will be purged.
723 */
1da177e4 724
ee1c2442
JB
725 /* must not acquire netlink_table_lock in any way again before unbind
726 * and notifying genetlink is done as otherwise it might deadlock
727 */
728 if (nlk->netlink_unbind) {
729 int i;
730
731 for (i = 0; i < nlk->ngroups; i++)
732 if (test_bit(i, nlk->groups))
733 nlk->netlink_unbind(sock_net(sk), i + 1);
734 }
735 if (sk->sk_protocol == NETLINK_GENERIC &&
736 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
737 wake_up(&genl_sk_destructing_waitq);
738
1da177e4
LT
739 sock->sk = NULL;
740 wake_up_interruptible_all(&nlk->wait);
741
742 skb_queue_purge(&sk->sk_write_queue);
743
e2726020 744 if (nlk->portid && nlk->bound) {
1da177e4 745 struct netlink_notify n = {
3b1e0a65 746 .net = sock_net(sk),
1da177e4 747 .protocol = sk->sk_protocol,
15e47304 748 .portid = nlk->portid,
1da177e4 749 };
efa172f4 750 blocking_notifier_call_chain(&netlink_chain,
e041c683 751 NETLINK_URELEASE, &n);
746fac4d 752 }
4fdb3bb7 753
5e7c001c 754 module_put(nlk->module);
4fdb3bb7 755
aed81560 756 if (netlink_is_kernel(sk)) {
b10dcb3b 757 netlink_table_grab();
869e58f8
DL
758 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
759 if (--nl_table[sk->sk_protocol].registered == 0) {
6d772ac5
ED
760 struct listeners *old;
761
762 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
763 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
764 kfree_rcu(old, rcu);
869e58f8 765 nl_table[sk->sk_protocol].module = NULL;
9785e10a 766 nl_table[sk->sk_protocol].bind = NULL;
4f520900 767 nl_table[sk->sk_protocol].unbind = NULL;
9785e10a 768 nl_table[sk->sk_protocol].flags = 0;
869e58f8
DL
769 nl_table[sk->sk_protocol].registered = 0;
770 }
b10dcb3b 771 netlink_table_ungrab();
658cb354 772 }
77247bbb 773
3755810c 774 local_bh_disable();
c1fd3b94 775 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
3755810c 776 local_bh_enable();
21e4902a 777 call_rcu(&nlk->rcu, deferred_put_nlk_sk);
1da177e4
LT
778 return 0;
779}
780
781static int netlink_autobind(struct socket *sock)
782{
783 struct sock *sk = sock->sk;
3b1e0a65 784 struct net *net = sock_net(sk);
da12c90e 785 struct netlink_table *table = &nl_table[sk->sk_protocol];
15e47304 786 s32 portid = task_tgid_vnr(current);
1da177e4 787 int err;
b9fbe709
HX
788 s32 rover = -4096;
789 bool ok;
1da177e4
LT
790
791retry:
792 cond_resched();
e341694e 793 rcu_read_lock();
b9fbe709
HX
794 ok = !__netlink_lookup(table, portid, net);
795 rcu_read_unlock();
796 if (!ok) {
e341694e 797 /* Bind collision, search negative portid values. */
b9fbe709
HX
798 if (rover == -4096)
799 /* rover will be in range [S32_MIN, -4097] */
800 rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
801 else if (rover >= -4096)
e341694e 802 rover = -4097;
b9fbe709 803 portid = rover--;
e341694e 804 goto retry;
1da177e4 805 }
1da177e4 806
8ea65f4a 807 err = netlink_insert(sk, portid);
1da177e4
LT
808 if (err == -EADDRINUSE)
809 goto retry;
d470e3b4
DM
810
811 /* If 2 threads race to autobind, that is fine. */
812 if (err == -EBUSY)
813 err = 0;
814
815 return err;
1da177e4
LT
816}
817
aa4cf945
EB
818/**
819 * __netlink_ns_capable - General netlink message capability test
820 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
821 * @user_ns: The user namespace of the capability to use
822 * @cap: The capability to use
823 *
824 * Test to see if the opener of the socket we received the message
825 * from had when the netlink socket was created and the sender of the
826 * message has has the capability @cap in the user namespace @user_ns.
827 */
828bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
829 struct user_namespace *user_ns, int cap)
830{
2d7a85f4
EB
831 return ((nsp->flags & NETLINK_SKB_DST) ||
832 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
833 ns_capable(user_ns, cap);
aa4cf945
EB
834}
835EXPORT_SYMBOL(__netlink_ns_capable);
836
837/**
838 * netlink_ns_capable - General netlink message capability test
839 * @skb: socket buffer holding a netlink command from userspace
840 * @user_ns: The user namespace of the capability to use
841 * @cap: The capability to use
842 *
843 * Test to see if the opener of the socket we received the message
844 * from had when the netlink socket was created and the sender of the
845 * message has has the capability @cap in the user namespace @user_ns.
846 */
847bool netlink_ns_capable(const struct sk_buff *skb,
848 struct user_namespace *user_ns, int cap)
849{
850 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
851}
852EXPORT_SYMBOL(netlink_ns_capable);
853
854/**
855 * netlink_capable - Netlink global message capability test
856 * @skb: socket buffer holding a netlink command from userspace
857 * @cap: The capability to use
858 *
859 * Test to see if the opener of the socket we received the message
860 * from had when the netlink socket was created and the sender of the
861 * message has has the capability @cap in all user namespaces.
862 */
863bool netlink_capable(const struct sk_buff *skb, int cap)
864{
865 return netlink_ns_capable(skb, &init_user_ns, cap);
866}
867EXPORT_SYMBOL(netlink_capable);
868
869/**
870 * netlink_net_capable - Netlink network namespace message capability test
871 * @skb: socket buffer holding a netlink command from userspace
872 * @cap: The capability to use
873 *
874 * Test to see if the opener of the socket we received the message
875 * from had when the netlink socket was created and the sender of the
876 * message has has the capability @cap over the network namespace of
877 * the socket we received the message from.
878 */
879bool netlink_net_capable(const struct sk_buff *skb, int cap)
880{
881 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
882}
883EXPORT_SYMBOL(netlink_net_capable);
884
5187cd05 885static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
746fac4d 886{
9785e10a 887 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
df008c91 888 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
746fac4d 889}
1da177e4 890
f7fa9b10
PM
891static void
892netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
893{
894 struct netlink_sock *nlk = nlk_sk(sk);
895
896 if (nlk->subscriptions && !subscriptions)
897 __sk_del_bind_node(sk);
898 else if (!nlk->subscriptions && subscriptions)
899 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
900 nlk->subscriptions = subscriptions;
901}
902
b4ff4f04 903static int netlink_realloc_groups(struct sock *sk)
513c2500
PM
904{
905 struct netlink_sock *nlk = nlk_sk(sk);
906 unsigned int groups;
b4ff4f04 907 unsigned long *new_groups;
513c2500
PM
908 int err = 0;
909
b4ff4f04
JB
910 netlink_table_grab();
911
513c2500 912 groups = nl_table[sk->sk_protocol].groups;
b4ff4f04 913 if (!nl_table[sk->sk_protocol].registered) {
513c2500 914 err = -ENOENT;
b4ff4f04
JB
915 goto out_unlock;
916 }
513c2500 917
b4ff4f04
JB
918 if (nlk->ngroups >= groups)
919 goto out_unlock;
513c2500 920
b4ff4f04
JB
921 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
922 if (new_groups == NULL) {
923 err = -ENOMEM;
924 goto out_unlock;
925 }
6ac552fd 926 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
b4ff4f04
JB
927 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
928
929 nlk->groups = new_groups;
513c2500 930 nlk->ngroups = groups;
b4ff4f04
JB
931 out_unlock:
932 netlink_table_ungrab();
933 return err;
513c2500
PM
934}
935
02c81ab9 936static void netlink_undo_bind(int group, long unsigned int groups,
023e2cfa 937 struct sock *sk)
4f520900 938{
023e2cfa 939 struct netlink_sock *nlk = nlk_sk(sk);
4f520900
RGB
940 int undo;
941
942 if (!nlk->netlink_unbind)
943 return;
944
945 for (undo = 0; undo < group; undo++)
6251edd9 946 if (test_bit(undo, &groups))
8b7c36d8 947 nlk->netlink_unbind(sock_net(sk), undo + 1);
4f520900
RGB
948}
949
6ac552fd
PM
950static int netlink_bind(struct socket *sock, struct sockaddr *addr,
951 int addr_len)
1da177e4
LT
952{
953 struct sock *sk = sock->sk;
3b1e0a65 954 struct net *net = sock_net(sk);
1da177e4
LT
955 struct netlink_sock *nlk = nlk_sk(sk);
956 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
f7736080 957 int err = 0;
4f520900 958 long unsigned int groups = nladdr->nl_groups;
da314c99 959 bool bound;
746fac4d 960
4e4b5376
HFS
961 if (addr_len < sizeof(struct sockaddr_nl))
962 return -EINVAL;
963
1da177e4
LT
964 if (nladdr->nl_family != AF_NETLINK)
965 return -EINVAL;
966
967 /* Only superuser is allowed to listen multicasts */
4f520900 968 if (groups) {
5187cd05 969 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
513c2500 970 return -EPERM;
b4ff4f04
JB
971 err = netlink_realloc_groups(sk);
972 if (err)
973 return err;
513c2500 974 }
1da177e4 975
da314c99
HX
976 bound = nlk->bound;
977 if (bound) {
978 /* Ensure nlk->portid is up-to-date. */
979 smp_rmb();
980
15e47304 981 if (nladdr->nl_pid != nlk->portid)
1da177e4 982 return -EINVAL;
da314c99 983 }
4f520900 984
f7736080 985 netlink_lock_table();
4f520900
RGB
986 if (nlk->netlink_bind && groups) {
987 int group;
988
989 for (group = 0; group < nlk->ngroups; group++) {
990 if (!test_bit(group, &groups))
991 continue;
8b7c36d8 992 err = nlk->netlink_bind(net, group + 1);
4f520900
RGB
993 if (!err)
994 continue;
023e2cfa 995 netlink_undo_bind(group, groups, sk);
f7736080 996 goto unlock;
4f520900
RGB
997 }
998 }
999
da314c99
HX
1000 /* No need for barriers here as we return to user-space without
1001 * using any of the bound attributes.
1002 */
1003 if (!bound) {
1da177e4 1004 err = nladdr->nl_pid ?
8ea65f4a 1005 netlink_insert(sk, nladdr->nl_pid) :
1da177e4 1006 netlink_autobind(sock);
4f520900 1007 if (err) {
023e2cfa 1008 netlink_undo_bind(nlk->ngroups, groups, sk);
f7736080 1009 goto unlock;
4f520900 1010 }
1da177e4
LT
1011 }
1012
4f520900 1013 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
f7736080
XL
1014 goto unlock;
1015 netlink_unlock_table();
1da177e4
LT
1016
1017 netlink_table_grab();
f7fa9b10 1018 netlink_update_subscriptions(sk, nlk->subscriptions +
4f520900 1019 hweight32(groups) -
746fac4d 1020 hweight32(nlk->groups[0]));
4f520900 1021 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
4277a083 1022 netlink_update_listeners(sk);
1da177e4
LT
1023 netlink_table_ungrab();
1024
1025 return 0;
f7736080
XL
1026
1027unlock:
1028 netlink_unlock_table();
1029 return err;
1da177e4
LT
1030}
1031
1032static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1033 int alen, int flags)
1034{
1035 int err = 0;
1036 struct sock *sk = sock->sk;
1037 struct netlink_sock *nlk = nlk_sk(sk);
6ac552fd 1038 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1da177e4 1039
6503d961
CG
1040 if (alen < sizeof(addr->sa_family))
1041 return -EINVAL;
1042
1da177e4
LT
1043 if (addr->sa_family == AF_UNSPEC) {
1044 sk->sk_state = NETLINK_UNCONNECTED;
15e47304 1045 nlk->dst_portid = 0;
d629b836 1046 nlk->dst_group = 0;
1da177e4
LT
1047 return 0;
1048 }
1049 if (addr->sa_family != AF_NETLINK)
1050 return -EINVAL;
1051
46833a86 1052 if ((nladdr->nl_groups || nladdr->nl_pid) &&
5187cd05 1053 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1da177e4
LT
1054 return -EPERM;
1055
da314c99
HX
1056 /* No need for barriers here as we return to user-space without
1057 * using any of the bound attributes.
1058 */
1059 if (!nlk->bound)
1da177e4
LT
1060 err = netlink_autobind(sock);
1061
1062 if (err == 0) {
1063 sk->sk_state = NETLINK_CONNECTED;
15e47304 1064 nlk->dst_portid = nladdr->nl_pid;
d629b836 1065 nlk->dst_group = ffs(nladdr->nl_groups);
1da177e4
LT
1066 }
1067
1068 return err;
1069}
1070
6ac552fd
PM
1071static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1072 int *addr_len, int peer)
1da177e4
LT
1073{
1074 struct sock *sk = sock->sk;
1075 struct netlink_sock *nlk = nlk_sk(sk);
13cfa97b 1076 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
746fac4d 1077
1da177e4
LT
1078 nladdr->nl_family = AF_NETLINK;
1079 nladdr->nl_pad = 0;
1080 *addr_len = sizeof(*nladdr);
1081
1082 if (peer) {
15e47304 1083 nladdr->nl_pid = nlk->dst_portid;
d629b836 1084 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1da177e4 1085 } else {
15e47304 1086 nladdr->nl_pid = nlk->portid;
f7736080 1087 netlink_lock_table();
513c2500 1088 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
f7736080 1089 netlink_unlock_table();
1da177e4
LT
1090 }
1091 return 0;
1092}
1093
025c6818
DD
1094static int netlink_ioctl(struct socket *sock, unsigned int cmd,
1095 unsigned long arg)
1096{
1097 /* try to hand this ioctl down to the NIC drivers.
1098 */
1099 return -ENOIOCTLCMD;
1100}
1101
15e47304 1102static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1da177e4 1103{
1da177e4
LT
1104 struct sock *sock;
1105 struct netlink_sock *nlk;
1106
15e47304 1107 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1da177e4
LT
1108 if (!sock)
1109 return ERR_PTR(-ECONNREFUSED);
1110
1111 /* Don't bother queuing skb if kernel socket has no input function */
1112 nlk = nlk_sk(sock);
cd40b7d3 1113 if (sock->sk_state == NETLINK_CONNECTED &&
15e47304 1114 nlk->dst_portid != nlk_sk(ssk)->portid) {
1da177e4
LT
1115 sock_put(sock);
1116 return ERR_PTR(-ECONNREFUSED);
1117 }
1118 return sock;
1119}
1120
1121struct sock *netlink_getsockbyfilp(struct file *filp)
1122{
496ad9aa 1123 struct inode *inode = file_inode(filp);
1da177e4
LT
1124 struct sock *sock;
1125
1126 if (!S_ISSOCK(inode->i_mode))
1127 return ERR_PTR(-ENOTSOCK);
1128
1129 sock = SOCKET_I(inode)->sk;
1130 if (sock->sk_family != AF_NETLINK)
1131 return ERR_PTR(-EINVAL);
1132
1133 sock_hold(sock);
1134 return sock;
1135}
1136
3a36515f
PN
1137static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1138 int broadcast)
c05cdb1b
PNA
1139{
1140 struct sk_buff *skb;
1141 void *data;
1142
3a36515f 1143 if (size <= NLMSG_GOODSIZE || broadcast)
c05cdb1b
PNA
1144 return alloc_skb(size, GFP_KERNEL);
1145
3a36515f
PN
1146 size = SKB_DATA_ALIGN(size) +
1147 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c05cdb1b
PNA
1148
1149 data = vmalloc(size);
1150 if (data == NULL)
3a36515f 1151 return NULL;
c05cdb1b 1152
2ea2f62c 1153 skb = __build_skb(data, size);
3a36515f
PN
1154 if (skb == NULL)
1155 vfree(data);
2ea2f62c 1156 else
3a36515f 1157 skb->destructor = netlink_skb_destructor;
c05cdb1b
PNA
1158
1159 return skb;
c05cdb1b
PNA
1160}
1161
1da177e4
LT
1162/*
1163 * Attach a skb to a netlink socket.
1164 * The caller must hold a reference to the destination socket. On error, the
1165 * reference is dropped. The skb is not send to the destination, just all
1166 * all error checks are performed and memory in the queue is reserved.
1167 * Return values:
1168 * < 0: error. skb freed, reference to sock dropped.
1169 * 0: continue
1170 * 1: repeat lookup - reference dropped while waiting for socket memory.
1171 */
9457afee 1172int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 1173 long *timeo, struct sock *ssk)
1da177e4
LT
1174{
1175 struct netlink_sock *nlk;
1176
1177 nlk = nlk_sk(sk);
1178
5fd96123 1179 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
d1b4c689 1180 test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
1da177e4 1181 DECLARE_WAITQUEUE(wait, current);
c3d8d1e3 1182 if (!*timeo) {
aed81560 1183 if (!ssk || netlink_is_kernel(ssk))
1da177e4
LT
1184 netlink_overrun(sk);
1185 sock_put(sk);
1186 kfree_skb(skb);
1187 return -EAGAIN;
1188 }
1189
1190 __set_current_state(TASK_INTERRUPTIBLE);
1191 add_wait_queue(&nlk->wait, &wait);
1192
1193 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
cc3a572f 1194 test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
1da177e4 1195 !sock_flag(sk, SOCK_DEAD))
c3d8d1e3 1196 *timeo = schedule_timeout(*timeo);
1da177e4
LT
1197
1198 __set_current_state(TASK_RUNNING);
1199 remove_wait_queue(&nlk->wait, &wait);
1200 sock_put(sk);
1201
1202 if (signal_pending(current)) {
1203 kfree_skb(skb);
c3d8d1e3 1204 return sock_intr_errno(*timeo);
1da177e4
LT
1205 }
1206 return 1;
1207 }
cf0a018a 1208 netlink_skb_set_owner_r(skb, sk);
1da177e4
LT
1209 return 0;
1210}
1211
4a7e7c2a 1212static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1da177e4 1213{
1da177e4
LT
1214 int len = skb->len;
1215
bcbde0d4
DB
1216 netlink_deliver_tap(skb);
1217
d1b4c689 1218 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 1219 sk->sk_data_ready(sk);
4a7e7c2a
ED
1220 return len;
1221}
1222
1223int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1224{
1225 int len = __netlink_sendskb(sk, skb);
1226
1da177e4
LT
1227 sock_put(sk);
1228 return len;
1229}
1230
1231void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1232{
1233 kfree_skb(skb);
1234 sock_put(sk);
1235}
1236
b57ef81f 1237static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1da177e4
LT
1238{
1239 int delta;
1240
1298ca46 1241 WARN_ON(skb->sk != NULL);
4305b541 1242 delta = skb->end - skb->tail;
c05cdb1b 1243 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
1da177e4
LT
1244 return skb;
1245
1246 if (skb_shared(skb)) {
1247 struct sk_buff *nskb = skb_clone(skb, allocation);
1248 if (!nskb)
1249 return skb;
8460c00f 1250 consume_skb(skb);
1da177e4
LT
1251 skb = nskb;
1252 }
1253
158f323b
ED
1254 pskb_expand_head(skb, 0, -delta,
1255 (allocation & ~__GFP_DIRECT_RECLAIM) |
1256 __GFP_NOWARN | __GFP_NORETRY);
1da177e4
LT
1257 return skb;
1258}
1259
3fbc2905
EB
1260static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1261 struct sock *ssk)
cd40b7d3
DL
1262{
1263 int ret;
1264 struct netlink_sock *nlk = nlk_sk(sk);
1265
1266 ret = -ECONNREFUSED;
1267 if (nlk->netlink_rcv != NULL) {
1268 ret = skb->len;
cf0a018a 1269 netlink_skb_set_owner_r(skb, sk);
e32123e5 1270 NETLINK_CB(skb).sk = ssk;
73bfd370 1271 netlink_deliver_tap_kernel(sk, ssk, skb);
cd40b7d3 1272 nlk->netlink_rcv(skb);
bfb253c9
ED
1273 consume_skb(skb);
1274 } else {
1275 kfree_skb(skb);
cd40b7d3 1276 }
cd40b7d3
DL
1277 sock_put(sk);
1278 return ret;
1279}
1280
1281int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
15e47304 1282 u32 portid, int nonblock)
1da177e4
LT
1283{
1284 struct sock *sk;
1285 int err;
1286 long timeo;
1287
1288 skb = netlink_trim(skb, gfp_any());
1289
1290 timeo = sock_sndtimeo(ssk, nonblock);
1291retry:
15e47304 1292 sk = netlink_getsockbyportid(ssk, portid);
1da177e4
LT
1293 if (IS_ERR(sk)) {
1294 kfree_skb(skb);
1295 return PTR_ERR(sk);
1296 }
cd40b7d3 1297 if (netlink_is_kernel(sk))
3fbc2905 1298 return netlink_unicast_kernel(sk, skb, ssk);
cd40b7d3 1299
b1153f29 1300 if (sk_filter(sk, skb)) {
84874607 1301 err = skb->len;
b1153f29
SH
1302 kfree_skb(skb);
1303 sock_put(sk);
1304 return err;
1305 }
1306
9457afee 1307 err = netlink_attachskb(sk, skb, &timeo, ssk);
1da177e4
LT
1308 if (err == 1)
1309 goto retry;
1310 if (err)
1311 return err;
1312
7ee015e0 1313 return netlink_sendskb(sk, skb);
1da177e4 1314}
6ac552fd 1315EXPORT_SYMBOL(netlink_unicast);
1da177e4 1316
4277a083
PM
1317int netlink_has_listeners(struct sock *sk, unsigned int group)
1318{
1319 int res = 0;
5c398dc8 1320 struct listeners *listeners;
4277a083 1321
aed81560 1322 BUG_ON(!netlink_is_kernel(sk));
b4ff4f04
JB
1323
1324 rcu_read_lock();
1325 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1326
6d772ac5 1327 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
5c398dc8 1328 res = test_bit(group - 1, listeners->masks);
b4ff4f04
JB
1329
1330 rcu_read_unlock();
1331
4277a083
PM
1332 return res;
1333}
1334EXPORT_SYMBOL_GPL(netlink_has_listeners);
1335
b57ef81f 1336static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1da177e4
LT
1337{
1338 struct netlink_sock *nlk = nlk_sk(sk);
1339
1340 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
cc3a572f 1341 !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
cf0a018a 1342 netlink_skb_set_owner_r(skb, sk);
4a7e7c2a 1343 __netlink_sendskb(sk, skb);
2c645800 1344 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1da177e4
LT
1345 }
1346 return -1;
1347}
1348
1349struct netlink_broadcast_data {
1350 struct sock *exclude_sk;
b4b51029 1351 struct net *net;
15e47304 1352 u32 portid;
1da177e4
LT
1353 u32 group;
1354 int failure;
ff491a73 1355 int delivery_failure;
1da177e4
LT
1356 int congested;
1357 int delivered;
7d877f3b 1358 gfp_t allocation;
1da177e4 1359 struct sk_buff *skb, *skb2;
910a7e90
EB
1360 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1361 void *tx_data;
1da177e4
LT
1362};
1363
46c9521f
RR
1364static void do_one_broadcast(struct sock *sk,
1365 struct netlink_broadcast_data *p)
1da177e4
LT
1366{
1367 struct netlink_sock *nlk = nlk_sk(sk);
1368 int val;
1369
1370 if (p->exclude_sk == sk)
46c9521f 1371 return;
1da177e4 1372
15e47304 1373 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 1374 !test_bit(p->group - 1, nlk->groups))
46c9521f 1375 return;
1da177e4 1376
59324cf3
ND
1377 if (!net_eq(sock_net(sk), p->net)) {
1378 if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
1379 return;
1380
1381 if (!peernet_has_id(sock_net(sk), p->net))
1382 return;
1383
1384 if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
1385 CAP_NET_BROADCAST))
1386 return;
1387 }
b4b51029 1388
1da177e4
LT
1389 if (p->failure) {
1390 netlink_overrun(sk);
46c9521f 1391 return;
1da177e4
LT
1392 }
1393
1394 sock_hold(sk);
1395 if (p->skb2 == NULL) {
68acc024 1396 if (skb_shared(p->skb)) {
1da177e4
LT
1397 p->skb2 = skb_clone(p->skb, p->allocation);
1398 } else {
68acc024
TC
1399 p->skb2 = skb_get(p->skb);
1400 /*
1401 * skb ownership may have been set when
1402 * delivered to a previous socket.
1403 */
1404 skb_orphan(p->skb2);
1da177e4
LT
1405 }
1406 }
1407 if (p->skb2 == NULL) {
1408 netlink_overrun(sk);
1409 /* Clone failed. Notify ALL listeners. */
1410 p->failure = 1;
cc3a572f 1411 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
be0c22a4 1412 p->delivery_failure = 1;
59324cf3
ND
1413 goto out;
1414 }
1415 if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
910a7e90
EB
1416 kfree_skb(p->skb2);
1417 p->skb2 = NULL;
59324cf3
ND
1418 goto out;
1419 }
1420 if (sk_filter(sk, p->skb2)) {
b1153f29
SH
1421 kfree_skb(p->skb2);
1422 p->skb2 = NULL;
59324cf3
ND
1423 goto out;
1424 }
1425 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
7212462f
ND
1426 if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
1427 NETLINK_CB(p->skb2).nsid_is_set = true;
59324cf3
ND
1428 val = netlink_broadcast_deliver(sk, p->skb2);
1429 if (val < 0) {
1da177e4 1430 netlink_overrun(sk);
cc3a572f 1431 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
be0c22a4 1432 p->delivery_failure = 1;
1da177e4
LT
1433 } else {
1434 p->congested |= val;
1435 p->delivered = 1;
1436 p->skb2 = NULL;
1437 }
59324cf3 1438out:
1da177e4 1439 sock_put(sk);
1da177e4
LT
1440}
1441
15e47304 1442int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
1443 u32 group, gfp_t allocation,
1444 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1445 void *filter_data)
1da177e4 1446{
3b1e0a65 1447 struct net *net = sock_net(ssk);
1da177e4 1448 struct netlink_broadcast_data info;
1da177e4
LT
1449 struct sock *sk;
1450
1451 skb = netlink_trim(skb, allocation);
1452
1453 info.exclude_sk = ssk;
b4b51029 1454 info.net = net;
15e47304 1455 info.portid = portid;
1da177e4
LT
1456 info.group = group;
1457 info.failure = 0;
ff491a73 1458 info.delivery_failure = 0;
1da177e4
LT
1459 info.congested = 0;
1460 info.delivered = 0;
1461 info.allocation = allocation;
1462 info.skb = skb;
1463 info.skb2 = NULL;
910a7e90
EB
1464 info.tx_filter = filter;
1465 info.tx_data = filter_data;
1da177e4
LT
1466
1467 /* While we sleep in clone, do not allow to change socket list */
1468
1469 netlink_lock_table();
1470
b67bfe0d 1471 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1da177e4
LT
1472 do_one_broadcast(sk, &info);
1473
70d4bf6d 1474 consume_skb(skb);
aa1c6a6f 1475
1da177e4
LT
1476 netlink_unlock_table();
1477
70d4bf6d
NH
1478 if (info.delivery_failure) {
1479 kfree_skb(info.skb2);
ff491a73 1480 return -ENOBUFS;
658cb354
ED
1481 }
1482 consume_skb(info.skb2);
ff491a73 1483
1da177e4 1484 if (info.delivered) {
d0164adc 1485 if (info.congested && gfpflags_allow_blocking(allocation))
1da177e4
LT
1486 yield();
1487 return 0;
1488 }
1da177e4
LT
1489 return -ESRCH;
1490}
910a7e90
EB
1491EXPORT_SYMBOL(netlink_broadcast_filtered);
1492
15e47304 1493int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
1494 u32 group, gfp_t allocation)
1495{
15e47304 1496 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
910a7e90
EB
1497 NULL, NULL);
1498}
6ac552fd 1499EXPORT_SYMBOL(netlink_broadcast);
1da177e4
LT
1500
1501struct netlink_set_err_data {
1502 struct sock *exclude_sk;
15e47304 1503 u32 portid;
1da177e4
LT
1504 u32 group;
1505 int code;
1506};
1507
b57ef81f 1508static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1da177e4
LT
1509{
1510 struct netlink_sock *nlk = nlk_sk(sk);
1a50307b 1511 int ret = 0;
1da177e4
LT
1512
1513 if (sk == p->exclude_sk)
1514 goto out;
1515
09ad9bc7 1516 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
b4b51029
EB
1517 goto out;
1518
15e47304 1519 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 1520 !test_bit(p->group - 1, nlk->groups))
1da177e4
LT
1521 goto out;
1522
cc3a572f 1523 if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
1a50307b
PNA
1524 ret = 1;
1525 goto out;
1526 }
1527
1da177e4
LT
1528 sk->sk_err = p->code;
1529 sk->sk_error_report(sk);
1530out:
1a50307b 1531 return ret;
1da177e4
LT
1532}
1533
4843b93c
PNA
1534/**
1535 * netlink_set_err - report error to broadcast listeners
1536 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
15e47304 1537 * @portid: the PORTID of a process that we want to skip (if any)
840e93f2 1538 * @group: the broadcast group that will notice the error
4843b93c 1539 * @code: error code, must be negative (as usual in kernelspace)
1a50307b
PNA
1540 *
1541 * This function returns the number of broadcast listeners that have set the
cc3a572f 1542 * NETLINK_NO_ENOBUFS socket option.
4843b93c 1543 */
15e47304 1544int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1da177e4
LT
1545{
1546 struct netlink_set_err_data info;
1da177e4 1547 struct sock *sk;
1a50307b 1548 int ret = 0;
1da177e4
LT
1549
1550 info.exclude_sk = ssk;
15e47304 1551 info.portid = portid;
1da177e4 1552 info.group = group;
4843b93c
PNA
1553 /* sk->sk_err wants a positive error value */
1554 info.code = -code;
1da177e4
LT
1555
1556 read_lock(&nl_table_lock);
1557
b67bfe0d 1558 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1a50307b 1559 ret += do_one_set_err(sk, &info);
1da177e4
LT
1560
1561 read_unlock(&nl_table_lock);
1a50307b 1562 return ret;
1da177e4 1563}
dd5b6ce6 1564EXPORT_SYMBOL(netlink_set_err);
1da177e4 1565
84659eb5
JB
1566/* must be called with netlink table grabbed */
1567static void netlink_update_socket_mc(struct netlink_sock *nlk,
1568 unsigned int group,
1569 int is_new)
1570{
1571 int old, new = !!is_new, subscriptions;
1572
1573 old = test_bit(group - 1, nlk->groups);
1574 subscriptions = nlk->subscriptions - old + new;
1575 if (new)
1576 __set_bit(group - 1, nlk->groups);
1577 else
1578 __clear_bit(group - 1, nlk->groups);
1579 netlink_update_subscriptions(&nlk->sk, subscriptions);
1580 netlink_update_listeners(&nlk->sk);
1581}
1582
9a4595bc 1583static int netlink_setsockopt(struct socket *sock, int level, int optname,
b7058842 1584 char __user *optval, unsigned int optlen)
9a4595bc
PM
1585{
1586 struct sock *sk = sock->sk;
1587 struct netlink_sock *nlk = nlk_sk(sk);
eb496534
JB
1588 unsigned int val = 0;
1589 int err;
9a4595bc
PM
1590
1591 if (level != SOL_NETLINK)
1592 return -ENOPROTOOPT;
1593
d1b4c689 1594 if (optlen >= sizeof(int) &&
eb496534 1595 get_user(val, (unsigned int __user *)optval))
9a4595bc
PM
1596 return -EFAULT;
1597
1598 switch (optname) {
1599 case NETLINK_PKTINFO:
1600 if (val)
cc3a572f 1601 nlk->flags |= NETLINK_F_RECV_PKTINFO;
9a4595bc 1602 else
cc3a572f 1603 nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
9a4595bc
PM
1604 err = 0;
1605 break;
1606 case NETLINK_ADD_MEMBERSHIP:
1607 case NETLINK_DROP_MEMBERSHIP: {
5187cd05 1608 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
9a4595bc 1609 return -EPERM;
b4ff4f04
JB
1610 err = netlink_realloc_groups(sk);
1611 if (err)
1612 return err;
9a4595bc
PM
1613 if (!val || val - 1 >= nlk->ngroups)
1614 return -EINVAL;
7774d5e0 1615 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
023e2cfa 1616 err = nlk->netlink_bind(sock_net(sk), val);
4f520900
RGB
1617 if (err)
1618 return err;
1619 }
9a4595bc 1620 netlink_table_grab();
84659eb5
JB
1621 netlink_update_socket_mc(nlk, val,
1622 optname == NETLINK_ADD_MEMBERSHIP);
9a4595bc 1623 netlink_table_ungrab();
7774d5e0 1624 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
023e2cfa 1625 nlk->netlink_unbind(sock_net(sk), val);
03292745 1626
9a4595bc
PM
1627 err = 0;
1628 break;
1629 }
be0c22a4
PNA
1630 case NETLINK_BROADCAST_ERROR:
1631 if (val)
cc3a572f 1632 nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
be0c22a4 1633 else
cc3a572f 1634 nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
be0c22a4
PNA
1635 err = 0;
1636 break;
38938bfe
PNA
1637 case NETLINK_NO_ENOBUFS:
1638 if (val) {
cc3a572f
ND
1639 nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
1640 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
38938bfe 1641 wake_up_interruptible(&nlk->wait);
658cb354 1642 } else {
cc3a572f 1643 nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
658cb354 1644 }
38938bfe
PNA
1645 err = 0;
1646 break;
59324cf3
ND
1647 case NETLINK_LISTEN_ALL_NSID:
1648 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
1649 return -EPERM;
1650
1651 if (val)
1652 nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
1653 else
1654 nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
1655 err = 0;
1656 break;
0a6a3a23
CR
1657 case NETLINK_CAP_ACK:
1658 if (val)
1659 nlk->flags |= NETLINK_F_CAP_ACK;
1660 else
1661 nlk->flags &= ~NETLINK_F_CAP_ACK;
1662 err = 0;
1663 break;
2d4bc933
JB
1664 case NETLINK_EXT_ACK:
1665 if (val)
1666 nlk->flags |= NETLINK_F_EXT_ACK;
1667 else
1668 nlk->flags &= ~NETLINK_F_EXT_ACK;
1669 err = 0;
1670 break;
9a4595bc
PM
1671 default:
1672 err = -ENOPROTOOPT;
1673 }
1674 return err;
1675}
1676
1677static int netlink_getsockopt(struct socket *sock, int level, int optname,
746fac4d 1678 char __user *optval, int __user *optlen)
9a4595bc
PM
1679{
1680 struct sock *sk = sock->sk;
1681 struct netlink_sock *nlk = nlk_sk(sk);
1682 int len, val, err;
1683
1684 if (level != SOL_NETLINK)
1685 return -ENOPROTOOPT;
1686
1687 if (get_user(len, optlen))
1688 return -EFAULT;
1689 if (len < 0)
1690 return -EINVAL;
1691
1692 switch (optname) {
1693 case NETLINK_PKTINFO:
1694 if (len < sizeof(int))
1695 return -EINVAL;
1696 len = sizeof(int);
cc3a572f 1697 val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
a27b58fe
HC
1698 if (put_user(len, optlen) ||
1699 put_user(val, optval))
1700 return -EFAULT;
9a4595bc
PM
1701 err = 0;
1702 break;
be0c22a4
PNA
1703 case NETLINK_BROADCAST_ERROR:
1704 if (len < sizeof(int))
1705 return -EINVAL;
1706 len = sizeof(int);
cc3a572f 1707 val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
be0c22a4
PNA
1708 if (put_user(len, optlen) ||
1709 put_user(val, optval))
1710 return -EFAULT;
1711 err = 0;
1712 break;
38938bfe
PNA
1713 case NETLINK_NO_ENOBUFS:
1714 if (len < sizeof(int))
1715 return -EINVAL;
1716 len = sizeof(int);
cc3a572f 1717 val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
38938bfe
PNA
1718 if (put_user(len, optlen) ||
1719 put_user(val, optval))
1720 return -EFAULT;
1721 err = 0;
1722 break;
b42be38b
DH
1723 case NETLINK_LIST_MEMBERSHIPS: {
1724 int pos, idx, shift;
1725
1726 err = 0;
47191d65 1727 netlink_lock_table();
b42be38b
DH
1728 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
1729 if (len - pos < sizeof(u32))
1730 break;
1731
1732 idx = pos / sizeof(unsigned long);
1733 shift = (pos % sizeof(unsigned long)) * 8;
1734 if (put_user((u32)(nlk->groups[idx] >> shift),
1735 (u32 __user *)(optval + pos))) {
1736 err = -EFAULT;
1737 break;
1738 }
1739 }
1740 if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1741 err = -EFAULT;
47191d65 1742 netlink_unlock_table();
b42be38b
DH
1743 break;
1744 }
0a6a3a23
CR
1745 case NETLINK_CAP_ACK:
1746 if (len < sizeof(int))
1747 return -EINVAL;
1748 len = sizeof(int);
1749 val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1750 if (put_user(len, optlen) ||
1751 put_user(val, optval))
1752 return -EFAULT;
1753 err = 0;
1754 break;
2d4bc933
JB
1755 case NETLINK_EXT_ACK:
1756 if (len < sizeof(int))
1757 return -EINVAL;
1758 len = sizeof(int);
1759 val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1760 if (put_user(len, optlen) || put_user(val, optval))
1761 return -EFAULT;
1762 err = 0;
1763 break;
9a4595bc
PM
1764 default:
1765 err = -ENOPROTOOPT;
1766 }
1767 return err;
1768}
1769
1770static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1771{
1772 struct nl_pktinfo info;
1773
1774 info.group = NETLINK_CB(skb).dst_group;
1775 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1776}
1777
59324cf3
ND
1778static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
1779 struct sk_buff *skb)
1780{
1781 if (!NETLINK_CB(skb).nsid_is_set)
1782 return;
1783
1784 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
1785 &NETLINK_CB(skb).nsid);
1786}
1787
1b784140 1788static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1da177e4 1789{
1da177e4
LT
1790 struct sock *sk = sock->sk;
1791 struct netlink_sock *nlk = nlk_sk(sk);
342dfc30 1792 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
15e47304 1793 u32 dst_portid;
d629b836 1794 u32 dst_group;
1da177e4
LT
1795 struct sk_buff *skb;
1796 int err;
1797 struct scm_cookie scm;
2d7a85f4 1798 u32 netlink_skb_flags = 0;
1da177e4
LT
1799
1800 if (msg->msg_flags&MSG_OOB)
1801 return -EOPNOTSUPP;
1802
7cc05662 1803 err = scm_send(sock, msg, &scm, true);
1da177e4
LT
1804 if (err < 0)
1805 return err;
1806
1807 if (msg->msg_namelen) {
b47030c7 1808 err = -EINVAL;
1da177e4 1809 if (addr->nl_family != AF_NETLINK)
b47030c7 1810 goto out;
15e47304 1811 dst_portid = addr->nl_pid;
d629b836 1812 dst_group = ffs(addr->nl_groups);
b47030c7 1813 err = -EPERM;
15e47304 1814 if ((dst_group || dst_portid) &&
5187cd05 1815 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
b47030c7 1816 goto out;
2d7a85f4 1817 netlink_skb_flags |= NETLINK_SKB_DST;
1da177e4 1818 } else {
15e47304 1819 dst_portid = nlk->dst_portid;
d629b836 1820 dst_group = nlk->dst_group;
1da177e4
LT
1821 }
1822
da314c99 1823 if (!nlk->bound) {
1da177e4
LT
1824 err = netlink_autobind(sock);
1825 if (err)
1826 goto out;
da314c99
HX
1827 } else {
1828 /* Ensure nlk is hashed and visible. */
1829 smp_rmb();
1da177e4
LT
1830 }
1831
1832 err = -EMSGSIZE;
1833 if (len > sk->sk_sndbuf - 32)
1834 goto out;
1835 err = -ENOBUFS;
3a36515f 1836 skb = netlink_alloc_large_skb(len, dst_group);
6ac552fd 1837 if (skb == NULL)
1da177e4
LT
1838 goto out;
1839
15e47304 1840 NETLINK_CB(skb).portid = nlk->portid;
d629b836 1841 NETLINK_CB(skb).dst_group = dst_group;
7cc05662 1842 NETLINK_CB(skb).creds = scm.creds;
2d7a85f4 1843 NETLINK_CB(skb).flags = netlink_skb_flags;
1da177e4 1844
1da177e4 1845 err = -EFAULT;
6ce8e9ce 1846 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1da177e4
LT
1847 kfree_skb(skb);
1848 goto out;
1849 }
1850
1851 err = security_netlink_send(sk, skb);
1852 if (err) {
1853 kfree_skb(skb);
1854 goto out;
1855 }
1856
d629b836 1857 if (dst_group) {
63354797 1858 refcount_inc(&skb->users);
15e47304 1859 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1da177e4 1860 }
15e47304 1861 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1da177e4
LT
1862
1863out:
7cc05662 1864 scm_destroy(&scm);
1da177e4
LT
1865 return err;
1866}
1867
1b784140 1868static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1da177e4
LT
1869 int flags)
1870{
1da177e4
LT
1871 struct scm_cookie scm;
1872 struct sock *sk = sock->sk;
1873 struct netlink_sock *nlk = nlk_sk(sk);
1874 int noblock = flags&MSG_DONTWAIT;
1875 size_t copied;
68d6ac6d 1876 struct sk_buff *skb, *data_skb;
b44d211e 1877 int err, ret;
1da177e4
LT
1878
1879 if (flags&MSG_OOB)
1880 return -EOPNOTSUPP;
1881
1882 copied = 0;
1883
6ac552fd
PM
1884 skb = skb_recv_datagram(sk, flags, noblock, &err);
1885 if (skb == NULL)
1da177e4
LT
1886 goto out;
1887
68d6ac6d
JB
1888 data_skb = skb;
1889
1dacc76d
JB
1890#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1891 if (unlikely(skb_shinfo(skb)->frag_list)) {
1dacc76d 1892 /*
68d6ac6d
JB
1893 * If this skb has a frag_list, then here that means that we
1894 * will have to use the frag_list skb's data for compat tasks
1895 * and the regular skb's data for normal (non-compat) tasks.
1dacc76d 1896 *
68d6ac6d
JB
1897 * If we need to send the compat skb, assign it to the
1898 * 'data_skb' variable so that it will be used below for data
1899 * copying. We keep 'skb' for everything else, including
1900 * freeing both later.
1dacc76d 1901 */
68d6ac6d
JB
1902 if (flags & MSG_CMSG_COMPAT)
1903 data_skb = skb_shinfo(skb)->frag_list;
1dacc76d
JB
1904 }
1905#endif
1906
9063e21f
ED
1907 /* Record the max length of recvmsg() calls for future allocations */
1908 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
1909 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
d35c99ff 1910 SKB_WITH_OVERHEAD(32768));
9063e21f 1911
68d6ac6d 1912 copied = data_skb->len;
1da177e4
LT
1913 if (len < copied) {
1914 msg->msg_flags |= MSG_TRUNC;
1915 copied = len;
1916 }
1917
68d6ac6d 1918 skb_reset_transport_header(data_skb);
51f3d02b 1919 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
1da177e4
LT
1920
1921 if (msg->msg_name) {
342dfc30 1922 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1da177e4
LT
1923 addr->nl_family = AF_NETLINK;
1924 addr->nl_pad = 0;
15e47304 1925 addr->nl_pid = NETLINK_CB(skb).portid;
d629b836 1926 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1da177e4
LT
1927 msg->msg_namelen = sizeof(*addr);
1928 }
1929
cc3a572f 1930 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
cc9a06cd 1931 netlink_cmsg_recv_pktinfo(msg, skb);
59324cf3
ND
1932 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
1933 netlink_cmsg_listen_all_nsid(sk, msg, skb);
cc9a06cd 1934
7cc05662
CH
1935 memset(&scm, 0, sizeof(scm));
1936 scm.creds = *NETLINK_CREDS(skb);
188ccb55 1937 if (flags & MSG_TRUNC)
68d6ac6d 1938 copied = data_skb->len;
daa3766e 1939
1da177e4
LT
1940 skb_free_datagram(sk, skb);
1941
16b304f3
PS
1942 if (nlk->cb_running &&
1943 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
b44d211e
AV
1944 ret = netlink_dump(sk);
1945 if (ret) {
ac30ef83 1946 sk->sk_err = -ret;
b44d211e
AV
1947 sk->sk_error_report(sk);
1948 }
1949 }
1da177e4 1950
7cc05662 1951 scm_recv(sock, msg, &scm, flags);
1da177e4
LT
1952out:
1953 netlink_rcv_wake(sk);
1954 return err ? : copied;
1955}
1956
676d2369 1957static void netlink_data_ready(struct sock *sk)
1da177e4 1958{
cd40b7d3 1959 BUG();
1da177e4
LT
1960}
1961
1962/*
746fac4d 1963 * We export these functions to other modules. They provide a
1da177e4
LT
1964 * complete set of kernel non-blocking support for message
1965 * queueing.
1966 */
1967
1968struct sock *
9f00d977
PNA
1969__netlink_kernel_create(struct net *net, int unit, struct module *module,
1970 struct netlink_kernel_cfg *cfg)
1da177e4
LT
1971{
1972 struct socket *sock;
1973 struct sock *sk;
77247bbb 1974 struct netlink_sock *nlk;
5c398dc8 1975 struct listeners *listeners = NULL;
a31f2d17
PNA
1976 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1977 unsigned int groups;
1da177e4 1978
fab2caf6 1979 BUG_ON(!nl_table);
1da177e4 1980
6ac552fd 1981 if (unit < 0 || unit >= MAX_LINKS)
1da177e4
LT
1982 return NULL;
1983
1984 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1985 return NULL;
13d3078e
EB
1986
1987 if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
23fe1866
PE
1988 goto out_sock_release_nosk;
1989
1990 sk = sock->sk;
4fdb3bb7 1991
a31f2d17 1992 if (!cfg || cfg->groups < 32)
4277a083 1993 groups = 32;
a31f2d17
PNA
1994 else
1995 groups = cfg->groups;
4277a083 1996
5c398dc8 1997 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
4277a083
PM
1998 if (!listeners)
1999 goto out_sock_release;
2000
1da177e4 2001 sk->sk_data_ready = netlink_data_ready;
a31f2d17
PNA
2002 if (cfg && cfg->input)
2003 nlk_sk(sk)->netlink_rcv = cfg->input;
1da177e4 2004
8ea65f4a 2005 if (netlink_insert(sk, 0))
77247bbb 2006 goto out_sock_release;
4fdb3bb7 2007
77247bbb 2008 nlk = nlk_sk(sk);
cc3a572f 2009 nlk->flags |= NETLINK_F_KERNEL_SOCKET;
4fdb3bb7 2010
4fdb3bb7 2011 netlink_table_grab();
b4b51029
EB
2012 if (!nl_table[unit].registered) {
2013 nl_table[unit].groups = groups;
5c398dc8 2014 rcu_assign_pointer(nl_table[unit].listeners, listeners);
b4b51029
EB
2015 nl_table[unit].cb_mutex = cb_mutex;
2016 nl_table[unit].module = module;
9785e10a
PNA
2017 if (cfg) {
2018 nl_table[unit].bind = cfg->bind;
6251edd9 2019 nl_table[unit].unbind = cfg->unbind;
9785e10a 2020 nl_table[unit].flags = cfg->flags;
da12c90e
G
2021 if (cfg->compare)
2022 nl_table[unit].compare = cfg->compare;
9785e10a 2023 }
b4b51029 2024 nl_table[unit].registered = 1;
f937f1f4
JJ
2025 } else {
2026 kfree(listeners);
869e58f8 2027 nl_table[unit].registered++;
b4b51029 2028 }
4fdb3bb7 2029 netlink_table_ungrab();
77247bbb
PM
2030 return sk;
2031
4fdb3bb7 2032out_sock_release:
4277a083 2033 kfree(listeners);
9dfbec1f 2034 netlink_kernel_release(sk);
23fe1866
PE
2035 return NULL;
2036
2037out_sock_release_nosk:
4fdb3bb7 2038 sock_release(sock);
77247bbb 2039 return NULL;
1da177e4 2040}
9f00d977 2041EXPORT_SYMBOL(__netlink_kernel_create);
b7c6ba6e
DL
2042
2043void
2044netlink_kernel_release(struct sock *sk)
2045{
13d3078e
EB
2046 if (sk == NULL || sk->sk_socket == NULL)
2047 return;
2048
2049 sock_release(sk->sk_socket);
b7c6ba6e
DL
2050}
2051EXPORT_SYMBOL(netlink_kernel_release);
2052
d136f1bd 2053int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
b4ff4f04 2054{
5c398dc8 2055 struct listeners *new, *old;
b4ff4f04 2056 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
b4ff4f04
JB
2057
2058 if (groups < 32)
2059 groups = 32;
2060
b4ff4f04 2061 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
5c398dc8
ED
2062 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2063 if (!new)
d136f1bd 2064 return -ENOMEM;
6d772ac5 2065 old = nl_deref_protected(tbl->listeners);
5c398dc8
ED
2066 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2067 rcu_assign_pointer(tbl->listeners, new);
2068
37b6b935 2069 kfree_rcu(old, rcu);
b4ff4f04
JB
2070 }
2071 tbl->groups = groups;
2072
d136f1bd
JB
2073 return 0;
2074}
2075
2076/**
2077 * netlink_change_ngroups - change number of multicast groups
2078 *
2079 * This changes the number of multicast groups that are available
2080 * on a certain netlink family. Note that it is not possible to
2081 * change the number of groups to below 32. Also note that it does
2082 * not implicitly call netlink_clear_multicast_users() when the
2083 * number of groups is reduced.
2084 *
2085 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2086 * @groups: The new number of groups.
2087 */
2088int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2089{
2090 int err;
2091
2092 netlink_table_grab();
2093 err = __netlink_change_ngroups(sk, groups);
b4ff4f04 2094 netlink_table_ungrab();
d136f1bd 2095
b4ff4f04
JB
2096 return err;
2097}
b4ff4f04 2098
b8273570
JB
2099void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2100{
2101 struct sock *sk;
b8273570
JB
2102 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2103
b67bfe0d 2104 sk_for_each_bound(sk, &tbl->mc_list)
b8273570
JB
2105 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2106}
2107
a46621a3 2108struct nlmsghdr *
15e47304 2109__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
a46621a3
DV
2110{
2111 struct nlmsghdr *nlh;
573ce260 2112 int size = nlmsg_msg_size(len);
a46621a3 2113
4df864c1 2114 nlh = skb_put(skb, NLMSG_ALIGN(size));
a46621a3
DV
2115 nlh->nlmsg_type = type;
2116 nlh->nlmsg_len = size;
2117 nlh->nlmsg_flags = flags;
15e47304 2118 nlh->nlmsg_pid = portid;
a46621a3
DV
2119 nlh->nlmsg_seq = seq;
2120 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
573ce260 2121 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
a46621a3
DV
2122 return nlh;
2123}
2124EXPORT_SYMBOL(__nlmsg_put);
2125
1da177e4
LT
2126/*
2127 * It looks a bit ugly.
2128 * It would be better to create kernel thread.
2129 */
2130
2131static int netlink_dump(struct sock *sk)
2132{
2133 struct netlink_sock *nlk = nlk_sk(sk);
2134 struct netlink_callback *cb;
c7ac8679 2135 struct sk_buff *skb = NULL;
1da177e4 2136 struct nlmsghdr *nlh;
92964c79 2137 struct module *module;
0642840b 2138 int err = -ENOBUFS;
db65a3aa 2139 int alloc_min_size;
c7ac8679 2140 int alloc_size;
1da177e4 2141
af65bdfc 2142 mutex_lock(nlk->cb_mutex);
16b304f3 2143 if (!nlk->cb_running) {
bf8b79e4
TG
2144 err = -EINVAL;
2145 goto errout_skb;
1da177e4
LT
2146 }
2147
d1b4c689 2148 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
f9c22888 2149 goto errout_skb;
9063e21f
ED
2150
2151 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2152 * required, but it makes sense to _attempt_ a 16K bytes allocation
2153 * to reduce number of system calls on dump operations, if user
2154 * ever provided a big enough buffer.
2155 */
db65a3aa
AR
2156 cb = &nlk->cb;
2157 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2158
2159 if (alloc_min_size < nlk->max_recvmsg_len) {
2160 alloc_size = nlk->max_recvmsg_len;
d35c99ff
ED
2161 skb = alloc_skb(alloc_size,
2162 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
2163 __GFP_NOWARN | __GFP_NORETRY);
9063e21f 2164 }
db65a3aa
AR
2165 if (!skb) {
2166 alloc_size = alloc_min_size;
c5b0db32 2167 skb = alloc_skb(alloc_size, GFP_KERNEL);
db65a3aa 2168 }
c7ac8679 2169 if (!skb)
c63d6ea3 2170 goto errout_skb;
db65a3aa
AR
2171
2172 /* Trim skb to allocated size. User is expected to provide buffer as
2173 * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
2174 * netlink_recvmsg())). dump will pack as many smaller messages as
2175 * could fit within the allocated skb. skb is typically allocated
2176 * with larger space than required (could be as much as near 2x the
2177 * requested size with align to next power of 2 approach). Allowing
2178 * dump to use the excess space makes it difficult for a user to have a
2179 * reasonable static buffer based on the expected largest dump of a
2180 * single netdev. The outcome is MSG_TRUNC error.
2181 */
d1b4c689 2182 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
f9c22888 2183 netlink_skb_set_owner_r(skb, sk);
c7ac8679 2184
0642840b
JD
2185 if (nlk->dump_done_errno > 0)
2186 nlk->dump_done_errno = cb->dump(skb, cb);
1da177e4 2187
0642840b
JD
2188 if (nlk->dump_done_errno > 0 ||
2189 skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
af65bdfc 2190 mutex_unlock(nlk->cb_mutex);
b1153f29
SH
2191
2192 if (sk_filter(sk, skb))
2193 kfree_skb(skb);
4a7e7c2a
ED
2194 else
2195 __netlink_sendskb(sk, skb);
1da177e4
LT
2196 return 0;
2197 }
2198
0642840b
JD
2199 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE,
2200 sizeof(nlk->dump_done_errno), NLM_F_MULTI);
2201 if (WARN_ON(!nlh))
bf8b79e4
TG
2202 goto errout_skb;
2203
670dc283
JB
2204 nl_dump_check_consistent(cb, nlh);
2205
0642840b
JD
2206 memcpy(nlmsg_data(nlh), &nlk->dump_done_errno,
2207 sizeof(nlk->dump_done_errno));
bf8b79e4 2208
b1153f29
SH
2209 if (sk_filter(sk, skb))
2210 kfree_skb(skb);
4a7e7c2a
ED
2211 else
2212 __netlink_sendskb(sk, skb);
1da177e4 2213
a8f74b22
TG
2214 if (cb->done)
2215 cb->done(cb);
1da177e4 2216
16b304f3 2217 nlk->cb_running = false;
92964c79
HX
2218 module = cb->module;
2219 skb = cb->skb;
16b304f3 2220 mutex_unlock(nlk->cb_mutex);
92964c79
HX
2221 module_put(module);
2222 consume_skb(skb);
1da177e4 2223 return 0;
1797754e 2224
bf8b79e4 2225errout_skb:
af65bdfc 2226 mutex_unlock(nlk->cb_mutex);
bf8b79e4 2227 kfree_skb(skb);
bf8b79e4 2228 return err;
1da177e4
LT
2229}
2230
6dc878a8
G
2231int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2232 const struct nlmsghdr *nlh,
2233 struct netlink_dump_control *control)
1da177e4
LT
2234{
2235 struct netlink_callback *cb;
2236 struct sock *sk;
2237 struct netlink_sock *nlk;
b44d211e 2238 int ret;
1da177e4 2239
63354797 2240 refcount_inc(&skb->users);
f9c22888 2241
15e47304 2242 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
1da177e4 2243 if (sk == NULL) {
16b304f3
PS
2244 ret = -ECONNREFUSED;
2245 goto error_free;
1da177e4 2246 }
6dc878a8 2247
16b304f3 2248 nlk = nlk_sk(sk);
af65bdfc 2249 mutex_lock(nlk->cb_mutex);
6dc878a8 2250 /* A dump is in progress... */
16b304f3 2251 if (nlk->cb_running) {
6dc878a8 2252 ret = -EBUSY;
16b304f3 2253 goto error_unlock;
1da177e4 2254 }
6dc878a8 2255 /* add reference of module which cb->dump belongs to */
16b304f3 2256 if (!try_module_get(control->module)) {
6dc878a8 2257 ret = -EPROTONOSUPPORT;
16b304f3 2258 goto error_unlock;
6dc878a8
G
2259 }
2260
16b304f3
PS
2261 cb = &nlk->cb;
2262 memset(cb, 0, sizeof(*cb));
fc9e50f5 2263 cb->start = control->start;
16b304f3
PS
2264 cb->dump = control->dump;
2265 cb->done = control->done;
2266 cb->nlh = nlh;
2267 cb->data = control->data;
2268 cb->module = control->module;
2269 cb->min_dump_alloc = control->min_dump_alloc;
2270 cb->skb = skb;
2271
41c87425
JD
2272 if (cb->start) {
2273 ret = cb->start(cb);
2274 if (ret)
2275 goto error_unlock;
2276 }
2277
16b304f3 2278 nlk->cb_running = true;
0642840b 2279 nlk->dump_done_errno = INT_MAX;
16b304f3 2280
af65bdfc 2281 mutex_unlock(nlk->cb_mutex);
1da177e4 2282
41c87425 2283 ret = netlink_dump(sk);
fc9e50f5 2284
1da177e4 2285 sock_put(sk);
5c58298c 2286
b44d211e
AV
2287 if (ret)
2288 return ret;
2289
5c58298c
DL
2290 /* We successfully started a dump, by returning -EINTR we
2291 * signal not to send ACK even if it was requested.
2292 */
2293 return -EINTR;
16b304f3
PS
2294
2295error_unlock:
2296 sock_put(sk);
2297 mutex_unlock(nlk->cb_mutex);
2298error_free:
2299 kfree_skb(skb);
2300 return ret;
1da177e4 2301}
6dc878a8 2302EXPORT_SYMBOL(__netlink_dump_start);
1da177e4 2303
2d4bc933
JB
2304void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
2305 const struct netlink_ext_ack *extack)
1da177e4
LT
2306{
2307 struct sk_buff *skb;
2308 struct nlmsghdr *rep;
2309 struct nlmsgerr *errmsg;
339bf98f 2310 size_t payload = sizeof(*errmsg);
2d4bc933 2311 size_t tlvlen = 0;
0a6a3a23 2312 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
2d4bc933 2313 unsigned int flags = 0;
48044eb4 2314 bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK;
1da177e4 2315
0a6a3a23 2316 /* Error messages get the original request appened, unless the user
2d4bc933
JB
2317 * requests to cap the error message, and get extra error data if
2318 * requested.
0a6a3a23 2319 */
4f6265d4
DA
2320 if (nlk_has_extack && extack && extack->_msg)
2321 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
2322
2d4bc933
JB
2323 if (err) {
2324 if (!(nlk->flags & NETLINK_F_CAP_ACK))
2325 payload += nlmsg_len(nlh);
2326 else
2327 flags |= NLM_F_CAPPED;
4f6265d4
DA
2328 if (nlk_has_extack && extack && extack->bad_attr)
2329 tlvlen += nla_total_size(sizeof(u32));
2d4bc933
JB
2330 } else {
2331 flags |= NLM_F_CAPPED;
ba0dc5f6 2332
48044eb4 2333 if (nlk_has_extack && extack && extack->cookie_len)
ba0dc5f6 2334 tlvlen += nla_total_size(extack->cookie_len);
2d4bc933 2335 }
1da177e4 2336
2d4bc933
JB
2337 if (tlvlen)
2338 flags |= NLM_F_ACK_TLVS;
2339
2340 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
1da177e4 2341 if (!skb) {
a2084f56
JB
2342 NETLINK_CB(in_skb).sk->sk_err = ENOBUFS;
2343 NETLINK_CB(in_skb).sk->sk_error_report(NETLINK_CB(in_skb).sk);
1da177e4
LT
2344 return;
2345 }
2346
15e47304 2347 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
2d4bc933 2348 NLMSG_ERROR, payload, flags);
bf8b79e4 2349 errmsg = nlmsg_data(rep);
1da177e4 2350 errmsg->error = err;
0a6a3a23 2351 memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
2d4bc933 2352
48044eb4 2353 if (nlk_has_extack && extack) {
4f6265d4
DA
2354 if (extack->_msg) {
2355 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
2356 extack->_msg));
2357 }
ba0dc5f6 2358 if (err) {
ba0dc5f6
JB
2359 if (extack->bad_attr &&
2360 !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2361 (u8 *)extack->bad_attr >= in_skb->data +
2362 in_skb->len))
2363 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2364 (u8 *)extack->bad_attr -
2365 in_skb->data));
2366 } else {
2367 if (extack->cookie_len)
2368 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2369 extack->cookie_len,
2370 extack->cookie));
2371 }
2d4bc933
JB
2372 }
2373
2374 nlmsg_end(skb, rep);
2375
15e47304 2376 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1da177e4 2377}
6ac552fd 2378EXPORT_SYMBOL(netlink_ack);
1da177e4 2379
cd40b7d3 2380int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
2d4bc933
JB
2381 struct nlmsghdr *,
2382 struct netlink_ext_ack *))
82ace47a 2383{
2d4bc933 2384 struct netlink_ext_ack extack = {};
82ace47a
TG
2385 struct nlmsghdr *nlh;
2386 int err;
2387
2388 while (skb->len >= nlmsg_total_size(0)) {
cd40b7d3
DL
2389 int msglen;
2390
b529ccf2 2391 nlh = nlmsg_hdr(skb);
d35b6856 2392 err = 0;
82ace47a 2393
ad8e4b75 2394 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
82ace47a
TG
2395 return 0;
2396
d35b6856
TG
2397 /* Only requests are handled by the kernel */
2398 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
5c58298c 2399 goto ack;
45e7ae7f
TG
2400
2401 /* Skip control messages */
2402 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
5c58298c 2403 goto ack;
d35b6856 2404
2d4bc933 2405 err = cb(skb, nlh, &extack);
5c58298c
DL
2406 if (err == -EINTR)
2407 goto skip;
2408
2409ack:
d35b6856 2410 if (nlh->nlmsg_flags & NLM_F_ACK || err)
2d4bc933 2411 netlink_ack(skb, nlh, err, &extack);
82ace47a 2412
5c58298c 2413skip:
6ac552fd 2414 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
cd40b7d3
DL
2415 if (msglen > skb->len)
2416 msglen = skb->len;
2417 skb_pull(skb, msglen);
82ace47a
TG
2418 }
2419
2420 return 0;
2421}
6ac552fd 2422EXPORT_SYMBOL(netlink_rcv_skb);
82ace47a 2423
d387f6ad
TG
2424/**
2425 * nlmsg_notify - send a notification netlink message
2426 * @sk: netlink socket to use
2427 * @skb: notification message
15e47304 2428 * @portid: destination netlink portid for reports or 0
d387f6ad
TG
2429 * @group: destination multicast group or 0
2430 * @report: 1 to report back, 0 to disable
2431 * @flags: allocation flags
2432 */
15e47304 2433int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
d387f6ad
TG
2434 unsigned int group, int report, gfp_t flags)
2435{
2436 int err = 0;
2437
2438 if (group) {
15e47304 2439 int exclude_portid = 0;
d387f6ad
TG
2440
2441 if (report) {
63354797 2442 refcount_inc(&skb->users);
15e47304 2443 exclude_portid = portid;
d387f6ad
TG
2444 }
2445
1ce85fe4
PNA
2446 /* errors reported via destination sk->sk_err, but propagate
2447 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
15e47304 2448 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
d387f6ad
TG
2449 }
2450
1ce85fe4
PNA
2451 if (report) {
2452 int err2;
2453
15e47304 2454 err2 = nlmsg_unicast(sk, skb, portid);
1ce85fe4
PNA
2455 if (!err || err == -ESRCH)
2456 err = err2;
2457 }
d387f6ad
TG
2458
2459 return err;
2460}
6ac552fd 2461EXPORT_SYMBOL(nlmsg_notify);
d387f6ad 2462
1da177e4
LT
2463#ifdef CONFIG_PROC_FS
2464struct nl_seq_iter {
e372c414 2465 struct seq_net_private p;
56d28b1e 2466 struct rhashtable_iter hti;
1da177e4 2467 int link;
1da177e4
LT
2468};
2469
56d28b1e 2470static int netlink_walk_start(struct nl_seq_iter *iter)
1da177e4 2471{
56d28b1e 2472 int err;
1da177e4 2473
8f6fd83c
BC
2474 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2475 GFP_KERNEL);
56d28b1e
HX
2476 if (err) {
2477 iter->link = MAX_LINKS;
2478 return err;
1da177e4 2479 }
56d28b1e 2480
97a6ec4a
TH
2481 rhashtable_walk_start(&iter->hti);
2482
2483 return 0;
1da177e4
LT
2484}
2485
56d28b1e 2486static void netlink_walk_stop(struct nl_seq_iter *iter)
1da177e4 2487{
56d28b1e
HX
2488 rhashtable_walk_stop(&iter->hti);
2489 rhashtable_walk_exit(&iter->hti);
1da177e4
LT
2490}
2491
56d28b1e 2492static void *__netlink_seq_next(struct seq_file *seq)
1da177e4 2493{
56d28b1e 2494 struct nl_seq_iter *iter = seq->private;
e341694e 2495 struct netlink_sock *nlk;
1da177e4 2496
56d28b1e
HX
2497 do {
2498 for (;;) {
2499 int err;
1da177e4 2500
56d28b1e 2501 nlk = rhashtable_walk_next(&iter->hti);
746fac4d 2502
56d28b1e
HX
2503 if (IS_ERR(nlk)) {
2504 if (PTR_ERR(nlk) == -EAGAIN)
2505 continue;
e341694e 2506
56d28b1e
HX
2507 return nlk;
2508 }
1da177e4 2509
56d28b1e
HX
2510 if (nlk)
2511 break;
1da177e4 2512
56d28b1e
HX
2513 netlink_walk_stop(iter);
2514 if (++iter->link >= MAX_LINKS)
2515 return NULL;
da12c90e 2516
56d28b1e
HX
2517 err = netlink_walk_start(iter);
2518 if (err)
2519 return ERR_PTR(err);
1da177e4 2520 }
56d28b1e 2521 } while (sock_net(&nlk->sk) != seq_file_net(seq));
1da177e4 2522
56d28b1e
HX
2523 return nlk;
2524}
1da177e4 2525
56d28b1e
HX
2526static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2527{
2528 struct nl_seq_iter *iter = seq->private;
2529 void *obj = SEQ_START_TOKEN;
2530 loff_t pos;
2531 int err;
2532
2533 iter->link = 0;
2534
2535 err = netlink_walk_start(iter);
2536 if (err)
2537 return ERR_PTR(err);
2538
2539 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2540 obj = __netlink_seq_next(seq);
2541
2542 return obj;
2543}
2544
2545static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2546{
2547 ++*pos;
2548 return __netlink_seq_next(seq);
1da177e4
LT
2549}
2550
2551static void netlink_seq_stop(struct seq_file *seq, void *v)
2552{
56d28b1e
HX
2553 struct nl_seq_iter *iter = seq->private;
2554
2555 if (iter->link >= MAX_LINKS)
2556 return;
2557
2558 netlink_walk_stop(iter);
1da177e4
LT
2559}
2560
2561
2562static int netlink_seq_show(struct seq_file *seq, void *v)
2563{
658cb354 2564 if (v == SEQ_START_TOKEN) {
1da177e4
LT
2565 seq_puts(seq,
2566 "sk Eth Pid Groups "
cf0aa4e0 2567 "Rmem Wmem Dump Locks Drops Inode\n");
658cb354 2568 } else {
1da177e4
LT
2569 struct sock *s = v;
2570 struct netlink_sock *nlk = nlk_sk(s);
2571
16b304f3 2572 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
1da177e4
LT
2573 s,
2574 s->sk_protocol,
15e47304 2575 nlk->portid,
513c2500 2576 nlk->groups ? (u32)nlk->groups[0] : 0,
31e6d363
ED
2577 sk_rmem_alloc_get(s),
2578 sk_wmem_alloc_get(s),
16b304f3 2579 nlk->cb_running,
41c6d650 2580 refcount_read(&s->sk_refcnt),
cf0aa4e0
MY
2581 atomic_read(&s->sk_drops),
2582 sock_i_ino(s)
1da177e4
LT
2583 );
2584
2585 }
2586 return 0;
2587}
2588
56b3d975 2589static const struct seq_operations netlink_seq_ops = {
1da177e4
LT
2590 .start = netlink_seq_start,
2591 .next = netlink_seq_next,
2592 .stop = netlink_seq_stop,
2593 .show = netlink_seq_show,
2594};
2595
2596
2597static int netlink_seq_open(struct inode *inode, struct file *file)
2598{
e372c414
DL
2599 return seq_open_net(inode, file, &netlink_seq_ops,
2600 sizeof(struct nl_seq_iter));
b4b51029
EB
2601}
2602
da7071d7 2603static const struct file_operations netlink_seq_fops = {
1da177e4
LT
2604 .owner = THIS_MODULE,
2605 .open = netlink_seq_open,
2606 .read = seq_read,
2607 .llseek = seq_lseek,
e372c414 2608 .release = seq_release_net,
1da177e4
LT
2609};
2610
2611#endif
2612
2613int netlink_register_notifier(struct notifier_block *nb)
2614{
efa172f4 2615 return blocking_notifier_chain_register(&netlink_chain, nb);
1da177e4 2616}
6ac552fd 2617EXPORT_SYMBOL(netlink_register_notifier);
1da177e4
LT
2618
2619int netlink_unregister_notifier(struct notifier_block *nb)
2620{
efa172f4 2621 return blocking_notifier_chain_unregister(&netlink_chain, nb);
1da177e4 2622}
6ac552fd 2623EXPORT_SYMBOL(netlink_unregister_notifier);
746fac4d 2624
90ddc4f0 2625static const struct proto_ops netlink_ops = {
1da177e4
LT
2626 .family = PF_NETLINK,
2627 .owner = THIS_MODULE,
2628 .release = netlink_release,
2629 .bind = netlink_bind,
2630 .connect = netlink_connect,
2631 .socketpair = sock_no_socketpair,
2632 .accept = sock_no_accept,
2633 .getname = netlink_getname,
d1b4c689 2634 .poll = datagram_poll,
025c6818 2635 .ioctl = netlink_ioctl,
1da177e4
LT
2636 .listen = sock_no_listen,
2637 .shutdown = sock_no_shutdown,
9a4595bc
PM
2638 .setsockopt = netlink_setsockopt,
2639 .getsockopt = netlink_getsockopt,
1da177e4
LT
2640 .sendmsg = netlink_sendmsg,
2641 .recvmsg = netlink_recvmsg,
d1b4c689 2642 .mmap = sock_no_mmap,
1da177e4
LT
2643 .sendpage = sock_no_sendpage,
2644};
2645
ec1b4cf7 2646static const struct net_proto_family netlink_family_ops = {
1da177e4
LT
2647 .family = PF_NETLINK,
2648 .create = netlink_create,
2649 .owner = THIS_MODULE, /* for consistency 8) */
2650};
2651
4665079c 2652static int __net_init netlink_net_init(struct net *net)
b4b51029
EB
2653{
2654#ifdef CONFIG_PROC_FS
d4beaa66 2655 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
b4b51029
EB
2656 return -ENOMEM;
2657#endif
2658 return 0;
2659}
2660
4665079c 2661static void __net_exit netlink_net_exit(struct net *net)
b4b51029
EB
2662{
2663#ifdef CONFIG_PROC_FS
ece31ffd 2664 remove_proc_entry("netlink", net->proc_net);
b4b51029
EB
2665#endif
2666}
2667
b963ea89
DM
2668static void __init netlink_add_usersock_entry(void)
2669{
5c398dc8 2670 struct listeners *listeners;
b963ea89
DM
2671 int groups = 32;
2672
5c398dc8 2673 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
b963ea89 2674 if (!listeners)
5c398dc8 2675 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
b963ea89
DM
2676
2677 netlink_table_grab();
2678
2679 nl_table[NETLINK_USERSOCK].groups = groups;
5c398dc8 2680 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
b963ea89
DM
2681 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2682 nl_table[NETLINK_USERSOCK].registered = 1;
9785e10a 2683 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
b963ea89
DM
2684
2685 netlink_table_ungrab();
2686}
2687
022cbae6 2688static struct pernet_operations __net_initdata netlink_net_ops = {
b4b51029
EB
2689 .init = netlink_net_init,
2690 .exit = netlink_net_exit,
2691};
2692
49f7b33e 2693static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
c428ecd1
HX
2694{
2695 const struct netlink_sock *nlk = data;
2696 struct netlink_compare_arg arg;
2697
da314c99 2698 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
11b58ba1 2699 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
c428ecd1
HX
2700}
2701
2702static const struct rhashtable_params netlink_rhashtable_params = {
2703 .head_offset = offsetof(struct netlink_sock, node),
2704 .key_len = netlink_compare_arg_len,
c428ecd1
HX
2705 .obj_hashfn = netlink_hash,
2706 .obj_cmpfn = netlink_compare,
b5e2c150 2707 .automatic_shrinking = true,
c428ecd1
HX
2708};
2709
1da177e4
LT
2710static int __init netlink_proto_init(void)
2711{
1da177e4 2712 int i;
1da177e4
LT
2713 int err = proto_register(&netlink_proto, 0);
2714
2715 if (err != 0)
2716 goto out;
2717
fab25745 2718 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
1da177e4 2719
0da974f4 2720 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
fab2caf6
AM
2721 if (!nl_table)
2722 goto panic;
1da177e4 2723
1da177e4 2724 for (i = 0; i < MAX_LINKS; i++) {
c428ecd1
HX
2725 if (rhashtable_init(&nl_table[i].hash,
2726 &netlink_rhashtable_params) < 0) {
e341694e
TG
2727 while (--i > 0)
2728 rhashtable_destroy(&nl_table[i].hash);
1da177e4 2729 kfree(nl_table);
fab2caf6 2730 goto panic;
1da177e4 2731 }
1da177e4
LT
2732 }
2733
bcbde0d4
DB
2734 INIT_LIST_HEAD(&netlink_tap_all);
2735
b963ea89
DM
2736 netlink_add_usersock_entry();
2737
1da177e4 2738 sock_register(&netlink_family_ops);
b4b51029 2739 register_pernet_subsys(&netlink_net_ops);
746fac4d 2740 /* The netlink device handler may be needed early. */
1da177e4
LT
2741 rtnetlink_init();
2742out:
2743 return err;
fab2caf6
AM
2744panic:
2745 panic("netlink_init: Cannot allocate nl_table\n");
1da177e4
LT
2746}
2747
1da177e4 2748core_initcall(netlink_proto_init);