ixp4xx/config: Enabled PPP for bluetooth dund usage (for Treo650 bluetooth access...
[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,583 @@
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 +       .family = AF_INET,
546 +       .me = THIS_MODULE
547 +};
548 +
549 +/* taken from drivers/video/modedb.c */
550 +static int my_atoi(const char *s)
551 +{
552 +       int val = 0;
553 +
554 +       for (;; s++) {
555 +               switch (*s) {
556 +                       case '0'...'9':
557 +                       val = 10*val+(*s-'0');
558 +                       break;
559 +               default:
560 +                       return val;
561 +               }
562 +       }
563 +}
564 +
565 +/* write out num_packets to userland. */
566 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
567 +                    int* eof, void * data)
568 +{
569 +       if(num_packets > 99 && net_ratelimit())
570 +               printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
571 +
572 +       page[0] = num_packets/10 + '0';
573 +       page[1] = num_packets%10 + '0';
574 +       page[2] = '\n';
575 +       page[3] = '\0';
576 +
577 +       *eof=1;
578 +
579 +       return 3;
580 +}
581 +
582 +/* Read in num_packets from userland */
583 +static int layer7_write_proc(struct file* file, const char* buffer,
584 +                     unsigned long count, void *data)
585 +{
586 +       char * foo = kmalloc(count, GFP_ATOMIC);
587 +
588 +       if(!foo){
589 +               if (net_ratelimit())
590 +                       printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
591 +               return count;
592 +       }
593 +
594 +       if(copy_from_user(foo, buffer, count)) {
595 +               return -EFAULT;
596 +       }
597 +
598 +
599 +       num_packets = my_atoi(foo);
600 +       kfree (foo);
601 +
602 +       /* This has an arbitrary limit to make the math easier. I'm lazy.
603 +       But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
604 +       if(num_packets > 99) {
605 +               printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
606 +               num_packets = 99;
607 +       } else if(num_packets < 1) {
608 +               printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
609 +               num_packets = 1;
610 +       }
611 +
612 +       return count;
613 +}
614 +
615 +/* register the proc file */
616 +static void layer7_init_proc(void)
617 +{
618 +       struct proc_dir_entry* entry;
619 +       entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
620 +       entry->read_proc = layer7_read_proc;
621 +       entry->write_proc = layer7_write_proc;
622 +}
623 +
624 +static void layer7_cleanup_proc(void)
625 +{
626 +       remove_proc_entry("layer7_numpackets", proc_net);
627 +}
628 +
629 +static int __init ipt_layer7_init(void)
630 +{
631 +       need_conntrack();
632 +
633 +       layer7_init_proc();
634 +       if(maxdatalen < 1) {
635 +               printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
636 +               maxdatalen = 1;
637 +       }
638 +       /* This is not a hard limit.  It's just here to prevent people from
639 +       bringing their slow machines to a grinding halt. */
640 +       else if(maxdatalen > 65536) {
641 +               printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
642 +               maxdatalen = 65536;
643 +       }
644 +       return xt_register_match(&layer7_match);
645 +}
646 +
647 +static void __exit ipt_layer7_fini(void)
648 +{
649 +       layer7_cleanup_proc();
650 +       xt_unregister_match(&layer7_match);
651 +}
652 +
653 +module_init(ipt_layer7_init);
654 +module_exit(ipt_layer7_fini);
655 Index: linux-2.6.21.5/net/ipv4/netfilter/Kconfig
656 ===================================================================
657 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Kconfig      2007-07-02 00:37:53.456287250 +0200
658 +++ linux-2.6.21.5/net/ipv4/netfilter/Kconfig   2007-07-02 01:21:17.231013000 +0200
659 @@ -245,6 +245,24 @@
660  
661           To compile it as a module, choose M here.  If unsure, say N.
662  
663 +config IP_NF_MATCH_LAYER7
664 +       tristate "Layer 7 match support (EXPERIMENTAL)"
665 +       depends on IP_NF_IPTABLES && NF_CT_ACCT && NF_CONNTRACK && EXPERIMENTAL
666 +       help
667 +         Say Y if you want to be able to classify connections (and their
668 +         packets) based on regular expression matching of their application
669 +         layer data.   This is one way to classify applications such as
670 +         peer-to-peer filesharing systems that do not always use the same
671 +         port.
672 +
673 +         To compile it as a module, choose M here.  If unsure, say N.
674 +
675 +config IP_NF_MATCH_LAYER7_DEBUG
676 +       bool "Layer 7 debugging output"
677 +       depends on IP_NF_MATCH_LAYER7
678 +       help
679 +         Say Y to get lots of debugging output.
680 +
681  config IP_NF_MATCH_TOS
682         tristate "TOS match support"
683         depends on IP_NF_IPTABLES
684 Index: linux-2.6.21.5/net/ipv4/netfilter/Makefile
685 ===================================================================
686 --- linux-2.6.21.5.orig/net/ipv4/netfilter/Makefile     2007-07-02 00:37:53.464287750 +0200
687 +++ linux-2.6.21.5/net/ipv4/netfilter/Makefile  2007-07-02 00:43:58.191081750 +0200
688 @@ -92,6 +92,8 @@
689  obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
690  obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
691  
692 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
693 +
694  # targets
695  obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
696  obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
697 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c
698 ===================================================================
699 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
700 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.c   2007-07-02 00:37:55.648424250 +0200
701 @@ -0,0 +1,1197 @@
702 +/*
703 + * regcomp and regexec -- regsub and regerror are elsewhere
704 + * @(#)regexp.c        1.3 of 18 April 87
705 + *
706 + *     Copyright (c) 1986 by University of Toronto.
707 + *     Written by Henry Spencer.  Not derived from licensed software.
708 + *
709 + *     Permission is granted to anyone to use this software for any
710 + *     purpose on any computer system, and to redistribute it freely,
711 + *     subject to the following restrictions:
712 + *
713 + *     1. The author is not responsible for the consequences of use of
714 + *             this software, no matter how awful, even if they arise
715 + *             from defects in it.
716 + *
717 + *     2. The origin of this software must not be misrepresented, either
718 + *             by explicit claim or by omission.
719 + *
720 + *     3. Altered versions must be plainly marked as such, and must not
721 + *             be misrepresented as being the original software.
722 + *
723 + * Beware that some of this code is subtly aware of the way operator
724 + * precedence is structured in regular expressions.  Serious changes in
725 + * regular-expression syntax might require a total rethink.
726 + *
727 + * This code was modified by Ethan Sommer to work within the kernel
728 + * (it now uses kmalloc etc..)
729 + *
730 + * Modified slightly by Matthew Strait to use more modern C.
731 + */
732 +
733 +#include "regexp.h"
734 +#include "regmagic.h"
735 +
736 +/* added by ethan and matt.  Lets it work in both kernel and user space.
737 +(So iptables can use it, for instance.)  Yea, it goes both ways... */
738 +#if __KERNEL__
739 +  #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
740 +#else
741 +  #define printk(format,args...) printf(format,##args)
742 +#endif
743 +
744 +void regerror(char * s)
745 +{
746 +        printk("<3>Regexp: %s\n", s);
747 +        /* NOTREACHED */
748 +}
749 +
750 +/*
751 + * The "internal use only" fields in regexp.h are present to pass info from
752 + * compile to execute that permits the execute phase to run lots faster on
753 + * simple cases.  They are:
754 + *
755 + * regstart    char that must begin a match; '\0' if none obvious
756 + * reganch     is the match anchored (at beginning-of-line only)?
757 + * regmust     string (pointer into program) that match must include, or NULL
758 + * regmlen     length of regmust string
759 + *
760 + * Regstart and reganch permit very fast decisions on suitable starting points
761 + * for a match, cutting down the work a lot.  Regmust permits fast rejection
762 + * of lines that cannot possibly match.  The regmust tests are costly enough
763 + * that regcomp() supplies a regmust only if the r.e. contains something
764 + * potentially expensive (at present, the only such thing detected is * or +
765 + * at the start of the r.e., which can involve a lot of backup).  Regmlen is
766 + * supplied because the test in regexec() needs it and regcomp() is computing
767 + * it anyway.
768 + */
769 +
770 +/*
771 + * Structure for regexp "program".  This is essentially a linear encoding
772 + * of a nondeterministic finite-state machine (aka syntax charts or
773 + * "railroad normal form" in parsing technology).  Each node is an opcode
774 + * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
775 + * all nodes except BRANCH implement concatenation; a "next" pointer with
776 + * a BRANCH on both ends of it is connecting two alternatives.  (Here we
777 + * have one of the subtle syntax dependencies:  an individual BRANCH (as
778 + * opposed to a collection of them) is never concatenated with anything
779 + * because of operator precedence.)  The operand of some types of node is
780 + * a literal string; for others, it is a node leading into a sub-FSM.  In
781 + * particular, the operand of a BRANCH node is the first node of the branch.
782 + * (NB this is *not* a tree structure:  the tail of the branch connects
783 + * to the thing following the set of BRANCHes.)  The opcodes are:
784 + */
785 +
786 +/* definition  number  opnd?   meaning */
787 +#define        END     0       /* no   End of program. */
788 +#define        BOL     1       /* no   Match "" at beginning of line. */
789 +#define        EOL     2       /* no   Match "" at end of line. */
790 +#define        ANY     3       /* no   Match any one character. */
791 +#define        ANYOF   4       /* str  Match any character in this string. */
792 +#define        ANYBUT  5       /* str  Match any character not in this string. */
793 +#define        BRANCH  6       /* node Match this alternative, or the next... */
794 +#define        BACK    7       /* no   Match "", "next" ptr points backward. */
795 +#define        EXACTLY 8       /* str  Match this string. */
796 +#define        NOTHING 9       /* no   Match empty string. */
797 +#define        STAR    10      /* node Match this (simple) thing 0 or more times. */
798 +#define        PLUS    11      /* node Match this (simple) thing 1 or more times. */
799 +#define        OPEN    20      /* no   Mark this point in input as start of #n. */
800 +                       /*      OPEN+1 is number 1, etc. */
801 +#define        CLOSE   30      /* no   Analogous to OPEN. */
802 +
803 +/*
804 + * Opcode notes:
805 + *
806 + * BRANCH      The set of branches constituting a single choice are hooked
807 + *             together with their "next" pointers, since precedence prevents
808 + *             anything being concatenated to any individual branch.  The
809 + *             "next" pointer of the last BRANCH in a choice points to the
810 + *             thing following the whole choice.  This is also where the
811 + *             final "next" pointer of each individual branch points; each
812 + *             branch starts with the operand node of a BRANCH node.
813 + *
814 + * BACK                Normal "next" pointers all implicitly point forward; BACK
815 + *             exists to make loop structures possible.
816 + *
817 + * STAR,PLUS   '?', and complex '*' and '+', are implemented as circular
818 + *             BRANCH structures using BACK.  Simple cases (one character
819 + *             per match) are implemented with STAR and PLUS for speed
820 + *             and to minimize recursive plunges.
821 + *
822 + * OPEN,CLOSE  ...are numbered at compile time.
823 + */
824 +
825 +/*
826 + * A node is one char of opcode followed by two chars of "next" pointer.
827 + * "Next" pointers are stored as two 8-bit pieces, high order first.  The
828 + * value is a positive offset from the opcode of the node containing it.
829 + * An operand, if any, simply follows the node.  (Note that much of the
830 + * code generation knows about this implicit relationship.)
831 + *
832 + * Using two bytes for the "next" pointer is vast overkill for most things,
833 + * but allows patterns to get big without disasters.
834 + */
835 +#define        OP(p)   (*(p))
836 +#define        NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
837 +#define        OPERAND(p)      ((p) + 3)
838 +
839 +/*
840 + * See regmagic.h for one further detail of program structure.
841 + */
842 +
843 +
844 +/*
845 + * Utility definitions.
846 + */
847 +#ifndef CHARBITS
848 +#define        UCHARAT(p)      ((int)*(unsigned char *)(p))
849 +#else
850 +#define        UCHARAT(p)      ((int)*(p)&CHARBITS)
851 +#endif
852 +
853 +#define        FAIL(m) { regerror(m); return(NULL); }
854 +#define        ISMULT(c)       ((c) == '*' || (c) == '+' || (c) == '?')
855 +#define        META    "^$.[()|?+*\\"
856 +
857 +/*
858 + * Flags to be passed up and down.
859 + */
860 +#define        HASWIDTH        01      /* Known never to match null string. */
861 +#define        SIMPLE          02      /* Simple enough to be STAR/PLUS operand. */
862 +#define        SPSTART         04      /* Starts with * or +. */
863 +#define        WORST           0       /* Worst case. */
864 +
865 +/*
866 + * Global work variables for regcomp().
867 + */
868 +struct match_globals {
869 +char *reginput;                /* String-input pointer. */
870 +char *regbol;          /* Beginning of input, for ^ check. */
871 +char **regstartp;      /* Pointer to startp array. */
872 +char **regendp;                /* Ditto for endp. */
873 +char *regparse;                /* Input-scan pointer. */
874 +int regnpar;           /* () count. */
875 +char regdummy;
876 +char *regcode;         /* Code-emit pointer; &regdummy = don't. */
877 +long regsize;          /* Code size. */
878 +};
879 +
880 +/*
881 + * Forward declarations for regcomp()'s friends.
882 + */
883 +#ifndef STATIC
884 +#define        STATIC  static
885 +#endif
886 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
887 +STATIC char *regbranch(struct match_globals *g, int *flagp);
888 +STATIC char *regpiece(struct match_globals *g, int *flagp);
889 +STATIC char *regatom(struct match_globals *g, int *flagp);
890 +STATIC char *regnode(struct match_globals *g, char op);
891 +STATIC char *regnext(struct match_globals *g, char *p);
892 +STATIC void regc(struct match_globals *g, char b);
893 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
894 +STATIC void regtail(struct match_globals *g, char *p, char *val);
895 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
896 +
897 +
898 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
899 +{
900 +        char *scan1;
901 +        char *scan2;
902 +        int count;
903 +
904 +        count = 0;
905 +        for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
906 +                for (scan2 = (char *)s2; *scan2 != '\0';)       /* ++ moved down. */
907 +                        if (*scan1 == *scan2++)
908 +                                return(count);
909 +                count++;
910 +        }
911 +        return(count);
912 +}
913 +
914 +/*
915 + - regcomp - compile a regular expression into internal code
916 + *
917 + * We can't allocate space until we know how big the compiled form will be,
918 + * but we can't compile it (and thus know how big it is) until we've got a
919 + * place to put the code.  So we cheat:  we compile it twice, once with code
920 + * generation turned off and size counting turned on, and once "for real".
921 + * This also means that we don't allocate space until we are sure that the
922 + * thing really will compile successfully, and we never have to move the
923 + * code and thus invalidate pointers into it.  (Note that it has to be in
924 + * one piece because free() must be able to free it all.)
925 + *
926 + * Beware that the optimization-preparation code in here knows about some
927 + * of the structure of the compiled regexp.
928 + */
929 +regexp *
930 +regcomp(char *exp,int *patternsize)
931 +{
932 +       register regexp *r;
933 +       register char *scan;
934 +       register char *longest;
935 +       register int len;
936 +       int flags;
937 +       struct match_globals g;
938 +       
939 +       /* commented out by ethan
940 +          extern char *malloc();
941 +       */
942 +
943 +       if (exp == NULL)
944 +               FAIL("NULL argument");
945 +
946 +       /* First pass: determine size, legality. */
947 +       g.regparse = exp;
948 +       g.regnpar = 1;
949 +       g.regsize = 0L;
950 +       g.regcode = &g.regdummy;
951 +       regc(&g, MAGIC);
952 +       if (reg(&g, 0, &flags) == NULL)
953 +               return(NULL);
954 +
955 +       /* Small enough for pointer-storage convention? */
956 +       if (g.regsize >= 32767L)                /* Probably could be 65535L. */
957 +               FAIL("regexp too big");
958 +
959 +       /* Allocate space. */
960 +       *patternsize=sizeof(regexp) + (unsigned)g.regsize;
961 +       r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
962 +       if (r == NULL)
963 +               FAIL("out of space");
964 +
965 +       /* Second pass: emit code. */
966 +       g.regparse = exp;
967 +       g.regnpar = 1;
968 +       g.regcode = r->program;
969 +       regc(&g, MAGIC);
970 +       if (reg(&g, 0, &flags) == NULL)
971 +               return(NULL);
972 +
973 +       /* Dig out information for optimizations. */
974 +       r->regstart = '\0';     /* Worst-case defaults. */
975 +       r->reganch = 0;
976 +       r->regmust = NULL;
977 +       r->regmlen = 0;
978 +       scan = r->program+1;                    /* First BRANCH. */
979 +       if (OP(regnext(&g, scan)) == END) {             /* Only one top-level choice. */
980 +               scan = OPERAND(scan);
981 +
982 +               /* Starting-point info. */
983 +               if (OP(scan) == EXACTLY)
984 +                       r->regstart = *OPERAND(scan);
985 +               else if (OP(scan) == BOL)
986 +                       r->reganch++;
987 +
988 +               /*
989 +                * If there's something expensive in the r.e., find the
990 +                * longest literal string that must appear and make it the
991 +                * regmust.  Resolve ties in favor of later strings, since
992 +                * the regstart check works with the beginning of the r.e.
993 +                * and avoiding duplication strengthens checking.  Not a
994 +                * strong reason, but sufficient in the absence of others.
995 +                */
996 +               if (flags&SPSTART) {
997 +                       longest = NULL;
998 +                       len = 0;
999 +                       for (; scan != NULL; scan = regnext(&g, scan))
1000 +                               if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1001 +                                       longest = OPERAND(scan);
1002 +                                       len = strlen(OPERAND(scan));
1003 +                               }
1004 +                       r->regmust = longest;
1005 +                       r->regmlen = len;
1006 +               }
1007 +       }
1008 +
1009 +       return(r);
1010 +}
1011 +
1012 +/*
1013 + - reg - regular expression, i.e. main body or parenthesized thing
1014 + *
1015 + * Caller must absorb opening parenthesis.
1016 + *
1017 + * Combining parenthesis handling with the base level of regular expression
1018 + * is a trifle forced, but the need to tie the tails of the branches to what
1019 + * follows makes it hard to avoid.
1020 + */
1021 +static char *
1022 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1023 +{
1024 +       register char *ret;
1025 +       register char *br;
1026 +       register char *ender;
1027 +       register int parno = 0; /* 0 makes gcc happy */
1028 +       int flags;
1029 +
1030 +       *flagp = HASWIDTH;      /* Tentatively. */
1031 +
1032 +       /* Make an OPEN node, if parenthesized. */
1033 +       if (paren) {
1034 +               if (g->regnpar >= NSUBEXP)
1035 +                       FAIL("too many ()");
1036 +               parno = g->regnpar;
1037 +               g->regnpar++;
1038 +               ret = regnode(g, OPEN+parno);
1039 +       } else
1040 +               ret = NULL;
1041 +
1042 +       /* Pick up the branches, linking them together. */
1043 +       br = regbranch(g, &flags);
1044 +       if (br == NULL)
1045 +               return(NULL);
1046 +       if (ret != NULL)
1047 +               regtail(g, ret, br);    /* OPEN -> first. */
1048 +       else
1049 +               ret = br;
1050 +       if (!(flags&HASWIDTH))
1051 +               *flagp &= ~HASWIDTH;
1052 +       *flagp |= flags&SPSTART;
1053 +       while (*g->regparse == '|') {
1054 +               g->regparse++;
1055 +               br = regbranch(g, &flags);
1056 +               if (br == NULL)
1057 +                       return(NULL);
1058 +               regtail(g, ret, br);    /* BRANCH -> BRANCH. */
1059 +               if (!(flags&HASWIDTH))
1060 +                       *flagp &= ~HASWIDTH;
1061 +               *flagp |= flags&SPSTART;
1062 +       }
1063 +
1064 +       /* Make a closing node, and hook it on the end. */
1065 +       ender = regnode(g, (paren) ? CLOSE+parno : END);        
1066 +       regtail(g, ret, ender);
1067 +
1068 +       /* Hook the tails of the branches to the closing node. */
1069 +       for (br = ret; br != NULL; br = regnext(g, br))
1070 +               regoptail(g, br, ender);
1071 +
1072 +       /* Check for proper termination. */
1073 +       if (paren && *g->regparse++ != ')') {
1074 +               FAIL("unmatched ()");
1075 +       } else if (!paren && *g->regparse != '\0') {
1076 +               if (*g->regparse == ')') {
1077 +                       FAIL("unmatched ()");
1078 +               } else
1079 +                       FAIL("junk on end");    /* "Can't happen". */
1080 +               /* NOTREACHED */
1081 +       }
1082 +
1083 +       return(ret);
1084 +}
1085 +
1086 +/*
1087 + - regbranch - one alternative of an | operator
1088 + *
1089 + * Implements the concatenation operator.
1090 + */
1091 +static char *
1092 +regbranch(struct match_globals *g, int *flagp)
1093 +{
1094 +       register char *ret;
1095 +       register char *chain;
1096 +       register char *latest;
1097 +       int flags;
1098 +
1099 +       *flagp = WORST;         /* Tentatively. */
1100 +
1101 +       ret = regnode(g, BRANCH);
1102 +       chain = NULL;
1103 +       while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1104 +               latest = regpiece(g, &flags);
1105 +               if (latest == NULL)
1106 +                       return(NULL);
1107 +               *flagp |= flags&HASWIDTH;
1108 +               if (chain == NULL)      /* First piece. */
1109 +                       *flagp |= flags&SPSTART;
1110 +               else
1111 +                       regtail(g, chain, latest);
1112 +               chain = latest;
1113 +       }
1114 +       if (chain == NULL)      /* Loop ran zero times. */
1115 +               (void) regnode(g, NOTHING);
1116 +
1117 +       return(ret);
1118 +}
1119 +
1120 +/*
1121 + - regpiece - something followed by possible [*+?]
1122 + *
1123 + * Note that the branching code sequences used for ? and the general cases
1124 + * of * and + are somewhat optimized:  they use the same NOTHING node as
1125 + * both the endmarker for their branch list and the body of the last branch.
1126 + * It might seem that this node could be dispensed with entirely, but the
1127 + * endmarker role is not redundant.
1128 + */
1129 +static char *
1130 +regpiece(struct match_globals *g, int *flagp)
1131 +{
1132 +       register char *ret;
1133 +       register char op;
1134 +       register char *next;
1135 +       int flags;
1136 +
1137 +       ret = regatom(g, &flags);
1138 +       if (ret == NULL)
1139 +               return(NULL);
1140 +
1141 +       op = *g->regparse;
1142 +       if (!ISMULT(op)) {
1143 +               *flagp = flags;
1144 +               return(ret);
1145 +       }
1146 +
1147 +       if (!(flags&HASWIDTH) && op != '?')
1148 +               FAIL("*+ operand could be empty");
1149 +       *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1150 +
1151 +       if (op == '*' && (flags&SIMPLE))
1152 +               reginsert(g, STAR, ret);
1153 +       else if (op == '*') {
1154 +               /* Emit x* as (x&|), where & means "self". */
1155 +               reginsert(g, BRANCH, ret);                      /* Either x */
1156 +               regoptail(g, ret, regnode(g, BACK));            /* and loop */
1157 +               regoptail(g, ret, ret);                 /* back */
1158 +               regtail(g, ret, regnode(g, BRANCH));            /* or */
1159 +               regtail(g, ret, regnode(g, NOTHING));           /* null. */
1160 +       } else if (op == '+' && (flags&SIMPLE))
1161 +               reginsert(g, PLUS, ret);
1162 +       else if (op == '+') {
1163 +               /* Emit x+ as x(&|), where & means "self". */
1164 +               next = regnode(g, BRANCH);                      /* Either */
1165 +               regtail(g, ret, next);
1166 +               regtail(g, regnode(g, BACK), ret);              /* loop back */
1167 +               regtail(g, next, regnode(g, BRANCH));           /* or */
1168 +               regtail(g, ret, regnode(g, NOTHING));           /* null. */
1169 +       } else if (op == '?') {
1170 +               /* Emit x? as (x|) */
1171 +               reginsert(g, BRANCH, ret);                      /* Either x */
1172 +               regtail(g, ret, regnode(g, BRANCH));            /* or */
1173 +               next = regnode(g, NOTHING);             /* null. */
1174 +               regtail(g, ret, next);
1175 +               regoptail(g, ret, next);
1176 +       }
1177 +       g->regparse++;
1178 +       if (ISMULT(*g->regparse))
1179 +               FAIL("nested *?+");
1180 +
1181 +       return(ret);
1182 +}
1183 +
1184 +/*
1185 + - regatom - the lowest level
1186 + *
1187 + * Optimization:  gobbles an entire sequence of ordinary characters so that
1188 + * it can turn them into a single node, which is smaller to store and
1189 + * faster to run.  Backslashed characters are exceptions, each becoming a
1190 + * separate node; the code is simpler that way and it's not worth fixing.
1191 + */
1192 +static char *
1193 +regatom(struct match_globals *g, int *flagp)
1194 +{
1195 +       register char *ret;
1196 +       int flags;
1197 +
1198 +       *flagp = WORST;         /* Tentatively. */
1199 +
1200 +       switch (*g->regparse++) {
1201 +       case '^':
1202 +               ret = regnode(g, BOL);
1203 +               break;
1204 +       case '$':
1205 +               ret = regnode(g, EOL);
1206 +               break;
1207 +       case '.':
1208 +               ret = regnode(g, ANY);
1209 +               *flagp |= HASWIDTH|SIMPLE;
1210 +               break;
1211 +       case '[': {
1212 +                       register int class;
1213 +                       register int classend;
1214 +
1215 +                       if (*g->regparse == '^') {      /* Complement of range. */
1216 +                               ret = regnode(g, ANYBUT);
1217 +                               g->regparse++;
1218 +                       } else
1219 +                               ret = regnode(g, ANYOF);
1220 +                       if (*g->regparse == ']' || *g->regparse == '-')
1221 +                               regc(g, *g->regparse++);
1222 +                       while (*g->regparse != '\0' && *g->regparse != ']') {
1223 +                               if (*g->regparse == '-') {
1224 +                                       g->regparse++;
1225 +                                       if (*g->regparse == ']' || *g->regparse == '\0')
1226 +                                               regc(g, '-');
1227 +                                       else {
1228 +                                               class = UCHARAT(g->regparse-2)+1;
1229 +                                               classend = UCHARAT(g->regparse);
1230 +                                               if (class > classend+1)
1231 +                                                       FAIL("invalid [] range");
1232 +                                               for (; class <= classend; class++)
1233 +                                                       regc(g, class);
1234 +                                               g->regparse++;
1235 +                                       }
1236 +                               } else
1237 +                                       regc(g, *g->regparse++);
1238 +                       }
1239 +                       regc(g, '\0');
1240 +                       if (*g->regparse != ']')
1241 +                               FAIL("unmatched []");
1242 +                       g->regparse++;
1243 +                       *flagp |= HASWIDTH|SIMPLE;
1244 +               }
1245 +               break;
1246 +       case '(':
1247 +               ret = reg(g, 1, &flags);
1248 +               if (ret == NULL)
1249 +                       return(NULL);
1250 +               *flagp |= flags&(HASWIDTH|SPSTART);
1251 +               break;
1252 +       case '\0':
1253 +       case '|':
1254 +       case ')':
1255 +               FAIL("internal urp");   /* Supposed to be caught earlier. */
1256 +               break;
1257 +       case '?':
1258 +       case '+':
1259 +       case '*':
1260 +               FAIL("?+* follows nothing");
1261 +               break;
1262 +       case '\\':
1263 +               if (*g->regparse == '\0')
1264 +                       FAIL("trailing \\");
1265 +               ret = regnode(g, EXACTLY);
1266 +               regc(g, *g->regparse++);
1267 +               regc(g, '\0');
1268 +               *flagp |= HASWIDTH|SIMPLE;
1269 +               break;
1270 +       default: {
1271 +                       register int len;
1272 +                       register char ender;
1273 +
1274 +                       g->regparse--;
1275 +                       len = my_strcspn((const char *)g->regparse, (const char *)META);
1276 +                       if (len <= 0)
1277 +                               FAIL("internal disaster");
1278 +                       ender = *(g->regparse+len);
1279 +                       if (len > 1 && ISMULT(ender))
1280 +                               len--;          /* Back off clear of ?+* operand. */
1281 +                       *flagp |= HASWIDTH;
1282 +                       if (len == 1)
1283 +                               *flagp |= SIMPLE;
1284 +                       ret = regnode(g, EXACTLY);
1285 +                       while (len > 0) {
1286 +                               regc(g, *g->regparse++);
1287 +                               len--;
1288 +                       }
1289 +                       regc(g, '\0');
1290 +               }
1291 +               break;
1292 +       }
1293 +
1294 +       return(ret);
1295 +}
1296 +
1297 +/*
1298 + - regnode - emit a node
1299 + */
1300 +static char *                  /* Location. */
1301 +regnode(struct match_globals *g, char op)
1302 +{
1303 +       register char *ret;
1304 +       register char *ptr;
1305 +
1306 +       ret = g->regcode;
1307 +       if (ret == &g->regdummy) {
1308 +               g->regsize += 3;
1309 +               return(ret);
1310 +       }
1311 +
1312 +       ptr = ret;
1313 +       *ptr++ = op;
1314 +       *ptr++ = '\0';          /* Null "next" pointer. */
1315 +       *ptr++ = '\0';
1316 +       g->regcode = ptr;
1317 +
1318 +       return(ret);
1319 +}
1320 +
1321 +/*
1322 + - regc - emit (if appropriate) a byte of code
1323 + */
1324 +static void
1325 +regc(struct match_globals *g, char b)
1326 +{
1327 +       if (g->regcode != &g->regdummy)
1328 +               *g->regcode++ = b;
1329 +       else
1330 +               g->regsize++;
1331 +}
1332 +
1333 +/*
1334 + - reginsert - insert an operator in front of already-emitted operand
1335 + *
1336 + * Means relocating the operand.
1337 + */
1338 +static void
1339 +reginsert(struct match_globals *g, char op, char* opnd)
1340 +{
1341 +       register char *src;
1342 +       register char *dst;
1343 +       register char *place;
1344 +
1345 +       if (g->regcode == &g->regdummy) {
1346 +               g->regsize += 3;
1347 +               return;
1348 +       }
1349 +
1350 +       src = g->regcode;
1351 +       g->regcode += 3;
1352 +       dst = g->regcode;
1353 +       while (src > opnd)
1354 +               *--dst = *--src;
1355 +
1356 +       place = opnd;           /* Op node, where operand used to be. */
1357 +       *place++ = op;
1358 +       *place++ = '\0';
1359 +       *place++ = '\0';
1360 +}
1361 +
1362 +/*
1363 + - regtail - set the next-pointer at the end of a node chain
1364 + */
1365 +static void
1366 +regtail(struct match_globals *g, char *p, char *val)
1367 +{
1368 +       register char *scan;
1369 +       register char *temp;
1370 +       register int offset;
1371 +
1372 +       if (p == &g->regdummy)
1373 +               return;
1374 +
1375 +       /* Find last node. */
1376 +       scan = p;
1377 +       for (;;) {
1378 +               temp = regnext(g, scan);
1379 +               if (temp == NULL)
1380 +                       break;
1381 +               scan = temp;
1382 +       }
1383 +
1384 +       if (OP(scan) == BACK)
1385 +               offset = scan - val;
1386 +       else
1387 +               offset = val - scan;
1388 +       *(scan+1) = (offset>>8)&0377;
1389 +       *(scan+2) = offset&0377;
1390 +}
1391 +
1392 +/*
1393 + - regoptail - regtail on operand of first argument; nop if operandless
1394 + */
1395 +static void
1396 +regoptail(struct match_globals *g, char *p, char *val)
1397 +{
1398 +       /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1399 +       if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1400 +               return;
1401 +       regtail(g, OPERAND(p), val);
1402 +}
1403 +
1404 +/*
1405 + * regexec and friends
1406 + */
1407 +
1408 +
1409 +/*
1410 + * Forwards.
1411 + */
1412 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1413 +STATIC int regmatch(struct match_globals *g, char *prog);
1414 +STATIC int regrepeat(struct match_globals *g, char *p);
1415 +
1416 +#ifdef DEBUG
1417 +int regnarrate = 0;
1418 +void regdump();
1419 +STATIC char *regprop(char *op);
1420 +#endif
1421 +
1422 +/*
1423 + - regexec - match a regexp against a string
1424 + */
1425 +int
1426 +regexec(regexp *prog, char *string)
1427 +{
1428 +       register char *s;
1429 +       struct match_globals g;
1430 +
1431 +       /* Be paranoid... */
1432 +       if (prog == NULL || string == NULL) {
1433 +               printk("<3>Regexp: NULL parameter\n");
1434 +               return(0);
1435 +       }
1436 +
1437 +       /* Check validity of program. */
1438 +       if (UCHARAT(prog->program) != MAGIC) {
1439 +               printk("<3>Regexp: corrupted program\n");
1440 +               return(0);
1441 +       }
1442 +
1443 +       /* If there is a "must appear" string, look for it. */
1444 +       if (prog->regmust != NULL) {
1445 +               s = string;
1446 +               while ((s = strchr(s, prog->regmust[0])) != NULL) {
1447 +                       if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1448 +                               break;  /* Found it. */
1449 +                       s++;
1450 +               }
1451 +               if (s == NULL)  /* Not present. */
1452 +                       return(0);
1453 +       }
1454 +
1455 +       /* Mark beginning of line for ^ . */
1456 +       g.regbol = string;
1457 +
1458 +       /* Simplest case:  anchored match need be tried only once. */
1459 +       if (prog->reganch)
1460 +               return(regtry(&g, prog, string));
1461 +
1462 +       /* Messy cases:  unanchored match. */
1463 +       s = string;
1464 +       if (prog->regstart != '\0')
1465 +               /* We know what char it must start with. */
1466 +               while ((s = strchr(s, prog->regstart)) != NULL) {
1467 +                       if (regtry(&g, prog, s))
1468 +                               return(1);
1469 +                       s++;
1470 +               }
1471 +       else
1472 +               /* We don't -- general case. */
1473 +               do {
1474 +                       if (regtry(&g, prog, s))
1475 +                               return(1);
1476 +               } while (*s++ != '\0');
1477 +
1478 +       /* Failure. */
1479 +       return(0);
1480 +}
1481 +
1482 +/*
1483 + - regtry - try match at specific point
1484 + */
1485 +static int                     /* 0 failure, 1 success */
1486 +regtry(struct match_globals *g, regexp *prog, char *string)
1487 +{
1488 +       register int i;
1489 +       register char **sp;
1490 +       register char **ep;
1491 +
1492 +       g->reginput = string;
1493 +       g->regstartp = prog->startp;
1494 +       g->regendp = prog->endp;
1495 +
1496 +       sp = prog->startp;
1497 +       ep = prog->endp;
1498 +       for (i = NSUBEXP; i > 0; i--) {
1499 +               *sp++ = NULL;
1500 +               *ep++ = NULL;
1501 +       }
1502 +       if (regmatch(g, prog->program + 1)) {
1503 +               prog->startp[0] = string;
1504 +               prog->endp[0] = g->reginput;
1505 +               return(1);
1506 +       } else
1507 +               return(0);
1508 +}
1509 +
1510 +/*
1511 + - regmatch - main matching routine
1512 + *
1513 + * Conceptually the strategy is simple:  check to see whether the current
1514 + * node matches, call self recursively to see whether the rest matches,
1515 + * and then act accordingly.  In practice we make some effort to avoid
1516 + * recursion, in particular by going through "ordinary" nodes (that don't
1517 + * need to know whether the rest of the match failed) by a loop instead of
1518 + * by recursion.
1519 + */
1520 +static int                     /* 0 failure, 1 success */
1521 +regmatch(struct match_globals *g, char *prog)
1522 +{
1523 +       register char *scan = prog; /* Current node. */
1524 +       char *next;                 /* Next node. */
1525 +
1526 +#ifdef DEBUG
1527 +       if (scan != NULL && regnarrate)
1528 +               fprintf(stderr, "%s(\n", regprop(scan));
1529 +#endif
1530 +       while (scan != NULL) {
1531 +#ifdef DEBUG
1532 +               if (regnarrate)
1533 +                       fprintf(stderr, "%s...\n", regprop(scan));
1534 +#endif
1535 +               next = regnext(g, scan);
1536 +
1537 +               switch (OP(scan)) {
1538 +               case BOL:
1539 +                       if (g->reginput != g->regbol)
1540 +                               return(0);
1541 +                       break;
1542 +               case EOL:
1543 +                       if (*g->reginput != '\0')
1544 +                               return(0);
1545 +                       break;
1546 +               case ANY:
1547 +                       if (*g->reginput == '\0')
1548 +                               return(0);
1549 +                       g->reginput++;
1550 +                       break;
1551 +               case EXACTLY: {
1552 +                               register int len;
1553 +                               register char *opnd;
1554 +
1555 +                               opnd = OPERAND(scan);
1556 +                               /* Inline the first character, for speed. */
1557 +                               if (*opnd != *g->reginput)
1558 +                                       return(0);
1559 +                               len = strlen(opnd);
1560 +                               if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1561 +                                       return(0);
1562 +                               g->reginput += len;
1563 +                       }
1564 +                       break;
1565 +               case ANYOF:
1566 +                       if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1567 +                               return(0);
1568 +                       g->reginput++;
1569 +                       break;
1570 +               case ANYBUT:
1571 +                       if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1572 +                               return(0);
1573 +                       g->reginput++;
1574 +                       break;
1575 +               case NOTHING:
1576 +               case BACK:
1577 +                       break;
1578 +               case OPEN+1:
1579 +               case OPEN+2:
1580 +               case OPEN+3:
1581 +               case OPEN+4:
1582 +               case OPEN+5:
1583 +               case OPEN+6:
1584 +               case OPEN+7:
1585 +               case OPEN+8:
1586 +               case OPEN+9: {
1587 +                               register int no;
1588 +                               register char *save;
1589 +
1590 +                               no = OP(scan) - OPEN;
1591 +                               save = g->reginput;
1592 +
1593 +                               if (regmatch(g, next)) {
1594 +                                       /*
1595 +                                        * Don't set startp if some later
1596 +                                        * invocation of the same parentheses
1597 +                                        * already has.
1598 +                                        */
1599 +                                       if (g->regstartp[no] == NULL)
1600 +                                               g->regstartp[no] = save;
1601 +                                       return(1);
1602 +                               } else
1603 +                                       return(0);
1604 +                       }
1605 +                       break;
1606 +               case CLOSE+1:
1607 +               case CLOSE+2:
1608 +               case CLOSE+3:
1609 +               case CLOSE+4:
1610 +               case CLOSE+5:
1611 +               case CLOSE+6:
1612 +               case CLOSE+7:
1613 +               case CLOSE+8:
1614 +               case CLOSE+9:
1615 +                       {
1616 +                               register int no;
1617 +                               register char *save;
1618 +
1619 +                               no = OP(scan) - CLOSE;
1620 +                               save = g->reginput;
1621 +
1622 +                               if (regmatch(g, next)) {
1623 +                                       /*
1624 +                                        * Don't set endp if some later
1625 +                                        * invocation of the same parentheses
1626 +                                        * already has.
1627 +                                        */
1628 +                                       if (g->regendp[no] == NULL)
1629 +                                               g->regendp[no] = save;
1630 +                                       return(1);
1631 +                               } else
1632 +                                       return(0);
1633 +                       }
1634 +                       break;
1635 +               case BRANCH: {
1636 +                               register char *save;
1637 +
1638 +                               if (OP(next) != BRANCH)         /* No choice. */
1639 +                                       next = OPERAND(scan);   /* Avoid recursion. */
1640 +                               else {
1641 +                                       do {
1642 +                                               save = g->reginput;
1643 +                                               if (regmatch(g, OPERAND(scan)))
1644 +                                                       return(1);
1645 +                                               g->reginput = save;
1646 +                                               scan = regnext(g, scan);
1647 +                                       } while (scan != NULL && OP(scan) == BRANCH);
1648 +                                       return(0);
1649 +                                       /* NOTREACHED */
1650 +                               }
1651 +                       }
1652 +                       break;
1653 +               case STAR:
1654 +               case PLUS: {
1655 +                               register char nextch;
1656 +                               register int no;
1657 +                               register char *save;
1658 +                               register int min;
1659 +
1660 +                               /*
1661 +                                * Lookahead to avoid useless match attempts
1662 +                                * when we know what character comes next.
1663 +                                */
1664 +                               nextch = '\0';
1665 +                               if (OP(next) == EXACTLY)
1666 +                                       nextch = *OPERAND(next);
1667 +                               min = (OP(scan) == STAR) ? 0 : 1;
1668 +                               save = g->reginput;
1669 +                               no = regrepeat(g, OPERAND(scan));
1670 +                               while (no >= min) {
1671 +                                       /* If it could work, try it. */
1672 +                                       if (nextch == '\0' || *g->reginput == nextch)
1673 +                                               if (regmatch(g, next))
1674 +                                                       return(1);
1675 +                                       /* Couldn't or didn't -- back up. */
1676 +                                       no--;
1677 +                                       g->reginput = save + no;
1678 +                               }
1679 +                               return(0);
1680 +                       }
1681 +                       break;
1682 +               case END:
1683 +                       return(1);      /* Success! */
1684 +                       break;
1685 +               default:
1686 +                       printk("<3>Regexp: memory corruption\n");
1687 +                       return(0);
1688 +                       break;
1689 +               }
1690 +
1691 +               scan = next;
1692 +       }
1693 +
1694 +       /*
1695 +        * We get here only if there's trouble -- normally "case END" is
1696 +        * the terminating point.
1697 +        */
1698 +       printk("<3>Regexp: corrupted pointers\n");
1699 +       return(0);
1700 +}
1701 +
1702 +/*
1703 + - regrepeat - repeatedly match something simple, report how many
1704 + */
1705 +static int
1706 +regrepeat(struct match_globals *g, char *p)
1707 +{
1708 +       register int count = 0;
1709 +       register char *scan;
1710 +       register char *opnd;
1711 +
1712 +       scan = g->reginput;
1713 +       opnd = OPERAND(p);
1714 +       switch (OP(p)) {
1715 +       case ANY:
1716 +               count = strlen(scan);
1717 +               scan += count;
1718 +               break;
1719 +       case EXACTLY:
1720 +               while (*opnd == *scan) {
1721 +                       count++;
1722 +                       scan++;
1723 +               }
1724 +               break;
1725 +       case ANYOF:
1726 +               while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1727 +                       count++;
1728 +                       scan++;
1729 +               }
1730 +               break;
1731 +       case ANYBUT:
1732 +               while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1733 +                       count++;
1734 +                       scan++;
1735 +               }
1736 +               break;
1737 +       default:                /* Oh dear.  Called inappropriately. */
1738 +               printk("<3>Regexp: internal foulup\n");
1739 +               count = 0;      /* Best compromise. */
1740 +               break;
1741 +       }
1742 +       g->reginput = scan;
1743 +
1744 +       return(count);
1745 +}
1746 +
1747 +/*
1748 + - regnext - dig the "next" pointer out of a node
1749 + */
1750 +static char*
1751 +regnext(struct match_globals *g, char *p)
1752 +{
1753 +       register int offset;
1754 +
1755 +       if (p == &g->regdummy)
1756 +               return(NULL);
1757 +
1758 +       offset = NEXT(p);
1759 +       if (offset == 0)
1760 +               return(NULL);
1761 +
1762 +       if (OP(p) == BACK)
1763 +               return(p-offset);
1764 +       else
1765 +               return(p+offset);
1766 +}
1767 +
1768 +#ifdef DEBUG
1769 +
1770 +STATIC char *regprop();
1771 +
1772 +/*
1773 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1774 + */
1775 +void
1776 +regdump(regexp *r)
1777 +{
1778 +       register char *s;
1779 +       register char op = EXACTLY;     /* Arbitrary non-END op. */
1780 +       register char *next;
1781 +       /* extern char *strchr(); */
1782 +
1783 +
1784 +       s = r->program + 1;
1785 +       while (op != END) {     /* While that wasn't END last time... */
1786 +               op = OP(s);
1787 +               printf("%2d%s", s-r->program, regprop(s));      /* Where, what. */
1788 +               next = regnext(s);
1789 +               if (next == NULL)               /* Next ptr. */
1790 +                       printf("(0)");
1791 +               else
1792 +                       printf("(%d)", (s-r->program)+(next-s));
1793 +               s += 3;
1794 +               if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1795 +                       /* Literal string, where present. */
1796 +                       while (*s != '\0') {
1797 +                               putchar(*s);
1798 +                               s++;
1799 +                       }
1800 +                       s++;
1801 +               }
1802 +               putchar('\n');
1803 +       }
1804 +
1805 +       /* Header fields of interest. */
1806 +       if (r->regstart != '\0')
1807 +               printf("start `%c' ", r->regstart);
1808 +       if (r->reganch)
1809 +               printf("anchored ");
1810 +       if (r->regmust != NULL)
1811 +               printf("must have \"%s\"", r->regmust);
1812 +       printf("\n");
1813 +}
1814 +
1815 +/*
1816 + - regprop - printable representation of opcode
1817 + */
1818 +static char *
1819 +regprop(char *op)
1820 +{
1821 +#define BUFLEN 50
1822 +       register char *p;
1823 +       static char buf[BUFLEN];
1824 +
1825 +       strcpy(buf, ":");
1826 +
1827 +       switch (OP(op)) {
1828 +       case BOL:
1829 +               p = "BOL";
1830 +               break;
1831 +       case EOL:
1832 +               p = "EOL";
1833 +               break;
1834 +       case ANY:
1835 +               p = "ANY";
1836 +               break;
1837 +       case ANYOF:
1838 +               p = "ANYOF";
1839 +               break;
1840 +       case ANYBUT:
1841 +               p = "ANYBUT";
1842 +               break;
1843 +       case BRANCH:
1844 +               p = "BRANCH";
1845 +               break;
1846 +       case EXACTLY:
1847 +               p = "EXACTLY";
1848 +               break;
1849 +       case NOTHING:
1850 +               p = "NOTHING";
1851 +               break;
1852 +       case BACK:
1853 +               p = "BACK";
1854 +               break;
1855 +       case END:
1856 +               p = "END";
1857 +               break;
1858 +       case OPEN+1:
1859 +       case OPEN+2:
1860 +       case OPEN+3:
1861 +       case OPEN+4:
1862 +       case OPEN+5:
1863 +       case OPEN+6:
1864 +       case OPEN+7:
1865 +       case OPEN+8:
1866 +       case OPEN+9:
1867 +               snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1868 +               p = NULL;
1869 +               break;
1870 +       case CLOSE+1:
1871 +       case CLOSE+2:
1872 +       case CLOSE+3:
1873 +       case CLOSE+4:
1874 +       case CLOSE+5:
1875 +       case CLOSE+6:
1876 +       case CLOSE+7:
1877 +       case CLOSE+8:
1878 +       case CLOSE+9:
1879 +               snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1880 +               p = NULL;
1881 +               break;
1882 +       case STAR:
1883 +               p = "STAR";
1884 +               break;
1885 +       case PLUS:
1886 +               p = "PLUS";
1887 +               break;
1888 +       default:
1889 +               printk("<3>Regexp: corrupted opcode\n");
1890 +               break;
1891 +       }
1892 +       if (p != NULL)
1893 +               strncat(buf, p, BUFLEN-strlen(buf));
1894 +       return(buf);
1895 +}
1896 +#endif
1897 +
1898 +
1899 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h
1900 ===================================================================
1901 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1902 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regexp.h   2007-07-02 00:37:55.700427500 +0200
1903 @@ -0,0 +1,41 @@
1904 +/*
1905 + * Definitions etc. for regexp(3) routines.
1906 + *
1907 + * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
1908 + * not the System V one.
1909 + */
1910 +
1911 +#ifndef REGEXP_H
1912 +#define REGEXP_H
1913 +
1914 +
1915 +/*
1916 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1917 +which contains a version of this library, says:
1918 +
1919 + *
1920 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1921 + * will not work properly.
1922 + *
1923 +
1924 +However, it looks rather like this library is limited to 10.  If you think
1925 +otherwise, let us know.
1926 +*/
1927 +
1928 +#define NSUBEXP  10
1929 +typedef struct regexp {
1930 +       char *startp[NSUBEXP];
1931 +       char *endp[NSUBEXP];
1932 +       char regstart;          /* Internal use only. */
1933 +       char reganch;           /* Internal use only. */
1934 +       char *regmust;          /* Internal use only. */
1935 +       int regmlen;            /* Internal use only. */
1936 +       char program[1];        /* Unwarranted chumminess with compiler. */
1937 +} regexp;
1938 +
1939 +regexp * regcomp(char *exp, int *patternsize);
1940 +int regexec(regexp *prog, char *string);
1941 +void regsub(regexp *prog, char *source, char *dest);
1942 +void regerror(char *s);
1943 +
1944 +#endif
1945 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h
1946 ===================================================================
1947 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1948 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regmagic.h 2007-07-02 00:37:55.724429000 +0200
1949 @@ -0,0 +1,5 @@
1950 +/*
1951 + * The first byte of the regexp internal "program" is actually this magic
1952 + * number; the start node begins in the second byte.
1953 + */
1954 +#define        MAGIC   0234
1955 Index: linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c
1956 ===================================================================
1957 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1958 +++ linux-2.6.21.5/net/ipv4/netfilter/regexp/regsub.c   2007-07-02 00:37:55.752430750 +0200
1959 @@ -0,0 +1,95 @@
1960 +/*
1961 + * regsub
1962 + * @(#)regsub.c        1.3 of 2 April 86
1963 + *
1964 + *     Copyright (c) 1986 by University of Toronto.
1965 + *     Written by Henry Spencer.  Not derived from licensed software.
1966 + *
1967 + *     Permission is granted to anyone to use this software for any
1968 + *     purpose on any computer system, and to redistribute it freely,
1969 + *     subject to the following restrictions:
1970 + *
1971 + *     1. The author is not responsible for the consequences of use of
1972 + *             this software, no matter how awful, even if they arise
1973 + *             from defects in it.
1974 + *
1975 + *     2. The origin of this software must not be misrepresented, either
1976 + *             by explicit claim or by omission.
1977 + *
1978 + *     3. Altered versions must be plainly marked as such, and must not
1979 + *             be misrepresented as being the original software.
1980 + *
1981 + *
1982 + * This code was modified by Ethan Sommer to work within the kernel
1983 + * (it now uses kmalloc etc..)
1984 + *
1985 + */
1986 +#include "regexp.h"
1987 +#include "regmagic.h"
1988 +#include <linux/string.h>
1989 +
1990 +
1991 +#ifndef CHARBITS
1992 +#define        UCHARAT(p)      ((int)*(unsigned char *)(p))
1993 +#else
1994 +#define        UCHARAT(p)      ((int)*(p)&CHARBITS)
1995 +#endif
1996 +
1997 +#if 0
1998 +//void regerror(char * s)
1999 +//{
2000 +//        printk("regexp(3): %s", s);
2001 +//        /* NOTREACHED */
2002 +//}
2003 +#endif
2004 +
2005 +/*
2006 + - regsub - perform substitutions after a regexp match
2007 + */
2008 +void
2009 +regsub(regexp * prog, char * source, char * dest)
2010 +{
2011 +       register char *src;
2012 +       register char *dst;
2013 +       register char c;
2014 +       register int no;
2015 +       register int len;
2016 +       
2017 +       /* Not necessary and gcc doesn't like it -MLS */
2018 +       /*extern char *strncpy();*/
2019 +
2020 +       if (prog == NULL || source == NULL || dest == NULL) {
2021 +               regerror("NULL parm to regsub");
2022 +               return;
2023 +       }
2024 +       if (UCHARAT(prog->program) != MAGIC) {
2025 +               regerror("damaged regexp fed to regsub");
2026 +               return;
2027 +       }
2028 +
2029 +       src = source;
2030 +       dst = dest;
2031 +       while ((c = *src++) != '\0') {
2032 +               if (c == '&')
2033 +                       no = 0;
2034 +               else if (c == '\\' && '0' <= *src && *src <= '9')
2035 +                       no = *src++ - '0';
2036 +               else
2037 +                       no = -1;
2038 +
2039 +               if (no < 0) {   /* Ordinary character. */
2040 +                       if (c == '\\' && (*src == '\\' || *src == '&'))
2041 +                               c = *src++;
2042 +                       *dst++ = c;
2043 +               } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2044 +                       len = prog->endp[no] - prog->startp[no];
2045 +                       (void) strncpy(dst, prog->startp[no], len);
2046 +                       dst += len;
2047 +                       if (len != 0 && *(dst-1) == '\0') {     /* strncpy hit NUL. */
2048 +                               regerror("damaged match string");
2049 +                               return;
2050 +                       }
2051 +               }
2052 +       }
2053 +       *dst++ = '\0';
2054 +}
2055 Index: linux-2.6.21.5/include/net/netfilter/nf_conntrack.h
2056 ===================================================================
2057 --- linux-2.6.21.5.orig/include/net/netfilter/nf_conntrack.h    2007-07-02 00:49:22.815369500 +0200
2058 +++ linux-2.6.21.5/include/net/netfilter/nf_conntrack.h 2007-07-02 00:56:21.413530250 +0200
2059 @@ -128,6 +128,21 @@
2060         u_int32_t secmark;
2061  #endif
2062  
2063 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
2064 +       struct {
2065 +               /*
2066 +                * e.g. "http". NULL before decision. "unknown" after decision
2067 +                * if no match.
2068 +                */
2069 +               char *app_proto;
2070 +               /*
2071 +                * application layer data so far. NULL after match decision.
2072 +                */
2073 +               char *app_data;
2074 +               unsigned int app_data_len;
2075 +       } layer7;
2076 +#endif
2077 +
2078         /* Storage reserved for other modules: */
2079         union nf_conntrack_proto proto;
2080