1 --- linux-2.6.28-stock/net/netfilter/Kconfig 2009-01-07 16:05:35.000000000 -0600
2 +++ linux-2.6.28/net/netfilter/Kconfig 2009-01-07 16:07:31.000000000 -0600
3 @@ -795,6 +795,27 @@ config NETFILTER_XT_MATCH_STATE
5 To compile it as a module, choose M here. If unsure, say N.
7 +config NETFILTER_XT_MATCH_LAYER7
8 + tristate '"layer7" match support'
9 + depends on NETFILTER_XTABLES
10 + depends on EXPERIMENTAL && (IP_NF_CONNTRACK || NF_CONNTRACK)
12 + Say Y if you want to be able to classify connections (and their
13 + packets) based on regular expression matching of their application
14 + layer data. This is one way to classify applications such as
15 + peer-to-peer filesharing systems that do not always use the same
18 + To compile it as a module, choose M here. If unsure, say N.
21 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
22 + bool 'Layer 7 debugging output'
23 + depends on NETFILTER_XT_MATCH_LAYER7
25 + Say Y to get lots of debugging output.
28 config NETFILTER_XT_MATCH_STATISTIC
29 tristate '"statistic" match support'
30 depends on NETFILTER_ADVANCED
31 --- linux-2.6.28-stock/net/netfilter/Makefile 2009-01-07 16:05:35.000000000 -0600
32 +++ linux-2.6.28/net/netfilter/Makefile 2009-01-07 16:07:31.000000000 -0600
33 @@ -84,6 +84,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_RECENT)
34 obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o
35 obj-$(CONFIG_NETFILTER_XT_MATCH_SOCKET) += xt_socket.o
36 obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o
37 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
38 obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o
39 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
40 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
41 --- linux-2.6.28-stock/net/netfilter/xt_layer7.c 1969-12-31 18:00:00.000000000 -0600
42 +++ linux-2.6.28/net/netfilter/xt_layer7.c 2009-01-07 20:47:14.000000000 -0600
45 + Kernel module to match application layer (OSI layer 7) data in connections.
47 + http://l7-filter.sf.net
49 + (C) 2003-2009 Matthew Strait and Ethan Sommer.
51 + This program is free software; you can redistribute it and/or
52 + modify it under the terms of the GNU General Public License
53 + as published by the Free Software Foundation; either version
54 + 2 of the License, or (at your option) any later version.
55 + http://www.gnu.org/licenses/gpl.txt
57 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>,
58 + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait,
59 + Ethan Sommer, Justin Levandoski.
62 +#include <linux/spinlock.h>
63 +#include <linux/version.h>
66 +#include <linux/module.h>
67 +#include <linux/skbuff.h>
68 +#include <linux/netfilter.h>
69 +#include <net/netfilter/nf_conntrack.h>
70 +#include <net/netfilter/nf_conntrack_core.h>
71 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
72 +#include <net/netfilter/nf_conntrack_extend.h>
73 +#include <net/netfilter/nf_conntrack_acct.h>
75 +#include <linux/netfilter/x_tables.h>
76 +#include <linux/netfilter/xt_layer7.h>
77 +#include <linux/ctype.h>
78 +#include <linux/proc_fs.h>
80 +#include "regexp/regexp.c"
82 +MODULE_LICENSE("GPL");
83 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
84 +MODULE_DESCRIPTION("iptables application layer match module");
85 +MODULE_ALIAS("ipt_layer7");
86 +MODULE_VERSION("2.21");
88 +static int maxdatalen = 2048; // this is the default
89 +module_param(maxdatalen, int, 0444);
90 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
91 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
92 + #define DPRINTK(format,args...) printk(format,##args)
94 + #define DPRINTK(format,args...)
97 +/* Number of packets whose data we look at.
98 +This can be modified through /proc/net/layer7_numpackets */
99 +static int num_packets = 10;
101 +static struct pattern_cache {
102 + char * regex_string;
104 + struct pattern_cache * next;
105 +} * first_pattern_cache = NULL;
107 +DEFINE_SPINLOCK(l7_lock);
109 +static int total_acct_packets(struct nf_conn *ct)
111 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 26)
112 + BUG_ON(ct == NULL);
113 + return (ct->counters[IP_CT_DIR_ORIGINAL].packets + ct->counters[IP_CT_DIR_REPLY].packets);
115 + struct nf_conn_counter *acct;
117 + BUG_ON(ct == NULL);
118 + acct = nf_conn_acct_find(ct);
121 + return (atomic64_read(&acct[IP_CT_DIR_ORIGINAL].packets) + atomic64_read(&acct[IP_CT_DIR_REPLY].packets));
125 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
126 +/* Converts an unfriendly string into a friendly one by
127 +replacing unprintables with periods and all whitespace with " ". */
128 +static char * friendly_print(unsigned char * s)
130 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
134 + if (net_ratelimit())
135 + printk(KERN_ERR "layer7: out of memory in "
136 + "friendly_print, bailing.\n");
140 + for(i = 0; i < strlen(s); i++){
141 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
142 + else if(isspace(s[i])) f[i] = ' ';
149 +static char dec2hex(int i)
156 + return (i - 10 + 'a');
159 + if (net_ratelimit())
160 + printk("layer7: Problem in dec2hex\n");
165 +static char * hex_print(unsigned char * s)
167 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
171 + if (net_ratelimit())
172 + printk(KERN_ERR "layer7: out of memory in hex_print, "
177 + for(i = 0; i < strlen(s); i++) {
178 + g[i*3 ] = dec2hex(s[i]/16);
179 + g[i*3 + 1] = dec2hex(s[i]%16);
188 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
189 +over again, it make sense to cache the results. */
190 +static regexp * compile_and_cache(const char * regex_string,
191 + const char * protocol)
193 + struct pattern_cache * node = first_pattern_cache;
194 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
195 + struct pattern_cache * tmp;
198 + while (node != NULL) {
199 + if (!strcmp(node->regex_string, regex_string))
200 + return node->pattern;
202 + last_pattern_cache = node;/* points at the last non-NULL node */
206 + /* If we reach the end of the list, then we have not yet cached
207 + the pattern for this regex. Let's do that now.
208 + Be paranoid about running out of memory to avoid list corruption. */
209 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
212 + if (net_ratelimit())
213 + printk(KERN_ERR "layer7: out of memory in "
214 + "compile_and_cache, bailing.\n");
218 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
219 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
222 + if(!tmp->regex_string || !tmp->pattern) {
223 + if (net_ratelimit())
224 + printk(KERN_ERR "layer7: out of memory in "
225 + "compile_and_cache, bailing.\n");
226 + kfree(tmp->regex_string);
227 + kfree(tmp->pattern);
232 + /* Ok. The new node is all ready now. */
235 + if(first_pattern_cache == NULL) /* list is empty */
236 + first_pattern_cache = node; /* make node the beginning */
238 + last_pattern_cache->next = node; /* attach node to the end */
240 + /* copy the string and compile the regex */
241 + len = strlen(regex_string);
242 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
243 + node->pattern = regcomp((char *)regex_string, &len);
244 + if ( !node->pattern ) {
245 + if (net_ratelimit())
246 + printk(KERN_ERR "layer7: Error compiling regexp "
248 + regex_string, protocol);
249 + /* pattern is now cached as NULL, so we won't try again. */
252 + strcpy(node->regex_string, regex_string);
253 + return node->pattern;
256 +static int can_handle(const struct sk_buff *skb)
258 + if(!ip_hdr(skb)) /* not IP */
260 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
261 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
262 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
267 +/* Returns offset the into the skb->data that the application data starts */
268 +static int app_data_offset(const struct sk_buff *skb)
270 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
271 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
272 + int ip_hl = 4*ip_hdr(skb)->ihl;
274 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
275 + /* 12 == offset into TCP header for the header length field.
276 + Can't get this with skb->h.th->doff because the tcphdr
277 + struct doesn't get set when routing (this is confirmed to be
278 + true in Netfilter as well as QoS.) */
279 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
281 + return ip_hl + tcp_hl;
282 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
283 + return ip_hl + 8; /* UDP header is always 8 bytes */
284 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
285 + return ip_hl + 8; /* ICMP header is 8 bytes */
287 + if (net_ratelimit())
288 + printk(KERN_ERR "layer7: tried to handle unknown "
290 + return ip_hl + 8; /* something reasonable */
294 +/* handles whether there's a match when we aren't appending data anymore */
295 +static int match_no_append(struct nf_conn * conntrack,
296 + struct nf_conn * master_conntrack,
297 + enum ip_conntrack_info ctinfo,
298 + enum ip_conntrack_info master_ctinfo,
299 + const struct xt_layer7_info * info)
301 + /* If we're in here, throw the app data away */
302 + if(master_conntrack->layer7.app_data != NULL) {
304 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
305 + if(!master_conntrack->layer7.app_proto) {
307 + friendly_print(master_conntrack->layer7.app_data);
309 + hex_print(master_conntrack->layer7.app_data);
310 + DPRINTK("\nl7-filter gave up after %d bytes "
311 + "(%d packets):\n%s\n",
312 + strlen(f), total_acct_packets(master_conntrack), f);
314 + DPRINTK("In hex: %s\n", g);
319 + kfree(master_conntrack->layer7.app_data);
320 + master_conntrack->layer7.app_data = NULL; /* don't free again */
323 + if(master_conntrack->layer7.app_proto){
324 + /* Here child connections set their .app_proto (for /proc) */
325 + if(!conntrack->layer7.app_proto) {
326 + conntrack->layer7.app_proto =
327 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
329 + if(!conntrack->layer7.app_proto){
330 + if (net_ratelimit())
331 + printk(KERN_ERR "layer7: out of memory "
332 + "in match_no_append, "
336 + strcpy(conntrack->layer7.app_proto,
337 + master_conntrack->layer7.app_proto);
340 + return (!strcmp(master_conntrack->layer7.app_proto,
344 + /* If not classified, set to "unknown" to distinguish from
345 + connections that are still being tested. */
346 + master_conntrack->layer7.app_proto =
347 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
348 + if(!master_conntrack->layer7.app_proto){
349 + if (net_ratelimit())
350 + printk(KERN_ERR "layer7: out of memory in "
351 + "match_no_append, bailing.\n");
354 + strcpy(master_conntrack->layer7.app_proto, "unknown");
359 +/* add the new app data to the conntrack. Return number of bytes added. */
360 +static int add_data(struct nf_conn * master_conntrack,
361 + char * app_data, int appdatalen)
364 + int oldlength = master_conntrack->layer7.app_data_len;
366 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
367 + clear on whether the race condition exists or whether this really
368 + fixes it. I might just be being dense... Anyway, if it's not really
369 + a fix, all it does is waste a very small amount of time. */
370 + if(!master_conntrack->layer7.app_data) return 0;
372 + /* Strip nulls. Make everything lower case (our regex lib doesn't
373 + do case insensitivity). Add it to the end of the current data. */
374 + for(i = 0; i < maxdatalen-oldlength-1 &&
375 + i < appdatalen; i++) {
376 + if(app_data[i] != '\0') {
377 + /* the kernel version of tolower mungs 'upper ascii' */
378 + master_conntrack->layer7.app_data[length+oldlength] =
379 + isascii(app_data[i])?
380 + tolower(app_data[i]) : app_data[i];
385 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
386 + master_conntrack->layer7.app_data_len = length + oldlength;
391 +/* taken from drivers/video/modedb.c */
392 +static int my_atoi(const char *s)
399 + val = 10*val+(*s-'0');
407 +/* write out num_packets to userland. */
408 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
409 + int* eof, void * data)
411 + if(num_packets > 99 && net_ratelimit())
412 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
414 + page[0] = num_packets/10 + '0';
415 + page[1] = num_packets%10 + '0';
424 +/* Read in num_packets from userland */
425 +static int layer7_write_proc(struct file* file, const char* buffer,
426 + unsigned long count, void *data)
428 + char * foo = kmalloc(count, GFP_ATOMIC);
431 + if (net_ratelimit())
432 + printk(KERN_ERR "layer7: out of memory, bailing. "
433 + "num_packets unchanged.\n");
437 + if(copy_from_user(foo, buffer, count)) {
442 + num_packets = my_atoi(foo);
445 + /* This has an arbitrary limit to make the math easier. I'm lazy.
446 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
447 + if(num_packets > 99) {
448 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
450 + } else if(num_packets < 1) {
451 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
459 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
460 +match(const struct sk_buff *skbin, struct xt_action_param *par)
462 +match(const struct sk_buff *skbin,
463 + const struct net_device *in,
464 + const struct net_device *out,
465 + const struct xt_match *match,
466 + const void *matchinfo,
468 + unsigned int protoff,
472 + /* sidestep const without getting a compiler warning... */
473 + struct sk_buff * skb = (struct sk_buff *)skbin;
475 + const struct xt_layer7_info * info =
476 + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
482 + enum ip_conntrack_info master_ctinfo, ctinfo;
483 + struct nf_conn *master_conntrack, *conntrack;
484 + unsigned char * app_data;
485 + unsigned int pattern_result, appdatalen;
486 + regexp * comppattern;
488 + /* Be paranoid/incompetent - lock the entire match function. */
489 + spin_lock_bh(&l7_lock);
491 + if(!can_handle(skb)){
492 + DPRINTK("layer7: This is some protocol I can't handle.\n");
493 + spin_unlock_bh(&l7_lock);
494 + return info->invert;
497 + /* Treat parent & all its children together as one connection, except
498 + for the purpose of setting conntrack->layer7.app_proto in the actual
499 + connection. This makes /proc/net/ip_conntrack more satisfying. */
500 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
501 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
502 + DPRINTK("layer7: couldn't get conntrack.\n");
503 + spin_unlock_bh(&l7_lock);
504 + return info->invert;
507 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
508 + while (master_ct(master_conntrack) != NULL)
509 + master_conntrack = master_ct(master_conntrack);
511 + /* if we've classified it or seen too many packets */
512 + if(total_acct_packets(master_conntrack) > num_packets ||
513 + master_conntrack->layer7.app_proto) {
515 + pattern_result = match_no_append(conntrack, master_conntrack,
516 + ctinfo, master_ctinfo, info);
518 + /* skb->cb[0] == seen. Don't do things twice if there are
519 + multiple l7 rules. I'm not sure that using cb for this purpose
520 + is correct, even though it says "put your private variables
521 + there". But it doesn't look like it is being used for anything
522 + else in the skbs that make it here. */
523 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
525 + spin_unlock_bh(&l7_lock);
526 + return (pattern_result ^ info->invert);
529 + if(skb_is_nonlinear(skb)){
530 + if(skb_linearize(skb) != 0){
531 + if (net_ratelimit())
532 + printk(KERN_ERR "layer7: failed to linearize "
533 + "packet, bailing.\n");
534 + spin_unlock_bh(&l7_lock);
535 + return info->invert;
539 + /* now that the skb is linearized, it's safe to set these. */
540 + app_data = skb->data + app_data_offset(skb);
541 + appdatalen = skb_tail_pointer(skb) - app_data;
543 + /* the return value gets checked later, when we're ready to use it */
544 + comppattern = compile_and_cache(info->pattern, info->protocol);
546 + /* On the first packet of a connection, allocate space for app data */
547 + if(total_acct_packets(master_conntrack) == 1 && !skb->cb[0] &&
548 + !master_conntrack->layer7.app_data){
549 + master_conntrack->layer7.app_data =
550 + kmalloc(maxdatalen, GFP_ATOMIC);
551 + if(!master_conntrack->layer7.app_data){
552 + if (net_ratelimit())
553 + printk(KERN_ERR "layer7: out of memory in "
554 + "match, bailing.\n");
555 + spin_unlock_bh(&l7_lock);
556 + return info->invert;
559 + master_conntrack->layer7.app_data[0] = '\0';
562 + /* Can be here, but unallocated, if numpackets is increased near
563 + the beginning of a connection */
564 + if(master_conntrack->layer7.app_data == NULL){
565 + spin_unlock_bh(&l7_lock);
566 + return info->invert; /* unmatched */
571 + newbytes = add_data(master_conntrack, app_data, appdatalen);
573 + if(newbytes == 0) { /* didn't add any data */
575 + /* Didn't match before, not going to match now */
576 + spin_unlock_bh(&l7_lock);
577 + return info->invert;
581 + /* If looking for "unknown", then never match. "Unknown" means that
582 + we've given up; we're still trying with these packets. */
583 + if(!strcmp(info->protocol, "unknown")) {
584 + pattern_result = 0;
585 + /* If looking for "unset", then always match. "Unset" means that we
586 + haven't yet classified the connection. */
587 + } else if(!strcmp(info->protocol, "unset")) {
588 + pattern_result = 2;
589 + DPRINTK("layer7: matched unset: not yet classified "
590 + "(%d/%d packets)\n",
591 + total_acct_packets(master_conntrack), num_packets);
592 + /* If the regexp failed to compile, don't bother running it */
593 + } else if(comppattern &&
594 + regexec(comppattern, master_conntrack->layer7.app_data)){
595 + DPRINTK("layer7: matched %s\n", info->protocol);
596 + pattern_result = 1;
597 + } else pattern_result = 0;
599 + if(pattern_result == 1) {
600 + master_conntrack->layer7.app_proto =
601 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
602 + if(!master_conntrack->layer7.app_proto){
603 + if (net_ratelimit())
604 + printk(KERN_ERR "layer7: out of memory in "
605 + "match, bailing.\n");
606 + spin_unlock_bh(&l7_lock);
607 + return (pattern_result ^ info->invert);
609 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
610 + } else if(pattern_result > 1) { /* cleanup from "unset" */
611 + pattern_result = 1;
614 + /* mark the packet seen */
617 + spin_unlock_bh(&l7_lock);
618 + return (pattern_result ^ info->invert);
621 +// load nf_conntrack_ipv4
622 +static int check(const struct xt_mtchk_param *par)
624 + if (nf_ct_l3proto_try_module_get(par->family) < 0) {
625 + pr_info("can't load conntrack support for "
626 + "proto=%d\n", par->family);
633 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
634 + static void destroy(const struct xt_mtdtor_param *par)
636 + nf_ct_l3proto_module_put(par->family);
639 + static void destroy(const struct xt_match *match, void *matchinfo)
641 + nf_ct_l3proto_module_put(match->family);
645 +static struct xt_match xt_layer7_match[] __read_mostly = {
649 + .checkentry = check,
651 + .destroy = destroy,
652 + .matchsize = sizeof(struct xt_layer7_info),
657 +static void layer7_cleanup_proc(void)
659 + remove_proc_entry("layer7_numpackets", init_net.proc_net);
662 +/* register the proc file */
663 +static void layer7_init_proc(void)
665 + struct proc_dir_entry* entry;
666 + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
667 + entry->read_proc = layer7_read_proc;
668 + entry->write_proc = layer7_write_proc;
671 +static int __init xt_layer7_init(void)
675 + layer7_init_proc();
676 + if(maxdatalen < 1) {
677 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
681 + /* This is not a hard limit. It's just here to prevent people from
682 + bringing their slow machines to a grinding halt. */
683 + else if(maxdatalen > 65536) {
684 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
686 + maxdatalen = 65536;
688 + return xt_register_matches(xt_layer7_match,
689 + ARRAY_SIZE(xt_layer7_match));
692 +static void __exit xt_layer7_fini(void)
694 + layer7_cleanup_proc();
695 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
698 +module_init(xt_layer7_init);
699 +module_exit(xt_layer7_fini);
700 --- linux-2.6.28-stock/net/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
701 +++ linux-2.6.28/net/netfilter/regexp/regexp.c 2009-01-07 16:07:31.000000000 -0600
704 + * regcomp and regexec -- regsub and regerror are elsewhere
705 + * @(#)regexp.c 1.3 of 18 April 87
707 + * Copyright (c) 1986 by University of Toronto.
708 + * Written by Henry Spencer. Not derived from licensed software.
710 + * Permission is granted to anyone to use this software for any
711 + * purpose on any computer system, and to redistribute it freely,
712 + * subject to the following restrictions:
714 + * 1. The author is not responsible for the consequences of use of
715 + * this software, no matter how awful, even if they arise
716 + * from defects in it.
718 + * 2. The origin of this software must not be misrepresented, either
719 + * by explicit claim or by omission.
721 + * 3. Altered versions must be plainly marked as such, and must not
722 + * be misrepresented as being the original software.
724 + * Beware that some of this code is subtly aware of the way operator
725 + * precedence is structured in regular expressions. Serious changes in
726 + * regular-expression syntax might require a total rethink.
728 + * This code was modified by Ethan Sommer to work within the kernel
729 + * (it now uses kmalloc etc..)
731 + * Modified slightly by Matthew Strait to use more modern C.
735 +#include "regmagic.h"
737 +/* added by ethan and matt. Lets it work in both kernel and user space.
738 +(So iptables can use it, for instance.) Yea, it goes both ways... */
740 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
742 + #define printk(format,args...) printf(format,##args)
745 +void regerror(char * s)
747 + printk("<3>Regexp: %s\n", s);
752 + * The "internal use only" fields in regexp.h are present to pass info from
753 + * compile to execute that permits the execute phase to run lots faster on
754 + * simple cases. They are:
756 + * regstart char that must begin a match; '\0' if none obvious
757 + * reganch is the match anchored (at beginning-of-line only)?
758 + * regmust string (pointer into program) that match must include, or NULL
759 + * regmlen length of regmust string
761 + * Regstart and reganch permit very fast decisions on suitable starting points
762 + * for a match, cutting down the work a lot. Regmust permits fast rejection
763 + * of lines that cannot possibly match. The regmust tests are costly enough
764 + * that regcomp() supplies a regmust only if the r.e. contains something
765 + * potentially expensive (at present, the only such thing detected is * or +
766 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
767 + * supplied because the test in regexec() needs it and regcomp() is computing
772 + * Structure for regexp "program". This is essentially a linear encoding
773 + * of a nondeterministic finite-state machine (aka syntax charts or
774 + * "railroad normal form" in parsing technology). Each node is an opcode
775 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
776 + * all nodes except BRANCH implement concatenation; a "next" pointer with
777 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
778 + * have one of the subtle syntax dependencies: an individual BRANCH (as
779 + * opposed to a collection of them) is never concatenated with anything
780 + * because of operator precedence.) The operand of some types of node is
781 + * a literal string; for others, it is a node leading into a sub-FSM. In
782 + * particular, the operand of a BRANCH node is the first node of the branch.
783 + * (NB this is *not* a tree structure: the tail of the branch connects
784 + * to the thing following the set of BRANCHes.) The opcodes are:
787 +/* definition number opnd? meaning */
788 +#define END 0 /* no End of program. */
789 +#define BOL 1 /* no Match "" at beginning of line. */
790 +#define EOL 2 /* no Match "" at end of line. */
791 +#define ANY 3 /* no Match any one character. */
792 +#define ANYOF 4 /* str Match any character in this string. */
793 +#define ANYBUT 5 /* str Match any character not in this string. */
794 +#define BRANCH 6 /* node Match this alternative, or the next... */
795 +#define BACK 7 /* no Match "", "next" ptr points backward. */
796 +#define EXACTLY 8 /* str Match this string. */
797 +#define NOTHING 9 /* no Match empty string. */
798 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
799 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
800 +#define OPEN 20 /* no Mark this point in input as start of #n. */
801 + /* OPEN+1 is number 1, etc. */
802 +#define CLOSE 30 /* no Analogous to OPEN. */
807 + * BRANCH The set of branches constituting a single choice are hooked
808 + * together with their "next" pointers, since precedence prevents
809 + * anything being concatenated to any individual branch. The
810 + * "next" pointer of the last BRANCH in a choice points to the
811 + * thing following the whole choice. This is also where the
812 + * final "next" pointer of each individual branch points; each
813 + * branch starts with the operand node of a BRANCH node.
815 + * BACK Normal "next" pointers all implicitly point forward; BACK
816 + * exists to make loop structures possible.
818 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
819 + * BRANCH structures using BACK. Simple cases (one character
820 + * per match) are implemented with STAR and PLUS for speed
821 + * and to minimize recursive plunges.
823 + * OPEN,CLOSE ...are numbered at compile time.
827 + * A node is one char of opcode followed by two chars of "next" pointer.
828 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
829 + * value is a positive offset from the opcode of the node containing it.
830 + * An operand, if any, simply follows the node. (Note that much of the
831 + * code generation knows about this implicit relationship.)
833 + * Using two bytes for the "next" pointer is vast overkill for most things,
834 + * but allows patterns to get big without disasters.
836 +#define OP(p) (*(p))
837 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
838 +#define OPERAND(p) ((p) + 3)
841 + * See regmagic.h for one further detail of program structure.
846 + * Utility definitions.
849 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
851 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
854 +#define FAIL(m) { regerror(m); return(NULL); }
855 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
856 +#define META "^$.[()|?+*\\"
859 + * Flags to be passed up and down.
861 +#define HASWIDTH 01 /* Known never to match null string. */
862 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
863 +#define SPSTART 04 /* Starts with * or +. */
864 +#define WORST 0 /* Worst case. */
867 + * Global work variables for regcomp().
869 +struct match_globals {
870 +char *reginput; /* String-input pointer. */
871 +char *regbol; /* Beginning of input, for ^ check. */
872 +char **regstartp; /* Pointer to startp array. */
873 +char **regendp; /* Ditto for endp. */
874 +char *regparse; /* Input-scan pointer. */
875 +int regnpar; /* () count. */
877 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
878 +long regsize; /* Code size. */
882 + * Forward declarations for regcomp()'s friends.
885 +#define STATIC static
887 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
888 +STATIC char *regbranch(struct match_globals *g, int *flagp);
889 +STATIC char *regpiece(struct match_globals *g, int *flagp);
890 +STATIC char *regatom(struct match_globals *g, int *flagp);
891 +STATIC char *regnode(struct match_globals *g, char op);
892 +STATIC char *regnext(struct match_globals *g, char *p);
893 +STATIC void regc(struct match_globals *g, char b);
894 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
895 +STATIC void regtail(struct match_globals *g, char *p, char *val);
896 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
899 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
906 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
907 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
908 + if (*scan1 == *scan2++)
916 + - regcomp - compile a regular expression into internal code
918 + * We can't allocate space until we know how big the compiled form will be,
919 + * but we can't compile it (and thus know how big it is) until we've got a
920 + * place to put the code. So we cheat: we compile it twice, once with code
921 + * generation turned off and size counting turned on, and once "for real".
922 + * This also means that we don't allocate space until we are sure that the
923 + * thing really will compile successfully, and we never have to move the
924 + * code and thus invalidate pointers into it. (Note that it has to be in
925 + * one piece because free() must be able to free it all.)
927 + * Beware that the optimization-preparation code in here knows about some
928 + * of the structure of the compiled regexp.
931 +regcomp(char *exp,int *patternsize)
933 + register regexp *r;
934 + register char *scan;
935 + register char *longest;
938 + struct match_globals g;
940 + /* commented out by ethan
941 + extern char *malloc();
945 + FAIL("NULL argument");
947 + /* First pass: determine size, legality. */
951 + g.regcode = &g.regdummy;
953 + if (reg(&g, 0, &flags) == NULL)
956 + /* Small enough for pointer-storage convention? */
957 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
958 + FAIL("regexp too big");
960 + /* Allocate space. */
961 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
962 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
964 + FAIL("out of space");
966 + /* Second pass: emit code. */
969 + g.regcode = r->program;
971 + if (reg(&g, 0, &flags) == NULL)
974 + /* Dig out information for optimizations. */
975 + r->regstart = '\0'; /* Worst-case defaults. */
979 + scan = r->program+1; /* First BRANCH. */
980 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
981 + scan = OPERAND(scan);
983 + /* Starting-point info. */
984 + if (OP(scan) == EXACTLY)
985 + r->regstart = *OPERAND(scan);
986 + else if (OP(scan) == BOL)
990 + * If there's something expensive in the r.e., find the
991 + * longest literal string that must appear and make it the
992 + * regmust. Resolve ties in favor of later strings, since
993 + * the regstart check works with the beginning of the r.e.
994 + * and avoiding duplication strengthens checking. Not a
995 + * strong reason, but sufficient in the absence of others.
997 + if (flags&SPSTART) {
1000 + for (; scan != NULL; scan = regnext(&g, scan))
1001 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1002 + longest = OPERAND(scan);
1003 + len = strlen(OPERAND(scan));
1005 + r->regmust = longest;
1014 + - reg - regular expression, i.e. main body or parenthesized thing
1016 + * Caller must absorb opening parenthesis.
1018 + * Combining parenthesis handling with the base level of regular expression
1019 + * is a trifle forced, but the need to tie the tails of the branches to what
1020 + * follows makes it hard to avoid.
1023 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1025 + register char *ret;
1026 + register char *br;
1027 + register char *ender;
1028 + register int parno = 0; /* 0 makes gcc happy */
1031 + *flagp = HASWIDTH; /* Tentatively. */
1033 + /* Make an OPEN node, if parenthesized. */
1035 + if (g->regnpar >= NSUBEXP)
1036 + FAIL("too many ()");
1037 + parno = g->regnpar;
1039 + ret = regnode(g, OPEN+parno);
1043 + /* Pick up the branches, linking them together. */
1044 + br = regbranch(g, &flags);
1048 + regtail(g, ret, br); /* OPEN -> first. */
1051 + if (!(flags&HASWIDTH))
1052 + *flagp &= ~HASWIDTH;
1053 + *flagp |= flags&SPSTART;
1054 + while (*g->regparse == '|') {
1056 + br = regbranch(g, &flags);
1059 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1060 + if (!(flags&HASWIDTH))
1061 + *flagp &= ~HASWIDTH;
1062 + *flagp |= flags&SPSTART;
1065 + /* Make a closing node, and hook it on the end. */
1066 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1067 + regtail(g, ret, ender);
1069 + /* Hook the tails of the branches to the closing node. */
1070 + for (br = ret; br != NULL; br = regnext(g, br))
1071 + regoptail(g, br, ender);
1073 + /* Check for proper termination. */
1074 + if (paren && *g->regparse++ != ')') {
1075 + FAIL("unmatched ()");
1076 + } else if (!paren && *g->regparse != '\0') {
1077 + if (*g->regparse == ')') {
1078 + FAIL("unmatched ()");
1080 + FAIL("junk on end"); /* "Can't happen". */
1088 + - regbranch - one alternative of an | operator
1090 + * Implements the concatenation operator.
1093 +regbranch(struct match_globals *g, int *flagp)
1095 + register char *ret;
1096 + register char *chain;
1097 + register char *latest;
1100 + *flagp = WORST; /* Tentatively. */
1102 + ret = regnode(g, BRANCH);
1104 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1105 + latest = regpiece(g, &flags);
1106 + if (latest == NULL)
1108 + *flagp |= flags&HASWIDTH;
1109 + if (chain == NULL) /* First piece. */
1110 + *flagp |= flags&SPSTART;
1112 + regtail(g, chain, latest);
1115 + if (chain == NULL) /* Loop ran zero times. */
1116 + (void) regnode(g, NOTHING);
1122 + - regpiece - something followed by possible [*+?]
1124 + * Note that the branching code sequences used for ? and the general cases
1125 + * of * and + are somewhat optimized: they use the same NOTHING node as
1126 + * both the endmarker for their branch list and the body of the last branch.
1127 + * It might seem that this node could be dispensed with entirely, but the
1128 + * endmarker role is not redundant.
1131 +regpiece(struct match_globals *g, int *flagp)
1133 + register char *ret;
1135 + register char *next;
1138 + ret = regatom(g, &flags);
1142 + op = *g->regparse;
1143 + if (!ISMULT(op)) {
1148 + if (!(flags&HASWIDTH) && op != '?')
1149 + FAIL("*+ operand could be empty");
1150 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1152 + if (op == '*' && (flags&SIMPLE))
1153 + reginsert(g, STAR, ret);
1154 + else if (op == '*') {
1155 + /* Emit x* as (x&|), where & means "self". */
1156 + reginsert(g, BRANCH, ret); /* Either x */
1157 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1158 + regoptail(g, ret, ret); /* back */
1159 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1160 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1161 + } else if (op == '+' && (flags&SIMPLE))
1162 + reginsert(g, PLUS, ret);
1163 + else if (op == '+') {
1164 + /* Emit x+ as x(&|), where & means "self". */
1165 + next = regnode(g, BRANCH); /* Either */
1166 + regtail(g, ret, next);
1167 + regtail(g, regnode(g, BACK), ret); /* loop back */
1168 + regtail(g, next, regnode(g, BRANCH)); /* or */
1169 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1170 + } else if (op == '?') {
1171 + /* Emit x? as (x|) */
1172 + reginsert(g, BRANCH, ret); /* Either x */
1173 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1174 + next = regnode(g, NOTHING); /* null. */
1175 + regtail(g, ret, next);
1176 + regoptail(g, ret, next);
1179 + if (ISMULT(*g->regparse))
1180 + FAIL("nested *?+");
1186 + - regatom - the lowest level
1188 + * Optimization: gobbles an entire sequence of ordinary characters so that
1189 + * it can turn them into a single node, which is smaller to store and
1190 + * faster to run. Backslashed characters are exceptions, each becoming a
1191 + * separate node; the code is simpler that way and it's not worth fixing.
1194 +regatom(struct match_globals *g, int *flagp)
1196 + register char *ret;
1199 + *flagp = WORST; /* Tentatively. */
1201 + switch (*g->regparse++) {
1203 + ret = regnode(g, BOL);
1206 + ret = regnode(g, EOL);
1209 + ret = regnode(g, ANY);
1210 + *flagp |= HASWIDTH|SIMPLE;
1213 + register int class;
1214 + register int classend;
1216 + if (*g->regparse == '^') { /* Complement of range. */
1217 + ret = regnode(g, ANYBUT);
1220 + ret = regnode(g, ANYOF);
1221 + if (*g->regparse == ']' || *g->regparse == '-')
1222 + regc(g, *g->regparse++);
1223 + while (*g->regparse != '\0' && *g->regparse != ']') {
1224 + if (*g->regparse == '-') {
1226 + if (*g->regparse == ']' || *g->regparse == '\0')
1229 + class = UCHARAT(g->regparse-2)+1;
1230 + classend = UCHARAT(g->regparse);
1231 + if (class > classend+1)
1232 + FAIL("invalid [] range");
1233 + for (; class <= classend; class++)
1238 + regc(g, *g->regparse++);
1241 + if (*g->regparse != ']')
1242 + FAIL("unmatched []");
1244 + *flagp |= HASWIDTH|SIMPLE;
1248 + ret = reg(g, 1, &flags);
1251 + *flagp |= flags&(HASWIDTH|SPSTART);
1256 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1261 + FAIL("?+* follows nothing");
1264 + if (*g->regparse == '\0')
1265 + FAIL("trailing \\");
1266 + ret = regnode(g, EXACTLY);
1267 + regc(g, *g->regparse++);
1269 + *flagp |= HASWIDTH|SIMPLE;
1273 + register char ender;
1276 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1278 + FAIL("internal disaster");
1279 + ender = *(g->regparse+len);
1280 + if (len > 1 && ISMULT(ender))
1281 + len--; /* Back off clear of ?+* operand. */
1282 + *flagp |= HASWIDTH;
1285 + ret = regnode(g, EXACTLY);
1287 + regc(g, *g->regparse++);
1299 + - regnode - emit a node
1301 +static char * /* Location. */
1302 +regnode(struct match_globals *g, char op)
1304 + register char *ret;
1305 + register char *ptr;
1308 + if (ret == &g->regdummy) {
1315 + *ptr++ = '\0'; /* Null "next" pointer. */
1323 + - regc - emit (if appropriate) a byte of code
1326 +regc(struct match_globals *g, char b)
1328 + if (g->regcode != &g->regdummy)
1329 + *g->regcode++ = b;
1335 + - reginsert - insert an operator in front of already-emitted operand
1337 + * Means relocating the operand.
1340 +reginsert(struct match_globals *g, char op, char* opnd)
1342 + register char *src;
1343 + register char *dst;
1344 + register char *place;
1346 + if (g->regcode == &g->regdummy) {
1354 + while (src > opnd)
1357 + place = opnd; /* Op node, where operand used to be. */
1364 + - regtail - set the next-pointer at the end of a node chain
1367 +regtail(struct match_globals *g, char *p, char *val)
1369 + register char *scan;
1370 + register char *temp;
1371 + register int offset;
1373 + if (p == &g->regdummy)
1376 + /* Find last node. */
1379 + temp = regnext(g, scan);
1385 + if (OP(scan) == BACK)
1386 + offset = scan - val;
1388 + offset = val - scan;
1389 + *(scan+1) = (offset>>8)&0377;
1390 + *(scan+2) = offset&0377;
1394 + - regoptail - regtail on operand of first argument; nop if operandless
1397 +regoptail(struct match_globals *g, char *p, char *val)
1399 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1400 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1402 + regtail(g, OPERAND(p), val);
1406 + * regexec and friends
1413 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1414 +STATIC int regmatch(struct match_globals *g, char *prog);
1415 +STATIC int regrepeat(struct match_globals *g, char *p);
1418 +int regnarrate = 0;
1420 +STATIC char *regprop(char *op);
1424 + - regexec - match a regexp against a string
1427 +regexec(regexp *prog, char *string)
1430 + struct match_globals g;
1432 + /* Be paranoid... */
1433 + if (prog == NULL || string == NULL) {
1434 + printk("<3>Regexp: NULL parameter\n");
1438 + /* Check validity of program. */
1439 + if (UCHARAT(prog->program) != MAGIC) {
1440 + printk("<3>Regexp: corrupted program\n");
1444 + /* If there is a "must appear" string, look for it. */
1445 + if (prog->regmust != NULL) {
1447 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1448 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1449 + break; /* Found it. */
1452 + if (s == NULL) /* Not present. */
1456 + /* Mark beginning of line for ^ . */
1457 + g.regbol = string;
1459 + /* Simplest case: anchored match need be tried only once. */
1460 + if (prog->reganch)
1461 + return(regtry(&g, prog, string));
1463 + /* Messy cases: unanchored match. */
1465 + if (prog->regstart != '\0')
1466 + /* We know what char it must start with. */
1467 + while ((s = strchr(s, prog->regstart)) != NULL) {
1468 + if (regtry(&g, prog, s))
1473 + /* We don't -- general case. */
1475 + if (regtry(&g, prog, s))
1477 + } while (*s++ != '\0');
1484 + - regtry - try match at specific point
1486 +static int /* 0 failure, 1 success */
1487 +regtry(struct match_globals *g, regexp *prog, char *string)
1490 + register char **sp;
1491 + register char **ep;
1493 + g->reginput = string;
1494 + g->regstartp = prog->startp;
1495 + g->regendp = prog->endp;
1497 + sp = prog->startp;
1499 + for (i = NSUBEXP; i > 0; i--) {
1503 + if (regmatch(g, prog->program + 1)) {
1504 + prog->startp[0] = string;
1505 + prog->endp[0] = g->reginput;
1512 + - regmatch - main matching routine
1514 + * Conceptually the strategy is simple: check to see whether the current
1515 + * node matches, call self recursively to see whether the rest matches,
1516 + * and then act accordingly. In practice we make some effort to avoid
1517 + * recursion, in particular by going through "ordinary" nodes (that don't
1518 + * need to know whether the rest of the match failed) by a loop instead of
1521 +static int /* 0 failure, 1 success */
1522 +regmatch(struct match_globals *g, char *prog)
1524 + register char *scan = prog; /* Current node. */
1525 + char *next; /* Next node. */
1528 + if (scan != NULL && regnarrate)
1529 + fprintf(stderr, "%s(\n", regprop(scan));
1531 + while (scan != NULL) {
1534 + fprintf(stderr, "%s...\n", regprop(scan));
1536 + next = regnext(g, scan);
1538 + switch (OP(scan)) {
1540 + if (g->reginput != g->regbol)
1544 + if (*g->reginput != '\0')
1548 + if (*g->reginput == '\0')
1554 + register char *opnd;
1556 + opnd = OPERAND(scan);
1557 + /* Inline the first character, for speed. */
1558 + if (*opnd != *g->reginput)
1560 + len = strlen(opnd);
1561 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1563 + g->reginput += len;
1567 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1572 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1589 + register char *save;
1591 + no = OP(scan) - OPEN;
1592 + save = g->reginput;
1594 + if (regmatch(g, next)) {
1596 + * Don't set startp if some later
1597 + * invocation of the same parentheses
1600 + if (g->regstartp[no] == NULL)
1601 + g->regstartp[no] = save;
1618 + register char *save;
1620 + no = OP(scan) - CLOSE;
1621 + save = g->reginput;
1623 + if (regmatch(g, next)) {
1625 + * Don't set endp if some later
1626 + * invocation of the same parentheses
1629 + if (g->regendp[no] == NULL)
1630 + g->regendp[no] = save;
1637 + register char *save;
1639 + if (OP(next) != BRANCH) /* No choice. */
1640 + next = OPERAND(scan); /* Avoid recursion. */
1643 + save = g->reginput;
1644 + if (regmatch(g, OPERAND(scan)))
1646 + g->reginput = save;
1647 + scan = regnext(g, scan);
1648 + } while (scan != NULL && OP(scan) == BRANCH);
1656 + register char nextch;
1658 + register char *save;
1662 + * Lookahead to avoid useless match attempts
1663 + * when we know what character comes next.
1666 + if (OP(next) == EXACTLY)
1667 + nextch = *OPERAND(next);
1668 + min = (OP(scan) == STAR) ? 0 : 1;
1669 + save = g->reginput;
1670 + no = regrepeat(g, OPERAND(scan));
1671 + while (no >= min) {
1672 + /* If it could work, try it. */
1673 + if (nextch == '\0' || *g->reginput == nextch)
1674 + if (regmatch(g, next))
1676 + /* Couldn't or didn't -- back up. */
1678 + g->reginput = save + no;
1684 + return(1); /* Success! */
1687 + printk("<3>Regexp: memory corruption\n");
1696 + * We get here only if there's trouble -- normally "case END" is
1697 + * the terminating point.
1699 + printk("<3>Regexp: corrupted pointers\n");
1704 + - regrepeat - repeatedly match something simple, report how many
1707 +regrepeat(struct match_globals *g, char *p)
1709 + register int count = 0;
1710 + register char *scan;
1711 + register char *opnd;
1713 + scan = g->reginput;
1714 + opnd = OPERAND(p);
1717 + count = strlen(scan);
1721 + while (*opnd == *scan) {
1727 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1733 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1738 + default: /* Oh dear. Called inappropriately. */
1739 + printk("<3>Regexp: internal foulup\n");
1740 + count = 0; /* Best compromise. */
1743 + g->reginput = scan;
1749 + - regnext - dig the "next" pointer out of a node
1752 +regnext(struct match_globals *g, char *p)
1754 + register int offset;
1756 + if (p == &g->regdummy)
1763 + if (OP(p) == BACK)
1771 +STATIC char *regprop();
1774 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1780 + register char op = EXACTLY; /* Arbitrary non-END op. */
1781 + register char *next;
1782 + /* extern char *strchr(); */
1785 + s = r->program + 1;
1786 + while (op != END) { /* While that wasn't END last time... */
1788 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1789 + next = regnext(s);
1790 + if (next == NULL) /* Next ptr. */
1793 + printf("(%d)", (s-r->program)+(next-s));
1795 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1796 + /* Literal string, where present. */
1797 + while (*s != '\0') {
1806 + /* Header fields of interest. */
1807 + if (r->regstart != '\0')
1808 + printf("start `%c' ", r->regstart);
1810 + printf("anchored ");
1811 + if (r->regmust != NULL)
1812 + printf("must have \"%s\"", r->regmust);
1817 + - regprop - printable representation of opcode
1824 + static char buf[BUFLEN];
1868 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1880 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1890 + printk("<3>Regexp: corrupted opcode\n");
1894 + strncat(buf, p, BUFLEN-strlen(buf));
1900 --- linux-2.6.28-stock/net/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1901 +++ linux-2.6.28/net/netfilter/regexp/regexp.h 2009-01-07 16:07:31.000000000 -0600
1904 + * Definitions etc. for regexp(3) routines.
1906 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1907 + * not the System V one.
1915 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1916 +which contains a version of this library, says:
1919 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1920 + * will not work properly.
1923 +However, it looks rather like this library is limited to 10. If you think
1924 +otherwise, let us know.
1928 +typedef struct regexp {
1929 + char *startp[NSUBEXP];
1930 + char *endp[NSUBEXP];
1931 + char regstart; /* Internal use only. */
1932 + char reganch; /* Internal use only. */
1933 + char *regmust; /* Internal use only. */
1934 + int regmlen; /* Internal use only. */
1935 + char program[1]; /* Unwarranted chumminess with compiler. */
1938 +regexp * regcomp(char *exp, int *patternsize);
1939 +int regexec(regexp *prog, char *string);
1940 +void regsub(regexp *prog, char *source, char *dest);
1941 +void regerror(char *s);
1944 --- linux-2.6.28-stock/net/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1945 +++ linux-2.6.28/net/netfilter/regexp/regmagic.h 2009-01-07 16:07:31.000000000 -0600
1948 + * The first byte of the regexp internal "program" is actually this magic
1949 + * number; the start node begins in the second byte.
1952 --- linux-2.6.28-stock/net/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1953 +++ linux-2.6.28/net/netfilter/regexp/regsub.c 2009-01-07 16:07:31.000000000 -0600
1957 + * @(#)regsub.c 1.3 of 2 April 86
1959 + * Copyright (c) 1986 by University of Toronto.
1960 + * Written by Henry Spencer. Not derived from licensed software.
1962 + * Permission is granted to anyone to use this software for any
1963 + * purpose on any computer system, and to redistribute it freely,
1964 + * subject to the following restrictions:
1966 + * 1. The author is not responsible for the consequences of use of
1967 + * this software, no matter how awful, even if they arise
1968 + * from defects in it.
1970 + * 2. The origin of this software must not be misrepresented, either
1971 + * by explicit claim or by omission.
1973 + * 3. Altered versions must be plainly marked as such, and must not
1974 + * be misrepresented as being the original software.
1977 + * This code was modified by Ethan Sommer to work within the kernel
1978 + * (it now uses kmalloc etc..)
1981 +#include "regexp.h"
1982 +#include "regmagic.h"
1983 +#include <linux/string.h>
1987 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1989 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1993 +//void regerror(char * s)
1995 +// printk("regexp(3): %s", s);
1996 +// /* NOTREACHED */
2001 + - regsub - perform substitutions after a regexp match
2004 +regsub(regexp * prog, char * source, char * dest)
2006 + register char *src;
2007 + register char *dst;
2012 + /* Not necessary and gcc doesn't like it -MLS */
2013 + /*extern char *strncpy();*/
2015 + if (prog == NULL || source == NULL || dest == NULL) {
2016 + regerror("NULL parm to regsub");
2019 + if (UCHARAT(prog->program) != MAGIC) {
2020 + regerror("damaged regexp fed to regsub");
2026 + while ((c = *src++) != '\0') {
2029 + else if (c == '\\' && '0' <= *src && *src <= '9')
2030 + no = *src++ - '0';
2034 + if (no < 0) { /* Ordinary character. */
2035 + if (c == '\\' && (*src == '\\' || *src == '&'))
2038 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2039 + len = prog->endp[no] - prog->startp[no];
2040 + (void) strncpy(dst, prog->startp[no], len);
2042 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2043 + regerror("damaged match string");
2050 --- linux-2.6.28-stock/net/netfilter/nf_conntrack_core.c 2009-01-07 16:05:35.000000000 -0600
2051 +++ linux-2.6.28/net/netfilter/nf_conntrack_core.c 2009-01-07 16:07:31.000000000 -0600
2052 @@ -201,6 +201,14 @@ destroy_conntrack(struct nf_conntrack *n
2054 nf_ct_remove_expectations(ct);
2056 + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2057 + if(ct->layer7.app_proto)
2058 + kfree(ct->layer7.app_proto);
2059 + if(ct->layer7.app_data)
2060 + kfree(ct->layer7.app_data);
2064 /* We overload first tuple to link into unconfirmed or dying list.*/
2065 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
2066 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
2067 --- linux-2.6.28-stock/net/netfilter/nf_conntrack_standalone.c 2009-01-07 16:05:35.000000000 -0600
2068 +++ linux-2.6.28/net/netfilter/nf_conntrack_standalone.c 2009-01-07 16:07:31.000000000 -0600
2069 @@ -165,6 +165,12 @@ static int ct_seq_show(struct seq_file *
2071 ct_show_delta_time(s, ct);
2073 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2074 + if(ct->layer7.app_proto &&
2075 + seq_printf(s, "l7proto=%s ", ct->layer7.app_proto))
2079 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
2081 if (seq_has_overflowed(s))
2082 --- linux-2.6.28-stock/include/net/netfilter/nf_conntrack.h 2009-01-07 16:05:30.000000000 -0600
2083 +++ linux-2.6.28/include/net/netfilter/nf_conntrack.h 2009-01-07 16:07:31.000000000 -0600
2084 @@ -120,6 +120,22 @@ struct nf_conn {
2086 struct nf_ct_ext *ext;
2088 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
2089 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2092 + * e.g. "http". NULL before decision. "unknown" after decision
2097 + * application layer data so far. NULL after match decision.
2100 + unsigned int app_data_len;
2104 /* Storage reserved for other modules, must be the last member */
2105 union nf_conntrack_proto proto;
2107 --- linux-2.6.28-stock/include/linux/netfilter/xt_layer7.h 1969-12-31 18:00:00.000000000 -0600
2108 +++ linux-2.6.28/include/linux/netfilter/xt_layer7.h 2009-01-07 16:07:31.000000000 -0600
2110 +#ifndef _XT_LAYER7_H
2111 +#define _XT_LAYER7_H
2113 +#define MAX_PATTERN_LEN 8192
2114 +#define MAX_PROTOCOL_LEN 256
2116 +struct xt_layer7_info {
2117 + char protocol[MAX_PROTOCOL_LEN];
2118 + char pattern[MAX_PATTERN_LEN];
2122 +#endif /* _XT_LAYER7_H */