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