1 diff -uNr linux-2.6.7.orig/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.6.7/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux-2.6.7.orig/include/linux/netfilter_ipv4/ip_conntrack.h 2004-07-14 09:43:26.000000000 +0200
3 +++ linux-2.6.7/include/linux/netfilter_ipv4/ip_conntrack.h 2004-07-14 09:48:56.039943024 +0200
8 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
10 + unsigned int numpackets; /* surely this is kept track of somewhere else, right? I can't find it... */
11 + char * app_proto; /* "http", "ftp", etc. NULL if unclassifed */
13 + /* the application layer data so far. NULL if ->numpackets > numpackets */
16 + unsigned int app_data_len;
21 /* get master conntrack via master expectation */
22 diff -uNr linux-2.6.7.orig/include/linux/netfilter_ipv4/ipt_layer7.h linux-2.6.7/include/linux/netfilter_ipv4/ipt_layer7.h
23 --- linux-2.6.7.orig/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
24 +++ linux-2.6.7/include/linux/netfilter_ipv4/ipt_layer7.h 2004-07-14 09:48:56.041942720 +0200
27 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
28 + http://l7-filter.sf.net
30 + This program is free software; you can redistribute it and/or
31 + modify it under the terms of the GNU General Public License
32 + as published by the Free Software Foundation; either version
33 + 2 of the License, or (at your option) any later version.
34 + http://www.gnu.org/licenses/gpl.txt
37 +#ifndef _IPT_LAYER7_H
38 +#define _IPT_LAYER7_H
40 +#define MAX_PATTERN_LEN 8192
41 +#define MAX_PROTOCOL_LEN 256
43 +typedef char *(*proc_ipt_search) (char *, char, char *);
45 +struct ipt_layer7_info {
46 + char protocol[MAX_PROTOCOL_LEN];
48 + char pattern[MAX_PATTERN_LEN];
51 +#endif /* _IPT_LAYER7_H */
52 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/ip_conntrack_core.c linux-2.6.7/net/ipv4/netfilter/ip_conntrack_core.c
53 --- linux-2.6.7.orig/net/ipv4/netfilter/ip_conntrack_core.c 2004-07-14 09:43:27.000000000 +0200
54 +++ linux-2.6.7/net/ipv4/netfilter/ip_conntrack_core.c 2004-07-14 09:48:56.054940744 +0200
57 kmem_cache_free(ip_conntrack_expect_cachep, ct->master);
60 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
61 + /* This ought to get free'd somewhere. How about here? */
62 + if(ct->layer7.app_proto) /* this is sufficient, right? */
63 + kfree(ct->layer7.app_proto);
64 + if(ct->layer7.app_data)
65 + kfree(ct->layer7.app_data);
68 WRITE_UNLOCK(&ip_conntrack_lock);
71 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.6.7/net/ipv4/netfilter/ip_conntrack_standalone.c
72 --- linux-2.6.7.orig/net/ipv4/netfilter/ip_conntrack_standalone.c 2004-07-14 09:43:27.000000000 +0200
73 +++ linux-2.6.7/net/ipv4/netfilter/ip_conntrack_standalone.c 2004-07-14 09:49:35.000000000 +0200
78 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
79 + if (conntrack->layer7.app_proto)
80 + if (seq_printf(s, "l7proto=%s ", conntrack->layer7.app_proto))
84 if (seq_printf(s, "\n"))
87 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/ipt_layer7.c linux-2.6.7/net/ipv4/netfilter/ipt_layer7.c
88 --- linux-2.6.7.orig/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
89 +++ linux-2.6.7/net/ipv4/netfilter/ipt_layer7.c 2004-07-14 09:48:56.060939832 +0200
92 + Kernel module to match application layer (OSI layer 7)
93 + data in connections.
95 + http://l7-filter.sf.net
97 + By Matthew Strait and Ethan Sommer, 2003.
99 + This program is free software; you can redistribute it and/or
100 + modify it under the terms of the GNU General Public License
101 + as published by the Free Software Foundation; either version
102 + 2 of the License, or (at your option) any later version.
103 + http://www.gnu.org/licenses/gpl.txt
105 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
106 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
109 +#include <linux/module.h>
110 +#include <linux/skbuff.h>
111 +#include <linux/netfilter_ipv4/ip_conntrack.h>
112 +#include <linux/proc_fs.h>
113 +#include <linux/ctype.h>
115 +#include <net/tcp.h>
117 +#include "regexp/regexp.c"
119 +#include <linux/netfilter_ipv4/ipt_layer7.h>
120 +#include <linux/netfilter_ipv4/ip_tables.h>
122 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
123 +MODULE_LICENSE("GPL");
124 +MODULE_DESCRIPTION("iptables application layer match module");
126 +#if defined(CONFIG_IP_NF_MATCH_LAYER7_DEBUG)
127 + #define DPRINTK(format,args...) printk(format,##args)
129 + #define DPRINTK(format,args...)
132 +/* Number of packets whose data we look at.
133 +This can be modified through /proc/net/layer7_numpackets */
134 +static int num_packets = 8;
136 +static struct pattern_cache {
137 + char * regex_string;
139 + struct pattern_cache * next;
140 +} * first_pattern_cache = NULL;
142 +/* I'm new to locking. Here are my assumptions:
144 +- No one is going to write to /proc/net/layer7_numpackets over and over
145 + within a short period of time, and if they did, nothing awful would happen.
147 +- This code will never be processing the same packet twice at the same time,
148 + because iptables rules need to be traversed in order.
150 +- It doesn't matter if two packets from different connections are in here at
151 + the same time, because they don't share any data.
153 +- It _does_ matter if two packets from the same connection are here at the same
154 + time. In this case, the things we have to protect are the conntracks and
155 + the list of compiled patterns.
157 +DECLARE_RWLOCK(ct_lock);
158 +DECLARE_LOCK(list_lock);
160 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
161 +over again, it make sense to cache the results. */
162 +static regexp * compile_and_cache(char * regex_string, char * protocol)
164 + struct pattern_cache * node = first_pattern_cache;
165 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
166 + struct pattern_cache * tmp;
169 + while (node != NULL) {
170 + if (!strcmp(node->regex_string, regex_string))
171 + return node->pattern;
173 + last_pattern_cache = node;/* points at the last non-NULL node */
177 + /* If we reach the end of the list, then we have not yet cached
178 + the pattern for this regex. Let's do that now.
179 + Be paranoid about running out of memory to avoid list corruption. */
180 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
183 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
187 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
188 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
190 + if(!tmp->regex_string || !tmp->pattern) {
191 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
192 + kfree(tmp->regex_string);
193 + kfree(tmp->pattern);
199 + /* Ok. The new node is all ready now. */
202 + if(first_pattern_cache == NULL) /* list is empty */
203 + first_pattern_cache = node; /* make node the beginning */
205 + last_pattern_cache->next = node; /* attach node to the end */
207 + /* copy the string and compile the regex */
208 + len = strlen(regex_string);
209 + node->pattern = regcomp(regex_string, &len);
210 + if ( !node->pattern ) {
211 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n",
212 + regex_string, protocol);
213 + /* pattern is now cached as NULL, so we won't try again. */
216 + strcpy(node->regex_string, regex_string);
217 + return node->pattern;
220 +#if CONFIG_IP_NF_MATCH_LAYER7_DEBUG
221 +/* Converts an unfriendly string into a friendly one by
222 +replacing unprintables with periods and all whitespace with " ". */
223 +static char * friendly_print(unsigned char * s)
225 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
229 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
233 + for(i = 0; i < strlen(s); i++){
234 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
235 + else if(isspace(s[i])) f[i] = ' ';
242 +static char dec2hex(int i)
246 + return (char)(i + '0');
249 + return (char)(i - 10 + 'a');
252 + printk("Problem in dec2hex\n");
257 +static char * hex_print(unsigned char * s)
259 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
263 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
267 + for(i = 0; i < strlen(s); i++) {
268 + g[i*3 ] = dec2hex(s[i]/16);
269 + g[i*3 + 1] = dec2hex(s[i]%16);
279 +/* The following functions are here in case we get ported into an environment
280 +(ebtables?) where skb->nh.iph->protocol isn't set. They assume that skb->data
281 +points at the beginning of the IP datagram, which is true for iptables (but in
282 +QoS it points to the beginning of the Ethernet frame). */
284 +#define IP_PROTO_OFFSET 9
285 +static int is_tcp_over_ipv4 (const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_TCP );}
286 +static int is_udp_over_ipv4 (const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_UDP );}
287 +static int is_icmp_over_ipv4(const struct sk_buff *skb){return(skb->data[IP_PROTO_OFFSET]==IPPROTO_ICMP);}
290 +static int can_handle(const struct sk_buff *skb)
292 + if(!skb->nh.iph) /* not IP */
294 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
295 + skb->nh.iph->protocol != IPPROTO_UDP &&
296 + skb->nh.iph->protocol != IPPROTO_ICMP)
301 +/* Returns offset the into the skb->data that the application data starts */
302 +static int app_data_offset(const struct sk_buff *skb)
304 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
305 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
306 + int ip_hl = 4*skb->nh.iph->ihl;
308 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
309 + /* 12 == offset into TCP header for the header length field.
310 + Can't get this with skb->h.th->doff because the tcphdr
311 + struct doesn't get set when routing (this is confirmed to be
312 + true in Netfilter as well as QoS.) */
313 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
315 + return ip_hl + tcp_hl;
316 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
317 + return ip_hl + 8; /* UDP header is always 8 bytes */
318 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
319 + return ip_hl + 8; /* ICMP header is 8 bytes */
321 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
322 + return ip_hl + 8; /* something reasonable */
326 +/* handles whether there's a match when we aren't appending data anymore */
327 +static int match_no_append(struct ip_conntrack * conntrack,
328 + struct ip_conntrack * master_conntrack,
329 + struct ipt_layer7_info * info)
331 + /* If we're in here, we don't care about the app data anymore */
332 + WRITE_LOCK(&ct_lock);
333 + if(master_conntrack->layer7.app_data != NULL) {
335 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
336 + if(!master_conntrack->layer7.app_proto) {
337 + char * f = friendly_print(master_conntrack->layer7.app_data);
338 + char * g = hex_print(master_conntrack->layer7.app_data);
339 + DPRINTK("\nGave up on the %d length stream: \n%s\n",
340 + master_conntrack->layer7.app_data_len, f);
341 + DPRINTK("\nIn hex: %s\n", g);
347 + kfree(master_conntrack->layer7.app_data);
348 + master_conntrack->layer7.app_data = NULL; /* don't free again */
350 + WRITE_UNLOCK(&ct_lock);
352 + /* Is top-level master (possibly self) classified? */
353 + if(master_conntrack->layer7.app_proto) {
354 + if(!strcmp(master_conntrack->layer7.app_proto, info->protocol))
356 + /* set own .protocol (for /proc/net/ip_conntrack) */
357 + WRITE_LOCK(&ct_lock);
358 + if(!conntrack->layer7.app_proto) {
359 + conntrack->layer7.app_proto = kmalloc(strlen(info->protocol), GFP_ATOMIC);
360 + if(!conntrack->layer7.app_proto){
361 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
362 + WRITE_UNLOCK(&ct_lock);
366 + strcpy(conntrack->layer7.app_proto, info->protocol);
368 + WRITE_UNLOCK(&ct_lock);
372 + } else return 0; /* no clasification */
375 +/* add the new app data to the conntrack. Return number of bytes added. */
376 +static int add_data(struct ip_conntrack * master_conntrack,
377 + char * app_data, int appdatalen)
380 + int oldlength = master_conntrack->layer7.app_data_len;
382 + /* Strip nulls. Make everything lower case (our regex lib doesn't
383 + do case insensitivity). Add it to the end of the current data. */
384 + for(i = 0; i < CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN-oldlength-1 &&
385 + i < appdatalen; i++) {
386 + if(app_data[i] != '\0') {
387 + master_conntrack->layer7.app_data[length+oldlength] =
388 + /* the kernel version of tolower mungs 'upper ascii' */
389 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
394 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
395 + master_conntrack->layer7.app_data_len = length + oldlength;
400 +/* Returns true on match and false otherwise. */
401 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
402 + const struct net_device *out, const void *matchinfo,
403 + int offset, int *hotdrop)
405 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
406 + enum ip_conntrack_info master_ctinfo, ctinfo;
407 + struct ip_conntrack *master_conntrack, *conntrack;
408 + unsigned char * app_data;
409 + unsigned int pattern_result, appdatalen;
410 + regexp * comppattern;
412 + if(!can_handle(skb)){
413 + DPRINTK("layer7: This is some protocol I can't handle\n");
414 + return info->invert;
417 + LOCK_BH(&list_lock);
418 + comppattern = compile_and_cache(info->pattern, info->protocol);
419 + UNLOCK_BH(&list_lock);
420 + /* the return value gets checked later, when we're ready to use it */
422 + app_data = skb->data + app_data_offset(skb);
423 + appdatalen = skb->tail - app_data;
425 + /* Treat the parent and all its children together as one connection,
426 + except for the purpose of setting conntrack->layer7.pattern in the
427 + actual connection. This makes /proc/net/ip_conntrack somewhat more
429 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
430 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
431 + DPRINTK("layer7: packet is not from a known connection, giving up.\n");
432 + return info->invert;
435 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
436 + while (master_ct(master_conntrack) != NULL)
437 + master_conntrack = master_ct(master_conntrack);
439 + /* skb->cb[0] == seen. Avoid doing things twice if there are two layer7
440 + rules. I'm not sure that using cb for this purpose is correct, although
441 + it says "put your private variables there" and this seems to qualify.
442 + But it doesn't look like it's being used for anything else in the
443 + sk_buffs that make it here. I'm open to suggestions for how to be able
444 + to write to cb without making the compiler angry. That I can't figure
445 + this out is an argument against this being correct. */
447 + WRITE_LOCK(&ct_lock);
448 + master_conntrack->layer7.numpackets++;/*starts at 0 via memset*/
449 + WRITE_UNLOCK(&ct_lock);
452 + /* On the first packet of a connection, allocate space for app data */
453 + WRITE_LOCK(&ct_lock);
454 + if(master_conntrack->layer7.numpackets == 1 && !skb->cb[0]) {
455 + master_conntrack->layer7.app_data = kmalloc(CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN, GFP_ATOMIC);
456 + if(!master_conntrack->layer7.app_data){
457 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
458 + WRITE_UNLOCK(&ct_lock);
459 + return info->invert;
462 + master_conntrack->layer7.app_data[0] = '\0';
464 + WRITE_UNLOCK(&ct_lock);
466 + /* if we've classified it or seen too many packets */
467 + if(master_conntrack->layer7.numpackets > num_packets ||
468 + master_conntrack->layer7.app_proto) {
470 + pattern_result = match_no_append(conntrack, master_conntrack, info);
472 + /* mark the packet seen (probably irrelevant, but consistant) */
475 + return (pattern_result ^ info->invert);
478 + /* Can end up here, but unallocated, if numpackets is increased during
479 + the beginning of a connection */
480 + if(master_conntrack->layer7.app_data == NULL)
481 + return (info->invert); /* unmatched */
485 + WRITE_LOCK(&ct_lock);
486 + newbytes = add_data(master_conntrack, app_data, appdatalen);
487 + WRITE_UNLOCK(&ct_lock);
489 + if(newbytes == 0) { /* didn't add any data */
491 + /* Didn't match before, not going to match now */
492 + return info->invert;
496 + /* If the regexp failed to compile, don't bother running it */
497 + if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
498 + DPRINTK("layer7: regexec positive: %s!\n", info->protocol);
499 + pattern_result = 1;
500 + } else pattern_result = 0;
502 + if(pattern_result) {
503 + WRITE_LOCK(&ct_lock);
504 + conntrack->layer7.app_proto = kmalloc(strlen(info->protocol), GFP_ATOMIC);
505 + if(!conntrack->layer7.app_proto){
506 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
507 + WRITE_UNLOCK(&ct_lock);
508 + return (pattern_result ^ info->invert);
511 + strcpy(conntrack->layer7.app_proto, info->protocol);
512 + WRITE_UNLOCK(&ct_lock);
515 + /* mark the packet seen */
518 + return (pattern_result ^ info->invert);
521 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
522 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
524 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
529 +static struct ipt_match layer7_match = {
532 + .checkentry = &checkentry,
536 +/* taken from drivers/video/modedb.c */
537 +static int my_atoi(const char *s)
544 + val = 10*val+(*s-'0');
552 +/* write out num_packets to userland. */
553 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
554 + int* eof, void * data)
556 + if(num_packets > 99)
557 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
559 + page[0] = num_packets/10 + '0';
560 + page[1] = num_packets%10 + '0';
569 +/* Read in num_packets from userland */
570 +static int layer7_write_proc(struct file* file, const char* buffer,
571 + unsigned long count, void *data)
573 + char * foo = kmalloc(count, GFP_ATOMIC);
576 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
580 + /* copy in the data from userland */
581 + copy_from_user(foo, buffer, count);
583 + num_packets = my_atoi(foo);
586 + /* This has an arbitrary limit to make the math easier. I'm lazy.
587 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
588 + if(num_packets > 99) {
589 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
591 + } else if(num_packets < 1) {
592 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
599 +/* register the proc file */
600 +static void layer7_init_proc(void)
602 + struct proc_dir_entry* entry;
604 + /* create the file */
605 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
607 + /* set the callback functions */
608 + entry->read_proc = layer7_read_proc;
609 + entry->write_proc = layer7_write_proc;
612 +static void layer7_cleanup_proc(void)
614 + remove_proc_entry("layer7_numpackets", proc_net);
617 +static int __init init(void)
619 + layer7_init_proc();
620 + return ipt_register_match(&layer7_match);
623 +static void __exit fini(void)
625 + layer7_cleanup_proc();
626 + ipt_unregister_match(&layer7_match);
631 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/Kconfig linux-2.6.7/net/ipv4/netfilter/Kconfig
632 --- linux-2.6.7.orig/net/ipv4/netfilter/Kconfig 2004-07-14 09:43:30.000000000 +0200
633 +++ linux-2.6.7/net/ipv4/netfilter/Kconfig 2004-07-14 09:48:56.042942568 +0200
635 depends on INET && NETFILTER
637 config IP_NF_CONNTRACK
638 - tristate "Connection tracking (required for masq/NAT)"
639 + tristate "Connection tracking (required for masq/NAT + layer7)"
641 Connection tracking keeps a record of what packets have passed
642 through your machine, in order to figure out how they are related
645 To compile it as a module, choose M here. If unsure, say N.
647 +config IP_NF_MATCH_LAYER7
648 + tristate "Layer 7 match support (EXPERIMENTAL)"
649 + depends on IP_NF_IPTABLES && IP_NF_CONNTRACK && EXPERIMENTAL
651 + Say Y if you want to be able to classify connections (and their
652 + packets) based on regular expression matching of their application
653 + layer data. This is one way to classify applications such as
654 + peer-to-peer filesharing systems that do not always use the same
657 + To compile it as a module, choose M here. If unsure, say N.
659 +config IP_NF_MATCH_LAYER7_DEBUG
660 + bool "Layer 7 debugging output"
661 + depends on IP_NF_MATCH_LAYER7
663 + Say Y to get lots of debugging output.
665 +config IP_NF_MATCH_LAYER7_MAXDATALEN
666 + int "Buffer size for application layer data" if IP_NF_MATCH_LAYER7
670 + Size of the buffer that the application layer data is stored in.
671 + Unless you know what you're doing, leave it at the default of 2kB.
674 config IP_NF_MATCH_PKTTYPE
675 tristate "Packet type match support"
676 depends on IP_NF_IPTABLES
677 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/Makefile linux-2.6.7/net/ipv4/netfilter/Makefile
678 --- linux-2.6.7.orig/net/ipv4/netfilter/Makefile 2004-07-14 09:43:30.000000000 +0200
679 +++ linux-2.6.7/net/ipv4/netfilter/Makefile 2004-07-14 09:48:56.050941352 +0200
681 obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
682 obj-$(CONFIG_IP_NF_MATCH_POLICY) += ipt_policy.o
684 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
687 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
688 obj-$(CONFIG_IP_NF_TARGET_TARPIT) += ipt_TARPIT.o
689 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/regexp/regexp.c linux-2.6.7/net/ipv4/netfilter/regexp/regexp.c
690 --- linux-2.6.7.orig/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
691 +++ linux-2.6.7/net/ipv4/netfilter/regexp/regexp.c 2004-07-14 09:48:56.065939072 +0200
694 + * regcomp and regexec -- regsub and regerror are elsewhere
695 + * @(#)regexp.c 1.3 of 18 April 87
697 + * Copyright (c) 1986 by University of Toronto.
698 + * Written by Henry Spencer. Not derived from licensed software.
700 + * Permission is granted to anyone to use this software for any
701 + * purpose on any computer system, and to redistribute it freely,
702 + * subject to the following restrictions:
704 + * 1. The author is not responsible for the consequences of use of
705 + * this software, no matter how awful, even if they arise
706 + * from defects in it.
708 + * 2. The origin of this software must not be misrepresented, either
709 + * by explicit claim or by omission.
711 + * 3. Altered versions must be plainly marked as such, and must not
712 + * be misrepresented as being the original software.
714 + * Beware that some of this code is subtly aware of the way operator
715 + * precedence is structured in regular expressions. Serious changes in
716 + * regular-expression syntax might require a total rethink.
718 + * This code was modified by Ethan Sommer to work within the kernel
719 + * (it now uses kmalloc etc..)
721 + * Modified slightly by Matthew Strait to use more modern C.
725 +#include "regmagic.h"
727 +/* added by ethan and matt. Lets it work in both kernel and user space.
728 +(So iptables can use it, for instance.) Yea, it goes both ways... */
730 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
732 + #define printk(format,args...) printf(format,##args)
735 +void regerror(char * s)
737 + printk("<3>Regexp: %s\n", s);
742 + * The "internal use only" fields in regexp.h are present to pass info from
743 + * compile to execute that permits the execute phase to run lots faster on
744 + * simple cases. They are:
746 + * regstart char that must begin a match; '\0' if none obvious
747 + * reganch is the match anchored (at beginning-of-line only)?
748 + * regmust string (pointer into program) that match must include, or NULL
749 + * regmlen length of regmust string
751 + * Regstart and reganch permit very fast decisions on suitable starting points
752 + * for a match, cutting down the work a lot. Regmust permits fast rejection
753 + * of lines that cannot possibly match. The regmust tests are costly enough
754 + * that regcomp() supplies a regmust only if the r.e. contains something
755 + * potentially expensive (at present, the only such thing detected is * or +
756 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
757 + * supplied because the test in regexec() needs it and regcomp() is computing
762 + * Structure for regexp "program". This is essentially a linear encoding
763 + * of a nondeterministic finite-state machine (aka syntax charts or
764 + * "railroad normal form" in parsing technology). Each node is an opcode
765 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
766 + * all nodes except BRANCH implement concatenation; a "next" pointer with
767 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
768 + * have one of the subtle syntax dependencies: an individual BRANCH (as
769 + * opposed to a collection of them) is never concatenated with anything
770 + * because of operator precedence.) The operand of some types of node is
771 + * a literal string; for others, it is a node leading into a sub-FSM. In
772 + * particular, the operand of a BRANCH node is the first node of the branch.
773 + * (NB this is *not* a tree structure: the tail of the branch connects
774 + * to the thing following the set of BRANCHes.) The opcodes are:
777 +/* definition number opnd? meaning */
778 +#define END 0 /* no End of program. */
779 +#define BOL 1 /* no Match "" at beginning of line. */
780 +#define EOL 2 /* no Match "" at end of line. */
781 +#define ANY 3 /* no Match any one character. */
782 +#define ANYOF 4 /* str Match any character in this string. */
783 +#define ANYBUT 5 /* str Match any character not in this string. */
784 +#define BRANCH 6 /* node Match this alternative, or the next... */
785 +#define BACK 7 /* no Match "", "next" ptr points backward. */
786 +#define EXACTLY 8 /* str Match this string. */
787 +#define NOTHING 9 /* no Match empty string. */
788 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
789 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
790 +#define OPEN 20 /* no Mark this point in input as start of #n. */
791 + /* OPEN+1 is number 1, etc. */
792 +#define CLOSE 30 /* no Analogous to OPEN. */
797 + * BRANCH The set of branches constituting a single choice are hooked
798 + * together with their "next" pointers, since precedence prevents
799 + * anything being concatenated to any individual branch. The
800 + * "next" pointer of the last BRANCH in a choice points to the
801 + * thing following the whole choice. This is also where the
802 + * final "next" pointer of each individual branch points; each
803 + * branch starts with the operand node of a BRANCH node.
805 + * BACK Normal "next" pointers all implicitly point forward; BACK
806 + * exists to make loop structures possible.
808 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
809 + * BRANCH structures using BACK. Simple cases (one character
810 + * per match) are implemented with STAR and PLUS for speed
811 + * and to minimize recursive plunges.
813 + * OPEN,CLOSE ...are numbered at compile time.
817 + * A node is one char of opcode followed by two chars of "next" pointer.
818 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
819 + * value is a positive offset from the opcode of the node containing it.
820 + * An operand, if any, simply follows the node. (Note that much of the
821 + * code generation knows about this implicit relationship.)
823 + * Using two bytes for the "next" pointer is vast overkill for most things,
824 + * but allows patterns to get big without disasters.
826 +#define OP(p) (*(p))
827 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
828 +#define OPERAND(p) ((p) + 3)
831 + * See regmagic.h for one further detail of program structure.
836 + * Utility definitions.
839 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
841 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
844 +#define FAIL(m) { regerror(m); return(NULL); }
845 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
846 +#define META "^$.[()|?+*\\"
849 + * Flags to be passed up and down.
851 +#define HASWIDTH 01 /* Known never to match null string. */
852 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
853 +#define SPSTART 04 /* Starts with * or +. */
854 +#define WORST 0 /* Worst case. */
857 + * Global work variables for regcomp().
859 +static char *regparse; /* Input-scan pointer. */
860 +static int regnpar; /* () count. */
861 +static char regdummy;
862 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
863 +static long regsize; /* Code size. */
866 + * Forward declarations for regcomp()'s friends.
869 +#define STATIC static
871 +STATIC char *reg(int paren,int *flagp);
872 +STATIC char *regbranch(int *flagp);
873 +STATIC char *regpiece(int *flagp);
874 +STATIC char *regatom(int *flagp);
875 +STATIC char *regnode(char op);
876 +STATIC char *regnext(char *p);
877 +STATIC void regc(char b);
878 +STATIC void reginsert(char op, char *opnd);
879 +STATIC void regtail(char *p, char *val);
880 +STATIC void regoptail(char *p, char *val);
883 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
890 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
891 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
892 + if (*scan1 == *scan2++)
900 + - regcomp - compile a regular expression into internal code
902 + * We can't allocate space until we know how big the compiled form will be,
903 + * but we can't compile it (and thus know how big it is) until we've got a
904 + * place to put the code. So we cheat: we compile it twice, once with code
905 + * generation turned off and size counting turned on, and once "for real".
906 + * This also means that we don't allocate space until we are sure that the
907 + * thing really will compile successfully, and we never have to move the
908 + * code and thus invalidate pointers into it. (Note that it has to be in
909 + * one piece because free() must be able to free it all.)
911 + * Beware that the optimization-preparation code in here knows about some
912 + * of the structure of the compiled regexp.
915 +regcomp(char *exp,int *patternsize)
917 + register regexp *r;
918 + register char *scan;
919 + register char *longest;
922 + /* commented out by ethan
923 + extern char *malloc();
927 + FAIL("NULL argument");
929 + /* First pass: determine size, legality. */
933 + regcode = ®dummy;
935 + if (reg(0, &flags) == NULL)
938 + /* Small enough for pointer-storage convention? */
939 + if (regsize >= 32767L) /* Probably could be 65535L. */
940 + FAIL("regexp too big");
942 + /* Allocate space. */
943 + *patternsize=sizeof(regexp) + (unsigned)regsize;
944 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
946 + FAIL("out of space");
948 + /* Second pass: emit code. */
951 + regcode = r->program;
953 + if (reg(0, &flags) == NULL)
956 + /* Dig out information for optimizations. */
957 + r->regstart = '\0'; /* Worst-case defaults. */
961 + scan = r->program+1; /* First BRANCH. */
962 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
963 + scan = OPERAND(scan);
965 + /* Starting-point info. */
966 + if (OP(scan) == EXACTLY)
967 + r->regstart = *OPERAND(scan);
968 + else if (OP(scan) == BOL)
972 + * If there's something expensive in the r.e., find the
973 + * longest literal string that must appear and make it the
974 + * regmust. Resolve ties in favor of later strings, since
975 + * the regstart check works with the beginning of the r.e.
976 + * and avoiding duplication strengthens checking. Not a
977 + * strong reason, but sufficient in the absence of others.
979 + if (flags&SPSTART) {
982 + for (; scan != NULL; scan = regnext(scan))
983 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
984 + longest = OPERAND(scan);
985 + len = strlen(OPERAND(scan));
987 + r->regmust = longest;
996 + - reg - regular expression, i.e. main body or parenthesized thing
998 + * Caller must absorb opening parenthesis.
1000 + * Combining parenthesis handling with the base level of regular expression
1001 + * is a trifle forced, but the need to tie the tails of the branches to what
1002 + * follows makes it hard to avoid.
1005 +reg(int paren, int *flagp /* Parenthesized? */ )
1007 + register char *ret;
1008 + register char *br;
1009 + register char *ender;
1010 + register int parno = 0; /* 0 makes gcc happy */
1013 + *flagp = HASWIDTH; /* Tentatively. */
1015 + /* Make an OPEN node, if parenthesized. */
1017 + if (regnpar >= NSUBEXP)
1018 + FAIL("too many ()");
1021 + ret = regnode(OPEN+parno);
1025 + /* Pick up the branches, linking them together. */
1026 + br = regbranch(&flags);
1030 + regtail(ret, br); /* OPEN -> first. */
1033 + if (!(flags&HASWIDTH))
1034 + *flagp &= ~HASWIDTH;
1035 + *flagp |= flags&SPSTART;
1036 + while (*regparse == '|') {
1038 + br = regbranch(&flags);
1041 + regtail(ret, br); /* BRANCH -> BRANCH. */
1042 + if (!(flags&HASWIDTH))
1043 + *flagp &= ~HASWIDTH;
1044 + *flagp |= flags&SPSTART;
1047 + /* Make a closing node, and hook it on the end. */
1048 + ender = regnode((paren) ? CLOSE+parno : END);
1049 + regtail(ret, ender);
1051 + /* Hook the tails of the branches to the closing node. */
1052 + for (br = ret; br != NULL; br = regnext(br))
1053 + regoptail(br, ender);
1055 + /* Check for proper termination. */
1056 + if (paren && *regparse++ != ')') {
1057 + FAIL("unmatched ()");
1058 + } else if (!paren && *regparse != '\0') {
1059 + if (*regparse == ')') {
1060 + FAIL("unmatched ()");
1062 + FAIL("junk on end"); /* "Can't happen". */
1070 + - regbranch - one alternative of an | operator
1072 + * Implements the concatenation operator.
1075 +regbranch(int *flagp)
1077 + register char *ret;
1078 + register char *chain;
1079 + register char *latest;
1082 + *flagp = WORST; /* Tentatively. */
1084 + ret = regnode(BRANCH);
1086 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1087 + latest = regpiece(&flags);
1088 + if (latest == NULL)
1090 + *flagp |= flags&HASWIDTH;
1091 + if (chain == NULL) /* First piece. */
1092 + *flagp |= flags&SPSTART;
1094 + regtail(chain, latest);
1097 + if (chain == NULL) /* Loop ran zero times. */
1098 + (void) regnode(NOTHING);
1104 + - regpiece - something followed by possible [*+?]
1106 + * Note that the branching code sequences used for ? and the general cases
1107 + * of * and + are somewhat optimized: they use the same NOTHING node as
1108 + * both the endmarker for their branch list and the body of the last branch.
1109 + * It might seem that this node could be dispensed with entirely, but the
1110 + * endmarker role is not redundant.
1113 +regpiece(int *flagp)
1115 + register char *ret;
1117 + register char *next;
1120 + ret = regatom(&flags);
1125 + if (!ISMULT(op)) {
1130 + if (!(flags&HASWIDTH) && op != '?')
1131 + FAIL("*+ operand could be empty");
1132 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1134 + if (op == '*' && (flags&SIMPLE))
1135 + reginsert(STAR, ret);
1136 + else if (op == '*') {
1137 + /* Emit x* as (x&|), where & means "self". */
1138 + reginsert(BRANCH, ret); /* Either x */
1139 + regoptail(ret, regnode(BACK)); /* and loop */
1140 + regoptail(ret, ret); /* back */
1141 + regtail(ret, regnode(BRANCH)); /* or */
1142 + regtail(ret, regnode(NOTHING)); /* null. */
1143 + } else if (op == '+' && (flags&SIMPLE))
1144 + reginsert(PLUS, ret);
1145 + else if (op == '+') {
1146 + /* Emit x+ as x(&|), where & means "self". */
1147 + next = regnode(BRANCH); /* Either */
1148 + regtail(ret, next);
1149 + regtail(regnode(BACK), ret); /* loop back */
1150 + regtail(next, regnode(BRANCH)); /* or */
1151 + regtail(ret, regnode(NOTHING)); /* null. */
1152 + } else if (op == '?') {
1153 + /* Emit x? as (x|) */
1154 + reginsert(BRANCH, ret); /* Either x */
1155 + regtail(ret, regnode(BRANCH)); /* or */
1156 + next = regnode(NOTHING); /* null. */
1157 + regtail(ret, next);
1158 + regoptail(ret, next);
1161 + if (ISMULT(*regparse))
1162 + FAIL("nested *?+");
1168 + - regatom - the lowest level
1170 + * Optimization: gobbles an entire sequence of ordinary characters so that
1171 + * it can turn them into a single node, which is smaller to store and
1172 + * faster to run. Backslashed characters are exceptions, each becoming a
1173 + * separate node; the code is simpler that way and it's not worth fixing.
1176 +regatom(int *flagp)
1178 + register char *ret;
1181 + *flagp = WORST; /* Tentatively. */
1183 + switch (*regparse++) {
1185 + ret = regnode(BOL);
1188 + ret = regnode(EOL);
1191 + ret = regnode(ANY);
1192 + *flagp |= HASWIDTH|SIMPLE;
1195 + register int class;
1196 + register int classend;
1198 + if (*regparse == '^') { /* Complement of range. */
1199 + ret = regnode(ANYBUT);
1202 + ret = regnode(ANYOF);
1203 + if (*regparse == ']' || *regparse == '-')
1204 + regc(*regparse++);
1205 + while (*regparse != '\0' && *regparse != ']') {
1206 + if (*regparse == '-') {
1208 + if (*regparse == ']' || *regparse == '\0')
1211 + class = UCHARAT(regparse-2)+1;
1212 + classend = UCHARAT(regparse);
1213 + if (class > classend+1)
1214 + FAIL("invalid [] range");
1215 + for (; class <= classend; class++)
1220 + regc(*regparse++);
1223 + if (*regparse != ']')
1224 + FAIL("unmatched []");
1226 + *flagp |= HASWIDTH|SIMPLE;
1230 + ret = reg(1, &flags);
1233 + *flagp |= flags&(HASWIDTH|SPSTART);
1238 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1243 + FAIL("?+* follows nothing");
1246 + if (*regparse == '\0')
1247 + FAIL("trailing \\");
1248 + ret = regnode(EXACTLY);
1249 + regc(*regparse++);
1251 + *flagp |= HASWIDTH|SIMPLE;
1255 + register char ender;
1258 + len = my_strcspn((const char *)regparse, (const char *)META);
1260 + FAIL("internal disaster");
1261 + ender = *(regparse+len);
1262 + if (len > 1 && ISMULT(ender))
1263 + len--; /* Back off clear of ?+* operand. */
1264 + *flagp |= HASWIDTH;
1267 + ret = regnode(EXACTLY);
1269 + regc(*regparse++);
1281 + - regnode - emit a node
1283 +static char * /* Location. */
1286 + register char *ret;
1287 + register char *ptr;
1290 + if (ret == ®dummy) {
1297 + *ptr++ = '\0'; /* Null "next" pointer. */
1305 + - regc - emit (if appropriate) a byte of code
1310 + if (regcode != ®dummy)
1317 + - reginsert - insert an operator in front of already-emitted operand
1319 + * Means relocating the operand.
1322 +reginsert(char op, char* opnd)
1324 + register char *src;
1325 + register char *dst;
1326 + register char *place;
1328 + if (regcode == ®dummy) {
1336 + while (src > opnd)
1339 + place = opnd; /* Op node, where operand used to be. */
1346 + - regtail - set the next-pointer at the end of a node chain
1349 +regtail(char *p, char *val)
1351 + register char *scan;
1352 + register char *temp;
1353 + register int offset;
1355 + if (p == ®dummy)
1358 + /* Find last node. */
1361 + temp = regnext(scan);
1367 + if (OP(scan) == BACK)
1368 + offset = scan - val;
1370 + offset = val - scan;
1371 + *(scan+1) = (offset>>8)&0377;
1372 + *(scan+2) = offset&0377;
1376 + - regoptail - regtail on operand of first argument; nop if operandless
1379 +regoptail(char *p, char *val)
1381 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1382 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1384 + regtail(OPERAND(p), val);
1388 + * regexec and friends
1392 + * Global work variables for regexec().
1394 +static char *reginput; /* String-input pointer. */
1395 +static char *regbol; /* Beginning of input, for ^ check. */
1396 +static char **regstartp; /* Pointer to startp array. */
1397 +static char **regendp; /* Ditto for endp. */
1402 +STATIC int regtry(regexp *prog, char *string);
1403 +STATIC int regmatch(char *prog);
1404 +STATIC int regrepeat(char *p);
1407 +int regnarrate = 0;
1409 +STATIC char *regprop(char *op);
1413 + - regexec - match a regexp against a string
1416 +regexec(regexp *prog, char *string)
1420 + /* Be paranoid... */
1421 + if (prog == NULL || string == NULL) {
1422 + printk("<3>Regexp: NULL parameter\n");
1426 + /* Check validity of program. */
1427 + if (UCHARAT(prog->program) != MAGIC) {
1428 + printk("<3>Regexp: corrupted program\n");
1432 + /* If there is a "must appear" string, look for it. */
1433 + if (prog->regmust != NULL) {
1435 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1436 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1437 + break; /* Found it. */
1440 + if (s == NULL) /* Not present. */
1444 + /* Mark beginning of line for ^ . */
1447 + /* Simplest case: anchored match need be tried only once. */
1448 + if (prog->reganch)
1449 + return(regtry(prog, string));
1451 + /* Messy cases: unanchored match. */
1453 + if (prog->regstart != '\0')
1454 + /* We know what char it must start with. */
1455 + while ((s = strchr(s, prog->regstart)) != NULL) {
1456 + if (regtry(prog, s))
1461 + /* We don't -- general case. */
1463 + if (regtry(prog, s))
1465 + } while (*s++ != '\0');
1472 + - regtry - try match at specific point
1474 +static int /* 0 failure, 1 success */
1475 +regtry(regexp *prog, char *string)
1478 + register char **sp;
1479 + register char **ep;
1481 + reginput = string;
1482 + regstartp = prog->startp;
1483 + regendp = prog->endp;
1485 + sp = prog->startp;
1487 + for (i = NSUBEXP; i > 0; i--) {
1491 + if (regmatch(prog->program + 1)) {
1492 + prog->startp[0] = string;
1493 + prog->endp[0] = reginput;
1500 + - regmatch - main matching routine
1502 + * Conceptually the strategy is simple: check to see whether the current
1503 + * node matches, call self recursively to see whether the rest matches,
1504 + * and then act accordingly. In practice we make some effort to avoid
1505 + * recursion, in particular by going through "ordinary" nodes (that don't
1506 + * need to know whether the rest of the match failed) by a loop instead of
1509 +static int /* 0 failure, 1 success */
1510 +regmatch(char *prog)
1512 + register char *scan = prog; /* Current node. */
1513 + char *next; /* Next node. */
1516 + if (scan != NULL && regnarrate)
1517 + fprintf(stderr, "%s(\n", regprop(scan));
1519 + while (scan != NULL) {
1522 + fprintf(stderr, "%s...\n", regprop(scan));
1524 + next = regnext(scan);
1526 + switch (OP(scan)) {
1528 + if (reginput != regbol)
1532 + if (*reginput != '\0')
1536 + if (*reginput == '\0')
1542 + register char *opnd;
1544 + opnd = OPERAND(scan);
1545 + /* Inline the first character, for speed. */
1546 + if (*opnd != *reginput)
1548 + len = strlen(opnd);
1549 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1555 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1560 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1577 + register char *save;
1579 + no = OP(scan) - OPEN;
1582 + if (regmatch(next)) {
1584 + * Don't set startp if some later
1585 + * invocation of the same parentheses
1588 + if (regstartp[no] == NULL)
1589 + regstartp[no] = save;
1606 + register char *save;
1608 + no = OP(scan) - CLOSE;
1611 + if (regmatch(next)) {
1613 + * Don't set endp if some later
1614 + * invocation of the same parentheses
1617 + if (regendp[no] == NULL)
1618 + regendp[no] = save;
1625 + register char *save;
1627 + if (OP(next) != BRANCH) /* No choice. */
1628 + next = OPERAND(scan); /* Avoid recursion. */
1632 + if (regmatch(OPERAND(scan)))
1635 + scan = regnext(scan);
1636 + } while (scan != NULL && OP(scan) == BRANCH);
1644 + register char nextch;
1646 + register char *save;
1650 + * Lookahead to avoid useless match attempts
1651 + * when we know what character comes next.
1654 + if (OP(next) == EXACTLY)
1655 + nextch = *OPERAND(next);
1656 + min = (OP(scan) == STAR) ? 0 : 1;
1658 + no = regrepeat(OPERAND(scan));
1659 + while (no >= min) {
1660 + /* If it could work, try it. */
1661 + if (nextch == '\0' || *reginput == nextch)
1662 + if (regmatch(next))
1664 + /* Couldn't or didn't -- back up. */
1666 + reginput = save + no;
1672 + return(1); /* Success! */
1675 + printk("<3>Regexp: memory corruption\n");
1684 + * We get here only if there's trouble -- normally "case END" is
1685 + * the terminating point.
1687 + printk("<3>Regexp: corrupted pointers\n");
1692 + - regrepeat - repeatedly match something simple, report how many
1697 + register int count = 0;
1698 + register char *scan;
1699 + register char *opnd;
1702 + opnd = OPERAND(p);
1705 + count = strlen(scan);
1709 + while (*opnd == *scan) {
1715 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1721 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1726 + default: /* Oh dear. Called inappropriately. */
1727 + printk("<3>Regexp: internal foulup\n");
1728 + count = 0; /* Best compromise. */
1737 + - regnext - dig the "next" pointer out of a node
1742 + register int offset;
1744 + if (p == ®dummy)
1751 + if (OP(p) == BACK)
1759 +STATIC char *regprop();
1762 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1768 + register char op = EXACTLY; /* Arbitrary non-END op. */
1769 + register char *next;
1770 + /* extern char *strchr(); */
1773 + s = r->program + 1;
1774 + while (op != END) { /* While that wasn't END last time... */
1776 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1777 + next = regnext(s);
1778 + if (next == NULL) /* Next ptr. */
1781 + printf("(%d)", (s-r->program)+(next-s));
1783 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1784 + /* Literal string, where present. */
1785 + while (*s != '\0') {
1794 + /* Header fields of interest. */
1795 + if (r->regstart != '\0')
1796 + printf("start `%c' ", r->regstart);
1798 + printf("anchored ");
1799 + if (r->regmust != NULL)
1800 + printf("must have \"%s\"", r->regmust);
1805 + - regprop - printable representation of opcode
1812 + static char buf[BUFLEN];
1856 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1868 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1878 + printk("<3>Regexp: corrupted opcode\n");
1882 + strncat(buf, p, BUFLEN-strlen(buf));
1888 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/regexp/regexp.h linux-2.6.7/net/ipv4/netfilter/regexp/regexp.h
1889 --- linux-2.6.7.orig/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1890 +++ linux-2.6.7/net/ipv4/netfilter/regexp/regexp.h 2004-07-14 09:48:56.066938920 +0200
1893 + * Definitions etc. for regexp(3) routines.
1895 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1896 + * not the System V one.
1903 +typedef struct regexp {
1904 + char *startp[NSUBEXP];
1905 + char *endp[NSUBEXP];
1906 + char regstart; /* Internal use only. */
1907 + char reganch; /* Internal use only. */
1908 + char *regmust; /* Internal use only. */
1909 + int regmlen; /* Internal use only. */
1910 + char program[1]; /* Unwarranted chumminess with compiler. */
1913 +regexp * regcomp(char *exp, int *patternsize);
1914 +int regexec(regexp *prog, char *string);
1915 +void regsub(regexp *prog, char *source, char *dest);
1916 +void regerror(char *s);
1919 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/regexp/regmagic.h linux-2.6.7/net/ipv4/netfilter/regexp/regmagic.h
1920 --- linux-2.6.7.orig/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1921 +++ linux-2.6.7/net/ipv4/netfilter/regexp/regmagic.h 2004-07-14 09:48:56.066938920 +0200
1924 + * The first byte of the regexp internal "program" is actually this magic
1925 + * number; the start node begins in the second byte.
1928 diff -uNr linux-2.6.7.orig/net/ipv4/netfilter/regexp/regsub.c linux-2.6.7/net/ipv4/netfilter/regexp/regsub.c
1929 --- linux-2.6.7.orig/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1930 +++ linux-2.6.7/net/ipv4/netfilter/regexp/regsub.c 2004-07-14 09:48:56.067938768 +0200
1934 + * @(#)regsub.c 1.3 of 2 April 86
1936 + * Copyright (c) 1986 by University of Toronto.
1937 + * Written by Henry Spencer. Not derived from licensed software.
1939 + * Permission is granted to anyone to use this software for any
1940 + * purpose on any computer system, and to redistribute it freely,
1941 + * subject to the following restrictions:
1943 + * 1. The author is not responsible for the consequences of use of
1944 + * this software, no matter how awful, even if they arise
1945 + * from defects in it.
1947 + * 2. The origin of this software must not be misrepresented, either
1948 + * by explicit claim or by omission.
1950 + * 3. Altered versions must be plainly marked as such, and must not
1951 + * be misrepresented as being the original software.
1954 + * This code was modified by Ethan Sommer to work within the kernel
1955 + * (it now uses kmalloc etc..)
1958 +#include "regexp.h"
1959 +#include "regmagic.h"
1960 +#include <linux/string.h>
1964 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1966 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1970 +//void regerror(char * s)
1972 +// printk("regexp(3): %s", s);
1973 +// /* NOTREACHED */
1978 + - regsub - perform substitutions after a regexp match
1981 +regsub(regexp * prog, char * source, char * dest)
1983 + register char *src;
1984 + register char *dst;
1989 + /* Not necessary and gcc doesn't like it -MLS */
1990 + /*extern char *strncpy();*/
1992 + if (prog == NULL || source == NULL || dest == NULL) {
1993 + regerror("NULL parm to regsub");
1996 + if (UCHARAT(prog->program) != MAGIC) {
1997 + regerror("damaged regexp fed to regsub");
2003 + while ((c = *src++) != '\0') {
2006 + else if (c == '\\' && '0' <= *src && *src <= '9')
2007 + no = *src++ - '0';
2011 + if (no < 0) { /* Ordinary character. */
2012 + if (c == '\\' && (*src == '\\' || *src == '&'))
2015 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2016 + len = prog->endp[no] - prog->startp[no];
2017 + (void) strncpy(dst, prog->startp[no], len);
2019 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2020 + regerror("damaged match string");