add 2 more symbols missed from my last commit
[openwrt.git] / package / iptables / patches / 002-layer7-1.5nbd.patch
1 Index: iptables-1.3.8/extensions/.layer7-test
2 ===================================================================
3 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4 +++ iptables-1.3.8/extensions/.layer7-test      2007-07-31 15:27:56.000000000 -0500
5 @@ -0,0 +1,3 @@
6 +#! /bin/sh
7 +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_layer7.h ] && echo layer7
8 +[ -f $KERNEL_DIR/include/linux/netfilter/xt_layer7.h ] && echo layer7
9 Index: iptables-1.3.8/extensions/ipt_layer7.h
10 ===================================================================
11 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
12 +++ iptables-1.3.8/extensions/ipt_layer7.h      2007-07-31 15:27:56.000000000 -0500
13 @@ -0,0 +1,27 @@
14 +/* 
15 +  By Matthew Strait <quadong@users.sf.net>, Dec 2003.
16 +  http://l7-filter.sf.net
17 +
18 +  This program is free software; you can redistribute it and/or
19 +  modify it under the terms of the GNU General Public License
20 +  as published by the Free Software Foundation; either version
21 +  2 of the License, or (at your option) any later version.
22 +  http://www.gnu.org/licenses/gpl.txt
23 +*/
24 +
25 +#ifndef _IPT_LAYER7_H
26 +#define _IPT_LAYER7_H
27 +
28 +#define MAX_PATTERN_LEN 8192
29 +#define MAX_PROTOCOL_LEN 256
30 +
31 +typedef char *(*proc_ipt_search) (char *, char, char *);
32 +
33 +struct ipt_layer7_info {
34 +    char protocol[MAX_PROTOCOL_LEN];
35 +    char invert:1;
36 +    char pattern[MAX_PATTERN_LEN];
37 +       char pkt;
38 +};
39 +
40 +#endif /* _IPT_LAYER7_H */
41 Index: iptables-1.3.8/extensions/libipt_layer7.c
42 ===================================================================
43 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
44 +++ iptables-1.3.8/extensions/libipt_layer7.c   2007-07-31 15:27:56.000000000 -0500
45 @@ -0,0 +1,358 @@
46 +/* 
47 +   Shared library add-on to iptables to add layer 7 matching support. 
48 +  
49 +   By Matthew Strait <quadong@users.sf.net>, Oct 2003.
50 +
51 +   http://l7-filter.sf.net 
52 +
53 +   This program is free software; you can redistribute it and/or
54 +   modify it under the terms of the GNU General Public License
55 +   as published by the Free Software Foundation; either version
56 +   2 of the License, or (at your option) any later version.
57 +   http://www.gnu.org/licenses/gpl.txt
58 +
59 +   Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
60 +*/
61 +
62 +#define _GNU_SOURCE
63 +#include <stdio.h>
64 +#include <netdb.h>
65 +#include <string.h>
66 +#include <stdlib.h>
67 +#include <getopt.h>
68 +#include <ctype.h>
69 +#include <dirent.h>
70 +
71 +#include <iptables.h>
72 +#include "ipt_layer7.h"
73 +
74 +#define MAX_FN_LEN 256
75 +
76 +static char l7dir[MAX_FN_LEN] = "\0";
77 +
78 +/* Function which prints out usage message. */
79 +static void help(void)
80 +{
81 +       printf(
82 +       "LAYER7 match v%s options:\n"
83 +       "--l7dir <directory>  : Look for patterns here instead of /etc/l7-protocols/\n"
84 +       "                       (--l7dir must be specified before --l7proto if used!)\n"
85 +       "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n"
86 +       "--l7pkt              : Skip connection tracking and match individual packets\n",
87 +       IPTABLES_VERSION);
88 +       fputc('\n', stdout);
89 +}
90 +
91 +static struct option opts[] = {
92 +       { .name = "l7proto", .has_arg = 1, .flag = 0, .val = '1' },
93 +       { .name = "l7dir",   .has_arg = 1, .flag = 0, .val = '2' },
94 +       { .name = "l7pkt",   .has_arg = 0, .flag = 0, .val = '3' },
95 +       { .name = 0 }
96 +};
97 +
98 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
99 +int parse_protocol_file(char * filename, const unsigned char * protoname, struct ipt_layer7_info *info)
100 +{
101 +       FILE * f;
102 +       char * line = NULL;
103 +       size_t len = 0;
104 +
105 +       enum { protocol, pattern, done } datatype = protocol;
106 +
107 +       f = fopen(filename, "r");
108 +
109 +       if(!f)
110 +               return 0;
111 +
112 +       while(getline(&line, &len, f) != -1)
113 +       {
114 +               if(strlen(line) < 2 || line[0] == '#')
115 +                       continue;
116 +
117 +               /* strip the pesky newline... */
118 +               if(line[strlen(line) - 1] == '\n')
119 +                       line[strlen(line) - 1] = '\0';
120 +
121 +               if(datatype == protocol)
122 +               {
123 +                       if(strcmp(line, protoname))
124 +                               exit_error(OTHER_PROBLEM, 
125 +                                       "Protocol name (%s) doesn't match file name (%s).  Bailing out\n",
126 +                                       protoname, filename);
127 +
128 +                       if(strlen(line) >= MAX_PROTOCOL_LEN)
129 +                                exit_error(PARAMETER_PROBLEM, 
130 +                                       "Protocol name in %s too long!", filename);
131 +                       strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
132 +
133 +                       datatype = pattern; 
134 +               }
135 +               else if(datatype == pattern)
136 +               {
137 +                       if(strlen(line) >= MAX_PATTERN_LEN)
138 +                                exit_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
139 +                       strncpy(info->pattern, line, MAX_PATTERN_LEN);
140 +                       
141 +                       datatype = done;                        
142 +                       break;
143 +               }
144 +               else
145 +                       exit_error(OTHER_PROBLEM, "Internal error");
146 +       }
147 +
148 +       if(datatype != done)
149 +               exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
150 +
151 +       if(line) free(line);
152 +       fclose(f);
153 +
154 +       return 1;
155 +
156 +/*
157 +       fprintf(stderr, "protocol: %s\npattern: %s\n\n", 
158 +                       info->protocol,
159 +                       info->pattern);
160 +*/
161 +}
162 +
163 +static int hex2dec(char c)
164 +{
165 +        switch (c)
166 +        {
167 +                case '0' ... '9':
168 +                        return c - '0';
169 +                case 'a' ... 'f':
170 +                        return c - 'a' + 10;
171 +                case 'A' ... 'F':
172 +                        return c - 'A' + 10;
173 +                default:
174 +                        exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
175 +                        return 0;
176 +        }
177 +}
178 +
179 +/* takes a string with \xHH escapes and returns one with the characters 
180 +they stand for */
181 +static char * pre_process(char * s)
182 +{
183 +       char * result = malloc(strlen(s) + 1);
184 +       int sindex = 0, rindex = 0;
185 +        while( sindex < strlen(s) )
186 +        {
187 +            if( sindex + 3 < strlen(s) &&
188 +                s[sindex] == '\\' && s[sindex+1] == 'x' && 
189 +                isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) ) 
190 +                {
191 +                        /* carefully remember to call tolower here... */
192 +                        result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
193 +                                                  hex2dec(s[sindex + 3] ) );
194 +                        sindex += 3; /* 4 total */
195 +                }
196 +                else
197 +                        result[rindex] = tolower(s[sindex]);
198 +
199 +               sindex++; 
200 +               rindex++;
201 +        }
202 +       result[rindex] = '\0';
203 +
204 +       return result;
205 +}
206 +
207 +#define MAX_SUBDIRS 128
208 +char ** readl7dir(char * dirname)
209 +{
210 +        DIR             * scratchdir;
211 +        struct dirent   ** namelist;
212 +       char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
213 +
214 +        int n, d = 1;
215 +       subdirs[0] = "";
216 +
217 +        n = scandir(dirname, &namelist, 0, alphasort);
218 +
219 +       if (n < 0)
220 +       {
221 +            perror("scandir");
222 +           exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
223 +       }
224 +        else 
225 +       {
226 +               while(n--) 
227 +               {
228 +                       char fulldirname[MAX_FN_LEN];
229 +
230 +                       snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
231 +
232 +                       if((scratchdir = opendir(fulldirname)) != NULL)
233 +                       {
234 +                               closedir(scratchdir);
235 +
236 +                               if(!strcmp(namelist[n]->d_name, ".") || 
237 +                                  !strcmp(namelist[n]->d_name, ".."))
238 +                                       /* do nothing */ ;
239 +                               else
240 +                               {
241 +                                       subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
242 +                                       strcpy(subdirs[d], namelist[n]->d_name);
243 +                                       d++;
244 +                                       if(d >= MAX_SUBDIRS - 1)
245 +                                       {
246 +                                               fprintf(stderr, 
247 +                                                 "Too many subdirectories, skipping the rest!\n");
248 +                                               break;
249 +                                       }
250 +                               }
251 +                       }
252 +                       free(namelist[n]);
253 +               }
254 +               free(namelist);
255 +        }
256 +       
257 +       subdirs[d] = NULL;
258 +
259 +       return subdirs;
260 +}
261 +
262 +static void
263 +parse_layer7_protocol(const unsigned char *s, struct ipt_layer7_info *info)
264 +{
265 +       char filename[MAX_FN_LEN];
266 +       char * dir = NULL;
267 +       char ** subdirs;
268 +       int n = 0, done = 0;
269 +
270 +       if(strlen(l7dir) > 0)
271 +               dir = l7dir;
272 +       else
273 +               dir = "/etc/l7-protocols";
274 +
275 +       subdirs = readl7dir(dir);
276 +
277 +       while(subdirs[n] != NULL)
278 +       {
279 +               int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
280 +
281 +               //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
282 +
283 +               if(c > MAX_FN_LEN)
284 +               {
285 +                       exit_error(OTHER_PROBLEM, 
286 +                               "Filename beginning with %s is too long!\n", filename);
287 +               }
288 +
289 +               /* read in the pattern from the file */
290 +               if(parse_protocol_file(filename, s, info))
291 +               {
292 +                       //fprintf(stderr, "found\n");
293 +                       done = 1;
294 +                       break;
295 +               }
296 +               
297 +               //fprintf(stderr, "not found\n");
298 +
299 +               n++;
300 +       }
301 +
302 +       if(!done)
303 +               exit_error(OTHER_PROBLEM, 
304 +                       "Couldn't find a pattern definition file for %s.\n", s);
305 +
306 +       /* process \xHH escapes and tolower everything. (our regex lib has no
307 +       case insensitivity option.) */
308 +       strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
309 +}
310 +
311 +/* Function which parses command options; returns true if it ate an option */
312 +static int parse(int c, char **argv, int invert, unsigned int *flags,
313 +      const struct ipt_entry *entry, unsigned int *nfcache,
314 +      struct ipt_entry_match **match)
315 +{
316 +       struct ipt_layer7_info *layer7info = 
317 +               (struct ipt_layer7_info *)(*match)->data;
318 +
319 +       switch (c) {
320 +       case '1':
321 +               check_inverse(optarg, &invert, &optind, 0);
322 +               parse_layer7_protocol(argv[optind-1], layer7info);
323 +               if (invert)
324 +                       layer7info->invert = 1;
325 +               *flags = 1;
326 +               break;
327 +
328 +       case '2':
329 +               /* not going to use this, but maybe we need to strip a ! anyway (?) */
330 +               check_inverse(optarg, &invert, &optind, 0);
331 +
332 +               if(strlen(argv[optind-1]) >= MAX_FN_LEN)
333 +                       exit_error(PARAMETER_PROBLEM, "directory name too long\n");
334 +
335 +               strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
336 +
337 +               *flags = 1;
338 +               break;
339 +       case '3':
340 +               layer7info->pkt = 1;
341 +               break;
342 +
343 +       default:
344 +               return 0;
345 +       }
346 +
347 +       return 1;
348 +}
349 +
350 +/* Final check; must have specified --pattern. */
351 +static void final_check(unsigned int flags)
352 +{
353 +       if (!flags)
354 +               exit_error(PARAMETER_PROBLEM,
355 +                          "LAYER7 match: You must specify `--pattern'");
356 +}
357 +
358 +static void print_protocol(char s[], int invert, int numeric)
359 +{
360 +       fputs("l7proto ", stdout);
361 +       if (invert) fputc('!', stdout);
362 +       printf("%s ", s);
363 +}
364 +
365 +/* Prints out the matchinfo. */
366 +static void print(const struct ipt_ip *ip,
367 +      const struct ipt_entry_match *match,
368 +      int numeric)
369 +{
370 +       printf("LAYER7 ");
371 +
372 +       print_protocol(((struct ipt_layer7_info *)match->data)->protocol,
373 +                 ((struct ipt_layer7_info *)match->data)->invert, numeric);
374 +
375 +       if (((struct ipt_layer7_info *)match->data)->pkt)
376 +               printf("l7pkt ");
377 +}
378 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
379 +static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
380 +{
381 +        const struct ipt_layer7_info *info =
382 +            (const struct ipt_layer7_info*) match->data;
383 +
384 +        printf("--l7proto %s%s ", (info->invert)   ? "! ": "", info->protocol);
385 +}
386 +
387 +static struct iptables_match layer7 = { 
388 +    .name          = "layer7",
389 +    .version       = IPTABLES_VERSION,
390 +    .size          = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
391 +    .userspacesize = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
392 +    .help          = &help,
393 +    .parse         = &parse,
394 +    .final_check   = &final_check,
395 +    .print         = &print,
396 +    .save          = &save,
397 +    .extra_opts    = opts
398 +};
399 +
400 +void _init(void)
401 +{
402 +       register_match(&layer7);
403 +}
404 Index: iptables-1.3.8/extensions/libipt_layer7.man
405 ===================================================================
406 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
407 +++ iptables-1.3.8/extensions/libipt_layer7.man 2007-07-31 15:27:56.000000000 -0500
408 @@ -0,0 +1,13 @@
409 +This module matches packets based on the application layer data of 
410 +their connections.  It uses regular expression matching to compare 
411 +the application layer data to regular expressions found it the layer7 
412 +configuration files.  This is an experimental module which can be found at 
413 +http://l7-filter.sf.net.  It takes two options.
414 +.TP
415 +.BI "--l7proto " "\fIprotocol\fP"
416 +Match the specified protocol.  The protocol name must match a file 
417 +name in /etc/l7-protocols/
418 +.TP
419 +.BI "--l7dir " "\fIdirectory\fP"
420 +Use \fIdirectory\fP instead of /etc/l7-protocols/
421 +