aboutsummaryrefslogtreecommitdiffstats
path: root/src/kern_wg.c
blob: 63eb6a3fc40fdf89dff170e3254a1eb323a79bae (plain) (blame)
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
367
368
369
370
371
372
373
374
/*
 * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>

#include <sys/antireplay.h>
#include <sys/bloombucket.h>
#include <sys/fixedmap.h>
#include <sys/mpq.h>

/*
 * antireplay.h
 */

/*
 * The following defines assist the antireplay_check function. *
 * ANTIREPLAY_INTEGER: the integer in the bitmap corresponding to num *
 * ANTIREPLAY_INTEGERBIT: the integer with corresponding single bit set
 */
#define ANTIREPLAY_INTEGER(ctx, num) (ctx->ar_bitmap[num % ARB_BITS / ARI_BITS])
#define ANTIREPLAY_INTEGERBIT(num) (1llu << (num & (ARI_BITS - 1)))

void
antireplay_init(struct antireplay *ctx)
{
	/* We just zero out the struct, expecting that then ctx->ar_head == 0 */
	explicit_bzero(ctx, sizeof(struct antireplay));
}

int
antireplay_update(struct antireplay *ctx, uint64_t num)
{
	/* Bits after ctx->ar_head need to be zeroed. This is called when num is
	 * in front of ctx->ar_head, and those bits need to be set to 0 */
	if (num < ctx->ar_head + ARB_BITS / ARI_BITS) {
		for (; ctx->ar_head <= num; ctx->ar_head += ARI_BITS) {
			ANTIREPLAY_INTEGER(ctx, (ctx->ar_head + 1)) = 0;
		}
	} else {
		bzero(ctx->ar_bitmap, ARB_BITS / ARI_BITS);
	}

	if (ctx->ar_head > (num + ARB_BITS - ARI_BITS)) {
		/* Expired */
		return 1;
	} else if (ANTIREPLAY_INTEGER(ctx, num) & ANTIREPLAY_INTEGERBIT(num)) {
		/* Replayed */
		return 1;
	} else {
		/* Unseen */
		return 0;
	}
}

/*
 * bloombucket.h
 */

void
bb_init(struct bloom_bucket *bb, size_t size, size_t keys, uint8_t interval,
		uint8_t thresh, int type, int flags)
{
	size_t i;

	for (bb->size = 1; bb->size < size; bb->size <<= 1);

	bb->type = type;
	bb->nkeys = keys;
	bb->thresh = thresh;
	bb->interval = interval;
	bb->keys = mallocarray(bb->nkeys, sizeof(*bb->keys), bb->type, flags);
	bb->bucket = mallocarray(bb->size, 1, bb->type, flags | M_ZERO);

	for (i = 0; i < bb->nkeys; i++)
		arc4random_buf(&bb->keys[i], sizeof(*bb->keys));

	timeout_set(&bb->tick, bb_tick, bb);
}

void
bb_destroy(struct bloom_bucket *bb)
{
	free(bb->keys, bb->type, bb->nkeys);
	free(bb->bucket, bb->type, bb->size);
}

void
bb_tick(void *_bb)
{
	struct bloom_bucket *bb = _bb;
	int act;
	size_t i;

	for (i = 0, act = 0; i < bb->size; i++) {
		/* Check to protect underflow, as well as flag
		 * if action has been taken, therefore should
		 * schedule again. */
		if (bb->bucket[i] > 0) {
			bb->bucket[i]--;
			act = 1;
		}
	}

	if (act && !timeout_pending(&bb->tick))
		timeout_add_sec(&bb->tick, bb->interval);
}

int
bb_recv(struct bloom_bucket *bb, uint8_t *buf, size_t n)
{
	size_t i;
	uint64_t hash;
	uint8_t *elem, min = 0xff;

	for (i = 0; i < bb->nkeys; i++) {
		hash = SipHash24(&bb->keys[i], buf, n);
		elem = &bb->bucket[hash & (bb->size - 1)];
		if (*elem < min)
			min = *elem;
		if (*elem < bb->thresh)
			(*elem)++;
	}

	if (!timeout_pending(&bb->tick))
		timeout_add_sec(&bb->tick, bb->interval);

	return min == bb->thresh;
}

uint8_t
bb_load(struct bloom_bucket *bb)
{
	size_t i;
	uint8_t log;
	uint64_t sum;
	for (i = 0, sum = 0; i < bb->size; i++)
		sum += bb->bucket[i];

	sum *= (0xffffffffffffffff / (bb->size * bb->thresh));

	log = 0;
	while (sum >>= 1)
		log++;

	return log;	
}

/*
 * fixedmap.h
 */

void	fm_resize(struct fixed_map *, size_t);

void
fm_init(struct fixed_map *fm)
{
	bzero(fm, sizeof(*fm));
	rw_init(&fm->lock, "fixedmap");
	fm_resize(fm, 128);
}

void
fm_resize(struct fixed_map *fm, size_t size)
{
	size_t		 i;
	size_t 		 osize;
	struct map_item	*omap;

	/* No lock as fm_resize is only called in fm_init (no lock) or
	 * fm_insert (already locked) */
	osize = fm->size;
	omap = fm->map;

	for (fm->size = 1; fm->size < size; fm->size <<= 1);
	KASSERT(fm->size >= osize);
	fm->map = mallocarray(fm->size, sizeof(*fm->map), M_DEVBUF, M_WAITOK | M_ZERO);

	if (omap != NULL) {
		for (i = 0; i < osize; i++)
			fm->map[omap[i].key % fm->size] = omap[i];
		free(omap, M_DEVBUF, 0);
	}
}

