1 --- linux-2.6.16.14-stock/include/linux/netfilter_ipv4/ip_conntrack.h 2006-05-04 19:03:45.000000000 -0500
2 +++ linux-2.6.16.14-layer7/include/linux/netfilter_ipv4/ip_conntrack.h 2006-05-08 22:57:35.000000000 -0500
3 @@ -122,6 +122,15 @@ struct ip_conntrack
4 /* Traversed often, so hopefully in different cacheline to top */
5 /* These are my tuples; original and reply */
6 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
8 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
10 + char * app_proto; /* e.g. "http". NULL before decision. "unknown" after decision if no match */
11 + char * app_data; /* application layer data so far. NULL after match decision */
12 + unsigned int app_data_len;
18 struct ip_conntrack_expect
19 --- linux-2.6.16.14-stock/include/linux/netfilter_ipv4/ipt_layer7.h 1969-12-31 18:00:00.000000000 -0600
20 +++ linux-2.6.16.14-layer7/include/linux/netfilter_ipv4/ipt_layer7.h 2006-05-08 22:57:35.000000000 -0500
23 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
24 + http://l7-filter.sf.net
26 + This program is free software; you can redistribute it and/or
27 + modify it under the terms of the GNU General Public License
28 + as published by the Free Software Foundation; either version
29 + 2 of the License, or (at your option) any later version.
30 + http://www.gnu.org/licenses/gpl.txt
33 +#ifndef _IPT_LAYER7_H
34 +#define _IPT_LAYER7_H
36 +#define MAX_PATTERN_LEN 8192
37 +#define MAX_PROTOCOL_LEN 256
39 +typedef char *(*proc_ipt_search) (char *, char, char *);
41 +struct ipt_layer7_info {
42 + char protocol[MAX_PROTOCOL_LEN];
44 + char pattern[MAX_PATTERN_LEN];
47 +#endif /* _IPT_LAYER7_H */
48 --- linux-2.6.16.14-stock/net/ipv4/netfilter/Kconfig 2006-05-04 19:03:45.000000000 -0500
49 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/Kconfig 2006-05-08 23:01:14.000000000 -0500
50 @@ -201,6 +201,24 @@ config IP_NF_MATCH_IPRANGE
52 To compile it as a module, choose M here. If unsure, say N.
54 +config IP_NF_MATCH_LAYER7
55 + tristate "Layer 7 match support (EXPERIMENTAL)"
56 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
58 + Say Y if you want to be able to classify connections (and their
59 + packets) based on regular expression matching of their application
60 + layer data. This is one way to classify applications such as
61 + peer-to-peer filesharing systems that do not always use the same
64 + To compile it as a module, choose M here. If unsure, say N.
66 +config IP_NF_MATCH_LAYER7_DEBUG
67 + bool "Layer 7 debugging output"
68 + depends on IP_NF_MATCH_LAYER7
70 + Say Y to get lots of debugging output.
72 config IP_NF_MATCH_MULTIPORT
73 tristate "Multiple port match support"
74 depends on IP_NF_IPTABLES
75 --- linux-2.6.16.14-stock/net/ipv4/netfilter/Makefile 2006-05-04 19:03:45.000000000 -0500
76 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/Makefile 2006-05-08 22:57:35.000000000 -0500
77 @@ -59,6 +59,8 @@ obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl
78 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
79 obj-$(CONFIG_IP_NF_MATCH_POLICY) += ipt_policy.o
81 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
84 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
85 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
86 --- linux-2.6.16.14-stock/net/ipv4/netfilter/ip_conntrack_core.c 2006-05-04 19:03:45.000000000 -0500
87 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/ip_conntrack_core.c 2006-05-08 22:57:35.000000000 -0500
88 @@ -339,6 +339,13 @@ destroy_conntrack(struct nf_conntrack *n
90 ip_ct_remove_expectations(ct);
92 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
93 + if(ct->layer7.app_proto)
94 + kfree(ct->layer7.app_proto);
95 + if(ct->layer7.app_data)
96 + kfree(ct->layer7.app_data);
99 /* We overload first tuple to link into unconfirmed list. */
100 if (!is_confirmed(ct)) {
101 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
102 --- linux-2.6.16.14-stock/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-05-04 19:03:45.000000000 -0500
103 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-05-08 22:57:35.000000000 -0500
104 @@ -189,6 +189,12 @@ static int ct_seq_show(struct seq_file *
108 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
109 + if(conntrack->layer7.app_proto)
110 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
114 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
117 --- linux-2.6.16.14-stock/net/ipv4/netfilter/ipt_layer7.c 1969-12-31 18:00:00.000000000 -0600
118 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/ipt_layer7.c 2006-05-09 00:44:11.000000000 -0500
121 + Kernel module to match application layer (OSI layer 7)
122 + data in connections.
124 + http://l7-filter.sf.net
126 + By Matthew Strait and Ethan Sommer, 2003-2005.
128 + This program is free software; you can redistribute it and/or
129 + modify it under the terms of the GNU General Public License
130 + as published by the Free Software Foundation; either version
131 + 2 of the License, or (at your option) any later version.
132 + http://www.gnu.org/licenses/gpl.txt
134 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
135 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
138 +#include <linux/module.h>
139 +#include <linux/skbuff.h>
140 +#include <linux/netfilter_ipv4/ip_conntrack.h>
141 +#include <linux/proc_fs.h>
142 +#include <linux/ctype.h>
144 +#include <net/tcp.h>
145 +#include <linux/spinlock.h>
147 +#include "regexp/regexp.c"
149 +#include <linux/netfilter_ipv4/ipt_layer7.h>
150 +#include <linux/netfilter_ipv4/ip_tables.h>
152 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
153 +MODULE_LICENSE("GPL");
154 +MODULE_DESCRIPTION("iptables application layer match module");
155 +MODULE_VERSION("2.0");
157 +static int maxdatalen = 2048; // this is the default
158 +module_param(maxdatalen, int, 0444);
159 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
161 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
162 + #define DPRINTK(format,args...) printk(format,##args)
164 + #define DPRINTK(format,args...)
167 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
168 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
170 +/* Number of packets whose data we look at.
171 +This can be modified through /proc/net/layer7_numpackets */
172 +static int num_packets = 10;
174 +static struct pattern_cache {
175 + char * regex_string;
177 + struct pattern_cache * next;
178 +} * first_pattern_cache = NULL;
180 +/* I'm new to locking. Here are my assumptions:
182 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
183 + if they did, nothing awful would happen.
185 +- This code will never be processing the same packet twice at the same time,
186 + because iptables rules are traversed in order.
188 +- It doesn't matter if two packets from different connections are in here at
189 + the same time, because they don't share any data.
191 +- It _does_ matter if two packets from the same connection are here at the same
192 + time. In this case, we have to protect the conntracks and the list of
195 +DEFINE_RWLOCK(ct_lock);
196 +DEFINE_SPINLOCK(list_lock);
198 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
199 +/* Converts an unfriendly string into a friendly one by
200 +replacing unprintables with periods and all whitespace with " ". */
201 +static char * friendly_print(unsigned char * s)
203 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
207 + if (net_ratelimit())
208 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
212 + for(i = 0; i < strlen(s); i++){
213 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
214 + else if(isspace(s[i])) f[i] = ' ';
221 +static char dec2hex(int i)
225 + return (char)(i + '0');
228 + return (char)(i - 10 + 'a');
231 + if (net_ratelimit())
232 + printk("Problem in dec2hex\n");
237 +static char * hex_print(unsigned char * s)
239 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
243 + if (net_ratelimit())
244 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
248 + for(i = 0; i < strlen(s); i++) {
249 + g[i*3 ] = dec2hex(s[i]/16);
250 + g[i*3 + 1] = dec2hex(s[i]%16);
259 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
260 +over again, it make sense to cache the results. */
261 +static regexp * compile_and_cache(char * regex_string, char * protocol)
263 + struct pattern_cache * node = first_pattern_cache;
264 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
265 + struct pattern_cache * tmp;
268 + while (node != NULL) {
269 + if (!strcmp(node->regex_string, regex_string))
270 + return node->pattern;
272 + last_pattern_cache = node;/* points at the last non-NULL node */
276 + /* If we reach the end of the list, then we have not yet cached
277 + the pattern for this regex. Let's do that now.
278 + Be paranoid about running out of memory to avoid list corruption. */
279 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
282 + if (net_ratelimit())
283 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
287 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
288 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
291 + if(!tmp->regex_string || !tmp->pattern) {
292 + if (net_ratelimit())
293 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
294 + kfree(tmp->regex_string);
295 + kfree(tmp->pattern);
300 + /* Ok. The new node is all ready now. */
303 + if(first_pattern_cache == NULL) /* list is empty */
304 + first_pattern_cache = node; /* make node the beginning */
306 + last_pattern_cache->next = node; /* attach node to the end */
308 + /* copy the string and compile the regex */
309 + len = strlen(regex_string);
310 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
311 + node->pattern = regcomp(regex_string, &len);
312 + if ( !node->pattern ) {
313 + if (net_ratelimit())
314 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
315 + /* pattern is now cached as NULL, so we won't try again. */
318 + strcpy(node->regex_string, regex_string);
319 + return node->pattern;
322 +static int can_handle(const struct sk_buff *skb)
324 + if(!skb->nh.iph) /* not IP */
326 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
327 + skb->nh.iph->protocol != IPPROTO_UDP &&
328 + skb->nh.iph->protocol != IPPROTO_ICMP)
333 +/* Returns offset the into the skb->data that the application data starts */
334 +static int app_data_offset(const struct sk_buff *skb)
336 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
337 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
338 + int ip_hl = 4*skb->nh.iph->ihl;
340 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
341 + /* 12 == offset into TCP header for the header length field.
342 + Can't get this with skb->h.th->doff because the tcphdr
343 + struct doesn't get set when routing (this is confirmed to be
344 + true in Netfilter as well as QoS.) */
345 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
347 + return ip_hl + tcp_hl;
348 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
349 + return ip_hl + 8; /* UDP header is always 8 bytes */
350 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
351 + return ip_hl + 8; /* ICMP header is 8 bytes */
353 + if (net_ratelimit())
354 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
355 + return ip_hl + 8; /* something reasonable */
359 +/* handles whether there's a match when we aren't appending data anymore */
360 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
361 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
362 + struct ipt_layer7_info * info)
364 + /* If we're in here, throw the app data away */
365 + write_lock(&ct_lock);
366 + if(master_conntrack->layer7.app_data != NULL) {
368 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
369 + if(!master_conntrack->layer7.app_proto) {
370 + char * f = friendly_print(master_conntrack->layer7.app_data);
371 + char * g = hex_print(master_conntrack->layer7.app_data);
372 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
373 + strlen(f), TOTAL_PACKETS, f);
375 + DPRINTK("In hex: %s\n", g);
380 + kfree(master_conntrack->layer7.app_data);
381 + master_conntrack->layer7.app_data = NULL; /* don't free again */
383 + write_unlock(&ct_lock);
385 + if(master_conntrack->layer7.app_proto){
386 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
387 + write_lock(&ct_lock);
388 + if(!conntrack->layer7.app_proto) {
389 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
390 + if(!conntrack->layer7.app_proto){
391 + if (net_ratelimit())
392 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
393 + write_unlock(&ct_lock);
396 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
398 + write_unlock(&ct_lock);
400 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
403 + /* If not classified, set to "unknown" to distinguish from
404 + connections that are still being tested. */
405 + write_lock(&ct_lock);
406 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
407 + if(!master_conntrack->layer7.app_proto){
408 + if (net_ratelimit())
409 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
410 + write_unlock(&ct_lock);
413 + strcpy(master_conntrack->layer7.app_proto, "unknown");
414 + write_unlock(&ct_lock);
419 +/* add the new app data to the conntrack. Return number of bytes added. */
420 +static int add_data(struct ip_conntrack * master_conntrack,
421 + char * app_data, int appdatalen)
424 + int oldlength = master_conntrack->layer7.app_data_len;
426 + /* Strip nulls. Make everything lower case (our regex lib doesn't
427 + do case insensitivity). Add it to the end of the current data. */
428 + for(i = 0; i < maxdatalen-oldlength-1 &&
429 + i < appdatalen; i++) {
430 + if(app_data[i] != '\0') {
431 + master_conntrack->layer7.app_data[length+oldlength] =
432 + /* the kernel version of tolower mungs 'upper ascii' */
433 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
438 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
439 + master_conntrack->layer7.app_data_len = length + oldlength;
444 +/* Returns true on match and false otherwise. */
445 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
446 + const struct net_device *out, const void *matchinfo,
447 + int offset, int *hotdrop)
449 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
450 + enum ip_conntrack_info master_ctinfo, ctinfo;
451 + struct ip_conntrack *master_conntrack, *conntrack;
452 + unsigned char * app_data;
453 + unsigned int pattern_result, appdatalen;
454 + regexp * comppattern;
456 + if(!can_handle(skb)){
457 + DPRINTK("layer7: This is some protocol I can't handle.\n");
458 + return info->invert;
461 + /* Treat parent & all its children together as one connection, except
462 + for the purpose of setting conntrack->layer7.app_proto in the actual
463 + connection. This makes /proc/net/ip_conntrack more satisfying. */
464 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
465 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
466 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
467 + return info->invert;
470 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
471 + while (master_ct(master_conntrack) != NULL)
472 + master_conntrack = master_ct(master_conntrack);
474 + /* if we've classified it or seen too many packets */
475 + if(TOTAL_PACKETS > num_packets ||
476 + master_conntrack->layer7.app_proto) {
478 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
480 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
481 + rules. I'm not sure that using cb for this purpose is correct, although
482 + it says "put your private variables there". But it doesn't look like it
483 + is being used for anything else in the skbs that make it here. How can
484 + I write to cb without making the compiler angry? */
485 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
487 + return (pattern_result ^ info->invert);
490 + if(skb_is_nonlinear(skb)){
491 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
492 + if (net_ratelimit())
493 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
494 + return info->invert;
498 + /* now that the skb is linearized, it's safe to set these. */
499 + app_data = skb->data + app_data_offset(skb);
500 + appdatalen = skb->tail - app_data;
502 + spin_lock_bh(&list_lock);
503 + /* the return value gets checked later, when we're ready to use it */
504 + comppattern = compile_and_cache(info->pattern, info->protocol);
505 + spin_unlock_bh(&list_lock);
507 + /* On the first packet of a connection, allocate space for app data */
508 + write_lock(&ct_lock);
509 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
510 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
511 + if(!master_conntrack->layer7.app_data){
512 + if (net_ratelimit())
513 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
514 + write_unlock(&ct_lock);
515 + return info->invert;
518 + master_conntrack->layer7.app_data[0] = '\0';
520 + write_unlock(&ct_lock);
522 + /* Can be here, but unallocated, if numpackets is increased near
523 + the beginning of a connection */
524 + if(master_conntrack->layer7.app_data == NULL)
525 + return (info->invert); /* unmatched */
529 + write_lock(&ct_lock);
530 + newbytes = add_data(master_conntrack, app_data, appdatalen);
531 + write_unlock(&ct_lock);
533 + if(newbytes == 0) { /* didn't add any data */
535 + /* Didn't match before, not going to match now */
536 + return info->invert;
540 + /* If looking for "unknown", then never match. "Unknown" means that
541 + we've given up; we're still trying with these packets. */
542 + if(!strcmp(info->protocol, "unknown")) {
543 + pattern_result = 0;
544 + /* If the regexp failed to compile, don't bother running it */
545 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
546 + DPRINTK("layer7: matched %s\n", info->protocol);
547 + pattern_result = 1;
548 + } else pattern_result = 0;
550 + if(pattern_result) {
551 + write_lock(&ct_lock);
552 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
553 + if(!master_conntrack->layer7.app_proto){
554 + if (net_ratelimit())
555 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
556 + write_unlock(&ct_lock);
557 + return (pattern_result ^ info->invert);
559 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
560 + write_unlock(&ct_lock);
563 + /* mark the packet seen */
566 + return (pattern_result ^ info->invert);
569 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
570 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
572 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
577 +static struct ipt_match layer7_match = {
580 + .checkentry = &checkentry,
584 +/* taken from drivers/video/modedb.c */
585 +static int my_atoi(const char *s)
592 + val = 10*val+(*s-'0');
600 +/* write out num_packets to userland. */
601 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
602 + int* eof, void * data)
604 + if(num_packets > 99 && net_ratelimit())
605 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
607 + page[0] = num_packets/10 + '0';
608 + page[1] = num_packets%10 + '0';
617 +/* Read in num_packets from userland */
618 +static int layer7_write_proc(struct file* file, const char* buffer,
619 + unsigned long count, void *data)
621 + char * foo = kmalloc(count, GFP_ATOMIC);
624 + if (net_ratelimit())
625 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
629 + if(copy_from_user(foo, buffer, count)) {
634 + num_packets = my_atoi(foo);
637 + /* This has an arbitrary limit to make the math easier. I'm lazy.
638 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
639 + if(num_packets > 99) {
640 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
642 + } else if(num_packets < 1) {
643 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
650 +/* register the proc file */
651 +static void layer7_init_proc(void)
653 + struct proc_dir_entry* entry;
654 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
655 + entry->read_proc = layer7_read_proc;
656 + entry->write_proc = layer7_write_proc;
659 +static void layer7_cleanup_proc(void)
661 + remove_proc_entry("layer7_numpackets", proc_net);
664 +static int __init init(void)
666 + layer7_init_proc();
667 + if(maxdatalen < 1) {
668 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
671 + /* This is not a hard limit. It's just here to prevent people from
672 + bringing their slow machines to a grinding halt. */
673 + else if(maxdatalen > 65536) {
674 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
675 + maxdatalen = 65536;
677 + return ipt_register_match(&layer7_match);
680 +static void __exit fini(void)
682 + layer7_cleanup_proc();
683 + ipt_unregister_match(&layer7_match);
688 --- linux-2.6.16.14-stock/net/ipv4/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
689 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/regexp/regexp.c 2006-05-08 22:57:35.000000000 -0500
692 + * regcomp and regexec -- regsub and regerror are elsewhere
693 + * @(#)regexp.c 1.3 of 18 April 87
695 + * Copyright (c) 1986 by University of Toronto.
696 + * Written by Henry Spencer. Not derived from licensed software.
698 + * Permission is granted to anyone to use this software for any
699 + * purpose on any computer system, and to redistribute it freely,
700 + * subject to the following restrictions:
702 + * 1. The author is not responsible for the consequences of use of
703 + * this software, no matter how awful, even if they arise
704 + * from defects in it.
706 + * 2. The origin of this software must not be misrepresented, either
707 + * by explicit claim or by omission.
709 + * 3. Altered versions must be plainly marked as such, and must not
710 + * be misrepresented as being the original software.
712 + * Beware that some of this code is subtly aware of the way operator
713 + * precedence is structured in regular expressions. Serious changes in
714 + * regular-expression syntax might require a total rethink.
716 + * This code was modified by Ethan Sommer to work within the kernel
717 + * (it now uses kmalloc etc..)
719 + * Modified slightly by Matthew Strait to use more modern C.
723 +#include "regmagic.h"
725 +/* added by ethan and matt. Lets it work in both kernel and user space.
726 +(So iptables can use it, for instance.) Yea, it goes both ways... */
728 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
730 + #define printk(format,args...) printf(format,##args)
733 +void regerror(char * s)
735 + printk("<3>Regexp: %s\n", s);
740 + * The "internal use only" fields in regexp.h are present to pass info from
741 + * compile to execute that permits the execute phase to run lots faster on
742 + * simple cases. They are:
744 + * regstart char that must begin a match; '\0' if none obvious
745 + * reganch is the match anchored (at beginning-of-line only)?
746 + * regmust string (pointer into program) that match must include, or NULL
747 + * regmlen length of regmust string
749 + * Regstart and reganch permit very fast decisions on suitable starting points
750 + * for a match, cutting down the work a lot. Regmust permits fast rejection
751 + * of lines that cannot possibly match. The regmust tests are costly enough
752 + * that regcomp() supplies a regmust only if the r.e. contains something
753 + * potentially expensive (at present, the only such thing detected is * or +
754 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
755 + * supplied because the test in regexec() needs it and regcomp() is computing
760 + * Structure for regexp "program". This is essentially a linear encoding
761 + * of a nondeterministic finite-state machine (aka syntax charts or
762 + * "railroad normal form" in parsing technology). Each node is an opcode
763 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
764 + * all nodes except BRANCH implement concatenation; a "next" pointer with
765 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
766 + * have one of the subtle syntax dependencies: an individual BRANCH (as
767 + * opposed to a collection of them) is never concatenated with anything
768 + * because of operator precedence.) The operand of some types of node is
769 + * a literal string; for others, it is a node leading into a sub-FSM. In
770 + * particular, the operand of a BRANCH node is the first node of the branch.
771 + * (NB this is *not* a tree structure: the tail of the branch connects
772 + * to the thing following the set of BRANCHes.) The opcodes are:
775 +/* definition number opnd? meaning */
776 +#define END 0 /* no End of program. */
777 +#define BOL 1 /* no Match "" at beginning of line. */
778 +#define EOL 2 /* no Match "" at end of line. */
779 +#define ANY 3 /* no Match any one character. */
780 +#define ANYOF 4 /* str Match any character in this string. */
781 +#define ANYBUT 5 /* str Match any character not in this string. */
782 +#define BRANCH 6 /* node Match this alternative, or the next... */
783 +#define BACK 7 /* no Match "", "next" ptr points backward. */
784 +#define EXACTLY 8 /* str Match this string. */
785 +#define NOTHING 9 /* no Match empty string. */
786 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
787 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
788 +#define OPEN 20 /* no Mark this point in input as start of #n. */
789 + /* OPEN+1 is number 1, etc. */
790 +#define CLOSE 30 /* no Analogous to OPEN. */
795 + * BRANCH The set of branches constituting a single choice are hooked
796 + * together with their "next" pointers, since precedence prevents
797 + * anything being concatenated to any individual branch. The
798 + * "next" pointer of the last BRANCH in a choice points to the
799 + * thing following the whole choice. This is also where the
800 + * final "next" pointer of each individual branch points; each
801 + * branch starts with the operand node of a BRANCH node.
803 + * BACK Normal "next" pointers all implicitly point forward; BACK
804 + * exists to make loop structures possible.
806 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
807 + * BRANCH structures using BACK. Simple cases (one character
808 + * per match) are implemented with STAR and PLUS for speed
809 + * and to minimize recursive plunges.
811 + * OPEN,CLOSE ...are numbered at compile time.
815 + * A node is one char of opcode followed by two chars of "next" pointer.
816 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
817 + * value is a positive offset from the opcode of the node containing it.
818 + * An operand, if any, simply follows the node. (Note that much of the
819 + * code generation knows about this implicit relationship.)
821 + * Using two bytes for the "next" pointer is vast overkill for most things,
822 + * but allows patterns to get big without disasters.
824 +#define OP(p) (*(p))
825 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
826 +#define OPERAND(p) ((p) + 3)
829 + * See regmagic.h for one further detail of program structure.
834 + * Utility definitions.
837 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
839 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
842 +#define FAIL(m) { regerror(m); return(NULL); }
843 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
844 +#define META "^$.[()|?+*\\"
847 + * Flags to be passed up and down.
849 +#define HASWIDTH 01 /* Known never to match null string. */
850 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
851 +#define SPSTART 04 /* Starts with * or +. */
852 +#define WORST 0 /* Worst case. */
855 + * Global work variables for regcomp().
857 +static char *regparse; /* Input-scan pointer. */
858 +static int regnpar; /* () count. */
859 +static char regdummy;
860 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
861 +static long regsize; /* Code size. */
864 + * Forward declarations for regcomp()'s friends.
867 +#define STATIC static
869 +STATIC char *reg(int paren,int *flagp);
870 +STATIC char *regbranch(int *flagp);
871 +STATIC char *regpiece(int *flagp);
872 +STATIC char *regatom(int *flagp);
873 +STATIC char *regnode(char op);
874 +STATIC char *regnext(char *p);
875 +STATIC void regc(char b);
876 +STATIC void reginsert(char op, char *opnd);
877 +STATIC void regtail(char *p, char *val);
878 +STATIC void regoptail(char *p, char *val);
881 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
888 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
889 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
890 + if (*scan1 == *scan2++)
898 + - regcomp - compile a regular expression into internal code
900 + * We can't allocate space until we know how big the compiled form will be,
901 + * but we can't compile it (and thus know how big it is) until we've got a
902 + * place to put the code. So we cheat: we compile it twice, once with code
903 + * generation turned off and size counting turned on, and once "for real".
904 + * This also means that we don't allocate space until we are sure that the
905 + * thing really will compile successfully, and we never have to move the
906 + * code and thus invalidate pointers into it. (Note that it has to be in
907 + * one piece because free() must be able to free it all.)
909 + * Beware that the optimization-preparation code in here knows about some
910 + * of the structure of the compiled regexp.
913 +regcomp(char *exp,int *patternsize)
915 + register regexp *r;
916 + register char *scan;
917 + register char *longest;
920 + /* commented out by ethan
921 + extern char *malloc();
925 + FAIL("NULL argument");
927 + /* First pass: determine size, legality. */
931 + regcode = ®dummy;
933 + if (reg(0, &flags) == NULL)
936 + /* Small enough for pointer-storage convention? */
937 + if (regsize >= 32767L) /* Probably could be 65535L. */
938 + FAIL("regexp too big");
940 + /* Allocate space. */
941 + *patternsize=sizeof(regexp) + (unsigned)regsize;
942 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
944 + FAIL("out of space");
946 + /* Second pass: emit code. */
949 + regcode = r->program;
951 + if (reg(0, &flags) == NULL)
954 + /* Dig out information for optimizations. */
955 + r->regstart = '\0'; /* Worst-case defaults. */
959 + scan = r->program+1; /* First BRANCH. */
960 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
961 + scan = OPERAND(scan);
963 + /* Starting-point info. */
964 + if (OP(scan) == EXACTLY)
965 + r->regstart = *OPERAND(scan);
966 + else if (OP(scan) == BOL)
970 + * If there's something expensive in the r.e., find the
971 + * longest literal string that must appear and make it the
972 + * regmust. Resolve ties in favor of later strings, since
973 + * the regstart check works with the beginning of the r.e.
974 + * and avoiding duplication strengthens checking. Not a
975 + * strong reason, but sufficient in the absence of others.
977 + if (flags&SPSTART) {
980 + for (; scan != NULL; scan = regnext(scan))
981 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
982 + longest = OPERAND(scan);
983 + len = strlen(OPERAND(scan));
985 + r->regmust = longest;
994 + - reg - regular expression, i.e. main body or parenthesized thing
996 + * Caller must absorb opening parenthesis.
998 + * Combining parenthesis handling with the base level of regular expression
999 + * is a trifle forced, but the need to tie the tails of the branches to what
1000 + * follows makes it hard to avoid.
1003 +reg(int paren, int *flagp /* Parenthesized? */ )
1005 + register char *ret;
1006 + register char *br;
1007 + register char *ender;
1008 + register int parno = 0; /* 0 makes gcc happy */
1011 + *flagp = HASWIDTH; /* Tentatively. */
1013 + /* Make an OPEN node, if parenthesized. */
1015 + if (regnpar >= NSUBEXP)
1016 + FAIL("too many ()");
1019 + ret = regnode(OPEN+parno);
1023 + /* Pick up the branches, linking them together. */
1024 + br = regbranch(&flags);
1028 + regtail(ret, br); /* OPEN -> first. */
1031 + if (!(flags&HASWIDTH))
1032 + *flagp &= ~HASWIDTH;
1033 + *flagp |= flags&SPSTART;
1034 + while (*regparse == '|') {
1036 + br = regbranch(&flags);
1039 + regtail(ret, br); /* BRANCH -> BRANCH. */
1040 + if (!(flags&HASWIDTH))
1041 + *flagp &= ~HASWIDTH;
1042 + *flagp |= flags&SPSTART;
1045 + /* Make a closing node, and hook it on the end. */
1046 + ender = regnode((paren) ? CLOSE+parno : END);
1047 + regtail(ret, ender);
1049 + /* Hook the tails of the branches to the closing node. */
1050 + for (br = ret; br != NULL; br = regnext(br))
1051 + regoptail(br, ender);
1053 + /* Check for proper termination. */
1054 + if (paren && *regparse++ != ')') {
1055 + FAIL("unmatched ()");
1056 + } else if (!paren && *regparse != '\0') {
1057 + if (*regparse == ')') {
1058 + FAIL("unmatched ()");
1060 + FAIL("junk on end"); /* "Can't happen". */
1068 + - regbranch - one alternative of an | operator
1070 + * Implements the concatenation operator.
1073 +regbranch(int *flagp)
1075 + register char *ret;
1076 + register char *chain;
1077 + register char *latest;
1080 + *flagp = WORST; /* Tentatively. */
1082 + ret = regnode(BRANCH);
1084 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1085 + latest = regpiece(&flags);
1086 + if (latest == NULL)
1088 + *flagp |= flags&HASWIDTH;
1089 + if (chain == NULL) /* First piece. */
1090 + *flagp |= flags&SPSTART;
1092 + regtail(chain, latest);
1095 + if (chain == NULL) /* Loop ran zero times. */
1096 + (void) regnode(NOTHING);
1102 + - regpiece - something followed by possible [*+?]
1104 + * Note that the branching code sequences used for ? and the general cases
1105 + * of * and + are somewhat optimized: they use the same NOTHING node as
1106 + * both the endmarker for their branch list and the body of the last branch.
1107 + * It might seem that this node could be dispensed with entirely, but the
1108 + * endmarker role is not redundant.
1111 +regpiece(int *flagp)
1113 + register char *ret;
1115 + register char *next;
1118 + ret = regatom(&flags);
1123 + if (!ISMULT(op)) {
1128 + if (!(flags&HASWIDTH) && op != '?')
1129 + FAIL("*+ operand could be empty");
1130 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1132 + if (op == '*' && (flags&SIMPLE))
1133 + reginsert(STAR, ret);
1134 + else if (op == '*') {
1135 + /* Emit x* as (x&|), where & means "self". */
1136 + reginsert(BRANCH, ret); /* Either x */
1137 + regoptail(ret, regnode(BACK)); /* and loop */
1138 + regoptail(ret, ret); /* back */
1139 + regtail(ret, regnode(BRANCH)); /* or */
1140 + regtail(ret, regnode(NOTHING)); /* null. */
1141 + } else if (op == '+' && (flags&SIMPLE))
1142 + reginsert(PLUS, ret);
1143 + else if (op == '+') {
1144 + /* Emit x+ as x(&|), where & means "self". */
1145 + next = regnode(BRANCH); /* Either */
1146 + regtail(ret, next);
1147 + regtail(regnode(BACK), ret); /* loop back */
1148 + regtail(next, regnode(BRANCH)); /* or */
1149 + regtail(ret, regnode(NOTHING)); /* null. */
1150 + } else if (op == '?') {
1151 + /* Emit x? as (x|) */
1152 + reginsert(BRANCH, ret); /* Either x */
1153 + regtail(ret, regnode(BRANCH)); /* or */
1154 + next = regnode(NOTHING); /* null. */
1155 + regtail(ret, next);
1156 + regoptail(ret, next);
1159 + if (ISMULT(*regparse))
1160 + FAIL("nested *?+");
1166 + - regatom - the lowest level
1168 + * Optimization: gobbles an entire sequence of ordinary characters so that
1169 + * it can turn them into a single node, which is smaller to store and
1170 + * faster to run. Backslashed characters are exceptions, each becoming a
1171 + * separate node; the code is simpler that way and it's not worth fixing.
1174 +regatom(int *flagp)
1176 + register char *ret;
1179 + *flagp = WORST; /* Tentatively. */
1181 + switch (*regparse++) {
1183 + ret = regnode(BOL);
1186 + ret = regnode(EOL);
1189 + ret = regnode(ANY);
1190 + *flagp |= HASWIDTH|SIMPLE;
1193 + register int class;
1194 + register int classend;
1196 + if (*regparse == '^') { /* Complement of range. */
1197 + ret = regnode(ANYBUT);
1200 + ret = regnode(ANYOF);
1201 + if (*regparse == ']' || *regparse == '-')
1202 + regc(*regparse++);
1203 + while (*regparse != '\0' && *regparse != ']') {
1204 + if (*regparse == '-') {
1206 + if (*regparse == ']' || *regparse == '\0')
1209 + class = UCHARAT(regparse-2)+1;
1210 + classend = UCHARAT(regparse);
1211 + if (class > classend+1)
1212 + FAIL("invalid [] range");
1213 + for (; class <= classend; class++)
1218 + regc(*regparse++);
1221 + if (*regparse != ']')
1222 + FAIL("unmatched []");
1224 + *flagp |= HASWIDTH|SIMPLE;
1228 + ret = reg(1, &flags);
1231 + *flagp |= flags&(HASWIDTH|SPSTART);
1236 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1241 + FAIL("?+* follows nothing");
1244 + if (*regparse == '\0')
1245 + FAIL("trailing \\");
1246 + ret = regnode(EXACTLY);
1247 + regc(*regparse++);
1249 + *flagp |= HASWIDTH|SIMPLE;
1253 + register char ender;
1256 + len = my_strcspn((const char *)regparse, (const char *)META);
1258 + FAIL("internal disaster");
1259 + ender = *(regparse+len);
1260 + if (len > 1 && ISMULT(ender))
1261 + len--; /* Back off clear of ?+* operand. */
1262 + *flagp |= HASWIDTH;
1265 + ret = regnode(EXACTLY);
1267 + regc(*regparse++);
1279 + - regnode - emit a node
1281 +static char * /* Location. */
1284 + register char *ret;
1285 + register char *ptr;
1288 + if (ret == ®dummy) {
1295 + *ptr++ = '\0'; /* Null "next" pointer. */
1303 + - regc - emit (if appropriate) a byte of code
1308 + if (regcode != ®dummy)
1315 + - reginsert - insert an operator in front of already-emitted operand
1317 + * Means relocating the operand.
1320 +reginsert(char op, char* opnd)
1322 + register char *src;
1323 + register char *dst;
1324 + register char *place;
1326 + if (regcode == ®dummy) {
1334 + while (src > opnd)
1337 + place = opnd; /* Op node, where operand used to be. */
1344 + - regtail - set the next-pointer at the end of a node chain
1347 +regtail(char *p, char *val)
1349 + register char *scan;
1350 + register char *temp;
1351 + register int offset;
1353 + if (p == ®dummy)
1356 + /* Find last node. */
1359 + temp = regnext(scan);
1365 + if (OP(scan) == BACK)
1366 + offset = scan - val;
1368 + offset = val - scan;
1369 + *(scan+1) = (offset>>8)&0377;
1370 + *(scan+2) = offset&0377;
1374 + - regoptail - regtail on operand of first argument; nop if operandless
1377 +regoptail(char *p, char *val)
1379 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1380 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1382 + regtail(OPERAND(p), val);
1386 + * regexec and friends
1390 + * Global work variables for regexec().
1392 +static char *reginput; /* String-input pointer. */
1393 +static char *regbol; /* Beginning of input, for ^ check. */
1394 +static char **regstartp; /* Pointer to startp array. */
1395 +static char **regendp; /* Ditto for endp. */
1400 +STATIC int regtry(regexp *prog, char *string);
1401 +STATIC int regmatch(char *prog);
1402 +STATIC int regrepeat(char *p);
1405 +int regnarrate = 0;
1407 +STATIC char *regprop(char *op);
1411 + - regexec - match a regexp against a string
1414 +regexec(regexp *prog, char *string)
1418 + /* Be paranoid... */
1419 + if (prog == NULL || string == NULL) {
1420 + printk("<3>Regexp: NULL parameter\n");
1424 + /* Check validity of program. */
1425 + if (UCHARAT(prog->program) != MAGIC) {
1426 + printk("<3>Regexp: corrupted program\n");
1430 + /* If there is a "must appear" string, look for it. */
1431 + if (prog->regmust != NULL) {
1433 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1434 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1435 + break; /* Found it. */
1438 + if (s == NULL) /* Not present. */
1442 + /* Mark beginning of line for ^ . */
1445 + /* Simplest case: anchored match need be tried only once. */
1446 + if (prog->reganch)
1447 + return(regtry(prog, string));
1449 + /* Messy cases: unanchored match. */
1451 + if (prog->regstart != '\0')
1452 + /* We know what char it must start with. */
1453 + while ((s = strchr(s, prog->regstart)) != NULL) {
1454 + if (regtry(prog, s))
1459 + /* We don't -- general case. */
1461 + if (regtry(prog, s))
1463 + } while (*s++ != '\0');
1470 + - regtry - try match at specific point
1472 +static int /* 0 failure, 1 success */
1473 +regtry(regexp *prog, char *string)
1476 + register char **sp;
1477 + register char **ep;
1479 + reginput = string;
1480 + regstartp = prog->startp;
1481 + regendp = prog->endp;
1483 + sp = prog->startp;
1485 + for (i = NSUBEXP; i > 0; i--) {
1489 + if (regmatch(prog->program + 1)) {
1490 + prog->startp[0] = string;
1491 + prog->endp[0] = reginput;
1498 + - regmatch - main matching routine
1500 + * Conceptually the strategy is simple: check to see whether the current
1501 + * node matches, call self recursively to see whether the rest matches,
1502 + * and then act accordingly. In practice we make some effort to avoid
1503 + * recursion, in particular by going through "ordinary" nodes (that don't
1504 + * need to know whether the rest of the match failed) by a loop instead of
1507 +static int /* 0 failure, 1 success */
1508 +regmatch(char *prog)
1510 + register char *scan = prog; /* Current node. */
1511 + char *next; /* Next node. */
1514 + if (scan != NULL && regnarrate)
1515 + fprintf(stderr, "%s(\n", regprop(scan));
1517 + while (scan != NULL) {
1520 + fprintf(stderr, "%s...\n", regprop(scan));
1522 + next = regnext(scan);
1524 + switch (OP(scan)) {
1526 + if (reginput != regbol)
1530 + if (*reginput != '\0')
1534 + if (*reginput == '\0')
1540 + register char *opnd;
1542 + opnd = OPERAND(scan);
1543 + /* Inline the first character, for speed. */
1544 + if (*opnd != *reginput)
1546 + len = strlen(opnd);
1547 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1553 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1558 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1575 + register char *save;
1577 + no = OP(scan) - OPEN;
1580 + if (regmatch(next)) {
1582 + * Don't set startp if some later
1583 + * invocation of the same parentheses
1586 + if (regstartp[no] == NULL)
1587 + regstartp[no] = save;
1604 + register char *save;
1606 + no = OP(scan) - CLOSE;
1609 + if (regmatch(next)) {
1611 + * Don't set endp if some later
1612 + * invocation of the same parentheses
1615 + if (regendp[no] == NULL)
1616 + regendp[no] = save;
1623 + register char *save;
1625 + if (OP(next) != BRANCH) /* No choice. */
1626 + next = OPERAND(scan); /* Avoid recursion. */
1630 + if (regmatch(OPERAND(scan)))
1633 + scan = regnext(scan);
1634 + } while (scan != NULL && OP(scan) == BRANCH);
1642 + register char nextch;
1644 + register char *save;
1648 + * Lookahead to avoid useless match attempts
1649 + * when we know what character comes next.
1652 + if (OP(next) == EXACTLY)
1653 + nextch = *OPERAND(next);
1654 + min = (OP(scan) == STAR) ? 0 : 1;
1656 + no = regrepeat(OPERAND(scan));
1657 + while (no >= min) {
1658 + /* If it could work, try it. */
1659 + if (nextch == '\0' || *reginput == nextch)
1660 + if (regmatch(next))
1662 + /* Couldn't or didn't -- back up. */
1664 + reginput = save + no;
1670 + return(1); /* Success! */
1673 + printk("<3>Regexp: memory corruption\n");
1682 + * We get here only if there's trouble -- normally "case END" is
1683 + * the terminating point.
1685 + printk("<3>Regexp: corrupted pointers\n");
1690 + - regrepeat - repeatedly match something simple, report how many
1695 + register int count = 0;
1696 + register char *scan;
1697 + register char *opnd;
1700 + opnd = OPERAND(p);
1703 + count = strlen(scan);
1707 + while (*opnd == *scan) {
1713 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1719 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1724 + default: /* Oh dear. Called inappropriately. */
1725 + printk("<3>Regexp: internal foulup\n");
1726 + count = 0; /* Best compromise. */
1735 + - regnext - dig the "next" pointer out of a node
1740 + register int offset;
1742 + if (p == ®dummy)
1749 + if (OP(p) == BACK)
1757 +STATIC char *regprop();
1760 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1766 + register char op = EXACTLY; /* Arbitrary non-END op. */
1767 + register char *next;
1768 + /* extern char *strchr(); */
1771 + s = r->program + 1;
1772 + while (op != END) { /* While that wasn't END last time... */
1774 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1775 + next = regnext(s);
1776 + if (next == NULL) /* Next ptr. */
1779 + printf("(%d)", (s-r->program)+(next-s));
1781 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1782 + /* Literal string, where present. */
1783 + while (*s != '\0') {
1792 + /* Header fields of interest. */
1793 + if (r->regstart != '\0')
1794 + printf("start `%c' ", r->regstart);
1796 + printf("anchored ");
1797 + if (r->regmust != NULL)
1798 + printf("must have \"%s\"", r->regmust);
1803 + - regprop - printable representation of opcode
1810 + static char buf[BUFLEN];
1854 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1866 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1876 + printk("<3>Regexp: corrupted opcode\n");
1880 + strncat(buf, p, BUFLEN-strlen(buf));
1886 --- linux-2.6.16.14-stock/net/ipv4/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1887 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/regexp/regexp.h 2006-05-08 22:57:35.000000000 -0500
1890 + * Definitions etc. for regexp(3) routines.
1892 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1893 + * not the System V one.
1901 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1902 +which contains a version of this library, says:
1905 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1906 + * will not work properly.
1909 +However, it looks rather like this library is limited to 10. If you think
1910 +otherwise, let us know.
1914 +typedef struct regexp {
1915 + char *startp[NSUBEXP];
1916 + char *endp[NSUBEXP];
1917 + char regstart; /* Internal use only. */
1918 + char reganch; /* Internal use only. */
1919 + char *regmust; /* Internal use only. */
1920 + int regmlen; /* Internal use only. */
1921 + char program[1]; /* Unwarranted chumminess with compiler. */
1924 +regexp * regcomp(char *exp, int *patternsize);
1925 +int regexec(regexp *prog, char *string);
1926 +void regsub(regexp *prog, char *source, char *dest);
1927 +void regerror(char *s);
1930 --- linux-2.6.16.14-stock/net/ipv4/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1931 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/regexp/regmagic.h 2006-05-08 22:57:35.000000000 -0500
1934 + * The first byte of the regexp internal "program" is actually this magic
1935 + * number; the start node begins in the second byte.
1938 --- linux-2.6.16.14-stock/net/ipv4/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1939 +++ linux-2.6.16.14-layer7/net/ipv4/netfilter/regexp/regsub.c 2006-05-08 22:57:35.000000000 -0500
1943 + * @(#)regsub.c 1.3 of 2 April 86
1945 + * Copyright (c) 1986 by University of Toronto.
1946 + * Written by Henry Spencer. Not derived from licensed software.
1948 + * Permission is granted to anyone to use this software for any
1949 + * purpose on any computer system, and to redistribute it freely,
1950 + * subject to the following restrictions:
1952 + * 1. The author is not responsible for the consequences of use of
1953 + * this software, no matter how awful, even if they arise
1954 + * from defects in it.
1956 + * 2. The origin of this software must not be misrepresented, either
1957 + * by explicit claim or by omission.
1959 + * 3. Altered versions must be plainly marked as such, and must not
1960 + * be misrepresented as being the original software.
1963 + * This code was modified by Ethan Sommer to work within the kernel
1964 + * (it now uses kmalloc etc..)
1967 +#include "regexp.h"
1968 +#include "regmagic.h"
1969 +#include <linux/string.h>
1973 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1975 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1979 +//void regerror(char * s)
1981 +// printk("regexp(3): %s", s);
1982 +// /* NOTREACHED */
1987 + - regsub - perform substitutions after a regexp match
1990 +regsub(regexp * prog, char * source, char * dest)
1992 + register char *src;
1993 + register char *dst;
1998 + /* Not necessary and gcc doesn't like it -MLS */
1999 + /*extern char *strncpy();*/
2001 + if (prog == NULL || source == NULL || dest == NULL) {
2002 + regerror("NULL parm to regsub");
2005 + if (UCHARAT(prog->program) != MAGIC) {
2006 + regerror("damaged regexp fed to regsub");
2012 + while ((c = *src++) != '\0') {
2015 + else if (c == '\\' && '0' <= *src && *src <= '9')
2016 + no = *src++ - '0';
2020 + if (no < 0) { /* Ordinary character. */
2021 + if (c == '\\' && (*src == '\\' || *src == '&'))
2024 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2025 + len = prog->endp[no] - prog->startp[no];
2026 + (void) strncpy(dst, prog->startp[no], len);
2028 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2029 + regerror("damaged match string");