1 diff -Nurp linux-2.4.26-stock/Documentation/Configure.help linux-2.4.26-layer7-clean/Documentation/Configure.help
2 --- linux-2.4.26-stock/Documentation/Configure.help 2004-04-14 08:05:24.000000000 -0500
3 +++ linux-2.4.26-layer7-clean/Documentation/Configure.help 2004-06-21 00:18:14.000000000 -0500
4 @@ -28819,6 +28819,23 @@ CONFIG_SOUND_WM97XX
8 +CONFIG_IP_NF_MATCH_LAYER7
9 + Say Y if you want to be able to classify connections (and their
10 + packets) based on regular expression matching of their application
11 + layer data. This is one way to classify applications such as
12 + peer-to-peer filesharing systems that do not always use the same
15 + To compile it as a module, choose M here. If unsure, say N.
17 +CONFIG_IP_NF_MATCH_LAYER7_DEBUG
18 + Say Y to get lots of debugging output.
20 +CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN
21 + Size of the buffer that the application layer data is stored in.
22 + Unless you know what you're doing, leave it at the default of 2048
26 # A couple of things I keep forgetting:
27 # capitalize: AppleTalk, Ethernet, DOS, DMA, FAT, FTP, Internet,
28 diff -Nurp linux-2.4.26-stock/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.4.26-layer7-clean/include/linux/netfilter_ipv4/ip_conntrack.h
29 --- linux-2.4.26-stock/include/linux/netfilter_ipv4/ip_conntrack.h 2004-04-14 08:05:40.000000000 -0500
30 +++ linux-2.4.26-layer7-clean/include/linux/netfilter_ipv4/ip_conntrack.h 2004-06-21 00:18:28.000000000 -0500
31 @@ -207,6 +207,17 @@ struct ip_conntrack
33 #endif /* CONFIG_IP_NF_NAT_NEEDED */
35 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
37 + unsigned int numpackets; /* surely this is kept track of somewhere else, right? I can't find it... */
38 + char * app_proto; /* "http", "ftp", etc. NULL if unclassifed */
40 + /* the application layer data so far. NULL if ->numpackets > numpackets */
43 + unsigned int app_data_len;
48 /* get master conntrack via master expectation */
49 diff -Nurp linux-2.4.26-stock/include/linux/netfilter_ipv4/ipt_layer7.h linux-2.4.26-layer7-clean/include/linux/netfilter_ipv4/ipt_layer7.h
50 --- linux-2.4.26-stock/include/linux/netfilter_ipv4/ipt_layer7.h 1969-12-31 18:00:00.000000000 -0600
51 +++ linux-2.4.26-layer7-clean/include/linux/netfilter_ipv4/ipt_layer7.h 2004-06-21 00:18:28.000000000 -0500
54 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
55 + http://l7-filter.sf.net
57 + This program is free software; you can redistribute it and/or
58 + modify it under the terms of the GNU General Public License
59 + as published by the Free Software Foundation; either version
60 + 2 of the License, or (at your option) any later version.
61 + http://www.gnu.org/licenses/gpl.txt
64 +#ifndef _IPT_LAYER7_H
65 +#define _IPT_LAYER7_H
67 +#define MAX_PATTERN_LEN 8192
68 +#define MAX_PROTOCOL_LEN 256
70 +typedef char *(*proc_ipt_search) (char *, char, char *);
72 +struct ipt_layer7_info {
73 + char protocol[MAX_PROTOCOL_LEN];
75 + char pattern[MAX_PATTERN_LEN];
78 +#endif /* _IPT_LAYER7_H */
79 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/Config.in linux-2.4.26-layer7-clean/net/ipv4/netfilter/Config.in
80 --- linux-2.4.26-stock/net/ipv4/netfilter/Config.in 2003-08-25 06:44:44.000000000 -0500
81 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/Config.in 2004-06-21 00:15:23.000000000 -0500
82 @@ -43,6 +43,10 @@ if [ "$CONFIG_IP_NF_IPTABLES" != "n" ];
83 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
84 dep_tristate ' Unclean match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_UNCLEAN $CONFIG_IP_NF_IPTABLES
85 dep_tristate ' Owner match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_OWNER $CONFIG_IP_NF_IPTABLES
86 + dep_tristate ' Layer 7 match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7 $CONFIG_IP_NF_CONNTRACK
87 + dep_mbool ' Layer 7 debugging output (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7_DEBUG $CONFIG_IP_NF_MATCH_LAYER7
88 + int ' Buffer size for application layer data (256-65536)' CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN 2048
92 dep_tristate ' Packet filtering' CONFIG_IP_NF_FILTER $CONFIG_IP_NF_IPTABLES
93 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/Makefile linux-2.4.26-layer7-clean/net/ipv4/netfilter/Makefile
94 --- linux-2.4.26-stock/net/ipv4/netfilter/Makefile 2003-08-25 06:44:44.000000000 -0500
95 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/Makefile 2004-06-21 00:15:22.000000000 -0500
96 @@ -87,6 +87,8 @@ obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += i
97 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
98 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
100 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
103 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
104 obj-$(CONFIG_IP_NF_TARGET_MIRROR) += ipt_MIRROR.o
105 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/ip_conntrack_core.c linux-2.4.26-layer7-clean/net/ipv4/netfilter/ip_conntrack_core.c
106 --- linux-2.4.26-stock/net/ipv4/netfilter/ip_conntrack_core.c 2004-02-18 07:36:32.000000000 -0600
107 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/ip_conntrack_core.c 2004-06-21 00:15:22.000000000 -0500
108 @@ -339,6 +339,15 @@ destroy_conntrack(struct nf_conntrack *n
113 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
114 + /* This ought to get free'd somewhere. How about here? */
115 + if(ct->layer7.app_proto) /* this is sufficient, right? */
116 + kfree(ct->layer7.app_proto);
117 + if(ct->layer7.app_data)
118 + kfree(ct->layer7.app_data);
121 WRITE_UNLOCK(&ip_conntrack_lock);
124 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.4.26-layer7-clean/net/ipv4/netfilter/ip_conntrack_standalone.c
125 --- linux-2.4.26-stock/net/ipv4/netfilter/ip_conntrack_standalone.c 2004-02-18 07:36:32.000000000 -0600
126 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/ip_conntrack_standalone.c 2004-06-21 00:15:22.000000000 -0500
127 @@ -107,6 +107,13 @@ print_conntrack(char *buffer, struct ip_
128 len += sprintf(buffer + len, "[ASSURED] ");
129 len += sprintf(buffer + len, "use=%u ",
130 atomic_read(&conntrack->ct_general.use));
132 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
133 + if(conntrack->layer7.app_proto)
134 + len += sprintf(buffer + len, "l7proto=%s ",
135 + conntrack->layer7.app_proto);
138 len += sprintf(buffer + len, "\n");
141 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/ipt_layer7.c linux-2.4.26-layer7-clean/net/ipv4/netfilter/ipt_layer7.c
142 --- linux-2.4.26-stock/net/ipv4/netfilter/ipt_layer7.c 1969-12-31 18:00:00.000000000 -0600
143 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/ipt_layer7.c 2004-06-27 19:06:51.000000000 -0500
146 + Kernel module to match application layer (OSI layer 7)
147 + data in connections.
149 + http://l7-filter.sf.net
151 + By Matthew Strait and Ethan Sommer, 2003.
153 + This program is free software; you can redistribute it and/or
154 + modify it under the terms of the GNU General Public License
155 + as published by the Free Software Foundation; either version
156 + 2 of the License, or (at your option) any later version.
157 + http://www.gnu.org/licenses/gpl.txt
159 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
160 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
163 +#include <linux/module.h>
164 +#include <linux/skbuff.h>
165 +#include <linux/netfilter_ipv4/ip_conntrack.h>
166 +#include <linux/proc_fs.h>
167 +#include <linux/ctype.h>
169 +#include <net/tcp.h>
171 +#include "regexp/regexp.c"
173 +#include <linux/netfilter_ipv4/ipt_layer7.h>
174 +#include <linux/netfilter_ipv4/ip_tables.h>
176 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
177 +MODULE_LICENSE("GPL");
178 +MODULE_DESCRIPTION("iptables application layer match module");
180 +#if defined(CONFIG_IP_NF_MATCH_LAYER7_DEBUG)
181 + #define DPRINTK(format,args...) printk(format,##args)
183 + #define DPRINTK(format,args...)
186 +/* Number of packets whose data we look at.
187 +This can be modified through /proc/net/layer7_numpackets */
188 +static int num_packets = 8;
190 +static struct pattern_cache {
191 + char * regex_string;
193 + struct pattern_cache * next;
194 +} * first_pattern_cache = NULL;
196 +/* I'm new to locking. Here are my assumptions:
198 +- No one is going to write to /proc/net/layer7_numpackets over and over
199 + within a short period of time, and if they did, nothing awful would happen.
201 +- This code will never be processing the same packet twice at the same time,
202 + because iptables rules need to be traversed in order.
204 +- It doesn't matter if two packets from different connections are in here at
205 + the same time, because they don't share any data.
207 +- It _does_ matter if two packets from the same connection are here at the same
208 + time. In this case, the things we have to protect are the conntracks and
209 + the list of compiled patterns.
211 +DECLARE_RWLOCK(ct_lock);
212 +DECLARE_LOCK(list_lock);
214 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
215 +over again, it make sense to cache the results. */
216 +static regexp * compile_and_cache(char * regex_string, char * protocol)
218 + struct pattern_cache * node = first_pattern_cache;
219 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
220 + struct pattern_cache * tmp;
223 + while (node != NULL) {
224 + if (!strcmp(node->regex_string, regex_string))
225 + return node->pattern;
227 + last_pattern_cache = node;/* points at the last non-NULL node */
231 + /* If we reach the end of the list, then we have not yet cached
232 + the pattern for this regex. Let's do that now.
233 + Be paranoid about running out of memory to avoid list corruption. */
234 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
237 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
241 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
242 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
244 + if(!tmp->regex_string || !tmp->pattern) {
245 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
246 + kfree(tmp->regex_string);
247 + kfree(tmp->pattern);
253 + /* Ok. The new node is all ready now. */
256 + if(first_pattern_cache == NULL) /* list is empty */
257 + first_pattern_cache = node; /* make node the beginning */
259 + last_pattern_cache->next = node; /* attach node to the end */
261 + /* copy the string and compile the regex */
262 + len = strlen(regex_string);
263 + node->pattern = regcomp(regex_string, &len);
264 + if ( !node->pattern ) {
265 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n",
266 + regex_string, protocol);
267 + /* pattern is now cached as NULL, so we won't try again. */
270 + strcpy(node->regex_string, regex_string);
271 + return node->pattern;
274 +#if CONFIG_IP_NF_MATCH_LAYER7_DEBUG
275 +/* Converts an unfriendly string into a friendly one by
276 +replacing unprintables with periods and all whitespace with " ". */
277 +static char * friendly_print(unsigned char * s)
279 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
283 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
287 + for(i = 0; i < strlen(s); i++){
288 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
289 + else if(isspace(s[i])) f[i] = ' ';
296 +static char dec2hex(int i)
300 + return (char)(i + '0');
303 + return (char)(i - 10 + 'a');
306 + printk("Problem in dec2hex\n");
311 +static char * hex_print(unsigned char * s)
313 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
317 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
321 + for(i = 0; i < strlen(s); i++) {
322 + g[i*3 ] = dec2hex(s[i]/16);
323 + g[i*3 + 1] = dec2hex(s[i]%16);
333 +/* The following functions are here in case we get ported into an environment
334 +(ebtables?) where skb->nh.iph->protocol isn't set. They assume that skb->data
335 +points at the beginning of the IP datagram, which is true for iptables (but in
336 +QoS it points to the beginning of the Ethernet frame). */
338 +#define IP_PROTO_OFFSET 9
339 +static int is_tcp_over_ipv4 (const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_TCP );}
340 +static int is_udp_over_ipv4 (const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_UDP );}
341 +static int is_icmp_over_ipv4(const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_ICMP);}
344 +static int can_handle(const struct sk_buff *skb)
346 + if(!skb->nh.iph) /* not IP */
348 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
349 + skb->nh.iph->protocol != IPPROTO_UDP &&
350 + skb->nh.iph->protocol != IPPROTO_ICMP)
355 +/* Returns offset the into the skb->data that the application data starts */
356 +static int app_data_offset(const struct sk_buff *skb)
358 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
359 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
360 + int ip_hl = 4*skb->nh.iph->ihl;
362 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
363 + /* 12 == offset into TCP header for the header length field.
364 + Can't get this with skb->h.th->doff because the tcphdr
365 + struct doesn't get set when routing (this is confirmed to be
366 + true in Netfilter as well as QoS.) */
367 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
369 + return ip_hl + tcp_hl;
370 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
371 + return ip_hl + 8; /* UDP header is always 8 bytes */
372 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
373 + return ip_hl + 8; /* ICMP header is 8 bytes */
375 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
376 + return ip_hl + 8; /* something reasonable */
380 +/* handles whether there's a match when we aren't appending data anymore */
381 +static int match_no_append(struct ip_conntrack * conntrack,
382 + struct ip_conntrack * master_conntrack,
383 + struct ipt_layer7_info * info)
385 + /* If we're in here, we don't care about the app data anymore */
386 + WRITE_LOCK(&ct_lock);
387 + if(master_conntrack->layer7.app_data != NULL) {
389 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
390 + if(!master_conntrack->layer7.app_proto) {
391 + char * f = friendly_print(master_conntrack->layer7.app_data);
392 + char * g = hex_print(master_conntrack->layer7.app_data);
393 + DPRINTK("\nGave up on the %d length stream: \n%s\n",
394 + master_conntrack->layer7.app_data_len, f);
395 + DPRINTK("\nIn hex: %s\n", g);
401 + kfree(master_conntrack->layer7.app_data);
402 + master_conntrack->layer7.app_data = NULL; /* don't free again */
404 + WRITE_UNLOCK(&ct_lock);
406 + /* Is top-level master (possibly self) classified? */
407 + if(master_conntrack->layer7.app_proto) {
408 + if(!strcmp(master_conntrack->layer7.app_proto, info->protocol))
410 + /* set own .protocol (for /proc/net/ip_conntrack) */
411 + WRITE_LOCK(&ct_lock);
412 + if(!conntrack->layer7.app_proto) {
413 + conntrack->layer7.app_proto = kmalloc(strlen(info->protocol), GFP_ATOMIC);
414 + if(!conntrack->layer7.app_proto){
415 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
416 + WRITE_UNLOCK(&ct_lock);
420 + strcpy(conntrack->layer7.app_proto, info->protocol);
422 + WRITE_UNLOCK(&ct_lock);
426 + } else return 0; /* no clasification */
429 +/* add the new app data to the conntrack. Return number of bytes added. */
430 +static int add_data(struct ip_conntrack * master_conntrack,
431 + char * app_data, int appdatalen)
434 + int oldlength = master_conntrack->layer7.app_data_len;
436 + /* Strip nulls. Make everything lower case (our regex lib doesn't
437 + do case insensitivity). Add it to the end of the current data. */
438 + for(i = 0; i < CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN-oldlength-1 &&
439 + i < appdatalen; i++) {
440 + if(app_data[i] != '\0') {
441 + master_conntrack->layer7.app_data[length+oldlength] =
442 + /* the kernel version of tolower mungs 'upper ascii' */
443 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
448 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
449 + master_conntrack->layer7.app_data_len = length + oldlength;
454 +/* Returns true on match and false otherwise. */
455 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
456 + const struct net_device *out, const void *matchinfo,
457 + int offset, int *hotdrop)
459 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
460 + enum ip_conntrack_info master_ctinfo, ctinfo;
461 + struct ip_conntrack *master_conntrack, *conntrack;
462 + unsigned char * app_data;
463 + unsigned int pattern_result, appdatalen;
464 + regexp * comppattern;
466 + if(!can_handle(skb)){
467 + DPRINTK("layer7: This is some protocol I can't handle\n");
468 + return info->invert;
471 + LOCK_BH(&list_lock);
472 + comppattern = compile_and_cache(info->pattern, info->protocol);
473 + UNLOCK_BH(&list_lock);
474 + /* the return value gets checked later, when we're ready to use it */
476 + app_data = skb->data + app_data_offset(skb);
477 + appdatalen = skb->tail - app_data;
479 + /* Treat the parent and all its children together as one connection,
480 + except for the purpose of setting conntrack->layer7.pattern in the
481 + actual connection. This makes /proc/net/ip_conntrack somewhat more
483 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
484 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
485 + DPRINTK("layer7: packet is not from a known connection, giving up.\n");
486 + return info->invert;
489 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
490 + while (master_ct(master_conntrack) != NULL)
491 + master_conntrack = master_ct(master_conntrack);
493 + /* skb->cb[0] == seen. Avoid doing things twice if there are two layer7
494 + rules. I'm not sure that using cb for this purpose is correct, although
495 + it says "put your private variables there" and this seems to qualify.
496 + But it doesn't look like it's being used for anything else in the
497 + sk_buffs that make it here. I'm open to suggestions for how to be able
498 + to write to cb without making the compiler angry. That I can't figure
499 + this out is an argument against this being correct. */
501 + WRITE_LOCK(&ct_lock);
502 + master_conntrack->layer7.numpackets++;/*starts at 0 via memset*/
503 + WRITE_UNLOCK(&ct_lock);
506 + /* On the first packet of a connection, allocate space for app data */
507 + WRITE_LOCK(&ct_lock);
508 + if(master_conntrack->layer7.numpackets == 1 && !skb->cb[0]) {
509 + master_conntrack->layer7.app_data = kmalloc(CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN, GFP_ATOMIC);
510 + if(!master_conntrack->layer7.app_data){
511 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
512 + WRITE_UNLOCK(&ct_lock);
513 + return info->invert;
516 + master_conntrack->layer7.app_data[0] = '\0';
518 + WRITE_UNLOCK(&ct_lock);
520 + /* if we've classified it or seen too many packets */
521 + if(master_conntrack->layer7.numpackets > num_packets ||
522 + master_conntrack->layer7.app_proto) {
524 + pattern_result = match_no_append(conntrack, master_conntrack, info);
526 + /* mark the packet seen (probably irrelevant, but consistant) */
529 + return (pattern_result ^ info->invert);
532 + /* Can end up here, but unallocated, if numpackets is increased during
533 + the beginning of a connection */
534 + if(master_conntrack->layer7.app_data == NULL)
535 + return (info->invert); /* unmatched */
539 + WRITE_LOCK(&ct_lock);
540 + newbytes = add_data(master_conntrack, app_data, appdatalen);
541 + WRITE_UNLOCK(&ct_lock);
543 + if(newbytes == 0) { /* didn't add any data */
545 + /* Didn't match before, not going to match now */
546 + return info->invert;
550 + /* If the regexp failed to compile, don't bother running it */
551 + if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
552 + DPRINTK("layer7: regexec positive: %s!\n", info->protocol);
553 + pattern_result = 1;
554 + } else pattern_result = 0;
556 + if(pattern_result) {
557 + WRITE_LOCK(&ct_lock);
558 + conntrack->layer7.app_proto = kmalloc(strlen(info->protocol), GFP_ATOMIC);
559 + if(!conntrack->layer7.app_proto){
560 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
561 + WRITE_UNLOCK(&ct_lock);
562 + return (pattern_result ^ info->invert);
565 + strcpy(conntrack->layer7.app_proto, info->protocol);
566 + WRITE_UNLOCK(&ct_lock);
569 + /* mark the packet seen */
572 + return (pattern_result ^ info->invert);
575 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
576 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
578 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
583 +static struct ipt_match layer7_match = {
586 + .checkentry = &checkentry,
590 +/* taken from drivers/video/modedb.c */
591 +static int my_atoi(const char *s)
598 + val = 10*val+(*s-'0');
606 +/* write out num_packets to userland. */
607 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
608 + int* eof, void * data)
610 + if(num_packets > 99)
611 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
613 + page[0] = num_packets/10 + '0';
614 + page[1] = num_packets%10 + '0';
623 +/* Read in num_packets from userland */
624 +static int layer7_write_proc(struct file* file, const char* buffer,
625 + unsigned long count, void *data)
627 + char * foo = kmalloc(count, GFP_ATOMIC);
630 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
634 + /* copy in the data from userland */
635 + copy_from_user(foo, buffer, count);
637 + num_packets = my_atoi(foo);
640 + /* This has an arbitrary limit to make the math easier. I'm lazy.
641 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
642 + if(num_packets > 99) {
643 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
645 + } else if(num_packets < 1) {
646 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
653 +/* register the proc file */
654 +static void layer7_init_proc(void)
656 + struct proc_dir_entry* entry;
658 + /* create the file */
659 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
661 + /* set the callback functions */
662 + entry->read_proc = layer7_read_proc;
663 + entry->write_proc = layer7_write_proc;
666 +static void layer7_cleanup_proc(void)
668 + remove_proc_entry("layer7_numpackets", proc_net);
671 +static int __init init(void)
673 + layer7_init_proc();
674 + return ipt_register_match(&layer7_match);
677 +static void __exit fini(void)
679 + layer7_cleanup_proc();
680 + ipt_unregister_match(&layer7_match);
685 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/regexp/regexp.c linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regexp.c
686 --- linux-2.4.26-stock/net/ipv4/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
687 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regexp.c 2004-06-27 19:07:00.000000000 -0500
690 + * regcomp and regexec -- regsub and regerror are elsewhere
691 + * @(#)regexp.c 1.3 of 18 April 87
693 + * Copyright (c) 1986 by University of Toronto.
694 + * Written by Henry Spencer. Not derived from licensed software.
696 + * Permission is granted to anyone to use this software for any
697 + * purpose on any computer system, and to redistribute it freely,
698 + * subject to the following restrictions:
700 + * 1. The author is not responsible for the consequences of use of
701 + * this software, no matter how awful, even if they arise
702 + * from defects in it.
704 + * 2. The origin of this software must not be misrepresented, either
705 + * by explicit claim or by omission.
707 + * 3. Altered versions must be plainly marked as such, and must not
708 + * be misrepresented as being the original software.
710 + * Beware that some of this code is subtly aware of the way operator
711 + * precedence is structured in regular expressions. Serious changes in
712 + * regular-expression syntax might require a total rethink.
714 + * This code was modified by Ethan Sommer to work within the kernel
715 + * (it now uses kmalloc etc..)
717 + * Modified slightly by Matthew Strait to use more modern C.
721 +#include "regmagic.h"
723 +/* added by ethan and matt. Lets it work in both kernel and user space.
724 +(So iptables can use it, for instance.) Yea, it goes both ways... */
726 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
728 + #define printk(format,args...) printf(format,##args)
731 +void regerror(char * s)
733 + printk("<3>Regexp: %s\n", s);
738 + * The "internal use only" fields in regexp.h are present to pass info from
739 + * compile to execute that permits the execute phase to run lots faster on
740 + * simple cases. They are:
742 + * regstart char that must begin a match; '\0' if none obvious
743 + * reganch is the match anchored (at beginning-of-line only)?
744 + * regmust string (pointer into program) that match must include, or NULL
745 + * regmlen length of regmust string
747 + * Regstart and reganch permit very fast decisions on suitable starting points
748 + * for a match, cutting down the work a lot. Regmust permits fast rejection
749 + * of lines that cannot possibly match. The regmust tests are costly enough
750 + * that regcomp() supplies a regmust only if the r.e. contains something
751 + * potentially expensive (at present, the only such thing detected is * or +
752 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
753 + * supplied because the test in regexec() needs it and regcomp() is computing
758 + * Structure for regexp "program". This is essentially a linear encoding
759 + * of a nondeterministic finite-state machine (aka syntax charts or
760 + * "railroad normal form" in parsing technology). Each node is an opcode
761 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
762 + * all nodes except BRANCH implement concatenation; a "next" pointer with
763 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
764 + * have one of the subtle syntax dependencies: an individual BRANCH (as
765 + * opposed to a collection of them) is never concatenated with anything
766 + * because of operator precedence.) The operand of some types of node is
767 + * a literal string; for others, it is a node leading into a sub-FSM. In
768 + * particular, the operand of a BRANCH node is the first node of the branch.
769 + * (NB this is *not* a tree structure: the tail of the branch connects
770 + * to the thing following the set of BRANCHes.) The opcodes are:
773 +/* definition number opnd? meaning */
774 +#define END 0 /* no End of program. */
775 +#define BOL 1 /* no Match "" at beginning of line. */
776 +#define EOL 2 /* no Match "" at end of line. */
777 +#define ANY 3 /* no Match any one character. */
778 +#define ANYOF 4 /* str Match any character in this string. */
779 +#define ANYBUT 5 /* str Match any character not in this string. */
780 +#define BRANCH 6 /* node Match this alternative, or the next... */
781 +#define BACK 7 /* no Match "", "next" ptr points backward. */
782 +#define EXACTLY 8 /* str Match this string. */
783 +#define NOTHING 9 /* no Match empty string. */
784 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
785 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
786 +#define OPEN 20 /* no Mark this point in input as start of #n. */
787 + /* OPEN+1 is number 1, etc. */
788 +#define CLOSE 30 /* no Analogous to OPEN. */
793 + * BRANCH The set of branches constituting a single choice are hooked
794 + * together with their "next" pointers, since precedence prevents
795 + * anything being concatenated to any individual branch. The
796 + * "next" pointer of the last BRANCH in a choice points to the
797 + * thing following the whole choice. This is also where the
798 + * final "next" pointer of each individual branch points; each
799 + * branch starts with the operand node of a BRANCH node.
801 + * BACK Normal "next" pointers all implicitly point forward; BACK
802 + * exists to make loop structures possible.
804 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
805 + * BRANCH structures using BACK. Simple cases (one character
806 + * per match) are implemented with STAR and PLUS for speed
807 + * and to minimize recursive plunges.
809 + * OPEN,CLOSE ...are numbered at compile time.
813 + * A node is one char of opcode followed by two chars of "next" pointer.
814 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
815 + * value is a positive offset from the opcode of the node containing it.
816 + * An operand, if any, simply follows the node. (Note that much of the
817 + * code generation knows about this implicit relationship.)
819 + * Using two bytes for the "next" pointer is vast overkill for most things,
820 + * but allows patterns to get big without disasters.
822 +#define OP(p) (*(p))
823 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
824 +#define OPERAND(p) ((p) + 3)
827 + * See regmagic.h for one further detail of program structure.
832 + * Utility definitions.
835 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
837 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
840 +#define FAIL(m) { regerror(m); return(NULL); }
841 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
842 +#define META "^$.[()|?+*\\"
845 + * Flags to be passed up and down.
847 +#define HASWIDTH 01 /* Known never to match null string. */
848 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
849 +#define SPSTART 04 /* Starts with * or +. */
850 +#define WORST 0 /* Worst case. */
853 + * Global work variables for regcomp().
855 +static char *regparse; /* Input-scan pointer. */
856 +static int regnpar; /* () count. */
857 +static char regdummy;
858 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
859 +static long regsize; /* Code size. */
862 + * Forward declarations for regcomp()'s friends.
865 +#define STATIC static
867 +STATIC char *reg(int paren,int *flagp);
868 +STATIC char *regbranch(int *flagp);
869 +STATIC char *regpiece(int *flagp);
870 +STATIC char *regatom(int *flagp);
871 +STATIC char *regnode(char op);
872 +STATIC char *regnext(char *p);
873 +STATIC void regc(char b);
874 +STATIC void reginsert(char op, char *opnd);
875 +STATIC void regtail(char *p, char *val);
876 +STATIC void regoptail(char *p, char *val);
879 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
886 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
887 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
888 + if (*scan1 == *scan2++)
896 + - regcomp - compile a regular expression into internal code
898 + * We can't allocate space until we know how big the compiled form will be,
899 + * but we can't compile it (and thus know how big it is) until we've got a
900 + * place to put the code. So we cheat: we compile it twice, once with code
901 + * generation turned off and size counting turned on, and once "for real".
902 + * This also means that we don't allocate space until we are sure that the
903 + * thing really will compile successfully, and we never have to move the
904 + * code and thus invalidate pointers into it. (Note that it has to be in
905 + * one piece because free() must be able to free it all.)
907 + * Beware that the optimization-preparation code in here knows about some
908 + * of the structure of the compiled regexp.
911 +regcomp(char *exp,int *patternsize)
913 + register regexp *r;
914 + register char *scan;
915 + register char *longest;
918 + /* commented out by ethan
919 + extern char *malloc();
923 + FAIL("NULL argument");
925 + /* First pass: determine size, legality. */
929 + regcode = ®dummy;
931 + if (reg(0, &flags) == NULL)
934 + /* Small enough for pointer-storage convention? */
935 + if (regsize >= 32767L) /* Probably could be 65535L. */
936 + FAIL("regexp too big");
938 + /* Allocate space. */
939 + *patternsize=sizeof(regexp) + (unsigned)regsize;
940 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
942 + FAIL("out of space");
944 + /* Second pass: emit code. */
947 + regcode = r->program;
949 + if (reg(0, &flags) == NULL)
952 + /* Dig out information for optimizations. */
953 + r->regstart = '\0'; /* Worst-case defaults. */
957 + scan = r->program+1; /* First BRANCH. */
958 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
959 + scan = OPERAND(scan);
961 + /* Starting-point info. */
962 + if (OP(scan) == EXACTLY)
963 + r->regstart = *OPERAND(scan);
964 + else if (OP(scan) == BOL)
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.
975 + if (flags&SPSTART) {
978 + for (; scan != NULL; scan = regnext(scan))
979 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
980 + longest = OPERAND(scan);
981 + len = strlen(OPERAND(scan));
983 + r->regmust = longest;
992 + - reg - regular expression, i.e. main body or parenthesized thing
994 + * Caller must absorb opening parenthesis.
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.
1001 +reg(int paren, int *flagp /* Parenthesized? */ )
1003 + register char *ret;
1004 + register char *br;
1005 + register char *ender;
1006 + register int parno = 0; /* 0 makes gcc happy */
1009 + *flagp = HASWIDTH; /* Tentatively. */
1011 + /* Make an OPEN node, if parenthesized. */
1013 + if (regnpar >= NSUBEXP)
1014 + FAIL("too many ()");
1017 + ret = regnode(OPEN+parno);
1021 + /* Pick up the branches, linking them together. */
1022 + br = regbranch(&flags);
1026 + regtail(ret, br); /* OPEN -> first. */
1029 + if (!(flags&HASWIDTH))
1030 + *flagp &= ~HASWIDTH;
1031 + *flagp |= flags&SPSTART;
1032 + while (*regparse == '|') {
1034 + br = regbranch(&flags);
1037 + regtail(ret, br); /* BRANCH -> BRANCH. */
1038 + if (!(flags&HASWIDTH))
1039 + *flagp &= ~HASWIDTH;
1040 + *flagp |= flags&SPSTART;
1043 + /* Make a closing node, and hook it on the end. */
1044 + ender = regnode((paren) ? CLOSE+parno : END);
1045 + regtail(ret, ender);
1047 + /* Hook the tails of the branches to the closing node. */
1048 + for (br = ret; br != NULL; br = regnext(br))
1049 + regoptail(br, ender);
1051 + /* Check for proper termination. */
1052 + if (paren && *regparse++ != ')') {
1053 + FAIL("unmatched ()");
1054 + } else if (!paren && *regparse != '\0') {
1055 + if (*regparse == ')') {
1056 + FAIL("unmatched ()");
1058 + FAIL("junk on end"); /* "Can't happen". */
1066 + - regbranch - one alternative of an | operator
1068 + * Implements the concatenation operator.
1071 +regbranch(int *flagp)
1073 + register char *ret;
1074 + register char *chain;
1075 + register char *latest;
1078 + *flagp = WORST; /* Tentatively. */
1080 + ret = regnode(BRANCH);
1082 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1083 + latest = regpiece(&flags);
1084 + if (latest == NULL)
1086 + *flagp |= flags&HASWIDTH;
1087 + if (chain == NULL) /* First piece. */
1088 + *flagp |= flags&SPSTART;
1090 + regtail(chain, latest);
1093 + if (chain == NULL) /* Loop ran zero times. */
1094 + (void) regnode(NOTHING);
1100 + - regpiece - something followed by possible [*+?]
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.
1109 +regpiece(int *flagp)
1111 + register char *ret;
1113 + register char *next;
1116 + ret = regatom(&flags);
1121 + if (!ISMULT(op)) {
1126 + if (!(flags&HASWIDTH) && op != '?')
1127 + FAIL("*+ operand could be empty");
1128 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1130 + if (op == '*' && (flags&SIMPLE))
1131 + reginsert(STAR, ret);
1132 + else if (op == '*') {
1133 + /* Emit x* as (x&|), where & means "self". */
1134 + reginsert(BRANCH, ret); /* Either x */
1135 + regoptail(ret, regnode(BACK)); /* and loop */
1136 + regoptail(ret, ret); /* back */
1137 + regtail(ret, regnode(BRANCH)); /* or */
1138 + regtail(ret, regnode(NOTHING)); /* null. */
1139 + } else if (op == '+' && (flags&SIMPLE))
1140 + reginsert(PLUS, ret);
1141 + else if (op == '+') {
1142 + /* Emit x+ as x(&|), where & means "self". */
1143 + next = regnode(BRANCH); /* Either */
1144 + regtail(ret, next);
1145 + regtail(regnode(BACK), ret); /* loop back */
1146 + regtail(next, regnode(BRANCH)); /* or */
1147 + regtail(ret, regnode(NOTHING)); /* null. */
1148 + } else if (op == '?') {
1149 + /* Emit x? as (x|) */
1150 + reginsert(BRANCH, ret); /* Either x */
1151 + regtail(ret, regnode(BRANCH)); /* or */
1152 + next = regnode(NOTHING); /* null. */
1153 + regtail(ret, next);
1154 + regoptail(ret, next);
1157 + if (ISMULT(*regparse))
1158 + FAIL("nested *?+");
1164 + - regatom - the lowest level
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.
1172 +regatom(int *flagp)
1174 + register char *ret;
1177 + *flagp = WORST; /* Tentatively. */
1179 + switch (*regparse++) {
1181 + ret = regnode(BOL);
1184 + ret = regnode(EOL);
1187 + ret = regnode(ANY);
1188 + *flagp |= HASWIDTH|SIMPLE;
1191 + register int class;
1192 + register int classend;
1194 + if (*regparse == '^') { /* Complement of range. */
1195 + ret = regnode(ANYBUT);
1198 + ret = regnode(ANYOF);
1199 + if (*regparse == ']' || *regparse == '-')
1200 + regc(*regparse++);
1201 + while (*regparse != '\0' && *regparse != ']') {
1202 + if (*regparse == '-') {
1204 + if (*regparse == ']' || *regparse == '\0')
1207 + class = UCHARAT(regparse-2)+1;
1208 + classend = UCHARAT(regparse);
1209 + if (class > classend+1)
1210 + FAIL("invalid [] range");
1211 + for (; class <= classend; class++)
1216 + regc(*regparse++);
1219 + if (*regparse != ']')
1220 + FAIL("unmatched []");
1222 + *flagp |= HASWIDTH|SIMPLE;
1226 + ret = reg(1, &flags);
1229 + *flagp |= flags&(HASWIDTH|SPSTART);
1234 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1239 + FAIL("?+* follows nothing");
1242 + if (*regparse == '\0')
1243 + FAIL("trailing \\");
1244 + ret = regnode(EXACTLY);
1245 + regc(*regparse++);
1247 + *flagp |= HASWIDTH|SIMPLE;
1251 + register char ender;
1254 + len = my_strcspn((const char *)regparse, (const char *)META);
1256 + FAIL("internal disaster");
1257 + ender = *(regparse+len);
1258 + if (len > 1 && ISMULT(ender))
1259 + len--; /* Back off clear of ?+* operand. */
1260 + *flagp |= HASWIDTH;
1263 + ret = regnode(EXACTLY);
1265 + regc(*regparse++);
1277 + - regnode - emit a node
1279 +static char * /* Location. */
1282 + register char *ret;
1283 + register char *ptr;
1286 + if (ret == ®dummy) {
1293 + *ptr++ = '\0'; /* Null "next" pointer. */
1301 + - regc - emit (if appropriate) a byte of code
1306 + if (regcode != ®dummy)
1313 + - reginsert - insert an operator in front of already-emitted operand
1315 + * Means relocating the operand.
1318 +reginsert(char op, char* opnd)
1320 + register char *src;
1321 + register char *dst;
1322 + register char *place;
1324 + if (regcode == ®dummy) {
1332 + while (src > opnd)
1335 + place = opnd; /* Op node, where operand used to be. */
1342 + - regtail - set the next-pointer at the end of a node chain
1345 +regtail(char *p, char *val)
1347 + register char *scan;
1348 + register char *temp;
1349 + register int offset;
1351 + if (p == ®dummy)
1354 + /* Find last node. */
1357 + temp = regnext(scan);
1363 + if (OP(scan) == BACK)
1364 + offset = scan - val;
1366 + offset = val - scan;
1367 + *(scan+1) = (offset>>8)&0377;
1368 + *(scan+2) = offset&0377;
1372 + - regoptail - regtail on operand of first argument; nop if operandless
1375 +regoptail(char *p, char *val)
1377 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1378 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1380 + regtail(OPERAND(p), val);
1384 + * regexec and friends
1388 + * Global work variables for regexec().
1390 +static char *reginput; /* String-input pointer. */
1391 +static char *regbol; /* Beginning of input, for ^ check. */
1392 +static char **regstartp; /* Pointer to startp array. */
1393 +static char **regendp; /* Ditto for endp. */
1398 +STATIC int regtry(regexp *prog, char *string);
1399 +STATIC int regmatch(char *prog);
1400 +STATIC int regrepeat(char *p);
1403 +int regnarrate = 0;
1405 +STATIC char *regprop(char *op);
1409 + - regexec - match a regexp against a string
1412 +regexec(regexp *prog, char *string)
1416 + /* Be paranoid... */
1417 + if (prog == NULL || string == NULL) {
1418 + printk("<3>Regexp: NULL parameter\n");
1422 + /* Check validity of program. */
1423 + if (UCHARAT(prog->program) != MAGIC) {
1424 + printk("<3>Regexp: corrupted program\n");
1428 + /* If there is a "must appear" string, look for it. */
1429 + if (prog->regmust != NULL) {
1431 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1432 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1433 + break; /* Found it. */
1436 + if (s == NULL) /* Not present. */
1440 + /* Mark beginning of line for ^ . */
1443 + /* Simplest case: anchored match need be tried only once. */
1444 + if (prog->reganch)
1445 + return(regtry(prog, string));
1447 + /* Messy cases: unanchored match. */
1449 + if (prog->regstart != '\0')
1450 + /* We know what char it must start with. */
1451 + while ((s = strchr(s, prog->regstart)) != NULL) {
1452 + if (regtry(prog, s))
1457 + /* We don't -- general case. */
1459 + if (regtry(prog, s))
1461 + } while (*s++ != '\0');
1468 + - regtry - try match at specific point
1470 +static int /* 0 failure, 1 success */
1471 +regtry(regexp *prog, char *string)
1474 + register char **sp;
1475 + register char **ep;
1477 + reginput = string;
1478 + regstartp = prog->startp;
1479 + regendp = prog->endp;
1481 + sp = prog->startp;
1483 + for (i = NSUBEXP; i > 0; i--) {
1487 + if (regmatch(prog->program + 1)) {
1488 + prog->startp[0] = string;
1489 + prog->endp[0] = reginput;
1496 + - regmatch - main matching routine
1498 + * Conceptually the strategy is simple: check to see whether the current
1499 + * node matches, call self recursively to see whether the rest matches,
1500 + * and then act accordingly. In practice we make some effort to avoid
1501 + * recursion, in particular by going through "ordinary" nodes (that don't
1502 + * need to know whether the rest of the match failed) by a loop instead of
1505 +static int /* 0 failure, 1 success */
1506 +regmatch(char *prog)
1508 + register char *scan = prog; /* Current node. */
1509 + char *next; /* Next node. */
1512 + if (scan != NULL && regnarrate)
1513 + fprintf(stderr, "%s(\n", regprop(scan));
1515 + while (scan != NULL) {
1518 + fprintf(stderr, "%s...\n", regprop(scan));
1520 + next = regnext(scan);
1522 + switch (OP(scan)) {
1524 + if (reginput != regbol)
1528 + if (*reginput != '\0')
1532 + if (*reginput == '\0')
1538 + register char *opnd;
1540 + opnd = OPERAND(scan);
1541 + /* Inline the first character, for speed. */
1542 + if (*opnd != *reginput)
1544 + len = strlen(opnd);
1545 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1551 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1556 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1573 + register char *save;
1575 + no = OP(scan) - OPEN;
1578 + if (regmatch(next)) {
1580 + * Don't set startp if some later
1581 + * invocation of the same parentheses
1584 + if (regstartp[no] == NULL)
1585 + regstartp[no] = save;
1602 + register char *save;
1604 + no = OP(scan) - CLOSE;
1607 + if (regmatch(next)) {
1609 + * Don't set endp if some later
1610 + * invocation of the same parentheses
1613 + if (regendp[no] == NULL)
1614 + regendp[no] = save;
1621 + register char *save;
1623 + if (OP(next) != BRANCH) /* No choice. */
1624 + next = OPERAND(scan); /* Avoid recursion. */
1628 + if (regmatch(OPERAND(scan)))
1631 + scan = regnext(scan);
1632 + } while (scan != NULL && OP(scan) == BRANCH);
1640 + register char nextch;
1642 + register char *save;
1646 + * Lookahead to avoid useless match attempts
1647 + * when we know what character comes next.
1650 + if (OP(next) == EXACTLY)
1651 + nextch = *OPERAND(next);
1652 + min = (OP(scan) == STAR) ? 0 : 1;
1654 + no = regrepeat(OPERAND(scan));
1655 + while (no >= min) {
1656 + /* If it could work, try it. */
1657 + if (nextch == '\0' || *reginput == nextch)
1658 + if (regmatch(next))
1660 + /* Couldn't or didn't -- back up. */
1662 + reginput = save + no;
1668 + return(1); /* Success! */
1671 + printk("<3>Regexp: memory corruption\n");
1680 + * We get here only if there's trouble -- normally "case END" is
1681 + * the terminating point.
1683 + printk("<3>Regexp: corrupted pointers\n");
1688 + - regrepeat - repeatedly match something simple, report how many
1693 + register int count = 0;
1694 + register char *scan;
1695 + register char *opnd;
1698 + opnd = OPERAND(p);
1701 + count = strlen(scan);
1705 + while (*opnd == *scan) {
1711 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1717 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1722 + default: /* Oh dear. Called inappropriately. */
1723 + printk("<3>Regexp: internal foulup\n");
1724 + count = 0; /* Best compromise. */
1733 + - regnext - dig the "next" pointer out of a node
1738 + register int offset;
1740 + if (p == ®dummy)
1747 + if (OP(p) == BACK)
1755 +STATIC char *regprop();
1758 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1764 + register char op = EXACTLY; /* Arbitrary non-END op. */
1765 + register char *next;
1766 + /* extern char *strchr(); */
1769 + s = r->program + 1;
1770 + while (op != END) { /* While that wasn't END last time... */
1772 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1773 + next = regnext(s);
1774 + if (next == NULL) /* Next ptr. */
1777 + printf("(%d)", (s-r->program)+(next-s));
1779 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1780 + /* Literal string, where present. */
1781 + while (*s != '\0') {
1790 + /* Header fields of interest. */
1791 + if (r->regstart != '\0')
1792 + printf("start `%c' ", r->regstart);
1794 + printf("anchored ");
1795 + if (r->regmust != NULL)
1796 + printf("must have \"%s\"", r->regmust);
1801 + - regprop - printable representation of opcode
1808 + static char buf[BUFLEN];
1852 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1864 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1874 + printk("<3>Regexp: corrupted opcode\n");
1878 + strncat(buf, p, BUFLEN-strlen(buf));
1884 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/regexp/regexp.h linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regexp.h
1885 --- linux-2.4.26-stock/net/ipv4/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1886 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regexp.h 2004-06-27 19:07:00.000000000 -0500
1889 + * Definitions etc. for regexp(3) routines.
1891 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1892 + * not the System V one.
1899 +typedef struct regexp {
1900 + char *startp[NSUBEXP];
1901 + char *endp[NSUBEXP];
1902 + char regstart; /* Internal use only. */
1903 + char reganch; /* Internal use only. */
1904 + char *regmust; /* Internal use only. */
1905 + int regmlen; /* Internal use only. */
1906 + char program[1]; /* Unwarranted chumminess with compiler. */
1909 +regexp * regcomp(char *exp, int *patternsize);
1910 +int regexec(regexp *prog, char *string);
1911 +void regsub(regexp *prog, char *source, char *dest);
1912 +void regerror(char *s);
1915 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/regexp/regmagic.h linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regmagic.h
1916 --- linux-2.4.26-stock/net/ipv4/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1917 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regmagic.h 2004-06-27 19:07:00.000000000 -0500
1920 + * The first byte of the regexp internal "program" is actually this magic
1921 + * number; the start node begins in the second byte.
1924 diff -Nurp linux-2.4.26-stock/net/ipv4/netfilter/regexp/regsub.c linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regsub.c
1925 --- linux-2.4.26-stock/net/ipv4/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1926 +++ linux-2.4.26-layer7-clean/net/ipv4/netfilter/regexp/regsub.c 2004-06-27 19:07:00.000000000 -0500
1930 + * @(#)regsub.c 1.3 of 2 April 86
1932 + * Copyright (c) 1986 by University of Toronto.
1933 + * Written by Henry Spencer. Not derived from licensed software.
1935 + * Permission is granted to anyone to use this software for any
1936 + * purpose on any computer system, and to redistribute it freely,
1937 + * subject to the following restrictions:
1939 + * 1. The author is not responsible for the consequences of use of
1940 + * this software, no matter how awful, even if they arise
1941 + * from defects in it.
1943 + * 2. The origin of this software must not be misrepresented, either
1944 + * by explicit claim or by omission.
1946 + * 3. Altered versions must be plainly marked as such, and must not
1947 + * be misrepresented as being the original software.
1950 + * This code was modified by Ethan Sommer to work within the kernel
1951 + * (it now uses kmalloc etc..)
1954 +#include "regexp.h"
1955 +#include "regmagic.h"
1956 +#include <linux/string.h>
1960 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1962 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1966 +//void regerror(char * s)
1968 +// printk("regexp(3): %s", s);
1969 +// /* NOTREACHED */
1974 + - regsub - perform substitutions after a regexp match
1977 +regsub(regexp * prog, char * source, char * dest)
1979 + register char *src;
1980 + register char *dst;
1985 + /* Not necessary and gcc doesn't like it -MLS */
1986 + /*extern char *strncpy();*/
1988 + if (prog == NULL || source == NULL || dest == NULL) {
1989 + regerror("NULL parm to regsub");
1992 + if (UCHARAT(prog->program) != MAGIC) {
1993 + regerror("damaged regexp fed to regsub");
1999 + while ((c = *src++) != '\0') {
2002 + else if (c == '\\' && '0' <= *src && *src <= '9')
2003 + no = *src++ - '0';
2007 + if (no < 0) { /* Ordinary character. */
2008 + if (c == '\\' && (*src == '\\' || *src == '&'))
2011 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2012 + len = prog->endp[no] - prog->startp[no];
2013 + (void) strncpy(dst, prog->startp[no], len);
2015 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2016 + regerror("damaged match string");