1c58794deSPawel Jakub Dawidek /*- 21e09ff3dSPawel Jakub Dawidek * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 3c58794deSPawel Jakub Dawidek * All rights reserved. 4c58794deSPawel Jakub Dawidek * 5c58794deSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 6c58794deSPawel Jakub Dawidek * modification, are permitted provided that the following conditions 7c58794deSPawel Jakub Dawidek * are met: 8c58794deSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 9c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 10c58794deSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 11c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 12c58794deSPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 13c58794deSPawel Jakub Dawidek * 14c58794deSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15c58794deSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c58794deSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c58794deSPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18c58794deSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c58794deSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c58794deSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c58794deSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c58794deSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c58794deSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c58794deSPawel Jakub Dawidek * SUCH DAMAGE. 25c58794deSPawel Jakub Dawidek */ 26c58794deSPawel Jakub Dawidek 27c58794deSPawel Jakub Dawidek #include <sys/cdefs.h> 28c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 29c58794deSPawel Jakub Dawidek 30c58794deSPawel Jakub Dawidek #include <sys/param.h> 31c58794deSPawel Jakub Dawidek #include <sys/systm.h> 32f6ce353eSAndriy Gapon #include <sys/cons.h> 33c58794deSPawel Jakub Dawidek #include <sys/kernel.h> 349af2131bSPawel Jakub Dawidek #include <sys/linker.h> 35c58794deSPawel Jakub Dawidek #include <sys/module.h> 36c58794deSPawel Jakub Dawidek #include <sys/lock.h> 37c58794deSPawel Jakub Dawidek #include <sys/mutex.h> 38c58794deSPawel Jakub Dawidek #include <sys/bio.h> 395d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 40c58794deSPawel Jakub Dawidek #include <sys/sysctl.h> 41c58794deSPawel Jakub Dawidek #include <sys/malloc.h> 42c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h> 43c58794deSPawel Jakub Dawidek #include <sys/kthread.h> 44c58794deSPawel Jakub Dawidek #include <sys/proc.h> 45c58794deSPawel Jakub Dawidek #include <sys/sched.h> 46dd549194SPawel Jakub Dawidek #include <sys/smp.h> 47c58794deSPawel Jakub Dawidek #include <sys/uio.h> 48a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h> 49c58794deSPawel Jakub Dawidek 50c58794deSPawel Jakub Dawidek #include <vm/uma.h> 51c58794deSPawel Jakub Dawidek 52c58794deSPawel Jakub Dawidek #include <geom/geom.h> 53c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h> 54c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 55c58794deSPawel Jakub Dawidek 56cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module"); 57c58794deSPawel Jakub Dawidek 58c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 59c58794deSPawel Jakub Dawidek 60c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 61c58794deSPawel Jakub Dawidek SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff"); 62a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION; 63a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 64a1f4a8c4SPawel Jakub Dawidek "GELI version"); 65f95168e0SPawel Jakub Dawidek int g_eli_debug = 0; 66af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 67c58794deSPawel Jakub Dawidek "Debug level"); 68c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3; 69af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 7098645006SChristian Brueffer "Number of tries for entering the passphrase"); 71eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO; 72af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 73c58794deSPawel Jakub Dawidek &g_eli_visible_passphrase, 0, 74eb4c31fdSEd Schouten "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 75b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES; 76af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 7798645006SChristian Brueffer 0, "Number of times on-disk keys should be overwritten when destroying them"); 78dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0; 79af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 80c58794deSPawel Jakub Dawidek "Number of threads doing crypto work"); 81eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0; 82af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 83eaa3b919SPawel Jakub Dawidek "Use crypto operations batching"); 84c58794deSPawel Jakub Dawidek 85835c4dd4SColin Percival /* 86835c4dd4SColin Percival * Passphrase cached during boot, in order to be more user-friendly if 87835c4dd4SColin Percival * there are multiple providers using the same passphrase. 88835c4dd4SColin Percival */ 89835c4dd4SColin Percival static char cached_passphrase[256]; 90835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1; 91835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 92835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 93835c4dd4SColin Percival &g_eli_boot_passcache, 0, 94835c4dd4SColin Percival "Passphrases are cached during boot process for possible reuse"); 95835c4dd4SColin Percival static void 9666427784SColin Percival fetch_loader_passphrase(void * dummy) 9766427784SColin Percival { 9866427784SColin Percival char * env_passphrase; 9966427784SColin Percival 10066427784SColin Percival KASSERT(dynamic_kenv, ("need dynamic kenv")); 10166427784SColin Percival 10266427784SColin Percival if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 10366427784SColin Percival /* Extract passphrase from the environment. */ 10466427784SColin Percival strlcpy(cached_passphrase, env_passphrase, 10566427784SColin Percival sizeof(cached_passphrase)); 10666427784SColin Percival freeenv(env_passphrase); 10766427784SColin Percival 10866427784SColin Percival /* Wipe the passphrase from the environment. */ 10966427784SColin Percival kern_unsetenv("kern.geom.eli.passphrase"); 11066427784SColin Percival } 11166427784SColin Percival } 11266427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 11366427784SColin Percival fetch_loader_passphrase, NULL); 11466427784SColin Percival static void 115835c4dd4SColin Percival zero_boot_passcache(void * dummy) 116835c4dd4SColin Percival { 117835c4dd4SColin Percival 118835c4dd4SColin Percival memset(cached_passphrase, 0, sizeof(cached_passphrase)); 119835c4dd4SColin Percival } 120835c4dd4SColin Percival EVENTHANDLER_DEFINE(mountroot, zero_boot_passcache, NULL, 0); 121835c4dd4SColin Percival 122c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL; 123c5d387d0SPawel Jakub Dawidek 124c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 125c58794deSPawel Jakub Dawidek struct g_geom *gp); 126c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp); 127c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp); 128c58794deSPawel Jakub Dawidek 129c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste; 130c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf; 131c58794deSPawel Jakub Dawidek 132c58794deSPawel Jakub Dawidek struct g_class g_eli_class = { 133c58794deSPawel Jakub Dawidek .name = G_ELI_CLASS_NAME, 134c58794deSPawel Jakub Dawidek .version = G_VERSION, 135c58794deSPawel Jakub Dawidek .ctlreq = g_eli_config, 136c58794deSPawel Jakub Dawidek .taste = g_eli_taste, 137c5d387d0SPawel Jakub Dawidek .destroy_geom = g_eli_destroy_geom, 138c5d387d0SPawel Jakub Dawidek .init = g_eli_init, 139c5d387d0SPawel Jakub Dawidek .fini = g_eli_fini 140c58794deSPawel Jakub Dawidek }; 141c58794deSPawel Jakub Dawidek 142c58794deSPawel Jakub Dawidek 143c58794deSPawel Jakub Dawidek /* 144c58794deSPawel Jakub Dawidek * Code paths: 145c58794deSPawel Jakub Dawidek * BIO_READ: 1465ad4a7c7SPawel Jakub Dawidek * 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 147c58794deSPawel Jakub Dawidek * BIO_WRITE: 148c58794deSPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 149c58794deSPawel Jakub Dawidek */ 150c58794deSPawel Jakub Dawidek 151c58794deSPawel Jakub Dawidek 152c58794deSPawel Jakub Dawidek /* 153c58794deSPawel Jakub Dawidek * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 154c58794deSPawel Jakub Dawidek * accelerator or something like this. 155c58794deSPawel Jakub Dawidek * The function updates the SID and rerun the operation. 156c58794deSPawel Jakub Dawidek */ 157eaa3b919SPawel Jakub Dawidek int 158c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp) 159c58794deSPawel Jakub Dawidek { 160c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 161c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 162c58794deSPawel Jakub Dawidek struct bio *bp; 163c58794deSPawel Jakub Dawidek int error; 164c58794deSPawel Jakub Dawidek 165c58794deSPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 166c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 167c58794deSPawel Jakub Dawidek LIST_FOREACH(wr, &sc->sc_workers, w_next) { 168c58794deSPawel Jakub Dawidek if (wr->w_number == bp->bio_pflags) 169c58794deSPawel Jakub Dawidek break; 170c58794deSPawel Jakub Dawidek } 171c58794deSPawel Jakub Dawidek KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 17298645006SChristian Brueffer G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).", 173c58794deSPawel Jakub Dawidek bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid, 174c58794deSPawel Jakub Dawidek (uintmax_t)crp->crp_sid); 175c58794deSPawel Jakub Dawidek wr->w_sid = crp->crp_sid; 176c58794deSPawel Jakub Dawidek crp->crp_etype = 0; 177c58794deSPawel Jakub Dawidek error = crypto_dispatch(crp); 178c58794deSPawel Jakub Dawidek if (error == 0) 179c58794deSPawel Jakub Dawidek return (0); 180c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 181c58794deSPawel Jakub Dawidek crp->crp_etype = error; 182c58794deSPawel Jakub Dawidek return (error); 183c58794deSPawel Jakub Dawidek } 184c58794deSPawel Jakub Dawidek 185c58794deSPawel Jakub Dawidek /* 186c58794deSPawel Jakub Dawidek * The function is called afer reading encrypted data from the provider. 187bb30fea6SPawel Jakub Dawidek * 1885ad4a7c7SPawel Jakub Dawidek * 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 189c58794deSPawel Jakub Dawidek */ 190eaa3b919SPawel Jakub Dawidek void 191c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp) 192c58794deSPawel Jakub Dawidek { 193c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 194c58794deSPawel Jakub Dawidek struct bio *pbp; 195c58794deSPawel Jakub Dawidek 196c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 197c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 198*2dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 199c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 20090574b0aSMikolaj Golub g_destroy_bio(bp); 201eaa3b919SPawel Jakub Dawidek /* 202eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 203eaa3b919SPawel Jakub Dawidek */ 204eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 205eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 206eaa3b919SPawel Jakub Dawidek return; 2075ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 208c58794deSPawel Jakub Dawidek if (pbp->bio_error != 0) { 209*2dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 210*2dc7e36bSSteven Hartland pbp->bio_error); 211c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 212eaa3b919SPawel Jakub Dawidek if (pbp->bio_driver2 != NULL) { 213eaa3b919SPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 214eaa3b919SPawel Jakub Dawidek pbp->bio_driver2 = NULL; 215eaa3b919SPawel Jakub Dawidek } 216c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 2175ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 218c58794deSPawel Jakub Dawidek return; 219c58794deSPawel Jakub Dawidek } 220c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 221c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, pbp); 222c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 223c58794deSPawel Jakub Dawidek wakeup(sc); 224c58794deSPawel Jakub Dawidek } 225c58794deSPawel Jakub Dawidek 226c58794deSPawel Jakub Dawidek /* 227c58794deSPawel Jakub Dawidek * The function is called after we encrypt and write data. 228bb30fea6SPawel Jakub Dawidek * 229bb30fea6SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 230c58794deSPawel Jakub Dawidek */ 231eaa3b919SPawel Jakub Dawidek void 232c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp) 233c58794deSPawel Jakub Dawidek { 2345ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 235c58794deSPawel Jakub Dawidek struct bio *pbp; 236c58794deSPawel Jakub Dawidek 237c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 238c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 239*2dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 240c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 24190574b0aSMikolaj Golub g_destroy_bio(bp); 242eaa3b919SPawel Jakub Dawidek /* 243eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 244eaa3b919SPawel Jakub Dawidek */ 245eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 246eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 247eaa3b919SPawel Jakub Dawidek return; 248c58794deSPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 249c58794deSPawel Jakub Dawidek pbp->bio_driver2 = NULL; 250eaa3b919SPawel Jakub Dawidek if (pbp->bio_error != 0) { 251*2dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 252c58794deSPawel Jakub Dawidek pbp->bio_error); 253c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 254*2dc7e36bSSteven Hartland } else 255*2dc7e36bSSteven Hartland pbp->bio_completed = pbp->bio_length; 256*2dc7e36bSSteven Hartland 257c58794deSPawel Jakub Dawidek /* 258c58794deSPawel Jakub Dawidek * Write is finished, send it up. 259c58794deSPawel Jakub Dawidek */ 2605ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 261c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 2625ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 263c58794deSPawel Jakub Dawidek } 264c58794deSPawel Jakub Dawidek 265c58794deSPawel Jakub Dawidek /* 266c58794deSPawel Jakub Dawidek * This function should never be called, but GEOM made as it set ->orphan() 267c58794deSPawel Jakub Dawidek * method for every geom. 268c58794deSPawel Jakub Dawidek */ 269c58794deSPawel Jakub Dawidek static void 270c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp) 271c58794deSPawel Jakub Dawidek { 272c58794deSPawel Jakub Dawidek 273c58794deSPawel Jakub Dawidek panic("Function %s() called for %s.", __func__, cp->geom->name); 274c58794deSPawel Jakub Dawidek } 275c58794deSPawel Jakub Dawidek 276c58794deSPawel Jakub Dawidek static void 277c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp) 278c58794deSPawel Jakub Dawidek { 279c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 280c58794deSPawel Jakub Dawidek 281c58794deSPawel Jakub Dawidek g_topology_assert(); 282c58794deSPawel Jakub Dawidek sc = cp->geom->softc; 283c58794deSPawel Jakub Dawidek if (sc == NULL) 284c58794deSPawel Jakub Dawidek return; 2855ad4a7c7SPawel Jakub Dawidek g_eli_destroy(sc, TRUE); 286c58794deSPawel Jakub Dawidek } 287c58794deSPawel Jakub Dawidek 288bb30fea6SPawel Jakub Dawidek /* 289056638c4SPawel Jakub Dawidek * BIO_READ: 2905ad4a7c7SPawel Jakub Dawidek * 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 291056638c4SPawel Jakub Dawidek * BIO_WRITE: 292056638c4SPawel Jakub Dawidek * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 293bb30fea6SPawel Jakub Dawidek */ 294c58794deSPawel Jakub Dawidek static void 295c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp) 296c58794deSPawel Jakub Dawidek { 297c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 298d3a1be90SPawel Jakub Dawidek struct g_consumer *cp; 299c58794deSPawel Jakub Dawidek struct bio *cbp; 300c58794deSPawel Jakub Dawidek 301c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 302c58794deSPawel Jakub Dawidek KASSERT(sc != NULL, 303c58794deSPawel Jakub Dawidek ("Provider's error should be set (error=%d)(device=%s).", 304c58794deSPawel Jakub Dawidek bp->bio_to->error, bp->bio_to->name)); 305c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request received."); 306c58794deSPawel Jakub Dawidek 307c58794deSPawel Jakub Dawidek switch (bp->bio_cmd) { 308c58794deSPawel Jakub Dawidek case BIO_READ: 309c58794deSPawel Jakub Dawidek case BIO_WRITE: 310d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 31142461fbaSPawel Jakub Dawidek case BIO_FLUSH: 312c58794deSPawel Jakub Dawidek break; 313c58794deSPawel Jakub Dawidek case BIO_DELETE: 314c58794deSPawel Jakub Dawidek /* 31546e34470SPawel Jakub Dawidek * If the user hasn't set the NODELETE flag, we just pass 31646e34470SPawel Jakub Dawidek * it down the stack and let the layers beneath us do (or 31746e34470SPawel Jakub Dawidek * not) whatever they do with it. If they have, we 31846e34470SPawel Jakub Dawidek * reject it. A possible extension would be an 31946e34470SPawel Jakub Dawidek * additional flag to take it as a hint to shred the data 32046e34470SPawel Jakub Dawidek * with [multiple?] overwrites. 321c58794deSPawel Jakub Dawidek */ 32246e34470SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 32346e34470SPawel Jakub Dawidek break; 324c58794deSPawel Jakub Dawidek default: 325c58794deSPawel Jakub Dawidek g_io_deliver(bp, EOPNOTSUPP); 326c58794deSPawel Jakub Dawidek return; 327c58794deSPawel Jakub Dawidek } 328c58794deSPawel Jakub Dawidek cbp = g_clone_bio(bp); 329c58794deSPawel Jakub Dawidek if (cbp == NULL) { 330c58794deSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 331c58794deSPawel Jakub Dawidek return; 332c58794deSPawel Jakub Dawidek } 3335ad4a7c7SPawel Jakub Dawidek bp->bio_driver1 = cbp; 3345ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = G_ELI_NEW_BIO; 335d3a1be90SPawel Jakub Dawidek switch (bp->bio_cmd) { 336d3a1be90SPawel Jakub Dawidek case BIO_READ: 337eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 3385ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 0); 339d3a1be90SPawel Jakub Dawidek break; 340eaa3b919SPawel Jakub Dawidek } 341eaa3b919SPawel Jakub Dawidek /* FALLTHROUGH */ 342d3a1be90SPawel Jakub Dawidek case BIO_WRITE: 343c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 344c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp); 345c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 346c58794deSPawel Jakub Dawidek wakeup(sc); 347d3a1be90SPawel Jakub Dawidek break; 348d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 34942461fbaSPawel Jakub Dawidek case BIO_FLUSH: 35046e34470SPawel Jakub Dawidek case BIO_DELETE: 351d3a1be90SPawel Jakub Dawidek cbp->bio_done = g_std_done; 352d3a1be90SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 353d3a1be90SPawel Jakub Dawidek cbp->bio_to = cp->provider; 354cd0d707eSPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 355d3a1be90SPawel Jakub Dawidek g_io_request(cbp, cp); 356d3a1be90SPawel Jakub Dawidek break; 357c58794deSPawel Jakub Dawidek } 358c58794deSPawel Jakub Dawidek } 359c58794deSPawel Jakub Dawidek 3603ac01bc2SPawel Jakub Dawidek static int 3613ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr) 3623ac01bc2SPawel Jakub Dawidek { 3633ac01bc2SPawel Jakub Dawidek struct g_eli_softc *sc; 3643ac01bc2SPawel Jakub Dawidek struct cryptoini crie, cria; 3653ac01bc2SPawel Jakub Dawidek int error; 3663ac01bc2SPawel Jakub Dawidek 3673ac01bc2SPawel Jakub Dawidek sc = wr->w_softc; 3683ac01bc2SPawel Jakub Dawidek 3693ac01bc2SPawel Jakub Dawidek bzero(&crie, sizeof(crie)); 3703ac01bc2SPawel Jakub Dawidek crie.cri_alg = sc->sc_ealgo; 3713ac01bc2SPawel Jakub Dawidek crie.cri_klen = sc->sc_ekeylen; 3723ac01bc2SPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 3733ac01bc2SPawel Jakub Dawidek crie.cri_klen <<= 1; 374ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 375ad0a5236SPawel Jakub Dawidek crie.cri_key = g_eli_key_hold(sc, 0, 376ad0a5236SPawel Jakub Dawidek LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 377ad0a5236SPawel Jakub Dawidek } else { 3781e09ff3dSPawel Jakub Dawidek crie.cri_key = sc->sc_ekey; 379ad0a5236SPawel Jakub Dawidek } 3803ac01bc2SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 3813ac01bc2SPawel Jakub Dawidek bzero(&cria, sizeof(cria)); 3823ac01bc2SPawel Jakub Dawidek cria.cri_alg = sc->sc_aalgo; 3833ac01bc2SPawel Jakub Dawidek cria.cri_klen = sc->sc_akeylen; 3843ac01bc2SPawel Jakub Dawidek cria.cri_key = sc->sc_akey; 3853ac01bc2SPawel Jakub Dawidek crie.cri_next = &cria; 3863ac01bc2SPawel Jakub Dawidek } 3873ac01bc2SPawel Jakub Dawidek 3883ac01bc2SPawel Jakub Dawidek switch (sc->sc_crypto) { 3893ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 3903ac01bc2SPawel Jakub Dawidek error = crypto_newsession(&wr->w_sid, &crie, 3913ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 3923ac01bc2SPawel Jakub Dawidek break; 3933ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 3943ac01bc2SPawel Jakub Dawidek error = crypto_newsession(&wr->w_sid, &crie, 3953ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 3963ac01bc2SPawel Jakub Dawidek break; 3973ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_UNKNOWN: 3983ac01bc2SPawel Jakub Dawidek error = crypto_newsession(&wr->w_sid, &crie, 3993ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 4003ac01bc2SPawel Jakub Dawidek if (error == 0) { 4013ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 4023ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 4033ac01bc2SPawel Jakub Dawidek sc->sc_crypto = G_ELI_CRYPTO_HW; 4043ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 4053ac01bc2SPawel Jakub Dawidek } else { 4063ac01bc2SPawel Jakub Dawidek error = crypto_newsession(&wr->w_sid, &crie, 4073ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 4083ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 4093ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 4103ac01bc2SPawel Jakub Dawidek sc->sc_crypto = G_ELI_CRYPTO_SW; 4113ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 4123ac01bc2SPawel Jakub Dawidek } 4133ac01bc2SPawel Jakub Dawidek break; 4143ac01bc2SPawel Jakub Dawidek default: 4153ac01bc2SPawel Jakub Dawidek panic("%s: invalid condition", __func__); 4163ac01bc2SPawel Jakub Dawidek } 4173ac01bc2SPawel Jakub Dawidek 418ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) 419ad0a5236SPawel Jakub Dawidek g_eli_key_drop(sc, crie.cri_key); 420ad0a5236SPawel Jakub Dawidek 4213ac01bc2SPawel Jakub Dawidek return (error); 4223ac01bc2SPawel Jakub Dawidek } 4233ac01bc2SPawel Jakub Dawidek 4243ac01bc2SPawel Jakub Dawidek static void 4253ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr) 4263ac01bc2SPawel Jakub Dawidek { 4273ac01bc2SPawel Jakub Dawidek 4283ac01bc2SPawel Jakub Dawidek crypto_freesession(wr->w_sid); 4293ac01bc2SPawel Jakub Dawidek } 4303ac01bc2SPawel Jakub Dawidek 4315ad4a7c7SPawel Jakub Dawidek static void 4325ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc) 4335ad4a7c7SPawel Jakub Dawidek { 4345ad4a7c7SPawel Jakub Dawidek struct bio *bp; 4355ad4a7c7SPawel Jakub Dawidek 4365ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 4375ad4a7c7SPawel Jakub Dawidek 4385ad4a7c7SPawel Jakub Dawidek while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 4395ad4a7c7SPawel Jakub Dawidek KASSERT(bp->bio_pflags == G_ELI_NEW_BIO, 4405ad4a7c7SPawel Jakub Dawidek ("Not new bio when canceling (bp=%p).", bp)); 4415ad4a7c7SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 4425ad4a7c7SPawel Jakub Dawidek } 4435ad4a7c7SPawel Jakub Dawidek } 4445ad4a7c7SPawel Jakub Dawidek 4455ad4a7c7SPawel Jakub Dawidek static struct bio * 4465ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc) 4475ad4a7c7SPawel Jakub Dawidek { 4485ad4a7c7SPawel Jakub Dawidek struct bio *bp; 4495ad4a7c7SPawel Jakub Dawidek 4505ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 4515ad4a7c7SPawel Jakub Dawidek 4525ad4a7c7SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 4535ad4a7c7SPawel Jakub Dawidek return (bioq_takefirst(&sc->sc_queue)); 4545ad4a7c7SPawel Jakub Dawidek /* 4555ad4a7c7SPawel Jakub Dawidek * Device suspended, so we skip new I/O requests. 4565ad4a7c7SPawel Jakub Dawidek */ 4575ad4a7c7SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 4585ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags != G_ELI_NEW_BIO) 4595ad4a7c7SPawel Jakub Dawidek break; 4605ad4a7c7SPawel Jakub Dawidek } 4615ad4a7c7SPawel Jakub Dawidek if (bp != NULL) 4625ad4a7c7SPawel Jakub Dawidek bioq_remove(&sc->sc_queue, bp); 4635ad4a7c7SPawel Jakub Dawidek return (bp); 4645ad4a7c7SPawel Jakub Dawidek } 4655ad4a7c7SPawel Jakub Dawidek 466c58794deSPawel Jakub Dawidek /* 467c58794deSPawel Jakub Dawidek * This is the main function for kernel worker thread when we don't have 468c58794deSPawel Jakub Dawidek * hardware acceleration and we have to do cryptography in software. 469c58794deSPawel Jakub Dawidek * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 470c58794deSPawel Jakub Dawidek * threads with crypto work. 471c58794deSPawel Jakub Dawidek */ 472c58794deSPawel Jakub Dawidek static void 473c58794deSPawel Jakub Dawidek g_eli_worker(void *arg) 474c58794deSPawel Jakub Dawidek { 475c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 476c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 477c58794deSPawel Jakub Dawidek struct bio *bp; 4783ac01bc2SPawel Jakub Dawidek int error; 479c58794deSPawel Jakub Dawidek 480c58794deSPawel Jakub Dawidek wr = arg; 481c58794deSPawel Jakub Dawidek sc = wr->w_softc; 482a1ea1a22SPawel Jakub Dawidek #ifdef SMP 483a1ea1a22SPawel Jakub Dawidek /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 4840c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) { 485a1ea1a22SPawel Jakub Dawidek while (!smp_started) 486a1ea1a22SPawel Jakub Dawidek tsleep(wr, 0, "geli:smp", hz / 4); 487a1ea1a22SPawel Jakub Dawidek } 488a1ea1a22SPawel Jakub Dawidek #endif 489982d11f8SJeff Roberson thread_lock(curthread); 49031c4cef7SPawel Jakub Dawidek sched_prio(curthread, PUSER); 4910c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) 4920c879bd9SPawel Jakub Dawidek sched_bind(curthread, wr->w_number % mp_ncpus); 493982d11f8SJeff Roberson thread_unlock(curthread); 494c58794deSPawel Jakub Dawidek 495c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 496c58794deSPawel Jakub Dawidek 497c58794deSPawel Jakub Dawidek for (;;) { 498c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 4995ad4a7c7SPawel Jakub Dawidek again: 5005ad4a7c7SPawel Jakub Dawidek bp = g_eli_takefirst(sc); 501c58794deSPawel Jakub Dawidek if (bp == NULL) { 502eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 5035ad4a7c7SPawel Jakub Dawidek g_eli_cancel(sc); 504c58794deSPawel Jakub Dawidek LIST_REMOVE(wr, w_next); 5053ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 506c58794deSPawel Jakub Dawidek free(wr, M_ELI); 507c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s exiting.", 508c58794deSPawel Jakub Dawidek curthread->td_proc->p_comm); 509c58794deSPawel Jakub Dawidek wakeup(&sc->sc_workers); 510c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5113745c395SJulian Elischer kproc_exit(0); 512c58794deSPawel Jakub Dawidek } 5135ad4a7c7SPawel Jakub Dawidek while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 5145ad4a7c7SPawel Jakub Dawidek if (sc->sc_inflight > 0) { 515038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, "inflight=%d", 516038c55adSPawel Jakub Dawidek sc->sc_inflight); 5175ad4a7c7SPawel Jakub Dawidek /* 5185ad4a7c7SPawel Jakub Dawidek * We still have inflight BIOs, so 5195ad4a7c7SPawel Jakub Dawidek * sleep and retry. 5205ad4a7c7SPawel Jakub Dawidek */ 5215ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 5225ad4a7c7SPawel Jakub Dawidek "geli:inf", hz / 5); 5235ad4a7c7SPawel Jakub Dawidek goto again; 5245ad4a7c7SPawel Jakub Dawidek } 5255ad4a7c7SPawel Jakub Dawidek /* 5265ad4a7c7SPawel Jakub Dawidek * Suspend requested, mark the worker as 5275ad4a7c7SPawel Jakub Dawidek * suspended and go to sleep. 5285ad4a7c7SPawel Jakub Dawidek */ 5293ac01bc2SPawel Jakub Dawidek if (wr->w_active) { 5303ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 5313ac01bc2SPawel Jakub Dawidek wr->w_active = FALSE; 5323ac01bc2SPawel Jakub Dawidek } 5335ad4a7c7SPawel Jakub Dawidek wakeup(&sc->sc_workers); 5345ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 5355ad4a7c7SPawel Jakub Dawidek "geli:suspend", 0); 5363ac01bc2SPawel Jakub Dawidek if (!wr->w_active && 5373ac01bc2SPawel Jakub Dawidek !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 5383ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 5393ac01bc2SPawel Jakub Dawidek KASSERT(error == 0, 5403ac01bc2SPawel Jakub Dawidek ("g_eli_newsession() failed on resume (error=%d)", 5413ac01bc2SPawel Jakub Dawidek error)); 5423ac01bc2SPawel Jakub Dawidek wr->w_active = TRUE; 5433ac01bc2SPawel Jakub Dawidek } 5445ad4a7c7SPawel Jakub Dawidek goto again; 5455ad4a7c7SPawel Jakub Dawidek } 54631c4cef7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 547c58794deSPawel Jakub Dawidek continue; 548c58794deSPawel Jakub Dawidek } 5495ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) 5505ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1); 551c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5525ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) { 5535ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = 0; 5545ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 5555ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 556eaa3b919SPawel Jakub Dawidek g_eli_auth_read(sc, bp); 5575ad4a7c7SPawel Jakub Dawidek else 5585ad4a7c7SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 5595ad4a7c7SPawel Jakub Dawidek } else { 5605ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 5615ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 1); 5625ad4a7c7SPawel Jakub Dawidek else 5635ad4a7c7SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 5645ad4a7c7SPawel Jakub Dawidek } 5655ad4a7c7SPawel Jakub Dawidek } else { 5665ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 567eaa3b919SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 568eaa3b919SPawel Jakub Dawidek else 569dddd1d53SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 570c58794deSPawel Jakub Dawidek } 571c58794deSPawel Jakub Dawidek } 5725ad4a7c7SPawel Jakub Dawidek } 573c58794deSPawel Jakub Dawidek 574c58794deSPawel Jakub Dawidek /* 575c58794deSPawel Jakub Dawidek * Here we generate IV. It is unique for every sector. 576c58794deSPawel Jakub Dawidek */ 577eaa3b919SPawel Jakub Dawidek void 578c58794deSPawel Jakub Dawidek g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 579c58794deSPawel Jakub Dawidek size_t size) 580c58794deSPawel Jakub Dawidek { 5819a5a1d1eSPawel Jakub Dawidek uint8_t off[8]; 582c58794deSPawel Jakub Dawidek 583efb46508SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0) 584efb46508SPawel Jakub Dawidek bcopy(&offset, off, sizeof(off)); 585efb46508SPawel Jakub Dawidek else 5862bd4ade6SPawel Jakub Dawidek le64enc(off, (uint64_t)offset); 5879a5a1d1eSPawel Jakub Dawidek 5889a5a1d1eSPawel Jakub Dawidek switch (sc->sc_ealgo) { 5899a5a1d1eSPawel Jakub Dawidek case CRYPTO_AES_XTS: 5909a5a1d1eSPawel Jakub Dawidek bcopy(off, iv, sizeof(off)); 5919a5a1d1eSPawel Jakub Dawidek bzero(iv + sizeof(off), size - sizeof(off)); 5929a5a1d1eSPawel Jakub Dawidek break; 5939a5a1d1eSPawel Jakub Dawidek default: 5949a5a1d1eSPawel Jakub Dawidek { 5959a5a1d1eSPawel Jakub Dawidek u_char hash[SHA256_DIGEST_LENGTH]; 5969a5a1d1eSPawel Jakub Dawidek SHA256_CTX ctx; 5979a5a1d1eSPawel Jakub Dawidek 598c58794deSPawel Jakub Dawidek /* Copy precalculated SHA256 context for IV-Key. */ 599c58794deSPawel Jakub Dawidek bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx)); 600efb46508SPawel Jakub Dawidek SHA256_Update(&ctx, off, sizeof(off)); 601c58794deSPawel Jakub Dawidek SHA256_Final(hash, &ctx); 6029a5a1d1eSPawel Jakub Dawidek bcopy(hash, iv, MIN(sizeof(hash), size)); 6039a5a1d1eSPawel Jakub Dawidek break; 6049a5a1d1eSPawel Jakub Dawidek } 6059a5a1d1eSPawel Jakub Dawidek } 606c58794deSPawel Jakub Dawidek } 607c58794deSPawel Jakub Dawidek 608c58794deSPawel Jakub Dawidek int 609c58794deSPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 610c58794deSPawel Jakub Dawidek struct g_eli_metadata *md) 611c58794deSPawel Jakub Dawidek { 612c58794deSPawel Jakub Dawidek struct g_geom *gp; 613c58794deSPawel Jakub Dawidek struct g_consumer *cp; 614c58794deSPawel Jakub Dawidek u_char *buf = NULL; 615c58794deSPawel Jakub Dawidek int error; 616c58794deSPawel Jakub Dawidek 617c58794deSPawel Jakub Dawidek g_topology_assert(); 618c58794deSPawel Jakub Dawidek 619c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "eli:taste"); 620c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 621c58794deSPawel Jakub Dawidek gp->access = g_std_access; 622c58794deSPawel Jakub Dawidek /* 623c58794deSPawel Jakub Dawidek * g_eli_read_metadata() is always called from the event thread. 624c58794deSPawel Jakub Dawidek * Our geom is created and destroyed in the same event, so there 625c58794deSPawel Jakub Dawidek * could be no orphan nor spoil event in the meantime. 626c58794deSPawel Jakub Dawidek */ 627c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan_spoil_assert; 628c58794deSPawel Jakub Dawidek gp->spoiled = g_eli_orphan_spoil_assert; 629c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 630c58794deSPawel Jakub Dawidek error = g_attach(cp, pp); 631c58794deSPawel Jakub Dawidek if (error != 0) 632c58794deSPawel Jakub Dawidek goto end; 633c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 634c58794deSPawel Jakub Dawidek if (error != 0) 635c58794deSPawel Jakub Dawidek goto end; 636c58794deSPawel Jakub Dawidek g_topology_unlock(); 637c58794deSPawel Jakub Dawidek buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 638c58794deSPawel Jakub Dawidek &error); 639c58794deSPawel Jakub Dawidek g_topology_lock(); 6408a4a44b5SMaxim Sobolev if (buf == NULL) 641c58794deSPawel Jakub Dawidek goto end; 642fefb6a14SPawel Jakub Dawidek error = eli_metadata_decode(buf, md); 643fefb6a14SPawel Jakub Dawidek if (error != 0) 644fefb6a14SPawel Jakub Dawidek goto end; 645fefb6a14SPawel Jakub Dawidek /* Metadata was read and decoded successfully. */ 646c58794deSPawel Jakub Dawidek end: 647c58794deSPawel Jakub Dawidek if (buf != NULL) 648c58794deSPawel Jakub Dawidek g_free(buf); 649c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 650c58794deSPawel Jakub Dawidek if (cp->acr == 1) 651c58794deSPawel Jakub Dawidek g_access(cp, -1, 0, 0); 652c58794deSPawel Jakub Dawidek g_detach(cp); 653c58794deSPawel Jakub Dawidek } 654c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 655c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 656c58794deSPawel Jakub Dawidek return (error); 657c58794deSPawel Jakub Dawidek } 658c58794deSPawel Jakub Dawidek 659c58794deSPawel Jakub Dawidek /* 660c58794deSPawel Jakub Dawidek * The function is called when we had last close on provider and user requested 661c58794deSPawel Jakub Dawidek * to close it when this situation occur. 662c58794deSPawel Jakub Dawidek */ 663c58794deSPawel Jakub Dawidek static void 66419351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused) 665c58794deSPawel Jakub Dawidek { 666c58794deSPawel Jakub Dawidek struct g_geom *gp; 66719351a14SAlexander Motin char gpname[64]; 668c58794deSPawel Jakub Dawidek int error; 669c58794deSPawel Jakub Dawidek 670c58794deSPawel Jakub Dawidek g_topology_assert(); 67119351a14SAlexander Motin gp = arg; 67219351a14SAlexander Motin strlcpy(gpname, gp->name, sizeof(gpname)); 67319351a14SAlexander Motin error = g_eli_destroy(gp->softc, TRUE); 674c58794deSPawel Jakub Dawidek KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 67519351a14SAlexander Motin gpname, error)); 67619351a14SAlexander Motin G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 677c58794deSPawel Jakub Dawidek } 678c58794deSPawel Jakub Dawidek 679c58794deSPawel Jakub Dawidek int 680c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de) 681c58794deSPawel Jakub Dawidek { 682c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 683c58794deSPawel Jakub Dawidek struct g_geom *gp; 684c58794deSPawel Jakub Dawidek 685c58794deSPawel Jakub Dawidek gp = pp->geom; 686c58794deSPawel Jakub Dawidek sc = gp->softc; 687c58794deSPawel Jakub Dawidek 688c58794deSPawel Jakub Dawidek if (dw > 0) { 68985059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) { 69085059016SPawel Jakub Dawidek /* Deny write attempts. */ 69185059016SPawel Jakub Dawidek return (EROFS); 69285059016SPawel Jakub Dawidek } 693c58794deSPawel Jakub Dawidek /* Someone is opening us for write, we need to remember that. */ 694c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_WOPEN; 695c58794deSPawel Jakub Dawidek return (0); 696c58794deSPawel Jakub Dawidek } 697c58794deSPawel Jakub Dawidek /* Is this the last close? */ 698c58794deSPawel Jakub Dawidek if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 699c58794deSPawel Jakub Dawidek return (0); 700c58794deSPawel Jakub Dawidek 701c58794deSPawel Jakub Dawidek /* 702c58794deSPawel Jakub Dawidek * Automatically detach on last close if requested. 703c58794deSPawel Jakub Dawidek */ 704c58794deSPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 705c58794deSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 70619351a14SAlexander Motin g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 707c58794deSPawel Jakub Dawidek } 708c58794deSPawel Jakub Dawidek return (0); 709c58794deSPawel Jakub Dawidek } 710c58794deSPawel Jakub Dawidek 711eba8f137SPawel Jakub Dawidek static int 712eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu) 713eba8f137SPawel Jakub Dawidek { 714eba8f137SPawel Jakub Dawidek #ifdef SMP 71571a19bdcSAttilio Rao return (CPU_ISSET(cpu, &hlt_cpus_mask)); 716eba8f137SPawel Jakub Dawidek #else 717eba8f137SPawel Jakub Dawidek return (0); 718eba8f137SPawel Jakub Dawidek #endif 719eba8f137SPawel Jakub Dawidek } 720eba8f137SPawel Jakub Dawidek 721c58794deSPawel Jakub Dawidek struct g_geom * 722c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 723c58794deSPawel Jakub Dawidek const struct g_eli_metadata *md, const u_char *mkey, int nkey) 724c58794deSPawel Jakub Dawidek { 725c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 726c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 727c58794deSPawel Jakub Dawidek struct g_geom *gp; 728c58794deSPawel Jakub Dawidek struct g_provider *pp; 729c58794deSPawel Jakub Dawidek struct g_consumer *cp; 730c58794deSPawel Jakub Dawidek u_int i, threads; 731c58794deSPawel Jakub Dawidek int error; 732c58794deSPawel Jakub Dawidek 733c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 734c58794deSPawel Jakub Dawidek 735c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 736c58794deSPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 737c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 738c58794deSPawel Jakub Dawidek /* 7394273d412SPawel Jakub Dawidek * Spoiling can happen even though we have the provider open 7404273d412SPawel Jakub Dawidek * exclusively, e.g. through media change events. 741c58794deSPawel Jakub Dawidek */ 7424273d412SPawel Jakub Dawidek gp->spoiled = g_eli_orphan; 743c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan; 74485059016SPawel Jakub Dawidek gp->dumpconf = g_eli_dumpconf; 745c58794deSPawel Jakub Dawidek /* 74685059016SPawel Jakub Dawidek * If detach-on-last-close feature is not enabled and we don't operate 74785059016SPawel Jakub Dawidek * on read-only provider, we can simply use g_std_access(). 748c58794deSPawel Jakub Dawidek */ 74985059016SPawel Jakub Dawidek if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 750c58794deSPawel Jakub Dawidek gp->access = g_eli_access; 751c58794deSPawel Jakub Dawidek else 752c58794deSPawel Jakub Dawidek gp->access = g_std_access; 753c58794deSPawel Jakub Dawidek 7541f8c92e6SPawel Jakub Dawidek sc->sc_version = md->md_version; 7555ad4a7c7SPawel Jakub Dawidek sc->sc_inflight = 0; 7563ac01bc2SPawel Jakub Dawidek sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN; 757c58794deSPawel Jakub Dawidek sc->sc_flags = md->md_flags; 7582bd4ade6SPawel Jakub Dawidek /* Backward compatibility. */ 7590e236b6cSPawel Jakub Dawidek if (md->md_version < G_ELI_VERSION_04) 7602bd4ade6SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER; 7610e236b6cSPawel Jakub Dawidek if (md->md_version < G_ELI_VERSION_05) 762c6a26d4cSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY; 7630e236b6cSPawel Jakub Dawidek if (md->md_version < G_ELI_VERSION_06 && 7640e236b6cSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) { 765ad0a5236SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_FIRST_KEY; 7660e236b6cSPawel Jakub Dawidek } 767457bbc4fSPawel Jakub Dawidek if (md->md_version < G_ELI_VERSION_07) 768457bbc4fSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY; 769eaa3b919SPawel Jakub Dawidek sc->sc_ealgo = md->md_ealgo; 770c58794deSPawel Jakub Dawidek sc->sc_nkey = nkey; 771eaa3b919SPawel Jakub Dawidek 772eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 773eaa3b919SPawel Jakub Dawidek sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 774eaa3b919SPawel Jakub Dawidek sc->sc_aalgo = md->md_aalgo; 775eaa3b919SPawel Jakub Dawidek sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 776eaa3b919SPawel Jakub Dawidek 777eaa3b919SPawel Jakub Dawidek sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen; 778eaa3b919SPawel Jakub Dawidek /* 779eaa3b919SPawel Jakub Dawidek * Some hash functions (like SHA1 and RIPEMD160) generates hash 780eaa3b919SPawel Jakub Dawidek * which length is not multiple of 128 bits, but we want data 781eaa3b919SPawel Jakub Dawidek * length to be multiple of 128, so we can encrypt without 782eaa3b919SPawel Jakub Dawidek * padding. The line below rounds down data length to multiple 783eaa3b919SPawel Jakub Dawidek * of 128 bits. 784eaa3b919SPawel Jakub Dawidek */ 785eaa3b919SPawel Jakub Dawidek sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 786eaa3b919SPawel Jakub Dawidek 787eaa3b919SPawel Jakub Dawidek sc->sc_bytes_per_sector = 788eaa3b919SPawel Jakub Dawidek (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 789eaa3b919SPawel Jakub Dawidek sc->sc_bytes_per_sector *= bpp->sectorsize; 790eaa3b919SPawel Jakub Dawidek } 791c58794deSPawel Jakub Dawidek 792c58794deSPawel Jakub Dawidek gp->softc = sc; 793c58794deSPawel Jakub Dawidek sc->sc_geom = gp; 794c58794deSPawel Jakub Dawidek 795c58794deSPawel Jakub Dawidek bioq_init(&sc->sc_queue); 796c58794deSPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 7971e09ff3dSPawel Jakub Dawidek mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 798c58794deSPawel Jakub Dawidek 799c58794deSPawel Jakub Dawidek pp = NULL; 800c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 801c58794deSPawel Jakub Dawidek error = g_attach(cp, bpp); 802c58794deSPawel Jakub Dawidek if (error != 0) { 803c58794deSPawel Jakub Dawidek if (req != NULL) { 804c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot attach to %s (error=%d).", 805c58794deSPawel Jakub Dawidek bpp->name, error); 806c58794deSPawel Jakub Dawidek } else { 807c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 808c58794deSPawel Jakub Dawidek bpp->name, error); 809c58794deSPawel Jakub Dawidek } 810c58794deSPawel Jakub Dawidek goto failed; 811c58794deSPawel Jakub Dawidek } 812c58794deSPawel Jakub Dawidek /* 813c58794deSPawel Jakub Dawidek * Keep provider open all the time, so we can run critical tasks, 814c58794deSPawel Jakub Dawidek * like Master Keys deletion, without wondering if we can open 815c58794deSPawel Jakub Dawidek * provider or not. 81685059016SPawel Jakub Dawidek * We don't open provider for writing only when user requested read-only 81785059016SPawel Jakub Dawidek * access. 818c58794deSPawel Jakub Dawidek */ 81985059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) 82085059016SPawel Jakub Dawidek error = g_access(cp, 1, 0, 1); 82185059016SPawel Jakub Dawidek else 822c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 1, 1); 823c58794deSPawel Jakub Dawidek if (error != 0) { 824c58794deSPawel Jakub Dawidek if (req != NULL) { 825c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot access %s (error=%d).", 826c58794deSPawel Jakub Dawidek bpp->name, error); 827c58794deSPawel Jakub Dawidek } else { 828c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 829c58794deSPawel Jakub Dawidek bpp->name, error); 830c58794deSPawel Jakub Dawidek } 831c58794deSPawel Jakub Dawidek goto failed; 832c58794deSPawel Jakub Dawidek } 833c58794deSPawel Jakub Dawidek 834c6a26d4cSPawel Jakub Dawidek sc->sc_sectorsize = md->md_sectorsize; 835c6a26d4cSPawel Jakub Dawidek sc->sc_mediasize = bpp->mediasize; 836c6a26d4cSPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) 837c6a26d4cSPawel Jakub Dawidek sc->sc_mediasize -= bpp->sectorsize; 838c6a26d4cSPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) 839c6a26d4cSPawel Jakub Dawidek sc->sc_mediasize -= (sc->sc_mediasize % sc->sc_sectorsize); 840c6a26d4cSPawel Jakub Dawidek else { 841c6a26d4cSPawel Jakub Dawidek sc->sc_mediasize /= sc->sc_bytes_per_sector; 842c6a26d4cSPawel Jakub Dawidek sc->sc_mediasize *= sc->sc_sectorsize; 843c6a26d4cSPawel Jakub Dawidek } 844c6a26d4cSPawel Jakub Dawidek 845c6a26d4cSPawel Jakub Dawidek /* 846c6a26d4cSPawel Jakub Dawidek * Remember the keys in our softc structure. 847c6a26d4cSPawel Jakub Dawidek */ 848c6a26d4cSPawel Jakub Dawidek g_eli_mkey_propagate(sc, mkey); 849c6a26d4cSPawel Jakub Dawidek sc->sc_ekeylen = md->md_keylen; 850c6a26d4cSPawel Jakub Dawidek 851c58794deSPawel Jakub Dawidek LIST_INIT(&sc->sc_workers); 852c58794deSPawel Jakub Dawidek 853c58794deSPawel Jakub Dawidek threads = g_eli_threads; 854dd549194SPawel Jakub Dawidek if (threads == 0) 855dd549194SPawel Jakub Dawidek threads = mp_ncpus; 8560c879bd9SPawel Jakub Dawidek sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 857c58794deSPawel Jakub Dawidek for (i = 0; i < threads; i++) { 858eba8f137SPawel Jakub Dawidek if (g_eli_cpu_is_disabled(i)) { 859eba8f137SPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 8601506db21SPawel Jakub Dawidek bpp->name, i); 861eba8f137SPawel Jakub Dawidek continue; 862eba8f137SPawel Jakub Dawidek } 863c58794deSPawel Jakub Dawidek wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 864c58794deSPawel Jakub Dawidek wr->w_softc = sc; 865c58794deSPawel Jakub Dawidek wr->w_number = i; 8665ad4a7c7SPawel Jakub Dawidek wr->w_active = TRUE; 867c58794deSPawel Jakub Dawidek 8683ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 869c58794deSPawel Jakub Dawidek if (error != 0) { 870c58794deSPawel Jakub Dawidek free(wr, M_ELI); 871c58794deSPawel Jakub Dawidek if (req != NULL) { 872c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot set up crypto session " 873c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 874c58794deSPawel Jakub Dawidek } else { 875c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot set up crypto session " 876c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 877c58794deSPawel Jakub Dawidek } 878c58794deSPawel Jakub Dawidek goto failed; 879c58794deSPawel Jakub Dawidek } 880c58794deSPawel Jakub Dawidek 8813745c395SJulian Elischer error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 882c58794deSPawel Jakub Dawidek "g_eli[%u] %s", i, bpp->name); 883c58794deSPawel Jakub Dawidek if (error != 0) { 8843ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 885c58794deSPawel Jakub Dawidek free(wr, M_ELI); 886c58794deSPawel Jakub Dawidek if (req != NULL) { 887dddd1d53SPawel Jakub Dawidek gctl_error(req, "Cannot create kernel thread " 888dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 889c58794deSPawel Jakub Dawidek } else { 890dddd1d53SPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot create kernel thread " 891dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 892c58794deSPawel Jakub Dawidek } 893c58794deSPawel Jakub Dawidek goto failed; 894c58794deSPawel Jakub Dawidek } 895c58794deSPawel Jakub Dawidek LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 896c58794deSPawel Jakub Dawidek } 897c58794deSPawel Jakub Dawidek 898c58794deSPawel Jakub Dawidek /* 899c58794deSPawel Jakub Dawidek * Create decrypted provider. 900c58794deSPawel Jakub Dawidek */ 901c58794deSPawel Jakub Dawidek pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 902c6a26d4cSPawel Jakub Dawidek pp->mediasize = sc->sc_mediasize; 903c6a26d4cSPawel Jakub Dawidek pp->sectorsize = sc->sc_sectorsize; 904eaa3b919SPawel Jakub Dawidek 905c58794deSPawel Jakub Dawidek g_error_provider(pp, 0); 906c58794deSPawel Jakub Dawidek 907c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s created.", pp->name); 908eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 909eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 910eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 911eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 912c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, " Crypto: %s", 913c58794deSPawel Jakub Dawidek sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 914c58794deSPawel Jakub Dawidek return (gp); 915c58794deSPawel Jakub Dawidek failed: 916c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 917c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 918c58794deSPawel Jakub Dawidek wakeup(sc); 919c58794deSPawel Jakub Dawidek /* 920c58794deSPawel Jakub Dawidek * Wait for kernel threads self destruction. 921c58794deSPawel Jakub Dawidek */ 922c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 923c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 924c58794deSPawel Jakub Dawidek "geli:destroy", 0); 925c58794deSPawel Jakub Dawidek } 926c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 927c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 928c58794deSPawel Jakub Dawidek if (cp->acr == 1) 929c58794deSPawel Jakub Dawidek g_access(cp, -1, -1, -1); 930c58794deSPawel Jakub Dawidek g_detach(cp); 931c58794deSPawel Jakub Dawidek } 932c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 933c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 9341e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 935c58794deSPawel Jakub Dawidek bzero(sc, sizeof(*sc)); 936c58794deSPawel Jakub Dawidek free(sc, M_ELI); 937c58794deSPawel Jakub Dawidek return (NULL); 938c58794deSPawel Jakub Dawidek } 939c58794deSPawel Jakub Dawidek 940c58794deSPawel Jakub Dawidek int 941c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 942c58794deSPawel Jakub Dawidek { 943c58794deSPawel Jakub Dawidek struct g_geom *gp; 944c58794deSPawel Jakub Dawidek struct g_provider *pp; 945c58794deSPawel Jakub Dawidek 946c58794deSPawel Jakub Dawidek g_topology_assert(); 947c58794deSPawel Jakub Dawidek 948c58794deSPawel Jakub Dawidek if (sc == NULL) 949c58794deSPawel Jakub Dawidek return (ENXIO); 950c58794deSPawel Jakub Dawidek 951c58794deSPawel Jakub Dawidek gp = sc->sc_geom; 952c58794deSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 953c58794deSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 954c58794deSPawel Jakub Dawidek if (force) { 955c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Device %s is still open, so it " 95698645006SChristian Brueffer "cannot be definitely removed.", pp->name); 95719351a14SAlexander Motin sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 95819351a14SAlexander Motin gp->access = g_eli_access; 95919351a14SAlexander Motin g_wither_provider(pp, ENXIO); 96019351a14SAlexander Motin return (EBUSY); 961c58794deSPawel Jakub Dawidek } else { 962c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, 963c58794deSPawel Jakub Dawidek "Device %s is still open (r%dw%de%d).", pp->name, 964c58794deSPawel Jakub Dawidek pp->acr, pp->acw, pp->ace); 965c58794deSPawel Jakub Dawidek return (EBUSY); 966c58794deSPawel Jakub Dawidek } 967c58794deSPawel Jakub Dawidek } 968c58794deSPawel Jakub Dawidek 969c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 970c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 971c58794deSPawel Jakub Dawidek wakeup(sc); 972c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 973c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 974c58794deSPawel Jakub Dawidek "geli:destroy", 0); 975c58794deSPawel Jakub Dawidek } 976c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 977c58794deSPawel Jakub Dawidek gp->softc = NULL; 9781e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 979c58794deSPawel Jakub Dawidek bzero(sc, sizeof(*sc)); 980c58794deSPawel Jakub Dawidek free(sc, M_ELI); 981c58794deSPawel Jakub Dawidek 982c58794deSPawel Jakub Dawidek if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 983c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 984c58794deSPawel Jakub Dawidek g_wither_geom_close(gp, ENXIO); 985c58794deSPawel Jakub Dawidek 986c58794deSPawel Jakub Dawidek return (0); 987c58794deSPawel Jakub Dawidek } 988c58794deSPawel Jakub Dawidek 989c58794deSPawel Jakub Dawidek static int 990c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused, 991c58794deSPawel Jakub Dawidek struct g_class *mp __unused, struct g_geom *gp) 992c58794deSPawel Jakub Dawidek { 993c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 994c58794deSPawel Jakub Dawidek 995c58794deSPawel Jakub Dawidek sc = gp->softc; 9965ad4a7c7SPawel Jakub Dawidek return (g_eli_destroy(sc, FALSE)); 997c58794deSPawel Jakub Dawidek } 998c58794deSPawel Jakub Dawidek 9999af2131bSPawel Jakub Dawidek static int 10009af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 10019af2131bSPawel Jakub Dawidek { 10021e189c08SMarcel Moolenaar u_char *keyfile, *data; 10039af2131bSPawel Jakub Dawidek char *file, name[64]; 10041e189c08SMarcel Moolenaar size_t size; 10059af2131bSPawel Jakub Dawidek int i; 10069af2131bSPawel Jakub Dawidek 10079af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 10089af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 10099af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1010edaa9008SPawel Jakub Dawidek if (keyfile == NULL && i == 0) { 1011edaa9008SPawel Jakub Dawidek /* 1012edaa9008SPawel Jakub Dawidek * If there is only one keyfile, allow simpler name. 1013edaa9008SPawel Jakub Dawidek */ 1014edaa9008SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 1015edaa9008SPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1016edaa9008SPawel Jakub Dawidek } 10179af2131bSPawel Jakub Dawidek if (keyfile == NULL) 10189af2131bSPawel Jakub Dawidek return (i); /* Return number of loaded keyfiles. */ 10191e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 10209af2131bSPawel Jakub Dawidek if (data == NULL) { 10219af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file data for %s.", 10229af2131bSPawel Jakub Dawidek name); 10239af2131bSPawel Jakub Dawidek return (0); 10249af2131bSPawel Jakub Dawidek } 10251e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 10261e189c08SMarcel Moolenaar if (size == 0) { 10279af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file size for %s.", 10289af2131bSPawel Jakub Dawidek name); 10299af2131bSPawel Jakub Dawidek return (0); 10309af2131bSPawel Jakub Dawidek } 10319af2131bSPawel Jakub Dawidek file = preload_search_info(keyfile, MODINFO_NAME); 10329af2131bSPawel Jakub Dawidek if (file == NULL) { 10339af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file name for %s.", 10349af2131bSPawel Jakub Dawidek name); 10359af2131bSPawel Jakub Dawidek return (0); 10369af2131bSPawel Jakub Dawidek } 10379af2131bSPawel Jakub Dawidek G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 10389af2131bSPawel Jakub Dawidek provider, name); 10391e189c08SMarcel Moolenaar g_eli_crypto_hmac_update(ctx, data, size); 10409af2131bSPawel Jakub Dawidek } 10419af2131bSPawel Jakub Dawidek } 10429af2131bSPawel Jakub Dawidek 10439af2131bSPawel Jakub Dawidek static void 10449af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider) 10459af2131bSPawel Jakub Dawidek { 10461e189c08SMarcel Moolenaar u_char *keyfile, *data; 10479af2131bSPawel Jakub Dawidek char name[64]; 10481e189c08SMarcel Moolenaar size_t size; 10499af2131bSPawel Jakub Dawidek int i; 10509af2131bSPawel Jakub Dawidek 10519af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 10529af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 10539af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 10549af2131bSPawel Jakub Dawidek if (keyfile == NULL) 10559af2131bSPawel Jakub Dawidek return; 10561e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 10571e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 10581e189c08SMarcel Moolenaar if (data != NULL && size != 0) 10591e189c08SMarcel Moolenaar bzero(data, size); 10609af2131bSPawel Jakub Dawidek } 10619af2131bSPawel Jakub Dawidek } 10629af2131bSPawel Jakub Dawidek 1063c58794deSPawel Jakub Dawidek /* 1064c58794deSPawel Jakub Dawidek * Tasting is only made on boot. 1065c58794deSPawel Jakub Dawidek * We detect providers which should be attached before root is mounted. 1066c58794deSPawel Jakub Dawidek */ 1067c58794deSPawel Jakub Dawidek static struct g_geom * 1068c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1069c58794deSPawel Jakub Dawidek { 1070c58794deSPawel Jakub Dawidek struct g_eli_metadata md; 1071c58794deSPawel Jakub Dawidek struct g_geom *gp; 1072c58794deSPawel Jakub Dawidek struct hmac_ctx ctx; 1073c58794deSPawel Jakub Dawidek char passphrase[256]; 1074c58794deSPawel Jakub Dawidek u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 10759af2131bSPawel Jakub Dawidek u_int i, nkey, nkeyfiles, tries; 1076c58794deSPawel Jakub Dawidek int error; 1077c58794deSPawel Jakub Dawidek 1078c58794deSPawel Jakub Dawidek g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1079c58794deSPawel Jakub Dawidek g_topology_assert(); 1080c58794deSPawel Jakub Dawidek 1081df3aed4fSPawel Jakub Dawidek if (root_mounted() || g_eli_tries == 0) 1082c58794deSPawel Jakub Dawidek return (NULL); 1083c58794deSPawel Jakub Dawidek 1084c58794deSPawel Jakub Dawidek G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1085c58794deSPawel Jakub Dawidek 1086c58794deSPawel Jakub Dawidek error = g_eli_read_metadata(mp, pp, &md); 1087c58794deSPawel Jakub Dawidek if (error != 0) 1088c58794deSPawel Jakub Dawidek return (NULL); 1089c58794deSPawel Jakub Dawidek gp = NULL; 1090c58794deSPawel Jakub Dawidek 1091c58794deSPawel Jakub Dawidek if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1092c58794deSPawel Jakub Dawidek return (NULL); 1093c58794deSPawel Jakub Dawidek if (md.md_version > G_ELI_VERSION) { 1094c58794deSPawel Jakub Dawidek printf("geom_eli.ko module is too old to handle %s.\n", 1095c58794deSPawel Jakub Dawidek pp->name); 1096c58794deSPawel Jakub Dawidek return (NULL); 1097c58794deSPawel Jakub Dawidek } 1098c58794deSPawel Jakub Dawidek if (md.md_provsize != pp->mediasize) 1099c58794deSPawel Jakub Dawidek return (NULL); 1100c58794deSPawel Jakub Dawidek /* Should we attach it on boot? */ 1101eaa3b919SPawel Jakub Dawidek if (!(md.md_flags & G_ELI_FLAG_BOOT)) 1102c58794deSPawel Jakub Dawidek return (NULL); 1103c58794deSPawel Jakub Dawidek if (md.md_keys == 0x00) { 1104c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1105c58794deSPawel Jakub Dawidek return (NULL); 1106c58794deSPawel Jakub Dawidek } 11079af2131bSPawel Jakub Dawidek if (md.md_iterations == -1) { 11089af2131bSPawel Jakub Dawidek /* If there is no passphrase, we try only once. */ 11099af2131bSPawel Jakub Dawidek tries = 1; 11109af2131bSPawel Jakub Dawidek } else { 11119af2131bSPawel Jakub Dawidek /* Ask for the passphrase no more than g_eli_tries times. */ 11129af2131bSPawel Jakub Dawidek tries = g_eli_tries; 11139af2131bSPawel Jakub Dawidek } 11149af2131bSPawel Jakub Dawidek 1115835c4dd4SColin Percival for (i = 0; i <= tries; i++) { 11169af2131bSPawel Jakub Dawidek g_eli_crypto_hmac_init(&ctx, NULL, 0); 1117c58794deSPawel Jakub Dawidek 1118c58794deSPawel Jakub Dawidek /* 11199af2131bSPawel Jakub Dawidek * Load all key files. 1120c58794deSPawel Jakub Dawidek */ 11219af2131bSPawel Jakub Dawidek nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 11229af2131bSPawel Jakub Dawidek 11239af2131bSPawel Jakub Dawidek if (nkeyfiles == 0 && md.md_iterations == -1) { 11249af2131bSPawel Jakub Dawidek /* 11259af2131bSPawel Jakub Dawidek * No key files and no passphrase, something is 11269af2131bSPawel Jakub Dawidek * definitely wrong here. 11279af2131bSPawel Jakub Dawidek * geli(8) doesn't allow for such situation, so assume 11289af2131bSPawel Jakub Dawidek * that there was really no passphrase and in that case 11299af2131bSPawel Jakub Dawidek * key files are no properly defined in loader.conf. 11309af2131bSPawel Jakub Dawidek */ 11319af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 11329af2131bSPawel Jakub Dawidek "Found no key files in loader.conf for %s.", 11339af2131bSPawel Jakub Dawidek pp->name); 11349af2131bSPawel Jakub Dawidek return (NULL); 11359af2131bSPawel Jakub Dawidek } 11369af2131bSPawel Jakub Dawidek 11379af2131bSPawel Jakub Dawidek /* Ask for the passphrase if defined. */ 11389af2131bSPawel Jakub Dawidek if (md.md_iterations >= 0) { 1139835c4dd4SColin Percival /* Try first with cached passphrase. */ 1140835c4dd4SColin Percival if (i == 0) { 1141835c4dd4SColin Percival if (!g_eli_boot_passcache) 1142835c4dd4SColin Percival continue; 1143835c4dd4SColin Percival memcpy(passphrase, cached_passphrase, 1144835c4dd4SColin Percival sizeof(passphrase)); 1145835c4dd4SColin Percival } else { 1146c58794deSPawel Jakub Dawidek printf("Enter passphrase for %s: ", pp->name); 1147f6ce353eSAndriy Gapon cngets(passphrase, sizeof(passphrase), 11489af2131bSPawel Jakub Dawidek g_eli_visible_passphrase); 1149835c4dd4SColin Percival memcpy(cached_passphrase, passphrase, 1150835c4dd4SColin Percival sizeof(passphrase)); 1151835c4dd4SColin Percival } 11529af2131bSPawel Jakub Dawidek } 11539af2131bSPawel Jakub Dawidek 1154c58794deSPawel Jakub Dawidek /* 1155c58794deSPawel Jakub Dawidek * Prepare Derived-Key from the user passphrase. 1156c58794deSPawel Jakub Dawidek */ 1157c58794deSPawel Jakub Dawidek if (md.md_iterations == 0) { 1158c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, md.md_salt, 1159c58794deSPawel Jakub Dawidek sizeof(md.md_salt)); 1160c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, passphrase, 1161c58794deSPawel Jakub Dawidek strlen(passphrase)); 11625527ecd9SPawel Jakub Dawidek bzero(passphrase, sizeof(passphrase)); 11639af2131bSPawel Jakub Dawidek } else if (md.md_iterations > 0) { 1164c58794deSPawel Jakub Dawidek u_char dkey[G_ELI_USERKEYLEN]; 1165c58794deSPawel Jakub Dawidek 1166c58794deSPawel Jakub Dawidek pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1167c58794deSPawel Jakub Dawidek sizeof(md.md_salt), passphrase, md.md_iterations); 11685527ecd9SPawel Jakub Dawidek bzero(passphrase, sizeof(passphrase)); 1169c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1170c58794deSPawel Jakub Dawidek bzero(dkey, sizeof(dkey)); 1171c58794deSPawel Jakub Dawidek } 11729af2131bSPawel Jakub Dawidek 1173c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_final(&ctx, key, 0); 1174c58794deSPawel Jakub Dawidek 1175c58794deSPawel Jakub Dawidek /* 1176c58794deSPawel Jakub Dawidek * Decrypt Master-Key. 1177c58794deSPawel Jakub Dawidek */ 1178c58794deSPawel Jakub Dawidek error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 1179c58794deSPawel Jakub Dawidek bzero(key, sizeof(key)); 1180c58794deSPawel Jakub Dawidek if (error == -1) { 1181835c4dd4SColin Percival if (i == tries) { 11829af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 11839af2131bSPawel Jakub Dawidek "Wrong key for %s. No tries left.", 11849af2131bSPawel Jakub Dawidek pp->name); 11859af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 11869af2131bSPawel Jakub Dawidek return (NULL); 1187c58794deSPawel Jakub Dawidek } 1188835c4dd4SColin Percival if (i > 0) { 1189835c4dd4SColin Percival G_ELI_DEBUG(0, 1190835c4dd4SColin Percival "Wrong key for %s. Tries left: %u.", 1191835c4dd4SColin Percival pp->name, tries - i); 1192835c4dd4SColin Percival } 1193c58794deSPawel Jakub Dawidek /* Try again. */ 1194c58794deSPawel Jakub Dawidek continue; 1195c58794deSPawel Jakub Dawidek } else if (error > 0) { 1196038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, 1197038c55adSPawel Jakub Dawidek "Cannot decrypt Master Key for %s (error=%d).", 1198c58794deSPawel Jakub Dawidek pp->name, error); 11999af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 1200c58794deSPawel Jakub Dawidek return (NULL); 1201c58794deSPawel Jakub Dawidek } 1202ebd05adaSBrad Davis g_eli_keyfiles_clear(pp->name); 1203c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1204c58794deSPawel Jakub Dawidek break; 1205c58794deSPawel Jakub Dawidek } 1206c58794deSPawel Jakub Dawidek 1207c58794deSPawel Jakub Dawidek /* 1208c58794deSPawel Jakub Dawidek * We have correct key, let's attach provider. 1209c58794deSPawel Jakub Dawidek */ 1210c58794deSPawel Jakub Dawidek gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 1211c58794deSPawel Jakub Dawidek bzero(mkey, sizeof(mkey)); 1212c58794deSPawel Jakub Dawidek bzero(&md, sizeof(md)); 1213c58794deSPawel Jakub Dawidek if (gp == NULL) { 1214c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1215c58794deSPawel Jakub Dawidek G_ELI_SUFFIX); 1216c58794deSPawel Jakub Dawidek return (NULL); 1217c58794deSPawel Jakub Dawidek } 1218c58794deSPawel Jakub Dawidek return (gp); 1219c58794deSPawel Jakub Dawidek } 1220c58794deSPawel Jakub Dawidek 1221c58794deSPawel Jakub Dawidek static void 1222c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1223c58794deSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 1224c58794deSPawel Jakub Dawidek { 1225c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1226c58794deSPawel Jakub Dawidek 1227c58794deSPawel Jakub Dawidek g_topology_assert(); 1228c58794deSPawel Jakub Dawidek sc = gp->softc; 1229c58794deSPawel Jakub Dawidek if (sc == NULL) 1230c58794deSPawel Jakub Dawidek return; 1231c58794deSPawel Jakub Dawidek if (pp != NULL || cp != NULL) 1232c58794deSPawel Jakub Dawidek return; /* Nothing here. */ 12331e09ff3dSPawel Jakub Dawidek 1234743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 12351e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_total); 1236743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 12371e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_allocated); 1238ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<Flags>", indent); 1239ea35a2ecSPawel Jakub Dawidek if (sc->sc_flags == 0) 1240ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "NONE"); 1241ea35a2ecSPawel Jakub Dawidek else { 1242ea35a2ecSPawel Jakub Dawidek int first = 1; 1243ea35a2ecSPawel Jakub Dawidek 1244ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name) do { \ 1245eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & (flag)) { \ 1246ea35a2ecSPawel Jakub Dawidek if (!first) \ 1247ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, ", "); \ 1248ea35a2ecSPawel Jakub Dawidek else \ 1249ea35a2ecSPawel Jakub Dawidek first = 0; \ 1250ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, name); \ 1251ea35a2ecSPawel Jakub Dawidek } \ 1252ea35a2ecSPawel Jakub Dawidek } while (0) 12535ad4a7c7SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1254c6a26d4cSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 12552bd4ade6SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1256ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1257ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1258ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1259ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1260eaa3b919SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1261ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1262ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 126385059016SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 126446e34470SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1265ea35a2ecSPawel Jakub Dawidek #undef ADD_FLAG 1266ea35a2ecSPawel Jakub Dawidek } 1267ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "</Flags>\n"); 1268ea35a2ecSPawel Jakub Dawidek 1269eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1270ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1271ea35a2ecSPawel Jakub Dawidek sc->sc_nkey); 1272ea35a2ecSPawel Jakub Dawidek } 12731f8c92e6SPawel Jakub Dawidek sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1274c58794deSPawel Jakub Dawidek sbuf_printf(sb, "%s<Crypto>", indent); 1275c58794deSPawel Jakub Dawidek switch (sc->sc_crypto) { 1276c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 1277c58794deSPawel Jakub Dawidek sbuf_printf(sb, "hardware"); 1278c58794deSPawel Jakub Dawidek break; 1279c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 1280c58794deSPawel Jakub Dawidek sbuf_printf(sb, "software"); 1281c58794deSPawel Jakub Dawidek break; 1282c58794deSPawel Jakub Dawidek default: 1283c58794deSPawel Jakub Dawidek sbuf_printf(sb, "UNKNOWN"); 1284c58794deSPawel Jakub Dawidek break; 1285c58794deSPawel Jakub Dawidek } 1286c58794deSPawel Jakub Dawidek sbuf_printf(sb, "</Crypto>\n"); 1287eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1288eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, 1289eaa3b919SPawel Jakub Dawidek "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1290eaa3b919SPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_aalgo)); 1291eaa3b919SPawel Jakub Dawidek } 1292eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1293eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1294038c55adSPawel Jakub Dawidek sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1295038c55adSPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_ealgo)); 1296d8d61ef8SPawel Jakub Dawidek sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1297d8d61ef8SPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1298c58794deSPawel Jakub Dawidek } 1299c58794deSPawel Jakub Dawidek 1300c5d387d0SPawel Jakub Dawidek static void 1301c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto) 1302c5d387d0SPawel Jakub Dawidek { 1303c5d387d0SPawel Jakub Dawidek struct g_class *mp; 1304c5d387d0SPawel Jakub Dawidek struct g_geom *gp, *gp2; 1305c5d387d0SPawel Jakub Dawidek struct g_provider *pp; 1306c5d387d0SPawel Jakub Dawidek struct g_eli_softc *sc; 1307c5d387d0SPawel Jakub Dawidek int error; 1308c5d387d0SPawel Jakub Dawidek 1309c5d387d0SPawel Jakub Dawidek mp = arg; 1310c5d387d0SPawel Jakub Dawidek DROP_GIANT(); 1311c5d387d0SPawel Jakub Dawidek g_topology_lock(); 1312c5d387d0SPawel Jakub Dawidek LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1313c5d387d0SPawel Jakub Dawidek sc = gp->softc; 1314c5d387d0SPawel Jakub Dawidek if (sc == NULL) 1315c5d387d0SPawel Jakub Dawidek continue; 1316c5d387d0SPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1317c5d387d0SPawel Jakub Dawidek KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 1318c5d387d0SPawel Jakub Dawidek if (pp->acr + pp->acw + pp->ace == 0) 13195ad4a7c7SPawel Jakub Dawidek error = g_eli_destroy(sc, TRUE); 1320c5d387d0SPawel Jakub Dawidek else { 1321c5d387d0SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1322c5d387d0SPawel Jakub Dawidek gp->access = g_eli_access; 1323c5d387d0SPawel Jakub Dawidek } 1324c5d387d0SPawel Jakub Dawidek } 1325c5d387d0SPawel Jakub Dawidek g_topology_unlock(); 1326c5d387d0SPawel Jakub Dawidek PICKUP_GIANT(); 1327c5d387d0SPawel Jakub Dawidek } 1328c5d387d0SPawel Jakub Dawidek 1329c5d387d0SPawel Jakub Dawidek static void 1330c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp) 1331c5d387d0SPawel Jakub Dawidek { 1332c5d387d0SPawel Jakub Dawidek 1333c5d387d0SPawel Jakub Dawidek g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1334c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1335c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync == NULL) 1336c5d387d0SPawel Jakub Dawidek G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1337c5d387d0SPawel Jakub Dawidek } 1338c5d387d0SPawel Jakub Dawidek 1339c5d387d0SPawel Jakub Dawidek static void 1340c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp) 1341c5d387d0SPawel Jakub Dawidek { 1342c5d387d0SPawel Jakub Dawidek 1343c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync != NULL) 1344c5d387d0SPawel Jakub Dawidek EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1345c5d387d0SPawel Jakub Dawidek } 1346c5d387d0SPawel Jakub Dawidek 1347c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1348f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 1349