blob: d3c48b14ab94c09d74f0c74e62b94ed5fcb58af7 [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 Engelhardt62fc8052009-07-07 20:42:08 +020026string_mt(const struct sk_buff *skb, struct xt_action_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;
Jan Engelhardtd879e192010-03-22 19:39:04 +010030 bool invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070031
32 memset(&state, 0, sizeof(struct ts_state));
Jan Engelhardtd879e192010-03-22 19:39:04 +010033 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070034
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080035 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
36 conf->to_offset, conf->config, &state)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070037 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070038}
39
Jan Engelhardte79ec502007-12-17 22:44:06 -080040#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070041
Jan Engelhardtb0f38452010-03-19 17:16:42 +010042static int string_mt_check(const struct xt_mtchk_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070043{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020044 struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070045 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070046 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070047
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070048 /* Damn, can't handle this case properly with iptables... */
49 if (conf->from_offset > conf->to_offset)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010050 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070051 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010052 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070053 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010054 return -EINVAL;
Jan Engelhardtd879e192010-03-22 19:39:04 +010055 if (conf->u.v1.flags &
56 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
57 return -EINVAL;
58 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
59 flags |= TS_IGNORECASE;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070060 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070061 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070062 if (IS_ERR(ts_conf))
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010063 return PTR_ERR(ts_conf);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070064
65 conf->config = ts_conf;
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010066 return 0;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070067}
68
Jan Engelhardt6be3d852008-10-08 11:35:19 +020069static void string_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070070{
Jan Engelhardt6be3d852008-10-08 11:35:19 +020071 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070072}
73
Jan Engelhardtd879e192010-03-22 19:39:04 +010074static struct xt_match xt_string_mt_reg __read_mostly = {
75 .name = "string",
76 .revision = 1,
77 .family = NFPROTO_UNSPEC,
78 .checkentry = string_mt_check,
79 .match = string_mt,
80 .destroy = string_mt_destroy,
81 .matchsize = sizeof(struct xt_string_info),
82 .me = THIS_MODULE,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070083};
84
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070086{
Jan Engelhardtd879e192010-03-22 19:39:04 +010087 return xt_register_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070088}
89
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070091{
Jan Engelhardtd879e192010-03-22 19:39:04 +010092 xt_unregister_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070093}
94
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095module_init(string_mt_init);
96module_exit(string_mt_exit);