1 /*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_geom.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 #include <sys/bio.h> 46 #include <sys/conf.h> 47 #include <sys/disk.h> 48 #include <sys/malloc.h> 49 #include <sys/sysctl.h> 50 #include <sys/sbuf.h> 51 52 #include <sys/lock.h> 53 #include <sys/mutex.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_extern.h> 57 58 #include <geom/geom.h> 59 #include <geom/geom_int.h> 60 #define GCTL_TABLE 1 61 #include <geom/geom_ctl.h> 62 63 #include <machine/stdarg.h> 64 65 static d_ioctl_t g_ctl_ioctl; 66 67 static struct cdevsw g_ctl_cdevsw = { 68 .d_version = D_VERSION, 69 .d_flags = D_NEEDGIANT, 70 .d_ioctl = g_ctl_ioctl, 71 .d_name = "g_ctl", 72 }; 73 74 void 75 g_ctl_init(void) 76 { 77 78 make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL, 79 UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL); 80 KASSERT(GCTL_PARAM_RD == VM_PROT_READ, 81 ("GCTL_PARAM_RD != VM_PROT_READ")); 82 KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE, 83 ("GCTL_PARAM_WR != VM_PROT_WRITE")); 84 } 85 86 /* 87 * Report an error back to the user in ascii format. Return nerror 88 * or EINVAL if nerror isn't specified. 89 */ 90 int 91 gctl_error(struct gctl_req *req, const char *fmt, ...) 92 { 93 va_list ap; 94 95 if (req == NULL) 96 return (EINVAL); 97 98 /* We only record the first error */ 99 if (sbuf_done(req->serror)) { 100 if (!req->nerror) 101 req->nerror = EEXIST; 102 return (req->nerror); 103 } 104 if (!req->nerror) 105 req->nerror = EINVAL; 106 107 va_start(ap, fmt); 108 sbuf_vprintf(req->serror, fmt, ap); 109 va_end(ap); 110 sbuf_finish(req->serror); 111 if (g_debugflags & G_F_CTLDUMP) 112 printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror)); 113 return (req->nerror); 114 } 115 116 /* 117 * Allocate space and copyin() something. 118 * XXX: this should really be a standard function in the kernel. 119 */ 120 static void * 121 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len) 122 { 123 void *ptr; 124 125 ptr = g_malloc(len, M_WAITOK); 126 req->nerror = copyin(uaddr, ptr, len); 127 if (!req->nerror) 128 return (ptr); 129 g_free(ptr); 130 return (NULL); 131 } 132 133 static void 134 gctl_copyin(struct gctl_req *req) 135 { 136 struct gctl_req_arg *ap; 137 char *p; 138 int i; 139 140 ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap)); 141 if (ap == NULL) { 142 gctl_error(req, "bad control request"); 143 req->arg = NULL; 144 return; 145 } 146 147 /* Nothing have been copyin()'ed yet */ 148 for (i = 0; i < req->narg; i++) { 149 ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL); 150 ap[i].flag &= ~GCTL_PARAM_CHANGED; 151 ap[i].kvalue = NULL; 152 } 153 154 for (i = 0; i < req->narg; i++) { 155 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 156 gctl_error(req, 157 "wrong param name length %d: %d", i, ap[i].nlen); 158 break; 159 } 160 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen); 161 if (p == NULL) 162 break; 163 if (p[ap[i].nlen - 1] != '\0') { 164 gctl_error(req, "unterminated param name"); 165 g_free(p); 166 break; 167 } 168 ap[i].name = p; 169 ap[i].flag |= GCTL_PARAM_NAMEKERNEL; 170 if (ap[i].len <= 0) { 171 gctl_error(req, "negative param length"); 172 break; 173 } 174 p = geom_alloc_copyin(req, ap[i].value, ap[i].len); 175 if (p == NULL) 176 break; 177 if ((ap[i].flag & GCTL_PARAM_ASCII) && 178 p[ap[i].len - 1] != '\0') { 179 gctl_error(req, "unterminated param value"); 180 g_free(p); 181 break; 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 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 213 gctl_free(struct gctl_req *req) 214 { 215 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 231 gctl_dump(struct gctl_req *req) 232 { 233 struct gctl_req_arg *ap; 234 u_int i; 235 int j; 236 237 printf("Dump of gctl request at %p:\n", 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 270 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, 271 int len) 272 { 273 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 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 * 312 gctl_get_param(struct gctl_req *req, const char *param, int *len) 313 { 314 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 & GCTL_PARAM_RD)) 323 continue; 324 p = ap->kvalue; 325 if (len != NULL) 326 *len = ap->len; 327 return (p); 328 } 329 return (NULL); 330 } 331 332 char const * 333 gctl_get_asciiparam(struct gctl_req *req, const char *param) 334 { 335 int i; 336 char const *p; 337 struct gctl_req_arg *ap; 338 339 for (i = 0; i < req->narg; i++) { 340 ap = &req->arg[i]; 341 if (strcmp(param, ap->name)) 342 continue; 343 if (!(ap->flag & GCTL_PARAM_RD)) 344 continue; 345 p = ap->kvalue; 346 if (ap->len < 1) { 347 gctl_error(req, "No length argument (%s)", param); 348 return (NULL); 349 } 350 if (p[ap->len - 1] != '\0') { 351 gctl_error(req, "Unterminated argument (%s)", param); 352 return (NULL); 353 } 354 return (p); 355 } 356 return (NULL); 357 } 358 359 void * 360 gctl_get_paraml(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 (p == NULL) 367 gctl_error(req, "Missing %s argument", param); 368 else if (i != len) { 369 p = NULL; 370 gctl_error(req, "Wrong length %s argument", param); 371 } 372 return (p); 373 } 374 375 struct g_class * 376 gctl_get_class(struct gctl_req *req, char const *arg) 377 { 378 char const *p; 379 struct g_class *cp; 380 381 p = gctl_get_asciiparam(req, arg); 382 if (p == NULL) 383 return (NULL); 384 LIST_FOREACH(cp, &g_classes, class) { 385 if (!strcmp(p, cp->name)) 386 return (cp); 387 } 388 return (NULL); 389 } 390 391 struct g_geom * 392 gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg) 393 { 394 char const *p; 395 struct g_class *mp; 396 struct g_geom *gp; 397 398 p = gctl_get_asciiparam(req, arg); 399 if (p == NULL) 400 return (NULL); 401 LIST_FOREACH(mp, &g_classes, class) { 402 if (mpr != NULL && mpr != mp) 403 continue; 404 LIST_FOREACH(gp, &mp->geom, geom) { 405 if (!strcmp(p, gp->name)) 406 return (gp); 407 } 408 } 409 gctl_error(req, "Geom not found: \"%s\"", p); 410 return (NULL); 411 } 412 413 struct g_provider * 414 gctl_get_provider(struct gctl_req *req, char const *arg) 415 { 416 char const *p; 417 struct g_provider *pp; 418 419 p = gctl_get_asciiparam(req, arg); 420 if (p == NULL) 421 return (NULL); 422 pp = g_provider_by_name(p); 423 if (pp != NULL) 424 return (pp); 425 gctl_error(req, "Provider not found: \"%s\"", p); 426 return (NULL); 427 } 428 429 static void 430 g_ctl_req(void *arg, int flag __unused) 431 { 432 struct g_class *mp; 433 struct gctl_req *req; 434 char const *verb; 435 436 g_topology_assert(); 437 req = arg; 438 mp = gctl_get_class(req, "class"); 439 if (mp == NULL) { 440 gctl_error(req, "Class not found"); 441 return; 442 } 443 if (mp->ctlreq == NULL) { 444 gctl_error(req, "Class takes no requests"); 445 return; 446 } 447 verb = gctl_get_param(req, "verb", NULL); 448 if (verb == NULL) { 449 gctl_error(req, "Verb missing"); 450 return; 451 } 452 mp->ctlreq(req, mp, verb); 453 g_topology_assert(); 454 } 455 456 457 static int 458 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 459 { 460 struct gctl_req *req; 461 int nerror; 462 463 req = (void *)data; 464 req->nerror = 0; 465 /* It is an error if we cannot return an error text */ 466 if (req->lerror < 2) 467 return (EINVAL); 468 if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 469 return (EINVAL); 470 471 req->serror = sbuf_new_auto(); 472 /* Check the version */ 473 if (req->version != GCTL_VERSION) { 474 gctl_error(req, "kernel and libgeom version mismatch."); 475 req->arg = NULL; 476 } else { 477 /* Get things on board */ 478 gctl_copyin(req); 479 480 if (g_debugflags & G_F_CTLDUMP) 481 gctl_dump(req); 482 483 if (!req->nerror) { 484 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL); 485 gctl_copyout(req); 486 } 487 } 488 if (sbuf_done(req->serror)) { 489 copyout(sbuf_data(req->serror), req->error, 490 imin(req->lerror, sbuf_len(req->serror) + 1)); 491 } 492 493 nerror = req->nerror; 494 gctl_free(req); 495 return (nerror); 496 } 497 498 static int 499 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 500 { 501 int error; 502 503 switch(cmd) { 504 case GEOM_CTL: 505 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 506 break; 507 default: 508 error = ENOIOCTL; 509 break; 510 } 511 return (error); 512 513 } 514