1 diff -urN linux.old/Documentation/Configure.help linux.dev/Documentation/Configure.help
2 --- linux.old/Documentation/Configure.help 2006-01-31 16:55:22.467939000 +0100
3 +++ linux.dev/Documentation/Configure.help 2006-01-31 16:58:24.751331500 +0100
4 @@ -29151,6 +29151,18 @@
8 +CONFIG_IP_NF_MATCH_LAYER7
9 + Say Y if you want to be able to classify connections (and their
10 + packets) based on regular expression matching of their application
11 + layer data. This is one way to classify applications such as
12 + peer-to-peer filesharing systems that do not always use the same
15 + To compile it as a module, choose M here. If unsure, say N.
17 +CONFIG_IP_NF_MATCH_LAYER7_DEBUG
18 + Say Y to get lots of debugging output.
21 # A couple of things I keep forgetting:
22 # capitalize: AppleTalk, Ethernet, DOS, DMA, FAT, FTP, Internet,
23 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h
24 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack.h 2005-11-16 20:12:54.000000000 +0100
25 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2006-01-31 16:58:24.775333000 +0100
28 #endif /* CONFIG_IP_NF_NAT_NEEDED */
30 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
32 + unsigned int numpackets; /* surely this is kept track of somewhere else, right? I can't find it... */
33 + char * app_proto; /* "http", "ftp", etc. NULL if unclassifed */
35 + /* the application layer data so far. NULL if ->numpackets > numpackets */
38 + unsigned int app_data_len;
43 /* get master conntrack via master expectation */
44 diff -urN linux.old/include/linux/netfilter_ipv4/ipt_layer7.h linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h
45 --- linux.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
46 +++ linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2006-01-31 19:29:03.774017500 +0100
49 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
50 + http://l7-filter.sf.net
52 + This program is free software; you can redistribute it and/or
53 + modify it under the terms of the GNU General Public License
54 + as published by the Free Software Foundation; either version
55 + 2 of the License, or (at your option) any later version.
56 + http://www.gnu.org/licenses/gpl.txt
59 +#ifndef _IPT_LAYER7_H
60 +#define _IPT_LAYER7_H
62 +#define MAX_PATTERN_LEN 8192
63 +#define MAX_PROTOCOL_LEN 256
65 +typedef char *(*proc_ipt_search) (char *, char, char *);
67 +struct ipt_layer7_info {
68 + char protocol[MAX_PROTOCOL_LEN];
70 + char pattern[MAX_PATTERN_LEN];
74 +#endif /* _IPT_LAYER7_H */
75 diff -urN linux.old/net/ipv4/netfilter/Config.in linux.dev/net/ipv4/netfilter/Config.in
76 --- linux.old/net/ipv4/netfilter/Config.in 2006-01-31 16:55:32.364558000 +0100
77 +++ linux.dev/net/ipv4/netfilter/Config.in 2006-01-31 16:58:24.803334750 +0100
79 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
80 dep_tristate ' Unclean match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_UNCLEAN $CONFIG_IP_NF_IPTABLES
81 dep_tristate ' Owner match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_OWNER $CONFIG_IP_NF_IPTABLES
82 + dep_tristate ' Layer 7 match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7 $CONFIG_IP_NF_CONNTRACK
83 + dep_mbool ' Layer 7 debugging output (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7_DEBUG $CONFIG_IP_NF_MATCH_LAYER7
87 dep_tristate ' Packet filtering' CONFIG_IP_NF_FILTER $CONFIG_IP_NF_IPTABLES
88 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
89 --- linux.old/net/ipv4/netfilter/Makefile 2006-01-31 16:55:32.372558000 +0100
90 +++ linux.dev/net/ipv4/netfilter/Makefile 2006-01-31 16:58:24.803334750 +0100
92 obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += ipt_conntrack.o
93 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
94 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
95 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
98 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
99 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_core.c linux.dev/net/ipv4/netfilter/ip_conntrack_core.c
100 --- linux.old/net/ipv4/netfilter/ip_conntrack_core.c 2005-04-04 03:42:20.000000000 +0200
101 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_core.c 2006-01-31 16:58:24.811335250 +0100
107 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
108 + if(ct->layer7.app_proto)
109 + kfree(ct->layer7.app_proto);
110 + if(ct->layer7.app_data)
111 + kfree(ct->layer7.app_data);
114 WRITE_UNLOCK(&ip_conntrack_lock);
117 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c
118 --- linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-04-04 03:42:20.000000000 +0200
119 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c 2006-01-31 16:58:24.815335500 +0100
121 len += sprintf(buffer + len, "[ASSURED] ");
122 len += sprintf(buffer + len, "use=%u ",
123 atomic_read(&conntrack->ct_general.use));
125 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
126 + if(conntrack->layer7.app_proto)
127 + len += sprintf(buffer + len, "l7proto=%s ",
128 + conntrack->layer7.app_proto);
131 len += sprintf(buffer + len, "\n");
134 diff -urN linux.old/net/ipv4/netfilter/ipt_layer7.c linux.dev/net/ipv4/netfilter/ipt_layer7.c
135 --- linux.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
136 +++ linux.dev/net/ipv4/netfilter/ipt_layer7.c 2006-01-31 19:31:38.591693000 +0100
139 + Kernel module to match application layer (OSI layer 7)
140 + data in connections.
142 + http://l7-filter.sf.net
144 + By Matthew Strait and Ethan Sommer, 2003-2005.
146 + This program is free software; you can redistribute it and/or
147 + modify it under the terms of the GNU General Public License
148 + as published by the Free Software Foundation; either version
149 + 2 of the License, or (at your option) any later version.
150 + http://www.gnu.org/licenses/gpl.txt
152 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
153 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
156 +#include <linux/module.h>
157 +#include <linux/skbuff.h>
158 +#include <linux/netfilter_ipv4/ip_conntrack.h>
159 +#include <linux/proc_fs.h>
160 +#include <linux/ctype.h>
162 +#include <net/tcp.h>
163 +#include <linux/netfilter_ipv4/lockhelp.h>
165 +#include "regexp/regexp.c"
167 +#include <linux/netfilter_ipv4/ipt_layer7.h>
168 +#include <linux/netfilter_ipv4/ip_tables.h>
170 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
171 +MODULE_LICENSE("GPL");
172 +MODULE_DESCRIPTION("iptables application layer match module");
174 +static int maxdatalen = 2048; // this is the default
175 +MODULE_PARM(maxdatalen,"i");
176 +MODULE_PARM_DESC(maxdatalen,"maximum bytes of data looked at by l7-filter");
178 +#if defined(CONFIG_IP_NF_MATCH_LAYER7_DEBUG)
179 + #define DPRINTK(format,args...) printk(format,##args)
181 + #define DPRINTK(format,args...)
184 +#define TOTAL_PACKETS master_conntrack->layer7.numpackets
186 +/* Number of packets whose data we look at.
187 +This can be modified through /proc/net/layer7_numpackets */
188 +static int num_packets = 10;
190 +static struct pattern_cache {
191 + char * regex_string;
193 + struct pattern_cache * next;
194 +} * first_pattern_cache = NULL;
196 +/* I'm new to locking. Here are my assumptions:
198 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
199 + if they did, nothing awful would happen.
201 +- This code will never be processing the same packet twice at the same time,
202 + because iptables rules are traversed in order.
204 +- It doesn't matter if two packets from different connections are in here at
205 + the same time, because they don't share any data.
207 +- It _does_ matter if two packets from the same connection are here at the same
208 + time. In this case, we have to protect the conntracks and the list of
211 +DECLARE_RWLOCK(ct_lock);
212 +DECLARE_LOCK(list_lock);
214 +#if CONFIG_IP_NF_MATCH_LAYER7_DEBUG
215 +/* Converts an unfriendly string into a friendly one by
216 +replacing unprintables with periods and all whitespace with " ". */
217 +static char * friendly_print(unsigned char * s)
219 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
223 + if (net_ratelimit())
224 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
228 + for(i = 0; i < strlen(s); i++){
229 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
230 + else if(isspace(s[i])) f[i] = ' ';
237 +static char dec2hex(int i)
241 + return (char)(i + '0');
244 + return (char)(i - 10 + 'a');
247 + if (net_ratelimit())
248 + printk("Problem in dec2hex\n");
253 +static char * hex_print(unsigned char * s)
255 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
259 + if (net_ratelimit())
260 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
264 + for(i = 0; i < strlen(s); i++) {
265 + g[i*3 ] = dec2hex(s[i]/16);
266 + g[i*3 + 1] = dec2hex(s[i]%16);
275 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
276 +over again, it make sense to cache the results. */
277 +static regexp * compile_and_cache(char * regex_string, char * protocol)
279 + struct pattern_cache * node = first_pattern_cache;
280 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
281 + struct pattern_cache * tmp;
284 + while (node != NULL) {
285 + if (!strcmp(node->regex_string, regex_string))
286 + return node->pattern;
288 + last_pattern_cache = node;/* points at the last non-NULL node */
292 + /* If we reach the end of the list, then we have not yet cached
293 + the pattern for this regex. Let's do that now.
294 + Be paranoid about running out of memory to avoid list corruption. */
295 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
298 + if (net_ratelimit())
299 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
303 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
304 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
307 + if(!tmp->regex_string || !tmp->pattern) {
308 + if (net_ratelimit())
309 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
310 + kfree(tmp->regex_string);
311 + kfree(tmp->pattern);
316 + /* Ok. The new node is all ready now. */
319 + if(first_pattern_cache == NULL) /* list is empty */
320 + first_pattern_cache = node; /* make node the beginning */
322 + last_pattern_cache->next = node; /* attach node to the end */
324 + /* copy the string and compile the regex */
325 + len = strlen(regex_string);
326 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
327 + node->pattern = regcomp(regex_string, &len);
328 + if ( !node->pattern ) {
329 + if (net_ratelimit())
330 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
331 + /* pattern is now cached as NULL, so we won't try again. */
334 + strcpy(node->regex_string, regex_string);
335 + return node->pattern;
338 +static int can_handle(const struct sk_buff *skb)
340 + if(!skb->nh.iph) /* not IP */
342 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
343 + skb->nh.iph->protocol != IPPROTO_UDP &&
344 + skb->nh.iph->protocol != IPPROTO_ICMP)
349 +/* Returns offset the into the skb->data that the application data starts */
350 +static int app_data_offset(const struct sk_buff *skb)
352 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
353 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
354 + int ip_hl = 4*skb->nh.iph->ihl;
356 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
357 + /* 12 == offset into TCP header for the header length field.
358 + Can't get this with skb->h.th->doff because the tcphdr
359 + struct doesn't get set when routing (this is confirmed to be
360 + true in Netfilter as well as QoS.) */
361 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
363 + return ip_hl + tcp_hl;
364 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
365 + return ip_hl + 8; /* UDP header is always 8 bytes */
366 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
367 + return ip_hl + 8; /* ICMP header is 8 bytes */
369 + if (net_ratelimit())
370 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
371 + return ip_hl + 8; /* something reasonable */
375 +/* handles whether there's a match when we aren't appending data anymore */
376 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
377 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
378 + struct ipt_layer7_info * info)
380 + /* If we're in here, throw the app data away */
381 + WRITE_LOCK(&ct_lock);
382 + if(master_conntrack->layer7.app_data != NULL) {
384 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
385 + if(!master_conntrack->layer7.app_proto) {
386 + char * f = friendly_print(master_conntrack->layer7.app_data);
387 + char * g = hex_print(master_conntrack->layer7.app_data);
388 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
392 + DPRINTK("In hex: %s\n", g);
397 + kfree(master_conntrack->layer7.app_data);
398 + master_conntrack->layer7.app_data = NULL; /* don't free again */
400 + WRITE_UNLOCK(&ct_lock);
402 + if(master_conntrack->layer7.app_proto){
403 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
404 + WRITE_LOCK(&ct_lock);
405 + if(!conntrack->layer7.app_proto) {
406 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
407 + if(!conntrack->layer7.app_proto){
408 + if (net_ratelimit())
409 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
410 + WRITE_UNLOCK(&ct_lock);
413 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
415 + WRITE_UNLOCK(&ct_lock);
417 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
420 + /* If not classified, set to "unknown" to distinguish from
421 + connections that are still being tested. */
422 + WRITE_LOCK(&ct_lock);
423 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
424 + if(!master_conntrack->layer7.app_proto){
425 + if (net_ratelimit())
426 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
427 + WRITE_UNLOCK(&ct_lock);
430 + strcpy(master_conntrack->layer7.app_proto, "unknown");
431 + WRITE_UNLOCK(&ct_lock);
436 +static int add_datastr(char *target, int offset, char *app_data, int len)
440 + /* Strip nulls. Make everything lower case (our regex lib doesn't
441 + do case insensitivity). Add it to the end of the current data. */
442 + for(i = 0; i < maxdatalen-offset-1 && i < len; i++) {
443 + if(app_data[i] != '\0') {
444 + target[length+offset] =
445 + /* the kernel version of tolower mungs 'upper ascii' */
446 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
451 + target[length+offset] = '\0';
456 +/* add the new app data to the conntrack. Return number of bytes added. */
457 +static int add_data(struct ip_conntrack * master_conntrack,
458 + char * app_data, int appdatalen)
462 + length = add_datastr(master_conntrack->layer7.app_data, master_conntrack->layer7.app_data_len, app_data, appdatalen);
463 + master_conntrack->layer7.app_data_len += length;
468 +/* Returns true on match and false otherwise. */
469 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
470 + const struct net_device *out, const void *matchinfo,
471 + int offset, int *hotdrop)
473 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
474 + enum ip_conntrack_info master_ctinfo, ctinfo;
475 + struct ip_conntrack *master_conntrack, *conntrack;
476 + unsigned char *app_data, *tmp_data;
477 + unsigned int pattern_result, appdatalen;
478 + regexp * comppattern;
480 + if(!can_handle(skb)){
481 + DPRINTK("layer7: This is some protocol I can't handle.\n");
482 + return info->invert;
485 + /* Treat the parent and all its children together as one connection,
486 + except for the purpose of setting conntrack->layer7.app_proto in the
487 + actual connection. This makes /proc/net/ip_conntrack somewhat more
489 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
490 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
491 + DPRINTK("layer7: packet is not from a known connection, giving up.\n");
492 + return info->invert;
495 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
496 + while (master_ct(master_conntrack) != NULL)
497 + master_conntrack = master_ct(master_conntrack);
500 + WRITE_LOCK(&ct_lock);
501 + master_conntrack->layer7.numpackets++;/*starts at 0 via memset*/
502 + WRITE_UNLOCK(&ct_lock);
505 + /* if we've classified it or seen too many packets */
506 + if(!info->pkt && (TOTAL_PACKETS > num_packets ||
507 + master_conntrack->layer7.app_proto)) {
509 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
511 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
512 + rules. I'm not sure that using cb for this purpose is correct, although
513 + it says "put your private variables there". But it doesn't look like it
514 + is being used for anything else in the skbs that make it here. How can
515 + I write to cb without making the compiler angry? */
516 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
518 + return (pattern_result ^ info->invert);
521 + if(skb_is_nonlinear(skb)){
522 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
523 + if (net_ratelimit())
524 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
525 + return info->invert;
529 + /* now that the skb is linearized, it's safe to set these. */
530 + app_data = skb->data + app_data_offset(skb);
531 + appdatalen = skb->tail - app_data;
533 + LOCK_BH(&list_lock);
534 + /* the return value gets checked later, when we're ready to use it */
535 + comppattern = compile_and_cache(info->pattern, info->protocol);
536 + UNLOCK_BH(&list_lock);
539 + tmp_data = kmalloc(maxdatalen, GFP_ATOMIC);
541 + if (net_ratelimit())
542 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
543 + return info->invert;
546 + tmp_data[0] = '\0';
547 + add_datastr(tmp_data, 0, app_data, appdatalen);
548 + pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
552 + return (pattern_result ^ info->invert);
555 + /* On the first packet of a connection, allocate space for app data */
556 + WRITE_LOCK(&ct_lock);
557 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
558 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
559 + if(!master_conntrack->layer7.app_data){
560 + if (net_ratelimit())
561 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
562 + WRITE_UNLOCK(&ct_lock);
563 + return info->invert;
566 + master_conntrack->layer7.app_data[0] = '\0';
568 + WRITE_UNLOCK(&ct_lock);
570 + /* Can be here, but unallocated, if numpackets is increased near
571 + the beginning of a connection */
572 + if(master_conntrack->layer7.app_data == NULL)
573 + return (info->invert); /* unmatched */
577 + WRITE_LOCK(&ct_lock);
578 + newbytes = add_data(master_conntrack, app_data, appdatalen);
579 + WRITE_UNLOCK(&ct_lock);
581 + if(newbytes == 0) { /* didn't add any data */
583 + /* Didn't match before, not going to match now */
584 + return info->invert;
588 + /* If looking for "unknown", then never match. "Unknown" means that
589 + we've given up; we're still trying with these packets. */
590 + if(!strcmp(info->protocol, "unknown")) {
591 + pattern_result = 0;
592 + /* If the regexp failed to compile, don't bother running it */
593 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
594 + DPRINTK("layer7: regexec positive: %s!\n", info->protocol);
595 + pattern_result = 1;
596 + } else pattern_result = 0;
598 + if(pattern_result) {
599 + WRITE_LOCK(&ct_lock);
600 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
601 + if(!master_conntrack->layer7.app_proto){
602 + if (net_ratelimit())
603 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
604 + WRITE_UNLOCK(&ct_lock);
605 + return (pattern_result ^ info->invert);
607 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
608 + WRITE_UNLOCK(&ct_lock);
611 + /* mark the packet seen */
614 + return (pattern_result ^ info->invert);
617 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
618 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
620 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
625 +static struct ipt_match layer7_match = {
628 + .checkentry = &checkentry,
632 +/* taken from drivers/video/modedb.c */
633 +static int my_atoi(const char *s)
640 + val = 10*val+(*s-'0');
648 +/* write out num_packets to userland. */
649 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
650 + int* eof, void * data)
652 + if(num_packets > 99 && net_ratelimit())
653 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
655 + page[0] = num_packets/10 + '0';
656 + page[1] = num_packets%10 + '0';
665 +/* Read in num_packets from userland */
666 +static int layer7_write_proc(struct file* file, const char* buffer,
667 + unsigned long count, void *data)
669 + char * foo = kmalloc(count, GFP_ATOMIC);
672 + if (net_ratelimit())
673 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
677 + copy_from_user(foo, buffer, count);
679 + num_packets = my_atoi(foo);
682 + /* This has an arbitrary limit to make the math easier. I'm lazy.
683 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
684 + if(num_packets > 99) {
685 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
687 + } else if(num_packets < 1) {
688 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
695 +/* register the proc file */
696 +static void layer7_init_proc(void)
698 + struct proc_dir_entry* entry;
699 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
700 + entry->read_proc = layer7_read_proc;
701 + entry->write_proc = layer7_write_proc;
704 +static void layer7_cleanup_proc(void)
706 + remove_proc_entry("layer7_numpackets", proc_net);
709 +static int __init init(void)
711 + layer7_init_proc();
712 + if(maxdatalen < 1) {
713 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
716 + /* This is not a hard limit. It's just here to prevent people from
717 + bringing their slow machines to a grinding halt. */
718 + else if(maxdatalen > 65536) {
719 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
720 + maxdatalen = 65536;
722 + return ipt_register_match(&layer7_match);
725 +static void __exit fini(void)
727 + layer7_cleanup_proc();
728 + ipt_unregister_match(&layer7_match);
733 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.c linux.dev/net/ipv4/netfilter/regexp/regexp.c
734 --- linux.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
735 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.c 2006-01-31 16:58:24.819335750 +0100
738 + * regcomp and regexec -- regsub and regerror are elsewhere
739 + * @(#)regexp.c 1.3 of 18 April 87
741 + * Copyright (c) 1986 by University of Toronto.
742 + * Written by Henry Spencer. Not derived from licensed software.
744 + * Permission is granted to anyone to use this software for any
745 + * purpose on any computer system, and to redistribute it freely,
746 + * subject to the following restrictions:
748 + * 1. The author is not responsible for the consequences of use of
749 + * this software, no matter how awful, even if they arise
750 + * from defects in it.
752 + * 2. The origin of this software must not be misrepresented, either
753 + * by explicit claim or by omission.
755 + * 3. Altered versions must be plainly marked as such, and must not
756 + * be misrepresented as being the original software.
758 + * Beware that some of this code is subtly aware of the way operator
759 + * precedence is structured in regular expressions. Serious changes in
760 + * regular-expression syntax might require a total rethink.
762 + * This code was modified by Ethan Sommer to work within the kernel
763 + * (it now uses kmalloc etc..)
765 + * Modified slightly by Matthew Strait to use more modern C.
769 +#include "regmagic.h"
771 +/* added by ethan and matt. Lets it work in both kernel and user space.
772 +(So iptables can use it, for instance.) Yea, it goes both ways... */
774 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
776 + #define printk(format,args...) printf(format,##args)
779 +void regerror(char * s)
781 + printk("<3>Regexp: %s\n", s);
786 + * The "internal use only" fields in regexp.h are present to pass info from
787 + * compile to execute that permits the execute phase to run lots faster on
788 + * simple cases. They are:
790 + * regstart char that must begin a match; '\0' if none obvious
791 + * reganch is the match anchored (at beginning-of-line only)?
792 + * regmust string (pointer into program) that match must include, or NULL
793 + * regmlen length of regmust string
795 + * Regstart and reganch permit very fast decisions on suitable starting points
796 + * for a match, cutting down the work a lot. Regmust permits fast rejection
797 + * of lines that cannot possibly match. The regmust tests are costly enough
798 + * that regcomp() supplies a regmust only if the r.e. contains something
799 + * potentially expensive (at present, the only such thing detected is * or +
800 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
801 + * supplied because the test in regexec() needs it and regcomp() is computing
806 + * Structure for regexp "program". This is essentially a linear encoding
807 + * of a nondeterministic finite-state machine (aka syntax charts or
808 + * "railroad normal form" in parsing technology). Each node is an opcode
809 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
810 + * all nodes except BRANCH implement concatenation; a "next" pointer with
811 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
812 + * have one of the subtle syntax dependencies: an individual BRANCH (as
813 + * opposed to a collection of them) is never concatenated with anything
814 + * because of operator precedence.) The operand of some types of node is
815 + * a literal string; for others, it is a node leading into a sub-FSM. In
816 + * particular, the operand of a BRANCH node is the first node of the branch.
817 + * (NB this is *not* a tree structure: the tail of the branch connects
818 + * to the thing following the set of BRANCHes.) The opcodes are:
821 +/* definition number opnd? meaning */
822 +#define END 0 /* no End of program. */
823 +#define BOL 1 /* no Match "" at beginning of line. */
824 +#define EOL 2 /* no Match "" at end of line. */
825 +#define ANY 3 /* no Match any one character. */
826 +#define ANYOF 4 /* str Match any character in this string. */
827 +#define ANYBUT 5 /* str Match any character not in this string. */
828 +#define BRANCH 6 /* node Match this alternative, or the next... */
829 +#define BACK 7 /* no Match "", "next" ptr points backward. */
830 +#define EXACTLY 8 /* str Match this string. */
831 +#define NOTHING 9 /* no Match empty string. */
832 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
833 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
834 +#define OPEN 20 /* no Mark this point in input as start of #n. */
835 + /* OPEN+1 is number 1, etc. */
836 +#define CLOSE 30 /* no Analogous to OPEN. */
841 + * BRANCH The set of branches constituting a single choice are hooked
842 + * together with their "next" pointers, since precedence prevents
843 + * anything being concatenated to any individual branch. The
844 + * "next" pointer of the last BRANCH in a choice points to the
845 + * thing following the whole choice. This is also where the
846 + * final "next" pointer of each individual branch points; each
847 + * branch starts with the operand node of a BRANCH node.
849 + * BACK Normal "next" pointers all implicitly point forward; BACK
850 + * exists to make loop structures possible.
852 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
853 + * BRANCH structures using BACK. Simple cases (one character
854 + * per match) are implemented with STAR and PLUS for speed
855 + * and to minimize recursive plunges.
857 + * OPEN,CLOSE ...are numbered at compile time.
861 + * A node is one char of opcode followed by two chars of "next" pointer.
862 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
863 + * value is a positive offset from the opcode of the node containing it.
864 + * An operand, if any, simply follows the node. (Note that much of the
865 + * code generation knows about this implicit relationship.)
867 + * Using two bytes for the "next" pointer is vast overkill for most things,
868 + * but allows patterns to get big without disasters.
870 +#define OP(p) (*(p))
871 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
872 +#define OPERAND(p) ((p) + 3)
875 + * See regmagic.h for one further detail of program structure.
880 + * Utility definitions.
883 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
885 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
888 +#define FAIL(m) { regerror(m); return(NULL); }
889 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
890 +#define META "^$.[()|?+*\\"
893 + * Flags to be passed up and down.
895 +#define HASWIDTH 01 /* Known never to match null string. */
896 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
897 +#define SPSTART 04 /* Starts with * or +. */
898 +#define WORST 0 /* Worst case. */
901 + * Global work variables for regcomp().
903 +static char *regparse; /* Input-scan pointer. */
904 +static int regnpar; /* () count. */
905 +static char regdummy;
906 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
907 +static long regsize; /* Code size. */
910 + * Forward declarations for regcomp()'s friends.
913 +#define STATIC static
915 +STATIC char *reg(int paren,int *flagp);
916 +STATIC char *regbranch(int *flagp);
917 +STATIC char *regpiece(int *flagp);
918 +STATIC char *regatom(int *flagp);
919 +STATIC char *regnode(char op);
920 +STATIC char *regnext(char *p);
921 +STATIC void regc(char b);
922 +STATIC void reginsert(char op, char *opnd);
923 +STATIC void regtail(char *p, char *val);
924 +STATIC void regoptail(char *p, char *val);
927 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
934 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
935 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
936 + if (*scan1 == *scan2++)
944 + - regcomp - compile a regular expression into internal code
946 + * We can't allocate space until we know how big the compiled form will be,
947 + * but we can't compile it (and thus know how big it is) until we've got a
948 + * place to put the code. So we cheat: we compile it twice, once with code
949 + * generation turned off and size counting turned on, and once "for real".
950 + * This also means that we don't allocate space until we are sure that the
951 + * thing really will compile successfully, and we never have to move the
952 + * code and thus invalidate pointers into it. (Note that it has to be in
953 + * one piece because free() must be able to free it all.)
955 + * Beware that the optimization-preparation code in here knows about some
956 + * of the structure of the compiled regexp.
959 +regcomp(char *exp,int *patternsize)
961 + register regexp *r;
962 + register char *scan;
963 + register char *longest;
966 + /* commented out by ethan
967 + extern char *malloc();
971 + FAIL("NULL argument");
973 + /* First pass: determine size, legality. */
977 + regcode = ®dummy;
979 + if (reg(0, &flags) == NULL)
982 + /* Small enough for pointer-storage convention? */
983 + if (regsize >= 32767L) /* Probably could be 65535L. */
984 + FAIL("regexp too big");
986 + /* Allocate space. */
987 + *patternsize=sizeof(regexp) + (unsigned)regsize;
988 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
990 + FAIL("out of space");
992 + /* Second pass: emit code. */
995 + regcode = r->program;
997 + if (reg(0, &flags) == NULL)
1000 + /* Dig out information for optimizations. */
1001 + r->regstart = '\0'; /* Worst-case defaults. */
1003 + r->regmust = NULL;
1005 + scan = r->program+1; /* First BRANCH. */
1006 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
1007 + scan = OPERAND(scan);
1009 + /* Starting-point info. */
1010 + if (OP(scan) == EXACTLY)
1011 + r->regstart = *OPERAND(scan);
1012 + else if (OP(scan) == BOL)
1016 + * If there's something expensive in the r.e., find the
1017 + * longest literal string that must appear and make it the
1018 + * regmust. Resolve ties in favor of later strings, since
1019 + * the regstart check works with the beginning of the r.e.
1020 + * and avoiding duplication strengthens checking. Not a
1021 + * strong reason, but sufficient in the absence of others.
1023 + if (flags&SPSTART) {
1026 + for (; scan != NULL; scan = regnext(scan))
1027 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1028 + longest = OPERAND(scan);
1029 + len = strlen(OPERAND(scan));
1031 + r->regmust = longest;
1040 + - reg - regular expression, i.e. main body or parenthesized thing
1042 + * Caller must absorb opening parenthesis.
1044 + * Combining parenthesis handling with the base level of regular expression
1045 + * is a trifle forced, but the need to tie the tails of the branches to what
1046 + * follows makes it hard to avoid.
1049 +reg(int paren, int *flagp /* Parenthesized? */ )
1051 + register char *ret;
1052 + register char *br;
1053 + register char *ender;
1054 + register int parno = 0; /* 0 makes gcc happy */
1057 + *flagp = HASWIDTH; /* Tentatively. */
1059 + /* Make an OPEN node, if parenthesized. */
1061 + if (regnpar >= NSUBEXP)
1062 + FAIL("too many ()");
1065 + ret = regnode(OPEN+parno);
1069 + /* Pick up the branches, linking them together. */
1070 + br = regbranch(&flags);
1074 + regtail(ret, br); /* OPEN -> first. */
1077 + if (!(flags&HASWIDTH))
1078 + *flagp &= ~HASWIDTH;
1079 + *flagp |= flags&SPSTART;
1080 + while (*regparse == '|') {
1082 + br = regbranch(&flags);
1085 + regtail(ret, br); /* BRANCH -> BRANCH. */
1086 + if (!(flags&HASWIDTH))
1087 + *flagp &= ~HASWIDTH;
1088 + *flagp |= flags&SPSTART;
1091 + /* Make a closing node, and hook it on the end. */
1092 + ender = regnode((paren) ? CLOSE+parno : END);
1093 + regtail(ret, ender);
1095 + /* Hook the tails of the branches to the closing node. */
1096 + for (br = ret; br != NULL; br = regnext(br))
1097 + regoptail(br, ender);
1099 + /* Check for proper termination. */
1100 + if (paren && *regparse++ != ')') {
1101 + FAIL("unmatched ()");
1102 + } else if (!paren && *regparse != '\0') {
1103 + if (*regparse == ')') {
1104 + FAIL("unmatched ()");
1106 + FAIL("junk on end"); /* "Can't happen". */
1114 + - regbranch - one alternative of an | operator
1116 + * Implements the concatenation operator.
1119 +regbranch(int *flagp)
1121 + register char *ret;
1122 + register char *chain;
1123 + register char *latest;
1126 + *flagp = WORST; /* Tentatively. */
1128 + ret = regnode(BRANCH);
1130 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1131 + latest = regpiece(&flags);
1132 + if (latest == NULL)
1134 + *flagp |= flags&HASWIDTH;
1135 + if (chain == NULL) /* First piece. */
1136 + *flagp |= flags&SPSTART;
1138 + regtail(chain, latest);
1141 + if (chain == NULL) /* Loop ran zero times. */
1142 + (void) regnode(NOTHING);
1148 + - regpiece - something followed by possible [*+?]
1150 + * Note that the branching code sequences used for ? and the general cases
1151 + * of * and + are somewhat optimized: they use the same NOTHING node as
1152 + * both the endmarker for their branch list and the body of the last branch.
1153 + * It might seem that this node could be dispensed with entirely, but the
1154 + * endmarker role is not redundant.
1157 +regpiece(int *flagp)
1159 + register char *ret;
1161 + register char *next;
1164 + ret = regatom(&flags);
1169 + if (!ISMULT(op)) {
1174 + if (!(flags&HASWIDTH) && op != '?')
1175 + FAIL("*+ operand could be empty");
1176 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1178 + if (op == '*' && (flags&SIMPLE))
1179 + reginsert(STAR, ret);
1180 + else if (op == '*') {
1181 + /* Emit x* as (x&|), where & means "self". */
1182 + reginsert(BRANCH, ret); /* Either x */
1183 + regoptail(ret, regnode(BACK)); /* and loop */
1184 + regoptail(ret, ret); /* back */
1185 + regtail(ret, regnode(BRANCH)); /* or */
1186 + regtail(ret, regnode(NOTHING)); /* null. */
1187 + } else if (op == '+' && (flags&SIMPLE))
1188 + reginsert(PLUS, ret);
1189 + else if (op == '+') {
1190 + /* Emit x+ as x(&|), where & means "self". */
1191 + next = regnode(BRANCH); /* Either */
1192 + regtail(ret, next);
1193 + regtail(regnode(BACK), ret); /* loop back */
1194 + regtail(next, regnode(BRANCH)); /* or */
1195 + regtail(ret, regnode(NOTHING)); /* null. */
1196 + } else if (op == '?') {
1197 + /* Emit x? as (x|) */
1198 + reginsert(BRANCH, ret); /* Either x */
1199 + regtail(ret, regnode(BRANCH)); /* or */
1200 + next = regnode(NOTHING); /* null. */
1201 + regtail(ret, next);
1202 + regoptail(ret, next);
1205 + if (ISMULT(*regparse))
1206 + FAIL("nested *?+");
1212 + - regatom - the lowest level
1214 + * Optimization: gobbles an entire sequence of ordinary characters so that
1215 + * it can turn them into a single node, which is smaller to store and
1216 + * faster to run. Backslashed characters are exceptions, each becoming a
1217 + * separate node; the code is simpler that way and it's not worth fixing.
1220 +regatom(int *flagp)
1222 + register char *ret;
1225 + *flagp = WORST; /* Tentatively. */
1227 + switch (*regparse++) {
1229 + ret = regnode(BOL);
1232 + ret = regnode(EOL);
1235 + ret = regnode(ANY);
1236 + *flagp |= HASWIDTH|SIMPLE;
1239 + register int class;
1240 + register int classend;
1242 + if (*regparse == '^') { /* Complement of range. */
1243 + ret = regnode(ANYBUT);
1246 + ret = regnode(ANYOF);
1247 + if (*regparse == ']' || *regparse == '-')
1248 + regc(*regparse++);
1249 + while (*regparse != '\0' && *regparse != ']') {
1250 + if (*regparse == '-') {
1252 + if (*regparse == ']' || *regparse == '\0')
1255 + class = UCHARAT(regparse-2)+1;
1256 + classend = UCHARAT(regparse);
1257 + if (class > classend+1)
1258 + FAIL("invalid [] range");
1259 + for (; class <= classend; class++)
1264 + regc(*regparse++);
1267 + if (*regparse != ']')
1268 + FAIL("unmatched []");
1270 + *flagp |= HASWIDTH|SIMPLE;
1274 + ret = reg(1, &flags);
1277 + *flagp |= flags&(HASWIDTH|SPSTART);
1282 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1287 + FAIL("?+* follows nothing");
1290 + if (*regparse == '\0')
1291 + FAIL("trailing \\");
1292 + ret = regnode(EXACTLY);
1293 + regc(*regparse++);
1295 + *flagp |= HASWIDTH|SIMPLE;
1299 + register char ender;
1302 + len = my_strcspn((const char *)regparse, (const char *)META);
1304 + FAIL("internal disaster");
1305 + ender = *(regparse+len);
1306 + if (len > 1 && ISMULT(ender))
1307 + len--; /* Back off clear of ?+* operand. */
1308 + *flagp |= HASWIDTH;
1311 + ret = regnode(EXACTLY);
1313 + regc(*regparse++);
1325 + - regnode - emit a node
1327 +static char * /* Location. */
1330 + register char *ret;
1331 + register char *ptr;
1334 + if (ret == ®dummy) {
1341 + *ptr++ = '\0'; /* Null "next" pointer. */
1349 + - regc - emit (if appropriate) a byte of code
1354 + if (regcode != ®dummy)
1361 + - reginsert - insert an operator in front of already-emitted operand
1363 + * Means relocating the operand.
1366 +reginsert(char op, char* opnd)
1368 + register char *src;
1369 + register char *dst;
1370 + register char *place;
1372 + if (regcode == ®dummy) {
1380 + while (src > opnd)
1383 + place = opnd; /* Op node, where operand used to be. */
1390 + - regtail - set the next-pointer at the end of a node chain
1393 +regtail(char *p, char *val)
1395 + register char *scan;
1396 + register char *temp;
1397 + register int offset;
1399 + if (p == ®dummy)
1402 + /* Find last node. */
1405 + temp = regnext(scan);
1411 + if (OP(scan) == BACK)
1412 + offset = scan - val;
1414 + offset = val - scan;
1415 + *(scan+1) = (offset>>8)&0377;
1416 + *(scan+2) = offset&0377;
1420 + - regoptail - regtail on operand of first argument; nop if operandless
1423 +regoptail(char *p, char *val)
1425 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1426 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1428 + regtail(OPERAND(p), val);
1432 + * regexec and friends
1436 + * Global work variables for regexec().
1438 +static char *reginput; /* String-input pointer. */
1439 +static char *regbol; /* Beginning of input, for ^ check. */
1440 +static char **regstartp; /* Pointer to startp array. */
1441 +static char **regendp; /* Ditto for endp. */
1446 +STATIC int regtry(regexp *prog, char *string);
1447 +STATIC int regmatch(char *prog);
1448 +STATIC int regrepeat(char *p);
1451 +int regnarrate = 0;
1453 +STATIC char *regprop(char *op);
1457 + - regexec - match a regexp against a string
1460 +regexec(regexp *prog, char *string)
1464 + /* Be paranoid... */
1465 + if (prog == NULL || string == NULL) {
1466 + printk("<3>Regexp: NULL parameter\n");
1470 + /* Check validity of program. */
1471 + if (UCHARAT(prog->program) != MAGIC) {
1472 + printk("<3>Regexp: corrupted program\n");
1476 + /* If there is a "must appear" string, look for it. */
1477 + if (prog->regmust != NULL) {
1479 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1480 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1481 + break; /* Found it. */
1484 + if (s == NULL) /* Not present. */
1488 + /* Mark beginning of line for ^ . */
1491 + /* Simplest case: anchored match need be tried only once. */
1492 + if (prog->reganch)
1493 + return(regtry(prog, string));
1495 + /* Messy cases: unanchored match. */
1497 + if (prog->regstart != '\0')
1498 + /* We know what char it must start with. */
1499 + while ((s = strchr(s, prog->regstart)) != NULL) {
1500 + if (regtry(prog, s))
1505 + /* We don't -- general case. */
1507 + if (regtry(prog, s))
1509 + } while (*s++ != '\0');
1516 + - regtry - try match at specific point
1518 +static int /* 0 failure, 1 success */
1519 +regtry(regexp *prog, char *string)
1522 + register char **sp;
1523 + register char **ep;
1525 + reginput = string;
1526 + regstartp = prog->startp;
1527 + regendp = prog->endp;
1529 + sp = prog->startp;
1531 + for (i = NSUBEXP; i > 0; i--) {
1535 + if (regmatch(prog->program + 1)) {
1536 + prog->startp[0] = string;
1537 + prog->endp[0] = reginput;
1544 + - regmatch - main matching routine
1546 + * Conceptually the strategy is simple: check to see whether the current
1547 + * node matches, call self recursively to see whether the rest matches,
1548 + * and then act accordingly. In practice we make some effort to avoid
1549 + * recursion, in particular by going through "ordinary" nodes (that don't
1550 + * need to know whether the rest of the match failed) by a loop instead of
1553 +static int /* 0 failure, 1 success */
1554 +regmatch(char *prog)
1556 + register char *scan = prog; /* Current node. */
1557 + char *next; /* Next node. */
1560 + if (scan != NULL && regnarrate)
1561 + fprintf(stderr, "%s(\n", regprop(scan));
1563 + while (scan != NULL) {
1566 + fprintf(stderr, "%s...\n", regprop(scan));
1568 + next = regnext(scan);
1570 + switch (OP(scan)) {
1572 + if (reginput != regbol)
1576 + if (*reginput != '\0')
1580 + if (*reginput == '\0')
1586 + register char *opnd;
1588 + opnd = OPERAND(scan);
1589 + /* Inline the first character, for speed. */
1590 + if (*opnd != *reginput)
1592 + len = strlen(opnd);
1593 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1599 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1604 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1621 + register char *save;
1623 + no = OP(scan) - OPEN;
1626 + if (regmatch(next)) {
1628 + * Don't set startp if some later
1629 + * invocation of the same parentheses
1632 + if (regstartp[no] == NULL)
1633 + regstartp[no] = save;
1650 + register char *save;
1652 + no = OP(scan) - CLOSE;
1655 + if (regmatch(next)) {
1657 + * Don't set endp if some later
1658 + * invocation of the same parentheses
1661 + if (regendp[no] == NULL)
1662 + regendp[no] = save;
1669 + register char *save;
1671 + if (OP(next) != BRANCH) /* No choice. */
1672 + next = OPERAND(scan); /* Avoid recursion. */
1676 + if (regmatch(OPERAND(scan)))
1679 + scan = regnext(scan);
1680 + } while (scan != NULL && OP(scan) == BRANCH);
1688 + register char nextch;
1690 + register char *save;
1694 + * Lookahead to avoid useless match attempts
1695 + * when we know what character comes next.
1698 + if (OP(next) == EXACTLY)
1699 + nextch = *OPERAND(next);
1700 + min = (OP(scan) == STAR) ? 0 : 1;
1702 + no = regrepeat(OPERAND(scan));
1703 + while (no >= min) {
1704 + /* If it could work, try it. */
1705 + if (nextch == '\0' || *reginput == nextch)
1706 + if (regmatch(next))
1708 + /* Couldn't or didn't -- back up. */
1710 + reginput = save + no;
1716 + return(1); /* Success! */
1719 + printk("<3>Regexp: memory corruption\n");
1728 + * We get here only if there's trouble -- normally "case END" is
1729 + * the terminating point.
1731 + printk("<3>Regexp: corrupted pointers\n");
1736 + - regrepeat - repeatedly match something simple, report how many
1741 + register int count = 0;
1742 + register char *scan;
1743 + register char *opnd;
1746 + opnd = OPERAND(p);
1749 + count = strlen(scan);
1753 + while (*opnd == *scan) {
1759 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1765 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1770 + default: /* Oh dear. Called inappropriately. */
1771 + printk("<3>Regexp: internal foulup\n");
1772 + count = 0; /* Best compromise. */
1781 + - regnext - dig the "next" pointer out of a node
1786 + register int offset;
1788 + if (p == ®dummy)
1795 + if (OP(p) == BACK)
1803 +STATIC char *regprop();
1806 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1812 + register char op = EXACTLY; /* Arbitrary non-END op. */
1813 + register char *next;
1814 + /* extern char *strchr(); */
1817 + s = r->program + 1;
1818 + while (op != END) { /* While that wasn't END last time... */
1820 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1821 + next = regnext(s);
1822 + if (next == NULL) /* Next ptr. */
1825 + printf("(%d)", (s-r->program)+(next-s));
1827 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1828 + /* Literal string, where present. */
1829 + while (*s != '\0') {
1838 + /* Header fields of interest. */
1839 + if (r->regstart != '\0')
1840 + printf("start `%c' ", r->regstart);
1842 + printf("anchored ");
1843 + if (r->regmust != NULL)
1844 + printf("must have \"%s\"", r->regmust);
1849 + - regprop - printable representation of opcode
1856 + static char buf[BUFLEN];
1900 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1912 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1922 + printk("<3>Regexp: corrupted opcode\n");
1926 + strncat(buf, p, BUFLEN-strlen(buf));
1932 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.h linux.dev/net/ipv4/netfilter/regexp/regexp.h
1933 --- linux.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1934 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.h 2006-01-31 16:58:24.819335750 +0100
1937 + * Definitions etc. for regexp(3) routines.
1939 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1940 + * not the System V one.
1947 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1948 +which contains a version of this library, says:
1951 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1952 + * will not work properly.
1955 +However, it looks rather like this library is limited to 10. If you think
1956 +otherwise, let us know.
1960 +typedef struct regexp {
1961 + char *startp[NSUBEXP];
1962 + char *endp[NSUBEXP];
1963 + char regstart; /* Internal use only. */
1964 + char reganch; /* Internal use only. */
1965 + char *regmust; /* Internal use only. */
1966 + int regmlen; /* Internal use only. */
1967 + char program[1]; /* Unwarranted chumminess with compiler. */
1970 +regexp * regcomp(char *exp, int *patternsize);
1971 +int regexec(regexp *prog, char *string);
1972 +void regsub(regexp *prog, char *source, char *dest);
1973 +void regerror(char *s);
1976 diff -urN linux.old/net/ipv4/netfilter/regexp/regmagic.h linux.dev/net/ipv4/netfilter/regexp/regmagic.h
1977 --- linux.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1978 +++ linux.dev/net/ipv4/netfilter/regexp/regmagic.h 2006-01-31 16:58:24.823336000 +0100
1981 + * The first byte of the regexp internal "program" is actually this magic
1982 + * number; the start node begins in the second byte.
1985 diff -urN linux.old/net/ipv4/netfilter/regexp/regsub.c linux.dev/net/ipv4/netfilter/regexp/regsub.c
1986 --- linux.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1987 +++ linux.dev/net/ipv4/netfilter/regexp/regsub.c 2006-01-31 16:58:24.823336000 +0100
1991 + * @(#)regsub.c 1.3 of 2 April 86
1993 + * Copyright (c) 1986 by University of Toronto.
1994 + * Written by Henry Spencer. Not derived from licensed software.
1996 + * Permission is granted to anyone to use this software for any
1997 + * purpose on any computer system, and to redistribute it freely,
1998 + * subject to the following restrictions:
2000 + * 1. The author is not responsible for the consequences of use of
2001 + * this software, no matter how awful, even if they arise
2002 + * from defects in it.
2004 + * 2. The origin of this software must not be misrepresented, either
2005 + * by explicit claim or by omission.
2007 + * 3. Altered versions must be plainly marked as such, and must not
2008 + * be misrepresented as being the original software.
2011 + * This code was modified by Ethan Sommer to work within the kernel
2012 + * (it now uses kmalloc etc..)
2015 +#include "regexp.h"
2016 +#include "regmagic.h"
2017 +#include <linux/string.h>
2021 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
2023 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
2027 +//void regerror(char * s)
2029 +// printk("regexp(3): %s", s);
2030 +// /* NOTREACHED */
2035 + - regsub - perform substitutions after a regexp match
2038 +regsub(regexp * prog, char * source, char * dest)
2040 + register char *src;
2041 + register char *dst;
2046 + /* Not necessary and gcc doesn't like it -MLS */
2047 + /*extern char *strncpy();*/
2049 + if (prog == NULL || source == NULL || dest == NULL) {
2050 + regerror("NULL parm to regsub");
2053 + if (UCHARAT(prog->program) != MAGIC) {
2054 + regerror("damaged regexp fed to regsub");
2060 + while ((c = *src++) != '\0') {
2063 + else if (c == '\\' && '0' <= *src && *src <= '9')
2064 + no = *src++ - '0';
2068 + if (no < 0) { /* Ordinary character. */
2069 + if (c == '\\' && (*src == '\\' || *src == '&'))
2072 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2073 + len = prog->endp[no] - prog->startp[no];
2074 + (void) strncpy(dst, prog->startp[no], len);
2076 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2077 + regerror("damaged match string");