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