xref: /freebsd/sys/geom/geom_ctl.c (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
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  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/malloc.h>
45 #include <sys/sbuf.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 
50 #include <geom/geom.h>
51 #include <geom/geom_int.h>
52 #define GCTL_TABLE 1
53 #include <geom/geom_ctl.h>
54 
55 #include <machine/stdarg.h>
56 
57 static d_ioctl_t g_ctl_ioctl;
58 
59 static struct cdevsw g_ctl_cdevsw = {
60 	.d_version =	D_VERSION,
61 	.d_flags =	0,
62 	.d_ioctl =	g_ctl_ioctl,
63 	.d_name =	"g_ctl",
64 };
65 
66 void
67 g_ctl_init(void)
68 {
69 
70 	make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
71 	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
72 	KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
73 		("GCTL_PARAM_RD != VM_PROT_READ"));
74 	KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
75 		("GCTL_PARAM_WR != VM_PROT_WRITE"));
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 		p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
226 		if (p == NULL)
227 			break;
228 		if ((ap[i].flag & GCTL_PARAM_ASCII) &&
229 		    p[ap[i].len - 1] != '\0') {
230 			gctl_error(req, "unterminated param value");
231 			g_free(p);
232 			break;
233 		}
234 		ap[i].kvalue = p;
235 		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
236 	}
237 	req->arg = ap;
238 	return;
239 }
240 
241 static void
242 gctl_copyout(struct gctl_req *req)
243 {
244 	int error, i;
245 	struct gctl_req_arg *ap;
246 
247 	if (req->nerror)
248 		return;
249 	error = 0;
250 	ap = req->arg;
251 	for (i = 0; i < req->narg; i++, ap++) {
252 		if (!(ap->flag & GCTL_PARAM_CHANGED))
253 			continue;
254 		error = copyout(ap->kvalue, ap->value, ap->len);
255 		if (!error)
256 			continue;
257 		req->nerror = error;
258 		return;
259 	}
260 	return;
261 }
262 
263 static void
264 gctl_free(struct gctl_req *req)
265 {
266 	u_int i;
267 
268 	sbuf_delete(req->serror);
269 	if (req->arg == NULL)
270 		return;
271 	for (i = 0; i < req->narg; i++) {
272 		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
273 			g_free(req->arg[i].name);
274 		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
275 		    req->arg[i].len > 0)
276 			g_free(req->arg[i].kvalue);
277 	}
278 	g_free(req->arg);
279 }
280 
281 static void
282 gctl_dump(struct gctl_req *req)
283 {
284 	struct gctl_req_arg *ap;
285 	u_int i;
286 	int j;
287 
288 	printf("Dump of gctl request at %p:\n", req);
289 	if (req->nerror > 0) {
290 		printf("  nerror:\t%d\n", req->nerror);
291 		if (sbuf_len(req->serror) > 0)
292 			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
293 	}
294 	if (req->arg == NULL)
295 		return;
296 	for (i = 0; i < req->narg; i++) {
297 		ap = &req->arg[i];
298 		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
299 			printf("  param:\t%d@%p", ap->nlen, ap->name);
300 		else
301 			printf("  param:\t\"%s\"", ap->name);
302 		printf(" [%s%s%d] = ",
303 		    ap->flag & GCTL_PARAM_RD ? "R" : "",
304 		    ap->flag & GCTL_PARAM_WR ? "W" : "",
305 		    ap->len);
306 		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
307 			printf(" =@ %p", ap->value);
308 		} else if (ap->flag & GCTL_PARAM_ASCII) {
309 			printf("\"%s\"", (char *)ap->kvalue);
310 		} else if (ap->len > 0) {
311 			for (j = 0; j < ap->len && j < 512; j++)
312 				printf(" %02x", ((u_char *)ap->kvalue)[j]);
313 		} else {
314 			printf(" = %p", ap->kvalue);
315 		}
316 		printf("\n");
317 	}
318 }
319 
320 int
321 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
322     int len)
323 {
324 	u_int i;
325 	struct gctl_req_arg *ap;
326 
327 	for (i = 0; i < req->narg; i++) {
328 		ap = &req->arg[i];
329 		if (strcmp(param, ap->name))
330 			continue;
331 		if (!(ap->flag & GCTL_PARAM_WR))
332 			return (EPERM);
333 		ap->flag |= GCTL_PARAM_CHANGED;
334 		if (ap->len < len) {
335 			bcopy(ptr, ap->kvalue, ap->len);
336 			return (ENOSPC);
337 		}
338 		bcopy(ptr, ap->kvalue, len);
339 		return (0);
340 	}
341 	return (EINVAL);
342 }
343 
344 void
345 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
346     int len)
347 {
348 
349 	switch (gctl_set_param(req, param, ptr, len)) {
350 	case EPERM:
351 		gctl_error(req, "No write access %s argument", param);
352 		break;
353 	case ENOSPC:
354 		gctl_error(req, "Wrong length %s argument", param);
355 		break;
356 	case EINVAL:
357 		gctl_error(req, "Missing %s argument", param);
358 		break;
359 	}
360 }
361 
362 void *
363 gctl_get_param(struct gctl_req *req, const char *param, int *len)
364 {
365 	u_int i;
366 	void *p;
367 	struct gctl_req_arg *ap;
368 
369 	for (i = 0; i < req->narg; i++) {
370 		ap = &req->arg[i];
371 		if (strcmp(param, ap->name))
372 			continue;
373 		if (!(ap->flag & GCTL_PARAM_RD))
374 			continue;
375 		p = ap->kvalue;
376 		if (len != NULL)
377 			*len = ap->len;
378 		return (p);
379 	}
380 	return (NULL);
381 }
382 
383 char const *
384 gctl_get_asciiparam(struct gctl_req *req, const char *param)
385 {
386 	u_int i;
387 	char const *p;
388 	struct gctl_req_arg *ap;
389 
390 	for (i = 0; i < req->narg; i++) {
391 		ap = &req->arg[i];
392 		if (strcmp(param, ap->name))
393 			continue;
394 		if (!(ap->flag & GCTL_PARAM_RD))
395 			continue;
396 		p = ap->kvalue;
397 		if (ap->len < 1) {
398 			gctl_error(req, "No length argument (%s)", param);
399 			return (NULL);
400 		}
401 		if (p[ap->len - 1] != '\0') {
402 			gctl_error(req, "Unterminated argument (%s)", param);
403 			return (NULL);
404 		}
405 		return (p);
406 	}
407 	return (NULL);
408 }
409 
410 void *
411 gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
412 {
413 	int i;
414 	void *p;
415 
416 	p = gctl_get_param(req, param, &i);
417 	if (i != len) {
418 		p = NULL;
419 		gctl_error(req, "Wrong length %s argument", param);
420 	}
421 	return (p);
422 }
423 
424 void *
425 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
426 {
427 	void *p;
428 
429 	p = gctl_get_paraml_opt(req, param, len);
430 	if (p == NULL)
431 		gctl_error(req, "Missing %s argument", param);
432 	return (p);
433 }
434 
435 struct g_class *
436 gctl_get_class(struct gctl_req *req, char const *arg)
437 {
438 	char const *p;
439 	struct g_class *cp;
440 
441 	p = gctl_get_asciiparam(req, arg);
442 	if (p == NULL) {
443 		gctl_error(req, "Missing %s argument", arg);
444 		return (NULL);
445 	}
446 	LIST_FOREACH(cp, &g_classes, class) {
447 		if (!strcmp(p, cp->name))
448 			return (cp);
449 	}
450 	gctl_error(req, "Class not found: \"%s\"", p);
451 	return (NULL);
452 }
453 
454 struct g_geom *
455 gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
456 {
457 	char const *p;
458 	struct g_geom *gp;
459 
460 	MPASS(mp != NULL);
461 	p = gctl_get_asciiparam(req, arg);
462 	if (p == NULL) {
463 		gctl_error(req, "Missing %s argument", arg);
464 		return (NULL);
465 	}
466 	LIST_FOREACH(gp, &mp->geom, geom)
467 		if (!strcmp(p, gp->name))
468 			return (gp);
469 	gctl_error(req, "Geom not found: \"%s\"", p);
470 	return (NULL);
471 }
472 
473 struct g_provider *
474 gctl_get_provider(struct gctl_req *req, char const *arg)
475 {
476 	char const *p;
477 	struct g_provider *pp;
478 
479 	p = gctl_get_asciiparam(req, arg);
480 	if (p == NULL) {
481 		gctl_error(req, "Missing '%s' argument", arg);
482 		return (NULL);
483 	}
484 	pp = g_provider_by_name(p);
485 	if (pp != NULL)
486 		return (pp);
487 	gctl_error(req, "Provider not found: \"%s\"", p);
488 	return (NULL);
489 }
490 
491 static void
492 g_ctl_req(void *arg, int flag __unused)
493 {
494 	struct g_class *mp;
495 	struct gctl_req *req;
496 	char const *verb;
497 
498 	g_topology_assert();
499 	req = arg;
500 	mp = gctl_get_class(req, "class");
501 	if (mp == NULL)
502 		return;
503 	if (mp->ctlreq == NULL) {
504 		gctl_error(req, "Class takes no requests");
505 		return;
506 	}
507 	verb = gctl_get_param(req, "verb", NULL);
508 	if (verb == NULL) {
509 		gctl_error(req, "Verb missing");
510 		return;
511 	}
512 	mp->ctlreq(req, mp, verb);
513 	g_topology_assert();
514 }
515 
516 static int
517 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
518 {
519 	struct gctl_req *req;
520 	int nerror;
521 
522 	req = (void *)data;
523 	req->nerror = 0;
524 	/* It is an error if we cannot return an error text */
525 	if (req->lerror < 2)
526 		return (EINVAL);
527 	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
528 		return (EINVAL);
529 
530 	req->serror = sbuf_new_auto();
531 	/* Check the version */
532 	if (req->version != GCTL_VERSION) {
533 		gctl_error(req, "kernel and libgeom version mismatch.");
534 		req->arg = NULL;
535 	} else {
536 		/* Get things on board */
537 		gctl_copyin(req);
538 
539 		if (g_debugflags & G_F_CTLDUMP)
540 			gctl_dump(req);
541 
542 		if (!req->nerror) {
543 			g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
544 			gctl_copyout(req);
545 		}
546 	}
547 	if (sbuf_done(req->serror)) {
548 		copyout(sbuf_data(req->serror), req->error,
549 		    imin(req->lerror, sbuf_len(req->serror) + 1));
550 	}
551 
552 	nerror = req->nerror;
553 	gctl_free(req);
554 	return (nerror);
555 }
556 
557 static int
558 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
559 {
560 	int error;
561 
562 	switch(cmd) {
563 	case GEOM_CTL:
564 		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
565 		break;
566 	default:
567 		error = ENOIOCTL;
568 		break;
569 	}
570 	return (error);
571 
572 }
573