blob: 864133442cdac6c35f0f50edb81664565bf65b47 [file] [log] [blame]
Pablo Neira Ayuso75676622005-08-21 23:30:34 -07001/* String matching match for iptables
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08002 *
Pablo Neira Ayuso75676622005-08-21 23:30:34 -07003 * (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
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070024static bool match(const struct sk_buff *skb,
25 const struct net_device *in,
26 const struct net_device *out,
27 const struct xt_match *match,
28 const void *matchinfo,
29 int offset,
30 unsigned int protoff,
31 bool *hotdrop)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070032{
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
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080038 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
39 conf->to_offset, conf->config, &state)
Phil Oester97c802a2006-08-13 18:05:35 -070040 != UINT_MAX) ^ conf->invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070041}
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
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070045static bool checkentry(const char *tablename,
46 const void *ip,
47 const struct xt_match *match,
48 void *matchinfo,
49 unsigned int hook_mask)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070050{
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 struct xt_string_info *conf = matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070052 struct ts_config *ts_conf;
53
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070054 /* Damn, can't handle this case properly with iptables... */
55 if (conf->from_offset > conf->to_offset)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070056 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070057 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070058 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070059 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070060 return false;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070061 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
62 GFP_KERNEL, TS_AUTOLOAD);
63 if (IS_ERR(ts_conf))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070064 return false;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070065
66 conf->config = ts_conf;
67
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070068 return true;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070069}
70
Patrick McHardyefa74162006-08-22 00:36:37 -070071static void destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070072{
73 textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
74}
75
Patrick McHardy9f15c532007-07-07 22:22:02 -070076static struct xt_match xt_string_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070077 {
78 .name = "string",
79 .family = AF_INET,
80 .checkentry = checkentry,
81 .match = match,
82 .destroy = destroy,
83 .matchsize = sizeof(struct xt_string_info),
84 .me = THIS_MODULE
85 },
86 {
87 .name = "string",
88 .family = AF_INET6,
89 .checkentry = checkentry,
90 .match = match,
91 .destroy = destroy,
92 .matchsize = sizeof(struct xt_string_info),
93 .me = THIS_MODULE
94 },
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070095};
96
Andrew Morton65b4b4e2006-03-28 16:37:06 -080097static int __init xt_string_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070098{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070099 return xt_register_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700100}
101
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800102static void __exit xt_string_fini(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700103{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700104 xt_unregister_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700105}
106
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800107module_init(xt_string_init);
108module_exit(xt_string_fini);