]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netlink/af_netlink.c
qlcnic: Fix a sleep-in-atomic bug in qlcnic_82xx_hw_write_wx_2M and qlcnic_82xx_hw_re...
[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>
54e0f520 65
457c4cbc 66#include <net/net_namespace.h>
1da177e4
LT
67#include <net/sock.h>
68#include <net/scm.h>
82ace47a 69#include <net/netlink.h>
1da177e4 70
0f29c768 71#include "af_netlink.h"
1da177e4 72
5c398dc8
ED
73struct listeners {
74 struct rcu_head rcu;
75 unsigned long masks[0];
6c04bb18
JB
76};
77
cd967e05 78/* state bits */
cc3a572f 79#define NETLINK_S_CONGESTED 0x0
cd967e05 80
035c4c16 81static inline int netlink_is_kernel(struct sock *sk)
aed81560 82{
cc3a572f 83 return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
aed81560
DL
84}
85
91dd93f9 86struct netlink_table *nl_table __read_mostly;
0f29c768 87EXPORT_SYMBOL_GPL(nl_table);
1da177e4
LT
88
89static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
90
8a0f5ccf
HX
91static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
92
93static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
94 "nlk_cb_mutex-ROUTE",
95 "nlk_cb_mutex-1",
96 "nlk_cb_mutex-USERSOCK",
97 "nlk_cb_mutex-FIREWALL",
98 "nlk_cb_mutex-SOCK_DIAG",
99 "nlk_cb_mutex-NFLOG",
100 "nlk_cb_mutex-XFRM",
101 "nlk_cb_mutex-SELINUX",
102 "nlk_cb_mutex-ISCSI",
103 "nlk_cb_mutex-AUDIT",
104 "nlk_cb_mutex-FIB_LOOKUP",
105 "nlk_cb_mutex-CONNECTOR",
106 "nlk_cb_mutex-NETFILTER",
107 "nlk_cb_mutex-IP6_FW",
108 "nlk_cb_mutex-DNRTMSG",
109 "nlk_cb_mutex-KOBJECT_UEVENT",
110 "nlk_cb_mutex-GENERIC",
111 "nlk_cb_mutex-17",
112 "nlk_cb_mutex-SCSITRANSPORT",
113 "nlk_cb_mutex-ECRYPTFS",
114 "nlk_cb_mutex-RDMA",
115 "nlk_cb_mutex-CRYPTO",
116 "nlk_cb_mutex-SMC",
117 "nlk_cb_mutex-23",
118 "nlk_cb_mutex-24",
119 "nlk_cb_mutex-25",
120 "nlk_cb_mutex-26",
121 "nlk_cb_mutex-27",
122 "nlk_cb_mutex-28",
123 "nlk_cb_mutex-29",
124 "nlk_cb_mutex-30",
125 "nlk_cb_mutex-31",
126 "nlk_cb_mutex-MAX_LINKS"
127};
128
1da177e4 129static int netlink_dump(struct sock *sk);
9652e931 130static void netlink_skb_destructor(struct sk_buff *skb);
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
172 memcpy(skb_put(new, len), skb->data, len);
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));
374 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
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)) {
e341694e
TG
577 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
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
693 if (!atomic_dec_and_test(&sk->sk_refcnt))
694 return;
695
696 if (nlk->cb_running && nlk->cb.done) {
697 INIT_WORK(&nlk->work, netlink_sock_destruct_work);
698 schedule_work(&nlk->work);
699 return;
700 }
21e4902a 701
ed5d7788 702 sk_free(sk);
21e4902a
TG
703}
704
1da177e4
LT
705static int netlink_release(struct socket *sock)
706{
707 struct sock *sk = sock->sk;
708 struct netlink_sock *nlk;
709
710 if (!sk)
711 return 0;
712
713 netlink_remove(sk);
ac57b3a9 714 sock_orphan(sk);
1da177e4
LT
715 nlk = nlk_sk(sk);
716
3f660d66
HX
717 /*
718 * OK. Socket is unlinked, any packets that arrive now
719 * will be purged.
720 */
1da177e4 721
ee1c2442
JB
722 /* must not acquire netlink_table_lock in any way again before unbind
723 * and notifying genetlink is done as otherwise it might deadlock
724 */
725 if (nlk->netlink_unbind) {
726 int i;
727
728 for (i = 0; i < nlk->ngroups; i++)
729 if (test_bit(i, nlk->groups))
730 nlk->netlink_unbind(sock_net(sk), i + 1);
731 }
732 if (sk->sk_protocol == NETLINK_GENERIC &&
733 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
734 wake_up(&genl_sk_destructing_waitq);
735
1da177e4
LT
736 sock->sk = NULL;
737 wake_up_interruptible_all(&nlk->wait);
738
739 skb_queue_purge(&sk->sk_write_queue);
740
e2726020 741 if (nlk->portid && nlk->bound) {
1da177e4 742 struct netlink_notify n = {
3b1e0a65 743 .net = sock_net(sk),
1da177e4 744 .protocol = sk->sk_protocol,
15e47304 745 .portid = nlk->portid,
1da177e4 746 };
efa172f4 747 blocking_notifier_call_chain(&netlink_chain,
e041c683 748 NETLINK_URELEASE, &n);
746fac4d 749 }
4fdb3bb7 750
5e7c001c 751 module_put(nlk->module);
4fdb3bb7 752
aed81560 753 if (netlink_is_kernel(sk)) {
b10dcb3b 754 netlink_table_grab();
869e58f8
DL
755 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
756 if (--nl_table[sk->sk_protocol].registered == 0) {
6d772ac5
ED
757 struct listeners *old;
758
759 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
760 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
761 kfree_rcu(old, rcu);
869e58f8 762 nl_table[sk->sk_protocol].module = NULL;
9785e10a 763 nl_table[sk->sk_protocol].bind = NULL;
4f520900 764 nl_table[sk->sk_protocol].unbind = NULL;
9785e10a 765 nl_table[sk->sk_protocol].flags = 0;
869e58f8
DL
766 nl_table[sk->sk_protocol].registered = 0;
767 }
b10dcb3b 768 netlink_table_ungrab();
658cb354 769 }
77247bbb 770
f7fa9b10
PM
771 kfree(nlk->groups);
772 nlk->groups = NULL;
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;
957 int err;
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
RGB
984
985 if (nlk->netlink_bind && groups) {
986 int group;
987
988 for (group = 0; group < nlk->ngroups; group++) {
989 if (!test_bit(group, &groups))
990 continue;
8b7c36d8 991 err = nlk->netlink_bind(net, group + 1);
4f520900
RGB
992 if (!err)
993 continue;
023e2cfa 994 netlink_undo_bind(group, groups, sk);
4f520900
RGB
995 return err;
996 }
997 }
998
da314c99
HX
999 /* No need for barriers here as we return to user-space without
1000 * using any of the bound attributes.
1001 */
1002 if (!bound) {
1da177e4 1003 err = nladdr->nl_pid ?
8ea65f4a 1004 netlink_insert(sk, nladdr->nl_pid) :
1da177e4 1005 netlink_autobind(sock);
4f520900 1006 if (err) {
023e2cfa 1007 netlink_undo_bind(nlk->ngroups, groups, sk);
1da177e4 1008 return err;
4f520900 1009 }
1da177e4
LT
1010 }
1011
4f520900 1012 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1da177e4
LT
1013 return 0;
1014
1015 netlink_table_grab();
f7fa9b10 1016 netlink_update_subscriptions(sk, nlk->subscriptions +
4f520900 1017 hweight32(groups) -
746fac4d 1018 hweight32(nlk->groups[0]));
4f520900 1019 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
4277a083 1020 netlink_update_listeners(sk);
1da177e4
LT
1021 netlink_table_ungrab();
1022
1023 return 0;
1024}
1025
1026static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1027 int alen, int flags)
1028{
1029 int err = 0;
1030 struct sock *sk = sock->sk;
1031 struct netlink_sock *nlk = nlk_sk(sk);
6ac552fd 1032 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1da177e4 1033
6503d961
CG
1034 if (alen < sizeof(addr->sa_family))
1035 return -EINVAL;
1036
1da177e4
LT
1037 if (addr->sa_family == AF_UNSPEC) {
1038 sk->sk_state = NETLINK_UNCONNECTED;
15e47304 1039 nlk->dst_portid = 0;
d629b836 1040 nlk->dst_group = 0;
1da177e4
LT
1041 return 0;
1042 }
1043 if (addr->sa_family != AF_NETLINK)
1044 return -EINVAL;
1045
46833a86 1046 if ((nladdr->nl_groups || nladdr->nl_pid) &&
5187cd05 1047 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1da177e4
LT
1048 return -EPERM;
1049
da314c99
HX
1050 /* No need for barriers here as we return to user-space without
1051 * using any of the bound attributes.
1052 */
1053 if (!nlk->bound)
1da177e4
LT
1054 err = netlink_autobind(sock);
1055
1056 if (err == 0) {
1057 sk->sk_state = NETLINK_CONNECTED;
15e47304 1058 nlk->dst_portid = nladdr->nl_pid;
d629b836 1059 nlk->dst_group = ffs(nladdr->nl_groups);
1da177e4
LT
1060 }
1061
1062 return err;
1063}
1064
6ac552fd
PM
1065static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1066 int *addr_len, int peer)
1da177e4
LT
1067{
1068 struct sock *sk = sock->sk;
1069 struct netlink_sock *nlk = nlk_sk(sk);
13cfa97b 1070 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
746fac4d 1071
1da177e4
LT
1072 nladdr->nl_family = AF_NETLINK;
1073 nladdr->nl_pad = 0;
1074 *addr_len = sizeof(*nladdr);
1075
1076 if (peer) {
15e47304 1077 nladdr->nl_pid = nlk->dst_portid;
d629b836 1078 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1da177e4 1079 } else {
15e47304 1080 nladdr->nl_pid = nlk->portid;
513c2500 1081 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
1da177e4
LT
1082 }
1083 return 0;
1084}
1085
025c6818
DD
1086static int netlink_ioctl(struct socket *sock, unsigned int cmd,
1087 unsigned long arg)
1088{
1089 /* try to hand this ioctl down to the NIC drivers.
1090 */
1091 return -ENOIOCTLCMD;
1092}
1093
15e47304 1094static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1da177e4 1095{
1da177e4
LT
1096 struct sock *sock;
1097 struct netlink_sock *nlk;
1098
15e47304 1099 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1da177e4
LT
1100 if (!sock)
1101 return ERR_PTR(-ECONNREFUSED);
1102
1103 /* Don't bother queuing skb if kernel socket has no input function */
1104 nlk = nlk_sk(sock);
cd40b7d3 1105 if (sock->sk_state == NETLINK_CONNECTED &&
15e47304 1106 nlk->dst_portid != nlk_sk(ssk)->portid) {
1da177e4
LT
1107 sock_put(sock);
1108 return ERR_PTR(-ECONNREFUSED);
1109 }
1110 return sock;
1111}
1112
1113struct sock *netlink_getsockbyfilp(struct file *filp)
1114{
496ad9aa 1115 struct inode *inode = file_inode(filp);
1da177e4
LT
1116 struct sock *sock;
1117
1118 if (!S_ISSOCK(inode->i_mode))
1119 return ERR_PTR(-ENOTSOCK);
1120
1121 sock = SOCKET_I(inode)->sk;
1122 if (sock->sk_family != AF_NETLINK)
1123 return ERR_PTR(-EINVAL);
1124
1125 sock_hold(sock);
1126 return sock;
1127}
1128
3a36515f
PN
1129static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1130 int broadcast)
c05cdb1b
PNA
1131{
1132 struct sk_buff *skb;
1133 void *data;
1134
3a36515f 1135 if (size <= NLMSG_GOODSIZE || broadcast)
c05cdb1b
PNA
1136 return alloc_skb(size, GFP_KERNEL);
1137
3a36515f
PN
1138 size = SKB_DATA_ALIGN(size) +
1139 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c05cdb1b
PNA
1140
1141 data = vmalloc(size);
1142 if (data == NULL)
3a36515f 1143 return NULL;
c05cdb1b 1144
2ea2f62c 1145 skb = __build_skb(data, size);
3a36515f
PN
1146 if (skb == NULL)
1147 vfree(data);
2ea2f62c 1148 else
3a36515f 1149 skb->destructor = netlink_skb_destructor;
c05cdb1b
PNA
1150
1151 return skb;
c05cdb1b
PNA
1152}
1153
1da177e4
LT
1154/*
1155 * Attach a skb to a netlink socket.
1156 * The caller must hold a reference to the destination socket. On error, the
1157 * reference is dropped. The skb is not send to the destination, just all
1158 * all error checks are performed and memory in the queue is reserved.
1159 * Return values:
1160 * < 0: error. skb freed, reference to sock dropped.
1161 * 0: continue
1162 * 1: repeat lookup - reference dropped while waiting for socket memory.
1163 */
9457afee 1164int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 1165 long *timeo, struct sock *ssk)
1da177e4
LT
1166{
1167 struct netlink_sock *nlk;
1168
1169 nlk = nlk_sk(sk);
1170
5fd96123 1171 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
d1b4c689 1172 test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
1da177e4 1173 DECLARE_WAITQUEUE(wait, current);
c3d8d1e3 1174 if (!*timeo) {
aed81560 1175 if (!ssk || netlink_is_kernel(ssk))
1da177e4
LT
1176 netlink_overrun(sk);
1177 sock_put(sk);
1178 kfree_skb(skb);
1179 return -EAGAIN;
1180 }
1181
1182 __set_current_state(TASK_INTERRUPTIBLE);
1183 add_wait_queue(&nlk->wait, &wait);
1184
1185 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
cc3a572f 1186 test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
1da177e4 1187 !sock_flag(sk, SOCK_DEAD))
c3d8d1e3 1188 *timeo = schedule_timeout(*timeo);
1da177e4
LT
1189
1190 __set_current_state(TASK_RUNNING);
1191 remove_wait_queue(&nlk->wait, &wait);
1192 sock_put(sk);
1193
1194 if (signal_pending(current)) {
1195 kfree_skb(skb);
c3d8d1e3 1196 return sock_intr_errno(*timeo);
1da177e4
LT
1197 }
1198 return 1;
1199 }
cf0a018a 1200 netlink_skb_set_owner_r(skb, sk);
1da177e4
LT
1201 return 0;
1202}
1203
4a7e7c2a 1204static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1da177e4 1205{
1da177e4
LT
1206 int len = skb->len;
1207
bcbde0d4
DB
1208 netlink_deliver_tap(skb);
1209
d1b4c689 1210 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 1211 sk->sk_data_ready(sk);
4a7e7c2a
ED
1212 return len;
1213}
1214
1215int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1216{
1217 int len = __netlink_sendskb(sk, skb);
1218
1da177e4
LT
1219 sock_put(sk);
1220 return len;
1221}
1222
1223void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1224{
1225 kfree_skb(skb);
1226 sock_put(sk);
1227}
1228
b57ef81f 1229static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1da177e4
LT
1230{
1231 int delta;
1232
1298ca46 1233 WARN_ON(skb->sk != NULL);
4305b541 1234 delta = skb->end - skb->tail;
c05cdb1b 1235 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
1da177e4
LT
1236 return skb;
1237
1238 if (skb_shared(skb)) {
1239 struct sk_buff *nskb = skb_clone(skb, allocation);
1240 if (!nskb)
1241 return skb;
8460c00f 1242 consume_skb(skb);
1da177e4
LT
1243 skb = nskb;
1244 }
1245
158f323b
ED
1246 pskb_expand_head(skb, 0, -delta,
1247 (allocation & ~__GFP_DIRECT_RECLAIM) |
1248 __GFP_NOWARN | __GFP_NORETRY);
1da177e4
LT
1249 return skb;
1250}
1251
3fbc2905
EB
1252static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1253 struct sock *ssk)
cd40b7d3
DL
1254{
1255 int ret;
1256 struct netlink_sock *nlk = nlk_sk(sk);
1257
1258 ret = -ECONNREFUSED;
1259 if (nlk->netlink_rcv != NULL) {
1260 ret = skb->len;
cf0a018a 1261 netlink_skb_set_owner_r(skb, sk);
e32123e5 1262 NETLINK_CB(skb).sk = ssk;
73bfd370 1263 netlink_deliver_tap_kernel(sk, ssk, skb);
cd40b7d3 1264 nlk->netlink_rcv(skb);
bfb253c9
ED
1265 consume_skb(skb);
1266 } else {
1267 kfree_skb(skb);
cd40b7d3 1268 }
cd40b7d3
DL
1269 sock_put(sk);
1270 return ret;
1271}
1272
1273int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
15e47304 1274 u32 portid, int nonblock)
1da177e4
LT
1275{
1276 struct sock *sk;
1277 int err;
1278 long timeo;
1279
1280 skb = netlink_trim(skb, gfp_any());
1281
1282 timeo = sock_sndtimeo(ssk, nonblock);
1283retry:
15e47304 1284 sk = netlink_getsockbyportid(ssk, portid);
1da177e4
LT
1285 if (IS_ERR(sk)) {
1286 kfree_skb(skb);
1287 return PTR_ERR(sk);
1288 }
cd40b7d3 1289 if (netlink_is_kernel(sk))
3fbc2905 1290 return netlink_unicast_kernel(sk, skb, ssk);
cd40b7d3 1291
b1153f29 1292 if (sk_filter(sk, skb)) {
84874607 1293 err = skb->len;
b1153f29
SH
1294 kfree_skb(skb);
1295 sock_put(sk);
1296 return err;
1297 }
1298
9457afee 1299 err = netlink_attachskb(sk, skb, &timeo, ssk);
1da177e4
LT
1300 if (err == 1)
1301 goto retry;
1302 if (err)
1303 return err;
1304
7ee015e0 1305 return netlink_sendskb(sk, skb);
1da177e4 1306}
6ac552fd 1307EXPORT_SYMBOL(netlink_unicast);
1da177e4 1308
4277a083
PM
1309int netlink_has_listeners(struct sock *sk, unsigned int group)
1310{
1311 int res = 0;
5c398dc8 1312 struct listeners *listeners;
4277a083 1313
aed81560 1314 BUG_ON(!netlink_is_kernel(sk));
b4ff4f04
JB
1315
1316 rcu_read_lock();
1317 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1318
6d772ac5 1319 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
5c398dc8 1320 res = test_bit(group - 1, listeners->masks);
b4ff4f04
JB
1321
1322 rcu_read_unlock();
1323
4277a083
PM
1324 return res;
1325}
1326EXPORT_SYMBOL_GPL(netlink_has_listeners);
1327
b57ef81f 1328static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1da177e4
LT
1329{
1330 struct netlink_sock *nlk = nlk_sk(sk);
1331
1332 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
cc3a572f 1333 !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
cf0a018a 1334 netlink_skb_set_owner_r(skb, sk);
4a7e7c2a 1335 __netlink_sendskb(sk, skb);
2c645800 1336 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1da177e4
LT
1337 }
1338 return -1;
1339}
1340
1341struct netlink_broadcast_data {
1342 struct sock *exclude_sk;
b4b51029 1343 struct net *net;
15e47304 1344 u32 portid;
1da177e4
LT
1345 u32 group;
1346 int failure;
ff491a73 1347 int delivery_failure;
1da177e4
LT
1348 int congested;
1349 int delivered;
7d877f3b 1350 gfp_t allocation;
1da177e4 1351 struct sk_buff *skb, *skb2;
910a7e90
EB
1352 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1353 void *tx_data;
1da177e4
LT
1354};
1355
46c9521f
RR
1356static void do_one_broadcast(struct sock *sk,
1357 struct netlink_broadcast_data *p)
1da177e4
LT
1358{
1359 struct netlink_sock *nlk = nlk_sk(sk);
1360 int val;
1361
1362 if (p->exclude_sk == sk)
46c9521f 1363 return;
1da177e4 1364
15e47304 1365 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 1366 !test_bit(p->group - 1, nlk->groups))
46c9521f 1367 return;
1da177e4 1368
59324cf3
ND
1369 if (!net_eq(sock_net(sk), p->net)) {
1370 if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
1371 return;
1372
1373 if (!peernet_has_id(sock_net(sk), p->net))
1374 return;
1375
1376 if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
1377 CAP_NET_BROADCAST))
1378 return;
1379 }
b4b51029 1380
1da177e4
LT
1381 if (p->failure) {
1382 netlink_overrun(sk);
46c9521f 1383 return;
1da177e4
LT
1384 }
1385
1386 sock_hold(sk);
1387 if (p->skb2 == NULL) {
68acc024 1388 if (skb_shared(p->skb)) {
1da177e4
LT
1389 p->skb2 = skb_clone(p->skb, p->allocation);
1390 } else {
68acc024
TC
1391 p->skb2 = skb_get(p->skb);
1392 /*
1393 * skb ownership may have been set when
1394 * delivered to a previous socket.
1395 */
1396 skb_orphan(p->skb2);
1da177e4
LT
1397 }
1398 }
1399 if (p->skb2 == NULL) {
1400 netlink_overrun(sk);
1401 /* Clone failed. Notify ALL listeners. */
1402 p->failure = 1;
cc3a572f 1403 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
be0c22a4 1404 p->delivery_failure = 1;
59324cf3
ND
1405 goto out;
1406 }
1407 if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
910a7e90
EB
1408 kfree_skb(p->skb2);
1409 p->skb2 = NULL;
59324cf3
ND
1410 goto out;
1411 }
1412 if (sk_filter(sk, p->skb2)) {
b1153f29
SH
1413 kfree_skb(p->skb2);
1414 p->skb2 = NULL;
59324cf3
ND
1415 goto out;
1416 }
1417 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
1418 NETLINK_CB(p->skb2).nsid_is_set = true;
1419 val = netlink_broadcast_deliver(sk, p->skb2);
1420 if (val < 0) {
1da177e4 1421 netlink_overrun(sk);
cc3a572f 1422 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
be0c22a4 1423 p->delivery_failure = 1;
1da177e4
LT
1424 } else {
1425 p->congested |= val;
1426 p->delivered = 1;
1427 p->skb2 = NULL;
1428 }
59324cf3 1429out:
1da177e4 1430 sock_put(sk);
1da177e4
LT
1431}
1432
15e47304 1433int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
1434 u32 group, gfp_t allocation,
1435 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1436 void *filter_data)
1da177e4 1437{
3b1e0a65 1438 struct net *net = sock_net(ssk);
1da177e4 1439 struct netlink_broadcast_data info;
1da177e4
LT
1440 struct sock *sk;
1441
1442 skb = netlink_trim(skb, allocation);
1443
1444 info.exclude_sk = ssk;
b4b51029 1445 info.net = net;
15e47304 1446 info.portid = portid;
1da177e4
LT
1447 info.group = group;
1448 info.failure = 0;
ff491a73 1449 info.delivery_failure = 0;
1da177e4
LT
1450 info.congested = 0;
1451 info.delivered = 0;
1452 info.allocation = allocation;
1453 info.skb = skb;
1454 info.skb2 = NULL;
910a7e90
EB
1455 info.tx_filter = filter;
1456 info.tx_data = filter_data;
1da177e4
LT
1457
1458 /* While we sleep in clone, do not allow to change socket list */
1459
1460 netlink_lock_table();
1461
b67bfe0d 1462 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1da177e4
LT
1463 do_one_broadcast(sk, &info);
1464
70d4bf6d 1465 consume_skb(skb);
aa1c6a6f 1466
1da177e4
LT
1467 netlink_unlock_table();
1468
70d4bf6d
NH
1469 if (info.delivery_failure) {
1470 kfree_skb(info.skb2);
ff491a73 1471 return -ENOBUFS;
658cb354
ED
1472 }
1473 consume_skb(info.skb2);
ff491a73 1474
1da177e4 1475 if (info.delivered) {
d0164adc 1476 if (info.congested && gfpflags_allow_blocking(allocation))
1da177e4
LT
1477 yield();
1478 return 0;
1479 }
1da177e4
LT
1480 return -ESRCH;
1481}
910a7e90
EB
1482EXPORT_SYMBOL(netlink_broadcast_filtered);
1483
15e47304 1484int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
1485 u32 group, gfp_t allocation)
1486{
15e47304 1487 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
910a7e90
EB
1488 NULL, NULL);
1489}
6ac552fd 1490EXPORT_SYMBOL(netlink_broadcast);
1da177e4
LT
1491
1492struct netlink_set_err_data {
1493 struct sock *exclude_sk;
15e47304 1494 u32 portid;
1da177e4
LT
1495 u32 group;
1496 int code;
1497};
1498
b57ef81f 1499static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1da177e4
LT
1500{
1501 struct netlink_sock *nlk = nlk_sk(sk);
1a50307b 1502 int ret = 0;
1da177e4
LT
1503
1504 if (sk == p->exclude_sk)
1505 goto out;
1506
09ad9bc7 1507 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
b4b51029
EB
1508 goto out;
1509
15e47304 1510 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 1511 !test_bit(p->group - 1, nlk->groups))
1da177e4
LT
1512 goto out;
1513
cc3a572f 1514 if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
1a50307b
PNA
1515 ret = 1;
1516 goto out;
1517 }
1518
1da177e4
LT
1519 sk->sk_err = p->code;
1520 sk->sk_error_report(sk);
1521out:
1a50307b 1522 return ret;
1da177e4
LT
1523}
1524
4843b93c
PNA
1525/**
1526 * netlink_set_err - report error to broadcast listeners
1527 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
15e47304 1528 * @portid: the PORTID of a process that we want to skip (if any)
840e93f2 1529 * @group: the broadcast group that will notice the error
4843b93c 1530 * @code: error code, must be negative (as usual in kernelspace)
1a50307b
PNA
1531 *
1532 * This function returns the number of broadcast listeners that have set the
cc3a572f 1533 * NETLINK_NO_ENOBUFS socket option.
4843b93c 1534 */
15e47304 1535int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1da177e4
LT
1536{
1537 struct netlink_set_err_data info;
1da177e4 1538 struct sock *sk;
1a50307b 1539 int ret = 0;
1da177e4
LT
1540
1541 info.exclude_sk = ssk;
15e47304 1542 info.portid = portid;
1da177e4 1543 info.group = group;
4843b93c
PNA
1544 /* sk->sk_err wants a positive error value */
1545 info.code = -code;
1da177e4
LT
1546
1547 read_lock(&nl_table_lock);
1548
b67bfe0d 1549 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1a50307b 1550 ret += do_one_set_err(sk, &info);
1da177e4
LT
1551
1552 read_unlock(&nl_table_lock);
1a50307b 1553 return ret;
1da177e4 1554}
dd5b6ce6 1555EXPORT_SYMBOL(netlink_set_err);
1da177e4 1556
84659eb5
JB
1557/* must be called with netlink table grabbed */
1558static void netlink_update_socket_mc(struct netlink_sock *nlk,
1559 unsigned int group,
1560 int is_new)
1561{
1562 int old, new = !!is_new, subscriptions;
1563
1564 old = test_bit(group - 1, nlk->groups);
1565 subscriptions = nlk->subscriptions - old + new;
1566 if (new)
1567 __set_bit(group - 1, nlk->groups);
1568 else
1569 __clear_bit(group - 1, nlk->groups);
1570 netlink_update_subscriptions(&nlk->sk, subscriptions);
1571 netlink_update_listeners(&nlk->sk);
1572}
1573
9a4595bc 1574static int netlink_setsockopt(struct socket *sock, int level, int optname,
b7058842 1575 char __user *optval, unsigned int optlen)
9a4595bc
PM
1576{
1577 struct sock *sk = sock->sk;
1578 struct netlink_sock *nlk = nlk_sk(sk);
eb496534
JB
1579 unsigned int val = 0;
1580 int err;
9a4595bc
PM
1581
1582 if (level != SOL_NETLINK)
1583 return -ENOPROTOOPT;
1584
d1b4c689 1585 if (optlen >= sizeof(int) &&
eb496534 1586 get_user(val, (unsigned int __user *)optval))
9a4595bc
PM
1587 return -EFAULT;
1588
1589 switch (optname) {
1590 case NETLINK_PKTINFO:
1591 if (val)
cc3a572f 1592 nlk->flags |= NETLINK_F_RECV_PKTINFO;
9a4595bc 1593 else
cc3a572f 1594 nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
9a4595bc
PM
1595 err = 0;
1596 break;
1597 case NETLINK_ADD_MEMBERSHIP:
1598 case NETLINK_DROP_MEMBERSHIP: {
5187cd05 1599 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
9a4595bc 1600 return -EPERM;
b4ff4f04
JB
1601 err = netlink_realloc_groups(sk);
1602 if (err)
1603 return err;
9a4595bc
PM
1604 if (!val || val - 1 >= nlk->ngroups)
1605 return -EINVAL;
7774d5e0 1606 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
023e2cfa 1607 err = nlk->netlink_bind(sock_net(sk), val);
4f520900
RGB
1608 if (err)
1609 return err;
1610 }
9a4595bc 1611 netlink_table_grab();
84659eb5
JB
1612 netlink_update_socket_mc(nlk, val,
1613 optname == NETLINK_ADD_MEMBERSHIP);
9a4595bc 1614 netlink_table_ungrab();
7774d5e0 1615 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
023e2cfa 1616 nlk->netlink_unbind(sock_net(sk), val);
03292745 1617
9a4595bc
PM
1618 err = 0;
1619 break;
1620 }
be0c22a4
PNA
1621 case NETLINK_BROADCAST_ERROR:
1622 if (val)
cc3a572f 1623 nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
be0c22a4 1624 else
cc3a572f 1625 nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
be0c22a4
PNA
1626 err = 0;
1627 break;
38938bfe
PNA
1628 case NETLINK_NO_ENOBUFS:
1629 if (val) {
cc3a572f
ND
1630 nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
1631 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
38938bfe 1632 wake_up_interruptible(&nlk->wait);
658cb354 1633 } else {
cc3a572f 1634 nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
658cb354 1635 }
38938bfe
PNA
1636 err = 0;
1637 break;
59324cf3
ND
1638 case NETLINK_LISTEN_ALL_NSID:
1639 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
1640 return -EPERM;
1641
1642 if (val)
1643 nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
1644 else
1645 nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
1646 err = 0;
1647 break;
0a6a3a23
CR
1648 case NETLINK_CAP_ACK:
1649 if (val)
1650 nlk->flags |= NETLINK_F_CAP_ACK;
1651 else
1652 nlk->flags &= ~NETLINK_F_CAP_ACK;
1653 err = 0;
1654 break;
2d4bc933
JB
1655 case NETLINK_EXT_ACK:
1656 if (val)
1657 nlk->flags |= NETLINK_F_EXT_ACK;
1658 else
1659 nlk->flags &= ~NETLINK_F_EXT_ACK;
1660 err = 0;
1661 break;
9a4595bc
PM
1662 default:
1663 err = -ENOPROTOOPT;
1664 }
1665 return err;
1666}
1667
1668static int netlink_getsockopt(struct socket *sock, int level, int optname,
746fac4d 1669 char __user *optval, int __user *optlen)
9a4595bc
PM
1670{
1671 struct sock *sk = sock->sk;
1672 struct netlink_sock *nlk = nlk_sk(sk);
1673 int len, val, err;
1674
1675 if (level != SOL_NETLINK)
1676 return -ENOPROTOOPT;
1677
1678 if (get_user(len, optlen))
1679 return -EFAULT;
1680 if (len < 0)
1681 return -EINVAL;
1682
1683 switch (optname) {
1684 case NETLINK_PKTINFO:
1685 if (len < sizeof(int))
1686 return -EINVAL;
1687 len = sizeof(int);
cc3a572f 1688 val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
a27b58fe
HC
1689 if (put_user(len, optlen) ||
1690 put_user(val, optval))
1691 return -EFAULT;
9a4595bc
PM
1692 err = 0;
1693 break;
be0c22a4
PNA
1694 case NETLINK_BROADCAST_ERROR:
1695 if (len < sizeof(int))
1696 return -EINVAL;
1697 len = sizeof(int);
cc3a572f 1698 val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
be0c22a4
PNA
1699 if (put_user(len, optlen) ||
1700 put_user(val, optval))
1701 return -EFAULT;
1702 err = 0;
1703 break;
38938bfe
PNA
1704 case NETLINK_NO_ENOBUFS:
1705 if (len < sizeof(int))
1706 return -EINVAL;
1707 len = sizeof(int);
cc3a572f 1708 val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
38938bfe
PNA
1709 if (put_user(len, optlen) ||
1710 put_user(val, optval))
1711 return -EFAULT;
1712 err = 0;
1713 break;
b42be38b
DH
1714 case NETLINK_LIST_MEMBERSHIPS: {
1715 int pos, idx, shift;
1716
1717 err = 0;
47191d65 1718 netlink_lock_table();
b42be38b
DH
1719 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
1720 if (len - pos < sizeof(u32))
1721 break;
1722
1723 idx = pos / sizeof(unsigned long);
1724 shift = (pos % sizeof(unsigned long)) * 8;
1725 if (put_user((u32)(nlk->groups[idx] >> shift),
1726 (u32 __user *)(optval + pos))) {
1727 err = -EFAULT;
1728 break;
1729 }
1730 }
1731 if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1732 err = -EFAULT;
47191d65 1733 netlink_unlock_table();
b42be38b
DH
1734 break;
1735 }
0a6a3a23
CR
1736 case NETLINK_CAP_ACK:
1737 if (len < sizeof(int))
1738 return -EINVAL;
1739 len = sizeof(int);
1740 val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1741 if (put_user(len, optlen) ||
1742 put_user(val, optval))
1743 return -EFAULT;
1744 err = 0;
1745 break;
2d4bc933
JB
1746 case NETLINK_EXT_ACK:
1747 if (len < sizeof(int))
1748 return -EINVAL;
1749 len = sizeof(int);
1750 val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1751 if (put_user(len, optlen) || put_user(val, optval))
1752 return -EFAULT;
1753 err = 0;
1754 break;
9a4595bc
PM
1755 default:
1756 err = -ENOPROTOOPT;
1757 }
1758 return err;
1759}
1760
1761static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1762{
1763 struct nl_pktinfo info;
1764
1765 info.group = NETLINK_CB(skb).dst_group;
1766 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1767}
1768
59324cf3
ND
1769static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
1770 struct sk_buff *skb)
1771{
1772 if (!NETLINK_CB(skb).nsid_is_set)
1773 return;
1774
1775 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
1776 &NETLINK_CB(skb).nsid);
1777}
1778
1b784140 1779static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1da177e4 1780{
1da177e4
LT
1781 struct sock *sk = sock->sk;
1782 struct netlink_sock *nlk = nlk_sk(sk);
342dfc30 1783 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
15e47304 1784 u32 dst_portid;
d629b836 1785 u32 dst_group;
1da177e4
LT
1786 struct sk_buff *skb;
1787 int err;
1788 struct scm_cookie scm;
2d7a85f4 1789 u32 netlink_skb_flags = 0;
1da177e4
LT
1790
1791 if (msg->msg_flags&MSG_OOB)
1792 return -EOPNOTSUPP;
1793
7cc05662 1794 err = scm_send(sock, msg, &scm, true);
1da177e4
LT
1795 if (err < 0)
1796 return err;
1797
1798 if (msg->msg_namelen) {
b47030c7 1799 err = -EINVAL;
1da177e4 1800 if (addr->nl_family != AF_NETLINK)
b47030c7 1801 goto out;
15e47304 1802 dst_portid = addr->nl_pid;
d629b836 1803 dst_group = ffs(addr->nl_groups);
b47030c7 1804 err = -EPERM;
15e47304 1805 if ((dst_group || dst_portid) &&
5187cd05 1806 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
b47030c7 1807 goto out;
2d7a85f4 1808 netlink_skb_flags |= NETLINK_SKB_DST;
1da177e4 1809 } else {
15e47304 1810 dst_portid = nlk->dst_portid;
d629b836 1811 dst_group = nlk->dst_group;
1da177e4
LT
1812 }
1813
da314c99 1814 if (!nlk->bound) {
1da177e4
LT
1815 err = netlink_autobind(sock);
1816 if (err)
1817 goto out;
da314c99
HX
1818 } else {
1819 /* Ensure nlk is hashed and visible. */
1820 smp_rmb();
1da177e4
LT
1821 }
1822
1823 err = -EMSGSIZE;
1824 if (len > sk->sk_sndbuf - 32)
1825 goto out;
1826 err = -ENOBUFS;
3a36515f 1827 skb = netlink_alloc_large_skb(len, dst_group);
6ac552fd 1828 if (skb == NULL)
1da177e4
LT
1829 goto out;
1830
15e47304 1831 NETLINK_CB(skb).portid = nlk->portid;
d629b836 1832 NETLINK_CB(skb).dst_group = dst_group;
7cc05662 1833 NETLINK_CB(skb).creds = scm.creds;
2d7a85f4 1834 NETLINK_CB(skb).flags = netlink_skb_flags;
1da177e4 1835
1da177e4 1836 err = -EFAULT;
6ce8e9ce 1837 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1da177e4
LT
1838 kfree_skb(skb);
1839 goto out;
1840 }
1841
1842 err = security_netlink_send(sk, skb);
1843 if (err) {
1844 kfree_skb(skb);
1845 goto out;
1846 }
1847
d629b836 1848 if (dst_group) {
1da177e4 1849 atomic_inc(&skb->users);
15e47304 1850 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1da177e4 1851 }
15e47304 1852 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1da177e4
LT
1853
1854out:
7cc05662 1855 scm_destroy(&scm);
1da177e4
LT
1856 return err;
1857}
1858
1b784140 1859static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1da177e4
LT
1860 int flags)
1861{
1da177e4
LT
1862 struct scm_cookie scm;
1863 struct sock *sk = sock->sk;
1864 struct netlink_sock *nlk = nlk_sk(sk);
1865 int noblock = flags&MSG_DONTWAIT;
1866 size_t copied;
68d6ac6d 1867 struct sk_buff *skb, *data_skb;
b44d211e 1868 int err, ret;
1da177e4
LT
1869
1870 if (flags&MSG_OOB)
1871 return -EOPNOTSUPP;
1872
1873 copied = 0;
1874
6ac552fd
PM
1875 skb = skb_recv_datagram(sk, flags, noblock, &err);
1876 if (skb == NULL)
1da177e4
LT
1877 goto out;
1878
68d6ac6d
JB
1879 data_skb = skb;
1880
1dacc76d
JB
1881#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1882 if (unlikely(skb_shinfo(skb)->frag_list)) {
1dacc76d 1883 /*
68d6ac6d
JB
1884 * If this skb has a frag_list, then here that means that we
1885 * will have to use the frag_list skb's data for compat tasks
1886 * and the regular skb's data for normal (non-compat) tasks.
1dacc76d 1887 *
68d6ac6d
JB
1888 * If we need to send the compat skb, assign it to the
1889 * 'data_skb' variable so that it will be used below for data
1890 * copying. We keep 'skb' for everything else, including
1891 * freeing both later.
1dacc76d 1892 */
68d6ac6d
JB
1893 if (flags & MSG_CMSG_COMPAT)
1894 data_skb = skb_shinfo(skb)->frag_list;
1dacc76d
JB
1895 }
1896#endif
1897
9063e21f
ED
1898 /* Record the max length of recvmsg() calls for future allocations */
1899 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
1900 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
d35c99ff 1901 SKB_WITH_OVERHEAD(32768));
9063e21f 1902
68d6ac6d 1903 copied = data_skb->len;
1da177e4
LT
1904 if (len < copied) {
1905 msg->msg_flags |= MSG_TRUNC;
1906 copied = len;
1907 }
1908
68d6ac6d 1909 skb_reset_transport_header(data_skb);
51f3d02b 1910 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
1da177e4
LT
1911
1912 if (msg->msg_name) {
342dfc30 1913 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1da177e4
LT
1914 addr->nl_family = AF_NETLINK;
1915 addr->nl_pad = 0;
15e47304 1916 addr->nl_pid = NETLINK_CB(skb).portid;
d629b836 1917 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1da177e4
LT
1918 msg->msg_namelen = sizeof(*addr);
1919 }
1920
cc3a572f 1921 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
cc9a06cd 1922 netlink_cmsg_recv_pktinfo(msg, skb);
59324cf3
ND
1923 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
1924 netlink_cmsg_listen_all_nsid(sk, msg, skb);
cc9a06cd 1925
7cc05662
CH
1926 memset(&scm, 0, sizeof(scm));
1927 scm.creds = *NETLINK_CREDS(skb);
188ccb55 1928 if (flags & MSG_TRUNC)
68d6ac6d 1929 copied = data_skb->len;
daa3766e 1930
1da177e4
LT
1931 skb_free_datagram(sk, skb);
1932
16b304f3
PS
1933 if (nlk->cb_running &&
1934 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
b44d211e
AV
1935 ret = netlink_dump(sk);
1936 if (ret) {
ac30ef83 1937 sk->sk_err = -ret;
b44d211e
AV
1938 sk->sk_error_report(sk);
1939 }
1940 }
1da177e4 1941
7cc05662 1942 scm_recv(sock, msg, &scm, flags);
1da177e4
LT
1943out:
1944 netlink_rcv_wake(sk);
1945 return err ? : copied;
1946}
1947
676d2369 1948static void netlink_data_ready(struct sock *sk)
1da177e4 1949{
cd40b7d3 1950 BUG();
1da177e4
LT
1951}
1952
1953/*
746fac4d 1954 * We export these functions to other modules. They provide a
1da177e4
LT
1955 * complete set of kernel non-blocking support for message
1956 * queueing.
1957 */
1958
1959struct sock *
9f00d977
PNA
1960__netlink_kernel_create(struct net *net, int unit, struct module *module,
1961 struct netlink_kernel_cfg *cfg)
1da177e4
LT
1962{
1963 struct socket *sock;
1964 struct sock *sk;
77247bbb 1965 struct netlink_sock *nlk;
5c398dc8 1966 struct listeners *listeners = NULL;
a31f2d17
PNA
1967 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1968 unsigned int groups;
1da177e4 1969
fab2caf6 1970 BUG_ON(!nl_table);
1da177e4 1971
6ac552fd 1972 if (unit < 0 || unit >= MAX_LINKS)
1da177e4
LT
1973 return NULL;
1974
1975 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1976 return NULL;
13d3078e
EB
1977
1978 if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
23fe1866
PE
1979 goto out_sock_release_nosk;
1980
1981 sk = sock->sk;
4fdb3bb7 1982
a31f2d17 1983 if (!cfg || cfg->groups < 32)
4277a083 1984 groups = 32;
a31f2d17
PNA
1985 else
1986 groups = cfg->groups;
4277a083 1987
5c398dc8 1988 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
4277a083
PM
1989 if (!listeners)
1990 goto out_sock_release;
1991
1da177e4 1992 sk->sk_data_ready = netlink_data_ready;
a31f2d17
PNA
1993 if (cfg && cfg->input)
1994 nlk_sk(sk)->netlink_rcv = cfg->input;
1da177e4 1995
8ea65f4a 1996 if (netlink_insert(sk, 0))
77247bbb 1997 goto out_sock_release;
4fdb3bb7 1998
77247bbb 1999 nlk = nlk_sk(sk);
cc3a572f 2000 nlk->flags |= NETLINK_F_KERNEL_SOCKET;
4fdb3bb7 2001
4fdb3bb7 2002 netlink_table_grab();
b4b51029
EB
2003 if (!nl_table[unit].registered) {
2004 nl_table[unit].groups = groups;
5c398dc8 2005 rcu_assign_pointer(nl_table[unit].listeners, listeners);
b4b51029
EB
2006 nl_table[unit].cb_mutex = cb_mutex;
2007 nl_table[unit].module = module;
9785e10a
PNA
2008 if (cfg) {
2009 nl_table[unit].bind = cfg->bind;
6251edd9 2010 nl_table[unit].unbind = cfg->unbind;
9785e10a 2011 nl_table[unit].flags = cfg->flags;
da12c90e
G
2012 if (cfg->compare)
2013 nl_table[unit].compare = cfg->compare;
9785e10a 2014 }
b4b51029 2015 nl_table[unit].registered = 1;
f937f1f4
JJ
2016 } else {
2017 kfree(listeners);
869e58f8 2018 nl_table[unit].registered++;
b4b51029 2019 }
4fdb3bb7 2020 netlink_table_ungrab();
77247bbb
PM
2021 return sk;
2022
4fdb3bb7 2023out_sock_release:
4277a083 2024 kfree(listeners);
9dfbec1f 2025 netlink_kernel_release(sk);
23fe1866
PE
2026 return NULL;
2027
2028out_sock_release_nosk:
4fdb3bb7 2029 sock_release(sock);
77247bbb 2030 return NULL;
1da177e4 2031}
9f00d977 2032EXPORT_SYMBOL(__netlink_kernel_create);
b7c6ba6e
DL
2033
2034void
2035netlink_kernel_release(struct sock *sk)
2036{
13d3078e
EB
2037 if (sk == NULL || sk->sk_socket == NULL)
2038 return;
2039
2040 sock_release(sk->sk_socket);
b7c6ba6e
DL
2041}
2042EXPORT_SYMBOL(netlink_kernel_release);
2043
d136f1bd 2044int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
b4ff4f04 2045{
5c398dc8 2046 struct listeners *new, *old;
b4ff4f04 2047 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
b4ff4f04
JB
2048
2049 if (groups < 32)
2050 groups = 32;
2051
b4ff4f04 2052 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
5c398dc8
ED
2053 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2054 if (!new)
d136f1bd 2055 return -ENOMEM;
6d772ac5 2056 old = nl_deref_protected(tbl->listeners);
5c398dc8
ED
2057 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2058 rcu_assign_pointer(tbl->listeners, new);
2059
37b6b935 2060 kfree_rcu(old, rcu);
b4ff4f04
JB
2061 }
2062 tbl->groups = groups;
2063
d136f1bd
JB
2064 return 0;
2065}
2066
2067/**
2068 * netlink_change_ngroups - change number of multicast groups
2069 *
2070 * This changes the number of multicast groups that are available
2071 * on a certain netlink family. Note that it is not possible to
2072 * change the number of groups to below 32. Also note that it does
2073 * not implicitly call netlink_clear_multicast_users() when the
2074 * number of groups is reduced.
2075 *
2076 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2077 * @groups: The new number of groups.
2078 */
2079int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2080{
2081 int err;
2082
2083 netlink_table_grab();
2084 err = __netlink_change_ngroups(sk, groups);
b4ff4f04 2085 netlink_table_ungrab();
d136f1bd 2086
b4ff4f04
JB
2087 return err;
2088}
b4ff4f04 2089
b8273570
JB
2090void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2091{
2092 struct sock *sk;
b8273570
JB
2093 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2094
b67bfe0d 2095 sk_for_each_bound(sk, &tbl->mc_list)
b8273570
JB
2096 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2097}
2098
a46621a3 2099struct nlmsghdr *
15e47304 2100__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
a46621a3
DV
2101{
2102 struct nlmsghdr *nlh;
573ce260 2103 int size = nlmsg_msg_size(len);
a46621a3 2104
23b45672 2105 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
a46621a3
DV
2106 nlh->nlmsg_type = type;
2107 nlh->nlmsg_len = size;
2108 nlh->nlmsg_flags = flags;
15e47304 2109 nlh->nlmsg_pid = portid;
a46621a3
DV
2110 nlh->nlmsg_seq = seq;
2111 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
573ce260 2112 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
a46621a3
DV
2113 return nlh;
2114}
2115EXPORT_SYMBOL(__nlmsg_put);
2116
1da177e4
LT
2117/*
2118 * It looks a bit ugly.
2119 * It would be better to create kernel thread.
2120 */
2121
2122static int netlink_dump(struct sock *sk)
2123{
2124 struct netlink_sock *nlk = nlk_sk(sk);
2125 struct netlink_callback *cb;
c7ac8679 2126 struct sk_buff *skb = NULL;
1da177e4 2127 struct nlmsghdr *nlh;
92964c79 2128 struct module *module;
bf8b79e4 2129 int len, err = -ENOBUFS;
db65a3aa 2130 int alloc_min_size;
c7ac8679 2131 int alloc_size;
1da177e4 2132
af65bdfc 2133 mutex_lock(nlk->cb_mutex);
16b304f3 2134 if (!nlk->cb_running) {
bf8b79e4
TG
2135 err = -EINVAL;
2136 goto errout_skb;
1da177e4
LT
2137 }
2138
d1b4c689 2139 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
f9c22888 2140 goto errout_skb;
9063e21f
ED
2141
2142 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2143 * required, but it makes sense to _attempt_ a 16K bytes allocation
2144 * to reduce number of system calls on dump operations, if user
2145 * ever provided a big enough buffer.
2146 */
db65a3aa
AR
2147 cb = &nlk->cb;
2148 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2149
2150 if (alloc_min_size < nlk->max_recvmsg_len) {
2151 alloc_size = nlk->max_recvmsg_len;
d35c99ff
ED
2152 skb = alloc_skb(alloc_size,
2153 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
2154 __GFP_NOWARN | __GFP_NORETRY);
9063e21f 2155 }
db65a3aa
AR
2156 if (!skb) {
2157 alloc_size = alloc_min_size;
c5b0db32 2158 skb = alloc_skb(alloc_size, GFP_KERNEL);
db65a3aa 2159 }
c7ac8679 2160 if (!skb)
c63d6ea3 2161 goto errout_skb;
db65a3aa
AR
2162
2163 /* Trim skb to allocated size. User is expected to provide buffer as
2164 * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
2165 * netlink_recvmsg())). dump will pack as many smaller messages as
2166 * could fit within the allocated skb. skb is typically allocated
2167 * with larger space than required (could be as much as near 2x the
2168 * requested size with align to next power of 2 approach). Allowing
2169 * dump to use the excess space makes it difficult for a user to have a
2170 * reasonable static buffer based on the expected largest dump of a
2171 * single netdev. The outcome is MSG_TRUNC error.
2172 */
d1b4c689 2173 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
f9c22888 2174 netlink_skb_set_owner_r(skb, sk);
c7ac8679 2175
1da177e4
LT
2176 len = cb->dump(skb, cb);
2177
2178 if (len > 0) {
af65bdfc 2179 mutex_unlock(nlk->cb_mutex);
b1153f29
SH
2180
2181 if (sk_filter(sk, skb))
2182 kfree_skb(skb);
4a7e7c2a
ED
2183 else
2184 __netlink_sendskb(sk, skb);
1da177e4
LT
2185 return 0;
2186 }
2187
bf8b79e4
TG
2188 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2189 if (!nlh)
2190 goto errout_skb;
2191
670dc283
JB
2192 nl_dump_check_consistent(cb, nlh);
2193
bf8b79e4
TG
2194 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2195
b1153f29
SH
2196 if (sk_filter(sk, skb))
2197 kfree_skb(skb);
4a7e7c2a
ED
2198 else
2199 __netlink_sendskb(sk, skb);
1da177e4 2200
a8f74b22
TG
2201 if (cb->done)
2202 cb->done(cb);
1da177e4 2203
16b304f3 2204 nlk->cb_running = false;
92964c79
HX
2205 module = cb->module;
2206 skb = cb->skb;
16b304f3 2207 mutex_unlock(nlk->cb_mutex);
92964c79
HX
2208 module_put(module);
2209 consume_skb(skb);
1da177e4 2210 return 0;
1797754e 2211
bf8b79e4 2212errout_skb:
af65bdfc 2213 mutex_unlock(nlk->cb_mutex);
bf8b79e4 2214 kfree_skb(skb);
bf8b79e4 2215 return err;
1da177e4
LT
2216}
2217
6dc878a8
G
2218int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2219 const struct nlmsghdr *nlh,
2220 struct netlink_dump_control *control)
1da177e4
LT
2221{
2222 struct netlink_callback *cb;
2223 struct sock *sk;
2224 struct netlink_sock *nlk;
b44d211e 2225 int ret;
1da177e4 2226
d1b4c689 2227 atomic_inc(&skb->users);
f9c22888 2228
15e47304 2229 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
1da177e4 2230 if (sk == NULL) {
16b304f3
PS
2231 ret = -ECONNREFUSED;
2232 goto error_free;
1da177e4 2233 }
6dc878a8 2234
16b304f3 2235 nlk = nlk_sk(sk);
af65bdfc 2236 mutex_lock(nlk->cb_mutex);
6dc878a8 2237 /* A dump is in progress... */
16b304f3 2238 if (nlk->cb_running) {
6dc878a8 2239 ret = -EBUSY;
16b304f3 2240 goto error_unlock;
1da177e4 2241 }
6dc878a8 2242 /* add reference of module which cb->dump belongs to */
16b304f3 2243 if (!try_module_get(control->module)) {
6dc878a8 2244 ret = -EPROTONOSUPPORT;
16b304f3 2245 goto error_unlock;
6dc878a8
G
2246 }
2247
16b304f3
PS
2248 cb = &nlk->cb;
2249 memset(cb, 0, sizeof(*cb));
fc9e50f5 2250 cb->start = control->start;
16b304f3
PS
2251 cb->dump = control->dump;
2252 cb->done = control->done;
2253 cb->nlh = nlh;
2254 cb->data = control->data;
2255 cb->module = control->module;
2256 cb->min_dump_alloc = control->min_dump_alloc;
2257 cb->skb = skb;
2258
2259 nlk->cb_running = true;
2260
af65bdfc 2261 mutex_unlock(nlk->cb_mutex);
1da177e4 2262
fc9e50f5
TH
2263 if (cb->start)
2264 cb->start(cb);
2265
b44d211e 2266 ret = netlink_dump(sk);
1da177e4 2267 sock_put(sk);
5c58298c 2268
b44d211e
AV
2269 if (ret)
2270 return ret;
2271
5c58298c
DL
2272 /* We successfully started a dump, by returning -EINTR we
2273 * signal not to send ACK even if it was requested.
2274 */
2275 return -EINTR;
16b304f3
PS
2276
2277error_unlock:
2278 sock_put(sk);
2279 mutex_unlock(nlk->cb_mutex);
2280error_free:
2281 kfree_skb(skb);
2282 return ret;
1da177e4 2283}
6dc878a8 2284EXPORT_SYMBOL(__netlink_dump_start);
1da177e4 2285
2d4bc933
JB
2286void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
2287 const struct netlink_ext_ack *extack)
1da177e4
LT
2288{
2289 struct sk_buff *skb;
2290 struct nlmsghdr *rep;
2291 struct nlmsgerr *errmsg;
339bf98f 2292 size_t payload = sizeof(*errmsg);
2d4bc933 2293 size_t tlvlen = 0;
0a6a3a23 2294 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
2d4bc933 2295 unsigned int flags = 0;
1da177e4 2296
0a6a3a23 2297 /* Error messages get the original request appened, unless the user
2d4bc933
JB
2298 * requests to cap the error message, and get extra error data if
2299 * requested.
0a6a3a23 2300 */
2d4bc933
JB
2301 if (err) {
2302 if (!(nlk->flags & NETLINK_F_CAP_ACK))
2303 payload += nlmsg_len(nlh);
2304 else
2305 flags |= NLM_F_CAPPED;
2306 if (nlk->flags & NETLINK_F_EXT_ACK && extack) {
2307 if (extack->_msg)
2308 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
2309 if (extack->bad_attr)
2310 tlvlen += nla_total_size(sizeof(u32));
2311 }
2312 } else {
2313 flags |= NLM_F_CAPPED;
ba0dc5f6
JB
2314
2315 if (nlk->flags & NETLINK_F_EXT_ACK &&
2316 extack && extack->cookie_len)
2317 tlvlen += nla_total_size(extack->cookie_len);
2d4bc933 2318 }
1da177e4 2319
2d4bc933
JB
2320 if (tlvlen)
2321 flags |= NLM_F_ACK_TLVS;
2322
2323 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
1da177e4
LT
2324 if (!skb) {
2325 struct sock *sk;
2326
3b1e0a65 2327 sk = netlink_lookup(sock_net(in_skb->sk),
b4b51029 2328 in_skb->sk->sk_protocol,
15e47304 2329 NETLINK_CB(in_skb).portid);
1da177e4
LT
2330 if (sk) {
2331 sk->sk_err = ENOBUFS;
2332 sk->sk_error_report(sk);
2333 sock_put(sk);
2334 }
2335 return;
2336 }
2337
15e47304 2338 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
2d4bc933 2339 NLMSG_ERROR, payload, flags);
bf8b79e4 2340 errmsg = nlmsg_data(rep);
1da177e4 2341 errmsg->error = err;
0a6a3a23 2342 memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
2d4bc933 2343
ba0dc5f6
JB
2344 if (nlk->flags & NETLINK_F_EXT_ACK && extack) {
2345 if (err) {
2346 if (extack->_msg)
2347 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
2348 extack->_msg));
2349 if (extack->bad_attr &&
2350 !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2351 (u8 *)extack->bad_attr >= in_skb->data +
2352 in_skb->len))
2353 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2354 (u8 *)extack->bad_attr -
2355 in_skb->data));
2356 } else {
2357 if (extack->cookie_len)
2358 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2359 extack->cookie_len,
2360 extack->cookie));
2361 }
2d4bc933
JB
2362 }
2363
2364 nlmsg_end(skb, rep);
2365
15e47304 2366 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1da177e4 2367}
6ac552fd 2368EXPORT_SYMBOL(netlink_ack);
1da177e4 2369
cd40b7d3 2370int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
2d4bc933
JB
2371 struct nlmsghdr *,
2372 struct netlink_ext_ack *))
82ace47a 2373{
2d4bc933 2374 struct netlink_ext_ack extack = {};
82ace47a
TG
2375 struct nlmsghdr *nlh;
2376 int err;
2377
2378 while (skb->len >= nlmsg_total_size(0)) {
cd40b7d3
DL
2379 int msglen;
2380
b529ccf2 2381 nlh = nlmsg_hdr(skb);
d35b6856 2382 err = 0;
82ace47a 2383
ad8e4b75 2384 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
82ace47a
TG
2385 return 0;
2386
d35b6856
TG
2387 /* Only requests are handled by the kernel */
2388 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
5c58298c 2389 goto ack;
45e7ae7f
TG
2390
2391 /* Skip control messages */
2392 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
5c58298c 2393 goto ack;
d35b6856 2394
2d4bc933 2395 err = cb(skb, nlh, &extack);
5c58298c
DL
2396 if (err == -EINTR)
2397 goto skip;
2398
2399ack:
d35b6856 2400 if (nlh->nlmsg_flags & NLM_F_ACK || err)
2d4bc933 2401 netlink_ack(skb, nlh, err, &extack);
82ace47a 2402
5c58298c 2403skip:
6ac552fd 2404 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
cd40b7d3
DL
2405 if (msglen > skb->len)
2406 msglen = skb->len;
2407 skb_pull(skb, msglen);
82ace47a
TG
2408 }
2409
2410 return 0;
2411}
6ac552fd 2412EXPORT_SYMBOL(netlink_rcv_skb);
82ace47a 2413
d387f6ad
TG
2414/**
2415 * nlmsg_notify - send a notification netlink message
2416 * @sk: netlink socket to use
2417 * @skb: notification message
15e47304 2418 * @portid: destination netlink portid for reports or 0
d387f6ad
TG
2419 * @group: destination multicast group or 0
2420 * @report: 1 to report back, 0 to disable
2421 * @flags: allocation flags
2422 */
15e47304 2423int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
d387f6ad
TG
2424 unsigned int group, int report, gfp_t flags)
2425{
2426 int err = 0;
2427
2428 if (group) {
15e47304 2429 int exclude_portid = 0;
d387f6ad
TG
2430
2431 if (report) {
2432 atomic_inc(&skb->users);
15e47304 2433 exclude_portid = portid;
d387f6ad
TG
2434 }
2435
1ce85fe4
PNA
2436 /* errors reported via destination sk->sk_err, but propagate
2437 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
15e47304 2438 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
d387f6ad
TG
2439 }
2440
1ce85fe4
PNA
2441 if (report) {
2442 int err2;
2443
15e47304 2444 err2 = nlmsg_unicast(sk, skb, portid);
1ce85fe4
PNA
2445 if (!err || err == -ESRCH)
2446 err = err2;
2447 }
d387f6ad
TG
2448
2449 return err;
2450}
6ac552fd 2451EXPORT_SYMBOL(nlmsg_notify);
d387f6ad 2452
1da177e4
LT
2453#ifdef CONFIG_PROC_FS
2454struct nl_seq_iter {
e372c414 2455 struct seq_net_private p;
56d28b1e 2456 struct rhashtable_iter hti;
1da177e4 2457 int link;
1da177e4
LT
2458};
2459
56d28b1e 2460static int netlink_walk_start(struct nl_seq_iter *iter)
1da177e4 2461{
56d28b1e 2462 int err;
1da177e4 2463
8f6fd83c
BC
2464 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2465 GFP_KERNEL);
56d28b1e
HX
2466 if (err) {
2467 iter->link = MAX_LINKS;
2468 return err;
1da177e4 2469 }
56d28b1e
HX
2470
2471 err = rhashtable_walk_start(&iter->hti);
2472 return err == -EAGAIN ? 0 : err;
1da177e4
LT
2473}
2474
56d28b1e 2475static void netlink_walk_stop(struct nl_seq_iter *iter)
1da177e4 2476{
56d28b1e
HX
2477 rhashtable_walk_stop(&iter->hti);
2478 rhashtable_walk_exit(&iter->hti);
1da177e4
LT
2479}
2480
56d28b1e 2481static void *__netlink_seq_next(struct seq_file *seq)
1da177e4 2482{
56d28b1e 2483 struct nl_seq_iter *iter = seq->private;
e341694e 2484 struct netlink_sock *nlk;
1da177e4 2485
56d28b1e
HX
2486 do {
2487 for (;;) {
2488 int err;
1da177e4 2489
56d28b1e 2490 nlk = rhashtable_walk_next(&iter->hti);
746fac4d 2491
56d28b1e
HX
2492 if (IS_ERR(nlk)) {
2493 if (PTR_ERR(nlk) == -EAGAIN)
2494 continue;
e341694e 2495
56d28b1e
HX
2496 return nlk;
2497 }
1da177e4 2498
56d28b1e
HX
2499 if (nlk)
2500 break;
1da177e4 2501
56d28b1e
HX
2502 netlink_walk_stop(iter);
2503 if (++iter->link >= MAX_LINKS)
2504 return NULL;
da12c90e 2505
56d28b1e
HX
2506 err = netlink_walk_start(iter);
2507 if (err)
2508 return ERR_PTR(err);
1da177e4 2509 }
56d28b1e 2510 } while (sock_net(&nlk->sk) != seq_file_net(seq));
1da177e4 2511
56d28b1e
HX
2512 return nlk;
2513}
1da177e4 2514
56d28b1e
HX
2515static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2516{
2517 struct nl_seq_iter *iter = seq->private;
2518 void *obj = SEQ_START_TOKEN;
2519 loff_t pos;
2520 int err;
2521
2522 iter->link = 0;
2523
2524 err = netlink_walk_start(iter);
2525 if (err)
2526 return ERR_PTR(err);
2527
2528 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2529 obj = __netlink_seq_next(seq);
2530
2531 return obj;
2532}
2533
2534static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2535{
2536 ++*pos;
2537 return __netlink_seq_next(seq);
1da177e4
LT
2538}
2539
2540static void netlink_seq_stop(struct seq_file *seq, void *v)
2541{
56d28b1e
HX
2542 struct nl_seq_iter *iter = seq->private;
2543
2544 if (iter->link >= MAX_LINKS)
2545 return;
2546
2547 netlink_walk_stop(iter);
1da177e4
LT
2548}
2549
2550
2551static int netlink_seq_show(struct seq_file *seq, void *v)
2552{
658cb354 2553 if (v == SEQ_START_TOKEN) {
1da177e4
LT
2554 seq_puts(seq,
2555 "sk Eth Pid Groups "
cf0aa4e0 2556 "Rmem Wmem Dump Locks Drops Inode\n");
658cb354 2557 } else {
1da177e4
LT
2558 struct sock *s = v;
2559 struct netlink_sock *nlk = nlk_sk(s);
2560
16b304f3 2561 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
1da177e4
LT
2562 s,
2563 s->sk_protocol,
15e47304 2564 nlk->portid,
513c2500 2565 nlk->groups ? (u32)nlk->groups[0] : 0,
31e6d363
ED
2566 sk_rmem_alloc_get(s),
2567 sk_wmem_alloc_get(s),
16b304f3 2568 nlk->cb_running,
38938bfe 2569 atomic_read(&s->sk_refcnt),
cf0aa4e0
MY
2570 atomic_read(&s->sk_drops),
2571 sock_i_ino(s)
1da177e4
LT
2572 );
2573
2574 }
2575 return 0;
2576}
2577
56b3d975 2578static const struct seq_operations netlink_seq_ops = {
1da177e4
LT
2579 .start = netlink_seq_start,
2580 .next = netlink_seq_next,
2581 .stop = netlink_seq_stop,
2582 .show = netlink_seq_show,
2583};
2584
2585
2586static int netlink_seq_open(struct inode *inode, struct file *file)
2587{
e372c414
DL
2588 return seq_open_net(inode, file, &netlink_seq_ops,
2589 sizeof(struct nl_seq_iter));
b4b51029
EB
2590}
2591
da7071d7 2592static const struct file_operations netlink_seq_fops = {
1da177e4
LT
2593 .owner = THIS_MODULE,
2594 .open = netlink_seq_open,
2595 .read = seq_read,
2596 .llseek = seq_lseek,
e372c414 2597 .release = seq_release_net,
1da177e4
LT
2598};
2599
2600#endif
2601
2602int netlink_register_notifier(struct notifier_block *nb)
2603{
efa172f4 2604 return blocking_notifier_chain_register(&netlink_chain, nb);
1da177e4 2605}
6ac552fd 2606EXPORT_SYMBOL(netlink_register_notifier);
1da177e4
LT
2607
2608int netlink_unregister_notifier(struct notifier_block *nb)
2609{
efa172f4 2610 return blocking_notifier_chain_unregister(&netlink_chain, nb);
1da177e4 2611}
6ac552fd 2612EXPORT_SYMBOL(netlink_unregister_notifier);
746fac4d 2613
90ddc4f0 2614static const struct proto_ops netlink_ops = {
1da177e4
LT
2615 .family = PF_NETLINK,
2616 .owner = THIS_MODULE,
2617 .release = netlink_release,
2618 .bind = netlink_bind,
2619 .connect = netlink_connect,
2620 .socketpair = sock_no_socketpair,
2621 .accept = sock_no_accept,
2622 .getname = netlink_getname,
d1b4c689 2623 .poll = datagram_poll,
025c6818 2624 .ioctl = netlink_ioctl,
1da177e4
LT
2625 .listen = sock_no_listen,
2626 .shutdown = sock_no_shutdown,
9a4595bc
PM
2627 .setsockopt = netlink_setsockopt,
2628 .getsockopt = netlink_getsockopt,
1da177e4
LT
2629 .sendmsg = netlink_sendmsg,
2630 .recvmsg = netlink_recvmsg,
d1b4c689 2631 .mmap = sock_no_mmap,
1da177e4
LT
2632 .sendpage = sock_no_sendpage,
2633};
2634
ec1b4cf7 2635static const struct net_proto_family netlink_family_ops = {
1da177e4
LT
2636 .family = PF_NETLINK,
2637 .create = netlink_create,
2638 .owner = THIS_MODULE, /* for consistency 8) */
2639};
2640
4665079c 2641static int __net_init netlink_net_init(struct net *net)
b4b51029
EB
2642{
2643#ifdef CONFIG_PROC_FS
d4beaa66 2644 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
b4b51029
EB
2645 return -ENOMEM;
2646#endif
2647 return 0;
2648}
2649
4665079c 2650static void __net_exit netlink_net_exit(struct net *net)
b4b51029
EB
2651{
2652#ifdef CONFIG_PROC_FS
ece31ffd 2653 remove_proc_entry("netlink", net->proc_net);
b4b51029
EB
2654#endif
2655}
2656
b963ea89
DM
2657static void __init netlink_add_usersock_entry(void)
2658{
5c398dc8 2659 struct listeners *listeners;
b963ea89
DM
2660 int groups = 32;
2661
5c398dc8 2662 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
b963ea89 2663 if (!listeners)
5c398dc8 2664 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
b963ea89
DM
2665
2666 netlink_table_grab();
2667
2668 nl_table[NETLINK_USERSOCK].groups = groups;
5c398dc8 2669 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
b963ea89
DM
2670 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2671 nl_table[NETLINK_USERSOCK].registered = 1;
9785e10a 2672 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
b963ea89
DM
2673
2674 netlink_table_ungrab();
2675}
2676
022cbae6 2677static struct pernet_operations __net_initdata netlink_net_ops = {
b4b51029
EB
2678 .init = netlink_net_init,
2679 .exit = netlink_net_exit,
2680};
2681
49f7b33e 2682static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
c428ecd1
HX
2683{
2684 const struct netlink_sock *nlk = data;
2685 struct netlink_compare_arg arg;
2686
da314c99 2687 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
11b58ba1 2688 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
c428ecd1
HX
2689}
2690
2691static const struct rhashtable_params netlink_rhashtable_params = {
2692 .head_offset = offsetof(struct netlink_sock, node),
2693 .key_len = netlink_compare_arg_len,
c428ecd1
HX
2694 .obj_hashfn = netlink_hash,
2695 .obj_cmpfn = netlink_compare,
b5e2c150 2696 .automatic_shrinking = true,
c428ecd1
HX
2697};
2698
1da177e4
LT
2699static int __init netlink_proto_init(void)
2700{
1da177e4 2701 int i;
1da177e4
LT
2702 int err = proto_register(&netlink_proto, 0);
2703
2704 if (err != 0)
2705 goto out;
2706
fab25745 2707 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
1da177e4 2708
0da974f4 2709 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
fab2caf6
AM
2710 if (!nl_table)
2711 goto panic;
1da177e4 2712
1da177e4 2713 for (i = 0; i < MAX_LINKS; i++) {
c428ecd1
HX
2714 if (rhashtable_init(&nl_table[i].hash,
2715 &netlink_rhashtable_params) < 0) {
e341694e
TG
2716 while (--i > 0)
2717 rhashtable_destroy(&nl_table[i].hash);
1da177e4 2718 kfree(nl_table);
fab2caf6 2719 goto panic;
1da177e4 2720 }
1da177e4
LT
2721 }
2722
bcbde0d4
DB
2723 INIT_LIST_HEAD(&netlink_tap_all);
2724
b963ea89
DM
2725 netlink_add_usersock_entry();
2726
1da177e4 2727 sock_register(&netlink_family_ops);
b4b51029 2728 register_pernet_subsys(&netlink_net_ops);
746fac4d 2729 /* The netlink device handler may be needed early. */
1da177e4
LT
2730 rtnetlink_init();
2731out:
2732 return err;
fab2caf6
AM
2733panic:
2734 panic("netlink_init: Cannot allocate nl_table\n");
1da177e4
LT
2735}
2736
1da177e4 2737core_initcall(netlink_proto_init);