]>
Commit | Line | Data |
---|---|---|
9547f064 | 1 | --- linux-2.6.25/net/netfilter/Kconfig 2008-04-16 21:49:44.000000000 -0500 |
2 | +++ linux-2.6.25-layer7/net/netfilter/Kconfig 2008-04-29 00:40:01.000000000 -0500 | |
3 | @@ -735,6 +735,27 @@ config NETFILTER_XT_MATCH_STATE | |
97491a6e | 4 | |
5 | To compile it as a module, choose M here. If unsure, say N. | |
6 | ||
a06ee22f | 7 | +config NETFILTER_XT_MATCH_LAYER7 |
8 | + tristate '"layer7" match support' | |
9 | + depends on NETFILTER_XTABLES | |
10 | + depends on EXPERIMENTAL && (IP_NF_CONNTRACK || NF_CONNTRACK) | |
9547f064 | 11 | + depends on NF_CT_ACCT |
97491a6e | 12 | + help |
13 | + Say Y if you want to be able to classify connections (and their | |
14 | + packets) based on regular expression matching of their application | |
15 | + layer data. This is one way to classify applications such as | |
16 | + peer-to-peer filesharing systems that do not always use the same | |
17 | + port. | |
18 | + | |
19 | + To compile it as a module, choose M here. If unsure, say N. | |
20 | + | |
a06ee22f | 21 | +config NETFILTER_XT_MATCH_LAYER7_DEBUG |
22 | + bool 'Layer 7 debugging output' | |
23 | + depends on NETFILTER_XT_MATCH_LAYER7 | |
24 | + help | |
25 | + Say Y to get lots of debugging output. | |
26 | + | |
27 | + | |
28 | config NETFILTER_XT_MATCH_STATISTIC | |
29 | tristate '"statistic" match support' | |
30 | depends on NETFILTER_XTABLES | |
9547f064 | 31 | --- linux-2.6.25/net/netfilter/Makefile 2008-04-16 21:49:44.000000000 -0500 |
32 | +++ linux-2.6.25-layer7/net/netfilter/Makefile 2008-04-29 00:40:01.000000000 -0500 | |
33 | @@ -77,6 +77,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_RATEEST) | |
34 | obj-$(CONFIG_NETFILTER_XT_MATCH_REALM) += xt_realm.o | |
35 | obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o | |
36 | obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o | |
a06ee22f | 37 | +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o |
9547f064 | 38 | obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o |
39 | obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o | |
40 | obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o | |
41 | --- linux-2.6.25/net/netfilter/xt_layer7.c 1969-12-31 18:00:00.000000000 -0600 | |
42 | +++ linux-2.6.25-layer7/net/netfilter/xt_layer7.c 2008-04-29 00:40:01.000000000 -0500 | |
43 | @@ -0,0 +1,634 @@ | |
97491a6e | 44 | +/* |
a06ee22f | 45 | + Kernel module to match application layer (OSI layer 7) data in connections. |
97491a6e | 46 | + |
47 | + http://l7-filter.sf.net | |
48 | + | |
a06ee22f | 49 | + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer. |
97491a6e | 50 | + |
51 | + This program is free software; you can redistribute it and/or | |
52 | + modify it under the terms of the GNU General Public License | |
53 | + as published by the Free Software Foundation; either version | |
54 | + 2 of the License, or (at your option) any later version. | |
55 | + http://www.gnu.org/licenses/gpl.txt | |
56 | + | |
a06ee22f | 57 | + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>, |
58 | + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait, | |
59 | + Ethan Sommer, Justin Levandoski. | |
97491a6e | 60 | +*/ |
61 | + | |
a06ee22f | 62 | +#include <linux/spinlock.h> |
9547f064 | 63 | +#include <linux/version.h> |
a06ee22f | 64 | +#include <net/ip.h> |
65 | +#include <net/tcp.h> | |
97491a6e | 66 | +#include <linux/module.h> |
67 | +#include <linux/skbuff.h> | |
a06ee22f | 68 | +#include <linux/netfilter.h> |
6890440f | 69 | +#include <net/netfilter/nf_conntrack.h> |
70 | +#include <net/netfilter/nf_conntrack_core.h> | |
a06ee22f | 71 | +#include <linux/netfilter/x_tables.h> |
72 | +#include <linux/netfilter/xt_layer7.h> | |
97491a6e | 73 | +#include <linux/ctype.h> |
a06ee22f | 74 | +#include <linux/proc_fs.h> |
97491a6e | 75 | + |
76 | +#include "regexp/regexp.c" | |
77 | + | |
97491a6e | 78 | +MODULE_LICENSE("GPL"); |
a06ee22f | 79 | +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>"); |
97491a6e | 80 | +MODULE_DESCRIPTION("iptables application layer match module"); |
a06ee22f | 81 | +MODULE_ALIAS("ipt_layer7"); |
9547f064 | 82 | +MODULE_VERSION("2.17"); |
97491a6e | 83 | + |
84 | +static int maxdatalen = 2048; // this is the default | |
85 | +module_param(maxdatalen, int, 0444); | |
86 | +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter"); | |
a06ee22f | 87 | +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG |
97491a6e | 88 | + #define DPRINTK(format,args...) printk(format,##args) |
89 | +#else | |
90 | + #define DPRINTK(format,args...) | |
91 | +#endif | |
92 | + | |
93 | +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \ | |
94 | + master_conntrack->counters[IP_CT_DIR_REPLY].packets | |
95 | + | |
96 | +/* Number of packets whose data we look at. | |
97 | +This can be modified through /proc/net/layer7_numpackets */ | |
98 | +static int num_packets = 10; | |
99 | + | |
100 | +static struct pattern_cache { | |
101 | + char * regex_string; | |
102 | + regexp * pattern; | |
103 | + struct pattern_cache * next; | |
104 | +} * first_pattern_cache = NULL; | |
105 | + | |
a06ee22f | 106 | +DEFINE_SPINLOCK(l7_lock); |
97491a6e | 107 | + |
108 | +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG | |
109 | +/* Converts an unfriendly string into a friendly one by | |
110 | +replacing unprintables with periods and all whitespace with " ". */ | |
111 | +static char * friendly_print(unsigned char * s) | |
112 | +{ | |
113 | + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC); | |
114 | + int i; | |
115 | + | |
116 | + if(!f) { | |
117 | + if (net_ratelimit()) | |
a06ee22f | 118 | + printk(KERN_ERR "layer7: out of memory in " |
119 | + "friendly_print, bailing.\n"); | |
97491a6e | 120 | + return NULL; |
121 | + } | |
122 | + | |
123 | + for(i = 0; i < strlen(s); i++){ | |
124 | + if(isprint(s[i]) && s[i] < 128) f[i] = s[i]; | |
125 | + else if(isspace(s[i])) f[i] = ' '; | |
126 | + else f[i] = '.'; | |
127 | + } | |
128 | + f[i] = '\0'; | |
129 | + return f; | |
130 | +} | |
131 | + | |
132 | +static char dec2hex(int i) | |
133 | +{ | |
134 | + switch (i) { | |
135 | + case 0 ... 9: | |
a06ee22f | 136 | + return (i + '0'); |
97491a6e | 137 | + break; |
138 | + case 10 ... 15: | |
a06ee22f | 139 | + return (i - 10 + 'a'); |
97491a6e | 140 | + break; |
141 | + default: | |
142 | + if (net_ratelimit()) | |
a06ee22f | 143 | + printk("layer7: Problem in dec2hex\n"); |
97491a6e | 144 | + return '\0'; |
145 | + } | |
146 | +} | |
147 | + | |
148 | +static char * hex_print(unsigned char * s) | |
149 | +{ | |
150 | + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC); | |
151 | + int i; | |
152 | + | |
153 | + if(!g) { | |
154 | + if (net_ratelimit()) | |
a06ee22f | 155 | + printk(KERN_ERR "layer7: out of memory in hex_print, " |
156 | + "bailing.\n"); | |
97491a6e | 157 | + return NULL; |
158 | + } | |
159 | + | |
160 | + for(i = 0; i < strlen(s); i++) { | |
161 | + g[i*3 ] = dec2hex(s[i]/16); | |
162 | + g[i*3 + 1] = dec2hex(s[i]%16); | |
163 | + g[i*3 + 2] = ' '; | |
164 | + } | |
165 | + g[i*3] = '\0'; | |
166 | + | |
167 | + return g; | |
168 | +} | |
169 | +#endif // DEBUG | |
170 | + | |
171 | +/* Use instead of regcomp. As we expect to be seeing the same regexps over and | |
172 | +over again, it make sense to cache the results. */ | |
a06ee22f | 173 | +static regexp * compile_and_cache(const char * regex_string, |
174 | + const char * protocol) | |
97491a6e | 175 | +{ |
176 | + struct pattern_cache * node = first_pattern_cache; | |
177 | + struct pattern_cache * last_pattern_cache = first_pattern_cache; | |
178 | + struct pattern_cache * tmp; | |
179 | + unsigned int len; | |
180 | + | |
181 | + while (node != NULL) { | |
182 | + if (!strcmp(node->regex_string, regex_string)) | |
183 | + return node->pattern; | |
184 | + | |
185 | + last_pattern_cache = node;/* points at the last non-NULL node */ | |
186 | + node = node->next; | |
187 | + } | |
188 | + | |
189 | + /* If we reach the end of the list, then we have not yet cached | |
190 | + the pattern for this regex. Let's do that now. | |
191 | + Be paranoid about running out of memory to avoid list corruption. */ | |
192 | + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC); | |
193 | + | |
194 | + if(!tmp) { | |
195 | + if (net_ratelimit()) | |
a06ee22f | 196 | + printk(KERN_ERR "layer7: out of memory in " |
197 | + "compile_and_cache, bailing.\n"); | |
97491a6e | 198 | + return NULL; |
199 | + } | |
200 | + | |
201 | + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC); | |
202 | + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC); | |
203 | + tmp->next = NULL; | |
204 | + | |
205 | + if(!tmp->regex_string || !tmp->pattern) { | |
206 | + if (net_ratelimit()) | |
a06ee22f | 207 | + printk(KERN_ERR "layer7: out of memory in " |
208 | + "compile_and_cache, bailing.\n"); | |
97491a6e | 209 | + kfree(tmp->regex_string); |
210 | + kfree(tmp->pattern); | |
211 | + kfree(tmp); | |
212 | + return NULL; | |
213 | + } | |
214 | + | |
215 | + /* Ok. The new node is all ready now. */ | |
216 | + node = tmp; | |
217 | + | |
218 | + if(first_pattern_cache == NULL) /* list is empty */ | |
219 | + first_pattern_cache = node; /* make node the beginning */ | |
220 | + else | |
221 | + last_pattern_cache->next = node; /* attach node to the end */ | |
222 | + | |
223 | + /* copy the string and compile the regex */ | |
224 | + len = strlen(regex_string); | |
225 | + DPRINTK("About to compile this: \"%s\"\n", regex_string); | |
a06ee22f | 226 | + node->pattern = regcomp((char *)regex_string, &len); |
97491a6e | 227 | + if ( !node->pattern ) { |
228 | + if (net_ratelimit()) | |
a06ee22f | 229 | + printk(KERN_ERR "layer7: Error compiling regexp " |
230 | + "\"%s\" (%s)\n", | |
231 | + regex_string, protocol); | |
97491a6e | 232 | + /* pattern is now cached as NULL, so we won't try again. */ |
233 | + } | |
234 | + | |
235 | + strcpy(node->regex_string, regex_string); | |
236 | + return node->pattern; | |
237 | +} | |
238 | + | |
239 | +static int can_handle(const struct sk_buff *skb) | |
240 | +{ | |
7bcd2589 | 241 | + if(!ip_hdr(skb)) /* not IP */ |
97491a6e | 242 | + return 0; |
7bcd2589 JR |
243 | + if(ip_hdr(skb)->protocol != IPPROTO_TCP && |
244 | + ip_hdr(skb)->protocol != IPPROTO_UDP && | |
245 | + ip_hdr(skb)->protocol != IPPROTO_ICMP) | |
97491a6e | 246 | + return 0; |
247 | + return 1; | |
248 | +} | |
249 | + | |
250 | +/* Returns offset the into the skb->data that the application data starts */ | |
251 | +static int app_data_offset(const struct sk_buff *skb) | |
252 | +{ | |
7bcd2589 | 253 | + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb) |
97491a6e | 254 | + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */ |
7bcd2589 | 255 | + int ip_hl = 4*ip_hdr(skb)->ihl; |
97491a6e | 256 | + |
7bcd2589 | 257 | + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) { |
97491a6e | 258 | + /* 12 == offset into TCP header for the header length field. |
259 | + Can't get this with skb->h.th->doff because the tcphdr | |
260 | + struct doesn't get set when routing (this is confirmed to be | |
261 | + true in Netfilter as well as QoS.) */ | |
262 | + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4); | |
263 | + | |
264 | + return ip_hl + tcp_hl; | |
7bcd2589 | 265 | + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) { |
97491a6e | 266 | + return ip_hl + 8; /* UDP header is always 8 bytes */ |
7bcd2589 | 267 | + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) { |
97491a6e | 268 | + return ip_hl + 8; /* ICMP header is 8 bytes */ |
269 | + } else { | |
270 | + if (net_ratelimit()) | |
a06ee22f | 271 | + printk(KERN_ERR "layer7: tried to handle unknown " |
272 | + "protocol!\n"); | |
97491a6e | 273 | + return ip_hl + 8; /* something reasonable */ |
274 | + } | |
275 | +} | |
276 | + | |
277 | +/* handles whether there's a match when we aren't appending data anymore */ | |
a06ee22f | 278 | +static int match_no_append(struct nf_conn * conntrack, |
279 | + struct nf_conn * master_conntrack, | |
280 | + enum ip_conntrack_info ctinfo, | |
281 | + enum ip_conntrack_info master_ctinfo, | |
282 | + const struct xt_layer7_info * info) | |
97491a6e | 283 | +{ |
284 | + /* If we're in here, throw the app data away */ | |
97491a6e | 285 | + if(master_conntrack->layer7.app_data != NULL) { |
286 | + | |
287 | + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG | |
288 | + if(!master_conntrack->layer7.app_proto) { | |
a06ee22f | 289 | + char * f = |
290 | + friendly_print(master_conntrack->layer7.app_data); | |
291 | + char * g = | |
292 | + hex_print(master_conntrack->layer7.app_data); | |
293 | + DPRINTK("\nl7-filter gave up after %d bytes " | |
294 | + "(%d packets):\n%s\n", | |
97491a6e | 295 | + strlen(f), TOTAL_PACKETS, f); |
296 | + kfree(f); | |
297 | + DPRINTK("In hex: %s\n", g); | |
298 | + kfree(g); | |
299 | + } | |
300 | + #endif | |
301 | + | |
302 | + kfree(master_conntrack->layer7.app_data); | |
303 | + master_conntrack->layer7.app_data = NULL; /* don't free again */ | |
304 | + } | |
97491a6e | 305 | + |
306 | + if(master_conntrack->layer7.app_proto){ | |
a06ee22f | 307 | + /* Here child connections set their .app_proto (for /proc) */ |
97491a6e | 308 | + if(!conntrack->layer7.app_proto) { |
a06ee22f | 309 | + conntrack->layer7.app_proto = |
310 | + kmalloc(strlen(master_conntrack->layer7.app_proto)+1, | |
311 | + GFP_ATOMIC); | |
97491a6e | 312 | + if(!conntrack->layer7.app_proto){ |
313 | + if (net_ratelimit()) | |
a06ee22f | 314 | + printk(KERN_ERR "layer7: out of memory " |
315 | + "in match_no_append, " | |
316 | + "bailing.\n"); | |
97491a6e | 317 | + return 1; |
318 | + } | |
a06ee22f | 319 | + strcpy(conntrack->layer7.app_proto, |
320 | + master_conntrack->layer7.app_proto); | |
97491a6e | 321 | + } |
97491a6e | 322 | + |
a06ee22f | 323 | + return (!strcmp(master_conntrack->layer7.app_proto, |
324 | + info->protocol)); | |
97491a6e | 325 | + } |
326 | + else { | |
327 | + /* If not classified, set to "unknown" to distinguish from | |
328 | + connections that are still being tested. */ | |
a06ee22f | 329 | + master_conntrack->layer7.app_proto = |
330 | + kmalloc(strlen("unknown")+1, GFP_ATOMIC); | |
97491a6e | 331 | + if(!master_conntrack->layer7.app_proto){ |
332 | + if (net_ratelimit()) | |
a06ee22f | 333 | + printk(KERN_ERR "layer7: out of memory in " |
334 | + "match_no_append, bailing.\n"); | |
97491a6e | 335 | + return 1; |
336 | + } | |
337 | + strcpy(master_conntrack->layer7.app_proto, "unknown"); | |
97491a6e | 338 | + return 0; |
339 | + } | |
340 | +} | |
341 | + | |
342 | +/* add the new app data to the conntrack. Return number of bytes added. */ | |
6890440f | 343 | +static int add_data(struct nf_conn * master_conntrack, |
a06ee22f | 344 | + char * app_data, int appdatalen) |
97491a6e | 345 | +{ |
346 | + int length = 0, i; | |
347 | + int oldlength = master_conntrack->layer7.app_data_len; | |
348 | + | |
a06ee22f | 349 | + /* This is a fix for a race condition by Deti Fliegl. However, I'm not |
350 | + clear on whether the race condition exists or whether this really | |
351 | + fixes it. I might just be being dense... Anyway, if it's not really | |
352 | + a fix, all it does is waste a very small amount of time. */ | |
97491a6e | 353 | + if(!master_conntrack->layer7.app_data) return 0; |
354 | + | |
355 | + /* Strip nulls. Make everything lower case (our regex lib doesn't | |
356 | + do case insensitivity). Add it to the end of the current data. */ | |
357 | + for(i = 0; i < maxdatalen-oldlength-1 && | |
358 | + i < appdatalen; i++) { | |
359 | + if(app_data[i] != '\0') { | |
a06ee22f | 360 | + /* the kernel version of tolower mungs 'upper ascii' */ |
97491a6e | 361 | + master_conntrack->layer7.app_data[length+oldlength] = |
a06ee22f | 362 | + isascii(app_data[i])? |
363 | + tolower(app_data[i]) : app_data[i]; | |
97491a6e | 364 | + length++; |
365 | + } | |
366 | + } | |
367 | + | |
368 | + master_conntrack->layer7.app_data[length+oldlength] = '\0'; | |
369 | + master_conntrack->layer7.app_data_len = length + oldlength; | |
370 | + | |
371 | + return length; | |
372 | +} | |
373 | + | |
a06ee22f | 374 | +/* taken from drivers/video/modedb.c */ |
375 | +static int my_atoi(const char *s) | |
97491a6e | 376 | +{ |
a06ee22f | 377 | + int val = 0; |
378 | + | |
379 | + for (;; s++) { | |
380 | + switch (*s) { | |
381 | + case '0'...'9': | |
382 | + val = 10*val+(*s-'0'); | |
383 | + break; | |
384 | + default: | |
385 | + return val; | |
386 | + } | |
387 | + } | |
388 | +} | |
389 | + | |
390 | +/* write out num_packets to userland. */ | |
391 | +static int layer7_read_proc(char* page, char ** start, off_t off, int count, | |
392 | + int* eof, void * data) | |
393 | +{ | |
394 | + if(num_packets > 99 && net_ratelimit()) | |
395 | + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n"); | |
396 | + | |
397 | + page[0] = num_packets/10 + '0'; | |
398 | + page[1] = num_packets%10 + '0'; | |
399 | + page[2] = '\n'; | |
400 | + page[3] = '\0'; | |
401 | + | |
402 | + *eof=1; | |
403 | + | |
404 | + return 3; | |
405 | +} | |
406 | + | |
407 | +/* Read in num_packets from userland */ | |
408 | +static int layer7_write_proc(struct file* file, const char* buffer, | |
409 | + unsigned long count, void *data) | |
410 | +{ | |
411 | + char * foo = kmalloc(count, GFP_ATOMIC); | |
412 | + | |
413 | + if(!foo){ | |
414 | + if (net_ratelimit()) | |
415 | + printk(KERN_ERR "layer7: out of memory, bailing. " | |
416 | + "num_packets unchanged.\n"); | |
417 | + return count; | |
418 | + } | |
419 | + | |
420 | + if(copy_from_user(foo, buffer, count)) { | |
421 | + return -EFAULT; | |
422 | + } | |
423 | + | |
424 | + | |
425 | + num_packets = my_atoi(foo); | |
426 | + kfree (foo); | |
427 | + | |
428 | + /* This has an arbitrary limit to make the math easier. I'm lazy. | |
429 | + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */ | |
430 | + if(num_packets > 99) { | |
431 | + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n"); | |
432 | + num_packets = 99; | |
433 | + } else if(num_packets < 1) { | |
434 | + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n"); | |
435 | + num_packets = 1; | |
436 | + } | |
437 | + | |
438 | + return count; | |
439 | +} | |
440 | + | |
441 | +static int | |
442 | +match(const struct sk_buff *skbin, | |
443 | + const struct net_device *in, | |
444 | + const struct net_device *out, | |
445 | + const struct xt_match *match, | |
446 | + const void *matchinfo, | |
447 | + int offset, | |
448 | + unsigned int protoff, | |
449 | + int *hotdrop) | |
450 | +{ | |
451 | + /* sidestep const without getting a compiler warning... */ | |
452 | + struct sk_buff * skb = (struct sk_buff *)skbin; | |
453 | + | |
454 | + const struct xt_layer7_info * info = matchinfo; | |
97491a6e | 455 | + enum ip_conntrack_info master_ctinfo, ctinfo; |
6890440f | 456 | + struct nf_conn *master_conntrack, *conntrack; |
97491a6e | 457 | + unsigned char * app_data; |
458 | + unsigned int pattern_result, appdatalen; | |
459 | + regexp * comppattern; | |
a06ee22f | 460 | + |
461 | + /* Be paranoid/incompetent - lock the entire match function. */ | |
462 | + spin_lock_bh(&l7_lock); | |
97491a6e | 463 | + |
464 | + if(!can_handle(skb)){ | |
465 | + DPRINTK("layer7: This is some protocol I can't handle.\n"); | |
a06ee22f | 466 | + spin_unlock_bh(&l7_lock); |
97491a6e | 467 | + return info->invert; |
468 | + } | |
469 | + | |
470 | + /* Treat parent & all its children together as one connection, except | |
471 | + for the purpose of setting conntrack->layer7.app_proto in the actual | |
472 | + connection. This makes /proc/net/ip_conntrack more satisfying. */ | |
a06ee22f | 473 | + if(!(conntrack = nf_ct_get(skb, &ctinfo)) || |
474 | + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){ | |
475 | + DPRINTK("layer7: couldn't get conntrack.\n"); | |
476 | + spin_unlock_bh(&l7_lock); | |
97491a6e | 477 | + return info->invert; |
478 | + } | |
479 | + | |
480 | + /* Try to get a master conntrack (and its master etc) for FTP, etc. */ | |
481 | + while (master_ct(master_conntrack) != NULL) | |
482 | + master_conntrack = master_ct(master_conntrack); | |
483 | + | |
484 | + /* if we've classified it or seen too many packets */ | |
485 | + if(TOTAL_PACKETS > num_packets || | |
486 | + master_conntrack->layer7.app_proto) { | |
487 | + | |
a06ee22f | 488 | + pattern_result = match_no_append(conntrack, master_conntrack, |
489 | + ctinfo, master_ctinfo, info); | |
97491a6e | 490 | + |
a06ee22f | 491 | + /* skb->cb[0] == seen. Don't do things twice if there are |
492 | + multiple l7 rules. I'm not sure that using cb for this purpose | |
493 | + is correct, even though it says "put your private variables | |
494 | + there". But it doesn't look like it is being used for anything | |
495 | + else in the skbs that make it here. */ | |
496 | + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */ | |
97491a6e | 497 | + |
a06ee22f | 498 | + spin_unlock_bh(&l7_lock); |
97491a6e | 499 | + return (pattern_result ^ info->invert); |
500 | + } | |
501 | + | |
502 | + if(skb_is_nonlinear(skb)){ | |
503 | + if(skb_linearize(skb) != 0){ | |
504 | + if (net_ratelimit()) | |
a06ee22f | 505 | + printk(KERN_ERR "layer7: failed to linearize " |
506 | + "packet, bailing.\n"); | |
507 | + spin_unlock_bh(&l7_lock); | |
97491a6e | 508 | + return info->invert; |
509 | + } | |
510 | + } | |
511 | + | |
512 | + /* now that the skb is linearized, it's safe to set these. */ | |
513 | + app_data = skb->data + app_data_offset(skb); | |
2795c7e2 | 514 | + appdatalen = skb_tail_pointer(skb) - app_data; |
97491a6e | 515 | + |
97491a6e | 516 | + /* the return value gets checked later, when we're ready to use it */ |
517 | + comppattern = compile_and_cache(info->pattern, info->protocol); | |
97491a6e | 518 | + |
519 | + /* On the first packet of a connection, allocate space for app data */ | |
a06ee22f | 520 | + if(TOTAL_PACKETS == 1 && !skb->cb[0] && |
521 | + !master_conntrack->layer7.app_data){ | |
522 | + master_conntrack->layer7.app_data = | |
523 | + kmalloc(maxdatalen, GFP_ATOMIC); | |
97491a6e | 524 | + if(!master_conntrack->layer7.app_data){ |
525 | + if (net_ratelimit()) | |
a06ee22f | 526 | + printk(KERN_ERR "layer7: out of memory in " |
527 | + "match, bailing.\n"); | |
528 | + spin_unlock_bh(&l7_lock); | |
97491a6e | 529 | + return info->invert; |
530 | + } | |
531 | + | |
532 | + master_conntrack->layer7.app_data[0] = '\0'; | |
533 | + } | |
97491a6e | 534 | + |
535 | + /* Can be here, but unallocated, if numpackets is increased near | |
536 | + the beginning of a connection */ | |
a06ee22f | 537 | + if(master_conntrack->layer7.app_data == NULL){ |
538 | + spin_unlock_bh(&l7_lock); | |
97491a6e | 539 | + return (info->invert); /* unmatched */ |
a06ee22f | 540 | + } |
97491a6e | 541 | + |
542 | + if(!skb->cb[0]){ | |
543 | + int newbytes; | |
97491a6e | 544 | + newbytes = add_data(master_conntrack, app_data, appdatalen); |
97491a6e | 545 | + |
546 | + if(newbytes == 0) { /* didn't add any data */ | |
547 | + skb->cb[0] = 1; | |
548 | + /* Didn't match before, not going to match now */ | |
a06ee22f | 549 | + spin_unlock_bh(&l7_lock); |
97491a6e | 550 | + return info->invert; |
551 | + } | |
552 | + } | |
553 | + | |
554 | + /* If looking for "unknown", then never match. "Unknown" means that | |
555 | + we've given up; we're still trying with these packets. */ | |
97491a6e | 556 | + if(!strcmp(info->protocol, "unknown")) { |
557 | + pattern_result = 0; | |
a06ee22f | 558 | + /* If looking for "unset", then always match. "Unset" means that we |
559 | + haven't yet classified the connection. */ | |
560 | + } else if(!strcmp(info->protocol, "unset")) { | |
561 | + pattern_result = 2; | |
562 | + DPRINTK("layer7: matched unset: not yet classified " | |
563 | + "(%d/%d packets)\n", TOTAL_PACKETS, num_packets); | |
97491a6e | 564 | + /* If the regexp failed to compile, don't bother running it */ |
a06ee22f | 565 | + } else if(comppattern && |
566 | + regexec(comppattern, master_conntrack->layer7.app_data)){ | |
97491a6e | 567 | + DPRINTK("layer7: matched %s\n", info->protocol); |
568 | + pattern_result = 1; | |
569 | + } else pattern_result = 0; | |
97491a6e | 570 | + |
a06ee22f | 571 | + if(pattern_result == 1) { |
572 | + master_conntrack->layer7.app_proto = | |
573 | + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC); | |
97491a6e | 574 | + if(!master_conntrack->layer7.app_proto){ |
575 | + if (net_ratelimit()) | |
a06ee22f | 576 | + printk(KERN_ERR "layer7: out of memory in " |
577 | + "match, bailing.\n"); | |
578 | + spin_unlock_bh(&l7_lock); | |
97491a6e | 579 | + return (pattern_result ^ info->invert); |
580 | + } | |
581 | + strcpy(master_conntrack->layer7.app_proto, info->protocol); | |
a06ee22f | 582 | + } else if(pattern_result > 1) { /* cleanup from "unset" */ |
583 | + pattern_result = 1; | |
97491a6e | 584 | + } |
585 | + | |
586 | + /* mark the packet seen */ | |
587 | + skb->cb[0] = 1; | |
588 | + | |
a06ee22f | 589 | + spin_unlock_bh(&l7_lock); |
97491a6e | 590 | + return (pattern_result ^ info->invert); |
591 | +} | |
592 | + | |
a06ee22f | 593 | +static int check(const char *tablename, |
594 | + const void *inf, | |
595 | + const struct xt_match *match, | |
596 | + void *matchinfo, | |
597 | + unsigned int hook_mask) | |
97491a6e | 598 | + |
a06ee22f | 599 | +{ |
600 | + // load nf_conntrack_ipv4 | |
601 | + if (nf_ct_l3proto_try_module_get(match->family) < 0) { | |
602 | + printk(KERN_WARNING "can't load conntrack support for " | |
603 | + "proto=%d\n", match->family); | |
604 | + return 0; | |
605 | + } | |
97491a6e | 606 | + return 1; |
607 | +} | |
608 | + | |
a06ee22f | 609 | +static void |
610 | +destroy(const struct xt_match *match, void *matchinfo) | |
97491a6e | 611 | +{ |
a06ee22f | 612 | + nf_ct_l3proto_module_put(match->family); |
97491a6e | 613 | +} |
614 | + | |
a06ee22f | 615 | +static struct xt_match xt_layer7_match[] = { |
97491a6e | 616 | +{ |
a06ee22f | 617 | + .name = "layer7", |
618 | + .family = AF_INET, | |
619 | + .checkentry = check, | |
620 | + .match = match, | |
621 | + .destroy = destroy, | |
622 | + .matchsize = sizeof(struct xt_layer7_info), | |
623 | + .me = THIS_MODULE | |
97491a6e | 624 | +} |
a06ee22f | 625 | +}; |
97491a6e | 626 | + |
a06ee22f | 627 | +static void layer7_cleanup_proc(void) |
97491a6e | 628 | +{ |
9547f064 | 629 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
630 | + remove_proc_entry("layer7_numpackets", proc_net); | |
631 | +#else | |
143a4708 | 632 | + remove_proc_entry("layer7_numpackets", init_net.proc_net); |
9547f064 | 633 | +#endif |
97491a6e | 634 | +} |
635 | + | |
636 | +/* register the proc file */ | |
637 | +static void layer7_init_proc(void) | |
638 | +{ | |
639 | + struct proc_dir_entry* entry; | |
9547f064 | 640 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
641 | + entry = create_proc_entry("layer7_numpackets", 0644, proc_net); | |
642 | +#else | |
143a4708 | 643 | + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net); |
9547f064 | 644 | +#endif |
97491a6e | 645 | + entry->read_proc = layer7_read_proc; |
646 | + entry->write_proc = layer7_write_proc; | |
647 | +} | |
648 | + | |
a06ee22f | 649 | +static int __init xt_layer7_init(void) |
97491a6e | 650 | +{ |
651 | + need_conntrack(); | |
652 | + | |
653 | + layer7_init_proc(); | |
654 | + if(maxdatalen < 1) { | |
a06ee22f | 655 | + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, " |
656 | + "using 1\n"); | |
97491a6e | 657 | + maxdatalen = 1; |
658 | + } | |
659 | + /* This is not a hard limit. It's just here to prevent people from | |
660 | + bringing their slow machines to a grinding halt. */ | |
661 | + else if(maxdatalen > 65536) { | |
a06ee22f | 662 | + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, " |
663 | + "using 65536\n"); | |
97491a6e | 664 | + maxdatalen = 65536; |
665 | + } | |
a06ee22f | 666 | + return xt_register_matches(xt_layer7_match, |
667 | + ARRAY_SIZE(xt_layer7_match)); | |
97491a6e | 668 | +} |
669 | + | |
a06ee22f | 670 | +static void __exit xt_layer7_fini(void) |
97491a6e | 671 | +{ |
672 | + layer7_cleanup_proc(); | |
a06ee22f | 673 | + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match)); |
97491a6e | 674 | +} |
675 | + | |
a06ee22f | 676 | +module_init(xt_layer7_init); |
677 | +module_exit(xt_layer7_fini); | |
9547f064 | 678 | --- linux-2.6.25/net/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600 |
679 | +++ linux-2.6.25-layer7/net/netfilter/regexp/regexp.c 2008-04-29 00:40:01.000000000 -0500 | |
97491a6e | 680 | @@ -0,0 +1,1197 @@ |
681 | +/* | |
682 | + * regcomp and regexec -- regsub and regerror are elsewhere | |
683 | + * @(#)regexp.c 1.3 of 18 April 87 | |
684 | + * | |
685 | + * Copyright (c) 1986 by University of Toronto. | |
686 | + * Written by Henry Spencer. Not derived from licensed software. | |
687 | + * | |
688 | + * Permission is granted to anyone to use this software for any | |
689 | + * purpose on any computer system, and to redistribute it freely, | |
690 | + * subject to the following restrictions: | |
691 | + * | |
692 | + * 1. The author is not responsible for the consequences of use of | |
693 | + * this software, no matter how awful, even if they arise | |
694 | + * from defects in it. | |
695 | + * | |
696 | + * 2. The origin of this software must not be misrepresented, either | |
697 | + * by explicit claim or by omission. | |
698 | + * | |
699 | + * 3. Altered versions must be plainly marked as such, and must not | |
700 | + * be misrepresented as being the original software. | |
701 | + * | |
702 | + * Beware that some of this code is subtly aware of the way operator | |
703 | + * precedence is structured in regular expressions. Serious changes in | |
704 | + * regular-expression syntax might require a total rethink. | |
705 | + * | |
706 | + * This code was modified by Ethan Sommer to work within the kernel | |
707 | + * (it now uses kmalloc etc..) | |
708 | + * | |
709 | + * Modified slightly by Matthew Strait to use more modern C. | |
710 | + */ | |
711 | + | |
712 | +#include "regexp.h" | |
713 | +#include "regmagic.h" | |
714 | + | |
715 | +/* added by ethan and matt. Lets it work in both kernel and user space. | |
716 | +(So iptables can use it, for instance.) Yea, it goes both ways... */ | |
717 | +#if __KERNEL__ | |
718 | + #define malloc(foo) kmalloc(foo,GFP_ATOMIC) | |
719 | +#else | |
720 | + #define printk(format,args...) printf(format,##args) | |
721 | +#endif | |
722 | + | |
723 | +void regerror(char * s) | |
724 | +{ | |
725 | + printk("<3>Regexp: %s\n", s); | |
726 | + /* NOTREACHED */ | |
727 | +} | |
728 | + | |
729 | +/* | |
730 | + * The "internal use only" fields in regexp.h are present to pass info from | |
731 | + * compile to execute that permits the execute phase to run lots faster on | |
732 | + * simple cases. They are: | |
733 | + * | |
734 | + * regstart char that must begin a match; '\0' if none obvious | |
735 | + * reganch is the match anchored (at beginning-of-line only)? | |
736 | + * regmust string (pointer into program) that match must include, or NULL | |
737 | + * regmlen length of regmust string | |
738 | + * | |
739 | + * Regstart and reganch permit very fast decisions on suitable starting points | |
740 | + * for a match, cutting down the work a lot. Regmust permits fast rejection | |
741 | + * of lines that cannot possibly match. The regmust tests are costly enough | |
742 | + * that regcomp() supplies a regmust only if the r.e. contains something | |
743 | + * potentially expensive (at present, the only such thing detected is * or + | |
744 | + * at the start of the r.e., which can involve a lot of backup). Regmlen is | |
745 | + * supplied because the test in regexec() needs it and regcomp() is computing | |
746 | + * it anyway. | |
747 | + */ | |
748 | + | |
749 | +/* | |
750 | + * Structure for regexp "program". This is essentially a linear encoding | |
751 | + * of a nondeterministic finite-state machine (aka syntax charts or | |
752 | + * "railroad normal form" in parsing technology). Each node is an opcode | |
753 | + * plus a "next" pointer, possibly plus an operand. "Next" pointers of | |
754 | + * all nodes except BRANCH implement concatenation; a "next" pointer with | |
755 | + * a BRANCH on both ends of it is connecting two alternatives. (Here we | |
756 | + * have one of the subtle syntax dependencies: an individual BRANCH (as | |
757 | + * opposed to a collection of them) is never concatenated with anything | |
758 | + * because of operator precedence.) The operand of some types of node is | |
759 | + * a literal string; for others, it is a node leading into a sub-FSM. In | |
760 | + * particular, the operand of a BRANCH node is the first node of the branch. | |
761 | + * (NB this is *not* a tree structure: the tail of the branch connects | |
762 | + * to the thing following the set of BRANCHes.) The opcodes are: | |
763 | + */ | |
764 | + | |
765 | +/* definition number opnd? meaning */ | |
766 | +#define END 0 /* no End of program. */ | |
767 | +#define BOL 1 /* no Match "" at beginning of line. */ | |
768 | +#define EOL 2 /* no Match "" at end of line. */ | |
769 | +#define ANY 3 /* no Match any one character. */ | |
770 | +#define ANYOF 4 /* str Match any character in this string. */ | |
771 | +#define ANYBUT 5 /* str Match any character not in this string. */ | |
772 | +#define BRANCH 6 /* node Match this alternative, or the next... */ | |
773 | +#define BACK 7 /* no Match "", "next" ptr points backward. */ | |
774 | +#define EXACTLY 8 /* str Match this string. */ | |
775 | +#define NOTHING 9 /* no Match empty string. */ | |
776 | +#define STAR 10 /* node Match this (simple) thing 0 or more times. */ | |
777 | +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */ | |
778 | +#define OPEN 20 /* no Mark this point in input as start of #n. */ | |
779 | + /* OPEN+1 is number 1, etc. */ | |
780 | +#define CLOSE 30 /* no Analogous to OPEN. */ | |
781 | + | |
782 | +/* | |
783 | + * Opcode notes: | |
784 | + * | |
785 | + * BRANCH The set of branches constituting a single choice are hooked | |
786 | + * together with their "next" pointers, since precedence prevents | |
787 | + * anything being concatenated to any individual branch. The | |
788 | + * "next" pointer of the last BRANCH in a choice points to the | |
789 | + * thing following the whole choice. This is also where the | |
790 | + * final "next" pointer of each individual branch points; each | |
791 | + * branch starts with the operand node of a BRANCH node. | |
792 | + * | |
793 | + * BACK Normal "next" pointers all implicitly point forward; BACK | |
794 | + * exists to make loop structures possible. | |
795 | + * | |
796 | + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular | |
797 | + * BRANCH structures using BACK. Simple cases (one character | |
798 | + * per match) are implemented with STAR and PLUS for speed | |
799 | + * and to minimize recursive plunges. | |
800 | + * | |
801 | + * OPEN,CLOSE ...are numbered at compile time. | |
802 | + */ | |
803 | + | |
804 | +/* | |
805 | + * A node is one char of opcode followed by two chars of "next" pointer. | |
806 | + * "Next" pointers are stored as two 8-bit pieces, high order first. The | |
807 | + * value is a positive offset from the opcode of the node containing it. | |
808 | + * An operand, if any, simply follows the node. (Note that much of the | |
809 | + * code generation knows about this implicit relationship.) | |
810 | + * | |
811 | + * Using two bytes for the "next" pointer is vast overkill for most things, | |
812 | + * but allows patterns to get big without disasters. | |
813 | + */ | |
814 | +#define OP(p) (*(p)) | |
815 | +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377)) | |
816 | +#define OPERAND(p) ((p) + 3) | |
817 | + | |
818 | +/* | |
819 | + * See regmagic.h for one further detail of program structure. | |
820 | + */ | |
821 | + | |
822 | + | |
823 | +/* | |
824 | + * Utility definitions. | |
825 | + */ | |
826 | +#ifndef CHARBITS | |
827 | +#define UCHARAT(p) ((int)*(unsigned char *)(p)) | |
828 | +#else | |
829 | +#define UCHARAT(p) ((int)*(p)&CHARBITS) | |
830 | +#endif | |
831 | + | |
832 | +#define FAIL(m) { regerror(m); return(NULL); } | |
833 | +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?') | |
834 | +#define META "^$.[()|?+*\\" | |
835 | + | |
836 | +/* | |
837 | + * Flags to be passed up and down. | |
838 | + */ | |
839 | +#define HASWIDTH 01 /* Known never to match null string. */ | |
840 | +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */ | |
841 | +#define SPSTART 04 /* Starts with * or +. */ | |
842 | +#define WORST 0 /* Worst case. */ | |
843 | + | |
844 | +/* | |
845 | + * Global work variables for regcomp(). | |
846 | + */ | |
847 | +struct match_globals { | |
848 | +char *reginput; /* String-input pointer. */ | |
849 | +char *regbol; /* Beginning of input, for ^ check. */ | |
850 | +char **regstartp; /* Pointer to startp array. */ | |
851 | +char **regendp; /* Ditto for endp. */ | |
852 | +char *regparse; /* Input-scan pointer. */ | |
853 | +int regnpar; /* () count. */ | |
854 | +char regdummy; | |
855 | +char *regcode; /* Code-emit pointer; ®dummy = don't. */ | |
856 | +long regsize; /* Code size. */ | |
857 | +}; | |
858 | + | |
859 | +/* | |
860 | + * Forward declarations for regcomp()'s friends. | |
861 | + */ | |
862 | +#ifndef STATIC | |
863 | +#define STATIC static | |
864 | +#endif | |
865 | +STATIC char *reg(struct match_globals *g, int paren,int *flagp); | |
866 | +STATIC char *regbranch(struct match_globals *g, int *flagp); | |
867 | +STATIC char *regpiece(struct match_globals *g, int *flagp); | |
868 | +STATIC char *regatom(struct match_globals *g, int *flagp); | |
869 | +STATIC char *regnode(struct match_globals *g, char op); | |
870 | +STATIC char *regnext(struct match_globals *g, char *p); | |
871 | +STATIC void regc(struct match_globals *g, char b); | |
872 | +STATIC void reginsert(struct match_globals *g, char op, char *opnd); | |
873 | +STATIC void regtail(struct match_globals *g, char *p, char *val); | |
874 | +STATIC void regoptail(struct match_globals *g, char *p, char *val); | |
875 | + | |
876 | + | |
877 | +__kernel_size_t my_strcspn(const char *s1,const char *s2) | |
878 | +{ | |
879 | + char *scan1; | |
880 | + char *scan2; | |
881 | + int count; | |
882 | + | |
883 | + count = 0; | |
884 | + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) { | |
885 | + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */ | |
886 | + if (*scan1 == *scan2++) | |
887 | + return(count); | |
888 | + count++; | |
889 | + } | |
890 | + return(count); | |
891 | +} | |
892 | + | |
893 | +/* | |
894 | + - regcomp - compile a regular expression into internal code | |
895 | + * | |
896 | + * We can't allocate space until we know how big the compiled form will be, | |
897 | + * but we can't compile it (and thus know how big it is) until we've got a | |
898 | + * place to put the code. So we cheat: we compile it twice, once with code | |
899 | + * generation turned off and size counting turned on, and once "for real". | |
900 | + * This also means that we don't allocate space until we are sure that the | |
901 | + * thing really will compile successfully, and we never have to move the | |
902 | + * code and thus invalidate pointers into it. (Note that it has to be in | |
903 | + * one piece because free() must be able to free it all.) | |
904 | + * | |
905 | + * Beware that the optimization-preparation code in here knows about some | |
906 | + * of the structure of the compiled regexp. | |
907 | + */ | |
908 | +regexp * | |
909 | +regcomp(char *exp,int *patternsize) | |
910 | +{ | |
911 | + register regexp *r; | |
912 | + register char *scan; | |
913 | + register char *longest; | |
914 | + register int len; | |
915 | + int flags; | |
916 | + struct match_globals g; | |
917 | + | |
918 | + /* commented out by ethan | |
919 | + extern char *malloc(); | |
920 | + */ | |
921 | + | |
922 | + if (exp == NULL) | |
923 | + FAIL("NULL argument"); | |
924 | + | |
925 | + /* First pass: determine size, legality. */ | |
926 | + g.regparse = exp; | |
927 | + g.regnpar = 1; | |
928 | + g.regsize = 0L; | |
929 | + g.regcode = &g.regdummy; | |
930 | + regc(&g, MAGIC); | |
931 | + if (reg(&g, 0, &flags) == NULL) | |
932 | + return(NULL); | |
933 | + | |
934 | + /* Small enough for pointer-storage convention? */ | |
935 | + if (g.regsize >= 32767L) /* Probably could be 65535L. */ | |
936 | + FAIL("regexp too big"); | |
937 | + | |
938 | + /* Allocate space. */ | |
939 | + *patternsize=sizeof(regexp) + (unsigned)g.regsize; | |
940 | + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize); | |
941 | + if (r == NULL) | |
942 | + FAIL("out of space"); | |
943 | + | |
944 | + /* Second pass: emit code. */ | |
945 | + g.regparse = exp; | |
946 | + g.regnpar = 1; | |
947 | + g.regcode = r->program; | |
948 | + regc(&g, MAGIC); | |
949 | + if (reg(&g, 0, &flags) == NULL) | |
950 | + return(NULL); | |
951 | + | |
952 | + /* Dig out information for optimizations. */ | |
953 | + r->regstart = '\0'; /* Worst-case defaults. */ | |
954 | + r->reganch = 0; | |
955 | + r->regmust = NULL; | |
956 | + r->regmlen = 0; | |
957 | + scan = r->program+1; /* First BRANCH. */ | |
958 | + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */ | |
959 | + scan = OPERAND(scan); | |
960 | + | |
961 | + /* Starting-point info. */ | |
962 | + if (OP(scan) == EXACTLY) | |
963 | + r->regstart = *OPERAND(scan); | |
964 | + else if (OP(scan) == BOL) | |
965 | + r->reganch++; | |
966 | + | |
967 | + /* | |
968 | + * If there's something expensive in the r.e., find the | |
969 | + * longest literal string that must appear and make it the | |
970 | + * regmust. Resolve ties in favor of later strings, since | |
971 | + * the regstart check works with the beginning of the r.e. | |
972 | + * and avoiding duplication strengthens checking. Not a | |
973 | + * strong reason, but sufficient in the absence of others. | |
974 | + */ | |
975 | + if (flags&SPSTART) { | |
976 | + longest = NULL; | |
977 | + len = 0; | |
978 | + for (; scan != NULL; scan = regnext(&g, scan)) | |
979 | + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) { | |
980 | + longest = OPERAND(scan); | |
981 | + len = strlen(OPERAND(scan)); | |
982 | + } | |
983 | + r->regmust = longest; | |
984 | + r->regmlen = len; | |
985 | + } | |
986 | + } | |
987 | + | |
988 | + return(r); | |
989 | +} | |
990 | + | |
991 | +/* | |
992 | + - reg - regular expression, i.e. main body or parenthesized thing | |
993 | + * | |
994 | + * Caller must absorb opening parenthesis. | |
995 | + * | |
996 | + * Combining parenthesis handling with the base level of regular expression | |
997 | + * is a trifle forced, but the need to tie the tails of the branches to what | |
998 | + * follows makes it hard to avoid. | |
999 | + */ | |
1000 | +static char * | |
1001 | +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ ) | |
1002 | +{ | |
1003 | + register char *ret; | |
1004 | + register char *br; | |
1005 | + register char *ender; | |
1006 | + register int parno = 0; /* 0 makes gcc happy */ | |
1007 | + int flags; | |
1008 | + | |
1009 | + *flagp = HASWIDTH; /* Tentatively. */ | |
1010 | + | |
1011 | + /* Make an OPEN node, if parenthesized. */ | |
1012 | + if (paren) { | |
1013 | + if (g->regnpar >= NSUBEXP) | |
1014 | + FAIL("too many ()"); | |
1015 | + parno = g->regnpar; | |
1016 | + g->regnpar++; | |
1017 | + ret = regnode(g, OPEN+parno); | |
1018 | + } else | |
1019 | + ret = NULL; | |
1020 | + | |
1021 | + /* Pick up the branches, linking them together. */ | |
1022 | + br = regbranch(g, &flags); | |
1023 | + if (br == NULL) | |
1024 | + return(NULL); | |
1025 | + if (ret != NULL) | |
1026 | + regtail(g, ret, br); /* OPEN -> first. */ | |
1027 | + else | |
1028 | + ret = br; | |
1029 | + if (!(flags&HASWIDTH)) | |
1030 | + *flagp &= ~HASWIDTH; | |
1031 | + *flagp |= flags&SPSTART; | |
1032 | + while (*g->regparse == '|') { | |
1033 | + g->regparse++; | |
1034 | + br = regbranch(g, &flags); | |
1035 | + if (br == NULL) | |
1036 | + return(NULL); | |
1037 | + regtail(g, ret, br); /* BRANCH -> BRANCH. */ | |
1038 | + if (!(flags&HASWIDTH)) | |
1039 | + *flagp &= ~HASWIDTH; | |
1040 | + *flagp |= flags&SPSTART; | |
1041 | + } | |
1042 | + | |
1043 | + /* Make a closing node, and hook it on the end. */ | |
1044 | + ender = regnode(g, (paren) ? CLOSE+parno : END); | |
1045 | + regtail(g, ret, ender); | |
1046 | + | |
1047 | + /* Hook the tails of the branches to the closing node. */ | |
1048 | + for (br = ret; br != NULL; br = regnext(g, br)) | |
1049 | + regoptail(g, br, ender); | |
1050 | + | |
1051 | + /* Check for proper termination. */ | |
1052 | + if (paren && *g->regparse++ != ')') { | |
1053 | + FAIL("unmatched ()"); | |
1054 | + } else if (!paren && *g->regparse != '\0') { | |
1055 | + if (*g->regparse == ')') { | |
1056 | + FAIL("unmatched ()"); | |
1057 | + } else | |
1058 | + FAIL("junk on end"); /* "Can't happen". */ | |
1059 | + /* NOTREACHED */ | |
1060 | + } | |
1061 | + | |
1062 | + return(ret); | |
1063 | +} | |
1064 | + | |
1065 | +/* | |
1066 | + - regbranch - one alternative of an | operator | |
1067 | + * | |
1068 | + * Implements the concatenation operator. | |
1069 | + */ | |
1070 | +static char * | |
1071 | +regbranch(struct match_globals *g, int *flagp) | |
1072 | +{ | |
1073 | + register char *ret; | |
1074 | + register char *chain; | |
1075 | + register char *latest; | |
1076 | + int flags; | |
1077 | + | |
1078 | + *flagp = WORST; /* Tentatively. */ | |
1079 | + | |
1080 | + ret = regnode(g, BRANCH); | |
1081 | + chain = NULL; | |
1082 | + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') { | |
1083 | + latest = regpiece(g, &flags); | |
1084 | + if (latest == NULL) | |
1085 | + return(NULL); | |
1086 | + *flagp |= flags&HASWIDTH; | |
1087 | + if (chain == NULL) /* First piece. */ | |
1088 | + *flagp |= flags&SPSTART; | |
1089 | + else | |
1090 | + regtail(g, chain, latest); | |
1091 | + chain = latest; | |
1092 | + } | |
1093 | + if (chain == NULL) /* Loop ran zero times. */ | |
1094 | + (void) regnode(g, NOTHING); | |
1095 | + | |
1096 | + return(ret); | |
1097 | +} | |
1098 | + | |
1099 | +/* | |
1100 | + - regpiece - something followed by possible [*+?] | |
1101 | + * | |
1102 | + * Note that the branching code sequences used for ? and the general cases | |
1103 | + * of * and + are somewhat optimized: they use the same NOTHING node as | |
1104 | + * both the endmarker for their branch list and the body of the last branch. | |
1105 | + * It might seem that this node could be dispensed with entirely, but the | |
1106 | + * endmarker role is not redundant. | |
1107 | + */ | |
1108 | +static char * | |
1109 | +regpiece(struct match_globals *g, int *flagp) | |
1110 | +{ | |
1111 | + register char *ret; | |
1112 | + register char op; | |
1113 | + register char *next; | |
1114 | + int flags; | |
1115 | + | |
1116 | + ret = regatom(g, &flags); | |
1117 | + if (ret == NULL) | |
1118 | + return(NULL); | |
1119 | + | |
1120 | + op = *g->regparse; | |
1121 | + if (!ISMULT(op)) { | |
1122 | + *flagp = flags; | |
1123 | + return(ret); | |
1124 | + } | |
1125 | + | |
1126 | + if (!(flags&HASWIDTH) && op != '?') | |
1127 | + FAIL("*+ operand could be empty"); | |
1128 | + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); | |
1129 | + | |
1130 | + if (op == '*' && (flags&SIMPLE)) | |
1131 | + reginsert(g, STAR, ret); | |
1132 | + else if (op == '*') { | |
1133 | + /* Emit x* as (x&|), where & means "self". */ | |
1134 | + reginsert(g, BRANCH, ret); /* Either x */ | |
1135 | + regoptail(g, ret, regnode(g, BACK)); /* and loop */ | |
1136 | + regoptail(g, ret, ret); /* back */ | |
1137 | + regtail(g, ret, regnode(g, BRANCH)); /* or */ | |
1138 | + regtail(g, ret, regnode(g, NOTHING)); /* null. */ | |
1139 | + } else if (op == '+' && (flags&SIMPLE)) | |
1140 | + reginsert(g, PLUS, ret); | |
1141 | + else if (op == '+') { | |
1142 | + /* Emit x+ as x(&|), where & means "self". */ | |
1143 | + next = regnode(g, BRANCH); /* Either */ | |
1144 | + regtail(g, ret, next); | |
1145 | + regtail(g, regnode(g, BACK), ret); /* loop back */ | |
1146 | + regtail(g, next, regnode(g, BRANCH)); /* or */ | |
1147 | + regtail(g, ret, regnode(g, NOTHING)); /* null. */ | |
1148 | + } else if (op == '?') { | |
1149 | + /* Emit x? as (x|) */ | |
1150 | + reginsert(g, BRANCH, ret); /* Either x */ | |
1151 | + regtail(g, ret, regnode(g, BRANCH)); /* or */ | |
1152 | + next = regnode(g, NOTHING); /* null. */ | |
1153 | + regtail(g, ret, next); | |
1154 | + regoptail(g, ret, next); | |
1155 | + } | |
1156 | + g->regparse++; | |
1157 | + if (ISMULT(*g->regparse)) | |
1158 | + FAIL("nested *?+"); | |
1159 | + | |
1160 | + return(ret); | |
1161 | +} | |
1162 | + | |
1163 | +/* | |
1164 | + - regatom - the lowest level | |
1165 | + * | |
1166 | + * Optimization: gobbles an entire sequence of ordinary characters so that | |
1167 | + * it can turn them into a single node, which is smaller to store and | |
1168 | + * faster to run. Backslashed characters are exceptions, each becoming a | |
1169 | + * separate node; the code is simpler that way and it's not worth fixing. | |
1170 | + */ | |
1171 | +static char * | |
1172 | +regatom(struct match_globals *g, int *flagp) | |
1173 | +{ | |
1174 | + register char *ret; | |
1175 | + int flags; | |
1176 | + | |
1177 | + *flagp = WORST; /* Tentatively. */ | |
1178 | + | |
1179 | + switch (*g->regparse++) { | |
1180 | + case '^': | |
1181 | + ret = regnode(g, BOL); | |
1182 | + break; | |
1183 | + case '$': | |
1184 | + ret = regnode(g, EOL); | |
1185 | + break; | |
1186 | + case '.': | |
1187 | + ret = regnode(g, ANY); | |
1188 | + *flagp |= HASWIDTH|SIMPLE; | |
1189 | + break; | |
1190 | + case '[': { | |
1191 | + register int class; | |
1192 | + register int classend; | |
1193 | + | |
1194 | + if (*g->regparse == '^') { /* Complement of range. */ | |
1195 | + ret = regnode(g, ANYBUT); | |
1196 | + g->regparse++; | |
1197 | + } else | |
1198 | + ret = regnode(g, ANYOF); | |
1199 | + if (*g->regparse == ']' || *g->regparse == '-') | |
1200 | + regc(g, *g->regparse++); | |
1201 | + while (*g->regparse != '\0' && *g->regparse != ']') { | |
1202 | + if (*g->regparse == '-') { | |
1203 | + g->regparse++; | |
1204 | + if (*g->regparse == ']' || *g->regparse == '\0') | |
1205 | + regc(g, '-'); | |
1206 | + else { | |
1207 | + class = UCHARAT(g->regparse-2)+1; | |
1208 | + classend = UCHARAT(g->regparse); | |
1209 | + if (class > classend+1) | |
1210 | + FAIL("invalid [] range"); | |
1211 | + for (; class <= classend; class++) | |
1212 | + regc(g, class); | |
1213 | + g->regparse++; | |
1214 | + } | |
1215 | + } else | |
1216 | + regc(g, *g->regparse++); | |
1217 | + } | |
1218 | + regc(g, '\0'); | |
1219 | + if (*g->regparse != ']') | |
1220 | + FAIL("unmatched []"); | |
1221 | + g->regparse++; | |
1222 | + *flagp |= HASWIDTH|SIMPLE; | |
1223 | + } | |
1224 | + break; | |
1225 | + case '(': | |
1226 | + ret = reg(g, 1, &flags); | |
1227 | + if (ret == NULL) | |
1228 | + return(NULL); | |
1229 | + *flagp |= flags&(HASWIDTH|SPSTART); | |
1230 | + break; | |
1231 | + case '\0': | |
1232 | + case '|': | |
1233 | + case ')': | |
1234 | + FAIL("internal urp"); /* Supposed to be caught earlier. */ | |
1235 | + break; | |
1236 | + case '?': | |
1237 | + case '+': | |
1238 | + case '*': | |
1239 | + FAIL("?+* follows nothing"); | |
1240 | + break; | |
1241 | + case '\\': | |
1242 | + if (*g->regparse == '\0') | |
1243 | + FAIL("trailing \\"); | |
1244 | + ret = regnode(g, EXACTLY); | |
1245 | + regc(g, *g->regparse++); | |
1246 | + regc(g, '\0'); | |
1247 | + *flagp |= HASWIDTH|SIMPLE; | |
1248 | + break; | |
1249 | + default: { | |
1250 | + register int len; | |
1251 | + register char ender; | |
1252 | + | |
1253 | + g->regparse--; | |
1254 | + len = my_strcspn((const char *)g->regparse, (const char *)META); | |
1255 | + if (len <= 0) | |
1256 | + FAIL("internal disaster"); | |
1257 | + ender = *(g->regparse+len); | |
1258 | + if (len > 1 && ISMULT(ender)) | |
1259 | + len--; /* Back off clear of ?+* operand. */ | |
1260 | + *flagp |= HASWIDTH; | |
1261 | + if (len == 1) | |
1262 | + *flagp |= SIMPLE; | |
1263 | + ret = regnode(g, EXACTLY); | |
1264 | + while (len > 0) { | |
1265 | + regc(g, *g->regparse++); | |
1266 | + len--; | |
1267 | + } | |
1268 | + regc(g, '\0'); | |
1269 | + } | |
1270 | + break; | |
1271 | + } | |
1272 | + | |
1273 | + return(ret); | |
1274 | +} | |
1275 | + | |
1276 | +/* | |
1277 | + - regnode - emit a node | |
1278 | + */ | |
1279 | +static char * /* Location. */ | |
1280 | +regnode(struct match_globals *g, char op) | |
1281 | +{ | |
1282 | + register char *ret; | |
1283 | + register char *ptr; | |
1284 | + | |
1285 | + ret = g->regcode; | |
1286 | + if (ret == &g->regdummy) { | |
1287 | + g->regsize += 3; | |
1288 | + return(ret); | |
1289 | + } | |
1290 | + | |
1291 | + ptr = ret; | |
1292 | + *ptr++ = op; | |
1293 | + *ptr++ = '\0'; /* Null "next" pointer. */ | |
1294 | + *ptr++ = '\0'; | |
1295 | + g->regcode = ptr; | |
1296 | + | |
1297 | + return(ret); | |
1298 | +} | |
1299 | + | |
1300 | +/* | |
1301 | + - regc - emit (if appropriate) a byte of code | |
1302 | + */ | |
1303 | +static void | |
1304 | +regc(struct match_globals *g, char b) | |
1305 | +{ | |
1306 | + if (g->regcode != &g->regdummy) | |
1307 | + *g->regcode++ = b; | |
1308 | + else | |
1309 | + g->regsize++; | |
1310 | +} | |
1311 | + | |
1312 | +/* | |
1313 | + - reginsert - insert an operator in front of already-emitted operand | |
1314 | + * | |
1315 | + * Means relocating the operand. | |
1316 | + */ | |
1317 | +static void | |
1318 | +reginsert(struct match_globals *g, char op, char* opnd) | |
1319 | +{ | |
1320 | + register char *src; | |
1321 | + register char *dst; | |
1322 | + register char *place; | |
1323 | + | |
1324 | + if (g->regcode == &g->regdummy) { | |
1325 | + g->regsize += 3; | |
1326 | + return; | |
1327 | + } | |
1328 | + | |
1329 | + src = g->regcode; | |
1330 | + g->regcode += 3; | |
1331 | + dst = g->regcode; | |
1332 | + while (src > opnd) | |
1333 | + *--dst = *--src; | |
1334 | + | |
1335 | + place = opnd; /* Op node, where operand used to be. */ | |
1336 | + *place++ = op; | |
1337 | + *place++ = '\0'; | |
1338 | + *place++ = '\0'; | |
1339 | +} | |
1340 | + | |
1341 | +/* | |
1342 | + - regtail - set the next-pointer at the end of a node chain | |
1343 | + */ | |
1344 | +static void | |
1345 | +regtail(struct match_globals *g, char *p, char *val) | |
1346 | +{ | |
1347 | + register char *scan; | |
1348 | + register char *temp; | |
1349 | + register int offset; | |
1350 | + | |
1351 | + if (p == &g->regdummy) | |
1352 | + return; | |
1353 | + | |
1354 | + /* Find last node. */ | |
1355 | + scan = p; | |
1356 | + for (;;) { | |
1357 | + temp = regnext(g, scan); | |
1358 | + if (temp == NULL) | |
1359 | + break; | |
1360 | + scan = temp; | |
1361 | + } | |
1362 | + | |
1363 | + if (OP(scan) == BACK) | |
1364 | + offset = scan - val; | |
1365 | + else | |
1366 | + offset = val - scan; | |
1367 | + *(scan+1) = (offset>>8)&0377; | |
1368 | + *(scan+2) = offset&0377; | |
1369 | +} | |
1370 | + | |
1371 | +/* | |
1372 | + - regoptail - regtail on operand of first argument; nop if operandless | |
1373 | + */ | |
1374 | +static void | |
1375 | +regoptail(struct match_globals *g, char *p, char *val) | |
1376 | +{ | |
1377 | + /* "Operandless" and "op != BRANCH" are synonymous in practice. */ | |
1378 | + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH) | |
1379 | + return; | |
1380 | + regtail(g, OPERAND(p), val); | |
1381 | +} | |
1382 | + | |
1383 | +/* | |
1384 | + * regexec and friends | |
1385 | + */ | |
1386 | + | |
1387 | + | |
1388 | +/* | |
1389 | + * Forwards. | |
1390 | + */ | |
1391 | +STATIC int regtry(struct match_globals *g, regexp *prog, char *string); | |
1392 | +STATIC int regmatch(struct match_globals *g, char *prog); | |
1393 | +STATIC int regrepeat(struct match_globals *g, char *p); | |
1394 | + | |
1395 | +#ifdef DEBUG | |
1396 | +int regnarrate = 0; | |
1397 | +void regdump(); | |
1398 | +STATIC char *regprop(char *op); | |
1399 | +#endif | |
1400 | + | |
1401 | +/* | |
1402 | + - regexec - match a regexp against a string | |
1403 | + */ | |
1404 | +int | |
1405 | +regexec(regexp *prog, char *string) | |
1406 | +{ | |
1407 | + register char *s; | |
1408 | + struct match_globals g; | |
1409 | + | |
1410 | + /* Be paranoid... */ | |
1411 | + if (prog == NULL || string == NULL) { | |
1412 | + printk("<3>Regexp: NULL parameter\n"); | |
1413 | + return(0); | |
1414 | + } | |
1415 | + | |
1416 | + /* Check validity of program. */ | |
1417 | + if (UCHARAT(prog->program) != MAGIC) { | |
1418 | + printk("<3>Regexp: corrupted program\n"); | |
1419 | + return(0); | |
1420 | + } | |
1421 | + | |
1422 | + /* If there is a "must appear" string, look for it. */ | |
1423 | + if (prog->regmust != NULL) { | |
1424 | + s = string; | |
1425 | + while ((s = strchr(s, prog->regmust[0])) != NULL) { | |
1426 | + if (strncmp(s, prog->regmust, prog->regmlen) == 0) | |
1427 | + break; /* Found it. */ | |
1428 | + s++; | |
1429 | + } | |
1430 | + if (s == NULL) /* Not present. */ | |
1431 | + return(0); | |
1432 | + } | |
1433 | + | |
1434 | + /* Mark beginning of line for ^ . */ | |
1435 | + g.regbol = string; | |
1436 | + | |
1437 | + /* Simplest case: anchored match need be tried only once. */ | |
1438 | + if (prog->reganch) | |
1439 | + return(regtry(&g, prog, string)); | |
1440 | + | |
1441 | + /* Messy cases: unanchored match. */ | |
1442 | + s = string; | |
1443 | + if (prog->regstart != '\0') | |
1444 | + /* We know what char it must start with. */ | |
1445 | + while ((s = strchr(s, prog->regstart)) != NULL) { | |
1446 | + if (regtry(&g, prog, s)) | |
1447 | + return(1); | |
1448 | + s++; | |
1449 | + } | |
1450 | + else | |
1451 | + /* We don't -- general case. */ | |
1452 | + do { | |
1453 | + if (regtry(&g, prog, s)) | |
1454 | + return(1); | |
1455 | + } while (*s++ != '\0'); | |
1456 | + | |
1457 | + /* Failure. */ | |
1458 | + return(0); | |
1459 | +} | |
1460 | + | |
1461 | +/* | |
1462 | + - regtry - try match at specific point | |
1463 | + */ | |
1464 | +static int /* 0 failure, 1 success */ | |
1465 | +regtry(struct match_globals *g, regexp *prog, char *string) | |
1466 | +{ | |
1467 | + register int i; | |
1468 | + register char **sp; | |
1469 | + register char **ep; | |
1470 | + | |
1471 | + g->reginput = string; | |
1472 | + g->regstartp = prog->startp; | |
1473 | + g->regendp = prog->endp; | |
1474 | + | |
1475 | + sp = prog->startp; | |
1476 | + ep = prog->endp; | |
1477 | + for (i = NSUBEXP; i > 0; i--) { | |
1478 | + *sp++ = NULL; | |
1479 | + *ep++ = NULL; | |
1480 | + } | |
1481 | + if (regmatch(g, prog->program + 1)) { | |
1482 | + prog->startp[0] = string; | |
1483 | + prog->endp[0] = g->reginput; | |
1484 | + return(1); | |
1485 | + } else | |
1486 | + return(0); | |
1487 | +} | |
1488 | + | |
1489 | +/* | |
1490 | + - regmatch - main matching routine | |
1491 | + * | |
1492 | + * Conceptually the strategy is simple: check to see whether the current | |
1493 | + * node matches, call self recursively to see whether the rest matches, | |
1494 | + * and then act accordingly. In practice we make some effort to avoid | |
1495 | + * recursion, in particular by going through "ordinary" nodes (that don't | |
1496 | + * need to know whether the rest of the match failed) by a loop instead of | |
1497 | + * by recursion. | |
1498 | + */ | |
1499 | +static int /* 0 failure, 1 success */ | |
1500 | +regmatch(struct match_globals *g, char *prog) | |
1501 | +{ | |
1502 | + register char *scan = prog; /* Current node. */ | |
1503 | + char *next; /* Next node. */ | |
1504 | + | |
1505 | +#ifdef DEBUG | |
1506 | + if (scan != NULL && regnarrate) | |
1507 | + fprintf(stderr, "%s(\n", regprop(scan)); | |
1508 | +#endif | |
1509 | + while (scan != NULL) { | |
1510 | +#ifdef DEBUG | |
1511 | + if (regnarrate) | |
1512 | + fprintf(stderr, "%s...\n", regprop(scan)); | |
1513 | +#endif | |
1514 | + next = regnext(g, scan); | |
1515 | + | |
1516 | + switch (OP(scan)) { | |
1517 | + case BOL: | |
1518 | + if (g->reginput != g->regbol) | |
1519 | + return(0); | |
1520 | + break; | |
1521 | + case EOL: | |
1522 | + if (*g->reginput != '\0') | |
1523 | + return(0); | |
1524 | + break; | |
1525 | + case ANY: | |
1526 | + if (*g->reginput == '\0') | |
1527 | + return(0); | |
1528 | + g->reginput++; | |
1529 | + break; | |
1530 | + case EXACTLY: { | |
1531 | + register int len; | |
1532 | + register char *opnd; | |
1533 | + | |
1534 | + opnd = OPERAND(scan); | |
1535 | + /* Inline the first character, for speed. */ | |
1536 | + if (*opnd != *g->reginput) | |
1537 | + return(0); | |
1538 | + len = strlen(opnd); | |
1539 | + if (len > 1 && strncmp(opnd, g->reginput, len) != 0) | |
1540 | + return(0); | |
1541 | + g->reginput += len; | |
1542 | + } | |
1543 | + break; | |
1544 | + case ANYOF: | |
1545 | + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL) | |
1546 | + return(0); | |
1547 | + g->reginput++; | |
1548 | + break; | |
1549 | + case ANYBUT: | |
1550 | + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL) | |
1551 | + return(0); | |
1552 | + g->reginput++; | |
1553 | + break; | |
1554 | + case NOTHING: | |
1555 | + case BACK: | |
1556 | + break; | |
1557 | + case OPEN+1: | |
1558 | + case OPEN+2: | |
1559 | + case OPEN+3: | |
1560 | + case OPEN+4: | |
1561 | + case OPEN+5: | |
1562 | + case OPEN+6: | |
1563 | + case OPEN+7: | |
1564 | + case OPEN+8: | |
1565 | + case OPEN+9: { | |
1566 | + register int no; | |
1567 | + register char *save; | |
1568 | + | |
1569 | + no = OP(scan) - OPEN; | |
1570 | + save = g->reginput; | |
1571 | + | |
1572 | + if (regmatch(g, next)) { | |
1573 | + /* | |
1574 | + * Don't set startp if some later | |
1575 | + * invocation of the same parentheses | |
1576 | + * already has. | |
1577 | + */ | |
1578 | + if (g->regstartp[no] == NULL) | |
1579 | + g->regstartp[no] = save; | |
1580 | + return(1); | |
1581 | + } else | |
1582 | + return(0); | |
1583 | + } | |
1584 | + break; | |
1585 | + case CLOSE+1: | |
1586 | + case CLOSE+2: | |
1587 | + case CLOSE+3: | |
1588 | + case CLOSE+4: | |
1589 | + case CLOSE+5: | |
1590 | + case CLOSE+6: | |
1591 | + case CLOSE+7: | |
1592 | + case CLOSE+8: | |
1593 | + case CLOSE+9: | |
1594 | + { | |
1595 | + register int no; | |
1596 | + register char *save; | |
1597 | + | |
1598 | + no = OP(scan) - CLOSE; | |
1599 | + save = g->reginput; | |
1600 | + | |
1601 | + if (regmatch(g, next)) { | |
1602 | + /* | |
1603 | + * Don't set endp if some later | |
1604 | + * invocation of the same parentheses | |
1605 | + * already has. | |
1606 | + */ | |
1607 | + if (g->regendp[no] == NULL) | |
1608 | + g->regendp[no] = save; | |
1609 | + return(1); | |
1610 | + } else | |
1611 | + return(0); | |
1612 | + } | |
1613 | + break; | |
1614 | + case BRANCH: { | |
1615 | + register char *save; | |
1616 | + | |
1617 | + if (OP(next) != BRANCH) /* No choice. */ | |
1618 | + next = OPERAND(scan); /* Avoid recursion. */ | |
1619 | + else { | |
1620 | + do { | |
1621 | + save = g->reginput; | |
1622 | + if (regmatch(g, OPERAND(scan))) | |
1623 | + return(1); | |
1624 | + g->reginput = save; | |
1625 | + scan = regnext(g, scan); | |
1626 | + } while (scan != NULL && OP(scan) == BRANCH); | |
1627 | + return(0); | |
1628 | + /* NOTREACHED */ | |
1629 | + } | |
1630 | + } | |
1631 | + break; | |
1632 | + case STAR: | |
1633 | + case PLUS: { | |
1634 | + register char nextch; | |
1635 | + register int no; | |
1636 | + register char *save; | |
1637 | + register int min; | |
1638 | + | |
1639 | + /* | |
1640 | + * Lookahead to avoid useless match attempts | |
1641 | + * when we know what character comes next. | |
1642 | + */ | |
1643 | + nextch = '\0'; | |
1644 | + if (OP(next) == EXACTLY) | |
1645 | + nextch = *OPERAND(next); | |
1646 | + min = (OP(scan) == STAR) ? 0 : 1; | |
1647 | + save = g->reginput; | |
1648 | + no = regrepeat(g, OPERAND(scan)); | |
1649 | + while (no >= min) { | |
1650 | + /* If it could work, try it. */ | |
1651 | + if (nextch == '\0' || *g->reginput == nextch) | |
1652 | + if (regmatch(g, next)) | |
1653 | + return(1); | |
1654 | + /* Couldn't or didn't -- back up. */ | |
1655 | + no--; | |
1656 | + g->reginput = save + no; | |
1657 | + } | |
1658 | + return(0); | |
1659 | + } | |
1660 | + break; | |
1661 | + case END: | |
1662 | + return(1); /* Success! */ | |
1663 | + break; | |
1664 | + default: | |
1665 | + printk("<3>Regexp: memory corruption\n"); | |
1666 | + return(0); | |
1667 | + break; | |
1668 | + } | |
1669 | + | |
1670 | + scan = next; | |
1671 | + } | |
1672 | + | |
1673 | + /* | |
1674 | + * We get here only if there's trouble -- normally "case END" is | |
1675 | + * the terminating point. | |
1676 | + */ | |
1677 | + printk("<3>Regexp: corrupted pointers\n"); | |
1678 | + return(0); | |
1679 | +} | |
1680 | + | |
1681 | +/* | |
1682 | + - regrepeat - repeatedly match something simple, report how many | |
1683 | + */ | |
1684 | +static int | |
1685 | +regrepeat(struct match_globals *g, char *p) | |
1686 | +{ | |
1687 | + register int count = 0; | |
1688 | + register char *scan; | |
1689 | + register char *opnd; | |
1690 | + | |
1691 | + scan = g->reginput; | |
1692 | + opnd = OPERAND(p); | |
1693 | + switch (OP(p)) { | |
1694 | + case ANY: | |
1695 | + count = strlen(scan); | |
1696 | + scan += count; | |
1697 | + break; | |
1698 | + case EXACTLY: | |
1699 | + while (*opnd == *scan) { | |
1700 | + count++; | |
1701 | + scan++; | |
1702 | + } | |
1703 | + break; | |
1704 | + case ANYOF: | |
1705 | + while (*scan != '\0' && strchr(opnd, *scan) != NULL) { | |
1706 | + count++; | |
1707 | + scan++; | |
1708 | + } | |
1709 | + break; | |
1710 | + case ANYBUT: | |
1711 | + while (*scan != '\0' && strchr(opnd, *scan) == NULL) { | |
1712 | + count++; | |
1713 | + scan++; | |
1714 | + } | |
1715 | + break; | |
1716 | + default: /* Oh dear. Called inappropriately. */ | |
1717 | + printk("<3>Regexp: internal foulup\n"); | |
1718 | + count = 0; /* Best compromise. */ | |
1719 | + break; | |
1720 | + } | |
1721 | + g->reginput = scan; | |
1722 | + | |
1723 | + return(count); | |
1724 | +} | |
1725 | + | |
1726 | +/* | |
1727 | + - regnext - dig the "next" pointer out of a node | |
1728 | + */ | |
1729 | +static char* | |
1730 | +regnext(struct match_globals *g, char *p) | |
1731 | +{ | |
1732 | + register int offset; | |
1733 | + | |
1734 | + if (p == &g->regdummy) | |
1735 | + return(NULL); | |
1736 | + | |
1737 | + offset = NEXT(p); | |
1738 | + if (offset == 0) | |
1739 | + return(NULL); | |
1740 | + | |
1741 | + if (OP(p) == BACK) | |
1742 | + return(p-offset); | |
1743 | + else | |
1744 | + return(p+offset); | |
1745 | +} | |
1746 | + | |
1747 | +#ifdef DEBUG | |
1748 | + | |
1749 | +STATIC char *regprop(); | |
1750 | + | |
1751 | +/* | |
1752 | + - regdump - dump a regexp onto stdout in vaguely comprehensible form | |
1753 | + */ | |
1754 | +void | |
1755 | +regdump(regexp *r) | |
1756 | +{ | |
1757 | + register char *s; | |
1758 | + register char op = EXACTLY; /* Arbitrary non-END op. */ | |
1759 | + register char *next; | |
1760 | + /* extern char *strchr(); */ | |
1761 | + | |
1762 | + | |
1763 | + s = r->program + 1; | |
1764 | + while (op != END) { /* While that wasn't END last time... */ | |
1765 | + op = OP(s); | |
1766 | + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */ | |
1767 | + next = regnext(s); | |
1768 | + if (next == NULL) /* Next ptr. */ | |
1769 | + printf("(0)"); | |
1770 | + else | |
1771 | + printf("(%d)", (s-r->program)+(next-s)); | |
1772 | + s += 3; | |
1773 | + if (op == ANYOF || op == ANYBUT || op == EXACTLY) { | |
1774 | + /* Literal string, where present. */ | |
1775 | + while (*s != '\0') { | |
1776 | + putchar(*s); | |
1777 | + s++; | |
1778 | + } | |
1779 | + s++; | |
1780 | + } | |
1781 | + putchar('\n'); | |
1782 | + } | |
1783 | + | |
1784 | + /* Header fields of interest. */ | |
1785 | + if (r->regstart != '\0') | |
1786 | + printf("start `%c' ", r->regstart); | |
1787 | + if (r->reganch) | |
1788 | + printf("anchored "); | |
1789 | + if (r->regmust != NULL) | |
1790 | + printf("must have \"%s\"", r->regmust); | |
1791 | + printf("\n"); | |
1792 | +} | |
1793 | + | |
1794 | +/* | |
1795 | + - regprop - printable representation of opcode | |
1796 | + */ | |
1797 | +static char * | |
1798 | +regprop(char *op) | |
1799 | +{ | |
1800 | +#define BUFLEN 50 | |
1801 | + register char *p; | |
1802 | + static char buf[BUFLEN]; | |
1803 | + | |
1804 | + strcpy(buf, ":"); | |
1805 | + | |
1806 | + switch (OP(op)) { | |
1807 | + case BOL: | |
1808 | + p = "BOL"; | |
1809 | + break; | |
1810 | + case EOL: | |
1811 | + p = "EOL"; | |
1812 | + break; | |
1813 | + case ANY: | |
1814 | + p = "ANY"; | |
1815 | + break; | |
1816 | + case ANYOF: | |
1817 | + p = "ANYOF"; | |
1818 | + break; | |
1819 | + case ANYBUT: | |
1820 | + p = "ANYBUT"; | |
1821 | + break; | |
1822 | + case BRANCH: | |
1823 | + p = "BRANCH"; | |
1824 | + break; | |
1825 | + case EXACTLY: | |
1826 | + p = "EXACTLY"; | |
1827 | + break; | |
1828 | + case NOTHING: | |
1829 | + p = "NOTHING"; | |
1830 | + break; | |
1831 | + case BACK: | |
1832 | + p = "BACK"; | |
1833 | + break; | |
1834 | + case END: | |
1835 | + p = "END"; | |
1836 | + break; | |
1837 | + case OPEN+1: | |
1838 | + case OPEN+2: | |
1839 | + case OPEN+3: | |
1840 | + case OPEN+4: | |
1841 | + case OPEN+5: | |
1842 | + case OPEN+6: | |
1843 | + case OPEN+7: | |
1844 | + case OPEN+8: | |
1845 | + case OPEN+9: | |
1846 | + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN); | |
1847 | + p = NULL; | |
1848 | + break; | |
1849 | + case CLOSE+1: | |
1850 | + case CLOSE+2: | |
1851 | + case CLOSE+3: | |
1852 | + case CLOSE+4: | |
1853 | + case CLOSE+5: | |
1854 | + case CLOSE+6: | |
1855 | + case CLOSE+7: | |
1856 | + case CLOSE+8: | |
1857 | + case CLOSE+9: | |
1858 | + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE); | |
1859 | + p = NULL; | |
1860 | + break; | |
1861 | + case STAR: | |
1862 | + p = "STAR"; | |
1863 | + break; | |
1864 | + case PLUS: | |
1865 | + p = "PLUS"; | |
1866 | + break; | |
1867 | + default: | |
1868 | + printk("<3>Regexp: corrupted opcode\n"); | |
1869 | + break; | |
1870 | + } | |
1871 | + if (p != NULL) | |
1872 | + strncat(buf, p, BUFLEN-strlen(buf)); | |
1873 | + return(buf); | |
1874 | +} | |
1875 | +#endif | |
1876 | + | |
1877 | + | |
9547f064 | 1878 | --- linux-2.6.25/net/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600 |
1879 | +++ linux-2.6.25-layer7/net/netfilter/regexp/regexp.h 2008-04-29 00:40:01.000000000 -0500 | |
97491a6e | 1880 | @@ -0,0 +1,41 @@ |
1881 | +/* | |
1882 | + * Definitions etc. for regexp(3) routines. | |
1883 | + * | |
1884 | + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof], | |
1885 | + * not the System V one. | |
1886 | + */ | |
1887 | + | |
1888 | +#ifndef REGEXP_H | |
1889 | +#define REGEXP_H | |
1890 | + | |
1891 | + | |
1892 | +/* | |
1893 | +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h , | |
1894 | +which contains a version of this library, says: | |
1895 | + | |
1896 | + * | |
1897 | + * NSUBEXP must be at least 10, and no greater than 117 or the parser | |
1898 | + * will not work properly. | |
1899 | + * | |
1900 | + | |
1901 | +However, it looks rather like this library is limited to 10. If you think | |
1902 | +otherwise, let us know. | |
1903 | +*/ | |
1904 | + | |
1905 | +#define NSUBEXP 10 | |
1906 | +typedef struct regexp { | |
1907 | + char *startp[NSUBEXP]; | |
1908 | + char *endp[NSUBEXP]; | |
1909 | + char regstart; /* Internal use only. */ | |
1910 | + char reganch; /* Internal use only. */ | |
1911 | + char *regmust; /* Internal use only. */ | |
1912 | + int regmlen; /* Internal use only. */ | |
1913 | + char program[1]; /* Unwarranted chumminess with compiler. */ | |
1914 | +} regexp; | |
1915 | + | |
1916 | +regexp * regcomp(char *exp, int *patternsize); | |
1917 | +int regexec(regexp *prog, char *string); | |
1918 | +void regsub(regexp *prog, char *source, char *dest); | |
1919 | +void regerror(char *s); | |
1920 | + | |
1921 | +#endif | |
9547f064 | 1922 | --- linux-2.6.25/net/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600 |
1923 | +++ linux-2.6.25-layer7/net/netfilter/regexp/regmagic.h 2008-04-29 00:40:01.000000000 -0500 | |
97491a6e | 1924 | @@ -0,0 +1,5 @@ |
1925 | +/* | |
1926 | + * The first byte of the regexp internal "program" is actually this magic | |
1927 | + * number; the start node begins in the second byte. | |
1928 | + */ | |
1929 | +#define MAGIC 0234 | |
9547f064 | 1930 | --- linux-2.6.25/net/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600 |
1931 | +++ linux-2.6.25-layer7/net/netfilter/regexp/regsub.c 2008-04-29 00:40:01.000000000 -0500 | |
97491a6e | 1932 | @@ -0,0 +1,95 @@ |
1933 | +/* | |
1934 | + * regsub | |
1935 | + * @(#)regsub.c 1.3 of 2 April 86 | |
1936 | + * | |
1937 | + * Copyright (c) 1986 by University of Toronto. | |
1938 | + * Written by Henry Spencer. Not derived from licensed software. | |
1939 | + * | |
1940 | + * Permission is granted to anyone to use this software for any | |
1941 | + * purpose on any computer system, and to redistribute it freely, | |
1942 | + * subject to the following restrictions: | |
1943 | + * | |
1944 | + * 1. The author is not responsible for the consequences of use of | |
1945 | + * this software, no matter how awful, even if they arise | |
1946 | + * from defects in it. | |
1947 | + * | |
1948 | + * 2. The origin of this software must not be misrepresented, either | |
1949 | + * by explicit claim or by omission. | |
1950 | + * | |
1951 | + * 3. Altered versions must be plainly marked as such, and must not | |
1952 | + * be misrepresented as being the original software. | |
1953 | + * | |
1954 | + * | |
1955 | + * This code was modified by Ethan Sommer to work within the kernel | |
1956 | + * (it now uses kmalloc etc..) | |
1957 | + * | |
1958 | + */ | |
1959 | +#include "regexp.h" | |
1960 | +#include "regmagic.h" | |
1961 | +#include <linux/string.h> | |
1962 | + | |
1963 | + | |
1964 | +#ifndef CHARBITS | |
1965 | +#define UCHARAT(p) ((int)*(unsigned char *)(p)) | |
1966 | +#else | |
1967 | +#define UCHARAT(p) ((int)*(p)&CHARBITS) | |
1968 | +#endif | |
1969 | + | |
1970 | +#if 0 | |
1971 | +//void regerror(char * s) | |
1972 | +//{ | |
1973 | +// printk("regexp(3): %s", s); | |
1974 | +// /* NOTREACHED */ | |
1975 | +//} | |
1976 | +#endif | |
1977 | + | |
1978 | +/* | |
1979 | + - regsub - perform substitutions after a regexp match | |
1980 | + */ | |
1981 | +void | |
1982 | +regsub(regexp * prog, char * source, char * dest) | |
1983 | +{ | |
1984 | + register char *src; | |
1985 | + register char *dst; | |
1986 | + register char c; | |
1987 | + register int no; | |
1988 | + register int len; | |
1989 | + | |
1990 | + /* Not necessary and gcc doesn't like it -MLS */ | |
1991 | + /*extern char *strncpy();*/ | |
1992 | + | |
1993 | + if (prog == NULL || source == NULL || dest == NULL) { | |
1994 | + regerror("NULL parm to regsub"); | |
1995 | + return; | |
1996 | + } | |
1997 | + if (UCHARAT(prog->program) != MAGIC) { | |
1998 | + regerror("damaged regexp fed to regsub"); | |
1999 | + return; | |
2000 | + } | |
2001 | + | |
2002 | + src = source; | |
2003 | + dst = dest; | |
2004 | + while ((c = *src++) != '\0') { | |
2005 | + if (c == '&') | |
2006 | + no = 0; | |
2007 | + else if (c == '\\' && '0' <= *src && *src <= '9') | |
2008 | + no = *src++ - '0'; | |
2009 | + else | |
2010 | + no = -1; | |
2011 | + | |
2012 | + if (no < 0) { /* Ordinary character. */ | |
2013 | + if (c == '\\' && (*src == '\\' || *src == '&')) | |
2014 | + c = *src++; | |
2015 | + *dst++ = c; | |
2016 | + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) { | |
2017 | + len = prog->endp[no] - prog->startp[no]; | |
2018 | + (void) strncpy(dst, prog->startp[no], len); | |
2019 | + dst += len; | |
2020 | + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */ | |
2021 | + regerror("damaged match string"); | |
2022 | + return; | |
2023 | + } | |
2024 | + } | |
2025 | + } | |
2026 | + *dst++ = '\0'; | |
2027 | +} | |
9547f064 | 2028 | --- linux-2.6.25/net/netfilter/nf_conntrack_core.c 2008-04-16 21:49:44.000000000 -0500 |
2029 | +++ linux-2.6.25-layer7/net/netfilter/nf_conntrack_core.c 2008-04-29 00:40:01.000000000 -0500 | |
2030 | @@ -210,6 +210,14 @@ destroy_conntrack(struct nf_conntrack *n | |
6890440f | 2031 | * too. */ |
2032 | nf_ct_remove_expectations(ct); | |
2033 | ||
a06ee22f | 2034 | + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE) |
6890440f | 2035 | + if(ct->layer7.app_proto) |
2036 | + kfree(ct->layer7.app_proto); | |
2037 | + if(ct->layer7.app_data) | |
a06ee22f | 2038 | + kfree(ct->layer7.app_data); |
2039 | + #endif | |
2040 | + | |
6890440f | 2041 | + |
2042 | /* We overload first tuple to link into unconfirmed list. */ | |
2043 | if (!nf_ct_is_confirmed(ct)) { | |
9547f064 | 2044 | BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode)); |
2045 | --- linux-2.6.25/net/netfilter/nf_conntrack_standalone.c 2008-04-16 21:49:44.000000000 -0500 | |
2046 | +++ linux-2.6.25-layer7/net/netfilter/nf_conntrack_standalone.c 2008-04-29 00:43:17.000000000 -0500 | |
2047 | @@ -181,6 +181,12 @@ static int ct_seq_show(struct seq_file * | |
6890440f | 2048 | return -ENOSPC; |
2049 | #endif | |
2050 | ||
a06ee22f | 2051 | +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE) |
9547f064 | 2052 | + if(ct->layer7.app_proto && |
2053 | + seq_printf(s, "l7proto=%s ", ct->layer7.app_proto)) | |
2054 | + return -ENOSPC; | |
6890440f | 2055 | +#endif |
9547f064 | 2056 | + |
9e282013 | 2057 | if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use))) |
6890440f | 2058 | return -ENOSPC; |
9547f064 | 2059 | |
2060 | --- linux-2.6.25/include/net/netfilter/nf_conntrack.h 2008-04-16 21:49:44.000000000 -0500 | |
2061 | +++ linux-2.6.25-layer7/include/net/netfilter/nf_conntrack.h 2008-04-29 00:40:01.000000000 -0500 | |
2062 | @@ -124,6 +124,22 @@ struct nf_conn | |
a06ee22f | 2063 | u_int32_t secmark; |
2064 | #endif | |
2065 | ||
2066 | +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \ | |
2067 | + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE) | |
2068 | + struct { | |
2069 | + /* | |
2070 | + * e.g. "http". NULL before decision. "unknown" after decision | |
2071 | + * if no match. | |
2072 | + */ | |
2073 | + char *app_proto; | |
2074 | + /* | |
2075 | + * application layer data so far. NULL after match decision. | |
2076 | + */ | |
2077 | + char *app_data; | |
2078 | + unsigned int app_data_len; | |
2079 | + } layer7; | |
2080 | +#endif | |
2081 | + | |
2082 | /* Storage reserved for other modules: */ | |
2083 | union nf_conntrack_proto proto; | |
2084 | ||
9547f064 | 2085 | --- linux-2.6.25/include/linux/netfilter/xt_layer7.h 1969-12-31 18:00:00.000000000 -0600 |
2086 | +++ linux-2.6.25-layer7/include/linux/netfilter/xt_layer7.h 2008-04-29 00:40:01.000000000 -0500 | |
a06ee22f | 2087 | @@ -0,0 +1,13 @@ |
2088 | +#ifndef _XT_LAYER7_H | |
2089 | +#define _XT_LAYER7_H | |
2090 | + | |
2091 | +#define MAX_PATTERN_LEN 8192 | |
2092 | +#define MAX_PROTOCOL_LEN 256 | |
2093 | + | |
2094 | +struct xt_layer7_info { | |
2095 | + char protocol[MAX_PROTOCOL_LEN]; | |
2096 | + char pattern[MAX_PATTERN_LEN]; | |
2097 | + u_int8_t invert; | |
2098 | +}; | |
2099 | + | |
2100 | +#endif /* _XT_LAYER7_H */ |