blob: da5db8b5237c4b1ea8c674ba2a823386abdc54f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Broz542da312009-12-10 23:51:57 +00004 * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Milan Broz43d69032008-02-08 02:11:09 +00009#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100010#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Andrew Morton3fcfab12006-10-19 23:28:16 -070020#include <linux/backing-dev.h>
Arun Sharma60063492011-07-26 16:09:06 -070021#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100022#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100024#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000025#include <crypto/hash.h>
26#include <crypto/md5.h>
27#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Mikulas Patocka586e80e2008-10-21 17:44:59 +010029#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * context holding the current state of a multi-part conversion
35 */
36struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000037 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct bio *bio_in;
39 struct bio *bio_out;
40 unsigned int offset_in;
41 unsigned int offset_out;
42 unsigned int idx_in;
43 unsigned int idx_out;
44 sector_t sector;
Mikulas Patocka815bc972012-03-27 13:17:56 -070045 atomic_t cc_pending;
Mikulas Patocka30c273a2012-03-27 14:16:33 -070046 struct ablkcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Milan Broz53017032008-02-08 02:10:38 +000049/*
50 * per bio private data
51 */
52struct dm_crypt_io {
53 struct dm_target *target;
54 struct bio *base_bio;
55 struct work_struct work;
56
57 struct convert_context ctx;
58
Mikulas Patocka815bc972012-03-27 13:17:56 -070059 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000060 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000061 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010062 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000063};
64
Milan Broz01482b72008-02-08 02:11:04 +000065struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000066 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000067 struct scatterlist sg_in;
68 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000069 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000070};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct crypt_config;
73
74struct crypt_iv_operations {
75 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010076 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000078 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000079 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000080 int (*generator)(struct crypt_config *cc, u8 *iv,
81 struct dm_crypt_request *dmreq);
82 int (*post)(struct crypt_config *cc, u8 *iv,
83 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Milan Broz60473592009-12-10 23:51:55 +000086struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000087 struct crypto_hash *hash_tfm;
88 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000089};
90
91struct iv_benbi_private {
92 int shift;
93};
94
Milan Broz34745782011-01-13 19:59:55 +000095#define LMK_SEED_SIZE 64 /* hash + 0 */
96struct iv_lmk_private {
97 struct crypto_shash *hash_tfm;
98 u8 *seed;
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * Crypt: maps a linear range of a block device
103 * and encrypts / decrypts at the same time.
104 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700105enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000106
107/*
Andi Kleenc0297722011-01-13 19:59:53 +0000108 * The fields in here must be read only after initialization,
Andi Kleenc0297722011-01-13 19:59:53 +0000109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct crypt_config {
111 struct dm_dev *dev;
112 sector_t start;
113
114 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000115 * pool for per bio private data, crypto requests and
116 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000119 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700121 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Milan Brozcabf08e2007-10-19 22:38:58 +0100123 struct workqueue_struct *io_queue;
124 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700125
Milan Broz5ebaee62010-08-12 04:14:07 +0100126 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000127 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800130 union {
Milan Broz60473592009-12-10 23:51:55 +0000131 struct iv_essiv_private essiv;
132 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000133 struct iv_lmk_private lmk;
Herbert Xu79066ad2006-12-05 13:41:52 -0800134 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 sector_t iv_offset;
136 unsigned int iv_size;
137
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700138 /* ESSIV: struct crypto_cipher *essiv_tfm */
139 void *iv_private;
140 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000141 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000142
143 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000144 * Layout of each crypto request:
145 *
146 * struct ablkcipher_request
147 * context
148 * padding
149 * struct dm_crypt_request
150 * padding
151 * IV
152 *
153 * The padding is added so that dm_crypt_request and the IV are
154 * correctly aligned.
155 */
156 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000157
Milan Broze48d4bb2006-10-03 01:15:37 -0700158 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned int key_size;
Milan Brozd1f96422011-01-13 19:59:54 +0000160 unsigned int key_parts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 u8 key[0];
162};
163
Milan Broz6a24c712006-10-03 01:15:40 -0700164#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define MIN_POOL_PAGES 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Lametere18b8902006-12-06 20:33:20 -0800167static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100169static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000170static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000171static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700172
Andi Kleenc0297722011-01-13 19:59:53 +0000173/*
174 * Use this to access cipher attributes that are the same for each CPU.
175 */
176static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
177{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700178 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Different IV generation algorithms:
183 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000184 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200185 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
Milan Broz61afef62009-12-10 23:52:25 +0000187 * plain64: the initial vector is the 64-bit little-endian version of the sector
188 * number, padded with zeros if necessary.
189 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000190 * essiv: "encrypted sector|salt initial vector", the sector number is
191 * encrypted with the bulk cipher using a salt as key. The salt
192 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Rik Snel48527fa2006-09-03 08:56:39 +1000194 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
195 * (needed for LRW-32-AES and possible other narrow block modes)
196 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700197 * null: the initial vector is always zero. Provides compatibility with
198 * obsolete loop_fish2 devices. Do not use for new devices.
199 *
Milan Broz34745782011-01-13 19:59:55 +0000200 * lmk: Compatible implementation of the block chaining mode used
201 * by the Loop-AES block device encryption system
202 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
203 * It operates on full 512 byte sectors and uses CBC
204 * with an IV derived from the sector number, the data and
205 * optionally extra IV seed.
206 * This means that after decryption the first block
207 * of sector must be tweaked according to decrypted data.
208 * Loop-AES can use three encryption schemes:
209 * version 1: is plain aes-cbc mode
210 * version 2: uses 64 multikey scheme with lmk IV generator
211 * version 3: the same as version 2 with additional IV seed
212 * (it uses 65 keys, last key is used as IV seed)
213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * plumb: unimplemented, see:
215 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
216 */
217
Milan Broz2dc53272011-01-13 19:59:54 +0000218static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
219 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100222 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 return 0;
225}
226
Milan Broz61afef62009-12-10 23:52:25 +0000227static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000228 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000229{
230 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100231 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000232
233 return 0;
234}
235
Milan Brozb95bf2d2009-12-10 23:51:56 +0000236/* Initialise ESSIV - compute salt but no local memory allocations */
237static int crypt_iv_essiv_init(struct crypt_config *cc)
238{
239 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
240 struct hash_desc desc;
241 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000242 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700243 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000244
245 sg_init_one(&sg, cc->key, cc->key_size);
246 desc.tfm = essiv->hash_tfm;
247 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
248
249 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
250 if (err)
251 return err;
252
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700253 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000254
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700255 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
256 crypto_hash_digestsize(essiv->hash_tfm));
257 if (err)
258 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000259
260 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000261}
262
Milan Broz542da312009-12-10 23:51:57 +0000263/* Wipe salt and reset key derived from volume key */
264static int crypt_iv_essiv_wipe(struct crypt_config *cc)
265{
266 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
267 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000268 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700269 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000270
271 memset(essiv->salt, 0, salt_size);
272
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700273 essiv_tfm = cc->iv_private;
274 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
275 if (r)
276 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000277
278 return err;
279}
280
281/* Set up per cpu cipher state */
282static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
283 struct dm_target *ti,
284 u8 *salt, unsigned saltsize)
285{
286 struct crypto_cipher *essiv_tfm;
287 int err;
288
289 /* Setup the essiv_tfm with the given salt */
290 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
291 if (IS_ERR(essiv_tfm)) {
292 ti->error = "Error allocating crypto tfm for ESSIV";
293 return essiv_tfm;
294 }
295
296 if (crypto_cipher_blocksize(essiv_tfm) !=
297 crypto_ablkcipher_ivsize(any_tfm(cc))) {
298 ti->error = "Block size of ESSIV cipher does "
299 "not match IV size of block cipher";
300 crypto_free_cipher(essiv_tfm);
301 return ERR_PTR(-EINVAL);
302 }
303
304 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
305 if (err) {
306 ti->error = "Failed to set key for ESSIV cipher";
307 crypto_free_cipher(essiv_tfm);
308 return ERR_PTR(err);
309 }
310
311 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000312}
313
Milan Broz60473592009-12-10 23:51:55 +0000314static void crypt_iv_essiv_dtr(struct crypt_config *cc)
315{
Andi Kleenc0297722011-01-13 19:59:53 +0000316 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000317 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
318
Milan Brozb95bf2d2009-12-10 23:51:56 +0000319 crypto_free_hash(essiv->hash_tfm);
320 essiv->hash_tfm = NULL;
321
322 kzfree(essiv->salt);
323 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000324
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700325 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000326
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700327 if (essiv_tfm)
328 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000329
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700330 cc->iv_private = NULL;
331
Milan Broz60473592009-12-10 23:51:55 +0000332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100335 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Milan Broz5861f1b2009-12-10 23:51:56 +0000337 struct crypto_cipher *essiv_tfm = NULL;
338 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000339 u8 *salt = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700340 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Milan Broz5861f1b2009-12-10 23:51:56 +0000342 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700343 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EINVAL;
345 }
346
Milan Brozb95bf2d2009-12-10 23:51:56 +0000347 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000348 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
349 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700350 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000351 err = PTR_ERR(hash_tfm);
352 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Milan Brozb95bf2d2009-12-10 23:51:56 +0000355 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000356 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700357 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000358 err = -ENOMEM;
359 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
Milan Brozb95bf2d2009-12-10 23:51:56 +0000362 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000363 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
364
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700365 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
366 crypto_hash_digestsize(hash_tfm));
367 if (IS_ERR(essiv_tfm)) {
368 crypt_iv_essiv_dtr(cc);
369 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000370 }
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700371 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000374
375bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000376 if (hash_tfm && !IS_ERR(hash_tfm))
377 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000378 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Milan Broz2dc53272011-01-13 19:59:54 +0000382static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
383 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700385
386 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100389 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000390 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393}
394
Rik Snel48527fa2006-09-03 08:56:39 +1000395static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
396 const char *opts)
397{
Andi Kleenc0297722011-01-13 19:59:53 +0000398 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800399 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000400
401 /* we need to calculate how far we must shift the sector count
402 * to get the cipher block count, we use this shift in _gen */
403
404 if (1 << log != bs) {
405 ti->error = "cypher blocksize is not a power of 2";
406 return -EINVAL;
407 }
408
409 if (log > 9) {
410 ti->error = "cypher blocksize is > 512";
411 return -EINVAL;
412 }
413
Milan Broz60473592009-12-10 23:51:55 +0000414 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000415
416 return 0;
417}
418
419static void crypt_iv_benbi_dtr(struct crypt_config *cc)
420{
Rik Snel48527fa2006-09-03 08:56:39 +1000421}
422
Milan Broz2dc53272011-01-13 19:59:54 +0000423static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
424 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000425{
Herbert Xu79066ad2006-12-05 13:41:52 -0800426 __be64 val;
427
Rik Snel48527fa2006-09-03 08:56:39 +1000428 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800429
Milan Broz2dc53272011-01-13 19:59:54 +0000430 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800431 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
Milan Broz2dc53272011-01-13 19:59:54 +0000436static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
437 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700438{
439 memset(iv, 0, cc->iv_size);
440
441 return 0;
442}
443
Milan Broz34745782011-01-13 19:59:55 +0000444static void crypt_iv_lmk_dtr(struct crypt_config *cc)
445{
446 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
447
448 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
449 crypto_free_shash(lmk->hash_tfm);
450 lmk->hash_tfm = NULL;
451
452 kzfree(lmk->seed);
453 lmk->seed = NULL;
454}
455
456static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
457 const char *opts)
458{
459 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
460
461 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
462 if (IS_ERR(lmk->hash_tfm)) {
463 ti->error = "Error initializing LMK hash";
464 return PTR_ERR(lmk->hash_tfm);
465 }
466
467 /* No seed in LMK version 2 */
468 if (cc->key_parts == cc->tfms_count) {
469 lmk->seed = NULL;
470 return 0;
471 }
472
473 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
474 if (!lmk->seed) {
475 crypt_iv_lmk_dtr(cc);
476 ti->error = "Error kmallocing seed storage in LMK";
477 return -ENOMEM;
478 }
479
480 return 0;
481}
482
483static int crypt_iv_lmk_init(struct crypt_config *cc)
484{
485 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
486 int subkey_size = cc->key_size / cc->key_parts;
487
488 /* LMK seed is on the position of LMK_KEYS + 1 key */
489 if (lmk->seed)
490 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
491 crypto_shash_digestsize(lmk->hash_tfm));
492
493 return 0;
494}
495
496static int crypt_iv_lmk_wipe(struct crypt_config *cc)
497{
498 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
499
500 if (lmk->seed)
501 memset(lmk->seed, 0, LMK_SEED_SIZE);
502
503 return 0;
504}
505
506static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
507 struct dm_crypt_request *dmreq,
508 u8 *data)
509{
510 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
511 struct {
512 struct shash_desc desc;
513 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
514 } sdesc;
515 struct md5_state md5state;
516 u32 buf[4];
517 int i, r;
518
519 sdesc.desc.tfm = lmk->hash_tfm;
520 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
521
522 r = crypto_shash_init(&sdesc.desc);
523 if (r)
524 return r;
525
526 if (lmk->seed) {
527 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
528 if (r)
529 return r;
530 }
531
532 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
533 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
534 if (r)
535 return r;
536
537 /* Sector is cropped to 56 bits here */
538 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
539 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
540 buf[2] = cpu_to_le32(4024);
541 buf[3] = 0;
542 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
543 if (r)
544 return r;
545
546 /* No MD5 padding here */
547 r = crypto_shash_export(&sdesc.desc, &md5state);
548 if (r)
549 return r;
550
551 for (i = 0; i < MD5_HASH_WORDS; i++)
552 __cpu_to_le32s(&md5state.hash[i]);
553 memcpy(iv, &md5state.hash, cc->iv_size);
554
555 return 0;
556}
557
558static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
559 struct dm_crypt_request *dmreq)
560{
561 u8 *src;
562 int r = 0;
563
564 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800565 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000566 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800567 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000568 } else
569 memset(iv, 0, cc->iv_size);
570
571 return r;
572}
573
574static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
575 struct dm_crypt_request *dmreq)
576{
577 u8 *dst;
578 int r;
579
580 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
581 return 0;
582
Cong Wangc2e022c2011-11-28 13:26:02 +0800583 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000584 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
585
586 /* Tweak the first block of plaintext sector */
587 if (!r)
588 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
589
Cong Wangc2e022c2011-11-28 13:26:02 +0800590 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000591 return r;
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static struct crypt_iv_operations crypt_iv_plain_ops = {
595 .generator = crypt_iv_plain_gen
596};
597
Milan Broz61afef62009-12-10 23:52:25 +0000598static struct crypt_iv_operations crypt_iv_plain64_ops = {
599 .generator = crypt_iv_plain64_gen
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static struct crypt_iv_operations crypt_iv_essiv_ops = {
603 .ctr = crypt_iv_essiv_ctr,
604 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000605 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000606 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 .generator = crypt_iv_essiv_gen
608};
609
Rik Snel48527fa2006-09-03 08:56:39 +1000610static struct crypt_iv_operations crypt_iv_benbi_ops = {
611 .ctr = crypt_iv_benbi_ctr,
612 .dtr = crypt_iv_benbi_dtr,
613 .generator = crypt_iv_benbi_gen
614};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Ludwig Nussel46b47732007-05-09 02:32:55 -0700616static struct crypt_iv_operations crypt_iv_null_ops = {
617 .generator = crypt_iv_null_gen
618};
619
Milan Broz34745782011-01-13 19:59:55 +0000620static struct crypt_iv_operations crypt_iv_lmk_ops = {
621 .ctr = crypt_iv_lmk_ctr,
622 .dtr = crypt_iv_lmk_dtr,
623 .init = crypt_iv_lmk_init,
624 .wipe = crypt_iv_lmk_wipe,
625 .generator = crypt_iv_lmk_gen,
626 .post = crypt_iv_lmk_post
627};
628
Milan Brozd469f842007-10-19 22:42:37 +0100629static void crypt_convert_init(struct crypt_config *cc,
630 struct convert_context *ctx,
631 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000632 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 ctx->bio_in = bio_in;
635 ctx->bio_out = bio_out;
636 ctx->offset_in = 0;
637 ctx->offset_out = 0;
638 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
639 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
640 ctx->sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000641 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Huang Yingb2174ee2009-03-16 17:44:33 +0000644static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
645 struct ablkcipher_request *req)
646{
647 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
648}
649
650static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
651 struct dm_crypt_request *dmreq)
652{
653 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
654}
655
Milan Broz2dc53272011-01-13 19:59:54 +0000656static u8 *iv_of_dmreq(struct crypt_config *cc,
657 struct dm_crypt_request *dmreq)
658{
659 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
660 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
661}
662
Milan Broz01482b72008-02-08 02:11:04 +0000663static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000664 struct convert_context *ctx,
665 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000666{
667 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
668 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000669 struct dm_crypt_request *dmreq;
670 u8 *iv;
671 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000672
Huang Yingb2174ee2009-03-16 17:44:33 +0000673 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000674 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000675
Milan Broz2dc53272011-01-13 19:59:54 +0000676 dmreq->iv_sector = ctx->sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000677 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000678 sg_init_table(&dmreq->sg_in, 1);
679 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000680 bv_in->bv_offset + ctx->offset_in);
681
Milan Broz3a7f6c92008-02-08 02:11:14 +0000682 sg_init_table(&dmreq->sg_out, 1);
683 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000684 bv_out->bv_offset + ctx->offset_out);
685
686 ctx->offset_in += 1 << SECTOR_SHIFT;
687 if (ctx->offset_in >= bv_in->bv_len) {
688 ctx->offset_in = 0;
689 ctx->idx_in++;
690 }
691
692 ctx->offset_out += 1 << SECTOR_SHIFT;
693 if (ctx->offset_out >= bv_out->bv_len) {
694 ctx->offset_out = 0;
695 ctx->idx_out++;
696 }
697
Milan Broz3a7f6c92008-02-08 02:11:14 +0000698 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000699 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000700 if (r < 0)
701 return r;
702 }
703
704 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
705 1 << SECTOR_SHIFT, iv);
706
707 if (bio_data_dir(ctx->bio_in) == WRITE)
708 r = crypto_ablkcipher_encrypt(req);
709 else
710 r = crypto_ablkcipher_decrypt(req);
711
Milan Broz2dc53272011-01-13 19:59:54 +0000712 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
713 r = cc->iv_gen_ops->post(cc, iv, dmreq);
714
Milan Broz3a7f6c92008-02-08 02:11:14 +0000715 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000716}
717
Milan Broz95497a92008-02-08 02:11:12 +0000718static void kcryptd_async_done(struct crypto_async_request *async_req,
719 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000720
Milan Brozddd42ed2008-02-08 02:11:07 +0000721static void crypt_alloc_req(struct crypt_config *cc,
722 struct convert_context *ctx)
723{
Milan Brozd1f96422011-01-13 19:59:54 +0000724 unsigned key_index = ctx->sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000725
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700726 if (!ctx->req)
727 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000728
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700729 ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
730 ablkcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000731 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700732 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Encrypt / decrypt data from one bio to another one (can be the same one)
737 */
738static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100739 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Milan Broz3f1e9072008-03-28 14:16:07 -0700741 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Mikulas Patocka815bc972012-03-27 13:17:56 -0700743 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
746 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Milan Broz3a7f6c92008-02-08 02:11:14 +0000748 crypt_alloc_req(cc, ctx);
749
Mikulas Patocka815bc972012-03-27 13:17:56 -0700750 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700751
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700752 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000753
754 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700755 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000756 case -EBUSY:
757 wait_for_completion(&ctx->restart);
758 INIT_COMPLETION(ctx->restart);
759 /* fall through*/
760 case -EINPROGRESS:
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700761 ctx->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000762 ctx->sector++;
763 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000764
Milan Broz3f1e9072008-03-28 14:16:07 -0700765 /* sync */
766 case 0:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700767 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700768 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100769 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700770 continue;
771
772 /* error */
773 default:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700774 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700775 return r;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
Milan Broz3f1e9072008-03-28 14:16:07 -0700779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Milan Brozd469f842007-10-19 22:42:37 +0100782static void dm_crypt_bio_destructor(struct bio *bio)
783{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100784 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700785 struct crypt_config *cc = io->target->private;
786
787 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100788}
Milan Broz6a24c712006-10-03 01:15:40 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/*
791 * Generate a new unfragmented bio with the given size
792 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100793 * May return a smaller bio when running out of pages, indicated by
794 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Milan Broz933f01d2008-10-10 13:37:08 +0100796static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
797 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Olaf Kirch027581f2007-05-09 02:32:52 -0700799 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700800 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400802 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000803 unsigned i, len;
804 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700806 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700807 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Olaf Kirch027581f2007-05-09 02:32:52 -0700810 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100811 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700812
Olaf Kirchf97380b2007-05-09 02:32:54 -0700813 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000814 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100815 if (!page) {
816 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /*
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +0100821 * If additional pages cannot be allocated without waiting,
822 * return a partially-allocated bio. The caller will then try
823 * to allocate more bios while submitting this partial bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +0100825 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Milan Broz91e10622007-12-13 14:16:10 +0000827 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Milan Broz91e10622007-12-13 14:16:10 +0000829 if (!bio_add_page(clone, page, len, 0)) {
830 mempool_free(page, cc->page_pool);
831 break;
832 }
833
834 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Milan Broz8b004452006-10-03 01:15:37 -0700837 if (!clone->bi_size) {
838 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return NULL;
840 }
841
Milan Broz8b004452006-10-03 01:15:37 -0700842 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Neil Brown644bd2f2007-10-16 13:48:46 +0200845static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Neil Brown644bd2f2007-10-16 13:48:46 +0200847 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct bio_vec *bv;
849
Neil Brown644bd2f2007-10-16 13:48:46 +0200850 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700851 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 BUG_ON(!bv->bv_page);
853 mempool_free(bv->bv_page, cc->page_pool);
854 bv->bv_page = NULL;
855 }
856}
857
Milan Brozdc440d1e2008-10-10 13:37:03 +0100858static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
859 struct bio *bio, sector_t sector)
860{
861 struct crypt_config *cc = ti->private;
862 struct dm_crypt_io *io;
863
864 io = mempool_alloc(cc->io_pool, GFP_NOIO);
865 io->target = ti;
866 io->base_bio = bio;
867 io->sector = sector;
868 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100869 io->base_io = NULL;
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700870 io->ctx.req = NULL;
Mikulas Patocka815bc972012-03-27 13:17:56 -0700871 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +0100872
873 return io;
874}
875
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100876static void crypt_inc_pending(struct dm_crypt_io *io)
877{
Mikulas Patocka815bc972012-03-27 13:17:56 -0700878 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * One of the bios was finished. Check for completion of
883 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100884 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Milan Broz5742fd72008-02-08 02:10:43 +0000886static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Milan Broz5742fd72008-02-08 02:10:43 +0000888 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000889 struct bio *base_bio = io->base_bio;
890 struct dm_crypt_io *base_io = io->base_io;
891 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Mikulas Patocka815bc972012-03-27 13:17:56 -0700893 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return;
895
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700896 if (io->ctx.req)
897 mempool_free(io->ctx.req, cc->req_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000899
900 if (likely(!base_io))
901 bio_endio(base_bio, error);
902 else {
903 if (error && !base_io->error)
904 base_io->error = error;
905 crypt_dec_pending(base_io);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100910 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
912 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700913 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100914 *
915 * kcryptd performs the actual encryption or decryption.
916 *
917 * kcryptd_io performs the IO submission.
918 *
919 * They must be separated as otherwise the final stages could be
920 * starved by new requests which can block in the first stages due
921 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +0000922 *
923 * The work is done per CPU global for all dm-crypt instances.
924 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200926static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700927{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100928 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700929 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000930 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700931
Milan Brozadfe4772007-12-13 14:15:51 +0000932 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
933 error = -EIO;
934
Milan Broz8b004452006-10-03 01:15:37 -0700935 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200936 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700937 */
Milan Brozee7a4912008-02-08 02:10:46 +0000938 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200939 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000940
941 bio_put(clone);
942
943 if (rw == READ && !error) {
944 kcryptd_queue_crypt(io);
945 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200946 }
Milan Broz8b004452006-10-03 01:15:37 -0700947
Milan Brozadfe4772007-12-13 14:15:51 +0000948 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000949 io->error = error;
950
951 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700952}
953
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100954static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700955{
956 struct crypt_config *cc = io->target->private;
957
958 clone->bi_private = io;
959 clone->bi_end_io = crypt_endio;
960 clone->bi_bdev = cc->dev->bdev;
961 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700962 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700963}
964
Milan Broz20c82532011-01-13 19:59:53 +0000965static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -0700966{
967 struct crypt_config *cc = io->target->private;
968 struct bio *base_bio = io->base_bio;
969 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700970
Milan Broz8b004452006-10-03 01:15:37 -0700971 /*
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 */
Milan Broz20c82532011-01-13 19:59:53 +0000976 clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100977 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +0000978 return 1;
Milan Broz8b004452006-10-03 01:15:37 -0700979
Milan Broz20c82532011-01-13 19:59:53 +0000980 crypt_inc_pending(io);
981
Milan Broz8b004452006-10-03 01:15:37 -0700982 clone_init(io, clone);
983 clone->bi_idx = 0;
984 clone->bi_vcnt = bio_segments(base_bio);
985 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000986 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700987 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
988 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700989
Milan Broz93e605c2006-10-03 01:15:38 -0700990 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +0000991 return 0;
Milan Broz8b004452006-10-03 01:15:37 -0700992}
993
Milan Broz4e4eef62008-02-08 02:10:49 +0000994static void kcryptd_io_write(struct dm_crypt_io *io)
995{
Milan Broz95497a92008-02-08 02:11:12 +0000996 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +0000997 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000998}
999
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001000static void kcryptd_io(struct work_struct *work)
1001{
1002 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1003
Milan Broz20c82532011-01-13 19:59:53 +00001004 if (bio_data_dir(io->base_bio) == READ) {
1005 crypt_inc_pending(io);
1006 if (kcryptd_io_read(io, GFP_NOIO))
1007 io->error = -ENOMEM;
1008 crypt_dec_pending(io);
1009 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001010 kcryptd_io_write(io);
1011}
1012
1013static void kcryptd_queue_io(struct dm_crypt_io *io)
1014{
1015 struct crypt_config *cc = io->target->private;
1016
1017 INIT_WORK(&io->work, kcryptd_io);
1018 queue_work(cc->io_queue, &io->work);
1019}
1020
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001021static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001022{
Milan Brozdec1ced2008-02-08 02:10:57 +00001023 struct bio *clone = io->ctx.bio_out;
1024 struct crypt_config *cc = io->target->private;
1025
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001026 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001027 crypt_free_buffer_pages(cc, clone);
1028 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001029 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001030 return;
1031 }
1032
1033 /* crypt_convert should have filled the clone bio */
1034 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
1035
1036 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001037
Milan Broz95497a92008-02-08 02:11:12 +00001038 if (async)
1039 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001040 else
Milan Broz95497a92008-02-08 02:11:12 +00001041 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001042}
1043
Milan Brozfc5a5e92008-10-10 13:37:04 +01001044static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001045{
1046 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -07001047 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001048 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001049 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001050 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +00001051 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001052 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001053 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001054
Milan Broz93e605c2006-10-03 01:15:38 -07001055 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001056 * Prevent io from disappearing until this function completes.
1057 */
1058 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001059 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001060
1061 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001062 * The allocated buffers can be smaller than the whole bio,
1063 * so repeat the whole process until all the data can be handled.
1064 */
1065 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001066 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001067 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001068 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001069 break;
Milan Broz23541d22006-10-03 01:15:39 -07001070 }
Milan Broz93e605c2006-10-03 01:15:38 -07001071
Milan Broz53017032008-02-08 02:10:38 +00001072 io->ctx.bio_out = clone;
1073 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -07001074
Milan Broz93e605c2006-10-03 01:15:38 -07001075 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001076 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001077
Milan Broz4e594092008-10-10 13:37:07 +01001078 crypt_inc_pending(io);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001079
Milan Brozdec1ced2008-02-08 02:10:57 +00001080 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001081 if (r < 0)
1082 io->error = -EIO;
Mikulas Patocka815bc972012-03-27 13:17:56 -07001083 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001084
Milan Brozc8081612008-10-10 13:37:08 +01001085 /* Encryption was already finished, submit io now */
1086 if (crypt_finished) {
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001087 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001088
1089 /*
1090 * If there was an error, do not try next fragments.
1091 * For async, error is processed in async handler.
1092 */
Milan Broz6c031f42008-10-10 13:37:06 +01001093 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001094 break;
Milan Brozb635b002008-10-21 17:45:00 +01001095
1096 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001097 }
Milan Broz93e605c2006-10-03 01:15:38 -07001098
Milan Broz933f01d2008-10-10 13:37:08 +01001099 /*
1100 * Out of memory -> run queues
1101 * But don't wait if split was due to the io size restriction
1102 */
1103 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001104 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001105
Milan Broz393b47e2008-10-21 17:45:02 +01001106 /*
1107 * With async crypto it is unsafe to share the crypto context
1108 * between fragments, so switch to a new dm_crypt_io structure.
1109 */
1110 if (unlikely(!crypt_finished && remaining)) {
1111 new_io = crypt_io_alloc(io->target, io->base_bio,
1112 sector);
1113 crypt_inc_pending(new_io);
1114 crypt_convert_init(cc, &new_io->ctx, NULL,
1115 io->base_bio, sector);
1116 new_io->ctx.idx_in = io->ctx.idx_in;
1117 new_io->ctx.offset_in = io->ctx.offset_in;
1118
1119 /*
1120 * Fragments after the first use the base_io
1121 * pending count.
1122 */
1123 if (!io->base_io)
1124 new_io->base_io = io;
1125 else {
1126 new_io->base_io = io->base_io;
1127 crypt_inc_pending(io->base_io);
1128 crypt_dec_pending(io);
1129 }
1130
1131 io = new_io;
1132 }
Milan Broz8b004452006-10-03 01:15:37 -07001133 }
Milan Broz899c95d2008-02-08 02:11:02 +00001134
1135 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001136}
1137
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001138static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001139{
Milan Broz5742fd72008-02-08 02:10:43 +00001140 crypt_dec_pending(io);
1141}
1142
Milan Broz4e4eef62008-02-08 02:10:49 +00001143static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001144{
1145 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +00001146 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001147
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001148 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001149
Milan Broz53017032008-02-08 02:10:38 +00001150 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001151 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001152
Milan Broz5742fd72008-02-08 02:10:43 +00001153 r = crypt_convert(cc, &io->ctx);
1154
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001155 if (r < 0)
1156 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001157
Mikulas Patocka815bc972012-03-27 13:17:56 -07001158 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001159 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001160
1161 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001162}
1163
Milan Broz95497a92008-02-08 02:11:12 +00001164static void kcryptd_async_done(struct crypto_async_request *async_req,
1165 int error)
1166{
Huang Yingb2174ee2009-03-16 17:44:33 +00001167 struct dm_crypt_request *dmreq = async_req->data;
1168 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001169 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1170 struct crypt_config *cc = io->target->private;
1171
1172 if (error == -EINPROGRESS) {
1173 complete(&ctx->restart);
1174 return;
1175 }
1176
Milan Broz2dc53272011-01-13 19:59:54 +00001177 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1178 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1179
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001180 if (error < 0)
1181 io->error = -EIO;
1182
Huang Yingb2174ee2009-03-16 17:44:33 +00001183 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001184
Mikulas Patocka815bc972012-03-27 13:17:56 -07001185 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001186 return;
1187
1188 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001189 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001190 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001191 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001192}
1193
Milan Broz4e4eef62008-02-08 02:10:49 +00001194static void kcryptd_crypt(struct work_struct *work)
1195{
1196 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1197
1198 if (bio_data_dir(io->base_bio) == READ)
1199 kcryptd_crypt_read_convert(io);
1200 else
1201 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001202}
1203
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001204static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1205{
1206 struct crypt_config *cc = io->target->private;
1207
1208 INIT_WORK(&io->work, kcryptd_crypt);
1209 queue_work(cc->crypt_queue, &io->work);
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/*
1213 * Decode key from its hex representation
1214 */
1215static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1216{
1217 char buffer[3];
1218 char *endp;
1219 unsigned int i;
1220
1221 buffer[2] = '\0';
1222
Milan Broz8b004452006-10-03 01:15:37 -07001223 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 buffer[0] = *hex++;
1225 buffer[1] = *hex++;
1226
1227 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
1228
1229 if (endp != &buffer[2])
1230 return -EINVAL;
1231 }
1232
1233 if (*hex != '\0')
1234 return -EINVAL;
1235
1236 return 0;
1237}
1238
1239/*
1240 * Encode key into its hex representation
1241 */
1242static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
1243{
1244 unsigned int i;
1245
Milan Broz8b004452006-10-03 01:15:37 -07001246 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 sprintf(hex, "%02x", *key);
1248 hex += 2;
1249 key++;
1250 }
1251}
1252
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001253static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001254{
Milan Brozd1f96422011-01-13 19:59:54 +00001255 unsigned i;
1256
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001257 if (!cc->tfms)
1258 return;
1259
Milan Brozd1f96422011-01-13 19:59:54 +00001260 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001261 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1262 crypto_free_ablkcipher(cc->tfms[i]);
1263 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001264 }
1265}
1266
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001267static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001268{
Milan Brozd1f96422011-01-13 19:59:54 +00001269 unsigned i;
1270 int err;
1271
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001272 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1273 GFP_KERNEL);
1274 if (!cc->tfms)
1275 return -ENOMEM;
1276
Milan Brozd1f96422011-01-13 19:59:54 +00001277 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001278 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1279 if (IS_ERR(cc->tfms[i])) {
1280 err = PTR_ERR(cc->tfms[i]);
1281 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001282 return err;
1283 }
1284 }
1285
1286 return 0;
1287}
1288
Andi Kleenc0297722011-01-13 19:59:53 +00001289static int crypt_setkey_allcpus(struct crypt_config *cc)
1290{
Milan Brozd1f96422011-01-13 19:59:54 +00001291 unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count);
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001292 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001293
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001294 for (i = 0; i < cc->tfms_count; i++) {
1295 r = crypto_ablkcipher_setkey(cc->tfms[i],
1296 cc->key + (i * subkey_size),
1297 subkey_size);
1298 if (r)
1299 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001300 }
1301
1302 return err;
1303}
1304
Milan Broze48d4bb2006-10-03 01:15:37 -07001305static int crypt_set_key(struct crypt_config *cc, char *key)
1306{
Milan Brozde8be5a2011-03-24 13:54:27 +00001307 int r = -EINVAL;
1308 int key_string_len = strlen(key);
1309
Milan Broz69a8cfc2011-01-13 19:59:49 +00001310 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001311 if (cc->key_size != (key_string_len >> 1))
1312 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001313
Milan Broz69a8cfc2011-01-13 19:59:49 +00001314 /* Hyphen (which gives a key_size of zero) means there is no key. */
1315 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001316 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001317
Milan Broz69a8cfc2011-01-13 19:59:49 +00001318 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001319 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001320
1321 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1322
Milan Brozde8be5a2011-03-24 13:54:27 +00001323 r = crypt_setkey_allcpus(cc);
1324
1325out:
1326 /* Hex key string not needed after here, so wipe it. */
1327 memset(key, '0', key_string_len);
1328
1329 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001330}
1331
1332static int crypt_wipe_key(struct crypt_config *cc)
1333{
1334 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1335 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001336
1337 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001338}
1339
Milan Broz28513fc2010-08-12 04:14:06 +01001340static void crypt_dtr(struct dm_target *ti)
1341{
1342 struct crypt_config *cc = ti->private;
1343
1344 ti->private = NULL;
1345
1346 if (!cc)
1347 return;
1348
1349 if (cc->io_queue)
1350 destroy_workqueue(cc->io_queue);
1351 if (cc->crypt_queue)
1352 destroy_workqueue(cc->crypt_queue);
1353
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001354 crypt_free_tfms(cc);
Andi Kleenc0297722011-01-13 19:59:53 +00001355
Milan Broz28513fc2010-08-12 04:14:06 +01001356 if (cc->bs)
1357 bioset_free(cc->bs);
1358
1359 if (cc->page_pool)
1360 mempool_destroy(cc->page_pool);
1361 if (cc->req_pool)
1362 mempool_destroy(cc->req_pool);
1363 if (cc->io_pool)
1364 mempool_destroy(cc->io_pool);
1365
1366 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1367 cc->iv_gen_ops->dtr(cc);
1368
Milan Broz28513fc2010-08-12 04:14:06 +01001369 if (cc->dev)
1370 dm_put_device(ti, cc->dev);
1371
Milan Broz5ebaee62010-08-12 04:14:07 +01001372 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001373 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001374
1375 /* Must zero key material before freeing */
1376 kzfree(cc);
1377}
1378
Milan Broz5ebaee62010-08-12 04:14:07 +01001379static int crypt_ctr_cipher(struct dm_target *ti,
1380 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Milan Broz5ebaee62010-08-12 04:14:07 +01001382 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001383 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001384 char *cipher_api = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001385 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001386 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Milan Broz5ebaee62010-08-12 04:14:07 +01001388 /* Convert to crypto api definition? */
1389 if (strchr(cipher_in, '(')) {
1390 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EINVAL;
1392 }
1393
Milan Broz7dbcd132011-01-13 19:59:52 +00001394 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1395 if (!cc->cipher_string)
1396 goto bad_mem;
1397
Milan Broz5ebaee62010-08-12 04:14:07 +01001398 /*
1399 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001400 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001401 */
1402 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001403 keycount = strsep(&tmp, "-");
1404 cipher = strsep(&keycount, ":");
1405
1406 if (!keycount)
1407 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001408 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001409 !is_power_of_2(cc->tfms_count)) {
1410 ti->error = "Bad cipher key count specification";
1411 return -EINVAL;
1412 }
1413 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01001414
1415 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1416 if (!cc->cipher)
1417 goto bad_mem;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 chainmode = strsep(&tmp, "-");
1420 ivopts = strsep(&tmp, "-");
1421 ivmode = strsep(&ivopts, ":");
1422
1423 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001424 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Milan Broz7dbcd132011-01-13 19:59:52 +00001426 /*
1427 * For compatibility with the original dm-crypt mapping format, if
1428 * only the cipher name is supplied, use cbc-plain.
1429 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001430 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 chainmode = "cbc";
1432 ivmode = "plain";
1433 }
1434
Herbert Xud1806f62006-08-22 20:29:17 +10001435 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001436 ti->error = "IV mechanism required";
1437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Milan Broz5ebaee62010-08-12 04:14:07 +01001440 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1441 if (!cipher_api)
1442 goto bad_mem;
1443
1444 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1445 "%s(%s)", chainmode, cipher);
1446 if (ret < 0) {
1447 kfree(cipher_api);
1448 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001449 }
1450
Milan Broz5ebaee62010-08-12 04:14:07 +01001451 /* Allocate cipher */
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001452 ret = crypt_alloc_tfms(cc, cipher_api);
1453 if (ret < 0) {
1454 ti->error = "Error allocating crypto tfm";
1455 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Milan Broz5ebaee62010-08-12 04:14:07 +01001458 /* Initialize and set key */
1459 ret = crypt_set_key(cc, key);
Milan Broz28513fc2010-08-12 04:14:06 +01001460 if (ret < 0) {
Milan Broz0b430952009-12-10 23:51:55 +00001461 ti->error = "Error decoding and setting key";
Milan Broz28513fc2010-08-12 04:14:06 +01001462 goto bad;
Milan Broz0b430952009-12-10 23:51:55 +00001463 }
1464
Milan Broz5ebaee62010-08-12 04:14:07 +01001465 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001466 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001467 if (cc->iv_size)
1468 /* at least a 64 bit sector number should fit in our buffer */
1469 cc->iv_size = max(cc->iv_size,
1470 (unsigned int)(sizeof(u64) / sizeof(u8)));
1471 else if (ivmode) {
1472 DMWARN("Selected cipher does not support IVs");
1473 ivmode = NULL;
1474 }
1475
1476 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (ivmode == NULL)
1478 cc->iv_gen_ops = NULL;
1479 else if (strcmp(ivmode, "plain") == 0)
1480 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001481 else if (strcmp(ivmode, "plain64") == 0)
1482 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 else if (strcmp(ivmode, "essiv") == 0)
1484 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001485 else if (strcmp(ivmode, "benbi") == 0)
1486 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001487 else if (strcmp(ivmode, "null") == 0)
1488 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001489 else if (strcmp(ivmode, "lmk") == 0) {
1490 cc->iv_gen_ops = &crypt_iv_lmk_ops;
1491 /* Version 2 and 3 is recognised according
1492 * to length of provided multi-key string.
1493 * If present (version 3), last key is used as IV seed.
1494 */
1495 if (cc->key_size % cc->key_parts)
1496 cc->key_parts++;
1497 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001498 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001499 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001500 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502
Milan Broz28513fc2010-08-12 04:14:06 +01001503 /* Allocate IV */
1504 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1505 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1506 if (ret < 0) {
1507 ti->error = "Error creating IV";
1508 goto bad;
1509 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001510 }
1511
Milan Broz28513fc2010-08-12 04:14:06 +01001512 /* Initialize IV (set keys for ESSIV etc) */
1513 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1514 ret = cc->iv_gen_ops->init(cc);
1515 if (ret < 0) {
1516 ti->error = "Error initialising IV";
1517 goto bad;
1518 }
1519 }
1520
Milan Broz5ebaee62010-08-12 04:14:07 +01001521 ret = 0;
1522bad:
1523 kfree(cipher_api);
1524 return ret;
1525
1526bad_mem:
1527 ti->error = "Cannot allocate cipher strings";
1528 return -ENOMEM;
1529}
1530
1531/*
1532 * Construct an encryption mapping:
1533 * <cipher> <key> <iv_offset> <dev_path> <start>
1534 */
1535static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1536{
1537 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001538 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001539 unsigned long long tmpll;
1540 int ret;
Milan Broz772ae5f2011-08-02 12:32:08 +01001541 struct dm_arg_set as;
1542 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001543 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001544
Milan Broz772ae5f2011-08-02 12:32:08 +01001545 static struct dm_arg _args[] = {
1546 {0, 1, "Invalid number of feature args"},
1547 };
1548
1549 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001550 ti->error = "Not enough arguments";
1551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
Milan Broz5ebaee62010-08-12 04:14:07 +01001554 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 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001561 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001562
1563 ti->private = cc;
1564 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1565 if (ret < 0)
1566 goto bad;
1567
Milan Broz28513fc2010-08-12 04:14:06 +01001568 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001569 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001571 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001572 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
Milan Brozddd42ed2008-02-08 02:11:07 +00001575 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001576 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001577 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001578 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001579 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001580
1581 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1582 sizeof(struct dm_crypt_request) + cc->iv_size);
1583 if (!cc->req_pool) {
1584 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001585 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001586 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001587
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001588 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001590 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001591 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Jens Axboebb799ca2008-12-10 15:35:05 +01001594 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001595 if (!cc->bs) {
1596 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001597 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001598 }
1599
Milan Broz28513fc2010-08-12 04:14:06 +01001600 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001601 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001602 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001603 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001605 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Milan Broz28513fc2010-08-12 04:14:06 +01001607 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1608 ti->error = "Device lookup failed";
1609 goto bad;
1610 }
1611
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001612 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001613 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001614 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001616 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Milan Broz772ae5f2011-08-02 12:32:08 +01001618 argv += 5;
1619 argc -= 5;
1620
1621 /* Optional parameters */
1622 if (argc) {
1623 as.argc = argc;
1624 as.argv = argv;
1625
1626 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1627 if (ret)
1628 goto bad;
1629
1630 opt_string = dm_shift_arg(&as);
1631
1632 if (opt_params == 1 && opt_string &&
1633 !strcasecmp(opt_string, "allow_discards"))
1634 ti->num_discard_requests = 1;
1635 else if (opt_params) {
1636 ret = -EINVAL;
1637 ti->error = "Invalid feature arguments";
1638 goto bad;
1639 }
1640 }
1641
Milan Broz28513fc2010-08-12 04:14:06 +01001642 ret = -ENOMEM;
Andi Kleenc0297722011-01-13 19:59:53 +00001643 cc->io_queue = alloc_workqueue("kcryptd_io",
1644 WQ_NON_REENTRANT|
1645 WQ_MEM_RECLAIM,
1646 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001647 if (!cc->io_queue) {
1648 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001649 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001650 }
1651
Andi Kleenc0297722011-01-13 19:59:53 +00001652 cc->crypt_queue = alloc_workqueue("kcryptd",
1653 WQ_NON_REENTRANT|
1654 WQ_CPU_INTENSIVE|
1655 WQ_MEM_RECLAIM,
1656 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001657 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001658 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001659 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001660 }
1661
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001662 ti->num_flush_requests = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01001663 ti->discard_zeroes_data_unsupported = 1;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
1666
Milan Broz28513fc2010-08-12 04:14:06 +01001667bad:
1668 crypt_dtr(ti);
1669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672static int crypt_map(struct dm_target *ti, struct bio *bio,
1673 union map_info *map_context)
1674{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001675 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001676 struct crypt_config *cc;
1677
Milan Broz772ae5f2011-08-02 12:32:08 +01001678 /*
1679 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1680 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1681 * - for REQ_DISCARD caller must use flush if IO ordering matters
1682 */
1683 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001684 cc = ti->private;
1685 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001686 if (bio_sectors(bio))
1687 bio->bi_sector = cc->start + dm_target_offset(ti, bio->bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001688 return DM_MAPIO_REMAPPED;
1689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001691 io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001692
Milan Broz20c82532011-01-13 19:59:53 +00001693 if (bio_data_dir(io->base_bio) == READ) {
1694 if (kcryptd_io_read(io, GFP_NOWAIT))
1695 kcryptd_queue_io(io);
1696 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001697 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001699 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702static int crypt_status(struct dm_target *ti, status_type_t type,
1703 char *result, unsigned int maxlen)
1704{
Milan Broz5ebaee62010-08-12 04:14:07 +01001705 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 unsigned int sz = 0;
1707
1708 switch (type) {
1709 case STATUSTYPE_INFO:
1710 result[0] = '\0';
1711 break;
1712
1713 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001714 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 if (cc->key_size > 0) {
1717 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1718 return -ENOMEM;
1719
1720 crypt_encode_key(result + sz, cc->key, cc->key_size);
1721 sz += cc->key_size << 1;
1722 } else {
1723 if (sz >= maxlen)
1724 return -ENOMEM;
1725 result[sz++] = '-';
1726 }
1727
Andrew Morton4ee218c2006-03-27 01:17:48 -08001728 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1729 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001730
1731 if (ti->num_discard_requests)
1732 DMEMIT(" 1 allow_discards");
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 break;
1735 }
1736 return 0;
1737}
1738
Milan Broze48d4bb2006-10-03 01:15:37 -07001739static void crypt_postsuspend(struct dm_target *ti)
1740{
1741 struct crypt_config *cc = ti->private;
1742
1743 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1744}
1745
1746static int crypt_preresume(struct dm_target *ti)
1747{
1748 struct crypt_config *cc = ti->private;
1749
1750 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1751 DMERR("aborting resume - crypt key is not set.");
1752 return -EAGAIN;
1753 }
1754
1755 return 0;
1756}
1757
1758static void crypt_resume(struct dm_target *ti)
1759{
1760 struct crypt_config *cc = ti->private;
1761
1762 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1763}
1764
1765/* Message interface
1766 * key set <key>
1767 * key wipe
1768 */
1769static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1770{
1771 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001772 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001773
1774 if (argc < 2)
1775 goto error;
1776
Mike Snitzer498f0102011-08-02 12:32:04 +01001777 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001778 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1779 DMWARN("not suspended during key manipulation.");
1780 return -EINVAL;
1781 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001782 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001783 ret = crypt_set_key(cc, argv[2]);
1784 if (ret)
1785 return ret;
1786 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1787 ret = cc->iv_gen_ops->init(cc);
1788 return ret;
1789 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001790 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001791 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1792 ret = cc->iv_gen_ops->wipe(cc);
1793 if (ret)
1794 return ret;
1795 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001796 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001797 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001798 }
1799
1800error:
1801 DMWARN("unrecognised message received.");
1802 return -EINVAL;
1803}
1804
Milan Brozd41e26b2008-07-21 12:00:40 +01001805static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1806 struct bio_vec *biovec, int max_size)
1807{
1808 struct crypt_config *cc = ti->private;
1809 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1810
1811 if (!q->merge_bvec_fn)
1812 return max_size;
1813
1814 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001815 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001816
1817 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1818}
1819
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001820static int crypt_iterate_devices(struct dm_target *ti,
1821 iterate_devices_callout_fn fn, void *data)
1822{
1823 struct crypt_config *cc = ti->private;
1824
Mike Snitzer5dea2712009-07-23 20:30:42 +01001825 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static struct target_type crypt_target = {
1829 .name = "crypt",
Milan Broz772ae5f2011-08-02 12:32:08 +01001830 .version = {1, 11, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 .module = THIS_MODULE,
1832 .ctr = crypt_ctr,
1833 .dtr = crypt_dtr,
1834 .map = crypt_map,
1835 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001836 .postsuspend = crypt_postsuspend,
1837 .preresume = crypt_preresume,
1838 .resume = crypt_resume,
1839 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001840 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001841 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842};
1843
1844static int __init dm_crypt_init(void)
1845{
1846 int r;
1847
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001848 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (!_crypt_io_pool)
1850 return -ENOMEM;
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 r = dm_register_target(&crypt_target);
1853 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001854 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001855 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return r;
1859}
1860
1861static void __exit dm_crypt_exit(void)
1862{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001863 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 kmem_cache_destroy(_crypt_io_pool);
1865}
1866
1867module_init(dm_crypt_init);
1868module_exit(dm_crypt_exit);
1869
1870MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1871MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1872MODULE_LICENSE("GPL");