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/kthread.h>
43 #include <sys/sched.h>
45 #include <sys/vnode.h>
49 #include <geom/geom.h>
50 #include <geom/geom_dbg.h>
51 #include <geom/eli/g_eli.h>
52 #include <geom/eli/pkcs5v2.h>
57 * 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
59 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
63 * Copy data from a (potentially unmapped) bio to a kernelspace buffer.
65 * The buffer must have at least as much room as bp->bio_length.
68 g_eli_bio_copyin(struct bio *bp, void *kaddr)
73 iov[0].iov_base = kaddr;
74 iov[0].iov_len = bp->bio_length;
78 uio.uio_resid = bp->bio_length;
79 uio.uio_segflg = UIO_SYSSPACE;
80 uio.uio_rw = UIO_READ;
81 uiomove_fromphys(bp->bio_ma, bp->bio_ma_offset, bp->bio_length, &uio);
85 * The function is called after we read and decrypt data.
87 * 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
90 g_eli_crypto_read_done(struct cryptop *crp)
92 struct g_eli_softc *sc;
95 if (crp->crp_etype == EAGAIN) {
96 if (g_eli_crypto_rerun(crp) == 0)
99 bp = (struct bio *)crp->crp_opaque;
101 if (crp->crp_etype == 0) {
102 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
103 bp->bio_inbed, bp->bio_children);
104 bp->bio_completed += crp->crp_payload_length;
106 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
107 bp->bio_inbed, bp->bio_children, crp->crp_etype);
108 if (bp->bio_error == 0)
109 bp->bio_error = crp->crp_etype;
111 sc = bp->bio_to->geom->softc;
112 if (sc != NULL && crp->crp_cipher_key != NULL)
113 g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
116 * Do we have all sectors already?
118 if (bp->bio_inbed < bp->bio_children)
121 if (bp->bio_error != 0) {
122 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
124 bp->bio_completed = 0;
127 * Read is finished, send it up.
129 g_io_deliver(bp, bp->bio_error);
131 atomic_subtract_int(&sc->sc_inflight, 1);
136 * The function is called after data encryption.
138 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
141 g_eli_crypto_write_done(struct cryptop *crp)
143 struct g_eli_softc *sc;
145 struct g_consumer *cp;
146 struct bio *bp, *cbp;
148 if (crp->crp_etype == EAGAIN) {
149 if (g_eli_crypto_rerun(crp) == 0)
152 bp = (struct bio *)crp->crp_opaque;
154 if (crp->crp_etype == 0) {
155 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
156 bp->bio_inbed, bp->bio_children);
158 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
159 bp->bio_inbed, bp->bio_children, crp->crp_etype);
160 if (bp->bio_error == 0)
161 bp->bio_error = crp->crp_etype;
163 gp = bp->bio_to->geom;
165 if (crp->crp_cipher_key != NULL)
166 g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
169 * All sectors are already encrypted?
171 if (bp->bio_inbed < bp->bio_children)
174 bp->bio_children = 1;
175 cbp = bp->bio_driver1;
176 bp->bio_driver1 = NULL;
177 if (bp->bio_error != 0) {
178 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
182 g_io_deliver(bp, bp->bio_error);
183 atomic_subtract_int(&sc->sc_inflight, 1);
186 cbp->bio_data = bp->bio_driver2;
188 * Clear BIO_UNMAPPED, which was inherited from where we cloned the bio
189 * in g_eli_start, because we manually set bio_data
191 cbp->bio_flags &= ~BIO_UNMAPPED;
192 cbp->bio_done = g_eli_write_done;
193 cp = LIST_FIRST(&gp->consumer);
194 cbp->bio_to = cp->provider;
195 G_ELI_LOGREQ(2, cbp, "Sending request.");
197 * Send encrypted data to the provider.
199 g_io_request(cbp, cp);
204 * The function is called to read encrypted data.
206 * 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
209 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
211 struct g_consumer *cp;
216 * We are not called from the worker thread, so check if
217 * device is suspended.
219 mtx_lock(&sc->sc_queue_mtx);
220 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
222 * If device is suspended, we place the request onto
223 * the queue, so it can be handled after resume.
225 G_ELI_DEBUG(0, "device suspended, move onto queue");
226 bioq_insert_tail(&sc->sc_queue, bp);
227 mtx_unlock(&sc->sc_queue_mtx);
231 atomic_add_int(&sc->sc_inflight, 1);
232 mtx_unlock(&sc->sc_queue_mtx);
234 G_ELI_SETWORKER(bp->bio_pflags, 0);
235 bp->bio_driver2 = NULL;
236 cbp = bp->bio_driver1;
237 cbp->bio_done = g_eli_read_done;
238 cp = LIST_FIRST(&sc->sc_geom->consumer);
239 cbp->bio_to = cp->provider;
240 G_ELI_LOGREQ(2, cbp, "Sending request.");
242 * Read encrypted data from provider.
244 g_io_request(cbp, cp);
248 * This is the main function responsible for cryptography (ie. communication
249 * with crypto(9) subsystem).
252 * 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
254 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
257 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
259 struct g_eli_softc *sc;
260 struct cryptopq crpq;
263 u_int i, nsec, secsize;
266 int error __diagused, pages_offset;
269 G_ELI_LOGREQ(3, bp, "%s", __func__);
271 G_ELI_SETWORKER(bp->bio_pflags, wr->w_number);
273 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
274 nsec = bp->bio_length / secsize;
277 bp->bio_children = nsec;
280 * If we write the data we cannot destroy current bio_data content,
281 * so we need to allocate more memory for encrypted data.
283 if (bp->bio_cmd == BIO_WRITE) {
284 if (!g_eli_alloc_data(bp, bp->bio_length)) {
285 G_ELI_LOGREQ(0, bp, "Crypto request failed (ENOMEM).");
286 if (bp->bio_driver1 != NULL) {
287 g_destroy_bio(bp->bio_driver1);
288 bp->bio_driver1 = NULL;
290 bp->bio_error = ENOMEM;
291 g_io_deliver(bp, bp->bio_error);
293 atomic_subtract_int(&sc->sc_inflight, 1);
296 data = bp->bio_driver2;
298 * This copy could be eliminated by using crypto's output
299 * buffer, instead of using a single overwriting buffer.
301 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
302 g_eli_bio_copyin(bp, data);
304 bcopy(bp->bio_data, data, bp->bio_length);
306 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
308 pages_offset = bp->bio_ma_offset;
315 batch = atomic_load_int(&g_eli_batch) != 0;
317 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
318 crp = crypto_getreq(wr->w_sid, M_WAITOK);
321 crypto_use_buf(crp, data, secsize);
324 MPASS(pages != NULL);
325 crypto_use_vmpage(crp, pages, secsize, pages_offset);
326 pages_offset += secsize;
327 pages += pages_offset >> PAGE_SHIFT;
328 pages_offset &= PAGE_MASK;
330 crp->crp_opaque = (void *)bp;
331 if (bp->bio_cmd == BIO_WRITE) {
332 crp->crp_op = CRYPTO_OP_ENCRYPT;
333 crp->crp_callback = g_eli_crypto_write_done;
334 } else /* if (bp->bio_cmd == BIO_READ) */ {
335 crp->crp_op = CRYPTO_OP_DECRYPT;
336 crp->crp_callback = g_eli_crypto_read_done;
338 crp->crp_flags = CRYPTO_F_CBIFSYNC;
339 crp->crp_payload_start = 0;
340 crp->crp_payload_length = secsize;
341 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
342 crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
345 if (g_eli_ivlen(sc->sc_ealgo) != 0) {
346 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
347 g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
348 sizeof(crp->crp_iv));
352 TAILQ_INSERT_TAIL(&crpq, crp, crp_next);
354 error = crypto_dispatch(crp);
356 ("crypto_dispatch() failed (error=%d)", error));
361 crypto_dispatch_batch(&crpq, 0);