1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Karels at Berkeley Software Design, Inc. 7 * 8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 9 * project, to make these variables more userfriendly. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_capsicum.h" 42 #include "opt_compat.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/fail.h> 47 #include <sys/systm.h> 48 #include <sys/capsicum.h> 49 #include <sys/kernel.h> 50 #include <sys/sysctl.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 #include <sys/proc.h> 54 #include <sys/jail.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/sbuf.h> 58 #include <sys/sx.h> 59 #include <sys/sysproto.h> 60 #include <sys/uio.h> 61 #ifdef KTRACE 62 #include <sys/ktrace.h> 63 #endif 64 65 #include <net/vnet.h> 66 67 #include <security/mac/mac_framework.h> 68 69 #include <vm/vm.h> 70 #include <vm/vm_extern.h> 71 72 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 73 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 74 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 75 76 /* 77 * The sysctllock protects the MIB tree. It also protects sysctl 78 * contexts used with dynamic sysctls. The sysctl_register_oid() and 79 * sysctl_unregister_oid() routines require the sysctllock to already 80 * be held, so the sysctl_lock() and sysctl_unlock() routines are 81 * provided for the few places in the kernel which need to use that 82 * API rather than using the dynamic API. Use of the dynamic API is 83 * strongly encouraged for most code. 84 * 85 * The sysctlmemlock is used to limit the amount of user memory wired for 86 * sysctl requests. This is implemented by serializing any userland 87 * sysctl requests larger than a single page via an exclusive lock. 88 */ 89 static struct sx sysctllock; 90 static struct sx sysctlmemlock; 91 92 #define SYSCTL_XLOCK() sx_xlock(&sysctllock) 93 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 94 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 95 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 96 #define SYSCTL_SLEEP(ch, wmesg, timo) \ 97 sx_sleep(ch, &sysctllock, 0, wmesg, timo) 98 99 static int sysctl_root(SYSCTL_HANDLER_ARGS); 100 101 /* Root list */ 102 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children); 103 104 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 105 int recurse); 106 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 107 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 108 109 static struct sysctl_oid * 110 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 111 { 112 struct sysctl_oid *oidp; 113 114 SYSCTL_ASSERT_XLOCKED(); 115 SLIST_FOREACH(oidp, list, oid_link) { 116 if (strcmp(oidp->oid_name, name) == 0) { 117 return (oidp); 118 } 119 } 120 return (NULL); 121 } 122 123 /* 124 * Initialization of the MIB tree. 125 * 126 * Order by number in each list. 127 */ 128 void 129 sysctl_lock(void) 130 { 131 132 SYSCTL_XLOCK(); 133 } 134 135 void 136 sysctl_unlock(void) 137 { 138 139 SYSCTL_XUNLOCK(); 140 } 141 142 static int 143 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2, 144 struct sysctl_req *req) 145 { 146 int error; 147 148 oid->oid_running++; 149 SYSCTL_XUNLOCK(); 150 151 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 152 mtx_lock(&Giant); 153 error = oid->oid_handler(oid, arg1, arg2, req); 154 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 155 mtx_unlock(&Giant); 156 157 SYSCTL_XLOCK(); 158 oid->oid_running--; 159 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0) 160 wakeup(&oid->oid_running); 161 162 return (error); 163 } 164 165 static void 166 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp) 167 { 168 struct sysctl_req req; 169 struct sysctl_oid *curr; 170 char *penv = NULL; 171 char path[64]; 172 ssize_t rem = sizeof(path); 173 ssize_t len; 174 int val_int; 175 long val_long; 176 int64_t val_64; 177 quad_t val_quad; 178 int error; 179 180 path[--rem] = 0; 181 182 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) { 183 len = strlen(curr->oid_name); 184 rem -= len; 185 if (curr != oidp) 186 rem -= 1; 187 if (rem < 0) { 188 printf("OID path exceeds %d bytes\n", (int)sizeof(path)); 189 return; 190 } 191 memcpy(path + rem, curr->oid_name, len); 192 if (curr != oidp) 193 path[rem + len] = '.'; 194 } 195 196 memset(&req, 0, sizeof(req)); 197 198 req.td = curthread; 199 req.oldfunc = sysctl_old_kernel; 200 req.newfunc = sysctl_new_kernel; 201 req.lock = REQ_UNWIRED; 202 203 switch (oidp->oid_kind & CTLTYPE) { 204 case CTLTYPE_INT: 205 if (getenv_int(path + rem, &val_int) == 0) 206 return; 207 req.newlen = sizeof(val_int); 208 req.newptr = &val_int; 209 break; 210 case CTLTYPE_UINT: 211 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0) 212 return; 213 req.newlen = sizeof(val_int); 214 req.newptr = &val_int; 215 break; 216 case CTLTYPE_LONG: 217 if (getenv_long(path + rem, &val_long) == 0) 218 return; 219 req.newlen = sizeof(val_long); 220 req.newptr = &val_long; 221 break; 222 case CTLTYPE_ULONG: 223 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0) 224 return; 225 req.newlen = sizeof(val_long); 226 req.newptr = &val_long; 227 break; 228 case CTLTYPE_S64: 229 if (getenv_quad(path + rem, &val_quad) == 0) 230 return; 231 val_64 = val_quad; 232 req.newlen = sizeof(val_64); 233 req.newptr = &val_64; 234 break; 235 case CTLTYPE_U64: 236 /* XXX there is no getenv_uquad() */ 237 if (getenv_quad(path + rem, &val_quad) == 0) 238 return; 239 val_64 = val_quad; 240 req.newlen = sizeof(val_64); 241 req.newptr = &val_64; 242 break; 243 case CTLTYPE_STRING: 244 penv = getenv(path + rem); 245 if (penv == NULL) 246 return; 247 req.newlen = strlen(penv); 248 req.newptr = penv; 249 break; 250 default: 251 return; 252 } 253 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1, 254 oidp->oid_arg2, &req); 255 if (error != 0) { 256 printf("Setting sysctl '%s' to '%s' failed: %d\n", 257 path, penv, error); 258 } 259 freeenv(penv); 260 } 261 262 void 263 sysctl_register_oid(struct sysctl_oid *oidp) 264 { 265 struct sysctl_oid_list *parent = oidp->oid_parent; 266 struct sysctl_oid *p; 267 struct sysctl_oid *q; 268 269 /* 270 * First check if another oid with the same name already 271 * exists in the parent's list. 272 */ 273 SYSCTL_ASSERT_XLOCKED(); 274 p = sysctl_find_oidname(oidp->oid_name, parent); 275 if (p != NULL) { 276 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 277 p->oid_refcnt++; 278 return; 279 } else { 280 printf("can't re-use a leaf (%s)!\n", p->oid_name); 281 return; 282 } 283 } 284 /* 285 * If this oid has a number OID_AUTO, give it a number which 286 * is greater than any current oid. 287 * NOTE: DO NOT change the starting value here, change it in 288 * <sys/sysctl.h>, and make sure it is at least 256 to 289 * accomodate e.g. net.inet.raw as a static sysctl node. 290 */ 291 if (oidp->oid_number == OID_AUTO) { 292 static int newoid = CTL_AUTO_START; 293 294 oidp->oid_number = newoid++; 295 if (newoid == 0x7fffffff) 296 panic("out of oids"); 297 } 298 #if 0 299 else if (oidp->oid_number >= CTL_AUTO_START) { 300 /* do not panic; this happens when unregistering sysctl sets */ 301 printf("static sysctl oid too high: %d", oidp->oid_number); 302 } 303 #endif 304 305 /* 306 * Insert the oid into the parent's list in order. 307 */ 308 q = NULL; 309 SLIST_FOREACH(p, parent, oid_link) { 310 if (oidp->oid_number < p->oid_number) 311 break; 312 q = p; 313 } 314 if (q) 315 SLIST_INSERT_AFTER(q, oidp, oid_link); 316 else 317 SLIST_INSERT_HEAD(parent, oidp, oid_link); 318 319 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE && 320 #ifdef VIMAGE 321 (oidp->oid_kind & CTLFLAG_VNET) == 0 && 322 #endif 323 (oidp->oid_kind & CTLFLAG_TUN) != 0 && 324 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) { 325 sysctl_load_tunable_by_oid_locked(oidp); 326 } 327 } 328 329 void 330 sysctl_unregister_oid(struct sysctl_oid *oidp) 331 { 332 struct sysctl_oid *p; 333 int error; 334 335 SYSCTL_ASSERT_XLOCKED(); 336 error = ENOENT; 337 if (oidp->oid_number == OID_AUTO) { 338 error = EINVAL; 339 } else { 340 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 341 if (p == oidp) { 342 SLIST_REMOVE(oidp->oid_parent, oidp, 343 sysctl_oid, oid_link); 344 error = 0; 345 break; 346 } 347 } 348 } 349 350 /* 351 * This can happen when a module fails to register and is 352 * being unloaded afterwards. It should not be a panic() 353 * for normal use. 354 */ 355 if (error) 356 printf("%s: failed to unregister sysctl\n", __func__); 357 } 358 359 /* Initialize a new context to keep track of dynamically added sysctls. */ 360 int 361 sysctl_ctx_init(struct sysctl_ctx_list *c) 362 { 363 364 if (c == NULL) { 365 return (EINVAL); 366 } 367 368 /* 369 * No locking here, the caller is responsible for not adding 370 * new nodes to a context until after this function has 371 * returned. 372 */ 373 TAILQ_INIT(c); 374 return (0); 375 } 376 377 /* Free the context, and destroy all dynamic oids registered in this context */ 378 int 379 sysctl_ctx_free(struct sysctl_ctx_list *clist) 380 { 381 struct sysctl_ctx_entry *e, *e1; 382 int error; 383 384 error = 0; 385 /* 386 * First perform a "dry run" to check if it's ok to remove oids. 387 * XXX FIXME 388 * XXX This algorithm is a hack. But I don't know any 389 * XXX better solution for now... 390 */ 391 SYSCTL_XLOCK(); 392 TAILQ_FOREACH(e, clist, link) { 393 error = sysctl_remove_oid_locked(e->entry, 0, 0); 394 if (error) 395 break; 396 } 397 /* 398 * Restore deregistered entries, either from the end, 399 * or from the place where error occured. 400 * e contains the entry that was not unregistered 401 */ 402 if (error) 403 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 404 else 405 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 406 while (e1 != NULL) { 407 sysctl_register_oid(e1->entry); 408 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 409 } 410 if (error) { 411 SYSCTL_XUNLOCK(); 412 return(EBUSY); 413 } 414 /* Now really delete the entries */ 415 e = TAILQ_FIRST(clist); 416 while (e != NULL) { 417 e1 = TAILQ_NEXT(e, link); 418 error = sysctl_remove_oid_locked(e->entry, 1, 0); 419 if (error) 420 panic("sysctl_remove_oid: corrupt tree, entry: %s", 421 e->entry->oid_name); 422 free(e, M_SYSCTLOID); 423 e = e1; 424 } 425 SYSCTL_XUNLOCK(); 426 return (error); 427 } 428 429 /* Add an entry to the context */ 430 struct sysctl_ctx_entry * 431 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 432 { 433 struct sysctl_ctx_entry *e; 434 435 SYSCTL_ASSERT_XLOCKED(); 436 if (clist == NULL || oidp == NULL) 437 return(NULL); 438 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 439 e->entry = oidp; 440 TAILQ_INSERT_HEAD(clist, e, link); 441 return (e); 442 } 443 444 /* Find an entry in the context */ 445 struct sysctl_ctx_entry * 446 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 447 { 448 struct sysctl_ctx_entry *e; 449 450 SYSCTL_ASSERT_XLOCKED(); 451 if (clist == NULL || oidp == NULL) 452 return(NULL); 453 TAILQ_FOREACH(e, clist, link) { 454 if(e->entry == oidp) 455 return(e); 456 } 457 return (e); 458 } 459 460 /* 461 * Delete an entry from the context. 462 * NOTE: this function doesn't free oidp! You have to remove it 463 * with sysctl_remove_oid(). 464 */ 465 int 466 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 467 { 468 struct sysctl_ctx_entry *e; 469 470 if (clist == NULL || oidp == NULL) 471 return (EINVAL); 472 SYSCTL_XLOCK(); 473 e = sysctl_ctx_entry_find(clist, oidp); 474 if (e != NULL) { 475 TAILQ_REMOVE(clist, e, link); 476 SYSCTL_XUNLOCK(); 477 free(e, M_SYSCTLOID); 478 return (0); 479 } else { 480 SYSCTL_XUNLOCK(); 481 return (ENOENT); 482 } 483 } 484 485 /* 486 * Remove dynamically created sysctl trees. 487 * oidp - top of the tree to be removed 488 * del - if 0 - just deregister, otherwise free up entries as well 489 * recurse - if != 0 traverse the subtree to be deleted 490 */ 491 int 492 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 493 { 494 int error; 495 496 SYSCTL_XLOCK(); 497 error = sysctl_remove_oid_locked(oidp, del, recurse); 498 SYSCTL_XUNLOCK(); 499 return (error); 500 } 501 502 int 503 sysctl_remove_name(struct sysctl_oid *parent, const char *name, 504 int del, int recurse) 505 { 506 struct sysctl_oid *p, *tmp; 507 int error; 508 509 error = ENOENT; 510 SYSCTL_XLOCK(); 511 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) { 512 if (strcmp(p->oid_name, name) == 0) { 513 error = sysctl_remove_oid_locked(p, del, recurse); 514 break; 515 } 516 } 517 SYSCTL_XUNLOCK(); 518 519 return (error); 520 } 521 522 523 static int 524 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 525 { 526 struct sysctl_oid *p, *tmp; 527 int error; 528 529 SYSCTL_ASSERT_XLOCKED(); 530 if (oidp == NULL) 531 return(EINVAL); 532 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 533 printf("can't remove non-dynamic nodes!\n"); 534 return (EINVAL); 535 } 536 /* 537 * WARNING: normal method to do this should be through 538 * sysctl_ctx_free(). Use recursing as the last resort 539 * method to purge your sysctl tree of leftovers... 540 * However, if some other code still references these nodes, 541 * it will panic. 542 */ 543 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 544 if (oidp->oid_refcnt == 1) { 545 SLIST_FOREACH_SAFE(p, 546 SYSCTL_CHILDREN(oidp), oid_link, tmp) { 547 if (!recurse) { 548 printf("Warning: failed attempt to " 549 "remove oid %s with child %s\n", 550 oidp->oid_name, p->oid_name); 551 return (ENOTEMPTY); 552 } 553 error = sysctl_remove_oid_locked(p, del, 554 recurse); 555 if (error) 556 return (error); 557 } 558 } 559 } 560 if (oidp->oid_refcnt > 1 ) { 561 oidp->oid_refcnt--; 562 } else { 563 if (oidp->oid_refcnt == 0) { 564 printf("Warning: bad oid_refcnt=%u (%s)!\n", 565 oidp->oid_refcnt, oidp->oid_name); 566 return (EINVAL); 567 } 568 sysctl_unregister_oid(oidp); 569 if (del) { 570 /* 571 * Wait for all threads running the handler to drain. 572 * This preserves the previous behavior when the 573 * sysctl lock was held across a handler invocation, 574 * and is necessary for module unload correctness. 575 */ 576 while (oidp->oid_running > 0) { 577 oidp->oid_kind |= CTLFLAG_DYING; 578 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0); 579 } 580 if (oidp->oid_descr) 581 free(__DECONST(char *, oidp->oid_descr), 582 M_SYSCTLOID); 583 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID); 584 free(oidp, M_SYSCTLOID); 585 } 586 } 587 return (0); 588 } 589 /* 590 * Create new sysctls at run time. 591 * clist may point to a valid context initialized with sysctl_ctx_init(). 592 */ 593 struct sysctl_oid * 594 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 595 int number, const char *name, int kind, void *arg1, intptr_t arg2, 596 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 597 { 598 struct sysctl_oid *oidp; 599 600 /* You have to hook up somewhere.. */ 601 if (parent == NULL) 602 return(NULL); 603 /* Check if the node already exists, otherwise create it */ 604 SYSCTL_XLOCK(); 605 oidp = sysctl_find_oidname(name, parent); 606 if (oidp != NULL) { 607 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 608 oidp->oid_refcnt++; 609 /* Update the context */ 610 if (clist != NULL) 611 sysctl_ctx_entry_add(clist, oidp); 612 SYSCTL_XUNLOCK(); 613 return (oidp); 614 } else { 615 SYSCTL_XUNLOCK(); 616 printf("can't re-use a leaf (%s)!\n", name); 617 return (NULL); 618 } 619 } 620 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 621 oidp->oid_parent = parent; 622 SLIST_INIT(&oidp->oid_children); 623 oidp->oid_number = number; 624 oidp->oid_refcnt = 1; 625 oidp->oid_name = strdup(name, M_SYSCTLOID); 626 oidp->oid_handler = handler; 627 oidp->oid_kind = CTLFLAG_DYN | kind; 628 oidp->oid_arg1 = arg1; 629 oidp->oid_arg2 = arg2; 630 oidp->oid_fmt = fmt; 631 if (descr != NULL) 632 oidp->oid_descr = strdup(descr, M_SYSCTLOID); 633 /* Update the context, if used */ 634 if (clist != NULL) 635 sysctl_ctx_entry_add(clist, oidp); 636 /* Register this oid */ 637 sysctl_register_oid(oidp); 638 SYSCTL_XUNLOCK(); 639 return (oidp); 640 } 641 642 /* 643 * Rename an existing oid. 644 */ 645 void 646 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 647 { 648 char *newname; 649 char *oldname; 650 651 newname = strdup(name, M_SYSCTLOID); 652 SYSCTL_XLOCK(); 653 oldname = __DECONST(char *, oidp->oid_name); 654 oidp->oid_name = newname; 655 SYSCTL_XUNLOCK(); 656 free(oldname, M_SYSCTLOID); 657 } 658 659 /* 660 * Reparent an existing oid. 661 */ 662 int 663 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 664 { 665 struct sysctl_oid *oidp; 666 667 SYSCTL_XLOCK(); 668 if (oid->oid_parent == parent) { 669 SYSCTL_XUNLOCK(); 670 return (0); 671 } 672 oidp = sysctl_find_oidname(oid->oid_name, parent); 673 if (oidp != NULL) { 674 SYSCTL_XUNLOCK(); 675 return (EEXIST); 676 } 677 sysctl_unregister_oid(oid); 678 oid->oid_parent = parent; 679 oid->oid_number = OID_AUTO; 680 sysctl_register_oid(oid); 681 SYSCTL_XUNLOCK(); 682 return (0); 683 } 684 685 /* 686 * Register the kernel's oids on startup. 687 */ 688 SET_DECLARE(sysctl_set, struct sysctl_oid); 689 690 static void 691 sysctl_register_all(void *arg) 692 { 693 struct sysctl_oid **oidp; 694 695 sx_init(&sysctlmemlock, "sysctl mem"); 696 SYSCTL_INIT(); 697 SYSCTL_XLOCK(); 698 SET_FOREACH(oidp, sysctl_set) 699 sysctl_register_oid(*oidp); 700 SYSCTL_XUNLOCK(); 701 } 702 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0); 703 704 /* 705 * "Staff-functions" 706 * 707 * These functions implement a presently undocumented interface 708 * used by the sysctl program to walk the tree, and get the type 709 * so it can print the value. 710 * This interface is under work and consideration, and should probably 711 * be killed with a big axe by the first person who can find the time. 712 * (be aware though, that the proper interface isn't as obvious as it 713 * may seem, there are various conflicting requirements. 714 * 715 * {0,0} printf the entire MIB-tree. 716 * {0,1,...} return the name of the "..." OID. 717 * {0,2,...} return the next OID. 718 * {0,3} return the OID of the name in "new" 719 * {0,4,...} return the kind & format info for the "..." OID. 720 * {0,5,...} return the description the "..." OID. 721 */ 722 723 #ifdef SYSCTL_DEBUG 724 static void 725 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 726 { 727 int k; 728 struct sysctl_oid *oidp; 729 730 SYSCTL_ASSERT_XLOCKED(); 731 SLIST_FOREACH(oidp, l, oid_link) { 732 733 for (k=0; k<i; k++) 734 printf(" "); 735 736 printf("%d %s ", oidp->oid_number, oidp->oid_name); 737 738 printf("%c%c", 739 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 740 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 741 742 if (oidp->oid_handler) 743 printf(" *Handler"); 744 745 switch (oidp->oid_kind & CTLTYPE) { 746 case CTLTYPE_NODE: 747 printf(" Node\n"); 748 if (!oidp->oid_handler) { 749 sysctl_sysctl_debug_dump_node( 750 SYSCTL_CHILDREN(oidp), i + 2); 751 } 752 break; 753 case CTLTYPE_INT: printf(" Int\n"); break; 754 case CTLTYPE_UINT: printf(" u_int\n"); break; 755 case CTLTYPE_LONG: printf(" Long\n"); break; 756 case CTLTYPE_ULONG: printf(" u_long\n"); break; 757 case CTLTYPE_STRING: printf(" String\n"); break; 758 case CTLTYPE_U64: printf(" uint64_t\n"); break; 759 case CTLTYPE_S64: printf(" int64_t\n"); break; 760 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 761 default: printf("\n"); 762 } 763 764 } 765 } 766 767 static int 768 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 769 { 770 int error; 771 772 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 773 if (error) 774 return (error); 775 SYSCTL_XLOCK(); 776 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 777 SYSCTL_XUNLOCK(); 778 return (ENOENT); 779 } 780 781 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 782 0, 0, sysctl_sysctl_debug, "-", ""); 783 #endif 784 785 static int 786 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 787 { 788 int *name = (int *) arg1; 789 u_int namelen = arg2; 790 int error = 0; 791 struct sysctl_oid *oid; 792 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 793 char buf[10]; 794 795 SYSCTL_XLOCK(); 796 while (namelen) { 797 if (!lsp) { 798 snprintf(buf,sizeof(buf),"%d",*name); 799 if (req->oldidx) 800 error = SYSCTL_OUT(req, ".", 1); 801 if (!error) 802 error = SYSCTL_OUT(req, buf, strlen(buf)); 803 if (error) 804 goto out; 805 namelen--; 806 name++; 807 continue; 808 } 809 lsp2 = 0; 810 SLIST_FOREACH(oid, lsp, oid_link) { 811 if (oid->oid_number != *name) 812 continue; 813 814 if (req->oldidx) 815 error = SYSCTL_OUT(req, ".", 1); 816 if (!error) 817 error = SYSCTL_OUT(req, oid->oid_name, 818 strlen(oid->oid_name)); 819 if (error) 820 goto out; 821 822 namelen--; 823 name++; 824 825 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 826 break; 827 828 if (oid->oid_handler) 829 break; 830 831 lsp2 = SYSCTL_CHILDREN(oid); 832 break; 833 } 834 lsp = lsp2; 835 } 836 error = SYSCTL_OUT(req, "", 1); 837 out: 838 SYSCTL_XUNLOCK(); 839 return (error); 840 } 841 842 /* 843 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 844 * capability mode. 845 */ 846 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD, 847 sysctl_sysctl_name, ""); 848 849 static int 850 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 851 int *next, int *len, int level, struct sysctl_oid **oidpp) 852 { 853 struct sysctl_oid *oidp; 854 855 SYSCTL_ASSERT_XLOCKED(); 856 *len = level; 857 SLIST_FOREACH(oidp, lsp, oid_link) { 858 *next = oidp->oid_number; 859 *oidpp = oidp; 860 861 if (oidp->oid_kind & CTLFLAG_SKIP) 862 continue; 863 864 if (!namelen) { 865 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 866 return (0); 867 if (oidp->oid_handler) 868 /* We really should call the handler here...*/ 869 return (0); 870 lsp = SYSCTL_CHILDREN(oidp); 871 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 872 len, level+1, oidpp)) 873 return (0); 874 goto emptynode; 875 } 876 877 if (oidp->oid_number < *name) 878 continue; 879 880 if (oidp->oid_number > *name) { 881 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 882 return (0); 883 if (oidp->oid_handler) 884 return (0); 885 lsp = SYSCTL_CHILDREN(oidp); 886 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 887 next+1, len, level+1, oidpp)) 888 return (0); 889 goto next; 890 } 891 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 892 continue; 893 894 if (oidp->oid_handler) 895 continue; 896 897 lsp = SYSCTL_CHILDREN(oidp); 898 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 899 len, level+1, oidpp)) 900 return (0); 901 next: 902 namelen = 1; 903 emptynode: 904 *len = level; 905 } 906 return (1); 907 } 908 909 static int 910 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 911 { 912 int *name = (int *) arg1; 913 u_int namelen = arg2; 914 int i, j, error; 915 struct sysctl_oid *oid; 916 struct sysctl_oid_list *lsp = &sysctl__children; 917 int newoid[CTL_MAXNAME]; 918 919 SYSCTL_XLOCK(); 920 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 921 SYSCTL_XUNLOCK(); 922 if (i) 923 return (ENOENT); 924 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 925 return (error); 926 } 927 928 /* 929 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in 930 * capability mode. 931 */ 932 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD, 933 sysctl_sysctl_next, ""); 934 935 static int 936 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 937 { 938 struct sysctl_oid *oidp; 939 struct sysctl_oid_list *lsp = &sysctl__children; 940 char *p; 941 942 SYSCTL_ASSERT_XLOCKED(); 943 944 for (*len = 0; *len < CTL_MAXNAME;) { 945 p = strsep(&name, "."); 946 947 oidp = SLIST_FIRST(lsp); 948 for (;; oidp = SLIST_NEXT(oidp, oid_link)) { 949 if (oidp == NULL) 950 return (ENOENT); 951 if (strcmp(p, oidp->oid_name) == 0) 952 break; 953 } 954 *oid++ = oidp->oid_number; 955 (*len)++; 956 957 if (name == NULL || *name == '\0') { 958 if (oidpp) 959 *oidpp = oidp; 960 return (0); 961 } 962 963 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 964 break; 965 966 if (oidp->oid_handler) 967 break; 968 969 lsp = SYSCTL_CHILDREN(oidp); 970 } 971 return (ENOENT); 972 } 973 974 static int 975 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 976 { 977 char *p; 978 int error, oid[CTL_MAXNAME], len = 0; 979 struct sysctl_oid *op = 0; 980 981 if (!req->newlen) 982 return (ENOENT); 983 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 984 return (ENAMETOOLONG); 985 986 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 987 988 error = SYSCTL_IN(req, p, req->newlen); 989 if (error) { 990 free(p, M_SYSCTL); 991 return (error); 992 } 993 994 p [req->newlen] = '\0'; 995 996 SYSCTL_XLOCK(); 997 error = name2oid(p, oid, &len, &op); 998 SYSCTL_XUNLOCK(); 999 1000 free(p, M_SYSCTL); 1001 1002 if (error) 1003 return (error); 1004 1005 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 1006 return (error); 1007 } 1008 1009 /* 1010 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in 1011 * capability mode. 1012 */ 1013 SYSCTL_PROC(_sysctl, 3, name2oid, 1014 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 1015 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 1016 1017 static int 1018 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 1019 { 1020 struct sysctl_oid *oid; 1021 int error; 1022 1023 SYSCTL_XLOCK(); 1024 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1025 if (error) 1026 goto out; 1027 1028 if (oid->oid_fmt == NULL) { 1029 error = ENOENT; 1030 goto out; 1031 } 1032 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 1033 if (error) 1034 goto out; 1035 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 1036 out: 1037 SYSCTL_XUNLOCK(); 1038 return (error); 1039 } 1040 1041 1042 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1043 sysctl_sysctl_oidfmt, ""); 1044 1045 static int 1046 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 1047 { 1048 struct sysctl_oid *oid; 1049 int error; 1050 1051 SYSCTL_XLOCK(); 1052 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1053 if (error) 1054 goto out; 1055 1056 if (oid->oid_descr == NULL) { 1057 error = ENOENT; 1058 goto out; 1059 } 1060 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 1061 out: 1062 SYSCTL_XUNLOCK(); 1063 return (error); 1064 } 1065 1066 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD, 1067 sysctl_sysctl_oiddescr, ""); 1068 1069 /* 1070 * Default "handler" functions. 1071 */ 1072 1073 /* 1074 * Handle an int, signed or unsigned. 1075 * Two cases: 1076 * a variable: point arg1 at it. 1077 * a constant: pass it in arg2. 1078 */ 1079 1080 int 1081 sysctl_handle_int(SYSCTL_HANDLER_ARGS) 1082 { 1083 int tmpout, error = 0; 1084 1085 /* 1086 * Attempt to get a coherent snapshot by making a copy of the data. 1087 */ 1088 if (arg1) 1089 tmpout = *(int *)arg1; 1090 else 1091 tmpout = arg2; 1092 error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 1093 1094 if (error || !req->newptr) 1095 return (error); 1096 1097 if (!arg1) 1098 error = EPERM; 1099 else 1100 error = SYSCTL_IN(req, arg1, sizeof(int)); 1101 return (error); 1102 } 1103 1104 /* 1105 * Based on on sysctl_handle_int() convert milliseconds into ticks. 1106 * Note: this is used by TCP. 1107 */ 1108 1109 int 1110 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 1111 { 1112 int error, s, tt; 1113 1114 tt = *(int *)arg1; 1115 s = (int)((int64_t)tt * 1000 / hz); 1116 1117 error = sysctl_handle_int(oidp, &s, 0, req); 1118 if (error || !req->newptr) 1119 return (error); 1120 1121 tt = (int)((int64_t)s * hz / 1000); 1122 if (tt < 1) 1123 return (EINVAL); 1124 1125 *(int *)arg1 = tt; 1126 return (0); 1127 } 1128 1129 1130 /* 1131 * Handle a long, signed or unsigned. 1132 * Two cases: 1133 * a variable: point arg1 at it. 1134 * a constant: pass it in arg2. 1135 */ 1136 1137 int 1138 sysctl_handle_long(SYSCTL_HANDLER_ARGS) 1139 { 1140 int error = 0; 1141 long tmplong; 1142 #ifdef SCTL_MASK32 1143 int tmpint; 1144 #endif 1145 1146 /* 1147 * Attempt to get a coherent snapshot by making a copy of the data. 1148 */ 1149 if (arg1) 1150 tmplong = *(long *)arg1; 1151 else 1152 tmplong = arg2; 1153 #ifdef SCTL_MASK32 1154 if (req->flags & SCTL_MASK32) { 1155 tmpint = tmplong; 1156 error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 1157 } else 1158 #endif 1159 error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 1160 1161 if (error || !req->newptr) 1162 return (error); 1163 1164 if (!arg1) 1165 error = EPERM; 1166 #ifdef SCTL_MASK32 1167 else if (req->flags & SCTL_MASK32) { 1168 error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1169 *(long *)arg1 = (long)tmpint; 1170 } 1171 #endif 1172 else 1173 error = SYSCTL_IN(req, arg1, sizeof(long)); 1174 return (error); 1175 } 1176 1177 /* 1178 * Handle a 64 bit int, signed or unsigned. 1179 * Two cases: 1180 * a variable: point arg1 at it. 1181 * a constant: pass it in arg2. 1182 */ 1183 int 1184 sysctl_handle_64(SYSCTL_HANDLER_ARGS) 1185 { 1186 int error = 0; 1187 uint64_t tmpout; 1188 1189 /* 1190 * Attempt to get a coherent snapshot by making a copy of the data. 1191 */ 1192 if (arg1) 1193 tmpout = *(uint64_t *)arg1; 1194 else 1195 tmpout = arg2; 1196 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1197 1198 if (error || !req->newptr) 1199 return (error); 1200 1201 if (!arg1) 1202 error = EPERM; 1203 else 1204 error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1205 return (error); 1206 } 1207 1208 /* 1209 * Handle our generic '\0' terminated 'C' string. 1210 * Two cases: 1211 * a variable string: point arg1 at it, arg2 is max length. 1212 * a constant string: point arg1 at it, arg2 is zero. 1213 */ 1214 1215 int 1216 sysctl_handle_string(SYSCTL_HANDLER_ARGS) 1217 { 1218 size_t outlen; 1219 int error = 0, ro_string = 0; 1220 1221 /* 1222 * A zero-length buffer indicates a fixed size read-only 1223 * string: 1224 */ 1225 if (arg2 == 0) { 1226 arg2 = strlen((char *)arg1) + 1; 1227 ro_string = 1; 1228 } 1229 1230 if (req->oldptr != NULL) { 1231 char *tmparg; 1232 1233 if (ro_string) { 1234 tmparg = arg1; 1235 } else { 1236 /* try to make a coherent snapshot of the string */ 1237 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 1238 memcpy(tmparg, arg1, arg2); 1239 } 1240 1241 outlen = strnlen(tmparg, arg2 - 1) + 1; 1242 error = SYSCTL_OUT(req, tmparg, outlen); 1243 1244 if (!ro_string) 1245 free(tmparg, M_SYSCTLTMP); 1246 } else { 1247 outlen = strnlen((char *)arg1, arg2 - 1) + 1; 1248 error = SYSCTL_OUT(req, NULL, outlen); 1249 } 1250 if (error || !req->newptr) 1251 return (error); 1252 1253 if ((req->newlen - req->newidx) >= arg2) { 1254 error = EINVAL; 1255 } else { 1256 arg2 = (req->newlen - req->newidx); 1257 error = SYSCTL_IN(req, arg1, arg2); 1258 ((char *)arg1)[arg2] = '\0'; 1259 } 1260 return (error); 1261 } 1262 1263 /* 1264 * Handle any kind of opaque data. 1265 * arg1 points to it, arg2 is the size. 1266 */ 1267 1268 int 1269 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 1270 { 1271 int error, tries; 1272 u_int generation; 1273 struct sysctl_req req2; 1274 1275 /* 1276 * Attempt to get a coherent snapshot, by using the thread 1277 * pre-emption counter updated from within mi_switch() to 1278 * determine if we were pre-empted during a bcopy() or 1279 * copyout(). Make 3 attempts at doing this before giving up. 1280 * If we encounter an error, stop immediately. 1281 */ 1282 tries = 0; 1283 req2 = *req; 1284 retry: 1285 generation = curthread->td_generation; 1286 error = SYSCTL_OUT(req, arg1, arg2); 1287 if (error) 1288 return (error); 1289 tries++; 1290 if (generation != curthread->td_generation && tries < 3) { 1291 *req = req2; 1292 goto retry; 1293 } 1294 1295 error = SYSCTL_IN(req, arg1, arg2); 1296 1297 return (error); 1298 } 1299 1300 /* 1301 * Transfer functions to/from kernel space. 1302 * XXX: rather untested at this point 1303 */ 1304 static int 1305 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 1306 { 1307 size_t i = 0; 1308 1309 if (req->oldptr) { 1310 i = l; 1311 if (req->oldlen <= req->oldidx) 1312 i = 0; 1313 else 1314 if (i > req->oldlen - req->oldidx) 1315 i = req->oldlen - req->oldidx; 1316 if (i > 0) 1317 bcopy(p, (char *)req->oldptr + req->oldidx, i); 1318 } 1319 req->oldidx += l; 1320 if (req->oldptr && i != l) 1321 return (ENOMEM); 1322 return (0); 1323 } 1324 1325 static int 1326 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 1327 { 1328 if (!req->newptr) 1329 return (0); 1330 if (req->newlen - req->newidx < l) 1331 return (EINVAL); 1332 bcopy((char *)req->newptr + req->newidx, p, l); 1333 req->newidx += l; 1334 return (0); 1335 } 1336 1337 int 1338 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1339 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 1340 { 1341 int error = 0; 1342 struct sysctl_req req; 1343 1344 bzero(&req, sizeof req); 1345 1346 req.td = td; 1347 req.flags = flags; 1348 1349 if (oldlenp) { 1350 req.oldlen = *oldlenp; 1351 } 1352 req.validlen = req.oldlen; 1353 1354 if (old) { 1355 req.oldptr= old; 1356 } 1357 1358 if (new != NULL) { 1359 req.newlen = newlen; 1360 req.newptr = new; 1361 } 1362 1363 req.oldfunc = sysctl_old_kernel; 1364 req.newfunc = sysctl_new_kernel; 1365 req.lock = REQ_UNWIRED; 1366 1367 SYSCTL_XLOCK(); 1368 error = sysctl_root(0, name, namelen, &req); 1369 SYSCTL_XUNLOCK(); 1370 1371 if (req.lock == REQ_WIRED && req.validlen > 0) 1372 vsunlock(req.oldptr, req.validlen); 1373 1374 if (error && error != ENOMEM) 1375 return (error); 1376 1377 if (retval) { 1378 if (req.oldptr && req.oldidx > req.validlen) 1379 *retval = req.validlen; 1380 else 1381 *retval = req.oldidx; 1382 } 1383 return (error); 1384 } 1385 1386 int 1387 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1388 void *new, size_t newlen, size_t *retval, int flags) 1389 { 1390 int oid[CTL_MAXNAME]; 1391 size_t oidlen, plen; 1392 int error; 1393 1394 oid[0] = 0; /* sysctl internal magic */ 1395 oid[1] = 3; /* name2oid */ 1396 oidlen = sizeof(oid); 1397 1398 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1399 (void *)name, strlen(name), &plen, flags); 1400 if (error) 1401 return (error); 1402 1403 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1404 new, newlen, retval, flags); 1405 return (error); 1406 } 1407 1408 /* 1409 * Transfer function to/from user space. 1410 */ 1411 static int 1412 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 1413 { 1414 size_t i, len, origidx; 1415 int error; 1416 1417 origidx = req->oldidx; 1418 req->oldidx += l; 1419 if (req->oldptr == NULL) 1420 return (0); 1421 /* 1422 * If we have not wired the user supplied buffer and we are currently 1423 * holding locks, drop a witness warning, as it's possible that 1424 * write operations to the user page can sleep. 1425 */ 1426 if (req->lock != REQ_WIRED) 1427 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1428 "sysctl_old_user()"); 1429 i = l; 1430 len = req->validlen; 1431 if (len <= origidx) 1432 i = 0; 1433 else { 1434 if (i > len - origidx) 1435 i = len - origidx; 1436 if (req->lock == REQ_WIRED) { 1437 error = copyout_nofault(p, (char *)req->oldptr + 1438 origidx, i); 1439 } else 1440 error = copyout(p, (char *)req->oldptr + origidx, i); 1441 if (error != 0) 1442 return (error); 1443 } 1444 if (i < l) 1445 return (ENOMEM); 1446 return (0); 1447 } 1448 1449 static int 1450 sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1451 { 1452 int error; 1453 1454 if (!req->newptr) 1455 return (0); 1456 if (req->newlen - req->newidx < l) 1457 return (EINVAL); 1458 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1459 "sysctl_new_user()"); 1460 error = copyin((char *)req->newptr + req->newidx, p, l); 1461 req->newidx += l; 1462 return (error); 1463 } 1464 1465 /* 1466 * Wire the user space destination buffer. If set to a value greater than 1467 * zero, the len parameter limits the maximum amount of wired memory. 1468 */ 1469 int 1470 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1471 { 1472 int ret; 1473 size_t wiredlen; 1474 1475 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1476 ret = 0; 1477 if (req->lock != REQ_WIRED && req->oldptr && 1478 req->oldfunc == sysctl_old_user) { 1479 if (wiredlen != 0) { 1480 ret = vslock(req->oldptr, wiredlen); 1481 if (ret != 0) { 1482 if (ret != ENOMEM) 1483 return (ret); 1484 wiredlen = 0; 1485 } 1486 } 1487 req->lock = REQ_WIRED; 1488 req->validlen = wiredlen; 1489 } 1490 return (0); 1491 } 1492 1493 int 1494 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1495 int *nindx, struct sysctl_req *req) 1496 { 1497 struct sysctl_oid_list *lsp; 1498 struct sysctl_oid *oid; 1499 int indx; 1500 1501 SYSCTL_ASSERT_XLOCKED(); 1502 lsp = &sysctl__children; 1503 indx = 0; 1504 while (indx < CTL_MAXNAME) { 1505 SLIST_FOREACH(oid, lsp, oid_link) { 1506 if (oid->oid_number == name[indx]) 1507 break; 1508 } 1509 if (oid == NULL) 1510 return (ENOENT); 1511 1512 indx++; 1513 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1514 if (oid->oid_handler != NULL || indx == namelen) { 1515 *noid = oid; 1516 if (nindx != NULL) 1517 *nindx = indx; 1518 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1519 ("%s found DYING node %p", __func__, oid)); 1520 return (0); 1521 } 1522 lsp = SYSCTL_CHILDREN(oid); 1523 } else if (indx == namelen) { 1524 *noid = oid; 1525 if (nindx != NULL) 1526 *nindx = indx; 1527 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1528 ("%s found DYING node %p", __func__, oid)); 1529 return (0); 1530 } else { 1531 return (ENOTDIR); 1532 } 1533 } 1534 return (ENOENT); 1535 } 1536 1537 /* 1538 * Traverse our tree, and find the right node, execute whatever it points 1539 * to, and return the resulting error code. 1540 */ 1541 1542 static int 1543 sysctl_root(SYSCTL_HANDLER_ARGS) 1544 { 1545 struct sysctl_oid *oid; 1546 int error, indx, lvl; 1547 1548 SYSCTL_ASSERT_XLOCKED(); 1549 1550 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1551 if (error) 1552 return (error); 1553 1554 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1555 /* 1556 * You can't call a sysctl when it's a node, but has 1557 * no handler. Inform the user that it's a node. 1558 * The indx may or may not be the same as namelen. 1559 */ 1560 if (oid->oid_handler == NULL) 1561 return (EISDIR); 1562 } 1563 1564 /* Is this sysctl writable? */ 1565 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 1566 return (EPERM); 1567 1568 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1569 1570 #ifdef CAPABILITY_MODE 1571 /* 1572 * If the process is in capability mode, then don't permit reading or 1573 * writing unless specifically granted for the node. 1574 */ 1575 if (IN_CAPABILITY_MODE(req->td)) { 1576 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) 1577 return (EPERM); 1578 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR)) 1579 return (EPERM); 1580 } 1581 #endif 1582 1583 /* Is this sysctl sensitive to securelevels? */ 1584 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1585 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1586 error = securelevel_gt(req->td->td_ucred, lvl); 1587 if (error) 1588 return (error); 1589 } 1590 1591 /* Is this sysctl writable by only privileged users? */ 1592 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1593 int priv; 1594 1595 if (oid->oid_kind & CTLFLAG_PRISON) 1596 priv = PRIV_SYSCTL_WRITEJAIL; 1597 #ifdef VIMAGE 1598 else if ((oid->oid_kind & CTLFLAG_VNET) && 1599 prison_owns_vnet(req->td->td_ucred)) 1600 priv = PRIV_SYSCTL_WRITEJAIL; 1601 #endif 1602 else 1603 priv = PRIV_SYSCTL_WRITE; 1604 error = priv_check(req->td, priv); 1605 if (error) 1606 return (error); 1607 } 1608 1609 if (!oid->oid_handler) 1610 return (EINVAL); 1611 1612 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1613 arg1 = (int *)arg1 + indx; 1614 arg2 -= indx; 1615 } else { 1616 arg1 = oid->oid_arg1; 1617 arg2 = oid->oid_arg2; 1618 } 1619 #ifdef MAC 1620 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1621 req); 1622 if (error != 0) 1623 return (error); 1624 #endif 1625 #ifdef VIMAGE 1626 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 1627 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 1628 #endif 1629 error = sysctl_root_handler_locked(oid, arg1, arg2, req); 1630 1631 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 1632 1633 return (error); 1634 } 1635 1636 #ifndef _SYS_SYSPROTO_H_ 1637 struct sysctl_args { 1638 int *name; 1639 u_int namelen; 1640 void *old; 1641 size_t *oldlenp; 1642 void *new; 1643 size_t newlen; 1644 }; 1645 #endif 1646 int 1647 sys___sysctl(struct thread *td, struct sysctl_args *uap) 1648 { 1649 int error, i, name[CTL_MAXNAME]; 1650 size_t j; 1651 1652 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1653 return (EINVAL); 1654 1655 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1656 if (error) 1657 return (error); 1658 1659 error = userland_sysctl(td, name, uap->namelen, 1660 uap->old, uap->oldlenp, 0, 1661 uap->new, uap->newlen, &j, 0); 1662 if (error && error != ENOMEM) 1663 return (error); 1664 if (uap->oldlenp) { 1665 i = copyout(&j, uap->oldlenp, sizeof(j)); 1666 if (i) 1667 return (i); 1668 } 1669 return (error); 1670 } 1671 1672 /* 1673 * This is used from various compatibility syscalls too. That's why name 1674 * must be in kernel space. 1675 */ 1676 int 1677 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1678 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1679 int flags) 1680 { 1681 int error = 0, memlocked; 1682 struct sysctl_req req; 1683 1684 bzero(&req, sizeof req); 1685 1686 req.td = td; 1687 req.flags = flags; 1688 1689 if (oldlenp) { 1690 if (inkernel) { 1691 req.oldlen = *oldlenp; 1692 } else { 1693 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1694 if (error) 1695 return (error); 1696 } 1697 } 1698 req.validlen = req.oldlen; 1699 1700 if (old) { 1701 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1702 return (EFAULT); 1703 req.oldptr= old; 1704 } 1705 1706 if (new != NULL) { 1707 if (!useracc(new, newlen, VM_PROT_READ)) 1708 return (EFAULT); 1709 req.newlen = newlen; 1710 req.newptr = new; 1711 } 1712 1713 req.oldfunc = sysctl_old_user; 1714 req.newfunc = sysctl_new_user; 1715 req.lock = REQ_UNWIRED; 1716 1717 #ifdef KTRACE 1718 if (KTRPOINT(curthread, KTR_SYSCTL)) 1719 ktrsysctl(name, namelen); 1720 #endif 1721 1722 if (req.oldlen > PAGE_SIZE) { 1723 memlocked = 1; 1724 sx_xlock(&sysctlmemlock); 1725 } else 1726 memlocked = 0; 1727 CURVNET_SET(TD_TO_VNET(td)); 1728 1729 for (;;) { 1730 req.oldidx = 0; 1731 req.newidx = 0; 1732 SYSCTL_XLOCK(); 1733 error = sysctl_root(0, name, namelen, &req); 1734 SYSCTL_XUNLOCK(); 1735 if (error != EAGAIN) 1736 break; 1737 kern_yield(PRI_USER); 1738 } 1739 1740 CURVNET_RESTORE(); 1741 1742 if (req.lock == REQ_WIRED && req.validlen > 0) 1743 vsunlock(req.oldptr, req.validlen); 1744 if (memlocked) 1745 sx_xunlock(&sysctlmemlock); 1746 1747 if (error && error != ENOMEM) 1748 return (error); 1749 1750 if (retval) { 1751 if (req.oldptr && req.oldidx > req.validlen) 1752 *retval = req.validlen; 1753 else 1754 *retval = req.oldidx; 1755 } 1756 return (error); 1757 } 1758 1759 /* 1760 * Drain into a sysctl struct. The user buffer should be wired if a page 1761 * fault would cause issue. 1762 */ 1763 static int 1764 sbuf_sysctl_drain(void *arg, const char *data, int len) 1765 { 1766 struct sysctl_req *req = arg; 1767 int error; 1768 1769 error = SYSCTL_OUT(req, data, len); 1770 KASSERT(error >= 0, ("Got unexpected negative value %d", error)); 1771 return (error == 0 ? len : -error); 1772 } 1773 1774 struct sbuf * 1775 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, 1776 struct sysctl_req *req) 1777 { 1778 1779 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN); 1780 sbuf_set_drain(s, sbuf_sysctl_drain, req); 1781 return (s); 1782 } 1783