libxt_hashlimit.c 45.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
/* ip6tables match extension for limiting packets per destination
 *
 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
 *
 * Development of this code was funded by Astaro AG, http://www.astaro.com/
 *
 * Based on ipt_limit.c by
 * Jérôme de Vivie   <devivie@info.enserb.u-bordeaux.fr>
 * Hervé Eychenne    <rv@wallfire.org>
10
 *
11
12
13
 * Error corections by nmalykh@bilim.com (22.01.2005)
 */
#define _BSD_SOURCE 1
14
#define _DEFAULT_SOURCE 1
15
#define _ISOC99_SOURCE 1
16
#include <inttypes.h>
17
18
19
20
21
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
22
#include <errno.h>
23
24
25
26
27
#include <xtables.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_hashlimit.h>

#define XT_HASHLIMIT_BURST	5
28
29
#define XT_HASHLIMIT_BURST_MAX_v1	10000
#define XT_HASHLIMIT_BURST_MAX		1000000
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

#define XT_HASHLIMIT_BYTE_EXPIRE	15
#define XT_HASHLIMIT_BYTE_EXPIRE_BURST	60

/* miliseconds */
#define XT_HASHLIMIT_GCINTERVAL	1000

struct hashlimit_mt_udata {
	uint32_t mult;
};

static void hashlimit_help(void)
{
	printf(
"hashlimit match options:\n"
"--hashlimit <avg>		max average match rate\n"
"                                [Packets per second unless followed by \n"
"                                /sec /minute /hour /day postfixes]\n"
"--hashlimit-mode <mode>		mode is a comma-separated list of\n"
"					dstip,srcip,dstport,srcport\n"
"--hashlimit-name <name>		name for /proc/net/ipt_hashlimit/\n"
"[--hashlimit-burst <num>]	number to match in a burst, default %u\n"
"[--hashlimit-htable-size <num>]	number of hashtable buckets\n"
"[--hashlimit-htable-max <num>]	number of hashtable entries\n"
"[--hashlimit-htable-gcinterval]	interval between garbage collection runs\n"
"[--hashlimit-htable-expire]	after which time are idle entries expired?\n",
XT_HASHLIMIT_BURST);
}

enum {
	O_UPTO = 0,
	O_ABOVE,
	O_LIMIT,
	O_MODE,
	O_SRCMASK,
	O_DSTMASK,
	O_NAME,
	O_BURST,
	O_HTABLE_SIZE,
	O_HTABLE_MAX,
	O_HTABLE_GCINT,
	O_HTABLE_EXPIRE,
72
73
	O_RATEMATCH,
	O_INTERVAL,
74
75
76
77
	F_BURST         = 1 << O_BURST,
	F_UPTO          = 1 << O_UPTO,
	F_ABOVE         = 1 << O_ABOVE,
	F_HTABLE_EXPIRE = 1 << O_HTABLE_EXPIRE,
78
	F_RATEMATCH	= 1 << O_RATEMATCH,
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
};

static void hashlimit_mt_help(void)
{
	printf(
"hashlimit match options:\n"
"  --hashlimit-upto <avg>           max average match rate\n"
"                                   [Packets per second unless followed by \n"
"                                   /sec /minute /hour /day postfixes]\n"
"  --hashlimit-above <avg>          min average match rate\n"
"  --hashlimit-mode <mode>          mode is a comma-separated list of\n"
"                                   dstip,srcip,dstport,srcport (or none)\n"
"  --hashlimit-srcmask <length>     source address grouping prefix length\n"
"  --hashlimit-dstmask <length>     destination address grouping prefix length\n"
"  --hashlimit-name <name>          name for /proc/net/ipt_hashlimit\n"
"  --hashlimit-burst <num>	    number to match in a burst, default %u\n"
"  --hashlimit-htable-size <num>    number of hashtable buckets\n"
"  --hashlimit-htable-max <num>     number of hashtable entries\n"
"  --hashlimit-htable-gcinterval    interval between garbage collection runs\n"
"  --hashlimit-htable-expire        after which time are idle entries expired?\n"
"\n", XT_HASHLIMIT_BURST);
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
static void hashlimit_mt_help_v3(void)
{
	printf(
"hashlimit match options:\n"
"  --hashlimit-upto <avg>           max average match rate\n"
"                                   [Packets per second unless followed by \n"
"                                   /sec /minute /hour /day postfixes]\n"
"  --hashlimit-above <avg>          min average match rate\n"
"  --hashlimit-mode <mode>          mode is a comma-separated list of\n"
"                                   dstip,srcip,dstport,srcport (or none)\n"
"  --hashlimit-srcmask <length>     source address grouping prefix length\n"
"  --hashlimit-dstmask <length>     destination address grouping prefix length\n"
"  --hashlimit-name <name>          name for /proc/net/ipt_hashlimit\n"
"  --hashlimit-burst <num>	    number to match in a burst, default %u\n"
"  --hashlimit-htable-size <num>    number of hashtable buckets\n"
"  --hashlimit-htable-max <num>     number of hashtable entries\n"
"  --hashlimit-htable-gcinterval    interval between garbage collection runs\n"
"  --hashlimit-htable-expire        after which time are idle entries expired?\n"
"  --hashlimit-rate-match           rate match the flow without rate-limiting it\n"
"  --hashlimit-rate-interval        interval in seconds for hashlimit-rate-match\n"
"\n", XT_HASHLIMIT_BURST);
}

125
126
127
128
129
#define s struct xt_hashlimit_info
static const struct xt_option_entry hashlimit_opts[] = {
	{.name = "hashlimit", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING},
	{.name = "hashlimit-burst", .id = O_BURST, .type = XTTYPE_UINT32,
130
	 .min = 1, .max = XT_HASHLIMIT_BURST_MAX_v1, .flags = XTOPT_PUT,
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	 XTOPT_POINTER(s, cfg.burst)},
	{.name = "hashlimit-htable-size", .id = O_HTABLE_SIZE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.size)},
	{.name = "hashlimit-htable-max", .id = O_HTABLE_MAX,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.max)},
	{.name = "hashlimit-htable-gcinterval", .id = O_HTABLE_GCINT,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.gc_interval)},
	{.name = "hashlimit-htable-expire", .id = O_HTABLE_EXPIRE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.expire)},
	{.name = "hashlimit-mode", .id = O_MODE, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND},
	{.name = "hashlimit-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name), .min = 1},
	XTOPT_TABLEEND,
};
#undef s

