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