1 Index: linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h 2007-07-02 00:43:58.271086750 +0200
7 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
8 + http://l7-filter.sf.net
10 + This program is free software; you can redistribute it and/or
11 + modify it under the terms of the GNU General Public License
12 + as published by the Free Software Foundation; either version
13 + 2 of the License, or (at your option) any later version.
14 + http://www.gnu.org/licenses/gpl.txt
17 +#ifndef _IPT_LAYER7_H
18 +#define _IPT_LAYER7_H
20 +#define MAX_PATTERN_LEN 8192
21 +#define MAX_PROTOCOL_LEN 256
23 +typedef char *(*proc_ipt_search) (char *, char, char *);
25 +struct ipt_layer7_info {
26 + char protocol[MAX_PROTOCOL_LEN];
28 + char pattern[MAX_PATTERN_LEN];
31 +#endif /* _IPT_LAYER7_H */
32 Index: linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_core.c
33 ===================================================================
34 --- linux-2.6.21.5.orig/net/ipv4/netfilter/ip_conntrack_core.c 2007-07-02 00:37:53.432285750 +0200
35 +++ linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_core.c 2007-07-02 00:37:55.496414750 +0200
38 ip_ct_remove_expectations(ct);
40 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
41 + if(ct->layer7.app_proto)
42 + kfree(ct->layer7.app_proto);
43 + if(ct->layer7.app_data)
44 + kfree(ct->layer7.app_data);
47 /* We overload first tuple to link into unconfirmed list. */
48 if (!is_confirmed(ct)) {
49 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
50 Index: linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_standalone.c
51 ===================================================================
52 --- linux-2.6.21.5.orig/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-07-02 00:37:53.440286250 +0200
53 +++ linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-07-02 00:37:55.544417750 +0200
58 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
59 + if(conntrack->layer7.app_proto)
60 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
64 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
67 Index: linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c
68 ===================================================================
69 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
70 +++ linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c 2007-07-02 01:27:54.195821750 +0200
73 + Kernel module to match application layer (OSI layer 7) data in connections.
75 + http://l7-filter.sf.net
77 + By Matthew Strait and Ethan Sommer, 2003-2006.
79 + This program is free software; you can redistribute it and/or
80 + modify it under the terms of the GNU General Public License
81 + as published by the Free Software Foundation; either version
82 + 2 of the License, or (at your option) any later version.
83 + http://www.gnu.org/licenses/gpl.txt
85 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
86 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
88 + Jan Engelhardt, 2007-03-11: Arrange to compile with nf_conntrack
91 +#include <linux/module.h>
92 +#include <linux/skbuff.h>
93 +#include <linux/proc_fs.h>
94 +#include <linux/ctype.h>
97 +#include <net/netfilter/nf_conntrack.h>
98 +#include <net/netfilter/nf_nat_rule.h>
99 +#include <linux/spinlock.h>
101 +#include "regexp/regexp.c"
103 +#include <linux/netfilter_ipv4/ipt_layer7.h>
104 +#include <linux/netfilter_ipv4/ip_tables.h>
106 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
107 +MODULE_LICENSE("GPL");
108 +MODULE_DESCRIPTION("iptables application layer match module");
109 +MODULE_VERSION("2.0");
111 +static int maxdatalen = 2048; // this is the default
112 +module_param(maxdatalen, int, 0444);
113 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
115 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
116 + #define DPRINTK(format,args...) printk(format,##args)
118 + #define DPRINTK(format,args...)
121 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
122 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
124 +/* Number of packets whose data we look at.
125 +This can be modified through /proc/net/layer7_numpackets */
126 +static int num_packets = 10;
128 +static struct pattern_cache {
129 + char * regex_string;
131 + struct pattern_cache * next;
132 +} * first_pattern_cache = NULL;
134 +/* I'm new to locking. Here are my assumptions:
136 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
137 + if they did, nothing awful would happen.
139 +- This code will never be processing the same packet twice at the same time,
140 + because iptables rules are traversed in order.
142 +- It doesn't matter if two packets from different connections are in here at
143 + the same time, because they don't share any data.
145 +- It _does_ matter if two packets from the same connection (or one from a
146 + master and one from its child) are here at the same time. In this case,
147 + we have to protect the conntracks and the list of compiled patterns.
149 +DEFINE_RWLOCK(ct_lock);
150 +DEFINE_SPINLOCK(list_lock);
152 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
153 +/* Converts an unfriendly string into a friendly one by
154 +replacing unprintables with periods and all whitespace with " ". */
155 +static char * friendly_print(unsigned char * s)
157 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
161 + if (net_ratelimit())
162 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
166 + for(i = 0; i < strlen(s); i++){
167 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
168 + else if(isspace(s[i])) f[i] = ' ';
175 +static char dec2hex(int i)
179 + return (char)(i + '0');
182 + return (char)(i - 10 + 'a');
185 + if (net_ratelimit())
186 + printk("Problem in dec2hex\n");
191 +static char * hex_print(unsigned char * s)
193 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
197 + if (net_ratelimit())
198 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
202 + for(i = 0; i < strlen(s); i++) {
203 + g[i*3 ] = dec2hex(s[i]/16);
204 + g[i*3 + 1] = dec2hex(s[i]%16);
213 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
214 +over again, it make sense to cache the results. */
215 +static regexp * compile_and_cache(char * regex_string, char * protocol)
217 + struct pattern_cache * node = first_pattern_cache;
218 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
219 + struct pattern_cache * tmp;
222 + while (node != NULL) {
223 + if (!strcmp(node->regex_string, regex_string))
224 + return node->pattern;
226 + last_pattern_cache = node;/* points at the last non-NULL node */
230 + /* If we reach the end of the list, then we have not yet cached
231 + the pattern for this regex. Let's do that now.
232 + Be paranoid about running out of memory to avoid list corruption. */
233 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
236 + if (net_ratelimit())
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);
245 + if(!tmp->regex_string || !tmp->pattern) {
246 + if (net_ratelimit())
247 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
248 + kfree(tmp->regex_string);
249 + kfree(tmp->pattern);
254 + /* Ok. The new node is all ready now. */
257 + if(first_pattern_cache == NULL) /* list is empty */
258 + first_pattern_cache = node; /* make node the beginning */
260 + last_pattern_cache->next = node; /* attach node to the end */
262 + /* copy the string and compile the regex */
263 + len = strlen(regex_string);
264 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
265 + node->pattern = regcomp(regex_string, &len);
266 + if ( !node->pattern ) {
267 + if (net_ratelimit())
268 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
269 + /* pattern is now cached as NULL, so we won't try again. */
272 + strcpy(node->regex_string, regex_string);
273 + return node->pattern;
276 +static int can_handle(const struct sk_buff *skb)
278 + if(!skb->nh.iph) /* not IP */
280 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
281 + skb->nh.iph->protocol != IPPROTO_UDP &&
282 + skb->nh.iph->protocol != IPPROTO_ICMP)
287 +/* Returns offset the into the skb->data that the application data starts */
288 +static int app_data_offset(const struct sk_buff *skb)
290 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
291 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
292 + int ip_hl = 4*skb->nh.iph->ihl;
294 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
295 + /* 12 == offset into TCP header for the header length field.
296 + Can't get this with skb->h.th->doff because the tcphdr
297 + struct doesn't get set when routing (this is confirmed to be
298 + true in Netfilter as well as QoS.) */
299 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
301 + return ip_hl + tcp_hl;
302 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
303 + return ip_hl + 8; /* UDP header is always 8 bytes */
304 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
305 + return ip_hl + 8; /* ICMP header is 8 bytes */
307 + if (net_ratelimit())
308 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
309 + return ip_hl + 8; /* something reasonable */
313 +/* handles whether there's a match when we aren't appending data anymore */
314 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
315 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
316 + struct ipt_layer7_info * info)
318 + /* If we're in here, throw the app data away */
319 + write_lock(&ct_lock);
320 + if(master_conntrack->layer7.app_data != NULL) {
322 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
323 + if(!master_conntrack->layer7.app_proto) {
324 + char * f = friendly_print(master_conntrack->layer7.app_data);
325 + char * g = hex_print(master_conntrack->layer7.app_data);
326 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
327 + strlen(f), TOTAL_PACKETS, f);
329 + DPRINTK("In hex: %s\n", g);
334 + kfree(master_conntrack->layer7.app_data);
335 + master_conntrack->layer7.app_data = NULL; /* don't free again */
337 + write_unlock(&ct_lock);
339 + if(master_conntrack->layer7.app_proto){
340 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
341 + write_lock(&ct_lock);
342 + if(!conntrack->layer7.app_proto) {
343 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
344 + if(!conntrack->layer7.app_proto){
345 + if (net_ratelimit())
346 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
347 + write_unlock(&ct_lock);
350 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
352 + write_unlock(&ct_lock);
354 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
357 + /* If not classified, set to "unknown" to distinguish from
358 + connections that are still being tested. */
359 + write_lock(&ct_lock);
360 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
361 + if(!master_conntrack->layer7.app_proto){
362 + if (net_ratelimit())
363 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
364 + write_unlock(&ct_lock);
367 + strcpy(master_conntrack->layer7.app_proto, "unknown");
368 + write_unlock(&ct_lock);
373 +/* add the new app data to the conntrack. Return number of bytes added. */
374 +static int add_data(struct ip_conntrack * master_conntrack,
375 + char * app_data, int appdatalen)
378 + int oldlength = master_conntrack->layer7.app_data_len;
380 + // This is a fix for a race condition by Deti Fliegl. However, I'm not
381 + // clear on whether the race condition exists or whether this really
382 + // fixes it. I might just be being dense... Anyway, if it's not really
383 + // a fix, all it does is waste a very small amount of time.
384 + if(!master_conntrack->layer7.app_data) return 0;
386 + /* Strip nulls. Make everything lower case (our regex lib doesn't
387 + do case insensitivity). Add it to the end of the current data. */
388 + for(i = 0; i < maxdatalen-oldlength-1 &&
389 + i < appdatalen; i++) {
390 + if(app_data[i] != '\0') {
391 + master_conntrack->layer7.app_data[length+oldlength] =
392 + /* the kernel version of tolower mungs 'upper ascii' */
393 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
398 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
399 + master_conntrack->layer7.app_data_len = length + oldlength;
404 +/* Returns true on match and false otherwise. */
405 +static int match(const struct sk_buff *skbin,
406 + const struct net_device *in, const struct net_device *out,
407 + const struct xt_match *match, const void *matchinfo,
408 + int offset, unsigned int protoff, int *hotdrop)
410 + /* sidestep const without getting a compiler warning... */
411 + struct sk_buff * skb = (struct sk_buff *)skbin;
413 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
414 + enum ip_conntrack_info master_ctinfo, ctinfo;
415 + struct nf_conn *master_conntrack;
416 + struct ip_conntrack *conntrack;
417 + unsigned char * app_data;
418 + unsigned int pattern_result, appdatalen;
419 + regexp * comppattern;
421 + if(!can_handle(skb)){
422 + DPRINTK("layer7: This is some protocol I can't handle.\n");
423 + return info->invert;
426 + /* Treat parent & all its children together as one connection, except
427 + for the purpose of setting conntrack->layer7.app_proto in the actual
428 + connection. This makes /proc/net/ip_conntrack more satisfying. */
429 + if(((conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) == NULL) ||
430 + ((master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo)) == NULL)) {
431 + return info->invert;
434 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
435 + while (master_ct(master_conntrack) != NULL)
436 + master_conntrack = master_ct(master_conntrack);
438 + /* if we've classified it or seen too many packets */
439 + if(TOTAL_PACKETS > num_packets ||
440 + master_conntrack->layer7.app_proto) {
442 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
444 + /* skb->cb[0] == seen. Don't do things twice if there are multiple l7
445 + rules. I'm not sure that using cb for this purpose is correct, even though
446 + it says "put your private variables there". But it doesn't look like it
447 + is being used for anything else in the skbs that make it here. */
448 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
450 + return (pattern_result ^ info->invert);
453 + if(skb_is_nonlinear(skb)){
454 + if(skb_linearize(skb) != 0){
455 + if (net_ratelimit())
456 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
457 + return info->invert;
461 + /* now that the skb is linearized, it's safe to set these. */
462 + app_data = skb->data + app_data_offset(skb);
463 + appdatalen = skb->tail - app_data;
465 + spin_lock_bh(&list_lock);
466 + /* the return value gets checked later, when we're ready to use it */
467 + comppattern = compile_and_cache(info->pattern, info->protocol);
468 + spin_unlock_bh(&list_lock);
470 + /* On the first packet of a connection, allocate space for app data */
471 + write_lock(&ct_lock);
472 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
473 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
474 + if(!master_conntrack->layer7.app_data){
475 + if (net_ratelimit())
476 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
477 + write_unlock(&ct_lock);
478 + return info->invert;
481 + master_conntrack->layer7.app_data[0] = '\0';
483 + write_unlock(&ct_lock);
485 + /* Can be here, but unallocated, if numpackets is increased near
486 + the beginning of a connection */
487 + if(master_conntrack->layer7.app_data == NULL)
488 + return (info->invert); /* unmatched */
492 + write_lock(&ct_lock);
493 + newbytes = add_data(master_conntrack, app_data, appdatalen);
494 + write_unlock(&ct_lock);
496 + if(newbytes == 0) { /* didn't add any data */
498 + /* Didn't match before, not going to match now */
499 + return info->invert;
503 + /* If looking for "unknown", then never match. "Unknown" means that
504 + we've given up; we're still trying with these packets. */
505 + read_lock(&ct_lock);
506 + if(!strcmp(info->protocol, "unknown")) {
507 + pattern_result = 0;
508 + /* If looking for "unset", then always match. "Unset" means that we
509 + haven't yet classified the connection. */
510 + } else if(!strcmp(info->protocol, "unset")) {
511 + pattern_result = 2;
512 + DPRINTK("layer7: matched unset: not yet classified (%d/%d packets)\n", TOTAL_PACKETS, num_packets);
513 + /* If the regexp failed to compile, don't bother running it */
514 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
515 + DPRINTK("layer7: matched %s\n", info->protocol);
516 + pattern_result = 1;
517 + } else pattern_result = 0;
518 + read_unlock(&ct_lock);
520 + if(pattern_result == 1) {
521 + write_lock(&ct_lock);
522 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
523 + if(!master_conntrack->layer7.app_proto){
524 + if (net_ratelimit())
525 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
526 + write_unlock(&ct_lock);
527 + return (pattern_result ^ info->invert);
529 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
530 + write_unlock(&ct_lock);
531 + } else if(pattern_result > 1) { /* cleanup from "unset" */
532 + pattern_result = 1;
535 + /* mark the packet seen */
538 + return (pattern_result ^ info->invert);
541 +static struct xt_match layer7_match = {
544 + .matchsize = sizeof(struct ipt_layer7_info),
549 +/* taken from drivers/video/modedb.c */
550 +static int my_atoi(const char *s)
557 + val = 10*val+(*s-'0');
565 +/* write out num_packets to userland. */
566 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
567 + int* eof, void * data)
569 + if(num_packets > 99 && net_ratelimit())
570 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
572 + page[0] = num_packets/10 + '0';
573 + page[1] = num_packets%10 + '0';
582 +/* Read in num_packets from userland */
583 +static int layer7_write_proc(struct file* file, const char* buffer,
584 + unsigned long count, void *data)
586 + char * foo = kmalloc(count, GFP_ATOMIC);
589 + if (net_ratelimit())
590 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
594 + if(copy_from_user(foo, buffer, count)) {
599 + num_packets = my_atoi(foo);
602 + /* This has an arbitrary limit to make the math easier. I'm lazy.
603 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
604 + if(num_packets > 99) {
605 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
607 + } else if(num_packets < 1) {
608 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
615 +/* register the proc file */
616 +static void layer7_init_proc(void)
618 + struct proc_dir_entry* entry;
619 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
620 + entry->read_proc = layer7_read_proc;
621 + entry->write_proc = layer7_write_proc;
624 +static void layer7_cleanup_proc(void)
626 + remove_proc_entry("layer7_numpackets", proc_net);
629 +static int __init ipt_layer7_init(void)
633 + layer7_init_proc();
634 + if(maxdatalen < 1) {
635 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
638 + /* This is not a hard limit. It's just here to prevent people from
639 + bringing their slow machines to a grinding halt. */
640 + else if(maxdatalen > 65536) {
641 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
642 + maxdatalen = 65536;
644 + return xt_register_match(&layer7_match);
647 +static void __exit ipt_layer7_fini(void)
649 + layer7_cleanup_proc();
650 + xt_unregister_match(&layer7_match);
653 +module_init(ipt_layer7_init);
654 +module_exit(ipt_layer7_fini);
655 Index: linux-2.6.21.5/net/ipv4/netfilter/Kconfig
656 ===================================================================
657 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Kconfig 2007-07-02 00:37:53.456287250 +0200
658 +++ linux-2.6.21.5/net/ipv4/netfilter/Kconfig 2007-07-02 01:21:17.231013000 +0200
661 To compile it as a module, choose M here. If unsure, say N.
663 +config IP_NF_MATCH_LAYER7
664 + tristate "Layer 7 match support (EXPERIMENTAL)"
665 + depends on IP_NF_IPTABLES && NF_CT_ACCT && NF_CONNTRACK && EXPERIMENTAL
667 + Say Y if you want to be able to classify connections (and their
668 + packets) based on regular expression matching of their application
669 + layer data. This is one way to classify applications such as
670 + peer-to-peer filesharing systems that do not always use the same
673 + To compile it as a module, choose M here. If unsure, say N.
675 +config IP_NF_MATCH_LAYER7_DEBUG
676 + bool "Layer 7 debugging output"
677 + depends on IP_NF_MATCH_LAYER7
679 + Say Y to get lots of debugging output.
681 config IP_NF_MATCH_TOS
682 tristate "TOS match support"
683 depends on IP_NF_IPTABLES
684 Index: linux-2.6.21.5/net/ipv4/netfilter/Makefile
685 ===================================================================
686 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Makefile 2007-07-02 00:37:53.464287750 +0200
687 +++ linux-2.6.21.5/net/ipv4/netfilter/Makefile 2007-07-02 00:43:58.191081750 +0200
689 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
690 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
692 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
695 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
696 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
697 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c
698 ===================================================================
699 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
700 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c 2007-07-02 00:37:55.648424250 +0200
703 + * regcomp and regexec -- regsub and regerror are elsewhere
704 + * @(#)regexp.c 1.3 of 18 April 87
706 + * Copyright (c) 1986 by University of Toronto.
707 + * Written by Henry Spencer. Not derived from licensed software.
709 + * Permission is granted to anyone to use this software for any
710 + * purpose on any computer system, and to redistribute it freely,
711 + * subject to the following restrictions:
713 + * 1. The author is not responsible for the consequences of use of
714 + * this software, no matter how awful, even if they arise
715 + * from defects in it.
717 + * 2. The origin of this software must not be misrepresented, either
718 + * by explicit claim or by omission.
720 + * 3. Altered versions must be plainly marked as such, and must not
721 + * be misrepresented as being the original software.
723 + * Beware that some of this code is subtly aware of the way operator
724 + * precedence is structured in regular expressions. Serious changes in
725 + * regular-expression syntax might require a total rethink.
727 + * This code was modified by Ethan Sommer to work within the kernel
728 + * (it now uses kmalloc etc..)
730 + * Modified slightly by Matthew Strait to use more modern C.
734 +#include "regmagic.h"
736 +/* added by ethan and matt. Lets it work in both kernel and user space.
737 +(So iptables can use it, for instance.) Yea, it goes both ways... */
739 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
741 + #define printk(format,args...) printf(format,##args)
744 +void regerror(char * s)
746 + printk("<3>Regexp: %s\n", s);
751 + * The "internal use only" fields in regexp.h are present to pass info from
752 + * compile to execute that permits the execute phase to run lots faster on
753 + * simple cases. They are:
755 + * regstart char that must begin a match; '\0' if none obvious
756 + * reganch is the match anchored (at beginning-of-line only)?
757 + * regmust string (pointer into program) that match must include, or NULL
758 + * regmlen length of regmust string
760 + * Regstart and reganch permit very fast decisions on suitable starting points
761 + * for a match, cutting down the work a lot. Regmust permits fast rejection
762 + * of lines that cannot possibly match. The regmust tests are costly enough
763 + * that regcomp() supplies a regmust only if the r.e. contains something
764 + * potentially expensive (at present, the only such thing detected is * or +
765 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
766 + * supplied because the test in regexec() needs it and regcomp() is computing
771 + * Structure for regexp "program". This is essentially a linear encoding
772 + * of a nondeterministic finite-state machine (aka syntax charts or
773 + * "railroad normal form" in parsing technology). Each node is an opcode
774 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
775 + * all nodes except BRANCH implement concatenation; a "next" pointer with
776 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
777 + * have one of the subtle syntax dependencies: an individual BRANCH (as
778 + * opposed to a collection of them) is never concatenated with anything
779 + * because of operator precedence.) The operand of some types of node is
780 + * a literal string; for others, it is a node leading into a sub-FSM. In
781 + * particular, the operand of a BRANCH node is the first node of the branch.
782 + * (NB this is *not* a tree structure: the tail of the branch connects
783 + * to the thing following the set of BRANCHes.) The opcodes are:
786 +/* definition number opnd? meaning */
787 +#define END 0 /* no End of program. */
788 +#define BOL 1 /* no Match "" at beginning of line. */
789 +#define EOL 2 /* no Match "" at end of line. */
790 +#define ANY 3 /* no Match any one character. */
791 +#define ANYOF 4 /* str Match any character in this string. */
792 +#define ANYBUT 5 /* str Match any character not in this string. */
793 +#define BRANCH 6 /* node Match this alternative, or the next... */
794 +#define BACK 7 /* no Match "", "next" ptr points backward. */
795 +#define EXACTLY 8 /* str Match this string. */
796 +#define NOTHING 9 /* no Match empty string. */
797 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
798 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
799 +#define OPEN 20 /* no Mark this point in input as start of #n. */
800 + /* OPEN+1 is number 1, etc. */
801 +#define CLOSE 30 /* no Analogous to OPEN. */
806 + * BRANCH The set of branches constituting a single choice are hooked
807 + * together with their "next" pointers, since precedence prevents
808 + * anything being concatenated to any individual branch. The
809 + * "next" pointer of the last BRANCH in a choice points to the
810 + * thing following the whole choice. This is also where the
811 + * final "next" pointer of each individual branch points; each
812 + * branch starts with the operand node of a BRANCH node.
814 + * BACK Normal "next" pointers all implicitly point forward; BACK
815 + * exists to make loop structures possible.
817 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
818 + * BRANCH structures using BACK. Simple cases (one character
819 + * per match) are implemented with STAR and PLUS for speed
820 + * and to minimize recursive plunges.
822 + * OPEN,CLOSE ...are numbered at compile time.
826 + * A node is one char of opcode followed by two chars of "next" pointer.
827 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
828 + * value is a positive offset from the opcode of the node containing it.
829 + * An operand, if any, simply follows the node. (Note that much of the
830 + * code generation knows about this implicit relationship.)
832 + * Using two bytes for the "next" pointer is vast overkill for most things,
833 + * but allows patterns to get big without disasters.
835 +#define OP(p) (*(p))
836 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
837 +#define OPERAND(p) ((p) + 3)
840 + * See regmagic.h for one further detail of program structure.
845 + * Utility definitions.
848 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
850 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
853 +#define FAIL(m) { regerror(m); return(NULL); }
854 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
855 +#define META "^$.[()|?+*\\"
858 + * Flags to be passed up and down.
860 +#define HASWIDTH 01 /* Known never to match null string. */
861 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
862 +#define SPSTART 04 /* Starts with * or +. */
863 +#define WORST 0 /* Worst case. */
866 + * Global work variables for regcomp().
868 +struct match_globals {
869 +char *reginput; /* String-input pointer. */
870 +char *regbol; /* Beginning of input, for ^ check. */
871 +char **regstartp; /* Pointer to startp array. */
872 +char **regendp; /* Ditto for endp. */
873 +char *regparse; /* Input-scan pointer. */
874 +int regnpar; /* () count. */
876 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
877 +long regsize; /* Code size. */
881 + * Forward declarations for regcomp()'s friends.
884 +#define STATIC static
886 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
887 +STATIC char *regbranch(struct match_globals *g, int *flagp);
888 +STATIC char *regpiece(struct match_globals *g, int *flagp);
889 +STATIC char *regatom(struct match_globals *g, int *flagp);
890 +STATIC char *regnode(struct match_globals *g, char op);
891 +STATIC char *regnext(struct match_globals *g, char *p);
892 +STATIC void regc(struct match_globals *g, char b);
893 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
894 +STATIC void regtail(struct match_globals *g, char *p, char *val);
895 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
898 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
905 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
906 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
907 + if (*scan1 == *scan2++)
915 + - regcomp - compile a regular expression into internal code
917 + * We can't allocate space until we know how big the compiled form will be,
918 + * but we can't compile it (and thus know how big it is) until we've got a
919 + * place to put the code. So we cheat: we compile it twice, once with code
920 + * generation turned off and size counting turned on, and once "for real".
921 + * This also means that we don't allocate space until we are sure that the
922 + * thing really will compile successfully, and we never have to move the
923 + * code and thus invalidate pointers into it. (Note that it has to be in
924 + * one piece because free() must be able to free it all.)
926 + * Beware that the optimization-preparation code in here knows about some
927 + * of the structure of the compiled regexp.
930 +regcomp(char *exp,int *patternsize)
932 + register regexp *r;
933 + register char *scan;
934 + register char *longest;
937 + struct match_globals g;
939 + /* commented out by ethan
940 + extern char *malloc();
944 + FAIL("NULL argument");
946 + /* First pass: determine size, legality. */
950 + g.regcode = &g.regdummy;
952 + if (reg(&g, 0, &flags) == NULL)
955 + /* Small enough for pointer-storage convention? */
956 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
957 + FAIL("regexp too big");
959 + /* Allocate space. */
960 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
961 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
963 + FAIL("out of space");
965 + /* Second pass: emit code. */
968 + g.regcode = r->program;
970 + if (reg(&g, 0, &flags) == NULL)
973 + /* Dig out information for optimizations. */
974 + r->regstart = '\0'; /* Worst-case defaults. */
978 + scan = r->program+1; /* First BRANCH. */
979 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
980 + scan = OPERAND(scan);
982 + /* Starting-point info. */
983 + if (OP(scan) == EXACTLY)
984 + r->regstart = *OPERAND(scan);
985 + else if (OP(scan) == BOL)
989 + * If there's something expensive in the r.e., find the
990 + * longest literal string that must appear and make it the
991 + * regmust. Resolve ties in favor of later strings, since
992 + * the regstart check works with the beginning of the r.e.
993 + * and avoiding duplication strengthens checking. Not a
994 + * strong reason, but sufficient in the absence of others.
996 + if (flags&SPSTART) {
999 + for (; scan != NULL; scan = regnext(&g, scan))
1000 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1001 + longest = OPERAND(scan);
1002 + len = strlen(OPERAND(scan));
1004 + r->regmust = longest;
1013 + - reg - regular expression, i.e. main body or parenthesized thing
1015 + * Caller must absorb opening parenthesis.
1017 + * Combining parenthesis handling with the base level of regular expression
1018 + * is a trifle forced, but the need to tie the tails of the branches to what
1019 + * follows makes it hard to avoid.
1022 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1024 + register char *ret;
1025 + register char *br;
1026 + register char *ender;
1027 + register int parno = 0; /* 0 makes gcc happy */
1030 + *flagp = HASWIDTH; /* Tentatively. */
1032 + /* Make an OPEN node, if parenthesized. */
1034 + if (g->regnpar >= NSUBEXP)
1035 + FAIL("too many ()");
1036 + parno = g->regnpar;
1038 + ret = regnode(g, OPEN+parno);
1042 + /* Pick up the branches, linking them together. */
1043 + br = regbranch(g, &flags);
1047 + regtail(g, ret, br); /* OPEN -> first. */
1050 + if (!(flags&HASWIDTH))
1051 + *flagp &= ~HASWIDTH;
1052 + *flagp |= flags&SPSTART;
1053 + while (*g->regparse == '|') {
1055 + br = regbranch(g, &flags);
1058 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1059 + if (!(flags&HASWIDTH))
1060 + *flagp &= ~HASWIDTH;
1061 + *flagp |= flags&SPSTART;
1064 + /* Make a closing node, and hook it on the end. */
1065 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1066 + regtail(g, ret, ender);
1068 + /* Hook the tails of the branches to the closing node. */
1069 + for (br = ret; br != NULL; br = regnext(g, br))
1070 + regoptail(g, br, ender);
1072 + /* Check for proper termination. */
1073 + if (paren && *g->regparse++ != ')') {
1074 + FAIL("unmatched ()");
1075 + } else if (!paren && *g->regparse != '\0') {
1076 + if (*g->regparse == ')') {
1077 + FAIL("unmatched ()");
1079 + FAIL("junk on end"); /* "Can't happen". */
1087 + - regbranch - one alternative of an | operator
1089 + * Implements the concatenation operator.
1092 +regbranch(struct match_globals *g, int *flagp)
1094 + register char *ret;
1095 + register char *chain;
1096 + register char *latest;
1099 + *flagp = WORST; /* Tentatively. */
1101 + ret = regnode(g, BRANCH);
1103 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1104 + latest = regpiece(g, &flags);
1105 + if (latest == NULL)
1107 + *flagp |= flags&HASWIDTH;
1108 + if (chain == NULL) /* First piece. */
1109 + *flagp |= flags&SPSTART;
1111 + regtail(g, chain, latest);
1114 + if (chain == NULL) /* Loop ran zero times. */
1115 + (void) regnode(g, NOTHING);
1121 + - regpiece - something followed by possible [*+?]
1123 + * Note that the branching code sequences used for ? and the general cases
1124 + * of * and + are somewhat optimized: they use the same NOTHING node as
1125 + * both the endmarker for their branch list and the body of the last branch.
1126 + * It might seem that this node could be dispensed with entirely, but the
1127 + * endmarker role is not redundant.
1130 +regpiece(struct match_globals *g, int *flagp)
1132 + register char *ret;
1134 + register char *next;
1137 + ret = regatom(g, &flags);
1141 + op = *g->regparse;
1142 + if (!ISMULT(op)) {
1147 + if (!(flags&HASWIDTH) && op != '?')
1148 + FAIL("*+ operand could be empty");
1149 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1151 + if (op == '*' && (flags&SIMPLE))
1152 + reginsert(g, STAR, ret);
1153 + else if (op == '*') {
1154 + /* Emit x* as (x&|), where & means "self". */
1155 + reginsert(g, BRANCH, ret); /* Either x */
1156 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1157 + regoptail(g, ret, ret); /* back */
1158 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1159 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1160 + } else if (op == '+' && (flags&SIMPLE))
1161 + reginsert(g, PLUS, ret);
1162 + else if (op == '+') {
1163 + /* Emit x+ as x(&|), where & means "self". */
1164 + next = regnode(g, BRANCH); /* Either */
1165 + regtail(g, ret, next);
1166 + regtail(g, regnode(g, BACK), ret); /* loop back */
1167 + regtail(g, next, regnode(g, BRANCH)); /* or */
1168 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1169 + } else if (op == '?') {
1170 + /* Emit x? as (x|) */
1171 + reginsert(g, BRANCH, ret); /* Either x */
1172 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1173 + next = regnode(g, NOTHING); /* null. */
1174 + regtail(g, ret, next);
1175 + regoptail(g, ret, next);
1178 + if (ISMULT(*g->regparse))
1179 + FAIL("nested *?+");
1185 + - regatom - the lowest level
1187 + * Optimization: gobbles an entire sequence of ordinary characters so that
1188 + * it can turn them into a single node, which is smaller to store and
1189 + * faster to run. Backslashed characters are exceptions, each becoming a
1190 + * separate node; the code is simpler that way and it's not worth fixing.
1193 +regatom(struct match_globals *g, int *flagp)
1195 + register char *ret;
1198 + *flagp = WORST; /* Tentatively. */
1200 + switch (*g->regparse++) {
1202 + ret = regnode(g, BOL);
1205 + ret = regnode(g, EOL);
1208 + ret = regnode(g, ANY);
1209 + *flagp |= HASWIDTH|SIMPLE;
1212 + register int class;
1213 + register int classend;
1215 + if (*g->regparse == '^') { /* Complement of range. */
1216 + ret = regnode(g, ANYBUT);
1219 + ret = regnode(g, ANYOF);
1220 + if (*g->regparse == ']' || *g->regparse == '-')
1221 + regc(g, *g->regparse++);
1222 + while (*g->regparse != '\0' && *g->regparse != ']') {
1223 + if (*g->regparse == '-') {
1225 + if (*g->regparse == ']' || *g->regparse == '\0')
1228 + class = UCHARAT(g->regparse-2)+1;
1229 + classend = UCHARAT(g->regparse);
1230 + if (class > classend+1)
1231 + FAIL("invalid [] range");
1232 + for (; class <= classend; class++)
1237 + regc(g, *g->regparse++);
1240 + if (*g->regparse != ']')
1241 + FAIL("unmatched []");
1243 + *flagp |= HASWIDTH|SIMPLE;
1247 + ret = reg(g, 1, &flags);
1250 + *flagp |= flags&(HASWIDTH|SPSTART);
1255 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1260 + FAIL("?+* follows nothing");
1263 + if (*g->regparse == '\0')
1264 + FAIL("trailing \\");
1265 + ret = regnode(g, EXACTLY);
1266 + regc(g, *g->regparse++);
1268 + *flagp |= HASWIDTH|SIMPLE;
1272 + register char ender;
1275 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1277 + FAIL("internal disaster");
1278 + ender = *(g->regparse+len);
1279 + if (len > 1 && ISMULT(ender))
1280 + len--; /* Back off clear of ?+* operand. */
1281 + *flagp |= HASWIDTH;
1284 + ret = regnode(g, EXACTLY);
1286 + regc(g, *g->regparse++);
1298 + - regnode - emit a node
1300 +static char * /* Location. */
1301 +regnode(struct match_globals *g, char op)
1303 + register char *ret;
1304 + register char *ptr;
1307 + if (ret == &g->regdummy) {
1314 + *ptr++ = '\0'; /* Null "next" pointer. */
1322 + - regc - emit (if appropriate) a byte of code
1325 +regc(struct match_globals *g, char b)
1327 + if (g->regcode != &g->regdummy)
1328 + *g->regcode++ = b;
1334 + - reginsert - insert an operator in front of already-emitted operand
1336 + * Means relocating the operand.
1339 +reginsert(struct match_globals *g, char op, char* opnd)
1341 + register char *src;
1342 + register char *dst;
1343 + register char *place;
1345 + if (g->regcode == &g->regdummy) {
1353 + while (src > opnd)
1356 + place = opnd; /* Op node, where operand used to be. */
1363 + - regtail - set the next-pointer at the end of a node chain
1366 +regtail(struct match_globals *g, char *p, char *val)
1368 + register char *scan;
1369 + register char *temp;
1370 + register int offset;
1372 + if (p == &g->regdummy)
1375 + /* Find last node. */
1378 + temp = regnext(g, scan);
1384 + if (OP(scan) == BACK)
1385 + offset = scan - val;
1387 + offset = val - scan;
1388 + *(scan+1) = (offset>>8)&0377;
1389 + *(scan+2) = offset&0377;
1393 + - regoptail - regtail on operand of first argument; nop if operandless
1396 +regoptail(struct match_globals *g, char *p, char *val)
1398 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1399 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1401 + regtail(g, OPERAND(p), val);
1405 + * regexec and friends
1412 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1413 +STATIC int regmatch(struct match_globals *g, char *prog);
1414 +STATIC int regrepeat(struct match_globals *g, char *p);
1417 +int regnarrate = 0;
1419 +STATIC char *regprop(char *op);
1423 + - regexec - match a regexp against a string
1426 +regexec(regexp *prog, char *string)
1429 + struct match_globals g;
1431 + /* Be paranoid... */
1432 + if (prog == NULL || string == NULL) {
1433 + printk("<3>Regexp: NULL parameter\n");
1437 + /* Check validity of program. */
1438 + if (UCHARAT(prog->program) != MAGIC) {
1439 + printk("<3>Regexp: corrupted program\n");
1443 + /* If there is a "must appear" string, look for it. */
1444 + if (prog->regmust != NULL) {
1446 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1447 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1448 + break; /* Found it. */
1451 + if (s == NULL) /* Not present. */
1455 + /* Mark beginning of line for ^ . */
1456 + g.regbol = string;
1458 + /* Simplest case: anchored match need be tried only once. */
1459 + if (prog->reganch)
1460 + return(regtry(&g, prog, string));
1462 + /* Messy cases: unanchored match. */
1464 + if (prog->regstart != '\0')
1465 + /* We know what char it must start with. */
1466 + while ((s = strchr(s, prog->regstart)) != NULL) {
1467 + if (regtry(&g, prog, s))
1472 + /* We don't -- general case. */
1474 + if (regtry(&g, prog, s))
1476 + } while (*s++ != '\0');
1483 + - regtry - try match at specific point
1485 +static int /* 0 failure, 1 success */
1486 +regtry(struct match_globals *g, regexp *prog, char *string)
1489 + register char **sp;
1490 + register char **ep;
1492 + g->reginput = string;
1493 + g->regstartp = prog->startp;
1494 + g->regendp = prog->endp;
1496 + sp = prog->startp;
1498 + for (i = NSUBEXP; i > 0; i--) {
1502 + if (regmatch(g, prog->program + 1)) {
1503 + prog->startp[0] = string;
1504 + prog->endp[0] = g->reginput;
1511 + - regmatch - main matching routine
1513 + * Conceptually the strategy is simple: check to see whether the current
1514 + * node matches, call self recursively to see whether the rest matches,
1515 + * and then act accordingly. In practice we make some effort to avoid
1516 + * recursion, in particular by going through "ordinary" nodes (that don't
1517 + * need to know whether the rest of the match failed) by a loop instead of
1520 +static int /* 0 failure, 1 success */
1521 +regmatch(struct match_globals *g, char *prog)
1523 + register char *scan = prog; /* Current node. */
1524 + char *next; /* Next node. */
1527 + if (scan != NULL && regnarrate)
1528 + fprintf(stderr, "%s(\n", regprop(scan));
1530 + while (scan != NULL) {
1533 + fprintf(stderr, "%s...\n", regprop(scan));
1535 + next = regnext(g, scan);
1537 + switch (OP(scan)) {
1539 + if (g->reginput != g->regbol)
1543 + if (*g->reginput != '\0')
1547 + if (*g->reginput == '\0')
1553 + register char *opnd;
1555 + opnd = OPERAND(scan);
1556 + /* Inline the first character, for speed. */
1557 + if (*opnd != *g->reginput)
1559 + len = strlen(opnd);
1560 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1562 + g->reginput += len;
1566 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1571 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1588 + register char *save;
1590 + no = OP(scan) - OPEN;
1591 + save = g->reginput;
1593 + if (regmatch(g, next)) {
1595 + * Don't set startp if some later
1596 + * invocation of the same parentheses
1599 + if (g->regstartp[no] == NULL)
1600 + g->regstartp[no] = save;
1617 + register char *save;
1619 + no = OP(scan) - CLOSE;
1620 + save = g->reginput;
1622 + if (regmatch(g, next)) {
1624 + * Don't set endp if some later
1625 + * invocation of the same parentheses
1628 + if (g->regendp[no] == NULL)
1629 + g->regendp[no] = save;
1636 + register char *save;
1638 + if (OP(next) != BRANCH) /* No choice. */
1639 + next = OPERAND(scan); /* Avoid recursion. */
1642 + save = g->reginput;
1643 + if (regmatch(g, OPERAND(scan)))
1645 + g->reginput = save;
1646 + scan = regnext(g, scan);
1647 + } while (scan != NULL && OP(scan) == BRANCH);
1655 + register char nextch;
1657 + register char *save;
1661 + * Lookahead to avoid useless match attempts
1662 + * when we know what character comes next.
1665 + if (OP(next) == EXACTLY)
1666 + nextch = *OPERAND(next);
1667 + min = (OP(scan) == STAR) ? 0 : 1;
1668 + save = g->reginput;
1669 + no = regrepeat(g, OPERAND(scan));
1670 + while (no >= min) {
1671 + /* If it could work, try it. */
1672 + if (nextch == '\0' || *g->reginput == nextch)
1673 + if (regmatch(g, next))
1675 + /* Couldn't or didn't -- back up. */
1677 + g->reginput = save + no;
1683 + return(1); /* Success! */
1686 + printk("<3>Regexp: memory corruption\n");
1695 + * We get here only if there's trouble -- normally "case END" is
1696 + * the terminating point.
1698 + printk("<3>Regexp: corrupted pointers\n");
1703 + - regrepeat - repeatedly match something simple, report how many
1706 +regrepeat(struct match_globals *g, char *p)
1708 + register int count = 0;
1709 + register char *scan;
1710 + register char *opnd;
1712 + scan = g->reginput;
1713 + opnd = OPERAND(p);
1716 + count = strlen(scan);
1720 + while (*opnd == *scan) {
1726 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1732 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1737 + default: /* Oh dear. Called inappropriately. */
1738 + printk("<3>Regexp: internal foulup\n");
1739 + count = 0; /* Best compromise. */
1742 + g->reginput = scan;
1748 + - regnext - dig the "next" pointer out of a node
1751 +regnext(struct match_globals *g, char *p)
1753 + register int offset;
1755 + if (p == &g->regdummy)
1762 + if (OP(p) == BACK)
1770 +STATIC char *regprop();
1773 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1779 + register char op = EXACTLY; /* Arbitrary non-END op. */
1780 + register char *next;
1781 + /* extern char *strchr(); */
1784 + s = r->program + 1;
1785 + while (op != END) { /* While that wasn't END last time... */
1787 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1788 + next = regnext(s);
1789 + if (next == NULL) /* Next ptr. */
1792 + printf("(%d)", (s-r->program)+(next-s));
1794 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1795 + /* Literal string, where present. */
1796 + while (*s != '\0') {
1805 + /* Header fields of interest. */
1806 + if (r->regstart != '\0')
1807 + printf("start `%c' ", r->regstart);
1809 + printf("anchored ");
1810 + if (r->regmust != NULL)
1811 + printf("must have \"%s\"", r->regmust);
1816 + - regprop - printable representation of opcode
1823 + static char buf[BUFLEN];
1867 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1879 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1889 + printk("<3>Regexp: corrupted opcode\n");
1893 + strncat(buf, p, BUFLEN-strlen(buf));
1899 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h
1900 ===================================================================
1901 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1902 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h 2007-07-02 00:37:55.700427500 +0200
1905 + * Definitions etc. for regexp(3) routines.
1907 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1908 + * not the System V one.
1916 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1917 +which contains a version of this library, says:
1920 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1921 + * will not work properly.
1924 +However, it looks rather like this library is limited to 10. If you think
1925 +otherwise, let us know.
1929 +typedef struct regexp {
1930 + char *startp[NSUBEXP];
1931 + char *endp[NSUBEXP];
1932 + char regstart; /* Internal use only. */
1933 + char reganch; /* Internal use only. */
1934 + char *regmust; /* Internal use only. */
1935 + int regmlen; /* Internal use only. */
1936 + char program[1]; /* Unwarranted chumminess with compiler. */
1939 +regexp * regcomp(char *exp, int *patternsize);
1940 +int regexec(regexp *prog, char *string);
1941 +void regsub(regexp *prog, char *source, char *dest);
1942 +void regerror(char *s);
1945 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h
1946 ===================================================================
1947 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1948 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h 2007-07-02 00:37:55.724429000 +0200
1951 + * The first byte of the regexp internal "program" is actually this magic
1952 + * number; the start node begins in the second byte.
1955 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c
1956 ===================================================================
1957 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1958 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c 2007-07-02 00:37:55.752430750 +0200
1962 + * @(#)regsub.c 1.3 of 2 April 86
1964 + * Copyright (c) 1986 by University of Toronto.
1965 + * Written by Henry Spencer. Not derived from licensed software.
1967 + * Permission is granted to anyone to use this software for any
1968 + * purpose on any computer system, and to redistribute it freely,
1969 + * subject to the following restrictions:
1971 + * 1. The author is not responsible for the consequences of use of
1972 + * this software, no matter how awful, even if they arise
1973 + * from defects in it.
1975 + * 2. The origin of this software must not be misrepresented, either
1976 + * by explicit claim or by omission.
1978 + * 3. Altered versions must be plainly marked as such, and must not
1979 + * be misrepresented as being the original software.
1982 + * This code was modified by Ethan Sommer to work within the kernel
1983 + * (it now uses kmalloc etc..)
1986 +#include "regexp.h"
1987 +#include "regmagic.h"
1988 +#include <linux/string.h>
1992 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1994 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1998 +//void regerror(char * s)
2000 +// printk("regexp(3): %s", s);
2001 +// /* NOTREACHED */
2006 + - regsub - perform substitutions after a regexp match
2009 +regsub(regexp * prog, char * source, char * dest)
2011 + register char *src;
2012 + register char *dst;
2017 + /* Not necessary and gcc doesn't like it -MLS */
2018 + /*extern char *strncpy();*/
2020 + if (prog == NULL || source == NULL || dest == NULL) {
2021 + regerror("NULL parm to regsub");
2024 + if (UCHARAT(prog->program) != MAGIC) {
2025 + regerror("damaged regexp fed to regsub");
2031 + while ((c = *src++) != '\0') {
2034 + else if (c == '\\' && '0' <= *src && *src <= '9')
2035 + no = *src++ - '0';
2039 + if (no < 0) { /* Ordinary character. */
2040 + if (c == '\\' && (*src == '\\' || *src == '&'))
2043 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2044 + len = prog->endp[no] - prog->startp[no];
2045 + (void) strncpy(dst, prog->startp[no], len);
2047 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2048 + regerror("damaged match string");
2055 Index: linux-2.6.21.5/include/net/netfilter/nf_conntrack.h
2056 ===================================================================
2057 --- linux-2.6.21.5.orig/include/net/netfilter/nf_conntrack.h 2007-07-02 00:49:22.815369500 +0200
2058 +++ linux-2.6.21.5/include/net/netfilter/nf_conntrack.h 2007-07-02 00:56:21.413530250 +0200
2059 @@ -128,6 +128,21 @@
2063 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
2066 + * e.g. "http". NULL before decision. "unknown" after decision
2071 + * application layer data so far. NULL after match decision.
2074 + unsigned int app_data_len;
2078 /* Storage reserved for other modules: */
2079 union nf_conntrack_proto proto;