blob: 488e368a2c4e8cc9ed1ed005c8e1f51fb46bf384 [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>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080019MODULE_DESCRIPTION("Xtables: string-based matching");
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070020MODULE_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 Engelhardtd3c5ee62007-12-04 23:24:03 -080024static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020025string_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070026{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020027 const struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070028 struct ts_state state;
Jan Engelhardtd879e192010-03-22 19:39:04 +010029 bool invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070030
31 memset(&state, 0, sizeof(struct ts_state));
Jan Engelhardtd879e192010-03-22 19:39:04 +010032 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070033
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080034 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
35 conf->to_offset, conf->config, &state)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070036 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070037}
38
Jan Engelhardte79ec502007-12-17 22:44:06 -080039#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070040
Jan Engelhardtb0f38452010-03-19 17:16:42 +010041static int string_mt_check(const struct xt_mtchk_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070042{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020043 struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070044 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070045 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070046
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070047 /* Damn, can't handle this case properly with iptables... */
48 if (conf->from_offset > conf->to_offset)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010049 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070050 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010051 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070052 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010053 return -EINVAL;
Jan Engelhardtd879e192010-03-22 19:39:04 +010054 if (conf->u.v1.flags &
55 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
56 return -EINVAL;
57 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
58 flags |= TS_IGNORECASE;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070059 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070060 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070061 if (IS_ERR(ts_conf))
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010062 return PTR_ERR(ts_conf);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070063
64 conf->config = ts_conf;
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010065 return 0;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070066}
67
Jan Engelhardt6be3d852008-10-08 11:35:19 +020068static void string_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070069{
Jan Engelhardt6be3d852008-10-08 11:35:19 +020070 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070071}
72
Jan Engelhardtd879e192010-03-22 19:39:04 +010073static struct xt_match xt_string_mt_reg __read_mostly = {
74 .name = "string",
75 .revision = 1,
76 .family = NFPROTO_UNSPEC,
77 .checkentry = string_mt_check,
78 .match = string_mt,
79 .destroy = string_mt_destroy,
80 .matchsize = sizeof(struct xt_string_info),
81 .me = THIS_MODULE,
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070082};
83
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070085{
Jan Engelhardtd879e192010-03-22 19:39:04 +010086 return xt_register_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070087}
88
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080089static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070090{
Jan Engelhardtd879e192010-03-22 19:39:04 +010091 xt_unregister_match(&xt_string_mt_reg);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070092}
93
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080094module_init(string_mt_init);
95module_exit(string_mt_exit);