1 /* String matching match for iptables
3 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
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.
10 #include <linux/gfp.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter/xt_string.h>
17 #include <linux/textsearch.h>
19 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
20 MODULE_DESCRIPTION("Xtables: string-based matching");
21 MODULE_LICENSE("GPL");
22 MODULE_ALIAS("ipt_string");
23 MODULE_ALIAS("ip6t_string");
26 string_mt(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
28 const struct xt_string_info
*conf
= par
->matchinfo
;
29 struct ts_state state
;
32 memset(&state
, 0, sizeof(struct ts_state
));
34 invert
= (par
->match
->revision
== 0 ? conf
->u
.v0
.invert
:
35 conf
->u
.v1
.flags
& XT_STRING_FLAG_INVERT
);
37 return (skb_find_text((struct sk_buff
*)skb
, conf
->from_offset
,
38 conf
->to_offset
, conf
->config
, &state
)
39 != UINT_MAX
) ^ invert
;
42 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
44 static bool string_mt_check(const struct xt_mtchk_param
*par
)
46 struct xt_string_info
*conf
= par
->matchinfo
;
47 struct ts_config
*ts_conf
;
48 int flags
= TS_AUTOLOAD
;
50 /* Damn, can't handle this case properly with iptables... */
51 if (conf
->from_offset
> conf
->to_offset
)
53 if (conf
->algo
[XT_STRING_MAX_ALGO_NAME_SIZE
- 1] != '\0')
55 if (conf
->patlen
> XT_STRING_MAX_PATTERN_SIZE
)
57 if (par
->match
->revision
== 1) {
58 if (conf
->u
.v1
.flags
&
59 ~(XT_STRING_FLAG_IGNORECASE
| XT_STRING_FLAG_INVERT
))
61 if (conf
->u
.v1
.flags
& XT_STRING_FLAG_IGNORECASE
)
62 flags
|= TS_IGNORECASE
;
64 ts_conf
= textsearch_prepare(conf
->algo
, conf
->pattern
, conf
->patlen
,
69 conf
->config
= ts_conf
;
74 static void string_mt_destroy(const struct xt_mtdtor_param
*par
)
76 textsearch_destroy(STRING_TEXT_PRIV(par
->matchinfo
)->config
);
79 static struct xt_match xt_string_mt_reg
[] __read_mostly
= {
83 .family
= NFPROTO_UNSPEC
,
84 .checkentry
= string_mt_check
,
86 .destroy
= string_mt_destroy
,
87 .matchsize
= sizeof(struct xt_string_info
),
93 .family
= NFPROTO_UNSPEC
,
94 .checkentry
= string_mt_check
,
96 .destroy
= string_mt_destroy
,
97 .matchsize
= sizeof(struct xt_string_info
),
102 static int __init
string_mt_init(void)
104 return xt_register_matches(xt_string_mt_reg
,
105 ARRAY_SIZE(xt_string_mt_reg
));
108 static void __exit
string_mt_exit(void)
110 xt_unregister_matches(xt_string_mt_reg
, ARRAY_SIZE(xt_string_mt_reg
));
113 module_init(string_mt_init
);
114 module_exit(string_mt_exit
);