dm-crypt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/bio.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/mempool.h>
  13. #include <linux/slab.h>
  14. #include <linux/crypto.h>
  15. #include <linux/workqueue.h>
  16. #include <asm/atomic.h>
  17. #include <asm/scatterlist.h>
  18. #include <asm/page.h>
  19. #include "dm.h"
  20. #define PFX "crypt: "
  21. /*
  22. * per bio private data
  23. */
  24. struct crypt_io {
  25. struct dm_target *target;
  26. struct bio *bio;
  27. struct bio *first_clone;
  28. struct work_struct work;
  29. atomic_t pending;
  30. int error;
  31. };
  32. /*
  33. * context holding the current state of a multi-part conversion
  34. */
  35. struct convert_context {
  36. struct bio *bio_in;
  37. struct bio *bio_out;
  38. unsigned int offset_in;
  39. unsigned int offset_out;
  40. unsigned int idx_in;
  41. unsigned int idx_out;
  42. sector_t sector;
  43. int write;
  44. };
  45. struct crypt_config;
  46. struct crypt_iv_operations {
  47. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  48. const char *opts);
  49. void (*dtr)(struct crypt_config *cc);
  50. const char *(*status)(struct crypt_config *cc);
  51. int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
  52. };
  53. /*
  54. * Crypt: maps a linear range of a block device
  55. * and encrypts / decrypts at the same time.
  56. */
  57. struct crypt_config {
  58. struct dm_dev *dev;
  59. sector_t start;
  60. /*
  61. * pool for per bio private data and
  62. * for encryption buffer pages
  63. */
  64. mempool_t *io_pool;
  65. mempool_t *page_pool;
  66. /*
  67. * crypto related data
  68. */
  69. struct crypt_iv_operations *iv_gen_ops;
  70. char *iv_mode;
  71. void *iv_gen_private;
  72. sector_t iv_offset;
  73. unsigned int iv_size;
  74. struct crypto_tfm *tfm;
  75. unsigned int key_size;
  76. u8 key[0];
  77. };
  78. #define MIN_IOS 256
  79. #define MIN_POOL_PAGES 32
  80. #define MIN_BIO_PAGES 8
  81. static kmem_cache_t *_crypt_io_pool;
  82. /*
  83. * Mempool alloc and free functions for the page
  84. */
  85. static void *mempool_alloc_page(unsigned int __nocast gfp_mask, void *data)
  86. {
  87. return alloc_page(gfp_mask);
  88. }
  89. static void mempool_free_page(void *page, void *data)
  90. {
  91. __free_page(page);
  92. }
  93. /*
  94. * Different IV generation algorithms:
  95. *
  96. * plain: the initial vector is the 32-bit low-endian version of the sector
  97. * number, padded with zeros if neccessary.
  98. *
  99. * ess_iv: "encrypted sector|salt initial vector", the sector number is
  100. * encrypted with the bulk cipher using a salt as key. The salt
  101. * should be derived from the bulk cipher's key via hashing.
  102. *
  103. * plumb: unimplemented, see:
  104. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  105. */
  106. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  107. {
  108. memset(iv, 0, cc->iv_size);
  109. *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
  110. return 0;
  111. }
  112. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  113. const char *opts)
  114. {
  115. struct crypto_tfm *essiv_tfm;
  116. struct crypto_tfm *hash_tfm;
  117. struct scatterlist sg;
  118. unsigned int saltsize;
  119. u8 *salt;
  120. if (opts == NULL) {
  121. ti->error = PFX "Digest algorithm missing for ESSIV mode";
  122. return -EINVAL;
  123. }
  124. /* Hash the cipher key with the given hash algorithm */
  125. hash_tfm = crypto_alloc_tfm(opts, CRYPTO_TFM_REQ_MAY_SLEEP);
  126. if (hash_tfm == NULL) {
  127. ti->error = PFX "Error initializing ESSIV hash";
  128. return -EINVAL;
  129. }
  130. if (crypto_tfm_alg_type(hash_tfm) != CRYPTO_ALG_TYPE_DIGEST) {
  131. ti->error = PFX "Expected digest algorithm for ESSIV hash";
  132. crypto_free_tfm(hash_tfm);
  133. return -EINVAL;
  134. }
  135. saltsize = crypto_tfm_alg_digestsize(hash_tfm);
  136. salt = kmalloc(saltsize, GFP_KERNEL);
  137. if (salt == NULL) {
  138. ti->error = PFX "Error kmallocing salt storage in ESSIV";
  139. crypto_free_tfm(hash_tfm);
  140. return -ENOMEM;
  141. }
  142. sg.page = virt_to_page(cc->key);
  143. sg.offset = offset_in_page(cc->key);
  144. sg.length = cc->key_size;
  145. crypto_digest_digest(hash_tfm, &sg, 1, salt);
  146. crypto_free_tfm(hash_tfm);
  147. /* Setup the essiv_tfm with the given salt */
  148. essiv_tfm = crypto_alloc_tfm(crypto_tfm_alg_name(cc->tfm),
  149. CRYPTO_TFM_MODE_ECB |
  150. CRYPTO_TFM_REQ_MAY_SLEEP);
  151. if (essiv_tfm == NULL) {
  152. ti->error = PFX "Error allocating crypto tfm for ESSIV";
  153. kfree(salt);
  154. return -EINVAL;
  155. }
  156. if (crypto_tfm_alg_blocksize(essiv_tfm)
  157. != crypto_tfm_alg_ivsize(cc->tfm)) {
  158. ti->error = PFX "Block size of ESSIV cipher does "
  159. "not match IV size of block cipher";
  160. crypto_free_tfm(essiv_tfm);
  161. kfree(salt);
  162. return -EINVAL;
  163. }
  164. if (crypto_cipher_setkey(essiv_tfm, salt, saltsize) < 0) {
  165. ti->error = PFX "Failed to set key for ESSIV cipher";
  166. crypto_free_tfm(essiv_tfm);
  167. kfree(salt);
  168. return -EINVAL;
  169. }
  170. kfree(salt);
  171. cc->iv_gen_private = (void *)essiv_tfm;
  172. return 0;
  173. }
  174. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  175. {
  176. crypto_free_tfm((struct crypto_tfm *)cc->iv_gen_private);
  177. cc->iv_gen_private = NULL;
  178. }
  179. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  180. {
  181. struct scatterlist sg = { NULL, };
  182. memset(iv, 0, cc->iv_size);
  183. *(u64 *)iv = cpu_to_le64(sector);
  184. sg.page = virt_to_page(iv);
  185. sg.offset = offset_in_page(iv);
  186. sg.length = cc->iv_size;
  187. crypto_cipher_encrypt((struct crypto_tfm *)cc->iv_gen_private,
  188. &sg, &sg, cc->iv_size);
  189. return 0;
  190. }
  191. static struct crypt_iv_operations crypt_iv_plain_ops = {
  192. .generator = crypt_iv_plain_gen
  193. };
  194. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  195. .ctr = crypt_iv_essiv_ctr,
  196. .dtr = crypt_iv_essiv_dtr,
  197. .generator = crypt_iv_essiv_gen
  198. };
  199. static inline int
  200. crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
  201. struct scatterlist *in, unsigned int length,
  202. int write, sector_t sector)
  203. {
  204. u8 iv[cc->iv_size];
  205. int r;
  206. if (cc->iv_gen_ops) {
  207. r = cc->iv_gen_ops->generator(cc, iv, sector);
  208. if (r < 0)
  209. return r;
  210. if (write)
  211. r = crypto_cipher_encrypt_iv(cc->tfm, out, in, length, iv);
  212. else
  213. r = crypto_cipher_decrypt_iv(cc->tfm, out, in, length, iv);
  214. } else {
  215. if (write)
  216. r = crypto_cipher_encrypt(cc->tfm, out, in, length);
  217. else
  218. r = crypto_cipher_decrypt(cc->tfm, out, in, length);
  219. }
  220. return r;
  221. }
  222. static void
  223. crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
  224. struct bio *bio_out, struct bio *bio_in,
  225. sector_t sector, int write)
  226. {
  227. ctx->bio_in = bio_in;
  228. ctx->bio_out = bio_out;
  229. ctx->offset_in = 0;
  230. ctx->offset_out = 0;
  231. ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
  232. ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
  233. ctx->sector = sector + cc->iv_offset;
  234. ctx->write = write;
  235. }
  236. /*
  237. * Encrypt / decrypt data from one bio to another one (can be the same one)
  238. */
  239. static int crypt_convert(struct crypt_config *cc,
  240. struct convert_context *ctx)
  241. {
  242. int r = 0;
  243. while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
  244. ctx->idx_out < ctx->bio_out->bi_vcnt) {
  245. struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
  246. struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
  247. struct scatterlist sg_in = {
  248. .page = bv_in->bv_page,
  249. .offset = bv_in->bv_offset + ctx->offset_in,
  250. .length = 1 << SECTOR_SHIFT
  251. };
  252. struct scatterlist sg_out = {
  253. .page = bv_out->bv_page,
  254. .offset = bv_out->bv_offset + ctx->offset_out,
  255. .length = 1 << SECTOR_SHIFT
  256. };
  257. ctx->offset_in += sg_in.length;
  258. if (ctx->offset_in >= bv_in->bv_len) {
  259. ctx->offset_in = 0;
  260. ctx->idx_in++;
  261. }
  262. ctx->offset_out += sg_out.length;
  263. if (ctx->offset_out >= bv_out->bv_len) {
  264. ctx->offset_out = 0;
  265. ctx->idx_out++;
  266. }
  267. r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
  268. ctx->write, ctx->sector);
  269. if (r < 0)
  270. break;
  271. ctx->sector++;
  272. }
  273. return r;
  274. }
  275. /*
  276. * Generate a new unfragmented bio with the given size
  277. * This should never violate the device limitations
  278. * May return a smaller bio when running out of pages
  279. */
  280. static struct bio *
  281. crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
  282. struct bio *base_bio, unsigned int *bio_vec_idx)
  283. {
  284. struct bio *bio;
  285. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  286. int gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  287. unsigned int i;
  288. /*
  289. * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
  290. * to fail earlier. This is not necessary but increases throughput.
  291. * FIXME: Is this really intelligent?
  292. */
  293. if (base_bio)
  294. bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
  295. else
  296. bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
  297. if (!bio)
  298. return NULL;
  299. /* if the last bio was not complete, continue where that one ended */
  300. bio->bi_idx = *bio_vec_idx;
  301. bio->bi_vcnt = *bio_vec_idx;
  302. bio->bi_size = 0;
  303. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  304. /* bio->bi_idx pages have already been allocated */
  305. size -= bio->bi_idx * PAGE_SIZE;
  306. for(i = bio->bi_idx; i < nr_iovecs; i++) {
  307. struct bio_vec *bv = bio_iovec_idx(bio, i);
  308. bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
  309. if (!bv->bv_page)
  310. break;
  311. /*
  312. * if additional pages cannot be allocated without waiting,
  313. * return a partially allocated bio, the caller will then try
  314. * to allocate additional bios while submitting this partial bio
  315. */
  316. if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
  317. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  318. bv->bv_offset = 0;
  319. if (size > PAGE_SIZE)
  320. bv->bv_len = PAGE_SIZE;
  321. else
  322. bv->bv_len = size;
  323. bio->bi_size += bv->bv_len;
  324. bio->bi_vcnt++;
  325. size -= bv->bv_len;
  326. }
  327. if (!bio->bi_size) {
  328. bio_put(bio);
  329. return NULL;
  330. }
  331. /*
  332. * Remember the last bio_vec allocated to be able
  333. * to correctly continue after the splitting.
  334. */
  335. *bio_vec_idx = bio->bi_vcnt;
  336. return bio;
  337. }
  338. static void crypt_free_buffer_pages(struct crypt_config *cc,
  339. struct bio *bio, unsigned int bytes)
  340. {
  341. unsigned int i, start, end;
  342. struct bio_vec *bv;
  343. /*
  344. * This is ugly, but Jens Axboe thinks that using bi_idx in the
  345. * endio function is too dangerous at the moment, so I calculate the
  346. * correct position using bi_vcnt and bi_size.
  347. * The bv_offset and bv_len fields might already be modified but we
  348. * know that we always allocated whole pages.
  349. * A fix to the bi_idx issue in the kernel is in the works, so
  350. * we will hopefully be able to revert to the cleaner solution soon.
  351. */
  352. i = bio->bi_vcnt - 1;
  353. bv = bio_iovec_idx(bio, i);
  354. end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
  355. start = end - bytes;
  356. start >>= PAGE_SHIFT;
  357. if (!bio->bi_size)
  358. end = bio->bi_vcnt;
  359. else
  360. end >>= PAGE_SHIFT;
  361. for(i = start; i < end; i++) {
  362. bv = bio_iovec_idx(bio, i);
  363. BUG_ON(!bv->bv_page);
  364. mempool_free(bv->bv_page, cc->page_pool);
  365. bv->bv_page = NULL;
  366. }
  367. }
  368. /*
  369. * One of the bios was finished. Check for completion of
  370. * the whole request and correctly clean up the buffer.
  371. */
  372. static void dec_pending(struct crypt_io *io, int error)
  373. {
  374. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  375. if (error < 0)
  376. io->error = error;
  377. if (!atomic_dec_and_test(&io->pending))
  378. return;
  379. if (io->first_clone)
  380. bio_put(io->first_clone);
  381. bio_endio(io->bio, io->bio->bi_size, io->error);
  382. mempool_free(io, cc->io_pool);
  383. }
  384. /*
  385. * kcryptd:
  386. *
  387. * Needed because it would be very unwise to do decryption in an
  388. * interrupt context, so bios returning from read requests get
  389. * queued here.
  390. */
  391. static struct workqueue_struct *_kcryptd_workqueue;
  392. static void kcryptd_do_work(void *data)
  393. {
  394. struct crypt_io *io = (struct crypt_io *) data;
  395. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  396. struct convert_context ctx;
  397. int r;
  398. crypt_convert_init(cc, &ctx, io->bio, io->bio,
  399. io->bio->bi_sector - io->target->begin, 0);
  400. r = crypt_convert(cc, &ctx);
  401. dec_pending(io, r);
  402. }
  403. static void kcryptd_queue_io(struct crypt_io *io)
  404. {
  405. INIT_WORK(&io->work, kcryptd_do_work, io);
  406. queue_work(_kcryptd_workqueue, &io->work);
  407. }
  408. /*
  409. * Decode key from its hex representation
  410. */
  411. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  412. {
  413. char buffer[3];
  414. char *endp;
  415. unsigned int i;
  416. buffer[2] = '\0';
  417. for(i = 0; i < size; i++) {
  418. buffer[0] = *hex++;
  419. buffer[1] = *hex++;
  420. key[i] = (u8)simple_strtoul(buffer, &endp, 16);
  421. if (endp != &buffer[2])
  422. return -EINVAL;
  423. }
  424. if (*hex != '\0')
  425. return -EINVAL;
  426. return 0;
  427. }
  428. /*
  429. * Encode key into its hex representation
  430. */
  431. static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
  432. {
  433. unsigned int i;
  434. for(i = 0; i < size; i++) {
  435. sprintf(hex, "%02x", *key);
  436. hex += 2;
  437. key++;
  438. }
  439. }
  440. /*
  441. * Construct an encryption mapping:
  442. * <cipher> <key> <iv_offset> <dev_path> <start>
  443. */
  444. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  445. {
  446. struct crypt_config *cc;
  447. struct crypto_tfm *tfm;
  448. char *tmp;
  449. char *cipher;
  450. char *chainmode;
  451. char *ivmode;
  452. char *ivopts;
  453. unsigned int crypto_flags;
  454. unsigned int key_size;
  455. if (argc != 5) {
  456. ti->error = PFX "Not enough arguments";
  457. return -EINVAL;
  458. }
  459. tmp = argv[0];
  460. cipher = strsep(&tmp, "-");
  461. chainmode = strsep(&tmp, "-");
  462. ivopts = strsep(&tmp, "-");
  463. ivmode = strsep(&ivopts, ":");
  464. if (tmp)
  465. DMWARN(PFX "Unexpected additional cipher options");
  466. key_size = strlen(argv[1]) >> 1;
  467. cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  468. if (cc == NULL) {
  469. ti->error =
  470. PFX "Cannot allocate transparent encryption context";
  471. return -ENOMEM;
  472. }
  473. cc->key_size = key_size;
  474. if ((!key_size && strcmp(argv[1], "-") != 0) ||
  475. (key_size && crypt_decode_key(cc->key, argv[1], key_size) < 0)) {
  476. ti->error = PFX "Error decoding key";
  477. goto bad1;
  478. }
  479. /* Compatiblity mode for old dm-crypt cipher strings */
  480. if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
  481. chainmode = "cbc";
  482. ivmode = "plain";
  483. }
  484. /* Choose crypto_flags according to chainmode */
  485. if (strcmp(chainmode, "cbc") == 0)
  486. crypto_flags = CRYPTO_TFM_MODE_CBC;
  487. else if (strcmp(chainmode, "ecb") == 0)
  488. crypto_flags = CRYPTO_TFM_MODE_ECB;
  489. else {
  490. ti->error = PFX "Unknown chaining mode";
  491. goto bad1;
  492. }
  493. if (crypto_flags != CRYPTO_TFM_MODE_ECB && !ivmode) {
  494. ti->error = PFX "This chaining mode requires an IV mechanism";
  495. goto bad1;
  496. }
  497. tfm = crypto_alloc_tfm(cipher, crypto_flags | CRYPTO_TFM_REQ_MAY_SLEEP);
  498. if (!tfm) {
  499. ti->error = PFX "Error allocating crypto tfm";
  500. goto bad1;
  501. }
  502. if (crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER) {
  503. ti->error = PFX "Expected cipher algorithm";
  504. goto bad2;
  505. }
  506. cc->tfm = tfm;
  507. /*
  508. * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
  509. * See comments at iv code
  510. */
  511. if (ivmode == NULL)
  512. cc->iv_gen_ops = NULL;
  513. else if (strcmp(ivmode, "plain") == 0)
  514. cc->iv_gen_ops = &crypt_iv_plain_ops;
  515. else if (strcmp(ivmode, "essiv") == 0)
  516. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  517. else {
  518. ti->error = PFX "Invalid IV mode";
  519. goto bad2;
  520. }
  521. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
  522. cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
  523. goto bad2;
  524. if (tfm->crt_cipher.cit_decrypt_iv && tfm->crt_cipher.cit_encrypt_iv)
  525. /* at least a 64 bit sector number should fit in our buffer */
  526. cc->iv_size = max(crypto_tfm_alg_ivsize(tfm),
  527. (unsigned int)(sizeof(u64) / sizeof(u8)));
  528. else {
  529. cc->iv_size = 0;
  530. if (cc->iv_gen_ops) {
  531. DMWARN(PFX "Selected cipher does not support IVs");
  532. if (cc->iv_gen_ops->dtr)
  533. cc->iv_gen_ops->dtr(cc);
  534. cc->iv_gen_ops = NULL;
  535. }
  536. }
  537. cc->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  538. mempool_free_slab, _crypt_io_pool);
  539. if (!cc->io_pool) {
  540. ti->error = PFX "Cannot allocate crypt io mempool";
  541. goto bad3;
  542. }
  543. cc->page_pool = mempool_create(MIN_POOL_PAGES, mempool_alloc_page,
  544. mempool_free_page, NULL);
  545. if (!cc->page_pool) {
  546. ti->error = PFX "Cannot allocate page mempool";
  547. goto bad4;
  548. }
  549. if (tfm->crt_cipher.cit_setkey(tfm, cc->key, key_size) < 0) {
  550. ti->error = PFX "Error setting key";
  551. goto bad5;
  552. }
  553. if (sscanf(argv[2], SECTOR_FORMAT, &cc->iv_offset) != 1) {
  554. ti->error = PFX "Invalid iv_offset sector";
  555. goto bad5;
  556. }
  557. if (sscanf(argv[4], SECTOR_FORMAT, &cc->start) != 1) {
  558. ti->error = PFX "Invalid device sector";
  559. goto bad5;
  560. }
  561. if (dm_get_device(ti, argv[3], cc->start, ti->len,
  562. dm_table_get_mode(ti->table), &cc->dev)) {
  563. ti->error = PFX "Device lookup failed";
  564. goto bad5;
  565. }
  566. if (ivmode && cc->iv_gen_ops) {
  567. if (ivopts)
  568. *(ivopts - 1) = ':';
  569. cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
  570. if (!cc->iv_mode) {
  571. ti->error = PFX "Error kmallocing iv_mode string";
  572. goto bad5;
  573. }
  574. strcpy(cc->iv_mode, ivmode);
  575. } else
  576. cc->iv_mode = NULL;
  577. ti->private = cc;
  578. return 0;
  579. bad5:
  580. mempool_destroy(cc->page_pool);
  581. bad4:
  582. mempool_destroy(cc->io_pool);
  583. bad3:
  584. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  585. cc->iv_gen_ops->dtr(cc);
  586. bad2:
  587. crypto_free_tfm(tfm);
  588. bad1:
  589. kfree(cc);
  590. return -EINVAL;
  591. }
  592. static void crypt_dtr(struct dm_target *ti)
  593. {
  594. struct crypt_config *cc = (struct crypt_config *) ti->private;
  595. mempool_destroy(cc->page_pool);
  596. mempool_destroy(cc->io_pool);
  597. kfree(cc->iv_mode);
  598. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  599. cc->iv_gen_ops->dtr(cc);
  600. crypto_free_tfm(cc->tfm);
  601. dm_put_device(ti, cc->dev);
  602. kfree(cc);
  603. }
  604. static int crypt_endio(struct bio *bio, unsigned int done, int error)
  605. {
  606. struct crypt_io *io = (struct crypt_io *) bio->bi_private;
  607. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  608. if (bio_data_dir(bio) == WRITE) {
  609. /*
  610. * free the processed pages, even if
  611. * it's only a partially completed write
  612. */
  613. crypt_free_buffer_pages(cc, bio, done);
  614. }
  615. if (bio->bi_size)
  616. return 1;
  617. bio_put(bio);
  618. /*
  619. * successful reads are decrypted by the worker thread
  620. */
  621. if ((bio_data_dir(bio) == READ)
  622. && bio_flagged(bio, BIO_UPTODATE)) {
  623. kcryptd_queue_io(io);
  624. return 0;
  625. }
  626. dec_pending(io, error);
  627. return error;
  628. }
  629. static inline struct bio *
  630. crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
  631. sector_t sector, unsigned int *bvec_idx,
  632. struct convert_context *ctx)
  633. {
  634. struct bio *clone;
  635. if (bio_data_dir(bio) == WRITE) {
  636. clone = crypt_alloc_buffer(cc, bio->bi_size,
  637. io->first_clone, bvec_idx);
  638. if (clone) {
  639. ctx->bio_out = clone;
  640. if (crypt_convert(cc, ctx) < 0) {
  641. crypt_free_buffer_pages(cc, clone,
  642. clone->bi_size);
  643. bio_put(clone);
  644. return NULL;
  645. }
  646. }
  647. } else {
  648. /*
  649. * The block layer might modify the bvec array, so always
  650. * copy the required bvecs because we need the original
  651. * one in order to decrypt the whole bio data *afterwards*.
  652. */
  653. clone = bio_alloc(GFP_NOIO, bio_segments(bio));
  654. if (clone) {
  655. clone->bi_idx = 0;
  656. clone->bi_vcnt = bio_segments(bio);
  657. clone->bi_size = bio->bi_size;
  658. memcpy(clone->bi_io_vec, bio_iovec(bio),
  659. sizeof(struct bio_vec) * clone->bi_vcnt);
  660. }
  661. }
  662. if (!clone)
  663. return NULL;
  664. clone->bi_private = io;
  665. clone->bi_end_io = crypt_endio;
  666. clone->bi_bdev = cc->dev->bdev;
  667. clone->bi_sector = cc->start + sector;
  668. clone->bi_rw = bio->bi_rw;
  669. return clone;
  670. }
  671. static int crypt_map(struct dm_target *ti, struct bio *bio,
  672. union map_info *map_context)
  673. {
  674. struct crypt_config *cc = (struct crypt_config *) ti->private;
  675. struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO);
  676. struct convert_context ctx;
  677. struct bio *clone;
  678. unsigned int remaining = bio->bi_size;
  679. sector_t sector = bio->bi_sector - ti->begin;
  680. unsigned int bvec_idx = 0;
  681. io->target = ti;
  682. io->bio = bio;
  683. io->first_clone = NULL;
  684. io->error = 0;
  685. atomic_set(&io->pending, 1); /* hold a reference */
  686. if (bio_data_dir(bio) == WRITE)
  687. crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
  688. /*
  689. * The allocated buffers can be smaller than the whole bio,
  690. * so repeat the whole process until all the data can be handled.
  691. */
  692. while (remaining) {
  693. clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
  694. if (!clone)
  695. goto cleanup;
  696. if (!io->first_clone) {
  697. /*
  698. * hold a reference to the first clone, because it
  699. * holds the bio_vec array and that can't be freed
  700. * before all other clones are released
  701. */
  702. bio_get(clone);
  703. io->first_clone = clone;
  704. }
  705. atomic_inc(&io->pending);
  706. remaining -= clone->bi_size;
  707. sector += bio_sectors(clone);
  708. generic_make_request(clone);
  709. /* out of memory -> run queues */
  710. if (remaining)
  711. blk_congestion_wait(bio_data_dir(clone), HZ/100);
  712. }
  713. /* drop reference, clones could have returned before we reach this */
  714. dec_pending(io, 0);
  715. return 0;
  716. cleanup:
  717. if (io->first_clone) {
  718. dec_pending(io, -ENOMEM);
  719. return 0;
  720. }
  721. /* if no bio has been dispatched yet, we can directly return the error */
  722. mempool_free(io, cc->io_pool);
  723. return -ENOMEM;
  724. }
  725. static int crypt_status(struct dm_target *ti, status_type_t type,
  726. char *result, unsigned int maxlen)
  727. {
  728. struct crypt_config *cc = (struct crypt_config *) ti->private;
  729. const char *cipher;
  730. const char *chainmode = NULL;
  731. unsigned int sz = 0;
  732. switch (type) {
  733. case STATUSTYPE_INFO:
  734. result[0] = '\0';
  735. break;
  736. case STATUSTYPE_TABLE:
  737. cipher = crypto_tfm_alg_name(cc->tfm);
  738. switch(cc->tfm->crt_cipher.cit_mode) {
  739. case CRYPTO_TFM_MODE_CBC:
  740. chainmode = "cbc";
  741. break;
  742. case CRYPTO_TFM_MODE_ECB:
  743. chainmode = "ecb";
  744. break;
  745. default:
  746. BUG();
  747. }
  748. if (cc->iv_mode)
  749. DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
  750. else
  751. DMEMIT("%s-%s ", cipher, chainmode);
  752. if (cc->key_size > 0) {
  753. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  754. return -ENOMEM;
  755. crypt_encode_key(result + sz, cc->key, cc->key_size);
  756. sz += cc->key_size << 1;
  757. } else {
  758. if (sz >= maxlen)
  759. return -ENOMEM;
  760. result[sz++] = '-';
  761. }
  762. DMEMIT(" " SECTOR_FORMAT " %s " SECTOR_FORMAT,
  763. cc->iv_offset, cc->dev->name, cc->start);
  764. break;
  765. }
  766. return 0;
  767. }
  768. static struct target_type crypt_target = {
  769. .name = "crypt",
  770. .version= {1, 1, 0},
  771. .module = THIS_MODULE,
  772. .ctr = crypt_ctr,
  773. .dtr = crypt_dtr,
  774. .map = crypt_map,
  775. .status = crypt_status,
  776. };
  777. static int __init dm_crypt_init(void)
  778. {
  779. int r;
  780. _crypt_io_pool = kmem_cache_create("dm-crypt_io",
  781. sizeof(struct crypt_io),
  782. 0, 0, NULL, NULL);
  783. if (!_crypt_io_pool)
  784. return -ENOMEM;
  785. _kcryptd_workqueue = create_workqueue("kcryptd");
  786. if (!_kcryptd_workqueue) {
  787. r = -ENOMEM;
  788. DMERR(PFX "couldn't create kcryptd");
  789. goto bad1;
  790. }
  791. r = dm_register_target(&crypt_target);
  792. if (r < 0) {
  793. DMERR(PFX "register failed %d", r);
  794. goto bad2;
  795. }
  796. return 0;
  797. bad2:
  798. destroy_workqueue(_kcryptd_workqueue);
  799. bad1:
  800. kmem_cache_destroy(_crypt_io_pool);
  801. return r;
  802. }
  803. static void __exit dm_crypt_exit(void)
  804. {
  805. int r = dm_unregister_target(&crypt_target);
  806. if (r < 0)
  807. DMERR(PFX "unregister failed %d", r);
  808. destroy_workqueue(_kcryptd_workqueue);
  809. kmem_cache_destroy(_crypt_io_pool);
  810. }
  811. module_init(dm_crypt_init);
  812. module_exit(dm_crypt_exit);
  813. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  814. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  815. MODULE_LICENSE("GPL");