#define s struct xt_hashlimit_mtinfo1
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
static const struct xt_option_entry hashlimit_mt_opts_v1[] = {
	{.name = "hashlimit-upto", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit-above", .id = O_ABOVE, .excl = F_UPTO,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT}, /* old name */
	{.name = "hashlimit-srcmask", .id = O_SRCMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-dstmask", .id = O_DSTMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-burst", .id = O_BURST, .type = XTTYPE_STRING},
	{.name = "hashlimit-htable-size", .id = O_HTABLE_SIZE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.size)},
	{.name = "hashlimit-htable-max", .id = O_HTABLE_MAX,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.max)},
	{.name = "hashlimit-htable-gcinterval", .id = O_HTABLE_GCINT,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.gc_interval)},
	{.name = "hashlimit-htable-expire", .id = O_HTABLE_EXPIRE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.expire)},
	{.name = "hashlimit-mode", .id = O_MODE, .type = XTTYPE_STRING},
	{.name = "hashlimit-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name), .min = 1},
	XTOPT_TABLEEND,
};
#undef s

#define s struct xt_hashlimit_mtinfo2
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
static const struct xt_option_entry hashlimit_mt_opts_v2[] = {
	{.name = "hashlimit-upto", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit-above", .id = O_ABOVE, .excl = F_UPTO,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT}, /* old name */
	{.name = "hashlimit-srcmask", .id = O_SRCMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-dstmask", .id = O_DSTMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-burst", .id = O_BURST, .type = XTTYPE_STRING},
	{.name = "hashlimit-htable-size", .id = O_HTABLE_SIZE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.size)},
	{.name = "hashlimit-htable-max", .id = O_HTABLE_MAX,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.max)},
	{.name = "hashlimit-htable-gcinterval", .id = O_HTABLE_GCINT,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.gc_interval)},
	{.name = "hashlimit-htable-expire", .id = O_HTABLE_EXPIRE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.expire)},
	{.name = "hashlimit-mode", .id = O_MODE, .type = XTTYPE_STRING},
	{.name = "hashlimit-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name), .min = 1},
};
#undef s

#define s struct xt_hashlimit_mtinfo3
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
static const struct xt_option_entry hashlimit_mt_opts[] = {
	{.name = "hashlimit-upto", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit-above", .id = O_ABOVE, .excl = F_UPTO,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
	{.name = "hashlimit", .id = O_UPTO, .excl = F_ABOVE,
	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT}, /* old name */
	{.name = "hashlimit-srcmask", .id = O_SRCMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-dstmask", .id = O_DSTMASK, .type = XTTYPE_PLEN},
	{.name = "hashlimit-burst", .id = O_BURST, .type = XTTYPE_STRING},
	{.name = "hashlimit-htable-size", .id = O_HTABLE_SIZE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.size)},
	{.name = "hashlimit-htable-max", .id = O_HTABLE_MAX,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.max)},
	{.name = "hashlimit-htable-gcinterval", .id = O_HTABLE_GCINT,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.gc_interval)},
	{.name = "hashlimit-htable-expire", .id = O_HTABLE_EXPIRE,
	 .type = XTTYPE_UINT32, .flags = XTOPT_PUT,
	 XTOPT_POINTER(s, cfg.expire)},
	{.name = "hashlimit-mode", .id = O_MODE, .type = XTTYPE_STRING},
	{.name = "hashlimit-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, name), .min = 1},
237
238
	{.name = "hashlimit-rate-match", .id = O_RATEMATCH, .type = XTTYPE_NONE},
	{.name = "hashlimit-rate-interval", .id = O_INTERVAL, .type = XTTYPE_STRING},
239
240
241
242
	XTOPT_TABLEEND,
};
#undef s

243
static int
244
cfg_copy(struct hashlimit_cfg3 *to, const void *from, int revision)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{
	if (revision == 1) {
		struct hashlimit_cfg1 *cfg = (struct hashlimit_cfg1 *)from;

		to->mode = cfg->mode;
		to->avg = cfg->avg;
		to->burst = cfg->burst;
		to->size = cfg->size;
		to->max = cfg->max;
		to->gc_interval = cfg->gc_interval;
		to->expire = cfg->expire;
		to->srcmask = cfg->srcmask;
		to->dstmask = cfg->dstmask;
	} else if (revision == 2) {
259
260
261
262
263
264
265
266
267
268
269
270
271
		struct hashlimit_cfg2 *cfg = (struct hashlimit_cfg2 *)from;

		to->mode = cfg->mode;
		to->avg = cfg->avg;
		to->burst = cfg->burst;
		to->size = cfg->size;
		to->max = cfg->max;
		to->gc_interval = cfg->gc_interval;
		to->expire = cfg->expire;
		to->srcmask = cfg->srcmask;
		to->dstmask = cfg->dstmask;
	} else if (revision == 3) {
		memcpy(to, from, sizeof(struct hashlimit_cfg3));
272
273
274
275
276
277
278
279
	} else {
		return -EINVAL;
	}

	return 0;
}

static uint64_t cost_to_bytes(uint64_t cost)
280
{
281
	uint64_t r;
282
283
284
285
286
287

	r = cost ? UINT32_MAX / cost : UINT32_MAX;
	r = (r - 1) << XT_HASHLIMIT_BYTE_SHIFT;
	return r;
}

288
static uint64_t bytes_to_cost(uint64_t bytes)
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{
	uint32_t r = bytes >> XT_HASHLIMIT_BYTE_SHIFT;
	return UINT32_MAX / (r+1);
}

static uint32_t get_factor(int chr)
{
	switch (chr) {
	case 'm': return 1024 * 1024;
	case 'k': return 1024;
	}
	return 1;
}

303
304
305
306
307
308
static void burst_error_v1(void)
{
	xtables_error(PARAMETER_PROBLEM, "bad value for option "
			"\"--hashlimit-burst\", or out of range (1-%u).", XT_HASHLIMIT_BURST_MAX_v1);
}

309
310
311
312
313
314
static void burst_error(void)
{
	xtables_error(PARAMETER_PROBLEM, "bad value for option "
			"\"--hashlimit-burst\", or out of range (1-%u).", XT_HASHLIMIT_BURST_MAX);
}

315
static uint64_t parse_burst(const char *burst, int revision)
316
317
318
{
	uintmax_t v;
	char *end;
319
320
321
322
323
324
325
326
327
328
329
	uint64_t max = (revision == 1) ? UINT32_MAX : UINT64_MAX;
	uint64_t burst_max = (revision == 1) ?
			      XT_HASHLIMIT_BURST_MAX_v1 : XT_HASHLIMIT_BURST_MAX;

	if (!xtables_strtoul(burst, &end, &v, 1, max) ||
		(*end == 0 && v > burst_max)) {
		if (revision == 1)
			burst_error_v1();
		else
			burst_error();
	}
330
331

	v *= get_factor(*end);
332
	if (v > max)
333
334
		xtables_error(PARAMETER_PROBLEM, "bad value for option "
			"\"--hashlimit-burst\", value \"%s\" too large "
335
				"(max %"PRIu64"mb).", burst, max/1024/1024);
336
337
338
	return v;
}

