summaryrefslogtreecommitdiffstats
path: root/extensions/libebt_among.c
blob: 715d559f432c222250ee3038c67529fc1ebf3c2e (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* ebt_among
 *
 * Authors:
 * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
 *
 * August, 2003
 */

#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <xtables.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/netfilter_bridge/ebt_among.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "iptables/nft.h"
#include "iptables/nft-bridge.h"

#define AMONG_DST '1'
#define AMONG_SRC '2'
#define AMONG_DST_F '3'
#define AMONG_SRC_F '4'

static const struct option bramong_opts[] = {
	{"among-dst", required_argument, 0, AMONG_DST},
	{"among-src", required_argument, 0, AMONG_SRC},
	{"among-dst-file", required_argument, 0, AMONG_DST_F},
	{"among-src-file", required_argument, 0, AMONG_SRC_F},
	{0}
};

static void bramong_print_help(void)
{
	printf(
"`among' options:\n"
"--among-dst      [!] list      : matches if ether dst is in list\n"
"--among-src      [!] list      : matches if ether src is in list\n"
"--among-dst-file [!] file      : obtain dst list from file\n"
"--among-src-file [!] file      : obtain src list from file\n"
"list has form:\n"
" xx:xx:xx:xx:xx:xx[=ip.ip.ip.ip],yy:yy:yy:yy:yy:yy[=ip.ip.ip.ip]"
",...,zz:zz:zz:zz:zz:zz[=ip.ip.ip.ip][,]\n"
"Things in brackets are optional.\n"
"If you want to allow two (or more) IP addresses to one MAC address, you\n"
"can specify two (or more) pairs with the same MAC, e.g.\n"
" 00:00:00:fa:eb:fe=153.19.120.250,00:00:00:fa:eb:fe=192.168.0.1\n"
	);
}

static void
parse_nft_among_pair(char *buf, struct nft_among_pair *pair, bool have_ip)
{
	char *sep = index(buf, '=');
	struct ether_addr *ether;

	if (have_ip ^ !!sep)
		xtables_error(PARAMETER_PROBLEM,
			      "among: Mixed MAC and MAC=IP not allowed.");

	if (sep) {
		*sep = '\0';

		if (!inet_aton(sep + 1, &pair->in))
			xtables_error(PARAMETER_PROBLEM,
				      "Invalid IP address '%s'\n", sep + 1);
	}
	ether = ether_aton(buf);
	if (!ether)
		xtables_error(PARAMETER_PROBLEM,
			      "Invalid MAC address '%s'\n", buf);
	memcpy(&pair->ether, ether, sizeof(*ether));
}

static void
parse_nft_among_pairs(struct nft_among_pair *pairs, char *buf,
		      size_t cnt, bool have_ip)
{
	size_t tmpcnt = 0;

	buf = strtok(buf, ",");
	while (buf) {
		struct nft_among_pair pair = {};

		parse_nft_among_pair(buf, &pair, have_ip);
		nft_among_insert_pair(pairs, &tmpcnt, &pair);
		buf = strtok(NULL, ",");
	}
}

static size_t count_nft_among_pairs(char *buf)
{
	size_t cnt = 0;
	char *p = buf;

	if (!*buf)
		return 0;

	do {
		cnt++;
		p = index(++p, ',');
	} while (p);

	return cnt;
}

static bool nft_among_pairs_have_ip(char *buf)
{
	return !!index(buf, '=');
}

static int bramong_parse(int c, char **argv, int invert,
		 unsigned int *flags, const void *entry,
		 struct xt_entry_match **match)
{
	struct nft_among_data *data = (struct nft_among_data *)(*match)->data;
	struct xt_entry_match *new_match;
	bool have_ip, dst = false;
	size_t new_size, cnt;
	struct stat stats;
	int fd = -1, poff;
	long flen = 0;

	switch (c) {
	case AMONG_DST_F:
		dst = true;
		/* fall through */
	case AMONG_SRC_F:
		if ((fd = open(optarg, O_RDONLY)) == -1)
			xtables_error(PARAMETER_PROBLEM,
				      "Couldn't open file '%s'", optarg);
		if (fstat(fd, &stats) < 0)
			xtables_error(PARAMETER_PROBLEM,
				      "fstat(%s) failed: '%s'",
				      optarg, strerror(errno));
		flen = stats.st_size;
		/* use mmap because the file will probably be big */
		optarg = mmap(0, flen, PROT_READ | PROT_WRITE,
			      MAP_PRIVATE, fd, 0);
		if (optarg == MAP_FAILED)
			xtables_error(PARAMETER_PROBLEM,
				      "Couldn't map file to memory");
		if (optarg[flen-1] != '\n')
			xtables_error(PARAMETER_PROBLEM,
				      "File should end with a newline");
		if (strchr(optarg, '\n') != optarg+flen-1)
			xtables_error(PARAMETER_PROBLEM,
				      "File should only contain one line");
		optarg[flen-1] = '\0';
		/* fall through */
	case AMONG_DST:
		if (c == AMONG_DST)
			dst = true;
		/* fall through */
	case AMONG_SRC:
		break;
	default:
		return 0;
	}

	cnt = count_nft_among_pairs(optarg);
	if (cnt == 0)
		return 0;

	new_size = data->src.cnt + data->dst.cnt + cnt;
	new_size *= sizeof(struct nft_among_pair);
	new_size += XT_ALIGN(sizeof(struct xt_entry_match)) +
			sizeof(struct nft_among_data);
	new_match = xtables_calloc(1, new_size);
	memcpy(new_match, *match, (*match)->u.match_size);
	new_match->u.match_size = new_size;

	data = (struct nft_among_data *)new_match->data;
	have_ip = nft_among_pairs_have_ip(optarg);
	poff = nft_among_prepare_data(data, dst, cnt, invert, have_ip);
	parse_nft_among_pairs(data->pairs + poff, optarg, cnt, have_ip);

	free(*match);
	*match = new_match;

	if (c == AMONG_DST_F || c == AMONG_SRC_F) {
		munmap(argv, flen);
		close(fd);
	}
	return 1;
}

static void __bramong_print(struct nft_among_pair *pairs,
			    int cnt, bool inv, bool have_ip)
{
	const char *isep = inv ? "! " : "";
	int i;

	for (i = 0; i < cnt; i++) {
		printf("%s", isep);
		isep = ",";

		printf("%s", ether_ntoa(&pairs[i].ether));
		if (have_ip)
			printf("=%s", inet_ntoa(pairs[i].in));
	}
	printf(" ");
}

static void bramong_print(const void *ip, const struct xt_entry_match *match,
			  int numeric)
{
	struct nft_among_data *data = (struct nft_among_data *)match->data;

	if (data->src.cnt) {
		printf("--among-src ");
		__bramong_print(data->pairs,
				data->src.cnt, data->src.inv, data->src.ip);
	}
	if (data->dst.cnt) {
		printf("--among-dst ");
		__bramong_print(data->pairs + data->src.cnt,
				data->dst.cnt, data->dst.inv, data->dst.ip);
	}
}

static struct xtables_match bramong_match = {
	.name		= "among",
	.revision	= 0,
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_BRIDGE,
	.size		= XT_ALIGN(sizeof(struct nft_among_data)),
	.userspacesize	= XT_ALIGN(sizeof(struct nft_among_data)),
	.help		= bramong_print_help,
	.parse		= bramong_parse,
	.print		= bramong_print,
	.extra_opts	= bramong_opts,
};

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