1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
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>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/bio.h>
40#include <sys/sysctl.h>
41#include <sys/malloc.h>
42#include <sys/kthread.h>
43#include <sys/proc.h>
44#include <sys/sched.h>
45#include <sys/smp.h>
46#include <sys/vnode.h>
47
48#include <vm/uma.h>
49
50#include <geom/geom.h>
51#include <geom/geom_dbg.h>
52#include <geom/eli/g_eli.h>
53#include <geom/eli/pkcs5v2.h>
54
55/*
56 * Code paths:
57 * BIO_READ:
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
59 * BIO_WRITE:
60 *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
61 */
62
63MALLOC_DECLARE(M_ELI);
64
65/*
66 * Copy data from a (potentially unmapped) bio to a kernelspace buffer.
67 *
68 * The buffer must have at least as much room as bp->bio_length.
69 */
70static void
71g_eli_bio_copyin(struct bio *bp, void *kaddr)
72{
73	struct uio uio;
74	struct iovec iov[1];
75
76	iov[0].iov_base = kaddr;
77	iov[0].iov_len = bp->bio_length;
78	uio.uio_iov = iov;
79	uio.uio_iovcnt = 1;
80	uio.uio_offset = 0;
81	uio.uio_resid = bp->bio_length;
82	uio.uio_segflg = UIO_SYSSPACE;
83	uio.uio_rw = UIO_READ;
84	uiomove_fromphys(bp->bio_ma, bp->bio_ma_offset, bp->bio_length, &uio);
85}
86
87/*
88 * The function is called after we read and decrypt data.
89 *
90 * 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
91 */
92static int
93g_eli_crypto_read_done(struct cryptop *crp)
94{
95	struct g_eli_softc *sc;
96	struct bio *bp;
97
98	if (crp->crp_etype == EAGAIN) {
99		if (g_eli_crypto_rerun(crp) == 0)
100			return (0);
101	}
102	bp = (struct bio *)crp->crp_opaque;
103	bp->bio_inbed++;
104	if (crp->crp_etype == 0) {
105		G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
106		    bp->bio_inbed, bp->bio_children);
107		bp->bio_completed += crp->crp_payload_length;
108	} else {
109		G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
110		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
111		if (bp->bio_error == 0)
112			bp->bio_error = crp->crp_etype;
113	}
114	sc = bp->bio_to->geom->softc;
115	if (sc != NULL && crp->crp_cipher_key != NULL)
116		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
117	crypto_freereq(crp);
118	/*
119	 * Do we have all sectors already?
120	 */
121	if (bp->bio_inbed < bp->bio_children)
122		return (0);
123
124	if (bp->bio_error != 0) {
125		G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
126		    bp->bio_error);
127		bp->bio_completed = 0;
128	}
129	/*
130	 * Read is finished, send it up.
131	 */
132	g_io_deliver(bp, bp->bio_error);
133	if (sc != NULL)
134		atomic_subtract_int(&sc->sc_inflight, 1);
135	return (0);
136}
137
138/*
139 * The function is called after data encryption.
140 *
141 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
142 */
143static int
144g_eli_crypto_write_done(struct cryptop *crp)
145{
146	struct g_eli_softc *sc;
147	struct g_geom *gp;
148	struct g_consumer *cp;
149	struct bio *bp, *cbp;
150
151	if (crp->crp_etype == EAGAIN) {
152		if (g_eli_crypto_rerun(crp) == 0)
153			return (0);
154	}
155	bp = (struct bio *)crp->crp_opaque;
156	bp->bio_inbed++;
157	if (crp->crp_etype == 0) {
158		G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
159		    bp->bio_inbed, bp->bio_children);
160	} else {
161		G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
162		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
163		if (bp->bio_error == 0)
164			bp->bio_error = crp->crp_etype;
165	}
166	gp = bp->bio_to->geom;
167	sc = gp->softc;
168	if (crp->crp_cipher_key != NULL)
169		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
170	crypto_freereq(crp);
171	/*
172	 * All sectors are already encrypted?
173	 */
174	if (bp->bio_inbed < bp->bio_children)
175		return (0);
176	bp->bio_inbed = 0;
177	bp->bio_children = 1;
178	cbp = bp->bio_driver1;
179	bp->bio_driver1 = NULL;
180	if (bp->bio_error != 0) {
181		G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
182		    bp->bio_error);
183		free(bp->bio_driver2, M_ELI);
184		bp->bio_driver2 = NULL;
185		g_destroy_bio(cbp);
186		g_io_deliver(bp, bp->bio_error);
187		atomic_subtract_int(&sc->sc_inflight, 1);
188		return (0);
189	}
190	cbp->bio_data = bp->bio_driver2;
191	/*
192	 * Clear BIO_UNMAPPED, which was inherited from where we cloned the bio
193	 * in g_eli_start, because we manually set bio_data
194	 */
195	cbp->bio_flags &= ~BIO_UNMAPPED;
196	cbp->bio_done = g_eli_write_done;
197	cp = LIST_FIRST(&gp->consumer);
198	cbp->bio_to = cp->provider;
199	G_ELI_LOGREQ(2, cbp, "Sending request.");
200	/*
201	 * Send encrypted data to the provider.
202	 */
203	g_io_request(cbp, cp);
204	return (0);
205}
206
207/*
208 * The function is called to read encrypted data.
209 *
210 * 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
211 */
212void
213g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
214{
215	struct g_consumer *cp;
216	struct bio *cbp;
217
218	if (!fromworker) {
219		/*
220		 * We are not called from the worker thread, so check if
221		 * device is suspended.
222		 */
223		mtx_lock(&sc->sc_queue_mtx);
224		if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
225			/*
226			 * If device is suspended, we place the request onto
227			 * the queue, so it can be handled after resume.
228			 */
229			G_ELI_DEBUG(0, "device suspended, move onto queue");
230			bioq_insert_tail(&sc->sc_queue, bp);
231			mtx_unlock(&sc->sc_queue_mtx);
232			wakeup(sc);
233			return;
234		}
235		atomic_add_int(&sc->sc_inflight, 1);
236		mtx_unlock(&sc->sc_queue_mtx);
237	}
238	bp->bio_pflags = 0;
239	bp->bio_driver2 = NULL;
240	cbp = bp->bio_driver1;
241	cbp->bio_done = g_eli_read_done;
242	cp = LIST_FIRST(&sc->sc_geom->consumer);
243	cbp->bio_to = cp->provider;
244	G_ELI_LOGREQ(2, cbp, "Sending request.");
245	/*
246	 * Read encrypted data from provider.
247	 */
248	g_io_request(cbp, cp);
249}
250
251/*
252 * This is the main function responsible for cryptography (ie. communication
253 * with crypto(9) subsystem).
254 *
255 * BIO_READ:
256 *	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
257 * BIO_WRITE:
258 *	g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
259 */
260void
261g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
262{
263	struct g_eli_softc *sc;
264	struct cryptop *crp;
265	vm_page_t *pages;
266	u_int i, nsec, secsize;
267	off_t dstoff;
268	u_char *data = NULL;
269	int error;
270	int pages_offset;
271
272	G_ELI_LOGREQ(3, bp, "%s", __func__);
273
274	bp->bio_pflags = wr->w_number;
275	sc = wr->w_softc;
276	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
277	nsec = bp->bio_length / secsize;
278
279	bp->bio_inbed = 0;
280	bp->bio_children = nsec;
281
282	/*
283	 * If we write the data we cannot destroy current bio_data content,
284	 * so we need to allocate more memory for encrypted data.
285	 */
286	if (bp->bio_cmd == BIO_WRITE) {
287		data = malloc(bp->bio_length, M_ELI, M_WAITOK);
288		bp->bio_driver2 = data;
289		/*
290		 * This copy could be eliminated by using crypto's output
291		 * buffer, instead of using a single overwriting buffer.
292		 */
293		if ((bp->bio_flags & BIO_UNMAPPED) != 0)
294			g_eli_bio_copyin(bp, data);
295		else
296			bcopy(bp->bio_data, data, bp->bio_length);
297	} else {
298		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
299			pages = bp->bio_ma;
300			pages_offset = bp->bio_ma_offset;
301		} else {
302			data = bp->bio_data;
303		}
304	}
305
306	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
307		crp = crypto_getreq(wr->w_sid, M_WAITOK);
308
309		if (data) {
310			crypto_use_buf(crp, data, secsize);
311			data += secsize;
312		} else {
313			MPASS(pages != NULL);
314			crypto_use_vmpage(crp, pages, secsize, pages_offset);
315			pages_offset += secsize;
316			pages += pages_offset >> PAGE_SHIFT;
317			pages_offset &= PAGE_MASK;
318		}
319		crp->crp_opaque = (void *)bp;
320		if (bp->bio_cmd == BIO_WRITE) {
321			crp->crp_op = CRYPTO_OP_ENCRYPT;
322			crp->crp_callback = g_eli_crypto_write_done;
323		} else /* if (bp->bio_cmd == BIO_READ) */ {
324			crp->crp_op = CRYPTO_OP_DECRYPT;
325			crp->crp_callback = g_eli_crypto_read_done;
326		}
327		crp->crp_flags = CRYPTO_F_CBIFSYNC;
328		if (g_eli_batch)
329			crp->crp_flags |= CRYPTO_F_BATCH;
330
331		crp->crp_payload_start = 0;
332		crp->crp_payload_length = secsize;
333		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
334			crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
335			    secsize);
336		}
337		if (g_eli_ivlen(sc->sc_ealgo) != 0) {
338			crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
339			g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
340			    sizeof(crp->crp_iv));
341		}
342
343		error = crypto_dispatch(crp);
344		KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",
345		    error));
346	}
347}
348