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