2 Shared library add-on to iptables to add layer 7 matching support.
4 By Matthew Strait <quadong@users.sf.net>, Oct 2003.
6 http://l7-filter.sf.net
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version
11 2 of the License, or (at your option) any later version.
12 http://www.gnu.org/licenses/gpl.txt
14 Based on libipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
27 #include <linux/netfilter_ipv4/ipt_layer7.h>
29 #define MAX_FN_LEN 256
31 static char l7dir
[MAX_FN_LEN
] = "\0";
33 /* Function which prints out usage message. */
34 static void help(void)
37 "LAYER7 match v%s options:\n"
38 "--l7dir <directory> : Look for patterns here instead of /etc/l7-protocols/\n"
39 " (--l7dir must be specified before --l7proto if used!)\n"
40 "--l7proto [!] <name> : Match the protocol defined in /etc/l7-protocols/name.pat\n",
45 static struct option opts
[] = {
46 { .name
= "l7proto", .has_arg
= 1, .flag
= 0, .val
= '1' },
47 { .name
= "l7dir", .has_arg
= 1, .flag
= 0, .val
= '2' },
51 /* reads filename, puts protocol info into layer7_protocol_info, number of protocols to numprotos */
52 int parse_protocol_file(char * filename
, const unsigned char * protoname
, struct ipt_layer7_info
*info
)
58 enum { protocol
, pattern
, done
} datatype
= protocol
;
60 f
= fopen(filename
, "r");
65 while(getline(&line
, &len
, f
) != -1)
67 if(strlen(line
) < 2 || line
[0] == '#')
70 /* strip the pesky newline... */
71 if(line
[strlen(line
) - 1] == '\n')
72 line
[strlen(line
) - 1] = '\0';
74 if(datatype
== protocol
)
76 /* Ignore everything on the line beginning with the
77 first space or tab . For instance, this allows the
78 protocol line in http.pat to be "http " (or
79 "http I am so cool") instead of just "http". */
80 if(strchr(line
, ' ')){
81 char * space
= strchr(line
, ' ');
84 if(strchr(line
, '\t')){
85 char * space
= strchr(line
, '\t');
89 /* sanity check. First non-comment non-blank
90 line must be the same as the file name. */
91 if(strcmp(line
, protoname
))
92 exit_error(OTHER_PROBLEM
,
93 "Protocol name (%s) doesn't match file name (%s). Bailing out\n",
96 if(strlen(line
) >= MAX_PROTOCOL_LEN
)
97 exit_error(PARAMETER_PROBLEM
,
98 "Protocol name in %s too long!", filename
);
99 strncpy(info
->protocol
, line
, MAX_PROTOCOL_LEN
);
103 else if(datatype
== pattern
)
105 if(strlen(line
) >= MAX_PATTERN_LEN
)
106 exit_error(PARAMETER_PROBLEM
, "Pattern in %s too long!", filename
);
107 strncpy(info
->pattern
, line
, MAX_PATTERN_LEN
);
113 exit_error(OTHER_PROBLEM
, "Internal error");
117 exit_error(OTHER_PROBLEM
, "Failed to get all needed data from %s", filename
);
125 fprintf(stderr, "protocol: %s\npattern: %s\n\n",
131 static int hex2dec(char c
)
142 exit_error(OTHER_PROBLEM
, "hex2dec: bad value!\n");
147 /* takes a string with \xHH escapes and returns one with the characters
149 static char * pre_process(char * s
)
151 char * result
= malloc(strlen(s
) + 1);
152 int sindex
= 0, rindex
= 0;
153 while( sindex
< strlen(s
) )
155 if( sindex
+ 3 < strlen(s
) &&
156 s
[sindex
] == '\\' && s
[sindex
+1] == 'x' &&
157 isxdigit(s
[sindex
+ 2]) && isxdigit(s
[sindex
+ 3]) )
159 /* carefully remember to call tolower here... */
160 result
[rindex
] = tolower( hex2dec(s
[sindex
+ 2])*16 +
161 hex2dec(s
[sindex
+ 3] ) );
163 switch ( result
[rindex
] )
177 "Warning: layer7 regexp contains a control character, %c, in hex (\\x%c%c).\n"
178 "I recommend that you write this as %c or \\%c, depending on what you meant.\n",
179 result
[rindex
], s
[sindex
+ 2], s
[sindex
+ 3], result
[rindex
], result
[rindex
]);
183 "Warning: null (\\x00) in layer7 regexp. A null terminates the regexp string!\n");
190 sindex
+= 3; /* 4 total */
193 result
[rindex
] = tolower(s
[sindex
]);
198 result
[rindex
] = '\0';
203 #define MAX_SUBDIRS 128
204 char ** readl7dir(char * dirname
)
207 struct dirent
** namelist
;
208 char ** subdirs
= malloc(MAX_SUBDIRS
* sizeof(char *));
213 n
= scandir(dirname
, &namelist
, 0, alphasort
);
218 exit_error(OTHER_PROBLEM
, "Couldn't open %s\n", dirname
);
224 char fulldirname
[MAX_FN_LEN
];
226 snprintf(fulldirname
, MAX_FN_LEN
, "%s/%s", dirname
, namelist
[n
]->d_name
);
228 if((scratchdir
= opendir(fulldirname
)) != NULL
)
230 closedir(scratchdir
);
232 if(!strcmp(namelist
[n
]->d_name
, ".") ||
233 !strcmp(namelist
[n
]->d_name
, ".."))
237 subdirs
[d
] = malloc(strlen(namelist
[n
]->d_name
) + 1);
238 strcpy(subdirs
[d
], namelist
[n
]->d_name
);
240 if(d
>= MAX_SUBDIRS
- 1)
243 "Too many subdirectories, skipping the rest!\n");
259 parse_layer7_protocol(const unsigned char *s
, struct ipt_layer7_info
*info
)
261 char filename
[MAX_FN_LEN
];
266 if(strlen(l7dir
) > 0)
269 dir
= "/etc/l7-protocols";
271 subdirs
= readl7dir(dir
);
273 while(subdirs
[n
] != NULL
)
275 int c
= snprintf(filename
, MAX_FN_LEN
, "%s/%s/%s.pat", dir
, subdirs
[n
], s
);
277 //fprintf(stderr, "Trying to find pattern in %s ... ", filename);
281 exit_error(OTHER_PROBLEM
,
282 "Filename beginning with %s is too long!\n", filename
);
285 /* read in the pattern from the file */
286 if(parse_protocol_file(filename
, s
, info
))
288 //fprintf(stderr, "found\n");
293 //fprintf(stderr, "not found\n");
299 exit_error(OTHER_PROBLEM
,
300 "Couldn't find a pattern definition file for %s.\n", s
);
302 /* process \xHH escapes and tolower everything. (our regex lib has no
303 case insensitivity option.) */
304 strncpy(info
->pattern
, pre_process(info
->pattern
), MAX_PATTERN_LEN
);
307 /* Function which parses command options; returns true if it ate an option */
308 static int parse(int c
, char **argv
, int invert
, unsigned int *flags
,
309 const struct ipt_entry
*entry
, unsigned int *nfcache
,
310 struct ipt_entry_match
**match
)
312 struct ipt_layer7_info
*layer7info
=
313 (struct ipt_layer7_info
*)(*match
)->data
;
317 check_inverse(optarg
, &invert
, &optind
, 0);
318 parse_layer7_protocol(argv
[optind
-1], layer7info
);
320 layer7info
->invert
= 1;
325 /* not going to use this, but maybe we need to strip a ! anyway (?) */
326 check_inverse(optarg
, &invert
, &optind
, 0);
328 if(strlen(argv
[optind
-1]) >= MAX_FN_LEN
)
329 exit_error(PARAMETER_PROBLEM
, "directory name too long\n");
331 strncpy(l7dir
, argv
[optind
-1], MAX_FN_LEN
);
343 /* Final check; must have specified --l7proto */
344 static void final_check(unsigned int flags
)
347 exit_error(PARAMETER_PROBLEM
,
348 "LAYER7 match: You must specify `--l7proto'");
351 static void print_protocol(char s
[], int invert
, int numeric
)
353 fputs("l7proto ", stdout
);
354 if (invert
) fputc('!', stdout
);
358 /* Prints out the matchinfo. */
359 static void print(const struct ipt_ip
*ip
,
360 const struct ipt_entry_match
*match
,
365 print_protocol(((struct ipt_layer7_info
*)match
->data
)->protocol
,
366 ((struct ipt_layer7_info
*)match
->data
)->invert
, numeric
);
368 /* Saves the union ipt_matchinfo in parsable form to stdout. */
369 static void save(const struct ipt_ip
*ip
, const struct ipt_entry_match
*match
)
371 const struct ipt_layer7_info
*info
=
372 (const struct ipt_layer7_info
*) match
->data
;
374 printf("--l7proto %s%s ", (info
->invert
) ? "! ": "", info
->protocol
);
377 static struct iptables_match layer7
= {
379 .version
= IPTABLES_VERSION
,
380 .size
= IPT_ALIGN(sizeof(struct ipt_layer7_info
)),
381 .userspacesize
= IPT_ALIGN(sizeof(struct ipt_layer7_info
)),
384 .final_check
= &final_check
,
392 register_match(&layer7
);