-
Notifications
You must be signed in to change notification settings - Fork 1
/
xt_wildstring.c
174 lines (149 loc) · 4.85 KB
/
xt_wildstring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* wildstring matching match for iptables
*
* (C) 2005 Pablo Neira Ayuso (as xt_string) <pablo@eurodev.net>
* (C) 2013 Oleg Strizhechenko (add wildcard support) <oleg@carbonsoft.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_wildstring.h>
#include <linux/textsearch.h>
MODULE_AUTHOR("Oleg Strizhechenko <oleg@carbonsoft.ru>");
MODULE_DESCRIPTION("Xtables: wildstring-based matching");
MODULE_LICENSE("GPL");
static bool
wildstring_mt(const struct sk_buff *skb, const struct xt_match_param *par)
{
const struct xt_wildstring_info *conf = par->matchinfo;
struct ts_state state;
int invert;
unsigned int skb_find = 0;
memset(&state, 0, sizeof(struct ts_state));
invert = (par->match->revision == 0 ? conf->u.v0.invert :
conf->u.v1.flags & XT_WILDSTRING_FLAG_INVERT);
/* Eye burns, but this is example, so i willn't rewrite to list usage */
skb_find = skb_find_text((struct sk_buff *)skb, conf->from_offset,
conf->to_offset, conf->config_part1, &state);
if (skb_find == UINT_MAX)
return false;
if (!conf->pattern_part2)
return true;
memset(&state, 0, sizeof(struct ts_state));
skb_find = skb_find_text((struct sk_buff *)skb, skb_find,
conf->to_offset, conf->config_part2, &state);
if (skb_find == UINT_MAX)
return false;
if (!conf->pattern_part3)
return true;
memset(&state, 0, sizeof(struct ts_state));
skb_find = skb_find_text((struct sk_buff *)skb, skb_find,
conf->to_offset, conf->config_part3, &state);
if (skb_find == UINT_MAX)
return false;
return true;
}
#define WILDSTRING_TEXT_PRIV(m) ((struct xt_wildstring_info *)(m))
static bool wildstring_mt_check(const struct xt_mtchk_param *par)
{
struct xt_wildstring_info *conf = par->matchinfo;
struct ts_config *ts_conf;
int flags = TS_AUTOLOAD;
char *s;
char delim[2] = "*";
/* Damn, can't handle this case properly with iptables... */
if (conf->from_offset > conf->to_offset)
return false;
if (conf->algo[XT_WILDSTRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
return false;
if (conf->patlen > XT_WILDSTRING_MAX_PATTERN_SIZE)
return false;
if (par->match->revision == 1) {
if (conf->u.v1.flags &
~(XT_WILDSTRING_FLAG_IGNORECASE |
XT_WILDSTRING_FLAG_INVERT))
return false;
if (conf->u.v1.flags & XT_WILDSTRING_FLAG_IGNORECASE)
flags |= TS_IGNORECASE;
}
/* a new wild logic appears, maybe lists.. */
s = (char *)conf->pattern;
/* pattern1 */
conf->pattern_part1 = strsep(&s, delim);
if (!conf->pattern_part1)
return false;
conf->patlen_part1 = strlen(conf->pattern_part1);
ts_conf = textsearch_prepare(conf->algo, conf->pattern_part1,
conf->patlen_part1, GFP_KERNEL, flags);
if (IS_ERR(ts_conf))
return false;
conf->config_part1 = ts_conf;
/* pattern2 */
conf->pattern_part2 = strsep(&s, delim);
if (!conf->pattern_part2)
return true;
conf->patlen_part2 = strlen(conf->pattern_part2);
ts_conf = textsearch_prepare(conf->algo, conf->pattern_part2,
conf->patlen_part2, GFP_KERNEL, flags);
if (IS_ERR(ts_conf))
return false;
conf->config_part2 = ts_conf;
/* pattern3 */
conf->pattern_part3 = strsep(&s, delim);
if (!conf->pattern_part3)
return true;
conf->patlen_part3 = strlen(conf->pattern_part3);
ts_conf = textsearch_prepare(conf->algo, conf->pattern_part3,
conf->patlen_part3, GFP_KERNEL, flags);
if (IS_ERR(ts_conf))
return false;
conf->config_part3 = ts_conf;
return true;
}
static void wildstring_mt_destroy(const struct xt_mtdtor_param *par)
{
struct xt_wildstring_info *conf = WILDSTRING_TEXT_PRIV(par->matchinfo);
if (conf->pattern_part1)
textsearch_destroy(conf->config_part1);
if (conf->pattern_part2)
textsearch_destroy(conf->config_part2);
if (conf->pattern_part3)
textsearch_destroy(conf->config_part3);
}
static struct xt_match xt_wildstring_mt_reg[] __read_mostly = {
{
.name = "wildstring",
.revision = 0,
.family = NFPROTO_UNSPEC,
.checkentry = wildstring_mt_check,
.match = wildstring_mt,
.destroy = wildstring_mt_destroy,
.matchsize = sizeof(struct xt_wildstring_info),
.me = THIS_MODULE},
{
.name = "wildstring",
.revision = 1,
.family = NFPROTO_UNSPEC,
.checkentry = wildstring_mt_check,
.match = wildstring_mt,
.destroy = wildstring_mt_destroy,
.matchsize = sizeof(struct xt_wildstring_info),
.me = THIS_MODULE},
};
static int __init wildstring_mt_init(void)
{
return xt_register_matches(xt_wildstring_mt_reg,
ARRAY_SIZE(xt_wildstring_mt_reg));
}
static void __exit wildstring_mt_exit(void)
{
xt_unregister_matches(xt_wildstring_mt_reg,
ARRAY_SIZE(xt_wildstring_mt_reg));
}
module_init(wildstring_mt_init);
module_exit(wildstring_mt_exit);