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