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