• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3  * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
4  * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
5  *
6  * This file is released under the GPL.
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/err.h>
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>
20 #include <linux/kthread.h>
21 #include <linux/backing-dev.h>
22 #include <linux/atomic.h>
23 #include <linux/scatterlist.h>
24 #include <linux/rbtree.h>
25 #include <asm/page.h>
26 #include <asm/unaligned.h>
27 #include <crypto/hash.h>
28 #include <crypto/md5.h>
29 #include <crypto/algapi.h>
30 
31 #include <linux/device-mapper.h>
32 
33 #define DM_MSG_PREFIX "crypt"
34 
35 /*
36  * context holding the current state of a multi-part conversion
37  */
38 struct convert_context {
39 	struct completion restart;
40 	struct bio *bio_in;
41 	struct bio *bio_out;
42 	unsigned int offset_in;
43 	unsigned int offset_out;
44 	unsigned int idx_in;
45 	unsigned int idx_out;
46 	sector_t cc_sector;
47 	atomic_t cc_pending;
48 	struct ablkcipher_request *req;
49 };
50 
51 /*
52  * per bio private data
53  */
54 struct dm_crypt_io {
55 	struct crypt_config *cc;
56 	struct bio *base_bio;
57 	struct work_struct work;
58 
59 	struct convert_context ctx;
60 
61 	atomic_t io_pending;
62 	int error;
63 	sector_t sector;
64 
65 	struct rb_node rb_node;
66 } CRYPTO_MINALIGN_ATTR;
67 
68 struct dm_crypt_request {
69 	struct convert_context *ctx;
70 	struct scatterlist sg_in;
71 	struct scatterlist sg_out;
72 	sector_t iv_sector;
73 };
74 
75 struct crypt_config;
76 
77 struct crypt_iv_operations {
78 	int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
79 		   const char *opts);
80 	void (*dtr)(struct crypt_config *cc);
81 	int (*init)(struct crypt_config *cc);
82 	int (*wipe)(struct crypt_config *cc);
83 	int (*generator)(struct crypt_config *cc, u8 *iv,
84 			 struct dm_crypt_request *dmreq);
85 	int (*post)(struct crypt_config *cc, u8 *iv,
86 		    struct dm_crypt_request *dmreq);
87 };
88 
89 struct iv_essiv_private {
90 	struct crypto_hash *hash_tfm;
91 	u8 *salt;
92 };
93 
94 struct iv_benbi_private {
95 	int shift;
96 };
97 
98 #define LMK_SEED_SIZE 64 /* hash + 0 */
99 struct iv_lmk_private {
100 	struct crypto_shash *hash_tfm;
101 	u8 *seed;
102 };
103 
104 /*
105  * Crypt: maps a linear range of a block device
106  * and encrypts / decrypts at the same time.
107  */
108 enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
109 
110 /*
111  * The fields in here must be read only after initialization.
112  */
113 struct crypt_config {
114 	struct dm_dev *dev;
115 	sector_t start;
116 
117 	/*
118 	 * pool for per bio private data, crypto requests and
119 	 * encryption requeusts/buffer pages
120 	 */
121 	mempool_t *req_pool;
122 	mempool_t *page_pool;
123 	struct bio_set *bs;
124 	struct mutex bio_alloc_lock;
125 
126 	struct workqueue_struct *io_queue;
127 	struct workqueue_struct *crypt_queue;
128 
129 	struct task_struct *write_thread;
130 	wait_queue_head_t write_thread_wait;
131 	struct rb_root write_tree;
132 
133 	char *cipher;
134 	char *cipher_string;
135 
136 	struct crypt_iv_operations *iv_gen_ops;
137 	union {
138 		struct iv_essiv_private essiv;
139 		struct iv_benbi_private benbi;
140 		struct iv_lmk_private lmk;
141 	} iv_gen_private;
142 	sector_t iv_offset;
143 	unsigned int iv_size;
144 
145 	/* ESSIV: struct crypto_cipher *essiv_tfm */
146 	void *iv_private;
147 	struct crypto_ablkcipher **tfms;
148 	unsigned tfms_count;
149 
150 	/*
151 	 * Layout of each crypto request:
152 	 *
153 	 *   struct ablkcipher_request
154 	 *      context
155 	 *      padding
156 	 *   struct dm_crypt_request
157 	 *      padding
158 	 *   IV
159 	 *
160 	 * The padding is added so that dm_crypt_request and the IV are
161 	 * correctly aligned.
162 	 */
163 	unsigned int dmreq_start;
164 
165 	unsigned int per_bio_data_size;
166 
167 	unsigned long flags;
168 	unsigned int key_size;
169 	unsigned int key_parts;
170 	u8 key[0];
171 };
172 
173 #define MIN_IOS        16
174 
175 static void clone_init(struct dm_crypt_io *, struct bio *);
176 static void kcryptd_queue_crypt(struct dm_crypt_io *io);
177 static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
178 
179 /*
180  * Use this to access cipher attributes that are the same for each CPU.
181  */
any_tfm(struct crypt_config * cc)182 static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
183 {
184 	return cc->tfms[0];
185 }
186 
187 /*
188  * Different IV generation algorithms:
189  *
190  * plain: the initial vector is the 32-bit little-endian version of the sector
191  *        number, padded with zeros if necessary.
192  *
193  * plain64: the initial vector is the 64-bit little-endian version of the sector
194  *        number, padded with zeros if necessary.
195  *
196  * essiv: "encrypted sector|salt initial vector", the sector number is
197  *        encrypted with the bulk cipher using a salt as key. The salt
198  *        should be derived from the bulk cipher's key via hashing.
199  *
200  * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
201  *        (needed for LRW-32-AES and possible other narrow block modes)
202  *
203  * null: the initial vector is always zero.  Provides compatibility with
204  *       obsolete loop_fish2 devices.  Do not use for new devices.
205  *
206  * lmk:  Compatible implementation of the block chaining mode used
207  *       by the Loop-AES block device encryption system
208  *       designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
209  *       It operates on full 512 byte sectors and uses CBC
210  *       with an IV derived from the sector number, the data and
211  *       optionally extra IV seed.
212  *       This means that after decryption the first block
213  *       of sector must be tweaked according to decrypted data.
214  *       Loop-AES can use three encryption schemes:
215  *         version 1: is plain aes-cbc mode
216  *         version 2: uses 64 multikey scheme with lmk IV generator
217  *         version 3: the same as version 2 with additional IV seed
218  *                   (it uses 65 keys, last key is used as IV seed)
219  *
220  * plumb: unimplemented, see:
221  * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
222  */
223 
crypt_iv_plain_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)224 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
225 			      struct dm_crypt_request *dmreq)
226 {
227 	memset(iv, 0, cc->iv_size);
228 	*(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
229 
230 	return 0;
231 }
232 
crypt_iv_plain64_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)233 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
234 				struct dm_crypt_request *dmreq)
235 {
236 	memset(iv, 0, cc->iv_size);
237 	*(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
238 
239 	return 0;
240 }
241 
242 /* Initialise ESSIV - compute salt but no local memory allocations */
crypt_iv_essiv_init(struct crypt_config * cc)243 static int crypt_iv_essiv_init(struct crypt_config *cc)
244 {
245 	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
246 	struct hash_desc desc;
247 	struct scatterlist sg;
248 	struct crypto_cipher *essiv_tfm;
249 	int err;
250 
251 	sg_init_one(&sg, cc->key, cc->key_size);
252 	desc.tfm = essiv->hash_tfm;
253 	desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
254 
255 	err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
256 	if (err)
257 		return err;
258 
259 	essiv_tfm = cc->iv_private;
260 
261 	err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
262 			    crypto_hash_digestsize(essiv->hash_tfm));
263 	if (err)
264 		return err;
265 
266 	return 0;
267 }
268 
269 /* Wipe salt and reset key derived from volume key */
crypt_iv_essiv_wipe(struct crypt_config * cc)270 static int crypt_iv_essiv_wipe(struct crypt_config *cc)
271 {
272 	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
273 	unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
274 	struct crypto_cipher *essiv_tfm;
275 	int r, err = 0;
276 
277 	memset(essiv->salt, 0, salt_size);
278 
279 	essiv_tfm = cc->iv_private;
280 	r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
281 	if (r)
282 		err = r;
283 
284 	return err;
285 }
286 
287 /* Set up per cpu cipher state */
setup_essiv_cpu(struct crypt_config * cc,struct dm_target * ti,u8 * salt,unsigned saltsize)288 static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
289 					     struct dm_target *ti,
290 					     u8 *salt, unsigned saltsize)
291 {
292 	struct crypto_cipher *essiv_tfm;
293 	int err;
294 
295 	/* Setup the essiv_tfm with the given salt */
296 	essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
297 	if (IS_ERR(essiv_tfm)) {
298 		ti->error = "Error allocating crypto tfm for ESSIV";
299 		return essiv_tfm;
300 	}
301 
302 	if (crypto_cipher_blocksize(essiv_tfm) !=
303 	    crypto_ablkcipher_ivsize(any_tfm(cc))) {
304 		ti->error = "Block size of ESSIV cipher does "
305 			    "not match IV size of block cipher";
306 		crypto_free_cipher(essiv_tfm);
307 		return ERR_PTR(-EINVAL);
308 	}
309 
310 	err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
311 	if (err) {
312 		ti->error = "Failed to set key for ESSIV cipher";
313 		crypto_free_cipher(essiv_tfm);
314 		return ERR_PTR(err);
315 	}
316 
317 	return essiv_tfm;
318 }
319 
crypt_iv_essiv_dtr(struct crypt_config * cc)320 static void crypt_iv_essiv_dtr(struct crypt_config *cc)
321 {
322 	struct crypto_cipher *essiv_tfm;
323 	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
324 
325 	crypto_free_hash(essiv->hash_tfm);
326 	essiv->hash_tfm = NULL;
327 
328 	kzfree(essiv->salt);
329 	essiv->salt = NULL;
330 
331 	essiv_tfm = cc->iv_private;
332 
333 	if (essiv_tfm)
334 		crypto_free_cipher(essiv_tfm);
335 
336 	cc->iv_private = NULL;
337 }
338 
crypt_iv_essiv_ctr(struct crypt_config * cc,struct dm_target * ti,const char * opts)339 static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
340 			      const char *opts)
341 {
342 	struct crypto_cipher *essiv_tfm = NULL;
343 	struct crypto_hash *hash_tfm = NULL;
344 	u8 *salt = NULL;
345 	int err;
346 
347 	if (!opts) {
348 		ti->error = "Digest algorithm missing for ESSIV mode";
349 		return -EINVAL;
350 	}
351 
352 	/* Allocate hash algorithm */
353 	hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
354 	if (IS_ERR(hash_tfm)) {
355 		ti->error = "Error initializing ESSIV hash";
356 		err = PTR_ERR(hash_tfm);
357 		goto bad;
358 	}
359 
360 	salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
361 	if (!salt) {
362 		ti->error = "Error kmallocing salt storage in ESSIV";
363 		err = -ENOMEM;
364 		goto bad;
365 	}
366 
367 	cc->iv_gen_private.essiv.salt = salt;
368 	cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
369 
370 	essiv_tfm = setup_essiv_cpu(cc, ti, salt,
371 				crypto_hash_digestsize(hash_tfm));
372 	if (IS_ERR(essiv_tfm)) {
373 		crypt_iv_essiv_dtr(cc);
374 		return PTR_ERR(essiv_tfm);
375 	}
376 	cc->iv_private = essiv_tfm;
377 
378 	return 0;
379 
380 bad:
381 	if (hash_tfm && !IS_ERR(hash_tfm))
382 		crypto_free_hash(hash_tfm);
383 	kfree(salt);
384 	return err;
385 }
386 
crypt_iv_essiv_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)387 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
388 			      struct dm_crypt_request *dmreq)
389 {
390 	struct crypto_cipher *essiv_tfm = cc->iv_private;
391 
392 	memset(iv, 0, cc->iv_size);
393 	*(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
394 	crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
395 
396 	return 0;
397 }
398 
crypt_iv_benbi_ctr(struct crypt_config * cc,struct dm_target * ti,const char * opts)399 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
400 			      const char *opts)
401 {
402 	unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
403 	int log = ilog2(bs);
404 
405 	/* we need to calculate how far we must shift the sector count
406 	 * to get the cipher block count, we use this shift in _gen */
407 
408 	if (1 << log != bs) {
409 		ti->error = "cypher blocksize is not a power of 2";
410 		return -EINVAL;
411 	}
412 
413 	if (log > 9) {
414 		ti->error = "cypher blocksize is > 512";
415 		return -EINVAL;
416 	}
417 
418 	cc->iv_gen_private.benbi.shift = 9 - log;
419 
420 	return 0;
421 }
422 
crypt_iv_benbi_dtr(struct crypt_config * cc)423 static void crypt_iv_benbi_dtr(struct crypt_config *cc)
424 {
425 }
426 
crypt_iv_benbi_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)427 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
428 			      struct dm_crypt_request *dmreq)
429 {
430 	__be64 val;
431 
432 	memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
433 
434 	val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
435 	put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
436 
437 	return 0;
438 }
439 
crypt_iv_null_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)440 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
441 			     struct dm_crypt_request *dmreq)
442 {
443 	memset(iv, 0, cc->iv_size);
444 
445 	return 0;
446 }
447 
crypt_iv_lmk_dtr(struct crypt_config * cc)448 static void crypt_iv_lmk_dtr(struct crypt_config *cc)
449 {
450 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
451 
452 	if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
453 		crypto_free_shash(lmk->hash_tfm);
454 	lmk->hash_tfm = NULL;
455 
456 	kzfree(lmk->seed);
457 	lmk->seed = NULL;
458 }
459 
crypt_iv_lmk_ctr(struct crypt_config * cc,struct dm_target * ti,const char * opts)460 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
461 			    const char *opts)
462 {
463 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
464 
465 	lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
466 	if (IS_ERR(lmk->hash_tfm)) {
467 		ti->error = "Error initializing LMK hash";
468 		return PTR_ERR(lmk->hash_tfm);
469 	}
470 
471 	/* No seed in LMK version 2 */
472 	if (cc->key_parts == cc->tfms_count) {
473 		lmk->seed = NULL;
474 		return 0;
475 	}
476 
477 	lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
478 	if (!lmk->seed) {
479 		crypt_iv_lmk_dtr(cc);
480 		ti->error = "Error kmallocing seed storage in LMK";
481 		return -ENOMEM;
482 	}
483 
484 	return 0;
485 }
486 
crypt_iv_lmk_init(struct crypt_config * cc)487 static int crypt_iv_lmk_init(struct crypt_config *cc)
488 {
489 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
490 	int subkey_size = cc->key_size / cc->key_parts;
491 
492 	/* LMK seed is on the position of LMK_KEYS + 1 key */
493 	if (lmk->seed)
494 		memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
495 		       crypto_shash_digestsize(lmk->hash_tfm));
496 
497 	return 0;
498 }
499 
crypt_iv_lmk_wipe(struct crypt_config * cc)500 static int crypt_iv_lmk_wipe(struct crypt_config *cc)
501 {
502 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
503 
504 	if (lmk->seed)
505 		memset(lmk->seed, 0, LMK_SEED_SIZE);
506 
507 	return 0;
508 }
509 
crypt_iv_lmk_one(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq,u8 * data)510 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
511 			    struct dm_crypt_request *dmreq,
512 			    u8 *data)
513 {
514 	struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
515 	struct {
516 		struct shash_desc desc;
517 		char ctx[crypto_shash_descsize(lmk->hash_tfm)];
518 	} sdesc;
519 	struct md5_state md5state;
520 	u32 buf[4];
521 	int i, r;
522 
523 	sdesc.desc.tfm = lmk->hash_tfm;
524 	sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
525 
526 	r = crypto_shash_init(&sdesc.desc);
527 	if (r)
528 		return r;
529 
530 	if (lmk->seed) {
531 		r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
532 		if (r)
533 			return r;
534 	}
535 
536 	/* Sector is always 512B, block size 16, add data of blocks 1-31 */
537 	r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
538 	if (r)
539 		return r;
540 
541 	/* Sector is cropped to 56 bits here */
542 	buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
543 	buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
544 	buf[2] = cpu_to_le32(4024);
545 	buf[3] = 0;
546 	r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
547 	if (r)
548 		return r;
549 
550 	/* No MD5 padding here */
551 	r = crypto_shash_export(&sdesc.desc, &md5state);
552 	if (r)
553 		return r;
554 
555 	for (i = 0; i < MD5_HASH_WORDS; i++)
556 		__cpu_to_le32s(&md5state.hash[i]);
557 	memcpy(iv, &md5state.hash, cc->iv_size);
558 
559 	return 0;
560 }
561 
crypt_iv_lmk_gen(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)562 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
563 			    struct dm_crypt_request *dmreq)
564 {
565 	u8 *src;
566 	int r = 0;
567 
568 	if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
569 		src = kmap_atomic(sg_page(&dmreq->sg_in));
570 		r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
571 		kunmap_atomic(src);
572 	} else
573 		memset(iv, 0, cc->iv_size);
574 
575 	return r;
576 }
577 
crypt_iv_lmk_post(struct crypt_config * cc,u8 * iv,struct dm_crypt_request * dmreq)578 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
579 			     struct dm_crypt_request *dmreq)
580 {
581 	u8 *dst;
582 	int r;
583 
584 	if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
585 		return 0;
586 
587 	dst = kmap_atomic(sg_page(&dmreq->sg_out));
588 	r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
589 
590 	/* Tweak the first block of plaintext sector */
591 	if (!r)
592 		crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
593 
594 	kunmap_atomic(dst);
595 	return r;
596 }
597 
598 static struct crypt_iv_operations crypt_iv_plain_ops = {
599 	.generator = crypt_iv_plain_gen
600 };
601 
602 static struct crypt_iv_operations crypt_iv_plain64_ops = {
603 	.generator = crypt_iv_plain64_gen
604 };
605 
606 static struct crypt_iv_operations crypt_iv_essiv_ops = {
607 	.ctr       = crypt_iv_essiv_ctr,
608 	.dtr       = crypt_iv_essiv_dtr,
609 	.init      = crypt_iv_essiv_init,
610 	.wipe      = crypt_iv_essiv_wipe,
611 	.generator = crypt_iv_essiv_gen
612 };
613 
614 static struct crypt_iv_operations crypt_iv_benbi_ops = {
615 	.ctr	   = crypt_iv_benbi_ctr,
616 	.dtr	   = crypt_iv_benbi_dtr,
617 	.generator = crypt_iv_benbi_gen
618 };
619 
620 static struct crypt_iv_operations crypt_iv_null_ops = {
621 	.generator = crypt_iv_null_gen
622 };
623 
624 static struct crypt_iv_operations crypt_iv_lmk_ops = {
625 	.ctr	   = crypt_iv_lmk_ctr,
626 	.dtr	   = crypt_iv_lmk_dtr,
627 	.init	   = crypt_iv_lmk_init,
628 	.wipe	   = crypt_iv_lmk_wipe,
629 	.generator = crypt_iv_lmk_gen,
630 	.post	   = crypt_iv_lmk_post
631 };
632 
crypt_convert_init(struct crypt_config * cc,struct convert_context * ctx,struct bio * bio_out,struct bio * bio_in,sector_t sector)633 static void crypt_convert_init(struct crypt_config *cc,
634 			       struct convert_context *ctx,
635 			       struct bio *bio_out, struct bio *bio_in,
636 			       sector_t sector)
637 {
638 	ctx->bio_in = bio_in;
639 	ctx->bio_out = bio_out;
640 	ctx->offset_in = 0;
641 	ctx->offset_out = 0;
642 	ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
643 	ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
644 	ctx->cc_sector = sector + cc->iv_offset;
645 	init_completion(&ctx->restart);
646 }
647 
dmreq_of_req(struct crypt_config * cc,struct ablkcipher_request * req)648 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
649 					     struct ablkcipher_request *req)
650 {
651 	return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
652 }
653 
req_of_dmreq(struct crypt_config * cc,struct dm_crypt_request * dmreq)654 static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
655 					       struct dm_crypt_request *dmreq)
656 {
657 	return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
658 }
659 
iv_of_dmreq(struct crypt_config * cc,struct dm_crypt_request * dmreq)660 static u8 *iv_of_dmreq(struct crypt_config *cc,
661 		       struct dm_crypt_request *dmreq)
662 {
663 	return (u8 *)ALIGN((unsigned long)(dmreq + 1),
664 		crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
665 }
666 
crypt_convert_block(struct crypt_config * cc,struct convert_context * ctx,struct ablkcipher_request * req)667 static int crypt_convert_block(struct crypt_config *cc,
668 			       struct convert_context *ctx,
669 			       struct ablkcipher_request *req)
670 {
671 	struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
672 	struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
673 	struct dm_crypt_request *dmreq;
674 	u8 *iv;
675 	int r;
676 
677 	dmreq = dmreq_of_req(cc, req);
678 	iv = iv_of_dmreq(cc, dmreq);
679 
680 	dmreq->iv_sector = ctx->cc_sector;
681 	dmreq->ctx = ctx;
682 	sg_init_table(&dmreq->sg_in, 1);
683 	sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
684 		    bv_in->bv_offset + ctx->offset_in);
685 
686 	sg_init_table(&dmreq->sg_out, 1);
687 	sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
688 		    bv_out->bv_offset + ctx->offset_out);
689 
690 	ctx->offset_in += 1 << SECTOR_SHIFT;
691 	if (ctx->offset_in >= bv_in->bv_len) {
692 		ctx->offset_in = 0;
693 		ctx->idx_in++;
694 	}
695 
696 	ctx->offset_out += 1 << SECTOR_SHIFT;
697 	if (ctx->offset_out >= bv_out->bv_len) {
698 		ctx->offset_out = 0;
699 		ctx->idx_out++;
700 	}
701 
702 	if (cc->iv_gen_ops) {
703 		r = cc->iv_gen_ops->generator(cc, iv, dmreq);
704 		if (r < 0)
705 			return r;
706 	}
707 
708 	ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
709 				     1 << SECTOR_SHIFT, iv);
710 
711 	if (bio_data_dir(ctx->bio_in) == WRITE)
712 		r = crypto_ablkcipher_encrypt(req);
713 	else
714 		r = crypto_ablkcipher_decrypt(req);
715 
716 	if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
717 		r = cc->iv_gen_ops->post(cc, iv, dmreq);
718 
719 	return r;
720 }
721 
722 static void kcryptd_async_done(struct crypto_async_request *async_req,
723 			       int error);
724 
crypt_alloc_req(struct crypt_config * cc,struct convert_context * ctx)725 static void crypt_alloc_req(struct crypt_config *cc,
726 			    struct convert_context *ctx)
727 {
728 	unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
729 
730 	if (!ctx->req)
731 		ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
732 
733 	ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
734 	ablkcipher_request_set_callback(ctx->req,
735 	    CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
736 	    kcryptd_async_done, dmreq_of_req(cc, ctx->req));
737 }
738 
crypt_free_req(struct crypt_config * cc,struct ablkcipher_request * req,struct bio * base_bio)739 static void crypt_free_req(struct crypt_config *cc,
740 			   struct ablkcipher_request *req, struct bio *base_bio)
741 {
742 	struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
743 
744 	if ((struct ablkcipher_request *)(io + 1) != req)
745 		mempool_free(req, cc->req_pool);
746 }
747 
748 /*
749  * Encrypt / decrypt data from one bio to another one (can be the same one)
750  */
crypt_convert(struct crypt_config * cc,struct convert_context * ctx)751 static int crypt_convert(struct crypt_config *cc,
752 			 struct convert_context *ctx)
753 {
754 	int r;
755 
756 	atomic_set(&ctx->cc_pending, 1);
757 
758 	while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
759 	      ctx->idx_out < ctx->bio_out->bi_vcnt) {
760 
761 		crypt_alloc_req(cc, ctx);
762 
763 		atomic_inc(&ctx->cc_pending);
764 
765 		r = crypt_convert_block(cc, ctx, ctx->req);
766 
767 		switch (r) {
768 		/* async */
769 		case -EBUSY:
770 			wait_for_completion(&ctx->restart);
771 			INIT_COMPLETION(ctx->restart);
772 			/* fall through*/
773 		case -EINPROGRESS:
774 			ctx->req = NULL;
775 			ctx->cc_sector++;
776 			continue;
777 
778 		/* sync */
779 		case 0:
780 			atomic_dec(&ctx->cc_pending);
781 			ctx->cc_sector++;
782 			cond_resched();
783 			continue;
784 
785 		/* error */
786 		default:
787 			atomic_dec(&ctx->cc_pending);
788 			return r;
789 		}
790 	}
791 
792 	return 0;
793 }
794 
795 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
796 
797 /*
798  * Generate a new unfragmented bio with the given size
799  * This should never violate the device limitations
800  *
801  * This function may be called concurrently. If we allocate from the mempool
802  * concurrently, there is a possibility of deadlock. For example, if we have
803  * mempool of 256 pages, two processes, each wanting 256, pages allocate from
804  * the mempool concurrently, it may deadlock in a situation where both processes
805  * have allocated 128 pages and the mempool is exhausted.
806  *
807  * In order to avoid this scenario we allocate the pages under a mutex.
808  *
809  * In order to not degrade performance with excessive locking, we try
810  * non-blocking allocations without a mutex first but on failure we fallback
811  * to blocking allocations with a mutex.
812  */
crypt_alloc_buffer(struct dm_crypt_io * io,unsigned size)813 static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
814 {
815 	struct crypt_config *cc = io->cc;
816 	struct bio *clone;
817 	unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
818 	gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
819 	unsigned i, len, remaining_size;
820 	struct page *page;
821 	struct bio_vec *bvec;
822 
823 retry:
824 	if (unlikely(gfp_mask & __GFP_WAIT))
825 		mutex_lock(&cc->bio_alloc_lock);
826 
827 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
828 	if (!clone)
829 		goto return_clone;
830 
831 	clone_init(io, clone);
832 
833 	remaining_size = size;
834 
835 	for (i = 0; i < nr_iovecs; i++) {
836 		page = mempool_alloc(cc->page_pool, gfp_mask);
837 		if (!page) {
838 			crypt_free_buffer_pages(cc, clone);
839 			bio_put(clone);
840 			gfp_mask |= __GFP_WAIT;
841 			goto retry;
842 		}
843 
844 		len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
845 
846 		bvec = &clone->bi_io_vec[clone->bi_vcnt++];
847 		bvec->bv_page = page;
848 		bvec->bv_len = len;
849 		bvec->bv_offset = 0;
850 
851 		clone->bi_size += len;
852 
853 		remaining_size -= len;
854 	}
855 
856 return_clone:
857 	if (unlikely(gfp_mask & __GFP_WAIT))
858 		mutex_unlock(&cc->bio_alloc_lock);
859 
860 	return clone;
861 }
862 
crypt_free_buffer_pages(struct crypt_config * cc,struct bio * clone)863 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
864 {
865 	unsigned int i;
866 	struct bio_vec *bv;
867 
868 	bio_for_each_segment_all(bv, clone, i) {
869 		BUG_ON(!bv->bv_page);
870 		mempool_free(bv->bv_page, cc->page_pool);
871 		bv->bv_page = NULL;
872 	}
873 }
874 
crypt_io_init(struct dm_crypt_io * io,struct crypt_config * cc,struct bio * bio,sector_t sector)875 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
876 			  struct bio *bio, sector_t sector)
877 {
878 	io->cc = cc;
879 	io->base_bio = bio;
880 	io->sector = sector;
881 	io->error = 0;
882 	io->ctx.req = NULL;
883 	atomic_set(&io->io_pending, 0);
884 }
885 
crypt_inc_pending(struct dm_crypt_io * io)886 static void crypt_inc_pending(struct dm_crypt_io *io)
887 {
888 	atomic_inc(&io->io_pending);
889 }
890 
891 /*
892  * One of the bios was finished. Check for completion of
893  * the whole request and correctly clean up the buffer.
894  */
crypt_dec_pending(struct dm_crypt_io * io)895 static void crypt_dec_pending(struct dm_crypt_io *io)
896 {
897 	struct crypt_config *cc = io->cc;
898 	struct bio *base_bio = io->base_bio;
899 	int error = io->error;
900 
901 	if (!atomic_dec_and_test(&io->io_pending))
902 		return;
903 
904 	if (io->ctx.req)
905 		crypt_free_req(cc, io->ctx.req, base_bio);
906 
907 	bio_endio(base_bio, error);
908 }
909 
910 /*
911  * kcryptd/kcryptd_io:
912  *
913  * Needed because it would be very unwise to do decryption in an
914  * interrupt context.
915  *
916  * kcryptd performs the actual encryption or decryption.
917  *
918  * kcryptd_io performs the IO submission.
919  *
920  * They must be separated as otherwise the final stages could be
921  * starved by new requests which can block in the first stages due
922  * to memory allocation.
923  *
924  * The work is done per CPU global for all dm-crypt instances.
925  * They should not depend on each other and do not block.
926  */
crypt_endio(struct bio * clone,int error)927 static void crypt_endio(struct bio *clone, int error)
928 {
929 	struct dm_crypt_io *io = clone->bi_private;
930 	struct crypt_config *cc = io->cc;
931 	unsigned rw = bio_data_dir(clone);
932 
933 	if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
934 		error = -EIO;
935 
936 	/*
937 	 * free the processed pages
938 	 */
939 	if (rw == WRITE)
940 		crypt_free_buffer_pages(cc, clone);
941 
942 	bio_put(clone);
943 
944 	if (rw == READ && !error) {
945 		kcryptd_queue_crypt(io);
946 		return;
947 	}
948 
949 	if (unlikely(error))
950 		io->error = error;
951 
952 	crypt_dec_pending(io);
953 }
954 
clone_init(struct dm_crypt_io * io,struct bio * clone)955 static void clone_init(struct dm_crypt_io *io, struct bio *clone)
956 {
957 	struct crypt_config *cc = io->cc;
958 
959 	clone->bi_private = io;
960 	clone->bi_end_io  = crypt_endio;
961 	clone->bi_bdev    = cc->dev->bdev;
962 	clone->bi_rw      = io->base_bio->bi_rw;
963 }
964 
kcryptd_io_read(struct dm_crypt_io * io,gfp_t gfp)965 static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
966 {
967 	struct crypt_config *cc = io->cc;
968 	struct bio *base_bio = io->base_bio;
969 	struct bio *clone;
970 
971 	/*
972 	 * The block layer might modify the bvec array, so always
973 	 * copy the required bvecs because we need the original
974 	 * one in order to decrypt the whole bio data *afterwards*.
975 	 */
976 	clone = bio_clone_bioset(base_bio, gfp, cc->bs);
977 	if (!clone)
978 		return 1;
979 
980 	crypt_inc_pending(io);
981 
982 	clone_init(io, clone);
983 	clone->bi_sector = cc->start + io->sector;
984 
985 	generic_make_request(clone);
986 	return 0;
987 }
988 
kcryptd_io_read_work(struct work_struct * work)989 static void kcryptd_io_read_work(struct work_struct *work)
990 {
991 	struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
992 
993 	crypt_inc_pending(io);
994 	if (kcryptd_io_read(io, GFP_NOIO))
995 		io->error = -ENOMEM;
996 	crypt_dec_pending(io);
997 }
998 
kcryptd_queue_read(struct dm_crypt_io * io)999 static void kcryptd_queue_read(struct dm_crypt_io *io)
1000 {
1001 	struct crypt_config *cc = io->cc;
1002 
1003 	INIT_WORK(&io->work, kcryptd_io_read_work);
1004 	queue_work(cc->io_queue, &io->work);
1005 }
1006 
kcryptd_io_write(struct dm_crypt_io * io)1007 static void kcryptd_io_write(struct dm_crypt_io *io)
1008 {
1009 	struct bio *clone = io->ctx.bio_out;
1010 
1011 	generic_make_request(clone);
1012 }
1013 
1014 #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1015 
dmcrypt_write(void * data)1016 static int dmcrypt_write(void *data)
1017 {
1018 	struct crypt_config *cc = data;
1019 	struct dm_crypt_io *io;
1020 
1021 	while (1) {
1022 		struct rb_root write_tree;
1023 		struct blk_plug plug;
1024 
1025 		DECLARE_WAITQUEUE(wait, current);
1026 
1027 		spin_lock_irq(&cc->write_thread_wait.lock);
1028 continue_locked:
1029 
1030 		if (!RB_EMPTY_ROOT(&cc->write_tree))
1031 			goto pop_from_list;
1032 
1033 		__set_current_state(TASK_INTERRUPTIBLE);
1034 		__add_wait_queue(&cc->write_thread_wait, &wait);
1035 
1036 		spin_unlock_irq(&cc->write_thread_wait.lock);
1037 
1038 		if (unlikely(kthread_should_stop())) {
1039 			set_task_state(current, TASK_RUNNING);
1040 			remove_wait_queue(&cc->write_thread_wait, &wait);
1041 			break;
1042 		}
1043 
1044 		schedule();
1045 
1046 		set_task_state(current, TASK_RUNNING);
1047 		spin_lock_irq(&cc->write_thread_wait.lock);
1048 		__remove_wait_queue(&cc->write_thread_wait, &wait);
1049 		goto continue_locked;
1050 
1051 pop_from_list:
1052 		write_tree = cc->write_tree;
1053 		cc->write_tree = RB_ROOT;
1054 		spin_unlock_irq(&cc->write_thread_wait.lock);
1055 
1056 		BUG_ON(rb_parent(write_tree.rb_node));
1057 
1058 		/*
1059 		 * Note: we cannot walk the tree here with rb_next because
1060 		 * the structures may be freed when kcryptd_io_write is called.
1061 		 */
1062 		blk_start_plug(&plug);
1063 		do {
1064 			io = crypt_io_from_node(rb_first(&write_tree));
1065 			rb_erase(&io->rb_node, &write_tree);
1066 			kcryptd_io_write(io);
1067 		} while (!RB_EMPTY_ROOT(&write_tree));
1068 		blk_finish_plug(&plug);
1069 	}
1070 	return 0;
1071 }
1072 
kcryptd_crypt_write_io_submit(struct dm_crypt_io * io,int async)1073 static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
1074 {
1075 	struct bio *clone = io->ctx.bio_out;
1076 	struct crypt_config *cc = io->cc;
1077 	unsigned long flags;
1078 	sector_t sector;
1079 	struct rb_node **rbp, *parent;
1080 
1081 	if (unlikely(io->error < 0)) {
1082 		crypt_free_buffer_pages(cc, clone);
1083 		bio_put(clone);
1084 		crypt_dec_pending(io);
1085 		return;
1086 	}
1087 
1088 	/* crypt_convert should have filled the clone bio */
1089 	BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
1090 
1091 	clone->bi_sector = cc->start + io->sector;
1092 
1093 	spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
1094 	rbp = &cc->write_tree.rb_node;
1095 	parent = NULL;
1096 	sector = io->sector;
1097 	while (*rbp) {
1098 		parent = *rbp;
1099 		if (sector < crypt_io_from_node(parent)->sector)
1100 			rbp = &(*rbp)->rb_left;
1101 		else
1102 			rbp = &(*rbp)->rb_right;
1103 	}
1104 	rb_link_node(&io->rb_node, parent, rbp);
1105 	rb_insert_color(&io->rb_node, &cc->write_tree);
1106 
1107 	wake_up_locked(&cc->write_thread_wait);
1108 	spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
1109 }
1110 
kcryptd_crypt_write_convert(struct dm_crypt_io * io)1111 static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
1112 {
1113 	struct crypt_config *cc = io->cc;
1114 	struct bio *clone;
1115 	int crypt_finished;
1116 	sector_t sector = io->sector;
1117 	int r;
1118 
1119 	/*
1120 	 * Prevent io from disappearing until this function completes.
1121 	 */
1122 	crypt_inc_pending(io);
1123 	crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
1124 
1125 	clone = crypt_alloc_buffer(io, io->base_bio->bi_size);
1126 	if (unlikely(!clone)) {
1127 		io->error = -EIO;
1128 		goto dec;
1129 	}
1130 
1131 	io->ctx.bio_out = clone;
1132 
1133 	sector += bio_sectors(clone);
1134 
1135 	crypt_inc_pending(io);
1136 	r = crypt_convert(cc, &io->ctx);
1137 	if (r)
1138 		io->error = -EIO;
1139 	crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1140 
1141 	/* Encryption was already finished, submit io now */
1142 	if (crypt_finished) {
1143 		kcryptd_crypt_write_io_submit(io, 0);
1144 		io->sector = sector;
1145 	}
1146 
1147 dec:
1148 	crypt_dec_pending(io);
1149 }
1150 
kcryptd_crypt_read_done(struct dm_crypt_io * io)1151 static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
1152 {
1153 	crypt_dec_pending(io);
1154 }
1155 
kcryptd_crypt_read_convert(struct dm_crypt_io * io)1156 static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
1157 {
1158 	struct crypt_config *cc = io->cc;
1159 	int r = 0;
1160 
1161 	crypt_inc_pending(io);
1162 
1163 	crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
1164 			   io->sector);
1165 
1166 	r = crypt_convert(cc, &io->ctx);
1167 	if (r < 0)
1168 		io->error = -EIO;
1169 
1170 	if (atomic_dec_and_test(&io->ctx.cc_pending))
1171 		kcryptd_crypt_read_done(io);
1172 
1173 	crypt_dec_pending(io);
1174 }
1175 
kcryptd_async_done(struct crypto_async_request * async_req,int error)1176 static void kcryptd_async_done(struct crypto_async_request *async_req,
1177 			       int error)
1178 {
1179 	struct dm_crypt_request *dmreq = async_req->data;
1180 	struct convert_context *ctx = dmreq->ctx;
1181 	struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1182 	struct crypt_config *cc = io->cc;
1183 
1184 	if (error == -EINPROGRESS) {
1185 		complete(&ctx->restart);
1186 		return;
1187 	}
1188 
1189 	if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1190 		error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1191 
1192 	if (error < 0)
1193 		io->error = -EIO;
1194 
1195 	crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
1196 
1197 	if (!atomic_dec_and_test(&ctx->cc_pending))
1198 		return;
1199 
1200 	if (bio_data_dir(io->base_bio) == READ)
1201 		kcryptd_crypt_read_done(io);
1202 	else
1203 		kcryptd_crypt_write_io_submit(io, 1);
1204 }
1205 
kcryptd_crypt(struct work_struct * work)1206 static void kcryptd_crypt(struct work_struct *work)
1207 {
1208 	struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1209 
1210 	if (bio_data_dir(io->base_bio) == READ)
1211 		kcryptd_crypt_read_convert(io);
1212 	else
1213 		kcryptd_crypt_write_convert(io);
1214 }
1215 
kcryptd_queue_crypt(struct dm_crypt_io * io)1216 static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1217 {
1218 	struct crypt_config *cc = io->cc;
1219 
1220 	INIT_WORK(&io->work, kcryptd_crypt);
1221 	queue_work(cc->crypt_queue, &io->work);
1222 }
1223 
1224 /*
1225  * Decode key from its hex representation
1226  */
crypt_decode_key(u8 * key,char * hex,unsigned int size)1227 static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1228 {
1229 	char buffer[3];
1230 	unsigned int i;
1231 
1232 	buffer[2] = '\0';
1233 
1234 	for (i = 0; i < size; i++) {
1235 		buffer[0] = *hex++;
1236 		buffer[1] = *hex++;
1237 
1238 		if (kstrtou8(buffer, 16, &key[i]))
1239 			return -EINVAL;
1240 	}
1241 
1242 	if (*hex != '\0')
1243 		return -EINVAL;
1244 
1245 	return 0;
1246 }
1247 
crypt_free_tfms(struct crypt_config * cc)1248 static void crypt_free_tfms(struct crypt_config *cc)
1249 {
1250 	unsigned i;
1251 
1252 	if (!cc->tfms)
1253 		return;
1254 
1255 	for (i = 0; i < cc->tfms_count; i++)
1256 		if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1257 			crypto_free_ablkcipher(cc->tfms[i]);
1258 			cc->tfms[i] = NULL;
1259 		}
1260 
1261 	kfree(cc->tfms);
1262 	cc->tfms = NULL;
1263 }
1264 
crypt_alloc_tfms(struct crypt_config * cc,char * ciphermode)1265 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1266 {
1267 	unsigned i;
1268 	int err;
1269 
1270 	cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1271 			   GFP_KERNEL);
1272 	if (!cc->tfms)
1273 		return -ENOMEM;
1274 
1275 	for (i = 0; i < cc->tfms_count; i++) {
1276 		cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1277 		if (IS_ERR(cc->tfms[i])) {
1278 			err = PTR_ERR(cc->tfms[i]);
1279 			crypt_free_tfms(cc);
1280 			return err;
1281 		}
1282 	}
1283 
1284 	return 0;
1285 }
1286 
crypt_setkey_allcpus(struct crypt_config * cc)1287 static int crypt_setkey_allcpus(struct crypt_config *cc)
1288 {
1289 	unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count);
1290 	int err = 0, i, r;
1291 
1292 	for (i = 0; i < cc->tfms_count; i++) {
1293 		r = crypto_ablkcipher_setkey(cc->tfms[i],
1294 					     cc->key + (i * subkey_size),
1295 					     subkey_size);
1296 		if (r)
1297 			err = r;
1298 	}
1299 
1300 	return err;
1301 }
1302 
crypt_set_key(struct crypt_config * cc,char * key)1303 static int crypt_set_key(struct crypt_config *cc, char *key)
1304 {
1305 	int r = -EINVAL;
1306 	int key_string_len = strlen(key);
1307 
1308 	/* The key size may not be changed. */
1309 	if (cc->key_size != (key_string_len >> 1))
1310 		goto out;
1311 
1312 	/* Hyphen (which gives a key_size of zero) means there is no key. */
1313 	if (!cc->key_size && strcmp(key, "-"))
1314 		goto out;
1315 
1316 	if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
1317 		goto out;
1318 
1319 	set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1320 
1321 	r = crypt_setkey_allcpus(cc);
1322 
1323 out:
1324 	/* Hex key string not needed after here, so wipe it. */
1325 	memset(key, '0', key_string_len);
1326 
1327 	return r;
1328 }
1329 
crypt_wipe_key(struct crypt_config * cc)1330 static int crypt_wipe_key(struct crypt_config *cc)
1331 {
1332 	clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1333 	memset(&cc->key, 0, cc->key_size * sizeof(u8));
1334 
1335 	return crypt_setkey_allcpus(cc);
1336 }
1337 
crypt_dtr(struct dm_target * ti)1338 static void crypt_dtr(struct dm_target *ti)
1339 {
1340 	struct crypt_config *cc = ti->private;
1341 
1342 	ti->private = NULL;
1343 
1344 	if (!cc)
1345 		return;
1346 
1347 	if (cc->write_thread)
1348 		kthread_stop(cc->write_thread);
1349 
1350 	if (cc->io_queue)
1351 		destroy_workqueue(cc->io_queue);
1352 	if (cc->crypt_queue)
1353 		destroy_workqueue(cc->crypt_queue);
1354 
1355 	crypt_free_tfms(cc);
1356 
1357 	if (cc->bs)
1358 		bioset_free(cc->bs);
1359 
1360 	if (cc->page_pool)
1361 		mempool_destroy(cc->page_pool);
1362 	if (cc->req_pool)
1363 		mempool_destroy(cc->req_pool);
1364 
1365 	if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1366 		cc->iv_gen_ops->dtr(cc);
1367 
1368 	if (cc->dev)
1369 		dm_put_device(ti, cc->dev);
1370 
1371 	kzfree(cc->cipher);
1372 	kzfree(cc->cipher_string);
1373 
1374 	/* Must zero key material before freeing */
1375 	kzfree(cc);
1376 }
1377 
crypt_ctr_cipher(struct dm_target * ti,char * cipher_in,char * key)1378 static int crypt_ctr_cipher(struct dm_target *ti,
1379 			    char *cipher_in, char *key)
1380 {
1381 	struct crypt_config *cc = ti->private;
1382 	char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
1383 	char *cipher_api = NULL;
1384 	int ret = -EINVAL;
1385 	char dummy;
1386 
1387 	/* Convert to crypto api definition? */
1388 	if (strchr(cipher_in, '(')) {
1389 		ti->error = "Bad cipher specification";
1390 		return -EINVAL;
1391 	}
1392 
1393 	cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1394 	if (!cc->cipher_string)
1395 		goto bad_mem;
1396 
1397 	/*
1398 	 * Legacy dm-crypt cipher specification
1399 	 * cipher[:keycount]-mode-iv:ivopts
1400 	 */
1401 	tmp = cipher_in;
1402 	keycount = strsep(&tmp, "-");
1403 	cipher = strsep(&keycount, ":");
1404 
1405 	if (!keycount)
1406 		cc->tfms_count = 1;
1407 	else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
1408 		 !is_power_of_2(cc->tfms_count)) {
1409 		ti->error = "Bad cipher key count specification";
1410 		return -EINVAL;
1411 	}
1412 	cc->key_parts = cc->tfms_count;
1413 
1414 	cc->cipher = kstrdup(cipher, GFP_KERNEL);
1415 	if (!cc->cipher)
1416 		goto bad_mem;
1417 
1418 	chainmode = strsep(&tmp, "-");
1419 	ivopts = strsep(&tmp, "-");
1420 	ivmode = strsep(&ivopts, ":");
1421 
1422 	if (tmp)
1423 		DMWARN("Ignoring unexpected additional cipher options");
1424 
1425 	/*
1426 	 * For compatibility with the original dm-crypt mapping format, if
1427 	 * only the cipher name is supplied, use cbc-plain.
1428 	 */
1429 	if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
1430 		chainmode = "cbc";
1431 		ivmode = "plain";
1432 	}
1433 
1434 	if (strcmp(chainmode, "ecb") && !ivmode) {
1435 		ti->error = "IV mechanism required";
1436 		return -EINVAL;
1437 	}
1438 
1439 	cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1440 	if (!cipher_api)
1441 		goto bad_mem;
1442 
1443 	ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1444 		       "%s(%s)", chainmode, cipher);
1445 	if (ret < 0) {
1446 		kfree(cipher_api);
1447 		goto bad_mem;
1448 	}
1449 
1450 	/* Allocate cipher */
1451 	ret = crypt_alloc_tfms(cc, cipher_api);
1452 	if (ret < 0) {
1453 		ti->error = "Error allocating crypto tfm";
1454 		goto bad;
1455 	}
1456 
1457 	/* Initialize and set key */
1458 	ret = crypt_set_key(cc, key);
1459 	if (ret < 0) {
1460 		ti->error = "Error decoding and setting key";
1461 		goto bad;
1462 	}
1463 
1464 	/* Initialize IV */
1465 	cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
1466 	if (cc->iv_size)
1467 		/* at least a 64 bit sector number should fit in our buffer */
1468 		cc->iv_size = max(cc->iv_size,
1469 				  (unsigned int)(sizeof(u64) / sizeof(u8)));
1470 	else if (ivmode) {
1471 		DMWARN("Selected cipher does not support IVs");
1472 		ivmode = NULL;
1473 	}
1474 
1475 	/* Choose ivmode, see comments at iv code. */
1476 	if (ivmode == NULL)
1477 		cc->iv_gen_ops = NULL;
1478 	else if (strcmp(ivmode, "plain") == 0)
1479 		cc->iv_gen_ops = &crypt_iv_plain_ops;
1480 	else if (strcmp(ivmode, "plain64") == 0)
1481 		cc->iv_gen_ops = &crypt_iv_plain64_ops;
1482 	else if (strcmp(ivmode, "essiv") == 0)
1483 		cc->iv_gen_ops = &crypt_iv_essiv_ops;
1484 	else if (strcmp(ivmode, "benbi") == 0)
1485 		cc->iv_gen_ops = &crypt_iv_benbi_ops;
1486 	else if (strcmp(ivmode, "null") == 0)
1487 		cc->iv_gen_ops = &crypt_iv_null_ops;
1488 	else if (strcmp(ivmode, "lmk") == 0) {
1489 		cc->iv_gen_ops = &crypt_iv_lmk_ops;
1490 		/* Version 2 and 3 is recognised according
1491 		 * to length of provided multi-key string.
1492 		 * If present (version 3), last key is used as IV seed.
1493 		 */
1494 		if (cc->key_size % cc->key_parts)
1495 			cc->key_parts++;
1496 	} else {
1497 		ret = -EINVAL;
1498 		ti->error = "Invalid IV mode";
1499 		goto bad;
1500 	}
1501 
1502 	/* Allocate IV */
1503 	if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1504 		ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1505 		if (ret < 0) {
1506 			ti->error = "Error creating IV";
1507 			goto bad;
1508 		}
1509 	}
1510 
1511 	/* Initialize IV (set keys for ESSIV etc) */
1512 	if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1513 		ret = cc->iv_gen_ops->init(cc);
1514 		if (ret < 0) {
1515 			ti->error = "Error initialising IV";
1516 			goto bad;
1517 		}
1518 	}
1519 
1520 	ret = 0;
1521 bad:
1522 	kfree(cipher_api);
1523 	return ret;
1524 
1525 bad_mem:
1526 	ti->error = "Cannot allocate cipher strings";
1527 	return -ENOMEM;
1528 }
1529 
1530 /*
1531  * Construct an encryption mapping:
1532  * <cipher> <key> <iv_offset> <dev_path> <start>
1533  */
crypt_ctr(struct dm_target * ti,unsigned int argc,char ** argv)1534 static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1535 {
1536 	struct crypt_config *cc;
1537 	unsigned int key_size, opt_params;
1538 	unsigned long long tmpll;
1539 	int ret;
1540 	size_t iv_size_padding;
1541 	struct dm_arg_set as;
1542 	const char *opt_string;
1543 	char dummy;
1544 
1545 	static struct dm_arg _args[] = {
1546 		{0, 1, "Invalid number of feature args"},
1547 	};
1548 
1549 	if (argc < 5) {
1550 		ti->error = "Not enough arguments";
1551 		return -EINVAL;
1552 	}
1553 
1554 	key_size = strlen(argv[1]) >> 1;
1555 
1556 	cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1557 	if (!cc) {
1558 		ti->error = "Cannot allocate encryption context";
1559 		return -ENOMEM;
1560 	}
1561 	cc->key_size = key_size;
1562 
1563 	ti->private = cc;
1564 	ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1565 	if (ret < 0)
1566 		goto bad;
1567 
1568 	cc->dmreq_start = sizeof(struct ablkcipher_request);
1569 	cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
1570 	cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
1571 
1572 	if (crypto_ablkcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
1573 		/* Allocate the padding exactly */
1574 		iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
1575 				& crypto_ablkcipher_alignmask(any_tfm(cc));
1576 	} else {
1577 		/*
1578 		 * If the cipher requires greater alignment than kmalloc
1579 		 * alignment, we don't know the exact position of the
1580 		 * initialization vector. We must assume worst case.
1581 		 */
1582 		iv_size_padding = crypto_ablkcipher_alignmask(any_tfm(cc));
1583 	}
1584 
1585 	ret = -ENOMEM;
1586 	cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1587 			sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
1588 	if (!cc->req_pool) {
1589 		ti->error = "Cannot allocate crypt request mempool";
1590 		goto bad;
1591 	}
1592 
1593 	cc->per_bio_data_size = ti->per_bio_data_size =
1594 		ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
1595 		      sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
1596 		      ARCH_KMALLOC_MINALIGN);
1597 
1598 	cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
1599 	if (!cc->page_pool) {
1600 		ti->error = "Cannot allocate page mempool";
1601 		goto bad;
1602 	}
1603 
1604 	cc->bs = bioset_create(MIN_IOS, 0);
1605 	if (!cc->bs) {
1606 		ti->error = "Cannot allocate crypt bioset";
1607 		goto bad;
1608 	}
1609 
1610 	mutex_init(&cc->bio_alloc_lock);
1611 
1612 	ret = -EINVAL;
1613 	if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
1614 		ti->error = "Invalid iv_offset sector";
1615 		goto bad;
1616 	}
1617 	cc->iv_offset = tmpll;
1618 
1619 	if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1620 		ti->error = "Device lookup failed";
1621 		goto bad;
1622 	}
1623 
1624 	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
1625 		ti->error = "Invalid device sector";
1626 		goto bad;
1627 	}
1628 	cc->start = tmpll;
1629 
1630 	argv += 5;
1631 	argc -= 5;
1632 
1633 	/* Optional parameters */
1634 	if (argc) {
1635 		as.argc = argc;
1636 		as.argv = argv;
1637 
1638 		ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1639 		if (ret)
1640 			goto bad;
1641 
1642 		opt_string = dm_shift_arg(&as);
1643 
1644 		if (opt_params == 1 && opt_string &&
1645 		    !strcasecmp(opt_string, "allow_discards"))
1646 			ti->num_discard_bios = 1;
1647 		else if (opt_params) {
1648 			ret = -EINVAL;
1649 			ti->error = "Invalid feature arguments";
1650 			goto bad;
1651 		}
1652 	}
1653 
1654 	ret = -ENOMEM;
1655 	cc->io_queue = alloc_workqueue("kcryptd_io",
1656 				       WQ_HIGHPRI |
1657 				       WQ_NON_REENTRANT|
1658 				       WQ_MEM_RECLAIM,
1659 				       1);
1660 	if (!cc->io_queue) {
1661 		ti->error = "Couldn't create kcryptd io queue";
1662 		goto bad;
1663 	}
1664 
1665 	cc->crypt_queue = alloc_workqueue("kcryptd",
1666 					  WQ_HIGHPRI |
1667 					  WQ_MEM_RECLAIM |
1668 					  WQ_UNBOUND, num_online_cpus());
1669 	if (!cc->crypt_queue) {
1670 		ti->error = "Couldn't create kcryptd queue";
1671 		goto bad;
1672 	}
1673 
1674 	init_waitqueue_head(&cc->write_thread_wait);
1675 	cc->write_tree = RB_ROOT;
1676 
1677 	cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
1678 	if (IS_ERR(cc->write_thread)) {
1679 		ret = PTR_ERR(cc->write_thread);
1680 		cc->write_thread = NULL;
1681 		ti->error = "Couldn't spawn write thread";
1682 		goto bad;
1683 	}
1684 	wake_up_process(cc->write_thread);
1685 
1686 	ti->num_flush_bios = 1;
1687 	ti->discard_zeroes_data_unsupported = true;
1688 
1689 	return 0;
1690 
1691 bad:
1692 	crypt_dtr(ti);
1693 	return ret;
1694 }
1695 
crypt_map(struct dm_target * ti,struct bio * bio)1696 static int crypt_map(struct dm_target *ti, struct bio *bio)
1697 {
1698 	struct dm_crypt_io *io;
1699 	struct crypt_config *cc = ti->private;
1700 
1701 	/*
1702 	 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1703 	 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1704 	 * - for REQ_DISCARD caller must use flush if IO ordering matters
1705 	 */
1706 	if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
1707 		bio->bi_bdev = cc->dev->bdev;
1708 		if (bio_sectors(bio))
1709 			bio->bi_sector = cc->start + dm_target_offset(ti, bio->bi_sector);
1710 		return DM_MAPIO_REMAPPED;
1711 	}
1712 
1713 	io = dm_per_bio_data(bio, cc->per_bio_data_size);
1714 	crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_sector));
1715 	io->ctx.req = (struct ablkcipher_request *)(io + 1);
1716 
1717 	if (bio_data_dir(io->base_bio) == READ) {
1718 		if (kcryptd_io_read(io, GFP_NOWAIT))
1719 			kcryptd_queue_read(io);
1720 	} else
1721 		kcryptd_queue_crypt(io);
1722 
1723 	return DM_MAPIO_SUBMITTED;
1724 }
1725 
crypt_status(struct dm_target * ti,status_type_t type,unsigned status_flags,char * result,unsigned maxlen)1726 static void crypt_status(struct dm_target *ti, status_type_t type,
1727 			 unsigned status_flags, char *result, unsigned maxlen)
1728 {
1729 	struct crypt_config *cc = ti->private;
1730 	unsigned i, sz = 0;
1731 
1732 	switch (type) {
1733 	case STATUSTYPE_INFO:
1734 		result[0] = '\0';
1735 		break;
1736 
1737 	case STATUSTYPE_TABLE:
1738 		DMEMIT("%s ", cc->cipher_string);
1739 
1740 		if (cc->key_size > 0)
1741 			for (i = 0; i < cc->key_size; i++)
1742 				DMEMIT("%02x", cc->key[i]);
1743 		else
1744 			DMEMIT("-");
1745 
1746 		DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1747 				cc->dev->name, (unsigned long long)cc->start);
1748 
1749 		if (ti->num_discard_bios)
1750 			DMEMIT(" 1 allow_discards");
1751 
1752 		break;
1753 	}
1754 }
1755 
crypt_postsuspend(struct dm_target * ti)1756 static void crypt_postsuspend(struct dm_target *ti)
1757 {
1758 	struct crypt_config *cc = ti->private;
1759 
1760 	set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1761 }
1762 
crypt_preresume(struct dm_target * ti)1763 static int crypt_preresume(struct dm_target *ti)
1764 {
1765 	struct crypt_config *cc = ti->private;
1766 
1767 	if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1768 		DMERR("aborting resume - crypt key is not set.");
1769 		return -EAGAIN;
1770 	}
1771 
1772 	return 0;
1773 }
1774 
crypt_resume(struct dm_target * ti)1775 static void crypt_resume(struct dm_target *ti)
1776 {
1777 	struct crypt_config *cc = ti->private;
1778 
1779 	clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1780 }
1781 
1782 /* Message interface
1783  *	key set <key>
1784  *	key wipe
1785  */
crypt_message(struct dm_target * ti,unsigned argc,char ** argv)1786 static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1787 {
1788 	struct crypt_config *cc = ti->private;
1789 	int ret = -EINVAL;
1790 
1791 	if (argc < 2)
1792 		goto error;
1793 
1794 	if (!strcasecmp(argv[0], "key")) {
1795 		if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1796 			DMWARN("not suspended during key manipulation.");
1797 			return -EINVAL;
1798 		}
1799 		if (argc == 3 && !strcasecmp(argv[1], "set")) {
1800 			ret = crypt_set_key(cc, argv[2]);
1801 			if (ret)
1802 				return ret;
1803 			if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1804 				ret = cc->iv_gen_ops->init(cc);
1805 			return ret;
1806 		}
1807 		if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
1808 			if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1809 				ret = cc->iv_gen_ops->wipe(cc);
1810 				if (ret)
1811 					return ret;
1812 			}
1813 			return crypt_wipe_key(cc);
1814 		}
1815 	}
1816 
1817 error:
1818 	DMWARN("unrecognised message received.");
1819 	return -EINVAL;
1820 }
1821 
crypt_merge(struct dm_target * ti,struct bvec_merge_data * bvm,struct bio_vec * biovec,int max_size)1822 static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1823 		       struct bio_vec *biovec, int max_size)
1824 {
1825 	struct crypt_config *cc = ti->private;
1826 	struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1827 
1828 	if (!q->merge_bvec_fn)
1829 		return max_size;
1830 
1831 	bvm->bi_bdev = cc->dev->bdev;
1832 	bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
1833 
1834 	return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1835 }
1836 
crypt_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)1837 static int crypt_iterate_devices(struct dm_target *ti,
1838 				 iterate_devices_callout_fn fn, void *data)
1839 {
1840 	struct crypt_config *cc = ti->private;
1841 
1842 	return fn(ti, cc->dev, cc->start, ti->len, data);
1843 }
1844 
1845 static struct target_type crypt_target = {
1846 	.name   = "crypt",
1847 	.version = {1, 12, 1},
1848 	.module = THIS_MODULE,
1849 	.ctr    = crypt_ctr,
1850 	.dtr    = crypt_dtr,
1851 	.map    = crypt_map,
1852 	.status = crypt_status,
1853 	.postsuspend = crypt_postsuspend,
1854 	.preresume = crypt_preresume,
1855 	.resume = crypt_resume,
1856 	.message = crypt_message,
1857 	.merge  = crypt_merge,
1858 	.iterate_devices = crypt_iterate_devices,
1859 };
1860 
dm_crypt_init(void)1861 static int __init dm_crypt_init(void)
1862 {
1863 	int r;
1864 
1865 	r = dm_register_target(&crypt_target);
1866 	if (r < 0)
1867 		DMERR("register failed %d", r);
1868 
1869 	return r;
1870 }
1871 
dm_crypt_exit(void)1872 static void __exit dm_crypt_exit(void)
1873 {
1874 	dm_unregister_target(&crypt_target);
1875 }
1876 
1877 module_init(dm_crypt_init);
1878 module_exit(dm_crypt_exit);
1879 
1880 MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1881 MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1882 MODULE_LICENSE("GPL");
1883