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