339
static bool parse_bytes(const char *rate, void *val, struct hashlimit_mt_udata *ud, int revision)
340
341
{
	unsigned int factor = 1;
342
	uint64_t tmp, r;
343
	const char *mode = strstr(rate, "b/s");
344
345
	uint64_t max = (revision == 1) ? UINT32_MAX : UINT64_MAX;

346
347
348
349
	if (!mode || mode == rate)
		return false;

	mode--;
350
	r = atoll(rate);
351
352
353
354
355
	if (r == 0)
		return false;

	factor = get_factor(*mode);
	tmp = (uint64_t) r * factor;
356
	if (tmp > max)
357
		xtables_error(PARAMETER_PROBLEM,
358
359
			"Rate value too large \"%"PRIu64"\" (max %"PRIu64")\n",
					tmp, max);
360

361
362
	tmp = bytes_to_cost(tmp);
	if (tmp == 0)
363
364
365
		xtables_error(PARAMETER_PROBLEM, "Rate too high \"%s\"\n", rate);

	ud->mult = XT_HASHLIMIT_BYTE_EXPIRE;
366
367
368
369
370
371

	if(revision == 1)
		*((uint32_t*)val) = tmp;
	else
		*((uint64_t*)val) = tmp;

372
373
374
375
	return true;
}

static
376
int parse_rate(const char *rate, void *val, struct hashlimit_mt_udata *ud, int revision)
377
378
{
	const char *delim;
379
380
	uint64_t tmp, r;
	uint64_t scale = (revision == 1) ? XT_HASHLIMIT_SCALE : XT_HASHLIMIT_SCALE_v2;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

	ud->mult = 1;  /* Seconds by default. */
	delim = strchr(rate, '/');
	if (delim) {
		if (strlen(delim+1) == 0)
			return 0;

		if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
			ud->mult = 1;
		else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
			ud->mult = 60;
		else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
			ud->mult = 60*60;
		else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
			ud->mult = 24*60*60;
		else
			return 0;
	}
399
	r = atoll(rate);
400
401
402
	if (!r)
		return 0;

403
404
	tmp = scale * ud->mult / r;
	if (tmp == 0)
405
406
407
408
409
		/*
		 * The rate maps to infinity. (1/day is the minimum they can
		 * specify, so we are ok at that end).
		 */
		xtables_error(PARAMETER_PROBLEM, "Rate too fast \"%s\"\n", rate);
410
411
412
413
414
415

	if(revision == 1)
		*((uint32_t*)val) = tmp;
	else
		*((uint64_t*)val) = tmp;

416
417
418
	return 1;
}

419
420
421
422
423
424
425
426
427
428
static int parse_interval(const char *rate, uint32_t *val)
{
	int r = atoi(rate);
	if (r <= 0)
		return 0;

	*val = r;
	return 1;
}

429
430
431
432
433
434
435
436
437
static void hashlimit_init(struct xt_entry_match *m)
{
	struct xt_hashlimit_info *r = (struct xt_hashlimit_info *)m->data;

	r->cfg.burst = XT_HASHLIMIT_BURST;
	r->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;

}

438
static void hashlimit_mt4_init_v1(struct xt_entry_match *match)
439
440
441
442
443
444
445
446
447
448
{
	struct xt_hashlimit_mtinfo1 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 32;
	info->cfg.dstmask     = 32;
}

449
static void hashlimit_mt6_init_v1(struct xt_entry_match *match)
450
451
452
453
454
455
456
457
458
459
{
	struct xt_hashlimit_mtinfo1 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 128;
	info->cfg.dstmask     = 128;
}

460
static void hashlimit_mt4_init_v2(struct xt_entry_match *match)
461
462
463
464
465
466
467
468
469
470
{
	struct xt_hashlimit_mtinfo2 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 32;
	info->cfg.dstmask     = 32;
}

471
static void hashlimit_mt6_init_v2(struct xt_entry_match *match)
472
473
474
475
476
477
478
479
480
481
{
	struct xt_hashlimit_mtinfo2 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 128;
	info->cfg.dstmask     = 128;
}

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
static void hashlimit_mt4_init(struct xt_entry_match *match)
{
	struct xt_hashlimit_mtinfo3 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 32;
	info->cfg.dstmask     = 32;
	info->cfg.interval    = 0;
}

static void hashlimit_mt6_init(struct xt_entry_match *match)
{
	struct xt_hashlimit_mtinfo3 *info = (void *)match->data;

	info->cfg.mode        = 0;
	info->cfg.burst       = XT_HASHLIMIT_BURST;
	info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
	info->cfg.srcmask     = 128;
	info->cfg.dstmask     = 128;
	info->cfg.interval    = 0;
}

506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* Parse a 'mode' parameter into the required bitmask */
static int parse_mode(uint32_t *mode, const char *option_arg)
{
	char *tok;
	char *arg = strdup(option_arg);

	if (!arg)
		return -1;

	for (tok = strtok(arg, ",|");
	     tok;
	     tok = strtok(NULL, ",|")) {
		if (!strcmp(tok, "dstip"))
			*mode |= XT_HASHLIMIT_HASH_DIP;
		else if (!strcmp(tok, "srcip"))
			*mode |= XT_HASHLIMIT_HASH_SIP;
		else if (!strcmp(tok, "srcport"))
			*mode |= XT_HASHLIMIT_HASH_SPT;
		else if (!strcmp(tok, "dstport"))
			*mode |= XT_HASHLIMIT_HASH_DPT;
		else {
			free(arg);
			return -1;
		}
	}
	free(arg);
	return 0;
}

static void hashlimit_parse(struct xt_option_call *cb)
{
	struct xt_hashlimit_info *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_UPTO:
542
		if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 1))
543
544
545
546
547
548
549
550
551
552
553
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-upto", cb->arg);
		break;
	case O_MODE:
		if (parse_mode(&info->cfg.mode, cb->arg) < 0)
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-mode", cb->arg);
		break;
	}
}

554
static void hashlimit_mt_parse_v1(struct xt_option_call *cb)
555
556
557
558
559
560
{
	struct xt_hashlimit_mtinfo1 *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_BURST:
561
		info->cfg.burst = parse_burst(cb->arg, 1);
562
563
564
565
		break;
	case O_UPTO:
		if (cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
566
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 1))
567
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
568
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 1))
569
570
571
572
573
574
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-upto", cb->arg);
		break;
	case O_ABOVE:
		if (!cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
575
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 1))
576
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 1))
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-above", cb->arg);
		break;
	case O_MODE:
		if (parse_mode(&info->cfg.mode, cb->arg) < 0)
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-mode", cb->arg);
		break;
	case O_SRCMASK:
		info->cfg.srcmask = cb->val.hlen;
		break;
	case O_DSTMASK:
		info->cfg.dstmask = cb->val.hlen;
		break;
	}
}

