1 Index: iptables-1.3.8/extensions/.layer7-test
2 ===================================================================
4 +++ iptables-1.3.8/extensions/.layer7-test
7 +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_layer7.h ] && echo layer7
8 Index: iptables-1.3.8/extensions/libipt_layer7.c
9 ===================================================================
11 +++ iptables-1.3.8/extensions/libipt_layer7.c
14 + Shared library add-on to iptables to add layer 7 matching support.
16 + By Matthew Strait <quadong@users.sf.net>, Oct 2003.
18 + http://l7-filter.sf.net
20 + This program is free software; you can redistribute it and/or
21 + modify it under the terms of the GNU General Public License
22 + as published by the Free Software Foundation; either version
23 + 2 of the License, or (at your option) any later version.
24 + http://www.gnu.org/licenses/gpl.txt
26 + Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
38 +#include <iptables.h>
39 +#include <linux/netfilter_ipv4/ipt_layer7.h>
41 +#define MAX_FN_LEN 256
43 +static char l7dir[MAX_FN_LEN] = "\0";
45 +/* Function which prints out usage message. */
46 +static void help(void)
49 + "LAYER7 match v%s options:\n"
50 + "--l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
51 + " (--l7dir must be specified before --l7proto if used!)\n"
52 + "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n",
54 + fputc('\n', stdout);
57 +static struct option opts[] = {
58 + { .name = "l7proto", .has_arg = 1, .flag = 0, .val = '1' },
59 + { .name = "l7dir", .has_arg = 1, .flag = 0, .val = '2' },
63 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
64 +int parse_protocol_file(char * filename, const unsigned char * protoname, struct ipt_layer7_info *info)
70 + enum { protocol, pattern, done } datatype = protocol;
72 + f = fopen(filename, "r");
77 + while(getline(&line, &len, f) != -1)
79 + if(strlen(line) < 2 || line[0] == '#')
82 + /* strip the pesky newline... */
83 + if(line[strlen(line) - 1] == '\n')
84 + line[strlen(line) - 1] = '\0';
86 + if(datatype == protocol)
88 + /* Ignore everything on the line beginning with the
89 + first space or tab . For instance, this allows the
90 + protocol line in http.pat to be "http " (or
91 + "http I am so cool") instead of just "http". */
92 + if(strchr(line, ' ')){
93 + char * space = strchr(line, ' ');
96 + if(strchr(line, '\t')){
97 + char * space = strchr(line, '\t');
101 + /* sanity check. First non-comment non-blank
102 + line must be the same as the file name. */
103 + if(strcmp(line, protoname))
104 + exit_error(OTHER_PROBLEM,
105 + "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
108 + if(strlen(line) >= MAX_PROTOCOL_LEN)
109 + exit_error(PARAMETER_PROBLEM,
110 + "Protocol name in %s too long!", filename);
111 + strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
113 + datatype = pattern;
115 + else if(datatype == pattern)
117 + if(strlen(line) >= MAX_PATTERN_LEN)
118 + exit_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
119 + strncpy(info->pattern, line, MAX_PATTERN_LEN);
125 + exit_error(OTHER_PROBLEM, "Internal error");
128 + if(datatype != done)
129 + exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
131 + if(line) free(line);
137 + fprintf(stderr, "protocol: %s\npattern: %s\n\n",
143 +static int hex2dec(char c)
150 + return c - 'a' + 10;
152 + return c - 'A' + 10;
154 + exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
159 +/* takes a string with \xHH escapes and returns one with the characters
161 +static char * pre_process(char * s)
163 + char * result = malloc(strlen(s) + 1);
164 + int sindex = 0, rindex = 0;
165 + while( sindex < strlen(s) )
167 + if( sindex + 3 < strlen(s) &&
168 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
169 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
171 + /* carefully remember to call tolower here... */
172 + result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
173 + hex2dec(s[sindex + 3] ) );
175 + switch ( result[rindex] )
190 + "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n"
191 + "I recommend that you write this as %c or \\%c, depending on what you meant.\n",
192 + result[rindex], s[sindex + 2], s[sindex + 3], result[rindex], result[rindex]);
196 + "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n");
203 + sindex += 3; /* 4 total */
206 + result[rindex] = tolower(s[sindex]);
211 + result[rindex] = '\0';
216 +#define MAX_SUBDIRS 128
217 +char ** readl7dir(char * dirname)
220 + struct dirent ** namelist;
221 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
226 + n = scandir(dirname, &namelist, 0, alphasort);
231 + exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
237 + char fulldirname[MAX_FN_LEN];
239 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
241 + if((scratchdir = opendir(fulldirname)) != NULL)
243 + closedir(scratchdir);
245 + if(!strcmp(namelist[n]->d_name, ".") ||
246 + !strcmp(namelist[n]->d_name, ".."))
250 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
251 + strcpy(subdirs[d], namelist[n]->d_name);
253 + if(d >= MAX_SUBDIRS - 1)
256 + "Too many subdirectories, skipping the rest!\n");
272 +parse_layer7_protocol(const unsigned char *s, struct ipt_layer7_info *info)
274 + char filename[MAX_FN_LEN];
277 + int n = 0, done = 0;
279 + if(strlen(l7dir) > 0)
282 + dir = "/etc/l7-protocols";
284 + subdirs = readl7dir(dir);
286 + while(subdirs[n] != NULL)
288 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
290 + //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
294 + exit_error(OTHER_PROBLEM,
295 + "Filename beginning with %s is too long!\n", filename);
298 + /* read in the pattern from the file */
299 + if(parse_protocol_file(filename, s, info))
301 + //fprintf(stderr, "found\n");
306 + //fprintf(stderr, "not found\n");
312 + exit_error(OTHER_PROBLEM,
313 + "Couldn't find a pattern definition file for %s.\n", s);
315 + /* process \xHH escapes and tolower everything. (our regex lib has no
316 + case insensitivity option.) */
317 + strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
320 +/* Function which parses command options; returns true if it ate an option */
321 +static int parse(int c, char **argv, int invert, unsigned int *flags,
322 + const struct ipt_entry *entry, unsigned int *nfcache,
323 + struct ipt_entry_match **match)
325 + struct ipt_layer7_info *layer7info =
326 + (struct ipt_layer7_info *)(*match)->data;
330 + check_inverse(optarg, &invert, &optind, 0);
331 + parse_layer7_protocol(argv[optind-1], layer7info);
333 + layer7info->invert = 1;
338 + /* not going to use this, but maybe we need to strip a ! anyway (?) */
339 + check_inverse(optarg, &invert, &optind, 0);
341 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
342 + exit_error(PARAMETER_PROBLEM, "directory name too long\n");
344 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
356 +/* Final check; must have specified --l7proto */
357 +static void final_check(unsigned int flags)
360 + exit_error(PARAMETER_PROBLEM,
361 + "LAYER7 match: You must specify `--l7proto'");
364 +static void print_protocol(char s[], int invert, int numeric)
366 + fputs("l7proto ", stdout);
367 + if (invert) fputc('!', stdout);
371 +/* Prints out the matchinfo. */
372 +static void print(const struct ipt_ip *ip,
373 + const struct ipt_entry_match *match,
378 + print_protocol(((struct ipt_layer7_info *)match->data)->protocol,
379 + ((struct ipt_layer7_info *)match->data)->invert, numeric);
381 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
382 +static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
384 + const struct ipt_layer7_info *info =
385 + (const struct ipt_layer7_info*) match->data;
387 + printf("--l7proto %s%s ", (info->invert) ? "! ": "", info->protocol);
390 +static struct iptables_match layer7 = {
392 + .version = IPTABLES_VERSION,
393 + .size = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
394 + .userspacesize = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
397 + .final_check = &final_check,
405 + register_match(&layer7);
407 Index: iptables-1.3.8/extensions/libipt_layer7.man
408 ===================================================================
410 +++ iptables-1.3.8/extensions/libipt_layer7.man
412 +This module matches packets based on the application layer data of
413 +their connections. It uses regular expression matching to compare
414 +the application layer data to regular expressions found it the layer7
415 +configuration files. This is an experimental module which can be found at
416 +http://l7-filter.sf.net. It takes two options.
418 +.BI "--l7proto " "\fIprotocol\fP"
419 +Match the specified protocol. The protocol name must match a file
420 +name in /etc/l7-protocols/ or one of its first-level child directories.
422 +.BI "--l7dir " "\fIdirectory\fP"
423 +Use \fIdirectory\fP instead of /etc/l7-protocols/. This option must be
424 +specified before --l7proto.