blob: e1f22a7a41528733b9e471bc3bef9d5bfeab19c4 [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;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070029 int invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070030
31 memset(&state, 0, sizeof(struct ts_state));
32
Jan Engelhardtf7108a22008-10-08 11:35:18 +020033 invert = (par->match->revision == 0 ? conf->u.v0.invert :
Joonwoo Park4ad3f262008-07-08 02:38:56 -070034 conf->u.v1.flags & XT_STRING_FLAG_INVERT);
35
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080036 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
37 conf->to_offset, conf->config, &state)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070038 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070039}
40
Jan Engelhardte79ec502007-12-17 22:44:06 -080041#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070042
Jan Engelhardtb0f38452010-03-19 17:16:42 +010043static int string_mt_check(const struct xt_mtchk_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070044{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020045 struct xt_string_info *conf = par->matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070046 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070047 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070048
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070049 /* Damn, can't handle this case properly with iptables... */
50 if (conf->from_offset > conf->to_offset)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010051 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070052 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010053 return -EINVAL;
Patrick McHardy3ab72082006-07-31 23:47:31 -070054 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010055 return -EINVAL;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020056 if (par->match->revision == 1) {
Joonwoo Park4ad3f262008-07-08 02:38:56 -070057 if (conf->u.v1.flags &
58 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010059 return -EINVAL;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070060 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
61 flags |= TS_IGNORECASE;
62 }
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070063 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070064 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070065 if (IS_ERR(ts_conf))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010066 return -EINVAL;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070067
68 conf->config = ts_conf;
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010069 return 0;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070070}
71
Jan Engelhardt6be3d852008-10-08 11:35:19 +020072static void string_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070073{
Jan Engelhardt6be3d852008-10-08 11:35:19 +020074 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070075}
76
Jan Engelhardt55b69e92008-10-08 11:35:01 +020077static struct xt_match xt_string_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070078 {
79 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070080 .revision = 0,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020081 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080082 .checkentry = string_mt_check,
83 .match = string_mt,
84 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070085 .matchsize = sizeof(struct xt_string_info),
86 .me = THIS_MODULE
87 },
88 {
89 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070090 .revision = 1,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020091 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080092 .checkentry = string_mt_check,
93 .match = string_mt,
94 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070095 .matchsize = sizeof(struct xt_string_info),
96 .me = THIS_MODULE
97 },
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070098};
99
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800100static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700101{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200102 return xt_register_matches(xt_string_mt_reg,
103 ARRAY_SIZE(xt_string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700104}
105
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800106static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700107{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200108 xt_unregister_matches(xt_string_mt_reg, ARRAY_SIZE(xt_string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700109}
110
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111module_init(string_mt_init);
112module_exit(string_mt_exit);