2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/kthread.h>
42 #include <sys/sched.h>
45 #include <sys/vnode.h>
49 #include <geom/geom.h>
50 #include <geom/eli/g_eli.h>
51 #include <geom/eli/pkcs5v2.h>
56 * 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
58 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
61 MALLOC_DECLARE(M_ELI);
64 * The function is called after we read and decrypt data.
66 * 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
69 g_eli_crypto_read_done(struct cryptop *crp)
71 struct g_eli_softc *sc;
74 if (crp->crp_etype == EAGAIN) {
75 if (g_eli_crypto_rerun(crp) == 0)
78 bp = (struct bio *)crp->crp_opaque;
80 if (crp->crp_etype == 0) {
81 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
82 bp->bio_inbed, bp->bio_children);
83 bp->bio_completed += crp->crp_olen;
85 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
86 bp->bio_inbed, bp->bio_children, crp->crp_etype);
87 if (bp->bio_error == 0)
88 bp->bio_error = crp->crp_etype;
90 sc = bp->bio_to->geom->softc;
91 g_eli_key_drop(sc, crp->crp_desc->crd_key);
93 * Do we have all sectors already?
95 if (bp->bio_inbed < bp->bio_children)
97 free(bp->bio_driver2, M_ELI);
98 bp->bio_driver2 = NULL;
99 if (bp->bio_error != 0) {
100 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
102 bp->bio_completed = 0;
105 * Read is finished, send it up.
107 g_io_deliver(bp, bp->bio_error);
108 atomic_subtract_int(&sc->sc_inflight, 1);
113 * The function is called after data encryption.
115 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
118 g_eli_crypto_write_done(struct cryptop *crp)
120 struct g_eli_softc *sc;
122 struct g_consumer *cp;
123 struct bio *bp, *cbp;
125 if (crp->crp_etype == EAGAIN) {
126 if (g_eli_crypto_rerun(crp) == 0)
129 bp = (struct bio *)crp->crp_opaque;
131 if (crp->crp_etype == 0) {
132 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
133 bp->bio_inbed, bp->bio_children);
135 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
136 bp->bio_inbed, bp->bio_children, crp->crp_etype);
137 if (bp->bio_error == 0)
138 bp->bio_error = crp->crp_etype;
140 gp = bp->bio_to->geom;
142 g_eli_key_drop(sc, crp->crp_desc->crd_key);
144 * All sectors are already encrypted?
146 if (bp->bio_inbed < bp->bio_children)
149 bp->bio_children = 1;
150 cbp = bp->bio_driver1;
151 bp->bio_driver1 = NULL;
152 if (bp->bio_error != 0) {
153 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
155 free(bp->bio_driver2, M_ELI);
156 bp->bio_driver2 = NULL;
158 g_io_deliver(bp, bp->bio_error);
159 atomic_subtract_int(&sc->sc_inflight, 1);
162 cbp->bio_data = bp->bio_driver2;
163 cbp->bio_done = g_eli_write_done;
164 cp = LIST_FIRST(&gp->consumer);
165 cbp->bio_to = cp->provider;
166 G_ELI_LOGREQ(2, cbp, "Sending request.");
168 * Send encrypted data to the provider.
170 g_io_request(cbp, cp);
175 * The function is called to read encrypted data.
177 * 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
180 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
182 struct g_consumer *cp;
187 * We are not called from the worker thread, so check if
188 * device is suspended.
190 mtx_lock(&sc->sc_queue_mtx);
191 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
193 * If device is suspended, we place the request onto
194 * the queue, so it can be handled after resume.
196 G_ELI_DEBUG(0, "device suspended, move onto queue");
197 bioq_insert_tail(&sc->sc_queue, bp);
198 mtx_unlock(&sc->sc_queue_mtx);
202 atomic_add_int(&sc->sc_inflight, 1);
203 mtx_unlock(&sc->sc_queue_mtx);
206 bp->bio_driver2 = NULL;
207 cbp = bp->bio_driver1;
208 cbp->bio_done = g_eli_read_done;
209 cp = LIST_FIRST(&sc->sc_geom->consumer);
210 cbp->bio_to = cp->provider;
211 G_ELI_LOGREQ(2, cbp, "Sending request.");
213 * Read encrypted data from provider.
215 g_io_request(cbp, cp);
219 * This is the main function responsible for cryptography (ie. communication
220 * with crypto(9) subsystem).
223 * 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
225 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
228 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
230 struct g_eli_softc *sc;
232 struct cryptodesc *crd;
235 u_int i, nsec, secsize;
241 G_ELI_LOGREQ(3, bp, "%s", __func__);
243 bp->bio_pflags = wr->w_number;
245 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
246 nsec = bp->bio_length / secsize;
249 * Calculate how much memory do we need.
250 * We need separate crypto operation for every single sector.
251 * It is much faster to calculate total amount of needed memory here and
252 * do the allocation once instead of allocating memory in pieces (many,
255 size = sizeof(*crp) * nsec;
256 size += sizeof(*crd) * nsec;
257 size += sizeof(*uio) * nsec;
258 size += sizeof(*iov) * nsec;
260 * If we write the data we cannot destroy current bio_data content,
261 * so we need to allocate more memory for encrypted data.
263 if (bp->bio_cmd == BIO_WRITE)
264 size += bp->bio_length;
265 p = malloc(size, M_ELI, M_WAITOK);
268 bp->bio_children = nsec;
271 if (bp->bio_cmd == BIO_READ)
276 bcopy(bp->bio_data, data, bp->bio_length);
280 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
281 crp = (struct cryptop *)p; p += sizeof(*crp);
282 crd = (struct cryptodesc *)p; p += sizeof(*crd);
283 uio = (struct uio *)p; p += sizeof(*uio);
284 iov = (struct iovec *)p; p += sizeof(*iov);
286 iov->iov_len = secsize;
287 iov->iov_base = data;
292 uio->uio_segflg = UIO_SYSSPACE;
293 uio->uio_resid = secsize;
295 crp->crp_sid = wr->w_sid;
296 crp->crp_ilen = secsize;
297 crp->crp_olen = secsize;
298 crp->crp_opaque = (void *)bp;
299 crp->crp_buf = (void *)uio;
300 if (bp->bio_cmd == BIO_WRITE)
301 crp->crp_callback = g_eli_crypto_write_done;
302 else /* if (bp->bio_cmd == BIO_READ) */
303 crp->crp_callback = g_eli_crypto_read_done;
304 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
306 crp->crp_flags |= CRYPTO_F_BATCH;
310 crd->crd_len = secsize;
311 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
312 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0)
313 crd->crd_flags |= CRD_F_KEY_EXPLICIT;
314 if (bp->bio_cmd == BIO_WRITE)
315 crd->crd_flags |= CRD_F_ENCRYPT;
316 crd->crd_alg = sc->sc_ealgo;
317 crd->crd_key = g_eli_key_hold(sc, dstoff, secsize);
318 crd->crd_klen = sc->sc_ekeylen;
319 if (sc->sc_ealgo == CRYPTO_AES_XTS)
321 g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
322 sizeof(crd->crd_iv));
323 crd->crd_next = NULL;
326 err = crypto_dispatch(crp);
330 if (bp->bio_error == 0)
331 bp->bio_error = error;