1 --- linux-2.6.11.3-stock/include/linux/netfilter_ipv4/ip_conntrack.h 2005-03-13 00:44:41.000000000 -0600
2 +++ linux-2.6.11.3-layer7/include/linux/netfilter_ipv4/ip_conntrack.h 2005-03-13 20:30:01.000000000 -0600
3 @@ -177,6 +177,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.11.3-stock/include/linux/netfilter_ipv4/ipt_layer7.h 1969-12-31 18:00:00.000000000 -0600
20 +++ linux-2.6.11.3-layer7/include/linux/netfilter_ipv4/ipt_layer7.h 2005-03-13 20:30:01.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.11.3-stock/net/ipv4/netfilter/Kconfig 2005-03-13 00:44:38.000000000 -0600
49 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/Kconfig 2005-03-13 20:30:01.000000000 -0600
50 @@ -146,6 +146,33 @@ 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_LAYER7_MAXDATALEN
73 + int "Buffer size for application layer data" if IP_NF_MATCH_LAYER7
77 + Size of the buffer that the application layer data is stored in.
78 + Unless you know what you're doing, leave it at the default of 2kB.
81 config IP_NF_MATCH_PKTTYPE
82 tristate "Packet type match support"
83 depends on IP_NF_IPTABLES
84 --- linux-2.6.11.3-stock/net/ipv4/netfilter/Makefile 2005-03-13 00:44:14.000000000 -0600
85 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/Makefile 2005-03-13 20:30:01.000000000 -0600
86 @@ -60,6 +60,8 @@ obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ip
87 obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
88 obj-$(CONFIG_IP_NF_MATCH_COMMENT) += ipt_comment.o
90 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
93 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
94 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
95 --- linux-2.6.11.3-stock/net/ipv4/netfilter/ip_conntrack_core.c 2005-03-13 00:43:57.000000000 -0600
96 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/ip_conntrack_core.c 2005-03-13 22:09:32.000000000 -0600
97 @@ -247,6 +247,13 @@ destroy_conntrack(struct nf_conntrack *n
99 remove_expectations(ct);
101 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
102 + if(ct->layer7.app_proto)
103 + kfree(ct->layer7.app_proto);
104 + if(ct->layer7.app_data)
105 + kfree(ct->layer7.app_data);
108 /* We overload first tuple to link into unconfirmed list. */
109 if (!is_confirmed(ct)) {
110 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
111 --- linux-2.6.11.3-stock/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-03-13 00:44:25.000000000 -0600
112 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-03-13 20:30:01.000000000 -0600
113 @@ -152,6 +152,12 @@ static int ct_seq_real_show(const struct
117 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
118 + if(conntrack->layer7.app_proto)
119 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
123 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
126 --- linux-2.6.11.3-stock/net/ipv4/netfilter/ipt_layer7.c 1969-12-31 18:00:00.000000000 -0600
127 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/ipt_layer7.c 2005-03-13 20:30:01.000000000 -0600
130 + Kernel module to match application layer (OSI layer 7)
131 + data in connections.
133 + http://l7-filter.sf.net
135 + By Matthew Strait and Ethan Sommer, 2003-2005.
137 + This program is free software; you can redistribute it and/or
138 + modify it under the terms of the GNU General Public License
139 + as published by the Free Software Foundation; either version
140 + 2 of the License, or (at your option) any later version.
141 + http://www.gnu.org/licenses/gpl.txt
143 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
144 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
147 +#include <linux/module.h>
148 +#include <linux/skbuff.h>
149 +#include <linux/netfilter_ipv4/ip_conntrack.h>
150 +#include <linux/proc_fs.h>
151 +#include <linux/ctype.h>
153 +#include <net/tcp.h>
154 +#include <linux/netfilter_ipv4/lockhelp.h>
156 +#include "regexp/regexp.c"
158 +#include <linux/netfilter_ipv4/ipt_layer7.h>
159 +#include <linux/netfilter_ipv4/ip_tables.h>
161 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
162 +MODULE_LICENSE("GPL");
163 +MODULE_DESCRIPTION("iptables application layer match module");
165 +#if defined(CONFIG_IP_NF_MATCH_LAYER7_DEBUG)
166 + #define DPRINTK(format,args...) printk(format,##args)
168 + #define DPRINTK(format,args...)
171 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
172 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
174 +/* Number of packets whose data we look at.
175 +This can be modified through /proc/net/layer7_numpackets */
176 +static int num_packets = 8;
178 +static struct pattern_cache {
179 + char * regex_string;
181 + struct pattern_cache * next;
182 +} * first_pattern_cache = NULL;
184 +/* I'm new to locking. Here are my assumptions:
186 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
187 + if they did, nothing awful would happen.
189 +- This code will never be processing the same packet twice at the same time,
190 + because iptables rules are traversed in order.
192 +- It doesn't matter if two packets from different connections are in here at
193 + the same time, because they don't share any data.
195 +- It _does_ matter if two packets from the same connection are here at the same
196 + time. In this case, we have to protect the conntracks and the list of
199 +DECLARE_RWLOCK(ct_lock);
200 +DECLARE_LOCK(list_lock);
202 +#if CONFIG_IP_NF_MATCH_LAYER7_DEBUG
203 +/* Converts an unfriendly string into a friendly one by
204 +replacing unprintables with periods and all whitespace with " ". */
205 +static char * friendly_print(unsigned char * s)
207 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
211 + if (net_ratelimit())
212 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
216 + for(i = 0; i < strlen(s); i++){
217 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
218 + else if(isspace(s[i])) f[i] = ' ';
225 +static char dec2hex(int i)
229 + return (char)(i + '0');
232 + return (char)(i - 10 + 'a');
235 + if (net_ratelimit())
236 + printk("Problem in dec2hex\n");
241 +static char * hex_print(unsigned char * s)
243 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
247 + if (net_ratelimit())
248 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
252 + for(i = 0; i < strlen(s); i++) {
253 + g[i*3 ] = dec2hex(s[i]/16);
254 + g[i*3 + 1] = dec2hex(s[i]%16);
263 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
264 +over again, it make sense to cache the results. */
265 +static regexp * compile_and_cache(char * regex_string, char * protocol)
267 + struct pattern_cache * node = first_pattern_cache;
268 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
269 + struct pattern_cache * tmp;
272 + while (node != NULL) {
273 + if (!strcmp(node->regex_string, regex_string))
274 + return node->pattern;
276 + last_pattern_cache = node;/* points at the last non-NULL node */
280 + /* If we reach the end of the list, then we have not yet cached
281 + the pattern for this regex. Let's do that now.
282 + Be paranoid about running out of memory to avoid list corruption. */
283 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
286 + if (net_ratelimit())
287 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
291 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
292 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
295 + if(!tmp->regex_string || !tmp->pattern) {
296 + if (net_ratelimit())
297 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
298 + kfree(tmp->regex_string);
299 + kfree(tmp->pattern);
304 + /* Ok. The new node is all ready now. */
307 + if(first_pattern_cache == NULL) /* list is empty */
308 + first_pattern_cache = node; /* make node the beginning */
310 + last_pattern_cache->next = node; /* attach node to the end */
312 + /* copy the string and compile the regex */
313 + len = strlen(regex_string);
314 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
315 + node->pattern = regcomp(regex_string, &len);
316 + if ( !node->pattern ) {
317 + if (net_ratelimit())
318 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
319 + /* pattern is now cached as NULL, so we won't try again. */
322 + strcpy(node->regex_string, regex_string);
323 + return node->pattern;
326 +static int can_handle(const struct sk_buff *skb)
328 + if(!skb->nh.iph) /* not IP */
330 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
331 + skb->nh.iph->protocol != IPPROTO_UDP &&
332 + skb->nh.iph->protocol != IPPROTO_ICMP)
337 +/* Returns offset the into the skb->data that the application data starts */
338 +static int app_data_offset(const struct sk_buff *skb)
340 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
341 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
342 + int ip_hl = 4*skb->nh.iph->ihl;
344 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
345 + /* 12 == offset into TCP header for the header length field.
346 + Can't get this with skb->h.th->doff because the tcphdr
347 + struct doesn't get set when routing (this is confirmed to be
348 + true in Netfilter as well as QoS.) */
349 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
351 + return ip_hl + tcp_hl;
352 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
353 + return ip_hl + 8; /* UDP header is always 8 bytes */
354 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
355 + return ip_hl + 8; /* ICMP header is 8 bytes */
357 + if (net_ratelimit())
358 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
359 + return ip_hl + 8; /* something reasonable */
363 +/* handles whether there's a match when we aren't appending data anymore */
364 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
365 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
366 + struct ipt_layer7_info * info)
368 + /* If we're in here, throw the app data away */
369 + WRITE_LOCK(&ct_lock);
370 + if(master_conntrack->layer7.app_data != NULL) {
372 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
373 + if(!master_conntrack->layer7.app_proto) {
374 + char * f = friendly_print(master_conntrack->layer7.app_data);
375 + char * g = hex_print(master_conntrack->layer7.app_data);
376 + DPRINTK("\nl7-filter gave up after %d bytes (%llu packets):\n%s\n",
380 + DPRINTK("In hex: %s\n", g);
385 + kfree(master_conntrack->layer7.app_data);
386 + master_conntrack->layer7.app_data = NULL; /* don't free again */
388 + WRITE_UNLOCK(&ct_lock);
390 + if(master_conntrack->layer7.app_proto){
391 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
392 + WRITE_LOCK(&ct_lock);
393 + if(!conntrack->layer7.app_proto) {
394 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
395 + if(!conntrack->layer7.app_proto){
396 + if (net_ratelimit())
397 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
398 + WRITE_UNLOCK(&ct_lock);
401 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
403 + WRITE_UNLOCK(&ct_lock);
405 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
408 + /* If not classified, set to "unknown" to distinguish from
409 + connections that are still being tested. */
410 + WRITE_LOCK(&ct_lock);
411 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
412 + if(!master_conntrack->layer7.app_proto){
413 + if (net_ratelimit())
414 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
415 + WRITE_UNLOCK(&ct_lock);
418 + strcpy(master_conntrack->layer7.app_proto, "unknown");
419 + WRITE_UNLOCK(&ct_lock);
424 +/* add the new app data to the conntrack. Return number of bytes added. */
425 +static int add_data(struct ip_conntrack * master_conntrack,
426 + char * app_data, int appdatalen)
429 + int oldlength = master_conntrack->layer7.app_data_len;
431 + /* Strip nulls. Make everything lower case (our regex lib doesn't
432 + do case insensitivity). Add it to the end of the current data. */
433 + for(i = 0; i < CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN-oldlength-1 &&
434 + i < appdatalen; i++) {
435 + if(app_data[i] != '\0') {
436 + master_conntrack->layer7.app_data[length+oldlength] =
437 + /* the kernel version of tolower mungs 'upper ascii' */
438 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
443 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
444 + master_conntrack->layer7.app_data_len = length + oldlength;
449 +/* Returns true on match and false otherwise. */
450 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
451 + const struct net_device *out, const void *matchinfo,
452 + int offset, int *hotdrop)
454 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
455 + enum ip_conntrack_info master_ctinfo, ctinfo;
456 + struct ip_conntrack *master_conntrack, *conntrack;
457 + unsigned char * app_data;
458 + unsigned int pattern_result, appdatalen;
459 + regexp * comppattern;
461 + if(!can_handle(skb)){
462 + DPRINTK("layer7: This is some protocol I can't handle.\n");
463 + return info->invert;
466 + /* Treat the parent and all its children together as one connection,
467 + except for the purpose of setting conntrack->layer7.app_proto in the
468 + actual connection. This makes /proc/net/ip_conntrack somewhat more
470 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
471 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
472 + DPRINTK("layer7: packet is not from a known connection, giving up.\n");
473 + return info->invert;
476 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
477 + while (master_ct(master_conntrack) != NULL)
478 + master_conntrack = master_ct(master_conntrack);
480 + /* if we've classified it or seen too many packets */
481 + if(TOTAL_PACKETS > num_packets ||
482 + master_conntrack->layer7.app_proto) {
484 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
486 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
487 + rules. I'm not sure that using cb for this purpose is correct, although
488 + it says "put your private variables there". But it doesn't look like it
489 + is being used for anything else in the skbs that make it here. How can
490 + I write to cb without making the compiler angry? */
491 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
493 + return (pattern_result ^ info->invert);
496 + if(skb_is_nonlinear(skb)){
497 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
498 + if (net_ratelimit())
499 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
500 + return info->invert;
504 + /* now that the skb is linearized, it's safe to set these. */
505 + app_data = skb->data + app_data_offset(skb);
506 + appdatalen = skb->tail - app_data;
508 + LOCK_BH(&list_lock);
509 + /* the return value gets checked later, when we're ready to use it */
510 + comppattern = compile_and_cache(info->pattern, info->protocol);
511 + UNLOCK_BH(&list_lock);
513 + /* On the first packet of a connection, allocate space for app data */
514 + WRITE_LOCK(&ct_lock);
515 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
516 + master_conntrack->layer7.app_data = kmalloc(CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN, GFP_ATOMIC);
517 + if(!master_conntrack->layer7.app_data){
518 + if (net_ratelimit())
519 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
520 + WRITE_UNLOCK(&ct_lock);
521 + return info->invert;
524 + master_conntrack->layer7.app_data[0] = '\0';
526 + WRITE_UNLOCK(&ct_lock);
528 + /* Can be here, but unallocated, if numpackets is increased near
529 + the beginning of a connection */
530 + if(master_conntrack->layer7.app_data == NULL)
531 + return (info->invert); /* unmatched */
535 + WRITE_LOCK(&ct_lock);
536 + newbytes = add_data(master_conntrack, app_data, appdatalen);
537 + WRITE_UNLOCK(&ct_lock);
539 + if(newbytes == 0) { /* didn't add any data */
541 + /* Didn't match before, not going to match now */
542 + return info->invert;
546 + /* If looking for "unknown", then never match. "Unknown" means that
547 + we've given up; we're still trying with these packets. */
548 + if(!strcmp(info->protocol, "unknown")) {
549 + pattern_result = 0;
550 + /* If the regexp failed to compile, don't bother running it */
551 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
552 + DPRINTK("layer7: regexec positive: %s!\n", info->protocol);
553 + pattern_result = 1;
554 + } else pattern_result = 0;
556 + if(pattern_result) {
557 + WRITE_LOCK(&ct_lock);
558 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
559 + if(!master_conntrack->layer7.app_proto){
560 + if (net_ratelimit())
561 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
562 + WRITE_UNLOCK(&ct_lock);
563 + return (pattern_result ^ info->invert);
565 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
566 + WRITE_UNLOCK(&ct_lock);
569 + /* mark the packet seen */
572 + return (pattern_result ^ info->invert);
575 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
576 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
578 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
583 +static struct ipt_match layer7_match = {
586 + .checkentry = &checkentry,
590 +/* taken from drivers/video/modedb.c */
591 +static int my_atoi(const char *s)
598 + val = 10*val+(*s-'0');
606 +/* write out num_packets to userland. */
607 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
608 + int* eof, void * data)
610 + if(num_packets > 99 && net_ratelimit())
611 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
613 + page[0] = num_packets/10 + '0';
614 + page[1] = num_packets%10 + '0';
623 +/* Read in num_packets from userland */
624 +static int layer7_write_proc(struct file* file, const char* buffer,
625 + unsigned long count, void *data)
627 + char * foo = kmalloc(count, GFP_ATOMIC);
630 + if (net_ratelimit())
631 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
635 + copy_from_user(foo, buffer, count);
637 + num_packets = my_atoi(foo);
640 + /* This has an arbitrary limit to make the math easier. I'm lazy.
641 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
642 + if(num_packets > 99) {
643 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
645 + } else if(num_packets < 1) {
646 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
653 +/* register the proc file */
654 +static void layer7_init_proc(void)
656 + struct proc_dir_entry* entry;
657 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
658 + entry->read_proc = layer7_read_proc;
659 + entry->write_proc = layer7_write_proc;
662 +static void layer7_cleanup_proc(void)
664 + remove_proc_entry("layer7_numpackets", proc_net);
667 +static int __init init(void)
669 + layer7_init_proc();
670 + return ipt_register_match(&layer7_match);
673 +static void __exit fini(void)
675 + layer7_cleanup_proc();
676 + ipt_unregister_match(&layer7_match);
681 --- linux-2.6.11.3-stock/net/ipv4/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
682 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/regexp/regexp.c 2005-03-13 20:30:01.000000000 -0600
685 + * regcomp and regexec -- regsub and regerror are elsewhere
686 + * @(#)regexp.c 1.3 of 18 April 87
688 + * Copyright (c) 1986 by University of Toronto.
689 + * Written by Henry Spencer. Not derived from licensed software.
691 + * Permission is granted to anyone to use this software for any
692 + * purpose on any computer system, and to redistribute it freely,
693 + * subject to the following restrictions:
695 + * 1. The author is not responsible for the consequences of use of
696 + * this software, no matter how awful, even if they arise
697 + * from defects in it.
699 + * 2. The origin of this software must not be misrepresented, either
700 + * by explicit claim or by omission.
702 + * 3. Altered versions must be plainly marked as such, and must not
703 + * be misrepresented as being the original software.
705 + * Beware that some of this code is subtly aware of the way operator
706 + * precedence is structured in regular expressions. Serious changes in
707 + * regular-expression syntax might require a total rethink.
709 + * This code was modified by Ethan Sommer to work within the kernel
710 + * (it now uses kmalloc etc..)
712 + * Modified slightly by Matthew Strait to use more modern C.
716 +#include "regmagic.h"
718 +/* added by ethan and matt. Lets it work in both kernel and user space.
719 +(So iptables can use it, for instance.) Yea, it goes both ways... */
721 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
723 + #define printk(format,args...) printf(format,##args)
726 +void regerror(char * s)
728 + printk("<3>Regexp: %s\n", s);
733 + * The "internal use only" fields in regexp.h are present to pass info from
734 + * compile to execute that permits the execute phase to run lots faster on
735 + * simple cases. They are:
737 + * regstart char that must begin a match; '\0' if none obvious
738 + * reganch is the match anchored (at beginning-of-line only)?
739 + * regmust string (pointer into program) that match must include, or NULL
740 + * regmlen length of regmust string
742 + * Regstart and reganch permit very fast decisions on suitable starting points
743 + * for a match, cutting down the work a lot. Regmust permits fast rejection
744 + * of lines that cannot possibly match. The regmust tests are costly enough
745 + * that regcomp() supplies a regmust only if the r.e. contains something
746 + * potentially expensive (at present, the only such thing detected is * or +
747 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
748 + * supplied because the test in regexec() needs it and regcomp() is computing
753 + * Structure for regexp "program". This is essentially a linear encoding
754 + * of a nondeterministic finite-state machine (aka syntax charts or
755 + * "railroad normal form" in parsing technology). Each node is an opcode
756 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
757 + * all nodes except BRANCH implement concatenation; a "next" pointer with
758 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
759 + * have one of the subtle syntax dependencies: an individual BRANCH (as
760 + * opposed to a collection of them) is never concatenated with anything
761 + * because of operator precedence.) The operand of some types of node is
762 + * a literal string; for others, it is a node leading into a sub-FSM. In
763 + * particular, the operand of a BRANCH node is the first node of the branch.
764 + * (NB this is *not* a tree structure: the tail of the branch connects
765 + * to the thing following the set of BRANCHes.) The opcodes are:
768 +/* definition number opnd? meaning */
769 +#define END 0 /* no End of program. */
770 +#define BOL 1 /* no Match "" at beginning of line. */
771 +#define EOL 2 /* no Match "" at end of line. */
772 +#define ANY 3 /* no Match any one character. */
773 +#define ANYOF 4 /* str Match any character in this string. */
774 +#define ANYBUT 5 /* str Match any character not in this string. */
775 +#define BRANCH 6 /* node Match this alternative, or the next... */
776 +#define BACK 7 /* no Match "", "next" ptr points backward. */
777 +#define EXACTLY 8 /* str Match this string. */
778 +#define NOTHING 9 /* no Match empty string. */
779 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
780 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
781 +#define OPEN 20 /* no Mark this point in input as start of #n. */
782 + /* OPEN+1 is number 1, etc. */
783 +#define CLOSE 30 /* no Analogous to OPEN. */
788 + * BRANCH The set of branches constituting a single choice are hooked
789 + * together with their "next" pointers, since precedence prevents
790 + * anything being concatenated to any individual branch. The
791 + * "next" pointer of the last BRANCH in a choice points to the
792 + * thing following the whole choice. This is also where the
793 + * final "next" pointer of each individual branch points; each
794 + * branch starts with the operand node of a BRANCH node.
796 + * BACK Normal "next" pointers all implicitly point forward; BACK
797 + * exists to make loop structures possible.
799 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
800 + * BRANCH structures using BACK. Simple cases (one character
801 + * per match) are implemented with STAR and PLUS for speed
802 + * and to minimize recursive plunges.
804 + * OPEN,CLOSE ...are numbered at compile time.
808 + * A node is one char of opcode followed by two chars of "next" pointer.
809 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
810 + * value is a positive offset from the opcode of the node containing it.
811 + * An operand, if any, simply follows the node. (Note that much of the
812 + * code generation knows about this implicit relationship.)
814 + * Using two bytes for the "next" pointer is vast overkill for most things,
815 + * but allows patterns to get big without disasters.
817 +#define OP(p) (*(p))
818 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
819 +#define OPERAND(p) ((p) + 3)
822 + * See regmagic.h for one further detail of program structure.
827 + * Utility definitions.
830 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
832 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
835 +#define FAIL(m) { regerror(m); return(NULL); }
836 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
837 +#define META "^$.[()|?+*\\"
840 + * Flags to be passed up and down.
842 +#define HASWIDTH 01 /* Known never to match null string. */
843 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
844 +#define SPSTART 04 /* Starts with * or +. */
845 +#define WORST 0 /* Worst case. */
848 + * Global work variables for regcomp().
850 +static char *regparse; /* Input-scan pointer. */
851 +static int regnpar; /* () count. */
852 +static char regdummy;
853 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
854 +static long regsize; /* Code size. */
857 + * Forward declarations for regcomp()'s friends.
860 +#define STATIC static
862 +STATIC char *reg(int paren,int *flagp);
863 +STATIC char *regbranch(int *flagp);
864 +STATIC char *regpiece(int *flagp);
865 +STATIC char *regatom(int *flagp);
866 +STATIC char *regnode(char op);
867 +STATIC char *regnext(char *p);
868 +STATIC void regc(char b);
869 +STATIC void reginsert(char op, char *opnd);
870 +STATIC void regtail(char *p, char *val);
871 +STATIC void regoptail(char *p, char *val);
874 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
881 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
882 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
883 + if (*scan1 == *scan2++)
891 + - regcomp - compile a regular expression into internal code
893 + * We can't allocate space until we know how big the compiled form will be,
894 + * but we can't compile it (and thus know how big it is) until we've got a
895 + * place to put the code. So we cheat: we compile it twice, once with code
896 + * generation turned off and size counting turned on, and once "for real".
897 + * This also means that we don't allocate space until we are sure that the
898 + * thing really will compile successfully, and we never have to move the
899 + * code and thus invalidate pointers into it. (Note that it has to be in
900 + * one piece because free() must be able to free it all.)
902 + * Beware that the optimization-preparation code in here knows about some
903 + * of the structure of the compiled regexp.
906 +regcomp(char *exp,int *patternsize)
908 + register regexp *r;
909 + register char *scan;
910 + register char *longest;
913 + /* commented out by ethan
914 + extern char *malloc();
918 + FAIL("NULL argument");
920 + /* First pass: determine size, legality. */
924 + regcode = ®dummy;
926 + if (reg(0, &flags) == NULL)
929 + /* Small enough for pointer-storage convention? */
930 + if (regsize >= 32767L) /* Probably could be 65535L. */
931 + FAIL("regexp too big");
933 + /* Allocate space. */
934 + *patternsize=sizeof(regexp) + (unsigned)regsize;
935 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
937 + FAIL("out of space");
939 + /* Second pass: emit code. */
942 + regcode = r->program;
944 + if (reg(0, &flags) == NULL)
947 + /* Dig out information for optimizations. */
948 + r->regstart = '\0'; /* Worst-case defaults. */
952 + scan = r->program+1; /* First BRANCH. */
953 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
954 + scan = OPERAND(scan);
956 + /* Starting-point info. */
957 + if (OP(scan) == EXACTLY)
958 + r->regstart = *OPERAND(scan);
959 + else if (OP(scan) == BOL)
963 + * If there's something expensive in the r.e., find the
964 + * longest literal string that must appear and make it the
965 + * regmust. Resolve ties in favor of later strings, since
966 + * the regstart check works with the beginning of the r.e.
967 + * and avoiding duplication strengthens checking. Not a
968 + * strong reason, but sufficient in the absence of others.
970 + if (flags&SPSTART) {
973 + for (; scan != NULL; scan = regnext(scan))
974 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
975 + longest = OPERAND(scan);
976 + len = strlen(OPERAND(scan));
978 + r->regmust = longest;
987 + - reg - regular expression, i.e. main body or parenthesized thing
989 + * Caller must absorb opening parenthesis.
991 + * Combining parenthesis handling with the base level of regular expression
992 + * is a trifle forced, but the need to tie the tails of the branches to what
993 + * follows makes it hard to avoid.
996 +reg(int paren, int *flagp /* Parenthesized? */ )
998 + register char *ret;
1000 + register char *ender;
1001 + register int parno = 0; /* 0 makes gcc happy */
1004 + *flagp = HASWIDTH; /* Tentatively. */
1006 + /* Make an OPEN node, if parenthesized. */
1008 + if (regnpar >= NSUBEXP)
1009 + FAIL("too many ()");
1012 + ret = regnode(OPEN+parno);
1016 + /* Pick up the branches, linking them together. */
1017 + br = regbranch(&flags);
1021 + regtail(ret, br); /* OPEN -> first. */
1024 + if (!(flags&HASWIDTH))
1025 + *flagp &= ~HASWIDTH;
1026 + *flagp |= flags&SPSTART;
1027 + while (*regparse == '|') {
1029 + br = regbranch(&flags);
1032 + regtail(ret, br); /* BRANCH -> BRANCH. */
1033 + if (!(flags&HASWIDTH))
1034 + *flagp &= ~HASWIDTH;
1035 + *flagp |= flags&SPSTART;
1038 + /* Make a closing node, and hook it on the end. */
1039 + ender = regnode((paren) ? CLOSE+parno : END);
1040 + regtail(ret, ender);
1042 + /* Hook the tails of the branches to the closing node. */
1043 + for (br = ret; br != NULL; br = regnext(br))
1044 + regoptail(br, ender);
1046 + /* Check for proper termination. */
1047 + if (paren && *regparse++ != ')') {
1048 + FAIL("unmatched ()");
1049 + } else if (!paren && *regparse != '\0') {
1050 + if (*regparse == ')') {
1051 + FAIL("unmatched ()");
1053 + FAIL("junk on end"); /* "Can't happen". */
1061 + - regbranch - one alternative of an | operator
1063 + * Implements the concatenation operator.
1066 +regbranch(int *flagp)
1068 + register char *ret;
1069 + register char *chain;
1070 + register char *latest;
1073 + *flagp = WORST; /* Tentatively. */
1075 + ret = regnode(BRANCH);
1077 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1078 + latest = regpiece(&flags);
1079 + if (latest == NULL)
1081 + *flagp |= flags&HASWIDTH;
1082 + if (chain == NULL) /* First piece. */
1083 + *flagp |= flags&SPSTART;
1085 + regtail(chain, latest);
1088 + if (chain == NULL) /* Loop ran zero times. */
1089 + (void) regnode(NOTHING);
1095 + - regpiece - something followed by possible [*+?]
1097 + * Note that the branching code sequences used for ? and the general cases
1098 + * of * and + are somewhat optimized: they use the same NOTHING node as
1099 + * both the endmarker for their branch list and the body of the last branch.
1100 + * It might seem that this node could be dispensed with entirely, but the
1101 + * endmarker role is not redundant.
1104 +regpiece(int *flagp)
1106 + register char *ret;
1108 + register char *next;
1111 + ret = regatom(&flags);
1116 + if (!ISMULT(op)) {
1121 + if (!(flags&HASWIDTH) && op != '?')
1122 + FAIL("*+ operand could be empty");
1123 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1125 + if (op == '*' && (flags&SIMPLE))
1126 + reginsert(STAR, ret);
1127 + else if (op == '*') {
1128 + /* Emit x* as (x&|), where & means "self". */
1129 + reginsert(BRANCH, ret); /* Either x */
1130 + regoptail(ret, regnode(BACK)); /* and loop */
1131 + regoptail(ret, ret); /* back */
1132 + regtail(ret, regnode(BRANCH)); /* or */
1133 + regtail(ret, regnode(NOTHING)); /* null. */
1134 + } else if (op == '+' && (flags&SIMPLE))
1135 + reginsert(PLUS, ret);
1136 + else if (op == '+') {
1137 + /* Emit x+ as x(&|), where & means "self". */
1138 + next = regnode(BRANCH); /* Either */
1139 + regtail(ret, next);
1140 + regtail(regnode(BACK), ret); /* loop back */
1141 + regtail(next, regnode(BRANCH)); /* or */
1142 + regtail(ret, regnode(NOTHING)); /* null. */
1143 + } else if (op == '?') {
1144 + /* Emit x? as (x|) */
1145 + reginsert(BRANCH, ret); /* Either x */
1146 + regtail(ret, regnode(BRANCH)); /* or */
1147 + next = regnode(NOTHING); /* null. */
1148 + regtail(ret, next);
1149 + regoptail(ret, next);
1152 + if (ISMULT(*regparse))
1153 + FAIL("nested *?+");
1159 + - regatom - the lowest level
1161 + * Optimization: gobbles an entire sequence of ordinary characters so that
1162 + * it can turn them into a single node, which is smaller to store and
1163 + * faster to run. Backslashed characters are exceptions, each becoming a
1164 + * separate node; the code is simpler that way and it's not worth fixing.
1167 +regatom(int *flagp)
1169 + register char *ret;
1172 + *flagp = WORST; /* Tentatively. */
1174 + switch (*regparse++) {
1176 + ret = regnode(BOL);
1179 + ret = regnode(EOL);
1182 + ret = regnode(ANY);
1183 + *flagp |= HASWIDTH|SIMPLE;
1186 + register int class;
1187 + register int classend;
1189 + if (*regparse == '^') { /* Complement of range. */
1190 + ret = regnode(ANYBUT);
1193 + ret = regnode(ANYOF);
1194 + if (*regparse == ']' || *regparse == '-')
1195 + regc(*regparse++);
1196 + while (*regparse != '\0' && *regparse != ']') {
1197 + if (*regparse == '-') {
1199 + if (*regparse == ']' || *regparse == '\0')
1202 + class = UCHARAT(regparse-2)+1;
1203 + classend = UCHARAT(regparse);
1204 + if (class > classend+1)
1205 + FAIL("invalid [] range");
1206 + for (; class <= classend; class++)
1211 + regc(*regparse++);
1214 + if (*regparse != ']')
1215 + FAIL("unmatched []");
1217 + *flagp |= HASWIDTH|SIMPLE;
1221 + ret = reg(1, &flags);
1224 + *flagp |= flags&(HASWIDTH|SPSTART);
1229 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1234 + FAIL("?+* follows nothing");
1237 + if (*regparse == '\0')
1238 + FAIL("trailing \\");
1239 + ret = regnode(EXACTLY);
1240 + regc(*regparse++);
1242 + *flagp |= HASWIDTH|SIMPLE;
1246 + register char ender;
1249 + len = my_strcspn((const char *)regparse, (const char *)META);
1251 + FAIL("internal disaster");
1252 + ender = *(regparse+len);
1253 + if (len > 1 && ISMULT(ender))
1254 + len--; /* Back off clear of ?+* operand. */
1255 + *flagp |= HASWIDTH;
1258 + ret = regnode(EXACTLY);
1260 + regc(*regparse++);
1272 + - regnode - emit a node
1274 +static char * /* Location. */
1277 + register char *ret;
1278 + register char *ptr;
1281 + if (ret == ®dummy) {
1288 + *ptr++ = '\0'; /* Null "next" pointer. */
1296 + - regc - emit (if appropriate) a byte of code
1301 + if (regcode != ®dummy)
1308 + - reginsert - insert an operator in front of already-emitted operand
1310 + * Means relocating the operand.
1313 +reginsert(char op, char* opnd)
1315 + register char *src;
1316 + register char *dst;
1317 + register char *place;
1319 + if (regcode == ®dummy) {
1327 + while (src > opnd)
1330 + place = opnd; /* Op node, where operand used to be. */
1337 + - regtail - set the next-pointer at the end of a node chain
1340 +regtail(char *p, char *val)
1342 + register char *scan;
1343 + register char *temp;
1344 + register int offset;
1346 + if (p == ®dummy)
1349 + /* Find last node. */
1352 + temp = regnext(scan);
1358 + if (OP(scan) == BACK)
1359 + offset = scan - val;
1361 + offset = val - scan;
1362 + *(scan+1) = (offset>>8)&0377;
1363 + *(scan+2) = offset&0377;
1367 + - regoptail - regtail on operand of first argument; nop if operandless
1370 +regoptail(char *p, char *val)
1372 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1373 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1375 + regtail(OPERAND(p), val);
1379 + * regexec and friends
1383 + * Global work variables for regexec().
1385 +static char *reginput; /* String-input pointer. */
1386 +static char *regbol; /* Beginning of input, for ^ check. */
1387 +static char **regstartp; /* Pointer to startp array. */
1388 +static char **regendp; /* Ditto for endp. */
1393 +STATIC int regtry(regexp *prog, char *string);
1394 +STATIC int regmatch(char *prog);
1395 +STATIC int regrepeat(char *p);
1398 +int regnarrate = 0;
1400 +STATIC char *regprop(char *op);
1404 + - regexec - match a regexp against a string
1407 +regexec(regexp *prog, char *string)
1411 + /* Be paranoid... */
1412 + if (prog == NULL || string == NULL) {
1413 + printk("<3>Regexp: NULL parameter\n");
1417 + /* Check validity of program. */
1418 + if (UCHARAT(prog->program) != MAGIC) {
1419 + printk("<3>Regexp: corrupted program\n");
1423 + /* If there is a "must appear" string, look for it. */
1424 + if (prog->regmust != NULL) {
1426 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1427 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1428 + break; /* Found it. */
1431 + if (s == NULL) /* Not present. */
1435 + /* Mark beginning of line for ^ . */
1438 + /* Simplest case: anchored match need be tried only once. */
1439 + if (prog->reganch)
1440 + return(regtry(prog, string));
1442 + /* Messy cases: unanchored match. */
1444 + if (prog->regstart != '\0')
1445 + /* We know what char it must start with. */
1446 + while ((s = strchr(s, prog->regstart)) != NULL) {
1447 + if (regtry(prog, s))
1452 + /* We don't -- general case. */
1454 + if (regtry(prog, s))
1456 + } while (*s++ != '\0');
1463 + - regtry - try match at specific point
1465 +static int /* 0 failure, 1 success */
1466 +regtry(regexp *prog, char *string)
1469 + register char **sp;
1470 + register char **ep;
1472 + reginput = string;
1473 + regstartp = prog->startp;
1474 + regendp = prog->endp;
1476 + sp = prog->startp;
1478 + for (i = NSUBEXP; i > 0; i--) {
1482 + if (regmatch(prog->program + 1)) {
1483 + prog->startp[0] = string;
1484 + prog->endp[0] = reginput;
1491 + - regmatch - main matching routine
1493 + * Conceptually the strategy is simple: check to see whether the current
1494 + * node matches, call self recursively to see whether the rest matches,
1495 + * and then act accordingly. In practice we make some effort to avoid
1496 + * recursion, in particular by going through "ordinary" nodes (that don't
1497 + * need to know whether the rest of the match failed) by a loop instead of
1500 +static int /* 0 failure, 1 success */
1501 +regmatch(char *prog)
1503 + register char *scan = prog; /* Current node. */
1504 + char *next; /* Next node. */
1507 + if (scan != NULL && regnarrate)
1508 + fprintf(stderr, "%s(\n", regprop(scan));
1510 + while (scan != NULL) {
1513 + fprintf(stderr, "%s...\n", regprop(scan));
1515 + next = regnext(scan);
1517 + switch (OP(scan)) {
1519 + if (reginput != regbol)
1523 + if (*reginput != '\0')
1527 + if (*reginput == '\0')
1533 + register char *opnd;
1535 + opnd = OPERAND(scan);
1536 + /* Inline the first character, for speed. */
1537 + if (*opnd != *reginput)
1539 + len = strlen(opnd);
1540 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1546 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1551 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1568 + register char *save;
1570 + no = OP(scan) - OPEN;
1573 + if (regmatch(next)) {
1575 + * Don't set startp if some later
1576 + * invocation of the same parentheses
1579 + if (regstartp[no] == NULL)
1580 + regstartp[no] = save;
1597 + register char *save;
1599 + no = OP(scan) - CLOSE;
1602 + if (regmatch(next)) {
1604 + * Don't set endp if some later
1605 + * invocation of the same parentheses
1608 + if (regendp[no] == NULL)
1609 + regendp[no] = save;
1616 + register char *save;
1618 + if (OP(next) != BRANCH) /* No choice. */
1619 + next = OPERAND(scan); /* Avoid recursion. */
1623 + if (regmatch(OPERAND(scan)))
1626 + scan = regnext(scan);
1627 + } while (scan != NULL && OP(scan) == BRANCH);
1635 + register char nextch;
1637 + register char *save;
1641 + * Lookahead to avoid useless match attempts
1642 + * when we know what character comes next.
1645 + if (OP(next) == EXACTLY)
1646 + nextch = *OPERAND(next);
1647 + min = (OP(scan) == STAR) ? 0 : 1;
1649 + no = regrepeat(OPERAND(scan));
1650 + while (no >= min) {
1651 + /* If it could work, try it. */
1652 + if (nextch == '\0' || *reginput == nextch)
1653 + if (regmatch(next))
1655 + /* Couldn't or didn't -- back up. */
1657 + reginput = save + no;
1663 + return(1); /* Success! */
1666 + printk("<3>Regexp: memory corruption\n");
1675 + * We get here only if there's trouble -- normally "case END" is
1676 + * the terminating point.
1678 + printk("<3>Regexp: corrupted pointers\n");
1683 + - regrepeat - repeatedly match something simple, report how many
1688 + register int count = 0;
1689 + register char *scan;
1690 + register char *opnd;
1693 + opnd = OPERAND(p);
1696 + count = strlen(scan);
1700 + while (*opnd == *scan) {
1706 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1712 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1717 + default: /* Oh dear. Called inappropriately. */
1718 + printk("<3>Regexp: internal foulup\n");
1719 + count = 0; /* Best compromise. */
1728 + - regnext - dig the "next" pointer out of a node
1733 + register int offset;
1735 + if (p == ®dummy)
1742 + if (OP(p) == BACK)
1750 +STATIC char *regprop();
1753 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1759 + register char op = EXACTLY; /* Arbitrary non-END op. */
1760 + register char *next;
1761 + /* extern char *strchr(); */
1764 + s = r->program + 1;
1765 + while (op != END) { /* While that wasn't END last time... */
1767 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1768 + next = regnext(s);
1769 + if (next == NULL) /* Next ptr. */
1772 + printf("(%d)", (s-r->program)+(next-s));
1774 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1775 + /* Literal string, where present. */
1776 + while (*s != '\0') {
1785 + /* Header fields of interest. */
1786 + if (r->regstart != '\0')
1787 + printf("start `%c' ", r->regstart);
1789 + printf("anchored ");
1790 + if (r->regmust != NULL)
1791 + printf("must have \"%s\"", r->regmust);
1796 + - regprop - printable representation of opcode
1803 + static char buf[BUFLEN];
1847 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1859 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1869 + printk("<3>Regexp: corrupted opcode\n");
1873 + strncat(buf, p, BUFLEN-strlen(buf));
1879 --- linux-2.6.11.3-stock/net/ipv4/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1880 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/regexp/regexp.h 2005-03-13 20:30:01.000000000 -0600
1883 + * Definitions etc. for regexp(3) routines.
1885 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1886 + * not the System V one.
1894 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1895 +which contains a version of this library, says:
1898 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1899 + * will not work properly.
1902 +However, it looks rather like this library is limited to 10. If you think
1903 +otherwise, let us know.
1907 +typedef struct regexp {
1908 + char *startp[NSUBEXP];
1909 + char *endp[NSUBEXP];
1910 + char regstart; /* Internal use only. */
1911 + char reganch; /* Internal use only. */
1912 + char *regmust; /* Internal use only. */
1913 + int regmlen; /* Internal use only. */
1914 + char program[1]; /* Unwarranted chumminess with compiler. */
1917 +regexp * regcomp(char *exp, int *patternsize);
1918 +int regexec(regexp *prog, char *string);
1919 +void regsub(regexp *prog, char *source, char *dest);
1920 +void regerror(char *s);
1923 --- linux-2.6.11.3-stock/net/ipv4/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1924 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/regexp/regmagic.h 2005-03-13 20:30:01.000000000 -0600
1927 + * The first byte of the regexp internal "program" is actually this magic
1928 + * number; the start node begins in the second byte.
1931 --- linux-2.6.11.3-stock/net/ipv4/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1932 +++ linux-2.6.11.3-layer7/net/ipv4/netfilter/regexp/regsub.c 2005-03-13 20:30:01.000000000 -0600
1936 + * @(#)regsub.c 1.3 of 2 April 86
1938 + * Copyright (c) 1986 by University of Toronto.
1939 + * Written by Henry Spencer. Not derived from licensed software.
1941 + * Permission is granted to anyone to use this software for any
1942 + * purpose on any computer system, and to redistribute it freely,
1943 + * subject to the following restrictions:
1945 + * 1. The author is not responsible for the consequences of use of
1946 + * this software, no matter how awful, even if they arise
1947 + * from defects in it.
1949 + * 2. The origin of this software must not be misrepresented, either
1950 + * by explicit claim or by omission.
1952 + * 3. Altered versions must be plainly marked as such, and must not
1953 + * be misrepresented as being the original software.
1956 + * This code was modified by Ethan Sommer to work within the kernel
1957 + * (it now uses kmalloc etc..)
1960 +#include "regexp.h"
1961 +#include "regmagic.h"
1962 +#include <linux/string.h>
1966 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1968 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1972 +//void regerror(char * s)
1974 +// printk("regexp(3): %s", s);
1975 +// /* NOTREACHED */
1980 + - regsub - perform substitutions after a regexp match
1983 +regsub(regexp * prog, char * source, char * dest)
1985 + register char *src;
1986 + register char *dst;
1991 + /* Not necessary and gcc doesn't like it -MLS */
1992 + /*extern char *strncpy();*/
1994 + if (prog == NULL || source == NULL || dest == NULL) {
1995 + regerror("NULL parm to regsub");
1998 + if (UCHARAT(prog->program) != MAGIC) {
1999 + regerror("damaged regexp fed to regsub");
2005 + while ((c = *src++) != '\0') {
2008 + else if (c == '\\' && '0' <= *src && *src <= '9')
2009 + no = *src++ - '0';
2013 + if (no < 0) { /* Ordinary character. */
2014 + if (c == '\\' && (*src == '\\' || *src == '&'))
2017 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2018 + len = prog->endp[no] - prog->startp[no];
2019 + (void) strncpy(dst, prog->startp[no], len);
2021 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2022 + regerror("damaged match string");