595
static void hashlimit_mt_parse_v2(struct xt_option_call *cb)
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
{
	struct xt_hashlimit_mtinfo2 *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_BURST:
		info->cfg.burst = parse_burst(cb->arg, 2);
		break;
	case O_UPTO:
		if (cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 2))
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 2))
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-upto", cb->arg);
		break;
	case O_ABOVE:
		if (!cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 2))
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 2))
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-above", cb->arg);
		break;
	case O_MODE:
		if (parse_mode(&info->cfg.mode, cb->arg) < 0)
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-mode", cb->arg);
		break;
	case O_SRCMASK:
		info->cfg.srcmask = cb->val.hlen;
		break;
	case O_DSTMASK:
		info->cfg.dstmask = cb->val.hlen;
		break;
	}
}

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
static void hashlimit_mt_parse(struct xt_option_call *cb)
{
	struct xt_hashlimit_mtinfo3 *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_BURST:
		info->cfg.burst = parse_burst(cb->arg, 2);
		break;
	case O_UPTO:
		if (cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 2))
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 2))
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-upto", cb->arg);
		break;
	case O_ABOVE:
		if (!cb->invert)
			info->cfg.mode |= XT_HASHLIMIT_INVERT;
		if (parse_bytes(cb->arg, &info->cfg.avg, cb->udata, 2))
			info->cfg.mode |= XT_HASHLIMIT_BYTES;
		else if (!parse_rate(cb->arg, &info->cfg.avg, cb->udata, 2))
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-above", cb->arg);
		break;
	case O_MODE:
		if (parse_mode(&info->cfg.mode, cb->arg) < 0)
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
			          "--hashlimit-mode", cb->arg);
		break;
	case O_SRCMASK:
		info->cfg.srcmask = cb->val.hlen;
		break;
	case O_DSTMASK:
		info->cfg.dstmask = cb->val.hlen;
		break;
	case O_RATEMATCH:
		info->cfg.mode |= XT_HASHLIMIT_RATE_MATCH;
		break;
	case O_INTERVAL:
		if (!parse_interval(cb->arg, &info->cfg.interval))
			xtables_param_act(XTF_BAD_VALUE, "hashlimit",
				"--hashlimit-rate-interval", cb->arg);
	}
}

684
685
686
687
688
689
690
691
692
693
694
695
static void hashlimit_check(struct xt_fcheck_call *cb)
{
	const struct hashlimit_mt_udata *udata = cb->udata;
	struct xt_hashlimit_info *info = cb->data;

	if (!(cb->xflags & (F_UPTO | F_ABOVE)))
		xtables_error(PARAMETER_PROBLEM,
				"You have to specify --hashlimit");
	if (!(cb->xflags & F_HTABLE_EXPIRE))
		info->cfg.expire = udata->mult * 1000; /* from s to msec */
}

696
static void hashlimit_mt_check_v1(struct xt_fcheck_call *cb)
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
{
	const struct hashlimit_mt_udata *udata = cb->udata;
	struct xt_hashlimit_mtinfo1 *info = cb->data;

	if (!(cb->xflags & (F_UPTO | F_ABOVE)))
		xtables_error(PARAMETER_PROBLEM,
				"You have to specify --hashlimit");
	if (!(cb->xflags & F_HTABLE_EXPIRE))
		info->cfg.expire = udata->mult * 1000; /* from s to msec */

	if (info->cfg.mode & XT_HASHLIMIT_BYTES) {
		uint32_t burst = 0;
		if (cb->xflags & F_BURST) {
			if (info->cfg.burst < cost_to_bytes(info->cfg.avg))
				xtables_error(PARAMETER_PROBLEM,
712
713
					"burst cannot be smaller than %"PRIu64"b",
					cost_to_bytes(info->cfg.avg));
714
715
716
717
718
719
720
721
722
723
724
725
726

			burst = info->cfg.burst;
			burst /= cost_to_bytes(info->cfg.avg);
			if (info->cfg.burst % cost_to_bytes(info->cfg.avg))
				burst++;
			if (!(cb->xflags & F_HTABLE_EXPIRE))
				info->cfg.expire = XT_HASHLIMIT_BYTE_EXPIRE_BURST * 1000;
		}
		info->cfg.burst = burst;
	} else if (info->cfg.burst > XT_HASHLIMIT_BURST_MAX_v1)
		burst_error_v1();
}

727
static void hashlimit_mt_check_v2(struct xt_fcheck_call *cb)
728
729
730
731
{
	const struct hashlimit_mt_udata *udata = cb->udata;
	struct xt_hashlimit_mtinfo2 *info = cb->data;

732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	if (!(cb->xflags & (F_UPTO | F_ABOVE)))
		xtables_error(PARAMETER_PROBLEM,
				"You have to specify --hashlimit");
	if (!(cb->xflags & F_HTABLE_EXPIRE))
		info->cfg.expire = udata->mult * 1000; /* from s to msec */

	if (info->cfg.mode & XT_HASHLIMIT_BYTES) {
		uint32_t burst = 0;
		if (cb->xflags & F_BURST) {
			if (info->cfg.burst < cost_to_bytes(info->cfg.avg))
				xtables_error(PARAMETER_PROBLEM,
					"burst cannot be smaller than %"PRIu64"b",
					cost_to_bytes(info->cfg.avg));

			burst = info->cfg.burst;
			burst /= cost_to_bytes(info->cfg.avg);
			if (info->cfg.burst % cost_to_bytes(info->cfg.avg))
				burst++;
			if (!(cb->xflags & F_HTABLE_EXPIRE))
				info->cfg.expire = XT_HASHLIMIT_BYTE_EXPIRE_BURST * 1000;
		}
		info->cfg.burst = burst;
	} else if (info->cfg.burst > XT_HASHLIMIT_BURST_MAX)
		burst_error();
}

static void hashlimit_mt_check(struct xt_fcheck_call *cb)
{
	const struct hashlimit_mt_udata *udata = cb->udata;
	struct xt_hashlimit_mtinfo3 *info = cb->data;

763
764
765
766
767
768
769
770
771
772
773
774
	if (!(cb->xflags & (F_UPTO | F_ABOVE)))
		xtables_error(PARAMETER_PROBLEM,
				"You have to specify --hashlimit");
	if (!(cb->xflags & F_HTABLE_EXPIRE))
		info->cfg.expire = udata->mult * 1000; /* from s to msec */

	if (info->cfg.mode & XT_HASHLIMIT_BYTES) {
		uint32_t burst = 0;
		if (cb->xflags & F_BURST) {
			if (info->cfg.burst < cost_to_bytes(info->cfg.avg))
				xtables_error(PARAMETER_PROBLEM,
					"burst cannot be smaller than %lub", cost_to_bytes(info->cfg.avg));
775
776
777
778
779
780
781
782
783
784
785

			burst = info->cfg.burst;
			burst /= cost_to_bytes(info->cfg.avg);
			if (info->cfg.burst % cost_to_bytes(info->cfg.avg))
				burst++;
			if (!(cb->xflags & F_HTABLE_EXPIRE))
				info->cfg.expire = XT_HASHLIMIT_BYTE_EXPIRE_BURST * 1000;
		}
		info->cfg.burst = burst;
	} else if (info->cfg.burst > XT_HASHLIMIT_BURST_MAX)
		burst_error();
786
787
788
789
790
791
792
793
794
795
796
797

	if (cb->xflags & F_RATEMATCH) {
		if (!(info->cfg.mode & XT_HASHLIMIT_BYTES))
			info->cfg.avg /= udata->mult;

		if (info->cfg.interval == 0) {
			if (info->cfg.mode & XT_HASHLIMIT_BYTES)
				info->cfg.interval = 1;
			else
				info->cfg.interval = udata->mult;
		}
	}
798
799
}

