kern_sysctl.c revision 1.156 1 /* $NetBSD: kern_sysctl.c,v 1.156 2003/12/29 04:16:25 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Mike Karels at Berkeley Software Design, Inc.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
71 */
72
73 /*
74 * sysctl system call.
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.156 2003/12/29 04:16:25 atatat Exp $");
79
80 #include "opt_defcorename.h"
81 #include "opt_insecure.h"
82 #include "ksyms.h"
83
84 #include <sys/param.h>
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_RWPROTO);
99 static int sysctl_alloc(struct sysctlnode *, int);
100 static int sysctl_realloc(struct sysctlnode *);
101
102 /*
103 * the "root" of the new sysctl tree
104 */
105 static struct sysctlnode sysctl_root = {
106 .sysctl_flags = SYSCTL_ROOT|
107 SYSCTL_READWRITE|
108 CTLTYPE_NODE,
109 .sysctl_num = 0,
110 .sysctl_size = sizeof(struct sysctlnode),
111 .sysctl_name = "(root)",
112 };
113
114 /*
115 * link set of functions that add nodes at boot time (see also
116 * sysctl_buildtree())
117 */
118 __link_set_decl(sysctl_funcs, sysctl_setup_func);
119
120 /*
121 * The `sysctl_lock' is intended to serialize access to the sysctl
122 * tree. Given that it is now (a) dynamic, and (b) most consumers of
123 * sysctl are going to be copying data out, the old `sysctl_memlock'
124 * has been `upgraded' to simply guard the whole tree.
125 *
126 * The two new data here are to keep track of the locked chunk of
127 * memory, if there is one, so that it can be released more easily
128 * from anywhere.
129 */
130 struct lock sysctl_treelock;
131 caddr_t sysctl_memaddr;
132 size_t sysctl_memsize;
133
134 /*
135 * Attributes stored in the kernel.
136 */
137 char hostname[MAXHOSTNAMELEN];
138 int hostnamelen;
139
140 char domainname[MAXHOSTNAMELEN];
141 int domainnamelen;
142
143 long hostid;
144
145 #ifdef INSECURE
146 int securelevel = -1;
147 #else
148 int securelevel = 0;
149 #endif
150
151 #ifndef DEFCORENAME
152 #define DEFCORENAME "%n.core"
153 #endif
154 char defcorename[MAXPATHLEN] = DEFCORENAME;
155
156 /*
157 * ********************************************************************
158 * Section 0: Some simple glue
159 * ********************************************************************
160 * By wrapping copyin(), copyout(), and copyinstr() like this, we can
161 * stop caring about who's calling us and simplify some code a bunch.
162 * ********************************************************************
163 */
164 static inline int
165 sysctl_copyin(const struct lwp *l, const void *uaddr, void *kaddr, size_t len)
166 {
167
168 if (l != NULL)
169 return (copyin(uaddr, kaddr, len));
170 else
171 return (kcopy(uaddr, kaddr, len));
172 }
173
174 static inline int
175 sysctl_copyout(const struct lwp *l, const void *kaddr, void *uaddr, size_t len)
176 {
177
178 if (l != NULL)
179 return (copyout(kaddr, uaddr, len));
180 else
181 return (kcopy(kaddr, uaddr, len));
182 }
183
184 static inline int
185 sysctl_copyinstr(const struct lwp *l, const void *uaddr, void *kaddr,
186 size_t len, size_t *done)
187 {
188
189 if (l != NULL)
190 return (copyinstr(uaddr, kaddr, len, done));
191 else
192 return (copystr(uaddr, kaddr, len, done));
193 }
194
195 /*
196 * ********************************************************************
197 * Initialize sysctl subsystem.
198 * ********************************************************************
199 */
200 void
201 sysctl_init(void)
202 {
203 sysctl_setup_func **sysctl_setup, f;
204
205 lockinit(&sysctl_treelock, PRIBIO|PCATCH, "sysctl", 0, 0);
206
207 /*
208 * dynamic mib numbers start here
209 */
210 sysctl_root.sysctl_num = CREATE_BASE;
211
212 __link_set_foreach(sysctl_setup, sysctl_funcs) {
213 /*
214 * XXX - why do i have to coerce the pointers like this?
215 */
216 f = (void*)*sysctl_setup;
217 (*f)();
218 }
219
220 /*
221 * setting this means no more permanent nodes can be added,
222 * trees that claim to be readonly at the root now are, and if
223 * the main tree is readonly, *everything* is.
224 */
225 sysctl_root.sysctl_flags |= SYSCTL_PERMANENT;
226
227 }
228
229 /*
230 * ********************************************************************
231 * The main native sysctl system call itself.
232 * ********************************************************************
233 */
234 int
235 sys___sysctl(struct lwp *l, void *v, register_t *retval)
236 {
237 struct sys___sysctl_args /* {
238 syscallarg(int *) name;
239 syscallarg(u_int) namelen;
240 syscallarg(void *) old;
241 syscallarg(size_t *) oldlenp;
242 syscallarg(void *) new;
243 syscallarg(size_t) newlen;
244 } */ *uap = v;
245 int error, nerror, name[CTL_MAXNAME];
246 size_t oldlen, savelen, *oldlenp;
247
248 /*
249 * get oldlen
250 */
251 oldlen = 0;
252 oldlenp = SCARG(uap, oldlenp);
253 if (oldlenp != NULL) {
254 error = copyin(oldlenp, &oldlen, sizeof(oldlen));
255 if (error)
256 return (error);
257 }
258 savelen = oldlen;
259
260 /*
261 * top-level sysctl names may or may not be non-terminal, but
262 * we don't care
263 */
264 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
265 return (EINVAL);
266 error = copyin(SCARG(uap, name), &name,
267 SCARG(uap, namelen) * sizeof(int));
268 if (error)
269 return (error);
270
271 /*
272 * wire old so that copyout() is less likely to fail?
273 */
274 error = sysctl_lock(l, SCARG(uap, old), savelen);
275 if (error)
276 return (error);
277
278 /*
279 * do sysctl work (NULL means main built-in default tree)
280 */
281 error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
282 SCARG(uap, old), &oldlen,
283 SCARG(uap, new), SCARG(uap, newlen),
284 &name[0], l, NULL);
285
286 /*
287 * release the sysctl lock
288 */
289 sysctl_unlock(l);
290
291 /*
292 * set caller's oldlen to new value even in the face of an
293 * error (if this gets an error and they didn't have one, they
294 * get this one)
295 */
296 if (oldlenp) {
297 nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
298 if (error == 0)
299 error = nerror;
300 }
301
302 /*
303 * if the only problem is that we weren't given enough space,
304 * that's an ENOMEM error
305 */
306 if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
307 error = ENOMEM;
308
309 return (error);
310 }
311
312 /*
313 * ********************************************************************
314 * Section 1: How the tree is used
315 * ********************************************************************
316 * Implementations of sysctl for emulations should typically need only
317 * these three functions in this order: lock the tree, dispatch
318 * request into it, unlock the tree.
319 * ********************************************************************
320 */
321 int
322 sysctl_lock(struct lwp *l, void *oldp, size_t savelen)
323 {
324 int error = 0;
325
326 error = lockmgr(&sysctl_treelock, LK_EXCLUSIVE, NULL);
327 if (error)
328 return (error);
329
330 if (l != NULL && oldp != NULL && savelen) {
331 error = uvm_vslock(l->l_proc, oldp, savelen, VM_PROT_WRITE);
332 if (error) {
333 (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
334 return (error);
335 }
336 sysctl_memaddr = oldp;
337 sysctl_memsize = savelen;
338 }
339
340 return (0);
341 }
342
343 /*
344 * ********************************************************************
345 * the main sysctl dispatch routine. scans the given tree and picks a
346 * function to call based on what it finds.
347 * ********************************************************************
348 */
349 int
350 sysctl_dispatch(SYSCTLFN_RWARGS)
351 {
352 int error;
353 sysctlfn fn;
354 int ni;
355
356 fn = NULL;
357 error = sysctl_locate(l, name, namelen, &rnode, &ni);
358
359 /*
360 * the node we ended up at has a function, so call it. it can
361 * hand off to query or create if it wants to.
362 */
363 if (rnode->sysctl_func != NULL)
364 fn = rnode->sysctl_func;
365
366 /*
367 * we found the node they were looking for, so do a lookup.
368 */
369 else if (error == 0)
370 fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
371
372 /*
373 * prospective parent node found, but the terminal node was
374 * not. generic operations associate with the parent.
375 */
376 else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
377 switch (name[ni]) {
378 case CTL_QUERY:
379 fn = sysctl_query;
380 break;
381 case CTL_CREATE:
382 #if NKSYMS > 0
383 case CTL_CREATESYM:
384 #endif /* NKSYMS > 0 */
385 fn = (sysctlfn)sysctl_create; /* we own the rnode */
386 break;
387 case CTL_DESTROY:
388 fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
389 break;
390 case CTL_MMAP:
391 fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
392 break;
393 default:
394 error = EOPNOTSUPP;
395 break;
396 }
397 }
398
399 /*
400 * after all of that, maybe we found someone who knows how to
401 * get us what we want?
402 */
403 if (fn != NULL)
404 error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
405 newp, newlen, name, l, rnode);
406
407 else if (error == 0)
408 error = EOPNOTSUPP;
409
410 return (error);
411 }
412
413 /*
414 * ********************************************************************
415 * Releases the tree lock. Note that if uvm_vslock() was called when
416 * the lock was taken, we release that memory now. By keeping track
417 * of where and how much by ourselves, the lock can be released much
418 * more easily from anywhere.
419 * ********************************************************************
420 */
421 void
422 sysctl_unlock(struct lwp *l)
423 {
424
425 if (l != NULL && sysctl_memsize != 0) {
426 uvm_vsunlock(l->l_proc, sysctl_memaddr, sysctl_memsize);
427 sysctl_memsize = 0;
428 }
429
430 (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
431 }
432
433 /*
434 * ********************************************************************
435 * Section 2: The main tree interfaces
436 * ********************************************************************
437 * This is how sysctl_dispatch() does its work, and you can too, by
438 * calling these routines from helpers (though typically only
439 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these
440 * are called.
441 * ********************************************************************
442 */
443
444 /*
445 * sysctl_locate -- Finds the node matching the given mib under the
446 * given tree (via rv). If no tree is given, we fall back to the
447 * native tree. The current process (via l) is used for access
448 * control on the tree (some nodes may be traversable only by root) and
449 * on return, nip will show how many numbers in the mib were consumed.
450 */
451 int
452 sysctl_locate(struct lwp *l, const int *name, u_int namelen,
453 struct sysctlnode **rv, int *nip)
454 {
455 struct sysctlnode *node, *pnode;
456 int tn, si, ni, error, alias;
457
458 /*
459 * basic checks and setup
460 */
461 if (*rv == NULL)
462 *rv = &sysctl_root;
463 if (nip)
464 *nip = 0;
465 if (namelen < 0)
466 return (EINVAL);
467 if (namelen == 0)
468 return (0);
469
470 /*
471 * search starts from "root"
472 */
473 pnode = *rv;
474 node = pnode->sysctl_child;
475 error = 0;
476
477 /*
478 * scan for node to which new node should be attached
479 */
480 for (ni = 0; ni < namelen; ni++) {
481 /*
482 * walked off bottom of tree
483 */
484 if (node == NULL) {
485 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
486 error = ENOENT;
487 else
488 error = ENOTDIR;
489 break;
490 }
491 /*
492 * can anyone traverse this node or only root?
493 */
494 if (l != NULL && (pnode->sysctl_flags & SYSCTL_PRIVATE) &&
495 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
496 != 0)
497 return (error);
498 /*
499 * find a child node with the right number
500 */
501 tn = name[ni];
502 alias = 0;
503 for (si = 0; si < pnode->sysctl_clen; si++) {
504 if (node[si].sysctl_num == tn ||
505 (tn >= 0 &&
506 node[si].sysctl_flags & SYSCTL_ANYNUMBER)) {
507 if (node[si].sysctl_flags & SYSCTL_ALIAS) {
508 if (alias++ == 4)
509 si = pnode->sysctl_clen - 1;
510 else {
511 tn = node[si].sysctl_alias;
512 si = -1;
513 }
514 }
515 else
516 break;
517 }
518 }
519 /*
520 * if we ran off the end, it obviously doesn't exist
521 */
522 if (si == pnode->sysctl_clen) {
523 error = ENOENT;
524 break;
525 }
526 /*
527 * so far so good, move on down the line
528 */
529 pnode = &node[si];
530 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
531 node = node[si].sysctl_child;
532 else
533 node = NULL;
534 }
535
536 *rv = pnode;
537 if (nip)
538 *nip = ni;
539
540 return (error);
541 }
542
543 /*
544 * sysctl_query -- The auto-discovery engine. Copies out the
545 * descriptions on nodes under the given node and handles overlay
546 * trees.
547 */
548 int
549 sysctl_query(SYSCTLFN_ARGS)
550 {
551 int error, ni, elim;
552 size_t out, left, t;
553 struct sysctlnode *enode, *onode;
554
555 if (newp != NULL)
556 return (EPERM);
557 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
558 return (ENOTDIR);
559 if (namelen != 1 || name[0] != CTL_QUERY)
560 return (EINVAL);
561
562 error = 0;
563 out = 0;
564 left = *oldlenp;
565 elim = 0;
566 enode = NULL;
567
568 /*
569 * process has overlay tree
570 */
571 if (l && l->l_proc->p_emul->e_sysctlovly) {
572 enode = (void*)l->l_proc->p_emul->e_sysctlovly;
573 elim = (name - oname);
574 error = sysctl_locate(l, oname, elim, &enode, NULL);
575 if (error == 0) {
576 /* ah, found parent in overlay */
577 elim = enode->sysctl_clen;
578 enode = enode->sysctl_child;
579 }
580 else {
581 error = 0;
582 elim = 0;
583 enode = NULL;
584 }
585 }
586
587 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
588 t = MIN(left, sizeof(struct sysctlnode));
589 onode = &rnode->sysctl_child[ni];
590 if (enode && enode->sysctl_num == onode->sysctl_num) {
591 if (SYSCTL_TYPE(enode->sysctl_flags) !=
592 CTLTYPE_NODE)
593 onode = enode;
594 if (--elim > 0)
595 enode++;
596 else
597 enode = NULL;
598 }
599 if (oldp != NULL && t > 0)
600 error = sysctl_copyout(l, onode, (char*)oldp + out, t);
601 if (error)
602 return (error);
603 out += sizeof(struct sysctlnode);
604 left -= t;
605 }
606
607 /*
608 * overlay trees *MUST* be entirely consumed
609 */
610 KASSERT(enode == NULL);
611
612 *oldlenp = out;
613
614 return (error);
615 }
616
617 #ifdef SYSCTL_DEBUG_CREATE
618 #undef sysctl_create
619 #endif /* SYSCTL_DEBUG_CREATE */
620
621 /*
622 * sysctl_create -- Adds a node (the description of which is taken
623 * from newp) to the tree, returning a copy of it in the space pointed
624 * to by oldp. In the event that the requested slot is already taken
625 * (either by name or by number), the offending node is returned
626 * instead. Yes, this is complex, but we want to make sure everything
627 * is proper.
628 */
629 int
630 sysctl_create(SYSCTLFN_RWARGS)
631 {
632 struct sysctlnode nnode, *node, *pnode;
633 int error, ni, at, nm, type, sz, flags, rw, anum;
634 void *own;
635
636 error = 0;
637 own = NULL;
638 anum = -1;
639
640 if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
641 #if NKSYMS > 0
642 && name[namelen - 1] != CTL_CREATESYM
643 #endif /* NKSYMS > 0 */
644 ))
645 return (EINVAL);
646
647 /*
648 * processes can only add nodes at securelevel 0, must be
649 * root, and can't add nodes to a parent that's not writeable
650 */
651 if (l != NULL) {
652 #ifndef SYSCTL_DISALLOW_CREATE
653 if (securelevel > 0)
654 return (EPERM);
655 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
656 if (error)
657 return (error);
658 if (!(rnode->sysctl_flags & SYSCTL_READWRITE))
659 #endif /* SYSCTL_DISALLOW_CREATE */
660 return (EPERM);
661 }
662
663 /*
664 * nothing can add a node if:
665 * we've finished initial set up and
666 * the tree itself is not writeable or
667 * the entire sysctl system is not writeable
668 */
669 if ((sysctl_root.sysctl_flags & SYSCTL_PERMANENT) &&
670 (!(sysctl_rootof(rnode)->sysctl_flags & SYSCTL_READWRITE) ||
671 !(sysctl_root.sysctl_flags & SYSCTL_READWRITE)))
672 return (EPERM);
673
674 /*
675 * it must be a "node", not a "int" or something
676 */
677 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
678 return (ENOTDIR);
679 pnode = rnode;
680
681 if (newp == NULL || newlen != sizeof(struct sysctlnode))
682 return (EINVAL);
683 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
684 if (error)
685 return (error);
686
687 /*
688 * nodes passed in don't *have* parents
689 */
690 if (nnode.sysctl_parent != NULL)
691 return (EINVAL);
692
693 /*
694 * if we are indeed adding it, it should be a "good" name and
695 * number
696 */
697 nm = nnode.sysctl_num;
698 #if NKSYMS > 0
699 if (nm == CTL_CREATESYM)
700 nm = CTL_CREATE;
701 #endif /* NKSYMS > 0 */
702 if (nm < 0 && nm != CTL_CREATE)
703 return (EINVAL);
704 sz = 0;
705
706 /*
707 * the name can't start with a digit
708 */
709 if (nnode.sysctl_name[sz] >= '0' &&
710 nnode.sysctl_name[sz] <= '9')
711 return (EINVAL);
712
713 /*
714 * the name must be only alphanumerics or - or _, longer than
715 * 0 bytes and less that SYSCTL_NAMELEN
716 */
717 while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
718 if ((nnode.sysctl_name[sz] >= '0' &&
719 nnode.sysctl_name[sz] <= '9') ||
720 (nnode.sysctl_name[sz] >= 'A' &&
721 nnode.sysctl_name[sz] <= 'Z') ||
722 (nnode.sysctl_name[sz] >= 'a' &&
723 nnode.sysctl_name[sz] <= 'z') ||
724 nnode.sysctl_name[sz] == '-' ||
725 nnode.sysctl_name[sz] == '_')
726 sz++;
727 else
728 return (EINVAL);
729 }
730 if (sz == 0 || sz == SYSCTL_NAMELEN)
731 return (EINVAL);
732
733 /*
734 * various checks revolve around size vs type, etc
735 */
736 type = SYSCTL_TYPE(nnode.sysctl_flags);
737 flags = SYSCTL_FLAGS(nnode.sysctl_flags);
738 rw = (flags & SYSCTL_READWRITE) ? B_WRITE : B_READ;
739 sz = nnode.sysctl_size;
740
741 /*
742 * find out if there's a collision, and if so, let the caller
743 * know what they collided with
744 */
745 node = pnode->sysctl_child;
746 if (((flags & SYSCTL_ANYNUMBER) && node) ||
747 (node && node->sysctl_flags & SYSCTL_ANYNUMBER))
748 return (EINVAL);
749 for (ni = at = 0; ni < pnode->sysctl_clen; ni++) {
750 if (nm == node[ni].sysctl_num ||
751 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
752 if (oldp != NULL) {
753 /*
754 * ignore error here, since we
755 * are already fixed on EEXIST
756 */
757 (void)sysctl_copyout(l, &node[ni], oldp,
758 MIN(*oldlenp, sizeof(struct sysctlnode)));
759 }
760 *oldlenp = sizeof(struct sysctlnode);
761 return (EEXIST);
762 }
763 if (nm > node[ni].sysctl_num)
764 at++;
765 }
766
767 /*
768 * use sysctl_ver to add to the tree iff it hasn't changed
769 */
770 if (nnode.sysctl_ver != 0) {
771 /*
772 * a specified value must match either the parent
773 * node's version or the root node's version
774 */
775 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
776 nnode.sysctl_ver != rnode->sysctl_ver) {
777 return (EINVAL);
778 }
779 }
780
781 /*
782 * only the kernel can assign functions to entries
783 */
784 if (l != NULL && nnode.sysctl_func != NULL)
785 return (EPERM);
786
787 /*
788 * only the kernel can create permanent entries, and only then
789 * before the kernel is finished setting itself up
790 */
791 if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
792 return (EPERM);
793 if ((flags & SYSCTL_PERMANENT) &
794 (sysctl_root.sysctl_flags & SYSCTL_PERMANENT))
795 return (EPERM);
796 if ((flags & (SYSCTL_OWNDATA | SYSCTL_IMMEDIATE)) ==
797 (SYSCTL_OWNDATA | SYSCTL_IMMEDIATE))
798 return (EINVAL);
799 if ((flags & SYSCTL_IMMEDIATE) &&
800 type != CTLTYPE_INT && type != CTLTYPE_QUAD)
801 return (EINVAL);
802
803 /*
804 * check size, or set it if unset and we can figure it out.
805 * kernel created nodes are allowed to have a function instead
806 * of a size (or a data pointer).
807 */
808 switch (type) {
809 case CTLTYPE_NODE:
810 /*
811 * only *i* can assert the size of a node
812 */
813 if (flags & SYSCTL_ALIAS) {
814 anum = nnode.sysctl_alias;
815 if (anum < 0)
816 return (EINVAL);
817 nnode.sysctl_alias = 0;
818 }
819 if (sz != 0 || nnode.sysctl_data != NULL)
820 return (EINVAL);
821 if (nnode.sysctl_csize != 0 ||
822 nnode.sysctl_clen != 0 ||
823 nnode.sysctl_child != 0)
824 return (EINVAL);
825 if (flags & SYSCTL_OWNDATA)
826 return (EINVAL);
827 sz = sizeof(struct sysctlnode);
828 break;
829 case CTLTYPE_INT:
830 /*
831 * since an int is an int, if the size is not given or
832 * is wrong, we can "int-uit" it.
833 */
834 if (sz != 0 && sz != sizeof(int))
835 return (EINVAL);
836 sz = sizeof(int);
837 break;
838 case CTLTYPE_STRING:
839 /*
840 * strings are a little more tricky
841 */
842 if (sz == 0) {
843 if (l == NULL) {
844 if (nnode.sysctl_func == NULL) {
845 if (nnode.sysctl_data == NULL)
846 return (EINVAL);
847 else
848 sz = strlen(nnode.sysctl_data) +
849 1;
850 }
851 }
852 else if (nnode.sysctl_data == NULL &&
853 flags & SYSCTL_OWNDATA) {
854 return (EINVAL);
855 }
856 else {
857 char v[PAGE_SIZE], *e;
858 size_t s;
859
860 /*
861 * we want a rough idea of what the
862 * size is now
863 */
864 e = nnode.sysctl_data;
865 do {
866 error = copystr(e, &v[0], sizeof(v),
867 &s);
868 if (error) {
869 if (error != ENAMETOOLONG)
870 return (error);
871 e += PAGE_SIZE;
872 if ((e - 32 * PAGE_SIZE) >
873 (char*)nnode.sysctl_data)
874 return (ERANGE);
875 }
876 } while (error != 0);
877 sz = s + (e - (char*)nnode.sysctl_data);
878 }
879 }
880 break;
881 case CTLTYPE_QUAD:
882 if (sz != 0 && sz != sizeof(u_quad_t))
883 return (EINVAL);
884 sz = sizeof(u_quad_t);
885 break;
886 case CTLTYPE_STRUCT:
887 if (sz == 0) {
888 if (l != NULL || nnode.sysctl_func == NULL)
889 return (EINVAL);
890 if (flags & SYSCTL_OWNDATA)
891 return (EINVAL);
892 }
893 break;
894 default:
895 return (EINVAL);
896 }
897
898 /*
899 * at this point, if sz is zero, we *must* have a
900 * function to go with it and we can't own it.
901 */
902
903 /*
904 * l ptr own
905 * 0 0 0 -> EINVAL (if no func)
906 * 0 0 1 -> own
907 * 0 1 0 -> kptr
908 * 0 1 1 -> kptr
909 * 1 0 0 -> EINVAL
910 * 1 0 1 -> own
911 * 1 1 0 -> kptr, no own (fault on lookup)
912 * 1 1 1 -> uptr, own
913 */
914 if (type != CTLTYPE_NODE) {
915 if (sz != 0) {
916 if (flags & SYSCTL_OWNDATA) {
917 own = malloc(sz, M_SYSCTLDATA,
918 M_WAITOK|M_CANFAIL);
919 if (nnode.sysctl_data == NULL)
920 memset(own, 0, sz);
921 else {
922 error = sysctl_copyin(l,
923 nnode.sysctl_data, own, sz);
924 if (error != 0) {
925 FREE(own, M_SYSCTLDATA);
926 return (error);
927 }
928 }
929 }
930 else if ((nnode.sysctl_data != NULL) &&
931 !(flags & SYSCTL_IMMEDIATE)) {
932 #if NKSYMS > 0
933 if (name[namelen - 1] == CTL_CREATESYM) {
934 char symname[128]; /* XXX enough? */
935 u_long symaddr;
936 size_t symlen;
937
938 error = sysctl_copyinstr(l,
939 nnode.sysctl_data, symname,
940 sizeof(symname), &symlen);
941 if (error)
942 return (error);
943 error = ksyms_getval_from_kernel(NULL,
944 symname, &symaddr, KSYMS_EXTERN);
945 if (error)
946 return (error); /* EINVAL? */
947 nnode.sysctl_data = (void*)symaddr;
948 }
949 #endif /* NKSYMS > 0 */
950 /*
951 * Ideally, we'd like to verify here
952 * that this address is acceptable,
953 * but...
954 *
955 * - it might be valid now, only to
956 * become invalid later
957 *
958 * - it might be invalid only for the
959 * moment and valid later
960 *
961 * - or something else.
962 *
963 * Since we can't get a good answer,
964 * we'll just accept the address as
965 * given, and fault on individual
966 * lookups.
967 */
968 }
969 }
970 else if (nnode.sysctl_func == NULL)
971 return (EINVAL);
972 }
973
974 /*
975 * a process can't assign a function to a node, and the kernel
976 * can't create a node that has no function or data.
977 * (XXX somewhat redundant check)
978 */
979 if (l != NULL || nnode.sysctl_func == NULL) {
980 if (type != CTLTYPE_NODE &&
981 nnode.sysctl_data == NULL &&
982 !(flags & SYSCTL_IMMEDIATE) &&
983 own == NULL)
984 return (EINVAL);
985 }
986
987 #ifdef SYSCTL_DISALLOW_KWRITE
988 /*
989 * a process can't create a writable node unless it refers to
990 * new data.
991 */
992 if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
993 (flags & SYSCTL_READWRITE) != SYSCTL_READONLY &&
994 !(flags & SYSCTL_IMMEDIATE))
995 return (EPERM);
996 #endif /* SYSCTL_DISALLOW_KWRITE */
997
998 /*
999 * make sure there's somewhere to put the new stuff.
1000 */
1001 if (pnode->sysctl_child == NULL) {
1002 if (flags & SYSCTL_ANYNUMBER)
1003 error = sysctl_alloc(pnode, 1);
1004 else
1005 error = sysctl_alloc(pnode, 0);
1006 if (error)
1007 return (error);
1008 }
1009 node = pnode->sysctl_child;
1010
1011 /*
1012 * no collisions, so pick a good dynamic number if we need to.
1013 */
1014 if (nm == CTL_CREATE) {
1015 nm = ++sysctl_root.sysctl_num;
1016 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
1017 if (nm == node[ni].sysctl_num) {
1018 nm++;
1019 ni = -1;
1020 }
1021 else if (nm > node[ni].sysctl_num)
1022 at = ni + 1;
1023 }
1024 }
1025
1026 /*
1027 * oops...ran out of space
1028 */
1029 if (pnode->sysctl_clen == pnode->sysctl_csize) {
1030 error = sysctl_realloc(pnode);
1031 if (error)
1032 return (error);
1033 node = pnode->sysctl_child;
1034 }
1035
1036 /*
1037 * insert new node data
1038 */
1039 if (at < pnode->sysctl_clen) {
1040 int t;
1041
1042 /*
1043 * move the nodes that should come after the new one
1044 */
1045 memmove(&node[at + 1], &node[at],
1046 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
1047 memset(&node[at], 0, sizeof(struct sysctlnode));
1048 node[at].sysctl_parent = pnode;
1049 /*
1050 * and...reparent any children of any moved nodes
1051 */
1052 for (ni = at; ni <= pnode->sysctl_clen; ni++)
1053 if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
1054 for (t = 0; t < node[ni].sysctl_clen; t++)
1055 node[ni].sysctl_child[t].sysctl_parent =
1056 &node[ni];
1057 }
1058 node = &node[at];
1059 pnode->sysctl_clen++;
1060
1061 strlcpy(node->sysctl_name, nnode.sysctl_name,
1062 sizeof(node->sysctl_name));
1063 node->sysctl_num = nm;
1064 node->sysctl_size = sz;
1065 node->sysctl_flags = type|flags;
1066 node->sysctl_csize = 0;
1067 node->sysctl_clen = 0;
1068 if (own) {
1069 node->sysctl_data = own;
1070 node->sysctl_flags |= SYSCTL_OWNDATA;
1071 }
1072 else if (flags & SYSCTL_ALIAS) {
1073 node->sysctl_alias = anum;
1074 }
1075 else if (flags & SYSCTL_IMMEDIATE) {
1076 switch (type) {
1077 case CTLTYPE_INT:
1078 node->sysctl_idata = nnode.sysctl_idata;
1079 break;
1080 case CTLTYPE_QUAD:
1081 node->sysctl_qdata = nnode.sysctl_qdata;
1082 break;
1083 }
1084 }
1085 else {
1086 node->sysctl_data = nnode.sysctl_data;
1087 node->sysctl_flags &= ~SYSCTL_OWNDATA;
1088 }
1089 node->sysctl_func = nnode.sysctl_func;
1090 node->sysctl_child = NULL;
1091 /* node->sysctl_parent should already be done */
1092
1093 /*
1094 * update "version" on path to "root"
1095 */
1096 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1097 ;
1098 pnode = node;
1099 for (nm = rnode->sysctl_ver + 1; pnode != NULL;
1100 pnode = pnode->sysctl_parent)
1101 pnode->sysctl_ver = nm;
1102
1103 if (oldp != NULL)
1104 error = sysctl_copyout(l, node, oldp,
1105 MIN(*oldlenp, sizeof(struct sysctlnode)));
1106 *oldlenp = sizeof(struct sysctlnode);
1107
1108 return (error);
1109 }
1110
1111 /*
1112 * ********************************************************************
1113 * A wrapper around sysctl_create() that prints the thing we're trying
1114 * to add.
1115 * ********************************************************************
1116 */
1117 #ifdef SYSCTL_DEBUG_CREATE
1118 int _sysctl_create(SYSCTLFN_RWPROTO);
1119 int
1120 _sysctl_create(SYSCTLFN_RWARGS)
1121 {
1122 const struct sysctlnode *node;
1123 int k, rc, ni, nl = namelen + (name - oname);
1124
1125 node = newp;
1126
1127 printf("namelen %d (", nl);
1128 for (ni = 0; ni < nl - 1; ni++)
1129 printf(" %d", oname[ni]);
1130 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1131 k = node->sysctl_num,
1132 node->sysctl_name,
1133 node->sysctl_flags,
1134 SYSCTL_FLAGS(node->sysctl_flags),
1135 SYSCTL_TYPE(node->sysctl_flags),
1136 node->sysctl_size);
1137
1138 node = rnode;
1139 rc = sysctl_create(SYSCTLFN_CALL(rnode));
1140
1141 printf("sysctl_create(");
1142 for (ni = 0; ni < nl - 1; ni++)
1143 printf(" %d", oname[ni]);
1144 printf(" %d ) returned %d\n", k, rc);
1145
1146 return (rc);
1147 }
1148 #define sysctl_create _sysctl_create
1149 #endif /* SYSCTL_DEBUG_CREATE */
1150
1151 /*
1152 * sysctl_destroy -- Removes a node (as described by newp) from the
1153 * given tree, returning (if successful) a copy of the dead node in
1154 * oldp. Since we're removing stuff, there's not much to check.
1155 */
1156 int
1157 sysctl_destroy(SYSCTLFN_RWARGS)
1158 {
1159 struct sysctlnode *node, *pnode, onode, nnode;
1160 int ni, error;
1161
1162 error = 0;
1163
1164 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
1165 return (EINVAL);
1166
1167 /*
1168 * processes can only destroy nodes at securelevel 0, must be
1169 * root, and can't remove nodes from a parent that's not
1170 * writeable
1171 */
1172 if (l != NULL) {
1173 #ifndef SYSCTL_DISALLOW_CREATE
1174 if (securelevel > 0)
1175 return (EPERM);
1176 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
1177 if (error)
1178 return (error);
1179 if (!(rnode->sysctl_flags & SYSCTL_READWRITE))
1180 #endif /* SYSCTL_DISALLOW_CREATE */
1181 return (EPERM);
1182 }
1183
1184 /*
1185 * nothing can remove a node if:
1186 * the node is permanent (checked later) or
1187 * the tree itself is not writeable or
1188 * the entire sysctl system is not writeable
1189 */
1190 if (!(sysctl_rootof(rnode)->sysctl_flags & SYSCTL_READWRITE) ||
1191 !(sysctl_root.sysctl_flags & SYSCTL_READWRITE))
1192 return (EPERM);
1193
1194 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1195 return (EINVAL);
1196 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
1197 if (error)
1198 return (error);
1199 memset(&onode, 0, sizeof(struct sysctlnode));
1200
1201 node = rnode->sysctl_child;
1202 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
1203 if (nnode.sysctl_num == node[ni].sysctl_num) {
1204 /*
1205 * if name specified, must match
1206 */
1207 if (nnode.sysctl_name[0] != '\0' &&
1208 strcmp(nnode.sysctl_name, node[ni].sysctl_name))
1209 continue;
1210 /*
1211 * if version specified, must match
1212 */
1213 if (nnode.sysctl_ver != 0 &&
1214 nnode.sysctl_ver != node[ni].sysctl_ver)
1215 continue;
1216 /*
1217 * this must be the one
1218 */
1219 break;
1220 }
1221 }
1222 if (ni == rnode->sysctl_clen)
1223 return (ENOENT);
1224 node = &node[ni];
1225 pnode = node->sysctl_parent;
1226
1227 /*
1228 * if the kernel says permanent, it is, so there. nyah.
1229 */
1230 if (SYSCTL_FLAGS(node->sysctl_flags) & SYSCTL_PERMANENT)
1231 return (EPERM);
1232
1233 /*
1234 * can't delete non-empty nodes
1235 */
1236 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
1237 node->sysctl_clen != 0)
1238 return (ENOTEMPTY);
1239
1240 /*
1241 * if the node "owns" data, release it now
1242 */
1243 if (node->sysctl_flags & SYSCTL_OWNDATA) {
1244 if (node->sysctl_data != NULL)
1245 FREE(node->sysctl_data, M_SYSCTLDATA);
1246 node->sysctl_data = NULL;
1247 }
1248
1249 /*
1250 * if the node to be removed is not the last one on the list,
1251 * move the remaining nodes up, and reparent any grandchildren
1252 */
1253 onode = *node;
1254 if (ni < pnode->sysctl_clen - 1) {
1255 int t;
1256
1257 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
1258 (pnode->sysctl_clen - ni - 1) *
1259 sizeof(struct sysctlnode));
1260 for (; ni < pnode->sysctl_clen - 1; ni++)
1261 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
1262 CTLTYPE_NODE)
1263 for (t = 0; t < pnode->sysctl_child[ni].sysctl_clen;
1264 t++)
1265 pnode->sysctl_child[ni].sysctl_child[t].
1266 sysctl_parent =
1267 &pnode->sysctl_child[ni];
1268 ni = pnode->sysctl_clen - 1;
1269 node = &pnode->sysctl_child[ni];
1270 }
1271
1272 /*
1273 * reset the space we just vacated
1274 */
1275 memset(node, 0, sizeof(struct sysctlnode));
1276 node->sysctl_parent = pnode;
1277 pnode->sysctl_clen--;
1278
1279 /*
1280 * if this parent just lost its last child, nuke the creche
1281 */
1282 if (pnode->sysctl_clen == 0) {
1283 FREE(pnode->sysctl_child, M_SYSCTLNODE);
1284 pnode->sysctl_csize = 0;
1285 pnode->sysctl_child = NULL;
1286 }
1287
1288 /*
1289 * update "version" on path to "root"
1290 */
1291 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1292 ;
1293 for (ni = rnode->sysctl_ver + 1; pnode != NULL;
1294 pnode = pnode->sysctl_parent)
1295 pnode->sysctl_ver = ni;
1296
1297 if (oldp != NULL)
1298 error = sysctl_copyout(l, &onode, oldp,
1299 MIN(*oldlenp, sizeof(struct sysctlnode)));
1300 *oldlenp = sizeof(struct sysctlnode);
1301
1302 return (error);
1303 }
1304
1305 /*
1306 * sysctl_lookup -- Handles copyin/copyout of new and old values.
1307 * Partial reads are globally allowed. Only root can write to things
1308 * unless the node says otherwise.
1309 */
1310 int
1311 sysctl_lookup(SYSCTLFN_RWARGS)
1312 {
1313 struct proc *p = l->l_proc;
1314 int error, rw;
1315 size_t sz, len;
1316 void *d;
1317
1318 error = 0;
1319
1320 /*
1321 * you can't "look up" a node. you can "query" it, but you
1322 * can't "look it up".
1323 */
1324 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
1325 return (EINVAL);
1326
1327 /*
1328 * some nodes are private, so only root can look into them.
1329 */
1330 if ((rnode->sysctl_flags & SYSCTL_PRIVATE) &&
1331 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
1332 return (error);
1333
1334 /*
1335 * if a node wants to be writable according to different rules
1336 * other than "only root can write to stuff unless a flag is
1337 * set", then it needs its own function which should have been
1338 * called and not us.
1339 */
1340 if (l != NULL && newp != NULL &&
1341 !(rnode->sysctl_flags & SYSCTL_ANYWRITE) &&
1342 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1343 return (error);
1344
1345 /*
1346 * is this node supposedly writable?
1347 */
1348 rw = 0;
1349 switch (rnode->sysctl_flags & SYSCTL_READWRITE) {
1350 case SYSCTL_READONLY1:
1351 rw = (securelevel < 1) ? 1 : 0;
1352 break;
1353 case SYSCTL_READONLY2:
1354 rw = (securelevel < 2) ? 1 : 0;
1355 break;
1356 case SYSCTL_READWRITE:
1357 rw = 1;
1358 break;
1359 }
1360
1361 /*
1362 * it appears not to be writable at this time, so if someone
1363 * tried to write to it, we must tell them to go away
1364 */
1365 if (!rw && newp != NULL)
1366 return (EPERM);
1367
1368 /*
1369 * step one, copy out the stuff we have presently
1370 */
1371 if (rnode->sysctl_flags & SYSCTL_IMMEDIATE) {
1372 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1373 case CTLTYPE_INT:
1374 d = &rnode->sysctl_idata;
1375 break;
1376 case CTLTYPE_QUAD:
1377 d = &rnode->sysctl_qdata;
1378 break;
1379 default:
1380 return (EINVAL);
1381 }
1382 }
1383 else
1384 d = rnode->sysctl_data;
1385 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
1386 sz = strlen(d) + 1;
1387 else
1388 sz = rnode->sysctl_size;
1389 if (oldp != NULL)
1390 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
1391 if (error)
1392 return (error);
1393 *oldlenp = sz;
1394
1395 /*
1396 * are we done?
1397 */
1398 if (newp == NULL || newlen == 0)
1399 return (0);
1400
1401 /*
1402 * hmm...not done. must now "copy in" new value. re-adjust
1403 * sz to maximum value (strings are "weird").
1404 */
1405 sz = rnode->sysctl_size;
1406 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1407 case CTLTYPE_INT:
1408 case CTLTYPE_QUAD:
1409 case CTLTYPE_STRUCT:
1410 /*
1411 * these data must be *exactly* the same size coming
1412 * in.
1413 */
1414 if (newlen != sz)
1415 return (EINVAL);
1416 error = sysctl_copyin(l, newp, d, sz);
1417 break;
1418 case CTLTYPE_STRING: {
1419 /*
1420 * strings, on the other hand, can be shorter, and we
1421 * let userland be sloppy about the trailing nul.
1422 */
1423 char *newbuf;
1424
1425 /*
1426 * too much new string?
1427 */
1428 if (newlen > sz)
1429 return (EINVAL);
1430
1431 /*
1432 * temporary copy of new inbound string
1433 */
1434 len = MIN(sz, newlen);
1435 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1436 if (newbuf == NULL)
1437 return (ENOMEM);
1438 error = sysctl_copyin(l, newp, newbuf, len);
1439 if (error) {
1440 FREE(newbuf, M_SYSCTLDATA);
1441 return (error);
1442 }
1443
1444 /*
1445 * did they null terminate it, or do we have space
1446 * left to do it ourselves?
1447 */
1448 if (newbuf[len - 1] != '\0' && len == sz) {
1449 FREE(newbuf, M_SYSCTLDATA);
1450 return (EINVAL);
1451 }
1452
1453 /*
1454 * looks good, so pop it into place and zero the rest.
1455 */
1456 if (len > 0)
1457 memcpy(rnode->sysctl_data, newbuf, len);
1458 if (sz != len)
1459 memset((char*)rnode->sysctl_data + len, 0, sz - len);
1460 FREE(newbuf, M_SYSCTLDATA);
1461 break;
1462 }
1463 default:
1464 return (EINVAL);
1465 }
1466
1467 return (error);
1468 }
1469
1470 /*
1471 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1472 * purport to handle it. This interface isn't fully fleshed out yet,
1473 * unfortunately.
1474 */
1475 static int
1476 sysctl_mmap(SYSCTLFN_RWARGS)
1477 {
1478 struct sysctlnode nnode, *node;
1479 int error;
1480
1481 /*
1482 * let's just pretend that didn't happen, m'kay?
1483 */
1484 if (l == NULL)
1485 return (EPERM);
1486
1487 /*
1488 * is this a sysctlnode description of an mmap request?
1489 */
1490 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1491 return (EINVAL);
1492 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
1493 if (error)
1494 return (error);
1495
1496 /*
1497 * does the node they asked for exist?
1498 */
1499 if (namelen != 1)
1500 return (EOPNOTSUPP);
1501 node = rnode;
1502 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1503 if (error)
1504 return (error);
1505
1506 /*
1507 * does this node that we have found purport to handle mmap?
1508 */
1509 if (node->sysctl_func == NULL ||
1510 !(node->sysctl_flags & SYSCTL_MMAP))
1511 return (EOPNOTSUPP);
1512
1513 /*
1514 * well...okay, they asked for it.
1515 */
1516 return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1517 }
1518
1519 /*
1520 * ********************************************************************
1521 * Section 3: Create and destroy from inside the kernel
1522 * ********************************************************************
1523 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1524 * interfaces for the kernel to fling new entries into the mib and rip
1525 * them out later. In the case of sysctl_createv(), the returned copy
1526 * of the node (see sysctl_create()) will be translated back into a
1527 * pointer to the actual node.
1528 *
1529 * Note that sysctl_createv() will return 0 if the create request
1530 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1531 * will return 0 if the node to be destroyed already does not exist
1532 * (aka rm -f) or if it is a parent of other nodes.
1533 *
1534 * This allows two (or more) different subsystems to assert sub-tree
1535 * existence before populating their own nodes, and to remove their
1536 * own nodes without orphaning the others when they are done.
1537 * ********************************************************************
1538 */
1539 int
1540 sysctl_createv(int flags, int type,
1541 const char *namep, struct sysctlnode **rnode,
1542 sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1543 ...)
1544 {
1545 va_list ap;
1546 int error, ni, namelen, name[CTL_MAXNAME];
1547 struct sysctlnode *pnode, nnode, onode;
1548 size_t sz;
1549
1550 /*
1551 * what is it?
1552 */
1553 flags = SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
1554
1555 /*
1556 * where do we put it?
1557 */
1558 va_start(ap, newlen);
1559 namelen = 0;
1560 ni = -1;
1561 do {
1562 if (++ni == CTL_MAXNAME)
1563 return (ENAMETOOLONG);
1564 name[ni] = va_arg(ap, int);
1565 /*
1566 * sorry, this is not supported from here
1567 */
1568 if (name[ni] == CTL_CREATESYM)
1569 return (EINVAL);
1570 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
1571 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
1572 va_end(ap);
1573
1574 /*
1575 * what's it called
1576 */
1577 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >
1578 sizeof(nnode.sysctl_name))
1579 return (ENAMETOOLONG);
1580
1581 /*
1582 * cons up the description of the new node
1583 */
1584 nnode.sysctl_num = name[namelen - 1];
1585 name[namelen - 1] = CTL_CREATE;
1586 nnode.sysctl_size = newlen;
1587 nnode.sysctl_flags = flags;
1588 if (type == CTLTYPE_NODE) {
1589 nnode.sysctl_csize = 0;
1590 nnode.sysctl_clen = 0;
1591 nnode.sysctl_child = NULL;
1592 if (flags & SYSCTL_ALIAS)
1593 nnode.sysctl_alias = qv;
1594 }
1595 else if (flags & SYSCTL_IMMEDIATE) {
1596 switch (type) {
1597 case CTLTYPE_INT:
1598 nnode.sysctl_idata = qv;
1599 break;
1600 case CTLTYPE_QUAD:
1601 nnode.sysctl_qdata = qv;
1602 break;
1603 default:
1604 return (EINVAL);
1605 }
1606 }
1607 else {
1608 nnode.sysctl_data = newp;
1609 }
1610 nnode.sysctl_func = func;
1611 nnode.sysctl_parent = NULL;
1612 nnode.sysctl_ver = 0;
1613
1614 /*
1615 * initialize lock state -- we need locks if the main tree has
1616 * been marked as complete, but since we could be called from
1617 * either there, or from a device driver (say, at device
1618 * insertion), or from an lkm (at lkm load time, say), we
1619 * don't really want to "wait"...
1620 */
1621 error = sysctl_lock(NULL, NULL, 0);
1622 if (error)
1623 return (error);
1624
1625 /*
1626 * locate the prospective parent of the new node, and if we
1627 * find it, add the new node.
1628 */
1629 sz = sizeof(onode);
1630 pnode = (rnode != NULL) ? *rnode : NULL;
1631 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1632 if (error == 0)
1633 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
1634 &nnode, sizeof(nnode), &name[0], NULL,
1635 pnode);
1636
1637 /*
1638 * unfortunately the node we wanted to create is already
1639 * there. if the node that's already there is a reasonable
1640 * facsimile of the node we wanted to create, just pretend
1641 * (for the caller's benefit) that we managed to create the
1642 * node they wanted.
1643 */
1644 if (error == EEXIST) {
1645 /* name is the same as requested... */
1646 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
1647 /* they want the same function... */
1648 nnode.sysctl_func == onode.sysctl_func &&
1649 /* number is the same as requested, or... */
1650 (nnode.sysctl_num == onode.sysctl_num ||
1651 /* they didn't pick a number... */
1652 nnode.sysctl_num == CTL_CREATE)) {
1653 /*
1654 * collision here from trying to create
1655 * something that already existed; let's give
1656 * our customers a hand and tell them they got
1657 * what they wanted.
1658 */
1659 #ifdef SYSCTL_DEBUG_CREATE
1660 printf("cleared\n");
1661 #endif /* SYSCTL_DEBUG_CREATE */
1662 error = 0;
1663 }
1664 }
1665
1666 /*
1667 * if they want to know where the new node is, go find the
1668 * address of the actual node, not the copy that
1669 * sysctl_create() gave us.
1670 */
1671 if (rnode != NULL && error == 0) {
1672 /*
1673 * sysctl_create() gave us back a copy of the node,
1674 * but we need to know where it actually is...
1675 */
1676 name[namelen - 1] = onode.sysctl_num;
1677 pnode = *rnode;
1678 error = sysctl_locate(NULL, &name[0], namelen, &pnode, &ni);
1679 /*
1680 * not expecting an error here, but...
1681 */
1682 if (error == 0)
1683 *rnode = pnode;
1684 }
1685
1686 /*
1687 * now it should be safe to release the lock state.
1688 */
1689 sysctl_unlock(NULL);
1690
1691 if (error != 0) {
1692 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
1693 nnode.sysctl_name, error);
1694 #if 0
1695 if (error != ENOENT)
1696 sysctl_dump(&onode);
1697 #endif
1698 }
1699
1700 return (error);
1701 }
1702
1703 int
1704 sysctl_destroyv(struct sysctlnode *rnode, ...)
1705 {
1706 va_list ap;
1707 int error, name[CTL_MAXNAME], namelen, ni;
1708 struct sysctlnode *pnode, *node;
1709
1710 va_start(ap, rnode);
1711 namelen = 0;
1712 ni = 0;
1713 do {
1714 if (ni == CTL_MAXNAME)
1715 return (ENAMETOOLONG);
1716 name[ni] = va_arg(ap, int);
1717 } while (name[ni++] != CTL_EOL);
1718 namelen = ni - 1;
1719 va_end(ap);
1720
1721 /*
1722 * i can't imagine why we'd be destroying a node when the tree
1723 * wasn't complete, but who knows?
1724 */
1725 error = sysctl_lock(NULL, NULL, 0);
1726 if (error)
1727 return (error);
1728
1729 /*
1730 * where is it?
1731 */
1732 node = rnode;
1733 error = sysctl_locate(NULL, &name[0], namelen, &node, &ni);
1734 if (error) {
1735 /* they want it gone and it's not there, so... */
1736 sysctl_unlock(NULL);
1737 return (error == ENOENT ? 0 : error);
1738 }
1739
1740 /*
1741 * we found it, now let's nuke it
1742 */
1743 name[namelen - 1] = CTL_DESTROY;
1744 pnode = node->sysctl_parent;
1745 error = sysctl_destroy(&name[namelen - 1], 1, NULL, NULL,
1746 node, sizeof(*node), &name[0], NULL,
1747 pnode);
1748 if (error == ENOTEMPTY)
1749 /*
1750 * think of trying to delete "foo" when "foo.bar"
1751 * (which someone else put there) is still in
1752 * existence
1753 */
1754 error = 0;
1755
1756 sysctl_unlock(NULL);
1757
1758 return (error);
1759 }
1760
1761 #if 0
1762 /*
1763 * ********************************************************************
1764 * the dump routine. i haven't yet decided how (if at all) i'll call
1765 * this from userland when it's in the kernel.
1766 * ********************************************************************
1767 */
1768 static const char *
1769 sf(int f)
1770 {
1771 static char s[256];
1772 char *c;
1773
1774 s[0] = '\0';
1775 c = "";
1776
1777 #define print_flag(_f, _s, _c, _q, _x) \
1778 if (((_x) && (((_f) & (_x)) == (__CONCAT(SYSCTL_,_q)))) || \
1779 (!(_x) && ((_f) & (__CONCAT(SYSCTL_,_q))))) { \
1780 strlcat((_s), (_c), sizeof(_s)); \
1781 strlcat((_s), __STRING(_q), sizeof(_s)); \
1782 (_c) = ","; \
1783 (_f) &= ~(__CONCAT(SYSCTL_,_q)|(_x)); \
1784 }
1785 print_flag(f, s, c, READONLY, SYSCTL_READWRITE);
1786 print_flag(f, s, c, READONLY1, SYSCTL_READWRITE);
1787 print_flag(f, s, c, READONLY2, SYSCTL_READWRITE);
1788 print_flag(f, s, c, READWRITE, SYSCTL_READWRITE);
1789 print_flag(f, s, c, ANYWRITE, 0);
1790 print_flag(f, s, c, PRIVATE, 0);
1791 print_flag(f, s, c, PERMANENT, 0);
1792 print_flag(f, s, c, OWNDATA, 0);
1793 print_flag(f, s, c, IMMEDIATE, 0);
1794 print_flag(f, s, c, HEX, 0);
1795 print_flag(f, s, c, ROOT, 0);
1796 print_flag(f, s, c, ANYNUMBER, 0);
1797 print_flag(f, s, c, HIDDEN, 0);
1798 print_flag(f, s, c, ALIAS, 0);
1799 #undef print_flag
1800
1801 if (f) {
1802 char foo[9];
1803 snprintf(foo, sizeof(foo), "%x", f);
1804 strlcat(s, c, sizeof(s));
1805 strlcat(s, foo, sizeof(s));
1806 }
1807
1808 return (s);
1809 }
1810
1811 static const char *
1812 st(int t)
1813 {
1814
1815 switch (t) {
1816 case CTLTYPE_NODE:
1817 return "NODE";
1818 case CTLTYPE_INT:
1819 return "INT";
1820 case CTLTYPE_STRING:
1821 return "STRING";
1822 case CTLTYPE_QUAD:
1823 return "QUAD";
1824 case CTLTYPE_STRUCT:
1825 return "STRUCT";
1826 }
1827
1828 return "???";
1829 }
1830
1831 void
1832 sysctl_dump(const struct sysctlnode *d)
1833 {
1834 static char nmib[64], smib[256];
1835 static int indent;
1836 struct sysctlnode *n;
1837 char *np, *sp, tmp[20];
1838 int i;
1839
1840 if (d == NULL)
1841 return;
1842
1843 np = &nmib[strlen(nmib)];
1844 sp = &smib[strlen(smib)];
1845
1846 if (!(d->sysctl_flags & SYSCTL_ROOT)) {
1847 snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
1848 strcat(nmib, ".");
1849 strcat(smib, ".");
1850 strcat(nmib, tmp);
1851 strcat(smib, d->sysctl_name);
1852 printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
1853 SYSCTL_TYPE(d->sysctl_flags));
1854 }
1855
1856 if (1) {
1857 printf("%*s%p:\tsysctl_name [%s]\n", indent, "",
1858 d, d->sysctl_name);
1859 printf("%*s\t\tsysctl_num %d\n", indent, "",
1860 d->sysctl_num);
1861 printf("%*s\t\tsysctl_flags %x (flags=%x<%s> type=%d<%s> "
1862 "size=%zu)\n",
1863 indent, "", d->sysctl_flags,
1864 SYSCTL_FLAGS(d->sysctl_flags),
1865 sf(SYSCTL_FLAGS(d->sysctl_flags)),
1866 SYSCTL_TYPE(d->sysctl_flags),
1867 st(SYSCTL_TYPE(d->sysctl_flags)),
1868 d->sysctl_size);
1869 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
1870 printf("%*s\t\tsysctl_csize %d\n", indent, "",
1871 d->sysctl_csize);
1872 printf("%*s\t\tsysctl_clen %d\n", indent, "",
1873 d->sysctl_clen);
1874 printf("%*s\t\tsysctl_child %p\n", indent, "",
1875 d->sysctl_child);
1876 }
1877 else
1878 printf("%*s\t\tsysctl_data %p\n", indent, "",
1879 d->sysctl_data);
1880 printf("%*s\t\tsysctl_func %p\n", indent, "",
1881 d->sysctl_func);
1882 printf("%*s\t\tsysctl_parent %p\n", indent, "",
1883 d->sysctl_parent);
1884 printf("%*s\t\tsysctl_ver %d\n", indent, "",
1885 d->sysctl_ver);
1886 }
1887
1888 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
1889 indent += 8;
1890 n = d->sysctl_child;
1891 for (i = 0; i < d->sysctl_clen; i++) {
1892 sysctl_dump(&n[i]);
1893 }
1894 indent -= 8;
1895 }
1896
1897 np[0] = '\0';
1898 sp[0] = '\0';
1899 }
1900 #endif /* 0 */
1901
1902 /*
1903 * ********************************************************************
1904 * Deletes an entire n-ary tree. Not recommended unless you know why
1905 * you're doing it. Personally, I don't know why you'd even think
1906 * about it.
1907 * ********************************************************************
1908 */
1909 void
1910 sysctl_free(struct sysctlnode *rnode)
1911 {
1912 struct sysctlnode *node, *pnode;
1913
1914 if (rnode == NULL)
1915 rnode = &sysctl_root;
1916 pnode = rnode;
1917
1918 node = pnode->sysctl_child;
1919 do {
1920 while (node != NULL && pnode->sysctl_csize > 0) {
1921 while (node <
1922 &pnode->sysctl_child[pnode->sysctl_clen] &&
1923 (SYSCTL_TYPE(node->sysctl_flags) !=
1924 CTLTYPE_NODE ||
1925 node->sysctl_csize == 0)) {
1926 if (SYSCTL_FLAGS(node->sysctl_flags) &
1927 SYSCTL_OWNDATA) {
1928 if (node->sysctl_data != NULL) {
1929 FREE(node->sysctl_data,
1930 M_SYSCTLDATA);
1931 node->sysctl_data = NULL;
1932 }
1933 }
1934 node++;
1935 }
1936 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
1937 pnode = node;
1938 node = node->sysctl_child;
1939 }
1940 else
1941 break;
1942 }
1943 if (pnode->sysctl_child != NULL)
1944 FREE(pnode->sysctl_child, M_SYSCTLNODE);
1945 pnode->sysctl_clen = 0;
1946 pnode->sysctl_csize = 0;
1947 pnode->sysctl_child = NULL;
1948 node = pnode;
1949 pnode = node->sysctl_parent;
1950 } while (pnode != NULL && pnode != rnode);
1951 }
1952
1953 /*
1954 * ********************************************************************
1955 * old_sysctl -- A routine to bridge old-style internal calls to the
1956 * new infrastructure.
1957 * ********************************************************************
1958 */
1959 int
1960 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1961 void *newp, size_t newlen, struct lwp *l)
1962 {
1963 int error;
1964 size_t savelen = *oldlenp;
1965
1966 error = sysctl_lock(l, oldp, savelen);
1967 if (error)
1968 return (error);
1969 error = sysctl_dispatch(name, namelen, oldp, oldlenp,
1970 newp, newlen, name, l, NULL);
1971 sysctl_unlock(l);
1972 if (error == 0 && oldp != NULL && savelen < *oldlenp)
1973 error = ENOMEM;
1974
1975 return (error);
1976 }
1977
1978 /*
1979 * ********************************************************************
1980 * Section 4: Generic helper routines
1981 * ********************************************************************
1982 * "helper" routines that can do more finely grained access control,
1983 * construct structures from disparate information, create the
1984 * appearance of more nodes and sub-trees, etc. for example, if
1985 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
1986 * with a dynamically created list of nodes that represented the
1987 * currently running processes at that instant.
1988 * ********************************************************************
1989 */
1990
1991 /*
1992 * first, a few generic helpers that provide:
1993 *
1994 * sysctl_needfunc() a readonly interface that emits a warning
1995 * sysctl_notavail() returns EOPNOTSUPP (generic error)
1996 * sysctl_null() an empty return buffer with no error
1997 */
1998 int
1999 sysctl_needfunc(SYSCTLFN_ARGS)
2000 {
2001 int error;
2002
2003 printf("!!SYSCTL_NEEDFUNC!!\n");
2004
2005 if (newp != NULL || namelen != 0)
2006 return (EOPNOTSUPP);
2007
2008 error = 0;
2009 if (oldp != NULL)
2010 error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2011 MIN(rnode->sysctl_size, *oldlenp));
2012 *oldlenp = rnode->sysctl_size;
2013
2014 return (error);
2015 }
2016
2017 int
2018 sysctl_notavail(SYSCTLFN_ARGS)
2019 {
2020
2021 if (namelen == 1 && name[0] == CTL_QUERY)
2022 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2023
2024 return (EOPNOTSUPP);
2025 }
2026
2027 int
2028 sysctl_null(SYSCTLFN_ARGS)
2029 {
2030
2031 *oldlenp = 0;
2032
2033 return (0);
2034 }
2035
2036 /*
2037 * ********************************************************************
2038 * Section 5: The machinery that makes it all go
2039 * ********************************************************************
2040 * Memory "manglement" routines. Not much to this, eh?
2041 * ********************************************************************
2042 */
2043 static int
2044 sysctl_alloc(struct sysctlnode *p, int x)
2045 {
2046 int i;
2047 struct sysctlnode *n;
2048
2049 assert(p->sysctl_child == NULL);
2050
2051 if (x == 1)
2052 MALLOC(n, struct sysctlnode *,
2053 sizeof(struct sysctlnode),
2054 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2055 else
2056 MALLOC(n, struct sysctlnode *,
2057 SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2058 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2059 if (n == NULL)
2060 return (ENOMEM);
2061
2062 if (x == 1) {
2063 memset(n, 0, sizeof(struct sysctlnode));
2064 p->sysctl_csize = 1;
2065 }
2066 else {
2067 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2068 p->sysctl_csize = SYSCTL_DEFSIZE;
2069 }
2070 p->sysctl_clen = 0;
2071
2072 for (i = 0; i < p->sysctl_csize; i++)
2073 n[i].sysctl_parent = p;
2074
2075 p->sysctl_child = n;
2076 return (0);
2077 }
2078
2079 static int
2080 sysctl_realloc(struct sysctlnode *p)
2081 {
2082 int i, j;
2083 struct sysctlnode *n;
2084
2085 assert(p->sysctl_csize == p->sysctl_clen);
2086
2087 /*
2088 * how many do we have...how many should we make?
2089 */
2090 i = p->sysctl_clen;
2091 n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
2092 M_WAITOK|M_CANFAIL);
2093 if (n == NULL)
2094 return (ENOMEM);
2095
2096 /*
2097 * move old children over...initialize new children
2098 */
2099 memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
2100 memset(&n[i], 0, i * sizeof(struct sysctlnode));
2101 p->sysctl_csize = 2 * i;
2102 p->sysctl_clen = i;
2103
2104 /*
2105 * reattach moved (and new) children to parent; if a moved
2106 * child node has children, reattach the parent pointers of
2107 * grandchildren
2108 */
2109 for (i = 0; i < p->sysctl_csize; i++) {
2110 n[i].sysctl_parent = p;
2111 if (n[i].sysctl_child != NULL) {
2112 for (j = 0; j < n[i].sysctl_csize; j++)
2113 n[i].sysctl_child[j].sysctl_parent = &n[i];
2114 }
2115 }
2116
2117 /*
2118 * get out with the old and in with the new
2119 */
2120 FREE(p->sysctl_child, M_SYSCTLNODE);
2121 p->sysctl_child = n;
2122
2123 return (0);
2124 }
2125