kern_sysctl.c revision 1.155 1 /* $NetBSD: kern_sysctl.c,v 1.155 2003/12/28 22:36:37 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 2003 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Mike Karels at Berkeley Software Design, Inc.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
71 */
72
73 /*
74 * sysctl system call.
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.155 2003/12/28 22:36:37 atatat Exp $");
79
80 #include "opt_defcorename.h"
81 #include "opt_insecure.h"
82 #include "ksyms.h"
83
84 #include <sys/param.h>
85 #include <sys/sysctl.h>
86 #include <sys/systm.h>
87 #include <sys/buf.h>
88 #include <sys/ksyms.h>
89 #include <sys/malloc.h>
90 #include <sys/mount.h>
91 #include <sys/sa.h>
92 #include <sys/syscallargs.h>
93 #include <machine/stdarg.h>
94
95 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
96 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data");
97
98 static int sysctl_mmap(SYSCTLFN_RWPROTO);
99 static int sysctl_alloc(struct sysctlnode *, int);
100 static int sysctl_realloc(struct sysctlnode *);
101
102 /*
103 * the "root" of the new sysctl tree
104 */
105 static struct sysctlnode sysctl_root = {
106 .sysctl_flags = SYSCTL_ROOT|
107 SYSCTL_READWRITE|
108 CTLTYPE_NODE,
109 .sysctl_num = 0,
110 .sysctl_size = sizeof(struct sysctlnode),
111 .sysctl_name = "(root)",
112 };
113
114 /*
115 * link set of functions that add nodes at boot time (see also
116 * sysctl_buildtree())
117 */
118 __link_set_decl(sysctl_funcs, sysctl_setup_func);
119
120 /*
121 * The `sysctl_lock' is intended to serialize access to the sysctl
122 * tree. Given that it is now (a) dynamic, and (b) most consumers of
123 * sysctl are going to be copying data out, the old `sysctl_memlock'
124 * has been `upgraded' to simply guard the whole tree.
125 *
126 * The two new data here are to keep track of the locked chunk of
127 * memory, if there is one, so that it can be released more easily
128 * from anywhere.
129 */
130 struct lock sysctl_treelock;
131 caddr_t sysctl_memaddr;
132 size_t sysctl_memsize;
133
134 /*
135 * Attributes stored in the kernel.
136 */
137 char hostname[MAXHOSTNAMELEN];
138 int hostnamelen;
139
140 char domainname[MAXHOSTNAMELEN];
141 int domainnamelen;
142
143 long hostid;
144
145 #ifdef INSECURE
146 int securelevel = -1;
147 #else
148 int securelevel = 0;
149 #endif
150
151 #ifndef DEFCORENAME
152 #define DEFCORENAME "%n.core"
153 #endif
154 char defcorename[MAXPATHLEN] = DEFCORENAME;
155
156 /*
157 * ********************************************************************
158 * Section 0: Some simple glue
159 * ********************************************************************
160 * By wrapping copyin(), copyout(), and copyinstr() like this, we can
161 * stop caring about who's calling us and simplify some code a bunch.
162 * ********************************************************************
163 */
164 static inline int
165 sysctl_copyin(const struct lwp *l, const void *uaddr, void *kaddr, size_t len)
166 {
167
168 if (l != NULL)
169 return (copyin(uaddr, kaddr, len));
170
171 memcpy(kaddr, uaddr, len);
172
173 return (0);
174 }
175
176 static inline int
177 sysctl_copyout(const struct lwp *l, const void *kaddr, void *uaddr, size_t len)
178 {
179
180 if (l != NULL)
181 return (copyout(kaddr, uaddr, len));
182
183 memcpy(uaddr, kaddr, len);
184
185 return (0);
186 }
187
188 static inline int
189 sysctl_copyinstr(const struct lwp *l, const void *uaddr, void *kaddr,
190 size_t len, size_t *done)
191 {
192
193 if (l != NULL)
194 return (copyinstr(uaddr, kaddr, len, done));
195 else
196 return (copystr(uaddr, kaddr, len, done));
197 }
198
199 /*
200 * ********************************************************************
201 * Initialize sysctl subsystem.
202 * ********************************************************************
203 */
204 void
205 sysctl_init(void)
206 {
207 sysctl_setup_func **sysctl_setup, f;
208
209 lockinit(&sysctl_treelock, PRIBIO|PCATCH, "sysctl", 0, 0);
210
211 /*
212 * dynamic mib numbers start here
213 */
214 sysctl_root.sysctl_num = CREATE_BASE;
215
216 __link_set_foreach(sysctl_setup, sysctl_funcs) {
217 /*
218 * XXX - why do i have to coerce the pointers like this?
219 */
220 f = (void*)*sysctl_setup;
221 (*f)();
222 }
223
224 /*
225 * setting this means no more permanent nodes can be added,
226 * trees that claim to be readonly at the root now are, and if
227 * the main tree is readonly, *everything* is.
228 */
229 sysctl_root.sysctl_flags |= SYSCTL_PERMANENT;
230
231 }
232
233 /*
234 * ********************************************************************
235 * The main native sysctl system call itself.
236 * ********************************************************************
237 */
238 int
239 sys___sysctl(struct lwp *l, void *v, register_t *retval)
240 {
241 struct sys___sysctl_args /* {
242 syscallarg(int *) name;
243 syscallarg(u_int) namelen;
244 syscallarg(void *) old;
245 syscallarg(size_t *) oldlenp;
246 syscallarg(void *) new;
247 syscallarg(size_t) newlen;
248 } */ *uap = v;
249 int error, nerror, name[CTL_MAXNAME];
250 size_t oldlen, savelen, *oldlenp;
251
252 /*
253 * get oldlen
254 */
255 oldlen = 0;
256 oldlenp = SCARG(uap, oldlenp);
257 if (oldlenp != NULL) {
258 error = copyin(oldlenp, &oldlen, sizeof(oldlen));
259 if (error)
260 return (error);
261 }
262 savelen = oldlen;
263
264 /*
265 * top-level sysctl names may or may not be non-terminal, but
266 * we don't care
267 */
268 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
269 return (EINVAL);
270 error = copyin(SCARG(uap, name), &name,
271 SCARG(uap, namelen) * sizeof(int));
272 if (error)
273 return (error);
274
275 /*
276 * wire old so that copyout() is less likely to fail?
277 */
278 error = sysctl_lock(l, SCARG(uap, old), savelen);
279 if (error)
280 return (error);
281
282 /*
283 * do sysctl work (NULL means main built-in default tree)
284 */
285 error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
286 SCARG(uap, old), &oldlen,
287 SCARG(uap, new), SCARG(uap, newlen),
288 &name[0], l, NULL);
289
290 /*
291 * release the sysctl lock
292 */
293 sysctl_unlock(l);
294
295 /*
296 * set caller's oldlen to new value even in the face of an
297 * error (if this gets an error and they didn't have one, they
298 * get this one)
299 */
300 if (oldlenp) {
301 nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
302 if (error == 0)
303 error = nerror;
304 }
305
306 /*
307 * if the only problem is that we weren't given enough space,
308 * that's an ENOMEM error
309 */
310 if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
311 error = ENOMEM;
312
313 return (error);
314 }
315
316 /*
317 * ********************************************************************
318 * Section 1: How the tree is used
319 * ********************************************************************
320 * Implementations of sysctl for emulations should typically need only
321 * these three functions in this order: lock the tree, dispatch
322 * request into it, unlock the tree.
323 * ********************************************************************
324 */
325 int
326 sysctl_lock(struct lwp *l, void *oldp, size_t savelen)
327 {
328 int error = 0;
329
330 error = lockmgr(&sysctl_treelock, LK_EXCLUSIVE, NULL);
331 if (error)
332 return (error);
333
334 if (l != NULL && oldp != NULL && savelen) {
335 error = uvm_vslock(l->l_proc, oldp, savelen, VM_PROT_WRITE);
336 if (error) {
337 (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
338 return (error);
339 }
340 sysctl_memaddr = oldp;
341 sysctl_memsize = savelen;
342 }
343
344 return (0);
345 }
346
347 /*
348 * ********************************************************************
349 * the main sysctl dispatch routine. scans the given tree and picks a
350 * function to call based on what it finds.
351 * ********************************************************************
352 */
353 int
354 sysctl_dispatch(SYSCTLFN_RWARGS)
355 {
356 int error;
357 sysctlfn fn;
358 int ni;
359
360 fn = NULL;
361 error = sysctl_locate(l, name, namelen, &rnode, &ni);
362
363 /*
364 * the node we ended up at has a function, so call it. it can
365 * hand off to query or create if it wants to.
366 */
367 if (rnode->sysctl_func != NULL)
368 fn = rnode->sysctl_func;
369
370 /*
371 * we found the node they were looking for, so do a lookup.
372 */
373 else if (error == 0)
374 fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
375
376 /*
377 * prospective parent node found, but the terminal node was
378 * not. generic operations associate with the parent.
379 */
380 else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
381 switch (name[ni]) {
382 case CTL_QUERY:
383 fn = sysctl_query;
384 break;
385 case CTL_CREATE:
386 #if NKSYMS > 0
387 case CTL_CREATESYM:
388 #endif /* NKSYMS > 0 */
389 fn = (sysctlfn)sysctl_create; /* we own the rnode */
390 break;
391 case CTL_DESTROY:
392 fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
393 break;
394 case CTL_MMAP:
395 fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
396 break;
397 default:
398 error = EOPNOTSUPP;
399 break;
400 }
401 }
402
403 /*
404 * after all of that, maybe we found someone who knows how to
405 * get us what we want?
406 */
407 if (fn != NULL)
408 error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
409 newp, newlen, name, l, rnode);
410
411 else if (error == 0)
412 error = EOPNOTSUPP;
413
414 return (error);
415 }
416
417 /*
418 * ********************************************************************
419 * Releases the tree lock. Note that if uvm_vslock() was called when
420 * the lock was taken, we release that memory now. By keeping track
421 * of where and how much by ourselves, the lock can be released much
422 * more easily from anywhere.
423 * ********************************************************************
424 */
425 void
426 sysctl_unlock(struct lwp *l)
427 {
428
429 if (l != NULL && sysctl_memsize != 0) {
430 uvm_vsunlock(l->l_proc, sysctl_memaddr, sysctl_memsize);
431 sysctl_memsize = 0;
432 }
433
434 (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
435 }
436
437 /*
438 * ********************************************************************
439 * Section 2: The main tree interfaces
440 * ********************************************************************
441 * This is how sysctl_dispatch() does its work, and you can too, by
442 * calling these routines from helpers (though typically only
443 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these
444 * are called.
445 * ********************************************************************
446 */
447
448 /*
449 * sysctl_locate -- Finds the node matching the given mib under the
450 * given tree (via rv). If no tree is given, we fall back to the
451 * native tree. The current process (via l) is used for access
452 * control on the tree (some nodes may be traverable only by root) and
453 * on return, nip will show how many numbers in the mib were consumed.
454 */
455 int
456 sysctl_locate(struct lwp *l, const int *name, u_int namelen,
457 struct sysctlnode **rv, int *nip)
458 {
459 struct sysctlnode *node, *pnode;
460 int tn, si, ni, error, alias;
461
462 /*
463 * basic checks and setup
464 */
465 if (*rv == NULL)
466 *rv = &sysctl_root;
467 if (nip)
468 *nip = 0;
469 if (namelen < 0)
470 return (EINVAL);
471 if (namelen == 0)
472 return (0);
473
474 /*
475 * search starts from "root"
476 */
477 pnode = *rv;
478 node = pnode->sysctl_child;
479 error = 0;
480
481 /*
482 * scan for node to which new node should be attached
483 */
484 for (ni = 0; ni < namelen; ni++) {
485 /*
486 * walked off bottom of tree
487 */
488 if (node == NULL) {
489 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
490 error = ENOENT;
491 else
492 error = ENOTDIR;
493 break;
494 }
495 /*
496 * can anyone traverse this node or only root?
497 */
498 if (l != NULL && (pnode->sysctl_flags & SYSCTL_PRIVATE) &&
499 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
500 != 0)
501 return (error);
502 /*
503 * find a child node with the right number
504 */
505 tn = name[ni];
506 alias = 0;
507 for (si = 0; si < pnode->sysctl_clen; si++) {
508 if (node[si].sysctl_num == tn ||
509 (tn >= 0 &&
510 node[si].sysctl_flags & SYSCTL_ANYNUMBER)) {
511 if (node[si].sysctl_flags & SYSCTL_ALIAS) {
512 if (alias++ == 4)
513 si = pnode->sysctl_clen - 1;
514 else {
515 tn = node[si].sysctl_alias;
516 si = -1;
517 }
518 }
519 else
520 break;
521 }
522 }
523 /*
524 * if we ran off the end, it obviously doesn't exist
525 */
526 if (si == pnode->sysctl_clen) {
527 error = ENOENT;
528 break;
529 }
530 /*
531 * so far so good, move on down the line
532 */
533 pnode = &node[si];
534 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
535 node = node[si].sysctl_child;
536 else
537 node = NULL;
538 }
539
540 *rv = pnode;
541 if (nip)
542 *nip = ni;
543
544 return (error);
545 }
546
547 /*
548 * sysctl_query -- The auto-discovery engine. Copies out the
549 * descriptions on nodes under the given node and handles overlay
550 * trees.
551 */
552 int
553 sysctl_query(SYSCTLFN_ARGS)
554 {
555 int error, ni, elim;
556 size_t out, left, t;
557 struct sysctlnode *enode, *onode;
558
559 if (newp != NULL)
560 return (EPERM);
561 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
562 return (ENOTDIR);
563 if (namelen != 1 || name[0] != CTL_QUERY)
564 return (EINVAL);
565
566 error = 0;
567 out = 0;
568 left = *oldlenp;
569 elim = 0;
570 enode = NULL;
571
572 /*
573 * process has overlay tree
574 */
575 if (l && l->l_proc->p_emul->e_sysctlovly) {
576 enode = (void*)l->l_proc->p_emul->e_sysctlovly;
577 elim = (name - oname);
578 error = sysctl_locate(l, oname, elim, &enode, NULL);
579 if (error == 0) {
580 /* ah, found parent in overlay */
581 elim = enode->sysctl_clen;
582 enode = enode->sysctl_child;
583 }
584 else {
585 error = 0;
586 elim = 0;
587 enode = NULL;
588 }
589 }
590
591 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
592 t = MIN(left, sizeof(struct sysctlnode));
593 onode = &rnode->sysctl_child[ni];
594 if (enode && enode->sysctl_num == onode->sysctl_num) {
595 if (SYSCTL_TYPE(enode->sysctl_flags) !=
596 CTLTYPE_NODE)
597 onode = enode;
598 if (--elim > 0)
599 enode++;
600 else
601 enode = NULL;
602 }
603 if (oldp != NULL && t > 0)
604 error = sysctl_copyout(l, onode, (char*)oldp + out, t);
605 if (error)
606 return (error);
607 out += sizeof(struct sysctlnode);
608 left -= t;
609 }
610
611 /*
612 * overlay trees *MUST* be entirely consumed
613 */
614 KASSERT(enode == NULL);
615
616 *oldlenp = out;
617
618 return (error);
619 }
620
621 #ifdef SYSCTL_DEBUG_CREATE
622 #undef sysctl_create
623 #endif /* SYSCTL_DEBUG_CREATE */
624
625 /*
626 * sysctl_create -- Adds a node (the description of which is taken
627 * from newp) to the tree, returning a copy of it in the space pointed
628 * to by oldp. In the event that the requested slot is already taken
629 * (either by name or by number), the offending node is returned
630 * instead. Yes, this is complex, but we want to make sure everything
631 * is proper.
632 */
633 int
634 sysctl_create(SYSCTLFN_RWARGS)
635 {
636 struct sysctlnode nnode, *node, *pnode;
637 int error, ni, at, nm, type, sz, flags, rw, anum;
638 void *own;
639
640 error = 0;
641 own = NULL;
642 anum = -1;
643
644 if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
645 #if NKSYMS > 0
646 && name[namelen - 1] != CTL_CREATESYM
647 #endif /* NKSYMS > 0 */
648 ))
649 return (EINVAL);
650
651 /*
652 * processes can only add nodes at securelevel 0, must be
653 * root, and can't add nodes to a parent that's not writeable
654 */
655 if (l != NULL) {
656 if (securelevel > 0)
657 return (EPERM);
658 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
659 if (error)
660 return (error);
661 #ifndef SYSCTL_DISALLOW_CREATE
662 if (!(rnode->sysctl_flags & SYSCTL_READWRITE))
663 #endif /* SYSCTL_DISALLOW_CREATE */
664 return (EPERM);
665 }
666
667 /*
668 * nothing can add a node if:
669 * we've finished initial set up and
670 * the tree itself is not writeable or
671 * the entire sysctl system is not writeable
672 */
673 if ((sysctl_root.sysctl_flags & SYSCTL_PERMANENT) &&
674 (!(sysctl_rootof(rnode)->sysctl_flags & SYSCTL_READWRITE) ||
675 !(sysctl_root.sysctl_flags & SYSCTL_READWRITE)))
676 return (EPERM);
677
678 /*
679 * it must be a "node", not a "int" or something
680 */
681 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
682 return (ENOTDIR);
683 pnode = rnode;
684
685 if (newp == NULL || newlen != sizeof(struct sysctlnode))
686 return (EINVAL);
687 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
688 if (error)
689 return (error);
690
691 /*
692 * nodes passed in don't *have* parents
693 */
694 if (nnode.sysctl_parent != NULL)
695 return (EINVAL);
696
697 /*
698 * if we are indeed adding it, it should be a "good" name and
699 * number
700 */
701 nm = nnode.sysctl_num;
702 #if NKSYMS > 0
703 if (nm == CTL_CREATESYM)
704 nm = CTL_CREATE;
705 #endif /* NKSYMS > 0 */
706 if (nm < 0 && nm != CTL_CREATE)
707 return (EINVAL);
708 sz = 0;
709
710 /*
711 * the name can't start with a digit
712 */
713 if (nnode.sysctl_name[sz] >= '0' &&
714 nnode.sysctl_name[sz] <= '9')
715 return (EINVAL);
716
717 /*
718 * the name must be only alphanumerics or - or _, longer than
719 * 0 bytes and less that SYSCTL_NAMELEN
720 */
721 while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
722 if ((nnode.sysctl_name[sz] >= '0' &&
723 nnode.sysctl_name[sz] <= '9') ||
724 (nnode.sysctl_name[sz] >= 'A' &&
725 nnode.sysctl_name[sz] <= 'Z') ||
726 (nnode.sysctl_name[sz] >= 'a' &&
727 nnode.sysctl_name[sz] <= 'z') ||
728 nnode.sysctl_name[sz] == '-' ||
729 nnode.sysctl_name[sz] == '_')
730 sz++;
731 else
732 return (EINVAL);
733 }
734 if (sz == 0 || sz == SYSCTL_NAMELEN)
735 return (EINVAL);
736
737 /*
738 * various checks revolve around size vs type, etc
739 */
740 type = SYSCTL_TYPE(nnode.sysctl_flags);
741 flags = SYSCTL_FLAGS(nnode.sysctl_flags);
742 rw = (flags & SYSCTL_READWRITE) ? B_WRITE : B_READ;
743 sz = nnode.sysctl_size;
744
745 /*
746 * find out if there's a collision, and if so, let the caller
747 * know what they collided with
748 */
749 node = pnode->sysctl_child;
750 if (((flags & SYSCTL_ANYNUMBER) && node) ||
751 (node && node->sysctl_flags & SYSCTL_ANYNUMBER))
752 return (EINVAL);
753 for (ni = at = 0; ni < pnode->sysctl_clen; ni++) {
754 if (nm == node[ni].sysctl_num ||
755 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
756 if (oldp != NULL) {
757 /*
758 * ignore error here, since we
759 * are already fixed on EEXIST
760 */
761 (void)sysctl_copyout(l, &node[ni], oldp,
762 MIN(*oldlenp, sizeof(struct sysctlnode)));
763 }
764 *oldlenp = sizeof(struct sysctlnode);
765 return (EEXIST);
766 }
767 if (nm > node[ni].sysctl_num)
768 at++;
769 }
770
771 /*
772 * use sysctl_ver to add to the tree iff it hasn't changed
773 */
774 if (nnode.sysctl_ver != 0) {
775 /*
776 * a specified value must match either the parent
777 * node's version or the root node's version
778 */
779 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
780 nnode.sysctl_ver != rnode->sysctl_ver) {
781 return (EINVAL);
782 }
783 }
784
785 /*
786 * only the kernel can assign functions to entries
787 */
788 if (l != NULL && nnode.sysctl_func != NULL)
789 return (EPERM);
790
791 /*
792 * only the kernel can create permanent entries, and only then
793 * before the kernel is finished setting itself up
794 */
795 if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
796 return (EPERM);
797 if ((flags & SYSCTL_PERMANENT) &
798 (sysctl_root.sysctl_flags & SYSCTL_PERMANENT))
799 return (EPERM);
800 if ((flags & (SYSCTL_OWNDATA | SYSCTL_IMMEDIATE)) ==
801 (SYSCTL_OWNDATA | SYSCTL_IMMEDIATE))
802 return (EINVAL);
803 if ((flags & SYSCTL_IMMEDIATE) &&
804 type != CTLTYPE_INT && type != CTLTYPE_QUAD)
805 return (EINVAL);
806
807 /*
808 * check size, or set it if unset and we can figure it out.
809 * kernel created nodes are allowed to have a function instead
810 * of a size (or a data pointer).
811 */
812 switch (type) {
813 case CTLTYPE_NODE:
814 /*
815 * only *i* can assert the size of a node
816 */
817 if (flags & SYSCTL_ALIAS) {
818 anum = nnode.sysctl_alias;
819 if (anum < 0)
820 return (EINVAL);
821 nnode.sysctl_alias = 0;
822 }
823 if (sz != 0 || nnode.sysctl_data != NULL)
824 return (EINVAL);
825 if (nnode.sysctl_csize != 0 ||
826 nnode.sysctl_clen != 0 ||
827 nnode.sysctl_child != 0)
828 return (EINVAL);
829 if (flags & SYSCTL_OWNDATA)
830 return (EINVAL);
831 sz = sizeof(struct sysctlnode);
832 break;
833 case CTLTYPE_INT:
834 /*
835 * since an int is an int, if the size is not given or
836 * is wrong, we can "int-uit" it.
837 */
838 if (sz != 0 && sz != sizeof(int))
839 return (EINVAL);
840 sz = sizeof(int);
841 break;
842 case CTLTYPE_STRING:
843 /*
844 * strings are a little more tricky
845 */
846 if (sz == 0) {
847 if (l == NULL) {
848 if (nnode.sysctl_func == NULL) {
849 if (nnode.sysctl_data == NULL)
850 return (EINVAL);
851 else
852 sz = strlen(nnode.sysctl_data) +
853 1;
854 }
855 }
856 else if (nnode.sysctl_data == NULL &&
857 flags & SYSCTL_OWNDATA) {
858 return (EINVAL);
859 }
860 else {
861 char *v, *e;
862 size_t s;
863
864 /*
865 * arbitrary limit here...
866 */
867 e = NULL; /* XXX: gcc on NetBSD/sparc */
868 for (s = PAGE_SIZE, v = nnode.sysctl_data;
869 s < 32 * PAGE_SIZE;
870 s += PAGE_SIZE, v += PAGE_SIZE) {
871 /*
872 * XXX @@@ the use of uvm_kernacc() can generate false negatives on
873 * some ports, so this needs to be refined shortly.
874 */
875 if (!uvm_kernacc(v, PAGE_SIZE, rw))
876 return (EFAULT);
877 e = memchr(v, '\0', PAGE_SIZE);
878 if (e != NULL)
879 break;
880 }
881 if (s >= 32 * PAGE_SIZE)
882 return (ERANGE);
883 sz = e - ((char*)nnode.sysctl_data) + 1;
884 }
885 }
886 break;
887 case CTLTYPE_QUAD:
888 if (sz != 0 && sz != sizeof(u_quad_t))
889 return (EINVAL);
890 sz = sizeof(u_quad_t);
891 break;
892 case CTLTYPE_STRUCT:
893 if (sz == 0) {
894 if (l != NULL || nnode.sysctl_func == NULL)
895 return (EINVAL);
896 if (flags & SYSCTL_OWNDATA)
897 return (EINVAL);
898 }
899 break;
900 default:
901 return (EINVAL);
902 }
903
904 /*
905 * at this point, if sz is zero, we *must* have a
906 * function to go with it and we can't own it.
907 */
908
909 /*
910 * l ptr own
911 * 0 0 0 -> EINVAL (if no func)
912 * 0 0 1 -> own
913 * 0 1 0 -> kptr
914 * 0 1 1 -> kptr
915 * 1 0 0 -> EINVAL
916 * 1 0 1 -> own
917 * 1 1 0 -> kptr, no own (check via uvm_kernacc)
918 * 1 1 1 -> uptr, own
919 */
920 if (type != CTLTYPE_NODE) {
921 if (sz != 0) {
922 if (flags & SYSCTL_OWNDATA) {
923 own = malloc(sz, M_SYSCTLDATA,
924 M_WAITOK|M_CANFAIL);
925 if (nnode.sysctl_data == NULL)
926 memset(own, 0, sz);
927 else {
928 error = sysctl_copyin(l,
929 nnode.sysctl_data, own, sz);
930 if (error != 0) {
931 FREE(own, M_SYSCTLDATA);
932 return (error);
933 }
934 }
935 }
936 else if ((nnode.sysctl_data != NULL) &&
937 !(flags & SYSCTL_IMMEDIATE)) {
938 #if NKSYMS > 0
939 if (name[namelen - 1] == CTL_CREATESYM) {
940 char symname[128]; /* XXX enough? */
941 u_long symaddr;
942 size_t symlen;
943
944 error = sysctl_copyinstr(l,
945 nnode.sysctl_data, symname,
946 sizeof(symname), &symlen);
947 if (error)
948 return (error);
949 error = ksyms_getval_from_kernel(NULL,
950 symname, &symaddr, KSYMS_EXTERN);
951 if (error)
952 return (error); /* EINVAL? */
953 nnode.sysctl_data = (void*)symaddr;
954 }
955 #endif /* NKSYMS > 0 */
956 if (!uvm_kernacc(nnode.sysctl_data, sz, rw)) {
957 #ifdef HAVE_SOLUTION_TO_UVM_KERNACC_PROBLEM
958 /* XXX @@@ what is fix? */
959 return (EFAULT);
960 #else /* HAVE_SOLUTION_TO_UVM_KERNACC_PROBLEM */
961 /*
962 * XXX @@@ the use of uvm_kernacc() can generate false negatives on
963 * some ports, so this needs to be refined shortly. by checking here
964 * to see if SYSCTL_PERMANENT is set in the root, we can differentiate
965 * between nodes being created from sysctl_init() during bootstrap and
966 * "other nodes", so we can at least allow the bootstrap to succeed by
967 * simply "trusting" the kernel not to shoot itself in the foot right
968 * from the start.
969 */
970 if ((sysctl_root.sysctl_flags &
971 SYSCTL_PERMANENT)) {
972 printf("fault 2 %p %lu %d\n", nnode.sysctl_data, (unsigned long)sz, rw);
973 return (EFAULT);
974 }
975 #endif /* HAVE_SOLUTION_TO_UVM_KERNACC_PROBLEM */
976 }
977 }
978 }
979 else if (nnode.sysctl_func == NULL)
980 return (EINVAL);
981 }
982
983 /*
984 * a process can't assign a function to a node, and the kernel
985 * can't create a node that has no function or data.
986 * (XXX somewhat redundant check)
987 */
988 if (l != NULL || nnode.sysctl_func == NULL) {
989 if (type != CTLTYPE_NODE &&
990 nnode.sysctl_data == NULL &&
991 !(flags & SYSCTL_IMMEDIATE) &&
992 own == NULL)
993 return (EINVAL);
994 }
995
996 #ifdef SYSCTL_DISALLOW_KWRITE
997 /*
998 * a process can't create a writable node unless it refers to
999 * new data.
1000 */
1001 if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
1002 (flags & SYSCTL_READWRITE) != SYSCTL_READONLY &&
1003 !(flags & SYSCTL_IMMEDIATE))
1004 return (EPERM);
1005 #endif /* SYSCTL_DISALLOW_KWRITE */
1006
1007 /*
1008 * make sure there's somewhere to put the new stuff.
1009 */
1010 if (pnode->sysctl_child == NULL) {
1011 if (flags & SYSCTL_ANYNUMBER)
1012 error = sysctl_alloc(pnode, 1);
1013 else
1014 error = sysctl_alloc(pnode, 0);
1015 if (error)
1016 return (error);
1017 }
1018 node = pnode->sysctl_child;
1019
1020 /*
1021 * no collisions, so pick a good dynamic number if we need to.
1022 */
1023 if (nm == CTL_CREATE) {
1024 nm = ++sysctl_root.sysctl_num;
1025 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
1026 if (nm == node[ni].sysctl_num) {
1027 nm++;
1028 ni = -1;
1029 }
1030 else if (nm > node[ni].sysctl_num)
1031 at = ni + 1;
1032 }
1033 }
1034
1035 /*
1036 * oops...ran out of space
1037 */
1038 if (pnode->sysctl_clen == pnode->sysctl_csize) {
1039 error = sysctl_realloc(pnode);
1040 if (error)
1041 return (error);
1042 node = pnode->sysctl_child;
1043 }
1044
1045 /*
1046 * insert new node data
1047 */
1048 if (at < pnode->sysctl_clen) {
1049 int t;
1050
1051 /*
1052 * move the nodes that should come after the new one
1053 */
1054 memmove(&node[at + 1], &node[at],
1055 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
1056 memset(&node[at], 0, sizeof(struct sysctlnode));
1057 node[at].sysctl_parent = pnode;
1058 /*
1059 * and...reparent any children of any moved nodes
1060 */
1061 for (ni = at; ni <= pnode->sysctl_clen; ni++)
1062 if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
1063 for (t = 0; t < node[ni].sysctl_clen; t++)
1064 node[ni].sysctl_child[t].sysctl_parent =
1065 &node[ni];
1066 }
1067 node = &node[at];
1068 pnode->sysctl_clen++;
1069
1070 strlcpy(node->sysctl_name, nnode.sysctl_name,
1071 sizeof(node->sysctl_name));
1072 node->sysctl_num = nm;
1073 node->sysctl_size = sz;
1074 node->sysctl_flags = type|flags;
1075 node->sysctl_csize = 0;
1076 node->sysctl_clen = 0;
1077 if (own) {
1078 node->sysctl_data = own;
1079 node->sysctl_flags |= SYSCTL_OWNDATA;
1080 }
1081 else if (flags & SYSCTL_ALIAS) {
1082 node->sysctl_alias = anum;
1083 }
1084 else if (flags & SYSCTL_IMMEDIATE) {
1085 switch (type) {
1086 case CTLTYPE_INT:
1087 node->sysctl_idata = nnode.sysctl_idata;
1088 break;
1089 case CTLTYPE_QUAD:
1090 node->sysctl_qdata = nnode.sysctl_qdata;
1091 break;
1092 }
1093 }
1094 else {
1095 node->sysctl_data = nnode.sysctl_data;
1096 node->sysctl_flags &= ~SYSCTL_OWNDATA;
1097 }
1098 node->sysctl_func = nnode.sysctl_func;
1099 node->sysctl_child = NULL;
1100 /* node->sysctl_parent should already be done */
1101
1102 /*
1103 * update "version" on path to "root"
1104 */
1105 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1106 ;
1107 pnode = node;
1108 for (nm = rnode->sysctl_ver + 1; pnode != NULL;
1109 pnode = pnode->sysctl_parent)
1110 pnode->sysctl_ver = nm;
1111
1112 if (oldp != NULL)
1113 error = sysctl_copyout(l, node, oldp,
1114 MIN(*oldlenp, sizeof(struct sysctlnode)));
1115 *oldlenp = sizeof(struct sysctlnode);
1116
1117 return (error);
1118 }
1119
1120 /*
1121 * ********************************************************************
1122 * A wrapper around sysctl_create() that prints the thing we're trying
1123 * to add.
1124 * ********************************************************************
1125 */
1126 #ifdef SYSCTL_DEBUG_CREATE
1127 int _sysctl_create(SYSCTLFN_RWPROTO);
1128 int
1129 _sysctl_create(SYSCTLFN_RWARGS)
1130 {
1131 const struct sysctlnode *node;
1132 int k, rc, ni, nl = namelen + (name - oname);
1133
1134 node = newp;
1135
1136 printf("namelen %d (", nl);
1137 for (ni = 0; ni < nl - 1; ni++)
1138 printf(" %d", oname[ni]);
1139 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1140 k = node->sysctl_num,
1141 node->sysctl_name,
1142 node->sysctl_flags,
1143 SYSCTL_FLAGS(node->sysctl_flags),
1144 SYSCTL_TYPE(node->sysctl_flags),
1145 node->sysctl_size);
1146
1147 node = rnode;
1148 rc = sysctl_create(SYSCTLFN_CALL(rnode));
1149
1150 printf("sysctl_create(");
1151 for (ni = 0; ni < nl - 1; ni++)
1152 printf(" %d", oname[ni]);
1153 printf(" %d ) returned %d\n", k, rc);
1154
1155 return (rc);
1156 }
1157 #define sysctl_create _sysctl_create
1158 #endif /* SYSCTL_DEBUG_CREATE */
1159
1160 /*
1161 * sysctl_destroy -- Removes a node (as described by newp) from the
1162 * given tree, returning (if successful) a copy of the dead node in
1163 * oldp. Since we're removing stuff, there's not much to check.
1164 */
1165 int
1166 sysctl_destroy(SYSCTLFN_RWARGS)
1167 {
1168 struct sysctlnode *node, *pnode, onode, nnode;
1169 int ni, error;
1170
1171 error = 0;
1172
1173 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
1174 return (EINVAL);
1175
1176 /*
1177 * processes can only destroy nodes at securelevel 0, must be
1178 * root, and can't remove nodes from a parent that's not
1179 * writeable
1180 */
1181 if (l != NULL) {
1182 if (securelevel > 0)
1183 return (EPERM);
1184 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
1185 if (error)
1186 return (error);
1187 if (!(rnode->sysctl_flags & SYSCTL_READWRITE))
1188 return (EPERM);
1189 }
1190
1191 /*
1192 * nothing can remove a node if:
1193 * the node is permanent (checked later) or
1194 * the tree itself is not writeable or
1195 * the entire sysctl system is not writeable
1196 */
1197 if (!(sysctl_rootof(rnode)->sysctl_flags & SYSCTL_READWRITE) ||
1198 !(sysctl_root.sysctl_flags & SYSCTL_READWRITE))
1199 return (EPERM);
1200
1201 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1202 return (EINVAL);
1203 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
1204 if (error)
1205 return (error);
1206 memset(&onode, 0, sizeof(struct sysctlnode));
1207
1208 node = rnode->sysctl_child;
1209 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
1210 if (nnode.sysctl_num == node[ni].sysctl_num) {
1211 /*
1212 * if name specified, must match
1213 */
1214 if (nnode.sysctl_name[0] != '\0' &&
1215 strcmp(nnode.sysctl_name, node[ni].sysctl_name))
1216 continue;
1217 /*
1218 * if version specified, must match
1219 */
1220 if (nnode.sysctl_ver != 0 &&
1221 nnode.sysctl_ver != node[ni].sysctl_ver)
1222 continue;
1223 /*
1224 * this must be the one
1225 */
1226 break;
1227 }
1228 }
1229 if (ni == rnode->sysctl_clen)
1230 return (ENOENT);
1231 node = &node[ni];
1232 pnode = node->sysctl_parent;
1233
1234 /*
1235 * if the kernel says permanent, it is, so there. nyah.
1236 */
1237 if (SYSCTL_FLAGS(node->sysctl_flags) & SYSCTL_PERMANENT)
1238 return (EPERM);
1239
1240 /*
1241 * can't delete non-empty nodes
1242 */
1243 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
1244 node->sysctl_clen != 0)
1245 return (ENOTEMPTY);
1246
1247 /*
1248 * if the node "owns" data, release it now
1249 */
1250 if (node->sysctl_flags & SYSCTL_OWNDATA) {
1251 if (node->sysctl_data != NULL)
1252 FREE(node->sysctl_data, M_SYSCTLDATA);
1253 node->sysctl_data = NULL;
1254 }
1255
1256 /*
1257 * if the node to be removed is not the last one on the list,
1258 * move the remaining nodes up, and reparent any grandchildren
1259 */
1260 onode = *node;
1261 if (ni < pnode->sysctl_clen - 1) {
1262 int t;
1263
1264 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
1265 (pnode->sysctl_clen - ni - 1) *
1266 sizeof(struct sysctlnode));
1267 for (; ni < pnode->sysctl_clen - 1; ni++)
1268 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
1269 CTLTYPE_NODE)
1270 for (t = 0; t < pnode->sysctl_child[ni].sysctl_clen;
1271 t++)
1272 pnode->sysctl_child[ni].sysctl_child[t].
1273 sysctl_parent =
1274 &pnode->sysctl_child[ni];
1275 ni = pnode->sysctl_clen - 1;
1276 node = &pnode->sysctl_child[ni];
1277 }
1278
1279 /*
1280 * reset the space we just vacated
1281 */
1282 memset(node, 0, sizeof(struct sysctlnode));
1283 node->sysctl_parent = pnode;
1284 pnode->sysctl_clen--;
1285
1286 /*
1287 * if this parent just lost its last child, nuke the creche
1288 */
1289 if (pnode->sysctl_clen == 0) {
1290 FREE(pnode->sysctl_child, M_SYSCTLNODE);
1291 pnode->sysctl_csize = 0;
1292 pnode->sysctl_child = NULL;
1293 }
1294
1295 /*
1296 * update "version" on path to "root"
1297 */
1298 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1299 ;
1300 for (ni = rnode->sysctl_ver + 1; pnode != NULL;
1301 pnode = pnode->sysctl_parent)
1302 pnode->sysctl_ver = ni;
1303
1304 if (oldp != NULL)
1305 error = sysctl_copyout(l, &onode, oldp,
1306 MIN(*oldlenp, sizeof(struct sysctlnode)));
1307 *oldlenp = sizeof(struct sysctlnode);
1308
1309 return (error);
1310 }
1311
1312 /*
1313 * sysctl_lookup -- Handles copyin/copyout of new and old values.
1314 * Partial reads are globally allowed. Only root can write to things
1315 * unless the node says otherwise.
1316 */
1317 int
1318 sysctl_lookup(SYSCTLFN_RWARGS)
1319 {
1320 struct proc *p = l->l_proc;
1321 int error, rw;
1322 size_t sz, len;
1323 void *d;
1324
1325 error = 0;
1326
1327 /*
1328 * you can't "look up" a node. you can "query" it, but you
1329 * can't "look it up".
1330 */
1331 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
1332 return (EINVAL);
1333
1334 /*
1335 * some nodes are private, so only root can look into them.
1336 */
1337 if ((rnode->sysctl_flags & SYSCTL_PRIVATE) &&
1338 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
1339 return (error);
1340
1341 /*
1342 * if a node wants to be writable according to different rules
1343 * other than "only root can write to stuff unless a flag is
1344 * set", then it needs its own function which should have been
1345 * called and not us.
1346 */
1347 if (l != NULL && newp != NULL &&
1348 !(rnode->sysctl_flags & SYSCTL_ANYWRITE) &&
1349 (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1350 return (error);
1351
1352 /*
1353 * is this node supposedly writable?
1354 */
1355 rw = 0;
1356 switch (rnode->sysctl_flags & SYSCTL_READWRITE) {
1357 case SYSCTL_READONLY1:
1358 rw = (securelevel < 1) ? 1 : 0;
1359 break;
1360 case SYSCTL_READONLY2:
1361 rw = (securelevel < 2) ? 1 : 0;
1362 break;
1363 case SYSCTL_READWRITE:
1364 rw = 1;
1365 break;
1366 }
1367
1368 /*
1369 * it appears not to be writable at this time, so if someone
1370 * tried to write to it, we must tell them to go away
1371 */
1372 if (!rw && newp != NULL)
1373 return (EPERM);
1374
1375 /*
1376 * step one, copy out the stuff we have presently
1377 */
1378 if (rnode->sysctl_flags & SYSCTL_IMMEDIATE) {
1379 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1380 case CTLTYPE_INT:
1381 d = &rnode->sysctl_idata;
1382 break;
1383 case CTLTYPE_QUAD:
1384 d = &rnode->sysctl_qdata;
1385 break;
1386 default:
1387 return (EINVAL);
1388 }
1389 }
1390 else
1391 d = rnode->sysctl_data;
1392 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
1393 sz = strlen(d) + 1;
1394 else
1395 sz = rnode->sysctl_size;
1396 if (oldp != NULL)
1397 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
1398 if (error)
1399 return (error);
1400 *oldlenp = sz;
1401
1402 /*
1403 * are we done?
1404 */
1405 if (newp == NULL || newlen == 0)
1406 return (0);
1407
1408 /*
1409 * hmm...not done. must now "copy in" new value. re-adjust
1410 * sz to maximum value (strings are "weird").
1411 */
1412 sz = rnode->sysctl_size;
1413 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1414 case CTLTYPE_INT:
1415 case CTLTYPE_QUAD:
1416 case CTLTYPE_STRUCT:
1417 /*
1418 * these data must be *exactly* the same size coming
1419 * in.
1420 */
1421 if (newlen != sz)
1422 return (EINVAL);
1423 error = sysctl_copyin(l, newp, d, sz);
1424 break;
1425 case CTLTYPE_STRING: {
1426 /*
1427 * strings, on the other hand, can be shorter, and we
1428 * let userland be sloppy about the trailing nul.
1429 */
1430 char *newbuf;
1431
1432 /*
1433 * too much new string?
1434 */
1435 if (newlen > sz)
1436 return (EINVAL);
1437
1438 /*
1439 * temporary copy of new inbound string
1440 */
1441 len = MIN(sz, newlen);
1442 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1443 if (newbuf == NULL)
1444 return (ENOMEM);
1445 error = sysctl_copyin(l, newp, newbuf, len);
1446 if (error) {
1447 FREE(newbuf, M_SYSCTLDATA);
1448 return (error);
1449 }
1450
1451 /*
1452 * did they null terminate it, or do we have space
1453 * left to do it ourselves?
1454 */
1455 if (newbuf[len - 1] != '\0' && len == sz) {
1456 FREE(newbuf, M_SYSCTLDATA);
1457 return (EINVAL);
1458 }
1459
1460 /*
1461 * looks good, so pop it into place and zero the rest.
1462 */
1463 if (len > 0)
1464 memcpy(rnode->sysctl_data, newbuf, len);
1465 if (sz != len)
1466 memset((char*)rnode->sysctl_data + len, 0, sz - len);
1467 FREE(newbuf, M_SYSCTLDATA);
1468 break;
1469 }
1470 default:
1471 return (EINVAL);
1472 }
1473
1474 return (error);
1475 }
1476
1477 /*
1478 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1479 * purport to handle it. This interface isn't fully fleshed out yet,
1480 * unfortunately.
1481 */
1482 static int
1483 sysctl_mmap(SYSCTLFN_RWARGS)
1484 {
1485 struct sysctlnode nnode, *node;
1486 int error;
1487
1488 /*
1489 * let's just pretend that didn't happen, m'kay?
1490 */
1491 if (l == NULL)
1492 return (EPERM);
1493
1494 /*
1495 * is this a sysctlnode description of an mmap request?
1496 */
1497 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1498 return (EINVAL);
1499 error = sysctl_copyin(l, newp, &nnode, sizeof(struct sysctlnode));
1500 if (error)
1501 return (error);
1502
1503 /*
1504 * does the node they asked for exist?
1505 */
1506 if (namelen != 1)
1507 return (EOPNOTSUPP);
1508 node = rnode;
1509 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1510 if (error)
1511 return (error);
1512
1513 /*
1514 * does this node that we have found purport to handle mmap?
1515 */
1516 if (node->sysctl_func == NULL ||
1517 !(node->sysctl_flags & SYSCTL_MMAP))
1518 return (EOPNOTSUPP);
1519
1520 /*
1521 * well...okay, they asked for it.
1522 */
1523 return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1524 }
1525
1526 /*
1527 * ********************************************************************
1528 * Section 3: Create and destroy from inside the kernel
1529 * ********************************************************************
1530 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1531 * interfaces for the kernel to fling new entries into the mib and rip
1532 * them out later. In the case of sysctl_createv(), the returned copy
1533 * of the node (see sysctl_create()) will be translated back into a
1534 * pointer to the actual node.
1535 *
1536 * Note that sysctl_createv() will return 0 if the create request
1537 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1538 * will return 0 if the node to be destroyed already does not exist
1539 * (aka rm -f) or if it is a parent of other nodes.
1540 *
1541 * This allows two (or more) different subsystems to assert sub-tree
1542 * existence before populating their own nodes, and to remove their
1543 * own nodes without orphaning the others when they are done.
1544 * ********************************************************************
1545 */
1546 int
1547 sysctl_createv(int flags, int type,
1548 const char *namep, struct sysctlnode **rnode,
1549 sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1550 ...)
1551 {
1552 va_list ap;
1553 int error, ni, namelen, name[CTL_MAXNAME];
1554 struct sysctlnode *pnode, nnode, onode;
1555 size_t sz;
1556
1557 /*
1558 * what is it?
1559 */
1560 flags = SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
1561
1562 /*
1563 * where do we put it?
1564 */
1565 va_start(ap, newlen);
1566 namelen = 0;
1567 ni = -1;
1568 do {
1569 if (++ni == CTL_MAXNAME)
1570 return (ENAMETOOLONG);
1571 name[ni] = va_arg(ap, int);
1572 /*
1573 * sorry, this is not supported from here
1574 */
1575 if (name[ni] == CTL_CREATESYM)
1576 return (EINVAL);
1577 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
1578 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
1579 va_end(ap);
1580
1581 /*
1582 * what's it called
1583 */
1584 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >
1585 sizeof(nnode.sysctl_name))
1586 return (ENAMETOOLONG);
1587
1588 /*
1589 * cons up the description of the new node
1590 */
1591 nnode.sysctl_num = name[namelen - 1];
1592 name[namelen - 1] = CTL_CREATE;
1593 nnode.sysctl_size = newlen;
1594 nnode.sysctl_flags = flags;
1595 if (type == CTLTYPE_NODE) {
1596 nnode.sysctl_csize = 0;
1597 nnode.sysctl_clen = 0;
1598 nnode.sysctl_child = NULL;
1599 if (flags & SYSCTL_ALIAS)
1600 nnode.sysctl_alias = qv;
1601 }
1602 else if (flags & SYSCTL_IMMEDIATE) {
1603 switch (type) {
1604 case CTLTYPE_INT:
1605 nnode.sysctl_idata = qv;
1606 break;
1607 case CTLTYPE_QUAD:
1608 nnode.sysctl_qdata = qv;
1609 break;
1610 default:
1611 return (EINVAL);
1612 }
1613 }
1614 else {
1615 nnode.sysctl_data = newp;
1616 }
1617 nnode.sysctl_func = func;
1618 nnode.sysctl_parent = NULL;
1619 nnode.sysctl_ver = 0;
1620
1621 /*
1622 * initialize lock state -- we need locks if the main tree has
1623 * been marked as complete, but since we could be called from
1624 * either there, or from a device driver (say, at device
1625 * insertion), or from an lkm (at lkm load time, say), we
1626 * don't really want to "wait"...
1627 */
1628 error = sysctl_lock(NULL, NULL, 0);
1629 if (error)
1630 return (error);
1631
1632 /*
1633 * locate the prospective parent of the new node, and if we
1634 * find it, add the new node.
1635 */
1636 sz = sizeof(onode);
1637 pnode = (rnode != NULL) ? *rnode : NULL;
1638 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1639 if (error == 0)
1640 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
1641 &nnode, sizeof(nnode), &name[0], NULL,
1642 pnode);
1643
1644 /*
1645 * unfortunately the node we wanted to create is already
1646 * there. if the node that's already there is a reasonable
1647 * facsimile of the node we wanted to create, just pretend
1648 * (for the caller's benefit) that we managed to create the
1649 * node they wanted.
1650 */
1651 if (error == EEXIST) {
1652 /* name is the same as requested... */
1653 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
1654 /* they want the same function... */
1655 nnode.sysctl_func == onode.sysctl_func &&
1656 /* number is the same as requested, or... */
1657 (nnode.sysctl_num == onode.sysctl_num ||
1658 /* they didn't pick a number... */
1659 nnode.sysctl_num == CTL_CREATE)) {
1660 /*
1661 * collision here from trying to create
1662 * something that already existed; let's give
1663 * our customers a hand and tell them they got
1664 * what they wanted.
1665 */
1666 #ifdef SYSCTL_DEBUG_CREATE
1667 printf("cleared\n");
1668 #endif /* SYSCTL_DEBUG_CREATE */
1669 error = 0;
1670 }
1671 }
1672
1673 /*
1674 * if they want to know where the new node is, go find the
1675 * address of the actual node, not the copy that
1676 * sysctl_create() gave us.
1677 */
1678 if (rnode != NULL && error == 0) {
1679 /*
1680 * sysctl_create() gave us back a copy of the node,
1681 * but we need to know where it actually is...
1682 */
1683 name[namelen - 1] = onode.sysctl_num;
1684 pnode = *rnode;
1685 error = sysctl_locate(NULL, &name[0], namelen, &pnode, &ni);
1686 /*
1687 * not expecting an error here, but...
1688 */
1689 if (error == 0)
1690 *rnode = pnode;
1691 }
1692
1693 /*
1694 * now it should be safe to release the lock state.
1695 */
1696 sysctl_unlock(NULL);
1697
1698 if (error != 0) {
1699 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
1700 nnode.sysctl_name, error);
1701 #if 0
1702 if (error != ENOENT)
1703 sysctl_dump(&onode);
1704 #endif
1705 }
1706
1707 return (error);
1708 }
1709
1710 int
1711 sysctl_destroyv(struct sysctlnode *rnode, ...)
1712 {
1713 va_list ap;
1714 int error, name[CTL_MAXNAME], namelen, ni;
1715 struct sysctlnode *pnode, *node;
1716
1717 va_start(ap, rnode);
1718 namelen = 0;
1719 ni = 0;
1720 do {
1721 if (ni == CTL_MAXNAME)
1722 return (ENAMETOOLONG);
1723 name[ni] = va_arg(ap, int);
1724 } while (name[ni++] != CTL_EOL);
1725 namelen = ni - 1;
1726 va_end(ap);
1727
1728 /*
1729 * i can't imagine why we'd be destroying a node when the tree
1730 * wasn't complete, but who knows?
1731 */
1732 error = sysctl_lock(NULL, NULL, 0);
1733 if (error)
1734 return (error);
1735
1736 /*
1737 * where is it?
1738 */
1739 node = rnode;
1740 error = sysctl_locate(NULL, &name[0], namelen, &node, &ni);
1741 if (error) {
1742 /* they want it gone and it's not there, so... */
1743 sysctl_unlock(NULL);
1744 return (error == ENOENT ? 0 : error);
1745 }
1746
1747 /*
1748 * we found it, now let's nuke it
1749 */
1750 name[namelen - 1] = CTL_DESTROY;
1751 pnode = node->sysctl_parent;
1752 error = sysctl_destroy(&name[namelen - 1], 1, NULL, NULL,
1753 node, sizeof(*node), &name[0], NULL,
1754 pnode);
1755 if (error == ENOTEMPTY)
1756 /*
1757 * think of trying to delete "foo" when "foo.bar"
1758 * (which someone else put there) is still in
1759 * existence
1760 */
1761 error = 0;
1762
1763 sysctl_unlock(NULL);
1764
1765 return (error);
1766 }
1767
1768 #if 0
1769 /*
1770 * ********************************************************************
1771 * the dump routine. i haven't yet decided how (if at all) i'll call
1772 * this from userland when it's in the kernel.
1773 * ********************************************************************
1774 */
1775 static const char *
1776 sf(int f)
1777 {
1778 static char s[256];
1779 char *c;
1780
1781 s[0] = '\0';
1782 c = "";
1783
1784 #define print_flag(_f, _s, _c, _q, _x) \
1785 if (((_x) && (((_f) & (_x)) == (__CONCAT(SYSCTL_,_q)))) || \
1786 (!(_x) && ((_f) & (__CONCAT(SYSCTL_,_q))))) { \
1787 strlcat((_s), (_c), sizeof(_s)); \
1788 strlcat((_s), __STRING(_q), sizeof(_s)); \
1789 (_c) = ","; \
1790 (_f) &= ~(__CONCAT(SYSCTL_,_q)|(_x)); \
1791 }
1792 print_flag(f, s, c, READONLY, SYSCTL_READWRITE);
1793 print_flag(f, s, c, READONLY1, SYSCTL_READWRITE);
1794 print_flag(f, s, c, READONLY2, SYSCTL_READWRITE);
1795 print_flag(f, s, c, READWRITE, SYSCTL_READWRITE);
1796 print_flag(f, s, c, ANYWRITE, 0);
1797 print_flag(f, s, c, PRIVATE, 0);
1798 print_flag(f, s, c, PERMANENT, 0);
1799 print_flag(f, s, c, OWNDATA, 0);
1800 print_flag(f, s, c, IMMEDIATE, 0);
1801 print_flag(f, s, c, HEX, 0);
1802 print_flag(f, s, c, ROOT, 0);
1803 print_flag(f, s, c, ANYNUMBER, 0);
1804 print_flag(f, s, c, HIDDEN, 0);
1805 print_flag(f, s, c, ALIAS, 0);
1806 #undef print_flag
1807
1808 if (f) {
1809 char foo[9];
1810 snprintf(foo, sizeof(foo), "%x", f);
1811 strlcat(s, c, sizeof(s));
1812 strlcat(s, foo, sizeof(s));
1813 }
1814
1815 return (s);
1816 }
1817
1818 static const char *
1819 st(int t)
1820 {
1821
1822 switch (t) {
1823 case CTLTYPE_NODE:
1824 return "NODE";
1825 case CTLTYPE_INT:
1826 return "INT";
1827 case CTLTYPE_STRING:
1828 return "STRING";
1829 case CTLTYPE_QUAD:
1830 return "QUAD";
1831 case CTLTYPE_STRUCT:
1832 return "STRUCT";
1833 }
1834
1835 return "???";
1836 }
1837
1838 void
1839 sysctl_dump(const struct sysctlnode *d)
1840 {
1841 static char nmib[64], smib[256];
1842 static int indent;
1843 struct sysctlnode *n;
1844 char *np, *sp, tmp[20];
1845 int i;
1846
1847 if (d == NULL)
1848 return;
1849
1850 np = &nmib[strlen(nmib)];
1851 sp = &smib[strlen(smib)];
1852
1853 if (!(d->sysctl_flags & SYSCTL_ROOT)) {
1854 snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
1855 strcat(nmib, ".");
1856 strcat(smib, ".");
1857 strcat(nmib, tmp);
1858 strcat(smib, d->sysctl_name);
1859 printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
1860 SYSCTL_TYPE(d->sysctl_flags));
1861 }
1862
1863 if (1) {
1864 printf("%*s%p:\tsysctl_name [%s]\n", indent, "",
1865 d, d->sysctl_name);
1866 printf("%*s\t\tsysctl_num %d\n", indent, "",
1867 d->sysctl_num);
1868 printf("%*s\t\tsysctl_flags %x (flags=%x<%s> type=%d<%s> "
1869 "size=%zu)\n",
1870 indent, "", d->sysctl_flags,
1871 SYSCTL_FLAGS(d->sysctl_flags),
1872 sf(SYSCTL_FLAGS(d->sysctl_flags)),
1873 SYSCTL_TYPE(d->sysctl_flags),
1874 st(SYSCTL_TYPE(d->sysctl_flags)),
1875 d->sysctl_size);
1876 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
1877 printf("%*s\t\tsysctl_csize %d\n", indent, "",
1878 d->sysctl_csize);
1879 printf("%*s\t\tsysctl_clen %d\n", indent, "",
1880 d->sysctl_clen);
1881 printf("%*s\t\tsysctl_child %p\n", indent, "",
1882 d->sysctl_child);
1883 }
1884 else
1885 printf("%*s\t\tsysctl_data %p\n", indent, "",
1886 d->sysctl_data);
1887 printf("%*s\t\tsysctl_func %p\n", indent, "",
1888 d->sysctl_func);
1889 printf("%*s\t\tsysctl_parent %p\n", indent, "",
1890 d->sysctl_parent);
1891 printf("%*s\t\tsysctl_ver %d\n", indent, "",
1892 d->sysctl_ver);
1893 }
1894
1895 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
1896 indent += 8;
1897 n = d->sysctl_child;
1898 for (i = 0; i < d->sysctl_clen; i++) {
1899 sysctl_dump(&n[i]);
1900 }
1901 indent -= 8;
1902 }
1903
1904 np[0] = '\0';
1905 sp[0] = '\0';
1906 }
1907 #endif /* 0 */
1908
1909 /*
1910 * ********************************************************************
1911 * Deletes an entire n-ary tree. Not recommended unless you know why
1912 * you're doing it. Personally, I don't know why you'd even think
1913 * about it.
1914 * ********************************************************************
1915 */
1916 void
1917 sysctl_free(struct sysctlnode *rnode)
1918 {
1919 struct sysctlnode *node, *pnode;
1920
1921 if (rnode == NULL)
1922 rnode = &sysctl_root;
1923 pnode = rnode;
1924
1925 node = pnode->sysctl_child;
1926 do {
1927 while (node != NULL && pnode->sysctl_csize > 0) {
1928 while (node <
1929 &pnode->sysctl_child[pnode->sysctl_clen] &&
1930 (SYSCTL_TYPE(node->sysctl_flags) !=
1931 CTLTYPE_NODE ||
1932 node->sysctl_csize == 0)) {
1933 if (SYSCTL_FLAGS(node->sysctl_flags) &
1934 SYSCTL_OWNDATA) {
1935 if (node->sysctl_data != NULL) {
1936 FREE(node->sysctl_data,
1937 M_SYSCTLDATA);
1938 node->sysctl_data = NULL;
1939 }
1940 }
1941 node++;
1942 }
1943 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
1944 pnode = node;
1945 node = node->sysctl_child;
1946 }
1947 else
1948 break;
1949 }
1950 if (pnode->sysctl_child != NULL)
1951 FREE(pnode->sysctl_child, M_SYSCTLNODE);
1952 pnode->sysctl_clen = 0;
1953 pnode->sysctl_csize = 0;
1954 pnode->sysctl_child = NULL;
1955 node = pnode;
1956 pnode = node->sysctl_parent;
1957 } while (pnode != NULL && pnode != rnode);
1958 }
1959
1960 /*
1961 * ********************************************************************
1962 * old_sysctl -- A routine to bridge old-style internal calls to the
1963 * new infrastructure.
1964 * ********************************************************************
1965 */
1966 int
1967 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1968 void *newp, size_t newlen, struct lwp *l)
1969 {
1970 int error;
1971 size_t savelen = *oldlenp;
1972
1973 error = sysctl_lock(l, oldp, savelen);
1974 if (error)
1975 return (error);
1976 error = sysctl_dispatch(name, namelen, oldp, oldlenp,
1977 newp, newlen, name, l, NULL);
1978 sysctl_unlock(l);
1979 if (error == 0 && oldp != NULL && savelen < *oldlenp)
1980 error = ENOMEM;
1981
1982 return (error);
1983 }
1984
1985 /*
1986 * ********************************************************************
1987 * Section 4: Generic helper routines
1988 * ********************************************************************
1989 * "helper" routines that can do more finely grained access control,
1990 * construct structures from disparate information, create the
1991 * appearance of more nodes and sub-trees, etc. for example, if
1992 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
1993 * with a dynamically created list of nodes that represented the
1994 * currently running processes at that instant.
1995 * ********************************************************************
1996 */
1997
1998 /*
1999 * first, a few generic helpers that provide:
2000 *
2001 * sysctl_needfunc() a readonly interface that emits a warning
2002 * sysctl_notavail() returns EOPNOTSUPP (generic error)
2003 * sysctl_null() an empty return buffer with no error
2004 */
2005 int
2006 sysctl_needfunc(SYSCTLFN_ARGS)
2007 {
2008 int error;
2009
2010 printf("!!SYSCTL_NEEDFUNC!!\n");
2011
2012 if (newp != NULL || namelen != 0)
2013 return (EOPNOTSUPP);
2014
2015 error = 0;
2016 if (oldp != NULL)
2017 error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2018 MIN(rnode->sysctl_size, *oldlenp));
2019 *oldlenp = rnode->sysctl_size;
2020
2021 return (error);
2022 }
2023
2024 int
2025 sysctl_notavail(SYSCTLFN_ARGS)
2026 {
2027
2028 if (namelen == 1 && name[0] == CTL_QUERY)
2029 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2030
2031 return (EOPNOTSUPP);
2032 }
2033
2034 int
2035 sysctl_null(SYSCTLFN_ARGS)
2036 {
2037
2038 *oldlenp = 0;
2039
2040 return (0);
2041 }
2042
2043 /*
2044 * ********************************************************************
2045 * Section 5: The machinery that makes it all go
2046 * ********************************************************************
2047 * Memory "manglement" routines. Not much to this, eh?
2048 * ********************************************************************
2049 */
2050 static int
2051 sysctl_alloc(struct sysctlnode *p, int x)
2052 {
2053 int i;
2054 struct sysctlnode *n;
2055
2056 assert(p->sysctl_child == NULL);
2057
2058 if (x == 1)
2059 MALLOC(n, struct sysctlnode *,
2060 sizeof(struct sysctlnode),
2061 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2062 else
2063 MALLOC(n, struct sysctlnode *,
2064 SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2065 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2066 if (n == NULL)
2067 return (ENOMEM);
2068
2069 if (x == 1) {
2070 memset(n, 0, sizeof(struct sysctlnode));
2071 p->sysctl_csize = 1;
2072 }
2073 else {
2074 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2075 p->sysctl_csize = SYSCTL_DEFSIZE;
2076 }
2077 p->sysctl_clen = 0;
2078
2079 for (i = 0; i < p->sysctl_csize; i++)
2080 n[i].sysctl_parent = p;
2081
2082 p->sysctl_child = n;
2083 return (0);
2084 }
2085
2086 static int
2087 sysctl_realloc(struct sysctlnode *p)
2088 {
2089 int i, j;
2090 struct sysctlnode *n;
2091
2092 assert(p->sysctl_csize == p->sysctl_clen);
2093
2094 /*
2095 * how many do we have...how many should we make?
2096 */
2097 i = p->sysctl_clen;
2098 n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
2099 M_WAITOK|M_CANFAIL);
2100 if (n == NULL)
2101 return (ENOMEM);
2102
2103 /*
2104 * move old children over...initialize new children
2105 */
2106 memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
2107 memset(&n[i], 0, i * sizeof(struct sysctlnode));
2108 p->sysctl_csize = 2 * i;
2109 p->sysctl_clen = i;
2110
2111 /*
2112 * reattach moved (and new) children to parent; if a moved
2113 * child node has children, reattach the parent pointers of
2114 * grandchildren
2115 */
2116 for (i = 0; i < p->sysctl_csize; i++) {
2117 n[i].sysctl_parent = p;
2118 if (n[i].sysctl_child != NULL) {
2119 for (j = 0; j < n[i].sysctl_csize; j++)
2120 n[i].sysctl_child[j].sysctl_parent = &n[i];
2121 }
2122 }
2123
2124 /*
2125 * get out with the old and in with the new
2126 */
2127 FREE(p->sysctl_child, M_SYSCTLNODE);
2128 p->sysctl_child = n;
2129
2130 return (0);
2131 }
2132