Commit | Line | Data |
---|---|---|
7567662b | 1 | /* String matching match for iptables |
601e68e1 | 2 | * |
7567662b PNA |
3 | * (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> | |
2e4e6a17 HW |
14 | #include <linux/netfilter/x_tables.h> |
15 | #include <linux/netfilter/xt_string.h> | |
7567662b PNA |
16 | #include <linux/textsearch.h> |
17 | ||
18 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>"); | |
2ae15b64 | 19 | MODULE_DESCRIPTION("Xtables: string-based matching"); |
7567662b | 20 | MODULE_LICENSE("GPL"); |
2e4e6a17 HW |
21 | MODULE_ALIAS("ipt_string"); |
22 | MODULE_ALIAS("ip6t_string"); | |
7567662b | 23 | |
d3c5ee6d JE |
24 | static bool |
25 | string_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) | |
7567662b | 29 | { |
3e72b2fe | 30 | const struct xt_string_info *conf = matchinfo; |
7567662b | 31 | struct ts_state state; |
4ad3f261 | 32 | int invert; |
7567662b PNA |
33 | |
34 | memset(&state, 0, sizeof(struct ts_state)); | |
35 | ||
4ad3f261 JP |
36 | invert = (match->revision == 0 ? conf->u.v0.invert : |
37 | conf->u.v1.flags & XT_STRING_FLAG_INVERT); | |
38 | ||
601e68e1 YH |
39 | return (skb_find_text((struct sk_buff *)skb, conf->from_offset, |
40 | conf->to_offset, conf->config, &state) | |
4ad3f261 | 41 | != UINT_MAX) ^ invert; |
7567662b PNA |
42 | } |
43 | ||
e79ec50b | 44 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) |
7567662b | 45 | |
d3c5ee6d JE |
46 | static bool |
47 | string_mt_check(const char *tablename, const void *ip, | |
48 | const struct xt_match *match, void *matchinfo, | |
49 | unsigned int hook_mask) | |
7567662b | 50 | { |
2e4e6a17 | 51 | struct xt_string_info *conf = matchinfo; |
7567662b | 52 | struct ts_config *ts_conf; |
4ad3f261 | 53 | int flags = TS_AUTOLOAD; |
7567662b | 54 | |
7567662b PNA |
55 | /* Damn, can't handle this case properly with iptables... */ |
56 | if (conf->from_offset > conf->to_offset) | |
ccb79bdc | 57 | return false; |
3ab72088 | 58 | if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') |
ccb79bdc | 59 | return false; |
3ab72088 | 60 | if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE) |
ccb79bdc | 61 | return false; |
4ad3f261 JP |
62 | 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 | } | |
7567662b | 69 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
4ad3f261 | 70 | GFP_KERNEL, flags); |
7567662b | 71 | if (IS_ERR(ts_conf)) |
ccb79bdc | 72 | return false; |
7567662b PNA |
73 | |
74 | conf->config = ts_conf; | |
75 | ||
ccb79bdc | 76 | return true; |
7567662b PNA |
77 | } |
78 | ||
d3c5ee6d | 79 | static void string_mt_destroy(const struct xt_match *match, void *matchinfo) |
7567662b PNA |
80 | { |
81 | textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config); | |
82 | } | |
83 | ||
55b69e91 | 84 | static struct xt_match xt_string_mt_reg[] __read_mostly = { |
4470bbc7 PM |
85 | { |
86 | .name = "string", | |
4ad3f261 | 87 | .revision = 0, |
55b69e91 | 88 | .family = NFPROTO_UNSPEC, |
4ad3f261 JP |
89 | .checkentry = string_mt_check, |
90 | .match = string_mt, | |
91 | .destroy = string_mt_destroy, | |
92 | .matchsize = sizeof(struct xt_string_info), | |
93 | .me = THIS_MODULE | |
94 | }, | |
95 | { | |
96 | .name = "string", | |
97 | .revision = 1, | |
55b69e91 | 98 | .family = NFPROTO_UNSPEC, |
d3c5ee6d JE |
99 | .checkentry = string_mt_check, |
100 | .match = string_mt, | |
101 | .destroy = string_mt_destroy, | |
4470bbc7 PM |
102 | .matchsize = sizeof(struct xt_string_info), |
103 | .me = THIS_MODULE | |
104 | }, | |
7567662b PNA |
105 | }; |
106 | ||
d3c5ee6d | 107 | static int __init string_mt_init(void) |
7567662b | 108 | { |
55b69e91 JE |
109 | return xt_register_matches(xt_string_mt_reg, |
110 | ARRAY_SIZE(xt_string_mt_reg)); | |
7567662b PNA |
111 | } |
112 | ||
d3c5ee6d | 113 | static void __exit string_mt_exit(void) |
7567662b | 114 | { |
55b69e91 | 115 | xt_unregister_matches(xt_string_mt_reg, ARRAY_SIZE(xt_string_mt_reg)); |
7567662b PNA |
116 | } |
117 | ||
d3c5ee6d JE |
118 | module_init(string_mt_init); |
119 | module_exit(string_mt_exit); |