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