summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_rateest.c
blob: 5f13340f719c9438c6813f6d8419cf528a416b34 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <getopt.h>

#include <xtables.h>
#include <linux/netfilter/xt_rateest.h>

/* Ugly hack to pass info to final_check function. We should fix the API */
static struct xt_rateest_match_info *rateest_info;

static void rateest_help(void)
{
	printf(
"rateest match v%s options:\n"
" --rateest1 name		Rate estimator name\n"
" --rateest2 name		Rate estimator name\n"
" --rateest-delta		Compare difference(s) to given rate(s)\n"
" --rateest-bps1 [bps]		Compare bps\n"
" --rateest-pps1 [pps]		Compare pps\n"
" --rateest-bps2 [bps]		Compare bps\n"
" --rateest-pps2 [pps]		Compare pps\n"
" [!] --rateest-lt		Match if rate is less than given rate/estimator\n"
" [!] --rateest-gt		Match if rate is greater than given rate/estimator\n"
" [!] --rateest-eq		Match if rate is equal to given rate/estimator\n"
"\n",
	       IPTABLES_VERSION);
}

enum rateest_options {
	OPT_RATEEST1,
	OPT_RATEEST2,
	OPT_RATEEST_BPS1,
	OPT_RATEEST_PPS1,
	OPT_RATEEST_BPS2,
	OPT_RATEEST_PPS2,
	OPT_RATEEST_DELTA,
	OPT_RATEEST_LT,
	OPT_RATEEST_GT,
	OPT_RATEEST_EQ,
};

static const struct option rateest_opts[] = {
	{ "rateest1",		1, NULL, OPT_RATEEST1 },
	{ "rateest",		1, NULL, OPT_RATEEST1 }, /* alias for absolute mode */
	{ "rateest2",		1, NULL, OPT_RATEEST2 },
	{ "rateest-bps1",	0, NULL, OPT_RATEEST_BPS1 },
	{ "rateest-pps1",	0, NULL, OPT_RATEEST_PPS1 },
	{ "rateest-bps2",	0, NULL, OPT_RATEEST_BPS2 },
	{ "rateest-pps2",	0, NULL, OPT_RATEEST_PPS2 },
	{ "rateest-bps",	0, NULL, OPT_RATEEST_BPS2 }, /* alias for absolute mode */
	{ "rateest-pps",	0, NULL, OPT_RATEEST_PPS2 }, /* alias for absolute mode */
	{ "rateest-delta",	0, NULL, OPT_RATEEST_DELTA },
	{ "rateest-lt",		0, NULL, OPT_RATEEST_LT },
	{ "rateest-gt",		0, NULL, OPT_RATEEST_GT },
	{ "rateest-eq",		0, NULL, OPT_RATEEST_EQ },
	{ .name = NULL }
};

/* Copied from iproute. See http://physics.nist.gov/cuu/Units/binary.html */
static const struct rate_suffix {
	const char *name;
	double scale;
} suffixes[] = {
	{ "bit",	1. },
	{ "Kibit",	1024. },
	{ "kbit",	1000. },
	{ "mibit",	1024.*1024. },
	{ "mbit",	1000000. },
	{ "gibit",	1024.*1024.*1024. },
	{ "gbit",	1000000000. },
	{ "tibit",	1024.*1024.*1024.*1024. },
	{ "tbit",	1000000000000. },
	{ "Bps",	8. },
	{ "KiBps",	8.*1024. },
	{ "KBps",	8000. },
	{ "MiBps",	8.*1024*1024. },
	{ "MBps",	8000000. },
	{ "GiBps",	8.*1024.*1024.*1024. },
	{ "GBps",	8000000000. },
	{ "TiBps",	8.*1024.*1024.*1024.*1024. },
	{ "TBps",	8000000000000. },
	{ .name = NULL }
};

static int
rateest_get_rate(u_int32_t *rate, const char *str)
{
	char *p;
	double bps = strtod(str, &p);
	const struct rate_suffix *s;

	if (p == str)
		return -1;

	if (*p == '\0') {
		*rate = bps / 8.;	/* assume bytes/sec */
		return 0;
	}

	for (s = suffixes; s->name; ++s) {
		if (strcasecmp(s->name, p) == 0) {
			*rate = (bps * s->scale) / 8.;
			return 0;
		}
	}

	return -1;
}

