summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_realm.c
blob: d0160408769d8a1ad9fc73b3e4386919ead30fb5 (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
#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,
};

/* array of realms from /etc/iproute2/rt_realms */
static struct xtables_lmap *realms;

static void realm_init(struct xt_entry_match *m)
{
	const char file[] = "/etc/iproute2/rt_realms";

	realms = xtables_lmap_init(file);
	if (realms == NULL && errno != ENOENT)
		fprintf(stderr, "Warning: %s: %s\n", file, strerror(errno));
}

static void realm_parse(struct xt_option_call *cb)
{
	struct xt_realm_info *realminfo = cb->data;
	int id;
	char *end;

	xtables_option_parse(cb);
	realminfo->id = strtoul(cb->arg, &end, 0);
	if (end != cb->arg && (*end == '/' || *end == '\0')) {
		if (*end == '/')
			realminfo->mask = strtoul(end+1, &end, 0);
		else
			realminfo->mask = 0xffffffff;
		if (*end != '\0' || end == cb->arg)
			xtables_error(PARAMETER_PROBLEM,
				   "Bad realm value \"%s\"", cb->arg);
	} else {
		id = xtables_lmap_name2id(realms, cb->arg);
		if (id == -1)
			xtables_error(PARAMETER_PROBLEM,
				   "Realm \"%s\" not found", cb->arg);
		realminfo->id = id;
		realminfo->mask = 0xffffffff;
	}
	if (cb->invert)
		realminfo->invert = 1;
}

static void
print_realm(unsigned long id, unsigned long mask, int numeric)
{
	const char *name = NULL;

	if (mask != 0xffffffff)
		printf(" 0x%lx/0x%lx", id, mask);
	else {
		if (numeric == 0)
			name = xtables_lmap_id2name(realms, id);
		if (name)
			printf(" %s", name);
		else
			printf(" 0x%lx", id);
	}
}

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

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

	printf(" realm");
	print_realm(ri->id, ri->mask, numeric);
}

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");
	print_realm(ri->id, ri->mask, 0);
}

static void
print_realm_xlate(unsigned long id, unsigned long mask,
		  int numeric, struct xt_buf *buf, uint32_t op)
{
	const char *name = NULL;

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

static int realm_xlate(const struct xt_entry_match *match,
		       struct xt_buf *buf, int numeric)
{
	const struct xt_realm_info *ri = (const void *)match->data;
	enum xt_op op = XT_OP_EQ;

	if (ri->invert)
		op = XT_OP_NEQ;

	xt_buf_add(buf, "rtclassid");
	print_realm_xlate(ri->id, ri->mask, 0, buf, op);

	return 1;
}

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,
	.init		= realm_init,
	.print		= realm_print,
	.save		= realm_save,
	.x6_parse	= realm_parse,
	.x6_options	= realm_opts,
	.xlate		= realm_xlate,
};

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