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