800
struct rates {
801
	const char *name;
802
803
804
805
806
807
808
809
810
811
812
813
	uint64_t mult;
} rates_v1[] = { { "day", XT_HASHLIMIT_SCALE*24*60*60 },
		 { "hour", XT_HASHLIMIT_SCALE*60*60 },
		 { "min", XT_HASHLIMIT_SCALE*60 },
		 { "sec", XT_HASHLIMIT_SCALE } };

static const struct rates rates[] = {
	{ "day", XT_HASHLIMIT_SCALE_v2*24*60*60 },
	{ "hour", XT_HASHLIMIT_SCALE_v2*60*60 },
	{ "min", XT_HASHLIMIT_SCALE_v2*60 },
	{ "sec", XT_HASHLIMIT_SCALE_v2 } };

814
static uint32_t print_rate(uint64_t period, int revision)
815
816
{
	unsigned int i;
817
818
	const struct rates *_rates = (revision == 1) ? rates_v1 : rates;
	uint64_t scale = (revision == 1) ? XT_HASHLIMIT_SCALE : XT_HASHLIMIT_SCALE_v2;
819
820
821
822
823
824
825

	if (period == 0) {
		printf(" %f", INFINITY);
		return 0;
	}

	for (i = 1; i < ARRAY_SIZE(rates); ++i)
826
827
		if (period > _rates[i].mult
            || _rates[i].mult/period < _rates[i].mult%period)
828
829
			break;

830
	printf(" %"PRIu64"/%s", _rates[i-1].mult / period, _rates[i-1].name);
831
	/* return in msec */
832
	return _rates[i-1].mult / scale * 1000;
833
834
835
836
837
838
839
840
841
842
843
}

static const struct {
	const char *name;
	uint32_t thresh;
} units[] = {
	{ "m", 1024 * 1024 },
	{ "k", 1024 },
	{ "", 1 },
};

844
static uint32_t print_bytes(uint64_t avg, uint64_t burst, const char *prefix)
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
{
	unsigned int i;
	unsigned long long r;

	r = cost_to_bytes(avg);

	for (i = 0; i < ARRAY_SIZE(units) -1; ++i)
		if (r >= units[i].thresh &&
		    bytes_to_cost(r & ~(units[i].thresh - 1)) == avg)
			break;
	printf(" %llu%sb/s", r/units[i].thresh, units[i].name);

	if (burst == 0)
		return XT_HASHLIMIT_BYTE_EXPIRE * 1000;

	r *= burst;
	printf(" %s", prefix);
	for (i = 0; i < ARRAY_SIZE(units) -1; ++i)
		if (r >= units[i].thresh)
			break;

	printf("burst %llu%sb", r / units[i].thresh, units[i].name);
	return XT_HASHLIMIT_BYTE_EXPIRE_BURST * 1000;
}

static void print_mode(unsigned int mode, char separator)
{
	bool prevmode = false;

	putchar(' ');
	if (mode & XT_HASHLIMIT_HASH_SIP) {
		fputs("srcip", stdout);
		prevmode = 1;
	}
	if (mode & XT_HASHLIMIT_HASH_SPT) {
		if (prevmode)
			putchar(separator);
		fputs("srcport", stdout);
		prevmode = 1;
	}
	if (mode & XT_HASHLIMIT_HASH_DIP) {
		if (prevmode)
			putchar(separator);
		fputs("dstip", stdout);
		prevmode = 1;
	}
	if (mode & XT_HASHLIMIT_HASH_DPT) {
		if (prevmode)
			putchar(separator);
		fputs("dstport", stdout);
	}
}

static void hashlimit_print(const void *ip,
                            const struct xt_entry_match *match, int numeric)
{
	const struct xt_hashlimit_info *r = (const void *)match->data;
	uint32_t quantum;

	fputs(" limit: avg", stdout);
905
	quantum = print_rate(r->cfg.avg, 1);
906
907
908
909
910
911
912
913
914
915
916
917
918
919
	printf(" burst %u", r->cfg.burst);
	fputs(" mode", stdout);
	print_mode(r->cfg.mode, '-');
	if (r->cfg.size)
		printf(" htable-size %u", r->cfg.size);
	if (r->cfg.max)
		printf(" htable-max %u", r->cfg.max);
	if (r->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
		printf(" htable-gcinterval %u", r->cfg.gc_interval);
	if (r->cfg.expire != quantum)
		printf(" htable-expire %u", r->cfg.expire);
}

static void
920
hashlimit_mt_print(const struct hashlimit_cfg3 *cfg, unsigned int dmask, int revision)
921
{
922
923
	uint64_t quantum;
	uint64_t period;
924

925
	if (cfg->mode & XT_HASHLIMIT_INVERT)
926
927
928
929
		fputs(" limit: above", stdout);
	else
		fputs(" limit: up to", stdout);

930
931
	if (cfg->mode & XT_HASHLIMIT_BYTES) {
		quantum = print_bytes(cfg->avg, cfg->burst, "");
932
	} else {
933
934
935
936
937
938
939
940
941
		if (revision == 3) {
			period = cfg->avg;
			if (cfg->interval != 0)
				period *= cfg->interval;

			quantum = print_rate(period, revision);
		} else {
			quantum = print_rate(cfg->avg, revision);
		}
942
		printf(" burst %llu", cfg->burst);
943
	}
944
	if (cfg->mode & (XT_HASHLIMIT_HASH_SIP | XT_HASHLIMIT_HASH_SPT |
945
946
	    XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT)) {
		fputs(" mode", stdout);
947
		print_mode(cfg->mode, '-');
948
	}
949
950
951
952
953
954
955
956
957
958
959
960
961
	if (cfg->size != 0)
		printf(" htable-size %u", cfg->size);
	if (cfg->max != 0)
		printf(" htable-max %u", cfg->max);
	if (cfg->gc_interval != XT_HASHLIMIT_GCINTERVAL)
		printf(" htable-gcinterval %u", cfg->gc_interval);
	if (cfg->expire != quantum)
		printf(" htable-expire %u", cfg->expire);

	if (cfg->srcmask != dmask)
		printf(" srcmask %u", cfg->srcmask);
	if (cfg->dstmask != dmask)
		printf(" dstmask %u", cfg->dstmask);
962
963
964
965
966
967
968

	if ((revision == 3) && (cfg->mode & XT_HASHLIMIT_RATE_MATCH))
		printf(" rate-match");

	if ((revision == 3) && (cfg->mode & XT_HASHLIMIT_RATE_MATCH))
		if (cfg->interval != 1)
			printf(" rate-interval %u", cfg->interval);
969
970
971
972
973
974
975
}

static void
hashlimit_mt4_print_v1(const void *ip, const struct xt_entry_match *match,
                   int numeric)
{
	const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
976
	struct hashlimit_cfg3 cfg;
977
978
979
980
981
982
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 1);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");
983

984
	hashlimit_mt_print(&cfg, 32, 1);
985
986
987
}

