1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_capsicum.h"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44
45 #include <sys/param.h>
46 #include <sys/fail.h>
47 #include <sys/systm.h>
48 #include <sys/capability.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/jail.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/sbuf.h>
58 #include <sys/sx.h>
59 #include <sys/sysproto.h>
60 #include <sys/uio.h>
61 #ifdef KTRACE
62 #include <sys/ktrace.h>
63 #endif
64
65 #include <net/vnet.h>
66
67 #include <security/mac/mac_framework.h>
68
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71
72 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
73 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
74 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
75
76 /*
77 * The sysctllock protects the MIB tree. It also protects sysctl
78 * contexts used with dynamic sysctls. The sysctl_register_oid() and
79 * sysctl_unregister_oid() routines require the sysctllock to already
80 * be held, so the sysctl_lock() and sysctl_unlock() routines are
81 * provided for the few places in the kernel which need to use that
82 * API rather than using the dynamic API. Use of the dynamic API is
83 * strongly encouraged for most code.
84 *
85 * The sysctlmemlock is used to limit the amount of user memory wired for
86 * sysctl requests. This is implemented by serializing any userland
87 * sysctl requests larger than a single page via an exclusive lock.
88 */
89 static struct sx sysctllock;
90 static struct sx sysctlmemlock;
91
92 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
93 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
94 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
95 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
96 #define SYSCTL_SLEEP(ch, wmesg, timo) \
97 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
98
99 static int sysctl_root(SYSCTL_HANDLER_ARGS);
100
101 struct sysctl_oid_list sysctl__children; /* root list */
102
103 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
104 int recurse);
105
106 static struct sysctl_oid *
107 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
108 {
109 struct sysctl_oid *oidp;
110
111 SYSCTL_ASSERT_XLOCKED();
112 SLIST_FOREACH(oidp, list, oid_link) {
113 if (strcmp(oidp->oid_name, name) == 0) {
114 return (oidp);
115 }
116 }
117 return (NULL);
118 }
119
120 /*
121 * Initialization of the MIB tree.
122 *
123 * Order by number in each list.
124 */
125 void
126 sysctl_lock(void)
127 {
128
129 SYSCTL_XLOCK();
130 }
131
132 void
133 sysctl_unlock(void)
134 {
135
136 SYSCTL_XUNLOCK();
137 }
138
139 void
140 sysctl_register_oid(struct sysctl_oid *oidp)
141 {
142 struct sysctl_oid_list *parent = oidp->oid_parent;
143 struct sysctl_oid *p;
144 struct sysctl_oid *q;
145 int oid_number;
146 int timeout = 2;
147
148 /*
149 * First check if another oid with the same name already
150 * exists in the parent's list.
151 */
152 SYSCTL_ASSERT_XLOCKED();
153 p = sysctl_find_oidname(oidp->oid_name, parent);
154 if (p != NULL) {
155 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
156 p->oid_refcnt++;
157 return;
158 } else {
159 printf("can't re-use a leaf (%s)!\n", p->oid_name);
160 return;
161 }
162 }
163 /* get current OID number */
164 oid_number = oidp->oid_number;
165
166 #if (OID_AUTO >= 0)
167 #error "OID_AUTO is expected to be a negative value"
168 #endif
169 /*
170 * Any negative OID number qualifies as OID_AUTO. Valid OID
171 * numbers should always be positive.
172 *
173 * NOTE: DO NOT change the starting value here, change it in
174 * <sys/sysctl.h>, and make sure it is at least 256 to
175 * accomodate e.g. net.inet.raw as a static sysctl node.
176 */
177 if (oid_number < 0) {
178 static int newoid;
179
180 /*
181 * By decrementing the next OID number we spend less
182 * time inserting the OIDs into a sorted list.
183 */
184 if (--newoid < CTL_AUTO_START)
185 newoid = 0x7fffffff;
186
187 oid_number = newoid;
188 }
189
190 /*
191 * Insert the OID into the parent's list sorted by OID number.
192 */
193 retry:
194 q = NULL;
195 SLIST_FOREACH(p, parent, oid_link) {
196 /* check if the current OID number is in use */
197 if (oid_number == p->oid_number) {
198 /* get the next valid OID number */
199 if (oid_number < CTL_AUTO_START ||
200 oid_number == 0x7fffffff) {
201 /* wraparound - restart */
202 oid_number = CTL_AUTO_START;
203 /* don't loop forever */
204 if (!timeout--)
205 panic("sysctl: Out of OID numbers\n");
206 goto retry;
207 } else {
208 oid_number++;
209 }
210 } else if (oid_number < p->oid_number)
211 break;
212 q = p;
213 }
214 /* check for non-auto OID number collision */
215 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
216 oid_number >= CTL_AUTO_START) {
217 printf("sysctl: OID number(%d) is already in use for '%s'\n",
218 oidp->oid_number, oidp->oid_name);
219 }
220 /* update the OID number, if any */
221 oidp->oid_number = oid_number;
222 if (q != NULL)
223 SLIST_INSERT_AFTER(q, oidp, oid_link);
224 else
225 SLIST_INSERT_HEAD(parent, oidp, oid_link);
226 }
227
228 void
229 sysctl_unregister_oid(struct sysctl_oid *oidp)
230 {
231 struct sysctl_oid *p;
232 int error;
233
234 SYSCTL_ASSERT_XLOCKED();
235 error = ENOENT;
236 if (oidp->oid_number == OID_AUTO) {
237 error = EINVAL;
238 } else {
239 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
240 if (p == oidp) {
241 SLIST_REMOVE(oidp->oid_parent, oidp,
242 sysctl_oid, oid_link);
243 error = 0;
244 break;
245 }
246 }
247 }
248
249 /*
250 * This can happen when a module fails to register and is
251 * being unloaded afterwards. It should not be a panic()
252 * for normal use.
253 */
254 if (error)
255 printf("%s: failed to unregister sysctl\n", __func__);
256 }
257
258 /* Initialize a new context to keep track of dynamically added sysctls. */
259 int
260 sysctl_ctx_init(struct sysctl_ctx_list *c)
261 {
262
263 if (c == NULL) {
264 return (EINVAL);
265 }
266
267 /*
268 * No locking here, the caller is responsible for not adding
269 * new nodes to a context until after this function has
270 * returned.
271 */
272 TAILQ_INIT(c);
273 return (0);
274 }
275
276 /* Free the context, and destroy all dynamic oids registered in this context */
277 int
278 sysctl_ctx_free(struct sysctl_ctx_list *clist)
279 {
280 struct sysctl_ctx_entry *e, *e1;
281 int error;
282
283 error = 0;
284 /*
285 * First perform a "dry run" to check if it's ok to remove oids.
286 * XXX FIXME
287 * XXX This algorithm is a hack. But I don't know any
288 * XXX better solution for now...
289 */
290 SYSCTL_XLOCK();
291 TAILQ_FOREACH(e, clist, link) {
292 error = sysctl_remove_oid_locked(e->entry, 0, 0);
293 if (error)
294 break;
295 }
296 /*
297 * Restore deregistered entries, either from the end,
298 * or from the place where error occured.
299 * e contains the entry that was not unregistered
300 */
301 if (error)
302 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
303 else
304 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
305 while (e1 != NULL) {
306 sysctl_register_oid(e1->entry);
307 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
308 }
309 if (error) {
310 SYSCTL_XUNLOCK();
311 return(EBUSY);
312 }
313 /* Now really delete the entries */
314 e = TAILQ_FIRST(clist);
315 while (e != NULL) {
316 e1 = TAILQ_NEXT(e, link);
317 error = sysctl_remove_oid_locked(e->entry, 1, 0);
318 if (error)
319 panic("sysctl_remove_oid: corrupt tree, entry: %s",
320 e->entry->oid_name);
321 free(e, M_SYSCTLOID);
322 e = e1;
323 }
324 SYSCTL_XUNLOCK();
325 return (error);
326 }
327
328 /* Add an entry to the context */
329 struct sysctl_ctx_entry *
330 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
331 {
332 struct sysctl_ctx_entry *e;
333
334 SYSCTL_ASSERT_XLOCKED();
335 if (clist == NULL || oidp == NULL)
336 return(NULL);
337 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
338 e->entry = oidp;
339 TAILQ_INSERT_HEAD(clist, e, link);
340 return (e);
341 }
342
343 /* Find an entry in the context */
344 struct sysctl_ctx_entry *
345 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
346 {
347 struct sysctl_ctx_entry *e;
348
349 SYSCTL_ASSERT_XLOCKED();
350 if (clist == NULL || oidp == NULL)
351 return(NULL);
352 TAILQ_FOREACH(e, clist, link) {
353 if(e->entry == oidp)
354 return(e);
355 }
356 return (e);
357 }
358
359 /*
360 * Delete an entry from the context.
361 * NOTE: this function doesn't free oidp! You have to remove it
362 * with sysctl_remove_oid().
363 */
364 int
365 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
366 {
367 struct sysctl_ctx_entry *e;
368
369 if (clist == NULL || oidp == NULL)
370 return (EINVAL);
371 SYSCTL_XLOCK();
372 e = sysctl_ctx_entry_find(clist, oidp);
373 if (e != NULL) {
374 TAILQ_REMOVE(clist, e, link);
375 SYSCTL_XUNLOCK();
376 free(e, M_SYSCTLOID);
377 return (0);
378 } else {
379 SYSCTL_XUNLOCK();
380 return (ENOENT);
381 }
382 }
383
384 /*
385 * Remove dynamically created sysctl trees.
386 * oidp - top of the tree to be removed
387 * del - if 0 - just deregister, otherwise free up entries as well
388 * recurse - if != 0 traverse the subtree to be deleted
389 */
390 int
391 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
392 {
393 int error;
394
395 SYSCTL_XLOCK();
396 error = sysctl_remove_oid_locked(oidp, del, recurse);
397 SYSCTL_XUNLOCK();
398 return (error);
399 }
400
401 int
402 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
403 int del, int recurse)
404 {
405 struct sysctl_oid *p, *tmp;
406 int error;
407
408 error = ENOENT;
409 SYSCTL_XLOCK();
410 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
411 if (strcmp(p->oid_name, name) == 0) {
412 error = sysctl_remove_oid_locked(p, del, recurse);
413 break;
414 }
415 }
416 SYSCTL_XUNLOCK();
417
418 return (error);
419 }
420
421
422 static int
423 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
424 {
425 struct sysctl_oid *p, *tmp;
426 int error;
427
428 SYSCTL_ASSERT_XLOCKED();
429 if (oidp == NULL)
430 return(EINVAL);
431 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
432 printf("can't remove non-dynamic nodes!\n");
433 return (EINVAL);
434 }
435 /*
436 * WARNING: normal method to do this should be through
437 * sysctl_ctx_free(). Use recursing as the last resort
438 * method to purge your sysctl tree of leftovers...
439 * However, if some other code still references these nodes,
440 * it will panic.
441 */
442 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
443 if (oidp->oid_refcnt == 1) {
444 SLIST_FOREACH_SAFE(p,
445 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
446 if (!recurse)
447 return (ENOTEMPTY);
448 error = sysctl_remove_oid_locked(p, del,
449 recurse);
450 if (error)
451 return (error);
452 }
453 if (del)
454 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
455 }
456 }
457 if (oidp->oid_refcnt > 1 ) {
458 oidp->oid_refcnt--;
459 } else {
460 if (oidp->oid_refcnt == 0) {
461 printf("Warning: bad oid_refcnt=%u (%s)!\n",
462 oidp->oid_refcnt, oidp->oid_name);
463 return (EINVAL);
464 }
465 sysctl_unregister_oid(oidp);
466 if (del) {
467 /*
468 * Wait for all threads running the handler to drain.
469 * This preserves the previous behavior when the
470 * sysctl lock was held across a handler invocation,
471 * and is necessary for module unload correctness.
472 */
473 while (oidp->oid_running > 0) {
474 oidp->oid_kind |= CTLFLAG_DYING;
475 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
476 }
477 if (oidp->oid_descr)
478 free(__DECONST(char *, oidp->oid_descr),
479 M_SYSCTLOID);
480 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
481 free(oidp, M_SYSCTLOID);
482 }
483 }
484 return (0);
485 }
486 /*
487 * Create new sysctls at run time.
488 * clist may point to a valid context initialized with sysctl_ctx_init().
489 */
490 struct sysctl_oid *
491 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
492 int number, const char *name, int kind, void *arg1, intptr_t arg2,
493 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
494 {
495 struct sysctl_oid *oidp;
496
497 /* You have to hook up somewhere.. */
498 if (parent == NULL)
499 return(NULL);
500 /* Check if the node already exists, otherwise create it */
501 SYSCTL_XLOCK();
502 oidp = sysctl_find_oidname(name, parent);
503 if (oidp != NULL) {
504 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
505 oidp->oid_refcnt++;
506 /* Update the context */
507 if (clist != NULL)
508 sysctl_ctx_entry_add(clist, oidp);
509 SYSCTL_XUNLOCK();
510 return (oidp);
511 } else {
512 SYSCTL_XUNLOCK();
513 printf("can't re-use a leaf (%s)!\n", name);
514 return (NULL);
515 }
516 }
517 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
518 oidp->oid_parent = parent;
519 SLIST_NEXT(oidp, oid_link) = NULL;
520 oidp->oid_number = number;
521 oidp->oid_refcnt = 1;
522 oidp->oid_name = strdup(name, M_SYSCTLOID);
523 oidp->oid_handler = handler;
524 oidp->oid_kind = CTLFLAG_DYN | kind;
525 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
526 /* Allocate space for children */
527 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
528 M_SYSCTLOID, M_WAITOK));
529 SLIST_INIT(SYSCTL_CHILDREN(oidp));
530 oidp->oid_arg2 = arg2;
531 } else {
532 oidp->oid_arg1 = arg1;
533 oidp->oid_arg2 = arg2;
534 }
535 oidp->oid_fmt = fmt;
536 if (descr)
537 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
538 /* Update the context, if used */
539 if (clist != NULL)
540 sysctl_ctx_entry_add(clist, oidp);
541 /* Register this oid */
542 sysctl_register_oid(oidp);
543 SYSCTL_XUNLOCK();
544 return (oidp);
545 }
546
547 /*
548 * Rename an existing oid.
549 */
550 void
551 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
552 {
553 char *newname;
554 char *oldname;
555
556 newname = strdup(name, M_SYSCTLOID);
557 SYSCTL_XLOCK();
558 oldname = __DECONST(char *, oidp->oid_name);
559 oidp->oid_name = newname;
560 SYSCTL_XUNLOCK();
561 free(oldname, M_SYSCTLOID);
562 }
563
564 /*
565 * Reparent an existing oid.
566 */
567 int
568 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
569 {
570 struct sysctl_oid *oidp;
571
572 SYSCTL_XLOCK();
573 if (oid->oid_parent == parent) {
574 SYSCTL_XUNLOCK();
575 return (0);
576 }
577 oidp = sysctl_find_oidname(oid->oid_name, parent);
578 if (oidp != NULL) {
579 SYSCTL_XUNLOCK();
580 return (EEXIST);
581 }
582 sysctl_unregister_oid(oid);
583 oid->oid_parent = parent;
584 oid->oid_number = OID_AUTO;
585 sysctl_register_oid(oid);
586 SYSCTL_XUNLOCK();
587 return (0);
588 }
589
590 /*
591 * Register the kernel's oids on startup.
592 */
593 SET_DECLARE(sysctl_set, struct sysctl_oid);
594
595 static void
596 sysctl_register_all(void *arg)
597 {
598 struct sysctl_oid **oidp;
599
600 sx_init(&sysctlmemlock, "sysctl mem");
601 SYSCTL_INIT();
602 SYSCTL_XLOCK();
603 SET_FOREACH(oidp, sysctl_set)
604 sysctl_register_oid(*oidp);
605 SYSCTL_XUNLOCK();
606 }
607 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
608
609 /*
610 * "Staff-functions"
611 *
612 * These functions implement a presently undocumented interface
613 * used by the sysctl program to walk the tree, and get the type
614 * so it can print the value.
615 * This interface is under work and consideration, and should probably
616 * be killed with a big axe by the first person who can find the time.
617 * (be aware though, that the proper interface isn't as obvious as it
618 * may seem, there are various conflicting requirements.
619 *
620 * {0,0} printf the entire MIB-tree.
621 * {0,1,...} return the name of the "..." OID.
622 * {0,2,...} return the next OID.
623 * {0,3} return the OID of the name in "new"
624 * {0,4,...} return the kind & format info for the "..." OID.
625 * {0,5,...} return the description the "..." OID.
626 */
627
628 #ifdef SYSCTL_DEBUG
629 static void
630 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
631 {
632 int k;
633 struct sysctl_oid *oidp;
634
635 SYSCTL_ASSERT_XLOCKED();
636 SLIST_FOREACH(oidp, l, oid_link) {
637
638 for (k=0; k<i; k++)
639 printf(" ");
640
641 printf("%d %s ", oidp->oid_number, oidp->oid_name);
642
643 printf("%c%c",
644 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
645 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
646
647 if (oidp->oid_handler)
648 printf(" *Handler");
649
650 switch (oidp->oid_kind & CTLTYPE) {
651 case CTLTYPE_NODE:
652 printf(" Node\n");
653 if (!oidp->oid_handler) {
654 sysctl_sysctl_debug_dump_node(
655 oidp->oid_arg1, i+2);
656 }
657 break;
658 case CTLTYPE_INT: printf(" Int\n"); break;
659 case CTLTYPE_UINT: printf(" u_int\n"); break;
660 case CTLTYPE_LONG: printf(" Long\n"); break;
661 case CTLTYPE_ULONG: printf(" u_long\n"); break;
662 case CTLTYPE_STRING: printf(" String\n"); break;
663 case CTLTYPE_U64: printf(" uint64_t\n"); break;
664 case CTLTYPE_S64: printf(" int64_t\n"); break;
665 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
666 default: printf("\n");
667 }
668
669 }
670 }
671
672 static int
673 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
674 {
675 int error;
676
677 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
678 if (error)
679 return (error);
680 SYSCTL_XLOCK();
681 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
682 SYSCTL_XUNLOCK();
683 return (ENOENT);
684 }
685
686 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
687 0, 0, sysctl_sysctl_debug, "-", "");
688 #endif
689
690 static int
691 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
692 {
693 int *name = (int *) arg1;
694 u_int namelen = arg2;
695 int error = 0;
696 struct sysctl_oid *oid;
697 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
698 char buf[10];
699
700 SYSCTL_XLOCK();
701 while (namelen) {
702 if (!lsp) {
703 snprintf(buf,sizeof(buf),"%d",*name);
704 if (req->oldidx)
705 error = SYSCTL_OUT(req, ".", 1);
706 if (!error)
707 error = SYSCTL_OUT(req, buf, strlen(buf));
708 if (error)
709 goto out;
710 namelen--;
711 name++;
712 continue;
713 }
714 lsp2 = 0;
715 SLIST_FOREACH(oid, lsp, oid_link) {
716 if (oid->oid_number != *name)
717 continue;
718
719 if (req->oldidx)
720 error = SYSCTL_OUT(req, ".", 1);
721 if (!error)
722 error = SYSCTL_OUT(req, oid->oid_name,
723 strlen(oid->oid_name));
724 if (error)
725 goto out;
726
727 namelen--;
728 name++;
729
730 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
731 break;
732
733 if (oid->oid_handler)
734 break;
735
736 lsp2 = SYSCTL_CHILDREN(oid);
737 break;
738 }
739 lsp = lsp2;
740 }
741 error = SYSCTL_OUT(req, "", 1);
742 out:
743 SYSCTL_XUNLOCK();
744 return (error);
745 }
746
747 /*
748 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
749 * capability mode.
750 */
751 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD,
752 sysctl_sysctl_name, "");
753
754 static int
755 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
756 int *next, int *len, int level, struct sysctl_oid **oidpp)
757 {
758 struct sysctl_oid *oidp;
759
760 SYSCTL_ASSERT_XLOCKED();
761 *len = level;
762 SLIST_FOREACH(oidp, lsp, oid_link) {
763 *next = oidp->oid_number;
764 *oidpp = oidp;
765
766 if (oidp->oid_kind & CTLFLAG_SKIP)
767 continue;
768
769 if (!namelen) {
770 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
771 return (0);
772 if (oidp->oid_handler)
773 /* We really should call the handler here...*/
774 return (0);
775 lsp = SYSCTL_CHILDREN(oidp);
776 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
777 len, level+1, oidpp))
778 return (0);
779 goto emptynode;
780 }
781
782 if (oidp->oid_number < *name)
783 continue;
784
785 if (oidp->oid_number > *name) {
786 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
787 return (0);
788 if (oidp->oid_handler)
789 return (0);
790 lsp = SYSCTL_CHILDREN(oidp);
791 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
792 next+1, len, level+1, oidpp))
793 return (0);
794 goto next;
795 }
796 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
797 continue;
798
799 if (oidp->oid_handler)
800 continue;
801
802 lsp = SYSCTL_CHILDREN(oidp);
803 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
804 len, level+1, oidpp))
805 return (0);
806 next:
807 namelen = 1;
808 emptynode:
809 *len = level;
810 }
811 return (1);
812 }
813
814 static int
815 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
816 {
817 int *name = (int *) arg1;
818 u_int namelen = arg2;
819 int i, j, error;
820 struct sysctl_oid *oid;
821 struct sysctl_oid_list *lsp = &sysctl__children;
822 int newoid[CTL_MAXNAME];
823
824 SYSCTL_XLOCK();
825 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
826 SYSCTL_XUNLOCK();
827 if (i)
828 return (ENOENT);
829 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
830 return (error);
831 }
832
833 /*
834 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
835 * capability mode.
836 */
837 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
838 sysctl_sysctl_next, "");
839
840 static int
841 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
842 {
843 struct sysctl_oid *oidp;
844 struct sysctl_oid_list *lsp = &sysctl__children;
845 char *p;
846
847 SYSCTL_ASSERT_XLOCKED();
848
849 for (*len = 0; *len < CTL_MAXNAME;) {
850 p = strsep(&name, ".");
851
852 oidp = SLIST_FIRST(lsp);
853 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
854 if (oidp == NULL)
855 return (ENOENT);
856 if (strcmp(p, oidp->oid_name) == 0)
857 break;
858 }
859 *oid++ = oidp->oid_number;
860 (*len)++;
861
862 if (name == NULL || *name == '\0') {
863 if (oidpp)
864 *oidpp = oidp;
865 return (0);
866 }
867
868 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
869 break;
870
871 if (oidp->oid_handler)
872 break;
873
874 lsp = SYSCTL_CHILDREN(oidp);
875 }
876 return (ENOENT);
877 }
878
879 static int
880 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
881 {
882 char *p;
883 int error, oid[CTL_MAXNAME], len = 0;
884 struct sysctl_oid *op = 0;
885
886 if (!req->newlen)
887 return (ENOENT);
888 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
889 return (ENAMETOOLONG);
890
891 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
892
893 error = SYSCTL_IN(req, p, req->newlen);
894 if (error) {
895 free(p, M_SYSCTL);
896 return (error);
897 }
898
899 p [req->newlen] = '\0';
900
901 SYSCTL_XLOCK();
902 error = name2oid(p, oid, &len, &op);
903 SYSCTL_XUNLOCK();
904
905 free(p, M_SYSCTL);
906
907 if (error)
908 return (error);
909
910 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
911 return (error);
912 }
913
914 /*
915 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
916 * capability mode.
917 */
918 SYSCTL_PROC(_sysctl, 3, name2oid,
919 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
920 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
921
922 static int
923 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
924 {
925 struct sysctl_oid *oid;
926 int error;
927
928 SYSCTL_XLOCK();
929 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
930 if (error)
931 goto out;
932
933 if (oid->oid_fmt == NULL) {
934 error = ENOENT;
935 goto out;
936 }
937 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
938 if (error)
939 goto out;
940 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
941 out:
942 SYSCTL_XUNLOCK();
943 return (error);
944 }
945
946
947 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
948 sysctl_sysctl_oidfmt, "");
949
950 static int
951 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
952 {
953 struct sysctl_oid *oid;
954 int error;
955
956 SYSCTL_XLOCK();
957 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
958 if (error)
959 goto out;
960
961 if (oid->oid_descr == NULL) {
962 error = ENOENT;
963 goto out;
964 }
965 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
966 out:
967 SYSCTL_XUNLOCK();
968 return (error);
969 }
970
971 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD,
972 sysctl_sysctl_oiddescr, "");
973
974 /*
975 * Default "handler" functions.
976 */
977
978 /*
979 * Handle an int, signed or unsigned.
980 * Two cases:
981 * a variable: point arg1 at it.
982 * a constant: pass it in arg2.
983 */
984
985 int
986 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
987 {
988 int tmpout, error = 0;
989
990 /*
991 * Attempt to get a coherent snapshot by making a copy of the data.
992 */
993 if (arg1)
994 tmpout = *(int *)arg1;
995 else
996 tmpout = arg2;
997 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
998
999 if (error || !req->newptr)
1000 return (error);
1001
1002 if (!arg1)
1003 error = EPERM;
1004 else
1005 error = SYSCTL_IN(req, arg1, sizeof(int));
1006 return (error);
1007 }
1008
1009 /*
1010 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1011 * Note: this is used by TCP.
1012 */
1013
1014 int
1015 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1016 {
1017 int error, s, tt;
1018
1019 tt = *(int *)arg1;
1020 s = (int)((int64_t)tt * 1000 / hz);
1021
1022 error = sysctl_handle_int(oidp, &s, 0, req);
1023 if (error || !req->newptr)
1024 return (error);
1025
1026 tt = (int)((int64_t)s * hz / 1000);
1027 if (tt < 1)
1028 return (EINVAL);
1029
1030 *(int *)arg1 = tt;
1031 return (0);
1032 }
1033
1034
1035 /*
1036 * Handle a long, signed or unsigned.
1037 * Two cases:
1038 * a variable: point arg1 at it.
1039 * a constant: pass it in arg2.
1040 */
1041
1042 int
1043 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1044 {
1045 int error = 0;
1046 long tmplong;
1047 #ifdef SCTL_MASK32
1048 int tmpint;
1049 #endif
1050
1051 /*
1052 * Attempt to get a coherent snapshot by making a copy of the data.
1053 */
1054 if (arg1)
1055 tmplong = *(long *)arg1;
1056 else
1057 tmplong = arg2;
1058 #ifdef SCTL_MASK32
1059 if (req->flags & SCTL_MASK32) {
1060 tmpint = tmplong;
1061 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1062 } else
1063 #endif
1064 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1065
1066 if (error || !req->newptr)
1067 return (error);
1068
1069 if (!arg1)
1070 error = EPERM;
1071 #ifdef SCTL_MASK32
1072 else if (req->flags & SCTL_MASK32) {
1073 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1074 *(long *)arg1 = (long)tmpint;
1075 }
1076 #endif
1077 else
1078 error = SYSCTL_IN(req, arg1, sizeof(long));
1079 return (error);
1080 }
1081
1082 /*
1083 * Handle a 64 bit int, signed or unsigned.
1084 * Two cases:
1085 * a variable: point arg1 at it.
1086 * a constant: pass it in arg2.
1087 */
1088 int
1089 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1090 {
1091 int error = 0;
1092 uint64_t tmpout;
1093
1094 /*
1095 * Attempt to get a coherent snapshot by making a copy of the data.
1096 */
1097 if (arg1)
1098 tmpout = *(uint64_t *)arg1;
1099 else
1100 tmpout = arg2;
1101 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1102
1103 if (error || !req->newptr)
1104 return (error);
1105
1106 if (!arg1)
1107 error = EPERM;
1108 else
1109 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1110 return (error);
1111 }
1112
1113 /*
1114 * Handle our generic '\0' terminated 'C' string.
1115 * Two cases:
1116 * a variable string: point arg1 at it, arg2 is max length.
1117 * a constant string: point arg1 at it, arg2 is zero.
1118 */
1119
1120 int
1121 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1122 {
1123 int error=0;
1124 char *tmparg;
1125 size_t outlen;
1126
1127 /*
1128 * Attempt to get a coherent snapshot by copying to a
1129 * temporary kernel buffer.
1130 */
1131 retry:
1132 outlen = strlen((char *)arg1)+1;
1133 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1134
1135 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1136 free(tmparg, M_SYSCTLTMP);
1137 goto retry;
1138 }
1139
1140 error = SYSCTL_OUT(req, tmparg, outlen);
1141 free(tmparg, M_SYSCTLTMP);
1142
1143 if (error || !req->newptr)
1144 return (error);
1145
1146 if ((req->newlen - req->newidx) >= arg2) {
1147 error = EINVAL;
1148 } else {
1149 arg2 = (req->newlen - req->newidx);
1150 error = SYSCTL_IN(req, arg1, arg2);
1151 ((char *)arg1)[arg2] = '\0';
1152 }
1153
1154 return (error);
1155 }
1156
1157 /*
1158 * Handle any kind of opaque data.
1159 * arg1 points to it, arg2 is the size.
1160 */
1161
1162 int
1163 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1164 {
1165 int error, tries;
1166 u_int generation;
1167 struct sysctl_req req2;
1168
1169 /*
1170 * Attempt to get a coherent snapshot, by using the thread
1171 * pre-emption counter updated from within mi_switch() to
1172 * determine if we were pre-empted during a bcopy() or
1173 * copyout(). Make 3 attempts at doing this before giving up.
1174 * If we encounter an error, stop immediately.
1175 */
1176 tries = 0;
1177 req2 = *req;
1178 retry:
1179 generation = curthread->td_generation;
1180 error = SYSCTL_OUT(req, arg1, arg2);
1181 if (error)
1182 return (error);
1183 tries++;
1184 if (generation != curthread->td_generation && tries < 3) {
1185 *req = req2;
1186 goto retry;
1187 }
1188
1189 error = SYSCTL_IN(req, arg1, arg2);
1190
1191 return (error);
1192 }
1193
1194 /*
1195 * Transfer functions to/from kernel space.
1196 * XXX: rather untested at this point
1197 */
1198 static int
1199 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1200 {
1201 size_t i = 0;
1202
1203 if (req->oldptr) {
1204 i = l;
1205 if (req->oldlen <= req->oldidx)
1206 i = 0;
1207 else
1208 if (i > req->oldlen - req->oldidx)
1209 i = req->oldlen - req->oldidx;
1210 if (i > 0)
1211 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1212 }
1213 req->oldidx += l;
1214 if (req->oldptr && i != l)
1215 return (ENOMEM);
1216 return (0);
1217 }
1218
1219 static int
1220 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1221 {
1222 if (!req->newptr)
1223 return (0);
1224 if (req->newlen - req->newidx < l)
1225 return (EINVAL);
1226 bcopy((char *)req->newptr + req->newidx, p, l);
1227 req->newidx += l;
1228 return (0);
1229 }
1230
1231 int
1232 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1233 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1234 {
1235 int error = 0;
1236 struct sysctl_req req;
1237
1238 bzero(&req, sizeof req);
1239
1240 req.td = td;
1241 req.flags = flags;
1242
1243 if (oldlenp) {
1244 req.oldlen = *oldlenp;
1245 }
1246 req.validlen = req.oldlen;
1247
1248 if (old) {
1249 req.oldptr= old;
1250 }
1251
1252 if (new != NULL) {
1253 req.newlen = newlen;
1254 req.newptr = new;
1255 }
1256
1257 req.oldfunc = sysctl_old_kernel;
1258 req.newfunc = sysctl_new_kernel;
1259 req.lock = REQ_UNWIRED;
1260
1261 SYSCTL_XLOCK();
1262 error = sysctl_root(0, name, namelen, &req);
1263 SYSCTL_XUNLOCK();
1264
1265 if (req.lock == REQ_WIRED && req.validlen > 0)
1266 vsunlock(req.oldptr, req.validlen);
1267
1268 if (error && error != ENOMEM)
1269 return (error);
1270
1271 if (retval) {
1272 if (req.oldptr && req.oldidx > req.validlen)
1273 *retval = req.validlen;
1274 else
1275 *retval = req.oldidx;
1276 }
1277 return (error);
1278 }
1279
1280 int
1281 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1282 void *new, size_t newlen, size_t *retval, int flags)
1283 {
1284 int oid[CTL_MAXNAME];
1285 size_t oidlen, plen;
1286 int error;
1287
1288 oid[0] = 0; /* sysctl internal magic */
1289 oid[1] = 3; /* name2oid */
1290 oidlen = sizeof(oid);
1291
1292 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1293 (void *)name, strlen(name), &plen, flags);
1294 if (error)
1295 return (error);
1296
1297 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1298 new, newlen, retval, flags);
1299 return (error);
1300 }
1301
1302 /*
1303 * Transfer function to/from user space.
1304 */
1305 static int
1306 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1307 {
1308 size_t i, len, origidx;
1309 int error;
1310
1311 origidx = req->oldidx;
1312 req->oldidx += l;
1313 if (req->oldptr == NULL)
1314 return (0);
1315 /*
1316 * If we have not wired the user supplied buffer and we are currently
1317 * holding locks, drop a witness warning, as it's possible that
1318 * write operations to the user page can sleep.
1319 */
1320 if (req->lock != REQ_WIRED)
1321 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1322 "sysctl_old_user()");
1323 i = l;
1324 len = req->validlen;
1325 if (len <= origidx)
1326 i = 0;
1327 else {
1328 if (i > len - origidx)
1329 i = len - origidx;
1330 if (req->lock == REQ_WIRED) {
1331 error = copyout_nofault(p, (char *)req->oldptr +
1332 origidx, i);
1333 } else
1334 error = copyout(p, (char *)req->oldptr + origidx, i);
1335 if (error != 0)
1336 return (error);
1337 }
1338 if (i < l)
1339 return (ENOMEM);
1340 return (0);
1341 }
1342
1343 static int
1344 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1345 {
1346 int error;
1347
1348 if (!req->newptr)
1349 return (0);
1350 if (req->newlen - req->newidx < l)
1351 return (EINVAL);
1352 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1353 "sysctl_new_user()");
1354 error = copyin((char *)req->newptr + req->newidx, p, l);
1355 req->newidx += l;
1356 return (error);
1357 }
1358
1359 /*
1360 * Wire the user space destination buffer. If set to a value greater than
1361 * zero, the len parameter limits the maximum amount of wired memory.
1362 */
1363 int
1364 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1365 {
1366 int ret;
1367 size_t wiredlen;
1368
1369 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1370 ret = 0;
1371 if (req->lock != REQ_WIRED && req->oldptr &&
1372 req->oldfunc == sysctl_old_user) {
1373 if (wiredlen != 0) {
1374 ret = vslock(req->oldptr, wiredlen);
1375 if (ret != 0) {
1376 if (ret != ENOMEM)
1377 return (ret);
1378 wiredlen = 0;
1379 }
1380 }
1381 req->lock = REQ_WIRED;
1382 req->validlen = wiredlen;
1383 }
1384 return (0);
1385 }
1386
1387 int
1388 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1389 int *nindx, struct sysctl_req *req)
1390 {
1391 struct sysctl_oid_list *lsp;
1392 struct sysctl_oid *oid;
1393 int indx;
1394
1395 SYSCTL_ASSERT_XLOCKED();
1396 lsp = &sysctl__children;
1397 indx = 0;
1398 while (indx < CTL_MAXNAME) {
1399 SLIST_FOREACH(oid, lsp, oid_link) {
1400 if (oid->oid_number == name[indx])
1401 break;
1402 }
1403 if (oid == NULL)
1404 return (ENOENT);
1405
1406 indx++;
1407 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1408 if (oid->oid_handler != NULL || indx == namelen) {
1409 *noid = oid;
1410 if (nindx != NULL)
1411 *nindx = indx;
1412 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1413 ("%s found DYING node %p", __func__, oid));
1414 return (0);
1415 }
1416 lsp = SYSCTL_CHILDREN(oid);
1417 } else if (indx == namelen) {
1418 *noid = oid;
1419 if (nindx != NULL)
1420 *nindx = indx;
1421 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1422 ("%s found DYING node %p", __func__, oid));
1423 return (0);
1424 } else {
1425 return (ENOTDIR);
1426 }
1427 }
1428 return (ENOENT);
1429 }
1430
1431 /*
1432 * Traverse our tree, and find the right node, execute whatever it points
1433 * to, and return the resulting error code.
1434 */
1435
1436 static int
1437 sysctl_root(SYSCTL_HANDLER_ARGS)
1438 {
1439 struct sysctl_oid *oid;
1440 int error, indx, lvl;
1441
1442 SYSCTL_ASSERT_XLOCKED();
1443
1444 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1445 if (error)
1446 return (error);
1447
1448 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1449 /*
1450 * You can't call a sysctl when it's a node, but has
1451 * no handler. Inform the user that it's a node.
1452 * The indx may or may not be the same as namelen.
1453 */
1454 if (oid->oid_handler == NULL)
1455 return (EISDIR);
1456 }
1457
1458 /* Is this sysctl writable? */
1459 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1460 return (EPERM);
1461
1462 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1463
1464 #ifdef CAPABILITY_MODE
1465 /*
1466 * If the process is in capability mode, then don't permit reading or
1467 * writing unless specifically granted for the node.
1468 */
1469 if (IN_CAPABILITY_MODE(req->td)) {
1470 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1471 return (EPERM);
1472 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1473 return (EPERM);
1474 }
1475 #endif
1476
1477 /* Is this sysctl sensitive to securelevels? */
1478 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1479 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1480 error = securelevel_gt(req->td->td_ucred, lvl);
1481 if (error)
1482 return (error);
1483 }
1484
1485 /* Is this sysctl writable by only privileged users? */
1486 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1487 int priv;
1488
1489 if (oid->oid_kind & CTLFLAG_PRISON)
1490 priv = PRIV_SYSCTL_WRITEJAIL;
1491 #ifdef VIMAGE
1492 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1493 prison_owns_vnet(req->td->td_ucred))
1494 priv = PRIV_SYSCTL_WRITEJAIL;
1495 #endif
1496 else
1497 priv = PRIV_SYSCTL_WRITE;
1498 error = priv_check(req->td, priv);
1499 if (error)
1500 return (error);
1501 }
1502
1503 if (!oid->oid_handler)
1504 return (EINVAL);
1505
1506 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1507 arg1 = (int *)arg1 + indx;
1508 arg2 -= indx;
1509 } else {
1510 arg1 = oid->oid_arg1;
1511 arg2 = oid->oid_arg2;
1512 }
1513 #ifdef MAC
1514 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1515 req);
1516 if (error != 0)
1517 return (error);
1518 #endif
1519 oid->oid_running++;
1520 SYSCTL_XUNLOCK();
1521
1522 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1523 mtx_lock(&Giant);
1524 error = oid->oid_handler(oid, arg1, arg2, req);
1525 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1526 mtx_unlock(&Giant);
1527
1528 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1529
1530 SYSCTL_XLOCK();
1531 oid->oid_running--;
1532 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1533 wakeup(&oid->oid_running);
1534 return (error);
1535 }
1536
1537 #ifndef _SYS_SYSPROTO_H_
1538 struct sysctl_args {
1539 int *name;
1540 u_int namelen;
1541 void *old;
1542 size_t *oldlenp;
1543 void *new;
1544 size_t newlen;
1545 };
1546 #endif
1547 int
1548 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1549 {
1550 int error, i, name[CTL_MAXNAME];
1551 size_t j;
1552
1553 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1554 return (EINVAL);
1555
1556 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1557 if (error)
1558 return (error);
1559
1560 error = userland_sysctl(td, name, uap->namelen,
1561 uap->old, uap->oldlenp, 0,
1562 uap->new, uap->newlen, &j, 0);
1563 if (error && error != ENOMEM)
1564 return (error);
1565 if (uap->oldlenp) {
1566 i = copyout(&j, uap->oldlenp, sizeof(j));
1567 if (i)
1568 return (i);
1569 }
1570 return (error);
1571 }
1572
1573 /*
1574 * This is used from various compatibility syscalls too. That's why name
1575 * must be in kernel space.
1576 */
1577 int
1578 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1579 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1580 int flags)
1581 {
1582 int error = 0, memlocked;
1583 struct sysctl_req req;
1584
1585 bzero(&req, sizeof req);
1586
1587 req.td = td;
1588 req.flags = flags;
1589
1590 if (oldlenp) {
1591 if (inkernel) {
1592 req.oldlen = *oldlenp;
1593 } else {
1594 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1595 if (error)
1596 return (error);
1597 }
1598 }
1599 req.validlen = req.oldlen;
1600
1601 if (old) {
1602 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1603 return (EFAULT);
1604 req.oldptr= old;
1605 }
1606
1607 if (new != NULL) {
1608 if (!useracc(new, newlen, VM_PROT_READ))
1609 return (EFAULT);
1610 req.newlen = newlen;
1611 req.newptr = new;
1612 }
1613
1614 req.oldfunc = sysctl_old_user;
1615 req.newfunc = sysctl_new_user;
1616 req.lock = REQ_UNWIRED;
1617
1618 #ifdef KTRACE
1619 if (KTRPOINT(curthread, KTR_SYSCTL))
1620 ktrsysctl(name, namelen);
1621 #endif
1622
1623 if (req.oldlen > PAGE_SIZE) {
1624 memlocked = 1;
1625 sx_xlock(&sysctlmemlock);
1626 } else
1627 memlocked = 0;
1628 CURVNET_SET(TD_TO_VNET(td));
1629
1630 for (;;) {
1631 req.oldidx = 0;
1632 req.newidx = 0;
1633 SYSCTL_XLOCK();
1634 error = sysctl_root(0, name, namelen, &req);
1635 SYSCTL_XUNLOCK();
1636 if (error != EAGAIN)
1637 break;
1638 kern_yield(PRI_USER);
1639 }
1640
1641 CURVNET_RESTORE();
1642
1643 if (req.lock == REQ_WIRED && req.validlen > 0)
1644 vsunlock(req.oldptr, req.validlen);
1645 if (memlocked)
1646 sx_xunlock(&sysctlmemlock);
1647
1648 if (error && error != ENOMEM)
1649 return (error);
1650
1651 if (retval) {
1652 if (req.oldptr && req.oldidx > req.validlen)
1653 *retval = req.validlen;
1654 else
1655 *retval = req.oldidx;
1656 }
1657 return (error);
1658 }
1659
1660 /*
1661 * Drain into a sysctl struct. The user buffer should be wired if a page
1662 * fault would cause issue.
1663 */
1664 static int
1665 sbuf_sysctl_drain(void *arg, const char *data, int len)
1666 {
1667 struct sysctl_req *req = arg;
1668 int error;
1669
1670 error = SYSCTL_OUT(req, data, len);
1671 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1672 return (error == 0 ? len : -error);
1673 }
1674
1675 struct sbuf *
1676 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1677 struct sysctl_req *req)
1678 {
1679
1680 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1681 sbuf_set_drain(s, sbuf_sysctl_drain, req);
1682 return (s);
1683 }
Cache object: 71414afbbc21066797b8e4f8ba59d849
|