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