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