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