tcp_sigpool.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <crypto/hash.h>
  3. #include <linux/cpu.h>
  4. #include <linux/kref.h>
  5. #include <linux/module.h>
  6. #include <linux/mutex.h>
  7. #include <linux/percpu.h>
  8. #include <linux/workqueue.h>
  9. #include <net/tcp.h>
  10. static size_t __scratch_size;
  11. struct sigpool_scratch {
  12. local_lock_t bh_lock;
  13. void __rcu *pad;
  14. };
  15. static DEFINE_PER_CPU(struct sigpool_scratch, sigpool_scratch) = {
  16. .bh_lock = INIT_LOCAL_LOCK(bh_lock),
  17. };
  18. struct sigpool_entry {
  19. struct crypto_ahash *hash;
  20. const char *alg;
  21. struct kref kref;
  22. uint16_t needs_key:1,
  23. reserved:15;
  24. };
  25. #define CPOOL_SIZE (PAGE_SIZE / sizeof(struct sigpool_entry))
  26. static struct sigpool_entry cpool[CPOOL_SIZE];
  27. static unsigned int cpool_populated;
  28. static DEFINE_MUTEX(cpool_mutex);
  29. /* Slow-path */
  30. struct scratches_to_free {
  31. struct rcu_head rcu;
  32. unsigned int cnt;
  33. void *scratches[];
  34. };
  35. static void free_old_scratches(struct rcu_head *head)
  36. {
  37. struct scratches_to_free *stf;
  38. stf = container_of(head, struct scratches_to_free, rcu);
  39. while (stf->cnt--)
  40. kfree(stf->scratches[stf->cnt]);
  41. kfree(stf);
  42. }
  43. /**
  44. * sigpool_reserve_scratch - re-allocates scratch buffer, slow-path
  45. * @size: request size for the scratch/temp buffer
  46. */
  47. static int sigpool_reserve_scratch(size_t size)
  48. {
  49. struct scratches_to_free *stf;
  50. size_t stf_sz = struct_size(stf, scratches, num_possible_cpus());
  51. int cpu, err = 0;
  52. lockdep_assert_held(&cpool_mutex);
  53. if (__scratch_size >= size)
  54. return 0;
  55. stf = kmalloc(stf_sz, GFP_KERNEL);
  56. if (!stf)
  57. return -ENOMEM;
  58. stf->cnt = 0;
  59. size = max(size, __scratch_size);
  60. cpus_read_lock();
  61. for_each_possible_cpu(cpu) {
  62. void *scratch, *old_scratch;
  63. scratch = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu));
  64. if (!scratch) {
  65. err = -ENOMEM;
  66. break;
  67. }
  68. old_scratch = rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
  69. scratch, lockdep_is_held(&cpool_mutex));
  70. if (!cpu_online(cpu) || !old_scratch) {
  71. kfree(old_scratch);
  72. continue;
  73. }
  74. stf->scratches[stf->cnt++] = old_scratch;
  75. }
  76. cpus_read_unlock();
  77. if (!err)
  78. __scratch_size = size;
  79. call_rcu(&stf->rcu, free_old_scratches);
  80. return err;
  81. }
  82. static void sigpool_scratch_free(void)
  83. {
  84. int cpu;
  85. for_each_possible_cpu(cpu)
  86. kfree(rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
  87. NULL, lockdep_is_held(&cpool_mutex)));
  88. __scratch_size = 0;
  89. }
  90. static int __cpool_try_clone(struct crypto_ahash *hash)
  91. {
  92. struct crypto_ahash *tmp;
  93. tmp = crypto_clone_ahash(hash);
  94. if (IS_ERR(tmp))
  95. return PTR_ERR(tmp);
  96. crypto_free_ahash(tmp);
  97. return 0;
  98. }
  99. static int __cpool_alloc_ahash(struct sigpool_entry *e, const char *alg)
  100. {
  101. struct crypto_ahash *cpu0_hash;
  102. int ret;
  103. e->alg = kstrdup(alg, GFP_KERNEL);
  104. if (!e->alg)
  105. return -ENOMEM;
  106. cpu0_hash = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC);
  107. if (IS_ERR(cpu0_hash)) {
  108. ret = PTR_ERR(cpu0_hash);
  109. goto out_free_alg;
  110. }
  111. e->needs_key = crypto_ahash_get_flags(cpu0_hash) & CRYPTO_TFM_NEED_KEY;
  112. ret = __cpool_try_clone(cpu0_hash);
  113. if (ret)
  114. goto out_free_cpu0_hash;
  115. e->hash = cpu0_hash;
  116. kref_init(&e->kref);
  117. return 0;
  118. out_free_cpu0_hash:
  119. crypto_free_ahash(cpu0_hash);
  120. out_free_alg:
  121. kfree(e->alg);
  122. e->alg = NULL;
  123. return ret;
  124. }
  125. /**
  126. * tcp_sigpool_alloc_ahash - allocates pool for ahash requests
  127. * @alg: name of async hash algorithm
  128. * @scratch_size: reserve a tcp_sigpool::scratch buffer of this size
  129. */
  130. int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size)
  131. {
  132. int i, ret;
  133. /* slow-path */
  134. mutex_lock(&cpool_mutex);
  135. ret = sigpool_reserve_scratch(scratch_size);
  136. if (ret)
  137. goto out;
  138. for (i = 0; i < cpool_populated; i++) {
  139. if (!cpool[i].alg)
  140. continue;
  141. if (strcmp(cpool[i].alg, alg))
  142. continue;
  143. /* pairs with tcp_sigpool_release() */
  144. if (!kref_get_unless_zero(&cpool[i].kref))
  145. kref_init(&cpool[i].kref);
  146. ret = i;
  147. goto out;
  148. }
  149. for (i = 0; i < cpool_populated; i++) {
  150. if (!cpool[i].alg)
  151. break;
  152. }
  153. if (i >= CPOOL_SIZE) {
  154. ret = -ENOSPC;
  155. goto out;
  156. }
  157. ret = __cpool_alloc_ahash(&cpool[i], alg);
  158. if (!ret) {
  159. ret = i;
  160. if (i == cpool_populated)
  161. cpool_populated++;
  162. }
  163. out:
  164. mutex_unlock(&cpool_mutex);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL_GPL(tcp_sigpool_alloc_ahash);
  168. static void __cpool_free_entry(struct sigpool_entry *e)
  169. {
  170. crypto_free_ahash(e->hash);
  171. kfree(e->alg);
  172. memset(e, 0, sizeof(*e));
  173. }
  174. static void cpool_cleanup_work_cb(struct work_struct *work)
  175. {
  176. bool free_scratch = true;
  177. unsigned int i;
  178. mutex_lock(&cpool_mutex);
  179. for (i = 0; i < cpool_populated; i++) {
  180. if (kref_read(&cpool[i].kref) > 0) {
  181. free_scratch = false;
  182. continue;
  183. }
  184. if (!cpool[i].alg)
  185. continue;
  186. __cpool_free_entry(&cpool[i]);
  187. }
  188. if (free_scratch)
  189. sigpool_scratch_free();
  190. mutex_unlock(&cpool_mutex);
  191. }
  192. static DECLARE_WORK(cpool_cleanup_work, cpool_cleanup_work_cb);
  193. static void cpool_schedule_cleanup(struct kref *kref)
  194. {
  195. schedule_work(&cpool_cleanup_work);
  196. }
  197. /**
  198. * tcp_sigpool_release - decreases number of users for a pool. If it was
  199. * the last user of the pool, releases any memory that was consumed.
  200. * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
  201. */
  202. void tcp_sigpool_release(unsigned int id)
  203. {
  204. if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
  205. return;
  206. /* slow-path */
  207. kref_put(&cpool[id].kref, cpool_schedule_cleanup);
  208. }
  209. EXPORT_SYMBOL_GPL(tcp_sigpool_release);
  210. /**
  211. * tcp_sigpool_get - increases number of users (refcounter) for a pool
  212. * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
  213. */
  214. void tcp_sigpool_get(unsigned int id)
  215. {
  216. if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
  217. return;
  218. kref_get(&cpool[id].kref);
  219. }
  220. EXPORT_SYMBOL_GPL(tcp_sigpool_get);
  221. int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c) __cond_acquires(RCU_BH)
  222. {
  223. struct crypto_ahash *hash;
  224. rcu_read_lock_bh();
  225. if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg)) {
  226. rcu_read_unlock_bh();
  227. return -EINVAL;
  228. }
  229. hash = crypto_clone_ahash(cpool[id].hash);
  230. if (IS_ERR(hash)) {
  231. rcu_read_unlock_bh();
  232. return PTR_ERR(hash);
  233. }
  234. c->req = ahash_request_alloc(hash, GFP_ATOMIC);
  235. if (!c->req) {
  236. crypto_free_ahash(hash);
  237. rcu_read_unlock_bh();
  238. return -ENOMEM;
  239. }
  240. ahash_request_set_callback(c->req, 0, NULL, NULL);
  241. /* Pairs with tcp_sigpool_reserve_scratch(), scratch area is
  242. * valid (allocated) until tcp_sigpool_end().
  243. */
  244. local_lock_nested_bh(&sigpool_scratch.bh_lock);
  245. c->scratch = rcu_dereference_bh(*this_cpu_ptr(&sigpool_scratch.pad));
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(tcp_sigpool_start);
  249. void tcp_sigpool_end(struct tcp_sigpool *c) __releases(RCU_BH)
  250. {
  251. struct crypto_ahash *hash = crypto_ahash_reqtfm(c->req);
  252. local_unlock_nested_bh(&sigpool_scratch.bh_lock);
  253. rcu_read_unlock_bh();
  254. ahash_request_free(c->req);
  255. crypto_free_ahash(hash);
  256. }
  257. EXPORT_SYMBOL_GPL(tcp_sigpool_end);
  258. /**
  259. * tcp_sigpool_algo - return algorithm of tcp_sigpool
  260. * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
  261. * @buf: buffer to return name of algorithm
  262. * @buf_len: size of @buf
  263. */
  264. size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len)
  265. {
  266. if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
  267. return -EINVAL;
  268. return strscpy(buf, cpool[id].alg, buf_len);
  269. }
  270. EXPORT_SYMBOL_GPL(tcp_sigpool_algo);
  271. /**
  272. * tcp_sigpool_hash_skb_data - hash data in skb with initialized tcp_sigpool
  273. * @hp: tcp_sigpool pointer
  274. * @skb: buffer to add sign for
  275. * @header_len: TCP header length for this segment
  276. */
  277. int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp,
  278. const struct sk_buff *skb,
  279. unsigned int header_len)
  280. {
  281. const unsigned int head_data_len = skb_headlen(skb) > header_len ?
  282. skb_headlen(skb) - header_len : 0;
  283. const struct skb_shared_info *shi = skb_shinfo(skb);
  284. const struct tcphdr *tp = tcp_hdr(skb);
  285. struct ahash_request *req = hp->req;
  286. struct sk_buff *frag_iter;
  287. struct scatterlist sg;
  288. unsigned int i;
  289. sg_init_table(&sg, 1);
  290. sg_set_buf(&sg, ((u8 *)tp) + header_len, head_data_len);
  291. ahash_request_set_crypt(req, &sg, NULL, head_data_len);
  292. if (crypto_ahash_update(req))
  293. return 1;
  294. for (i = 0; i < shi->nr_frags; ++i) {
  295. const skb_frag_t *f = &shi->frags[i];
  296. unsigned int offset = skb_frag_off(f);
  297. struct page *page;
  298. page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
  299. sg_set_page(&sg, page, skb_frag_size(f), offset_in_page(offset));
  300. ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
  301. if (crypto_ahash_update(req))
  302. return 1;
  303. }
  304. skb_walk_frags(skb, frag_iter)
  305. if (tcp_sigpool_hash_skb_data(hp, frag_iter, 0))
  306. return 1;
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(tcp_sigpool_hash_skb_data);
  310. MODULE_LICENSE("GPL");
  311. MODULE_DESCRIPTION("Per-CPU pool of crypto requests");