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