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