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