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