1 diff -Nurp iptables-1.3.0-stock/extensions/.layer7-test iptables-1.3.0-layer7/extensions/.layer7-test
2 --- iptables-1.3.0-stock/extensions/.layer7-test 1969-12-31 18:00:00.000000000 -0600
3 +++ iptables-1.3.0-layer7/extensions/.layer7-test 2005-03-01 22:12:06.000000000 -0600
6 +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_layer7.h ] && echo layer7
7 diff -Nurp iptables-1.3.0-stock/extensions/libipt_layer7.c iptables-1.3.0-layer7/extensions/libipt_layer7.c
8 --- iptables-1.3.0-stock/extensions/libipt_layer7.c 1969-12-31 18:00:00.000000000 -0600
9 +++ iptables-1.3.0-layer7/extensions/libipt_layer7.c 2005-03-06 22:14:13.000000000 -0600
12 + Shared library add-on to iptables to add layer 7 matching support.
14 + By Matthew Strait <quadong@users.sf.net>, Oct 2003.
16 + http://l7-filter.sf.net
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
24 + Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
36 +#include <iptables.h>
37 +#include <linux/netfilter_ipv4/ipt_layer7.h>
39 +#define MAX_FN_LEN 256
41 +static char l7dir[MAX_FN_LEN] = "\0";
43 +/* Function which prints out usage message. */
44 +static void help(void)
47 + "LAYER7 match v%s options:\n"
48 + "--l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
49 + " (--l7dir must be specified before --l7proto if used!)\n"
50 + "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n",
52 + fputc('\n', stdout);
55 +static struct option opts[] = {
56 + { .name = "l7proto", .has_arg = 1, .flag = 0, .val = '1' },
57 + { .name = "l7dir", .has_arg = 1, .flag = 0, .val = '2' },
61 +/* Initialize the match. */
62 +static void init(struct ipt_entry_match *m, unsigned int *nfcache)
64 + *nfcache |= NFC_UNKNOWN;
67 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
68 +int parse_protocol_file(char * filename, const unsigned char * protoname, struct ipt_layer7_info *info)
74 + enum { protocol, pattern, done } datatype = protocol;
76 + f = fopen(filename, "r");
81 + while(getline(&line, &len, f) != -1)
83 + if(strlen(line) < 2 || line[0] == '#')
86 + /* strip the pesky newline... */
87 + if(line[strlen(line) - 1] == '\n')
88 + line[strlen(line) - 1] = '\0';
90 + if(datatype == protocol)
92 + if(strcmp(line, protoname))
93 + exit_error(OTHER_PROBLEM,
94 + "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
95 + protoname, filename);
97 + if(strlen(line) >= MAX_PROTOCOL_LEN)
98 + exit_error(PARAMETER_PROBLEM,
99 + "Protocol name in %s too long!", filename);
100 + strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
102 + datatype = pattern;
104 + else if(datatype == pattern)
106 + if(strlen(line) >= MAX_PATTERN_LEN)
107 + exit_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
108 + strncpy(info->pattern, line, MAX_PATTERN_LEN);
114 + exit_error(OTHER_PROBLEM, "Internal error");
117 + if(datatype != done)
118 + exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
120 + if(line) free(line);
126 + fprintf(stderr, "protocol: %s\npattern: %s\n\n",
132 +static int hex2dec(char c)
139 + return c - 'a' + 10;
141 + return c - 'A' + 10;
143 + exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
148 +/* takes a string with \xHH escapes and returns one with the characters
150 +static char * pre_process(char * s)
152 + char * result = malloc(strlen(s) + 1);
153 + int sindex = 0, rindex = 0;
154 + while( sindex < strlen(s) )
156 + if( sindex + 3 < strlen(s) &&
157 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
158 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
160 + /* carefully remember to call tolower here... */
161 + result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
162 + hex2dec(s[sindex + 3] ) );
163 + sindex += 3; /* 4 total */
166 + result[rindex] = tolower(s[sindex]);
171 + result[rindex] = '\0';
176 +#define MAX_SUBDIRS 128
177 +char ** readl7dir(char * dirname)
180 + struct dirent ** namelist;
181 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
186 + n = scandir(dirname, &namelist, 0, alphasort);
191 + exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
197 + char fulldirname[MAX_FN_LEN];
199 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
201 + if((scratchdir = opendir(fulldirname)) != NULL)
203 + closedir(scratchdir);
205 + if(!strcmp(namelist[n]->d_name, ".") ||
206 + !strcmp(namelist[n]->d_name, ".."))
210 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
211 + strcpy(subdirs[d], namelist[n]->d_name);
213 + if(d >= MAX_SUBDIRS - 1)
216 + "Too many subdirectories, skipping the rest!\n");
232 +parse_layer7_protocol(const unsigned char *s, struct ipt_layer7_info *info)
234 + char filename[MAX_FN_LEN];
237 + int n = 0, done = 0;
239 + if(strlen(l7dir) > 0)
242 + dir = "/etc/l7-protocols";
244 + subdirs = readl7dir(dir);
246 + while(subdirs[n] != NULL)
248 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
250 + //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
254 + exit_error(OTHER_PROBLEM,
255 + "Filename beginning with %s is too long!\n", filename);
258 + /* read in the pattern from the file */
259 + if(parse_protocol_file(filename, s, info))
261 + //fprintf(stderr, "found\n");
266 + //fprintf(stderr, "not found\n");
272 + exit_error(OTHER_PROBLEM,
273 + "Couldn't find a pattern definition file for %s.\n", s);
275 + /* process \xHH escapes and tolower everything. (our regex lib has no
276 + case insensitivity option.) */
277 + strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
280 +/* Function which parses command options; returns true if it ate an option */
281 +static int parse(int c, char **argv, int invert, unsigned int *flags,
282 + const struct ipt_entry *entry, unsigned int *nfcache,
283 + struct ipt_entry_match **match)
285 + struct ipt_layer7_info *layer7info =
286 + (struct ipt_layer7_info *)(*match)->data;
290 + check_inverse(optarg, &invert, &optind, 0);
291 + parse_layer7_protocol(argv[optind-1], layer7info);
293 + layer7info->invert = 1;
298 + /* not going to use this, but maybe we need to strip a ! anyway (?) */
299 + check_inverse(optarg, &invert, &optind, 0);
301 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
302 + exit_error(PARAMETER_PROBLEM, "directory name too long\n");
304 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
316 +/* Final check; must have specified --pattern. */
317 +static void final_check(unsigned int flags)
320 + exit_error(PARAMETER_PROBLEM,
321 + "LAYER7 match: You must specify `--pattern'");
324 +static void print_protocol(char s[], int invert, int numeric)
326 + fputs("l7proto ", stdout);
327 + if (invert) fputc('!', stdout);
331 +/* Prints out the matchinfo. */
332 +static void print(const struct ipt_ip *ip,
333 + const struct ipt_entry_match *match,
338 + print_protocol(((struct ipt_layer7_info *)match->data)->protocol,
339 + ((struct ipt_layer7_info *)match->data)->invert, numeric);
341 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
342 +static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
344 + const struct ipt_layer7_info *info =
345 + (const struct ipt_layer7_info*) match->data;
347 + printf("--l7proto %s%s ", (info->invert) ? "! ": "", info->protocol);
350 +static struct iptables_match layer7 = {
352 + .version = IPTABLES_VERSION,
353 + .size = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
354 + .userspacesize = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
358 + .final_check = &final_check,
366 + register_match(&layer7);
368 diff -Nurp iptables-1.3.0-stock/extensions/libipt_layer7.man iptables-1.3.0-layer7/extensions/libipt_layer7.man
369 --- iptables-1.3.0-stock/extensions/libipt_layer7.man 1969-12-31 18:00:00.000000000 -0600
370 +++ iptables-1.3.0-layer7/extensions/libipt_layer7.man 2005-03-01 22:12:06.000000000 -0600
372 +This module matches packets based on the application layer data of
373 +their connections. It uses regular expression matching to compare
374 +the application layer data to regular expressions found it the layer7
375 +configuration files. This is an experimental module which can be found at
376 +http://l7-filter.sf.net. It takes two options.
378 +.BI "--l7proto " "\fIprotocol\fP"
379 +Match the specified protocol. The protocol name must match a file
380 +name in /etc/l7-protocols/
382 +.BI "--l7dir " "\fIdirectory\fP"
383 +Use \fIdirectory\fP instead of /etc/l7-protocols/