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