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