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.c.orig iptables-1.3.0-layer7/extensions/libipt_layer7.c.orig
369 --- iptables-1.3.0-stock/extensions/libipt_layer7.c.orig 1969-12-31 18:00:00.000000000 -0600
370 +++ iptables-1.3.0-layer7/extensions/libipt_layer7.c.orig 2005-03-06 22:20:28.000000000 -0600
373 + Shared library add-on to iptables to add layer 7 matching support.
375 + By Matthew Strait <quadong@users.sf.net>, Oct 2003.
377 + http://l7-filter.sf.net
379 + This program is free software; you can redistribute it and/or
380 + modify it under the terms of the GNU General Public License
381 + as published by the Free Software Foundation; either version
382 + 2 of the License, or (at your option) any later version.
383 + http://www.gnu.org/licenses/gpl.txt
385 + Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
397 +#include <iptables.h>
398 +#include <linux/netfilter_ipv4/ipt_layer7.h>
400 +#define MAX_FN_LEN 256
402 +static char l7dir[MAX_FN_LEN] = "\0";
404 +/* Function which prints out usage message. */
405 +static void help(void)
408 + "LAYER7 match v%s options:\n"
409 + "--l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
410 + " (--l7dir must be specified before --l7proto if used!)\n"
411 + "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n",
413 + fputc('\n', stdout);
416 +static struct option opts[] = {
417 + { .name = "l7proto", .has_arg = 1, .flag = 0, .val = '1' },
418 + { .name = "l7dir", .has_arg = 1, .flag = 0, .val = '2' },
422 +/* Initialize the match. */
423 +static void init(struct ipt_entry_match *m, unsigned int *nfcache)
425 + *nfcache |= NFC_UNKNOWN;
428 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
429 +int parse_protocol_file(char * filename, const unsigned char * protoname, struct ipt_layer7_info *info)
432 + char * line = NULL;
435 + enum { protocol, pattern, done } datatype = protocol;
437 + f = fopen(filename, "r");
442 + while(getline(&line, &len, f) != -1)
444 + if(strlen(line) < 2 || line[0] == '#')
447 + /* strip the pesky newline... */
448 + if(line[strlen(line) - 1] == '\n')
449 + line[strlen(line) - 1] = '\0';
451 + if(datatype == protocol)
453 + if(strcmp(line, protoname))
454 + exit_error(OTHER_PROBLEM,
455 + "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
456 + protoname, filename);
458 + if(strlen(line) >= MAX_PROTOCOL_LEN)
459 + exit_error(PARAMETER_PROBLEM,
460 + "Protocol name in %s too long!", filename);
461 + strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
463 + datatype = pattern;
465 + else if(datatype == pattern)
467 + if(strlen(line) >= MAX_PATTERN_LEN)
468 + exit_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
469 + strncpy(info->pattern, line, MAX_PATTERN_LEN);
475 + exit_error(OTHER_PROBLEM, "Internal error");
478 + if(datatype != done)
479 + exit_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
481 + if(line) free(line);
487 + fprintf(stderr, "protocol: %s\npattern: %s\n\n",
493 +static int hex2dec(char c)
500 + return c - 'a' + 10;
502 + return c - 'A' + 10;
504 + exit_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
509 +/* takes a string with \xHH escapes and returns one with the characters
511 +static char * pre_process(char * s)
513 + char * result = malloc(strlen(s) + 1);
514 + int sindex = 0, rindex = 0;
515 + while( sindex < strlen(s) )
517 + if( sindex + 3 < strlen(s) &&
518 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
519 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
521 + /* carefully remember to call tolower here... */
522 + result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
523 + hex2dec(s[sindex + 3] ) );
524 + sindex += 3; /* 4 total */
527 + result[rindex] = tolower(s[sindex]);
532 + result[rindex] = '\0';
537 +#define MAX_SUBDIRS 128
538 +char ** readl7dir(char * dirname)
541 + struct dirent ** namelist;
542 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
547 + n = scandir(dirname, &namelist, 0, alphasort);
552 + exit_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
558 + char fulldirname[MAX_FN_LEN];
560 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
562 + if((scratchdir = opendir(fulldirname)) != NULL)
564 + closedir(scratchdir);
566 + if(!strcmp(namelist[n]->d_name, ".") ||
567 + !strcmp(namelist[n]->d_name, ".."))
571 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
572 + strcpy(subdirs[d], namelist[n]->d_name);
574 + if(d >= MAX_SUBDIRS - 1)
577 + "Too many subdirectories, skipping the rest!\n");
593 +parse_layer7_protocol(const unsigned char *s, struct ipt_layer7_info *info)
595 + char filename[MAX_FN_LEN];
598 + int n = 0, done = 0;
600 + if(strlen(l7dir) > 0)
603 + dir = "/etc/l7-protocols";
605 + subdirs = readl7dir(dir);
607 + while(subdirs[n] != NULL)
609 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
611 + //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
615 + exit_error(OTHER_PROBLEM,
616 + "Filename beginning with %s is too long!\n", filename);
619 + /* read in the pattern from the file */
620 + if(parse_protocol_file(filename, s, info))
622 + //fprintf(stderr, "found\n");
627 + //fprintf(stderr, "not found\n");
633 + exit_error(OTHER_PROBLEM,
634 + "Couldn't find a pattern definition file for %s.\n", s);
636 + /* process \xHH escapes and tolower everything. (our regex lib has no
637 + case insensitivity option.) */
638 + strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
641 +/* Function which parses command options; returns true if it ate an option */
642 +static int parse(int c, char **argv, int invert, unsigned int *flags,
643 + const struct ipt_entry *entry, unsigned int *nfcache,
644 + struct ipt_entry_match **match)
646 + struct ipt_layer7_info *layer7info =
647 + (struct ipt_layer7_info *)(*match)->data;
651 + check_inverse(optarg, &invert, &optind, 0);
652 + parse_layer7_protocol(argv[optind-1], layer7info);
654 + layer7info->invert = 1;
659 + /* not going to use this, but maybe we need to strip a ! anyway (?) */
660 + check_inverse(optarg, &invert, &optind, 0);
662 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
663 + exit_error(PARAMETER_PROBLEM, "directory name too long\n");
665 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
677 +/* Final check; must have specified --pattern. */
678 +static void final_check(unsigned int flags)
681 + exit_error(PARAMETER_PROBLEM,
682 + "LAYER7 match: You must specify `--pattern'");
685 +static void print_protocol(char s[], int invert, int numeric)
687 + fputs("l7proto ", stdout);
688 + if (invert) fputc('!', stdout);
692 +/* Prints out the matchinfo. */
693 +static void print(const struct ipt_ip *ip,
694 + const struct ipt_entry_match *match,
699 + print_protocol(((struct ipt_layer7_info *)match->data)->protocol,
700 + ((struct ipt_layer7_info *)match->data)->invert, numeric);
702 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
703 +static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
705 + const struct ipt_layer7_info *info =
706 + (const struct ipt_layer7_info*) match->data;
708 + printf("--l7proto %s%s ", (info->invert) ? "! ": "", info->protocol);
711 +static struct iptables_match layer7 = {
713 + .version = IPTABLES_VERSION,
714 + .size = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
715 + .userspacesize = IPT_ALIGN(sizeof(struct ipt_layer7_info)),
719 + .final_check = &final_check,
727 + register_match(&layer7);
729 diff -Nurp iptables-1.3.0-stock/extensions/libipt_layer7.man iptables-1.3.0-layer7/extensions/libipt_layer7.man
730 --- iptables-1.3.0-stock/extensions/libipt_layer7.man 1969-12-31 18:00:00.000000000 -0600
731 +++ iptables-1.3.0-layer7/extensions/libipt_layer7.man 2005-03-01 22:12:06.000000000 -0600
733 +This module matches packets based on the application layer data of
734 +their connections. It uses regular expression matching to compare
735 +the application layer data to regular expressions found it the layer7
736 +configuration files. This is an experimental module which can be found at
737 +http://l7-filter.sf.net. It takes two options.
739 +.BI "--l7proto " "\fIprotocol\fP"
740 +Match the specified protocol. The protocol name must match a file
741 +name in /etc/l7-protocols/
743 +.BI "--l7dir " "\fIdirectory\fP"
744 +Use \fIdirectory\fP instead of /etc/l7-protocols/
746 --- iptables-1.3.1/extensions/Makefile.orig 2005-04-01 23:02:50.000000000 +0200
747 +++ iptables-1.3.1/extensions/Makefile 2005-04-01 23:03:06.000000000 +0200
753 +PF_EXT_SLIB += layer7
756 PF_EXT_SLIB_OPTS:=$(foreach T,$(wildcard extensions/.*-test),$(shell KERNEL_DIR=$(KERNEL_DIR) $(T)))