]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2003 Christophe Saout <christophe@saout.de> | |
3 | * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> | |
3f1e9070 | 4 | * Copyright (C) 2006-2008 Red Hat, Inc. All rights reserved. |
1da177e4 LT |
5 | * |
6 | * This file is released under the GPL. | |
7 | */ | |
8 | ||
43d69034 | 9 | #include <linux/completion.h> |
d1806f6a | 10 | #include <linux/err.h> |
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/init.h> | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/bio.h> | |
15 | #include <linux/blkdev.h> | |
16 | #include <linux/mempool.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/crypto.h> | |
19 | #include <linux/workqueue.h> | |
3fcfab16 | 20 | #include <linux/backing-dev.h> |
1da177e4 | 21 | #include <asm/atomic.h> |
378f058c | 22 | #include <linux/scatterlist.h> |
1da177e4 | 23 | #include <asm/page.h> |
48527fa7 | 24 | #include <asm/unaligned.h> |
1da177e4 | 25 | |
586e80e6 | 26 | #include <linux/device-mapper.h> |
1da177e4 | 27 | |
72d94861 | 28 | #define DM_MSG_PREFIX "crypt" |
e48d4bbf | 29 | #define MESG_STR(x) x, sizeof(x) |
1da177e4 | 30 | |
1da177e4 LT |
31 | /* |
32 | * context holding the current state of a multi-part conversion | |
33 | */ | |
34 | struct convert_context { | |
43d69034 | 35 | struct completion restart; |
1da177e4 LT |
36 | struct bio *bio_in; |
37 | struct bio *bio_out; | |
38 | unsigned int offset_in; | |
39 | unsigned int offset_out; | |
40 | unsigned int idx_in; | |
41 | unsigned int idx_out; | |
42 | sector_t sector; | |
43d69034 | 43 | atomic_t pending; |
1da177e4 LT |
44 | }; |
45 | ||
53017030 MB |
46 | /* |
47 | * per bio private data | |
48 | */ | |
49 | struct dm_crypt_io { | |
50 | struct dm_target *target; | |
51 | struct bio *base_bio; | |
52 | struct work_struct work; | |
53 | ||
54 | struct convert_context ctx; | |
55 | ||
56 | atomic_t pending; | |
57 | int error; | |
0c395b0f | 58 | sector_t sector; |
393b47ef | 59 | struct dm_crypt_io *base_io; |
53017030 MB |
60 | }; |
61 | ||
01482b76 | 62 | struct dm_crypt_request { |
b2174eeb | 63 | struct convert_context *ctx; |
01482b76 MB |
64 | struct scatterlist sg_in; |
65 | struct scatterlist sg_out; | |
66 | }; | |
67 | ||
1da177e4 LT |
68 | struct crypt_config; |
69 | ||
70 | struct crypt_iv_operations { | |
71 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, | |
d469f841 | 72 | const char *opts); |
1da177e4 | 73 | void (*dtr)(struct crypt_config *cc); |
b95bf2d3 | 74 | int (*init)(struct crypt_config *cc); |
1da177e4 LT |
75 | int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector); |
76 | }; | |
77 | ||
60473592 MB |
78 | struct iv_essiv_private { |
79 | struct crypto_cipher *tfm; | |
b95bf2d3 MB |
80 | struct crypto_hash *hash_tfm; |
81 | u8 *salt; | |
60473592 MB |
82 | }; |
83 | ||
84 | struct iv_benbi_private { | |
85 | int shift; | |
86 | }; | |
87 | ||
1da177e4 LT |
88 | /* |
89 | * Crypt: maps a linear range of a block device | |
90 | * and encrypts / decrypts at the same time. | |
91 | */ | |
e48d4bbf | 92 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID }; |
1da177e4 LT |
93 | struct crypt_config { |
94 | struct dm_dev *dev; | |
95 | sector_t start; | |
96 | ||
97 | /* | |
ddd42edf MB |
98 | * pool for per bio private data, crypto requests and |
99 | * encryption requeusts/buffer pages | |
1da177e4 LT |
100 | */ |
101 | mempool_t *io_pool; | |
ddd42edf | 102 | mempool_t *req_pool; |
1da177e4 | 103 | mempool_t *page_pool; |
6a24c718 | 104 | struct bio_set *bs; |
1da177e4 | 105 | |
cabf08e4 MB |
106 | struct workqueue_struct *io_queue; |
107 | struct workqueue_struct *crypt_queue; | |
3f1e9070 | 108 | |
1da177e4 LT |
109 | /* |
110 | * crypto related data | |
111 | */ | |
112 | struct crypt_iv_operations *iv_gen_ops; | |
113 | char *iv_mode; | |
79066ad3 | 114 | union { |
60473592 MB |
115 | struct iv_essiv_private essiv; |
116 | struct iv_benbi_private benbi; | |
79066ad3 | 117 | } iv_gen_private; |
1da177e4 LT |
118 | sector_t iv_offset; |
119 | unsigned int iv_size; | |
120 | ||
ddd42edf MB |
121 | /* |
122 | * Layout of each crypto request: | |
123 | * | |
124 | * struct ablkcipher_request | |
125 | * context | |
126 | * padding | |
127 | * struct dm_crypt_request | |
128 | * padding | |
129 | * IV | |
130 | * | |
131 | * The padding is added so that dm_crypt_request and the IV are | |
132 | * correctly aligned. | |
133 | */ | |
134 | unsigned int dmreq_start; | |
135 | struct ablkcipher_request *req; | |
136 | ||
d1806f6a HX |
137 | char cipher[CRYPTO_MAX_ALG_NAME]; |
138 | char chainmode[CRYPTO_MAX_ALG_NAME]; | |
3a7f6c99 | 139 | struct crypto_ablkcipher *tfm; |
e48d4bbf | 140 | unsigned long flags; |
1da177e4 LT |
141 | unsigned int key_size; |
142 | u8 key[0]; | |
143 | }; | |
144 | ||
6a24c718 | 145 | #define MIN_IOS 16 |
1da177e4 LT |
146 | #define MIN_POOL_PAGES 32 |
147 | #define MIN_BIO_PAGES 8 | |
148 | ||
e18b890b | 149 | static struct kmem_cache *_crypt_io_pool; |
1da177e4 | 150 | |
028867ac | 151 | static void clone_init(struct dm_crypt_io *, struct bio *); |
395b167c | 152 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
027581f3 | 153 | |
1da177e4 LT |
154 | /* |
155 | * Different IV generation algorithms: | |
156 | * | |
3c164bd8 | 157 | * plain: the initial vector is the 32-bit little-endian version of the sector |
3a4fa0a2 | 158 | * number, padded with zeros if necessary. |
1da177e4 | 159 | * |
3c164bd8 RS |
160 | * essiv: "encrypted sector|salt initial vector", the sector number is |
161 | * encrypted with the bulk cipher using a salt as key. The salt | |
162 | * should be derived from the bulk cipher's key via hashing. | |
1da177e4 | 163 | * |
48527fa7 RS |
164 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
165 | * (needed for LRW-32-AES and possible other narrow block modes) | |
166 | * | |
46b47730 LN |
167 | * null: the initial vector is always zero. Provides compatibility with |
168 | * obsolete loop_fish2 devices. Do not use for new devices. | |
169 | * | |
1da177e4 LT |
170 | * plumb: unimplemented, see: |
171 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 | |
172 | */ | |
173 | ||
174 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector) | |
175 | { | |
176 | memset(iv, 0, cc->iv_size); | |
177 | *(u32 *)iv = cpu_to_le32(sector & 0xffffffff); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
b95bf2d3 MB |
182 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
183 | static int crypt_iv_essiv_init(struct crypt_config *cc) | |
184 | { | |
185 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; | |
186 | struct hash_desc desc; | |
187 | struct scatterlist sg; | |
188 | int err; | |
189 | ||
190 | sg_init_one(&sg, cc->key, cc->key_size); | |
191 | desc.tfm = essiv->hash_tfm; | |
192 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; | |
193 | ||
194 | err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt); | |
195 | if (err) | |
196 | return err; | |
197 | ||
198 | return crypto_cipher_setkey(essiv->tfm, essiv->salt, | |
199 | crypto_hash_digestsize(essiv->hash_tfm)); | |
200 | } | |
201 | ||
60473592 MB |
202 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
203 | { | |
204 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; | |
205 | ||
206 | crypto_free_cipher(essiv->tfm); | |
207 | essiv->tfm = NULL; | |
b95bf2d3 MB |
208 | |
209 | crypto_free_hash(essiv->hash_tfm); | |
210 | essiv->hash_tfm = NULL; | |
211 | ||
212 | kzfree(essiv->salt); | |
213 | essiv->salt = NULL; | |
60473592 MB |
214 | } |
215 | ||
1da177e4 | 216 | static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, |
d469f841 | 217 | const char *opts) |
1da177e4 | 218 | { |
5861f1be MB |
219 | struct crypto_cipher *essiv_tfm = NULL; |
220 | struct crypto_hash *hash_tfm = NULL; | |
5861f1be | 221 | u8 *salt = NULL; |
d1806f6a | 222 | int err; |
1da177e4 | 223 | |
5861f1be | 224 | if (!opts) { |
72d94861 | 225 | ti->error = "Digest algorithm missing for ESSIV mode"; |
1da177e4 LT |
226 | return -EINVAL; |
227 | } | |
228 | ||
b95bf2d3 | 229 | /* Allocate hash algorithm */ |
35058687 HX |
230 | hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC); |
231 | if (IS_ERR(hash_tfm)) { | |
72d94861 | 232 | ti->error = "Error initializing ESSIV hash"; |
5861f1be MB |
233 | err = PTR_ERR(hash_tfm); |
234 | goto bad; | |
1da177e4 LT |
235 | } |
236 | ||
b95bf2d3 | 237 | salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL); |
5861f1be | 238 | if (!salt) { |
72d94861 | 239 | ti->error = "Error kmallocing salt storage in ESSIV"; |
5861f1be MB |
240 | err = -ENOMEM; |
241 | goto bad; | |
1da177e4 LT |
242 | } |
243 | ||
b95bf2d3 | 244 | /* Allocate essiv_tfm */ |
d1806f6a HX |
245 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
246 | if (IS_ERR(essiv_tfm)) { | |
72d94861 | 247 | ti->error = "Error allocating crypto tfm for ESSIV"; |
5861f1be MB |
248 | err = PTR_ERR(essiv_tfm); |
249 | goto bad; | |
1da177e4 | 250 | } |
d1806f6a | 251 | if (crypto_cipher_blocksize(essiv_tfm) != |
3a7f6c99 | 252 | crypto_ablkcipher_ivsize(cc->tfm)) { |
72d94861 | 253 | ti->error = "Block size of ESSIV cipher does " |
d469f841 | 254 | "not match IV size of block cipher"; |
5861f1be MB |
255 | err = -EINVAL; |
256 | goto bad; | |
1da177e4 | 257 | } |
1da177e4 | 258 | |
b95bf2d3 | 259 | cc->iv_gen_private.essiv.salt = salt; |
60473592 | 260 | cc->iv_gen_private.essiv.tfm = essiv_tfm; |
b95bf2d3 MB |
261 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
262 | ||
1da177e4 | 263 | return 0; |
5861f1be MB |
264 | |
265 | bad: | |
266 | if (essiv_tfm && !IS_ERR(essiv_tfm)) | |
267 | crypto_free_cipher(essiv_tfm); | |
268 | if (hash_tfm && !IS_ERR(hash_tfm)) | |
269 | crypto_free_hash(hash_tfm); | |
b95bf2d3 | 270 | kfree(salt); |
5861f1be | 271 | return err; |
1da177e4 LT |
272 | } |
273 | ||
1da177e4 LT |
274 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
275 | { | |
1da177e4 LT |
276 | memset(iv, 0, cc->iv_size); |
277 | *(u64 *)iv = cpu_to_le64(sector); | |
60473592 | 278 | crypto_cipher_encrypt_one(cc->iv_gen_private.essiv.tfm, iv, iv); |
1da177e4 LT |
279 | return 0; |
280 | } | |
281 | ||
48527fa7 RS |
282 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
283 | const char *opts) | |
284 | { | |
3a7f6c99 | 285 | unsigned bs = crypto_ablkcipher_blocksize(cc->tfm); |
f0d1b0b3 | 286 | int log = ilog2(bs); |
48527fa7 RS |
287 | |
288 | /* we need to calculate how far we must shift the sector count | |
289 | * to get the cipher block count, we use this shift in _gen */ | |
290 | ||
291 | if (1 << log != bs) { | |
292 | ti->error = "cypher blocksize is not a power of 2"; | |
293 | return -EINVAL; | |
294 | } | |
295 | ||
296 | if (log > 9) { | |
297 | ti->error = "cypher blocksize is > 512"; | |
298 | return -EINVAL; | |
299 | } | |
300 | ||
60473592 | 301 | cc->iv_gen_private.benbi.shift = 9 - log; |
48527fa7 RS |
302 | |
303 | return 0; | |
304 | } | |
305 | ||
306 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) | |
307 | { | |
48527fa7 RS |
308 | } |
309 | ||
310 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector) | |
311 | { | |
79066ad3 HX |
312 | __be64 val; |
313 | ||
48527fa7 | 314 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
79066ad3 | 315 | |
60473592 | 316 | val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi.shift) + 1); |
79066ad3 | 317 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
48527fa7 | 318 | |
1da177e4 LT |
319 | return 0; |
320 | } | |
321 | ||
46b47730 LN |
322 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
323 | { | |
324 | memset(iv, 0, cc->iv_size); | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
1da177e4 LT |
329 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
330 | .generator = crypt_iv_plain_gen | |
331 | }; | |
332 | ||
333 | static struct crypt_iv_operations crypt_iv_essiv_ops = { | |
334 | .ctr = crypt_iv_essiv_ctr, | |
335 | .dtr = crypt_iv_essiv_dtr, | |
b95bf2d3 | 336 | .init = crypt_iv_essiv_init, |
1da177e4 LT |
337 | .generator = crypt_iv_essiv_gen |
338 | }; | |
339 | ||
48527fa7 RS |
340 | static struct crypt_iv_operations crypt_iv_benbi_ops = { |
341 | .ctr = crypt_iv_benbi_ctr, | |
342 | .dtr = crypt_iv_benbi_dtr, | |
343 | .generator = crypt_iv_benbi_gen | |
344 | }; | |
1da177e4 | 345 | |
46b47730 LN |
346 | static struct crypt_iv_operations crypt_iv_null_ops = { |
347 | .generator = crypt_iv_null_gen | |
348 | }; | |
349 | ||
d469f841 MB |
350 | static void crypt_convert_init(struct crypt_config *cc, |
351 | struct convert_context *ctx, | |
352 | struct bio *bio_out, struct bio *bio_in, | |
fcd369da | 353 | sector_t sector) |
1da177e4 LT |
354 | { |
355 | ctx->bio_in = bio_in; | |
356 | ctx->bio_out = bio_out; | |
357 | ctx->offset_in = 0; | |
358 | ctx->offset_out = 0; | |
359 | ctx->idx_in = bio_in ? bio_in->bi_idx : 0; | |
360 | ctx->idx_out = bio_out ? bio_out->bi_idx : 0; | |
361 | ctx->sector = sector + cc->iv_offset; | |
43d69034 | 362 | init_completion(&ctx->restart); |
1da177e4 LT |
363 | } |
364 | ||
b2174eeb HY |
365 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
366 | struct ablkcipher_request *req) | |
367 | { | |
368 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); | |
369 | } | |
370 | ||
371 | static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc, | |
372 | struct dm_crypt_request *dmreq) | |
373 | { | |
374 | return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start); | |
375 | } | |
376 | ||
01482b76 | 377 | static int crypt_convert_block(struct crypt_config *cc, |
3a7f6c99 MB |
378 | struct convert_context *ctx, |
379 | struct ablkcipher_request *req) | |
01482b76 MB |
380 | { |
381 | struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in); | |
382 | struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out); | |
3a7f6c99 MB |
383 | struct dm_crypt_request *dmreq; |
384 | u8 *iv; | |
385 | int r = 0; | |
386 | ||
b2174eeb | 387 | dmreq = dmreq_of_req(cc, req); |
3a7f6c99 MB |
388 | iv = (u8 *)ALIGN((unsigned long)(dmreq + 1), |
389 | crypto_ablkcipher_alignmask(cc->tfm) + 1); | |
01482b76 | 390 | |
b2174eeb | 391 | dmreq->ctx = ctx; |
3a7f6c99 MB |
392 | sg_init_table(&dmreq->sg_in, 1); |
393 | sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT, | |
01482b76 MB |
394 | bv_in->bv_offset + ctx->offset_in); |
395 | ||
3a7f6c99 MB |
396 | sg_init_table(&dmreq->sg_out, 1); |
397 | sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT, | |
01482b76 MB |
398 | bv_out->bv_offset + ctx->offset_out); |
399 | ||
400 | ctx->offset_in += 1 << SECTOR_SHIFT; | |
401 | if (ctx->offset_in >= bv_in->bv_len) { | |
402 | ctx->offset_in = 0; | |
403 | ctx->idx_in++; | |
404 | } | |
405 | ||
406 | ctx->offset_out += 1 << SECTOR_SHIFT; | |
407 | if (ctx->offset_out >= bv_out->bv_len) { | |
408 | ctx->offset_out = 0; | |
409 | ctx->idx_out++; | |
410 | } | |
411 | ||
3a7f6c99 MB |
412 | if (cc->iv_gen_ops) { |
413 | r = cc->iv_gen_ops->generator(cc, iv, ctx->sector); | |
414 | if (r < 0) | |
415 | return r; | |
416 | } | |
417 | ||
418 | ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out, | |
419 | 1 << SECTOR_SHIFT, iv); | |
420 | ||
421 | if (bio_data_dir(ctx->bio_in) == WRITE) | |
422 | r = crypto_ablkcipher_encrypt(req); | |
423 | else | |
424 | r = crypto_ablkcipher_decrypt(req); | |
425 | ||
426 | return r; | |
01482b76 MB |
427 | } |
428 | ||
95497a96 MB |
429 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
430 | int error); | |
ddd42edf MB |
431 | static void crypt_alloc_req(struct crypt_config *cc, |
432 | struct convert_context *ctx) | |
433 | { | |
434 | if (!cc->req) | |
435 | cc->req = mempool_alloc(cc->req_pool, GFP_NOIO); | |
95497a96 MB |
436 | ablkcipher_request_set_tfm(cc->req, cc->tfm); |
437 | ablkcipher_request_set_callback(cc->req, CRYPTO_TFM_REQ_MAY_BACKLOG | | |
b2174eeb HY |
438 | CRYPTO_TFM_REQ_MAY_SLEEP, |
439 | kcryptd_async_done, | |
440 | dmreq_of_req(cc, cc->req)); | |
ddd42edf MB |
441 | } |
442 | ||
1da177e4 LT |
443 | /* |
444 | * Encrypt / decrypt data from one bio to another one (can be the same one) | |
445 | */ | |
446 | static int crypt_convert(struct crypt_config *cc, | |
d469f841 | 447 | struct convert_context *ctx) |
1da177e4 | 448 | { |
3f1e9070 | 449 | int r; |
1da177e4 | 450 | |
c8081618 MB |
451 | atomic_set(&ctx->pending, 1); |
452 | ||
1da177e4 LT |
453 | while(ctx->idx_in < ctx->bio_in->bi_vcnt && |
454 | ctx->idx_out < ctx->bio_out->bi_vcnt) { | |
1da177e4 | 455 | |
3a7f6c99 MB |
456 | crypt_alloc_req(cc, ctx); |
457 | ||
3f1e9070 MB |
458 | atomic_inc(&ctx->pending); |
459 | ||
3a7f6c99 MB |
460 | r = crypt_convert_block(cc, ctx, cc->req); |
461 | ||
462 | switch (r) { | |
3f1e9070 | 463 | /* async */ |
3a7f6c99 MB |
464 | case -EBUSY: |
465 | wait_for_completion(&ctx->restart); | |
466 | INIT_COMPLETION(ctx->restart); | |
467 | /* fall through*/ | |
468 | case -EINPROGRESS: | |
3a7f6c99 | 469 | cc->req = NULL; |
3f1e9070 MB |
470 | ctx->sector++; |
471 | continue; | |
472 | ||
473 | /* sync */ | |
3a7f6c99 | 474 | case 0: |
3f1e9070 | 475 | atomic_dec(&ctx->pending); |
3a7f6c99 | 476 | ctx->sector++; |
c7f1b204 | 477 | cond_resched(); |
3a7f6c99 | 478 | continue; |
3a7f6c99 | 479 | |
3f1e9070 MB |
480 | /* error */ |
481 | default: | |
482 | atomic_dec(&ctx->pending); | |
483 | return r; | |
484 | } | |
1da177e4 LT |
485 | } |
486 | ||
3f1e9070 | 487 | return 0; |
1da177e4 LT |
488 | } |
489 | ||
d469f841 MB |
490 | static void dm_crypt_bio_destructor(struct bio *bio) |
491 | { | |
028867ac | 492 | struct dm_crypt_io *io = bio->bi_private; |
6a24c718 MB |
493 | struct crypt_config *cc = io->target->private; |
494 | ||
495 | bio_free(bio, cc->bs); | |
d469f841 | 496 | } |
6a24c718 | 497 | |
1da177e4 LT |
498 | /* |
499 | * Generate a new unfragmented bio with the given size | |
500 | * This should never violate the device limitations | |
933f01d4 MB |
501 | * May return a smaller bio when running out of pages, indicated by |
502 | * *out_of_pages set to 1. | |
1da177e4 | 503 | */ |
933f01d4 MB |
504 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size, |
505 | unsigned *out_of_pages) | |
1da177e4 | 506 | { |
027581f3 | 507 | struct crypt_config *cc = io->target->private; |
8b004457 | 508 | struct bio *clone; |
1da177e4 | 509 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
b4e3ca1a | 510 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
91e10625 MB |
511 | unsigned i, len; |
512 | struct page *page; | |
1da177e4 | 513 | |
2f9941b6 | 514 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs); |
8b004457 | 515 | if (!clone) |
1da177e4 | 516 | return NULL; |
1da177e4 | 517 | |
027581f3 | 518 | clone_init(io, clone); |
933f01d4 | 519 | *out_of_pages = 0; |
6a24c718 | 520 | |
f97380bc | 521 | for (i = 0; i < nr_iovecs; i++) { |
91e10625 | 522 | page = mempool_alloc(cc->page_pool, gfp_mask); |
933f01d4 MB |
523 | if (!page) { |
524 | *out_of_pages = 1; | |
1da177e4 | 525 | break; |
933f01d4 | 526 | } |
1da177e4 LT |
527 | |
528 | /* | |
529 | * if additional pages cannot be allocated without waiting, | |
530 | * return a partially allocated bio, the caller will then try | |
531 | * to allocate additional bios while submitting this partial bio | |
532 | */ | |
f97380bc | 533 | if (i == (MIN_BIO_PAGES - 1)) |
1da177e4 LT |
534 | gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT; |
535 | ||
91e10625 MB |
536 | len = (size > PAGE_SIZE) ? PAGE_SIZE : size; |
537 | ||
538 | if (!bio_add_page(clone, page, len, 0)) { | |
539 | mempool_free(page, cc->page_pool); | |
540 | break; | |
541 | } | |
1da177e4 | 542 | |
91e10625 | 543 | size -= len; |
1da177e4 LT |
544 | } |
545 | ||
8b004457 MB |
546 | if (!clone->bi_size) { |
547 | bio_put(clone); | |
1da177e4 LT |
548 | return NULL; |
549 | } | |
550 | ||
8b004457 | 551 | return clone; |
1da177e4 LT |
552 | } |
553 | ||
644bd2f0 | 554 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) |
1da177e4 | 555 | { |
644bd2f0 | 556 | unsigned int i; |
1da177e4 LT |
557 | struct bio_vec *bv; |
558 | ||
644bd2f0 | 559 | for (i = 0; i < clone->bi_vcnt; i++) { |
8b004457 | 560 | bv = bio_iovec_idx(clone, i); |
1da177e4 LT |
561 | BUG_ON(!bv->bv_page); |
562 | mempool_free(bv->bv_page, cc->page_pool); | |
563 | bv->bv_page = NULL; | |
564 | } | |
565 | } | |
566 | ||
dc440d1e MB |
567 | static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti, |
568 | struct bio *bio, sector_t sector) | |
569 | { | |
570 | struct crypt_config *cc = ti->private; | |
571 | struct dm_crypt_io *io; | |
572 | ||
573 | io = mempool_alloc(cc->io_pool, GFP_NOIO); | |
574 | io->target = ti; | |
575 | io->base_bio = bio; | |
576 | io->sector = sector; | |
577 | io->error = 0; | |
393b47ef | 578 | io->base_io = NULL; |
dc440d1e MB |
579 | atomic_set(&io->pending, 0); |
580 | ||
581 | return io; | |
582 | } | |
583 | ||
3e1a8bdd MB |
584 | static void crypt_inc_pending(struct dm_crypt_io *io) |
585 | { | |
586 | atomic_inc(&io->pending); | |
587 | } | |
588 | ||
1da177e4 LT |
589 | /* |
590 | * One of the bios was finished. Check for completion of | |
591 | * the whole request and correctly clean up the buffer. | |
393b47ef | 592 | * If base_io is set, wait for the last fragment to complete. |
1da177e4 | 593 | */ |
5742fd77 | 594 | static void crypt_dec_pending(struct dm_crypt_io *io) |
1da177e4 | 595 | { |
5742fd77 | 596 | struct crypt_config *cc = io->target->private; |
b35f8caa MB |
597 | struct bio *base_bio = io->base_bio; |
598 | struct dm_crypt_io *base_io = io->base_io; | |
599 | int error = io->error; | |
1da177e4 LT |
600 | |
601 | if (!atomic_dec_and_test(&io->pending)) | |
602 | return; | |
603 | ||
b35f8caa MB |
604 | mempool_free(io, cc->io_pool); |
605 | ||
606 | if (likely(!base_io)) | |
607 | bio_endio(base_bio, error); | |
393b47ef | 608 | else { |
b35f8caa MB |
609 | if (error && !base_io->error) |
610 | base_io->error = error; | |
611 | crypt_dec_pending(base_io); | |
393b47ef | 612 | } |
1da177e4 LT |
613 | } |
614 | ||
615 | /* | |
cabf08e4 | 616 | * kcryptd/kcryptd_io: |
1da177e4 LT |
617 | * |
618 | * Needed because it would be very unwise to do decryption in an | |
23541d2d | 619 | * interrupt context. |
cabf08e4 MB |
620 | * |
621 | * kcryptd performs the actual encryption or decryption. | |
622 | * | |
623 | * kcryptd_io performs the IO submission. | |
624 | * | |
625 | * They must be separated as otherwise the final stages could be | |
626 | * starved by new requests which can block in the first stages due | |
627 | * to memory allocation. | |
1da177e4 | 628 | */ |
6712ecf8 | 629 | static void crypt_endio(struct bio *clone, int error) |
8b004457 | 630 | { |
028867ac | 631 | struct dm_crypt_io *io = clone->bi_private; |
8b004457 | 632 | struct crypt_config *cc = io->target->private; |
ee7a491e | 633 | unsigned rw = bio_data_dir(clone); |
8b004457 | 634 | |
adfe4770 MB |
635 | if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error)) |
636 | error = -EIO; | |
637 | ||
8b004457 | 638 | /* |
6712ecf8 | 639 | * free the processed pages |
8b004457 | 640 | */ |
ee7a491e | 641 | if (rw == WRITE) |
644bd2f0 | 642 | crypt_free_buffer_pages(cc, clone); |
8b004457 MB |
643 | |
644 | bio_put(clone); | |
8b004457 | 645 | |
ee7a491e MB |
646 | if (rw == READ && !error) { |
647 | kcryptd_queue_crypt(io); | |
648 | return; | |
649 | } | |
5742fd77 MB |
650 | |
651 | if (unlikely(error)) | |
652 | io->error = error; | |
653 | ||
654 | crypt_dec_pending(io); | |
8b004457 MB |
655 | } |
656 | ||
028867ac | 657 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
8b004457 MB |
658 | { |
659 | struct crypt_config *cc = io->target->private; | |
660 | ||
661 | clone->bi_private = io; | |
662 | clone->bi_end_io = crypt_endio; | |
663 | clone->bi_bdev = cc->dev->bdev; | |
664 | clone->bi_rw = io->base_bio->bi_rw; | |
027581f3 | 665 | clone->bi_destructor = dm_crypt_bio_destructor; |
8b004457 MB |
666 | } |
667 | ||
4e4eef64 | 668 | static void kcryptd_io_read(struct dm_crypt_io *io) |
8b004457 MB |
669 | { |
670 | struct crypt_config *cc = io->target->private; | |
671 | struct bio *base_bio = io->base_bio; | |
672 | struct bio *clone; | |
93e605c2 | 673 | |
3e1a8bdd | 674 | crypt_inc_pending(io); |
8b004457 MB |
675 | |
676 | /* | |
677 | * The block layer might modify the bvec array, so always | |
678 | * copy the required bvecs because we need the original | |
679 | * one in order to decrypt the whole bio data *afterwards*. | |
680 | */ | |
6a24c718 | 681 | clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs); |
93e605c2 | 682 | if (unlikely(!clone)) { |
5742fd77 MB |
683 | io->error = -ENOMEM; |
684 | crypt_dec_pending(io); | |
23541d2d | 685 | return; |
93e605c2 | 686 | } |
8b004457 MB |
687 | |
688 | clone_init(io, clone); | |
689 | clone->bi_idx = 0; | |
690 | clone->bi_vcnt = bio_segments(base_bio); | |
691 | clone->bi_size = base_bio->bi_size; | |
0c395b0f | 692 | clone->bi_sector = cc->start + io->sector; |
8b004457 MB |
693 | memcpy(clone->bi_io_vec, bio_iovec(base_bio), |
694 | sizeof(struct bio_vec) * clone->bi_vcnt); | |
8b004457 | 695 | |
93e605c2 | 696 | generic_make_request(clone); |
8b004457 MB |
697 | } |
698 | ||
4e4eef64 MB |
699 | static void kcryptd_io_write(struct dm_crypt_io *io) |
700 | { | |
95497a96 | 701 | struct bio *clone = io->ctx.bio_out; |
95497a96 | 702 | generic_make_request(clone); |
4e4eef64 MB |
703 | } |
704 | ||
395b167c AK |
705 | static void kcryptd_io(struct work_struct *work) |
706 | { | |
707 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); | |
708 | ||
709 | if (bio_data_dir(io->base_bio) == READ) | |
710 | kcryptd_io_read(io); | |
711 | else | |
712 | kcryptd_io_write(io); | |
713 | } | |
714 | ||
715 | static void kcryptd_queue_io(struct dm_crypt_io *io) | |
716 | { | |
717 | struct crypt_config *cc = io->target->private; | |
718 | ||
719 | INIT_WORK(&io->work, kcryptd_io); | |
720 | queue_work(cc->io_queue, &io->work); | |
721 | } | |
722 | ||
95497a96 MB |
723 | static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, |
724 | int error, int async) | |
4e4eef64 | 725 | { |
dec1cedf MB |
726 | struct bio *clone = io->ctx.bio_out; |
727 | struct crypt_config *cc = io->target->private; | |
728 | ||
729 | if (unlikely(error < 0)) { | |
730 | crypt_free_buffer_pages(cc, clone); | |
731 | bio_put(clone); | |
732 | io->error = -EIO; | |
6c031f41 | 733 | crypt_dec_pending(io); |
dec1cedf MB |
734 | return; |
735 | } | |
736 | ||
737 | /* crypt_convert should have filled the clone bio */ | |
738 | BUG_ON(io->ctx.idx_out < clone->bi_vcnt); | |
739 | ||
740 | clone->bi_sector = cc->start + io->sector; | |
899c95d3 | 741 | |
95497a96 MB |
742 | if (async) |
743 | kcryptd_queue_io(io); | |
1e37bb8e | 744 | else |
95497a96 | 745 | generic_make_request(clone); |
4e4eef64 MB |
746 | } |
747 | ||
fc5a5e9a | 748 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
8b004457 MB |
749 | { |
750 | struct crypt_config *cc = io->target->private; | |
8b004457 | 751 | struct bio *clone; |
393b47ef | 752 | struct dm_crypt_io *new_io; |
c8081618 | 753 | int crypt_finished; |
933f01d4 | 754 | unsigned out_of_pages = 0; |
dec1cedf | 755 | unsigned remaining = io->base_bio->bi_size; |
b635b00e | 756 | sector_t sector = io->sector; |
dec1cedf | 757 | int r; |
8b004457 | 758 | |
fc5a5e9a MB |
759 | /* |
760 | * Prevent io from disappearing until this function completes. | |
761 | */ | |
762 | crypt_inc_pending(io); | |
b635b00e | 763 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
fc5a5e9a | 764 | |
93e605c2 MB |
765 | /* |
766 | * The allocated buffers can be smaller than the whole bio, | |
767 | * so repeat the whole process until all the data can be handled. | |
768 | */ | |
769 | while (remaining) { | |
933f01d4 | 770 | clone = crypt_alloc_buffer(io, remaining, &out_of_pages); |
23541d2d | 771 | if (unlikely(!clone)) { |
5742fd77 | 772 | io->error = -ENOMEM; |
fc5a5e9a | 773 | break; |
23541d2d | 774 | } |
93e605c2 | 775 | |
53017030 MB |
776 | io->ctx.bio_out = clone; |
777 | io->ctx.idx_out = 0; | |
93e605c2 | 778 | |
dec1cedf | 779 | remaining -= clone->bi_size; |
b635b00e | 780 | sector += bio_sectors(clone); |
93e605c2 | 781 | |
4e594098 | 782 | crypt_inc_pending(io); |
dec1cedf | 783 | r = crypt_convert(cc, &io->ctx); |
c8081618 | 784 | crypt_finished = atomic_dec_and_test(&io->ctx.pending); |
f97380bc | 785 | |
c8081618 MB |
786 | /* Encryption was already finished, submit io now */ |
787 | if (crypt_finished) { | |
3a7f6c99 | 788 | kcryptd_crypt_write_io_submit(io, r, 0); |
c8081618 MB |
789 | |
790 | /* | |
791 | * If there was an error, do not try next fragments. | |
792 | * For async, error is processed in async handler. | |
793 | */ | |
6c031f41 | 794 | if (unlikely(r < 0)) |
fc5a5e9a | 795 | break; |
b635b00e MB |
796 | |
797 | io->sector = sector; | |
4e594098 | 798 | } |
93e605c2 | 799 | |
933f01d4 MB |
800 | /* |
801 | * Out of memory -> run queues | |
802 | * But don't wait if split was due to the io size restriction | |
803 | */ | |
804 | if (unlikely(out_of_pages)) | |
8aa7e847 | 805 | congestion_wait(BLK_RW_ASYNC, HZ/100); |
933f01d4 | 806 | |
393b47ef MB |
807 | /* |
808 | * With async crypto it is unsafe to share the crypto context | |
809 | * between fragments, so switch to a new dm_crypt_io structure. | |
810 | */ | |
811 | if (unlikely(!crypt_finished && remaining)) { | |
812 | new_io = crypt_io_alloc(io->target, io->base_bio, | |
813 | sector); | |
814 | crypt_inc_pending(new_io); | |
815 | crypt_convert_init(cc, &new_io->ctx, NULL, | |
816 | io->base_bio, sector); | |
817 | new_io->ctx.idx_in = io->ctx.idx_in; | |
818 | new_io->ctx.offset_in = io->ctx.offset_in; | |
819 | ||
820 | /* | |
821 | * Fragments after the first use the base_io | |
822 | * pending count. | |
823 | */ | |
824 | if (!io->base_io) | |
825 | new_io->base_io = io; | |
826 | else { | |
827 | new_io->base_io = io->base_io; | |
828 | crypt_inc_pending(io->base_io); | |
829 | crypt_dec_pending(io); | |
830 | } | |
831 | ||
832 | io = new_io; | |
833 | } | |
93e605c2 | 834 | } |
899c95d3 MB |
835 | |
836 | crypt_dec_pending(io); | |
84131db6 MB |
837 | } |
838 | ||
4e4eef64 | 839 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error) |
5742fd77 MB |
840 | { |
841 | if (unlikely(error < 0)) | |
842 | io->error = -EIO; | |
843 | ||
844 | crypt_dec_pending(io); | |
845 | } | |
846 | ||
4e4eef64 | 847 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
8b004457 MB |
848 | { |
849 | struct crypt_config *cc = io->target->private; | |
5742fd77 | 850 | int r = 0; |
1da177e4 | 851 | |
3e1a8bdd | 852 | crypt_inc_pending(io); |
3a7f6c99 | 853 | |
53017030 | 854 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
0c395b0f | 855 | io->sector); |
1da177e4 | 856 | |
5742fd77 MB |
857 | r = crypt_convert(cc, &io->ctx); |
858 | ||
3f1e9070 | 859 | if (atomic_dec_and_test(&io->ctx.pending)) |
3a7f6c99 MB |
860 | kcryptd_crypt_read_done(io, r); |
861 | ||
862 | crypt_dec_pending(io); | |
1da177e4 LT |
863 | } |
864 | ||
95497a96 MB |
865 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
866 | int error) | |
867 | { | |
b2174eeb HY |
868 | struct dm_crypt_request *dmreq = async_req->data; |
869 | struct convert_context *ctx = dmreq->ctx; | |
95497a96 MB |
870 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); |
871 | struct crypt_config *cc = io->target->private; | |
872 | ||
873 | if (error == -EINPROGRESS) { | |
874 | complete(&ctx->restart); | |
875 | return; | |
876 | } | |
877 | ||
b2174eeb | 878 | mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool); |
95497a96 MB |
879 | |
880 | if (!atomic_dec_and_test(&ctx->pending)) | |
881 | return; | |
882 | ||
883 | if (bio_data_dir(io->base_bio) == READ) | |
884 | kcryptd_crypt_read_done(io, error); | |
885 | else | |
886 | kcryptd_crypt_write_io_submit(io, error, 1); | |
887 | } | |
888 | ||
395b167c | 889 | static void kcryptd_crypt(struct work_struct *work) |
1da177e4 | 890 | { |
028867ac | 891 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
8b004457 | 892 | |
cabf08e4 | 893 | if (bio_data_dir(io->base_bio) == READ) |
395b167c | 894 | kcryptd_crypt_read_convert(io); |
4e4eef64 | 895 | else |
395b167c | 896 | kcryptd_crypt_write_convert(io); |
cabf08e4 MB |
897 | } |
898 | ||
395b167c | 899 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
cabf08e4 | 900 | { |
395b167c | 901 | struct crypt_config *cc = io->target->private; |
cabf08e4 | 902 | |
395b167c AK |
903 | INIT_WORK(&io->work, kcryptd_crypt); |
904 | queue_work(cc->crypt_queue, &io->work); | |
1da177e4 LT |
905 | } |
906 | ||
907 | /* | |
908 | * Decode key from its hex representation | |
909 | */ | |
910 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) | |
911 | { | |
912 | char buffer[3]; | |
913 | char *endp; | |
914 | unsigned int i; | |
915 | ||
916 | buffer[2] = '\0'; | |
917 | ||
8b004457 | 918 | for (i = 0; i < size; i++) { |
1da177e4 LT |
919 | buffer[0] = *hex++; |
920 | buffer[1] = *hex++; | |
921 | ||
922 | key[i] = (u8)simple_strtoul(buffer, &endp, 16); | |
923 | ||
924 | if (endp != &buffer[2]) | |
925 | return -EINVAL; | |
926 | } | |
927 | ||
928 | if (*hex != '\0') | |
929 | return -EINVAL; | |
930 | ||
931 | return 0; | |
932 | } | |
933 | ||
934 | /* | |
935 | * Encode key into its hex representation | |
936 | */ | |
937 | static void crypt_encode_key(char *hex, u8 *key, unsigned int size) | |
938 | { | |
939 | unsigned int i; | |
940 | ||
8b004457 | 941 | for (i = 0; i < size; i++) { |
1da177e4 LT |
942 | sprintf(hex, "%02x", *key); |
943 | hex += 2; | |
944 | key++; | |
945 | } | |
946 | } | |
947 | ||
e48d4bbf MB |
948 | static int crypt_set_key(struct crypt_config *cc, char *key) |
949 | { | |
950 | unsigned key_size = strlen(key) >> 1; | |
951 | ||
952 | if (cc->key_size && cc->key_size != key_size) | |
953 | return -EINVAL; | |
954 | ||
955 | cc->key_size = key_size; /* initial settings */ | |
956 | ||
957 | if ((!key_size && strcmp(key, "-")) || | |
d469f841 | 958 | (key_size && crypt_decode_key(cc->key, key, key_size) < 0)) |
e48d4bbf MB |
959 | return -EINVAL; |
960 | ||
961 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
962 | ||
0b430958 | 963 | return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size); |
e48d4bbf MB |
964 | } |
965 | ||
966 | static int crypt_wipe_key(struct crypt_config *cc) | |
967 | { | |
968 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
969 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); | |
0b430958 | 970 | return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size); |
e48d4bbf MB |
971 | } |
972 | ||
1da177e4 LT |
973 | /* |
974 | * Construct an encryption mapping: | |
975 | * <cipher> <key> <iv_offset> <dev_path> <start> | |
976 | */ | |
977 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
978 | { | |
979 | struct crypt_config *cc; | |
3a7f6c99 | 980 | struct crypto_ablkcipher *tfm; |
1da177e4 LT |
981 | char *tmp; |
982 | char *cipher; | |
983 | char *chainmode; | |
984 | char *ivmode; | |
985 | char *ivopts; | |
1da177e4 | 986 | unsigned int key_size; |
4ee218cd | 987 | unsigned long long tmpll; |
1da177e4 LT |
988 | |
989 | if (argc != 5) { | |
72d94861 | 990 | ti->error = "Not enough arguments"; |
1da177e4 LT |
991 | return -EINVAL; |
992 | } | |
993 | ||
994 | tmp = argv[0]; | |
995 | cipher = strsep(&tmp, "-"); | |
996 | chainmode = strsep(&tmp, "-"); | |
997 | ivopts = strsep(&tmp, "-"); | |
998 | ivmode = strsep(&ivopts, ":"); | |
999 | ||
1000 | if (tmp) | |
72d94861 | 1001 | DMWARN("Unexpected additional cipher options"); |
1da177e4 LT |
1002 | |
1003 | key_size = strlen(argv[1]) >> 1; | |
1004 | ||
e48d4bbf | 1005 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
1da177e4 LT |
1006 | if (cc == NULL) { |
1007 | ti->error = | |
72d94861 | 1008 | "Cannot allocate transparent encryption context"; |
1da177e4 LT |
1009 | return -ENOMEM; |
1010 | } | |
1011 | ||
06fe9fb4 | 1012 | /* Compatibility mode for old dm-crypt cipher strings */ |
1da177e4 LT |
1013 | if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) { |
1014 | chainmode = "cbc"; | |
1015 | ivmode = "plain"; | |
1016 | } | |
1017 | ||
d1806f6a HX |
1018 | if (strcmp(chainmode, "ecb") && !ivmode) { |
1019 | ti->error = "This chaining mode requires an IV mechanism"; | |
636d5786 | 1020 | goto bad_cipher; |
1da177e4 LT |
1021 | } |
1022 | ||
d469f841 MB |
1023 | if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
1024 | chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) { | |
d1806f6a | 1025 | ti->error = "Chain mode + cipher name is too long"; |
636d5786 | 1026 | goto bad_cipher; |
1da177e4 LT |
1027 | } |
1028 | ||
3a7f6c99 | 1029 | tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0); |
d1806f6a | 1030 | if (IS_ERR(tfm)) { |
72d94861 | 1031 | ti->error = "Error allocating crypto tfm"; |
636d5786 | 1032 | goto bad_cipher; |
1da177e4 | 1033 | } |
1da177e4 | 1034 | |
d1806f6a HX |
1035 | strcpy(cc->cipher, cipher); |
1036 | strcpy(cc->chainmode, chainmode); | |
1da177e4 LT |
1037 | cc->tfm = tfm; |
1038 | ||
0b430958 MB |
1039 | if (crypt_set_key(cc, argv[1]) < 0) { |
1040 | ti->error = "Error decoding and setting key"; | |
1041 | goto bad_ivmode; | |
1042 | } | |
1043 | ||
1da177e4 | 1044 | /* |
48527fa7 | 1045 | * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi". |
1da177e4 LT |
1046 | * See comments at iv code |
1047 | */ | |
1048 | ||
1049 | if (ivmode == NULL) | |
1050 | cc->iv_gen_ops = NULL; | |
1051 | else if (strcmp(ivmode, "plain") == 0) | |
1052 | cc->iv_gen_ops = &crypt_iv_plain_ops; | |
1053 | else if (strcmp(ivmode, "essiv") == 0) | |
1054 | cc->iv_gen_ops = &crypt_iv_essiv_ops; | |
48527fa7 RS |
1055 | else if (strcmp(ivmode, "benbi") == 0) |
1056 | cc->iv_gen_ops = &crypt_iv_benbi_ops; | |
46b47730 LN |
1057 | else if (strcmp(ivmode, "null") == 0) |
1058 | cc->iv_gen_ops = &crypt_iv_null_ops; | |
1da177e4 | 1059 | else { |
72d94861 | 1060 | ti->error = "Invalid IV mode"; |
636d5786 | 1061 | goto bad_ivmode; |
1da177e4 LT |
1062 | } |
1063 | ||
1064 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr && | |
1065 | cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0) | |
636d5786 | 1066 | goto bad_ivmode; |
1da177e4 | 1067 | |
b95bf2d3 MB |
1068 | if (cc->iv_gen_ops && cc->iv_gen_ops->init && |
1069 | cc->iv_gen_ops->init(cc) < 0) { | |
1070 | ti->error = "Error initialising IV"; | |
1071 | goto bad_slab_pool; | |
1072 | } | |
1073 | ||
3a7f6c99 | 1074 | cc->iv_size = crypto_ablkcipher_ivsize(tfm); |
d1806f6a | 1075 | if (cc->iv_size) |
1da177e4 | 1076 | /* at least a 64 bit sector number should fit in our buffer */ |
d1806f6a | 1077 | cc->iv_size = max(cc->iv_size, |
d469f841 | 1078 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
1da177e4 | 1079 | else { |
1da177e4 | 1080 | if (cc->iv_gen_ops) { |
72d94861 | 1081 | DMWARN("Selected cipher does not support IVs"); |
1da177e4 LT |
1082 | if (cc->iv_gen_ops->dtr) |
1083 | cc->iv_gen_ops->dtr(cc); | |
1084 | cc->iv_gen_ops = NULL; | |
1085 | } | |
1086 | } | |
1087 | ||
93d2341c | 1088 | cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool); |
1da177e4 | 1089 | if (!cc->io_pool) { |
72d94861 | 1090 | ti->error = "Cannot allocate crypt io mempool"; |
636d5786 | 1091 | goto bad_slab_pool; |
1da177e4 LT |
1092 | } |
1093 | ||
ddd42edf | 1094 | cc->dmreq_start = sizeof(struct ablkcipher_request); |
3a7f6c99 | 1095 | cc->dmreq_start += crypto_ablkcipher_reqsize(tfm); |
ddd42edf | 1096 | cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment()); |
3a7f6c99 MB |
1097 | cc->dmreq_start += crypto_ablkcipher_alignmask(tfm) & |
1098 | ~(crypto_tfm_ctx_alignment() - 1); | |
ddd42edf MB |
1099 | |
1100 | cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + | |
1101 | sizeof(struct dm_crypt_request) + cc->iv_size); | |
1102 | if (!cc->req_pool) { | |
1103 | ti->error = "Cannot allocate crypt request mempool"; | |
1104 | goto bad_req_pool; | |
1105 | } | |
1106 | cc->req = NULL; | |
1107 | ||
a19b27ce | 1108 | cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
1da177e4 | 1109 | if (!cc->page_pool) { |
72d94861 | 1110 | ti->error = "Cannot allocate page mempool"; |
636d5786 | 1111 | goto bad_page_pool; |
1da177e4 LT |
1112 | } |
1113 | ||
bb799ca0 | 1114 | cc->bs = bioset_create(MIN_IOS, 0); |
6a24c718 MB |
1115 | if (!cc->bs) { |
1116 | ti->error = "Cannot allocate crypt bioset"; | |
1117 | goto bad_bs; | |
1118 | } | |
1119 | ||
4ee218cd | 1120 | if (sscanf(argv[2], "%llu", &tmpll) != 1) { |
72d94861 | 1121 | ti->error = "Invalid iv_offset sector"; |
636d5786 | 1122 | goto bad_device; |
1da177e4 | 1123 | } |
4ee218cd | 1124 | cc->iv_offset = tmpll; |
1da177e4 | 1125 | |
4ee218cd | 1126 | if (sscanf(argv[4], "%llu", &tmpll) != 1) { |
72d94861 | 1127 | ti->error = "Invalid device sector"; |
636d5786 | 1128 | goto bad_device; |
1da177e4 | 1129 | } |
4ee218cd | 1130 | cc->start = tmpll; |
1da177e4 LT |
1131 | |
1132 | if (dm_get_device(ti, argv[3], cc->start, ti->len, | |
d469f841 | 1133 | dm_table_get_mode(ti->table), &cc->dev)) { |
72d94861 | 1134 | ti->error = "Device lookup failed"; |
636d5786 | 1135 | goto bad_device; |
1da177e4 LT |
1136 | } |
1137 | ||
1138 | if (ivmode && cc->iv_gen_ops) { | |
1139 | if (ivopts) | |
1140 | *(ivopts - 1) = ':'; | |
1141 | cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL); | |
1142 | if (!cc->iv_mode) { | |
72d94861 | 1143 | ti->error = "Error kmallocing iv_mode string"; |
636d5786 | 1144 | goto bad_ivmode_string; |
1da177e4 LT |
1145 | } |
1146 | strcpy(cc->iv_mode, ivmode); | |
1147 | } else | |
1148 | cc->iv_mode = NULL; | |
1149 | ||
cabf08e4 MB |
1150 | cc->io_queue = create_singlethread_workqueue("kcryptd_io"); |
1151 | if (!cc->io_queue) { | |
1152 | ti->error = "Couldn't create kcryptd io queue"; | |
1153 | goto bad_io_queue; | |
1154 | } | |
1155 | ||
1156 | cc->crypt_queue = create_singlethread_workqueue("kcryptd"); | |
1157 | if (!cc->crypt_queue) { | |
9934a8be | 1158 | ti->error = "Couldn't create kcryptd queue"; |
cabf08e4 | 1159 | goto bad_crypt_queue; |
9934a8be MB |
1160 | } |
1161 | ||
647c7db1 | 1162 | ti->num_flush_requests = 1; |
1da177e4 LT |
1163 | ti->private = cc; |
1164 | return 0; | |
1165 | ||
cabf08e4 MB |
1166 | bad_crypt_queue: |
1167 | destroy_workqueue(cc->io_queue); | |
1168 | bad_io_queue: | |
9934a8be | 1169 | kfree(cc->iv_mode); |
636d5786 | 1170 | bad_ivmode_string: |
55b42c5a | 1171 | dm_put_device(ti, cc->dev); |
636d5786 | 1172 | bad_device: |
6a24c718 MB |
1173 | bioset_free(cc->bs); |
1174 | bad_bs: | |
1da177e4 | 1175 | mempool_destroy(cc->page_pool); |
636d5786 | 1176 | bad_page_pool: |
ddd42edf MB |
1177 | mempool_destroy(cc->req_pool); |
1178 | bad_req_pool: | |
1da177e4 | 1179 | mempool_destroy(cc->io_pool); |
636d5786 | 1180 | bad_slab_pool: |
1da177e4 LT |
1181 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
1182 | cc->iv_gen_ops->dtr(cc); | |
636d5786 | 1183 | bad_ivmode: |
3a7f6c99 | 1184 | crypto_free_ablkcipher(tfm); |
636d5786 | 1185 | bad_cipher: |
9d3520a3 | 1186 | /* Must zero key material before freeing */ |
b81d6cf7 | 1187 | kzfree(cc); |
1da177e4 LT |
1188 | return -EINVAL; |
1189 | } | |
1190 | ||
1191 | static void crypt_dtr(struct dm_target *ti) | |
1192 | { | |
1193 | struct crypt_config *cc = (struct crypt_config *) ti->private; | |
1194 | ||
cabf08e4 MB |
1195 | destroy_workqueue(cc->io_queue); |
1196 | destroy_workqueue(cc->crypt_queue); | |
80b16c19 | 1197 | |
ddd42edf MB |
1198 | if (cc->req) |
1199 | mempool_free(cc->req, cc->req_pool); | |
1200 | ||
6a24c718 | 1201 | bioset_free(cc->bs); |
1da177e4 | 1202 | mempool_destroy(cc->page_pool); |
ddd42edf | 1203 | mempool_destroy(cc->req_pool); |
1da177e4 LT |
1204 | mempool_destroy(cc->io_pool); |
1205 | ||
990a8baf | 1206 | kfree(cc->iv_mode); |
1da177e4 LT |
1207 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
1208 | cc->iv_gen_ops->dtr(cc); | |
3a7f6c99 | 1209 | crypto_free_ablkcipher(cc->tfm); |
1da177e4 | 1210 | dm_put_device(ti, cc->dev); |
9d3520a3 SR |
1211 | |
1212 | /* Must zero key material before freeing */ | |
b81d6cf7 | 1213 | kzfree(cc); |
1da177e4 LT |
1214 | } |
1215 | ||
1da177e4 LT |
1216 | static int crypt_map(struct dm_target *ti, struct bio *bio, |
1217 | union map_info *map_context) | |
1218 | { | |
028867ac | 1219 | struct dm_crypt_io *io; |
647c7db1 MP |
1220 | struct crypt_config *cc; |
1221 | ||
1222 | if (unlikely(bio_empty_barrier(bio))) { | |
1223 | cc = ti->private; | |
1224 | bio->bi_bdev = cc->dev->bdev; | |
1225 | return DM_MAPIO_REMAPPED; | |
1226 | } | |
1da177e4 | 1227 | |
dc440d1e | 1228 | io = crypt_io_alloc(ti, bio, bio->bi_sector - ti->begin); |
cabf08e4 MB |
1229 | |
1230 | if (bio_data_dir(io->base_bio) == READ) | |
1231 | kcryptd_queue_io(io); | |
1232 | else | |
1233 | kcryptd_queue_crypt(io); | |
1da177e4 | 1234 | |
d2a7ad29 | 1235 | return DM_MAPIO_SUBMITTED; |
1da177e4 LT |
1236 | } |
1237 | ||
1238 | static int crypt_status(struct dm_target *ti, status_type_t type, | |
1239 | char *result, unsigned int maxlen) | |
1240 | { | |
1241 | struct crypt_config *cc = (struct crypt_config *) ti->private; | |
1da177e4 LT |
1242 | unsigned int sz = 0; |
1243 | ||
1244 | switch (type) { | |
1245 | case STATUSTYPE_INFO: | |
1246 | result[0] = '\0'; | |
1247 | break; | |
1248 | ||
1249 | case STATUSTYPE_TABLE: | |
1da177e4 | 1250 | if (cc->iv_mode) |
37af6560 CS |
1251 | DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode, |
1252 | cc->iv_mode); | |
1da177e4 | 1253 | else |
37af6560 | 1254 | DMEMIT("%s-%s ", cc->cipher, cc->chainmode); |
1da177e4 LT |
1255 | |
1256 | if (cc->key_size > 0) { | |
1257 | if ((maxlen - sz) < ((cc->key_size << 1) + 1)) | |
1258 | return -ENOMEM; | |
1259 | ||
1260 | crypt_encode_key(result + sz, cc->key, cc->key_size); | |
1261 | sz += cc->key_size << 1; | |
1262 | } else { | |
1263 | if (sz >= maxlen) | |
1264 | return -ENOMEM; | |
1265 | result[sz++] = '-'; | |
1266 | } | |
1267 | ||
4ee218cd AM |
1268 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
1269 | cc->dev->name, (unsigned long long)cc->start); | |
1da177e4 LT |
1270 | break; |
1271 | } | |
1272 | return 0; | |
1273 | } | |
1274 | ||
e48d4bbf MB |
1275 | static void crypt_postsuspend(struct dm_target *ti) |
1276 | { | |
1277 | struct crypt_config *cc = ti->private; | |
1278 | ||
1279 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); | |
1280 | } | |
1281 | ||
1282 | static int crypt_preresume(struct dm_target *ti) | |
1283 | { | |
1284 | struct crypt_config *cc = ti->private; | |
1285 | ||
1286 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { | |
1287 | DMERR("aborting resume - crypt key is not set."); | |
1288 | return -EAGAIN; | |
1289 | } | |
1290 | ||
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | static void crypt_resume(struct dm_target *ti) | |
1295 | { | |
1296 | struct crypt_config *cc = ti->private; | |
1297 | ||
1298 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); | |
1299 | } | |
1300 | ||
1301 | /* Message interface | |
1302 | * key set <key> | |
1303 | * key wipe | |
1304 | */ | |
1305 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv) | |
1306 | { | |
1307 | struct crypt_config *cc = ti->private; | |
1308 | ||
1309 | if (argc < 2) | |
1310 | goto error; | |
1311 | ||
1312 | if (!strnicmp(argv[0], MESG_STR("key"))) { | |
1313 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { | |
1314 | DMWARN("not suspended during key manipulation."); | |
1315 | return -EINVAL; | |
1316 | } | |
1317 | if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) | |
1318 | return crypt_set_key(cc, argv[2]); | |
1319 | if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) | |
1320 | return crypt_wipe_key(cc); | |
1321 | } | |
1322 | ||
1323 | error: | |
1324 | DMWARN("unrecognised message received."); | |
1325 | return -EINVAL; | |
1326 | } | |
1327 | ||
d41e26b9 MB |
1328 | static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
1329 | struct bio_vec *biovec, int max_size) | |
1330 | { | |
1331 | struct crypt_config *cc = ti->private; | |
1332 | struct request_queue *q = bdev_get_queue(cc->dev->bdev); | |
1333 | ||
1334 | if (!q->merge_bvec_fn) | |
1335 | return max_size; | |
1336 | ||
1337 | bvm->bi_bdev = cc->dev->bdev; | |
1338 | bvm->bi_sector = cc->start + bvm->bi_sector - ti->begin; | |
1339 | ||
1340 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); | |
1341 | } | |
1342 | ||
af4874e0 MS |
1343 | static int crypt_iterate_devices(struct dm_target *ti, |
1344 | iterate_devices_callout_fn fn, void *data) | |
1345 | { | |
1346 | struct crypt_config *cc = ti->private; | |
1347 | ||
5dea271b | 1348 | return fn(ti, cc->dev, cc->start, ti->len, data); |
af4874e0 MS |
1349 | } |
1350 | ||
1da177e4 LT |
1351 | static struct target_type crypt_target = { |
1352 | .name = "crypt", | |
af4874e0 | 1353 | .version = {1, 7, 0}, |
1da177e4 LT |
1354 | .module = THIS_MODULE, |
1355 | .ctr = crypt_ctr, | |
1356 | .dtr = crypt_dtr, | |
1357 | .map = crypt_map, | |
1358 | .status = crypt_status, | |
e48d4bbf MB |
1359 | .postsuspend = crypt_postsuspend, |
1360 | .preresume = crypt_preresume, | |
1361 | .resume = crypt_resume, | |
1362 | .message = crypt_message, | |
d41e26b9 | 1363 | .merge = crypt_merge, |
af4874e0 | 1364 | .iterate_devices = crypt_iterate_devices, |
1da177e4 LT |
1365 | }; |
1366 | ||
1367 | static int __init dm_crypt_init(void) | |
1368 | { | |
1369 | int r; | |
1370 | ||
028867ac | 1371 | _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0); |
1da177e4 LT |
1372 | if (!_crypt_io_pool) |
1373 | return -ENOMEM; | |
1374 | ||
1da177e4 LT |
1375 | r = dm_register_target(&crypt_target); |
1376 | if (r < 0) { | |
72d94861 | 1377 | DMERR("register failed %d", r); |
9934a8be | 1378 | kmem_cache_destroy(_crypt_io_pool); |
1da177e4 LT |
1379 | } |
1380 | ||
1da177e4 LT |
1381 | return r; |
1382 | } | |
1383 | ||
1384 | static void __exit dm_crypt_exit(void) | |
1385 | { | |
10d3bd09 | 1386 | dm_unregister_target(&crypt_target); |
1da177e4 LT |
1387 | kmem_cache_destroy(_crypt_io_pool); |
1388 | } | |
1389 | ||
1390 | module_init(dm_crypt_init); | |
1391 | module_exit(dm_crypt_exit); | |
1392 | ||
1393 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); | |
1394 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); | |
1395 | MODULE_LICENSE("GPL"); |