summaryrefslogtreecommitdiff
path: root/net/ipv4/tcp_sigpool.c
blob: d8a4f192873a2a1036c2f017c85fb4436226e53c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-License-Identifier: GPL-2.0-or-later

#include <crypto/hash.h>
#include <linux/cpu.h>
#include <linux/kref.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/percpu.h>
#include <linux/workqueue.h>
#include <net/tcp.h>

static size_t __scratch_size;
struct sigpool_scratch {
	local_lock_t bh_lock;
	void __rcu *pad;
};

static DEFINE_PER_CPU(struct sigpool_scratch, sigpool_scratch) = {
	.bh_lock = INIT_LOCAL_LOCK(bh_lock),
};

struct sigpool_entry {
	struct crypto_ahash	*hash;
	const char		*alg;
	struct kref		kref;
	uint16_t		needs_key:1,
				reserved:15;
};

#define CPOOL_SIZE (PAGE_SIZE / sizeof(struct sigpool_entry))
static struct sigpool_entry cpool[CPOOL_SIZE];
static unsigned int cpool_populated;
static DEFINE_MUTEX(cpool_mutex);

/* Slow-path */
struct scratches_to_free {
	struct rcu_head rcu;
	unsigned int cnt;
	void *scratches[];
};

static void free_old_scratches(struct rcu_head *head)
{
	struct scratches_to_free *stf;

	stf = container_of(head, struct scratches_to_free, rcu);
	while (stf->cnt--)
		kfree(stf->scratches[stf->cnt]);
	kfree(stf);
}

/**
 * sigpool_reserve_scratch - re-allocates scratch buffer, slow-path
 * @size: request size for the scratch/temp buffer
 */
static int sigpool_reserve_scratch(size_t size)
{
	struct scratches_to_free *stf;
	size_t stf_sz = struct_size(stf, scratches, num_possible_cpus());
	int cpu, err = 0;

	lockdep_assert_held(&cpool_mutex);
	if (__scratch_size >= size)
		return 0;

	stf = kmalloc(stf_sz, GFP_KERNEL);
	if (!stf)
		return -ENOMEM;
	stf->cnt = 0;

	size = max(size, __scratch_size);
	cpus_read_lock();
	for_each_possible_cpu(cpu) {
		void *scratch, *old_scratch;

		scratch = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu));
		if (!scratch) {
			err = -ENOMEM;
			break;
		}

		old_scratch = rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
					scratch, lockdep_is_held(&cpool_mutex));
		if (!cpu_online(cpu) || !old_scratch) {
			kfree(old_scratch);
			continue;
		}
		stf->scratches[stf->cnt++] = old_scratch;
	}
	cpus_read_unlock();
	if (!err)
		__scratch_size = size;

	call_rcu(&stf->rcu, free_old_scratches);
	return err;
}

static void sigpool_scratch_free(void)
{
	int cpu;

	for_each_possible_cpu(cpu)
		kfree(rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
					  NULL, lockdep_is_held(&cpool_mutex)));
	__scratch_size = 0;
}

static int __cpool_try_clone(struct crypto_ahash *hash)
{
	struct crypto_ahash *tmp;

	tmp = crypto_clone_ahash(hash);
	if (IS_ERR(tmp))
		return PTR_ERR(tmp);

	crypto_free_ahash(tmp);
	return 0;
}

static int __cpool_alloc_ahash(struct sigpool_entry *e, const char *alg)
{
	struct crypto_ahash *cpu0_hash;
	int ret;

	e->alg = kstrdup(alg, GFP_KERNEL);
	if (!e->alg)
		return -ENOMEM;

	cpu0_hash = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC);
	if (IS_ERR(cpu0_hash)) {
		ret = PTR_ERR(cpu0_hash);
		goto out_free_alg;
	}

	e->needs_key = crypto_ahash_get_flags(cpu0_hash) & CRYPTO_TFM_NEED_KEY;

	ret = __cpool_try_clone(cpu0_hash);
	if (ret)
		goto out_free_cpu0_hash;
	e->hash = cpu0_hash;
	kref_init(&e->kref);
	return 0;

out_free_cpu0_hash:
	crypto_free_ahash(cpu0_hash);
out_free_alg:
	kfree(e->alg);
	e->alg = NULL;
	return ret;
}

/**
 * tcp_sigpool_alloc_ahash - allocates pool for ahash requests
 * @alg: name of async hash algorithm
 * @scratch_size: reserve a tcp_sigpool::scratch buffer of this size
 */
