libipt_realm.c 2.84 KB
Newer Older
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#if defined(__GLIBC__) && __GLIBC__ == 2
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
#include <xtables.h>
#include <linux/netfilter_ipv4/ipt_realm.h>

enum {
	O_REALM = 0,
};

static void realm_help(void)
{
	printf(
"realm match options:\n"
"[!] --realm value[/mask]\n"
"				Match realm\n");
}

static const struct xt_option_entry realm_opts[] = {
	{.name = "realm", .id = O_REALM, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_INVERT},
	XTOPT_TABLEEND,
};

31
32
static const char f_realms[] = "/etc/iproute2/rt_realms";
/* array of realms from f_realms[] */
33
34
35
36
static struct xtables_lmap *realms;

static void realm_parse(struct xt_option_call *cb)
{
37
38
	struct xt_realm_info *ri = cb->data;
	unsigned int id, mask;
39
40

	xtables_option_parse(cb);
41
	xtables_parse_val_mask(cb, &id, &mask, realms);
42

43
44
	ri->id = id;
	ri->mask = mask;
45

46
47
	if (cb->invert)
		ri->invert = 1;
48
49
50
}

static void realm_print(const void *ip, const struct xt_entry_match *match,
51
			int numeric)
52
53
54
55
56
57
58
{
	const struct xt_realm_info *ri = (const void *)match->data;

	if (ri->invert)
		printf(" !");

	printf(" realm");
59
	xtables_print_val_mask(ri->id, ri->mask, numeric ? NULL : realms);
60
61
62
63
64
65
66
67
68
69
}

static void realm_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_realm_info *ri = (const void *)match->data;

	if (ri->invert)
		printf(" !");

	printf(" --realm");
70
	xtables_print_val_mask(ri->id, ri->mask, realms);
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
static void
print_realm_xlate(unsigned long id, unsigned long mask,
		  int numeric, struct xt_xlate *xl, uint32_t op)
{
	const char *name = NULL;

	if (mask != 0xffffffff)
		xt_xlate_add(xl, " and 0x%lx %s 0x%lx", mask,
			   op == XT_OP_EQ ? "==" : "!=", id);
	else {
		if (numeric == 0)
			name = xtables_lmap_id2name(realms, id);
		if (name)
			xt_xlate_add(xl, " %s%s",
				   op == XT_OP_EQ ? "" : "!= ", name);
		else
			xt_xlate_add(xl, " %s0x%lx",
				   op == XT_OP_EQ ? "" : "!= ", id);
	}
}

static int realm_xlate(struct xt_xlate *xl,
		       const struct xt_xlate_mt_params *params)
{
	const struct xt_realm_info *ri = (const void *)params->match->data;
	enum xt_op op = XT_OP_EQ;

	if (ri->invert)
		op = XT_OP_NEQ;

	xt_xlate_add(xl, "rtclassid");
	print_realm_xlate(ri->id, ri->mask, 0, xl, op);

	return 1;
}

109
110
111
112
113
114
115
116
117
118
119
static struct xtables_match realm_mt_reg = {
	.name		= "realm",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_IPV4,
	.size		= XT_ALIGN(sizeof(struct xt_realm_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_realm_info)),
	.help		= realm_help,
	.print		= realm_print,
	.save		= realm_save,
	.x6_parse	= realm_parse,
	.x6_options	= realm_opts,
120
	.xlate		= realm_xlate,
121
122
123
124
};

void _init(void)
{
125
126
127
128
129
	realms = xtables_lmap_init(f_realms);
	if (realms == NULL && errno != ENOENT)
		fprintf(stderr, "Warning: %s: %s\n", f_realms,
			strerror(errno));

130
131
	xtables_register_match(&realm_mt_reg);
}