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