1 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack.h 2007-01-01 05:17:07.000000000 +0100
3 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2007-01-01 05:18:48.000000000 +0100
5 /* Traversed often, so hopefully in different cacheline to top */
6 /* These are my tuples; original and reply */
7 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
9 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
11 + char * app_proto; /* e.g. "http". NULL before decision. "unknown" after decision if no match */
12 + char * app_data; /* application layer data so far. NULL after match decision */
13 + unsigned int app_data_len;
19 struct ip_conntrack_expect
20 diff -urN linux.old/include/linux/netfilter_ipv4/ipt_layer7.h linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h
21 --- linux.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
22 +++ linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2007-01-01 05:18:48.000000000 +0100
25 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
26 + http://l7-filter.sf.net
28 + This program is free software; you can redistribute it and/or
29 + modify it under the terms of the GNU General Public License
30 + as published by the Free Software Foundation; either version
31 + 2 of the License, or (at your option) any later version.
32 + http://www.gnu.org/licenses/gpl.txt
35 +#ifndef _IPT_LAYER7_H
36 +#define _IPT_LAYER7_H
38 +#define MAX_PATTERN_LEN 8192
39 +#define MAX_PROTOCOL_LEN 256
41 +typedef char *(*proc_ipt_search) (char *, char, char *);
43 +struct ipt_layer7_info {
44 + char protocol[MAX_PROTOCOL_LEN];
46 + char pattern[MAX_PATTERN_LEN];
49 +#endif /* _IPT_LAYER7_H */
50 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_core.c linux.dev/net/ipv4/netfilter/ip_conntrack_core.c
51 --- linux.old/net/ipv4/netfilter/ip_conntrack_core.c 2007-01-01 05:17:07.000000000 +0100
52 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_core.c 2007-01-01 05:18:48.000000000 +0100
55 ip_ct_remove_expectations(ct);
57 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
58 + if(ct->layer7.app_proto)
59 + kfree(ct->layer7.app_proto);
60 + if(ct->layer7.app_data)
61 + kfree(ct->layer7.app_data);
64 /* We overload first tuple to link into unconfirmed list. */
65 if (!is_confirmed(ct)) {
66 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
67 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c
68 --- linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-01-01 05:17:07.000000000 +0100
69 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-01-01 05:18:48.000000000 +0100
74 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
75 + if(conntrack->layer7.app_proto)
76 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
80 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
83 diff -urN linux.old/net/ipv4/netfilter/ipt_layer7.c linux.dev/net/ipv4/netfilter/ipt_layer7.c
84 --- linux.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
85 +++ linux.dev/net/ipv4/netfilter/ipt_layer7.c 2007-01-01 05:18:48.000000000 +0100
88 + Kernel module to match application layer (OSI layer 7) data in connections.
90 + http://l7-filter.sf.net
92 + By Matthew Strait and Ethan Sommer, 2003-2006.
94 + This program is free software; you can redistribute it and/or
95 + modify it under the terms of the GNU General Public License
96 + as published by the Free Software Foundation; either version
97 + 2 of the License, or (at your option) any later version.
98 + http://www.gnu.org/licenses/gpl.txt
100 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
101 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
104 +#include <linux/module.h>
105 +#include <linux/skbuff.h>
106 +#include <linux/netfilter_ipv4/ip_conntrack.h>
107 +#include <linux/proc_fs.h>
108 +#include <linux/ctype.h>
110 +#include <net/tcp.h>
111 +#include <linux/spinlock.h>
113 +#include "regexp/regexp.c"
115 +#include <linux/netfilter_ipv4/ipt_layer7.h>
116 +#include <linux/netfilter_ipv4/ip_tables.h>
118 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
119 +MODULE_LICENSE("GPL");
120 +MODULE_DESCRIPTION("iptables application layer match module");
121 +MODULE_VERSION("2.0");
123 +static int maxdatalen = 2048; // this is the default
124 +module_param(maxdatalen, int, 0444);
125 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
127 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
128 + #define DPRINTK(format,args...) printk(format,##args)
130 + #define DPRINTK(format,args...)
133 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
134 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
136 +/* Number of packets whose data we look at.
137 +This can be modified through /proc/net/layer7_numpackets */
138 +static int num_packets = 10;
140 +static struct pattern_cache {
141 + char * regex_string;
143 + struct pattern_cache * next;
144 +} * first_pattern_cache = NULL;
146 +/* I'm new to locking. Here are my assumptions:
148 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
149 + if they did, nothing awful would happen.
151 +- This code will never be processing the same packet twice at the same time,
152 + because iptables rules are traversed in order.
154 +- It doesn't matter if two packets from different connections are in here at
155 + the same time, because they don't share any data.
157 +- It _does_ matter if two packets from the same connection (or one from a
158 + master and one from its child) are here at the same time. In this case,
159 + we have to protect the conntracks and the list of compiled patterns.
161 +DEFINE_RWLOCK(ct_lock);
162 +DEFINE_SPINLOCK(list_lock);
164 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
165 +/* Converts an unfriendly string into a friendly one by
166 +replacing unprintables with periods and all whitespace with " ". */
167 +static char * friendly_print(unsigned char * s)
169 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
173 + if (net_ratelimit())
174 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
178 + for(i = 0; i < strlen(s); i++){
179 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
180 + else if(isspace(s[i])) f[i] = ' ';
187 +static char dec2hex(int i)
191 + return (char)(i + '0');
194 + return (char)(i - 10 + 'a');
197 + if (net_ratelimit())
198 + printk("Problem in dec2hex\n");
203 +static char * hex_print(unsigned char * s)
205 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
209 + if (net_ratelimit())
210 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
214 + for(i = 0; i < strlen(s); i++) {
215 + g[i*3 ] = dec2hex(s[i]/16);
216 + g[i*3 + 1] = dec2hex(s[i]%16);
225 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
226 +over again, it make sense to cache the results. */
227 +static regexp * compile_and_cache(char * regex_string, char * protocol)
229 + struct pattern_cache * node = first_pattern_cache;
230 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
231 + struct pattern_cache * tmp;
234 + while (node != NULL) {
235 + if (!strcmp(node->regex_string, regex_string))
236 + return node->pattern;
238 + last_pattern_cache = node;/* points at the last non-NULL node */
242 + /* If we reach the end of the list, then we have not yet cached
243 + the pattern for this regex. Let's do that now.
244 + Be paranoid about running out of memory to avoid list corruption. */
245 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
248 + if (net_ratelimit())
249 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
253 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
254 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
257 + if(!tmp->regex_string || !tmp->pattern) {
258 + if (net_ratelimit())
259 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
260 + kfree(tmp->regex_string);
261 + kfree(tmp->pattern);
266 + /* Ok. The new node is all ready now. */
269 + if(first_pattern_cache == NULL) /* list is empty */
270 + first_pattern_cache = node; /* make node the beginning */
272 + last_pattern_cache->next = node; /* attach node to the end */
274 + /* copy the string and compile the regex */
275 + len = strlen(regex_string);
276 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
277 + node->pattern = regcomp(regex_string, &len);
278 + if ( !node->pattern ) {
279 + if (net_ratelimit())
280 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
281 + /* pattern is now cached as NULL, so we won't try again. */
284 + strcpy(node->regex_string, regex_string);
285 + return node->pattern;
288 +static int can_handle(const struct sk_buff *skb)
290 + if(!skb->nh.iph) /* not IP */
292 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
293 + skb->nh.iph->protocol != IPPROTO_UDP &&
294 + skb->nh.iph->protocol != IPPROTO_ICMP)
299 +/* Returns offset the into the skb->data that the application data starts */
300 +static int app_data_offset(const struct sk_buff *skb)
302 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
303 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
304 + int ip_hl = 4*skb->nh.iph->ihl;
306 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
307 + /* 12 == offset into TCP header for the header length field.
308 + Can't get this with skb->h.th->doff because the tcphdr
309 + struct doesn't get set when routing (this is confirmed to be
310 + true in Netfilter as well as QoS.) */
311 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
313 + return ip_hl + tcp_hl;
314 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
315 + return ip_hl + 8; /* UDP header is always 8 bytes */
316 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
317 + return ip_hl + 8; /* ICMP header is 8 bytes */
319 + if (net_ratelimit())
320 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
321 + return ip_hl + 8; /* something reasonable */
325 +/* handles whether there's a match when we aren't appending data anymore */
326 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
327 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
328 + struct ipt_layer7_info * info)
330 + /* If we're in here, throw the app data away */
331 + write_lock(&ct_lock);
332 + if(master_conntrack->layer7.app_data != NULL) {
334 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
335 + if(!master_conntrack->layer7.app_proto) {
336 + char * f = friendly_print(master_conntrack->layer7.app_data);
337 + char * g = hex_print(master_conntrack->layer7.app_data);
338 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
339 + strlen(f), TOTAL_PACKETS, f);
341 + DPRINTK("In hex: %s\n", g);
346 + kfree(master_conntrack->layer7.app_data);
347 + master_conntrack->layer7.app_data = NULL; /* don't free again */
349 + write_unlock(&ct_lock);
351 + if(master_conntrack->layer7.app_proto){
352 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
353 + write_lock(&ct_lock);
354 + if(!conntrack->layer7.app_proto) {
355 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
356 + if(!conntrack->layer7.app_proto){
357 + if (net_ratelimit())
358 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
359 + write_unlock(&ct_lock);
362 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
364 + write_unlock(&ct_lock);
366 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
369 + /* If not classified, set to "unknown" to distinguish from
370 + connections that are still being tested. */
371 + write_lock(&ct_lock);
372 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
373 + if(!master_conntrack->layer7.app_proto){
374 + if (net_ratelimit())
375 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
376 + write_unlock(&ct_lock);
379 + strcpy(master_conntrack->layer7.app_proto, "unknown");
380 + write_unlock(&ct_lock);
385 +/* add the new app data to the conntrack. Return number of bytes added. */
386 +static int add_data(struct ip_conntrack * master_conntrack,
387 + char * app_data, int appdatalen)
390 + int oldlength = master_conntrack->layer7.app_data_len;
392 + // This is a fix for a race condition by Deti Fliegl. However, I'm not
393 + // clear on whether the race condition exists or whether this really
394 + // fixes it. I might just be being dense... Anyway, if it's not really
395 + // a fix, all it does is waste a very small amount of time.
396 + if(!master_conntrack->layer7.app_data) return 0;
398 + /* Strip nulls. Make everything lower case (our regex lib doesn't
399 + do case insensitivity). Add it to the end of the current data. */
400 + for(i = 0; i < maxdatalen-oldlength-1 &&
401 + i < appdatalen; i++) {
402 + if(app_data[i] != '\0') {
403 + master_conntrack->layer7.app_data[length+oldlength] =
404 + /* the kernel version of tolower mungs 'upper ascii' */
405 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
410 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
411 + master_conntrack->layer7.app_data_len = length + oldlength;
416 +/* Returns true on match and false otherwise. */
417 +static int match(const struct sk_buff *skbin,
418 + const struct net_device *in, const struct net_device *out,
419 + const struct xt_match *match, const void *matchinfo,
420 + int offset, unsigned int protoff, int *hotdrop)
422 + /* sidestep const without getting a compiler warning... */
423 + struct sk_buff * skb = (struct sk_buff *)skbin;
425 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
426 + enum ip_conntrack_info master_ctinfo, ctinfo;
427 + struct ip_conntrack *master_conntrack, *conntrack;
428 + unsigned char * app_data;
429 + unsigned int pattern_result, appdatalen;
430 + regexp * comppattern;
432 + if(!can_handle(skb)){
433 + DPRINTK("layer7: This is some protocol I can't handle.\n");
434 + return info->invert;
437 + /* Treat parent & all its children together as one connection, except
438 + for the purpose of setting conntrack->layer7.app_proto in the actual
439 + connection. This makes /proc/net/ip_conntrack more satisfying. */
440 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
441 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
442 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
443 + return info->invert;
446 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
447 + while (master_ct(master_conntrack) != NULL)
448 + master_conntrack = master_ct(master_conntrack);
450 + /* if we've classified it or seen too many packets */
451 + if(TOTAL_PACKETS > num_packets ||
452 + master_conntrack->layer7.app_proto) {
454 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
456 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
457 + rules. I'm not sure that using cb for this purpose is correct, although
458 + it says "put your private variables there". But it doesn't look like it
459 + is being used for anything else in the skbs that make it here. How can
460 + I write to cb without making the compiler angry? */
461 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
463 + return (pattern_result ^ info->invert);
466 + if(skb_is_nonlinear(skb)){
467 + if(skb_linearize(skb) != 0){
468 + if (net_ratelimit())
469 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
470 + return info->invert;
474 + /* now that the skb is linearized, it's safe to set these. */
475 + app_data = skb->data + app_data_offset(skb);
476 + appdatalen = skb->tail - app_data;
478 + spin_lock_bh(&list_lock);
479 + /* the return value gets checked later, when we're ready to use it */
480 + comppattern = compile_and_cache(info->pattern, info->protocol);
481 + spin_unlock_bh(&list_lock);
483 + /* On the first packet of a connection, allocate space for app data */
484 + write_lock(&ct_lock);
485 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
486 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
487 + if(!master_conntrack->layer7.app_data){
488 + if (net_ratelimit())
489 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
490 + write_unlock(&ct_lock);
491 + return info->invert;
494 + master_conntrack->layer7.app_data[0] = '\0';
496 + write_unlock(&ct_lock);
498 + /* Can be here, but unallocated, if numpackets is increased near
499 + the beginning of a connection */
500 + if(master_conntrack->layer7.app_data == NULL)
501 + return (info->invert); /* unmatched */
505 + write_lock(&ct_lock);
506 + newbytes = add_data(master_conntrack, app_data, appdatalen);
507 + write_unlock(&ct_lock);
509 + if(newbytes == 0) { /* didn't add any data */
511 + /* Didn't match before, not going to match now */
512 + return info->invert;
516 + /* If looking for "unknown", then never match. "Unknown" means that
517 + we've given up; we're still trying with these packets. */
518 + read_lock(&ct_lock);
519 + if(!strcmp(info->protocol, "unknown")) {
520 + pattern_result = 0;
521 + /* If the regexp failed to compile, don't bother running it */
522 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
523 + DPRINTK("layer7: matched %s\n", info->protocol);
524 + pattern_result = 1;
525 + } else pattern_result = 0;
526 + read_unlock(&ct_lock);
528 + if(pattern_result) {
529 + write_lock(&ct_lock);
530 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
531 + if(!master_conntrack->layer7.app_proto){
532 + if (net_ratelimit())
533 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
534 + write_unlock(&ct_lock);
535 + return (pattern_result ^ info->invert);
537 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
538 + write_unlock(&ct_lock);
541 + /* mark the packet seen */
544 + return (pattern_result ^ info->invert);
547 +static struct ipt_match layer7_match = {
550 + .matchsize = sizeof(struct ipt_layer7_info),
554 +/* taken from drivers/video/modedb.c */
555 +static int my_atoi(const char *s)
562 + val = 10*val+(*s-'0');
570 +/* write out num_packets to userland. */
571 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
572 + int* eof, void * data)
574 + if(num_packets > 99 && net_ratelimit())
575 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
577 + page[0] = num_packets/10 + '0';
578 + page[1] = num_packets%10 + '0';
587 +/* Read in num_packets from userland */
588 +static int layer7_write_proc(struct file* file, const char* buffer,
589 + unsigned long count, void *data)
591 + char * foo = kmalloc(count, GFP_ATOMIC);
594 + if (net_ratelimit())
595 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
599 + if(copy_from_user(foo, buffer, count)) {
604 + num_packets = my_atoi(foo);
607 + /* This has an arbitrary limit to make the math easier. I'm lazy.
608 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
609 + if(num_packets > 99) {
610 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
612 + } else if(num_packets < 1) {
613 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
620 +/* register the proc file */
621 +static void layer7_init_proc(void)
623 + struct proc_dir_entry* entry;
624 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
625 + entry->read_proc = layer7_read_proc;
626 + entry->write_proc = layer7_write_proc;
629 +static void layer7_cleanup_proc(void)
631 + remove_proc_entry("layer7_numpackets", proc_net);
634 +static int __init ipt_layer7_init(void)
638 + layer7_init_proc();
639 + if(maxdatalen < 1) {
640 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
643 + /* This is not a hard limit. It's just here to prevent people from
644 + bringing their slow machines to a grinding halt. */
645 + else if(maxdatalen > 65536) {
646 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
647 + maxdatalen = 65536;
649 + return ipt_register_match(&layer7_match);
652 +static void __exit ipt_layer7_fini(void)
654 + layer7_cleanup_proc();
655 + ipt_unregister_match(&layer7_match);
658 +module_init(ipt_layer7_init);
659 +module_exit(ipt_layer7_fini);
660 diff -urN linux.old/net/ipv4/netfilter/Kconfig linux.dev/net/ipv4/netfilter/Kconfig
661 --- linux.old/net/ipv4/netfilter/Kconfig 2007-01-01 05:17:07.000000000 +0100
662 +++ linux.dev/net/ipv4/netfilter/Kconfig 2007-01-01 05:18:48.000000000 +0100
665 To compile it as a module, choose M here. If unsure, say N.
667 +config IP_NF_MATCH_LAYER7
668 + tristate "Layer 7 match support (EXPERIMENTAL)"
669 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
671 + Say Y if you want to be able to classify connections (and their
672 + packets) based on regular expression matching of their application
673 + layer data. This is one way to classify applications such as
674 + peer-to-peer filesharing systems that do not always use the same
677 + To compile it as a module, choose M here. If unsure, say N.
679 +config IP_NF_MATCH_LAYER7_DEBUG
680 + bool "Layer 7 debugging output"
681 + depends on IP_NF_MATCH_LAYER7
683 + Say Y to get lots of debugging output.
685 config IP_NF_MATCH_TOS
686 tristate "TOS match support"
687 depends on IP_NF_IPTABLES
688 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
689 --- linux.old/net/ipv4/netfilter/Makefile 2007-01-01 05:17:07.000000000 +0100
690 +++ linux.dev/net/ipv4/netfilter/Makefile 2007-01-01 05:18:48.000000000 +0100
692 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
693 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
695 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
698 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
699 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
700 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.c linux.dev/net/ipv4/netfilter/regexp/regexp.c
701 --- linux.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
702 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.c 2007-01-01 05:18:48.000000000 +0100
705 + * regcomp and regexec -- regsub and regerror are elsewhere
706 + * @(#)regexp.c 1.3 of 18 April 87
708 + * Copyright (c) 1986 by University of Toronto.
709 + * Written by Henry Spencer. Not derived from licensed software.
711 + * Permission is granted to anyone to use this software for any
712 + * purpose on any computer system, and to redistribute it freely,
713 + * subject to the following restrictions:
715 + * 1. The author is not responsible for the consequences of use of
716 + * this software, no matter how awful, even if they arise
717 + * from defects in it.
719 + * 2. The origin of this software must not be misrepresented, either
720 + * by explicit claim or by omission.
722 + * 3. Altered versions must be plainly marked as such, and must not
723 + * be misrepresented as being the original software.
725 + * Beware that some of this code is subtly aware of the way operator
726 + * precedence is structured in regular expressions. Serious changes in
727 + * regular-expression syntax might require a total rethink.
729 + * This code was modified by Ethan Sommer to work within the kernel
730 + * (it now uses kmalloc etc..)
732 + * Modified slightly by Matthew Strait to use more modern C.
736 +#include "regmagic.h"
738 +/* added by ethan and matt. Lets it work in both kernel and user space.
739 +(So iptables can use it, for instance.) Yea, it goes both ways... */
741 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
743 + #define printk(format,args...) printf(format,##args)
746 +void regerror(char * s)
748 + printk("<3>Regexp: %s\n", s);
753 + * The "internal use only" fields in regexp.h are present to pass info from
754 + * compile to execute that permits the execute phase to run lots faster on
755 + * simple cases. They are:
757 + * regstart char that must begin a match; '\0' if none obvious
758 + * reganch is the match anchored (at beginning-of-line only)?
759 + * regmust string (pointer into program) that match must include, or NULL
760 + * regmlen length of regmust string
762 + * Regstart and reganch permit very fast decisions on suitable starting points
763 + * for a match, cutting down the work a lot. Regmust permits fast rejection
764 + * of lines that cannot possibly match. The regmust tests are costly enough
765 + * that regcomp() supplies a regmust only if the r.e. contains something
766 + * potentially expensive (at present, the only such thing detected is * or +
767 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
768 + * supplied because the test in regexec() needs it and regcomp() is computing
773 + * Structure for regexp "program". This is essentially a linear encoding
774 + * of a nondeterministic finite-state machine (aka syntax charts or
775 + * "railroad normal form" in parsing technology). Each node is an opcode
776 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
777 + * all nodes except BRANCH implement concatenation; a "next" pointer with
778 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
779 + * have one of the subtle syntax dependencies: an individual BRANCH (as
780 + * opposed to a collection of them) is never concatenated with anything
781 + * because of operator precedence.) The operand of some types of node is
782 + * a literal string; for others, it is a node leading into a sub-FSM. In
783 + * particular, the operand of a BRANCH node is the first node of the branch.
784 + * (NB this is *not* a tree structure: the tail of the branch connects
785 + * to the thing following the set of BRANCHes.) The opcodes are:
788 +/* definition number opnd? meaning */
789 +#define END 0 /* no End of program. */
790 +#define BOL 1 /* no Match "" at beginning of line. */
791 +#define EOL 2 /* no Match "" at end of line. */
792 +#define ANY 3 /* no Match any one character. */
793 +#define ANYOF 4 /* str Match any character in this string. */
794 +#define ANYBUT 5 /* str Match any character not in this string. */
795 +#define BRANCH 6 /* node Match this alternative, or the next... */
796 +#define BACK 7 /* no Match "", "next" ptr points backward. */
797 +#define EXACTLY 8 /* str Match this string. */
798 +#define NOTHING 9 /* no Match empty string. */
799 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
800 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
801 +#define OPEN 20 /* no Mark this point in input as start of #n. */
802 + /* OPEN+1 is number 1, etc. */
803 +#define CLOSE 30 /* no Analogous to OPEN. */
808 + * BRANCH The set of branches constituting a single choice are hooked
809 + * together with their "next" pointers, since precedence prevents
810 + * anything being concatenated to any individual branch. The
811 + * "next" pointer of the last BRANCH in a choice points to the
812 + * thing following the whole choice. This is also where the
813 + * final "next" pointer of each individual branch points; each
814 + * branch starts with the operand node of a BRANCH node.
816 + * BACK Normal "next" pointers all implicitly point forward; BACK
817 + * exists to make loop structures possible.
819 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
820 + * BRANCH structures using BACK. Simple cases (one character
821 + * per match) are implemented with STAR and PLUS for speed
822 + * and to minimize recursive plunges.
824 + * OPEN,CLOSE ...are numbered at compile time.
828 + * A node is one char of opcode followed by two chars of "next" pointer.
829 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
830 + * value is a positive offset from the opcode of the node containing it.
831 + * An operand, if any, simply follows the node. (Note that much of the
832 + * code generation knows about this implicit relationship.)
834 + * Using two bytes for the "next" pointer is vast overkill for most things,
835 + * but allows patterns to get big without disasters.
837 +#define OP(p) (*(p))
838 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
839 +#define OPERAND(p) ((p) + 3)
842 + * See regmagic.h for one further detail of program structure.
847 + * Utility definitions.
850 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
852 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
855 +#define FAIL(m) { regerror(m); return(NULL); }
856 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
857 +#define META "^$.[()|?+*\\"
860 + * Flags to be passed up and down.
862 +#define HASWIDTH 01 /* Known never to match null string. */
863 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
864 +#define SPSTART 04 /* Starts with * or +. */
865 +#define WORST 0 /* Worst case. */
868 + * Global work variables for regcomp().
870 +struct match_globals {
871 +char *reginput; /* String-input pointer. */
872 +char *regbol; /* Beginning of input, for ^ check. */
873 +char **regstartp; /* Pointer to startp array. */
874 +char **regendp; /* Ditto for endp. */
875 +char *regparse; /* Input-scan pointer. */
876 +int regnpar; /* () count. */
878 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
879 +long regsize; /* Code size. */
883 + * Forward declarations for regcomp()'s friends.
886 +#define STATIC static
888 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
889 +STATIC char *regbranch(struct match_globals *g, int *flagp);
890 +STATIC char *regpiece(struct match_globals *g, int *flagp);
891 +STATIC char *regatom(struct match_globals *g, int *flagp);
892 +STATIC char *regnode(struct match_globals *g, char op);
893 +STATIC char *regnext(struct match_globals *g, char *p);
894 +STATIC void regc(struct match_globals *g, char b);
895 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
896 +STATIC void regtail(struct match_globals *g, char *p, char *val);
897 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
900 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
907 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
908 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
909 + if (*scan1 == *scan2++)
917 + - regcomp - compile a regular expression into internal code
919 + * We can't allocate space until we know how big the compiled form will be,
920 + * but we can't compile it (and thus know how big it is) until we've got a
921 + * place to put the code. So we cheat: we compile it twice, once with code
922 + * generation turned off and size counting turned on, and once "for real".
923 + * This also means that we don't allocate space until we are sure that the
924 + * thing really will compile successfully, and we never have to move the
925 + * code and thus invalidate pointers into it. (Note that it has to be in
926 + * one piece because free() must be able to free it all.)
928 + * Beware that the optimization-preparation code in here knows about some
929 + * of the structure of the compiled regexp.
932 +regcomp(char *exp,int *patternsize)
934 + register regexp *r;
935 + register char *scan;
936 + register char *longest;
939 + struct match_globals g;
941 + /* commented out by ethan
942 + extern char *malloc();
946 + FAIL("NULL argument");
948 + /* First pass: determine size, legality. */
952 + g.regcode = &g.regdummy;
954 + if (reg(&g, 0, &flags) == NULL)
957 + /* Small enough for pointer-storage convention? */
958 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
959 + FAIL("regexp too big");
961 + /* Allocate space. */
962 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
963 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
965 + FAIL("out of space");
967 + /* Second pass: emit code. */
970 + g.regcode = r->program;
972 + if (reg(&g, 0, &flags) == NULL)
975 + /* Dig out information for optimizations. */
976 + r->regstart = '\0'; /* Worst-case defaults. */
980 + scan = r->program+1; /* First BRANCH. */
981 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
982 + scan = OPERAND(scan);
984 + /* Starting-point info. */
985 + if (OP(scan) == EXACTLY)
986 + r->regstart = *OPERAND(scan);
987 + else if (OP(scan) == BOL)
991 + * If there's something expensive in the r.e., find the
992 + * longest literal string that must appear and make it the
993 + * regmust. Resolve ties in favor of later strings, since
994 + * the regstart check works with the beginning of the r.e.
995 + * and avoiding duplication strengthens checking. Not a
996 + * strong reason, but sufficient in the absence of others.
998 + if (flags&SPSTART) {
1001 + for (; scan != NULL; scan = regnext(&g, scan))
1002 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1003 + longest = OPERAND(scan);
1004 + len = strlen(OPERAND(scan));
1006 + r->regmust = longest;
1015 + - reg - regular expression, i.e. main body or parenthesized thing
1017 + * Caller must absorb opening parenthesis.
1019 + * Combining parenthesis handling with the base level of regular expression
1020 + * is a trifle forced, but the need to tie the tails of the branches to what
1021 + * follows makes it hard to avoid.
1024 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1026 + register char *ret;
1027 + register char *br;
1028 + register char *ender;
1029 + register int parno = 0; /* 0 makes gcc happy */
1032 + *flagp = HASWIDTH; /* Tentatively. */
1034 + /* Make an OPEN node, if parenthesized. */
1036 + if (g->regnpar >= NSUBEXP)
1037 + FAIL("too many ()");
1038 + parno = g->regnpar;
1040 + ret = regnode(g, OPEN+parno);
1044 + /* Pick up the branches, linking them together. */
1045 + br = regbranch(g, &flags);
1049 + regtail(g, ret, br); /* OPEN -> first. */
1052 + if (!(flags&HASWIDTH))
1053 + *flagp &= ~HASWIDTH;
1054 + *flagp |= flags&SPSTART;
1055 + while (*g->regparse == '|') {
1057 + br = regbranch(g, &flags);
1060 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1061 + if (!(flags&HASWIDTH))
1062 + *flagp &= ~HASWIDTH;
1063 + *flagp |= flags&SPSTART;
1066 + /* Make a closing node, and hook it on the end. */
1067 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1068 + regtail(g, ret, ender);
1070 + /* Hook the tails of the branches to the closing node. */
1071 + for (br = ret; br != NULL; br = regnext(g, br))
1072 + regoptail(g, br, ender);
1074 + /* Check for proper termination. */
1075 + if (paren && *g->regparse++ != ')') {
1076 + FAIL("unmatched ()");
1077 + } else if (!paren && *g->regparse != '\0') {
1078 + if (*g->regparse == ')') {
1079 + FAIL("unmatched ()");
1081 + FAIL("junk on end"); /* "Can't happen". */
1089 + - regbranch - one alternative of an | operator
1091 + * Implements the concatenation operator.
1094 +regbranch(struct match_globals *g, int *flagp)
1096 + register char *ret;
1097 + register char *chain;
1098 + register char *latest;
1101 + *flagp = WORST; /* Tentatively. */
1103 + ret = regnode(g, BRANCH);
1105 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1106 + latest = regpiece(g, &flags);
1107 + if (latest == NULL)
1109 + *flagp |= flags&HASWIDTH;
1110 + if (chain == NULL) /* First piece. */
1111 + *flagp |= flags&SPSTART;
1113 + regtail(g, chain, latest);
1116 + if (chain == NULL) /* Loop ran zero times. */
1117 + (void) regnode(g, NOTHING);
1123 + - regpiece - something followed by possible [*+?]
1125 + * Note that the branching code sequences used for ? and the general cases
1126 + * of * and + are somewhat optimized: they use the same NOTHING node as
1127 + * both the endmarker for their branch list and the body of the last branch.
1128 + * It might seem that this node could be dispensed with entirely, but the
1129 + * endmarker role is not redundant.
1132 +regpiece(struct match_globals *g, int *flagp)
1134 + register char *ret;
1136 + register char *next;
1139 + ret = regatom(g, &flags);
1143 + op = *g->regparse;
1144 + if (!ISMULT(op)) {
1149 + if (!(flags&HASWIDTH) && op != '?')
1150 + FAIL("*+ operand could be empty");
1151 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1153 + if (op == '*' && (flags&SIMPLE))
1154 + reginsert(g, STAR, ret);
1155 + else if (op == '*') {
1156 + /* Emit x* as (x&|), where & means "self". */
1157 + reginsert(g, BRANCH, ret); /* Either x */
1158 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1159 + regoptail(g, ret, ret); /* back */
1160 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1161 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1162 + } else if (op == '+' && (flags&SIMPLE))
1163 + reginsert(g, PLUS, ret);
1164 + else if (op == '+') {
1165 + /* Emit x+ as x(&|), where & means "self". */
1166 + next = regnode(g, BRANCH); /* Either */
1167 + regtail(g, ret, next);
1168 + regtail(g, regnode(g, BACK), ret); /* loop back */
1169 + regtail(g, next, regnode(g, BRANCH)); /* or */
1170 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1171 + } else if (op == '?') {
1172 + /* Emit x? as (x|) */
1173 + reginsert(g, BRANCH, ret); /* Either x */
1174 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1175 + next = regnode(g, NOTHING); /* null. */
1176 + regtail(g, ret, next);
1177 + regoptail(g, ret, next);
1180 + if (ISMULT(*g->regparse))
1181 + FAIL("nested *?+");
1187 + - regatom - the lowest level
1189 + * Optimization: gobbles an entire sequence of ordinary characters so that
1190 + * it can turn them into a single node, which is smaller to store and
1191 + * faster to run. Backslashed characters are exceptions, each becoming a
1192 + * separate node; the code is simpler that way and it's not worth fixing.
1195 +regatom(struct match_globals *g, int *flagp)
1197 + register char *ret;
1200 + *flagp = WORST; /* Tentatively. */
1202 + switch (*g->regparse++) {
1204 + ret = regnode(g, BOL);
1207 + ret = regnode(g, EOL);
1210 + ret = regnode(g, ANY);
1211 + *flagp |= HASWIDTH|SIMPLE;
1214 + register int class;
1215 + register int classend;
1217 + if (*g->regparse == '^') { /* Complement of range. */
1218 + ret = regnode(g, ANYBUT);
1221 + ret = regnode(g, ANYOF);
1222 + if (*g->regparse == ']' || *g->regparse == '-')
1223 + regc(g, *g->regparse++);
1224 + while (*g->regparse != '\0' && *g->regparse != ']') {
1225 + if (*g->regparse == '-') {
1227 + if (*g->regparse == ']' || *g->regparse == '\0')
1230 + class = UCHARAT(g->regparse-2)+1;
1231 + classend = UCHARAT(g->regparse);
1232 + if (class > classend+1)
1233 + FAIL("invalid [] range");
1234 + for (; class <= classend; class++)
1239 + regc(g, *g->regparse++);
1242 + if (*g->regparse != ']')
1243 + FAIL("unmatched []");
1245 + *flagp |= HASWIDTH|SIMPLE;
1249 + ret = reg(g, 1, &flags);
1252 + *flagp |= flags&(HASWIDTH|SPSTART);
1257 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1262 + FAIL("?+* follows nothing");
1265 + if (*g->regparse == '\0')
1266 + FAIL("trailing \\");
1267 + ret = regnode(g, EXACTLY);
1268 + regc(g, *g->regparse++);
1270 + *flagp |= HASWIDTH|SIMPLE;
1274 + register char ender;
1277 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1279 + FAIL("internal disaster");
1280 + ender = *(g->regparse+len);
1281 + if (len > 1 && ISMULT(ender))
1282 + len--; /* Back off clear of ?+* operand. */
1283 + *flagp |= HASWIDTH;
1286 + ret = regnode(g, EXACTLY);
1288 + regc(g, *g->regparse++);
1300 + - regnode - emit a node
1302 +static char * /* Location. */
1303 +regnode(struct match_globals *g, char op)
1305 + register char *ret;
1306 + register char *ptr;
1309 + if (ret == &g->regdummy) {
1316 + *ptr++ = '\0'; /* Null "next" pointer. */
1324 + - regc - emit (if appropriate) a byte of code
1327 +regc(struct match_globals *g, char b)
1329 + if (g->regcode != &g->regdummy)
1330 + *g->regcode++ = b;
1336 + - reginsert - insert an operator in front of already-emitted operand
1338 + * Means relocating the operand.
1341 +reginsert(struct match_globals *g, char op, char* opnd)
1343 + register char *src;
1344 + register char *dst;
1345 + register char *place;
1347 + if (g->regcode == &g->regdummy) {
1355 + while (src > opnd)
1358 + place = opnd; /* Op node, where operand used to be. */
1365 + - regtail - set the next-pointer at the end of a node chain
1368 +regtail(struct match_globals *g, char *p, char *val)
1370 + register char *scan;
1371 + register char *temp;
1372 + register int offset;
1374 + if (p == &g->regdummy)
1377 + /* Find last node. */
1380 + temp = regnext(g, scan);
1386 + if (OP(scan) == BACK)
1387 + offset = scan - val;
1389 + offset = val - scan;
1390 + *(scan+1) = (offset>>8)&0377;
1391 + *(scan+2) = offset&0377;
1395 + - regoptail - regtail on operand of first argument; nop if operandless
1398 +regoptail(struct match_globals *g, char *p, char *val)
1400 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1401 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1403 + regtail(g, OPERAND(p), val);
1407 + * regexec and friends
1414 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1415 +STATIC int regmatch(struct match_globals *g, char *prog);
1416 +STATIC int regrepeat(struct match_globals *g, char *p);
1419 +int regnarrate = 0;
1421 +STATIC char *regprop(char *op);
1425 + - regexec - match a regexp against a string
1428 +regexec(regexp *prog, char *string)
1431 + struct match_globals g;
1433 + /* Be paranoid... */
1434 + if (prog == NULL || string == NULL) {
1435 + printk("<3>Regexp: NULL parameter\n");
1439 + /* Check validity of program. */
1440 + if (UCHARAT(prog->program) != MAGIC) {
1441 + printk("<3>Regexp: corrupted program\n");
1445 + /* If there is a "must appear" string, look for it. */
1446 + if (prog->regmust != NULL) {
1448 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1449 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1450 + break; /* Found it. */
1453 + if (s == NULL) /* Not present. */
1457 + /* Mark beginning of line for ^ . */
1458 + g.regbol = string;
1460 + /* Simplest case: anchored match need be tried only once. */
1461 + if (prog->reganch)
1462 + return(regtry(&g, prog, string));
1464 + /* Messy cases: unanchored match. */
1466 + if (prog->regstart != '\0')
1467 + /* We know what char it must start with. */
1468 + while ((s = strchr(s, prog->regstart)) != NULL) {
1469 + if (regtry(&g, prog, s))
1474 + /* We don't -- general case. */
1476 + if (regtry(&g, prog, s))
1478 + } while (*s++ != '\0');
1485 + - regtry - try match at specific point
1487 +static int /* 0 failure, 1 success */
1488 +regtry(struct match_globals *g, regexp *prog, char *string)
1491 + register char **sp;
1492 + register char **ep;
1494 + g->reginput = string;
1495 + g->regstartp = prog->startp;
1496 + g->regendp = prog->endp;
1498 + sp = prog->startp;
1500 + for (i = NSUBEXP; i > 0; i--) {
1504 + if (regmatch(g, prog->program + 1)) {
1505 + prog->startp[0] = string;
1506 + prog->endp[0] = g->reginput;
1513 + - regmatch - main matching routine
1515 + * Conceptually the strategy is simple: check to see whether the current
1516 + * node matches, call self recursively to see whether the rest matches,
1517 + * and then act accordingly. In practice we make some effort to avoid
1518 + * recursion, in particular by going through "ordinary" nodes (that don't
1519 + * need to know whether the rest of the match failed) by a loop instead of
1522 +static int /* 0 failure, 1 success */
1523 +regmatch(struct match_globals *g, char *prog)
1525 + register char *scan = prog; /* Current node. */
1526 + char *next; /* Next node. */
1529 + if (scan != NULL && regnarrate)
1530 + fprintf(stderr, "%s(\n", regprop(scan));
1532 + while (scan != NULL) {
1535 + fprintf(stderr, "%s...\n", regprop(scan));
1537 + next = regnext(g, scan);
1539 + switch (OP(scan)) {
1541 + if (g->reginput != g->regbol)
1545 + if (*g->reginput != '\0')
1549 + if (*g->reginput == '\0')
1555 + register char *opnd;
1557 + opnd = OPERAND(scan);
1558 + /* Inline the first character, for speed. */
1559 + if (*opnd != *g->reginput)
1561 + len = strlen(opnd);
1562 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1564 + g->reginput += len;
1568 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1573 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1590 + register char *save;
1592 + no = OP(scan) - OPEN;
1593 + save = g->reginput;
1595 + if (regmatch(g, next)) {
1597 + * Don't set startp if some later
1598 + * invocation of the same parentheses
1601 + if (g->regstartp[no] == NULL)
1602 + g->regstartp[no] = save;
1619 + register char *save;
1621 + no = OP(scan) - CLOSE;
1622 + save = g->reginput;
1624 + if (regmatch(g, next)) {
1626 + * Don't set endp if some later
1627 + * invocation of the same parentheses
1630 + if (g->regendp[no] == NULL)
1631 + g->regendp[no] = save;
1638 + register char *save;
1640 + if (OP(next) != BRANCH) /* No choice. */
1641 + next = OPERAND(scan); /* Avoid recursion. */
1644 + save = g->reginput;
1645 + if (regmatch(g, OPERAND(scan)))
1647 + g->reginput = save;
1648 + scan = regnext(g, scan);
1649 + } while (scan != NULL && OP(scan) == BRANCH);
1657 + register char nextch;
1659 + register char *save;
1663 + * Lookahead to avoid useless match attempts
1664 + * when we know what character comes next.
1667 + if (OP(next) == EXACTLY)
1668 + nextch = *OPERAND(next);
1669 + min = (OP(scan) == STAR) ? 0 : 1;
1670 + save = g->reginput;
1671 + no = regrepeat(g, OPERAND(scan));
1672 + while (no >= min) {
1673 + /* If it could work, try it. */
1674 + if (nextch == '\0' || *g->reginput == nextch)
1675 + if (regmatch(g, next))
1677 + /* Couldn't or didn't -- back up. */
1679 + g->reginput = save + no;
1685 + return(1); /* Success! */
1688 + printk("<3>Regexp: memory corruption\n");
1697 + * We get here only if there's trouble -- normally "case END" is
1698 + * the terminating point.
1700 + printk("<3>Regexp: corrupted pointers\n");
1705 + - regrepeat - repeatedly match something simple, report how many
1708 +regrepeat(struct match_globals *g, char *p)
1710 + register int count = 0;
1711 + register char *scan;
1712 + register char *opnd;
1714 + scan = g->reginput;
1715 + opnd = OPERAND(p);
1718 + count = strlen(scan);
1722 + while (*opnd == *scan) {
1728 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1734 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1739 + default: /* Oh dear. Called inappropriately. */
1740 + printk("<3>Regexp: internal foulup\n");
1741 + count = 0; /* Best compromise. */
1744 + g->reginput = scan;
1750 + - regnext - dig the "next" pointer out of a node
1753 +regnext(struct match_globals *g, char *p)
1755 + register int offset;
1757 + if (p == &g->regdummy)
1764 + if (OP(p) == BACK)
1772 +STATIC char *regprop();
1775 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1781 + register char op = EXACTLY; /* Arbitrary non-END op. */
1782 + register char *next;
1783 + /* extern char *strchr(); */
1786 + s = r->program + 1;
1787 + while (op != END) { /* While that wasn't END last time... */
1789 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1790 + next = regnext(s);
1791 + if (next == NULL) /* Next ptr. */
1794 + printf("(%d)", (s-r->program)+(next-s));
1796 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1797 + /* Literal string, where present. */
1798 + while (*s != '\0') {
1807 + /* Header fields of interest. */
1808 + if (r->regstart != '\0')
1809 + printf("start `%c' ", r->regstart);
1811 + printf("anchored ");
1812 + if (r->regmust != NULL)
1813 + printf("must have \"%s\"", r->regmust);
1818 + - regprop - printable representation of opcode
1825 + static char buf[BUFLEN];
1869 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1881 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1891 + printk("<3>Regexp: corrupted opcode\n");
1895 + strncat(buf, p, BUFLEN-strlen(buf));
1901 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.h linux.dev/net/ipv4/netfilter/regexp/regexp.h
1902 --- linux.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1903 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.h 2007-01-01 05:18:48.000000000 +0100
1906 + * Definitions etc. for regexp(3) routines.
1908 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1909 + * not the System V one.
1917 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1918 +which contains a version of this library, says:
1921 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1922 + * will not work properly.
1925 +However, it looks rather like this library is limited to 10. If you think
1926 +otherwise, let us know.
1930 +typedef struct regexp {
1931 + char *startp[NSUBEXP];
1932 + char *endp[NSUBEXP];
1933 + char regstart; /* Internal use only. */
1934 + char reganch; /* Internal use only. */
1935 + char *regmust; /* Internal use only. */
1936 + int regmlen; /* Internal use only. */
1937 + char program[1]; /* Unwarranted chumminess with compiler. */
1940 +regexp * regcomp(char *exp, int *patternsize);
1941 +int regexec(regexp *prog, char *string);
1942 +void regsub(regexp *prog, char *source, char *dest);
1943 +void regerror(char *s);
1946 diff -urN linux.old/net/ipv4/netfilter/regexp/regmagic.h linux.dev/net/ipv4/netfilter/regexp/regmagic.h
1947 --- linux.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1948 +++ linux.dev/net/ipv4/netfilter/regexp/regmagic.h 2007-01-01 05:18:48.000000000 +0100
1951 + * The first byte of the regexp internal "program" is actually this magic
1952 + * number; the start node begins in the second byte.
1955 diff -urN linux.old/net/ipv4/netfilter/regexp/regsub.c linux.dev/net/ipv4/netfilter/regexp/regsub.c
1956 --- linux.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1957 +++ linux.dev/net/ipv4/netfilter/regexp/regsub.c 2007-01-01 05:18:48.000000000 +0100
1961 + * @(#)regsub.c 1.3 of 2 April 86
1963 + * Copyright (c) 1986 by University of Toronto.
1964 + * Written by Henry Spencer. Not derived from licensed software.
1966 + * Permission is granted to anyone to use this software for any
1967 + * purpose on any computer system, and to redistribute it freely,
1968 + * subject to the following restrictions:
1970 + * 1. The author is not responsible for the consequences of use of
1971 + * this software, no matter how awful, even if they arise
1972 + * from defects in it.
1974 + * 2. The origin of this software must not be misrepresented, either
1975 + * by explicit claim or by omission.
1977 + * 3. Altered versions must be plainly marked as such, and must not
1978 + * be misrepresented as being the original software.
1981 + * This code was modified by Ethan Sommer to work within the kernel
1982 + * (it now uses kmalloc etc..)
1985 +#include "regexp.h"
1986 +#include "regmagic.h"
1987 +#include <linux/string.h>
1991 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1993 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1997 +//void regerror(char * s)
1999 +// printk("regexp(3): %s", s);
2000 +// /* NOTREACHED */
2005 + - regsub - perform substitutions after a regexp match
2008 +regsub(regexp * prog, char * source, char * dest)
2010 + register char *src;
2011 + register char *dst;
2016 + /* Not necessary and gcc doesn't like it -MLS */
2017 + /*extern char *strncpy();*/
2019 + if (prog == NULL || source == NULL || dest == NULL) {
2020 + regerror("NULL parm to regsub");
2023 + if (UCHARAT(prog->program) != MAGIC) {
2024 + regerror("damaged regexp fed to regsub");
2030 + while ((c = *src++) != '\0') {
2033 + else if (c == '\\' && '0' <= *src && *src <= '9')
2034 + no = *src++ - '0';
2038 + if (no < 0) { /* Ordinary character. */
2039 + if (c == '\\' && (*src == '\\' || *src == '&'))
2042 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2043 + len = prog->endp[no] - prog->startp[no];
2044 + (void) strncpy(dst, prog->startp[no], len);
2046 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2047 + regerror("damaged match string");