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