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