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