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/x_tables.h>
15 #include <linux/netfilter/xt_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");
21 MODULE_ALIAS("ipt_string");
22 MODULE_ALIAS("ip6t_string");
24 static int match(const struct sk_buff *skb,
25 const struct net_device *in,
26 const struct net_device *out,
27 const void *matchinfo,
32 struct ts_state state;
33 struct xt_string_info *conf = (struct xt_string_info *) matchinfo;
35 memset(&state, 0, sizeof(struct ts_state));
37 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
38 conf->to_offset, conf->config, &state)
39 != UINT_MAX) && !conf->invert;
42 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *) m)
44 static int checkentry(const char *tablename,
47 unsigned int matchsize,
48 unsigned int hook_mask)
50 struct xt_string_info *conf = matchinfo;
51 struct ts_config *ts_conf;
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 xt_match string_match = {
75 .matchsize = sizeof(struct xt_string_info),
76 .checkentry = checkentry,
80 static struct xt_match string6_match = {
83 .matchsize = sizeof(struct xt_string_info),
84 .checkentry = checkentry,
89 static int __init init(void)
93 ret = xt_register_match(AF_INET, &string_match);
96 ret = xt_register_match(AF_INET6, &string6_match);
98 xt_unregister_match(AF_INET, &string_match);
103 static void __exit fini(void)
105 xt_unregister_match(AF_INET, &string_match);
106 xt_unregister_match(AF_INET6, &string6_match);