dm-crypt.c 23 KB

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