blob: 96801ffd8af8d79c33e2d30fdd62c39ade795265 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070011#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080015#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter/xt_string.h>
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070017#include <linux/textsearch.h>
18
19MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080020MODULE_DESCRIPTION("Xtables: string-based matching");
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070021MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080022MODULE_ALIAS("ipt_string");
23MODULE_ALIAS("ip6t_string");
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070024
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080025static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020026string_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070027{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020028 const struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070029 struct ts_state state;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070030 int invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070031
32 memset(&state, 0, sizeof(struct ts_state));
33
Jan Engelhardtf7108a22008-10-08 11:35:18 +020034 invert = (par->match->revision == 0 ? conf->u.v0.invert :
Joonwoo Park4ad3f262008-07-08 02:38:56 -070035 conf->u.v1.flags & XT_STRING_FLAG_INVERT);
36
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080037 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
38 conf->to_offset, conf->config, &state)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070039 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070040}
41
Jan Engelhardte79ec502007-12-17 22:44:06 -080042#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070043
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020044static bool string_mt_check(const struct xt_mtchk_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070045{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020046 struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070047 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070048 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070049
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070050 /* Damn, can't handle this case properly with iptables... */
51 if (conf->from_offset > conf->to_offset)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070052 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070053 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070054 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070055 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070056 return false;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020057 if (par->match->revision == 1) {
Joonwoo Park4ad3f262008-07-08 02:38:56 -070058 if (conf->u.v1.flags &
59 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
60 return false;
61 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
62 flags |= TS_IGNORECASE;
63 }
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070064 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070065 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070066 if (IS_ERR(ts_conf))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070067 return false;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070068
69 conf->config = ts_conf;
70
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070071 return true;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070072}
73
Jan Engelhardt6be3d852008-10-08 11:35:19 +020074static void string_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070075{
Jan Engelhardt6be3d852008-10-08 11:35:19 +020076 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070077}
78
Jan Engelhardt55b69e92008-10-08 11:35:01 +020079static struct xt_match xt_string_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070080 {
81 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070082 .revision = 0,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020083 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084 .checkentry = string_mt_check,
85 .match = string_mt,
86 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070087 .matchsize = sizeof(struct xt_string_info),
88 .me = THIS_MODULE
89 },
90 {
91 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070092 .revision = 1,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020093 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080094 .checkentry = string_mt_check,
95 .match = string_mt,
96 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070097 .matchsize = sizeof(struct xt_string_info),
98 .me = THIS_MODULE
99 },
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700100};
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700103{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200104 return xt_register_matches(xt_string_mt_reg,
105 ARRAY_SIZE(xt_string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700106}
107
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800108static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700109{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200110 xt_unregister_matches(xt_string_mt_reg, ARRAY_SIZE(xt_string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700111}
112
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800113module_init(string_mt_init);
114module_exit(string_mt_exit);