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