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