xref: /freebsd/sys/kern/kern_sysctl.c (revision 898496ee09ed2b7d25f6807edc4515628196ec0a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Karels at Berkeley Software Design, Inc.
9  *
10  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
11  * project, to make these variables more userfriendly.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
38  */
39 
40 #include <sys/cdefs.h>
41 #include "opt_capsicum.h"
42 #include "opt_ddb.h"
43 #include "opt_ktrace.h"
44 #include "opt_sysctl.h"
45 
46 #include <sys/param.h>
47 #include <sys/fail.h>
48 #include <sys/systm.h>
49 #include <sys/capsicum.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/jail.h>
57 #include <sys/kdb.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/rmlock.h>
61 #include <sys/sbuf.h>
62 #include <sys/sx.h>
63 #include <sys/sysproto.h>
64 #include <sys/uio.h>
65 #ifdef KTRACE
66 #include <sys/ktrace.h>
67 #endif
68 
69 #ifdef DDB
70 #include <ddb/ddb.h>
71 #include <ddb/db_lex.h>
72 #endif
73 
74 #include <net/vnet.h>
75 
76 #include <security/mac/mac_framework.h>
77 
78 #include <vm/vm.h>
79 #include <vm/vm_extern.h>
80 
81 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
82 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
83 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
84 
85 RB_GENERATE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid);
86 
87 /*
88  * The sysctllock protects the MIB tree.  It also protects sysctl
89  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
90  * sysctl_unregister_oid() routines require the sysctllock to already
91  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
92  * provided for the few places in the kernel which need to use that
93  * API rather than using the dynamic API.  Use of the dynamic API is
94  * strongly encouraged for most code.
95  *
96  * The sysctlmemlock is used to limit the amount of user memory wired for
97  * sysctl requests.  This is implemented by serializing any userland
98  * sysctl requests larger than a single page via an exclusive lock.
99  *
100  * The sysctlstringlock is used to protect concurrent access to writable
101  * string nodes in sysctl_handle_string().
102  */
103 static struct rmlock sysctllock;
104 static struct sx __exclusive_cache_line sysctlmemlock;
105 static struct sx sysctlstringlock;
106 
107 #define	SYSCTL_WLOCK()		rm_wlock(&sysctllock)
108 #define	SYSCTL_WUNLOCK()	rm_wunlock(&sysctllock)
109 #define	SYSCTL_RLOCK(tracker)	rm_rlock(&sysctllock, (tracker))
110 #define	SYSCTL_RUNLOCK(tracker)	rm_runlock(&sysctllock, (tracker))
111 #define	SYSCTL_WLOCKED()	rm_wowned(&sysctllock)
112 #define	SYSCTL_ASSERT_LOCKED()	rm_assert(&sysctllock, RA_LOCKED)
113 #define	SYSCTL_ASSERT_WLOCKED()	rm_assert(&sysctllock, RA_WLOCKED)
114 #define	SYSCTL_ASSERT_RLOCKED()	rm_assert(&sysctllock, RA_RLOCKED)
115 #define	SYSCTL_INIT()		rm_init_flags(&sysctllock, "sysctl lock", \
116 				    RM_SLEEPABLE)
117 #define	SYSCTL_SLEEP(ch, wmesg, timo)					\
118 				rm_sleep(ch, &sysctllock, 0, wmesg, timo)
119 
120 static int sysctl_root(SYSCTL_HANDLER_ARGS);
121 
122 /* Root list */
123 struct sysctl_oid_list sysctl__children = RB_INITIALIZER(&sysctl__children);
124 
125 static char*	sysctl_escape_name(const char*);
126 static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
127 		    int recurse);
128 static int	sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
129 static int	sysctl_new_kernel(struct sysctl_req *, void *, size_t);
130 
131 static struct sysctl_oid *
132 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
133 {
134 	struct sysctl_oid *oidp;
135 
136 	SYSCTL_ASSERT_LOCKED();
137 	SYSCTL_FOREACH(oidp, list) {
138 		if (strcmp(oidp->oid_name, name) == 0) {
139 			return (oidp);
140 		}
141 	}
142 	return (NULL);
143 }
144 
145 /*
146  * Initialization of the MIB tree.
147  *
148  * Order by number in each list.
149  */
150 void
151 sysctl_wlock(void)
152 {
153 
154 	SYSCTL_WLOCK();
155 }
156 
157 void
158 sysctl_wunlock(void)
159 {
160 
161 	SYSCTL_WUNLOCK();
162 }
163 
164 static int
165 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
166     struct sysctl_req *req, struct rm_priotracker *tracker)
167 {
168 	int error;
169 
170 	if (oid->oid_kind & CTLFLAG_DYN)
171 		atomic_add_int(&oid->oid_running, 1);
172 
173 	if (tracker != NULL)
174 		SYSCTL_RUNLOCK(tracker);
175 	else
176 		SYSCTL_WUNLOCK();
177 
178 	/*
179 	 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
180 	 * untill we're ready to remove all traces of Giant from sysctl(9).
181 	 */
182 	if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
183 	    (!(oid->oid_kind & CTLFLAG_MPSAFE)))
184 		mtx_lock(&Giant);
185 	error = oid->oid_handler(oid, arg1, arg2, req);
186 	if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
187 	    (!(oid->oid_kind & CTLFLAG_MPSAFE)))
188 		mtx_unlock(&Giant);
189 
190 	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
191 
192 	if (tracker != NULL)
193 		SYSCTL_RLOCK(tracker);
194 	else
195 		SYSCTL_WLOCK();
196 
197 	if (oid->oid_kind & CTLFLAG_DYN) {
198 		if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
199 		    (oid->oid_kind & CTLFLAG_DYING) != 0)
200 			wakeup(&oid->oid_running);
201 	}
202 
203 	return (error);
204 }
205 
206 static void
207 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
208 {
209 	struct sysctl_req req;
210 	struct sysctl_oid *curr;
211 	char *penv = NULL;
212 	char path[96];
213 	ssize_t rem = sizeof(path);
214 	ssize_t len;
215 	uint8_t data[512] __aligned(sizeof(uint64_t));
216 	int size;
217 	int error;
218 
219 	path[--rem] = 0;
220 
221 	for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
222 		len = strlen(curr->oid_name);
223 		rem -= len;
224 		if (curr != oidp)
225 			rem -= 1;
226 		if (rem < 0) {
227 			printf("OID path exceeds %d bytes\n", (int)sizeof(path));
228 			return;
229 		}
230 		memcpy(path + rem, curr->oid_name, len);
231 		if (curr != oidp)
232 			path[rem + len] = '.';
233 	}
234 
235 	memset(&req, 0, sizeof(req));
236 
237 	req.td = curthread;
238 	req.oldfunc = sysctl_old_kernel;
239 	req.newfunc = sysctl_new_kernel;
240 	req.lock = REQ_UNWIRED;
241 
242 	switch (oidp->oid_kind & CTLTYPE) {
243 	case CTLTYPE_INT:
244 		if (getenv_array(path + rem, data, sizeof(data), &size,
245 		    sizeof(int), GETENV_SIGNED) == 0)
246 			return;
247 		req.newlen = size;
248 		req.newptr = data;
249 		break;
250 	case CTLTYPE_UINT:
251 		if (getenv_array(path + rem, data, sizeof(data), &size,
252 		    sizeof(int), GETENV_UNSIGNED) == 0)
253 			return;
254 		req.newlen = size;
255 		req.newptr = data;
256 		break;
257 	case CTLTYPE_LONG:
258 		if (getenv_array(path + rem, data, sizeof(data), &size,
259 		    sizeof(long), GETENV_SIGNED) == 0)
260 			return;
261 		req.newlen = size;
262 		req.newptr = data;
263 		break;
264 	case CTLTYPE_ULONG:
265 		if (getenv_array(path + rem, data, sizeof(data), &size,
266 		    sizeof(long), GETENV_UNSIGNED) == 0)
267 			return;
268 		req.newlen = size;
269 		req.newptr = data;
270 		break;
271 	case CTLTYPE_S8:
272 		if (getenv_array(path + rem, data, sizeof(data), &size,
273 		    sizeof(int8_t), GETENV_SIGNED) == 0)
274 			return;
275 		req.newlen = size;
276 		req.newptr = data;
277 		break;
278 	case CTLTYPE_S16:
279 		if (getenv_array(path + rem, data, sizeof(data), &size,
280 		    sizeof(int16_t), GETENV_SIGNED) == 0)
281 			return;
282 		req.newlen = size;
283 		req.newptr = data;
284 		break;
285 	case CTLTYPE_S32:
286 		if (getenv_array(path + rem, data, sizeof(data), &size,
287 		    sizeof(int32_t), GETENV_SIGNED) == 0)
288 			return;
289 		req.newlen = size;
290 		req.newptr = data;
291 		break;
292 	case CTLTYPE_S64:
293 		if (getenv_array(path + rem, data, sizeof(data), &size,
294 		    sizeof(int64_t), GETENV_SIGNED) == 0)
295 			return;
296 		req.newlen = size;
297 		req.newptr = data;
298 		break;
299 	case CTLTYPE_U8:
300 		if (getenv_array(path + rem, data, sizeof(data), &size,
301 		    sizeof(uint8_t), GETENV_UNSIGNED) == 0)
302 			return;
303 		req.newlen = size;
304 		req.newptr = data;
305 		break;
306 	case CTLTYPE_U16:
307 		if (getenv_array(path + rem, data, sizeof(data), &size,
308 		    sizeof(uint16_t), GETENV_UNSIGNED) == 0)
309 			return;
310 		req.newlen = size;
311 		req.newptr = data;
312 		break;
313 	case CTLTYPE_U32:
314 		if (getenv_array(path + rem, data, sizeof(data), &size,
315 		    sizeof(uint32_t), GETENV_UNSIGNED) == 0)
316 			return;
317 		req.newlen = size;
318 		req.newptr = data;
319 		break;
320 	case CTLTYPE_U64:
321 		if (getenv_array(path + rem, data, sizeof(data), &size,
322 		    sizeof(uint64_t), GETENV_UNSIGNED) == 0)
323 			return;
324 		req.newlen = size;
325 		req.newptr = data;
326 		break;
327 	case CTLTYPE_STRING:
328 		penv = kern_getenv(path + rem);
329 		if (penv == NULL)
330 			return;
331 		req.newlen = strlen(penv);
332 		req.newptr = penv;
333 		break;
334 	default:
335 		return;
336 	}
337 	error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
338 	    oidp->oid_arg2, &req, NULL);
339 	if (error != 0)
340 		printf("Setting sysctl %s failed: %d\n", path + rem, error);
341 	if (penv != NULL)
342 		freeenv(penv);
343 }
344 
345 /*
346  * Locate the path to a given oid.  Returns the length of the resulting path,
347  * or -1 if the oid was not found.  nodes must have room for CTL_MAXNAME
348  * elements.
349  */
350 static int
351 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
352 {
353 	int indx;
354 
355 	SYSCTL_ASSERT_LOCKED();
356 	indx = 0;
357 	/*
358 	 * Do a depth-first search of the oid tree, looking for 'needle'. Start
359 	 * with the first child of the root.
360 	 */
361 	nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children);
362 	for (;;) {
363 		if (nodes[indx] == needle)
364 			return (indx + 1);
365 
366 		if (nodes[indx] == NULL) {
367 			/* Node has no more siblings, so back up to parent. */
368 			if (indx-- == 0) {
369 				/* Retreat to root, so give up. */
370 				break;
371 			}
372 		} else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
373 			/* Node has children. */
374 			if (++indx == CTL_MAXNAME) {
375 				/* Max search depth reached, so give up. */
376 				break;
377 			}
378 			/* Start with the first child. */
379 			nodes[indx] = RB_MIN(sysctl_oid_list,
380 			    &nodes[indx - 1]->oid_children);
381 			continue;
382 		}
383 		/* Consider next sibling. */
384 		nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]);
385 	}
386 	return (-1);
387 }
388 
389 static void
390 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
391 {
392 	struct sysctl_oid *nodes[CTL_MAXNAME];
393 	char buf[128];
394 	struct sbuf sb;
395 	int rc, i;
396 
397 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
398 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
399 
400 	sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
401 
402 	rc = sysctl_search_oid(nodes, leaf);
403 	if (rc > 0) {
404 		for (i = 0; i < rc; i++)
405 			sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
406 			    i != (rc - 1), ".");
407 	} else {
408 		sbuf_printf(&sb, "%s", leaf->oid_name);
409 	}
410 	sbuf_printf(&sb, ")!\n");
411 
412 	(void)sbuf_finish(&sb);
413 }
414 
415 #ifdef SYSCTL_DEBUG
416 static int
417 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
418 {
419 	struct rm_priotracker tracker;
420 
421 	SYSCTL_RLOCK(&tracker);
422 	sysctl_warn_reuse(__func__, oidp);
423 	SYSCTL_RUNLOCK(&tracker);
424 	return (0);
425 }
426 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
427     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
428     "");
429 #endif
430 
431 void
432 sysctl_register_oid(struct sysctl_oid *oidp)
433 {
434 	struct sysctl_oid_list *parent = oidp->oid_parent;
435 	struct sysctl_oid *p, key;
436 	int oid_number;
437 	int timeout = 2;
438 
439 	/*
440 	 * First check if another oid with the same name already
441 	 * exists in the parent's list.
442 	 */
443 	SYSCTL_ASSERT_WLOCKED();
444 	p = sysctl_find_oidname(oidp->oid_name, parent);
445 	if (p != NULL) {
446 		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
447 			p->oid_refcnt++;
448 			return;
449 		} else {
450 			sysctl_warn_reuse(__func__, p);
451 			return;
452 		}
453 	}
454 	/* get current OID number */
455 	oid_number = oidp->oid_number;
456 
457 #if (OID_AUTO >= 0)
458 #error "OID_AUTO is expected to be a negative value"
459 #endif
460 	/*
461 	 * Any negative OID number qualifies as OID_AUTO. Valid OID
462 	 * numbers should always be positive.
463 	 *
464 	 * NOTE: DO NOT change the starting value here, change it in
465 	 * <sys/sysctl.h>, and make sure it is at least 256 to
466 	 * accommodate e.g. net.inet.raw as a static sysctl node.
467 	 */
468 	if (oid_number < 0) {
469 		static int newoid;
470 
471 		/*
472 		 * By decrementing the next OID number we spend less
473 		 * time inserting the OIDs into a sorted list.
474 		 */
475 		if (--newoid < CTL_AUTO_START)
476 			newoid = 0x7fffffff;
477 
478 		oid_number = newoid;
479 	}
480 
481 	/*
482 	 * Insert the OID into the parent's list sorted by OID number.
483 	 */
484 	key.oid_number = oid_number;
485 	p = RB_NFIND(sysctl_oid_list, parent, &key);
486 	while (p != NULL && oid_number == p->oid_number) {
487 		/* get the next valid OID number */
488 		if (oid_number < CTL_AUTO_START ||
489 		    oid_number == 0x7fffffff) {
490 			/* wraparound - restart */
491 			oid_number = CTL_AUTO_START;
492 			/* don't loop forever */
493 			if (!timeout--)
494 				panic("sysctl: Out of OID numbers\n");
495 			key.oid_number = oid_number;
496 			p = RB_NFIND(sysctl_oid_list, parent, &key);
497 			continue;
498 		}
499 		p = RB_NEXT(sysctl_oid_list, NULL, p);
500 		oid_number++;
501 	}
502 	/* check for non-auto OID number collision */
503 	if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
504 	    oid_number >= CTL_AUTO_START) {
505 		printf("sysctl: OID number(%d) is already in use for '%s'\n",
506 		    oidp->oid_number, oidp->oid_name);
507 	}
508 	/* update the OID number, if any */
509 	oidp->oid_number = oid_number;
510 	RB_INSERT(sysctl_oid_list, parent, oidp);
511 
512 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
513 	    (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
514 	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
515 		/* only fetch value once */
516 		oidp->oid_kind |= CTLFLAG_NOFETCH;
517 		/* try to fetch value from kernel environment */
518 		sysctl_load_tunable_by_oid_locked(oidp);
519 	}
520 }
521 
522 void
523 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
524 {
525 
526 	/*
527 	 * Mark the leaf as dormant if it's not to be immediately enabled.
528 	 * We do not disable nodes as they can be shared between modules
529 	 * and it is always safe to access a node.
530 	 */
531 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
532 	    ("internal flag is set in oid_kind"));
533 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
534 		oidp->oid_kind |= CTLFLAG_DORMANT;
535 	sysctl_register_oid(oidp);
536 }
537 
538 void
539 sysctl_enable_oid(struct sysctl_oid *oidp)
540 {
541 
542 	SYSCTL_ASSERT_WLOCKED();
543 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
544 		KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
545 		    ("sysctl node is marked as dormant"));
546 		return;
547 	}
548 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
549 	    ("enabling already enabled sysctl oid"));
550 	oidp->oid_kind &= ~CTLFLAG_DORMANT;
551 }
552 
553 void
554 sysctl_unregister_oid(struct sysctl_oid *oidp)
555 {
556 	int error;
557 
558 	SYSCTL_ASSERT_WLOCKED();
559 	if (oidp->oid_number == OID_AUTO) {
560 		error = EINVAL;
561 	} else {
562 		error = ENOENT;
563 		if (RB_REMOVE(sysctl_oid_list, oidp->oid_parent, oidp))
564 			error = 0;
565 	}
566 
567 	/*
568 	 * This can happen when a module fails to register and is
569 	 * being unloaded afterwards.  It should not be a panic()
570 	 * for normal use.
571 	 */
572 	if (error) {
573 		printf("%s: failed(%d) to unregister sysctl(%s)\n",
574 		    __func__, error, oidp->oid_name);
575 	}
576 }
577 
578 /* Initialize a new context to keep track of dynamically added sysctls. */
579 int
580 sysctl_ctx_init(struct sysctl_ctx_list *c)
581 {
582 
583 	if (c == NULL) {
584 		return (EINVAL);
585 	}
586 
587 	/*
588 	 * No locking here, the caller is responsible for not adding
589 	 * new nodes to a context until after this function has
590 	 * returned.
591 	 */
592 	TAILQ_INIT(c);
593 	return (0);
594 }
595 
596 /* Free the context, and destroy all dynamic oids registered in this context */
597 int
598 sysctl_ctx_free(struct sysctl_ctx_list *clist)
599 {
600 	struct sysctl_ctx_entry *e, *e1;
601 	int error;
602 
603 	error = 0;
604 	/*
605 	 * First perform a "dry run" to check if it's ok to remove oids.
606 	 * XXX FIXME
607 	 * XXX This algorithm is a hack. But I don't know any
608 	 * XXX better solution for now...
609 	 */
610 	SYSCTL_WLOCK();
611 	TAILQ_FOREACH(e, clist, link) {
612 		error = sysctl_remove_oid_locked(e->entry, 0, 0);
613 		if (error)
614 			break;
615 	}
616 	/*
617 	 * Restore deregistered entries, either from the end,
618 	 * or from the place where error occurred.
619 	 * e contains the entry that was not unregistered
620 	 */
621 	if (error)
622 		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
623 	else
624 		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
625 	while (e1 != NULL) {
626 		sysctl_register_oid(e1->entry);
627 		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
628 	}
629 	if (error) {
630 		SYSCTL_WUNLOCK();
631 		return(EBUSY);
632 	}
633 	/* Now really delete the entries */
634 	e = TAILQ_FIRST(clist);
635 	while (e != NULL) {
636 		e1 = TAILQ_NEXT(e, link);
637 		error = sysctl_remove_oid_locked(e->entry, 1, 0);
638 		if (error)
639 			panic("sysctl_remove_oid: corrupt tree, entry: %s",
640 			    e->entry->oid_name);
641 		free(e, M_SYSCTLOID);
642 		e = e1;
643 	}
644 	SYSCTL_WUNLOCK();
645 	return (error);
646 }
647 
648 /* Add an entry to the context */
649 struct sysctl_ctx_entry *
650 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
651 {
652 	struct sysctl_ctx_entry *e;
653 
654 	SYSCTL_ASSERT_WLOCKED();
655 	if (clist == NULL || oidp == NULL)
656 		return(NULL);
657 	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
658 	e->entry = oidp;
659 	TAILQ_INSERT_HEAD(clist, e, link);
660 	return (e);
661 }
662 
663 /* Find an entry in the context */
664 struct sysctl_ctx_entry *
665 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
666 {
667 	struct sysctl_ctx_entry *e;
668 
669 	SYSCTL_ASSERT_WLOCKED();
670 	if (clist == NULL || oidp == NULL)
671 		return(NULL);
672 	TAILQ_FOREACH(e, clist, link) {
673 		if (e->entry == oidp)
674 			return(e);
675 	}
676 	return (e);
677 }
678 
679 /*
680  * Delete an entry from the context.
681  * NOTE: this function doesn't free oidp! You have to remove it
682  * with sysctl_remove_oid().
683  */
684 int
685 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
686 {
687 	struct sysctl_ctx_entry *e;
688 
689 	if (clist == NULL || oidp == NULL)
690 		return (EINVAL);
691 	SYSCTL_WLOCK();
692 	e = sysctl_ctx_entry_find(clist, oidp);
693 	if (e != NULL) {
694 		TAILQ_REMOVE(clist, e, link);
695 		SYSCTL_WUNLOCK();
696 		free(e, M_SYSCTLOID);
697 		return (0);
698 	} else {
699 		SYSCTL_WUNLOCK();
700 		return (ENOENT);
701 	}
702 }
703 
704 /*
705  * Remove dynamically created sysctl trees.
706  * oidp - top of the tree to be removed
707  * del - if 0 - just deregister, otherwise free up entries as well
708  * recurse - if != 0 traverse the subtree to be deleted
709  */
710 int
711 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
712 {
713 	int error;
714 
715 	SYSCTL_WLOCK();
716 	error = sysctl_remove_oid_locked(oidp, del, recurse);
717 	SYSCTL_WUNLOCK();
718 	return (error);
719 }
720 
721 int
722 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
723     int del, int recurse)
724 {
725 	struct sysctl_oid *p;
726 	int error;
727 
728 	error = ENOENT;
729 	SYSCTL_WLOCK();
730 	p = sysctl_find_oidname(name, &parent->oid_children);
731 	if (p)
732 		error = sysctl_remove_oid_locked(p, del, recurse);
733 	SYSCTL_WUNLOCK();
734 
735 	return (error);
736 }
737 
738 /*
739  * Duplicate the provided string, escaping any illegal characters.  The result
740  * must be freed when no longer in use.
741  *
742  * The list of illegal characters is ".".
743  */
744 static char*
745 sysctl_escape_name(const char* orig)
746 {
747 	int i, s = 0, d = 0, nillegals = 0;
748 	char *new;
749 
750 	/* First count the number of illegal characters */
751 	for (i = 0; orig[i] != '\0'; i++) {
752 		if (orig[i] == '.')
753 			nillegals++;
754 	}
755 
756 	/* Allocate storage for new string */
757 	new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
758 
759 	/* Copy the name, escaping characters as we go */
760 	while (orig[s] != '\0') {
761 		if (orig[s] == '.') {
762 			/* %25 is the hexadecimal representation of '.' */
763 			new[d++] = '%';
764 			new[d++] = '2';
765 			new[d++] = '5';
766 			s++;
767 		} else {
768 			new[d++] = orig[s++];
769 		}
770 	}
771 
772 	/* Finally, nul-terminate */
773 	new[d] = '\0';
774 
775 	return (new);
776 }
777 
778 static int
779 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
780 {
781 	struct sysctl_oid *p, *tmp;
782 	int error;
783 
784 	SYSCTL_ASSERT_WLOCKED();
785 	if (oidp == NULL)
786 		return(EINVAL);
787 	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
788 		printf("Warning: can't remove non-dynamic nodes (%s)!\n",
789 		    oidp->oid_name);
790 		return (EINVAL);
791 	}
792 	/*
793 	 * WARNING: normal method to do this should be through
794 	 * sysctl_ctx_free(). Use recursing as the last resort
795 	 * method to purge your sysctl tree of leftovers...
796 	 * However, if some other code still references these nodes,
797 	 * it will panic.
798 	 */
799 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
800 		if (oidp->oid_refcnt == 1) {
801 			for(p = RB_MIN(sysctl_oid_list, &oidp->oid_children);
802 			    p != NULL; p = tmp) {
803 				if (!recurse) {
804 					printf("Warning: failed attempt to "
805 					    "remove oid %s with child %s\n",
806 					    oidp->oid_name, p->oid_name);
807 					return (ENOTEMPTY);
808 				}
809 				tmp = RB_NEXT(sysctl_oid_list,
810 				    &oidp->oid_children, p);
811 				error = sysctl_remove_oid_locked(p, del,
812 				    recurse);
813 				if (error)
814 					return (error);
815 			}
816 		}
817 	}
818 	if (oidp->oid_refcnt > 1 ) {
819 		oidp->oid_refcnt--;
820 	} else {
821 		if (oidp->oid_refcnt == 0) {
822 			printf("Warning: bad oid_refcnt=%u (%s)!\n",
823 				oidp->oid_refcnt, oidp->oid_name);
824 			return (EINVAL);
825 		}
826 		sysctl_unregister_oid(oidp);
827 		if (del) {
828 			/*
829 			 * Wait for all threads running the handler to drain.
830 			 * This preserves the previous behavior when the
831 			 * sysctl lock was held across a handler invocation,
832 			 * and is necessary for module unload correctness.
833 			 */
834 			while (oidp->oid_running > 0) {
835 				oidp->oid_kind |= CTLFLAG_DYING;
836 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
837 			}
838 			if (oidp->oid_descr)
839 				free(__DECONST(char *, oidp->oid_descr),
840 				    M_SYSCTLOID);
841 			if (oidp->oid_label)
842 				free(__DECONST(char *, oidp->oid_label),
843 				    M_SYSCTLOID);
844 			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
845 			free(oidp, M_SYSCTLOID);
846 		}
847 	}
848 	return (0);
849 }
850 /*
851  * Create new sysctls at run time.
852  * clist may point to a valid context initialized with sysctl_ctx_init().
853  */
854 struct sysctl_oid *
855 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
856 	int number, const char *name, int kind, void *arg1, intmax_t arg2,
857 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
858 	const char *label)
859 {
860 	struct sysctl_oid *oidp;
861 	char *escaped;
862 
863 	/* You have to hook up somewhere.. */
864 	if (parent == NULL)
865 		return(NULL);
866 	escaped = sysctl_escape_name(name);
867 	/* Check if the node already exists, otherwise create it */
868 	SYSCTL_WLOCK();
869 	oidp = sysctl_find_oidname(escaped, parent);
870 	if (oidp != NULL) {
871 		free(escaped, M_SYSCTLOID);
872 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
873 			oidp->oid_refcnt++;
874 			/* Update the context */
875 			if (clist != NULL)
876 				sysctl_ctx_entry_add(clist, oidp);
877 			SYSCTL_WUNLOCK();
878 			return (oidp);
879 		} else {
880 			sysctl_warn_reuse(__func__, oidp);
881 			SYSCTL_WUNLOCK();
882 			return (NULL);
883 		}
884 	}
885 	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
886 	oidp->oid_parent = parent;
887 	RB_INIT(&oidp->oid_children);
888 	oidp->oid_number = number;
889 	oidp->oid_refcnt = 1;
890 	oidp->oid_name = escaped;
891 	oidp->oid_handler = handler;
892 	oidp->oid_kind = CTLFLAG_DYN | kind;
893 	oidp->oid_arg1 = arg1;
894 	oidp->oid_arg2 = arg2;
895 	oidp->oid_fmt = fmt;
896 	if (descr != NULL)
897 		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
898 	if (label != NULL)
899 		oidp->oid_label = strdup(label, M_SYSCTLOID);
900 	/* Update the context, if used */
901 	if (clist != NULL)
902 		sysctl_ctx_entry_add(clist, oidp);
903 	/* Register this oid */
904 	sysctl_register_oid(oidp);
905 	SYSCTL_WUNLOCK();
906 	return (oidp);
907 }
908 
909 /*
910  * Rename an existing oid.
911  */
912 void
913 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
914 {
915 	char *newname;
916 	char *oldname;
917 
918 	newname = strdup(name, M_SYSCTLOID);
919 	SYSCTL_WLOCK();
920 	oldname = __DECONST(char *, oidp->oid_name);
921 	oidp->oid_name = newname;
922 	SYSCTL_WUNLOCK();
923 	free(oldname, M_SYSCTLOID);
924 }
925 
926 /*
927  * Reparent an existing oid.
928  */
929 int
930 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
931 {
932 	struct sysctl_oid *oidp;
933 
934 	SYSCTL_WLOCK();
935 	if (oid->oid_parent == parent) {
936 		SYSCTL_WUNLOCK();
937 		return (0);
938 	}
939 	oidp = sysctl_find_oidname(oid->oid_name, parent);
940 	if (oidp != NULL) {
941 		SYSCTL_WUNLOCK();
942 		return (EEXIST);
943 	}
944 	sysctl_unregister_oid(oid);
945 	oid->oid_parent = parent;
946 	oid->oid_number = OID_AUTO;
947 	sysctl_register_oid(oid);
948 	SYSCTL_WUNLOCK();
949 	return (0);
950 }
951 
952 /*
953  * Register the kernel's oids on startup.
954  */
955 SET_DECLARE(sysctl_set, struct sysctl_oid);
956 
957 static void
958 sysctl_register_all(void *arg)
959 {
960 	struct sysctl_oid **oidp;
961 
962 	sx_init(&sysctlmemlock, "sysctl mem");
963 	sx_init(&sysctlstringlock, "sysctl string handler");
964 	SYSCTL_INIT();
965 	SYSCTL_WLOCK();
966 	SET_FOREACH(oidp, sysctl_set)
967 		sysctl_register_oid(*oidp);
968 	SYSCTL_WUNLOCK();
969 }
970 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
971 
972 /*
973  * "Staff-functions"
974  *
975  * These functions implement a presently undocumented interface
976  * used by the sysctl program to walk the tree, and get the type
977  * so it can print the value.
978  * This interface is under work and consideration, and should probably
979  * be killed with a big axe by the first person who can find the time.
980  * (be aware though, that the proper interface isn't as obvious as it
981  * may seem, there are various conflicting requirements.
982  *
983  * {CTL_SYSCTL, CTL_SYSCTL_DEBUG}		printf the entire MIB-tree.
984  * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...}		return the name of the "..."
985  *						OID.
986  * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...}		return the next OID, honoring
987  *						CTLFLAG_SKIP.
988  * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID}		return the OID of the name in
989  *						"new"
990  * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...}		return the kind & format info
991  *						for the "..." OID.
992  * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...}	return the description of the
993  *						"..." OID.
994  * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...}	return the aggregation label of
995  *						the "..." OID.
996  * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...}	return the next OID, ignoring
997  *						CTLFLAG_SKIP.
998  */
999 
1000 #ifdef SYSCTL_DEBUG
1001 static void
1002 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1003 {
1004 	int k;
1005 	struct sysctl_oid *oidp;
1006 
1007 	SYSCTL_ASSERT_LOCKED();
1008 	SYSCTL_FOREACH(oidp, l) {
1009 		for (k=0; k<i; k++)
1010 			printf(" ");
1011 
1012 		printf("%d %s ", oidp->oid_number, oidp->oid_name);
1013 
1014 		printf("%c%c",
1015 			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1016 			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1017 
1018 		if (oidp->oid_handler)
1019 			printf(" *Handler");
1020 
1021 		switch (oidp->oid_kind & CTLTYPE) {
1022 			case CTLTYPE_NODE:
1023 				printf(" Node\n");
1024 				if (!oidp->oid_handler) {
1025 					sysctl_sysctl_debug_dump_node(
1026 					    SYSCTL_CHILDREN(oidp), i + 2);
1027 				}
1028 				break;
1029 			case CTLTYPE_INT:    printf(" Int\n"); break;
1030 			case CTLTYPE_UINT:   printf(" u_int\n"); break;
1031 			case CTLTYPE_LONG:   printf(" Long\n"); break;
1032 			case CTLTYPE_ULONG:  printf(" u_long\n"); break;
1033 			case CTLTYPE_STRING: printf(" String\n"); break;
1034 			case CTLTYPE_S8:     printf(" int8_t\n"); break;
1035 			case CTLTYPE_S16:    printf(" int16_t\n"); break;
1036 			case CTLTYPE_S32:    printf(" int32_t\n"); break;
1037 			case CTLTYPE_S64:    printf(" int64_t\n"); break;
1038 			case CTLTYPE_U8:     printf(" uint8_t\n"); break;
1039 			case CTLTYPE_U16:    printf(" uint16_t\n"); break;
1040 			case CTLTYPE_U32:    printf(" uint32_t\n"); break;
1041 			case CTLTYPE_U64:    printf(" uint64_t\n"); break;
1042 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1043 			default:	     printf("\n");
1044 		}
1045 	}
1046 }
1047 
1048 static int
1049 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1050 {
1051 	struct rm_priotracker tracker;
1052 	int error;
1053 
1054 	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1055 	if (error)
1056 		return (error);
1057 	SYSCTL_RLOCK(&tracker);
1058 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1059 	SYSCTL_RUNLOCK(&tracker);
1060 	return (ENOENT);
1061 }
1062 
1063 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1064     CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1065 #endif
1066 
1067 static int
1068 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1069 {
1070 	int *name = (int *) arg1;
1071 	u_int namelen = arg2;
1072 	int error;
1073 	struct sysctl_oid *oid, key;
1074 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1075 	struct rm_priotracker tracker;
1076 	char buf[10];
1077 
1078 	error = sysctl_wire_old_buffer(req, 0);
1079 	if (error)
1080 		return (error);
1081 
1082 	SYSCTL_RLOCK(&tracker);
1083 	while (namelen) {
1084 		if (!lsp) {
1085 			snprintf(buf,sizeof(buf),"%d",*name);
1086 			if (req->oldidx)
1087 				error = SYSCTL_OUT(req, ".", 1);
1088 			if (!error)
1089 				error = SYSCTL_OUT(req, buf, strlen(buf));
1090 			if (error)
1091 				goto out;
1092 			namelen--;
1093 			name++;
1094 			continue;
1095 		}
1096 		lsp2 = NULL;
1097 		key.oid_number = *name;
1098 		oid = RB_FIND(sysctl_oid_list, lsp, &key);
1099 		if (oid) {
1100 			if (req->oldidx)
1101 				error = SYSCTL_OUT(req, ".", 1);
1102 			if (!error)
1103 				error = SYSCTL_OUT(req, oid->oid_name,
1104 					strlen(oid->oid_name));
1105 			if (error)
1106 				goto out;
1107 
1108 			namelen--;
1109 			name++;
1110 
1111 			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE &&
1112 				!oid->oid_handler)
1113 				lsp2 = SYSCTL_CHILDREN(oid);
1114 		}
1115 		lsp = lsp2;
1116 	}
1117 	error = SYSCTL_OUT(req, "", 1);
1118  out:
1119 	SYSCTL_RUNLOCK(&tracker);
1120 	return (error);
1121 }
1122 
1123 /*
1124  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1125  * capability mode.
1126  */
1127 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1128     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1129 
1130 enum sysctl_iter_action {
1131 	ITER_SIBLINGS,	/* Not matched, continue iterating siblings */
1132 	ITER_CHILDREN,	/* Node has children we need to iterate over them */
1133 	ITER_FOUND,	/* Matching node was found */
1134 };
1135 
1136 /*
1137  * Tries to find the next node for @name and @namelen.
1138  *
1139  * Returns next action to take.
1140  */
1141 static enum sysctl_iter_action
1142 sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen,
1143     bool honor_skip)
1144 {
1145 
1146 	if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1147 		return (ITER_SIBLINGS);
1148 
1149 	if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1150 		return (ITER_SIBLINGS);
1151 
1152 	if (namelen == 0) {
1153 		/*
1154 		 * We have reached a node with a full name match and are
1155 		 * looking for the next oid in its children.
1156 		 *
1157 		 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1158 		 *
1159 		 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1160 		 * has a handler) and move on to the children.
1161 		 */
1162 		if (!honor_skip)
1163 			return (ITER_FOUND);
1164 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1165 			return (ITER_FOUND);
1166 		/* If node does not have an iterator, treat it as leaf */
1167 		if (oidp->oid_handler)
1168 			return (ITER_FOUND);
1169 
1170 		/* Report oid as a node to iterate */
1171 		return (ITER_CHILDREN);
1172 	}
1173 
1174 	/*
1175 	 * No match yet. Continue seeking the given name.
1176 	 *
1177 	 * We are iterating in order by oid_number, so skip oids lower
1178 	 * than the one we are looking for.
1179 	 *
1180 	 * When the current oid_number is higher than the one we seek,
1181 	 * that means we have reached the next oid in the sequence and
1182 	 * should return it.
1183 	 *
1184 	 * If the oid_number matches the name at this level then we
1185 	 * have to find a node to continue searching at the next level.
1186 	 */
1187 	if (oidp->oid_number < *name)
1188 		return (ITER_SIBLINGS);
1189 	if (oidp->oid_number > *name) {
1190 		/*
1191 		 * We have reached the next oid.
1192 		 *
1193 		 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1194 		 *
1195 		 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1196 		 * has a handler) and move on to the children.
1197 		 */
1198 		if (!honor_skip)
1199 			return (ITER_FOUND);
1200 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1201 			return (ITER_FOUND);
1202 		/* If node does not have an iterator, treat it as leaf */
1203 		if (oidp->oid_handler)
1204 			return (ITER_FOUND);
1205 		return (ITER_CHILDREN);
1206 	}
1207 
1208 	/* match at a current level */
1209 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1210 		return (ITER_SIBLINGS);
1211 	if (oidp->oid_handler)
1212 		return (ITER_SIBLINGS);
1213 
1214 	return (ITER_CHILDREN);
1215 }
1216 
1217 /*
1218  * Recursively walk the sysctl subtree at lsp until we find the given name.
1219  * Returns true and fills in next oid data in @next and @len if oid is found.
1220  */
1221 static bool
1222 sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1223     int *next, int *len, int level, bool honor_skip)
1224 {
1225 	struct sysctl_oid_list *next_lsp;
1226 	struct sysctl_oid *oidp = NULL, key;
1227 	bool success = false;
1228 	enum sysctl_iter_action action;
1229 
1230 	SYSCTL_ASSERT_LOCKED();
1231 	/*
1232 	 * Start the search at the requested oid.  But if not found, then scan
1233 	 * through all children.
1234 	 */
1235 	if (namelen > 0) {
1236 		key.oid_number = *name;
1237 		oidp = RB_FIND(sysctl_oid_list, lsp, &key);
1238 	}
1239 	if (!oidp)
1240 		oidp = RB_MIN(sysctl_oid_list, lsp);
1241 	for(; oidp != NULL; oidp = RB_NEXT(sysctl_oid_list, lsp, oidp)) {
1242 		action = sysctl_sysctl_next_node(oidp, name, namelen,
1243 		    honor_skip);
1244 		if (action == ITER_SIBLINGS)
1245 			continue;
1246 		if (action == ITER_FOUND) {
1247 			success = true;
1248 			break;
1249 		}
1250 		KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action));
1251 
1252 		next_lsp = SYSCTL_CHILDREN(oidp);
1253 		if (namelen == 0) {
1254 			success = sysctl_sysctl_next_action(next_lsp, NULL, 0,
1255 			    next + 1, len, level + 1, honor_skip);
1256 		} else {
1257 			success = sysctl_sysctl_next_action(next_lsp, name + 1,
1258 			    namelen - 1, next + 1, len, level + 1, honor_skip);
1259 			if (!success) {
1260 
1261 				/*
1262 				 * We maintain the invariant that current node oid
1263 				 * is >= the oid provided in @name.
1264 				 * As there are no usable children at this node,
1265 				 *  current node oid is strictly > than the requested
1266 				 *  oid.
1267 				 * Hence, reduce namelen to 0 to allow for picking first
1268 				 *  nodes/leafs in the next node in list.
1269 				 */
1270 				namelen = 0;
1271 			}
1272 		}
1273 		if (success)
1274 			break;
1275 	}
1276 
1277 	if (success) {
1278 		*next = oidp->oid_number;
1279 		if (level > *len)
1280 			*len = level;
1281 	}
1282 
1283 	return (success);
1284 }
1285 
1286 static int
1287 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1288 {
1289 	int *name = (int *) arg1;
1290 	u_int namelen = arg2;
1291 	int len, error;
1292 	bool success;
1293 	struct sysctl_oid_list *lsp = &sysctl__children;
1294 	struct rm_priotracker tracker;
1295 	int next[CTL_MAXNAME];
1296 
1297 	len = 0;
1298 	SYSCTL_RLOCK(&tracker);
1299 	success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1,
1300 	    oidp->oid_number == CTL_SYSCTL_NEXT);
1301 	SYSCTL_RUNLOCK(&tracker);
1302 	if (!success)
1303 		return (ENOENT);
1304 	error = SYSCTL_OUT(req, next, len * sizeof (int));
1305 	return (error);
1306 }
1307 
1308 /*
1309  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1310  * capability mode.
1311  */
1312 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1313     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1314 
1315 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1316     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1317 
1318 static int
1319 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1320 {
1321 	struct sysctl_oid *oidp;
1322 	struct sysctl_oid_list *lsp = &sysctl__children;
1323 
1324 	SYSCTL_ASSERT_LOCKED();
1325 
1326 	for (*len = 0; *len < CTL_MAXNAME;) {
1327 		oidp = sysctl_find_oidname(strsep(&name, "."), lsp);
1328 		if (oidp == NULL)
1329 			return (ENOENT);
1330 		*oid++ = oidp->oid_number;
1331 		(*len)++;
1332 
1333 		if (name == NULL || *name == '\0') {
1334 			if (oidpp)
1335 				*oidpp = oidp;
1336 			return (0);
1337 		}
1338 
1339 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1340 			break;
1341 
1342 		if (oidp->oid_handler)
1343 			break;
1344 
1345 		lsp = SYSCTL_CHILDREN(oidp);
1346 	}
1347 	return (ENOENT);
1348 }
1349 
1350 static int
1351 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1352 {
1353 	char *p;
1354 	int error, oid[CTL_MAXNAME], len = 0;
1355 	struct sysctl_oid *op = NULL;
1356 	struct rm_priotracker tracker;
1357 	char buf[32];
1358 
1359 	if (!req->newlen)
1360 		return (ENOENT);
1361 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
1362 		return (ENAMETOOLONG);
1363 
1364 	p = buf;
1365 	if (req->newlen >= sizeof(buf))
1366 		p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1367 
1368 	error = SYSCTL_IN(req, p, req->newlen);
1369 	if (error) {
1370 		if (p != buf)
1371 			free(p, M_SYSCTL);
1372 		return (error);
1373 	}
1374 
1375 	p [req->newlen] = '\0';
1376 
1377 	SYSCTL_RLOCK(&tracker);
1378 	error = name2oid(p, oid, &len, &op);
1379 	SYSCTL_RUNLOCK(&tracker);
1380 
1381 	if (p != buf)
1382 		free(p, M_SYSCTL);
1383 
1384 	if (error)
1385 		return (error);
1386 
1387 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1388 	return (error);
1389 }
1390 
1391 /*
1392  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1393  * capability mode.
1394  */
1395 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1396     CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1397     sysctl_sysctl_name2oid, "I", "");
1398 
1399 static int
1400 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1401 {
1402 	struct sysctl_oid *oid;
1403 	struct rm_priotracker tracker;
1404 	int error;
1405 
1406 	error = sysctl_wire_old_buffer(req, 0);
1407 	if (error)
1408 		return (error);
1409 
1410 	SYSCTL_RLOCK(&tracker);
1411 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1412 	if (error)
1413 		goto out;
1414 
1415 	if (oid->oid_fmt == NULL) {
1416 		error = ENOENT;
1417 		goto out;
1418 	}
1419 	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1420 	if (error)
1421 		goto out;
1422 	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1423  out:
1424 	SYSCTL_RUNLOCK(&tracker);
1425 	return (error);
1426 }
1427 
1428 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1429     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1430 
1431 static int
1432 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1433 {
1434 	struct sysctl_oid *oid;
1435 	struct rm_priotracker tracker;
1436 	int error;
1437 
1438 	error = sysctl_wire_old_buffer(req, 0);
1439 	if (error)
1440 		return (error);
1441 
1442 	SYSCTL_RLOCK(&tracker);
1443 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1444 	if (error)
1445 		goto out;
1446 
1447 	if (oid->oid_descr == NULL) {
1448 		error = ENOENT;
1449 		goto out;
1450 	}
1451 	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1452  out:
1453 	SYSCTL_RUNLOCK(&tracker);
1454 	return (error);
1455 }
1456 
1457 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1458     CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1459 
1460 static int
1461 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1462 {
1463 	struct sysctl_oid *oid;
1464 	struct rm_priotracker tracker;
1465 	int error;
1466 
1467 	error = sysctl_wire_old_buffer(req, 0);
1468 	if (error)
1469 		return (error);
1470 
1471 	SYSCTL_RLOCK(&tracker);
1472 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1473 	if (error)
1474 		goto out;
1475 
1476 	if (oid->oid_label == NULL) {
1477 		error = ENOENT;
1478 		goto out;
1479 	}
1480 	error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1481  out:
1482 	SYSCTL_RUNLOCK(&tracker);
1483 	return (error);
1484 }
1485 
1486 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1487     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1488 
1489 /*
1490  * Default "handler" functions.
1491  */
1492 
1493 /*
1494  * Handle a bool.
1495  * Two cases:
1496  *     a variable:  point arg1 at it.
1497  *     a constant:  pass it in arg2.
1498  */
1499 
1500 int
1501 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1502 {
1503 	uint8_t temp;
1504 	int error;
1505 
1506 	/*
1507 	 * Attempt to get a coherent snapshot by making a copy of the data.
1508 	 */
1509 	if (arg1)
1510 		temp = *(bool *)arg1 ? 1 : 0;
1511 	else
1512 		temp = arg2 ? 1 : 0;
1513 
1514 	error = SYSCTL_OUT(req, &temp, sizeof(temp));
1515 	if (error || !req->newptr)
1516 		return (error);
1517 
1518 	if (!arg1)
1519 		error = EPERM;
1520 	else {
1521 		error = SYSCTL_IN(req, &temp, sizeof(temp));
1522 		if (!error)
1523 			*(bool *)arg1 = temp ? 1 : 0;
1524 	}
1525 	return (error);
1526 }
1527 
1528 /*
1529  * Handle an int8_t, signed or unsigned.
1530  * Two cases:
1531  *     a variable:  point arg1 at it.
1532  *     a constant:  pass it in arg2.
1533  */
1534 
1535 int
1536 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1537 {
1538 	int8_t tmpout;
1539 	int error = 0;
1540 
1541 	/*
1542 	 * Attempt to get a coherent snapshot by making a copy of the data.
1543 	 */
1544 	if (arg1)
1545 		tmpout = *(int8_t *)arg1;
1546 	else
1547 		tmpout = arg2;
1548 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1549 
1550 	if (error || !req->newptr)
1551 		return (error);
1552 
1553 	if (!arg1)
1554 		error = EPERM;
1555 	else
1556 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1557 	return (error);
1558 }
1559 
1560 /*
1561  * Handle an int16_t, signed or unsigned.
1562  * Two cases:
1563  *     a variable:  point arg1 at it.
1564  *     a constant:  pass it in arg2.
1565  */
1566 
1567 int
1568 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1569 {
1570 	int16_t tmpout;
1571 	int error = 0;
1572 
1573 	/*
1574 	 * Attempt to get a coherent snapshot by making a copy of the data.
1575 	 */
1576 	if (arg1)
1577 		tmpout = *(int16_t *)arg1;
1578 	else
1579 		tmpout = arg2;
1580 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1581 
1582 	if (error || !req->newptr)
1583 		return (error);
1584 
1585 	if (!arg1)
1586 		error = EPERM;
1587 	else
1588 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1589 	return (error);
1590 }
1591 
1592 /*
1593  * Handle an int32_t, signed or unsigned.
1594  * Two cases:
1595  *     a variable:  point arg1 at it.
1596  *     a constant:  pass it in arg2.
1597  */
1598 
1599 int
1600 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1601 {
1602 	int32_t tmpout;
1603 	int error = 0;
1604 
1605 	/*
1606 	 * Attempt to get a coherent snapshot by making a copy of the data.
1607 	 */
1608 	if (arg1)
1609 		tmpout = *(int32_t *)arg1;
1610 	else
1611 		tmpout = arg2;
1612 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1613 
1614 	if (error || !req->newptr)
1615 		return (error);
1616 
1617 	if (!arg1)
1618 		error = EPERM;
1619 	else
1620 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1621 	return (error);
1622 }
1623 
1624 /*
1625  * Handle an int, signed or unsigned.
1626  * Two cases:
1627  *     a variable:  point arg1 at it.
1628  *     a constant:  pass it in arg2.
1629  */
1630 
1631 int
1632 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1633 {
1634 	int tmpout, error = 0;
1635 
1636 	/*
1637 	 * Attempt to get a coherent snapshot by making a copy of the data.
1638 	 */
1639 	if (arg1)
1640 		tmpout = *(int *)arg1;
1641 	else
1642 		tmpout = arg2;
1643 	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1644 
1645 	if (error || !req->newptr)
1646 		return (error);
1647 
1648 	if (!arg1)
1649 		error = EPERM;
1650 	else
1651 		error = SYSCTL_IN(req, arg1, sizeof(int));
1652 	return (error);
1653 }
1654 
1655 /*
1656  * Based on sysctl_handle_int() convert milliseconds into ticks.
1657  * Note: this is used by TCP.
1658  */
1659 
1660 int
1661 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1662 {
1663 	int error, s, tt;
1664 
1665 	tt = *(int *)arg1;
1666 	s = (int)((int64_t)tt * 1000 / hz);
1667 
1668 	error = sysctl_handle_int(oidp, &s, 0, req);
1669 	if (error || !req->newptr)
1670 		return (error);
1671 
1672 	tt = (int)((int64_t)s * hz / 1000);
1673 	if (tt < 1)
1674 		return (EINVAL);
1675 
1676 	*(int *)arg1 = tt;
1677 	return (0);
1678 }
1679 
1680 /*
1681  * Handle a long, signed or unsigned.
1682  * Two cases:
1683  *     a variable:  point arg1 at it.
1684  *     a constant:  pass it in arg2.
1685  */
1686 
1687 int
1688 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1689 {
1690 	int error = 0;
1691 	long tmplong;
1692 #ifdef SCTL_MASK32
1693 	int tmpint;
1694 #endif
1695 
1696 	/*
1697 	 * Attempt to get a coherent snapshot by making a copy of the data.
1698 	 */
1699 	if (arg1)
1700 		tmplong = *(long *)arg1;
1701 	else
1702 		tmplong = arg2;
1703 #ifdef SCTL_MASK32
1704 	if (req->flags & SCTL_MASK32) {
1705 		tmpint = tmplong;
1706 		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1707 	} else
1708 #endif
1709 		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1710 
1711 	if (error || !req->newptr)
1712 		return (error);
1713 
1714 	if (!arg1)
1715 		error = EPERM;
1716 #ifdef SCTL_MASK32
1717 	else if (req->flags & SCTL_MASK32) {
1718 		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1719 		*(long *)arg1 = (long)tmpint;
1720 	}
1721 #endif
1722 	else
1723 		error = SYSCTL_IN(req, arg1, sizeof(long));
1724 	return (error);
1725 }
1726 
1727 /*
1728  * Handle a 64 bit int, signed or unsigned.
1729  * Two cases:
1730  *     a variable:  point arg1 at it.
1731  *     a constant:  pass it in arg2.
1732  */
1733 int
1734 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1735 {
1736 	int error = 0;
1737 	uint64_t tmpout;
1738 
1739 	/*
1740 	 * Attempt to get a coherent snapshot by making a copy of the data.
1741 	 */
1742 	if (arg1)
1743 		tmpout = *(uint64_t *)arg1;
1744 	else
1745 		tmpout = arg2;
1746 	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1747 
1748 	if (error || !req->newptr)
1749 		return (error);
1750 
1751 	if (!arg1)
1752 		error = EPERM;
1753 	else
1754 		error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1755 	return (error);
1756 }
1757 
1758 /*
1759  * Handle our generic '\0' terminated 'C' string.
1760  * Two cases:
1761  * 	a variable string:  point arg1 at it, arg2 is max length.
1762  * 	a constant string:  point arg1 at it, arg2 is zero.
1763  */
1764 
1765 int
1766 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1767 {
1768 	char *tmparg;
1769 	size_t outlen;
1770 	int error = 0, ro_string = 0;
1771 
1772 	/*
1773 	 * If the sysctl isn't writable and isn't a preallocated tunable that
1774 	 * can be modified by kenv(2), microoptimise and treat it as a
1775 	 * read-only string.
1776 	 * A zero-length buffer indicates a fixed size read-only
1777 	 * string.  In ddb, don't worry about trying to make a malloced
1778 	 * snapshot.
1779 	 */
1780 	if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
1781 	    arg2 == 0 || kdb_active) {
1782 		arg2 = strlen((char *)arg1) + 1;
1783 		ro_string = 1;
1784 	}
1785 
1786 	if (req->oldptr != NULL) {
1787 		if (ro_string) {
1788 			tmparg = arg1;
1789 			outlen = strlen(tmparg) + 1;
1790 		} else {
1791 			tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1792 			sx_slock(&sysctlstringlock);
1793 			memcpy(tmparg, arg1, arg2);
1794 			sx_sunlock(&sysctlstringlock);
1795 			outlen = strlen(tmparg) + 1;
1796 		}
1797 
1798 		error = SYSCTL_OUT(req, tmparg, outlen);
1799 
1800 		if (!ro_string)
1801 			free(tmparg, M_SYSCTLTMP);
1802 	} else {
1803 		if (!ro_string)
1804 			sx_slock(&sysctlstringlock);
1805 		outlen = strlen((char *)arg1) + 1;
1806 		if (!ro_string)
1807 			sx_sunlock(&sysctlstringlock);
1808 		error = SYSCTL_OUT(req, NULL, outlen);
1809 	}
1810 	if (error || !req->newptr)
1811 		return (error);
1812 
1813 	if (req->newlen - req->newidx >= arg2 ||
1814 	    req->newlen - req->newidx < 0) {
1815 		error = EINVAL;
1816 	} else if (req->newlen - req->newidx == 0) {
1817 		sx_xlock(&sysctlstringlock);
1818 		((char *)arg1)[0] = '\0';
1819 		sx_xunlock(&sysctlstringlock);
1820 	} else if (req->newfunc == sysctl_new_kernel) {
1821 		arg2 = req->newlen - req->newidx;
1822 		sx_xlock(&sysctlstringlock);
1823 		error = SYSCTL_IN(req, arg1, arg2);
1824 		if (error == 0) {
1825 			((char *)arg1)[arg2] = '\0';
1826 			req->newidx += arg2;
1827 		}
1828 		sx_xunlock(&sysctlstringlock);
1829 	} else {
1830 		arg2 = req->newlen - req->newidx;
1831 		tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1832 
1833 		error = SYSCTL_IN(req, tmparg, arg2);
1834 		if (error) {
1835 			free(tmparg, M_SYSCTLTMP);
1836 			return (error);
1837 		}
1838 
1839 		sx_xlock(&sysctlstringlock);
1840 		memcpy(arg1, tmparg, arg2);
1841 		((char *)arg1)[arg2] = '\0';
1842 		sx_xunlock(&sysctlstringlock);
1843 		free(tmparg, M_SYSCTLTMP);
1844 		req->newidx += arg2;
1845 	}
1846 	return (error);
1847 }
1848 
1849 /*
1850  * Handle any kind of opaque data.
1851  * arg1 points to it, arg2 is the size.
1852  */
1853 
1854 int
1855 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1856 {
1857 	int error, tries;
1858 	u_int generation;
1859 	struct sysctl_req req2;
1860 
1861 	/*
1862 	 * Attempt to get a coherent snapshot, by using the thread
1863 	 * pre-emption counter updated from within mi_switch() to
1864 	 * determine if we were pre-empted during a bcopy() or
1865 	 * copyout(). Make 3 attempts at doing this before giving up.
1866 	 * If we encounter an error, stop immediately.
1867 	 */
1868 	tries = 0;
1869 	req2 = *req;
1870 retry:
1871 	generation = curthread->td_generation;
1872 	error = SYSCTL_OUT(req, arg1, arg2);
1873 	if (error)
1874 		return (error);
1875 	tries++;
1876 	if (generation != curthread->td_generation && tries < 3) {
1877 		*req = req2;
1878 		goto retry;
1879 	}
1880 
1881 	error = SYSCTL_IN(req, arg1, arg2);
1882 
1883 	return (error);
1884 }
1885 
1886 /*
1887  * Based on sysctl_handle_64() convert microseconds to a sbintime.
1888  */
1889 int
1890 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1891 {
1892 	int error;
1893 	int64_t usec;
1894 
1895 	usec = sbttous(*(sbintime_t *)arg1);
1896 
1897 	error = sysctl_handle_64(oidp, &usec, 0, req);
1898 	if (error || !req->newptr)
1899 		return (error);
1900 
1901 	*(sbintime_t *)arg1 = ustosbt(usec);
1902 
1903 	return (0);
1904 }
1905 
1906 /*
1907  * Based on sysctl_handle_64() convert milliseconds to a sbintime.
1908  */
1909 int
1910 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1911 {
1912 	int error;
1913 	int64_t msec;
1914 
1915 	msec = sbttoms(*(sbintime_t *)arg1);
1916 
1917 	error = sysctl_handle_64(oidp, &msec, 0, req);
1918 	if (error || !req->newptr)
1919 		return (error);
1920 
1921 	*(sbintime_t *)arg1 = mstosbt(msec);
1922 
1923 	return (0);
1924 }
1925 
1926 /*
1927  * Convert seconds to a struct timeval.  Intended for use with
1928  * intervals and thus does not permit negative seconds.
1929  */
1930 int
1931 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1932 {
1933 	struct timeval *tv;
1934 	int error, secs;
1935 
1936 	tv = arg1;
1937 	secs = tv->tv_sec;
1938 
1939 	error = sysctl_handle_int(oidp, &secs, 0, req);
1940 	if (error || req->newptr == NULL)
1941 		return (error);
1942 
1943 	if (secs < 0)
1944 		return (EINVAL);
1945 	tv->tv_sec = secs;
1946 
1947 	return (0);
1948 }
1949 
1950 /*
1951  * Transfer functions to/from kernel space.
1952  * XXX: rather untested at this point
1953  */
1954 static int
1955 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1956 {
1957 	size_t i = 0;
1958 
1959 	if (req->oldptr) {
1960 		i = l;
1961 		if (req->oldlen <= req->oldidx)
1962 			i = 0;
1963 		else
1964 			if (i > req->oldlen - req->oldidx)
1965 				i = req->oldlen - req->oldidx;
1966 		if (i > 0)
1967 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1968 	}
1969 	req->oldidx += l;
1970 	if (req->oldptr && i != l)
1971 		return (ENOMEM);
1972 	return (0);
1973 }
1974 
1975 static int
1976 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1977 {
1978 	if (!req->newptr)
1979 		return (0);
1980 	if (req->newlen - req->newidx < l)
1981 		return (EINVAL);
1982 	bcopy((const char *)req->newptr + req->newidx, p, l);
1983 	req->newidx += l;
1984 	return (0);
1985 }
1986 
1987 int
1988 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1989     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1990 {
1991 	int error = 0;
1992 	struct sysctl_req req;
1993 
1994 	bzero(&req, sizeof req);
1995 
1996 	req.td = td;
1997 	req.flags = flags;
1998 
1999 	if (oldlenp) {
2000 		req.oldlen = *oldlenp;
2001 	}
2002 	req.validlen = req.oldlen;
2003 
2004 	if (old) {
2005 		req.oldptr= old;
2006 	}
2007 
2008 	if (new != NULL) {
2009 		req.newlen = newlen;
2010 		req.newptr = new;
2011 	}
2012 
2013 	req.oldfunc = sysctl_old_kernel;
2014 	req.newfunc = sysctl_new_kernel;
2015 	req.lock = REQ_UNWIRED;
2016 
2017 	error = sysctl_root(0, name, namelen, &req);
2018 
2019 	if (req.lock == REQ_WIRED && req.validlen > 0)
2020 		vsunlock(req.oldptr, req.validlen);
2021 
2022 	if (error && error != ENOMEM)
2023 		return (error);
2024 
2025 	if (retval) {
2026 		if (req.oldptr && req.oldidx > req.validlen)
2027 			*retval = req.validlen;
2028 		else
2029 			*retval = req.oldidx;
2030 	}
2031 	return (error);
2032 }
2033 
2034 int
2035 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
2036     void *new, size_t newlen, size_t *retval, int flags)
2037 {
2038         int oid[CTL_MAXNAME];
2039         size_t oidlen, plen;
2040 	int error;
2041 
2042 	oid[0] = CTL_SYSCTL;
2043 	oid[1] = CTL_SYSCTL_NAME2OID;
2044 	oidlen = sizeof(oid);
2045 
2046 	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
2047 	    (void *)name, strlen(name), &plen, flags);
2048 	if (error)
2049 		return (error);
2050 
2051 	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
2052 	    new, newlen, retval, flags);
2053 	return (error);
2054 }
2055 
2056 /*
2057  * Transfer function to/from user space.
2058  */
2059 static int
2060 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
2061 {
2062 	size_t i, len, origidx;
2063 	int error;
2064 
2065 	origidx = req->oldidx;
2066 	req->oldidx += l;
2067 	if (req->oldptr == NULL)
2068 		return (0);
2069 	/*
2070 	 * If we have not wired the user supplied buffer and we are currently
2071 	 * holding locks, drop a witness warning, as it's possible that
2072 	 * write operations to the user page can sleep.
2073 	 */
2074 	if (req->lock != REQ_WIRED)
2075 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2076 		    "sysctl_old_user()");
2077 	i = l;
2078 	len = req->validlen;
2079 	if (len <= origidx)
2080 		i = 0;
2081 	else {
2082 		if (i > len - origidx)
2083 			i = len - origidx;
2084 		if (req->lock == REQ_WIRED) {
2085 			error = copyout_nofault(p, (char *)req->oldptr +
2086 			    origidx, i);
2087 		} else
2088 			error = copyout(p, (char *)req->oldptr + origidx, i);
2089 		if (error != 0)
2090 			return (error);
2091 	}
2092 	if (i < l)
2093 		return (ENOMEM);
2094 	return (0);
2095 }
2096 
2097 static int
2098 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2099 {
2100 	int error;
2101 
2102 	if (!req->newptr)
2103 		return (0);
2104 	if (req->newlen - req->newidx < l)
2105 		return (EINVAL);
2106 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2107 	    "sysctl_new_user()");
2108 	error = copyin((const char *)req->newptr + req->newidx, p, l);
2109 	req->newidx += l;
2110 	return (error);
2111 }
2112 
2113 /*
2114  * Wire the user space destination buffer.  If set to a value greater than
2115  * zero, the len parameter limits the maximum amount of wired memory.
2116  */
2117 int
2118 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2119 {
2120 	int ret;
2121 	size_t wiredlen;
2122 
2123 	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2124 	ret = 0;
2125 	if (req->lock != REQ_WIRED && req->oldptr &&
2126 	    req->oldfunc == sysctl_old_user) {
2127 		if (wiredlen != 0) {
2128 			ret = vslock(req->oldptr, wiredlen);
2129 			if (ret != 0) {
2130 				if (ret != ENOMEM)
2131 					return (ret);
2132 				wiredlen = 0;
2133 			}
2134 		}
2135 		req->lock = REQ_WIRED;
2136 		req->validlen = wiredlen;
2137 	}
2138 	return (0);
2139 }
2140 
2141 int
2142 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2143     int *nindx, struct sysctl_req *req)
2144 {
2145 	struct sysctl_oid_list *lsp;
2146 	struct sysctl_oid *oid;
2147 	struct sysctl_oid key;
2148 	int indx;
2149 
2150 	SYSCTL_ASSERT_LOCKED();
2151 	lsp = &sysctl__children;
2152 	indx = 0;
2153 	while (indx < CTL_MAXNAME) {
2154 		key.oid_number = name[indx];
2155 		oid = RB_FIND(sysctl_oid_list, lsp, &key);
2156 		if (oid == NULL)
2157 			return (ENOENT);
2158 
2159 		indx++;
2160 		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2161 			if (oid->oid_handler != NULL || indx == namelen) {
2162 				*noid = oid;
2163 				if (nindx != NULL)
2164 					*nindx = indx;
2165 				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2166 				    ("%s found DYING node %p", __func__, oid));
2167 				return (0);
2168 			}
2169 			lsp = SYSCTL_CHILDREN(oid);
2170 		} else if (indx == namelen) {
2171 			if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2172 				return (ENOENT);
2173 			*noid = oid;
2174 			if (nindx != NULL)
2175 				*nindx = indx;
2176 			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2177 			    ("%s found DYING node %p", __func__, oid));
2178 			return (0);
2179 		} else {
2180 			return (ENOTDIR);
2181 		}
2182 	}
2183 	return (ENOENT);
2184 }
2185 
2186 /*
2187  * Traverse our tree, and find the right node, execute whatever it points
2188  * to, and return the resulting error code.
2189  */
2190 
2191 static int
2192 sysctl_root(SYSCTL_HANDLER_ARGS)
2193 {
2194 	struct sysctl_oid *oid;
2195 	struct rm_priotracker tracker;
2196 	int error, indx, lvl;
2197 
2198 	SYSCTL_RLOCK(&tracker);
2199 
2200 	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2201 	if (error)
2202 		goto out;
2203 
2204 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2205 		/*
2206 		 * You can't call a sysctl when it's a node, but has
2207 		 * no handler.  Inform the user that it's a node.
2208 		 * The indx may or may not be the same as namelen.
2209 		 */
2210 		if (oid->oid_handler == NULL) {
2211 			error = EISDIR;
2212 			goto out;
2213 		}
2214 	}
2215 
2216 	/* Is this sysctl writable? */
2217 	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2218 		error = EPERM;
2219 		goto out;
2220 	}
2221 
2222 	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2223 
2224 #ifdef CAPABILITY_MODE
2225 	/*
2226 	 * If the process is in capability mode, then don't permit reading or
2227 	 * writing unless specifically granted for the node.
2228 	 */
2229 	if (IN_CAPABILITY_MODE(req->td)) {
2230 		if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2231 		    (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2232 			error = EPERM;
2233 			goto out;
2234 		}
2235 	}
2236 #endif
2237 
2238 	/* Is this sysctl sensitive to securelevels? */
2239 	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2240 		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2241 		error = securelevel_gt(req->td->td_ucred, lvl);
2242 		if (error)
2243 			goto out;
2244 	}
2245 
2246 	/* Is this sysctl writable by only privileged users? */
2247 	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2248 		int priv;
2249 
2250 		if (oid->oid_kind & CTLFLAG_PRISON)
2251 			priv = PRIV_SYSCTL_WRITEJAIL;
2252 #ifdef VIMAGE
2253 		else if ((oid->oid_kind & CTLFLAG_VNET) &&
2254 		     prison_owns_vnet(req->td->td_ucred))
2255 			priv = PRIV_SYSCTL_WRITEJAIL;
2256 #endif
2257 		else
2258 			priv = PRIV_SYSCTL_WRITE;
2259 		error = priv_check(req->td, priv);
2260 		if (error)
2261 			goto out;
2262 	}
2263 
2264 	if (!oid->oid_handler) {
2265 		error = EINVAL;
2266 		goto out;
2267 	}
2268 
2269 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2270 		arg1 = (int *)arg1 + indx;
2271 		arg2 -= indx;
2272 	} else {
2273 		arg1 = oid->oid_arg1;
2274 		arg2 = oid->oid_arg2;
2275 	}
2276 #ifdef MAC
2277 	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2278 	    req);
2279 	if (error != 0)
2280 		goto out;
2281 #endif
2282 #ifdef VIMAGE
2283 	if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2284 		arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2285 #endif
2286 	error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2287 
2288 out:
2289 	SYSCTL_RUNLOCK(&tracker);
2290 	return (error);
2291 }
2292 
2293 #ifndef _SYS_SYSPROTO_H_
2294 struct __sysctl_args {
2295 	int	*name;
2296 	u_int	namelen;
2297 	void	*old;
2298 	size_t	*oldlenp;
2299 	void	*new;
2300 	size_t	newlen;
2301 };
2302 #endif
2303 int
2304 sys___sysctl(struct thread *td, struct __sysctl_args *uap)
2305 {
2306 	int error, i, name[CTL_MAXNAME];
2307 	size_t j;
2308 
2309 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2310 		return (EINVAL);
2311 
2312  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2313  	if (error)
2314 		return (error);
2315 
2316 	error = userland_sysctl(td, name, uap->namelen,
2317 		uap->old, uap->oldlenp, 0,
2318 		uap->new, uap->newlen, &j, 0);
2319 	if (error && error != ENOMEM)
2320 		return (error);
2321 	if (uap->oldlenp) {
2322 		i = copyout(&j, uap->oldlenp, sizeof(j));
2323 		if (i)
2324 			return (i);
2325 	}
2326 	return (error);
2327 }
2328 
2329 int
2330 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2331     void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2332     int flags, bool inkernel)
2333 {
2334 	int oid[CTL_MAXNAME];
2335 	char namebuf[16];
2336 	char *name;
2337 	size_t oidlen;
2338 	int error;
2339 
2340 	if (namelen > MAXPATHLEN || namelen == 0)
2341 		return (EINVAL);
2342 	name = namebuf;
2343 	if (namelen > sizeof(namebuf))
2344 		name = malloc(namelen, M_SYSCTL, M_WAITOK);
2345 	error = copyin(oname, name, namelen);
2346 	if (error != 0)
2347 		goto out;
2348 
2349 	oid[0] = CTL_SYSCTL;
2350 	oid[1] = CTL_SYSCTL_NAME2OID;
2351 	oidlen = sizeof(oid);
2352 	error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2353 	    retval, flags);
2354 	if (error != 0)
2355 		goto out;
2356 	error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2357 	    inkernel, new, newlen, retval, flags);
2358 
2359 out:
2360 	if (namelen > sizeof(namebuf))
2361 		free(name, M_SYSCTL);
2362 	return (error);
2363 }
2364 
2365 #ifndef	_SYS_SYSPROTO_H_
2366 struct __sysctlbyname_args {
2367 	const char	*name;
2368 	size_t	namelen;
2369 	void	*old;
2370 	size_t	*oldlenp;
2371 	void	*new;
2372 	size_t	newlen;
2373 };
2374 #endif
2375 int
2376 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2377 {
2378 	size_t rv;
2379 	int error;
2380 
2381 	error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2382 	    uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2383 	if (error != 0)
2384 		return (error);
2385 	if (uap->oldlenp != NULL)
2386 		error = copyout(&rv, uap->oldlenp, sizeof(rv));
2387 
2388 	return (error);
2389 }
2390 
2391 /*
2392  * This is used from various compatibility syscalls too.  That's why name
2393  * must be in kernel space.
2394  */
2395 int
2396 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2397     size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2398     size_t *retval, int flags)
2399 {
2400 	int error = 0, memlocked;
2401 	struct sysctl_req req;
2402 
2403 	bzero(&req, sizeof req);
2404 
2405 	req.td = td;
2406 	req.flags = flags;
2407 
2408 	if (oldlenp) {
2409 		if (inkernel) {
2410 			req.oldlen = *oldlenp;
2411 		} else {
2412 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2413 			if (error)
2414 				return (error);
2415 		}
2416 	}
2417 	req.validlen = req.oldlen;
2418 	req.oldptr = old;
2419 
2420 	if (new != NULL) {
2421 		req.newlen = newlen;
2422 		req.newptr = new;
2423 	}
2424 
2425 	req.oldfunc = sysctl_old_user;
2426 	req.newfunc = sysctl_new_user;
2427 	req.lock = REQ_UNWIRED;
2428 
2429 #ifdef KTRACE
2430 	if (KTRPOINT(curthread, KTR_SYSCTL))
2431 		ktrsysctl(name, namelen);
2432 #endif
2433 	memlocked = 0;
2434 	if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2435 		memlocked = 1;
2436 		sx_xlock(&sysctlmemlock);
2437 	}
2438 	CURVNET_SET(TD_TO_VNET(td));
2439 
2440 	for (;;) {
2441 		req.oldidx = 0;
2442 		req.newidx = 0;
2443 		error = sysctl_root(0, name, namelen, &req);
2444 		if (error != EAGAIN)
2445 			break;
2446 		kern_yield(PRI_USER);
2447 	}
2448 
2449 	CURVNET_RESTORE();
2450 
2451 	if (req.lock == REQ_WIRED && req.validlen > 0)
2452 		vsunlock(req.oldptr, req.validlen);
2453 	if (memlocked)
2454 		sx_xunlock(&sysctlmemlock);
2455 
2456 	if (error && error != ENOMEM)
2457 		return (error);
2458 
2459 	if (retval) {
2460 		if (req.oldptr && req.oldidx > req.validlen)
2461 			*retval = req.validlen;
2462 		else
2463 			*retval = req.oldidx;
2464 	}
2465 	return (error);
2466 }
2467 
2468 /*
2469  * Drain into a sysctl struct.  The user buffer should be wired if a page
2470  * fault would cause issue.
2471  */
2472 static int
2473 sbuf_sysctl_drain(void *arg, const char *data, int len)
2474 {
2475 	struct sysctl_req *req = arg;
2476 	int error;
2477 
2478 	error = SYSCTL_OUT(req, data, len);
2479 	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2480 	return (error == 0 ? len : -error);
2481 }
2482 
2483 struct sbuf *
2484 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2485     struct sysctl_req *req)
2486 {
2487 
2488 	/* Supply a default buffer size if none given. */
2489 	if (buf == NULL && length == 0)
2490 		length = 64;
2491 	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2492 	sbuf_set_drain(s, sbuf_sysctl_drain, req);
2493 	return (s);
2494 }
2495 
2496 #ifdef DDB
2497 
2498 /* The current OID the debugger is working with */
2499 static struct sysctl_oid *g_ddb_oid;
2500 
2501 /* The current flags specified by the user */
2502 static int g_ddb_sysctl_flags;
2503 
2504 /* Check to see if the last sysctl printed */
2505 static int g_ddb_sysctl_printed;
2506 
2507 static const int ctl_sign[CTLTYPE+1] = {
2508 	[CTLTYPE_INT] = 1,
2509 	[CTLTYPE_LONG] = 1,
2510 	[CTLTYPE_S8] = 1,
2511 	[CTLTYPE_S16] = 1,
2512 	[CTLTYPE_S32] = 1,
2513 	[CTLTYPE_S64] = 1,
2514 };
2515 
2516 static const int ctl_size[CTLTYPE+1] = {
2517 	[CTLTYPE_INT] = sizeof(int),
2518 	[CTLTYPE_UINT] = sizeof(u_int),
2519 	[CTLTYPE_LONG] = sizeof(long),
2520 	[CTLTYPE_ULONG] = sizeof(u_long),
2521 	[CTLTYPE_S8] = sizeof(int8_t),
2522 	[CTLTYPE_S16] = sizeof(int16_t),
2523 	[CTLTYPE_S32] = sizeof(int32_t),
2524 	[CTLTYPE_S64] = sizeof(int64_t),
2525 	[CTLTYPE_U8] = sizeof(uint8_t),
2526 	[CTLTYPE_U16] = sizeof(uint16_t),
2527 	[CTLTYPE_U32] = sizeof(uint32_t),
2528 	[CTLTYPE_U64] = sizeof(uint64_t),
2529 };
2530 
2531 #define DB_SYSCTL_NAME_ONLY	0x001	/* Compare with -N */
2532 #define DB_SYSCTL_VALUE_ONLY	0x002	/* Compare with -n */
2533 #define DB_SYSCTL_OPAQUE	0x004	/* Compare with -o */
2534 #define DB_SYSCTL_HEX		0x008	/* Compare with -x */
2535 
2536 #define DB_SYSCTL_SAFE_ONLY	0x100	/* Only simple types */
2537 
2538 static const char db_sysctl_modifs[] = {
2539 	'N', 'n', 'o', 'x',
2540 };
2541 
2542 static const int db_sysctl_modif_values[] = {
2543 	DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2544 	DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2545 };
2546 
2547 /* Handlers considered safe to print while recursing */
2548 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2549 	sysctl_handle_bool,
2550 	sysctl_handle_8,
2551 	sysctl_handle_16,
2552 	sysctl_handle_32,
2553 	sysctl_handle_64,
2554 	sysctl_handle_int,
2555 	sysctl_handle_long,
2556 	sysctl_handle_string,
2557 	sysctl_handle_opaque,
2558 };
2559 
2560 /*
2561  * Use in place of sysctl_old_kernel to print sysctl values.
2562  *
2563  * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2564  */
2565 static int
2566 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2567 {
2568 	const u_char *val, *p;
2569 	const char *sep1;
2570 	size_t intlen, slen;
2571 	uintmax_t umv;
2572 	intmax_t mv;
2573 	int sign, ctltype, hexlen, xflag, error;
2574 
2575 	/* Suppress false-positive GCC uninitialized variable warnings */
2576 	mv = 0;
2577 	umv = 0;
2578 
2579 	slen = len;
2580 	val = p = ptr;
2581 
2582 	if (ptr == NULL) {
2583 		error = 0;
2584 		goto out;
2585 	}
2586 
2587 	/* We are going to print */
2588 	g_ddb_sysctl_printed = 1;
2589 
2590 	xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2591 
2592 	ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2593 	sign = ctl_sign[ctltype];
2594 	intlen = ctl_size[ctltype];
2595 
2596 	switch (ctltype) {
2597 	case CTLTYPE_NODE:
2598 	case CTLTYPE_STRING:
2599 		db_printf("%.*s", (int) len, (const char *) p);
2600 		error = 0;
2601 		goto out;
2602 
2603 	case CTLTYPE_INT:
2604 	case CTLTYPE_UINT:
2605 	case CTLTYPE_LONG:
2606 	case CTLTYPE_ULONG:
2607 	case CTLTYPE_S8:
2608 	case CTLTYPE_S16:
2609 	case CTLTYPE_S32:
2610 	case CTLTYPE_S64:
2611 	case CTLTYPE_U8:
2612 	case CTLTYPE_U16:
2613 	case CTLTYPE_U32:
2614 	case CTLTYPE_U64:
2615 		hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2616 		sep1 = "";
2617 		while (len >= intlen) {
2618 			switch (ctltype) {
2619 			case CTLTYPE_INT:
2620 			case CTLTYPE_UINT:
2621 				umv = *(const u_int *)p;
2622 				mv = *(const int *)p;
2623 				break;
2624 			case CTLTYPE_LONG:
2625 			case CTLTYPE_ULONG:
2626 				umv = *(const u_long *)p;
2627 				mv = *(const long *)p;
2628 				break;
2629 			case CTLTYPE_S8:
2630 			case CTLTYPE_U8:
2631 				umv = *(const uint8_t *)p;
2632 				mv = *(const int8_t *)p;
2633 				break;
2634 			case CTLTYPE_S16:
2635 			case CTLTYPE_U16:
2636 				umv = *(const uint16_t *)p;
2637 				mv = *(const int16_t *)p;
2638 				break;
2639 			case CTLTYPE_S32:
2640 			case CTLTYPE_U32:
2641 				umv = *(const uint32_t *)p;
2642 				mv = *(const int32_t *)p;
2643 				break;
2644 			case CTLTYPE_S64:
2645 			case CTLTYPE_U64:
2646 				umv = *(const uint64_t *)p;
2647 				mv = *(const int64_t *)p;
2648 				break;
2649 			}
2650 
2651 			db_printf("%s", sep1);
2652 			if (xflag)
2653 				db_printf("%#0*jx", hexlen, umv);
2654 			else if (!sign)
2655 				db_printf("%ju", umv);
2656 			else if (g_ddb_oid->oid_fmt[1] == 'K') {
2657 				/* Kelvins are currently unsupported. */
2658 				error = EOPNOTSUPP;
2659 				goto out;
2660 			} else
2661 				db_printf("%jd", mv);
2662 
2663 			sep1 = " ";
2664 			len -= intlen;
2665 			p += intlen;
2666 		}
2667 		error = 0;
2668 		goto out;
2669 
2670 	case CTLTYPE_OPAQUE:
2671 		/* TODO: Support struct functions. */
2672 
2673 		/* FALLTHROUGH */
2674 	default:
2675 		db_printf("Format:%s Length:%zu Dump:0x",
2676 		    g_ddb_oid->oid_fmt, len);
2677 		while (len-- && (xflag || p < val + 16))
2678 			db_printf("%02x", *p++);
2679 		if (!xflag && len > 16)
2680 			db_printf("...");
2681 		error = 0;
2682 		goto out;
2683 	}
2684 
2685 out:
2686 	req->oldidx += slen;
2687 	return (error);
2688 }
2689 
2690 /*
2691  * Avoid setting new sysctl values from the debugger
2692  */
2693 static int
2694 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2695 {
2696 
2697 	if (!req->newptr)
2698 		return (0);
2699 
2700 	/* Changing sysctls from the debugger is currently unsupported */
2701 	return (EPERM);
2702 }
2703 
2704 /*
2705  * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2706  * Instead of copying any output to a buffer we'll dump it right to
2707  * the console.
2708  */
2709 static int
2710 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2711     void *old, size_t *oldlenp, size_t *retval, int flags)
2712 {
2713 	struct sysctl_req req;
2714 	int error;
2715 
2716 	/* Setup the request */
2717 	bzero(&req, sizeof req);
2718 	req.td = kdb_thread;
2719 	req.oldfunc = sysctl_old_ddb;
2720 	req.newfunc = sysctl_new_ddb;
2721 	req.lock = REQ_UNWIRED;
2722 	if (oldlenp) {
2723 		req.oldlen = *oldlenp;
2724 	}
2725 	req.validlen = req.oldlen;
2726 	if (old) {
2727 		req.oldptr = old;
2728 	}
2729 
2730 	/* Setup our globals for sysctl_old_ddb */
2731 	g_ddb_oid = oidp;
2732 	g_ddb_sysctl_flags = flags;
2733 	g_ddb_sysctl_printed = 0;
2734 
2735 	error = sysctl_root(0, name, namelen, &req);
2736 
2737 	/* Reset globals */
2738 	g_ddb_oid = NULL;
2739 	g_ddb_sysctl_flags = 0;
2740 
2741 	if (retval) {
2742 		if (req.oldptr && req.oldidx > req.validlen)
2743 			*retval = req.validlen;
2744 		else
2745 			*retval = req.oldidx;
2746 	}
2747 	return (error);
2748 }
2749 
2750 /*
2751  * Show a sysctl's name
2752  */
2753 static void
2754 db_show_oid_name(int *oid, size_t nlen)
2755 {
2756 	struct sysctl_oid *oidp;
2757 	int qoid[CTL_MAXNAME + 2];
2758 	int error;
2759 
2760 	qoid[0] = CTL_SYSCTL;
2761 	qoid[1] = CTL_SYSCTL_NAME;
2762 	memcpy(qoid + 2, oid, nlen * sizeof(int));
2763 
2764 	error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2765 	if (error)
2766 		db_error("sysctl name oid");
2767 
2768 	error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2769 	if (error)
2770 		db_error("sysctl name");
2771 }
2772 
2773 /*
2774  * Check to see if an OID is safe to print from ddb.
2775  */
2776 static bool
2777 db_oid_safe(const struct sysctl_oid *oidp)
2778 {
2779 	for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2780 		if (oidp->oid_handler == db_safe_handlers[i])
2781 			return (true);
2782 	}
2783 
2784 	return (false);
2785 }
2786 
2787 /*
2788  * Show a sysctl at a specific OID
2789  * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2790  */
2791 static int
2792 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2793 {
2794 	int error, xflag, oflag, Nflag, nflag;
2795 	size_t len;
2796 
2797 	xflag = flags & DB_SYSCTL_HEX;
2798 	oflag = flags & DB_SYSCTL_OPAQUE;
2799 	nflag = flags & DB_SYSCTL_VALUE_ONLY;
2800 	Nflag = flags & DB_SYSCTL_NAME_ONLY;
2801 
2802 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2803 	    (!xflag && !oflag))
2804 		return (0);
2805 
2806 	if (Nflag) {
2807 		db_show_oid_name(oid, nlen);
2808 		error = 0;
2809 		goto out;
2810 	}
2811 
2812 	if (!nflag) {
2813 		db_show_oid_name(oid, nlen);
2814 		db_printf(": ");
2815 	}
2816 
2817 	if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2818 		db_printf("Skipping, unsafe to print while recursing.");
2819 		error = 0;
2820 		goto out;
2821 	}
2822 
2823 	/* Try once, and ask about the size */
2824 	len = 0;
2825 	error = db_sysctl(oidp, oid, nlen,
2826 	    NULL, NULL, &len, flags);
2827 	if (error)
2828 		goto out;
2829 
2830 	if (!g_ddb_sysctl_printed)
2831 		/* Lie about the size */
2832 		error = db_sysctl(oidp, oid, nlen,
2833 		    (void *) 1, &len, NULL, flags);
2834 
2835 out:
2836 	db_printf("\n");
2837 	return (error);
2838 }
2839 
2840 /*
2841  * Show all sysctls under a specific OID
2842  * Compare to sysctl_all from sbin/sysctl/sysctl.c
2843  */
2844 static int
2845 db_show_sysctl_all(int *oid, size_t len, int flags)
2846 {
2847 	struct sysctl_oid *oidp;
2848 	int qoid[CTL_MAXNAME + 2], next[CTL_MAXNAME];
2849 	size_t nlen;
2850 
2851 	qoid[0] = CTL_SYSCTL;
2852 	qoid[1] = CTL_SYSCTL_NEXT;
2853 	if (len) {
2854 		nlen = len;
2855 		memcpy(&qoid[2], oid, nlen * sizeof(int));
2856 	} else {
2857 		nlen = 1;
2858 		qoid[2] = CTL_KERN;
2859 	}
2860 	for (;;) {
2861 		int error;
2862 		size_t nextsize = sizeof(next);
2863 
2864 		error = kernel_sysctl(kdb_thread, qoid, nlen + 2,
2865 		    next, &nextsize, NULL, 0, &nlen, 0);
2866 		if (error != 0) {
2867 			if (error == ENOENT)
2868 				return (0);
2869 			else
2870 				db_error("sysctl(next)");
2871 		}
2872 
2873 		nlen /= sizeof(int);
2874 
2875 		if (nlen < (unsigned int)len)
2876 			return (0);
2877 
2878 		if (memcmp(&oid[0], &next[0], len * sizeof(int)) != 0)
2879 			return (0);
2880 
2881 		/* Find the OID in question */
2882 		error = sysctl_find_oid(next, nlen, &oidp, NULL, NULL);
2883 		if (error)
2884 			return (error);
2885 
2886 		(void)db_show_oid(oidp, next, nlen, flags | DB_SYSCTL_SAFE_ONLY);
2887 
2888 		if (db_pager_quit)
2889 			return (0);
2890 
2891 		memcpy(&qoid[2 + len], &next[len], (nlen - len) * sizeof(int));
2892 	}
2893 }
2894 
2895 /*
2896  * Show a sysctl by its user facing string
2897  */
2898 static int
2899 db_sysctlbyname(char *name, int flags)
2900 {
2901 	struct sysctl_oid *oidp;
2902 	int oid[CTL_MAXNAME];
2903 	int error, nlen;
2904 
2905 	error = name2oid(name, oid, &nlen, &oidp);
2906 	if (error) {
2907 		return (error);
2908 	}
2909 
2910 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2911 		db_show_sysctl_all(oid, nlen, flags);
2912 	} else {
2913 		error = db_show_oid(oidp, oid, nlen, flags);
2914 	}
2915 
2916 	return (error);
2917 }
2918 
2919 static void
2920 db_sysctl_cmd_usage(void)
2921 {
2922 	db_printf(
2923 	    " sysctl [/Nnox] <sysctl>					    \n"
2924 	    "								    \n"
2925 	    " <sysctl> The name of the sysctl to show.			    \n"
2926 	    "								    \n"
2927 	    " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT.	    \n"
2928 	    " This will work for most sysctls, but should not be used	    \n"
2929 	    " with sysctls that are known to malloc.			    \n"
2930 	    "								    \n"
2931 	    " While recursing any \"unsafe\" sysctls will be skipped.	    \n"
2932 	    " Call sysctl directly on the sysctl to try printing the	    \n"
2933 	    " skipped sysctl. This is unsafe and may make the ddb	    \n"
2934 	    " session unusable.						    \n"
2935 	    "								    \n"
2936 	    " Arguments:						    \n"
2937 	    "	/N	Display only the name of the sysctl.		    \n"
2938 	    "	/n	Display only the value of the sysctl.		    \n"
2939 	    "	/o	Display opaque values.				    \n"
2940 	    "	/x	Display the sysctl in hex.			    \n"
2941 	    "								    \n"
2942 	    "For example:						    \n"
2943 	    "sysctl vm.v_free_min					    \n"
2944 	    "vn.v_free_min: 12669					    \n"
2945 	    );
2946 }
2947 
2948 /*
2949  * Show a specific sysctl similar to sysctl (8).
2950  */
2951 DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN)
2952 {
2953 	char name[TOK_STRING_SIZE];
2954 	int error, i, t, flags;
2955 
2956 	/* Parse the modifiers */
2957 	t = db_read_token();
2958 	if (t == tSLASH || t == tMINUS) {
2959 		t = db_read_token();
2960 		if (t != tIDENT) {
2961 			db_printf("Bad modifier\n");
2962 			error = EINVAL;
2963 			goto out;
2964 		}
2965 		db_strcpy(modif, db_tok_string);
2966 	}
2967 	else {
2968 		db_unread_token(t);
2969 		modif[0] = '\0';
2970 	}
2971 
2972 	flags = 0;
2973 	for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2974 		if (strchr(modif, db_sysctl_modifs[i])) {
2975 			flags |= db_sysctl_modif_values[i];
2976 		}
2977 	}
2978 
2979 	/* Parse the sysctl names */
2980 	t = db_read_token();
2981 	if (t != tIDENT) {
2982 		db_printf("Need sysctl name\n");
2983 		error = EINVAL;
2984 		goto out;
2985 	}
2986 
2987 	/* Copy the name into a temporary buffer */
2988 	db_strcpy(name, db_tok_string);
2989 
2990 	/* Ensure there is no trailing cruft */
2991 	t = db_read_token();
2992 	if (t != tEOL) {
2993 		db_printf("Unexpected sysctl argument\n");
2994 		error = EINVAL;
2995 		goto out;
2996 	}
2997 
2998 	error = db_sysctlbyname(name, flags);
2999 	if (error == ENOENT) {
3000 		db_printf("unknown oid: '%s'\n", db_tok_string);
3001 		goto out;
3002 	} else if (error) {
3003 		db_printf("%s: error: %d\n", db_tok_string, error);
3004 		goto out;
3005 	}
3006 
3007 out:
3008 	/* Ensure we eat all of our text */
3009 	db_flush_lex();
3010 
3011 	if (error == EINVAL) {
3012 		db_sysctl_cmd_usage();
3013 	}
3014 }
3015 
3016 #endif /* DDB */
3017