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