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