summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_length.c
blob: 4f3a3318eb81ad035e6f3d0de604d11c9d28ff53 (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
#include <stdio.h>
#include <xtables.h>
#include <linux/netfilter/xt_length.h>

enum {
	O_LENGTH = 0,
};

static void length_help(void)
{
	printf(
"length match options:\n"
"[!] --length length[:length]    Match packet length against value or range\n"
"                                of values (inclusive)\n");
}
  
static const struct xt_option_entry length_opts[] = {
	{.name = "length", .id = O_LENGTH, .type = XTTYPE_UINT16RC,
	 .flags = XTOPT_MAND | XTOPT_INVERT},
	XTOPT_TABLEEND,
};

static void length_parse(struct xt_option_call *cb)
{
	struct xt_length_info *info = cb->data;

	xtables_option_parse(cb);
	info->min = cb->val.u16_range[0];
	info->max = (cb->nvals == 2) ? cb->val.u16_range[1] : UINT16_MAX;
	if (cb->invert)
		info->invert = 1;
}

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

	printf(" length %s", info->invert ? "!" : "");
	if (info->min == info->max)
		printf("%u", info->min);
	else
		printf("%u:%u", info->min, info->max);
}

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

	printf("%s --length ", info->invert ? " !" : "");
	if (info->min == info->max)
		printf("%u", info->min);
	else
		printf("%u:%u", info->min, info->max);
}

static struct xtables_match length_match = {
	.family		= NFPROTO_UNSPEC,
	.name		= "length",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_length_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_length_info)),
	.help		= length_help,
	.print		= length_print,
	.save		= length_save,
	.x6_parse	= length_parse,
	.x6_options	= length_opts,
};

void _init(void)
{
	xtables_register_match(&length_match);
}