Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 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> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 14 | #include <linux/netfilter/x_tables.h> |
| 15 | #include <linux/netfilter/xt_string.h> |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 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"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 21 | MODULE_ALIAS("ipt_string"); |
| 22 | MODULE_ALIAS("ip6t_string"); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 23 | |
| 24 | static int match(const struct sk_buff *skb, |
| 25 | const struct net_device *in, |
| 26 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 27 | const struct xt_match *match, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 28 | const void *matchinfo, |
| 29 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 30 | unsigned int protoff, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 31 | int *hotdrop) |
| 32 | { |
Patrick McHardy | 3e72b2f | 2006-05-29 18:19:19 -0700 | [diff] [blame] | 33 | const struct xt_string_info *conf = matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 34 | struct ts_state state; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 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 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *) m) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 44 | |
| 45 | static int checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 46 | const void *ip, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 47 | const struct xt_match *match, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 48 | void *matchinfo, |
| 49 | unsigned int matchsize, |
| 50 | unsigned int hook_mask) |
| 51 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 52 | struct xt_string_info *conf = matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 53 | struct ts_config *ts_conf; |
| 54 | |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 55 | /* Damn, can't handle this case properly with iptables... */ |
| 56 | if (conf->from_offset > conf->to_offset) |
| 57 | return 0; |
| 58 | |
| 59 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
| 60 | GFP_KERNEL, TS_AUTOLOAD); |
| 61 | if (IS_ERR(ts_conf)) |
| 62 | return 0; |
| 63 | |
| 64 | conf->config = ts_conf; |
| 65 | |
| 66 | return 1; |
| 67 | } |
| 68 | |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 69 | static void destroy(const struct xt_match *match, void *matchinfo, |
| 70 | unsigned int matchsize) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 71 | { |
| 72 | textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config); |
| 73 | } |
| 74 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | static struct xt_match string_match = { |
| 76 | .name = "string", |
| 77 | .match = match, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 78 | .matchsize = sizeof(struct xt_string_info), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | .checkentry = checkentry, |
| 80 | .destroy = destroy, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 81 | .family = AF_INET, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 82 | .me = THIS_MODULE |
| 83 | }; |
| 84 | static struct xt_match string6_match = { |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 85 | .name = "string", |
| 86 | .match = match, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 87 | .matchsize = sizeof(struct xt_string_info), |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 88 | .checkentry = checkentry, |
| 89 | .destroy = destroy, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 90 | .family = AF_INET6, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 91 | .me = THIS_MODULE |
| 92 | }; |
| 93 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 94 | static int __init xt_string_init(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 95 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | int ret; |
| 97 | |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 98 | ret = xt_register_match(&string_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 99 | if (ret) |
| 100 | return ret; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 101 | ret = xt_register_match(&string6_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 102 | if (ret) |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 103 | xt_unregister_match(&string_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 104 | |
| 105 | return ret; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 108 | static void __exit xt_string_fini(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 109 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 110 | xt_unregister_match(&string_match); |
| 111 | xt_unregister_match(&string6_match); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 114 | module_init(xt_string_init); |
| 115 | module_exit(xt_string_fini); |