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