xref: /freebsd/sys/geom/geom_ctl.c (revision 1dfcff294e44d4b45813288ef4095c36abb22f0e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  * Copyright (c) 2022 Alexander Motin <mav@FreeBSD.org>
8  *
9  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
10  * and NAI Labs, the Security Research Division of Network Associates, Inc.
11  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
12  * DARPA CHATS research program.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. The names of the authors may not be used to endorse or promote
23  *    products derived from this software without specific prior written
24  *    permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/malloc.h>
46 #include <sys/sbuf.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_extern.h>
50 
51 #include <geom/geom.h>
52 #include <geom/geom_int.h>
53 #define GCTL_TABLE 1
54 #include <geom/geom_ctl.h>
55 
56 #include <machine/stdarg.h>
57 
58 static d_ioctl_t g_ctl_ioctl;
59 
60 static struct cdevsw g_ctl_cdevsw = {
61 	.d_version =	D_VERSION,
62 	.d_flags =	0,
63 	.d_ioctl =	g_ctl_ioctl,
64 	.d_name =	"g_ctl",
65 };
66 
67 CTASSERT(GCTL_PARAM_RD == VM_PROT_READ);
68 CTASSERT(GCTL_PARAM_WR == VM_PROT_WRITE);
69 
70 void
71 g_ctl_init(void)
72 {
73 
74 	make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
75 	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
76 }
77 
78 /*
79  * Report an error back to the user in ascii format.  Return nerror
80  * or EINVAL if nerror isn't specified.
81  */
82 int
83 gctl_error(struct gctl_req *req, const char *fmt, ...)
84 {
85 	va_list ap;
86 
87 	if (req == NULL)
88 		return (EINVAL);
89 
90 	/* We only record the first error */
91 	if (sbuf_done(req->serror)) {
92 		if (!req->nerror)
93 			req->nerror = EEXIST;
94 #ifdef DIAGNOSTIC
95 		printf("gctl_error: buffer closed, message discarded.\n");
96 #endif
97 		return (req->nerror);
98 	}
99 	if (!req->nerror)
100 		req->nerror = EINVAL;
101 
102 	/* If this is the last of several messages, indent it on a new line */
103 	if (sbuf_len(req->serror) > 0)
104 		sbuf_cat(req->serror, "\n\t");
105 	va_start(ap, fmt);
106 	sbuf_vprintf(req->serror, fmt, ap);
107 	va_end(ap);
108 	gctl_post_messages(req);
109 	return (req->nerror);
110 }
111 
112 /*
113  * The gctl_error() function will only report a single message.
114  * Commands that handle multiple devices may want to report a
115  * message for each of the devices. The gctl_msg() function
116  * can be called multiple times to post messages. When done
117  * the application must either call gctl_post_messages() or
118  * call gctl_error() to cause the messages to be reported to
119  * the calling process.
120  */
121 void
122 gctl_msg(struct gctl_req *req, const char *fmt, ...)
123 {
124 	va_list ap;
125 
126 	if (req == NULL)
127 		return;
128 	if (sbuf_done(req->serror)) {
129 #ifdef DIAGNOSTIC
130 		printf("gctl_msg: buffer closed, message discarded.\n");
131 #endif
132 		return;
133 	}
134 	/* Put second and later messages indented on a new line */
135 	if (sbuf_len(req->serror) > 0)
136 		sbuf_cat(req->serror, "\n\t");
137 	va_start(ap, fmt);
138 	sbuf_vprintf(req->serror, fmt, ap);
139 	va_end(ap);
140 }
141 
142 /*
143  * Post the messages to the user.
144  */
145 void
146 gctl_post_messages(struct gctl_req *req)
147 {
148 
149 	if (sbuf_done(req->serror)) {
150 #ifdef DIAGNOSTIC
151 		printf("gctl_post_messages: message buffer already closed.\n");
152 #endif
153 		return;
154 	}
155 	sbuf_finish(req->serror);
156 	if (g_debugflags & G_F_CTLDUMP)
157 		printf("gctl %p message(s) \"%s\"\n", req,
158 		    sbuf_data(req->serror));
159 }
160 
161 /*
162  * Allocate space and copyin() something.
163  * XXX: this should really be a standard function in the kernel.
164  */
165 static void *
166 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
167 {
168 	void *ptr;
169 
170 	ptr = g_malloc(len, M_WAITOK);
171 	req->nerror = copyin(uaddr, ptr, len);
172 	if (!req->nerror)
173 		return (ptr);
174 	g_free(ptr);
175 	return (NULL);
176 }
177 
178 static void
179 gctl_copyin(struct gctl_req *req)
180 {
181 	struct gctl_req_arg *ap;
182 	char *p;
183 	u_int i;
184 
185 	if (req->narg > GEOM_CTL_ARG_MAX) {
186 		gctl_error(req, "too many arguments");
187 		req->arg = NULL;
188 		return;
189 	}
190 
191 	ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
192 	if (ap == NULL) {
193 		gctl_error(req, "bad control request");
194 		req->arg = NULL;
195 		return;
196 	}
197 
198 	/* Nothing have been copyin()'ed yet */
199 	for (i = 0; i < req->narg; i++) {
200 		ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
201 		ap[i].flag &= ~GCTL_PARAM_CHANGED;
202 		ap[i].kvalue = NULL;
203 	}
204 
205 	for (i = 0; i < req->narg; i++) {
206 		if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
207 			gctl_error(req,
208 			    "wrong param name length %d: %d", i, ap[i].nlen);
209 			break;
210 		}
211 		p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
212 		if (p == NULL)
213 			break;
214 		if (p[ap[i].nlen - 1] != '\0') {
215 			gctl_error(req, "unterminated param name");
216 			g_free(p);
217 			break;
218 		}
219 		ap[i].name = p;
220 		ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
221 		if (ap[i].len <= 0) {
222 			gctl_error(req, "negative param length");
223 			break;
224 		}
225 		if (ap[i].flag & GCTL_PARAM_RD) {
226 			p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
227 			if (p == NULL)
228 				break;
229 			if ((ap[i].flag & GCTL_PARAM_ASCII) &&
230 			    p[ap[i].len - 1] != '\0') {
231 				gctl_error(req, "unterminated param value");
232 				g_free(p);
233 				break;
234 			}
235 		} else {
236 			p = g_malloc(ap[i].len, M_WAITOK | M_ZERO);
237 		}
238 		ap[i].kvalue = p;
239 		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
240 	}
241 	req->arg = ap;
242 	return;
243 }
244 
245 static void
246 gctl_copyout(struct gctl_req *req)
247 {
248 	int error, i;
249 	struct gctl_req_arg *ap;
250 
251 	if (req->nerror)
252 		return;
253 	error = 0;
254 	ap = req->arg;
255 	for (i = 0; i < req->narg; i++, ap++) {
256 		if (!(ap->flag & GCTL_PARAM_CHANGED))
257 			continue;
258 		error = copyout(ap->kvalue, ap->value, ap->len);
259 		if (!error)
260 			continue;
261 		req->nerror = error;
262 		return;
263 	}
264 	return;
265 }
266 
267 static void
268 gctl_free(struct gctl_req *req)
269 {
270 	u_int i;
271 
272 	sbuf_delete(req->serror);
273 	if (req->arg == NULL)
274 		return;
275 	for (i = 0; i < req->narg; i++) {
276 		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
277 			g_free(req->arg[i].name);
278 		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
279 		    req->arg[i].len > 0)
280 			g_free(req->arg[i].kvalue);
281 	}
282 	g_free(req->arg);
283 }
284 
285 static void
286 gctl_dump(struct gctl_req *req, const char *what)
287 {
288 	struct gctl_req_arg *ap;
289 	u_int i;
290 	int j;
291 
292 	printf("Dump of gctl %s at %p:\n", what, req);
293 	if (req->nerror > 0) {
294 		printf("  nerror:\t%d\n", req->nerror);
295 		if (sbuf_len(req->serror) > 0)
296 			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
297 	}
298 	if (req->arg == NULL)
299 		return;
300 	for (i = 0; i < req->narg; i++) {
301 		ap = &req->arg[i];
302 		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
303 			printf("  param:\t%d@%p", ap->nlen, ap->name);
304 		else
305 			printf("  param:\t\"%s\"", ap->name);
306 		printf(" [%s%s%d] = ",
307 		    ap->flag & GCTL_PARAM_RD ? "R" : "",
308 		    ap->flag & GCTL_PARAM_WR ? "W" : "",
309 		    ap->len);
310 		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
311 			printf(" =@ %p", ap->value);
312 		} else if (ap->flag & GCTL_PARAM_ASCII) {
313 			printf("\"%s\"", (char *)ap->kvalue);
314 		} else if (ap->len > 0) {
315 			for (j = 0; j < ap->len && j < 512; j++)
316 				printf(" %02x", ((u_char *)ap->kvalue)[j]);
317 		} else {
318 			printf(" = %p", ap->kvalue);
319 		}
320 		printf("\n");
321 	}
322 }
323 
324 int
325 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
326     int len)
327 {
328 	u_int i;
329 	struct gctl_req_arg *ap;
330 
331 	for (i = 0; i < req->narg; i++) {
332 		ap = &req->arg[i];
333 		if (strcmp(param, ap->name))
334 			continue;
335 		if (!(ap->flag & GCTL_PARAM_WR))
336 			return (EPERM);
337 		ap->flag |= GCTL_PARAM_CHANGED;
338 		if (ap->len < len) {
339 			bcopy(ptr, ap->kvalue, ap->len);
340 			return (ENOSPC);
341 		}
342 		bcopy(ptr, ap->kvalue, len);
343 		return (0);
344 	}
345 	return (EINVAL);
346 }
347 
348 void
349 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
350     int len)
351 {
352 
353 	switch (gctl_set_param(req, param, ptr, len)) {
354 	case EPERM:
355 		gctl_error(req, "No write access %s argument", param);
356 		break;
357 	case ENOSPC:
358 		gctl_error(req, "Wrong length %s argument", param);
359 		break;
360 	case EINVAL:
361 		gctl_error(req, "Missing %s argument", param);
362 		break;
363 	}
364 }
365 
366 void *
367 gctl_get_param_flags(struct gctl_req *req, const char *param, int flags, int *len)
368 {
369 	u_int i;
370 	void *p;
371 	struct gctl_req_arg *ap;
372 
373 	for (i = 0; i < req->narg; i++) {
374 		ap = &req->arg[i];
375 		if (strcmp(param, ap->name))
376 			continue;
377 		if ((ap->flag & flags) != flags)
378 			continue;
379 		p = ap->kvalue;
380 		if (len != NULL)
381 			*len = ap->len;
382 		return (p);
383 	}
384 	return (NULL);
385 }
386 
387 void *
388 gctl_get_param(struct gctl_req *req, const char *param, int *len)
389 {
390 
391 	return (gctl_get_param_flags(req, param, GCTL_PARAM_RD, len));
392 }
393 
394 char const *
395 gctl_get_asciiparam(struct gctl_req *req, const char *param)
396 {
397 	char const *p;
398 	int len;
399 
400 	p = gctl_get_param_flags(req, param, GCTL_PARAM_RD, &len);
401 	if (p == NULL)
402 		return (NULL);
403 	if (len < 1) {
404 		gctl_error(req, "Argument without length (%s)", param);
405 		return (NULL);
406 	}
407 	if (p[len - 1] != '\0') {
408 		gctl_error(req, "Unterminated argument (%s)", param);
409 		return (NULL);
410 	}
411 	return (p);
412 }
413 
414 void *
415 gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
416 {
417 	int i;
418 	void *p;
419 
420 	p = gctl_get_param(req, param, &i);
421 	if (i != len) {
422 		p = NULL;
423 		gctl_error(req, "Wrong length %s argument", param);
424 	}
425 	return (p);
426 }
427 
428 void *
429 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
430 {
431 	void *p;
432 
433 	p = gctl_get_paraml_opt(req, param, len);
434 	if (p == NULL)
435 		gctl_error(req, "Missing %s argument", param);
436 	return (p);
437 }
438 
439 struct g_class *
440 gctl_get_class(struct gctl_req *req, char const *arg)
441 {
442 	char const *p;
443 	struct g_class *cp;
444 
445 	p = gctl_get_asciiparam(req, arg);
446 	if (p == NULL) {
447 		gctl_error(req, "Missing %s argument", arg);
448 		return (NULL);
449 	}
450 	LIST_FOREACH(cp, &g_classes, class) {
451 		if (!strcmp(p, cp->name))
452 			return (cp);
453 	}
454 	gctl_error(req, "Class not found: \"%s\"", p);
455 	return (NULL);
456 }
457 
458 struct g_geom *
459 gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
460 {
461 	char const *p;
462 	struct g_geom *gp;
463 
464 	MPASS(mp != NULL);
465 	p = gctl_get_asciiparam(req, arg);
466 	if (p == NULL) {
467 		gctl_error(req, "Missing %s argument", arg);
468 		return (NULL);
469 	}
470 	LIST_FOREACH(gp, &mp->geom, geom)
471 		if (!strcmp(p, gp->name))
472 			return (gp);
473 	gctl_error(req, "Geom not found: \"%s\"", p);
474 	return (NULL);
475 }
476 
477 struct g_provider *
478 gctl_get_provider(struct gctl_req *req, char const *arg)
479 {
480 	char const *p;
481 	struct g_provider *pp;
482 
483 	p = gctl_get_asciiparam(req, arg);
484 	if (p == NULL) {
485 		gctl_error(req, "Missing '%s' argument", arg);
486 		return (NULL);
487 	}
488 	pp = g_provider_by_name(p);
489 	if (pp != NULL)
490 		return (pp);
491 	gctl_error(req, "Provider not found: \"%s\"", p);
492 	return (NULL);
493 }
494 
495 static void
496 g_ctl_getxml(struct gctl_req *req, struct g_class *mp)
497 {
498 	const char *name;
499 	char *buf;
500 	struct sbuf *sb;
501 	int len, i = 0, n = 0, *parents;
502 	struct g_geom *gp, **gps;
503 	struct g_consumer *cp;
504 
505 	parents = gctl_get_paraml(req, "parents", sizeof(*parents));
506 	if (parents == NULL)
507 		return;
508 	name = gctl_get_asciiparam(req, "arg0");
509 	n = 0;
510 	LIST_FOREACH(gp, &mp->geom, geom) {
511 		if (name && strcmp(gp->name, name) != 0)
512 			continue;
513 		n++;
514 		if (*parents) {
515 			LIST_FOREACH(cp, &gp->consumer, consumer)
516 				n++;
517 		}
518 	}
519 	gps = g_malloc((n + 1) * sizeof(*gps), M_WAITOK);
520 	i = 0;
521 	LIST_FOREACH(gp, &mp->geom, geom) {
522 		if (name && strcmp(gp->name, name) != 0)
523 			continue;
524 		gps[i++] = gp;
525 		if (*parents) {
526 			LIST_FOREACH(cp, &gp->consumer, consumer) {
527 				if (cp->provider != NULL)
528 					gps[i++] = cp->provider->geom;
529 			}
530 		}
531 	}
532 	KASSERT(i == n, ("different number of geoms found (%d != %d)",
533 	    i, n));
534 	gps[i] = 0;
535 
536 	buf = gctl_get_param_flags(req, "output", GCTL_PARAM_WR, &len);
537 	if (buf == NULL) {
538 		gctl_error(req, "output parameter missing");
539 		g_free(gps);
540 		return;
541 	}
542 	sb = sbuf_new(NULL, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
543 	g_conf_specific(sb, gps);
544 	gctl_set_param(req, "output", buf, 0);
545 	if (sbuf_error(sb))
546 		gctl_error(req, "output buffer overflow");
547 	sbuf_delete(sb);
548 	g_free(gps);
549 }
550 
551 static void
552 g_ctl_req(void *arg, int flag __unused)
553 {
554 	struct g_class *mp;
555 	struct gctl_req *req;
556 	char const *verb;
557 
558 	g_topology_assert();
559 	req = arg;
560 	mp = gctl_get_class(req, "class");
561 	if (mp == NULL)
562 		return;
563 	verb = gctl_get_param(req, "verb", NULL);
564 	if (verb == NULL) {
565 		gctl_error(req, "Verb missing");
566 		return;
567 	}
568 	if (strcmp(verb, "getxml") == 0) {
569 		g_ctl_getxml(req, mp);
570 	} else if (mp->ctlreq == NULL) {
571 		gctl_error(req, "Class takes no requests");
572 	} else {
573 		mp->ctlreq(req, mp, verb);
574 	}
575 	g_topology_assert();
576 }
577 
578 static int
579 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
580 {
581 	struct gctl_req *req;
582 	int nerror;
583 
584 	req = (void *)data;
585 	req->nerror = 0;
586 	/* It is an error if we cannot return an error text */
587 	if (req->lerror < 2)
588 		return (EINVAL);
589 	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
590 		return (EINVAL);
591 
592 	req->serror = sbuf_new_auto();
593 	/* Check the version */
594 	if (req->version != GCTL_VERSION) {
595 		gctl_error(req, "kernel and libgeom version mismatch.");
596 		req->arg = NULL;
597 	} else {
598 		/* Get things on board */
599 		gctl_copyin(req);
600 
601 		if (g_debugflags & G_F_CTLDUMP)
602 			gctl_dump(req, "request");
603 
604 		if (!req->nerror) {
605 			g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
606 
607 			if (g_debugflags & G_F_CTLDUMP)
608 				gctl_dump(req, "result");
609 
610 			gctl_copyout(req);
611 		}
612 	}
613 	if (sbuf_done(req->serror)) {
614 		copyout(sbuf_data(req->serror), req->error,
615 		    imin(req->lerror, sbuf_len(req->serror) + 1));
616 	}
617 
618 	nerror = req->nerror;
619 	gctl_free(req);
620 	return (nerror);
621 }
622 
623 static int
624 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
625 {
626 	int error;
627 
628 	switch(cmd) {
629 	case GEOM_CTL:
630 		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
631 		break;
632 	default:
633 		error = ENOIOCTL;
634 		break;
635 	}
636 	return (error);
637 
638 }
639