1 diff -Naur linux-3.10.5.org/include/linux/netfilter/xt_layer7.h linux-3.10.5/include/linux/netfilter/xt_layer7.h
2 --- linux-3.10.5.org/include/linux/netfilter/xt_layer7.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-3.10.5/include/linux/netfilter/xt_layer7.h 2013-08-07 06:16:14.260806739 +0200
8 +#define MAX_PATTERN_LEN 8192
9 +#define MAX_PROTOCOL_LEN 256
11 +struct xt_layer7_info {
12 + char protocol[MAX_PROTOCOL_LEN];
13 + char pattern[MAX_PATTERN_LEN];
17 +#endif /* _XT_LAYER7_H */
18 diff -Naur linux-3.10.5.org/include/net/netfilter/nf_conntrack.h linux-3.10.5/include/net/netfilter/nf_conntrack.h
19 --- linux-3.10.5.org/include/net/netfilter/nf_conntrack.h 2013-08-04 10:51:49.000000000 +0200
20 +++ linux-3.10.5/include/net/netfilter/nf_conntrack.h 2013-08-07 06:16:14.280806062 +0200
25 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || \
26 + defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
29 + * e.g. "http". NULL before decision. "unknown" after decision
34 + * application layer data so far. NULL after match decision.
37 + unsigned int app_data_len;
41 /* Storage reserved for other modules, must be the last member */
42 union nf_conntrack_proto proto;
44 diff -Naur linux-3.10.5.org/net/netfilter/Kconfig linux-3.10.5/net/netfilter/Kconfig
45 --- linux-3.10.5.org/net/netfilter/Kconfig 2013-08-04 10:51:49.000000000 +0200
46 +++ linux-3.10.5/net/netfilter/Kconfig 2013-08-07 06:16:14.310805048 +0200
47 @@ -1205,6 +1205,26 @@
49 To compile it as a module, choose M here. If unsure, say N.
51 +config NETFILTER_XT_MATCH_LAYER7
52 + tristate '"layer7" match support'
53 + depends on NETFILTER_XTABLES
54 + depends on IP_NF_CONNTRACK || NF_CONNTRACK
56 + Say Y if you want to be able to classify connections (and their
57 + packets) based on regular expression matching of their application
58 + layer data. This is one way to classify applications such as
59 + peer-to-peer filesharing systems that do not always use the same
62 + To compile it as a module, choose M here. If unsure, say N.
64 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
65 + bool 'Layer 7 debugging output'
66 + depends on NETFILTER_XT_MATCH_LAYER7
68 + Say Y to get lots of debugging output.
71 config NETFILTER_XT_MATCH_STATISTIC
72 tristate '"statistic" match support'
73 depends on NETFILTER_ADVANCED
74 diff -Naur linux-3.10.5.org/net/netfilter/Makefile linux-3.10.5/net/netfilter/Makefile
75 --- linux-3.10.5.org/net/netfilter/Makefile 2013-08-04 10:51:49.000000000 +0200
76 +++ linux-3.10.5/net/netfilter/Makefile 2013-08-07 06:16:14.320804710 +0200
78 obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o
79 obj-$(CONFIG_NETFILTER_XT_MATCH_SOCKET) += xt_socket.o
80 obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o
81 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
82 obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o
83 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
84 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
85 diff -Naur linux-3.10.5.org/net/netfilter/nf_conntrack_core.c linux-3.10.5/net/netfilter/nf_conntrack_core.c
86 --- linux-3.10.5.org/net/netfilter/nf_conntrack_core.c 2013-08-04 10:51:49.000000000 +0200
87 +++ linux-3.10.5/net/netfilter/nf_conntrack_core.c 2013-08-07 06:20:34.941991510 +0200
92 /* Connection state tracking for netfilter. This is separated from,
93 but required by, the NAT layer; it can also be used by an iptables
97 nf_ct_remove_expectations(ct);
99 + #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
100 + if(ct->layer7.app_proto)
101 + kfree(ct->layer7.app_proto);
102 + if(ct->layer7.app_data)
103 + kfree(ct->layer7.app_data);
106 /* We overload first tuple to link into unconfirmed or dying list.*/
107 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
108 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
109 diff -Naur linux-3.10.5.org/net/netfilter/nf_conntrack_standalone.c linux-3.10.5/net/netfilter/nf_conntrack_standalone.c
110 --- linux-3.10.5.org/net/netfilter/nf_conntrack_standalone.c 2013-08-04 10:51:49.000000000 +0200
111 +++ linux-3.10.5/net/netfilter/nf_conntrack_standalone.c 2013-08-07 06:16:14.380802681 +0200
113 if (ct_show_delta_time(s, ct))
116 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
117 + if(ct->layer7.app_proto &&
118 + seq_printf(s, "l7proto=%s ", ct->layer7.app_proto))
122 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
125 diff -Naur linux-3.10.5.org/net/netfilter/regexp/regexp.c linux-3.10.5/net/netfilter/regexp/regexp.c
126 --- linux-3.10.5.org/net/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
127 +++ linux-3.10.5/net/netfilter/regexp/regexp.c 2013-08-07 06:16:14.410801666 +0200
130 + * regcomp and regexec -- regsub and regerror are elsewhere
131 + * @(#)regexp.c 1.3 of 18 April 87
133 + * Copyright (c) 1986 by University of Toronto.
134 + * Written by Henry Spencer. Not derived from licensed software.
136 + * Permission is granted to anyone to use this software for any
137 + * purpose on any computer system, and to redistribute it freely,
138 + * subject to the following restrictions:
140 + * 1. The author is not responsible for the consequences of use of
141 + * this software, no matter how awful, even if they arise
142 + * from defects in it.
144 + * 2. The origin of this software must not be misrepresented, either
145 + * by explicit claim or by omission.
147 + * 3. Altered versions must be plainly marked as such, and must not
148 + * be misrepresented as being the original software.
150 + * Beware that some of this code is subtly aware of the way operator
151 + * precedence is structured in regular expressions. Serious changes in
152 + * regular-expression syntax might require a total rethink.
154 + * This code was modified by Ethan Sommer to work within the kernel
155 + * (it now uses kmalloc etc..)
157 + * Modified slightly by Matthew Strait to use more modern C.
161 +#include "regmagic.h"
163 +/* added by ethan and matt. Lets it work in both kernel and user space.
164 +(So iptables can use it, for instance.) Yea, it goes both ways... */
166 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
168 + #define printk(format,args...) printf(format,##args)
171 +void regerror(char * s)
173 + printk("<3>Regexp: %s\n", s);
178 + * The "internal use only" fields in regexp.h are present to pass info from
179 + * compile to execute that permits the execute phase to run lots faster on
180 + * simple cases. They are:
182 + * regstart char that must begin a match; '\0' if none obvious
183 + * reganch is the match anchored (at beginning-of-line only)?
184 + * regmust string (pointer into program) that match must include, or NULL
185 + * regmlen length of regmust string
187 + * Regstart and reganch permit very fast decisions on suitable starting points
188 + * for a match, cutting down the work a lot. Regmust permits fast rejection
189 + * of lines that cannot possibly match. The regmust tests are costly enough
190 + * that regcomp() supplies a regmust only if the r.e. contains something
191 + * potentially expensive (at present, the only such thing detected is * or +
192 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
193 + * supplied because the test in regexec() needs it and regcomp() is computing
198 + * Structure for regexp "program". This is essentially a linear encoding
199 + * of a nondeterministic finite-state machine (aka syntax charts or
200 + * "railroad normal form" in parsing technology). Each node is an opcode
201 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
202 + * all nodes except BRANCH implement concatenation; a "next" pointer with
203 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
204 + * have one of the subtle syntax dependencies: an individual BRANCH (as
205 + * opposed to a collection of them) is never concatenated with anything
206 + * because of operator precedence.) The operand of some types of node is
207 + * a literal string; for others, it is a node leading into a sub-FSM. In
208 + * particular, the operand of a BRANCH node is the first node of the branch.
209 + * (NB this is *not* a tree structure: the tail of the branch connects
210 + * to the thing following the set of BRANCHes.) The opcodes are:
213 +/* definition number opnd? meaning */
214 +#define END 0 /* no End of program. */
215 +#define BOL 1 /* no Match "" at beginning of line. */
216 +#define EOL 2 /* no Match "" at end of line. */
217 +#define ANY 3 /* no Match any one character. */
218 +#define ANYOF 4 /* str Match any character in this string. */
219 +#define ANYBUT 5 /* str Match any character not in this string. */
220 +#define BRANCH 6 /* node Match this alternative, or the next... */
221 +#define BACK 7 /* no Match "", "next" ptr points backward. */
222 +#define EXACTLY 8 /* str Match this string. */
223 +#define NOTHING 9 /* no Match empty string. */
224 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
225 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
226 +#define OPEN 20 /* no Mark this point in input as start of #n. */
227 + /* OPEN+1 is number 1, etc. */
228 +#define CLOSE 30 /* no Analogous to OPEN. */
233 + * BRANCH The set of branches constituting a single choice are hooked
234 + * together with their "next" pointers, since precedence prevents
235 + * anything being concatenated to any individual branch. The
236 + * "next" pointer of the last BRANCH in a choice points to the
237 + * thing following the whole choice. This is also where the
238 + * final "next" pointer of each individual branch points; each
239 + * branch starts with the operand node of a BRANCH node.
241 + * BACK Normal "next" pointers all implicitly point forward; BACK
242 + * exists to make loop structures possible.
244 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
245 + * BRANCH structures using BACK. Simple cases (one character
246 + * per match) are implemented with STAR and PLUS for speed
247 + * and to minimize recursive plunges.
249 + * OPEN,CLOSE ...are numbered at compile time.
253 + * A node is one char of opcode followed by two chars of "next" pointer.
254 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
255 + * value is a positive offset from the opcode of the node containing it.
256 + * An operand, if any, simply follows the node. (Note that much of the
257 + * code generation knows about this implicit relationship.)
259 + * Using two bytes for the "next" pointer is vast overkill for most things,
260 + * but allows patterns to get big without disasters.
262 +#define OP(p) (*(p))
263 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
264 +#define OPERAND(p) ((p) + 3)
267 + * See regmagic.h for one further detail of program structure.
272 + * Utility definitions.
275 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
277 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
280 +#define FAIL(m) { regerror(m); return(NULL); }
281 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
282 +#define META "^$.[()|?+*\\"
285 + * Flags to be passed up and down.
287 +#define HASWIDTH 01 /* Known never to match null string. */
288 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
289 +#define SPSTART 04 /* Starts with * or +. */
290 +#define WORST 0 /* Worst case. */
293 + * Global work variables for regcomp().
295 +struct match_globals {
296 +char *reginput; /* String-input pointer. */
297 +char *regbol; /* Beginning of input, for ^ check. */
298 +char **regstartp; /* Pointer to startp array. */
299 +char **regendp; /* Ditto for endp. */
300 +char *regparse; /* Input-scan pointer. */
301 +int regnpar; /* () count. */
303 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
304 +long regsize; /* Code size. */
308 + * Forward declarations for regcomp()'s friends.
311 +#define STATIC static
313 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
314 +STATIC char *regbranch(struct match_globals *g, int *flagp);
315 +STATIC char *regpiece(struct match_globals *g, int *flagp);
316 +STATIC char *regatom(struct match_globals *g, int *flagp);
317 +STATIC char *regnode(struct match_globals *g, char op);
318 +STATIC char *regnext(struct match_globals *g, char *p);
319 +STATIC void regc(struct match_globals *g, char b);
320 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
321 +STATIC void regtail(struct match_globals *g, char *p, char *val);
322 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
325 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
332 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
333 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
334 + if (*scan1 == *scan2++)
342 + - regcomp - compile a regular expression into internal code
344 + * We can't allocate space until we know how big the compiled form will be,
345 + * but we can't compile it (and thus know how big it is) until we've got a
346 + * place to put the code. So we cheat: we compile it twice, once with code
347 + * generation turned off and size counting turned on, and once "for real".
348 + * This also means that we don't allocate space until we are sure that the
349 + * thing really will compile successfully, and we never have to move the
350 + * code and thus invalidate pointers into it. (Note that it has to be in
351 + * one piece because free() must be able to free it all.)
353 + * Beware that the optimization-preparation code in here knows about some
354 + * of the structure of the compiled regexp.
357 +regcomp(char *exp,int *patternsize)
359 + register regexp *r;
360 + register char *scan;
361 + register char *longest;
364 + struct match_globals g;
366 + /* commented out by ethan
367 + extern char *malloc();
371 + FAIL("NULL argument");
373 + /* First pass: determine size, legality. */
377 + g.regcode = &g.regdummy;
379 + if (reg(&g, 0, &flags) == NULL)
382 + /* Small enough for pointer-storage convention? */
383 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
384 + FAIL("regexp too big");
386 + /* Allocate space. */
387 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
388 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
390 + FAIL("out of space");
392 + /* Second pass: emit code. */
395 + g.regcode = r->program;
397 + if (reg(&g, 0, &flags) == NULL)
400 + /* Dig out information for optimizations. */
401 + r->regstart = '\0'; /* Worst-case defaults. */
405 + scan = r->program+1; /* First BRANCH. */
406 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
407 + scan = OPERAND(scan);
409 + /* Starting-point info. */
410 + if (OP(scan) == EXACTLY)
411 + r->regstart = *OPERAND(scan);
412 + else if (OP(scan) == BOL)
416 + * If there's something expensive in the r.e., find the
417 + * longest literal string that must appear and make it the
418 + * regmust. Resolve ties in favor of later strings, since
419 + * the regstart check works with the beginning of the r.e.
420 + * and avoiding duplication strengthens checking. Not a
421 + * strong reason, but sufficient in the absence of others.
423 + if (flags&SPSTART) {
426 + for (; scan != NULL; scan = regnext(&g, scan))
427 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
428 + longest = OPERAND(scan);
429 + len = strlen(OPERAND(scan));
431 + r->regmust = longest;
440 + - reg - regular expression, i.e. main body or parenthesized thing
442 + * Caller must absorb opening parenthesis.
444 + * Combining parenthesis handling with the base level of regular expression
445 + * is a trifle forced, but the need to tie the tails of the branches to what
446 + * follows makes it hard to avoid.
449 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
451 + register char *ret;
453 + register char *ender;
454 + register int parno = 0; /* 0 makes gcc happy */
457 + *flagp = HASWIDTH; /* Tentatively. */
459 + /* Make an OPEN node, if parenthesized. */
461 + if (g->regnpar >= NSUBEXP)
462 + FAIL("too many ()");
463 + parno = g->regnpar;
465 + ret = regnode(g, OPEN+parno);
469 + /* Pick up the branches, linking them together. */
470 + br = regbranch(g, &flags);
474 + regtail(g, ret, br); /* OPEN -> first. */
477 + if (!(flags&HASWIDTH))
478 + *flagp &= ~HASWIDTH;
479 + *flagp |= flags&SPSTART;
480 + while (*g->regparse == '|') {
482 + br = regbranch(g, &flags);
485 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
486 + if (!(flags&HASWIDTH))
487 + *flagp &= ~HASWIDTH;
488 + *flagp |= flags&SPSTART;
491 + /* Make a closing node, and hook it on the end. */
492 + ender = regnode(g, (paren) ? CLOSE+parno : END);
493 + regtail(g, ret, ender);
495 + /* Hook the tails of the branches to the closing node. */
496 + for (br = ret; br != NULL; br = regnext(g, br))
497 + regoptail(g, br, ender);
499 + /* Check for proper termination. */
500 + if (paren && *g->regparse++ != ')') {
501 + FAIL("unmatched ()");
502 + } else if (!paren && *g->regparse != '\0') {
503 + if (*g->regparse == ')') {
504 + FAIL("unmatched ()");
506 + FAIL("junk on end"); /* "Can't happen". */
514 + - regbranch - one alternative of an | operator
516 + * Implements the concatenation operator.
519 +regbranch(struct match_globals *g, int *flagp)
521 + register char *ret;
522 + register char *chain;
523 + register char *latest;
526 + *flagp = WORST; /* Tentatively. */
528 + ret = regnode(g, BRANCH);
530 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
531 + latest = regpiece(g, &flags);
532 + if (latest == NULL)
534 + *flagp |= flags&HASWIDTH;
535 + if (chain == NULL) /* First piece. */
536 + *flagp |= flags&SPSTART;
538 + regtail(g, chain, latest);
541 + if (chain == NULL) /* Loop ran zero times. */
542 + (void) regnode(g, NOTHING);
548 + - regpiece - something followed by possible [*+?]
550 + * Note that the branching code sequences used for ? and the general cases
551 + * of * and + are somewhat optimized: they use the same NOTHING node as
552 + * both the endmarker for their branch list and the body of the last branch.
553 + * It might seem that this node could be dispensed with entirely, but the
554 + * endmarker role is not redundant.
557 +regpiece(struct match_globals *g, int *flagp)
559 + register char *ret;
561 + register char *next;
564 + ret = regatom(g, &flags);
574 + if (!(flags&HASWIDTH) && op != '?')
575 + FAIL("*+ operand could be empty");
576 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
578 + if (op == '*' && (flags&SIMPLE))
579 + reginsert(g, STAR, ret);
580 + else if (op == '*') {
581 + /* Emit x* as (x&|), where & means "self". */
582 + reginsert(g, BRANCH, ret); /* Either x */
583 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
584 + regoptail(g, ret, ret); /* back */
585 + regtail(g, ret, regnode(g, BRANCH)); /* or */
586 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
587 + } else if (op == '+' && (flags&SIMPLE))
588 + reginsert(g, PLUS, ret);
589 + else if (op == '+') {
590 + /* Emit x+ as x(&|), where & means "self". */
591 + next = regnode(g, BRANCH); /* Either */
592 + regtail(g, ret, next);
593 + regtail(g, regnode(g, BACK), ret); /* loop back */
594 + regtail(g, next, regnode(g, BRANCH)); /* or */
595 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
596 + } else if (op == '?') {
597 + /* Emit x? as (x|) */
598 + reginsert(g, BRANCH, ret); /* Either x */
599 + regtail(g, ret, regnode(g, BRANCH)); /* or */
600 + next = regnode(g, NOTHING); /* null. */
601 + regtail(g, ret, next);
602 + regoptail(g, ret, next);
605 + if (ISMULT(*g->regparse))
606 + FAIL("nested *?+");
612 + - regatom - the lowest level
614 + * Optimization: gobbles an entire sequence of ordinary characters so that
615 + * it can turn them into a single node, which is smaller to store and
616 + * faster to run. Backslashed characters are exceptions, each becoming a
617 + * separate node; the code is simpler that way and it's not worth fixing.
620 +regatom(struct match_globals *g, int *flagp)
622 + register char *ret;
625 + *flagp = WORST; /* Tentatively. */
627 + switch (*g->regparse++) {
629 + ret = regnode(g, BOL);
632 + ret = regnode(g, EOL);
635 + ret = regnode(g, ANY);
636 + *flagp |= HASWIDTH|SIMPLE;
639 + register int class;
640 + register int classend;
642 + if (*g->regparse == '^') { /* Complement of range. */
643 + ret = regnode(g, ANYBUT);
646 + ret = regnode(g, ANYOF);
647 + if (*g->regparse == ']' || *g->regparse == '-')
648 + regc(g, *g->regparse++);
649 + while (*g->regparse != '\0' && *g->regparse != ']') {
650 + if (*g->regparse == '-') {
652 + if (*g->regparse == ']' || *g->regparse == '\0')
655 + class = UCHARAT(g->regparse-2)+1;
656 + classend = UCHARAT(g->regparse);
657 + if (class > classend+1)
658 + FAIL("invalid [] range");
659 + for (; class <= classend; class++)
664 + regc(g, *g->regparse++);
667 + if (*g->regparse != ']')
668 + FAIL("unmatched []");
670 + *flagp |= HASWIDTH|SIMPLE;
674 + ret = reg(g, 1, &flags);
677 + *flagp |= flags&(HASWIDTH|SPSTART);
682 + FAIL("internal urp"); /* Supposed to be caught earlier. */
687 + FAIL("?+* follows nothing");
690 + if (*g->regparse == '\0')
691 + FAIL("trailing \\");
692 + ret = regnode(g, EXACTLY);
693 + regc(g, *g->regparse++);
695 + *flagp |= HASWIDTH|SIMPLE;
699 + register char ender;
702 + len = my_strcspn((const char *)g->regparse, (const char *)META);
704 + FAIL("internal disaster");
705 + ender = *(g->regparse+len);
706 + if (len > 1 && ISMULT(ender))
707 + len--; /* Back off clear of ?+* operand. */
708 + *flagp |= HASWIDTH;
711 + ret = regnode(g, EXACTLY);
713 + regc(g, *g->regparse++);
725 + - regnode - emit a node
727 +static char * /* Location. */
728 +regnode(struct match_globals *g, char op)
730 + register char *ret;
731 + register char *ptr;
734 + if (ret == &g->regdummy) {
741 + *ptr++ = '\0'; /* Null "next" pointer. */
749 + - regc - emit (if appropriate) a byte of code
752 +regc(struct match_globals *g, char b)
754 + if (g->regcode != &g->regdummy)
761 + - reginsert - insert an operator in front of already-emitted operand
763 + * Means relocating the operand.
766 +reginsert(struct match_globals *g, char op, char* opnd)
768 + register char *src;
769 + register char *dst;
770 + register char *place;
772 + if (g->regcode == &g->regdummy) {
783 + place = opnd; /* Op node, where operand used to be. */
790 + - regtail - set the next-pointer at the end of a node chain
793 +regtail(struct match_globals *g, char *p, char *val)
795 + register char *scan;
796 + register char *temp;
797 + register int offset;
799 + if (p == &g->regdummy)
802 + /* Find last node. */
805 + temp = regnext(g, scan);
811 + if (OP(scan) == BACK)
812 + offset = scan - val;
814 + offset = val - scan;
815 + *(scan+1) = (offset>>8)&0377;
816 + *(scan+2) = offset&0377;
820 + - regoptail - regtail on operand of first argument; nop if operandless
823 +regoptail(struct match_globals *g, char *p, char *val)
825 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
826 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
828 + regtail(g, OPERAND(p), val);
832 + * regexec and friends
839 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
840 +STATIC int regmatch(struct match_globals *g, char *prog);
841 +STATIC int regrepeat(struct match_globals *g, char *p);
846 +STATIC char *regprop(char *op);
850 + - regexec - match a regexp against a string
853 +regexec(regexp *prog, char *string)
856 + struct match_globals g;
858 + /* Be paranoid... */
859 + if (prog == NULL || string == NULL) {
860 + printk("<3>Regexp: NULL parameter\n");
864 + /* Check validity of program. */
865 + if (UCHARAT(prog->program) != MAGIC) {
866 + printk("<3>Regexp: corrupted program\n");
870 + /* If there is a "must appear" string, look for it. */
871 + if (prog->regmust != NULL) {
873 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
874 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
875 + break; /* Found it. */
878 + if (s == NULL) /* Not present. */
882 + /* Mark beginning of line for ^ . */
885 + /* Simplest case: anchored match need be tried only once. */
887 + return(regtry(&g, prog, string));
889 + /* Messy cases: unanchored match. */
891 + if (prog->regstart != '\0')
892 + /* We know what char it must start with. */
893 + while ((s = strchr(s, prog->regstart)) != NULL) {
894 + if (regtry(&g, prog, s))
899 + /* We don't -- general case. */
901 + if (regtry(&g, prog, s))
903 + } while (*s++ != '\0');
910 + - regtry - try match at specific point
912 +static int /* 0 failure, 1 success */
913 +regtry(struct match_globals *g, regexp *prog, char *string)
916 + register char **sp;
917 + register char **ep;
919 + g->reginput = string;
920 + g->regstartp = prog->startp;
921 + g->regendp = prog->endp;
925 + for (i = NSUBEXP; i > 0; i--) {
929 + if (regmatch(g, prog->program + 1)) {
930 + prog->startp[0] = string;
931 + prog->endp[0] = g->reginput;
938 + - regmatch - main matching routine
940 + * Conceptually the strategy is simple: check to see whether the current
941 + * node matches, call self recursively to see whether the rest matches,
942 + * and then act accordingly. In practice we make some effort to avoid
943 + * recursion, in particular by going through "ordinary" nodes (that don't
944 + * need to know whether the rest of the match failed) by a loop instead of
947 +static int /* 0 failure, 1 success */
948 +regmatch(struct match_globals *g, char *prog)
950 + register char *scan = prog; /* Current node. */
951 + char *next; /* Next node. */
954 + if (scan != NULL && regnarrate)
955 + fprintf(stderr, "%s(\n", regprop(scan));
957 + while (scan != NULL) {
960 + fprintf(stderr, "%s...\n", regprop(scan));
962 + next = regnext(g, scan);
964 + switch (OP(scan)) {
966 + if (g->reginput != g->regbol)
970 + if (*g->reginput != '\0')
974 + if (*g->reginput == '\0')
980 + register char *opnd;
982 + opnd = OPERAND(scan);
983 + /* Inline the first character, for speed. */
984 + if (*opnd != *g->reginput)
986 + len = strlen(opnd);
987 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
989 + g->reginput += len;
993 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
998 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1015 + register char *save;
1017 + no = OP(scan) - OPEN;
1018 + save = g->reginput;
1020 + if (regmatch(g, next)) {
1022 + * Don't set startp if some later
1023 + * invocation of the same parentheses
1026 + if (g->regstartp[no] == NULL)
1027 + g->regstartp[no] = save;
1044 + register char *save;
1046 + no = OP(scan) - CLOSE;
1047 + save = g->reginput;
1049 + if (regmatch(g, next)) {
1051 + * Don't set endp if some later
1052 + * invocation of the same parentheses
1055 + if (g->regendp[no] == NULL)
1056 + g->regendp[no] = save;
1063 + register char *save;
1065 + if (OP(next) != BRANCH) /* No choice. */
1066 + next = OPERAND(scan); /* Avoid recursion. */
1069 + save = g->reginput;
1070 + if (regmatch(g, OPERAND(scan)))
1072 + g->reginput = save;
1073 + scan = regnext(g, scan);
1074 + } while (scan != NULL && OP(scan) == BRANCH);
1082 + register char nextch;
1084 + register char *save;
1088 + * Lookahead to avoid useless match attempts
1089 + * when we know what character comes next.
1092 + if (OP(next) == EXACTLY)
1093 + nextch = *OPERAND(next);
1094 + min = (OP(scan) == STAR) ? 0 : 1;
1095 + save = g->reginput;
1096 + no = regrepeat(g, OPERAND(scan));
1097 + while (no >= min) {
1098 + /* If it could work, try it. */
1099 + if (nextch == '\0' || *g->reginput == nextch)
1100 + if (regmatch(g, next))
1102 + /* Couldn't or didn't -- back up. */
1104 + g->reginput = save + no;
1110 + return(1); /* Success! */
1113 + printk("<3>Regexp: memory corruption\n");
1122 + * We get here only if there's trouble -- normally "case END" is
1123 + * the terminating point.
1125 + printk("<3>Regexp: corrupted pointers\n");
1130 + - regrepeat - repeatedly match something simple, report how many
1133 +regrepeat(struct match_globals *g, char *p)
1135 + register int count = 0;
1136 + register char *scan;
1137 + register char *opnd;
1139 + scan = g->reginput;
1140 + opnd = OPERAND(p);
1143 + count = strlen(scan);
1147 + while (*opnd == *scan) {
1153 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1159 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1164 + default: /* Oh dear. Called inappropriately. */
1165 + printk("<3>Regexp: internal foulup\n");
1166 + count = 0; /* Best compromise. */
1169 + g->reginput = scan;
1175 + - regnext - dig the "next" pointer out of a node
1178 +regnext(struct match_globals *g, char *p)
1180 + register int offset;
1182 + if (p == &g->regdummy)
1189 + if (OP(p) == BACK)
1197 +STATIC char *regprop();
1200 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1206 + register char op = EXACTLY; /* Arbitrary non-END op. */
1207 + register char *next;
1208 + /* extern char *strchr(); */
1211 + s = r->program + 1;
1212 + while (op != END) { /* While that wasn't END last time... */
1214 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1215 + next = regnext(s);
1216 + if (next == NULL) /* Next ptr. */
1219 + printf("(%d)", (s-r->program)+(next-s));
1221 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1222 + /* Literal string, where present. */
1223 + while (*s != '\0') {
1232 + /* Header fields of interest. */
1233 + if (r->regstart != '\0')
1234 + printf("start `%c' ", r->regstart);
1236 + printf("anchored ");
1237 + if (r->regmust != NULL)
1238 + printf("must have \"%s\"", r->regmust);
1243 + - regprop - printable representation of opcode
1250 + static char buf[BUFLEN];
1294 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1306 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1316 + printk("<3>Regexp: corrupted opcode\n");
1320 + strncat(buf, p, BUFLEN-strlen(buf));
1326 diff -Naur linux-3.10.5.org/net/netfilter/regexp/regexp.h linux-3.10.5/net/netfilter/regexp/regexp.h
1327 --- linux-3.10.5.org/net/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1328 +++ linux-3.10.5/net/netfilter/regexp/regexp.h 2013-08-07 06:16:14.430800990 +0200
1331 + * Definitions etc. for regexp(3) routines.
1333 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1334 + * not the System V one.
1342 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1343 +which contains a version of this library, says:
1346 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1347 + * will not work properly.
1350 +However, it looks rather like this library is limited to 10. If you think
1351 +otherwise, let us know.
1355 +typedef struct regexp {
1356 + char *startp[NSUBEXP];
1357 + char *endp[NSUBEXP];
1358 + char regstart; /* Internal use only. */
1359 + char reganch; /* Internal use only. */
1360 + char *regmust; /* Internal use only. */
1361 + int regmlen; /* Internal use only. */
1362 + char program[1]; /* Unwarranted chumminess with compiler. */
1365 +regexp * regcomp(char *exp, int *patternsize);
1366 +int regexec(regexp *prog, char *string);
1367 +void regsub(regexp *prog, char *source, char *dest);
1368 +void regerror(char *s);
1371 diff -Naur linux-3.10.5.org/net/netfilter/regexp/regmagic.h linux-3.10.5/net/netfilter/regexp/regmagic.h
1372 --- linux-3.10.5.org/net/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1373 +++ linux-3.10.5/net/netfilter/regexp/regmagic.h 2013-08-07 06:16:14.450800314 +0200
1376 + * The first byte of the regexp internal "program" is actually this magic
1377 + * number; the start node begins in the second byte.
1380 diff -Naur linux-3.10.5.org/net/netfilter/regexp/regsub.c linux-3.10.5/net/netfilter/regexp/regsub.c
1381 --- linux-3.10.5.org/net/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1382 +++ linux-3.10.5/net/netfilter/regexp/regsub.c 2013-08-07 06:16:14.450800314 +0200
1386 + * @(#)regsub.c 1.3 of 2 April 86
1388 + * Copyright (c) 1986 by University of Toronto.
1389 + * Written by Henry Spencer. Not derived from licensed software.
1391 + * Permission is granted to anyone to use this software for any
1392 + * purpose on any computer system, and to redistribute it freely,
1393 + * subject to the following restrictions:
1395 + * 1. The author is not responsible for the consequences of use of
1396 + * this software, no matter how awful, even if they arise
1397 + * from defects in it.
1399 + * 2. The origin of this software must not be misrepresented, either
1400 + * by explicit claim or by omission.
1402 + * 3. Altered versions must be plainly marked as such, and must not
1403 + * be misrepresented as being the original software.
1406 + * This code was modified by Ethan Sommer to work within the kernel
1407 + * (it now uses kmalloc etc..)
1410 +#include "regexp.h"
1411 +#include "regmagic.h"
1412 +#include <linux/string.h>
1416 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1418 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1422 +//void regerror(char * s)
1424 +// printk("regexp(3): %s", s);
1425 +// /* NOTREACHED */
1430 + - regsub - perform substitutions after a regexp match
1433 +regsub(regexp * prog, char * source, char * dest)
1435 + register char *src;
1436 + register char *dst;
1441 + /* Not necessary and gcc doesn't like it -MLS */
1442 + /*extern char *strncpy();*/
1444 + if (prog == NULL || source == NULL || dest == NULL) {
1445 + regerror("NULL parm to regsub");
1448 + if (UCHARAT(prog->program) != MAGIC) {
1449 + regerror("damaged regexp fed to regsub");
1455 + while ((c = *src++) != '\0') {
1458 + else if (c == '\\' && '0' <= *src && *src <= '9')
1459 + no = *src++ - '0';
1463 + if (no < 0) { /* Ordinary character. */
1464 + if (c == '\\' && (*src == '\\' || *src == '&'))
1467 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
1468 + len = prog->endp[no] - prog->startp[no];
1469 + (void) strncpy(dst, prog->startp[no], len);
1471 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
1472 + regerror("damaged match string");
1479 diff -Naur linux-3.10.5.org/net/netfilter/xt_layer7.c linux-3.10.5/net/netfilter/xt_layer7.c
1480 --- linux-3.10.5.org/net/netfilter/xt_layer7.c 1970-01-01 01:00:00.000000000 +0100
1481 +++ linux-3.10.5/net/netfilter/xt_layer7.c 2013-08-07 06:16:14.490798961 +0200
1484 + Kernel module to match application layer (OSI layer 7) data in connections.
1486 + http://l7-filter.sf.net
1488 + (C) 2003-2009 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 <linux/version.h>
1503 +#include <net/ip.h>
1504 +#include <net/tcp.h>
1505 +#include <linux/module.h>
1506 +#include <linux/skbuff.h>
1507 +#include <linux/netfilter.h>
1508 +#include <net/netfilter/nf_conntrack.h>
1509 +#include <net/netfilter/nf_conntrack_core.h>
1510 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
1511 +#include <net/netfilter/nf_conntrack_extend.h>
1512 +#include <net/netfilter/nf_conntrack_acct.h>
1514 +#include <linux/netfilter/x_tables.h>
1515 +#include <linux/netfilter/xt_layer7.h>
1516 +#include <linux/ctype.h>
1517 +#include <linux/proc_fs.h>
1519 +#include "regexp/regexp.c"
1521 +MODULE_LICENSE("GPL");
1522 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
1523 +MODULE_DESCRIPTION("iptables application layer match module");
1524 +MODULE_ALIAS("ipt_layer7");
1525 +MODULE_VERSION("2.22ipfire");
1527 +static int maxdatalen = 2048; // this is the default
1528 +module_param(maxdatalen, int, 0444);
1529 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
1530 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
1531 + #define DPRINTK(format,args...) printk(format,##args)
1533 + #define DPRINTK(format,args...)
1536 +/* Number of packets whose data we look at.
1537 +This can be modified through /proc/net/layer7_numpackets */
1538 +static int num_packets = 10;
1540 +static struct pattern_cache {
1541 + char * regex_string;
1543 + struct pattern_cache * next;
1544 +} * first_pattern_cache = NULL;
1546 +DEFINE_SPINLOCK(l7_lock);
1548 +static int total_acct_packets(struct nf_conn *ct)
1550 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 26)
1551 + BUG_ON(ct == NULL);
1552 + return (ct->counters[IP_CT_DIR_ORIGINAL].packets + ct->counters[IP_CT_DIR_REPLY].packets);
1554 + struct nf_conn_counter *acct;
1556 + BUG_ON(ct == NULL);
1557 + acct = nf_conn_acct_find(ct);
1560 + return ( atomic64_read(&acct[IP_CT_DIR_ORIGINAL].packets) + atomic64_read(&acct[IP_CT_DIR_REPLY].packets) );
1564 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
1565 +/* Converts an unfriendly string into a friendly one by
1566 +replacing unprintables with periods and all whitespace with " ". */
1567 +static char * friendly_print(unsigned char * s)
1569 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
1573 + if (net_ratelimit())
1574 + printk(KERN_ERR "layer7: out of memory in "
1575 + "friendly_print, bailing.\n");
1579 + for(i = 0; i < strlen(s); i++){
1580 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
1581 + else if(isspace(s[i])) f[i] = ' ';
1588 +static char dec2hex(int i)
1595 + return (i - 10 + 'a');
1598 + if (net_ratelimit())
1599 + printk("layer7: Problem in dec2hex\n");
1604 +static char * hex_print(unsigned char * s)
1606 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
1610 + if (net_ratelimit())
1611 + printk(KERN_ERR "layer7: out of memory in hex_print, "
1616 + for(i = 0; i < strlen(s); i++) {
1617 + g[i*3 ] = dec2hex(s[i]/16);
1618 + g[i*3 + 1] = dec2hex(s[i]%16);
1627 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
1628 +over again, it make sense to cache the results. */
1629 +static regexp * compile_and_cache(const char * regex_string,
1630 + const char * protocol)
1632 + struct pattern_cache * node = first_pattern_cache;
1633 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
1634 + struct pattern_cache * tmp;
1637 + while (node != NULL) {
1638 + if (!strcmp(node->regex_string, regex_string))
1639 + return node->pattern;
1641 + last_pattern_cache = node;/* points at the last non-NULL node */
1642 + node = node->next;
1645 + /* If we reach the end of the list, then we have not yet cached
1646 + the pattern for this regex. Let's do that now.
1647 + Be paranoid about running out of memory to avoid list corruption. */
1648 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
1651 + if (net_ratelimit())
1652 + printk(KERN_ERR "layer7: out of memory in "
1653 + "compile_and_cache, bailing.\n");
1657 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
1658 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
1661 + if(!tmp->regex_string || !tmp->pattern) {
1662 + if (net_ratelimit())
1663 + printk(KERN_ERR "layer7: out of memory in "
1664 + "compile_and_cache, bailing.\n");
1665 + kfree(tmp->regex_string);
1666 + kfree(tmp->pattern);
1671 + /* Ok. The new node is all ready now. */
1674 + if(first_pattern_cache == NULL) /* list is empty */
1675 + first_pattern_cache = node; /* make node the beginning */
1677 + last_pattern_cache->next = node; /* attach node to the end */
1679 + /* copy the string and compile the regex */
1680 + len = strlen(regex_string);
1681 + DPRINTK("layer7: about to compile this: \"%s\"\n", regex_string);
1682 + node->pattern = regcomp((char *)regex_string, &len);
1683 + if ( !node->pattern ) {
1684 + if (net_ratelimit())
1685 + printk(KERN_ERR "layer7: Error compiling regexp "
1687 + regex_string, protocol);
1688 + /* pattern is now cached as NULL, so we won't try again. */
1691 + strcpy(node->regex_string, regex_string);
1692 + return node->pattern;
1695 +static int can_handle(const struct sk_buff *skb)
1697 + if(!ip_hdr(skb)) /* not IP */
1699 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
1700 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
1701 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
1706 +/* Returns offset the into the skb->data that the application data starts */
1707 +static int app_data_offset(const struct sk_buff *skb)
1709 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
1710 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
1711 + int ip_hl = 4*ip_hdr(skb)->ihl;
1713 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
1714 + /* 12 == offset into TCP header for the header length field.
1715 + Can't get this with skb->h.th->doff because the tcphdr
1716 + struct doesn't get set when routing (this is confirmed to be
1717 + true in Netfilter as well as QoS.) */
1718 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
1720 + return ip_hl + tcp_hl;
1721 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
1722 + return ip_hl + 8; /* UDP header is always 8 bytes */
1723 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
1724 + return ip_hl + 8; /* ICMP header is 8 bytes */
1726 + if (net_ratelimit())
1727 + printk(KERN_ERR "layer7: tried to handle unknown "
1729 + return ip_hl + 8; /* something reasonable */
1733 +/* handles whether there's a match when we aren't appending data anymore */
1734 +static int match_no_append(struct nf_conn * conntrack,
1735 + struct nf_conn * master_conntrack,
1736 + enum ip_conntrack_info ctinfo,
1737 + enum ip_conntrack_info master_ctinfo,
1738 + const struct xt_layer7_info * info)
1740 + /* If we're in here, throw the app data away */
1741 + if(master_conntrack->layer7.app_data != NULL) {
1743 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
1744 + if(!master_conntrack->layer7.app_proto) {
1746 + friendly_print(master_conntrack->layer7.app_data);
1748 + hex_print(master_conntrack->layer7.app_data);
1749 + DPRINTK("\nl7-filter gave up after %d bytes "
1750 + "(%d packets):\n%s\n",
1751 + strlen(f), total_acct_packets(master_conntrack), f);
1753 + DPRINTK("In hex: %s\n", g);
1758 + kfree(master_conntrack->layer7.app_data);
1759 + master_conntrack->layer7.app_data = NULL; /* don't free again */
1762 + if(master_conntrack->layer7.app_proto){
1763 + /* Here child connections set their .app_proto (for /proc) */
1764 + if(!conntrack->layer7.app_proto) {
1765 + conntrack->layer7.app_proto =
1766 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
1768 + if(!conntrack->layer7.app_proto){
1769 + if (net_ratelimit())
1770 + printk(KERN_ERR "layer7: out of memory "
1771 + "in match_no_append, "
1775 + strcpy(conntrack->layer7.app_proto,
1776 + master_conntrack->layer7.app_proto);
1779 + return (!strcmp(master_conntrack->layer7.app_proto,
1783 + /* If not classified, set to "unknown" to distinguish from
1784 + connections that are still being tested. */
1785 + master_conntrack->layer7.app_proto =
1786 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
1787 + if(!master_conntrack->layer7.app_proto){
1788 + if (net_ratelimit())
1789 + printk(KERN_ERR "layer7: out of memory in "
1790 + "match_no_append, bailing.\n");
1793 + strcpy(master_conntrack->layer7.app_proto, "unknown");
1798 +/* add the new app data to the conntrack. Return number of bytes added. */
1799 +static int add_data(struct nf_conn * master_conntrack,
1800 + char * app_data, int appdatalen)
1802 + int length = 0, i;
1803 + int oldlength = master_conntrack->layer7.app_data_len;
1805 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
1806 + clear on whether the race condition exists or whether this really
1807 + fixes it. I might just be being dense... Anyway, if it's not really
1808 + a fix, all it does is waste a very small amount of time. */
1809 + if(!master_conntrack->layer7.app_data) return 0;
1811 + /* Strip nulls. Make everything lower case (our regex lib doesn't
1812 + do case insensitivity). Add it to the end of the current data. */
1813 + for(i = 0; i < maxdatalen-oldlength-1 &&
1814 + i < appdatalen; i++) {
1815 + if(app_data[i] != '\0') {
1816 + /* the kernel version of tolower mungs 'upper ascii' */
1817 + master_conntrack->layer7.app_data[length+oldlength] =
1818 + isascii(app_data[i])?
1819 + tolower(app_data[i]) : app_data[i];
1824 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
1825 + master_conntrack->layer7.app_data_len = length + oldlength;
1830 +/* taken from drivers/video/modedb.c */
1831 +static int my_atoi(const char *s)
1838 + val = 10*val+(*s-'0');
1846 +/* write out num_packets to userland. */
1847 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
1848 + int* eof, void * data)
1850 + if(num_packets > 99 && net_ratelimit())
1851 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
1853 + page[0] = num_packets/10 + '0';
1854 + page[1] = num_packets%10 + '0';
1863 +/* Read in num_packets from userland */
1864 +static int layer7_write_proc(struct file* file, const char* buffer,
1865 + unsigned long count, void *data)
1867 + char * foo = kmalloc(count, GFP_ATOMIC);
1870 + if (net_ratelimit())
1871 + printk(KERN_ERR "layer7: out of memory, bailing. "
1872 + "num_packets unchanged.\n");
1876 + if(copy_from_user(foo, buffer, count)) {
1881 + num_packets = my_atoi(foo);
1884 + /* This has an arbitrary limit to make the math easier. I'm lazy.
1885 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
1886 + if(num_packets > 99) {
1887 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
1889 + } else if(num_packets < 1) {
1890 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
1898 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
1899 +match(const struct sk_buff *skbin, struct xt_action_param *par)
1900 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1901 +match(const struct sk_buff *skbin, const struct xt_match_param *par)
1903 +match(const struct sk_buff *skbin,
1904 + const struct net_device *in,
1905 + const struct net_device *out,
1906 + const struct xt_match *match,
1907 + const void *matchinfo,
1909 + unsigned int protoff,
1913 + /* sidestep const without getting a compiler warning... */
1914 + struct sk_buff * skb = (struct sk_buff *)skbin;
1916 + const struct xt_layer7_info * info =
1917 + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1923 + enum ip_conntrack_info master_ctinfo, ctinfo;
1924 + struct nf_conn *master_conntrack, *conntrack;
1925 + unsigned char * app_data;
1926 + unsigned int pattern_result, appdatalen;
1927 + regexp * comppattern;
1929 + /* Be paranoid/incompetent - lock the entire match function. */
1930 + spin_lock_bh(&l7_lock);
1932 + if(!can_handle(skb)){
1933 + DPRINTK("layer7: This is some protocol I can't handle.\n");
1934 + spin_unlock_bh(&l7_lock);
1935 + return info->invert;
1938 + /* Treat parent & all its children together as one connection, except
1939 + for the purpose of setting conntrack->layer7.app_proto in the actual
1940 + connection. This makes /proc/net/ip_conntrack more satisfying. */
1941 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
1942 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
1943 + DPRINTK("layer7: couldn't get conntrack.\n");
1944 + spin_unlock_bh(&l7_lock);
1945 + return info->invert;
1948 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
1949 + while (master_ct(master_conntrack) != NULL)
1950 + master_conntrack = master_ct(master_conntrack);
1952 + /* if we've classified it or seen too many packets */
1953 + if(total_acct_packets(master_conntrack) > num_packets ||
1954 + master_conntrack->layer7.app_proto) {
1956 + pattern_result = match_no_append(conntrack, master_conntrack,
1957 + ctinfo, master_ctinfo, info);
1959 + /* skb->cb[0] == seen. Don't do things twice if there are
1960 + multiple l7 rules. I'm not sure that using cb for this purpose
1961 + is correct, even though it says "put your private variables
1962 + there". But it doesn't look like it is being used for anything
1963 + else in the skbs that make it here. */
1964 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
1966 + spin_unlock_bh(&l7_lock);
1967 + return (pattern_result ^ info->invert);
1970 + if(skb_is_nonlinear(skb)){
1971 + if(skb_linearize(skb) != 0){
1972 + if (net_ratelimit())
1973 + printk(KERN_ERR "layer7: failed to linearize "
1974 + "packet, bailing.\n");
1975 + spin_unlock_bh(&l7_lock);
1976 + return info->invert;
1980 + /* now that the skb is linearized, it's safe to set these. */
1981 + app_data = skb->data + app_data_offset(skb);
1982 + appdatalen = skb_tail_pointer(skb) - app_data;
1984 + /* the return value gets checked later, when we're ready to use it */
1985 + comppattern = compile_and_cache(info->pattern, info->protocol);
1987 + /* On the first packet of a connection, allocate space for app data */
1988 + if(total_acct_packets(master_conntrack) == 1 && !skb->cb[0] &&
1989 + !master_conntrack->layer7.app_data){
1990 + master_conntrack->layer7.app_data =
1991 + kmalloc(maxdatalen, GFP_ATOMIC);
1992 + if(!master_conntrack->layer7.app_data){
1993 + if (net_ratelimit())
1994 + printk(KERN_ERR "layer7: out of memory in "
1995 + "match, bailing.\n");
1996 + spin_unlock_bh(&l7_lock);
1997 + return info->invert;
2000 + master_conntrack->layer7.app_data[0] = '\0';
2003 + /* Can be here, but unallocated, if numpackets is increased near
2004 + the beginning of a connection */
2005 + if(master_conntrack->layer7.app_data == NULL){
2006 + spin_unlock_bh(&l7_lock);
2007 + return info->invert; /* unmatched */
2012 + newbytes = add_data(master_conntrack, app_data, appdatalen);
2014 + if(newbytes == 0) { /* didn't add any data */
2016 + /* Didn't match before, not going to match now */
2017 + spin_unlock_bh(&l7_lock);
2018 + return info->invert;
2022 + /* If looking for "unknown", then never match. "Unknown" means that
2023 + we've given up; we're still trying with these packets. */
2024 + if(!strcmp(info->protocol, "unknown")) {
2025 + pattern_result = 0;
2026 + /* If looking for "unset", then always match. "Unset" means that we
2027 + haven't yet classified the connection. */
2028 + } else if(!strcmp(info->protocol, "unset")) {
2029 + pattern_result = 2;
2030 + DPRINTK("layer7: matched unset: not yet classified "
2031 + "(%d/%d packets)\n",
2032 + total_acct_packets(master_conntrack), num_packets);
2033 + /* If the regexp failed to compile, don't bother running it */
2034 + } else if(comppattern &&
2035 + regexec(comppattern, master_conntrack->layer7.app_data)){
2036 + DPRINTK("layer7: matched %s\n", info->protocol);
2037 + pattern_result = 1;
2038 + } else pattern_result = 0;
2040 + if(pattern_result == 1) {
2041 + master_conntrack->layer7.app_proto =
2042 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
2043 + if(!master_conntrack->layer7.app_proto){
2044 + if (net_ratelimit())
2045 + printk(KERN_ERR "layer7: out of memory in "
2046 + "match, bailing.\n");
2047 + spin_unlock_bh(&l7_lock);
2048 + return (pattern_result ^ info->invert);
2050 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
2051 + } else if(pattern_result > 1) { /* cleanup from "unset" */
2052 + pattern_result = 1;
2055 + /* mark the packet seen */
2058 + spin_unlock_bh(&l7_lock);
2059 + return (pattern_result ^ info->invert);
2062 +// load nf_conntrack_ipv4
2063 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
2068 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
2069 +check(const struct xt_mtchk_param *par)
2071 + if (nf_ct_l3proto_try_module_get(par->match->family) < 0) {
2072 + printk(KERN_WARNING "can't load conntrack support for "
2073 + "proto=%d\n", par->match->family);
2075 +check(const char *tablename, const void *inf,
2076 + const struct xt_match *match, void *matchinfo,
2077 + unsigned int hook_mask)
2079 + if (nf_ct_l3proto_try_module_get(match->family) < 0) {
2080 + printk(KERN_WARNING "can't load conntrack support for "
2081 + "proto=%d\n", match->family);
2083 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
2095 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
2096 + static void destroy(const struct xt_mtdtor_param *par)
2098 + nf_ct_l3proto_module_put(par->match->family);
2101 + static void destroy(const struct xt_match *match, void *matchinfo)
2103 + nf_ct_l3proto_module_put(match->family);
2107 +static struct xt_match xt_layer7_match[] __read_mostly = {
2110 + .family = AF_INET,
2111 + .checkentry = check,
2113 + .destroy = destroy,
2114 + .matchsize = sizeof(struct xt_layer7_info),
2119 +static void layer7_cleanup_proc(void)
2121 +// remove_proc_entry("layer7_numpackets", init_net.proc_net);
2124 +/* register the proc file */
2125 +static void layer7_init_proc(void)
2127 + struct proc_dir_entry* entry;
2128 +// entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
2129 +// entry->read_proc = layer7_read_proc;
2130 +// entry->write_proc = layer7_write_proc;
2133 +static int __init xt_layer7_init(void)
2137 + if (init_net.ct.sysctl_acct == 0) {
2138 + printk(KERN_WARNING "layer7: enabling nf_conntrack_acct\n");
2139 + init_net.ct.sysctl_acct = 1;
2142 + layer7_init_proc();
2143 + if(maxdatalen < 1) {
2144 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
2148 + /* This is not a hard limit. It's just here to prevent people from
2149 + bringing their slow machines to a grinding halt. */
2150 + else if(maxdatalen > 65536) {
2151 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
2153 + maxdatalen = 65536;
2155 + return xt_register_matches(xt_layer7_match,
2156 + ARRAY_SIZE(xt_layer7_match));
2159 +static void __exit xt_layer7_fini(void)
2161 + layer7_cleanup_proc();
2162 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
2165 +module_init(xt_layer7_init);
2166 +module_exit(xt_layer7_fini);