xt_string.c 2.51 KB
Newer Older
1
/* String matching match for iptables
2
 *
3
4
5
6
7
8
9
 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

10
#include <linux/gfp.h>
11
12
13
14
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
15
16
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_string.h>
17
18
19
#include <linux/textsearch.h>

MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
20
MODULE_DESCRIPTION("Xtables: string-based matching");
21
MODULE_LICENSE("GPL");
22
23
MODULE_ALIAS("ipt_string");
MODULE_ALIAS("ip6t_string");
24

25
static bool
26
string_mt(const struct sk_buff *skb, struct xt_action_param *par)
27
{
28
	const struct xt_string_info *conf = par->matchinfo;
29
	bool invert;
30

31
	invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
32

33
	return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
34
			     conf->to_offset, conf->config)
35
			     != UINT_MAX) ^ invert;
36
37
}

38
#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
39

40
static int string_mt_check(const struct xt_mtchk_param *par)
41
{
42
	struct xt_string_info *conf = par->matchinfo;
43
	struct ts_config *ts_conf;
44
	int flags = TS_AUTOLOAD;
45
46
47

	/* Damn, can't handle this case properly with iptables... */
	if (conf->from_offset > conf->to_offset)
48
		return -EINVAL;
49
	if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
50
		return -EINVAL;
51
	if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
52
		return -EINVAL;
53
54
55
56
57
	if (conf->u.v1.flags &
	    ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
		return -EINVAL;
	if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
		flags |= TS_IGNORECASE;
58
	ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
59
				     GFP_KERNEL, flags);
60
	if (IS_ERR(ts_conf))
61
		return PTR_ERR(ts_conf);
62
63

	conf->config = ts_conf;
64
	return 0;
65
66
}

67
static void string_mt_destroy(const struct xt_mtdtor_param *par)
68
{
69
	textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
70
71
}

72
73
74
75
76
77
78
79
80
static struct xt_match xt_string_mt_reg __read_mostly = {
	.name       = "string",
	.revision   = 1,
	.family     = NFPROTO_UNSPEC,
	.checkentry = string_mt_check,
	.match      = string_mt,
	.destroy    = string_mt_destroy,
	.matchsize  = sizeof(struct xt_string_info),
	.me         = THIS_MODULE,
81
82
};

83
static int __init string_mt_init(void)
84
{
85
	return xt_register_match(&xt_string_mt_reg);
86
87
}

88
static void __exit string_mt_exit(void)
89
{
90
	xt_unregister_match(&xt_string_mt_reg);
91
92
}

93
94
module_init(string_mt_init);
module_exit(string_mt_exit);