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
7 +#define MAX_PATTERN_LEN 8192
8 +#define MAX_PROTOCOL_LEN 256
10 +struct xt_layer7_info {
11 + char protocol[MAX_PROTOCOL_LEN];
12 + char pattern[MAX_PATTERN_LEN];
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
23 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
24 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
27 + * e.g. "http". NULL before decision. "unknown" after decision
32 + * application layer data so far. NULL after match decision.
35 + unsigned int app_data_len;
39 /* Storage reserved for other modules: */
40 union nf_conntrack_proto proto;
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 *
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))
54 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
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
61 To compile it as a module, choose M here. If unsure, say N.
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
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
75 + To compile it as a module, choose M here. If unsure, say N.
77 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
78 + bool 'Layer 7 debugging output'
79 + depends on NETFILTER_XT_MATCH_LAYER7
81 + Say Y to get lots of debugging output.
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
101 nf_ct_remove_expectations(ct);
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);
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 *
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))
126 + if (seq_printf(s, "asdfuse=%u\n", atomic_read(&conntrack->ct_general.use)))
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
134 + * regcomp and regexec -- regsub and regerror are elsewhere
135 + * @(#)regexp.c 1.3 of 18 April 87
137 + * Copyright (c) 1986 by University of Toronto.
138 + * Written by Henry Spencer. Not derived from licensed software.
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:
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.
148 + * 2. The origin of this software must not be misrepresented, either
149 + * by explicit claim or by omission.
151 + * 3. Altered versions must be plainly marked as such, and must not
152 + * be misrepresented as being the original software.
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.
158 + * This code was modified by Ethan Sommer to work within the kernel
159 + * (it now uses kmalloc etc..)
161 + * Modified slightly by Matthew Strait to use more modern C.
165 +#include "regmagic.h"
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... */
170 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
172 + #define printk(format,args...) printf(format,##args)
175 +void regerror(char * s)
177 + printk("<3>Regexp: %s\n", s);
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:
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
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
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:
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. */
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.
245 + * BACK Normal "next" pointers all implicitly point forward; BACK
246 + * exists to make loop structures possible.
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.
253 + * OPEN,CLOSE ...are numbered at compile time.
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.)
263 + * Using two bytes for the "next" pointer is vast overkill for most things,
264 + * but allows patterns to get big without disasters.
266 +#define OP(p) (*(p))
267 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
268 +#define OPERAND(p) ((p) + 3)
271 + * See regmagic.h for one further detail of program structure.
276 + * Utility definitions.
279 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
281 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
284 +#define FAIL(m) { regerror(m); return(NULL); }
285 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
286 +#define META "^$.[()|?+*\\"
289 + * Flags to be passed up and down.
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. */
297 + * Global work variables for regcomp().
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. */
307 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
308 +long regsize; /* Code size. */
312 + * Forward declarations for regcomp()'s friends.
315 +#define STATIC static
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);
329 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
336 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
337 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
338 + if (*scan1 == *scan2++)
346 + - regcomp - compile a regular expression into internal code
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.)
357 + * Beware that the optimization-preparation code in here knows about some
358 + * of the structure of the compiled regexp.
361 +regcomp(char *exp,int *patternsize)
363 + register regexp *r;
364 + register char *scan;
365 + register char *longest;
368 + struct match_globals g;
370 + /* commented out by ethan
371 + extern char *malloc();
375 + FAIL("NULL argument");
377 + /* First pass: determine size, legality. */
381 + g.regcode = &g.regdummy;
383 + if (reg(&g, 0, &flags) == NULL)
386 + /* Small enough for pointer-storage convention? */
387 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
388 + FAIL("regexp too big");
390 + /* Allocate space. */
391 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
392 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
394 + FAIL("out of space");
396 + /* Second pass: emit code. */
399 + g.regcode = r->program;
401 + if (reg(&g, 0, &flags) == NULL)
404 + /* Dig out information for optimizations. */
405 + r->regstart = '\0'; /* Worst-case defaults. */
409 + scan = r->program+1; /* First BRANCH. */
410 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
411 + scan = OPERAND(scan);
413 + /* Starting-point info. */
414 + if (OP(scan) == EXACTLY)
415 + r->regstart = *OPERAND(scan);
416 + else if (OP(scan) == BOL)
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.
427 + if (flags&SPSTART) {
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));
435 + r->regmust = longest;
444 + - reg - regular expression, i.e. main body or parenthesized thing
446 + * Caller must absorb opening parenthesis.
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.
453 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
455 + register char *ret;
457 + register char *ender;
458 + register int parno = 0; /* 0 makes gcc happy */
461 + *flagp = HASWIDTH; /* Tentatively. */
463 + /* Make an OPEN node, if parenthesized. */
465 + if (g->regnpar >= NSUBEXP)
466 + FAIL("too many ()");
467 + parno = g->regnpar;
469 + ret = regnode(g, OPEN+parno);
473 + /* Pick up the branches, linking them together. */
474 + br = regbranch(g, &flags);
478 + regtail(g, ret, br); /* OPEN -> first. */
481 + if (!(flags&HASWIDTH))
482 + *flagp &= ~HASWIDTH;
483 + *flagp |= flags&SPSTART;
484 + while (*g->regparse == '|') {
486 + br = regbranch(g, &flags);
489 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
490 + if (!(flags&HASWIDTH))
491 + *flagp &= ~HASWIDTH;
492 + *flagp |= flags&SPSTART;
495 + /* Make a closing node, and hook it on the end. */
496 + ender = regnode(g, (paren) ? CLOSE+parno : END);
497 + regtail(g, ret, ender);
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);
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 ()");
510 + FAIL("junk on end"); /* "Can't happen". */
518 + - regbranch - one alternative of an | operator
520 + * Implements the concatenation operator.
523 +regbranch(struct match_globals *g, int *flagp)
525 + register char *ret;
526 + register char *chain;
527 + register char *latest;
530 + *flagp = WORST; /* Tentatively. */
532 + ret = regnode(g, BRANCH);
534 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
535 + latest = regpiece(g, &flags);
536 + if (latest == NULL)
538 + *flagp |= flags&HASWIDTH;
539 + if (chain == NULL) /* First piece. */
540 + *flagp |= flags&SPSTART;
542 + regtail(g, chain, latest);
545 + if (chain == NULL) /* Loop ran zero times. */
546 + (void) regnode(g, NOTHING);
552 + - regpiece - something followed by possible [*+?]
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.
561 +regpiece(struct match_globals *g, int *flagp)
563 + register char *ret;
565 + register char *next;
568 + ret = regatom(g, &flags);
578 + if (!(flags&HASWIDTH) && op != '?')
579 + FAIL("*+ operand could be empty");
580 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
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);
609 + if (ISMULT(*g->regparse))
610 + FAIL("nested *?+");
616 + - regatom - the lowest level
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.
624 +regatom(struct match_globals *g, int *flagp)
626 + register char *ret;
629 + *flagp = WORST; /* Tentatively. */
631 + switch (*g->regparse++) {
633 + ret = regnode(g, BOL);
636 + ret = regnode(g, EOL);
639 + ret = regnode(g, ANY);
640 + *flagp |= HASWIDTH|SIMPLE;
643 + register int class;
644 + register int classend;
646 + if (*g->regparse == '^') { /* Complement of range. */
647 + ret = regnode(g, ANYBUT);
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 == '-') {
656 + if (*g->regparse == ']' || *g->regparse == '\0')
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++)
668 + regc(g, *g->regparse++);
671 + if (*g->regparse != ']')
672 + FAIL("unmatched []");
674 + *flagp |= HASWIDTH|SIMPLE;
678 + ret = reg(g, 1, &flags);
681 + *flagp |= flags&(HASWIDTH|SPSTART);
686 + FAIL("internal urp"); /* Supposed to be caught earlier. */
691 + FAIL("?+* follows nothing");
694 + if (*g->regparse == '\0')
695 + FAIL("trailing \\");
696 + ret = regnode(g, EXACTLY);
697 + regc(g, *g->regparse++);
699 + *flagp |= HASWIDTH|SIMPLE;
703 + register char ender;
706 + len = my_strcspn((const char *)g->regparse, (const char *)META);
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;
715 + ret = regnode(g, EXACTLY);
717 + regc(g, *g->regparse++);
729 + - regnode - emit a node
731 +static char * /* Location. */
732 +regnode(struct match_globals *g, char op)
734 + register char *ret;
735 + register char *ptr;
738 + if (ret == &g->regdummy) {
745 + *ptr++ = '\0'; /* Null "next" pointer. */
753 + - regc - emit (if appropriate) a byte of code
756 +regc(struct match_globals *g, char b)
758 + if (g->regcode != &g->regdummy)
765 + - reginsert - insert an operator in front of already-emitted operand
767 + * Means relocating the operand.
770 +reginsert(struct match_globals *g, char op, char* opnd)
772 + register char *src;
773 + register char *dst;
774 + register char *place;
776 + if (g->regcode == &g->regdummy) {
787 + place = opnd; /* Op node, where operand used to be. */
794 + - regtail - set the next-pointer at the end of a node chain
797 +regtail(struct match_globals *g, char *p, char *val)
799 + register char *scan;
800 + register char *temp;
801 + register int offset;
803 + if (p == &g->regdummy)
806 + /* Find last node. */
809 + temp = regnext(g, scan);
815 + if (OP(scan) == BACK)
816 + offset = scan - val;
818 + offset = val - scan;
819 + *(scan+1) = (offset>>8)&0377;
820 + *(scan+2) = offset&0377;
824 + - regoptail - regtail on operand of first argument; nop if operandless
827 +regoptail(struct match_globals *g, char *p, char *val)
829 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
830 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
832 + regtail(g, OPERAND(p), val);
836 + * regexec and friends
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);
850 +STATIC char *regprop(char *op);
854 + - regexec - match a regexp against a string
857 +regexec(regexp *prog, char *string)
860 + struct match_globals g;
862 + /* Be paranoid... */
863 + if (prog == NULL || string == NULL) {
864 + printk("<3>Regexp: NULL parameter\n");
868 + /* Check validity of program. */
869 + if (UCHARAT(prog->program) != MAGIC) {
870 + printk("<3>Regexp: corrupted program\n");
874 + /* If there is a "must appear" string, look for it. */
875 + if (prog->regmust != NULL) {
877 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
878 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
879 + break; /* Found it. */
882 + if (s == NULL) /* Not present. */
886 + /* Mark beginning of line for ^ . */
889 + /* Simplest case: anchored match need be tried only once. */
891 + return(regtry(&g, prog, string));
893 + /* Messy cases: unanchored match. */
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))
903 + /* We don't -- general case. */
905 + if (regtry(&g, prog, s))
907 + } while (*s++ != '\0');
914 + - regtry - try match at specific point
916 +static int /* 0 failure, 1 success */
917 +regtry(struct match_globals *g, regexp *prog, char *string)
920 + register char **sp;
921 + register char **ep;
923 + g->reginput = string;
924 + g->regstartp = prog->startp;
925 + g->regendp = prog->endp;
929 + for (i = NSUBEXP; i > 0; i--) {
933 + if (regmatch(g, prog->program + 1)) {
934 + prog->startp[0] = string;
935 + prog->endp[0] = g->reginput;
942 + - regmatch - main matching routine
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
951 +static int /* 0 failure, 1 success */
952 +regmatch(struct match_globals *g, char *prog)
954 + register char *scan = prog; /* Current node. */
955 + char *next; /* Next node. */
958 + if (scan != NULL && regnarrate)
959 + fprintf(stderr, "%s(\n", regprop(scan));
961 + while (scan != NULL) {
964 + fprintf(stderr, "%s...\n", regprop(scan));
966 + next = regnext(g, scan);
968 + switch (OP(scan)) {
970 + if (g->reginput != g->regbol)
974 + if (*g->reginput != '\0')
978 + if (*g->reginput == '\0')
984 + register char *opnd;
986 + opnd = OPERAND(scan);
987 + /* Inline the first character, for speed. */
988 + if (*opnd != *g->reginput)
990 + len = strlen(opnd);
991 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
993 + g->reginput += len;
997 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1002 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1019 + register char *save;
1021 + no = OP(scan) - OPEN;
1022 + save = g->reginput;
1024 + if (regmatch(g, next)) {
1026 + * Don't set startp if some later
1027 + * invocation of the same parentheses
1030 + if (g->regstartp[no] == NULL)
1031 + g->regstartp[no] = save;
1048 + register char *save;
1050 + no = OP(scan) - CLOSE;
1051 + save = g->reginput;
1053 + if (regmatch(g, next)) {
1055 + * Don't set endp if some later
1056 + * invocation of the same parentheses
1059 + if (g->regendp[no] == NULL)
1060 + g->regendp[no] = save;
1067 + register char *save;
1069 + if (OP(next) != BRANCH) /* No choice. */
1070 + next = OPERAND(scan); /* Avoid recursion. */
1073 + save = g->reginput;
1074 + if (regmatch(g, OPERAND(scan)))
1076 + g->reginput = save;
1077 + scan = regnext(g, scan);
1078 + } while (scan != NULL && OP(scan) == BRANCH);
1086 + register char nextch;
1088 + register char *save;
1092 + * Lookahead to avoid useless match attempts
1093 + * when we know what character comes next.
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))
1106 + /* Couldn't or didn't -- back up. */
1108 + g->reginput = save + no;
1114 + return(1); /* Success! */
1117 + printk("<3>Regexp: memory corruption\n");
1126 + * We get here only if there's trouble -- normally "case END" is
1127 + * the terminating point.
1129 + printk("<3>Regexp: corrupted pointers\n");
1134 + - regrepeat - repeatedly match something simple, report how many
1137 +regrepeat(struct match_globals *g, char *p)
1139 + register int count = 0;
1140 + register char *scan;
1141 + register char *opnd;
1143 + scan = g->reginput;
1144 + opnd = OPERAND(p);
1147 + count = strlen(scan);
1151 + while (*opnd == *scan) {
1157 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1163 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1168 + default: /* Oh dear. Called inappropriately. */
1169 + printk("<3>Regexp: internal foulup\n");
1170 + count = 0; /* Best compromise. */
1173 + g->reginput = scan;
1179 + - regnext - dig the "next" pointer out of a node
1182 +regnext(struct match_globals *g, char *p)
1184 + register int offset;
1186 + if (p == &g->regdummy)
1193 + if (OP(p) == BACK)
1201 +STATIC char *regprop();
1204 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1210 + register char op = EXACTLY; /* Arbitrary non-END op. */
1211 + register char *next;
1212 + /* extern char *strchr(); */
1215 + s = r->program + 1;
1216 + while (op != END) { /* While that wasn't END last time... */
1218 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1219 + next = regnext(s);
1220 + if (next == NULL) /* Next ptr. */
1223 + printf("(%d)", (s-r->program)+(next-s));
1225 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1226 + /* Literal string, where present. */
1227 + while (*s != '\0') {
1236 + /* Header fields of interest. */
1237 + if (r->regstart != '\0')
1238 + printf("start `%c' ", r->regstart);
1240 + printf("anchored ");
1241 + if (r->regmust != NULL)
1242 + printf("must have \"%s\"", r->regmust);
1247 + - regprop - printable representation of opcode
1254 + static char buf[BUFLEN];
1298 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1310 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1320 + printk("<3>Regexp: corrupted opcode\n");
1324 + strncat(buf, p, BUFLEN-strlen(buf));
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
1334 + * Definitions etc. for regexp(3) routines.
1336 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1337 + * not the System V one.
1345 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1346 +which contains a version of this library, says:
1349 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1350 + * will not work properly.
1353 +However, it looks rather like this library is limited to 10. If you think
1354 +otherwise, let us know.
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. */
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);
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
1378 + * The first byte of the regexp internal "program" is actually this magic
1379 + * number; the start node begins in the second byte.
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
1387 + * @(#)regsub.c 1.3 of 2 April 86
1389 + * Copyright (c) 1986 by University of Toronto.
1390 + * Written by Henry Spencer. Not derived from licensed software.
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:
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.
1400 + * 2. The origin of this software must not be misrepresented, either
1401 + * by explicit claim or by omission.
1403 + * 3. Altered versions must be plainly marked as such, and must not
1404 + * be misrepresented as being the original software.
1407 + * This code was modified by Ethan Sommer to work within the kernel
1408 + * (it now uses kmalloc etc..)
1411 +#include "regexp.h"
1412 +#include "regmagic.h"
1413 +#include <linux/string.h>
1417 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1419 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1423 +//void regerror(char * s)
1425 +// printk("regexp(3): %s", s);
1426 +// /* NOTREACHED */
1431 + - regsub - perform substitutions after a regexp match
1434 +regsub(regexp * prog, char * source, char * dest)
1436 + register char *src;
1437 + register char *dst;
1442 + /* Not necessary and gcc doesn't like it -MLS */
1443 + /*extern char *strncpy();*/
1445 + if (prog == NULL || source == NULL || dest == NULL) {
1446 + regerror("NULL parm to regsub");
1449 + if (UCHARAT(prog->program) != MAGIC) {
1450 + regerror("damaged regexp fed to regsub");
1456 + while ((c = *src++) != '\0') {
1459 + else if (c == '\\' && '0' <= *src && *src <= '9')
1460 + no = *src++ - '0';
1464 + if (no < 0) { /* Ordinary character. */
1465 + if (c == '\\' && (*src == '\\' || *src == '&'))
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);
1472 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
1473 + regerror("damaged match string");
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
1484 + Kernel module to match application layer (OSI layer 7) data in connections.
1486 + http://l7-filter.sf.net
1488 + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer.
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
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.
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>
1514 +#include "regexp/regexp.c"
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");
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)
1528 + #define DPRINTK(format,args...)
1531 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
1532 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
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;
1538 +static struct pattern_cache {
1539 + char * regex_string;
1541 + struct pattern_cache * next;
1542 +} * first_pattern_cache = NULL;
1544 +DEFINE_SPINLOCK(l7_lock);
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)
1551 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
1555 + if (net_ratelimit())
1556 + printk(KERN_ERR "layer7: out of memory in "
1557 + "friendly_print, bailing.\n");
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] = ' ';
1570 +static char dec2hex(int i)
1577 + return (i - 10 + 'a');
1580 + if (net_ratelimit())
1581 + printk("layer7: Problem in dec2hex\n");
1586 +static char * hex_print(unsigned char * s)
1588 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
1592 + if (net_ratelimit())
1593 + printk(KERN_ERR "layer7: out of memory in hex_print, "
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);
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)
1614 + struct pattern_cache * node = first_pattern_cache;
1615 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
1616 + struct pattern_cache * tmp;
1619 + while (node != NULL) {
1620 + if (!strcmp(node->regex_string, regex_string))
1621 + return node->pattern;
1623 + last_pattern_cache = node;/* points at the last non-NULL node */
1624 + node = node->next;
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);
1633 + if (net_ratelimit())
1634 + printk(KERN_ERR "layer7: out of memory in "
1635 + "compile_and_cache, bailing.\n");
1639 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
1640 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
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);
1653 + /* Ok. The new node is all ready now. */
1656 + if(first_pattern_cache == NULL) /* list is empty */
1657 + first_pattern_cache = node; /* make node the beginning */
1659 + last_pattern_cache->next = node; /* attach node to the end */
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 "
1669 + regex_string, protocol);
1670 + /* pattern is now cached as NULL, so we won't try again. */
1673 + strcpy(node->regex_string, regex_string);
1674 + return node->pattern;
1677 +static int can_handle(const struct sk_buff *skb)
1679 + if(!ip_hdr(skb)) /* not IP */
1681 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
1682 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
1683 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
1688 +/* Returns offset the into the skb->data that the application data starts */
1689 +static int app_data_offset(const struct sk_buff *skb)
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;
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);
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 */
1708 + if (net_ratelimit())
1709 + printk(KERN_ERR "layer7: tried to handle unknown "
1711 + return ip_hl + 8; /* something reasonable */
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)
1722 + /* If we're in here, throw the app data away */
1723 + if(master_conntrack->layer7.app_data != NULL) {
1725 + #ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
1726 + if(!master_conntrack->layer7.app_proto) {
1728 + friendly_print(master_conntrack->layer7.app_data);
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);
1735 + DPRINTK("In hex: %s\n", g);
1740 + kfree(master_conntrack->layer7.app_data);
1741 + master_conntrack->layer7.app_data = NULL; /* don't free again */
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,
1750 + if(!conntrack->layer7.app_proto){
1751 + if (net_ratelimit())
1752 + printk(KERN_ERR "layer7: out of memory "
1753 + "in match_no_append, "
1757 + strcpy(conntrack->layer7.app_proto,
1758 + master_conntrack->layer7.app_proto);
1761 + return (!strcmp(master_conntrack->layer7.app_proto,
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");
1775 + strcpy(master_conntrack->layer7.app_proto, "unknown");
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)
1784 + int length = 0, i;
1785 + int oldlength = master_conntrack->layer7.app_data_len;
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;
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];
1806 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
1807 + master_conntrack->layer7.app_data_len = length + oldlength;
1812 +/* taken from drivers/video/modedb.c */
1813 +static int my_atoi(const char *s)
1820 + val = 10*val+(*s-'0');
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)
1832 + if(num_packets > 99 && net_ratelimit())
1833 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
1835 + page[0] = num_packets/10 + '0';
1836 + page[1] = num_packets%10 + '0';
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)
1849 + char * foo = kmalloc(count, GFP_ATOMIC);
1852 + if (net_ratelimit())
1853 + printk(KERN_ERR "layer7: out of memory, bailing. "
1854 + "num_packets unchanged.\n");
1858 + if(copy_from_user(foo, buffer, count)) {
1863 + num_packets = my_atoi(foo);
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");
1871 + } else if(num_packets < 1) {
1872 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
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,
1886 + unsigned int protoff,
1889 + /* sidestep const without getting a compiler warning... */
1890 + struct sk_buff * skb = (struct sk_buff *)skbin;
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;
1899 + /* Be paranoid/incompetent - lock the entire match function. */
1900 + spin_lock_bh(&l7_lock);
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;
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;
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);
1922 + /* if we've classified it or seen too many packets */
1923 + if(TOTAL_PACKETS > num_packets ||
1924 + master_conntrack->layer7.app_proto) {
1926 + pattern_result = match_no_append(conntrack, master_conntrack,
1927 + ctinfo, master_ctinfo, info);
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 */
1936 + spin_unlock_bh(&l7_lock);
1937 + return (pattern_result ^ info->invert);
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;
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;
1954 + /* the return value gets checked later, when we're ready to use it */
1955 + comppattern = compile_and_cache(info->pattern, info->protocol);
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;
1970 + master_conntrack->layer7.app_data[0] = '\0';
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 */
1982 + newbytes = add_data(master_conntrack, app_data, appdatalen);
1984 + if(newbytes == 0) { /* didn't add any data */
1986 + /* Didn't match before, not going to match now */
1987 + spin_unlock_bh(&l7_lock);
1988 + return info->invert;
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;
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);
2019 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
2020 + } else if(pattern_result > 1) { /* cleanup from "unset" */
2021 + pattern_result = 1;
2024 + /* mark the packet seen */
2027 + spin_unlock_bh(&l7_lock);
2028 + return (pattern_result ^ info->invert);
2031 +static int check(const char *tablename,
2033 + const struct xt_match *match,
2035 + unsigned int hook_mask)
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);
2048 +destroy(const struct xt_match *match, void *matchinfo)
2050 + nf_ct_l3proto_module_put(match->family);
2053 +static struct xt_match xt_layer7_match[] = {
2056 + .family = AF_INET,
2057 + .checkentry = check,
2059 + .destroy = destroy,
2060 + .matchsize = sizeof(struct xt_layer7_info),
2065 +static void layer7_cleanup_proc(void)
2067 + remove_proc_entry("layer7_numpackets", proc_net);
2070 +/* register the proc file */
2071 +static void layer7_init_proc(void)
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;
2079 +static int __init xt_layer7_init(void)
2083 + layer7_init_proc();
2084 + if(maxdatalen < 1) {
2085 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
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, "
2094 + maxdatalen = 65536;
2096 + return xt_register_matches(xt_layer7_match,
2097 + ARRAY_SIZE(xt_layer7_match));
2100 +static void __exit xt_layer7_fini(void)
2102 + layer7_cleanup_proc();
2103 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
2106 +module_init(xt_layer7_init);
2107 +module_exit(xt_layer7_fini);