int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size)
{
	int i, ret;

	/* slow-path */
	mutex_lock(&cpool_mutex);
	ret = sigpool_reserve_scratch(scratch_size);
	if (ret)
		goto out;
	for (i = 0; i < cpool_populated; i++) {
		if (!cpool[i].alg)
			continue;
		if (strcmp(cpool[i].alg, alg))
			continue;

		/* pairs with tcp_sigpool_release() */
		if (!kref_get_unless_zero(&cpool[i].kref))
			kref_init(&cpool[i].kref);
		ret = i;
		goto out;
	}

	for (i = 0; i < cpool_populated; i++) {
		if (!cpool[i].alg)
			break;
	}
	if (i >= CPOOL_SIZE) {
		ret = -ENOSPC;
		goto out;
	}

	ret = __cpool_alloc_ahash(&cpool[i], alg);
	if (!ret) {
		ret = i;
		if (i == cpool_populated)
			cpool_populated++;
	}
out:
	mutex_unlock(&cpool_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(tcp_sigpool_alloc_ahash);

static void __cpool_free_entry(struct sigpool_entry *e)
{
	crypto_free_ahash(e->hash);
	kfree(e->alg);
	memset(e, 0, sizeof(*e));
}

static void cpool_cleanup_work_cb(struct work_struct *work)
{
	bool free_scratch = true;
	unsigned int i;

	mutex_lock(&cpool_mutex);
	for (i = 0; i < cpool_populated; i++) {
		if (kref_read(&cpool[i].kref) > 0) {
			free_scratch = false;
			continue;
		}
		if (!cpool[i].alg)
			continue;
		__cpool_free_entry(&cpool[i]);
	}
	if (free_scratch)
		sigpool_scratch_free();
	mutex_unlock(&cpool_mutex);
}

static DECLARE_WORK(cpool_cleanup_work, cpool_cleanup_work_cb);
static void cpool_schedule_cleanup(struct kref *kref)
{
	schedule_work(&cpool_cleanup_work);
}

/**
 * tcp_sigpool_release - decreases number of users for a pool. If it was
 * the last user of the pool, releases any memory that was consumed.
 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
 */
void tcp_sigpool_release(unsigned int id)
{
	if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
		return;

	/* slow-path */
	kref_put(&cpool[id].kref, cpool_schedule_cleanup);
}
EXPORT_SYMBOL_GPL(tcp_sigpool_release);

/**
 * tcp_sigpool_get - increases number of users (refcounter) for a pool
 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
 */
void tcp_sigpool_get(unsigned int id)
{
	if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
		return;
	kref_get(&cpool[id].kref);
}
EXPORT_SYMBOL_GPL(tcp_sigpool_get);

int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c) __cond_acquires(RCU_BH)
{
	struct crypto_ahash *hash;

	rcu_read_lock_bh();
	if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg)) {
		rcu_read_unlock_bh();
		return -EINVAL;
	}

	hash = crypto_clone_ahash(cpool[id].hash);
	if (IS_ERR(hash)) {
		rcu_read_unlock_bh();
		return PTR_ERR(hash);
	}

	c->req = ahash_request_alloc(hash, GFP_ATOMIC);
	if (!c->req) {
		crypto_free_ahash(hash);
		rcu_read_unlock_bh();
		return -ENOMEM;
	}
	ahash_request_set_callback(c->req, 0, NULL, NULL);

	/* Pairs with tcp_sigpool_reserve_scratch(), scratch area is
	 * valid (allocated) until tcp_sigpool_end().
	 */
	local_lock_nested_bh(&sigpool_scratch.bh_lock);
	c->scratch = rcu_dereference_bh(*this_cpu_ptr(&sigpool_scratch.pad));
	return 0;
}
EXPORT_SYMBOL_GPL(tcp_sigpool_start);

void tcp_sigpool_end(struct tcp_sigpool *c) __releases(RCU_BH)
{
	struct crypto_ahash *hash = crypto_ahash_reqtfm(c->req);

	local_unlock_nested_bh(&sigpool_scratch.bh_lock);
	rcu_read_unlock_bh();
	ahash_request_free(c->req);
	crypto_free_ahash(hash);
}
EXPORT_SYMBOL_GPL(tcp_sigpool_end);

/**
 * tcp_sigpool_algo - return algorithm of tcp_sigpool
 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
 * @buf: buffer to return name of algorithm
 * @buf_len: size of @buf
 */
size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len)
{
	if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
		return -EINVAL;

	return strscpy(buf, cpool[id].alg, buf_len);
}
EXPORT_SYMBOL_GPL(tcp_sigpool_algo);

/**
 * tcp_sigpool_hash_skb_data - hash data in skb with initialized tcp_sigpool
 * @hp: tcp_sigpool pointer
 * @skb: buffer to add sign for
 * @header_len: TCP header length for this segment
 */
int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp,
			      const struct sk_buff *skb,
			      unsigned int header_len)
{
	const unsigned int head_data_len = skb_headlen(skb) > header_len ?
					   skb_headlen(skb) - header_len : 0;
	const struct skb_shared_info *shi = skb_shinfo(skb);
	const struct tcphdr *tp = tcp_hdr(skb);
	struct ahash_request *req = hp->req;
	struct sk_buff *frag_iter;
	struct scatterlist sg;
	unsigned int i;

	sg_init_table(&sg, 1);

	sg_set_buf(&sg, ((u8 *)tp) + header_len, head_data_len);
	ahash_request_set_crypt(req, &sg, NULL, head_data_len);
	if (crypto_ahash_update(req))
		return 1;

	for (i = 0; i < shi->nr_frags; ++i) {
		const skb_frag_t *f = &shi->frags[i];
		unsigned int offset = skb_frag_off(f);
		struct page *page;

		page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
		sg_set_page(&sg, page, skb_frag_size(f), offset_in_page(offset));
		ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
		if (crypto_ahash_update(req))
			return 1;
	}

	skb_walk_frags(skb, frag_iter)
		if (tcp_sigpool_hash_skb_data(hp, frag_iter, 0))
			return 1;

	return 0;
}
EXPORT_SYMBOL(tcp_sigpool_hash_skb_data);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Per-CPU pool of crypto requests");