dm-crypt.c 23 KB

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