Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 1 | /* String matching match for iptables |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 2 | * |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 19 | MODULE_DESCRIPTION("Xtables: string-based matching"); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 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 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 24 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 25 | string_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 26 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 27 | const struct xt_string_info *conf = par->matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 28 | struct ts_state state; |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 29 | int invert; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 30 | |
| 31 | memset(&state, 0, sizeof(struct ts_state)); |
| 32 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 33 | invert = (par->match->revision == 0 ? conf->u.v0.invert : |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 34 | conf->u.v1.flags & XT_STRING_FLAG_INVERT); |
| 35 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 36 | return (skb_find_text((struct sk_buff *)skb, conf->from_offset, |
| 37 | conf->to_offset, conf->config, &state) |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 38 | != UINT_MAX) ^ invert; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jan Engelhardt | e79ec50 | 2007-12-17 22:44:06 -0800 | [diff] [blame] | 41 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 42 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 43 | static int string_mt_check(const struct xt_mtchk_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 44 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 45 | struct xt_string_info *conf = par->matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 46 | struct ts_config *ts_conf; |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 47 | int flags = TS_AUTOLOAD; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 48 | |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 49 | /* Damn, can't handle this case properly with iptables... */ |
| 50 | if (conf->from_offset > conf->to_offset) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 51 | return -EINVAL; |
Patrick McHardy | 3ab7208 | 2006-07-31 23:47:31 -0700 | [diff] [blame] | 52 | if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 53 | return -EINVAL; |
Patrick McHardy | 3ab7208 | 2006-07-31 23:47:31 -0700 | [diff] [blame] | 54 | if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 55 | return -EINVAL; |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 56 | if (par->match->revision == 1) { |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 57 | if (conf->u.v1.flags & |
| 58 | ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 59 | return -EINVAL; |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 60 | if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE) |
| 61 | flags |= TS_IGNORECASE; |
| 62 | } |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 63 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 64 | GFP_KERNEL, flags); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 65 | if (IS_ERR(ts_conf)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 66 | return -EINVAL; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 67 | |
| 68 | conf->config = ts_conf; |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 69 | return 0; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 72 | static void string_mt_destroy(const struct xt_mtdtor_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 73 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 74 | textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 77 | static struct xt_match xt_string_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 78 | { |
| 79 | .name = "string", |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 80 | .revision = 0, |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 81 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 82 | .checkentry = string_mt_check, |
| 83 | .match = string_mt, |
| 84 | .destroy = string_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 85 | .matchsize = sizeof(struct xt_string_info), |
| 86 | .me = THIS_MODULE |
| 87 | }, |
| 88 | { |
| 89 | .name = "string", |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 90 | .revision = 1, |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 91 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 92 | .checkentry = string_mt_check, |
| 93 | .match = string_mt, |
| 94 | .destroy = string_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 95 | .matchsize = sizeof(struct xt_string_info), |
| 96 | .me = THIS_MODULE |
| 97 | }, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 100 | static int __init string_mt_init(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 101 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 102 | return xt_register_matches(xt_string_mt_reg, |
| 103 | ARRAY_SIZE(xt_string_mt_reg)); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 106 | static void __exit string_mt_exit(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 107 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 108 | xt_unregister_matches(xt_string_mt_reg, ARRAY_SIZE(xt_string_mt_reg)); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 111 | module_init(string_mt_init); |
| 112 | module_exit(string_mt_exit); |