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