static int
rateest_parse(int c, char **argv, int invert, unsigned int *flags,
	      const void *entry, struct xt_entry_match **match)
{
	struct xt_rateest_match_info *info = (void *)(*match)->data;

	rateest_info = info;

	switch (c) {
	case OPT_RATEEST1:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest1 twice");
		*flags |= 1 << c;

		strncpy(info->name1, optarg, sizeof(info->name1) - 1);
		break;

	case OPT_RATEEST2:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest2 twice");
		*flags |= 1 << c;

		strncpy(info->name2, optarg, sizeof(info->name2) - 1);
		info->flags |= XT_RATEEST_MATCH_REL;
		break;

	case OPT_RATEEST_BPS1:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest-bps can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest-bps1 twice");
		*flags |= 1 << c;

		info->flags |= XT_RATEEST_MATCH_BPS;

		/* The rate is optional and only required in absolute mode */
		if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
			break;

		if (rateest_get_rate(&info->bps1, argv[optind]) < 0)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: could not parse rate `%s'",
				   argv[optind]);
		optind++;
		break;

	case OPT_RATEEST_PPS1:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest-pps can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest-pps1 twice");
		*flags |= 1 << c;

		info->flags |= XT_RATEEST_MATCH_PPS;

		/* The rate is optional and only required in absolute mode */
		if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
			break;

		if (string_to_number(argv[optind], 0, 0, &info->pps1) < 0)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: could not parse pps `%s'",
				   argv[optind]);
		optind++;
		break;

	case OPT_RATEEST_BPS2:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest-bps can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest-bps2 twice");
		*flags |= 1 << c;

		info->flags |= XT_RATEEST_MATCH_BPS;

		/* The rate is optional and only required in absolute mode */
		if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
			break;

		if (rateest_get_rate(&info->bps2, argv[optind]) < 0)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: could not parse rate `%s'",
				   argv[optind]);
		optind++;
		break;

	case OPT_RATEEST_PPS2:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest-pps can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest-pps2 twice");
		*flags |= 1 << c;

		info->flags |= XT_RATEEST_MATCH_PPS;

		/* The rate is optional and only required in absolute mode */
		if (!argv[optind] || *argv[optind] == '-' || *argv[optind] == '!')
			break;

		if (string_to_number(argv[optind], 0, 0, &info->pps2) < 0)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: could not parse pps `%s'",
				   argv[optind]);
		optind++;
		break;

	case OPT_RATEEST_DELTA:
		check_inverse(optarg, &invert, &optind, 0);
		if (invert)
			exit_error(PARAMETER_PROBLEM,
				   "rateest: rateest-delta can't be inverted");

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify --rateest-delta twice");
		*flags |= 1 << c;

		info->flags |= XT_RATEEST_MATCH_DELTA;
		break;

	case OPT_RATEEST_EQ:
		check_inverse(argv[optind-1], &invert, &optind, 0);

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify lt/gt/eq twice");
		*flags |= 1 << c;

		info->mode = XT_RATEEST_MATCH_EQ;
		if (invert)
			info->flags |= XT_RATEEST_MATCH_INVERT;
		break;

	case OPT_RATEEST_LT:
		check_inverse(argv[optind-1], &invert, &optind, 0);

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify lt/gt/eq twice");
		*flags |= 1 << c;

		info->mode = XT_RATEEST_MATCH_LT;
		if (invert)
			info->flags |= XT_RATEEST_MATCH_INVERT;
		break;

	case OPT_RATEEST_GT:
		check_inverse(argv[optind-1], &invert, &optind, 0);

		if (*flags & (1 << c))
			exit_error(PARAMETER_PROBLEM,
				   "rateest: can't specify lt/gt/eq twice");
		*flags |= 1 << c;

		info->mode = XT_RATEEST_MATCH_GT;
		if (invert)
			info->flags |= XT_RATEEST_MATCH_INVERT;
		break;

	default:
		return 0;
	}

	return 1;
}

static void
rateest_final_check(unsigned int flags)
{
	struct xt_rateest_match_info *info = rateest_info;

	if (!(info->flags & XT_RATEEST_MATCH_REL))
		info->flags |= XT_RATEEST_MATCH_ABS;
}

