1 --- linux-2.6.22-rc7/net/netfilter/Kconfig 2007-07-01 14:54:24.000000000 -0500
2 +++ linux-2.6.22-rc7-layer7/net/netfilter/Kconfig 2007-07-03 03:21:32.000000000 -0500
3 @@ -603,6 +603,26 @@ 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.
20 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
21 + bool 'Layer 7 debugging output'
22 + depends on NETFILTER_XT_MATCH_LAYER7
24 + Say Y to get lots of debugging output.
27 config NETFILTER_XT_MATCH_STATISTIC
28 tristate '"statistic" match support'
29 depends on NETFILTER_XTABLES
30 --- linux-2.6.22-rc7/net/netfilter/Makefile 2007-07-01 14:54:24.000000000 -0500
31 +++ linux-2.6.22-rc7-layer7/net/netfilter/Makefile 2007-07-03 01:32:35.000000000 -0500
33 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
34 --- linux-2.6.22-rc7/net/netfilter/xt_layer7.c 1969-12-31 18:00:00.000000000 -0600
35 +++ linux-2.6.22-rc7-layer7/net/netfilter/xt_layer7.c 2007-07-03 07:57:38.000000000 -0500
38 + Kernel module to match application layer (OSI layer 7) data in connections.
40 + http://l7-filter.sf.net
42 + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer.
44 + This program is free software; you can redistribute it and/or
45 + modify it under the terms of the GNU General Public License
46 + as published by the Free Software Foundation; either version
47 + 2 of the License, or (at your option) any later version.
48 + http://www.gnu.org/licenses/gpl.txt
50 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>,
51 + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait,
52 + Ethan Sommer, Justin Levandoski.
55 +#include <linux/spinlock.h>
58 +#include <linux/module.h>
59 +#include <linux/skbuff.h>
60 +#include <linux/netfilter.h>
61 +#include <net/netfilter/nf_conntrack.h>
62 +#include <net/netfilter/nf_conntrack_core.h>
63 +#include <linux/netfilter/x_tables.h>
64 +#include <linux/netfilter/xt_layer7.h>
65 +#include <linux/ctype.h>
66 +#include <linux/proc_fs.h>
68 +#include "regexp/regexp.c"
70 +MODULE_LICENSE("GPL");
71 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
72 +MODULE_DESCRIPTION("iptables application layer match module");
73 +MODULE_ALIAS("ipt_layer7");
74 +MODULE_VERSION("2.0");
76 +static int maxdatalen = 2048; // this is the default
77 +module_param(maxdatalen, int, 0444);
78 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
79 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
80 + #define DPRINTK(format,args...) printk(format,##args)
82 + #define DPRINTK(format,args...)
85 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
86 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
88 +/* Number of packets whose data we look at.
89 +This can be modified through /proc/net/layer7_numpackets */
90 +static int num_packets = 10;
92 +static struct pattern_cache {
93 + char * regex_string;
95 + struct pattern_cache * next;
96 +} * first_pattern_cache = NULL;
98 +DEFINE_SPINLOCK(l7_lock);
100 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
101 +/* Converts an unfriendly string into a friendly one by
102 +replacing unprintables with periods and all whitespace with " ". */
103 +static char * friendly_print(unsigned char * s)
105 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
109 + if (net_ratelimit())
110 + printk(KERN_ERR "layer7: out of memory in "
111 + "friendly_print, bailing.\n");
115 + for(i = 0; i < strlen(s); i++){
116 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
117 + else if(isspace(s[i])) f[i] = ' ';
124 +static char dec2hex(int i)
131 + return (i - 10 + 'a');
134 + if (net_ratelimit())
135 + printk("layer7: Problem in dec2hex\n");
140 +static char * hex_print(unsigned char * s)
142 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
146 + if (net_ratelimit())
147 + printk(KERN_ERR "layer7: out of memory in hex_print, "
152 + for(i = 0; i < strlen(s); i++) {
153 + g[i*3 ] = dec2hex(s[i]/16);
154 + g[i*3 + 1] = dec2hex(s[i]%16);
163 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
164 +over again, it make sense to cache the results. */
165 +static regexp * compile_and_cache(const char * regex_string,
166 + const char * protocol)
168 + struct pattern_cache * node = first_pattern_cache;
169 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
170 + struct pattern_cache * tmp;
173 + while (node != NULL) {
174 + if (!strcmp(node->regex_string, regex_string))
175 + return node->pattern;
177 + last_pattern_cache = node;/* points at the last non-NULL node */
181 + /* If we reach the end of the list, then we have not yet cached
182 + the pattern for this regex. Let's do that now.
183 + Be paranoid about running out of memory to avoid list corruption. */
184 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
187 + if (net_ratelimit())
188 + printk(KERN_ERR "layer7: out of memory in "
189 + "compile_and_cache, bailing.\n");
193 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
194 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
197 + if(!tmp->regex_string || !tmp->pattern) {
198 + if (net_ratelimit())
199 + printk(KERN_ERR "layer7: out of memory in "
200 + "compile_and_cache, bailing.\n");
201 + kfree(tmp->regex_string);
202 + kfree(tmp->pattern);
207 + /* Ok. The new node is all ready now. */
210 + if(first_pattern_cache == NULL) /* list is empty */
211 + first_pattern_cache = node; /* make node the beginning */
213 + last_pattern_cache->next = node; /* attach node to the end */
215 + /* copy the string and compile the regex */
216 + len = strlen(regex_string);
217 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
218 + node->pattern = regcomp((char *)regex_string, &len);
219 + if ( !node->pattern ) {
220 + if (net_ratelimit())
221 + printk(KERN_ERR "layer7: Error compiling regexp "
223 + regex_string, protocol);
224 + /* pattern is now cached as NULL, so we won't try again. */
227 + strcpy(node->regex_string, regex_string);
228 + return node->pattern;
231 +static int can_handle(const struct sk_buff *skb)
233 + if(!ip_hdr(skb)) /* not IP */
235 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
236 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
237 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
242 +/* Returns offset the into the skb->data that the application data starts */
243 +static int app_data_offset(const struct sk_buff *skb)
245 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
246 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
247 + int ip_hl = 4*ip_hdr(skb)->ihl;
249 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
250 + /* 12 == offset into TCP header for the header length field.
251 + Can't get this with skb->h.th->doff because the tcphdr
252 + struct doesn't get set when routing (this is confirmed to be
253 + true in Netfilter as well as QoS.) */
254 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
256 + return ip_hl + tcp_hl;
257 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
258 + return ip_hl + 8; /* UDP header is always 8 bytes */
259 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
260 + return ip_hl + 8; /* ICMP header is 8 bytes */
262 + if (net_ratelimit())
263 + printk(KERN_ERR "layer7: tried to handle unknown "
265 + return ip_hl + 8; /* something reasonable */
269 +/* handles whether there's a match when we aren't appending data anymore */
270 +static int match_no_append(struct nf_conn * conntrack,
271 + struct nf_conn * master_conntrack,
272 + enum ip_conntrack_info ctinfo,
273 + enum ip_conntrack_info master_ctinfo,
274 + const struct xt_layer7_info * info)
276 + /* If we're in here, throw the app data away */
277 + if(master_conntrack->layer7.app_data != NULL) {
279 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
280 + if(!master_conntrack->layer7.app_proto) {
282 + friendly_print(master_conntrack->layer7.app_data);
284 + hex_print(master_conntrack->layer7.app_data);
285 + DPRINTK("\nl7-filter gave up after %d bytes "
286 + "(%d packets):\n%s\n",
287 + strlen(f), TOTAL_PACKETS, f);
289 + DPRINTK("In hex: %s\n", g);
294 + kfree(master_conntrack->layer7.app_data);
295 + master_conntrack->layer7.app_data = NULL; /* don't free again */
298 + if(master_conntrack->layer7.app_proto){
299 + /* Here child connections set their .app_proto (for /proc) */
300 + if(!conntrack->layer7.app_proto) {
301 + conntrack->layer7.app_proto =
302 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
304 + if(!conntrack->layer7.app_proto){
305 + if (net_ratelimit())
306 + printk(KERN_ERR "layer7: out of memory "
307 + "in match_no_append, "
311 + strcpy(conntrack->layer7.app_proto,
312 + master_conntrack->layer7.app_proto);
315 + return (!strcmp(master_conntrack->layer7.app_proto,
319 + /* If not classified, set to "unknown" to distinguish from
320 + connections that are still being tested. */
321 + master_conntrack->layer7.app_proto =
322 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
323 + if(!master_conntrack->layer7.app_proto){
324 + if (net_ratelimit())
325 + printk(KERN_ERR "layer7: out of memory in "
326 + "match_no_append, bailing.\n");
329 + strcpy(master_conntrack->layer7.app_proto, "unknown");
334 +/* add the new app data to the conntrack. Return number of bytes added. */
335 +static int add_data(struct nf_conn * master_conntrack,
336 + char * app_data, int appdatalen)
339 + int oldlength = master_conntrack->layer7.app_data_len;
341 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
342 + clear on whether the race condition exists or whether this really
343 + fixes it. I might just be being dense... Anyway, if it's not really
344 + a fix, all it does is waste a very small amount of time. */
345 + if(!master_conntrack->layer7.app_data) return 0;
347 + /* Strip nulls. Make everything lower case (our regex lib doesn't
348 + do case insensitivity). Add it to the end of the current data. */
349 + for(i = 0; i < maxdatalen-oldlength-1 &&
350 + i < appdatalen; i++) {
351 + if(app_data[i] != '\0') {
352 + /* the kernel version of tolower mungs 'upper ascii' */
353 + master_conntrack->layer7.app_data[length+oldlength] =
354 + isascii(app_data[i])?
355 + tolower(app_data[i]) : app_data[i];
360 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
361 + master_conntrack->layer7.app_data_len = length + oldlength;
366 +/* taken from drivers/video/modedb.c */
367 +static int my_atoi(const char *s)
374 + val = 10*val+(*s-'0');
382 +/* write out num_packets to userland. */
383 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
384 + int* eof, void * data)
386 + if(num_packets > 99 && net_ratelimit())
387 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
389 + page[0] = num_packets/10 + '0';
390 + page[1] = num_packets%10 + '0';
399 +/* Read in num_packets from userland */
400 +static int layer7_write_proc(struct file* file, const char* buffer,
401 + unsigned long count, void *data)
403 + char * foo = kmalloc(count, GFP_ATOMIC);
406 + if (net_ratelimit())
407 + printk(KERN_ERR "layer7: out of memory, bailing. "
408 + "num_packets unchanged.\n");
412 + if(copy_from_user(foo, buffer, count)) {
417 + num_packets = my_atoi(foo);
420 + /* This has an arbitrary limit to make the math easier. I'm lazy.
421 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
422 + if(num_packets > 99) {
423 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
425 + } else if(num_packets < 1) {
426 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
434 +match(const struct sk_buff *skbin,
435 + const struct net_device *in,
436 + const struct net_device *out,
437 + const struct xt_match *match,
438 + const void *matchinfo,
440 + unsigned int protoff,
443 + /* sidestep const without getting a compiler warning... */
444 + struct sk_buff * skb = (struct sk_buff *)skbin;
446 + const struct xt_layer7_info * info = matchinfo;
447 + enum ip_conntrack_info master_ctinfo, ctinfo;
448 + struct nf_conn *master_conntrack, *conntrack;
449 + unsigned char * app_data;
450 + unsigned int pattern_result, appdatalen;
451 + regexp * comppattern;
453 + /* Be paranoid/incompetent - lock the entire match function. */
454 + spin_lock_bh(&l7_lock);
456 + if(!can_handle(skb)){
457 + DPRINTK("layer7: This is some protocol I can't handle.\n");
458 + spin_unlock_bh(&l7_lock);
459 + return info->invert;
462 + /* Treat parent & all its children together as one connection, except
463 + for the purpose of setting conntrack->layer7.app_proto in the actual
464 + connection. This makes /proc/net/ip_conntrack more satisfying. */
465 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
466 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
467 + DPRINTK("layer7: couldn't get conntrack.\n");
468 + spin_unlock_bh(&l7_lock);
469 + return info->invert;
472 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
473 + while (master_ct(master_conntrack) != NULL)
474 + master_conntrack = master_ct(master_conntrack);
476 + /* if we've classified it or seen too many packets */
477 + if(TOTAL_PACKETS > num_packets ||
478 + master_conntrack->layer7.app_proto) {
480 + pattern_result = match_no_append(conntrack, master_conntrack,
481 + ctinfo, master_ctinfo, info);
483 + /* skb->cb[0] == seen. Don't do things twice if there are
484 + multiple l7 rules. I'm not sure that using cb for this purpose
485 + is correct, even though it says "put your private variables
486 + there". But it doesn't look like it is being used for anything
487 + else in the skbs that make it here. */
488 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
490 + spin_unlock_bh(&l7_lock);
491 + return (pattern_result ^ info->invert);
494 + if(skb_is_nonlinear(skb)){
495 + if(skb_linearize(skb) != 0){
496 + if (net_ratelimit())
497 + printk(KERN_ERR "layer7: failed to linearize "
498 + "packet, bailing.\n");
499 + spin_unlock_bh(&l7_lock);
500 + return info->invert;
504 + /* now that the skb is linearized, it's safe to set these. */
505 + app_data = skb->data + app_data_offset(skb);
506 + appdatalen = skb_tail_pointer(skb) - app_data;
508 + /* the return value gets checked later, when we're ready to use it */
509 + comppattern = compile_and_cache(info->pattern, info->protocol);
511 + /* On the first packet of a connection, allocate space for app data */
512 + if(TOTAL_PACKETS == 1 && !skb->cb[0] &&
513 + !master_conntrack->layer7.app_data){
514 + master_conntrack->layer7.app_data =
515 + kmalloc(maxdatalen, GFP_ATOMIC);
516 + if(!master_conntrack->layer7.app_data){
517 + if (net_ratelimit())
518 + printk(KERN_ERR "layer7: out of memory in "
519 + "match, bailing.\n");
520 + spin_unlock_bh(&l7_lock);
521 + return info->invert;
524 + master_conntrack->layer7.app_data[0] = '\0';
527 + /* Can be here, but unallocated, if numpackets is increased near
528 + the beginning of a connection */
529 + if(master_conntrack->layer7.app_data == NULL){
530 + spin_unlock_bh(&l7_lock);
531 + return (info->invert); /* unmatched */
536 + newbytes = add_data(master_conntrack, app_data, appdatalen);
538 + if(newbytes == 0) { /* didn't add any data */
540 + /* Didn't match before, not going to match now */
541 + spin_unlock_bh(&l7_lock);
542 + return info->invert;
546 + /* If looking for "unknown", then never match. "Unknown" means that
547 + we've given up; we're still trying with these packets. */
548 + if(!strcmp(info->protocol, "unknown")) {
549 + pattern_result = 0;
550 + /* If looking for "unset", then always match. "Unset" means that we
551 + haven't yet classified the connection. */
552 + } else if(!strcmp(info->protocol, "unset")) {
553 + pattern_result = 2;
554 + DPRINTK("layer7: matched unset: not yet classified "
555 + "(%d/%d packets)\n", TOTAL_PACKETS, num_packets);
556 + /* If the regexp failed to compile, don't bother running it */
557 + } else if(comppattern &&
558 + regexec(comppattern, master_conntrack->layer7.app_data)){
559 + DPRINTK("layer7: matched %s\n", info->protocol);
560 + pattern_result = 1;
561 + } else pattern_result = 0;
563 + if(pattern_result == 1) {
564 + master_conntrack->layer7.app_proto =
565 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
566 + if(!master_conntrack->layer7.app_proto){
567 + if (net_ratelimit())
568 + printk(KERN_ERR "layer7: out of memory in "
569 + "match, bailing.\n");
570 + spin_unlock_bh(&l7_lock);
571 + return (pattern_result ^ info->invert);
573 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
574 + } else if(pattern_result > 1) { /* cleanup from "unset" */
575 + pattern_result = 1;
578 + /* mark the packet seen */
581 + spin_unlock_bh(&l7_lock);
582 + return (pattern_result ^ info->invert);
585 +static int check(const char *tablename,
587 + const struct xt_match *match,
589 + unsigned int hook_mask)
592 + // load nf_conntrack_ipv4
593 + if (nf_ct_l3proto_try_module_get(match->family) < 0) {
594 + printk(KERN_WARNING "can't load conntrack support for "
595 + "proto=%d\n", match->family);
602 +destroy(const struct xt_match *match, void *matchinfo)
604 + nf_ct_l3proto_module_put(match->family);
607 +static struct xt_match xt_layer7_match[] = {
611 + .checkentry = check,
613 + .destroy = destroy,
614 + .matchsize = sizeof(struct xt_layer7_info),
619 +static void layer7_cleanup_proc(void)
621 + remove_proc_entry("layer7_numpackets", proc_net);
624 +/* register the proc file */
625 +static void layer7_init_proc(void)
627 + struct proc_dir_entry* entry;
628 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
629 + entry->read_proc = layer7_read_proc;
630 + entry->write_proc = layer7_write_proc;
633 +static int __init xt_layer7_init(void)
637 + layer7_init_proc();
638 + if(maxdatalen < 1) {
639 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
643 + /* This is not a hard limit. It's just here to prevent people from
644 + bringing their slow machines to a grinding halt. */
645 + else if(maxdatalen > 65536) {
646 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
648 + maxdatalen = 65536;
650 + return xt_register_matches(xt_layer7_match,
651 + ARRAY_SIZE(xt_layer7_match));
654 +static void __exit xt_layer7_fini(void)
656 + layer7_cleanup_proc();
657 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
660 +module_init(xt_layer7_init);
661 +module_exit(xt_layer7_fini);
663 --- linux-2.6.22-rc7/net/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
664 +++ linux-2.6.22-rc7-layer7/net/netfilter/regexp/regexp.c 2007-07-03 01:32:35.000000000 -0500
667 + * regcomp and regexec -- regsub and regerror are elsewhere
668 + * @(#)regexp.c 1.3 of 18 April 87
670 + * Copyright (c) 1986 by University of Toronto.
671 + * Written by Henry Spencer. Not derived from licensed software.
673 + * Permission is granted to anyone to use this software for any
674 + * purpose on any computer system, and to redistribute it freely,
675 + * subject to the following restrictions:
677 + * 1. The author is not responsible for the consequences of use of
678 + * this software, no matter how awful, even if they arise
679 + * from defects in it.
681 + * 2. The origin of this software must not be misrepresented, either
682 + * by explicit claim or by omission.
684 + * 3. Altered versions must be plainly marked as such, and must not
685 + * be misrepresented as being the original software.
687 + * Beware that some of this code is subtly aware of the way operator
688 + * precedence is structured in regular expressions. Serious changes in
689 + * regular-expression syntax might require a total rethink.
691 + * This code was modified by Ethan Sommer to work within the kernel
692 + * (it now uses kmalloc etc..)
694 + * Modified slightly by Matthew Strait to use more modern C.
698 +#include "regmagic.h"
700 +/* added by ethan and matt. Lets it work in both kernel and user space.
701 +(So iptables can use it, for instance.) Yea, it goes both ways... */
703 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
705 + #define printk(format,args...) printf(format,##args)
708 +void regerror(char * s)
710 + printk("<3>Regexp: %s\n", s);
715 + * The "internal use only" fields in regexp.h are present to pass info from
716 + * compile to execute that permits the execute phase to run lots faster on
717 + * simple cases. They are:
719 + * regstart char that must begin a match; '\0' if none obvious
720 + * reganch is the match anchored (at beginning-of-line only)?
721 + * regmust string (pointer into program) that match must include, or NULL
722 + * regmlen length of regmust string
724 + * Regstart and reganch permit very fast decisions on suitable starting points
725 + * for a match, cutting down the work a lot. Regmust permits fast rejection
726 + * of lines that cannot possibly match. The regmust tests are costly enough
727 + * that regcomp() supplies a regmust only if the r.e. contains something
728 + * potentially expensive (at present, the only such thing detected is * or +
729 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
730 + * supplied because the test in regexec() needs it and regcomp() is computing
735 + * Structure for regexp "program". This is essentially a linear encoding
736 + * of a nondeterministic finite-state machine (aka syntax charts or
737 + * "railroad normal form" in parsing technology). Each node is an opcode
738 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
739 + * all nodes except BRANCH implement concatenation; a "next" pointer with
740 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
741 + * have one of the subtle syntax dependencies: an individual BRANCH (as
742 + * opposed to a collection of them) is never concatenated with anything
743 + * because of operator precedence.) The operand of some types of node is
744 + * a literal string; for others, it is a node leading into a sub-FSM. In
745 + * particular, the operand of a BRANCH node is the first node of the branch.
746 + * (NB this is *not* a tree structure: the tail of the branch connects
747 + * to the thing following the set of BRANCHes.) The opcodes are:
750 +/* definition number opnd? meaning */
751 +#define END 0 /* no End of program. */
752 +#define BOL 1 /* no Match "" at beginning of line. */
753 +#define EOL 2 /* no Match "" at end of line. */
754 +#define ANY 3 /* no Match any one character. */
755 +#define ANYOF 4 /* str Match any character in this string. */
756 +#define ANYBUT 5 /* str Match any character not in this string. */
757 +#define BRANCH 6 /* node Match this alternative, or the next... */
758 +#define BACK 7 /* no Match "", "next" ptr points backward. */
759 +#define EXACTLY 8 /* str Match this string. */
760 +#define NOTHING 9 /* no Match empty string. */
761 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
762 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
763 +#define OPEN 20 /* no Mark this point in input as start of #n. */
764 + /* OPEN+1 is number 1, etc. */
765 +#define CLOSE 30 /* no Analogous to OPEN. */
770 + * BRANCH The set of branches constituting a single choice are hooked
771 + * together with their "next" pointers, since precedence prevents
772 + * anything being concatenated to any individual branch. The
773 + * "next" pointer of the last BRANCH in a choice points to the
774 + * thing following the whole choice. This is also where the
775 + * final "next" pointer of each individual branch points; each
776 + * branch starts with the operand node of a BRANCH node.
778 + * BACK Normal "next" pointers all implicitly point forward; BACK
779 + * exists to make loop structures possible.
781 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
782 + * BRANCH structures using BACK. Simple cases (one character
783 + * per match) are implemented with STAR and PLUS for speed
784 + * and to minimize recursive plunges.
786 + * OPEN,CLOSE ...are numbered at compile time.
790 + * A node is one char of opcode followed by two chars of "next" pointer.
791 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
792 + * value is a positive offset from the opcode of the node containing it.
793 + * An operand, if any, simply follows the node. (Note that much of the
794 + * code generation knows about this implicit relationship.)
796 + * Using two bytes for the "next" pointer is vast overkill for most things,
797 + * but allows patterns to get big without disasters.
799 +#define OP(p) (*(p))
800 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
801 +#define OPERAND(p) ((p) + 3)
804 + * See regmagic.h for one further detail of program structure.
809 + * Utility definitions.
812 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
814 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
817 +#define FAIL(m) { regerror(m); return(NULL); }
818 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
819 +#define META "^$.[()|?+*\\"
822 + * Flags to be passed up and down.
824 +#define HASWIDTH 01 /* Known never to match null string. */
825 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
826 +#define SPSTART 04 /* Starts with * or +. */
827 +#define WORST 0 /* Worst case. */
830 + * Global work variables for regcomp().
832 +struct match_globals {
833 +char *reginput; /* String-input pointer. */
834 +char *regbol; /* Beginning of input, for ^ check. */
835 +char **regstartp; /* Pointer to startp array. */
836 +char **regendp; /* Ditto for endp. */
837 +char *regparse; /* Input-scan pointer. */
838 +int regnpar; /* () count. */
840 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
841 +long regsize; /* Code size. */
845 + * Forward declarations for regcomp()'s friends.
848 +#define STATIC static
850 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
851 +STATIC char *regbranch(struct match_globals *g, int *flagp);
852 +STATIC char *regpiece(struct match_globals *g, int *flagp);
853 +STATIC char *regatom(struct match_globals *g, int *flagp);
854 +STATIC char *regnode(struct match_globals *g, char op);
855 +STATIC char *regnext(struct match_globals *g, char *p);
856 +STATIC void regc(struct match_globals *g, char b);
857 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
858 +STATIC void regtail(struct match_globals *g, char *p, char *val);
859 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
862 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
869 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
870 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
871 + if (*scan1 == *scan2++)
879 + - regcomp - compile a regular expression into internal code
881 + * We can't allocate space until we know how big the compiled form will be,
882 + * but we can't compile it (and thus know how big it is) until we've got a
883 + * place to put the code. So we cheat: we compile it twice, once with code
884 + * generation turned off and size counting turned on, and once "for real".
885 + * This also means that we don't allocate space until we are sure that the
886 + * thing really will compile successfully, and we never have to move the
887 + * code and thus invalidate pointers into it. (Note that it has to be in
888 + * one piece because free() must be able to free it all.)
890 + * Beware that the optimization-preparation code in here knows about some
891 + * of the structure of the compiled regexp.
894 +regcomp(char *exp,int *patternsize)
896 + register regexp *r;
897 + register char *scan;
898 + register char *longest;
901 + struct match_globals g;
903 + /* commented out by ethan
904 + extern char *malloc();
908 + FAIL("NULL argument");
910 + /* First pass: determine size, legality. */
914 + g.regcode = &g.regdummy;
916 + if (reg(&g, 0, &flags) == NULL)
919 + /* Small enough for pointer-storage convention? */
920 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
921 + FAIL("regexp too big");
923 + /* Allocate space. */
924 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
925 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
927 + FAIL("out of space");
929 + /* Second pass: emit code. */
932 + g.regcode = r->program;
934 + if (reg(&g, 0, &flags) == NULL)
937 + /* Dig out information for optimizations. */
938 + r->regstart = '\0'; /* Worst-case defaults. */
942 + scan = r->program+1; /* First BRANCH. */
943 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
944 + scan = OPERAND(scan);
946 + /* Starting-point info. */
947 + if (OP(scan) == EXACTLY)
948 + r->regstart = *OPERAND(scan);
949 + else if (OP(scan) == BOL)
953 + * If there's something expensive in the r.e., find the
954 + * longest literal string that must appear and make it the
955 + * regmust. Resolve ties in favor of later strings, since
956 + * the regstart check works with the beginning of the r.e.
957 + * and avoiding duplication strengthens checking. Not a
958 + * strong reason, but sufficient in the absence of others.
960 + if (flags&SPSTART) {
963 + for (; scan != NULL; scan = regnext(&g, scan))
964 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
965 + longest = OPERAND(scan);
966 + len = strlen(OPERAND(scan));
968 + r->regmust = longest;
977 + - reg - regular expression, i.e. main body or parenthesized thing
979 + * Caller must absorb opening parenthesis.
981 + * Combining parenthesis handling with the base level of regular expression
982 + * is a trifle forced, but the need to tie the tails of the branches to what
983 + * follows makes it hard to avoid.
986 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
988 + register char *ret;
990 + register char *ender;
991 + register int parno = 0; /* 0 makes gcc happy */
994 + *flagp = HASWIDTH; /* Tentatively. */
996 + /* Make an OPEN node, if parenthesized. */
998 + if (g->regnpar >= NSUBEXP)
999 + FAIL("too many ()");
1000 + parno = g->regnpar;
1002 + ret = regnode(g, OPEN+parno);
1006 + /* Pick up the branches, linking them together. */
1007 + br = regbranch(g, &flags);
1011 + regtail(g, ret, br); /* OPEN -> first. */
1014 + if (!(flags&HASWIDTH))
1015 + *flagp &= ~HASWIDTH;
1016 + *flagp |= flags&SPSTART;
1017 + while (*g->regparse == '|') {
1019 + br = regbranch(g, &flags);
1022 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1023 + if (!(flags&HASWIDTH))
1024 + *flagp &= ~HASWIDTH;
1025 + *flagp |= flags&SPSTART;
1028 + /* Make a closing node, and hook it on the end. */
1029 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1030 + regtail(g, ret, ender);
1032 + /* Hook the tails of the branches to the closing node. */
1033 + for (br = ret; br != NULL; br = regnext(g, br))
1034 + regoptail(g, br, ender);
1036 + /* Check for proper termination. */
1037 + if (paren && *g->regparse++ != ')') {
1038 + FAIL("unmatched ()");
1039 + } else if (!paren && *g->regparse != '\0') {
1040 + if (*g->regparse == ')') {
1041 + FAIL("unmatched ()");
1043 + FAIL("junk on end"); /* "Can't happen". */
1051 + - regbranch - one alternative of an | operator
1053 + * Implements the concatenation operator.
1056 +regbranch(struct match_globals *g, int *flagp)
1058 + register char *ret;
1059 + register char *chain;
1060 + register char *latest;
1063 + *flagp = WORST; /* Tentatively. */
1065 + ret = regnode(g, BRANCH);
1067 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1068 + latest = regpiece(g, &flags);
1069 + if (latest == NULL)
1071 + *flagp |= flags&HASWIDTH;
1072 + if (chain == NULL) /* First piece. */
1073 + *flagp |= flags&SPSTART;
1075 + regtail(g, chain, latest);
1078 + if (chain == NULL) /* Loop ran zero times. */
1079 + (void) regnode(g, NOTHING);
1085 + - regpiece - something followed by possible [*+?]
1087 + * Note that the branching code sequences used for ? and the general cases
1088 + * of * and + are somewhat optimized: they use the same NOTHING node as
1089 + * both the endmarker for their branch list and the body of the last branch.
1090 + * It might seem that this node could be dispensed with entirely, but the
1091 + * endmarker role is not redundant.
1094 +regpiece(struct match_globals *g, int *flagp)
1096 + register char *ret;
1098 + register char *next;
1101 + ret = regatom(g, &flags);
1105 + op = *g->regparse;
1106 + if (!ISMULT(op)) {
1111 + if (!(flags&HASWIDTH) && op != '?')
1112 + FAIL("*+ operand could be empty");
1113 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1115 + if (op == '*' && (flags&SIMPLE))
1116 + reginsert(g, STAR, ret);
1117 + else if (op == '*') {
1118 + /* Emit x* as (x&|), where & means "self". */
1119 + reginsert(g, BRANCH, ret); /* Either x */
1120 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1121 + regoptail(g, ret, ret); /* back */
1122 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1123 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1124 + } else if (op == '+' && (flags&SIMPLE))
1125 + reginsert(g, PLUS, ret);
1126 + else if (op == '+') {
1127 + /* Emit x+ as x(&|), where & means "self". */
1128 + next = regnode(g, BRANCH); /* Either */
1129 + regtail(g, ret, next);
1130 + regtail(g, regnode(g, BACK), ret); /* loop back */
1131 + regtail(g, next, regnode(g, BRANCH)); /* or */
1132 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1133 + } else if (op == '?') {
1134 + /* Emit x? as (x|) */
1135 + reginsert(g, BRANCH, ret); /* Either x */
1136 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1137 + next = regnode(g, NOTHING); /* null. */
1138 + regtail(g, ret, next);
1139 + regoptail(g, ret, next);
1142 + if (ISMULT(*g->regparse))
1143 + FAIL("nested *?+");
1149 + - regatom - the lowest level
1151 + * Optimization: gobbles an entire sequence of ordinary characters so that
1152 + * it can turn them into a single node, which is smaller to store and
1153 + * faster to run. Backslashed characters are exceptions, each becoming a
1154 + * separate node; the code is simpler that way and it's not worth fixing.
1157 +regatom(struct match_globals *g, int *flagp)
1159 + register char *ret;
1162 + *flagp = WORST; /* Tentatively. */
1164 + switch (*g->regparse++) {
1166 + ret = regnode(g, BOL);
1169 + ret = regnode(g, EOL);
1172 + ret = regnode(g, ANY);
1173 + *flagp |= HASWIDTH|SIMPLE;
1176 + register int class;
1177 + register int classend;
1179 + if (*g->regparse == '^') { /* Complement of range. */
1180 + ret = regnode(g, ANYBUT);
1183 + ret = regnode(g, ANYOF);
1184 + if (*g->regparse == ']' || *g->regparse == '-')
1185 + regc(g, *g->regparse++);
1186 + while (*g->regparse != '\0' && *g->regparse != ']') {
1187 + if (*g->regparse == '-') {
1189 + if (*g->regparse == ']' || *g->regparse == '\0')
1192 + class = UCHARAT(g->regparse-2)+1;
1193 + classend = UCHARAT(g->regparse);
1194 + if (class > classend+1)
1195 + FAIL("invalid [] range");
1196 + for (; class <= classend; class++)
1201 + regc(g, *g->regparse++);
1204 + if (*g->regparse != ']')
1205 + FAIL("unmatched []");
1207 + *flagp |= HASWIDTH|SIMPLE;
1211 + ret = reg(g, 1, &flags);
1214 + *flagp |= flags&(HASWIDTH|SPSTART);
1219 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1224 + FAIL("?+* follows nothing");
1227 + if (*g->regparse == '\0')
1228 + FAIL("trailing \\");
1229 + ret = regnode(g, EXACTLY);
1230 + regc(g, *g->regparse++);
1232 + *flagp |= HASWIDTH|SIMPLE;
1236 + register char ender;
1239 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1241 + FAIL("internal disaster");
1242 + ender = *(g->regparse+len);
1243 + if (len > 1 && ISMULT(ender))
1244 + len--; /* Back off clear of ?+* operand. */
1245 + *flagp |= HASWIDTH;
1248 + ret = regnode(g, EXACTLY);
1250 + regc(g, *g->regparse++);
1262 + - regnode - emit a node
1264 +static char * /* Location. */
1265 +regnode(struct match_globals *g, char op)
1267 + register char *ret;
1268 + register char *ptr;
1271 + if (ret == &g->regdummy) {
1278 + *ptr++ = '\0'; /* Null "next" pointer. */
1286 + - regc - emit (if appropriate) a byte of code
1289 +regc(struct match_globals *g, char b)
1291 + if (g->regcode != &g->regdummy)
1292 + *g->regcode++ = b;
1298 + - reginsert - insert an operator in front of already-emitted operand
1300 + * Means relocating the operand.
1303 +reginsert(struct match_globals *g, char op, char* opnd)
1305 + register char *src;
1306 + register char *dst;
1307 + register char *place;
1309 + if (g->regcode == &g->regdummy) {
1317 + while (src > opnd)
1320 + place = opnd; /* Op node, where operand used to be. */
1327 + - regtail - set the next-pointer at the end of a node chain
1330 +regtail(struct match_globals *g, char *p, char *val)
1332 + register char *scan;
1333 + register char *temp;
1334 + register int offset;
1336 + if (p == &g->regdummy)
1339 + /* Find last node. */
1342 + temp = regnext(g, scan);
1348 + if (OP(scan) == BACK)
1349 + offset = scan - val;
1351 + offset = val - scan;
1352 + *(scan+1) = (offset>>8)&0377;
1353 + *(scan+2) = offset&0377;
1357 + - regoptail - regtail on operand of first argument; nop if operandless
1360 +regoptail(struct match_globals *g, char *p, char *val)
1362 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1363 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1365 + regtail(g, OPERAND(p), val);
1369 + * regexec and friends
1376 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1377 +STATIC int regmatch(struct match_globals *g, char *prog);
1378 +STATIC int regrepeat(struct match_globals *g, char *p);
1381 +int regnarrate = 0;
1383 +STATIC char *regprop(char *op);
1387 + - regexec - match a regexp against a string
1390 +regexec(regexp *prog, char *string)
1393 + struct match_globals g;
1395 + /* Be paranoid... */
1396 + if (prog == NULL || string == NULL) {
1397 + printk("<3>Regexp: NULL parameter\n");
1401 + /* Check validity of program. */
1402 + if (UCHARAT(prog->program) != MAGIC) {
1403 + printk("<3>Regexp: corrupted program\n");
1407 + /* If there is a "must appear" string, look for it. */
1408 + if (prog->regmust != NULL) {
1410 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1411 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1412 + break; /* Found it. */
1415 + if (s == NULL) /* Not present. */
1419 + /* Mark beginning of line for ^ . */
1420 + g.regbol = string;
1422 + /* Simplest case: anchored match need be tried only once. */
1423 + if (prog->reganch)
1424 + return(regtry(&g, prog, string));
1426 + /* Messy cases: unanchored match. */
1428 + if (prog->regstart != '\0')
1429 + /* We know what char it must start with. */
1430 + while ((s = strchr(s, prog->regstart)) != NULL) {
1431 + if (regtry(&g, prog, s))
1436 + /* We don't -- general case. */
1438 + if (regtry(&g, prog, s))
1440 + } while (*s++ != '\0');
1447 + - regtry - try match at specific point
1449 +static int /* 0 failure, 1 success */
1450 +regtry(struct match_globals *g, regexp *prog, char *string)
1453 + register char **sp;
1454 + register char **ep;
1456 + g->reginput = string;
1457 + g->regstartp = prog->startp;
1458 + g->regendp = prog->endp;
1460 + sp = prog->startp;
1462 + for (i = NSUBEXP; i > 0; i--) {
1466 + if (regmatch(g, prog->program + 1)) {
1467 + prog->startp[0] = string;
1468 + prog->endp[0] = g->reginput;
1475 + - regmatch - main matching routine
1477 + * Conceptually the strategy is simple: check to see whether the current
1478 + * node matches, call self recursively to see whether the rest matches,
1479 + * and then act accordingly. In practice we make some effort to avoid
1480 + * recursion, in particular by going through "ordinary" nodes (that don't
1481 + * need to know whether the rest of the match failed) by a loop instead of
1484 +static int /* 0 failure, 1 success */
1485 +regmatch(struct match_globals *g, char *prog)
1487 + register char *scan = prog; /* Current node. */
1488 + char *next; /* Next node. */
1491 + if (scan != NULL && regnarrate)
1492 + fprintf(stderr, "%s(\n", regprop(scan));
1494 + while (scan != NULL) {
1497 + fprintf(stderr, "%s...\n", regprop(scan));
1499 + next = regnext(g, scan);
1501 + switch (OP(scan)) {
1503 + if (g->reginput != g->regbol)
1507 + if (*g->reginput != '\0')
1511 + if (*g->reginput == '\0')
1517 + register char *opnd;
1519 + opnd = OPERAND(scan);
1520 + /* Inline the first character, for speed. */
1521 + if (*opnd != *g->reginput)
1523 + len = strlen(opnd);
1524 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1526 + g->reginput += len;
1530 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1535 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1552 + register char *save;
1554 + no = OP(scan) - OPEN;
1555 + save = g->reginput;
1557 + if (regmatch(g, next)) {
1559 + * Don't set startp if some later
1560 + * invocation of the same parentheses
1563 + if (g->regstartp[no] == NULL)
1564 + g->regstartp[no] = save;
1581 + register char *save;
1583 + no = OP(scan) - CLOSE;
1584 + save = g->reginput;
1586 + if (regmatch(g, next)) {
1588 + * Don't set endp if some later
1589 + * invocation of the same parentheses
1592 + if (g->regendp[no] == NULL)
1593 + g->regendp[no] = save;
1600 + register char *save;
1602 + if (OP(next) != BRANCH) /* No choice. */
1603 + next = OPERAND(scan); /* Avoid recursion. */
1606 + save = g->reginput;
1607 + if (regmatch(g, OPERAND(scan)))
1609 + g->reginput = save;
1610 + scan = regnext(g, scan);
1611 + } while (scan != NULL && OP(scan) == BRANCH);
1619 + register char nextch;
1621 + register char *save;
1625 + * Lookahead to avoid useless match attempts
1626 + * when we know what character comes next.
1629 + if (OP(next) == EXACTLY)
1630 + nextch = *OPERAND(next);
1631 + min = (OP(scan) == STAR) ? 0 : 1;
1632 + save = g->reginput;
1633 + no = regrepeat(g, OPERAND(scan));
1634 + while (no >= min) {
1635 + /* If it could work, try it. */
1636 + if (nextch == '\0' || *g->reginput == nextch)
1637 + if (regmatch(g, next))
1639 + /* Couldn't or didn't -- back up. */
1641 + g->reginput = save + no;
1647 + return(1); /* Success! */
1650 + printk("<3>Regexp: memory corruption\n");
1659 + * We get here only if there's trouble -- normally "case END" is
1660 + * the terminating point.
1662 + printk("<3>Regexp: corrupted pointers\n");
1667 + - regrepeat - repeatedly match something simple, report how many
1670 +regrepeat(struct match_globals *g, char *p)
1672 + register int count = 0;
1673 + register char *scan;
1674 + register char *opnd;
1676 + scan = g->reginput;
1677 + opnd = OPERAND(p);
1680 + count = strlen(scan);
1684 + while (*opnd == *scan) {
1690 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1696 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1701 + default: /* Oh dear. Called inappropriately. */
1702 + printk("<3>Regexp: internal foulup\n");
1703 + count = 0; /* Best compromise. */
1706 + g->reginput = scan;
1712 + - regnext - dig the "next" pointer out of a node
1715 +regnext(struct match_globals *g, char *p)
1717 + register int offset;
1719 + if (p == &g->regdummy)
1726 + if (OP(p) == BACK)
1734 +STATIC char *regprop();
1737 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1743 + register char op = EXACTLY; /* Arbitrary non-END op. */
1744 + register char *next;
1745 + /* extern char *strchr(); */
1748 + s = r->program + 1;
1749 + while (op != END) { /* While that wasn't END last time... */
1751 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1752 + next = regnext(s);
1753 + if (next == NULL) /* Next ptr. */
1756 + printf("(%d)", (s-r->program)+(next-s));
1758 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1759 + /* Literal string, where present. */
1760 + while (*s != '\0') {
1769 + /* Header fields of interest. */
1770 + if (r->regstart != '\0')
1771 + printf("start `%c' ", r->regstart);
1773 + printf("anchored ");
1774 + if (r->regmust != NULL)
1775 + printf("must have \"%s\"", r->regmust);
1780 + - regprop - printable representation of opcode
1787 + static char buf[BUFLEN];
1831 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1843 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1853 + printk("<3>Regexp: corrupted opcode\n");
1857 + strncat(buf, p, BUFLEN-strlen(buf));
1863 --- linux-2.6.22-rc7/net/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1864 +++ linux-2.6.22-rc7-layer7/net/netfilter/regexp/regexp.h 2007-07-03 01:32:35.000000000 -0500
1867 + * Definitions etc. for regexp(3) routines.
1869 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1870 + * not the System V one.
1878 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1879 +which contains a version of this library, says:
1882 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1883 + * will not work properly.
1886 +However, it looks rather like this library is limited to 10. If you think
1887 +otherwise, let us know.
1891 +typedef struct regexp {
1892 + char *startp[NSUBEXP];
1893 + char *endp[NSUBEXP];
1894 + char regstart; /* Internal use only. */
1895 + char reganch; /* Internal use only. */
1896 + char *regmust; /* Internal use only. */
1897 + int regmlen; /* Internal use only. */
1898 + char program[1]; /* Unwarranted chumminess with compiler. */
1901 +regexp * regcomp(char *exp, int *patternsize);
1902 +int regexec(regexp *prog, char *string);
1903 +void regsub(regexp *prog, char *source, char *dest);
1904 +void regerror(char *s);
1907 --- linux-2.6.22-rc7/net/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1908 +++ linux-2.6.22-rc7-layer7/net/netfilter/regexp/regmagic.h 2007-07-03 01:32:35.000000000 -0500
1911 + * The first byte of the regexp internal "program" is actually this magic
1912 + * number; the start node begins in the second byte.
1915 --- linux-2.6.22-rc7/net/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1916 +++ linux-2.6.22-rc7-layer7/net/netfilter/regexp/regsub.c 2007-07-03 01:32:35.000000000 -0500
1920 + * @(#)regsub.c 1.3 of 2 April 86
1922 + * Copyright (c) 1986 by University of Toronto.
1923 + * Written by Henry Spencer. Not derived from licensed software.
1925 + * Permission is granted to anyone to use this software for any
1926 + * purpose on any computer system, and to redistribute it freely,
1927 + * subject to the following restrictions:
1929 + * 1. The author is not responsible for the consequences of use of
1930 + * this software, no matter how awful, even if they arise
1931 + * from defects in it.
1933 + * 2. The origin of this software must not be misrepresented, either
1934 + * by explicit claim or by omission.
1936 + * 3. Altered versions must be plainly marked as such, and must not
1937 + * be misrepresented as being the original software.
1940 + * This code was modified by Ethan Sommer to work within the kernel
1941 + * (it now uses kmalloc etc..)
1944 +#include "regexp.h"
1945 +#include "regmagic.h"
1946 +#include <linux/string.h>
1950 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1952 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1956 +//void regerror(char * s)
1958 +// printk("regexp(3): %s", s);
1959 +// /* NOTREACHED */
1964 + - regsub - perform substitutions after a regexp match
1967 +regsub(regexp * prog, char * source, char * dest)
1969 + register char *src;
1970 + register char *dst;
1975 + /* Not necessary and gcc doesn't like it -MLS */
1976 + /*extern char *strncpy();*/
1978 + if (prog == NULL || source == NULL || dest == NULL) {
1979 + regerror("NULL parm to regsub");
1982 + if (UCHARAT(prog->program) != MAGIC) {
1983 + regerror("damaged regexp fed to regsub");
1989 + while ((c = *src++) != '\0') {
1992 + else if (c == '\\' && '0' <= *src && *src <= '9')
1993 + no = *src++ - '0';
1997 + if (no < 0) { /* Ordinary character. */
1998 + if (c == '\\' && (*src == '\\' || *src == '&'))
2001 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2002 + len = prog->endp[no] - prog->startp[no];
2003 + (void) strncpy(dst, prog->startp[no], len);
2005 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2006 + regerror("damaged match string");
2013 --- linux-2.6.22-rc7/net/netfilter/nf_conntrack_core.c 2007-07-01 14:54:24.000000000 -0500
2014 +++ linux-2.6.22-rc7-layer7/net/netfilter/nf_conntrack_core.c 2007-07-03 01:32:35.000000000 -0500
2015 @@ -330,6 +330,14 @@ destroy_conntrack(struct nf_conntrack *n
2017 nf_ct_remove_expectations(ct);
2019 + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2020 + if(ct->layer7.app_proto)
2021 + kfree(ct->layer7.app_proto);
2022 + if(ct->layer7.app_data)
2023 + kfree(ct->layer7.app_data);
2027 /* We overload first tuple to link into unconfirmed list. */
2028 if (!nf_ct_is_confirmed(ct)) {
2029 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
2030 --- linux-2.6.22-rc7/net/netfilter/nf_conntrack_standalone.c 2007-07-01 14:54:24.000000000 -0500
2031 +++ linux-2.6.22-rc7-layer7/net/netfilter/nf_conntrack_standalone.c 2007-07-03 01:32:35.000000000 -0500
2032 @@ -184,6 +184,11 @@ static int ct_seq_show(struct seq_file *
2036 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2037 + if(conntrack->layer7.app_proto)
2038 + if(seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
2041 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
2044 --- linux-2.6.22-rc7/include/net/netfilter/nf_conntrack.h 2007-07-01 14:54:24.000000000 -0500
2045 +++ linux-2.6.22-rc7-layer7/include/net/netfilter/nf_conntrack.h 2007-07-03 01:32:35.000000000 -0500
2046 @@ -128,6 +128,22 @@ struct nf_conn
2050 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
2051 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2054 + * e.g. "http". NULL before decision. "unknown" after decision
2059 + * application layer data so far. NULL after match decision.
2062 + unsigned int app_data_len;
2066 /* Storage reserved for other modules: */
2067 union nf_conntrack_proto proto;
2069 --- linux-2.6.22-rc7/include/linux/netfilter/xt_layer7.h 1969-12-31 18:00:00.000000000 -0600
2070 +++ linux-2.6.22-rc7-layer7/include/linux/netfilter/xt_layer7.h 2007-07-03 07:48:19.000000000 -0500
2072 +#ifndef _XT_LAYER7_H
2073 +#define _XT_LAYER7_H
2075 +#define MAX_PATTERN_LEN 8192
2076 +#define MAX_PROTOCOL_LEN 256
2078 +struct xt_layer7_info {
2079 + char protocol[MAX_PROTOCOL_LEN];
2080 + char pattern[MAX_PATTERN_LEN];
2084 +#endif /* _XT_LAYER7_H */