xref: /freebsd/sys/kern/kern_sysctl.c (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
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 __FBSDID("$FreeBSD$");
42 
43 #include "opt_capsicum.h"
44 #include "opt_ktrace.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/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/jail.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/rmlock.h>
59 #include <sys/sbuf.h>
60 #include <sys/sx.h>
61 #include <sys/sysproto.h>
62 #include <sys/uio.h>
63 #ifdef KTRACE
64 #include <sys/ktrace.h>
65 #endif
66 
67 #include <net/vnet.h>
68 
69 #include <security/mac/mac_framework.h>
70 
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73 
74 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
75 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
76 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
77 
78 /*
79  * The sysctllock protects the MIB tree.  It also protects sysctl
80  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
81  * sysctl_unregister_oid() routines require the sysctllock to already
82  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
83  * provided for the few places in the kernel which need to use that
84  * API rather than using the dynamic API.  Use of the dynamic API is
85  * strongly encouraged for most code.
86  *
87  * The sysctlmemlock is used to limit the amount of user memory wired for
88  * sysctl requests.  This is implemented by serializing any userland
89  * sysctl requests larger than a single page via an exclusive lock.
90  */
91 static struct rmlock sysctllock;
92 static struct sx __exclusive_cache_line sysctlmemlock;
93 
94 #define	SYSCTL_WLOCK()		rm_wlock(&sysctllock)
95 #define	SYSCTL_WUNLOCK()	rm_wunlock(&sysctllock)
96 #define	SYSCTL_RLOCK(tracker)	rm_rlock(&sysctllock, (tracker))
97 #define	SYSCTL_RUNLOCK(tracker)	rm_runlock(&sysctllock, (tracker))
98 #define	SYSCTL_WLOCKED()	rm_wowned(&sysctllock)
99 #define	SYSCTL_ASSERT_LOCKED()	rm_assert(&sysctllock, RA_LOCKED)
100 #define	SYSCTL_ASSERT_WLOCKED()	rm_assert(&sysctllock, RA_WLOCKED)
101 #define	SYSCTL_ASSERT_RLOCKED()	rm_assert(&sysctllock, RA_RLOCKED)
102 #define	SYSCTL_INIT()		rm_init_flags(&sysctllock, "sysctl lock", \
103 				    RM_SLEEPABLE)
104 #define	SYSCTL_SLEEP(ch, wmesg, timo)					\
105 				rm_sleep(ch, &sysctllock, 0, wmesg, timo)
106 
107 static int sysctl_root(SYSCTL_HANDLER_ARGS);
108 
109 /* Root list */
110 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
111 
112 static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
113 		    int recurse);
114 static int	sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
115 static int	sysctl_new_kernel(struct sysctl_req *, void *, size_t);
116 
117 static struct sysctl_oid *
118 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
119 {
120 	struct sysctl_oid *oidp;
121 
122 	SYSCTL_ASSERT_LOCKED();
123 	SLIST_FOREACH(oidp, list, oid_link) {
124 		if (strcmp(oidp->oid_name, name) == 0) {
125 			return (oidp);
126 		}
127 	}
128 	return (NULL);
129 }
130 
131 /*
132  * Initialization of the MIB tree.
133  *
134  * Order by number in each list.
135  */
136 void
137 sysctl_wlock(void)
138 {
139 
140 	SYSCTL_WLOCK();
141 }
142 
143 void
144 sysctl_wunlock(void)
145 {
146 
147 	SYSCTL_WUNLOCK();
148 }
149 
150 static int
151 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
152     struct sysctl_req *req, struct rm_priotracker *tracker)
153 {
154 	int error;
155 
156 	if (oid->oid_kind & CTLFLAG_DYN)
157 		atomic_add_int(&oid->oid_running, 1);
158 
159 	if (tracker != NULL)
160 		SYSCTL_RUNLOCK(tracker);
161 	else
162 		SYSCTL_WUNLOCK();
163 
164 	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
165 		mtx_lock(&Giant);
166 	error = oid->oid_handler(oid, arg1, arg2, req);
167 	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
168 		mtx_unlock(&Giant);
169 
170 	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
171 
172 	if (tracker != NULL)
173 		SYSCTL_RLOCK(tracker);
174 	else
175 		SYSCTL_WLOCK();
176 
177 	if (oid->oid_kind & CTLFLAG_DYN) {
178 		if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
179 		    (oid->oid_kind & CTLFLAG_DYING) != 0)
180 			wakeup(&oid->oid_running);
181 	}
182 
183 	return (error);
184 }
185 
186 static void
187 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
188 {
189 	struct sysctl_req req;
190 	struct sysctl_oid *curr;
191 	char *penv = NULL;
192 	char path[96];
193 	ssize_t rem = sizeof(path);
194 	ssize_t len;
195 	uint8_t data[512] __aligned(sizeof(uint64_t));
196 	int size;
197 	int error;
198 
199 	path[--rem] = 0;
200 
201 	for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
202 		len = strlen(curr->oid_name);
203 		rem -= len;
204 		if (curr != oidp)
205 			rem -= 1;
206 		if (rem < 0) {
207 			printf("OID path exceeds %d bytes\n", (int)sizeof(path));
208 			return;
209 		}
210 		memcpy(path + rem, curr->oid_name, len);
211 		if (curr != oidp)
212 			path[rem + len] = '.';
213 	}
214 
215 	memset(&req, 0, sizeof(req));
216 
217 	req.td = curthread;
218 	req.oldfunc = sysctl_old_kernel;
219 	req.newfunc = sysctl_new_kernel;
220 	req.lock = REQ_UNWIRED;
221 
222 	switch (oidp->oid_kind & CTLTYPE) {
223 	case CTLTYPE_INT:
224 		if (getenv_array(path + rem, data, sizeof(data), &size,
225 		    sizeof(int), GETENV_SIGNED) == 0)
226 			return;
227 		req.newlen = size;
228 		req.newptr = data;
229 		break;
230 	case CTLTYPE_UINT:
231 		if (getenv_array(path + rem, data, sizeof(data), &size,
232 		    sizeof(int), GETENV_UNSIGNED) == 0)
233 			return;
234 		req.newlen = size;
235 		req.newptr = data;
236 		break;
237 	case CTLTYPE_LONG:
238 		if (getenv_array(path + rem, data, sizeof(data), &size,
239 		    sizeof(long), GETENV_SIGNED) == 0)
240 			return;
241 		req.newlen = size;
242 		req.newptr = data;
243 		break;
244 	case CTLTYPE_ULONG:
245 		if (getenv_array(path + rem, data, sizeof(data), &size,
246 		    sizeof(long), GETENV_UNSIGNED) == 0)
247 			return;
248 		req.newlen = size;
249 		req.newptr = data;
250 		break;
251 	case CTLTYPE_S8:
252 		if (getenv_array(path + rem, data, sizeof(data), &size,
253 		    sizeof(int8_t), GETENV_SIGNED) == 0)
254 			return;
255 		req.newlen = size;
256 		req.newptr = data;
257 		break;
258 	case CTLTYPE_S16:
259 		if (getenv_array(path + rem, data, sizeof(data), &size,
260 		    sizeof(int16_t), GETENV_SIGNED) == 0)
261 			return;
262 		req.newlen = size;
263 		req.newptr = data;
264 		break;
265 	case CTLTYPE_S32:
266 		if (getenv_array(path + rem, data, sizeof(data), &size,
267 		    sizeof(int32_t), GETENV_SIGNED) == 0)
268 			return;
269 		req.newlen = size;
270 		req.newptr = data;
271 		break;
272 	case CTLTYPE_S64:
273 		if (getenv_array(path + rem, data, sizeof(data), &size,
274 		    sizeof(int64_t), GETENV_SIGNED) == 0)
275 			return;
276 		req.newlen = size;
277 		req.newptr = data;
278 		break;
279 	case CTLTYPE_U8:
280 		if (getenv_array(path + rem, data, sizeof(data), &size,
281 		    sizeof(uint8_t), GETENV_UNSIGNED) == 0)
282 			return;
283 		req.newlen = size;
284 		req.newptr = data;
285 		break;
286 	case CTLTYPE_U16:
287 		if (getenv_array(path + rem, data, sizeof(data), &size,
288 		    sizeof(uint16_t), GETENV_UNSIGNED) == 0)
289 			return;
290 		req.newlen = size;
291 		req.newptr = data;
292 		break;
293 	case CTLTYPE_U32:
294 		if (getenv_array(path + rem, data, sizeof(data), &size,
295 		    sizeof(uint32_t), GETENV_UNSIGNED) == 0)
296 			return;
297 		req.newlen = size;
298 		req.newptr = data;
299 		break;
300 	case CTLTYPE_U64:
301 		if (getenv_array(path + rem, data, sizeof(data), &size,
302 		    sizeof(uint64_t), GETENV_UNSIGNED) == 0)
303 			return;
304 		req.newlen = size;
305 		req.newptr = data;
306 		break;
307 	case CTLTYPE_STRING:
308 		penv = kern_getenv(path + rem);
309 		if (penv == NULL)
310 			return;
311 		req.newlen = strlen(penv);
312 		req.newptr = penv;
313 		break;
314 	default:
315 		return;
316 	}
317 	error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
318 	    oidp->oid_arg2, &req, NULL);
319 	if (error != 0)
320 		printf("Setting sysctl %s failed: %d\n", path + rem, error);
321 	if (penv != NULL)
322 		freeenv(penv);
323 }
324 
325 static int
326 sbuf_printf_drain(void *arg __unused, const char *data, int len)
327 {
328 
329 	return (printf("%.*s", len, data));
330 }
331 
332 /*
333  * Locate the path to a given oid.  Returns the length of the resulting path,
334  * or -1 if the oid was not found.  nodes must have room for CTL_MAXNAME
335  * elements and be NULL initialized.
336  */
337 static int
338 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
339 {
340 	int indx;
341 
342 	SYSCTL_ASSERT_LOCKED();
343 	indx = 0;
344 	while (indx < CTL_MAXNAME && indx >= 0) {
345 		if (nodes[indx] == NULL && indx == 0)
346 			nodes[indx] = SLIST_FIRST(&sysctl__children);
347 		else if (nodes[indx] == NULL)
348 			nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
349 		else
350 			nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
351 
352 		if (nodes[indx] == needle)
353 			return (indx + 1);
354 
355 		if (nodes[indx] == NULL) {
356 			indx--;
357 			continue;
358 		}
359 
360 		if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
361 			indx++;
362 			continue;
363 		}
364 	}
365 	return (-1);
366 }
367 
368 static void
369 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
370 {
371 	struct sysctl_oid *nodes[CTL_MAXNAME];
372 	char buf[128];
373 	struct sbuf sb;
374 	int rc, i;
375 
376 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
377 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
378 
379 	sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
380 
381 	memset(nodes, 0, sizeof(nodes));
382 	rc = sysctl_search_oid(nodes, leaf);
383 	if (rc > 0) {
384 		for (i = 0; i < rc; i++)
385 			sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
386 			    i != (rc - 1), ".");
387 	} else {
388 		sbuf_printf(&sb, "%s", leaf->oid_name);
389 	}
390 	sbuf_printf(&sb, ")!\n");
391 
392 	(void)sbuf_finish(&sb);
393 }
394 
395 #ifdef SYSCTL_DEBUG
396 static int
397 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
398 {
399 	struct rm_priotracker tracker;
400 
401 	SYSCTL_RLOCK(&tracker);
402 	sysctl_warn_reuse(__func__, oidp);
403 	SYSCTL_RUNLOCK(&tracker);
404 	return (0);
405 }
406 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
407 	0, 0, sysctl_reuse_test, "-", "");
408 #endif
409 
410 void
411 sysctl_register_oid(struct sysctl_oid *oidp)
412 {
413 	struct sysctl_oid_list *parent = oidp->oid_parent;
414 	struct sysctl_oid *p;
415 	struct sysctl_oid *q;
416 	int oid_number;
417 	int timeout = 2;
418 
419 	/*
420 	 * First check if another oid with the same name already
421 	 * exists in the parent's list.
422 	 */
423 	SYSCTL_ASSERT_WLOCKED();
424 	p = sysctl_find_oidname(oidp->oid_name, parent);
425 	if (p != NULL) {
426 		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
427 			p->oid_refcnt++;
428 			return;
429 		} else {
430 			sysctl_warn_reuse(__func__, p);
431 			return;
432 		}
433 	}
434 	/* get current OID number */
435 	oid_number = oidp->oid_number;
436 
437 #if (OID_AUTO >= 0)
438 #error "OID_AUTO is expected to be a negative value"
439 #endif
440 	/*
441 	 * Any negative OID number qualifies as OID_AUTO. Valid OID
442 	 * numbers should always be positive.
443 	 *
444 	 * NOTE: DO NOT change the starting value here, change it in
445 	 * <sys/sysctl.h>, and make sure it is at least 256 to
446 	 * accommodate e.g. net.inet.raw as a static sysctl node.
447 	 */
448 	if (oid_number < 0) {
449 		static int newoid;
450 
451 		/*
452 		 * By decrementing the next OID number we spend less
453 		 * time inserting the OIDs into a sorted list.
454 		 */
455 		if (--newoid < CTL_AUTO_START)
456 			newoid = 0x7fffffff;
457 
458 		oid_number = newoid;
459 	}
460 
461 	/*
462 	 * Insert the OID into the parent's list sorted by OID number.
463 	 */
464 retry:
465 	q = NULL;
466 	SLIST_FOREACH(p, parent, oid_link) {
467 		/* check if the current OID number is in use */
468 		if (oid_number == p->oid_number) {
469 			/* get the next valid OID number */
470 			if (oid_number < CTL_AUTO_START ||
471 			    oid_number == 0x7fffffff) {
472 				/* wraparound - restart */
473 				oid_number = CTL_AUTO_START;
474 				/* don't loop forever */
475 				if (!timeout--)
476 					panic("sysctl: Out of OID numbers\n");
477 				goto retry;
478 			} else {
479 				oid_number++;
480 			}
481 		} else if (oid_number < p->oid_number)
482 			break;
483 		q = p;
484 	}
485 	/* check for non-auto OID number collision */
486 	if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
487 	    oid_number >= CTL_AUTO_START) {
488 		printf("sysctl: OID number(%d) is already in use for '%s'\n",
489 		    oidp->oid_number, oidp->oid_name);
490 	}
491 	/* update the OID number, if any */
492 	oidp->oid_number = oid_number;
493 	if (q != NULL)
494 		SLIST_INSERT_AFTER(q, oidp, oid_link);
495 	else
496 		SLIST_INSERT_HEAD(parent, oidp, oid_link);
497 
498 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
499 #ifdef VIMAGE
500 	    (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
501 #endif
502 	    (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
503 	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
504 		/* only fetch value once */
505 		oidp->oid_kind |= CTLFLAG_NOFETCH;
506 		/* try to fetch value from kernel environment */
507 		sysctl_load_tunable_by_oid_locked(oidp);
508 	}
509 }
510 
511 void
512 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
513 {
514 
515 	/*
516 	 * Mark the leaf as dormant if it's not to be immediately enabled.
517 	 * We do not disable nodes as they can be shared between modules
518 	 * and it is always safe to access a node.
519 	 */
520 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
521 	    ("internal flag is set in oid_kind"));
522 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
523 		oidp->oid_kind |= CTLFLAG_DORMANT;
524 	sysctl_register_oid(oidp);
525 }
526 
527 void
528 sysctl_enable_oid(struct sysctl_oid *oidp)
529 {
530 
531 	SYSCTL_ASSERT_WLOCKED();
532 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
533 		KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
534 		    ("sysctl node is marked as dormant"));
535 		return;
536 	}
537 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
538 	    ("enabling already enabled sysctl oid"));
539 	oidp->oid_kind &= ~CTLFLAG_DORMANT;
540 }
541 
542 void
543 sysctl_unregister_oid(struct sysctl_oid *oidp)
544 {
545 	struct sysctl_oid *p;
546 	int error;
547 
548 	SYSCTL_ASSERT_WLOCKED();
549 	error = ENOENT;
550 	if (oidp->oid_number == OID_AUTO) {
551 		error = EINVAL;
552 	} else {
553 		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
554 			if (p == oidp) {
555 				SLIST_REMOVE(oidp->oid_parent, oidp,
556 				    sysctl_oid, oid_link);
557 				error = 0;
558 				break;
559 			}
560 		}
561 	}
562 
563 	/*
564 	 * This can happen when a module fails to register and is
565 	 * being unloaded afterwards.  It should not be a panic()
566 	 * for normal use.
567 	 */
568 	if (error)
569 		printf("%s: failed to unregister sysctl\n", __func__);
570 }
571 
572 /* Initialize a new context to keep track of dynamically added sysctls. */
573 int
574 sysctl_ctx_init(struct sysctl_ctx_list *c)
575 {
576 
577 	if (c == NULL) {
578 		return (EINVAL);
579 	}
580 
581 	/*
582 	 * No locking here, the caller is responsible for not adding
583 	 * new nodes to a context until after this function has
584 	 * returned.
585 	 */
586 	TAILQ_INIT(c);
587 	return (0);
588 }
589 
590 /* Free the context, and destroy all dynamic oids registered in this context */
591 int
592 sysctl_ctx_free(struct sysctl_ctx_list *clist)
593 {
594 	struct sysctl_ctx_entry *e, *e1;
595 	int error;
596 
597 	error = 0;
598 	/*
599 	 * First perform a "dry run" to check if it's ok to remove oids.
600 	 * XXX FIXME
601 	 * XXX This algorithm is a hack. But I don't know any
602 	 * XXX better solution for now...
603 	 */
604 	SYSCTL_WLOCK();
605 	TAILQ_FOREACH(e, clist, link) {
606 		error = sysctl_remove_oid_locked(e->entry, 0, 0);
607 		if (error)
608 			break;
609 	}
610 	/*
611 	 * Restore deregistered entries, either from the end,
612 	 * or from the place where error occurred.
613 	 * e contains the entry that was not unregistered
614 	 */
615 	if (error)
616 		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
617 	else
618 		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
619 	while (e1 != NULL) {
620 		sysctl_register_oid(e1->entry);
621 		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
622 	}
623 	if (error) {
624 		SYSCTL_WUNLOCK();
625 		return(EBUSY);
626 	}
627 	/* Now really delete the entries */
628 	e = TAILQ_FIRST(clist);
629 	while (e != NULL) {
630 		e1 = TAILQ_NEXT(e, link);
631 		error = sysctl_remove_oid_locked(e->entry, 1, 0);
632 		if (error)
633 			panic("sysctl_remove_oid: corrupt tree, entry: %s",
634 			    e->entry->oid_name);
635 		free(e, M_SYSCTLOID);
636 		e = e1;
637 	}
638 	SYSCTL_WUNLOCK();
639 	return (error);
640 }
641 
642 /* Add an entry to the context */
643 struct sysctl_ctx_entry *
644 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
645 {
646 	struct sysctl_ctx_entry *e;
647 
648 	SYSCTL_ASSERT_WLOCKED();
649 	if (clist == NULL || oidp == NULL)
650 		return(NULL);
651 	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
652 	e->entry = oidp;
653 	TAILQ_INSERT_HEAD(clist, e, link);
654 	return (e);
655 }
656 
657 /* Find an entry in the context */
658 struct sysctl_ctx_entry *
659 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
660 {
661 	struct sysctl_ctx_entry *e;
662 
663 	SYSCTL_ASSERT_WLOCKED();
664 	if (clist == NULL || oidp == NULL)
665 		return(NULL);
666 	TAILQ_FOREACH(e, clist, link) {
667 		if(e->entry == oidp)
668 			return(e);
669 	}
670 	return (e);
671 }
672 
673 /*
674  * Delete an entry from the context.
675  * NOTE: this function doesn't free oidp! You have to remove it
676  * with sysctl_remove_oid().
677  */
678 int
679 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
680 {
681 	struct sysctl_ctx_entry *e;
682 
683 	if (clist == NULL || oidp == NULL)
684 		return (EINVAL);
685 	SYSCTL_WLOCK();
686 	e = sysctl_ctx_entry_find(clist, oidp);
687 	if (e != NULL) {
688 		TAILQ_REMOVE(clist, e, link);
689 		SYSCTL_WUNLOCK();
690 		free(e, M_SYSCTLOID);
691 		return (0);
692 	} else {
693 		SYSCTL_WUNLOCK();
694 		return (ENOENT);
695 	}
696 }
697 
698 /*
699  * Remove dynamically created sysctl trees.
700  * oidp - top of the tree to be removed
701  * del - if 0 - just deregister, otherwise free up entries as well
702  * recurse - if != 0 traverse the subtree to be deleted
703  */
704 int
705 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
706 {
707 	int error;
708 
709 	SYSCTL_WLOCK();
710 	error = sysctl_remove_oid_locked(oidp, del, recurse);
711 	SYSCTL_WUNLOCK();
712 	return (error);
713 }
714 
715 int
716 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
717     int del, int recurse)
718 {
719 	struct sysctl_oid *p, *tmp;
720 	int error;
721 
722 	error = ENOENT;
723 	SYSCTL_WLOCK();
724 	SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
725 		if (strcmp(p->oid_name, name) == 0) {
726 			error = sysctl_remove_oid_locked(p, del, recurse);
727 			break;
728 		}
729 	}
730 	SYSCTL_WUNLOCK();
731 
732 	return (error);
733 }
734 
735 
736 static int
737 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
738 {
739 	struct sysctl_oid *p, *tmp;
740 	int error;
741 
742 	SYSCTL_ASSERT_WLOCKED();
743 	if (oidp == NULL)
744 		return(EINVAL);
745 	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
746 		printf("Warning: can't remove non-dynamic nodes (%s)!\n",
747 		    oidp->oid_name);
748 		return (EINVAL);
749 	}
750 	/*
751 	 * WARNING: normal method to do this should be through
752 	 * sysctl_ctx_free(). Use recursing as the last resort
753 	 * method to purge your sysctl tree of leftovers...
754 	 * However, if some other code still references these nodes,
755 	 * it will panic.
756 	 */
757 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
758 		if (oidp->oid_refcnt == 1) {
759 			SLIST_FOREACH_SAFE(p,
760 			    SYSCTL_CHILDREN(oidp), oid_link, tmp) {
761 				if (!recurse) {
762 					printf("Warning: failed attempt to "
763 					    "remove oid %s with child %s\n",
764 					    oidp->oid_name, p->oid_name);
765 					return (ENOTEMPTY);
766 				}
767 				error = sysctl_remove_oid_locked(p, del,
768 				    recurse);
769 				if (error)
770 					return (error);
771 			}
772 		}
773 	}
774 	if (oidp->oid_refcnt > 1 ) {
775 		oidp->oid_refcnt--;
776 	} else {
777 		if (oidp->oid_refcnt == 0) {
778 			printf("Warning: bad oid_refcnt=%u (%s)!\n",
779 				oidp->oid_refcnt, oidp->oid_name);
780 			return (EINVAL);
781 		}
782 		sysctl_unregister_oid(oidp);
783 		if (del) {
784 			/*
785 			 * Wait for all threads running the handler to drain.
786 			 * This preserves the previous behavior when the
787 			 * sysctl lock was held across a handler invocation,
788 			 * and is necessary for module unload correctness.
789 			 */
790 			while (oidp->oid_running > 0) {
791 				oidp->oid_kind |= CTLFLAG_DYING;
792 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
793 			}
794 			if (oidp->oid_descr)
795 				free(__DECONST(char *, oidp->oid_descr),
796 				    M_SYSCTLOID);
797 			if (oidp->oid_label)
798 				free(__DECONST(char *, oidp->oid_label),
799 				    M_SYSCTLOID);
800 			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
801 			free(oidp, M_SYSCTLOID);
802 		}
803 	}
804 	return (0);
805 }
806 /*
807  * Create new sysctls at run time.
808  * clist may point to a valid context initialized with sysctl_ctx_init().
809  */
810 struct sysctl_oid *
811 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
812 	int number, const char *name, int kind, void *arg1, intmax_t arg2,
813 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
814 	const char *label)
815 {
816 	struct sysctl_oid *oidp;
817 
818 	/* You have to hook up somewhere.. */
819 	if (parent == NULL)
820 		return(NULL);
821 	/* Check if the node already exists, otherwise create it */
822 	SYSCTL_WLOCK();
823 	oidp = sysctl_find_oidname(name, parent);
824 	if (oidp != NULL) {
825 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
826 			oidp->oid_refcnt++;
827 			/* Update the context */
828 			if (clist != NULL)
829 				sysctl_ctx_entry_add(clist, oidp);
830 			SYSCTL_WUNLOCK();
831 			return (oidp);
832 		} else {
833 			sysctl_warn_reuse(__func__, oidp);
834 			SYSCTL_WUNLOCK();
835 			return (NULL);
836 		}
837 	}
838 	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
839 	oidp->oid_parent = parent;
840 	SLIST_INIT(&oidp->oid_children);
841 	oidp->oid_number = number;
842 	oidp->oid_refcnt = 1;
843 	oidp->oid_name = strdup(name, M_SYSCTLOID);
844 	oidp->oid_handler = handler;
845 	oidp->oid_kind = CTLFLAG_DYN | kind;
846 	oidp->oid_arg1 = arg1;
847 	oidp->oid_arg2 = arg2;
848 	oidp->oid_fmt = fmt;
849 	if (descr != NULL)
850 		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
851 	if (label != NULL)
852 		oidp->oid_label = strdup(label, M_SYSCTLOID);
853 	/* Update the context, if used */
854 	if (clist != NULL)
855 		sysctl_ctx_entry_add(clist, oidp);
856 	/* Register this oid */
857 	sysctl_register_oid(oidp);
858 	SYSCTL_WUNLOCK();
859 	return (oidp);
860 }
861 
862 /*
863  * Rename an existing oid.
864  */
865 void
866 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
867 {
868 	char *newname;
869 	char *oldname;
870 
871 	newname = strdup(name, M_SYSCTLOID);
872 	SYSCTL_WLOCK();
873 	oldname = __DECONST(char *, oidp->oid_name);
874 	oidp->oid_name = newname;
875 	SYSCTL_WUNLOCK();
876 	free(oldname, M_SYSCTLOID);
877 }
878 
879 /*
880  * Reparent an existing oid.
881  */
882 int
883 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
884 {
885 	struct sysctl_oid *oidp;
886 
887 	SYSCTL_WLOCK();
888 	if (oid->oid_parent == parent) {
889 		SYSCTL_WUNLOCK();
890 		return (0);
891 	}
892 	oidp = sysctl_find_oidname(oid->oid_name, parent);
893 	if (oidp != NULL) {
894 		SYSCTL_WUNLOCK();
895 		return (EEXIST);
896 	}
897 	sysctl_unregister_oid(oid);
898 	oid->oid_parent = parent;
899 	oid->oid_number = OID_AUTO;
900 	sysctl_register_oid(oid);
901 	SYSCTL_WUNLOCK();
902 	return (0);
903 }
904 
905 /*
906  * Register the kernel's oids on startup.
907  */
908 SET_DECLARE(sysctl_set, struct sysctl_oid);
909 
910 static void
911 sysctl_register_all(void *arg)
912 {
913 	struct sysctl_oid **oidp;
914 
915 	sx_init(&sysctlmemlock, "sysctl mem");
916 	SYSCTL_INIT();
917 	SYSCTL_WLOCK();
918 	SET_FOREACH(oidp, sysctl_set)
919 		sysctl_register_oid(*oidp);
920 	SYSCTL_WUNLOCK();
921 }
922 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
923 
924 /*
925  * "Staff-functions"
926  *
927  * These functions implement a presently undocumented interface
928  * used by the sysctl program to walk the tree, and get the type
929  * so it can print the value.
930  * This interface is under work and consideration, and should probably
931  * be killed with a big axe by the first person who can find the time.
932  * (be aware though, that the proper interface isn't as obvious as it
933  * may seem, there are various conflicting requirements.
934  *
935  * {0,0}	printf the entire MIB-tree.
936  * {0,1,...}	return the name of the "..." OID.
937  * {0,2,...}	return the next OID.
938  * {0,3}	return the OID of the name in "new"
939  * {0,4,...}	return the kind & format info for the "..." OID.
940  * {0,5,...}	return the description of the "..." OID.
941  * {0,6,...}	return the aggregation label of the "..." OID.
942  */
943 
944 #ifdef SYSCTL_DEBUG
945 static void
946 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
947 {
948 	int k;
949 	struct sysctl_oid *oidp;
950 
951 	SYSCTL_ASSERT_LOCKED();
952 	SLIST_FOREACH(oidp, l, oid_link) {
953 
954 		for (k=0; k<i; k++)
955 			printf(" ");
956 
957 		printf("%d %s ", oidp->oid_number, oidp->oid_name);
958 
959 		printf("%c%c",
960 			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
961 			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
962 
963 		if (oidp->oid_handler)
964 			printf(" *Handler");
965 
966 		switch (oidp->oid_kind & CTLTYPE) {
967 			case CTLTYPE_NODE:
968 				printf(" Node\n");
969 				if (!oidp->oid_handler) {
970 					sysctl_sysctl_debug_dump_node(
971 					    SYSCTL_CHILDREN(oidp), i + 2);
972 				}
973 				break;
974 			case CTLTYPE_INT:    printf(" Int\n"); break;
975 			case CTLTYPE_UINT:   printf(" u_int\n"); break;
976 			case CTLTYPE_LONG:   printf(" Long\n"); break;
977 			case CTLTYPE_ULONG:  printf(" u_long\n"); break;
978 			case CTLTYPE_STRING: printf(" String\n"); break;
979 			case CTLTYPE_S8:     printf(" int8_t\n"); break;
980 			case CTLTYPE_S16:    printf(" int16_t\n"); break;
981 			case CTLTYPE_S32:    printf(" int32_t\n"); break;
982 			case CTLTYPE_S64:    printf(" int64_t\n"); break;
983 			case CTLTYPE_U8:     printf(" uint8_t\n"); break;
984 			case CTLTYPE_U16:    printf(" uint16_t\n"); break;
985 			case CTLTYPE_U32:    printf(" uint32_t\n"); break;
986 			case CTLTYPE_U64:    printf(" uint64_t\n"); break;
987 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
988 			default:	     printf("\n");
989 		}
990 
991 	}
992 }
993 
994 static int
995 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
996 {
997 	struct rm_priotracker tracker;
998 	int error;
999 
1000 	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1001 	if (error)
1002 		return (error);
1003 	SYSCTL_RLOCK(&tracker);
1004 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1005 	SYSCTL_RUNLOCK(&tracker);
1006 	return (ENOENT);
1007 }
1008 
1009 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
1010 	0, 0, sysctl_sysctl_debug, "-", "");
1011 #endif
1012 
1013 static int
1014 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1015 {
1016 	int *name = (int *) arg1;
1017 	u_int namelen = arg2;
1018 	int error = 0;
1019 	struct sysctl_oid *oid;
1020 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1021 	struct rm_priotracker tracker;
1022 	char buf[10];
1023 
1024 	SYSCTL_RLOCK(&tracker);
1025 	while (namelen) {
1026 		if (!lsp) {
1027 			snprintf(buf,sizeof(buf),"%d",*name);
1028 			if (req->oldidx)
1029 				error = SYSCTL_OUT(req, ".", 1);
1030 			if (!error)
1031 				error = SYSCTL_OUT(req, buf, strlen(buf));
1032 			if (error)
1033 				goto out;
1034 			namelen--;
1035 			name++;
1036 			continue;
1037 		}
1038 		lsp2 = NULL;
1039 		SLIST_FOREACH(oid, lsp, oid_link) {
1040 			if (oid->oid_number != *name)
1041 				continue;
1042 
1043 			if (req->oldidx)
1044 				error = SYSCTL_OUT(req, ".", 1);
1045 			if (!error)
1046 				error = SYSCTL_OUT(req, oid->oid_name,
1047 					strlen(oid->oid_name));
1048 			if (error)
1049 				goto out;
1050 
1051 			namelen--;
1052 			name++;
1053 
1054 			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1055 				break;
1056 
1057 			if (oid->oid_handler)
1058 				break;
1059 
1060 			lsp2 = SYSCTL_CHILDREN(oid);
1061 			break;
1062 		}
1063 		lsp = lsp2;
1064 	}
1065 	error = SYSCTL_OUT(req, "", 1);
1066  out:
1067 	SYSCTL_RUNLOCK(&tracker);
1068 	return (error);
1069 }
1070 
1071 /*
1072  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1073  * capability mode.
1074  */
1075 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1076     sysctl_sysctl_name, "");
1077 
1078 static int
1079 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1080 	int *next, int *len, int level, struct sysctl_oid **oidpp)
1081 {
1082 	struct sysctl_oid *oidp;
1083 
1084 	SYSCTL_ASSERT_LOCKED();
1085 	*len = level;
1086 	SLIST_FOREACH(oidp, lsp, oid_link) {
1087 		*next = oidp->oid_number;
1088 		*oidpp = oidp;
1089 
1090 		if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1091 			continue;
1092 
1093 		if (!namelen) {
1094 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1095 				return (0);
1096 			if (oidp->oid_handler)
1097 				/* We really should call the handler here...*/
1098 				return (0);
1099 			lsp = SYSCTL_CHILDREN(oidp);
1100 			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1101 				len, level+1, oidpp))
1102 				return (0);
1103 			goto emptynode;
1104 		}
1105 
1106 		if (oidp->oid_number < *name)
1107 			continue;
1108 
1109 		if (oidp->oid_number > *name) {
1110 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1111 				return (0);
1112 			if (oidp->oid_handler)
1113 				return (0);
1114 			lsp = SYSCTL_CHILDREN(oidp);
1115 			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1116 				next+1, len, level+1, oidpp))
1117 				return (0);
1118 			goto next;
1119 		}
1120 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1121 			continue;
1122 
1123 		if (oidp->oid_handler)
1124 			continue;
1125 
1126 		lsp = SYSCTL_CHILDREN(oidp);
1127 		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1128 			len, level+1, oidpp))
1129 			return (0);
1130 	next:
1131 		namelen = 1;
1132 	emptynode:
1133 		*len = level;
1134 	}
1135 	return (1);
1136 }
1137 
1138 static int
1139 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1140 {
1141 	int *name = (int *) arg1;
1142 	u_int namelen = arg2;
1143 	int i, j, error;
1144 	struct sysctl_oid *oid;
1145 	struct sysctl_oid_list *lsp = &sysctl__children;
1146 	struct rm_priotracker tracker;
1147 	int newoid[CTL_MAXNAME];
1148 
1149 	SYSCTL_RLOCK(&tracker);
1150 	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1151 	SYSCTL_RUNLOCK(&tracker);
1152 	if (i)
1153 		return (ENOENT);
1154 	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1155 	return (error);
1156 }
1157 
1158 /*
1159  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1160  * capability mode.
1161  */
1162 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1163     sysctl_sysctl_next, "");
1164 
1165 static int
1166 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1167 {
1168 	struct sysctl_oid *oidp;
1169 	struct sysctl_oid_list *lsp = &sysctl__children;
1170 	char *p;
1171 
1172 	SYSCTL_ASSERT_LOCKED();
1173 
1174 	for (*len = 0; *len < CTL_MAXNAME;) {
1175 		p = strsep(&name, ".");
1176 
1177 		oidp = SLIST_FIRST(lsp);
1178 		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1179 			if (oidp == NULL)
1180 				return (ENOENT);
1181 			if (strcmp(p, oidp->oid_name) == 0)
1182 				break;
1183 		}
1184 		*oid++ = oidp->oid_number;
1185 		(*len)++;
1186 
1187 		if (name == NULL || *name == '\0') {
1188 			if (oidpp)
1189 				*oidpp = oidp;
1190 			return (0);
1191 		}
1192 
1193 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1194 			break;
1195 
1196 		if (oidp->oid_handler)
1197 			break;
1198 
1199 		lsp = SYSCTL_CHILDREN(oidp);
1200 	}
1201 	return (ENOENT);
1202 }
1203 
1204 static int
1205 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1206 {
1207 	char *p;
1208 	int error, oid[CTL_MAXNAME], len = 0;
1209 	struct sysctl_oid *op = NULL;
1210 	struct rm_priotracker tracker;
1211 	char buf[32];
1212 
1213 	if (!req->newlen)
1214 		return (ENOENT);
1215 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
1216 		return (ENAMETOOLONG);
1217 
1218 	p = buf;
1219 	if (req->newlen >= sizeof(buf))
1220 		p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1221 
1222 	error = SYSCTL_IN(req, p, req->newlen);
1223 	if (error) {
1224 		if (p != buf)
1225 			free(p, M_SYSCTL);
1226 		return (error);
1227 	}
1228 
1229 	p [req->newlen] = '\0';
1230 
1231 	SYSCTL_RLOCK(&tracker);
1232 	error = name2oid(p, oid, &len, &op);
1233 	SYSCTL_RUNLOCK(&tracker);
1234 
1235 	if (p != buf)
1236 		free(p, M_SYSCTL);
1237 
1238 	if (error)
1239 		return (error);
1240 
1241 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1242 	return (error);
1243 }
1244 
1245 /*
1246  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1247  * capability mode.
1248  */
1249 SYSCTL_PROC(_sysctl, 3, name2oid,
1250     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1251     | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1252 
1253 static int
1254 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1255 {
1256 	struct sysctl_oid *oid;
1257 	struct rm_priotracker tracker;
1258 	int error;
1259 
1260 	SYSCTL_RLOCK(&tracker);
1261 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1262 	if (error)
1263 		goto out;
1264 
1265 	if (oid->oid_fmt == NULL) {
1266 		error = ENOENT;
1267 		goto out;
1268 	}
1269 	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1270 	if (error)
1271 		goto out;
1272 	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1273  out:
1274 	SYSCTL_RUNLOCK(&tracker);
1275 	return (error);
1276 }
1277 
1278 
1279 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1280     sysctl_sysctl_oidfmt, "");
1281 
1282 static int
1283 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1284 {
1285 	struct sysctl_oid *oid;
1286 	struct rm_priotracker tracker;
1287 	int error;
1288 
1289 	SYSCTL_RLOCK(&tracker);
1290 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1291 	if (error)
1292 		goto out;
1293 
1294 	if (oid->oid_descr == NULL) {
1295 		error = ENOENT;
1296 		goto out;
1297 	}
1298 	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1299  out:
1300 	SYSCTL_RUNLOCK(&tracker);
1301 	return (error);
1302 }
1303 
1304 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1305     sysctl_sysctl_oiddescr, "");
1306 
1307 static int
1308 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1309 {
1310 	struct sysctl_oid *oid;
1311 	struct rm_priotracker tracker;
1312 	int error;
1313 
1314 	SYSCTL_RLOCK(&tracker);
1315 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1316 	if (error)
1317 		goto out;
1318 
1319 	if (oid->oid_label == NULL) {
1320 		error = ENOENT;
1321 		goto out;
1322 	}
1323 	error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1324  out:
1325 	SYSCTL_RUNLOCK(&tracker);
1326 	return (error);
1327 }
1328 
1329 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1330     CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1331 
1332 /*
1333  * Default "handler" functions.
1334  */
1335 
1336 /*
1337  * Handle a bool.
1338  * Two cases:
1339  *     a variable:  point arg1 at it.
1340  *     a constant:  pass it in arg2.
1341  */
1342 
1343 int
1344 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1345 {
1346 	uint8_t temp;
1347 	int error;
1348 
1349 	/*
1350 	 * Attempt to get a coherent snapshot by making a copy of the data.
1351 	 */
1352 	if (arg1)
1353 		temp = *(bool *)arg1 ? 1 : 0;
1354 	else
1355 		temp = arg2 ? 1 : 0;
1356 
1357 	error = SYSCTL_OUT(req, &temp, sizeof(temp));
1358 	if (error || !req->newptr)
1359 		return (error);
1360 
1361 	if (!arg1)
1362 		error = EPERM;
1363 	else {
1364 		error = SYSCTL_IN(req, &temp, sizeof(temp));
1365 		if (!error)
1366 			*(bool *)arg1 = temp ? 1 : 0;
1367 	}
1368 	return (error);
1369 }
1370 
1371 /*
1372  * Handle an int8_t, signed or unsigned.
1373  * Two cases:
1374  *     a variable:  point arg1 at it.
1375  *     a constant:  pass it in arg2.
1376  */
1377 
1378 int
1379 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1380 {
1381 	int8_t tmpout;
1382 	int error = 0;
1383 
1384 	/*
1385 	 * Attempt to get a coherent snapshot by making a copy of the data.
1386 	 */
1387 	if (arg1)
1388 		tmpout = *(int8_t *)arg1;
1389 	else
1390 		tmpout = arg2;
1391 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1392 
1393 	if (error || !req->newptr)
1394 		return (error);
1395 
1396 	if (!arg1)
1397 		error = EPERM;
1398 	else
1399 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1400 	return (error);
1401 }
1402 
1403 /*
1404  * Handle an int16_t, signed or unsigned.
1405  * Two cases:
1406  *     a variable:  point arg1 at it.
1407  *     a constant:  pass it in arg2.
1408  */
1409 
1410 int
1411 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1412 {
1413 	int16_t tmpout;
1414 	int error = 0;
1415 
1416 	/*
1417 	 * Attempt to get a coherent snapshot by making a copy of the data.
1418 	 */
1419 	if (arg1)
1420 		tmpout = *(int16_t *)arg1;
1421 	else
1422 		tmpout = arg2;
1423 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1424 
1425 	if (error || !req->newptr)
1426 		return (error);
1427 
1428 	if (!arg1)
1429 		error = EPERM;
1430 	else
1431 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1432 	return (error);
1433 }
1434 
1435 /*
1436  * Handle an int32_t, signed or unsigned.
1437  * Two cases:
1438  *     a variable:  point arg1 at it.
1439  *     a constant:  pass it in arg2.
1440  */
1441 
1442 int
1443 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1444 {
1445 	int32_t tmpout;
1446 	int error = 0;
1447 
1448 	/*
1449 	 * Attempt to get a coherent snapshot by making a copy of the data.
1450 	 */
1451 	if (arg1)
1452 		tmpout = *(int32_t *)arg1;
1453 	else
1454 		tmpout = arg2;
1455 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1456 
1457 	if (error || !req->newptr)
1458 		return (error);
1459 
1460 	if (!arg1)
1461 		error = EPERM;
1462 	else
1463 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1464 	return (error);
1465 }
1466 
1467 /*
1468  * Handle an int, signed or unsigned.
1469  * Two cases:
1470  *     a variable:  point arg1 at it.
1471  *     a constant:  pass it in arg2.
1472  */
1473 
1474 int
1475 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1476 {
1477 	int tmpout, error = 0;
1478 
1479 	/*
1480 	 * Attempt to get a coherent snapshot by making a copy of the data.
1481 	 */
1482 	if (arg1)
1483 		tmpout = *(int *)arg1;
1484 	else
1485 		tmpout = arg2;
1486 	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1487 
1488 	if (error || !req->newptr)
1489 		return (error);
1490 
1491 	if (!arg1)
1492 		error = EPERM;
1493 	else
1494 		error = SYSCTL_IN(req, arg1, sizeof(int));
1495 	return (error);
1496 }
1497 
1498 /*
1499  * Based on on sysctl_handle_int() convert milliseconds into ticks.
1500  * Note: this is used by TCP.
1501  */
1502 
1503 int
1504 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1505 {
1506 	int error, s, tt;
1507 
1508 	tt = *(int *)arg1;
1509 	s = (int)((int64_t)tt * 1000 / hz);
1510 
1511 	error = sysctl_handle_int(oidp, &s, 0, req);
1512 	if (error || !req->newptr)
1513 		return (error);
1514 
1515 	tt = (int)((int64_t)s * hz / 1000);
1516 	if (tt < 1)
1517 		return (EINVAL);
1518 
1519 	*(int *)arg1 = tt;
1520 	return (0);
1521 }
1522 
1523 
1524 /*
1525  * Handle a long, signed or unsigned.
1526  * Two cases:
1527  *     a variable:  point arg1 at it.
1528  *     a constant:  pass it in arg2.
1529  */
1530 
1531 int
1532 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1533 {
1534 	int error = 0;
1535 	long tmplong;
1536 #ifdef SCTL_MASK32
1537 	int tmpint;
1538 #endif
1539 
1540 	/*
1541 	 * Attempt to get a coherent snapshot by making a copy of the data.
1542 	 */
1543 	if (arg1)
1544 		tmplong = *(long *)arg1;
1545 	else
1546 		tmplong = arg2;
1547 #ifdef SCTL_MASK32
1548 	if (req->flags & SCTL_MASK32) {
1549 		tmpint = tmplong;
1550 		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1551 	} else
1552 #endif
1553 		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1554 
1555 	if (error || !req->newptr)
1556 		return (error);
1557 
1558 	if (!arg1)
1559 		error = EPERM;
1560 #ifdef SCTL_MASK32
1561 	else if (req->flags & SCTL_MASK32) {
1562 		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1563 		*(long *)arg1 = (long)tmpint;
1564 	}
1565 #endif
1566 	else
1567 		error = SYSCTL_IN(req, arg1, sizeof(long));
1568 	return (error);
1569 }
1570 
1571 /*
1572  * Handle a 64 bit int, signed or unsigned.
1573  * Two cases:
1574  *     a variable:  point arg1 at it.
1575  *     a constant:  pass it in arg2.
1576  */
1577 int
1578 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1579 {
1580 	int error = 0;
1581 	uint64_t tmpout;
1582 
1583 	/*
1584 	 * Attempt to get a coherent snapshot by making a copy of the data.
1585 	 */
1586 	if (arg1)
1587 		tmpout = *(uint64_t *)arg1;
1588 	else
1589 		tmpout = arg2;
1590 	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1591 
1592 	if (error || !req->newptr)
1593 		return (error);
1594 
1595 	if (!arg1)
1596 		error = EPERM;
1597 	else
1598 		error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1599 	return (error);
1600 }
1601 
1602 /*
1603  * Handle our generic '\0' terminated 'C' string.
1604  * Two cases:
1605  * 	a variable string:  point arg1 at it, arg2 is max length.
1606  * 	a constant string:  point arg1 at it, arg2 is zero.
1607  */
1608 
1609 int
1610 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1611 {
1612 	size_t outlen;
1613 	int error = 0, ro_string = 0;
1614 
1615 	/*
1616 	 * A zero-length buffer indicates a fixed size read-only
1617 	 * string:
1618 	 */
1619 	if (arg2 == 0) {
1620 		arg2 = strlen((char *)arg1) + 1;
1621 		ro_string = 1;
1622 	}
1623 
1624 	if (req->oldptr != NULL) {
1625 		char *tmparg;
1626 
1627 		if (ro_string) {
1628 			tmparg = arg1;
1629 		} else {
1630 			/* try to make a coherent snapshot of the string */
1631 			tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1632 			memcpy(tmparg, arg1, arg2);
1633 		}
1634 
1635 		outlen = strnlen(tmparg, arg2 - 1) + 1;
1636 		error = SYSCTL_OUT(req, tmparg, outlen);
1637 
1638 		if (!ro_string)
1639 			free(tmparg, M_SYSCTLTMP);
1640 	} else {
1641 		outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1642 		error = SYSCTL_OUT(req, NULL, outlen);
1643 	}
1644 	if (error || !req->newptr)
1645 		return (error);
1646 
1647 	if ((req->newlen - req->newidx) >= arg2) {
1648 		error = EINVAL;
1649 	} else {
1650 		arg2 = (req->newlen - req->newidx);
1651 		error = SYSCTL_IN(req, arg1, arg2);
1652 		((char *)arg1)[arg2] = '\0';
1653 	}
1654 	return (error);
1655 }
1656 
1657 /*
1658  * Handle any kind of opaque data.
1659  * arg1 points to it, arg2 is the size.
1660  */
1661 
1662 int
1663 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1664 {
1665 	int error, tries;
1666 	u_int generation;
1667 	struct sysctl_req req2;
1668 
1669 	/*
1670 	 * Attempt to get a coherent snapshot, by using the thread
1671 	 * pre-emption counter updated from within mi_switch() to
1672 	 * determine if we were pre-empted during a bcopy() or
1673 	 * copyout(). Make 3 attempts at doing this before giving up.
1674 	 * If we encounter an error, stop immediately.
1675 	 */
1676 	tries = 0;
1677 	req2 = *req;
1678 retry:
1679 	generation = curthread->td_generation;
1680 	error = SYSCTL_OUT(req, arg1, arg2);
1681 	if (error)
1682 		return (error);
1683 	tries++;
1684 	if (generation != curthread->td_generation && tries < 3) {
1685 		*req = req2;
1686 		goto retry;
1687 	}
1688 
1689 	error = SYSCTL_IN(req, arg1, arg2);
1690 
1691 	return (error);
1692 }
1693 
1694 /*
1695  * Transfer functions to/from kernel space.
1696  * XXX: rather untested at this point
1697  */
1698 static int
1699 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1700 {
1701 	size_t i = 0;
1702 
1703 	if (req->oldptr) {
1704 		i = l;
1705 		if (req->oldlen <= req->oldidx)
1706 			i = 0;
1707 		else
1708 			if (i > req->oldlen - req->oldidx)
1709 				i = req->oldlen - req->oldidx;
1710 		if (i > 0)
1711 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1712 	}
1713 	req->oldidx += l;
1714 	if (req->oldptr && i != l)
1715 		return (ENOMEM);
1716 	return (0);
1717 }
1718 
1719 static int
1720 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1721 {
1722 	if (!req->newptr)
1723 		return (0);
1724 	if (req->newlen - req->newidx < l)
1725 		return (EINVAL);
1726 	bcopy((char *)req->newptr + req->newidx, p, l);
1727 	req->newidx += l;
1728 	return (0);
1729 }
1730 
1731 int
1732 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1733     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1734 {
1735 	int error = 0;
1736 	struct sysctl_req req;
1737 
1738 	bzero(&req, sizeof req);
1739 
1740 	req.td = td;
1741 	req.flags = flags;
1742 
1743 	if (oldlenp) {
1744 		req.oldlen = *oldlenp;
1745 	}
1746 	req.validlen = req.oldlen;
1747 
1748 	if (old) {
1749 		req.oldptr= old;
1750 	}
1751 
1752 	if (new != NULL) {
1753 		req.newlen = newlen;
1754 		req.newptr = new;
1755 	}
1756 
1757 	req.oldfunc = sysctl_old_kernel;
1758 	req.newfunc = sysctl_new_kernel;
1759 	req.lock = REQ_UNWIRED;
1760 
1761 	error = sysctl_root(0, name, namelen, &req);
1762 
1763 	if (req.lock == REQ_WIRED && req.validlen > 0)
1764 		vsunlock(req.oldptr, req.validlen);
1765 
1766 	if (error && error != ENOMEM)
1767 		return (error);
1768 
1769 	if (retval) {
1770 		if (req.oldptr && req.oldidx > req.validlen)
1771 			*retval = req.validlen;
1772 		else
1773 			*retval = req.oldidx;
1774 	}
1775 	return (error);
1776 }
1777 
1778 int
1779 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1780     void *new, size_t newlen, size_t *retval, int flags)
1781 {
1782         int oid[CTL_MAXNAME];
1783         size_t oidlen, plen;
1784 	int error;
1785 
1786 	oid[0] = 0;		/* sysctl internal magic */
1787 	oid[1] = 3;		/* name2oid */
1788 	oidlen = sizeof(oid);
1789 
1790 	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1791 	    (void *)name, strlen(name), &plen, flags);
1792 	if (error)
1793 		return (error);
1794 
1795 	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1796 	    new, newlen, retval, flags);
1797 	return (error);
1798 }
1799 
1800 /*
1801  * Transfer function to/from user space.
1802  */
1803 static int
1804 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1805 {
1806 	size_t i, len, origidx;
1807 	int error;
1808 
1809 	origidx = req->oldidx;
1810 	req->oldidx += l;
1811 	if (req->oldptr == NULL)
1812 		return (0);
1813 	/*
1814 	 * If we have not wired the user supplied buffer and we are currently
1815 	 * holding locks, drop a witness warning, as it's possible that
1816 	 * write operations to the user page can sleep.
1817 	 */
1818 	if (req->lock != REQ_WIRED)
1819 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1820 		    "sysctl_old_user()");
1821 	i = l;
1822 	len = req->validlen;
1823 	if (len <= origidx)
1824 		i = 0;
1825 	else {
1826 		if (i > len - origidx)
1827 			i = len - origidx;
1828 		if (req->lock == REQ_WIRED) {
1829 			error = copyout_nofault(p, (char *)req->oldptr +
1830 			    origidx, i);
1831 		} else
1832 			error = copyout(p, (char *)req->oldptr + origidx, i);
1833 		if (error != 0)
1834 			return (error);
1835 	}
1836 	if (i < l)
1837 		return (ENOMEM);
1838 	return (0);
1839 }
1840 
1841 static int
1842 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1843 {
1844 	int error;
1845 
1846 	if (!req->newptr)
1847 		return (0);
1848 	if (req->newlen - req->newidx < l)
1849 		return (EINVAL);
1850 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1851 	    "sysctl_new_user()");
1852 	error = copyin((char *)req->newptr + req->newidx, p, l);
1853 	req->newidx += l;
1854 	return (error);
1855 }
1856 
1857 /*
1858  * Wire the user space destination buffer.  If set to a value greater than
1859  * zero, the len parameter limits the maximum amount of wired memory.
1860  */
1861 int
1862 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1863 {
1864 	int ret;
1865 	size_t wiredlen;
1866 
1867 	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1868 	ret = 0;
1869 	if (req->lock != REQ_WIRED && req->oldptr &&
1870 	    req->oldfunc == sysctl_old_user) {
1871 		if (wiredlen != 0) {
1872 			ret = vslock(req->oldptr, wiredlen);
1873 			if (ret != 0) {
1874 				if (ret != ENOMEM)
1875 					return (ret);
1876 				wiredlen = 0;
1877 			}
1878 		}
1879 		req->lock = REQ_WIRED;
1880 		req->validlen = wiredlen;
1881 	}
1882 	return (0);
1883 }
1884 
1885 int
1886 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1887     int *nindx, struct sysctl_req *req)
1888 {
1889 	struct sysctl_oid_list *lsp;
1890 	struct sysctl_oid *oid;
1891 	int indx;
1892 
1893 	SYSCTL_ASSERT_LOCKED();
1894 	lsp = &sysctl__children;
1895 	indx = 0;
1896 	while (indx < CTL_MAXNAME) {
1897 		SLIST_FOREACH(oid, lsp, oid_link) {
1898 			if (oid->oid_number == name[indx])
1899 				break;
1900 		}
1901 		if (oid == NULL)
1902 			return (ENOENT);
1903 
1904 		indx++;
1905 		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1906 			if (oid->oid_handler != NULL || indx == namelen) {
1907 				*noid = oid;
1908 				if (nindx != NULL)
1909 					*nindx = indx;
1910 				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1911 				    ("%s found DYING node %p", __func__, oid));
1912 				return (0);
1913 			}
1914 			lsp = SYSCTL_CHILDREN(oid);
1915 		} else if (indx == namelen) {
1916 			if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1917 				return (ENOENT);
1918 			*noid = oid;
1919 			if (nindx != NULL)
1920 				*nindx = indx;
1921 			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1922 			    ("%s found DYING node %p", __func__, oid));
1923 			return (0);
1924 		} else {
1925 			return (ENOTDIR);
1926 		}
1927 	}
1928 	return (ENOENT);
1929 }
1930 
1931 /*
1932  * Traverse our tree, and find the right node, execute whatever it points
1933  * to, and return the resulting error code.
1934  */
1935 
1936 static int
1937 sysctl_root(SYSCTL_HANDLER_ARGS)
1938 {
1939 	struct sysctl_oid *oid;
1940 	struct rm_priotracker tracker;
1941 	int error, indx, lvl;
1942 
1943 	SYSCTL_RLOCK(&tracker);
1944 
1945 	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1946 	if (error)
1947 		goto out;
1948 
1949 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1950 		/*
1951 		 * You can't call a sysctl when it's a node, but has
1952 		 * no handler.  Inform the user that it's a node.
1953 		 * The indx may or may not be the same as namelen.
1954 		 */
1955 		if (oid->oid_handler == NULL) {
1956 			error = EISDIR;
1957 			goto out;
1958 		}
1959 	}
1960 
1961 	/* Is this sysctl writable? */
1962 	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1963 		error = EPERM;
1964 		goto out;
1965 	}
1966 
1967 	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1968 
1969 #ifdef CAPABILITY_MODE
1970 	/*
1971 	 * If the process is in capability mode, then don't permit reading or
1972 	 * writing unless specifically granted for the node.
1973 	 */
1974 	if (IN_CAPABILITY_MODE(req->td)) {
1975 		if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1976 		    (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1977 			error = EPERM;
1978 			goto out;
1979 		}
1980 	}
1981 #endif
1982 
1983 	/* Is this sysctl sensitive to securelevels? */
1984 	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1985 		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1986 		error = securelevel_gt(req->td->td_ucred, lvl);
1987 		if (error)
1988 			goto out;
1989 	}
1990 
1991 	/* Is this sysctl writable by only privileged users? */
1992 	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1993 		int priv;
1994 
1995 		if (oid->oid_kind & CTLFLAG_PRISON)
1996 			priv = PRIV_SYSCTL_WRITEJAIL;
1997 #ifdef VIMAGE
1998 		else if ((oid->oid_kind & CTLFLAG_VNET) &&
1999 		     prison_owns_vnet(req->td->td_ucred))
2000 			priv = PRIV_SYSCTL_WRITEJAIL;
2001 #endif
2002 		else
2003 			priv = PRIV_SYSCTL_WRITE;
2004 		error = priv_check(req->td, priv);
2005 		if (error)
2006 			goto out;
2007 	}
2008 
2009 	if (!oid->oid_handler) {
2010 		error = EINVAL;
2011 		goto out;
2012 	}
2013 
2014 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2015 		arg1 = (int *)arg1 + indx;
2016 		arg2 -= indx;
2017 	} else {
2018 		arg1 = oid->oid_arg1;
2019 		arg2 = oid->oid_arg2;
2020 	}
2021 #ifdef MAC
2022 	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2023 	    req);
2024 	if (error != 0)
2025 		goto out;
2026 #endif
2027 #ifdef VIMAGE
2028 	if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2029 		arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2030 #endif
2031 	error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2032 
2033 out:
2034 	SYSCTL_RUNLOCK(&tracker);
2035 	return (error);
2036 }
2037 
2038 #ifndef _SYS_SYSPROTO_H_
2039 struct sysctl_args {
2040 	int	*name;
2041 	u_int	namelen;
2042 	void	*old;
2043 	size_t	*oldlenp;
2044 	void	*new;
2045 	size_t	newlen;
2046 };
2047 #endif
2048 int
2049 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2050 {
2051 	int error, i, name[CTL_MAXNAME];
2052 	size_t j;
2053 
2054 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2055 		return (EINVAL);
2056 
2057  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2058  	if (error)
2059 		return (error);
2060 
2061 	error = userland_sysctl(td, name, uap->namelen,
2062 		uap->old, uap->oldlenp, 0,
2063 		uap->new, uap->newlen, &j, 0);
2064 	if (error && error != ENOMEM)
2065 		return (error);
2066 	if (uap->oldlenp) {
2067 		i = copyout(&j, uap->oldlenp, sizeof(j));
2068 		if (i)
2069 			return (i);
2070 	}
2071 	return (error);
2072 }
2073 
2074 /*
2075  * This is used from various compatibility syscalls too.  That's why name
2076  * must be in kernel space.
2077  */
2078 int
2079 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2080     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2081     int flags)
2082 {
2083 	int error = 0, memlocked;
2084 	struct sysctl_req req;
2085 
2086 	bzero(&req, sizeof req);
2087 
2088 	req.td = td;
2089 	req.flags = flags;
2090 
2091 	if (oldlenp) {
2092 		if (inkernel) {
2093 			req.oldlen = *oldlenp;
2094 		} else {
2095 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2096 			if (error)
2097 				return (error);
2098 		}
2099 	}
2100 	req.validlen = req.oldlen;
2101 	req.oldptr = old;
2102 
2103 	if (new != NULL) {
2104 		req.newlen = newlen;
2105 		req.newptr = new;
2106 	}
2107 
2108 	req.oldfunc = sysctl_old_user;
2109 	req.newfunc = sysctl_new_user;
2110 	req.lock = REQ_UNWIRED;
2111 
2112 #ifdef KTRACE
2113 	if (KTRPOINT(curthread, KTR_SYSCTL))
2114 		ktrsysctl(name, namelen);
2115 #endif
2116 	memlocked = 0;
2117 	if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2118 		memlocked = 1;
2119 		sx_xlock(&sysctlmemlock);
2120 	}
2121 	CURVNET_SET(TD_TO_VNET(td));
2122 
2123 	for (;;) {
2124 		req.oldidx = 0;
2125 		req.newidx = 0;
2126 		error = sysctl_root(0, name, namelen, &req);
2127 		if (error != EAGAIN)
2128 			break;
2129 		kern_yield(PRI_USER);
2130 	}
2131 
2132 	CURVNET_RESTORE();
2133 
2134 	if (req.lock == REQ_WIRED && req.validlen > 0)
2135 		vsunlock(req.oldptr, req.validlen);
2136 	if (memlocked)
2137 		sx_xunlock(&sysctlmemlock);
2138 
2139 	if (error && error != ENOMEM)
2140 		return (error);
2141 
2142 	if (retval) {
2143 		if (req.oldptr && req.oldidx > req.validlen)
2144 			*retval = req.validlen;
2145 		else
2146 			*retval = req.oldidx;
2147 	}
2148 	return (error);
2149 }
2150 
2151 /*
2152  * Drain into a sysctl struct.  The user buffer should be wired if a page
2153  * fault would cause issue.
2154  */
2155 static int
2156 sbuf_sysctl_drain(void *arg, const char *data, int len)
2157 {
2158 	struct sysctl_req *req = arg;
2159 	int error;
2160 
2161 	error = SYSCTL_OUT(req, data, len);
2162 	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2163 	return (error == 0 ? len : -error);
2164 }
2165 
2166 struct sbuf *
2167 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2168     struct sysctl_req *req)
2169 {
2170 
2171 	/* Supply a default buffer size if none given. */
2172 	if (buf == NULL && length == 0)
2173 		length = 64;
2174 	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2175 	sbuf_set_drain(s, sbuf_sysctl_drain, req);
2176 	return (s);
2177 }
2178