1 diff -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_conntrack.h 2006-11-29 22:57:37.000000000 +0100
3 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2006-12-14 03:13:37.000000000 +0100
5 /* Traversed often, so hopefully in different cacheline to top */
6 /* These are my tuples; original and reply */
7 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
9 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
11 + char * app_proto; /* e.g. "http". NULL before decision. "unknown" after decision if no match */
12 + char * app_data; /* application layer data so far. NULL after match decision */
13 + unsigned int app_data_len;
19 struct ip_conntrack_expect
20 diff -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_layer7.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_layer7.h
21 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
22 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2006-12-14 03:13:37.000000000 +0100
25 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
26 + http://l7-filter.sf.net
28 + This program is free software; you can redistribute it and/or
29 + modify it under the terms of the GNU General Public License
30 + as published by the Free Software Foundation; either version
31 + 2 of the License, or (at your option) any later version.
32 + http://www.gnu.org/licenses/gpl.txt
35 +#ifndef _IPT_LAYER7_H
36 +#define _IPT_LAYER7_H
38 +#define MAX_PATTERN_LEN 8192
39 +#define MAX_PROTOCOL_LEN 256
41 +typedef char *(*proc_ipt_search) (char *, char, char *);
43 +struct ipt_layer7_info {
44 + char protocol[MAX_PROTOCOL_LEN];
46 + char pattern[MAX_PATTERN_LEN];
50 +#endif /* _IPT_LAYER7_H */
51 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_conntrack_core.c linux-2.6.19.dev/net/ipv4/netfilter/ip_conntrack_core.c
52 --- linux-2.6.19.old/net/ipv4/netfilter/ip_conntrack_core.c 2006-11-29 22:57:37.000000000 +0100
53 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_conntrack_core.c 2006-12-14 03:13:37.000000000 +0100
56 ip_ct_remove_expectations(ct);
58 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
59 + if(ct->layer7.app_proto)
60 + kfree(ct->layer7.app_proto);
61 + if(ct->layer7.app_data)
62 + kfree(ct->layer7.app_data);
65 /* We overload first tuple to link into unconfirmed list. */
66 if (!is_confirmed(ct)) {
67 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
68 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.6.19.dev/net/ipv4/netfilter/ip_conntrack_standalone.c
69 --- linux-2.6.19.old/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-11-29 22:57:37.000000000 +0100
70 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-12-14 03:13:37.000000000 +0100
75 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
76 + if(conntrack->layer7.app_proto)
77 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
81 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
84 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ipt_layer7.c linux-2.6.19.dev/net/ipv4/netfilter/ipt_layer7.c
85 --- linux-2.6.19.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
86 +++ linux-2.6.19.dev/net/ipv4/netfilter/ipt_layer7.c 2006-12-14 03:13:37.000000000 +0100
89 + Kernel module to match application layer (OSI layer 7)
90 + data in connections.
92 + http://l7-filter.sf.net
94 + By Matthew Strait and Ethan Sommer, 2003-2005.
96 + This program is free software; you can redistribute it and/or
97 + modify it under the terms of the GNU General Public License
98 + as published by the Free Software Foundation; either version
99 + 2 of the License, or (at your option) any later version.
100 + http://www.gnu.org/licenses/gpl.txt
102 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
103 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
106 +#include <linux/module.h>
107 +#include <linux/skbuff.h>
108 +#include <linux/netfilter_ipv4/ip_conntrack.h>
109 +#include <linux/proc_fs.h>
110 +#include <linux/ctype.h>
112 +#include <net/tcp.h>
113 +#include <linux/spinlock.h>
115 +#include "regexp/regexp.c"
117 +#include <linux/netfilter_ipv4/ipt_layer7.h>
118 +#include <linux/netfilter_ipv4/ip_tables.h>
120 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
121 +MODULE_LICENSE("GPL");
122 +MODULE_DESCRIPTION("iptables application layer match module");
123 +MODULE_VERSION("2.0");
125 +static int maxdatalen = 2048; // this is the default
126 +module_param(maxdatalen, int, 0444);
127 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
129 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
130 + #define DPRINTK(format,args...) printk(format,##args)
132 + #define DPRINTK(format,args...)
135 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
136 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
138 +/* Number of packets whose data we look at.
139 +This can be modified through /proc/net/layer7_numpackets */
140 +static int num_packets = 10;
142 +static struct pattern_cache {
143 + char * regex_string;
145 + struct pattern_cache * next;
146 +} * first_pattern_cache = NULL;
148 +/* I'm new to locking. Here are my assumptions:
150 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
151 + if they did, nothing awful would happen.
153 +- This code will never be processing the same packet twice at the same time,
154 + because iptables rules are traversed in order.
156 +- It doesn't matter if two packets from different connections are in here at
157 + the same time, because they don't share any data.
159 +- It _does_ matter if two packets from the same connection are here at the same
160 + time. In this case, we have to protect the conntracks and the list of
163 +DEFINE_RWLOCK(ct_lock);
164 +DEFINE_SPINLOCK(list_lock);
166 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
167 +/* Converts an unfriendly string into a friendly one by
168 +replacing unprintables with periods and all whitespace with " ". */
169 +static char * friendly_print(unsigned char * s)
171 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
175 + if (net_ratelimit())
176 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
180 + for(i = 0; i < strlen(s); i++){
181 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
182 + else if(isspace(s[i])) f[i] = ' ';
189 +static char dec2hex(int i)
193 + return (char)(i + '0');
196 + return (char)(i - 10 + 'a');
199 + if (net_ratelimit())
200 + printk("Problem in dec2hex\n");
205 +static char * hex_print(unsigned char * s)
207 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
211 + if (net_ratelimit())
212 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
216 + for(i = 0; i < strlen(s); i++) {
217 + g[i*3 ] = dec2hex(s[i]/16);
218 + g[i*3 + 1] = dec2hex(s[i]%16);
227 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
228 +over again, it make sense to cache the results. */
229 +static regexp * compile_and_cache(char * regex_string, char * protocol)
231 + struct pattern_cache * node = first_pattern_cache;
232 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
233 + struct pattern_cache * tmp;
236 + while (node != NULL) {
237 + if (!strcmp(node->regex_string, regex_string))
238 + return node->pattern;
240 + last_pattern_cache = node;/* points at the last non-NULL node */
244 + /* If we reach the end of the list, then we have not yet cached
245 + the pattern for this regex. Let's do that now.
246 + Be paranoid about running out of memory to avoid list corruption. */
247 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
250 + if (net_ratelimit())
251 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
255 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
256 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
259 + if(!tmp->regex_string || !tmp->pattern) {
260 + if (net_ratelimit())
261 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
262 + kfree(tmp->regex_string);
263 + kfree(tmp->pattern);
268 + /* Ok. The new node is all ready now. */
271 + if(first_pattern_cache == NULL) /* list is empty */
272 + first_pattern_cache = node; /* make node the beginning */
274 + last_pattern_cache->next = node; /* attach node to the end */
276 + /* copy the string and compile the regex */
277 + len = strlen(regex_string);
278 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
279 + node->pattern = regcomp(regex_string, &len);
280 + if ( !node->pattern ) {
281 + if (net_ratelimit())
282 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
283 + /* pattern is now cached as NULL, so we won't try again. */
286 + strcpy(node->regex_string, regex_string);
287 + return node->pattern;
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 + if (net_ratelimit())
322 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
323 + return ip_hl + 8; /* something reasonable */
327 +/* handles whether there's a match when we aren't appending data anymore */
328 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
329 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
330 + struct ipt_layer7_info * info)
332 + /* If we're in here, throw the app data away */
333 + write_lock(&ct_lock);
334 + if(master_conntrack->layer7.app_data != NULL) {
336 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
337 + if(!master_conntrack->layer7.app_proto) {
338 + char * f = friendly_print(master_conntrack->layer7.app_data);
339 + char * g = hex_print(master_conntrack->layer7.app_data);
340 + DPRINTK("\nl7-filter gave up after %d bytes (%llu packets):\n%s\n",
344 + DPRINTK("In hex: %s\n", g);
349 + kfree(master_conntrack->layer7.app_data);
350 + master_conntrack->layer7.app_data = NULL; /* don't free again */
352 + write_unlock(&ct_lock);
354 + if(master_conntrack->layer7.app_proto){
355 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
356 + write_lock(&ct_lock);
357 + if(!conntrack->layer7.app_proto) {
358 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
359 + if(!conntrack->layer7.app_proto){
360 + if (net_ratelimit())
361 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
362 + write_unlock(&ct_lock);
365 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
367 + write_unlock(&ct_lock);
369 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
372 + /* If not classified, set to "unknown" to distinguish from
373 + connections that are still being tested. */
374 + write_lock(&ct_lock);
375 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
376 + if(!master_conntrack->layer7.app_proto){
377 + if (net_ratelimit())
378 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
379 + write_unlock(&ct_lock);
382 + strcpy(master_conntrack->layer7.app_proto, "unknown");
383 + write_unlock(&ct_lock);
388 +static int add_datastr(char *target, int offset, char *app_data, int len)
392 + /* Strip nulls. Make everything lower case (our regex lib doesn't
393 + do case insensitivity). Add it to the end of the current data. */
394 + for(i = 0; i < maxdatalen-offset-1 && i < len; i++) {
395 + if(app_data[i] != '\0') {
396 + target[length+offset] =
397 + /* the kernel version of tolower mungs 'upper ascii' */
398 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
402 + target[length+offset] = '\0';
407 +/* add the new app data to the conntrack. Return number of bytes added. */
408 +static int add_data(struct ip_conntrack * master_conntrack,
409 + char * app_data, int appdatalen)
413 + length = add_datastr(master_conntrack->layer7.app_data, master_conntrack->layer7.app_data_len, app_data, appdatalen);
414 + master_conntrack->layer7.app_data_len += length;
419 +/* Returns true on match and false otherwise. */
420 +static int match(const struct sk_buff *skb_t, const struct net_device *in,
421 + const struct net_device *out, const struct xt_match *match,
422 + const void *matchinfo, int offset,
423 + unsigned int protoff, int *hotdrop)
425 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
426 + enum ip_conntrack_info master_ctinfo, ctinfo;
427 + struct ip_conntrack *master_conntrack, *conntrack;
428 + unsigned char *app_data, *tmp_data;
429 + unsigned int pattern_result, appdatalen;
430 + regexp * comppattern;
431 + struct sk_buff *skb = skb_t; /* to leave warning - FIXME */
433 + if(!can_handle(skb)){
434 + DPRINTK("layer7: This is some protocol I can't handle.\n");
435 + return info->invert;
438 + /* Treat parent & all its children together as one connection, except
439 + for the purpose of setting conntrack->layer7.app_proto in the actual
440 + connection. This makes /proc/net/ip_conntrack more satisfying. */
441 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
442 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
443 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
444 + return info->invert;
447 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
448 + while (master_ct(master_conntrack) != NULL)
449 + master_conntrack = master_ct(master_conntrack);
451 + /* if we've classified it or seen too many packets */
452 + if(!info->pkt && (TOTAL_PACKETS > num_packets ||
453 + master_conntrack->layer7.app_proto)) {
455 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
457 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
458 + rules. I'm not sure that using cb for this purpose is correct, although
459 + it says "put your private variables there". But it doesn't look like it
460 + is being used for anything else in the skbs that make it here. How can
461 + I write to cb without making the compiler angry? */
462 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
464 + return (pattern_result ^ info->invert);
467 + if(skb_is_nonlinear(skb)){
468 + if(skb_linearize(skb) != 0){
469 + if (net_ratelimit())
470 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
471 + return info->invert;
475 + /* now that the skb is linearized, it's safe to set these. */
476 + app_data = skb->data + app_data_offset(skb);
477 + appdatalen = skb->tail - app_data;
479 + spin_lock_bh(&list_lock);
480 + /* the return value gets checked later, when we're ready to use it */
481 + comppattern = compile_and_cache(info->pattern, info->protocol);
482 + spin_unlock_bh(&list_lock);
485 + tmp_data = kmalloc(maxdatalen, GFP_ATOMIC);
487 + if (net_ratelimit())
488 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
489 + return info->invert;
492 + tmp_data[0] = '\0';
493 + add_datastr(tmp_data, 0, app_data, appdatalen);
494 + pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
498 + return (pattern_result ^ info->invert);
501 + /* On the first packet of a connection, allocate space for app data */
502 + write_lock(&ct_lock);
503 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
504 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
505 + if(!master_conntrack->layer7.app_data){
506 + if (net_ratelimit())
507 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
508 + write_unlock(&ct_lock);
509 + return info->invert;
512 + master_conntrack->layer7.app_data[0] = '\0';
514 + write_unlock(&ct_lock);
516 + /* Can be here, but unallocated, if numpackets is increased near
517 + the beginning of a connection */
518 + if(master_conntrack->layer7.app_data == NULL)
519 + return (info->invert); /* unmatched */
523 + write_lock(&ct_lock);
524 + newbytes = add_data(master_conntrack, app_data, appdatalen);
525 + write_unlock(&ct_lock);
527 + if(newbytes == 0) { /* didn't add any data */
529 + /* Didn't match before, not going to match now */
530 + return info->invert;
534 + /* If looking for "unknown", then never match. "Unknown" means that
535 + we've given up; we're still trying with these packets. */
536 + if(!strcmp(info->protocol, "unknown")) {
537 + pattern_result = 0;
538 + /* If the regexp failed to compile, don't bother running it */
539 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
540 + DPRINTK("layer7: matched %s\n", info->protocol);
541 + pattern_result = 1;
542 + } else pattern_result = 0;
544 + if(pattern_result) {
545 + write_lock(&ct_lock);
546 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
547 + if(!master_conntrack->layer7.app_proto){
548 + if (net_ratelimit())
549 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
550 + write_unlock(&ct_lock);
551 + return (pattern_result ^ info->invert);
553 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
554 + write_unlock(&ct_lock);
557 + /* mark the packet seen */
560 + return (pattern_result ^ info->invert);
563 +static struct ipt_match layer7_match = {
566 + .matchsize = sizeof(struct ipt_layer7_info),
570 +/* taken from drivers/video/modedb.c */
571 +static int my_atoi(const char *s)
578 + val = 10*val+(*s-'0');
586 +/* write out num_packets to userland. */
587 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
588 + int* eof, void * data)
590 + if(num_packets > 99 && net_ratelimit())
591 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
593 + page[0] = num_packets/10 + '0';
594 + page[1] = num_packets%10 + '0';
603 +/* Read in num_packets from userland */
604 +static int layer7_write_proc(struct file* file, const char* buffer,
605 + unsigned long count, void *data)
607 + char * foo = kmalloc(count, GFP_ATOMIC);
610 + if (net_ratelimit())
611 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
615 + if(copy_from_user(foo, buffer, count)) {
620 + num_packets = my_atoi(foo);
623 + /* This has an arbitrary limit to make the math easier. I'm lazy.
624 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
625 + if(num_packets > 99) {
626 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
628 + } else if(num_packets < 1) {
629 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
636 +/* register the proc file */
637 +static void layer7_init_proc(void)
639 + struct proc_dir_entry* entry;
640 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
641 + entry->read_proc = layer7_read_proc;
642 + entry->write_proc = layer7_write_proc;
645 +static void layer7_cleanup_proc(void)
647 + remove_proc_entry("layer7_numpackets", proc_net);
650 +static int __init init(void)
652 + layer7_init_proc();
653 + if(maxdatalen < 1) {
654 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
657 + /* This is not a hard limit. It's just here to prevent people from
658 + bringing their slow machines to a grinding halt. */
659 + else if(maxdatalen > 65536) {
660 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
661 + maxdatalen = 65536;
663 + return ipt_register_match(&layer7_match);
666 +static void __exit fini(void)
668 + layer7_cleanup_proc();
669 + ipt_unregister_match(&layer7_match);
674 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Kconfig linux-2.6.19.dev/net/ipv4/netfilter/Kconfig
675 --- linux-2.6.19.old/net/ipv4/netfilter/Kconfig 2006-11-29 22:57:37.000000000 +0100
676 +++ linux-2.6.19.dev/net/ipv4/netfilter/Kconfig 2006-12-14 03:13:37.000000000 +0100
678 destination IP' or `500pps from any given source IP' with a single
681 +config IP_NF_MATCH_LAYER7
682 + tristate "Layer 7 match support (EXPERIMENTAL)"
683 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
685 + Say Y if you want to be able to classify connections (and their
686 + packets) based on regular expression matching of their application
687 + layer data. This is one way to classify applications such as
688 + peer-to-peer filesharing systems that do not always use the same
691 + To compile it as a module, choose M here. If unsure, say N.
693 +config IP_NF_MATCH_LAYER7_DEBUG
694 + bool "Layer 7 debugging output"
695 + depends on IP_NF_MATCH_LAYER7
697 + Say Y to get lots of debugging output.
699 # `filter', generic and specific targets
701 tristate "Packet filtering"
702 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Makefile linux-2.6.19.dev/net/ipv4/netfilter/Makefile
703 --- linux-2.6.19.old/net/ipv4/netfilter/Makefile 2006-11-29 22:57:37.000000000 +0100
704 +++ linux-2.6.19.dev/net/ipv4/netfilter/Makefile 2006-12-14 03:13:37.000000000 +0100
706 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
707 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
709 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
712 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
713 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
714 diff -urN linux-2.6.19.old/net/ipv4/netfilter/regexp/regexp.c linux-2.6.19.dev/net/ipv4/netfilter/regexp/regexp.c
715 --- linux-2.6.19.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
716 +++ linux-2.6.19.dev/net/ipv4/netfilter/regexp/regexp.c 2006-12-14 03:13:37.000000000 +0100
719 + * regcomp and regexec -- regsub and regerror are elsewhere
720 + * @(#)regexp.c 1.3 of 18 April 87
722 + * Copyright (c) 1986 by University of Toronto.
723 + * Written by Henry Spencer. Not derived from licensed software.
725 + * Permission is granted to anyone to use this software for any
726 + * purpose on any computer system, and to redistribute it freely,
727 + * subject to the following restrictions:
729 + * 1. The author is not responsible for the consequences of use of
730 + * this software, no matter how awful, even if they arise
731 + * from defects in it.
733 + * 2. The origin of this software must not be misrepresented, either
734 + * by explicit claim or by omission.
736 + * 3. Altered versions must be plainly marked as such, and must not
737 + * be misrepresented as being the original software.
739 + * Beware that some of this code is subtly aware of the way operator
740 + * precedence is structured in regular expressions. Serious changes in
741 + * regular-expression syntax might require a total rethink.
743 + * This code was modified by Ethan Sommer to work within the kernel
744 + * (it now uses kmalloc etc..)
746 + * Modified slightly by Matthew Strait to use more modern C.
750 +#include "regmagic.h"
752 +/* added by ethan and matt. Lets it work in both kernel and user space.
753 +(So iptables can use it, for instance.) Yea, it goes both ways... */
755 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
757 + #define printk(format,args...) printf(format,##args)
760 +void regerror(char * s)
762 + printk("<3>Regexp: %s\n", s);
767 + * The "internal use only" fields in regexp.h are present to pass info from
768 + * compile to execute that permits the execute phase to run lots faster on
769 + * simple cases. They are:
771 + * regstart char that must begin a match; '\0' if none obvious
772 + * reganch is the match anchored (at beginning-of-line only)?
773 + * regmust string (pointer into program) that match must include, or NULL
774 + * regmlen length of regmust string
776 + * Regstart and reganch permit very fast decisions on suitable starting points
777 + * for a match, cutting down the work a lot. Regmust permits fast rejection
778 + * of lines that cannot possibly match. The regmust tests are costly enough
779 + * that regcomp() supplies a regmust only if the r.e. contains something
780 + * potentially expensive (at present, the only such thing detected is * or +
781 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
782 + * supplied because the test in regexec() needs it and regcomp() is computing
787 + * Structure for regexp "program". This is essentially a linear encoding
788 + * of a nondeterministic finite-state machine (aka syntax charts or
789 + * "railroad normal form" in parsing technology). Each node is an opcode
790 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
791 + * all nodes except BRANCH implement concatenation; a "next" pointer with
792 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
793 + * have one of the subtle syntax dependencies: an individual BRANCH (as
794 + * opposed to a collection of them) is never concatenated with anything
795 + * because of operator precedence.) The operand of some types of node is
796 + * a literal string; for others, it is a node leading into a sub-FSM. In
797 + * particular, the operand of a BRANCH node is the first node of the branch.
798 + * (NB this is *not* a tree structure: the tail of the branch connects
799 + * to the thing following the set of BRANCHes.) The opcodes are:
802 +/* definition number opnd? meaning */
803 +#define END 0 /* no End of program. */
804 +#define BOL 1 /* no Match "" at beginning of line. */
805 +#define EOL 2 /* no Match "" at end of line. */
806 +#define ANY 3 /* no Match any one character. */
807 +#define ANYOF 4 /* str Match any character in this string. */
808 +#define ANYBUT 5 /* str Match any character not in this string. */
809 +#define BRANCH 6 /* node Match this alternative, or the next... */
810 +#define BACK 7 /* no Match "", "next" ptr points backward. */
811 +#define EXACTLY 8 /* str Match this string. */
812 +#define NOTHING 9 /* no Match empty string. */
813 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
814 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
815 +#define OPEN 20 /* no Mark this point in input as start of #n. */
816 + /* OPEN+1 is number 1, etc. */
817 +#define CLOSE 30 /* no Analogous to OPEN. */
822 + * BRANCH The set of branches constituting a single choice are hooked
823 + * together with their "next" pointers, since precedence prevents
824 + * anything being concatenated to any individual branch. The
825 + * "next" pointer of the last BRANCH in a choice points to the
826 + * thing following the whole choice. This is also where the
827 + * final "next" pointer of each individual branch points; each
828 + * branch starts with the operand node of a BRANCH node.
830 + * BACK Normal "next" pointers all implicitly point forward; BACK
831 + * exists to make loop structures possible.
833 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
834 + * BRANCH structures using BACK. Simple cases (one character
835 + * per match) are implemented with STAR and PLUS for speed
836 + * and to minimize recursive plunges.
838 + * OPEN,CLOSE ...are numbered at compile time.
842 + * A node is one char of opcode followed by two chars of "next" pointer.
843 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
844 + * value is a positive offset from the opcode of the node containing it.
845 + * An operand, if any, simply follows the node. (Note that much of the
846 + * code generation knows about this implicit relationship.)
848 + * Using two bytes for the "next" pointer is vast overkill for most things,
849 + * but allows patterns to get big without disasters.
851 +#define OP(p) (*(p))
852 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
853 +#define OPERAND(p) ((p) + 3)
856 + * See regmagic.h for one further detail of program structure.
861 + * Utility definitions.
864 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
866 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
869 +#define FAIL(m) { regerror(m); return(NULL); }
870 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
871 +#define META "^$.[()|?+*\\"
874 + * Flags to be passed up and down.
876 +#define HASWIDTH 01 /* Known never to match null string. */
877 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
878 +#define SPSTART 04 /* Starts with * or +. */
879 +#define WORST 0 /* Worst case. */
882 + * Global work variables for regcomp().
884 +static char *regparse; /* Input-scan pointer. */
885 +static int regnpar; /* () count. */
886 +static char regdummy;
887 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
888 +static long regsize; /* Code size. */
891 + * Forward declarations for regcomp()'s friends.
894 +#define STATIC static
896 +STATIC char *reg(int paren,int *flagp);
897 +STATIC char *regbranch(int *flagp);
898 +STATIC char *regpiece(int *flagp);
899 +STATIC char *regatom(int *flagp);
900 +STATIC char *regnode(char op);
901 +STATIC char *regnext(char *p);
902 +STATIC void regc(char b);
903 +STATIC void reginsert(char op, char *opnd);
904 +STATIC void regtail(char *p, char *val);
905 +STATIC void regoptail(char *p, char *val);
908 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
915 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
916 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
917 + if (*scan1 == *scan2++)
925 + - regcomp - compile a regular expression into internal code
927 + * We can't allocate space until we know how big the compiled form will be,
928 + * but we can't compile it (and thus know how big it is) until we've got a
929 + * place to put the code. So we cheat: we compile it twice, once with code
930 + * generation turned off and size counting turned on, and once "for real".
931 + * This also means that we don't allocate space until we are sure that the
932 + * thing really will compile successfully, and we never have to move the
933 + * code and thus invalidate pointers into it. (Note that it has to be in
934 + * one piece because free() must be able to free it all.)
936 + * Beware that the optimization-preparation code in here knows about some
937 + * of the structure of the compiled regexp.
940 +regcomp(char *exp,int *patternsize)
942 + register regexp *r;
943 + register char *scan;
944 + register char *longest;
947 + /* commented out by ethan
948 + extern char *malloc();
952 + FAIL("NULL argument");
954 + /* First pass: determine size, legality. */
958 + regcode = ®dummy;
960 + if (reg(0, &flags) == NULL)
963 + /* Small enough for pointer-storage convention? */
964 + if (regsize >= 32767L) /* Probably could be 65535L. */
965 + FAIL("regexp too big");
967 + /* Allocate space. */
968 + *patternsize=sizeof(regexp) + (unsigned)regsize;
969 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
971 + FAIL("out of space");
973 + /* Second pass: emit code. */
976 + regcode = r->program;
978 + if (reg(0, &flags) == NULL)
981 + /* Dig out information for optimizations. */
982 + r->regstart = '\0'; /* Worst-case defaults. */
986 + scan = r->program+1; /* First BRANCH. */
987 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
988 + scan = OPERAND(scan);
990 + /* Starting-point info. */
991 + if (OP(scan) == EXACTLY)
992 + r->regstart = *OPERAND(scan);
993 + else if (OP(scan) == BOL)
997 + * If there's something expensive in the r.e., find the
998 + * longest literal string that must appear and make it the
999 + * regmust. Resolve ties in favor of later strings, since
1000 + * the regstart check works with the beginning of the r.e.
1001 + * and avoiding duplication strengthens checking. Not a
1002 + * strong reason, but sufficient in the absence of others.
1004 + if (flags&SPSTART) {
1007 + for (; scan != NULL; scan = regnext(scan))
1008 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1009 + longest = OPERAND(scan);
1010 + len = strlen(OPERAND(scan));
1012 + r->regmust = longest;
1021 + - reg - regular expression, i.e. main body or parenthesized thing
1023 + * Caller must absorb opening parenthesis.
1025 + * Combining parenthesis handling with the base level of regular expression
1026 + * is a trifle forced, but the need to tie the tails of the branches to what
1027 + * follows makes it hard to avoid.
1030 +reg(int paren, int *flagp /* Parenthesized? */ )
1032 + register char *ret;
1033 + register char *br;
1034 + register char *ender;
1035 + register int parno = 0; /* 0 makes gcc happy */
1038 + *flagp = HASWIDTH; /* Tentatively. */
1040 + /* Make an OPEN node, if parenthesized. */
1042 + if (regnpar >= NSUBEXP)
1043 + FAIL("too many ()");
1046 + ret = regnode(OPEN+parno);
1050 + /* Pick up the branches, linking them together. */
1051 + br = regbranch(&flags);
1055 + regtail(ret, br); /* OPEN -> first. */
1058 + if (!(flags&HASWIDTH))
1059 + *flagp &= ~HASWIDTH;
1060 + *flagp |= flags&SPSTART;
1061 + while (*regparse == '|') {
1063 + br = regbranch(&flags);
1066 + regtail(ret, br); /* BRANCH -> BRANCH. */
1067 + if (!(flags&HASWIDTH))
1068 + *flagp &= ~HASWIDTH;
1069 + *flagp |= flags&SPSTART;
1072 + /* Make a closing node, and hook it on the end. */
1073 + ender = regnode((paren) ? CLOSE+parno : END);
1074 + regtail(ret, ender);
1076 + /* Hook the tails of the branches to the closing node. */
1077 + for (br = ret; br != NULL; br = regnext(br))
1078 + regoptail(br, ender);
1080 + /* Check for proper termination. */
1081 + if (paren && *regparse++ != ')') {
1082 + FAIL("unmatched ()");
1083 + } else if (!paren && *regparse != '\0') {
1084 + if (*regparse == ')') {
1085 + FAIL("unmatched ()");
1087 + FAIL("junk on end"); /* "Can't happen". */
1095 + - regbranch - one alternative of an | operator
1097 + * Implements the concatenation operator.
1100 +regbranch(int *flagp)
1102 + register char *ret;
1103 + register char *chain;
1104 + register char *latest;
1107 + *flagp = WORST; /* Tentatively. */
1109 + ret = regnode(BRANCH);
1111 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1112 + latest = regpiece(&flags);
1113 + if (latest == NULL)
1115 + *flagp |= flags&HASWIDTH;
1116 + if (chain == NULL) /* First piece. */
1117 + *flagp |= flags&SPSTART;
1119 + regtail(chain, latest);
1122 + if (chain == NULL) /* Loop ran zero times. */
1123 + (void) regnode(NOTHING);
1129 + - regpiece - something followed by possible [*+?]
1131 + * Note that the branching code sequences used for ? and the general cases
1132 + * of * and + are somewhat optimized: they use the same NOTHING node as
1133 + * both the endmarker for their branch list and the body of the last branch.
1134 + * It might seem that this node could be dispensed with entirely, but the
1135 + * endmarker role is not redundant.
1138 +regpiece(int *flagp)
1140 + register char *ret;
1142 + register char *next;
1145 + ret = regatom(&flags);
1150 + if (!ISMULT(op)) {
1155 + if (!(flags&HASWIDTH) && op != '?')
1156 + FAIL("*+ operand could be empty");
1157 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1159 + if (op == '*' && (flags&SIMPLE))
1160 + reginsert(STAR, ret);
1161 + else if (op == '*') {
1162 + /* Emit x* as (x&|), where & means "self". */
1163 + reginsert(BRANCH, ret); /* Either x */
1164 + regoptail(ret, regnode(BACK)); /* and loop */
1165 + regoptail(ret, ret); /* back */
1166 + regtail(ret, regnode(BRANCH)); /* or */
1167 + regtail(ret, regnode(NOTHING)); /* null. */
1168 + } else if (op == '+' && (flags&SIMPLE))
1169 + reginsert(PLUS, ret);
1170 + else if (op == '+') {
1171 + /* Emit x+ as x(&|), where & means "self". */
1172 + next = regnode(BRANCH); /* Either */
1173 + regtail(ret, next);
1174 + regtail(regnode(BACK), ret); /* loop back */
1175 + regtail(next, regnode(BRANCH)); /* or */
1176 + regtail(ret, regnode(NOTHING)); /* null. */
1177 + } else if (op == '?') {
1178 + /* Emit x? as (x|) */
1179 + reginsert(BRANCH, ret); /* Either x */
1180 + regtail(ret, regnode(BRANCH)); /* or */
1181 + next = regnode(NOTHING); /* null. */
1182 + regtail(ret, next);
1183 + regoptail(ret, next);
1186 + if (ISMULT(*regparse))
1187 + FAIL("nested *?+");
1193 + - regatom - the lowest level
1195 + * Optimization: gobbles an entire sequence of ordinary characters so that
1196 + * it can turn them into a single node, which is smaller to store and
1197 + * faster to run. Backslashed characters are exceptions, each becoming a
1198 + * separate node; the code is simpler that way and it's not worth fixing.
1201 +regatom(int *flagp)
1203 + register char *ret;
1206 + *flagp = WORST; /* Tentatively. */
1208 + switch (*regparse++) {
1210 + ret = regnode(BOL);
1213 + ret = regnode(EOL);
1216 + ret = regnode(ANY);
1217 + *flagp |= HASWIDTH|SIMPLE;
1220 + register int class;
1221 + register int classend;
1223 + if (*regparse == '^') { /* Complement of range. */
1224 + ret = regnode(ANYBUT);
1227 + ret = regnode(ANYOF);
1228 + if (*regparse == ']' || *regparse == '-')
1229 + regc(*regparse++);
1230 + while (*regparse != '\0' && *regparse != ']') {
1231 + if (*regparse == '-') {
1233 + if (*regparse == ']' || *regparse == '\0')
1236 + class = UCHARAT(regparse-2)+1;
1237 + classend = UCHARAT(regparse);
1238 + if (class > classend+1)
1239 + FAIL("invalid [] range");
1240 + for (; class <= classend; class++)
1245 + regc(*regparse++);
1248 + if (*regparse != ']')
1249 + FAIL("unmatched []");
1251 + *flagp |= HASWIDTH|SIMPLE;
1255 + ret = reg(1, &flags);
1258 + *flagp |= flags&(HASWIDTH|SPSTART);
1263 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1268 + FAIL("?+* follows nothing");
1271 + if (*regparse == '\0')
1272 + FAIL("trailing \\");
1273 + ret = regnode(EXACTLY);
1274 + regc(*regparse++);
1276 + *flagp |= HASWIDTH|SIMPLE;
1280 + register char ender;
1283 + len = my_strcspn((const char *)regparse, (const char *)META);
1285 + FAIL("internal disaster");
1286 + ender = *(regparse+len);
1287 + if (len > 1 && ISMULT(ender))
1288 + len--; /* Back off clear of ?+* operand. */
1289 + *flagp |= HASWIDTH;
1292 + ret = regnode(EXACTLY);
1294 + regc(*regparse++);
1306 + - regnode - emit a node
1308 +static char * /* Location. */
1311 + register char *ret;
1312 + register char *ptr;
1315 + if (ret == ®dummy) {
1322 + *ptr++ = '\0'; /* Null "next" pointer. */
1330 + - regc - emit (if appropriate) a byte of code
1335 + if (regcode != ®dummy)
1342 + - reginsert - insert an operator in front of already-emitted operand
1344 + * Means relocating the operand.
1347 +reginsert(char op, char* opnd)
1349 + register char *src;
1350 + register char *dst;
1351 + register char *place;
1353 + if (regcode == ®dummy) {
1361 + while (src > opnd)
1364 + place = opnd; /* Op node, where operand used to be. */
1371 + - regtail - set the next-pointer at the end of a node chain
1374 +regtail(char *p, char *val)
1376 + register char *scan;
1377 + register char *temp;
1378 + register int offset;
1380 + if (p == ®dummy)
1383 + /* Find last node. */
1386 + temp = regnext(scan);
1392 + if (OP(scan) == BACK)
1393 + offset = scan - val;
1395 + offset = val - scan;
1396 + *(scan+1) = (offset>>8)&0377;
1397 + *(scan+2) = offset&0377;
1401 + - regoptail - regtail on operand of first argument; nop if operandless
1404 +regoptail(char *p, char *val)
1406 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1407 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1409 + regtail(OPERAND(p), val);
1413 + * regexec and friends
1417 + * Global work variables for regexec().
1419 +static char *reginput; /* String-input pointer. */
1420 +static char *regbol; /* Beginning of input, for ^ check. */
1421 +static char **regstartp; /* Pointer to startp array. */
1422 +static char **regendp; /* Ditto for endp. */
1427 +STATIC int regtry(regexp *prog, char *string);
1428 +STATIC int regmatch(char *prog);
1429 +STATIC int regrepeat(char *p);
1432 +int regnarrate = 0;
1434 +STATIC char *regprop(char *op);
1438 + - regexec - match a regexp against a string
1441 +regexec(regexp *prog, char *string)
1445 + /* Be paranoid... */
1446 + if (prog == NULL || string == NULL) {
1447 + printk("<3>Regexp: NULL parameter\n");
1451 + /* Check validity of program. */
1452 + if (UCHARAT(prog->program) != MAGIC) {
1453 + printk("<3>Regexp: corrupted program\n");
1457 + /* If there is a "must appear" string, look for it. */
1458 + if (prog->regmust != NULL) {
1460 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1461 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1462 + break; /* Found it. */
1465 + if (s == NULL) /* Not present. */
1469 + /* Mark beginning of line for ^ . */
1472 + /* Simplest case: anchored match need be tried only once. */
1473 + if (prog->reganch)
1474 + return(regtry(prog, string));
1476 + /* Messy cases: unanchored match. */
1478 + if (prog->regstart != '\0')
1479 + /* We know what char it must start with. */
1480 + while ((s = strchr(s, prog->regstart)) != NULL) {
1481 + if (regtry(prog, s))
1486 + /* We don't -- general case. */
1488 + if (regtry(prog, s))
1490 + } while (*s++ != '\0');
1497 + - regtry - try match at specific point
1499 +static int /* 0 failure, 1 success */
1500 +regtry(regexp *prog, char *string)
1503 + register char **sp;
1504 + register char **ep;
1506 + reginput = string;
1507 + regstartp = prog->startp;
1508 + regendp = prog->endp;
1510 + sp = prog->startp;
1512 + for (i = NSUBEXP; i > 0; i--) {
1516 + if (regmatch(prog->program + 1)) {
1517 + prog->startp[0] = string;
1518 + prog->endp[0] = reginput;
1525 + - regmatch - main matching routine
1527 + * Conceptually the strategy is simple: check to see whether the current
1528 + * node matches, call self recursively to see whether the rest matches,
1529 + * and then act accordingly. In practice we make some effort to avoid
1530 + * recursion, in particular by going through "ordinary" nodes (that don't
1531 + * need to know whether the rest of the match failed) by a loop instead of
1534 +static int /* 0 failure, 1 success */
1535 +regmatch(char *prog)
1537 + register char *scan = prog; /* Current node. */
1538 + char *next; /* Next node. */
1541 + if (scan != NULL && regnarrate)
1542 + fprintf(stderr, "%s(\n", regprop(scan));
1544 + while (scan != NULL) {
1547 + fprintf(stderr, "%s...\n", regprop(scan));
1549 + next = regnext(scan);
1551 + switch (OP(scan)) {
1553 + if (reginput != regbol)
1557 + if (*reginput != '\0')
1561 + if (*reginput == '\0')
1567 + register char *opnd;
1569 + opnd = OPERAND(scan);
1570 + /* Inline the first character, for speed. */
1571 + if (*opnd != *reginput)
1573 + len = strlen(opnd);
1574 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1580 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1585 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1602 + register char *save;
1604 + no = OP(scan) - OPEN;
1607 + if (regmatch(next)) {
1609 + * Don't set startp if some later
1610 + * invocation of the same parentheses
1613 + if (regstartp[no] == NULL)
1614 + regstartp[no] = save;
1631 + register char *save;
1633 + no = OP(scan) - CLOSE;
1636 + if (regmatch(next)) {
1638 + * Don't set endp if some later
1639 + * invocation of the same parentheses
1642 + if (regendp[no] == NULL)
1643 + regendp[no] = save;
1650 + register char *save;
1652 + if (OP(next) != BRANCH) /* No choice. */
1653 + next = OPERAND(scan); /* Avoid recursion. */
1657 + if (regmatch(OPERAND(scan)))
1660 + scan = regnext(scan);
1661 + } while (scan != NULL && OP(scan) == BRANCH);
1669 + register char nextch;
1671 + register char *save;
1675 + * Lookahead to avoid useless match attempts
1676 + * when we know what character comes next.
1679 + if (OP(next) == EXACTLY)
1680 + nextch = *OPERAND(next);
1681 + min = (OP(scan) == STAR) ? 0 : 1;
1683 + no = regrepeat(OPERAND(scan));
1684 + while (no >= min) {
1685 + /* If it could work, try it. */
1686 + if (nextch == '\0' || *reginput == nextch)
1687 + if (regmatch(next))
1689 + /* Couldn't or didn't -- back up. */
1691 + reginput = save + no;
1697 + return(1); /* Success! */
1700 + printk("<3>Regexp: memory corruption\n");
1709 + * We get here only if there's trouble -- normally "case END" is
1710 + * the terminating point.
1712 + printk("<3>Regexp: corrupted pointers\n");
1717 + - regrepeat - repeatedly match something simple, report how many
1722 + register int count = 0;
1723 + register char *scan;
1724 + register char *opnd;
1727 + opnd = OPERAND(p);
1730 + count = strlen(scan);
1734 + while (*opnd == *scan) {
1740 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1746 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1751 + default: /* Oh dear. Called inappropriately. */
1752 + printk("<3>Regexp: internal foulup\n");
1753 + count = 0; /* Best compromise. */
1762 + - regnext - dig the "next" pointer out of a node
1767 + register int offset;
1769 + if (p == ®dummy)
1776 + if (OP(p) == BACK)
1784 +STATIC char *regprop();
1787 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1793 + register char op = EXACTLY; /* Arbitrary non-END op. */
1794 + register char *next;
1795 + /* extern char *strchr(); */
1798 + s = r->program + 1;
1799 + while (op != END) { /* While that wasn't END last time... */
1801 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1802 + next = regnext(s);
1803 + if (next == NULL) /* Next ptr. */
1806 + printf("(%d)", (s-r->program)+(next-s));
1808 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1809 + /* Literal string, where present. */
1810 + while (*s != '\0') {
1819 + /* Header fields of interest. */
1820 + if (r->regstart != '\0')
1821 + printf("start `%c' ", r->regstart);
1823 + printf("anchored ");
1824 + if (r->regmust != NULL)
1825 + printf("must have \"%s\"", r->regmust);
1830 + - regprop - printable representation of opcode
1837 + static char buf[BUFLEN];
1881 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1893 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1903 + printk("<3>Regexp: corrupted opcode\n");
1907 + strncat(buf, p, BUFLEN-strlen(buf));
1913 diff -urN linux-2.6.19.old/net/ipv4/netfilter/regexp/regexp.h linux-2.6.19.dev/net/ipv4/netfilter/regexp/regexp.h
1914 --- linux-2.6.19.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1915 +++ linux-2.6.19.dev/net/ipv4/netfilter/regexp/regexp.h 2006-12-14 03:13:37.000000000 +0100
1918 + * Definitions etc. for regexp(3) routines.
1920 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1921 + * not the System V one.
1929 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1930 +which contains a version of this library, says:
1933 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1934 + * will not work properly.
1937 +However, it looks rather like this library is limited to 10. If you think
1938 +otherwise, let us know.
1942 +typedef struct regexp {
1943 + char *startp[NSUBEXP];
1944 + char *endp[NSUBEXP];
1945 + char regstart; /* Internal use only. */
1946 + char reganch; /* Internal use only. */
1947 + char *regmust; /* Internal use only. */
1948 + int regmlen; /* Internal use only. */
1949 + char program[1]; /* Unwarranted chumminess with compiler. */
1952 +regexp * regcomp(char *exp, int *patternsize);
1953 +int regexec(regexp *prog, char *string);
1954 +void regsub(regexp *prog, char *source, char *dest);
1955 +void regerror(char *s);
1958 diff -urN linux-2.6.19.old/net/ipv4/netfilter/regexp/regmagic.h linux-2.6.19.dev/net/ipv4/netfilter/regexp/regmagic.h
1959 --- linux-2.6.19.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1960 +++ linux-2.6.19.dev/net/ipv4/netfilter/regexp/regmagic.h 2006-12-14 03:13:37.000000000 +0100
1963 + * The first byte of the regexp internal "program" is actually this magic
1964 + * number; the start node begins in the second byte.
1967 diff -urN linux-2.6.19.old/net/ipv4/netfilter/regexp/regsub.c linux-2.6.19.dev/net/ipv4/netfilter/regexp/regsub.c
1968 --- linux-2.6.19.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1969 +++ linux-2.6.19.dev/net/ipv4/netfilter/regexp/regsub.c 2006-12-14 03:13:37.000000000 +0100
1973 + * @(#)regsub.c 1.3 of 2 April 86
1975 + * Copyright (c) 1986 by University of Toronto.
1976 + * Written by Henry Spencer. Not derived from licensed software.
1978 + * Permission is granted to anyone to use this software for any
1979 + * purpose on any computer system, and to redistribute it freely,
1980 + * subject to the following restrictions:
1982 + * 1. The author is not responsible for the consequences of use of
1983 + * this software, no matter how awful, even if they arise
1984 + * from defects in it.
1986 + * 2. The origin of this software must not be misrepresented, either
1987 + * by explicit claim or by omission.
1989 + * 3. Altered versions must be plainly marked as such, and must not
1990 + * be misrepresented as being the original software.
1993 + * This code was modified by Ethan Sommer to work within the kernel
1994 + * (it now uses kmalloc etc..)
1997 +#include "regexp.h"
1998 +#include "regmagic.h"
1999 +#include <linux/string.h>
2003 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
2005 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
2009 +//void regerror(char * s)
2011 +// printk("regexp(3): %s", s);
2012 +// /* NOTREACHED */
2017 + - regsub - perform substitutions after a regexp match
2020 +regsub(regexp * prog, char * source, char * dest)
2022 + register char *src;
2023 + register char *dst;
2028 + /* Not necessary and gcc doesn't like it -MLS */
2029 + /*extern char *strncpy();*/
2031 + if (prog == NULL || source == NULL || dest == NULL) {
2032 + regerror("NULL parm to regsub");
2035 + if (UCHARAT(prog->program) != MAGIC) {
2036 + regerror("damaged regexp fed to regsub");
2042 + while ((c = *src++) != '\0') {
2045 + else if (c == '\\' && '0' <= *src && *src <= '9')
2046 + no = *src++ - '0';
2050 + if (no < 0) { /* Ordinary character. */
2051 + if (c == '\\' && (*src == '\\' || *src == '&'))
2054 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2055 + len = prog->endp[no] - prog->startp[no];
2056 + (void) strncpy(dst, prog->startp[no], len);
2058 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2059 + regerror("damaged match string");