1 Index: linux-2.6.24.7/net/netfilter/Kconfig
2 ===================================================================
3 --- linux-2.6.24.7.orig/net/netfilter/Kconfig
4 +++ linux-2.6.24.7/net/netfilter/Kconfig
5 @@ -633,6 +633,27 @@ config NETFILTER_XT_MATCH_STATE
7 To compile it as a module, choose M here. If unsure, say N.
9 +config NETFILTER_XT_MATCH_LAYER7
10 + tristate '"layer7" match support'
11 + depends on NETFILTER_XTABLES
12 + depends on EXPERIMENTAL && (IP_NF_CONNTRACK || NF_CONNTRACK)
13 + depends on NF_CT_ACCT
15 + Say Y if you want to be able to classify connections (and their
16 + packets) based on regular expression matching of their application
17 + layer data. This is one way to classify applications such as
18 + peer-to-peer filesharing systems that do not always use the same
21 + To compile it as a module, choose M here. If unsure, say N.
23 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
24 + bool 'Layer 7 debugging output'
25 + depends on NETFILTER_XT_MATCH_LAYER7
27 + Say Y to get lots of debugging output.
30 config NETFILTER_XT_MATCH_STATISTIC
31 tristate '"statistic" match support'
32 depends on NETFILTER_XTABLES
33 Index: linux-2.6.24.7/net/netfilter/Makefile
34 ===================================================================
35 --- linux-2.6.24.7.orig/net/netfilter/Makefile
36 +++ linux-2.6.24.7/net/netfilter/Makefile
37 @@ -73,6 +73,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_QUOTA) +
38 obj-$(CONFIG_NETFILTER_XT_MATCH_REALM) += xt_realm.o
39 obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o
40 obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o
41 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
42 obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o
43 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
44 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
45 Index: linux-2.6.24.7/net/netfilter/xt_layer7.c
46 ===================================================================
48 +++ linux-2.6.24.7/net/netfilter/xt_layer7.c
51 + Kernel module to match application layer (OSI layer 7) data in connections.
53 + http://l7-filter.sf.net
55 + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer.
57 + This program is free software; you can redistribute it and/or
58 + modify it under the terms of the GNU General Public License
59 + as published by the Free Software Foundation; either version
60 + 2 of the License, or (at your option) any later version.
61 + http://www.gnu.org/licenses/gpl.txt
63 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>,
64 + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait,
65 + Ethan Sommer, Justin Levandoski.
68 +#include <linux/spinlock.h>
69 +#include <linux/version.h>
72 +#include <linux/module.h>
73 +#include <linux/skbuff.h>
74 +#include <linux/netfilter.h>
75 +#include <net/netfilter/nf_conntrack.h>
76 +#include <net/netfilter/nf_conntrack_core.h>
77 +#include <linux/netfilter/x_tables.h>
78 +#include <linux/netfilter/xt_layer7.h>
79 +#include <linux/ctype.h>
80 +#include <linux/proc_fs.h>
82 +#include "regexp/regexp.c"
84 +MODULE_LICENSE("GPL");
85 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
86 +MODULE_DESCRIPTION("iptables application layer match module");
87 +MODULE_ALIAS("ipt_layer7");
88 +MODULE_VERSION("2.17");
90 +static int maxdatalen = 2048; // this is the default
91 +module_param(maxdatalen, int, 0444);
92 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
93 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
94 + #define DPRINTK(format,args...) printk(format,##args)
96 + #define DPRINTK(format,args...)
99 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
100 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
102 +/* Number of packets whose data we look at.
103 +This can be modified through /proc/net/layer7_numpackets */
104 +static int num_packets = 10;
106 +static struct pattern_cache {
107 + char * regex_string;
109 + struct pattern_cache * next;
110 +} * first_pattern_cache = NULL;
112 +DEFINE_SPINLOCK(l7_lock);
114 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
115 +/* Converts an unfriendly string into a friendly one by
116 +replacing unprintables with periods and all whitespace with " ". */
117 +static char * friendly_print(unsigned char * s)
119 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
123 + if (net_ratelimit())
124 + printk(KERN_ERR "layer7: out of memory in "
125 + "friendly_print, bailing.\n");
129 + for(i = 0; i < strlen(s); i++){
130 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
131 + else if(isspace(s[i])) f[i] = ' ';
138 +static char dec2hex(int i)
145 + return (i - 10 + 'a');
148 + if (net_ratelimit())
149 + printk("layer7: Problem in dec2hex\n");
154 +static char * hex_print(unsigned char * s)
156 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
160 + if (net_ratelimit())
161 + printk(KERN_ERR "layer7: out of memory in hex_print, "
166 + for(i = 0; i < strlen(s); i++) {
167 + g[i*3 ] = dec2hex(s[i]/16);
168 + g[i*3 + 1] = dec2hex(s[i]%16);
177 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
178 +over again, it make sense to cache the results. */
179 +static regexp * compile_and_cache(const char * regex_string,
180 + const char * protocol)
182 + struct pattern_cache * node = first_pattern_cache;
183 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
184 + struct pattern_cache * tmp;
187 + while (node != NULL) {
188 + if (!strcmp(node->regex_string, regex_string))
189 + return node->pattern;
191 + last_pattern_cache = node;/* points at the last non-NULL node */
195 + /* If we reach the end of the list, then we have not yet cached
196 + the pattern for this regex. Let's do that now.
197 + Be paranoid about running out of memory to avoid list corruption. */
198 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
201 + if (net_ratelimit())
202 + printk(KERN_ERR "layer7: out of memory in "
203 + "compile_and_cache, bailing.\n");
207 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
208 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
211 + if(!tmp->regex_string || !tmp->pattern) {
212 + if (net_ratelimit())
213 + printk(KERN_ERR "layer7: out of memory in "
214 + "compile_and_cache, bailing.\n");
215 + kfree(tmp->regex_string);
216 + kfree(tmp->pattern);
221 + /* Ok. The new node is all ready now. */
224 + if(first_pattern_cache == NULL) /* list is empty */
225 + first_pattern_cache = node; /* make node the beginning */
227 + last_pattern_cache->next = node; /* attach node to the end */
229 + /* copy the string and compile the regex */
230 + len = strlen(regex_string);
231 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
232 + node->pattern = regcomp((char *)regex_string, &len);
233 + if ( !node->pattern ) {
234 + if (net_ratelimit())
235 + printk(KERN_ERR "layer7: Error compiling regexp "
237 + regex_string, protocol);
238 + /* pattern is now cached as NULL, so we won't try again. */
241 + strcpy(node->regex_string, regex_string);
242 + return node->pattern;
245 +static int can_handle(const struct sk_buff *skb)
247 + if(!ip_hdr(skb)) /* not IP */
249 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
250 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
251 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
256 +/* Returns offset the into the skb->data that the application data starts */
257 +static int app_data_offset(const struct sk_buff *skb)
259 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
260 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
261 + int ip_hl = 4*ip_hdr(skb)->ihl;
263 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
264 + /* 12 == offset into TCP header for the header length field.
265 + Can't get this with skb->h.th->doff because the tcphdr
266 + struct doesn't get set when routing (this is confirmed to be
267 + true in Netfilter as well as QoS.) */
268 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
270 + return ip_hl + tcp_hl;
271 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
272 + return ip_hl + 8; /* UDP header is always 8 bytes */
273 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
274 + return ip_hl + 8; /* ICMP header is 8 bytes */
276 + if (net_ratelimit())
277 + printk(KERN_ERR "layer7: tried to handle unknown "
279 + return ip_hl + 8; /* something reasonable */
283 +/* handles whether there's a match when we aren't appending data anymore */
284 +static int match_no_append(struct nf_conn * conntrack,
285 + struct nf_conn * master_conntrack,
286 + enum ip_conntrack_info ctinfo,
287 + enum ip_conntrack_info master_ctinfo,
288 + const struct xt_layer7_info * info)
290 + /* If we're in here, throw the app data away */
291 + if(master_conntrack->layer7.app_data != NULL) {
293 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
294 + if(!master_conntrack->layer7.app_proto) {
296 + friendly_print(master_conntrack->layer7.app_data);
298 + hex_print(master_conntrack->layer7.app_data);
299 + DPRINTK("\nl7-filter gave up after %d bytes "
300 + "(%d packets):\n%s\n",
301 + strlen(f), TOTAL_PACKETS, f);
303 + DPRINTK("In hex: %s\n", g);
308 + kfree(master_conntrack->layer7.app_data);
309 + master_conntrack->layer7.app_data = NULL; /* don't free again */
312 + if(master_conntrack->layer7.app_proto){
313 + /* Here child connections set their .app_proto (for /proc) */
314 + if(!conntrack->layer7.app_proto) {
315 + conntrack->layer7.app_proto =
316 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
318 + if(!conntrack->layer7.app_proto){
319 + if (net_ratelimit())
320 + printk(KERN_ERR "layer7: out of memory "
321 + "in match_no_append, "
325 + strcpy(conntrack->layer7.app_proto,
326 + master_conntrack->layer7.app_proto);
329 + return (!strcmp(master_conntrack->layer7.app_proto,
333 + /* If not classified, set to "unknown" to distinguish from
334 + connections that are still being tested. */
335 + master_conntrack->layer7.app_proto =
336 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
337 + if(!master_conntrack->layer7.app_proto){
338 + if (net_ratelimit())
339 + printk(KERN_ERR "layer7: out of memory in "
340 + "match_no_append, bailing.\n");
343 + strcpy(master_conntrack->layer7.app_proto, "unknown");
348 +/* add the new app data to the conntrack. Return number of bytes added. */
349 +static int add_data(struct nf_conn * master_conntrack,
350 + char * app_data, int appdatalen)
353 + int oldlength = master_conntrack->layer7.app_data_len;
355 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
356 + clear on whether the race condition exists or whether this really
357 + fixes it. I might just be being dense... Anyway, if it's not really
358 + a fix, all it does is waste a very small amount of time. */
359 + if(!master_conntrack->layer7.app_data) return 0;
361 + /* Strip nulls. Make everything lower case (our regex lib doesn't
362 + do case insensitivity). Add it to the end of the current data. */
363 + for(i = 0; i < maxdatalen-oldlength-1 &&
364 + i < appdatalen; i++) {
365 + if(app_data[i] != '\0') {
366 + /* the kernel version of tolower mungs 'upper ascii' */
367 + master_conntrack->layer7.app_data[length+oldlength] =
368 + isascii(app_data[i])?
369 + tolower(app_data[i]) : app_data[i];
374 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
375 + master_conntrack->layer7.app_data_len = length + oldlength;
380 +/* taken from drivers/video/modedb.c */
381 +static int my_atoi(const char *s)
388 + val = 10*val+(*s-'0');
396 +/* write out num_packets to userland. */
397 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
398 + int* eof, void * data)
400 + if(num_packets > 99 && net_ratelimit())
401 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
403 + page[0] = num_packets/10 + '0';
404 + page[1] = num_packets%10 + '0';
413 +/* Read in num_packets from userland */
414 +static int layer7_write_proc(struct file* file, const char* buffer,
415 + unsigned long count, void *data)
417 + char * foo = kmalloc(count, GFP_ATOMIC);
420 + if (net_ratelimit())
421 + printk(KERN_ERR "layer7: out of memory, bailing. "
422 + "num_packets unchanged.\n");
426 + if(copy_from_user(foo, buffer, count)) {
431 + num_packets = my_atoi(foo);
434 + /* This has an arbitrary limit to make the math easier. I'm lazy.
435 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
436 + if(num_packets > 99) {
437 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
439 + } else if(num_packets < 1) {
440 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
448 +match(const struct sk_buff *skbin,
449 + const struct net_device *in,
450 + const struct net_device *out,
451 + const struct xt_match *match,
452 + const void *matchinfo,
454 + unsigned int protoff,
457 + /* sidestep const without getting a compiler warning... */
458 + struct sk_buff * skb = (struct sk_buff *)skbin;
460 + const struct xt_layer7_info * info = matchinfo;
461 + enum ip_conntrack_info master_ctinfo, ctinfo;
462 + struct nf_conn *master_conntrack, *conntrack;
463 + unsigned char * app_data;
464 + unsigned int pattern_result, appdatalen;
465 + regexp * comppattern;
467 + /* Be paranoid/incompetent - lock the entire match function. */
468 + spin_lock_bh(&l7_lock);
470 + if(!can_handle(skb)){
471 + DPRINTK("layer7: This is some protocol I can't handle.\n");
472 + spin_unlock_bh(&l7_lock);
473 + return info->invert;
476 + /* Treat parent & all its children together as one connection, except
477 + for the purpose of setting conntrack->layer7.app_proto in the actual
478 + connection. This makes /proc/net/ip_conntrack more satisfying. */
479 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
480 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
481 + DPRINTK("layer7: couldn't get conntrack.\n");
482 + spin_unlock_bh(&l7_lock);
483 + return info->invert;
486 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
487 + while (master_ct(master_conntrack) != NULL)
488 + master_conntrack = master_ct(master_conntrack);
490 + /* if we've classified it or seen too many packets */
491 + if(TOTAL_PACKETS > num_packets ||
492 + master_conntrack->layer7.app_proto) {
494 + pattern_result = match_no_append(conntrack, master_conntrack,
495 + ctinfo, master_ctinfo, info);
497 + /* skb->cb[0] == seen. Don't do things twice if there are
498 + multiple l7 rules. I'm not sure that using cb for this purpose
499 + is correct, even though it says "put your private variables
500 + there". But it doesn't look like it is being used for anything
501 + else in the skbs that make it here. */
502 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
504 + spin_unlock_bh(&l7_lock);
505 + return (pattern_result ^ info->invert);
508 + if(skb_is_nonlinear(skb)){
509 + if(skb_linearize(skb) != 0){
510 + if (net_ratelimit())
511 + printk(KERN_ERR "layer7: failed to linearize "
512 + "packet, bailing.\n");
513 + spin_unlock_bh(&l7_lock);
514 + return info->invert;
518 + /* now that the skb is linearized, it's safe to set these. */
519 + app_data = skb->data + app_data_offset(skb);
520 + appdatalen = skb_tail_pointer(skb) - app_data;
522 + /* the return value gets checked later, when we're ready to use it */
523 + comppattern = compile_and_cache(info->pattern, info->protocol);
525 + /* On the first packet of a connection, allocate space for app data */
526 + if(TOTAL_PACKETS == 1 && !skb->cb[0] &&
527 + !master_conntrack->layer7.app_data){
528 + master_conntrack->layer7.app_data =
529 + kmalloc(maxdatalen, GFP_ATOMIC);
530 + if(!master_conntrack->layer7.app_data){
531 + if (net_ratelimit())
532 + printk(KERN_ERR "layer7: out of memory in "
533 + "match, bailing.\n");
534 + spin_unlock_bh(&l7_lock);
535 + return info->invert;
538 + master_conntrack->layer7.app_data[0] = '\0';
541 + /* Can be here, but unallocated, if numpackets is increased near
542 + the beginning of a connection */
543 + if(master_conntrack->layer7.app_data == NULL){
544 + spin_unlock_bh(&l7_lock);
545 + return (info->invert); /* unmatched */
550 + newbytes = add_data(master_conntrack, app_data, appdatalen);
552 + if(newbytes == 0) { /* didn't add any data */
554 + /* Didn't match before, not going to match now */
555 + spin_unlock_bh(&l7_lock);
556 + return info->invert;
560 + /* If looking for "unknown", then never match. "Unknown" means that
561 + we've given up; we're still trying with these packets. */
562 + if(!strcmp(info->protocol, "unknown")) {
563 + pattern_result = 0;
564 + /* If looking for "unset", then always match. "Unset" means that we
565 + haven't yet classified the connection. */
566 + } else if(!strcmp(info->protocol, "unset")) {
567 + pattern_result = 2;
568 + DPRINTK("layer7: matched unset: not yet classified "
569 + "(%d/%d packets)\n", TOTAL_PACKETS, num_packets);
570 + /* If the regexp failed to compile, don't bother running it */
571 + } else if(comppattern &&
572 + regexec(comppattern, master_conntrack->layer7.app_data)){
573 + DPRINTK("layer7: matched %s\n", info->protocol);
574 + pattern_result = 1;
575 + } else pattern_result = 0;
577 + if(pattern_result == 1) {
578 + master_conntrack->layer7.app_proto =
579 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
580 + if(!master_conntrack->layer7.app_proto){
581 + if (net_ratelimit())
582 + printk(KERN_ERR "layer7: out of memory in "
583 + "match, bailing.\n");
584 + spin_unlock_bh(&l7_lock);
585 + return (pattern_result ^ info->invert);
587 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
588 + } else if(pattern_result > 1) { /* cleanup from "unset" */
589 + pattern_result = 1;
592 + /* mark the packet seen */
595 + spin_unlock_bh(&l7_lock);
596 + return (pattern_result ^ info->invert);
599 +static bool check(const char *tablename,
601 + const struct xt_match *match,
603 + unsigned int hook_mask)
606 + // load nf_conntrack_ipv4
607 + if (nf_ct_l3proto_try_module_get(match->family) < 0) {
608 + printk(KERN_WARNING "can't load conntrack support for "
609 + "proto=%d\n", match->family);
616 +destroy(const struct xt_match *match, void *matchinfo)
618 + nf_ct_l3proto_module_put(match->family);
621 +static struct xt_match xt_layer7_match[] = {
625 + .checkentry = check,
627 + .destroy = destroy,
628 + .matchsize = sizeof(struct xt_layer7_info),
633 +static void layer7_cleanup_proc(void)
635 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
636 + remove_proc_entry("layer7_numpackets", proc_net);
638 + remove_proc_entry("layer7_numpackets", init_net.proc_net);
642 +/* register the proc file */
643 +static void layer7_init_proc(void)
645 + struct proc_dir_entry* entry;
646 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
647 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
649 + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
651 + entry->read_proc = layer7_read_proc;
652 + entry->write_proc = layer7_write_proc;
655 +static int __init xt_layer7_init(void)
659 + layer7_init_proc();
660 + if(maxdatalen < 1) {
661 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
665 + /* This is not a hard limit. It's just here to prevent people from
666 + bringing their slow machines to a grinding halt. */
667 + else if(maxdatalen > 65536) {
668 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
670 + maxdatalen = 65536;
672 + return xt_register_matches(xt_layer7_match,
673 + ARRAY_SIZE(xt_layer7_match));
676 +static void __exit xt_layer7_fini(void)
678 + layer7_cleanup_proc();
679 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
682 +module_init(xt_layer7_init);
683 +module_exit(xt_layer7_fini);
684 Index: linux-2.6.24.7/net/netfilter/regexp/regexp.c
685 ===================================================================
687 +++ linux-2.6.24.7/net/netfilter/regexp/regexp.c
690 + * regcomp and regexec -- regsub and regerror are elsewhere
691 + * @(#)regexp.c 1.3 of 18 April 87
693 + * Copyright (c) 1986 by University of Toronto.
694 + * Written by Henry Spencer. Not derived from licensed software.
696 + * Permission is granted to anyone to use this software for any
697 + * purpose on any computer system, and to redistribute it freely,
698 + * subject to the following restrictions:
700 + * 1. The author is not responsible for the consequences of use of
701 + * this software, no matter how awful, even if they arise
702 + * from defects in it.
704 + * 2. The origin of this software must not be misrepresented, either
705 + * by explicit claim or by omission.
707 + * 3. Altered versions must be plainly marked as such, and must not
708 + * be misrepresented as being the original software.
710 + * Beware that some of this code is subtly aware of the way operator
711 + * precedence is structured in regular expressions. Serious changes in
712 + * regular-expression syntax might require a total rethink.
714 + * This code was modified by Ethan Sommer to work within the kernel
715 + * (it now uses kmalloc etc..)
717 + * Modified slightly by Matthew Strait to use more modern C.
721 +#include "regmagic.h"
723 +/* added by ethan and matt. Lets it work in both kernel and user space.
724 +(So iptables can use it, for instance.) Yea, it goes both ways... */
726 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
728 + #define printk(format,args...) printf(format,##args)
731 +void regerror(char * s)
733 + printk("<3>Regexp: %s\n", s);
738 + * The "internal use only" fields in regexp.h are present to pass info from
739 + * compile to execute that permits the execute phase to run lots faster on
740 + * simple cases. They are:
742 + * regstart char that must begin a match; '\0' if none obvious
743 + * reganch is the match anchored (at beginning-of-line only)?
744 + * regmust string (pointer into program) that match must include, or NULL
745 + * regmlen length of regmust string
747 + * Regstart and reganch permit very fast decisions on suitable starting points
748 + * for a match, cutting down the work a lot. Regmust permits fast rejection
749 + * of lines that cannot possibly match. The regmust tests are costly enough
750 + * that regcomp() supplies a regmust only if the r.e. contains something
751 + * potentially expensive (at present, the only such thing detected is * or +
752 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
753 + * supplied because the test in regexec() needs it and regcomp() is computing
758 + * Structure for regexp "program". This is essentially a linear encoding
759 + * of a nondeterministic finite-state machine (aka syntax charts or
760 + * "railroad normal form" in parsing technology). Each node is an opcode
761 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
762 + * all nodes except BRANCH implement concatenation; a "next" pointer with
763 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
764 + * have one of the subtle syntax dependencies: an individual BRANCH (as
765 + * opposed to a collection of them) is never concatenated with anything
766 + * because of operator precedence.) The operand of some types of node is
767 + * a literal string; for others, it is a node leading into a sub-FSM. In
768 + * particular, the operand of a BRANCH node is the first node of the branch.
769 + * (NB this is *not* a tree structure: the tail of the branch connects
770 + * to the thing following the set of BRANCHes.) The opcodes are:
773 +/* definition number opnd? meaning */
774 +#define END 0 /* no End of program. */
775 +#define BOL 1 /* no Match "" at beginning of line. */
776 +#define EOL 2 /* no Match "" at end of line. */
777 +#define ANY 3 /* no Match any one character. */
778 +#define ANYOF 4 /* str Match any character in this string. */
779 +#define ANYBUT 5 /* str Match any character not in this string. */
780 +#define BRANCH 6 /* node Match this alternative, or the next... */
781 +#define BACK 7 /* no Match "", "next" ptr points backward. */
782 +#define EXACTLY 8 /* str Match this string. */
783 +#define NOTHING 9 /* no Match empty string. */
784 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
785 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
786 +#define OPEN 20 /* no Mark this point in input as start of #n. */
787 + /* OPEN+1 is number 1, etc. */
788 +#define CLOSE 30 /* no Analogous to OPEN. */
793 + * BRANCH The set of branches constituting a single choice are hooked
794 + * together with their "next" pointers, since precedence prevents
795 + * anything being concatenated to any individual branch. The
796 + * "next" pointer of the last BRANCH in a choice points to the
797 + * thing following the whole choice. This is also where the
798 + * final "next" pointer of each individual branch points; each
799 + * branch starts with the operand node of a BRANCH node.
801 + * BACK Normal "next" pointers all implicitly point forward; BACK
802 + * exists to make loop structures possible.
804 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
805 + * BRANCH structures using BACK. Simple cases (one character
806 + * per match) are implemented with STAR and PLUS for speed
807 + * and to minimize recursive plunges.
809 + * OPEN,CLOSE ...are numbered at compile time.
813 + * A node is one char of opcode followed by two chars of "next" pointer.
814 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
815 + * value is a positive offset from the opcode of the node containing it.
816 + * An operand, if any, simply follows the node. (Note that much of the
817 + * code generation knows about this implicit relationship.)
819 + * Using two bytes for the "next" pointer is vast overkill for most things,
820 + * but allows patterns to get big without disasters.
822 +#define OP(p) (*(p))
823 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
824 +#define OPERAND(p) ((p) + 3)
827 + * See regmagic.h for one further detail of program structure.
832 + * Utility definitions.
835 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
837 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
840 +#define FAIL(m) { regerror(m); return(NULL); }
841 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
842 +#define META "^$.[()|?+*\\"
845 + * Flags to be passed up and down.
847 +#define HASWIDTH 01 /* Known never to match null string. */
848 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
849 +#define SPSTART 04 /* Starts with * or +. */
850 +#define WORST 0 /* Worst case. */
853 + * Global work variables for regcomp().
855 +struct match_globals {
856 +char *reginput; /* String-input pointer. */
857 +char *regbol; /* Beginning of input, for ^ check. */
858 +char **regstartp; /* Pointer to startp array. */
859 +char **regendp; /* Ditto for endp. */
860 +char *regparse; /* Input-scan pointer. */
861 +int regnpar; /* () count. */
863 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
864 +long regsize; /* Code size. */
868 + * Forward declarations for regcomp()'s friends.
871 +#define STATIC static
873 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
874 +STATIC char *regbranch(struct match_globals *g, int *flagp);
875 +STATIC char *regpiece(struct match_globals *g, int *flagp);
876 +STATIC char *regatom(struct match_globals *g, int *flagp);
877 +STATIC char *regnode(struct match_globals *g, char op);
878 +STATIC char *regnext(struct match_globals *g, char *p);
879 +STATIC void regc(struct match_globals *g, char b);
880 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
881 +STATIC void regtail(struct match_globals *g, char *p, char *val);
882 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
885 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
892 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
893 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
894 + if (*scan1 == *scan2++)
902 + - regcomp - compile a regular expression into internal code
904 + * We can't allocate space until we know how big the compiled form will be,
905 + * but we can't compile it (and thus know how big it is) until we've got a
906 + * place to put the code. So we cheat: we compile it twice, once with code
907 + * generation turned off and size counting turned on, and once "for real".
908 + * This also means that we don't allocate space until we are sure that the
909 + * thing really will compile successfully, and we never have to move the
910 + * code and thus invalidate pointers into it. (Note that it has to be in
911 + * one piece because free() must be able to free it all.)
913 + * Beware that the optimization-preparation code in here knows about some
914 + * of the structure of the compiled regexp.
917 +regcomp(char *exp,int *patternsize)
919 + register regexp *r;
920 + register char *scan;
921 + register char *longest;
924 + struct match_globals g;
926 + /* commented out by ethan
927 + extern char *malloc();
931 + FAIL("NULL argument");
933 + /* First pass: determine size, legality. */
937 + g.regcode = &g.regdummy;
939 + if (reg(&g, 0, &flags) == NULL)
942 + /* Small enough for pointer-storage convention? */
943 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
944 + FAIL("regexp too big");
946 + /* Allocate space. */
947 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
948 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
950 + FAIL("out of space");
952 + /* Second pass: emit code. */
955 + g.regcode = r->program;
957 + if (reg(&g, 0, &flags) == NULL)
960 + /* Dig out information for optimizations. */
961 + r->regstart = '\0'; /* Worst-case defaults. */
965 + scan = r->program+1; /* First BRANCH. */
966 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
967 + scan = OPERAND(scan);
969 + /* Starting-point info. */
970 + if (OP(scan) == EXACTLY)
971 + r->regstart = *OPERAND(scan);
972 + else if (OP(scan) == BOL)
976 + * If there's something expensive in the r.e., find the
977 + * longest literal string that must appear and make it the
978 + * regmust. Resolve ties in favor of later strings, since
979 + * the regstart check works with the beginning of the r.e.
980 + * and avoiding duplication strengthens checking. Not a
981 + * strong reason, but sufficient in the absence of others.
983 + if (flags&SPSTART) {
986 + for (; scan != NULL; scan = regnext(&g, scan))
987 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
988 + longest = OPERAND(scan);
989 + len = strlen(OPERAND(scan));
991 + r->regmust = longest;
1000 + - reg - regular expression, i.e. main body or parenthesized thing
1002 + * Caller must absorb opening parenthesis.
1004 + * Combining parenthesis handling with the base level of regular expression
1005 + * is a trifle forced, but the need to tie the tails of the branches to what
1006 + * follows makes it hard to avoid.
1009 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1011 + register char *ret;
1012 + register char *br;
1013 + register char *ender;
1014 + register int parno = 0; /* 0 makes gcc happy */
1017 + *flagp = HASWIDTH; /* Tentatively. */
1019 + /* Make an OPEN node, if parenthesized. */
1021 + if (g->regnpar >= NSUBEXP)
1022 + FAIL("too many ()");
1023 + parno = g->regnpar;
1025 + ret = regnode(g, OPEN+parno);
1029 + /* Pick up the branches, linking them together. */
1030 + br = regbranch(g, &flags);
1034 + regtail(g, ret, br); /* OPEN -> first. */
1037 + if (!(flags&HASWIDTH))
1038 + *flagp &= ~HASWIDTH;
1039 + *flagp |= flags&SPSTART;
1040 + while (*g->regparse == '|') {
1042 + br = regbranch(g, &flags);
1045 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1046 + if (!(flags&HASWIDTH))
1047 + *flagp &= ~HASWIDTH;
1048 + *flagp |= flags&SPSTART;
1051 + /* Make a closing node, and hook it on the end. */
1052 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1053 + regtail(g, ret, ender);
1055 + /* Hook the tails of the branches to the closing node. */
1056 + for (br = ret; br != NULL; br = regnext(g, br))
1057 + regoptail(g, br, ender);
1059 + /* Check for proper termination. */
1060 + if (paren && *g->regparse++ != ')') {
1061 + FAIL("unmatched ()");
1062 + } else if (!paren && *g->regparse != '\0') {
1063 + if (*g->regparse == ')') {
1064 + FAIL("unmatched ()");
1066 + FAIL("junk on end"); /* "Can't happen". */
1074 + - regbranch - one alternative of an | operator
1076 + * Implements the concatenation operator.
1079 +regbranch(struct match_globals *g, int *flagp)
1081 + register char *ret;
1082 + register char *chain;
1083 + register char *latest;
1086 + *flagp = WORST; /* Tentatively. */
1088 + ret = regnode(g, BRANCH);
1090 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1091 + latest = regpiece(g, &flags);
1092 + if (latest == NULL)
1094 + *flagp |= flags&HASWIDTH;
1095 + if (chain == NULL) /* First piece. */
1096 + *flagp |= flags&SPSTART;
1098 + regtail(g, chain, latest);
1101 + if (chain == NULL) /* Loop ran zero times. */
1102 + (void) regnode(g, NOTHING);
1108 + - regpiece - something followed by possible [*+?]
1110 + * Note that the branching code sequences used for ? and the general cases
1111 + * of * and + are somewhat optimized: they use the same NOTHING node as
1112 + * both the endmarker for their branch list and the body of the last branch.
1113 + * It might seem that this node could be dispensed with entirely, but the
1114 + * endmarker role is not redundant.
1117 +regpiece(struct match_globals *g, int *flagp)
1119 + register char *ret;
1121 + register char *next;
1124 + ret = regatom(g, &flags);
1128 + op = *g->regparse;
1129 + if (!ISMULT(op)) {
1134 + if (!(flags&HASWIDTH) && op != '?')
1135 + FAIL("*+ operand could be empty");
1136 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1138 + if (op == '*' && (flags&SIMPLE))
1139 + reginsert(g, STAR, ret);
1140 + else if (op == '*') {
1141 + /* Emit x* as (x&|), where & means "self". */
1142 + reginsert(g, BRANCH, ret); /* Either x */
1143 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1144 + regoptail(g, ret, ret); /* back */
1145 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1146 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1147 + } else if (op == '+' && (flags&SIMPLE))
1148 + reginsert(g, PLUS, ret);
1149 + else if (op == '+') {
1150 + /* Emit x+ as x(&|), where & means "self". */
1151 + next = regnode(g, BRANCH); /* Either */
1152 + regtail(g, ret, next);
1153 + regtail(g, regnode(g, BACK), ret); /* loop back */
1154 + regtail(g, next, regnode(g, BRANCH)); /* or */
1155 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1156 + } else if (op == '?') {
1157 + /* Emit x? as (x|) */
1158 + reginsert(g, BRANCH, ret); /* Either x */
1159 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1160 + next = regnode(g, NOTHING); /* null. */
1161 + regtail(g, ret, next);
1162 + regoptail(g, ret, next);
1165 + if (ISMULT(*g->regparse))
1166 + FAIL("nested *?+");
1172 + - regatom - the lowest level
1174 + * Optimization: gobbles an entire sequence of ordinary characters so that
1175 + * it can turn them into a single node, which is smaller to store and
1176 + * faster to run. Backslashed characters are exceptions, each becoming a
1177 + * separate node; the code is simpler that way and it's not worth fixing.
1180 +regatom(struct match_globals *g, int *flagp)
1182 + register char *ret;
1185 + *flagp = WORST; /* Tentatively. */
1187 + switch (*g->regparse++) {
1189 + ret = regnode(g, BOL);
1192 + ret = regnode(g, EOL);
1195 + ret = regnode(g, ANY);
1196 + *flagp |= HASWIDTH|SIMPLE;
1199 + register int class;
1200 + register int classend;
1202 + if (*g->regparse == '^') { /* Complement of range. */
1203 + ret = regnode(g, ANYBUT);
1206 + ret = regnode(g, ANYOF);
1207 + if (*g->regparse == ']' || *g->regparse == '-')
1208 + regc(g, *g->regparse++);
1209 + while (*g->regparse != '\0' && *g->regparse != ']') {
1210 + if (*g->regparse == '-') {
1212 + if (*g->regparse == ']' || *g->regparse == '\0')
1215 + class = UCHARAT(g->regparse-2)+1;
1216 + classend = UCHARAT(g->regparse);
1217 + if (class > classend+1)
1218 + FAIL("invalid [] range");
1219 + for (; class <= classend; class++)
1224 + regc(g, *g->regparse++);
1227 + if (*g->regparse != ']')
1228 + FAIL("unmatched []");
1230 + *flagp |= HASWIDTH|SIMPLE;
1234 + ret = reg(g, 1, &flags);
1237 + *flagp |= flags&(HASWIDTH|SPSTART);
1242 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1247 + FAIL("?+* follows nothing");
1250 + if (*g->regparse == '\0')
1251 + FAIL("trailing \\");
1252 + ret = regnode(g, EXACTLY);
1253 + regc(g, *g->regparse++);
1255 + *flagp |= HASWIDTH|SIMPLE;
1259 + register char ender;
1262 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1264 + FAIL("internal disaster");
1265 + ender = *(g->regparse+len);
1266 + if (len > 1 && ISMULT(ender))
1267 + len--; /* Back off clear of ?+* operand. */
1268 + *flagp |= HASWIDTH;
1271 + ret = regnode(g, EXACTLY);
1273 + regc(g, *g->regparse++);
1285 + - regnode - emit a node
1287 +static char * /* Location. */
1288 +regnode(struct match_globals *g, char op)
1290 + register char *ret;
1291 + register char *ptr;
1294 + if (ret == &g->regdummy) {
1301 + *ptr++ = '\0'; /* Null "next" pointer. */
1309 + - regc - emit (if appropriate) a byte of code
1312 +regc(struct match_globals *g, char b)
1314 + if (g->regcode != &g->regdummy)
1315 + *g->regcode++ = b;
1321 + - reginsert - insert an operator in front of already-emitted operand
1323 + * Means relocating the operand.
1326 +reginsert(struct match_globals *g, char op, char* opnd)
1328 + register char *src;
1329 + register char *dst;
1330 + register char *place;
1332 + if (g->regcode == &g->regdummy) {
1340 + while (src > opnd)
1343 + place = opnd; /* Op node, where operand used to be. */
1350 + - regtail - set the next-pointer at the end of a node chain
1353 +regtail(struct match_globals *g, char *p, char *val)
1355 + register char *scan;
1356 + register char *temp;
1357 + register int offset;
1359 + if (p == &g->regdummy)
1362 + /* Find last node. */
1365 + temp = regnext(g, scan);
1371 + if (OP(scan) == BACK)
1372 + offset = scan - val;
1374 + offset = val - scan;
1375 + *(scan+1) = (offset>>8)&0377;
1376 + *(scan+2) = offset&0377;
1380 + - regoptail - regtail on operand of first argument; nop if operandless
1383 +regoptail(struct match_globals *g, char *p, char *val)
1385 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1386 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1388 + regtail(g, OPERAND(p), val);
1392 + * regexec and friends
1399 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1400 +STATIC int regmatch(struct match_globals *g, char *prog);
1401 +STATIC int regrepeat(struct match_globals *g, char *p);
1404 +int regnarrate = 0;
1406 +STATIC char *regprop(char *op);
1410 + - regexec - match a regexp against a string
1413 +regexec(regexp *prog, char *string)
1416 + struct match_globals g;
1418 + /* Be paranoid... */
1419 + if (prog == NULL || string == NULL) {
1420 + printk("<3>Regexp: NULL parameter\n");
1424 + /* Check validity of program. */
1425 + if (UCHARAT(prog->program) != MAGIC) {
1426 + printk("<3>Regexp: corrupted program\n");
1430 + /* If there is a "must appear" string, look for it. */
1431 + if (prog->regmust != NULL) {
1433 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1434 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1435 + break; /* Found it. */
1438 + if (s == NULL) /* Not present. */
1442 + /* Mark beginning of line for ^ . */
1443 + g.regbol = string;
1445 + /* Simplest case: anchored match need be tried only once. */
1446 + if (prog->reganch)
1447 + return(regtry(&g, prog, string));
1449 + /* Messy cases: unanchored match. */
1451 + if (prog->regstart != '\0')
1452 + /* We know what char it must start with. */
1453 + while ((s = strchr(s, prog->regstart)) != NULL) {
1454 + if (regtry(&g, prog, s))
1459 + /* We don't -- general case. */
1461 + if (regtry(&g, prog, s))
1463 + } while (*s++ != '\0');
1470 + - regtry - try match at specific point
1472 +static int /* 0 failure, 1 success */
1473 +regtry(struct match_globals *g, regexp *prog, char *string)
1476 + register char **sp;
1477 + register char **ep;
1479 + g->reginput = string;
1480 + g->regstartp = prog->startp;
1481 + g->regendp = prog->endp;
1483 + sp = prog->startp;
1485 + for (i = NSUBEXP; i > 0; i--) {
1489 + if (regmatch(g, prog->program + 1)) {
1490 + prog->startp[0] = string;
1491 + prog->endp[0] = g->reginput;
1498 + - regmatch - main matching routine
1500 + * Conceptually the strategy is simple: check to see whether the current
1501 + * node matches, call self recursively to see whether the rest matches,
1502 + * and then act accordingly. In practice we make some effort to avoid
1503 + * recursion, in particular by going through "ordinary" nodes (that don't
1504 + * need to know whether the rest of the match failed) by a loop instead of
1507 +static int /* 0 failure, 1 success */
1508 +regmatch(struct match_globals *g, char *prog)
1510 + register char *scan = prog; /* Current node. */
1511 + char *next; /* Next node. */
1514 + if (scan != NULL && regnarrate)
1515 + fprintf(stderr, "%s(\n", regprop(scan));
1517 + while (scan != NULL) {
1520 + fprintf(stderr, "%s...\n", regprop(scan));
1522 + next = regnext(g, scan);
1524 + switch (OP(scan)) {
1526 + if (g->reginput != g->regbol)
1530 + if (*g->reginput != '\0')
1534 + if (*g->reginput == '\0')
1540 + register char *opnd;
1542 + opnd = OPERAND(scan);
1543 + /* Inline the first character, for speed. */
1544 + if (*opnd != *g->reginput)
1546 + len = strlen(opnd);
1547 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1549 + g->reginput += len;
1553 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1558 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1575 + register char *save;
1577 + no = OP(scan) - OPEN;
1578 + save = g->reginput;
1580 + if (regmatch(g, next)) {
1582 + * Don't set startp if some later
1583 + * invocation of the same parentheses
1586 + if (g->regstartp[no] == NULL)
1587 + g->regstartp[no] = save;
1604 + register char *save;
1606 + no = OP(scan) - CLOSE;
1607 + save = g->reginput;
1609 + if (regmatch(g, next)) {
1611 + * Don't set endp if some later
1612 + * invocation of the same parentheses
1615 + if (g->regendp[no] == NULL)
1616 + g->regendp[no] = save;
1623 + register char *save;
1625 + if (OP(next) != BRANCH) /* No choice. */
1626 + next = OPERAND(scan); /* Avoid recursion. */
1629 + save = g->reginput;
1630 + if (regmatch(g, OPERAND(scan)))
1632 + g->reginput = save;
1633 + scan = regnext(g, scan);
1634 + } while (scan != NULL && OP(scan) == BRANCH);
1642 + register char nextch;
1644 + register char *save;
1648 + * Lookahead to avoid useless match attempts
1649 + * when we know what character comes next.
1652 + if (OP(next) == EXACTLY)
1653 + nextch = *OPERAND(next);
1654 + min = (OP(scan) == STAR) ? 0 : 1;
1655 + save = g->reginput;
1656 + no = regrepeat(g, OPERAND(scan));
1657 + while (no >= min) {
1658 + /* If it could work, try it. */
1659 + if (nextch == '\0' || *g->reginput == nextch)
1660 + if (regmatch(g, next))
1662 + /* Couldn't or didn't -- back up. */
1664 + g->reginput = save + no;
1670 + return(1); /* Success! */
1673 + printk("<3>Regexp: memory corruption\n");
1682 + * We get here only if there's trouble -- normally "case END" is
1683 + * the terminating point.
1685 + printk("<3>Regexp: corrupted pointers\n");
1690 + - regrepeat - repeatedly match something simple, report how many
1693 +regrepeat(struct match_globals *g, char *p)
1695 + register int count = 0;
1696 + register char *scan;
1697 + register char *opnd;
1699 + scan = g->reginput;
1700 + opnd = OPERAND(p);
1703 + count = strlen(scan);
1707 + while (*opnd == *scan) {
1713 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1719 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1724 + default: /* Oh dear. Called inappropriately. */
1725 + printk("<3>Regexp: internal foulup\n");
1726 + count = 0; /* Best compromise. */
1729 + g->reginput = scan;
1735 + - regnext - dig the "next" pointer out of a node
1738 +regnext(struct match_globals *g, char *p)
1740 + register int offset;
1742 + if (p == &g->regdummy)
1749 + if (OP(p) == BACK)
1757 +STATIC char *regprop();
1760 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1766 + register char op = EXACTLY; /* Arbitrary non-END op. */
1767 + register char *next;
1768 + /* extern char *strchr(); */
1771 + s = r->program + 1;
1772 + while (op != END) { /* While that wasn't END last time... */
1774 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1775 + next = regnext(s);
1776 + if (next == NULL) /* Next ptr. */
1779 + printf("(%d)", (s-r->program)+(next-s));
1781 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1782 + /* Literal string, where present. */
1783 + while (*s != '\0') {
1792 + /* Header fields of interest. */
1793 + if (r->regstart != '\0')
1794 + printf("start `%c' ", r->regstart);
1796 + printf("anchored ");
1797 + if (r->regmust != NULL)
1798 + printf("must have \"%s\"", r->regmust);
1803 + - regprop - printable representation of opcode
1810 + static char buf[BUFLEN];
1854 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1866 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1876 + printk("<3>Regexp: corrupted opcode\n");
1880 + strncat(buf, p, BUFLEN-strlen(buf));
1886 Index: linux-2.6.24.7/net/netfilter/regexp/regexp.h
1887 ===================================================================
1889 +++ linux-2.6.24.7/net/netfilter/regexp/regexp.h
1892 + * Definitions etc. for regexp(3) routines.
1894 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1895 + * not the System V one.
1903 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1904 +which contains a version of this library, says:
1907 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1908 + * will not work properly.
1911 +However, it looks rather like this library is limited to 10. If you think
1912 +otherwise, let us know.
1916 +typedef struct regexp {
1917 + char *startp[NSUBEXP];
1918 + char *endp[NSUBEXP];
1919 + char regstart; /* Internal use only. */
1920 + char reganch; /* Internal use only. */
1921 + char *regmust; /* Internal use only. */
1922 + int regmlen; /* Internal use only. */
1923 + char program[1]; /* Unwarranted chumminess with compiler. */
1926 +regexp * regcomp(char *exp, int *patternsize);
1927 +int regexec(regexp *prog, char *string);
1928 +void regsub(regexp *prog, char *source, char *dest);
1929 +void regerror(char *s);
1932 Index: linux-2.6.24.7/net/netfilter/regexp/regmagic.h
1933 ===================================================================
1935 +++ linux-2.6.24.7/net/netfilter/regexp/regmagic.h
1938 + * The first byte of the regexp internal "program" is actually this magic
1939 + * number; the start node begins in the second byte.
1942 Index: linux-2.6.24.7/net/netfilter/regexp/regsub.c
1943 ===================================================================
1945 +++ linux-2.6.24.7/net/netfilter/regexp/regsub.c
1949 + * @(#)regsub.c 1.3 of 2 April 86
1951 + * Copyright (c) 1986 by University of Toronto.
1952 + * Written by Henry Spencer. Not derived from licensed software.
1954 + * Permission is granted to anyone to use this software for any
1955 + * purpose on any computer system, and to redistribute it freely,
1956 + * subject to the following restrictions:
1958 + * 1. The author is not responsible for the consequences of use of
1959 + * this software, no matter how awful, even if they arise
1960 + * from defects in it.
1962 + * 2. The origin of this software must not be misrepresented, either
1963 + * by explicit claim or by omission.
1965 + * 3. Altered versions must be plainly marked as such, and must not
1966 + * be misrepresented as being the original software.
1969 + * This code was modified by Ethan Sommer to work within the kernel
1970 + * (it now uses kmalloc etc..)
1973 +#include "regexp.h"
1974 +#include "regmagic.h"
1975 +#include <linux/string.h>
1979 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1981 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1985 +//void regerror(char * s)
1987 +// printk("regexp(3): %s", s);
1988 +// /* NOTREACHED */
1993 + - regsub - perform substitutions after a regexp match
1996 +regsub(regexp * prog, char * source, char * dest)
1998 + register char *src;
1999 + register char *dst;
2004 + /* Not necessary and gcc doesn't like it -MLS */
2005 + /*extern char *strncpy();*/
2007 + if (prog == NULL || source == NULL || dest == NULL) {
2008 + regerror("NULL parm to regsub");
2011 + if (UCHARAT(prog->program) != MAGIC) {
2012 + regerror("damaged regexp fed to regsub");
2018 + while ((c = *src++) != '\0') {
2021 + else if (c == '\\' && '0' <= *src && *src <= '9')
2022 + no = *src++ - '0';
2026 + if (no < 0) { /* Ordinary character. */
2027 + if (c == '\\' && (*src == '\\' || *src == '&'))
2030 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2031 + len = prog->endp[no] - prog->startp[no];
2032 + (void) strncpy(dst, prog->startp[no], len);
2034 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2035 + regerror("damaged match string");
2042 Index: linux-2.6.24.7/net/netfilter/nf_conntrack_core.c
2043 ===================================================================
2044 --- linux-2.6.24.7.orig/net/netfilter/nf_conntrack_core.c
2045 +++ linux-2.6.24.7/net/netfilter/nf_conntrack_core.c
2046 @@ -206,6 +206,14 @@ destroy_conntrack(struct nf_conntrack *n
2048 nf_ct_remove_expectations(ct);
2050 + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2051 + if(ct->layer7.app_proto)
2052 + kfree(ct->layer7.app_proto);
2053 + if(ct->layer7.app_data)
2054 + kfree(ct->layer7.app_data);
2058 /* We overload first tuple to link into unconfirmed list. */
2059 if (!nf_ct_is_confirmed(ct)) {
2060 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
2061 Index: linux-2.6.24.7/net/netfilter/nf_conntrack_standalone.c
2062 ===================================================================
2063 --- linux-2.6.24.7.orig/net/netfilter/nf_conntrack_standalone.c
2064 +++ linux-2.6.24.7/net/netfilter/nf_conntrack_standalone.c
2065 @@ -180,7 +180,12 @@ static int ct_seq_show(struct seq_file *
2069 - if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
2070 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2071 + if(conntrack->layer7.app_proto)
2072 + if(seq_printf(s, "l7proto=%s ", conntrack->layer7.app_proto))
2075 + if (seq_printf(s, "asdfuse=%u\n", atomic_read(&conntrack->ct_general.use)))
2079 Index: linux-2.6.24.7/include/net/netfilter/nf_conntrack.h
2080 ===================================================================
2081 --- linux-2.6.24.7.orig/include/net/netfilter/nf_conntrack.h
2082 +++ linux-2.6.24.7/include/net/netfilter/nf_conntrack.h
2083 @@ -124,6 +124,22 @@ struct nf_conn
2087 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
2088 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2091 + * e.g. "http". NULL before decision. "unknown" after decision
2096 + * application layer data so far. NULL after match decision.
2099 + unsigned int app_data_len;
2103 /* Storage reserved for other modules: */
2104 union nf_conntrack_proto proto;
2106 Index: linux-2.6.24.7/include/linux/netfilter/xt_layer7.h
2107 ===================================================================
2109 +++ linux-2.6.24.7/include/linux/netfilter/xt_layer7.h
2111 +#ifndef _XT_LAYER7_H
2112 +#define _XT_LAYER7_H
2114 +#define MAX_PATTERN_LEN 8192
2115 +#define MAX_PROTOCOL_LEN 256
2117 +struct xt_layer7_info {
2118 + char protocol[MAX_PROTOCOL_LEN];
2119 + char pattern[MAX_PATTERN_LEN];
2123 +#endif /* _XT_LAYER7_H */