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