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