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