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