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