1 --- linux-2.6.14/include/linux/netfilter_ipv4/ip_conntrack.h 2005-10-27 19:02:08.000000000 -0500
2 +++ linux-2.6.14-layer7/include/linux/netfilter_ipv4/ip_conntrack.h 2005-11-12 17:31:34.000000000 -0600
3 @@ -253,6 +253,15 @@ struct ip_conntrack
4 /* Traversed often, so hopefully in different cacheline to top */
5 /* These are my tuples; original and reply */
6 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
8 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
10 + char * app_proto; /* e.g. "http". NULL before decision. "unknown" after decision if no match */
11 + char * app_data; /* application layer data so far. NULL after match decision */
12 + unsigned int app_data_len;
18 struct ip_conntrack_expect
19 --- linux-2.6.14/include/linux/netfilter_ipv4/ipt_layer7.h 1969-12-31 18:00:00.000000000 -0600
20 +++ linux-2.6.14-layer7/include/linux/netfilter_ipv4/ipt_layer7.h 2005-11-12 17:31:34.000000000 -0600
23 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
24 + http://l7-filter.sf.net
26 + This program is free software; you can redistribute it and/or
27 + modify it under the terms of the GNU General Public License
28 + as published by the Free Software Foundation; either version
29 + 2 of the License, or (at your option) any later version.
30 + http://www.gnu.org/licenses/gpl.txt
33 +#ifndef _IPT_LAYER7_H
34 +#define _IPT_LAYER7_H
36 +#define MAX_PATTERN_LEN 8192
37 +#define MAX_PROTOCOL_LEN 256
39 +typedef char *(*proc_ipt_search) (char *, char, char *);
41 +struct ipt_layer7_info {
42 + char protocol[MAX_PROTOCOL_LEN];
44 + char pattern[MAX_PATTERN_LEN];
47 +#endif /* _IPT_LAYER7_H */
48 --- linux-2.6.14/net/ipv4/netfilter/Kconfig 2005-10-27 19:02:08.000000000 -0500
49 +++ linux-2.6.14-layer7/net/ipv4/netfilter/Kconfig 2005-11-12 17:31:34.000000000 -0600
50 @@ -205,6 +205,24 @@ config IP_NF_MATCH_MAC
52 To compile it as a module, choose M here. If unsure, say N.
54 +config IP_NF_MATCH_LAYER7
55 + tristate "Layer 7 match support (EXPERIMENTAL)"
56 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
58 + Say Y if you want to be able to classify connections (and their
59 + packets) based on regular expression matching of their application
60 + layer data. This is one way to classify applications such as
61 + peer-to-peer filesharing systems that do not always use the same
64 + To compile it as a module, choose M here. If unsure, say N.
66 +config IP_NF_MATCH_LAYER7_DEBUG
67 + bool "Layer 7 debugging output"
68 + depends on IP_NF_MATCH_LAYER7
70 + Say Y to get lots of debugging output.
72 config IP_NF_MATCH_PKTTYPE
73 tristate "Packet type match support"
74 depends on IP_NF_IPTABLES
75 --- linux-2.6.14/net/ipv4/netfilter/Makefile 2005-10-27 19:02:08.000000000 -0500
76 +++ linux-2.6.14-layer7/net/ipv4/netfilter/Makefile 2005-11-12 17:31:34.000000000 -0600
77 @@ -74,6 +74,8 @@ obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt
78 obj-$(CONFIG_IP_NF_MATCH_COMMENT) += ipt_comment.o
79 obj-$(CONFIG_IP_NF_MATCH_STRING) += ipt_string.o
81 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
84 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
85 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
86 --- linux-2.6.14/net/ipv4/netfilter/ip_conntrack_core.c 2005-10-27 19:02:08.000000000 -0500
87 +++ linux-2.6.14-layer7/net/ipv4/netfilter/ip_conntrack_core.c 2005-11-12 17:31:34.000000000 -0600
88 @@ -335,6 +335,13 @@ destroy_conntrack(struct nf_conntrack *n
90 ip_ct_remove_expectations(ct);
92 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
93 + if(ct->layer7.app_proto)
94 + kfree(ct->layer7.app_proto);
95 + if(ct->layer7.app_data)
96 + kfree(ct->layer7.app_data);
99 /* We overload first tuple to link into unconfirmed list. */
100 if (!is_confirmed(ct)) {
101 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
102 --- linux-2.6.14/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-10-27 19:02:08.000000000 -0500
103 +++ linux-2.6.14-layer7/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-11-12 17:31:34.000000000 -0600
104 @@ -188,6 +188,12 @@ static int ct_seq_show(struct seq_file *
108 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
109 + if(conntrack->layer7.app_proto)
110 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
114 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
117 --- linux-2.6.14/net/ipv4/netfilter/ipt_layer7.c 1969-12-31 18:00:00.000000000 -0600
118 +++ linux-2.6.14-layer7/net/ipv4/netfilter/ipt_layer7.c 2005-11-12 17:49:24.000000000 -0600
121 + Kernel module to match application layer (OSI layer 7)
122 + data in connections.
124 + http://l7-filter.sf.net
126 + By Matthew Strait and Ethan Sommer, 2003-2005.
128 + This program is free software; you can redistribute it and/or
129 + modify it under the terms of the GNU General Public License
130 + as published by the Free Software Foundation; either version
131 + 2 of the License, or (at your option) any later version.
132 + http://www.gnu.org/licenses/gpl.txt
134 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
135 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
138 +#include <linux/module.h>
139 +#include <linux/skbuff.h>
140 +#include <linux/netfilter_ipv4/ip_conntrack.h>
141 +#include <linux/proc_fs.h>
142 +#include <linux/ctype.h>
144 +#include <net/tcp.h>
145 +#include <linux/spinlock.h>
147 +#include "regexp/regexp.c"
149 +#include <linux/netfilter_ipv4/ipt_layer7.h>
150 +#include <linux/netfilter_ipv4/ip_tables.h>
152 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
153 +MODULE_LICENSE("GPL");
154 +MODULE_DESCRIPTION("iptables application layer match module");
155 +MODULE_VERSION("2.0");
157 +static int maxdatalen = 2048; // this is the default
158 +module_param(maxdatalen, int, 0444);
159 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
161 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
162 + #define DPRINTK(format,args...) printk(format,##args)
164 + #define DPRINTK(format,args...)
167 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
168 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
170 +/* Number of packets whose data we look at.
171 +This can be modified through /proc/net/layer7_numpackets */
172 +static int num_packets = 10;
174 +static struct pattern_cache {
175 + char * regex_string;
177 + struct pattern_cache * next;
178 +} * first_pattern_cache = NULL;
180 +/* I'm new to locking. Here are my assumptions:
182 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
183 + if they did, nothing awful would happen.
185 +- This code will never be processing the same packet twice at the same time,
186 + because iptables rules are traversed in order.
188 +- It doesn't matter if two packets from different connections are in here at
189 + the same time, because they don't share any data.
191 +- It _does_ matter if two packets from the same connection are here at the same
192 + time. In this case, we have to protect the conntracks and the list of
195 +DEFINE_RWLOCK(ct_lock);
196 +DEFINE_SPINLOCK(list_lock);
198 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
199 +/* Converts an unfriendly string into a friendly one by
200 +replacing unprintables with periods and all whitespace with " ". */
201 +static char * friendly_print(unsigned char * s)
203 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
207 + if (net_ratelimit())
208 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
212 + for(i = 0; i < strlen(s); i++){
213 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
214 + else if(isspace(s[i])) f[i] = ' ';
221 +static char dec2hex(int i)
225 + return (char)(i + '0');
228 + return (char)(i - 10 + 'a');
231 + if (net_ratelimit())
232 + printk("Problem in dec2hex\n");
237 +static char * hex_print(unsigned char * s)
239 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
243 + if (net_ratelimit())
244 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
248 + for(i = 0; i < strlen(s); i++) {
249 + g[i*3 ] = dec2hex(s[i]/16);
250 + g[i*3 + 1] = dec2hex(s[i]%16);
259 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
260 +over again, it make sense to cache the results. */
261 +static regexp * compile_and_cache(char * regex_string, char * protocol)
263 + struct pattern_cache * node = first_pattern_cache;
264 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
265 + struct pattern_cache * tmp;
268 + while (node != NULL) {
269 + if (!strcmp(node->regex_string, regex_string))
270 + return node->pattern;
272 + last_pattern_cache = node;/* points at the last non-NULL node */
276 + /* If we reach the end of the list, then we have not yet cached
277 + the pattern for this regex. Let's do that now.
278 + Be paranoid about running out of memory to avoid list corruption. */
279 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
282 + if (net_ratelimit())
283 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
287 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
288 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
291 + if(!tmp->regex_string || !tmp->pattern) {
292 + if (net_ratelimit())
293 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
294 + kfree(tmp->regex_string);
295 + kfree(tmp->pattern);
300 + /* Ok. The new node is all ready now. */
303 + if(first_pattern_cache == NULL) /* list is empty */
304 + first_pattern_cache = node; /* make node the beginning */
306 + last_pattern_cache->next = node; /* attach node to the end */
308 + /* copy the string and compile the regex */
309 + len = strlen(regex_string);
310 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
311 + node->pattern = regcomp(regex_string, &len);
312 + if ( !node->pattern ) {
313 + if (net_ratelimit())
314 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
315 + /* pattern is now cached as NULL, so we won't try again. */
318 + strcpy(node->regex_string, regex_string);
319 + return node->pattern;
322 +static int can_handle(const struct sk_buff *skb)
324 + if(!skb->nh.iph) /* not IP */
326 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
327 + skb->nh.iph->protocol != IPPROTO_UDP &&
328 + skb->nh.iph->protocol != IPPROTO_ICMP)
333 +/* Returns offset the into the skb->data that the application data starts */
334 +static int app_data_offset(const struct sk_buff *skb)
336 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
337 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
338 + int ip_hl = 4*skb->nh.iph->ihl;
340 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
341 + /* 12 == offset into TCP header for the header length field.
342 + Can't get this with skb->h.th->doff because the tcphdr
343 + struct doesn't get set when routing (this is confirmed to be
344 + true in Netfilter as well as QoS.) */
345 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
347 + return ip_hl + tcp_hl;
348 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
349 + return ip_hl + 8; /* UDP header is always 8 bytes */
350 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
351 + return ip_hl + 8; /* ICMP header is 8 bytes */
353 + if (net_ratelimit())
354 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
355 + return ip_hl + 8; /* something reasonable */
359 +/* handles whether there's a match when we aren't appending data anymore */
360 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
361 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
362 + struct ipt_layer7_info * info)
364 + /* If we're in here, throw the app data away */
365 + write_lock(&ct_lock);
366 + if(master_conntrack->layer7.app_data != NULL) {
368 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
369 + if(!master_conntrack->layer7.app_proto) {
370 + char * f = friendly_print(master_conntrack->layer7.app_data);
371 + char * g = hex_print(master_conntrack->layer7.app_data);
372 + DPRINTK("\nl7-filter gave up after %d bytes (%llu packets):\n%s\n",
376 + DPRINTK("In hex: %s\n", g);
381 + kfree(master_conntrack->layer7.app_data);
382 + master_conntrack->layer7.app_data = NULL; /* don't free again */
384 + write_unlock(&ct_lock);
386 + if(master_conntrack->layer7.app_proto){
387 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
388 + write_lock(&ct_lock);
389 + if(!conntrack->layer7.app_proto) {
390 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
391 + if(!conntrack->layer7.app_proto){
392 + if (net_ratelimit())
393 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
394 + write_unlock(&ct_lock);
397 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
399 + write_unlock(&ct_lock);
401 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
404 + /* If not classified, set to "unknown" to distinguish from
405 + connections that are still being tested. */
406 + write_lock(&ct_lock);
407 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
408 + if(!master_conntrack->layer7.app_proto){
409 + if (net_ratelimit())
410 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
411 + write_unlock(&ct_lock);
414 + strcpy(master_conntrack->layer7.app_proto, "unknown");
415 + write_unlock(&ct_lock);
420 +/* add the new app data to the conntrack. Return number of bytes added. */
421 +static int add_data(struct ip_conntrack * master_conntrack,
422 + char * app_data, int appdatalen)
425 + int oldlength = master_conntrack->layer7.app_data_len;
427 + /* Strip nulls. Make everything lower case (our regex lib doesn't
428 + do case insensitivity). Add it to the end of the current data. */
429 + for(i = 0; i < maxdatalen-oldlength-1 &&
430 + i < appdatalen; i++) {
431 + if(app_data[i] != '\0') {
432 + master_conntrack->layer7.app_data[length+oldlength] =
433 + /* the kernel version of tolower mungs 'upper ascii' */
434 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
439 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
440 + master_conntrack->layer7.app_data_len = length + oldlength;
445 +/* Returns true on match and false otherwise. */
446 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
447 + const struct net_device *out, const void *matchinfo,
448 + int offset, int *hotdrop)
450 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
451 + enum ip_conntrack_info master_ctinfo, ctinfo;
452 + struct ip_conntrack *master_conntrack, *conntrack;
453 + unsigned char * app_data;
454 + unsigned int pattern_result, appdatalen;
455 + regexp * comppattern;
457 + if(!can_handle(skb)){
458 + DPRINTK("layer7: This is some protocol I can't handle.\n");
459 + return info->invert;
462 + /* Treat parent & all its children together as one connection, except
463 + for the purpose of setting conntrack->layer7.app_proto in the actual
464 + connection. This makes /proc/net/ip_conntrack more satisfying. */
465 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
466 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
467 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
468 + return info->invert;
471 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
472 + while (master_ct(master_conntrack) != NULL)
473 + master_conntrack = master_ct(master_conntrack);
475 + /* if we've classified it or seen too many packets */
476 + if(TOTAL_PACKETS > num_packets ||
477 + master_conntrack->layer7.app_proto) {
479 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
481 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
482 + rules. I'm not sure that using cb for this purpose is correct, although
483 + it says "put your private variables there". But it doesn't look like it
484 + is being used for anything else in the skbs that make it here. How can
485 + I write to cb without making the compiler angry? */
486 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
488 + return (pattern_result ^ info->invert);
491 + if(skb_is_nonlinear(skb)){
492 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
493 + if (net_ratelimit())
494 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
495 + return info->invert;
499 + /* now that the skb is linearized, it's safe to set these. */
500 + app_data = skb->data + app_data_offset(skb);
501 + appdatalen = skb->tail - app_data;
503 + spin_lock_bh(&list_lock);
504 + /* the return value gets checked later, when we're ready to use it */
505 + comppattern = compile_and_cache(info->pattern, info->protocol);
506 + spin_unlock_bh(&list_lock);
508 + /* On the first packet of a connection, allocate space for app data */
509 + write_lock(&ct_lock);
510 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
511 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
512 + if(!master_conntrack->layer7.app_data){
513 + if (net_ratelimit())
514 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
515 + write_unlock(&ct_lock);
516 + return info->invert;
519 + master_conntrack->layer7.app_data[0] = '\0';
521 + write_unlock(&ct_lock);
523 + /* Can be here, but unallocated, if numpackets is increased near
524 + the beginning of a connection */
525 + if(master_conntrack->layer7.app_data == NULL)
526 + return (info->invert); /* unmatched */
530 + write_lock(&ct_lock);
531 + newbytes = add_data(master_conntrack, app_data, appdatalen);
532 + write_unlock(&ct_lock);
534 + if(newbytes == 0) { /* didn't add any data */
536 + /* Didn't match before, not going to match now */
537 + return info->invert;
541 + /* If looking for "unknown", then never match. "Unknown" means that
542 + we've given up; we're still trying with these packets. */
543 + if(!strcmp(info->protocol, "unknown")) {
544 + pattern_result = 0;
545 + /* If the regexp failed to compile, don't bother running it */
546 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
547 + DPRINTK("layer7: matched %s\n", info->protocol);
548 + pattern_result = 1;
549 + } else pattern_result = 0;
551 + if(pattern_result) {
552 + write_lock(&ct_lock);
553 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
554 + if(!master_conntrack->layer7.app_proto){
555 + if (net_ratelimit())
556 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
557 + write_unlock(&ct_lock);
558 + return (pattern_result ^ info->invert);
560 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
561 + write_unlock(&ct_lock);
564 + /* mark the packet seen */
567 + return (pattern_result ^ info->invert);
570 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
571 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
573 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
578 +static struct ipt_match layer7_match = {
581 + .checkentry = &checkentry,
585 +/* taken from drivers/video/modedb.c */
586 +static int my_atoi(const char *s)
593 + val = 10*val+(*s-'0');
601 +/* write out num_packets to userland. */
602 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
603 + int* eof, void * data)
605 + if(num_packets > 99 && net_ratelimit())
606 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
608 + page[0] = num_packets/10 + '0';
609 + page[1] = num_packets%10 + '0';
618 +/* Read in num_packets from userland */
619 +static int layer7_write_proc(struct file* file, const char* buffer,
620 + unsigned long count, void *data)
622 + char * foo = kmalloc(count, GFP_ATOMIC);
625 + if (net_ratelimit())
626 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
630 + if(copy_from_user(foo, buffer, count)) {
635 + num_packets = my_atoi(foo);
638 + /* This has an arbitrary limit to make the math easier. I'm lazy.
639 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
640 + if(num_packets > 99) {
641 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
643 + } else if(num_packets < 1) {
644 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
651 +/* register the proc file */
652 +static void layer7_init_proc(void)
654 + struct proc_dir_entry* entry;
655 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
656 + entry->read_proc = layer7_read_proc;
657 + entry->write_proc = layer7_write_proc;
660 +static void layer7_cleanup_proc(void)
662 + remove_proc_entry("layer7_numpackets", proc_net);
665 +static int __init init(void)
667 + layer7_init_proc();
668 + if(maxdatalen < 1) {
669 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
672 + /* This is not a hard limit. It's just here to prevent people from
673 + bringing their slow machines to a grinding halt. */
674 + else if(maxdatalen > 65536) {
675 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
676 + maxdatalen = 65536;
678 + return ipt_register_match(&layer7_match);
681 +static void __exit fini(void)
683 + layer7_cleanup_proc();
684 + ipt_unregister_match(&layer7_match);
689 --- linux-2.6.14/net/ipv4/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
690 +++ linux-2.6.14-layer7/net/ipv4/netfilter/regexp/regexp.c 2005-11-12 17:31:34.000000000 -0600
693 + * regcomp and regexec -- regsub and regerror are elsewhere
694 + * @(#)regexp.c 1.3 of 18 April 87
696 + * Copyright (c) 1986 by University of Toronto.
697 + * Written by Henry Spencer. Not derived from licensed software.
699 + * Permission is granted to anyone to use this software for any
700 + * purpose on any computer system, and to redistribute it freely,
701 + * subject to the following restrictions:
703 + * 1. The author is not responsible for the consequences of use of
704 + * this software, no matter how awful, even if they arise
705 + * from defects in it.
707 + * 2. The origin of this software must not be misrepresented, either
708 + * by explicit claim or by omission.
710 + * 3. Altered versions must be plainly marked as such, and must not
711 + * be misrepresented as being the original software.
713 + * Beware that some of this code is subtly aware of the way operator
714 + * precedence is structured in regular expressions. Serious changes in
715 + * regular-expression syntax might require a total rethink.
717 + * This code was modified by Ethan Sommer to work within the kernel
718 + * (it now uses kmalloc etc..)
720 + * Modified slightly by Matthew Strait to use more modern C.
724 +#include "regmagic.h"
726 +/* added by ethan and matt. Lets it work in both kernel and user space.
727 +(So iptables can use it, for instance.) Yea, it goes both ways... */
729 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
731 + #define printk(format,args...) printf(format,##args)
734 +void regerror(char * s)
736 + printk("<3>Regexp: %s\n", s);
741 + * The "internal use only" fields in regexp.h are present to pass info from
742 + * compile to execute that permits the execute phase to run lots faster on
743 + * simple cases. They are:
745 + * regstart char that must begin a match; '\0' if none obvious
746 + * reganch is the match anchored (at beginning-of-line only)?
747 + * regmust string (pointer into program) that match must include, or NULL
748 + * regmlen length of regmust string
750 + * Regstart and reganch permit very fast decisions on suitable starting points
751 + * for a match, cutting down the work a lot. Regmust permits fast rejection
752 + * of lines that cannot possibly match. The regmust tests are costly enough
753 + * that regcomp() supplies a regmust only if the r.e. contains something
754 + * potentially expensive (at present, the only such thing detected is * or +
755 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
756 + * supplied because the test in regexec() needs it and regcomp() is computing
761 + * Structure for regexp "program". This is essentially a linear encoding
762 + * of a nondeterministic finite-state machine (aka syntax charts or
763 + * "railroad normal form" in parsing technology). Each node is an opcode
764 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
765 + * all nodes except BRANCH implement concatenation; a "next" pointer with
766 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
767 + * have one of the subtle syntax dependencies: an individual BRANCH (as
768 + * opposed to a collection of them) is never concatenated with anything
769 + * because of operator precedence.) The operand of some types of node is
770 + * a literal string; for others, it is a node leading into a sub-FSM. In
771 + * particular, the operand of a BRANCH node is the first node of the branch.
772 + * (NB this is *not* a tree structure: the tail of the branch connects
773 + * to the thing following the set of BRANCHes.) The opcodes are:
776 +/* definition number opnd? meaning */
777 +#define END 0 /* no End of program. */
778 +#define BOL 1 /* no Match "" at beginning of line. */
779 +#define EOL 2 /* no Match "" at end of line. */
780 +#define ANY 3 /* no Match any one character. */
781 +#define ANYOF 4 /* str Match any character in this string. */
782 +#define ANYBUT 5 /* str Match any character not in this string. */
783 +#define BRANCH 6 /* node Match this alternative, or the next... */
784 +#define BACK 7 /* no Match "", "next" ptr points backward. */
785 +#define EXACTLY 8 /* str Match this string. */
786 +#define NOTHING 9 /* no Match empty string. */
787 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
788 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
789 +#define OPEN 20 /* no Mark this point in input as start of #n. */
790 + /* OPEN+1 is number 1, etc. */
791 +#define CLOSE 30 /* no Analogous to OPEN. */
796 + * BRANCH The set of branches constituting a single choice are hooked
797 + * together with their "next" pointers, since precedence prevents
798 + * anything being concatenated to any individual branch. The
799 + * "next" pointer of the last BRANCH in a choice points to the
800 + * thing following the whole choice. This is also where the
801 + * final "next" pointer of each individual branch points; each
802 + * branch starts with the operand node of a BRANCH node.
804 + * BACK Normal "next" pointers all implicitly point forward; BACK
805 + * exists to make loop structures possible.
807 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
808 + * BRANCH structures using BACK. Simple cases (one character
809 + * per match) are implemented with STAR and PLUS for speed
810 + * and to minimize recursive plunges.
812 + * OPEN,CLOSE ...are numbered at compile time.
816 + * A node is one char of opcode followed by two chars of "next" pointer.
817 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
818 + * value is a positive offset from the opcode of the node containing it.
819 + * An operand, if any, simply follows the node. (Note that much of the
820 + * code generation knows about this implicit relationship.)
822 + * Using two bytes for the "next" pointer is vast overkill for most things,
823 + * but allows patterns to get big without disasters.
825 +#define OP(p) (*(p))
826 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
827 +#define OPERAND(p) ((p) + 3)
830 + * See regmagic.h for one further detail of program structure.
835 + * Utility definitions.
838 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
840 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
843 +#define FAIL(m) { regerror(m); return(NULL); }
844 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
845 +#define META "^$.[()|?+*\\"
848 + * Flags to be passed up and down.
850 +#define HASWIDTH 01 /* Known never to match null string. */
851 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
852 +#define SPSTART 04 /* Starts with * or +. */
853 +#define WORST 0 /* Worst case. */
856 + * Global work variables for regcomp().
858 +static char *regparse; /* Input-scan pointer. */
859 +static int regnpar; /* () count. */
860 +static char regdummy;
861 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
862 +static long regsize; /* Code size. */
865 + * Forward declarations for regcomp()'s friends.
868 +#define STATIC static
870 +STATIC char *reg(int paren,int *flagp);
871 +STATIC char *regbranch(int *flagp);
872 +STATIC char *regpiece(int *flagp);
873 +STATIC char *regatom(int *flagp);
874 +STATIC char *regnode(char op);
875 +STATIC char *regnext(char *p);
876 +STATIC void regc(char b);
877 +STATIC void reginsert(char op, char *opnd);
878 +STATIC void regtail(char *p, char *val);
879 +STATIC void regoptail(char *p, char *val);
882 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
889 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
890 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
891 + if (*scan1 == *scan2++)
899 + - regcomp - compile a regular expression into internal code
901 + * We can't allocate space until we know how big the compiled form will be,
902 + * but we can't compile it (and thus know how big it is) until we've got a
903 + * place to put the code. So we cheat: we compile it twice, once with code
904 + * generation turned off and size counting turned on, and once "for real".
905 + * This also means that we don't allocate space until we are sure that the
906 + * thing really will compile successfully, and we never have to move the
907 + * code and thus invalidate pointers into it. (Note that it has to be in
908 + * one piece because free() must be able to free it all.)
910 + * Beware that the optimization-preparation code in here knows about some
911 + * of the structure of the compiled regexp.
914 +regcomp(char *exp,int *patternsize)
916 + register regexp *r;
917 + register char *scan;
918 + register char *longest;
921 + /* commented out by ethan
922 + extern char *malloc();
926 + FAIL("NULL argument");
928 + /* First pass: determine size, legality. */
932 + regcode = ®dummy;
934 + if (reg(0, &flags) == NULL)
937 + /* Small enough for pointer-storage convention? */
938 + if (regsize >= 32767L) /* Probably could be 65535L. */
939 + FAIL("regexp too big");
941 + /* Allocate space. */
942 + *patternsize=sizeof(regexp) + (unsigned)regsize;
943 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
945 + FAIL("out of space");
947 + /* Second pass: emit code. */
950 + regcode = r->program;
952 + if (reg(0, &flags) == NULL)
955 + /* Dig out information for optimizations. */
956 + r->regstart = '\0'; /* Worst-case defaults. */
960 + scan = r->program+1; /* First BRANCH. */
961 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
962 + scan = OPERAND(scan);
964 + /* Starting-point info. */
965 + if (OP(scan) == EXACTLY)
966 + r->regstart = *OPERAND(scan);
967 + else if (OP(scan) == BOL)
971 + * If there's something expensive in the r.e., find the
972 + * longest literal string that must appear and make it the
973 + * regmust. Resolve ties in favor of later strings, since
974 + * the regstart check works with the beginning of the r.e.
975 + * and avoiding duplication strengthens checking. Not a
976 + * strong reason, but sufficient in the absence of others.
978 + if (flags&SPSTART) {
981 + for (; scan != NULL; scan = regnext(scan))
982 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
983 + longest = OPERAND(scan);
984 + len = strlen(OPERAND(scan));
986 + r->regmust = longest;
995 + - reg - regular expression, i.e. main body or parenthesized thing
997 + * Caller must absorb opening parenthesis.
999 + * Combining parenthesis handling with the base level of regular expression
1000 + * is a trifle forced, but the need to tie the tails of the branches to what
1001 + * follows makes it hard to avoid.
1004 +reg(int paren, int *flagp /* Parenthesized? */ )
1006 + register char *ret;
1007 + register char *br;
1008 + register char *ender;
1009 + register int parno = 0; /* 0 makes gcc happy */
1012 + *flagp = HASWIDTH; /* Tentatively. */
1014 + /* Make an OPEN node, if parenthesized. */
1016 + if (regnpar >= NSUBEXP)
1017 + FAIL("too many ()");
1020 + ret = regnode(OPEN+parno);
1024 + /* Pick up the branches, linking them together. */
1025 + br = regbranch(&flags);
1029 + regtail(ret, br); /* OPEN -> first. */
1032 + if (!(flags&HASWIDTH))
1033 + *flagp &= ~HASWIDTH;
1034 + *flagp |= flags&SPSTART;
1035 + while (*regparse == '|') {
1037 + br = regbranch(&flags);
1040 + regtail(ret, br); /* BRANCH -> BRANCH. */
1041 + if (!(flags&HASWIDTH))
1042 + *flagp &= ~HASWIDTH;
1043 + *flagp |= flags&SPSTART;
1046 + /* Make a closing node, and hook it on the end. */
1047 + ender = regnode((paren) ? CLOSE+parno : END);
1048 + regtail(ret, ender);
1050 + /* Hook the tails of the branches to the closing node. */
1051 + for (br = ret; br != NULL; br = regnext(br))
1052 + regoptail(br, ender);
1054 + /* Check for proper termination. */
1055 + if (paren && *regparse++ != ')') {
1056 + FAIL("unmatched ()");
1057 + } else if (!paren && *regparse != '\0') {
1058 + if (*regparse == ')') {
1059 + FAIL("unmatched ()");
1061 + FAIL("junk on end"); /* "Can't happen". */
1069 + - regbranch - one alternative of an | operator
1071 + * Implements the concatenation operator.
1074 +regbranch(int *flagp)
1076 + register char *ret;
1077 + register char *chain;
1078 + register char *latest;
1081 + *flagp = WORST; /* Tentatively. */
1083 + ret = regnode(BRANCH);
1085 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1086 + latest = regpiece(&flags);
1087 + if (latest == NULL)
1089 + *flagp |= flags&HASWIDTH;
1090 + if (chain == NULL) /* First piece. */
1091 + *flagp |= flags&SPSTART;
1093 + regtail(chain, latest);
1096 + if (chain == NULL) /* Loop ran zero times. */
1097 + (void) regnode(NOTHING);
1103 + - regpiece - something followed by possible [*+?]
1105 + * Note that the branching code sequences used for ? and the general cases
1106 + * of * and + are somewhat optimized: they use the same NOTHING node as
1107 + * both the endmarker for their branch list and the body of the last branch.
1108 + * It might seem that this node could be dispensed with entirely, but the
1109 + * endmarker role is not redundant.
1112 +regpiece(int *flagp)
1114 + register char *ret;
1116 + register char *next;
1119 + ret = regatom(&flags);
1124 + if (!ISMULT(op)) {
1129 + if (!(flags&HASWIDTH) && op != '?')
1130 + FAIL("*+ operand could be empty");
1131 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1133 + if (op == '*' && (flags&SIMPLE))
1134 + reginsert(STAR, ret);
1135 + else if (op == '*') {
1136 + /* Emit x* as (x&|), where & means "self". */
1137 + reginsert(BRANCH, ret); /* Either x */
1138 + regoptail(ret, regnode(BACK)); /* and loop */
1139 + regoptail(ret, ret); /* back */
1140 + regtail(ret, regnode(BRANCH)); /* or */
1141 + regtail(ret, regnode(NOTHING)); /* null. */
1142 + } else if (op == '+' && (flags&SIMPLE))
1143 + reginsert(PLUS, ret);
1144 + else if (op == '+') {
1145 + /* Emit x+ as x(&|), where & means "self". */
1146 + next = regnode(BRANCH); /* Either */
1147 + regtail(ret, next);
1148 + regtail(regnode(BACK), ret); /* loop back */
1149 + regtail(next, regnode(BRANCH)); /* or */
1150 + regtail(ret, regnode(NOTHING)); /* null. */
1151 + } else if (op == '?') {
1152 + /* Emit x? as (x|) */
1153 + reginsert(BRANCH, ret); /* Either x */
1154 + regtail(ret, regnode(BRANCH)); /* or */
1155 + next = regnode(NOTHING); /* null. */
1156 + regtail(ret, next);
1157 + regoptail(ret, next);
1160 + if (ISMULT(*regparse))
1161 + FAIL("nested *?+");
1167 + - regatom - the lowest level
1169 + * Optimization: gobbles an entire sequence of ordinary characters so that
1170 + * it can turn them into a single node, which is smaller to store and
1171 + * faster to run. Backslashed characters are exceptions, each becoming a
1172 + * separate node; the code is simpler that way and it's not worth fixing.
1175 +regatom(int *flagp)
1177 + register char *ret;
1180 + *flagp = WORST; /* Tentatively. */
1182 + switch (*regparse++) {
1184 + ret = regnode(BOL);
1187 + ret = regnode(EOL);
1190 + ret = regnode(ANY);
1191 + *flagp |= HASWIDTH|SIMPLE;
1194 + register int class;
1195 + register int classend;
1197 + if (*regparse == '^') { /* Complement of range. */
1198 + ret = regnode(ANYBUT);
1201 + ret = regnode(ANYOF);
1202 + if (*regparse == ']' || *regparse == '-')
1203 + regc(*regparse++);
1204 + while (*regparse != '\0' && *regparse != ']') {
1205 + if (*regparse == '-') {
1207 + if (*regparse == ']' || *regparse == '\0')
1210 + class = UCHARAT(regparse-2)+1;
1211 + classend = UCHARAT(regparse);
1212 + if (class > classend+1)
1213 + FAIL("invalid [] range");
1214 + for (; class <= classend; class++)
1219 + regc(*regparse++);
1222 + if (*regparse != ']')
1223 + FAIL("unmatched []");
1225 + *flagp |= HASWIDTH|SIMPLE;
1229 + ret = reg(1, &flags);
1232 + *flagp |= flags&(HASWIDTH|SPSTART);
1237 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1242 + FAIL("?+* follows nothing");
1245 + if (*regparse == '\0')
1246 + FAIL("trailing \\");
1247 + ret = regnode(EXACTLY);
1248 + regc(*regparse++);
1250 + *flagp |= HASWIDTH|SIMPLE;
1254 + register char ender;
1257 + len = my_strcspn((const char *)regparse, (const char *)META);
1259 + FAIL("internal disaster");
1260 + ender = *(regparse+len);
1261 + if (len > 1 && ISMULT(ender))
1262 + len--; /* Back off clear of ?+* operand. */
1263 + *flagp |= HASWIDTH;
1266 + ret = regnode(EXACTLY);
1268 + regc(*regparse++);
1280 + - regnode - emit a node
1282 +static char * /* Location. */
1285 + register char *ret;
1286 + register char *ptr;
1289 + if (ret == ®dummy) {
1296 + *ptr++ = '\0'; /* Null "next" pointer. */
1304 + - regc - emit (if appropriate) a byte of code
1309 + if (regcode != ®dummy)
1316 + - reginsert - insert an operator in front of already-emitted operand
1318 + * Means relocating the operand.
1321 +reginsert(char op, char* opnd)
1323 + register char *src;
1324 + register char *dst;
1325 + register char *place;
1327 + if (regcode == ®dummy) {
1335 + while (src > opnd)
1338 + place = opnd; /* Op node, where operand used to be. */
1345 + - regtail - set the next-pointer at the end of a node chain
1348 +regtail(char *p, char *val)
1350 + register char *scan;
1351 + register char *temp;
1352 + register int offset;
1354 + if (p == ®dummy)
1357 + /* Find last node. */
1360 + temp = regnext(scan);
1366 + if (OP(scan) == BACK)
1367 + offset = scan - val;
1369 + offset = val - scan;
1370 + *(scan+1) = (offset>>8)&0377;
1371 + *(scan+2) = offset&0377;
1375 + - regoptail - regtail on operand of first argument; nop if operandless
1378 +regoptail(char *p, char *val)
1380 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1381 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1383 + regtail(OPERAND(p), val);
1387 + * regexec and friends
1391 + * Global work variables for regexec().
1393 +static char *reginput; /* String-input pointer. */
1394 +static char *regbol; /* Beginning of input, for ^ check. */
1395 +static char **regstartp; /* Pointer to startp array. */
1396 +static char **regendp; /* Ditto for endp. */
1401 +STATIC int regtry(regexp *prog, char *string);
1402 +STATIC int regmatch(char *prog);
1403 +STATIC int regrepeat(char *p);
1406 +int regnarrate = 0;
1408 +STATIC char *regprop(char *op);
1412 + - regexec - match a regexp against a string
1415 +regexec(regexp *prog, char *string)
1419 + /* Be paranoid... */
1420 + if (prog == NULL || string == NULL) {
1421 + printk("<3>Regexp: NULL parameter\n");
1425 + /* Check validity of program. */
1426 + if (UCHARAT(prog->program) != MAGIC) {
1427 + printk("<3>Regexp: corrupted program\n");
1431 + /* If there is a "must appear" string, look for it. */
1432 + if (prog->regmust != NULL) {
1434 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1435 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1436 + break; /* Found it. */
1439 + if (s == NULL) /* Not present. */
1443 + /* Mark beginning of line for ^ . */
1446 + /* Simplest case: anchored match need be tried only once. */
1447 + if (prog->reganch)
1448 + return(regtry(prog, string));
1450 + /* Messy cases: unanchored match. */
1452 + if (prog->regstart != '\0')
1453 + /* We know what char it must start with. */
1454 + while ((s = strchr(s, prog->regstart)) != NULL) {
1455 + if (regtry(prog, s))
1460 + /* We don't -- general case. */
1462 + if (regtry(prog, s))
1464 + } while (*s++ != '\0');
1471 + - regtry - try match at specific point
1473 +static int /* 0 failure, 1 success */
1474 +regtry(regexp *prog, char *string)
1477 + register char **sp;
1478 + register char **ep;
1480 + reginput = string;
1481 + regstartp = prog->startp;
1482 + regendp = prog->endp;
1484 + sp = prog->startp;
1486 + for (i = NSUBEXP; i > 0; i--) {
1490 + if (regmatch(prog->program + 1)) {
1491 + prog->startp[0] = string;
1492 + prog->endp[0] = reginput;
1499 + - regmatch - main matching routine
1501 + * Conceptually the strategy is simple: check to see whether the current
1502 + * node matches, call self recursively to see whether the rest matches,
1503 + * and then act accordingly. In practice we make some effort to avoid
1504 + * recursion, in particular by going through "ordinary" nodes (that don't
1505 + * need to know whether the rest of the match failed) by a loop instead of
1508 +static int /* 0 failure, 1 success */
1509 +regmatch(char *prog)
1511 + register char *scan = prog; /* Current node. */
1512 + char *next; /* Next node. */
1515 + if (scan != NULL && regnarrate)
1516 + fprintf(stderr, "%s(\n", regprop(scan));
1518 + while (scan != NULL) {
1521 + fprintf(stderr, "%s...\n", regprop(scan));
1523 + next = regnext(scan);
1525 + switch (OP(scan)) {
1527 + if (reginput != regbol)
1531 + if (*reginput != '\0')
1535 + if (*reginput == '\0')
1541 + register char *opnd;
1543 + opnd = OPERAND(scan);
1544 + /* Inline the first character, for speed. */
1545 + if (*opnd != *reginput)
1547 + len = strlen(opnd);
1548 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1554 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1559 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1576 + register char *save;
1578 + no = OP(scan) - OPEN;
1581 + if (regmatch(next)) {
1583 + * Don't set startp if some later
1584 + * invocation of the same parentheses
1587 + if (regstartp[no] == NULL)
1588 + regstartp[no] = save;
1605 + register char *save;
1607 + no = OP(scan) - CLOSE;
1610 + if (regmatch(next)) {
1612 + * Don't set endp if some later
1613 + * invocation of the same parentheses
1616 + if (regendp[no] == NULL)
1617 + regendp[no] = save;
1624 + register char *save;
1626 + if (OP(next) != BRANCH) /* No choice. */
1627 + next = OPERAND(scan); /* Avoid recursion. */
1631 + if (regmatch(OPERAND(scan)))
1634 + scan = regnext(scan);
1635 + } while (scan != NULL && OP(scan) == BRANCH);
1643 + register char nextch;
1645 + register char *save;
1649 + * Lookahead to avoid useless match attempts
1650 + * when we know what character comes next.
1653 + if (OP(next) == EXACTLY)
1654 + nextch = *OPERAND(next);
1655 + min = (OP(scan) == STAR) ? 0 : 1;
1657 + no = regrepeat(OPERAND(scan));
1658 + while (no >= min) {
1659 + /* If it could work, try it. */
1660 + if (nextch == '\0' || *reginput == nextch)
1661 + if (regmatch(next))
1663 + /* Couldn't or didn't -- back up. */
1665 + reginput = save + no;
1671 + return(1); /* Success! */
1674 + printk("<3>Regexp: memory corruption\n");
1683 + * We get here only if there's trouble -- normally "case END" is
1684 + * the terminating point.
1686 + printk("<3>Regexp: corrupted pointers\n");
1691 + - regrepeat - repeatedly match something simple, report how many
1696 + register int count = 0;
1697 + register char *scan;
1698 + register char *opnd;
1701 + opnd = OPERAND(p);
1704 + count = strlen(scan);
1708 + while (*opnd == *scan) {
1714 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1720 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1725 + default: /* Oh dear. Called inappropriately. */
1726 + printk("<3>Regexp: internal foulup\n");
1727 + count = 0; /* Best compromise. */
1736 + - regnext - dig the "next" pointer out of a node
1741 + register int offset;
1743 + if (p == ®dummy)
1750 + if (OP(p) == BACK)
1758 +STATIC char *regprop();
1761 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1767 + register char op = EXACTLY; /* Arbitrary non-END op. */
1768 + register char *next;
1769 + /* extern char *strchr(); */
1772 + s = r->program + 1;
1773 + while (op != END) { /* While that wasn't END last time... */
1775 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1776 + next = regnext(s);
1777 + if (next == NULL) /* Next ptr. */
1780 + printf("(%d)", (s-r->program)+(next-s));
1782 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1783 + /* Literal string, where present. */
1784 + while (*s != '\0') {
1793 + /* Header fields of interest. */
1794 + if (r->regstart != '\0')
1795 + printf("start `%c' ", r->regstart);
1797 + printf("anchored ");
1798 + if (r->regmust != NULL)
1799 + printf("must have \"%s\"", r->regmust);
1804 + - regprop - printable representation of opcode
1811 + static char buf[BUFLEN];
1855 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1867 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1877 + printk("<3>Regexp: corrupted opcode\n");
1881 + strncat(buf, p, BUFLEN-strlen(buf));
1887 --- linux-2.6.14/net/ipv4/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1888 +++ linux-2.6.14-layer7/net/ipv4/netfilter/regexp/regexp.h 2005-11-12 17:31:34.000000000 -0600
1891 + * Definitions etc. for regexp(3) routines.
1893 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1894 + * not the System V one.
1902 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1903 +which contains a version of this library, says:
1906 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1907 + * will not work properly.
1910 +However, it looks rather like this library is limited to 10. If you think
1911 +otherwise, let us know.
1915 +typedef struct regexp {
1916 + char *startp[NSUBEXP];
1917 + char *endp[NSUBEXP];
1918 + char regstart; /* Internal use only. */
1919 + char reganch; /* Internal use only. */
1920 + char *regmust; /* Internal use only. */
1921 + int regmlen; /* Internal use only. */
1922 + char program[1]; /* Unwarranted chumminess with compiler. */
1925 +regexp * regcomp(char *exp, int *patternsize);
1926 +int regexec(regexp *prog, char *string);
1927 +void regsub(regexp *prog, char *source, char *dest);
1928 +void regerror(char *s);
1931 --- linux-2.6.14/net/ipv4/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1932 +++ linux-2.6.14-layer7/net/ipv4/netfilter/regexp/regmagic.h 2005-11-12 17:31:34.000000000 -0600
1935 + * The first byte of the regexp internal "program" is actually this magic
1936 + * number; the start node begins in the second byte.
1939 --- linux-2.6.14/net/ipv4/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1940 +++ linux-2.6.14-layer7/net/ipv4/netfilter/regexp/regsub.c 2005-11-12 17:31:34.000000000 -0600
1944 + * @(#)regsub.c 1.3 of 2 April 86
1946 + * Copyright (c) 1986 by University of Toronto.
1947 + * Written by Henry Spencer. Not derived from licensed software.
1949 + * Permission is granted to anyone to use this software for any
1950 + * purpose on any computer system, and to redistribute it freely,
1951 + * subject to the following restrictions:
1953 + * 1. The author is not responsible for the consequences of use of
1954 + * this software, no matter how awful, even if they arise
1955 + * from defects in it.
1957 + * 2. The origin of this software must not be misrepresented, either
1958 + * by explicit claim or by omission.
1960 + * 3. Altered versions must be plainly marked as such, and must not
1961 + * be misrepresented as being the original software.
1964 + * This code was modified by Ethan Sommer to work within the kernel
1965 + * (it now uses kmalloc etc..)
1968 +#include "regexp.h"
1969 +#include "regmagic.h"
1970 +#include <linux/string.h>
1974 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1976 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1980 +//void regerror(char * s)
1982 +// printk("regexp(3): %s", s);
1983 +// /* NOTREACHED */
1988 + - regsub - perform substitutions after a regexp match
1991 +regsub(regexp * prog, char * source, char * dest)
1993 + register char *src;
1994 + register char *dst;
1999 + /* Not necessary and gcc doesn't like it -MLS */
2000 + /*extern char *strncpy();*/
2002 + if (prog == NULL || source == NULL || dest == NULL) {
2003 + regerror("NULL parm to regsub");
2006 + if (UCHARAT(prog->program) != MAGIC) {
2007 + regerror("damaged regexp fed to regsub");
2013 + while ((c = *src++) != '\0') {
2016 + else if (c == '\\' && '0' <= *src && *src <= '9')
2017 + no = *src++ - '0';
2021 + if (no < 0) { /* Ordinary character. */
2022 + if (c == '\\' && (*src == '\\' || *src == '&'))
2025 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2026 + len = prog->endp[no] - prog->startp[no];
2027 + (void) strncpy(dst, prog->startp[no], len);
2029 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2030 + regerror("damaged match string");