1 /* String matching match for iptables
3 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
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.
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>
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");
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
,
30 const struct xt_string_info
*conf
= matchinfo
;
31 struct ts_state state
;
33 memset(&state
, 0, sizeof(struct ts_state
));
35 return (skb_find_text((struct sk_buff
*)skb
, conf
->from_offset
,
36 conf
->to_offset
, conf
->config
, &state
)
37 != UINT_MAX
) ^ conf
->invert
;
40 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
43 string_mt_check(const char *tablename
, const void *ip
,
44 const struct xt_match
*match
, void *matchinfo
,
45 unsigned int hook_mask
)
47 struct xt_string_info
*conf
= matchinfo
;
48 struct ts_config
*ts_conf
;
50 /* Damn, can't handle this case properly with iptables... */
51 if (conf
->from_offset
> conf
->to_offset
)
53 if (conf
->algo
[XT_STRING_MAX_ALGO_NAME_SIZE
- 1] != '\0')
55 if (conf
->patlen
> XT_STRING_MAX_PATTERN_SIZE
)
57 ts_conf
= textsearch_prepare(conf
->algo
, conf
->pattern
, conf
->patlen
,
58 GFP_KERNEL
, TS_AUTOLOAD
);
62 conf
->config
= ts_conf
;
67 static void string_mt_destroy(const struct xt_match
*match
, void *matchinfo
)
69 textsearch_destroy(STRING_TEXT_PRIV(matchinfo
)->config
);
72 static struct xt_match string_mt_reg
[] __read_mostly
= {
76 .checkentry
= string_mt_check
,
78 .destroy
= string_mt_destroy
,
79 .matchsize
= sizeof(struct xt_string_info
),
85 .checkentry
= string_mt_check
,
87 .destroy
= string_mt_destroy
,
88 .matchsize
= sizeof(struct xt_string_info
),
93 static int __init
string_mt_init(void)
95 return xt_register_matches(string_mt_reg
, ARRAY_SIZE(string_mt_reg
));
98 static void __exit
string_mt_exit(void)
100 xt_unregister_matches(string_mt_reg
, ARRAY_SIZE(string_mt_reg
));
103 module_init(string_mt_init
);
104 module_exit(string_mt_exit
);