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