1 diff -urN linux.old/Documentation/Configure.help linux.dev/Documentation/Configure.help
2 --- linux.old/Documentation/Configure.help 2005-11-10 16:01:07.645540500 +0100
3 +++ linux.dev/Documentation/Configure.help 2005-11-10 16:03:00.524595000 +0100
4 @@ -29082,6 +29082,23 @@
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.
20 +CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN
21 + Size of the buffer that the application layer data is stored in.
22 + Unless you know what you're doing, leave it at the default of 2048
26 # A couple of things I keep forgetting:
27 # capitalize: AppleTalk, Ethernet, DOS, DMA, FAT, FTP, Internet,
28 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h
29 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack.h 2005-04-04 03:42:20.000000000 +0200
30 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2005-11-10 16:03:00.544596250 +0100
33 #endif /* CONFIG_IP_NF_NAT_NEEDED */
35 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
37 + unsigned int numpackets; /* surely this is kept track of somewhere else, right? I can't find it... */
38 + char * app_proto; /* "http", "ftp", etc. NULL if unclassifed */
40 + /* the application layer data so far. NULL if ->numpackets > numpackets */
43 + unsigned int app_data_len;
48 /* get master conntrack via master expectation */
49 diff -urN linux.old/include/linux/netfilter_ipv4/ipt_layer7.h linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h
50 --- linux.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
51 +++ linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2005-11-10 17:22:12.777440750 +0100
54 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
55 + http://l7-filter.sf.net
57 + This program is free software; you can redistribute it and/or
58 + modify it under the terms of the GNU General Public License
59 + as published by the Free Software Foundation; either version
60 + 2 of the License, or (at your option) any later version.
61 + http://www.gnu.org/licenses/gpl.txt
64 +#ifndef _IPT_LAYER7_H
65 +#define _IPT_LAYER7_H
67 +#define MAX_PATTERN_LEN 8192
68 +#define MAX_PROTOCOL_LEN 256
70 +typedef char *(*proc_ipt_search) (char *, char, char *);
72 +struct ipt_layer7_info {
73 + char protocol[MAX_PROTOCOL_LEN];
75 + char pattern[MAX_PATTERN_LEN];
79 +#endif /* _IPT_LAYER7_H */
80 diff -urN linux.old/net/ipv4/netfilter/Config.in linux.dev/net/ipv4/netfilter/Config.in
81 --- linux.old/net/ipv4/netfilter/Config.in 2005-11-10 16:01:16.194074750 +0100
82 +++ linux.dev/net/ipv4/netfilter/Config.in 2005-11-10 16:03:00.576598250 +0100
84 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
85 dep_tristate ' Unclean match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_UNCLEAN $CONFIG_IP_NF_IPTABLES
86 dep_tristate ' Owner match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_OWNER $CONFIG_IP_NF_IPTABLES
87 + dep_tristate ' Layer 7 match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7 $CONFIG_IP_NF_CONNTRACK
88 + dep_mbool ' Layer 7 debugging output (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7_DEBUG $CONFIG_IP_NF_MATCH_LAYER7
89 + int ' Buffer size for application layer data (256-65536)' CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN 2048
93 dep_tristate ' Packet filtering' CONFIG_IP_NF_FILTER $CONFIG_IP_NF_IPTABLES
94 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
95 --- linux.old/net/ipv4/netfilter/Makefile 2005-11-10 16:01:16.210075750 +0100
96 +++ linux.dev/net/ipv4/netfilter/Makefile 2005-11-10 16:03:00.576598250 +0100
98 obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += ipt_conntrack.o
99 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
100 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
101 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
104 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
105 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_core.c linux.dev/net/ipv4/netfilter/ip_conntrack_core.c
106 --- linux.old/net/ipv4/netfilter/ip_conntrack_core.c 2005-04-04 03:42:20.000000000 +0200
107 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_core.c 2005-11-10 16:03:00.584598750 +0100
113 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
114 + if(ct->layer7.app_proto)
115 + kfree(ct->layer7.app_proto);
116 + if(ct->layer7.app_data)
117 + kfree(ct->layer7.app_data);
120 WRITE_UNLOCK(&ip_conntrack_lock);
123 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c
124 --- linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-04-04 03:42:20.000000000 +0200
125 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c 2005-11-10 16:03:00.592599250 +0100
127 len += sprintf(buffer + len, "[ASSURED] ");
128 len += sprintf(buffer + len, "use=%u ",
129 atomic_read(&conntrack->ct_general.use));
131 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
132 + if(conntrack->layer7.app_proto)
133 + len += sprintf(buffer + len, "l7proto=%s ",
134 + conntrack->layer7.app_proto);
137 len += sprintf(buffer + len, "\n");
140 diff -urN linux.old/net/ipv4/netfilter/ipt_layer7.c linux.dev/net/ipv4/netfilter/ipt_layer7.c
141 --- linux.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
142 +++ linux.dev/net/ipv4/netfilter/ipt_layer7.c 2005-11-10 16:55:35.238845250 +0100
145 + Kernel module to match application layer (OSI layer 7)
146 + data in connections.
148 + http://l7-filter.sf.net
150 + By Matthew Strait and Ethan Sommer, 2003-2005.
152 + This program is free software; you can redistribute it and/or
153 + modify it under the terms of the GNU General Public License
154 + as published by the Free Software Foundation; either version
155 + 2 of the License, or (at your option) any later version.
156 + http://www.gnu.org/licenses/gpl.txt
158 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
159 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
162 +#include <linux/module.h>
163 +#include <linux/skbuff.h>
164 +#include <linux/netfilter_ipv4/ip_conntrack.h>
165 +#include <linux/proc_fs.h>
166 +#include <linux/ctype.h>
168 +#include <net/tcp.h>
169 +#include <linux/netfilter_ipv4/lockhelp.h>
171 +#include "regexp/regexp.c"
173 +#include <linux/netfilter_ipv4/ipt_layer7.h>
174 +#include <linux/netfilter_ipv4/ip_tables.h>
176 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
177 +MODULE_LICENSE("GPL");
178 +MODULE_DESCRIPTION("iptables application layer match module");
180 +#if defined(CONFIG_IP_NF_MATCH_LAYER7_DEBUG)
181 + #define DPRINTK(format,args...) printk(format,##args)
183 + #define DPRINTK(format,args...)
186 +#define TOTAL_PACKETS master_conntrack->layer7.numpackets
188 +/* Number of packets whose data we look at.
189 +This can be modified through /proc/net/layer7_numpackets */
190 +static int num_packets = 8;
192 +static struct pattern_cache {
193 + char * regex_string;
195 + struct pattern_cache * next;
196 +} * first_pattern_cache = NULL;
198 +/* I'm new to locking. Here are my assumptions:
200 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
201 + if they did, nothing awful would happen.
203 +- This code will never be processing the same packet twice at the same time,
204 + because iptables rules are traversed in order.
206 +- It doesn't matter if two packets from different connections are in here at
207 + the same time, because they don't share any data.
209 +- It _does_ matter if two packets from the same connection are here at the same
210 + time. In this case, we have to protect the conntracks and the list of
213 +DECLARE_RWLOCK(ct_lock);
214 +DECLARE_LOCK(list_lock);
216 +#if CONFIG_IP_NF_MATCH_LAYER7_DEBUG
217 +/* Converts an unfriendly string into a friendly one by
218 +replacing unprintables with periods and all whitespace with " ". */
219 +static char * friendly_print(unsigned char * s)
221 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
225 + if (net_ratelimit())
226 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
230 + for(i = 0; i < strlen(s); i++){
231 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
232 + else if(isspace(s[i])) f[i] = ' ';
239 +static char dec2hex(int i)
243 + return (char)(i + '0');
246 + return (char)(i - 10 + 'a');
249 + if (net_ratelimit())
250 + printk("Problem in dec2hex\n");
255 +static char * hex_print(unsigned char * s)
257 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
261 + if (net_ratelimit())
262 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
266 + for(i = 0; i < strlen(s); i++) {
267 + g[i*3 ] = dec2hex(s[i]/16);
268 + g[i*3 + 1] = dec2hex(s[i]%16);
277 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
278 +over again, it make sense to cache the results. */
279 +static regexp * compile_and_cache(char * regex_string, char * protocol)
281 + struct pattern_cache * node = first_pattern_cache;
282 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
283 + struct pattern_cache * tmp;
286 + while (node != NULL) {
287 + if (!strcmp(node->regex_string, regex_string))
288 + return node->pattern;
290 + last_pattern_cache = node;/* points at the last non-NULL node */
294 + /* If we reach the end of the list, then we have not yet cached
295 + the pattern for this regex. Let's do that now.
296 + Be paranoid about running out of memory to avoid list corruption. */
297 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
300 + if (net_ratelimit())
301 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
305 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
306 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
309 + if(!tmp->regex_string || !tmp->pattern) {
310 + if (net_ratelimit())
311 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
312 + kfree(tmp->regex_string);
313 + kfree(tmp->pattern);
318 + /* Ok. The new node is all ready now. */
321 + if(first_pattern_cache == NULL) /* list is empty */
322 + first_pattern_cache = node; /* make node the beginning */
324 + last_pattern_cache->next = node; /* attach node to the end */
326 + /* copy the string and compile the regex */
327 + len = strlen(regex_string);
328 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
329 + node->pattern = regcomp(regex_string, &len);
330 + if ( !node->pattern ) {
331 + if (net_ratelimit())
332 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
333 + /* pattern is now cached as NULL, so we won't try again. */
336 + strcpy(node->regex_string, regex_string);
337 + return node->pattern;
340 +static int can_handle(const struct sk_buff *skb)
342 + if(!skb->nh.iph) /* not IP */
344 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
345 + skb->nh.iph->protocol != IPPROTO_UDP &&
346 + skb->nh.iph->protocol != IPPROTO_ICMP)
351 +/* Returns offset the into the skb->data that the application data starts */
352 +static int app_data_offset(const struct sk_buff *skb)
354 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
355 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
356 + int ip_hl = 4*skb->nh.iph->ihl;
358 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
359 + /* 12 == offset into TCP header for the header length field.
360 + Can't get this with skb->h.th->doff because the tcphdr
361 + struct doesn't get set when routing (this is confirmed to be
362 + true in Netfilter as well as QoS.) */
363 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
365 + return ip_hl + tcp_hl;
366 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
367 + return ip_hl + 8; /* UDP header is always 8 bytes */
368 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
369 + return ip_hl + 8; /* ICMP header is 8 bytes */
371 + if (net_ratelimit())
372 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
373 + return ip_hl + 8; /* something reasonable */
377 +/* handles whether there's a match when we aren't appending data anymore */
378 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
379 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
380 + struct ipt_layer7_info * info)
382 + /* If we're in here, throw the app data away */
383 + WRITE_LOCK(&ct_lock);
384 + if(master_conntrack->layer7.app_data != NULL) {
386 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
387 + if(!master_conntrack->layer7.app_proto) {
388 + char * f = friendly_print(master_conntrack->layer7.app_data);
389 + char * g = hex_print(master_conntrack->layer7.app_data);
390 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
394 + DPRINTK("In hex: %s\n", g);
399 + kfree(master_conntrack->layer7.app_data);
400 + master_conntrack->layer7.app_data = NULL; /* don't free again */
402 + WRITE_UNLOCK(&ct_lock);
404 + if(master_conntrack->layer7.app_proto){
405 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
406 + WRITE_LOCK(&ct_lock);
407 + if(!conntrack->layer7.app_proto) {
408 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
409 + if(!conntrack->layer7.app_proto){
410 + if (net_ratelimit())
411 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
412 + WRITE_UNLOCK(&ct_lock);
415 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
417 + WRITE_UNLOCK(&ct_lock);
419 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
422 + /* If not classified, set to "unknown" to distinguish from
423 + connections that are still being tested. */
424 + WRITE_LOCK(&ct_lock);
425 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
426 + if(!master_conntrack->layer7.app_proto){
427 + if (net_ratelimit())
428 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
429 + WRITE_UNLOCK(&ct_lock);
432 + strcpy(master_conntrack->layer7.app_proto, "unknown");
433 + WRITE_UNLOCK(&ct_lock);
438 +static int add_datastr(char *target, int offset, char *app_data, int len)
442 + /* Strip nulls. Make everything lower case (our regex lib doesn't
443 + do case insensitivity). Add it to the end of the current data. */
444 + for(i = 0; i < CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN-offset-1 &&
446 + if(app_data[i] != '\0') {
447 + target[length+offset] =
448 + /* the kernel version of tolower mungs 'upper ascii' */
449 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
453 + target[length+offset] = '\0';
458 +/* add the new app data to the conntrack. Return number of bytes added. */
459 +static int add_data(struct ip_conntrack * master_conntrack,
460 + char * app_data, int appdatalen)
464 + length = add_datastr(master_conntrack->layer7.app_data, master_conntrack->layer7.app_data_len, app_data, appdatalen);
465 + master_conntrack->layer7.app_data_len += length;
470 +/* Returns true on match and false otherwise. */
471 +static int match(/* const */struct sk_buff *skb, const struct net_device *in,
472 + const struct net_device *out, const void *matchinfo,
473 + int offset, int *hotdrop)
475 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
476 + enum ip_conntrack_info master_ctinfo, ctinfo;
477 + struct ip_conntrack *master_conntrack, *conntrack;
478 + unsigned char *app_data, *tmp_data;
479 + unsigned int pattern_result, appdatalen;
480 + regexp * comppattern;
482 + if(!can_handle(skb)){
483 + DPRINTK("layer7: This is some protocol I can't handle.\n");
484 + return info->invert;
487 + /* Treat the parent and all its children together as one connection,
488 + except for the purpose of setting conntrack->layer7.app_proto in the
489 + actual connection. This makes /proc/net/ip_conntrack somewhat more
491 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
492 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
493 + DPRINTK("layer7: packet is not from a known connection, giving up.\n");
494 + return info->invert;
497 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
498 + while (master_ct(master_conntrack) != NULL)
499 + master_conntrack = master_ct(master_conntrack);
502 + WRITE_LOCK(&ct_lock);
503 + master_conntrack->layer7.numpackets++;/*starts at 0 via memset*/
504 + WRITE_UNLOCK(&ct_lock);
507 + /* if we've classified it or seen too many packets */
508 + if(!info->pkt && (TOTAL_PACKETS > num_packets ||
509 + master_conntrack->layer7.app_proto)) {
511 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
513 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
514 + rules. I'm not sure that using cb for this purpose is correct, although
515 + it says "put your private variables there". But it doesn't look like it
516 + is being used for anything else in the skbs that make it here. How can
517 + I write to cb without making the compiler angry? */
518 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
520 + return (pattern_result ^ info->invert);
523 + if(skb_is_nonlinear(skb)){
524 + if(skb_linearize(skb, GFP_ATOMIC) != 0){
525 + if (net_ratelimit())
526 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
527 + return info->invert;
531 + /* now that the skb is linearized, it's safe to set these. */
532 + app_data = skb->data + app_data_offset(skb);
533 + appdatalen = skb->tail - app_data;
535 + LOCK_BH(&list_lock);
536 + /* the return value gets checked later, when we're ready to use it */
537 + comppattern = compile_and_cache(info->pattern, info->protocol);
538 + UNLOCK_BH(&list_lock);
541 + tmp_data = kmalloc(CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN, GFP_ATOMIC);
543 + if (net_ratelimit())
544 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
545 + return info->invert;
548 + tmp_data[0] = '\0';
549 + add_datastr(tmp_data, 0, app_data, appdatalen);
550 + pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
554 + return (pattern_result ^ info->invert);
557 + /* On the first packet of a connection, allocate space for app data */
558 + WRITE_LOCK(&ct_lock);
559 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
560 + master_conntrack->layer7.app_data = kmalloc(CONFIG_IP_NF_MATCH_LAYER7_MAXDATALEN, GFP_ATOMIC);
561 + if(!master_conntrack->layer7.app_data){
562 + if (net_ratelimit())
563 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
564 + WRITE_UNLOCK(&ct_lock);
565 + return info->invert;
568 + master_conntrack->layer7.app_data[0] = '\0';
570 + WRITE_UNLOCK(&ct_lock);
572 + /* Can be here, but unallocated, if numpackets is increased near
573 + the beginning of a connection */
574 + if(master_conntrack->layer7.app_data == NULL)
575 + return (info->invert); /* unmatched */
579 + WRITE_LOCK(&ct_lock);
580 + newbytes = add_data(master_conntrack, app_data, appdatalen);
581 + WRITE_UNLOCK(&ct_lock);
583 + if(newbytes == 0) { /* didn't add any data */
585 + /* Didn't match before, not going to match now */
586 + return info->invert;
590 + /* If looking for "unknown", then never match. "Unknown" means that
591 + we've given up; we're still trying with these packets. */
592 + if(!strcmp(info->protocol, "unknown")) {
593 + pattern_result = 0;
594 + /* If the regexp failed to compile, don't bother running it */
595 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
596 + DPRINTK("layer7: regexec positive: %s!\n", info->protocol);
597 + pattern_result = 1;
598 + } else pattern_result = 0;
600 + if(pattern_result) {
601 + WRITE_LOCK(&ct_lock);
602 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
603 + if(!master_conntrack->layer7.app_proto){
604 + if (net_ratelimit())
605 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
606 + WRITE_UNLOCK(&ct_lock);
607 + return (pattern_result ^ info->invert);
609 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
610 + WRITE_UNLOCK(&ct_lock);
613 + /* mark the packet seen */
616 + return (pattern_result ^ info->invert);
619 +static int checkentry(const char *tablename, const struct ipt_ip *ip,
620 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
622 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_layer7_info)))
627 +static struct ipt_match layer7_match = {
630 + .checkentry = &checkentry,
634 +/* taken from drivers/video/modedb.c */
635 +static int my_atoi(const char *s)
642 + val = 10*val+(*s-'0');
650 +/* write out num_packets to userland. */
651 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
652 + int* eof, void * data)
654 + if(num_packets > 99 && net_ratelimit())
655 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
657 + page[0] = num_packets/10 + '0';
658 + page[1] = num_packets%10 + '0';
667 +/* Read in num_packets from userland */
668 +static int layer7_write_proc(struct file* file, const char* buffer,
669 + unsigned long count, void *data)
671 + char * foo = kmalloc(count, GFP_ATOMIC);
674 + if (net_ratelimit())
675 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
679 + copy_from_user(foo, buffer, count);
681 + num_packets = my_atoi(foo);
684 + /* This has an arbitrary limit to make the math easier. I'm lazy.
685 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
686 + if(num_packets > 99) {
687 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
689 + } else if(num_packets < 1) {
690 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
697 +/* register the proc file */
698 +static void layer7_init_proc(void)
700 + struct proc_dir_entry* entry;
701 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
702 + entry->read_proc = layer7_read_proc;
703 + entry->write_proc = layer7_write_proc;
706 +static void layer7_cleanup_proc(void)
708 + remove_proc_entry("layer7_numpackets", proc_net);
711 +static int __init init(void)
713 + layer7_init_proc();
714 + return ipt_register_match(&layer7_match);
717 +static void __exit fini(void)
719 + layer7_cleanup_proc();
720 + ipt_unregister_match(&layer7_match);
725 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.c linux.dev/net/ipv4/netfilter/regexp/regexp.c
726 --- linux.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
727 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.c 2005-11-10 16:03:00.596599500 +0100
730 + * regcomp and regexec -- regsub and regerror are elsewhere
731 + * @(#)regexp.c 1.3 of 18 April 87
733 + * Copyright (c) 1986 by University of Toronto.
734 + * Written by Henry Spencer. Not derived from licensed software.
736 + * Permission is granted to anyone to use this software for any
737 + * purpose on any computer system, and to redistribute it freely,
738 + * subject to the following restrictions:
740 + * 1. The author is not responsible for the consequences of use of
741 + * this software, no matter how awful, even if they arise
742 + * from defects in it.
744 + * 2. The origin of this software must not be misrepresented, either
745 + * by explicit claim or by omission.
747 + * 3. Altered versions must be plainly marked as such, and must not
748 + * be misrepresented as being the original software.
750 + * Beware that some of this code is subtly aware of the way operator
751 + * precedence is structured in regular expressions. Serious changes in
752 + * regular-expression syntax might require a total rethink.
754 + * This code was modified by Ethan Sommer to work within the kernel
755 + * (it now uses kmalloc etc..)
757 + * Modified slightly by Matthew Strait to use more modern C.
761 +#include "regmagic.h"
763 +/* added by ethan and matt. Lets it work in both kernel and user space.
764 +(So iptables can use it, for instance.) Yea, it goes both ways... */
766 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
768 + #define printk(format,args...) printf(format,##args)
771 +void regerror(char * s)
773 + printk("<3>Regexp: %s\n", s);
778 + * The "internal use only" fields in regexp.h are present to pass info from
779 + * compile to execute that permits the execute phase to run lots faster on
780 + * simple cases. They are:
782 + * regstart char that must begin a match; '\0' if none obvious
783 + * reganch is the match anchored (at beginning-of-line only)?
784 + * regmust string (pointer into program) that match must include, or NULL
785 + * regmlen length of regmust string
787 + * Regstart and reganch permit very fast decisions on suitable starting points
788 + * for a match, cutting down the work a lot. Regmust permits fast rejection
789 + * of lines that cannot possibly match. The regmust tests are costly enough
790 + * that regcomp() supplies a regmust only if the r.e. contains something
791 + * potentially expensive (at present, the only such thing detected is * or +
792 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
793 + * supplied because the test in regexec() needs it and regcomp() is computing
798 + * Structure for regexp "program". This is essentially a linear encoding
799 + * of a nondeterministic finite-state machine (aka syntax charts or
800 + * "railroad normal form" in parsing technology). Each node is an opcode
801 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
802 + * all nodes except BRANCH implement concatenation; a "next" pointer with
803 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
804 + * have one of the subtle syntax dependencies: an individual BRANCH (as
805 + * opposed to a collection of them) is never concatenated with anything
806 + * because of operator precedence.) The operand of some types of node is
807 + * a literal string; for others, it is a node leading into a sub-FSM. In
808 + * particular, the operand of a BRANCH node is the first node of the branch.
809 + * (NB this is *not* a tree structure: the tail of the branch connects
810 + * to the thing following the set of BRANCHes.) The opcodes are:
813 +/* definition number opnd? meaning */
814 +#define END 0 /* no End of program. */
815 +#define BOL 1 /* no Match "" at beginning of line. */
816 +#define EOL 2 /* no Match "" at end of line. */
817 +#define ANY 3 /* no Match any one character. */
818 +#define ANYOF 4 /* str Match any character in this string. */
819 +#define ANYBUT 5 /* str Match any character not in this string. */
820 +#define BRANCH 6 /* node Match this alternative, or the next... */
821 +#define BACK 7 /* no Match "", "next" ptr points backward. */
822 +#define EXACTLY 8 /* str Match this string. */
823 +#define NOTHING 9 /* no Match empty string. */
824 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
825 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
826 +#define OPEN 20 /* no Mark this point in input as start of #n. */
827 + /* OPEN+1 is number 1, etc. */
828 +#define CLOSE 30 /* no Analogous to OPEN. */
833 + * BRANCH The set of branches constituting a single choice are hooked
834 + * together with their "next" pointers, since precedence prevents
835 + * anything being concatenated to any individual branch. The
836 + * "next" pointer of the last BRANCH in a choice points to the
837 + * thing following the whole choice. This is also where the
838 + * final "next" pointer of each individual branch points; each
839 + * branch starts with the operand node of a BRANCH node.
841 + * BACK Normal "next" pointers all implicitly point forward; BACK
842 + * exists to make loop structures possible.
844 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
845 + * BRANCH structures using BACK. Simple cases (one character
846 + * per match) are implemented with STAR and PLUS for speed
847 + * and to minimize recursive plunges.
849 + * OPEN,CLOSE ...are numbered at compile time.
853 + * A node is one char of opcode followed by two chars of "next" pointer.
854 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
855 + * value is a positive offset from the opcode of the node containing it.
856 + * An operand, if any, simply follows the node. (Note that much of the
857 + * code generation knows about this implicit relationship.)
859 + * Using two bytes for the "next" pointer is vast overkill for most things,
860 + * but allows patterns to get big without disasters.
862 +#define OP(p) (*(p))
863 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
864 +#define OPERAND(p) ((p) + 3)
867 + * See regmagic.h for one further detail of program structure.
872 + * Utility definitions.
875 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
877 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
880 +#define FAIL(m) { regerror(m); return(NULL); }
881 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
882 +#define META "^$.[()|?+*\\"
885 + * Flags to be passed up and down.
887 +#define HASWIDTH 01 /* Known never to match null string. */
888 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
889 +#define SPSTART 04 /* Starts with * or +. */
890 +#define WORST 0 /* Worst case. */
893 + * Global work variables for regcomp().
895 +static char *regparse; /* Input-scan pointer. */
896 +static int regnpar; /* () count. */
897 +static char regdummy;
898 +static char *regcode; /* Code-emit pointer; ®dummy = don't. */
899 +static long regsize; /* Code size. */
902 + * Forward declarations for regcomp()'s friends.
905 +#define STATIC static
907 +STATIC char *reg(int paren,int *flagp);
908 +STATIC char *regbranch(int *flagp);
909 +STATIC char *regpiece(int *flagp);
910 +STATIC char *regatom(int *flagp);
911 +STATIC char *regnode(char op);
912 +STATIC char *regnext(char *p);
913 +STATIC void regc(char b);
914 +STATIC void reginsert(char op, char *opnd);
915 +STATIC void regtail(char *p, char *val);
916 +STATIC void regoptail(char *p, char *val);
919 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
926 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
927 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
928 + if (*scan1 == *scan2++)
936 + - regcomp - compile a regular expression into internal code
938 + * We can't allocate space until we know how big the compiled form will be,
939 + * but we can't compile it (and thus know how big it is) until we've got a
940 + * place to put the code. So we cheat: we compile it twice, once with code
941 + * generation turned off and size counting turned on, and once "for real".
942 + * This also means that we don't allocate space until we are sure that the
943 + * thing really will compile successfully, and we never have to move the
944 + * code and thus invalidate pointers into it. (Note that it has to be in
945 + * one piece because free() must be able to free it all.)
947 + * Beware that the optimization-preparation code in here knows about some
948 + * of the structure of the compiled regexp.
951 +regcomp(char *exp,int *patternsize)
953 + register regexp *r;
954 + register char *scan;
955 + register char *longest;
958 + /* commented out by ethan
959 + extern char *malloc();
963 + FAIL("NULL argument");
965 + /* First pass: determine size, legality. */
969 + regcode = ®dummy;
971 + if (reg(0, &flags) == NULL)
974 + /* Small enough for pointer-storage convention? */
975 + if (regsize >= 32767L) /* Probably could be 65535L. */
976 + FAIL("regexp too big");
978 + /* Allocate space. */
979 + *patternsize=sizeof(regexp) + (unsigned)regsize;
980 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
982 + FAIL("out of space");
984 + /* Second pass: emit code. */
987 + regcode = r->program;
989 + if (reg(0, &flags) == NULL)
992 + /* Dig out information for optimizations. */
993 + r->regstart = '\0'; /* Worst-case defaults. */
997 + scan = r->program+1; /* First BRANCH. */
998 + if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
999 + scan = OPERAND(scan);
1001 + /* Starting-point info. */
1002 + if (OP(scan) == EXACTLY)
1003 + r->regstart = *OPERAND(scan);
1004 + else if (OP(scan) == BOL)
1008 + * If there's something expensive in the r.e., find the
1009 + * longest literal string that must appear and make it the
1010 + * regmust. Resolve ties in favor of later strings, since
1011 + * the regstart check works with the beginning of the r.e.
1012 + * and avoiding duplication strengthens checking. Not a
1013 + * strong reason, but sufficient in the absence of others.
1015 + if (flags&SPSTART) {
1018 + for (; scan != NULL; scan = regnext(scan))
1019 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1020 + longest = OPERAND(scan);
1021 + len = strlen(OPERAND(scan));
1023 + r->regmust = longest;
1032 + - reg - regular expression, i.e. main body or parenthesized thing
1034 + * Caller must absorb opening parenthesis.
1036 + * Combining parenthesis handling with the base level of regular expression
1037 + * is a trifle forced, but the need to tie the tails of the branches to what
1038 + * follows makes it hard to avoid.
1041 +reg(int paren, int *flagp /* Parenthesized? */ )
1043 + register char *ret;
1044 + register char *br;
1045 + register char *ender;
1046 + register int parno = 0; /* 0 makes gcc happy */
1049 + *flagp = HASWIDTH; /* Tentatively. */
1051 + /* Make an OPEN node, if parenthesized. */
1053 + if (regnpar >= NSUBEXP)
1054 + FAIL("too many ()");
1057 + ret = regnode(OPEN+parno);
1061 + /* Pick up the branches, linking them together. */
1062 + br = regbranch(&flags);
1066 + regtail(ret, br); /* OPEN -> first. */
1069 + if (!(flags&HASWIDTH))
1070 + *flagp &= ~HASWIDTH;
1071 + *flagp |= flags&SPSTART;
1072 + while (*regparse == '|') {
1074 + br = regbranch(&flags);
1077 + regtail(ret, br); /* BRANCH -> BRANCH. */
1078 + if (!(flags&HASWIDTH))
1079 + *flagp &= ~HASWIDTH;
1080 + *flagp |= flags&SPSTART;
1083 + /* Make a closing node, and hook it on the end. */
1084 + ender = regnode((paren) ? CLOSE+parno : END);
1085 + regtail(ret, ender);
1087 + /* Hook the tails of the branches to the closing node. */
1088 + for (br = ret; br != NULL; br = regnext(br))
1089 + regoptail(br, ender);
1091 + /* Check for proper termination. */
1092 + if (paren && *regparse++ != ')') {
1093 + FAIL("unmatched ()");
1094 + } else if (!paren && *regparse != '\0') {
1095 + if (*regparse == ')') {
1096 + FAIL("unmatched ()");
1098 + FAIL("junk on end"); /* "Can't happen". */
1106 + - regbranch - one alternative of an | operator
1108 + * Implements the concatenation operator.
1111 +regbranch(int *flagp)
1113 + register char *ret;
1114 + register char *chain;
1115 + register char *latest;
1118 + *flagp = WORST; /* Tentatively. */
1120 + ret = regnode(BRANCH);
1122 + while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
1123 + latest = regpiece(&flags);
1124 + if (latest == NULL)
1126 + *flagp |= flags&HASWIDTH;
1127 + if (chain == NULL) /* First piece. */
1128 + *flagp |= flags&SPSTART;
1130 + regtail(chain, latest);
1133 + if (chain == NULL) /* Loop ran zero times. */
1134 + (void) regnode(NOTHING);
1140 + - regpiece - something followed by possible [*+?]
1142 + * Note that the branching code sequences used for ? and the general cases
1143 + * of * and + are somewhat optimized: they use the same NOTHING node as
1144 + * both the endmarker for their branch list and the body of the last branch.
1145 + * It might seem that this node could be dispensed with entirely, but the
1146 + * endmarker role is not redundant.
1149 +regpiece(int *flagp)
1151 + register char *ret;
1153 + register char *next;
1156 + ret = regatom(&flags);
1161 + if (!ISMULT(op)) {
1166 + if (!(flags&HASWIDTH) && op != '?')
1167 + FAIL("*+ operand could be empty");
1168 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1170 + if (op == '*' && (flags&SIMPLE))
1171 + reginsert(STAR, ret);
1172 + else if (op == '*') {
1173 + /* Emit x* as (x&|), where & means "self". */
1174 + reginsert(BRANCH, ret); /* Either x */
1175 + regoptail(ret, regnode(BACK)); /* and loop */
1176 + regoptail(ret, ret); /* back */
1177 + regtail(ret, regnode(BRANCH)); /* or */
1178 + regtail(ret, regnode(NOTHING)); /* null. */
1179 + } else if (op == '+' && (flags&SIMPLE))
1180 + reginsert(PLUS, ret);
1181 + else if (op == '+') {
1182 + /* Emit x+ as x(&|), where & means "self". */
1183 + next = regnode(BRANCH); /* Either */
1184 + regtail(ret, next);
1185 + regtail(regnode(BACK), ret); /* loop back */
1186 + regtail(next, regnode(BRANCH)); /* or */
1187 + regtail(ret, regnode(NOTHING)); /* null. */
1188 + } else if (op == '?') {
1189 + /* Emit x? as (x|) */
1190 + reginsert(BRANCH, ret); /* Either x */
1191 + regtail(ret, regnode(BRANCH)); /* or */
1192 + next = regnode(NOTHING); /* null. */
1193 + regtail(ret, next);
1194 + regoptail(ret, next);
1197 + if (ISMULT(*regparse))
1198 + FAIL("nested *?+");
1204 + - regatom - the lowest level
1206 + * Optimization: gobbles an entire sequence of ordinary characters so that
1207 + * it can turn them into a single node, which is smaller to store and
1208 + * faster to run. Backslashed characters are exceptions, each becoming a
1209 + * separate node; the code is simpler that way and it's not worth fixing.
1212 +regatom(int *flagp)
1214 + register char *ret;
1217 + *flagp = WORST; /* Tentatively. */
1219 + switch (*regparse++) {
1221 + ret = regnode(BOL);
1224 + ret = regnode(EOL);
1227 + ret = regnode(ANY);
1228 + *flagp |= HASWIDTH|SIMPLE;
1231 + register int class;
1232 + register int classend;
1234 + if (*regparse == '^') { /* Complement of range. */
1235 + ret = regnode(ANYBUT);
1238 + ret = regnode(ANYOF);
1239 + if (*regparse == ']' || *regparse == '-')
1240 + regc(*regparse++);
1241 + while (*regparse != '\0' && *regparse != ']') {
1242 + if (*regparse == '-') {
1244 + if (*regparse == ']' || *regparse == '\0')
1247 + class = UCHARAT(regparse-2)+1;
1248 + classend = UCHARAT(regparse);
1249 + if (class > classend+1)
1250 + FAIL("invalid [] range");
1251 + for (; class <= classend; class++)
1256 + regc(*regparse++);
1259 + if (*regparse != ']')
1260 + FAIL("unmatched []");
1262 + *flagp |= HASWIDTH|SIMPLE;
1266 + ret = reg(1, &flags);
1269 + *flagp |= flags&(HASWIDTH|SPSTART);
1274 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1279 + FAIL("?+* follows nothing");
1282 + if (*regparse == '\0')
1283 + FAIL("trailing \\");
1284 + ret = regnode(EXACTLY);
1285 + regc(*regparse++);
1287 + *flagp |= HASWIDTH|SIMPLE;
1291 + register char ender;
1294 + len = my_strcspn((const char *)regparse, (const char *)META);
1296 + FAIL("internal disaster");
1297 + ender = *(regparse+len);
1298 + if (len > 1 && ISMULT(ender))
1299 + len--; /* Back off clear of ?+* operand. */
1300 + *flagp |= HASWIDTH;
1303 + ret = regnode(EXACTLY);
1305 + regc(*regparse++);
1317 + - regnode - emit a node
1319 +static char * /* Location. */
1322 + register char *ret;
1323 + register char *ptr;
1326 + if (ret == ®dummy) {
1333 + *ptr++ = '\0'; /* Null "next" pointer. */
1341 + - regc - emit (if appropriate) a byte of code
1346 + if (regcode != ®dummy)
1353 + - reginsert - insert an operator in front of already-emitted operand
1355 + * Means relocating the operand.
1358 +reginsert(char op, char* opnd)
1360 + register char *src;
1361 + register char *dst;
1362 + register char *place;
1364 + if (regcode == ®dummy) {
1372 + while (src > opnd)
1375 + place = opnd; /* Op node, where operand used to be. */
1382 + - regtail - set the next-pointer at the end of a node chain
1385 +regtail(char *p, char *val)
1387 + register char *scan;
1388 + register char *temp;
1389 + register int offset;
1391 + if (p == ®dummy)
1394 + /* Find last node. */
1397 + temp = regnext(scan);
1403 + if (OP(scan) == BACK)
1404 + offset = scan - val;
1406 + offset = val - scan;
1407 + *(scan+1) = (offset>>8)&0377;
1408 + *(scan+2) = offset&0377;
1412 + - regoptail - regtail on operand of first argument; nop if operandless
1415 +regoptail(char *p, char *val)
1417 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1418 + if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1420 + regtail(OPERAND(p), val);
1424 + * regexec and friends
1428 + * Global work variables for regexec().
1430 +static char *reginput; /* String-input pointer. */
1431 +static char *regbol; /* Beginning of input, for ^ check. */
1432 +static char **regstartp; /* Pointer to startp array. */
1433 +static char **regendp; /* Ditto for endp. */
1438 +STATIC int regtry(regexp *prog, char *string);
1439 +STATIC int regmatch(char *prog);
1440 +STATIC int regrepeat(char *p);
1443 +int regnarrate = 0;
1445 +STATIC char *regprop(char *op);
1449 + - regexec - match a regexp against a string
1452 +regexec(regexp *prog, char *string)
1456 + /* Be paranoid... */
1457 + if (prog == NULL || string == NULL) {
1458 + printk("<3>Regexp: NULL parameter\n");
1462 + /* Check validity of program. */
1463 + if (UCHARAT(prog->program) != MAGIC) {
1464 + printk("<3>Regexp: corrupted program\n");
1468 + /* If there is a "must appear" string, look for it. */
1469 + if (prog->regmust != NULL) {
1471 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1472 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1473 + break; /* Found it. */
1476 + if (s == NULL) /* Not present. */
1480 + /* Mark beginning of line for ^ . */
1483 + /* Simplest case: anchored match need be tried only once. */
1484 + if (prog->reganch)
1485 + return(regtry(prog, string));
1487 + /* Messy cases: unanchored match. */
1489 + if (prog->regstart != '\0')
1490 + /* We know what char it must start with. */
1491 + while ((s = strchr(s, prog->regstart)) != NULL) {
1492 + if (regtry(prog, s))
1497 + /* We don't -- general case. */
1499 + if (regtry(prog, s))
1501 + } while (*s++ != '\0');
1508 + - regtry - try match at specific point
1510 +static int /* 0 failure, 1 success */
1511 +regtry(regexp *prog, char *string)
1514 + register char **sp;
1515 + register char **ep;
1517 + reginput = string;
1518 + regstartp = prog->startp;
1519 + regendp = prog->endp;
1521 + sp = prog->startp;
1523 + for (i = NSUBEXP; i > 0; i--) {
1527 + if (regmatch(prog->program + 1)) {
1528 + prog->startp[0] = string;
1529 + prog->endp[0] = reginput;
1536 + - regmatch - main matching routine
1538 + * Conceptually the strategy is simple: check to see whether the current
1539 + * node matches, call self recursively to see whether the rest matches,
1540 + * and then act accordingly. In practice we make some effort to avoid
1541 + * recursion, in particular by going through "ordinary" nodes (that don't
1542 + * need to know whether the rest of the match failed) by a loop instead of
1545 +static int /* 0 failure, 1 success */
1546 +regmatch(char *prog)
1548 + register char *scan = prog; /* Current node. */
1549 + char *next; /* Next node. */
1552 + if (scan != NULL && regnarrate)
1553 + fprintf(stderr, "%s(\n", regprop(scan));
1555 + while (scan != NULL) {
1558 + fprintf(stderr, "%s...\n", regprop(scan));
1560 + next = regnext(scan);
1562 + switch (OP(scan)) {
1564 + if (reginput != regbol)
1568 + if (*reginput != '\0')
1572 + if (*reginput == '\0')
1578 + register char *opnd;
1580 + opnd = OPERAND(scan);
1581 + /* Inline the first character, for speed. */
1582 + if (*opnd != *reginput)
1584 + len = strlen(opnd);
1585 + if (len > 1 && strncmp(opnd, reginput, len) != 0)
1591 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
1596 + if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
1613 + register char *save;
1615 + no = OP(scan) - OPEN;
1618 + if (regmatch(next)) {
1620 + * Don't set startp if some later
1621 + * invocation of the same parentheses
1624 + if (regstartp[no] == NULL)
1625 + regstartp[no] = save;
1642 + register char *save;
1644 + no = OP(scan) - CLOSE;
1647 + if (regmatch(next)) {
1649 + * Don't set endp if some later
1650 + * invocation of the same parentheses
1653 + if (regendp[no] == NULL)
1654 + regendp[no] = save;
1661 + register char *save;
1663 + if (OP(next) != BRANCH) /* No choice. */
1664 + next = OPERAND(scan); /* Avoid recursion. */
1668 + if (regmatch(OPERAND(scan)))
1671 + scan = regnext(scan);
1672 + } while (scan != NULL && OP(scan) == BRANCH);
1680 + register char nextch;
1682 + register char *save;
1686 + * Lookahead to avoid useless match attempts
1687 + * when we know what character comes next.
1690 + if (OP(next) == EXACTLY)
1691 + nextch = *OPERAND(next);
1692 + min = (OP(scan) == STAR) ? 0 : 1;
1694 + no = regrepeat(OPERAND(scan));
1695 + while (no >= min) {
1696 + /* If it could work, try it. */
1697 + if (nextch == '\0' || *reginput == nextch)
1698 + if (regmatch(next))
1700 + /* Couldn't or didn't -- back up. */
1702 + reginput = save + no;
1708 + return(1); /* Success! */
1711 + printk("<3>Regexp: memory corruption\n");
1720 + * We get here only if there's trouble -- normally "case END" is
1721 + * the terminating point.
1723 + printk("<3>Regexp: corrupted pointers\n");
1728 + - regrepeat - repeatedly match something simple, report how many
1733 + register int count = 0;
1734 + register char *scan;
1735 + register char *opnd;
1738 + opnd = OPERAND(p);
1741 + count = strlen(scan);
1745 + while (*opnd == *scan) {
1751 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1757 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1762 + default: /* Oh dear. Called inappropriately. */
1763 + printk("<3>Regexp: internal foulup\n");
1764 + count = 0; /* Best compromise. */
1773 + - regnext - dig the "next" pointer out of a node
1778 + register int offset;
1780 + if (p == ®dummy)
1787 + if (OP(p) == BACK)
1795 +STATIC char *regprop();
1798 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1804 + register char op = EXACTLY; /* Arbitrary non-END op. */
1805 + register char *next;
1806 + /* extern char *strchr(); */
1809 + s = r->program + 1;
1810 + while (op != END) { /* While that wasn't END last time... */
1812 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1813 + next = regnext(s);
1814 + if (next == NULL) /* Next ptr. */
1817 + printf("(%d)", (s-r->program)+(next-s));
1819 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1820 + /* Literal string, where present. */
1821 + while (*s != '\0') {
1830 + /* Header fields of interest. */
1831 + if (r->regstart != '\0')
1832 + printf("start `%c' ", r->regstart);
1834 + printf("anchored ");
1835 + if (r->regmust != NULL)
1836 + printf("must have \"%s\"", r->regmust);
1841 + - regprop - printable representation of opcode
1848 + static char buf[BUFLEN];
1892 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1904 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1914 + printk("<3>Regexp: corrupted opcode\n");
1918 + strncat(buf, p, BUFLEN-strlen(buf));
1924 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.h linux.dev/net/ipv4/netfilter/regexp/regexp.h
1925 --- linux.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1926 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.h 2005-11-10 16:03:00.596599500 +0100
1929 + * Definitions etc. for regexp(3) routines.
1931 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1932 + * not the System V one.
1939 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1940 +which contains a version of this library, says:
1943 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1944 + * will not work properly.
1947 +However, it looks rather like this library is limited to 10. If you think
1948 +otherwise, let us know.
1952 +typedef struct regexp {
1953 + char *startp[NSUBEXP];
1954 + char *endp[NSUBEXP];
1955 + char regstart; /* Internal use only. */
1956 + char reganch; /* Internal use only. */
1957 + char *regmust; /* Internal use only. */
1958 + int regmlen; /* Internal use only. */
1959 + char program[1]; /* Unwarranted chumminess with compiler. */
1962 +regexp * regcomp(char *exp, int *patternsize);
1963 +int regexec(regexp *prog, char *string);
1964 +void regsub(regexp *prog, char *source, char *dest);
1965 +void regerror(char *s);
1968 diff -urN linux.old/net/ipv4/netfilter/regexp/regmagic.h linux.dev/net/ipv4/netfilter/regexp/regmagic.h
1969 --- linux.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1970 +++ linux.dev/net/ipv4/netfilter/regexp/regmagic.h 2005-11-10 16:03:00.596599500 +0100
1973 + * The first byte of the regexp internal "program" is actually this magic
1974 + * number; the start node begins in the second byte.
1977 diff -urN linux.old/net/ipv4/netfilter/regexp/regsub.c linux.dev/net/ipv4/netfilter/regexp/regsub.c
1978 --- linux.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1979 +++ linux.dev/net/ipv4/netfilter/regexp/regsub.c 2005-11-10 16:03:00.596599500 +0100
1983 + * @(#)regsub.c 1.3 of 2 April 86
1985 + * Copyright (c) 1986 by University of Toronto.
1986 + * Written by Henry Spencer. Not derived from licensed software.
1988 + * Permission is granted to anyone to use this software for any
1989 + * purpose on any computer system, and to redistribute it freely,
1990 + * subject to the following restrictions:
1992 + * 1. The author is not responsible for the consequences of use of
1993 + * this software, no matter how awful, even if they arise
1994 + * from defects in it.
1996 + * 2. The origin of this software must not be misrepresented, either
1997 + * by explicit claim or by omission.
1999 + * 3. Altered versions must be plainly marked as such, and must not
2000 + * be misrepresented as being the original software.
2003 + * This code was modified by Ethan Sommer to work within the kernel
2004 + * (it now uses kmalloc etc..)
2007 +#include "regexp.h"
2008 +#include "regmagic.h"
2009 +#include <linux/string.h>
2013 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
2015 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
2019 +//void regerror(char * s)
2021 +// printk("regexp(3): %s", s);
2022 +// /* NOTREACHED */
2027 + - regsub - perform substitutions after a regexp match
2030 +regsub(regexp * prog, char * source, char * dest)
2032 + register char *src;
2033 + register char *dst;
2038 + /* Not necessary and gcc doesn't like it -MLS */
2039 + /*extern char *strncpy();*/
2041 + if (prog == NULL || source == NULL || dest == NULL) {
2042 + regerror("NULL parm to regsub");
2045 + if (UCHARAT(prog->program) != MAGIC) {
2046 + regerror("damaged regexp fed to regsub");
2052 + while ((c = *src++) != '\0') {
2055 + else if (c == '\\' && '0' <= *src && *src <= '9')
2056 + no = *src++ - '0';
2060 + if (no < 0) { /* Ordinary character. */
2061 + if (c == '\\' && (*src == '\\' || *src == '&'))
2064 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2065 + len = prog->endp[no] - prog->startp[no];
2066 + (void) strncpy(dst, prog->startp[no], len);
2068 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2069 + regerror("damaged match string");