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 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net); 1487 if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED) 1488 NETLINK_CB(p->skb2).nsid_is_set = true; 1489 val = netlink_broadcast_deliver(sk, p->skb2); 1490 if (val < 0) { 1491 netlink_overrun(sk); 1492 if (nlk_test_bit(BROADCAST_SEND_ERROR, sk)) 1493 p->delivery_failure = 1; 1494 } else { 1495 p->congested |= val; 1496 p->delivered = 1; 1497 p->skb2 = NULL; 1498 } 1499 out: 1500 sock_put(sk); 1501 } 1502 1503 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, 1504 u32 portid, 1505 u32 group, gfp_t allocation, 1506 netlink_filter_fn filter, 1507 void *filter_data) 1508 { 1509 struct net *net = sock_net(ssk); 1510 struct netlink_broadcast_data info; 1511 struct sock *sk; 1512 1513 skb = netlink_trim(skb, allocation); 1514 1515 info.exclude_sk = ssk; 1516 info.net = net; 1517 info.portid = portid; 1518 info.group = group; 1519 info.failure = 0; 1520 info.delivery_failure = 0; 1521 info.congested = 0; 1522 info.delivered = 0; 1523 info.allocation = allocation; 1524 info.skb = skb; 1525 info.skb2 = NULL; 1526 info.tx_filter = filter; 1527 info.tx_data = filter_data; 1528 1529 /* While we sleep in clone, do not allow to change socket list */ 1530 1531 netlink_lock_table(); 1532 1533 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) 1534 do_one_broadcast(sk, &info); 1535 1536 consume_skb(skb); 1537 1538 netlink_unlock_table(); 1539 1540 if (info.delivery_failure) { 1541 kfree_skb(info.skb2); 1542 return -ENOBUFS; 1543 } 1544 consume_skb(info.skb2); 1545 1546 if (info.delivered) { 1547 if (info.congested && gfpflags_allow_blocking(allocation)) 1548 yield(); 1549 return 0; 1550 } 1551 return -ESRCH; 1552 } 1553 EXPORT_SYMBOL(netlink_broadcast_filtered); 1554 1555 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid, 1556 u32 group, gfp_t allocation) 1557 { 1558 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation, 1559 NULL, NULL); 1560 } 1561 EXPORT_SYMBOL(netlink_broadcast); 1562 1563 struct netlink_set_err_data { 1564 struct sock *exclude_sk; 1565 u32 portid; 1566 u32 group; 1567 int code; 1568 }; 1569 1570 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) 1571 { 1572 struct netlink_sock *nlk = nlk_sk(sk); 1573 int ret = 0; 1574 1575 if (sk == p->exclude_sk) 1576 goto out; 1577 1578 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) 1579 goto out; 1580 1581 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || 1582 !test_bit(p->group - 1, nlk->groups)) 1583 goto out; 1584 1585 if (p->code == ENOBUFS && nlk_test_bit(RECV_NO_ENOBUFS, sk)) { 1586 ret = 1; 1587 goto out; 1588 } 1589 1590 WRITE_ONCE(sk->sk_err, p->code); 1591 sk_error_report(sk); 1592 out: 1593 return ret; 1594 } 1595 1596 /** 1597 * netlink_set_err - report error to broadcast listeners 1598 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() 1599 * @portid: the PORTID of a process that we want to skip (if any) 1600 * @group: the broadcast group that will notice the error 1601 * @code: error code, must be negative (as usual in kernelspace) 1602 * 1603 * This function returns the number of broadcast listeners that have set the 1604 * NETLINK_NO_ENOBUFS socket option. 1605 */ 1606 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code) 1607 { 1608 struct netlink_set_err_data info; 1609 unsigned long flags; 1610 struct sock *sk; 1611 int ret = 0; 1612 1613 info.exclude_sk = ssk; 1614 info.portid = portid; 1615 info.group = group; 1616 /* sk->sk_err wants a positive error value */ 1617 info.code = -code; 1618 1619 read_lock_irqsave(&nl_table_lock, flags); 1620 1621 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) 1622 ret += do_one_set_err(sk, &info); 1623 1624 read_unlock_irqrestore(&nl_table_lock, flags); 1625 return ret; 1626 } 1627 EXPORT_SYMBOL(netlink_set_err); 1628 1629 /* must be called with netlink table grabbed */ 1630 static void netlink_update_socket_mc(struct netlink_sock *nlk, 1631 unsigned int group, 1632 int is_new) 1633 { 1634 int old, new = !!is_new, subscriptions; 1635 1636 old = test_bit(group - 1, nlk->groups); 1637 subscriptions = nlk->subscriptions - old + new; 1638 __assign_bit(group - 1, nlk->groups, new); 1639 netlink_update_subscriptions(&nlk->sk, subscriptions); 1640 netlink_update_listeners(&nlk->sk); 1641 } 1642 1643 static int netlink_setsockopt(struct socket *sock, int level, int optname, 1644 sockptr_t optval, unsigned int optlen) 1645 { 1646 struct sock *sk = sock->sk; 1647 struct netlink_sock *nlk = nlk_sk(sk); 1648 unsigned int val = 0; 1649 int nr = -1; 1650 1651 if (level != SOL_NETLINK) 1652 return -ENOPROTOOPT; 1653 1654 if (optlen >= sizeof(int) && 1655 copy_from_sockptr(&val, optval, sizeof(val))) 1656 return -EFAULT; 1657 1658 switch (optname) { 1659 case NETLINK_PKTINFO: 1660 nr = NETLINK_F_RECV_PKTINFO; 1661 break; 1662 case NETLINK_ADD_MEMBERSHIP: 1663 case NETLINK_DROP_MEMBERSHIP: { 1664 int err; 1665 1666 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) 1667 return -EPERM; 1668 err = netlink_realloc_groups(sk); 1669 if (err) 1670 return err; 1671 if (!val || val - 1 >= nlk->ngroups) 1672 return -EINVAL; 1673 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) { 1674 err = nlk->netlink_bind(sock_net(sk), val); 1675 if (err) 1676 return err; 1677 } 1678 netlink_table_grab(); 1679 netlink_update_socket_mc(nlk, val, 1680 optname == NETLINK_ADD_MEMBERSHIP); 1681 netlink_table_ungrab(); 1682 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind) 1683 nlk->netlink_unbind(sock_net(sk), val); 1684 1685 break; 1686 } 1687 case NETLINK_BROADCAST_ERROR: 1688 nr = NETLINK_F_BROADCAST_SEND_ERROR; 1689 break; 1690 case NETLINK_NO_ENOBUFS: 1691 assign_bit(NETLINK_F_RECV_NO_ENOBUFS, &nlk->flags, val); 1692 if (val) { 1693 clear_bit(NETLINK_S_CONGESTED, &nlk->state); 1694 wake_up_interruptible(&nlk->wait); 1695 } 1696 break; 1697 case NETLINK_LISTEN_ALL_NSID: 1698 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST)) 1699 return -EPERM; 1700 nr = NETLINK_F_LISTEN_ALL_NSID; 1701 break; 1702 case NETLINK_CAP_ACK: 1703 nr = NETLINK_F_CAP_ACK; 1704 break; 1705 case NETLINK_EXT_ACK: 1706 nr = NETLINK_F_EXT_ACK; 1707 break; 1708 case NETLINK_GET_STRICT_CHK: 1709 nr = NETLINK_F_STRICT_CHK; 1710 break; 1711 default: 1712 return -ENOPROTOOPT; 1713 } 1714 if (nr >= 0) 1715 assign_bit(nr, &nlk->flags, val); 1716 return 0; 1717 } 1718 1719 static int netlink_getsockopt(struct socket *sock, int level, int optname, 1720 sockopt_t *opt) 1721 { 1722 struct sock *sk = sock->sk; 1723 struct netlink_sock *nlk = nlk_sk(sk); 1724 unsigned int flag; 1725 int len, val; 1726 u32 group; 1727 1728 if (level != SOL_NETLINK) 1729 return -ENOPROTOOPT; 1730 1731 len = opt->optlen; 1732 if (len < 0) 1733 return -EINVAL; 1734 1735 switch (optname) { 1736 case NETLINK_PKTINFO: 1737 flag = NETLINK_F_RECV_PKTINFO; 1738 break; 1739 case NETLINK_BROADCAST_ERROR: 1740 flag = NETLINK_F_BROADCAST_SEND_ERROR; 1741 break; 1742 case NETLINK_NO_ENOBUFS: 1743 flag = NETLINK_F_RECV_NO_ENOBUFS; 1744 break; 1745 case NETLINK_LIST_MEMBERSHIPS: { 1746 int pos, idx, shift, err = 0; 1747 1748 netlink_lock_table(); 1749 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) { 1750 if (len - pos < sizeof(u32)) 1751 break; 1752 1753 idx = pos / sizeof(unsigned long); 1754 shift = (pos % sizeof(unsigned long)) * 8; 1755 group = (u32)(nlk->groups[idx] >> shift); 1756 if (copy_to_iter(&group, sizeof(u32), 1757 &opt->iter_out) != sizeof(u32)) { 1758 err = -EFAULT; 1759 break; 1760 } 1761 } 1762 opt->optlen = ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)); 1763 netlink_unlock_table(); 1764 return err; 1765 } 1766 case NETLINK_LISTEN_ALL_NSID: 1767 flag = NETLINK_F_LISTEN_ALL_NSID; 1768 break; 1769 case NETLINK_CAP_ACK: 1770 flag = NETLINK_F_CAP_ACK; 1771 break; 1772 case NETLINK_EXT_ACK: 1773 flag = NETLINK_F_EXT_ACK; 1774 break; 1775 case NETLINK_GET_STRICT_CHK: 1776 flag = NETLINK_F_STRICT_CHK; 1777 break; 1778 default: 1779 return -ENOPROTOOPT; 1780 } 1781 1782 if (len < sizeof(int)) 1783 return -EINVAL; 1784 1785 len = sizeof(int); 1786 val = test_bit(flag, &nlk->flags); 1787 1788 opt->optlen = len; 1789 if (copy_to_iter(&val, len, &opt->iter_out) != len) 1790 return -EFAULT; 1791 1792 return 0; 1793 } 1794 1795 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) 1796 { 1797 struct nl_pktinfo info; 1798 1799 info.group = NETLINK_CB(skb).dst_group; 1800 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); 1801 } 1802 1803 static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg, 1804 struct sk_buff *skb) 1805 { 1806 if (!NETLINK_CB(skb).nsid_is_set) 1807 return; 1808 1809 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int), 1810 &NETLINK_CB(skb).nsid); 1811 } 1812 1813 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 1814 { 1815 struct sock *sk = sock->sk; 1816 struct netlink_sock *nlk = nlk_sk(sk); 1817 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); 1818 u32 dst_portid; 1819 u32 dst_group; 1820 struct sk_buff *skb; 1821 int err; 1822 struct scm_cookie scm; 1823 u32 netlink_skb_flags = 0; 1824 1825 if (msg->msg_flags & MSG_OOB) 1826 return -EOPNOTSUPP; 1827 1828 if (len == 0) { 1829 pr_warn_once("Zero length message leads to an empty skb\n"); 1830 return -ENODATA; 1831 } 1832 1833 err = scm_send(sock, msg, &scm, true); 1834 if (err < 0) 1835 return err; 1836 1837 if (msg->msg_namelen) { 1838 err = -EINVAL; 1839 if (msg->msg_namelen < sizeof(struct sockaddr_nl)) 1840 goto out; 1841 if (addr->nl_family != AF_NETLINK) 1842 goto out; 1843 dst_portid = addr->nl_pid; 1844 dst_group = ffs(addr->nl_groups); 1845 err = -EPERM; 1846 if ((dst_group || dst_portid) && 1847 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) 1848 goto out; 1849 netlink_skb_flags |= NETLINK_SKB_DST; 1850 } else { 1851 /* Paired with WRITE_ONCE() in netlink_connect() */ 1852 dst_portid = READ_ONCE(nlk->dst_portid); 1853 dst_group = READ_ONCE(nlk->dst_group); 1854 } 1855 1856 /* Paired with WRITE_ONCE() in netlink_insert() */ 1857 if (!READ_ONCE(nlk->bound)) { 1858 err = netlink_autobind(sock); 1859 if (err) 1860 goto out; 1861 } else { 1862 /* Ensure nlk is hashed and visible. */ 1863 smp_rmb(); 1864 } 1865 1866 err = -EMSGSIZE; 1867 if (len > sk->sk_sndbuf - 32) 1868 goto out; 1869 err = -ENOBUFS; 1870 skb = netlink_alloc_large_skb(len, dst_group); 1871 if (skb == NULL) 1872 goto out; 1873 1874 NETLINK_CB(skb).portid = nlk->portid; 1875 NETLINK_CB(skb).dst_group = dst_group; 1876 NETLINK_CB(skb).creds = scm.creds; 1877 NETLINK_CB(skb).flags = netlink_skb_flags; 1878 1879 err = -EFAULT; 1880 if (memcpy_from_msg(skb_put(skb, len), msg, len)) { 1881 kfree_skb(skb); 1882 goto out; 1883 } 1884 1885 err = security_netlink_send(sk, skb); 1886 if (err) { 1887 kfree_skb(skb); 1888 goto out; 1889 } 1890 1891 if (dst_group) { 1892 refcount_inc(&skb->users); 1893 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL); 1894 } 1895 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags & MSG_DONTWAIT); 1896 1897 out: 1898 scm_destroy(&scm); 1899 return err; 1900 } 1901 1902 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 1903 int flags) 1904 { 1905 struct scm_cookie scm; 1906 struct sock *sk = sock->sk; 1907 struct netlink_sock *nlk = nlk_sk(sk); 1908 size_t copied, max_recvmsg_len; 1909 struct sk_buff *skb, *data_skb; 1910 int err, ret; 1911 1912 if (flags & MSG_OOB) 1913 return -EOPNOTSUPP; 1914 1915 copied = 0; 1916 1917 skb = skb_recv_datagram(sk, flags, &err); 1918 if (skb == NULL) 1919 goto out; 1920 1921 data_skb = skb; 1922 1923 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES 1924 if (unlikely(skb_shinfo(skb)->frag_list)) { 1925 /* 1926 * If this skb has a frag_list, then here that means that we 1927 * will have to use the frag_list skb's data for compat tasks 1928 * and the regular skb's data for normal (non-compat) tasks. 1929 * 1930 * If we need to send the compat skb, assign it to the 1931 * 'data_skb' variable so that it will be used below for data 1932 * copying. We keep 'skb' for everything else, including 1933 * freeing both later. 1934 */ 1935 if (flags & MSG_CMSG_COMPAT) 1936 data_skb = skb_shinfo(skb)->frag_list; 1937 } 1938 #endif 1939 1940 /* Record the max length of recvmsg() calls for future allocations */ 1941 max_recvmsg_len = max(READ_ONCE(nlk->max_recvmsg_len), len); 1942 max_recvmsg_len = min_t(size_t, max_recvmsg_len, 1943 SKB_WITH_OVERHEAD(32768)); 1944 WRITE_ONCE(nlk->max_recvmsg_len, max_recvmsg_len); 1945 1946 copied = data_skb->len; 1947 if (len < copied) { 1948 msg->msg_flags |= MSG_TRUNC; 1949 copied = len; 1950 } 1951 1952 err = skb_copy_datagram_msg(data_skb, 0, msg, copied); 1953 1954 if (msg->msg_name) { 1955 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); 1956 addr->nl_family = AF_NETLINK; 1957 addr->nl_pad = 0; 1958 addr->nl_pid = NETLINK_CB(skb).portid; 1959 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); 1960 msg->msg_namelen = sizeof(*addr); 1961 } 1962 1963 if (nlk_test_bit(RECV_PKTINFO, sk)) 1964 netlink_cmsg_recv_pktinfo(msg, skb); 1965 if (nlk_test_bit(LISTEN_ALL_NSID, sk)) 1966 netlink_cmsg_listen_all_nsid(sk, msg, skb); 1967 1968 memset(&scm, 0, sizeof(scm)); 1969 scm.creds = *NETLINK_CREDS(skb); 1970 if (flags & MSG_TRUNC) 1971 copied = data_skb->len; 1972 1973 skb_free_datagram(sk, skb); 1974 1975 if (READ_ONCE(nlk->cb_running) && 1976 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { 1977 ret = netlink_dump(sk, false); 1978 if (ret) { 1979 WRITE_ONCE(sk->sk_err, -ret); 1980 sk_error_report(sk); 1981 } 1982 } 1983 1984 scm_recv(sock, msg, &scm, flags); 1985 out: 1986 netlink_rcv_wake(sk); 1987 return err ? : copied; 1988 } 1989 1990 static void netlink_data_ready(struct sock *sk) 1991 { 1992 BUG(); 1993 } 1994 1995 /* 1996 * We export these functions to other modules. They provide a 1997 * complete set of kernel non-blocking support for message 1998 * queueing. 1999 */ 2000 2001 struct sock * 2002 __netlink_kernel_create(struct net *net, int unit, struct module *module, 2003 struct netlink_kernel_cfg *cfg) 2004 { 2005 struct socket *sock; 2006 struct sock *sk; 2007 struct netlink_sock *nlk; 2008 struct listeners *listeners = NULL; 2009 unsigned int groups; 2010 2011 BUG_ON(!nl_table); 2012 2013 if (unit < 0 || unit >= MAX_LINKS) 2014 return NULL; 2015 2016 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) 2017 return NULL; 2018 2019 if (__netlink_create(net, sock, unit, 1) < 0) 2020 goto out_sock_release_nosk; 2021 2022 sk = sock->sk; 2023 2024 if (!cfg || cfg->groups < 32) 2025 groups = 32; 2026 else 2027 groups = cfg->groups; 2028 2029 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); 2030 if (!listeners) 2031 goto out_sock_release; 2032 2033 sk->sk_data_ready = netlink_data_ready; 2034 if (cfg && cfg->input) 2035 nlk_sk(sk)->netlink_rcv = cfg->input; 2036 2037 if (netlink_insert(sk, 0)) 2038 goto out_sock_release; 2039 2040 nlk = nlk_sk(sk); 2041 set_bit(NETLINK_F_KERNEL_SOCKET, &nlk->flags); 2042 2043 netlink_table_grab(); 2044 if (!nl_table[unit].registered) { 2045 nl_table[unit].groups = groups; 2046 rcu_assign_pointer(nl_table[unit].listeners, listeners); 2047 nl_table[unit].module = module; 2048 if (cfg) { 2049 nl_table[unit].bind = cfg->bind; 2050 nl_table[unit].unbind = cfg->unbind; 2051 nl_table[unit].release = cfg->release; 2052 nl_table[unit].flags = cfg->flags; 2053 } 2054 nl_table[unit].registered = 1; 2055 } else { 2056 kfree(listeners); 2057 nl_table[unit].registered++; 2058 } 2059 netlink_table_ungrab(); 2060 return sk; 2061 2062 out_sock_release: 2063 kfree(listeners); 2064 netlink_kernel_release(sk); 2065 return NULL; 2066 2067 out_sock_release_nosk: 2068 sock_release(sock); 2069 return NULL; 2070 } 2071 EXPORT_SYMBOL(__netlink_kernel_create); 2072 2073 void 2074 netlink_kernel_release(struct sock *sk) 2075 { 2076 if (sk == NULL || sk->sk_socket == NULL) 2077 return; 2078 2079 sock_release(sk->sk_socket); 2080 } 2081 EXPORT_SYMBOL(netlink_kernel_release); 2082 2083 int __netlink_change_ngroups(struct sock *sk, unsigned int groups) 2084 { 2085 struct listeners *new, *old; 2086 struct netlink_table *tbl = &nl_table[sk->sk_protocol]; 2087 2088 if (groups < 32) 2089 groups = 32; 2090 2091 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { 2092 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); 2093 if (!new) 2094 return -ENOMEM; 2095 old = nl_deref_protected(tbl->listeners); 2096 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); 2097 rcu_assign_pointer(tbl->listeners, new); 2098 2099 kfree_rcu(old, rcu); 2100 } 2101 tbl->groups = groups; 2102 2103 return 0; 2104 } 2105 2106 /** 2107 * netlink_change_ngroups - change number of multicast groups 2108 * 2109 * This changes the number of multicast groups that are available 2110 * on a certain netlink family. Note that it is not possible to 2111 * change the number of groups to below 32. Also note that it does 2112 * not implicitly clear listeners from groups that are removed when 2113 * the number of groups is reduced. 2114 * 2115 * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). 2116 * @groups: The new number of groups. 2117 */ 2118 int netlink_change_ngroups(struct sock *sk, unsigned int groups) 2119 { 2120 int err; 2121 2122 netlink_table_grab(); 2123 err = __netlink_change_ngroups(sk, groups); 2124 netlink_table_ungrab(); 2125 2126 return err; 2127 } 2128 2129 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) 2130 { 2131 struct sock *sk; 2132 struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; 2133 struct hlist_node *tmp; 2134 2135 sk_for_each_bound_safe(sk, tmp, &tbl->mc_list) 2136 netlink_update_socket_mc(nlk_sk(sk), group, 0); 2137 } 2138 2139 struct nlmsghdr * 2140 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags) 2141 { 2142 struct nlmsghdr *nlh; 2143 int size = nlmsg_msg_size(len); 2144 2145 nlh = skb_put(skb, NLMSG_ALIGN(size)); 2146 nlh->nlmsg_type = type; 2147 nlh->nlmsg_len = size; 2148 nlh->nlmsg_flags = flags; 2149 nlh->nlmsg_pid = portid; 2150 nlh->nlmsg_seq = seq; 2151 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) 2152 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size); 2153 return nlh; 2154 } 2155 EXPORT_SYMBOL(__nlmsg_put); 2156 2157 static size_t 2158 netlink_ack_tlv_len(struct netlink_sock *nlk, int err, 2159 const struct netlink_ext_ack *extack) 2160 { 2161 size_t tlvlen; 2162 2163 if (!extack || !test_bit(NETLINK_F_EXT_ACK, &nlk->flags)) 2164 return 0; 2165 2166 tlvlen = 0; 2167 if (extack->_msg) 2168 tlvlen += nla_total_size(strlen(extack->_msg) + 1); 2169 if (extack->cookie_len) 2170 tlvlen += nla_total_size(extack->cookie_len); 2171 2172 /* Following attributes are only reported as error (not warning) */ 2173 if (!err) 2174 return tlvlen; 2175 2176 if (extack->bad_attr) 2177 tlvlen += nla_total_size(sizeof(u32)); 2178 if (extack->policy) 2179 tlvlen += netlink_policy_dump_attr_size_estimate(extack->policy); 2180 if (extack->miss_type) 2181 tlvlen += nla_total_size(sizeof(u32)); 2182 if (extack->miss_nest) 2183 tlvlen += nla_total_size(sizeof(u32)); 2184 2185 return tlvlen; 2186 } 2187 2188 static bool nlmsg_check_in_payload(const struct nlmsghdr *nlh, const void *addr) 2189 { 2190 return !WARN_ON(addr < nlmsg_data(nlh) || 2191 addr - (const void *) nlh >= nlh->nlmsg_len); 2192 } 2193 2194 static void 2195 netlink_ack_tlv_fill(struct sk_buff *skb, const struct nlmsghdr *nlh, int err, 2196 const struct netlink_ext_ack *extack) 2197 { 2198 if (extack->_msg) 2199 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG, extack->_msg)); 2200 if (extack->cookie_len) 2201 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE, 2202 extack->cookie_len, extack->cookie)); 2203 2204 if (!err) 2205 return; 2206 2207 if (extack->bad_attr && nlmsg_check_in_payload(nlh, extack->bad_attr)) 2208 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, 2209 (u8 *)extack->bad_attr - (const u8 *)nlh)); 2210 if (extack->policy) 2211 netlink_policy_dump_write_attr(skb, extack->policy, 2212 NLMSGERR_ATTR_POLICY); 2213 if (extack->miss_type) 2214 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_TYPE, 2215 extack->miss_type)); 2216 if (extack->miss_nest && nlmsg_check_in_payload(nlh, extack->miss_nest)) 2217 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_NEST, 2218 (u8 *)extack->miss_nest - (const u8 *)nlh)); 2219 } 2220 2221 /* 2222 * It looks a bit ugly. 2223 * It would be better to create kernel thread. 2224 */ 2225 2226 static int netlink_dump_done(struct netlink_sock *nlk, struct sk_buff *skb, 2227 struct netlink_callback *cb, 2228 struct netlink_ext_ack *extack) 2229 { 2230 struct nlmsghdr *nlh; 2231 size_t extack_len; 2232 2233 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(nlk->dump_done_errno), 2234 NLM_F_MULTI | cb->answer_flags); 2235 if (WARN_ON(!nlh)) 2236 return -ENOBUFS; 2237 2238 nl_dump_check_consistent(cb, nlh); 2239 memcpy(nlmsg_data(nlh), &nlk->dump_done_errno, sizeof(nlk->dump_done_errno)); 2240 2241 extack_len = netlink_ack_tlv_len(nlk, nlk->dump_done_errno, extack); 2242 if (extack_len) { 2243 nlh->nlmsg_flags |= NLM_F_ACK_TLVS; 2244 if (skb_tailroom(skb) >= extack_len) { 2245 netlink_ack_tlv_fill(skb, cb->nlh, 2246 nlk->dump_done_errno, extack); 2247 nlmsg_end(skb, nlh); 2248 } 2249 } 2250 2251 return 0; 2252 } 2253 2254 static int netlink_dump(struct sock *sk, bool lock_taken) 2255 { 2256 struct netlink_sock *nlk = nlk_sk(sk); 2257 struct netlink_ext_ack extack = {}; 2258 struct netlink_callback *cb; 2259 struct sk_buff *skb = NULL; 2260 unsigned int rmem, rcvbuf; 2261 size_t max_recvmsg_len; 2262 struct module *module; 2263 int err = -ENOBUFS; 2264 int alloc_min_size; 2265 int alloc_size; 2266 2267 if (!lock_taken) 2268 mutex_lock(&nlk->nl_cb_mutex); 2269 if (!nlk->cb_running) { 2270 err = -EINVAL; 2271 goto errout_skb; 2272 } 2273 2274 /* NLMSG_GOODSIZE is small to avoid high order allocations being 2275 * required, but it makes sense to _attempt_ a 32KiB allocation 2276 * to reduce number of system calls on dump operations, if user 2277 * ever provided a big enough buffer. 2278 */ 2279 cb = &nlk->cb; 2280 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); 2281 2282 max_recvmsg_len = READ_ONCE(nlk->max_recvmsg_len); 2283 if (alloc_min_size < max_recvmsg_len) { 2284 alloc_size = max_recvmsg_len; 2285 skb = alloc_skb(alloc_size, 2286 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) | 2287 __GFP_NOWARN | __GFP_NORETRY); 2288 } 2289 if (!skb) { 2290 alloc_size = alloc_min_size; 2291 skb = alloc_skb(alloc_size, GFP_KERNEL); 2292 } 2293 if (!skb) 2294 goto errout_skb; 2295 2296 rcvbuf = READ_ONCE(sk->sk_rcvbuf); 2297 rmem = atomic_add_return(skb->truesize, &sk->sk_rmem_alloc); 2298 if (rmem != skb->truesize && rmem >= rcvbuf) { 2299 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 2300 goto errout_skb; 2301 } 2302 2303 /* Trim skb to allocated size. User is expected to provide buffer as 2304 * large as max(min_dump_alloc, 32KiB (max_recvmsg_len capped at 2305 * netlink_recvmsg())). dump will pack as many smaller messages as 2306 * could fit within the allocated skb. skb is typically allocated 2307 * with larger space than required (could be as much as near 2x the 2308 * requested size with align to next power of 2 approach). Allowing 2309 * dump to use the excess space makes it difficult for a user to have a 2310 * reasonable static buffer based on the expected largest dump of a 2311 * single netdev. The outcome is MSG_TRUNC error. 2312 */ 2313 skb_reserve(skb, skb_tailroom(skb) - alloc_size); 2314 2315 /* Make sure malicious BPF programs can not read unitialized memory 2316 * from skb->head -> skb->data 2317 */ 2318 skb_reset_network_header(skb); 2319 skb_reset_mac_header(skb); 2320 2321 netlink_skb_set_owner_r(skb, sk); 2322 2323 if (nlk->dump_done_errno > 0) { 2324 cb->extack = &extack; 2325 2326 nlk->dump_done_errno = cb->dump(skb, cb); 2327 2328 /* EMSGSIZE plus something already in the skb means 2329 * that there's more to dump but current skb has filled up. 2330 * If the callback really wants to return EMSGSIZE to user space 2331 * it needs to do so again, on the next cb->dump() call, 2332 * without putting data in the skb. 2333 */ 2334 if (nlk->dump_done_errno == -EMSGSIZE && skb->len) 2335 nlk->dump_done_errno = skb->len; 2336 2337 cb->extack = NULL; 2338 } 2339 2340 if (nlk->dump_done_errno > 0 || 2341 skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) { 2342 mutex_unlock(&nlk->nl_cb_mutex); 2343 2344 if (sk_filter(sk, skb)) 2345 kfree_skb(skb); 2346 else 2347 __netlink_sendskb(sk, skb); 2348 return 0; 2349 } 2350 2351 if (netlink_dump_done(nlk, skb, cb, &extack)) 2352 goto errout_skb; 2353 2354 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES 2355 /* frag_list skb's data is used for compat tasks 2356 * and the regular skb's data for normal (non-compat) tasks. 2357 * See netlink_recvmsg(). 2358 */ 2359 if (unlikely(skb_shinfo(skb)->frag_list)) { 2360 if (netlink_dump_done(nlk, skb_shinfo(skb)->frag_list, cb, &extack)) 2361 goto errout_skb; 2362 } 2363 #endif 2364 2365 if (sk_filter(sk, skb)) 2366 kfree_skb(skb); 2367 else 2368 __netlink_sendskb(sk, skb); 2369 2370 if (cb->done) 2371 cb->done(cb); 2372 2373 WRITE_ONCE(nlk->cb_running, false); 2374 module = cb->module; 2375 skb = cb->skb; 2376 mutex_unlock(&nlk->nl_cb_mutex); 2377 module_put(module); 2378 consume_skb(skb); 2379 return 0; 2380 2381 errout_skb: 2382 mutex_unlock(&nlk->nl_cb_mutex); 2383 kfree_skb(skb); 2384 return err; 2385 } 2386 2387 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, 2388 const struct nlmsghdr *nlh, 2389 struct netlink_dump_control *control) 2390 { 2391 struct netlink_callback *cb; 2392 struct netlink_sock *nlk; 2393 struct sock *sk; 2394 int ret; 2395 2396 refcount_inc(&skb->users); 2397 2398 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid); 2399 if (sk == NULL) { 2400 ret = -ECONNREFUSED; 2401 goto error_free; 2402 } 2403 2404 nlk = nlk_sk(sk); 2405 mutex_lock(&nlk->nl_cb_mutex); 2406 /* A dump is in progress... */ 2407 if (nlk->cb_running) { 2408 ret = -EBUSY; 2409 goto error_unlock; 2410 } 2411 /* add reference of module which cb->dump belongs to */ 2412 if (!try_module_get(control->module)) { 2413 ret = -EPROTONOSUPPORT; 2414 goto error_unlock; 2415 } 2416 2417 cb = &nlk->cb; 2418 memset(cb, 0, sizeof(*cb)); 2419 cb->dump = control->dump; 2420 cb->done = control->done; 2421 cb->nlh = nlh; 2422 cb->data = control->data; 2423 cb->module = control->module; 2424 cb->min_dump_alloc = control->min_dump_alloc; 2425 cb->flags = control->flags; 2426 cb->skb = skb; 2427 2428 cb->strict_check = nlk_test_bit(STRICT_CHK, NETLINK_CB(skb).sk); 2429 2430 if (control->start) { 2431 cb->extack = control->extack; 2432 ret = control->start(cb); 2433 cb->extack = NULL; 2434 if (ret) 2435 goto error_put; 2436 } 2437 2438 WRITE_ONCE(nlk->cb_running, true); 2439 nlk->dump_done_errno = INT_MAX; 2440 2441 ret = netlink_dump(sk, true); 2442 2443 sock_put(sk); 2444 2445 if (ret) 2446 return ret; 2447 2448 /* We successfully started a dump, by returning -EINTR we 2449 * signal not to send ACK even if it was requested. 2450 */ 2451 return -EINTR; 2452 2453 error_put: 2454 module_put(control->module); 2455 error_unlock: 2456 sock_put(sk); 2457 mutex_unlock(&nlk->nl_cb_mutex); 2458 error_free: 2459 kfree_skb(skb); 2460 return ret; 2461 } 2462 EXPORT_SYMBOL(__netlink_dump_start); 2463 2464 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err, 2465 const struct netlink_ext_ack *extack) 2466 { 2467 struct sk_buff *skb; 2468 struct nlmsghdr *rep; 2469 struct nlmsgerr *errmsg; 2470 size_t payload = sizeof(*errmsg); 2471 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk); 2472 unsigned int flags = 0; 2473 size_t tlvlen; 2474 2475 /* Error messages get the original request appended, unless the user 2476 * requests to cap the error message, and get extra error data if 2477 * requested. 2478 */ 2479 if (err && !test_bit(NETLINK_F_CAP_ACK, &nlk->flags)) 2480 payload += nlmsg_len(nlh); 2481 else 2482 flags |= NLM_F_CAPPED; 2483 2484 tlvlen = netlink_ack_tlv_len(nlk, err, extack); 2485 if (tlvlen) 2486 flags |= NLM_F_ACK_TLVS; 2487 2488 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL); 2489 if (!skb) 2490 goto err_skb; 2491 2492 rep = nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 2493 NLMSG_ERROR, sizeof(*errmsg), flags); 2494 if (!rep) 2495 goto err_bad_put; 2496 errmsg = nlmsg_data(rep); 2497 errmsg->error = err; 2498 errmsg->msg = *nlh; 2499 2500 if (!(flags & NLM_F_CAPPED)) { 2501 if (!nlmsg_append(skb, nlmsg_len(nlh))) 2502 goto err_bad_put; 2503 2504 memcpy(nlmsg_data(&errmsg->msg), nlmsg_data(nlh), 2505 nlmsg_len(nlh)); 2506 } 2507 2508 if (tlvlen) 2509 netlink_ack_tlv_fill(skb, nlh, err, extack); 2510 2511 nlmsg_end(skb, rep); 2512 2513 nlmsg_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid); 2514 2515 return; 2516 2517 err_bad_put: 2518 nlmsg_free(skb); 2519 err_skb: 2520 WRITE_ONCE(NETLINK_CB(in_skb).sk->sk_err, ENOBUFS); 2521 sk_error_report(NETLINK_CB(in_skb).sk); 2522 } 2523 EXPORT_SYMBOL(netlink_ack); 2524 2525 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, 2526 struct nlmsghdr *, 2527 struct netlink_ext_ack *)) 2528 { 2529 struct netlink_ext_ack extack; 2530 struct nlmsghdr *nlh; 2531 int err; 2532 2533 while (skb->len >= nlmsg_total_size(0)) { 2534 int msglen; 2535 2536 memset(&extack, 0, sizeof(extack)); 2537 nlh = nlmsg_hdr(skb); 2538 err = 0; 2539 2540 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) 2541 return 0; 2542 2543 /* Only requests are handled by the kernel */ 2544 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) 2545 goto ack; 2546 2547 /* Skip control messages */ 2548 if (nlh->nlmsg_type < NLMSG_MIN_TYPE) 2549 goto ack; 2550 2551 err = cb(skb, nlh, &extack); 2552 if (err == -EINTR) 2553 goto skip; 2554 2555 ack: 2556 if (nlh->nlmsg_flags & NLM_F_ACK || err) 2557 netlink_ack(skb, nlh, err, &extack); 2558 2559 skip: 2560 msglen = NLMSG_ALIGN(nlh->nlmsg_len); 2561 if (msglen > skb->len) 2562 msglen = skb->len; 2563 skb_pull(skb, msglen); 2564 } 2565 2566 return 0; 2567 } 2568 EXPORT_SYMBOL(netlink_rcv_skb); 2569 2570 /** 2571 * nlmsg_notify - send a notification netlink message 2572 * @sk: netlink socket to use 2573 * @skb: notification message 2574 * @portid: destination netlink portid for reports or 0 2575 * @group: destination multicast group or 0 2576 * @report: 1 to report back, 0 to disable 2577 * @flags: allocation flags 2578 */ 2579 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 2580 unsigned int group, int report, gfp_t flags) 2581 { 2582 int err = 0; 2583 2584 if (group) { 2585 int exclude_portid = 0; 2586 2587 if (report) { 2588 refcount_inc(&skb->users); 2589 exclude_portid = portid; 2590 } 2591 2592 /* errors reported via destination sk->sk_err, but propagate 2593 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ 2594 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); 2595 if (err == -ESRCH) 2596 err = 0; 2597 } 2598 2599 if (report) { 2600 int err2; 2601 2602 err2 = nlmsg_unicast(sk, skb, portid); 2603 if (!err) 2604 err = err2; 2605 } 2606 2607 return err; 2608 } 2609 EXPORT_SYMBOL(nlmsg_notify); 2610 2611 #ifdef CONFIG_PROC_FS 2612 struct nl_seq_iter { 2613 struct seq_net_private p; 2614 struct rhashtable_iter hti; 2615 int link; 2616 }; 2617 2618 static void netlink_walk_start(struct nl_seq_iter *iter) 2619 { 2620 rhashtable_walk_enter(&nl_table[iter->link].hash, &iter->hti); 2621 rhashtable_walk_start(&iter->hti); 2622 } 2623 2624 static void netlink_walk_stop(struct nl_seq_iter *iter) 2625 { 2626 rhashtable_walk_stop(&iter->hti); 2627 rhashtable_walk_exit(&iter->hti); 2628 } 2629 2630 static void *__netlink_seq_next(struct seq_file *seq) 2631 { 2632 struct nl_seq_iter *iter = seq->private; 2633 struct netlink_sock *nlk; 2634 2635 do { 2636 for (;;) { 2637 nlk = rhashtable_walk_next(&iter->hti); 2638 2639 if (IS_ERR(nlk)) { 2640 if (PTR_ERR(nlk) == -EAGAIN) 2641 continue; 2642 2643 return nlk; 2644 } 2645 2646 if (nlk) 2647 break; 2648 2649 netlink_walk_stop(iter); 2650 if (++iter->link >= MAX_LINKS) 2651 return NULL; 2652 2653 netlink_walk_start(iter); 2654 } 2655 } while (sock_net(&nlk->sk) != seq_file_net(seq)); 2656 2657 return nlk; 2658 } 2659 2660 static void *netlink_seq_start(struct seq_file *seq, loff_t *posp) 2661 __acquires(RCU) 2662 { 2663 struct nl_seq_iter *iter = seq->private; 2664 void *obj = SEQ_START_TOKEN; 2665 loff_t pos; 2666 2667 iter->link = 0; 2668 2669 netlink_walk_start(iter); 2670 2671 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--) 2672 obj = __netlink_seq_next(seq); 2673 2674 return obj; 2675 } 2676 2677 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2678 { 2679 ++*pos; 2680 return __netlink_seq_next(seq); 2681 } 2682 2683 static void netlink_native_seq_stop(struct seq_file *seq, void *v) 2684 { 2685 struct nl_seq_iter *iter = seq->private; 2686 2687 if (iter->link >= MAX_LINKS) 2688 return; 2689 2690 netlink_walk_stop(iter); 2691 } 2692 2693 2694 static int netlink_native_seq_show(struct seq_file *seq, void *v) 2695 { 2696 if (v == SEQ_START_TOKEN) { 2697 seq_puts(seq, 2698 "sk Eth Pid Groups " 2699 "Rmem Wmem Dump Locks Drops Inode\n"); 2700 } else { 2701 struct sock *s = v; 2702 struct netlink_sock *nlk = nlk_sk(s); 2703 2704 seq_printf(seq, "%pK %-3d %-10u %08x %-8d %-8d %-5d %-8d %-8u %-8llu\n", 2705 s, 2706 s->sk_protocol, 2707 nlk->portid, 2708 nlk->groups ? (u32)nlk->groups[0] : 0, 2709 sk_rmem_alloc_get(s), 2710 sk_wmem_alloc_get(s), 2711 READ_ONCE(nlk->cb_running), 2712 refcount_read(&s->sk_refcnt), 2713 sk_drops_read(s), 2714 sock_i_ino(s) 2715 ); 2716 2717 } 2718 return 0; 2719 } 2720 2721 #ifdef CONFIG_BPF_SYSCALL 2722 struct bpf_iter__netlink { 2723 __bpf_md_ptr(struct bpf_iter_meta *, meta); 2724 __bpf_md_ptr(struct netlink_sock *, sk); 2725 }; 2726 2727 DEFINE_BPF_ITER_FUNC(netlink, struct bpf_iter_meta *meta, struct netlink_sock *sk) 2728 2729 static int netlink_prog_seq_show(struct bpf_prog *prog, 2730 struct bpf_iter_meta *meta, 2731 void *v) 2732 { 2733 struct bpf_iter__netlink ctx; 2734 2735 meta->seq_num--; /* skip SEQ_START_TOKEN */ 2736 ctx.meta = meta; 2737 ctx.sk = nlk_sk((struct sock *)v); 2738 return bpf_iter_run_prog(prog, &ctx); 2739 } 2740 2741 static int netlink_seq_show(struct seq_file *seq, void *v) 2742 { 2743 struct bpf_iter_meta meta; 2744 struct bpf_prog *prog; 2745 2746 meta.seq = seq; 2747 prog = bpf_iter_get_info(&meta, false); 2748 if (!prog) 2749 return netlink_native_seq_show(seq, v); 2750 2751 if (v != SEQ_START_TOKEN) 2752 return netlink_prog_seq_show(prog, &meta, v); 2753 2754 return 0; 2755 } 2756 2757 static void netlink_seq_stop(struct seq_file *seq, void *v) 2758 { 2759 struct bpf_iter_meta meta; 2760 struct bpf_prog *prog; 2761 2762 if (!v) { 2763 meta.seq = seq; 2764 prog = bpf_iter_get_info(&meta, true); 2765 if (prog) 2766 (void)netlink_prog_seq_show(prog, &meta, v); 2767 } 2768 2769 netlink_native_seq_stop(seq, v); 2770 } 2771 #else 2772 static int netlink_seq_show(struct seq_file *seq, void *v) 2773 { 2774 return netlink_native_seq_show(seq, v); 2775 } 2776 2777 static void netlink_seq_stop(struct seq_file *seq, void *v) 2778 { 2779 netlink_native_seq_stop(seq, v); 2780 } 2781 #endif 2782 2783 static const struct seq_operations netlink_seq_ops = { 2784 .start = netlink_seq_start, 2785 .next = netlink_seq_next, 2786 .stop = netlink_seq_stop, 2787 .show = netlink_seq_show, 2788 }; 2789 #endif 2790 2791 int netlink_register_notifier(struct notifier_block *nb) 2792 { 2793 return blocking_notifier_chain_register(&netlink_chain, nb); 2794 } 2795 EXPORT_SYMBOL(netlink_register_notifier); 2796 2797 int netlink_unregister_notifier(struct notifier_block *nb) 2798 { 2799 return blocking_notifier_chain_unregister(&netlink_chain, nb); 2800 } 2801 EXPORT_SYMBOL(netlink_unregister_notifier); 2802 2803 static const struct proto_ops netlink_ops = { 2804 .family = PF_NETLINK, 2805 .owner = THIS_MODULE, 2806 .release = netlink_release, 2807 .bind = netlink_bind, 2808 .connect = netlink_connect, 2809 .socketpair = sock_no_socketpair, 2810 .accept = sock_no_accept, 2811 .getname = netlink_getname, 2812 .poll = datagram_poll, 2813 .ioctl = netlink_ioctl, 2814 .listen = sock_no_listen, 2815 .shutdown = sock_no_shutdown, 2816 .setsockopt = netlink_setsockopt, 2817 .getsockopt_iter = netlink_getsockopt, 2818 .sendmsg = netlink_sendmsg, 2819 .recvmsg = netlink_recvmsg, 2820 .mmap = sock_no_mmap, 2821 }; 2822 2823 static const struct net_proto_family netlink_family_ops = { 2824 .family = PF_NETLINK, 2825 .create = netlink_create, 2826 .owner = THIS_MODULE, /* for consistency 8) */ 2827 }; 2828 2829 static int __net_init netlink_net_init(struct net *net) 2830 { 2831 #ifdef CONFIG_PROC_FS 2832 if (!proc_create_net("netlink", 0, net->proc_net, &netlink_seq_ops, 2833 sizeof(struct nl_seq_iter))) 2834 return -ENOMEM; 2835 #endif 2836 return 0; 2837 } 2838 2839 static void __net_exit netlink_net_exit(struct net *net) 2840 { 2841 #ifdef CONFIG_PROC_FS 2842 remove_proc_entry("netlink", net->proc_net); 2843 #endif 2844 } 2845 2846 static void __init netlink_add_usersock_entry(void) 2847 { 2848 struct listeners *listeners; 2849 int groups = 32; 2850 2851 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); 2852 if (!listeners) 2853 panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); 2854 2855 netlink_table_grab(); 2856 2857 nl_table[NETLINK_USERSOCK].groups = groups; 2858 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); 2859 nl_table[NETLINK_USERSOCK].module = THIS_MODULE; 2860 nl_table[NETLINK_USERSOCK].registered = 1; 2861 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND; 2862 2863 netlink_table_ungrab(); 2864 } 2865 2866 static struct pernet_operations __net_initdata netlink_net_ops = { 2867 .init = netlink_net_init, 2868 .exit = netlink_net_exit, 2869 }; 2870 2871 static inline u32 netlink_hash(const void *data, u32 len, u32 seed) 2872 { 2873 const struct netlink_sock *nlk = data; 2874 struct netlink_compare_arg arg; 2875 2876 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid); 2877 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed); 2878 } 2879 2880 static const struct rhashtable_params netlink_rhashtable_params = { 2881 .head_offset = offsetof(struct netlink_sock, node), 2882 .key_len = netlink_compare_arg_len, 2883 .obj_hashfn = netlink_hash, 2884 .obj_cmpfn = netlink_compare, 2885 .automatic_shrinking = true, 2886 }; 2887 2888 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 2889 BTF_ID_LIST_SINGLE(btf_netlink_sock_id, struct, netlink_sock) 2890 2891 static const struct bpf_iter_seq_info netlink_seq_info = { 2892 .seq_ops = &netlink_seq_ops, 2893 .init_seq_private = bpf_iter_init_seq_net, 2894 .fini_seq_private = bpf_iter_fini_seq_net, 2895 .seq_priv_size = sizeof(struct nl_seq_iter), 2896 }; 2897 2898 static struct bpf_iter_reg netlink_reg_info = { 2899 .target = "netlink", 2900 .ctx_arg_info_size = 1, 2901 .ctx_arg_info = { 2902 { offsetof(struct bpf_iter__netlink, sk), 2903 PTR_TO_BTF_ID_OR_NULL }, 2904 }, 2905 .seq_info = &netlink_seq_info, 2906 }; 2907 2908 static int __init bpf_iter_register(void) 2909 { 2910 netlink_reg_info.ctx_arg_info[0].btf_id = *btf_netlink_sock_id; 2911 return bpf_iter_reg_target(&netlink_reg_info); 2912 } 2913 #endif 2914 2915 static int __init netlink_proto_init(void) 2916 { 2917 int i; 2918 int err = proto_register(&netlink_proto, 0); 2919 2920 if (err != 0) 2921 goto out; 2922 2923 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 2924 err = bpf_iter_register(); 2925 if (err) 2926 goto out; 2927 #endif 2928 2929 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof_field(struct sk_buff, cb)); 2930 2931 nl_table = kzalloc_objs(*nl_table, MAX_LINKS); 2932 if (!nl_table) 2933 goto panic; 2934 2935 for (i = 0; i < MAX_LINKS; i++) { 2936 if (rhashtable_init(&nl_table[i].hash, 2937 &netlink_rhashtable_params) < 0) 2938 goto panic; 2939 } 2940 2941 netlink_add_usersock_entry(); 2942 2943 sock_register(&netlink_family_ops); 2944 register_pernet_subsys(&netlink_net_ops); 2945 register_pernet_subsys(&netlink_tap_net_ops); 2946 /* The netlink device handler may be needed early. */ 2947 rtnetlink_init(); 2948 out: 2949 return err; 2950 panic: 2951 panic("netlink_init: Cannot allocate nl_table\n"); 2952 } 2953 2954 core_initcall(netlink_proto_init); 2955