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