static void
988
hashlimit_mt6_print_v1(const void *ip, const struct xt_entry_match *match,
989
990
991
                   int numeric)
{
	const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
992
	struct hashlimit_cfg3 cfg;
993
994
995
996
997
998
999
1000
1001
1002
1003
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 1);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");

	hashlimit_mt_print(&cfg, 128, 1);
}

static void
1004
hashlimit_mt4_print_v2(const void *ip, const struct xt_entry_match *match,
1005
1006
1007
                   int numeric)
{
	const struct xt_hashlimit_mtinfo2 *info = (const void *)match->data;
1008
1009
1010
1011
1012
1013
1014
	struct hashlimit_cfg3 cfg;
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 2);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");
1015

1016
	hashlimit_mt_print(&cfg, 32, 2);
1017
1018
1019
}

static void
1020
hashlimit_mt6_print_v2(const void *ip, const struct xt_entry_match *match,
1021
1022
                   int numeric)
{
1023
	const struct xt_hashlimit_mtinfo2 *info = (const void *)match->data;
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
	struct hashlimit_cfg3 cfg;
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 2);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");

	hashlimit_mt_print(&cfg, 128, 2);
}
static void
hashlimit_mt4_print(const void *ip, const struct xt_entry_match *match,
                   int numeric)
{
	const struct xt_hashlimit_mtinfo3 *info = (const void *)match->data;

	hashlimit_mt_print(&info->cfg, 32, 3);
}

static void
hashlimit_mt6_print(const void *ip, const struct xt_entry_match *match,
                   int numeric)
{
	const struct xt_hashlimit_mtinfo3 *info = (const void *)match->data;
1048

1049
	hashlimit_mt_print(&info->cfg, 128, 3);
1050
1051
1052
1053
1054
1055
1056
1057
}

static void hashlimit_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_hashlimit_info *r = (const void *)match->data;
	uint32_t quantum;

	fputs(" --hashlimit", stdout);
1058
	quantum = print_rate(r->cfg.avg, 1);
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
	printf(" --hashlimit-burst %u", r->cfg.burst);

	fputs(" --hashlimit-mode", stdout);
	print_mode(r->cfg.mode, ',');

	printf(" --hashlimit-name %s", r->name);

	if (r->cfg.size)
		printf(" --hashlimit-htable-size %u", r->cfg.size);
	if (r->cfg.max)
		printf(" --hashlimit-htable-max %u", r->cfg.max);
	if (r->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
		printf(" --hashlimit-htable-gcinterval %u", r->cfg.gc_interval);
	if (r->cfg.expire != quantum)
		printf(" --hashlimit-htable-expire %u", r->cfg.expire);
}

static void
1077
hashlimit_mt_save(const struct hashlimit_cfg3 *cfg, const char* name, unsigned int dmask, int revision)
1078
1079
1080
{
	uint32_t quantum;

1081
	if (cfg->mode & XT_HASHLIMIT_INVERT)
1082
1083
1084
1085
		fputs(" --hashlimit-above", stdout);
	else
		fputs(" --hashlimit-upto", stdout);

1086
1087
	if (cfg->mode & XT_HASHLIMIT_BYTES) {
		quantum = print_bytes(cfg->avg, cfg->burst, "--hashlimit-");
1088
	} else {
1089
1090
		quantum = print_rate(cfg->avg, revision);
		printf(" --hashlimit-burst %llu", cfg->burst);
1091
1092
	}

1093
	if (cfg->mode & (XT_HASHLIMIT_HASH_SIP | XT_HASHLIMIT_HASH_SPT |
1094
1095
	    XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT)) {
		fputs(" --hashlimit-mode", stdout);
1096
		print_mode(cfg->mode, ',');
1097
1098
	}

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
	printf(" --hashlimit-name %s", name);

	if (cfg->size != 0)
		printf(" --hashlimit-htable-size %u", cfg->size);
	if (cfg->max != 0)
		printf(" --hashlimit-htable-max %u", cfg->max);
	if (cfg->gc_interval != XT_HASHLIMIT_GCINTERVAL)
		printf(" --hashlimit-htable-gcinterval %u", cfg->gc_interval);
	if (cfg->expire != quantum)
		printf(" --hashlimit-htable-expire %u", cfg->expire);

	if (cfg->srcmask != dmask)
		printf(" --hashlimit-srcmask %u", cfg->srcmask);
	if (cfg->dstmask != dmask)
		printf(" --hashlimit-dstmask %u", cfg->dstmask);
1114
1115
1116
1117
1118
1119
1120

	if ((revision == 3) && (cfg->mode & XT_HASHLIMIT_RATE_MATCH))
		printf(" --hashlimit-rate-match");

	if ((revision == 3) && (cfg->mode & XT_HASHLIMIT_RATE_MATCH))
		if (cfg->interval != 1)
			printf(" --hashlimit-rate-interval %u", cfg->interval);
1121
1122
1123
1124
1125
1126
}

static void
hashlimit_mt4_save_v1(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
1127
	struct hashlimit_cfg3 cfg;
1128
1129
1130
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 1);
1131

1132
1133
	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");
1134

1135
	hashlimit_mt_save(&cfg, info->name, 32, 1);
1136
1137
1138
}

static void
1139
hashlimit_mt6_save_v1(const void *ip, const struct xt_entry_match *match)
1140
1141
{
	const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
1142
	struct hashlimit_cfg3 cfg;
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 1);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");

	hashlimit_mt_save(&cfg, info->name, 128, 1);
}

static void
1154
hashlimit_mt4_save_v2(const void *ip, const struct xt_entry_match *match)
1155
1156
{
	const struct xt_hashlimit_mtinfo2 *info = (const void *)match->data;
1157
1158
1159
1160
	struct hashlimit_cfg3 cfg;
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 2);
1161

1162
1163
1164
1165
	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");

	hashlimit_mt_save(&cfg, info->name, 32, 2);
1166
1167
1168
}

static void
1169
hashlimit_mt6_save_v2(const void *ip, const struct xt_entry_match *match)
1170
{
1171
	const struct xt_hashlimit_mtinfo2 *info = (const void *)match->data;
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
	struct hashlimit_cfg3 cfg;
	int ret;

	ret = cfg_copy(&cfg, (const void *)&info->cfg, 2);

	if (ret)
		xtables_error(OTHER_PROBLEM, "unknown revision");

	hashlimit_mt_save(&cfg, info->name, 128, 2);
}

static void
hashlimit_mt4_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_hashlimit_mtinfo3 *info = (const void *)match->data;

	hashlimit_mt_save(&info->cfg, info->name, 32, 3);
}

