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