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> 35*bc683a89SWarner Losh #include <sys/kenv.h> 36c58794deSPawel Jakub Dawidek #include <sys/kernel.h> 379af2131bSPawel Jakub Dawidek #include <sys/linker.h> 38c58794deSPawel Jakub Dawidek #include <sys/module.h> 39c58794deSPawel Jakub Dawidek #include <sys/lock.h> 40c58794deSPawel Jakub Dawidek #include <sys/mutex.h> 41c58794deSPawel Jakub Dawidek #include <sys/bio.h> 425d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 43c58794deSPawel Jakub Dawidek #include <sys/sysctl.h> 44c58794deSPawel Jakub Dawidek #include <sys/malloc.h> 45c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h> 46c58794deSPawel Jakub Dawidek #include <sys/kthread.h> 47c58794deSPawel Jakub Dawidek #include <sys/proc.h> 48c58794deSPawel Jakub Dawidek #include <sys/sched.h> 49dd549194SPawel Jakub Dawidek #include <sys/smp.h> 50c58794deSPawel Jakub Dawidek #include <sys/uio.h> 51a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h> 52c58794deSPawel Jakub Dawidek 537d874f0fSAlan Somers #include <machine/vmparam.h> 547d874f0fSAlan Somers 55c58794deSPawel Jakub Dawidek #include <vm/uma.h> 56c58794deSPawel Jakub Dawidek 57c58794deSPawel Jakub Dawidek #include <geom/geom.h> 58ac03832eSConrad Meyer #include <geom/geom_dbg.h> 59c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h> 60c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 61c58794deSPawel Jakub Dawidek 62ec5c0e5bSAllan Jude #include <crypto/intake.h> 63ec5c0e5bSAllan Jude 64cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module"); 65c58794deSPawel Jakub Dawidek 66c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 67c58794deSPawel Jakub Dawidek 68c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 6953a6215cSPawel Biernacki SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 7053a6215cSPawel Biernacki "GEOM_ELI stuff"); 71a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION; 72a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0, 73a1f4a8c4SPawel Jakub Dawidek "GELI version"); 74f95168e0SPawel Jakub Dawidek int g_eli_debug = 0; 75af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0, 76c58794deSPawel Jakub Dawidek "Debug level"); 77c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3; 78af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0, 7998645006SChristian Brueffer "Number of tries for entering the passphrase"); 80eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO; 81af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN, 82c58794deSPawel Jakub Dawidek &g_eli_visible_passphrase, 0, 83eb4c31fdSEd Schouten "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)"); 84b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES; 85af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites, 8698645006SChristian Brueffer 0, "Number of times on-disk keys should be overwritten when destroying them"); 87dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0; 88af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0, 89c58794deSPawel Jakub Dawidek "Number of threads doing crypto work"); 90eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0; 91af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0, 92eaa3b919SPawel Jakub Dawidek "Use crypto operations batching"); 93c58794deSPawel Jakub Dawidek 94835c4dd4SColin Percival /* 95835c4dd4SColin Percival * Passphrase cached during boot, in order to be more user-friendly if 96835c4dd4SColin Percival * there are multiple providers using the same passphrase. 97835c4dd4SColin Percival */ 98835c4dd4SColin Percival static char cached_passphrase[256]; 99835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1; 100835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache); 101835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD, 102835c4dd4SColin Percival &g_eli_boot_passcache, 0, 103835c4dd4SColin Percival "Passphrases are cached during boot process for possible reuse"); 104835c4dd4SColin Percival static void 10566427784SColin Percival fetch_loader_passphrase(void * dummy) 10666427784SColin Percival { 10766427784SColin Percival char * env_passphrase; 10866427784SColin Percival 10966427784SColin Percival KASSERT(dynamic_kenv, ("need dynamic kenv")); 11066427784SColin Percival 11166427784SColin Percival if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) { 11266427784SColin Percival /* Extract passphrase from the environment. */ 11366427784SColin Percival strlcpy(cached_passphrase, env_passphrase, 11466427784SColin Percival sizeof(cached_passphrase)); 11566427784SColin Percival freeenv(env_passphrase); 11666427784SColin Percival 11766427784SColin Percival /* Wipe the passphrase from the environment. */ 11866427784SColin Percival kern_unsetenv("kern.geom.eli.passphrase"); 11966427784SColin Percival } 12066427784SColin Percival } 12166427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY, 12266427784SColin Percival fetch_loader_passphrase, NULL); 123ec5c0e5bSAllan Jude 12466427784SColin Percival static void 125ec5c0e5bSAllan Jude zero_boot_passcache(void) 126835c4dd4SColin Percival { 127835c4dd4SColin Percival 128ec5c0e5bSAllan Jude explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 129835c4dd4SColin Percival } 130ec5c0e5bSAllan Jude 131ec5c0e5bSAllan Jude static void 132ec5c0e5bSAllan Jude zero_geli_intake_keys(void) 133ec5c0e5bSAllan Jude { 134ec5c0e5bSAllan Jude struct keybuf *keybuf; 135ec5c0e5bSAllan Jude int i; 136ec5c0e5bSAllan Jude 137ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 138ec5c0e5bSAllan Jude /* Scan the key buffer, clear all GELI keys. */ 139ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 140ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 141ec5c0e5bSAllan Jude explicit_bzero(keybuf->kb_ents[i].ke_data, 142ec5c0e5bSAllan Jude sizeof(keybuf->kb_ents[i].ke_data)); 143ec5c0e5bSAllan Jude keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE; 144ec5c0e5bSAllan Jude } 145ec5c0e5bSAllan Jude } 146ec5c0e5bSAllan Jude } 147ec5c0e5bSAllan Jude } 148ec5c0e5bSAllan Jude 149ec5c0e5bSAllan Jude static void 150ec5c0e5bSAllan Jude zero_intake_passcache(void *dummy) 151ec5c0e5bSAllan Jude { 152ec5c0e5bSAllan Jude zero_boot_passcache(); 153ec5c0e5bSAllan Jude zero_geli_intake_keys(); 154ec5c0e5bSAllan Jude } 155ec5c0e5bSAllan Jude EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0); 156835c4dd4SColin Percival 157c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL; 158c5d387d0SPawel Jakub Dawidek 1592f07cdf8SPawel Jakub Dawidek static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 1602f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md); 1612f07cdf8SPawel Jakub Dawidek 162c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 163c58794deSPawel Jakub Dawidek struct g_geom *gp); 164c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp); 165c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp); 166c58794deSPawel Jakub Dawidek 167c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste; 168c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf; 169c58794deSPawel Jakub Dawidek 170c58794deSPawel Jakub Dawidek struct g_class g_eli_class = { 171c58794deSPawel Jakub Dawidek .name = G_ELI_CLASS_NAME, 172c58794deSPawel Jakub Dawidek .version = G_VERSION, 173c58794deSPawel Jakub Dawidek .ctlreq = g_eli_config, 174c58794deSPawel Jakub Dawidek .taste = g_eli_taste, 175c5d387d0SPawel Jakub Dawidek .destroy_geom = g_eli_destroy_geom, 176c5d387d0SPawel Jakub Dawidek .init = g_eli_init, 177c5d387d0SPawel Jakub Dawidek .fini = g_eli_fini 178c58794deSPawel Jakub Dawidek }; 179c58794deSPawel Jakub Dawidek 180c58794deSPawel Jakub Dawidek /* 181c58794deSPawel Jakub Dawidek * Code paths: 182c58794deSPawel Jakub Dawidek * BIO_READ: 1835ad4a7c7SPawel 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 184c58794deSPawel Jakub Dawidek * BIO_WRITE: 185c58794deSPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 186c58794deSPawel Jakub Dawidek */ 187c58794deSPawel Jakub Dawidek 188c58794deSPawel Jakub Dawidek /* 189c58794deSPawel Jakub Dawidek * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 190c58794deSPawel Jakub Dawidek * accelerator or something like this. 191c58794deSPawel Jakub Dawidek * The function updates the SID and rerun the operation. 192c58794deSPawel Jakub Dawidek */ 193eaa3b919SPawel Jakub Dawidek int 194c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp) 195c58794deSPawel Jakub Dawidek { 196c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 197c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 198c58794deSPawel Jakub Dawidek struct bio *bp; 199c58794deSPawel Jakub Dawidek int error; 200c58794deSPawel Jakub Dawidek 201c58794deSPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 202c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 203c58794deSPawel Jakub Dawidek LIST_FOREACH(wr, &sc->sc_workers, w_next) { 204c58794deSPawel Jakub Dawidek if (wr->w_number == bp->bio_pflags) 205c58794deSPawel Jakub Dawidek break; 206c58794deSPawel Jakub Dawidek } 207c58794deSPawel Jakub Dawidek KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 2081b0909d5SConrad Meyer G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).", 2091b0909d5SConrad Meyer bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid, 2101b0909d5SConrad Meyer crp->crp_session); 2111b0909d5SConrad Meyer wr->w_sid = crp->crp_session; 212c58794deSPawel Jakub Dawidek crp->crp_etype = 0; 213c58794deSPawel Jakub Dawidek error = crypto_dispatch(crp); 214c58794deSPawel Jakub Dawidek if (error == 0) 215c58794deSPawel Jakub Dawidek return (0); 216c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 217c58794deSPawel Jakub Dawidek crp->crp_etype = error; 218c58794deSPawel Jakub Dawidek return (error); 219c58794deSPawel Jakub Dawidek } 220c58794deSPawel Jakub Dawidek 2210bab7fa8SAlan Somers static void 2220bab7fa8SAlan Somers g_eli_getattr_done(struct bio *bp) 2230bab7fa8SAlan Somers { 2240bab7fa8SAlan Somers if (bp->bio_error == 0 && 2250bab7fa8SAlan Somers !strcmp(bp->bio_attribute, "GEOM::physpath")) { 2260bab7fa8SAlan Somers strlcat(bp->bio_data, "/eli", bp->bio_length); 2270bab7fa8SAlan Somers } 2280bab7fa8SAlan Somers g_std_done(bp); 2290bab7fa8SAlan Somers } 2300bab7fa8SAlan Somers 231c58794deSPawel Jakub Dawidek /* 232c58794deSPawel Jakub Dawidek * The function is called afer reading encrypted data from the provider. 233bb30fea6SPawel Jakub Dawidek * 2345ad4a7c7SPawel 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 235c58794deSPawel Jakub Dawidek */ 236eaa3b919SPawel Jakub Dawidek void 237c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp) 238c58794deSPawel Jakub Dawidek { 239c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 240c58794deSPawel Jakub Dawidek struct bio *pbp; 241c58794deSPawel Jakub Dawidek 242c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 243c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2442dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 245c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 24690574b0aSMikolaj Golub g_destroy_bio(bp); 247eaa3b919SPawel Jakub Dawidek /* 248eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 249eaa3b919SPawel Jakub Dawidek */ 250eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 251eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 252eaa3b919SPawel Jakub Dawidek return; 2535ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 254c58794deSPawel Jakub Dawidek if (pbp->bio_error != 0) { 2552dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 2562dc7e36bSSteven Hartland pbp->bio_error); 257c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 258eaa3b919SPawel Jakub Dawidek if (pbp->bio_driver2 != NULL) { 259eaa3b919SPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 260eaa3b919SPawel Jakub Dawidek pbp->bio_driver2 = NULL; 261eaa3b919SPawel Jakub Dawidek } 262c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 26378f79a9aSMariusz Zaborski if (sc != NULL) 2645ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 265c58794deSPawel Jakub Dawidek return; 266c58794deSPawel Jakub Dawidek } 267c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 268c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, pbp); 269c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 270c58794deSPawel Jakub Dawidek wakeup(sc); 271c58794deSPawel Jakub Dawidek } 272c58794deSPawel Jakub Dawidek 273c58794deSPawel Jakub Dawidek /* 274c58794deSPawel Jakub Dawidek * The function is called after we encrypt and write data. 275bb30fea6SPawel Jakub Dawidek * 276bb30fea6SPawel Jakub Dawidek * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 277c58794deSPawel Jakub Dawidek */ 278eaa3b919SPawel Jakub Dawidek void 279c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp) 280c58794deSPawel Jakub Dawidek { 2815ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 282c58794deSPawel Jakub Dawidek struct bio *pbp; 283c58794deSPawel Jakub Dawidek 284c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request done."); 285c58794deSPawel Jakub Dawidek pbp = bp->bio_parent; 2862dc7e36bSSteven Hartland if (pbp->bio_error == 0 && bp->bio_error != 0) 287c58794deSPawel Jakub Dawidek pbp->bio_error = bp->bio_error; 28890574b0aSMikolaj Golub g_destroy_bio(bp); 289eaa3b919SPawel Jakub Dawidek /* 290eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 291eaa3b919SPawel Jakub Dawidek */ 292eaa3b919SPawel Jakub Dawidek pbp->bio_inbed++; 293eaa3b919SPawel Jakub Dawidek if (pbp->bio_inbed < pbp->bio_children) 294eaa3b919SPawel Jakub Dawidek return; 295c58794deSPawel Jakub Dawidek free(pbp->bio_driver2, M_ELI); 296c58794deSPawel Jakub Dawidek pbp->bio_driver2 = NULL; 297eaa3b919SPawel Jakub Dawidek if (pbp->bio_error != 0) { 2982dc7e36bSSteven Hartland G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__, 299c58794deSPawel Jakub Dawidek pbp->bio_error); 300c58794deSPawel Jakub Dawidek pbp->bio_completed = 0; 3012dc7e36bSSteven Hartland } else 3022dc7e36bSSteven Hartland pbp->bio_completed = pbp->bio_length; 3032dc7e36bSSteven Hartland 304c58794deSPawel Jakub Dawidek /* 305c58794deSPawel Jakub Dawidek * Write is finished, send it up. 306c58794deSPawel Jakub Dawidek */ 3075ad4a7c7SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 308c58794deSPawel Jakub Dawidek g_io_deliver(pbp, pbp->bio_error); 30978f79a9aSMariusz Zaborski if (sc != NULL) 3105ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 311c58794deSPawel Jakub Dawidek } 312c58794deSPawel Jakub Dawidek 313c58794deSPawel Jakub Dawidek /* 314c58794deSPawel Jakub Dawidek * This function should never be called, but GEOM made as it set ->orphan() 315c58794deSPawel Jakub Dawidek * method for every geom. 316c58794deSPawel Jakub Dawidek */ 317c58794deSPawel Jakub Dawidek static void 318c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp) 319c58794deSPawel Jakub Dawidek { 320c58794deSPawel Jakub Dawidek 321c58794deSPawel Jakub Dawidek panic("Function %s() called for %s.", __func__, cp->geom->name); 322c58794deSPawel Jakub Dawidek } 323c58794deSPawel Jakub Dawidek 324c58794deSPawel Jakub Dawidek static void 325c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp) 326c58794deSPawel Jakub Dawidek { 327c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 328c58794deSPawel Jakub Dawidek 329c58794deSPawel Jakub Dawidek g_topology_assert(); 330c58794deSPawel Jakub Dawidek sc = cp->geom->softc; 331c58794deSPawel Jakub Dawidek if (sc == NULL) 332c58794deSPawel Jakub Dawidek return; 3335ad4a7c7SPawel Jakub Dawidek g_eli_destroy(sc, TRUE); 334c58794deSPawel Jakub Dawidek } 335c58794deSPawel Jakub Dawidek 3362f07cdf8SPawel Jakub Dawidek static void 3372f07cdf8SPawel Jakub Dawidek g_eli_resize(struct g_consumer *cp) 3382f07cdf8SPawel Jakub Dawidek { 3392f07cdf8SPawel Jakub Dawidek struct g_eli_softc *sc; 3402f07cdf8SPawel Jakub Dawidek struct g_provider *epp, *pp; 3412f07cdf8SPawel Jakub Dawidek off_t oldsize; 3422f07cdf8SPawel Jakub Dawidek 3432f07cdf8SPawel Jakub Dawidek g_topology_assert(); 3442f07cdf8SPawel Jakub Dawidek sc = cp->geom->softc; 3452f07cdf8SPawel Jakub Dawidek if (sc == NULL) 3462f07cdf8SPawel Jakub Dawidek return; 3472f07cdf8SPawel Jakub Dawidek 3482f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) { 3492f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.", 3502f07cdf8SPawel Jakub Dawidek (intmax_t)sc->sc_provsize); 3512f07cdf8SPawel Jakub Dawidek return; 3522f07cdf8SPawel Jakub Dawidek } 3532f07cdf8SPawel Jakub Dawidek 3542f07cdf8SPawel Jakub Dawidek pp = cp->provider; 3552f07cdf8SPawel Jakub Dawidek 3562f07cdf8SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 3572f07cdf8SPawel Jakub Dawidek struct g_eli_metadata md; 3582f07cdf8SPawel Jakub Dawidek u_char *sector; 3592f07cdf8SPawel Jakub Dawidek int error; 3602f07cdf8SPawel Jakub Dawidek 3612f07cdf8SPawel Jakub Dawidek sector = NULL; 3622f07cdf8SPawel Jakub Dawidek 3632f07cdf8SPawel Jakub Dawidek error = g_eli_read_metadata_offset(cp->geom->class, pp, 3642f07cdf8SPawel Jakub Dawidek sc->sc_provsize - pp->sectorsize, &md); 3652f07cdf8SPawel Jakub Dawidek if (error != 0) { 3662f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).", 3672f07cdf8SPawel Jakub Dawidek pp->name, error); 3682f07cdf8SPawel Jakub Dawidek goto iofail; 3692f07cdf8SPawel Jakub Dawidek } 3702f07cdf8SPawel Jakub Dawidek 3712f07cdf8SPawel Jakub Dawidek md.md_provsize = pp->mediasize; 3722f07cdf8SPawel Jakub Dawidek 3732f07cdf8SPawel Jakub Dawidek sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 3742f07cdf8SPawel Jakub Dawidek eli_metadata_encode(&md, sector); 3752f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 3762f07cdf8SPawel Jakub Dawidek pp->sectorsize); 3772f07cdf8SPawel Jakub Dawidek if (error != 0) { 3782f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).", 3792f07cdf8SPawel Jakub Dawidek pp->name, error); 3802f07cdf8SPawel Jakub Dawidek goto iofail; 3812f07cdf8SPawel Jakub Dawidek } 3822f07cdf8SPawel Jakub Dawidek explicit_bzero(sector, pp->sectorsize); 3832f07cdf8SPawel Jakub Dawidek error = g_write_data(cp, sc->sc_provsize - pp->sectorsize, 3842f07cdf8SPawel Jakub Dawidek sector, pp->sectorsize); 3852f07cdf8SPawel Jakub Dawidek if (error != 0) { 3862f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).", 3872f07cdf8SPawel Jakub Dawidek pp->name, error); 3882f07cdf8SPawel Jakub Dawidek goto iofail; 3892f07cdf8SPawel Jakub Dawidek } 3902f07cdf8SPawel Jakub Dawidek iofail: 3912f07cdf8SPawel Jakub Dawidek explicit_bzero(&md, sizeof(md)); 3924a711b8dSJohn Baldwin zfree(sector, M_ELI); 3932f07cdf8SPawel Jakub Dawidek } 3942f07cdf8SPawel Jakub Dawidek 3952f07cdf8SPawel Jakub Dawidek oldsize = sc->sc_mediasize; 3962f07cdf8SPawel Jakub Dawidek sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize); 3972f07cdf8SPawel Jakub Dawidek g_eli_key_resize(sc); 3982f07cdf8SPawel Jakub Dawidek sc->sc_provsize = pp->mediasize; 3992f07cdf8SPawel Jakub Dawidek 4002f07cdf8SPawel Jakub Dawidek epp = LIST_FIRST(&sc->sc_geom->provider); 4012f07cdf8SPawel Jakub Dawidek g_resize_provider(epp, sc->sc_mediasize); 4022f07cdf8SPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name, 4032f07cdf8SPawel Jakub Dawidek (intmax_t)oldsize, (intmax_t)sc->sc_mediasize); 4042f07cdf8SPawel Jakub Dawidek } 4052f07cdf8SPawel Jakub Dawidek 406bb30fea6SPawel Jakub Dawidek /* 407056638c4SPawel Jakub Dawidek * BIO_READ: 4085ad4a7c7SPawel 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 409056638c4SPawel Jakub Dawidek * BIO_WRITE: 410056638c4SPawel Jakub Dawidek * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 411bb30fea6SPawel Jakub Dawidek */ 412c58794deSPawel Jakub Dawidek static void 413c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp) 414c58794deSPawel Jakub Dawidek { 415c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 416d3a1be90SPawel Jakub Dawidek struct g_consumer *cp; 417c58794deSPawel Jakub Dawidek struct bio *cbp; 418c58794deSPawel Jakub Dawidek 419c58794deSPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 420c58794deSPawel Jakub Dawidek KASSERT(sc != NULL, 421c58794deSPawel Jakub Dawidek ("Provider's error should be set (error=%d)(device=%s).", 422c58794deSPawel Jakub Dawidek bp->bio_to->error, bp->bio_to->name)); 423c58794deSPawel Jakub Dawidek G_ELI_LOGREQ(2, bp, "Request received."); 424c58794deSPawel Jakub Dawidek 425c58794deSPawel Jakub Dawidek switch (bp->bio_cmd) { 426c58794deSPawel Jakub Dawidek case BIO_READ: 427c58794deSPawel Jakub Dawidek case BIO_WRITE: 428d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 42942461fbaSPawel Jakub Dawidek case BIO_FLUSH: 4309a6844d5SKenneth D. Merry case BIO_ZONE: 4318b522bdaSWarner Losh case BIO_SPEEDUP: 432c58794deSPawel Jakub Dawidek break; 433c58794deSPawel Jakub Dawidek case BIO_DELETE: 434c58794deSPawel Jakub Dawidek /* 43546e34470SPawel Jakub Dawidek * If the user hasn't set the NODELETE flag, we just pass 43646e34470SPawel Jakub Dawidek * it down the stack and let the layers beneath us do (or 43746e34470SPawel Jakub Dawidek * not) whatever they do with it. If they have, we 43846e34470SPawel Jakub Dawidek * reject it. A possible extension would be an 43946e34470SPawel Jakub Dawidek * additional flag to take it as a hint to shred the data 44046e34470SPawel Jakub Dawidek * with [multiple?] overwrites. 441c58794deSPawel Jakub Dawidek */ 44246e34470SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_NODELETE)) 44346e34470SPawel Jakub Dawidek break; 444c58794deSPawel Jakub Dawidek default: 445c58794deSPawel Jakub Dawidek g_io_deliver(bp, EOPNOTSUPP); 446c58794deSPawel Jakub Dawidek return; 447c58794deSPawel Jakub Dawidek } 448c58794deSPawel Jakub Dawidek cbp = g_clone_bio(bp); 449c58794deSPawel Jakub Dawidek if (cbp == NULL) { 450c58794deSPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 451c58794deSPawel Jakub Dawidek return; 452c58794deSPawel Jakub Dawidek } 4535ad4a7c7SPawel Jakub Dawidek bp->bio_driver1 = cbp; 4545ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = G_ELI_NEW_BIO; 455d3a1be90SPawel Jakub Dawidek switch (bp->bio_cmd) { 456d3a1be90SPawel Jakub Dawidek case BIO_READ: 457eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 4585ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 0); 459d3a1be90SPawel Jakub Dawidek break; 460eaa3b919SPawel Jakub Dawidek } 461eaa3b919SPawel Jakub Dawidek /* FALLTHROUGH */ 462d3a1be90SPawel Jakub Dawidek case BIO_WRITE: 463c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 464c58794deSPawel Jakub Dawidek bioq_insert_tail(&sc->sc_queue, bp); 465c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 466c58794deSPawel Jakub Dawidek wakeup(sc); 467d3a1be90SPawel Jakub Dawidek break; 468d3a1be90SPawel Jakub Dawidek case BIO_GETATTR: 46942461fbaSPawel Jakub Dawidek case BIO_FLUSH: 47046e34470SPawel Jakub Dawidek case BIO_DELETE: 4718b522bdaSWarner Losh case BIO_SPEEDUP: 4729a6844d5SKenneth D. Merry case BIO_ZONE: 4730bab7fa8SAlan Somers if (bp->bio_cmd == BIO_GETATTR) 4740bab7fa8SAlan Somers cbp->bio_done = g_eli_getattr_done; 4750bab7fa8SAlan Somers else 476d3a1be90SPawel Jakub Dawidek cbp->bio_done = g_std_done; 477d3a1be90SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 478d3a1be90SPawel Jakub Dawidek cbp->bio_to = cp->provider; 479cd0d707eSPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 480d3a1be90SPawel Jakub Dawidek g_io_request(cbp, cp); 481d3a1be90SPawel Jakub Dawidek break; 482c58794deSPawel Jakub Dawidek } 483c58794deSPawel Jakub Dawidek } 484c58794deSPawel Jakub Dawidek 4853ac01bc2SPawel Jakub Dawidek static int 4863ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr) 4873ac01bc2SPawel Jakub Dawidek { 4883ac01bc2SPawel Jakub Dawidek struct g_eli_softc *sc; 489c0341432SJohn Baldwin struct crypto_session_params csp; 490a3d565a1SJohn Baldwin uint32_t caps; 491a3d565a1SJohn Baldwin int error, new_crypto; 492c0341432SJohn Baldwin void *key; 4933ac01bc2SPawel Jakub Dawidek 4943ac01bc2SPawel Jakub Dawidek sc = wr->w_softc; 4953ac01bc2SPawel Jakub Dawidek 496c0341432SJohn Baldwin memset(&csp, 0, sizeof(csp)); 497c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_CIPHER; 498c0341432SJohn Baldwin csp.csp_cipher_alg = sc->sc_ealgo; 499c0341432SJohn Baldwin csp.csp_ivlen = g_eli_ivlen(sc->sc_ealgo); 500c0341432SJohn Baldwin csp.csp_cipher_klen = sc->sc_ekeylen / 8; 5013ac01bc2SPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 502c0341432SJohn Baldwin csp.csp_cipher_klen <<= 1; 503ad0a5236SPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 504c0341432SJohn Baldwin key = g_eli_key_hold(sc, 0, 505ad0a5236SPawel Jakub Dawidek LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize); 506c0341432SJohn Baldwin csp.csp_cipher_key = key; 507ad0a5236SPawel Jakub Dawidek } else { 508c0341432SJohn Baldwin key = NULL; 509c0341432SJohn Baldwin csp.csp_cipher_key = sc->sc_ekey; 510ad0a5236SPawel Jakub Dawidek } 5113ac01bc2SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 512c0341432SJohn Baldwin csp.csp_mode = CSP_MODE_ETA; 513c0341432SJohn Baldwin csp.csp_auth_alg = sc->sc_aalgo; 514c0341432SJohn Baldwin csp.csp_auth_klen = G_ELI_AUTH_SECKEYLEN; 5153ac01bc2SPawel Jakub Dawidek } 5163ac01bc2SPawel Jakub Dawidek 5173ac01bc2SPawel Jakub Dawidek switch (sc->sc_crypto) { 518a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 5193ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 520c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5213ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_SOFTWARE); 5223ac01bc2SPawel Jakub Dawidek break; 5233ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 524c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 5253ac01bc2SPawel Jakub Dawidek CRYPTOCAP_F_HARDWARE); 5263ac01bc2SPawel Jakub Dawidek break; 5273ac01bc2SPawel Jakub Dawidek case G_ELI_CRYPTO_UNKNOWN: 528c0341432SJohn Baldwin error = crypto_newsession(&wr->w_sid, &csp, 529a3d565a1SJohn Baldwin CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE); 5303ac01bc2SPawel Jakub Dawidek if (error == 0) { 531a3d565a1SJohn Baldwin caps = crypto_ses2caps(wr->w_sid); 532a3d565a1SJohn Baldwin if (caps & CRYPTOCAP_F_HARDWARE) 533a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_HW; 534a3d565a1SJohn Baldwin else if (caps & CRYPTOCAP_F_ACCEL_SOFTWARE) 535a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW_ACCEL; 536a3d565a1SJohn Baldwin else 537a3d565a1SJohn Baldwin new_crypto = G_ELI_CRYPTO_SW; 5383ac01bc2SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 5393ac01bc2SPawel Jakub Dawidek if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN) 540a3d565a1SJohn Baldwin sc->sc_crypto = new_crypto; 5413ac01bc2SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 5423ac01bc2SPawel Jakub Dawidek } 5433ac01bc2SPawel Jakub Dawidek break; 5443ac01bc2SPawel Jakub Dawidek default: 5453ac01bc2SPawel Jakub Dawidek panic("%s: invalid condition", __func__); 5463ac01bc2SPawel Jakub Dawidek } 5473ac01bc2SPawel Jakub Dawidek 548c0341432SJohn Baldwin if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) { 549c0341432SJohn Baldwin if (error) 550c0341432SJohn Baldwin g_eli_key_drop(sc, key); 551c0341432SJohn Baldwin else 552c0341432SJohn Baldwin wr->w_first_key = key; 553c0341432SJohn Baldwin } 554ad0a5236SPawel Jakub Dawidek 5553ac01bc2SPawel Jakub Dawidek return (error); 5563ac01bc2SPawel Jakub Dawidek } 5573ac01bc2SPawel Jakub Dawidek 5583ac01bc2SPawel Jakub Dawidek static void 5593ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr) 5603ac01bc2SPawel Jakub Dawidek { 561c0341432SJohn Baldwin struct g_eli_softc *sc; 5623ac01bc2SPawel Jakub Dawidek 5633ac01bc2SPawel Jakub Dawidek crypto_freesession(wr->w_sid); 564c0341432SJohn Baldwin if (wr->w_first_key != NULL) { 565c0341432SJohn Baldwin sc = wr->w_softc; 566c0341432SJohn Baldwin g_eli_key_drop(sc, wr->w_first_key); 567c0341432SJohn Baldwin wr->w_first_key = NULL; 568c0341432SJohn Baldwin } 5693ac01bc2SPawel Jakub Dawidek } 5703ac01bc2SPawel Jakub Dawidek 5715ad4a7c7SPawel Jakub Dawidek static void 5725ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc) 5735ad4a7c7SPawel Jakub Dawidek { 5745ad4a7c7SPawel Jakub Dawidek struct bio *bp; 5755ad4a7c7SPawel Jakub Dawidek 5765ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 5775ad4a7c7SPawel Jakub Dawidek 5785ad4a7c7SPawel Jakub Dawidek while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 5795ad4a7c7SPawel Jakub Dawidek KASSERT(bp->bio_pflags == G_ELI_NEW_BIO, 5805ad4a7c7SPawel Jakub Dawidek ("Not new bio when canceling (bp=%p).", bp)); 5815ad4a7c7SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 5825ad4a7c7SPawel Jakub Dawidek } 5835ad4a7c7SPawel Jakub Dawidek } 5845ad4a7c7SPawel Jakub Dawidek 5855ad4a7c7SPawel Jakub Dawidek static struct bio * 5865ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc) 5875ad4a7c7SPawel Jakub Dawidek { 5885ad4a7c7SPawel Jakub Dawidek struct bio *bp; 5895ad4a7c7SPawel Jakub Dawidek 5905ad4a7c7SPawel Jakub Dawidek mtx_assert(&sc->sc_queue_mtx, MA_OWNED); 5915ad4a7c7SPawel Jakub Dawidek 5925ad4a7c7SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 5935ad4a7c7SPawel Jakub Dawidek return (bioq_takefirst(&sc->sc_queue)); 5945ad4a7c7SPawel Jakub Dawidek /* 5955ad4a7c7SPawel Jakub Dawidek * Device suspended, so we skip new I/O requests. 5965ad4a7c7SPawel Jakub Dawidek */ 5975ad4a7c7SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 5985ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags != G_ELI_NEW_BIO) 5995ad4a7c7SPawel Jakub Dawidek break; 6005ad4a7c7SPawel Jakub Dawidek } 6015ad4a7c7SPawel Jakub Dawidek if (bp != NULL) 6025ad4a7c7SPawel Jakub Dawidek bioq_remove(&sc->sc_queue, bp); 6035ad4a7c7SPawel Jakub Dawidek return (bp); 6045ad4a7c7SPawel Jakub Dawidek } 6055ad4a7c7SPawel Jakub Dawidek 606c58794deSPawel Jakub Dawidek /* 607c58794deSPawel Jakub Dawidek * This is the main function for kernel worker thread when we don't have 608c58794deSPawel Jakub Dawidek * hardware acceleration and we have to do cryptography in software. 609c58794deSPawel Jakub Dawidek * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 610c58794deSPawel Jakub Dawidek * threads with crypto work. 611c58794deSPawel Jakub Dawidek */ 612c58794deSPawel Jakub Dawidek static void 613c58794deSPawel Jakub Dawidek g_eli_worker(void *arg) 614c58794deSPawel Jakub Dawidek { 615c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 616c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 617c58794deSPawel Jakub Dawidek struct bio *bp; 6183ac01bc2SPawel Jakub Dawidek int error; 619c58794deSPawel Jakub Dawidek 620c58794deSPawel Jakub Dawidek wr = arg; 621c58794deSPawel Jakub Dawidek sc = wr->w_softc; 622fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 623fdce57a0SJohn Baldwin MPASS(!sc->sc_cpubind || smp_started); 624fdce57a0SJohn Baldwin #elif defined(SMP) 625a1ea1a22SPawel Jakub Dawidek /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 6260c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) { 627a1ea1a22SPawel Jakub Dawidek while (!smp_started) 628a1ea1a22SPawel Jakub Dawidek tsleep(wr, 0, "geli:smp", hz / 4); 629a1ea1a22SPawel Jakub Dawidek } 630a1ea1a22SPawel Jakub Dawidek #endif 631982d11f8SJeff Roberson thread_lock(curthread); 63231c4cef7SPawel Jakub Dawidek sched_prio(curthread, PUSER); 6330c879bd9SPawel Jakub Dawidek if (sc->sc_cpubind) 6340c879bd9SPawel Jakub Dawidek sched_bind(curthread, wr->w_number % mp_ncpus); 635982d11f8SJeff Roberson thread_unlock(curthread); 636c58794deSPawel Jakub Dawidek 637c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 638c58794deSPawel Jakub Dawidek 639c58794deSPawel Jakub Dawidek for (;;) { 640c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 6415ad4a7c7SPawel Jakub Dawidek again: 6425ad4a7c7SPawel Jakub Dawidek bp = g_eli_takefirst(sc); 643c58794deSPawel Jakub Dawidek if (bp == NULL) { 644eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 6455ad4a7c7SPawel Jakub Dawidek g_eli_cancel(sc); 646c58794deSPawel Jakub Dawidek LIST_REMOVE(wr, w_next); 6473ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 648c58794deSPawel Jakub Dawidek free(wr, M_ELI); 649c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Thread %s exiting.", 650c58794deSPawel Jakub Dawidek curthread->td_proc->p_comm); 651c58794deSPawel Jakub Dawidek wakeup(&sc->sc_workers); 652c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6533745c395SJulian Elischer kproc_exit(0); 654c58794deSPawel Jakub Dawidek } 6555ad4a7c7SPawel Jakub Dawidek while (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 6565ad4a7c7SPawel Jakub Dawidek if (sc->sc_inflight > 0) { 657038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, "inflight=%d", 658038c55adSPawel Jakub Dawidek sc->sc_inflight); 6595ad4a7c7SPawel Jakub Dawidek /* 6605ad4a7c7SPawel Jakub Dawidek * We still have inflight BIOs, so 6615ad4a7c7SPawel Jakub Dawidek * sleep and retry. 6625ad4a7c7SPawel Jakub Dawidek */ 6635ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 6645ad4a7c7SPawel Jakub Dawidek "geli:inf", hz / 5); 6655ad4a7c7SPawel Jakub Dawidek goto again; 6665ad4a7c7SPawel Jakub Dawidek } 6675ad4a7c7SPawel Jakub Dawidek /* 6685ad4a7c7SPawel Jakub Dawidek * Suspend requested, mark the worker as 6695ad4a7c7SPawel Jakub Dawidek * suspended and go to sleep. 6705ad4a7c7SPawel Jakub Dawidek */ 6713ac01bc2SPawel Jakub Dawidek if (wr->w_active) { 6723ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 6733ac01bc2SPawel Jakub Dawidek wr->w_active = FALSE; 6743ac01bc2SPawel Jakub Dawidek } 6755ad4a7c7SPawel Jakub Dawidek wakeup(&sc->sc_workers); 6765ad4a7c7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PRIBIO, 6775ad4a7c7SPawel Jakub Dawidek "geli:suspend", 0); 6783ac01bc2SPawel Jakub Dawidek if (!wr->w_active && 6793ac01bc2SPawel Jakub Dawidek !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) { 6803ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 6813ac01bc2SPawel Jakub Dawidek KASSERT(error == 0, 6823ac01bc2SPawel Jakub Dawidek ("g_eli_newsession() failed on resume (error=%d)", 6833ac01bc2SPawel Jakub Dawidek error)); 6843ac01bc2SPawel Jakub Dawidek wr->w_active = TRUE; 6853ac01bc2SPawel Jakub Dawidek } 6865ad4a7c7SPawel Jakub Dawidek goto again; 6875ad4a7c7SPawel Jakub Dawidek } 68831c4cef7SPawel Jakub Dawidek msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 689c58794deSPawel Jakub Dawidek continue; 690c58794deSPawel Jakub Dawidek } 6915ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) 6925ad4a7c7SPawel Jakub Dawidek atomic_add_int(&sc->sc_inflight, 1); 693c58794deSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 6945ad4a7c7SPawel Jakub Dawidek if (bp->bio_pflags == G_ELI_NEW_BIO) { 6955ad4a7c7SPawel Jakub Dawidek bp->bio_pflags = 0; 6965ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 6975ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 698eaa3b919SPawel Jakub Dawidek g_eli_auth_read(sc, bp); 6995ad4a7c7SPawel Jakub Dawidek else 7005ad4a7c7SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 7015ad4a7c7SPawel Jakub Dawidek } else { 7025ad4a7c7SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) 7035ad4a7c7SPawel Jakub Dawidek g_eli_crypto_read(sc, bp, 1); 7045ad4a7c7SPawel Jakub Dawidek else 7055ad4a7c7SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 7065ad4a7c7SPawel Jakub Dawidek } 7075ad4a7c7SPawel Jakub Dawidek } else { 7085ad4a7c7SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) 709eaa3b919SPawel Jakub Dawidek g_eli_auth_run(wr, bp); 710eaa3b919SPawel Jakub Dawidek else 711dddd1d53SPawel Jakub Dawidek g_eli_crypto_run(wr, bp); 712c58794deSPawel Jakub Dawidek } 713c58794deSPawel Jakub Dawidek } 7145ad4a7c7SPawel Jakub Dawidek } 715c58794deSPawel Jakub Dawidek 7162f07cdf8SPawel Jakub Dawidek static int 7172f07cdf8SPawel Jakub Dawidek g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp, 7182f07cdf8SPawel Jakub Dawidek off_t offset, struct g_eli_metadata *md) 719c58794deSPawel Jakub Dawidek { 720c58794deSPawel Jakub Dawidek struct g_geom *gp; 721c58794deSPawel Jakub Dawidek struct g_consumer *cp; 722c58794deSPawel Jakub Dawidek u_char *buf = NULL; 723c58794deSPawel Jakub Dawidek int error; 724c58794deSPawel Jakub Dawidek 725c58794deSPawel Jakub Dawidek g_topology_assert(); 726c58794deSPawel Jakub Dawidek 727c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "eli:taste"); 728c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 729c58794deSPawel Jakub Dawidek gp->access = g_std_access; 730c58794deSPawel Jakub Dawidek /* 731c58794deSPawel Jakub Dawidek * g_eli_read_metadata() is always called from the event thread. 732c58794deSPawel Jakub Dawidek * Our geom is created and destroyed in the same event, so there 733c58794deSPawel Jakub Dawidek * could be no orphan nor spoil event in the meantime. 734c58794deSPawel Jakub Dawidek */ 735c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan_spoil_assert; 736c58794deSPawel Jakub Dawidek gp->spoiled = g_eli_orphan_spoil_assert; 737c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 7386f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 739c58794deSPawel Jakub Dawidek error = g_attach(cp, pp); 740c58794deSPawel Jakub Dawidek if (error != 0) 741c58794deSPawel Jakub Dawidek goto end; 742c58794deSPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 743c58794deSPawel Jakub Dawidek if (error != 0) 744c58794deSPawel Jakub Dawidek goto end; 745c58794deSPawel Jakub Dawidek g_topology_unlock(); 7462f07cdf8SPawel Jakub Dawidek buf = g_read_data(cp, offset, pp->sectorsize, &error); 747c58794deSPawel Jakub Dawidek g_topology_lock(); 7488a4a44b5SMaxim Sobolev if (buf == NULL) 749c58794deSPawel Jakub Dawidek goto end; 750fefb6a14SPawel Jakub Dawidek error = eli_metadata_decode(buf, md); 751fefb6a14SPawel Jakub Dawidek if (error != 0) 752fefb6a14SPawel Jakub Dawidek goto end; 753fefb6a14SPawel Jakub Dawidek /* Metadata was read and decoded successfully. */ 754c58794deSPawel Jakub Dawidek end: 755c58794deSPawel Jakub Dawidek if (buf != NULL) 756c58794deSPawel Jakub Dawidek g_free(buf); 757c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 758c58794deSPawel Jakub Dawidek if (cp->acr == 1) 759c58794deSPawel Jakub Dawidek g_access(cp, -1, 0, 0); 760c58794deSPawel Jakub Dawidek g_detach(cp); 761c58794deSPawel Jakub Dawidek } 762c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 763c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 764c58794deSPawel Jakub Dawidek return (error); 765c58794deSPawel Jakub Dawidek } 766c58794deSPawel Jakub Dawidek 7672f07cdf8SPawel Jakub Dawidek int 7682f07cdf8SPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 7692f07cdf8SPawel Jakub Dawidek struct g_eli_metadata *md) 7702f07cdf8SPawel Jakub Dawidek { 7712f07cdf8SPawel Jakub Dawidek 7722f07cdf8SPawel Jakub Dawidek return (g_eli_read_metadata_offset(mp, pp, 7732f07cdf8SPawel Jakub Dawidek pp->mediasize - pp->sectorsize, md)); 7742f07cdf8SPawel Jakub Dawidek } 7752f07cdf8SPawel Jakub Dawidek 776c58794deSPawel Jakub Dawidek /* 777c58794deSPawel Jakub Dawidek * The function is called when we had last close on provider and user requested 778c58794deSPawel Jakub Dawidek * to close it when this situation occur. 779c58794deSPawel Jakub Dawidek */ 780c58794deSPawel Jakub Dawidek static void 78119351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused) 782c58794deSPawel Jakub Dawidek { 783c58794deSPawel Jakub Dawidek struct g_geom *gp; 78419351a14SAlexander Motin char gpname[64]; 785c58794deSPawel Jakub Dawidek int error; 786c58794deSPawel Jakub Dawidek 787c58794deSPawel Jakub Dawidek g_topology_assert(); 78819351a14SAlexander Motin gp = arg; 78919351a14SAlexander Motin strlcpy(gpname, gp->name, sizeof(gpname)); 79019351a14SAlexander Motin error = g_eli_destroy(gp->softc, TRUE); 791c58794deSPawel Jakub Dawidek KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 79219351a14SAlexander Motin gpname, error)); 79319351a14SAlexander Motin G_ELI_DEBUG(0, "Detached %s on last close.", gpname); 794c58794deSPawel Jakub Dawidek } 795c58794deSPawel Jakub Dawidek 796c58794deSPawel Jakub Dawidek int 797c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de) 798c58794deSPawel Jakub Dawidek { 799c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 800c58794deSPawel Jakub Dawidek struct g_geom *gp; 801c58794deSPawel Jakub Dawidek 802c58794deSPawel Jakub Dawidek gp = pp->geom; 803c58794deSPawel Jakub Dawidek sc = gp->softc; 804c58794deSPawel Jakub Dawidek 805c58794deSPawel Jakub Dawidek if (dw > 0) { 80685059016SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_RO) { 80785059016SPawel Jakub Dawidek /* Deny write attempts. */ 80885059016SPawel Jakub Dawidek return (EROFS); 80985059016SPawel Jakub Dawidek } 810c58794deSPawel Jakub Dawidek /* Someone is opening us for write, we need to remember that. */ 811c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_WOPEN; 812c58794deSPawel Jakub Dawidek return (0); 813c58794deSPawel Jakub Dawidek } 814c58794deSPawel Jakub Dawidek /* Is this the last close? */ 815c58794deSPawel Jakub Dawidek if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 816c58794deSPawel Jakub Dawidek return (0); 817c58794deSPawel Jakub Dawidek 818c58794deSPawel Jakub Dawidek /* 819c58794deSPawel Jakub Dawidek * Automatically detach on last close if requested. 820c58794deSPawel Jakub Dawidek */ 821c58794deSPawel Jakub Dawidek if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 822c58794deSPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 82319351a14SAlexander Motin g_post_event(g_eli_last_close, gp, M_WAITOK, NULL); 824c58794deSPawel Jakub Dawidek } 825c58794deSPawel Jakub Dawidek return (0); 826c58794deSPawel Jakub Dawidek } 827c58794deSPawel Jakub Dawidek 828eba8f137SPawel Jakub Dawidek static int 829eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu) 830eba8f137SPawel Jakub Dawidek { 831eba8f137SPawel Jakub Dawidek #ifdef SMP 83271a19bdcSAttilio Rao return (CPU_ISSET(cpu, &hlt_cpus_mask)); 833eba8f137SPawel Jakub Dawidek #else 834eba8f137SPawel Jakub Dawidek return (0); 835eba8f137SPawel Jakub Dawidek #endif 836eba8f137SPawel Jakub Dawidek } 837eba8f137SPawel Jakub Dawidek 838c58794deSPawel Jakub Dawidek struct g_geom * 839c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 840c58794deSPawel Jakub Dawidek const struct g_eli_metadata *md, const u_char *mkey, int nkey) 841c58794deSPawel Jakub Dawidek { 842c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 843c58794deSPawel Jakub Dawidek struct g_eli_worker *wr; 844c58794deSPawel Jakub Dawidek struct g_geom *gp; 845c58794deSPawel Jakub Dawidek struct g_provider *pp; 846c58794deSPawel Jakub Dawidek struct g_consumer *cp; 847ae1cce52SWarner Losh struct g_geom_alias *gap; 848c58794deSPawel Jakub Dawidek u_int i, threads; 8493bb6e0f0SRyan Libby int dcw, error; 850c58794deSPawel Jakub Dawidek 851c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 852e2b99193SJohn Baldwin KASSERT(eli_metadata_crypto_supported(md), 853e2b99193SJohn Baldwin ("%s: unsupported crypto for %s", __func__, bpp->name)); 854c58794deSPawel Jakub Dawidek 855c58794deSPawel Jakub Dawidek gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 856c58794deSPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 857c58794deSPawel Jakub Dawidek gp->start = g_eli_start; 858c58794deSPawel Jakub Dawidek /* 8594273d412SPawel Jakub Dawidek * Spoiling can happen even though we have the provider open 8604273d412SPawel Jakub Dawidek * exclusively, e.g. through media change events. 861c58794deSPawel Jakub Dawidek */ 8624273d412SPawel Jakub Dawidek gp->spoiled = g_eli_orphan; 863c58794deSPawel Jakub Dawidek gp->orphan = g_eli_orphan; 8642f07cdf8SPawel Jakub Dawidek gp->resize = g_eli_resize; 86585059016SPawel Jakub Dawidek gp->dumpconf = g_eli_dumpconf; 866c58794deSPawel Jakub Dawidek /* 86785059016SPawel Jakub Dawidek * If detach-on-last-close feature is not enabled and we don't operate 86885059016SPawel Jakub Dawidek * on read-only provider, we can simply use g_std_access(). 869c58794deSPawel Jakub Dawidek */ 87085059016SPawel Jakub Dawidek if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 871c58794deSPawel Jakub Dawidek gp->access = g_eli_access; 872c58794deSPawel Jakub Dawidek else 873c58794deSPawel Jakub Dawidek gp->access = g_std_access; 874c58794deSPawel Jakub Dawidek 8754332fecaSAllan Jude eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize); 876c58794deSPawel Jakub Dawidek sc->sc_nkey = nkey; 877eaa3b919SPawel Jakub Dawidek 878c58794deSPawel Jakub Dawidek gp->softc = sc; 879c58794deSPawel Jakub Dawidek sc->sc_geom = gp; 880c58794deSPawel Jakub Dawidek 881c58794deSPawel Jakub Dawidek bioq_init(&sc->sc_queue); 882c58794deSPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 8831e09ff3dSPawel Jakub Dawidek mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF); 884c58794deSPawel Jakub Dawidek 885c58794deSPawel Jakub Dawidek pp = NULL; 886c58794deSPawel Jakub Dawidek cp = g_new_consumer(gp); 8876f818c1fSAlan Somers cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 888c58794deSPawel Jakub Dawidek error = g_attach(cp, bpp); 889c58794deSPawel Jakub Dawidek if (error != 0) { 890c58794deSPawel Jakub Dawidek if (req != NULL) { 891c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot attach to %s (error=%d).", 892c58794deSPawel Jakub Dawidek bpp->name, error); 893c58794deSPawel Jakub Dawidek } else { 894c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 895c58794deSPawel Jakub Dawidek bpp->name, error); 896c58794deSPawel Jakub Dawidek } 897c58794deSPawel Jakub Dawidek goto failed; 898c58794deSPawel Jakub Dawidek } 899c58794deSPawel Jakub Dawidek /* 900c58794deSPawel Jakub Dawidek * Keep provider open all the time, so we can run critical tasks, 901c58794deSPawel Jakub Dawidek * like Master Keys deletion, without wondering if we can open 902c58794deSPawel Jakub Dawidek * provider or not. 90385059016SPawel Jakub Dawidek * We don't open provider for writing only when user requested read-only 90485059016SPawel Jakub Dawidek * access. 905c58794deSPawel Jakub Dawidek */ 9063bb6e0f0SRyan Libby dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1; 9073bb6e0f0SRyan Libby error = g_access(cp, 1, dcw, 1); 908c58794deSPawel Jakub Dawidek if (error != 0) { 909c58794deSPawel Jakub Dawidek if (req != NULL) { 910c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot access %s (error=%d).", 911c58794deSPawel Jakub Dawidek bpp->name, error); 912c58794deSPawel Jakub Dawidek } else { 913c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 914c58794deSPawel Jakub Dawidek bpp->name, error); 915c58794deSPawel Jakub Dawidek } 916c58794deSPawel Jakub Dawidek goto failed; 917c58794deSPawel Jakub Dawidek } 918c58794deSPawel Jakub Dawidek 919c6a26d4cSPawel Jakub Dawidek /* 920c6a26d4cSPawel Jakub Dawidek * Remember the keys in our softc structure. 921c6a26d4cSPawel Jakub Dawidek */ 922c6a26d4cSPawel Jakub Dawidek g_eli_mkey_propagate(sc, mkey); 923c6a26d4cSPawel Jakub Dawidek 924c58794deSPawel Jakub Dawidek LIST_INIT(&sc->sc_workers); 925c58794deSPawel Jakub Dawidek 926c58794deSPawel Jakub Dawidek threads = g_eli_threads; 927dd549194SPawel Jakub Dawidek if (threads == 0) 928dd549194SPawel Jakub Dawidek threads = mp_ncpus; 9290c879bd9SPawel Jakub Dawidek sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus); 930c58794deSPawel Jakub Dawidek for (i = 0; i < threads; i++) { 931eba8f137SPawel Jakub Dawidek if (g_eli_cpu_is_disabled(i)) { 932eba8f137SPawel Jakub Dawidek G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 9331506db21SPawel Jakub Dawidek bpp->name, i); 934eba8f137SPawel Jakub Dawidek continue; 935eba8f137SPawel Jakub Dawidek } 936c58794deSPawel Jakub Dawidek wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 937c58794deSPawel Jakub Dawidek wr->w_softc = sc; 938c58794deSPawel Jakub Dawidek wr->w_number = i; 9395ad4a7c7SPawel Jakub Dawidek wr->w_active = TRUE; 940c58794deSPawel Jakub Dawidek 9413ac01bc2SPawel Jakub Dawidek error = g_eli_newsession(wr); 942c58794deSPawel Jakub Dawidek if (error != 0) { 943c58794deSPawel Jakub Dawidek free(wr, M_ELI); 944c58794deSPawel Jakub Dawidek if (req != NULL) { 945c58794deSPawel Jakub Dawidek gctl_error(req, "Cannot set up crypto session " 946c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 947c58794deSPawel Jakub Dawidek } else { 948c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot set up crypto session " 949c58794deSPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 950c58794deSPawel Jakub Dawidek } 951c58794deSPawel Jakub Dawidek goto failed; 952c58794deSPawel Jakub Dawidek } 953c58794deSPawel Jakub Dawidek 9543745c395SJulian Elischer error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 955c58794deSPawel Jakub Dawidek "g_eli[%u] %s", i, bpp->name); 956c58794deSPawel Jakub Dawidek if (error != 0) { 9573ac01bc2SPawel Jakub Dawidek g_eli_freesession(wr); 958c58794deSPawel Jakub Dawidek free(wr, M_ELI); 959c58794deSPawel Jakub Dawidek if (req != NULL) { 960dddd1d53SPawel Jakub Dawidek gctl_error(req, "Cannot create kernel thread " 961dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 962c58794deSPawel Jakub Dawidek } else { 963dddd1d53SPawel Jakub Dawidek G_ELI_DEBUG(1, "Cannot create kernel thread " 964dddd1d53SPawel Jakub Dawidek "for %s (error=%d).", bpp->name, error); 965c58794deSPawel Jakub Dawidek } 966c58794deSPawel Jakub Dawidek goto failed; 967c58794deSPawel Jakub Dawidek } 968c58794deSPawel Jakub Dawidek LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 969c58794deSPawel Jakub Dawidek } 970c58794deSPawel Jakub Dawidek 971c58794deSPawel Jakub Dawidek /* 972c58794deSPawel Jakub Dawidek * Create decrypted provider. 973c58794deSPawel Jakub Dawidek */ 974c58794deSPawel Jakub Dawidek pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 9756f818c1fSAlan Somers pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 9767d874f0fSAlan Somers if (CRYPTO_HAS_VMPAGE) { 9777d874f0fSAlan Somers /* 9787d874f0fSAlan Somers * On DMAP architectures we can use unmapped I/O. But don't 9797d874f0fSAlan Somers * use it with data integrity verification. That code hasn't 9807d874f0fSAlan Somers * been written yet. 9817d874f0fSAlan Somers */ 9827d874f0fSAlan Somers if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) 9837d874f0fSAlan Somers pp->flags |= G_PF_ACCEPT_UNMAPPED; 9847d874f0fSAlan Somers } 985c6a26d4cSPawel Jakub Dawidek pp->mediasize = sc->sc_mediasize; 986c6a26d4cSPawel Jakub Dawidek pp->sectorsize = sc->sc_sectorsize; 987ae1cce52SWarner Losh LIST_FOREACH(gap, &bpp->aliases, ga_next) 988ae1cce52SWarner Losh g_provider_add_alias(pp, "%s%s", gap->ga_alias, G_ELI_SUFFIX); 989eaa3b919SPawel Jakub Dawidek 990c58794deSPawel Jakub Dawidek g_error_provider(pp, 0); 991c58794deSPawel Jakub Dawidek 992c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s created.", pp->name); 993eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 994eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 995e2b99193SJohn Baldwin if (sc->sc_flags & G_ELI_FLAG_AUTH) 996eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 997c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, " Crypto: %s", 998a3d565a1SJohn Baldwin sc->sc_crypto == G_ELI_CRYPTO_SW_ACCEL ? "accelerated software" : 999c58794deSPawel Jakub Dawidek sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 1000c58794deSPawel Jakub Dawidek return (gp); 1001c58794deSPawel Jakub Dawidek failed: 1002c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1003c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1004c58794deSPawel Jakub Dawidek wakeup(sc); 1005c58794deSPawel Jakub Dawidek /* 1006c58794deSPawel Jakub Dawidek * Wait for kernel threads self destruction. 1007c58794deSPawel Jakub Dawidek */ 1008c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1009c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1010c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1011c58794deSPawel Jakub Dawidek } 1012c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1013c58794deSPawel Jakub Dawidek if (cp->provider != NULL) { 1014c58794deSPawel Jakub Dawidek if (cp->acr == 1) 10153bb6e0f0SRyan Libby g_access(cp, -1, -dcw, -1); 1016c58794deSPawel Jakub Dawidek g_detach(cp); 1017c58794deSPawel Jakub Dawidek } 1018c58794deSPawel Jakub Dawidek g_destroy_consumer(cp); 1019c58794deSPawel Jakub Dawidek g_destroy_geom(gp); 10201e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1021b172f23dSJohn Baldwin zfree(sc, M_ELI); 1022c58794deSPawel Jakub Dawidek return (NULL); 1023c58794deSPawel Jakub Dawidek } 1024c58794deSPawel Jakub Dawidek 1025c58794deSPawel Jakub Dawidek int 1026c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 1027c58794deSPawel Jakub Dawidek { 1028c58794deSPawel Jakub Dawidek struct g_geom *gp; 1029c58794deSPawel Jakub Dawidek struct g_provider *pp; 1030c58794deSPawel Jakub Dawidek 1031c58794deSPawel Jakub Dawidek g_topology_assert(); 1032c58794deSPawel Jakub Dawidek 1033c58794deSPawel Jakub Dawidek if (sc == NULL) 1034c58794deSPawel Jakub Dawidek return (ENXIO); 1035c58794deSPawel Jakub Dawidek 1036c58794deSPawel Jakub Dawidek gp = sc->sc_geom; 1037c58794deSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1038c58794deSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 1039c58794deSPawel Jakub Dawidek if (force) { 1040c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Device %s is still open, so it " 104198645006SChristian Brueffer "cannot be definitely removed.", pp->name); 104219351a14SAlexander Motin sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 104319351a14SAlexander Motin gp->access = g_eli_access; 104419351a14SAlexander Motin g_wither_provider(pp, ENXIO); 104519351a14SAlexander Motin return (EBUSY); 1046c58794deSPawel Jakub Dawidek } else { 1047c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, 1048c58794deSPawel Jakub Dawidek "Device %s is still open (r%dw%de%d).", pp->name, 1049c58794deSPawel Jakub Dawidek pp->acr, pp->acw, pp->ace); 1050c58794deSPawel Jakub Dawidek return (EBUSY); 1051c58794deSPawel Jakub Dawidek } 1052c58794deSPawel Jakub Dawidek } 1053c58794deSPawel Jakub Dawidek 1054c58794deSPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1055c58794deSPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_DESTROY; 1056c58794deSPawel Jakub Dawidek wakeup(sc); 1057c58794deSPawel Jakub Dawidek while (!LIST_EMPTY(&sc->sc_workers)) { 1058c58794deSPawel Jakub Dawidek msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 1059c58794deSPawel Jakub Dawidek "geli:destroy", 0); 1060c58794deSPawel Jakub Dawidek } 1061c58794deSPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1062c58794deSPawel Jakub Dawidek gp->softc = NULL; 10631e09ff3dSPawel Jakub Dawidek g_eli_key_destroy(sc); 1064b172f23dSJohn Baldwin zfree(sc, M_ELI); 1065c58794deSPawel Jakub Dawidek 1066c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 1067c58794deSPawel Jakub Dawidek g_wither_geom_close(gp, ENXIO); 1068c58794deSPawel Jakub Dawidek 1069c58794deSPawel Jakub Dawidek return (0); 1070c58794deSPawel Jakub Dawidek } 1071c58794deSPawel Jakub Dawidek 1072c58794deSPawel Jakub Dawidek static int 1073c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused, 1074c58794deSPawel Jakub Dawidek struct g_class *mp __unused, struct g_geom *gp) 1075c58794deSPawel Jakub Dawidek { 1076c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1077c58794deSPawel Jakub Dawidek 1078c58794deSPawel Jakub Dawidek sc = gp->softc; 10795ad4a7c7SPawel Jakub Dawidek return (g_eli_destroy(sc, FALSE)); 1080c58794deSPawel Jakub Dawidek } 1081c58794deSPawel Jakub Dawidek 10829af2131bSPawel Jakub Dawidek static int 10839af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 10849af2131bSPawel Jakub Dawidek { 10851e189c08SMarcel Moolenaar u_char *keyfile, *data; 10869af2131bSPawel Jakub Dawidek char *file, name[64]; 10871e189c08SMarcel Moolenaar size_t size; 10889af2131bSPawel Jakub Dawidek int i; 10899af2131bSPawel Jakub Dawidek 10909af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 10919af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 10929af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1093edaa9008SPawel Jakub Dawidek if (keyfile == NULL && i == 0) { 1094edaa9008SPawel Jakub Dawidek /* 1095edaa9008SPawel Jakub Dawidek * If there is only one keyfile, allow simpler name. 1096edaa9008SPawel Jakub Dawidek */ 1097edaa9008SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile", provider); 1098edaa9008SPawel Jakub Dawidek keyfile = preload_search_by_type(name); 1099edaa9008SPawel Jakub Dawidek } 11009af2131bSPawel Jakub Dawidek if (keyfile == NULL) 11019af2131bSPawel Jakub Dawidek return (i); /* Return number of loaded keyfiles. */ 11021e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 11039af2131bSPawel Jakub Dawidek if (data == NULL) { 11049af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file data for %s.", 11059af2131bSPawel Jakub Dawidek name); 11069af2131bSPawel Jakub Dawidek return (0); 11079af2131bSPawel Jakub Dawidek } 11081e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 11091e189c08SMarcel Moolenaar if (size == 0) { 11109af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file size for %s.", 11119af2131bSPawel Jakub Dawidek name); 11129af2131bSPawel Jakub Dawidek return (0); 11139af2131bSPawel Jakub Dawidek } 11149af2131bSPawel Jakub Dawidek file = preload_search_info(keyfile, MODINFO_NAME); 11159af2131bSPawel Jakub Dawidek if (file == NULL) { 11169af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot find key file name for %s.", 11179af2131bSPawel Jakub Dawidek name); 11189af2131bSPawel Jakub Dawidek return (0); 11199af2131bSPawel Jakub Dawidek } 11209af2131bSPawel Jakub Dawidek G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 11219af2131bSPawel Jakub Dawidek provider, name); 11221e189c08SMarcel Moolenaar g_eli_crypto_hmac_update(ctx, data, size); 11239af2131bSPawel Jakub Dawidek } 11249af2131bSPawel Jakub Dawidek } 11259af2131bSPawel Jakub Dawidek 11269af2131bSPawel Jakub Dawidek static void 11279af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider) 11289af2131bSPawel Jakub Dawidek { 11291e189c08SMarcel Moolenaar u_char *keyfile, *data; 11309af2131bSPawel Jakub Dawidek char name[64]; 11311e189c08SMarcel Moolenaar size_t size; 11329af2131bSPawel Jakub Dawidek int i; 11339af2131bSPawel Jakub Dawidek 11349af2131bSPawel Jakub Dawidek for (i = 0; ; i++) { 11359af2131bSPawel Jakub Dawidek snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 11369af2131bSPawel Jakub Dawidek keyfile = preload_search_by_type(name); 11379af2131bSPawel Jakub Dawidek if (keyfile == NULL) 11389af2131bSPawel Jakub Dawidek return; 11391e189c08SMarcel Moolenaar data = preload_fetch_addr(keyfile); 11401e189c08SMarcel Moolenaar size = preload_fetch_size(keyfile); 11411e189c08SMarcel Moolenaar if (data != NULL && size != 0) 11426572e5ffSJohn Baldwin explicit_bzero(data, size); 11439af2131bSPawel Jakub Dawidek } 11449af2131bSPawel Jakub Dawidek } 11459af2131bSPawel Jakub Dawidek 1146c58794deSPawel Jakub Dawidek /* 1147c58794deSPawel Jakub Dawidek * Tasting is only made on boot. 1148c58794deSPawel Jakub Dawidek * We detect providers which should be attached before root is mounted. 1149c58794deSPawel Jakub Dawidek */ 1150c58794deSPawel Jakub Dawidek static struct g_geom * 1151c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 1152c58794deSPawel Jakub Dawidek { 1153c58794deSPawel Jakub Dawidek struct g_eli_metadata md; 1154c58794deSPawel Jakub Dawidek struct g_geom *gp; 1155c58794deSPawel Jakub Dawidek struct hmac_ctx ctx; 1156c58794deSPawel Jakub Dawidek char passphrase[256]; 1157c58794deSPawel Jakub Dawidek u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 11583453dc72SMariusz Zaborski u_int i, nkey, nkeyfiles, tries, showpass; 1159c58794deSPawel Jakub Dawidek int error; 1160ec5c0e5bSAllan Jude struct keybuf *keybuf; 1161c58794deSPawel Jakub Dawidek 1162c58794deSPawel Jakub Dawidek g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 1163c58794deSPawel Jakub Dawidek g_topology_assert(); 1164c58794deSPawel Jakub Dawidek 1165df3aed4fSPawel Jakub Dawidek if (root_mounted() || g_eli_tries == 0) 1166c58794deSPawel Jakub Dawidek return (NULL); 1167c58794deSPawel Jakub Dawidek 1168c58794deSPawel Jakub Dawidek G_ELI_DEBUG(3, "Tasting %s.", pp->name); 1169c58794deSPawel Jakub Dawidek 1170c58794deSPawel Jakub Dawidek error = g_eli_read_metadata(mp, pp, &md); 1171c58794deSPawel Jakub Dawidek if (error != 0) 1172c58794deSPawel Jakub Dawidek return (NULL); 1173c58794deSPawel Jakub Dawidek gp = NULL; 1174c58794deSPawel Jakub Dawidek 1175c58794deSPawel Jakub Dawidek if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 1176c58794deSPawel Jakub Dawidek return (NULL); 1177c58794deSPawel Jakub Dawidek if (md.md_version > G_ELI_VERSION) { 1178c58794deSPawel Jakub Dawidek printf("geom_eli.ko module is too old to handle %s.\n", 1179c58794deSPawel Jakub Dawidek pp->name); 1180c58794deSPawel Jakub Dawidek return (NULL); 1181c58794deSPawel Jakub Dawidek } 1182c58794deSPawel Jakub Dawidek if (md.md_provsize != pp->mediasize) 1183c58794deSPawel Jakub Dawidek return (NULL); 1184c58794deSPawel Jakub Dawidek /* Should we attach it on boot? */ 1185c81929d3SKyle Evans if (!(md.md_flags & G_ELI_FLAG_BOOT) && 1186c81929d3SKyle Evans !(md.md_flags & G_ELI_FLAG_GELIBOOT)) 1187c58794deSPawel Jakub Dawidek return (NULL); 1188c58794deSPawel Jakub Dawidek if (md.md_keys == 0x00) { 1189c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1190c58794deSPawel Jakub Dawidek return (NULL); 1191c58794deSPawel Jakub Dawidek } 1192e2b99193SJohn Baldwin if (!eli_metadata_crypto_supported(&md)) { 1193e2b99193SJohn Baldwin G_ELI_DEBUG(0, "%s uses invalid or unsupported algorithms\n", 1194e2b99193SJohn Baldwin pp->name); 1195e2b99193SJohn Baldwin return (NULL); 1196e2b99193SJohn Baldwin } 11979af2131bSPawel Jakub Dawidek if (md.md_iterations == -1) { 11989af2131bSPawel Jakub Dawidek /* If there is no passphrase, we try only once. */ 11999af2131bSPawel Jakub Dawidek tries = 1; 12009af2131bSPawel Jakub Dawidek } else { 12019af2131bSPawel Jakub Dawidek /* Ask for the passphrase no more than g_eli_tries times. */ 12029af2131bSPawel Jakub Dawidek tries = g_eli_tries; 12039af2131bSPawel Jakub Dawidek } 12049af2131bSPawel Jakub Dawidek 1205ec5c0e5bSAllan Jude if ((keybuf = get_keybuf()) != NULL) { 1206ec5c0e5bSAllan Jude /* Scan the key buffer, try all GELI keys. */ 1207ec5c0e5bSAllan Jude for (i = 0; i < keybuf->kb_nents; i++) { 1208ec5c0e5bSAllan Jude if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) { 1209ec5c0e5bSAllan Jude memcpy(key, keybuf->kb_ents[i].ke_data, 1210ec5c0e5bSAllan Jude sizeof(key)); 1211ec5c0e5bSAllan Jude 121231f7586dSMariusz Zaborski if (g_eli_mkey_decrypt_any(&md, key, 1213ec5c0e5bSAllan Jude mkey, &nkey) == 0 ) { 1214ec5c0e5bSAllan Jude explicit_bzero(key, sizeof(key)); 1215ec5c0e5bSAllan Jude goto have_key; 1216ec5c0e5bSAllan Jude } 1217ec5c0e5bSAllan Jude } 1218ec5c0e5bSAllan Jude } 1219ec5c0e5bSAllan Jude } 1220ec5c0e5bSAllan Jude 1221835c4dd4SColin Percival for (i = 0; i <= tries; i++) { 12229af2131bSPawel Jakub Dawidek g_eli_crypto_hmac_init(&ctx, NULL, 0); 1223c58794deSPawel Jakub Dawidek 1224c58794deSPawel Jakub Dawidek /* 12259af2131bSPawel Jakub Dawidek * Load all key files. 1226c58794deSPawel Jakub Dawidek */ 12279af2131bSPawel Jakub Dawidek nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 12289af2131bSPawel Jakub Dawidek 12299af2131bSPawel Jakub Dawidek if (nkeyfiles == 0 && md.md_iterations == -1) { 12309af2131bSPawel Jakub Dawidek /* 12319af2131bSPawel Jakub Dawidek * No key files and no passphrase, something is 12329af2131bSPawel Jakub Dawidek * definitely wrong here. 12339af2131bSPawel Jakub Dawidek * geli(8) doesn't allow for such situation, so assume 12349af2131bSPawel Jakub Dawidek * that there was really no passphrase and in that case 12359af2131bSPawel Jakub Dawidek * key files are no properly defined in loader.conf. 12369af2131bSPawel Jakub Dawidek */ 12379af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 12389af2131bSPawel Jakub Dawidek "Found no key files in loader.conf for %s.", 12399af2131bSPawel Jakub Dawidek pp->name); 12409af2131bSPawel Jakub Dawidek return (NULL); 12419af2131bSPawel Jakub Dawidek } 12429af2131bSPawel Jakub Dawidek 12439af2131bSPawel Jakub Dawidek /* Ask for the passphrase if defined. */ 12449af2131bSPawel Jakub Dawidek if (md.md_iterations >= 0) { 1245835c4dd4SColin Percival /* Try first with cached passphrase. */ 1246835c4dd4SColin Percival if (i == 0) { 1247835c4dd4SColin Percival if (!g_eli_boot_passcache) 1248835c4dd4SColin Percival continue; 1249835c4dd4SColin Percival memcpy(passphrase, cached_passphrase, 1250835c4dd4SColin Percival sizeof(passphrase)); 1251835c4dd4SColin Percival } else { 1252c58794deSPawel Jakub Dawidek printf("Enter passphrase for %s: ", pp->name); 12533453dc72SMariusz Zaborski showpass = g_eli_visible_passphrase; 12543453dc72SMariusz Zaborski if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0) 12553453dc72SMariusz Zaborski showpass = GETS_ECHOPASS; 1256f6ce353eSAndriy Gapon cngets(passphrase, sizeof(passphrase), 12573453dc72SMariusz Zaborski showpass); 1258835c4dd4SColin Percival memcpy(cached_passphrase, passphrase, 1259835c4dd4SColin Percival sizeof(passphrase)); 1260835c4dd4SColin Percival } 12619af2131bSPawel Jakub Dawidek } 12629af2131bSPawel Jakub Dawidek 1263c58794deSPawel Jakub Dawidek /* 1264c58794deSPawel Jakub Dawidek * Prepare Derived-Key from the user passphrase. 1265c58794deSPawel Jakub Dawidek */ 1266c58794deSPawel Jakub Dawidek if (md.md_iterations == 0) { 1267c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, md.md_salt, 1268c58794deSPawel Jakub Dawidek sizeof(md.md_salt)); 1269c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, passphrase, 1270c58794deSPawel Jakub Dawidek strlen(passphrase)); 1271ec5c0e5bSAllan Jude explicit_bzero(passphrase, sizeof(passphrase)); 12729af2131bSPawel Jakub Dawidek } else if (md.md_iterations > 0) { 1273c58794deSPawel Jakub Dawidek u_char dkey[G_ELI_USERKEYLEN]; 1274c58794deSPawel Jakub Dawidek 1275c58794deSPawel Jakub Dawidek pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1276c58794deSPawel Jakub Dawidek sizeof(md.md_salt), passphrase, md.md_iterations); 12776572e5ffSJohn Baldwin explicit_bzero(passphrase, sizeof(passphrase)); 1278c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1279ec5c0e5bSAllan Jude explicit_bzero(dkey, sizeof(dkey)); 1280c58794deSPawel Jakub Dawidek } 12819af2131bSPawel Jakub Dawidek 1282c58794deSPawel Jakub Dawidek g_eli_crypto_hmac_final(&ctx, key, 0); 1283c58794deSPawel Jakub Dawidek 1284c58794deSPawel Jakub Dawidek /* 1285c58794deSPawel Jakub Dawidek * Decrypt Master-Key. 1286c58794deSPawel Jakub Dawidek */ 128731f7586dSMariusz Zaborski error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey); 12886572e5ffSJohn Baldwin explicit_bzero(key, sizeof(key)); 1289c58794deSPawel Jakub Dawidek if (error == -1) { 1290835c4dd4SColin Percival if (i == tries) { 12919af2131bSPawel Jakub Dawidek G_ELI_DEBUG(0, 12929af2131bSPawel Jakub Dawidek "Wrong key for %s. No tries left.", 12939af2131bSPawel Jakub Dawidek pp->name); 12949af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 12959af2131bSPawel Jakub Dawidek return (NULL); 1296c58794deSPawel Jakub Dawidek } 1297835c4dd4SColin Percival if (i > 0) { 1298835c4dd4SColin Percival G_ELI_DEBUG(0, 1299835c4dd4SColin Percival "Wrong key for %s. Tries left: %u.", 1300835c4dd4SColin Percival pp->name, tries - i); 1301835c4dd4SColin Percival } 1302c58794deSPawel Jakub Dawidek /* Try again. */ 1303c58794deSPawel Jakub Dawidek continue; 1304c58794deSPawel Jakub Dawidek } else if (error > 0) { 1305038c55adSPawel Jakub Dawidek G_ELI_DEBUG(0, 1306038c55adSPawel Jakub Dawidek "Cannot decrypt Master Key for %s (error=%d).", 1307c58794deSPawel Jakub Dawidek pp->name, error); 13089af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(pp->name); 1309c58794deSPawel Jakub Dawidek return (NULL); 1310c58794deSPawel Jakub Dawidek } 1311ebd05adaSBrad Davis g_eli_keyfiles_clear(pp->name); 1312c58794deSPawel Jakub Dawidek G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1313c58794deSPawel Jakub Dawidek break; 1314c58794deSPawel Jakub Dawidek } 1315ec5c0e5bSAllan Jude have_key: 1316c58794deSPawel Jakub Dawidek 1317c58794deSPawel Jakub Dawidek /* 1318c58794deSPawel Jakub Dawidek * We have correct key, let's attach provider. 1319c58794deSPawel Jakub Dawidek */ 1320c58794deSPawel Jakub Dawidek gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 13216572e5ffSJohn Baldwin explicit_bzero(mkey, sizeof(mkey)); 13226572e5ffSJohn Baldwin explicit_bzero(&md, sizeof(md)); 1323c58794deSPawel Jakub Dawidek if (gp == NULL) { 1324c58794deSPawel Jakub Dawidek G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1325c58794deSPawel Jakub Dawidek G_ELI_SUFFIX); 1326c58794deSPawel Jakub Dawidek return (NULL); 1327c58794deSPawel Jakub Dawidek } 1328c58794deSPawel Jakub Dawidek return (gp); 1329c58794deSPawel Jakub Dawidek } 1330c58794deSPawel Jakub Dawidek 1331c58794deSPawel Jakub Dawidek static void 1332c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1333c58794deSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 1334c58794deSPawel Jakub Dawidek { 1335c58794deSPawel Jakub Dawidek struct g_eli_softc *sc; 1336c58794deSPawel Jakub Dawidek 1337c58794deSPawel Jakub Dawidek g_topology_assert(); 1338c58794deSPawel Jakub Dawidek sc = gp->softc; 1339c58794deSPawel Jakub Dawidek if (sc == NULL) 1340c58794deSPawel Jakub Dawidek return; 1341c58794deSPawel Jakub Dawidek if (pp != NULL || cp != NULL) 1342c58794deSPawel Jakub Dawidek return; /* Nothing here. */ 13431e09ff3dSPawel Jakub Dawidek 1344743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent, 13451e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_total); 1346743437c4SAndrey V. Elsukov sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent, 13471e09ff3dSPawel Jakub Dawidek (uintmax_t)sc->sc_ekeys_allocated); 1348ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<Flags>", indent); 1349ea35a2ecSPawel Jakub Dawidek if (sc->sc_flags == 0) 135049ee0fceSAlexander Motin sbuf_cat(sb, "NONE"); 1351ea35a2ecSPawel Jakub Dawidek else { 1352ea35a2ecSPawel Jakub Dawidek int first = 1; 1353ea35a2ecSPawel Jakub Dawidek 1354ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name) do { \ 1355eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & (flag)) { \ 1356ea35a2ecSPawel Jakub Dawidek if (!first) \ 135749ee0fceSAlexander Motin sbuf_cat(sb, ", "); \ 1358ea35a2ecSPawel Jakub Dawidek else \ 1359ea35a2ecSPawel Jakub Dawidek first = 0; \ 136049ee0fceSAlexander Motin sbuf_cat(sb, name); \ 1361ea35a2ecSPawel Jakub Dawidek } \ 1362ea35a2ecSPawel Jakub Dawidek } while (0) 13635ad4a7c7SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND"); 1364c6a26d4cSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 13652bd4ade6SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1366ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1367ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1368ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1369ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1370eaa3b919SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1371ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1372ea35a2ecSPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 137385059016SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 137446e34470SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE"); 1375d8736625SAllan Jude ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT"); 13763453dc72SMariusz Zaborski ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS"); 13772f07cdf8SPawel Jakub Dawidek ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE"); 1378ea35a2ecSPawel Jakub Dawidek #undef ADD_FLAG 1379ea35a2ecSPawel Jakub Dawidek } 138049ee0fceSAlexander Motin sbuf_cat(sb, "</Flags>\n"); 1381ea35a2ecSPawel Jakub Dawidek 1382eaa3b919SPawel Jakub Dawidek if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1383ea35a2ecSPawel Jakub Dawidek sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1384ea35a2ecSPawel Jakub Dawidek sc->sc_nkey); 1385ea35a2ecSPawel Jakub Dawidek } 13861f8c92e6SPawel Jakub Dawidek sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version); 1387c58794deSPawel Jakub Dawidek sbuf_printf(sb, "%s<Crypto>", indent); 1388c58794deSPawel Jakub Dawidek switch (sc->sc_crypto) { 1389c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_HW: 139049ee0fceSAlexander Motin sbuf_cat(sb, "hardware"); 1391c58794deSPawel Jakub Dawidek break; 1392c58794deSPawel Jakub Dawidek case G_ELI_CRYPTO_SW: 139349ee0fceSAlexander Motin sbuf_cat(sb, "software"); 1394c58794deSPawel Jakub Dawidek break; 1395a3d565a1SJohn Baldwin case G_ELI_CRYPTO_SW_ACCEL: 1396a3d565a1SJohn Baldwin sbuf_cat(sb, "accelerated software"); 1397a3d565a1SJohn Baldwin break; 1398c58794deSPawel Jakub Dawidek default: 139949ee0fceSAlexander Motin sbuf_cat(sb, "UNKNOWN"); 1400c58794deSPawel Jakub Dawidek break; 1401c58794deSPawel Jakub Dawidek } 140249ee0fceSAlexander Motin sbuf_cat(sb, "</Crypto>\n"); 1403eaa3b919SPawel Jakub Dawidek if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1404eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, 1405eaa3b919SPawel Jakub Dawidek "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1406eaa3b919SPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_aalgo)); 1407eaa3b919SPawel Jakub Dawidek } 1408eaa3b919SPawel Jakub Dawidek sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1409eaa3b919SPawel Jakub Dawidek sc->sc_ekeylen); 1410038c55adSPawel Jakub Dawidek sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", 1411038c55adSPawel Jakub Dawidek indent, g_eli_algo2str(sc->sc_ealgo)); 1412d8d61ef8SPawel Jakub Dawidek sbuf_printf(sb, "%s<State>%s</State>\n", indent, 1413d8d61ef8SPawel Jakub Dawidek (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE"); 1414c58794deSPawel Jakub Dawidek } 1415c58794deSPawel Jakub Dawidek 1416c5d387d0SPawel Jakub Dawidek static void 1417c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto) 1418c5d387d0SPawel Jakub Dawidek { 1419c5d387d0SPawel Jakub Dawidek struct g_class *mp; 1420c5d387d0SPawel Jakub Dawidek struct g_geom *gp, *gp2; 1421c5d387d0SPawel Jakub Dawidek struct g_provider *pp; 1422c5d387d0SPawel Jakub Dawidek struct g_eli_softc *sc; 1423c5d387d0SPawel Jakub Dawidek int error; 1424c5d387d0SPawel Jakub Dawidek 1425c5d387d0SPawel Jakub Dawidek mp = arg; 1426c5d387d0SPawel Jakub Dawidek g_topology_lock(); 1427c5d387d0SPawel Jakub Dawidek LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1428c5d387d0SPawel Jakub Dawidek sc = gp->softc; 1429c5d387d0SPawel Jakub Dawidek if (sc == NULL) 1430c5d387d0SPawel Jakub Dawidek continue; 1431c5d387d0SPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider); 1432c5d387d0SPawel Jakub Dawidek KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 14332a230609SAlan Somers if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 || 14342a230609SAlan Somers SCHEDULER_STOPPED()) 14352a230609SAlan Somers { 1436c5d387d0SPawel Jakub Dawidek sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1437c5d387d0SPawel Jakub Dawidek gp->access = g_eli_access; 14382a230609SAlan Somers } else { 14392a230609SAlan Somers error = g_eli_destroy(sc, TRUE); 1440c5d387d0SPawel Jakub Dawidek } 1441c5d387d0SPawel Jakub Dawidek } 1442c5d387d0SPawel Jakub Dawidek g_topology_unlock(); 1443c5d387d0SPawel Jakub Dawidek } 1444c5d387d0SPawel Jakub Dawidek 1445c5d387d0SPawel Jakub Dawidek static void 1446c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp) 1447c5d387d0SPawel Jakub Dawidek { 1448c5d387d0SPawel Jakub Dawidek 1449c5d387d0SPawel Jakub Dawidek g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1450c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1451c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync == NULL) 1452c5d387d0SPawel Jakub Dawidek G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1453c5d387d0SPawel Jakub Dawidek } 1454c5d387d0SPawel Jakub Dawidek 1455c5d387d0SPawel Jakub Dawidek static void 1456c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp) 1457c5d387d0SPawel Jakub Dawidek { 1458c5d387d0SPawel Jakub Dawidek 1459c5d387d0SPawel Jakub Dawidek if (g_eli_pre_sync != NULL) 1460c5d387d0SPawel Jakub Dawidek EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1461c5d387d0SPawel Jakub Dawidek } 1462c5d387d0SPawel Jakub Dawidek 1463c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1464f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 146574d6c131SKyle Evans MODULE_VERSION(geom_eli, 0); 1466