static void
hashlimit_mt6_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_hashlimit_mtinfo3 *info = (const void *)match->data;

	hashlimit_mt_save(&info->cfg, info->name, 128, 3);
}

static const struct rates rates_v1_xlate[] = {
	{ "day", XT_HASHLIMIT_SCALE * 24 * 60 * 60 },
	{ "hour", XT_HASHLIMIT_SCALE * 60 * 60 },
	{ "minute", XT_HASHLIMIT_SCALE * 60 },
	{ "second", XT_HASHLIMIT_SCALE } };

static const struct rates rates_xlate[] = {
	{ "day", XT_HASHLIMIT_SCALE_v2 * 24 * 60 * 60 },
	{ "hour", XT_HASHLIMIT_SCALE_v2 * 60 * 60 },
	{ "minute", XT_HASHLIMIT_SCALE_v2 * 60 },
	{ "second", XT_HASHLIMIT_SCALE_v2 } };

static void print_packets_rate_xlate(struct xt_xlate *xl, uint64_t avg,
				     int revision)
{
	unsigned int i;
	const struct rates *_rates = (revision == 1) ?
		rates_v1_xlate : rates_xlate;

	for (i = 1; i < ARRAY_SIZE(rates); ++i)
		if (avg > _rates[i].mult ||
		    _rates[i].mult / avg < _rates[i].mult % avg)
			break;

	xt_xlate_add(xl, " %llu/%s ",
		     _rates[i-1].mult / avg, _rates[i-1].name);
}

static void print_bytes_rate_xlate(struct xt_xlate *xl,
				   const struct hashlimit_cfg3 *cfg)
{
	unsigned int i;
	unsigned long long r;

	r = cost_to_bytes(cfg->avg);

	for (i = 0; i < ARRAY_SIZE(units) -1; ++i)
		if (r >= units[i].thresh &&
		    bytes_to_cost(r & ~(units[i].thresh - 1)) == cfg->avg)
			break;

	xt_xlate_add(xl, " %llu %sbytes/second", r / units[i].thresh,
		     units[i].name);

	r *= cfg->burst;
	for (i = 0; i < ARRAY_SIZE(units) -1; ++i)
		if (r >= units[i].thresh)
			break;

	if (cfg->burst > 0)
		xt_xlate_add(xl, " burst %llu %sbytes", r / units[i].thresh,
			     units[i].name);
}

static void hashlimit_print_subnet_xlate(struct xt_xlate *xl,
					 uint32_t nsub, int family)
{
	char sep = (family == NFPROTO_IPV4) ? '.' : ':';
	char *fmt = (family == NFPROTO_IPV4) ? "%u" : "%04x";
	unsigned int nblocks = (family == NFPROTO_IPV4) ? 4 : 8;
	unsigned int nbits = (family == NFPROTO_IPV4) ? 8 : 16;
	unsigned int acm, i;

	xt_xlate_add(xl, " and ");
	while (nblocks--) {
		acm = 0;

		for (i = 0; i < nbits; i++) {
			acm <<= 1;

			if (nsub > 0) {
				acm++;
				nsub--;
			}
		}

		xt_xlate_add(xl, fmt, acm);
		if (nblocks > 0)
			xt_xlate_add(xl, "%c", sep);
	}
}

static const char *const hashlimit_modes4_xlate[] = {
	[XT_HASHLIMIT_HASH_DIP]	= "ip daddr",
	[XT_HASHLIMIT_HASH_DPT]	= "tcp dport",
	[XT_HASHLIMIT_HASH_SIP]	= "ip saddr",
	[XT_HASHLIMIT_HASH_SPT]	= "tcp sport",
};

static const char *const hashlimit_modes6_xlate[] = {
	[XT_HASHLIMIT_HASH_DIP]	= "ip6 daddr",
	[XT_HASHLIMIT_HASH_DPT]	= "tcp dport",
	[XT_HASHLIMIT_HASH_SIP]	= "ip6 saddr",
	[XT_HASHLIMIT_HASH_SPT]	= "tcp sport",
};

static int hashlimit_mode_xlate(struct xt_xlate *xl,
				uint32_t mode, int family,
				unsigned int nsrc, unsigned int ndst)
{
	const char * const *_modes = (family == NFPROTO_IPV4) ?
		hashlimit_modes4_xlate : hashlimit_modes6_xlate;
	bool prevopt = false;
	unsigned int mask;

	mode &= ~XT_HASHLIMIT_INVERT & ~XT_HASHLIMIT_BYTES;

	for (mask = 1; mode > 0; mask <<= 1) {
		if (!(mode & mask))
			continue;

		if (!prevopt) {
			xt_xlate_add(xl, " ");
			prevopt = true;
		}
		else {
			xt_xlate_add(xl, " . ");
		}

		xt_xlate_add(xl, "%s", _modes[mask]);

		if (mask == XT_HASHLIMIT_HASH_DIP &&
		    ((family == NFPROTO_IPV4 && ndst != 32) ||
		     (family == NFPROTO_IPV6 && ndst != 128)))
			hashlimit_print_subnet_xlate(xl, ndst, family);
		else if (mask == XT_HASHLIMIT_HASH_SIP &&
			 ((family == NFPROTO_IPV4 && nsrc != 32) ||
			  (family == NFPROTO_IPV6 && nsrc != 128)))
			hashlimit_print_subnet_xlate(xl, nsrc, family);

		mode &= ~mask;
	}

	return prevopt;
}

static int hashlimit_mt_xlate(struct xt_xlate *xl, const char *name,
			      const struct hashlimit_cfg3 *cfg,
			      int revision, int family)
{
	int ret = 1;

	xt_xlate_add(xl, "meter %s {", name);
	ret = hashlimit_mode_xlate(xl, cfg->mode, family,
				   cfg->srcmask, cfg->dstmask);
	if (cfg->expire != 1000)
		xt_xlate_add(xl, " timeout %us", cfg->expire / 1000);
	xt_xlate_add(xl, " limit rate");

	if (cfg->mode & XT_HASHLIMIT_INVERT)
		xt_xlate_add(xl, " over");

	if (cfg->mode & XT_HASHLIMIT_BYTES)
		print_bytes_rate_xlate(xl, cfg);
	else {
		print_packets_rate_xlate(xl, cfg->avg, revision);
		if (cfg->burst != XT_HASHLIMIT_BURST)
			xt_xlate_add(xl, "burst %lu packets", cfg->burst);

	}
	xt_xlate_add(xl, "}");

	return ret;
}

static int hashlimit_xlate(struct xt_xlate *xl,
			   const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_info *info = (const void *)params->match->data;
	int ret = 1;

	xt_xlate_add(xl, "meter %s {", info->name);
	ret = hashlimit_mode_xlate(xl, info->cfg.mode, NFPROTO_IPV4, 32, 32);
	xt_xlate_add(xl, " timeout %us limit rate", info->cfg.expire / 1000);
	print_packets_rate_xlate(xl, info->cfg.avg, 1);
	xt_xlate_add(xl, " burst %lu packets", info->cfg.burst);
	xt_xlate_add(xl, "}");

	return ret;
}

