kern_sysctl.c revision 195814
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 195814 2009-07-21 21:58:55Z bz $");
40
41#include "opt_compat.h"
42#include "opt_ktrace.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/malloc.h>
49#include <sys/priv.h>
50#include <sys/proc.h>
51#include <sys/jail.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/sx.h>
55#include <sys/sysproto.h>
56#include <sys/uio.h>
57#include <sys/vimage.h>
58#ifdef KTRACE
59#include <sys/ktrace.h>
60#endif
61
62#include <net/vnet.h>
63
64#include <security/mac/mac_framework.h>
65
66#include <vm/vm.h>
67#include <vm/vm_extern.h>
68
69static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
70static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
71static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
72
73/*
74 * The sysctllock protects the MIB tree.  It also protects sysctl
75 * contexts used with dynamic sysctls.  The sysctl_register_oid() and
76 * sysctl_unregister_oid() routines require the sysctllock to already
77 * be held, so the sysctl_lock() and sysctl_unlock() routines are
78 * provided for the few places in the kernel which need to use that
79 * API rather than using the dynamic API.  Use of the dynamic API is
80 * strongly encouraged for most code.
81 *
82 * The sysctlmemlock is used to limit the amount of user memory wired for
83 * sysctl requests.  This is implemented by serializing any userland
84 * sysctl requests larger than a single page via an exclusive lock.
85 */
86static struct sx sysctllock;
87static struct sx sysctlmemlock;
88
89#define	SYSCTL_SLOCK()		sx_slock(&sysctllock)
90#define	SYSCTL_SUNLOCK()	sx_sunlock(&sysctllock)
91#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
92#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
93#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
94#define	SYSCTL_ASSERT_LOCKED()	sx_assert(&sysctllock, SA_LOCKED)
95#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
96
97static int sysctl_root(SYSCTL_HANDLER_ARGS);
98
99struct sysctl_oid_list sysctl__children; /* root list */
100
101static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
102		    int recurse);
103
104static struct sysctl_oid *
105sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
106{
107	struct sysctl_oid *oidp;
108
109	SYSCTL_ASSERT_LOCKED();
110	SLIST_FOREACH(oidp, list, oid_link) {
111		if (strcmp(oidp->oid_name, name) == 0) {
112			return (oidp);
113		}
114	}
115	return (NULL);
116}
117
118/*
119 * Initialization of the MIB tree.
120 *
121 * Order by number in each list.
122 */
123void
124sysctl_lock(void)
125{
126
127	SYSCTL_XLOCK();
128}
129
130void
131sysctl_unlock(void)
132{
133
134	SYSCTL_XUNLOCK();
135}
136
137void
138sysctl_register_oid(struct sysctl_oid *oidp)
139{
140	struct sysctl_oid_list *parent = oidp->oid_parent;
141	struct sysctl_oid *p;
142	struct sysctl_oid *q;
143
144	/*
145	 * First check if another oid with the same name already
146	 * exists in the parent's list.
147	 */
148	SYSCTL_ASSERT_XLOCKED();
149	p = sysctl_find_oidname(oidp->oid_name, parent);
150	if (p != NULL) {
151		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
152			p->oid_refcnt++;
153			return;
154		} else {
155			printf("can't re-use a leaf (%s)!\n", p->oid_name);
156			return;
157		}
158	}
159	/*
160	 * If this oid has a number OID_AUTO, give it a number which
161	 * is greater than any current oid.
162	 * NOTE: DO NOT change the starting value here, change it in
163	 * <sys/sysctl.h>, and make sure it is at least 256 to
164	 * accomodate e.g. net.inet.raw as a static sysctl node.
165	 */
166	if (oidp->oid_number == OID_AUTO) {
167		static int newoid = CTL_AUTO_START;
168
169		oidp->oid_number = newoid++;
170		if (newoid == 0x7fffffff)
171			panic("out of oids");
172	}
173#if 0
174	else if (oidp->oid_number >= CTL_AUTO_START) {
175		/* do not panic; this happens when unregistering sysctl sets */
176		printf("static sysctl oid too high: %d", oidp->oid_number);
177	}
178#endif
179
180	/*
181	 * Insert the oid into the parent's list in order.
182	 */
183	q = NULL;
184	SLIST_FOREACH(p, parent, oid_link) {
185		if (oidp->oid_number < p->oid_number)
186			break;
187		q = p;
188	}
189	if (q)
190		SLIST_INSERT_AFTER(q, oidp, oid_link);
191	else
192		SLIST_INSERT_HEAD(parent, oidp, oid_link);
193}
194
195void
196sysctl_unregister_oid(struct sysctl_oid *oidp)
197{
198	struct sysctl_oid *p;
199	int error;
200
201	SYSCTL_ASSERT_XLOCKED();
202	error = ENOENT;
203	if (oidp->oid_number == OID_AUTO) {
204		error = EINVAL;
205	} else {
206		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
207			if (p == oidp) {
208				SLIST_REMOVE(oidp->oid_parent, oidp,
209				    sysctl_oid, oid_link);
210				error = 0;
211				break;
212			}
213		}
214	}
215
216	/*
217	 * This can happen when a module fails to register and is
218	 * being unloaded afterwards.  It should not be a panic()
219	 * for normal use.
220	 */
221	if (error)
222		printf("%s: failed to unregister sysctl\n", __func__);
223}
224
225/* Initialize a new context to keep track of dynamically added sysctls. */
226int
227sysctl_ctx_init(struct sysctl_ctx_list *c)
228{
229
230	if (c == NULL) {
231		return (EINVAL);
232	}
233
234	/*
235	 * No locking here, the caller is responsible for not adding
236	 * new nodes to a context until after this function has
237	 * returned.
238	 */
239	TAILQ_INIT(c);
240	return (0);
241}
242
243/* Free the context, and destroy all dynamic oids registered in this context */
244int
245sysctl_ctx_free(struct sysctl_ctx_list *clist)
246{
247	struct sysctl_ctx_entry *e, *e1;
248	int error;
249
250	error = 0;
251	/*
252	 * First perform a "dry run" to check if it's ok to remove oids.
253	 * XXX FIXME
254	 * XXX This algorithm is a hack. But I don't know any
255	 * XXX better solution for now...
256	 */
257	SYSCTL_XLOCK();
258	TAILQ_FOREACH(e, clist, link) {
259		error = sysctl_remove_oid_locked(e->entry, 0, 0);
260		if (error)
261			break;
262	}
263	/*
264	 * Restore deregistered entries, either from the end,
265	 * or from the place where error occured.
266	 * e contains the entry that was not unregistered
267	 */
268	if (error)
269		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
270	else
271		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
272	while (e1 != NULL) {
273		sysctl_register_oid(e1->entry);
274		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
275	}
276	if (error) {
277		SYSCTL_XUNLOCK();
278		return(EBUSY);
279	}
280	/* Now really delete the entries */
281	e = TAILQ_FIRST(clist);
282	while (e != NULL) {
283		e1 = TAILQ_NEXT(e, link);
284		error = sysctl_remove_oid_locked(e->entry, 1, 0);
285		if (error)
286			panic("sysctl_remove_oid: corrupt tree, entry: %s",
287			    e->entry->oid_name);
288		free(e, M_SYSCTLOID);
289		e = e1;
290	}
291	SYSCTL_XUNLOCK();
292	return (error);
293}
294
295/* Add an entry to the context */
296struct sysctl_ctx_entry *
297sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
298{
299	struct sysctl_ctx_entry *e;
300
301	SYSCTL_ASSERT_XLOCKED();
302	if (clist == NULL || oidp == NULL)
303		return(NULL);
304	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
305	e->entry = oidp;
306	TAILQ_INSERT_HEAD(clist, e, link);
307	return (e);
308}
309
310/* Find an entry in the context */
311struct sysctl_ctx_entry *
312sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
313{
314	struct sysctl_ctx_entry *e;
315
316	SYSCTL_ASSERT_LOCKED();
317	if (clist == NULL || oidp == NULL)
318		return(NULL);
319	TAILQ_FOREACH(e, clist, link) {
320		if(e->entry == oidp)
321			return(e);
322	}
323	return (e);
324}
325
326/*
327 * Delete an entry from the context.
328 * NOTE: this function doesn't free oidp! You have to remove it
329 * with sysctl_remove_oid().
330 */
331int
332sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
333{
334	struct sysctl_ctx_entry *e;
335
336	if (clist == NULL || oidp == NULL)
337		return (EINVAL);
338	SYSCTL_XLOCK();
339	e = sysctl_ctx_entry_find(clist, oidp);
340	if (e != NULL) {
341		TAILQ_REMOVE(clist, e, link);
342		SYSCTL_XUNLOCK();
343		free(e, M_SYSCTLOID);
344		return (0);
345	} else {
346		SYSCTL_XUNLOCK();
347		return (ENOENT);
348	}
349}
350
351/*
352 * Remove dynamically created sysctl trees.
353 * oidp - top of the tree to be removed
354 * del - if 0 - just deregister, otherwise free up entries as well
355 * recurse - if != 0 traverse the subtree to be deleted
356 */
357int
358sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
359{
360	int error;
361
362	SYSCTL_XLOCK();
363	error = sysctl_remove_oid_locked(oidp, del, recurse);
364	SYSCTL_XUNLOCK();
365	return (error);
366}
367
368static int
369sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
370{
371	struct sysctl_oid *p;
372	int error;
373
374	SYSCTL_ASSERT_XLOCKED();
375	if (oidp == NULL)
376		return(EINVAL);
377	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
378		printf("can't remove non-dynamic nodes!\n");
379		return (EINVAL);
380	}
381	/*
382	 * WARNING: normal method to do this should be through
383	 * sysctl_ctx_free(). Use recursing as the last resort
384	 * method to purge your sysctl tree of leftovers...
385	 * However, if some other code still references these nodes,
386	 * it will panic.
387	 */
388	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
389		if (oidp->oid_refcnt == 1) {
390			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
391				if (!recurse)
392					return (ENOTEMPTY);
393				error = sysctl_remove_oid_locked(p, del,
394				    recurse);
395				if (error)
396					return (error);
397			}
398			if (del)
399				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
400		}
401	}
402	if (oidp->oid_refcnt > 1 ) {
403		oidp->oid_refcnt--;
404	} else {
405		if (oidp->oid_refcnt == 0) {
406			printf("Warning: bad oid_refcnt=%u (%s)!\n",
407				oidp->oid_refcnt, oidp->oid_name);
408			return (EINVAL);
409		}
410		sysctl_unregister_oid(oidp);
411		if (del) {
412			if (oidp->oid_descr)
413				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
414			free((void *)(uintptr_t)(const void *)oidp->oid_name,
415			     M_SYSCTLOID);
416			free(oidp, M_SYSCTLOID);
417		}
418	}
419	return (0);
420}
421
422/*
423 * Create new sysctls at run time.
424 * clist may point to a valid context initialized with sysctl_ctx_init().
425 */
426struct sysctl_oid *
427sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
428	int number, const char *name, int kind, void *arg1, int arg2,
429	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
430{
431	struct sysctl_oid *oidp;
432	ssize_t len;
433	char *newname;
434
435	/* You have to hook up somewhere.. */
436	if (parent == NULL)
437		return(NULL);
438	/* Check if the node already exists, otherwise create it */
439	SYSCTL_XLOCK();
440	oidp = sysctl_find_oidname(name, parent);
441	if (oidp != NULL) {
442		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
443			oidp->oid_refcnt++;
444			/* Update the context */
445			if (clist != NULL)
446				sysctl_ctx_entry_add(clist, oidp);
447			SYSCTL_XUNLOCK();
448			return (oidp);
449		} else {
450			SYSCTL_XUNLOCK();
451			printf("can't re-use a leaf (%s)!\n", name);
452			return (NULL);
453		}
454	}
455	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
456	oidp->oid_parent = parent;
457	SLIST_NEXT(oidp, oid_link) = NULL;
458	oidp->oid_number = number;
459	oidp->oid_refcnt = 1;
460	len = strlen(name);
461	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
462	bcopy(name, newname, len + 1);
463	newname[len] = '\0';
464	oidp->oid_name = newname;
465	oidp->oid_handler = handler;
466	oidp->oid_kind = CTLFLAG_DYN | kind;
467	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
468		/* Allocate space for children */
469		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
470		    M_SYSCTLOID, M_WAITOK));
471		SLIST_INIT(SYSCTL_CHILDREN(oidp));
472	} else {
473		oidp->oid_arg1 = arg1;
474		oidp->oid_arg2 = arg2;
475	}
476	oidp->oid_fmt = fmt;
477	if (descr) {
478		int len = strlen(descr) + 1;
479		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
480		if (oidp->oid_descr)
481			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
482	}
483	/* Update the context, if used */
484	if (clist != NULL)
485		sysctl_ctx_entry_add(clist, oidp);
486	/* Register this oid */
487	sysctl_register_oid(oidp);
488	SYSCTL_XUNLOCK();
489	return (oidp);
490}
491
492/*
493 * Rename an existing oid.
494 */
495void
496sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
497{
498	ssize_t len;
499	char *newname;
500	void *oldname;
501
502	len = strlen(name);
503	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
504	bcopy(name, newname, len + 1);
505	newname[len] = '\0';
506	SYSCTL_XLOCK();
507	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
508	oidp->oid_name = newname;
509	SYSCTL_XUNLOCK();
510	free(oldname, M_SYSCTLOID);
511}
512
513/*
514 * Reparent an existing oid.
515 */
516int
517sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
518{
519	struct sysctl_oid *oidp;
520
521	SYSCTL_XLOCK();
522	if (oid->oid_parent == parent) {
523		SYSCTL_XUNLOCK();
524		return (0);
525	}
526	oidp = sysctl_find_oidname(oid->oid_name, parent);
527	if (oidp != NULL) {
528		SYSCTL_XUNLOCK();
529		return (EEXIST);
530	}
531	sysctl_unregister_oid(oid);
532	oid->oid_parent = parent;
533	oid->oid_number = OID_AUTO;
534	sysctl_register_oid(oid);
535	SYSCTL_XUNLOCK();
536	return (0);
537}
538
539/*
540 * Register the kernel's oids on startup.
541 */
542SET_DECLARE(sysctl_set, struct sysctl_oid);
543
544static void
545sysctl_register_all(void *arg)
546{
547	struct sysctl_oid **oidp;
548
549	sx_init(&sysctlmemlock, "sysctl mem");
550	SYSCTL_INIT();
551	SYSCTL_XLOCK();
552	SET_FOREACH(oidp, sysctl_set)
553		sysctl_register_oid(*oidp);
554	SYSCTL_XUNLOCK();
555}
556SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
557
558/*
559 * "Staff-functions"
560 *
561 * These functions implement a presently undocumented interface
562 * used by the sysctl program to walk the tree, and get the type
563 * so it can print the value.
564 * This interface is under work and consideration, and should probably
565 * be killed with a big axe by the first person who can find the time.
566 * (be aware though, that the proper interface isn't as obvious as it
567 * may seem, there are various conflicting requirements.
568 *
569 * {0,0}	printf the entire MIB-tree.
570 * {0,1,...}	return the name of the "..." OID.
571 * {0,2,...}	return the next OID.
572 * {0,3}	return the OID of the name in "new"
573 * {0,4,...}	return the kind & format info for the "..." OID.
574 * {0,5,...}	return the description the "..." OID.
575 */
576
577#ifdef SYSCTL_DEBUG
578static void
579sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
580{
581	int k;
582	struct sysctl_oid *oidp;
583
584	SYSCTL_ASSERT_LOCKED();
585	SLIST_FOREACH(oidp, l, oid_link) {
586
587		for (k=0; k<i; k++)
588			printf(" ");
589
590		printf("%d %s ", oidp->oid_number, oidp->oid_name);
591
592		printf("%c%c",
593			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
594			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
595
596		if (oidp->oid_handler)
597			printf(" *Handler");
598
599		switch (oidp->oid_kind & CTLTYPE) {
600			case CTLTYPE_NODE:
601				printf(" Node\n");
602				if (!oidp->oid_handler) {
603					sysctl_sysctl_debug_dump_node(
604						oidp->oid_arg1, i+2);
605				}
606				break;
607			case CTLTYPE_INT:    printf(" Int\n"); break;
608			case CTLTYPE_STRING: printf(" String\n"); break;
609			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
610			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
611			default:	     printf("\n");
612		}
613
614	}
615}
616
617static int
618sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
619{
620	int error;
621
622	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
623	if (error)
624		return (error);
625	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
626	return (ENOENT);
627}
628
629SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
630	0, 0, sysctl_sysctl_debug, "-", "");
631#endif
632
633static int
634sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
635{
636	int *name = (int *) arg1;
637	u_int namelen = arg2;
638	int error = 0;
639	struct sysctl_oid *oid;
640	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
641	char buf[10];
642
643	SYSCTL_ASSERT_LOCKED();
644	while (namelen) {
645		if (!lsp) {
646			snprintf(buf,sizeof(buf),"%d",*name);
647			if (req->oldidx)
648				error = SYSCTL_OUT(req, ".", 1);
649			if (!error)
650				error = SYSCTL_OUT(req, buf, strlen(buf));
651			if (error)
652				return (error);
653			namelen--;
654			name++;
655			continue;
656		}
657		lsp2 = 0;
658		SLIST_FOREACH(oid, lsp, oid_link) {
659			if (oid->oid_number != *name)
660				continue;
661
662			if (req->oldidx)
663				error = SYSCTL_OUT(req, ".", 1);
664			if (!error)
665				error = SYSCTL_OUT(req, oid->oid_name,
666					strlen(oid->oid_name));
667			if (error)
668				return (error);
669
670			namelen--;
671			name++;
672
673			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
674				break;
675
676			if (oid->oid_handler)
677				break;
678
679			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
680			break;
681		}
682		lsp = lsp2;
683	}
684	return (SYSCTL_OUT(req, "", 1));
685}
686
687static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
688
689static int
690sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
691	int *next, int *len, int level, struct sysctl_oid **oidpp)
692{
693	struct sysctl_oid *oidp;
694
695	SYSCTL_ASSERT_LOCKED();
696	*len = level;
697	SLIST_FOREACH(oidp, lsp, oid_link) {
698		*next = oidp->oid_number;
699		*oidpp = oidp;
700
701		if (oidp->oid_kind & CTLFLAG_SKIP)
702			continue;
703
704		if (!namelen) {
705			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
706				return (0);
707			if (oidp->oid_handler)
708				/* We really should call the handler here...*/
709				return (0);
710			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
711			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
712				len, level+1, oidpp))
713				return (0);
714			goto emptynode;
715		}
716
717		if (oidp->oid_number < *name)
718			continue;
719
720		if (oidp->oid_number > *name) {
721			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
722				return (0);
723			if (oidp->oid_handler)
724				return (0);
725			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
726			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
727				next+1, len, level+1, oidpp))
728				return (0);
729			goto next;
730		}
731		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
732			continue;
733
734		if (oidp->oid_handler)
735			continue;
736
737		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
738		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
739			len, level+1, oidpp))
740			return (0);
741	next:
742		namelen = 1;
743	emptynode:
744		*len = level;
745	}
746	return (1);
747}
748
749static int
750sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
751{
752	int *name = (int *) arg1;
753	u_int namelen = arg2;
754	int i, j, error;
755	struct sysctl_oid *oid;
756	struct sysctl_oid_list *lsp = &sysctl__children;
757	int newoid[CTL_MAXNAME];
758
759	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
760	if (i)
761		return (ENOENT);
762	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
763	return (error);
764}
765
766static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
767
768static int
769name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
770{
771	int i;
772	struct sysctl_oid *oidp;
773	struct sysctl_oid_list *lsp = &sysctl__children;
774	char *p;
775
776	SYSCTL_ASSERT_LOCKED();
777
778	if (!*name)
779		return (ENOENT);
780
781	p = name + strlen(name) - 1 ;
782	if (*p == '.')
783		*p = '\0';
784
785	*len = 0;
786
787	for (p = name; *p && *p != '.'; p++)
788		;
789	i = *p;
790	if (i == '.')
791		*p = '\0';
792
793	oidp = SLIST_FIRST(lsp);
794
795	while (oidp && *len < CTL_MAXNAME) {
796		if (strcmp(name, oidp->oid_name)) {
797			oidp = SLIST_NEXT(oidp, oid_link);
798			continue;
799		}
800		*oid++ = oidp->oid_number;
801		(*len)++;
802
803		if (!i) {
804			if (oidpp)
805				*oidpp = oidp;
806			return (0);
807		}
808
809		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
810			break;
811
812		if (oidp->oid_handler)
813			break;
814
815		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
816		oidp = SLIST_FIRST(lsp);
817		name = p+1;
818		for (p = name; *p && *p != '.'; p++)
819				;
820		i = *p;
821		if (i == '.')
822			*p = '\0';
823	}
824	return (ENOENT);
825}
826
827static int
828sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
829{
830	char *p;
831	int error, oid[CTL_MAXNAME], len;
832	struct sysctl_oid *op = 0;
833
834	SYSCTL_ASSERT_LOCKED();
835
836	if (!req->newlen)
837		return (ENOENT);
838	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
839		return (ENAMETOOLONG);
840
841	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
842
843	error = SYSCTL_IN(req, p, req->newlen);
844	if (error) {
845		free(p, M_SYSCTL);
846		return (error);
847	}
848
849	p [req->newlen] = '\0';
850
851	error = name2oid(p, oid, &len, &op);
852
853	free(p, M_SYSCTL);
854
855	if (error)
856		return (error);
857
858	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
859	return (error);
860}
861
862SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
863    0, 0, sysctl_sysctl_name2oid, "I", "");
864
865static int
866sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
867{
868	struct sysctl_oid *oid;
869	int error;
870
871	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
872	if (error)
873		return (error);
874
875	if (!oid->oid_fmt)
876		return (ENOENT);
877	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
878	if (error)
879		return (error);
880	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
881	return (error);
882}
883
884
885static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
886    sysctl_sysctl_oidfmt, "");
887
888static int
889sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
890{
891	struct sysctl_oid *oid;
892	int error;
893
894	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
895	if (error)
896		return (error);
897
898	if (!oid->oid_descr)
899		return (ENOENT);
900	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
901	return (error);
902}
903
904static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
905
906/*
907 * Default "handler" functions.
908 */
909
910/*
911 * Handle an int, signed or unsigned.
912 * Two cases:
913 *     a variable:  point arg1 at it.
914 *     a constant:  pass it in arg2.
915 */
916
917int
918sysctl_handle_int(SYSCTL_HANDLER_ARGS)
919{
920	int tmpout, error = 0;
921
922	/*
923	 * Attempt to get a coherent snapshot by making a copy of the data.
924	 */
925	if (arg1)
926		tmpout = *(int *)arg1;
927	else
928		tmpout = arg2;
929	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
930
931	if (error || !req->newptr)
932		return (error);
933
934	if (!arg1)
935		error = EPERM;
936	else
937		error = SYSCTL_IN(req, arg1, sizeof(int));
938	return (error);
939}
940
941/*
942 * Based on on sysctl_handle_int() convert milliseconds into ticks.
943 * Note: this is used by TCP.
944 */
945
946int
947sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
948{
949	int error, s, tt;
950
951	tt = *(int *)arg1;
952	s = (int)((int64_t)tt * 1000 / hz);
953
954	error = sysctl_handle_int(oidp, &s, 0, req);
955	if (error || !req->newptr)
956		return (error);
957
958	tt = (int)((int64_t)s * hz / 1000);
959	if (tt < 1)
960		return (EINVAL);
961
962	*(int *)arg1 = tt;
963	return (0);
964}
965
966
967/*
968 * Handle a long, signed or unsigned.  arg1 points to it.
969 */
970
971int
972sysctl_handle_long(SYSCTL_HANDLER_ARGS)
973{
974	int error = 0;
975	long tmplong;
976#ifdef SCTL_MASK32
977	int tmpint;
978#endif
979
980	/*
981	 * Attempt to get a coherent snapshot by making a copy of the data.
982	 */
983	if (!arg1)
984		return (EINVAL);
985	tmplong = *(long *)arg1;
986#ifdef SCTL_MASK32
987	if (req->flags & SCTL_MASK32) {
988		tmpint = tmplong;
989		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
990	} else
991#endif
992		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
993
994	if (error || !req->newptr)
995		return (error);
996
997#ifdef SCTL_MASK32
998	if (req->flags & SCTL_MASK32) {
999		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1000		*(long *)arg1 = (long)tmpint;
1001	} else
1002#endif
1003		error = SYSCTL_IN(req, arg1, sizeof(long));
1004	return (error);
1005}
1006
1007/*
1008 * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1009 */
1010
1011int
1012sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1013{
1014	int error = 0;
1015	uint64_t tmpout;
1016
1017	/*
1018	 * Attempt to get a coherent snapshot by making a copy of the data.
1019	 */
1020	if (!arg1)
1021		return (EINVAL);
1022	tmpout = *(uint64_t *)arg1;
1023	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1024
1025	if (error || !req->newptr)
1026		return (error);
1027
1028	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1029	return (error);
1030}
1031
1032/*
1033 * Handle our generic '\0' terminated 'C' string.
1034 * Two cases:
1035 * 	a variable string:  point arg1 at it, arg2 is max length.
1036 * 	a constant string:  point arg1 at it, arg2 is zero.
1037 */
1038
1039int
1040sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1041{
1042	int error=0;
1043	char *tmparg;
1044	size_t outlen;
1045
1046	/*
1047	 * Attempt to get a coherent snapshot by copying to a
1048	 * temporary kernel buffer.
1049	 */
1050retry:
1051	outlen = strlen((char *)arg1)+1;
1052	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1053
1054	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1055		free(tmparg, M_SYSCTLTMP);
1056		goto retry;
1057	}
1058
1059	error = SYSCTL_OUT(req, tmparg, outlen);
1060	free(tmparg, M_SYSCTLTMP);
1061
1062	if (error || !req->newptr)
1063		return (error);
1064
1065	if ((req->newlen - req->newidx) >= arg2) {
1066		error = EINVAL;
1067	} else {
1068		arg2 = (req->newlen - req->newidx);
1069		error = SYSCTL_IN(req, arg1, arg2);
1070		((char *)arg1)[arg2] = '\0';
1071	}
1072
1073	return (error);
1074}
1075
1076/*
1077 * Handle any kind of opaque data.
1078 * arg1 points to it, arg2 is the size.
1079 */
1080
1081int
1082sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1083{
1084	int error, tries;
1085	u_int generation;
1086	struct sysctl_req req2;
1087
1088	/*
1089	 * Attempt to get a coherent snapshot, by using the thread
1090	 * pre-emption counter updated from within mi_switch() to
1091	 * determine if we were pre-empted during a bcopy() or
1092	 * copyout(). Make 3 attempts at doing this before giving up.
1093	 * If we encounter an error, stop immediately.
1094	 */
1095	tries = 0;
1096	req2 = *req;
1097retry:
1098	generation = curthread->td_generation;
1099	error = SYSCTL_OUT(req, arg1, arg2);
1100	if (error)
1101		return (error);
1102	tries++;
1103	if (generation != curthread->td_generation && tries < 3) {
1104		*req = req2;
1105		goto retry;
1106	}
1107
1108	error = SYSCTL_IN(req, arg1, arg2);
1109
1110	return (error);
1111}
1112
1113/*
1114 * Transfer functions to/from kernel space.
1115 * XXX: rather untested at this point
1116 */
1117static int
1118sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1119{
1120	size_t i = 0;
1121
1122	if (req->oldptr) {
1123		i = l;
1124		if (req->oldlen <= req->oldidx)
1125			i = 0;
1126		else
1127			if (i > req->oldlen - req->oldidx)
1128				i = req->oldlen - req->oldidx;
1129		if (i > 0)
1130			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1131	}
1132	req->oldidx += l;
1133	if (req->oldptr && i != l)
1134		return (ENOMEM);
1135	return (0);
1136}
1137
1138static int
1139sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1140{
1141	if (!req->newptr)
1142		return (0);
1143	if (req->newlen - req->newidx < l)
1144		return (EINVAL);
1145	bcopy((char *)req->newptr + req->newidx, p, l);
1146	req->newidx += l;
1147	return (0);
1148}
1149
1150int
1151kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1152    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1153{
1154	int error = 0;
1155	struct sysctl_req req;
1156
1157	bzero(&req, sizeof req);
1158
1159	req.td = td;
1160	req.flags = flags;
1161
1162	if (oldlenp) {
1163		req.oldlen = *oldlenp;
1164	}
1165	req.validlen = req.oldlen;
1166
1167	if (old) {
1168		req.oldptr= old;
1169	}
1170
1171	if (new != NULL) {
1172		req.newlen = newlen;
1173		req.newptr = new;
1174	}
1175
1176	req.oldfunc = sysctl_old_kernel;
1177	req.newfunc = sysctl_new_kernel;
1178	req.lock = REQ_LOCKED;
1179
1180	SYSCTL_SLOCK();
1181	error = sysctl_root(0, name, namelen, &req);
1182	SYSCTL_SUNLOCK();
1183
1184	if (req.lock == REQ_WIRED && req.validlen > 0)
1185		vsunlock(req.oldptr, req.validlen);
1186
1187	if (error && error != ENOMEM)
1188		return (error);
1189
1190	if (retval) {
1191		if (req.oldptr && req.oldidx > req.validlen)
1192			*retval = req.validlen;
1193		else
1194			*retval = req.oldidx;
1195	}
1196	return (error);
1197}
1198
1199int
1200kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1201    void *new, size_t newlen, size_t *retval, int flags)
1202{
1203        int oid[CTL_MAXNAME];
1204        size_t oidlen, plen;
1205	int error;
1206
1207	oid[0] = 0;		/* sysctl internal magic */
1208	oid[1] = 3;		/* name2oid */
1209	oidlen = sizeof(oid);
1210
1211	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1212	    (void *)name, strlen(name), &plen, flags);
1213	if (error)
1214		return (error);
1215
1216	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1217	    new, newlen, retval, flags);
1218	return (error);
1219}
1220
1221/*
1222 * Transfer function to/from user space.
1223 */
1224static int
1225sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1226{
1227	int error = 0;
1228	size_t i, len, origidx;
1229
1230	origidx = req->oldidx;
1231	req->oldidx += l;
1232	if (req->oldptr == NULL)
1233		return (0);
1234	/*
1235	 * If we have not wired the user supplied buffer and we are currently
1236	 * holding locks, drop a witness warning, as it's possible that
1237	 * write operations to the user page can sleep.
1238	 */
1239	if (req->lock != REQ_WIRED)
1240		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1241		    "sysctl_old_user()");
1242	i = l;
1243	len = req->validlen;
1244	if (len <= origidx)
1245		i = 0;
1246	else {
1247		if (i > len - origidx)
1248			i = len - origidx;
1249		error = copyout(p, (char *)req->oldptr + origidx, i);
1250	}
1251	if (error)
1252		return (error);
1253	if (i < l)
1254		return (ENOMEM);
1255	return (0);
1256}
1257
1258static int
1259sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1260{
1261	int error;
1262
1263	if (!req->newptr)
1264		return (0);
1265	if (req->newlen - req->newidx < l)
1266		return (EINVAL);
1267	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1268	    "sysctl_new_user()");
1269	error = copyin((char *)req->newptr + req->newidx, p, l);
1270	req->newidx += l;
1271	return (error);
1272}
1273
1274/*
1275 * Wire the user space destination buffer.  If set to a value greater than
1276 * zero, the len parameter limits the maximum amount of wired memory.
1277 */
1278int
1279sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1280{
1281	int ret;
1282	size_t wiredlen;
1283
1284	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1285	ret = 0;
1286	if (req->lock == REQ_LOCKED && req->oldptr &&
1287	    req->oldfunc == sysctl_old_user) {
1288		if (wiredlen != 0) {
1289			ret = vslock(req->oldptr, wiredlen);
1290			if (ret != 0) {
1291				if (ret != ENOMEM)
1292					return (ret);
1293				wiredlen = 0;
1294			}
1295		}
1296		req->lock = REQ_WIRED;
1297		req->validlen = wiredlen;
1298	}
1299	return (0);
1300}
1301
1302int
1303sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1304    int *nindx, struct sysctl_req *req)
1305{
1306	struct sysctl_oid *oid;
1307	int indx;
1308
1309	SYSCTL_ASSERT_LOCKED();
1310	oid = SLIST_FIRST(&sysctl__children);
1311	indx = 0;
1312	while (oid && indx < CTL_MAXNAME) {
1313		if (oid->oid_number == name[indx]) {
1314			indx++;
1315			if (oid->oid_kind & CTLFLAG_NOLOCK)
1316				req->lock = REQ_UNLOCKED;
1317			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1318				if (oid->oid_handler != NULL ||
1319				    indx == namelen) {
1320					*noid = oid;
1321					if (nindx != NULL)
1322						*nindx = indx;
1323					return (0);
1324				}
1325				oid = SLIST_FIRST(
1326				    (struct sysctl_oid_list *)oid->oid_arg1);
1327			} else if (indx == namelen) {
1328				*noid = oid;
1329				if (nindx != NULL)
1330					*nindx = indx;
1331				return (0);
1332			} else {
1333				return (ENOTDIR);
1334			}
1335		} else {
1336			oid = SLIST_NEXT(oid, oid_link);
1337		}
1338	}
1339	return (ENOENT);
1340}
1341
1342/*
1343 * Traverse our tree, and find the right node, execute whatever it points
1344 * to, and return the resulting error code.
1345 */
1346
1347static int
1348sysctl_root(SYSCTL_HANDLER_ARGS)
1349{
1350	struct sysctl_oid *oid;
1351	int error, indx, lvl;
1352
1353	SYSCTL_ASSERT_LOCKED();
1354
1355	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1356	if (error)
1357		return (error);
1358
1359	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1360		/*
1361		 * You can't call a sysctl when it's a node, but has
1362		 * no handler.  Inform the user that it's a node.
1363		 * The indx may or may not be the same as namelen.
1364		 */
1365		if (oid->oid_handler == NULL)
1366			return (EISDIR);
1367	}
1368
1369	/* Is this sysctl writable? */
1370	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1371		return (EPERM);
1372
1373	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1374
1375	/* Is this sysctl sensitive to securelevels? */
1376	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1377		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1378		error = securelevel_gt(req->td->td_ucred, lvl);
1379		if (error)
1380			return (error);
1381	}
1382
1383	/* Is this sysctl writable by only privileged users? */
1384	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1385		if (oid->oid_kind & CTLFLAG_PRISON)
1386			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1387		else
1388			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1389		if (error)
1390			return (error);
1391	}
1392
1393	if (!oid->oid_handler)
1394		return (EINVAL);
1395
1396	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1397		arg1 = (int *)arg1 + indx;
1398		arg2 -= indx;
1399	} else {
1400		arg1 = oid->oid_arg1;
1401		arg2 = oid->oid_arg2;
1402	}
1403#ifdef MAC
1404	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1405	    req);
1406	if (error != 0)
1407		return (error);
1408#endif
1409	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1410		mtx_lock(&Giant);
1411	error = oid->oid_handler(oid, arg1, arg2, req);
1412	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1413		mtx_unlock(&Giant);
1414
1415	return (error);
1416}
1417
1418#ifndef _SYS_SYSPROTO_H_
1419struct sysctl_args {
1420	int	*name;
1421	u_int	namelen;
1422	void	*old;
1423	size_t	*oldlenp;
1424	void	*new;
1425	size_t	newlen;
1426};
1427#endif
1428int
1429__sysctl(struct thread *td, struct sysctl_args *uap)
1430{
1431	int error, i, name[CTL_MAXNAME];
1432	size_t j;
1433
1434	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1435		return (EINVAL);
1436
1437 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1438 	if (error)
1439		return (error);
1440
1441	error = userland_sysctl(td, name, uap->namelen,
1442		uap->old, uap->oldlenp, 0,
1443		uap->new, uap->newlen, &j, 0);
1444	if (error && error != ENOMEM)
1445		return (error);
1446	if (uap->oldlenp) {
1447		i = copyout(&j, uap->oldlenp, sizeof(j));
1448		if (i)
1449			return (i);
1450	}
1451	return (error);
1452}
1453
1454/*
1455 * This is used from various compatibility syscalls too.  That's why name
1456 * must be in kernel space.
1457 */
1458int
1459userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1460    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1461    int flags)
1462{
1463	int error = 0, memlocked;
1464	struct sysctl_req req;
1465
1466	bzero(&req, sizeof req);
1467
1468	req.td = td;
1469	req.flags = flags;
1470
1471	if (oldlenp) {
1472		if (inkernel) {
1473			req.oldlen = *oldlenp;
1474		} else {
1475			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1476			if (error)
1477				return (error);
1478		}
1479	}
1480	req.validlen = req.oldlen;
1481
1482	if (old) {
1483		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1484			return (EFAULT);
1485		req.oldptr= old;
1486	}
1487
1488	if (new != NULL) {
1489		if (!useracc(new, newlen, VM_PROT_READ))
1490			return (EFAULT);
1491		req.newlen = newlen;
1492		req.newptr = new;
1493	}
1494
1495	req.oldfunc = sysctl_old_user;
1496	req.newfunc = sysctl_new_user;
1497	req.lock = REQ_LOCKED;
1498
1499#ifdef KTRACE
1500	if (KTRPOINT(curthread, KTR_SYSCTL))
1501		ktrsysctl(name, namelen);
1502#endif
1503
1504	if (req.oldlen > PAGE_SIZE) {
1505		memlocked = 1;
1506		sx_xlock(&sysctlmemlock);
1507	} else
1508		memlocked = 0;
1509	CURVNET_SET(TD_TO_VNET(td));
1510
1511	for (;;) {
1512		req.oldidx = 0;
1513		req.newidx = 0;
1514		SYSCTL_SLOCK();
1515		error = sysctl_root(0, name, namelen, &req);
1516		SYSCTL_SUNLOCK();
1517		if (error != EAGAIN)
1518			break;
1519		uio_yield();
1520	}
1521
1522	CURVNET_RESTORE();
1523
1524	if (req.lock == REQ_WIRED && req.validlen > 0)
1525		vsunlock(req.oldptr, req.validlen);
1526	if (memlocked)
1527		sx_xunlock(&sysctlmemlock);
1528
1529	if (error && error != ENOMEM)
1530		return (error);
1531
1532	if (retval) {
1533		if (req.oldptr && req.oldidx > req.validlen)
1534			*retval = req.validlen;
1535		else
1536			*retval = req.oldidx;
1537	}
1538	return (error);
1539}
1540