1 --- linux-2.6.24-rc5/net/netfilter/Kconfig 2007-12-11 23:43:21.000000000 -0600
2 +++ linux-2.6.24-rc5-layer7/net/netfilter/Kconfig 2007-12-11 23:35:12.000000000 -0600
3 @@ -633,6 +633,27 @@ config NETFILTER_XT_MATCH_STATE
5 To compile it as a module, choose M here. If unsure, say N.
7 +config NETFILTER_XT_MATCH_LAYER7
8 + tristate '"layer7" match support'
9 + depends on NETFILTER_XTABLES
10 + depends on EXPERIMENTAL && (IP_NF_CONNTRACK || NF_CONNTRACK)
11 + depends on NF_CT_ACCT
13 + Say Y if you want to be able to classify connections (and their
14 + packets) based on regular expression matching of their application
15 + layer data. This is one way to classify applications such as
16 + peer-to-peer filesharing systems that do not always use the same
19 + To compile it as a module, choose M here. If unsure, say N.
21 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
22 + bool 'Layer 7 debugging output'
23 + depends on NETFILTER_XT_MATCH_LAYER7
25 + Say Y to get lots of debugging output.
28 config NETFILTER_XT_MATCH_STATISTIC
29 tristate '"statistic" match support'
30 depends on NETFILTER_XTABLES
31 --- linux-2.6.24-rc5/net/netfilter/Makefile 2007-12-11 23:43:21.000000000 -0600
32 +++ linux-2.6.24-rc5-layer7/net/netfilter/Makefile 2007-12-11 23:35:12.000000000 -0600
33 @@ -73,6 +73,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_QUOTA) +
34 obj-$(CONFIG_NETFILTER_XT_MATCH_REALM) += xt_realm.o
35 obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o
36 obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o
37 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
38 obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o
39 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
40 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
41 --- linux-2.6.24-rc5/net/netfilter/xt_layer7.c 1969-12-31 18:00:00.000000000 -0600
42 +++ linux-2.6.24-rc5-layer7/net/netfilter/xt_layer7.c 2007-12-11 23:55:46.000000000 -0600
45 + Kernel module to match application layer (OSI layer 7) data in connections.
47 + http://l7-filter.sf.net
49 + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer.
51 + This program is free software; you can redistribute it and/or
52 + modify it under the terms of the GNU General Public License
53 + as published by the Free Software Foundation; either version
54 + 2 of the License, or (at your option) any later version.
55 + http://www.gnu.org/licenses/gpl.txt
57 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>,
58 + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait,
59 + Ethan Sommer, Justin Levandoski.
62 +#include <linux/spinlock.h>
63 +#include <linux/version.h>
66 +#include <linux/module.h>
67 +#include <linux/skbuff.h>
68 +#include <linux/netfilter.h>
69 +#include <net/netfilter/nf_conntrack.h>
70 +#include <net/netfilter/nf_conntrack_core.h>
71 +#include <linux/netfilter/x_tables.h>
72 +#include <linux/netfilter/xt_layer7.h>
73 +#include <linux/ctype.h>
74 +#include <linux/proc_fs.h>
76 +#include "regexp/regexp.c"
78 +MODULE_LICENSE("GPL");
79 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
80 +MODULE_DESCRIPTION("iptables application layer match module");
81 +MODULE_ALIAS("ipt_layer7");
82 +MODULE_VERSION("2.17");
84 +static int maxdatalen = 2048; // this is the default
85 +module_param(maxdatalen, int, 0444);
86 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
87 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
88 + #define DPRINTK(format,args...) printk(format,##args)
90 + #define DPRINTK(format,args...)
93 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
94 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
96 +/* Number of packets whose data we look at.
97 +This can be modified through /proc/net/layer7_numpackets */
98 +static int num_packets = 10;
100 +static struct pattern_cache {
101 + char * regex_string;
103 + struct pattern_cache * next;
104 +} * first_pattern_cache = NULL;
106 +DEFINE_SPINLOCK(l7_lock);
108 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
109 +/* Converts an unfriendly string into a friendly one by
110 +replacing unprintables with periods and all whitespace with " ". */
111 +static char * friendly_print(unsigned char * s)
113 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
117 + if (net_ratelimit())
118 + printk(KERN_ERR "layer7: out of memory in "
119 + "friendly_print, bailing.\n");
123 + for(i = 0; i < strlen(s); i++){
124 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
125 + else if(isspace(s[i])) f[i] = ' ';
132 +static char dec2hex(int i)
139 + return (i - 10 + 'a');
142 + if (net_ratelimit())
143 + printk("layer7: Problem in dec2hex\n");
148 +static char * hex_print(unsigned char * s)
150 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
154 + if (net_ratelimit())
155 + printk(KERN_ERR "layer7: out of memory in hex_print, "
160 + for(i = 0; i < strlen(s); i++) {
161 + g[i*3 ] = dec2hex(s[i]/16);
162 + g[i*3 + 1] = dec2hex(s[i]%16);
171 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
172 +over again, it make sense to cache the results. */
173 +static regexp * compile_and_cache(const char * regex_string,
174 + const char * protocol)
176 + struct pattern_cache * node = first_pattern_cache;
177 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
178 + struct pattern_cache * tmp;
181 + while (node != NULL) {
182 + if (!strcmp(node->regex_string, regex_string))
183 + return node->pattern;
185 + last_pattern_cache = node;/* points at the last non-NULL node */
189 + /* If we reach the end of the list, then we have not yet cached
190 + the pattern for this regex. Let's do that now.
191 + Be paranoid about running out of memory to avoid list corruption. */
192 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
195 + if (net_ratelimit())
196 + printk(KERN_ERR "layer7: out of memory in "
197 + "compile_and_cache, bailing.\n");
201 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
202 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
205 + if(!tmp->regex_string || !tmp->pattern) {
206 + if (net_ratelimit())
207 + printk(KERN_ERR "layer7: out of memory in "
208 + "compile_and_cache, bailing.\n");
209 + kfree(tmp->regex_string);
210 + kfree(tmp->pattern);
215 + /* Ok. The new node is all ready now. */
218 + if(first_pattern_cache == NULL) /* list is empty */
219 + first_pattern_cache = node; /* make node the beginning */
221 + last_pattern_cache->next = node; /* attach node to the end */
223 + /* copy the string and compile the regex */
224 + len = strlen(regex_string);
225 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
226 + node->pattern = regcomp((char *)regex_string, &len);
227 + if ( !node->pattern ) {
228 + if (net_ratelimit())
229 + printk(KERN_ERR "layer7: Error compiling regexp "
231 + regex_string, protocol);
232 + /* pattern is now cached as NULL, so we won't try again. */
235 + strcpy(node->regex_string, regex_string);
236 + return node->pattern;
239 +static int can_handle(const struct sk_buff *skb)
241 + if(!ip_hdr(skb)) /* not IP */
243 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
244 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
245 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
250 +/* Returns offset the into the skb->data that the application data starts */
251 +static int app_data_offset(const struct sk_buff *skb)
253 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
254 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
255 + int ip_hl = 4*ip_hdr(skb)->ihl;
257 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
258 + /* 12 == offset into TCP header for the header length field.
259 + Can't get this with skb->h.th->doff because the tcphdr
260 + struct doesn't get set when routing (this is confirmed to be
261 + true in Netfilter as well as QoS.) */
262 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
264 + return ip_hl + tcp_hl;
265 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
266 + return ip_hl + 8; /* UDP header is always 8 bytes */
267 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
268 + return ip_hl + 8; /* ICMP header is 8 bytes */
270 + if (net_ratelimit())
271 + printk(KERN_ERR "layer7: tried to handle unknown "
273 + return ip_hl + 8; /* something reasonable */
277 +/* handles whether there's a match when we aren't appending data anymore */
278 +static int match_no_append(struct nf_conn * conntrack,
279 + struct nf_conn * master_conntrack,
280 + enum ip_conntrack_info ctinfo,
281 + enum ip_conntrack_info master_ctinfo,
282 + const struct xt_layer7_info * info)
284 + /* If we're in here, throw the app data away */
285 + if(master_conntrack->layer7.app_data != NULL) {
287 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
288 + if(!master_conntrack->layer7.app_proto) {
290 + friendly_print(master_conntrack->layer7.app_data);
292 + hex_print(master_conntrack->layer7.app_data);
293 + DPRINTK("\nl7-filter gave up after %d bytes "
294 + "(%d packets):\n%s\n",
295 + strlen(f), TOTAL_PACKETS, f);
297 + DPRINTK("In hex: %s\n", g);
302 + kfree(master_conntrack->layer7.app_data);
303 + master_conntrack->layer7.app_data = NULL; /* don't free again */
306 + if(master_conntrack->layer7.app_proto){
307 + /* Here child connections set their .app_proto (for /proc) */
308 + if(!conntrack->layer7.app_proto) {
309 + conntrack->layer7.app_proto =
310 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
312 + if(!conntrack->layer7.app_proto){
313 + if (net_ratelimit())
314 + printk(KERN_ERR "layer7: out of memory "
315 + "in match_no_append, "
319 + strcpy(conntrack->layer7.app_proto,
320 + master_conntrack->layer7.app_proto);
323 + return (!strcmp(master_conntrack->layer7.app_proto,
327 + /* If not classified, set to "unknown" to distinguish from
328 + connections that are still being tested. */
329 + master_conntrack->layer7.app_proto =
330 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
331 + if(!master_conntrack->layer7.app_proto){
332 + if (net_ratelimit())
333 + printk(KERN_ERR "layer7: out of memory in "
334 + "match_no_append, bailing.\n");
337 + strcpy(master_conntrack->layer7.app_proto, "unknown");
342 +/* add the new app data to the conntrack. Return number of bytes added. */
343 +static int add_data(struct nf_conn * master_conntrack,
344 + char * app_data, int appdatalen)
347 + int oldlength = master_conntrack->layer7.app_data_len;
349 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
350 + clear on whether the race condition exists or whether this really
351 + fixes it. I might just be being dense... Anyway, if it's not really
352 + a fix, all it does is waste a very small amount of time. */
353 + if(!master_conntrack->layer7.app_data) return 0;
355 + /* Strip nulls. Make everything lower case (our regex lib doesn't
356 + do case insensitivity). Add it to the end of the current data. */
357 + for(i = 0; i < maxdatalen-oldlength-1 &&
358 + i < appdatalen; i++) {
359 + if(app_data[i] != '\0') {
360 + /* the kernel version of tolower mungs 'upper ascii' */
361 + master_conntrack->layer7.app_data[length+oldlength] =
362 + isascii(app_data[i])?
363 + tolower(app_data[i]) : app_data[i];
368 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
369 + master_conntrack->layer7.app_data_len = length + oldlength;
374 +/* taken from drivers/video/modedb.c */
375 +static int my_atoi(const char *s)
382 + val = 10*val+(*s-'0');
390 +/* write out num_packets to userland. */
391 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
392 + int* eof, void * data)
394 + if(num_packets > 99 && net_ratelimit())
395 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
397 + page[0] = num_packets/10 + '0';
398 + page[1] = num_packets%10 + '0';
407 +/* Read in num_packets from userland */
408 +static int layer7_write_proc(struct file* file, const char* buffer,
409 + unsigned long count, void *data)
411 + char * foo = kmalloc(count, GFP_ATOMIC);
414 + if (net_ratelimit())
415 + printk(KERN_ERR "layer7: out of memory, bailing. "
416 + "num_packets unchanged.\n");
420 + if(copy_from_user(foo, buffer, count)) {
425 + num_packets = my_atoi(foo);
428 + /* This has an arbitrary limit to make the math easier. I'm lazy.
429 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
430 + if(num_packets > 99) {
431 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
433 + } else if(num_packets < 1) {
434 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
442 +match(const struct sk_buff *skbin,
443 + const struct net_device *in,
444 + const struct net_device *out,
445 + const struct xt_match *match,
446 + const void *matchinfo,
448 + unsigned int protoff,
451 + /* sidestep const without getting a compiler warning... */
452 + struct sk_buff * skb = (struct sk_buff *)skbin;
454 + const struct xt_layer7_info * info = matchinfo;
455 + enum ip_conntrack_info master_ctinfo, ctinfo;
456 + struct nf_conn *master_conntrack, *conntrack;
457 + unsigned char * app_data;
458 + unsigned int pattern_result, appdatalen;
459 + regexp * comppattern;
461 + /* Be paranoid/incompetent - lock the entire match function. */
462 + spin_lock_bh(&l7_lock);
464 + if(!can_handle(skb)){
465 + DPRINTK("layer7: This is some protocol I can't handle.\n");
466 + spin_unlock_bh(&l7_lock);
467 + return info->invert;
470 + /* Treat parent & all its children together as one connection, except
471 + for the purpose of setting conntrack->layer7.app_proto in the actual
472 + connection. This makes /proc/net/ip_conntrack more satisfying. */
473 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
474 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
475 + DPRINTK("layer7: couldn't get conntrack.\n");
476 + spin_unlock_bh(&l7_lock);
477 + return info->invert;
480 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
481 + while (master_ct(master_conntrack) != NULL)
482 + master_conntrack = master_ct(master_conntrack);
484 + /* if we've classified it or seen too many packets */
485 + if(TOTAL_PACKETS > num_packets ||
486 + master_conntrack->layer7.app_proto) {
488 + pattern_result = match_no_append(conntrack, master_conntrack,
489 + ctinfo, master_ctinfo, info);
491 + /* skb->cb[0] == seen. Don't do things twice if there are
492 + multiple l7 rules. I'm not sure that using cb for this purpose
493 + is correct, even though it says "put your private variables
494 + there". But it doesn't look like it is being used for anything
495 + else in the skbs that make it here. */
496 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
498 + spin_unlock_bh(&l7_lock);
499 + return (pattern_result ^ info->invert);
502 + if(skb_is_nonlinear(skb)){
503 + if(skb_linearize(skb) != 0){
504 + if (net_ratelimit())
505 + printk(KERN_ERR "layer7: failed to linearize "
506 + "packet, bailing.\n");
507 + spin_unlock_bh(&l7_lock);
508 + return info->invert;
512 + /* now that the skb is linearized, it's safe to set these. */
513 + app_data = skb->data + app_data_offset(skb);
514 + appdatalen = skb_tail_pointer(skb) - app_data;
516 + /* the return value gets checked later, when we're ready to use it */
517 + comppattern = compile_and_cache(info->pattern, info->protocol);
519 + /* On the first packet of a connection, allocate space for app data */
520 + if(TOTAL_PACKETS == 1 && !skb->cb[0] &&
521 + !master_conntrack->layer7.app_data){
522 + master_conntrack->layer7.app_data =
523 + kmalloc(maxdatalen, GFP_ATOMIC);
524 + if(!master_conntrack->layer7.app_data){
525 + if (net_ratelimit())
526 + printk(KERN_ERR "layer7: out of memory in "
527 + "match, bailing.\n");
528 + spin_unlock_bh(&l7_lock);
529 + return info->invert;
532 + master_conntrack->layer7.app_data[0] = '\0';
535 + /* Can be here, but unallocated, if numpackets is increased near
536 + the beginning of a connection */
537 + if(master_conntrack->layer7.app_data == NULL){
538 + spin_unlock_bh(&l7_lock);
539 + return (info->invert); /* unmatched */
544 + newbytes = add_data(master_conntrack, app_data, appdatalen);
546 + if(newbytes == 0) { /* didn't add any data */
548 + /* Didn't match before, not going to match now */
549 + spin_unlock_bh(&l7_lock);
550 + return info->invert;
554 + /* If looking for "unknown", then never match. "Unknown" means that
555 + we've given up; we're still trying with these packets. */
556 + if(!strcmp(info->protocol, "unknown")) {
557 + pattern_result = 0;
558 + /* If looking for "unset", then always match. "Unset" means that we
559 + haven't yet classified the connection. */
560 + } else if(!strcmp(info->protocol, "unset")) {
561 + pattern_result = 2;
562 + DPRINTK("layer7: matched unset: not yet classified "
563 + "(%d/%d packets)\n", TOTAL_PACKETS, num_packets);
564 + /* If the regexp failed to compile, don't bother running it */
565 + } else if(comppattern &&
566 + regexec(comppattern, master_conntrack->layer7.app_data)){
567 + DPRINTK("layer7: matched %s\n", info->protocol);
568 + pattern_result = 1;
569 + } else pattern_result = 0;
571 + if(pattern_result == 1) {
572 + master_conntrack->layer7.app_proto =
573 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
574 + if(!master_conntrack->layer7.app_proto){
575 + if (net_ratelimit())
576 + printk(KERN_ERR "layer7: out of memory in "
577 + "match, bailing.\n");
578 + spin_unlock_bh(&l7_lock);
579 + return (pattern_result ^ info->invert);
581 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
582 + } else if(pattern_result > 1) { /* cleanup from "unset" */
583 + pattern_result = 1;
586 + /* mark the packet seen */
589 + spin_unlock_bh(&l7_lock);
590 + return (pattern_result ^ info->invert);
593 +static bool check(const char *tablename,
595 + const struct xt_match *match,
597 + unsigned int hook_mask)
600 + // load nf_conntrack_ipv4
601 + if (nf_ct_l3proto_try_module_get(match->family) < 0) {
602 + printk(KERN_WARNING "can't load conntrack support for "
603 + "proto=%d\n", match->family);
610 +destroy(const struct xt_match *match, void *matchinfo)
612 + nf_ct_l3proto_module_put(match->family);
615 +static struct xt_match xt_layer7_match[] = {
619 + .checkentry = check,
621 + .destroy = destroy,
622 + .matchsize = sizeof(struct xt_layer7_info),
627 +static void layer7_cleanup_proc(void)
629 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
630 + remove_proc_entry("layer7_numpackets", proc_net);
632 + remove_proc_entry("layer7_numpackets", init_net.proc_net);
636 +/* register the proc file */
637 +static void layer7_init_proc(void)
639 + struct proc_dir_entry* entry;
640 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
641 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
643 + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
645 + entry->read_proc = layer7_read_proc;
646 + entry->write_proc = layer7_write_proc;
649 +static int __init xt_layer7_init(void)
653 + layer7_init_proc();
654 + if(maxdatalen < 1) {
655 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
659 + /* This is not a hard limit. It's just here to prevent people from
660 + bringing their slow machines to a grinding halt. */
661 + else if(maxdatalen > 65536) {
662 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
664 + maxdatalen = 65536;
666 + return xt_register_matches(xt_layer7_match,
667 + ARRAY_SIZE(xt_layer7_match));
670 +static void __exit xt_layer7_fini(void)
672 + layer7_cleanup_proc();
673 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
676 +module_init(xt_layer7_init);
677 +module_exit(xt_layer7_fini);
678 --- linux-2.6.24-rc5/net/netfilter/regexp/regexp.c 1969-12-31 18:00:00.000000000 -0600
679 +++ linux-2.6.24-rc5-layer7/net/netfilter/regexp/regexp.c 2007-12-11 23:35:12.000000000 -0600
682 + * regcomp and regexec -- regsub and regerror are elsewhere
683 + * @(#)regexp.c 1.3 of 18 April 87
685 + * Copyright (c) 1986 by University of Toronto.
686 + * Written by Henry Spencer. Not derived from licensed software.
688 + * Permission is granted to anyone to use this software for any
689 + * purpose on any computer system, and to redistribute it freely,
690 + * subject to the following restrictions:
692 + * 1. The author is not responsible for the consequences of use of
693 + * this software, no matter how awful, even if they arise
694 + * from defects in it.
696 + * 2. The origin of this software must not be misrepresented, either
697 + * by explicit claim or by omission.
699 + * 3. Altered versions must be plainly marked as such, and must not
700 + * be misrepresented as being the original software.
702 + * Beware that some of this code is subtly aware of the way operator
703 + * precedence is structured in regular expressions. Serious changes in
704 + * regular-expression syntax might require a total rethink.
706 + * This code was modified by Ethan Sommer to work within the kernel
707 + * (it now uses kmalloc etc..)
709 + * Modified slightly by Matthew Strait to use more modern C.
713 +#include "regmagic.h"
715 +/* added by ethan and matt. Lets it work in both kernel and user space.
716 +(So iptables can use it, for instance.) Yea, it goes both ways... */
718 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
720 + #define printk(format,args...) printf(format,##args)
723 +void regerror(char * s)
725 + printk("<3>Regexp: %s\n", s);
730 + * The "internal use only" fields in regexp.h are present to pass info from
731 + * compile to execute that permits the execute phase to run lots faster on
732 + * simple cases. They are:
734 + * regstart char that must begin a match; '\0' if none obvious
735 + * reganch is the match anchored (at beginning-of-line only)?
736 + * regmust string (pointer into program) that match must include, or NULL
737 + * regmlen length of regmust string
739 + * Regstart and reganch permit very fast decisions on suitable starting points
740 + * for a match, cutting down the work a lot. Regmust permits fast rejection
741 + * of lines that cannot possibly match. The regmust tests are costly enough
742 + * that regcomp() supplies a regmust only if the r.e. contains something
743 + * potentially expensive (at present, the only such thing detected is * or +
744 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
745 + * supplied because the test in regexec() needs it and regcomp() is computing
750 + * Structure for regexp "program". This is essentially a linear encoding
751 + * of a nondeterministic finite-state machine (aka syntax charts or
752 + * "railroad normal form" in parsing technology). Each node is an opcode
753 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
754 + * all nodes except BRANCH implement concatenation; a "next" pointer with
755 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
756 + * have one of the subtle syntax dependencies: an individual BRANCH (as
757 + * opposed to a collection of them) is never concatenated with anything
758 + * because of operator precedence.) The operand of some types of node is
759 + * a literal string; for others, it is a node leading into a sub-FSM. In
760 + * particular, the operand of a BRANCH node is the first node of the branch.
761 + * (NB this is *not* a tree structure: the tail of the branch connects
762 + * to the thing following the set of BRANCHes.) The opcodes are:
765 +/* definition number opnd? meaning */
766 +#define END 0 /* no End of program. */
767 +#define BOL 1 /* no Match "" at beginning of line. */
768 +#define EOL 2 /* no Match "" at end of line. */
769 +#define ANY 3 /* no Match any one character. */
770 +#define ANYOF 4 /* str Match any character in this string. */
771 +#define ANYBUT 5 /* str Match any character not in this string. */
772 +#define BRANCH 6 /* node Match this alternative, or the next... */
773 +#define BACK 7 /* no Match "", "next" ptr points backward. */
774 +#define EXACTLY 8 /* str Match this string. */
775 +#define NOTHING 9 /* no Match empty string. */
776 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
777 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
778 +#define OPEN 20 /* no Mark this point in input as start of #n. */
779 + /* OPEN+1 is number 1, etc. */
780 +#define CLOSE 30 /* no Analogous to OPEN. */
785 + * BRANCH The set of branches constituting a single choice are hooked
786 + * together with their "next" pointers, since precedence prevents
787 + * anything being concatenated to any individual branch. The
788 + * "next" pointer of the last BRANCH in a choice points to the
789 + * thing following the whole choice. This is also where the
790 + * final "next" pointer of each individual branch points; each
791 + * branch starts with the operand node of a BRANCH node.
793 + * BACK Normal "next" pointers all implicitly point forward; BACK
794 + * exists to make loop structures possible.
796 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
797 + * BRANCH structures using BACK. Simple cases (one character
798 + * per match) are implemented with STAR and PLUS for speed
799 + * and to minimize recursive plunges.
801 + * OPEN,CLOSE ...are numbered at compile time.
805 + * A node is one char of opcode followed by two chars of "next" pointer.
806 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
807 + * value is a positive offset from the opcode of the node containing it.
808 + * An operand, if any, simply follows the node. (Note that much of the
809 + * code generation knows about this implicit relationship.)
811 + * Using two bytes for the "next" pointer is vast overkill for most things,
812 + * but allows patterns to get big without disasters.
814 +#define OP(p) (*(p))
815 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
816 +#define OPERAND(p) ((p) + 3)
819 + * See regmagic.h for one further detail of program structure.
824 + * Utility definitions.
827 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
829 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
832 +#define FAIL(m) { regerror(m); return(NULL); }
833 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
834 +#define META "^$.[()|?+*\\"
837 + * Flags to be passed up and down.
839 +#define HASWIDTH 01 /* Known never to match null string. */
840 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
841 +#define SPSTART 04 /* Starts with * or +. */
842 +#define WORST 0 /* Worst case. */
845 + * Global work variables for regcomp().
847 +struct match_globals {
848 +char *reginput; /* String-input pointer. */
849 +char *regbol; /* Beginning of input, for ^ check. */
850 +char **regstartp; /* Pointer to startp array. */
851 +char **regendp; /* Ditto for endp. */
852 +char *regparse; /* Input-scan pointer. */
853 +int regnpar; /* () count. */
855 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
856 +long regsize; /* Code size. */
860 + * Forward declarations for regcomp()'s friends.
863 +#define STATIC static
865 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
866 +STATIC char *regbranch(struct match_globals *g, int *flagp);
867 +STATIC char *regpiece(struct match_globals *g, int *flagp);
868 +STATIC char *regatom(struct match_globals *g, int *flagp);
869 +STATIC char *regnode(struct match_globals *g, char op);
870 +STATIC char *regnext(struct match_globals *g, char *p);
871 +STATIC void regc(struct match_globals *g, char b);
872 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
873 +STATIC void regtail(struct match_globals *g, char *p, char *val);
874 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
877 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
884 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
885 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
886 + if (*scan1 == *scan2++)
894 + - regcomp - compile a regular expression into internal code
896 + * We can't allocate space until we know how big the compiled form will be,
897 + * but we can't compile it (and thus know how big it is) until we've got a
898 + * place to put the code. So we cheat: we compile it twice, once with code
899 + * generation turned off and size counting turned on, and once "for real".
900 + * This also means that we don't allocate space until we are sure that the
901 + * thing really will compile successfully, and we never have to move the
902 + * code and thus invalidate pointers into it. (Note that it has to be in
903 + * one piece because free() must be able to free it all.)
905 + * Beware that the optimization-preparation code in here knows about some
906 + * of the structure of the compiled regexp.
909 +regcomp(char *exp,int *patternsize)
911 + register regexp *r;
912 + register char *scan;
913 + register char *longest;
916 + struct match_globals g;
918 + /* commented out by ethan
919 + extern char *malloc();
923 + FAIL("NULL argument");
925 + /* First pass: determine size, legality. */
929 + g.regcode = &g.regdummy;
931 + if (reg(&g, 0, &flags) == NULL)
934 + /* Small enough for pointer-storage convention? */
935 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
936 + FAIL("regexp too big");
938 + /* Allocate space. */
939 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
940 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
942 + FAIL("out of space");
944 + /* Second pass: emit code. */
947 + g.regcode = r->program;
949 + if (reg(&g, 0, &flags) == NULL)
952 + /* Dig out information for optimizations. */
953 + r->regstart = '\0'; /* Worst-case defaults. */
957 + scan = r->program+1; /* First BRANCH. */
958 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
959 + scan = OPERAND(scan);
961 + /* Starting-point info. */
962 + if (OP(scan) == EXACTLY)
963 + r->regstart = *OPERAND(scan);
964 + else if (OP(scan) == BOL)
968 + * If there's something expensive in the r.e., find the
969 + * longest literal string that must appear and make it the
970 + * regmust. Resolve ties in favor of later strings, since
971 + * the regstart check works with the beginning of the r.e.
972 + * and avoiding duplication strengthens checking. Not a
973 + * strong reason, but sufficient in the absence of others.
975 + if (flags&SPSTART) {
978 + for (; scan != NULL; scan = regnext(&g, scan))
979 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
980 + longest = OPERAND(scan);
981 + len = strlen(OPERAND(scan));
983 + r->regmust = longest;
992 + - reg - regular expression, i.e. main body or parenthesized thing
994 + * Caller must absorb opening parenthesis.
996 + * Combining parenthesis handling with the base level of regular expression
997 + * is a trifle forced, but the need to tie the tails of the branches to what
998 + * follows makes it hard to avoid.
1001 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1003 + register char *ret;
1004 + register char *br;
1005 + register char *ender;
1006 + register int parno = 0; /* 0 makes gcc happy */
1009 + *flagp = HASWIDTH; /* Tentatively. */
1011 + /* Make an OPEN node, if parenthesized. */
1013 + if (g->regnpar >= NSUBEXP)
1014 + FAIL("too many ()");
1015 + parno = g->regnpar;
1017 + ret = regnode(g, OPEN+parno);
1021 + /* Pick up the branches, linking them together. */
1022 + br = regbranch(g, &flags);
1026 + regtail(g, ret, br); /* OPEN -> first. */
1029 + if (!(flags&HASWIDTH))
1030 + *flagp &= ~HASWIDTH;
1031 + *flagp |= flags&SPSTART;
1032 + while (*g->regparse == '|') {
1034 + br = regbranch(g, &flags);
1037 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1038 + if (!(flags&HASWIDTH))
1039 + *flagp &= ~HASWIDTH;
1040 + *flagp |= flags&SPSTART;
1043 + /* Make a closing node, and hook it on the end. */
1044 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1045 + regtail(g, ret, ender);
1047 + /* Hook the tails of the branches to the closing node. */
1048 + for (br = ret; br != NULL; br = regnext(g, br))
1049 + regoptail(g, br, ender);
1051 + /* Check for proper termination. */
1052 + if (paren && *g->regparse++ != ')') {
1053 + FAIL("unmatched ()");
1054 + } else if (!paren && *g->regparse != '\0') {
1055 + if (*g->regparse == ')') {
1056 + FAIL("unmatched ()");
1058 + FAIL("junk on end"); /* "Can't happen". */
1066 + - regbranch - one alternative of an | operator
1068 + * Implements the concatenation operator.
1071 +regbranch(struct match_globals *g, int *flagp)
1073 + register char *ret;
1074 + register char *chain;
1075 + register char *latest;
1078 + *flagp = WORST; /* Tentatively. */
1080 + ret = regnode(g, BRANCH);
1082 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1083 + latest = regpiece(g, &flags);
1084 + if (latest == NULL)
1086 + *flagp |= flags&HASWIDTH;
1087 + if (chain == NULL) /* First piece. */
1088 + *flagp |= flags&SPSTART;
1090 + regtail(g, chain, latest);
1093 + if (chain == NULL) /* Loop ran zero times. */
1094 + (void) regnode(g, NOTHING);
1100 + - regpiece - something followed by possible [*+?]
1102 + * Note that the branching code sequences used for ? and the general cases
1103 + * of * and + are somewhat optimized: they use the same NOTHING node as
1104 + * both the endmarker for their branch list and the body of the last branch.
1105 + * It might seem that this node could be dispensed with entirely, but the
1106 + * endmarker role is not redundant.
1109 +regpiece(struct match_globals *g, int *flagp)
1111 + register char *ret;
1113 + register char *next;
1116 + ret = regatom(g, &flags);
1120 + op = *g->regparse;
1121 + if (!ISMULT(op)) {
1126 + if (!(flags&HASWIDTH) && op != '?')
1127 + FAIL("*+ operand could be empty");
1128 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1130 + if (op == '*' && (flags&SIMPLE))
1131 + reginsert(g, STAR, ret);
1132 + else if (op == '*') {
1133 + /* Emit x* as (x&|), where & means "self". */
1134 + reginsert(g, BRANCH, ret); /* Either x */
1135 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1136 + regoptail(g, ret, ret); /* back */
1137 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1138 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1139 + } else if (op == '+' && (flags&SIMPLE))
1140 + reginsert(g, PLUS, ret);
1141 + else if (op == '+') {
1142 + /* Emit x+ as x(&|), where & means "self". */
1143 + next = regnode(g, BRANCH); /* Either */
1144 + regtail(g, ret, next);
1145 + regtail(g, regnode(g, BACK), ret); /* loop back */
1146 + regtail(g, next, regnode(g, BRANCH)); /* or */
1147 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1148 + } else if (op == '?') {
1149 + /* Emit x? as (x|) */
1150 + reginsert(g, BRANCH, ret); /* Either x */
1151 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1152 + next = regnode(g, NOTHING); /* null. */
1153 + regtail(g, ret, next);
1154 + regoptail(g, ret, next);
1157 + if (ISMULT(*g->regparse))
1158 + FAIL("nested *?+");
1164 + - regatom - the lowest level
1166 + * Optimization: gobbles an entire sequence of ordinary characters so that
1167 + * it can turn them into a single node, which is smaller to store and
1168 + * faster to run. Backslashed characters are exceptions, each becoming a
1169 + * separate node; the code is simpler that way and it's not worth fixing.
1172 +regatom(struct match_globals *g, int *flagp)
1174 + register char *ret;
1177 + *flagp = WORST; /* Tentatively. */
1179 + switch (*g->regparse++) {
1181 + ret = regnode(g, BOL);
1184 + ret = regnode(g, EOL);
1187 + ret = regnode(g, ANY);
1188 + *flagp |= HASWIDTH|SIMPLE;
1191 + register int class;
1192 + register int classend;
1194 + if (*g->regparse == '^') { /* Complement of range. */
1195 + ret = regnode(g, ANYBUT);
1198 + ret = regnode(g, ANYOF);
1199 + if (*g->regparse == ']' || *g->regparse == '-')
1200 + regc(g, *g->regparse++);
1201 + while (*g->regparse != '\0' && *g->regparse != ']') {
1202 + if (*g->regparse == '-') {
1204 + if (*g->regparse == ']' || *g->regparse == '\0')
1207 + class = UCHARAT(g->regparse-2)+1;
1208 + classend = UCHARAT(g->regparse);
1209 + if (class > classend+1)
1210 + FAIL("invalid [] range");
1211 + for (; class <= classend; class++)
1216 + regc(g, *g->regparse++);
1219 + if (*g->regparse != ']')
1220 + FAIL("unmatched []");
1222 + *flagp |= HASWIDTH|SIMPLE;
1226 + ret = reg(g, 1, &flags);
1229 + *flagp |= flags&(HASWIDTH|SPSTART);
1234 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1239 + FAIL("?+* follows nothing");
1242 + if (*g->regparse == '\0')
1243 + FAIL("trailing \\");
1244 + ret = regnode(g, EXACTLY);
1245 + regc(g, *g->regparse++);
1247 + *flagp |= HASWIDTH|SIMPLE;
1251 + register char ender;
1254 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1256 + FAIL("internal disaster");
1257 + ender = *(g->regparse+len);
1258 + if (len > 1 && ISMULT(ender))
1259 + len--; /* Back off clear of ?+* operand. */
1260 + *flagp |= HASWIDTH;
1263 + ret = regnode(g, EXACTLY);
1265 + regc(g, *g->regparse++);
1277 + - regnode - emit a node
1279 +static char * /* Location. */
1280 +regnode(struct match_globals *g, char op)
1282 + register char *ret;
1283 + register char *ptr;
1286 + if (ret == &g->regdummy) {
1293 + *ptr++ = '\0'; /* Null "next" pointer. */
1301 + - regc - emit (if appropriate) a byte of code
1304 +regc(struct match_globals *g, char b)
1306 + if (g->regcode != &g->regdummy)
1307 + *g->regcode++ = b;
1313 + - reginsert - insert an operator in front of already-emitted operand
1315 + * Means relocating the operand.
1318 +reginsert(struct match_globals *g, char op, char* opnd)
1320 + register char *src;
1321 + register char *dst;
1322 + register char *place;
1324 + if (g->regcode == &g->regdummy) {
1332 + while (src > opnd)
1335 + place = opnd; /* Op node, where operand used to be. */
1342 + - regtail - set the next-pointer at the end of a node chain
1345 +regtail(struct match_globals *g, char *p, char *val)
1347 + register char *scan;
1348 + register char *temp;
1349 + register int offset;
1351 + if (p == &g->regdummy)
1354 + /* Find last node. */
1357 + temp = regnext(g, scan);
1363 + if (OP(scan) == BACK)
1364 + offset = scan - val;
1366 + offset = val - scan;
1367 + *(scan+1) = (offset>>8)&0377;
1368 + *(scan+2) = offset&0377;
1372 + - regoptail - regtail on operand of first argument; nop if operandless
1375 +regoptail(struct match_globals *g, char *p, char *val)
1377 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1378 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1380 + regtail(g, OPERAND(p), val);
1384 + * regexec and friends
1391 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1392 +STATIC int regmatch(struct match_globals *g, char *prog);
1393 +STATIC int regrepeat(struct match_globals *g, char *p);
1396 +int regnarrate = 0;
1398 +STATIC char *regprop(char *op);
1402 + - regexec - match a regexp against a string
1405 +regexec(regexp *prog, char *string)
1408 + struct match_globals g;
1410 + /* Be paranoid... */
1411 + if (prog == NULL || string == NULL) {
1412 + printk("<3>Regexp: NULL parameter\n");
1416 + /* Check validity of program. */
1417 + if (UCHARAT(prog->program) != MAGIC) {
1418 + printk("<3>Regexp: corrupted program\n");
1422 + /* If there is a "must appear" string, look for it. */
1423 + if (prog->regmust != NULL) {
1425 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1426 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1427 + break; /* Found it. */
1430 + if (s == NULL) /* Not present. */
1434 + /* Mark beginning of line for ^ . */
1435 + g.regbol = string;
1437 + /* Simplest case: anchored match need be tried only once. */
1438 + if (prog->reganch)
1439 + return(regtry(&g, prog, string));
1441 + /* Messy cases: unanchored match. */
1443 + if (prog->regstart != '\0')
1444 + /* We know what char it must start with. */
1445 + while ((s = strchr(s, prog->regstart)) != NULL) {
1446 + if (regtry(&g, prog, s))
1451 + /* We don't -- general case. */
1453 + if (regtry(&g, prog, s))
1455 + } while (*s++ != '\0');
1462 + - regtry - try match at specific point
1464 +static int /* 0 failure, 1 success */
1465 +regtry(struct match_globals *g, regexp *prog, char *string)
1468 + register char **sp;
1469 + register char **ep;
1471 + g->reginput = string;
1472 + g->regstartp = prog->startp;
1473 + g->regendp = prog->endp;
1475 + sp = prog->startp;
1477 + for (i = NSUBEXP; i > 0; i--) {
1481 + if (regmatch(g, prog->program + 1)) {
1482 + prog->startp[0] = string;
1483 + prog->endp[0] = g->reginput;
1490 + - regmatch - main matching routine
1492 + * Conceptually the strategy is simple: check to see whether the current
1493 + * node matches, call self recursively to see whether the rest matches,
1494 + * and then act accordingly. In practice we make some effort to avoid
1495 + * recursion, in particular by going through "ordinary" nodes (that don't
1496 + * need to know whether the rest of the match failed) by a loop instead of
1499 +static int /* 0 failure, 1 success */
1500 +regmatch(struct match_globals *g, char *prog)
1502 + register char *scan = prog; /* Current node. */
1503 + char *next; /* Next node. */
1506 + if (scan != NULL && regnarrate)
1507 + fprintf(stderr, "%s(\n", regprop(scan));
1509 + while (scan != NULL) {
1512 + fprintf(stderr, "%s...\n", regprop(scan));
1514 + next = regnext(g, scan);
1516 + switch (OP(scan)) {
1518 + if (g->reginput != g->regbol)
1522 + if (*g->reginput != '\0')
1526 + if (*g->reginput == '\0')
1532 + register char *opnd;
1534 + opnd = OPERAND(scan);
1535 + /* Inline the first character, for speed. */
1536 + if (*opnd != *g->reginput)
1538 + len = strlen(opnd);
1539 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1541 + g->reginput += len;
1545 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1550 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1567 + register char *save;
1569 + no = OP(scan) - OPEN;
1570 + save = g->reginput;
1572 + if (regmatch(g, next)) {
1574 + * Don't set startp if some later
1575 + * invocation of the same parentheses
1578 + if (g->regstartp[no] == NULL)
1579 + g->regstartp[no] = save;
1596 + register char *save;
1598 + no = OP(scan) - CLOSE;
1599 + save = g->reginput;
1601 + if (regmatch(g, next)) {
1603 + * Don't set endp if some later
1604 + * invocation of the same parentheses
1607 + if (g->regendp[no] == NULL)
1608 + g->regendp[no] = save;
1615 + register char *save;
1617 + if (OP(next) != BRANCH) /* No choice. */
1618 + next = OPERAND(scan); /* Avoid recursion. */
1621 + save = g->reginput;
1622 + if (regmatch(g, OPERAND(scan)))
1624 + g->reginput = save;
1625 + scan = regnext(g, scan);
1626 + } while (scan != NULL && OP(scan) == BRANCH);
1634 + register char nextch;
1636 + register char *save;
1640 + * Lookahead to avoid useless match attempts
1641 + * when we know what character comes next.
1644 + if (OP(next) == EXACTLY)
1645 + nextch = *OPERAND(next);
1646 + min = (OP(scan) == STAR) ? 0 : 1;
1647 + save = g->reginput;
1648 + no = regrepeat(g, OPERAND(scan));
1649 + while (no >= min) {
1650 + /* If it could work, try it. */
1651 + if (nextch == '\0' || *g->reginput == nextch)
1652 + if (regmatch(g, next))
1654 + /* Couldn't or didn't -- back up. */
1656 + g->reginput = save + no;
1662 + return(1); /* Success! */
1665 + printk("<3>Regexp: memory corruption\n");
1674 + * We get here only if there's trouble -- normally "case END" is
1675 + * the terminating point.
1677 + printk("<3>Regexp: corrupted pointers\n");
1682 + - regrepeat - repeatedly match something simple, report how many
1685 +regrepeat(struct match_globals *g, char *p)
1687 + register int count = 0;
1688 + register char *scan;
1689 + register char *opnd;
1691 + scan = g->reginput;
1692 + opnd = OPERAND(p);
1695 + count = strlen(scan);
1699 + while (*opnd == *scan) {
1705 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1711 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1716 + default: /* Oh dear. Called inappropriately. */
1717 + printk("<3>Regexp: internal foulup\n");
1718 + count = 0; /* Best compromise. */
1721 + g->reginput = scan;
1727 + - regnext - dig the "next" pointer out of a node
1730 +regnext(struct match_globals *g, char *p)
1732 + register int offset;
1734 + if (p == &g->regdummy)
1741 + if (OP(p) == BACK)
1749 +STATIC char *regprop();
1752 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1758 + register char op = EXACTLY; /* Arbitrary non-END op. */
1759 + register char *next;
1760 + /* extern char *strchr(); */
1763 + s = r->program + 1;
1764 + while (op != END) { /* While that wasn't END last time... */
1766 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1767 + next = regnext(s);
1768 + if (next == NULL) /* Next ptr. */
1771 + printf("(%d)", (s-r->program)+(next-s));
1773 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1774 + /* Literal string, where present. */
1775 + while (*s != '\0') {
1784 + /* Header fields of interest. */
1785 + if (r->regstart != '\0')
1786 + printf("start `%c' ", r->regstart);
1788 + printf("anchored ");
1789 + if (r->regmust != NULL)
1790 + printf("must have \"%s\"", r->regmust);
1795 + - regprop - printable representation of opcode
1802 + static char buf[BUFLEN];
1846 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1858 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1868 + printk("<3>Regexp: corrupted opcode\n");
1872 + strncat(buf, p, BUFLEN-strlen(buf));
1878 --- linux-2.6.24-rc5/net/netfilter/regexp/regexp.h 1969-12-31 18:00:00.000000000 -0600
1879 +++ linux-2.6.24-rc5-layer7/net/netfilter/regexp/regexp.h 2007-12-11 23:35:12.000000000 -0600
1882 + * Definitions etc. for regexp(3) routines.
1884 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1885 + * not the System V one.
1893 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1894 +which contains a version of this library, says:
1897 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1898 + * will not work properly.
1901 +However, it looks rather like this library is limited to 10. If you think
1902 +otherwise, let us know.
1906 +typedef struct regexp {
1907 + char *startp[NSUBEXP];
1908 + char *endp[NSUBEXP];
1909 + char regstart; /* Internal use only. */
1910 + char reganch; /* Internal use only. */
1911 + char *regmust; /* Internal use only. */
1912 + int regmlen; /* Internal use only. */
1913 + char program[1]; /* Unwarranted chumminess with compiler. */
1916 +regexp * regcomp(char *exp, int *patternsize);
1917 +int regexec(regexp *prog, char *string);
1918 +void regsub(regexp *prog, char *source, char *dest);
1919 +void regerror(char *s);
1922 --- linux-2.6.24-rc5/net/netfilter/regexp/regmagic.h 1969-12-31 18:00:00.000000000 -0600
1923 +++ linux-2.6.24-rc5-layer7/net/netfilter/regexp/regmagic.h 2007-12-11 23:35:12.000000000 -0600
1926 + * The first byte of the regexp internal "program" is actually this magic
1927 + * number; the start node begins in the second byte.
1930 --- linux-2.6.24-rc5/net/netfilter/regexp/regsub.c 1969-12-31 18:00:00.000000000 -0600
1931 +++ linux-2.6.24-rc5-layer7/net/netfilter/regexp/regsub.c 2007-12-11 23:35:12.000000000 -0600
1935 + * @(#)regsub.c 1.3 of 2 April 86
1937 + * Copyright (c) 1986 by University of Toronto.
1938 + * Written by Henry Spencer. Not derived from licensed software.
1940 + * Permission is granted to anyone to use this software for any
1941 + * purpose on any computer system, and to redistribute it freely,
1942 + * subject to the following restrictions:
1944 + * 1. The author is not responsible for the consequences of use of
1945 + * this software, no matter how awful, even if they arise
1946 + * from defects in it.
1948 + * 2. The origin of this software must not be misrepresented, either
1949 + * by explicit claim or by omission.
1951 + * 3. Altered versions must be plainly marked as such, and must not
1952 + * be misrepresented as being the original software.
1955 + * This code was modified by Ethan Sommer to work within the kernel
1956 + * (it now uses kmalloc etc..)
1959 +#include "regexp.h"
1960 +#include "regmagic.h"
1961 +#include <linux/string.h>
1965 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1967 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1971 +//void regerror(char * s)
1973 +// printk("regexp(3): %s", s);
1974 +// /* NOTREACHED */
1979 + - regsub - perform substitutions after a regexp match
1982 +regsub(regexp * prog, char * source, char * dest)
1984 + register char *src;
1985 + register char *dst;
1990 + /* Not necessary and gcc doesn't like it -MLS */
1991 + /*extern char *strncpy();*/
1993 + if (prog == NULL || source == NULL || dest == NULL) {
1994 + regerror("NULL parm to regsub");
1997 + if (UCHARAT(prog->program) != MAGIC) {
1998 + regerror("damaged regexp fed to regsub");
2004 + while ((c = *src++) != '\0') {
2007 + else if (c == '\\' && '0' <= *src && *src <= '9')
2008 + no = *src++ - '0';
2012 + if (no < 0) { /* Ordinary character. */
2013 + if (c == '\\' && (*src == '\\' || *src == '&'))
2016 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2017 + len = prog->endp[no] - prog->startp[no];
2018 + (void) strncpy(dst, prog->startp[no], len);
2020 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2021 + regerror("damaged match string");
2028 --- linux-2.6.24-rc5/net/netfilter/nf_conntrack_core.c 2007-12-11 23:43:21.000000000 -0600
2029 +++ linux-2.6.24-rc5-layer7/net/netfilter/nf_conntrack_core.c 2007-12-11 23:35:12.000000000 -0600
2030 @@ -206,6 +206,14 @@ destroy_conntrack(struct nf_conntrack *n
2032 nf_ct_remove_expectations(ct);
2034 + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2035 + if(ct->layer7.app_proto)
2036 + kfree(ct->layer7.app_proto);
2037 + if(ct->layer7.app_data)
2038 + kfree(ct->layer7.app_data);
2042 /* We overload first tuple to link into unconfirmed list. */
2043 if (!nf_ct_is_confirmed(ct)) {
2044 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
2045 --- linux-2.6.24-rc5/net/netfilter/nf_conntrack_standalone.c 2007-12-11 23:43:22.000000000 -0600
2046 +++ linux-2.6.24-rc5-layer7/net/netfilter/nf_conntrack_standalone.c 2007-12-11 23:35:12.000000000 -0600
2047 @@ -180,7 +180,12 @@ static int ct_seq_show(struct seq_file *
2051 - if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
2052 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2053 + if(conntrack->layer7.app_proto)
2054 + if(seq_printf(s, "l7proto=%s ", conntrack->layer7.app_proto))
2057 + if (seq_printf(s, "asdfuse=%u\n", atomic_read(&conntrack->ct_general.use)))
2061 --- linux-2.6.24-rc5/include/net/netfilter/nf_conntrack.h 2007-12-11 23:43:16.000000000 -0600
2062 +++ linux-2.6.24-rc5-layer7/include/net/netfilter/nf_conntrack.h 2007-12-11 23:35:12.000000000 -0600
2063 @@ -124,6 +124,22 @@ struct nf_conn
2067 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
2068 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
2071 + * e.g. "http". NULL before decision. "unknown" after decision
2076 + * application layer data so far. NULL after match decision.
2079 + unsigned int app_data_len;
2083 /* Storage reserved for other modules: */
2084 union nf_conntrack_proto proto;
2086 --- linux-2.6.24-rc5/include/linux/netfilter/xt_layer7.h 1969-12-31 18:00:00.000000000 -0600
2087 +++ linux-2.6.24-rc5-layer7/include/linux/netfilter/xt_layer7.h 2007-12-11 23:35:12.000000000 -0600
2089 +#ifndef _XT_LAYER7_H
2090 +#define _XT_LAYER7_H
2092 +#define MAX_PATTERN_LEN 8192
2093 +#define MAX_PROTOCOL_LEN 256
2095 +struct xt_layer7_info {
2096 + char protocol[MAX_PROTOCOL_LEN];
2097 + char pattern[MAX_PATTERN_LEN];
2101 +#endif /* _XT_LAYER7_H */