fix a typo in the imq module (patch from #2005)
[openwrt.git] / target / linux / generic-2.6 / patches / 100-netfilter_layer7_2.9.patch
1 Index: linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h
2 ===================================================================
3 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h    2007-07-02 00:43:58.271086750 +0200
5 @@ -0,0 +1,26 @@
6 +/*
7 +  By Matthew Strait <quadong@users.sf.net>, Dec 2003.
8 +  http://l7-filter.sf.net
9 +
10 +  This program is free software; you can redistribute it and/or
11 +  modify it under the terms of the GNU General Public License
12 +  as published by the Free Software Foundation; either version
13 +  2 of the License, or (at your option) any later version.
14 +  http://www.gnu.org/licenses/gpl.txt
15 +*/
16 +
17 +#ifndef _IPT_LAYER7_H
18 +#define _IPT_LAYER7_H
19 +
20 +#define MAX_PATTERN_LEN 8192
21 +#define MAX_PROTOCOL_LEN 256
22 +
23 +typedef char *(*proc_ipt_search) (char *, char, char *);
24 +
25 +struct ipt_layer7_info {
26 +    char protocol[MAX_PROTOCOL_LEN];
27 +    char invert:1;
28 +    char pattern[MAX_PATTERN_LEN];
29 +};
30 +
31 +#endif /* _IPT_LAYER7_H */
32 Index: linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_core.c
33 ===================================================================
34 --- linux-2.6.21.5.orig/net/ipv4/netfilter/ip_conntrack_core.c  2007-07-02 00:37:53.432285750 +0200
35 +++ linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_core.c       2007-07-02 00:37:55.496414750 +0200
36 @@ -332,6 +332,13 @@
37          * too. */
38         ip_ct_remove_expectations(ct);
39  
40 +       #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
41 +       if(ct->layer7.app_proto)
42 +               kfree(ct->layer7.app_proto);
43 +       if(ct->layer7.app_data)
44 +               kfree(ct->layer7.app_data);
45 +       #endif
46 +
47         /* We overload first tuple to link into unconfirmed list. */
48         if (!is_confirmed(ct)) {
49                 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
50 Index: linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_standalone.c
51 ===================================================================
52 --- linux-2.6.21.5.orig/net/ipv4/netfilter/ip_conntrack_standalone.c    2007-07-02 00:37:53.440286250 +0200
53 +++ linux-2.6.21.5/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-07-02 00:37:55.544417750 +0200
54 @@ -188,6 +188,12 @@
55                 return -ENOSPC;
56  #endif
57  
58 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
59 +       if(conntrack->layer7.app_proto)
60 +               if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
61 +                       return 1;
62 +#endif
63 +
64         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
65                 return -ENOSPC;
66  
67 Index: linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c
68 ===================================================================
69 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
70 +++ linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c      2007-07-02 01:27:54.195821750 +0200
71 @@ -0,0 +1,582 @@
72 +/*
73 +  Kernel module to match application layer (OSI layer 7) data in connections.
74 +
75 +  http://l7-filter.sf.net
76 +
77 +  By Matthew Strait and Ethan Sommer, 2003-2006.
78 +
79 +  This program is free software; you can redistribute it and/or
80 +  modify it under the terms of the GNU General Public License
81 +  as published by the Free Software Foundation; either version
82 +  2 of the License, or (at your option) any later version.
83 +  http://www.gnu.org/licenses/gpl.txt
84 +
85 +  Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
86 +  and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
87 +
88 +  Jan Engelhardt, 2007-03-11: Arrange to compile with nf_conntrack
89 +*/
90 +
91 +#include <linux/module.h>
92 +#include <linux/skbuff.h>
93 +#include <linux/proc_fs.h>
94 +#include <linux/ctype.h>
95 +#include <net/ip.h>
96 +#include <net/tcp.h>
97 +#include <net/netfilter/nf_conntrack.h>
98 +#include <net/netfilter/nf_nat_rule.h>
99 +#include <linux/spinlock.h>
100 +
101 +#include "regexp/regexp.c"
102 +
103 +#include <linux/netfilter_ipv4/ipt_layer7.h>
104 +#include <linux/netfilter_ipv4/ip_tables.h>
105 +
106 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
107 +MODULE_LICENSE("GPL");
108 +MODULE_DESCRIPTION("iptables application layer match module");
109 +MODULE_VERSION("2.0");
110 +
111 +static int maxdatalen = 2048; // this is the default
112 +module_param(maxdatalen, int, 0444);
113 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
114 +
115 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
116 +       #define DPRINTK(format,args...) printk(format,##args)
117 +#else
118 +       #define DPRINTK(format,args...)
119 +#endif
120 +
121 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
122 +                     master_conntrack->counters[IP_CT_DIR_REPLY].packets
123 +
124 +/* Number of packets whose data we look at.
125 +This can be modified through /proc/net/layer7_numpackets */
126 +static int num_packets = 10;
127 +
128 +static struct pattern_cache {
129 +       char * regex_string;
130 +       regexp * pattern;
131 +       struct pattern_cache * next;
132 +} * first_pattern_cache = NULL;
133 +
134 +/* I'm new to locking.  Here are my assumptions:
135 +
136 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
137 +  if they did, nothing awful would happen.
138 +
139 +- This code will never be processing the same packet twice at the same time,
140 +  because iptables rules are traversed in order.
141 +
142 +- It doesn't matter if two packets from different connections are in here at
143 +  the same time, because they don't share any data.
144 +
145 +- It _does_ matter if two packets from the same connection (or one from a
146 +  master and one from its child) are here at the same time.  In this case,
147 +  we have to protect the conntracks and the list of compiled patterns.
148 +*/
149 +DEFINE_RWLOCK(ct_lock);
150 +DEFINE_SPINLOCK(list_lock);
151 +
152 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
153 +/* Converts an unfriendly string into a friendly one by
154 +replacing unprintables with periods and all whitespace with " ". */
155 +static char * friendly_print(unsigned char * s)
156 +{
157 +       char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
158 +       int i;
159 +
160 +       if(!f) {
161 +               if (net_ratelimit())
162 +                       printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
163 +               return NULL;
164 +       }
165 +
166 +       for(i = 0; i < strlen(s); i++){
167 +               if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
168 +               else if(isspace(s[i]))          f[i] = ' ';
169 +               else                            f[i] = '.';
170 +       }
171 +       f[i] = '\0';
172 +       return f;
173 +}
174 +
175 +static char dec2hex(int i)
176 +{
177 +       switch (i) {
178 +               case 0 ... 9:
179 +                       return (char)(i + '0');
180 +                       break;
181 +               case 10 ... 15:
182 +                       return (char)(i - 10 + 'a');
183 +                       break;
184 +               default:
185 +                       if (net_ratelimit())
186 +                               printk("Problem in dec2hex\n");
187 +                       return '\0';
188 +       }
189 +}
190 +
191 +static char * hex_print(unsigned char * s)
192 +{
193 +       char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
194 +       int i;
195 +
196 +       if(!g) {
197 +              if (net_ratelimit())
198 +                       printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
199 +              return NULL;
200 +       }
201 +
202 +       for(i = 0; i < strlen(s); i++) {
203 +               g[i*3    ] = dec2hex(s[i]/16);
204 +               g[i*3 + 1] = dec2hex(s[i]%16);
205 +               g[i*3 + 2] = ' ';
206 +       }
207 +       g[i*3] = '\0';
208 +
209 +       return g;
210 +}
211 +#endif // DEBUG
212 +
213 +/* Use instead of regcomp.  As we expect to be seeing the same regexps over and
214 +over again, it make sense to cache the results. */
215 +static regexp * compile_and_cache(char * regex_string, char * protocol)
216 +{
217 +       struct pattern_cache * node               = first_pattern_cache;
218 +       struct pattern_cache * last_pattern_cache = first_pattern_cache;
219 +       struct pattern_cache * tmp;
220 +       unsigned int len;
221 +
222 +       while (node != NULL) {
223 +               if (!strcmp(node->regex_string, regex_string))
224 +               return node->pattern;
225 +
226 +               last_pattern_cache = node;/* points at the last non-NULL node */
227 +               node = node->next;
228 +       }
229 +
230 +       /* If we reach the end of the list, then we have not yet cached
231 +          the pattern for this regex. Let's do that now.
232 +          Be paranoid about running out of memory to avoid list corruption. */
233 +       tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
234 +
235 +       if(!tmp) {
236 +               if (net_ratelimit())
237 +                       printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
238 +               return NULL;
239 +       }
240 +
241 +       tmp->regex_string  = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
242 +       tmp->pattern       = kmalloc(sizeof(struct regexp),    GFP_ATOMIC);
243 +       tmp->next = NULL;
244 +
245 +       if(!tmp->regex_string || !tmp->pattern) {
246 +               if (net_ratelimit())
247 +                       printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
248 +               kfree(tmp->regex_string);
249 +               kfree(tmp->pattern);
250 +               kfree(tmp);
251 +               return NULL;
252 +       }
253 +
254 +       /* Ok.  The new node is all ready now. */
255 +       node = tmp;
256 +
257 +       if(first_pattern_cache == NULL) /* list is empty */
258 +               first_pattern_cache = node; /* make node the beginning */
259 +       else
260 +               last_pattern_cache->next = node; /* attach node to the end */
261 +
262 +       /* copy the string and compile the regex */
263 +       len = strlen(regex_string);
264 +       DPRINTK("About to compile this: \"%s\"\n", regex_string);
265 +       node->pattern = regcomp(regex_string, &len);
266 +       if ( !node->pattern ) {
267 +               if (net_ratelimit())
268 +                       printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
269 +               /* pattern is now cached as NULL, so we won't try again. */
270 +       }
271 +
272 +       strcpy(node->regex_string, regex_string);
273 +       return node->pattern;
274 +}
275 +
276 +static int can_handle(const struct sk_buff *skb)
277 +{
278 +       if(!skb->nh.iph) /* not IP */
279 +               return 0;
280 +       if(skb->nh.iph->protocol != IPPROTO_TCP &&
281 +          skb->nh.iph->protocol != IPPROTO_UDP &&
282 +          skb->nh.iph->protocol != IPPROTO_ICMP)
283 +               return 0;
284 +       return 1;
285 +}
286 +
287 +/* Returns offset the into the skb->data that the application data starts */
288 +static int app_data_offset(const struct sk_buff *skb)
289 +{
290 +       /* In case we are ported somewhere (ebtables?) where skb->nh.iph
291 +       isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
292 +       int ip_hl = 4*skb->nh.iph->ihl;
293 +
294 +       if( skb->nh.iph->protocol == IPPROTO_TCP ) {
295 +               /* 12 == offset into TCP header for the header length field.
296 +               Can't get this with skb->h.th->doff because the tcphdr
297 +               struct doesn't get set when routing (this is confirmed to be
298 +               true in Netfilter as well as QoS.) */
299 +               int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
300 +
301 +               return ip_hl + tcp_hl;
302 +       } else if( skb->nh.iph->protocol == IPPROTO_UDP  ) {
303 +               return ip_hl + 8; /* UDP header is always 8 bytes */
304 +       } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
305 +               return ip_hl + 8; /* ICMP header is 8 bytes */
306 +       } else {
307 +               if (net_ratelimit())
308 +                       printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
309 +               return ip_hl + 8; /* something reasonable */
310 +       }
311 +}
312 +
313 +/* handles whether there's a match when we aren't appending data anymore */
314 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
315 +                       enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
316 +                       struct ipt_layer7_info * info)
317 +{
318 +       /* If we're in here, throw the app data away */
319 +       write_lock(&ct_lock);
320 +       if(master_conntrack->layer7.app_data != NULL) {
321 +
322 +       #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
323 +               if(!master_conntrack->layer7.app_proto) {
324 +                       char * f = friendly_print(master_conntrack->layer7.app_data);
325 +                       char * g = hex_print(master_conntrack->layer7.app_data);
326 +                       DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
327 +                               strlen(f), TOTAL_PACKETS, f);
328 +                       kfree(f);
329 +                       DPRINTK("In hex: %s\n", g);
330 +                       kfree(g);
331 +               }
332 +       #endif
333 +
334 +               kfree(master_conntrack->layer7.app_data);
335 +               master_conntrack->layer7.app_data = NULL; /* don't free again */
336 +       }
337 +       write_unlock(&ct_lock);
338 +
339 +       if(master_conntrack->layer7.app_proto){
340 +               /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
341 +               write_lock(&ct_lock);
342 +               if(!conntrack->layer7.app_proto) {
343 +                       conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
344 +                       if(!conntrack->layer7.app_proto){
345 +                               if (net_ratelimit())
346 +                                       printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
347 +                               write_unlock(&ct_lock);
348 +                               return 1;
349 +                       }
350 +                       strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
351 +               }
352 +               write_unlock(&ct_lock);
353 +
354 +               return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
355 +       }
356 +       else {
357 +               /* If not classified, set to "unknown" to distinguish from
358 +               connections that are still being tested. */
359 +               write_lock(&ct_lock);
360 +               master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
361 +               if(!master_conntrack->layer7.app_proto){
362 +                       if (net_ratelimit())
363 +                               printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
364 +                       write_unlock(&ct_lock);
365 +                       return 1;
366 +               }
367 +               strcpy(master_conntrack->layer7.app_proto, "unknown");
368 +               write_unlock(&ct_lock);
369 +               return 0;
370 +       }
371 +}
372 +
373 +/* add the new app data to the conntrack.  Return number of bytes added. */
374 +static int add_data(struct ip_conntrack * master_conntrack,
375 +                       char * app_data, int appdatalen)
376 +{
377 +       int length = 0, i;
378 +       int oldlength = master_conntrack->layer7.app_data_len;
379 +
380 +       // This is a fix for a race condition by Deti Fliegl. However, I'm not 
381 +       // clear on whether the race condition exists or whether this really 
382 +       // fixes it.  I might just be being dense... Anyway, if it's not really 
383 +       // a fix, all it does is waste a very small amount of time.
384 +       if(!master_conntrack->layer7.app_data) return 0;
385 +
386 +       /* Strip nulls. Make everything lower case (our regex lib doesn't
387 +       do case insensitivity).  Add it to the end of the current data. */
388 +       for(i = 0; i < maxdatalen-oldlength-1 &&
389 +                  i < appdatalen; i++) {
390 +               if(app_data[i] != '\0') {
391 +                       master_conntrack->layer7.app_data[length+oldlength] =
392 +                               /* the kernel version of tolower mungs 'upper ascii' */
393 +                               isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
394 +                       length++;
395 +               }
396 +       }
397 +
398 +       master_conntrack->layer7.app_data[length+oldlength] = '\0';
399 +       master_conntrack->layer7.app_data_len = length + oldlength;
400 +
401 +       return length;
402 +}
403 +
404 +/* Returns true on match and false otherwise.  */
405 +static int match(const struct sk_buff *skbin,
406 +       const struct net_device *in, const struct net_device *out,
407 +       const struct xt_match *match, const void *matchinfo,
408 +       int offset, unsigned int protoff, int *hotdrop)
409 +{
410 +       /* sidestep const without getting a compiler warning... */
411 +       struct sk_buff * skb = (struct sk_buff *)skbin; 
412 +
413 +       struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
414 +       enum ip_conntrack_info master_ctinfo, ctinfo;
415 +       struct nf_conn *master_conntrack;
416 +       struct ip_conntrack *conntrack;
417 +       unsigned char * app_data;
418 +       unsigned int pattern_result, appdatalen;
419 +       regexp * comppattern;
420 +
421 +       if(!can_handle(skb)){
422 +               DPRINTK("layer7: This is some protocol I can't handle.\n");
423 +               return info->invert;
424 +       }
425 +
426 +       /* Treat parent & all its children together as one connection, except
427 +       for the purpose of setting conntrack->layer7.app_proto in the actual
428 +       connection. This makes /proc/net/ip_conntrack more satisfying. */
429 +       if(((conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) == NULL) ||
430 +          ((master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo)) == NULL)) {
431 +               return info->invert;
432 +       }
433 +
434 +       /* Try to get a master conntrack (and its master etc) for FTP, etc. */
435 +       while (master_ct(master_conntrack) != NULL)
436 +               master_conntrack = master_ct(master_conntrack);
437 +
438 +       /* if we've classified it or seen too many packets */
439 +       if(TOTAL_PACKETS > num_packets ||
440 +          master_conntrack->layer7.app_proto) {
441 +
442 +               pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
443 +
444 +               /* skb->cb[0] == seen. Don't do things twice if there are multiple l7
445 +               rules. I'm not sure that using cb for this purpose is correct, even though
446 +               it says "put your private variables there". But it doesn't look like it
447 +               is being used for anything else in the skbs that make it here. */
448 +               skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
449 +
450 +               return (pattern_result ^ info->invert);
451 +       }
452 +
453 +       if(skb_is_nonlinear(skb)){
454 +               if(skb_linearize(skb) != 0){
455 +                       if (net_ratelimit())
456 +                               printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
457 +                       return info->invert;
458 +               }
459 +       }
460 +
461 +       /* now that the skb is linearized, it's safe to set these. */
462 +       app_data = skb->data + app_data_offset(skb);
463 +       appdatalen = skb->tail - app_data;
464 +
465 +       spin_lock_bh(&list_lock);
466 +       /* the return value gets checked later, when we're ready to use it */
467 +       comppattern = compile_and_cache(info->pattern, info->protocol);
468 +       spin_unlock_bh(&list_lock);
469 +
470 +       /* On the first packet of a connection, allocate space for app data */
471 +       write_lock(&ct_lock);
472 +       if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
473 +               master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
474 +               if(!master_conntrack->layer7.app_data){
475 +                       if (net_ratelimit())
476 +                               printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
477 +                       write_unlock(&ct_lock);
478 +                       return info->invert;
479 +               }
480 +
481 +               master_conntrack->layer7.app_data[0] = '\0';
482 +       }
483 +       write_unlock(&ct_lock);
484 +
485 +       /* Can be here, but unallocated, if numpackets is increased near
486 +       the beginning of a connection */
487 +       if(master_conntrack->layer7.app_data == NULL)
488 +               return (info->invert); /* unmatched */
489 +
490 +       if(!skb->cb[0]){
491 +               int newbytes;
492 +               write_lock(&ct_lock);
493 +               newbytes = add_data(master_conntrack, app_data, appdatalen);
494 +               write_unlock(&ct_lock);
495 +
496 +               if(newbytes == 0) { /* didn't add any data */
497 +                       skb->cb[0] = 1;
498 +                       /* Didn't match before, not going to match now */
499 +                       return info->invert;
500 +               }
501 +       }
502 +
503 +       /* If looking for "unknown", then never match.  "Unknown" means that
504 +       we've given up; we're still trying with these packets. */
505 +       read_lock(&ct_lock);
506 +       if(!strcmp(info->protocol, "unknown")) {
507 +               pattern_result = 0;
508 +       /* If looking for "unset", then always match. "Unset" means that we
509 +       haven't yet classified the connection. */
510 +       } else if(!strcmp(info->protocol, "unset")) {
511 +               pattern_result = 2;
512 +               DPRINTK("layer7: matched unset: not yet classified (%d/%d packets)\n", TOTAL_PACKETS, num_packets);
513 +       /* If the regexp failed to compile, don't bother running it */
514 +       } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
515 +               DPRINTK("layer7: matched %s\n", info->protocol);
516 +               pattern_result = 1;
517 +       } else pattern_result = 0;
518 +       read_unlock(&ct_lock);
519 +
520 +       if(pattern_result == 1) {
521 +               write_lock(&ct_lock);
522 +               master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
523 +               if(!master_conntrack->layer7.app_proto){
524 +                       if (net_ratelimit())
525 +                               printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
526 +                       write_unlock(&ct_lock);
527 +                       return (pattern_result ^ info->invert);
528 +               }
529 +               strcpy(master_conntrack->layer7.app_proto, info->protocol);
530 +               write_unlock(&ct_lock);
531 +       } else if(pattern_result > 1) { /* cleanup from "unset" */
532 +               pattern_result = 1;
533 +       }
534 +
535 +       /* mark the packet seen */
536 +       skb->cb[0] = 1;
537 +
538 +       return (pattern_result ^ info->invert);
539 +}
540 +
541 +static struct xt_match layer7_match = {
542 +       .name = "layer7",
543 +       .match = &match,
544 +       .matchsize  = sizeof(struct ipt_layer7_info),
545 +       .me = THIS_MODULE
546 +};
547 +
548 +/* taken from drivers/video/modedb.c */
549 +static int my_atoi(const char *s)
550 +{
551 +       int val = 0;
552 +
553 +       for (;; s++) {
554 +               switch (*s) {
555 +                       case '0'...'9':
556 +                       val = 10*val+(*s-'0');
557 +                       break;
558 +               default:
559 +                       return val;
560 +               }
561 +       }
562 +}
563 +
564 +/* write out num_packets to userland. */
565 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
566 +                    int* eof, void * data)
567 +{
568 +       if(num_packets > 99 && net_ratelimit())
569 +               printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
570 +
571 +       page[0] = num_packets/10 + '0';
572 +       page[1] = num_packets%10 + '0';
573 +       page[2] = '\n';
574 +       page[3] = '\0';
575 +
576 +       *eof=1;
577 +
578 +       return 3;
579 +}
580 +
581 +/* Read in num_packets from userland */
582 +static int layer7_write_proc(struct file* file, const char* buffer,
583 +                     unsigned long count, void *data)
584 +{
585 +       char * foo = kmalloc(count, GFP_ATOMIC);
586 +
587 +       if(!foo){
588 +               if (net_ratelimit())
589 +                       printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
590 +               return count;
591 +       }
592 +
593 +       if(copy_from_user(foo, buffer, count)) {
594 +               return -EFAULT;
595 +       }
596 +
597 +
598 +       num_packets = my_atoi(foo);
599 +       kfree (foo);
600 +
601 +       /* This has an arbitrary limit to make the math easier. I'm lazy.
602 +       But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
603 +       if(num_packets > 99) {
604 +               printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
605 +               num_packets = 99;
606 +       } else if(num_packets < 1) {
607 +               printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
608 +               num_packets = 1;
609 +       }
610 +
611 +       return count;
612 +}
613 +
614 +/* register the proc file */
615 +static void layer7_init_proc(void)
616 +{
617 +       struct proc_dir_entry* entry;
618 +       entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
619 +       entry->read_proc = layer7_read_proc;
620 +       entry->write_proc = layer7_write_proc;
621 +}
622 +
623 +static void layer7_cleanup_proc(void)
624 +{
625 +       remove_proc_entry("layer7_numpackets", proc_net);
626 +}
627 +
628 +static int __init ipt_layer7_init(void)
629 +{
630 +       need_conntrack();
631 +
632 +       layer7_init_proc();
633 +       if(maxdatalen < 1) {
634 +               printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
635 +               maxdatalen = 1;
636 +       }
637 +       /* This is not a hard limit.  It's just here to prevent people from
638 +       bringing their slow machines to a grinding halt. */
639 +       else if(maxdatalen > 65536) {
640 +               printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
641 +               maxdatalen = 65536;
642 +       }
643 +       return xt_register_match(&layer7_match);
644 +}
645 +
646 +static void __exit ipt_layer7_fini(void)
647 +{
648 +       layer7_cleanup_proc();
649 +       xt_unregister_match(&layer7_match);
650 +}
651 +
652 +module_init(ipt_layer7_init);
653 +module_exit(ipt_layer7_fini);
654 Index: linux-2.6.21.5/net/ipv4/netfilter/Kconfig
655 ===================================================================
656 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Kconfig      2007-07-02 00:37:53.456287250 +0200
657 +++ linux-2.6.21.5/net/ipv4/netfilter/Kconfig   2007-07-02 01:21:17.231013000 +0200
658 @@ -245,6 +245,24 @@
659  
660           To compile it as a module, choose M here.  If unsure, say N.
661  
662 +config IP_NF_MATCH_LAYER7
663 +       tristate "Layer 7 match support (EXPERIMENTAL)"
664 +       depends on IP_NF_IPTABLES && NF_CT_ACCT && NF_CONNTRACK && EXPERIMENTAL
665 +       help
666 +         Say Y if you want to be able to classify connections (and their
667 +         packets) based on regular expression matching of their application
668 +         layer data.   This is one way to classify applications such as
669 +         peer-to-peer filesharing systems that do not always use the same
670 +         port.
671 +
672 +         To compile it as a module, choose M here.  If unsure, say N.
673 +
674 +config IP_NF_MATCH_LAYER7_DEBUG
675 +       bool "Layer 7 debugging output"
676 +       depends on IP_NF_MATCH_LAYER7
677 +       help
678 +         Say Y to get lots of debugging output.
679 +
680  config IP_NF_MATCH_TOS
681         tristate "TOS match support"
682         depends on IP_NF_IPTABLES
683 Index: linux-2.6.21.5/net/ipv4/netfilter/Makefile
684 ===================================================================
685 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Makefile     2007-07-02 00:37:53.464287750 +0200
686 +++ linux-2.6.21.5/net/ipv4/netfilter/Makefile  2007-07-02 00:43:58.191081750 +0200
687 @@ -92,6 +92,8 @@
688  obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
689  obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
690  
691 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
692 +
693  # targets
694  obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
695  obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
696 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c
697 ===================================================================
698 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
699 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c   2007-07-02 00:37:55.648424250 +0200
700 @@ -0,0 +1,1197 @@
701 +/*
702 + * regcomp and regexec -- regsub and regerror are elsewhere
703 + * @(#)regexp.c        1.3 of 18 April 87
704 + *
705 + *     Copyright (c) 1986 by University of Toronto.
706 + *     Written by Henry Spencer.  Not derived from licensed software.
707 + *
708 + *     Permission is granted to anyone to use this software for any
709 + *     purpose on any computer system, and to redistribute it freely,
710 + *     subject to the following restrictions:
711 + *
712 + *     1. The author is not responsible for the consequences of use of
713 + *             this software, no matter how awful, even if they arise
714 + *             from defects in it.
715 + *
716 + *     2. The origin of this software must not be misrepresented, either
717 + *             by explicit claim or by omission.
718 + *
719 + *     3. Altered versions must be plainly marked as such, and must not
720 + *             be misrepresented as being the original software.
721 + *
722 + * Beware that some of this code is subtly aware of the way operator
723 + * precedence is structured in regular expressions.  Serious changes in
724 + * regular-expression syntax might require a total rethink.
725 + *
726 + * This code was modified by Ethan Sommer to work within the kernel
727 + * (it now uses kmalloc etc..)
728 + *
729 + * Modified slightly by Matthew Strait to use more modern C.
730 + */
731 +
732 +#include "regexp.h"
733 +#include "regmagic.h"
734 +
735 +/* added by ethan and matt.  Lets it work in both kernel and user space.
736 +(So iptables can use it, for instance.)  Yea, it goes both ways... */
737 +#if __KERNEL__
738 +  #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
739 +#else
740 +  #define printk(format,args...) printf(format,##args)
741 +#endif
742 +
743 +void regerror(char * s)
744 +{
745 +        printk("<3>Regexp: %s\n", s);
746 +        /* NOTREACHED */
747 +}
748 +
749 +/*
750 + * The "internal use only" fields in regexp.h are present to pass info from
751 + * compile to execute that permits the execute phase to run lots faster on
752 + * simple cases.  They are:
753 + *
754 + * regstart    char that must begin a match; '\0' if none obvious
755 + * reganch     is the match anchored (at beginning-of-line only)?
756 + * regmust     string (pointer into program) that match must include, or NULL
757 + * regmlen     length of regmust string
758 + *
759 + * Regstart and reganch permit very fast decisions on suitable starting points
760 + * for a match, cutting down the work a lot.  Regmust permits fast rejection
761 + * of lines that cannot possibly match.  The regmust tests are costly enough
762 + * that regcomp() supplies a regmust only if the r.e. contains something
763 + * potentially expensive (at present, the only such thing detected is * or +
764 + * at the start of the r.e., which can involve a lot of backup).  Regmlen is
765 + * supplied because the test in regexec() needs it and regcomp() is computing
766 + * it anyway.
767 + */
768 +
769 +/*
770 + * Structure for regexp "program".  This is essentially a linear encoding
771 + * of a nondeterministic finite-state machine (aka syntax charts or
772 + * "railroad normal form" in parsing technology).  Each node is an opcode
773 + * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
774 + * all nodes except BRANCH implement concatenation; a "next" pointer with
775 + * a BRANCH on both ends of it is connecting two alternatives.  (Here we
776 + * have one of the subtle syntax dependencies:  an individual BRANCH (as
777 + * opposed to a collection of them) is never concatenated with anything
778 + * because of operator precedence.)  The operand of some types of node is
779 + * a literal string; for others, it is a node leading into a sub-FSM.  In
780 + * particular, the operand of a BRANCH node is the first node of the branch.
781 + * (NB this is *not* a tree structure:  the tail of the branch connects
782 + * to the thing following the set of BRANCHes.)  The opcodes are:
783 + */
784 +
785 +/* definition  number  opnd?   meaning */
786 +#define        END     0       /* no   End of program. */
787 +#define        BOL     1       /* no   Match "" at beginning of line. */
788 +#define        EOL     2       /* no   Match "" at end of line. */
789 +#define        ANY     3       /* no   Match any one character. */
790 +#define        ANYOF   4       /* str  Match any character in this string. */
791 +#define        ANYBUT  5       /* str  Match any character not in this string. */
792 +#define        BRANCH  6       /* node Match this alternative, or the next... */
793 +#define        BACK    7       /* no   Match "", "next" ptr points backward. */
794 +#define        EXACTLY 8       /* str  Match this string. */
795 +#define        NOTHING 9       /* no   Match empty string. */
796 +#define        STAR    10      /* node Match this (simple) thing 0 or more times. */
797 +#define        PLUS    11      /* node Match this (simple) thing 1 or more times. */
798 +#define        OPEN    20      /* no   Mark this point in input as start of #n. */
799 +                       /*      OPEN+1 is number 1, etc. */
800 +#define        CLOSE   30      /* no   Analogous to OPEN. */
801 +
802 +/*
803 + * Opcode notes:
804 + *
805 + * BRANCH      The set of branches constituting a single choice are hooked
806 + *             together with their "next" pointers, since precedence prevents
807 + *             anything being concatenated to any individual branch.  The
808 + *             "next" pointer of the last BRANCH in a choice points to the
809 + *             thing following the whole choice.  This is also where the
810 + *             final "next" pointer of each individual branch points; each
811 + *             branch starts with the operand node of a BRANCH node.
812 + *
813 + * BACK                Normal "next" pointers all implicitly point forward; BACK
814 + *             exists to make loop structures possible.
815 + *
816 + * STAR,PLUS   '?', and complex '*' and '+', are implemented as circular
817 + *             BRANCH structures using BACK.  Simple cases (one character
818 + *             per match) are implemented with STAR and PLUS for speed
819 + *             and to minimize recursive plunges.
820 + *
821 + * OPEN,CLOSE  ...are numbered at compile time.
822 + */
823 +
824 +/*
825 + * A node is one char of opcode followed by two chars of "next" pointer.
826 + * "Next" pointers are stored as two 8-bit pieces, high order first.  The
827 + * value is a positive offset from the opcode of the node containing it.
828 + * An operand, if any, simply follows the node.  (Note that much of the
829 + * code generation knows about this implicit relationship.)
830 + *
831 + * Using two bytes for the "next" pointer is vast overkill for most things,
832 + * but allows patterns to get big without disasters.
833 + */
834 +#define        OP(p)   (*(p))
835 +#define        NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
836 +#define        OPERAND(p)      ((p) + 3)
837 +
838 +/*
839 + * See regmagic.h for one further detail of program structure.
840 + */
841 +
842 +
843 +/*
844 + * Utility definitions.
845 + */
846 +#ifndef CHARBITS
847 +#define        UCHARAT(p)      ((int)*(unsigned char *)(p))
848 +#else
849 +#define        UCHARAT(p)      ((int)*(p)&CHARBITS)
850 +#endif
851 +
852 +#define        FAIL(m) { regerror(m); return(NULL); }
853 +#define        ISMULT(c)       ((c) == '*' || (c) == '+' || (c) == '?')
854 +#define        META    "^$.[()|?+*\\"
855 +
856 +/*
857 + * Flags to be passed up and down.
858 + */
859 +#define        HASWIDTH        01      /* Known never to match null string. */
860 +#define        SIMPLE          02      /* Simple enough to be STAR/PLUS operand. */
861 +#define        SPSTART         04      /* Starts with * or +. */
862 +#define        WORST           0       /* Worst case. */
863 +
864 +/*
865 + * Global work variables for regcomp().
866 + */
867 +struct match_globals {
868 +char *reginput;                /* String-input pointer. */
869 +char *regbol;          /* Beginning of input, for ^ check. */
870 +char **regstartp;      /* Pointer to startp array. */
871 +char **regendp;                /* Ditto for endp. */
872 +char *regparse;                /* Input-scan pointer. */
873 +int regnpar;           /* () count. */
874 +char regdummy;
875 +char *regcode;         /* Code-emit pointer; &regdummy = don't. */
876 +long regsize;          /* Code size. */
877 +};
878 +
879 +/*
880 + * Forward declarations for regcomp()'s friends.
881 + */
882 +#ifndef STATIC
883 +#define        STATIC  static
884 +#endif
885 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
886 +STATIC char *regbranch(struct match_globals *g, int *flagp);
887 +STATIC char *regpiece(struct match_globals *g, int *flagp);
888 +STATIC char *regatom(struct match_globals *g, int *flagp);
889 +STATIC char *regnode(struct match_globals *g, char op);
890 +STATIC char *regnext(struct match_globals *g, char *p);
891 +STATIC void regc(struct match_globals *g, char b);
892 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
893 +STATIC void regtail(struct match_globals *g, char *p, char *val);
894 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
895 +
896 +
897 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
898 +{
899 +        char *scan1;
900 +        char *scan2;
901 +        int count;
902 +
903 +        count = 0;
904 +        for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
905 +                for (scan2 = (char *)s2; *scan2 != '\0';)       /* ++ moved down. */
906 +                        if (*scan1 == *scan2++)
907 +                                return(count);
908 +                count++;
909 +        }
910 +        return(count);
911 +}
912 +
913 +/*
914 + - regcomp - compile a regular expression into internal code
915 + *
916 + * We can't allocate space until we know how big the compiled form will be,
917 + * but we can't compile it (and thus know how big it is) until we've got a
918 + * place to put the code.  So we cheat:  we compile it twice, once with code
919 + * generation turned off and size counting turned on, and once "for real".
920 + * This also means that we don't allocate space until we are sure that the
921 + * thing really will compile successfully, and we never have to move the
922 + * code and thus invalidate pointers into it.  (Note that it has to be in
923 + * one piece because free() must be able to free it all.)
924 + *
925 + * Beware that the optimization-preparation code in here knows about some
926 + * of the structure of the compiled regexp.
927 + */
928 +regexp *
929 +regcomp(char *exp,int *patternsize)
930 +{
931 +       register regexp *r;
932 +       register char *scan;
933 +       register char *longest;
934 +       register int len;
935 +       int flags;
936 +       struct match_globals g;
937 +       
938 +       /* commented out by ethan
939 +          extern char *malloc();
940 +       */
941 +
942 +       if (exp == NULL)
943 +               FAIL("NULL argument");
944 +
945 +       /* First pass: determine size, legality. */
946 +       g.regparse = exp;
947 +       g.regnpar = 1;
948 +       g.regsize = 0L;
949 +       g.regcode = &g.regdummy;
950 +       regc(&g, MAGIC);
951 +       if (reg(&g, 0, &flags) == NULL)
952 +               return(NULL);
953 +
954 +       /* Small enough for pointer-storage convention? */
955 +       if (g.regsize >= 32767L)                /* Probably could be 65535L. */
956 +               FAIL("regexp too big");
957 +
958 +       /* Allocate space. */
959 +       *patternsize=sizeof(regexp) + (unsigned)g.regsize;
960 +       r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
961 +       if (r == NULL)
962 +               FAIL("out of space");
963 +
964 +       /* Second pass: emit code. */
965 +       g.regparse = exp;
966 +       g.regnpar = 1;
967 +       g.regcode = r->program;
968 +       regc(&g, MAGIC);
969 +       if (reg(&g, 0, &flags) == NULL)
970 +               return(NULL);
971 +
972 +       /* Dig out information for optimizations. */
973 +       r->regstart = '\0';     /* Worst-case defaults. */
974 +       r->reganch = 0;
975 +       r->regmust = NULL;
976 +       r->regmlen = 0;
977 +       scan = r->program+1;                    /* First BRANCH. */
978 +       if (OP(regnext(&g, scan)) == END) {             /* Only one top-level choice. */
979 +               scan = OPERAND(scan);
980 +
981 +               /* Starting-point info. */
982 +               if (OP(scan) == EXACTLY)
983 +                       r->regstart = *OPERAND(scan);
984 +               else if (OP(scan) == BOL)
985 +                       r->reganch++;
986 +
987 +               /*
988 +                * If there's something expensive in the r.e., find the
989 +                * longest literal string that must appear and make it the
990 +                * regmust.  Resolve ties in favor of later strings, since
991 +                * the regstart check works with the beginning of the r.e.
992 +                * and avoiding duplication strengthens checking.  Not a
993 +                * strong reason, but sufficient in the absence of others.
994 +                */
995 +               if (flags&SPSTART) {
996 +                       longest = NULL;
997 +                       len = 0;
998 +                       for (; scan != NULL; scan = regnext(&g, scan))
999 +                               if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1000 +                                       longest = OPERAND(scan);
1001 +                                       len = strlen(OPERAND(scan));
1002 +                               }
1003 +                       r->regmust = longest;
1004 +                       r->regmlen = len;
1005 +               }
1006 +       }
1007 +
1008 +       return(r);
1009 +}
1010 +
1011 +/*
1012 + - reg - regular expression, i.e. main body or parenthesized thing
1013 + *
1014 + * Caller must absorb opening parenthesis.
1015 + *
1016 + * Combining parenthesis handling with the base level of regular expression
1017 + * is a trifle forced, but the need to tie the tails of the branches to what
1018 + * follows makes it hard to avoid.
1019 + */
1020 +static char *
1021 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1022 +{
1023 +       register char *ret;
1024 +       register char *br;
1025 +       register char *ender;
1026 +       register int parno = 0; /* 0 makes gcc happy */
1027 +       int flags;
1028 +
1029 +       *flagp = HASWIDTH;      /* Tentatively. */
1030 +
1031 +       /* Make an OPEN node, if parenthesized. */
1032 +       if (paren) {
1033 +               if (g->regnpar >= NSUBEXP)
1034 +                       FAIL("too many ()");
1035 +               parno = g->regnpar;
1036 +               g->regnpar++;
1037 +               ret = regnode(g, OPEN+parno);
1038 +       } else
1039 +               ret = NULL;
1040 +
1041 +       /* Pick up the branches, linking them together. */
1042 +       br = regbranch(g, &flags);
1043 +       if (br == NULL)
1044 +               return(NULL);
1045 +       if (ret != NULL)
1046 +               regtail(g, ret, br);    /* OPEN -> first. */
1047 +       else
1048 +               ret = br;
1049 +       if (!(flags&HASWIDTH))
1050 +               *flagp &= ~HASWIDTH;
1051 +       *flagp |= flags&SPSTART;
1052 +       while (*g->regparse == '|') {
1053 +               g->regparse++;
1054 +               br = regbranch(g, &flags);
1055 +               if (br == NULL)
1056 +                       return(NULL);
1057 +               regtail(g, ret, br);    /* BRANCH -> BRANCH. */
1058 +               if (!(flags&HASWIDTH))
1059 +                       *flagp &= ~HASWIDTH;
1060 +               *flagp |= flags&SPSTART;
1061 +       }
1062 +
1063 +       /* Make a closing node, and hook it on the end. */
1064 +       ender = regnode(g, (paren) ? CLOSE+parno : END);        
1065 +       regtail(g, ret, ender);
1066 +
1067 +       /* Hook the tails of the branches to the closing node. */
1068 +       for (br = ret; br != NULL; br = regnext(g, br))
1069 +               regoptail(g, br, ender);
1070 +
1071 +       /* Check for proper termination. */
1072 +       if (paren && *g->regparse++ != ')') {
1073 +               FAIL("unmatched ()");
1074 +       } else if (!paren && *g->regparse != '\0') {
1075 +               if (*g->regparse == ')') {
1076 +                       FAIL("unmatched ()");
1077 +               } else
1078 +                       FAIL("junk on end");    /* "Can't happen". */
1079 +               /* NOTREACHED */
1080 +       }
1081 +
1082 +       return(ret);
1083 +}
1084 +
1085 +/*
1086 + - regbranch - one alternative of an | operator
1087 + *
1088 + * Implements the concatenation operator.
1089 + */
1090 +static char *
1091 +regbranch(struct match_globals *g, int *flagp)
1092 +{
1093 +       register char *ret;
1094 +       register char *chain;
1095 +       register char *latest;
1096 +       int flags;
1097 +
1098 +       *flagp = WORST;         /* Tentatively. */
1099 +
1100 +       ret = regnode(g, BRANCH);
1101 +       chain = NULL;
1102 +       while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1103 +               latest = regpiece(g, &flags);
1104 +               if (latest == NULL)
1105 +                       return(NULL);
1106 +               *flagp |= flags&HASWIDTH;
1107 +               if (chain == NULL)      /* First piece. */
1108 +                       *flagp |= flags&SPSTART;
1109 +               else
1110 +                       regtail(g, chain, latest);
1111 +               chain = latest;
1112 +       }
1113 +       if (chain == NULL)      /* Loop ran zero times. */
1114 +               (void) regnode(g, NOTHING);
1115 +
1116 +       return(ret);
1117 +}
1118 +
1119 +/*
1120 + - regpiece - something followed by possible [*+?]
1121 + *
1122 + * Note that the branching code sequences used for ? and the general cases
1123 + * of * and + are somewhat optimized:  they use the same NOTHING node as
1124 + * both the endmarker for their branch list and the body of the last branch.
1125 + * It might seem that this node could be dispensed with entirely, but the
1126 + * endmarker role is not redundant.
1127 + */
1128 +static char *
1129 +regpiece(struct match_globals *g, int *flagp)
1130 +{
1131 +       register char *ret;
1132 +       register char op;
1133 +       register char *next;
1134 +       int flags;
1135 +
1136 +       ret = regatom(g, &flags);
1137 +       if (ret == NULL)
1138 +               return(NULL);
1139 +
1140 +       op = *g->regparse;
1141 +       if (!ISMULT(op)) {
1142 +               *flagp = flags;
1143 +               return(ret);
1144 +       }
1145 +
1146 +       if (!(flags&HASWIDTH) && op != '?')
1147 +               FAIL("*+ operand could be empty");
1148 +       *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1149 +
1150 +       if (op == '*' && (flags&SIMPLE))
1151 +               reginsert(g, STAR, ret);
1152 +       else if (op == '*') {
1153 +               /* Emit x* as (x&|), where & means "self". */
1154 +               reginsert(g, BRANCH, ret);                      /* Either x */
1155 +               regoptail(g, ret, regnode(g, BACK));            /* and loop */
1156 +               regoptail(g, ret, ret);                 /* back */
1157 +               regtail(g, ret, regnode(g, BRANCH));            /* or */
1158 +               regtail(g, ret, regnode(g, NOTHING));           /* null. */
1159 +       } else if (op == '+' && (flags&SIMPLE))
1160 +               reginsert(g, PLUS, ret);
1161 +       else if (op == '+') {
1162 +               /* Emit x+ as x(&|), where & means "self". */
1163 +               next = regnode(g, BRANCH);                      /* Either */
1164 +               regtail(g, ret, next);
1165 +               regtail(g, regnode(g, BACK), ret);              /* loop back */
1166 +               regtail(g, next, regnode(g, BRANCH));           /* or */
1167 +               regtail(g, ret, regnode(g, NOTHING));           /* null. */
1168 +       } else if (op == '?') {
1169 +               /* Emit x? as (x|) */
1170 +               reginsert(g, BRANCH, ret);                      /* Either x */
1171 +               regtail(g, ret, regnode(g, BRANCH));            /* or */
1172 +               next = regnode(g, NOTHING);             /* null. */
1173 +               regtail(g, ret, next);
1174 +               regoptail(g, ret, next);
1175 +       }
1176 +       g->regparse++;
1177 +       if (ISMULT(*g->regparse))
1178 +               FAIL("nested *?+");
1179 +
1180 +       return(ret);
1181 +}
1182 +
1183 +/*
1184 + - regatom - the lowest level
1185 + *
1186 + * Optimization:  gobbles an entire sequence of ordinary characters so that
1187 + * it can turn them into a single node, which is smaller to store and
1188 + * faster to run.  Backslashed characters are exceptions, each becoming a
1189 + * separate node; the code is simpler that way and it's not worth fixing.
1190 + */
1191 +static char *
1192 +regatom(struct match_globals *g, int *flagp)
1193 +{
1194 +       register char *ret;
1195 +       int flags;
1196 +
1197 +       *flagp = WORST;         /* Tentatively. */
1198 +
1199 +       switch (*g->regparse++) {
1200 +       case '^':
1201 +               ret = regnode(g, BOL);
1202 +               break;
1203 +       case '$':
1204 +               ret = regnode(g, EOL);
1205 +               break;
1206 +       case '.':
1207 +               ret = regnode(g, ANY);
1208 +               *flagp |= HASWIDTH|SIMPLE;
1209 +               break;
1210 +       case '[': {
1211 +                       register int class;
1212 +                       register int classend;
1213 +
1214 +                       if (*g->regparse == '^') {      /* Complement of range. */
1215 +                               ret = regnode(g, ANYBUT);
1216 +                               g->regparse++;
1217 +                       } else
1218 +                               ret = regnode(g, ANYOF);
1219 +                       if (*g->regparse == ']' || *g->regparse == '-')
1220 +                               regc(g, *g->regparse++);
1221 +                       while (*g->regparse != '\0' && *g->regparse != ']') {
1222 +                               if (*g->regparse == '-') {
1223 +                                       g->regparse++;
1224 +                                       if (*g->regparse == ']' || *g->regparse == '\0')
1225 +                                               regc(g, '-');
1226 +                                       else {
1227 +                                               class = UCHARAT(g->regparse-2)+1;
1228 +                                               classend = UCHARAT(g->regparse);
1229 +                                               if (class > classend+1)
1230 +                                                       FAIL("invalid [] range");
1231 +                                               for (; class <= classend; class++)
1232 +                                                       regc(g, class);
1233 +                                               g->regparse++;
1234 +                                       }
1235 +                               } else
1236 +                                       regc(g, *g->regparse++);
1237 +                       }
1238 +                       regc(g, '\0');
1239 +                       if (*g->regparse != ']')
1240 +                               FAIL("unmatched []");
1241 +                       g->regparse++;
1242 +                       *flagp |= HASWIDTH|SIMPLE;
1243 +               }
1244 +               break;
1245 +       case '(':
1246 +               ret = reg(g, 1, &flags);
1247 +               if (ret == NULL)
1248 +                       return(NULL);
1249 +               *flagp |= flags&(HASWIDTH|SPSTART);
1250 +               break;
1251 +       case '\0':
1252 +       case '|':
1253 +       case ')':
1254 +               FAIL("internal urp");   /* Supposed to be caught earlier. */
1255 +               break;
1256 +       case '?':
1257 +       case '+':
1258 +       case '*':
1259 +               FAIL("?+* follows nothing");
1260 +               break;
1261 +       case '\\':
1262 +               if (*g->regparse == '\0')
1263 +                       FAIL("trailing \\");
1264 +               ret = regnode(g, EXACTLY);
1265 +               regc(g, *g->regparse++);
1266 +               regc(g, '\0');
1267 +               *flagp |= HASWIDTH|SIMPLE;
1268 +               break;
1269 +       default: {
1270 +                       register int len;
1271 +                       register char ender;
1272 +
1273 +                       g->regparse--;
1274 +                       len = my_strcspn((const char *)g->regparse, (const char *)META);
1275 +                       if (len <= 0)
1276 +                               FAIL("internal disaster");
1277 +                       ender = *(g->regparse+len);
1278 +                       if (len > 1 && ISMULT(ender))
1279 +                               len--;          /* Back off clear of ?+* operand. */
1280 +                       *flagp |= HASWIDTH;
1281 +                       if (len == 1)
1282 +                               *flagp |= SIMPLE;
1283 +                       ret = regnode(g, EXACTLY);
1284 +                       while (len > 0) {
1285 +                               regc(g, *g->regparse++);
1286 +                               len--;
1287 +                       }
1288 +                       regc(g, '\0');
1289 +               }
1290 +               break;
1291 +       }
1292 +
1293 +       return(ret);
1294 +}
1295 +
1296 +/*
1297 + - regnode - emit a node
1298 + */
1299 +static char *                  /* Location. */
1300 +regnode(struct match_globals *g, char op)
1301 +{
1302 +       register char *ret;
1303 +       register char *ptr;
1304 +
1305 +       ret = g->regcode;
1306 +       if (ret == &g->regdummy) {
1307 +               g->regsize += 3;
1308 +               return(ret);
1309 +       }
1310 +
1311 +       ptr = ret;
1312 +       *ptr++ = op;
1313 +       *ptr++ = '\0';          /* Null "next" pointer. */
1314 +       *ptr++ = '\0';
1315 +       g->regcode = ptr;
1316 +
1317 +       return(ret);
1318 +}
1319 +
1320 +/*
1321 + - regc - emit (if appropriate) a byte of code
1322 + */
1323 +static void
1324 +regc(struct match_globals *g, char b)
1325 +{
1326 +       if (g->regcode != &g->regdummy)
1327 +               *g->regcode++ = b;
1328 +       else
1329 +               g->regsize++;
1330 +}
1331 +
1332 +/*
1333 + - reginsert - insert an operator in front of already-emitted operand
1334 + *
1335 + * Means relocating the operand.
1336 + */
1337 +static void
1338 +reginsert(struct match_globals *g, char op, char* opnd)
1339 +{
1340 +       register char *src;
1341 +       register char *dst;
1342 +       register char *place;
1343 +
1344 +       if (g->regcode == &g->regdummy) {
1345 +               g->regsize += 3;
1346 +               return;
1347 +       }
1348 +
1349 +       src = g->regcode;
1350 +       g->regcode += 3;
1351 +       dst = g->regcode;
1352 +       while (src > opnd)
1353 +               *--dst = *--src;
1354 +
1355 +       place = opnd;           /* Op node, where operand used to be. */
1356 +       *place++ = op;
1357 +       *place++ = '\0';
1358 +       *place++ = '\0';
1359 +}
1360 +
1361 +/*
1362 + - regtail - set the next-pointer at the end of a node chain
1363 + */
1364 +static void
1365 +regtail(struct match_globals *g, char *p, char *val)
1366 +{
1367 +       register char *scan;
1368 +       register char *temp;
1369 +       register int offset;
1370 +
1371 +       if (p == &g->regdummy)
1372 +               return;
1373 +
1374 +       /* Find last node. */
1375 +       scan = p;
1376 +       for (;;) {
1377 +               temp = regnext(g, scan);
1378 +               if (temp == NULL)
1379 +                       break;
1380 +               scan = temp;
1381 +       }
1382 +
1383 +       if (OP(scan) == BACK)
1384 +               offset = scan - val;
1385 +       else
1386 +               offset = val - scan;
1387 +       *(scan+1) = (offset>>8)&0377;
1388 +       *(scan+2) = offset&0377;
1389 +}
1390 +
1391 +/*
1392 + - regoptail - regtail on operand of first argument; nop if operandless
1393 + */
1394 +static void
1395 +regoptail(struct match_globals *g, char *p, char *val)
1396 +{
1397 +       /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1398 +       if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1399 +               return;
1400 +       regtail(g, OPERAND(p), val);
1401 +}
1402 +
1403 +/*
1404 + * regexec and friends
1405 + */
1406 +
1407 +
1408 +/*
1409 + * Forwards.
1410 + */
1411 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1412 +STATIC int regmatch(struct match_globals *g, char *prog);
1413 +STATIC int regrepeat(struct match_globals *g, char *p);
1414 +
1415 +#ifdef DEBUG
1416 +int regnarrate = 0;
1417 +void regdump();
1418 +STATIC char *regprop(char *op);
1419 +#endif
1420 +
1421 +/*
1422 + - regexec - match a regexp against a string
1423 + */
1424 +int
1425 +regexec(regexp *prog, char *string)
1426 +{
1427 +       register char *s;
1428 +       struct match_globals g;
1429 +
1430 +       /* Be paranoid... */
1431 +       if (prog == NULL || string == NULL) {
1432 +               printk("<3>Regexp: NULL parameter\n");
1433 +               return(0);
1434 +       }
1435 +
1436 +       /* Check validity of program. */
1437 +       if (UCHARAT(prog->program) != MAGIC) {
1438 +               printk("<3>Regexp: corrupted program\n");
1439 +               return(0);
1440 +       }
1441 +
1442 +       /* If there is a "must appear" string, look for it. */
1443 +       if (prog->regmust != NULL) {
1444 +               s = string;
1445 +               while ((s = strchr(s, prog->regmust[0])) != NULL) {
1446 +                       if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1447 +                               break;  /* Found it. */
1448 +                       s++;
1449 +               }
1450 +               if (s == NULL)  /* Not present. */
1451 +                       return(0);
1452 +       }
1453 +
1454 +       /* Mark beginning of line for ^ . */
1455 +       g.regbol = string;
1456 +
1457 +       /* Simplest case:  anchored match need be tried only once. */
1458 +       if (prog->reganch)
1459 +               return(regtry(&g, prog, string));
1460 +
1461 +       /* Messy cases:  unanchored match. */
1462 +       s = string;
1463 +       if (prog->regstart != '\0')
1464 +               /* We know what char it must start with. */
1465 +               while ((s = strchr(s, prog->regstart)) != NULL) {
1466 +                       if (regtry(&g, prog, s))
1467 +                               return(1);
1468 +                       s++;
1469 +               }
1470 +       else
1471 +               /* We don't -- general case. */
1472 +               do {
1473 +                       if (regtry(&g, prog, s))
1474 +                               return(1);
1475 +               } while (*s++ != '\0');
1476 +
1477 +       /* Failure. */
1478 +       return(0);
1479 +}
1480 +
1481 +/*
1482 + - regtry - try match at specific point
1483 + */
1484 +static int                     /* 0 failure, 1 success */
1485 +regtry(struct match_globals *g, regexp *prog, char *string)
1486 +{
1487 +       register int i;
1488 +       register char **sp;
1489 +       register char **ep;
1490 +
1491 +       g->reginput = string;
1492 +       g->regstartp = prog->startp;
1493 +       g->regendp = prog->endp;
1494 +
1495 +       sp = prog->startp;
1496 +       ep = prog->endp;
1497 +       for (i = NSUBEXP; i > 0; i--) {
1498 +               *sp++ = NULL;
1499 +               *ep++ = NULL;
1500 +       }
1501 +       if (regmatch(g, prog->program + 1)) {
1502 +               prog->startp[0] = string;
1503 +               prog->endp[0] = g->reginput;
1504 +               return(1);
1505 +       } else
1506 +               return(0);
1507 +}
1508 +
1509 +/*
1510 + - regmatch - main matching routine
1511 + *
1512 + * Conceptually the strategy is simple:  check to see whether the current
1513 + * node matches, call self recursively to see whether the rest matches,
1514 + * and then act accordingly.  In practice we make some effort to avoid
1515 + * recursion, in particular by going through "ordinary" nodes (that don't
1516 + * need to know whether the rest of the match failed) by a loop instead of
1517 + * by recursion.
1518 + */
1519 +static int                     /* 0 failure, 1 success */
1520 +regmatch(struct match_globals *g, char *prog)
1521 +{
1522 +       register char *scan = prog; /* Current node. */
1523 +       char *next;                 /* Next node. */
1524 +
1525 +#ifdef DEBUG
1526 +       if (scan != NULL && regnarrate)
1527 +               fprintf(stderr, "%s(\n", regprop(scan));
1528 +#endif
1529 +       while (scan != NULL) {
1530 +#ifdef DEBUG
1531 +               if (regnarrate)
1532 +                       fprintf(stderr, "%s...\n", regprop(scan));
1533 +#endif
1534 +               next = regnext(g, scan);
1535 +
1536 +               switch (OP(scan)) {
1537 +               case BOL:
1538 +                       if (g->reginput != g->regbol)
1539 +                               return(0);
1540 +                       break;
1541 +               case EOL:
1542 +                       if (*g->reginput != '\0')
1543 +                               return(0);
1544 +                       break;
1545 +               case ANY:
1546 +                       if (*g->reginput == '\0')
1547 +                               return(0);
1548 +                       g->reginput++;
1549 +                       break;
1550 +               case EXACTLY: {
1551 +                               register int len;
1552 +                               register char *opnd;
1553 +
1554 +                               opnd = OPERAND(scan);
1555 +                               /* Inline the first character, for speed. */
1556 +                               if (*opnd != *g->reginput)
1557 +                                       return(0);
1558 +                               len = strlen(opnd);
1559 +                               if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1560 +                                       return(0);
1561 +                               g->reginput += len;
1562 +                       }
1563 +                       break;
1564 +               case ANYOF:
1565 +                       if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1566 +                               return(0);
1567 +                       g->reginput++;
1568 +                       break;
1569 +               case ANYBUT:
1570 +                       if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1571 +                               return(0);
1572 +                       g->reginput++;
1573 +                       break;
1574 +               case NOTHING:
1575 +               case BACK:
1576 +                       break;
1577 +               case OPEN+1:
1578 +               case OPEN+2:
1579 +               case OPEN+3:
1580 +               case OPEN+4:
1581 +               case OPEN+5:
1582 +               case OPEN+6:
1583 +               case OPEN+7:
1584 +               case OPEN+8:
1585 +               case OPEN+9: {
1586 +                               register int no;
1587 +                               register char *save;
1588 +
1589 +                               no = OP(scan) - OPEN;
1590 +                               save = g->reginput;
1591 +
1592 +                               if (regmatch(g, next)) {
1593 +                                       /*
1594 +                                        * Don't set startp if some later
1595 +                                        * invocation of the same parentheses
1596 +                                        * already has.
1597 +                                        */
1598 +                                       if (g->regstartp[no] == NULL)
1599 +                                               g->regstartp[no] = save;
1600 +                                       return(1);
1601 +                               } else
1602 +                                       return(0);
1603 +                       }
1604 +                       break;
1605 +               case CLOSE+1:
1606 +               case CLOSE+2:
1607 +               case CLOSE+3:
1608 +               case CLOSE+4:
1609 +               case CLOSE+5:
1610 +               case CLOSE+6:
1611 +               case CLOSE+7:
1612 +               case CLOSE+8:
1613 +               case CLOSE+9:
1614 +                       {
1615 +                               register int no;
1616 +                               register char *save;
1617 +
1618 +                               no = OP(scan) - CLOSE;
1619 +                               save = g->reginput;
1620 +
1621 +                               if (regmatch(g, next)) {
1622 +                                       /*
1623 +                                        * Don't set endp if some later
1624 +                                        * invocation of the same parentheses
1625 +                                        * already has.
1626 +                                        */
1627 +                                       if (g->regendp[no] == NULL)
1628 +                                               g->regendp[no] = save;
1629 +                                       return(1);
1630 +                               } else
1631 +                                       return(0);
1632 +                       }
1633 +                       break;
1634 +               case BRANCH: {
1635 +                               register char *save;
1636 +
1637 +                               if (OP(next) != BRANCH)         /* No choice. */
1638 +                                       next = OPERAND(scan);   /* Avoid recursion. */
1639 +                               else {
1640 +                                       do {
1641 +                                               save = g->reginput;
1642 +                                               if (regmatch(g, OPERAND(scan)))
1643 +                                                       return(1);
1644 +                                               g->reginput = save;
1645 +                                               scan = regnext(g, scan);
1646 +                                       } while (scan != NULL && OP(scan) == BRANCH);
1647 +                                       return(0);
1648 +                                       /* NOTREACHED */
1649 +                               }
1650 +                       }
1651 +                       break;
1652 +               case STAR:
1653 +               case PLUS: {
1654 +                               register char nextch;
1655 +                               register int no;
1656 +                               register char *save;
1657 +                               register int min;
1658 +
1659 +                               /*
1660 +                                * Lookahead to avoid useless match attempts
1661 +                                * when we know what character comes next.
1662 +                                */
1663 +                               nextch = '\0';
1664 +                               if (OP(next) == EXACTLY)
1665 +                                       nextch = *OPERAND(next);
1666 +                               min = (OP(scan) == STAR) ? 0 : 1;
1667 +                               save = g->reginput;
1668 +                               no = regrepeat(g, OPERAND(scan));
1669 +                               while (no >= min) {
1670 +                                       /* If it could work, try it. */
1671 +                                       if (nextch == '\0' || *g->reginput == nextch)
1672 +                                               if (regmatch(g, next))
1673 +                                                       return(1);
1674 +                                       /* Couldn't or didn't -- back up. */
1675 +                                       no--;
1676 +                                       g->reginput = save + no;
1677 +                               }
1678 +                               return(0);
1679 +                       }
1680 +                       break;
1681 +               case END:
1682 +                       return(1);      /* Success! */
1683 +                       break;
1684 +               default:
1685 +                       printk("<3>Regexp: memory corruption\n");
1686 +                       return(0);
1687 +                       break;
1688 +               }
1689 +
1690 +               scan = next;
1691 +       }
1692 +
1693 +       /*
1694 +        * We get here only if there's trouble -- normally "case END" is
1695 +        * the terminating point.
1696 +        */
1697 +       printk("<3>Regexp: corrupted pointers\n");
1698 +       return(0);
1699 +}
1700 +
1701 +/*
1702 + - regrepeat - repeatedly match something simple, report how many
1703 + */
1704 +static int
1705 +regrepeat(struct match_globals *g, char *p)
1706 +{
1707 +       register int count = 0;
1708 +       register char *scan;
1709 +       register char *opnd;
1710 +
1711 +       scan = g->reginput;
1712 +       opnd = OPERAND(p);
1713 +       switch (OP(p)) {
1714 +       case ANY:
1715 +               count = strlen(scan);
1716 +               scan += count;
1717 +               break;
1718 +       case EXACTLY:
1719 +               while (*opnd == *scan) {
1720 +                       count++;
1721 +                       scan++;
1722 +               }
1723 +               break;
1724 +       case ANYOF:
1725 +               while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1726 +                       count++;
1727 +                       scan++;
1728 +               }
1729 +               break;
1730 +       case ANYBUT:
1731 +               while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1732 +                       count++;
1733 +                       scan++;
1734 +               }
1735 +               break;
1736 +       default:                /* Oh dear.  Called inappropriately. */
1737 +               printk("<3>Regexp: internal foulup\n");
1738 +               count = 0;      /* Best compromise. */
1739 +               break;
1740 +       }
1741 +       g->reginput = scan;
1742 +
1743 +       return(count);
1744 +}
1745 +
1746 +/*
1747 + - regnext - dig the "next" pointer out of a node
1748 + */
1749 +static char*
1750 +regnext(struct match_globals *g, char *p)
1751 +{
1752 +       register int offset;
1753 +
1754 +       if (p == &g->regdummy)
1755 +               return(NULL);
1756 +
1757 +       offset = NEXT(p);
1758 +       if (offset == 0)
1759 +               return(NULL);
1760 +
1761 +       if (OP(p) == BACK)
1762 +               return(p-offset);
1763 +       else
1764 +               return(p+offset);
1765 +}
1766 +
1767 +#ifdef DEBUG
1768 +
1769 +STATIC char *regprop();
1770 +
1771 +/*
1772 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1773 + */
1774 +void
1775 +regdump(regexp *r)
1776 +{
1777 +       register char *s;
1778 +       register char op = EXACTLY;     /* Arbitrary non-END op. */
1779 +       register char *next;
1780 +       /* extern char *strchr(); */
1781 +
1782 +
1783 +       s = r->program + 1;
1784 +       while (op != END) {     /* While that wasn't END last time... */
1785 +               op = OP(s);
1786 +               printf("%2d%s", s-r->program, regprop(s));      /* Where, what. */
1787 +               next = regnext(s);
1788 +               if (next == NULL)               /* Next ptr. */
1789 +                       printf("(0)");
1790 +               else
1791 +                       printf("(%d)", (s-r->program)+(next-s));
1792 +               s += 3;
1793 +               if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1794 +                       /* Literal string, where present. */
1795 +                       while (*s != '\0') {
1796 +                               putchar(*s);
1797 +                               s++;
1798 +                       }
1799 +                       s++;
1800 +               }
1801 +               putchar('\n');
1802 +       }
1803 +
1804 +       /* Header fields of interest. */
1805 +       if (r->regstart != '\0')
1806 +               printf("start `%c' ", r->regstart);
1807 +       if (r->reganch)
1808 +               printf("anchored ");
1809 +       if (r->regmust != NULL)
1810 +               printf("must have \"%s\"", r->regmust);
1811 +       printf("\n");
1812 +}
1813 +
1814 +/*
1815 + - regprop - printable representation of opcode
1816 + */
1817 +static char *
1818 +regprop(char *op)
1819 +{
1820 +#define BUFLEN 50
1821 +       register char *p;
1822 +       static char buf[BUFLEN];
1823 +
1824 +       strcpy(buf, ":");
1825 +
1826 +       switch (OP(op)) {
1827 +       case BOL:
1828 +               p = "BOL";
1829 +               break;
1830 +       case EOL:
1831 +               p = "EOL";
1832 +               break;
1833 +       case ANY:
1834 +               p = "ANY";
1835 +               break;
1836 +       case ANYOF:
1837 +               p = "ANYOF";
1838 +               break;
1839 +       case ANYBUT:
1840 +               p = "ANYBUT";
1841 +               break;
1842 +       case BRANCH:
1843 +               p = "BRANCH";
1844 +               break;
1845 +       case EXACTLY:
1846 +               p = "EXACTLY";
1847 +               break;
1848 +       case NOTHING:
1849 +               p = "NOTHING";
1850 +               break;
1851 +       case BACK:
1852 +               p = "BACK";
1853 +               break;
1854 +       case END:
1855 +               p = "END";
1856 +               break;
1857 +       case OPEN+1:
1858 +       case OPEN+2:
1859 +       case OPEN+3:
1860 +       case OPEN+4:
1861 +       case OPEN+5:
1862 +       case OPEN+6:
1863 +       case OPEN+7:
1864 +       case OPEN+8:
1865 +       case OPEN+9:
1866 +               snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1867 +               p = NULL;
1868 +               break;
1869 +       case CLOSE+1:
1870 +       case CLOSE+2:
1871 +       case CLOSE+3:
1872 +       case CLOSE+4:
1873 +       case CLOSE+5:
1874 +       case CLOSE+6:
1875 +       case CLOSE+7:
1876 +       case CLOSE+8:
1877 +       case CLOSE+9:
1878 +               snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1879 +               p = NULL;
1880 +               break;
1881 +       case STAR:
1882 +               p = "STAR";
1883 +               break;
1884 +       case PLUS:
1885 +               p = "PLUS";
1886 +               break;
1887 +       default:
1888 +               printk("<3>Regexp: corrupted opcode\n");
1889 +               break;
1890 +       }
1891 +       if (p != NULL)
1892 +               strncat(buf, p, BUFLEN-strlen(buf));
1893 +       return(buf);
1894 +}
1895 +#endif
1896 +
1897 +
1898 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h
1899 ===================================================================
1900 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1901 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h   2007-07-02 00:37:55.700427500 +0200
1902 @@ -0,0 +1,41 @@
1903 +/*
1904 + * Definitions etc. for regexp(3) routines.
1905 + *
1906 + * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
1907 + * not the System V one.
1908 + */
1909 +
1910 +#ifndef REGEXP_H
1911 +#define REGEXP_H
1912 +
1913 +
1914 +/*
1915 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1916 +which contains a version of this library, says:
1917 +
1918 + *
1919 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1920 + * will not work properly.
1921 + *
1922 +
1923 +However, it looks rather like this library is limited to 10.  If you think
1924 +otherwise, let us know.
1925 +*/
1926 +
1927 +#define NSUBEXP  10
1928 +typedef struct regexp {
1929 +       char *startp[NSUBEXP];
1930 +       char *endp[NSUBEXP];
1931 +       char regstart;          /* Internal use only. */
1932 +       char reganch;           /* Internal use only. */
1933 +       char *regmust;          /* Internal use only. */
1934 +       int regmlen;            /* Internal use only. */
1935 +       char program[1];        /* Unwarranted chumminess with compiler. */
1936 +} regexp;
1937 +
1938 +regexp * regcomp(char *exp, int *patternsize);
1939 +int regexec(regexp *prog, char *string);
1940 +void regsub(regexp *prog, char *source, char *dest);
1941 +void regerror(char *s);
1942 +
1943 +#endif
1944 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h
1945 ===================================================================
1946 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1947 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h 2007-07-02 00:37:55.724429000 +0200
1948 @@ -0,0 +1,5 @@
1949 +/*
1950 + * The first byte of the regexp internal "program" is actually this magic
1951 + * number; the start node begins in the second byte.
1952 + */
1953 +#define        MAGIC   0234
1954 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c
1955 ===================================================================
1956 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1957 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c   2007-07-02 00:37:55.752430750 +0200
1958 @@ -0,0 +1,95 @@
1959 +/*
1960 + * regsub
1961 + * @(#)regsub.c        1.3 of 2 April 86
1962 + *
1963 + *     Copyright (c) 1986 by University of Toronto.
1964 + *     Written by Henry Spencer.  Not derived from licensed software.
1965 + *
1966 + *     Permission is granted to anyone to use this software for any
1967 + *     purpose on any computer system, and to redistribute it freely,
1968 + *     subject to the following restrictions:
1969 + *
1970 + *     1. The author is not responsible for the consequences of use of
1971 + *             this software, no matter how awful, even if they arise
1972 + *             from defects in it.
1973 + *
1974 + *     2. The origin of this software must not be misrepresented, either
1975 + *             by explicit claim or by omission.
1976 + *
1977 + *     3. Altered versions must be plainly marked as such, and must not
1978 + *             be misrepresented as being the original software.
1979 + *
1980 + *
1981 + * This code was modified by Ethan Sommer to work within the kernel
1982 + * (it now uses kmalloc etc..)
1983 + *
1984 + */
1985 +#include "regexp.h"
1986 +#include "regmagic.h"
1987 +#include <linux/string.h>
1988 +
1989 +
1990 +#ifndef CHARBITS
1991 +#define        UCHARAT(p)      ((int)*(unsigned char *)(p))
1992 +#else
1993 +#define        UCHARAT(p)      ((int)*(p)&CHARBITS)
1994 +#endif
1995 +
1996 +#if 0
1997 +//void regerror(char * s)
1998 +//{
1999 +//        printk("regexp(3): %s", s);
2000 +//        /* NOTREACHED */
2001 +//}
2002 +#endif
2003 +
2004 +/*
2005 + - regsub - perform substitutions after a regexp match
2006 + */
2007 +void
2008 +regsub(regexp * prog, char * source, char * dest)
2009 +{
2010 +       register char *src;
2011 +       register char *dst;
2012 +       register char c;
2013 +       register int no;
2014 +       register int len;
2015 +       
2016 +       /* Not necessary and gcc doesn't like it -MLS */
2017 +       /*extern char *strncpy();*/
2018 +
2019 +       if (prog == NULL || source == NULL || dest == NULL) {
2020 +               regerror("NULL parm to regsub");
2021 +               return;
2022 +       }
2023 +       if (UCHARAT(prog->program) != MAGIC) {
2024 +               regerror("damaged regexp fed to regsub");
2025 +               return;
2026 +       }
2027 +
2028 +       src = source;
2029 +       dst = dest;
2030 +       while ((c = *src++) != '\0') {
2031 +               if (c == '&')
2032 +                       no = 0;
2033 +               else if (c == '\\' && '0' <= *src && *src <= '9')
2034 +                       no = *src++ - '0';
2035 +               else
2036 +                       no = -1;
2037 +
2038 +               if (no < 0) {   /* Ordinary character. */
2039 +                       if (c == '\\' && (*src == '\\' || *src == '&'))
2040 +                               c = *src++;
2041 +                       *dst++ = c;
2042 +               } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2043 +                       len = prog->endp[no] - prog->startp[no];
2044 +                       (void) strncpy(dst, prog->startp[no], len);
2045 +                       dst += len;
2046 +                       if (len != 0 && *(dst-1) == '\0') {     /* strncpy hit NUL. */
2047 +                               regerror("damaged match string");
2048 +                               return;
2049 +                       }
2050 +               }
2051 +       }
2052 +       *dst++ = '\0';
2053 +}
2054 Index: linux-2.6.21.5/include/net/netfilter/nf_conntrack.h
2055 ===================================================================
2056 --- linux-2.6.21.5.orig/include/net/netfilter/nf_conntrack.h    2007-07-02 00:49:22.815369500 +0200
2057 +++ linux-2.6.21.5/include/net/netfilter/nf_conntrack.h 2007-07-02 00:56:21.413530250 +0200
2058 @@ -128,6 +128,21 @@
2059         u_int32_t secmark;
2060  #endif
2061  
2062 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
2063 +       struct {
2064 +               /*
2065 +                * e.g. "http". NULL before decision. "unknown" after decision
2066 +                * if no match.
2067 +                */
2068 +               char *app_proto;
2069 +               /*
2070 +                * application layer data so far. NULL after match decision.
2071 +                */
2072 +               char *app_data;
2073 +               unsigned int app_data_len;
2074 +       } layer7;
2075 +#endif
2076 +
2077         /* Storage reserved for other modules: */
2078         union nf_conntrack_proto proto;
2079