1 /* $NetBSD: kern_sysctl.c,v 1.229 2010/04/11 01:50:25 mrg 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.229 2010/04/11 01:50:25 mrg 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 <machine/stdarg.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 struct sysctllog { 108 const struct sysctlnode *log_root; 109 int *log_num; 110 int log_size, log_left; 111 }; 112 113 /* 114 * the "root" of the new sysctl tree 115 */ 116 struct sysctlnode sysctl_root = { 117 .sysctl_flags = SYSCTL_VERSION| 118 CTLFLAG_ROOT|CTLFLAG_READWRITE| 119 CTLTYPE_NODE, 120 .sysctl_num = 0, 121 /* 122 * XXX once all ports are on gcc3, we can get rid of this 123 * ugliness and simply make it into 124 * 125 * .sysctl_size = sizeof(struct sysctlnode), 126 */ 127 sysc_init_field(_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 static inline 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 static inline 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 static inline 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, f; 225 226 rw_init(&sysctl_treelock); 227 228 /* 229 * dynamic mib numbers start here 230 */ 231 sysctl_root.sysctl_num = CREATE_BASE; 232 233 __link_set_foreach(sysctl_setup, sysctl_funcs) { 234 /* 235 * XXX - why do i have to coerce the pointers like this? 236 */ 237 f = (void*)*sysctl_setup; 238 (*f)(NULL); 239 } 240 241 mutex_init(&sysctl_file_marker_lock, MUTEX_DEFAULT, IPL_NONE); 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 * 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, new) != 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, old), &oldlen, 309 SCARG(uap, new), 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, old) != 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_idata = 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("sysctl_lookup: rnode %p wrong version\n", rnode); 1433 return (EINVAL); 1434 } 1435 1436 error = 0; 1437 1438 /* 1439 * you can't "look up" a node. you can "query" it, but you 1440 * can't "look it up". 1441 */ 1442 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0) 1443 return (EINVAL); 1444 1445 /* 1446 * some nodes are private, so only root can look into them. 1447 */ 1448 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) && 1449 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1450 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) 1451 return (error); 1452 1453 /* 1454 * if a node wants to be writable according to different rules 1455 * other than "only root can write to stuff unless a flag is 1456 * set", then it needs its own function which should have been 1457 * called and not us. 1458 */ 1459 if (l != NULL && newp != NULL && 1460 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) && 1461 (error = kauth_authorize_system(l->l_cred, 1462 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_MODIFY, NULL, NULL, 1463 NULL)) != 0) 1464 return (error); 1465 1466 /* 1467 * is this node supposedly writable? 1468 */ 1469 rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0; 1470 1471 /* 1472 * it appears not to be writable at this time, so if someone 1473 * tried to write to it, we must tell them to go away 1474 */ 1475 if (!rw && newp != NULL) 1476 return (EPERM); 1477 1478 /* 1479 * step one, copy out the stuff we have presently 1480 */ 1481 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) { 1482 /* 1483 * note that we discard const here because we are 1484 * modifying the contents of the node (which is okay 1485 * because it's ours) 1486 */ 1487 switch (SYSCTL_TYPE(rnode->sysctl_flags)) { 1488 case CTLTYPE_BOOL: 1489 d = __UNCONST(&rnode->sysctl_bdata); 1490 break; 1491 case CTLTYPE_INT: 1492 d = __UNCONST(&rnode->sysctl_idata); 1493 break; 1494 case CTLTYPE_QUAD: 1495 d = __UNCONST(&rnode->sysctl_qdata); 1496 break; 1497 default: 1498 return (EINVAL); 1499 } 1500 } else 1501 d = rnode->sysctl_data; 1502 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING) 1503 sz = strlen(d) + 1; /* XXX@@@ possible fault here */ 1504 else 1505 sz = rnode->sysctl_size; 1506 if (oldp != NULL) 1507 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp)); 1508 if (error) 1509 return (error); 1510 *oldlenp = sz; 1511 1512 /* 1513 * are we done? 1514 */ 1515 if (newp == NULL || newlen == 0) 1516 return (0); 1517 1518 /* 1519 * hmm...not done. must now "copy in" new value. re-adjust 1520 * sz to maximum value (strings are "weird"). 1521 */ 1522 sz = rnode->sysctl_size; 1523 switch (SYSCTL_TYPE(rnode->sysctl_flags)) { 1524 case CTLTYPE_BOOL: { 1525 bool tmp; 1526 /* 1527 * these data must be *exactly* the same size coming 1528 * in. bool may only be true or false. 1529 */ 1530 if (newlen != sz) 1531 return (EINVAL); 1532 error = sysctl_copyin(l, newp, &tmp, sz); 1533 if (tmp != true && tmp != false) 1534 return EINVAL; 1535 if (error) 1536 break; 1537 *(bool *)d = tmp; 1538 break; 1539 } 1540 case CTLTYPE_INT: 1541 case CTLTYPE_QUAD: 1542 case CTLTYPE_STRUCT: 1543 /* 1544 * these data must be *exactly* the same size coming 1545 * in. 1546 */ 1547 if (newlen != sz) 1548 return (EINVAL); 1549 error = sysctl_copyin(l, newp, d, sz); 1550 break; 1551 case CTLTYPE_STRING: { 1552 /* 1553 * strings, on the other hand, can be shorter, and we 1554 * let userland be sloppy about the trailing nul. 1555 */ 1556 char *newbuf; 1557 1558 /* 1559 * too much new string? 1560 */ 1561 if (newlen > sz) 1562 return (EINVAL); 1563 1564 /* 1565 * temporary copy of new inbound string 1566 */ 1567 len = MIN(sz, newlen); 1568 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 1569 if (newbuf == NULL) 1570 return (ENOMEM); 1571 error = sysctl_copyin(l, newp, newbuf, len); 1572 if (error) { 1573 free(newbuf, M_SYSCTLDATA); 1574 return (error); 1575 } 1576 1577 /* 1578 * did they null terminate it, or do we have space 1579 * left to do it ourselves? 1580 */ 1581 if (newbuf[len - 1] != '\0' && len == sz) { 1582 free(newbuf, M_SYSCTLDATA); 1583 return (EINVAL); 1584 } 1585 1586 /* 1587 * looks good, so pop it into place and zero the rest. 1588 */ 1589 if (len > 0) 1590 memcpy(d, newbuf, len); 1591 if (sz != len) 1592 memset((char*)d + len, 0, sz - len); 1593 free(newbuf, M_SYSCTLDATA); 1594 break; 1595 } 1596 default: 1597 return (EINVAL); 1598 } 1599 1600 return (error); 1601 } 1602 1603 /* 1604 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that 1605 * purport to handle it. This interface isn't fully fleshed out yet, 1606 * unfortunately. 1607 */ 1608 static int 1609 sysctl_mmap(SYSCTLFN_ARGS) 1610 { 1611 const struct sysctlnode *node; 1612 struct sysctlnode nnode; 1613 int error; 1614 1615 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1616 printf("sysctl_mmap: rnode %p wrong version\n", rnode); 1617 return (EINVAL); 1618 } 1619 1620 /* 1621 * let's just pretend that didn't happen, m'kay? 1622 */ 1623 if (l == NULL) 1624 return (EPERM); 1625 1626 /* 1627 * is this a sysctlnode description of an mmap request? 1628 */ 1629 if (newp == NULL || newlen != sizeof(struct sysctlnode)) 1630 return (EINVAL); 1631 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode)); 1632 if (error) 1633 return (error); 1634 1635 /* 1636 * does the node they asked for exist? 1637 */ 1638 if (namelen != 1) 1639 return (EOPNOTSUPP); 1640 node = rnode; 1641 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL); 1642 if (error) 1643 return (error); 1644 1645 /* 1646 * does this node that we have found purport to handle mmap? 1647 */ 1648 if (node->sysctl_func == NULL || 1649 !(node->sysctl_flags & CTLFLAG_MMAP)) 1650 return (EOPNOTSUPP); 1651 1652 /* 1653 * well...okay, they asked for it. 1654 */ 1655 return ((*node->sysctl_func)(SYSCTLFN_CALL(node))); 1656 } 1657 1658 int 1659 sysctl_describe(SYSCTLFN_ARGS) 1660 { 1661 struct sysctldesc *d; 1662 void *bf; 1663 size_t sz, left, tot; 1664 int i, error, v = -1; 1665 struct sysctlnode *node; 1666 struct sysctlnode dnode; 1667 1668 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1669 printf("sysctl_query: rnode %p wrong version\n", rnode); 1670 return (EINVAL); 1671 } 1672 1673 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 1674 return (ENOTDIR); 1675 if (namelen != 1 || name[0] != CTL_DESCRIBE) 1676 return (EINVAL); 1677 1678 /* 1679 * get ready... 1680 */ 1681 error = 0; 1682 d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK|M_CANFAIL); 1683 if (bf == NULL) 1684 return ENOMEM; 1685 tot = 0; 1686 node = rnode->sysctl_child; 1687 left = *oldlenp; 1688 1689 /* 1690 * no request -> all descriptions at this level 1691 * request with desc unset -> just this node 1692 * request with desc set -> set descr for this node 1693 */ 1694 if (newp != NULL) { 1695 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode); 1696 if (error) 1697 goto out; 1698 if (dnode.sysctl_desc != NULL) { 1699 /* 1700 * processes cannot set descriptions above 1701 * securelevel 0. and must be root. blah 1702 * blah blah. a couple more checks are made 1703 * once we find the node we want. 1704 */ 1705 if (l != NULL) { 1706 #ifndef SYSCTL_DISALLOW_CREATE 1707 error = kauth_authorize_system(l->l_cred, 1708 KAUTH_SYSTEM_SYSCTL, 1709 KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL, 1710 NULL, NULL); 1711 if (error) 1712 goto out; 1713 #else /* SYSCTL_DISALLOW_CREATE */ 1714 error = EPERM; 1715 goto out; 1716 #endif /* SYSCTL_DISALLOW_CREATE */ 1717 } 1718 1719 /* 1720 * find node and try to set the description on it 1721 */ 1722 for (i = 0; i < rnode->sysctl_clen; i++) 1723 if (node[i].sysctl_num == dnode.sysctl_num) 1724 break; 1725 if (i == rnode->sysctl_clen) { 1726 error = ENOENT; 1727 goto out; 1728 } 1729 node = &node[i]; 1730 1731 /* 1732 * did the caller specify a node version? 1733 */ 1734 if (dnode.sysctl_ver != 0 && 1735 dnode.sysctl_ver != node->sysctl_ver) { 1736 error = EINVAL; 1737 goto out; 1738 } 1739 1740 /* 1741 * okay...some rules: 1742 * (1) if setup is done and the tree is 1743 * read-only or the whole system is 1744 * read-only 1745 * (2) no one can set a description on a 1746 * permanent node (it must be set when 1747 * using createv) 1748 * (3) processes cannot *change* a description 1749 * (4) processes *can*, however, set a 1750 * description on a read-only node so that 1751 * one can be created and then described 1752 * in two steps 1753 * anything else come to mind? 1754 */ 1755 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) && 1756 (!(sysctl_rootof(node)->sysctl_flags & 1757 CTLFLAG_READWRITE) || 1758 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) { 1759 error = EPERM; 1760 goto out; 1761 } 1762 if (node->sysctl_flags & CTLFLAG_PERMANENT) { 1763 error = EPERM; 1764 goto out; 1765 } 1766 if (l != NULL && node->sysctl_desc != NULL) { 1767 error = EPERM; 1768 goto out; 1769 } 1770 1771 /* 1772 * right, let's go ahead. the first step is 1773 * making the description into something the 1774 * node can "own", if need be. 1775 */ 1776 if (l != NULL || 1777 dnode.sysctl_flags & CTLFLAG_OWNDESC) { 1778 char *nd, *k; 1779 1780 k = malloc(MAXDESCLEN, M_TEMP, 1781 M_WAITOK|M_CANFAIL); 1782 if (k == NULL) { 1783 error = ENOMEM; 1784 goto out; 1785 } 1786 error = sysctl_copyinstr(l, dnode.sysctl_desc, 1787 k, MAXDESCLEN, &sz); 1788 if (error) { 1789 free(k, M_TEMP); 1790 goto out; 1791 } 1792 nd = malloc(sz, M_SYSCTLDATA, 1793 M_WAITOK|M_CANFAIL); 1794 if (nd == NULL) { 1795 free(k, M_TEMP); 1796 error = ENOMEM; 1797 goto out; 1798 } 1799 memcpy(nd, k, sz); 1800 dnode.sysctl_flags |= CTLFLAG_OWNDESC; 1801 dnode.sysctl_desc = nd; 1802 free(k, M_TEMP); 1803 } 1804 1805 /* 1806 * now "release" the old description and 1807 * attach the new one. ta-da. 1808 */ 1809 if ((node->sysctl_flags & CTLFLAG_OWNDESC) && 1810 node->sysctl_desc != NULL) 1811 /*XXXUNCONST*/ 1812 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA); 1813 node->sysctl_desc = dnode.sysctl_desc; 1814 node->sysctl_flags |= 1815 (dnode.sysctl_flags & CTLFLAG_OWNDESC); 1816 1817 /* 1818 * now we "fall out" and into the loop which 1819 * will copy the new description back out for 1820 * those interested parties 1821 */ 1822 } 1823 } 1824 1825 /* 1826 * scan for one description or just retrieve all descriptions 1827 */ 1828 for (i = 0; i < rnode->sysctl_clen; i++) { 1829 /* 1830 * did they ask for the description of only one node? 1831 */ 1832 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num) 1833 continue; 1834 1835 /* 1836 * don't describe "private" nodes to non-suser users 1837 */ 1838 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) && 1839 !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1840 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL))) 1841 continue; 1842 1843 /* 1844 * is this description "valid"? 1845 */ 1846 memset(bf, 0, MAXDESCLEN); 1847 if (node[i].sysctl_desc == NULL) 1848 sz = 1; 1849 else if (copystr(node[i].sysctl_desc, &d->descr_str[0], 1850 MAXDESCLEN - sizeof(*d), &sz) != 0) { 1851 /* 1852 * erase possible partial description 1853 */ 1854 memset(bf, 0, MAXDESCLEN); 1855 sz = 1; 1856 } 1857 1858 /* 1859 * we've got it, stuff it into the caller's buffer 1860 */ 1861 d->descr_num = node[i].sysctl_num; 1862 d->descr_ver = node[i].sysctl_ver; 1863 d->descr_len = sz; /* includes trailing nul */ 1864 sz = (char *)NEXT_DESCR(d) - (char *)d; 1865 if (oldp != NULL && left >= sz) { 1866 error = sysctl_copyout(l, d, oldp, sz); 1867 if (error) 1868 goto out; 1869 left -= sz; 1870 oldp = (void *)__sysc_desc_adv(oldp, d->descr_len); 1871 } 1872 tot += sz; 1873 1874 /* 1875 * if we get this far with v not "unset", they asked 1876 * for a specific node and we found it 1877 */ 1878 if (v != -1) 1879 break; 1880 } 1881 1882 /* 1883 * did we find it after all? 1884 */ 1885 if (v != -1 && tot == 0) 1886 error = ENOENT; 1887 else 1888 *oldlenp = tot; 1889 1890 out: 1891 free(bf, M_TEMP); 1892 return (error); 1893 } 1894 1895 /* 1896 * ******************************************************************** 1897 * Section 3: Create and destroy from inside the kernel 1898 * ******************************************************************** 1899 * sysctl_createv() and sysctl_destroyv() are simpler-to-use 1900 * interfaces for the kernel to fling new entries into the mib and rip 1901 * them out later. In the case of sysctl_createv(), the returned copy 1902 * of the node (see sysctl_create()) will be translated back into a 1903 * pointer to the actual node. 1904 * 1905 * Note that sysctl_createv() will return 0 if the create request 1906 * matches an existing node (ala mkdir -p), and that sysctl_destroyv() 1907 * will return 0 if the node to be destroyed already does not exist 1908 * (aka rm -f) or if it is a parent of other nodes. 1909 * 1910 * This allows two (or more) different subsystems to assert sub-tree 1911 * existence before populating their own nodes, and to remove their 1912 * own nodes without orphaning the others when they are done. 1913 * ******************************************************************** 1914 */ 1915 int 1916 sysctl_createv(struct sysctllog **log, int cflags, 1917 const struct sysctlnode **rnode, const struct sysctlnode **cnode, 1918 int flags, int type, const char *namep, const char *descr, 1919 sysctlfn func, u_quad_t qv, void *newp, size_t newlen, 1920 ...) 1921 { 1922 va_list ap; 1923 int error, ni, namelen, name[CTL_MAXNAME]; 1924 const struct sysctlnode *root, *pnode; 1925 struct sysctlnode nnode, onode, *dnode; 1926 size_t sz; 1927 1928 /* 1929 * where are we putting this? 1930 */ 1931 if (rnode != NULL && *rnode == NULL) { 1932 printf("sysctl_createv: rnode NULL\n"); 1933 return (EINVAL); 1934 } 1935 root = rnode ? *rnode : NULL; 1936 if (cnode != NULL) 1937 *cnode = NULL; 1938 if (cflags != 0) 1939 return (EINVAL); 1940 1941 /* 1942 * what is it? 1943 */ 1944 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags); 1945 if (log != NULL) 1946 flags &= ~CTLFLAG_PERMANENT; 1947 1948 /* 1949 * where do we put it? 1950 */ 1951 va_start(ap, newlen); 1952 namelen = 0; 1953 ni = -1; 1954 do { 1955 if (++ni == CTL_MAXNAME) 1956 return (ENAMETOOLONG); 1957 name[ni] = va_arg(ap, int); 1958 /* 1959 * sorry, this is not supported from here 1960 */ 1961 if (name[ni] == CTL_CREATESYM) 1962 return (EINVAL); 1963 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE); 1964 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0); 1965 va_end(ap); 1966 1967 /* 1968 * what's it called 1969 */ 1970 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >= 1971 sizeof(nnode.sysctl_name)) 1972 return (ENAMETOOLONG); 1973 1974 /* 1975 * cons up the description of the new node 1976 */ 1977 nnode.sysctl_num = name[namelen - 1]; 1978 name[namelen - 1] = CTL_CREATE; 1979 nnode.sysctl_size = newlen; 1980 nnode.sysctl_flags = flags; 1981 if (type == CTLTYPE_NODE) { 1982 nnode.sysctl_csize = 0; 1983 nnode.sysctl_clen = 0; 1984 nnode.sysctl_child = NULL; 1985 if (flags & CTLFLAG_ALIAS) 1986 nnode.sysctl_alias = qv; 1987 } else if (flags & CTLFLAG_IMMEDIATE) { 1988 switch (type) { 1989 case CTLTYPE_BOOL: 1990 nnode.sysctl_bdata = qv; 1991 break; 1992 case CTLTYPE_INT: 1993 nnode.sysctl_idata = qv; 1994 break; 1995 case CTLTYPE_QUAD: 1996 nnode.sysctl_qdata = qv; 1997 break; 1998 default: 1999 return (EINVAL); 2000 } 2001 } else { 2002 nnode.sysctl_data = newp; 2003 } 2004 nnode.sysctl_func = func; 2005 nnode.sysctl_parent = NULL; 2006 nnode.sysctl_ver = 0; 2007 2008 /* 2009 * initialize lock state -- we need locks if the main tree has 2010 * been marked as complete, but since we could be called from 2011 * either there, or from a device driver (say, at device 2012 * insertion), or from a module (at module load time, say), we 2013 * don't really want to "wait"... 2014 */ 2015 sysctl_lock(true); 2016 2017 /* 2018 * locate the prospective parent of the new node, and if we 2019 * find it, add the new node. 2020 */ 2021 sz = sizeof(onode); 2022 pnode = root; 2023 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2024 if (error) { 2025 printf("sysctl_createv: sysctl_locate(%s) returned %d\n", 2026 nnode.sysctl_name, error); 2027 sysctl_unlock(); 2028 return (error); 2029 } 2030 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz, 2031 &nnode, sizeof(nnode), &name[0], NULL, 2032 pnode); 2033 2034 /* 2035 * unfortunately the node we wanted to create is already 2036 * there. if the node that's already there is a reasonable 2037 * facsimile of the node we wanted to create, just pretend 2038 * (for the caller's benefit) that we managed to create the 2039 * node they wanted. 2040 */ 2041 if (error == EEXIST) { 2042 /* name is the same as requested... */ 2043 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 && 2044 /* they want the same function... */ 2045 nnode.sysctl_func == onode.sysctl_func && 2046 /* number is the same as requested, or... */ 2047 (nnode.sysctl_num == onode.sysctl_num || 2048 /* they didn't pick a number... */ 2049 nnode.sysctl_num == CTL_CREATE)) { 2050 /* 2051 * collision here from trying to create 2052 * something that already existed; let's give 2053 * our customers a hand and tell them they got 2054 * what they wanted. 2055 */ 2056 #ifdef SYSCTL_DEBUG_CREATE 2057 printf("cleared\n"); 2058 #endif /* SYSCTL_DEBUG_CREATE */ 2059 error = 0; 2060 } 2061 } 2062 2063 if (error == 0 && 2064 (cnode != NULL || log != NULL || descr != NULL)) { 2065 /* 2066 * sysctl_create() gave us back a copy of the node, 2067 * but we need to know where it actually is... 2068 */ 2069 pnode = root; 2070 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2071 2072 /* 2073 * manual scan of last layer so that aliased nodes 2074 * aren't followed. 2075 */ 2076 if (error == 0) { 2077 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2078 if (pnode->sysctl_child[ni].sysctl_num == 2079 onode.sysctl_num) 2080 break; 2081 if (ni < pnode->sysctl_clen) 2082 pnode = &pnode->sysctl_child[ni]; 2083 else 2084 error = ENOENT; 2085 } 2086 2087 /* 2088 * not expecting an error here, but... 2089 */ 2090 if (error == 0) { 2091 if (log != NULL) 2092 sysctl_log_add(log, pnode); 2093 if (cnode != NULL) 2094 *cnode = pnode; 2095 if (descr != NULL) { 2096 /* 2097 * allow first caller to *set* a 2098 * description actually to set it 2099 * 2100 * discard const here so we can attach 2101 * the description 2102 */ 2103 dnode = __UNCONST(pnode); 2104 if (pnode->sysctl_desc != NULL) 2105 /* skip it...we've got one */; 2106 else if (flags & CTLFLAG_OWNDESC) { 2107 size_t l = strlen(descr) + 1; 2108 char *d = malloc(l, M_SYSCTLDATA, 2109 M_WAITOK|M_CANFAIL); 2110 if (d != NULL) { 2111 memcpy(d, descr, l); 2112 dnode->sysctl_desc = d; 2113 dnode->sysctl_flags |= 2114 CTLFLAG_OWNDESC; 2115 } 2116 } else 2117 dnode->sysctl_desc = descr; 2118 } 2119 } else { 2120 printf("sysctl_create succeeded but node not found?!\n"); 2121 /* 2122 * confusing, but the create said it 2123 * succeeded, so... 2124 */ 2125 error = 0; 2126 } 2127 } 2128 2129 /* 2130 * now it should be safe to release the lock state. note that 2131 * the pointer to the newly created node being passed back may 2132 * not be "good" for very long. 2133 */ 2134 sysctl_unlock(); 2135 2136 if (error != 0) { 2137 printf("sysctl_createv: sysctl_create(%s) returned %d\n", 2138 nnode.sysctl_name, error); 2139 #if 0 2140 if (error != ENOENT) 2141 sysctl_dump(&onode); 2142 #endif 2143 } 2144 2145 return (error); 2146 } 2147 2148 int 2149 sysctl_destroyv(struct sysctlnode *rnode, ...) 2150 { 2151 va_list ap; 2152 int error, name[CTL_MAXNAME], namelen, ni; 2153 const struct sysctlnode *pnode, *node; 2154 struct sysctlnode dnode, *onode; 2155 size_t sz; 2156 2157 va_start(ap, rnode); 2158 namelen = 0; 2159 ni = 0; 2160 do { 2161 if (ni == CTL_MAXNAME) 2162 return (ENAMETOOLONG); 2163 name[ni] = va_arg(ap, int); 2164 } while (name[ni++] != CTL_EOL); 2165 namelen = ni - 1; 2166 va_end(ap); 2167 2168 /* 2169 * i can't imagine why we'd be destroying a node when the tree 2170 * wasn't complete, but who knows? 2171 */ 2172 sysctl_lock(true); 2173 2174 /* 2175 * where is it? 2176 */ 2177 node = rnode; 2178 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni); 2179 if (error) { 2180 /* they want it gone and it's not there, so... */ 2181 sysctl_unlock(); 2182 return (error == ENOENT ? 0 : error); 2183 } 2184 2185 /* 2186 * set up the deletion 2187 */ 2188 pnode = node; 2189 node = &dnode; 2190 memset(&dnode, 0, sizeof(dnode)); 2191 dnode.sysctl_flags = SYSCTL_VERSION; 2192 dnode.sysctl_num = name[namelen - 1]; 2193 2194 /* 2195 * we found it, now let's nuke it 2196 */ 2197 name[namelen - 1] = CTL_DESTROY; 2198 sz = 0; 2199 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz, 2200 node, sizeof(*node), &name[0], NULL, 2201 pnode); 2202 if (error == ENOTEMPTY) { 2203 /* 2204 * think of trying to delete "foo" when "foo.bar" 2205 * (which someone else put there) is still in 2206 * existence 2207 */ 2208 error = 0; 2209 2210 /* 2211 * dunno who put the description there, but if this 2212 * node can ever be removed, we need to make sure the 2213 * string doesn't go out of context. that means we 2214 * need to find the node that's still there (don't use 2215 * sysctl_locate() because that follows aliasing). 2216 */ 2217 node = pnode->sysctl_child; 2218 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2219 if (node[ni].sysctl_num == dnode.sysctl_num) 2220 break; 2221 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL; 2222 2223 /* 2224 * if we found it, and this node has a description, 2225 * and this node can be released, and it doesn't 2226 * already own its own description...sigh. :) 2227 */ 2228 if (node != NULL && node->sysctl_desc != NULL && 2229 !(node->sysctl_flags & CTLFLAG_PERMANENT) && 2230 !(node->sysctl_flags & CTLFLAG_OWNDESC)) { 2231 char *d; 2232 2233 sz = strlen(node->sysctl_desc) + 1; 2234 d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2235 if (d != NULL) { 2236 /* 2237 * discard const so that we can 2238 * re-attach the description 2239 */ 2240 memcpy(d, node->sysctl_desc, sz); 2241 onode = __UNCONST(node); 2242 onode->sysctl_desc = d; 2243 onode->sysctl_flags |= CTLFLAG_OWNDESC; 2244 } else { 2245 /* 2246 * XXX drop the description? be 2247 * afraid? don't care? 2248 */ 2249 } 2250 } 2251 } 2252 2253 sysctl_unlock(); 2254 2255 return (error); 2256 } 2257 2258 /* 2259 * ******************************************************************** 2260 * Deletes an entire n-ary tree. Not recommended unless you know why 2261 * you're doing it. Personally, I don't know why you'd even think 2262 * about it. 2263 * ******************************************************************** 2264 */ 2265 void 2266 sysctl_free(struct sysctlnode *rnode) 2267 { 2268 struct sysctlnode *node, *pnode; 2269 2270 rw_enter(&sysctl_treelock, RW_WRITER); 2271 2272 if (rnode == NULL) 2273 rnode = &sysctl_root; 2274 2275 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 2276 printf("sysctl_free: rnode %p wrong version\n", rnode); 2277 rw_exit(&sysctl_treelock); 2278 return; 2279 } 2280 2281 pnode = rnode; 2282 2283 node = pnode->sysctl_child; 2284 do { 2285 while (node != NULL && pnode->sysctl_csize > 0) { 2286 while (node < 2287 &pnode->sysctl_child[pnode->sysctl_clen] && 2288 (SYSCTL_TYPE(node->sysctl_flags) != 2289 CTLTYPE_NODE || 2290 node->sysctl_csize == 0)) { 2291 if (SYSCTL_FLAGS(node->sysctl_flags) & 2292 CTLFLAG_OWNDATA) { 2293 if (node->sysctl_data != NULL) { 2294 free(node->sysctl_data, 2295 M_SYSCTLDATA); 2296 node->sysctl_data = NULL; 2297 } 2298 } 2299 if (SYSCTL_FLAGS(node->sysctl_flags) & 2300 CTLFLAG_OWNDESC) { 2301 if (node->sysctl_desc != NULL) { 2302 /*XXXUNCONST*/ 2303 free(__UNCONST(node->sysctl_desc), 2304 M_SYSCTLDATA); 2305 node->sysctl_desc = NULL; 2306 } 2307 } 2308 node++; 2309 } 2310 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) { 2311 pnode = node; 2312 node = node->sysctl_child; 2313 } else 2314 break; 2315 } 2316 if (pnode->sysctl_child != NULL) 2317 free(pnode->sysctl_child, M_SYSCTLNODE); 2318 pnode->sysctl_clen = 0; 2319 pnode->sysctl_csize = 0; 2320 pnode->sysctl_child = NULL; 2321 node = pnode; 2322 pnode = node->sysctl_parent; 2323 } while (pnode != NULL && node != rnode); 2324 2325 rw_exit(&sysctl_treelock); 2326 } 2327 2328 void 2329 sysctl_log_print(const struct sysctllog *slog) 2330 { 2331 int i, len; 2332 2333 printf("root %p left %d size %d content", (const void *)slog->log_root, 2334 slog->log_left, slog->log_size); 2335 2336 for (len = 0, i = slog->log_left; i < slog->log_size; i++) { 2337 switch (len) { 2338 case 0: 2339 len = -1; 2340 printf(" version %d", slog->log_num[i]); 2341 break; 2342 case -1: 2343 len = -2; 2344 printf(" type %d", slog->log_num[i]); 2345 break; 2346 case -2: 2347 len = slog->log_num[i]; 2348 printf(" len %d:", slog->log_num[i]); 2349 if (len <= 0) 2350 len = -1; 2351 break; 2352 default: 2353 len--; 2354 printf(" %d", slog->log_num[i]); 2355 break; 2356 } 2357 } 2358 printf(" end\n"); 2359 } 2360 2361 int 2362 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node) 2363 { 2364 const int size0 = 16; 2365 int name[CTL_MAXNAME], namelen, i; 2366 const struct sysctlnode *pnode; 2367 struct sysctllog *log; 2368 2369 if (node->sysctl_flags & CTLFLAG_PERMANENT) 2370 return (0); 2371 2372 if (logp == NULL) 2373 return (0); 2374 2375 if (*logp == NULL) { 2376 log = malloc(sizeof(struct sysctllog), 2377 M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2378 if (log == NULL) { 2379 /* XXX print error message? */ 2380 return (-1); 2381 } 2382 log->log_num = malloc(size0 * sizeof(int), 2383 M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2384 if (log->log_num == NULL) { 2385 /* XXX print error message? */ 2386 free(log, M_SYSCTLDATA); 2387 return (-1); 2388 } 2389 memset(log->log_num, 0, size0 * sizeof(int)); 2390 log->log_root = NULL; 2391 log->log_size = size0; 2392 log->log_left = size0; 2393 *logp = log; 2394 } else 2395 log = *logp; 2396 2397 /* 2398 * check that the root is proper. it's okay to record the 2399 * address of the root of a tree. it's the only thing that's 2400 * guaranteed not to shift around as nodes come and go. 2401 */ 2402 if (log->log_root == NULL) 2403 log->log_root = sysctl_rootof(node); 2404 else if (log->log_root != sysctl_rootof(node)) { 2405 printf("sysctl: log %p root mismatch (%p)\n", 2406 log->log_root, sysctl_rootof(node)); 2407 return (-1); 2408 } 2409 2410 /* 2411 * we will copy out name in reverse order 2412 */ 2413 for (pnode = node, namelen = 0; 2414 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT); 2415 pnode = pnode->sysctl_parent) 2416 name[namelen++] = pnode->sysctl_num; 2417 2418 /* 2419 * do we have space? 2420 */ 2421 if (log->log_left < (namelen + 3)) 2422 sysctl_log_realloc(log); 2423 if (log->log_left < (namelen + 3)) 2424 return (-1); 2425 2426 /* 2427 * stuff name in, then namelen, then node type, and finally, 2428 * the version for non-node nodes. 2429 */ 2430 for (i = 0; i < namelen; i++) 2431 log->log_num[--log->log_left] = name[i]; 2432 log->log_num[--log->log_left] = namelen; 2433 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags); 2434 if (log->log_num[log->log_left] != CTLTYPE_NODE) 2435 log->log_num[--log->log_left] = node->sysctl_ver; 2436 else 2437 log->log_num[--log->log_left] = 0; 2438 2439 return (0); 2440 } 2441 2442 void 2443 sysctl_teardown(struct sysctllog **logp) 2444 { 2445 const struct sysctlnode *rnode; 2446 struct sysctlnode node; 2447 struct sysctllog *log; 2448 uint namelen; 2449 int *name, t, v, error, ni; 2450 size_t sz; 2451 2452 if (logp == NULL || *logp == NULL) 2453 return; 2454 log = *logp; 2455 2456 rw_enter(&sysctl_treelock, RW_WRITER); 2457 memset(&node, 0, sizeof(node)); 2458 2459 while (log->log_left < log->log_size) { 2460 KASSERT((log->log_left + 3 < log->log_size) && 2461 (log->log_left + log->log_num[log->log_left + 2] <= 2462 log->log_size)); 2463 v = log->log_num[log->log_left++]; 2464 t = log->log_num[log->log_left++]; 2465 namelen = log->log_num[log->log_left++]; 2466 name = &log->log_num[log->log_left]; 2467 2468 node.sysctl_num = name[namelen - 1]; 2469 node.sysctl_flags = SYSCTL_VERSION|t; 2470 node.sysctl_ver = v; 2471 2472 rnode = log->log_root; 2473 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni); 2474 if (error == 0) { 2475 name[namelen - 1] = CTL_DESTROY; 2476 rnode = rnode->sysctl_parent; 2477 sz = 0; 2478 (void)sysctl_destroy(&name[namelen - 1], 1, NULL, 2479 &sz, &node, sizeof(node), 2480 &name[0], NULL, rnode); 2481 } 2482 2483 log->log_left += namelen; 2484 } 2485 2486 KASSERT(log->log_size == log->log_left); 2487 free(log->log_num, M_SYSCTLDATA); 2488 free(log, M_SYSCTLDATA); 2489 *logp = NULL; 2490 2491 rw_exit(&sysctl_treelock); 2492 } 2493 2494 /* 2495 * ******************************************************************** 2496 * old_sysctl -- A routine to bridge old-style internal calls to the 2497 * new infrastructure. 2498 * ******************************************************************** 2499 */ 2500 int 2501 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2502 void *newp, size_t newlen, struct lwp *l) 2503 { 2504 int error; 2505 size_t oldlen = 0; 2506 size_t savelen; 2507 2508 if (oldlenp) { 2509 oldlen = *oldlenp; 2510 } 2511 savelen = oldlen; 2512 2513 sysctl_lock(newp != NULL); 2514 error = sysctl_dispatch(name, namelen, oldp, &oldlen, 2515 newp, newlen, name, l, NULL); 2516 sysctl_unlock(); 2517 if (error == 0 && oldp != NULL && savelen < oldlen) 2518 error = ENOMEM; 2519 if (oldlenp) { 2520 *oldlenp = oldlen; 2521 } 2522 2523 return (error); 2524 } 2525 2526 /* 2527 * ******************************************************************** 2528 * Section 4: Generic helper routines 2529 * ******************************************************************** 2530 * "helper" routines that can do more finely grained access control, 2531 * construct structures from disparate information, create the 2532 * appearance of more nodes and sub-trees, etc. for example, if 2533 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY 2534 * with a dynamically created list of nodes that represented the 2535 * currently running processes at that instant. 2536 * ******************************************************************** 2537 */ 2538 2539 /* 2540 * first, a few generic helpers that provide: 2541 * 2542 * sysctl_needfunc() a readonly interface that emits a warning 2543 * sysctl_notavail() returns EOPNOTSUPP (generic error) 2544 * sysctl_null() an empty return buffer with no error 2545 */ 2546 int 2547 sysctl_needfunc(SYSCTLFN_ARGS) 2548 { 2549 int error; 2550 2551 printf("!!SYSCTL_NEEDFUNC!!\n"); 2552 2553 if (newp != NULL || namelen != 0) 2554 return (EOPNOTSUPP); 2555 2556 error = 0; 2557 if (oldp != NULL) 2558 error = sysctl_copyout(l, rnode->sysctl_data, oldp, 2559 MIN(rnode->sysctl_size, *oldlenp)); 2560 *oldlenp = rnode->sysctl_size; 2561 2562 return (error); 2563 } 2564 2565 int 2566 sysctl_notavail(SYSCTLFN_ARGS) 2567 { 2568 2569 if (namelen == 1 && name[0] == CTL_QUERY) 2570 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2571 2572 return (EOPNOTSUPP); 2573 } 2574 2575 int 2576 sysctl_null(SYSCTLFN_ARGS) 2577 { 2578 2579 *oldlenp = 0; 2580 2581 return (0); 2582 } 2583 2584 /* 2585 * ******************************************************************** 2586 * Section 5: The machinery that makes it all go 2587 * ******************************************************************** 2588 * Memory "manglement" routines. Not much to this, eh? 2589 * ******************************************************************** 2590 */ 2591 static int 2592 sysctl_alloc(struct sysctlnode *p, int x) 2593 { 2594 int i; 2595 struct sysctlnode *n; 2596 2597 assert(p->sysctl_child == NULL); 2598 2599 if (x == 1) 2600 n = malloc(sizeof(struct sysctlnode), 2601 M_SYSCTLNODE, M_WAITOK|M_CANFAIL); 2602 else 2603 n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode), 2604 M_SYSCTLNODE, M_WAITOK|M_CANFAIL); 2605 if (n == NULL) 2606 return (ENOMEM); 2607 2608 if (x == 1) { 2609 memset(n, 0, sizeof(struct sysctlnode)); 2610 p->sysctl_csize = 1; 2611 } else { 2612 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode)); 2613 p->sysctl_csize = SYSCTL_DEFSIZE; 2614 } 2615 p->sysctl_clen = 0; 2616 2617 for (i = 0; i < p->sysctl_csize; i++) 2618 n[i].sysctl_parent = p; 2619 2620 p->sysctl_child = n; 2621 return (0); 2622 } 2623 2624 static int 2625 sysctl_realloc(struct sysctlnode *p) 2626 { 2627 int i, j, olen; 2628 struct sysctlnode *n; 2629 2630 assert(p->sysctl_csize == p->sysctl_clen); 2631 2632 /* 2633 * how many do we have...how many should we make? 2634 */ 2635 olen = p->sysctl_clen; 2636 n = malloc(2 * olen * sizeof(struct sysctlnode), M_SYSCTLNODE, 2637 M_WAITOK|M_CANFAIL); 2638 if (n == NULL) 2639 return (ENOMEM); 2640 2641 /* 2642 * move old children over...initialize new children 2643 */ 2644 memcpy(n, p->sysctl_child, olen * sizeof(struct sysctlnode)); 2645 memset(&n[olen], 0, olen * sizeof(struct sysctlnode)); 2646 p->sysctl_csize = 2 * olen; 2647 2648 /* 2649 * reattach moved (and new) children to parent; if a moved 2650 * child node has children, reattach the parent pointers of 2651 * grandchildren 2652 */ 2653 for (i = 0; i < p->sysctl_csize; i++) { 2654 n[i].sysctl_parent = p; 2655 if (n[i].sysctl_child != NULL) { 2656 for (j = 0; j < n[i].sysctl_csize; j++) 2657 n[i].sysctl_child[j].sysctl_parent = &n[i]; 2658 } 2659 } 2660 2661 /* 2662 * get out with the old and in with the new 2663 */ 2664 free(p->sysctl_child, M_SYSCTLNODE); 2665 p->sysctl_child = n; 2666 2667 return (0); 2668 } 2669 2670 static int 2671 sysctl_log_realloc(struct sysctllog *log) 2672 { 2673 int *n, s, d; 2674 2675 s = log->log_size * 2; 2676 d = log->log_size; 2677 2678 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2679 if (n == NULL) 2680 return (-1); 2681 2682 memset(n, 0, s * sizeof(int)); 2683 memcpy(&n[d], log->log_num, d * sizeof(int)); 2684 free(log->log_num, M_SYSCTLDATA); 2685 log->log_num = n; 2686 if (d) 2687 log->log_left += d; 2688 else 2689 log->log_left = s; 2690 log->log_size = s; 2691 2692 return (0); 2693 } 2694 2695 /* 2696 * ******************************************************************** 2697 * Section 6: Conversion between API versions wrt the sysctlnode 2698 * ******************************************************************** 2699 */ 2700 static int 2701 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz, 2702 struct sysctlnode *node) 2703 { 2704 int error, flags; 2705 2706 if (i == NULL || sz < sizeof(flags)) 2707 return (EINVAL); 2708 2709 error = sysctl_copyin(l, i, &flags, sizeof(flags)); 2710 if (error) 2711 return (error); 2712 2713 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2714 #error sysctl_cvt_in: no support for SYSCTL_VERSION 2715 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2716 2717 if (sz == sizeof(*node) && 2718 SYSCTL_VERS(flags) == SYSCTL_VERSION) { 2719 error = sysctl_copyin(l, i, node, sizeof(*node)); 2720 if (error) 2721 return (error); 2722 *vp = SYSCTL_VERSION; 2723 return (0); 2724 } 2725 2726 return (EINVAL); 2727 } 2728 2729 static int 2730 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i, 2731 void *ovp, size_t left, size_t *szp) 2732 { 2733 size_t sz = sizeof(*i); 2734 const void *src = i; 2735 int error; 2736 2737 switch (v) { 2738 case SYSCTL_VERS_0: 2739 return (EINVAL); 2740 2741 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2742 #error sysctl_cvt_out: no support for SYSCTL_VERSION 2743 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2744 2745 case SYSCTL_VERSION: 2746 /* nothing more to do here */ 2747 break; 2748 } 2749 2750 if (ovp != NULL && left >= sz) { 2751 error = sysctl_copyout(l, src, ovp, sz); 2752 if (error) 2753 return (error); 2754 } 2755 2756 if (szp != NULL) 2757 *szp = sz; 2758 2759 return (0); 2760 } 2761