summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_connlabel.c
blob: ae52901b26cfd00102e54329179014f551981877 (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
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <xtables.h>
#include <linux/netfilter/xt_connlabel.h>

enum {
	O_LABEL = 0,
	O_SET = 1,
};

#define CONNLABEL_CFG "/etc/xtables/connlabel.conf"

static void connlabel_mt_help(void)
{
	puts(
"connlabel match options:\n"
"[!] --label name     Match if label has been set on connection\n"
"    --set            Set label on connection");
}

static const struct xt_option_entry connlabel_mt_opts[] = {
	{.name = "label", .id = O_LABEL, .type = XTTYPE_STRING,
	 .min = 1, .flags = XTOPT_MAND|XTOPT_INVERT},
	{.name = "set", .id = O_SET, .type = XTTYPE_NONE},
	XTOPT_TABLEEND,
};

static int
xtables_parse_connlabel_numerical(const char *s, char **end)
{
	uintmax_t value;

	if (!xtables_strtoul(s, end, &value, 0, XT_CONNLABEL_MAXBIT))
		return -1;
	return value;
}

static bool is_space_posix(int c)
{
	return c == ' ' || c == '\f' || c == '\r' || c == '\t' || c == '\v';
}

static char * trim_label(char *label)
{
	char *end;

	while (is_space_posix(*label))
		label++;
	end = strchr(label, '\n');
	if (end)
		*end = 0;
	else
		end = strchr(label, '\0');
	end--;

	while (is_space_posix(*end) && end > label) {
		*end = 0;
		end--;
	}

	return *label ? label : NULL;
}

static void
xtables_get_connlabel(uint16_t bit, char *buf, size_t len)
{
	FILE *fp = fopen(CONNLABEL_CFG, "r");
	char label[1024];
	char *end;

	if (!fp)
		goto error;

	while (fgets(label, sizeof(label), fp)) {
		int tmp;

		if (label[0] == '#')
			continue;
		tmp = xtables_parse_connlabel_numerical(label, &end);
		if (tmp < 0 || tmp < (int) bit)
			continue;
		if (tmp > (int) bit)
			break;

		end = trim_label(end);
		if (!end)
			continue;
		snprintf(buf, len, "%s", end);
		fclose(fp);
		return;
	}
	fclose(fp);
 error:
	snprintf(buf, len, "%u", (unsigned int) bit);
}


static uint16_t xtables_parse_connlabel(const char *s)
{
	FILE *fp = fopen(CONNLABEL_CFG, "r");
	char label[1024];
	char *end;
	int bit;

	if (!fp)
		xtables_error(PARAMETER_PROBLEM, "label '%s': could not open '%s': %s",
						s, CONNLABEL_CFG, strerror(errno));

	while (fgets(label, sizeof(label), fp)) {
		if (label[0] == '#' || !strstr(label, s))
			continue;
		bit = xtables_parse_connlabel_numerical(label, &end);
		if (bit < 0)
			continue;

		end = trim_label(end);
		if (!end)
			continue;
		if (strcmp(end, s) == 0) {
			fclose(fp);
			return bit;
		}
	}
	fclose(fp);
	xtables_error(PARAMETER_PROBLEM, "label '%s' not found in config file %s",
					s, CONNLABEL_CFG);
}

static void connlabel_mt_parse(struct xt_option_call *cb)
{
	struct xt_connlabel_mtinfo *info = cb->data;
	int tmp;

	xtables_option_parse(cb);

	switch (cb->entry->id) {
	case O_LABEL:
		tmp = xtables_parse_connlabel_numerical(cb->arg, NULL);
		info->bit = tmp < 0 ? xtables_parse_connlabel(cb->arg) : tmp;

		if (cb->invert)
			info->options |= XT_CONNLABEL_OP_INVERT;
		break;
	case O_SET:
		info->options |= XT_CONNLABEL_OP_SET;
		break;
	}

}

static void
connlabel_mt_print_op(const struct xt_connlabel_mtinfo *info, const char *prefix)
{
	if (info->options & XT_CONNLABEL_OP_SET)
		printf(" %sset", prefix);
}

static void
connlabel_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
{
	const struct xt_connlabel_mtinfo *info = (const void *)match->data;
	char buf[1024];

	printf(" connlabel");
	if (info->options & XT_CONNLABEL_OP_INVERT)
		printf(" !");
	if (numeric) {
		printf(" %u", info->bit);
	} else {
		xtables_get_connlabel(info->bit, buf, sizeof(buf));
		printf(" '%s'", buf);
	}
	connlabel_mt_print_op(info, "");
}

static void
connlabel_mt_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_connlabel_mtinfo *info = (const void *)match->data;
	char buf[1024];

	if (info->options & XT_CONNLABEL_OP_INVERT)
		printf(" !");

	xtables_get_connlabel(info->bit, buf, sizeof(buf));
	printf(" --label \"%s\"", buf);

	connlabel_mt_print_op(info, "--");
}

static struct xtables_match connlabel_mt_reg = {
	.family        = NFPROTO_UNSPEC,
	.name          = "connlabel",
	.version       = XTABLES_VERSION,
	.size          = XT_ALIGN(sizeof(struct xt_connlabel_mtinfo)),
	.userspacesize = offsetof(struct xt_connlabel_mtinfo, bit),
	.help          = connlabel_mt_help,
	.print         = connlabel_mt_print,
	.save          = connlabel_mt_save,
	.x6_parse      = connlabel_mt_parse,
	.x6_options    = connlabel_mt_opts,
};

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