kern_sysctl.c revision 268285
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: head/sys/kern/kern_sysctl.c 268285 2014-07-05 06:12:48Z hselasky $"); 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 72static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 73static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 74static 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 */ 89static struct sx sysctllock; 90static 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 99static int sysctl_root(SYSCTL_HANDLER_ARGS); 100 101/* Root list */ 102struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children); 103 104static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 105 int recurse); 106static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 107static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 108 109static struct sysctl_oid * 110sysctl_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 */ 128void 129sysctl_lock(void) 130{ 131 132 SYSCTL_XLOCK(); 133} 134 135void 136sysctl_unlock(void) 137{ 138 139 SYSCTL_XUNLOCK(); 140} 141 142static int 143sysctl_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 165static void 166sysctl_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 262void 263sysctl_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 329void 330sysctl_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. */ 360int 361sysctl_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 */ 378int 379sysctl_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 */ 430struct sysctl_ctx_entry * 431sysctl_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 */ 445struct sysctl_ctx_entry * 446sysctl_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 */ 465int 466sysctl_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 */ 491int 492sysctl_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 502int 503sysctl_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 523static int 524sysctl_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 */ 593struct sysctl_oid * 594sysctl_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 */ 645void 646sysctl_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 */ 662int 663sysctl_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 */ 688SET_DECLARE(sysctl_set, struct sysctl_oid); 689 690static void 691sysctl_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} 702SYSINIT(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 724static void 725sysctl_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 767static int 768sysctl_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 781SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 782 0, 0, sysctl_sysctl_debug, "-", ""); 783#endif 784 785static int 786sysctl_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 */ 846static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD, 847 sysctl_sysctl_name, ""); 848 849static int 850sysctl_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 909static int 910sysctl_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 */ 932static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD, 933 sysctl_sysctl_next, ""); 934 935static int 936name2oid(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 974static int 975sysctl_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 */ 1013SYSCTL_PROC(_sysctl, 3, name2oid, 1014 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 1015 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 1016 1017static int 1018sysctl_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 1042static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1043 sysctl_sysctl_oidfmt, ""); 1044 1045static int 1046sysctl_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 1066static 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 1080int 1081sysctl_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 1109int 1110sysctl_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 1137int 1138sysctl_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 */ 1183int 1184sysctl_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 1215int 1216sysctl_handle_string(SYSCTL_HANDLER_ARGS) 1217{ 1218 size_t outlen; 1219 int error = 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 1228 if (req->oldptr != NULL) { 1229 char *tmparg; 1230 1231 /* try to make a coherent snapshot of the string */ 1232 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 1233 memcpy(tmparg, arg1, arg2); 1234 1235 outlen = strnlen(tmparg, arg2 - 1) + 1; 1236 error = SYSCTL_OUT(req, tmparg, outlen); 1237 1238 free(tmparg, M_SYSCTLTMP); 1239 } else { 1240 outlen = strnlen((char *)arg1, arg2 - 1) + 1; 1241 error = SYSCTL_OUT(req, NULL, outlen); 1242 } 1243 if (error || !req->newptr) 1244 return (error); 1245 1246 if ((req->newlen - req->newidx) >= arg2) { 1247 error = EINVAL; 1248 } else { 1249 arg2 = (req->newlen - req->newidx); 1250 error = SYSCTL_IN(req, arg1, arg2); 1251 ((char *)arg1)[arg2] = '\0'; 1252 } 1253 return (error); 1254} 1255 1256/* 1257 * Handle any kind of opaque data. 1258 * arg1 points to it, arg2 is the size. 1259 */ 1260 1261int 1262sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 1263{ 1264 int error, tries; 1265 u_int generation; 1266 struct sysctl_req req2; 1267 1268 /* 1269 * Attempt to get a coherent snapshot, by using the thread 1270 * pre-emption counter updated from within mi_switch() to 1271 * determine if we were pre-empted during a bcopy() or 1272 * copyout(). Make 3 attempts at doing this before giving up. 1273 * If we encounter an error, stop immediately. 1274 */ 1275 tries = 0; 1276 req2 = *req; 1277retry: 1278 generation = curthread->td_generation; 1279 error = SYSCTL_OUT(req, arg1, arg2); 1280 if (error) 1281 return (error); 1282 tries++; 1283 if (generation != curthread->td_generation && tries < 3) { 1284 *req = req2; 1285 goto retry; 1286 } 1287 1288 error = SYSCTL_IN(req, arg1, arg2); 1289 1290 return (error); 1291} 1292 1293/* 1294 * Transfer functions to/from kernel space. 1295 * XXX: rather untested at this point 1296 */ 1297static int 1298sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 1299{ 1300 size_t i = 0; 1301 1302 if (req->oldptr) { 1303 i = l; 1304 if (req->oldlen <= req->oldidx) 1305 i = 0; 1306 else 1307 if (i > req->oldlen - req->oldidx) 1308 i = req->oldlen - req->oldidx; 1309 if (i > 0) 1310 bcopy(p, (char *)req->oldptr + req->oldidx, i); 1311 } 1312 req->oldidx += l; 1313 if (req->oldptr && i != l) 1314 return (ENOMEM); 1315 return (0); 1316} 1317 1318static int 1319sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 1320{ 1321 if (!req->newptr) 1322 return (0); 1323 if (req->newlen - req->newidx < l) 1324 return (EINVAL); 1325 bcopy((char *)req->newptr + req->newidx, p, l); 1326 req->newidx += l; 1327 return (0); 1328} 1329 1330int 1331kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1332 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 1333{ 1334 int error = 0; 1335 struct sysctl_req req; 1336 1337 bzero(&req, sizeof req); 1338 1339 req.td = td; 1340 req.flags = flags; 1341 1342 if (oldlenp) { 1343 req.oldlen = *oldlenp; 1344 } 1345 req.validlen = req.oldlen; 1346 1347 if (old) { 1348 req.oldptr= old; 1349 } 1350 1351 if (new != NULL) { 1352 req.newlen = newlen; 1353 req.newptr = new; 1354 } 1355 1356 req.oldfunc = sysctl_old_kernel; 1357 req.newfunc = sysctl_new_kernel; 1358 req.lock = REQ_UNWIRED; 1359 1360 SYSCTL_XLOCK(); 1361 error = sysctl_root(0, name, namelen, &req); 1362 SYSCTL_XUNLOCK(); 1363 1364 if (req.lock == REQ_WIRED && req.validlen > 0) 1365 vsunlock(req.oldptr, req.validlen); 1366 1367 if (error && error != ENOMEM) 1368 return (error); 1369 1370 if (retval) { 1371 if (req.oldptr && req.oldidx > req.validlen) 1372 *retval = req.validlen; 1373 else 1374 *retval = req.oldidx; 1375 } 1376 return (error); 1377} 1378 1379int 1380kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1381 void *new, size_t newlen, size_t *retval, int flags) 1382{ 1383 int oid[CTL_MAXNAME]; 1384 size_t oidlen, plen; 1385 int error; 1386 1387 oid[0] = 0; /* sysctl internal magic */ 1388 oid[1] = 3; /* name2oid */ 1389 oidlen = sizeof(oid); 1390 1391 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1392 (void *)name, strlen(name), &plen, flags); 1393 if (error) 1394 return (error); 1395 1396 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1397 new, newlen, retval, flags); 1398 return (error); 1399} 1400 1401/* 1402 * Transfer function to/from user space. 1403 */ 1404static int 1405sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 1406{ 1407 size_t i, len, origidx; 1408 int error; 1409 1410 origidx = req->oldidx; 1411 req->oldidx += l; 1412 if (req->oldptr == NULL) 1413 return (0); 1414 /* 1415 * If we have not wired the user supplied buffer and we are currently 1416 * holding locks, drop a witness warning, as it's possible that 1417 * write operations to the user page can sleep. 1418 */ 1419 if (req->lock != REQ_WIRED) 1420 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1421 "sysctl_old_user()"); 1422 i = l; 1423 len = req->validlen; 1424 if (len <= origidx) 1425 i = 0; 1426 else { 1427 if (i > len - origidx) 1428 i = len - origidx; 1429 if (req->lock == REQ_WIRED) { 1430 error = copyout_nofault(p, (char *)req->oldptr + 1431 origidx, i); 1432 } else 1433 error = copyout(p, (char *)req->oldptr + origidx, i); 1434 if (error != 0) 1435 return (error); 1436 } 1437 if (i < l) 1438 return (ENOMEM); 1439 return (0); 1440} 1441 1442static int 1443sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1444{ 1445 int error; 1446 1447 if (!req->newptr) 1448 return (0); 1449 if (req->newlen - req->newidx < l) 1450 return (EINVAL); 1451 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1452 "sysctl_new_user()"); 1453 error = copyin((char *)req->newptr + req->newidx, p, l); 1454 req->newidx += l; 1455 return (error); 1456} 1457 1458/* 1459 * Wire the user space destination buffer. If set to a value greater than 1460 * zero, the len parameter limits the maximum amount of wired memory. 1461 */ 1462int 1463sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1464{ 1465 int ret; 1466 size_t wiredlen; 1467 1468 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1469 ret = 0; 1470 if (req->lock != REQ_WIRED && req->oldptr && 1471 req->oldfunc == sysctl_old_user) { 1472 if (wiredlen != 0) { 1473 ret = vslock(req->oldptr, wiredlen); 1474 if (ret != 0) { 1475 if (ret != ENOMEM) 1476 return (ret); 1477 wiredlen = 0; 1478 } 1479 } 1480 req->lock = REQ_WIRED; 1481 req->validlen = wiredlen; 1482 } 1483 return (0); 1484} 1485 1486int 1487sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1488 int *nindx, struct sysctl_req *req) 1489{ 1490 struct sysctl_oid_list *lsp; 1491 struct sysctl_oid *oid; 1492 int indx; 1493 1494 SYSCTL_ASSERT_XLOCKED(); 1495 lsp = &sysctl__children; 1496 indx = 0; 1497 while (indx < CTL_MAXNAME) { 1498 SLIST_FOREACH(oid, lsp, oid_link) { 1499 if (oid->oid_number == name[indx]) 1500 break; 1501 } 1502 if (oid == NULL) 1503 return (ENOENT); 1504 1505 indx++; 1506 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1507 if (oid->oid_handler != NULL || indx == namelen) { 1508 *noid = oid; 1509 if (nindx != NULL) 1510 *nindx = indx; 1511 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1512 ("%s found DYING node %p", __func__, oid)); 1513 return (0); 1514 } 1515 lsp = SYSCTL_CHILDREN(oid); 1516 } else if (indx == namelen) { 1517 *noid = oid; 1518 if (nindx != NULL) 1519 *nindx = indx; 1520 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1521 ("%s found DYING node %p", __func__, oid)); 1522 return (0); 1523 } else { 1524 return (ENOTDIR); 1525 } 1526 } 1527 return (ENOENT); 1528} 1529 1530/* 1531 * Traverse our tree, and find the right node, execute whatever it points 1532 * to, and return the resulting error code. 1533 */ 1534 1535static int 1536sysctl_root(SYSCTL_HANDLER_ARGS) 1537{ 1538 struct sysctl_oid *oid; 1539 int error, indx, lvl; 1540 1541 SYSCTL_ASSERT_XLOCKED(); 1542 1543 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1544 if (error) 1545 return (error); 1546 1547 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1548 /* 1549 * You can't call a sysctl when it's a node, but has 1550 * no handler. Inform the user that it's a node. 1551 * The indx may or may not be the same as namelen. 1552 */ 1553 if (oid->oid_handler == NULL) 1554 return (EISDIR); 1555 } 1556 1557 /* Is this sysctl writable? */ 1558 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 1559 return (EPERM); 1560 1561 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1562 1563#ifdef CAPABILITY_MODE 1564 /* 1565 * If the process is in capability mode, then don't permit reading or 1566 * writing unless specifically granted for the node. 1567 */ 1568 if (IN_CAPABILITY_MODE(req->td)) { 1569 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) 1570 return (EPERM); 1571 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR)) 1572 return (EPERM); 1573 } 1574#endif 1575 1576 /* Is this sysctl sensitive to securelevels? */ 1577 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1578 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1579 error = securelevel_gt(req->td->td_ucred, lvl); 1580 if (error) 1581 return (error); 1582 } 1583 1584 /* Is this sysctl writable by only privileged users? */ 1585 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1586 int priv; 1587 1588 if (oid->oid_kind & CTLFLAG_PRISON) 1589 priv = PRIV_SYSCTL_WRITEJAIL; 1590#ifdef VIMAGE 1591 else if ((oid->oid_kind & CTLFLAG_VNET) && 1592 prison_owns_vnet(req->td->td_ucred)) 1593 priv = PRIV_SYSCTL_WRITEJAIL; 1594#endif 1595 else 1596 priv = PRIV_SYSCTL_WRITE; 1597 error = priv_check(req->td, priv); 1598 if (error) 1599 return (error); 1600 } 1601 1602 if (!oid->oid_handler) 1603 return (EINVAL); 1604 1605 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1606 arg1 = (int *)arg1 + indx; 1607 arg2 -= indx; 1608 } else { 1609 arg1 = oid->oid_arg1; 1610 arg2 = oid->oid_arg2; 1611 } 1612#ifdef MAC 1613 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1614 req); 1615 if (error != 0) 1616 return (error); 1617#endif 1618#ifdef VIMAGE 1619 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 1620 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 1621#endif 1622 error = sysctl_root_handler_locked(oid, arg1, arg2, req); 1623 1624 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 1625 1626 return (error); 1627} 1628 1629#ifndef _SYS_SYSPROTO_H_ 1630struct sysctl_args { 1631 int *name; 1632 u_int namelen; 1633 void *old; 1634 size_t *oldlenp; 1635 void *new; 1636 size_t newlen; 1637}; 1638#endif 1639int 1640sys___sysctl(struct thread *td, struct sysctl_args *uap) 1641{ 1642 int error, i, name[CTL_MAXNAME]; 1643 size_t j; 1644 1645 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1646 return (EINVAL); 1647 1648 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1649 if (error) 1650 return (error); 1651 1652 error = userland_sysctl(td, name, uap->namelen, 1653 uap->old, uap->oldlenp, 0, 1654 uap->new, uap->newlen, &j, 0); 1655 if (error && error != ENOMEM) 1656 return (error); 1657 if (uap->oldlenp) { 1658 i = copyout(&j, uap->oldlenp, sizeof(j)); 1659 if (i) 1660 return (i); 1661 } 1662 return (error); 1663} 1664 1665/* 1666 * This is used from various compatibility syscalls too. That's why name 1667 * must be in kernel space. 1668 */ 1669int 1670userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1671 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1672 int flags) 1673{ 1674 int error = 0, memlocked; 1675 struct sysctl_req req; 1676 1677 bzero(&req, sizeof req); 1678 1679 req.td = td; 1680 req.flags = flags; 1681 1682 if (oldlenp) { 1683 if (inkernel) { 1684 req.oldlen = *oldlenp; 1685 } else { 1686 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1687 if (error) 1688 return (error); 1689 } 1690 } 1691 req.validlen = req.oldlen; 1692 1693 if (old) { 1694 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1695 return (EFAULT); 1696 req.oldptr= old; 1697 } 1698 1699 if (new != NULL) { 1700 if (!useracc(new, newlen, VM_PROT_READ)) 1701 return (EFAULT); 1702 req.newlen = newlen; 1703 req.newptr = new; 1704 } 1705 1706 req.oldfunc = sysctl_old_user; 1707 req.newfunc = sysctl_new_user; 1708 req.lock = REQ_UNWIRED; 1709 1710#ifdef KTRACE 1711 if (KTRPOINT(curthread, KTR_SYSCTL)) 1712 ktrsysctl(name, namelen); 1713#endif 1714 1715 if (req.oldlen > PAGE_SIZE) { 1716 memlocked = 1; 1717 sx_xlock(&sysctlmemlock); 1718 } else 1719 memlocked = 0; 1720 CURVNET_SET(TD_TO_VNET(td)); 1721 1722 for (;;) { 1723 req.oldidx = 0; 1724 req.newidx = 0; 1725 SYSCTL_XLOCK(); 1726 error = sysctl_root(0, name, namelen, &req); 1727 SYSCTL_XUNLOCK(); 1728 if (error != EAGAIN) 1729 break; 1730 kern_yield(PRI_USER); 1731 } 1732 1733 CURVNET_RESTORE(); 1734 1735 if (req.lock == REQ_WIRED && req.validlen > 0) 1736 vsunlock(req.oldptr, req.validlen); 1737 if (memlocked) 1738 sx_xunlock(&sysctlmemlock); 1739 1740 if (error && error != ENOMEM) 1741 return (error); 1742 1743 if (retval) { 1744 if (req.oldptr && req.oldidx > req.validlen) 1745 *retval = req.validlen; 1746 else 1747 *retval = req.oldidx; 1748 } 1749 return (error); 1750} 1751 1752/* 1753 * Drain into a sysctl struct. The user buffer should be wired if a page 1754 * fault would cause issue. 1755 */ 1756static int 1757sbuf_sysctl_drain(void *arg, const char *data, int len) 1758{ 1759 struct sysctl_req *req = arg; 1760 int error; 1761 1762 error = SYSCTL_OUT(req, data, len); 1763 KASSERT(error >= 0, ("Got unexpected negative value %d", error)); 1764 return (error == 0 ? len : -error); 1765} 1766 1767struct sbuf * 1768sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, 1769 struct sysctl_req *req) 1770{ 1771 1772 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN); 1773 sbuf_set_drain(s, sbuf_sysctl_drain, req); 1774 return (s); 1775} 1776