static int hashlimit_mt4_xlate_v1(struct xt_xlate *xl,
				  const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo1 *info =
		(const void *)params->match->data;
	struct hashlimit_cfg3 cfg;

	if (cfg_copy(&cfg, (const void *)&info->cfg, 1))
		xtables_error(OTHER_PROBLEM, "unknown revision");

	return hashlimit_mt_xlate(xl, info->name, &cfg, 1, NFPROTO_IPV4);
}

static int hashlimit_mt6_xlate_v1(struct xt_xlate *xl,
				  const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo1 *info =
		(const void *)params->match->data;
	struct hashlimit_cfg3 cfg;
1399

1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
	if (cfg_copy(&cfg, (const void *)&info->cfg, 1))
		xtables_error(OTHER_PROBLEM, "unknown revision");

	return hashlimit_mt_xlate(xl, info->name, &cfg, 1, NFPROTO_IPV6);
}

static int hashlimit_mt4_xlate_v2(struct xt_xlate *xl,
				  const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo2 *info =
		(const void *)params->match->data;
	struct hashlimit_cfg3 cfg;

	if (cfg_copy(&cfg, (const void *)&info->cfg, 2))
		xtables_error(OTHER_PROBLEM, "unknown revision");

	return hashlimit_mt_xlate(xl, info->name, &cfg, 2, NFPROTO_IPV4);
}

static int hashlimit_mt6_xlate_v2(struct xt_xlate *xl,
				  const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo2 *info =
		(const void *)params->match->data;
	struct hashlimit_cfg3 cfg;

	if (cfg_copy(&cfg, (const void *)&info->cfg, 2))
		xtables_error(OTHER_PROBLEM, "unknown revision");

	return hashlimit_mt_xlate(xl, info->name, &cfg, 2, NFPROTO_IPV6);
}

static int hashlimit_mt4_xlate(struct xt_xlate *xl,
			       const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo3 *info =
		(const void *)params->match->data;

	return hashlimit_mt_xlate(xl, info->name, &info->cfg, 3, NFPROTO_IPV4);
}

static int hashlimit_mt6_xlate(struct xt_xlate *xl,
			       const struct xt_xlate_mt_params *params)
{
	const struct xt_hashlimit_mtinfo3 *info =
		(const void *)params->match->data;

	return hashlimit_mt_xlate(xl, info->name, &info->cfg, 3, NFPROTO_IPV6);
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
}

static struct xtables_match hashlimit_mt_reg[] = {
	{
		.family        = NFPROTO_UNSPEC,
		.name          = "hashlimit",
		.version       = XTABLES_VERSION,
		.revision      = 0,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_info)),
		.userspacesize = offsetof(struct xt_hashlimit_info, hinfo),
		.help          = hashlimit_help,
		.init          = hashlimit_init,
		.x6_parse      = hashlimit_parse,
		.x6_fcheck     = hashlimit_check,
		.print         = hashlimit_print,
		.save          = hashlimit_save,
		.x6_options    = hashlimit_opts,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
1466
		.xlate         = hashlimit_xlate,
1467
1468
1469
1470
1471
1472
1473
1474
1475
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
		.revision      = 1,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo1)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
		.help          = hashlimit_mt_help,
1476
1477
1478
1479
1480
1481
1482
		.init          = hashlimit_mt4_init_v1,
		.x6_parse      = hashlimit_mt_parse_v1,
		.x6_fcheck     = hashlimit_mt_check_v1,
		.print         = hashlimit_mt4_print_v1,
		.save          = hashlimit_mt4_save_v1,
		.x6_options    = hashlimit_mt_opts_v1,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
1483
		.xlate         = hashlimit_mt4_xlate_v1,
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
		.revision      = 1,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo1)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
		.help          = hashlimit_mt_help,
		.init          = hashlimit_mt6_init_v1,
		.x6_parse      = hashlimit_mt_parse_v1,
		.x6_fcheck     = hashlimit_mt_check_v1,
		.print         = hashlimit_mt6_print_v1,
		.save          = hashlimit_mt6_save_v1,
		.x6_options    = hashlimit_mt_opts_v1,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
1500
		.xlate         = hashlimit_mt6_xlate_v1,
1501
1502
1503
1504
1505
1506
1507
1508
1509
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
		.revision      = 2,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo2)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo2, hinfo),
		.help          = hashlimit_mt_help,
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
		.init          = hashlimit_mt4_init_v2,
		.x6_parse      = hashlimit_mt_parse_v2,
		.x6_fcheck     = hashlimit_mt_check_v2,
		.print         = hashlimit_mt4_print_v2,
		.save          = hashlimit_mt4_save_v2,
		.x6_options    = hashlimit_mt_opts_v2,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
		.xlate         = hashlimit_mt4_xlate_v2,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
		.revision      = 2,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo2)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo2, hinfo),
		.help          = hashlimit_mt_help,
		.init          = hashlimit_mt6_init_v2,
		.x6_parse      = hashlimit_mt_parse_v2,
		.x6_fcheck     = hashlimit_mt_check_v2,
		.print         = hashlimit_mt6_print_v2,
		.save          = hashlimit_mt6_save_v2,
		.x6_options    = hashlimit_mt_opts_v2,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
		.xlate         = hashlimit_mt6_xlate_v2,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
		.revision      = 3,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo3)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo3, hinfo),
		.help          = hashlimit_mt_help_v3,
1544
1545
1546
1547
1548
1549
1550
		.init          = hashlimit_mt4_init,
		.x6_parse      = hashlimit_mt_parse,
		.x6_fcheck     = hashlimit_mt_check,
		.print         = hashlimit_mt4_print,
		.save          = hashlimit_mt4_save,
		.x6_options    = hashlimit_mt_opts,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
1551
		.xlate         = hashlimit_mt4_xlate,
1552
1553
1554
1555
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "hashlimit",
1556
		.revision      = 3,
1557
		.family        = NFPROTO_IPV6,
1558
1559
1560
		.size          = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo3)),
		.userspacesize = offsetof(struct xt_hashlimit_mtinfo3, hinfo),
		.help          = hashlimit_mt_help_v3,
1561
1562
1563
1564
1565
1566
1567
		.init          = hashlimit_mt6_init,
		.x6_parse      = hashlimit_mt_parse,
		.x6_fcheck     = hashlimit_mt_check,
		.print         = hashlimit_mt6_print,
		.save          = hashlimit_mt6_save,
		.x6_options    = hashlimit_mt_opts,
		.udata_size    = sizeof(struct hashlimit_mt_udata),
1568
		.xlate         = hashlimit_mt6_xlate,
1569
1570
1571
1572
1573
1574
1575
	},
};

void _init(void)
{
	xtables_register_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
}