1 diff -urN linux-2.6.21.1.old/include/linux/netfilter_ipv4/ipt_layer7.h linux-2.6.21.1.dev/include/linux/netfilter_ipv4/ipt_layer7.h
2 --- linux-2.6.21.1.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2007-05-26 20:13:52.648130120 +0200
6 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
7 + http://l7-filter.sf.net
9 + This program is free software; you can redistribute it and/or
10 + modify it under the terms of the GNU General Public License
11 + as published by the Free Software Foundation; either version
12 + 2 of the License, or (at your option) any later version.
13 + http://www.gnu.org/licenses/gpl.txt
16 +#ifndef _IPT_LAYER7_H
17 +#define _IPT_LAYER7_H
19 +#define MAX_PATTERN_LEN 8192
20 +#define MAX_PROTOCOL_LEN 256
22 +typedef char *(*proc_ipt_search) (char *, char, char *);
24 +struct ipt_layer7_info {
25 + char protocol[MAX_PROTOCOL_LEN];
27 + char pattern[MAX_PATTERN_LEN];
30 +#endif /* _IPT_LAYER7_H */
31 diff -urN linux-2.6.21.1.old/net/netfilter/nf_conntrack_core.c linux-2.6.21.1.dev/net/netfilter/nf_conntrack_core.c
32 --- linux-2.6.21.1.old/net/netfilter/nf_conntrack_core.c 2007-04-27 23:49:26.000000000 +0200
33 +++ linux-2.6.21.1.dev/net/netfilter/nf_conntrack_core.c 2007-05-26 20:13:52.649129968 +0200
36 nf_ct_remove_expectations(ct);
38 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
39 + if(ct->layer7.app_proto)
40 + kfree(ct->layer7.app_proto);
41 + if(ct->layer7.app_data)
42 + kfree(ct->layer7.app_data);
45 /* We overload first tuple to link into unconfirmed list. */
46 if (!nf_ct_is_confirmed(ct)) {
47 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
48 diff -urN linux-2.6.21.1.old/net/netfilter/nf_conntrack_standalone.c linux-2.6.21.1.dev/net/netfilter/nf_conntrack_standalone.c
49 --- linux-2.6.21.1.old/net/netfilter/nf_conntrack_standalone.c 2007-04-27 23:49:26.000000000 +0200
50 +++ linux-2.6.21.1.dev/net/netfilter/nf_conntrack_standalone.c 2007-05-26 20:13:52.649129968 +0200
55 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
56 + if(conntrack->layer7.app_proto)
57 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
61 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
64 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/ipt_layer7.c linux-2.6.21.1.dev/net/ipv4/netfilter/ipt_layer7.c
65 --- linux-2.6.21.1.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
66 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/ipt_layer7.c 2007-05-26 20:13:52.650129816 +0200
69 + Kernel module to match application layer (OSI layer 7) data in connections.
71 + http://l7-filter.sf.net
73 + By Matthew Strait and Ethan Sommer, 2003-2006.
75 + This program is free software; you can redistribute it and/or
76 + modify it under the terms of the GNU General Public License
77 + as published by the Free Software Foundation; either version
78 + 2 of the License, or (at your option) any later version.
79 + http://www.gnu.org/licenses/gpl.txt
81 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
82 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
85 +#include <linux/module.h>
86 +#include <linux/skbuff.h>
87 +#include <linux/netfilter_ipv4/ip_conntrack.h>
88 +#include <linux/proc_fs.h>
89 +#include <linux/ctype.h>
92 +#include <linux/spinlock.h>
94 +#include "regexp/regexp.c"
96 +#include <linux/netfilter_ipv4/ipt_layer7.h>
97 +#include <linux/netfilter_ipv4/ip_tables.h>
99 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
100 +MODULE_LICENSE("GPL");
101 +MODULE_DESCRIPTION("iptables application layer match module");
102 +MODULE_VERSION("2.0");
104 +static int maxdatalen = 2048; // this is the default
105 +module_param(maxdatalen, int, 0444);
106 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
108 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
109 + #define DPRINTK(format,args...) printk(format,##args)
111 + #define DPRINTK(format,args...)
114 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
115 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
117 +/* Number of packets whose data we look at.
118 +This can be modified through /proc/net/layer7_numpackets */
119 +static int num_packets = 10;
121 +static struct pattern_cache {
122 + char * regex_string;
124 + struct pattern_cache * next;
125 +} * first_pattern_cache = NULL;
127 +/* I'm new to locking. Here are my assumptions:
129 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
130 + if they did, nothing awful would happen.
132 +- This code will never be processing the same packet twice at the same time,
133 + because iptables rules are traversed in order.
135 +- It doesn't matter if two packets from different connections are in here at
136 + the same time, because they don't share any data.
138 +- It _does_ matter if two packets from the same connection (or one from a
139 + master and one from its child) are here at the same time. In this case,
140 + we have to protect the conntracks and the list of compiled patterns.
142 +DEFINE_RWLOCK(ct_lock);
143 +DEFINE_SPINLOCK(list_lock);
145 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
146 +/* Converts an unfriendly string into a friendly one by
147 +replacing unprintables with periods and all whitespace with " ". */
148 +static char * friendly_print(unsigned char * s)
150 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
154 + if (net_ratelimit())
155 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
159 + for(i = 0; i < strlen(s); i++){
160 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
161 + else if(isspace(s[i])) f[i] = ' ';
168 +static char dec2hex(int i)
172 + return (char)(i + '0');
175 + return (char)(i - 10 + 'a');
178 + if (net_ratelimit())
179 + printk("Problem in dec2hex\n");
184 +static char * hex_print(unsigned char * s)
186 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
190 + if (net_ratelimit())
191 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
195 + for(i = 0; i < strlen(s); i++) {
196 + g[i*3 ] = dec2hex(s[i]/16);
197 + g[i*3 + 1] = dec2hex(s[i]%16);
206 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
207 +over again, it make sense to cache the results. */
208 +static regexp * compile_and_cache(char * regex_string, char * protocol)
210 + struct pattern_cache * node = first_pattern_cache;
211 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
212 + struct pattern_cache * tmp;
215 + while (node != NULL) {
216 + if (!strcmp(node->regex_string, regex_string))
217 + return node->pattern;
219 + last_pattern_cache = node;/* points at the last non-NULL node */
223 + /* If we reach the end of the list, then we have not yet cached
224 + the pattern for this regex. Let's do that now.
225 + Be paranoid about running out of memory to avoid list corruption. */
226 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
229 + if (net_ratelimit())
230 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
234 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
235 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
238 + if(!tmp->regex_string || !tmp->pattern) {
239 + if (net_ratelimit())
240 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
241 + kfree(tmp->regex_string);
242 + kfree(tmp->pattern);
247 + /* Ok. The new node is all ready now. */
250 + if(first_pattern_cache == NULL) /* list is empty */
251 + first_pattern_cache = node; /* make node the beginning */
253 + last_pattern_cache->next = node; /* attach node to the end */
255 + /* copy the string and compile the regex */
256 + len = strlen(regex_string);
257 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
258 + node->pattern = regcomp(regex_string, &len);
259 + if ( !node->pattern ) {
260 + if (net_ratelimit())
261 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
262 + /* pattern is now cached as NULL, so we won't try again. */
265 + strcpy(node->regex_string, regex_string);
266 + return node->pattern;
269 +static int can_handle(const struct sk_buff *skb)
271 + if(!ip_hdr(skb)) /* not IP */
273 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
274 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
275 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
280 +/* Returns offset the into the skb->data that the application data starts */
281 +static int app_data_offset(const struct sk_buff *skb)
283 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
284 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
285 + int ip_hl = ip_hdrlen(skb);
287 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
288 + /* 12 == offset into TCP header for the header length field.
289 + Can't get this with skb->h.th->doff because the tcphdr
290 + struct doesn't get set when routing (this is confirmed to be
291 + true in Netfilter as well as QoS.) */
292 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
294 + return ip_hl + tcp_hl;
295 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
296 + return ip_hl + 8; /* UDP header is always 8 bytes */
297 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
298 + return ip_hl + 8; /* ICMP header is 8 bytes */
300 + if (net_ratelimit())
301 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
302 + return ip_hl + 8; /* something reasonable */
306 +/* handles whether there's a match when we aren't appending data anymore */
307 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
308 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
309 + struct ipt_layer7_info * info)
311 + /* If we're in here, throw the app data away */
312 + write_lock(&ct_lock);
313 + if(master_conntrack->layer7.app_data != NULL) {
315 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
316 + if(!master_conntrack->layer7.app_proto) {
317 + char * f = friendly_print(master_conntrack->layer7.app_data);
318 + char * g = hex_print(master_conntrack->layer7.app_data);
319 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
320 + strlen(f), TOTAL_PACKETS, f);
322 + DPRINTK("In hex: %s\n", g);
327 + kfree(master_conntrack->layer7.app_data);
328 + master_conntrack->layer7.app_data = NULL; /* don't free again */
330 + write_unlock(&ct_lock);
332 + if(master_conntrack->layer7.app_proto){
333 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
334 + write_lock(&ct_lock);
335 + if(!conntrack->layer7.app_proto) {
336 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
337 + if(!conntrack->layer7.app_proto){
338 + if (net_ratelimit())
339 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
340 + write_unlock(&ct_lock);
343 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
345 + write_unlock(&ct_lock);
347 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
350 + /* If not classified, set to "unknown" to distinguish from
351 + connections that are still being tested. */
352 + write_lock(&ct_lock);
353 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
354 + if(!master_conntrack->layer7.app_proto){
355 + if (net_ratelimit())
356 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
357 + write_unlock(&ct_lock);
360 + strcpy(master_conntrack->layer7.app_proto, "unknown");
361 + write_unlock(&ct_lock);
366 +/* add the new app data to the conntrack. Return number of bytes added. */
367 +static int add_data(struct ip_conntrack * master_conntrack,
368 + char * app_data, int appdatalen)
371 + int oldlength = master_conntrack->layer7.app_data_len;
373 + // This is a fix for a race condition by Deti Fliegl. However, I'm not
374 + // clear on whether the race condition exists or whether this really
375 + // fixes it. I might just be being dense... Anyway, if it's not really
376 + // a fix, all it does is waste a very small amount of time.
377 + if(!master_conntrack->layer7.app_data) return 0;
379 + /* Strip nulls. Make everything lower case (our regex lib doesn't
380 + do case insensitivity). Add it to the end of the current data. */
381 + for(i = 0; i < maxdatalen-oldlength-1 &&
382 + i < appdatalen; i++) {
383 + if(app_data[i] != '\0') {
384 + master_conntrack->layer7.app_data[length+oldlength] =
385 + /* the kernel version of tolower mungs 'upper ascii' */
386 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
391 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
392 + master_conntrack->layer7.app_data_len = length + oldlength;
397 +/* Returns true on match and false otherwise. */
398 +static int match(const struct sk_buff *skbin,
399 + const struct net_device *in, const struct net_device *out,
400 + const struct xt_match *match, const void *matchinfo,
401 + int offset, unsigned int protoff, int *hotdrop)
403 + /* sidestep const without getting a compiler warning... */
404 + struct sk_buff * skb = (struct sk_buff *)skbin;
406 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
407 + enum ip_conntrack_info master_ctinfo, ctinfo;
408 + struct ip_conntrack *master_conntrack, *conntrack;
409 + unsigned char * app_data;
410 + unsigned int pattern_result, appdatalen;
411 + regexp * comppattern;
413 + if(!can_handle(skb)){
414 + DPRINTK("layer7: This is some protocol I can't handle.\n");
415 + return info->invert;
418 + /* Treat parent & all its children together as one connection, except
419 + for the purpose of setting conntrack->layer7.app_proto in the actual
420 + connection. This makes /proc/net/ip_conntrack more satisfying. */
421 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
422 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
423 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
424 + return info->invert;
427 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
428 + while (master_ct(master_conntrack) != NULL)
429 + master_conntrack = master_ct(master_conntrack);
431 + /* if we've classified it or seen too many packets */
432 + if(TOTAL_PACKETS > num_packets ||
433 + master_conntrack->layer7.app_proto) {
435 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
437 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
438 + rules. I'm not sure that using cb for this purpose is correct, although
439 + it says "put your private variables there". But it doesn't look like it
440 + is being used for anything else in the skbs that make it here. How can
441 + I write to cb without making the compiler angry? */
442 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
444 + return (pattern_result ^ info->invert);
447 + if(skb_is_nonlinear(skb)){
448 + if(skb_linearize(skb) != 0){
449 + if (net_ratelimit())
450 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
451 + return info->invert;
455 + /* now that the skb is linearized, it's safe to set these. */
456 + app_data = skb->data + app_data_offset(skb);
457 + appdatalen = skb->tail - app_data;
459 + spin_lock_bh(&list_lock);
460 + /* the return value gets checked later, when we're ready to use it */
461 + comppattern = compile_and_cache(info->pattern, info->protocol);
462 + spin_unlock_bh(&list_lock);
464 + /* On the first packet of a connection, allocate space for app data */
465 + write_lock(&ct_lock);
466 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
467 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
468 + if(!master_conntrack->layer7.app_data){
469 + if (net_ratelimit())
470 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
471 + write_unlock(&ct_lock);
472 + return info->invert;
475 + master_conntrack->layer7.app_data[0] = '\0';
477 + write_unlock(&ct_lock);
479 + /* Can be here, but unallocated, if numpackets is increased near
480 + the beginning of a connection */
481 + if(master_conntrack->layer7.app_data == NULL)
482 + return (info->invert); /* unmatched */
486 + write_lock(&ct_lock);
487 + newbytes = add_data(master_conntrack, app_data, appdatalen);
488 + write_unlock(&ct_lock);
490 + if(newbytes == 0) { /* didn't add any data */
492 + /* Didn't match before, not going to match now */
493 + return info->invert;
497 + /* If looking for "unknown", then never match. "Unknown" means that
498 + we've given up; we're still trying with these packets. */
499 + read_lock(&ct_lock);
500 + if(!strcmp(info->protocol, "unknown")) {
501 + pattern_result = 0;
502 + /* If the regexp failed to compile, don't bother running it */
503 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
504 + DPRINTK("layer7: matched %s\n", info->protocol);
505 + pattern_result = 1;
506 + } else pattern_result = 0;
507 + read_unlock(&ct_lock);
509 + if(pattern_result) {
510 + write_lock(&ct_lock);
511 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
512 + if(!master_conntrack->layer7.app_proto){
513 + if (net_ratelimit())
514 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
515 + write_unlock(&ct_lock);
516 + return (pattern_result ^ info->invert);
518 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
519 + write_unlock(&ct_lock);
522 + /* mark the packet seen */
525 + return (pattern_result ^ info->invert);
528 +static struct ipt_match layer7_match = {
531 + .matchsize = sizeof(struct ipt_layer7_info),
535 +/* taken from drivers/video/modedb.c */
536 +static int my_atoi(const char *s)
543 + val = 10*val+(*s-'0');
551 +/* write out num_packets to userland. */
552 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
553 + int* eof, void * data)
555 + if(num_packets > 99 && net_ratelimit())
556 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
558 + page[0] = num_packets/10 + '0';
559 + page[1] = num_packets%10 + '0';
568 +/* Read in num_packets from userland */
569 +static int layer7_write_proc(struct file* file, const char* buffer,
570 + unsigned long count, void *data)
572 + char * foo = kmalloc(count, GFP_ATOMIC);
575 + if (net_ratelimit())
576 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
580 + if(copy_from_user(foo, buffer, count)) {
585 + num_packets = my_atoi(foo);
588 + /* This has an arbitrary limit to make the math easier. I'm lazy.
589 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
590 + if(num_packets > 99) {
591 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
593 + } else if(num_packets < 1) {
594 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
601 +/* register the proc file */
602 +static void layer7_init_proc(void)
604 + struct proc_dir_entry* entry;
605 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
606 + entry->read_proc = layer7_read_proc;
607 + entry->write_proc = layer7_write_proc;
610 +static void layer7_cleanup_proc(void)
612 + remove_proc_entry("layer7_numpackets", proc_net);
615 +static int __init ipt_layer7_init(void)
619 + layer7_init_proc();
620 + if(maxdatalen < 1) {
621 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
624 + /* This is not a hard limit. It's just here to prevent people from
625 + bringing their slow machines to a grinding halt. */
626 + else if(maxdatalen > 65536) {
627 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
628 + maxdatalen = 65536;
630 + return ipt_register_match(&layer7_match);
633 +static void __exit ipt_layer7_fini(void)
635 + layer7_cleanup_proc();
636 + ipt_unregister_match(&layer7_match);
639 +module_init(ipt_layer7_init);
640 +module_exit(ipt_layer7_fini);
641 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/Kconfig linux-2.6.21.1.dev/net/ipv4/netfilter/Kconfig
642 --- linux-2.6.21.1.old/net/ipv4/netfilter/Kconfig 2007-04-27 23:49:26.000000000 +0200
643 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/Kconfig 2007-05-26 20:13:52.650129816 +0200
646 To compile it as a module, choose M here. If unsure, say N.
648 +config IP_NF_MATCH_LAYER7
649 + tristate "Layer 7 match support (EXPERIMENTAL)"
650 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
652 + Say Y if you want to be able to classify connections (and their
653 + packets) based on regular expression matching of their application
654 + layer data. This is one way to classify applications such as
655 + peer-to-peer filesharing systems that do not always use the same
658 + To compile it as a module, choose M here. If unsure, say N.
660 +config IP_NF_MATCH_LAYER7_DEBUG
661 + bool "Layer 7 debugging output"
662 + depends on IP_NF_MATCH_LAYER7
664 + Say Y to get lots of debugging output.
666 config IP_NF_MATCH_TOS
667 tristate "TOS match support"
668 depends on IP_NF_IPTABLES
669 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/Makefile linux-2.6.21.1.dev/net/ipv4/netfilter/Makefile
670 --- linux-2.6.21.1.old/net/ipv4/netfilter/Makefile 2007-04-27 23:49:26.000000000 +0200
671 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/Makefile 2007-05-26 20:13:52.651129664 +0200
673 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
674 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
676 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
679 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
680 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
681 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regexp.c linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regexp.c
682 --- linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
683 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regexp.c 2007-05-26 20:13:52.652129512 +0200
686 + * regcomp and regexec -- regsub and regerror are elsewhere
687 + * @(#)regexp.c 1.3 of 18 April 87
689 + * Copyright (c) 1986 by University of Toronto.
690 + * Written by Henry Spencer. Not derived from licensed software.
692 + * Permission is granted to anyone to use this software for any
693 + * purpose on any computer system, and to redistribute it freely,
694 + * subject to the following restrictions:
696 + * 1. The author is not responsible for the consequences of use of
697 + * this software, no matter how awful, even if they arise
698 + * from defects in it.
700 + * 2. The origin of this software must not be misrepresented, either
701 + * by explicit claim or by omission.
703 + * 3. Altered versions must be plainly marked as such, and must not
704 + * be misrepresented as being the original software.
706 + * Beware that some of this code is subtly aware of the way operator
707 + * precedence is structured in regular expressions. Serious changes in
708 + * regular-expression syntax might require a total rethink.
710 + * This code was modified by Ethan Sommer to work within the kernel
711 + * (it now uses kmalloc etc..)
713 + * Modified slightly by Matthew Strait to use more modern C.
717 +#include "regmagic.h"
719 +/* added by ethan and matt. Lets it work in both kernel and user space.
720 +(So iptables can use it, for instance.) Yea, it goes both ways... */
722 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
724 + #define printk(format,args...) printf(format,##args)
727 +void regerror(char * s)
729 + printk("<3>Regexp: %s\n", s);
734 + * The "internal use only" fields in regexp.h are present to pass info from
735 + * compile to execute that permits the execute phase to run lots faster on
736 + * simple cases. They are:
738 + * regstart char that must begin a match; '\0' if none obvious
739 + * reganch is the match anchored (at beginning-of-line only)?
740 + * regmust string (pointer into program) that match must include, or NULL
741 + * regmlen length of regmust string
743 + * Regstart and reganch permit very fast decisions on suitable starting points
744 + * for a match, cutting down the work a lot. Regmust permits fast rejection
745 + * of lines that cannot possibly match. The regmust tests are costly enough
746 + * that regcomp() supplies a regmust only if the r.e. contains something
747 + * potentially expensive (at present, the only such thing detected is * or +
748 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
749 + * supplied because the test in regexec() needs it and regcomp() is computing
754 + * Structure for regexp "program". This is essentially a linear encoding
755 + * of a nondeterministic finite-state machine (aka syntax charts or
756 + * "railroad normal form" in parsing technology). Each node is an opcode
757 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
758 + * all nodes except BRANCH implement concatenation; a "next" pointer with
759 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
760 + * have one of the subtle syntax dependencies: an individual BRANCH (as
761 + * opposed to a collection of them) is never concatenated with anything
762 + * because of operator precedence.) The operand of some types of node is
763 + * a literal string; for others, it is a node leading into a sub-FSM. In
764 + * particular, the operand of a BRANCH node is the first node of the branch.
765 + * (NB this is *not* a tree structure: the tail of the branch connects
766 + * to the thing following the set of BRANCHes.) The opcodes are:
769 +/* definition number opnd? meaning */
770 +#define END 0 /* no End of program. */
771 +#define BOL 1 /* no Match "" at beginning of line. */
772 +#define EOL 2 /* no Match "" at end of line. */
773 +#define ANY 3 /* no Match any one character. */
774 +#define ANYOF 4 /* str Match any character in this string. */
775 +#define ANYBUT 5 /* str Match any character not in this string. */
776 +#define BRANCH 6 /* node Match this alternative, or the next... */
777 +#define BACK 7 /* no Match "", "next" ptr points backward. */
778 +#define EXACTLY 8 /* str Match this string. */
779 +#define NOTHING 9 /* no Match empty string. */
780 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
781 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
782 +#define OPEN 20 /* no Mark this point in input as start of #n. */
783 + /* OPEN+1 is number 1, etc. */
784 +#define CLOSE 30 /* no Analogous to OPEN. */
789 + * BRANCH The set of branches constituting a single choice are hooked
790 + * together with their "next" pointers, since precedence prevents
791 + * anything being concatenated to any individual branch. The
792 + * "next" pointer of the last BRANCH in a choice points to the
793 + * thing following the whole choice. This is also where the
794 + * final "next" pointer of each individual branch points; each
795 + * branch starts with the operand node of a BRANCH node.
797 + * BACK Normal "next" pointers all implicitly point forward; BACK
798 + * exists to make loop structures possible.
800 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
801 + * BRANCH structures using BACK. Simple cases (one character
802 + * per match) are implemented with STAR and PLUS for speed
803 + * and to minimize recursive plunges.
805 + * OPEN,CLOSE ...are numbered at compile time.
809 + * A node is one char of opcode followed by two chars of "next" pointer.
810 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
811 + * value is a positive offset from the opcode of the node containing it.
812 + * An operand, if any, simply follows the node. (Note that much of the
813 + * code generation knows about this implicit relationship.)
815 + * Using two bytes for the "next" pointer is vast overkill for most things,
816 + * but allows patterns to get big without disasters.
818 +#define OP(p) (*(p))
819 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
820 +#define OPERAND(p) ((p) + 3)
823 + * See regmagic.h for one further detail of program structure.
828 + * Utility definitions.
831 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
833 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
836 +#define FAIL(m) { regerror(m); return(NULL); }
837 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
838 +#define META "^$.[()|?+*\\"
841 + * Flags to be passed up and down.
843 +#define HASWIDTH 01 /* Known never to match null string. */
844 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
845 +#define SPSTART 04 /* Starts with * or +. */
846 +#define WORST 0 /* Worst case. */
849 + * Global work variables for regcomp().
851 +struct match_globals {
852 +char *reginput; /* String-input pointer. */
853 +char *regbol; /* Beginning of input, for ^ check. */
854 +char **regstartp; /* Pointer to startp array. */
855 +char **regendp; /* Ditto for endp. */
856 +char *regparse; /* Input-scan pointer. */
857 +int regnpar; /* () count. */
859 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
860 +long regsize; /* Code size. */
864 + * Forward declarations for regcomp()'s friends.
867 +#define STATIC static
869 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
870 +STATIC char *regbranch(struct match_globals *g, int *flagp);
871 +STATIC char *regpiece(struct match_globals *g, int *flagp);
872 +STATIC char *regatom(struct match_globals *g, int *flagp);
873 +STATIC char *regnode(struct match_globals *g, char op);
874 +STATIC char *regnext(struct match_globals *g, char *p);
875 +STATIC void regc(struct match_globals *g, char b);
876 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
877 +STATIC void regtail(struct match_globals *g, char *p, char *val);
878 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
881 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
888 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
889 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
890 + if (*scan1 == *scan2++)
898 + - regcomp - compile a regular expression into internal code
900 + * We can't allocate space until we know how big the compiled form will be,
901 + * but we can't compile it (and thus know how big it is) until we've got a
902 + * place to put the code. So we cheat: we compile it twice, once with code
903 + * generation turned off and size counting turned on, and once "for real".
904 + * This also means that we don't allocate space until we are sure that the
905 + * thing really will compile successfully, and we never have to move the
906 + * code and thus invalidate pointers into it. (Note that it has to be in
907 + * one piece because free() must be able to free it all.)
909 + * Beware that the optimization-preparation code in here knows about some
910 + * of the structure of the compiled regexp.
913 +regcomp(char *exp,int *patternsize)
915 + register regexp *r;
916 + register char *scan;
917 + register char *longest;
920 + struct match_globals g;
922 + /* commented out by ethan
923 + extern char *malloc();
927 + FAIL("NULL argument");
929 + /* First pass: determine size, legality. */
933 + g.regcode = &g.regdummy;
935 + if (reg(&g, 0, &flags) == NULL)
938 + /* Small enough for pointer-storage convention? */
939 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
940 + FAIL("regexp too big");
942 + /* Allocate space. */
943 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
944 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
946 + FAIL("out of space");
948 + /* Second pass: emit code. */
951 + g.regcode = r->program;
953 + if (reg(&g, 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(&g, 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(&g, 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(struct match_globals *g, 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 (g->regnpar >= NSUBEXP)
1018 + FAIL("too many ()");
1019 + parno = g->regnpar;
1021 + ret = regnode(g, OPEN+parno);
1025 + /* Pick up the branches, linking them together. */
1026 + br = regbranch(g, &flags);
1030 + regtail(g, ret, br); /* OPEN -> first. */
1033 + if (!(flags&HASWIDTH))
1034 + *flagp &= ~HASWIDTH;
1035 + *flagp |= flags&SPSTART;
1036 + while (*g->regparse == '|') {
1038 + br = regbranch(g, &flags);
1041 + regtail(g, 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(g, (paren) ? CLOSE+parno : END);
1049 + regtail(g, ret, ender);
1051 + /* Hook the tails of the branches to the closing node. */
1052 + for (br = ret; br != NULL; br = regnext(g, br))
1053 + regoptail(g, br, ender);
1055 + /* Check for proper termination. */
1056 + if (paren && *g->regparse++ != ')') {
1057 + FAIL("unmatched ()");
1058 + } else if (!paren && *g->regparse != '\0') {
1059 + if (*g->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(struct match_globals *g, int *flagp)
1077 + register char *ret;
1078 + register char *chain;
1079 + register char *latest;
1082 + *flagp = WORST; /* Tentatively. */
1084 + ret = regnode(g, BRANCH);
1086 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1087 + latest = regpiece(g, &flags);
1088 + if (latest == NULL)
1090 + *flagp |= flags&HASWIDTH;
1091 + if (chain == NULL) /* First piece. */
1092 + *flagp |= flags&SPSTART;
1094 + regtail(g, chain, latest);
1097 + if (chain == NULL) /* Loop ran zero times. */
1098 + (void) regnode(g, 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(struct match_globals *g, int *flagp)
1115 + register char *ret;
1117 + register char *next;
1120 + ret = regatom(g, &flags);
1124 + op = *g->regparse;
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(g, STAR, ret);
1136 + else if (op == '*') {
1137 + /* Emit x* as (x&|), where & means "self". */
1138 + reginsert(g, BRANCH, ret); /* Either x */
1139 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1140 + regoptail(g, ret, ret); /* back */
1141 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1142 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1143 + } else if (op == '+' && (flags&SIMPLE))
1144 + reginsert(g, PLUS, ret);
1145 + else if (op == '+') {
1146 + /* Emit x+ as x(&|), where & means "self". */
1147 + next = regnode(g, BRANCH); /* Either */
1148 + regtail(g, ret, next);
1149 + regtail(g, regnode(g, BACK), ret); /* loop back */
1150 + regtail(g, next, regnode(g, BRANCH)); /* or */
1151 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1152 + } else if (op == '?') {
1153 + /* Emit x? as (x|) */
1154 + reginsert(g, BRANCH, ret); /* Either x */
1155 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1156 + next = regnode(g, NOTHING); /* null. */
1157 + regtail(g, ret, next);
1158 + regoptail(g, ret, next);
1161 + if (ISMULT(*g->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(struct match_globals *g, int *flagp)
1178 + register char *ret;
1181 + *flagp = WORST; /* Tentatively. */
1183 + switch (*g->regparse++) {
1185 + ret = regnode(g, BOL);
1188 + ret = regnode(g, EOL);
1191 + ret = regnode(g, ANY);
1192 + *flagp |= HASWIDTH|SIMPLE;
1195 + register int class;
1196 + register int classend;
1198 + if (*g->regparse == '^') { /* Complement of range. */
1199 + ret = regnode(g, ANYBUT);
1202 + ret = regnode(g, ANYOF);
1203 + if (*g->regparse == ']' || *g->regparse == '-')
1204 + regc(g, *g->regparse++);
1205 + while (*g->regparse != '\0' && *g->regparse != ']') {
1206 + if (*g->regparse == '-') {
1208 + if (*g->regparse == ']' || *g->regparse == '\0')
1211 + class = UCHARAT(g->regparse-2)+1;
1212 + classend = UCHARAT(g->regparse);
1213 + if (class > classend+1)
1214 + FAIL("invalid [] range");
1215 + for (; class <= classend; class++)
1220 + regc(g, *g->regparse++);
1223 + if (*g->regparse != ']')
1224 + FAIL("unmatched []");
1226 + *flagp |= HASWIDTH|SIMPLE;
1230 + ret = reg(g, 1, &flags);
1233 + *flagp |= flags&(HASWIDTH|SPSTART);
1238 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1243 + FAIL("?+* follows nothing");
1246 + if (*g->regparse == '\0')
1247 + FAIL("trailing \\");
1248 + ret = regnode(g, EXACTLY);
1249 + regc(g, *g->regparse++);
1251 + *flagp |= HASWIDTH|SIMPLE;
1255 + register char ender;
1258 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1260 + FAIL("internal disaster");
1261 + ender = *(g->regparse+len);
1262 + if (len > 1 && ISMULT(ender))
1263 + len--; /* Back off clear of ?+* operand. */
1264 + *flagp |= HASWIDTH;
1267 + ret = regnode(g, EXACTLY);
1269 + regc(g, *g->regparse++);
1281 + - regnode - emit a node
1283 +static char * /* Location. */
1284 +regnode(struct match_globals *g, char op)
1286 + register char *ret;
1287 + register char *ptr;
1290 + if (ret == &g->regdummy) {
1297 + *ptr++ = '\0'; /* Null "next" pointer. */
1305 + - regc - emit (if appropriate) a byte of code
1308 +regc(struct match_globals *g, char b)
1310 + if (g->regcode != &g->regdummy)
1311 + *g->regcode++ = b;
1317 + - reginsert - insert an operator in front of already-emitted operand
1319 + * Means relocating the operand.
1322 +reginsert(struct match_globals *g, char op, char* opnd)
1324 + register char *src;
1325 + register char *dst;
1326 + register char *place;
1328 + if (g->regcode == &g->regdummy) {
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(struct match_globals *g, char *p, char *val)
1351 + register char *scan;
1352 + register char *temp;
1353 + register int offset;
1355 + if (p == &g->regdummy)
1358 + /* Find last node. */
1361 + temp = regnext(g, 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(struct match_globals *g, char *p, char *val)
1381 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1382 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1384 + regtail(g, OPERAND(p), val);
1388 + * regexec and friends
1395 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1396 +STATIC int regmatch(struct match_globals *g, char *prog);
1397 +STATIC int regrepeat(struct match_globals *g, char *p);
1400 +int regnarrate = 0;
1402 +STATIC char *regprop(char *op);
1406 + - regexec - match a regexp against a string
1409 +regexec(regexp *prog, char *string)
1412 + struct match_globals g;
1414 + /* Be paranoid... */
1415 + if (prog == NULL || string == NULL) {
1416 + printk("<3>Regexp: NULL parameter\n");
1420 + /* Check validity of program. */
1421 + if (UCHARAT(prog->program) != MAGIC) {
1422 + printk("<3>Regexp: corrupted program\n");
1426 + /* If there is a "must appear" string, look for it. */
1427 + if (prog->regmust != NULL) {
1429 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1430 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1431 + break; /* Found it. */
1434 + if (s == NULL) /* Not present. */
1438 + /* Mark beginning of line for ^ . */
1439 + g.regbol = string;
1441 + /* Simplest case: anchored match need be tried only once. */
1442 + if (prog->reganch)
1443 + return(regtry(&g, prog, string));
1445 + /* Messy cases: unanchored match. */
1447 + if (prog->regstart != '\0')
1448 + /* We know what char it must start with. */
1449 + while ((s = strchr(s, prog->regstart)) != NULL) {
1450 + if (regtry(&g, prog, s))
1455 + /* We don't -- general case. */
1457 + if (regtry(&g, prog, s))
1459 + } while (*s++ != '\0');
1466 + - regtry - try match at specific point
1468 +static int /* 0 failure, 1 success */
1469 +regtry(struct match_globals *g, regexp *prog, char *string)
1472 + register char **sp;
1473 + register char **ep;
1475 + g->reginput = string;
1476 + g->regstartp = prog->startp;
1477 + g->regendp = prog->endp;
1479 + sp = prog->startp;
1481 + for (i = NSUBEXP; i > 0; i--) {
1485 + if (regmatch(g, prog->program + 1)) {
1486 + prog->startp[0] = string;
1487 + prog->endp[0] = g->reginput;
1494 + - regmatch - main matching routine
1496 + * Conceptually the strategy is simple: check to see whether the current
1497 + * node matches, call self recursively to see whether the rest matches,
1498 + * and then act accordingly. In practice we make some effort to avoid
1499 + * recursion, in particular by going through "ordinary" nodes (that don't
1500 + * need to know whether the rest of the match failed) by a loop instead of
1503 +static int /* 0 failure, 1 success */
1504 +regmatch(struct match_globals *g, char *prog)
1506 + register char *scan = prog; /* Current node. */
1507 + char *next; /* Next node. */
1510 + if (scan != NULL && regnarrate)
1511 + fprintf(stderr, "%s(\n", regprop(scan));
1513 + while (scan != NULL) {
1516 + fprintf(stderr, "%s...\n", regprop(scan));
1518 + next = regnext(g, scan);
1520 + switch (OP(scan)) {
1522 + if (g->reginput != g->regbol)
1526 + if (*g->reginput != '\0')
1530 + if (*g->reginput == '\0')
1536 + register char *opnd;
1538 + opnd = OPERAND(scan);
1539 + /* Inline the first character, for speed. */
1540 + if (*opnd != *g->reginput)
1542 + len = strlen(opnd);
1543 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1545 + g->reginput += len;
1549 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1554 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1571 + register char *save;
1573 + no = OP(scan) - OPEN;
1574 + save = g->reginput;
1576 + if (regmatch(g, next)) {
1578 + * Don't set startp if some later
1579 + * invocation of the same parentheses
1582 + if (g->regstartp[no] == NULL)
1583 + g->regstartp[no] = save;
1600 + register char *save;
1602 + no = OP(scan) - CLOSE;
1603 + save = g->reginput;
1605 + if (regmatch(g, next)) {
1607 + * Don't set endp if some later
1608 + * invocation of the same parentheses
1611 + if (g->regendp[no] == NULL)
1612 + g->regendp[no] = save;
1619 + register char *save;
1621 + if (OP(next) != BRANCH) /* No choice. */
1622 + next = OPERAND(scan); /* Avoid recursion. */
1625 + save = g->reginput;
1626 + if (regmatch(g, OPERAND(scan)))
1628 + g->reginput = save;
1629 + scan = regnext(g, scan);
1630 + } while (scan != NULL && OP(scan) == BRANCH);
1638 + register char nextch;
1640 + register char *save;
1644 + * Lookahead to avoid useless match attempts
1645 + * when we know what character comes next.
1648 + if (OP(next) == EXACTLY)
1649 + nextch = *OPERAND(next);
1650 + min = (OP(scan) == STAR) ? 0 : 1;
1651 + save = g->reginput;
1652 + no = regrepeat(g, OPERAND(scan));
1653 + while (no >= min) {
1654 + /* If it could work, try it. */
1655 + if (nextch == '\0' || *g->reginput == nextch)
1656 + if (regmatch(g, next))
1658 + /* Couldn't or didn't -- back up. */
1660 + g->reginput = save + no;
1666 + return(1); /* Success! */
1669 + printk("<3>Regexp: memory corruption\n");
1678 + * We get here only if there's trouble -- normally "case END" is
1679 + * the terminating point.
1681 + printk("<3>Regexp: corrupted pointers\n");
1686 + - regrepeat - repeatedly match something simple, report how many
1689 +regrepeat(struct match_globals *g, char *p)
1691 + register int count = 0;
1692 + register char *scan;
1693 + register char *opnd;
1695 + scan = g->reginput;
1696 + opnd = OPERAND(p);
1699 + count = strlen(scan);
1703 + while (*opnd == *scan) {
1709 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1715 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1720 + default: /* Oh dear. Called inappropriately. */
1721 + printk("<3>Regexp: internal foulup\n");
1722 + count = 0; /* Best compromise. */
1725 + g->reginput = scan;
1731 + - regnext - dig the "next" pointer out of a node
1734 +regnext(struct match_globals *g, char *p)
1736 + register int offset;
1738 + if (p == &g->regdummy)
1745 + if (OP(p) == BACK)
1753 +STATIC char *regprop();
1756 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1762 + register char op = EXACTLY; /* Arbitrary non-END op. */
1763 + register char *next;
1764 + /* extern char *strchr(); */
1767 + s = r->program + 1;
1768 + while (op != END) { /* While that wasn't END last time... */
1770 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1771 + next = regnext(s);
1772 + if (next == NULL) /* Next ptr. */
1775 + printf("(%d)", (s-r->program)+(next-s));
1777 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1778 + /* Literal string, where present. */
1779 + while (*s != '\0') {
1788 + /* Header fields of interest. */
1789 + if (r->regstart != '\0')
1790 + printf("start `%c' ", r->regstart);
1792 + printf("anchored ");
1793 + if (r->regmust != NULL)
1794 + printf("must have \"%s\"", r->regmust);
1799 + - regprop - printable representation of opcode
1806 + static char buf[BUFLEN];
1850 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1862 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1872 + printk("<3>Regexp: corrupted opcode\n");
1876 + strncat(buf, p, BUFLEN-strlen(buf));
1882 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regexp.h linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regexp.h
1883 --- linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1884 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regexp.h 2007-05-26 20:13:52.652129512 +0200
1887 + * Definitions etc. for regexp(3) routines.
1889 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1890 + * not the System V one.
1898 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1899 +which contains a version of this library, says:
1902 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1903 + * will not work properly.
1906 +However, it looks rather like this library is limited to 10. If you think
1907 +otherwise, let us know.
1911 +typedef struct regexp {
1912 + char *startp[NSUBEXP];
1913 + char *endp[NSUBEXP];
1914 + char regstart; /* Internal use only. */
1915 + char reganch; /* Internal use only. */
1916 + char *regmust; /* Internal use only. */
1917 + int regmlen; /* Internal use only. */
1918 + char program[1]; /* Unwarranted chumminess with compiler. */
1921 +regexp * regcomp(char *exp, int *patternsize);
1922 +int regexec(regexp *prog, char *string);
1923 +void regsub(regexp *prog, char *source, char *dest);
1924 +void regerror(char *s);
1927 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regmagic.h linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regmagic.h
1928 --- linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1929 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regmagic.h 2007-05-26 20:13:52.652129512 +0200
1932 + * The first byte of the regexp internal "program" is actually this magic
1933 + * number; the start node begins in the second byte.
1936 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regsub.c linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regsub.c
1937 --- linux-2.6.21.1.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1938 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/regexp/regsub.c 2007-05-26 20:13:52.653129360 +0200
1942 + * @(#)regsub.c 1.3 of 2 April 86
1944 + * Copyright (c) 1986 by University of Toronto.
1945 + * Written by Henry Spencer. Not derived from licensed software.
1947 + * Permission is granted to anyone to use this software for any
1948 + * purpose on any computer system, and to redistribute it freely,
1949 + * subject to the following restrictions:
1951 + * 1. The author is not responsible for the consequences of use of
1952 + * this software, no matter how awful, even if they arise
1953 + * from defects in it.
1955 + * 2. The origin of this software must not be misrepresented, either
1956 + * by explicit claim or by omission.
1958 + * 3. Altered versions must be plainly marked as such, and must not
1959 + * be misrepresented as being the original software.
1962 + * This code was modified by Ethan Sommer to work within the kernel
1963 + * (it now uses kmalloc etc..)
1966 +#include "regexp.h"
1967 +#include "regmagic.h"
1968 +#include <linux/string.h>
1972 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1974 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1978 +//void regerror(char * s)
1980 +// printk("regexp(3): %s", s);
1981 +// /* NOTREACHED */
1986 + - regsub - perform substitutions after a regexp match
1989 +regsub(regexp * prog, char * source, char * dest)
1991 + register char *src;
1992 + register char *dst;
1997 + /* Not necessary and gcc doesn't like it -MLS */
1998 + /*extern char *strncpy();*/
2000 + if (prog == NULL || source == NULL || dest == NULL) {
2001 + regerror("NULL parm to regsub");
2004 + if (UCHARAT(prog->program) != MAGIC) {
2005 + regerror("damaged regexp fed to regsub");
2011 + while ((c = *src++) != '\0') {
2014 + else if (c == '\\' && '0' <= *src && *src <= '9')
2015 + no = *src++ - '0';
2019 + if (no < 0) { /* Ordinary character. */
2020 + if (c == '\\' && (*src == '\\' || *src == '&'))
2023 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2024 + len = prog->endp[no] - prog->startp[no];
2025 + (void) strncpy(dst, prog->startp[no], len);
2027 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2028 + regerror("damaged match string");