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