2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/kthread.h>
44 #include <sys/sched.h>
46 #include <sys/vnode.h>
50 #include <geom/geom.h>
51 #include <geom/geom_dbg.h>
52 #include <geom/eli/g_eli.h>
53 #include <geom/eli/pkcs5v2.h>
58 * 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
60 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
63 MALLOC_DECLARE(M_ELI);
66 * The function is called after we read and decrypt data.
68 * 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
71 g_eli_crypto_read_done(struct cryptop *crp)
73 struct g_eli_softc *sc;
76 if (crp->crp_etype == EAGAIN) {
77 if (g_eli_crypto_rerun(crp) == 0)
80 bp = (struct bio *)crp->crp_opaque;
82 if (crp->crp_etype == 0) {
83 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
84 bp->bio_inbed, bp->bio_children);
85 bp->bio_completed += crp->crp_payload_length;
87 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
88 bp->bio_inbed, bp->bio_children, crp->crp_etype);
89 if (bp->bio_error == 0)
90 bp->bio_error = crp->crp_etype;
92 sc = bp->bio_to->geom->softc;
93 if (sc != NULL && crp->crp_cipher_key != NULL)
94 g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
97 * Do we have all sectors already?
99 if (bp->bio_inbed < bp->bio_children)
101 free(bp->bio_driver2, M_ELI);
102 bp->bio_driver2 = NULL;
103 if (bp->bio_error != 0) {
104 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
106 bp->bio_completed = 0;
109 * Read is finished, send it up.
111 g_io_deliver(bp, bp->bio_error);
113 atomic_subtract_int(&sc->sc_inflight, 1);
118 * The function is called after data encryption.
120 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
123 g_eli_crypto_write_done(struct cryptop *crp)
125 struct g_eli_softc *sc;
127 struct g_consumer *cp;
128 struct bio *bp, *cbp;
130 if (crp->crp_etype == EAGAIN) {
131 if (g_eli_crypto_rerun(crp) == 0)
134 bp = (struct bio *)crp->crp_opaque;
136 if (crp->crp_etype == 0) {
137 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
138 bp->bio_inbed, bp->bio_children);
140 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
141 bp->bio_inbed, bp->bio_children, crp->crp_etype);
142 if (bp->bio_error == 0)
143 bp->bio_error = crp->crp_etype;
145 gp = bp->bio_to->geom;
147 if (crp->crp_cipher_key != NULL)
148 g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
151 * All sectors are already encrypted?
153 if (bp->bio_inbed < bp->bio_children)
156 bp->bio_children = 1;
157 cbp = bp->bio_driver1;
158 bp->bio_driver1 = NULL;
159 if (bp->bio_error != 0) {
160 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
162 free(bp->bio_driver2, M_ELI);
163 bp->bio_driver2 = NULL;
165 g_io_deliver(bp, bp->bio_error);
166 atomic_subtract_int(&sc->sc_inflight, 1);
169 cbp->bio_data = bp->bio_driver2;
170 cbp->bio_done = g_eli_write_done;
171 cp = LIST_FIRST(&gp->consumer);
172 cbp->bio_to = cp->provider;
173 G_ELI_LOGREQ(2, cbp, "Sending request.");
175 * Send encrypted data to the provider.
177 g_io_request(cbp, cp);
182 * The function is called to read encrypted data.
184 * 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
187 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
189 struct g_consumer *cp;
194 * We are not called from the worker thread, so check if
195 * device is suspended.
197 mtx_lock(&sc->sc_queue_mtx);
198 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
200 * If device is suspended, we place the request onto
201 * the queue, so it can be handled after resume.
203 G_ELI_DEBUG(0, "device suspended, move onto queue");
204 bioq_insert_tail(&sc->sc_queue, bp);
205 mtx_unlock(&sc->sc_queue_mtx);
209 atomic_add_int(&sc->sc_inflight, 1);
210 mtx_unlock(&sc->sc_queue_mtx);
213 bp->bio_driver2 = NULL;
214 cbp = bp->bio_driver1;
215 cbp->bio_done = g_eli_read_done;
216 cp = LIST_FIRST(&sc->sc_geom->consumer);
217 cbp->bio_to = cp->provider;
218 G_ELI_LOGREQ(2, cbp, "Sending request.");
220 * Read encrypted data from provider.
222 g_io_request(cbp, cp);
226 * This is the main function responsible for cryptography (ie. communication
227 * with crypto(9) subsystem).
230 * 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
232 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
235 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
237 struct g_eli_softc *sc;
239 u_int i, nsec, secsize;
244 G_ELI_LOGREQ(3, bp, "%s", __func__);
246 bp->bio_pflags = wr->w_number;
248 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
249 nsec = bp->bio_length / secsize;
252 bp->bio_children = nsec;
255 * If we write the data we cannot destroy current bio_data content,
256 * so we need to allocate more memory for encrypted data.
258 if (bp->bio_cmd == BIO_WRITE) {
259 data = malloc(bp->bio_length, M_ELI, M_WAITOK);
260 bp->bio_driver2 = data;
261 bcopy(bp->bio_data, data, bp->bio_length);
265 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
266 crp = crypto_getreq(wr->w_sid, M_WAITOK);
268 crypto_use_buf(crp, data, secsize);
269 crp->crp_opaque = (void *)bp;
271 if (bp->bio_cmd == BIO_WRITE) {
272 crp->crp_op = CRYPTO_OP_ENCRYPT;
273 crp->crp_callback = g_eli_crypto_write_done;
274 } else /* if (bp->bio_cmd == BIO_READ) */ {
275 crp->crp_op = CRYPTO_OP_DECRYPT;
276 crp->crp_callback = g_eli_crypto_read_done;
278 crp->crp_flags = CRYPTO_F_CBIFSYNC;
280 crp->crp_flags |= CRYPTO_F_BATCH;
282 crp->crp_payload_start = 0;
283 crp->crp_payload_length = secsize;
284 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
285 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
286 crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
289 g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
290 sizeof(crp->crp_iv));
292 error = crypto_dispatch(crp);
293 KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",