OSDN Git Service

1a1c1d17d85e0d0d2cef0010be5135601d34e870
[linux-kernel-docs/linux-2.6.git] / 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("IP tables string match module");
20 MODULE_LICENSE("GPL");
21 MODULE_ALIAS("ipt_string");
22 MODULE_ALIAS("ip6t_string");
23
24 static int match(const struct sk_buff *skb,
25                  const struct net_device *in,
26                  const struct net_device *out,
27                  const struct xt_match *match,
28                  const void *matchinfo,
29                  int offset,
30                  unsigned int protoff,
31                  int *hotdrop)
32 {
33         const struct xt_string_info *conf = matchinfo;
34         struct ts_state state;
35
36         memset(&state, 0, sizeof(struct ts_state));
37
38         return (skb_find_text((struct sk_buff *)skb, conf->from_offset, 
39                              conf->to_offset, conf->config, &state) 
40                              != UINT_MAX) ^ conf->invert;
41 }
42
43 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *) m)
44
45 static int checkentry(const char *tablename,
46                       const void *ip,
47                       const struct xt_match *match,
48                       void *matchinfo,
49                       unsigned int matchsize,
50                       unsigned int hook_mask)
51 {
52         struct xt_string_info *conf = matchinfo;
53         struct ts_config *ts_conf;
54
55         /* Damn, can't handle this case properly with iptables... */
56         if (conf->from_offset > conf->to_offset)
57                 return 0;
58         if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
59                 return 0;
60         if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
61                 return 0;
62         ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
63                                      GFP_KERNEL, TS_AUTOLOAD);
64         if (IS_ERR(ts_conf))
65                 return 0;
66
67         conf->config = ts_conf;
68
69         return 1;
70 }
71
72 static void destroy(const struct xt_match *match, void *matchinfo,
73                     unsigned int matchsize)
74 {
75         textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
76 }
77
78 static struct xt_match xt_string_match[] = {
79         {
80                 .name           = "string",
81                 .family         = AF_INET,
82                 .checkentry     = checkentry,
83                 .match          = match,
84                 .destroy        = destroy,
85                 .matchsize      = sizeof(struct xt_string_info),
86                 .me             = THIS_MODULE
87         },
88         {
89                 .name           = "string",
90                 .family         = AF_INET6,
91                 .checkentry     = checkentry,
92                 .match          = match,
93                 .destroy        = destroy,
94                 .matchsize      = sizeof(struct xt_string_info),
95                 .me             = THIS_MODULE
96         },
97 };
98
99 static int __init xt_string_init(void)
100 {
101         return xt_register_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
102 }
103
104 static void __exit xt_string_fini(void)
105 {
106         xt_unregister_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
107 }
108
109 module_init(xt_string_init);
110 module_exit(xt_string_fini);