kern_sysctl.c revision 196176
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 196176 2009-08-13 10:26:34Z bz $"); 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> 57189707Sjhb#ifdef KTRACE 58189707Sjhb#include <sys/ktrace.h> 59189707Sjhb#endif 60163606Srwatson 61195699Srwatson#include <net/vnet.h> 62195699Srwatson 63163606Srwatson#include <security/mac/mac_framework.h> 64163606Srwatson 6512645Sbde#include <vm/vm.h> 6612662Sdg#include <vm/vm_extern.h> 6712645Sbde 6830354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 6963212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 70100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 7130309Sphk 7212429Sphk/* 73188232Sjhb * The sysctllock protects the MIB tree. It also protects sysctl 74188232Sjhb * contexts used with dynamic sysctls. The sysctl_register_oid() and 75188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already 76188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are 77188232Sjhb * provided for the few places in the kernel which need to use that 78188232Sjhb * API rather than using the dynamic API. Use of the dynamic API is 79188232Sjhb * strongly encouraged for most code. 80188232Sjhb * 81192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for 82192125Sjhb * sysctl requests. This is implemented by serializing any userland 83192125Sjhb * sysctl requests larger than a single page via an exclusive lock. 8412429Sphk */ 8593625Srwatsonstatic struct sx sysctllock; 86192125Sjhbstatic struct sx sysctlmemlock; 8712429Sphk 88188232Sjhb#define SYSCTL_SLOCK() sx_slock(&sysctllock) 89188232Sjhb#define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock) 90188232Sjhb#define SYSCTL_XLOCK() sx_xlock(&sysctllock) 91188232Sjhb#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 92188232Sjhb#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 93188232Sjhb#define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED) 94112107Sjhb#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 9593616Salfred 9662573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS); 9712429Sphk 9844078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */ 9912152Sphk 100188232Sjhbstatic int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 101188232Sjhb int recurse); 102188232Sjhb 10363212Sabialstatic struct sysctl_oid * 10463212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 10563212Sabial{ 10663212Sabial struct sysctl_oid *oidp; 10763212Sabial 108188232Sjhb SYSCTL_ASSERT_LOCKED(); 10963212Sabial SLIST_FOREACH(oidp, list, oid_link) { 11063212Sabial if (strcmp(oidp->oid_name, name) == 0) { 11163212Sabial return (oidp); 11263212Sabial } 11363212Sabial } 11463212Sabial return (NULL); 11563212Sabial} 11663212Sabial 11712623Sphk/* 11812623Sphk * Initialization of the MIB tree. 11912623Sphk * 12044078Sdfr * Order by number in each list. 12112623Sphk */ 122188232Sjhbvoid 123188232Sjhbsysctl_lock(void) 124188232Sjhb{ 12512429Sphk 126188232Sjhb SYSCTL_XLOCK(); 127188232Sjhb} 128188232Sjhb 12980338Sroamvoid 130188232Sjhbsysctl_unlock(void) 131188232Sjhb{ 132188232Sjhb 133188232Sjhb SYSCTL_XUNLOCK(); 134188232Sjhb} 135188232Sjhb 136188232Sjhbvoid 13780338Sroamsysctl_register_oid(struct sysctl_oid *oidp) 13812152Sphk{ 13944078Sdfr struct sysctl_oid_list *parent = oidp->oid_parent; 14044078Sdfr struct sysctl_oid *p; 14144078Sdfr struct sysctl_oid *q; 14212197Sbde 14344078Sdfr /* 14463212Sabial * First check if another oid with the same name already 14563212Sabial * exists in the parent's list. 14663212Sabial */ 147188232Sjhb SYSCTL_ASSERT_XLOCKED(); 14863212Sabial p = sysctl_find_oidname(oidp->oid_name, parent); 14963212Sabial if (p != NULL) { 15063212Sabial if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 15163212Sabial p->oid_refcnt++; 15263212Sabial return; 15363212Sabial } else { 15463212Sabial printf("can't re-use a leaf (%s)!\n", p->oid_name); 15563212Sabial return; 15663212Sabial } 15763212Sabial } 15863212Sabial /* 15944078Sdfr * If this oid has a number OID_AUTO, give it a number which 16080339Sroam * is greater than any current oid. 16180339Sroam * NOTE: DO NOT change the starting value here, change it in 16280339Sroam * <sys/sysctl.h>, and make sure it is at least 256 to 16380339Sroam * accomodate e.g. net.inet.raw as a static sysctl node. 16444078Sdfr */ 16544078Sdfr if (oidp->oid_number == OID_AUTO) { 16680339Sroam static int newoid = CTL_AUTO_START; 16771510Smckusick 16871510Smckusick oidp->oid_number = newoid++; 16971510Smckusick if (newoid == 0x7fffffff) 17071510Smckusick panic("out of oids"); 17144078Sdfr } 17284832Sroam#if 0 17384832Sroam else if (oidp->oid_number >= CTL_AUTO_START) { 17484832Sroam /* do not panic; this happens when unregistering sysctl sets */ 17584832Sroam printf("static sysctl oid too high: %d", oidp->oid_number); 17684832Sroam } 17784832Sroam#endif 17844078Sdfr 17944078Sdfr /* 18044078Sdfr * Insert the oid into the parent's list in order. 18144078Sdfr */ 18244078Sdfr q = NULL; 18344078Sdfr SLIST_FOREACH(p, parent, oid_link) { 18444078Sdfr if (oidp->oid_number < p->oid_number) 18544078Sdfr break; 18644078Sdfr q = p; 18744078Sdfr } 18844078Sdfr if (q) 18944078Sdfr SLIST_INSERT_AFTER(q, oidp, oid_link); 19044078Sdfr else 19144078Sdfr SLIST_INSERT_HEAD(parent, oidp, oid_link); 19212152Sphk} 19312131Sphk 19480338Sroamvoid 19580338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp) 19612152Sphk{ 197115391Smux struct sysctl_oid *p; 198115391Smux int error; 199115391Smux 200188232Sjhb SYSCTL_ASSERT_XLOCKED(); 201115391Smux error = ENOENT; 202115391Smux if (oidp->oid_number == OID_AUTO) { 203115391Smux error = EINVAL; 204115391Smux } else { 205115391Smux SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 206115391Smux if (p == oidp) { 207115391Smux SLIST_REMOVE(oidp->oid_parent, oidp, 208115391Smux sysctl_oid, oid_link); 209115391Smux error = 0; 210115391Smux break; 211115391Smux } 212115391Smux } 213115391Smux } 214115391Smux 215115391Smux /* 216115391Smux * This can happen when a module fails to register and is 217115391Smux * being unloaded afterwards. It should not be a panic() 218115391Smux * for normal use. 219115391Smux */ 220115391Smux if (error) 221115391Smux printf("%s: failed to unregister sysctl\n", __func__); 22244078Sdfr} 22312152Sphk 22463212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */ 22563212Sabialint 22663212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c) 22763212Sabial{ 22863212Sabial 22963212Sabial if (c == NULL) { 23063212Sabial return (EINVAL); 23163212Sabial } 232188232Sjhb 233188232Sjhb /* 234188232Sjhb * No locking here, the caller is responsible for not adding 235188232Sjhb * new nodes to a context until after this function has 236188232Sjhb * returned. 237188232Sjhb */ 23863212Sabial TAILQ_INIT(c); 23963212Sabial return (0); 24063212Sabial} 24163212Sabial 24263212Sabial/* Free the context, and destroy all dynamic oids registered in this context */ 24363212Sabialint 24463212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist) 24563212Sabial{ 24663212Sabial struct sysctl_ctx_entry *e, *e1; 24763212Sabial int error; 24863212Sabial 24963212Sabial error = 0; 25063212Sabial /* 25163212Sabial * First perform a "dry run" to check if it's ok to remove oids. 25263212Sabial * XXX FIXME 25363212Sabial * XXX This algorithm is a hack. But I don't know any 25463212Sabial * XXX better solution for now... 25563212Sabial */ 256188232Sjhb SYSCTL_XLOCK(); 25763212Sabial TAILQ_FOREACH(e, clist, link) { 258188232Sjhb error = sysctl_remove_oid_locked(e->entry, 0, 0); 25963212Sabial if (error) 26063212Sabial break; 26163212Sabial } 26263212Sabial /* 26363212Sabial * Restore deregistered entries, either from the end, 26463212Sabial * or from the place where error occured. 26563212Sabial * e contains the entry that was not unregistered 26663212Sabial */ 26763212Sabial if (error) 26863212Sabial e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 26963212Sabial else 27063212Sabial e1 = TAILQ_LAST(clist, sysctl_ctx_list); 27163212Sabial while (e1 != NULL) { 27263212Sabial sysctl_register_oid(e1->entry); 27363212Sabial e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 27463212Sabial } 275188232Sjhb if (error) { 276188232Sjhb SYSCTL_XUNLOCK(); 27763212Sabial return(EBUSY); 278188232Sjhb } 27963212Sabial /* Now really delete the entries */ 28063212Sabial e = TAILQ_FIRST(clist); 28163212Sabial while (e != NULL) { 28263212Sabial e1 = TAILQ_NEXT(e, link); 283188232Sjhb error = sysctl_remove_oid_locked(e->entry, 1, 0); 28463212Sabial if (error) 28563212Sabial panic("sysctl_remove_oid: corrupt tree, entry: %s", 28663212Sabial e->entry->oid_name); 28763212Sabial free(e, M_SYSCTLOID); 28863212Sabial e = e1; 28963212Sabial } 290188232Sjhb SYSCTL_XUNLOCK(); 29163212Sabial return (error); 29263212Sabial} 29363212Sabial 29463212Sabial/* Add an entry to the context */ 29563212Sabialstruct sysctl_ctx_entry * 29663212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 29763212Sabial{ 29863212Sabial struct sysctl_ctx_entry *e; 29963212Sabial 300188232Sjhb SYSCTL_ASSERT_XLOCKED(); 30163212Sabial if (clist == NULL || oidp == NULL) 30263212Sabial return(NULL); 303111119Simp e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 30463212Sabial e->entry = oidp; 30563212Sabial TAILQ_INSERT_HEAD(clist, e, link); 30663212Sabial return (e); 30763212Sabial} 30863212Sabial 30963212Sabial/* Find an entry in the context */ 31063212Sabialstruct sysctl_ctx_entry * 31163212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 31263212Sabial{ 31363212Sabial struct sysctl_ctx_entry *e; 31463212Sabial 315188232Sjhb SYSCTL_ASSERT_LOCKED(); 31663212Sabial if (clist == NULL || oidp == NULL) 31763212Sabial return(NULL); 31871999Sphk TAILQ_FOREACH(e, clist, link) { 31963212Sabial if(e->entry == oidp) 32063212Sabial return(e); 32163212Sabial } 32263212Sabial return (e); 32363212Sabial} 32463212Sabial 32544078Sdfr/* 32663212Sabial * Delete an entry from the context. 32763212Sabial * NOTE: this function doesn't free oidp! You have to remove it 32863212Sabial * with sysctl_remove_oid(). 32963212Sabial */ 33063212Sabialint 33163212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 33263212Sabial{ 33363212Sabial struct sysctl_ctx_entry *e; 33463212Sabial 33563212Sabial if (clist == NULL || oidp == NULL) 33663212Sabial return (EINVAL); 337188232Sjhb SYSCTL_XLOCK(); 33863212Sabial e = sysctl_ctx_entry_find(clist, oidp); 33963212Sabial if (e != NULL) { 34063212Sabial TAILQ_REMOVE(clist, e, link); 341188232Sjhb SYSCTL_XUNLOCK(); 34263212Sabial free(e, M_SYSCTLOID); 34363212Sabial return (0); 344188232Sjhb } else { 345188232Sjhb SYSCTL_XUNLOCK(); 34663212Sabial return (ENOENT); 347188232Sjhb } 34863212Sabial} 34963212Sabial 35063212Sabial/* 35163212Sabial * Remove dynamically created sysctl trees. 35263212Sabial * oidp - top of the tree to be removed 35363212Sabial * del - if 0 - just deregister, otherwise free up entries as well 35463212Sabial * recurse - if != 0 traverse the subtree to be deleted 35563212Sabial */ 35663212Sabialint 35763212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 35863212Sabial{ 359188232Sjhb int error; 360188232Sjhb 361188232Sjhb SYSCTL_XLOCK(); 362188232Sjhb error = sysctl_remove_oid_locked(oidp, del, recurse); 363188232Sjhb SYSCTL_XUNLOCK(); 364188232Sjhb return (error); 365188232Sjhb} 366188232Sjhb 367188232Sjhbstatic int 368188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 369188232Sjhb{ 37063212Sabial struct sysctl_oid *p; 37163212Sabial int error; 37263212Sabial 373188232Sjhb SYSCTL_ASSERT_XLOCKED(); 37463212Sabial if (oidp == NULL) 37563212Sabial return(EINVAL); 37663212Sabial if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 37763212Sabial printf("can't remove non-dynamic nodes!\n"); 37863212Sabial return (EINVAL); 37963212Sabial } 38063212Sabial /* 38163212Sabial * WARNING: normal method to do this should be through 38263212Sabial * sysctl_ctx_free(). Use recursing as the last resort 38363212Sabial * method to purge your sysctl tree of leftovers... 38463212Sabial * However, if some other code still references these nodes, 38563212Sabial * it will panic. 38663212Sabial */ 38763212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 38863212Sabial if (oidp->oid_refcnt == 1) { 38963212Sabial SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 39063212Sabial if (!recurse) 39163212Sabial return (ENOTEMPTY); 392188232Sjhb error = sysctl_remove_oid_locked(p, del, 393188232Sjhb recurse); 39463212Sabial if (error) 39563212Sabial return (error); 39663212Sabial } 39763212Sabial if (del) 39863212Sabial free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 39963212Sabial } 40063212Sabial } 40163212Sabial if (oidp->oid_refcnt > 1 ) { 40263212Sabial oidp->oid_refcnt--; 40363212Sabial } else { 40463212Sabial if (oidp->oid_refcnt == 0) { 40563212Sabial printf("Warning: bad oid_refcnt=%u (%s)!\n", 40663212Sabial oidp->oid_refcnt, oidp->oid_name); 40763212Sabial return (EINVAL); 40863212Sabial } 40963212Sabial sysctl_unregister_oid(oidp); 41063212Sabial if (del) { 411141433Sphk if (oidp->oid_descr) 412141433Sphk free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID); 41363978Speter free((void *)(uintptr_t)(const void *)oidp->oid_name, 41463978Speter M_SYSCTLOID); 41563212Sabial free(oidp, M_SYSCTLOID); 41663212Sabial } 41763212Sabial } 41863212Sabial return (0); 41963212Sabial} 42063212Sabial 42163212Sabial/* 42263212Sabial * Create new sysctls at run time. 42363212Sabial * clist may point to a valid context initialized with sysctl_ctx_init(). 42463212Sabial */ 42563212Sabialstruct sysctl_oid * 42663212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 42770679Sjhb int number, const char *name, int kind, void *arg1, int arg2, 42870679Sjhb int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 42963212Sabial{ 43063212Sabial struct sysctl_oid *oidp; 43163212Sabial ssize_t len; 43263978Speter char *newname; 43363212Sabial 43463212Sabial /* You have to hook up somewhere.. */ 43563212Sabial if (parent == NULL) 43663212Sabial return(NULL); 43763212Sabial /* Check if the node already exists, otherwise create it */ 438188232Sjhb SYSCTL_XLOCK(); 43963212Sabial oidp = sysctl_find_oidname(name, parent); 44063212Sabial if (oidp != NULL) { 44163212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 44263212Sabial oidp->oid_refcnt++; 44363212Sabial /* Update the context */ 44463212Sabial if (clist != NULL) 44563212Sabial sysctl_ctx_entry_add(clist, oidp); 446188232Sjhb SYSCTL_XUNLOCK(); 44763212Sabial return (oidp); 44863212Sabial } else { 449188232Sjhb SYSCTL_XUNLOCK(); 45063212Sabial printf("can't re-use a leaf (%s)!\n", name); 45163212Sabial return (NULL); 45263212Sabial } 45363212Sabial } 454111119Simp oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 45563212Sabial oidp->oid_parent = parent; 45663212Sabial SLIST_NEXT(oidp, oid_link) = NULL; 45763212Sabial oidp->oid_number = number; 45863212Sabial oidp->oid_refcnt = 1; 45963212Sabial len = strlen(name); 460111119Simp newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 46163978Speter bcopy(name, newname, len + 1); 46263978Speter newname[len] = '\0'; 46363978Speter oidp->oid_name = newname; 46463212Sabial oidp->oid_handler = handler; 46563212Sabial oidp->oid_kind = CTLFLAG_DYN | kind; 46663212Sabial if ((kind & CTLTYPE) == CTLTYPE_NODE) { 46763212Sabial /* Allocate space for children */ 468132776Skan SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list), 469132776Skan M_SYSCTLOID, M_WAITOK)); 47063212Sabial SLIST_INIT(SYSCTL_CHILDREN(oidp)); 47163212Sabial } else { 47263212Sabial oidp->oid_arg1 = arg1; 47363212Sabial oidp->oid_arg2 = arg2; 47463212Sabial } 47563212Sabial oidp->oid_fmt = fmt; 47688006Sluigi if (descr) { 47788006Sluigi int len = strlen(descr) + 1; 478141433Sphk oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK); 479141433Sphk if (oidp->oid_descr) 480141433Sphk strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr); 48188006Sluigi } 48263212Sabial /* Update the context, if used */ 48363212Sabial if (clist != NULL) 48463212Sabial sysctl_ctx_entry_add(clist, oidp); 48563212Sabial /* Register this oid */ 48663212Sabial sysctl_register_oid(oidp); 487188232Sjhb SYSCTL_XUNLOCK(); 48863212Sabial return (oidp); 48963212Sabial} 49063212Sabial 49163212Sabial/* 492174113Speter * Rename an existing oid. 493174113Speter */ 494174113Spetervoid 495174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 496174113Speter{ 497174113Speter ssize_t len; 498174113Speter char *newname; 499174113Speter void *oldname; 500174113Speter 501174113Speter len = strlen(name); 502174113Speter newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 503174113Speter bcopy(name, newname, len + 1); 504174113Speter newname[len] = '\0'; 505188232Sjhb SYSCTL_XLOCK(); 506188232Sjhb oldname = (void *)(uintptr_t)(const void *)oidp->oid_name; 507174113Speter oidp->oid_name = newname; 508188232Sjhb SYSCTL_XUNLOCK(); 509174113Speter free(oldname, M_SYSCTLOID); 510174113Speter} 511174113Speter 512174113Speter/* 513126319Sdes * Reparent an existing oid. 514126319Sdes */ 515126319Sdesint 516126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 517126319Sdes{ 518126319Sdes struct sysctl_oid *oidp; 519126319Sdes 520188232Sjhb SYSCTL_XLOCK(); 521188232Sjhb if (oid->oid_parent == parent) { 522188232Sjhb SYSCTL_XUNLOCK(); 523126319Sdes return (0); 524188232Sjhb } 525126319Sdes oidp = sysctl_find_oidname(oid->oid_name, parent); 526188232Sjhb if (oidp != NULL) { 527188232Sjhb SYSCTL_XUNLOCK(); 528126319Sdes return (EEXIST); 529188232Sjhb } 530126319Sdes sysctl_unregister_oid(oid); 531126319Sdes oid->oid_parent = parent; 532126319Sdes oid->oid_number = OID_AUTO; 533126319Sdes sysctl_register_oid(oid); 534188232Sjhb SYSCTL_XUNLOCK(); 535126319Sdes return (0); 536126319Sdes} 537126319Sdes 538126319Sdes/* 53944078Sdfr * Register the kernel's oids on startup. 54044078Sdfr */ 54178161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid); 54212152Sphk 54380338Sroamstatic void 54480338Sroamsysctl_register_all(void *arg) 54538869Sbde{ 54678161Speter struct sysctl_oid **oidp; 54778161Speter 548192125Sjhb sx_init(&sysctlmemlock, "sysctl mem"); 54993625Srwatson SYSCTL_INIT(); 550188232Sjhb SYSCTL_XLOCK(); 55178161Speter SET_FOREACH(oidp, sysctl_set) 55278161Speter sysctl_register_oid(*oidp); 553188232Sjhb SYSCTL_XUNLOCK(); 55438869Sbde} 55544078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 55644078Sdfr 55712623Sphk/* 55812623Sphk * "Staff-functions" 55912623Sphk * 56012650Sphk * These functions implement a presently undocumented interface 56112650Sphk * used by the sysctl program to walk the tree, and get the type 56212650Sphk * so it can print the value. 56312650Sphk * This interface is under work and consideration, and should probably 56412650Sphk * be killed with a big axe by the first person who can find the time. 56512650Sphk * (be aware though, that the proper interface isn't as obvious as it 56612650Sphk * may seem, there are various conflicting requirements. 56712650Sphk * 56812623Sphk * {0,0} printf the entire MIB-tree. 56912623Sphk * {0,1,...} return the name of the "..." OID. 57042467Sphk * {0,2,...} return the next OID. 57112623Sphk * {0,3} return the OID of the name in "new" 57212650Sphk * {0,4,...} return the kind & format info for the "..." OID. 57388006Sluigi * {0,5,...} return the description the "..." OID. 57412623Sphk */ 57512623Sphk 576136999Srwatson#ifdef SYSCTL_DEBUG 57712152Sphkstatic void 57844078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 57912152Sphk{ 58044078Sdfr int k; 58144078Sdfr struct sysctl_oid *oidp; 58212152Sphk 583188232Sjhb SYSCTL_ASSERT_LOCKED(); 58444078Sdfr SLIST_FOREACH(oidp, l, oid_link) { 58512152Sphk 58612152Sphk for (k=0; k<i; k++) 58712152Sphk printf(" "); 58812152Sphk 58944078Sdfr printf("%d %s ", oidp->oid_number, oidp->oid_name); 59012152Sphk 59112152Sphk printf("%c%c", 59244078Sdfr oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 59344078Sdfr oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 59412152Sphk 59544078Sdfr if (oidp->oid_handler) 59615241Sphk printf(" *Handler"); 59715241Sphk 59844078Sdfr switch (oidp->oid_kind & CTLTYPE) { 59912243Sphk case CTLTYPE_NODE: 60015241Sphk printf(" Node\n"); 60144078Sdfr if (!oidp->oid_handler) { 60212152Sphk sysctl_sysctl_debug_dump_node( 60344078Sdfr oidp->oid_arg1, i+2); 60412152Sphk } 60512152Sphk break; 60612152Sphk case CTLTYPE_INT: printf(" Int\n"); break; 60712152Sphk case CTLTYPE_STRING: printf(" String\n"); break; 60812152Sphk case CTLTYPE_QUAD: printf(" Quad\n"); break; 60912152Sphk case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 61012152Sphk default: printf("\n"); 61112152Sphk } 61212152Sphk 61312152Sphk } 61412152Sphk} 61512152Sphk 61612152Sphkstatic int 61762573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 61812152Sphk{ 61987024Speter int error; 62087024Speter 621164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 62287024Speter if (error) 623139483Spjd return (error); 62444078Sdfr sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 625139483Spjd return (ENOENT); 62612152Sphk} 62712152Sphk 62812152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 62912623Sphk 0, 0, sysctl_sysctl_debug, "-", ""); 630136999Srwatson#endif 63112152Sphk 63212623Sphkstatic int 63362573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 63412623Sphk{ 63512623Sphk int *name = (int *) arg1; 63612623Sphk u_int namelen = arg2; 63744078Sdfr int error = 0; 63844078Sdfr struct sysctl_oid *oid; 63944972Sphk struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 64012623Sphk char buf[10]; 64112131Sphk 642188232Sjhb SYSCTL_ASSERT_LOCKED(); 64312623Sphk while (namelen) { 64412623Sphk if (!lsp) { 64541514Sarchie snprintf(buf,sizeof(buf),"%d",*name); 64612623Sphk if (req->oldidx) 64712623Sphk error = SYSCTL_OUT(req, ".", 1); 64812623Sphk if (!error) 64912623Sphk error = SYSCTL_OUT(req, buf, strlen(buf)); 65012623Sphk if (error) 65112623Sphk return (error); 65212623Sphk namelen--; 65312623Sphk name++; 65412623Sphk continue; 65512623Sphk } 65644972Sphk lsp2 = 0; 65744078Sdfr SLIST_FOREACH(oid, lsp, oid_link) { 65844078Sdfr if (oid->oid_number != *name) 65912623Sphk continue; 66012131Sphk 66112623Sphk if (req->oldidx) 66212623Sphk error = SYSCTL_OUT(req, ".", 1); 66312623Sphk if (!error) 66444078Sdfr error = SYSCTL_OUT(req, oid->oid_name, 66544078Sdfr strlen(oid->oid_name)); 66612623Sphk if (error) 66712623Sphk return (error); 66812623Sphk 66912623Sphk namelen--; 67012623Sphk name++; 67112623Sphk 67244972Sphk if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 67312623Sphk break; 67412623Sphk 67544078Sdfr if (oid->oid_handler) 67612623Sphk break; 67712623Sphk 67844972Sphk lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 67912623Sphk break; 68012623Sphk } 68144972Sphk lsp = lsp2; 68212623Sphk } 68312623Sphk return (SYSCTL_OUT(req, "", 1)); 68412623Sphk} 68512623Sphk 686141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 68712623Sphk 68812623Sphkstatic int 68963978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 69044078Sdfr int *next, int *len, int level, struct sysctl_oid **oidpp) 69112623Sphk{ 69244078Sdfr struct sysctl_oid *oidp; 69312623Sphk 694188232Sjhb SYSCTL_ASSERT_LOCKED(); 69512623Sphk *len = level; 69644078Sdfr SLIST_FOREACH(oidp, lsp, oid_link) { 69744078Sdfr *next = oidp->oid_number; 69844078Sdfr *oidpp = oidp; 69912623Sphk 700101650Smux if (oidp->oid_kind & CTLFLAG_SKIP) 701101650Smux continue; 702101650Smux 70312623Sphk if (!namelen) { 70444078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 705139483Spjd return (0); 70644078Sdfr if (oidp->oid_handler) 70712623Sphk /* We really should call the handler here...*/ 708139483Spjd return (0); 70944078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 71063978Speter if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 71144078Sdfr len, level+1, oidpp)) 712139483Spjd return (0); 713111260Srwatson goto emptynode; 71412623Sphk } 71512623Sphk 71644078Sdfr if (oidp->oid_number < *name) 71712623Sphk continue; 71812623Sphk 71944078Sdfr if (oidp->oid_number > *name) { 72044078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 721139483Spjd return (0); 72244078Sdfr if (oidp->oid_handler) 723139483Spjd return (0); 72444078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 72563978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 72644078Sdfr next+1, len, level+1, oidpp)) 72712623Sphk return (0); 72815241Sphk goto next; 72912623Sphk } 73044078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 73112623Sphk continue; 73212623Sphk 73344078Sdfr if (oidp->oid_handler) 73412623Sphk continue; 73512623Sphk 73644078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 73763978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 73844078Sdfr len, level+1, oidpp)) 73912623Sphk return (0); 74015241Sphk next: 74112623Sphk namelen = 1; 742111260Srwatson emptynode: 74312623Sphk *len = level; 74412623Sphk } 745139483Spjd return (1); 74612623Sphk} 74712623Sphk 74812623Sphkstatic int 74962573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 75012623Sphk{ 75112623Sphk int *name = (int *) arg1; 75212623Sphk u_int namelen = arg2; 75312623Sphk int i, j, error; 75412623Sphk struct sysctl_oid *oid; 75544078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 75612623Sphk int newoid[CTL_MAXNAME]; 75712623Sphk 75863978Speter i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 75912623Sphk if (i) 760139483Spjd return (ENOENT); 76112650Sphk error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 76212623Sphk return (error); 76312623Sphk} 76412623Sphk 765141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 76612623Sphk 76712623Sphkstatic int 768189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 76912623Sphk{ 77044078Sdfr int i; 77144078Sdfr struct sysctl_oid *oidp; 77244078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 77312623Sphk char *p; 77412623Sphk 775188232Sjhb SYSCTL_ASSERT_LOCKED(); 776186564Sed 77712623Sphk if (!*name) 778139483Spjd return (ENOENT); 77912623Sphk 78012623Sphk p = name + strlen(name) - 1 ; 78112623Sphk if (*p == '.') 78212623Sphk *p = '\0'; 78312623Sphk 78412623Sphk *len = 0; 78512623Sphk 78612623Sphk for (p = name; *p && *p != '.'; p++) 78712623Sphk ; 78812623Sphk i = *p; 78912623Sphk if (i == '.') 79012623Sphk *p = '\0'; 79112623Sphk 79244078Sdfr oidp = SLIST_FIRST(lsp); 79312623Sphk 79444078Sdfr while (oidp && *len < CTL_MAXNAME) { 79544078Sdfr if (strcmp(name, oidp->oid_name)) { 79644078Sdfr oidp = SLIST_NEXT(oidp, oid_link); 79712623Sphk continue; 79812623Sphk } 79944078Sdfr *oid++ = oidp->oid_number; 80012623Sphk (*len)++; 80112623Sphk 80212623Sphk if (!i) { 80344078Sdfr if (oidpp) 80444078Sdfr *oidpp = oidp; 80512623Sphk return (0); 80612623Sphk } 80712623Sphk 80844078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 80912623Sphk break; 81012623Sphk 81144078Sdfr if (oidp->oid_handler) 81212623Sphk break; 81312623Sphk 81444078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 81544078Sdfr oidp = SLIST_FIRST(lsp); 81612623Sphk name = p+1; 81712623Sphk for (p = name; *p && *p != '.'; p++) 81812623Sphk ; 81912623Sphk i = *p; 82012623Sphk if (i == '.') 82112623Sphk *p = '\0'; 82212623Sphk } 823139483Spjd return (ENOENT); 82412623Sphk} 82512623Sphk 82612623Sphkstatic int 82762573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 82812623Sphk{ 82912623Sphk char *p; 83012623Sphk int error, oid[CTL_MAXNAME], len; 83112623Sphk struct sysctl_oid *op = 0; 83212623Sphk 833188232Sjhb SYSCTL_ASSERT_LOCKED(); 834186564Sed 83512623Sphk if (!req->newlen) 836139483Spjd return (ENOENT); 83745140Sphk if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 83845140Sphk return (ENAMETOOLONG); 83912623Sphk 840111119Simp p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 84112623Sphk 84212623Sphk error = SYSCTL_IN(req, p, req->newlen); 84312623Sphk if (error) { 84412623Sphk free(p, M_SYSCTL); 84512623Sphk return (error); 84612623Sphk } 84712623Sphk 84812623Sphk p [req->newlen] = '\0'; 84912623Sphk 85012623Sphk error = name2oid(p, oid, &len, &op); 85112623Sphk 85212623Sphk free(p, M_SYSCTL); 85312623Sphk 85412623Sphk if (error) 85512623Sphk return (error); 85612623Sphk 85712650Sphk error = SYSCTL_OUT(req, oid, len * sizeof *oid); 85812623Sphk return (error); 85912623Sphk} 86012623Sphk 861187864SedSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE, 862187864Sed 0, 0, sysctl_sysctl_name2oid, "I", ""); 86312623Sphk 86412623Sphkstatic int 86562573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 86612623Sphk{ 86744078Sdfr struct sysctl_oid *oid; 86853977Sgreen int error; 86912623Sphk 87053977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 87153977Sgreen if (error) 87253977Sgreen return (error); 87312623Sphk 87444078Sdfr if (!oid->oid_fmt) 87553977Sgreen return (ENOENT); 87653977Sgreen error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 87753977Sgreen if (error) 87853977Sgreen return (error); 87953977Sgreen error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 88012650Sphk return (error); 88112623Sphk} 88212623Sphk 88342467Sphk 884187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE, 885187864Sed sysctl_sysctl_oidfmt, ""); 88612623Sphk 88788006Sluigistatic int 88888006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 88988006Sluigi{ 89088006Sluigi struct sysctl_oid *oid; 89188006Sluigi int error; 89288006Sluigi 89388006Sluigi error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 89488006Sluigi if (error) 89588006Sluigi return (error); 89688006Sluigi 897141433Sphk if (!oid->oid_descr) 89888006Sluigi return (ENOENT); 899141433Sphk error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 90088006Sluigi return (error); 90188006Sluigi} 90288006Sluigi 903141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 90488006Sluigi 90512243Sphk/* 90612623Sphk * Default "handler" functions. 90712623Sphk */ 90812623Sphk 90912623Sphk/* 91042095Sdfr * Handle an int, signed or unsigned. 91112243Sphk * Two cases: 91212243Sphk * a variable: point arg1 at it. 91312243Sphk * a constant: pass it in arg2. 91412243Sphk */ 91512243Sphk 91611865Sphkint 91762573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS) 91811863Sphk{ 919100833Struckman int tmpout, error = 0; 92011863Sphk 921100833Struckman /* 922100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 923100833Struckman */ 92412243Sphk if (arg1) 925100833Struckman tmpout = *(int *)arg1; 92620506Sbde else 927100833Struckman tmpout = arg2; 928100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 92911863Sphk 93012243Sphk if (error || !req->newptr) 93112243Sphk return (error); 93211863Sphk 93312243Sphk if (!arg1) 93412243Sphk error = EPERM; 93512243Sphk else 93612243Sphk error = SYSCTL_IN(req, arg1, sizeof(int)); 93712243Sphk return (error); 93811863Sphk} 93911863Sphk 94012243Sphk/* 941155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks. 942195699Srwatson * Note: this is used by TCP. 943155758Sandre */ 944155758Sandre 945155758Sandreint 946155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 947155758Sandre{ 948155758Sandre int error, s, tt; 949155758Sandre 950191688Szec tt = *(int *)arg1; 951155758Sandre s = (int)((int64_t)tt * 1000 / hz); 952155758Sandre 953155758Sandre error = sysctl_handle_int(oidp, &s, 0, req); 954155758Sandre if (error || !req->newptr) 955155758Sandre return (error); 956155758Sandre 957155758Sandre tt = (int)((int64_t)s * hz / 1000); 958155758Sandre if (tt < 1) 959155758Sandre return (EINVAL); 960155758Sandre 961191688Szec *(int *)arg1 = tt; 962155758Sandre return (0); 963155758Sandre} 964155758Sandre 965155758Sandre 966155758Sandre/* 96745140Sphk * Handle a long, signed or unsigned. arg1 points to it. 96838517Sdfr */ 96938517Sdfr 97038517Sdfrint 97162573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS) 97238517Sdfr{ 97338517Sdfr int error = 0; 974136404Speter long tmplong; 975136404Speter#ifdef SCTL_MASK32 976136404Speter int tmpint; 977136404Speter#endif 97838517Sdfr 979100833Struckman /* 980100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 981100833Struckman */ 98245140Sphk if (!arg1) 98345140Sphk return (EINVAL); 984136404Speter tmplong = *(long *)arg1; 985136404Speter#ifdef SCTL_MASK32 986136404Speter if (req->flags & SCTL_MASK32) { 987136404Speter tmpint = tmplong; 988136404Speter error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 989136404Speter } else 990136404Speter#endif 991136404Speter error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 99238517Sdfr 99338517Sdfr if (error || !req->newptr) 99438517Sdfr return (error); 99538517Sdfr 996136404Speter#ifdef SCTL_MASK32 997136404Speter if (req->flags & SCTL_MASK32) { 998136404Speter error = SYSCTL_IN(req, &tmpint, sizeof(int)); 999136404Speter *(long *)arg1 = (long)tmpint; 1000136404Speter } else 1001136404Speter#endif 1002136404Speter error = SYSCTL_IN(req, arg1, sizeof(long)); 100338517Sdfr return (error); 100438517Sdfr} 100538517Sdfr 100638517Sdfr/* 1007170288Sdwmalone * Handle a 64 bit int, signed or unsigned. arg1 points to it. 1008170288Sdwmalone */ 1009170288Sdwmalone 1010170288Sdwmaloneint 1011170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS) 1012170288Sdwmalone{ 1013170288Sdwmalone int error = 0; 1014170288Sdwmalone uint64_t tmpout; 1015170288Sdwmalone 1016170288Sdwmalone /* 1017170288Sdwmalone * Attempt to get a coherent snapshot by making a copy of the data. 1018170288Sdwmalone */ 1019170288Sdwmalone if (!arg1) 1020170288Sdwmalone return (EINVAL); 1021170288Sdwmalone tmpout = *(uint64_t *)arg1; 1022170288Sdwmalone error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1023170288Sdwmalone 1024170288Sdwmalone if (error || !req->newptr) 1025170288Sdwmalone return (error); 1026170288Sdwmalone 1027170288Sdwmalone error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1028170288Sdwmalone return (error); 1029170288Sdwmalone} 1030170288Sdwmalone 1031170288Sdwmalone/* 103212243Sphk * Handle our generic '\0' terminated 'C' string. 103312243Sphk * Two cases: 103412243Sphk * a variable string: point arg1 at it, arg2 is max length. 103512243Sphk * a constant string: point arg1 at it, arg2 is zero. 103612243Sphk */ 103712243Sphk 103811865Sphkint 103962573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS) 104011863Sphk{ 104112243Sphk int error=0; 1042100833Struckman char *tmparg; 1043100833Struckman size_t outlen; 104411863Sphk 1045100833Struckman /* 1046100833Struckman * Attempt to get a coherent snapshot by copying to a 1047100833Struckman * temporary kernel buffer. 1048100833Struckman */ 1049100833Struckmanretry: 1050100833Struckman outlen = strlen((char *)arg1)+1; 1051111119Simp tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 1052105354Srobert 1053105354Srobert if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 1054100833Struckman free(tmparg, M_SYSCTLTMP); 1055100833Struckman goto retry; 1056100833Struckman } 1057105354Srobert 1058100833Struckman error = SYSCTL_OUT(req, tmparg, outlen); 1059100833Struckman free(tmparg, M_SYSCTLTMP); 106011863Sphk 106145140Sphk if (error || !req->newptr) 106212243Sphk return (error); 106311863Sphk 106445140Sphk if ((req->newlen - req->newidx) >= arg2) { 106545140Sphk error = EINVAL; 106612243Sphk } else { 106712243Sphk arg2 = (req->newlen - req->newidx); 106812243Sphk error = SYSCTL_IN(req, arg1, arg2); 106912243Sphk ((char *)arg1)[arg2] = '\0'; 107011863Sphk } 107112131Sphk 107212131Sphk return (error); 107311863Sphk} 107411863Sphk 107512243Sphk/* 107612243Sphk * Handle any kind of opaque data. 107712243Sphk * arg1 points to it, arg2 is the size. 107812243Sphk */ 107912243Sphk 108011865Sphkint 108162573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 108211863Sphk{ 1083120803Sbms int error, tries; 1084120803Sbms u_int generation; 1085120813Sbms struct sysctl_req req2; 108612243Sphk 1087100833Struckman /* 1088120803Sbms * Attempt to get a coherent snapshot, by using the thread 1089120803Sbms * pre-emption counter updated from within mi_switch() to 1090120803Sbms * determine if we were pre-empted during a bcopy() or 1091120803Sbms * copyout(). Make 3 attempts at doing this before giving up. 1092120803Sbms * If we encounter an error, stop immediately. 1093100833Struckman */ 1094120803Sbms tries = 0; 1095120813Sbms req2 = *req; 1096120813Sbmsretry: 1097120813Sbms generation = curthread->td_generation; 1098120813Sbms error = SYSCTL_OUT(req, arg1, arg2); 1099120813Sbms if (error) 1100120813Sbms return (error); 1101120813Sbms tries++; 1102120813Sbms if (generation != curthread->td_generation && tries < 3) { 1103120813Sbms *req = req2; 1104120813Sbms goto retry; 1105120813Sbms } 110612243Sphk 110712243Sphk error = SYSCTL_IN(req, arg1, arg2); 110812243Sphk 110912243Sphk return (error); 111012243Sphk} 111112243Sphk 111212260Sphk/* 111312260Sphk * Transfer functions to/from kernel space. 111412260Sphk * XXX: rather untested at this point 111512260Sphk */ 111612260Sphkstatic int 111738517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 111812243Sphk{ 111938517Sdfr size_t i = 0; 112012260Sphk 112112260Sphk if (req->oldptr) { 112238517Sdfr i = l; 112373971Stmm if (req->oldlen <= req->oldidx) 112473971Stmm i = 0; 112573971Stmm else 112673971Stmm if (i > req->oldlen - req->oldidx) 112773971Stmm i = req->oldlen - req->oldidx; 112812260Sphk if (i > 0) 112917971Sbde bcopy(p, (char *)req->oldptr + req->oldidx, i); 113012243Sphk } 1131192144Skib req->oldidx += l; 113216282Snate if (req->oldptr && i != l) 113311863Sphk return (ENOMEM); 113412260Sphk return (0); 113512243Sphk} 113612243Sphk 113712260Sphkstatic int 113838517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 113912243Sphk{ 114012260Sphk if (!req->newptr) 1141139483Spjd return (0); 114212260Sphk if (req->newlen - req->newidx < l) 114311863Sphk return (EINVAL); 114417971Sbde bcopy((char *)req->newptr + req->newidx, p, l); 114512243Sphk req->newidx += l; 114612131Sphk return (0); 114711863Sphk} 114811863Sphk 114916282Snateint 115083366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1151136404Speter size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 115216282Snate{ 115316282Snate int error = 0; 115416282Snate struct sysctl_req req; 115516282Snate 115616282Snate bzero(&req, sizeof req); 115716282Snate 115886183Srwatson req.td = td; 1159136404Speter req.flags = flags; 116016282Snate 116116282Snate if (oldlenp) { 116216282Snate req.oldlen = *oldlenp; 116316282Snate } 1164127052Struckman req.validlen = req.oldlen; 116516282Snate 116616282Snate if (old) { 116716282Snate req.oldptr= old; 116816282Snate } 116916282Snate 117077646Sdd if (new != NULL) { 117116282Snate req.newlen = newlen; 117216282Snate req.newptr = new; 117316282Snate } 117416282Snate 117516282Snate req.oldfunc = sysctl_old_kernel; 117616282Snate req.newfunc = sysctl_new_kernel; 1177120781Sbms req.lock = REQ_LOCKED; 117816282Snate 1179188232Sjhb SYSCTL_SLOCK(); 118016282Snate error = sysctl_root(0, name, namelen, &req); 1181188232Sjhb SYSCTL_SUNLOCK(); 1182120813Sbms 1183127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1184127052Struckman vsunlock(req.oldptr, req.validlen); 118516282Snate 118616282Snate if (error && error != ENOMEM) 118716282Snate return (error); 118816282Snate 118916282Snate if (retval) { 1190127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1191127052Struckman *retval = req.validlen; 119216282Snate else 119316282Snate *retval = req.oldidx; 119416282Snate } 119516282Snate return (error); 119616282Snate} 119716282Snate 119876834Sjlemonint 119983366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1200136404Speter void *new, size_t newlen, size_t *retval, int flags) 120176834Sjlemon{ 120276834Sjlemon int oid[CTL_MAXNAME]; 120378620Smjacob size_t oidlen, plen; 120478620Smjacob int error; 120576834Sjlemon 120676834Sjlemon oid[0] = 0; /* sysctl internal magic */ 120776834Sjlemon oid[1] = 3; /* name2oid */ 120876834Sjlemon oidlen = sizeof(oid); 120976834Sjlemon 121083366Sjulian error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1211136404Speter (void *)name, strlen(name), &plen, flags); 121276834Sjlemon if (error) 121376834Sjlemon return (error); 121476834Sjlemon 121583366Sjulian error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1216136404Speter new, newlen, retval, flags); 121776834Sjlemon return (error); 121876834Sjlemon} 121976834Sjlemon 122012260Sphk/* 122112260Sphk * Transfer function to/from user space. 122212260Sphk */ 122312260Sphkstatic int 122438517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 122512243Sphk{ 122638517Sdfr int error = 0; 1227126253Struckman size_t i, len, origidx; 122812243Sphk 1229126253Struckman origidx = req->oldidx; 1230192144Skib req->oldidx += l; 1231192144Skib if (req->oldptr == NULL) 1232126253Struckman return (0); 1233148864Scsjp /* 1234148864Scsjp * If we have not wired the user supplied buffer and we are currently 1235148864Scsjp * holding locks, drop a witness warning, as it's possible that 1236148864Scsjp * write operations to the user page can sleep. 1237148864Scsjp */ 1238148864Scsjp if (req->lock != REQ_WIRED) 1239111883Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1240111883Sjhb "sysctl_old_user()"); 1241126253Struckman i = l; 1242127052Struckman len = req->validlen; 1243126253Struckman if (len <= origidx) 1244126253Struckman i = 0; 1245126253Struckman else { 1246126253Struckman if (i > len - origidx) 1247126253Struckman i = len - origidx; 1248126253Struckman error = copyout(p, (char *)req->oldptr + origidx, i); 124912260Sphk } 125012243Sphk if (error) 125112243Sphk return (error); 1252126253Struckman if (i < l) 125312243Sphk return (ENOMEM); 125412260Sphk return (0); 125512243Sphk} 125612243Sphk 125712260Sphkstatic int 125838517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l) 125912243Sphk{ 126012285Sphk int error; 126112260Sphk 126212260Sphk if (!req->newptr) 1263139483Spjd return (0); 126412260Sphk if (req->newlen - req->newidx < l) 126512243Sphk return (EINVAL); 1266148873Scsjp WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1267148873Scsjp "sysctl_new_user()"); 126817971Sbde error = copyin((char *)req->newptr + req->newidx, p, l); 126912243Sphk req->newidx += l; 127012243Sphk return (error); 127112243Sphk} 127212243Sphk 1273100487Struckman/* 1274100487Struckman * Wire the user space destination buffer. If set to a value greater than 1275100487Struckman * zero, the len parameter limits the maximum amount of wired memory. 1276100487Struckman */ 1277126253Struckmanint 1278100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1279100487Struckman{ 1280126253Struckman int ret; 1281192160Sdes size_t wiredlen; 1282126253Struckman 1283126253Struckman wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1284126253Struckman ret = 0; 1285120781Sbms if (req->lock == REQ_LOCKED && req->oldptr && 1286120781Sbms req->oldfunc == sysctl_old_user) { 1287127050Struckman if (wiredlen != 0) { 1288127050Struckman ret = vslock(req->oldptr, wiredlen); 1289130327Sgreen if (ret != 0) { 1290130327Sgreen if (ret != ENOMEM) 1291130327Sgreen return (ret); 1292130327Sgreen wiredlen = 0; 1293130327Sgreen } 1294126253Struckman } 1295127050Struckman req->lock = REQ_WIRED; 1296127052Struckman req->validlen = wiredlen; 1297100487Struckman } 1298127050Struckman return (0); 1299100487Struckman} 1300100487Struckman 13011541Srgrimesint 130253977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 130353977Sgreen int *nindx, struct sysctl_req *req) 130412131Sphk{ 130544078Sdfr struct sysctl_oid *oid; 130653977Sgreen int indx; 130712131Sphk 1308188232Sjhb SYSCTL_ASSERT_LOCKED(); 130953977Sgreen oid = SLIST_FIRST(&sysctl__children); 131012131Sphk indx = 0; 131144078Sdfr while (oid && indx < CTL_MAXNAME) { 131244078Sdfr if (oid->oid_number == name[indx]) { 131312131Sphk indx++; 131444078Sdfr if (oid->oid_kind & CTLFLAG_NOLOCK) 1315120781Sbms req->lock = REQ_UNLOCKED; 131644078Sdfr if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 131753977Sgreen if (oid->oid_handler != NULL || 131853977Sgreen indx == namelen) { 131953977Sgreen *noid = oid; 132053977Sgreen if (nindx != NULL) 132153977Sgreen *nindx = indx; 132253977Sgreen return (0); 132353977Sgreen } 132453977Sgreen oid = SLIST_FIRST( 132553977Sgreen (struct sysctl_oid_list *)oid->oid_arg1); 132653977Sgreen } else if (indx == namelen) { 132753977Sgreen *noid = oid; 132853977Sgreen if (nindx != NULL) 132953977Sgreen *nindx = indx; 133053977Sgreen return (0); 133112131Sphk } else { 133253977Sgreen return (ENOTDIR); 133312131Sphk } 133412131Sphk } else { 133544078Sdfr oid = SLIST_NEXT(oid, oid_link); 133612131Sphk } 133712131Sphk } 133853977Sgreen return (ENOENT); 133953977Sgreen} 134053977Sgreen 134153977Sgreen/* 134253977Sgreen * Traverse our tree, and find the right node, execute whatever it points 134353977Sgreen * to, and return the resulting error code. 134453977Sgreen */ 134553977Sgreen 1346104094Sphkstatic int 134762573Sphksysctl_root(SYSCTL_HANDLER_ARGS) 134853977Sgreen{ 134953977Sgreen struct sysctl_oid *oid; 1350109246Sdillon int error, indx, lvl; 135153977Sgreen 1352188232Sjhb SYSCTL_ASSERT_LOCKED(); 1353186564Sed 135453977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 135553977Sgreen if (error) 135653977Sgreen return (error); 135753977Sgreen 135853977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 135953977Sgreen /* 136053977Sgreen * You can't call a sysctl when it's a node, but has 136153977Sgreen * no handler. Inform the user that it's a node. 136253977Sgreen * The indx may or may not be the same as namelen. 136353977Sgreen */ 136453977Sgreen if (oid->oid_handler == NULL) 136553977Sgreen return (EISDIR); 136653977Sgreen } 136753977Sgreen 136883968Srwatson /* Is this sysctl writable? */ 136983968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 137012131Sphk return (EPERM); 137112131Sphk 137292953Srwatson KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 137392953Srwatson 137483968Srwatson /* Is this sysctl sensitive to securelevels? */ 137583968Srwatson if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1376109246Sdillon lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1377109246Sdillon error = securelevel_gt(req->td->td_ucred, lvl); 137892953Srwatson if (error) 137992953Srwatson return (error); 138083968Srwatson } 138112910Sphk 138283968Srwatson /* Is this sysctl writable by only privileged users? */ 138383968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1384196176Sbz int priv; 1385196176Sbz 138692953Srwatson if (oid->oid_kind & CTLFLAG_PRISON) 1387196176Sbz priv = PRIV_SYSCTL_WRITEJAIL; 1388196176Sbz#ifdef VIMAGE 1389196176Sbz else if ((oid->oid_kind & CTLFLAG_VNET) && 1390196176Sbz prison_owns_vnet(req->td->td_ucred)) 1391196176Sbz priv = PRIV_SYSCTL_WRITEJAIL; 1392196176Sbz#endif 139392953Srwatson else 1394196176Sbz priv = PRIV_SYSCTL_WRITE; 1395196176Sbz error = priv_check(req->td, priv); 139692953Srwatson if (error) 139792953Srwatson return (error); 139883968Srwatson } 139983968Srwatson 140044078Sdfr if (!oid->oid_handler) 1401139483Spjd return (EINVAL); 140212131Sphk 1403126121Spjd if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1404132776Skan arg1 = (int *)arg1 + indx; 1405126121Spjd arg2 -= indx; 1406126121Spjd } else { 1407126121Spjd arg1 = oid->oid_arg1; 1408126121Spjd arg2 = oid->oid_arg2; 1409126121Spjd } 1410126121Spjd#ifdef MAC 1411172930Srwatson error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1412126121Spjd req); 1413126121Spjd if (error != 0) 1414126121Spjd return (error); 1415126121Spjd#endif 1416187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1417187656Sjhb mtx_lock(&Giant); 1418126121Spjd error = oid->oid_handler(oid, arg1, arg2, req); 1419187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1420187656Sjhb mtx_unlock(&Giant); 1421126121Spjd 142253977Sgreen return (error); 142312131Sphk} 142412131Sphk 142512221Sbde#ifndef _SYS_SYSPROTO_H_ 142612171Sphkstruct sysctl_args { 142712171Sphk int *name; 142812171Sphk u_int namelen; 142912171Sphk void *old; 143012171Sphk size_t *oldlenp; 143112171Sphk void *new; 143212171Sphk size_t newlen; 143312171Sphk}; 143412221Sbde#endif 143512131Sphkint 143683366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap) 14371541Srgrimes{ 1438188232Sjhb int error, i, name[CTL_MAXNAME]; 143938517Sdfr size_t j; 14401541Srgrimes 14411541Srgrimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 14421541Srgrimes return (EINVAL); 144311863Sphk 14443308Sphk error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 14453308Sphk if (error) 14461541Srgrimes return (error); 14471541Srgrimes 144883366Sjulian error = userland_sysctl(td, name, uap->namelen, 144912171Sphk uap->old, uap->oldlenp, 0, 1450136404Speter uap->new, uap->newlen, &j, 0); 145112260Sphk if (error && error != ENOMEM) 1452186564Sed return (error); 1453186664Sed if (uap->oldlenp) { 1454188232Sjhb i = copyout(&j, uap->oldlenp, sizeof(j)); 1455186664Sed if (i) 1456186664Sed return (i); 1457186664Sed } 145812260Sphk return (error); 145912171Sphk} 146012171Sphk 146112171Sphk/* 146212171Sphk * This is used from various compatibility syscalls too. That's why name 146312171Sphk * must be in kernel space. 146412171Sphk */ 146512171Sphkint 146683366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1467136404Speter size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1468136404Speter int flags) 146912171Sphk{ 1470192125Sjhb int error = 0, memlocked; 1471127052Struckman struct sysctl_req req; 147212171Sphk 147312243Sphk bzero(&req, sizeof req); 147412243Sphk 147586183Srwatson req.td = td; 1476136404Speter req.flags = flags; 147712285Sphk 147812171Sphk if (oldlenp) { 147912171Sphk if (inkernel) { 148012243Sphk req.oldlen = *oldlenp; 148112171Sphk } else { 148212260Sphk error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 148312171Sphk if (error) 148412171Sphk return (error); 148512171Sphk } 148612171Sphk } 1487127052Struckman req.validlen = req.oldlen; 148812171Sphk 148912243Sphk if (old) { 149052644Sphk if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 149112243Sphk return (EFAULT); 149212243Sphk req.oldptr= old; 149312243Sphk } 149412131Sphk 149577646Sdd if (new != NULL) { 1496172038Srwatson if (!useracc(new, newlen, VM_PROT_READ)) 149712243Sphk return (EFAULT); 149812243Sphk req.newlen = newlen; 149912243Sphk req.newptr = new; 150011863Sphk } 150112131Sphk 150212243Sphk req.oldfunc = sysctl_old_user; 150312243Sphk req.newfunc = sysctl_new_user; 1504120781Sbms req.lock = REQ_LOCKED; 150511863Sphk 1506189707Sjhb#ifdef KTRACE 1507189707Sjhb if (KTRPOINT(curthread, KTR_SYSCTL)) 1508189707Sjhb ktrsysctl(name, namelen); 1509189707Sjhb#endif 1510192125Sjhb 1511192125Sjhb if (req.oldlen > PAGE_SIZE) { 1512192125Sjhb memlocked = 1; 1513192125Sjhb sx_xlock(&sysctlmemlock); 1514192125Sjhb } else 1515192125Sjhb memlocked = 0; 1516194252Sjamie CURVNET_SET(TD_TO_VNET(td)); 151712429Sphk 1518185983Skib for (;;) { 1519127052Struckman req.oldidx = 0; 1520127052Struckman req.newidx = 0; 1521192125Sjhb SYSCTL_SLOCK(); 1522127052Struckman error = sysctl_root(0, name, namelen, &req); 1523192125Sjhb SYSCTL_SUNLOCK(); 1524185983Skib if (error != EAGAIN) 1525185983Skib break; 1526185983Skib uio_yield(); 1527185983Skib } 152812243Sphk 1529186564Sed CURVNET_RESTORE(); 1530186564Sed 1531127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1532127052Struckman vsunlock(req.oldptr, req.validlen); 1533192125Sjhb if (memlocked) 1534192125Sjhb sx_xunlock(&sysctlmemlock); 153512429Sphk 153612260Sphk if (error && error != ENOMEM) 153712260Sphk return (error); 153812260Sphk 153912260Sphk if (retval) { 1540127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1541127052Struckman *retval = req.validlen; 154212260Sphk else 154312260Sphk *retval = req.oldidx; 154411863Sphk } 154512260Sphk return (error); 15461541Srgrimes} 1547