netfilter: xtables: change targets to return error code
[deliverable/linux.git] / net / netfilter / xt_string.c
CommitLineData
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
18MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
2ae15b64 19MODULE_DESCRIPTION("Xtables: string-based matching");
7567662b 20MODULE_LICENSE("GPL");
2e4e6a17
HW
21MODULE_ALIAS("ipt_string");
22MODULE_ALIAS("ip6t_string");
7567662b 23
d3c5ee6d 24static bool
f7108a20 25string_mt(const struct sk_buff *skb, const struct xt_match_param *par)
7567662b 26{
f7108a20 27 const struct xt_string_info *conf = par->matchinfo;
7567662b 28 struct ts_state state;
4ad3f261 29 int invert;
7567662b
PNA
30
31 memset(&state, 0, sizeof(struct ts_state));
32
f7108a20 33 invert = (par->match->revision == 0 ? conf->u.v0.invert :
4ad3f261
JP
34 conf->u.v1.flags & XT_STRING_FLAG_INVERT);
35
601e68e1
YH
36 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
37 conf->to_offset, conf->config, &state)
4ad3f261 38 != UINT_MAX) ^ invert;
7567662b
PNA
39}
40
e79ec50b 41#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
7567662b 42
b0f38452 43static int string_mt_check(const struct xt_mtchk_param *par)
7567662b 44{
9b4fce7a 45 struct xt_string_info *conf = par->matchinfo;
7567662b 46 struct ts_config *ts_conf;
4ad3f261 47 int flags = TS_AUTOLOAD;
7567662b 48
7567662b
PNA
49 /* Damn, can't handle this case properly with iptables... */
50 if (conf->from_offset > conf->to_offset)
bd414ee6 51 return -EINVAL;
3ab72088 52 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
bd414ee6 53 return -EINVAL;
3ab72088 54 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
bd414ee6 55 return -EINVAL;
9b4fce7a 56 if (par->match->revision == 1) {
4ad3f261
JP
57 if (conf->u.v1.flags &
58 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
bd414ee6 59 return -EINVAL;
4ad3f261
JP
60 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
61 flags |= TS_IGNORECASE;
62 }
7567662b 63 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
4ad3f261 64 GFP_KERNEL, flags);
7567662b 65 if (IS_ERR(ts_conf))
bd414ee6 66 return -EINVAL;
7567662b
PNA
67
68 conf->config = ts_conf;
bd414ee6 69 return 0;
7567662b
PNA
70}
71
6be3d859 72static void string_mt_destroy(const struct xt_mtdtor_param *par)
7567662b 73{
6be3d859 74 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
7567662b
PNA
75}
76
55b69e91 77static struct xt_match xt_string_mt_reg[] __read_mostly = {
4470bbc7
PM
78 {
79 .name = "string",
4ad3f261 80 .revision = 0,
55b69e91 81 .family = NFPROTO_UNSPEC,
4ad3f261
JP
82 .checkentry = string_mt_check,
83 .match = string_mt,
84 .destroy = string_mt_destroy,
85 .matchsize = sizeof(struct xt_string_info),
86 .me = THIS_MODULE
87 },
88 {
89 .name = "string",
90 .revision = 1,
55b69e91 91 .family = NFPROTO_UNSPEC,
d3c5ee6d
JE
92 .checkentry = string_mt_check,
93 .match = string_mt,
94 .destroy = string_mt_destroy,
4470bbc7
PM
95 .matchsize = sizeof(struct xt_string_info),
96 .me = THIS_MODULE
97 },
7567662b
PNA
98};
99
d3c5ee6d 100static int __init string_mt_init(void)
7567662b 101{
55b69e91
JE
102 return xt_register_matches(xt_string_mt_reg,
103 ARRAY_SIZE(xt_string_mt_reg));
7567662b
PNA
104}
105
d3c5ee6d 106static void __exit string_mt_exit(void)
7567662b 107{
55b69e91 108 xt_unregister_matches(xt_string_mt_reg, ARRAY_SIZE(xt_string_mt_reg));
7567662b
PNA
109}
110
d3c5ee6d
JE
111module_init(string_mt_init);
112module_exit(string_mt_exit);
This page took 0.446058 seconds and 5 git commands to generate.