blob: 0bc3460319c8bd565cc11564180d566f3f89488e [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;
Jan Engelhardtd879e192010-03-22 19:39:04 +010029 bool invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070030
Jan Engelhardtd879e192010-03-22 19:39:04 +010031 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070032
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080033 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
Bojan Prtvar059a2442015-02-22 11:46:35 +010034 conf->to_offset, conf->config)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070035 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070036}
37
Jan Engelhardte79ec502007-12-17 22:44:06 -080038#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070039
Jan Engelhardtb0f38452010-03-19 17:16:42 +010040static int string_mt_check(const struct xt_mtchk_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070041{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020042 struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070043 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070044 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070045
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070046 /* Damn, can't handle this case properly with iptables... */
47 if (conf->from_offset > conf->to_offset)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010048 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070049 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010050 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070051 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010052 return -EINVAL;
Jan Engelhardtd879e192010-03-22 19:39:04 +010053 if (conf->u.v1.flags &
54 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
55 return -EINVAL;
56 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
57 flags |= TS_IGNORECASE;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070058 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070059 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070060 if (IS_ERR(ts_conf))
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010061 return PTR_ERR(ts_conf);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070062
63 conf->config = ts_conf;
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010064 return 0;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070065}
66
Jan Engelhardt6be3d852008-10-08 11:35:19 +020067static void string_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070068{
Jan Engelhardt6be3d852008-10-08 11:35:19 +020069 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070070}
71
Jan Engelhardtd879e192010-03-22 19:39:04 +010072static struct xt_match xt_string_mt_reg __read_mostly = {
73 .name = "string",
74 .revision = 1,
75 .family = NFPROTO_UNSPEC,
76 .checkentry = string_mt_check,
77 .match = string_mt,
78 .destroy = string_mt_destroy,
79 .matchsize = sizeof(struct xt_string_info),
80 .me = THIS_MODULE,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070081};
82
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080083static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070084{
Jan Engelhardtd879e192010-03-22 19:39:04 +010085 return xt_register_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070086}
87
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080088static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070089{
Jan Engelhardtd879e192010-03-22 19:39:04 +010090 xt_unregister_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070091}
92
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080093module_init(string_mt_init);
94module_exit(string_mt_exit);