kern_sysctl.c revision 1.167 1 /* $NetBSD: kern_sysctl.c,v 1.167 2004/03/25 18:36:49 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.167 2004/03/25 18:36:49 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 pnode = rnode;
745
746 if (newp == NULL)
747 return (EINVAL);
748 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
749 if (error)
750 return (error);
751
752 /*
753 * nodes passed in don't *have* parents
754 */
755 if (nnode.sysctl_parent != NULL)
756 return (EINVAL);
757
758 /*
759 * if we are indeed adding it, it should be a "good" name and
760 * number
761 */
762 nm = nnode.sysctl_num;
763 #if NKSYMS > 0
764 if (nm == CTL_CREATESYM)
765 nm = CTL_CREATE;
766 #endif /* NKSYMS > 0 */
767 if (nm < 0 && nm != CTL_CREATE)
768 return (EINVAL);
769 sz = 0;
770
771 /*
772 * the name can't start with a digit
773 */
774 if (nnode.sysctl_name[sz] >= '0' &&
775 nnode.sysctl_name[sz] <= '9')
776 return (EINVAL);
777
778 /*
779 * the name must be only alphanumerics or - or _, longer than
780 * 0 bytes and less that SYSCTL_NAMELEN
781 */
782 while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
783 if ((nnode.sysctl_name[sz] >= '0' &&
784 nnode.sysctl_name[sz] <= '9') ||
785 (nnode.sysctl_name[sz] >= 'A' &&
786 nnode.sysctl_name[sz] <= 'Z') ||
787 (nnode.sysctl_name[sz] >= 'a' &&
788 nnode.sysctl_name[sz] <= 'z') ||
789 nnode.sysctl_name[sz] == '-' ||
790 nnode.sysctl_name[sz] == '_')
791 sz++;
792 else
793 return (EINVAL);
794 }
795 if (sz == 0 || sz == SYSCTL_NAMELEN)
796 return (EINVAL);
797
798 /*
799 * various checks revolve around size vs type, etc
800 */
801 type = SYSCTL_TYPE(nnode.sysctl_flags);
802 flags = SYSCTL_FLAGS(nnode.sysctl_flags);
803 rw = (flags & CTLFLAG_READWRITE) ? B_WRITE : B_READ;
804 sz = nnode.sysctl_size;
805
806 /*
807 * find out if there's a collision, and if so, let the caller
808 * know what they collided with
809 */
810 node = pnode->sysctl_child;
811 if (((flags & CTLFLAG_ANYNUMBER) && node) ||
812 (node && node->sysctl_flags & CTLFLAG_ANYNUMBER))
813 return (EINVAL);
814 for (ni = at = 0; ni < pnode->sysctl_clen; ni++) {
815 if (nm == node[ni].sysctl_num ||
816 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
817 /*
818 * ignore error here, since we
819 * are already fixed on EEXIST
820 */
821 (void)sysctl_cvt_out(l, v, &node[ni], oldp,
822 *oldlenp, oldlenp);
823 return (EEXIST);
824 }
825 if (nm > node[ni].sysctl_num)
826 at++;
827 }
828
829 /*
830 * use sysctl_ver to add to the tree iff it hasn't changed
831 */
832 if (nnode.sysctl_ver != 0) {
833 /*
834 * a specified value must match either the parent
835 * node's version or the root node's version
836 */
837 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
838 nnode.sysctl_ver != rnode->sysctl_ver) {
839 return (EINVAL);
840 }
841 }
842
843 /*
844 * only the kernel can assign functions to entries
845 */
846 if (l != NULL && nnode.sysctl_func != NULL)
847 return (EPERM);
848
849 /*
850 * only the kernel can create permanent entries, and only then
851 * before the kernel is finished setting itself up
852 */
853 if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
854 return (EPERM);
855 if ((flags & CTLFLAG_PERMANENT) &
856 (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT))
857 return (EPERM);
858 if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) ==
859 (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE))
860 return (EINVAL);
861 if ((flags & CTLFLAG_IMMEDIATE) &&
862 type != CTLTYPE_INT && type != CTLTYPE_QUAD)
863 return (EINVAL);
864
865 /*
866 * check size, or set it if unset and we can figure it out.
867 * kernel created nodes are allowed to have a function instead
868 * of a size (or a data pointer).
869 */
870 switch (type) {
871 case CTLTYPE_NODE:
872 /*
873 * only *i* can assert the size of a node
874 */
875 if (flags & CTLFLAG_ALIAS) {
876 anum = nnode.sysctl_alias;
877 if (anum < 0)
878 return (EINVAL);
879 nnode.sysctl_alias = 0;
880 }
881 if (sz != 0 || nnode.sysctl_data != NULL)
882 return (EINVAL);
883 if (nnode.sysctl_csize != 0 ||
884 nnode.sysctl_clen != 0 ||
885 nnode.sysctl_child != 0)
886 return (EINVAL);
887 if (flags & CTLFLAG_OWNDATA)
888 return (EINVAL);
889 sz = sizeof(struct sysctlnode);
890 break;
891 case CTLTYPE_INT:
892 /*
893 * since an int is an int, if the size is not given or
894 * is wrong, we can "int-uit" it.
895 */
896 if (sz != 0 && sz != sizeof(int))
897 return (EINVAL);
898 sz = sizeof(int);
899 break;
900 case CTLTYPE_STRING:
901 /*
902 * strings are a little more tricky
903 */
904 if (sz == 0) {
905 if (l == NULL) {
906 if (nnode.sysctl_func == NULL) {
907 if (nnode.sysctl_data == NULL)
908 return (EINVAL);
909 else
910 sz = strlen(nnode.sysctl_data) +
911 1;
912 }
913 }
914 else if (nnode.sysctl_data == NULL &&
915 flags & CTLFLAG_OWNDATA) {
916 return (EINVAL);
917 }
918 else {
919 char v[PAGE_SIZE], *e;
920 size_t s;
921
922 /*
923 * we want a rough idea of what the
924 * size is now
925 */
926 e = nnode.sysctl_data;
927 do {
928 error = copyinstr(e, &v[0], sizeof(v),
929 &s);
930 if (error) {
931 if (error != ENAMETOOLONG)
932 return (error);
933 e += PAGE_SIZE;
934 if ((e - 32 * PAGE_SIZE) >
935 (char*)nnode.sysctl_data)
936 return (ERANGE);
937 }
938 } while (error != 0);
939 sz = s + (e - (char*)nnode.sysctl_data);
940 }
941 }
942 break;
943 case CTLTYPE_QUAD:
944 if (sz != 0 && sz != sizeof(u_quad_t))
945 return (EINVAL);
946 sz = sizeof(u_quad_t);
947 break;
948 case CTLTYPE_STRUCT:
949 if (sz == 0) {
950 if (l != NULL || nnode.sysctl_func == NULL)
951 return (EINVAL);
952 if (flags & CTLFLAG_OWNDATA)
953 return (EINVAL);
954 }
955 break;
956 default:
957 return (EINVAL);
958 }
959
960 /*
961 * at this point, if sz is zero, we *must* have a
962 * function to go with it and we can't own it.
963 */
964
965 /*
966 * l ptr own
967 * 0 0 0 -> EINVAL (if no func)
968 * 0 0 1 -> own
969 * 0 1 0 -> kptr
970 * 0 1 1 -> kptr
971 * 1 0 0 -> EINVAL
972 * 1 0 1 -> own
973 * 1 1 0 -> kptr, no own (fault on lookup)
974 * 1 1 1 -> uptr, own
975 */
976 if (type != CTLTYPE_NODE) {
977 if (sz != 0) {
978 if (flags & CTLFLAG_OWNDATA) {
979 own = malloc(sz, M_SYSCTLDATA,
980 M_WAITOK|M_CANFAIL);
981 if (nnode.sysctl_data == NULL)
982 memset(own, 0, sz);
983 else {
984 error = sysctl_copyin(l,
985 nnode.sysctl_data, own, sz);
986 if (error != 0) {
987 FREE(own, M_SYSCTLDATA);
988 return (error);
989 }
990 }
991 }
992 else if ((nnode.sysctl_data != NULL) &&
993 !(flags & CTLFLAG_IMMEDIATE)) {
994 #if NKSYMS > 0
995 if (name[namelen - 1] == CTL_CREATESYM) {
996 char symname[128]; /* XXX enough? */
997 u_long symaddr;
998 size_t symlen;
999
1000 error = sysctl_copyinstr(l,
1001 nnode.sysctl_data, symname,
1002 sizeof(symname), &symlen);
1003 if (error)
1004 return (error);
1005 error = ksyms_getval_from_kernel(NULL,
1006 symname, &symaddr, KSYMS_EXTERN);
1007 if (error)
1008 return (error); /* EINVAL? */
1009 nnode.sysctl_data = (void*)symaddr;
1010 }
1011 #endif /* NKSYMS > 0 */
1012 /*
1013 * Ideally, we'd like to verify here
1014 * that this address is acceptable,
1015 * but...
1016 *
1017 * - it might be valid now, only to
1018 * become invalid later
1019 *
1020 * - it might be invalid only for the
1021 * moment and valid later
1022 *
1023 * - or something else.
1024 *
1025 * Since we can't get a good answer,
1026 * we'll just accept the address as
1027 * given, and fault on individual
1028 * lookups.
1029 */
1030 }
1031 }
1032 else if (nnode.sysctl_func == NULL)
1033 return (EINVAL);
1034 }
1035
1036 /*
1037 * a process can't assign a function to a node, and the kernel
1038 * can't create a node that has no function or data.
1039 * (XXX somewhat redundant check)
1040 */
1041 if (l != NULL || nnode.sysctl_func == NULL) {
1042 if (type != CTLTYPE_NODE &&
1043 nnode.sysctl_data == NULL &&
1044 !(flags & CTLFLAG_IMMEDIATE) &&
1045 own == NULL)
1046 return (EINVAL);
1047 }
1048
1049 #ifdef SYSCTL_DISALLOW_KWRITE
1050 /*
1051 * a process can't create a writable node unless it refers to
1052 * new data.
1053 */
1054 if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
1055 (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY &&
1056 !(flags & CTLFLAG_IMMEDIATE))
1057 return (EPERM);
1058 #endif /* SYSCTL_DISALLOW_KWRITE */
1059
1060 /*
1061 * make sure there's somewhere to put the new stuff.
1062 */
1063 if (pnode->sysctl_child == NULL) {
1064 if (flags & CTLFLAG_ANYNUMBER)
1065 error = sysctl_alloc(pnode, 1);
1066 else
1067 error = sysctl_alloc(pnode, 0);
1068 if (error)
1069 return (error);
1070 }
1071 node = pnode->sysctl_child;
1072
1073 /*
1074 * no collisions, so pick a good dynamic number if we need to.
1075 */
1076 if (nm == CTL_CREATE) {
1077 nm = ++sysctl_root.sysctl_num;
1078 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
1079 if (nm == node[ni].sysctl_num) {
1080 nm++;
1081 ni = -1;
1082 }
1083 else if (nm > node[ni].sysctl_num)
1084 at = ni + 1;
1085 }
1086 }
1087
1088 /*
1089 * oops...ran out of space
1090 */
1091 if (pnode->sysctl_clen == pnode->sysctl_csize) {
1092 error = sysctl_realloc(pnode);
1093 if (error)
1094 return (error);
1095 node = pnode->sysctl_child;
1096 }
1097
1098 /*
1099 * insert new node data
1100 */
1101 if (at < pnode->sysctl_clen) {
1102 int t;
1103
1104 /*
1105 * move the nodes that should come after the new one
1106 */
1107 memmove(&node[at + 1], &node[at],
1108 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
1109 memset(&node[at], 0, sizeof(struct sysctlnode));
1110 node[at].sysctl_parent = pnode;
1111 /*
1112 * and...reparent any children of any moved nodes
1113 */
1114 for (ni = at; ni <= pnode->sysctl_clen; ni++)
1115 if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
1116 for (t = 0; t < node[ni].sysctl_clen; t++)
1117 node[ni].sysctl_child[t].sysctl_parent =
1118 &node[ni];
1119 }
1120 node = &node[at];
1121 pnode->sysctl_clen++;
1122
1123 strlcpy(node->sysctl_name, nnode.sysctl_name,
1124 sizeof(node->sysctl_name));
1125 node->sysctl_num = nm;
1126 node->sysctl_size = sz;
1127 node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */
1128 node->sysctl_csize = 0;
1129 node->sysctl_clen = 0;
1130 if (own) {
1131 node->sysctl_data = own;
1132 node->sysctl_flags |= CTLFLAG_OWNDATA;
1133 }
1134 else if (flags & CTLFLAG_ALIAS) {
1135 node->sysctl_alias = anum;
1136 }
1137 else if (flags & CTLFLAG_IMMEDIATE) {
1138 switch (type) {
1139 case CTLTYPE_INT:
1140 node->sysctl_idata = nnode.sysctl_idata;
1141 break;
1142 case CTLTYPE_QUAD:
1143 node->sysctl_qdata = nnode.sysctl_qdata;
1144 break;
1145 }
1146 }
1147 else {
1148 node->sysctl_data = nnode.sysctl_data;
1149 node->sysctl_flags &= ~CTLFLAG_OWNDATA;
1150 }
1151 node->sysctl_func = nnode.sysctl_func;
1152 node->sysctl_child = NULL;
1153 /* node->sysctl_parent should already be done */
1154
1155 /*
1156 * update "version" on path to "root"
1157 */
1158 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1159 ;
1160 pnode = node;
1161 for (nm = rnode->sysctl_ver + 1; pnode != NULL;
1162 pnode = pnode->sysctl_parent)
1163 pnode->sysctl_ver = nm;
1164
1165 error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp);
1166
1167 return (error);
1168 }
1169
1170 /*
1171 * ********************************************************************
1172 * A wrapper around sysctl_create() that prints the thing we're trying
1173 * to add.
1174 * ********************************************************************
1175 */
1176 #ifdef SYSCTL_DEBUG_CREATE
1177 int _sysctl_create(SYSCTLFN_RWPROTO);
1178 int
1179 _sysctl_create(SYSCTLFN_RWARGS)
1180 {
1181 const struct sysctlnode *node;
1182 int k, rc, ni, nl = namelen + (name - oname);
1183
1184 node = newp;
1185
1186 printf("namelen %d (", nl);
1187 for (ni = 0; ni < nl - 1; ni++)
1188 printf(" %d", oname[ni]);
1189 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1190 k = node->sysctl_num,
1191 node->sysctl_name,
1192 node->sysctl_flags,
1193 SYSCTL_FLAGS(node->sysctl_flags),
1194 SYSCTL_TYPE(node->sysctl_flags),
1195 node->sysctl_size);
1196
1197 node = rnode;
1198 rc = sysctl_create(SYSCTLFN_CALL(rnode));
1199
1200 printf("sysctl_create(");
1201 for (ni = 0; ni < nl - 1; ni++)
1202 printf(" %d", oname[ni]);
1203 printf(" %d ) returned %d\n", k, rc);
1204
1205 return (rc);
1206 }
1207 #define sysctl_create _sysctl_create
1208 #endif /* SYSCTL_DEBUG_CREATE */
1209
1210 /*
1211 * sysctl_destroy -- Removes a node (as described by newp) from the
1212 * given tree, returning (if successful) a copy of the dead node in
1213 * oldp. Since we're removing stuff, there's not much to check.
1214 */
1215 int
1216 sysctl_destroy(SYSCTLFN_RWARGS)
1217 {
1218 struct sysctlnode *node, *pnode, onode, nnode;
1219 int ni, error, v;
1220
1221 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1222 printf("sysctl_destroy: rnode %p wrong version\n", rnode);
1223 return (EINVAL);
1224 }
1225
1226 error = 0;
1227
1228 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
1229 return (EINVAL);
1230
1231 /*
1232 * processes can only destroy nodes at securelevel 0, must be
1233 * root, and can't remove nodes from a parent that's not
1234 * writeable
1235 */
1236 if (l != NULL) {
1237 #ifndef SYSCTL_DISALLOW_CREATE
1238 if (securelevel > 0)
1239 return (EPERM);
1240 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
1241 if (error)
1242 return (error);
1243 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
1244 #endif /* SYSCTL_DISALLOW_CREATE */
1245 return (EPERM);
1246 }
1247
1248 /*
1249 * nothing can remove a node if:
1250 * the node is permanent (checked later) or
1251 * the tree itself is not writeable or
1252 * the entire sysctl system is not writeable
1253 */
1254 if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
1255 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))
1256 return (EPERM);
1257
1258 if (newp == NULL)
1259 return (EINVAL);
1260 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
1261 if (error)
1262 return (error);
1263 memset(&onode, 0, sizeof(struct sysctlnode));
1264
1265 node = rnode->sysctl_child;
1266 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
1267 if (nnode.sysctl_num == node[ni].sysctl_num) {
1268 /*
1269 * if name specified, must match
1270 */
1271 if (nnode.sysctl_name[0] != '\0' &&
1272 strcmp(nnode.sysctl_name, node[ni].sysctl_name))
1273 continue;
1274 /*
1275 * if version specified, must match
1276 */
1277 if (nnode.sysctl_ver != 0 &&
1278 nnode.sysctl_ver != node[ni].sysctl_ver)
1279 continue;
1280 /*
1281 * this must be the one
1282 */
1283 break;
1284 }
1285 }
1286 if (ni == rnode->sysctl_clen)
1287 return (ENOENT);
1288 node = &node[ni];
1289 pnode = node->sysctl_parent;
1290
1291 /*
1292 * if the kernel says permanent, it is, so there. nyah.
1293 */
1294 if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT)
1295 return (EPERM);
1296
1297 /*
1298 * can't delete non-empty nodes
1299 */
1300 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
1301 node->sysctl_clen != 0)
1302 return (ENOTEMPTY);
1303
1304 /*
1305 * if the node "owns" data, release it now
1306 */
1307 if (node->sysctl_flags & CTLFLAG_OWNDATA) {
1308 if (node->sysctl_data != NULL)
1309 FREE(node->sysctl_data, M_SYSCTLDATA);
1310 node->sysctl_data = NULL;
1311 }
1312 if (node->sysctl_flags & CTLFLAG_OWNDESC) {
1313 if (node->sysctl_desc != NULL)
1314 FREE(node->sysctl_desc, M_SYSCTLDATA);
1315 node->sysctl_desc = NULL;
1316 }
1317
1318 /*
1319 * if the node to be removed is not the last one on the list,
1320 * move the remaining nodes up, and reparent any grandchildren
1321 */
1322 onode = *node;
1323 if (ni < pnode->sysctl_clen - 1) {
1324 int t;
1325
1326 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
1327 (pnode->sysctl_clen - ni - 1) *
1328 sizeof(struct sysctlnode));
1329 for (; ni < pnode->sysctl_clen - 1; ni++)
1330 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
1331 CTLTYPE_NODE)
1332 for (t = 0;
1333 t < pnode->sysctl_child[ni].sysctl_clen;
1334 t++)
1335 pnode->sysctl_child[ni].sysctl_child[t].
1336 sysctl_parent =
1337 &pnode->sysctl_child[ni];
1338 ni = pnode->sysctl_clen - 1;
1339 node = &pnode->sysctl_child[ni];
1340 }
1341
1342 /*
1343 * reset the space we just vacated
1344 */
1345 memset(node, 0, sizeof(struct sysctlnode));
1346 node->sysctl_parent = pnode;
1347 pnode->sysctl_clen--;
1348
1349 /*
1350 * if this parent just lost its last child, nuke the creche
1351 */
1352 if (pnode->sysctl_clen == 0) {
1353 FREE(pnode->sysctl_child, M_SYSCTLNODE);
1354 pnode->sysctl_csize = 0;
1355 pnode->sysctl_child = NULL;
1356 }
1357
1358 /*
1359 * update "version" on path to "root"
1360 */
1361 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1362 ;
1363 for (ni = rnode->sysctl_ver + 1; pnode != NULL;
1364 pnode = pnode->sysctl_parent)
1365 pnode->sysctl_ver = ni;
1366
1367 error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp);
1368
1369 return (error);
1370 }
1371
1372 /*
1373 * sysctl_lookup -- Handles copyin/copyout of new and old values.
1374 * Partial reads are globally allowed. Only root can write to things
1375 * unless the node says otherwise.
1376 */
1377 int
1378 sysctl_lookup(SYSCTLFN_RWARGS)
1379 {
1380 int error, rw;
1381 size_t sz, len;
1382 void *d;
1383
1384 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1385 printf("sysctl_lookup: rnode %p wrong version\n", rnode);
1386 return (EINVAL);
1387 }
1388
1389 error = 0;
1390
1391 /*
1392 * you can't "look up" a node. you can "query" it, but you
1393 * can't "look it up".
1394 */
1395 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
1396 return (EINVAL);
1397
1398 /*
1399 * some nodes are private, so only root can look into them.
1400 */
1401 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
1402 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1403 return (error);
1404
1405 /*
1406 * if a node wants to be writable according to different rules
1407 * other than "only root can write to stuff unless a flag is
1408 * set", then it needs its own function which should have been
1409 * called and not us.
1410 */
1411 if (l != NULL && newp != NULL &&
1412 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
1413 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1414 return (error);
1415
1416 /*
1417 * is this node supposedly writable?
1418 */
1419 rw = 0;
1420 switch (rnode->sysctl_flags & CTLFLAG_READWRITE) {
1421 case CTLFLAG_READONLY1:
1422 rw = (securelevel < 1) ? 1 : 0;
1423 break;
1424 case CTLFLAG_READONLY2:
1425 rw = (securelevel < 2) ? 1 : 0;
1426 break;
1427 case CTLFLAG_READWRITE:
1428 rw = 1;
1429 break;
1430 }
1431
1432 /*
1433 * it appears not to be writable at this time, so if someone
1434 * tried to write to it, we must tell them to go away
1435 */
1436 if (!rw && newp != NULL)
1437 return (EPERM);
1438
1439 /*
1440 * step one, copy out the stuff we have presently
1441 */
1442 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
1443 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1444 case CTLTYPE_INT:
1445 d = &rnode->sysctl_idata;
1446 break;
1447 case CTLTYPE_QUAD:
1448 d = &rnode->sysctl_qdata;
1449 break;
1450 default:
1451 return (EINVAL);
1452 }
1453 }
1454 else
1455 d = rnode->sysctl_data;
1456 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
1457 sz = strlen(d) + 1; /* XXX@@@ possible fault here */
1458 else
1459 sz = rnode->sysctl_size;
1460 if (oldp != NULL)
1461 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
1462 if (error)
1463 return (error);
1464 *oldlenp = sz;
1465
1466 /*
1467 * are we done?
1468 */
1469 if (newp == NULL || newlen == 0)
1470 return (0);
1471
1472 /*
1473 * hmm...not done. must now "copy in" new value. re-adjust
1474 * sz to maximum value (strings are "weird").
1475 */
1476 sz = rnode->sysctl_size;
1477 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1478 case CTLTYPE_INT:
1479 case CTLTYPE_QUAD:
1480 case CTLTYPE_STRUCT:
1481 /*
1482 * these data must be *exactly* the same size coming
1483 * in.
1484 */
1485 if (newlen != sz)
1486 return (EINVAL);
1487 error = sysctl_copyin(l, newp, d, sz);
1488 break;
1489 case CTLTYPE_STRING: {
1490 /*
1491 * strings, on the other hand, can be shorter, and we
1492 * let userland be sloppy about the trailing nul.
1493 */
1494 char *newbuf;
1495
1496 /*
1497 * too much new string?
1498 */
1499 if (newlen > sz)
1500 return (EINVAL);
1501
1502 /*
1503 * temporary copy of new inbound string
1504 */
1505 len = MIN(sz, newlen);
1506 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1507 if (newbuf == NULL)
1508 return (ENOMEM);
1509 error = sysctl_copyin(l, newp, newbuf, len);
1510 if (error) {
1511 FREE(newbuf, M_SYSCTLDATA);
1512 return (error);
1513 }
1514
1515 /*
1516 * did they null terminate it, or do we have space
1517 * left to do it ourselves?
1518 */
1519 if (newbuf[len - 1] != '\0' && len == sz) {
1520 FREE(newbuf, M_SYSCTLDATA);
1521 return (EINVAL);
1522 }
1523
1524 /*
1525 * looks good, so pop it into place and zero the rest.
1526 */
1527 if (len > 0)
1528 memcpy(rnode->sysctl_data, newbuf, len);
1529 if (sz != len)
1530 memset((char*)rnode->sysctl_data + len, 0, sz - len);
1531 FREE(newbuf, M_SYSCTLDATA);
1532 break;
1533 }
1534 default:
1535 return (EINVAL);
1536 }
1537
1538 return (error);
1539 }
1540
1541 /*
1542 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1543 * purport to handle it. This interface isn't fully fleshed out yet,
1544 * unfortunately.
1545 */
1546 static int
1547 sysctl_mmap(SYSCTLFN_RWARGS)
1548 {
1549 struct sysctlnode nnode, *node;
1550 int error;
1551
1552 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1553 printf("sysctl_mmap: rnode %p wrong version\n", rnode);
1554 return (EINVAL);
1555 }
1556
1557 /*
1558 * let's just pretend that didn't happen, m'kay?
1559 */
1560 if (l == NULL)
1561 return (EPERM);
1562
1563 /*
1564 * is this a sysctlnode description of an mmap request?
1565 */
1566 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1567 return (EINVAL);
1568 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
1569 if (error)
1570 return (error);
1571
1572 /*
1573 * does the node they asked for exist?
1574 */
1575 if (namelen != 1)
1576 return (EOPNOTSUPP);
1577 node = rnode;
1578 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1579 if (error)
1580 return (error);
1581
1582 /*
1583 * does this node that we have found purport to handle mmap?
1584 */
1585 if (node->sysctl_func == NULL ||
1586 !(node->sysctl_flags & CTLFLAG_MMAP))
1587 return (EOPNOTSUPP);
1588
1589 /*
1590 * well...okay, they asked for it.
1591 */
1592 return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1593 }
1594
1595 int
1596 sysctl_describe(SYSCTLFN_ARGS)
1597 {
1598 struct sysctldesc *d;
1599 char buf[1024];
1600 size_t sz, left, tot;
1601 int i, error, v = -1;
1602 struct sysctlnode *node;
1603 struct sysctlnode dnode;
1604
1605 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1606 printf("sysctl_query: rnode %p wrong version\n", rnode);
1607 return (EINVAL);
1608 }
1609
1610 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
1611 return (ENOTDIR);
1612 if (namelen != 1 || name[0] != CTL_DESCRIBE)
1613 return (EINVAL);
1614
1615 /*
1616 * get ready...
1617 */
1618 error = 0;
1619 d = (void*)&buf[0];
1620 tot = 0;
1621 node = rnode->sysctl_child;
1622 left = *oldlenp;
1623
1624 /*
1625 * no request -> all descriptions at this level
1626 * request with desc unset -> just this node
1627 * request with desc set -> set descr for this node
1628 */
1629 if (newp != NULL) {
1630 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode);
1631 if (error)
1632 return (error);
1633 if (dnode.sysctl_desc != NULL) {
1634 /*
1635 * processes cannot set descriptions above
1636 * securelevel 0. and must be root. blah
1637 * blah blah. a couple more checks are made
1638 * once we find the node we want.
1639 */
1640 if (l != NULL) {
1641 #ifndef SYSCTL_DISALLOW_CREATE
1642 if (securelevel > 0)
1643 return (EPERM);
1644 error = suser(l->l_proc->p_ucred,
1645 &l->l_proc->p_acflag);
1646 if (error)
1647 return (error);
1648 #else /* SYSCTL_DISALLOW_CREATE */
1649 return (EPERM);
1650 #endif /* SYSCTL_DISALLOW_CREATE */
1651 }
1652
1653 /*
1654 * find node and try to set the description on it
1655 */
1656 for (i = 0; i < rnode->sysctl_clen; i++)
1657 if (node[i].sysctl_num == dnode.sysctl_num)
1658 break;
1659 if (i == rnode->sysctl_clen)
1660 return (ENOENT);
1661 node = &node[i];
1662
1663 /*
1664 * did the caller specify a node version?
1665 */
1666 if (dnode.sysctl_ver != 0 &&
1667 dnode.sysctl_ver != node->sysctl_ver)
1668 return (EINVAL);
1669
1670 /*
1671 * okay...some rules:
1672 * (1) no one can set a description on a
1673 * permanent node (it must be set when
1674 * using createv)
1675 * (2) processes cannot *change* a description
1676 * (3) processes *can*, however, set a
1677 * description on a read-only node so that
1678 * one can be created and then described
1679 * in two steps
1680 * anything else come to mind?
1681 */
1682 if (node->sysctl_flags & CTLFLAG_PERMANENT)
1683 return (EPERM);
1684 if (l != NULL && node->sysctl_desc != NULL)
1685 return (EPERM);
1686
1687 /*
1688 * right, let's go ahead. the first step is
1689 * making the description into something the
1690 * node can "own", if need be.
1691 */
1692 if (l != NULL ||
1693 dnode.sysctl_flags & CTLFLAG_OWNDESC) {
1694 char *nd, k[1024];
1695
1696 error = sysctl_copyinstr(l, dnode.sysctl_desc,
1697 &k[0], sizeof(k), &sz);
1698 if (error)
1699 return (error);
1700 nd = malloc(sz, M_SYSCTLDATA,
1701 M_WAITOK|M_CANFAIL);
1702 if (nd == NULL)
1703 return (ENOMEM);
1704 memcpy(nd, k, sz);
1705 dnode.sysctl_flags |= CTLFLAG_OWNDESC;
1706 dnode.sysctl_desc = nd;
1707 }
1708
1709 /*
1710 * now "release" the old description and
1711 * attach the new one. ta-da.
1712 */
1713 if ((node->sysctl_flags & CTLFLAG_OWNDESC) &&
1714 node->sysctl_desc != NULL)
1715 free((void*)node->sysctl_desc, M_SYSCTLDATA);
1716 node->sysctl_desc = dnode.sysctl_desc;
1717 node->sysctl_flags |=
1718 (dnode.sysctl_flags & CTLFLAG_OWNDESC);
1719
1720 /*
1721 * now we "fall out" and into the loop which
1722 * will copy the new description back out for
1723 * those interested parties
1724 */
1725 }
1726 }
1727
1728 /*
1729 * scan for one description or just retrieve all descriptions
1730 */
1731 for (i = 0; i < rnode->sysctl_clen; i++) {
1732 /*
1733 * did they ask for the description of only one node?
1734 */
1735 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num)
1736 continue;
1737
1738 /*
1739 * don't describe "private" nodes to non-suser users
1740 */
1741 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) &&
1742 !(suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)))
1743 continue;
1744
1745 /*
1746 * is this description "valid"?
1747 */
1748 memset(&buf[0], 0, sizeof(buf));
1749 if (node[i].sysctl_desc == NULL)
1750 sz = 1;
1751 else if (copystr(node[i].sysctl_desc, &d->descr_str[0],
1752 sizeof(buf) - sizeof(*d), &sz) != 0) {
1753 /*
1754 * erase possible partial description
1755 */
1756 memset(&buf[0], 0, sizeof(buf));
1757 sz = 1;
1758 }
1759
1760 /*
1761 * we've got it, stuff it into the caller's buffer
1762 */
1763 d->descr_num = node[i].sysctl_num;
1764 d->descr_ver = node[i].sysctl_ver;
1765 d->descr_len = sz; /* includes trailing nul */
1766 sz = (caddr_t)NEXT_DESCR(d) - (caddr_t)d;
1767 if (oldp != NULL && left >= sz) {
1768 error = sysctl_copyout(l, d, oldp, sz);
1769 if (error)
1770 return (error);
1771 left -= sz;
1772 oldp = (void*)__sysc_desc_adv(oldp, d->descr_len);
1773 }
1774 tot += sz;
1775
1776 /*
1777 * if we get this far with v not "unset", they asked
1778 * for a specific node and we found it
1779 */
1780 if (v != -1)
1781 break;
1782 }
1783
1784 /*
1785 * did we find it after all?
1786 */
1787 if (v != -1 && tot == 0)
1788 error = ENOENT;
1789 else
1790 *oldlenp = tot;
1791
1792 return (error);
1793 }
1794
1795 /*
1796 * ********************************************************************
1797 * Section 3: Create and destroy from inside the kernel
1798 * ********************************************************************
1799 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1800 * interfaces for the kernel to fling new entries into the mib and rip
1801 * them out later. In the case of sysctl_createv(), the returned copy
1802 * of the node (see sysctl_create()) will be translated back into a
1803 * pointer to the actual node.
1804 *
1805 * Note that sysctl_createv() will return 0 if the create request
1806 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1807 * will return 0 if the node to be destroyed already does not exist
1808 * (aka rm -f) or if it is a parent of other nodes.
1809 *
1810 * This allows two (or more) different subsystems to assert sub-tree
1811 * existence before populating their own nodes, and to remove their
1812 * own nodes without orphaning the others when they are done.
1813 * ********************************************************************
1814 */
1815 int
1816 sysctl_createv(struct sysctllog **log, int cflags,
1817 struct sysctlnode **rnode, struct sysctlnode **cnode,
1818 int flags, int type, const char *namep, const char *descr,
1819 sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1820 ...)
1821 {
1822 va_list ap;
1823 int error, ni, namelen, name[CTL_MAXNAME];
1824 struct sysctlnode *pnode, nnode, onode;
1825 size_t sz;
1826
1827 if (cnode != NULL)
1828 *cnode = NULL;
1829 if (cflags != 0)
1830 return (EINVAL);
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
1840 /*
1841 * what is it?
1842 */
1843 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
1844 if (log != NULL)
1845 flags &= ~CTLFLAG_PERMANENT;
1846
1847 /*
1848 * where do we put it?
1849 */
1850 va_start(ap, newlen);
1851 namelen = 0;
1852 ni = -1;
1853 do {
1854 if (++ni == CTL_MAXNAME)
1855 return (ENAMETOOLONG);
1856 name[ni] = va_arg(ap, int);
1857 /*
1858 * sorry, this is not supported from here
1859 */
1860 if (name[ni] == CTL_CREATESYM)
1861 return (EINVAL);
1862 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
1863 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
1864 va_end(ap);
1865
1866 /*
1867 * what's it called
1868 */
1869 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >
1870 sizeof(nnode.sysctl_name))
1871 return (ENAMETOOLONG);
1872
1873 /*
1874 * cons up the description of the new node
1875 */
1876 nnode.sysctl_num = name[namelen - 1];
1877 name[namelen - 1] = CTL_CREATE;
1878 nnode.sysctl_size = newlen;
1879 nnode.sysctl_flags = flags;
1880 if (type == CTLTYPE_NODE) {
1881 nnode.sysctl_csize = 0;
1882 nnode.sysctl_clen = 0;
1883 nnode.sysctl_child = NULL;
1884 if (flags & CTLFLAG_ALIAS)
1885 nnode.sysctl_alias = qv;
1886 }
1887 else if (flags & CTLFLAG_IMMEDIATE) {
1888 switch (type) {
1889 case CTLTYPE_INT:
1890 nnode.sysctl_idata = qv;
1891 break;
1892 case CTLTYPE_QUAD:
1893 nnode.sysctl_qdata = qv;
1894 break;
1895 default:
1896 return (EINVAL);
1897 }
1898 }
1899 else {
1900 nnode.sysctl_data = newp;
1901 }
1902 nnode.sysctl_func = func;
1903 nnode.sysctl_parent = NULL;
1904 nnode.sysctl_ver = 0;
1905
1906 /*
1907 * initialize lock state -- we need locks if the main tree has
1908 * been marked as complete, but since we could be called from
1909 * either there, or from a device driver (say, at device
1910 * insertion), or from an lkm (at lkm load time, say), we
1911 * don't really want to "wait"...
1912 */
1913 error = sysctl_lock(NULL, NULL, 0);
1914 if (error)
1915 return (error);
1916
1917 /*
1918 * locate the prospective parent of the new node, and if we
1919 * find it, add the new node.
1920 */
1921 sz = sizeof(onode);
1922 pnode = rnode ? *rnode : NULL;
1923 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1924 if (error) {
1925 sysctl_unlock(NULL);
1926 return (error);
1927 }
1928 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
1929 &nnode, sizeof(nnode), &name[0], NULL,
1930 pnode);
1931
1932 /*
1933 * unfortunately the node we wanted to create is already
1934 * there. if the node that's already there is a reasonable
1935 * facsimile of the node we wanted to create, just pretend
1936 * (for the caller's benefit) that we managed to create the
1937 * node they wanted.
1938 */
1939 if (error == EEXIST) {
1940 /* name is the same as requested... */
1941 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
1942 /* they want the same function... */
1943 nnode.sysctl_func == onode.sysctl_func &&
1944 /* number is the same as requested, or... */
1945 (nnode.sysctl_num == onode.sysctl_num ||
1946 /* they didn't pick a number... */
1947 nnode.sysctl_num == CTL_CREATE)) {
1948 /*
1949 * collision here from trying to create
1950 * something that already existed; let's give
1951 * our customers a hand and tell them they got
1952 * what they wanted.
1953 */
1954 #ifdef SYSCTL_DEBUG_CREATE
1955 printf("cleared\n");
1956 #endif /* SYSCTL_DEBUG_CREATE */
1957 error = 0;
1958 }
1959 }
1960
1961 if (error == 0 &&
1962 (cnode != NULL || log != NULL || descr != NULL)) {
1963 /*
1964 * sysctl_create() gave us back a copy of the node,
1965 * but we need to know where it actually is...
1966 */
1967 pnode = rnode ? *rnode : NULL;
1968 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1969
1970 /*
1971 * manual scan of last layer so that aliased nodes
1972 * aren't followed.
1973 */
1974 if (error == 0) {
1975 for (ni = 0; ni < pnode->sysctl_clen; ni++)
1976 if (pnode->sysctl_child[ni].sysctl_num ==
1977 onode.sysctl_num)
1978 break;
1979 if (ni < pnode->sysctl_clen)
1980 pnode = &pnode->sysctl_child[ni];
1981 else
1982 error = ENOENT;
1983 }
1984
1985 /*
1986 * not expecting an error here, but...
1987 */
1988 if (error == 0) {
1989 if (log != NULL)
1990 sysctl_log_add(log, pnode);
1991 if (cnode != NULL)
1992 *cnode = pnode;
1993 if (descr != NULL) {
1994 if (flags & CTLFLAG_OWNDESC) {
1995 size_t l = strlen(descr) + 1;
1996 char *d = malloc(l, M_SYSCTLDATA,
1997 M_WAITOK|M_CANFAIL);
1998 if (d != NULL) {
1999 memcpy(d, descr, l);
2000 pnode->sysctl_desc = d;
2001 }
2002 }
2003 else
2004 pnode->sysctl_desc = descr;
2005 }
2006 }
2007 else {
2008 printf("sysctl_create succeeded but node not found?!\n");
2009 /*
2010 * confusing, but the create said it
2011 * succeeded, so...
2012 */
2013 error = 0;
2014 }
2015 }
2016
2017 /*
2018 * now it should be safe to release the lock state. note that
2019 * the pointer to the newly created node being passed back may
2020 * not be "good" for very long.
2021 */
2022 sysctl_unlock(NULL);
2023
2024 if (error != 0) {
2025 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
2026 nnode.sysctl_name, error);
2027 #if 0
2028 if (error != ENOENT)
2029 sysctl_dump(&onode);
2030 #endif
2031 }
2032
2033 return (error);
2034 }
2035
2036 int
2037 sysctl_destroyv(struct sysctlnode *rnode, ...)
2038 {
2039 va_list ap;
2040 int error, name[CTL_MAXNAME], namelen, ni;
2041 struct sysctlnode *pnode, *node, dnode;
2042 size_t sz;
2043
2044 va_start(ap, rnode);
2045 namelen = 0;
2046 ni = 0;
2047 do {
2048 if (ni == CTL_MAXNAME)
2049 return (ENAMETOOLONG);
2050 name[ni] = va_arg(ap, int);
2051 } while (name[ni++] != CTL_EOL);
2052 namelen = ni - 1;
2053 va_end(ap);
2054
2055 /*
2056 * i can't imagine why we'd be destroying a node when the tree
2057 * wasn't complete, but who knows?
2058 */
2059 error = sysctl_lock(NULL, NULL, 0);
2060 if (error)
2061 return (error);
2062
2063 /*
2064 * where is it?
2065 */
2066 node = rnode;
2067 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
2068 if (error) {
2069 /* they want it gone and it's not there, so... */
2070 sysctl_unlock(NULL);
2071 return (error == ENOENT ? 0 : error);
2072 }
2073
2074 /*
2075 * set up the deletion
2076 */
2077 pnode = node;
2078 node = &dnode;
2079 memset(&dnode, 0, sizeof(dnode));
2080 dnode.sysctl_num = name[namelen - 1];
2081
2082 /*
2083 * we found it, now let's nuke it
2084 */
2085 name[namelen - 1] = CTL_DESTROY;
2086 sz = 0;
2087 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
2088 node, sizeof(*node), &name[0], NULL,
2089 pnode);
2090 if (error == ENOTEMPTY) {
2091 /*
2092 * think of trying to delete "foo" when "foo.bar"
2093 * (which someone else put there) is still in
2094 * existence
2095 */
2096 error = 0;
2097
2098 /*
2099 * dunno who put the description there, but if this
2100 * node can ever be removed, we need to make sure the
2101 * string doesn't go out of context. that means we
2102 * need to find the node that's still there (don't use
2103 * sysctl_locate() because that follows aliasing).
2104 */
2105 node = pnode->sysctl_child;
2106 for (ni = 0; ni < pnode->sysctl_clen; ni++)
2107 if (node[ni].sysctl_num == dnode.sysctl_num)
2108 break;
2109 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL;
2110
2111 /*
2112 * if we found it, and this node has a description,
2113 * and this node can be released, and it doesn't
2114 * already own its own description...sigh. :)
2115 */
2116 if (node != NULL && node->sysctl_desc != NULL &&
2117 !(node->sysctl_flags & CTLFLAG_PERMANENT) &&
2118 !(node->sysctl_flags & CTLFLAG_OWNDESC)) {
2119 char *d;
2120
2121 sz = strlen(node->sysctl_desc) + 1;
2122 d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2123 if (d != NULL) {
2124 memcpy(d, node->sysctl_desc, sz);
2125 node->sysctl_desc = d;
2126 node->sysctl_flags |= CTLFLAG_OWNDESC;
2127 }
2128 else {
2129 /*
2130 * XXX drop the description? be
2131 * afraid? don't care?
2132 */
2133 }
2134 }
2135 }
2136
2137 sysctl_unlock(NULL);
2138
2139 return (error);
2140 }
2141
2142 #if 0
2143 /*
2144 * ********************************************************************
2145 * the dump routine. i haven't yet decided how (if at all) i'll call
2146 * this from userland when it's in the kernel.
2147 * ********************************************************************
2148 */
2149 static const char *
2150 sf(int f)
2151 {
2152 static char s[256];
2153 char *c;
2154
2155 s[0] = '\0';
2156 c = "";
2157
2158 #define print_flag(_f, _s, _c, _q, _x) \
2159 if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
2160 strlcat((_s), (_c), sizeof(_s)); \
2161 strlcat((_s), __STRING(_q), sizeof(_s)); \
2162 (_c) = ","; \
2163 (_f) &= ~__CONCAT(CTLFLAG_,_x); \
2164 }
2165
2166 print_flag(f, s, c, READONLY, READWRITE);
2167 print_flag(f, s, c, READONLY1, READWRITE);
2168 print_flag(f, s, c, READONLY2, READWRITE);
2169 print_flag(f, s, c, READWRITE, READWRITE);
2170 print_flag(f, s, c, ANYWRITE, ANYWRITE);
2171 print_flag(f, s, c, PRIVATE, PRIVATE);
2172 print_flag(f, s, c, PERMANENT, PERMANENT);
2173 print_flag(f, s, c, OWNDATA, OWNDATA);
2174 print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
2175 print_flag(f, s, c, HEX, HEX);
2176 print_flag(f, s, c, ROOT, ROOT);
2177 print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
2178 print_flag(f, s, c, HIDDEN, HIDDEN);
2179 print_flag(f, s, c, ALIAS, ALIAS);
2180 #undef print_flag
2181
2182 if (f) {
2183 char foo[9];
2184 snprintf(foo, sizeof(foo), "%x", f);
2185 strlcat(s, c, sizeof(s));
2186 strlcat(s, foo, sizeof(s));
2187 }
2188
2189 return (s);
2190 }
2191
2192 static const char *
2193 st(int t)
2194 {
2195
2196 switch (t) {
2197 case CTLTYPE_NODE:
2198 return "NODE";
2199 case CTLTYPE_INT:
2200 return "INT";
2201 case CTLTYPE_STRING:
2202 return "STRING";
2203 case CTLTYPE_QUAD:
2204 return "QUAD";
2205 case CTLTYPE_STRUCT:
2206 return "STRUCT";
2207 }
2208
2209 return "???";
2210 }
2211
2212 void
2213 sysctl_dump(const struct sysctlnode *d)
2214 {
2215 static char nmib[64], smib[256];
2216 static int indent;
2217 struct sysctlnode *n;
2218 char *np, *sp, tmp[20];
2219 int i;
2220
2221 if (d == NULL)
2222 return;
2223
2224 np = &nmib[strlen(nmib)];
2225 sp = &smib[strlen(smib)];
2226
2227 if (!(d->sysctl_flags & CTLFLAG_ROOT)) {
2228 snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
2229 strcat(nmib, ".");
2230 strcat(smib, ".");
2231 strcat(nmib, tmp);
2232 strcat(smib, d->sysctl_name);
2233 printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
2234 SYSCTL_TYPE(d->sysctl_flags));
2235 }
2236
2237 if (1) {
2238 printf("%*s%p:\tsysctl_name [%s]\n", indent, "",
2239 d, d->sysctl_name);
2240 printf("%*s\t\tsysctl_num %d\n", indent, "",
2241 d->sysctl_num);
2242 printf("%*s\t\tsysctl_flags %x (flags=%x<%s> type=%d<%s> "
2243 "size=%zu)\n",
2244 indent, "", d->sysctl_flags,
2245 SYSCTL_FLAGS(d->sysctl_flags),
2246 sf(SYSCTL_FLAGS(d->sysctl_flags)),
2247 SYSCTL_TYPE(d->sysctl_flags),
2248 st(SYSCTL_TYPE(d->sysctl_flags)),
2249 d->sysctl_size);
2250 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2251 printf("%*s\t\tsysctl_csize %d\n", indent, "",
2252 d->sysctl_csize);
2253 printf("%*s\t\tsysctl_clen %d\n", indent, "",
2254 d->sysctl_clen);
2255 printf("%*s\t\tsysctl_child %p\n", indent, "",
2256 d->sysctl_child);
2257 }
2258 else
2259 printf("%*s\t\tsysctl_data %p\n", indent, "",
2260 d->sysctl_data);
2261 printf("%*s\t\tsysctl_func %p\n", indent, "",
2262 d->sysctl_func);
2263 printf("%*s\t\tsysctl_parent %p\n", indent, "",
2264 d->sysctl_parent);
2265 printf("%*s\t\tsysctl_ver %d\n", indent, "",
2266 d->sysctl_ver);
2267 }
2268
2269 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2270 indent += 8;
2271 n = d->sysctl_child;
2272 for (i = 0; i < d->sysctl_clen; i++) {
2273 sysctl_dump(&n[i]);
2274 }
2275 indent -= 8;
2276 }
2277
2278 np[0] = '\0';
2279 sp[0] = '\0';
2280 }
2281 #endif /* 0 */
2282
2283 /*
2284 * ********************************************************************
2285 * Deletes an entire n-ary tree. Not recommended unless you know why
2286 * you're doing it. Personally, I don't know why you'd even think
2287 * about it.
2288 * ********************************************************************
2289 */
2290 void
2291 sysctl_free(struct sysctlnode *rnode)
2292 {
2293 struct sysctlnode *node, *pnode;
2294
2295 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
2296 printf("sysctl_free: rnode %p wrong version\n", rnode);
2297 return;
2298 }
2299
2300 if (rnode == NULL)
2301 rnode = &sysctl_root;
2302 pnode = rnode;
2303
2304 node = pnode->sysctl_child;
2305 do {
2306 while (node != NULL && pnode->sysctl_csize > 0) {
2307 while (node <
2308 &pnode->sysctl_child[pnode->sysctl_clen] &&
2309 (SYSCTL_TYPE(node->sysctl_flags) !=
2310 CTLTYPE_NODE ||
2311 node->sysctl_csize == 0)) {
2312 if (SYSCTL_FLAGS(node->sysctl_flags) &
2313 CTLFLAG_OWNDATA) {
2314 if (node->sysctl_data != NULL) {
2315 FREE(node->sysctl_data,
2316 M_SYSCTLDATA);
2317 node->sysctl_data = NULL;
2318 }
2319 }
2320 if (SYSCTL_FLAGS(node->sysctl_flags) &
2321 CTLFLAG_OWNDESC) {
2322 if (node->sysctl_desc != NULL) {
2323 FREE(node->sysctl_desc,
2324 M_SYSCTLDATA);
2325 node->sysctl_desc = NULL;
2326 }
2327 }
2328 node++;
2329 }
2330 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
2331 pnode = node;
2332 node = node->sysctl_child;
2333 }
2334 else
2335 break;
2336 }
2337 if (pnode->sysctl_child != NULL)
2338 FREE(pnode->sysctl_child, M_SYSCTLNODE);
2339 pnode->sysctl_clen = 0;
2340 pnode->sysctl_csize = 0;
2341 pnode->sysctl_child = NULL;
2342 node = pnode;
2343 pnode = node->sysctl_parent;
2344 } while (pnode != NULL && node != rnode);
2345 }
2346
2347 int
2348 sysctl_log_add(struct sysctllog **logp, struct sysctlnode *node)
2349 {
2350 int name[CTL_MAXNAME], namelen, i;
2351 struct sysctlnode *pnode;
2352 struct sysctllog *log;
2353
2354 if (node->sysctl_flags & CTLFLAG_PERMANENT)
2355 return (0);
2356
2357 if (logp == NULL)
2358 return (0);
2359
2360 if (*logp == NULL) {
2361 MALLOC(log, struct sysctllog *, sizeof(struct sysctllog),
2362 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2363 if (log == NULL) {
2364 /* XXX print error message? */
2365 return (-1);
2366 }
2367 MALLOC(log->log_num, int *, 16 * sizeof(int),
2368 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2369 if (log->log_num == NULL) {
2370 /* XXX print error message? */
2371 free(log, M_SYSCTLDATA);
2372 return (-1);
2373 }
2374 memset(log->log_num, 0, 16 * sizeof(int));
2375 log->log_root = NULL;
2376 log->log_size = 16;
2377 log->log_left = 16;
2378 *logp = log;
2379 }
2380 else
2381 log = *logp;
2382
2383 /*
2384 * check that the root is proper. it's okay to record the
2385 * address of the root of a tree. it's the only thing that's
2386 * guaranteed not to shift around as nodes come and go.
2387 */
2388 if (log->log_root == NULL)
2389 log->log_root = sysctl_rootof(node);
2390 else if (log->log_root != sysctl_rootof(node)) {
2391 printf("sysctl: log %p root mismatch (%p)\n",
2392 log->log_root, sysctl_rootof(node));
2393 return (-1);
2394 }
2395
2396 /*
2397 * we will copy out name in reverse order
2398 */
2399 for (pnode = node, namelen = 0;
2400 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
2401 pnode = pnode->sysctl_parent)
2402 name[namelen++] = pnode->sysctl_num;
2403
2404 /*
2405 * do we have space?
2406 */
2407 if (log->log_left < (namelen + 3))
2408 sysctl_log_realloc(log);
2409 if (log->log_left < (namelen + 3))
2410 return (-1);
2411
2412 /*
2413 * stuff name in, then namelen, then node type, and finally,
2414 * the version for non-node nodes.
2415 */
2416 for (i = 0; i < namelen; i++)
2417 log->log_num[--log->log_left] = name[i];
2418 log->log_num[--log->log_left] = namelen;
2419 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
2420 if (log->log_num[log->log_left] != CTLTYPE_NODE)
2421 log->log_num[--log->log_left] = node->sysctl_ver;
2422 else
2423 log->log_num[--log->log_left] = 0;
2424
2425 return (0);
2426 }
2427
2428 void
2429 sysctl_teardown(struct sysctllog **logp)
2430 {
2431 struct sysctlnode node, *rnode;
2432 struct sysctllog *log;
2433 uint namelen;
2434 int *name, t, v, error, ni;
2435 size_t sz;
2436
2437 if (logp == NULL || *logp == NULL)
2438 return;
2439 log = *logp;
2440
2441 error = sysctl_lock(NULL, NULL, 0);
2442 if (error)
2443 return;
2444
2445 memset(&node, 0, sizeof(node));
2446
2447 while (log->log_left < log->log_size) {
2448 KASSERT((log->log_left + 3 < log->log_size) &&
2449 (log->log_left + log->log_num[log->log_left + 2] <=
2450 log->log_size));
2451 v = log->log_num[log->log_left++];
2452 t = log->log_num[log->log_left++];
2453 namelen = log->log_num[log->log_left++];
2454 name = &log->log_num[log->log_left];
2455
2456 node.sysctl_num = name[namelen - 1];
2457 node.sysctl_flags = t;
2458 node.sysctl_ver = v;
2459
2460 rnode = log->log_root;
2461 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
2462 if (error == 0) {
2463 name[namelen - 1] = CTL_DESTROY;
2464 rnode = rnode->sysctl_parent;
2465 sz = 0;
2466 (void)sysctl_destroy(&name[namelen - 1], 1, NULL,
2467 &sz, &node, sizeof(node),
2468 &name[0], NULL, rnode);
2469 }
2470
2471 log->log_left += namelen;
2472 }
2473
2474 KASSERT(log->log_size == log->log_left);
2475 free(log->log_num, M_SYSCTLDATA);
2476 free(log, M_SYSCTLDATA);
2477 *logp = NULL;
2478
2479 sysctl_unlock(NULL);
2480 }
2481
2482 /*
2483 * ********************************************************************
2484 * old_sysctl -- A routine to bridge old-style internal calls to the
2485 * new infrastructure.
2486 * ********************************************************************
2487 */
2488 int
2489 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2490 void *newp, size_t newlen, struct lwp *l)
2491 {
2492 int error;
2493 size_t savelen = *oldlenp;
2494
2495 error = sysctl_lock(l, oldp, savelen);
2496 if (error)
2497 return (error);
2498 error = sysctl_dispatch(name, namelen, oldp, oldlenp,
2499 newp, newlen, name, l, NULL);
2500 sysctl_unlock(l);
2501 if (error == 0 && oldp != NULL && savelen < *oldlenp)
2502 error = ENOMEM;
2503
2504 return (error);
2505 }
2506
2507 /*
2508 * ********************************************************************
2509 * Section 4: Generic helper routines
2510 * ********************************************************************
2511 * "helper" routines that can do more finely grained access control,
2512 * construct structures from disparate information, create the
2513 * appearance of more nodes and sub-trees, etc. for example, if
2514 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
2515 * with a dynamically created list of nodes that represented the
2516 * currently running processes at that instant.
2517 * ********************************************************************
2518 */
2519
2520 /*
2521 * first, a few generic helpers that provide:
2522 *
2523 * sysctl_needfunc() a readonly interface that emits a warning
2524 * sysctl_notavail() returns EOPNOTSUPP (generic error)
2525 * sysctl_null() an empty return buffer with no error
2526 */
2527 int
2528 sysctl_needfunc(SYSCTLFN_ARGS)
2529 {
2530 int error;
2531
2532 printf("!!SYSCTL_NEEDFUNC!!\n");
2533
2534 if (newp != NULL || namelen != 0)
2535 return (EOPNOTSUPP);
2536
2537 error = 0;
2538 if (oldp != NULL)
2539 error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2540 MIN(rnode->sysctl_size, *oldlenp));
2541 *oldlenp = rnode->sysctl_size;
2542
2543 return (error);
2544 }
2545
2546 int
2547 sysctl_notavail(SYSCTLFN_ARGS)
2548 {
2549
2550 if (namelen == 1 && name[0] == CTL_QUERY)
2551 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2552
2553 return (EOPNOTSUPP);
2554 }
2555
2556 int
2557 sysctl_null(SYSCTLFN_ARGS)
2558 {
2559
2560 *oldlenp = 0;
2561
2562 return (0);
2563 }
2564
2565 /*
2566 * ********************************************************************
2567 * Section 5: The machinery that makes it all go
2568 * ********************************************************************
2569 * Memory "manglement" routines. Not much to this, eh?
2570 * ********************************************************************
2571 */
2572 static int
2573 sysctl_alloc(struct sysctlnode *p, int x)
2574 {
2575 int i;
2576 struct sysctlnode *n;
2577
2578 assert(p->sysctl_child == NULL);
2579
2580 if (x == 1)
2581 MALLOC(n, struct sysctlnode *,
2582 sizeof(struct sysctlnode),
2583 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2584 else
2585 MALLOC(n, struct sysctlnode *,
2586 SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2587 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2588 if (n == NULL)
2589 return (ENOMEM);
2590
2591 if (x == 1) {
2592 memset(n, 0, sizeof(struct sysctlnode));
2593 p->sysctl_csize = 1;
2594 }
2595 else {
2596 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2597 p->sysctl_csize = SYSCTL_DEFSIZE;
2598 }
2599 p->sysctl_clen = 0;
2600
2601 for (i = 0; i < p->sysctl_csize; i++)
2602 n[i].sysctl_parent = p;
2603
2604 p->sysctl_child = n;
2605 return (0);
2606 }
2607
2608 static int
2609 sysctl_realloc(struct sysctlnode *p)
2610 {
2611 int i, j;
2612 struct sysctlnode *n;
2613
2614 assert(p->sysctl_csize == p->sysctl_clen);
2615
2616 /*
2617 * how many do we have...how many should we make?
2618 */
2619 i = p->sysctl_clen;
2620 n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
2621 M_WAITOK|M_CANFAIL);
2622 if (n == NULL)
2623 return (ENOMEM);
2624
2625 /*
2626 * move old children over...initialize new children
2627 */
2628 memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
2629 memset(&n[i], 0, i * sizeof(struct sysctlnode));
2630 p->sysctl_csize = 2 * i;
2631
2632 /*
2633 * reattach moved (and new) children to parent; if a moved
2634 * child node has children, reattach the parent pointers of
2635 * grandchildren
2636 */
2637 for (i = 0; i < p->sysctl_csize; i++) {
2638 n[i].sysctl_parent = p;
2639 if (n[i].sysctl_child != NULL) {
2640 for (j = 0; j < n[i].sysctl_csize; j++)
2641 n[i].sysctl_child[j].sysctl_parent = &n[i];
2642 }
2643 }
2644
2645 /*
2646 * get out with the old and in with the new
2647 */
2648 FREE(p->sysctl_child, M_SYSCTLNODE);
2649 p->sysctl_child = n;
2650
2651 return (0);
2652 }
2653
2654 static int
2655 sysctl_log_realloc(struct sysctllog *log)
2656 {
2657 int *n, s, d;
2658
2659 s = log->log_size * 2;
2660 d = log->log_size;
2661
2662 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2663 if (n == NULL)
2664 return (-1);
2665
2666 memset(n, 0, s * sizeof(int));
2667 memcpy(&n[d], log->log_num, d * sizeof(int));
2668 free(log->log_num, M_SYSCTLDATA);
2669 log->log_num = n;
2670 if (d)
2671 log->log_left += d;
2672 else
2673 log->log_left = s;
2674 log->log_size = s;
2675
2676 return (0);
2677 }
2678
2679 /*
2680 * ********************************************************************
2681 * Section 6: Conversion between API versions wrt the sysctlnode
2682 * ********************************************************************
2683 */
2684 static int
2685 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
2686 struct sysctlnode *node)
2687 {
2688 struct sysctlnode0 inode0;
2689 int error, flags;
2690
2691 if (i == NULL) {
2692 memset(node, 0, sizeof(*node));
2693 *vp = SYSCTL_VERS_0;
2694 return (0);
2695 }
2696
2697 if (sz < sizeof(flags))
2698 return (EINVAL);
2699
2700 error = sysctl_copyin(l, i, &flags, sizeof(flags));
2701 if (error)
2702 return (error);
2703
2704 if (sz == sizeof(inode0) &&
2705 SYSCTL_VERS(flags) == SYSCTL_VERS_0) {
2706 error = sysctl_copyin(l, i, &inode0, sizeof(inode0));
2707 if (error)
2708 return (error);
2709
2710 node->sysctl_flags = inode0.sysctl0_flags;
2711 node->sysctl_num = inode0.sysctl0_num;
2712 memcpy(node->sysctl_name, inode0.sysctl0_name, SYSCTL_NAMELEN);
2713 node->sysctl_ver = inode0.sysctl0_ver;
2714 node->sysctl_csize = inode0.sysctl0_csize;
2715 node->sysctl_clen = inode0.sysctl0_clen;
2716 node->sysctl_child = NULL; /* inode.sysctl0_child; */
2717 node->sysctl_data = inode0.sysctl0_data;
2718 node->sysctl_offset = 0;
2719 node->sysctl_alias = inode0.sysctl0_alias;
2720 node->sysctl_idata = inode0.sysctl0_idata;
2721 node->sysctl_qdata = inode0.sysctl0_qdata;
2722 node->sysctl_size = inode0.sysctl0_size;
2723 node->sysctl_func = inode0.sysctl0_func;
2724 node->sysctl_parent = NULL; /* inode.sysctl0_parent; */
2725 node->sysctl_desc = NULL;
2726
2727 node->sysctl_flags &= ~SYSCTL_VERS_MASK;
2728 node->sysctl_flags |= SYSCTL_VERSION;
2729 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
2730 node->sysctl_size == sizeof(inode0))
2731 node->sysctl_size = sizeof(*node);
2732
2733 *vp = SYSCTL_VERS_0;
2734 return (0);
2735 }
2736
2737 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2738 #error sysctl_cvt_in: no support for SYSCTL_VERSION
2739 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2740
2741 if (sz == sizeof(*node) &&
2742 SYSCTL_VERS(flags) == SYSCTL_VERSION) {
2743 error = sysctl_copyin(l, i, node, sizeof(*node));
2744 if (error)
2745 return (error);
2746 *vp = SYSCTL_VERSION;
2747 return (0);
2748 }
2749
2750 return (EINVAL);
2751 }
2752
2753 static int
2754 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
2755 void *ovp, size_t left, size_t *szp)
2756 {
2757 struct sysctlnode0 onode0;
2758 size_t sz = sizeof(*i);
2759 const void *src = i;
2760 int error;
2761
2762 switch (v) {
2763 case SYSCTL_VERS_0:
2764 sz = sizeof(onode0);
2765 src = &onode0;
2766 memset(&onode0, 0, sz);
2767 onode0.sysctl0_flags = i->sysctl_flags;
2768 onode0.sysctl0_num = i->sysctl_num;
2769 onode0.sysctl0_size = i->sysctl_size;
2770 memcpy(onode0.sysctl0_name, i->sysctl_name, SYSCTL_NAMELEN);
2771 onode0.sysctl0_csize = i->sysctl_csize;
2772 onode0.sysctl0_clen = i->sysctl_clen;
2773 onode0.sysctl0_child = NULL; /* i->sysctl_child; */
2774 onode0.sysctl0_alias = i->sysctl_alias;
2775 onode0.sysctl0_data = i->sysctl_data;
2776 onode0.sysctl0_idata = i->sysctl_idata;
2777 onode0.sysctl0_qdata = i->sysctl_qdata;
2778 onode0.sysctl0_func = i->sysctl_func;
2779 onode0.sysctl0_parent = NULL; /* i->syctl_parent; */
2780 onode0.sysctl0_ver = i->sysctl_ver;
2781
2782 onode0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
2783 onode0.sysctl0_flags |= SYSCTL_VERS_0;
2784 if (SYSCTL_TYPE(onode0.sysctl0_flags) == CTLTYPE_NODE &&
2785 onode0.sysctl0_size == sizeof(*i))
2786 onode0.sysctl0_size = sizeof(onode0);
2787
2788 break;
2789
2790 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2791 #error sysctl_cvt_out: no support for SYSCTL_VERSION
2792 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2793
2794 case SYSCTL_VERSION:
2795 /* nothing more to do here */
2796 break;
2797 }
2798
2799 if (ovp != NULL && left >= sz) {
2800 error = sysctl_copyout(l, src, ovp, sz);
2801 if (error)
2802 return (error);
2803 }
2804
2805 if (szp != NULL)
2806 *szp = sz;
2807
2808 return (0);
2809 }
2810