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