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 | ||
5a0e3ad6 | 10 | #include <linux/gfp.h> |
7567662b PNA |
11 | #include <linux/init.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/skbuff.h> | |
2e4e6a17 HW |
15 | #include <linux/netfilter/x_tables.h> |
16 | #include <linux/netfilter/xt_string.h> | |
7567662b PNA |
17 | #include <linux/textsearch.h> |
18 | ||
19 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>"); | |
2ae15b64 | 20 | MODULE_DESCRIPTION("Xtables: string-based matching"); |
7567662b | 21 | MODULE_LICENSE("GPL"); |
2e4e6a17 HW |
22 | MODULE_ALIAS("ipt_string"); |
23 | MODULE_ALIAS("ip6t_string"); | |
7567662b | 24 | |
d3c5ee6d | 25 | static bool |
62fc8051 | 26 | string_mt(const struct sk_buff *skb, struct xt_action_param *par) |
7567662b | 27 | { |
f7108a20 | 28 | const struct xt_string_info *conf = par->matchinfo; |
7567662b | 29 | struct ts_state state; |
d879e19e | 30 | bool invert; |
7567662b PNA |
31 | |
32 | memset(&state, 0, sizeof(struct ts_state)); | |
d879e19e | 33 | invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT; |
4ad3f261 | 34 | |
601e68e1 YH |
35 | return (skb_find_text((struct sk_buff *)skb, conf->from_offset, |
36 | conf->to_offset, conf->config, &state) | |
4ad3f261 | 37 | != UINT_MAX) ^ invert; |
7567662b PNA |
38 | } |
39 | ||
e79ec50b | 40 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) |
7567662b | 41 | |
b0f38452 | 42 | static int string_mt_check(const struct xt_mtchk_param *par) |
7567662b | 43 | { |
9b4fce7a | 44 | struct xt_string_info *conf = par->matchinfo; |
7567662b | 45 | struct ts_config *ts_conf; |
4ad3f261 | 46 | int flags = TS_AUTOLOAD; |
7567662b | 47 | |
7567662b PNA |
48 | /* Damn, can't handle this case properly with iptables... */ |
49 | if (conf->from_offset > conf->to_offset) | |
bd414ee6 | 50 | return -EINVAL; |
3ab72088 | 51 | if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') |
bd414ee6 | 52 | return -EINVAL; |
3ab72088 | 53 | if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE) |
bd414ee6 | 54 | return -EINVAL; |
d879e19e JE |
55 | if (conf->u.v1.flags & |
56 | ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT)) | |
57 | return -EINVAL; | |
58 | if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE) | |
59 | flags |= TS_IGNORECASE; | |
7567662b | 60 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
4ad3f261 | 61 | GFP_KERNEL, flags); |
7567662b | 62 | if (IS_ERR(ts_conf)) |
4a5a5c73 | 63 | return PTR_ERR(ts_conf); |
7567662b PNA |
64 | |
65 | conf->config = ts_conf; | |
bd414ee6 | 66 | return 0; |
7567662b PNA |
67 | } |
68 | ||
6be3d859 | 69 | static void string_mt_destroy(const struct xt_mtdtor_param *par) |
7567662b | 70 | { |
6be3d859 | 71 | textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config); |
7567662b PNA |
72 | } |
73 | ||
d879e19e JE |
74 | static struct xt_match xt_string_mt_reg __read_mostly = { |
75 | .name = "string", | |
76 | .revision = 1, | |
77 | .family = NFPROTO_UNSPEC, | |
78 | .checkentry = string_mt_check, | |
79 | .match = string_mt, | |
80 | .destroy = string_mt_destroy, | |
81 | .matchsize = sizeof(struct xt_string_info), | |
82 | .me = THIS_MODULE, | |
7567662b PNA |
83 | }; |
84 | ||
d3c5ee6d | 85 | static int __init string_mt_init(void) |
7567662b | 86 | { |
d879e19e | 87 | return xt_register_match(&xt_string_mt_reg); |
7567662b PNA |
88 | } |
89 | ||
d3c5ee6d | 90 | static void __exit string_mt_exit(void) |
7567662b | 91 | { |
d879e19e | 92 | xt_unregister_match(&xt_string_mt_reg); |
7567662b PNA |
93 | } |
94 | ||
d3c5ee6d JE |
95 | module_init(string_mt_init); |
96 | module_exit(string_mt_exit); |