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