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