Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.244
      1 /*	$NetBSD: kern_sysctl.c,v 1.244 2014/01/17 02:12:48 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Brown.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This code is derived from software contributed to Berkeley by
     37  * Mike Karels at Berkeley Software Design, Inc.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)kern_sysctl.c	8.9 (Berkeley) 5/20/95
     64  */
     65 
     66 /*
     67  * sysctl system call.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.244 2014/01/17 02:12:48 pooka Exp $");
     72 
     73 #include "opt_defcorename.h"
     74 #include "ksyms.h"
     75 
     76 #include <sys/param.h>
     77 #define __COMPAT_SYSCTL
     78 #include <sys/sysctl.h>
     79 #include <sys/systm.h>
     80 #include <sys/buf.h>
     81 #include <sys/ksyms.h>
     82 #include <sys/malloc.h>
     83 #include <sys/mount.h>
     84 #include <sys/syscallargs.h>
     85 #include <sys/kauth.h>
     86 #include <sys/ktrace.h>
     87 
     88 #define	MAXDESCLEN	1024
     89 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
     90 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data");
     91 
     92 static int sysctl_mmap(SYSCTLFN_PROTO);
     93 static int sysctl_alloc(struct sysctlnode *, int);
     94 static int sysctl_realloc(struct sysctlnode *);
     95 
     96 static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t,
     97 			 struct sysctlnode *);
     98 static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *,
     99 			  void *, size_t, size_t *);
    100 
    101 static int sysctl_log_add(struct sysctllog **, const struct sysctlnode *);
    102 static int sysctl_log_realloc(struct sysctllog *);
    103 
    104 typedef void sysctl_setup_func(struct sysctllog **);
    105 
    106 #ifdef SYSCTL_DEBUG
    107 #define DPRINTF(a)	printf a
    108 #else
    109 #define DPRINTF(a)
    110 #endif
    111 
    112 struct sysctllog {
    113 	const struct sysctlnode *log_root;
    114 	int *log_num;
    115 	int log_size, log_left;
    116 };
    117 
    118 /*
    119  * the "root" of the new sysctl tree
    120  */
    121 struct sysctlnode sysctl_root = {
    122 	.sysctl_flags = SYSCTL_VERSION|
    123 	    CTLFLAG_ROOT|CTLFLAG_READWRITE|
    124 	    CTLTYPE_NODE,
    125 	.sysctl_num = 0,
    126 	/*
    127 	 * XXX once all ports are on gcc3, we can get rid of this
    128 	 * ugliness and simply make it into
    129 	 *
    130 	 *	.sysctl_size = sizeof(struct sysctlnode),
    131 	 */
    132 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
    133 	.sysctl_name = "(root)",
    134 };
    135 
    136 /*
    137  * link set of functions that add nodes at boot time (see also
    138  * sysctl_buildtree())
    139  */
    140 __link_set_decl(sysctl_funcs, sysctl_setup_func);
    141 
    142 /*
    143  * The `sysctl_treelock' is intended to serialize access to the sysctl
    144  * tree.  XXX This has serious problems; allocating memory and
    145  * copying data out with the lock held is insane.
    146  */
    147 krwlock_t sysctl_treelock;
    148 
    149 kmutex_t sysctl_file_marker_lock;
    150 
    151 /*
    152  * Attributes stored in the kernel.
    153  */
    154 char hostname[MAXHOSTNAMELEN];
    155 int hostnamelen;
    156 
    157 char domainname[MAXHOSTNAMELEN];
    158 int domainnamelen;
    159 
    160 long hostid;
    161 
    162 #ifndef DEFCORENAME
    163 #define	DEFCORENAME	"%n.core"
    164 #endif
    165 char defcorename[MAXPATHLEN] = DEFCORENAME;
    166 
    167 /*
    168  * ********************************************************************
    169  * Section 0: Some simple glue
    170  * ********************************************************************
    171  * By wrapping copyin(), copyout(), and copyinstr() like this, we can
    172  * stop caring about who's calling us and simplify some code a bunch.
    173  * ********************************************************************
    174  */
    175 int
    176 sysctl_copyin(struct lwp *l, const void *uaddr, void *kaddr, size_t len)
    177 {
    178 	int error;
    179 
    180 	if (l != NULL) {
    181 		error = copyin(uaddr, kaddr, len);
    182 		ktrmibio(-1, UIO_WRITE, uaddr, len, error);
    183 	} else {
    184 		error = kcopy(uaddr, kaddr, len);
    185 	}
    186 
    187 	return error;
    188 }
    189 
    190 int
    191 sysctl_copyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
    192 {
    193 	int error;
    194 
    195 	if (l != NULL) {
    196 		error = copyout(kaddr, uaddr, len);
    197 		ktrmibio(-1, UIO_READ, uaddr, len, error);
    198 	} else {
    199 		error = kcopy(kaddr, uaddr, len);
    200 	}
    201 
    202 	return error;
    203 }
    204 
    205 int
    206 sysctl_copyinstr(struct lwp *l, const void *uaddr, void *kaddr,
    207 		 size_t len, size_t *done)
    208 {
    209 	int error;
    210 
    211 	if (l != NULL) {
    212 		error = copyinstr(uaddr, kaddr, len, done);
    213 		ktrmibio(-1, UIO_WRITE, uaddr, len, error);
    214 	} else {
    215 		error = copystr(uaddr, kaddr, len, done);
    216 	}
    217 
    218 	return error;
    219 }
    220 
    221 /*
    222  * ********************************************************************
    223  * Initialize sysctl subsystem.
    224  * ********************************************************************
    225  */
    226 void
    227 sysctl_init(void)
    228 {
    229 	sysctl_setup_func *const *sysctl_setup;
    230 
    231 	rw_init(&sysctl_treelock);
    232 
    233 	/*
    234 	 * dynamic mib numbers start here
    235 	 */
    236 	sysctl_root.sysctl_num = CREATE_BASE;
    237 
    238         __link_set_foreach(sysctl_setup, sysctl_funcs) {
    239 		(**sysctl_setup)(NULL);
    240 	}
    241 
    242 	mutex_init(&sysctl_file_marker_lock, MUTEX_DEFAULT, IPL_NONE);
    243 }
    244 
    245 /*
    246  * Setting this means no more permanent nodes can be added,
    247  * trees that claim to be readonly at the root now are, and if
    248  * the main tree is readonly, *everything* is.
    249  *
    250  * Also starts up the PRNG used for the "random" sysctl: it's
    251  * better to start it later than sooner.
    252  *
    253  * Call this at the end of kernel init.
    254  */
    255 void
    256 sysctl_finalize(void)
    257 {
    258 
    259 	sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT;
    260 }
    261 
    262 /*
    263  * ********************************************************************
    264  * The main native sysctl system call itself.
    265  * ********************************************************************
    266  */
    267 int
    268 sys___sysctl(struct lwp *l, const struct sys___sysctl_args *uap, register_t *retval)
    269 {
    270 	/* {
    271 		syscallarg(const int *) name;
    272 		syscallarg(u_int) namelen;
    273 		syscallarg(void *) old;
    274 		syscallarg(size_t *) oldlenp;
    275 		syscallarg(const void *) new;
    276 		syscallarg(size_t) newlen;
    277 	} */
    278 	int error, nerror, name[CTL_MAXNAME];
    279 	size_t oldlen, savelen, *oldlenp;
    280 
    281 	/*
    282 	 * get oldlen
    283 	 */
    284 	oldlen = 0;
    285 	oldlenp = SCARG(uap, oldlenp);
    286 	if (oldlenp != NULL) {
    287 		error = copyin(oldlenp, &oldlen, sizeof(oldlen));
    288 		if (error)
    289 			return (error);
    290 	}
    291 	savelen = oldlen;
    292 
    293 	/*
    294 	 * top-level sysctl names may or may not be non-terminal, but
    295 	 * we don't care
    296 	 */
    297 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
    298 		return (EINVAL);
    299 	error = copyin(SCARG(uap, name), &name,
    300 		       SCARG(uap, namelen) * sizeof(int));
    301 	if (error)
    302 		return (error);
    303 
    304 	ktrmib(name, SCARG(uap, namelen));
    305 
    306 	sysctl_lock(SCARG(uap, new) != NULL);
    307 
    308 	/*
    309 	 * do sysctl work (NULL means main built-in default tree)
    310 	 */
    311 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
    312 				SCARG(uap, old), &oldlen,
    313 				SCARG(uap, new), SCARG(uap, newlen),
    314 				&name[0], l, NULL);
    315 
    316 	/*
    317 	 * release the sysctl lock
    318 	 */
    319 	sysctl_unlock();
    320 
    321 	/*
    322 	 * set caller's oldlen to new value even in the face of an
    323 	 * error (if this gets an error and they didn't have one, they
    324 	 * get this one)
    325 	 */
    326 	if (oldlenp) {
    327 		nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
    328 		if (error == 0)
    329 			error = nerror;
    330 	}
    331 
    332 	/*
    333 	 * if the only problem is that we weren't given enough space,
    334 	 * that's an ENOMEM error
    335 	 */
    336 	if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
    337 		error = ENOMEM;
    338 
    339 	return (error);
    340 }
    341 
    342 /*
    343  * ********************************************************************
    344  * Section 1: How the tree is used
    345  * ********************************************************************
    346  * Implementations of sysctl for emulations should typically need only
    347  * these three functions in this order: lock the tree, dispatch
    348  * request into it, unlock the tree.
    349  * ********************************************************************
    350  */
    351 void
    352 sysctl_lock(bool write)
    353 {
    354 
    355 	if (write) {
    356 		rw_enter(&sysctl_treelock, RW_WRITER);
    357 		curlwp->l_pflag |= LP_SYSCTLWRITE;
    358 	} else {
    359 		rw_enter(&sysctl_treelock, RW_READER);
    360 		curlwp->l_pflag &= ~LP_SYSCTLWRITE;
    361 	}
    362 }
    363 
    364 void
    365 sysctl_relock(void)
    366 {
    367 
    368 	if ((curlwp->l_pflag & LP_SYSCTLWRITE) != 0) {
    369 		rw_enter(&sysctl_treelock, RW_WRITER);
    370 	} else {
    371 		rw_enter(&sysctl_treelock, RW_READER);
    372 	}
    373 }
    374 
    375 /*
    376  * ********************************************************************
    377  * the main sysctl dispatch routine.  scans the given tree and picks a
    378  * function to call based on what it finds.
    379  * ********************************************************************
    380  */
    381 int
    382 sysctl_dispatch(SYSCTLFN_ARGS)
    383 {
    384 	int error;
    385 	sysctlfn fn;
    386 	int ni;
    387 
    388 	KASSERT(rw_lock_held(&sysctl_treelock));
    389 
    390 	if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    391 		printf("sysctl_dispatch: rnode %p wrong version\n", rnode);
    392 		error = EINVAL;
    393 		goto out;
    394 	}
    395 
    396 	fn = NULL;
    397 	error = sysctl_locate(l, name, namelen, &rnode, &ni);
    398 
    399 	if (rnode->sysctl_func != NULL) {
    400 		/*
    401 		 * the node we ended up at has a function, so call it.  it can
    402 		 * hand off to query or create if it wants to.
    403 		 */
    404 		fn = rnode->sysctl_func;
    405 	} else if (error == 0) {
    406 		/*
    407 		 * we found the node they were looking for, so do a lookup.
    408 		 */
    409 		fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
    410 	} else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
    411 		/*
    412 		 * prospective parent node found, but the terminal node was
    413 		 * not.  generic operations associate with the parent.
    414 		 */
    415 		switch (name[ni]) {
    416 		case CTL_QUERY:
    417 			fn = sysctl_query;
    418 			break;
    419 		case CTL_CREATE:
    420 #if NKSYMS > 0
    421 		case CTL_CREATESYM:
    422 #endif /* NKSYMS > 0 */
    423 			if (newp == NULL) {
    424 				error = EINVAL;
    425 				break;
    426 			}
    427 			KASSERT(rw_write_held(&sysctl_treelock));
    428 			fn = (sysctlfn)sysctl_create; /* we own the rnode */
    429 			break;
    430 		case CTL_DESTROY:
    431 			if (newp == NULL) {
    432 				error = EINVAL;
    433 				break;
    434 			}
    435 			KASSERT(rw_write_held(&sysctl_treelock));
    436 			fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
    437 			break;
    438 		case CTL_MMAP:
    439 			fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
    440 			break;
    441 		case CTL_DESCRIBE:
    442 			fn = sysctl_describe;
    443 			break;
    444 		default:
    445 			error = EOPNOTSUPP;
    446 			break;
    447 		}
    448 	}
    449 
    450 	/*
    451 	 * after all of that, maybe we found someone who knows how to
    452 	 * get us what we want?
    453 	 */
    454 	if (fn != NULL)
    455 		error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
    456 			      newp, newlen, name, l, rnode);
    457 	else if (error == 0)
    458 		error = EOPNOTSUPP;
    459 
    460 out:
    461 	return (error);
    462 }
    463 
    464 /*
    465  * ********************************************************************
    466  * Releases the tree lock.
    467  * ********************************************************************
    468  */
    469 void
    470 sysctl_unlock(void)
    471 {
    472 
    473 	rw_exit(&sysctl_treelock);
    474 }
    475 
    476 /*
    477  * ********************************************************************
    478  * Section 2: The main tree interfaces
    479  * ********************************************************************
    480  * This is how sysctl_dispatch() does its work, and you can too, by
    481  * calling these routines from helpers (though typically only
    482  * sysctl_lookup() will be used).  The tree MUST BE LOCKED when these
    483  * are called.
    484  * ********************************************************************
    485  */
    486 
    487 /*
    488  * sysctl_locate -- Finds the node matching the given mib under the
    489  * given tree (via rv).  If no tree is given, we fall back to the
    490  * native tree.  The current process (via l) is used for access
    491  * control on the tree (some nodes may be traversable only by root) and
    492  * on return, nip will show how many numbers in the mib were consumed.
    493  */
    494 int
    495 sysctl_locate(struct lwp *l, const int *name, u_int namelen,
    496 	      const struct sysctlnode **rnode, int *nip)
    497 {
    498 	const struct sysctlnode *node, *pnode;
    499 	int tn, si, ni, error, alias;
    500 
    501 	KASSERT(rw_lock_held(&sysctl_treelock));
    502 
    503 	/*
    504 	 * basic checks and setup
    505 	 */
    506 	if (*rnode == NULL)
    507 		*rnode = &sysctl_root;
    508 	if (nip)
    509 		*nip = 0;
    510 	if (namelen == 0)
    511 		return (0);
    512 
    513 	/*
    514 	 * search starts from "root"
    515 	 */
    516 	pnode = *rnode;
    517 	if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) {
    518 		printf("sysctl_locate: pnode %p wrong version\n", pnode);
    519 		return (EINVAL);
    520 	}
    521 	node = pnode->sysctl_child;
    522 	error = 0;
    523 
    524 	/*
    525 	 * scan for node to which new node should be attached
    526 	 */
    527 	for (ni = 0; ni < namelen; ni++) {
    528 		/*
    529 		 * walked off bottom of tree
    530 		 */
    531 		if (node == NULL) {
    532 			if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
    533 				error = ENOENT;
    534 			else
    535 				error = ENOTDIR;
    536 			break;
    537 		}
    538 		/*
    539 		 * can anyone traverse this node or only root?
    540 		 */
    541 		if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) &&
    542 		    (error = kauth_authorize_system(l->l_cred,
    543 		    KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_PRVT,
    544 		    NULL, NULL, NULL)) != 0)
    545 			return (error);
    546 		/*
    547 		 * find a child node with the right number
    548 		 */
    549 		tn = name[ni];
    550 		alias = 0;
    551 
    552 		si = 0;
    553 		/*
    554 		 * Note: ANYNUMBER only matches positive integers.
    555 		 * Since ANYNUMBER is only permitted on single-node
    556 		 * sub-trees (eg proc), check before the loop and skip
    557 		 * it if we can.
    558 		 */
    559 		if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0))
    560 			goto foundit;
    561 		for (; si < pnode->sysctl_clen; si++) {
    562 			if (node[si].sysctl_num == tn) {
    563 				if (node[si].sysctl_flags & CTLFLAG_ALIAS) {
    564 					if (alias++ == 4)
    565 						break;
    566 					else {
    567 						tn = node[si].sysctl_alias;
    568 						si = -1;
    569 					}
    570 				} else
    571 					goto foundit;
    572 			}
    573 		}
    574 		/*
    575 		 * if we ran off the end, it obviously doesn't exist
    576 		 */
    577 		error = ENOENT;
    578 		break;
    579 
    580 		/*
    581 		 * so far so good, move on down the line
    582 		 */
    583 	  foundit:
    584 		pnode = &node[si];
    585 		if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
    586 			node = node[si].sysctl_child;
    587 		else
    588 			node = NULL;
    589 	}
    590 
    591 	*rnode = pnode;
    592 	if (nip)
    593 		*nip = ni;
    594 
    595 	return (error);
    596 }
    597 
    598 /*
    599  * sysctl_query -- The auto-discovery engine.  Copies out the structs
    600  * describing nodes under the given node and handles overlay trees.
    601  */
    602 int
    603 sysctl_query(SYSCTLFN_ARGS)
    604 {
    605 	int error, ni, elim, v;
    606 	size_t out, left, t;
    607 	const struct sysctlnode *enode, *onode;
    608 	struct sysctlnode qnode;
    609 
    610 	KASSERT(rw_lock_held(&sysctl_treelock));
    611 
    612 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    613 		printf("sysctl_query: rnode %p wrong version\n", rnode);
    614 		return (EINVAL);
    615 	}
    616 
    617 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
    618 		return (ENOTDIR);
    619 	if (namelen != 1 || name[0] != CTL_QUERY)
    620 		return (EINVAL);
    621 
    622 	error = 0;
    623 	out = 0;
    624 	left = *oldlenp;
    625 	elim = 0;
    626 	enode = NULL;
    627 
    628 	/*
    629 	 * translate the given request to a current node
    630 	 */
    631 	error = sysctl_cvt_in(l, &v, newp, newlen, &qnode);
    632 	if (error)
    633 		return (error);
    634 
    635 	/*
    636 	 * if the request specifies a version, check it
    637 	 */
    638 	if (qnode.sysctl_ver != 0) {
    639 		enode = rnode;
    640 		if (qnode.sysctl_ver != enode->sysctl_ver &&
    641 		    qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver)
    642 			return (EINVAL);
    643 	}
    644 
    645 	/*
    646 	 * process has overlay tree
    647 	 */
    648 	if (l && l->l_proc->p_emul->e_sysctlovly) {
    649 		enode = l->l_proc->p_emul->e_sysctlovly;
    650 		elim = (name - oname);
    651 		error = sysctl_locate(l, oname, elim, &enode, NULL);
    652 		if (error == 0) {
    653 			/* ah, found parent in overlay */
    654 			elim = enode->sysctl_clen;
    655 			enode = enode->sysctl_child;
    656 		} else {
    657 			error = 0;
    658 			elim = 0;
    659 			enode = NULL;
    660 		}
    661 	}
    662 
    663 	for (ni = 0; ni < rnode->sysctl_clen; ni++) {
    664 		onode = &rnode->sysctl_child[ni];
    665 		if (enode && enode->sysctl_num == onode->sysctl_num) {
    666 			if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE)
    667 				onode = enode;
    668 			if (--elim > 0)
    669 				enode++;
    670 			else
    671 				enode = NULL;
    672 		}
    673 		error = sysctl_cvt_out(l, v, onode, oldp, left, &t);
    674 		if (error)
    675 			return (error);
    676 		if (oldp != NULL)
    677 			oldp = (char*)oldp + t;
    678 		out += t;
    679 		left -= MIN(left, t);
    680 	}
    681 
    682 	/*
    683 	 * overlay trees *MUST* be entirely consumed
    684 	 */
    685 	KASSERT(enode == NULL);
    686 
    687 	*oldlenp = out;
    688 
    689 	return (error);
    690 }
    691 
    692 /*
    693  * sysctl_create -- Adds a node (the description of which is taken
    694  * from newp) to the tree, returning a copy of it in the space pointed
    695  * to by oldp.  In the event that the requested slot is already taken
    696  * (either by name or by number), the offending node is returned
    697  * instead.  Yes, this is complex, but we want to make sure everything
    698  * is proper.
    699  */
    700 #ifdef SYSCTL_DEBUG_CREATE
    701 int _sysctl_create(SYSCTLFN_ARGS);
    702 int
    703 _sysctl_create(SYSCTLFN_ARGS)
    704 #else
    705 int
    706 sysctl_create(SYSCTLFN_ARGS)
    707 #endif
    708 {
    709 	struct sysctlnode nnode, *node, *pnode;
    710 	int error, ni, at, nm, type, nsz, sz, flags, anum, v;
    711 	void *own;
    712 
    713 	KASSERT(rw_write_held(&sysctl_treelock));
    714 
    715 	error = 0;
    716 	own = NULL;
    717 	anum = -1;
    718 
    719 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    720 		printf("sysctl_create: rnode %p wrong version\n", rnode);
    721 		return (EINVAL);
    722 	}
    723 
    724 	if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
    725 #if NKSYMS > 0
    726 			     && name[namelen - 1] != CTL_CREATESYM
    727 #endif /* NKSYMS > 0 */
    728 			     ))
    729 		return (EINVAL);
    730 
    731 	/*
    732 	 * processes can only add nodes at securelevel 0, must be
    733 	 * root, and can't add nodes to a parent that's not writeable
    734 	 */
    735 	if (l != NULL) {
    736 #ifndef SYSCTL_DISALLOW_CREATE
    737 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
    738 		    KAUTH_REQ_SYSTEM_SYSCTL_ADD, NULL, NULL, NULL);
    739 		if (error)
    740 			return (error);
    741 		if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
    742 #endif /* SYSCTL_DISALLOW_CREATE */
    743 			return (EPERM);
    744 	}
    745 
    746 	/*
    747 	 * nothing can add a node if:
    748 	 * we've finished initial set up of this tree and
    749 	 * (the tree itself is not writeable or
    750 	 * the entire sysctl system is not writeable)
    751 	 */
    752 	if ((sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_PERMANENT) &&
    753 	    (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
    754 	     !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)))
    755 		return (EPERM);
    756 
    757 	/*
    758 	 * it must be a "node", not a "int" or something
    759 	 */
    760 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
    761 		return (ENOTDIR);
    762 	if (rnode->sysctl_flags & CTLFLAG_ALIAS) {
    763 		printf("sysctl_create: attempt to add node to aliased "
    764 		       "node %p\n", rnode);
    765 		return (EINVAL);
    766 	}
    767 	pnode = __UNCONST(rnode); /* we are adding children to this node */
    768 
    769 	if (newp == NULL)
    770 		return (EINVAL);
    771 	error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
    772 	if (error)
    773 		return (error);
    774 
    775 	/*
    776 	 * nodes passed in don't *have* parents
    777 	 */
    778 	if (nnode.sysctl_parent != NULL)
    779 		return (EINVAL);
    780 
    781 	/*
    782 	 * if we are indeed adding it, it should be a "good" name and
    783 	 * number
    784 	 */
    785 	nm = nnode.sysctl_num;
    786 #if NKSYMS > 0
    787 	if (nm == CTL_CREATESYM)
    788 		nm = CTL_CREATE;
    789 #endif /* NKSYMS > 0 */
    790 	if (nm < 0 && nm != CTL_CREATE)
    791 		return (EINVAL);
    792 
    793 	/*
    794 	 * the name can't start with a digit
    795 	 */
    796 	if (nnode.sysctl_name[0] >= '0' &&
    797 	    nnode.sysctl_name[0] <= '9')
    798 		return (EINVAL);
    799 
    800 	/*
    801 	 * the name must be only alphanumerics or - or _, longer than
    802 	 * 0 bytes and less that SYSCTL_NAMELEN
    803 	 */
    804 	nsz = 0;
    805 	while (nsz < SYSCTL_NAMELEN && nnode.sysctl_name[nsz] != '\0') {
    806 		if ((nnode.sysctl_name[nsz] >= '0' &&
    807 		     nnode.sysctl_name[nsz] <= '9') ||
    808 		    (nnode.sysctl_name[nsz] >= 'A' &&
    809 		     nnode.sysctl_name[nsz] <= 'Z') ||
    810 		    (nnode.sysctl_name[nsz] >= 'a' &&
    811 		     nnode.sysctl_name[nsz] <= 'z') ||
    812 		    nnode.sysctl_name[nsz] == '-' ||
    813 		    nnode.sysctl_name[nsz] == '_')
    814 			nsz++;
    815 		else
    816 			return (EINVAL);
    817 	}
    818 	if (nsz == 0 || nsz == SYSCTL_NAMELEN)
    819 		return (EINVAL);
    820 
    821 	/*
    822 	 * various checks revolve around size vs type, etc
    823 	 */
    824 	type = SYSCTL_TYPE(nnode.sysctl_flags);
    825 	flags = SYSCTL_FLAGS(nnode.sysctl_flags);
    826 	sz = nnode.sysctl_size;
    827 
    828 	/*
    829 	 * find out if there's a collision, and if so, let the caller
    830 	 * know what they collided with
    831 	 */
    832 	node = pnode->sysctl_child;
    833 	at = 0;
    834 	if (node) {
    835 		if ((flags | node->sysctl_flags) & CTLFLAG_ANYNUMBER)
    836 			/* No siblings for a CTLFLAG_ANYNUMBER node */
    837 			return EINVAL;
    838 		for (ni = 0; ni < pnode->sysctl_clen; ni++) {
    839 			if (nm == node[ni].sysctl_num ||
    840 			    strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
    841 				/*
    842 				 * ignore error here, since we
    843 				 * are already fixed on EEXIST
    844 				 */
    845 				(void)sysctl_cvt_out(l, v, &node[ni], oldp,
    846 						     *oldlenp, oldlenp);
    847 				return (EEXIST);
    848 			}
    849 			if (nm > node[ni].sysctl_num)
    850 				at++;
    851 		}
    852 	}
    853 
    854 	/*
    855 	 * use sysctl_ver to add to the tree iff it hasn't changed
    856 	 */
    857 	if (nnode.sysctl_ver != 0) {
    858 		/*
    859 		 * a specified value must match either the parent
    860 		 * node's version or the root node's version
    861 		 */
    862 		if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
    863 		    nnode.sysctl_ver != rnode->sysctl_ver) {
    864 			return (EINVAL);
    865 		}
    866 	}
    867 
    868 	/*
    869 	 * only the kernel can assign functions to entries
    870 	 */
    871 	if (l != NULL && nnode.sysctl_func != NULL)
    872 		return (EPERM);
    873 
    874 	/*
    875 	 * only the kernel can create permanent entries, and only then
    876 	 * before the kernel is finished setting itself up
    877 	 */
    878 	if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
    879 		return (EPERM);
    880 	if ((flags & CTLFLAG_PERMANENT) &
    881 	    (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT))
    882 		return (EPERM);
    883 	if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) ==
    884 	    (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE))
    885 		return (EINVAL);
    886 	if ((flags & CTLFLAG_IMMEDIATE) &&
    887 	    type != CTLTYPE_INT && type != CTLTYPE_QUAD && type != CTLTYPE_BOOL)
    888 		return (EINVAL);
    889 
    890 	/*
    891 	 * check size, or set it if unset and we can figure it out.
    892 	 * kernel created nodes are allowed to have a function instead
    893 	 * of a size (or a data pointer).
    894 	 */
    895 	switch (type) {
    896 	case CTLTYPE_NODE:
    897 		/*
    898 		 * only *i* can assert the size of a node
    899 		 */
    900 		if (flags & CTLFLAG_ALIAS) {
    901 			anum = nnode.sysctl_alias;
    902 			if (anum < 0)
    903 				return (EINVAL);
    904 			nnode.sysctl_alias = 0;
    905 		}
    906 		if (sz != 0 || nnode.sysctl_data != NULL)
    907 			return (EINVAL);
    908 		if (nnode.sysctl_csize != 0 ||
    909 		    nnode.sysctl_clen != 0 ||
    910 		    nnode.sysctl_child != 0)
    911 			return (EINVAL);
    912 		if (flags & CTLFLAG_OWNDATA)
    913 			return (EINVAL);
    914 		sz = sizeof(struct sysctlnode);
    915 		break;
    916 	case CTLTYPE_INT:
    917 		/*
    918 		 * since an int is an int, if the size is not given or
    919 		 * is wrong, we can "int-uit" it.
    920 		 */
    921 		if (sz != 0 && sz != sizeof(int))
    922 			return (EINVAL);
    923 		sz = sizeof(int);
    924 		break;
    925 	case CTLTYPE_STRING:
    926 		/*
    927 		 * strings are a little more tricky
    928 		 */
    929 		if (sz == 0) {
    930 			if (l == NULL) {
    931 				if (nnode.sysctl_func == NULL) {
    932 					if (nnode.sysctl_data == NULL)
    933 						return (EINVAL);
    934 					else
    935 						sz = strlen(nnode.sysctl_data) +
    936 						    1;
    937 				}
    938 			} else if (nnode.sysctl_data == NULL &&
    939 				 flags & CTLFLAG_OWNDATA) {
    940 				return (EINVAL);
    941 			} else {
    942 				char *vp, *e;
    943 				size_t s;
    944 
    945 				/*
    946 				 * we want a rough idea of what the
    947 				 * size is now
    948 				 */
    949 				vp = malloc(PAGE_SIZE, M_SYSCTLDATA,
    950 					     M_WAITOK|M_CANFAIL);
    951 				if (vp == NULL)
    952 					return (ENOMEM);
    953 				e = nnode.sysctl_data;
    954 				do {
    955 					error = copyinstr(e, vp, PAGE_SIZE, &s);
    956 					if (error) {
    957 						if (error != ENAMETOOLONG) {
    958 							free(vp, M_SYSCTLDATA);
    959 							return (error);
    960 						}
    961 						e += PAGE_SIZE;
    962 						if ((e - 32 * PAGE_SIZE) >
    963 						    (char*)nnode.sysctl_data) {
    964 							free(vp, M_SYSCTLDATA);
    965 							return (ERANGE);
    966 						}
    967 					}
    968 				} while (error != 0);
    969 				sz = s + (e - (char*)nnode.sysctl_data);
    970 				free(vp, M_SYSCTLDATA);
    971 			}
    972 		}
    973 		break;
    974 	case CTLTYPE_QUAD:
    975 		if (sz != 0 && sz != sizeof(u_quad_t))
    976 			return (EINVAL);
    977 		sz = sizeof(u_quad_t);
    978 		break;
    979 	case CTLTYPE_BOOL:
    980 		/*
    981 		 * since an bool is an bool, if the size is not given or
    982 		 * is wrong, we can "intuit" it.
    983 		 */
    984 		if (sz != 0 && sz != sizeof(bool))
    985 			return (EINVAL);
    986 		sz = sizeof(bool);
    987 		break;
    988 	case CTLTYPE_STRUCT:
    989 		if (sz == 0) {
    990 			if (l != NULL || nnode.sysctl_func == NULL)
    991 				return (EINVAL);
    992 			if (flags & CTLFLAG_OWNDATA)
    993 				return (EINVAL);
    994 		}
    995 		break;
    996 	default:
    997 		return (EINVAL);
    998 	}
    999 
   1000 	/*
   1001 	 * at this point, if sz is zero, we *must* have a
   1002 	 * function to go with it and we can't own it.
   1003 	 */
   1004 
   1005 	/*
   1006 	 *  l  ptr own
   1007 	 *  0   0   0  -> EINVAL (if no func)
   1008 	 *  0   0   1  -> own
   1009 	 *  0   1   0  -> kptr
   1010 	 *  0   1   1  -> kptr
   1011 	 *  1   0   0  -> EINVAL
   1012 	 *  1   0   1  -> own
   1013 	 *  1   1   0  -> kptr, no own (fault on lookup)
   1014 	 *  1   1   1  -> uptr, own
   1015 	 */
   1016 	if (type != CTLTYPE_NODE) {
   1017 		if (sz != 0) {
   1018 			if (flags & CTLFLAG_OWNDATA) {
   1019 				own = malloc(sz, M_SYSCTLDATA,
   1020 					     M_WAITOK|M_CANFAIL);
   1021 				if (own == NULL)
   1022 					return ENOMEM;
   1023 				if (nnode.sysctl_data == NULL)
   1024 					memset(own, 0, sz);
   1025 				else {
   1026 					error = sysctl_copyin(l,
   1027 					    nnode.sysctl_data, own, sz);
   1028 					if (error != 0) {
   1029 						free(own, M_SYSCTLDATA);
   1030 						return (error);
   1031 					}
   1032 				}
   1033 			} else if ((nnode.sysctl_data != NULL) &&
   1034 				 !(flags & CTLFLAG_IMMEDIATE)) {
   1035 #if NKSYMS > 0
   1036 				if (name[namelen - 1] == CTL_CREATESYM) {
   1037 					char symname[128]; /* XXX enough? */
   1038 					u_long symaddr;
   1039 					size_t symlen;
   1040 
   1041 					error = sysctl_copyinstr(l,
   1042 					    nnode.sysctl_data, symname,
   1043 					    sizeof(symname), &symlen);
   1044 					if (error)
   1045 						return (error);
   1046 					error = ksyms_getval(NULL, symname,
   1047 					    &symaddr, KSYMS_EXTERN);
   1048 					if (error)
   1049 						return (error); /* EINVAL? */
   1050 					nnode.sysctl_data = (void*)symaddr;
   1051 				}
   1052 #endif /* NKSYMS > 0 */
   1053 				/*
   1054 				 * Ideally, we'd like to verify here
   1055 				 * that this address is acceptable,
   1056 				 * but...
   1057 				 *
   1058 				 * - it might be valid now, only to
   1059 				 *   become invalid later
   1060 				 *
   1061 				 * - it might be invalid only for the
   1062 				 *   moment and valid later
   1063 				 *
   1064 				 * - or something else.
   1065 				 *
   1066 				 * Since we can't get a good answer,
   1067 				 * we'll just accept the address as
   1068 				 * given, and fault on individual
   1069 				 * lookups.
   1070 				 */
   1071 			}
   1072 		} else if (nnode.sysctl_func == NULL)
   1073 			return (EINVAL);
   1074 	}
   1075 
   1076 	/*
   1077 	 * a process can't assign a function to a node, and the kernel
   1078 	 * can't create a node that has no function or data.
   1079 	 * (XXX somewhat redundant check)
   1080 	 */
   1081 	if (l != NULL || nnode.sysctl_func == NULL) {
   1082 		if (type != CTLTYPE_NODE &&
   1083 		    nnode.sysctl_data == NULL &&
   1084 		    !(flags & CTLFLAG_IMMEDIATE) &&
   1085 		    own == NULL)
   1086 			return (EINVAL);
   1087 	}
   1088 
   1089 #ifdef SYSCTL_DISALLOW_KWRITE
   1090 	/*
   1091 	 * a process can't create a writable node unless it refers to
   1092 	 * new data.
   1093 	 */
   1094 	if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
   1095 	    (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY &&
   1096 	    !(flags & CTLFLAG_IMMEDIATE))
   1097 		return (EPERM);
   1098 #endif /* SYSCTL_DISALLOW_KWRITE */
   1099 
   1100 	/*
   1101 	 * make sure there's somewhere to put the new stuff.
   1102 	 */
   1103 	if (pnode->sysctl_child == NULL) {
   1104 		if (flags & CTLFLAG_ANYNUMBER)
   1105 			error = sysctl_alloc(pnode, 1);
   1106 		else
   1107 			error = sysctl_alloc(pnode, 0);
   1108 		if (error) {
   1109 			if (own != NULL)
   1110 				free(own, M_SYSCTLDATA);
   1111 			return (error);
   1112 		}
   1113 	}
   1114 	node = pnode->sysctl_child;
   1115 
   1116 	/*
   1117 	 * no collisions, so pick a good dynamic number if we need to.
   1118 	 */
   1119 	if (nm == CTL_CREATE) {
   1120 		nm = ++sysctl_root.sysctl_num;
   1121 		for (ni = 0; ni < pnode->sysctl_clen; ni++) {
   1122 			if (nm == node[ni].sysctl_num) {
   1123 				nm++;
   1124 				ni = -1;
   1125 			} else if (nm > node[ni].sysctl_num)
   1126 				at = ni + 1;
   1127 		}
   1128 	}
   1129 
   1130 	/*
   1131 	 * oops...ran out of space
   1132 	 */
   1133 	if (pnode->sysctl_clen == pnode->sysctl_csize) {
   1134 		error = sysctl_realloc(pnode);
   1135 		if (error) {
   1136 			if (own != NULL)
   1137 				free(own, M_SYSCTLDATA);
   1138 			return (error);
   1139 		}
   1140 		node = pnode->sysctl_child;
   1141 	}
   1142 
   1143 	/*
   1144 	 * insert new node data
   1145 	 */
   1146 	if (at < pnode->sysctl_clen) {
   1147 		int t;
   1148 
   1149 		/*
   1150 		 * move the nodes that should come after the new one
   1151 		 */
   1152 		memmove(&node[at + 1], &node[at],
   1153 			(pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
   1154 		memset(&node[at], 0, sizeof(struct sysctlnode));
   1155 		node[at].sysctl_parent = pnode;
   1156 		/*
   1157 		 * and...reparent any children of any moved nodes
   1158 		 */
   1159 		for (ni = at; ni <= pnode->sysctl_clen; ni++)
   1160 			if (node[ni].sysctl_child != NULL)
   1161 				for (t = 0; t < node[ni].sysctl_csize; t++)
   1162 					node[ni].sysctl_child[t].sysctl_parent =
   1163 						&node[ni];
   1164 	}
   1165 	node = &node[at];
   1166 	pnode->sysctl_clen++;
   1167 
   1168 	strlcpy(node->sysctl_name, nnode.sysctl_name,
   1169 		sizeof(node->sysctl_name));
   1170 	node->sysctl_num = nm;
   1171 	node->sysctl_size = sz;
   1172 	node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */
   1173 	node->sysctl_csize = 0;
   1174 	node->sysctl_clen = 0;
   1175 	if (own) {
   1176 		node->sysctl_data = own;
   1177 		node->sysctl_flags |= CTLFLAG_OWNDATA;
   1178 	} else if (flags & CTLFLAG_ALIAS) {
   1179 		node->sysctl_alias = anum;
   1180 	} else if (flags & CTLFLAG_IMMEDIATE) {
   1181 		switch (type) {
   1182 		case CTLTYPE_BOOL:
   1183 			node->sysctl_bdata = nnode.sysctl_bdata;
   1184 			break;
   1185 		case CTLTYPE_INT:
   1186 			node->sysctl_idata = nnode.sysctl_idata;
   1187 			break;
   1188 		case CTLTYPE_QUAD:
   1189 			node->sysctl_qdata = nnode.sysctl_qdata;
   1190 			break;
   1191 		}
   1192 	} else {
   1193 		node->sysctl_data = nnode.sysctl_data;
   1194 		node->sysctl_flags &= ~CTLFLAG_OWNDATA;
   1195 	}
   1196         node->sysctl_func = nnode.sysctl_func;
   1197         node->sysctl_child = NULL;
   1198 	/* node->sysctl_parent should already be done */
   1199 
   1200 	/*
   1201 	 * update "version" on path to "root"
   1202 	 */
   1203 	for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
   1204 		;
   1205 	pnode = node;
   1206 	for (nm = rnode->sysctl_ver + 1; pnode != NULL;
   1207 	     pnode = pnode->sysctl_parent)
   1208 		pnode->sysctl_ver = nm;
   1209 
   1210 	/* If this fails, the node is already added - the user won't know! */
   1211 	error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp);
   1212 
   1213 	return (error);
   1214 }
   1215 
   1216 /*
   1217  * ********************************************************************
   1218  * A wrapper around sysctl_create() that prints the thing we're trying
   1219  * to add.
   1220  * ********************************************************************
   1221  */
   1222 #ifdef SYSCTL_DEBUG_CREATE
   1223 int
   1224 sysctl_create(SYSCTLFN_ARGS)
   1225 {
   1226 	const struct sysctlnode *node;
   1227 	int k, rc, ni, nl = namelen + (name - oname);
   1228 
   1229 	node = newp;
   1230 
   1231 	printf("namelen %d (", nl);
   1232 	for (ni = 0; ni < nl - 1; ni++)
   1233 		printf(" %d", oname[ni]);
   1234 	printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
   1235 	       k = node->sysctl_num,
   1236 	       node->sysctl_name,
   1237 	       node->sysctl_flags,
   1238 	       SYSCTL_FLAGS(node->sysctl_flags),
   1239 	       SYSCTL_TYPE(node->sysctl_flags),
   1240 	       node->sysctl_size);
   1241 
   1242 	node = rnode;
   1243 	rc = _sysctl_create(SYSCTLFN_CALL(rnode));
   1244 
   1245 	printf("sysctl_create(");
   1246 	for (ni = 0; ni < nl - 1; ni++)
   1247 		printf(" %d", oname[ni]);
   1248 	printf(" %d ) returned %d\n", k, rc);
   1249 
   1250 	return (rc);
   1251 }
   1252 #endif /* SYSCTL_DEBUG_CREATE */
   1253 
   1254 /*
   1255  * sysctl_destroy -- Removes a node (as described by newp) from the
   1256  * given tree, returning (if successful) a copy of the dead node in
   1257  * oldp.  Since we're removing stuff, there's not much to check.
   1258  */
   1259 int
   1260 sysctl_destroy(SYSCTLFN_ARGS)
   1261 {
   1262 	struct sysctlnode *node, *pnode, onode, nnode;
   1263 	int ni, error, v;
   1264 
   1265 	KASSERT(rw_write_held(&sysctl_treelock));
   1266 
   1267 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1268 		printf("sysctl_destroy: rnode %p wrong version\n", rnode);
   1269 		return (EINVAL);
   1270 	}
   1271 
   1272 	error = 0;
   1273 
   1274 	if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
   1275 		return (EINVAL);
   1276 
   1277 	/*
   1278 	 * processes can only destroy nodes at securelevel 0, must be
   1279 	 * root, and can't remove nodes from a parent that's not
   1280 	 * writeable
   1281 	 */
   1282 	if (l != NULL) {
   1283 #ifndef SYSCTL_DISALLOW_CREATE
   1284 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
   1285 		    KAUTH_REQ_SYSTEM_SYSCTL_DELETE, NULL, NULL, NULL);
   1286 		if (error)
   1287 			return (error);
   1288 		if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
   1289 #endif /* SYSCTL_DISALLOW_CREATE */
   1290 			return (EPERM);
   1291 	}
   1292 
   1293 	/*
   1294 	 * nothing can remove a node if:
   1295 	 * the node is permanent (checked later) or
   1296 	 * the tree itself is not writeable or
   1297 	 * the entire sysctl system is not writeable
   1298 	 *
   1299 	 * note that we ignore whether setup is complete or not,
   1300 	 * because these rules always apply.
   1301 	 */
   1302 	if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
   1303 	    !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))
   1304 		return (EPERM);
   1305 
   1306 	if (newp == NULL)
   1307 		return (EINVAL);
   1308 	error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
   1309 	if (error)
   1310 		return (error);
   1311 	memset(&onode, 0, sizeof(struct sysctlnode));
   1312 
   1313 	node = rnode->sysctl_child;
   1314 	for (ni = 0; ni < rnode->sysctl_clen; ni++) {
   1315 		if (nnode.sysctl_num == node[ni].sysctl_num) {
   1316 			/*
   1317 			 * if name specified, must match
   1318 			 */
   1319 			if (nnode.sysctl_name[0] != '\0' &&
   1320 			    strcmp(nnode.sysctl_name, node[ni].sysctl_name))
   1321 				continue;
   1322 			/*
   1323 			 * if version specified, must match
   1324 			 */
   1325 			if (nnode.sysctl_ver != 0 &&
   1326 			    nnode.sysctl_ver != node[ni].sysctl_ver)
   1327 				continue;
   1328 			/*
   1329 			 * this must be the one
   1330 			 */
   1331 			break;
   1332 		}
   1333 	}
   1334 	if (ni == rnode->sysctl_clen)
   1335 		return (ENOENT);
   1336 	node = &node[ni];
   1337 	pnode = node->sysctl_parent;
   1338 
   1339 	/*
   1340 	 * if the kernel says permanent, it is, so there.  nyah.
   1341 	 */
   1342 	if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT)
   1343 		return (EPERM);
   1344 
   1345 	/*
   1346 	 * can't delete non-empty nodes
   1347 	 */
   1348 	if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
   1349 	    node->sysctl_clen != 0)
   1350 		return (ENOTEMPTY);
   1351 
   1352 	/*
   1353 	 * if the node "owns" data, release it now
   1354 	 */
   1355 	if (node->sysctl_flags & CTLFLAG_OWNDATA) {
   1356 		if (node->sysctl_data != NULL)
   1357 			free(node->sysctl_data, M_SYSCTLDATA);
   1358 		node->sysctl_data = NULL;
   1359 	}
   1360 	if (node->sysctl_flags & CTLFLAG_OWNDESC) {
   1361 		if (node->sysctl_desc != NULL)
   1362 			/*XXXUNCONST*/
   1363 			free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA);
   1364 		node->sysctl_desc = NULL;
   1365 	}
   1366 
   1367 	/*
   1368 	 * if the node to be removed is not the last one on the list,
   1369 	 * move the remaining nodes up, and reparent any grandchildren
   1370 	 */
   1371 	onode = *node;
   1372 	if (ni < pnode->sysctl_clen - 1) {
   1373 		int t;
   1374 
   1375 		memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
   1376 			(pnode->sysctl_clen - ni - 1) *
   1377 			sizeof(struct sysctlnode));
   1378 		for (; ni < pnode->sysctl_clen - 1; ni++)
   1379 			if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
   1380 			    CTLTYPE_NODE)
   1381 				for (t = 0;
   1382 				     t < pnode->sysctl_child[ni].sysctl_clen;
   1383 				     t++)
   1384 					pnode->sysctl_child[ni].sysctl_child[t].
   1385 						sysctl_parent =
   1386 						&pnode->sysctl_child[ni];
   1387 		ni = pnode->sysctl_clen - 1;
   1388 		node = &pnode->sysctl_child[ni];
   1389 	}
   1390 
   1391 	/*
   1392 	 * reset the space we just vacated
   1393 	 */
   1394 	memset(node, 0, sizeof(struct sysctlnode));
   1395 	node->sysctl_parent = pnode;
   1396 	pnode->sysctl_clen--;
   1397 
   1398 	/*
   1399 	 * if this parent just lost its last child, nuke the creche
   1400 	 */
   1401 	if (pnode->sysctl_clen == 0) {
   1402 		free(pnode->sysctl_child, M_SYSCTLNODE);
   1403 		pnode->sysctl_csize = 0;
   1404 		pnode->sysctl_child = NULL;
   1405 	}
   1406 
   1407 	/*
   1408 	 * update "version" on path to "root"
   1409 	 */
   1410         for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
   1411                 ;
   1412 	for (ni = rnode->sysctl_ver + 1; pnode != NULL;
   1413 	     pnode = pnode->sysctl_parent)
   1414 		pnode->sysctl_ver = ni;
   1415 
   1416 	error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp);
   1417 
   1418 	return (error);
   1419 }
   1420 
   1421 /*
   1422  * sysctl_lookup -- Handles copyin/copyout of new and old values.
   1423  * Partial reads are globally allowed.  Only root can write to things
   1424  * unless the node says otherwise.
   1425  */
   1426 int
   1427 sysctl_lookup(SYSCTLFN_ARGS)
   1428 {
   1429 	int error, rw;
   1430 	size_t sz, len;
   1431 	void *d;
   1432 
   1433 	KASSERT(rw_lock_held(&sysctl_treelock));
   1434 
   1435 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1436 		printf("%s: rnode %p wrong version\n", __func__, rnode);
   1437 		return EINVAL;
   1438 	}
   1439 
   1440 	error = 0;
   1441 
   1442 	/*
   1443 	 * you can't "look up" a node.  you can "query" it, but you
   1444 	 * can't "look it up".
   1445 	 */
   1446 	if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0) {
   1447 		DPRINTF(("%s: can't lookup a node\n", __func__));
   1448 		return EINVAL;
   1449 	}
   1450 
   1451 	/*
   1452 	 * some nodes are private, so only root can look into them.
   1453 	 */
   1454 	if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
   1455 	    (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
   1456 	    KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) {
   1457 		DPRINTF(("%s: private node\n", __func__));
   1458 		return error;
   1459 	}
   1460 
   1461 	/*
   1462 	 * if a node wants to be writable according to different rules
   1463 	 * other than "only root can write to stuff unless a flag is
   1464 	 * set", then it needs its own function which should have been
   1465 	 * called and not us.
   1466 	 */
   1467 	if (l != NULL && newp != NULL &&
   1468 	    !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
   1469 	    (error = kauth_authorize_system(l->l_cred,
   1470 	    KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_MODIFY, NULL, NULL,
   1471 	    NULL)) != 0) {
   1472 		DPRINTF(("%s: can't modify\n", __func__));
   1473 		return error;
   1474 	}
   1475 
   1476 	/*
   1477 	 * is this node supposedly writable?
   1478 	 */
   1479 	rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0;
   1480 
   1481 	/*
   1482 	 * it appears not to be writable at this time, so if someone
   1483 	 * tried to write to it, we must tell them to go away
   1484 	 */
   1485 	if (!rw && newp != NULL) {
   1486 		DPRINTF(("%s: not writable\n", __func__));
   1487 		return EPERM;
   1488 	}
   1489 
   1490 	/*
   1491 	 * step one, copy out the stuff we have presently
   1492 	 */
   1493 	if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
   1494 		/*
   1495 		 * note that we discard const here because we are
   1496 		 * modifying the contents of the node (which is okay
   1497 		 * because it's ours)
   1498 		 */
   1499 		switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
   1500 		case CTLTYPE_BOOL:
   1501 			d = __UNCONST(&rnode->sysctl_bdata);
   1502 			break;
   1503 		case CTLTYPE_INT:
   1504 			d = __UNCONST(&rnode->sysctl_idata);
   1505 			break;
   1506 		case CTLTYPE_QUAD:
   1507 			d = __UNCONST(&rnode->sysctl_qdata);
   1508 			break;
   1509 		default:
   1510 			DPRINTF(("%s: bad type\n", __func__));
   1511 			return EINVAL;
   1512 		}
   1513 	} else
   1514 		d = rnode->sysctl_data;
   1515 	if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
   1516 		sz = strlen(d) + 1; /* XXX@@@ possible fault here */
   1517 	else
   1518 		sz = rnode->sysctl_size;
   1519 	if (oldp != NULL) {
   1520 		error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
   1521 		if (error) {
   1522 			DPRINTF(("%s: bad copyout %d\n", __func__, error));
   1523 			return error;
   1524 		}
   1525 	}
   1526 	*oldlenp = sz;
   1527 
   1528 	/*
   1529 	 * are we done?
   1530 	 */
   1531 	if (newp == NULL || newlen == 0)
   1532 		return 0;
   1533 
   1534 	/*
   1535 	 * hmm...not done.  must now "copy in" new value.  re-adjust
   1536 	 * sz to maximum value (strings are "weird").
   1537 	 */
   1538 	sz = rnode->sysctl_size;
   1539 	switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
   1540 	case CTLTYPE_BOOL: {
   1541 		bool tmp;
   1542 		/*
   1543 		 * these data must be *exactly* the same size coming
   1544 		 * in.  bool may only be true or false.
   1545 		 */
   1546 		if (newlen != sz) {
   1547 			DPRINTF(("%s: bad size %zu != %zu\n", __func__, newlen,
   1548 			    sz));
   1549 			return EINVAL;
   1550 		}
   1551 		error = sysctl_copyin(l, newp, &tmp, sz);
   1552 		if (tmp != true && tmp != false) {
   1553 			DPRINTF(("%s: tmp %d\n", __func__, tmp));
   1554 			return EINVAL;
   1555 		}
   1556 		if (error) {
   1557 			DPRINTF(("%s: copyin %d\n", __func__, error));
   1558 			break;
   1559 		}
   1560 		*(bool *)d = tmp;
   1561 		break;
   1562 	}
   1563 	case CTLTYPE_INT:
   1564 	case CTLTYPE_QUAD:
   1565 	case CTLTYPE_STRUCT:
   1566 		/*
   1567 		 * these data must be *exactly* the same size coming
   1568 		 * in.
   1569 		 */
   1570 		if (newlen != sz) {
   1571 			DPRINTF(("%s: bad size %zu != %zu\n", __func__, newlen,
   1572 			    sz));
   1573 			return EINVAL;
   1574 		}
   1575 		error = sysctl_copyin(l, newp, d, sz);
   1576 		break;
   1577 	case CTLTYPE_STRING: {
   1578 		/*
   1579 		 * strings, on the other hand, can be shorter, and we
   1580 		 * let userland be sloppy about the trailing nul.
   1581 		 */
   1582 		char *newbuf;
   1583 
   1584 		/*
   1585 		 * too much new string?
   1586 		 */
   1587 		if (newlen > sz) {
   1588 			DPRINTF(("%s: bad size %zu > %zu\n", __func__, newlen,
   1589 			    sz));
   1590 			return EINVAL;
   1591 		}
   1592 
   1593 		/*
   1594 		 * temporary copy of new inbound string
   1595 		 */
   1596 		len = MIN(sz, newlen);
   1597 		newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   1598 		if (newbuf == NULL) {
   1599 			DPRINTF(("%s: oomem %zu\n", __func__, len));
   1600 			return ENOMEM;
   1601 		}
   1602 		error = sysctl_copyin(l, newp, newbuf, len);
   1603 		if (error) {
   1604 			free(newbuf, M_SYSCTLDATA);
   1605 			DPRINTF(("%s: copyin %d\n", __func__, error));
   1606 			return error;
   1607 		}
   1608 
   1609 		/*
   1610 		 * did they NUL terminate it, or do we have space
   1611 		 * left to do it ourselves?
   1612 		 */
   1613 		if (newbuf[len - 1] != '\0' && len == sz) {
   1614 			free(newbuf, M_SYSCTLDATA);
   1615 			DPRINTF(("%s: string too long\n", __func__));
   1616 			return EINVAL;
   1617 		}
   1618 
   1619 		/*
   1620 		 * looks good, so pop it into place and zero the rest.
   1621 		 */
   1622 		if (len > 0)
   1623 			memcpy(d, newbuf, len);
   1624 		if (sz != len)
   1625 			memset((char*)d + len, 0, sz - len);
   1626 		free(newbuf, M_SYSCTLDATA);
   1627 		break;
   1628 	}
   1629 	default:
   1630 		DPRINTF(("%s: bad type\n", __func__));
   1631 		return EINVAL;
   1632 	}
   1633 	if (error) {
   1634 		DPRINTF(("%s: copyin %d\n", __func__, error));
   1635 	}
   1636 
   1637 	return error;
   1638 }
   1639 
   1640 /*
   1641  * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
   1642  * purport to handle it.  This interface isn't fully fleshed out yet,
   1643  * unfortunately.
   1644  */
   1645 static int
   1646 sysctl_mmap(SYSCTLFN_ARGS)
   1647 {
   1648 	const struct sysctlnode *node;
   1649 	struct sysctlnode nnode;
   1650 	int error;
   1651 
   1652 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1653 		printf("sysctl_mmap: rnode %p wrong version\n", rnode);
   1654 		return (EINVAL);
   1655 	}
   1656 
   1657 	/*
   1658 	 * let's just pretend that didn't happen, m'kay?
   1659 	 */
   1660 	if (l == NULL)
   1661 		return (EPERM);
   1662 
   1663 	/*
   1664 	 * is this a sysctlnode description of an mmap request?
   1665 	 */
   1666 	if (newp == NULL || newlen != sizeof(struct sysctlnode))
   1667 		return (EINVAL);
   1668 	error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
   1669 	if (error)
   1670 		return (error);
   1671 
   1672 	/*
   1673 	 * does the node they asked for exist?
   1674 	 */
   1675 	if (namelen != 1)
   1676 		return (EOPNOTSUPP);
   1677 	node = rnode;
   1678         error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
   1679 	if (error)
   1680 		return (error);
   1681 
   1682 	/*
   1683 	 * does this node that we have found purport to handle mmap?
   1684 	 */
   1685 	if (node->sysctl_func == NULL ||
   1686 	    !(node->sysctl_flags & CTLFLAG_MMAP))
   1687 		return (EOPNOTSUPP);
   1688 
   1689 	/*
   1690 	 * well...okay, they asked for it.
   1691 	 */
   1692 	return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
   1693 }
   1694 
   1695 int
   1696 sysctl_describe(SYSCTLFN_ARGS)
   1697 {
   1698 	struct sysctldesc *d;
   1699 	void *bf;
   1700 	size_t sz, left, tot;
   1701 	int i, error, v = -1;
   1702 	struct sysctlnode *node;
   1703 	struct sysctlnode dnode;
   1704 
   1705 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1706 		printf("sysctl_query: rnode %p wrong version\n", rnode);
   1707 		return (EINVAL);
   1708 	}
   1709 
   1710 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
   1711 		return (ENOTDIR);
   1712 	if (namelen != 1 || name[0] != CTL_DESCRIBE)
   1713 		return (EINVAL);
   1714 
   1715 	/*
   1716 	 * get ready...
   1717 	 */
   1718 	error = 0;
   1719 	d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK|M_CANFAIL);
   1720 	if (bf == NULL)
   1721 		return ENOMEM;
   1722 	tot = 0;
   1723 	node = rnode->sysctl_child;
   1724 	left = *oldlenp;
   1725 
   1726 	/*
   1727 	 * no request -> all descriptions at this level
   1728 	 * request with desc unset -> just this node
   1729 	 * request with desc set -> set descr for this node
   1730 	 */
   1731 	if (newp != NULL) {
   1732 		error = sysctl_cvt_in(l, &v, newp, newlen, &dnode);
   1733 		if (error)
   1734 			goto out;
   1735 		if (dnode.sysctl_desc != NULL) {
   1736 			/*
   1737 			 * processes cannot set descriptions above
   1738 			 * securelevel 0.  and must be root.  blah
   1739 			 * blah blah.  a couple more checks are made
   1740 			 * once we find the node we want.
   1741 			 */
   1742 			if (l != NULL) {
   1743 #ifndef SYSCTL_DISALLOW_CREATE
   1744 				error = kauth_authorize_system(l->l_cred,
   1745 				    KAUTH_SYSTEM_SYSCTL,
   1746 				    KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL,
   1747 				    NULL, NULL);
   1748 				if (error)
   1749 					goto out;
   1750 #else /* SYSCTL_DISALLOW_CREATE */
   1751 				error = EPERM;
   1752 				goto out;
   1753 #endif /* SYSCTL_DISALLOW_CREATE */
   1754 			}
   1755 
   1756 			/*
   1757 			 * find node and try to set the description on it
   1758 			 */
   1759 			for (i = 0; i < rnode->sysctl_clen; i++)
   1760 				if (node[i].sysctl_num == dnode.sysctl_num)
   1761 					break;
   1762 			if (i == rnode->sysctl_clen) {
   1763 				error = ENOENT;
   1764 				goto out;
   1765 			}
   1766 			node = &node[i];
   1767 
   1768 			/*
   1769 			 * did the caller specify a node version?
   1770 			 */
   1771 			if (dnode.sysctl_ver != 0 &&
   1772 			    dnode.sysctl_ver != node->sysctl_ver) {
   1773 				error = EINVAL;
   1774 				goto out;
   1775 			}
   1776 
   1777 			/*
   1778 			 * okay...some rules:
   1779 			 * (1) if setup is done and the tree is
   1780 			 *     read-only or the whole system is
   1781 			 *     read-only
   1782 			 * (2) no one can set a description on a
   1783 			 *     permanent node (it must be set when
   1784 			 *     using createv)
   1785 			 * (3) processes cannot *change* a description
   1786 			 * (4) processes *can*, however, set a
   1787 			 *     description on a read-only node so that
   1788 			 *     one can be created and then described
   1789 			 *     in two steps
   1790 			 * anything else come to mind?
   1791 			 */
   1792 			if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
   1793 			    (!(sysctl_rootof(node)->sysctl_flags &
   1794 			       CTLFLAG_READWRITE) ||
   1795 			     !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) {
   1796 				error = EPERM;
   1797 				goto out;
   1798 			}
   1799 			if (node->sysctl_flags & CTLFLAG_PERMANENT) {
   1800 				error = EPERM;
   1801 				goto out;
   1802 			}
   1803 			if (l != NULL && node->sysctl_desc != NULL) {
   1804 				error = EPERM;
   1805 				goto out;
   1806 			}
   1807 
   1808 			/*
   1809 			 * right, let's go ahead.  the first step is
   1810 			 * making the description into something the
   1811 			 * node can "own", if need be.
   1812 			 */
   1813 			if (l != NULL ||
   1814 			    dnode.sysctl_flags & CTLFLAG_OWNDESC) {
   1815 				char *nd, *k;
   1816 
   1817 				k = malloc(MAXDESCLEN, M_TEMP,
   1818 				    M_WAITOK|M_CANFAIL);
   1819 				if (k == NULL) {
   1820 					error = ENOMEM;
   1821 					goto out;
   1822 				}
   1823 				error = sysctl_copyinstr(l, dnode.sysctl_desc,
   1824 							 k, MAXDESCLEN, &sz);
   1825 				if (error) {
   1826 					free(k, M_TEMP);
   1827 					goto out;
   1828 				}
   1829 				nd = malloc(sz, M_SYSCTLDATA,
   1830 					    M_WAITOK|M_CANFAIL);
   1831 				if (nd == NULL) {
   1832 					free(k, M_TEMP);
   1833 					error = ENOMEM;
   1834 					goto out;
   1835 				}
   1836 				memcpy(nd, k, sz);
   1837 				dnode.sysctl_flags |= CTLFLAG_OWNDESC;
   1838 				dnode.sysctl_desc = nd;
   1839 				free(k, M_TEMP);
   1840 			}
   1841 
   1842 			/*
   1843 			 * now "release" the old description and
   1844 			 * attach the new one.  ta-da.
   1845 			 */
   1846 			if ((node->sysctl_flags & CTLFLAG_OWNDESC) &&
   1847 			    node->sysctl_desc != NULL)
   1848 				/*XXXUNCONST*/
   1849 				free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA);
   1850 			node->sysctl_desc = dnode.sysctl_desc;
   1851 			node->sysctl_flags |=
   1852 				(dnode.sysctl_flags & CTLFLAG_OWNDESC);
   1853 
   1854 			/*
   1855 			 * now we "fall out" and into the loop which
   1856 			 * will copy the new description back out for
   1857 			 * those interested parties
   1858 			 */
   1859 		}
   1860 	}
   1861 
   1862 	/*
   1863 	 * scan for one description or just retrieve all descriptions
   1864 	 */
   1865 	for (i = 0; i < rnode->sysctl_clen; i++) {
   1866 		/*
   1867 		 * did they ask for the description of only one node?
   1868 		 */
   1869 		if (v != -1 && node[i].sysctl_num != dnode.sysctl_num)
   1870 			continue;
   1871 
   1872 		/*
   1873 		 * don't describe "private" nodes to non-suser users
   1874 		 */
   1875 		if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) &&
   1876 		    !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
   1877 		    KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)))
   1878 			continue;
   1879 
   1880 		/*
   1881 		 * is this description "valid"?
   1882 		 */
   1883 		memset(bf, 0, MAXDESCLEN);
   1884 		if (node[i].sysctl_desc == NULL)
   1885 			sz = 1;
   1886 		else if (copystr(node[i].sysctl_desc, &d->descr_str[0],
   1887 				 MAXDESCLEN - sizeof(*d), &sz) != 0) {
   1888 			/*
   1889 			 * erase possible partial description
   1890 			 */
   1891 			memset(bf, 0, MAXDESCLEN);
   1892 			sz = 1;
   1893 		}
   1894 
   1895 		/*
   1896 		 * we've got it, stuff it into the caller's buffer
   1897 		 */
   1898 		d->descr_num = node[i].sysctl_num;
   1899 		d->descr_ver = node[i].sysctl_ver;
   1900 		d->descr_len = sz; /* includes trailing nul */
   1901 		sz = (char *)NEXT_DESCR(d) - (char *)d;
   1902 		if (oldp != NULL && left >= sz) {
   1903 			error = sysctl_copyout(l, d, oldp, sz);
   1904 			if (error)
   1905 				goto out;
   1906 			left -= sz;
   1907 			oldp = (void *)__sysc_desc_adv(oldp, d->descr_len);
   1908 		}
   1909 		tot += sz;
   1910 
   1911 		/*
   1912 		 * if we get this far with v not "unset", they asked
   1913 		 * for a specific node and we found it
   1914 		 */
   1915 		if (v != -1)
   1916 			break;
   1917 	}
   1918 
   1919 	/*
   1920 	 * did we find it after all?
   1921 	 */
   1922 	if (v != -1 && tot == 0)
   1923 		error = ENOENT;
   1924 	else
   1925 		*oldlenp = tot;
   1926 
   1927 out:
   1928 	free(bf, M_TEMP);
   1929 	return (error);
   1930 }
   1931 
   1932 /*
   1933  * ********************************************************************
   1934  * Section 3: Create and destroy from inside the kernel
   1935  * ********************************************************************
   1936  * sysctl_createv() and sysctl_destroyv() are simpler-to-use
   1937  * interfaces for the kernel to fling new entries into the mib and rip
   1938  * them out later.  In the case of sysctl_createv(), the returned copy
   1939  * of the node (see sysctl_create()) will be translated back into a
   1940  * pointer to the actual node.
   1941  *
   1942  * Note that sysctl_createv() will return 0 if the create request
   1943  * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
   1944  * will return 0 if the node to be destroyed already does not exist
   1945  * (aka rm -f) or if it is a parent of other nodes.
   1946  *
   1947  * This allows two (or more) different subsystems to assert sub-tree
   1948  * existence before populating their own nodes, and to remove their
   1949  * own nodes without orphaning the others when they are done.
   1950  * ********************************************************************
   1951  */
   1952 #undef sysctl_createv
   1953 int
   1954 sysctl_createv(struct sysctllog **log, int cflags,
   1955 	       const struct sysctlnode **rnode, const struct sysctlnode **cnode,
   1956 	       int flags, int type, const char *namep, const char *descr,
   1957 	       sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
   1958 	       ...)
   1959 {
   1960 	va_list ap;
   1961 	int error, ni, namelen, name[CTL_MAXNAME];
   1962 	const struct sysctlnode *root, *pnode;
   1963 	struct sysctlnode nnode, onode, *dnode;
   1964 	size_t sz;
   1965 
   1966 	/*
   1967 	 * where are we putting this?
   1968 	 */
   1969 	if (rnode != NULL && *rnode == NULL) {
   1970 		printf("sysctl_createv: rnode NULL\n");
   1971 		return (EINVAL);
   1972 	}
   1973 	root = rnode ? *rnode : NULL;
   1974 	if (cnode != NULL)
   1975 		*cnode = NULL;
   1976 	if (cflags != 0)
   1977 		return (EINVAL);
   1978 
   1979 	/*
   1980 	 * what is it?
   1981 	 */
   1982 	flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
   1983 	if (log != NULL)
   1984 		flags &= ~CTLFLAG_PERMANENT;
   1985 
   1986 	/*
   1987 	 * where do we put it?
   1988 	 */
   1989 	va_start(ap, newlen);
   1990 	namelen = 0;
   1991 	error = 0;
   1992 	ni = -1;
   1993 	do {
   1994 		if (++ni == CTL_MAXNAME) {
   1995 			error = ENAMETOOLONG;
   1996 			break;
   1997 		}
   1998 		name[ni] = va_arg(ap, int);
   1999 		/*
   2000 		 * sorry, this is not supported from here
   2001 		 */
   2002 		if (name[ni] == CTL_CREATESYM) {
   2003 			error = EINVAL;
   2004 			break;
   2005 		}
   2006 	} while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
   2007 	va_end(ap);
   2008 	if (error)
   2009 		return error;
   2010 	namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
   2011 
   2012 	/*
   2013 	 * what's it called
   2014 	 */
   2015 	if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >=
   2016 	    sizeof(nnode.sysctl_name))
   2017 		return (ENAMETOOLONG);
   2018 
   2019 	/*
   2020 	 * cons up the description of the new node
   2021 	 */
   2022 	nnode.sysctl_num = name[namelen - 1];
   2023 	name[namelen - 1] = CTL_CREATE;
   2024 	nnode.sysctl_size = newlen;
   2025 	nnode.sysctl_flags = flags;
   2026 	if (type == CTLTYPE_NODE) {
   2027 		nnode.sysctl_csize = 0;
   2028 		nnode.sysctl_clen = 0;
   2029 		nnode.sysctl_child = NULL;
   2030 		if (flags & CTLFLAG_ALIAS)
   2031 			nnode.sysctl_alias = qv;
   2032 	} else if (flags & CTLFLAG_IMMEDIATE) {
   2033 		switch (type) {
   2034 		case CTLTYPE_BOOL:
   2035 			nnode.sysctl_bdata = qv;
   2036 			break;
   2037 		case CTLTYPE_INT:
   2038 			nnode.sysctl_idata = qv;
   2039 			break;
   2040 		case CTLTYPE_QUAD:
   2041 			nnode.sysctl_qdata = qv;
   2042 			break;
   2043 		default:
   2044 			return (EINVAL);
   2045 		}
   2046 	} else {
   2047 		nnode.sysctl_data = newp;
   2048 	}
   2049 	nnode.sysctl_func = func;
   2050 	nnode.sysctl_parent = NULL;
   2051 	nnode.sysctl_ver = 0;
   2052 
   2053 	/*
   2054 	 * initialize lock state -- we need locks if the main tree has
   2055 	 * been marked as complete, but since we could be called from
   2056 	 * either there, or from a device driver (say, at device
   2057 	 * insertion), or from a module (at module load time, say), we
   2058 	 * don't really want to "wait"...
   2059 	 */
   2060 	sysctl_lock(true);
   2061 
   2062 	/*
   2063 	 * locate the prospective parent of the new node, and if we
   2064 	 * find it, add the new node.
   2065 	 */
   2066 	sz = sizeof(onode);
   2067 	pnode = root;
   2068 	error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
   2069 	if (error) {
   2070 		printf("sysctl_createv: sysctl_locate(%s) returned %d\n",
   2071 		       nnode.sysctl_name, error);
   2072 		sysctl_unlock();
   2073 		return (error);
   2074 	}
   2075 	error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
   2076 			      &nnode, sizeof(nnode), &name[0], NULL,
   2077 			      pnode);
   2078 
   2079 	/*
   2080 	 * unfortunately the node we wanted to create is already
   2081 	 * there.  if the node that's already there is a reasonable
   2082 	 * facsimile of the node we wanted to create, just pretend
   2083 	 * (for the caller's benefit) that we managed to create the
   2084 	 * node they wanted.
   2085 	 */
   2086 	if (error == EEXIST) {
   2087 		/* name is the same as requested... */
   2088 		if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
   2089 		    /* they want the same function... */
   2090 		    nnode.sysctl_func == onode.sysctl_func &&
   2091 		    /* number is the same as requested, or... */
   2092 		    (nnode.sysctl_num == onode.sysctl_num ||
   2093 		     /* they didn't pick a number... */
   2094 		     nnode.sysctl_num == CTL_CREATE)) {
   2095 			/*
   2096 			 * collision here from trying to create
   2097 			 * something that already existed; let's give
   2098 			 * our customers a hand and tell them they got
   2099 			 * what they wanted.
   2100 			 */
   2101 #ifdef SYSCTL_DEBUG_CREATE
   2102 			printf("cleared\n");
   2103 #endif /* SYSCTL_DEBUG_CREATE */
   2104 			error = 0;
   2105 		}
   2106 	}
   2107 
   2108 	if (error == 0 &&
   2109 	    (cnode != NULL || log != NULL || descr != NULL)) {
   2110 		/*
   2111 		 * sysctl_create() gave us back a copy of the node,
   2112 		 * but we need to know where it actually is...
   2113 		 */
   2114 		pnode = root;
   2115 		error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
   2116 
   2117 		/*
   2118 		 * manual scan of last layer so that aliased nodes
   2119 		 * aren't followed.
   2120 		 */
   2121 		if (error == 0) {
   2122 			for (ni = 0; ni < pnode->sysctl_clen; ni++)
   2123 				if (pnode->sysctl_child[ni].sysctl_num ==
   2124 				    onode.sysctl_num)
   2125 					break;
   2126 			if (ni < pnode->sysctl_clen)
   2127 				pnode = &pnode->sysctl_child[ni];
   2128 			else
   2129 				error = ENOENT;
   2130 		}
   2131 
   2132 		/*
   2133 		 * not expecting an error here, but...
   2134 		 */
   2135 		if (error == 0) {
   2136 			if (log != NULL)
   2137 				sysctl_log_add(log, pnode);
   2138 			if (cnode != NULL)
   2139 				*cnode = pnode;
   2140 			if (descr != NULL) {
   2141 				/*
   2142 				 * allow first caller to *set* a
   2143 				 * description actually to set it
   2144 				 *
   2145 				 * discard const here so we can attach
   2146 				 * the description
   2147 				 */
   2148 				dnode = __UNCONST(pnode);
   2149 				if (pnode->sysctl_desc != NULL)
   2150 					/* skip it...we've got one */;
   2151 				else if (flags & CTLFLAG_OWNDESC) {
   2152 					size_t l = strlen(descr) + 1;
   2153 					char *d = malloc(l, M_SYSCTLDATA,
   2154 							 M_WAITOK|M_CANFAIL);
   2155 					if (d != NULL) {
   2156 						memcpy(d, descr, l);
   2157 						dnode->sysctl_desc = d;
   2158 						dnode->sysctl_flags |=
   2159 						    CTLFLAG_OWNDESC;
   2160 					}
   2161 				} else
   2162 					dnode->sysctl_desc = descr;
   2163 			}
   2164 		} else {
   2165 			printf("sysctl_create succeeded but node not found?!\n");
   2166 			/*
   2167 			 *  confusing, but the create said it
   2168 			 * succeeded, so...
   2169 			 */
   2170 			error = 0;
   2171 		}
   2172 	}
   2173 
   2174 	/*
   2175 	 * now it should be safe to release the lock state.  note that
   2176 	 * the pointer to the newly created node being passed back may
   2177 	 * not be "good" for very long.
   2178 	 */
   2179 	sysctl_unlock();
   2180 
   2181 	if (error != 0) {
   2182 		printf("sysctl_createv: sysctl_create(%s) returned %d\n",
   2183 		       nnode.sysctl_name, error);
   2184 #if 0
   2185 		if (error != ENOENT)
   2186 			sysctl_dump(&onode);
   2187 #endif
   2188 	}
   2189 
   2190 	return (error);
   2191 }
   2192 
   2193 int
   2194 sysctl_destroyv(struct sysctlnode *rnode, ...)
   2195 {
   2196 	va_list ap;
   2197 	int error, name[CTL_MAXNAME], namelen, ni;
   2198 	const struct sysctlnode *pnode, *node;
   2199 	struct sysctlnode dnode, *onode;
   2200 	size_t sz;
   2201 
   2202 	va_start(ap, rnode);
   2203 	namelen = 0;
   2204 	ni = 0;
   2205 	do {
   2206 		if (ni == CTL_MAXNAME) {
   2207 			va_end(ap);
   2208 			return (ENAMETOOLONG);
   2209 		}
   2210 		name[ni] = va_arg(ap, int);
   2211 	} while (name[ni++] != CTL_EOL);
   2212 	namelen = ni - 1;
   2213 	va_end(ap);
   2214 
   2215 	/*
   2216 	 * i can't imagine why we'd be destroying a node when the tree
   2217 	 * wasn't complete, but who knows?
   2218 	 */
   2219 	sysctl_lock(true);
   2220 
   2221 	/*
   2222 	 * where is it?
   2223 	 */
   2224 	node = rnode;
   2225 	error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
   2226 	if (error) {
   2227 		/* they want it gone and it's not there, so... */
   2228 		sysctl_unlock();
   2229 		return (error == ENOENT ? 0 : error);
   2230 	}
   2231 
   2232 	/*
   2233 	 * set up the deletion
   2234 	 */
   2235 	pnode = node;
   2236 	node = &dnode;
   2237 	memset(&dnode, 0, sizeof(dnode));
   2238 	dnode.sysctl_flags = SYSCTL_VERSION;
   2239 	dnode.sysctl_num = name[namelen - 1];
   2240 
   2241 	/*
   2242 	 * we found it, now let's nuke it
   2243 	 */
   2244 	name[namelen - 1] = CTL_DESTROY;
   2245 	sz = 0;
   2246 	error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
   2247 			       node, sizeof(*node), &name[0], NULL,
   2248 			       pnode);
   2249 	if (error == ENOTEMPTY) {
   2250 		/*
   2251 		 * think of trying to delete "foo" when "foo.bar"
   2252 		 * (which someone else put there) is still in
   2253 		 * existence
   2254 		 */
   2255 		error = 0;
   2256 
   2257 		/*
   2258 		 * dunno who put the description there, but if this
   2259 		 * node can ever be removed, we need to make sure the
   2260 		 * string doesn't go out of context.  that means we
   2261 		 * need to find the node that's still there (don't use
   2262 		 * sysctl_locate() because that follows aliasing).
   2263 		 */
   2264 		node = pnode->sysctl_child;
   2265 		for (ni = 0; ni < pnode->sysctl_clen; ni++)
   2266 			if (node[ni].sysctl_num == dnode.sysctl_num)
   2267 				break;
   2268 		node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL;
   2269 
   2270 		/*
   2271 		 * if we found it, and this node has a description,
   2272 		 * and this node can be released, and it doesn't
   2273 		 * already own its own description...sigh.  :)
   2274 		 */
   2275 		if (node != NULL && node->sysctl_desc != NULL &&
   2276 		    !(node->sysctl_flags & CTLFLAG_PERMANENT) &&
   2277 		    !(node->sysctl_flags & CTLFLAG_OWNDESC)) {
   2278 			char *d;
   2279 
   2280 			sz = strlen(node->sysctl_desc) + 1;
   2281 			d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2282 			if (d != NULL) {
   2283 				/*
   2284 				 * discard const so that we can
   2285 				 * re-attach the description
   2286 				 */
   2287 				memcpy(d, node->sysctl_desc, sz);
   2288 				onode = __UNCONST(node);
   2289 				onode->sysctl_desc = d;
   2290 				onode->sysctl_flags |= CTLFLAG_OWNDESC;
   2291 			} else {
   2292 				/*
   2293 				 * XXX drop the description?  be
   2294 				 * afraid?  don't care?
   2295 				 */
   2296 			}
   2297 		}
   2298 	}
   2299 
   2300         sysctl_unlock();
   2301 
   2302 	return (error);
   2303 }
   2304 
   2305 /*
   2306  * ********************************************************************
   2307  * Deletes an entire n-ary tree.  Not recommended unless you know why
   2308  * you're doing it.  Personally, I don't know why you'd even think
   2309  * about it.
   2310  * ********************************************************************
   2311  */
   2312 void
   2313 sysctl_free(struct sysctlnode *rnode)
   2314 {
   2315 	struct sysctlnode *node, *pnode;
   2316 
   2317 	rw_enter(&sysctl_treelock, RW_WRITER);
   2318 
   2319 	if (rnode == NULL)
   2320 		rnode = &sysctl_root;
   2321 
   2322 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   2323 		printf("sysctl_free: rnode %p wrong version\n", rnode);
   2324 		rw_exit(&sysctl_treelock);
   2325 		return;
   2326 	}
   2327 
   2328 	pnode = rnode;
   2329 
   2330 	node = pnode->sysctl_child;
   2331 	do {
   2332 		while (node != NULL && pnode->sysctl_csize > 0) {
   2333 			while (node <
   2334 			       &pnode->sysctl_child[pnode->sysctl_clen] &&
   2335 			       (SYSCTL_TYPE(node->sysctl_flags) !=
   2336 				CTLTYPE_NODE ||
   2337 				node->sysctl_csize == 0)) {
   2338 				if (SYSCTL_FLAGS(node->sysctl_flags) &
   2339 				    CTLFLAG_OWNDATA) {
   2340 					if (node->sysctl_data != NULL) {
   2341 						free(node->sysctl_data,
   2342 						     M_SYSCTLDATA);
   2343 						node->sysctl_data = NULL;
   2344 					}
   2345 				}
   2346 				if (SYSCTL_FLAGS(node->sysctl_flags) &
   2347 				    CTLFLAG_OWNDESC) {
   2348 					if (node->sysctl_desc != NULL) {
   2349 						/*XXXUNCONST*/
   2350 						free(__UNCONST(node->sysctl_desc),
   2351 						     M_SYSCTLDATA);
   2352 						node->sysctl_desc = NULL;
   2353 					}
   2354 				}
   2355 				node++;
   2356 			}
   2357 			if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
   2358 				pnode = node;
   2359 				node = node->sysctl_child;
   2360 			} else
   2361 				break;
   2362 		}
   2363 		if (pnode->sysctl_child != NULL)
   2364 			free(pnode->sysctl_child, M_SYSCTLNODE);
   2365 		pnode->sysctl_clen = 0;
   2366 		pnode->sysctl_csize = 0;
   2367 		pnode->sysctl_child = NULL;
   2368 		node = pnode;
   2369 		pnode = node->sysctl_parent;
   2370 	} while (pnode != NULL && node != rnode);
   2371 
   2372 	rw_exit(&sysctl_treelock);
   2373 }
   2374 
   2375 void
   2376 sysctl_log_print(const struct sysctllog *slog)
   2377 {
   2378 	int i, len;
   2379 
   2380 	printf("root %p left %d size %d content", (const void *)slog->log_root,
   2381 	    slog->log_left, slog->log_size);
   2382 
   2383 	for (len = 0, i = slog->log_left; i < slog->log_size; i++) {
   2384 		switch (len) {
   2385 		case 0:
   2386 			len = -1;
   2387 			printf(" version %d", slog->log_num[i]);
   2388 			break;
   2389 		case -1:
   2390 			len = -2;
   2391 			printf(" type %d", slog->log_num[i]);
   2392 			break;
   2393 		case -2:
   2394 			len =  slog->log_num[i];
   2395 			printf(" len %d:", slog->log_num[i]);
   2396 			if (len <= 0)
   2397 				len = -1;
   2398 			break;
   2399 		default:
   2400 			len--;
   2401 			printf(" %d", slog->log_num[i]);
   2402 			break;
   2403 		}
   2404 	}
   2405 	printf(" end\n");
   2406 }
   2407 
   2408 int
   2409 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node)
   2410 {
   2411 	const int size0 = 16;
   2412 	int name[CTL_MAXNAME], namelen, i;
   2413 	const struct sysctlnode *pnode;
   2414 	struct sysctllog *log;
   2415 
   2416 	if (node->sysctl_flags & CTLFLAG_PERMANENT)
   2417 		return (0);
   2418 
   2419 	if (logp == NULL)
   2420 		return (0);
   2421 
   2422 	if (*logp == NULL) {
   2423 		log = malloc(sizeof(struct sysctllog),
   2424 		       M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2425 		if (log == NULL) {
   2426 			/* XXX print error message? */
   2427 			return (-1);
   2428 		}
   2429 		log->log_num = malloc(size0 * sizeof(int),
   2430 		       M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2431 		if (log->log_num == NULL) {
   2432 			/* XXX print error message? */
   2433 			free(log, M_SYSCTLDATA);
   2434 			return (-1);
   2435 		}
   2436 		memset(log->log_num, 0, size0 * sizeof(int));
   2437 		log->log_root = NULL;
   2438 		log->log_size = size0;
   2439 		log->log_left = size0;
   2440 		*logp = log;
   2441 	} else
   2442 		log = *logp;
   2443 
   2444 	/*
   2445 	 * check that the root is proper.  it's okay to record the
   2446 	 * address of the root of a tree.  it's the only thing that's
   2447 	 * guaranteed not to shift around as nodes come and go.
   2448 	 */
   2449 	if (log->log_root == NULL)
   2450 		log->log_root = sysctl_rootof(node);
   2451 	else if (log->log_root != sysctl_rootof(node)) {
   2452 		printf("sysctl: log %p root mismatch (%p)\n",
   2453 		       log->log_root, sysctl_rootof(node));
   2454 		return (-1);
   2455 	}
   2456 
   2457 	/*
   2458 	 * we will copy out name in reverse order
   2459 	 */
   2460 	for (pnode = node, namelen = 0;
   2461 	     pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
   2462 	     pnode = pnode->sysctl_parent)
   2463 		name[namelen++] = pnode->sysctl_num;
   2464 
   2465 	/*
   2466 	 * do we have space?
   2467 	 */
   2468 	if (log->log_left < (namelen + 3))
   2469 		sysctl_log_realloc(log);
   2470 	if (log->log_left < (namelen + 3))
   2471 		return (-1);
   2472 
   2473 	/*
   2474 	 * stuff name in, then namelen, then node type, and finally,
   2475 	 * the version for non-node nodes.
   2476 	 */
   2477 	for (i = 0; i < namelen; i++)
   2478 		log->log_num[--log->log_left] = name[i];
   2479 	log->log_num[--log->log_left] = namelen;
   2480 	log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
   2481 	if (log->log_num[log->log_left] != CTLTYPE_NODE)
   2482 		log->log_num[--log->log_left] = node->sysctl_ver;
   2483 	else
   2484 		log->log_num[--log->log_left] = 0;
   2485 
   2486 	return (0);
   2487 }
   2488 
   2489 void
   2490 sysctl_teardown(struct sysctllog **logp)
   2491 {
   2492 	const struct sysctlnode *rnode;
   2493 	struct sysctlnode node;
   2494 	struct sysctllog *log;
   2495 	uint namelen;
   2496 	int *name, t, v, error, ni;
   2497 	size_t sz;
   2498 
   2499 	if (logp == NULL || *logp == NULL)
   2500 		return;
   2501 	log = *logp;
   2502 
   2503 	rw_enter(&sysctl_treelock, RW_WRITER);
   2504 	memset(&node, 0, sizeof(node));
   2505 
   2506 	while (log->log_left < log->log_size) {
   2507 		KASSERT((log->log_left + 3 < log->log_size) &&
   2508 			(log->log_left + log->log_num[log->log_left + 2] <=
   2509 			 log->log_size));
   2510 		v = log->log_num[log->log_left++];
   2511 		t = log->log_num[log->log_left++];
   2512 		namelen = log->log_num[log->log_left++];
   2513 		name = &log->log_num[log->log_left];
   2514 
   2515 		node.sysctl_num = name[namelen - 1];
   2516 		node.sysctl_flags = SYSCTL_VERSION|t;
   2517 		node.sysctl_ver = v;
   2518 
   2519 		rnode = log->log_root;
   2520 		error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
   2521 		if (error == 0) {
   2522 			name[namelen - 1] = CTL_DESTROY;
   2523 			rnode = rnode->sysctl_parent;
   2524 			sz = 0;
   2525 			(void)sysctl_destroy(&name[namelen - 1], 1, NULL,
   2526 					     &sz, &node, sizeof(node),
   2527 					     &name[0], NULL, rnode);
   2528 		}
   2529 
   2530 		log->log_left += namelen;
   2531 	}
   2532 
   2533 	KASSERT(log->log_size == log->log_left);
   2534 	free(log->log_num, M_SYSCTLDATA);
   2535 	free(log, M_SYSCTLDATA);
   2536 	*logp = NULL;
   2537 
   2538 	rw_exit(&sysctl_treelock);
   2539 }
   2540 
   2541 /*
   2542  * ********************************************************************
   2543  * old_sysctl -- A routine to bridge old-style internal calls to the
   2544  * new infrastructure.
   2545  * ********************************************************************
   2546  */
   2547 int
   2548 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
   2549 	   void *newp, size_t newlen, struct lwp *l)
   2550 {
   2551 	int error;
   2552 	size_t oldlen = 0;
   2553 	size_t savelen;
   2554 
   2555 	if (oldlenp) {
   2556 		oldlen = *oldlenp;
   2557 	}
   2558 	savelen = oldlen;
   2559 
   2560 	sysctl_lock(newp != NULL);
   2561 	error = sysctl_dispatch(name, namelen, oldp, &oldlen,
   2562 				newp, newlen, name, l, NULL);
   2563 	sysctl_unlock();
   2564 	if (error == 0 && oldp != NULL && savelen < oldlen)
   2565 		error = ENOMEM;
   2566 	if (oldlenp) {
   2567 		*oldlenp = oldlen;
   2568 	}
   2569 
   2570 	return (error);
   2571 }
   2572 
   2573 /*
   2574  * ********************************************************************
   2575  * Section 4: Generic helper routines
   2576  * ********************************************************************
   2577  * "helper" routines that can do more finely grained access control,
   2578  * construct structures from disparate information, create the
   2579  * appearance of more nodes and sub-trees, etc.  for example, if
   2580  * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
   2581  * with a dynamically created list of nodes that represented the
   2582  * currently running processes at that instant.
   2583  * ********************************************************************
   2584  */
   2585 
   2586 /*
   2587  * first, a few generic helpers that provide:
   2588  *
   2589  * sysctl_needfunc()		a readonly interface that emits a warning
   2590  * sysctl_notavail()		returns EOPNOTSUPP (generic error)
   2591  * sysctl_null()		an empty return buffer with no error
   2592  */
   2593 int
   2594 sysctl_needfunc(SYSCTLFN_ARGS)
   2595 {
   2596 	int error;
   2597 
   2598 	printf("!!SYSCTL_NEEDFUNC!!\n");
   2599 
   2600 	if (newp != NULL || namelen != 0)
   2601 		return (EOPNOTSUPP);
   2602 
   2603 	error = 0;
   2604 	if (oldp != NULL)
   2605 		error = sysctl_copyout(l, rnode->sysctl_data, oldp,
   2606 				       MIN(rnode->sysctl_size, *oldlenp));
   2607 	*oldlenp = rnode->sysctl_size;
   2608 
   2609 	return (error);
   2610 }
   2611 
   2612 int
   2613 sysctl_notavail(SYSCTLFN_ARGS)
   2614 {
   2615 
   2616 	if (namelen == 1 && name[0] == CTL_QUERY)
   2617 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2618 
   2619 	return (EOPNOTSUPP);
   2620 }
   2621 
   2622 int
   2623 sysctl_null(SYSCTLFN_ARGS)
   2624 {
   2625 
   2626 	*oldlenp = 0;
   2627 
   2628 	return (0);
   2629 }
   2630 
   2631 u_int
   2632 sysctl_map_flags(const u_int *map, u_int word)
   2633 {
   2634 	u_int rv;
   2635 
   2636 	for (rv = 0; *map != 0; map += 2)
   2637 		if ((word & map[0]) != 0)
   2638 			rv |= map[1];
   2639 
   2640 	return rv;
   2641 }
   2642 
   2643 /*
   2644  * ********************************************************************
   2645  * Section 5: The machinery that makes it all go
   2646  * ********************************************************************
   2647  * Memory "manglement" routines.  Not much to this, eh?
   2648  * ********************************************************************
   2649  */
   2650 static int
   2651 sysctl_alloc(struct sysctlnode *p, int x)
   2652 {
   2653 	int i;
   2654 	struct sysctlnode *n;
   2655 
   2656 	assert(p->sysctl_child == NULL);
   2657 
   2658 	if (x == 1)
   2659 		n = malloc(sizeof(struct sysctlnode),
   2660 		       M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
   2661 	else
   2662 		n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
   2663 		       M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
   2664 	if (n == NULL)
   2665 		return (ENOMEM);
   2666 
   2667 	if (x == 1) {
   2668 		memset(n, 0, sizeof(struct sysctlnode));
   2669 		p->sysctl_csize = 1;
   2670 	} else {
   2671 		memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
   2672 		p->sysctl_csize = SYSCTL_DEFSIZE;
   2673 	}
   2674 	p->sysctl_clen = 0;
   2675 
   2676 	for (i = 0; i < p->sysctl_csize; i++)
   2677 		n[i].sysctl_parent = p;
   2678 
   2679 	p->sysctl_child = n;
   2680 	return (0);
   2681 }
   2682 
   2683 static int
   2684 sysctl_realloc(struct sysctlnode *p)
   2685 {
   2686 	int i, j, olen;
   2687 	struct sysctlnode *n;
   2688 
   2689 	assert(p->sysctl_csize == p->sysctl_clen);
   2690 
   2691 	/*
   2692 	 * how many do we have...how many should we make?
   2693 	 */
   2694 	olen = p->sysctl_clen;
   2695 	n = malloc(2 * olen * sizeof(struct sysctlnode), M_SYSCTLNODE,
   2696 		   M_WAITOK|M_CANFAIL);
   2697 	if (n == NULL)
   2698 		return (ENOMEM);
   2699 
   2700 	/*
   2701 	 * move old children over...initialize new children
   2702 	 */
   2703 	memcpy(n, p->sysctl_child, olen * sizeof(struct sysctlnode));
   2704 	memset(&n[olen], 0, olen * sizeof(struct sysctlnode));
   2705 	p->sysctl_csize = 2 * olen;
   2706 
   2707 	/*
   2708 	 * reattach moved (and new) children to parent; if a moved
   2709 	 * child node has children, reattach the parent pointers of
   2710 	 * grandchildren
   2711 	 */
   2712         for (i = 0; i < p->sysctl_csize; i++) {
   2713                 n[i].sysctl_parent = p;
   2714 		if (n[i].sysctl_child != NULL) {
   2715 			for (j = 0; j < n[i].sysctl_csize; j++)
   2716 				n[i].sysctl_child[j].sysctl_parent = &n[i];
   2717 		}
   2718 	}
   2719 
   2720 	/*
   2721 	 * get out with the old and in with the new
   2722 	 */
   2723 	free(p->sysctl_child, M_SYSCTLNODE);
   2724 	p->sysctl_child = n;
   2725 
   2726 	return (0);
   2727 }
   2728 
   2729 static int
   2730 sysctl_log_realloc(struct sysctllog *log)
   2731 {
   2732 	int *n, s, d;
   2733 
   2734 	s = log->log_size * 2;
   2735 	d = log->log_size;
   2736 
   2737 	n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2738 	if (n == NULL)
   2739 		return (-1);
   2740 
   2741 	memset(n, 0, s * sizeof(int));
   2742 	memcpy(&n[d], log->log_num, d * sizeof(int));
   2743 	free(log->log_num, M_SYSCTLDATA);
   2744 	log->log_num = n;
   2745 	if (d)
   2746 		log->log_left += d;
   2747 	else
   2748 		log->log_left = s;
   2749 	log->log_size = s;
   2750 
   2751 	return (0);
   2752 }
   2753 
   2754 /*
   2755  * ********************************************************************
   2756  * Section 6: Conversion between API versions wrt the sysctlnode
   2757  * ********************************************************************
   2758  */
   2759 static int
   2760 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
   2761 	      struct sysctlnode *node)
   2762 {
   2763 	int error, flags;
   2764 
   2765 	if (i == NULL || sz < sizeof(flags))
   2766 		return (EINVAL);
   2767 
   2768 	error = sysctl_copyin(l, i, &flags, sizeof(flags));
   2769 	if (error)
   2770 		return (error);
   2771 
   2772 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
   2773 #error sysctl_cvt_in: no support for SYSCTL_VERSION
   2774 #endif /*  (SYSCTL_VERSION != SYSCTL_VERS_1) */
   2775 
   2776 	if (sz == sizeof(*node) &&
   2777 	    SYSCTL_VERS(flags) == SYSCTL_VERSION) {
   2778 		error = sysctl_copyin(l, i, node, sizeof(*node));
   2779 		if (error)
   2780 			return (error);
   2781 		*vp = SYSCTL_VERSION;
   2782 		return (0);
   2783 	}
   2784 
   2785 	return (EINVAL);
   2786 }
   2787 
   2788 static int
   2789 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
   2790 	       void *ovp, size_t left, size_t *szp)
   2791 {
   2792 	size_t sz = sizeof(*i);
   2793 	const void *src = i;
   2794 	int error;
   2795 
   2796 	switch (v) {
   2797 	case SYSCTL_VERS_0:
   2798 		return (EINVAL);
   2799 
   2800 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
   2801 #error sysctl_cvt_out: no support for SYSCTL_VERSION
   2802 #endif /*  (SYSCTL_VERSION != SYSCTL_VERS_1) */
   2803 
   2804 	case SYSCTL_VERSION:
   2805 		/* nothing more to do here */
   2806 		break;
   2807 	}
   2808 
   2809 	if (ovp != NULL && left >= sz) {
   2810 		error = sysctl_copyout(l, src, ovp, sz);
   2811 		if (error)
   2812 			return (error);
   2813 	}
   2814 
   2815 	if (szp != NULL)
   2816 		*szp = sz;
   2817 
   2818 	return (0);
   2819 }
   2820