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