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