1 diff -Nur linux-2.6.17/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.6.17-owrt/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux-2.6.17/include/linux/netfilter_ipv4/ip_conntrack.h 2006-06-18 03:49:35.000000000 +0200
3 +++ linux-2.6.17-owrt/include/linux/netfilter_ipv4/ip_conntrack.h 2006-06-18 12:29:04.000000000 +0200
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 -Nur linux-2.6.17/include/linux/netfilter_ipv4/ipt_layer7.h linux-2.6.17-owrt/include/linux/netfilter_ipv4/ipt_layer7.h
21 --- linux-2.6.17/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
22 +++ linux-2.6.17-owrt/include/linux/netfilter_ipv4/ipt_layer7.h 2006-06-18 12:29:04.000000000 +0200
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 -Nur linux-2.6.17/net/ipv4/netfilter/ip_conntrack_core.c linux-2.6.17-owrt/net/ipv4/netfilter/ip_conntrack_core.c
52 --- linux-2.6.17/net/ipv4/netfilter/ip_conntrack_core.c 2006-06-18 03:49:35.000000000 +0200
53 +++ linux-2.6.17-owrt/net/ipv4/netfilter/ip_conntrack_core.c 2006-06-18 12:29:04.000000000 +0200
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 -Nur linux-2.6.17/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.6.17-owrt/net/ipv4/netfilter/ip_conntrack_standalone.c
69 --- linux-2.6.17/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-06-18 03:49:35.000000000 +0200
70 +++ linux-2.6.17-owrt/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-06-18 12:29:04.000000000 +0200
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 -Nur linux-2.6.17/net/ipv4/netfilter/ipt_layer7.c linux-2.6.17-owrt/net/ipv4/netfilter/ipt_layer7.c
85 --- linux-2.6.17/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
86 +++ linux-2.6.17-owrt/net/ipv4/netfilter/ipt_layer7.c 2006-06-18 12:29:04.000000000 +0200
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, const struct net_device *in,
421 + const struct net_device *out, const void *matchinfo,
422 + int offset, int *hotdrop)
424 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
425 + enum ip_conntrack_info master_ctinfo, ctinfo;
426 + struct ip_conntrack *master_conntrack, *conntrack;
427 + unsigned char *app_data, *tmp_data;
428 + unsigned int pattern_result, appdatalen;
429 + regexp * comppattern;
431 + if(!can_handle(skb)){
432 + DPRINTK("layer7: This is some protocol I can't handle.\n");
433 + return info->invert;
436 + /* Treat parent & all its children together as one connection, except
437 + for the purpose of setting conntrack->layer7.app_proto in the actual
438 + connection. This makes /proc/net/ip_conntrack more satisfying. */
439 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
440 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
441 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
442 + return info->invert;
445 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
446 + while (master_ct(master_conntrack) != NULL)
447 + master_conntrack = master_ct(master_conntrack);
449 + /* if we've classified it or seen too many packets */
450 + if(!info->pkt && (TOTAL_PACKETS > num_packets ||
451 + master_conntrack->layer7.app_proto)) {
453 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
455 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
456 + rules. I'm not sure that using cb for this purpose is correct, although
457 + it says "put your private variables there". But it doesn't look like it
458 + is being used for anything else in the skbs that make it here. How can
459 + I write to cb without making the compiler angry? */
460 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
462 + return (pattern_result ^ info->invert);
465 + if(skb_is_nonlinear(skb)){
466 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
467 + if (net_ratelimit())
468 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
469 + return info->invert;
473 + /* now that the skb is linearized, it's safe to set these. */
474 + app_data = skb->data + app_data_offset(skb);
475 + appdatalen = skb->tail - app_data;
477 + spin_lock_bh(&list_lock);
478 + /* the return value gets checked later, when we're ready to use it */
479 + comppattern = compile_and_cache(info->pattern, info->protocol);
480 + spin_unlock_bh(&list_lock);
483 + tmp_data = kmalloc(maxdatalen, GFP_ATOMIC);
485 + if (net_ratelimit())
486 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
487 + return info->invert;
490 + tmp_data[0] = '\0';
491 + add_datastr(tmp_data, 0, app_data, appdatalen);
492 + pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
496 + return (pattern_result ^ info->invert);
499 + /* On the first packet of a connection, allocate space for app data */
500 + write_lock(&ct_lock);
501 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
502 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
503 + if(!master_conntrack->layer7.app_data){
504 + if (net_ratelimit())
505 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
506 + write_unlock(&ct_lock);
507 + return info->invert;
510 + master_conntrack->layer7.app_data[0] = '\0';
512 + write_unlock(&ct_lock);
514 + /* Can be here, but unallocated, if numpackets is increased near
515 + the beginning of a connection */
516 + if(master_conntrack->layer7.app_data == NULL)
517 + return (info->invert); /* unmatched */
521 + write_lock(&ct_lock);
522 + newbytes = add_data(master_conntrack, app_data, appdatalen);
523 + write_unlock(&ct_lock);
525 + if(newbytes == 0) { /* didn't add any data */
527 + /* Didn't match before, not going to match now */
528 + return info->invert;
532 + /* If looking for "unknown", then never match. "Unknown" means that
533 + we've given up; we're still trying with these packets. */
534 + if(!strcmp(info->protocol, "unknown")) {
535 + pattern_result = 0;
536 + /* If the regexp failed to compile, don't bother running it */
537 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
538 + DPRINTK("layer7: matched %s\n", info->protocol);
539 + pattern_result = 1;
540 + } else pattern_result = 0;
542 + if(pattern_result) {
543 + write_lock(&ct_lock);
544 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
545 + if(!master_conntrack->layer7.app_proto){
546 + if (net_ratelimit())
547 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
548 + write_unlock(&ct_lock);
549 + return (pattern_result ^ info->invert);
551 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
552 + write_unlock(&ct_lock);
555 + /* mark the packet seen */
558 + return (pattern_result ^ info->invert);
561 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
562 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
564 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
569 +static struct ipt_match layer7_match = {
572 + .checkentry = &checkentry,
576 +/* taken from drivers/video/modedb.c */
577 +static int my_atoi(const char *s)
584 + val = 10*val+(*s-'0');
592 +/* write out num_packets to userland. */
593 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
594 + int* eof, void * data)
596 + if(num_packets > 99 && net_ratelimit())
597 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
599 + page[0] = num_packets/10 + '0';
600 + page[1] = num_packets%10 + '0';
609 +/* Read in num_packets from userland */
610 +static int layer7_write_proc(struct file* file, const char* buffer,
611 + unsigned long count, void *data)
613 + char * foo = kmalloc(count, GFP_ATOMIC);
616 + if (net_ratelimit())
617 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
621 + if(copy_from_user(foo, buffer, count)) {
626 + num_packets = my_atoi(foo);
629 + /* This has an arbitrary limit to make the math easier. I'm lazy.
630 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
631 + if(num_packets > 99) {
632 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
634 + } else if(num_packets < 1) {
635 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
642 +/* register the proc file */
643 +static void layer7_init_proc(void)
645 + struct proc_dir_entry* entry;
646 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
647 + entry->read_proc = layer7_read_proc;
648 + entry->write_proc = layer7_write_proc;
651 +static void layer7_cleanup_proc(void)
653 + remove_proc_entry("layer7_numpackets", proc_net);
656 +static int __init init(void)
658 + layer7_init_proc();
659 + if(maxdatalen < 1) {
660 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
663 + /* This is not a hard limit. It's just here to prevent people from
664 + bringing their slow machines to a grinding halt. */
665 + else if(maxdatalen > 65536) {
666 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
667 + maxdatalen = 65536;
669 + return ipt_register_match(&layer7_match);
672 +static void __exit fini(void)
674 + layer7_cleanup_proc();
675 + ipt_unregister_match(&layer7_match);
680 diff -Nur linux-2.6.17/net/ipv4/netfilter/Kconfig linux-2.6.17-owrt/net/ipv4/netfilter/Kconfig
681 --- linux-2.6.17/net/ipv4/netfilter/Kconfig 2006-06-18 03:49:35.000000000 +0200
682 +++ linux-2.6.17-owrt/net/ipv4/netfilter/Kconfig 2006-06-18 12:32:58.000000000 +0200
684 destination IP' or `500pps from any given source IP' with a single
687 +config IP_NF_MATCH_LAYER7
688 + tristate "Layer 7 match support (EXPERIMENTAL)"
689 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
691 + Say Y if you want to be able to classify connections (and their
692 + packets) based on regular expression matching of their application
693 + layer data. This is one way to classify applications such as
694 + peer-to-peer filesharing systems that do not always use the same
697 + To compile it as a module, choose M here. If unsure, say N.
699 +config IP_NF_MATCH_LAYER7_DEBUG
700 + bool "Layer 7 debugging output"
701 + depends on IP_NF_MATCH_LAYER7
703 + Say Y to get lots of debugging output.
705 # `filter', generic and specific targets
707 tristate "Packet filtering"
708 diff -Nur linux-2.6.17/net/ipv4/netfilter/Makefile linux-2.6.17-owrt/net/ipv4/netfilter/Makefile
709 --- linux-2.6.17/net/ipv4/netfilter/Makefile 2006-06-18 03:49:35.000000000 +0200
710 +++ linux-2.6.17-owrt/net/ipv4/netfilter/Makefile 2006-06-18 12:29:04.000000000 +0200
712 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
713 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
715 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
718 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
719 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
720 diff -Nur linux-2.6.17/net/ipv4/netfilter/regexp/regexp.c linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regexp.c
721 --- linux-2.6.17/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
722 +++ linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regexp.c 2006-06-18 12:29:04.000000000 +0200
725 + * regcomp and regexec -- regsub and regerror are elsewhere
726 + * @(#)regexp.c 1.3 of 18 April 87
728 + * Copyright (c) 1986 by University of Toronto.
729 + * Written by Henry Spencer. Not derived from licensed software.
731 + * Permission is granted to anyone to use this software for any
732 + * purpose on any computer system, and to redistribute it freely,
733 + * subject to the following restrictions:
735 + * 1. The author is not responsible for the consequences of use of
736 + * this software, no matter how awful, even if they arise
737 + * from defects in it.
739 + * 2. The origin of this software must not be misrepresented, either
740 + * by explicit claim or by omission.
742 + * 3. Altered versions must be plainly marked as such, and must not
743 + * be misrepresented as being the original software.
745 + * Beware that some of this code is subtly aware of the way operator
746 + * precedence is structured in regular expressions. Serious changes in
747 + * regular-expression syntax might require a total rethink.
749 + * This code was modified by Ethan Sommer to work within the kernel
750 + * (it now uses kmalloc etc..)
752 + * Modified slightly by Matthew Strait to use more modern C.
756 +#include "regmagic.h"
758 +/* added by ethan and matt. Lets it work in both kernel and user space.
759 +(So iptables can use it, for instance.) Yea, it goes both ways... */
761 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
763 + #define printk(format,args...) printf(format,##args)
766 +void regerror(char * s)
768 + printk("<3>Regexp: %s\n", s);
773 + * The "internal use only" fields in regexp.h are present to pass info from
774 + * compile to execute that permits the execute phase to run lots faster on
775 + * simple cases. They are:
777 + * regstart char that must begin a match; '\0' if none obvious
778 + * reganch is the match anchored (at beginning-of-line only)?
779 + * regmust string (pointer into program) that match must include, or NULL
780 + * regmlen length of regmust string
782 + * Regstart and reganch permit very fast decisions on suitable starting points
783 + * for a match, cutting down the work a lot. Regmust permits fast rejection
784 + * of lines that cannot possibly match. The regmust tests are costly enough
785 + * that regcomp() supplies a regmust only if the r.e. contains something
786 + * potentially expensive (at present, the only such thing detected is * or +
787 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
788 + * supplied because the test in regexec() needs it and regcomp() is computing
793 + * Structure for regexp "program". This is essentially a linear encoding
794 + * of a nondeterministic finite-state machine (aka syntax charts or
795 + * "railroad normal form" in parsing technology). Each node is an opcode
796 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
797 + * all nodes except BRANCH implement concatenation; a "next" pointer with
798 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
799 + * have one of the subtle syntax dependencies: an individual BRANCH (as
800 + * opposed to a collection of them) is never concatenated with anything
801 + * because of operator precedence.) The operand of some types of node is
802 + * a literal string; for others, it is a node leading into a sub-FSM. In
803 + * particular, the operand of a BRANCH node is the first node of the branch.
804 + * (NB this is *not* a tree structure: the tail of the branch connects
805 + * to the thing following the set of BRANCHes.) The opcodes are:
808 +/* definition number opnd? meaning */
809 +#define END 0 /* no End of program. */
810 +#define BOL 1 /* no Match "" at beginning of line. */
811 +#define EOL 2 /* no Match "" at end of line. */
812 +#define ANY 3 /* no Match any one character. */
813 +#define ANYOF 4 /* str Match any character in this string. */
814 +#define ANYBUT 5 /* str Match any character not in this string. */
815 +#define BRANCH 6 /* node Match this alternative, or the next... */
816 +#define BACK 7 /* no Match "", "next" ptr points backward. */
817 +#define EXACTLY 8 /* str Match this string. */
818 +#define NOTHING 9 /* no Match empty string. */
819 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
820 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
821 +#define OPEN 20 /* no Mark this point in input as start of #n. */
822 + /* OPEN+1 is number 1, etc. */
823 +#define CLOSE 30 /* no Analogous to OPEN. */
828 + * BRANCH The set of branches constituting a single choice are hooked
829 + * together with their "next" pointers, since precedence prevents
830 + * anything being concatenated to any individual branch. The
831 + * "next" pointer of the last BRANCH in a choice points to the
832 + * thing following the whole choice. This is also where the
833 + * final "next" pointer of each individual branch points; each
834 + * branch starts with the operand node of a BRANCH node.
836 + * BACK Normal "next" pointers all implicitly point forward; BACK
837 + * exists to make loop structures possible.
839 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
840 + * BRANCH structures using BACK. Simple cases (one character
841 + * per match) are implemented with STAR and PLUS for speed
842 + * and to minimize recursive plunges.
844 + * OPEN,CLOSE ...are numbered at compile time.
848 + * A node is one char of opcode followed by two chars of "next" pointer.
849 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
850 + * value is a positive offset from the opcode of the node containing it.
851 + * An operand, if any, simply follows the node. (Note that much of the
852 + * code generation knows about this implicit relationship.)
854 + * Using two bytes for the "next" pointer is vast overkill for most things,
855 + * but allows patterns to get big without disasters.
857 +#define OP(p) (*(p))
858 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
859 +#define OPERAND(p) ((p) + 3)
862 + * See regmagic.h for one further detail of program structure.
867 + * Utility definitions.
870 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
872 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
875 +#define FAIL(m) { regerror(m); return(NULL); }
876 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
877 +#define META "^$.[()|?+*\\"
880 + * Flags to be passed up and down.
882 +#define HASWIDTH 01 /* Known never to match null string. */
883 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
884 +#define SPSTART 04 /* Starts with * or +. */
885 +#define WORST 0 /* Worst case. */
888 + * Global work variables for regcomp().
890 +static char *regparse; /* Input-scan pointer. */
891 +static int regnpar; /* () count. */
892 +static char regdummy;
893 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
894 +static long regsize; /* Code size. */
897 + * Forward declarations for regcomp()'s friends.
900 +#define STATIC static
902 +STATIC char *reg(int paren,int *flagp);
903 +STATIC char *regbranch(int *flagp);
904 +STATIC char *regpiece(int *flagp);
905 +STATIC char *regatom(int *flagp);
906 +STATIC char *regnode(char op);
907 +STATIC char *regnext(char *p);
908 +STATIC void regc(char b);
909 +STATIC void reginsert(char op, char *opnd);
910 +STATIC void regtail(char *p, char *val);
911 +STATIC void regoptail(char *p, char *val);
914 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
921 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
922 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
923 + if (*scan1 == *scan2++)
931 + - regcomp - compile a regular expression into internal code
933 + * We can't allocate space until we know how big the compiled form will be,
934 + * but we can't compile it (and thus know how big it is) until we've got a
935 + * place to put the code. So we cheat: we compile it twice, once with code
936 + * generation turned off and size counting turned on, and once "for real".
937 + * This also means that we don't allocate space until we are sure that the
938 + * thing really will compile successfully, and we never have to move the
939 + * code and thus invalidate pointers into it. (Note that it has to be in
940 + * one piece because free() must be able to free it all.)
942 + * Beware that the optimization-preparation code in here knows about some
943 + * of the structure of the compiled regexp.
946 +regcomp(char *exp,int *patternsize)
948 + register regexp *r;
949 + register char *scan;
950 + register char *longest;
953 + /* commented out by ethan
954 + extern char *malloc();
958 + FAIL("NULL argument");
960 + /* First pass: determine size, legality. */
964 + regcode = ®dummy;
966 + if (reg(0, &flags) == NULL)
969 + /* Small enough for pointer-storage convention? */
970 + if (regsize >= 32767L) /* Probably could be 65535L. */
971 + FAIL("regexp too big");
973 + /* Allocate space. */
974 + *patternsize=sizeof(regexp) + (unsigned)regsize;
975 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
977 + FAIL("out of space");
979 + /* Second pass: emit code. */
982 + regcode = r->program;
984 + if (reg(0, &flags) == NULL)
987 + /* Dig out information for optimizations. */
988 + r->regstart = '\0'; /* Worst-case defaults. */
992 + scan = r->program+1; /* First BRANCH. */
993 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
994 + scan = OPERAND(scan);
996 + /* Starting-point info. */
997 + if (OP(scan) == EXACTLY)
998 + r->regstart = *OPERAND(scan);
999 + else if (OP(scan) == BOL)
1003 + * If there's something expensive in the r.e., find the
1004 + * longest literal string that must appear and make it the
1005 + * regmust. Resolve ties in favor of later strings, since
1006 + * the regstart check works with the beginning of the r.e.
1007 + * and avoiding duplication strengthens checking. Not a
1008 + * strong reason, but sufficient in the absence of others.
1010 + if (flags&SPSTART) {
1013 + for (; scan != NULL; scan = regnext(scan))
1014 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1015 + longest = OPERAND(scan);
1016 + len = strlen(OPERAND(scan));
1018 + r->regmust = longest;
1027 + - reg - regular expression, i.e. main body or parenthesized thing
1029 + * Caller must absorb opening parenthesis.
1031 + * Combining parenthesis handling with the base level of regular expression
1032 + * is a trifle forced, but the need to tie the tails of the branches to what
1033 + * follows makes it hard to avoid.
1036 +reg(int paren, int *flagp /* Parenthesized? */ )
1038 + register char *ret;
1039 + register char *br;
1040 + register char *ender;
1041 + register int parno = 0; /* 0 makes gcc happy */
1044 + *flagp = HASWIDTH; /* Tentatively. */
1046 + /* Make an OPEN node, if parenthesized. */
1048 + if (regnpar >= NSUBEXP)
1049 + FAIL("too many ()");
1052 + ret = regnode(OPEN+parno);
1056 + /* Pick up the branches, linking them together. */
1057 + br = regbranch(&flags);
1061 + regtail(ret, br); /* OPEN -> first. */
1064 + if (!(flags&HASWIDTH))
1065 + *flagp &= ~HASWIDTH;
1066 + *flagp |= flags&SPSTART;
1067 + while (*regparse == '|') {
1069 + br = regbranch(&flags);
1072 + regtail(ret, br); /* BRANCH -> BRANCH. */
1073 + if (!(flags&HASWIDTH))
1074 + *flagp &= ~HASWIDTH;
1075 + *flagp |= flags&SPSTART;
1078 + /* Make a closing node, and hook it on the end. */
1079 + ender = regnode((paren) ? CLOSE+parno : END);
1080 + regtail(ret, ender);
1082 + /* Hook the tails of the branches to the closing node. */
1083 + for (br = ret; br != NULL; br = regnext(br))
1084 + regoptail(br, ender);
1086 + /* Check for proper termination. */
1087 + if (paren && *regparse++ != ')') {
1088 + FAIL("unmatched ()");
1089 + } else if (!paren && *regparse != '\0') {
1090 + if (*regparse == ')') {
1091 + FAIL("unmatched ()");
1093 + FAIL("junk on end"); /* "Can't happen". */
1101 + - regbranch - one alternative of an | operator
1103 + * Implements the concatenation operator.
1106 +regbranch(int *flagp)
1108 + register char *ret;
1109 + register char *chain;
1110 + register char *latest;
1113 + *flagp = WORST; /* Tentatively. */
1115 + ret = regnode(BRANCH);
1117 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1118 + latest = regpiece(&flags);
1119 + if (latest == NULL)
1121 + *flagp |= flags&HASWIDTH;
1122 + if (chain == NULL) /* First piece. */
1123 + *flagp |= flags&SPSTART;
1125 + regtail(chain, latest);
1128 + if (chain == NULL) /* Loop ran zero times. */
1129 + (void) regnode(NOTHING);
1135 + - regpiece - something followed by possible [*+?]
1137 + * Note that the branching code sequences used for ? and the general cases
1138 + * of * and + are somewhat optimized: they use the same NOTHING node as
1139 + * both the endmarker for their branch list and the body of the last branch.
1140 + * It might seem that this node could be dispensed with entirely, but the
1141 + * endmarker role is not redundant.
1144 +regpiece(int *flagp)
1146 + register char *ret;
1148 + register char *next;
1151 + ret = regatom(&flags);
1156 + if (!ISMULT(op)) {
1161 + if (!(flags&HASWIDTH) && op != '?')
1162 + FAIL("*+ operand could be empty");
1163 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1165 + if (op == '*' && (flags&SIMPLE))
1166 + reginsert(STAR, ret);
1167 + else if (op == '*') {
1168 + /* Emit x* as (x&|), where & means "self". */
1169 + reginsert(BRANCH, ret); /* Either x */
1170 + regoptail(ret, regnode(BACK)); /* and loop */
1171 + regoptail(ret, ret); /* back */
1172 + regtail(ret, regnode(BRANCH)); /* or */
1173 + regtail(ret, regnode(NOTHING)); /* null. */
1174 + } else if (op == '+' && (flags&SIMPLE))
1175 + reginsert(PLUS, ret);
1176 + else if (op == '+') {
1177 + /* Emit x+ as x(&|), where & means "self". */
1178 + next = regnode(BRANCH); /* Either */
1179 + regtail(ret, next);
1180 + regtail(regnode(BACK), ret); /* loop back */
1181 + regtail(next, regnode(BRANCH)); /* or */
1182 + regtail(ret, regnode(NOTHING)); /* null. */
1183 + } else if (op == '?') {
1184 + /* Emit x? as (x|) */
1185 + reginsert(BRANCH, ret); /* Either x */
1186 + regtail(ret, regnode(BRANCH)); /* or */
1187 + next = regnode(NOTHING); /* null. */
1188 + regtail(ret, next);
1189 + regoptail(ret, next);
1192 + if (ISMULT(*regparse))
1193 + FAIL("nested *?+");
1199 + - regatom - the lowest level
1201 + * Optimization: gobbles an entire sequence of ordinary characters so that
1202 + * it can turn them into a single node, which is smaller to store and
1203 + * faster to run. Backslashed characters are exceptions, each becoming a
1204 + * separate node; the code is simpler that way and it's not worth fixing.
1207 +regatom(int *flagp)
1209 + register char *ret;
1212 + *flagp = WORST; /* Tentatively. */
1214 + switch (*regparse++) {
1216 + ret = regnode(BOL);
1219 + ret = regnode(EOL);
1222 + ret = regnode(ANY);
1223 + *flagp |= HASWIDTH|SIMPLE;
1226 + register int class;
1227 + register int classend;
1229 + if (*regparse == '^') { /* Complement of range. */
1230 + ret = regnode(ANYBUT);
1233 + ret = regnode(ANYOF);
1234 + if (*regparse == ']' || *regparse == '-')
1235 + regc(*regparse++);
1236 + while (*regparse != '\0' && *regparse != ']') {
1237 + if (*regparse == '-') {
1239 + if (*regparse == ']' || *regparse == '\0')
1242 + class = UCHARAT(regparse-2)+1;
1243 + classend = UCHARAT(regparse);
1244 + if (class > classend+1)
1245 + FAIL("invalid [] range");
1246 + for (; class <= classend; class++)
1251 + regc(*regparse++);
1254 + if (*regparse != ']')
1255 + FAIL("unmatched []");
1257 + *flagp |= HASWIDTH|SIMPLE;
1261 + ret = reg(1, &flags);
1264 + *flagp |= flags&(HASWIDTH|SPSTART);
1269 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1274 + FAIL("?+* follows nothing");
1277 + if (*regparse == '\0')
1278 + FAIL("trailing \\");
1279 + ret = regnode(EXACTLY);
1280 + regc(*regparse++);
1282 + *flagp |= HASWIDTH|SIMPLE;
1286 + register char ender;
1289 + len = my_strcspn((const char *)regparse, (const char *)META);
1291 + FAIL("internal disaster");
1292 + ender = *(regparse+len);
1293 + if (len > 1 && ISMULT(ender))
1294 + len--; /* Back off clear of ?+* operand. */
1295 + *flagp |= HASWIDTH;
1298 + ret = regnode(EXACTLY);
1300 + regc(*regparse++);
1312 + - regnode - emit a node
1314 +static char * /* Location. */
1317 + register char *ret;
1318 + register char *ptr;
1321 + if (ret == ®dummy) {
1328 + *ptr++ = '\0'; /* Null "next" pointer. */
1336 + - regc - emit (if appropriate) a byte of code
1341 + if (regcode != ®dummy)
1348 + - reginsert - insert an operator in front of already-emitted operand
1350 + * Means relocating the operand.
1353 +reginsert(char op, char* opnd)
1355 + register char *src;
1356 + register char *dst;
1357 + register char *place;
1359 + if (regcode == ®dummy) {
1367 + while (src > opnd)
1370 + place = opnd; /* Op node, where operand used to be. */
1377 + - regtail - set the next-pointer at the end of a node chain
1380 +regtail(char *p, char *val)
1382 + register char *scan;
1383 + register char *temp;
1384 + register int offset;
1386 + if (p == ®dummy)
1389 + /* Find last node. */
1392 + temp = regnext(scan);
1398 + if (OP(scan) == BACK)
1399 + offset = scan - val;
1401 + offset = val - scan;
1402 + *(scan+1) = (offset>>8)&0377;
1403 + *(scan+2) = offset&0377;
1407 + - regoptail - regtail on operand of first argument; nop if operandless
1410 +regoptail(char *p, char *val)
1412 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1413 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1415 + regtail(OPERAND(p), val);
1419 + * regexec and friends
1423 + * Global work variables for regexec().
1425 +static char *reginput; /* String-input pointer. */
1426 +static char *regbol; /* Beginning of input, for ^ check. */
1427 +static char **regstartp; /* Pointer to startp array. */
1428 +static char **regendp; /* Ditto for endp. */
1433 +STATIC int regtry(regexp *prog, char *string);
1434 +STATIC int regmatch(char *prog);
1435 +STATIC int regrepeat(char *p);
1438 +int regnarrate = 0;
1440 +STATIC char *regprop(char *op);
1444 + - regexec - match a regexp against a string
1447 +regexec(regexp *prog, char *string)
1451 + /* Be paranoid... */
1452 + if (prog == NULL || string == NULL) {
1453 + printk("<3>Regexp: NULL parameter\n");
1457 + /* Check validity of program. */
1458 + if (UCHARAT(prog->program) != MAGIC) {
1459 + printk("<3>Regexp: corrupted program\n");
1463 + /* If there is a "must appear" string, look for it. */
1464 + if (prog->regmust != NULL) {
1466 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1467 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1468 + break; /* Found it. */
1471 + if (s == NULL) /* Not present. */
1475 + /* Mark beginning of line for ^ . */
1478 + /* Simplest case: anchored match need be tried only once. */
1479 + if (prog->reganch)
1480 + return(regtry(prog, string));
1482 + /* Messy cases: unanchored match. */
1484 + if (prog->regstart != '\0')
1485 + /* We know what char it must start with. */
1486 + while ((s = strchr(s, prog->regstart)) != NULL) {
1487 + if (regtry(prog, s))
1492 + /* We don't -- general case. */
1494 + if (regtry(prog, s))
1496 + } while (*s++ != '\0');
1503 + - regtry - try match at specific point
1505 +static int /* 0 failure, 1 success */
1506 +regtry(regexp *prog, char *string)
1509 + register char **sp;
1510 + register char **ep;
1512 + reginput = string;
1513 + regstartp = prog->startp;
1514 + regendp = prog->endp;
1516 + sp = prog->startp;
1518 + for (i = NSUBEXP; i > 0; i--) {
1522 + if (regmatch(prog->program + 1)) {
1523 + prog->startp[0] = string;
1524 + prog->endp[0] = reginput;
1531 + - regmatch - main matching routine
1533 + * Conceptually the strategy is simple: check to see whether the current
1534 + * node matches, call self recursively to see whether the rest matches,
1535 + * and then act accordingly. In practice we make some effort to avoid
1536 + * recursion, in particular by going through "ordinary" nodes (that don't
1537 + * need to know whether the rest of the match failed) by a loop instead of
1540 +static int /* 0 failure, 1 success */
1541 +regmatch(char *prog)
1543 + register char *scan = prog; /* Current node. */
1544 + char *next; /* Next node. */
1547 + if (scan != NULL && regnarrate)
1548 + fprintf(stderr, "%s(\n", regprop(scan));
1550 + while (scan != NULL) {
1553 + fprintf(stderr, "%s...\n", regprop(scan));
1555 + next = regnext(scan);
1557 + switch (OP(scan)) {
1559 + if (reginput != regbol)
1563 + if (*reginput != '\0')
1567 + if (*reginput == '\0')
1573 + register char *opnd;
1575 + opnd = OPERAND(scan);
1576 + /* Inline the first character, for speed. */
1577 + if (*opnd != *reginput)
1579 + len = strlen(opnd);
1580 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1586 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1591 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1608 + register char *save;
1610 + no = OP(scan) - OPEN;
1613 + if (regmatch(next)) {
1615 + * Don't set startp if some later
1616 + * invocation of the same parentheses
1619 + if (regstartp[no] == NULL)
1620 + regstartp[no] = save;
1637 + register char *save;
1639 + no = OP(scan) - CLOSE;
1642 + if (regmatch(next)) {
1644 + * Don't set endp if some later
1645 + * invocation of the same parentheses
1648 + if (regendp[no] == NULL)
1649 + regendp[no] = save;
1656 + register char *save;
1658 + if (OP(next) != BRANCH) /* No choice. */
1659 + next = OPERAND(scan); /* Avoid recursion. */
1663 + if (regmatch(OPERAND(scan)))
1666 + scan = regnext(scan);
1667 + } while (scan != NULL && OP(scan) == BRANCH);
1675 + register char nextch;
1677 + register char *save;
1681 + * Lookahead to avoid useless match attempts
1682 + * when we know what character comes next.
1685 + if (OP(next) == EXACTLY)
1686 + nextch = *OPERAND(next);
1687 + min = (OP(scan) == STAR) ? 0 : 1;
1689 + no = regrepeat(OPERAND(scan));
1690 + while (no >= min) {
1691 + /* If it could work, try it. */
1692 + if (nextch == '\0' || *reginput == nextch)
1693 + if (regmatch(next))
1695 + /* Couldn't or didn't -- back up. */
1697 + reginput = save + no;
1703 + return(1); /* Success! */
1706 + printk("<3>Regexp: memory corruption\n");
1715 + * We get here only if there's trouble -- normally "case END" is
1716 + * the terminating point.
1718 + printk("<3>Regexp: corrupted pointers\n");
1723 + - regrepeat - repeatedly match something simple, report how many
1728 + register int count = 0;
1729 + register char *scan;
1730 + register char *opnd;
1733 + opnd = OPERAND(p);
1736 + count = strlen(scan);
1740 + while (*opnd == *scan) {
1746 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1752 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1757 + default: /* Oh dear. Called inappropriately. */
1758 + printk("<3>Regexp: internal foulup\n");
1759 + count = 0; /* Best compromise. */
1768 + - regnext - dig the "next" pointer out of a node
1773 + register int offset;
1775 + if (p == ®dummy)
1782 + if (OP(p) == BACK)
1790 +STATIC char *regprop();
1793 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1799 + register char op = EXACTLY; /* Arbitrary non-END op. */
1800 + register char *next;
1801 + /* extern char *strchr(); */
1804 + s = r->program + 1;
1805 + while (op != END) { /* While that wasn't END last time... */
1807 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1808 + next = regnext(s);
1809 + if (next == NULL) /* Next ptr. */
1812 + printf("(%d)", (s-r->program)+(next-s));
1814 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1815 + /* Literal string, where present. */
1816 + while (*s != '\0') {
1825 + /* Header fields of interest. */
1826 + if (r->regstart != '\0')
1827 + printf("start `%c' ", r->regstart);
1829 + printf("anchored ");
1830 + if (r->regmust != NULL)
1831 + printf("must have \"%s\"", r->regmust);
1836 + - regprop - printable representation of opcode
1843 + static char buf[BUFLEN];
1887 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1899 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1909 + printk("<3>Regexp: corrupted opcode\n");
1913 + strncat(buf, p, BUFLEN-strlen(buf));
1919 diff -Nur linux-2.6.17/net/ipv4/netfilter/regexp/regexp.h linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regexp.h
1920 --- linux-2.6.17/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1921 +++ linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regexp.h 2006-06-18 12:29:04.000000000 +0200
1924 + * Definitions etc. for regexp(3) routines.
1926 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1927 + * not the System V one.
1935 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1936 +which contains a version of this library, says:
1939 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1940 + * will not work properly.
1943 +However, it looks rather like this library is limited to 10. If you think
1944 +otherwise, let us know.
1948 +typedef struct regexp {
1949 + char *startp[NSUBEXP];
1950 + char *endp[NSUBEXP];
1951 + char regstart; /* Internal use only. */
1952 + char reganch; /* Internal use only. */
1953 + char *regmust; /* Internal use only. */
1954 + int regmlen; /* Internal use only. */
1955 + char program[1]; /* Unwarranted chumminess with compiler. */
1958 +regexp * regcomp(char *exp, int *patternsize);
1959 +int regexec(regexp *prog, char *string);
1960 +void regsub(regexp *prog, char *source, char *dest);
1961 +void regerror(char *s);
1964 diff -Nur linux-2.6.17/net/ipv4/netfilter/regexp/regmagic.h linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regmagic.h
1965 --- linux-2.6.17/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1966 +++ linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regmagic.h 2006-06-18 12:29:04.000000000 +0200
1969 + * The first byte of the regexp internal "program" is actually this magic
1970 + * number; the start node begins in the second byte.
1973 diff -Nur linux-2.6.17/net/ipv4/netfilter/regexp/regsub.c linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regsub.c
1974 --- linux-2.6.17/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1975 +++ linux-2.6.17-owrt/net/ipv4/netfilter/regexp/regsub.c 2006-06-18 12:29:04.000000000 +0200
1979 + * @(#)regsub.c 1.3 of 2 April 86
1981 + * Copyright (c) 1986 by University of Toronto.
1982 + * Written by Henry Spencer. Not derived from licensed software.
1984 + * Permission is granted to anyone to use this software for any
1985 + * purpose on any computer system, and to redistribute it freely,
1986 + * subject to the following restrictions:
1988 + * 1. The author is not responsible for the consequences of use of
1989 + * this software, no matter how awful, even if they arise
1990 + * from defects in it.
1992 + * 2. The origin of this software must not be misrepresented, either
1993 + * by explicit claim or by omission.
1995 + * 3. Altered versions must be plainly marked as such, and must not
1996 + * be misrepresented as being the original software.
1999 + * This code was modified by Ethan Sommer to work within the kernel
2000 + * (it now uses kmalloc etc..)
2003 +#include "regexp.h"
2004 +#include "regmagic.h"
2005 +#include <linux/string.h>
2009 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
2011 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
2015 +//void regerror(char * s)
2017 +// printk("regexp(3): %s", s);
2018 +// /* NOTREACHED */
2023 + - regsub - perform substitutions after a regexp match
2026 +regsub(regexp * prog, char * source, char * dest)
2028 + register char *src;
2029 + register char *dst;
2034 + /* Not necessary and gcc doesn't like it -MLS */
2035 + /*extern char *strncpy();*/
2037 + if (prog == NULL || source == NULL || dest == NULL) {
2038 + regerror("NULL parm to regsub");
2041 + if (UCHARAT(prog->program) != MAGIC) {
2042 + regerror("damaged regexp fed to regsub");
2048 + while ((c = *src++) != '\0') {
2051 + else if (c == '\\' && '0' <= *src && *src <= '9')
2052 + no = *src++ - '0';
2056 + if (no < 0) { /* Ordinary character. */
2057 + if (c == '\\' && (*src == '\\' || *src == '&'))
2060 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2061 + len = prog->endp[no] - prog->startp[no];
2062 + (void) strncpy(dst, prog->startp[no], len);
2064 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2065 + regerror("damaged match string");