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/ipv4/netfilter/ipt_u32.c
7 * (C) 2002 by Don Cohen <don-netf@isis.cs3-inc.com>
8 * Released under the terms of GNU GPL v2
16 #include <linux/netfilter_ipv4/ipt_u32.h>
20 /* Function which prints out usage message. */
24 printf( "u32 v%s options:\n"
26 " tests := location = value | tests && location = value\n"
27 " value := range | value , range\n"
28 " range := number | number : number\n"
29 " location := number | location operator number\n"
30 " operator := & | << | >> | @\n"
34 /* defined in /usr/include/getopt.h maybe in man getopt */
35 static struct option opts[] = {
40 /* shared printing code */
41 static void print_u32(struct ipt_u32 *data)
45 for (testind=0; testind < data->ntests; testind++) {
46 if (testind) printf("&&");
50 printf("0x%x", data->tests[testind].location[0].number);
51 for (i = 1; i < data->tests[testind].nnums; i++) {
52 switch (data->tests[testind].location[i].nextop) {
53 case IPT_U32_AND: printf("&"); break;
54 case IPT_U32_LEFTSH: printf("<<"); break;
55 case IPT_U32_RIGHTSH: printf(">>"); break;
56 case IPT_U32_AT: printf("@"); break;
58 printf("0x%x", data->tests[testind].location[i].number);
61 for (i = 0; i < data->tests[testind].nvalues; i++) {
63 if (data->tests[testind].value[i].min
64 == data->tests[testind].value[i].max)
65 printf("0x%x", data->tests[testind].value[i].min);
66 else printf("0x%x:0x%x", data->tests[testind].value[i].min,
67 data->tests[testind].value[i].max);
74 /* string_to_number is not quite what we need here ... */
75 u_int32_t parse_number(char **s, int pos)
81 number = strtoul(*s, &end, 0);
83 exit_error(PARAMETER_PROBLEM,
84 "u32: at char %d expected number", pos);
86 exit_error(PARAMETER_PROBLEM,
87 "u32: at char %d error reading number", pos);
92 /* Function which parses command options; returns true if it ate an option */
94 parse(int c, char **argv, int invert, unsigned int *flags,
95 const struct ipt_entry *entry,
96 struct ipt_entry_match **match)
98 struct ipt_u32 *data = (struct ipt_u32 *)(*match)->data;
99 char *arg = argv[optind-1]; /* the argument string */
101 int state=0, testind=0, locind=0, valind=0;
103 if (c != '1') return 0;
104 /* states: 0 = looking for numbers and operations, 1 = looking for ranges */
105 while (1) { /* read next operand/number or range */
106 while (isspace(*arg))
107 arg++; /* skip white space */
108 if (! *arg) { /* end of argument found */
110 exit_error(PARAMETER_PROBLEM,
111 "u32: input ended in location spec");
113 exit_error(PARAMETER_PROBLEM,
114 "u32: test ended with no value spec");
115 data->tests[testind].nnums = locind;
116 data->tests[testind].nvalues = valind;
118 data->ntests=testind;
119 if (testind > U32MAXSIZE)
120 exit_error(PARAMETER_PROBLEM,
121 "u32: at char %d too many &&'s",
124 print_u32(data);printf("\n");
125 exit_error(PARAMETER_PROBLEM, "debugging output done"); */
129 /* reading location: read a number if nothing read yet,
130 otherwise either op number or = to end location spec */
133 exit_error(PARAMETER_PROBLEM,
134 "u32: at char %d location spec missing", arg-start);
141 if (locind) { /* need op before number */
143 data->tests[testind].location[locind].nextop = IPT_U32_AND;
145 else if (*arg == '<') {
148 exit_error(PARAMETER_PROBLEM,
149 "u32: at char %d a second < expected", arg-start);
150 data->tests[testind].location[locind].nextop = IPT_U32_LEFTSH;
152 else if (*arg == '>') {
155 exit_error(PARAMETER_PROBLEM,
156 "u32: at char %d a second > expected", arg-start);
157 data->tests[testind].location[locind].nextop = IPT_U32_RIGHTSH;
159 else if (*arg == '@') {
160 data->tests[testind].location[locind].nextop = IPT_U32_AT;
162 else exit_error(PARAMETER_PROBLEM,
163 "u32: at char %d operator expected", arg-start);
166 /* now a number; string_to_number skips white space? */
167 data->tests[testind].location[locind].number =
168 parse_number(&arg, arg-start);
170 if (locind > U32MAXSIZE)
171 exit_error(PARAMETER_PROBLEM,
172 "u32: at char %d too many operators", arg-start);
176 /* state 1 - reading values: read a range if nothing read yet,
177 otherwise either ,range or && to end test spec */
181 exit_error(PARAMETER_PROBLEM,
182 "u32: at char %d a second & expected", arg-start);
184 exit_error(PARAMETER_PROBLEM,
185 "u32: at char %d value spec missing", arg-start);
187 data->tests[testind].nnums = locind;
188 data->tests[testind].nvalues = valind;
190 if (testind > U32MAXSIZE)
191 exit_error(PARAMETER_PROBLEM,
192 "u32: at char %d too many &&'s", arg-start);
193 arg++; state=0; locind=0; valind=0;
196 else { /* read value range */
197 if (valind) { /* need , before number */
199 exit_error(PARAMETER_PROBLEM,
200 "u32: at char %d expected , or &&", arg-start);
203 data->tests[testind].value[valind].min = parse_number(&arg, arg-start);
204 while (isspace(*arg))
205 arg++; /* another place white space could be */
208 data->tests[testind].value[valind].max
209 = parse_number(&arg, arg-start);
211 else data->tests[testind].value[valind].max
212 = data->tests[testind].value[valind].min;
214 if (valind > U32MAXSIZE)
215 exit_error(PARAMETER_PROBLEM,
216 "u32: at char %d too many ,'s", arg-start);
222 /* Final check; must specify something. */
224 final_check(unsigned int flags)
228 /* Prints out the matchinfo. */
230 print(const struct ipt_ip *ip,
231 const struct ipt_entry_match *match,
235 print_u32((struct ipt_u32 *)match->data);
238 /* Saves the union ipt_matchinfo in parsable form to stdout. */
239 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
242 print_u32((struct ipt_u32 *)match->data);
245 struct iptables_match u32 = {
248 .version = IPTABLES_VERSION,
249 .size = IPT_ALIGN(sizeof(struct ipt_u32)),
250 .userspacesize = IPT_ALIGN(sizeof(struct ipt_u32)),
253 .final_check = &final_check,
262 register_match(&u32);