2 +++ b/extensions/libxt_layer7.c
5 + Shared library add-on to iptables for layer 7 matching support.
7 + By Matthew Strait <quadong@users.sf.net>, Oct 2003-Aug 2008.
9 + http://l7-filter.sf.net
11 + This program is free software; you can redistribute it and/or
12 + modify it under the terms of the GNU General Public License
13 + as published by the Free Software Foundation; either version
14 + 2 of the License, or (at your option) any later version.
15 + http://www.gnu.org/licenses/gpl.txt
28 +#include <linux/netfilter/xt_layer7.h>
30 +#define MAX_FN_LEN 256
32 +static char l7dir[MAX_FN_LEN] = "\0";
34 +/* Function which prints out usage message. */
35 +static void help(void)
38 + "layer7 match options:\n"
39 + " --l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
40 + " (--l7dir must be specified before --l7proto if used)\n"
41 + "[!] --l7proto <name>: Match named protocol using /etc/l7-protocols/.../name.pat\n");
44 +static const struct option opts[] = {
45 + { .name = "l7proto", .has_arg = 1, .val = 'p' },
46 + { .name = "l7dir", .has_arg = 1, .val = 'd' },
50 +/* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
51 +static int parse_protocol_file(char * filename, const char * protoname, struct xt_layer7_info *info)
57 + enum { protocol, pattern, done } datatype = protocol;
59 + f = fopen(filename, "r");
64 + while(getline(&line, &len, f) != -1)
66 + if(strlen(line) < 2 || line[0] == '#')
69 + /* strip the pesky newline... */
70 + if(line[strlen(line) - 1] == '\n')
71 + line[strlen(line) - 1] = '\0';
73 + if(datatype == protocol)
75 + /* Ignore everything on the line beginning with the
76 + first space or tab . For instance, this allows the
77 + protocol line in http.pat to be "http " (or
78 + "http I am so cool") instead of just "http". */
79 + if(strchr(line, ' ')){
80 + char * space = strchr(line, ' ');
83 + if(strchr(line, '\t')){
84 + char * space = strchr(line, '\t');
88 + /* sanity check. First non-comment non-blank
89 + line must be the same as the file name. */
90 + if(strcmp(line, protoname))
91 + xtables_error(OTHER_PROBLEM,
92 + "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
95 + if(strlen(line) >= MAX_PROTOCOL_LEN)
96 + xtables_error(PARAMETER_PROBLEM,
97 + "Protocol name in %s too long!", filename);
98 + strncpy(info->protocol, line, MAX_PROTOCOL_LEN);
100 + datatype = pattern;
102 + else if(datatype == pattern)
104 + if(strlen(line) >= MAX_PATTERN_LEN)
105 + xtables_error(PARAMETER_PROBLEM, "Pattern in %s too long!", filename);
106 + strncpy(info->pattern, line, MAX_PATTERN_LEN);
112 + xtables_error(OTHER_PROBLEM, "Internal error");
115 + if(datatype != done)
116 + xtables_error(OTHER_PROBLEM, "Failed to get all needed data from %s", filename);
118 + if(line) free(line);
124 +static int hex2dec(char c)
131 + return c - 'a' + 10;
133 + return c - 'A' + 10;
135 + xtables_error(OTHER_PROBLEM, "hex2dec: bad value!\n");
140 +/* takes a string with \xHH escapes and returns one with the characters
142 +static char * pre_process(char * s)
144 + char * result = malloc(strlen(s) + 1);
145 + int sindex = 0, rrindex = 0;
146 + while( sindex < strlen(s) )
148 + if( sindex + 3 < strlen(s) &&
149 + s[sindex] == '\\' && s[sindex+1] == 'x' &&
150 + isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
152 + /* carefully remember to call tolower here... */
153 + result[rrindex] = tolower( hex2dec(s[sindex + 2])*16 +
154 + hex2dec(s[sindex + 3] ) );
156 + switch ( result[rrindex] )
171 + "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n"
172 + "I recommend that you write this as %c or \\%c, depending on what you meant.\n",
173 + result[rrindex], s[sindex + 2], s[sindex + 3], result[rrindex], result[rrindex]);
177 + "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n");
184 + sindex += 3; /* 4 total */
187 + result[rrindex] = tolower(s[sindex]);
192 + result[rrindex] = '\0';
197 +#define MAX_SUBDIRS 128
198 +static char ** readl7dir(char * dirname)
201 + struct dirent ** namelist;
202 + char ** subdirs = malloc(MAX_SUBDIRS * sizeof(char *));
207 + n = scandir(dirname, &namelist, 0, alphasort);
212 + xtables_error(OTHER_PROBLEM, "Couldn't open %s\n", dirname);
218 + char fulldirname[MAX_FN_LEN];
220 + snprintf(fulldirname, MAX_FN_LEN, "%s/%s", dirname, namelist[n]->d_name);
222 + if((scratchdir = opendir(fulldirname)) != NULL)
224 + closedir(scratchdir);
226 + if(!strcmp(namelist[n]->d_name, ".") ||
227 + !strcmp(namelist[n]->d_name, ".."))
231 + subdirs[d] = malloc(strlen(namelist[n]->d_name) + 1);
232 + strcpy(subdirs[d], namelist[n]->d_name);
234 + if(d >= MAX_SUBDIRS - 1)
237 + "Too many subdirectories, skipping the rest!\n");
252 +static void parse_layer7_protocol(const char *s, struct xt_layer7_info *info)
254 + char filename[MAX_FN_LEN];
257 + int n = 0, done = 0;
259 + if(strlen(l7dir) > 0) dir = l7dir;
260 + else dir = "/etc/l7-protocols";
262 + subdirs = readl7dir(dir);
264 + while(subdirs[n] != NULL)
266 + int c = snprintf(filename, MAX_FN_LEN, "%s/%s/%s.pat", dir, subdirs[n], s);
269 + xtables_error(OTHER_PROBLEM,
270 + "Filename beginning with %s is too long!\n", filename);
272 + /* read in the pattern from the file */
273 + if(parse_protocol_file(filename, s, info)){
282 + xtables_error(OTHER_PROBLEM,
283 + "Couldn't find a pattern definition file for %s.\n", s);
285 + /* process \xHH escapes and tolower everything. (our regex lib has no
286 + case insensitivity option.) */
287 + strncpy(info->pattern, pre_process(info->pattern), MAX_PATTERN_LEN);
290 +/* Function which parses command options; returns true if it ate an option */
291 +static int parse(int c, char **argv, int invert, unsigned int *flags,
292 + const void *entry, struct xt_entry_match **match)
294 + struct xt_layer7_info *layer7info =
295 + (struct xt_layer7_info *)(*match)->data;
299 + parse_layer7_protocol(argv[optind-1], layer7info);
301 + layer7info->invert = true;
306 + if(strlen(argv[optind-1]) >= MAX_FN_LEN)
307 + xtables_error(PARAMETER_PROBLEM, "directory name too long\n");
309 + strncpy(l7dir, argv[optind-1], MAX_FN_LEN);
321 +/* Final check; must have specified --l7proto */
322 +static void final_check(unsigned int flags)
325 + xtables_error(PARAMETER_PROBLEM,
326 + "LAYER7 match: You must specify `--l7proto'");
329 +static void print_protocol(char s[], int invert, int numeric)
331 + fputs("l7proto ", stdout);
332 + if (invert) fputc('!', stdout);
336 +/* Prints out the matchinfo. */
337 +static void print(const void *ip,
338 + const struct xt_entry_match *match,
342 + print_protocol(((struct xt_layer7_info *)match->data)->protocol,
343 + ((struct xt_layer7_info *)match->data)->invert, numeric);
345 +/* Saves the union ipt_matchinfo in parsable form to stdout. */
346 +static void save(const void *ip, const struct xt_entry_match *match)
348 + const struct xt_layer7_info *info =
349 + (const struct xt_layer7_info*) match->data;
351 + printf("--l7proto %s%s ", (info->invert)? "! ":"", info->protocol);
354 +static struct xtables_match layer7 = {
357 + .version = XTABLES_VERSION,
358 + .size = XT_ALIGN(sizeof(struct xt_layer7_info)),
359 + .userspacesize = XT_ALIGN(sizeof(struct xt_layer7_info)),
362 + .final_check = &final_check,
370 + xtables_register_match(&layer7);