xref: /freebsd-13-stable/sys/geom/geom_ctl.c (revision bd8a789e3c28e4f2debf0c511c89fadbc29a34f9)
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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 #include <sys/malloc.h>
44 #include <sys/sbuf.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 
49 #include <geom/geom.h>
50 #include <geom/geom_int.h>
51 #define GCTL_TABLE 1
52 #include <geom/geom_ctl.h>
53 
54 #include <machine/stdarg.h>
55 
56 static d_ioctl_t g_ctl_ioctl;
57 
58 static struct cdevsw g_ctl_cdevsw = {
59 	.d_version =	D_VERSION,
60 	.d_flags =	0,
61 	.d_ioctl =	g_ctl_ioctl,
62 	.d_name =	"g_ctl",
63 };
64 
65 CTASSERT(GCTL_PARAM_RD == VM_PROT_READ);
66 CTASSERT(GCTL_PARAM_WR == VM_PROT_WRITE);
67 
68 void
g_ctl_init(void)69 g_ctl_init(void)
70 {
71 
72 	make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
73 	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
74 }
75 
76 /*
77  * Report an error back to the user in ascii format.  Return nerror
78  * or EINVAL if nerror isn't specified.
79  */
80 int
gctl_error(struct gctl_req * req,const char * fmt,...)81 gctl_error(struct gctl_req *req, const char *fmt, ...)
82 {
83 	va_list ap;
84 
85 	if (req == NULL)
86 		return (EINVAL);
87 
88 	/* We only record the first error */
89 	if (sbuf_done(req->serror)) {
90 		if (!req->nerror)
91 			req->nerror = EEXIST;
92 		return (req->nerror);
93 	}
94 	if (!req->nerror)
95 		req->nerror = EINVAL;
96 
97 	va_start(ap, fmt);
98 	sbuf_vprintf(req->serror, fmt, ap);
99 	va_end(ap);
100 	sbuf_finish(req->serror);
101 	if (g_debugflags & G_F_CTLDUMP)
102 		printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
103 	return (req->nerror);
104 }
105 
106 /*
107  * Allocate space and copyin() something.
108  * XXX: this should really be a standard function in the kernel.
109  */
110 static void *
geom_alloc_copyin(struct gctl_req * req,void * uaddr,size_t len)111 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
112 {
113 	void *ptr;
114 
115 	ptr = g_malloc(len, M_WAITOK);
116 	req->nerror = copyin(uaddr, ptr, len);
117 	if (!req->nerror)
118 		return (ptr);
119 	g_free(ptr);
120 	return (NULL);
121 }
122 
123 static void
gctl_copyin(struct gctl_req * req)124 gctl_copyin(struct gctl_req *req)
125 {
126 	struct gctl_req_arg *ap;
127 	char *p;
128 	u_int i;
129 
130 	if (req->narg > GEOM_CTL_ARG_MAX) {
131 		gctl_error(req, "too many arguments");
132 		req->arg = NULL;
133 		return;
134 	}
135 
136 	ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
137 	if (ap == NULL) {
138 		gctl_error(req, "bad control request");
139 		req->arg = NULL;
140 		return;
141 	}
142 
143 	/* Nothing have been copyin()'ed yet */
144 	for (i = 0; i < req->narg; i++) {
145 		ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
146 		ap[i].flag &= ~GCTL_PARAM_CHANGED;
147 		ap[i].kvalue = NULL;
148 	}
149 
150 	for (i = 0; i < req->narg; i++) {
151 		if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
152 			gctl_error(req,
153 			    "wrong param name length %d: %d", i, ap[i].nlen);
154 			break;
155 		}
156 		p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
157 		if (p == NULL)
158 			break;
159 		if (p[ap[i].nlen - 1] != '\0') {
160 			gctl_error(req, "unterminated param name");
161 			g_free(p);
162 			break;
163 		}
164 		ap[i].name = p;
165 		ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
166 		if (ap[i].len <= 0) {
167 			gctl_error(req, "negative param length");
168 			break;
169 		}
170 		if (ap[i].flag & GCTL_PARAM_RD) {
171 			p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
172 			if (p == NULL)
173 				break;
174 			if ((ap[i].flag & GCTL_PARAM_ASCII) &&
175 			    p[ap[i].len - 1] != '\0') {
176 				gctl_error(req, "unterminated param value");
177 				g_free(p);
178 				break;
179 			}
180 		} else {
181 			p = g_malloc(ap[i].len, M_WAITOK | M_ZERO);
182 		}
183 		ap[i].kvalue = p;
184 		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
185 	}
186 	req->arg = ap;
187 	return;
188 }
189 
190 static void
gctl_copyout(struct gctl_req * req)191 gctl_copyout(struct gctl_req *req)
192 {
193 	int error, i;
194 	struct gctl_req_arg *ap;
195 
196 	if (req->nerror)
197 		return;
198 	error = 0;
199 	ap = req->arg;
200 	for (i = 0; i < req->narg; i++, ap++) {
201 		if (!(ap->flag & GCTL_PARAM_CHANGED))
202 			continue;
203 		error = copyout(ap->kvalue, ap->value, ap->len);
204 		if (!error)
205 			continue;
206 		req->nerror = error;
207 		return;
208 	}
209 	return;
210 }
211 
212 static void
gctl_free(struct gctl_req * req)213 gctl_free(struct gctl_req *req)
214 {
215 	u_int i;
216 
217 	sbuf_delete(req->serror);
218 	if (req->arg == NULL)
219 		return;
220 	for (i = 0; i < req->narg; i++) {
221 		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
222 			g_free(req->arg[i].name);
223 		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
224 		    req->arg[i].len > 0)
225 			g_free(req->arg[i].kvalue);
226 	}
227 	g_free(req->arg);
228 }
229 
230 static void
gctl_dump(struct gctl_req * req,const char * what)231 gctl_dump(struct gctl_req *req, const char *what)
232 {
233 	struct gctl_req_arg *ap;
234 	u_int i;
235 	int j;
236 
237 	printf("Dump of gctl %s at %p:\n", what, req);
238 	if (req->nerror > 0) {
239 		printf("  nerror:\t%d\n", req->nerror);
240 		if (sbuf_len(req->serror) > 0)
241 			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
242 	}
243 	if (req->arg == NULL)
244 		return;
245 	for (i = 0; i < req->narg; i++) {
246 		ap = &req->arg[i];
247 		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
248 			printf("  param:\t%d@%p", ap->nlen, ap->name);
249 		else
250 			printf("  param:\t\"%s\"", ap->name);
251 		printf(" [%s%s%d] = ",
252 		    ap->flag & GCTL_PARAM_RD ? "R" : "",
253 		    ap->flag & GCTL_PARAM_WR ? "W" : "",
254 		    ap->len);
255 		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
256 			printf(" =@ %p", ap->value);
257 		} else if (ap->flag & GCTL_PARAM_ASCII) {
258 			printf("\"%s\"", (char *)ap->kvalue);
259 		} else if (ap->len > 0) {
260 			for (j = 0; j < ap->len && j < 512; j++)
261 				printf(" %02x", ((u_char *)ap->kvalue)[j]);
262 		} else {
263 			printf(" = %p", ap->kvalue);
264 		}
265 		printf("\n");
266 	}
267 }
268 
269 int
gctl_set_param(struct gctl_req * req,const char * param,void const * ptr,int len)270 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
271     int len)
272 {
273 	u_int i;
274 	struct gctl_req_arg *ap;
275 
276 	for (i = 0; i < req->narg; i++) {
277 		ap = &req->arg[i];
278 		if (strcmp(param, ap->name))
279 			continue;
280 		if (!(ap->flag & GCTL_PARAM_WR))
281 			return (EPERM);
282 		ap->flag |= GCTL_PARAM_CHANGED;
283 		if (ap->len < len) {
284 			bcopy(ptr, ap->kvalue, ap->len);
285 			return (ENOSPC);
286 		}
287 		bcopy(ptr, ap->kvalue, len);
288 		return (0);
289 	}
290 	return (EINVAL);
291 }
292 
293 void
gctl_set_param_err(struct gctl_req * req,const char * param,void const * ptr,int len)294 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
295     int len)
296 {
297 
298 	switch (gctl_set_param(req, param, ptr, len)) {
299 	case EPERM:
300 		gctl_error(req, "No write access %s argument", param);
301 		break;
302 	case ENOSPC:
303 		gctl_error(req, "Wrong length %s argument", param);
304 		break;
305 	case EINVAL:
306 		gctl_error(req, "Missing %s argument", param);
307 		break;
308 	}
309 }
310 
311 void *
gctl_get_param_flags(struct gctl_req * req,const char * param,int flags,int * len)312 gctl_get_param_flags(struct gctl_req *req, const char *param, int flags, int *len)
313 {
314 	u_int i;
315 	void *p;
316 	struct gctl_req_arg *ap;
317 
318 	for (i = 0; i < req->narg; i++) {
319 		ap = &req->arg[i];
320 		if (strcmp(param, ap->name))
321 			continue;
322 		if ((ap->flag & flags) != flags)
323 			continue;
324 		p = ap->kvalue;
325 		if (len != NULL)
326 			*len = ap->len;
327 		return (p);
328 	}
329 	return (NULL);
330 }
331 
332 void *
gctl_get_param(struct gctl_req * req,const char * param,int * len)333 gctl_get_param(struct gctl_req *req, const char *param, int *len)
334 {
335 
336 	return (gctl_get_param_flags(req, param, GCTL_PARAM_RD, len));
337 }
338 
339 char const *
gctl_get_asciiparam(struct gctl_req * req,const char * param)340 gctl_get_asciiparam(struct gctl_req *req, const char *param)
341 {
342 	char const *p;
343 	int len;
344 
345 	p = gctl_get_param_flags(req, param, GCTL_PARAM_RD, &len);
346 	if (p == NULL)
347 		return (NULL);
348 	if (len < 1) {
349 		gctl_error(req, "Argument without length (%s)", param);
350 		return (NULL);
351 	}
352 	if (p[len - 1] != '\0') {
353 		gctl_error(req, "Unterminated argument (%s)", param);
354 		return (NULL);
355 	}
356 	return (p);
357 }
358 
359 void *
gctl_get_paraml_opt(struct gctl_req * req,const char * param,int len)360 gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
361 {
362 	int i;
363 	void *p;
364 
365 	p = gctl_get_param(req, param, &i);
366 	if (i != len) {
367 		p = NULL;
368 		gctl_error(req, "Wrong length %s argument", param);
369 	}
370 	return (p);
371 }
372 
373 void *
gctl_get_paraml(struct gctl_req * req,const char * param,int len)374 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
375 {
376 	void *p;
377 
378 	p = gctl_get_paraml_opt(req, param, len);
379 	if (p == NULL)
380 		gctl_error(req, "Missing %s argument", param);
381 	return (p);
382 }
383 
384 struct g_class *
gctl_get_class(struct gctl_req * req,char const * arg)385 gctl_get_class(struct gctl_req *req, char const *arg)
386 {
387 	char const *p;
388 	struct g_class *cp;
389 
390 	p = gctl_get_asciiparam(req, arg);
391 	if (p == NULL) {
392 		gctl_error(req, "Missing %s argument", arg);
393 		return (NULL);
394 	}
395 	LIST_FOREACH(cp, &g_classes, class) {
396 		if (!strcmp(p, cp->name))
397 			return (cp);
398 	}
399 	gctl_error(req, "Class not found: \"%s\"", p);
400 	return (NULL);
401 }
402 
403 struct g_geom *
gctl_get_geom(struct gctl_req * req,struct g_class * mp,char const * arg)404 gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
405 {
406 	char const *p;
407 	struct g_geom *gp;
408 
409 	MPASS(mp != NULL);
410 	p = gctl_get_asciiparam(req, arg);
411 	if (p == NULL) {
412 		gctl_error(req, "Missing %s argument", arg);
413 		return (NULL);
414 	}
415 	LIST_FOREACH(gp, &mp->geom, geom)
416 		if (!strcmp(p, gp->name))
417 			return (gp);
418 	gctl_error(req, "Geom not found: \"%s\"", p);
419 	return (NULL);
420 }
421 
422 struct g_provider *
gctl_get_provider(struct gctl_req * req,char const * arg)423 gctl_get_provider(struct gctl_req *req, char const *arg)
424 {
425 	char const *p;
426 	struct g_provider *pp;
427 
428 	p = gctl_get_asciiparam(req, arg);
429 	if (p == NULL) {
430 		gctl_error(req, "Missing '%s' argument", arg);
431 		return (NULL);
432 	}
433 	pp = g_provider_by_name(p);
434 	if (pp != NULL)
435 		return (pp);
436 	gctl_error(req, "Provider not found: \"%s\"", p);
437 	return (NULL);
438 }
439 
440 static void
g_ctl_getxml(struct gctl_req * req,struct g_class * mp)441 g_ctl_getxml(struct gctl_req *req, struct g_class *mp)
442 {
443 	const char *name;
444 	char *buf;
445 	struct sbuf *sb;
446 	int len, i = 0, n = 0, *parents;
447 	struct g_geom *gp, **gps;
448 	struct g_consumer *cp;
449 
450 	parents = gctl_get_paraml(req, "parents", sizeof(*parents));
451 	if (parents == NULL)
452 		return;
453 	name = gctl_get_asciiparam(req, "arg0");
454 	n = 0;
455 	LIST_FOREACH(gp, &mp->geom, geom) {
456 		if (name && strcmp(gp->name, name) != 0)
457 			continue;
458 		n++;
459 		if (*parents) {
460 			LIST_FOREACH(cp, &gp->consumer, consumer)
461 				n++;
462 		}
463 	}
464 	gps = g_malloc((n + 1) * sizeof(*gps), M_WAITOK);
465 	i = 0;
466 	LIST_FOREACH(gp, &mp->geom, geom) {
467 		if (name && strcmp(gp->name, name) != 0)
468 			continue;
469 		gps[i++] = gp;
470 		if (*parents) {
471 			LIST_FOREACH(cp, &gp->consumer, consumer) {
472 				if (cp->provider != NULL)
473 					gps[i++] = cp->provider->geom;
474 			}
475 		}
476 	}
477 	KASSERT(i == n, ("different number of geoms found (%d != %d)",
478 	    i, n));
479 	gps[i] = 0;
480 
481 	buf = gctl_get_param_flags(req, "output", GCTL_PARAM_WR, &len);
482 	if (buf == NULL) {
483 		gctl_error(req, "output parameter missing");
484 		g_free(gps);
485 		return;
486 	}
487 	sb = sbuf_new(NULL, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
488 	g_conf_specific(sb, gps);
489 	gctl_set_param(req, "output", buf, 0);
490 	if (sbuf_error(sb))
491 		gctl_error(req, "output buffer overflow");
492 	sbuf_delete(sb);
493 	g_free(gps);
494 }
495 
496 static void
g_ctl_req(void * arg,int flag __unused)497 g_ctl_req(void *arg, int flag __unused)
498 {
499 	struct g_class *mp;
500 	struct gctl_req *req;
501 	char const *verb;
502 
503 	g_topology_assert();
504 	req = arg;
505 	mp = gctl_get_class(req, "class");
506 	if (mp == NULL)
507 		return;
508 	verb = gctl_get_param(req, "verb", NULL);
509 	if (verb == NULL) {
510 		gctl_error(req, "Verb missing");
511 		return;
512 	}
513 	if (strcmp(verb, "getxml") == 0) {
514 		g_ctl_getxml(req, mp);
515 	} else if (mp->ctlreq == NULL) {
516 		gctl_error(req, "Class takes no requests");
517 	} else {
518 		mp->ctlreq(req, mp, verb);
519 	}
520 	g_topology_assert();
521 }
522 
523 static int
g_ctl_ioctl_ctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)524 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
525 {
526 	struct gctl_req *req;
527 	int nerror;
528 
529 	req = (void *)data;
530 	req->nerror = 0;
531 	/* It is an error if we cannot return an error text */
532 	if (req->lerror < 2)
533 		return (EINVAL);
534 	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
535 		return (EINVAL);
536 
537 	req->serror = sbuf_new_auto();
538 	/* Check the version */
539 	if (req->version != GCTL_VERSION) {
540 		gctl_error(req, "kernel and libgeom version mismatch.");
541 		req->arg = NULL;
542 	} else {
543 		/* Get things on board */
544 		gctl_copyin(req);
545 
546 		if (g_debugflags & G_F_CTLDUMP)
547 			gctl_dump(req, "request");
548 
549 		if (!req->nerror) {
550 			g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
551 
552 			if (g_debugflags & G_F_CTLDUMP)
553 				gctl_dump(req, "result");
554 
555 			gctl_copyout(req);
556 		}
557 	}
558 	if (sbuf_done(req->serror)) {
559 		nerror = copyout(sbuf_data(req->serror), req->error,
560 		    imin(req->lerror, sbuf_len(req->serror) + 1));
561 		if (nerror != 0 && req->nerror == 0)
562 			req->nerror = nerror;
563 	}
564 
565 	nerror = req->nerror;
566 	gctl_free(req);
567 	return (nerror);
568 }
569 
570 static int
g_ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)571 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
572 {
573 	int error;
574 
575 	switch(cmd) {
576 	case GEOM_CTL:
577 		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
578 		break;
579 	default:
580 		error = ENOIOCTL;
581 		break;
582 	}
583 	return (error);
584 
585 }
586