libxt_u32.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* Shared library add-on to iptables to add u32 matching,
2 * generalized matching on values found at packet offsets
3 *
4 * Detailed doc is in the kernel module source
5 * net/netfilter/xt_u32.c
6 *
7 * (C) 2002 by Don Cohen <don-netf@isis.cs3-inc.com>
8 * Released under the terms of GNU GPL v2
9 *
10 * Copyright © CC Computer Consultants GmbH, 2007
11 * Contact: <jengelh@computergmbh.de>
12 */
13#include <sys/types.h>
14#include <ctype.h>
15#include <errno.h>
16#include <getopt.h>
17#include <netdb.h>
18#include <stdbool.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22
23#include <xtables.h>
24#include <linux/netfilter/xt_u32.h>
25
26static const struct option u32_opts[] = {
27	{.name = "u32", .has_arg = true, .val = 'u'},
28	XT_GETOPT_TABLEEND,
29};
30
31static void u32_help(void)
32{
33	printf(
34		"u32 match options:\n"
35		"[!] --u32 tests\n"
36		"\t\t""tests := location \"=\" value | tests \"&&\" location \"=\" value\n"
37		"\t\t""value := range | value \",\" range\n"
38		"\t\t""range := number | number \":\" number\n"
39		"\t\t""location := number | location operator number\n"
40		"\t\t""operator := \"&\" | \"<<\" | \">>\" | \"@\"\n");
41}
42
43static void u32_dump(const struct xt_u32 *data)
44{
45	const struct xt_u32_test *ct;
46	unsigned int testind, i;
47
48	for (testind = 0; testind < data->ntests; ++testind) {
49		ct = &data->tests[testind];
50
51		if (testind > 0)
52			printf("&&");
53
54		printf("0x%x", ct->location[0].number);
55		for (i = 1; i < ct->nnums; ++i) {
56			switch (ct->location[i].nextop) {
57			case XT_U32_AND:
58				printf("&");
59				break;
60			case XT_U32_LEFTSH:
61				printf("<<");
62				break;
63			case XT_U32_RIGHTSH:
64				printf(">>");
65				break;
66			case XT_U32_AT:
67				printf("@");
68				break;
69			}
70			printf("0x%x", ct->location[i].number);
71		}
72
73		printf("=");
74		for (i = 0; i < ct->nvalues; ++i) {
75			if (i > 0)
76				printf(",");
77			if (ct->value[i].min == ct->value[i].max)
78				printf("0x%x", ct->value[i].min);
79			else
80				printf("0x%x:0x%x", ct->value[i].min,
81				       ct->value[i].max);
82		}
83	}
84	printf(" ");
85}
86
87/* string_to_number() is not quite what we need here ... */
88static u_int32_t parse_number(char **s, int pos)
89{
90	u_int32_t number;
91	char *end;
92
93	errno  = 0;
94	number = strtoul(*s, &end, 0);
95	if (end == *s)
96		xtables_error(PARAMETER_PROBLEM,
97			   "u32: at char %d: expected number", pos);
98	if (errno != 0)
99		xtables_error(PARAMETER_PROBLEM,
100			   "u32: at char %d: error reading number", pos);
101	*s = end;
102	return number;
103}
104
105static int u32_parse(int c, char **argv, int invert, unsigned int *flags,
106		     const void *entry, struct xt_entry_match **match)
107{
108	struct xt_u32 *data = (void *)(*match)->data;
109	unsigned int testind = 0, locind = 0, valind = 0;
110	struct xt_u32_test *ct = &data->tests[testind]; /* current test */
111	char *arg = optarg; /* the argument string */
112	char *start = arg;
113	int state = 0;
114
115	if (c != 'u')
116		return 0;
117
118	data->invert = invert;
119
120	/*
121	 * states:
122	 * 0 = looking for numbers and operations,
123	 * 1 = looking for ranges
124	 */
125	while (1) {
126		/* read next operand/number or range */
127		while (isspace(*arg))
128			++arg;
129
130		if (*arg == '\0') {
131			/* end of argument found */
132			if (state == 0)
133				xtables_error(PARAMETER_PROBLEM,
134					   "u32: abrupt end of input after location specifier");
135			if (valind == 0)
136				xtables_error(PARAMETER_PROBLEM,
137					   "u32: test ended with no value specified");
138
139			ct->nnums    = locind;
140			ct->nvalues  = valind;
141			data->ntests = ++testind;
142
143			if (testind > XT_U32_MAXSIZE)
144				xtables_error(PARAMETER_PROBLEM,
145				           "u32: at char %u: too many \"&&\"s",
146				           (unsigned int)(arg - start));
147			return 1;
148		}
149
150		if (state == 0) {
151			/*
152			 * reading location: read a number if nothing read yet,
153			 * otherwise either op number or = to end location spec
154			 */
155			if (*arg == '=') {
156				if (locind == 0) {
157					xtables_error(PARAMETER_PROBLEM,
158					           "u32: at char %u: "
159					           "location spec missing",
160					           (unsigned int)(arg - start));
161				} else {
162					++arg;
163					state = 1;
164				}
165			} else {
166				if (locind != 0) {
167					/* need op before number */
168					if (*arg == '&') {
169						ct->location[locind].nextop = XT_U32_AND;
170					} else if (*arg == '<') {
171						if (*++arg != '<')
172							xtables_error(PARAMETER_PROBLEM,
173								   "u32: at char %u: a second '<' was expected", (unsigned int)(arg - start));
174						ct->location[locind].nextop = XT_U32_LEFTSH;
175					} else if (*arg == '>') {
176						if (*++arg != '>')
177							xtables_error(PARAMETER_PROBLEM,
178								   "u32: at char %u: a second '>' was expected", (unsigned int)(arg - start));
179						ct->location[locind].nextop = XT_U32_RIGHTSH;
180					} else if (*arg == '@') {
181						ct->location[locind].nextop = XT_U32_AT;
182					} else {
183						xtables_error(PARAMETER_PROBLEM,
184							"u32: at char %u: operator expected", (unsigned int)(arg - start));
185					}
186					++arg;
187				}
188				/* now a number; string_to_number skips white space? */
189				ct->location[locind].number =
190					parse_number(&arg, arg - start);
191				if (++locind > XT_U32_MAXSIZE)
192					xtables_error(PARAMETER_PROBLEM,
193						   "u32: at char %u: too many operators", (unsigned int)(arg - start));
194			}
195		} else {
196			/*
197			 * state 1 - reading values: read a range if nothing
198			 * read yet, otherwise either ,range or && to end
199			 * test spec
200			 */
201			if (*arg == '&') {
202				if (*++arg != '&')
203					xtables_error(PARAMETER_PROBLEM,
204						   "u32: at char %u: a second '&' was expected", (unsigned int)(arg - start));
205				if (valind == 0) {
206					xtables_error(PARAMETER_PROBLEM,
207						   "u32: at char %u: value spec missing", (unsigned int)(arg - start));
208				} else {
209					ct->nnums   = locind;
210					ct->nvalues = valind;
211					ct = &data->tests[++testind];
212					if (testind > XT_U32_MAXSIZE)
213						xtables_error(PARAMETER_PROBLEM,
214							   "u32: at char %u: too many \"&&\"s", (unsigned int)(arg - start));
215					++arg;
216					state  = 0;
217					locind = 0;
218					valind = 0;
219				}
220			} else { /* read value range */
221				if (valind > 0) { /* need , before number */
222					if (*arg != ',')
223						xtables_error(PARAMETER_PROBLEM,
224							   "u32: at char %u: expected \",\" or \"&&\"", (unsigned int)(arg - start));
225					++arg;
226				}
227				ct->value[valind].min =
228					parse_number(&arg, arg - start);
229
230				while (isspace(*arg))
231					++arg;
232
233				if (*arg == ':') {
234					++arg;
235					ct->value[valind].max =
236						parse_number(&arg, arg-start);
237				} else {
238					ct->value[valind].max =
239						ct->value[valind].min;
240				}
241
242				if (++valind > XT_U32_MAXSIZE)
243					xtables_error(PARAMETER_PROBLEM,
244						   "u32: at char %u: too many \",\"s", (unsigned int)(arg - start));
245			}
246		}
247	}
248}
249
250static void u32_print(const void *ip, const struct xt_entry_match *match,
251                      int numeric)
252{
253	const struct xt_u32 *data = (const void *)match->data;
254	printf("u32 ");
255	if (data->invert)
256		printf("! ");
257	u32_dump(data);
258}
259
260static void u32_save(const void *ip, const struct xt_entry_match *match)
261{
262	const struct xt_u32 *data = (const void *)match->data;
263	if (data->invert)
264		printf("! ");
265	printf("--u32 ");
266	u32_dump(data);
267}
268
269static struct xtables_match u32_match = {
270	.name          = "u32",
271	.family        = NFPROTO_UNSPEC,
272	.version       = XTABLES_VERSION,
273	.size          = XT_ALIGN(sizeof(struct xt_u32)),
274	.userspacesize = XT_ALIGN(sizeof(struct xt_u32)),
275	.help          = u32_help,
276	.parse         = u32_parse,
277	.print         = u32_print,
278	.save          = u32_save,
279	.extra_opts    = u32_opts,
280};
281
282void _init(void)
283{
284	xtables_register_match(&u32_match);
285}
286