1 /*- 2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/cons.h> 33 #include <sys/kernel.h> 34 #include <sys/linker.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/bio.h> 39 #include <sys/sbuf.h> 40 #include <sys/sysctl.h> 41 #include <sys/malloc.h> 42 #include <sys/eventhandler.h> 43 #include <sys/kthread.h> 44 #include <sys/proc.h> 45 #include <sys/sched.h> 46 #include <sys/smp.h> 47 #include <sys/uio.h> 48 #include <sys/vnode.h> 49 50 #include <vm/uma.h> 51 52 #include <geom/geom.h> 53 #include <geom/eli/g_eli.h> 54 #include <geom/eli/pkcs5v2.h> 55 56 FEATURE(geom_eli, "GEOM crypto module"); 57 58 MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 59 60 SYSCTL_DECL(_kern_geom); 61 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff"); 62 static int g_eli_version = G_ELI_VERSION; 63 SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 64 "GELI version"); 65 int g_eli_debug = 0; 66 SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 67 "Debug level"); 68 static u_int g_eli_tries = 3; 69 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 70 "Number of tries for entering the passphrase"); 71 static u_int g_eli_visible_passphrase = GETS_NOECHO; 72 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 73 &g_eli_visible_passphrase, 0, 74 "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 75 u_int g_eli_overwrites = G_ELI_OVERWRITES; 76 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 77 0, "Number of times on-disk keys should be overwritten when destroying them"); 78 static u_int g_eli_threads = 0; 79 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 80 "Number of threads doing crypto work"); 81 u_int g_eli_batch = 0; 82 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 83 "Use crypto operations batching"); 84 85 /* 86 * Passphrase cached during boot, in order to be more user-friendly if 87 * there are multiple providers using the same passphrase. 88 */ 89 static char cached_passphrase[256]; 90 static u_int g_eli_boot_passcache = 1; 91 TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 92 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 93 &g_eli_boot_passcache, 0, 94 "Passphrases are cached during boot process for possible reuse"); 95 static void 96 fetch_loader_passphrase(void * dummy) 97 { 98 char * env_passphrase; 99 100 KASSERT(dynamic_kenv, ("need dynamic kenv")); 101 102 if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 103 /* Extract passphrase from the environment. */ 104 strlcpy(cached_passphrase, env_passphrase, 105 sizeof(cached_passphrase)); 106 freeenv(env_passphrase); 107 108 /* Wipe the passphrase from the environment. */ 109 kern_unsetenv("kern.geom.eli.passphrase"); 110 } 111 } 112 SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 113 fetch_loader_passphrase, NULL); 114 static void 115 zero_boot_passcache(void * dummy) 116 { 117 118 memset(cached_passphrase, 0, sizeof(cached_passphrase)); 119 } 120 EVENTHANDLER_DEFINE(mountroot, zero_boot_passcache, NULL, 0); 121 122 static eventhandler_tag g_eli_pre_sync = NULL; 123 124 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 125 struct g_geom *gp); 126 static void g_eli_init(struct g_class *mp); 127 static void g_eli_fini(struct g_class *mp); 128 129 static g_taste_t g_eli_taste; 130 static g_dumpconf_t g_eli_dumpconf; 131 132 struct g_class g_eli_class = { 133 .name = G_ELI_CLASS_NAME, 134 .version = G_VERSION, 135 .ctlreq = g_eli_config, 136 .taste = g_eli_taste, 137 .destroy_geom = g_eli_destroy_geom, 138 .init = g_eli_init, 139 .fini = g_eli_fini 140 }; 141 142 143 /* 144 * Code paths: 145 * BIO_READ: 146 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 147 * BIO_WRITE: 148 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 149 */ 150 151 152 /* 153 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 154 * accelerator or something like this. 155 * The function updates the SID and rerun the operation. 156 */ 157 int 158 g_eli_crypto_rerun(struct cryptop *crp) 159 { 160 struct g_eli_softc *sc; 161 struct g_eli_worker *wr; 162 struct bio *bp; 163 int error; 164 165 bp = (struct bio *)crp->crp_opaque; 166 sc = bp->bio_to->geom->softc; 167 LIST_FOREACH(wr, &sc->sc_workers, w_next) { 168 if (wr->w_number == bp->bio_pflags) 169 break; 170 } 171 KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 172 G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).", 173 bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid, 174 (uintmax_t)crp->crp_sid); 175 wr->w_sid = crp->crp_sid; 176 crp->crp_etype = 0; 177 error = crypto_dispatch(crp); 178 if (error == 0) 179 return (0); 180 G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 181 crp->crp_etype = error; 182 return (error); 183 } 184 185 /* 186 * The function is called afer reading encrypted data from the provider. 187 * 188 * g_eli_start -> g_eli_crypto_read -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 189 */ 190 void 191 g_eli_read_done(struct bio *bp) 192 { 193 struct g_eli_softc *sc; 194 struct bio *pbp; 195 196 G_ELI_LOGREQ(2, bp, "Request done."); 197 pbp = bp->bio_parent; 198 if (pbp->bio_error == 0 && bp->bio_error != 0) 199 pbp->bio_error = bp->bio_error; 200 g_destroy_bio(bp); 201 /* 202 * Do we have all sectors already? 203 */ 204 pbp->bio_inbed++; 205 if (pbp->bio_inbed < pbp->bio_children) 206 return; 207 sc = pbp->bio_to->geom->softc; 208 if (pbp->bio_error != 0) { 209 G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 210 pbp->bio_error); 211 pbp->bio_completed = 0; 212 if (pbp->bio_driver2 != NULL) { 213 free(pbp->bio_driver2, M_ELI); 214 pbp->bio_driver2 = NULL; 215 } 216 g_io_deliver(pbp, pbp->bio_error); 217 atomic_subtract_int(&sc->sc_inflight, 1); 218 return; 219 } 220 mtx_lock(&sc->sc_queue_mtx); 221 bioq_insert_tail(&sc->sc_queue, pbp); 222 mtx_unlock(&sc->sc_queue_mtx); 223 wakeup(sc); 224 } 225 226 /* 227 * The function is called after we encrypt and write data. 228 * 229 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 230 */ 231 void 232 g_eli_write_done(struct bio *bp) 233 { 234 struct g_eli_softc *sc; 235 struct bio *pbp; 236 237 G_ELI_LOGREQ(2, bp, "Request done."); 238 pbp = bp->bio_parent; 239 if (pbp->bio_error == 0 && bp->bio_error != 0) 240 pbp->bio_error = bp->bio_error; 241 g_destroy_bio(bp); 242 /* 243 * Do we have all sectors already? 244 */ 245 pbp->bio_inbed++; 246 if (pbp->bio_inbed < pbp->bio_children) 247 return; 248 free(pbp->bio_driver2, M_ELI); 249 pbp->bio_driver2 = NULL; 250 if (pbp->bio_error != 0) { 251 G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 252 pbp->bio_error); 253 pbp->bio_completed = 0; 254 } else 255 pbp->bio_completed = pbp->bio_length; 256 257 /* 258 * Write is finished, send it up. 259 */ 260 sc = pbp->bio_to->geom->softc; 261 g_io_deliver(pbp, pbp->bio_error); 262 atomic_subtract_int(&sc->sc_inflight, 1); 263 } 264 265 /* 266 * This function should never be called, but GEOM made as it set ->orphan() 267 * method for every geom. 268 */ 269 static void 270 g_eli_orphan_spoil_assert(struct g_consumer *cp) 271 { 272 273 panic("Function %s() called for %s.", __func__, cp->geom->name); 274 } 275 276 static void 277 g_eli_orphan(struct g_consumer *cp) 278 { 279 struct g_eli_softc *sc; 280 281 g_topology_assert(); 282 sc = cp->geom->softc; 283 if (sc == NULL) 284 return; 285 g_eli_destroy(sc, TRUE); 286 } 287 288 /* 289 * BIO_READ: 290 * G_ELI_START -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 291 * BIO_WRITE: 292 * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 293 */ 294 static void 295 g_eli_start(struct bio *bp) 296 { 297 struct g_eli_softc *sc; 298 struct g_consumer *cp; 299 struct bio *cbp; 300 301 sc = bp->bio_to->geom->softc; 302 KASSERT(sc != NULL, 303 ("Provider's error should be set (error=%d)(device=%s).", 304 bp->bio_to->error, bp->bio_to->name)); 305 G_ELI_LOGREQ(2, bp, "Request received."); 306 307 switch (bp->bio_cmd) { 308 case BIO_READ: 309 case BIO_WRITE: 310 case BIO_GETATTR: 311 case BIO_FLUSH: 312 break; 313 case BIO_DELETE: 314 /* 315 * If the user hasn't set the NODELETE flag, we just pass 316 * it down the stack and let the layers beneath us do (or 317 * not) whatever they do with it. If they have, we 318 * reject it. A possible extension would be an 319 * additional flag to take it as a hint to shred the data 320 * with [multiple?] overwrites. 321 */ 322 if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 323 break; 324 default: 325 g_io_deliver(bp, EOPNOTSUPP); 326 return; 327 } 328 cbp = g_clone_bio(bp); 329 if (cbp == NULL) { 330 g_io_deliver(bp, ENOMEM); 331 return; 332 } 333 bp->bio_driver1 = cbp; 334 bp->bio_pflags = G_ELI_NEW_BIO; 335 switch (bp->bio_cmd) { 336 case BIO_READ: 337 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 338 g_eli_crypto_read(sc, bp, 0); 339 break; 340 } 341 /* FALLTHROUGH */ 342 case BIO_WRITE: 343 mtx_lock(&sc->sc_queue_mtx); 344 bioq_insert_tail(&sc->sc_queue, bp); 345 mtx_unlock(&sc->sc_queue_mtx); 346 wakeup(sc); 347 break; 348 case BIO_GETATTR: 349 case BIO_FLUSH: 350 case BIO_DELETE: 351 cbp->bio_done = g_std_done; 352 cp = LIST_FIRST(&sc->sc_geom->consumer); 353 cbp->bio_to = cp->provider; 354 G_ELI_LOGREQ(2, cbp, "Sending request."); 355 g_io_request(cbp, cp); 356 break; 357 } 358 } 359 360 static int 361 g_eli_newsession(struct g_eli_worker *wr) 362 { 363 struct g_eli_softc *sc; 364 struct cryptoini crie, cria; 365 int error; 366 367 sc = wr->w_softc; 368 369 bzero(&crie, sizeof(crie)); 370 crie.cri_alg = sc->sc_ealgo; 371 crie.cri_klen = sc->sc_ekeylen; 372 if (sc->sc_ealgo == CRYPTO_AES_XTS) 373 crie.cri_klen <<= 1; 374 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 375 crie.cri_key = g_eli_key_hold(sc, 0, 376 LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 377 } else { 378 crie.cri_key = sc->sc_ekey; 379 } 380 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 381 bzero(&cria, sizeof(cria)); 382 cria.cri_alg = sc->sc_aalgo; 383 cria.cri_klen = sc->sc_akeylen; 384 cria.cri_key = sc->sc_akey; 385 crie.cri_next = &cria; 386 } 387 388 switch (sc->sc_crypto) { 389 case G_ELI_CRYPTO_SW: 390 error = crypto_newsession(&wr->w_sid, &crie, 391 CRYPTOCAP_F_SOFTWARE); 392 break; 393 case G_ELI_CRYPTO_HW: 394 error = crypto_newsession(&wr->w_sid, &crie, 395 CRYPTOCAP_F_HARDWARE); 396 break; 397 case G_ELI_CRYPTO_UNKNOWN: 398 error = crypto_newsession(&wr->w_sid, &crie, 399 CRYPTOCAP_F_HARDWARE); 400 if (error == 0) { 401 mtx_lock(&sc->sc_queue_mtx); 402 if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 403 sc->sc_crypto = G_ELI_CRYPTO_HW; 404 mtx_unlock(&sc->sc_queue_mtx); 405 } else { 406 error = crypto_newsession(&wr->w_sid, &crie, 407 CRYPTOCAP_F_SOFTWARE); 408 mtx_lock(&sc->sc_queue_mtx); 409 if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 410 sc->sc_crypto = G_ELI_CRYPTO_SW; 411 mtx_unlock(&sc->sc_queue_mtx); 412 } 413 break; 414 default: 415 panic("%s: invalid condition", __func__); 416 } 417 418 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) 419 g_eli_key_drop(sc, crie.cri_key); 420 421 return (error); 422 } 423 424 static void 425 g_eli_freesession(struct g_eli_worker *wr) 426 { 427 428 crypto_freesession(wr->w_sid); 429 } 430 431 static void 432 g_eli_cancel(struct g_eli_softc *sc) 433 { 434 struct bio *bp; 435 436 mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 437 438 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 439 KASSERT(bp->bio_pflags == G_ELI_NEW_BIO, 440 ("Not new bio when canceling (bp=%p).", bp)); 441 g_io_deliver(bp, ENXIO); 442 } 443 } 444 445 static struct bio * 446 g_eli_takefirst(struct g_eli_softc *sc) 447 { 448 struct bio *bp; 449 450 mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 451 452 if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 453 return (bioq_takefirst(&sc->sc_queue)); 454 /* 455 * Device suspended, so we skip new I/O requests. 456 */ 457 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 458 if (bp->bio_pflags != G_ELI_NEW_BIO) 459 break; 460 } 461 if (bp != NULL) 462 bioq_remove(&sc->sc_queue, bp); 463 return (bp); 464 } 465 466 /* 467 * This is the main function for kernel worker thread when we don't have 468 * hardware acceleration and we have to do cryptography in software. 469 * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 470 * threads with crypto work. 471 */ 472 static void 473 g_eli_worker(void *arg) 474 { 475 struct g_eli_softc *sc; 476 struct g_eli_worker *wr; 477 struct bio *bp; 478 int error; 479 480 wr = arg; 481 sc = wr->w_softc; 482 #ifdef EARLY_AP_STARTUP 483 MPASS(!sc->sc_cpubind || smp_started); 484 #elif defined(SMP) 485 /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 486 if (sc->sc_cpubind) { 487 while (!smp_started) 488 tsleep(wr, 0, "geli:smp", hz / 4); 489 } 490 #endif 491 thread_lock(curthread); 492 sched_prio(curthread, PUSER); 493 if (sc->sc_cpubind) 494 sched_bind(curthread, wr->w_number % mp_ncpus); 495 thread_unlock(curthread); 496 497 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 498 499 for (;;) { 500 mtx_lock(&sc->sc_queue_mtx); 501 again: 502 bp = g_eli_takefirst(sc); 503 if (bp == NULL) { 504 if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 505 g_eli_cancel(sc); 506 LIST_REMOVE(wr, w_next); 507 g_eli_freesession(wr); 508 free(wr, M_ELI); 509 G_ELI_DEBUG(1, "Thread %s exiting.", 510 curthread->td_proc->p_comm); 511 wakeup(&sc->sc_workers); 512 mtx_unlock(&sc->sc_queue_mtx); 513 kproc_exit(0); 514 } 515 while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 516 if (sc->sc_inflight > 0) { 517 G_ELI_DEBUG(0, "inflight=%d", 518 sc->sc_inflight); 519 /* 520 * We still have inflight BIOs, so 521 * sleep and retry. 522 */ 523 msleep(sc, &sc->sc_queue_mtx, PRIBIO, 524 "geli:inf", hz / 5); 525 goto again; 526 } 527 /* 528 * Suspend requested, mark the worker as 529 * suspended and go to sleep. 530 */ 531 if (wr->w_active) { 532 g_eli_freesession(wr); 533 wr->w_active = FALSE; 534 } 535 wakeup(&sc->sc_workers); 536 msleep(sc, &sc->sc_queue_mtx, PRIBIO, 537 "geli:suspend", 0); 538 if (!wr->w_active && 539 !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 540 error = g_eli_newsession(wr); 541 KASSERT(error == 0, 542 ("g_eli_newsession() failed on resume (error=%d)", 543 error)); 544 wr->w_active = TRUE; 545 } 546 goto again; 547 } 548 msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 549 continue; 550 } 551 if (bp->bio_pflags == G_ELI_NEW_BIO) 552 atomic_add_int(&sc->sc_inflight, 1); 553 mtx_unlock(&sc->sc_queue_mtx); 554 if (bp->bio_pflags == G_ELI_NEW_BIO) { 555 bp->bio_pflags = 0; 556 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 557 if (bp->bio_cmd == BIO_READ) 558 g_eli_auth_read(sc, bp); 559 else 560 g_eli_auth_run(wr, bp); 561 } else { 562 if (bp->bio_cmd == BIO_READ) 563 g_eli_crypto_read(sc, bp, 1); 564 else 565 g_eli_crypto_run(wr, bp); 566 } 567 } else { 568 if (sc->sc_flags & G_ELI_FLAG_AUTH) 569 g_eli_auth_run(wr, bp); 570 else 571 g_eli_crypto_run(wr, bp); 572 } 573 } 574 } 575 576 int 577 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 578 struct g_eli_metadata *md) 579 { 580 struct g_geom *gp; 581 struct g_consumer *cp; 582 u_char *buf = NULL; 583 int error; 584 585 g_topology_assert(); 586 587 gp = g_new_geomf(mp, "eli:taste"); 588 gp->start = g_eli_start; 589 gp->access = g_std_access; 590 /* 591 * g_eli_read_metadata() is always called from the event thread. 592 * Our geom is created and destroyed in the same event, so there 593 * could be no orphan nor spoil event in the meantime. 594 */ 595 gp->orphan = g_eli_orphan_spoil_assert; 596 gp->spoiled = g_eli_orphan_spoil_assert; 597 cp = g_new_consumer(gp); 598 error = g_attach(cp, pp); 599 if (error != 0) 600 goto end; 601 error = g_access(cp, 1, 0, 0); 602 if (error != 0) 603 goto end; 604 g_topology_unlock(); 605 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 606 &error); 607 g_topology_lock(); 608 if (buf == NULL) 609 goto end; 610 error = eli_metadata_decode(buf, md); 611 if (error != 0) 612 goto end; 613 /* Metadata was read and decoded successfully. */ 614 end: 615 if (buf != NULL) 616 g_free(buf); 617 if (cp->provider != NULL) { 618 if (cp->acr == 1) 619 g_access(cp, -1, 0, 0); 620 g_detach(cp); 621 } 622 g_destroy_consumer(cp); 623 g_destroy_geom(gp); 624 return (error); 625 } 626 627 /* 628 * The function is called when we had last close on provider and user requested 629 * to close it when this situation occur. 630 */ 631 static void 632 g_eli_last_close(void *arg, int flags __unused) 633 { 634 struct g_geom *gp; 635 char gpname[64]; 636 int error; 637 638 g_topology_assert(); 639 gp = arg; 640 strlcpy(gpname, gp->name, sizeof(gpname)); 641 error = g_eli_destroy(gp->softc, TRUE); 642 KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 643 gpname, error)); 644 G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 645 } 646 647 int 648 g_eli_access(struct g_provider *pp, int dr, int dw, int de) 649 { 650 struct g_eli_softc *sc; 651 struct g_geom *gp; 652 653 gp = pp->geom; 654 sc = gp->softc; 655 656 if (dw > 0) { 657 if (sc->sc_flags & G_ELI_FLAG_RO) { 658 /* Deny write attempts. */ 659 return (EROFS); 660 } 661 /* Someone is opening us for write, we need to remember that. */ 662 sc->sc_flags |= G_ELI_FLAG_WOPEN; 663 return (0); 664 } 665 /* Is this the last close? */ 666 if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 667 return (0); 668 669 /* 670 * Automatically detach on last close if requested. 671 */ 672 if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 673 (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 674 g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 675 } 676 return (0); 677 } 678 679 static int 680 g_eli_cpu_is_disabled(int cpu) 681 { 682 #ifdef SMP 683 return (CPU_ISSET(cpu, &hlt_cpus_mask)); 684 #else 685 return (0); 686 #endif 687 } 688 689 struct g_geom * 690 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 691 const struct g_eli_metadata *md, const u_char *mkey, int nkey) 692 { 693 struct g_eli_softc *sc; 694 struct g_eli_worker *wr; 695 struct g_geom *gp; 696 struct g_provider *pp; 697 struct g_consumer *cp; 698 u_int i, threads; 699 int error; 700 701 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 702 703 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 704 sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 705 gp->start = g_eli_start; 706 /* 707 * Spoiling can happen even though we have the provider open 708 * exclusively, e.g. through media change events. 709 */ 710 gp->spoiled = g_eli_orphan; 711 gp->orphan = g_eli_orphan; 712 gp->dumpconf = g_eli_dumpconf; 713 /* 714 * If detach-on-last-close feature is not enabled and we don't operate 715 * on read-only provider, we can simply use g_std_access(). 716 */ 717 if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 718 gp->access = g_eli_access; 719 else 720 gp->access = g_std_access; 721 722 eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize); 723 sc->sc_nkey = nkey; 724 725 gp->softc = sc; 726 sc->sc_geom = gp; 727 728 bioq_init(&sc->sc_queue); 729 mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 730 mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 731 732 pp = NULL; 733 cp = g_new_consumer(gp); 734 error = g_attach(cp, bpp); 735 if (error != 0) { 736 if (req != NULL) { 737 gctl_error(req, "Cannot attach to %s (error=%d).", 738 bpp->name, error); 739 } else { 740 G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 741 bpp->name, error); 742 } 743 goto failed; 744 } 745 /* 746 * Keep provider open all the time, so we can run critical tasks, 747 * like Master Keys deletion, without wondering if we can open 748 * provider or not. 749 * We don't open provider for writing only when user requested read-only 750 * access. 751 */ 752 if (sc->sc_flags & G_ELI_FLAG_RO) 753 error = g_access(cp, 1, 0, 1); 754 else 755 error = g_access(cp, 1, 1, 1); 756 if (error != 0) { 757 if (req != NULL) { 758 gctl_error(req, "Cannot access %s (error=%d).", 759 bpp->name, error); 760 } else { 761 G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 762 bpp->name, error); 763 } 764 goto failed; 765 } 766 767 /* 768 * Remember the keys in our softc structure. 769 */ 770 g_eli_mkey_propagate(sc, mkey); 771 772 LIST_INIT(&sc->sc_workers); 773 774 threads = g_eli_threads; 775 if (threads == 0) 776 threads = mp_ncpus; 777 sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 778 for (i = 0; i < threads; i++) { 779 if (g_eli_cpu_is_disabled(i)) { 780 G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 781 bpp->name, i); 782 continue; 783 } 784 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 785 wr->w_softc = sc; 786 wr->w_number = i; 787 wr->w_active = TRUE; 788 789 error = g_eli_newsession(wr); 790 if (error != 0) { 791 free(wr, M_ELI); 792 if (req != NULL) { 793 gctl_error(req, "Cannot set up crypto session " 794 "for %s (error=%d).", bpp->name, error); 795 } else { 796 G_ELI_DEBUG(1, "Cannot set up crypto session " 797 "for %s (error=%d).", bpp->name, error); 798 } 799 goto failed; 800 } 801 802 error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 803 "g_eli[%u] %s", i, bpp->name); 804 if (error != 0) { 805 g_eli_freesession(wr); 806 free(wr, M_ELI); 807 if (req != NULL) { 808 gctl_error(req, "Cannot create kernel thread " 809 "for %s (error=%d).", bpp->name, error); 810 } else { 811 G_ELI_DEBUG(1, "Cannot create kernel thread " 812 "for %s (error=%d).", bpp->name, error); 813 } 814 goto failed; 815 } 816 LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 817 } 818 819 /* 820 * Create decrypted provider. 821 */ 822 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 823 pp->mediasize = sc->sc_mediasize; 824 pp->sectorsize = sc->sc_sectorsize; 825 826 g_error_provider(pp, 0); 827 828 G_ELI_DEBUG(0, "Device %s created.", pp->name); 829 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 830 sc->sc_ekeylen); 831 if (sc->sc_flags & G_ELI_FLAG_AUTH) 832 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 833 G_ELI_DEBUG(0, " Crypto: %s", 834 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 835 return (gp); 836 failed: 837 mtx_lock(&sc->sc_queue_mtx); 838 sc->sc_flags |= G_ELI_FLAG_DESTROY; 839 wakeup(sc); 840 /* 841 * Wait for kernel threads self destruction. 842 */ 843 while (!LIST_EMPTY(&sc->sc_workers)) { 844 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 845 "geli:destroy", 0); 846 } 847 mtx_destroy(&sc->sc_queue_mtx); 848 if (cp->provider != NULL) { 849 if (cp->acr == 1) 850 g_access(cp, -1, -1, -1); 851 g_detach(cp); 852 } 853 g_destroy_consumer(cp); 854 g_destroy_geom(gp); 855 g_eli_key_destroy(sc); 856 bzero(sc, sizeof(*sc)); 857 free(sc, M_ELI); 858 return (NULL); 859 } 860 861 int 862 g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 863 { 864 struct g_geom *gp; 865 struct g_provider *pp; 866 867 g_topology_assert(); 868 869 if (sc == NULL) 870 return (ENXIO); 871 872 gp = sc->sc_geom; 873 pp = LIST_FIRST(&gp->provider); 874 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 875 if (force) { 876 G_ELI_DEBUG(1, "Device %s is still open, so it " 877 "cannot be definitely removed.", pp->name); 878 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 879 gp->access = g_eli_access; 880 g_wither_provider(pp, ENXIO); 881 return (EBUSY); 882 } else { 883 G_ELI_DEBUG(1, 884 "Device %s is still open (r%dw%de%d).", pp->name, 885 pp->acr, pp->acw, pp->ace); 886 return (EBUSY); 887 } 888 } 889 890 mtx_lock(&sc->sc_queue_mtx); 891 sc->sc_flags |= G_ELI_FLAG_DESTROY; 892 wakeup(sc); 893 while (!LIST_EMPTY(&sc->sc_workers)) { 894 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 895 "geli:destroy", 0); 896 } 897 mtx_destroy(&sc->sc_queue_mtx); 898 gp->softc = NULL; 899 g_eli_key_destroy(sc); 900 bzero(sc, sizeof(*sc)); 901 free(sc, M_ELI); 902 903 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 904 G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 905 g_wither_geom_close(gp, ENXIO); 906 907 return (0); 908 } 909 910 static int 911 g_eli_destroy_geom(struct gctl_req *req __unused, 912 struct g_class *mp __unused, struct g_geom *gp) 913 { 914 struct g_eli_softc *sc; 915 916 sc = gp->softc; 917 return (g_eli_destroy(sc, FALSE)); 918 } 919 920 static int 921 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 922 { 923 u_char *keyfile, *data; 924 char *file, name[64]; 925 size_t size; 926 int i; 927 928 for (i = 0; ; i++) { 929 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 930 keyfile = preload_search_by_type(name); 931 if (keyfile == NULL && i == 0) { 932 /* 933 * If there is only one keyfile, allow simpler name. 934 */ 935 snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 936 keyfile = preload_search_by_type(name); 937 } 938 if (keyfile == NULL) 939 return (i); /* Return number of loaded keyfiles. */ 940 data = preload_fetch_addr(keyfile); 941 if (data == NULL) { 942 G_ELI_DEBUG(0, "Cannot find key file data for %s.", 943 name); 944 return (0); 945 } 946 size = preload_fetch_size(keyfile); 947 if (size == 0) { 948 G_ELI_DEBUG(0, "Cannot find key file size for %s.", 949 name); 950 return (0); 951 } 952 file = preload_search_info(keyfile, MODINFO_NAME); 953 if (file == NULL) { 954 G_ELI_DEBUG(0, "Cannot find key file name for %s.", 955 name); 956 return (0); 957 } 958 G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 959 provider, name); 960 g_eli_crypto_hmac_update(ctx, data, size); 961 } 962 } 963 964 static void 965 g_eli_keyfiles_clear(const char *provider) 966 { 967 u_char *keyfile, *data; 968 char name[64]; 969 size_t size; 970 int i; 971 972 for (i = 0; ; i++) { 973 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 974 keyfile = preload_search_by_type(name); 975 if (keyfile == NULL) 976 return; 977 data = preload_fetch_addr(keyfile); 978 size = preload_fetch_size(keyfile); 979 if (data != NULL && size != 0) 980 bzero(data, size); 981 } 982 } 983 984 /* 985 * Tasting is only made on boot. 986 * We detect providers which should be attached before root is mounted. 987 */ 988 static struct g_geom * 989 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 990 { 991 struct g_eli_metadata md; 992 struct g_geom *gp; 993 struct hmac_ctx ctx; 994 char passphrase[256]; 995 u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 996 u_int i, nkey, nkeyfiles, tries; 997 int error; 998 999 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1000 g_topology_assert(); 1001 1002 if (root_mounted() || g_eli_tries == 0) 1003 return (NULL); 1004 1005 G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1006 1007 error = g_eli_read_metadata(mp, pp, &md); 1008 if (error != 0) 1009 return (NULL); 1010 gp = NULL; 1011 1012 if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1013 return (NULL); 1014 if (md.md_version > G_ELI_VERSION) { 1015 printf("geom_eli.ko module is too old to handle %s.\n", 1016 pp->name); 1017 return (NULL); 1018 } 1019 if (md.md_provsize != pp->mediasize) 1020 return (NULL); 1021 /* Should we attach it on boot? */ 1022 if (!(md.md_flags & G_ELI_FLAG_BOOT)) 1023 return (NULL); 1024 if (md.md_keys == 0x00) { 1025 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1026 return (NULL); 1027 } 1028 if (md.md_iterations == -1) { 1029 /* If there is no passphrase, we try only once. */ 1030 tries = 1; 1031 } else { 1032 /* Ask for the passphrase no more than g_eli_tries times. */ 1033 tries = g_eli_tries; 1034 } 1035 1036 for (i = 0; i <= tries; i++) { 1037 g_eli_crypto_hmac_init(&ctx, NULL, 0); 1038 1039 /* 1040 * Load all key files. 1041 */ 1042 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 1043 1044 if (nkeyfiles == 0 && md.md_iterations == -1) { 1045 /* 1046 * No key files and no passphrase, something is 1047 * definitely wrong here. 1048 * geli(8) doesn't allow for such situation, so assume 1049 * that there was really no passphrase and in that case 1050 * key files are no properly defined in loader.conf. 1051 */ 1052 G_ELI_DEBUG(0, 1053 "Found no key files in loader.conf for %s.", 1054 pp->name); 1055 return (NULL); 1056 } 1057 1058 /* Ask for the passphrase if defined. */ 1059 if (md.md_iterations >= 0) { 1060 /* Try first with cached passphrase. */ 1061 if (i == 0) { 1062 if (!g_eli_boot_passcache) 1063 continue; 1064 memcpy(passphrase, cached_passphrase, 1065 sizeof(passphrase)); 1066 } else { 1067 printf("Enter passphrase for %s: ", pp->name); 1068 cngets(passphrase, sizeof(passphrase), 1069 g_eli_visible_passphrase); 1070 memcpy(cached_passphrase, passphrase, 1071 sizeof(passphrase)); 1072 } 1073 } 1074 1075 /* 1076 * Prepare Derived-Key from the user passphrase. 1077 */ 1078 if (md.md_iterations == 0) { 1079 g_eli_crypto_hmac_update(&ctx, md.md_salt, 1080 sizeof(md.md_salt)); 1081 g_eli_crypto_hmac_update(&ctx, passphrase, 1082 strlen(passphrase)); 1083 bzero(passphrase, sizeof(passphrase)); 1084 } else if (md.md_iterations > 0) { 1085 u_char dkey[G_ELI_USERKEYLEN]; 1086 1087 pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1088 sizeof(md.md_salt), passphrase, md.md_iterations); 1089 bzero(passphrase, sizeof(passphrase)); 1090 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1091 bzero(dkey, sizeof(dkey)); 1092 } 1093 1094 g_eli_crypto_hmac_final(&ctx, key, 0); 1095 1096 /* 1097 * Decrypt Master-Key. 1098 */ 1099 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 1100 bzero(key, sizeof(key)); 1101 if (error == -1) { 1102 if (i == tries) { 1103 G_ELI_DEBUG(0, 1104 "Wrong key for %s. No tries left.", 1105 pp->name); 1106 g_eli_keyfiles_clear(pp->name); 1107 return (NULL); 1108 } 1109 if (i > 0) { 1110 G_ELI_DEBUG(0, 1111 "Wrong key for %s. Tries left: %u.", 1112 pp->name, tries - i); 1113 } 1114 /* Try again. */ 1115 continue; 1116 } else if (error > 0) { 1117 G_ELI_DEBUG(0, 1118 "Cannot decrypt Master Key for %s (error=%d).", 1119 pp->name, error); 1120 g_eli_keyfiles_clear(pp->name); 1121 return (NULL); 1122 } 1123 g_eli_keyfiles_clear(pp->name); 1124 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1125 break; 1126 } 1127 1128 /* 1129 * We have correct key, let's attach provider. 1130 */ 1131 gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 1132 bzero(mkey, sizeof(mkey)); 1133 bzero(&md, sizeof(md)); 1134 if (gp == NULL) { 1135 G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1136 G_ELI_SUFFIX); 1137 return (NULL); 1138 } 1139 return (gp); 1140 } 1141 1142 static void 1143 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1144 struct g_consumer *cp, struct g_provider *pp) 1145 { 1146 struct g_eli_softc *sc; 1147 1148 g_topology_assert(); 1149 sc = gp->softc; 1150 if (sc == NULL) 1151 return; 1152 if (pp != NULL || cp != NULL) 1153 return; /* Nothing here. */ 1154 1155 sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 1156 (uintmax_t)sc->sc_ekeys_total); 1157 sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 1158 (uintmax_t)sc->sc_ekeys_allocated); 1159 sbuf_printf(sb, "%s<Flags>", indent); 1160 if (sc->sc_flags == 0) 1161 sbuf_printf(sb, "NONE"); 1162 else { 1163 int first = 1; 1164 1165 #define ADD_FLAG(flag, name) do { \ 1166 if (sc->sc_flags & (flag)) { \ 1167 if (!first) \ 1168 sbuf_printf(sb, ", "); \ 1169 else \ 1170 first = 0; \ 1171 sbuf_printf(sb, name); \ 1172 } \ 1173 } while (0) 1174 ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1175 ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 1176 ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1177 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1178 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1179 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1180 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1181 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1182 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1183 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 1184 ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 1185 ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1186 ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT"); 1187 #undef ADD_FLAG 1188 } 1189 sbuf_printf(sb, "</Flags>\n"); 1190 1191 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1192 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1193 sc->sc_nkey); 1194 } 1195 sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1196 sbuf_printf(sb, "%s<Crypto>", indent); 1197 switch (sc->sc_crypto) { 1198 case G_ELI_CRYPTO_HW: 1199 sbuf_printf(sb, "hardware"); 1200 break; 1201 case G_ELI_CRYPTO_SW: 1202 sbuf_printf(sb, "software"); 1203 break; 1204 default: 1205 sbuf_printf(sb, "UNKNOWN"); 1206 break; 1207 } 1208 sbuf_printf(sb, "</Crypto>\n"); 1209 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1210 sbuf_printf(sb, 1211 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1212 indent, g_eli_algo2str(sc->sc_aalgo)); 1213 } 1214 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1215 sc->sc_ekeylen); 1216 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1217 indent, g_eli_algo2str(sc->sc_ealgo)); 1218 sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1219 (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1220 } 1221 1222 static void 1223 g_eli_shutdown_pre_sync(void *arg, int howto) 1224 { 1225 struct g_class *mp; 1226 struct g_geom *gp, *gp2; 1227 struct g_provider *pp; 1228 struct g_eli_softc *sc; 1229 int error; 1230 1231 mp = arg; 1232 DROP_GIANT(); 1233 g_topology_lock(); 1234 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1235 sc = gp->softc; 1236 if (sc == NULL) 1237 continue; 1238 pp = LIST_FIRST(&gp->provider); 1239 KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 1240 if (pp->acr + pp->acw + pp->ace == 0) 1241 error = g_eli_destroy(sc, TRUE); 1242 else { 1243 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1244 gp->access = g_eli_access; 1245 } 1246 } 1247 g_topology_unlock(); 1248 PICKUP_GIANT(); 1249 } 1250 1251 static void 1252 g_eli_init(struct g_class *mp) 1253 { 1254 1255 g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1256 g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1257 if (g_eli_pre_sync == NULL) 1258 G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1259 } 1260 1261 static void 1262 g_eli_fini(struct g_class *mp) 1263 { 1264 1265 if (g_eli_pre_sync != NULL) 1266 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1267 } 1268 1269 DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1270 MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 1271