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
g_ctl_init(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
gctl_error(struct gctl_req * req,const char * fmt,...)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 return (req->nerror);
95 }
96 if (!req->nerror)
97 req->nerror = EINVAL;
98
99 va_start(ap, fmt);
100 sbuf_vprintf(req->serror, fmt, ap);
101 va_end(ap);
102 sbuf_finish(req->serror);
103 if (g_debugflags & G_F_CTLDUMP)
104 printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
105 return (req->nerror);
106 }
107
108 /*
109 * Allocate space and copyin() something.
110 * XXX: this should really be a standard function in the kernel.
111 */
112 static void *
geom_alloc_copyin(struct gctl_req * req,void * uaddr,size_t len)113 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
114 {
115 void *ptr;
116
117 ptr = g_malloc(len, M_WAITOK);
118 req->nerror = copyin(uaddr, ptr, len);
119 if (!req->nerror)
120 return (ptr);
121 g_free(ptr);
122 return (NULL);
123 }
124
125 static void
gctl_copyin(struct gctl_req * req)126 gctl_copyin(struct gctl_req *req)
127 {
128 struct gctl_req_arg *ap;
129 char *p;
130 u_int i;
131
132 if (req->narg > GEOM_CTL_ARG_MAX) {
133 gctl_error(req, "too many arguments");
134 req->arg = NULL;
135 return;
136 }
137
138 ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
139 if (ap == NULL) {
140 gctl_error(req, "bad control request");
141 req->arg = NULL;
142 return;
143 }
144
145 /* Nothing have been copyin()'ed yet */
146 for (i = 0; i < req->narg; i++) {
147 ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
148 ap[i].flag &= ~GCTL_PARAM_CHANGED;
149 ap[i].kvalue = NULL;
150 }
151
152 for (i = 0; i < req->narg; i++) {
153 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
154 gctl_error(req,
155 "wrong param name length %d: %d", i, ap[i].nlen);
156 break;
157 }
158 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
159 if (p == NULL)
160 break;
161 if (p[ap[i].nlen - 1] != '\0') {
162 gctl_error(req, "unterminated param name");
163 g_free(p);
164 break;
165 }
166 ap[i].name = p;
167 ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
168 if (ap[i].len <= 0) {
169 gctl_error(req, "negative param length");
170 break;
171 }
172 p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
173 if (p == NULL)
174 break;
175 if ((ap[i].flag & GCTL_PARAM_ASCII) &&
176 p[ap[i].len - 1] != '\0') {
177 gctl_error(req, "unterminated param value");
178 g_free(p);
179 break;
180 }
181 ap[i].kvalue = p;
182 ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
183 }
184 req->arg = ap;
185 return;
186 }
187
188 static void
gctl_copyout(struct gctl_req * req)189 gctl_copyout(struct gctl_req *req)
190 {
191 int error, i;
192 struct gctl_req_arg *ap;
193
194 if (req->nerror)
195 return;
196 error = 0;
197 ap = req->arg;
198 for (i = 0; i < req->narg; i++, ap++) {
199 if (!(ap->flag & GCTL_PARAM_CHANGED))
200 continue;
201 error = copyout(ap->kvalue, ap->value, ap->len);
202 if (!error)
203 continue;
204 req->nerror = error;
205 return;
206 }
207 return;
208 }
209
210 static void
gctl_free(struct gctl_req * req)211 gctl_free(struct gctl_req *req)
212 {
213 u_int i;
214
215 sbuf_delete(req->serror);
216 if (req->arg == NULL)
217 return;
218 for (i = 0; i < req->narg; i++) {
219 if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
220 g_free(req->arg[i].name);
221 if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
222 req->arg[i].len > 0)
223 g_free(req->arg[i].kvalue);
224 }
225 g_free(req->arg);
226 }
227
228 static void
gctl_dump(struct gctl_req * req)229 gctl_dump(struct gctl_req *req)
230 {
231 struct gctl_req_arg *ap;
232 u_int i;
233 int j;
234
235 printf("Dump of gctl request at %p:\n", req);
236 if (req->nerror > 0) {
237 printf(" nerror:\t%d\n", req->nerror);
238 if (sbuf_len(req->serror) > 0)
239 printf(" error:\t\"%s\"\n", sbuf_data(req->serror));
240 }
241 if (req->arg == NULL)
242 return;
243 for (i = 0; i < req->narg; i++) {
244 ap = &req->arg[i];
245 if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
246 printf(" param:\t%d@%p", ap->nlen, ap->name);
247 else
248 printf(" param:\t\"%s\"", ap->name);
249 printf(" [%s%s%d] = ",
250 ap->flag & GCTL_PARAM_RD ? "R" : "",
251 ap->flag & GCTL_PARAM_WR ? "W" : "",
252 ap->len);
253 if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
254 printf(" =@ %p", ap->value);
255 } else if (ap->flag & GCTL_PARAM_ASCII) {
256 printf("\"%s\"", (char *)ap->kvalue);
257 } else if (ap->len > 0) {
258 for (j = 0; j < ap->len && j < 512; j++)
259 printf(" %02x", ((u_char *)ap->kvalue)[j]);
260 } else {
261 printf(" = %p", ap->kvalue);
262 }
263 printf("\n");
264 }
265 }
266
267 int
gctl_set_param(struct gctl_req * req,const char * param,void const * ptr,int len)268 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
269 int len)
270 {
271 u_int i;
272 struct gctl_req_arg *ap;
273
274 for (i = 0; i < req->narg; i++) {
275 ap = &req->arg[i];
276 if (strcmp(param, ap->name))
277 continue;
278 if (!(ap->flag & GCTL_PARAM_WR))
279 return (EPERM);
280 ap->flag |= GCTL_PARAM_CHANGED;
281 if (ap->len < len) {
282 bcopy(ptr, ap->kvalue, ap->len);
283 return (ENOSPC);
284 }
285 bcopy(ptr, ap->kvalue, len);
286 return (0);
287 }
288 return (EINVAL);
289 }
290
291 void
gctl_set_param_err(struct gctl_req * req,const char * param,void const * ptr,int len)292 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
293 int len)
294 {
295
296 switch (gctl_set_param(req, param, ptr, len)) {
297 case EPERM:
298 gctl_error(req, "No write access %s argument", param);
299 break;
300 case ENOSPC:
301 gctl_error(req, "Wrong length %s argument", param);
302 break;
303 case EINVAL:
304 gctl_error(req, "Missing %s argument", param);
305 break;
306 }
307 }
308
309 void *
gctl_get_param(struct gctl_req * req,const char * param,int * len)310 gctl_get_param(struct gctl_req *req, const char *param, int *len)
311 {
312 u_int i;
313 void *p;
314 struct gctl_req_arg *ap;
315
316 for (i = 0; i < req->narg; i++) {
317 ap = &req->arg[i];
318 if (strcmp(param, ap->name))
319 continue;
320 if (!(ap->flag & GCTL_PARAM_RD))
321 continue;
322 p = ap->kvalue;
323 if (len != NULL)
324 *len = ap->len;
325 return (p);
326 }
327 return (NULL);
328 }
329
330 char const *
gctl_get_asciiparam(struct gctl_req * req,const char * param)331 gctl_get_asciiparam(struct gctl_req *req, const char *param)
332 {
333 u_int i;
334 char const *p;
335 struct gctl_req_arg *ap;
336
337 for (i = 0; i < req->narg; i++) {
338 ap = &req->arg[i];
339 if (strcmp(param, ap->name))
340 continue;
341 if (!(ap->flag & GCTL_PARAM_RD))
342 continue;
343 p = ap->kvalue;
344 if (ap->len < 1) {
345 gctl_error(req, "No length argument (%s)", param);
346 return (NULL);
347 }
348 if (p[ap->len - 1] != '\0') {
349 gctl_error(req, "Unterminated argument (%s)", param);
350 return (NULL);
351 }
352 return (p);
353 }
354 return (NULL);
355 }
356
357 void *
gctl_get_paraml_opt(struct gctl_req * req,const char * param,int len)358 gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
359 {
360 int i;
361 void *p;
362
363 p = gctl_get_param(req, param, &i);
364 if (i != len) {
365 p = NULL;
366 gctl_error(req, "Wrong length %s argument", param);
367 }
368 return (p);
369 }
370
371 void *
gctl_get_paraml(struct gctl_req * req,const char * param,int len)372 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
373 {
374 void *p;
375
376 p = gctl_get_paraml_opt(req, param, len);
377 if (p == NULL)
378 gctl_error(req, "Missing %s argument", param);
379 return (p);
380 }
381
382 struct g_class *
gctl_get_class(struct gctl_req * req,char const * arg)383 gctl_get_class(struct gctl_req *req, char const *arg)
384 {
385 char const *p;
386 struct g_class *cp;
387
388 p = gctl_get_asciiparam(req, arg);
389 if (p == NULL) {
390 gctl_error(req, "Missing %s argument", arg);
391 return (NULL);
392 }
393 LIST_FOREACH(cp, &g_classes, class) {
394 if (!strcmp(p, cp->name))
395 return (cp);
396 }
397 gctl_error(req, "Class not found: \"%s\"", p);
398 return (NULL);
399 }
400
401 struct g_geom *
gctl_get_geom(struct gctl_req * req,struct g_class * mp,char const * arg)402 gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
403 {
404 char const *p;
405 struct g_geom *gp;
406
407 MPASS(mp != NULL);
408 p = gctl_get_asciiparam(req, arg);
409 if (p == NULL) {
410 gctl_error(req, "Missing %s argument", arg);
411 return (NULL);
412 }
413 LIST_FOREACH(gp, &mp->geom, geom)
414 if (!strcmp(p, gp->name))
415 return (gp);
416 gctl_error(req, "Geom not found: \"%s\"", p);
417 return (NULL);
418 }
419
420 struct g_provider *
gctl_get_provider(struct gctl_req * req,char const * arg)421 gctl_get_provider(struct gctl_req *req, char const *arg)
422 {
423 char const *p;
424 struct g_provider *pp;
425
426 p = gctl_get_asciiparam(req, arg);
427 if (p == NULL) {
428 gctl_error(req, "Missing '%s' argument", arg);
429 return (NULL);
430 }
431 pp = g_provider_by_name(p);
432 if (pp != NULL)
433 return (pp);
434 gctl_error(req, "Provider not found: \"%s\"", p);
435 return (NULL);
436 }
437
438 static void
g_ctl_req(void * arg,int flag __unused)439 g_ctl_req(void *arg, int flag __unused)
440 {
441 struct g_class *mp;
442 struct gctl_req *req;
443 char const *verb;
444
445 g_topology_assert();
446 req = arg;
447 mp = gctl_get_class(req, "class");
448 if (mp == NULL)
449 return;
450 if (mp->ctlreq == NULL) {
451 gctl_error(req, "Class takes no requests");
452 return;
453 }
454 verb = gctl_get_param(req, "verb", NULL);
455 if (verb == NULL) {
456 gctl_error(req, "Verb missing");
457 return;
458 }
459 mp->ctlreq(req, mp, verb);
460 g_topology_assert();
461 }
462
463 static int
g_ctl_ioctl_ctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)464 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
465 {
466 struct gctl_req *req;
467 int nerror;
468
469 req = (void *)data;
470 req->nerror = 0;
471 /* It is an error if we cannot return an error text */
472 if (req->lerror < 2)
473 return (EINVAL);
474 if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
475 return (EINVAL);
476
477 req->serror = sbuf_new_auto();
478 /* Check the version */
479 if (req->version != GCTL_VERSION) {
480 gctl_error(req, "kernel and libgeom version mismatch.");
481 req->arg = NULL;
482 } else {
483 /* Get things on board */
484 gctl_copyin(req);
485
486 if (g_debugflags & G_F_CTLDUMP)
487 gctl_dump(req);
488
489 if (!req->nerror) {
490 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
491 gctl_copyout(req);
492 }
493 }
494 if (sbuf_done(req->serror)) {
495 copyout(sbuf_data(req->serror), req->error,
496 imin(req->lerror, sbuf_len(req->serror) + 1));
497 }
498
499 nerror = req->nerror;
500 gctl_free(req);
501 return (nerror);
502 }
503
504 static int
g_ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)505 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
506 {
507 int error;
508
509 switch(cmd) {
510 case GEOM_CTL:
511 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
512 break;
513 default:
514 error = ENOIOCTL;
515 break;
516 }
517 return (error);
518
519 }
520