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