1 /* String matching match for iptables
3 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/netfilter_ipv4/ip_tables.h>
15 #include <linux/netfilter_ipv4/ipt_string.h>
16 #include <linux/textsearch.h>
18 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
19 MODULE_DESCRIPTION("IP tables string match module");
20 MODULE_LICENSE("GPL");
22 static int match(const struct sk_buff *skb,
23 const struct net_device *in,
24 const struct net_device *out,
25 const void *matchinfo,
29 struct ts_state state;
30 struct ipt_string_info *conf = (struct ipt_string_info *) matchinfo;
32 memset(&state, 0, sizeof(struct ts_state));
34 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
35 conf->to_offset, conf->config, &state)
36 != UINT_MAX) && !conf->invert;
39 #define STRING_TEXT_PRIV(m) ((struct ipt_string_info *) m)
41 static int checkentry(const char *tablename,
42 const struct ipt_ip *ip,
44 unsigned int matchsize,
45 unsigned int hook_mask)
47 struct ipt_string_info *conf = matchinfo;
48 struct ts_config *ts_conf;
50 if (matchsize != IPT_ALIGN(sizeof(struct ipt_string_info)))
53 /* Damn, can't handle this case properly with iptables... */
54 if (conf->from_offset > conf->to_offset)
57 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
58 GFP_KERNEL, TS_AUTOLOAD);
62 conf->config = ts_conf;
67 static void destroy(void *matchinfo, unsigned int matchsize)
69 textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
72 static struct ipt_match string_match = {
75 .checkentry = checkentry,
80 static int __init init(void)
82 return ipt_register_match(&string_match);
85 static void __exit fini(void)
87 ipt_unregister_match(&string_match);