2 +++ b/include/linux/netfilter/xt_layer7.h
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 --- a/include/net/netfilter/nf_conntrack.h
18 +++ b/include/net/netfilter/nf_conntrack.h
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 --- a/net/netfilter/Kconfig
43 +++ b/net/netfilter/Kconfig
44 @@ -633,6 +633,27 @@ config NETFILTER_XT_MATCH_STATE
46 To compile it as a module, choose M here. If unsure, say N.
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
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
60 + To compile it as a module, choose M here. If unsure, say N.
62 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
63 + bool 'Layer 7 debugging output'
64 + depends on NETFILTER_XT_MATCH_LAYER7
66 + Say Y to get lots of debugging output.
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 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_QUOTA) +
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 @@ destroy_conntrack(struct nf_conntrack *n
86 nf_ct_remove_expectations(ct);
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);
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 @@ static int ct_seq_show(struct seq_file *
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))
110 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
114 +++ b/net/netfilter/regexp/regexp.c
117 + * regcomp and regexec -- regsub and regerror are elsewhere
118 + * @(#)regexp.c 1.3 of 18 April 87
120 + * Copyright (c) 1986 by University of Toronto.
121 + * Written by Henry Spencer. Not derived from licensed software.
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:
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.
131 + * 2. The origin of this software must not be misrepresented, either
132 + * by explicit claim or by omission.
134 + * 3. Altered versions must be plainly marked as such, and must not
135 + * be misrepresented as being the original software.
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.
141 + * This code was modified by Ethan Sommer to work within the kernel
142 + * (it now uses kmalloc etc..)
144 + * Modified slightly by Matthew Strait to use more modern C.
148 +#include "regmagic.h"
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... */
153 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
155 + #define printk(format,args...) printf(format,##args)
158 +void regerror(char * s)
160 + printk("<3>Regexp: %s\n", s);
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:
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
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
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:
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. */
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.
228 + * BACK Normal "next" pointers all implicitly point forward; BACK
229 + * exists to make loop structures possible.
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.
236 + * OPEN,CLOSE ...are numbered at compile time.
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.)
246 + * Using two bytes for the "next" pointer is vast overkill for most things,
247 + * but allows patterns to get big without disasters.
249 +#define OP(p) (*(p))
250 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
251 +#define OPERAND(p) ((p) + 3)
254 + * See regmagic.h for one further detail of program structure.
259 + * Utility definitions.
262 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
264 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
267 +#define FAIL(m) { regerror(m); return(NULL); }
268 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
269 +#define META "^$.[()|?+*\\"
272 + * Flags to be passed up and down.
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. */
280 + * Global work variables for regcomp().
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. */
290 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
291 +long regsize; /* Code size. */
295 + * Forward declarations for regcomp()'s friends.
298 +#define STATIC static
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);
312 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
319 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
320 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
321 + if (*scan1 == *scan2++)
329 + - regcomp - compile a regular expression into internal code
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.)
340 + * Beware that the optimization-preparation code in here knows about some
341 + * of the structure of the compiled regexp.
344 +regcomp(char *exp,int *patternsize)
346 + register regexp *r;
347 + register char *scan;
348 + register char *longest;
351 + struct match_globals g;
353 + /* commented out by ethan
354 + extern char *malloc();
358 + FAIL("NULL argument");
360 + /* First pass: determine size, legality. */
364 + g.regcode = &g.regdummy;
366 + if (reg(&g, 0, &flags) == NULL)
369 + /* Small enough for pointer-storage convention? */
370 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
371 + FAIL("regexp too big");
373 + /* Allocate space. */
374 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
375 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
377 + FAIL("out of space");
379 + /* Second pass: emit code. */
382 + g.regcode = r->program;
384 + if (reg(&g, 0, &flags) == NULL)
387 + /* Dig out information for optimizations. */
388 + r->regstart = '\0'; /* Worst-case defaults. */
392 + scan = r->program+1; /* First BRANCH. */
393 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
394 + scan = OPERAND(scan);
396 + /* Starting-point info. */
397 + if (OP(scan) == EXACTLY)
398 + r->regstart = *OPERAND(scan);
399 + else if (OP(scan) == BOL)
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.
410 + if (flags&SPSTART) {
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));
418 + r->regmust = longest;
427 + - reg - regular expression, i.e. main body or parenthesized thing
429 + * Caller must absorb opening parenthesis.
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.
436 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
438 + register char *ret;
440 + register char *ender;
441 + register int parno = 0; /* 0 makes gcc happy */
444 + *flagp = HASWIDTH; /* Tentatively. */
446 + /* Make an OPEN node, if parenthesized. */
448 + if (g->regnpar >= NSUBEXP)
449 + FAIL("too many ()");
450 + parno = g->regnpar;
452 + ret = regnode(g, OPEN+parno);
456 + /* Pick up the branches, linking them together. */
457 + br = regbranch(g, &flags);
461 + regtail(g, ret, br); /* OPEN -> first. */
464 + if (!(flags&HASWIDTH))
465 + *flagp &= ~HASWIDTH;
466 + *flagp |= flags&SPSTART;
467 + while (*g->regparse == '|') {
469 + br = regbranch(g, &flags);
472 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
473 + if (!(flags&HASWIDTH))
474 + *flagp &= ~HASWIDTH;
475 + *flagp |= flags&SPSTART;
478 + /* Make a closing node, and hook it on the end. */
479 + ender = regnode(g, (paren) ? CLOSE+parno : END);
480 + regtail(g, ret, ender);
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);
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 ()");
493 + FAIL("junk on end"); /* "Can't happen". */
501 + - regbranch - one alternative of an | operator
503 + * Implements the concatenation operator.
506 +regbranch(struct match_globals *g, int *flagp)
508 + register char *ret;
509 + register char *chain;
510 + register char *latest;
513 + *flagp = WORST; /* Tentatively. */
515 + ret = regnode(g, BRANCH);
517 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
518 + latest = regpiece(g, &flags);
519 + if (latest == NULL)
521 + *flagp |= flags&HASWIDTH;
522 + if (chain == NULL) /* First piece. */
523 + *flagp |= flags&SPSTART;
525 + regtail(g, chain, latest);
528 + if (chain == NULL) /* Loop ran zero times. */
529 + (void) regnode(g, NOTHING);
535 + - regpiece - something followed by possible [*+?]
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.
544 +regpiece(struct match_globals *g, int *flagp)
546 + register char *ret;
548 + register char *next;
551 + ret = regatom(g, &flags);
561 + if (!(flags&HASWIDTH) && op != '?')
562 + FAIL("*+ operand could be empty");
563 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
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);
592 + if (ISMULT(*g->regparse))
593 + FAIL("nested *?+");
599 + - regatom - the lowest level
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.
607 +regatom(struct match_globals *g, int *flagp)
609 + register char *ret;
612 + *flagp = WORST; /* Tentatively. */
614 + switch (*g->regparse++) {
616 + ret = regnode(g, BOL);
619 + ret = regnode(g, EOL);
622 + ret = regnode(g, ANY);
623 + *flagp |= HASWIDTH|SIMPLE;
626 + register int class;
627 + register int classend;
629 + if (*g->regparse == '^') { /* Complement of range. */
630 + ret = regnode(g, ANYBUT);
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 == '-') {
639 + if (*g->regparse == ']' || *g->regparse == '\0')
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++)
651 + regc(g, *g->regparse++);
654 + if (*g->regparse != ']')
655 + FAIL("unmatched []");
657 + *flagp |= HASWIDTH|SIMPLE;
661 + ret = reg(g, 1, &flags);
664 + *flagp |= flags&(HASWIDTH|SPSTART);
669 + FAIL("internal urp"); /* Supposed to be caught earlier. */
674 + FAIL("?+* follows nothing");
677 + if (*g->regparse == '\0')
678 + FAIL("trailing \\");
679 + ret = regnode(g, EXACTLY);
680 + regc(g, *g->regparse++);
682 + *flagp |= HASWIDTH|SIMPLE;
686 + register char ender;
689 + len = my_strcspn((const char *)g->regparse, (const char *)META);
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;
698 + ret = regnode(g, EXACTLY);
700 + regc(g, *g->regparse++);
712 + - regnode - emit a node
714 +static char * /* Location. */
715 +regnode(struct match_globals *g, char op)
717 + register char *ret;
718 + register char *ptr;
721 + if (ret == &g->regdummy) {
728 + *ptr++ = '\0'; /* Null "next" pointer. */
736 + - regc - emit (if appropriate) a byte of code
739 +regc(struct match_globals *g, char b)
741 + if (g->regcode != &g->regdummy)
748 + - reginsert - insert an operator in front of already-emitted operand
750 + * Means relocating the operand.
753 +reginsert(struct match_globals *g, char op, char* opnd)
755 + register char *src;
756 + register char *dst;
757 + register char *place;
759 + if (g->regcode == &g->regdummy) {
770 + place = opnd; /* Op node, where operand used to be. */
777 + - regtail - set the next-pointer at the end of a node chain
780 +regtail(struct match_globals *g, char *p, char *val)
782 + register char *scan;
783 + register char *temp;
784 + register int offset;
786 + if (p == &g->regdummy)
789 + /* Find last node. */
792 + temp = regnext(g, scan);
798 + if (OP(scan) == BACK)
799 + offset = scan - val;
801 + offset = val - scan;
802 + *(scan+1) = (offset>>8)&0377;
803 + *(scan+2) = offset&0377;
807 + - regoptail - regtail on operand of first argument; nop if operandless
810 +regoptail(struct match_globals *g, char *p, char *val)
812 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
813 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
815 + regtail(g, OPERAND(p), val);
819 + * regexec and friends
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);
833 +STATIC char *regprop(char *op);
837 + - regexec - match a regexp against a string
840 +regexec(regexp *prog, char *string)
843 + struct match_globals g;
845 + /* Be paranoid... */
846 + if (prog == NULL || string == NULL) {
847 + printk("<3>Regexp: NULL parameter\n");
851 + /* Check validity of program. */
852 + if (UCHARAT(prog->program) != MAGIC) {
853 + printk("<3>Regexp: corrupted program\n");
857 + /* If there is a "must appear" string, look for it. */
858 + if (prog->regmust != NULL) {
860 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
861 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
862 + break; /* Found it. */
865 + if (s == NULL) /* Not present. */
869 + /* Mark beginning of line for ^ . */
872 + /* Simplest case: anchored match need be tried only once. */
874 + return(regtry(&g, prog, string));
876 + /* Messy cases: unanchored match. */
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))
886 + /* We don't -- general case. */
888 + if (regtry(&g, prog, s))
890 + } while (*s++ != '\0');
897 + - regtry - try match at specific point
899 +static int /* 0 failure, 1 success */
900 +regtry(struct match_globals *g, regexp *prog, char *string)
903 + register char **sp;
904 + register char **ep;
906 + g->reginput = string;
907 + g->regstartp = prog->startp;
908 + g->regendp = prog->endp;
912 + for (i = NSUBEXP; i > 0; i--) {
916 + if (regmatch(g, prog->program + 1)) {
917 + prog->startp[0] = string;
918 + prog->endp[0] = g->reginput;
925 + - regmatch - main matching routine
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
934 +static int /* 0 failure, 1 success */
935 +regmatch(struct match_globals *g, char *prog)
937 + register char *scan = prog; /* Current node. */
938 + char *next; /* Next node. */
941 + if (scan != NULL && regnarrate)
942 + fprintf(stderr, "%s(\n", regprop(scan));
944 + while (scan != NULL) {
947 + fprintf(stderr, "%s...\n", regprop(scan));
949 + next = regnext(g, scan);
951 + switch (OP(scan)) {
953 + if (g->reginput != g->regbol)
957 + if (*g->reginput != '\0')
961 + if (*g->reginput == '\0')
967 + register char *opnd;
969 + opnd = OPERAND(scan);
970 + /* Inline the first character, for speed. */
971 + if (*opnd != *g->reginput)
973 + len = strlen(opnd);
974 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
976 + g->reginput += len;
980 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
985 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1002 + register char *save;
1004 + no = OP(scan) - OPEN;
1005 + save = g->reginput;
1007 + if (regmatch(g, next)) {
1009 + * Don't set startp if some later
1010 + * invocation of the same parentheses
1013 + if (g->regstartp[no] == NULL)
1014 + g->regstartp[no] = save;
1031 + register char *save;
1033 + no = OP(scan) - CLOSE;
1034 + save = g->reginput;
1036 + if (regmatch(g, next)) {
1038 + * Don't set endp if some later
1039 + * invocation of the same parentheses
1042 + if (g->regendp[no] == NULL)
1043 + g->regendp[no] = save;
1050 + register char *save;
1052 + if (OP(next) != BRANCH) /* No choice. */
1053 + next = OPERAND(scan); /* Avoid recursion. */
1056 + save = g->reginput;
1057 + if (regmatch(g, OPERAND(scan)))
1059 + g->reginput = save;
1060 + scan = regnext(g, scan);
1061 + } while (scan != NULL && OP(scan) == BRANCH);
1069 + register char nextch;
1071 + register char *save;
1075 + * Lookahead to avoid useless match attempts
1076 + * when we know what character comes next.
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))
1089 + /* Couldn't or didn't -- back up. */
1091 + g->reginput = save + no;
1097 + return(1); /* Success! */
1100 + printk("<3>Regexp: memory corruption\n");
1109 + * We get here only if there's trouble -- normally "case END" is
1110 + * the terminating point.
1112 + printk("<3>Regexp: corrupted pointers\n");
1117 + - regrepeat - repeatedly match something simple, report how many
1120 +regrepeat(struct match_globals *g, char *p)
1122 + register int count = 0;
1123 + register char *scan;
1124 + register char *opnd;
1126 + scan = g->reginput;
1127 + opnd = OPERAND(p);
1130 + count = strlen(scan);
1134 + while (*opnd == *scan) {
1140 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1146 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1151 + default: /* Oh dear. Called inappropriately. */
1152 + printk("<3>Regexp: internal foulup\n");
1153 + count = 0; /* Best compromise. */
1156 + g->reginput = scan;
1162 + - regnext - dig the "next" pointer out of a node
1165 +regnext(struct match_globals *g, char *p)
1167 + register int offset;
1169 + if (p == &g->regdummy)
1176 + if (OP(p) == BACK)
1184 +STATIC char *regprop();
1187 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1193 + register char op = EXACTLY; /* Arbitrary non-END op. */
1194 + register char *next;
1195 + /* extern char *strchr(); */
1198 + s = r->program + 1;
1199 + while (op != END) { /* While that wasn't END last time... */
1201 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1202 + next = regnext(s);
1203 + if (next == NULL) /* Next ptr. */
1206 + printf("(%d)", (s-r->program)+(next-s));
1208 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1209 + /* Literal string, where present. */
1210 + while (*s != '\0') {
1219 + /* Header fields of interest. */
1220 + if (r->regstart != '\0')
1221 + printf("start `%c' ", r->regstart);
1223 + printf("anchored ");
1224 + if (r->regmust != NULL)
1225 + printf("must have \"%s\"", r->regmust);
1230 + - regprop - printable representation of opcode
1237 + static char buf[BUFLEN];
1281 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1293 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1303 + printk("<3>Regexp: corrupted opcode\n");
1307 + strncat(buf, p, BUFLEN-strlen(buf));
1314 +++ b/net/netfilter/regexp/regexp.h
1317 + * Definitions etc. for regexp(3) routines.
1319 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1320 + * not the System V one.
1328 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1329 +which contains a version of this library, says:
1332 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1333 + * will not work properly.
1336 +However, it looks rather like this library is limited to 10. If you think
1337 +otherwise, let us know.
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. */
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);
1358 +++ b/net/netfilter/regexp/regmagic.h
1361 + * The first byte of the regexp internal "program" is actually this magic
1362 + * number; the start node begins in the second byte.
1366 +++ b/net/netfilter/regexp/regsub.c
1370 + * @(#)regsub.c 1.3 of 2 April 86
1372 + * Copyright (c) 1986 by University of Toronto.
1373 + * Written by Henry Spencer. Not derived from licensed software.
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:
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.
1383 + * 2. The origin of this software must not be misrepresented, either
1384 + * by explicit claim or by omission.
1386 + * 3. Altered versions must be plainly marked as such, and must not
1387 + * be misrepresented as being the original software.
1390 + * This code was modified by Ethan Sommer to work within the kernel
1391 + * (it now uses kmalloc etc..)
1394 +#include "regexp.h"
1395 +#include "regmagic.h"
1396 +#include <linux/string.h>
1400 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1402 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1406 +//void regerror(char * s)
1408 +// printk("regexp(3): %s", s);
1409 +// /* NOTREACHED */
1414 + - regsub - perform substitutions after a regexp match
1417 +regsub(regexp * prog, char * source, char * dest)
1419 + register char *src;
1420 + register char *dst;
1425 + /* Not necessary and gcc doesn't like it -MLS */
1426 + /*extern char *strncpy();*/
1428 + if (prog == NULL || source == NULL || dest == NULL) {
1429 + regerror("NULL parm to regsub");
1432 + if (UCHARAT(prog->program) != MAGIC) {
1433 + regerror("damaged regexp fed to regsub");
1439 + while ((c = *src++) != '\0') {
1442 + else if (c == '\\' && '0' <= *src && *src <= '9')
1443 + no = *src++ - '0';
1447 + if (no < 0) { /* Ordinary character. */
1448 + if (c == '\\' && (*src == '\\' || *src == '&'))
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);
1455 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
1456 + regerror("damaged match string");
1464 +++ b/net/netfilter/xt_layer7.c
1467 + Kernel module to match application layer (OSI layer 7) data in connections.
1469 + http://l7-filter.sf.net
1471 + (C) 2003, 2004, 2005, 2006, 2007 Matthew Strait and Ethan Sommer.
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
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.
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>
1498 +#include "regexp/regexp.c"
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");
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)
1512 + #define DPRINTK(format,args...)
1515 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
1516 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
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;
1522 +static struct pattern_cache {
1523 + char * regex_string;
1525 + struct pattern_cache * next;
1526 +} * first_pattern_cache = NULL;
1528 +DEFINE_SPINLOCK(l7_lock);
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)
1535 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
1539 + if (net_ratelimit())
1540 + printk(KERN_ERR "layer7: out of memory in "
1541 + "friendly_print, bailing.\n");
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] = ' ';
1554 +static char dec2hex(int i)
1561 + return (i - 10 + 'a');
1564 + if (net_ratelimit())
1565 + printk("layer7: Problem in dec2hex\n");
1570 +static char * hex_print(unsigned char * s)
1572 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
1576 + if (net_ratelimit())
1577 + printk(KERN_ERR "layer7: out of memory in hex_print, "
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);
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)
1598 + struct pattern_cache * node = first_pattern_cache;
1599 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
1600 + struct pattern_cache * tmp;
1603 + while (node != NULL) {
1604 + if (!strcmp(node->regex_string, regex_string))
1605 + return node->pattern;
1607 + last_pattern_cache = node;/* points at the last non-NULL node */
1608 + node = node->next;
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);
1617 + if (net_ratelimit())
1618 + printk(KERN_ERR "layer7: out of memory in "
1619 + "compile_and_cache, bailing.\n");
1623 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
1624 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
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);
1637 + /* Ok. The new node is all ready now. */
1640 + if(first_pattern_cache == NULL) /* list is empty */
1641 + first_pattern_cache = node; /* make node the beginning */
1643 + last_pattern_cache->next = node; /* attach node to the end */
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 "
1653 + regex_string, protocol);
1654 + /* pattern is now cached as NULL, so we won't try again. */
1657 + strcpy(node->regex_string, regex_string);
1658 + return node->pattern;
1661 +static int can_handle(const struct sk_buff *skb)
1663 + if(!ip_hdr(skb)) /* not IP */
1665 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
1666 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
1667 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
1672 +/* Returns offset the into the skb->data that the application data starts */
1673 +static int app_data_offset(const struct sk_buff *skb)
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;
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);
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 */
1692 + if (net_ratelimit())
1693 + printk(KERN_ERR "layer7: tried to handle unknown "
1695 + return ip_hl + 8; /* something reasonable */
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)
1706 + /* If we're in here, throw the app data away */
1707 + if(master_conntrack->layer7.app_data != NULL) {
1709 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
1710 + if(!master_conntrack->layer7.app_proto) {
1712 + friendly_print(master_conntrack->layer7.app_data);
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);
1719 + DPRINTK("In hex: %s\n", g);
1724 + kfree(master_conntrack->layer7.app_data);
1725 + master_conntrack->layer7.app_data = NULL; /* don't free again */
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,
1734 + if(!conntrack->layer7.app_proto){
1735 + if (net_ratelimit())
1736 + printk(KERN_ERR "layer7: out of memory "
1737 + "in match_no_append, "
1741 + strcpy(conntrack->layer7.app_proto,
1742 + master_conntrack->layer7.app_proto);
1745 + return (!strcmp(master_conntrack->layer7.app_proto,
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");
1759 + strcpy(master_conntrack->layer7.app_proto, "unknown");
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)
1768 + int length = 0, i;
1769 + int oldlength = master_conntrack->layer7.app_data_len;
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;
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];
1790 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
1791 + master_conntrack->layer7.app_data_len = length + oldlength;
1796 +/* taken from drivers/video/modedb.c */
1797 +static int my_atoi(const char *s)
1804 + val = 10*val+(*s-'0');
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)
1816 + if(num_packets > 99 && net_ratelimit())
1817 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
1819 + page[0] = num_packets/10 + '0';
1820 + page[1] = num_packets%10 + '0';
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)
1833 + char * foo = kmalloc(count, GFP_ATOMIC);
1836 + if (net_ratelimit())
1837 + printk(KERN_ERR "layer7: out of memory, bailing. "
1838 + "num_packets unchanged.\n");
1842 + if(copy_from_user(foo, buffer, count)) {
1847 + num_packets = my_atoi(foo);
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");
1855 + } else if(num_packets < 1) {
1856 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
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,
1870 + unsigned int protoff,
1873 + /* sidestep const without getting a compiler warning... */
1874 + struct sk_buff * skb = (struct sk_buff *)skbin;
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;
1883 + /* Be paranoid/incompetent - lock the entire match function. */
1884 + spin_lock_bh(&l7_lock);
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;
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;
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);
1906 + /* if we've classified it or seen too many packets */
1907 + if(TOTAL_PACKETS > num_packets ||
1908 + master_conntrack->layer7.app_proto) {
1910 + pattern_result = match_no_append(conntrack, master_conntrack,
1911 + ctinfo, master_ctinfo, info);
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 */
1920 + spin_unlock_bh(&l7_lock);
1921 + return (pattern_result ^ info->invert);
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;
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;
1938 + /* the return value gets checked later, when we're ready to use it */
1939 + comppattern = compile_and_cache(info->pattern, info->protocol);
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;
1954 + master_conntrack->layer7.app_data[0] = '\0';
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 */
1966 + newbytes = add_data(master_conntrack, app_data, appdatalen);
1968 + if(newbytes == 0) { /* didn't add any data */
1970 + /* Didn't match before, not going to match now */
1971 + spin_unlock_bh(&l7_lock);
1972 + return info->invert;
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;
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);
2003 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
2004 + } else if(pattern_result > 1) { /* cleanup from "unset" */
2005 + pattern_result = 1;
2008 + /* mark the packet seen */
2011 + spin_unlock_bh(&l7_lock);
2012 + return (pattern_result ^ info->invert);
2015 +static int check(const char *tablename,
2017 + const struct xt_match *match,
2019 + unsigned int hook_mask)
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);
2032 +destroy(const struct xt_match *match, void *matchinfo)
2034 + nf_ct_l3proto_module_put(match->family);
2037 +static struct xt_match xt_layer7_match[] = {
2040 + .family = AF_INET,
2041 + .checkentry = check,
2043 + .destroy = destroy,
2044 + .matchsize = sizeof(struct xt_layer7_info),
2049 +static void layer7_cleanup_proc(void)
2051 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2052 + remove_proc_entry("layer7_numpackets", proc_net);
2054 + remove_proc_entry("layer7_numpackets", init_net.proc_net);
2058 +/* register the proc file */
2059 +static void layer7_init_proc(void)
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);
2065 + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
2067 + entry->read_proc = layer7_read_proc;
2068 + entry->write_proc = layer7_write_proc;
2071 +static int __init xt_layer7_init(void)
2075 + layer7_init_proc();
2076 + if(maxdatalen < 1) {
2077 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
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, "
2086 + maxdatalen = 65536;
2088 + return xt_register_matches(xt_layer7_match,
2089 + ARRAY_SIZE(xt_layer7_match));
2092 +static void __exit xt_layer7_fini(void)
2094 + layer7_cleanup_proc();
2095 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
2098 +module_init(xt_layer7_init);
2099 +module_exit(xt_layer7_fini);