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