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