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