static void
rateest_print_rate(u_int32_t rate, int numeric)
{
	double tmp = (double)rate*8;

	if (numeric)
		printf("%u ", rate);
	else if (tmp >= 1000.0*1000000.0)
		printf("%.0fMbit ", tmp/1000000.0);
	else if (tmp >= 1000.0 * 1000.0)
		printf("%.0fKbit ", tmp/1000.0);
	else
		printf("%.0fbit ", tmp);
}

static void
rateest_print_mode(struct xt_rateest_match_info *info, const char *prefix)
{
	if (info->flags & XT_RATEEST_MATCH_INVERT)
		printf("! ");

	switch (info->mode) {
	case XT_RATEEST_MATCH_EQ:
		printf("%seq ", prefix);
		break;
	case XT_RATEEST_MATCH_LT:
		printf("%slt ", prefix);
		break;
	case XT_RATEEST_MATCH_GT:
		printf("%sgt ", prefix);
		break;
	default:
		exit(1);
	}
}

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

	printf("rateest match ");

	printf("%s ", info->name1);
	if (info->flags & XT_RATEEST_MATCH_DELTA)
		printf("delta ");

	if (info->flags & XT_RATEEST_MATCH_BPS) {
		printf("bps ");
		if (info->flags & XT_RATEEST_MATCH_DELTA)
			rateest_print_rate(info->bps1, numeric);
		if (info->flags & XT_RATEEST_MATCH_ABS) {
			rateest_print_mode(info, "");
			rateest_print_rate(info->bps2, numeric);
		}
	}
	if (info->flags & XT_RATEEST_MATCH_PPS) {
		printf("pps ");
		if (info->flags & XT_RATEEST_MATCH_DELTA)
			printf("%u ", info->pps1);
		if (info->flags & XT_RATEEST_MATCH_ABS) {
			rateest_print_mode(info, "");
			printf("%u ", info->pps2);
		}
	}

	if (info->flags & XT_RATEEST_MATCH_REL) {
		rateest_print_mode(info, "");

		printf("%s ", info->name2);
		if (info->flags & XT_RATEEST_MATCH_DELTA)
			printf("delta ");

		if (info->flags & XT_RATEEST_MATCH_BPS) {
			printf("bps ");
			if (info->flags & XT_RATEEST_MATCH_DELTA)
				rateest_print_rate(info->bps2, numeric);
		}
		if (info->flags & XT_RATEEST_MATCH_PPS) {
			printf("pps ");
			if (info->flags & XT_RATEEST_MATCH_DELTA)
				printf("%u ", info->pps2);
		}
	}
}

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

	if (info->flags & XT_RATEEST_MATCH_REL) {
		printf("--rateest1 %s ", info->name1);
		if (info->flags & XT_RATEEST_MATCH_BPS)
			printf("--rateest-bps ");
		if (info->flags & XT_RATEEST_MATCH_PPS)
			printf("--rateest-pps ");
		rateest_print_mode(info, "--rateest-");
		printf("--rateest2 %s ", info->name2);
	} else {
		printf("--rateest %s ", info->name1);
		if (info->flags & XT_RATEEST_MATCH_BPS) {
			printf("--rateest-bps ");
			rateest_print_mode(info, "--rateest-");
			rateest_print_rate(info->bps2, 0);
		}
		if (info->flags & XT_RATEEST_MATCH_PPS) {
			printf("--rateest-pps ");
			rateest_print_mode(info, "--rateest-");
			printf("%u ", info->pps2);
		}
	}
}

static struct xtables_match rateest_match4 = {
	.family		= AF_INET,
	.name		= "rateest",
	.version	= IPTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_rateest_match_info)),
	.userspacesize	= XT_ALIGN(offsetof(struct xt_rateest_match_info, est1)),
	.help		= rateest_help,
	.parse		= rateest_parse,
	.final_check	= rateest_final_check,
	.print		= rateest_print,
	.save		= rateest_save,
	.extra_opts	= rateest_opts,
};

static struct xtables_match rateest_match6 = {
	.family		= AF_INET6,
	.name		= "rateest",
	.version	= IPTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_rateest_match_info)),
	.userspacesize	= XT_ALIGN(offsetof(struct xt_rateest_match_info, est1)),
	.help		= rateest_help,
	.parse		= rateest_parse,
	.final_check	= rateest_final_check,
	.print		= rateest_print,
	.save		= rateest_save,
	.extra_opts	= rateest_opts,
};

void _init(void)
{
	xtables_register_match(&rateest_match4);
	xtables_register_match(&rateest_match6);
}