1c58794deSPawel Jakub Dawidek /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 42f07cdf8SPawel Jakub Dawidek * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net> 5c58794deSPawel Jakub Dawidek * All rights reserved. 6c58794deSPawel Jakub Dawidek * 7c58794deSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 8c58794deSPawel Jakub Dawidek * modification, are permitted provided that the following conditions 9c58794deSPawel Jakub Dawidek * are met: 10c58794deSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 11c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 12c58794deSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 13c58794deSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 14c58794deSPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 15c58794deSPawel Jakub Dawidek * 16c58794deSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17c58794deSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18c58794deSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19c58794deSPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20c58794deSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21c58794deSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22c58794deSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23c58794deSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24c58794deSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25c58794deSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c58794deSPawel Jakub Dawidek * SUCH DAMAGE. 27c58794deSPawel Jakub Dawidek */ 28c58794deSPawel Jakub Dawidek 29c58794deSPawel Jakub Dawidek #include <sys/cdefs.h> 30c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 31c58794deSPawel Jakub Dawidek 32c58794deSPawel Jakub Dawidek #include <sys/param.h> 33c58794deSPawel Jakub Dawidek #include <sys/systm.h> 34f6ce353eSAndriy Gapon #include <sys/cons.h> 35c58794deSPawel Jakub Dawidek #include <sys/kernel.h> 369af2131bSPawel Jakub Dawidek #include <sys/linker.h> 37c58794deSPawel Jakub Dawidek #include <sys/module.h> 38c58794deSPawel Jakub Dawidek #include <sys/lock.h> 39c58794deSPawel Jakub Dawidek #include <sys/mutex.h> 40c58794deSPawel Jakub Dawidek #include <sys/bio.h> 415d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 42c58794deSPawel Jakub Dawidek #include <sys/sysctl.h> 43c58794deSPawel Jakub Dawidek #include <sys/malloc.h> 44c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h> 45c58794deSPawel Jakub Dawidek #include <sys/kthread.h> 46c58794deSPawel Jakub Dawidek #include <sys/proc.h> 47c58794deSPawel Jakub Dawidek #include <sys/sched.h> 48dd549194SPawel Jakub Dawidek #include <sys/smp.h> 49c58794deSPawel Jakub Dawidek #include <sys/uio.h> 50a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h> 51c58794deSPawel Jakub Dawidek 52c58794deSPawel Jakub Dawidek #include <vm/uma.h> 53c58794deSPawel Jakub Dawidek 54c58794deSPawel Jakub Dawidek #include <geom/geom.h> 55ac03832eSConrad Meyer #include <geom/geom_dbg.h> 56c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h> 57c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 58c58794deSPawel Jakub Dawidek 59ec5c0e5bSAllan Jude #include <crypto/intake.h> 60ec5c0e5bSAllan Jude 61cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module"); 62c58794deSPawel Jakub Dawidek 63c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 64c58794deSPawel Jakub Dawidek 65c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 6653a6215cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 6753a6215cSPawel Biernacki "GEOM_ELI stuff"); 68a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION; 69a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 70a1f4a8c4SPawel Jakub Dawidek "GELI version"); 71f95168e0SPawel Jakub Dawidek int g_eli_debug = 0; 72af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 73c58794deSPawel Jakub Dawidek "Debug level"); 74c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3; 75af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 7698645006SChristian Brueffer "Number of tries for entering the passphrase"); 77eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO; 78af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 79c58794deSPawel Jakub Dawidek &g_eli_visible_passphrase, 0, 80eb4c31fdSEd Schouten "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 81b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES; 82af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 8398645006SChristian Brueffer 0, "Number of times on-disk keys should be overwritten when destroying them"); 84dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0; 85af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 86c58794deSPawel Jakub Dawidek "Number of threads doing crypto work"); 87eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0; 88af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 89eaa3b919SPawel Jakub Dawidek "Use crypto operations batching"); 90c58794deSPawel Jakub Dawidek 91835c4dd4SColin Percival /* 92835c4dd4SColin Percival * Passphrase cached during boot, in order to be more user-friendly if 93835c4dd4SColin Percival * there are multiple providers using the same passphrase. 94835c4dd4SColin Percival */ 95835c4dd4SColin Percival static char cached_passphrase[256]; 96835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1; 97835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 98835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 99835c4dd4SColin Percival &g_eli_boot_passcache, 0, 100835c4dd4SColin Percival "Passphrases are cached during boot process for possible reuse"); 101835c4dd4SColin Percival static void 10266427784SColin Percival fetch_loader_passphrase(void * dummy) 10366427784SColin Percival { 10466427784SColin Percival char * env_passphrase; 10566427784SColin Percival 10666427784SColin Percival KASSERT(dynamic_kenv, ("need dynamic kenv")); 10766427784SColin Percival 10866427784SColin Percival if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 10966427784SColin Percival /* Extract passphrase from the environment. */ 11066427784SColin Percival strlcpy(cached_passphrase, env_passphrase, 11166427784SColin Percival sizeof(cached_passphrase)); 11266427784SColin Percival freeenv(env_passphrase); 11366427784SColin Percival 11466427784SColin Percival /* Wipe the passphrase from the environment. */ 11566427784SColin Percival kern_unsetenv("kern.geom.eli.passphrase"); 11666427784SColin Percival } 11766427784SColin Percival } 11866427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 11966427784SColin Percival fetch_loader_passphrase, NULL); 120ec5c0e5bSAllan Jude 12166427784SColin Percival static void 122ec5c0e5bSAllan Jude zero_boot_passcache(void) 123835c4dd4SColin Percival { 124835c4dd4SColin Percival 125ec5c0e5bSAllan Jude explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 126835c4dd4SColin Percival } 127ec5c0e5bSAllan Jude 128ec5c0e5bSAllan Jude static void 129ec5c0e5bSAllan Jude zero_geli_intake_keys(void) 130ec5c0e5bSAllan Jude { 131ec5c0e5bSAllan Jude struct keybuf *keybuf; 132ec5c0e5bSAllan Jude int i; 133ec5c0e5bSAllan Jude 134ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 135ec5c0e5bSAllan Jude /* Scan the key buffer, clear all GELI keys. */ 136ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 137ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 138ec5c0e5bSAllan Jude explicit_bzero(keybuf->kb_ents[i].ke_data, 139ec5c0e5bSAllan Jude sizeof(keybuf->kb_ents[i].ke_data)); 140ec5c0e5bSAllan Jude keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE; 141ec5c0e5bSAllan Jude } 142ec5c0e5bSAllan Jude } 143ec5c0e5bSAllan Jude } 144ec5c0e5bSAllan Jude } 145ec5c0e5bSAllan Jude 146ec5c0e5bSAllan Jude static void 147ec5c0e5bSAllan Jude zero_intake_passcache(void *dummy) 148ec5c0e5bSAllan Jude { 149ec5c0e5bSAllan Jude zero_boot_passcache(); 150ec5c0e5bSAllan Jude zero_geli_intake_keys(); 151ec5c0e5bSAllan Jude } 152ec5c0e5bSAllan Jude EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0); 153835c4dd4SColin Percival 154c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL; 155c5d387d0SPawel Jakub Dawidek 1562f07cdf8SPawel Jakub Dawidek static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 1572f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md); 1582f07cdf8SPawel Jakub Dawidek 159c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 160c58794deSPawel Jakub Dawidek struct g_geom *gp); 161c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp); 162c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp); 163c58794deSPawel Jakub Dawidek 164c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste; 165c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf; 166c58794deSPawel Jakub Dawidek 167c58794deSPawel Jakub Dawidek struct g_class g_eli_class = { 168c58794deSPawel Jakub Dawidek .name = G_ELI_CLASS_NAME, 169c58794deSPawel Jakub Dawidek .version = G_VERSION, 170c58794deSPawel Jakub Dawidek .ctlreq = g_eli_config, 171c58794deSPawel Jakub Dawidek .taste = g_eli_taste, 172c5d387d0SPawel Jakub Dawidek .destroy_geom = g_eli_destroy_geom, 173c5d387d0SPawel Jakub Dawidek .init = g_eli_init, 174c5d387d0SPawel Jakub Dawidek .fini = g_eli_fini 175c58794deSPawel Jakub Dawidek }; 176c58794deSPawel Jakub Dawidek 177c58794deSPawel Jakub Dawidek 178c58794deSPawel Jakub Dawidek /* 179c58794deSPawel Jakub Dawidek * Code paths: 180c58794deSPawel Jakub Dawidek * BIO_READ: 1815ad4a7c7SPawel 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 182c58794deSPawel Jakub Dawidek * BIO_WRITE: 183c58794deSPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 184c58794deSPawel Jakub Dawidek */ 185c58794deSPawel Jakub Dawidek 186c58794deSPawel Jakub Dawidek 187c58794deSPawel Jakub Dawidek /* 188c58794deSPawel Jakub Dawidek * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 189c58794deSPawel Jakub Dawidek * accelerator or something like this. 190c58794deSPawel Jakub Dawidek * The function updates the SID and rerun the operation. 191c58794deSPawel Jakub Dawidek */ 192eaa3b919SPawel Jakub Dawidek int 193c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp) 194c58794deSPawel Jakub Dawidek { 195c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 196c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 197c58794deSPawel Jakub Dawidek struct bio *bp; 198c58794deSPawel Jakub Dawidek int error; 199c58794deSPawel Jakub Dawidek 200c58794deSPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 201c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 202c58794deSPawel Jakub Dawidek LIST_FOREACH(wr, &sc->sc_workers, w_next) { 203c58794deSPawel Jakub Dawidek if (wr->w_number == bp->bio_pflags) 204c58794deSPawel Jakub Dawidek break; 205c58794deSPawel Jakub Dawidek } 206c58794deSPawel Jakub Dawidek KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 2071b0909d5SConrad Meyer G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).", 2081b0909d5SConrad Meyer bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid, 2091b0909d5SConrad Meyer crp->crp_session); 2101b0909d5SConrad Meyer wr->w_sid = crp->crp_session; 211c58794deSPawel Jakub Dawidek crp->crp_etype = 0; 212c58794deSPawel Jakub Dawidek error = crypto_dispatch(crp); 213c58794deSPawel Jakub Dawidek if (error == 0) 214c58794deSPawel Jakub Dawidek return (0); 215c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 216c58794deSPawel Jakub Dawidek crp->crp_etype = error; 217c58794deSPawel Jakub Dawidek return (error); 218c58794deSPawel Jakub Dawidek } 219c58794deSPawel Jakub Dawidek 2200bab7fa8SAlan Somers static void 2210bab7fa8SAlan Somers g_eli_getattr_done(struct bio *bp) 2220bab7fa8SAlan Somers { 2230bab7fa8SAlan Somers if (bp->bio_error == 0 && 2240bab7fa8SAlan Somers !strcmp(bp->bio_attribute, "GEOM::physpath")) { 2250bab7fa8SAlan Somers strlcat(bp->bio_data, "/eli", bp->bio_length); 2260bab7fa8SAlan Somers } 2270bab7fa8SAlan Somers g_std_done(bp); 2280bab7fa8SAlan Somers } 2290bab7fa8SAlan Somers 230c58794deSPawel Jakub Dawidek /* 231c58794deSPawel Jakub Dawidek * The function is called afer reading encrypted data from the provider. 232bb30fea6SPawel Jakub Dawidek * 2335ad4a7c7SPawel 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 234c58794deSPawel Jakub Dawidek */ 235eaa3b919SPawel Jakub Dawidek void 236c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp) 237c58794deSPawel Jakub Dawidek { 238c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 239c58794deSPawel Jakub Dawidek struct bio *pbp; 240c58794deSPawel Jakub Dawidek 241c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 242c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2432dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 244c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 24590574b0aSMikolaj Golub g_destroy_bio(bp); 246eaa3b919SPawel Jakub Dawidek /* 247eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 248eaa3b919SPawel Jakub Dawidek */ 249eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 250eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 251eaa3b919SPawel Jakub Dawidek return; 2525ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 253c58794deSPawel Jakub Dawidek if (pbp->bio_error != 0) { 2542dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 2552dc7e36bSSteven Hartland pbp->bio_error); 256c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 257eaa3b919SPawel Jakub Dawidek if (pbp->bio_driver2 != NULL) { 258eaa3b919SPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 259eaa3b919SPawel Jakub Dawidek pbp->bio_driver2 = NULL; 260eaa3b919SPawel Jakub Dawidek } 261c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 26278f79a9aSMariusz Zaborski if (sc != NULL) 2635ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 264c58794deSPawel Jakub Dawidek return; 265c58794deSPawel Jakub Dawidek } 266c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 267c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, pbp); 268c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 269c58794deSPawel Jakub Dawidek wakeup(sc); 270c58794deSPawel Jakub Dawidek } 271c58794deSPawel Jakub Dawidek 272c58794deSPawel Jakub Dawidek /* 273c58794deSPawel Jakub Dawidek * The function is called after we encrypt and write data. 274bb30fea6SPawel Jakub Dawidek * 275bb30fea6SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 276c58794deSPawel Jakub Dawidek */ 277eaa3b919SPawel Jakub Dawidek void 278c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp) 279c58794deSPawel Jakub Dawidek { 2805ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 281c58794deSPawel Jakub Dawidek struct bio *pbp; 282c58794deSPawel Jakub Dawidek 283c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 284c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2852dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 286c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 28790574b0aSMikolaj Golub g_destroy_bio(bp); 288eaa3b919SPawel Jakub Dawidek /* 289eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 290eaa3b919SPawel Jakub Dawidek */ 291eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 292eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 293eaa3b919SPawel Jakub Dawidek return; 294c58794deSPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 295c58794deSPawel Jakub Dawidek pbp->bio_driver2 = NULL; 296eaa3b919SPawel Jakub Dawidek if (pbp->bio_error != 0) { 2972dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 298c58794deSPawel Jakub Dawidek pbp->bio_error); 299c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 3002dc7e36bSSteven Hartland } else 3012dc7e36bSSteven Hartland pbp->bio_completed = pbp->bio_length; 3022dc7e36bSSteven Hartland 303c58794deSPawel Jakub Dawidek /* 304c58794deSPawel Jakub Dawidek * Write is finished, send it up. 305c58794deSPawel Jakub Dawidek */ 3065ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 307c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 30878f79a9aSMariusz Zaborski if (sc != NULL) 3095ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 310c58794deSPawel Jakub Dawidek } 311c58794deSPawel Jakub Dawidek 312c58794deSPawel Jakub Dawidek /* 313c58794deSPawel Jakub Dawidek * This function should never be called, but GEOM made as it set ->orphan() 314c58794deSPawel Jakub Dawidek * method for every geom. 315c58794deSPawel Jakub Dawidek */ 316c58794deSPawel Jakub Dawidek static void 317c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp) 318c58794deSPawel Jakub Dawidek { 319c58794deSPawel Jakub Dawidek 320c58794deSPawel Jakub Dawidek panic("Function %s() called for %s.", __func__, cp->geom->name); 321c58794deSPawel Jakub Dawidek } 322c58794deSPawel Jakub Dawidek 323c58794deSPawel Jakub Dawidek static void 324c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp) 325c58794deSPawel Jakub Dawidek { 326c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 327c58794deSPawel Jakub Dawidek 328c58794deSPawel Jakub Dawidek g_topology_assert(); 329c58794deSPawel Jakub Dawidek sc = cp->geom->softc; 330c58794deSPawel Jakub Dawidek if (sc == NULL) 331c58794deSPawel Jakub Dawidek return; 3325ad4a7c7SPawel Jakub Dawidek g_eli_destroy(sc, TRUE); 333c58794deSPawel Jakub Dawidek } 334c58794deSPawel Jakub Dawidek 3352f07cdf8SPawel Jakub Dawidek static void 3362f07cdf8SPawel Jakub Dawidek g_eli_resize(struct g_consumer *cp) 3372f07cdf8SPawel Jakub Dawidek { 3382f07cdf8SPawel Jakub Dawidek struct g_eli_softc *sc; 3392f07cdf8SPawel Jakub Dawidek struct g_provider *epp, *pp; 3402f07cdf8SPawel Jakub Dawidek off_t oldsize; 3412f07cdf8SPawel Jakub Dawidek 3422f07cdf8SPawel Jakub Dawidek g_topology_assert(); 3432f07cdf8SPawel Jakub Dawidek sc = cp->geom->softc; 3442f07cdf8SPawel Jakub Dawidek if (sc == NULL) 3452f07cdf8SPawel Jakub Dawidek return; 3462f07cdf8SPawel Jakub Dawidek 3472f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) { 3482f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.", 3492f07cdf8SPawel Jakub Dawidek (intmax_t)sc->sc_provsize); 3502f07cdf8SPawel Jakub Dawidek return; 3512f07cdf8SPawel Jakub Dawidek } 3522f07cdf8SPawel Jakub Dawidek 3532f07cdf8SPawel Jakub Dawidek pp = cp->provider; 3542f07cdf8SPawel Jakub Dawidek 3552f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 3562f07cdf8SPawel Jakub Dawidek struct g_eli_metadata md; 3572f07cdf8SPawel Jakub Dawidek u_char *sector; 3582f07cdf8SPawel Jakub Dawidek int error; 3592f07cdf8SPawel Jakub Dawidek 3602f07cdf8SPawel Jakub Dawidek sector = NULL; 3612f07cdf8SPawel Jakub Dawidek 3622f07cdf8SPawel Jakub Dawidek error = g_eli_read_metadata_offset(cp->geom->class, pp, 3632f07cdf8SPawel Jakub Dawidek sc->sc_provsize - pp->sectorsize, &md); 3642f07cdf8SPawel Jakub Dawidek if (error != 0) { 3652f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).", 3662f07cdf8SPawel Jakub Dawidek pp->name, error); 3672f07cdf8SPawel Jakub Dawidek goto iofail; 3682f07cdf8SPawel Jakub Dawidek } 3692f07cdf8SPawel Jakub Dawidek 3702f07cdf8SPawel Jakub Dawidek md.md_provsize = pp->mediasize; 3712f07cdf8SPawel Jakub Dawidek 3722f07cdf8SPawel Jakub Dawidek sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 3732f07cdf8SPawel Jakub Dawidek eli_metadata_encode(&md, sector); 3742f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 3752f07cdf8SPawel Jakub Dawidek pp->sectorsize); 3762f07cdf8SPawel Jakub Dawidek if (error != 0) { 3772f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).", 3782f07cdf8SPawel Jakub Dawidek pp->name, error); 3792f07cdf8SPawel Jakub Dawidek goto iofail; 3802f07cdf8SPawel Jakub Dawidek } 3812f07cdf8SPawel Jakub Dawidek explicit_bzero(sector, pp->sectorsize); 3822f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, sc->sc_provsize - pp->sectorsize, 3832f07cdf8SPawel Jakub Dawidek sector, pp->sectorsize); 3842f07cdf8SPawel Jakub Dawidek if (error != 0) { 3852f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).", 3862f07cdf8SPawel Jakub Dawidek pp->name, error); 3872f07cdf8SPawel Jakub Dawidek goto iofail; 3882f07cdf8SPawel Jakub Dawidek } 3892f07cdf8SPawel Jakub Dawidek iofail: 3902f07cdf8SPawel Jakub Dawidek explicit_bzero(&md, sizeof(md)); 3914a711b8dSJohn Baldwin zfree(sector, M_ELI); 3922f07cdf8SPawel Jakub Dawidek } 3932f07cdf8SPawel Jakub Dawidek 3942f07cdf8SPawel Jakub Dawidek oldsize = sc->sc_mediasize; 3952f07cdf8SPawel Jakub Dawidek sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize); 3962f07cdf8SPawel Jakub Dawidek g_eli_key_resize(sc); 3972f07cdf8SPawel Jakub Dawidek sc->sc_provsize = pp->mediasize; 3982f07cdf8SPawel Jakub Dawidek 3992f07cdf8SPawel Jakub Dawidek epp = LIST_FIRST(&sc->sc_geom->provider); 4002f07cdf8SPawel Jakub Dawidek g_resize_provider(epp, sc->sc_mediasize); 4012f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name, 4022f07cdf8SPawel Jakub Dawidek (intmax_t)oldsize, (intmax_t)sc->sc_mediasize); 4032f07cdf8SPawel Jakub Dawidek } 4042f07cdf8SPawel Jakub Dawidek 405bb30fea6SPawel Jakub Dawidek /* 406056638c4SPawel Jakub Dawidek * BIO_READ: 4075ad4a7c7SPawel 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 408056638c4SPawel Jakub Dawidek * BIO_WRITE: 409056638c4SPawel Jakub Dawidek * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 410bb30fea6SPawel Jakub Dawidek */ 411c58794deSPawel Jakub Dawidek static void 412c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp) 413c58794deSPawel Jakub Dawidek { 414c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 415d3a1be90SPawel Jakub Dawidek struct g_consumer *cp; 416c58794deSPawel Jakub Dawidek struct bio *cbp; 417c58794deSPawel Jakub Dawidek 418c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 419c58794deSPawel Jakub Dawidek KASSERT(sc != NULL, 420c58794deSPawel Jakub Dawidek ("Provider's error should be set (error=%d)(device=%s).", 421c58794deSPawel Jakub Dawidek bp->bio_to->error, bp->bio_to->name)); 422c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request received."); 423c58794deSPawel Jakub Dawidek 424c58794deSPawel Jakub Dawidek switch (bp->bio_cmd) { 425c58794deSPawel Jakub Dawidek case BIO_READ: 426c58794deSPawel Jakub Dawidek case BIO_WRITE: 427d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 42842461fbaSPawel Jakub Dawidek case BIO_FLUSH: 4299a6844d5SKenneth D. Merry case BIO_ZONE: 4308b522bdaSWarner Losh case BIO_SPEEDUP: 431c58794deSPawel Jakub Dawidek break; 432c58794deSPawel Jakub Dawidek case BIO_DELETE: 433c58794deSPawel Jakub Dawidek /* 43446e34470SPawel Jakub Dawidek * If the user hasn't set the NODELETE flag, we just pass 43546e34470SPawel Jakub Dawidek * it down the stack and let the layers beneath us do (or 43646e34470SPawel Jakub Dawidek * not) whatever they do with it. If they have, we 43746e34470SPawel Jakub Dawidek * reject it. A possible extension would be an 43846e34470SPawel Jakub Dawidek * additional flag to take it as a hint to shred the data 43946e34470SPawel Jakub Dawidek * with [multiple?] overwrites. 440c58794deSPawel Jakub Dawidek */ 44146e34470SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 44246e34470SPawel Jakub Dawidek break; 443c58794deSPawel Jakub Dawidek default: 444c58794deSPawel Jakub Dawidek g_io_deliver(bp, EOPNOTSUPP); 445c58794deSPawel Jakub Dawidek return; 446c58794deSPawel Jakub Dawidek } 447c58794deSPawel Jakub Dawidek cbp = g_clone_bio(bp); 448c58794deSPawel Jakub Dawidek if (cbp == NULL) { 449c58794deSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 450c58794deSPawel Jakub Dawidek return; 451c58794deSPawel Jakub Dawidek } 4525ad4a7c7SPawel Jakub Dawidek bp->bio_driver1 = cbp; 4535ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = G_ELI_NEW_BIO; 454d3a1be90SPawel Jakub Dawidek switch (bp->bio_cmd) { 455d3a1be90SPawel Jakub Dawidek case BIO_READ: 456eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 4575ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 0); 458d3a1be90SPawel Jakub Dawidek break; 459eaa3b919SPawel Jakub Dawidek } 460eaa3b919SPawel Jakub Dawidek /* FALLTHROUGH */ 461d3a1be90SPawel Jakub Dawidek case BIO_WRITE: 462c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 463c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp); 464c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 465c58794deSPawel Jakub Dawidek wakeup(sc); 466d3a1be90SPawel Jakub Dawidek break; 467d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 46842461fbaSPawel Jakub Dawidek case BIO_FLUSH: 46946e34470SPawel Jakub Dawidek case BIO_DELETE: 4708b522bdaSWarner Losh case BIO_SPEEDUP: 4719a6844d5SKenneth D. Merry case BIO_ZONE: 4720bab7fa8SAlan Somers if (bp->bio_cmd == BIO_GETATTR) 4730bab7fa8SAlan Somers cbp->bio_done = g_eli_getattr_done; 4740bab7fa8SAlan Somers else 475d3a1be90SPawel Jakub Dawidek cbp->bio_done = g_std_done; 476d3a1be90SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 477d3a1be90SPawel Jakub Dawidek cbp->bio_to = cp->provider; 478cd0d707eSPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 479d3a1be90SPawel Jakub Dawidek g_io_request(cbp, cp); 480d3a1be90SPawel Jakub Dawidek break; 481c58794deSPawel Jakub Dawidek } 482c58794deSPawel Jakub Dawidek } 483c58794deSPawel Jakub Dawidek 4843ac01bc2SPawel Jakub Dawidek static int 4853ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr) 4863ac01bc2SPawel Jakub Dawidek { 4873ac01bc2SPawel Jakub Dawidek struct g_eli_softc *sc; 488c0341432SJohn Baldwin struct crypto_session_params csp; 489a3d565a1SJohn Baldwin uint32_t caps; 490a3d565a1SJohn Baldwin int error, new_crypto; 491c0341432SJohn Baldwin void *key; 4923ac01bc2SPawel Jakub Dawidek 4933ac01bc2SPawel Jakub Dawidek sc = wr->w_softc; 4943ac01bc2SPawel Jakub Dawidek 495c0341432SJohn Baldwin memset(&csp, 0, sizeof(csp)); 496c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_CIPHER; 497c0341432SJohn Baldwin csp.csp_cipher_alg = sc->sc_ealgo; 498c0341432SJohn Baldwin csp.csp_ivlen = g_eli_ivlen(sc->sc_ealgo); 499c0341432SJohn Baldwin csp.csp_cipher_klen = sc->sc_ekeylen / 8; 5003ac01bc2SPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 501c0341432SJohn Baldwin csp.csp_cipher_klen <<= 1; 502ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 503c0341432SJohn Baldwin key = g_eli_key_hold(sc, 0, 504ad0a5236SPawel Jakub Dawidek LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 505c0341432SJohn Baldwin csp.csp_cipher_key = key; 506ad0a5236SPawel Jakub Dawidek } else { 507c0341432SJohn Baldwin key = NULL; 508c0341432SJohn Baldwin csp.csp_cipher_key = sc->sc_ekey; 509ad0a5236SPawel Jakub Dawidek } 5103ac01bc2SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 511c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_ETA; 512c0341432SJohn Baldwin csp.csp_auth_alg = sc->sc_aalgo; 513c0341432SJohn Baldwin csp.csp_auth_klen = G_ELI_AUTH_SECKEYLEN; 5143ac01bc2SPawel Jakub Dawidek } 5153ac01bc2SPawel Jakub Dawidek 5163ac01bc2SPawel Jakub Dawidek switch (sc->sc_crypto) { 517a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 5183ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 519c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5203ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 5213ac01bc2SPawel Jakub Dawidek break; 5223ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 523c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5243ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 5253ac01bc2SPawel Jakub Dawidek break; 5263ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_UNKNOWN: 527c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 528a3d565a1SJohn Baldwin CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE); 5293ac01bc2SPawel Jakub Dawidek if (error == 0) { 530a3d565a1SJohn Baldwin caps = crypto_ses2caps(wr->w_sid); 531a3d565a1SJohn Baldwin if (caps & CRYPTOCAP_F_HARDWARE) 532a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_HW; 533a3d565a1SJohn Baldwin else if (caps & CRYPTOCAP_F_ACCEL_SOFTWARE) 534a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW_ACCEL; 535a3d565a1SJohn Baldwin else 536a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW; 5373ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 5383ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 539a3d565a1SJohn Baldwin sc->sc_crypto = new_crypto; 5403ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5413ac01bc2SPawel Jakub Dawidek } 5423ac01bc2SPawel Jakub Dawidek break; 5433ac01bc2SPawel Jakub Dawidek default: 5443ac01bc2SPawel Jakub Dawidek panic("%s: invalid condition", __func__); 5453ac01bc2SPawel Jakub Dawidek } 5463ac01bc2SPawel Jakub Dawidek 547c0341432SJohn Baldwin if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 548c0341432SJohn Baldwin if (error) 549c0341432SJohn Baldwin g_eli_key_drop(sc, key); 550c0341432SJohn Baldwin else 551c0341432SJohn Baldwin wr->w_first_key = key; 552c0341432SJohn Baldwin } 553ad0a5236SPawel Jakub Dawidek 5543ac01bc2SPawel Jakub Dawidek return (error); 5553ac01bc2SPawel Jakub Dawidek } 5563ac01bc2SPawel Jakub Dawidek 5573ac01bc2SPawel Jakub Dawidek static void 5583ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr) 5593ac01bc2SPawel Jakub Dawidek { 560c0341432SJohn Baldwin struct g_eli_softc *sc; 5613ac01bc2SPawel Jakub Dawidek 5623ac01bc2SPawel Jakub Dawidek crypto_freesession(wr->w_sid); 563c0341432SJohn Baldwin if (wr->w_first_key != NULL) { 564c0341432SJohn Baldwin sc = wr->w_softc; 565c0341432SJohn Baldwin g_eli_key_drop(sc, wr->w_first_key); 566c0341432SJohn Baldwin wr->w_first_key = NULL; 567c0341432SJohn Baldwin } 5683ac01bc2SPawel Jakub Dawidek } 5693ac01bc2SPawel Jakub Dawidek 5705ad4a7c7SPawel Jakub Dawidek static void 5715ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc) 5725ad4a7c7SPawel Jakub Dawidek { 5735ad4a7c7SPawel Jakub Dawidek struct bio *bp; 5745ad4a7c7SPawel Jakub Dawidek 5755ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 5765ad4a7c7SPawel Jakub Dawidek 5775ad4a7c7SPawel Jakub Dawidek while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 5785ad4a7c7SPawel Jakub Dawidek KASSERT(bp->bio_pflags == G_ELI_NEW_BIO, 5795ad4a7c7SPawel Jakub Dawidek ("Not new bio when canceling (bp=%p).", bp)); 5805ad4a7c7SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 5815ad4a7c7SPawel Jakub Dawidek } 5825ad4a7c7SPawel Jakub Dawidek } 5835ad4a7c7SPawel Jakub Dawidek 5845ad4a7c7SPawel Jakub Dawidek static struct bio * 5855ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc) 5865ad4a7c7SPawel Jakub Dawidek { 5875ad4a7c7SPawel Jakub Dawidek struct bio *bp; 5885ad4a7c7SPawel Jakub Dawidek 5895ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 5905ad4a7c7SPawel Jakub Dawidek 5915ad4a7c7SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 5925ad4a7c7SPawel Jakub Dawidek return (bioq_takefirst(&sc->sc_queue)); 5935ad4a7c7SPawel Jakub Dawidek /* 5945ad4a7c7SPawel Jakub Dawidek * Device suspended, so we skip new I/O requests. 5955ad4a7c7SPawel Jakub Dawidek */ 5965ad4a7c7SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 5975ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags != G_ELI_NEW_BIO) 5985ad4a7c7SPawel Jakub Dawidek break; 5995ad4a7c7SPawel Jakub Dawidek } 6005ad4a7c7SPawel Jakub Dawidek if (bp != NULL) 6015ad4a7c7SPawel Jakub Dawidek bioq_remove(&sc->sc_queue, bp); 6025ad4a7c7SPawel Jakub Dawidek return (bp); 6035ad4a7c7SPawel Jakub Dawidek } 6045ad4a7c7SPawel Jakub Dawidek 605c58794deSPawel Jakub Dawidek /* 606c58794deSPawel Jakub Dawidek * This is the main function for kernel worker thread when we don't have 607c58794deSPawel Jakub Dawidek * hardware acceleration and we have to do cryptography in software. 608c58794deSPawel Jakub Dawidek * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 609c58794deSPawel Jakub Dawidek * threads with crypto work. 610c58794deSPawel Jakub Dawidek */ 611c58794deSPawel Jakub Dawidek static void 612c58794deSPawel Jakub Dawidek g_eli_worker(void *arg) 613c58794deSPawel Jakub Dawidek { 614c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 615c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 616c58794deSPawel Jakub Dawidek struct bio *bp; 6173ac01bc2SPawel Jakub Dawidek int error; 618c58794deSPawel Jakub Dawidek 619c58794deSPawel Jakub Dawidek wr = arg; 620c58794deSPawel Jakub Dawidek sc = wr->w_softc; 621fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 622fdce57a0SJohn Baldwin MPASS(!sc->sc_cpubind || smp_started); 623fdce57a0SJohn Baldwin #elif defined(SMP) 624a1ea1a22SPawel Jakub Dawidek /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 6250c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) { 626a1ea1a22SPawel Jakub Dawidek while (!smp_started) 627a1ea1a22SPawel Jakub Dawidek tsleep(wr, 0, "geli:smp", hz / 4); 628a1ea1a22SPawel Jakub Dawidek } 629a1ea1a22SPawel Jakub Dawidek #endif 630982d11f8SJeff Roberson thread_lock(curthread); 63131c4cef7SPawel Jakub Dawidek sched_prio(curthread, PUSER); 6320c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) 6330c879bd9SPawel Jakub Dawidek sched_bind(curthread, wr->w_number % mp_ncpus); 634982d11f8SJeff Roberson thread_unlock(curthread); 635c58794deSPawel Jakub Dawidek 636c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 637c58794deSPawel Jakub Dawidek 638c58794deSPawel Jakub Dawidek for (;;) { 639c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 6405ad4a7c7SPawel Jakub Dawidek again: 6415ad4a7c7SPawel Jakub Dawidek bp = g_eli_takefirst(sc); 642c58794deSPawel Jakub Dawidek if (bp == NULL) { 643eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 6445ad4a7c7SPawel Jakub Dawidek g_eli_cancel(sc); 645c58794deSPawel Jakub Dawidek LIST_REMOVE(wr, w_next); 6463ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 647c58794deSPawel Jakub Dawidek free(wr, M_ELI); 648c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s exiting.", 649c58794deSPawel Jakub Dawidek curthread->td_proc->p_comm); 650c58794deSPawel Jakub Dawidek wakeup(&sc->sc_workers); 651c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6523745c395SJulian Elischer kproc_exit(0); 653c58794deSPawel Jakub Dawidek } 6545ad4a7c7SPawel Jakub Dawidek while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 6555ad4a7c7SPawel Jakub Dawidek if (sc->sc_inflight > 0) { 656038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, "inflight=%d", 657038c55adSPawel Jakub Dawidek sc->sc_inflight); 6585ad4a7c7SPawel Jakub Dawidek /* 6595ad4a7c7SPawel Jakub Dawidek * We still have inflight BIOs, so 6605ad4a7c7SPawel Jakub Dawidek * sleep and retry. 6615ad4a7c7SPawel Jakub Dawidek */ 6625ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 6635ad4a7c7SPawel Jakub Dawidek "geli:inf", hz / 5); 6645ad4a7c7SPawel Jakub Dawidek goto again; 6655ad4a7c7SPawel Jakub Dawidek } 6665ad4a7c7SPawel Jakub Dawidek /* 6675ad4a7c7SPawel Jakub Dawidek * Suspend requested, mark the worker as 6685ad4a7c7SPawel Jakub Dawidek * suspended and go to sleep. 6695ad4a7c7SPawel Jakub Dawidek */ 6703ac01bc2SPawel Jakub Dawidek if (wr->w_active) { 6713ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 6723ac01bc2SPawel Jakub Dawidek wr->w_active = FALSE; 6733ac01bc2SPawel Jakub Dawidek } 6745ad4a7c7SPawel Jakub Dawidek wakeup(&sc->sc_workers); 6755ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 6765ad4a7c7SPawel Jakub Dawidek "geli:suspend", 0); 6773ac01bc2SPawel Jakub Dawidek if (!wr->w_active && 6783ac01bc2SPawel Jakub Dawidek !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 6793ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 6803ac01bc2SPawel Jakub Dawidek KASSERT(error == 0, 6813ac01bc2SPawel Jakub Dawidek ("g_eli_newsession() failed on resume (error=%d)", 6823ac01bc2SPawel Jakub Dawidek error)); 6833ac01bc2SPawel Jakub Dawidek wr->w_active = TRUE; 6843ac01bc2SPawel Jakub Dawidek } 6855ad4a7c7SPawel Jakub Dawidek goto again; 6865ad4a7c7SPawel Jakub Dawidek } 68731c4cef7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 688c58794deSPawel Jakub Dawidek continue; 689c58794deSPawel Jakub Dawidek } 6905ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) 6915ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1); 692c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6935ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) { 6945ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = 0; 6955ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 6965ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 697eaa3b919SPawel Jakub Dawidek g_eli_auth_read(sc, bp); 6985ad4a7c7SPawel Jakub Dawidek else 6995ad4a7c7SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 7005ad4a7c7SPawel Jakub Dawidek } else { 7015ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 7025ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 1); 7035ad4a7c7SPawel Jakub Dawidek else 7045ad4a7c7SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 7055ad4a7c7SPawel Jakub Dawidek } 7065ad4a7c7SPawel Jakub Dawidek } else { 7075ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 708eaa3b919SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 709eaa3b919SPawel Jakub Dawidek else 710dddd1d53SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 711c58794deSPawel Jakub Dawidek } 712c58794deSPawel Jakub Dawidek } 7135ad4a7c7SPawel Jakub Dawidek } 714c58794deSPawel Jakub Dawidek 7152f07cdf8SPawel Jakub Dawidek static int 7162f07cdf8SPawel Jakub Dawidek g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 7172f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md) 718c58794deSPawel Jakub Dawidek { 719c58794deSPawel Jakub Dawidek struct g_geom *gp; 720c58794deSPawel Jakub Dawidek struct g_consumer *cp; 721c58794deSPawel Jakub Dawidek u_char *buf = NULL; 722c58794deSPawel Jakub Dawidek int error; 723c58794deSPawel Jakub Dawidek 724c58794deSPawel Jakub Dawidek g_topology_assert(); 725c58794deSPawel Jakub Dawidek 726c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "eli:taste"); 727c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 728c58794deSPawel Jakub Dawidek gp->access = g_std_access; 729c58794deSPawel Jakub Dawidek /* 730c58794deSPawel Jakub Dawidek * g_eli_read_metadata() is always called from the event thread. 731c58794deSPawel Jakub Dawidek * Our geom is created and destroyed in the same event, so there 732c58794deSPawel Jakub Dawidek * could be no orphan nor spoil event in the meantime. 733c58794deSPawel Jakub Dawidek */ 734c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan_spoil_assert; 735c58794deSPawel Jakub Dawidek gp->spoiled = g_eli_orphan_spoil_assert; 736c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 737*6f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 738c58794deSPawel Jakub Dawidek error = g_attach(cp, pp); 739c58794deSPawel Jakub Dawidek if (error != 0) 740c58794deSPawel Jakub Dawidek goto end; 741c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 742c58794deSPawel Jakub Dawidek if (error != 0) 743c58794deSPawel Jakub Dawidek goto end; 744c58794deSPawel Jakub Dawidek g_topology_unlock(); 7452f07cdf8SPawel Jakub Dawidek buf = g_read_data(cp, offset, pp->sectorsize, &error); 746c58794deSPawel Jakub Dawidek g_topology_lock(); 7478a4a44b5SMaxim Sobolev if (buf == NULL) 748c58794deSPawel Jakub Dawidek goto end; 749fefb6a14SPawel Jakub Dawidek error = eli_metadata_decode(buf, md); 750fefb6a14SPawel Jakub Dawidek if (error != 0) 751fefb6a14SPawel Jakub Dawidek goto end; 752fefb6a14SPawel Jakub Dawidek /* Metadata was read and decoded successfully. */ 753c58794deSPawel Jakub Dawidek end: 754c58794deSPawel Jakub Dawidek if (buf != NULL) 755c58794deSPawel Jakub Dawidek g_free(buf); 756c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 757c58794deSPawel Jakub Dawidek if (cp->acr == 1) 758c58794deSPawel Jakub Dawidek g_access(cp, -1, 0, 0); 759c58794deSPawel Jakub Dawidek g_detach(cp); 760c58794deSPawel Jakub Dawidek } 761c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 762c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 763c58794deSPawel Jakub Dawidek return (error); 764c58794deSPawel Jakub Dawidek } 765c58794deSPawel Jakub Dawidek 7662f07cdf8SPawel Jakub Dawidek int 7672f07cdf8SPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 7682f07cdf8SPawel Jakub Dawidek struct g_eli_metadata *md) 7692f07cdf8SPawel Jakub Dawidek { 7702f07cdf8SPawel Jakub Dawidek 7712f07cdf8SPawel Jakub Dawidek return (g_eli_read_metadata_offset(mp, pp, 7722f07cdf8SPawel Jakub Dawidek pp->mediasize - pp->sectorsize, md)); 7732f07cdf8SPawel Jakub Dawidek } 7742f07cdf8SPawel Jakub Dawidek 775c58794deSPawel Jakub Dawidek /* 776c58794deSPawel Jakub Dawidek * The function is called when we had last close on provider and user requested 777c58794deSPawel Jakub Dawidek * to close it when this situation occur. 778c58794deSPawel Jakub Dawidek */ 779c58794deSPawel Jakub Dawidek static void 78019351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused) 781c58794deSPawel Jakub Dawidek { 782c58794deSPawel Jakub Dawidek struct g_geom *gp; 78319351a14SAlexander Motin char gpname[64]; 784c58794deSPawel Jakub Dawidek int error; 785c58794deSPawel Jakub Dawidek 786c58794deSPawel Jakub Dawidek g_topology_assert(); 78719351a14SAlexander Motin gp = arg; 78819351a14SAlexander Motin strlcpy(gpname, gp->name, sizeof(gpname)); 78919351a14SAlexander Motin error = g_eli_destroy(gp->softc, TRUE); 790c58794deSPawel Jakub Dawidek KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 79119351a14SAlexander Motin gpname, error)); 79219351a14SAlexander Motin G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 793c58794deSPawel Jakub Dawidek } 794c58794deSPawel Jakub Dawidek 795c58794deSPawel Jakub Dawidek int 796c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de) 797c58794deSPawel Jakub Dawidek { 798c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 799c58794deSPawel Jakub Dawidek struct g_geom *gp; 800c58794deSPawel Jakub Dawidek 801c58794deSPawel Jakub Dawidek gp = pp->geom; 802c58794deSPawel Jakub Dawidek sc = gp->softc; 803c58794deSPawel Jakub Dawidek 804c58794deSPawel Jakub Dawidek if (dw > 0) { 80585059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) { 80685059016SPawel Jakub Dawidek /* Deny write attempts. */ 80785059016SPawel Jakub Dawidek return (EROFS); 80885059016SPawel Jakub Dawidek } 809c58794deSPawel Jakub Dawidek /* Someone is opening us for write, we need to remember that. */ 810c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_WOPEN; 811c58794deSPawel Jakub Dawidek return (0); 812c58794deSPawel Jakub Dawidek } 813c58794deSPawel Jakub Dawidek /* Is this the last close? */ 814c58794deSPawel Jakub Dawidek if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 815c58794deSPawel Jakub Dawidek return (0); 816c58794deSPawel Jakub Dawidek 817c58794deSPawel Jakub Dawidek /* 818c58794deSPawel Jakub Dawidek * Automatically detach on last close if requested. 819c58794deSPawel Jakub Dawidek */ 820c58794deSPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 821c58794deSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 82219351a14SAlexander Motin g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 823c58794deSPawel Jakub Dawidek } 824c58794deSPawel Jakub Dawidek return (0); 825c58794deSPawel Jakub Dawidek } 826c58794deSPawel Jakub Dawidek 827eba8f137SPawel Jakub Dawidek static int 828eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu) 829eba8f137SPawel Jakub Dawidek { 830eba8f137SPawel Jakub Dawidek #ifdef SMP 83171a19bdcSAttilio Rao return (CPU_ISSET(cpu, &hlt_cpus_mask)); 832eba8f137SPawel Jakub Dawidek #else 833eba8f137SPawel Jakub Dawidek return (0); 834eba8f137SPawel Jakub Dawidek #endif 835eba8f137SPawel Jakub Dawidek } 836eba8f137SPawel Jakub Dawidek 837c58794deSPawel Jakub Dawidek struct g_geom * 838c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 839c58794deSPawel Jakub Dawidek const struct g_eli_metadata *md, const u_char *mkey, int nkey) 840c58794deSPawel Jakub Dawidek { 841c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 842c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 843c58794deSPawel Jakub Dawidek struct g_geom *gp; 844c58794deSPawel Jakub Dawidek struct g_provider *pp; 845c58794deSPawel Jakub Dawidek struct g_consumer *cp; 846ae1cce52SWarner Losh struct g_geom_alias *gap; 847c58794deSPawel Jakub Dawidek u_int i, threads; 8483bb6e0f0SRyan Libby int dcw, error; 849c58794deSPawel Jakub Dawidek 850c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 851e2b99193SJohn Baldwin KASSERT(eli_metadata_crypto_supported(md), 852e2b99193SJohn Baldwin ("%s: unsupported crypto for %s", __func__, bpp->name)); 853c58794deSPawel Jakub Dawidek 854c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 855c58794deSPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 856c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 857c58794deSPawel Jakub Dawidek /* 8584273d412SPawel Jakub Dawidek * Spoiling can happen even though we have the provider open 8594273d412SPawel Jakub Dawidek * exclusively, e.g. through media change events. 860c58794deSPawel Jakub Dawidek */ 8614273d412SPawel Jakub Dawidek gp->spoiled = g_eli_orphan; 862c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan; 8632f07cdf8SPawel Jakub Dawidek gp->resize = g_eli_resize; 86485059016SPawel Jakub Dawidek gp->dumpconf = g_eli_dumpconf; 865c58794deSPawel Jakub Dawidek /* 86685059016SPawel Jakub Dawidek * If detach-on-last-close feature is not enabled and we don't operate 86785059016SPawel Jakub Dawidek * on read-only provider, we can simply use g_std_access(). 868c58794deSPawel Jakub Dawidek */ 86985059016SPawel Jakub Dawidek if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 870c58794deSPawel Jakub Dawidek gp->access = g_eli_access; 871c58794deSPawel Jakub Dawidek else 872c58794deSPawel Jakub Dawidek gp->access = g_std_access; 873c58794deSPawel Jakub Dawidek 8744332fecaSAllan Jude eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize); 875c58794deSPawel Jakub Dawidek sc->sc_nkey = nkey; 876eaa3b919SPawel Jakub Dawidek 877c58794deSPawel Jakub Dawidek gp->softc = sc; 878c58794deSPawel Jakub Dawidek sc->sc_geom = gp; 879c58794deSPawel Jakub Dawidek 880c58794deSPawel Jakub Dawidek bioq_init(&sc->sc_queue); 881c58794deSPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 8821e09ff3dSPawel Jakub Dawidek mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 883c58794deSPawel Jakub Dawidek 884c58794deSPawel Jakub Dawidek pp = NULL; 885c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 886*6f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 887c58794deSPawel Jakub Dawidek error = g_attach(cp, bpp); 888c58794deSPawel Jakub Dawidek if (error != 0) { 889c58794deSPawel Jakub Dawidek if (req != NULL) { 890c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot attach to %s (error=%d).", 891c58794deSPawel Jakub Dawidek bpp->name, error); 892c58794deSPawel Jakub Dawidek } else { 893c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 894c58794deSPawel Jakub Dawidek bpp->name, error); 895c58794deSPawel Jakub Dawidek } 896c58794deSPawel Jakub Dawidek goto failed; 897c58794deSPawel Jakub Dawidek } 898c58794deSPawel Jakub Dawidek /* 899c58794deSPawel Jakub Dawidek * Keep provider open all the time, so we can run critical tasks, 900c58794deSPawel Jakub Dawidek * like Master Keys deletion, without wondering if we can open 901c58794deSPawel Jakub Dawidek * provider or not. 90285059016SPawel Jakub Dawidek * We don't open provider for writing only when user requested read-only 90385059016SPawel Jakub Dawidek * access. 904c58794deSPawel Jakub Dawidek */ 9053bb6e0f0SRyan Libby dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1; 9063bb6e0f0SRyan Libby error = g_access(cp, 1, dcw, 1); 907c58794deSPawel Jakub Dawidek if (error != 0) { 908c58794deSPawel Jakub Dawidek if (req != NULL) { 909c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot access %s (error=%d).", 910c58794deSPawel Jakub Dawidek bpp->name, error); 911c58794deSPawel Jakub Dawidek } else { 912c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 913c58794deSPawel Jakub Dawidek bpp->name, error); 914c58794deSPawel Jakub Dawidek } 915c58794deSPawel Jakub Dawidek goto failed; 916c58794deSPawel Jakub Dawidek } 917c58794deSPawel Jakub Dawidek 918c6a26d4cSPawel Jakub Dawidek /* 919c6a26d4cSPawel Jakub Dawidek * Remember the keys in our softc structure. 920c6a26d4cSPawel Jakub Dawidek */ 921c6a26d4cSPawel Jakub Dawidek g_eli_mkey_propagate(sc, mkey); 922c6a26d4cSPawel Jakub Dawidek 923c58794deSPawel Jakub Dawidek LIST_INIT(&sc->sc_workers); 924c58794deSPawel Jakub Dawidek 925c58794deSPawel Jakub Dawidek threads = g_eli_threads; 926dd549194SPawel Jakub Dawidek if (threads == 0) 927dd549194SPawel Jakub Dawidek threads = mp_ncpus; 9280c879bd9SPawel Jakub Dawidek sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 929c58794deSPawel Jakub Dawidek for (i = 0; i < threads; i++) { 930eba8f137SPawel Jakub Dawidek if (g_eli_cpu_is_disabled(i)) { 931eba8f137SPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 9321506db21SPawel Jakub Dawidek bpp->name, i); 933eba8f137SPawel Jakub Dawidek continue; 934eba8f137SPawel Jakub Dawidek } 935c58794deSPawel Jakub Dawidek wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 936c58794deSPawel Jakub Dawidek wr->w_softc = sc; 937c58794deSPawel Jakub Dawidek wr->w_number = i; 9385ad4a7c7SPawel Jakub Dawidek wr->w_active = TRUE; 939c58794deSPawel Jakub Dawidek 9403ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 941c58794deSPawel Jakub Dawidek if (error != 0) { 942c58794deSPawel Jakub Dawidek free(wr, M_ELI); 943c58794deSPawel Jakub Dawidek if (req != NULL) { 944c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot set up crypto session " 945c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 946c58794deSPawel Jakub Dawidek } else { 947c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot set up crypto session " 948c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 949c58794deSPawel Jakub Dawidek } 950c58794deSPawel Jakub Dawidek goto failed; 951c58794deSPawel Jakub Dawidek } 952c58794deSPawel Jakub Dawidek 9533745c395SJulian Elischer error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 954c58794deSPawel Jakub Dawidek "g_eli[%u] %s", i, bpp->name); 955c58794deSPawel Jakub Dawidek if (error != 0) { 9563ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 957c58794deSPawel Jakub Dawidek free(wr, M_ELI); 958c58794deSPawel Jakub Dawidek if (req != NULL) { 959dddd1d53SPawel Jakub Dawidek gctl_error(req, "Cannot create kernel thread " 960dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 961c58794deSPawel Jakub Dawidek } else { 962dddd1d53SPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot create kernel thread " 963dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 964c58794deSPawel Jakub Dawidek } 965c58794deSPawel Jakub Dawidek goto failed; 966c58794deSPawel Jakub Dawidek } 967c58794deSPawel Jakub Dawidek LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 968c58794deSPawel Jakub Dawidek } 969c58794deSPawel Jakub Dawidek 970c58794deSPawel Jakub Dawidek /* 971c58794deSPawel Jakub Dawidek * Create decrypted provider. 972c58794deSPawel Jakub Dawidek */ 973c58794deSPawel Jakub Dawidek pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 974*6f818c1fSAlan Somers pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 975c6a26d4cSPawel Jakub Dawidek pp->mediasize = sc->sc_mediasize; 976c6a26d4cSPawel Jakub Dawidek pp->sectorsize = sc->sc_sectorsize; 977ae1cce52SWarner Losh LIST_FOREACH(gap, &bpp->aliases, ga_next) 978ae1cce52SWarner Losh g_provider_add_alias(pp, "%s%s", gap->ga_alias, G_ELI_SUFFIX); 979eaa3b919SPawel Jakub Dawidek 980c58794deSPawel Jakub Dawidek g_error_provider(pp, 0); 981c58794deSPawel Jakub Dawidek 982c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s created.", pp->name); 983eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 984eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 985e2b99193SJohn Baldwin if (sc->sc_flags & G_ELI_FLAG_AUTH) 986eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 987c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, " Crypto: %s", 988a3d565a1SJohn Baldwin sc->sc_crypto == G_ELI_CRYPTO_SW_ACCEL ? "accelerated software" : 989c58794deSPawel Jakub Dawidek sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 990c58794deSPawel Jakub Dawidek return (gp); 991c58794deSPawel Jakub Dawidek failed: 992c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 993c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 994c58794deSPawel Jakub Dawidek wakeup(sc); 995c58794deSPawel Jakub Dawidek /* 996c58794deSPawel Jakub Dawidek * Wait for kernel threads self destruction. 997c58794deSPawel Jakub Dawidek */ 998c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 999c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1000c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1001c58794deSPawel Jakub Dawidek } 1002c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1003c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 1004c58794deSPawel Jakub Dawidek if (cp->acr == 1) 10053bb6e0f0SRyan Libby g_access(cp, -1, -dcw, -1); 1006c58794deSPawel Jakub Dawidek g_detach(cp); 1007c58794deSPawel Jakub Dawidek } 1008c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 1009c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 10101e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1011b172f23dSJohn Baldwin zfree(sc, M_ELI); 1012c58794deSPawel Jakub Dawidek return (NULL); 1013c58794deSPawel Jakub Dawidek } 1014c58794deSPawel Jakub Dawidek 1015c58794deSPawel Jakub Dawidek int 1016c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 1017c58794deSPawel Jakub Dawidek { 1018c58794deSPawel Jakub Dawidek struct g_geom *gp; 1019c58794deSPawel Jakub Dawidek struct g_provider *pp; 1020c58794deSPawel Jakub Dawidek 1021c58794deSPawel Jakub Dawidek g_topology_assert(); 1022c58794deSPawel Jakub Dawidek 1023c58794deSPawel Jakub Dawidek if (sc == NULL) 1024c58794deSPawel Jakub Dawidek return (ENXIO); 1025c58794deSPawel Jakub Dawidek 1026c58794deSPawel Jakub Dawidek gp = sc->sc_geom; 1027c58794deSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1028c58794deSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 1029c58794deSPawel Jakub Dawidek if (force) { 1030c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Device %s is still open, so it " 103198645006SChristian Brueffer "cannot be definitely removed.", pp->name); 103219351a14SAlexander Motin sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 103319351a14SAlexander Motin gp->access = g_eli_access; 103419351a14SAlexander Motin g_wither_provider(pp, ENXIO); 103519351a14SAlexander Motin return (EBUSY); 1036c58794deSPawel Jakub Dawidek } else { 1037c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, 1038c58794deSPawel Jakub Dawidek "Device %s is still open (r%dw%de%d).", pp->name, 1039c58794deSPawel Jakub Dawidek pp->acr, pp->acw, pp->ace); 1040c58794deSPawel Jakub Dawidek return (EBUSY); 1041c58794deSPawel Jakub Dawidek } 1042c58794deSPawel Jakub Dawidek } 1043c58794deSPawel Jakub Dawidek 1044c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1045c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1046c58794deSPawel Jakub Dawidek wakeup(sc); 1047c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1048c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1049c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1050c58794deSPawel Jakub Dawidek } 1051c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1052c58794deSPawel Jakub Dawidek gp->softc = NULL; 10531e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1054b172f23dSJohn Baldwin zfree(sc, M_ELI); 1055c58794deSPawel Jakub Dawidek 1056c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 1057c58794deSPawel Jakub Dawidek g_wither_geom_close(gp, ENXIO); 1058c58794deSPawel Jakub Dawidek 1059c58794deSPawel Jakub Dawidek return (0); 1060c58794deSPawel Jakub Dawidek } 1061c58794deSPawel Jakub Dawidek 1062c58794deSPawel Jakub Dawidek static int 1063c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused, 1064c58794deSPawel Jakub Dawidek struct g_class *mp __unused, struct g_geom *gp) 1065c58794deSPawel Jakub Dawidek { 1066c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1067c58794deSPawel Jakub Dawidek 1068c58794deSPawel Jakub Dawidek sc = gp->softc; 10695ad4a7c7SPawel Jakub Dawidek return (g_eli_destroy(sc, FALSE)); 1070c58794deSPawel Jakub Dawidek } 1071c58794deSPawel Jakub Dawidek 10729af2131bSPawel Jakub Dawidek static int 10739af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 10749af2131bSPawel Jakub Dawidek { 10751e189c08SMarcel Moolenaar u_char *keyfile, *data; 10769af2131bSPawel Jakub Dawidek char *file, name[64]; 10771e189c08SMarcel Moolenaar size_t size; 10789af2131bSPawel Jakub Dawidek int i; 10799af2131bSPawel Jakub Dawidek 10809af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 10819af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 10829af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1083edaa9008SPawel Jakub Dawidek if (keyfile == NULL && i == 0) { 1084edaa9008SPawel Jakub Dawidek /* 1085edaa9008SPawel Jakub Dawidek * If there is only one keyfile, allow simpler name. 1086edaa9008SPawel Jakub Dawidek */ 1087edaa9008SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 1088edaa9008SPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1089edaa9008SPawel Jakub Dawidek } 10909af2131bSPawel Jakub Dawidek if (keyfile == NULL) 10919af2131bSPawel Jakub Dawidek return (i); /* Return number of loaded keyfiles. */ 10921e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 10939af2131bSPawel Jakub Dawidek if (data == NULL) { 10949af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file data for %s.", 10959af2131bSPawel Jakub Dawidek name); 10969af2131bSPawel Jakub Dawidek return (0); 10979af2131bSPawel Jakub Dawidek } 10981e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 10991e189c08SMarcel Moolenaar if (size == 0) { 11009af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file size for %s.", 11019af2131bSPawel Jakub Dawidek name); 11029af2131bSPawel Jakub Dawidek return (0); 11039af2131bSPawel Jakub Dawidek } 11049af2131bSPawel Jakub Dawidek file = preload_search_info(keyfile, MODINFO_NAME); 11059af2131bSPawel Jakub Dawidek if (file == NULL) { 11069af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file name for %s.", 11079af2131bSPawel Jakub Dawidek name); 11089af2131bSPawel Jakub Dawidek return (0); 11099af2131bSPawel Jakub Dawidek } 11109af2131bSPawel Jakub Dawidek G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 11119af2131bSPawel Jakub Dawidek provider, name); 11121e189c08SMarcel Moolenaar g_eli_crypto_hmac_update(ctx, data, size); 11139af2131bSPawel Jakub Dawidek } 11149af2131bSPawel Jakub Dawidek } 11159af2131bSPawel Jakub Dawidek 11169af2131bSPawel Jakub Dawidek static void 11179af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider) 11189af2131bSPawel Jakub Dawidek { 11191e189c08SMarcel Moolenaar u_char *keyfile, *data; 11209af2131bSPawel Jakub Dawidek char name[64]; 11211e189c08SMarcel Moolenaar size_t size; 11229af2131bSPawel Jakub Dawidek int i; 11239af2131bSPawel Jakub Dawidek 11249af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 11259af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 11269af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 11279af2131bSPawel Jakub Dawidek if (keyfile == NULL) 11289af2131bSPawel Jakub Dawidek return; 11291e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 11301e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 11311e189c08SMarcel Moolenaar if (data != NULL && size != 0) 11326572e5ffSJohn Baldwin explicit_bzero(data, size); 11339af2131bSPawel Jakub Dawidek } 11349af2131bSPawel Jakub Dawidek } 11359af2131bSPawel Jakub Dawidek 1136c58794deSPawel Jakub Dawidek /* 1137c58794deSPawel Jakub Dawidek * Tasting is only made on boot. 1138c58794deSPawel Jakub Dawidek * We detect providers which should be attached before root is mounted. 1139c58794deSPawel Jakub Dawidek */ 1140c58794deSPawel Jakub Dawidek static struct g_geom * 1141c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1142c58794deSPawel Jakub Dawidek { 1143c58794deSPawel Jakub Dawidek struct g_eli_metadata md; 1144c58794deSPawel Jakub Dawidek struct g_geom *gp; 1145c58794deSPawel Jakub Dawidek struct hmac_ctx ctx; 1146c58794deSPawel Jakub Dawidek char passphrase[256]; 1147c58794deSPawel Jakub Dawidek u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 11483453dc72SMariusz Zaborski u_int i, nkey, nkeyfiles, tries, showpass; 1149c58794deSPawel Jakub Dawidek int error; 1150ec5c0e5bSAllan Jude struct keybuf *keybuf; 1151c58794deSPawel Jakub Dawidek 1152c58794deSPawel Jakub Dawidek g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1153c58794deSPawel Jakub Dawidek g_topology_assert(); 1154c58794deSPawel Jakub Dawidek 1155df3aed4fSPawel Jakub Dawidek if (root_mounted() || g_eli_tries == 0) 1156c58794deSPawel Jakub Dawidek return (NULL); 1157c58794deSPawel Jakub Dawidek 1158c58794deSPawel Jakub Dawidek G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1159c58794deSPawel Jakub Dawidek 1160c58794deSPawel Jakub Dawidek error = g_eli_read_metadata(mp, pp, &md); 1161c58794deSPawel Jakub Dawidek if (error != 0) 1162c58794deSPawel Jakub Dawidek return (NULL); 1163c58794deSPawel Jakub Dawidek gp = NULL; 1164c58794deSPawel Jakub Dawidek 1165c58794deSPawel Jakub Dawidek if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1166c58794deSPawel Jakub Dawidek return (NULL); 1167c58794deSPawel Jakub Dawidek if (md.md_version > G_ELI_VERSION) { 1168c58794deSPawel Jakub Dawidek printf("geom_eli.ko module is too old to handle %s.\n", 1169c58794deSPawel Jakub Dawidek pp->name); 1170c58794deSPawel Jakub Dawidek return (NULL); 1171c58794deSPawel Jakub Dawidek } 1172c58794deSPawel Jakub Dawidek if (md.md_provsize != pp->mediasize) 1173c58794deSPawel Jakub Dawidek return (NULL); 1174c58794deSPawel Jakub Dawidek /* Should we attach it on boot? */ 1175c81929d3SKyle Evans if (!(md.md_flags & G_ELI_FLAG_BOOT) && 1176c81929d3SKyle Evans !(md.md_flags & G_ELI_FLAG_GELIBOOT)) 1177c58794deSPawel Jakub Dawidek return (NULL); 1178c58794deSPawel Jakub Dawidek if (md.md_keys == 0x00) { 1179c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1180c58794deSPawel Jakub Dawidek return (NULL); 1181c58794deSPawel Jakub Dawidek } 1182e2b99193SJohn Baldwin if (!eli_metadata_crypto_supported(&md)) { 1183e2b99193SJohn Baldwin G_ELI_DEBUG(0, "%s uses invalid or unsupported algorithms\n", 1184e2b99193SJohn Baldwin pp->name); 1185e2b99193SJohn Baldwin return (NULL); 1186e2b99193SJohn Baldwin } 11879af2131bSPawel Jakub Dawidek if (md.md_iterations == -1) { 11889af2131bSPawel Jakub Dawidek /* If there is no passphrase, we try only once. */ 11899af2131bSPawel Jakub Dawidek tries = 1; 11909af2131bSPawel Jakub Dawidek } else { 11919af2131bSPawel Jakub Dawidek /* Ask for the passphrase no more than g_eli_tries times. */ 11929af2131bSPawel Jakub Dawidek tries = g_eli_tries; 11939af2131bSPawel Jakub Dawidek } 11949af2131bSPawel Jakub Dawidek 1195ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 1196ec5c0e5bSAllan Jude /* Scan the key buffer, try all GELI keys. */ 1197ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 1198ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 1199ec5c0e5bSAllan Jude memcpy(key, keybuf->kb_ents[i].ke_data, 1200ec5c0e5bSAllan Jude sizeof(key)); 1201ec5c0e5bSAllan Jude 120231f7586dSMariusz Zaborski if (g_eli_mkey_decrypt_any(&md, key, 1203ec5c0e5bSAllan Jude mkey, &nkey) == 0 ) { 1204ec5c0e5bSAllan Jude explicit_bzero(key, sizeof(key)); 1205ec5c0e5bSAllan Jude goto have_key; 1206ec5c0e5bSAllan Jude } 1207ec5c0e5bSAllan Jude } 1208ec5c0e5bSAllan Jude } 1209ec5c0e5bSAllan Jude } 1210ec5c0e5bSAllan Jude 1211835c4dd4SColin Percival for (i = 0; i <= tries; i++) { 12129af2131bSPawel Jakub Dawidek g_eli_crypto_hmac_init(&ctx, NULL, 0); 1213c58794deSPawel Jakub Dawidek 1214c58794deSPawel Jakub Dawidek /* 12159af2131bSPawel Jakub Dawidek * Load all key files. 1216c58794deSPawel Jakub Dawidek */ 12179af2131bSPawel Jakub Dawidek nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 12189af2131bSPawel Jakub Dawidek 12199af2131bSPawel Jakub Dawidek if (nkeyfiles == 0 && md.md_iterations == -1) { 12209af2131bSPawel Jakub Dawidek /* 12219af2131bSPawel Jakub Dawidek * No key files and no passphrase, something is 12229af2131bSPawel Jakub Dawidek * definitely wrong here. 12239af2131bSPawel Jakub Dawidek * geli(8) doesn't allow for such situation, so assume 12249af2131bSPawel Jakub Dawidek * that there was really no passphrase and in that case 12259af2131bSPawel Jakub Dawidek * key files are no properly defined in loader.conf. 12269af2131bSPawel Jakub Dawidek */ 12279af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 12289af2131bSPawel Jakub Dawidek "Found no key files in loader.conf for %s.", 12299af2131bSPawel Jakub Dawidek pp->name); 12309af2131bSPawel Jakub Dawidek return (NULL); 12319af2131bSPawel Jakub Dawidek } 12329af2131bSPawel Jakub Dawidek 12339af2131bSPawel Jakub Dawidek /* Ask for the passphrase if defined. */ 12349af2131bSPawel Jakub Dawidek if (md.md_iterations >= 0) { 1235835c4dd4SColin Percival /* Try first with cached passphrase. */ 1236835c4dd4SColin Percival if (i == 0) { 1237835c4dd4SColin Percival if (!g_eli_boot_passcache) 1238835c4dd4SColin Percival continue; 1239835c4dd4SColin Percival memcpy(passphrase, cached_passphrase, 1240835c4dd4SColin Percival sizeof(passphrase)); 1241835c4dd4SColin Percival } else { 1242c58794deSPawel Jakub Dawidek printf("Enter passphrase for %s: ", pp->name); 12433453dc72SMariusz Zaborski showpass = g_eli_visible_passphrase; 12443453dc72SMariusz Zaborski if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0) 12453453dc72SMariusz Zaborski showpass = GETS_ECHOPASS; 1246f6ce353eSAndriy Gapon cngets(passphrase, sizeof(passphrase), 12473453dc72SMariusz Zaborski showpass); 1248835c4dd4SColin Percival memcpy(cached_passphrase, passphrase, 1249835c4dd4SColin Percival sizeof(passphrase)); 1250835c4dd4SColin Percival } 12519af2131bSPawel Jakub Dawidek } 12529af2131bSPawel Jakub Dawidek 1253c58794deSPawel Jakub Dawidek /* 1254c58794deSPawel Jakub Dawidek * Prepare Derived-Key from the user passphrase. 1255c58794deSPawel Jakub Dawidek */ 1256c58794deSPawel Jakub Dawidek if (md.md_iterations == 0) { 1257c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, md.md_salt, 1258c58794deSPawel Jakub Dawidek sizeof(md.md_salt)); 1259c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, passphrase, 1260c58794deSPawel Jakub Dawidek strlen(passphrase)); 1261ec5c0e5bSAllan Jude explicit_bzero(passphrase, sizeof(passphrase)); 12629af2131bSPawel Jakub Dawidek } else if (md.md_iterations > 0) { 1263c58794deSPawel Jakub Dawidek u_char dkey[G_ELI_USERKEYLEN]; 1264c58794deSPawel Jakub Dawidek 1265c58794deSPawel Jakub Dawidek pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1266c58794deSPawel Jakub Dawidek sizeof(md.md_salt), passphrase, md.md_iterations); 12676572e5ffSJohn Baldwin explicit_bzero(passphrase, sizeof(passphrase)); 1268c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1269ec5c0e5bSAllan Jude explicit_bzero(dkey, sizeof(dkey)); 1270c58794deSPawel Jakub Dawidek } 12719af2131bSPawel Jakub Dawidek 1272c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_final(&ctx, key, 0); 1273c58794deSPawel Jakub Dawidek 1274c58794deSPawel Jakub Dawidek /* 1275c58794deSPawel Jakub Dawidek * Decrypt Master-Key. 1276c58794deSPawel Jakub Dawidek */ 127731f7586dSMariusz Zaborski error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey); 12786572e5ffSJohn Baldwin explicit_bzero(key, sizeof(key)); 1279c58794deSPawel Jakub Dawidek if (error == -1) { 1280835c4dd4SColin Percival if (i == tries) { 12819af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 12829af2131bSPawel Jakub Dawidek "Wrong key for %s. No tries left.", 12839af2131bSPawel Jakub Dawidek pp->name); 12849af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 12859af2131bSPawel Jakub Dawidek return (NULL); 1286c58794deSPawel Jakub Dawidek } 1287835c4dd4SColin Percival if (i > 0) { 1288835c4dd4SColin Percival G_ELI_DEBUG(0, 1289835c4dd4SColin Percival "Wrong key for %s. Tries left: %u.", 1290835c4dd4SColin Percival pp->name, tries - i); 1291835c4dd4SColin Percival } 1292c58794deSPawel Jakub Dawidek /* Try again. */ 1293c58794deSPawel Jakub Dawidek continue; 1294c58794deSPawel Jakub Dawidek } else if (error > 0) { 1295038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, 1296038c55adSPawel Jakub Dawidek "Cannot decrypt Master Key for %s (error=%d).", 1297c58794deSPawel Jakub Dawidek pp->name, error); 12989af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 1299c58794deSPawel Jakub Dawidek return (NULL); 1300c58794deSPawel Jakub Dawidek } 1301ebd05adaSBrad Davis g_eli_keyfiles_clear(pp->name); 1302c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1303c58794deSPawel Jakub Dawidek break; 1304c58794deSPawel Jakub Dawidek } 1305ec5c0e5bSAllan Jude have_key: 1306c58794deSPawel Jakub Dawidek 1307c58794deSPawel Jakub Dawidek /* 1308c58794deSPawel Jakub Dawidek * We have correct key, let's attach provider. 1309c58794deSPawel Jakub Dawidek */ 1310c58794deSPawel Jakub Dawidek gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 13116572e5ffSJohn Baldwin explicit_bzero(mkey, sizeof(mkey)); 13126572e5ffSJohn Baldwin explicit_bzero(&md, sizeof(md)); 1313c58794deSPawel Jakub Dawidek if (gp == NULL) { 1314c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1315c58794deSPawel Jakub Dawidek G_ELI_SUFFIX); 1316c58794deSPawel Jakub Dawidek return (NULL); 1317c58794deSPawel Jakub Dawidek } 1318c58794deSPawel Jakub Dawidek return (gp); 1319c58794deSPawel Jakub Dawidek } 1320c58794deSPawel Jakub Dawidek 1321c58794deSPawel Jakub Dawidek static void 1322c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1323c58794deSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 1324c58794deSPawel Jakub Dawidek { 1325c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1326c58794deSPawel Jakub Dawidek 1327c58794deSPawel Jakub Dawidek g_topology_assert(); 1328c58794deSPawel Jakub Dawidek sc = gp->softc; 1329c58794deSPawel Jakub Dawidek if (sc == NULL) 1330c58794deSPawel Jakub Dawidek return; 1331c58794deSPawel Jakub Dawidek if (pp != NULL || cp != NULL) 1332c58794deSPawel Jakub Dawidek return; /* Nothing here. */ 13331e09ff3dSPawel Jakub Dawidek 1334743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 13351e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_total); 1336743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 13371e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_allocated); 1338ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<Flags>", indent); 1339ea35a2ecSPawel Jakub Dawidek if (sc->sc_flags == 0) 134049ee0fceSAlexander Motin sbuf_cat(sb, "NONE"); 1341ea35a2ecSPawel Jakub Dawidek else { 1342ea35a2ecSPawel Jakub Dawidek int first = 1; 1343ea35a2ecSPawel Jakub Dawidek 1344ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name) do { \ 1345eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & (flag)) { \ 1346ea35a2ecSPawel Jakub Dawidek if (!first) \ 134749ee0fceSAlexander Motin sbuf_cat(sb, ", "); \ 1348ea35a2ecSPawel Jakub Dawidek else \ 1349ea35a2ecSPawel Jakub Dawidek first = 0; \ 135049ee0fceSAlexander Motin sbuf_cat(sb, name); \ 1351ea35a2ecSPawel Jakub Dawidek } \ 1352ea35a2ecSPawel Jakub Dawidek } while (0) 13535ad4a7c7SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1354c6a26d4cSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 13552bd4ade6SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1356ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1357ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1358ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1359ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1360eaa3b919SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1361ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1362ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 136385059016SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 136446e34470SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1365d8736625SAllan Jude ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT"); 13663453dc72SMariusz Zaborski ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS"); 13672f07cdf8SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE"); 1368ea35a2ecSPawel Jakub Dawidek #undef ADD_FLAG 1369ea35a2ecSPawel Jakub Dawidek } 137049ee0fceSAlexander Motin sbuf_cat(sb, "</Flags>\n"); 1371ea35a2ecSPawel Jakub Dawidek 1372eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1373ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1374ea35a2ecSPawel Jakub Dawidek sc->sc_nkey); 1375ea35a2ecSPawel Jakub Dawidek } 13761f8c92e6SPawel Jakub Dawidek sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1377c58794deSPawel Jakub Dawidek sbuf_printf(sb, "%s<Crypto>", indent); 1378c58794deSPawel Jakub Dawidek switch (sc->sc_crypto) { 1379c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 138049ee0fceSAlexander Motin sbuf_cat(sb, "hardware"); 1381c58794deSPawel Jakub Dawidek break; 1382c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 138349ee0fceSAlexander Motin sbuf_cat(sb, "software"); 1384c58794deSPawel Jakub Dawidek break; 1385a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 1386a3d565a1SJohn Baldwin sbuf_cat(sb, "accelerated software"); 1387a3d565a1SJohn Baldwin break; 1388c58794deSPawel Jakub Dawidek default: 138949ee0fceSAlexander Motin sbuf_cat(sb, "UNKNOWN"); 1390c58794deSPawel Jakub Dawidek break; 1391c58794deSPawel Jakub Dawidek } 139249ee0fceSAlexander Motin sbuf_cat(sb, "</Crypto>\n"); 1393eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1394eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, 1395eaa3b919SPawel Jakub Dawidek "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1396eaa3b919SPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_aalgo)); 1397eaa3b919SPawel Jakub Dawidek } 1398eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1399eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1400038c55adSPawel Jakub Dawidek sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1401038c55adSPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_ealgo)); 1402d8d61ef8SPawel Jakub Dawidek sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1403d8d61ef8SPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1404c58794deSPawel Jakub Dawidek } 1405c58794deSPawel Jakub Dawidek 1406c5d387d0SPawel Jakub Dawidek static void 1407c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto) 1408c5d387d0SPawel Jakub Dawidek { 1409c5d387d0SPawel Jakub Dawidek struct g_class *mp; 1410c5d387d0SPawel Jakub Dawidek struct g_geom *gp, *gp2; 1411c5d387d0SPawel Jakub Dawidek struct g_provider *pp; 1412c5d387d0SPawel Jakub Dawidek struct g_eli_softc *sc; 1413c5d387d0SPawel Jakub Dawidek int error; 1414c5d387d0SPawel Jakub Dawidek 1415c5d387d0SPawel Jakub Dawidek mp = arg; 1416c5d387d0SPawel Jakub Dawidek g_topology_lock(); 1417c5d387d0SPawel Jakub Dawidek LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1418c5d387d0SPawel Jakub Dawidek sc = gp->softc; 1419c5d387d0SPawel Jakub Dawidek if (sc == NULL) 1420c5d387d0SPawel Jakub Dawidek continue; 1421c5d387d0SPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1422c5d387d0SPawel Jakub Dawidek KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 14232a230609SAlan Somers if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 || 14242a230609SAlan Somers SCHEDULER_STOPPED()) 14252a230609SAlan Somers { 1426c5d387d0SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1427c5d387d0SPawel Jakub Dawidek gp->access = g_eli_access; 14282a230609SAlan Somers } else { 14292a230609SAlan Somers error = g_eli_destroy(sc, TRUE); 1430c5d387d0SPawel Jakub Dawidek } 1431c5d387d0SPawel Jakub Dawidek } 1432c5d387d0SPawel Jakub Dawidek g_topology_unlock(); 1433c5d387d0SPawel Jakub Dawidek } 1434c5d387d0SPawel Jakub Dawidek 1435c5d387d0SPawel Jakub Dawidek static void 1436c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp) 1437c5d387d0SPawel Jakub Dawidek { 1438c5d387d0SPawel Jakub Dawidek 1439c5d387d0SPawel Jakub Dawidek g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1440c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1441c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync == NULL) 1442c5d387d0SPawel Jakub Dawidek G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1443c5d387d0SPawel Jakub Dawidek } 1444c5d387d0SPawel Jakub Dawidek 1445c5d387d0SPawel Jakub Dawidek static void 1446c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp) 1447c5d387d0SPawel Jakub Dawidek { 1448c5d387d0SPawel Jakub Dawidek 1449c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync != NULL) 1450c5d387d0SPawel Jakub Dawidek EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1451c5d387d0SPawel Jakub Dawidek } 1452c5d387d0SPawel Jakub Dawidek 1453c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1454f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 145574d6c131SKyle Evans MODULE_VERSION(geom_eli, 0); 1456