blob: d8e3891b5f8bd0aa9287fd8a7af88d4a6514a6e9 [file] [log] [blame]
Pablo Neira Ayuso75676622005-08-21 23:30:34 -07001/* String matching match for iptables
2 *
3 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
4 *
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.
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter/xt_string.h>
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070016#include <linux/textsearch.h>
17
18MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
19MODULE_DESCRIPTION("IP tables string match module");
20MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080021MODULE_ALIAS("ipt_string");
22MODULE_ALIAS("ip6t_string");
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070023
24static int match(const struct sk_buff *skb,
25 const struct net_device *in,
26 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080027 const struct xt_match *match,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070028 const void *matchinfo,
29 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080030 unsigned int protoff,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070031 int *hotdrop)
32{
Patrick McHardy3e72b2f2006-05-29 18:19:19 -070033 const struct xt_string_info *conf = matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070034 struct ts_state state;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070035
36 memset(&state, 0, sizeof(struct ts_state));
37
38 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
39 conf->to_offset, conf->config, &state)
40 != UINT_MAX) && !conf->invert;
41}
42
Harald Welte2e4e6a12006-01-12 13:30:04 -080043#define STRING_TEXT_PRIV(m) ((struct xt_string_info *) m)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070044
45static int checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080046 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -080047 const struct xt_match *match,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070048 void *matchinfo,
49 unsigned int matchsize,
50 unsigned int hook_mask)
51{
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 struct xt_string_info *conf = matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070053 struct ts_config *ts_conf;
54
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070055 /* Damn, can't handle this case properly with iptables... */
56 if (conf->from_offset > conf->to_offset)
57 return 0;
Patrick McHardy3ab72082006-07-31 23:47:31 -070058 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
59 return 0;
60 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
61 return 0;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070062 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
63 GFP_KERNEL, TS_AUTOLOAD);
64 if (IS_ERR(ts_conf))
65 return 0;
66
67 conf->config = ts_conf;
68
69 return 1;
70}
71
Patrick McHardyc4986732006-03-20 18:02:56 -080072static void destroy(const struct xt_match *match, void *matchinfo,
73 unsigned int matchsize)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070074{
75 textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
76}
77
Harald Welte2e4e6a12006-01-12 13:30:04 -080078static struct xt_match string_match = {
79 .name = "string",
80 .match = match,
Patrick McHardy5d04bff2006-03-20 18:01:58 -080081 .matchsize = sizeof(struct xt_string_info),
Harald Welte2e4e6a12006-01-12 13:30:04 -080082 .checkentry = checkentry,
83 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080084 .family = AF_INET,
Harald Welte2e4e6a12006-01-12 13:30:04 -080085 .me = THIS_MODULE
86};
87static struct xt_match string6_match = {
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070088 .name = "string",
89 .match = match,
Patrick McHardy5d04bff2006-03-20 18:01:58 -080090 .matchsize = sizeof(struct xt_string_info),
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070091 .checkentry = checkentry,
92 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080093 .family = AF_INET6,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070094 .me = THIS_MODULE
95};
96
Andrew Morton65b4b4e2006-03-28 16:37:06 -080097static int __init xt_string_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070098{
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 int ret;
100
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800101 ret = xt_register_match(&string_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800102 if (ret)
103 return ret;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800104 ret = xt_register_match(&string6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800105 if (ret)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800106 xt_unregister_match(&string_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800107
108 return ret;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700109}
110
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800111static void __exit xt_string_fini(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700112{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800113 xt_unregister_match(&string_match);
114 xt_unregister_match(&string6_match);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700115}
116
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800117module_init(xt_string_init);
118module_exit(xt_string_fini);