kern_sysctl.c revision 1.247 1 /* $NetBSD: kern_sysctl.c,v 1.247 2014/02/27 22:50:52 dsl 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.247 2014/02/27 22:50:52 dsl 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, *d_out;
1433 uint64_t qval;
1434 int ival;
1435
1436 KASSERT(rw_lock_held(&sysctl_treelock));
1437
1438 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1439 printf("%s: rnode %p wrong version\n", __func__, rnode);
1440 return EINVAL;
1441 }
1442
1443 if (newlen == 0)
1444 newp = NULL;
1445
1446 error = 0;
1447
1448 /*
1449 * you can't "look up" a node. you can "query" it, but you
1450 * can't "look it up".
1451 */
1452 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0) {
1453 DPRINTF(("%s: can't lookup a node\n", __func__));
1454 return EINVAL;
1455 }
1456
1457 /*
1458 * some nodes are private, so only root can look into them.
1459 */
1460 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
1461 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
1462 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) {
1463 DPRINTF(("%s: private node\n", __func__));
1464 return error;
1465 }
1466
1467 /*
1468 * if a node wants to be writable according to different rules
1469 * other than "only root can write to stuff unless a flag is
1470 * set", then it needs its own function which should have been
1471 * called and not us.
1472 */
1473 if (l != NULL && newp != NULL &&
1474 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
1475 (error = kauth_authorize_system(l->l_cred,
1476 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_MODIFY, NULL, NULL,
1477 NULL)) != 0) {
1478 DPRINTF(("%s: can't modify\n", __func__));
1479 return error;
1480 }
1481
1482 /*
1483 * is this node supposedly writable?
1484 */
1485 rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0;
1486
1487 /*
1488 * it appears not to be writable at this time, so if someone
1489 * tried to write to it, we must tell them to go away
1490 */
1491 if (!rw && newp != NULL) {
1492 DPRINTF(("%s: not writable\n", __func__));
1493 return EPERM;
1494 }
1495
1496 /*
1497 * step one, copy out the stuff we have presently
1498 */
1499 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
1500 /*
1501 * note that we discard const here because we are
1502 * modifying the contents of the node (which is okay
1503 * because it's ours)
1504 *
1505 * It also doesn't matter which field of the union we pick.
1506 */
1507 d = __UNCONST(&rnode->sysctl_qdata);
1508 } else
1509 d = rnode->sysctl_data;
1510 d_out = d;
1511
1512 sz = rnode->sysctl_size;
1513 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1514 case CTLTYPE_INT:
1515 /* Allow for 64bit read of 32bit value */
1516 if (*oldlenp == sizeof (uint64_t)) {
1517 qval = *(int *)d;
1518 d_out = &qval;
1519 sz = sizeof (uint64_t);
1520 }
1521 break;
1522 case CTLTYPE_QUAD:
1523 /* Allow for 32bit read of 64bit value */
1524 if (*oldlenp == sizeof (int)) {
1525 qval = *(uint64_t *)d;
1526 ival = qval < 0x100000000 ? qval : 0xffffffff;
1527 d_out = &ival;
1528 sz = sizeof (int);
1529 }
1530 break;
1531 case CTLTYPE_STRING:
1532 sz = strlen(d) + 1; /* XXX@@@ possible fault here */
1533 break;
1534 default:
1535 break;
1536 }
1537 if (oldp != NULL) {
1538 error = sysctl_copyout(l, d_out, oldp, MIN(sz, *oldlenp));
1539 if (error) {
1540 DPRINTF(("%s: bad copyout %d\n", __func__, error));
1541 return error;
1542 }
1543 }
1544 *oldlenp = sz;
1545
1546 /*
1547 * are we done?
1548 */
1549 if (newp == NULL)
1550 return 0;
1551
1552 /*
1553 * hmm...not done. must now "copy in" new value. re-adjust
1554 * sz to maximum value (strings are "weird").
1555 */
1556 sz = rnode->sysctl_size;
1557 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1558 case CTLTYPE_BOOL: {
1559 bool tmp;
1560 /*
1561 * these data must be *exactly* the same size coming
1562 * in. bool may only be true or false.
1563 */
1564 if (newlen != sz) {
1565 DPRINTF(("%s: bad size %zu != %zu\n", __func__, newlen,
1566 sz));
1567 return EINVAL;
1568 }
1569 error = sysctl_copyin(l, newp, &tmp, sz);
1570 if (error)
1571 break;
1572 if (tmp != true && tmp != false) {
1573 DPRINTF(("%s: tmp %d\n", __func__, tmp));
1574 return EINVAL;
1575 }
1576 *(bool *)d = tmp;
1577 break;
1578 }
1579 case CTLTYPE_INT:
1580 case CTLTYPE_QUAD:
1581 /* Allow 32bit of 64bit integers */
1582 if (newlen == sizeof (uint64_t)) {
1583 error = sysctl_copyin(l, newp, &qval, sizeof qval);
1584 } else if (newlen == sizeof (int)) {
1585 error = sysctl_copyin(l, newp, &ival, sizeof ival);
1586 qval = ival;
1587 } else {
1588 goto bad_size;
1589 }
1590 if (!error) {
1591 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_INT) {
1592 ival = qval;
1593 /* Error out of range values */
1594 if (ival != qval)
1595 goto bad_size;
1596 *(int *)d = ival;
1597 } else {
1598 *(uint64_t *)d = qval;
1599 }
1600 }
1601 break;
1602 case CTLTYPE_STRUCT:
1603 /*
1604 * these data must be *exactly* the same size coming
1605 * in.
1606 */
1607 if (newlen != sz)
1608 goto bad_size;
1609 error = sysctl_copyin(l, newp, d, sz);
1610 break;
1611 case CTLTYPE_STRING: {
1612 /*
1613 * strings, on the other hand, can be shorter, and we
1614 * let userland be sloppy about the trailing nul.
1615 */
1616 char *newbuf;
1617
1618 /*
1619 * too much new string?
1620 */
1621 if (newlen > sz)
1622 goto bad_size;
1623
1624 /*
1625 * temporary copy of new inbound string
1626 */
1627 len = MIN(sz, newlen);
1628 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1629 if (newbuf == NULL) {
1630 DPRINTF(("%s: oomem %zu\n", __func__, len));
1631 return ENOMEM;
1632 }
1633 error = sysctl_copyin(l, newp, newbuf, len);
1634 if (error) {
1635 free(newbuf, M_SYSCTLDATA);
1636 DPRINTF(("%s: copyin %d\n", __func__, error));
1637 return error;
1638 }
1639
1640 /*
1641 * did they NUL terminate it, or do we have space
1642 * left to do it ourselves?
1643 */
1644 if (newbuf[len - 1] != '\0' && len == sz) {
1645 free(newbuf, M_SYSCTLDATA);
1646 DPRINTF(("%s: string too long\n", __func__));
1647 return EINVAL;
1648 }
1649
1650 /*
1651 * looks good, so pop it into place and zero the rest.
1652 */
1653 if (len > 0)
1654 memcpy(d, newbuf, len);
1655 if (sz != len)
1656 memset((char*)d + len, 0, sz - len);
1657 free(newbuf, M_SYSCTLDATA);
1658 break;
1659 }
1660 default:
1661 DPRINTF(("%s: bad type\n", __func__));
1662 return EINVAL;
1663 }
1664 if (error) {
1665 DPRINTF(("%s: copyin %d\n", __func__, error));
1666 }
1667
1668 return error;
1669
1670 bad_size:
1671 DPRINTF(("%s: bad size %zu > %zu\n", __func__, newlen, sz));
1672 return EINVAL;
1673 }
1674
1675 /*
1676 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1677 * purport to handle it. This interface isn't fully fleshed out yet,
1678 * unfortunately.
1679 */
1680 static int
1681 sysctl_mmap(SYSCTLFN_ARGS)
1682 {
1683 const struct sysctlnode *node;
1684 struct sysctlnode nnode;
1685 int error;
1686
1687 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1688 printf("sysctl_mmap: rnode %p wrong version\n", rnode);
1689 return (EINVAL);
1690 }
1691
1692 /*
1693 * let's just pretend that didn't happen, m'kay?
1694 */
1695 if (l == NULL)
1696 return (EPERM);
1697
1698 /*
1699 * is this a sysctlnode description of an mmap request?
1700 */
1701 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1702 return (EINVAL);
1703 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
1704 if (error)
1705 return (error);
1706
1707 /*
1708 * does the node they asked for exist?
1709 */
1710 if (namelen != 1)
1711 return (EOPNOTSUPP);
1712 node = rnode;
1713 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1714 if (error)
1715 return (error);
1716
1717 /*
1718 * does this node that we have found purport to handle mmap?
1719 */
1720 if (node->sysctl_func == NULL ||
1721 !(node->sysctl_flags & CTLFLAG_MMAP))
1722 return (EOPNOTSUPP);
1723
1724 /*
1725 * well...okay, they asked for it.
1726 */
1727 return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1728 }
1729
1730 int
1731 sysctl_describe(SYSCTLFN_ARGS)
1732 {
1733 struct sysctldesc *d;
1734 void *bf;
1735 size_t sz, left, tot;
1736 int i, error, v = -1;
1737 struct sysctlnode *node;
1738 struct sysctlnode dnode;
1739
1740 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1741 printf("sysctl_query: rnode %p wrong version\n", rnode);
1742 return (EINVAL);
1743 }
1744
1745 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
1746 return (ENOTDIR);
1747 if (namelen != 1 || name[0] != CTL_DESCRIBE)
1748 return (EINVAL);
1749
1750 /*
1751 * get ready...
1752 */
1753 error = 0;
1754 d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK|M_CANFAIL);
1755 if (bf == NULL)
1756 return ENOMEM;
1757 tot = 0;
1758 node = rnode->sysctl_child;
1759 left = *oldlenp;
1760
1761 /*
1762 * no request -> all descriptions at this level
1763 * request with desc unset -> just this node
1764 * request with desc set -> set descr for this node
1765 */
1766 if (newp != NULL) {
1767 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode);
1768 if (error)
1769 goto out;
1770 if (dnode.sysctl_desc != NULL) {
1771 /*
1772 * processes cannot set descriptions above
1773 * securelevel 0. and must be root. blah
1774 * blah blah. a couple more checks are made
1775 * once we find the node we want.
1776 */
1777 if (l != NULL) {
1778 #ifndef SYSCTL_DISALLOW_CREATE
1779 error = kauth_authorize_system(l->l_cred,
1780 KAUTH_SYSTEM_SYSCTL,
1781 KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL,
1782 NULL, NULL);
1783 if (error)
1784 goto out;
1785 #else /* SYSCTL_DISALLOW_CREATE */
1786 error = EPERM;
1787 goto out;
1788 #endif /* SYSCTL_DISALLOW_CREATE */
1789 }
1790
1791 /*
1792 * find node and try to set the description on it
1793 */
1794 for (i = 0; i < rnode->sysctl_clen; i++)
1795 if (node[i].sysctl_num == dnode.sysctl_num)
1796 break;
1797 if (i == rnode->sysctl_clen) {
1798 error = ENOENT;
1799 goto out;
1800 }
1801 node = &node[i];
1802
1803 /*
1804 * did the caller specify a node version?
1805 */
1806 if (dnode.sysctl_ver != 0 &&
1807 dnode.sysctl_ver != node->sysctl_ver) {
1808 error = EINVAL;
1809 goto out;
1810 }
1811
1812 /*
1813 * okay...some rules:
1814 * (1) if setup is done and the tree is
1815 * read-only or the whole system is
1816 * read-only
1817 * (2) no one can set a description on a
1818 * permanent node (it must be set when
1819 * using createv)
1820 * (3) processes cannot *change* a description
1821 * (4) processes *can*, however, set a
1822 * description on a read-only node so that
1823 * one can be created and then described
1824 * in two steps
1825 * anything else come to mind?
1826 */
1827 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
1828 (!(sysctl_rootof(node)->sysctl_flags &
1829 CTLFLAG_READWRITE) ||
1830 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) {
1831 error = EPERM;
1832 goto out;
1833 }
1834 if (node->sysctl_flags & CTLFLAG_PERMANENT) {
1835 error = EPERM;
1836 goto out;
1837 }
1838 if (l != NULL && node->sysctl_desc != NULL) {
1839 error = EPERM;
1840 goto out;
1841 }
1842
1843 /*
1844 * right, let's go ahead. the first step is
1845 * making the description into something the
1846 * node can "own", if need be.
1847 */
1848 if (l != NULL ||
1849 dnode.sysctl_flags & CTLFLAG_OWNDESC) {
1850 char *nd, *k;
1851
1852 k = malloc(MAXDESCLEN, M_TEMP,
1853 M_WAITOK|M_CANFAIL);
1854 if (k == NULL) {
1855 error = ENOMEM;
1856 goto out;
1857 }
1858 error = sysctl_copyinstr(l, dnode.sysctl_desc,
1859 k, MAXDESCLEN, &sz);
1860 if (error) {
1861 free(k, M_TEMP);
1862 goto out;
1863 }
1864 nd = malloc(sz, M_SYSCTLDATA,
1865 M_WAITOK|M_CANFAIL);
1866 if (nd == NULL) {
1867 free(k, M_TEMP);
1868 error = ENOMEM;
1869 goto out;
1870 }
1871 memcpy(nd, k, sz);
1872 dnode.sysctl_flags |= CTLFLAG_OWNDESC;
1873 dnode.sysctl_desc = nd;
1874 free(k, M_TEMP);
1875 }
1876
1877 /*
1878 * now "release" the old description and
1879 * attach the new one. ta-da.
1880 */
1881 if ((node->sysctl_flags & CTLFLAG_OWNDESC) &&
1882 node->sysctl_desc != NULL)
1883 /*XXXUNCONST*/
1884 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA);
1885 node->sysctl_desc = dnode.sysctl_desc;
1886 node->sysctl_flags |=
1887 (dnode.sysctl_flags & CTLFLAG_OWNDESC);
1888
1889 /*
1890 * now we "fall out" and into the loop which
1891 * will copy the new description back out for
1892 * those interested parties
1893 */
1894 }
1895 }
1896
1897 /*
1898 * scan for one description or just retrieve all descriptions
1899 */
1900 for (i = 0; i < rnode->sysctl_clen; i++) {
1901 /*
1902 * did they ask for the description of only one node?
1903 */
1904 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num)
1905 continue;
1906
1907 /*
1908 * don't describe "private" nodes to non-suser users
1909 */
1910 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) &&
1911 !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
1912 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)))
1913 continue;
1914
1915 /*
1916 * is this description "valid"?
1917 */
1918 memset(bf, 0, MAXDESCLEN);
1919 if (node[i].sysctl_desc == NULL)
1920 sz = 1;
1921 else if (copystr(node[i].sysctl_desc, &d->descr_str[0],
1922 MAXDESCLEN - sizeof(*d), &sz) != 0) {
1923 /*
1924 * erase possible partial description
1925 */
1926 memset(bf, 0, MAXDESCLEN);
1927 sz = 1;
1928 }
1929
1930 /*
1931 * we've got it, stuff it into the caller's buffer
1932 */
1933 d->descr_num = node[i].sysctl_num;
1934 d->descr_ver = node[i].sysctl_ver;
1935 d->descr_len = sz; /* includes trailing nul */
1936 sz = (char *)NEXT_DESCR(d) - (char *)d;
1937 if (oldp != NULL && left >= sz) {
1938 error = sysctl_copyout(l, d, oldp, sz);
1939 if (error)
1940 goto out;
1941 left -= sz;
1942 oldp = (void *)__sysc_desc_adv(oldp, d->descr_len);
1943 }
1944 tot += sz;
1945
1946 /*
1947 * if we get this far with v not "unset", they asked
1948 * for a specific node and we found it
1949 */
1950 if (v != -1)
1951 break;
1952 }
1953
1954 /*
1955 * did we find it after all?
1956 */
1957 if (v != -1 && tot == 0)
1958 error = ENOENT;
1959 else
1960 *oldlenp = tot;
1961
1962 out:
1963 free(bf, M_TEMP);
1964 return (error);
1965 }
1966
1967 /*
1968 * ********************************************************************
1969 * Section 3: Create and destroy from inside the kernel
1970 * ********************************************************************
1971 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1972 * interfaces for the kernel to fling new entries into the mib and rip
1973 * them out later. In the case of sysctl_createv(), the returned copy
1974 * of the node (see sysctl_create()) will be translated back into a
1975 * pointer to the actual node.
1976 *
1977 * Note that sysctl_createv() will return 0 if the create request
1978 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1979 * will return 0 if the node to be destroyed already does not exist
1980 * (aka rm -f) or if it is a parent of other nodes.
1981 *
1982 * This allows two (or more) different subsystems to assert sub-tree
1983 * existence before populating their own nodes, and to remove their
1984 * own nodes without orphaning the others when they are done.
1985 * ********************************************************************
1986 */
1987 #undef sysctl_createv
1988 int
1989 sysctl_createv(struct sysctllog **log, int cflags,
1990 const struct sysctlnode **rnode, const struct sysctlnode **cnode,
1991 int flags, int type, const char *namep, const char *descr,
1992 sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1993 ...)
1994 {
1995 va_list ap;
1996 int error, ni, namelen, name[CTL_MAXNAME];
1997 const struct sysctlnode *root, *pnode;
1998 struct sysctlnode nnode, onode, *dnode;
1999 size_t sz;
2000
2001 /*
2002 * where are we putting this?
2003 */
2004 if (rnode != NULL && *rnode == NULL) {
2005 printf("sysctl_createv: rnode NULL\n");
2006 return (EINVAL);
2007 }
2008 root = rnode ? *rnode : NULL;
2009 if (cnode != NULL)
2010 *cnode = NULL;
2011 if (cflags != 0)
2012 return (EINVAL);
2013
2014 /*
2015 * what is it?
2016 */
2017 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
2018 if (log != NULL)
2019 flags &= ~CTLFLAG_PERMANENT;
2020
2021 /*
2022 * where do we put it?
2023 */
2024 va_start(ap, newlen);
2025 namelen = 0;
2026 error = 0;
2027 ni = -1;
2028 do {
2029 if (++ni == CTL_MAXNAME) {
2030 error = ENAMETOOLONG;
2031 break;
2032 }
2033 name[ni] = va_arg(ap, int);
2034 /*
2035 * sorry, this is not supported from here
2036 */
2037 if (name[ni] == CTL_CREATESYM) {
2038 error = EINVAL;
2039 break;
2040 }
2041 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
2042 va_end(ap);
2043 if (error)
2044 return error;
2045 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
2046
2047 /*
2048 * what's it called
2049 */
2050 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >=
2051 sizeof(nnode.sysctl_name))
2052 return (ENAMETOOLONG);
2053
2054 /*
2055 * cons up the description of the new node
2056 */
2057 nnode.sysctl_num = name[namelen - 1];
2058 name[namelen - 1] = CTL_CREATE;
2059 nnode.sysctl_size = newlen;
2060 nnode.sysctl_flags = flags;
2061 if (type == CTLTYPE_NODE) {
2062 nnode.sysctl_csize = 0;
2063 nnode.sysctl_clen = 0;
2064 nnode.sysctl_child = NULL;
2065 if (flags & CTLFLAG_ALIAS)
2066 nnode.sysctl_alias = qv;
2067 } else if (flags & CTLFLAG_IMMEDIATE) {
2068 switch (type) {
2069 case CTLTYPE_BOOL:
2070 nnode.sysctl_bdata = qv;
2071 break;
2072 case CTLTYPE_INT:
2073 nnode.sysctl_idata = qv;
2074 break;
2075 case CTLTYPE_QUAD:
2076 nnode.sysctl_qdata = qv;
2077 break;
2078 default:
2079 return (EINVAL);
2080 }
2081 } else {
2082 nnode.sysctl_data = newp;
2083 }
2084 nnode.sysctl_func = func;
2085 nnode.sysctl_parent = NULL;
2086 nnode.sysctl_ver = 0;
2087
2088 /*
2089 * initialize lock state -- we need locks if the main tree has
2090 * been marked as complete, but since we could be called from
2091 * either there, or from a device driver (say, at device
2092 * insertion), or from a module (at module load time, say), we
2093 * don't really want to "wait"...
2094 */
2095 sysctl_lock(true);
2096
2097 /*
2098 * locate the prospective parent of the new node, and if we
2099 * find it, add the new node.
2100 */
2101 sz = sizeof(onode);
2102 pnode = root;
2103 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
2104 if (error) {
2105 printf("sysctl_createv: sysctl_locate(%s) returned %d\n",
2106 nnode.sysctl_name, error);
2107 sysctl_unlock();
2108 return (error);
2109 }
2110 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
2111 &nnode, sizeof(nnode), &name[0], NULL,
2112 pnode);
2113
2114 /*
2115 * unfortunately the node we wanted to create is already
2116 * there. if the node that's already there is a reasonable
2117 * facsimile of the node we wanted to create, just pretend
2118 * (for the caller's benefit) that we managed to create the
2119 * node they wanted.
2120 */
2121 if (error == EEXIST) {
2122 /* name is the same as requested... */
2123 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
2124 /* they want the same function... */
2125 nnode.sysctl_func == onode.sysctl_func &&
2126 /* number is the same as requested, or... */
2127 (nnode.sysctl_num == onode.sysctl_num ||
2128 /* they didn't pick a number... */
2129 nnode.sysctl_num == CTL_CREATE)) {
2130 /*
2131 * collision here from trying to create
2132 * something that already existed; let's give
2133 * our customers a hand and tell them they got
2134 * what they wanted.
2135 */
2136 #ifdef SYSCTL_DEBUG_CREATE
2137 printf("cleared\n");
2138 #endif /* SYSCTL_DEBUG_CREATE */
2139 error = 0;
2140 }
2141 }
2142
2143 if (error == 0 &&
2144 (cnode != NULL || log != NULL || descr != NULL)) {
2145 /*
2146 * sysctl_create() gave us back a copy of the node,
2147 * but we need to know where it actually is...
2148 */
2149 pnode = root;
2150 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
2151
2152 /*
2153 * manual scan of last layer so that aliased nodes
2154 * aren't followed.
2155 */
2156 if (error == 0) {
2157 for (ni = 0; ni < pnode->sysctl_clen; ni++)
2158 if (pnode->sysctl_child[ni].sysctl_num ==
2159 onode.sysctl_num)
2160 break;
2161 if (ni < pnode->sysctl_clen)
2162 pnode = &pnode->sysctl_child[ni];
2163 else
2164 error = ENOENT;
2165 }
2166
2167 /*
2168 * not expecting an error here, but...
2169 */
2170 if (error == 0) {
2171 if (log != NULL)
2172 sysctl_log_add(log, pnode);
2173 if (cnode != NULL)
2174 *cnode = pnode;
2175 if (descr != NULL) {
2176 /*
2177 * allow first caller to *set* a
2178 * description actually to set it
2179 *
2180 * discard const here so we can attach
2181 * the description
2182 */
2183 dnode = __UNCONST(pnode);
2184 if (pnode->sysctl_desc != NULL)
2185 /* skip it...we've got one */;
2186 else if (flags & CTLFLAG_OWNDESC) {
2187 size_t l = strlen(descr) + 1;
2188 char *d = malloc(l, M_SYSCTLDATA,
2189 M_WAITOK|M_CANFAIL);
2190 if (d != NULL) {
2191 memcpy(d, descr, l);
2192 dnode->sysctl_desc = d;
2193 dnode->sysctl_flags |=
2194 CTLFLAG_OWNDESC;
2195 }
2196 } else
2197 dnode->sysctl_desc = descr;
2198 }
2199 } else {
2200 printf("sysctl_create succeeded but node not found?!\n");
2201 /*
2202 * confusing, but the create said it
2203 * succeeded, so...
2204 */
2205 error = 0;
2206 }
2207 }
2208
2209 /*
2210 * now it should be safe to release the lock state. note that
2211 * the pointer to the newly created node being passed back may
2212 * not be "good" for very long.
2213 */
2214 sysctl_unlock();
2215
2216 if (error != 0) {
2217 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
2218 nnode.sysctl_name, error);
2219 #if 0
2220 if (error != ENOENT)
2221 sysctl_dump(&onode);
2222 #endif
2223 }
2224
2225 return (error);
2226 }
2227
2228 int
2229 sysctl_destroyv(struct sysctlnode *rnode, ...)
2230 {
2231 va_list ap;
2232 int error, name[CTL_MAXNAME], namelen, ni;
2233 const struct sysctlnode *pnode, *node;
2234 struct sysctlnode dnode, *onode;
2235 size_t sz;
2236
2237 va_start(ap, rnode);
2238 namelen = 0;
2239 ni = 0;
2240 do {
2241 if (ni == CTL_MAXNAME) {
2242 va_end(ap);
2243 return (ENAMETOOLONG);
2244 }
2245 name[ni] = va_arg(ap, int);
2246 } while (name[ni++] != CTL_EOL);
2247 namelen = ni - 1;
2248 va_end(ap);
2249
2250 /*
2251 * i can't imagine why we'd be destroying a node when the tree
2252 * wasn't complete, but who knows?
2253 */
2254 sysctl_lock(true);
2255
2256 /*
2257 * where is it?
2258 */
2259 node = rnode;
2260 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
2261 if (error) {
2262 /* they want it gone and it's not there, so... */
2263 sysctl_unlock();
2264 return (error == ENOENT ? 0 : error);
2265 }
2266
2267 /*
2268 * set up the deletion
2269 */
2270 pnode = node;
2271 node = &dnode;
2272 memset(&dnode, 0, sizeof(dnode));
2273 dnode.sysctl_flags = SYSCTL_VERSION;
2274 dnode.sysctl_num = name[namelen - 1];
2275
2276 /*
2277 * we found it, now let's nuke it
2278 */
2279 name[namelen - 1] = CTL_DESTROY;
2280 sz = 0;
2281 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
2282 node, sizeof(*node), &name[0], NULL,
2283 pnode);
2284 if (error == ENOTEMPTY) {
2285 /*
2286 * think of trying to delete "foo" when "foo.bar"
2287 * (which someone else put there) is still in
2288 * existence
2289 */
2290 error = 0;
2291
2292 /*
2293 * dunno who put the description there, but if this
2294 * node can ever be removed, we need to make sure the
2295 * string doesn't go out of context. that means we
2296 * need to find the node that's still there (don't use
2297 * sysctl_locate() because that follows aliasing).
2298 */
2299 node = pnode->sysctl_child;
2300 for (ni = 0; ni < pnode->sysctl_clen; ni++)
2301 if (node[ni].sysctl_num == dnode.sysctl_num)
2302 break;
2303 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL;
2304
2305 /*
2306 * if we found it, and this node has a description,
2307 * and this node can be released, and it doesn't
2308 * already own its own description...sigh. :)
2309 */
2310 if (node != NULL && node->sysctl_desc != NULL &&
2311 !(node->sysctl_flags & CTLFLAG_PERMANENT) &&
2312 !(node->sysctl_flags & CTLFLAG_OWNDESC)) {
2313 char *d;
2314
2315 sz = strlen(node->sysctl_desc) + 1;
2316 d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2317 if (d != NULL) {
2318 /*
2319 * discard const so that we can
2320 * re-attach the description
2321 */
2322 memcpy(d, node->sysctl_desc, sz);
2323 onode = __UNCONST(node);
2324 onode->sysctl_desc = d;
2325 onode->sysctl_flags |= CTLFLAG_OWNDESC;
2326 } else {
2327 /*
2328 * XXX drop the description? be
2329 * afraid? don't care?
2330 */
2331 }
2332 }
2333 }
2334
2335 sysctl_unlock();
2336
2337 return (error);
2338 }
2339
2340 /*
2341 * ********************************************************************
2342 * Deletes an entire n-ary tree. Not recommended unless you know why
2343 * you're doing it. Personally, I don't know why you'd even think
2344 * about it.
2345 * ********************************************************************
2346 */
2347 void
2348 sysctl_free(struct sysctlnode *rnode)
2349 {
2350 struct sysctlnode *node, *pnode;
2351
2352 rw_enter(&sysctl_treelock, RW_WRITER);
2353
2354 if (rnode == NULL)
2355 rnode = &sysctl_root;
2356
2357 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
2358 printf("sysctl_free: rnode %p wrong version\n", rnode);
2359 rw_exit(&sysctl_treelock);
2360 return;
2361 }
2362
2363 pnode = rnode;
2364
2365 node = pnode->sysctl_child;
2366 do {
2367 while (node != NULL && pnode->sysctl_csize > 0) {
2368 while (node <
2369 &pnode->sysctl_child[pnode->sysctl_clen] &&
2370 (SYSCTL_TYPE(node->sysctl_flags) !=
2371 CTLTYPE_NODE ||
2372 node->sysctl_csize == 0)) {
2373 if (SYSCTL_FLAGS(node->sysctl_flags) &
2374 CTLFLAG_OWNDATA) {
2375 if (node->sysctl_data != NULL) {
2376 free(node->sysctl_data,
2377 M_SYSCTLDATA);
2378 node->sysctl_data = NULL;
2379 }
2380 }
2381 if (SYSCTL_FLAGS(node->sysctl_flags) &
2382 CTLFLAG_OWNDESC) {
2383 if (node->sysctl_desc != NULL) {
2384 /*XXXUNCONST*/
2385 free(__UNCONST(node->sysctl_desc),
2386 M_SYSCTLDATA);
2387 node->sysctl_desc = NULL;
2388 }
2389 }
2390 node++;
2391 }
2392 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
2393 pnode = node;
2394 node = node->sysctl_child;
2395 } else
2396 break;
2397 }
2398 if (pnode->sysctl_child != NULL)
2399 free(pnode->sysctl_child, M_SYSCTLNODE);
2400 pnode->sysctl_clen = 0;
2401 pnode->sysctl_csize = 0;
2402 pnode->sysctl_child = NULL;
2403 node = pnode;
2404 pnode = node->sysctl_parent;
2405 } while (pnode != NULL && node != rnode);
2406
2407 rw_exit(&sysctl_treelock);
2408 }
2409
2410 void
2411 sysctl_log_print(const struct sysctllog *slog)
2412 {
2413 int i, len;
2414
2415 printf("root %p left %d size %d content", (const void *)slog->log_root,
2416 slog->log_left, slog->log_size);
2417
2418 for (len = 0, i = slog->log_left; i < slog->log_size; i++) {
2419 switch (len) {
2420 case 0:
2421 len = -1;
2422 printf(" version %d", slog->log_num[i]);
2423 break;
2424 case -1:
2425 len = -2;
2426 printf(" type %d", slog->log_num[i]);
2427 break;
2428 case -2:
2429 len = slog->log_num[i];
2430 printf(" len %d:", slog->log_num[i]);
2431 if (len <= 0)
2432 len = -1;
2433 break;
2434 default:
2435 len--;
2436 printf(" %d", slog->log_num[i]);
2437 break;
2438 }
2439 }
2440 printf(" end\n");
2441 }
2442
2443 int
2444 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node)
2445 {
2446 const int size0 = 16;
2447 int name[CTL_MAXNAME], namelen, i;
2448 const struct sysctlnode *pnode;
2449 struct sysctllog *log;
2450
2451 if (node->sysctl_flags & CTLFLAG_PERMANENT)
2452 return (0);
2453
2454 if (logp == NULL)
2455 return (0);
2456
2457 if (*logp == NULL) {
2458 log = malloc(sizeof(struct sysctllog),
2459 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2460 if (log == NULL) {
2461 /* XXX print error message? */
2462 return (-1);
2463 }
2464 log->log_num = malloc(size0 * sizeof(int),
2465 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2466 if (log->log_num == NULL) {
2467 /* XXX print error message? */
2468 free(log, M_SYSCTLDATA);
2469 return (-1);
2470 }
2471 memset(log->log_num, 0, size0 * sizeof(int));
2472 log->log_root = NULL;
2473 log->log_size = size0;
2474 log->log_left = size0;
2475 *logp = log;
2476 } else
2477 log = *logp;
2478
2479 /*
2480 * check that the root is proper. it's okay to record the
2481 * address of the root of a tree. it's the only thing that's
2482 * guaranteed not to shift around as nodes come and go.
2483 */
2484 if (log->log_root == NULL)
2485 log->log_root = sysctl_rootof(node);
2486 else if (log->log_root != sysctl_rootof(node)) {
2487 printf("sysctl: log %p root mismatch (%p)\n",
2488 log->log_root, sysctl_rootof(node));
2489 return (-1);
2490 }
2491
2492 /*
2493 * we will copy out name in reverse order
2494 */
2495 for (pnode = node, namelen = 0;
2496 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
2497 pnode = pnode->sysctl_parent)
2498 name[namelen++] = pnode->sysctl_num;
2499
2500 /*
2501 * do we have space?
2502 */
2503 if (log->log_left < (namelen + 3))
2504 sysctl_log_realloc(log);
2505 if (log->log_left < (namelen + 3))
2506 return (-1);
2507
2508 /*
2509 * stuff name in, then namelen, then node type, and finally,
2510 * the version for non-node nodes.
2511 */
2512 for (i = 0; i < namelen; i++)
2513 log->log_num[--log->log_left] = name[i];
2514 log->log_num[--log->log_left] = namelen;
2515 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
2516 if (log->log_num[log->log_left] != CTLTYPE_NODE)
2517 log->log_num[--log->log_left] = node->sysctl_ver;
2518 else
2519 log->log_num[--log->log_left] = 0;
2520
2521 return (0);
2522 }
2523
2524 void
2525 sysctl_teardown(struct sysctllog **logp)
2526 {
2527 const struct sysctlnode *rnode;
2528 struct sysctlnode node;
2529 struct sysctllog *log;
2530 uint namelen;
2531 int *name, t, v, error, ni;
2532 size_t sz;
2533
2534 if (logp == NULL || *logp == NULL)
2535 return;
2536 log = *logp;
2537
2538 rw_enter(&sysctl_treelock, RW_WRITER);
2539 memset(&node, 0, sizeof(node));
2540
2541 while (log->log_left < log->log_size) {
2542 KASSERT((log->log_left + 3 < log->log_size) &&
2543 (log->log_left + log->log_num[log->log_left + 2] <=
2544 log->log_size));
2545 v = log->log_num[log->log_left++];
2546 t = log->log_num[log->log_left++];
2547 namelen = log->log_num[log->log_left++];
2548 name = &log->log_num[log->log_left];
2549
2550 node.sysctl_num = name[namelen - 1];
2551 node.sysctl_flags = SYSCTL_VERSION|t;
2552 node.sysctl_ver = v;
2553
2554 rnode = log->log_root;
2555 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
2556 if (error == 0) {
2557 name[namelen - 1] = CTL_DESTROY;
2558 rnode = rnode->sysctl_parent;
2559 sz = 0;
2560 (void)sysctl_destroy(&name[namelen - 1], 1, NULL,
2561 &sz, &node, sizeof(node),
2562 &name[0], NULL, rnode);
2563 }
2564
2565 log->log_left += namelen;
2566 }
2567
2568 KASSERT(log->log_size == log->log_left);
2569 free(log->log_num, M_SYSCTLDATA);
2570 free(log, M_SYSCTLDATA);
2571 *logp = NULL;
2572
2573 rw_exit(&sysctl_treelock);
2574 }
2575
2576 /*
2577 * ********************************************************************
2578 * old_sysctl -- A routine to bridge old-style internal calls to the
2579 * new infrastructure.
2580 * ********************************************************************
2581 */
2582 int
2583 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2584 void *newp, size_t newlen, struct lwp *l)
2585 {
2586 int error;
2587 size_t oldlen = 0;
2588 size_t savelen;
2589
2590 if (oldlenp) {
2591 oldlen = *oldlenp;
2592 }
2593 savelen = oldlen;
2594
2595 sysctl_lock(newp != NULL);
2596 error = sysctl_dispatch(name, namelen, oldp, &oldlen,
2597 newp, newlen, name, l, NULL);
2598 sysctl_unlock();
2599 if (error == 0 && oldp != NULL && savelen < oldlen)
2600 error = ENOMEM;
2601 if (oldlenp) {
2602 *oldlenp = oldlen;
2603 }
2604
2605 return (error);
2606 }
2607
2608 /*
2609 * ********************************************************************
2610 * Section 4: Generic helper routines
2611 * ********************************************************************
2612 * "helper" routines that can do more finely grained access control,
2613 * construct structures from disparate information, create the
2614 * appearance of more nodes and sub-trees, etc. for example, if
2615 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
2616 * with a dynamically created list of nodes that represented the
2617 * currently running processes at that instant.
2618 * ********************************************************************
2619 */
2620
2621 /*
2622 * first, a few generic helpers that provide:
2623 *
2624 * sysctl_needfunc() a readonly interface that emits a warning
2625 * sysctl_notavail() returns EOPNOTSUPP (generic error)
2626 * sysctl_null() an empty return buffer with no error
2627 */
2628 int
2629 sysctl_needfunc(SYSCTLFN_ARGS)
2630 {
2631 int error;
2632
2633 printf("!!SYSCTL_NEEDFUNC!!\n");
2634
2635 if (newp != NULL || namelen != 0)
2636 return (EOPNOTSUPP);
2637
2638 error = 0;
2639 if (oldp != NULL)
2640 error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2641 MIN(rnode->sysctl_size, *oldlenp));
2642 *oldlenp = rnode->sysctl_size;
2643
2644 return (error);
2645 }
2646
2647 int
2648 sysctl_notavail(SYSCTLFN_ARGS)
2649 {
2650
2651 if (namelen == 1 && name[0] == CTL_QUERY)
2652 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2653
2654 return (EOPNOTSUPP);
2655 }
2656
2657 int
2658 sysctl_null(SYSCTLFN_ARGS)
2659 {
2660
2661 *oldlenp = 0;
2662
2663 return (0);
2664 }
2665
2666 u_int
2667 sysctl_map_flags(const u_int *map, u_int word)
2668 {
2669 u_int rv;
2670
2671 for (rv = 0; *map != 0; map += 2)
2672 if ((word & map[0]) != 0)
2673 rv |= map[1];
2674
2675 return rv;
2676 }
2677
2678 /*
2679 * ********************************************************************
2680 * Section 5: The machinery that makes it all go
2681 * ********************************************************************
2682 * Memory "manglement" routines. Not much to this, eh?
2683 * ********************************************************************
2684 */
2685 static int
2686 sysctl_alloc(struct sysctlnode *p, int x)
2687 {
2688 int i;
2689 struct sysctlnode *n;
2690
2691 assert(p->sysctl_child == NULL);
2692
2693 if (x == 1)
2694 n = malloc(sizeof(struct sysctlnode),
2695 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2696 else
2697 n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2698 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2699 if (n == NULL)
2700 return (ENOMEM);
2701
2702 if (x == 1) {
2703 memset(n, 0, sizeof(struct sysctlnode));
2704 p->sysctl_csize = 1;
2705 } else {
2706 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2707 p->sysctl_csize = SYSCTL_DEFSIZE;
2708 }
2709 p->sysctl_clen = 0;
2710
2711 for (i = 0; i < p->sysctl_csize; i++)
2712 n[i].sysctl_parent = p;
2713
2714 p->sysctl_child = n;
2715 return (0);
2716 }
2717
2718 static int
2719 sysctl_realloc(struct sysctlnode *p)
2720 {
2721 int i, j, olen;
2722 struct sysctlnode *n;
2723
2724 assert(p->sysctl_csize == p->sysctl_clen);
2725
2726 /*
2727 * how many do we have...how many should we make?
2728 */
2729 olen = p->sysctl_clen;
2730 n = malloc(2 * olen * sizeof(struct sysctlnode), M_SYSCTLNODE,
2731 M_WAITOK|M_CANFAIL);
2732 if (n == NULL)
2733 return (ENOMEM);
2734
2735 /*
2736 * move old children over...initialize new children
2737 */
2738 memcpy(n, p->sysctl_child, olen * sizeof(struct sysctlnode));
2739 memset(&n[olen], 0, olen * sizeof(struct sysctlnode));
2740 p->sysctl_csize = 2 * olen;
2741
2742 /*
2743 * reattach moved (and new) children to parent; if a moved
2744 * child node has children, reattach the parent pointers of
2745 * grandchildren
2746 */
2747 for (i = 0; i < p->sysctl_csize; i++) {
2748 n[i].sysctl_parent = p;
2749 if (n[i].sysctl_child != NULL) {
2750 for (j = 0; j < n[i].sysctl_csize; j++)
2751 n[i].sysctl_child[j].sysctl_parent = &n[i];
2752 }
2753 }
2754
2755 /*
2756 * get out with the old and in with the new
2757 */
2758 free(p->sysctl_child, M_SYSCTLNODE);
2759 p->sysctl_child = n;
2760
2761 return (0);
2762 }
2763
2764 static int
2765 sysctl_log_realloc(struct sysctllog *log)
2766 {
2767 int *n, s, d;
2768
2769 s = log->log_size * 2;
2770 d = log->log_size;
2771
2772 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2773 if (n == NULL)
2774 return (-1);
2775
2776 memset(n, 0, s * sizeof(int));
2777 memcpy(&n[d], log->log_num, d * sizeof(int));
2778 free(log->log_num, M_SYSCTLDATA);
2779 log->log_num = n;
2780 if (d)
2781 log->log_left += d;
2782 else
2783 log->log_left = s;
2784 log->log_size = s;
2785
2786 return (0);
2787 }
2788
2789 /*
2790 * ********************************************************************
2791 * Section 6: Conversion between API versions wrt the sysctlnode
2792 * ********************************************************************
2793 */
2794 static int
2795 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
2796 struct sysctlnode *node)
2797 {
2798 int error, flags;
2799
2800 if (i == NULL || sz < sizeof(flags))
2801 return (EINVAL);
2802
2803 error = sysctl_copyin(l, i, &flags, sizeof(flags));
2804 if (error)
2805 return (error);
2806
2807 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2808 #error sysctl_cvt_in: no support for SYSCTL_VERSION
2809 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2810
2811 if (sz == sizeof(*node) &&
2812 SYSCTL_VERS(flags) == SYSCTL_VERSION) {
2813 error = sysctl_copyin(l, i, node, sizeof(*node));
2814 if (error)
2815 return (error);
2816 *vp = SYSCTL_VERSION;
2817 return (0);
2818 }
2819
2820 return (EINVAL);
2821 }
2822
2823 static int
2824 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
2825 void *ovp, size_t left, size_t *szp)
2826 {
2827 size_t sz = sizeof(*i);
2828 const void *src = i;
2829 int error;
2830
2831 switch (v) {
2832 case SYSCTL_VERS_0:
2833 return (EINVAL);
2834
2835 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2836 #error sysctl_cvt_out: no support for SYSCTL_VERSION
2837 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2838
2839 case SYSCTL_VERSION:
2840 /* nothing more to do here */
2841 break;
2842 }
2843
2844 if (ovp != NULL && left >= sz) {
2845 error = sysctl_copyout(l, src, ovp, sz);
2846 if (error)
2847 return (error);
2848 }
2849
2850 if (szp != NULL)
2851 *szp = sz;
2852
2853 return (0);
2854 }
2855