summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_length.c
blob: 00acae0e6a11df165cc6e8300414bdbacb8c93d1 (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
#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->val.u16_range[0];
	if (cb->nvals >= 2)
		info->max = cb->val.u16_range[1];
	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 int length_xlate(const struct xt_entry_match *match,
			struct xt_xlate *xl, int numeric)
{
	const struct xt_length_info *info = (void *)match->data;

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

	return 1;
}


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,
	.xlate		= length_xlate,
};

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