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