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