1 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_string.h
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_string.h 2007-12-15 05:20:08.388318268 +0100
9 +#define IPT_STRING_MAX_PATTERN_SIZE 128
10 +#define IPT_STRING_MAX_ALGO_NAME_SIZE 16
12 +struct ipt_string_info
14 + u_int16_t from_offset;
15 + u_int16_t to_offset;
16 + char algo[IPT_STRING_MAX_ALGO_NAME_SIZE];
17 + char pattern[IPT_STRING_MAX_PATTERN_SIZE];
20 + struct ts_config __attribute__((aligned(8))) *config;
23 +#endif /*_IPT_STRING_H*/
24 Index: linux-2.4.35.4/net/ipv4/netfilter/Config.in
25 ===================================================================
26 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Config.in 2007-12-15 05:20:07.892290000 +0100
27 +++ linux-2.4.35.4/net/ipv4/netfilter/Config.in 2007-12-15 05:20:08.396318720 +0100
30 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
31 dep_tristate ' Unclean match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_UNCLEAN $CONFIG_IP_NF_IPTABLES
32 + dep_tristate ' String match support (EXPERIMENTAL) ' CONFIG_IP_NF_MATCH_STRING $CONFIG_IP_NF_IPTABLES
33 dep_tristate ' Owner match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_OWNER $CONFIG_IP_NF_IPTABLES
34 dep_tristate ' Layer 7 match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7 $CONFIG_IP_NF_CONNTRACK
35 dep_mbool ' Layer 7 debugging output (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_LAYER7_DEBUG $CONFIG_IP_NF_MATCH_LAYER7
36 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_string.c
37 ===================================================================
38 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
39 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_string.c 2007-12-15 05:20:08.400318949 +0100
41 +/* String matching match for iptables
43 + * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
45 + * This program is free software; you can redistribute it and/or modify
46 + * it under the terms of the GNU General Public License version 2 as
47 + * published by the Free Software Foundation.
50 +#include <linux/init.h>
51 +#include <linux/module.h>
52 +#include <linux/kernel.h>
53 +#include <linux/skbuff.h>
54 +#include <linux/netfilter_ipv4/ip_tables.h>
55 +#include <linux/netfilter_ipv4/ipt_string.h>
56 +#include "textsearch/textsearch.h"
57 +#include "textsearch/textsearch.c"
58 +#include "textsearch/ts_bm.c"
59 +#include "textsearch/ts_kmp.c"
61 +MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
62 +MODULE_DESCRIPTION("IP tables string match module");
63 +MODULE_LICENSE("GPL");
65 +static int match(const struct sk_buff *skb,
66 + const struct net_device *in,
67 + const struct net_device *out,
68 + const void *matchinfo,
72 + struct iphdr *ip = skb->nh.iph;
73 + struct ts_state state;
74 + struct ipt_string_info *conf = (struct ipt_string_info *) matchinfo;
75 + char *buf = (char *)ip+(ip->ihl*4);
76 + int len = ntohs(ip->tot_len)-(ip->ihl*4);
78 + memset(&state, 0, sizeof(struct ts_state));
80 + return (textsearch_find_continuous(conf->config, &state, buf, len) != UINT_MAX) && !conf->invert;
83 +#define STRING_TEXT_PRIV(m) ((struct ipt_string_info *) m)
85 +static int checkentry(const char *tablename,
86 + const struct ipt_ip *ip,
88 + unsigned int matchsize,
89 + unsigned int hook_mask)
91 + struct ipt_string_info *conf = matchinfo;
92 + struct ts_config *ts_conf;
94 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_string_info)))
97 + /* Damn, can't handle this case properly with iptables... */
98 + if (conf->from_offset > conf->to_offset)
101 + ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
102 + GFP_KERNEL, TS_AUTOLOAD);
103 + if (IS_ERR(ts_conf))
106 + conf->config = ts_conf;
111 +static void destroy(void *matchinfo, unsigned int matchsize)
113 + textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
116 +static struct ipt_match string_match = {
119 + .checkentry = checkentry,
120 + .destroy = destroy,
124 +static int __init init(void)
128 + return ipt_register_match(&string_match);
131 +static void __exit fini(void)
135 + ipt_unregister_match(&string_match);
140 Index: linux-2.4.35.4/net/ipv4/netfilter/Makefile
141 ===================================================================
142 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Makefile 2007-12-15 05:20:07.900290454 +0100
143 +++ linux-2.4.35.4/net/ipv4/netfilter/Makefile 2007-12-15 05:20:08.400318949 +0100
145 obj-$(CONFIG_IP_NF_MATCH_CONNMARK) += ipt_connmark.o
146 obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += ipt_conntrack.o
147 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
148 +obj-$(CONFIG_IP_NF_MATCH_STRING) += ipt_string.o
149 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
150 obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
152 Index: linux-2.4.35.4/net/ipv4/netfilter/textsearch/textsearch.c
153 ===================================================================
154 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
155 +++ linux-2.4.35.4/net/ipv4/netfilter/textsearch/textsearch.c 2007-12-15 05:20:08.400318949 +0100
158 + * lib/textsearch.c Generic text search interface
160 + * This program is free software; you can redistribute it and/or
161 + * modify it under the terms of the GNU General Public License
162 + * as published by the Free Software Foundation; either version
163 + * 2 of the License, or (at your option) any later version.
165 + * Authors: Thomas Graf <tgraf@suug.ch>
166 + * Pablo Neira Ayuso <pablo@eurodev.net>
168 + * ==========================================================================
172 + * The textsearch infrastructure provides text searching facitilies for
173 + * both linear and non-linear data. Individual search algorithms are
174 + * implemented in modules and chosen by the user.
179 + * +----------------+
180 + * | finish()|<--------------(6)-----------------+
181 + * |get_next_block()|<--------------(5)---------------+ |
182 + * | | Algorithm | |
183 + * | | +------------------------------+
184 + * | | | init() find() destroy() |
185 + * | | +------------------------------+
186 + * | | Core API ^ ^ ^
187 + * | | +---------------+ (2) (4) (8)
188 + * | (1)|----->| prepare() |---+ | |
189 + * | (3)|----->| find()/next() |-----------+ |
190 + * | (7)|----->| destroy() |----------------------+
191 + * +----------------+ +---------------+
193 + * (1) User configures a search by calling _prepare() specifying the
194 + * search parameters such as the pattern and algorithm name.
195 + * (2) Core requests the algorithm to allocate and initialize a search
196 + * configuration according to the specified parameters.
197 + * (3) User starts the search(es) by calling _find() or _next() to
198 + * fetch subsequent occurrences. A state variable is provided
199 + * to the algorihtm to store persistant variables.
200 + * (4) Core eventually resets the search offset and forwards the find()
201 + * request to the algorithm.
202 + * (5) Algorithm calls get_next_block() provided by the user continously
203 + * to fetch the data to be searched in block by block.
204 + * (6) Algorithm invokes finish() after the last call to get_next_block
205 + * to clean up any leftovers from get_next_block. (Optional)
206 + * (7) User destroys the configuration by calling _destroy().
207 + * (8) Core notifies the algorithm to destroy algorithm specific
208 + * allocations. (Optional)
212 + * Before a search can be performed, a configuration must be created
213 + * by calling textsearch_prepare() specyfing the searching algorithm and
214 + * the pattern to look for. The returned configuration may then be used
215 + * for an arbitary amount of times and even in parallel as long as a
216 + * separate struct ts_state variable is provided to every instance.
218 + * The actual search is performed by either calling textsearch_find_-
219 + * continuous() for linear data or by providing an own get_next_block()
220 + * implementation and calling textsearch_find(). Both functions return
221 + * the position of the first occurrence of the patern or UINT_MAX if
222 + * no match was found. Subsequent occurences can be found by calling
223 + * textsearch_next() regardless of the linearity of the data.
225 + * Once you're done using a configuration it must be given back via
226 + * textsearch_destroy.
231 + * struct ts_config *conf;
232 + * struct ts_state state;
233 + * const char *pattern = "chicken";
234 + * const char *example = "We dance the funky chicken";
236 + * conf = textsearch_prepare("kmp", pattern, strlen(pattern),
237 + * GFP_KERNEL, TS_AUTOLOAD);
238 + * if (IS_ERR(conf)) {
239 + * err = PTR_ERR(conf);
243 + * pos = textsearch_find_continuous(conf, &state, example, strlen(example));
244 + * if (pos != UINT_MAX)
245 + * panic("Oh my god, dancing chickens at %d\n", pos);
247 + * textsearch_destroy(conf);
249 + * ==========================================================================
252 +#include <linux/config.h>
253 +#include <linux/module.h>
254 +#include <linux/types.h>
255 +#include <linux/string.h>
256 +#include <linux/init.h>
257 +#include <linux/netfilter_ipv4/lockhelp.h>
258 +#include "textsearch.h"
260 +static LIST_HEAD(ts_ops);
261 +static spinlock_t ts_mod_lock = SPIN_LOCK_UNLOCKED;
262 +static DECLARE_RWLOCK(ts_ops_lock);
264 +static inline struct ts_ops *lookup_ts_algo(const char *name)
268 + read_lock(&ts_ops_lock);
269 + list_for_each_entry(o, &ts_ops, list) {
270 + if (!strcmp(name, o->name)) {
272 + read_unlock(&ts_ops_lock);
276 + read_unlock(&ts_ops_lock);
282 + * textsearch_register - register a textsearch module
283 + * @ops: operations lookup table
285 + * This function must be called by textsearch modules to announce
286 + * their presence. The specified &@ops must have %name set to a
287 + * unique identifier and the callbacks find(), init(), get_pattern(),
288 + * and get_pattern_len() must be implemented.
290 + * Returns 0 or -EEXISTS if another module has already registered
293 +int textsearch_register(struct ts_ops *ops)
298 + if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
299 + ops->get_pattern == NULL || ops->get_pattern_len == NULL)
302 + spin_lock(&ts_mod_lock);
303 + list_for_each_entry(o, &ts_ops, list) {
304 + if (!strcmp(ops->name, o->name))
308 + write_lock(&ts_ops_lock);
309 + list_add_tail(&ops->list, &ts_ops);
310 + write_unlock(&ts_ops_lock);
314 + spin_unlock(&ts_mod_lock);
319 + * textsearch_unregister - unregister a textsearch module
320 + * @ops: operations lookup table
322 + * This function must be called by textsearch modules to announce
323 + * their disappearance for examples when the module gets unloaded.
324 + * The &ops parameter must be the same as the one during the
327 + * Returns 0 on success or -ENOENT if no matching textsearch
328 + * registration was found.
330 +int textsearch_unregister(struct ts_ops *ops)
335 + spin_lock(&ts_mod_lock);
336 + list_for_each_entry(o, &ts_ops, list) {
338 + write_lock(&ts_ops_lock);
339 + list_del(&o->list);
340 + write_unlock(&ts_ops_lock);
347 + spin_unlock(&ts_mod_lock);
351 +struct ts_linear_state
357 +static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
358 + struct ts_config *conf,
359 + struct ts_state *state)
361 + struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
363 + if (likely(consumed < st->len)) {
364 + *dst = st->data + consumed;
365 + return st->len - consumed;
372 + * textsearch_find_continuous - search a pattern in continuous/linear data
373 + * @conf: search configuration
374 + * @state: search state
375 + * @data: data to search in
376 + * @len: length of data
378 + * A simplified version of textsearch_find() for continuous/linear data.
379 + * Call textsearch_next() to retrieve subsequent matches.
381 + * Returns the position of first occurrence of the pattern or
382 + * UINT_MAX if no occurrence was found.
384 +unsigned int textsearch_find_continuous(struct ts_config *conf,
385 + struct ts_state *state,
386 + const void *data, unsigned int len)
388 + struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
390 + conf->get_next_block = get_linear_data;
394 + return textsearch_find(conf, state);
398 + * textsearch_prepare - Prepare a search
399 + * @algo: name of search algorithm
400 + * @pattern: pattern data
401 + * @len: length of pattern
402 + * @gfp_mask: allocation mask
403 + * @flags: search flags
405 + * Looks up the search algorithm module and creates a new textsearch
406 + * configuration for the specified pattern. Upon completion all
407 + * necessary refcnts are held and the configuration must be put back
408 + * using textsearch_put() after usage.
410 + * Note: The format of the pattern may not be compatible between
411 + * the various search algorithms.
413 + * Returns a new textsearch configuration according to the specified
414 + * parameters or a ERR_PTR().
416 +struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
417 + unsigned int len, gfp_t gfp_mask, int flags)
420 + struct ts_config *conf;
421 + struct ts_ops *ops;
423 + ops = lookup_ts_algo(algo);
428 + conf = ops->init(pattern, len, gfp_mask);
429 + if (IS_ERR(conf)) {
430 + err = PTR_ERR(conf);
441 + return ERR_PTR(err);
445 + * textsearch_destroy - destroy a search configuration
446 + * @conf: search configuration
448 + * Releases all references of the configuration and frees
451 +void textsearch_destroy(struct ts_config *conf)
454 + if (conf->ops->destroy)
455 + conf->ops->destroy(conf);
462 Index: linux-2.4.35.4/net/ipv4/netfilter/textsearch/textsearch.h
463 ===================================================================
464 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
465 +++ linux-2.4.35.4/net/ipv4/netfilter/textsearch/textsearch.h 2007-12-15 05:20:08.400318949 +0100
467 +#ifndef __LINUX_TEXTSEARCH_H
468 +#define __LINUX_TEXTSEARCH_H
472 +#include <linux/types.h>
473 +#include <linux/list.h>
474 +#include <linux/kernel.h>
475 +#include <linux/module.h>
476 +#include <linux/slab.h>
482 + * TS_AUTOLOAD - Automatically load textsearch modules when needed
484 +#define TS_AUTOLOAD 1
487 + * struct ts_state - search state
488 + * @offset: offset for next match
489 + * @cb: control buffer, for persistant variables of get_next_block()
493 + unsigned int offset;
498 + * struct ts_ops - search module operations
499 + * @name: name of search algorithm
500 + * @init: initialization function to prepare a search
501 + * @find: find the next occurrence of the pattern
502 + * @destroy: destroy algorithm specific parts of a search configuration
503 + * @get_pattern: return head of pattern
504 + * @get_pattern_len: return length of pattern
505 + * @owner: module reference to algorithm
510 + struct ts_config * (*init)(const void *, unsigned int, gfp_t);
511 + unsigned int (*find)(struct ts_config *,
512 + struct ts_state *);
513 + void (*destroy)(struct ts_config *);
514 + void * (*get_pattern)(struct ts_config *);
515 + unsigned int (*get_pattern_len)(struct ts_config *);
516 + struct module *owner;
517 + struct list_head list;
521 + * struct ts_config - search configuration
522 + * @ops: operations of chosen algorithm
523 + * @get_next_block: callback to fetch the next block to search in
524 + * @finish: callback to finalize a search
528 + struct ts_ops *ops;
531 + * get_next_block - fetch next block of data
532 + * @consumed: number of bytes consumed by the caller
533 + * @dst: destination buffer
534 + * @conf: search configuration
535 + * @state: search state
537 + * Called repeatedly until 0 is returned. Must assign the
538 + * head of the next block of data to &*dst and return the length
539 + * of the block or 0 if at the end. consumed == 0 indicates
540 + * a new search. May store/read persistant values in state->cb.
542 + unsigned int (*get_next_block)(unsigned int consumed,
544 + struct ts_config *conf,
545 + struct ts_state *state);
548 + * finish - finalize/clean a series of get_next_block() calls
549 + * @conf: search configuration
550 + * @state: search state
552 + * Called after the last use of get_next_block(), may be used
553 + * to cleanup any leftovers.
555 + void (*finish)(struct ts_config *conf,
556 + struct ts_state *state);
560 + * textsearch_next - continue searching for a pattern
561 + * @conf: search configuration
562 + * @state: search state
564 + * Continues a search looking for more occurrences of the pattern.
565 + * textsearch_find() must be called to find the first occurrence
566 + * in order to reset the state.
568 + * Returns the position of the next occurrence of the pattern or
569 + * UINT_MAX if not match was found.
571 +static inline unsigned int textsearch_next(struct ts_config *conf,
572 + struct ts_state *state)
574 + unsigned int ret = conf->ops->find(conf, state);
577 + conf->finish(conf, state);
583 + * textsearch_find - start searching for a pattern
584 + * @conf: search configuration
585 + * @state: search state
587 + * Returns the position of first occurrence of the pattern or
588 + * UINT_MAX if no match was found.
590 +static inline unsigned int textsearch_find(struct ts_config *conf,
591 + struct ts_state *state)
594 + return textsearch_next(conf, state);
598 + * textsearch_get_pattern - return head of the pattern
599 + * @conf: search configuration
601 +static inline void *textsearch_get_pattern(struct ts_config *conf)
603 + return conf->ops->get_pattern(conf);
607 + * textsearch_get_pattern_len - return length of the pattern
608 + * @conf: search configuration
610 +static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf)
612 + return conf->ops->get_pattern_len(conf);
615 +extern int textsearch_register(struct ts_ops *);
616 +extern int textsearch_unregister(struct ts_ops *);
617 +extern struct ts_config *textsearch_prepare(const char *, const void *,
618 + unsigned int, gfp_t, int);
619 +extern void textsearch_destroy(struct ts_config *conf);
620 +extern unsigned int textsearch_find_continuous(struct ts_config *,
622 + const void *, unsigned int);
625 +#define TS_PRIV_ALIGNTO 8
626 +#define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1))
628 +static inline struct ts_config *alloc_ts_config(size_t payload,
631 + struct ts_config *conf;
633 + conf = kmalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
635 + return ERR_PTR(-ENOMEM);
637 + memset(conf, 0, TS_PRIV_ALIGN(sizeof(*conf)) + payload);
641 +static inline void *ts_config_priv(struct ts_config *conf)
643 + return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config)));
646 +#endif /* __KERNEL__ */
649 Index: linux-2.4.35.4/net/ipv4/netfilter/textsearch/ts_bm.c
650 ===================================================================
651 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
652 +++ linux-2.4.35.4/net/ipv4/netfilter/textsearch/ts_bm.c 2007-12-15 05:20:08.400318949 +0100
655 + * lib/ts_bm.c Boyer-Moore text search implementation
657 + * This program is free software; you can redistribute it and/or
658 + * modify it under the terms of the GNU General Public License
659 + * as published by the Free Software Foundation; either version
660 + * 2 of the License, or (at your option) any later version.
662 + * Authors: Pablo Neira Ayuso <pablo@eurodev.net>
664 + * ==========================================================================
666 + * Implements Boyer-Moore string matching algorithm:
668 + * [1] A Fast String Searching Algorithm, R.S. Boyer and Moore.
669 + * Communications of the Association for Computing Machinery,
670 + * 20(10), 1977, pp. 762-772.
671 + * http://www.cs.utexas.edu/users/moore/publications/fstrpos.pdf
673 + * [2] Handbook of Exact String Matching Algorithms, Thierry Lecroq, 2004
674 + * http://www-igm.univ-mlv.fr/~lecroq/string/string.pdf
676 + * Note: Since Boyer-Moore (BM) performs searches for matchings from right
677 + * to left, it's still possible that a matching could be spread over
678 + * multiple blocks, in that case this algorithm won't find any coincidence.
680 + * If you're willing to ensure that such thing won't ever happen, use the
681 + * Knuth-Pratt-Morris (KMP) implementation instead. In conclusion, choose
682 + * the proper string search algorithm depending on your setting.
684 + * Say you're using the textsearch infrastructure for filtering, NIDS or
685 + * any similar security focused purpose, then go KMP. Otherwise, if you
686 + * really care about performance, say you're classifying packets to apply
687 + * Quality of Service (QoS) policies, and you don't mind about possible
688 + * matchings spread over multiple fragments, then go BM.
691 +#include <linux/config.h>
692 +#include <linux/kernel.h>
693 +#include <linux/module.h>
694 +#include <linux/types.h>
695 +#include <linux/string.h>
696 +#include "textsearch.h"
698 +/* Alphabet size, use ASCII */
702 +#define DEBUGP printk
704 +#define DEBUGP(args, format...)
710 + unsigned int patlen;
711 + unsigned int bad_shift[ASIZE];
712 + unsigned int good_shift[0];
715 +static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
717 + struct ts_bm *bm = ts_config_priv(conf);
718 + unsigned int i, text_len, consumed = state->offset;
720 + int shift = bm->patlen, bs;
723 + text_len = conf->get_next_block(consumed, &text, conf, state);
725 + if (unlikely(text_len == 0))
728 + while (shift < text_len) {
729 + DEBUGP("Searching in position %d (%c)\n",
730 + shift, text[shift]);
731 + for (i = 0; i < bm->patlen; i++)
732 + if (text[shift-i] != bm->pattern[bm->patlen-1-i])
735 + /* London calling... */
736 + DEBUGP("found!\n");
737 + return consumed += (shift-(bm->patlen-1));
739 +next: bs = bm->bad_shift[text[shift-i]];
741 + /* Now jumping to... */
742 + shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
744 + consumed += text_len;
750 +static int subpattern(u8 *pattern, int i, int j, int g)
752 + int x = i+g-1, y = j+g-1, ret = 0;
754 + while(pattern[x--] == pattern[y--]) {
760 + ret = pattern[i-1] != pattern[j-1];
768 +static void bm_compute_prefix_tbl(struct ts_bm *bm, const u8 *pattern,
773 + for (i = 0; i < ASIZE; i++)
774 + bm->bad_shift[i] = len;
775 + for (i = 0; i < len - 1; i++)
776 + bm->bad_shift[pattern[i]] = len - 1 - i;
778 + /* Compute the good shift array, used to match reocurrences
779 + * of a subpattern */
780 + bm->good_shift[0] = 1;
781 + for (i = 1; i < bm->patlen; i++)
782 + bm->good_shift[i] = bm->patlen;
783 + for (i = bm->patlen-1, g = 1; i > 0; g++, i--) {
784 + for (j = i-1; j >= 1-g ; j--)
785 + if (subpattern(bm->pattern, i, j, g)) {
786 + bm->good_shift[g] = bm->patlen-j-g;
792 +static struct ts_config *bm_init(const void *pattern, unsigned int len,
795 + struct ts_config *conf;
797 + unsigned int prefix_tbl_len = len * sizeof(unsigned int);
798 + size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
800 + conf = alloc_ts_config(priv_size, gfp_mask);
804 + bm = ts_config_priv(conf);
806 + bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
807 + bm_compute_prefix_tbl(bm, pattern, len);
808 + memcpy(bm->pattern, pattern, len);
813 +static void *bm_get_pattern(struct ts_config *conf)
815 + struct ts_bm *bm = ts_config_priv(conf);
816 + return bm->pattern;
819 +static unsigned int bm_get_pattern_len(struct ts_config *conf)
821 + struct ts_bm *bm = ts_config_priv(conf);
825 +static struct ts_ops bm_ops = {
829 + .get_pattern = bm_get_pattern,
830 + .get_pattern_len = bm_get_pattern_len,
831 + .owner = THIS_MODULE,
832 + .list = LIST_HEAD_INIT(bm_ops.list)
835 +static int __init init_bm(void)
837 + return textsearch_register(&bm_ops);
840 +static void __exit exit_bm(void)
842 + textsearch_unregister(&bm_ops);
844 Index: linux-2.4.35.4/net/ipv4/netfilter/textsearch/ts_kmp.c
845 ===================================================================
846 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
847 +++ linux-2.4.35.4/net/ipv4/netfilter/textsearch/ts_kmp.c 2007-12-15 05:20:08.404319178 +0100
850 + * lib/ts_kmp.c Knuth-Morris-Pratt text search implementation
852 + * This program is free software; you can redistribute it and/or
853 + * modify it under the terms of the GNU General Public License
854 + * as published by the Free Software Foundation; either version
855 + * 2 of the License, or (at your option) any later version.
857 + * Authors: Thomas Graf <tgraf@suug.ch>
859 + * ==========================================================================
861 + * Implements a linear-time string-matching algorithm due to Knuth,
862 + * Morris, and Pratt [1]. Their algorithm avoids the explicit
863 + * computation of the transition function DELTA altogether. Its
864 + * matching time is O(n), for n being length(text), using just an
865 + * auxiliary function PI[1..m], for m being length(pattern),
866 + * precomputed from the pattern in time O(m). The array PI allows
867 + * the transition function DELTA to be computed efficiently
868 + * "on the fly" as needed. Roughly speaking, for any state
869 + * "q" = 0,1,...,m and any character "a" in SIGMA, the value
870 + * PI["q"] contains the information that is independent of "a" and
871 + * is needed to compute DELTA("q", "a") [2]. Since the array PI
872 + * has only m entries, whereas DELTA has O(m|SIGMA|) entries, we
873 + * save a factor of |SIGMA| in the preprocessing time by computing
874 + * PI rather than DELTA.
876 + * [1] Cormen, Leiserson, Rivest, Stein
877 + * Introdcution to Algorithms, 2nd Edition, MIT Press
878 + * [2] See finite automation theory
881 +#include <linux/config.h>
882 +#include <linux/module.h>
883 +#include <linux/types.h>
884 +#include <linux/string.h>
885 +#include "textsearch.h"
890 + unsigned int pattern_len;
891 + unsigned int prefix_tbl[0];
894 +static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state)
896 + struct ts_kmp *kmp = ts_config_priv(conf);
897 + unsigned int i, q = 0, text_len, consumed = state->offset;
901 + text_len = conf->get_next_block(consumed, &text, conf, state);
903 + if (unlikely(text_len == 0))
906 + for (i = 0; i < text_len; i++) {
907 + while (q > 0 && kmp->pattern[q] != text[i])
908 + q = kmp->prefix_tbl[q - 1];
909 + if (kmp->pattern[q] == text[i])
911 + if (unlikely(q == kmp->pattern_len)) {
912 + state->offset = consumed + i + 1;
913 + return state->offset - kmp->pattern_len;
917 + consumed += text_len;
923 +static inline void kmp_compute_prefix_tbl(const u8 *pattern, unsigned int len,
924 + unsigned int *prefix_tbl)
928 + for (k = 0, q = 1; q < len; q++) {
929 + while (k > 0 && pattern[k] != pattern[q])
930 + k = prefix_tbl[k-1];
931 + if (pattern[k] == pattern[q])
937 +static struct ts_config *kmp_init(const void *pattern, unsigned int len,
940 + struct ts_config *conf;
941 + struct ts_kmp *kmp;
942 + unsigned int prefix_tbl_len = len * sizeof(unsigned int);
943 + size_t priv_size = sizeof(*kmp) + len + prefix_tbl_len;
945 + conf = alloc_ts_config(priv_size, gfp_mask);
949 + kmp = ts_config_priv(conf);
950 + kmp->pattern_len = len;
951 + kmp_compute_prefix_tbl(pattern, len, kmp->prefix_tbl);
952 + kmp->pattern = (u8 *) kmp->prefix_tbl + prefix_tbl_len;
953 + memcpy(kmp->pattern, pattern, len);
958 +static void *kmp_get_pattern(struct ts_config *conf)
960 + struct ts_kmp *kmp = ts_config_priv(conf);
961 + return kmp->pattern;
964 +static unsigned int kmp_get_pattern_len(struct ts_config *conf)
966 + struct ts_kmp *kmp = ts_config_priv(conf);
967 + return kmp->pattern_len;
970 +static struct ts_ops kmp_ops = {
974 + .get_pattern = kmp_get_pattern,
975 + .get_pattern_len = kmp_get_pattern_len,
976 + .owner = THIS_MODULE,
977 + .list = LIST_HEAD_INIT(kmp_ops.list)
980 +static int __init init_kmp(void)
982 + return textsearch_register(&kmp_ops);
985 +static void __exit exit_kmp(void)
987 + textsearch_unregister(&kmp_ops);