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