1 /* $NetBSD: kern_sysctl.c,v 1.273 2026/02/28 15:46:17 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Brown. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Mike Karels at Berkeley Software Design, Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95 64 */ 65 66 /* 67 * sysctl system call. 68 */ 69 70 #define __COMPAT_SYSCTL 71 72 #include <sys/cdefs.h> 73 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.273 2026/02/28 15:46:17 christos Exp $"); 74 75 #ifdef _KERNEL_OPT 76 #include "opt_defcorename.h" 77 #endif 78 79 #include "ksyms.h" 80 81 #include <sys/param.h> 82 #include <sys/types.h> 83 84 #include <sys/buf.h> 85 #include <sys/cprng.h> 86 #include <sys/kauth.h> 87 #include <sys/ksyms.h> 88 #include <sys/ktrace.h> 89 #include <sys/malloc.h> 90 #include <sys/mount.h> 91 #include <sys/once.h> 92 #include <sys/rndsource.h> 93 #include <sys/sdt.h> 94 #include <sys/syscallargs.h> 95 #include <sys/sysctl.h> 96 #include <sys/systm.h> 97 98 #include <crypto/blake2/blake2s.h> 99 100 #define MAXDESCLEN 1024 101 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures"); 102 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data"); 103 104 static int sysctl_mmap(SYSCTLFN_PROTO); 105 static int sysctl_alloc(struct sysctlnode *, int); 106 static int sysctl_realloc(struct sysctlnode *); 107 108 static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t, 109 struct sysctlnode *); 110 static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *, 111 void *, size_t, size_t *); 112 113 static int sysctl_log_add(struct sysctllog **, const struct sysctlnode *); 114 static int sysctl_log_realloc(struct sysctllog *); 115 116 typedef void sysctl_setup_func(struct sysctllog **); 117 118 #ifdef SYSCTL_DEBUG 119 #define DPRINTF(a) printf a 120 #else 121 #define DPRINTF(a) 122 #endif 123 124 struct sysctllog { 125 const struct sysctlnode *log_root; 126 int *log_num; 127 int log_size, log_left; 128 }; 129 130 /* 131 * the "root" of the new sysctl tree 132 */ 133 struct sysctlnode sysctl_root = { 134 .sysctl_flags = SYSCTL_VERSION| 135 CTLFLAG_ROOT|CTLFLAG_READWRITE| 136 CTLTYPE_NODE, 137 .sysctl_num = 0, 138 .sysctl_size = sizeof(struct sysctlnode), 139 .sysctl_name = "(root)", 140 }; 141 142 /* 143 * link set of functions that add nodes at boot time (see also 144 * sysctl_buildtree()) 145 */ 146 __link_set_decl(sysctl_funcs, sysctl_setup_func); 147 148 /* 149 * The `sysctl_treelock' is intended to serialize access to the sysctl 150 * tree. XXX This has serious problems; allocating memory and 151 * copying data out with the lock held is insane. 152 */ 153 krwlock_t sysctl_treelock; 154 155 kmutex_t sysctl_file_marker_lock; 156 157 /* 158 * Attributes stored in the kernel. 159 */ 160 char hostname[MAXHOSTNAMELEN]; 161 int hostnamelen; 162 163 char domainname[MAXHOSTNAMELEN]; 164 int domainnamelen; 165 166 long hostid; 167 168 #ifndef DEFCORENAME 169 #define DEFCORENAME "%n.core" 170 #endif 171 char defcorename[MAXPATHLEN] = DEFCORENAME; 172 173 /* 174 * ******************************************************************** 175 * Section 0: Some simple glue 176 * ******************************************************************** 177 * By wrapping copyin(), copyout(), and copyinstr() like this, we can 178 * stop caring about who's calling us and simplify some code a bunch. 179 * ******************************************************************** 180 */ 181 int 182 sysctl_copyin(struct lwp *l, const void *uaddr, void *kaddr, size_t len) 183 { 184 int error; 185 186 if (l != NULL) { 187 error = copyin(uaddr, kaddr, len); 188 ktrmibio(-1, UIO_WRITE, uaddr, len, error); 189 } else { 190 error = kcopy(uaddr, kaddr, len); 191 } 192 193 return error; 194 } 195 196 int 197 sysctl_copyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len) 198 { 199 int error; 200 201 if (l != NULL) { 202 error = copyout(kaddr, uaddr, len); 203 ktrmibio(-1, UIO_READ, uaddr, len, error); 204 } else { 205 error = kcopy(kaddr, uaddr, len); 206 } 207 208 return error; 209 } 210 211 int 212 sysctl_copyinstr(struct lwp *l, const void *uaddr, void *kaddr, 213 size_t len, size_t *done) 214 { 215 int error; 216 217 if (l != NULL) { 218 error = copyinstr(uaddr, kaddr, len, done); 219 ktrmibio(-1, UIO_WRITE, uaddr, len, error); 220 } else { 221 error = copystr(uaddr, kaddr, len, done); 222 } 223 224 return error; 225 } 226 227 /* 228 * ******************************************************************** 229 * Initialize sysctl subsystem. 230 * ******************************************************************** 231 */ 232 void 233 sysctl_init(void) 234 { 235 sysctl_setup_func *const *sysctl_setup; 236 237 rw_init(&sysctl_treelock); 238 239 /* 240 * dynamic mib numbers start here 241 */ 242 sysctl_root.sysctl_num = CREATE_BASE; 243 sysctl_basenode_init(); 244 245 __link_set_foreach(sysctl_setup, sysctl_funcs) { 246 (**sysctl_setup)(NULL); 247 } 248 249 mutex_init(&sysctl_file_marker_lock, MUTEX_DEFAULT, IPL_NONE); 250 } 251 252 /* 253 * Setting this means no more permanent nodes can be added, 254 * trees that claim to be readonly at the root now are, and if 255 * the main tree is readonly, *everything* is. 256 * 257 * Also starts up the PRNG used for the "random" sysctl: it's 258 * better to start it later than sooner. 259 * 260 * Call this at the end of kernel init. 261 */ 262 void 263 sysctl_finalize(void) 264 { 265 266 sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT; 267 } 268 269 /* 270 * ******************************************************************** 271 * The main native sysctl system call itself. 272 * ******************************************************************** 273 */ 274 int 275 sys___sysctl(struct lwp *l, const struct sys___sysctl_args *uap, register_t *retval) 276 { 277 /* { 278 syscallarg(const int *) name; 279 syscallarg(u_int) namelen; 280 syscallarg(void *) old; 281 syscallarg(size_t *) oldlenp; 282 syscallarg(const void *) new; 283 syscallarg(size_t) newlen; 284 } */ 285 int error, nerror, name[CTL_MAXNAME]; 286 size_t oldlen, savelen, *oldlenp; 287 288 /* 289 * get oldlen 290 */ 291 oldlen = 0; 292 oldlenp = SCARG(uap, oldlenp); 293 if (oldlenp != NULL) { 294 error = copyin(oldlenp, &oldlen, sizeof(oldlen)); 295 if (error) 296 return error; 297 } 298 savelen = oldlen; 299 300 /* 301 * top-level sysctl names may or may not be non-terminal, but 302 * we don't care 303 */ 304 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1) 305 return SET_ERROR(EINVAL); 306 error = copyin(SCARG(uap, name), &name, 307 SCARG(uap, namelen) * sizeof(int)); 308 if (error) 309 return error; 310 311 ktrmib(name, SCARG(uap, namelen)); 312 313 sysctl_lock(SCARG(uap, newv) != NULL); 314 315 /* 316 * do sysctl work (NULL means main built-in default tree) 317 */ 318 error = sysctl_dispatch(&name[0], SCARG(uap, namelen), 319 SCARG(uap, oldv), &oldlen, 320 SCARG(uap, newv), SCARG(uap, newlen), 321 &name[0], l, NULL); 322 323 /* 324 * release the sysctl lock 325 */ 326 sysctl_unlock(); 327 328 /* 329 * set caller's oldlen to new value even in the face of an 330 * error (if this gets an error and they didn't have one, they 331 * get this one) 332 */ 333 if (oldlenp) { 334 nerror = copyout(&oldlen, oldlenp, sizeof(oldlen)); 335 if (error == 0) 336 error = nerror; 337 } 338 339 /* 340 * if the only problem is that we weren't given enough space, 341 * that's an ENOMEM error 342 */ 343 if (error == 0 && SCARG(uap, oldv) != NULL && savelen < oldlen) 344 error = SET_ERROR(ENOMEM); 345 346 return error; 347 } 348 349 /* 350 * ******************************************************************** 351 * Section 1: How the tree is used 352 * ******************************************************************** 353 * Implementations of sysctl for emulations should typically need only 354 * these three functions in this order: lock the tree, dispatch 355 * request into it, unlock the tree. 356 * ******************************************************************** 357 */ 358 void 359 sysctl_lock(bool write) 360 { 361 362 if (write) { 363 rw_enter(&sysctl_treelock, RW_WRITER); 364 curlwp->l_pflag |= LP_SYSCTLWRITE; 365 } else { 366 rw_enter(&sysctl_treelock, RW_READER); 367 curlwp->l_pflag &= ~LP_SYSCTLWRITE; 368 } 369 } 370 371 void 372 sysctl_relock(void) 373 { 374 375 if ((curlwp->l_pflag & LP_SYSCTLWRITE) != 0) { 376 rw_enter(&sysctl_treelock, RW_WRITER); 377 } else { 378 rw_enter(&sysctl_treelock, RW_READER); 379 } 380 } 381 382 /* 383 * ******************************************************************** 384 * the main sysctl dispatch routine. scans the given tree and picks a 385 * function to call based on what it finds. 386 * ******************************************************************** 387 */ 388 int 389 sysctl_dispatch(SYSCTLFN_ARGS) 390 { 391 int error; 392 sysctlfn fn; 393 int ni; 394 395 KASSERT(rw_lock_held(&sysctl_treelock)); 396 397 if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 398 printf("sysctl_dispatch: rnode %p wrong version\n", rnode); 399 error = SET_ERROR(EINVAL); 400 goto out; 401 } 402 403 fn = NULL; 404 error = sysctl_locate(l, name, namelen, &rnode, &ni); 405 406 /* 407 * Enforce private-node read permissions on the final target even when 408 * dispatching through a custom sysctl handler rather than 409 * sysctl_lookup(). 410 */ 411 if (error == 0 && l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) 412 && (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 413 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) 414 goto out; 415 416 if (rnode->sysctl_func != NULL) { 417 /* 418 * the node we ended up at has a function, so call it. it can 419 * hand off to query or create if it wants to. 420 */ 421 fn = rnode->sysctl_func; 422 } else if (error == 0) { 423 /* 424 * we found the node they were looking for, so do a lookup. 425 */ 426 fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */ 427 } else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) { 428 /* 429 * prospective parent node found, but the terminal node was 430 * not. generic operations associate with the parent. 431 */ 432 switch (name[ni]) { 433 case CTL_QUERY: 434 fn = sysctl_query; 435 break; 436 case CTL_CREATE: 437 #if NKSYMS > 0 438 case CTL_CREATESYM: 439 #endif /* NKSYMS > 0 */ 440 if (newp == NULL) { 441 error = SET_ERROR(EINVAL); 442 break; 443 } 444 KASSERT(rw_write_held(&sysctl_treelock)); 445 fn = (sysctlfn)sysctl_create; /* we own the rnode */ 446 break; 447 case CTL_DESTROY: 448 if (newp == NULL) { 449 error = SET_ERROR(EINVAL); 450 break; 451 } 452 KASSERT(rw_write_held(&sysctl_treelock)); 453 fn = (sysctlfn)sysctl_destroy; /* we own the rnode */ 454 break; 455 case CTL_MMAP: 456 fn = (sysctlfn)sysctl_mmap; /* we own the rnode */ 457 break; 458 case CTL_DESCRIBE: 459 fn = sysctl_describe; 460 break; 461 default: 462 error = SET_ERROR(EOPNOTSUPP); 463 break; 464 } 465 } 466 467 /* 468 * after all of that, maybe we found someone who knows how to 469 * get us what we want? 470 */ 471 if (fn != NULL) 472 error = (*fn)(name + ni, namelen - ni, oldp, oldlenp, 473 newp, newlen, name, l, rnode); 474 else if (error == 0) 475 error = SET_ERROR(EOPNOTSUPP); 476 477 out: 478 return error; 479 } 480 481 /* 482 * ******************************************************************** 483 * Releases the tree lock. 484 * ******************************************************************** 485 */ 486 void 487 sysctl_unlock(void) 488 { 489 490 rw_exit(&sysctl_treelock); 491 } 492 493 /* 494 * ******************************************************************** 495 * Section 2: The main tree interfaces 496 * ******************************************************************** 497 * This is how sysctl_dispatch() does its work, and you can too, by 498 * calling these routines from helpers (though typically only 499 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these 500 * are called. 501 * ******************************************************************** 502 */ 503 504 /* 505 * sysctl_locate -- Finds the node matching the given mib under the 506 * given tree (via rv). If no tree is given, we fall back to the 507 * native tree. The current process (via l) is used for access 508 * control on the tree (some nodes may be traversable only by root) and 509 * on return, nip will show how many numbers in the mib were consumed. 510 */ 511 int 512 sysctl_locate(struct lwp *l, const int *name, u_int namelen, 513 const struct sysctlnode **rnode, int *nip) 514 { 515 const struct sysctlnode *node, *pnode; 516 int tn, si, ni, error, alias; 517 518 KASSERT(rw_lock_held(&sysctl_treelock)); 519 520 /* 521 * basic checks and setup 522 */ 523 if (*rnode == NULL) 524 *rnode = &sysctl_root; 525 if (nip) 526 *nip = 0; 527 if (namelen == 0) 528 return (0); 529 530 /* 531 * search starts from "root" 532 */ 533 pnode = *rnode; 534 if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) { 535 printf("sysctl_locate: pnode %p wrong version\n", pnode); 536 return SET_ERROR(EINVAL); 537 } 538 node = pnode->sysctl_child; 539 error = 0; 540 541 /* 542 * scan for node to which new node should be attached 543 */ 544 for (ni = 0; ni < namelen; ni++) { 545 /* 546 * walked off bottom of tree 547 */ 548 if (node == NULL) { 549 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE) 550 error = SET_ERROR(ENOENT); 551 else 552 error = SET_ERROR(ENOTDIR); 553 break; 554 } 555 /* 556 * can anyone traverse this node or only root? 557 */ 558 if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) && 559 (error = kauth_authorize_system(l->l_cred, 560 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_PRVT, 561 NULL, NULL, NULL)) != 0) 562 return error; 563 /* 564 * find a child node with the right number 565 */ 566 tn = name[ni]; 567 alias = 0; 568 569 si = 0; 570 /* 571 * Note: ANYNUMBER only matches positive integers. 572 * Since ANYNUMBER is only permitted on single-node 573 * sub-trees (eg proc), check before the loop and skip 574 * it if we can. 575 */ 576 if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0)) 577 goto foundit; 578 for (; si < pnode->sysctl_clen; si++) { 579 if (node[si].sysctl_num == tn) { 580 if (node[si].sysctl_flags & CTLFLAG_ALIAS) { 581 if (alias++ == 4) 582 break; 583 else { 584 tn = node[si].sysctl_alias; 585 si = -1; 586 } 587 } else 588 goto foundit; 589 } 590 } 591 /* 592 * if we ran off the end, it obviously doesn't exist 593 */ 594 error = SET_ERROR(ENOENT); 595 break; 596 597 /* 598 * so far so good, move on down the line 599 */ 600 foundit: 601 pnode = &node[si]; 602 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE) 603 node = node[si].sysctl_child; 604 else 605 node = NULL; 606 } 607 608 *rnode = pnode; 609 if (nip) 610 *nip = ni; 611 612 return error; 613 } 614 615 /* 616 * sysctl_query -- The auto-discovery engine. Copies out the structs 617 * describing nodes under the given node and handles overlay trees. 618 */ 619 int 620 sysctl_query(SYSCTLFN_ARGS) 621 { 622 int error, ni, elim, v; 623 size_t out, left, t; 624 const struct sysctlnode *enode, *onode; 625 struct sysctlnode qnode; 626 627 KASSERT(rw_lock_held(&sysctl_treelock)); 628 629 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 630 printf("sysctl_query: rnode %p wrong version\n", rnode); 631 return SET_ERROR(EINVAL); 632 } 633 634 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 635 return SET_ERROR(ENOTDIR); 636 if (namelen != 1 || name[0] != CTL_QUERY) 637 return SET_ERROR(EINVAL); 638 639 error = 0; 640 out = 0; 641 left = *oldlenp; 642 elim = 0; 643 enode = NULL; 644 645 /* 646 * translate the given request to a current node 647 */ 648 error = sysctl_cvt_in(l, &v, newp, newlen, &qnode); 649 if (error) 650 return error; 651 652 /* 653 * if the request specifies a version, check it 654 */ 655 if (qnode.sysctl_ver != 0) { 656 enode = rnode; 657 if (qnode.sysctl_ver != enode->sysctl_ver && 658 qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver) 659 return SET_ERROR(EINVAL); 660 } 661 662 /* 663 * process has overlay tree 664 */ 665 if (l && l->l_proc->p_emul->e_sysctlovly) { 666 enode = l->l_proc->p_emul->e_sysctlovly; 667 elim = (name - oname); 668 error = sysctl_locate(l, oname, elim, &enode, NULL); 669 if (error == 0) { 670 /* ah, found parent in overlay */ 671 elim = enode->sysctl_clen; 672 enode = enode->sysctl_child; 673 } else { 674 error = 0; 675 elim = 0; 676 enode = NULL; 677 } 678 } 679 680 for (ni = 0; ni < rnode->sysctl_clen; ni++) { 681 onode = &rnode->sysctl_child[ni]; 682 if (enode && enode->sysctl_num == onode->sysctl_num) { 683 if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE) 684 onode = enode; 685 if (--elim > 0) 686 enode++; 687 else 688 enode = NULL; 689 } 690 error = sysctl_cvt_out(l, v, onode, oldp, left, &t); 691 if (error) 692 return error; 693 if (oldp != NULL) 694 oldp = (char*)oldp + t; 695 out += t; 696 left -= MIN(left, t); 697 } 698 699 /* 700 * overlay trees *MUST* be entirely consumed 701 */ 702 KASSERT(enode == NULL); 703 704 *oldlenp = out; 705 706 return error; 707 } 708 709 /* 710 * sysctl_create -- Adds a node (the description of which is taken 711 * from newp) to the tree, returning a copy of it in the space pointed 712 * to by oldp. In the event that the requested slot is already taken 713 * (either by name or by number), the offending node is returned 714 * instead. Yes, this is complex, but we want to make sure everything 715 * is proper. 716 */ 717 #ifdef SYSCTL_DEBUG_CREATE 718 int _sysctl_create(SYSCTLFN_ARGS); 719 int 720 _sysctl_create(SYSCTLFN_ARGS) 721 #else 722 int 723 sysctl_create(SYSCTLFN_ARGS) 724 #endif 725 { 726 struct sysctlnode nnode, *node, *pnode; 727 int error, ni, at, nm, type, nsz, sz, flags, anum, v; 728 void *own; 729 730 KASSERT(rw_write_held(&sysctl_treelock)); 731 732 error = 0; 733 own = NULL; 734 anum = -1; 735 736 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 737 printf("sysctl_create: rnode %p wrong version\n", rnode); 738 return SET_ERROR(EINVAL); 739 } 740 741 if (namelen != 1 || (name[namelen - 1] != CTL_CREATE 742 #if NKSYMS > 0 743 && name[namelen - 1] != CTL_CREATESYM 744 #endif /* NKSYMS > 0 */ 745 )) 746 return SET_ERROR(EINVAL); 747 748 /* 749 * processes can only add nodes at securelevel 0, must be 750 * root, and can't add nodes to a parent that's not writeable 751 */ 752 if (l != NULL) { 753 #ifndef SYSCTL_DISALLOW_CREATE 754 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 755 KAUTH_REQ_SYSTEM_SYSCTL_ADD, NULL, NULL, NULL); 756 if (error) 757 return error; 758 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE)) 759 #endif /* SYSCTL_DISALLOW_CREATE */ 760 return SET_ERROR(EPERM); 761 } 762 763 /* 764 * nothing can add a node if: 765 * we've finished initial set up of this tree and 766 * (the tree itself is not writeable or 767 * the entire sysctl system is not writeable) 768 */ 769 if ((sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_PERMANENT) && 770 (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) || 771 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) 772 return SET_ERROR(EPERM); 773 774 /* 775 * it must be a "node", not a "int" or something 776 */ 777 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 778 return SET_ERROR(ENOTDIR); 779 if (rnode->sysctl_flags & CTLFLAG_ALIAS) { 780 printf("sysctl_create: attempt to add node to aliased " 781 "node %p\n", rnode); 782 return SET_ERROR(EINVAL); 783 } 784 pnode = __UNCONST(rnode); /* we are adding children to this node */ 785 786 if (newp == NULL) 787 return SET_ERROR(EINVAL); 788 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode); 789 if (error) 790 return error; 791 792 /* 793 * nodes passed in don't *have* parents 794 */ 795 if (nnode.sysctl_parent != NULL) 796 return SET_ERROR(EINVAL); 797 798 /* 799 * if we are indeed adding it, it should be a "good" name and 800 * number 801 */ 802 nm = nnode.sysctl_num; 803 #if NKSYMS > 0 804 if (nm == CTL_CREATESYM) 805 nm = CTL_CREATE; 806 #endif /* NKSYMS > 0 */ 807 if (nm < 0 && nm != CTL_CREATE) 808 return SET_ERROR(EINVAL); 809 810 /* 811 * the name can't start with a digit 812 */ 813 if (nnode.sysctl_name[0] >= '0' && 814 nnode.sysctl_name[0] <= '9') 815 return SET_ERROR(EINVAL); 816 817 /* 818 * the name must be only alphanumerics or - or _, longer than 819 * 0 bytes and less than SYSCTL_NAMELEN 820 */ 821 nsz = 0; 822 while (nsz < SYSCTL_NAMELEN && nnode.sysctl_name[nsz] != '\0') { 823 if ((nnode.sysctl_name[nsz] >= '0' && 824 nnode.sysctl_name[nsz] <= '9') || 825 (nnode.sysctl_name[nsz] >= 'A' && 826 nnode.sysctl_name[nsz] <= 'Z') || 827 (nnode.sysctl_name[nsz] >= 'a' && 828 nnode.sysctl_name[nsz] <= 'z') || 829 nnode.sysctl_name[nsz] == '-' || 830 nnode.sysctl_name[nsz] == '_') 831 nsz++; 832 else 833 return SET_ERROR(EINVAL); 834 } 835 if (nsz == 0 || nsz == SYSCTL_NAMELEN) 836 return SET_ERROR(EINVAL); 837 838 /* 839 * various checks revolve around size vs type, etc 840 */ 841 type = SYSCTL_TYPE(nnode.sysctl_flags); 842 flags = SYSCTL_FLAGS(nnode.sysctl_flags); 843 sz = nnode.sysctl_size; 844 845 /* 846 * find out if there's a collision, and if so, let the caller 847 * know what they collided with 848 */ 849 node = pnode->sysctl_child; 850 at = 0; 851 if (node) { 852 if ((flags | node->sysctl_flags) & CTLFLAG_ANYNUMBER) 853 /* No siblings for a CTLFLAG_ANYNUMBER node */ 854 return SET_ERROR(EINVAL); 855 for (ni = 0; ni < pnode->sysctl_clen; ni++) { 856 if (nm == node[ni].sysctl_num || 857 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) { 858 /* 859 * ignore error here, since we 860 * are already fixed on EEXIST 861 */ 862 (void)sysctl_cvt_out(l, v, &node[ni], oldp, 863 *oldlenp, oldlenp); 864 return SET_ERROR(EEXIST); 865 } 866 if (nm > node[ni].sysctl_num) 867 at++; 868 } 869 } 870 871 /* 872 * use sysctl_ver to add to the tree iff it hasn't changed 873 */ 874 if (nnode.sysctl_ver != 0) { 875 /* 876 * a specified value must match either the parent 877 * node's version or the root node's version 878 */ 879 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver && 880 nnode.sysctl_ver != rnode->sysctl_ver) { 881 return SET_ERROR(EINVAL); 882 } 883 } 884 885 /* 886 * only the kernel can assign functions to entries 887 */ 888 if (l != NULL && nnode.sysctl_func != NULL) 889 return SET_ERROR(EPERM); 890 891 /* 892 * only the kernel can create permanent entries, and only then 893 * before the kernel is finished setting itself up 894 */ 895 if (l != NULL && (flags & ~SYSCTL_USERFLAGS)) 896 return SET_ERROR(EPERM); 897 if ((flags & CTLFLAG_PERMANENT) & 898 (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT)) 899 return SET_ERROR(EPERM); 900 if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) == 901 (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) 902 return SET_ERROR(EINVAL); 903 if ((flags & CTLFLAG_IMMEDIATE) && 904 type != CTLTYPE_INT && type != CTLTYPE_QUAD && type != CTLTYPE_BOOL) 905 return SET_ERROR(EINVAL); 906 907 /* 908 * check size, or set it if unset and we can figure it out. 909 * kernel created nodes are allowed to have a function instead 910 * of a size (or a data pointer). 911 */ 912 switch (type) { 913 case CTLTYPE_NODE: 914 /* 915 * only *i* can assert the size of a node 916 */ 917 if (flags & CTLFLAG_ALIAS) { 918 anum = nnode.sysctl_alias; 919 if (anum < 0) 920 return SET_ERROR(EINVAL); 921 nnode.sysctl_alias = 0; 922 } 923 if (sz != 0 || nnode.sysctl_data != NULL) 924 return SET_ERROR(EINVAL); 925 if (nnode.sysctl_csize != 0 || 926 nnode.sysctl_clen != 0 || 927 nnode.sysctl_child != 0) 928 return SET_ERROR(EINVAL); 929 if (flags & CTLFLAG_OWNDATA) 930 return SET_ERROR(EINVAL); 931 sz = sizeof(struct sysctlnode); 932 break; 933 case CTLTYPE_INT: 934 /* 935 * since an int is an int, if the size is not given or 936 * is wrong, we can "int-uit" it. 937 */ 938 if (sz != 0 && sz != sizeof(int)) 939 return SET_ERROR(EINVAL); 940 sz = sizeof(int); 941 break; 942 case CTLTYPE_STRING: 943 /* 944 * strings are a little more tricky 945 */ 946 if (sz == 0) { 947 if (l == NULL) { 948 if (nnode.sysctl_func == NULL) { 949 if (nnode.sysctl_data == NULL) 950 return SET_ERROR(EINVAL); 951 else 952 sz = strlen(nnode.sysctl_data) + 953 1; 954 } 955 } else if (nnode.sysctl_data == NULL && 956 flags & CTLFLAG_OWNDATA) { 957 return SET_ERROR(EINVAL); 958 } else { 959 char *vp, *e; 960 size_t s; 961 962 /* 963 * we want a rough idea of what the 964 * size is now 965 */ 966 vp = malloc(PAGE_SIZE, M_SYSCTLDATA, M_WAITOK); 967 if (vp == NULL) 968 return SET_ERROR(ENOMEM); 969 e = nnode.sysctl_data; 970 do { 971 error = copyinstr(e, vp, PAGE_SIZE, &s); 972 if (error) { 973 if (error != ENAMETOOLONG) { 974 free(vp, M_SYSCTLDATA); 975 return error; 976 } 977 e += PAGE_SIZE; 978 if ((e - 32 * PAGE_SIZE) > 979 (char*)nnode.sysctl_data) { 980 free(vp, M_SYSCTLDATA); 981 return 982 SET_ERROR(ERANGE); 983 } 984 } 985 } while (error != 0); 986 sz = s + (e - (char*)nnode.sysctl_data); 987 free(vp, M_SYSCTLDATA); 988 } 989 } 990 break; 991 case CTLTYPE_QUAD: 992 if (sz != 0 && sz != sizeof(u_quad_t)) 993 return SET_ERROR(EINVAL); 994 sz = sizeof(u_quad_t); 995 break; 996 case CTLTYPE_BOOL: 997 /* 998 * since a bool is a bool, if the size is not given or 999 * is wrong, we can "intuit" it. 1000 */ 1001 if (sz != 0 && sz != sizeof(bool)) 1002 return SET_ERROR(EINVAL); 1003 sz = sizeof(bool); 1004 break; 1005 case CTLTYPE_STRUCT: 1006 if (sz == 0) { 1007 if (l != NULL || nnode.sysctl_func == NULL) 1008 return SET_ERROR(EINVAL); 1009 if (flags & CTLFLAG_OWNDATA) 1010 return SET_ERROR(EINVAL); 1011 } 1012 break; 1013 default: 1014 return SET_ERROR(EINVAL); 1015 } 1016 1017 /* 1018 * at this point, if sz is zero, we *must* have a 1019 * function to go with it and we can't own it. 1020 */ 1021 1022 /* 1023 * l ptr own 1024 * 0 0 0 -> EINVAL (if no func) 1025 * 0 0 1 -> own 1026 * 0 1 0 -> kptr 1027 * 0 1 1 -> kptr 1028 * 1 0 0 -> EINVAL 1029 * 1 0 1 -> own 1030 * 1 1 0 -> kptr, no own (fault on lookup) 1031 * 1 1 1 -> uptr, own 1032 */ 1033 if (type != CTLTYPE_NODE) { 1034 if (sz != 0) { 1035 if (flags & CTLFLAG_OWNDATA) { 1036 own = malloc(sz, M_SYSCTLDATA, M_WAITOK); 1037 if (own == NULL) 1038 return SET_ERROR(ENOMEM); 1039 if (nnode.sysctl_data == NULL) 1040 memset(own, 0, sz); 1041 else { 1042 error = sysctl_copyin(l, 1043 nnode.sysctl_data, own, sz); 1044 if (error != 0) { 1045 free(own, M_SYSCTLDATA); 1046 return error; 1047 } 1048 } 1049 } else if ((nnode.sysctl_data != NULL) && 1050 !(flags & CTLFLAG_IMMEDIATE)) { 1051 #if NKSYMS > 0 1052 if (name[namelen - 1] == CTL_CREATESYM) { 1053 char symname[128]; /* XXX enough? */ 1054 u_long symaddr; 1055 size_t symlen; 1056 1057 error = sysctl_copyinstr(l, 1058 nnode.sysctl_data, symname, 1059 sizeof(symname), &symlen); 1060 if (error) 1061 return error; 1062 error = ksyms_getval(NULL, symname, 1063 &symaddr, KSYMS_EXTERN); 1064 if (error) 1065 return error; /* EINVAL? */ 1066 nnode.sysctl_data = (void*)symaddr; 1067 } 1068 #endif /* NKSYMS > 0 */ 1069 /* 1070 * Ideally, we'd like to verify here 1071 * that this address is acceptable, 1072 * but... 1073 * 1074 * - it might be valid now, only to 1075 * become invalid later 1076 * 1077 * - it might be invalid only for the 1078 * moment and valid later 1079 * 1080 * - or something else. 1081 * 1082 * Since we can't get a good answer, 1083 * we'll just accept the address as 1084 * given, and fault on individual 1085 * lookups. 1086 */ 1087 } 1088 } else if (nnode.sysctl_func == NULL) 1089 return SET_ERROR(EINVAL); 1090 } 1091 1092 /* 1093 * a process can't assign a function to a node, and the kernel 1094 * can't create a node that has no function or data. 1095 * (XXX somewhat redundant check) 1096 */ 1097 if (l != NULL || nnode.sysctl_func == NULL) { 1098 if (type != CTLTYPE_NODE && 1099 !(flags & CTLFLAG_IMMEDIATE) && 1100 nnode.sysctl_data == NULL && 1101 own == NULL) 1102 return SET_ERROR(EINVAL); 1103 } 1104 1105 #ifdef SYSCTL_DISALLOW_KWRITE 1106 /* 1107 * a process can't create a writable node unless it refers to 1108 * new data. 1109 */ 1110 if (l != NULL && own == NULL && type != CTLTYPE_NODE && 1111 (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY && 1112 !(flags & CTLFLAG_IMMEDIATE)) 1113 return SET_ERROR(EPERM); 1114 #endif /* SYSCTL_DISALLOW_KWRITE */ 1115 1116 /* 1117 * make sure there's somewhere to put the new stuff. 1118 */ 1119 if (pnode->sysctl_child == NULL) { 1120 if (flags & CTLFLAG_ANYNUMBER) 1121 error = sysctl_alloc(pnode, 1); 1122 else 1123 error = sysctl_alloc(pnode, 0); 1124 if (error) { 1125 if (own != NULL) 1126 free(own, M_SYSCTLDATA); 1127 return error; 1128 } 1129 } 1130 node = pnode->sysctl_child; 1131 1132 /* 1133 * no collisions, so pick a good dynamic number if we need to. 1134 */ 1135 if (nm == CTL_CREATE) { 1136 nm = ++sysctl_root.sysctl_num; 1137 for (ni = 0; ni < pnode->sysctl_clen; ni++) { 1138 if (nm == node[ni].sysctl_num) { 1139 nm++; 1140 ni = -1; 1141 } else if (nm > node[ni].sysctl_num) 1142 at = ni + 1; 1143 } 1144 } 1145 1146 /* 1147 * oops...ran out of space 1148 */ 1149 if (pnode->sysctl_clen == pnode->sysctl_csize) { 1150 error = sysctl_realloc(pnode); 1151 if (error) { 1152 if (own != NULL) 1153 free(own, M_SYSCTLDATA); 1154 return error; 1155 } 1156 node = pnode->sysctl_child; 1157 } 1158 1159 /* 1160 * insert new node data 1161 */ 1162 if (at < pnode->sysctl_clen) { 1163 int t; 1164 1165 /* 1166 * move the nodes that should come after the new one 1167 */ 1168 memmove(&node[at + 1], &node[at], 1169 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode)); 1170 memset(&node[at], 0, sizeof(struct sysctlnode)); 1171 node[at].sysctl_parent = pnode; 1172 /* 1173 * and...reparent any children of any moved nodes 1174 */ 1175 for (ni = at; ni <= pnode->sysctl_clen; ni++) 1176 if (node[ni].sysctl_child != NULL) 1177 for (t = 0; t < node[ni].sysctl_csize; t++) 1178 node[ni].sysctl_child[t].sysctl_parent = 1179 &node[ni]; 1180 } 1181 node = &node[at]; 1182 pnode->sysctl_clen++; 1183 1184 strlcpy(node->sysctl_name, nnode.sysctl_name, 1185 sizeof(node->sysctl_name)); 1186 node->sysctl_num = nm; 1187 node->sysctl_size = sz; 1188 node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */ 1189 node->sysctl_csize = 0; 1190 node->sysctl_clen = 0; 1191 if (own) { 1192 node->sysctl_data = own; 1193 node->sysctl_flags |= CTLFLAG_OWNDATA; 1194 } else if (flags & CTLFLAG_ALIAS) { 1195 node->sysctl_alias = anum; 1196 } else if (flags & CTLFLAG_IMMEDIATE) { 1197 switch (type) { 1198 case CTLTYPE_BOOL: 1199 node->sysctl_bdata = nnode.sysctl_bdata; 1200 break; 1201 case CTLTYPE_INT: 1202 node->sysctl_idata = nnode.sysctl_idata; 1203 break; 1204 case CTLTYPE_QUAD: 1205 node->sysctl_qdata = nnode.sysctl_qdata; 1206 break; 1207 } 1208 } else { 1209 node->sysctl_data = nnode.sysctl_data; 1210 node->sysctl_flags &= ~CTLFLAG_OWNDATA; 1211 } 1212 node->sysctl_func = nnode.sysctl_func; 1213 node->sysctl_child = NULL; 1214 /* node->sysctl_parent should already be done */ 1215 1216 /* 1217 * update "version" on path to "root" 1218 */ 1219 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent) 1220 ; 1221 pnode = node; 1222 for (nm = rnode->sysctl_ver + 1; pnode != NULL; 1223 pnode = pnode->sysctl_parent) 1224 pnode->sysctl_ver = nm; 1225 1226 /* If this fails, the node is already added - the user won't know! */ 1227 error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp); 1228 1229 return error; 1230 } 1231 1232 /* 1233 * ******************************************************************** 1234 * A wrapper around sysctl_create() that prints the thing we're trying 1235 * to add. 1236 * ******************************************************************** 1237 */ 1238 #ifdef SYSCTL_DEBUG_CREATE 1239 int 1240 sysctl_create(SYSCTLFN_ARGS) 1241 { 1242 const struct sysctlnode *node; 1243 int k, v, rc, ni, nl = namelen + (name - oname); 1244 struct sysctlnode nnode; 1245 1246 if (newp == NULL) 1247 return SET_ERROR(EINVAL); 1248 int error = sysctl_cvt_in(l, &v, newp, newlen, &nnode); 1249 if (error) 1250 return error; 1251 1252 node = &nnode; 1253 1254 printf("namelen %d (", nl); 1255 for (ni = 0; ni < nl - 1; ni++) 1256 printf(" %d", oname[ni]); 1257 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n", 1258 k = node->sysctl_num, 1259 node->sysctl_name, 1260 node->sysctl_flags, 1261 SYSCTL_FLAGS(node->sysctl_flags), 1262 SYSCTL_TYPE(node->sysctl_flags), 1263 node->sysctl_size); 1264 1265 node = rnode; 1266 rc = _sysctl_create(SYSCTLFN_CALL(rnode)); 1267 1268 printf("sysctl_create("); 1269 for (ni = 0; ni < nl - 1; ni++) 1270 printf(" %d", oname[ni]); 1271 printf(" %d ) returned %d\n", k, rc); 1272 1273 return (rc); 1274 } 1275 #endif /* SYSCTL_DEBUG_CREATE */ 1276 1277 /* 1278 * sysctl_destroy -- Removes a node (as described by newp) from the 1279 * given tree, returning (if successful) a copy of the dead node in 1280 * oldp. Since we're removing stuff, there's not much to check. 1281 */ 1282 int 1283 sysctl_destroy(SYSCTLFN_ARGS) 1284 { 1285 struct sysctlnode *node, *pnode, onode, nnode; 1286 int ni, error, v; 1287 1288 KASSERT(rw_write_held(&sysctl_treelock)); 1289 1290 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1291 printf("sysctl_destroy: rnode %p wrong version\n", rnode); 1292 return SET_ERROR(EINVAL); 1293 } 1294 1295 error = 0; 1296 1297 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY) 1298 return SET_ERROR(EINVAL); 1299 1300 /* 1301 * processes can only destroy nodes at securelevel 0, must be 1302 * root, and can't remove nodes from a parent that's not 1303 * writeable 1304 */ 1305 if (l != NULL) { 1306 #ifndef SYSCTL_DISALLOW_CREATE 1307 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1308 KAUTH_REQ_SYSTEM_SYSCTL_DELETE, NULL, NULL, NULL); 1309 if (error) 1310 return error; 1311 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE)) 1312 #endif /* SYSCTL_DISALLOW_CREATE */ 1313 return SET_ERROR(EPERM); 1314 } 1315 1316 /* 1317 * nothing can remove a node if: 1318 * the node is permanent (checked later) or 1319 * the tree itself is not writeable or 1320 * the entire sysctl system is not writeable 1321 * 1322 * note that we ignore whether setup is complete or not, 1323 * because these rules always apply. 1324 */ 1325 if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) || 1326 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)) 1327 return SET_ERROR(EPERM); 1328 1329 if (newp == NULL) 1330 return SET_ERROR(EINVAL); 1331 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode); 1332 if (error) 1333 return error; 1334 memset(&onode, 0, sizeof(struct sysctlnode)); 1335 1336 node = rnode->sysctl_child; 1337 for (ni = 0; ni < rnode->sysctl_clen; ni++) { 1338 if (nnode.sysctl_num == node[ni].sysctl_num) { 1339 /* 1340 * if name specified, must match 1341 */ 1342 if (nnode.sysctl_name[0] != '\0' && 1343 strcmp(nnode.sysctl_name, node[ni].sysctl_name)) 1344 continue; 1345 /* 1346 * if version specified, must match 1347 */ 1348 if (nnode.sysctl_ver != 0 && 1349 nnode.sysctl_ver != node[ni].sysctl_ver) 1350 continue; 1351 /* 1352 * this must be the one 1353 */ 1354 break; 1355 } 1356 } 1357 if (ni == rnode->sysctl_clen) 1358 return SET_ERROR(ENOENT); 1359 node = &node[ni]; 1360 pnode = node->sysctl_parent; 1361 1362 /* 1363 * if the kernel says permanent, it is, so there. nyah. 1364 */ 1365 if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT) 1366 return SET_ERROR(EPERM); 1367 1368 /* 1369 * can't delete non-empty nodes 1370 */ 1371 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE && 1372 node->sysctl_clen != 0) 1373 return SET_ERROR(ENOTEMPTY); 1374 1375 /* 1376 * if the node "owns" data, release it now 1377 */ 1378 if (node->sysctl_flags & CTLFLAG_OWNDATA) { 1379 if (node->sysctl_data != NULL) 1380 free(node->sysctl_data, M_SYSCTLDATA); 1381 node->sysctl_data = NULL; 1382 } 1383 if (node->sysctl_flags & CTLFLAG_OWNDESC) { 1384 if (node->sysctl_desc != NULL) 1385 /*XXXUNCONST*/ 1386 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA); 1387 node->sysctl_desc = NULL; 1388 } 1389 1390 /* 1391 * if the node to be removed is not the last one on the list, 1392 * move the remaining nodes up, and reparent any grandchildren 1393 */ 1394 onode = *node; 1395 if (ni < pnode->sysctl_clen - 1) { 1396 int t; 1397 1398 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1], 1399 (pnode->sysctl_clen - ni - 1) * 1400 sizeof(struct sysctlnode)); 1401 for (; ni < pnode->sysctl_clen - 1; ni++) 1402 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) == 1403 CTLTYPE_NODE) 1404 for (t = 0; 1405 t < pnode->sysctl_child[ni].sysctl_clen; 1406 t++) 1407 pnode->sysctl_child[ni].sysctl_child[t]. 1408 sysctl_parent = 1409 &pnode->sysctl_child[ni]; 1410 ni = pnode->sysctl_clen - 1; 1411 node = &pnode->sysctl_child[ni]; 1412 } 1413 1414 /* 1415 * reset the space we just vacated 1416 */ 1417 memset(node, 0, sizeof(struct sysctlnode)); 1418 node->sysctl_parent = pnode; 1419 pnode->sysctl_clen--; 1420 1421 /* 1422 * if this parent just lost its last child, nuke the creche 1423 */ 1424 if (pnode->sysctl_clen == 0) { 1425 free(pnode->sysctl_child, M_SYSCTLNODE); 1426 pnode->sysctl_csize = 0; 1427 pnode->sysctl_child = NULL; 1428 } 1429 1430 /* 1431 * update "version" on path to "root" 1432 */ 1433 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent) 1434 ; 1435 for (ni = rnode->sysctl_ver + 1; pnode != NULL; 1436 pnode = pnode->sysctl_parent) 1437 pnode->sysctl_ver = ni; 1438 1439 error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp); 1440 1441 return error; 1442 } 1443 1444 /* 1445 * sysctl_lookup -- Handles copyin/copyout of new and old values. 1446 * Partial reads are globally allowed. Only root can write to things 1447 * unless the node says otherwise. 1448 */ 1449 int 1450 sysctl_lookup(SYSCTLFN_ARGS) 1451 { 1452 int error, rw; 1453 size_t sz, len; 1454 void *d; 1455 1456 KASSERT(rw_lock_held(&sysctl_treelock)); 1457 1458 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1459 printf("%s: rnode %p wrong version\n", __func__, rnode); 1460 return SET_ERROR(EINVAL); 1461 } 1462 1463 if (newlen == 0) 1464 newp = NULL; 1465 1466 error = 0; 1467 1468 /* 1469 * you can't "look up" a node. you can "query" it, but you 1470 * can't "look it up". 1471 */ 1472 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0) { 1473 DPRINTF(("%s: can't lookup a node\n", __func__)); 1474 return SET_ERROR(EINVAL); 1475 } 1476 1477 /* 1478 * some nodes are private, so only root can look into them. 1479 */ 1480 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) && 1481 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1482 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) { 1483 DPRINTF(("%s: private node\n", __func__)); 1484 return error; 1485 } 1486 1487 /* 1488 * if a node wants to be writable according to different rules 1489 * other than "only root can write to stuff unless a flag is 1490 * set", then it needs its own function which should have been 1491 * called and not us. 1492 */ 1493 if (l != NULL && newp != NULL && 1494 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) && 1495 (error = kauth_authorize_system(l->l_cred, 1496 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_MODIFY, NULL, NULL, 1497 NULL)) != 0) { 1498 DPRINTF(("%s: can't modify\n", __func__)); 1499 return error; 1500 } 1501 1502 /* 1503 * is this node supposedly writable? 1504 */ 1505 rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0; 1506 1507 /* 1508 * it appears not to be writable at this time, so if someone 1509 * tried to write to it, we must tell them to go away 1510 */ 1511 if (!rw && newp != NULL) { 1512 DPRINTF(("%s: not writable\n", __func__)); 1513 return SET_ERROR(EPERM); 1514 } 1515 1516 /* 1517 * step one, copy out the stuff we have presently 1518 */ 1519 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) { 1520 /* 1521 * note that we discard const here because we are 1522 * modifying the contents of the node (which is okay 1523 * because it's ours) 1524 * 1525 * It also doesn't matter which field of the union we pick. 1526 */ 1527 d = __UNCONST(&rnode->sysctl_qdata); 1528 } else 1529 d = rnode->sysctl_data; 1530 1531 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING) 1532 sz = strlen(d) + 1; /* XXX@@@ possible fault here */ 1533 else 1534 sz = rnode->sysctl_size; 1535 if (oldp != NULL) { 1536 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp)); 1537 if (error) { 1538 DPRINTF(("%s: bad copyout %d\n", __func__, error)); 1539 return error; 1540 } 1541 } 1542 *oldlenp = sz; 1543 1544 /* 1545 * are we done? 1546 */ 1547 if (newp == NULL) 1548 return 0; 1549 1550 /* 1551 * hmm...not done. must now "copy in" new value. re-adjust 1552 * sz to maximum value (strings are "weird"). 1553 */ 1554 sz = rnode->sysctl_size; 1555 switch (SYSCTL_TYPE(rnode->sysctl_flags)) { 1556 case CTLTYPE_BOOL: { 1557 bool tmp; 1558 /* 1559 * these data must be *exactly* the same size coming 1560 * in. bool may only be true or false. 1561 */ 1562 if (newlen != sz) { 1563 DPRINTF(("%s: bad size %zu != %zu\n", __func__, newlen, 1564 sz)); 1565 return SET_ERROR(EINVAL); 1566 } 1567 error = sysctl_copyin(l, newp, &tmp, sz); 1568 if (error) 1569 break; 1570 if (tmp != true && tmp != false) { 1571 DPRINTF(("%s: tmp %d\n", __func__, tmp)); 1572 return SET_ERROR(EINVAL); 1573 } 1574 *(bool *)d = tmp; 1575 break; 1576 } 1577 case CTLTYPE_INT: 1578 case CTLTYPE_QUAD: 1579 case CTLTYPE_STRUCT: 1580 /* 1581 * these data must be *exactly* the same size coming 1582 * in. 1583 */ 1584 if (newlen != sz) 1585 goto bad_size; 1586 error = sysctl_copyin(l, newp, d, sz); 1587 rnd_add_data(NULL, d, sz, 0); 1588 break; 1589 case CTLTYPE_STRING: { 1590 /* 1591 * strings, on the other hand, can be shorter, and we 1592 * let userland be sloppy about the trailing nul. 1593 */ 1594 char *newbuf; 1595 1596 /* 1597 * too much new string? 1598 */ 1599 if (newlen > sz) 1600 goto bad_size; 1601 1602 /* 1603 * temporary copy of new inbound string 1604 */ 1605 len = MIN(sz, newlen); 1606 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK); 1607 if (newbuf == NULL) { 1608 DPRINTF(("%s: oomem %zu\n", __func__, len)); 1609 return SET_ERROR(ENOMEM); 1610 } 1611 error = sysctl_copyin(l, newp, newbuf, len); 1612 if (error) { 1613 free(newbuf, M_SYSCTLDATA); 1614 DPRINTF(("%s: copyin %d\n", __func__, error)); 1615 return error; 1616 } 1617 1618 /* 1619 * did they NUL terminate it, or do we have space 1620 * left to do it ourselves? 1621 */ 1622 if (newbuf[len - 1] != '\0' && len == sz) { 1623 free(newbuf, M_SYSCTLDATA); 1624 DPRINTF(("%s: string too long\n", __func__)); 1625 return SET_ERROR(EINVAL); 1626 } 1627 1628 /* 1629 * looks good, so pop it into place and zero the rest. 1630 */ 1631 if (len > 0) { 1632 memcpy(d, newbuf, len); 1633 rnd_add_data(NULL, d, len, 0); 1634 } 1635 if (sz != len) 1636 memset((char*)d + len, 0, sz - len); 1637 free(newbuf, M_SYSCTLDATA); 1638 break; 1639 } 1640 default: 1641 DPRINTF(("%s: bad type\n", __func__)); 1642 return SET_ERROR(EINVAL); 1643 } 1644 if (error) { 1645 DPRINTF(("%s: copyin %d\n", __func__, error)); 1646 } 1647 1648 return error; 1649 1650 bad_size: 1651 DPRINTF(("%s: bad size %zu > %zu\n", __func__, newlen, sz)); 1652 return SET_ERROR(EINVAL); 1653 } 1654 1655 /* 1656 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that 1657 * purport to handle it. This interface isn't fully fleshed out yet, 1658 * unfortunately. 1659 */ 1660 static int 1661 sysctl_mmap(SYSCTLFN_ARGS) 1662 { 1663 const struct sysctlnode *node; 1664 struct sysctlnode nnode; 1665 int error; 1666 int sysctl_num; 1667 1668 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1669 printf("sysctl_mmap: rnode %p wrong version\n", rnode); 1670 return SET_ERROR(EINVAL); 1671 } 1672 1673 /* 1674 * let's just pretend that didn't happen, m'kay? 1675 */ 1676 if (l == NULL) 1677 return SET_ERROR(EPERM); 1678 1679 /* 1680 * is this a sysctlnode description of an mmap request? 1681 */ 1682 if (newp == NULL || newlen != sizeof(struct sysctlnode)) 1683 return SET_ERROR(EINVAL); 1684 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode)); 1685 if (error) 1686 return error; 1687 1688 /* 1689 * does the node they asked for exist? 1690 */ 1691 if (namelen != 1) 1692 return SET_ERROR(EOPNOTSUPP); 1693 node = rnode; 1694 sysctl_num = nnode.sysctl_num; 1695 error = sysctl_locate(l, &sysctl_num, 1, &node, NULL); 1696 if (error) 1697 return error; 1698 1699 /* 1700 * does this node that we have found purport to handle mmap? 1701 */ 1702 if (node->sysctl_func == NULL || 1703 !(node->sysctl_flags & CTLFLAG_MMAP)) 1704 return SET_ERROR(EOPNOTSUPP); 1705 1706 /* 1707 * well...okay, they asked for it. 1708 */ 1709 return ((*node->sysctl_func)(SYSCTLFN_CALL(node))); 1710 } 1711 1712 int 1713 sysctl_describe(SYSCTLFN_ARGS) 1714 { 1715 struct sysctldesc *d; 1716 void *bf; 1717 size_t sz, left, tot; 1718 int i, error, v = -1; 1719 struct sysctlnode *node; 1720 struct sysctlnode dnode; 1721 1722 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1723 printf("sysctl_query: rnode %p wrong version\n", rnode); 1724 return SET_ERROR(EINVAL); 1725 } 1726 1727 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 1728 return SET_ERROR(ENOTDIR); 1729 if (namelen != 1 || name[0] != CTL_DESCRIBE) 1730 return SET_ERROR(EINVAL); 1731 1732 /* 1733 * get ready... 1734 */ 1735 error = 0; 1736 d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK); 1737 if (bf == NULL) 1738 return SET_ERROR(ENOMEM); 1739 tot = 0; 1740 node = rnode->sysctl_child; 1741 left = *oldlenp; 1742 1743 /* 1744 * no request -> all descriptions at this level 1745 * request with desc unset -> just this node 1746 * request with desc set -> set descr for this node 1747 */ 1748 if (newp != NULL) { 1749 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode); 1750 if (error) 1751 goto out; 1752 if (dnode.sysctl_desc != NULL) { 1753 /* 1754 * processes cannot set descriptions above 1755 * securelevel 0. and must be root. blah 1756 * blah blah. a couple more checks are made 1757 * once we find the node we want. 1758 */ 1759 if (l != NULL) { 1760 #ifndef SYSCTL_DISALLOW_CREATE 1761 error = kauth_authorize_system(l->l_cred, 1762 KAUTH_SYSTEM_SYSCTL, 1763 KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL, 1764 NULL, NULL); 1765 if (error) 1766 goto out; 1767 #else /* SYSCTL_DISALLOW_CREATE */ 1768 error = SET_ERROR(EPERM); 1769 goto out; 1770 #endif /* SYSCTL_DISALLOW_CREATE */ 1771 } 1772 1773 /* 1774 * find node and try to set the description on it 1775 */ 1776 for (i = 0; i < rnode->sysctl_clen; i++) 1777 if (node[i].sysctl_num == dnode.sysctl_num) 1778 break; 1779 if (i == rnode->sysctl_clen) { 1780 error = SET_ERROR(ENOENT); 1781 goto out; 1782 } 1783 node = &node[i]; 1784 1785 /* 1786 * did the caller specify a node version? 1787 */ 1788 if (dnode.sysctl_ver != 0 && 1789 dnode.sysctl_ver != node->sysctl_ver) { 1790 error = SET_ERROR(EINVAL); 1791 goto out; 1792 } 1793 1794 /* 1795 * okay...some rules: 1796 * (1) if setup is done and the tree is 1797 * read-only or the whole system is 1798 * read-only 1799 * (2) no one can set a description on a 1800 * permanent node (it must be set when 1801 * using createv) 1802 * (3) processes cannot *change* a description 1803 * (4) processes *can*, however, set a 1804 * description on a read-only node so that 1805 * one can be created and then described 1806 * in two steps 1807 * anything else come to mind? 1808 */ 1809 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) && 1810 (!(sysctl_rootof(node)->sysctl_flags & 1811 CTLFLAG_READWRITE) || 1812 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) { 1813 error = SET_ERROR(EPERM); 1814 goto out; 1815 } 1816 if (node->sysctl_flags & CTLFLAG_PERMANENT) { 1817 error = SET_ERROR(EPERM); 1818 goto out; 1819 } 1820 if (l != NULL && node->sysctl_desc != NULL) { 1821 error = SET_ERROR(EPERM); 1822 goto out; 1823 } 1824 1825 /* 1826 * right, let's go ahead. the first step is 1827 * making the description into something the 1828 * node can "own", if need be. 1829 */ 1830 if (l != NULL || 1831 dnode.sysctl_flags & CTLFLAG_OWNDESC) { 1832 char *nd, *k; 1833 1834 k = malloc(MAXDESCLEN, M_TEMP, M_WAITOK); 1835 if (k == NULL) { 1836 error = SET_ERROR(ENOMEM); 1837 goto out; 1838 } 1839 error = sysctl_copyinstr(l, dnode.sysctl_desc, 1840 k, MAXDESCLEN, &sz); 1841 if (error) { 1842 free(k, M_TEMP); 1843 goto out; 1844 } 1845 nd = malloc(sz, M_SYSCTLDATA, M_WAITOK); 1846 if (nd == NULL) { 1847 free(k, M_TEMP); 1848 error = SET_ERROR(ENOMEM); 1849 goto out; 1850 } 1851 memcpy(nd, k, sz); 1852 dnode.sysctl_flags |= CTLFLAG_OWNDESC; 1853 dnode.sysctl_desc = nd; 1854 free(k, M_TEMP); 1855 } 1856 1857 /* 1858 * now "release" the old description and 1859 * attach the new one. ta-da. 1860 */ 1861 if ((node->sysctl_flags & CTLFLAG_OWNDESC) && 1862 node->sysctl_desc != NULL) 1863 /*XXXUNCONST*/ 1864 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA); 1865 node->sysctl_desc = dnode.sysctl_desc; 1866 node->sysctl_flags |= 1867 (dnode.sysctl_flags & CTLFLAG_OWNDESC); 1868 1869 /* 1870 * now we "fall out" and into the loop which 1871 * will copy the new description back out for 1872 * those interested parties 1873 */ 1874 } 1875 } 1876 1877 /* 1878 * scan for one description or just retrieve all descriptions 1879 */ 1880 for (i = 0; i < rnode->sysctl_clen; i++) { 1881 /* 1882 * did they ask for the description of only one node? 1883 */ 1884 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num) 1885 continue; 1886 1887 /* 1888 * don't describe "private" nodes to non-suser users 1889 */ 1890 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) && 1891 !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1892 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL))) 1893 continue; 1894 1895 /* 1896 * is this description "valid"? 1897 */ 1898 memset(bf, 0, MAXDESCLEN); 1899 if (node[i].sysctl_desc == NULL) 1900 sz = 1; 1901 else if (copystr(node[i].sysctl_desc, &d->descr_str[0], 1902 MAXDESCLEN - sizeof(*d), &sz) != 0) { 1903 /* 1904 * erase possible partial description 1905 */ 1906 memset(bf, 0, MAXDESCLEN); 1907 sz = 1; 1908 } 1909 1910 /* 1911 * we've got it, stuff it into the caller's buffer 1912 */ 1913 d->descr_num = node[i].sysctl_num; 1914 d->descr_ver = node[i].sysctl_ver; 1915 d->descr_len = sz; /* includes trailing nul */ 1916 sz = (char *)NEXT_DESCR(d) - (char *)d; 1917 if (oldp != NULL && left >= sz) { 1918 error = sysctl_copyout(l, d, oldp, sz); 1919 if (error) 1920 goto out; 1921 left -= sz; 1922 oldp = (void *)__sysc_desc_adv(oldp, d->descr_len); 1923 } 1924 tot += sz; 1925 1926 /* 1927 * if we get this far with v not "unset", they asked 1928 * for a specific node and we found it 1929 */ 1930 if (v != -1) 1931 break; 1932 } 1933 1934 /* 1935 * did we find it after all? 1936 */ 1937 if (v != -1 && tot == 0) 1938 error = SET_ERROR(ENOENT); 1939 else 1940 *oldlenp = tot; 1941 1942 out: 1943 free(bf, M_TEMP); 1944 return error; 1945 } 1946 1947 /* 1948 * ******************************************************************** 1949 * Section 3: Create and destroy from inside the kernel 1950 * ******************************************************************** 1951 * sysctl_createv() and sysctl_destroyv() are simpler-to-use 1952 * interfaces for the kernel to fling new entries into the mib and rip 1953 * them out later. In the case of sysctl_createv(), the returned copy 1954 * of the node (see sysctl_create()) will be translated back into a 1955 * pointer to the actual node. 1956 * 1957 * Note that sysctl_createv() will return 0 if the create request 1958 * matches an existing node (ala mkdir -p), and that sysctl_destroyv() 1959 * will return 0 if the node to be destroyed already does not exist 1960 * (aka rm -f) or if it is a parent of other nodes. 1961 * 1962 * This allows two (or more) different subsystems to assert sub-tree 1963 * existence before populating their own nodes, and to remove their 1964 * own nodes without orphaning the others when they are done. 1965 * ******************************************************************** 1966 */ 1967 #undef sysctl_createv 1968 int 1969 sysctl_createv(struct sysctllog **log, int cflags, 1970 const struct sysctlnode **rnode, const struct sysctlnode **cnode, 1971 int flags, int type, const char *namep, const char *descr, 1972 sysctlfn func, u_quad_t qv, void *newp, size_t newlen, 1973 ...) 1974 { 1975 va_list ap; 1976 int error, ni, namelen, name[CTL_MAXNAME]; 1977 const struct sysctlnode *root, *pnode; 1978 struct sysctlnode nnode, onode, *dnode; 1979 size_t sz; 1980 const struct sysctlnode *snode __diagused; 1981 1982 /* 1983 * where are we putting this? 1984 */ 1985 if (rnode != NULL && *rnode == NULL) { 1986 printf("sysctl_createv: rnode NULL\n"); 1987 return SET_ERROR(EINVAL); 1988 } 1989 root = rnode ? *rnode : NULL; 1990 if (cnode != NULL) 1991 *cnode = NULL; 1992 if (cflags != 0) 1993 return SET_ERROR(EINVAL); 1994 1995 /* 1996 * what is it? 1997 */ 1998 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags); 1999 if (log != NULL) 2000 flags &= ~CTLFLAG_PERMANENT; 2001 2002 /* 2003 * where do we put it? 2004 */ 2005 va_start(ap, newlen); 2006 namelen = 0; 2007 error = 0; 2008 ni = -1; 2009 do { 2010 if (++ni == CTL_MAXNAME) { 2011 error = SET_ERROR(ENAMETOOLONG); 2012 break; 2013 } 2014 name[ni] = va_arg(ap, int); 2015 /* 2016 * sorry, this is not supported from here 2017 */ 2018 if (name[ni] == CTL_CREATESYM) { 2019 error = SET_ERROR(EINVAL); 2020 break; 2021 } 2022 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE); 2023 va_end(ap); 2024 if (error) 2025 return error; 2026 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0); 2027 2028 /* 2029 * what's it called 2030 */ 2031 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >= 2032 sizeof(nnode.sysctl_name)) 2033 return SET_ERROR(ENAMETOOLONG); 2034 2035 /* 2036 * cons up the description of the new node 2037 */ 2038 nnode.sysctl_num = name[namelen - 1]; 2039 name[namelen - 1] = CTL_CREATE; 2040 nnode.sysctl_size = newlen; 2041 nnode.sysctl_flags = flags; 2042 if (type == CTLTYPE_NODE) { 2043 nnode.sysctl_csize = 0; 2044 nnode.sysctl_clen = 0; 2045 nnode.sysctl_child = NULL; 2046 if (flags & CTLFLAG_ALIAS) 2047 nnode.sysctl_alias = qv; 2048 } else if (flags & CTLFLAG_IMMEDIATE) { 2049 switch (type) { 2050 case CTLTYPE_BOOL: 2051 nnode.sysctl_bdata = qv; 2052 break; 2053 case CTLTYPE_INT: 2054 nnode.sysctl_idata = qv; 2055 break; 2056 case CTLTYPE_QUAD: 2057 nnode.sysctl_qdata = qv; 2058 break; 2059 default: 2060 return SET_ERROR(EINVAL); 2061 } 2062 } else { 2063 nnode.sysctl_data = newp; 2064 } 2065 nnode.sysctl_func = func; 2066 nnode.sysctl_parent = NULL; 2067 nnode.sysctl_ver = 0; 2068 2069 /* 2070 * initialize lock state -- we need locks if the main tree has 2071 * been marked as complete, but since we could be called from 2072 * either there, or from a device driver (say, at device 2073 * insertion), or from a module (at module load time, say), we 2074 * don't really want to "wait"... 2075 */ 2076 sysctl_lock(true); 2077 2078 /* 2079 * locate the prospective parent of the new node, and if we 2080 * find it, add the new node. 2081 */ 2082 sz = sizeof(onode); 2083 pnode = root; 2084 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2085 if (error) { 2086 /* 2087 * XXX: If you are seeing this printf in early bringup 2088 * stages, perhaps your setfault is not functioning and 2089 * thus kcopy() is mis-behaving. 2090 */ 2091 printf("sysctl_createv: sysctl_locate(%s) returned %d\n", 2092 nnode.sysctl_name, error); 2093 sysctl_unlock(); 2094 return error; 2095 } 2096 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz, 2097 &nnode, sizeof(nnode), &name[0], NULL, 2098 pnode); 2099 2100 /* 2101 * unfortunately the node we wanted to create is already 2102 * there. if the node that's already there is a reasonable 2103 * facsimile of the node we wanted to create, just pretend 2104 * (for the caller's benefit) that we managed to create the 2105 * node they wanted. 2106 */ 2107 if (error == EEXIST) { 2108 /* name is the same as requested... */ 2109 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 && 2110 /* they want the same function... */ 2111 nnode.sysctl_func == onode.sysctl_func && 2112 /* number is the same as requested, or... */ 2113 (nnode.sysctl_num == onode.sysctl_num || 2114 /* they didn't pick a number... */ 2115 nnode.sysctl_num == CTL_CREATE)) { 2116 /* 2117 * collision here from trying to create 2118 * something that already existed; let's give 2119 * our customers a hand and tell them they got 2120 * what they wanted. 2121 */ 2122 #ifdef SYSCTL_DEBUG_CREATE 2123 printf("cleared\n"); 2124 #endif /* SYSCTL_DEBUG_CREATE */ 2125 error = 0; 2126 } 2127 } 2128 2129 if (error == 0 && 2130 (cnode != NULL || log != NULL || descr != NULL)) { 2131 /* 2132 * sysctl_create() gave us back a copy of the node, 2133 * but we need to know where it actually is... 2134 */ 2135 pnode = root; 2136 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2137 snode = pnode; 2138 2139 /* 2140 * manual scan of last layer so that aliased nodes 2141 * aren't followed. 2142 */ 2143 if (error == 0) { 2144 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2145 if (pnode->sysctl_child[ni].sysctl_num == 2146 onode.sysctl_num) 2147 break; 2148 if (ni < pnode->sysctl_clen) 2149 pnode = &pnode->sysctl_child[ni]; 2150 else 2151 error = SET_ERROR(ENOENT); 2152 } 2153 2154 /* 2155 * not expecting an error here, but... 2156 */ 2157 if (error == 0) { 2158 KASSERTMSG(pnode->sysctl_parent == snode, 2159 "sysctl parent mis-match pnode %s, snode %s", 2160 pnode->sysctl_name, snode->sysctl_name); 2161 if (log != NULL) 2162 sysctl_log_add(log, pnode); 2163 if (cnode != NULL) 2164 *cnode = pnode; 2165 if (descr != NULL) { 2166 /* 2167 * allow first caller to *set* a 2168 * description actually to set it 2169 * 2170 * discard const here so we can attach 2171 * the description 2172 */ 2173 dnode = __UNCONST(pnode); 2174 if (pnode->sysctl_desc != NULL) 2175 /* skip it...we've got one */; 2176 else if (flags & CTLFLAG_OWNDESC) { 2177 size_t l = strlen(descr) + 1; 2178 char *d = malloc(l, M_SYSCTLDATA, 2179 M_WAITOK); 2180 if (d != NULL) { 2181 memcpy(d, descr, l); 2182 dnode->sysctl_desc = d; 2183 dnode->sysctl_flags |= 2184 CTLFLAG_OWNDESC; 2185 } 2186 } else 2187 dnode->sysctl_desc = descr; 2188 } 2189 } else { 2190 printf("sysctl_create succeeded but node not found?!\n"); 2191 /* 2192 * confusing, but the create said it 2193 * succeeded, so... 2194 */ 2195 error = 0; 2196 } 2197 } 2198 2199 /* 2200 * now it should be safe to release the lock state. note that 2201 * the pointer to the newly created node being passed back may 2202 * not be "good" for very long. 2203 */ 2204 sysctl_unlock(); 2205 2206 if (error != 0) { 2207 printf("sysctl_createv: sysctl_create(%s) returned %d\n", 2208 nnode.sysctl_name, error); 2209 #if 0 2210 if (error != ENOENT) 2211 sysctl_dump(&onode); 2212 #endif 2213 } 2214 2215 return error; 2216 } 2217 2218 int 2219 sysctl_destroyv(struct sysctlnode *rnode, ...) 2220 { 2221 va_list ap; 2222 int error, name[CTL_MAXNAME], namelen, ni; 2223 const struct sysctlnode *pnode, *node; 2224 struct sysctlnode dnode, *onode; 2225 size_t sz; 2226 2227 va_start(ap, rnode); 2228 namelen = 0; 2229 ni = 0; 2230 do { 2231 if (ni == CTL_MAXNAME) { 2232 va_end(ap); 2233 return SET_ERROR(ENAMETOOLONG); 2234 } 2235 name[ni] = va_arg(ap, int); 2236 } while (name[ni++] != CTL_EOL); 2237 namelen = ni - 1; 2238 va_end(ap); 2239 2240 /* 2241 * i can't imagine why we'd be destroying a node when the tree 2242 * wasn't complete, but who knows? 2243 */ 2244 sysctl_lock(true); 2245 2246 /* 2247 * where is it? 2248 */ 2249 node = rnode; 2250 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni); 2251 if (error) { 2252 /* they want it gone and it's not there, so... */ 2253 sysctl_unlock(); 2254 return (error == ENOENT ? 0 : error); 2255 } 2256 2257 /* 2258 * set up the deletion 2259 */ 2260 pnode = node; 2261 node = &dnode; 2262 memset(&dnode, 0, sizeof(dnode)); 2263 dnode.sysctl_flags = SYSCTL_VERSION; 2264 dnode.sysctl_num = name[namelen - 1]; 2265 2266 /* 2267 * we found it, now let's nuke it 2268 */ 2269 name[namelen - 1] = CTL_DESTROY; 2270 sz = 0; 2271 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz, 2272 node, sizeof(*node), &name[0], NULL, 2273 pnode); 2274 if (error == ENOTEMPTY) { 2275 /* 2276 * think of trying to delete "foo" when "foo.bar" 2277 * (which someone else put there) is still in 2278 * existence 2279 */ 2280 error = 0; 2281 2282 /* 2283 * dunno who put the description there, but if this 2284 * node can ever be removed, we need to make sure the 2285 * string doesn't go out of context. that means we 2286 * need to find the node that's still there (don't use 2287 * sysctl_locate() because that follows aliasing). 2288 */ 2289 node = pnode->sysctl_child; 2290 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2291 if (node[ni].sysctl_num == dnode.sysctl_num) 2292 break; 2293 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL; 2294 2295 /* 2296 * if we found it, and this node has a description, 2297 * and this node can be released, and it doesn't 2298 * already own its own description...sigh. :) 2299 */ 2300 if (node != NULL && node->sysctl_desc != NULL && 2301 !(node->sysctl_flags & CTLFLAG_PERMANENT) && 2302 !(node->sysctl_flags & CTLFLAG_OWNDESC)) { 2303 char *d; 2304 2305 sz = strlen(node->sysctl_desc) + 1; 2306 d = malloc(sz, M_SYSCTLDATA, M_WAITOK); 2307 if (d != NULL) { 2308 /* 2309 * discard const so that we can 2310 * re-attach the description 2311 */ 2312 memcpy(d, node->sysctl_desc, sz); 2313 onode = __UNCONST(node); 2314 onode->sysctl_desc = d; 2315 onode->sysctl_flags |= CTLFLAG_OWNDESC; 2316 } else { 2317 /* 2318 * XXX drop the description? be 2319 * afraid? don't care? 2320 */ 2321 } 2322 } 2323 } 2324 2325 sysctl_unlock(); 2326 2327 return error; 2328 } 2329 2330 /* 2331 * ******************************************************************** 2332 * Deletes an entire n-ary tree. Not recommended unless you know why 2333 * you're doing it. Personally, I don't know why you'd even think 2334 * about it. 2335 * ******************************************************************** 2336 */ 2337 void 2338 sysctl_free(struct sysctlnode *rnode) 2339 { 2340 struct sysctlnode *node, *pnode; 2341 2342 rw_enter(&sysctl_treelock, RW_WRITER); 2343 2344 if (rnode == NULL) 2345 rnode = &sysctl_root; 2346 2347 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 2348 printf("sysctl_free: rnode %p wrong version\n", rnode); 2349 rw_exit(&sysctl_treelock); 2350 return; 2351 } 2352 2353 pnode = rnode; 2354 2355 node = pnode->sysctl_child; 2356 do { 2357 while (node != NULL && pnode->sysctl_csize > 0) { 2358 while (node < 2359 &pnode->sysctl_child[pnode->sysctl_clen] && 2360 (SYSCTL_TYPE(node->sysctl_flags) != 2361 CTLTYPE_NODE || 2362 node->sysctl_csize == 0)) { 2363 if (SYSCTL_FLAGS(node->sysctl_flags) & 2364 CTLFLAG_OWNDATA) { 2365 if (node->sysctl_data != NULL) { 2366 free(node->sysctl_data, 2367 M_SYSCTLDATA); 2368 node->sysctl_data = NULL; 2369 } 2370 } 2371 if (SYSCTL_FLAGS(node->sysctl_flags) & 2372 CTLFLAG_OWNDESC) { 2373 if (node->sysctl_desc != NULL) { 2374 /*XXXUNCONST*/ 2375 free(__UNCONST(node->sysctl_desc), 2376 M_SYSCTLDATA); 2377 node->sysctl_desc = NULL; 2378 } 2379 } 2380 node++; 2381 } 2382 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) { 2383 pnode = node; 2384 node = node->sysctl_child; 2385 } else 2386 break; 2387 } 2388 if (pnode->sysctl_child != NULL) 2389 free(pnode->sysctl_child, M_SYSCTLNODE); 2390 pnode->sysctl_clen = 0; 2391 pnode->sysctl_csize = 0; 2392 pnode->sysctl_child = NULL; 2393 node = pnode; 2394 pnode = node->sysctl_parent; 2395 } while (pnode != NULL && node != rnode); 2396 2397 rw_exit(&sysctl_treelock); 2398 } 2399 2400 void 2401 sysctl_log_print(const struct sysctllog *slog) 2402 { 2403 int i, len; 2404 2405 printf("root %p left %d size %d content", (const void *)slog->log_root, 2406 slog->log_left, slog->log_size); 2407 2408 for (len = 0, i = slog->log_left; i < slog->log_size; i++) { 2409 switch (len) { 2410 case 0: 2411 len = -1; 2412 printf(" version %d", slog->log_num[i]); 2413 break; 2414 case -1: 2415 len = -2; 2416 printf(" type %d", slog->log_num[i]); 2417 break; 2418 case -2: 2419 len = slog->log_num[i]; 2420 printf(" len %d:", slog->log_num[i]); 2421 if (len <= 0) 2422 len = -1; 2423 break; 2424 default: 2425 len--; 2426 printf(" %d", slog->log_num[i]); 2427 break; 2428 } 2429 } 2430 printf(" end\n"); 2431 } 2432 2433 int 2434 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node) 2435 { 2436 const int size0 = 16; 2437 int name[CTL_MAXNAME], namelen, i; 2438 const struct sysctlnode *pnode; 2439 struct sysctllog *log; 2440 2441 if (node->sysctl_flags & CTLFLAG_PERMANENT) 2442 return (0); 2443 2444 if (logp == NULL) 2445 return (0); 2446 2447 if (*logp == NULL) { 2448 log = malloc(sizeof(struct sysctllog), 2449 M_SYSCTLDATA, M_WAITOK); 2450 if (log == NULL) { 2451 /* XXX print error message? */ 2452 return (-1); 2453 } 2454 log->log_num = malloc(size0 * sizeof(int), 2455 M_SYSCTLDATA, M_WAITOK); 2456 if (log->log_num == NULL) { 2457 /* XXX print error message? */ 2458 free(log, M_SYSCTLDATA); 2459 return (-1); 2460 } 2461 memset(log->log_num, 0, size0 * sizeof(int)); 2462 log->log_root = NULL; 2463 log->log_size = size0; 2464 log->log_left = size0; 2465 *logp = log; 2466 } else 2467 log = *logp; 2468 2469 /* 2470 * check that the root is proper. it's okay to record the 2471 * address of the root of a tree. it's the only thing that's 2472 * guaranteed not to shift around as nodes come and go. 2473 */ 2474 if (log->log_root == NULL) 2475 log->log_root = sysctl_rootof(node); 2476 else if (log->log_root != sysctl_rootof(node)) { 2477 printf("sysctl: log %p root mismatch (%p)\n", 2478 log->log_root, sysctl_rootof(node)); 2479 return (-1); 2480 } 2481 2482 /* 2483 * we will copy out name in reverse order 2484 */ 2485 for (pnode = node, namelen = 0; 2486 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT); 2487 pnode = pnode->sysctl_parent) 2488 name[namelen++] = pnode->sysctl_num; 2489 2490 /* 2491 * do we have space? 2492 */ 2493 if (log->log_left < (namelen + 3)) 2494 sysctl_log_realloc(log); 2495 if (log->log_left < (namelen + 3)) 2496 return (-1); 2497 2498 /* 2499 * stuff name in, then namelen, then node type, and finally, 2500 * the version for non-node nodes. 2501 */ 2502 for (i = 0; i < namelen && i < CTL_MAXNAME; i++) 2503 log->log_num[--log->log_left] = name[i]; 2504 log->log_num[--log->log_left] = namelen; 2505 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags); 2506 if (log->log_num[log->log_left] != CTLTYPE_NODE) 2507 log->log_num[--log->log_left] = node->sysctl_ver; 2508 else 2509 log->log_num[--log->log_left] = 0; 2510 2511 return (0); 2512 } 2513 2514 void 2515 sysctl_teardown(struct sysctllog **logp) 2516 { 2517 const struct sysctlnode *rnode; 2518 struct sysctlnode node; 2519 struct sysctllog *log; 2520 uint namelen; 2521 int *name, t, v, error, ni; 2522 size_t sz; 2523 2524 if (logp == NULL || *logp == NULL) 2525 return; 2526 log = *logp; 2527 2528 rw_enter(&sysctl_treelock, RW_WRITER); 2529 memset(&node, 0, sizeof(node)); 2530 2531 while (log->log_left < log->log_size) { 2532 KASSERT(log->log_left + 3 < log->log_size); 2533 KASSERT(log->log_left + log->log_num[log->log_left + 2] <= 2534 log->log_size); 2535 v = log->log_num[log->log_left++]; 2536 t = log->log_num[log->log_left++]; 2537 namelen = log->log_num[log->log_left++]; 2538 name = &log->log_num[log->log_left]; 2539 2540 node.sysctl_num = name[namelen - 1]; 2541 node.sysctl_flags = SYSCTL_VERSION|t; 2542 node.sysctl_ver = v; 2543 2544 rnode = log->log_root; 2545 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni); 2546 if (error == 0) { 2547 name[namelen - 1] = CTL_DESTROY; 2548 rnode = rnode->sysctl_parent; 2549 sz = 0; 2550 (void)sysctl_destroy(&name[namelen - 1], 1, NULL, 2551 &sz, &node, sizeof(node), 2552 &name[0], NULL, rnode); 2553 } 2554 2555 log->log_left += namelen; 2556 } 2557 2558 KASSERT(log->log_size == log->log_left); 2559 free(log->log_num, M_SYSCTLDATA); 2560 free(log, M_SYSCTLDATA); 2561 *logp = NULL; 2562 2563 rw_exit(&sysctl_treelock); 2564 } 2565 2566 /* 2567 * ******************************************************************** 2568 * old_sysctl -- A routine to bridge old-style internal calls to the 2569 * new infrastructure. 2570 * ******************************************************************** 2571 */ 2572 int 2573 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2574 void *newp, size_t newlen, struct lwp *l) 2575 { 2576 int error; 2577 size_t oldlen = 0; 2578 size_t savelen; 2579 2580 if (oldlenp) { 2581 oldlen = *oldlenp; 2582 } 2583 savelen = oldlen; 2584 2585 sysctl_lock(newp != NULL); 2586 error = sysctl_dispatch(name, namelen, oldp, &oldlen, 2587 newp, newlen, name, l, NULL); 2588 sysctl_unlock(); 2589 if (error == 0 && oldp != NULL && savelen < oldlen) 2590 error = SET_ERROR(ENOMEM); 2591 if (oldlenp) { 2592 *oldlenp = oldlen; 2593 } 2594 2595 return error; 2596 } 2597 2598 /* 2599 * ******************************************************************** 2600 * Section 4: Generic helper routines 2601 * ******************************************************************** 2602 * "helper" routines that can do more finely grained access control, 2603 * construct structures from disparate information, create the 2604 * appearance of more nodes and sub-trees, etc. for example, if 2605 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY 2606 * with a dynamically created list of nodes that represented the 2607 * currently running processes at that instant. 2608 * ******************************************************************** 2609 */ 2610 2611 /* 2612 * first, a few generic helpers that provide: 2613 * 2614 * sysctl_needfunc() a readonly interface that emits a warning 2615 * sysctl_notavail() returns EOPNOTSUPP (generic error) 2616 * sysctl_null() an empty return buffer with no error 2617 */ 2618 int 2619 sysctl_needfunc(SYSCTLFN_ARGS) 2620 { 2621 int error; 2622 2623 printf("!!SYSCTL_NEEDFUNC!!\n"); 2624 2625 if (newp != NULL || namelen != 0) 2626 return SET_ERROR(EOPNOTSUPP); 2627 2628 error = 0; 2629 if (oldp != NULL) 2630 error = sysctl_copyout(l, rnode->sysctl_data, oldp, 2631 MIN(rnode->sysctl_size, *oldlenp)); 2632 *oldlenp = rnode->sysctl_size; 2633 2634 return error; 2635 } 2636 2637 int 2638 sysctl_notavail(SYSCTLFN_ARGS) 2639 { 2640 2641 if (namelen == 1 && name[0] == CTL_QUERY) 2642 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2643 2644 return SET_ERROR(EOPNOTSUPP); 2645 } 2646 2647 int 2648 sysctl_null(SYSCTLFN_ARGS) 2649 { 2650 2651 *oldlenp = 0; 2652 2653 return (0); 2654 } 2655 2656 u_int 2657 sysctl_map_flags(const u_int *map, u_int word) 2658 { 2659 u_int rv; 2660 2661 for (rv = 0; *map != 0; map += 2) 2662 if ((word & map[0]) != 0) 2663 rv |= map[1]; 2664 2665 return rv; 2666 } 2667 2668 /* 2669 * ******************************************************************** 2670 * Section 5: The machinery that makes it all go 2671 * ******************************************************************** 2672 * Memory "manglement" routines. Not much to this, eh? 2673 * ******************************************************************** 2674 */ 2675 static int 2676 sysctl_alloc(struct sysctlnode *p, int x) 2677 { 2678 int i; 2679 struct sysctlnode *n; 2680 2681 assert(p->sysctl_child == NULL); 2682 2683 if (x == 1) 2684 n = malloc(sizeof(struct sysctlnode), 2685 M_SYSCTLNODE, M_WAITOK); 2686 else 2687 n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode), 2688 M_SYSCTLNODE, M_WAITOK); 2689 if (n == NULL) 2690 return SET_ERROR(ENOMEM); 2691 2692 if (x == 1) { 2693 memset(n, 0, sizeof(struct sysctlnode)); 2694 p->sysctl_csize = 1; 2695 } else { 2696 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode)); 2697 p->sysctl_csize = SYSCTL_DEFSIZE; 2698 } 2699 p->sysctl_clen = 0; 2700 2701 for (i = 0; i < p->sysctl_csize; i++) 2702 n[i].sysctl_parent = p; 2703 2704 p->sysctl_child = n; 2705 return (0); 2706 } 2707 2708 static int 2709 sysctl_realloc(struct sysctlnode *p) 2710 { 2711 int i, j, olen; 2712 struct sysctlnode *n; 2713 2714 assert(p->sysctl_csize == p->sysctl_clen); 2715 2716 /* 2717 * how many do we have...how many should we make? 2718 */ 2719 olen = p->sysctl_clen; 2720 n = malloc(2 * olen * sizeof(struct sysctlnode), M_SYSCTLNODE, 2721 M_WAITOK); 2722 if (n == NULL) 2723 return SET_ERROR(ENOMEM); 2724 2725 /* 2726 * move old children over...initialize new children 2727 */ 2728 memcpy(n, p->sysctl_child, olen * sizeof(struct sysctlnode)); 2729 memset(&n[olen], 0, olen * sizeof(struct sysctlnode)); 2730 p->sysctl_csize = 2 * olen; 2731 2732 /* 2733 * reattach moved (and new) children to parent; if a moved 2734 * child node has children, reattach the parent pointers of 2735 * grandchildren 2736 */ 2737 for (i = 0; i < p->sysctl_csize; i++) { 2738 n[i].sysctl_parent = p; 2739 if (n[i].sysctl_child != NULL) { 2740 for (j = 0; j < n[i].sysctl_csize; j++) 2741 n[i].sysctl_child[j].sysctl_parent = &n[i]; 2742 } 2743 } 2744 2745 /* 2746 * get out with the old and in with the new 2747 */ 2748 free(p->sysctl_child, M_SYSCTLNODE); 2749 p->sysctl_child = n; 2750 2751 return (0); 2752 } 2753 2754 static int 2755 sysctl_log_realloc(struct sysctllog *log) 2756 { 2757 int *n, s, d; 2758 2759 s = log->log_size * 2; 2760 d = log->log_size; 2761 2762 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK); 2763 if (n == NULL) 2764 return (-1); 2765 2766 memset(n, 0, s * sizeof(int)); 2767 memcpy(&n[d], log->log_num, d * sizeof(int)); 2768 free(log->log_num, M_SYSCTLDATA); 2769 log->log_num = n; 2770 if (d) 2771 log->log_left += d; 2772 else 2773 log->log_left = s; 2774 log->log_size = s; 2775 2776 return (0); 2777 } 2778 2779 /* 2780 * ******************************************************************** 2781 * Section 6: Conversion between API versions wrt the sysctlnode 2782 * ******************************************************************** 2783 */ 2784 static int 2785 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz, 2786 struct sysctlnode *node) 2787 { 2788 int error, flags; 2789 2790 if (i == NULL || sz < sizeof(flags)) 2791 return SET_ERROR(EINVAL); 2792 2793 error = sysctl_copyin(l, i, &flags, sizeof(flags)); 2794 if (error) 2795 return error; 2796 2797 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2798 #error sysctl_cvt_in: no support for SYSCTL_VERSION 2799 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2800 2801 if (sz == sizeof(*node) && 2802 SYSCTL_VERS(flags) == SYSCTL_VERSION) { 2803 error = sysctl_copyin(l, i, node, sizeof(*node)); 2804 if (error) 2805 return error; 2806 *vp = SYSCTL_VERSION; 2807 return (0); 2808 } 2809 2810 return SET_ERROR(EINVAL); 2811 } 2812 2813 static int 2814 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i, 2815 void *ovp, size_t left, size_t *szp) 2816 { 2817 size_t sz = sizeof(*i); 2818 const void *src = i; 2819 int error; 2820 2821 switch (v) { 2822 case SYSCTL_VERS_0: 2823 return SET_ERROR(EINVAL); 2824 2825 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2826 #error sysctl_cvt_out: no support for SYSCTL_VERSION 2827 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2828 2829 case SYSCTL_VERSION: 2830 /* nothing more to do here */ 2831 break; 2832 } 2833 2834 if (ovp != NULL && left >= sz) { 2835 error = sysctl_copyout(l, src, ovp, sz); 2836 if (error) 2837 return error; 2838 } 2839 2840 if (szp != NULL) 2841 *szp = sz; 2842 2843 return (0); 2844 } 2845 2846 static uint8_t address_key[32]; /* key used in address hashing */ 2847 static ONCE_DECL(random_inithook); 2848 2849 static int 2850 random_address_init(void) 2851 { 2852 2853 cprng_strong(kern_cprng, address_key, sizeof(address_key), 0); 2854 return 0; 2855 } 2856 2857 void 2858 hash_value_ensure_initialized(void) 2859 { 2860 2861 RUN_ONCE(&random_inithook, random_address_init); 2862 } 2863 2864 void 2865 hash_value(void *d, size_t ds, const void *s, size_t ss) 2866 { 2867 2868 blake2s(d, ds, address_key, sizeof(address_key), s, ss); 2869 } 2870