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