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