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