blob: 4903182a062b251a43ae583526889aefb770b264 [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
25string_mt(const struct sk_buff *skb, const struct net_device *in,
26 const struct net_device *out, const struct xt_match *match,
27 const void *matchinfo, int offset, unsigned int protoff,
28 bool *hotdrop)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070029{
Patrick McHardy3e72b2f2006-05-29 18:19:19 -070030 const struct xt_string_info *conf = matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070031 struct ts_state state;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070032 int invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070033
34 memset(&state, 0, sizeof(struct ts_state));
35
Joonwoo Park4ad3f262008-07-08 02:38:56 -070036 invert = (match->revision == 0 ? conf->u.v0.invert :
37 conf->u.v1.flags & XT_STRING_FLAG_INVERT);
38
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080039 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
40 conf->to_offset, conf->config, &state)
Joonwoo Park4ad3f262008-07-08 02:38:56 -070041 != UINT_MAX) ^ invert;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070042}
43
Jan Engelhardte79ec502007-12-17 22:44:06 -080044#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070045
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080046static bool
47string_mt_check(const char *tablename, const void *ip,
48 const struct xt_match *match, void *matchinfo,
49 unsigned int hook_mask)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070050{
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 struct xt_string_info *conf = matchinfo;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070052 struct ts_config *ts_conf;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070053 int flags = TS_AUTOLOAD;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070054
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070055 /* Damn, can't handle this case properly with iptables... */
56 if (conf->from_offset > conf->to_offset)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070057 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070058 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070059 return false;
Patrick McHardy3ab72082006-07-31 23:47:31 -070060 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070061 return false;
Joonwoo Park4ad3f262008-07-08 02:38:56 -070062 if (match->revision == 1) {
63 if (conf->u.v1.flags &
64 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
65 return false;
66 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
67 flags |= TS_IGNORECASE;
68 }
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070069 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
Joonwoo Park4ad3f262008-07-08 02:38:56 -070070 GFP_KERNEL, flags);
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070071 if (IS_ERR(ts_conf))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070072 return false;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070073
74 conf->config = ts_conf;
75
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070076 return true;
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070077}
78
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080079static void string_mt_destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -070080{
81 textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
82}
83
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084static struct xt_match string_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070085 {
86 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070087 .revision = 0,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070088 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080089 .checkentry = string_mt_check,
90 .match = string_mt,
91 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070092 .matchsize = sizeof(struct xt_string_info),
93 .me = THIS_MODULE
94 },
95 {
96 .name = "string",
Joonwoo Park4ad3f262008-07-08 02:38:56 -070097 .revision = 1,
98 .family = AF_INET,
99 .checkentry = string_mt_check,
100 .match = string_mt,
101 .destroy = string_mt_destroy,
102 .matchsize = sizeof(struct xt_string_info),
103 .me = THIS_MODULE
104 },
105 {
106 .name = "string",
107 .revision = 0,
108 .family = AF_INET6,
109 .checkentry = string_mt_check,
110 .match = string_mt,
111 .destroy = string_mt_destroy,
112 .matchsize = sizeof(struct xt_string_info),
113 .me = THIS_MODULE
114 },
115 {
116 .name = "string",
117 .revision = 1,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700118 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800119 .checkentry = string_mt_check,
120 .match = string_mt,
121 .destroy = string_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700122 .matchsize = sizeof(struct xt_string_info),
123 .me = THIS_MODULE
124 },
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700125};
126
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127static int __init string_mt_init(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700128{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800129 return xt_register_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700130}
131
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132static void __exit string_mt_exit(void)
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700133{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134 xt_unregister_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg));
Pablo Neira Ayuso75676622005-08-21 23:30:34 -0700135}
136
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800137module_init(string_mt_init);
138module_exit(string_mt_exit);