1 diff -urNp -x '*.orig' linux-5.11/include/linux/netfilter/xt_layer7.h linux-5.11/include/linux/netfilter/xt_layer7.h
2 --- linux-5.11/include/linux/netfilter/xt_layer7.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-5.11/include/linux/netfilter/xt_layer7.h 2021-02-24 13:21:48.338440845 +0100
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 -urNp -x '*.orig' linux-5.11/include/net/netfilter/nf_conntrack.h linux-5.11/include/net/netfilter/nf_conntrack.h
19 --- linux-5.11/include/net/netfilter/nf_conntrack.h 2021-02-14 23:32:24.000000000 +0100
20 +++ linux-5.11/include/net/netfilter/nf_conntrack.h 2021-02-24 13:21:48.338440845 +0100
21 @@ -103,6 +103,22 @@ struct nf_conn {
23 struct nf_ct_ext *ext;
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 -urNp -x '*.orig' linux-5.11/net/netfilter/Kconfig linux-5.11/net/netfilter/Kconfig
45 --- linux-5.11/net/netfilter/Kconfig 2021-02-14 23:32:24.000000000 +0100
46 +++ linux-5.11/net/netfilter/Kconfig 2021-02-24 13:21:48.335107407 +0100
47 @@ -1562,6 +1562,27 @@ config NETFILTER_XT_MATCH_STATE
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 EXPERIMENTAL && (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.
65 +config NETFILTER_XT_MATCH_LAYER7_DEBUG
66 + bool 'Layer 7 debugging output'
67 + depends on NETFILTER_XT_MATCH_LAYER7
69 + Say Y to get lots of debugging output.
72 config NETFILTER_XT_MATCH_STATISTIC
73 tristate '"statistic" match support'
74 depends on NETFILTER_ADVANCED
75 diff -urNp -x '*.orig' linux-5.11/net/netfilter/Makefile linux-5.11/net/netfilter/Makefile
76 --- linux-5.11/net/netfilter/Makefile 2021-02-14 23:32:24.000000000 +0100
77 +++ linux-5.11/net/netfilter/Makefile 2021-02-24 13:21:48.335107407 +0100
78 @@ -204,6 +204,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_RECENT)
79 obj-$(CONFIG_NETFILTER_XT_MATCH_SCTP) += xt_sctp.o
80 obj-$(CONFIG_NETFILTER_XT_MATCH_SOCKET) += xt_socket.o
81 obj-$(CONFIG_NETFILTER_XT_MATCH_STATE) += xt_state.o
82 +obj-$(CONFIG_NETFILTER_XT_MATCH_LAYER7) += xt_layer7.o
83 obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTIC) += xt_statistic.o
84 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
85 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
86 diff -urNp -x '*.orig' linux-5.11/net/netfilter/nf_conntrack_standalone.c linux-5.11/net/netfilter/nf_conntrack_standalone.c
87 --- linux-5.11/net/netfilter/nf_conntrack_standalone.c 2021-02-14 23:32:24.000000000 +0100
88 +++ linux-5.11/net/netfilter/nf_conntrack_standalone.c 2021-02-24 13:21:48.338440845 +0100
89 @@ -366,6 +366,12 @@ static int ct_seq_show(struct seq_file *
90 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
91 ct_show_delta_time(s, ct);
93 +#if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
94 + if(ct->layer7.app_proto &&
95 + seq_printf(s, "l7proto=%s ", ct->layer7.app_proto))
99 seq_printf(s, "use=%u\n", refcount_read(&ct->ct_general.use));
101 if (seq_has_overflowed(s))
102 diff -urNp -x '*.orig' linux-5.11/net/netfilter/regexp/regexp.c linux-5.11/net/netfilter/regexp/regexp.c
103 --- linux-5.11/net/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
104 +++ linux-5.11/net/netfilter/regexp/regexp.c 2021-02-24 13:21:48.335107407 +0100
107 + * regcomp and regexec -- regsub and regerror are elsewhere
108 + * @(#)regexp.c 1.3 of 18 April 87
110 + * Copyright (c) 1986 by University of Toronto.
111 + * Written by Henry Spencer. Not derived from licensed software.
113 + * Permission is granted to anyone to use this software for any
114 + * purpose on any computer system, and to redistribute it freely,
115 + * subject to the following restrictions:
117 + * 1. The author is not responsible for the consequences of use of
118 + * this software, no matter how awful, even if they arise
119 + * from defects in it.
121 + * 2. The origin of this software must not be misrepresented, either
122 + * by explicit claim or by omission.
124 + * 3. Altered versions must be plainly marked as such, and must not
125 + * be misrepresented as being the original software.
127 + * Beware that some of this code is subtly aware of the way operator
128 + * precedence is structured in regular expressions. Serious changes in
129 + * regular-expression syntax might require a total rethink.
131 + * This code was modified by Ethan Sommer to work within the kernel
132 + * (it now uses kmalloc etc..)
134 + * Modified slightly by Matthew Strait to use more modern C.
138 +#include "regmagic.h"
140 +/* added by ethan and matt. Lets it work in both kernel and user space.
141 +(So iptables can use it, for instance.) Yea, it goes both ways... */
143 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
145 + #define printk(format,args...) printf(format,##args)
148 +void regerror(char * s)
150 + printk("<3>Regexp: %s\n", s);
155 + * The "internal use only" fields in regexp.h are present to pass info from
156 + * compile to execute that permits the execute phase to run lots faster on
157 + * simple cases. They are:
159 + * regstart char that must begin a match; '\0' if none obvious
160 + * reganch is the match anchored (at beginning-of-line only)?
161 + * regmust string (pointer into program) that match must include, or NULL
162 + * regmlen length of regmust string
164 + * Regstart and reganch permit very fast decisions on suitable starting points
165 + * for a match, cutting down the work a lot. Regmust permits fast rejection
166 + * of lines that cannot possibly match. The regmust tests are costly enough
167 + * that regcomp() supplies a regmust only if the r.e. contains something
168 + * potentially expensive (at present, the only such thing detected is * or +
169 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
170 + * supplied because the test in regexec() needs it and regcomp() is computing
175 + * Structure for regexp "program". This is essentially a linear encoding
176 + * of a nondeterministic finite-state machine (aka syntax charts or
177 + * "railroad normal form" in parsing technology). Each node is an opcode
178 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
179 + * all nodes except BRANCH implement concatenation; a "next" pointer with
180 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
181 + * have one of the subtle syntax dependencies: an individual BRANCH (as
182 + * opposed to a collection of them) is never concatenated with anything
183 + * because of operator precedence.) The operand of some types of node is
184 + * a literal string; for others, it is a node leading into a sub-FSM. In
185 + * particular, the operand of a BRANCH node is the first node of the branch.
186 + * (NB this is *not* a tree structure: the tail of the branch connects
187 + * to the thing following the set of BRANCHes.) The opcodes are:
190 +/* definition number opnd? meaning */
191 +#define END 0 /* no End of program. */
192 +#define BOL 1 /* no Match "" at beginning of line. */
193 +#define EOL 2 /* no Match "" at end of line. */
194 +#define ANY 3 /* no Match any one character. */
195 +#define ANYOF 4 /* str Match any character in this string. */
196 +#define ANYBUT 5 /* str Match any character not in this string. */
197 +#define BRANCH 6 /* node Match this alternative, or the next... */
198 +#define BACK 7 /* no Match "", "next" ptr points backward. */
199 +#define EXACTLY 8 /* str Match this string. */
200 +#define NOTHING 9 /* no Match empty string. */
201 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
202 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
203 +#define OPEN 20 /* no Mark this point in input as start of #n. */
204 + /* OPEN+1 is number 1, etc. */
205 +#define CLOSE 30 /* no Analogous to OPEN. */
210 + * BRANCH The set of branches constituting a single choice are hooked
211 + * together with their "next" pointers, since precedence prevents
212 + * anything being concatenated to any individual branch. The
213 + * "next" pointer of the last BRANCH in a choice points to the
214 + * thing following the whole choice. This is also where the
215 + * final "next" pointer of each individual branch points; each
216 + * branch starts with the operand node of a BRANCH node.
218 + * BACK Normal "next" pointers all implicitly point forward; BACK
219 + * exists to make loop structures possible.
221 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
222 + * BRANCH structures using BACK. Simple cases (one character
223 + * per match) are implemented with STAR and PLUS for speed
224 + * and to minimize recursive plunges.
226 + * OPEN,CLOSE ...are numbered at compile time.
230 + * A node is one char of opcode followed by two chars of "next" pointer.
231 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
232 + * value is a positive offset from the opcode of the node containing it.
233 + * An operand, if any, simply follows the node. (Note that much of the
234 + * code generation knows about this implicit relationship.)
236 + * Using two bytes for the "next" pointer is vast overkill for most things,
237 + * but allows patterns to get big without disasters.
239 +#define OP(p) (*(p))
240 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
241 +#define OPERAND(p) ((p) + 3)
244 + * See regmagic.h for one further detail of program structure.
249 + * Utility definitions.
252 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
254 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
257 +#define FAIL(m) { regerror(m); return(NULL); }
258 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
259 +#define META "^$.[()|?+*\\"
262 + * Flags to be passed up and down.
264 +#define HASWIDTH 01 /* Known never to match null string. */
265 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
266 +#define SPSTART 04 /* Starts with * or +. */
267 +#define WORST 0 /* Worst case. */
270 + * Global work variables for regcomp().
272 +struct match_globals {
273 +char *reginput; /* String-input pointer. */
274 +char *regbol; /* Beginning of input, for ^ check. */
275 +char **regstartp; /* Pointer to startp array. */
276 +char **regendp; /* Ditto for endp. */
277 +char *regparse; /* Input-scan pointer. */
278 +int regnpar; /* () count. */
280 +char *regcode; /* Code-emit pointer; ®dummy = don't. */
281 +long regsize; /* Code size. */
285 + * Forward declarations for regcomp()'s friends.
288 +#define STATIC static
290 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
291 +STATIC char *regbranch(struct match_globals *g, int *flagp);
292 +STATIC char *regpiece(struct match_globals *g, int *flagp);
293 +STATIC char *regatom(struct match_globals *g, int *flagp);
294 +STATIC char *regnode(struct match_globals *g, char op);
295 +STATIC char *regnext(struct match_globals *g, char *p);
296 +STATIC void regc(struct match_globals *g, char b);
297 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
298 +STATIC void regtail(struct match_globals *g, char *p, char *val);
299 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
302 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
309 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
310 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
311 + if (*scan1 == *scan2++)
319 + - regcomp - compile a regular expression into internal code
321 + * We can't allocate space until we know how big the compiled form will be,
322 + * but we can't compile it (and thus know how big it is) until we've got a
323 + * place to put the code. So we cheat: we compile it twice, once with code
324 + * generation turned off and size counting turned on, and once "for real".
325 + * This also means that we don't allocate space until we are sure that the
326 + * thing really will compile successfully, and we never have to move the
327 + * code and thus invalidate pointers into it. (Note that it has to be in
328 + * one piece because free() must be able to free it all.)
330 + * Beware that the optimization-preparation code in here knows about some
331 + * of the structure of the compiled regexp.
334 +regcomp(char *exp,int *patternsize)
336 + register regexp *r;
337 + register char *scan;
338 + register char *longest;
341 + struct match_globals g;
343 + /* commented out by ethan
344 + extern char *malloc();
348 + FAIL("NULL argument");
350 + /* First pass: determine size, legality. */
354 + g.regcode = &g.regdummy;
356 + if (reg(&g, 0, &flags) == NULL)
359 + /* Small enough for pointer-storage convention? */
360 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
361 + FAIL("regexp too big");
363 + /* Allocate space. */
364 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
365 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
367 + FAIL("out of space");
369 + /* Second pass: emit code. */
372 + g.regcode = r->program;
374 + if (reg(&g, 0, &flags) == NULL)
377 + /* Dig out information for optimizations. */
378 + r->regstart = '\0'; /* Worst-case defaults. */
382 + scan = r->program+1; /* First BRANCH. */
383 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
384 + scan = OPERAND(scan);
386 + /* Starting-point info. */
387 + if (OP(scan) == EXACTLY)
388 + r->regstart = *OPERAND(scan);
389 + else if (OP(scan) == BOL)
393 + * If there's something expensive in the r.e., find the
394 + * longest literal string that must appear and make it the
395 + * regmust. Resolve ties in favor of later strings, since
396 + * the regstart check works with the beginning of the r.e.
397 + * and avoiding duplication strengthens checking. Not a
398 + * strong reason, but sufficient in the absence of others.
400 + if (flags&SPSTART) {
403 + for (; scan != NULL; scan = regnext(&g, scan))
404 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
405 + longest = OPERAND(scan);
406 + len = strlen(OPERAND(scan));
408 + r->regmust = longest;
417 + - reg - regular expression, i.e. main body or parenthesized thing
419 + * Caller must absorb opening parenthesis.
421 + * Combining parenthesis handling with the base level of regular expression
422 + * is a trifle forced, but the need to tie the tails of the branches to what
423 + * follows makes it hard to avoid.
426 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
428 + register char *ret;
430 + register char *ender;
431 + register int parno = 0; /* 0 makes gcc happy */
434 + *flagp = HASWIDTH; /* Tentatively. */
436 + /* Make an OPEN node, if parenthesized. */
438 + if (g->regnpar >= NSUBEXP)
439 + FAIL("too many ()");
440 + parno = g->regnpar;
442 + ret = regnode(g, OPEN+parno);
446 + /* Pick up the branches, linking them together. */
447 + br = regbranch(g, &flags);
451 + regtail(g, ret, br); /* OPEN -> first. */
454 + if (!(flags&HASWIDTH))
455 + *flagp &= ~HASWIDTH;
456 + *flagp |= flags&SPSTART;
457 + while (*g->regparse == '|') {
459 + br = regbranch(g, &flags);
462 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
463 + if (!(flags&HASWIDTH))
464 + *flagp &= ~HASWIDTH;
465 + *flagp |= flags&SPSTART;
468 + /* Make a closing node, and hook it on the end. */
469 + ender = regnode(g, (paren) ? CLOSE+parno : END);
470 + regtail(g, ret, ender);
472 + /* Hook the tails of the branches to the closing node. */
473 + for (br = ret; br != NULL; br = regnext(g, br))
474 + regoptail(g, br, ender);
476 + /* Check for proper termination. */
477 + if (paren && *g->regparse++ != ')') {
478 + FAIL("unmatched ()");
479 + } else if (!paren && *g->regparse != '\0') {
480 + if (*g->regparse == ')') {
481 + FAIL("unmatched ()");
483 + FAIL("junk on end"); /* "Can't happen". */
491 + - regbranch - one alternative of an | operator
493 + * Implements the concatenation operator.
496 +regbranch(struct match_globals *g, int *flagp)
498 + register char *ret;
499 + register char *chain;
500 + register char *latest;
503 + *flagp = WORST; /* Tentatively. */
505 + ret = regnode(g, BRANCH);
507 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
508 + latest = regpiece(g, &flags);
509 + if (latest == NULL)
511 + *flagp |= flags&HASWIDTH;
512 + if (chain == NULL) /* First piece. */
513 + *flagp |= flags&SPSTART;
515 + regtail(g, chain, latest);
518 + if (chain == NULL) /* Loop ran zero times. */
519 + (void) regnode(g, NOTHING);
525 + - regpiece - something followed by possible [*+?]
527 + * Note that the branching code sequences used for ? and the general cases
528 + * of * and + are somewhat optimized: they use the same NOTHING node as
529 + * both the endmarker for their branch list and the body of the last branch.
530 + * It might seem that this node could be dispensed with entirely, but the
531 + * endmarker role is not redundant.
534 +regpiece(struct match_globals *g, int *flagp)
536 + register char *ret;
538 + register char *next;
541 + ret = regatom(g, &flags);
551 + if (!(flags&HASWIDTH) && op != '?')
552 + FAIL("*+ operand could be empty");
553 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
555 + if (op == '*' && (flags&SIMPLE))
556 + reginsert(g, STAR, ret);
557 + else if (op == '*') {
558 + /* Emit x* as (x&|), where & means "self". */
559 + reginsert(g, BRANCH, ret); /* Either x */
560 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
561 + regoptail(g, ret, ret); /* back */
562 + regtail(g, ret, regnode(g, BRANCH)); /* or */
563 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
564 + } else if (op == '+' && (flags&SIMPLE))
565 + reginsert(g, PLUS, ret);
566 + else if (op == '+') {
567 + /* Emit x+ as x(&|), where & means "self". */
568 + next = regnode(g, BRANCH); /* Either */
569 + regtail(g, ret, next);
570 + regtail(g, regnode(g, BACK), ret); /* loop back */
571 + regtail(g, next, regnode(g, BRANCH)); /* or */
572 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
573 + } else if (op == '?') {
574 + /* Emit x? as (x|) */
575 + reginsert(g, BRANCH, ret); /* Either x */
576 + regtail(g, ret, regnode(g, BRANCH)); /* or */
577 + next = regnode(g, NOTHING); /* null. */
578 + regtail(g, ret, next);
579 + regoptail(g, ret, next);
582 + if (ISMULT(*g->regparse))
583 + FAIL("nested *?+");
589 + - regatom - the lowest level
591 + * Optimization: gobbles an entire sequence of ordinary characters so that
592 + * it can turn them into a single node, which is smaller to store and
593 + * faster to run. Backslashed characters are exceptions, each becoming a
594 + * separate node; the code is simpler that way and it's not worth fixing.
597 +regatom(struct match_globals *g, int *flagp)
599 + register char *ret;
602 + *flagp = WORST; /* Tentatively. */
604 + switch (*g->regparse++) {
606 + ret = regnode(g, BOL);
609 + ret = regnode(g, EOL);
612 + ret = regnode(g, ANY);
613 + *flagp |= HASWIDTH|SIMPLE;
616 + register int class;
617 + register int classend;
619 + if (*g->regparse == '^') { /* Complement of range. */
620 + ret = regnode(g, ANYBUT);
623 + ret = regnode(g, ANYOF);
624 + if (*g->regparse == ']' || *g->regparse == '-')
625 + regc(g, *g->regparse++);
626 + while (*g->regparse != '\0' && *g->regparse != ']') {
627 + if (*g->regparse == '-') {
629 + if (*g->regparse == ']' || *g->regparse == '\0')
632 + class = UCHARAT(g->regparse-2)+1;
633 + classend = UCHARAT(g->regparse);
634 + if (class > classend+1)
635 + FAIL("invalid [] range");
636 + for (; class <= classend; class++)
641 + regc(g, *g->regparse++);
644 + if (*g->regparse != ']')
645 + FAIL("unmatched []");
647 + *flagp |= HASWIDTH|SIMPLE;
651 + ret = reg(g, 1, &flags);
654 + *flagp |= flags&(HASWIDTH|SPSTART);
659 + FAIL("internal urp"); /* Supposed to be caught earlier. */
664 + FAIL("?+* follows nothing");
667 + if (*g->regparse == '\0')
668 + FAIL("trailing \\");
669 + ret = regnode(g, EXACTLY);
670 + regc(g, *g->regparse++);
672 + *flagp |= HASWIDTH|SIMPLE;
676 + register char ender;
679 + len = my_strcspn((const char *)g->regparse, (const char *)META);
681 + FAIL("internal disaster");
682 + ender = *(g->regparse+len);
683 + if (len > 1 && ISMULT(ender))
684 + len--; /* Back off clear of ?+* operand. */
685 + *flagp |= HASWIDTH;
688 + ret = regnode(g, EXACTLY);
690 + regc(g, *g->regparse++);
702 + - regnode - emit a node
704 +static char * /* Location. */
705 +regnode(struct match_globals *g, char op)
707 + register char *ret;
708 + register char *ptr;
711 + if (ret == &g->regdummy) {
718 + *ptr++ = '\0'; /* Null "next" pointer. */
726 + - regc - emit (if appropriate) a byte of code
729 +regc(struct match_globals *g, char b)
731 + if (g->regcode != &g->regdummy)
738 + - reginsert - insert an operator in front of already-emitted operand
740 + * Means relocating the operand.
743 +reginsert(struct match_globals *g, char op, char* opnd)
745 + register char *src;
746 + register char *dst;
747 + register char *place;
749 + if (g->regcode == &g->regdummy) {
760 + place = opnd; /* Op node, where operand used to be. */
767 + - regtail - set the next-pointer at the end of a node chain
770 +regtail(struct match_globals *g, char *p, char *val)
772 + register char *scan;
773 + register char *temp;
774 + register int offset;
776 + if (p == &g->regdummy)
779 + /* Find last node. */
782 + temp = regnext(g, scan);
788 + if (OP(scan) == BACK)
789 + offset = scan - val;
791 + offset = val - scan;
792 + *(scan+1) = (offset>>8)&0377;
793 + *(scan+2) = offset&0377;
797 + - regoptail - regtail on operand of first argument; nop if operandless
800 +regoptail(struct match_globals *g, char *p, char *val)
802 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
803 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
805 + regtail(g, OPERAND(p), val);
809 + * regexec and friends
816 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
817 +STATIC int regmatch(struct match_globals *g, char *prog);
818 +STATIC int regrepeat(struct match_globals *g, char *p);
823 +STATIC char *regprop(char *op);
827 + - regexec - match a regexp against a string
830 +regexec(regexp *prog, char *string)
833 + struct match_globals g;
835 + /* Be paranoid... */
836 + if (prog == NULL || string == NULL) {
837 + printk("<3>Regexp: NULL parameter\n");
841 + /* Check validity of program. */
842 + if (UCHARAT(prog->program) != MAGIC) {
843 + printk("<3>Regexp: corrupted program\n");
847 + /* If there is a "must appear" string, look for it. */
848 + if (prog->regmust != NULL) {
850 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
851 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
852 + break; /* Found it. */
855 + if (s == NULL) /* Not present. */
859 + /* Mark beginning of line for ^ . */
862 + /* Simplest case: anchored match need be tried only once. */
864 + return(regtry(&g, prog, string));
866 + /* Messy cases: unanchored match. */
868 + if (prog->regstart != '\0')
869 + /* We know what char it must start with. */
870 + while ((s = strchr(s, prog->regstart)) != NULL) {
871 + if (regtry(&g, prog, s))
876 + /* We don't -- general case. */
878 + if (regtry(&g, prog, s))
880 + } while (*s++ != '\0');
887 + - regtry - try match at specific point
889 +static int /* 0 failure, 1 success */
890 +regtry(struct match_globals *g, regexp *prog, char *string)
893 + register char **sp;
894 + register char **ep;
896 + g->reginput = string;
897 + g->regstartp = prog->startp;
898 + g->regendp = prog->endp;
902 + for (i = NSUBEXP; i > 0; i--) {
906 + if (regmatch(g, prog->program + 1)) {
907 + prog->startp[0] = string;
908 + prog->endp[0] = g->reginput;
915 + - regmatch - main matching routine
917 + * Conceptually the strategy is simple: check to see whether the current
918 + * node matches, call self recursively to see whether the rest matches,
919 + * and then act accordingly. In practice we make some effort to avoid
920 + * recursion, in particular by going through "ordinary" nodes (that don't
921 + * need to know whether the rest of the match failed) by a loop instead of
924 +static int /* 0 failure, 1 success */
925 +regmatch(struct match_globals *g, char *prog)
927 + register char *scan = prog; /* Current node. */
928 + char *next; /* Next node. */
931 + if (scan != NULL && regnarrate)
932 + fprintf(stderr, "%s(\n", regprop(scan));
934 + while (scan != NULL) {
937 + fprintf(stderr, "%s...\n", regprop(scan));
939 + next = regnext(g, scan);
941 + switch (OP(scan)) {
943 + if (g->reginput != g->regbol)
947 + if (*g->reginput != '\0')
951 + if (*g->reginput == '\0')
957 + register char *opnd;
959 + opnd = OPERAND(scan);
960 + /* Inline the first character, for speed. */
961 + if (*opnd != *g->reginput)
963 + len = strlen(opnd);
964 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
966 + g->reginput += len;
970 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
975 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
992 + register char *save;
994 + no = OP(scan) - OPEN;
995 + save = g->reginput;
997 + if (regmatch(g, next)) {
999 + * Don't set startp if some later
1000 + * invocation of the same parentheses
1003 + if (g->regstartp[no] == NULL)
1004 + g->regstartp[no] = save;
1021 + register char *save;
1023 + no = OP(scan) - CLOSE;
1024 + save = g->reginput;
1026 + if (regmatch(g, next)) {
1028 + * Don't set endp if some later
1029 + * invocation of the same parentheses
1032 + if (g->regendp[no] == NULL)
1033 + g->regendp[no] = save;
1040 + register char *save;
1042 + if (OP(next) != BRANCH) /* No choice. */
1043 + next = OPERAND(scan); /* Avoid recursion. */
1046 + save = g->reginput;
1047 + if (regmatch(g, OPERAND(scan)))
1049 + g->reginput = save;
1050 + scan = regnext(g, scan);
1051 + } while (scan != NULL && OP(scan) == BRANCH);
1059 + register char nextch;
1061 + register char *save;
1065 + * Lookahead to avoid useless match attempts
1066 + * when we know what character comes next.
1069 + if (OP(next) == EXACTLY)
1070 + nextch = *OPERAND(next);
1071 + min = (OP(scan) == STAR) ? 0 : 1;
1072 + save = g->reginput;
1073 + no = regrepeat(g, OPERAND(scan));
1074 + while (no >= min) {
1075 + /* If it could work, try it. */
1076 + if (nextch == '\0' || *g->reginput == nextch)
1077 + if (regmatch(g, next))
1079 + /* Couldn't or didn't -- back up. */
1081 + g->reginput = save + no;
1087 + return(1); /* Success! */
1090 + printk("<3>Regexp: memory corruption\n");
1099 + * We get here only if there's trouble -- normally "case END" is
1100 + * the terminating point.
1102 + printk("<3>Regexp: corrupted pointers\n");
1107 + - regrepeat - repeatedly match something simple, report how many
1110 +regrepeat(struct match_globals *g, char *p)
1112 + register int count = 0;
1113 + register char *scan;
1114 + register char *opnd;
1116 + scan = g->reginput;
1117 + opnd = OPERAND(p);
1120 + count = strlen(scan);
1124 + while (*opnd == *scan) {
1130 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1136 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1141 + default: /* Oh dear. Called inappropriately. */
1142 + printk("<3>Regexp: internal foulup\n");
1143 + count = 0; /* Best compromise. */
1146 + g->reginput = scan;
1152 + - regnext - dig the "next" pointer out of a node
1155 +regnext(struct match_globals *g, char *p)
1157 + register int offset;
1159 + if (p == &g->regdummy)
1166 + if (OP(p) == BACK)
1174 +STATIC char *regprop();
1177 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1183 + register char op = EXACTLY; /* Arbitrary non-END op. */
1184 + register char *next;
1185 + /* extern char *strchr(); */
1188 + s = r->program + 1;
1189 + while (op != END) { /* While that wasn't END last time... */
1191 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1192 + next = regnext(s);
1193 + if (next == NULL) /* Next ptr. */
1196 + printf("(%d)", (s-r->program)+(next-s));
1198 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1199 + /* Literal string, where present. */
1200 + while (*s != '\0') {
1209 + /* Header fields of interest. */
1210 + if (r->regstart != '\0')
1211 + printf("start `%c' ", r->regstart);
1213 + printf("anchored ");
1214 + if (r->regmust != NULL)
1215 + printf("must have \"%s\"", r->regmust);
1220 + - regprop - printable representation of opcode
1227 + static char buf[BUFLEN];
1271 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1283 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1293 + printk("<3>Regexp: corrupted opcode\n");
1297 + strncat(buf, p, BUFLEN-strlen(buf));
1303 diff -urNp -x '*.orig' linux-5.11/net/netfilter/regexp/regexp.h linux-5.11/net/netfilter/regexp/regexp.h
1304 --- linux-5.11/net/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1305 +++ linux-5.11/net/netfilter/regexp/regexp.h 2021-02-24 13:21:48.335107407 +0100
1308 + * Definitions etc. for regexp(3) routines.
1310 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1311 + * not the System V one.
1319 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1320 +which contains a version of this library, says:
1323 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1324 + * will not work properly.
1327 +However, it looks rather like this library is limited to 10. If you think
1328 +otherwise, let us know.
1332 +typedef struct regexp {
1333 + char *startp[NSUBEXP];
1334 + char *endp[NSUBEXP];
1335 + char regstart; /* Internal use only. */
1336 + char reganch; /* Internal use only. */
1337 + char *regmust; /* Internal use only. */
1338 + int regmlen; /* Internal use only. */
1339 + char program[1]; /* Unwarranted chumminess with compiler. */
1342 +regexp * regcomp(char *exp, int *patternsize);
1343 +int regexec(regexp *prog, char *string);
1344 +void regsub(regexp *prog, char *source, char *dest);
1345 +void regerror(char *s);
1348 diff -urNp -x '*.orig' linux-5.11/net/netfilter/regexp/regmagic.h linux-5.11/net/netfilter/regexp/regmagic.h
1349 --- linux-5.11/net/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1350 +++ linux-5.11/net/netfilter/regexp/regmagic.h 2021-02-24 13:21:48.335107407 +0100
1353 + * The first byte of the regexp internal "program" is actually this magic
1354 + * number; the start node begins in the second byte.
1357 diff -urNp -x '*.orig' linux-5.11/net/netfilter/regexp/regsub.c linux-5.11/net/netfilter/regexp/regsub.c
1358 --- linux-5.11/net/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1359 +++ linux-5.11/net/netfilter/regexp/regsub.c 2021-02-24 13:21:48.335107407 +0100
1363 + * @(#)regsub.c 1.3 of 2 April 86
1365 + * Copyright (c) 1986 by University of Toronto.
1366 + * Written by Henry Spencer. Not derived from licensed software.
1368 + * Permission is granted to anyone to use this software for any
1369 + * purpose on any computer system, and to redistribute it freely,
1370 + * subject to the following restrictions:
1372 + * 1. The author is not responsible for the consequences of use of
1373 + * this software, no matter how awful, even if they arise
1374 + * from defects in it.
1376 + * 2. The origin of this software must not be misrepresented, either
1377 + * by explicit claim or by omission.
1379 + * 3. Altered versions must be plainly marked as such, and must not
1380 + * be misrepresented as being the original software.
1383 + * This code was modified by Ethan Sommer to work within the kernel
1384 + * (it now uses kmalloc etc..)
1387 +#include "regexp.h"
1388 +#include "regmagic.h"
1389 +#include <linux/string.h>
1393 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1395 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1399 +//void regerror(char * s)
1401 +// printk("regexp(3): %s", s);
1402 +// /* NOTREACHED */
1407 + - regsub - perform substitutions after a regexp match
1410 +regsub(regexp * prog, char * source, char * dest)
1412 + register char *src;
1413 + register char *dst;
1418 + /* Not necessary and gcc doesn't like it -MLS */
1419 + /*extern char *strncpy();*/
1421 + if (prog == NULL || source == NULL || dest == NULL) {
1422 + regerror("NULL parm to regsub");
1425 + if (UCHARAT(prog->program) != MAGIC) {
1426 + regerror("damaged regexp fed to regsub");
1432 + while ((c = *src++) != '\0') {
1435 + else if (c == '\\' && '0' <= *src && *src <= '9')
1436 + no = *src++ - '0';
1440 + if (no < 0) { /* Ordinary character. */
1441 + if (c == '\\' && (*src == '\\' || *src == '&'))
1444 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
1445 + len = prog->endp[no] - prog->startp[no];
1446 + (void) strncpy(dst, prog->startp[no], len);
1448 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
1449 + regerror("damaged match string");
1456 diff -urNp -x '*.orig' linux-5.11/net/netfilter/xt_layer7.c linux-5.11/net/netfilter/xt_layer7.c
1457 --- linux-5.11/net/netfilter/xt_layer7.c 1970-01-01 01:00:00.000000000 +0100
1458 +++ linux-5.11/net/netfilter/xt_layer7.c 2021-02-24 13:21:48.335107407 +0100
1461 + Kernel module to match application layer (OSI layer 7) data in connections.
1463 + http://l7-filter.sf.net
1465 + (C) 2003-2009 Matthew Strait and Ethan Sommer.
1467 + This program is free software; you can redistribute it and/or
1468 + modify it under the terms of the GNU General Public License
1469 + as published by the Free Software Foundation; either version
1470 + 2 of the License, or (at your option) any later version.
1471 + http://www.gnu.org/licenses/gpl.txt
1473 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>,
1474 + xt_helper.c (C) 2002 Harald Welte and cls_layer7.c (C) 2003 Matthew Strait,
1475 + Ethan Sommer, Justin Levandoski.
1478 +#include <linux/spinlock.h>
1479 +#include <linux/version.h>
1480 +#include <net/ip.h>
1481 +#include <net/tcp.h>
1482 +#include <linux/module.h>
1483 +#include <linux/skbuff.h>
1484 +#include <linux/netfilter.h>
1485 +#include <net/netfilter/nf_conntrack.h>
1486 +#include <net/netfilter/nf_conntrack_core.h>
1487 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
1488 +#include <net/netfilter/nf_conntrack_extend.h>
1489 +#include <net/netfilter/nf_conntrack_acct.h>
1491 +#include <linux/netfilter/x_tables.h>
1492 +#include <linux/netfilter/xt_layer7.h>
1493 +#include <linux/ctype.h>
1494 +#include <linux/proc_fs.h>
1496 +#include "regexp/regexp.c"
1498 +MODULE_LICENSE("GPL");
1499 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
1500 +MODULE_DESCRIPTION("iptables application layer match module");
1501 +MODULE_ALIAS("ipt_layer7");
1502 +MODULE_VERSION("2.21");
1504 +static int maxdatalen = 2048; // this is the default
1505 +module_param(maxdatalen, int, 0444);
1506 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
1507 +#ifdef CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG
1508 + #define DPRINTK(format,args...) printk(format,##args)
1510 + #define DPRINTK(format,args...)
1513 +/* Number of packets whose data we look at.
1514 +This can be modified through /proc/net/layer7_numpackets */
1515 +static int num_packets = 10;
1517 +static struct pattern_cache {
1518 + char * regex_string;
1520 + struct pattern_cache * next;
1521 +} * first_pattern_cache = NULL;
1523 +DEFINE_SPINLOCK(l7_lock);
1525 +static int total_acct_packets(struct nf_conn *ct)
1527 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 26)
1528 + BUG_ON(ct == NULL);
1529 + return (ct->counters[IP_CT_DIR_ORIGINAL].packets + ct->counters[IP_CT_DIR_REPLY].packets);
1531 + struct nf_conn_counter *acct;
1533 + BUG_ON(ct == NULL);
1534 + acct = nf_conn_acct_find(ct);
1537 + return (atomic64_read(&acct[IP_CT_DIR_ORIGINAL].packets) + atomic64_read(&acct[IP_CT_DIR_REPLY].packets));
1541 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
1542 +/* Converts an unfriendly string into a friendly one by
1543 +replacing unprintables with periods and all whitespace with " ". */
1544 +static char * friendly_print(unsigned char * s)
1546 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
1550 + if (net_ratelimit())
1551 + printk(KERN_ERR "layer7: out of memory in "
1552 + "friendly_print, bailing.\n");
1556 + for(i = 0; i < strlen(s); i++){
1557 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
1558 + else if(isspace(s[i])) f[i] = ' ';
1565 +static char dec2hex(int i)
1572 + return (i - 10 + 'a');
1575 + if (net_ratelimit())
1576 + printk("layer7: Problem in dec2hex\n");
1581 +static char * hex_print(unsigned char * s)
1583 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
1587 + if (net_ratelimit())
1588 + printk(KERN_ERR "layer7: out of memory in hex_print, "
1593 + for(i = 0; i < strlen(s); i++) {
1594 + g[i*3 ] = dec2hex(s[i]/16);
1595 + g[i*3 + 1] = dec2hex(s[i]%16);
1604 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
1605 +over again, it make sense to cache the results. */
1606 +static regexp * compile_and_cache(const char * regex_string,
1607 + const char * protocol)
1609 + struct pattern_cache * node = first_pattern_cache;
1610 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
1611 + struct pattern_cache * tmp;
1614 + while (node != NULL) {
1615 + if (!strcmp(node->regex_string, regex_string))
1616 + return node->pattern;
1618 + last_pattern_cache = node;/* points at the last non-NULL node */
1619 + node = node->next;
1622 + /* If we reach the end of the list, then we have not yet cached
1623 + the pattern for this regex. Let's do that now.
1624 + Be paranoid about running out of memory to avoid list corruption. */
1625 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
1628 + if (net_ratelimit())
1629 + printk(KERN_ERR "layer7: out of memory in "
1630 + "compile_and_cache, bailing.\n");
1634 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
1635 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
1638 + if(!tmp->regex_string || !tmp->pattern) {
1639 + if (net_ratelimit())
1640 + printk(KERN_ERR "layer7: out of memory in "
1641 + "compile_and_cache, bailing.\n");
1642 + kfree(tmp->regex_string);
1643 + kfree(tmp->pattern);
1648 + /* Ok. The new node is all ready now. */
1651 + if(first_pattern_cache == NULL) /* list is empty */
1652 + first_pattern_cache = node; /* make node the beginning */
1654 + last_pattern_cache->next = node; /* attach node to the end */
1656 + /* copy the string and compile the regex */
1657 + len = strlen(regex_string);
1658 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
1659 + node->pattern = regcomp((char *)regex_string, &len);
1660 + if ( !node->pattern ) {
1661 + if (net_ratelimit())
1662 + printk(KERN_ERR "layer7: Error compiling regexp "
1664 + regex_string, protocol);
1665 + /* pattern is now cached as NULL, so we won't try again. */
1668 + strcpy(node->regex_string, regex_string);
1669 + return node->pattern;
1672 +static int can_handle(const struct sk_buff *skb)
1674 + if(!ip_hdr(skb)) /* not IP */
1676 + if(ip_hdr(skb)->protocol != IPPROTO_TCP &&
1677 + ip_hdr(skb)->protocol != IPPROTO_UDP &&
1678 + ip_hdr(skb)->protocol != IPPROTO_ICMP)
1683 +/* Returns offset the into the skb->data that the application data starts */
1684 +static int app_data_offset(const struct sk_buff *skb)
1686 + /* In case we are ported somewhere (ebtables?) where ip_hdr(skb)
1687 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
1688 + int ip_hl = 4*ip_hdr(skb)->ihl;
1690 + if( ip_hdr(skb)->protocol == IPPROTO_TCP ) {
1691 + /* 12 == offset into TCP header for the header length field.
1692 + Can't get this with skb->h.th->doff because the tcphdr
1693 + struct doesn't get set when routing (this is confirmed to be
1694 + true in Netfilter as well as QoS.) */
1695 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
1697 + return ip_hl + tcp_hl;
1698 + } else if( ip_hdr(skb)->protocol == IPPROTO_UDP ) {
1699 + return ip_hl + 8; /* UDP header is always 8 bytes */
1700 + } else if( ip_hdr(skb)->protocol == IPPROTO_ICMP ) {
1701 + return ip_hl + 8; /* ICMP header is 8 bytes */
1703 + if (net_ratelimit())
1704 + printk(KERN_ERR "layer7: tried to handle unknown "
1706 + return ip_hl + 8; /* something reasonable */
1710 +/* handles whether there's a match when we aren't appending data anymore */
1711 +static int match_no_append(struct nf_conn * conntrack,
1712 + struct nf_conn * master_conntrack,
1713 + enum ip_conntrack_info ctinfo,
1714 + enum ip_conntrack_info master_ctinfo,
1715 + const struct xt_layer7_info * info)
1717 + /* If we're in here, throw the app data away */
1718 + if(master_conntrack->layer7.app_data != NULL) {
1720 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
1721 + if(!master_conntrack->layer7.app_proto) {
1723 + friendly_print(master_conntrack->layer7.app_data);
1725 + hex_print(master_conntrack->layer7.app_data);
1726 + DPRINTK("\nl7-filter gave up after %d bytes "
1727 + "(%d packets):\n%s\n",
1728 + strlen(f), total_acct_packets(master_conntrack), f);
1730 + DPRINTK("In hex: %s\n", g);
1735 + kfree(master_conntrack->layer7.app_data);
1736 + master_conntrack->layer7.app_data = NULL; /* don't free again */
1739 + if(master_conntrack->layer7.app_proto){
1740 + /* Here child connections set their .app_proto (for /proc) */
1741 + if(!conntrack->layer7.app_proto) {
1742 + conntrack->layer7.app_proto =
1743 + kmalloc(strlen(master_conntrack->layer7.app_proto)+1,
1745 + if(!conntrack->layer7.app_proto){
1746 + if (net_ratelimit())
1747 + printk(KERN_ERR "layer7: out of memory "
1748 + "in match_no_append, "
1752 + strcpy(conntrack->layer7.app_proto,
1753 + master_conntrack->layer7.app_proto);
1756 + return (!strcmp(master_conntrack->layer7.app_proto,
1760 + /* If not classified, set to "unknown" to distinguish from
1761 + connections that are still being tested. */
1762 + master_conntrack->layer7.app_proto =
1763 + kmalloc(strlen("unknown")+1, GFP_ATOMIC);
1764 + if(!master_conntrack->layer7.app_proto){
1765 + if (net_ratelimit())
1766 + printk(KERN_ERR "layer7: out of memory in "
1767 + "match_no_append, bailing.\n");
1770 + strcpy(master_conntrack->layer7.app_proto, "unknown");
1775 +/* add the new app data to the conntrack. Return number of bytes added. */
1776 +static int add_data(struct nf_conn * master_conntrack,
1777 + char * app_data, int appdatalen)
1779 + int length = 0, i;
1780 + int oldlength = master_conntrack->layer7.app_data_len;
1782 + /* This is a fix for a race condition by Deti Fliegl. However, I'm not
1783 + clear on whether the race condition exists or whether this really
1784 + fixes it. I might just be being dense... Anyway, if it's not really
1785 + a fix, all it does is waste a very small amount of time. */
1786 + if(!master_conntrack->layer7.app_data) return 0;
1788 + /* Strip nulls. Make everything lower case (our regex lib doesn't
1789 + do case insensitivity). Add it to the end of the current data. */
1790 + for(i = 0; i < maxdatalen-oldlength-1 &&
1791 + i < appdatalen; i++) {
1792 + if(app_data[i] != '\0') {
1793 + /* the kernel version of tolower mungs 'upper ascii' */
1794 + master_conntrack->layer7.app_data[length+oldlength] =
1795 + isascii(app_data[i])?
1796 + tolower(app_data[i]) : app_data[i];
1801 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
1802 + master_conntrack->layer7.app_data_len = length + oldlength;
1807 +/* taken from drivers/video/modedb.c */
1808 +static int my_atoi(const char *s)
1815 + val = 10*val+(*s-'0');
1823 +/* write out num_packets to userland. */
1824 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
1825 + int* eof, void * data)
1827 + if(num_packets > 99 && net_ratelimit())
1828 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
1830 + page[0] = num_packets/10 + '0';
1831 + page[1] = num_packets%10 + '0';
1840 +/* Read in num_packets from userland */
1841 +static int layer7_write_proc(struct file* file, const char* buffer,
1842 + unsigned long count, void *data)
1844 + char * foo = kmalloc(count, GFP_ATOMIC);
1847 + if (net_ratelimit())
1848 + printk(KERN_ERR "layer7: out of memory, bailing. "
1849 + "num_packets unchanged.\n");
1853 + if(copy_from_user(foo, buffer, count)) {
1858 + num_packets = my_atoi(foo);
1861 + /* This has an arbitrary limit to make the math easier. I'm lazy.
1862 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
1863 + if(num_packets > 99) {
1864 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
1866 + } else if(num_packets < 1) {
1867 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
1875 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1876 +match(const struct sk_buff *skbin, struct xt_action_param *par)
1878 +match(const struct sk_buff *skbin,
1879 + const struct net_device *in,
1880 + const struct net_device *out,
1881 + const struct xt_match *match,
1882 + const void *matchinfo,
1884 + unsigned int protoff,
1888 + /* sidestep const without getting a compiler warning... */
1889 + struct sk_buff * skb = (struct sk_buff *)skbin;
1891 + const struct xt_layer7_info * info =
1892 + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1898 + enum ip_conntrack_info master_ctinfo, ctinfo;
1899 + struct nf_conn *master_conntrack, *conntrack;
1900 + unsigned char * app_data;
1901 + unsigned int pattern_result, appdatalen;
1902 + regexp * comppattern;
1904 + /* Be paranoid/incompetent - lock the entire match function. */
1905 + spin_lock_bh(&l7_lock);
1907 + if(!can_handle(skb)){
1908 + DPRINTK("layer7: This is some protocol I can't handle.\n");
1909 + spin_unlock_bh(&l7_lock);
1910 + return info->invert;
1913 + /* Treat parent & all its children together as one connection, except
1914 + for the purpose of setting conntrack->layer7.app_proto in the actual
1915 + connection. This makes /proc/net/ip_conntrack more satisfying. */
1916 + if(!(conntrack = nf_ct_get(skb, &ctinfo)) ||
1917 + !(master_conntrack=nf_ct_get(skb,&master_ctinfo))){
1918 + DPRINTK("layer7: couldn't get conntrack.\n");
1919 + spin_unlock_bh(&l7_lock);
1920 + return info->invert;
1923 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
1924 + while (master_ct(master_conntrack) != NULL)
1925 + master_conntrack = master_ct(master_conntrack);
1927 + /* if we've classified it or seen too many packets */
1928 + if(total_acct_packets(master_conntrack) > num_packets ||
1929 + master_conntrack->layer7.app_proto) {
1931 + pattern_result = match_no_append(conntrack, master_conntrack,
1932 + ctinfo, master_ctinfo, info);
1934 + /* skb->cb[0] == seen. Don't do things twice if there are
1935 + multiple l7 rules. I'm not sure that using cb for this purpose
1936 + is correct, even though it says "put your private variables
1937 + there". But it doesn't look like it is being used for anything
1938 + else in the skbs that make it here. */
1939 + skb->cb[0] = 1; /* marking it seen here's probably irrelevant */
1941 + spin_unlock_bh(&l7_lock);
1942 + return (pattern_result ^ info->invert);
1945 + if(skb_is_nonlinear(skb)){
1946 + if(skb_linearize(skb) != 0){
1947 + if (net_ratelimit())
1948 + printk(KERN_ERR "layer7: failed to linearize "
1949 + "packet, bailing.\n");
1950 + spin_unlock_bh(&l7_lock);
1951 + return info->invert;
1955 + /* now that the skb is linearized, it's safe to set these. */
1956 + app_data = skb->data + app_data_offset(skb);
1957 + appdatalen = skb_tail_pointer(skb) - app_data;
1959 + /* the return value gets checked later, when we're ready to use it */
1960 + comppattern = compile_and_cache(info->pattern, info->protocol);
1962 + /* On the first packet of a connection, allocate space for app data */
1963 + if(total_acct_packets(master_conntrack) == 1 && !skb->cb[0] &&
1964 + !master_conntrack->layer7.app_data){
1965 + master_conntrack->layer7.app_data =
1966 + kmalloc(maxdatalen, GFP_ATOMIC);
1967 + if(!master_conntrack->layer7.app_data){
1968 + if (net_ratelimit())
1969 + printk(KERN_ERR "layer7: out of memory in "
1970 + "match, bailing.\n");
1971 + spin_unlock_bh(&l7_lock);
1972 + return info->invert;
1975 + master_conntrack->layer7.app_data[0] = '\0';
1978 + /* Can be here, but unallocated, if numpackets is increased near
1979 + the beginning of a connection */
1980 + if(master_conntrack->layer7.app_data == NULL){
1981 + spin_unlock_bh(&l7_lock);
1982 + return info->invert; /* unmatched */
1987 + newbytes = add_data(master_conntrack, app_data, appdatalen);
1989 + if(newbytes == 0) { /* didn't add any data */
1991 + /* Didn't match before, not going to match now */
1992 + spin_unlock_bh(&l7_lock);
1993 + return info->invert;
1997 + /* If looking for "unknown", then never match. "Unknown" means that
1998 + we've given up; we're still trying with these packets. */
1999 + if(!strcmp(info->protocol, "unknown")) {
2000 + pattern_result = 0;
2001 + /* If looking for "unset", then always match. "Unset" means that we
2002 + haven't yet classified the connection. */
2003 + } else if(!strcmp(info->protocol, "unset")) {
2004 + pattern_result = 2;
2005 + DPRINTK("layer7: matched unset: not yet classified "
2006 + "(%d/%d packets)\n",
2007 + total_acct_packets(master_conntrack), num_packets);
2008 + /* If the regexp failed to compile, don't bother running it */
2009 + } else if(comppattern &&
2010 + regexec(comppattern, master_conntrack->layer7.app_data)){
2011 + DPRINTK("layer7: matched %s\n", info->protocol);
2012 + pattern_result = 1;
2013 + } else pattern_result = 0;
2015 + if(pattern_result == 1) {
2016 + master_conntrack->layer7.app_proto =
2017 + kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
2018 + if(!master_conntrack->layer7.app_proto){
2019 + if (net_ratelimit())
2020 + printk(KERN_ERR "layer7: out of memory in "
2021 + "match, bailing.\n");
2022 + spin_unlock_bh(&l7_lock);
2023 + return (pattern_result ^ info->invert);
2025 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
2026 + } else if(pattern_result > 1) { /* cleanup from "unset" */
2027 + pattern_result = 1;
2030 + /* mark the packet seen */
2033 + spin_unlock_bh(&l7_lock);
2034 + return (pattern_result ^ info->invert);
2037 +// load nf_conntrack_ipv4
2038 +static int check(const struct xt_mtchk_param *par)
2040 + if (nf_ct_l3proto_try_module_get(par->family) < 0) {
2041 + pr_info("can't load conntrack support for "
2042 + "proto=%d\n", par->family);
2049 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
2050 + static void destroy(const struct xt_mtdtor_param *par)
2052 + nf_ct_l3proto_module_put(par->family);
2055 + static void destroy(const struct xt_match *match, void *matchinfo)
2057 + nf_ct_l3proto_module_put(match->family);
2061 +static struct xt_match xt_layer7_match[] __read_mostly = {
2064 + .family = AF_INET,
2065 + .checkentry = check,
2067 + .destroy = destroy,
2068 + .matchsize = sizeof(struct xt_layer7_info),
2073 +static void layer7_cleanup_proc(void)
2075 + remove_proc_entry("layer7_numpackets", init_net.proc_net);
2078 +/* register the proc file */
2079 +static void layer7_init_proc(void)
2081 + struct proc_dir_entry* entry;
2082 + entry = create_proc_entry("layer7_numpackets", 0644, init_net.proc_net);
2083 + entry->read_proc = layer7_read_proc;
2084 + entry->write_proc = layer7_write_proc;
2087 +static int __init xt_layer7_init(void)
2091 + layer7_init_proc();
2092 + if(maxdatalen < 1) {
2093 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, "
2097 + /* This is not a hard limit. It's just here to prevent people from
2098 + bringing their slow machines to a grinding halt. */
2099 + else if(maxdatalen > 65536) {
2100 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, "
2102 + maxdatalen = 65536;
2104 + return xt_register_matches(xt_layer7_match,
2105 + ARRAY_SIZE(xt_layer7_match));
2108 +static void __exit xt_layer7_fini(void)
2110 + layer7_cleanup_proc();
2111 + xt_unregister_matches(xt_layer7_match, ARRAY_SIZE(xt_layer7_match));
2114 +module_init(xt_layer7_init);
2115 +module_exit(xt_layer7_fini);