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