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
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
14 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
15 + http://l7-filter.sf.net
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
24 +#ifndef _IPT_LAYER7_H
25 +#define _IPT_LAYER7_H
27 +#define MAX_PATTERN_LEN 8192
28 +#define MAX_PROTOCOL_LEN 256
30 +typedef char *(*proc_ipt_search) (char *, char, char *);
32 +struct ipt_layer7_info {
33 + char protocol[MAX_PROTOCOL_LEN];
35 + char pattern[MAX_PATTERN_LEN];
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
46 + Shared library add-on to iptables to add layer 7 matching support.
48 + By Matthew Strait <quadong@users.sf.net>, Oct 2003.
50 + http://l7-filter.sf.net
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
58 + Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
70 +#include <iptables.h>
71 +#include "ipt_layer7.h"
73 +#define MAX_FN_LEN 256
75 +static char l7dir[MAX_FN_LEN] = "\0";
77 +/* Function which prints out usage message. */
78 +static void help(void)
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",
87 + fputc('\n', stdout);
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' },
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)
101 + char * line = NULL;
104 + enum { protocol, pattern, done } datatype = protocol;
106 + f = fopen(filename, "r");
111 + while(getline(&line, &len, f) != -1)
113 + if(strlen(line) < 2 || line[0] == '#')
116 + /* strip the pesky newline... */
117 + if(line[strlen(line) - 1] == '\n')
118 + line[strlen(line) - 1] = '\0';
120 + if(datatype == protocol)
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);
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);
132 + datatype = pattern;
134 + else if(datatype == pattern)
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);
144 + exit_error(OTHER_PROBLEM, "Internal error");
147 + if(datatype != done)
148 + exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
150 + if(line) free(line);
156 + fprintf(stderr, "protocol: %s\npattern: %s\n\n",
162 +static int hex2dec(char c)
169 + return c - 'a' + 10;
171 + return c - 'A' + 10;
173 + exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
178 +/* takes a string with \xHH escapes and returns one with the characters
180 +static char * pre_process(char * s)
182 + char * result = malloc(strlen(s) + 1);
183 + int sindex = 0, rindex = 0;
184 + while( sindex < strlen(s) )
186 + if( sindex + 3 < strlen(s) &&
187 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
188 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
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 */
196 + result[rindex] = tolower(s[sindex]);
201 + result[rindex] = '\0';
206 +#define MAX_SUBDIRS 128
207 +char ** readl7dir(char * dirname)
210 + struct dirent ** namelist;
211 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
216 + n = scandir(dirname, &namelist, 0, alphasort);
221 + exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
227 + char fulldirname[MAX_FN_LEN];
229 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
231 + if((scratchdir = opendir(fulldirname)) != NULL)
233 + closedir(scratchdir);
235 + if(!strcmp(namelist[n]->d_name, ".") ||
236 + !strcmp(namelist[n]->d_name, ".."))
240 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
241 + strcpy(subdirs[d], namelist[n]->d_name);
243 + if(d >= MAX_SUBDIRS - 1)
246 + "Too many subdirectories, skipping the rest!\n");
262 +parse_layer7_protocol(const unsigned char *s, struct ipt_layer7_info *info)
264 + char filename[MAX_FN_LEN];
267 + int n = 0, done = 0;
269 + if(strlen(l7dir) > 0)
272 + dir = "/etc/l7-protocols";
274 + subdirs = readl7dir(dir);
276 + while(subdirs[n] != NULL)
278 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
280 + //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
284 + exit_error(OTHER_PROBLEM,
285 + "Filename beginning with %s is too long!\n", filename);
288 + /* read in the pattern from the file */
289 + if(parse_protocol_file(filename, s, info))
291 + //fprintf(stderr, "found\n");
296 + //fprintf(stderr, "not found\n");
302 + exit_error(OTHER_PROBLEM,
303 + "Couldn't find a pattern definition file for %s.\n", s);
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);
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)
315 + struct ipt_layer7_info *layer7info =
316 + (struct ipt_layer7_info *)(*match)->data;
320 + check_inverse(optarg, &invert, &optind, 0);
321 + parse_layer7_protocol(argv[optind-1], layer7info);
323 + layer7info->invert = 1;
328 + /* not going to use this, but maybe we need to strip a ! anyway (?) */
329 + check_inverse(optarg, &invert, &optind, 0);
331 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
332 + exit_error(PARAMETER_PROBLEM, "directory name too long\n");
334 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
339 + layer7info->pkt = 1;
349 +/* Final check; must have specified --pattern. */
350 +static void final_check(unsigned int flags)
353 + exit_error(PARAMETER_PROBLEM,
354 + "LAYER7 match: You must specify `--pattern'");
357 +static void print_protocol(char s[], int invert, int numeric)
359 + fputs("l7proto ", stdout);
360 + if (invert) fputc('!', stdout);
364 +/* Prints out the matchinfo. */
365 +static void print(const struct ipt_ip *ip,
366 + const struct ipt_entry_match *match,
371 + print_protocol(((struct ipt_layer7_info *)match->data)->protocol,
372 + ((struct ipt_layer7_info *)match->data)->invert, numeric);
374 + if (((struct ipt_layer7_info *)match->data)->pkt)
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)
380 + const struct ipt_layer7_info *info =
381 + (const struct ipt_layer7_info*) match->data;
383 + printf("--l7proto %s%s ", (info->invert) ? "! ": "", info->protocol);
386 +static struct iptables_match layer7 = {
388 + .version = IPTABLES_VERSION,
389 + .size = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
390 + .userspacesize = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
393 + .final_check = &final_check,
401 + register_match(&layer7);
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
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.
414 +.BI "--l7proto " "\fIprotocol\fP"
415 +Match the specified protocol. The protocol name must match a file
416 +name in /etc/l7-protocols/
418 +.BI "--l7dir " "\fIdirectory\fP"
419 +Use \fIdirectory\fP instead of /etc/l7-protocols/