xref: /freebsd/sys/geom/eli/g_eli.c (revision edaa9008ffa1b7836c2766d1d614507a83d73627)
1c58794deSPawel Jakub Dawidek /*-
21e09ff3dSPawel Jakub Dawidek  * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
3c58794deSPawel Jakub Dawidek  * All rights reserved.
4c58794deSPawel Jakub Dawidek  *
5c58794deSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
6c58794deSPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
7c58794deSPawel Jakub Dawidek  * are met:
8c58794deSPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
9c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
10c58794deSPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
11c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
12c58794deSPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
13c58794deSPawel Jakub Dawidek  *
14c58794deSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15c58794deSPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c58794deSPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c58794deSPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18c58794deSPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c58794deSPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c58794deSPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c58794deSPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c58794deSPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c58794deSPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c58794deSPawel Jakub Dawidek  * SUCH DAMAGE.
25c58794deSPawel Jakub Dawidek  */
26c58794deSPawel Jakub Dawidek 
27c58794deSPawel Jakub Dawidek #include <sys/cdefs.h>
28c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$");
29c58794deSPawel Jakub Dawidek 
30c58794deSPawel Jakub Dawidek #include <sys/param.h>
31c58794deSPawel Jakub Dawidek #include <sys/systm.h>
32f6ce353eSAndriy Gapon #include <sys/cons.h>
33c58794deSPawel Jakub Dawidek #include <sys/kernel.h>
349af2131bSPawel Jakub Dawidek #include <sys/linker.h>
35c58794deSPawel Jakub Dawidek #include <sys/module.h>
36c58794deSPawel Jakub Dawidek #include <sys/lock.h>
37c58794deSPawel Jakub Dawidek #include <sys/mutex.h>
38c58794deSPawel Jakub Dawidek #include <sys/bio.h>
395d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
40c58794deSPawel Jakub Dawidek #include <sys/sysctl.h>
41c58794deSPawel Jakub Dawidek #include <sys/malloc.h>
42c5d387d0SPawel Jakub Dawidek #include <sys/eventhandler.h>
43c58794deSPawel Jakub Dawidek #include <sys/kthread.h>
44c58794deSPawel Jakub Dawidek #include <sys/proc.h>
45c58794deSPawel Jakub Dawidek #include <sys/sched.h>
46dd549194SPawel Jakub Dawidek #include <sys/smp.h>
47c58794deSPawel Jakub Dawidek #include <sys/uio.h>
48a80f82a4SPawel Jakub Dawidek #include <sys/vnode.h>
49c58794deSPawel Jakub Dawidek 
50c58794deSPawel Jakub Dawidek #include <vm/uma.h>
51c58794deSPawel Jakub Dawidek 
52c58794deSPawel Jakub Dawidek #include <geom/geom.h>
53c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h>
54c58794deSPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h>
55c58794deSPawel Jakub Dawidek 
56cb08c2ccSAlexander Leidinger FEATURE(geom_eli, "GEOM crypto module");
57c58794deSPawel Jakub Dawidek 
58c58794deSPawel Jakub Dawidek MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
59c58794deSPawel Jakub Dawidek 
60c58794deSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
61c58794deSPawel Jakub Dawidek SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff");
62a1f4a8c4SPawel Jakub Dawidek static int g_eli_version = G_ELI_VERSION;
63a1f4a8c4SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0,
64a1f4a8c4SPawel Jakub Dawidek     "GELI version");
65f95168e0SPawel Jakub Dawidek int g_eli_debug = 0;
66af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0,
67c58794deSPawel Jakub Dawidek     "Debug level");
68c58794deSPawel Jakub Dawidek static u_int g_eli_tries = 3;
69af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0,
7098645006SChristian Brueffer     "Number of tries for entering the passphrase");
71eb4c31fdSEd Schouten static u_int g_eli_visible_passphrase = GETS_NOECHO;
72af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN,
73c58794deSPawel Jakub Dawidek     &g_eli_visible_passphrase, 0,
74eb4c31fdSEd Schouten     "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)");
75b35bfe7eSPawel Jakub Dawidek u_int g_eli_overwrites = G_ELI_OVERWRITES;
76af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites,
7798645006SChristian Brueffer     0, "Number of times on-disk keys should be overwritten when destroying them");
78dd549194SPawel Jakub Dawidek static u_int g_eli_threads = 0;
79af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0,
80c58794deSPawel Jakub Dawidek     "Number of threads doing crypto work");
81eaa3b919SPawel Jakub Dawidek u_int g_eli_batch = 0;
82af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0,
83eaa3b919SPawel Jakub Dawidek     "Use crypto operations batching");
84c58794deSPawel Jakub Dawidek 
85835c4dd4SColin Percival /*
86835c4dd4SColin Percival  * Passphrase cached during boot, in order to be more user-friendly if
87835c4dd4SColin Percival  * there are multiple providers using the same passphrase.
88835c4dd4SColin Percival  */
89835c4dd4SColin Percival static char cached_passphrase[256];
90835c4dd4SColin Percival static u_int g_eli_boot_passcache = 1;
91835c4dd4SColin Percival TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache);
92835c4dd4SColin Percival SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD,
93835c4dd4SColin Percival     &g_eli_boot_passcache, 0,
94835c4dd4SColin Percival     "Passphrases are cached during boot process for possible reuse");
95835c4dd4SColin Percival static void
9666427784SColin Percival fetch_loader_passphrase(void * dummy)
9766427784SColin Percival {
9866427784SColin Percival 	char * env_passphrase;
9966427784SColin Percival 
10066427784SColin Percival 	KASSERT(dynamic_kenv, ("need dynamic kenv"));
10166427784SColin Percival 
10266427784SColin Percival 	if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) {
10366427784SColin Percival 		/* Extract passphrase from the environment. */
10466427784SColin Percival 		strlcpy(cached_passphrase, env_passphrase,
10566427784SColin Percival 		    sizeof(cached_passphrase));
10666427784SColin Percival 		freeenv(env_passphrase);
10766427784SColin Percival 
10866427784SColin Percival 		/* Wipe the passphrase from the environment. */
10966427784SColin Percival 		kern_unsetenv("kern.geom.eli.passphrase");
11066427784SColin Percival 	}
11166427784SColin Percival }
11266427784SColin Percival SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KMEM + 1, SI_ORDER_ANY,
11366427784SColin Percival     fetch_loader_passphrase, NULL);
11466427784SColin Percival static void
115835c4dd4SColin Percival zero_boot_passcache(void * dummy)
116835c4dd4SColin Percival {
117835c4dd4SColin Percival 
118835c4dd4SColin Percival 	memset(cached_passphrase, 0, sizeof(cached_passphrase));
119835c4dd4SColin Percival }
120835c4dd4SColin Percival EVENTHANDLER_DEFINE(mountroot, zero_boot_passcache, NULL, 0);
121835c4dd4SColin Percival 
122c5d387d0SPawel Jakub Dawidek static eventhandler_tag g_eli_pre_sync = NULL;
123c5d387d0SPawel Jakub Dawidek 
124c58794deSPawel Jakub Dawidek static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
125c58794deSPawel Jakub Dawidek     struct g_geom *gp);
126c5d387d0SPawel Jakub Dawidek static void g_eli_init(struct g_class *mp);
127c5d387d0SPawel Jakub Dawidek static void g_eli_fini(struct g_class *mp);
128c58794deSPawel Jakub Dawidek 
129c58794deSPawel Jakub Dawidek static g_taste_t g_eli_taste;
130c58794deSPawel Jakub Dawidek static g_dumpconf_t g_eli_dumpconf;
131c58794deSPawel Jakub Dawidek 
132c58794deSPawel Jakub Dawidek struct g_class g_eli_class = {
133c58794deSPawel Jakub Dawidek 	.name = G_ELI_CLASS_NAME,
134c58794deSPawel Jakub Dawidek 	.version = G_VERSION,
135c58794deSPawel Jakub Dawidek 	.ctlreq = g_eli_config,
136c58794deSPawel Jakub Dawidek 	.taste = g_eli_taste,
137c5d387d0SPawel Jakub Dawidek 	.destroy_geom = g_eli_destroy_geom,
138c5d387d0SPawel Jakub Dawidek 	.init = g_eli_init,
139c5d387d0SPawel Jakub Dawidek 	.fini = g_eli_fini
140c58794deSPawel Jakub Dawidek };
141c58794deSPawel Jakub Dawidek 
142c58794deSPawel Jakub Dawidek 
143c58794deSPawel Jakub Dawidek /*
144c58794deSPawel Jakub Dawidek  * Code paths:
145c58794deSPawel Jakub Dawidek  * BIO_READ:
1465ad4a7c7SPawel Jakub Dawidek  *	g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
147c58794deSPawel Jakub Dawidek  * BIO_WRITE:
148c58794deSPawel Jakub Dawidek  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
149c58794deSPawel Jakub Dawidek  */
150c58794deSPawel Jakub Dawidek 
151c58794deSPawel Jakub Dawidek 
152c58794deSPawel Jakub Dawidek /*
153c58794deSPawel Jakub Dawidek  * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
154c58794deSPawel Jakub Dawidek  * accelerator or something like this.
155c58794deSPawel Jakub Dawidek  * The function updates the SID and rerun the operation.
156c58794deSPawel Jakub Dawidek  */
157eaa3b919SPawel Jakub Dawidek int
158c58794deSPawel Jakub Dawidek g_eli_crypto_rerun(struct cryptop *crp)
159c58794deSPawel Jakub Dawidek {
160c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
161c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
162c58794deSPawel Jakub Dawidek 	struct bio *bp;
163c58794deSPawel Jakub Dawidek 	int error;
164c58794deSPawel Jakub Dawidek 
165c58794deSPawel Jakub Dawidek 	bp = (struct bio *)crp->crp_opaque;
166c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
167c58794deSPawel Jakub Dawidek 	LIST_FOREACH(wr, &sc->sc_workers, w_next) {
168c58794deSPawel Jakub Dawidek 		if (wr->w_number == bp->bio_pflags)
169c58794deSPawel Jakub Dawidek 			break;
170c58794deSPawel Jakub Dawidek 	}
171c58794deSPawel Jakub Dawidek 	KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
17298645006SChristian Brueffer 	G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).",
173c58794deSPawel Jakub Dawidek 	    bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid,
174c58794deSPawel Jakub Dawidek 	    (uintmax_t)crp->crp_sid);
175c58794deSPawel Jakub Dawidek 	wr->w_sid = crp->crp_sid;
176c58794deSPawel Jakub Dawidek 	crp->crp_etype = 0;
177c58794deSPawel Jakub Dawidek 	error = crypto_dispatch(crp);
178c58794deSPawel Jakub Dawidek 	if (error == 0)
179c58794deSPawel Jakub Dawidek 		return (0);
180c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
181c58794deSPawel Jakub Dawidek 	crp->crp_etype = error;
182c58794deSPawel Jakub Dawidek 	return (error);
183c58794deSPawel Jakub Dawidek }
184c58794deSPawel Jakub Dawidek 
185c58794deSPawel Jakub Dawidek /*
186c58794deSPawel Jakub Dawidek  * The function is called afer reading encrypted data from the provider.
187bb30fea6SPawel Jakub Dawidek  *
1885ad4a7c7SPawel Jakub Dawidek  * g_eli_start -> g_eli_crypto_read -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
189c58794deSPawel Jakub Dawidek  */
190eaa3b919SPawel Jakub Dawidek void
191c58794deSPawel Jakub Dawidek g_eli_read_done(struct bio *bp)
192c58794deSPawel Jakub Dawidek {
193c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
194c58794deSPawel Jakub Dawidek 	struct bio *pbp;
195c58794deSPawel Jakub Dawidek 
196c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
197c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
198c58794deSPawel Jakub Dawidek 	if (pbp->bio_error == 0)
199c58794deSPawel Jakub Dawidek 		pbp->bio_error = bp->bio_error;
20090574b0aSMikolaj Golub 	g_destroy_bio(bp);
201eaa3b919SPawel Jakub Dawidek 	/*
202eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
203eaa3b919SPawel Jakub Dawidek 	 */
204eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
205eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
206eaa3b919SPawel Jakub Dawidek 		return;
2075ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
208c58794deSPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
209c58794deSPawel Jakub Dawidek 		G_ELI_LOGREQ(0, pbp, "%s() failed", __func__);
210c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
211eaa3b919SPawel Jakub Dawidek 		if (pbp->bio_driver2 != NULL) {
212eaa3b919SPawel Jakub Dawidek 			free(pbp->bio_driver2, M_ELI);
213eaa3b919SPawel Jakub Dawidek 			pbp->bio_driver2 = NULL;
214eaa3b919SPawel Jakub Dawidek 		}
215c58794deSPawel Jakub Dawidek 		g_io_deliver(pbp, pbp->bio_error);
2165ad4a7c7SPawel Jakub Dawidek 		atomic_subtract_int(&sc->sc_inflight, 1);
217c58794deSPawel Jakub Dawidek 		return;
218c58794deSPawel Jakub Dawidek 	}
219c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
220c58794deSPawel Jakub Dawidek 	bioq_insert_tail(&sc->sc_queue, pbp);
221c58794deSPawel Jakub Dawidek 	mtx_unlock(&sc->sc_queue_mtx);
222c58794deSPawel Jakub Dawidek 	wakeup(sc);
223c58794deSPawel Jakub Dawidek }
224c58794deSPawel Jakub Dawidek 
225c58794deSPawel Jakub Dawidek /*
226c58794deSPawel Jakub Dawidek  * The function is called after we encrypt and write data.
227bb30fea6SPawel Jakub Dawidek  *
228bb30fea6SPawel Jakub Dawidek  * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
229c58794deSPawel Jakub Dawidek  */
230eaa3b919SPawel Jakub Dawidek void
231c58794deSPawel Jakub Dawidek g_eli_write_done(struct bio *bp)
232c58794deSPawel Jakub Dawidek {
2335ad4a7c7SPawel Jakub Dawidek 	struct g_eli_softc *sc;
234c58794deSPawel Jakub Dawidek 	struct bio *pbp;
235c58794deSPawel Jakub Dawidek 
236c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request done.");
237c58794deSPawel Jakub Dawidek 	pbp = bp->bio_parent;
238eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error == 0) {
239eaa3b919SPawel Jakub Dawidek 		if (bp->bio_error != 0)
240c58794deSPawel Jakub Dawidek 			pbp->bio_error = bp->bio_error;
241eaa3b919SPawel Jakub Dawidek 	}
24290574b0aSMikolaj Golub 	g_destroy_bio(bp);
243eaa3b919SPawel Jakub Dawidek 	/*
244eaa3b919SPawel Jakub Dawidek 	 * Do we have all sectors already?
245eaa3b919SPawel Jakub Dawidek 	 */
246eaa3b919SPawel Jakub Dawidek 	pbp->bio_inbed++;
247eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_inbed < pbp->bio_children)
248eaa3b919SPawel Jakub Dawidek 		return;
249c58794deSPawel Jakub Dawidek 	free(pbp->bio_driver2, M_ELI);
250c58794deSPawel Jakub Dawidek 	pbp->bio_driver2 = NULL;
251eaa3b919SPawel Jakub Dawidek 	if (pbp->bio_error != 0) {
25271270ca6SPawel Jakub Dawidek 		G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).",
253c58794deSPawel Jakub Dawidek 		    pbp->bio_error);
254c58794deSPawel Jakub Dawidek 		pbp->bio_completed = 0;
255c58794deSPawel Jakub Dawidek 	}
256c58794deSPawel Jakub Dawidek 	/*
257c58794deSPawel Jakub Dawidek 	 * Write is finished, send it up.
258c58794deSPawel Jakub Dawidek 	 */
259eaa3b919SPawel Jakub Dawidek 	pbp->bio_completed = pbp->bio_length;
2605ad4a7c7SPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
261c58794deSPawel Jakub Dawidek 	g_io_deliver(pbp, pbp->bio_error);
2625ad4a7c7SPawel Jakub Dawidek 	atomic_subtract_int(&sc->sc_inflight, 1);
263c58794deSPawel Jakub Dawidek }
264c58794deSPawel Jakub Dawidek 
265c58794deSPawel Jakub Dawidek /*
266c58794deSPawel Jakub Dawidek  * This function should never be called, but GEOM made as it set ->orphan()
267c58794deSPawel Jakub Dawidek  * method for every geom.
268c58794deSPawel Jakub Dawidek  */
269c58794deSPawel Jakub Dawidek static void
270c58794deSPawel Jakub Dawidek g_eli_orphan_spoil_assert(struct g_consumer *cp)
271c58794deSPawel Jakub Dawidek {
272c58794deSPawel Jakub Dawidek 
273c58794deSPawel Jakub Dawidek 	panic("Function %s() called for %s.", __func__, cp->geom->name);
274c58794deSPawel Jakub Dawidek }
275c58794deSPawel Jakub Dawidek 
276c58794deSPawel Jakub Dawidek static void
277c58794deSPawel Jakub Dawidek g_eli_orphan(struct g_consumer *cp)
278c58794deSPawel Jakub Dawidek {
279c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
280c58794deSPawel Jakub Dawidek 
281c58794deSPawel Jakub Dawidek 	g_topology_assert();
282c58794deSPawel Jakub Dawidek 	sc = cp->geom->softc;
283c58794deSPawel Jakub Dawidek 	if (sc == NULL)
284c58794deSPawel Jakub Dawidek 		return;
2855ad4a7c7SPawel Jakub Dawidek 	g_eli_destroy(sc, TRUE);
286c58794deSPawel Jakub Dawidek }
287c58794deSPawel Jakub Dawidek 
288bb30fea6SPawel Jakub Dawidek /*
289056638c4SPawel Jakub Dawidek  * BIO_READ:
2905ad4a7c7SPawel Jakub Dawidek  *	G_ELI_START -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
291056638c4SPawel Jakub Dawidek  * BIO_WRITE:
292056638c4SPawel Jakub Dawidek  *	G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
293bb30fea6SPawel Jakub Dawidek  */
294c58794deSPawel Jakub Dawidek static void
295c58794deSPawel Jakub Dawidek g_eli_start(struct bio *bp)
296c58794deSPawel Jakub Dawidek {
297c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
298d3a1be90SPawel Jakub Dawidek 	struct g_consumer *cp;
299c58794deSPawel Jakub Dawidek 	struct bio *cbp;
300c58794deSPawel Jakub Dawidek 
301c58794deSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
302c58794deSPawel Jakub Dawidek 	KASSERT(sc != NULL,
303c58794deSPawel Jakub Dawidek 	    ("Provider's error should be set (error=%d)(device=%s).",
304c58794deSPawel Jakub Dawidek 	    bp->bio_to->error, bp->bio_to->name));
305c58794deSPawel Jakub Dawidek 	G_ELI_LOGREQ(2, bp, "Request received.");
306c58794deSPawel Jakub Dawidek 
307c58794deSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
308c58794deSPawel Jakub Dawidek 	case BIO_READ:
309c58794deSPawel Jakub Dawidek 	case BIO_WRITE:
310d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
31142461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
312c58794deSPawel Jakub Dawidek 		break;
313c58794deSPawel Jakub Dawidek 	case BIO_DELETE:
314c58794deSPawel Jakub Dawidek 		/*
315c58794deSPawel Jakub Dawidek 		 * We could eventually support BIO_DELETE request.
316c58794deSPawel Jakub Dawidek 		 * It could be done by overwritting requested sector with
317c58794deSPawel Jakub Dawidek 		 * random data g_eli_overwrites number of times.
318c58794deSPawel Jakub Dawidek 		 */
319c58794deSPawel Jakub Dawidek 	default:
320c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, EOPNOTSUPP);
321c58794deSPawel Jakub Dawidek 		return;
322c58794deSPawel Jakub Dawidek 	}
323c58794deSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
324c58794deSPawel Jakub Dawidek 	if (cbp == NULL) {
325c58794deSPawel Jakub Dawidek 		g_io_deliver(bp, ENOMEM);
326c58794deSPawel Jakub Dawidek 		return;
327c58794deSPawel Jakub Dawidek 	}
3285ad4a7c7SPawel Jakub Dawidek 	bp->bio_driver1 = cbp;
3295ad4a7c7SPawel Jakub Dawidek 	bp->bio_pflags = G_ELI_NEW_BIO;
330d3a1be90SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
331d3a1be90SPawel Jakub Dawidek 	case BIO_READ:
332eaa3b919SPawel Jakub Dawidek 		if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
3335ad4a7c7SPawel Jakub Dawidek 			g_eli_crypto_read(sc, bp, 0);
334d3a1be90SPawel Jakub Dawidek 			break;
335eaa3b919SPawel Jakub Dawidek 		}
336eaa3b919SPawel Jakub Dawidek 		/* FALLTHROUGH */
337d3a1be90SPawel Jakub Dawidek 	case BIO_WRITE:
338c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
339c58794deSPawel Jakub Dawidek 		bioq_insert_tail(&sc->sc_queue, bp);
340c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
341c58794deSPawel Jakub Dawidek 		wakeup(sc);
342d3a1be90SPawel Jakub Dawidek 		break;
343d3a1be90SPawel Jakub Dawidek 	case BIO_GETATTR:
34442461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
345d3a1be90SPawel Jakub Dawidek 		cbp->bio_done = g_std_done;
346d3a1be90SPawel Jakub Dawidek 		cp = LIST_FIRST(&sc->sc_geom->consumer);
347d3a1be90SPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
348cd0d707eSPawel Jakub Dawidek 		G_ELI_LOGREQ(2, cbp, "Sending request.");
349d3a1be90SPawel Jakub Dawidek 		g_io_request(cbp, cp);
350d3a1be90SPawel Jakub Dawidek 		break;
351c58794deSPawel Jakub Dawidek 	}
352c58794deSPawel Jakub Dawidek }
353c58794deSPawel Jakub Dawidek 
3543ac01bc2SPawel Jakub Dawidek static int
3553ac01bc2SPawel Jakub Dawidek g_eli_newsession(struct g_eli_worker *wr)
3563ac01bc2SPawel Jakub Dawidek {
3573ac01bc2SPawel Jakub Dawidek 	struct g_eli_softc *sc;
3583ac01bc2SPawel Jakub Dawidek 	struct cryptoini crie, cria;
3593ac01bc2SPawel Jakub Dawidek 	int error;
3603ac01bc2SPawel Jakub Dawidek 
3613ac01bc2SPawel Jakub Dawidek 	sc = wr->w_softc;
3623ac01bc2SPawel Jakub Dawidek 
3633ac01bc2SPawel Jakub Dawidek 	bzero(&crie, sizeof(crie));
3643ac01bc2SPawel Jakub Dawidek 	crie.cri_alg = sc->sc_ealgo;
3653ac01bc2SPawel Jakub Dawidek 	crie.cri_klen = sc->sc_ekeylen;
3663ac01bc2SPawel Jakub Dawidek 	if (sc->sc_ealgo == CRYPTO_AES_XTS)
3673ac01bc2SPawel Jakub Dawidek 		crie.cri_klen <<= 1;
368ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
369ad0a5236SPawel Jakub Dawidek 		crie.cri_key = g_eli_key_hold(sc, 0,
370ad0a5236SPawel Jakub Dawidek 		    LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize);
371ad0a5236SPawel Jakub Dawidek 	} else {
3721e09ff3dSPawel Jakub Dawidek 		crie.cri_key = sc->sc_ekey;
373ad0a5236SPawel Jakub Dawidek 	}
3743ac01bc2SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
3753ac01bc2SPawel Jakub Dawidek 		bzero(&cria, sizeof(cria));
3763ac01bc2SPawel Jakub Dawidek 		cria.cri_alg = sc->sc_aalgo;
3773ac01bc2SPawel Jakub Dawidek 		cria.cri_klen = sc->sc_akeylen;
3783ac01bc2SPawel Jakub Dawidek 		cria.cri_key = sc->sc_akey;
3793ac01bc2SPawel Jakub Dawidek 		crie.cri_next = &cria;
3803ac01bc2SPawel Jakub Dawidek 	}
3813ac01bc2SPawel Jakub Dawidek 
3823ac01bc2SPawel Jakub Dawidek 	switch (sc->sc_crypto) {
3833ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
3843ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3853ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_SOFTWARE);
3863ac01bc2SPawel Jakub Dawidek 		break;
3873ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
3883ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3893ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
3903ac01bc2SPawel Jakub Dawidek 		break;
3913ac01bc2SPawel Jakub Dawidek 	case G_ELI_CRYPTO_UNKNOWN:
3923ac01bc2SPawel Jakub Dawidek 		error = crypto_newsession(&wr->w_sid, &crie,
3933ac01bc2SPawel Jakub Dawidek 		    CRYPTOCAP_F_HARDWARE);
3943ac01bc2SPawel Jakub Dawidek 		if (error == 0) {
3953ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
3963ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
3973ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_HW;
3983ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
3993ac01bc2SPawel Jakub Dawidek 		} else {
4003ac01bc2SPawel Jakub Dawidek 			error = crypto_newsession(&wr->w_sid, &crie,
4013ac01bc2SPawel Jakub Dawidek 			    CRYPTOCAP_F_SOFTWARE);
4023ac01bc2SPawel Jakub Dawidek 			mtx_lock(&sc->sc_queue_mtx);
4033ac01bc2SPawel Jakub Dawidek 			if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
4043ac01bc2SPawel Jakub Dawidek 				sc->sc_crypto = G_ELI_CRYPTO_SW;
4053ac01bc2SPawel Jakub Dawidek 			mtx_unlock(&sc->sc_queue_mtx);
4063ac01bc2SPawel Jakub Dawidek 		}
4073ac01bc2SPawel Jakub Dawidek 		break;
4083ac01bc2SPawel Jakub Dawidek 	default:
4093ac01bc2SPawel Jakub Dawidek 		panic("%s: invalid condition", __func__);
4103ac01bc2SPawel Jakub Dawidek 	}
4113ac01bc2SPawel Jakub Dawidek 
412ad0a5236SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0)
413ad0a5236SPawel Jakub Dawidek 		g_eli_key_drop(sc, crie.cri_key);
414ad0a5236SPawel Jakub Dawidek 
4153ac01bc2SPawel Jakub Dawidek 	return (error);
4163ac01bc2SPawel Jakub Dawidek }
4173ac01bc2SPawel Jakub Dawidek 
4183ac01bc2SPawel Jakub Dawidek static void
4193ac01bc2SPawel Jakub Dawidek g_eli_freesession(struct g_eli_worker *wr)
4203ac01bc2SPawel Jakub Dawidek {
4213ac01bc2SPawel Jakub Dawidek 
4223ac01bc2SPawel Jakub Dawidek 	crypto_freesession(wr->w_sid);
4233ac01bc2SPawel Jakub Dawidek }
4243ac01bc2SPawel Jakub Dawidek 
4255ad4a7c7SPawel Jakub Dawidek static void
4265ad4a7c7SPawel Jakub Dawidek g_eli_cancel(struct g_eli_softc *sc)
4275ad4a7c7SPawel Jakub Dawidek {
4285ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
4295ad4a7c7SPawel Jakub Dawidek 
4305ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
4315ad4a7c7SPawel Jakub Dawidek 
4325ad4a7c7SPawel Jakub Dawidek 	while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
4335ad4a7c7SPawel Jakub Dawidek 		KASSERT(bp->bio_pflags == G_ELI_NEW_BIO,
4345ad4a7c7SPawel Jakub Dawidek 		    ("Not new bio when canceling (bp=%p).", bp));
4355ad4a7c7SPawel Jakub Dawidek 		g_io_deliver(bp, ENXIO);
4365ad4a7c7SPawel Jakub Dawidek 	}
4375ad4a7c7SPawel Jakub Dawidek }
4385ad4a7c7SPawel Jakub Dawidek 
4395ad4a7c7SPawel Jakub Dawidek static struct bio *
4405ad4a7c7SPawel Jakub Dawidek g_eli_takefirst(struct g_eli_softc *sc)
4415ad4a7c7SPawel Jakub Dawidek {
4425ad4a7c7SPawel Jakub Dawidek 	struct bio *bp;
4435ad4a7c7SPawel Jakub Dawidek 
4445ad4a7c7SPawel Jakub Dawidek 	mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
4455ad4a7c7SPawel Jakub Dawidek 
4465ad4a7c7SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND))
4475ad4a7c7SPawel Jakub Dawidek 		return (bioq_takefirst(&sc->sc_queue));
4485ad4a7c7SPawel Jakub Dawidek 	/*
4495ad4a7c7SPawel Jakub Dawidek 	 * Device suspended, so we skip new I/O requests.
4505ad4a7c7SPawel Jakub Dawidek 	 */
4515ad4a7c7SPawel Jakub Dawidek 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
4525ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags != G_ELI_NEW_BIO)
4535ad4a7c7SPawel Jakub Dawidek 			break;
4545ad4a7c7SPawel Jakub Dawidek 	}
4555ad4a7c7SPawel Jakub Dawidek 	if (bp != NULL)
4565ad4a7c7SPawel Jakub Dawidek 		bioq_remove(&sc->sc_queue, bp);
4575ad4a7c7SPawel Jakub Dawidek 	return (bp);
4585ad4a7c7SPawel Jakub Dawidek }
4595ad4a7c7SPawel Jakub Dawidek 
460c58794deSPawel Jakub Dawidek /*
461c58794deSPawel Jakub Dawidek  * This is the main function for kernel worker thread when we don't have
462c58794deSPawel Jakub Dawidek  * hardware acceleration and we have to do cryptography in software.
463c58794deSPawel Jakub Dawidek  * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
464c58794deSPawel Jakub Dawidek  * threads with crypto work.
465c58794deSPawel Jakub Dawidek  */
466c58794deSPawel Jakub Dawidek static void
467c58794deSPawel Jakub Dawidek g_eli_worker(void *arg)
468c58794deSPawel Jakub Dawidek {
469c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
470c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
471c58794deSPawel Jakub Dawidek 	struct bio *bp;
4723ac01bc2SPawel Jakub Dawidek 	int error;
473c58794deSPawel Jakub Dawidek 
474c58794deSPawel Jakub Dawidek 	wr = arg;
475c58794deSPawel Jakub Dawidek 	sc = wr->w_softc;
476a1ea1a22SPawel Jakub Dawidek #ifdef SMP
477a1ea1a22SPawel Jakub Dawidek 	/* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
4780c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind) {
479a1ea1a22SPawel Jakub Dawidek 		while (!smp_started)
480a1ea1a22SPawel Jakub Dawidek 			tsleep(wr, 0, "geli:smp", hz / 4);
481a1ea1a22SPawel Jakub Dawidek 	}
482a1ea1a22SPawel Jakub Dawidek #endif
483982d11f8SJeff Roberson 	thread_lock(curthread);
48431c4cef7SPawel Jakub Dawidek 	sched_prio(curthread, PUSER);
4850c879bd9SPawel Jakub Dawidek 	if (sc->sc_cpubind)
4860c879bd9SPawel Jakub Dawidek 		sched_bind(curthread, wr->w_number % mp_ncpus);
487982d11f8SJeff Roberson 	thread_unlock(curthread);
488c58794deSPawel Jakub Dawidek 
489c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
490c58794deSPawel Jakub Dawidek 
491c58794deSPawel Jakub Dawidek 	for (;;) {
492c58794deSPawel Jakub Dawidek 		mtx_lock(&sc->sc_queue_mtx);
4935ad4a7c7SPawel Jakub Dawidek again:
4945ad4a7c7SPawel Jakub Dawidek 		bp = g_eli_takefirst(sc);
495c58794deSPawel Jakub Dawidek 		if (bp == NULL) {
496eaa3b919SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
4975ad4a7c7SPawel Jakub Dawidek 				g_eli_cancel(sc);
498c58794deSPawel Jakub Dawidek 				LIST_REMOVE(wr, w_next);
4993ac01bc2SPawel Jakub Dawidek 				g_eli_freesession(wr);
500c58794deSPawel Jakub Dawidek 				free(wr, M_ELI);
501c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Thread %s exiting.",
502c58794deSPawel Jakub Dawidek 				    curthread->td_proc->p_comm);
503c58794deSPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
504c58794deSPawel Jakub Dawidek 				mtx_unlock(&sc->sc_queue_mtx);
5053745c395SJulian Elischer 				kproc_exit(0);
506c58794deSPawel Jakub Dawidek 			}
5075ad4a7c7SPawel Jakub Dawidek 			while (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
5085ad4a7c7SPawel Jakub Dawidek 				if (sc->sc_inflight > 0) {
509038c55adSPawel Jakub Dawidek 					G_ELI_DEBUG(0, "inflight=%d",
510038c55adSPawel Jakub Dawidek 					    sc->sc_inflight);
5115ad4a7c7SPawel Jakub Dawidek 					/*
5125ad4a7c7SPawel Jakub Dawidek 					 * We still have inflight BIOs, so
5135ad4a7c7SPawel Jakub Dawidek 					 * sleep and retry.
5145ad4a7c7SPawel Jakub Dawidek 					 */
5155ad4a7c7SPawel Jakub Dawidek 					msleep(sc, &sc->sc_queue_mtx, PRIBIO,
5165ad4a7c7SPawel Jakub Dawidek 					    "geli:inf", hz / 5);
5175ad4a7c7SPawel Jakub Dawidek 					goto again;
5185ad4a7c7SPawel Jakub Dawidek 				}
5195ad4a7c7SPawel Jakub Dawidek 				/*
5205ad4a7c7SPawel Jakub Dawidek 				 * Suspend requested, mark the worker as
5215ad4a7c7SPawel Jakub Dawidek 				 * suspended and go to sleep.
5225ad4a7c7SPawel Jakub Dawidek 				 */
5233ac01bc2SPawel Jakub Dawidek 				if (wr->w_active) {
5243ac01bc2SPawel Jakub Dawidek 					g_eli_freesession(wr);
5253ac01bc2SPawel Jakub Dawidek 					wr->w_active = FALSE;
5263ac01bc2SPawel Jakub Dawidek 				}
5275ad4a7c7SPawel Jakub Dawidek 				wakeup(&sc->sc_workers);
5285ad4a7c7SPawel Jakub Dawidek 				msleep(sc, &sc->sc_queue_mtx, PRIBIO,
5295ad4a7c7SPawel Jakub Dawidek 				    "geli:suspend", 0);
5303ac01bc2SPawel Jakub Dawidek 				if (!wr->w_active &&
5313ac01bc2SPawel Jakub Dawidek 				    !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) {
5323ac01bc2SPawel Jakub Dawidek 					error = g_eli_newsession(wr);
5333ac01bc2SPawel Jakub Dawidek 					KASSERT(error == 0,
5343ac01bc2SPawel Jakub Dawidek 					    ("g_eli_newsession() failed on resume (error=%d)",
5353ac01bc2SPawel Jakub Dawidek 					    error));
5363ac01bc2SPawel Jakub Dawidek 					wr->w_active = TRUE;
5373ac01bc2SPawel Jakub Dawidek 				}
5385ad4a7c7SPawel Jakub Dawidek 				goto again;
5395ad4a7c7SPawel Jakub Dawidek 			}
54031c4cef7SPawel Jakub Dawidek 			msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
541c58794deSPawel Jakub Dawidek 			continue;
542c58794deSPawel Jakub Dawidek 		}
5435ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO)
5445ad4a7c7SPawel Jakub Dawidek 			atomic_add_int(&sc->sc_inflight, 1);
545c58794deSPawel Jakub Dawidek 		mtx_unlock(&sc->sc_queue_mtx);
5465ad4a7c7SPawel Jakub Dawidek 		if (bp->bio_pflags == G_ELI_NEW_BIO) {
5475ad4a7c7SPawel Jakub Dawidek 			bp->bio_pflags = 0;
5485ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH) {
5495ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
550eaa3b919SPawel Jakub Dawidek 					g_eli_auth_read(sc, bp);
5515ad4a7c7SPawel Jakub Dawidek 				else
5525ad4a7c7SPawel Jakub Dawidek 					g_eli_auth_run(wr, bp);
5535ad4a7c7SPawel Jakub Dawidek 			} else {
5545ad4a7c7SPawel Jakub Dawidek 				if (bp->bio_cmd == BIO_READ)
5555ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_read(sc, bp, 1);
5565ad4a7c7SPawel Jakub Dawidek 				else
5575ad4a7c7SPawel Jakub Dawidek 					g_eli_crypto_run(wr, bp);
5585ad4a7c7SPawel Jakub Dawidek 			}
5595ad4a7c7SPawel Jakub Dawidek 		} else {
5605ad4a7c7SPawel Jakub Dawidek 			if (sc->sc_flags & G_ELI_FLAG_AUTH)
561eaa3b919SPawel Jakub Dawidek 				g_eli_auth_run(wr, bp);
562eaa3b919SPawel Jakub Dawidek 			else
563dddd1d53SPawel Jakub Dawidek 				g_eli_crypto_run(wr, bp);
564c58794deSPawel Jakub Dawidek 		}
565c58794deSPawel Jakub Dawidek 	}
5665ad4a7c7SPawel Jakub Dawidek }
567c58794deSPawel Jakub Dawidek 
568c58794deSPawel Jakub Dawidek /*
569c58794deSPawel Jakub Dawidek  * Here we generate IV. It is unique for every sector.
570c58794deSPawel Jakub Dawidek  */
571eaa3b919SPawel Jakub Dawidek void
572c58794deSPawel Jakub Dawidek g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
573c58794deSPawel Jakub Dawidek     size_t size)
574c58794deSPawel Jakub Dawidek {
5759a5a1d1eSPawel Jakub Dawidek 	uint8_t off[8];
576c58794deSPawel Jakub Dawidek 
577efb46508SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0)
578efb46508SPawel Jakub Dawidek 		bcopy(&offset, off, sizeof(off));
579efb46508SPawel Jakub Dawidek 	else
5802bd4ade6SPawel Jakub Dawidek 		le64enc(off, (uint64_t)offset);
5819a5a1d1eSPawel Jakub Dawidek 
5829a5a1d1eSPawel Jakub Dawidek 	switch (sc->sc_ealgo) {
5839a5a1d1eSPawel Jakub Dawidek 	case CRYPTO_AES_XTS:
5849a5a1d1eSPawel Jakub Dawidek 		bcopy(off, iv, sizeof(off));
5859a5a1d1eSPawel Jakub Dawidek 		bzero(iv + sizeof(off), size - sizeof(off));
5869a5a1d1eSPawel Jakub Dawidek 		break;
5879a5a1d1eSPawel Jakub Dawidek 	default:
5889a5a1d1eSPawel Jakub Dawidek 	    {
5899a5a1d1eSPawel Jakub Dawidek 		u_char hash[SHA256_DIGEST_LENGTH];
5909a5a1d1eSPawel Jakub Dawidek 		SHA256_CTX ctx;
5919a5a1d1eSPawel Jakub Dawidek 
592c58794deSPawel Jakub Dawidek 		/* Copy precalculated SHA256 context for IV-Key. */
593c58794deSPawel Jakub Dawidek 		bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
594efb46508SPawel Jakub Dawidek 		SHA256_Update(&ctx, off, sizeof(off));
595c58794deSPawel Jakub Dawidek 		SHA256_Final(hash, &ctx);
5969a5a1d1eSPawel Jakub Dawidek 		bcopy(hash, iv, MIN(sizeof(hash), size));
5979a5a1d1eSPawel Jakub Dawidek 		break;
5989a5a1d1eSPawel Jakub Dawidek 	    }
5999a5a1d1eSPawel Jakub Dawidek 	}
600c58794deSPawel Jakub Dawidek }
601c58794deSPawel Jakub Dawidek 
602c58794deSPawel Jakub Dawidek int
603c58794deSPawel Jakub Dawidek g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
604c58794deSPawel Jakub Dawidek     struct g_eli_metadata *md)
605c58794deSPawel Jakub Dawidek {
606c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
607c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
608c58794deSPawel Jakub Dawidek 	u_char *buf = NULL;
609c58794deSPawel Jakub Dawidek 	int error;
610c58794deSPawel Jakub Dawidek 
611c58794deSPawel Jakub Dawidek 	g_topology_assert();
612c58794deSPawel Jakub Dawidek 
613c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "eli:taste");
614c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
615c58794deSPawel Jakub Dawidek 	gp->access = g_std_access;
616c58794deSPawel Jakub Dawidek 	/*
617c58794deSPawel Jakub Dawidek 	 * g_eli_read_metadata() is always called from the event thread.
618c58794deSPawel Jakub Dawidek 	 * Our geom is created and destroyed in the same event, so there
619c58794deSPawel Jakub Dawidek 	 * could be no orphan nor spoil event in the meantime.
620c58794deSPawel Jakub Dawidek 	 */
621c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan_spoil_assert;
622c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
623c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
624c58794deSPawel Jakub Dawidek 	error = g_attach(cp, pp);
625c58794deSPawel Jakub Dawidek 	if (error != 0)
626c58794deSPawel Jakub Dawidek 		goto end;
627c58794deSPawel Jakub Dawidek 	error = g_access(cp, 1, 0, 0);
628c58794deSPawel Jakub Dawidek 	if (error != 0)
629c58794deSPawel Jakub Dawidek 		goto end;
630c58794deSPawel Jakub Dawidek 	g_topology_unlock();
631c58794deSPawel Jakub Dawidek 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
632c58794deSPawel Jakub Dawidek 	    &error);
633c58794deSPawel Jakub Dawidek 	g_topology_lock();
6348a4a44b5SMaxim Sobolev 	if (buf == NULL)
635c58794deSPawel Jakub Dawidek 		goto end;
636c58794deSPawel Jakub Dawidek 	eli_metadata_decode(buf, md);
637c58794deSPawel Jakub Dawidek end:
638c58794deSPawel Jakub Dawidek 	if (buf != NULL)
639c58794deSPawel Jakub Dawidek 		g_free(buf);
640c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
641c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
642c58794deSPawel Jakub Dawidek 			g_access(cp, -1, 0, 0);
643c58794deSPawel Jakub Dawidek 		g_detach(cp);
644c58794deSPawel Jakub Dawidek 	}
645c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
646c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
647c58794deSPawel Jakub Dawidek 	return (error);
648c58794deSPawel Jakub Dawidek }
649c58794deSPawel Jakub Dawidek 
650c58794deSPawel Jakub Dawidek /*
651c58794deSPawel Jakub Dawidek  * The function is called when we had last close on provider and user requested
652c58794deSPawel Jakub Dawidek  * to close it when this situation occur.
653c58794deSPawel Jakub Dawidek  */
654c58794deSPawel Jakub Dawidek static void
65519351a14SAlexander Motin g_eli_last_close(void *arg, int flags __unused)
656c58794deSPawel Jakub Dawidek {
657c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
65819351a14SAlexander Motin 	char gpname[64];
659c58794deSPawel Jakub Dawidek 	int error;
660c58794deSPawel Jakub Dawidek 
661c58794deSPawel Jakub Dawidek 	g_topology_assert();
66219351a14SAlexander Motin 	gp = arg;
66319351a14SAlexander Motin 	strlcpy(gpname, gp->name, sizeof(gpname));
66419351a14SAlexander Motin 	error = g_eli_destroy(gp->softc, TRUE);
665c58794deSPawel Jakub Dawidek 	KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
66619351a14SAlexander Motin 	    gpname, error));
66719351a14SAlexander Motin 	G_ELI_DEBUG(0, "Detached %s on last close.", gpname);
668c58794deSPawel Jakub Dawidek }
669c58794deSPawel Jakub Dawidek 
670c58794deSPawel Jakub Dawidek int
671c58794deSPawel Jakub Dawidek g_eli_access(struct g_provider *pp, int dr, int dw, int de)
672c58794deSPawel Jakub Dawidek {
673c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
674c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
675c58794deSPawel Jakub Dawidek 
676c58794deSPawel Jakub Dawidek 	gp = pp->geom;
677c58794deSPawel Jakub Dawidek 	sc = gp->softc;
678c58794deSPawel Jakub Dawidek 
679c58794deSPawel Jakub Dawidek 	if (dw > 0) {
68085059016SPawel Jakub Dawidek 		if (sc->sc_flags & G_ELI_FLAG_RO) {
68185059016SPawel Jakub Dawidek 			/* Deny write attempts. */
68285059016SPawel Jakub Dawidek 			return (EROFS);
68385059016SPawel Jakub Dawidek 		}
684c58794deSPawel Jakub Dawidek 		/* Someone is opening us for write, we need to remember that. */
685c58794deSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_WOPEN;
686c58794deSPawel Jakub Dawidek 		return (0);
687c58794deSPawel Jakub Dawidek 	}
688c58794deSPawel Jakub Dawidek 	/* Is this the last close? */
689c58794deSPawel Jakub Dawidek 	if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
690c58794deSPawel Jakub Dawidek 		return (0);
691c58794deSPawel Jakub Dawidek 
692c58794deSPawel Jakub Dawidek 	/*
693c58794deSPawel Jakub Dawidek 	 * Automatically detach on last close if requested.
694c58794deSPawel Jakub Dawidek 	 */
695c58794deSPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
696c58794deSPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
69719351a14SAlexander Motin 		g_post_event(g_eli_last_close, gp, M_WAITOK, NULL);
698c58794deSPawel Jakub Dawidek 	}
699c58794deSPawel Jakub Dawidek 	return (0);
700c58794deSPawel Jakub Dawidek }
701c58794deSPawel Jakub Dawidek 
702eba8f137SPawel Jakub Dawidek static int
703eba8f137SPawel Jakub Dawidek g_eli_cpu_is_disabled(int cpu)
704eba8f137SPawel Jakub Dawidek {
705eba8f137SPawel Jakub Dawidek #ifdef SMP
70671a19bdcSAttilio Rao 	return (CPU_ISSET(cpu, &hlt_cpus_mask));
707eba8f137SPawel Jakub Dawidek #else
708eba8f137SPawel Jakub Dawidek 	return (0);
709eba8f137SPawel Jakub Dawidek #endif
710eba8f137SPawel Jakub Dawidek }
711eba8f137SPawel Jakub Dawidek 
712c58794deSPawel Jakub Dawidek struct g_geom *
713c58794deSPawel Jakub Dawidek g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
714c58794deSPawel Jakub Dawidek     const struct g_eli_metadata *md, const u_char *mkey, int nkey)
715c58794deSPawel Jakub Dawidek {
716c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
717c58794deSPawel Jakub Dawidek 	struct g_eli_worker *wr;
718c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
719c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
720c58794deSPawel Jakub Dawidek 	struct g_consumer *cp;
721c58794deSPawel Jakub Dawidek 	u_int i, threads;
722c58794deSPawel Jakub Dawidek 	int error;
723c58794deSPawel Jakub Dawidek 
724c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
725c58794deSPawel Jakub Dawidek 
726c58794deSPawel Jakub Dawidek 	gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
727c58794deSPawel Jakub Dawidek 	sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
728c58794deSPawel Jakub Dawidek 	gp->start = g_eli_start;
729c58794deSPawel Jakub Dawidek 	/*
730c58794deSPawel Jakub Dawidek 	 * Spoiling cannot happen actually, because we keep provider open for
73185059016SPawel Jakub Dawidek 	 * writing all the time or provider is read-only.
732c58794deSPawel Jakub Dawidek 	 */
733c58794deSPawel Jakub Dawidek 	gp->spoiled = g_eli_orphan_spoil_assert;
734c58794deSPawel Jakub Dawidek 	gp->orphan = g_eli_orphan;
73585059016SPawel Jakub Dawidek 	gp->dumpconf = g_eli_dumpconf;
736c58794deSPawel Jakub Dawidek 	/*
73785059016SPawel Jakub Dawidek 	 * If detach-on-last-close feature is not enabled and we don't operate
73885059016SPawel Jakub Dawidek 	 * on read-only provider, we can simply use g_std_access().
739c58794deSPawel Jakub Dawidek 	 */
74085059016SPawel Jakub Dawidek 	if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
741c58794deSPawel Jakub Dawidek 		gp->access = g_eli_access;
742c58794deSPawel Jakub Dawidek 	else
743c58794deSPawel Jakub Dawidek 		gp->access = g_std_access;
744c58794deSPawel Jakub Dawidek 
7451f8c92e6SPawel Jakub Dawidek 	sc->sc_version = md->md_version;
7465ad4a7c7SPawel Jakub Dawidek 	sc->sc_inflight = 0;
7473ac01bc2SPawel Jakub Dawidek 	sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN;
748c58794deSPawel Jakub Dawidek 	sc->sc_flags = md->md_flags;
7492bd4ade6SPawel Jakub Dawidek 	/* Backward compatibility. */
7500e236b6cSPawel Jakub Dawidek 	if (md->md_version < G_ELI_VERSION_04)
7512bd4ade6SPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
7520e236b6cSPawel Jakub Dawidek 	if (md->md_version < G_ELI_VERSION_05)
753c6a26d4cSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY;
7540e236b6cSPawel Jakub Dawidek 	if (md->md_version < G_ELI_VERSION_06 &&
7550e236b6cSPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
756ad0a5236SPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_FIRST_KEY;
7570e236b6cSPawel Jakub Dawidek 	}
758457bbc4fSPawel Jakub Dawidek 	if (md->md_version < G_ELI_VERSION_07)
759457bbc4fSPawel Jakub Dawidek 		sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY;
760eaa3b919SPawel Jakub Dawidek 	sc->sc_ealgo = md->md_ealgo;
761c58794deSPawel Jakub Dawidek 	sc->sc_nkey = nkey;
762eaa3b919SPawel Jakub Dawidek 
763eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
764eaa3b919SPawel Jakub Dawidek 		sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
765eaa3b919SPawel Jakub Dawidek 		sc->sc_aalgo = md->md_aalgo;
766eaa3b919SPawel Jakub Dawidek 		sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
767eaa3b919SPawel Jakub Dawidek 
768eaa3b919SPawel Jakub Dawidek 		sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen;
769eaa3b919SPawel Jakub Dawidek 		/*
770eaa3b919SPawel Jakub Dawidek 		 * Some hash functions (like SHA1 and RIPEMD160) generates hash
771eaa3b919SPawel Jakub Dawidek 		 * which length is not multiple of 128 bits, but we want data
772eaa3b919SPawel Jakub Dawidek 		 * length to be multiple of 128, so we can encrypt without
773eaa3b919SPawel Jakub Dawidek 		 * padding. The line below rounds down data length to multiple
774eaa3b919SPawel Jakub Dawidek 		 * of 128 bits.
775eaa3b919SPawel Jakub Dawidek 		 */
776eaa3b919SPawel Jakub Dawidek 		sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
777eaa3b919SPawel Jakub Dawidek 
778eaa3b919SPawel Jakub Dawidek 		sc->sc_bytes_per_sector =
779eaa3b919SPawel Jakub Dawidek 		    (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
780eaa3b919SPawel Jakub Dawidek 		sc->sc_bytes_per_sector *= bpp->sectorsize;
781eaa3b919SPawel Jakub Dawidek 	}
782c58794deSPawel Jakub Dawidek 
783c58794deSPawel Jakub Dawidek 	gp->softc = sc;
784c58794deSPawel Jakub Dawidek 	sc->sc_geom = gp;
785c58794deSPawel Jakub Dawidek 
786c58794deSPawel Jakub Dawidek 	bioq_init(&sc->sc_queue);
787c58794deSPawel Jakub Dawidek 	mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
7881e09ff3dSPawel Jakub Dawidek 	mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF);
789c58794deSPawel Jakub Dawidek 
790c58794deSPawel Jakub Dawidek 	pp = NULL;
791c58794deSPawel Jakub Dawidek 	cp = g_new_consumer(gp);
792c58794deSPawel Jakub Dawidek 	error = g_attach(cp, bpp);
793c58794deSPawel Jakub Dawidek 	if (error != 0) {
794c58794deSPawel Jakub Dawidek 		if (req != NULL) {
795c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot attach to %s (error=%d).",
796c58794deSPawel Jakub Dawidek 			    bpp->name, error);
797c58794deSPawel Jakub Dawidek 		} else {
798c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
799c58794deSPawel Jakub Dawidek 			    bpp->name, error);
800c58794deSPawel Jakub Dawidek 		}
801c58794deSPawel Jakub Dawidek 		goto failed;
802c58794deSPawel Jakub Dawidek 	}
803c58794deSPawel Jakub Dawidek 	/*
804c58794deSPawel Jakub Dawidek 	 * Keep provider open all the time, so we can run critical tasks,
805c58794deSPawel Jakub Dawidek 	 * like Master Keys deletion, without wondering if we can open
806c58794deSPawel Jakub Dawidek 	 * provider or not.
80785059016SPawel Jakub Dawidek 	 * We don't open provider for writing only when user requested read-only
80885059016SPawel Jakub Dawidek 	 * access.
809c58794deSPawel Jakub Dawidek 	 */
81085059016SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_RO)
81185059016SPawel Jakub Dawidek 		error = g_access(cp, 1, 0, 1);
81285059016SPawel Jakub Dawidek 	else
813c58794deSPawel Jakub Dawidek 		error = g_access(cp, 1, 1, 1);
814c58794deSPawel Jakub Dawidek 	if (error != 0) {
815c58794deSPawel Jakub Dawidek 		if (req != NULL) {
816c58794deSPawel Jakub Dawidek 			gctl_error(req, "Cannot access %s (error=%d).",
817c58794deSPawel Jakub Dawidek 			    bpp->name, error);
818c58794deSPawel Jakub Dawidek 		} else {
819c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
820c58794deSPawel Jakub Dawidek 			    bpp->name, error);
821c58794deSPawel Jakub Dawidek 		}
822c58794deSPawel Jakub Dawidek 		goto failed;
823c58794deSPawel Jakub Dawidek 	}
824c58794deSPawel Jakub Dawidek 
825c6a26d4cSPawel Jakub Dawidek 	sc->sc_sectorsize = md->md_sectorsize;
826c6a26d4cSPawel Jakub Dawidek 	sc->sc_mediasize = bpp->mediasize;
827c6a26d4cSPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME))
828c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize -= bpp->sectorsize;
829c6a26d4cSPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_AUTH))
830c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize -= (sc->sc_mediasize % sc->sc_sectorsize);
831c6a26d4cSPawel Jakub Dawidek 	else {
832c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize /= sc->sc_bytes_per_sector;
833c6a26d4cSPawel Jakub Dawidek 		sc->sc_mediasize *= sc->sc_sectorsize;
834c6a26d4cSPawel Jakub Dawidek 	}
835c6a26d4cSPawel Jakub Dawidek 
836c6a26d4cSPawel Jakub Dawidek 	/*
837c6a26d4cSPawel Jakub Dawidek 	 * Remember the keys in our softc structure.
838c6a26d4cSPawel Jakub Dawidek 	 */
839c6a26d4cSPawel Jakub Dawidek 	g_eli_mkey_propagate(sc, mkey);
840c6a26d4cSPawel Jakub Dawidek 	sc->sc_ekeylen = md->md_keylen;
841c6a26d4cSPawel Jakub Dawidek 
842c58794deSPawel Jakub Dawidek 	LIST_INIT(&sc->sc_workers);
843c58794deSPawel Jakub Dawidek 
844c58794deSPawel Jakub Dawidek 	threads = g_eli_threads;
845dd549194SPawel Jakub Dawidek 	if (threads == 0)
846dd549194SPawel Jakub Dawidek 		threads = mp_ncpus;
8470c879bd9SPawel Jakub Dawidek 	sc->sc_cpubind = (mp_ncpus > 1 && threads == mp_ncpus);
848c58794deSPawel Jakub Dawidek 	for (i = 0; i < threads; i++) {
849eba8f137SPawel Jakub Dawidek 		if (g_eli_cpu_is_disabled(i)) {
850eba8f137SPawel Jakub Dawidek 			G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
8511506db21SPawel Jakub Dawidek 			    bpp->name, i);
852eba8f137SPawel Jakub Dawidek 			continue;
853eba8f137SPawel Jakub Dawidek 		}
854c58794deSPawel Jakub Dawidek 		wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
855c58794deSPawel Jakub Dawidek 		wr->w_softc = sc;
856c58794deSPawel Jakub Dawidek 		wr->w_number = i;
8575ad4a7c7SPawel Jakub Dawidek 		wr->w_active = TRUE;
858c58794deSPawel Jakub Dawidek 
8593ac01bc2SPawel Jakub Dawidek 		error = g_eli_newsession(wr);
860c58794deSPawel Jakub Dawidek 		if (error != 0) {
861c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
862c58794deSPawel Jakub Dawidek 			if (req != NULL) {
863c58794deSPawel Jakub Dawidek 				gctl_error(req, "Cannot set up crypto session "
864c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
865c58794deSPawel Jakub Dawidek 			} else {
866c58794deSPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot set up crypto session "
867c58794deSPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
868c58794deSPawel Jakub Dawidek 			}
869c58794deSPawel Jakub Dawidek 			goto failed;
870c58794deSPawel Jakub Dawidek 		}
871c58794deSPawel Jakub Dawidek 
8723745c395SJulian Elischer 		error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
873c58794deSPawel Jakub Dawidek 		    "g_eli[%u] %s", i, bpp->name);
874c58794deSPawel Jakub Dawidek 		if (error != 0) {
8753ac01bc2SPawel Jakub Dawidek 			g_eli_freesession(wr);
876c58794deSPawel Jakub Dawidek 			free(wr, M_ELI);
877c58794deSPawel Jakub Dawidek 			if (req != NULL) {
878dddd1d53SPawel Jakub Dawidek 				gctl_error(req, "Cannot create kernel thread "
879dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
880c58794deSPawel Jakub Dawidek 			} else {
881dddd1d53SPawel Jakub Dawidek 				G_ELI_DEBUG(1, "Cannot create kernel thread "
882dddd1d53SPawel Jakub Dawidek 				    "for %s (error=%d).", bpp->name, error);
883c58794deSPawel Jakub Dawidek 			}
884c58794deSPawel Jakub Dawidek 			goto failed;
885c58794deSPawel Jakub Dawidek 		}
886c58794deSPawel Jakub Dawidek 		LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
887c58794deSPawel Jakub Dawidek 	}
888c58794deSPawel Jakub Dawidek 
889c58794deSPawel Jakub Dawidek 	/*
890c58794deSPawel Jakub Dawidek 	 * Create decrypted provider.
891c58794deSPawel Jakub Dawidek 	 */
892c58794deSPawel Jakub Dawidek 	pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
893c6a26d4cSPawel Jakub Dawidek 	pp->mediasize = sc->sc_mediasize;
894c6a26d4cSPawel Jakub Dawidek 	pp->sectorsize = sc->sc_sectorsize;
895eaa3b919SPawel Jakub Dawidek 
896c58794deSPawel Jakub Dawidek 	g_error_provider(pp, 0);
897c58794deSPawel Jakub Dawidek 
898c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Device %s created.", pp->name);
899eaa3b919SPawel Jakub Dawidek 	G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
900eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
901eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH)
902eaa3b919SPawel Jakub Dawidek 		G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
903c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(0, "    Crypto: %s",
904c58794deSPawel Jakub Dawidek 	    sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
905c58794deSPawel Jakub Dawidek 	return (gp);
906c58794deSPawel Jakub Dawidek failed:
907c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
908c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
909c58794deSPawel Jakub Dawidek 	wakeup(sc);
910c58794deSPawel Jakub Dawidek 	/*
911c58794deSPawel Jakub Dawidek 	 * Wait for kernel threads self destruction.
912c58794deSPawel Jakub Dawidek 	 */
913c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
914c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
915c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
916c58794deSPawel Jakub Dawidek 	}
917c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
918c58794deSPawel Jakub Dawidek 	if (cp->provider != NULL) {
919c58794deSPawel Jakub Dawidek 		if (cp->acr == 1)
920c58794deSPawel Jakub Dawidek 			g_access(cp, -1, -1, -1);
921c58794deSPawel Jakub Dawidek 		g_detach(cp);
922c58794deSPawel Jakub Dawidek 	}
923c58794deSPawel Jakub Dawidek 	g_destroy_consumer(cp);
924c58794deSPawel Jakub Dawidek 	g_destroy_geom(gp);
9251e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
926c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
927c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
928c58794deSPawel Jakub Dawidek 	return (NULL);
929c58794deSPawel Jakub Dawidek }
930c58794deSPawel Jakub Dawidek 
931c58794deSPawel Jakub Dawidek int
932c58794deSPawel Jakub Dawidek g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
933c58794deSPawel Jakub Dawidek {
934c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
935c58794deSPawel Jakub Dawidek 	struct g_provider *pp;
936c58794deSPawel Jakub Dawidek 
937c58794deSPawel Jakub Dawidek 	g_topology_assert();
938c58794deSPawel Jakub Dawidek 
939c58794deSPawel Jakub Dawidek 	if (sc == NULL)
940c58794deSPawel Jakub Dawidek 		return (ENXIO);
941c58794deSPawel Jakub Dawidek 
942c58794deSPawel Jakub Dawidek 	gp = sc->sc_geom;
943c58794deSPawel Jakub Dawidek 	pp = LIST_FIRST(&gp->provider);
944c58794deSPawel Jakub Dawidek 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
945c58794deSPawel Jakub Dawidek 		if (force) {
946c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1, "Device %s is still open, so it "
94798645006SChristian Brueffer 			    "cannot be definitely removed.", pp->name);
94819351a14SAlexander Motin 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
94919351a14SAlexander Motin 			gp->access = g_eli_access;
95019351a14SAlexander Motin 			g_wither_provider(pp, ENXIO);
95119351a14SAlexander Motin 			return (EBUSY);
952c58794deSPawel Jakub Dawidek 		} else {
953c58794deSPawel Jakub Dawidek 			G_ELI_DEBUG(1,
954c58794deSPawel Jakub Dawidek 			    "Device %s is still open (r%dw%de%d).", pp->name,
955c58794deSPawel Jakub Dawidek 			    pp->acr, pp->acw, pp->ace);
956c58794deSPawel Jakub Dawidek 			return (EBUSY);
957c58794deSPawel Jakub Dawidek 		}
958c58794deSPawel Jakub Dawidek 	}
959c58794deSPawel Jakub Dawidek 
960c58794deSPawel Jakub Dawidek 	mtx_lock(&sc->sc_queue_mtx);
961c58794deSPawel Jakub Dawidek 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
962c58794deSPawel Jakub Dawidek 	wakeup(sc);
963c58794deSPawel Jakub Dawidek 	while (!LIST_EMPTY(&sc->sc_workers)) {
964c58794deSPawel Jakub Dawidek 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
965c58794deSPawel Jakub Dawidek 		    "geli:destroy", 0);
966c58794deSPawel Jakub Dawidek 	}
967c58794deSPawel Jakub Dawidek 	mtx_destroy(&sc->sc_queue_mtx);
968c58794deSPawel Jakub Dawidek 	gp->softc = NULL;
9691e09ff3dSPawel Jakub Dawidek 	g_eli_key_destroy(sc);
970c58794deSPawel Jakub Dawidek 	bzero(sc, sizeof(*sc));
971c58794deSPawel Jakub Dawidek 	free(sc, M_ELI);
972c58794deSPawel Jakub Dawidek 
973c58794deSPawel Jakub Dawidek 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
974c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
975c58794deSPawel Jakub Dawidek 	g_wither_geom_close(gp, ENXIO);
976c58794deSPawel Jakub Dawidek 
977c58794deSPawel Jakub Dawidek 	return (0);
978c58794deSPawel Jakub Dawidek }
979c58794deSPawel Jakub Dawidek 
980c58794deSPawel Jakub Dawidek static int
981c58794deSPawel Jakub Dawidek g_eli_destroy_geom(struct gctl_req *req __unused,
982c58794deSPawel Jakub Dawidek     struct g_class *mp __unused, struct g_geom *gp)
983c58794deSPawel Jakub Dawidek {
984c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
985c58794deSPawel Jakub Dawidek 
986c58794deSPawel Jakub Dawidek 	sc = gp->softc;
9875ad4a7c7SPawel Jakub Dawidek 	return (g_eli_destroy(sc, FALSE));
988c58794deSPawel Jakub Dawidek }
989c58794deSPawel Jakub Dawidek 
9909af2131bSPawel Jakub Dawidek static int
9919af2131bSPawel Jakub Dawidek g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
9929af2131bSPawel Jakub Dawidek {
9931e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
9949af2131bSPawel Jakub Dawidek 	char *file, name[64];
9951e189c08SMarcel Moolenaar 	size_t size;
9969af2131bSPawel Jakub Dawidek 	int i;
9979af2131bSPawel Jakub Dawidek 
9989af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
9999af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
10009af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
1001*edaa9008SPawel Jakub Dawidek 		if (keyfile == NULL && i == 0) {
1002*edaa9008SPawel Jakub Dawidek 			/*
1003*edaa9008SPawel Jakub Dawidek 			 * If there is only one keyfile, allow simpler name.
1004*edaa9008SPawel Jakub Dawidek 			 */
1005*edaa9008SPawel Jakub Dawidek 			snprintf(name, sizeof(name), "%s:geli_keyfile", provider);
1006*edaa9008SPawel Jakub Dawidek 			keyfile = preload_search_by_type(name);
1007*edaa9008SPawel Jakub Dawidek 		}
10089af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
10099af2131bSPawel Jakub Dawidek 			return (i);	/* Return number of loaded keyfiles. */
10101e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
10119af2131bSPawel Jakub Dawidek 		if (data == NULL) {
10129af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file data for %s.",
10139af2131bSPawel Jakub Dawidek 			    name);
10149af2131bSPawel Jakub Dawidek 			return (0);
10159af2131bSPawel Jakub Dawidek 		}
10161e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
10171e189c08SMarcel Moolenaar 		if (size == 0) {
10189af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file size for %s.",
10199af2131bSPawel Jakub Dawidek 			    name);
10209af2131bSPawel Jakub Dawidek 			return (0);
10219af2131bSPawel Jakub Dawidek 		}
10229af2131bSPawel Jakub Dawidek 		file = preload_search_info(keyfile, MODINFO_NAME);
10239af2131bSPawel Jakub Dawidek 		if (file == NULL) {
10249af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0, "Cannot find key file name for %s.",
10259af2131bSPawel Jakub Dawidek 			    name);
10269af2131bSPawel Jakub Dawidek 			return (0);
10279af2131bSPawel Jakub Dawidek 		}
10289af2131bSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
10299af2131bSPawel Jakub Dawidek 		    provider, name);
10301e189c08SMarcel Moolenaar 		g_eli_crypto_hmac_update(ctx, data, size);
10319af2131bSPawel Jakub Dawidek 	}
10329af2131bSPawel Jakub Dawidek }
10339af2131bSPawel Jakub Dawidek 
10349af2131bSPawel Jakub Dawidek static void
10359af2131bSPawel Jakub Dawidek g_eli_keyfiles_clear(const char *provider)
10369af2131bSPawel Jakub Dawidek {
10371e189c08SMarcel Moolenaar 	u_char *keyfile, *data;
10389af2131bSPawel Jakub Dawidek 	char name[64];
10391e189c08SMarcel Moolenaar 	size_t size;
10409af2131bSPawel Jakub Dawidek 	int i;
10419af2131bSPawel Jakub Dawidek 
10429af2131bSPawel Jakub Dawidek 	for (i = 0; ; i++) {
10439af2131bSPawel Jakub Dawidek 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
10449af2131bSPawel Jakub Dawidek 		keyfile = preload_search_by_type(name);
10459af2131bSPawel Jakub Dawidek 		if (keyfile == NULL)
10469af2131bSPawel Jakub Dawidek 			return;
10471e189c08SMarcel Moolenaar 		data = preload_fetch_addr(keyfile);
10481e189c08SMarcel Moolenaar 		size = preload_fetch_size(keyfile);
10491e189c08SMarcel Moolenaar 		if (data != NULL && size != 0)
10501e189c08SMarcel Moolenaar 			bzero(data, size);
10519af2131bSPawel Jakub Dawidek 	}
10529af2131bSPawel Jakub Dawidek }
10539af2131bSPawel Jakub Dawidek 
1054c58794deSPawel Jakub Dawidek /*
1055c58794deSPawel Jakub Dawidek  * Tasting is only made on boot.
1056c58794deSPawel Jakub Dawidek  * We detect providers which should be attached before root is mounted.
1057c58794deSPawel Jakub Dawidek  */
1058c58794deSPawel Jakub Dawidek static struct g_geom *
1059c58794deSPawel Jakub Dawidek g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1060c58794deSPawel Jakub Dawidek {
1061c58794deSPawel Jakub Dawidek 	struct g_eli_metadata md;
1062c58794deSPawel Jakub Dawidek 	struct g_geom *gp;
1063c58794deSPawel Jakub Dawidek 	struct hmac_ctx ctx;
1064c58794deSPawel Jakub Dawidek 	char passphrase[256];
1065c58794deSPawel Jakub Dawidek 	u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
10669af2131bSPawel Jakub Dawidek 	u_int i, nkey, nkeyfiles, tries;
1067c58794deSPawel Jakub Dawidek 	int error;
1068c58794deSPawel Jakub Dawidek 
1069c58794deSPawel Jakub Dawidek 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
1070c58794deSPawel Jakub Dawidek 	g_topology_assert();
1071c58794deSPawel Jakub Dawidek 
1072df3aed4fSPawel Jakub Dawidek 	if (root_mounted() || g_eli_tries == 0)
1073c58794deSPawel Jakub Dawidek 		return (NULL);
1074c58794deSPawel Jakub Dawidek 
1075c58794deSPawel Jakub Dawidek 	G_ELI_DEBUG(3, "Tasting %s.", pp->name);
1076c58794deSPawel Jakub Dawidek 
1077c58794deSPawel Jakub Dawidek 	error = g_eli_read_metadata(mp, pp, &md);
1078c58794deSPawel Jakub Dawidek 	if (error != 0)
1079c58794deSPawel Jakub Dawidek 		return (NULL);
1080c58794deSPawel Jakub Dawidek 	gp = NULL;
1081c58794deSPawel Jakub Dawidek 
1082c58794deSPawel Jakub Dawidek 	if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
1083c58794deSPawel Jakub Dawidek 		return (NULL);
1084c58794deSPawel Jakub Dawidek 	if (md.md_version > G_ELI_VERSION) {
1085c58794deSPawel Jakub Dawidek 		printf("geom_eli.ko module is too old to handle %s.\n",
1086c58794deSPawel Jakub Dawidek 		    pp->name);
1087c58794deSPawel Jakub Dawidek 		return (NULL);
1088c58794deSPawel Jakub Dawidek 	}
1089c58794deSPawel Jakub Dawidek 	if (md.md_provsize != pp->mediasize)
1090c58794deSPawel Jakub Dawidek 		return (NULL);
1091c58794deSPawel Jakub Dawidek 	/* Should we attach it on boot? */
1092eaa3b919SPawel Jakub Dawidek 	if (!(md.md_flags & G_ELI_FLAG_BOOT))
1093c58794deSPawel Jakub Dawidek 		return (NULL);
1094c58794deSPawel Jakub Dawidek 	if (md.md_keys == 0x00) {
1095c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
1096c58794deSPawel Jakub Dawidek 		return (NULL);
1097c58794deSPawel Jakub Dawidek 	}
10989af2131bSPawel Jakub Dawidek 	if (md.md_iterations == -1) {
10999af2131bSPawel Jakub Dawidek 		/* If there is no passphrase, we try only once. */
11009af2131bSPawel Jakub Dawidek 		tries = 1;
11019af2131bSPawel Jakub Dawidek 	} else {
11029af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase no more than g_eli_tries times. */
11039af2131bSPawel Jakub Dawidek 		tries = g_eli_tries;
11049af2131bSPawel Jakub Dawidek 	}
11059af2131bSPawel Jakub Dawidek 
1106835c4dd4SColin Percival 	for (i = 0; i <= tries; i++) {
11079af2131bSPawel Jakub Dawidek 		g_eli_crypto_hmac_init(&ctx, NULL, 0);
1108c58794deSPawel Jakub Dawidek 
1109c58794deSPawel Jakub Dawidek 		/*
11109af2131bSPawel Jakub Dawidek 		 * Load all key files.
1111c58794deSPawel Jakub Dawidek 		 */
11129af2131bSPawel Jakub Dawidek 		nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
11139af2131bSPawel Jakub Dawidek 
11149af2131bSPawel Jakub Dawidek 		if (nkeyfiles == 0 && md.md_iterations == -1) {
11159af2131bSPawel Jakub Dawidek 			/*
11169af2131bSPawel Jakub Dawidek 			 * No key files and no passphrase, something is
11179af2131bSPawel Jakub Dawidek 			 * definitely wrong here.
11189af2131bSPawel Jakub Dawidek 			 * geli(8) doesn't allow for such situation, so assume
11199af2131bSPawel Jakub Dawidek 			 * that there was really no passphrase and in that case
11209af2131bSPawel Jakub Dawidek 			 * key files are no properly defined in loader.conf.
11219af2131bSPawel Jakub Dawidek 			 */
11229af2131bSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
11239af2131bSPawel Jakub Dawidek 			    "Found no key files in loader.conf for %s.",
11249af2131bSPawel Jakub Dawidek 			    pp->name);
11259af2131bSPawel Jakub Dawidek 			return (NULL);
11269af2131bSPawel Jakub Dawidek 		}
11279af2131bSPawel Jakub Dawidek 
11289af2131bSPawel Jakub Dawidek 		/* Ask for the passphrase if defined. */
11299af2131bSPawel Jakub Dawidek 		if (md.md_iterations >= 0) {
1130835c4dd4SColin Percival 			/* Try first with cached passphrase. */
1131835c4dd4SColin Percival 			if (i == 0) {
1132835c4dd4SColin Percival 				if (!g_eli_boot_passcache)
1133835c4dd4SColin Percival 					continue;
1134835c4dd4SColin Percival 				memcpy(passphrase, cached_passphrase,
1135835c4dd4SColin Percival 				    sizeof(passphrase));
1136835c4dd4SColin Percival 			} else {
1137c58794deSPawel Jakub Dawidek 				printf("Enter passphrase for %s: ", pp->name);
1138f6ce353eSAndriy Gapon 				cngets(passphrase, sizeof(passphrase),
11399af2131bSPawel Jakub Dawidek 				    g_eli_visible_passphrase);
1140835c4dd4SColin Percival 				memcpy(cached_passphrase, passphrase,
1141835c4dd4SColin Percival 				    sizeof(passphrase));
1142835c4dd4SColin Percival 			}
11439af2131bSPawel Jakub Dawidek 		}
11449af2131bSPawel Jakub Dawidek 
1145c58794deSPawel Jakub Dawidek 		/*
1146c58794deSPawel Jakub Dawidek 		 * Prepare Derived-Key from the user passphrase.
1147c58794deSPawel Jakub Dawidek 		 */
1148c58794deSPawel Jakub Dawidek 		if (md.md_iterations == 0) {
1149c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, md.md_salt,
1150c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt));
1151c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, passphrase,
1152c58794deSPawel Jakub Dawidek 			    strlen(passphrase));
11535527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
11549af2131bSPawel Jakub Dawidek 		} else if (md.md_iterations > 0) {
1155c58794deSPawel Jakub Dawidek 			u_char dkey[G_ELI_USERKEYLEN];
1156c58794deSPawel Jakub Dawidek 
1157c58794deSPawel Jakub Dawidek 			pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
1158c58794deSPawel Jakub Dawidek 			    sizeof(md.md_salt), passphrase, md.md_iterations);
11595527ecd9SPawel Jakub Dawidek 			bzero(passphrase, sizeof(passphrase));
1160c58794deSPawel Jakub Dawidek 			g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
1161c58794deSPawel Jakub Dawidek 			bzero(dkey, sizeof(dkey));
1162c58794deSPawel Jakub Dawidek 		}
11639af2131bSPawel Jakub Dawidek 
1164c58794deSPawel Jakub Dawidek 		g_eli_crypto_hmac_final(&ctx, key, 0);
1165c58794deSPawel Jakub Dawidek 
1166c58794deSPawel Jakub Dawidek 		/*
1167c58794deSPawel Jakub Dawidek 		 * Decrypt Master-Key.
1168c58794deSPawel Jakub Dawidek 		 */
1169c58794deSPawel Jakub Dawidek 		error = g_eli_mkey_decrypt(&md, key, mkey, &nkey);
1170c58794deSPawel Jakub Dawidek 		bzero(key, sizeof(key));
1171c58794deSPawel Jakub Dawidek 		if (error == -1) {
1172835c4dd4SColin Percival 			if (i == tries) {
11739af2131bSPawel Jakub Dawidek 				G_ELI_DEBUG(0,
11749af2131bSPawel Jakub Dawidek 				    "Wrong key for %s. No tries left.",
11759af2131bSPawel Jakub Dawidek 				    pp->name);
11769af2131bSPawel Jakub Dawidek 				g_eli_keyfiles_clear(pp->name);
11779af2131bSPawel Jakub Dawidek 				return (NULL);
1178c58794deSPawel Jakub Dawidek 			}
1179835c4dd4SColin Percival 			if (i > 0) {
1180835c4dd4SColin Percival 				G_ELI_DEBUG(0,
1181835c4dd4SColin Percival 				    "Wrong key for %s. Tries left: %u.",
1182835c4dd4SColin Percival 				    pp->name, tries - i);
1183835c4dd4SColin Percival 			}
1184c58794deSPawel Jakub Dawidek 			/* Try again. */
1185c58794deSPawel Jakub Dawidek 			continue;
1186c58794deSPawel Jakub Dawidek 		} else if (error > 0) {
1187038c55adSPawel Jakub Dawidek 			G_ELI_DEBUG(0,
1188038c55adSPawel Jakub Dawidek 			    "Cannot decrypt Master Key for %s (error=%d).",
1189c58794deSPawel Jakub Dawidek 			    pp->name, error);
11909af2131bSPawel Jakub Dawidek 			g_eli_keyfiles_clear(pp->name);
1191c58794deSPawel Jakub Dawidek 			return (NULL);
1192c58794deSPawel Jakub Dawidek 		}
1193ebd05adaSBrad Davis 		g_eli_keyfiles_clear(pp->name);
1194c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1195c58794deSPawel Jakub Dawidek 		break;
1196c58794deSPawel Jakub Dawidek 	}
1197c58794deSPawel Jakub Dawidek 
1198c58794deSPawel Jakub Dawidek 	/*
1199c58794deSPawel Jakub Dawidek 	 * We have correct key, let's attach provider.
1200c58794deSPawel Jakub Dawidek 	 */
1201c58794deSPawel Jakub Dawidek 	gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1202c58794deSPawel Jakub Dawidek 	bzero(mkey, sizeof(mkey));
1203c58794deSPawel Jakub Dawidek 	bzero(&md, sizeof(md));
1204c58794deSPawel Jakub Dawidek 	if (gp == NULL) {
1205c58794deSPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1206c58794deSPawel Jakub Dawidek 		    G_ELI_SUFFIX);
1207c58794deSPawel Jakub Dawidek 		return (NULL);
1208c58794deSPawel Jakub Dawidek 	}
1209c58794deSPawel Jakub Dawidek 	return (gp);
1210c58794deSPawel Jakub Dawidek }
1211c58794deSPawel Jakub Dawidek 
1212c58794deSPawel Jakub Dawidek static void
1213c58794deSPawel Jakub Dawidek g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1214c58794deSPawel Jakub Dawidek     struct g_consumer *cp, struct g_provider *pp)
1215c58794deSPawel Jakub Dawidek {
1216c58794deSPawel Jakub Dawidek 	struct g_eli_softc *sc;
1217c58794deSPawel Jakub Dawidek 
1218c58794deSPawel Jakub Dawidek 	g_topology_assert();
1219c58794deSPawel Jakub Dawidek 	sc = gp->softc;
1220c58794deSPawel Jakub Dawidek 	if (sc == NULL)
1221c58794deSPawel Jakub Dawidek 		return;
1222c58794deSPawel Jakub Dawidek 	if (pp != NULL || cp != NULL)
1223c58794deSPawel Jakub Dawidek 		return;	/* Nothing here. */
12241e09ff3dSPawel Jakub Dawidek 
1225743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent,
12261e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_total);
1227743437c4SAndrey V. Elsukov 	sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent,
12281e09ff3dSPawel Jakub Dawidek 	    (uintmax_t)sc->sc_ekeys_allocated);
1229ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Flags>", indent);
1230ea35a2ecSPawel Jakub Dawidek 	if (sc->sc_flags == 0)
1231ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "NONE");
1232ea35a2ecSPawel Jakub Dawidek 	else {
1233ea35a2ecSPawel Jakub Dawidek 		int first = 1;
1234ea35a2ecSPawel Jakub Dawidek 
1235ea35a2ecSPawel Jakub Dawidek #define ADD_FLAG(flag, name)	do {					\
1236eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & (flag)) {					\
1237ea35a2ecSPawel Jakub Dawidek 		if (!first)						\
1238ea35a2ecSPawel Jakub Dawidek 			sbuf_printf(sb, ", ");				\
1239ea35a2ecSPawel Jakub Dawidek 		else							\
1240ea35a2ecSPawel Jakub Dawidek 			first = 0;					\
1241ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, name);					\
1242ea35a2ecSPawel Jakub Dawidek 	}								\
1243ea35a2ecSPawel Jakub Dawidek } while (0)
12445ad4a7c7SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND");
1245c6a26d4cSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY");
12462bd4ade6SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1247ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1248ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1249ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1250ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1251eaa3b919SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1252ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1253ea35a2ecSPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
125485059016SPawel Jakub Dawidek 		ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
1255ea35a2ecSPawel Jakub Dawidek #undef  ADD_FLAG
1256ea35a2ecSPawel Jakub Dawidek 	}
1257ea35a2ecSPawel Jakub Dawidek 	sbuf_printf(sb, "</Flags>\n");
1258ea35a2ecSPawel Jakub Dawidek 
1259eaa3b919SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1260ea35a2ecSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1261ea35a2ecSPawel Jakub Dawidek 		    sc->sc_nkey);
1262ea35a2ecSPawel Jakub Dawidek 	}
12631f8c92e6SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version);
1264c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<Crypto>", indent);
1265c58794deSPawel Jakub Dawidek 	switch (sc->sc_crypto) {
1266c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_HW:
1267c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "hardware");
1268c58794deSPawel Jakub Dawidek 		break;
1269c58794deSPawel Jakub Dawidek 	case G_ELI_CRYPTO_SW:
1270c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "software");
1271c58794deSPawel Jakub Dawidek 		break;
1272c58794deSPawel Jakub Dawidek 	default:
1273c58794deSPawel Jakub Dawidek 		sbuf_printf(sb, "UNKNOWN");
1274c58794deSPawel Jakub Dawidek 		break;
1275c58794deSPawel Jakub Dawidek 	}
1276c58794deSPawel Jakub Dawidek 	sbuf_printf(sb, "</Crypto>\n");
1277eaa3b919SPawel Jakub Dawidek 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1278eaa3b919SPawel Jakub Dawidek 		sbuf_printf(sb,
1279eaa3b919SPawel Jakub Dawidek 		    "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1280eaa3b919SPawel Jakub Dawidek 		    indent, g_eli_algo2str(sc->sc_aalgo));
1281eaa3b919SPawel Jakub Dawidek 	}
1282eaa3b919SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1283eaa3b919SPawel Jakub Dawidek 	    sc->sc_ekeylen);
1284038c55adSPawel Jakub Dawidek 	sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n",
1285038c55adSPawel Jakub Dawidek 	    indent, g_eli_algo2str(sc->sc_ealgo));
1286d8d61ef8SPawel Jakub Dawidek 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1287d8d61ef8SPawel Jakub Dawidek 	    (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE");
1288c58794deSPawel Jakub Dawidek }
1289c58794deSPawel Jakub Dawidek 
1290c5d387d0SPawel Jakub Dawidek static void
1291c5d387d0SPawel Jakub Dawidek g_eli_shutdown_pre_sync(void *arg, int howto)
1292c5d387d0SPawel Jakub Dawidek {
1293c5d387d0SPawel Jakub Dawidek 	struct g_class *mp;
1294c5d387d0SPawel Jakub Dawidek 	struct g_geom *gp, *gp2;
1295c5d387d0SPawel Jakub Dawidek 	struct g_provider *pp;
1296c5d387d0SPawel Jakub Dawidek 	struct g_eli_softc *sc;
1297c5d387d0SPawel Jakub Dawidek 	int error;
1298c5d387d0SPawel Jakub Dawidek 
1299c5d387d0SPawel Jakub Dawidek 	mp = arg;
1300c5d387d0SPawel Jakub Dawidek 	DROP_GIANT();
1301c5d387d0SPawel Jakub Dawidek 	g_topology_lock();
1302c5d387d0SPawel Jakub Dawidek 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1303c5d387d0SPawel Jakub Dawidek 		sc = gp->softc;
1304c5d387d0SPawel Jakub Dawidek 		if (sc == NULL)
1305c5d387d0SPawel Jakub Dawidek 			continue;
1306c5d387d0SPawel Jakub Dawidek 		pp = LIST_FIRST(&gp->provider);
1307c5d387d0SPawel Jakub Dawidek 		KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1308c5d387d0SPawel Jakub Dawidek 		if (pp->acr + pp->acw + pp->ace == 0)
13095ad4a7c7SPawel Jakub Dawidek 			error = g_eli_destroy(sc, TRUE);
1310c5d387d0SPawel Jakub Dawidek 		else {
1311c5d387d0SPawel Jakub Dawidek 			sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1312c5d387d0SPawel Jakub Dawidek 			gp->access = g_eli_access;
1313c5d387d0SPawel Jakub Dawidek 		}
1314c5d387d0SPawel Jakub Dawidek 	}
1315c5d387d0SPawel Jakub Dawidek 	g_topology_unlock();
1316c5d387d0SPawel Jakub Dawidek 	PICKUP_GIANT();
1317c5d387d0SPawel Jakub Dawidek }
1318c5d387d0SPawel Jakub Dawidek 
1319c5d387d0SPawel Jakub Dawidek static void
1320c5d387d0SPawel Jakub Dawidek g_eli_init(struct g_class *mp)
1321c5d387d0SPawel Jakub Dawidek {
1322c5d387d0SPawel Jakub Dawidek 
1323c5d387d0SPawel Jakub Dawidek 	g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1324c5d387d0SPawel Jakub Dawidek 	    g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1325c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync == NULL)
1326c5d387d0SPawel Jakub Dawidek 		G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1327c5d387d0SPawel Jakub Dawidek }
1328c5d387d0SPawel Jakub Dawidek 
1329c5d387d0SPawel Jakub Dawidek static void
1330c5d387d0SPawel Jakub Dawidek g_eli_fini(struct g_class *mp)
1331c5d387d0SPawel Jakub Dawidek {
1332c5d387d0SPawel Jakub Dawidek 
1333c5d387d0SPawel Jakub Dawidek 	if (g_eli_pre_sync != NULL)
1334c5d387d0SPawel Jakub Dawidek 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1335c5d387d0SPawel Jakub Dawidek }
1336c5d387d0SPawel Jakub Dawidek 
1337c58794deSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1338f6829a05SYaroslav Tykhiy MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
1339