1 /* Shared library add-on to iptables to add u32 matching,
2 * generalized matching on values found at packet offsets
4 * Detailed doc is in the kernel module source
5 * net/netfilter/xt_u32.c
7 * (C) 2002 by Don Cohen <don-netf@isis.cs3-inc.com>
8 * Released under the terms of GNU GPL v2
10 * Copyright © CC Computer Consultants GmbH, 2007
11 * Contact: <jengelh@computergmbh.de>
13 #include <sys/types.h>
23 #include "../include/linux/netfilter/xt_u32.h"
25 static const struct option u32_opts[] = {
26 {"u32", 1, NULL, 'u'},
30 static void u32_help(void)
33 "u32 match options:\n"
35 "\t\t""tests := location \"=\" value | tests \"&&\" location \"=\" value\n"
36 "\t\t""value := range | value \",\" range\n"
37 "\t\t""range := number | number \":\" number\n"
38 "\t\t""location := number | location operator number\n"
39 "\t\t""operator := \"&\" | \"<<\" | \">>\" | \"@\"\n");
42 static void u32_dump(const struct xt_u32 *data)
44 const struct xt_u32_test *ct;
45 unsigned int testind, i;
47 for (testind = 0; testind < data->ntests; ++testind) {
48 ct = &data->tests[testind];
53 printf("0x%x", ct->location[0].number);
54 for (i = 1; i < ct->nnums; ++i) {
55 switch (ct->location[i].nextop) {
69 printf("0x%x", ct->location[i].number);
73 for (i = 0; i < ct->nvalues; ++i) {
76 if (ct->value[i].min == ct->value[i].max)
77 printf("0x%x", ct->value[i].min);
79 printf("0x%x:0x%x", ct->value[i].min,
86 /* string_to_number() is not quite what we need here ... */
87 static u_int32_t parse_number(char **s, int pos)
93 number = strtoul(*s, &end, 0);
95 exit_error(PARAMETER_PROBLEM,
96 "u32: at char %d: expected number", pos);
98 exit_error(PARAMETER_PROBLEM,
99 "u32: at char %d: error reading number", pos);
104 /* Function which parses command options; returns true if it ate an option */
105 static int u32_parse(int c, char **argv, int invert, unsigned int *flags,
106 const void *entry, struct xt_entry_match **match)
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 = argv[optind-1]; /* the argument string */
118 data->invert = invert;
122 * 0 = looking for numbers and operations,
123 * 1 = looking for ranges
126 /* read next operand/number or range */
127 while (isspace(*arg))
131 /* end of argument found */
133 exit_error(PARAMETER_PROBLEM,
134 "u32: abrupt end of input after location specifier");
136 exit_error(PARAMETER_PROBLEM,
137 "u32: test ended with no value specified");
140 ct->nvalues = valind;
141 data->ntests = ++testind;
143 if (testind > XT_U32_MAXSIZE)
144 exit_error(PARAMETER_PROBLEM,
145 "u32: at char %u: too many \"&&\"s",
146 (unsigned int)(arg - start));
152 * reading location: read a number if nothing read yet,
153 * otherwise either op number or = to end location spec
157 exit_error(PARAMETER_PROBLEM,
159 "location spec missing",
160 (unsigned int)(arg - start));
167 /* need op before number */
169 ct->location[locind].nextop = XT_U32_AND;
170 } else if (*arg == '<') {
172 exit_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 == '>') {
177 exit_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;
183 exit_error(PARAMETER_PROBLEM,
184 "u32: at char %u: operator expected", (unsigned int)(arg - start));
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 exit_error(PARAMETER_PROBLEM,
193 "u32: at char %u: too many operators", (unsigned int)(arg - start));
197 * state 1 - reading values: read a range if nothing
198 * read yet, otherwise either ,range or && to end
203 exit_error(PARAMETER_PROBLEM,
204 "u32: at char %u: a second '&' was expected", (unsigned int)(arg - start));
206 exit_error(PARAMETER_PROBLEM,
207 "u32: at char %u: value spec missing", (unsigned int)(arg - start));
210 ct->nvalues = valind;
211 ct = &data->tests[++testind];
212 if (testind > XT_U32_MAXSIZE)
213 exit_error(PARAMETER_PROBLEM,
214 "u32: at char %u: too many \"&&\"s", (unsigned int)(arg - start));
220 } else { /* read value range */
221 if (valind > 0) { /* need , before number */
223 exit_error(PARAMETER_PROBLEM,
224 "u32: at char %u: expected \",\" or \"&&\"", (unsigned int)(arg - start));
227 ct->value[valind].min =
228 parse_number(&arg, arg - start);
230 while (isspace(*arg))
235 ct->value[valind].max =
236 parse_number(&arg, arg-start);
238 ct->value[valind].max =
239 ct->value[valind].min;
242 if (++valind > XT_U32_MAXSIZE)
243 exit_error(PARAMETER_PROBLEM,
244 "u32: at char %u: too many \",\"s", (unsigned int)(arg - start));
250 static void u32_print(const void *ip, const struct xt_entry_match *match,
253 const struct xt_u32 *data = (const void *)match->data;
261 static void u32_save(const void *ip, const struct xt_entry_match *match)
263 const struct xt_u32 *data = (const void *)match->data;
271 static struct xtables_match u32_match = {
274 .version = XTABLES_VERSION,
275 .size = XT_ALIGN(sizeof(struct xt_u32)),
276 .userspacesize = XT_ALIGN(sizeof(struct xt_u32)),
281 .extra_opts = u32_opts,
286 xtables_register_match(&u32_match);