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/eli/g_eli.h> 52 #include <geom/eli/pkcs5v2.h> 53 54 /* 55 * Code paths: 56 * BIO_READ: 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 58 * BIO_WRITE: 59 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 60 */ 61 62 MALLOC_DECLARE(M_ELI); 63 64 /* 65 * The function is called after we read and decrypt data. 66 * 67 * 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 68 */ 69 static int 70 g_eli_crypto_read_done(struct cryptop *crp) 71 { 72 struct g_eli_softc *sc; 73 struct bio *bp; 74 75 if (crp->crp_etype == EAGAIN) { 76 if (g_eli_crypto_rerun(crp) == 0) 77 return (0); 78 } 79 bp = (struct bio *)crp->crp_opaque; 80 bp->bio_inbed++; 81 if (crp->crp_etype == 0) { 82 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).", 83 bp->bio_inbed, bp->bio_children); 84 bp->bio_completed += crp->crp_olen; 85 } else { 86 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.", 87 bp->bio_inbed, bp->bio_children, crp->crp_etype); 88 if (bp->bio_error == 0) 89 bp->bio_error = crp->crp_etype; 90 } 91 sc = bp->bio_to->geom->softc; 92 if (sc != NULL) 93 g_eli_key_drop(sc, crp->crp_desc->crd_key); 94 /* 95 * Do we have all sectors already? 96 */ 97 if (bp->bio_inbed < bp->bio_children) 98 return (0); 99 free(bp->bio_driver2, M_ELI); 100 bp->bio_driver2 = NULL; 101 if (bp->bio_error != 0) { 102 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).", 103 bp->bio_error); 104 bp->bio_completed = 0; 105 } 106 /* 107 * Read is finished, send it up. 108 */ 109 g_io_deliver(bp, bp->bio_error); 110 if (sc != NULL) 111 atomic_subtract_int(&sc->sc_inflight, 1); 112 return (0); 113 } 114 115 /* 116 * The function is called after data encryption. 117 * 118 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver 119 */ 120 static int 121 g_eli_crypto_write_done(struct cryptop *crp) 122 { 123 struct g_eli_softc *sc; 124 struct g_geom *gp; 125 struct g_consumer *cp; 126 struct bio *bp, *cbp; 127 128 if (crp->crp_etype == EAGAIN) { 129 if (g_eli_crypto_rerun(crp) == 0) 130 return (0); 131 } 132 bp = (struct bio *)crp->crp_opaque; 133 bp->bio_inbed++; 134 if (crp->crp_etype == 0) { 135 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).", 136 bp->bio_inbed, bp->bio_children); 137 } else { 138 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.", 139 bp->bio_inbed, bp->bio_children, crp->crp_etype); 140 if (bp->bio_error == 0) 141 bp->bio_error = crp->crp_etype; 142 } 143 gp = bp->bio_to->geom; 144 sc = gp->softc; 145 g_eli_key_drop(sc, crp->crp_desc->crd_key); 146 /* 147 * All sectors are already encrypted? 148 */ 149 if (bp->bio_inbed < bp->bio_children) 150 return (0); 151 bp->bio_inbed = 0; 152 bp->bio_children = 1; 153 cbp = bp->bio_driver1; 154 bp->bio_driver1 = NULL; 155 if (bp->bio_error != 0) { 156 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).", 157 bp->bio_error); 158 free(bp->bio_driver2, M_ELI); 159 bp->bio_driver2 = NULL; 160 g_destroy_bio(cbp); 161 g_io_deliver(bp, bp->bio_error); 162 atomic_subtract_int(&sc->sc_inflight, 1); 163 return (0); 164 } 165 cbp->bio_data = bp->bio_driver2; 166 cbp->bio_done = g_eli_write_done; 167 cp = LIST_FIRST(&gp->consumer); 168 cbp->bio_to = cp->provider; 169 G_ELI_LOGREQ(2, cbp, "Sending request."); 170 /* 171 * Send encrypted data to the provider. 172 */ 173 g_io_request(cbp, cp); 174 return (0); 175 } 176 177 /* 178 * The function is called to read encrypted data. 179 * 180 * 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 181 */ 182 void 183 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker) 184 { 185 struct g_consumer *cp; 186 struct bio *cbp; 187 188 if (!fromworker) { 189 /* 190 * We are not called from the worker thread, so check if 191 * device is suspended. 192 */ 193 mtx_lock(&sc->sc_queue_mtx); 194 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 195 /* 196 * If device is suspended, we place the request onto 197 * the queue, so it can be handled after resume. 198 */ 199 G_ELI_DEBUG(0, "device suspended, move onto queue"); 200 bioq_insert_tail(&sc->sc_queue, bp); 201 mtx_unlock(&sc->sc_queue_mtx); 202 wakeup(sc); 203 return; 204 } 205 atomic_add_int(&sc->sc_inflight, 1); 206 mtx_unlock(&sc->sc_queue_mtx); 207 } 208 bp->bio_pflags = 0; 209 bp->bio_driver2 = NULL; 210 cbp = bp->bio_driver1; 211 cbp->bio_done = g_eli_read_done; 212 cp = LIST_FIRST(&sc->sc_geom->consumer); 213 cbp->bio_to = cp->provider; 214 G_ELI_LOGREQ(2, cbp, "Sending request."); 215 /* 216 * Read encrypted data from provider. 217 */ 218 g_io_request(cbp, cp); 219 } 220 221 /* 222 * This is the main function responsible for cryptography (ie. communication 223 * with crypto(9) subsystem). 224 * 225 * BIO_READ: 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 * BIO_WRITE: 228 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 229 */ 230 void 231 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp) 232 { 233 struct g_eli_softc *sc; 234 struct cryptop *crp; 235 struct cryptodesc *crd; 236 u_int i, nsec, secsize; 237 off_t dstoff; 238 size_t size; 239 u_char *p, *data; 240 int error; 241 242 G_ELI_LOGREQ(3, bp, "%s", __func__); 243 244 bp->bio_pflags = wr->w_number; 245 sc = wr->w_softc; 246 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize; 247 nsec = bp->bio_length / secsize; 248 249 /* 250 * Calculate how much memory do we need. 251 * We need separate crypto operation for every single sector. 252 * It is much faster to calculate total amount of needed memory here and 253 * do the allocation once instead of allocating memory in pieces (many, 254 * many pieces). 255 */ 256 size = sizeof(*crp) * nsec; 257 size += sizeof(*crd) * nsec; 258 /* 259 * If we write the data we cannot destroy current bio_data content, 260 * so we need to allocate more memory for encrypted data. 261 */ 262 if (bp->bio_cmd == BIO_WRITE) 263 size += bp->bio_length; 264 p = malloc(size, M_ELI, M_WAITOK); 265 266 bp->bio_inbed = 0; 267 bp->bio_children = nsec; 268 bp->bio_driver2 = p; 269 270 if (bp->bio_cmd == BIO_READ) 271 data = bp->bio_data; 272 else { 273 data = p; 274 p += bp->bio_length; 275 bcopy(bp->bio_data, data, bp->bio_length); 276 } 277 278 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) { 279 crp = (struct cryptop *)p; p += sizeof(*crp); 280 crd = (struct cryptodesc *)p; p += sizeof(*crd); 281 282 crp->crp_session = wr->w_sid; 283 crp->crp_ilen = secsize; 284 crp->crp_olen = secsize; 285 crp->crp_opaque = (void *)bp; 286 crp->crp_buf = (void *)data; 287 data += secsize; 288 if (bp->bio_cmd == BIO_WRITE) 289 crp->crp_callback = g_eli_crypto_write_done; 290 else /* if (bp->bio_cmd == BIO_READ) */ 291 crp->crp_callback = g_eli_crypto_read_done; 292 crp->crp_flags = CRYPTO_F_CBIFSYNC; 293 if (g_eli_batch) 294 crp->crp_flags |= CRYPTO_F_BATCH; 295 crp->crp_desc = crd; 296 297 crd->crd_skip = 0; 298 crd->crd_len = secsize; 299 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 300 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) 301 crd->crd_flags |= CRD_F_KEY_EXPLICIT; 302 if (bp->bio_cmd == BIO_WRITE) 303 crd->crd_flags |= CRD_F_ENCRYPT; 304 crd->crd_alg = sc->sc_ealgo; 305 crd->crd_key = g_eli_key_hold(sc, dstoff, secsize); 306 crd->crd_klen = sc->sc_ekeylen; 307 if (sc->sc_ealgo == CRYPTO_AES_XTS) 308 crd->crd_klen <<= 1; 309 g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv, 310 sizeof(crd->crd_iv)); 311 crd->crd_next = NULL; 312 313 crp->crp_etype = 0; 314 error = crypto_dispatch(crp); 315 KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)", 316 error)); 317 } 318 } 319