void
fm_destroy(struct fixed_map *fm)
{
	struct map_item *item;
	/* No lock, as this is the last reference */
	FM_FOREACH_FILLED(item, fm)
		refcnt_finalize(&item->cnt, "fm_wait");
	free(fm->map, M_DEVBUF, 0);
}

uint32_t
fm_insert(struct fixed_map *fm, void *v)
{
	struct map_item *item = NULL, *iter;

	rw_enter_write(&fm->lock);
retry:
	FM_FOREACH_EMPTY(iter, fm) {
		item = iter;
		break;
	}

	if (item == NULL) {
		fm_resize(fm, fm->size * 2);
		goto retry;
	}

	/* TODO make not zero */
	item->key = (arc4random() & ~(fm->size - 1)) + (item - fm->map);
	item->state = FM_ITEM_FILLED;
	item->value = v;
	refcnt_init(&item->cnt);

	rw_exit_write(&fm->lock);

	return item->key;
}

void
fm_put(struct fixed_map *fm, uint32_t k)
{
	struct map_item *item;

	rw_enter_read(&fm->lock);
	item = fm->map + (k & (fm->size - 1));
	if (item->key != k)
		panic("unexpected key, should exist");
	refcnt_rele_wake(&item->cnt);
	rw_exit_read(&fm->lock);
}

void *
fm_ref(struct fixed_map *fm, uint32_t k)
{
	void *v = NULL;
	struct map_item *item;

	rw_enter_read(&fm->lock);
	item = fm->map + (k & (fm->size - 1));
	if (item->key == k) {
		v = item->value;
		refcnt_take(&item->cnt);
	}
	rw_exit_read(&fm->lock);
	return v;
}

void
fm_remove(struct fixed_map *fm, uint32_t k)
{
	struct map_item *item;

	rw_enter_read(&fm->lock);
	item = fm->map + (k & (fm->size - 1));
	refcnt_finalize(&item->cnt, "fm_wait");
	rw_exit_read(&fm->lock);

	rw_enter_write(&fm->lock);
	if (item->key == k) {
		item->state = FM_ITEM_EMPTY;
		item->value = NULL;
	}
	rw_exit_write(&fm->lock);
}

/*
 * mpq.h
 */

void
mpq_init(struct mpq *mpq, int ipl)
{
	mpq->mpq_cursor = NULL;
	mpq->mpq_serializer = NULL;
	mtx_init(&mpq->mpq_mtx, ipl);
	ml_init(&mpq->mpq_list);
}

int
mpq_full(struct mpq *mpq)
{
	int full;
	mtx_enter(&mpq->mpq_mtx);
	full = ml_len(&mpq->mpq_list) >= MPQ_LEN;
	mtx_leave(&mpq->mpq_mtx);
	return full;
}

int
mpq_serialize_try_enter(struct mpq *mpq)
{
	int error = 1;
	mtx_enter(&mpq->mpq_mtx);
	if (mpq->mpq_serializer == NULL) {
		mpq->mpq_serializer = curcpu();
		error = 0;
	}
	mtx_leave(&mpq->mpq_mtx);
	return error;
}

void
mpq_serialize_leave(struct mpq *mpq)
{
	mtx_enter(&mpq->mpq_mtx);
	mpq->mpq_serializer = NULL;
	mtx_leave(&mpq->mpq_mtx);
}

void
mpq_threaddone(struct mpq *mpq, struct mbuf *m)
{
	mtx_enter(&mpq->mpq_mtx);
	SET(m->m_flags, M_LINK0);
	mtx_leave(&mpq->mpq_mtx);
}

void
mpq_enqueue(struct mpq *mpq, struct mbuf *m)
{
	CLR(m->m_flags, M_LINK0);
	mtx_enter(&mpq->mpq_mtx);
	if (ml_len(&mpq->mpq_list) < MPQ_LEN) {
		ml_enqueue(&mpq->mpq_list, m);
		if (mpq->mpq_cursor == NULL)
			mpq->mpq_cursor = m;
	} else {
		m_freem(m);
	}
	mtx_leave(&mpq->mpq_mtx);
}

struct mbuf
*mpq_dethread(struct mpq *mpq)
{
	struct mbuf *m;
	mtx_enter(&mpq->mpq_mtx);
	m = mpq->mpq_cursor;
	if (mpq->mpq_cursor != NULL)
		mpq->mpq_cursor = MBUF_LIST_NEXT(mpq->mpq_cursor);
	mtx_leave(&mpq->mpq_mtx);
	return m;
}

struct mbuf
*mpq_dequeue(struct mpq *mpq)
{
	struct mbuf *m;
	mtx_enter(&mpq->mpq_mtx);
	m = MBUF_LIST_FIRST(&mpq->mpq_list);
	if (m != NULL && ISSET(m->m_flags, M_LINK0))
		m = ml_dequeue(&mpq->mpq_list);
	else
		m = NULL;
	mtx_leave(&mpq->mpq_mtx);
	return m;
}