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