fix for f12, gcc4.4
[iptables.git] / extensions / libipt_u32.c
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/ipv4/netfilter/ipt_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 #include <stdio.h>
11 #include <netdb.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <getopt.h>
15 #include <iptables.h>
16 #include <linux/netfilter_ipv4/ipt_u32.h>
17 #include <errno.h>
18 #include <ctype.h>
19
20 /* Function which prints out usage message. */
21 static void
22 help(void)
23 {
24         printf( "u32 v%s options:\n"
25                 " --u32 tests\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"
31                 ,IPTABLES_VERSION);
32 }
33
34 /* defined in /usr/include/getopt.h maybe in man getopt */
35 static struct option opts[] = {
36         { "u32", 1, 0, '1' },
37         { 0 }
38 };
39
40 /* shared printing code */
41 static void print_u32(struct ipt_u32 *data)
42 {
43         unsigned int testind;
44
45         for (testind=0; testind < data->ntests; testind++) {
46                 if (testind) printf("&&");
47                 {
48                         unsigned int i;
49
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;
57                                 }
58                                 printf("0x%x", data->tests[testind].location[i].number);
59                         }
60                         printf("=");
61                         for (i = 0; i < data->tests[testind].nvalues; i++) {
62                                 if (i) printf(",");
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);
68                         }
69                 }
70         }
71         printf(" ");
72 }
73
74 /* string_to_number is not quite what we need here ... */
75 u_int32_t parse_number(char **s, int pos)
76 {
77         u_int32_t number;
78         char *end;
79         errno = 0;
80
81         number = strtoul(*s, &end, 0);
82         if (end == *s)
83                 exit_error(PARAMETER_PROBLEM, 
84                            "u32: at char %d expected number", pos);
85         if (errno)
86                 exit_error(PARAMETER_PROBLEM, 
87                            "u32: at char %d error reading number", pos);
88         *s = end;
89         return number;
90 }
91
92 /* Function which parses command options; returns true if it ate an option */
93 static int
94 parse(int c, char **argv, int invert, unsigned int *flags,
95       const struct ipt_entry *entry,
96       struct ipt_entry_match **match)
97 {
98         struct ipt_u32 *data = (struct ipt_u32 *)(*match)->data;
99         char *arg = argv[optind-1]; /* the argument string */
100         char *start = arg;
101         int state=0, testind=0, locind=0, valind=0;
102
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 */
109                         if (state == 0)
110                                 exit_error(PARAMETER_PROBLEM, 
111                                            "u32: input ended in location spec");
112                         if (valind == 0)
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;
117                         testind++;
118                         data->ntests=testind;
119                         if (testind > U32MAXSIZE)
120                                 exit_error(PARAMETER_PROBLEM, 
121                                            "u32: at char %d too many &&'s",
122                                            arg-start);
123                         /* debugging 
124                            print_u32(data);printf("\n");
125                            exit_error(PARAMETER_PROBLEM, "debugging output done"); */
126                         return 1;
127                 }
128                 if (state == 0) {
129                         /* reading location: read a number if nothing read yet,
130                            otherwise either op number or = to end location spec */       
131                         if (*arg == '=') {
132                                 if (locind == 0)
133                                         exit_error(PARAMETER_PROBLEM,
134                                                    "u32: at char %d location spec missing", arg-start);
135                                 else {
136                                         arg++; 
137                                         state=1;
138                                 }
139                         }
140                         else {
141                                 if (locind) { /* need op before number */
142                                         if (*arg == '&') {
143                                                 data->tests[testind].location[locind].nextop = IPT_U32_AND;
144                                         }
145                                         else if (*arg == '<') {
146                                                 arg++;
147                                                 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;
151                                         }
152                                         else if (*arg == '>') {
153                                                 arg++;
154                                                 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;
158                                         }
159                                         else if (*arg == '@') {
160                                                 data->tests[testind].location[locind].nextop = IPT_U32_AT;
161                                         }
162                                         else exit_error(PARAMETER_PROBLEM,
163                                                         "u32: at char %d operator expected", arg-start);
164                                         arg++;
165                                 }
166                                 /* now a number; string_to_number skips white space? */
167                                 data->tests[testind].location[locind].number =
168                                         parse_number(&arg, arg-start);
169                                 locind++;
170                                 if (locind > U32MAXSIZE)
171                                         exit_error(PARAMETER_PROBLEM,
172                                                    "u32: at char %d too many operators", arg-start);
173                         }
174                 }
175                 else {
176                         /* state 1 - reading values: read a range if nothing read yet,
177                            otherwise either ,range or && to end test spec */
178                         if (*arg == '&') {
179                                 arg++;
180                                 if (*arg != '&')
181                                         exit_error(PARAMETER_PROBLEM,
182                                                    "u32: at char %d a second & expected", arg-start);
183                                 if (valind == 0)
184                                         exit_error(PARAMETER_PROBLEM,
185                                                    "u32: at char %d value spec missing", arg-start);
186                                 else {
187                                         data->tests[testind].nnums = locind;
188                                         data->tests[testind].nvalues = valind;
189                                         testind++;
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;
194                                 }
195                         }
196                         else { /* read value range */
197                                 if (valind) { /* need , before number */
198                                         if (*arg != ',')
199                                                 exit_error(PARAMETER_PROBLEM,
200                                                            "u32: at char %d expected , or &&", arg-start);
201                                         arg++;
202                                 }
203                                 data->tests[testind].value[valind].min = parse_number(&arg, arg-start);
204                                 while (isspace(*arg)) 
205                                         arg++;  /* another place white space could be */
206                                 if (*arg==':') {
207                                         arg++;
208                                         data->tests[testind].value[valind].max
209                                                 = parse_number(&arg, arg-start);
210                                 }
211                                 else data->tests[testind].value[valind].max
212                                              = data->tests[testind].value[valind].min;
213                                 valind++;
214                                 if (valind > U32MAXSIZE)
215                                         exit_error(PARAMETER_PROBLEM,
216                                                    "u32: at char %d too many ,'s", arg-start);
217                         }
218                 }
219         }
220 }
221
222 /* Final check; must specify something. */
223 static void
224 final_check(unsigned int flags)
225 {
226 }
227
228 /* Prints out the matchinfo. */
229 static void
230 print(const struct ipt_ip *ip,
231       const struct ipt_entry_match *match,
232       int numeric)
233 {
234         printf("u32 ");
235         print_u32((struct ipt_u32 *)match->data);
236 }
237
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)
240 {
241         printf("--u32 ");
242         print_u32((struct ipt_u32 *)match->data);
243 }
244
245 struct iptables_match u32 = {
246         .next           = NULL,
247         .name           = "u32",
248         .version        = IPTABLES_VERSION,
249         .size           = IPT_ALIGN(sizeof(struct ipt_u32)),
250         .userspacesize  = IPT_ALIGN(sizeof(struct ipt_u32)),
251         .help           = &help,
252         .parse          = &parse,
253         .final_check    = &final_check,
254         .print          = &print,
255         .save           = &save,
256         .extra_opts     = opts
257 };
258
259 void
260 _init(void)
261 {
262         register_match(&u32);
263 }