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