kern_sysctl.c revision 195699
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Mike Karels at Berkeley Software Design, Inc. 71541Srgrimes * 812623Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 912623Sphk * project, to make these variables more userfriendly. 1012623Sphk * 111541Srgrimes * Redistribution and use in source and binary forms, with or without 121541Srgrimes * modification, are permitted provided that the following conditions 131541Srgrimes * are met: 141541Srgrimes * 1. Redistributions of source code must retain the above copyright 151541Srgrimes * notice, this list of conditions and the following disclaimer. 161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171541Srgrimes * notice, this list of conditions and the following disclaimer in the 181541Srgrimes * documentation and/or other materials provided with the distribution. 191541Srgrimes * 4. Neither the name of the University nor the names of its contributors 201541Srgrimes * may be used to endorse or promote products derived from this software 211541Srgrimes * without specific prior written permission. 221541Srgrimes * 231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331541Srgrimes * SUCH DAMAGE. 341541Srgrimes * 351541Srgrimes * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 361541Srgrimes */ 371541Srgrimes 38116182Sobrien#include <sys/cdefs.h> 39116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 195699 2009-07-14 22:48:30Z rwatson $"); 40116182Sobrien 4131778Seivind#include "opt_compat.h" 42189707Sjhb#include "opt_ktrace.h" 4331778Seivind 441541Srgrimes#include <sys/param.h> 4548274Speter#include <sys/systm.h> 4648274Speter#include <sys/kernel.h> 471541Srgrimes#include <sys/sysctl.h> 4812623Sphk#include <sys/malloc.h> 49164033Srwatson#include <sys/priv.h> 5012662Sdg#include <sys/proc.h> 51194368Sbz#include <sys/jail.h> 5282746Sdillon#include <sys/lock.h> 5382746Sdillon#include <sys/mutex.h> 5493616Salfred#include <sys/sx.h> 5515103Sphk#include <sys/sysproto.h> 56185983Skib#include <sys/uio.h> 57185348Szec#include <sys/vimage.h> 58189707Sjhb#ifdef KTRACE 59189707Sjhb#include <sys/ktrace.h> 60189707Sjhb#endif 61163606Srwatson 62195699Srwatson#include <net/vnet.h> 63195699Srwatson 64163606Srwatson#include <security/mac/mac_framework.h> 65163606Srwatson 6612645Sbde#include <vm/vm.h> 6712662Sdg#include <vm/vm_extern.h> 6812645Sbde 6930354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 7063212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 71100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 7230309Sphk 7312429Sphk/* 74188232Sjhb * The sysctllock protects the MIB tree. It also protects sysctl 75188232Sjhb * contexts used with dynamic sysctls. The sysctl_register_oid() and 76188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already 77188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are 78188232Sjhb * provided for the few places in the kernel which need to use that 79188232Sjhb * API rather than using the dynamic API. Use of the dynamic API is 80188232Sjhb * strongly encouraged for most code. 81188232Sjhb * 82192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for 83192125Sjhb * sysctl requests. This is implemented by serializing any userland 84192125Sjhb * sysctl requests larger than a single page via an exclusive lock. 8512429Sphk */ 8693625Srwatsonstatic struct sx sysctllock; 87192125Sjhbstatic struct sx sysctlmemlock; 8812429Sphk 89188232Sjhb#define SYSCTL_SLOCK() sx_slock(&sysctllock) 90188232Sjhb#define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock) 91188232Sjhb#define SYSCTL_XLOCK() sx_xlock(&sysctllock) 92188232Sjhb#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 93188232Sjhb#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 94188232Sjhb#define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED) 95112107Sjhb#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 9693616Salfred 9762573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS); 9812429Sphk 9944078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */ 10012152Sphk 101188232Sjhbstatic int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 102188232Sjhb int recurse); 103188232Sjhb 10463212Sabialstatic struct sysctl_oid * 10563212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 10663212Sabial{ 10763212Sabial struct sysctl_oid *oidp; 10863212Sabial 109188232Sjhb SYSCTL_ASSERT_LOCKED(); 11063212Sabial SLIST_FOREACH(oidp, list, oid_link) { 11163212Sabial if (strcmp(oidp->oid_name, name) == 0) { 11263212Sabial return (oidp); 11363212Sabial } 11463212Sabial } 11563212Sabial return (NULL); 11663212Sabial} 11763212Sabial 11812623Sphk/* 11912623Sphk * Initialization of the MIB tree. 12012623Sphk * 12144078Sdfr * Order by number in each list. 12212623Sphk */ 123188232Sjhbvoid 124188232Sjhbsysctl_lock(void) 125188232Sjhb{ 12612429Sphk 127188232Sjhb SYSCTL_XLOCK(); 128188232Sjhb} 129188232Sjhb 13080338Sroamvoid 131188232Sjhbsysctl_unlock(void) 132188232Sjhb{ 133188232Sjhb 134188232Sjhb SYSCTL_XUNLOCK(); 135188232Sjhb} 136188232Sjhb 137188232Sjhbvoid 13880338Sroamsysctl_register_oid(struct sysctl_oid *oidp) 13912152Sphk{ 14044078Sdfr struct sysctl_oid_list *parent = oidp->oid_parent; 14144078Sdfr struct sysctl_oid *p; 14244078Sdfr struct sysctl_oid *q; 14312197Sbde 14444078Sdfr /* 14563212Sabial * First check if another oid with the same name already 14663212Sabial * exists in the parent's list. 14763212Sabial */ 148188232Sjhb SYSCTL_ASSERT_XLOCKED(); 14963212Sabial p = sysctl_find_oidname(oidp->oid_name, parent); 15063212Sabial if (p != NULL) { 15163212Sabial if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 15263212Sabial p->oid_refcnt++; 15363212Sabial return; 15463212Sabial } else { 15563212Sabial printf("can't re-use a leaf (%s)!\n", p->oid_name); 15663212Sabial return; 15763212Sabial } 15863212Sabial } 15963212Sabial /* 16044078Sdfr * If this oid has a number OID_AUTO, give it a number which 16180339Sroam * is greater than any current oid. 16280339Sroam * NOTE: DO NOT change the starting value here, change it in 16380339Sroam * <sys/sysctl.h>, and make sure it is at least 256 to 16480339Sroam * accomodate e.g. net.inet.raw as a static sysctl node. 16544078Sdfr */ 16644078Sdfr if (oidp->oid_number == OID_AUTO) { 16780339Sroam static int newoid = CTL_AUTO_START; 16871510Smckusick 16971510Smckusick oidp->oid_number = newoid++; 17071510Smckusick if (newoid == 0x7fffffff) 17171510Smckusick panic("out of oids"); 17244078Sdfr } 17384832Sroam#if 0 17484832Sroam else if (oidp->oid_number >= CTL_AUTO_START) { 17584832Sroam /* do not panic; this happens when unregistering sysctl sets */ 17684832Sroam printf("static sysctl oid too high: %d", oidp->oid_number); 17784832Sroam } 17884832Sroam#endif 17944078Sdfr 18044078Sdfr /* 18144078Sdfr * Insert the oid into the parent's list in order. 18244078Sdfr */ 18344078Sdfr q = NULL; 18444078Sdfr SLIST_FOREACH(p, parent, oid_link) { 18544078Sdfr if (oidp->oid_number < p->oid_number) 18644078Sdfr break; 18744078Sdfr q = p; 18844078Sdfr } 18944078Sdfr if (q) 19044078Sdfr SLIST_INSERT_AFTER(q, oidp, oid_link); 19144078Sdfr else 19244078Sdfr SLIST_INSERT_HEAD(parent, oidp, oid_link); 19312152Sphk} 19412131Sphk 19580338Sroamvoid 19680338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp) 19712152Sphk{ 198115391Smux struct sysctl_oid *p; 199115391Smux int error; 200115391Smux 201188232Sjhb SYSCTL_ASSERT_XLOCKED(); 202115391Smux error = ENOENT; 203115391Smux if (oidp->oid_number == OID_AUTO) { 204115391Smux error = EINVAL; 205115391Smux } else { 206115391Smux SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 207115391Smux if (p == oidp) { 208115391Smux SLIST_REMOVE(oidp->oid_parent, oidp, 209115391Smux sysctl_oid, oid_link); 210115391Smux error = 0; 211115391Smux break; 212115391Smux } 213115391Smux } 214115391Smux } 215115391Smux 216115391Smux /* 217115391Smux * This can happen when a module fails to register and is 218115391Smux * being unloaded afterwards. It should not be a panic() 219115391Smux * for normal use. 220115391Smux */ 221115391Smux if (error) 222115391Smux printf("%s: failed to unregister sysctl\n", __func__); 22344078Sdfr} 22412152Sphk 22563212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */ 22663212Sabialint 22763212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c) 22863212Sabial{ 22963212Sabial 23063212Sabial if (c == NULL) { 23163212Sabial return (EINVAL); 23263212Sabial } 233188232Sjhb 234188232Sjhb /* 235188232Sjhb * No locking here, the caller is responsible for not adding 236188232Sjhb * new nodes to a context until after this function has 237188232Sjhb * returned. 238188232Sjhb */ 23963212Sabial TAILQ_INIT(c); 24063212Sabial return (0); 24163212Sabial} 24263212Sabial 24363212Sabial/* Free the context, and destroy all dynamic oids registered in this context */ 24463212Sabialint 24563212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist) 24663212Sabial{ 24763212Sabial struct sysctl_ctx_entry *e, *e1; 24863212Sabial int error; 24963212Sabial 25063212Sabial error = 0; 25163212Sabial /* 25263212Sabial * First perform a "dry run" to check if it's ok to remove oids. 25363212Sabial * XXX FIXME 25463212Sabial * XXX This algorithm is a hack. But I don't know any 25563212Sabial * XXX better solution for now... 25663212Sabial */ 257188232Sjhb SYSCTL_XLOCK(); 25863212Sabial TAILQ_FOREACH(e, clist, link) { 259188232Sjhb error = sysctl_remove_oid_locked(e->entry, 0, 0); 26063212Sabial if (error) 26163212Sabial break; 26263212Sabial } 26363212Sabial /* 26463212Sabial * Restore deregistered entries, either from the end, 26563212Sabial * or from the place where error occured. 26663212Sabial * e contains the entry that was not unregistered 26763212Sabial */ 26863212Sabial if (error) 26963212Sabial e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 27063212Sabial else 27163212Sabial e1 = TAILQ_LAST(clist, sysctl_ctx_list); 27263212Sabial while (e1 != NULL) { 27363212Sabial sysctl_register_oid(e1->entry); 27463212Sabial e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 27563212Sabial } 276188232Sjhb if (error) { 277188232Sjhb SYSCTL_XUNLOCK(); 27863212Sabial return(EBUSY); 279188232Sjhb } 28063212Sabial /* Now really delete the entries */ 28163212Sabial e = TAILQ_FIRST(clist); 28263212Sabial while (e != NULL) { 28363212Sabial e1 = TAILQ_NEXT(e, link); 284188232Sjhb error = sysctl_remove_oid_locked(e->entry, 1, 0); 28563212Sabial if (error) 28663212Sabial panic("sysctl_remove_oid: corrupt tree, entry: %s", 28763212Sabial e->entry->oid_name); 28863212Sabial free(e, M_SYSCTLOID); 28963212Sabial e = e1; 29063212Sabial } 291188232Sjhb SYSCTL_XUNLOCK(); 29263212Sabial return (error); 29363212Sabial} 29463212Sabial 29563212Sabial/* Add an entry to the context */ 29663212Sabialstruct sysctl_ctx_entry * 29763212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 29863212Sabial{ 29963212Sabial struct sysctl_ctx_entry *e; 30063212Sabial 301188232Sjhb SYSCTL_ASSERT_XLOCKED(); 30263212Sabial if (clist == NULL || oidp == NULL) 30363212Sabial return(NULL); 304111119Simp e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 30563212Sabial e->entry = oidp; 30663212Sabial TAILQ_INSERT_HEAD(clist, e, link); 30763212Sabial return (e); 30863212Sabial} 30963212Sabial 31063212Sabial/* Find an entry in the context */ 31163212Sabialstruct sysctl_ctx_entry * 31263212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 31363212Sabial{ 31463212Sabial struct sysctl_ctx_entry *e; 31563212Sabial 316188232Sjhb SYSCTL_ASSERT_LOCKED(); 31763212Sabial if (clist == NULL || oidp == NULL) 31863212Sabial return(NULL); 31971999Sphk TAILQ_FOREACH(e, clist, link) { 32063212Sabial if(e->entry == oidp) 32163212Sabial return(e); 32263212Sabial } 32363212Sabial return (e); 32463212Sabial} 32563212Sabial 32644078Sdfr/* 32763212Sabial * Delete an entry from the context. 32863212Sabial * NOTE: this function doesn't free oidp! You have to remove it 32963212Sabial * with sysctl_remove_oid(). 33063212Sabial */ 33163212Sabialint 33263212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 33363212Sabial{ 33463212Sabial struct sysctl_ctx_entry *e; 33563212Sabial 33663212Sabial if (clist == NULL || oidp == NULL) 33763212Sabial return (EINVAL); 338188232Sjhb SYSCTL_XLOCK(); 33963212Sabial e = sysctl_ctx_entry_find(clist, oidp); 34063212Sabial if (e != NULL) { 34163212Sabial TAILQ_REMOVE(clist, e, link); 342188232Sjhb SYSCTL_XUNLOCK(); 34363212Sabial free(e, M_SYSCTLOID); 34463212Sabial return (0); 345188232Sjhb } else { 346188232Sjhb SYSCTL_XUNLOCK(); 34763212Sabial return (ENOENT); 348188232Sjhb } 34963212Sabial} 35063212Sabial 35163212Sabial/* 35263212Sabial * Remove dynamically created sysctl trees. 35363212Sabial * oidp - top of the tree to be removed 35463212Sabial * del - if 0 - just deregister, otherwise free up entries as well 35563212Sabial * recurse - if != 0 traverse the subtree to be deleted 35663212Sabial */ 35763212Sabialint 35863212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 35963212Sabial{ 360188232Sjhb int error; 361188232Sjhb 362188232Sjhb SYSCTL_XLOCK(); 363188232Sjhb error = sysctl_remove_oid_locked(oidp, del, recurse); 364188232Sjhb SYSCTL_XUNLOCK(); 365188232Sjhb return (error); 366188232Sjhb} 367188232Sjhb 368188232Sjhbstatic int 369188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 370188232Sjhb{ 37163212Sabial struct sysctl_oid *p; 37263212Sabial int error; 37363212Sabial 374188232Sjhb SYSCTL_ASSERT_XLOCKED(); 37563212Sabial if (oidp == NULL) 37663212Sabial return(EINVAL); 37763212Sabial if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 37863212Sabial printf("can't remove non-dynamic nodes!\n"); 37963212Sabial return (EINVAL); 38063212Sabial } 38163212Sabial /* 38263212Sabial * WARNING: normal method to do this should be through 38363212Sabial * sysctl_ctx_free(). Use recursing as the last resort 38463212Sabial * method to purge your sysctl tree of leftovers... 38563212Sabial * However, if some other code still references these nodes, 38663212Sabial * it will panic. 38763212Sabial */ 38863212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 38963212Sabial if (oidp->oid_refcnt == 1) { 39063212Sabial SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 39163212Sabial if (!recurse) 39263212Sabial return (ENOTEMPTY); 393188232Sjhb error = sysctl_remove_oid_locked(p, del, 394188232Sjhb recurse); 39563212Sabial if (error) 39663212Sabial return (error); 39763212Sabial } 39863212Sabial if (del) 39963212Sabial free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 40063212Sabial } 40163212Sabial } 40263212Sabial if (oidp->oid_refcnt > 1 ) { 40363212Sabial oidp->oid_refcnt--; 40463212Sabial } else { 40563212Sabial if (oidp->oid_refcnt == 0) { 40663212Sabial printf("Warning: bad oid_refcnt=%u (%s)!\n", 40763212Sabial oidp->oid_refcnt, oidp->oid_name); 40863212Sabial return (EINVAL); 40963212Sabial } 41063212Sabial sysctl_unregister_oid(oidp); 41163212Sabial if (del) { 412141433Sphk if (oidp->oid_descr) 413141433Sphk free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID); 41463978Speter free((void *)(uintptr_t)(const void *)oidp->oid_name, 41563978Speter M_SYSCTLOID); 41663212Sabial free(oidp, M_SYSCTLOID); 41763212Sabial } 41863212Sabial } 41963212Sabial return (0); 42063212Sabial} 42163212Sabial 42263212Sabial/* 42363212Sabial * Create new sysctls at run time. 42463212Sabial * clist may point to a valid context initialized with sysctl_ctx_init(). 42563212Sabial */ 42663212Sabialstruct sysctl_oid * 42763212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 42870679Sjhb int number, const char *name, int kind, void *arg1, int arg2, 42970679Sjhb int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 43063212Sabial{ 43163212Sabial struct sysctl_oid *oidp; 43263212Sabial ssize_t len; 43363978Speter char *newname; 43463212Sabial 43563212Sabial /* You have to hook up somewhere.. */ 43663212Sabial if (parent == NULL) 43763212Sabial return(NULL); 43863212Sabial /* Check if the node already exists, otherwise create it */ 439188232Sjhb SYSCTL_XLOCK(); 44063212Sabial oidp = sysctl_find_oidname(name, parent); 44163212Sabial if (oidp != NULL) { 44263212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 44363212Sabial oidp->oid_refcnt++; 44463212Sabial /* Update the context */ 44563212Sabial if (clist != NULL) 44663212Sabial sysctl_ctx_entry_add(clist, oidp); 447188232Sjhb SYSCTL_XUNLOCK(); 44863212Sabial return (oidp); 44963212Sabial } else { 450188232Sjhb SYSCTL_XUNLOCK(); 45163212Sabial printf("can't re-use a leaf (%s)!\n", name); 45263212Sabial return (NULL); 45363212Sabial } 45463212Sabial } 455111119Simp oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 45663212Sabial oidp->oid_parent = parent; 45763212Sabial SLIST_NEXT(oidp, oid_link) = NULL; 45863212Sabial oidp->oid_number = number; 45963212Sabial oidp->oid_refcnt = 1; 46063212Sabial len = strlen(name); 461111119Simp newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 46263978Speter bcopy(name, newname, len + 1); 46363978Speter newname[len] = '\0'; 46463978Speter oidp->oid_name = newname; 46563212Sabial oidp->oid_handler = handler; 46663212Sabial oidp->oid_kind = CTLFLAG_DYN | kind; 46763212Sabial if ((kind & CTLTYPE) == CTLTYPE_NODE) { 46863212Sabial /* Allocate space for children */ 469132776Skan SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list), 470132776Skan M_SYSCTLOID, M_WAITOK)); 47163212Sabial SLIST_INIT(SYSCTL_CHILDREN(oidp)); 47263212Sabial } else { 47363212Sabial oidp->oid_arg1 = arg1; 47463212Sabial oidp->oid_arg2 = arg2; 47563212Sabial } 47663212Sabial oidp->oid_fmt = fmt; 47788006Sluigi if (descr) { 47888006Sluigi int len = strlen(descr) + 1; 479141433Sphk oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK); 480141433Sphk if (oidp->oid_descr) 481141433Sphk strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr); 48288006Sluigi } 48363212Sabial /* Update the context, if used */ 48463212Sabial if (clist != NULL) 48563212Sabial sysctl_ctx_entry_add(clist, oidp); 48663212Sabial /* Register this oid */ 48763212Sabial sysctl_register_oid(oidp); 488188232Sjhb SYSCTL_XUNLOCK(); 48963212Sabial return (oidp); 49063212Sabial} 49163212Sabial 49263212Sabial/* 493174113Speter * Rename an existing oid. 494174113Speter */ 495174113Spetervoid 496174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 497174113Speter{ 498174113Speter ssize_t len; 499174113Speter char *newname; 500174113Speter void *oldname; 501174113Speter 502174113Speter len = strlen(name); 503174113Speter newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 504174113Speter bcopy(name, newname, len + 1); 505174113Speter newname[len] = '\0'; 506188232Sjhb SYSCTL_XLOCK(); 507188232Sjhb oldname = (void *)(uintptr_t)(const void *)oidp->oid_name; 508174113Speter oidp->oid_name = newname; 509188232Sjhb SYSCTL_XUNLOCK(); 510174113Speter free(oldname, M_SYSCTLOID); 511174113Speter} 512174113Speter 513174113Speter/* 514126319Sdes * Reparent an existing oid. 515126319Sdes */ 516126319Sdesint 517126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 518126319Sdes{ 519126319Sdes struct sysctl_oid *oidp; 520126319Sdes 521188232Sjhb SYSCTL_XLOCK(); 522188232Sjhb if (oid->oid_parent == parent) { 523188232Sjhb SYSCTL_XUNLOCK(); 524126319Sdes return (0); 525188232Sjhb } 526126319Sdes oidp = sysctl_find_oidname(oid->oid_name, parent); 527188232Sjhb if (oidp != NULL) { 528188232Sjhb SYSCTL_XUNLOCK(); 529126319Sdes return (EEXIST); 530188232Sjhb } 531126319Sdes sysctl_unregister_oid(oid); 532126319Sdes oid->oid_parent = parent; 533126319Sdes oid->oid_number = OID_AUTO; 534126319Sdes sysctl_register_oid(oid); 535188232Sjhb SYSCTL_XUNLOCK(); 536126319Sdes return (0); 537126319Sdes} 538126319Sdes 539126319Sdes/* 54044078Sdfr * Register the kernel's oids on startup. 54144078Sdfr */ 54278161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid); 54312152Sphk 54480338Sroamstatic void 54580338Sroamsysctl_register_all(void *arg) 54638869Sbde{ 54778161Speter struct sysctl_oid **oidp; 54878161Speter 549192125Sjhb sx_init(&sysctlmemlock, "sysctl mem"); 55093625Srwatson SYSCTL_INIT(); 551188232Sjhb SYSCTL_XLOCK(); 55278161Speter SET_FOREACH(oidp, sysctl_set) 55378161Speter sysctl_register_oid(*oidp); 554188232Sjhb SYSCTL_XUNLOCK(); 55538869Sbde} 55644078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 55744078Sdfr 55812623Sphk/* 55912623Sphk * "Staff-functions" 56012623Sphk * 56112650Sphk * These functions implement a presently undocumented interface 56212650Sphk * used by the sysctl program to walk the tree, and get the type 56312650Sphk * so it can print the value. 56412650Sphk * This interface is under work and consideration, and should probably 56512650Sphk * be killed with a big axe by the first person who can find the time. 56612650Sphk * (be aware though, that the proper interface isn't as obvious as it 56712650Sphk * may seem, there are various conflicting requirements. 56812650Sphk * 56912623Sphk * {0,0} printf the entire MIB-tree. 57012623Sphk * {0,1,...} return the name of the "..." OID. 57142467Sphk * {0,2,...} return the next OID. 57212623Sphk * {0,3} return the OID of the name in "new" 57312650Sphk * {0,4,...} return the kind & format info for the "..." OID. 57488006Sluigi * {0,5,...} return the description the "..." OID. 57512623Sphk */ 57612623Sphk 577136999Srwatson#ifdef SYSCTL_DEBUG 57812152Sphkstatic void 57944078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 58012152Sphk{ 58144078Sdfr int k; 58244078Sdfr struct sysctl_oid *oidp; 58312152Sphk 584188232Sjhb SYSCTL_ASSERT_LOCKED(); 58544078Sdfr SLIST_FOREACH(oidp, l, oid_link) { 58612152Sphk 58712152Sphk for (k=0; k<i; k++) 58812152Sphk printf(" "); 58912152Sphk 59044078Sdfr printf("%d %s ", oidp->oid_number, oidp->oid_name); 59112152Sphk 59212152Sphk printf("%c%c", 59344078Sdfr oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 59444078Sdfr oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 59512152Sphk 59644078Sdfr if (oidp->oid_handler) 59715241Sphk printf(" *Handler"); 59815241Sphk 59944078Sdfr switch (oidp->oid_kind & CTLTYPE) { 60012243Sphk case CTLTYPE_NODE: 60115241Sphk printf(" Node\n"); 60244078Sdfr if (!oidp->oid_handler) { 60312152Sphk sysctl_sysctl_debug_dump_node( 60444078Sdfr oidp->oid_arg1, i+2); 60512152Sphk } 60612152Sphk break; 60712152Sphk case CTLTYPE_INT: printf(" Int\n"); break; 60812152Sphk case CTLTYPE_STRING: printf(" String\n"); break; 60912152Sphk case CTLTYPE_QUAD: printf(" Quad\n"); break; 61012152Sphk case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 61112152Sphk default: printf("\n"); 61212152Sphk } 61312152Sphk 61412152Sphk } 61512152Sphk} 61612152Sphk 61712152Sphkstatic int 61862573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 61912152Sphk{ 62087024Speter int error; 62187024Speter 622164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 62387024Speter if (error) 624139483Spjd return (error); 62544078Sdfr sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 626139483Spjd return (ENOENT); 62712152Sphk} 62812152Sphk 62912152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 63012623Sphk 0, 0, sysctl_sysctl_debug, "-", ""); 631136999Srwatson#endif 63212152Sphk 63312623Sphkstatic int 63462573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 63512623Sphk{ 63612623Sphk int *name = (int *) arg1; 63712623Sphk u_int namelen = arg2; 63844078Sdfr int error = 0; 63944078Sdfr struct sysctl_oid *oid; 64044972Sphk struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 64112623Sphk char buf[10]; 64212131Sphk 643188232Sjhb SYSCTL_ASSERT_LOCKED(); 64412623Sphk while (namelen) { 64512623Sphk if (!lsp) { 64641514Sarchie snprintf(buf,sizeof(buf),"%d",*name); 64712623Sphk if (req->oldidx) 64812623Sphk error = SYSCTL_OUT(req, ".", 1); 64912623Sphk if (!error) 65012623Sphk error = SYSCTL_OUT(req, buf, strlen(buf)); 65112623Sphk if (error) 65212623Sphk return (error); 65312623Sphk namelen--; 65412623Sphk name++; 65512623Sphk continue; 65612623Sphk } 65744972Sphk lsp2 = 0; 65844078Sdfr SLIST_FOREACH(oid, lsp, oid_link) { 65944078Sdfr if (oid->oid_number != *name) 66012623Sphk continue; 66112131Sphk 66212623Sphk if (req->oldidx) 66312623Sphk error = SYSCTL_OUT(req, ".", 1); 66412623Sphk if (!error) 66544078Sdfr error = SYSCTL_OUT(req, oid->oid_name, 66644078Sdfr strlen(oid->oid_name)); 66712623Sphk if (error) 66812623Sphk return (error); 66912623Sphk 67012623Sphk namelen--; 67112623Sphk name++; 67212623Sphk 67344972Sphk if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 67412623Sphk break; 67512623Sphk 67644078Sdfr if (oid->oid_handler) 67712623Sphk break; 67812623Sphk 67944972Sphk lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 68012623Sphk break; 68112623Sphk } 68244972Sphk lsp = lsp2; 68312623Sphk } 68412623Sphk return (SYSCTL_OUT(req, "", 1)); 68512623Sphk} 68612623Sphk 687141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 68812623Sphk 68912623Sphkstatic int 69063978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 69144078Sdfr int *next, int *len, int level, struct sysctl_oid **oidpp) 69212623Sphk{ 69344078Sdfr struct sysctl_oid *oidp; 69412623Sphk 695188232Sjhb SYSCTL_ASSERT_LOCKED(); 69612623Sphk *len = level; 69744078Sdfr SLIST_FOREACH(oidp, lsp, oid_link) { 69844078Sdfr *next = oidp->oid_number; 69944078Sdfr *oidpp = oidp; 70012623Sphk 701101650Smux if (oidp->oid_kind & CTLFLAG_SKIP) 702101650Smux continue; 703101650Smux 70412623Sphk if (!namelen) { 70544078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 706139483Spjd return (0); 70744078Sdfr if (oidp->oid_handler) 70812623Sphk /* We really should call the handler here...*/ 709139483Spjd return (0); 71044078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 71163978Speter if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 71244078Sdfr len, level+1, oidpp)) 713139483Spjd return (0); 714111260Srwatson goto emptynode; 71512623Sphk } 71612623Sphk 71744078Sdfr if (oidp->oid_number < *name) 71812623Sphk continue; 71912623Sphk 72044078Sdfr if (oidp->oid_number > *name) { 72144078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 722139483Spjd return (0); 72344078Sdfr if (oidp->oid_handler) 724139483Spjd return (0); 72544078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 72663978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 72744078Sdfr next+1, len, level+1, oidpp)) 72812623Sphk return (0); 72915241Sphk goto next; 73012623Sphk } 73144078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 73212623Sphk continue; 73312623Sphk 73444078Sdfr if (oidp->oid_handler) 73512623Sphk continue; 73612623Sphk 73744078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 73863978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 73944078Sdfr len, level+1, oidpp)) 74012623Sphk return (0); 74115241Sphk next: 74212623Sphk namelen = 1; 743111260Srwatson emptynode: 74412623Sphk *len = level; 74512623Sphk } 746139483Spjd return (1); 74712623Sphk} 74812623Sphk 74912623Sphkstatic int 75062573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 75112623Sphk{ 75212623Sphk int *name = (int *) arg1; 75312623Sphk u_int namelen = arg2; 75412623Sphk int i, j, error; 75512623Sphk struct sysctl_oid *oid; 75644078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 75712623Sphk int newoid[CTL_MAXNAME]; 75812623Sphk 75963978Speter i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 76012623Sphk if (i) 761139483Spjd return (ENOENT); 76212650Sphk error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 76312623Sphk return (error); 76412623Sphk} 76512623Sphk 766141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 76712623Sphk 76812623Sphkstatic int 769189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 77012623Sphk{ 77144078Sdfr int i; 77244078Sdfr struct sysctl_oid *oidp; 77344078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 77412623Sphk char *p; 77512623Sphk 776188232Sjhb SYSCTL_ASSERT_LOCKED(); 777186564Sed 77812623Sphk if (!*name) 779139483Spjd return (ENOENT); 78012623Sphk 78112623Sphk p = name + strlen(name) - 1 ; 78212623Sphk if (*p == '.') 78312623Sphk *p = '\0'; 78412623Sphk 78512623Sphk *len = 0; 78612623Sphk 78712623Sphk for (p = name; *p && *p != '.'; p++) 78812623Sphk ; 78912623Sphk i = *p; 79012623Sphk if (i == '.') 79112623Sphk *p = '\0'; 79212623Sphk 79344078Sdfr oidp = SLIST_FIRST(lsp); 79412623Sphk 79544078Sdfr while (oidp && *len < CTL_MAXNAME) { 79644078Sdfr if (strcmp(name, oidp->oid_name)) { 79744078Sdfr oidp = SLIST_NEXT(oidp, oid_link); 79812623Sphk continue; 79912623Sphk } 80044078Sdfr *oid++ = oidp->oid_number; 80112623Sphk (*len)++; 80212623Sphk 80312623Sphk if (!i) { 80444078Sdfr if (oidpp) 80544078Sdfr *oidpp = oidp; 80612623Sphk return (0); 80712623Sphk } 80812623Sphk 80944078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 81012623Sphk break; 81112623Sphk 81244078Sdfr if (oidp->oid_handler) 81312623Sphk break; 81412623Sphk 81544078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 81644078Sdfr oidp = SLIST_FIRST(lsp); 81712623Sphk name = p+1; 81812623Sphk for (p = name; *p && *p != '.'; p++) 81912623Sphk ; 82012623Sphk i = *p; 82112623Sphk if (i == '.') 82212623Sphk *p = '\0'; 82312623Sphk } 824139483Spjd return (ENOENT); 82512623Sphk} 82612623Sphk 82712623Sphkstatic int 82862573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 82912623Sphk{ 83012623Sphk char *p; 83112623Sphk int error, oid[CTL_MAXNAME], len; 83212623Sphk struct sysctl_oid *op = 0; 83312623Sphk 834188232Sjhb SYSCTL_ASSERT_LOCKED(); 835186564Sed 83612623Sphk if (!req->newlen) 837139483Spjd return (ENOENT); 83845140Sphk if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 83945140Sphk return (ENAMETOOLONG); 84012623Sphk 841111119Simp p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 84212623Sphk 84312623Sphk error = SYSCTL_IN(req, p, req->newlen); 84412623Sphk if (error) { 84512623Sphk free(p, M_SYSCTL); 84612623Sphk return (error); 84712623Sphk } 84812623Sphk 84912623Sphk p [req->newlen] = '\0'; 85012623Sphk 85112623Sphk error = name2oid(p, oid, &len, &op); 85212623Sphk 85312623Sphk free(p, M_SYSCTL); 85412623Sphk 85512623Sphk if (error) 85612623Sphk return (error); 85712623Sphk 85812650Sphk error = SYSCTL_OUT(req, oid, len * sizeof *oid); 85912623Sphk return (error); 86012623Sphk} 86112623Sphk 862187864SedSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE, 863187864Sed 0, 0, sysctl_sysctl_name2oid, "I", ""); 86412623Sphk 86512623Sphkstatic int 86662573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 86712623Sphk{ 86844078Sdfr struct sysctl_oid *oid; 86953977Sgreen int error; 87012623Sphk 87153977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 87253977Sgreen if (error) 87353977Sgreen return (error); 87412623Sphk 87544078Sdfr if (!oid->oid_fmt) 87653977Sgreen return (ENOENT); 87753977Sgreen error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 87853977Sgreen if (error) 87953977Sgreen return (error); 88053977Sgreen error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 88112650Sphk return (error); 88212623Sphk} 88312623Sphk 88442467Sphk 885187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE, 886187864Sed sysctl_sysctl_oidfmt, ""); 88712623Sphk 88888006Sluigistatic int 88988006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 89088006Sluigi{ 89188006Sluigi struct sysctl_oid *oid; 89288006Sluigi int error; 89388006Sluigi 89488006Sluigi error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 89588006Sluigi if (error) 89688006Sluigi return (error); 89788006Sluigi 898141433Sphk if (!oid->oid_descr) 89988006Sluigi return (ENOENT); 900141433Sphk error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 90188006Sluigi return (error); 90288006Sluigi} 90388006Sluigi 904141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 90588006Sluigi 90612243Sphk/* 90712623Sphk * Default "handler" functions. 90812623Sphk */ 90912623Sphk 91012623Sphk/* 91142095Sdfr * Handle an int, signed or unsigned. 91212243Sphk * Two cases: 91312243Sphk * a variable: point arg1 at it. 91412243Sphk * a constant: pass it in arg2. 91512243Sphk */ 91612243Sphk 91711865Sphkint 91862573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS) 91911863Sphk{ 920100833Struckman int tmpout, error = 0; 92111863Sphk 922100833Struckman /* 923100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 924100833Struckman */ 92512243Sphk if (arg1) 926100833Struckman tmpout = *(int *)arg1; 92720506Sbde else 928100833Struckman tmpout = arg2; 929100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 93011863Sphk 93112243Sphk if (error || !req->newptr) 93212243Sphk return (error); 93311863Sphk 93412243Sphk if (!arg1) 93512243Sphk error = EPERM; 93612243Sphk else 93712243Sphk error = SYSCTL_IN(req, arg1, sizeof(int)); 93812243Sphk return (error); 93911863Sphk} 94011863Sphk 94112243Sphk/* 942155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks. 943195699Srwatson * Note: this is used by TCP. 944155758Sandre */ 945155758Sandre 946155758Sandreint 947155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 948155758Sandre{ 949155758Sandre int error, s, tt; 950155758Sandre 951195699Srwatson#ifdef VIMAGE 952195699Srwatson if (arg1 != NULL) 953195699Srwatson arg1 = (void *)(TD_TO_VNET(req->td)->vnet_data_base + 954195699Srwatson (uintptr_t)arg1); 955195699Srwatson#endif 956191688Szec tt = *(int *)arg1; 957155758Sandre s = (int)((int64_t)tt * 1000 / hz); 958155758Sandre 959155758Sandre error = sysctl_handle_int(oidp, &s, 0, req); 960155758Sandre if (error || !req->newptr) 961155758Sandre return (error); 962155758Sandre 963155758Sandre tt = (int)((int64_t)s * hz / 1000); 964155758Sandre if (tt < 1) 965155758Sandre return (EINVAL); 966155758Sandre 967191688Szec *(int *)arg1 = tt; 968155758Sandre return (0); 969155758Sandre} 970155758Sandre 971155758Sandre 972155758Sandre/* 97345140Sphk * Handle a long, signed or unsigned. arg1 points to it. 97438517Sdfr */ 97538517Sdfr 97638517Sdfrint 97762573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS) 97838517Sdfr{ 97938517Sdfr int error = 0; 980136404Speter long tmplong; 981136404Speter#ifdef SCTL_MASK32 982136404Speter int tmpint; 983136404Speter#endif 98438517Sdfr 985100833Struckman /* 986100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 987100833Struckman */ 98845140Sphk if (!arg1) 98945140Sphk return (EINVAL); 990136404Speter tmplong = *(long *)arg1; 991136404Speter#ifdef SCTL_MASK32 992136404Speter if (req->flags & SCTL_MASK32) { 993136404Speter tmpint = tmplong; 994136404Speter error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 995136404Speter } else 996136404Speter#endif 997136404Speter error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 99838517Sdfr 99938517Sdfr if (error || !req->newptr) 100038517Sdfr return (error); 100138517Sdfr 1002136404Speter#ifdef SCTL_MASK32 1003136404Speter if (req->flags & SCTL_MASK32) { 1004136404Speter error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1005136404Speter *(long *)arg1 = (long)tmpint; 1006136404Speter } else 1007136404Speter#endif 1008136404Speter error = SYSCTL_IN(req, arg1, sizeof(long)); 100938517Sdfr return (error); 101038517Sdfr} 101138517Sdfr 101238517Sdfr/* 1013170288Sdwmalone * Handle a 64 bit int, signed or unsigned. arg1 points to it. 1014170288Sdwmalone */ 1015170288Sdwmalone 1016170288Sdwmaloneint 1017170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS) 1018170288Sdwmalone{ 1019170288Sdwmalone int error = 0; 1020170288Sdwmalone uint64_t tmpout; 1021170288Sdwmalone 1022170288Sdwmalone /* 1023170288Sdwmalone * Attempt to get a coherent snapshot by making a copy of the data. 1024170288Sdwmalone */ 1025170288Sdwmalone if (!arg1) 1026170288Sdwmalone return (EINVAL); 1027170288Sdwmalone tmpout = *(uint64_t *)arg1; 1028170288Sdwmalone error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1029170288Sdwmalone 1030170288Sdwmalone if (error || !req->newptr) 1031170288Sdwmalone return (error); 1032170288Sdwmalone 1033170288Sdwmalone error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1034170288Sdwmalone return (error); 1035170288Sdwmalone} 1036170288Sdwmalone 1037170288Sdwmalone/* 103812243Sphk * Handle our generic '\0' terminated 'C' string. 103912243Sphk * Two cases: 104012243Sphk * a variable string: point arg1 at it, arg2 is max length. 104112243Sphk * a constant string: point arg1 at it, arg2 is zero. 104212243Sphk */ 104312243Sphk 104411865Sphkint 104562573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS) 104611863Sphk{ 104712243Sphk int error=0; 1048100833Struckman char *tmparg; 1049100833Struckman size_t outlen; 105011863Sphk 1051100833Struckman /* 1052100833Struckman * Attempt to get a coherent snapshot by copying to a 1053100833Struckman * temporary kernel buffer. 1054100833Struckman */ 1055100833Struckmanretry: 1056100833Struckman outlen = strlen((char *)arg1)+1; 1057111119Simp tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 1058105354Srobert 1059105354Srobert if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 1060100833Struckman free(tmparg, M_SYSCTLTMP); 1061100833Struckman goto retry; 1062100833Struckman } 1063105354Srobert 1064100833Struckman error = SYSCTL_OUT(req, tmparg, outlen); 1065100833Struckman free(tmparg, M_SYSCTLTMP); 106611863Sphk 106745140Sphk if (error || !req->newptr) 106812243Sphk return (error); 106911863Sphk 107045140Sphk if ((req->newlen - req->newidx) >= arg2) { 107145140Sphk error = EINVAL; 107212243Sphk } else { 107312243Sphk arg2 = (req->newlen - req->newidx); 107412243Sphk error = SYSCTL_IN(req, arg1, arg2); 107512243Sphk ((char *)arg1)[arg2] = '\0'; 107611863Sphk } 107712131Sphk 107812131Sphk return (error); 107911863Sphk} 108011863Sphk 108112243Sphk/* 108212243Sphk * Handle any kind of opaque data. 108312243Sphk * arg1 points to it, arg2 is the size. 108412243Sphk */ 108512243Sphk 108611865Sphkint 108762573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 108811863Sphk{ 1089120803Sbms int error, tries; 1090120803Sbms u_int generation; 1091120813Sbms struct sysctl_req req2; 109212243Sphk 1093100833Struckman /* 1094120803Sbms * Attempt to get a coherent snapshot, by using the thread 1095120803Sbms * pre-emption counter updated from within mi_switch() to 1096120803Sbms * determine if we were pre-empted during a bcopy() or 1097120803Sbms * copyout(). Make 3 attempts at doing this before giving up. 1098120803Sbms * If we encounter an error, stop immediately. 1099100833Struckman */ 1100120803Sbms tries = 0; 1101120813Sbms req2 = *req; 1102120813Sbmsretry: 1103120813Sbms generation = curthread->td_generation; 1104120813Sbms error = SYSCTL_OUT(req, arg1, arg2); 1105120813Sbms if (error) 1106120813Sbms return (error); 1107120813Sbms tries++; 1108120813Sbms if (generation != curthread->td_generation && tries < 3) { 1109120813Sbms *req = req2; 1110120813Sbms goto retry; 1111120813Sbms } 111212243Sphk 111312243Sphk error = SYSCTL_IN(req, arg1, arg2); 111412243Sphk 111512243Sphk return (error); 111612243Sphk} 111712243Sphk 111812260Sphk/* 111912260Sphk * Transfer functions to/from kernel space. 112012260Sphk * XXX: rather untested at this point 112112260Sphk */ 112212260Sphkstatic int 112338517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 112412243Sphk{ 112538517Sdfr size_t i = 0; 112612260Sphk 112712260Sphk if (req->oldptr) { 112838517Sdfr i = l; 112973971Stmm if (req->oldlen <= req->oldidx) 113073971Stmm i = 0; 113173971Stmm else 113273971Stmm if (i > req->oldlen - req->oldidx) 113373971Stmm i = req->oldlen - req->oldidx; 113412260Sphk if (i > 0) 113517971Sbde bcopy(p, (char *)req->oldptr + req->oldidx, i); 113612243Sphk } 1137192144Skib req->oldidx += l; 113816282Snate if (req->oldptr && i != l) 113911863Sphk return (ENOMEM); 114012260Sphk return (0); 114112243Sphk} 114212243Sphk 114312260Sphkstatic int 114438517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 114512243Sphk{ 114612260Sphk if (!req->newptr) 1147139483Spjd return (0); 114812260Sphk if (req->newlen - req->newidx < l) 114911863Sphk return (EINVAL); 115017971Sbde bcopy((char *)req->newptr + req->newidx, p, l); 115112243Sphk req->newidx += l; 115212131Sphk return (0); 115311863Sphk} 115411863Sphk 115516282Snateint 115683366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1157136404Speter size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 115816282Snate{ 115916282Snate int error = 0; 116016282Snate struct sysctl_req req; 116116282Snate 116216282Snate bzero(&req, sizeof req); 116316282Snate 116486183Srwatson req.td = td; 1165136404Speter req.flags = flags; 116616282Snate 116716282Snate if (oldlenp) { 116816282Snate req.oldlen = *oldlenp; 116916282Snate } 1170127052Struckman req.validlen = req.oldlen; 117116282Snate 117216282Snate if (old) { 117316282Snate req.oldptr= old; 117416282Snate } 117516282Snate 117677646Sdd if (new != NULL) { 117716282Snate req.newlen = newlen; 117816282Snate req.newptr = new; 117916282Snate } 118016282Snate 118116282Snate req.oldfunc = sysctl_old_kernel; 118216282Snate req.newfunc = sysctl_new_kernel; 1183120781Sbms req.lock = REQ_LOCKED; 118416282Snate 1185188232Sjhb SYSCTL_SLOCK(); 118616282Snate error = sysctl_root(0, name, namelen, &req); 1187188232Sjhb SYSCTL_SUNLOCK(); 1188120813Sbms 1189127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1190127052Struckman vsunlock(req.oldptr, req.validlen); 119116282Snate 119216282Snate if (error && error != ENOMEM) 119316282Snate return (error); 119416282Snate 119516282Snate if (retval) { 1196127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1197127052Struckman *retval = req.validlen; 119816282Snate else 119916282Snate *retval = req.oldidx; 120016282Snate } 120116282Snate return (error); 120216282Snate} 120316282Snate 120476834Sjlemonint 120583366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1206136404Speter void *new, size_t newlen, size_t *retval, int flags) 120776834Sjlemon{ 120876834Sjlemon int oid[CTL_MAXNAME]; 120978620Smjacob size_t oidlen, plen; 121078620Smjacob int error; 121176834Sjlemon 121276834Sjlemon oid[0] = 0; /* sysctl internal magic */ 121376834Sjlemon oid[1] = 3; /* name2oid */ 121476834Sjlemon oidlen = sizeof(oid); 121576834Sjlemon 121683366Sjulian error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1217136404Speter (void *)name, strlen(name), &plen, flags); 121876834Sjlemon if (error) 121976834Sjlemon return (error); 122076834Sjlemon 122183366Sjulian error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1222136404Speter new, newlen, retval, flags); 122376834Sjlemon return (error); 122476834Sjlemon} 122576834Sjlemon 122612260Sphk/* 122712260Sphk * Transfer function to/from user space. 122812260Sphk */ 122912260Sphkstatic int 123038517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 123112243Sphk{ 123238517Sdfr int error = 0; 1233126253Struckman size_t i, len, origidx; 123412243Sphk 1235126253Struckman origidx = req->oldidx; 1236192144Skib req->oldidx += l; 1237192144Skib if (req->oldptr == NULL) 1238126253Struckman return (0); 1239148864Scsjp /* 1240148864Scsjp * If we have not wired the user supplied buffer and we are currently 1241148864Scsjp * holding locks, drop a witness warning, as it's possible that 1242148864Scsjp * write operations to the user page can sleep. 1243148864Scsjp */ 1244148864Scsjp if (req->lock != REQ_WIRED) 1245111883Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1246111883Sjhb "sysctl_old_user()"); 1247126253Struckman i = l; 1248127052Struckman len = req->validlen; 1249126253Struckman if (len <= origidx) 1250126253Struckman i = 0; 1251126253Struckman else { 1252126253Struckman if (i > len - origidx) 1253126253Struckman i = len - origidx; 1254126253Struckman error = copyout(p, (char *)req->oldptr + origidx, i); 125512260Sphk } 125612243Sphk if (error) 125712243Sphk return (error); 1258126253Struckman if (i < l) 125912243Sphk return (ENOMEM); 126012260Sphk return (0); 126112243Sphk} 126212243Sphk 126312260Sphkstatic int 126438517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l) 126512243Sphk{ 126612285Sphk int error; 126712260Sphk 126812260Sphk if (!req->newptr) 1269139483Spjd return (0); 127012260Sphk if (req->newlen - req->newidx < l) 127112243Sphk return (EINVAL); 1272148873Scsjp WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1273148873Scsjp "sysctl_new_user()"); 127417971Sbde error = copyin((char *)req->newptr + req->newidx, p, l); 127512243Sphk req->newidx += l; 127612243Sphk return (error); 127712243Sphk} 127812243Sphk 1279100487Struckman/* 1280100487Struckman * Wire the user space destination buffer. If set to a value greater than 1281100487Struckman * zero, the len parameter limits the maximum amount of wired memory. 1282100487Struckman */ 1283126253Struckmanint 1284100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1285100487Struckman{ 1286126253Struckman int ret; 1287192160Sdes size_t wiredlen; 1288126253Struckman 1289126253Struckman wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1290126253Struckman ret = 0; 1291120781Sbms if (req->lock == REQ_LOCKED && req->oldptr && 1292120781Sbms req->oldfunc == sysctl_old_user) { 1293127050Struckman if (wiredlen != 0) { 1294127050Struckman ret = vslock(req->oldptr, wiredlen); 1295130327Sgreen if (ret != 0) { 1296130327Sgreen if (ret != ENOMEM) 1297130327Sgreen return (ret); 1298130327Sgreen wiredlen = 0; 1299130327Sgreen } 1300126253Struckman } 1301127050Struckman req->lock = REQ_WIRED; 1302127052Struckman req->validlen = wiredlen; 1303100487Struckman } 1304127050Struckman return (0); 1305100487Struckman} 1306100487Struckman 13071541Srgrimesint 130853977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 130953977Sgreen int *nindx, struct sysctl_req *req) 131012131Sphk{ 131144078Sdfr struct sysctl_oid *oid; 131253977Sgreen int indx; 131312131Sphk 1314188232Sjhb SYSCTL_ASSERT_LOCKED(); 131553977Sgreen oid = SLIST_FIRST(&sysctl__children); 131612131Sphk indx = 0; 131744078Sdfr while (oid && indx < CTL_MAXNAME) { 131844078Sdfr if (oid->oid_number == name[indx]) { 131912131Sphk indx++; 132044078Sdfr if (oid->oid_kind & CTLFLAG_NOLOCK) 1321120781Sbms req->lock = REQ_UNLOCKED; 132244078Sdfr if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 132353977Sgreen if (oid->oid_handler != NULL || 132453977Sgreen indx == namelen) { 132553977Sgreen *noid = oid; 132653977Sgreen if (nindx != NULL) 132753977Sgreen *nindx = indx; 132853977Sgreen return (0); 132953977Sgreen } 133053977Sgreen oid = SLIST_FIRST( 133153977Sgreen (struct sysctl_oid_list *)oid->oid_arg1); 133253977Sgreen } else if (indx == namelen) { 133353977Sgreen *noid = oid; 133453977Sgreen if (nindx != NULL) 133553977Sgreen *nindx = indx; 133653977Sgreen return (0); 133712131Sphk } else { 133853977Sgreen return (ENOTDIR); 133912131Sphk } 134012131Sphk } else { 134144078Sdfr oid = SLIST_NEXT(oid, oid_link); 134212131Sphk } 134312131Sphk } 134453977Sgreen return (ENOENT); 134553977Sgreen} 134653977Sgreen 134753977Sgreen/* 134853977Sgreen * Traverse our tree, and find the right node, execute whatever it points 134953977Sgreen * to, and return the resulting error code. 135053977Sgreen */ 135153977Sgreen 1352104094Sphkstatic int 135362573Sphksysctl_root(SYSCTL_HANDLER_ARGS) 135453977Sgreen{ 135553977Sgreen struct sysctl_oid *oid; 1356109246Sdillon int error, indx, lvl; 135753977Sgreen 1358188232Sjhb SYSCTL_ASSERT_LOCKED(); 1359186564Sed 136053977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 136153977Sgreen if (error) 136253977Sgreen return (error); 136353977Sgreen 136453977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 136553977Sgreen /* 136653977Sgreen * You can't call a sysctl when it's a node, but has 136753977Sgreen * no handler. Inform the user that it's a node. 136853977Sgreen * The indx may or may not be the same as namelen. 136953977Sgreen */ 137053977Sgreen if (oid->oid_handler == NULL) 137153977Sgreen return (EISDIR); 137253977Sgreen } 137353977Sgreen 137483968Srwatson /* Is this sysctl writable? */ 137583968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 137612131Sphk return (EPERM); 137712131Sphk 137892953Srwatson KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 137992953Srwatson 138083968Srwatson /* Is this sysctl sensitive to securelevels? */ 138183968Srwatson if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1382109246Sdillon lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1383109246Sdillon error = securelevel_gt(req->td->td_ucred, lvl); 138492953Srwatson if (error) 138592953Srwatson return (error); 138683968Srwatson } 138712910Sphk 138883968Srwatson /* Is this sysctl writable by only privileged users? */ 138983968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 139092953Srwatson if (oid->oid_kind & CTLFLAG_PRISON) 1391170587Srwatson error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL); 139292953Srwatson else 1393164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_WRITE); 139492953Srwatson if (error) 139592953Srwatson return (error); 139683968Srwatson } 139783968Srwatson 139844078Sdfr if (!oid->oid_handler) 1399139483Spjd return (EINVAL); 140012131Sphk 1401126121Spjd if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1402132776Skan arg1 = (int *)arg1 + indx; 1403126121Spjd arg2 -= indx; 1404126121Spjd } else { 1405126121Spjd arg1 = oid->oid_arg1; 1406126121Spjd arg2 = oid->oid_arg2; 1407126121Spjd } 1408126121Spjd#ifdef MAC 1409172930Srwatson error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1410126121Spjd req); 1411126121Spjd if (error != 0) 1412126121Spjd return (error); 1413126121Spjd#endif 1414187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1415187656Sjhb mtx_lock(&Giant); 1416126121Spjd error = oid->oid_handler(oid, arg1, arg2, req); 1417187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1418187656Sjhb mtx_unlock(&Giant); 1419126121Spjd 142053977Sgreen return (error); 142112131Sphk} 142212131Sphk 142312221Sbde#ifndef _SYS_SYSPROTO_H_ 142412171Sphkstruct sysctl_args { 142512171Sphk int *name; 142612171Sphk u_int namelen; 142712171Sphk void *old; 142812171Sphk size_t *oldlenp; 142912171Sphk void *new; 143012171Sphk size_t newlen; 143112171Sphk}; 143212221Sbde#endif 143312131Sphkint 143483366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap) 14351541Srgrimes{ 1436188232Sjhb int error, i, name[CTL_MAXNAME]; 143738517Sdfr size_t j; 14381541Srgrimes 14391541Srgrimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 14401541Srgrimes return (EINVAL); 144111863Sphk 14423308Sphk error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 14433308Sphk if (error) 14441541Srgrimes return (error); 14451541Srgrimes 144683366Sjulian error = userland_sysctl(td, name, uap->namelen, 144712171Sphk uap->old, uap->oldlenp, 0, 1448136404Speter uap->new, uap->newlen, &j, 0); 144912260Sphk if (error && error != ENOMEM) 1450186564Sed return (error); 1451186664Sed if (uap->oldlenp) { 1452188232Sjhb i = copyout(&j, uap->oldlenp, sizeof(j)); 1453186664Sed if (i) 1454186664Sed return (i); 1455186664Sed } 145612260Sphk return (error); 145712171Sphk} 145812171Sphk 145912171Sphk/* 146012171Sphk * This is used from various compatibility syscalls too. That's why name 146112171Sphk * must be in kernel space. 146212171Sphk */ 146312171Sphkint 146483366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1465136404Speter size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1466136404Speter int flags) 146712171Sphk{ 1468192125Sjhb int error = 0, memlocked; 1469127052Struckman struct sysctl_req req; 147012171Sphk 147112243Sphk bzero(&req, sizeof req); 147212243Sphk 147386183Srwatson req.td = td; 1474136404Speter req.flags = flags; 147512285Sphk 147612171Sphk if (oldlenp) { 147712171Sphk if (inkernel) { 147812243Sphk req.oldlen = *oldlenp; 147912171Sphk } else { 148012260Sphk error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 148112171Sphk if (error) 148212171Sphk return (error); 148312171Sphk } 148412171Sphk } 1485127052Struckman req.validlen = req.oldlen; 148612171Sphk 148712243Sphk if (old) { 148852644Sphk if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 148912243Sphk return (EFAULT); 149012243Sphk req.oldptr= old; 149112243Sphk } 149212131Sphk 149377646Sdd if (new != NULL) { 1494172038Srwatson if (!useracc(new, newlen, VM_PROT_READ)) 149512243Sphk return (EFAULT); 149612243Sphk req.newlen = newlen; 149712243Sphk req.newptr = new; 149811863Sphk } 149912131Sphk 150012243Sphk req.oldfunc = sysctl_old_user; 150112243Sphk req.newfunc = sysctl_new_user; 1502120781Sbms req.lock = REQ_LOCKED; 150311863Sphk 1504189707Sjhb#ifdef KTRACE 1505189707Sjhb if (KTRPOINT(curthread, KTR_SYSCTL)) 1506189707Sjhb ktrsysctl(name, namelen); 1507189707Sjhb#endif 1508192125Sjhb 1509192125Sjhb if (req.oldlen > PAGE_SIZE) { 1510192125Sjhb memlocked = 1; 1511192125Sjhb sx_xlock(&sysctlmemlock); 1512192125Sjhb } else 1513192125Sjhb memlocked = 0; 1514194252Sjamie CURVNET_SET(TD_TO_VNET(td)); 151512429Sphk 1516185983Skib for (;;) { 1517127052Struckman req.oldidx = 0; 1518127052Struckman req.newidx = 0; 1519192125Sjhb SYSCTL_SLOCK(); 1520127052Struckman error = sysctl_root(0, name, namelen, &req); 1521192125Sjhb SYSCTL_SUNLOCK(); 1522185983Skib if (error != EAGAIN) 1523185983Skib break; 1524185983Skib uio_yield(); 1525185983Skib } 152612243Sphk 1527186564Sed CURVNET_RESTORE(); 1528186564Sed 1529127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1530127052Struckman vsunlock(req.oldptr, req.validlen); 1531192125Sjhb if (memlocked) 1532192125Sjhb sx_xunlock(&sysctlmemlock); 153312429Sphk 153412260Sphk if (error && error != ENOMEM) 153512260Sphk return (error); 153612260Sphk 153712260Sphk if (retval) { 1538127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1539127052Struckman *retval = req.validlen; 154012260Sphk else 154112260Sphk *retval = req.oldidx; 154211863Sphk } 154312260Sphk return (error); 15441541Srgrimes} 1545