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 |
| 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, |
| 28 | bool *hotdrop) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 29 | { |
Patrick McHardy | 3e72b2f | 2006-05-29 18:19:19 -0700 | [diff] [blame] | 30 | const struct xt_string_info *conf = matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 31 | struct ts_state state; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 32 | |
| 33 | memset(&state, 0, sizeof(struct ts_state)); |
| 34 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 35 | return (skb_find_text((struct sk_buff *)skb, conf->from_offset, |
| 36 | conf->to_offset, conf->config, &state) |
Phil Oester | 97c802a | 2006-08-13 18:05:35 -0700 | [diff] [blame] | 37 | != UINT_MAX) ^ conf->invert; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Jan Engelhardt | e79ec50 | 2007-12-17 22:44:06 -0800 | [diff] [blame] | 40 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 41 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 42 | static bool |
| 43 | string_mt_check(const char *tablename, const void *ip, |
| 44 | const struct xt_match *match, void *matchinfo, |
| 45 | unsigned int hook_mask) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 46 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | struct xt_string_info *conf = matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 48 | struct ts_config *ts_conf; |
| 49 | |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 50 | /* Damn, can't handle this case properly with iptables... */ |
| 51 | if (conf->from_offset > conf->to_offset) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 52 | return false; |
Patrick McHardy | 3ab7208 | 2006-07-31 23:47:31 -0700 | [diff] [blame] | 53 | if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 54 | return false; |
Patrick McHardy | 3ab7208 | 2006-07-31 23:47:31 -0700 | [diff] [blame] | 55 | if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 56 | return false; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 57 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
| 58 | GFP_KERNEL, TS_AUTOLOAD); |
| 59 | if (IS_ERR(ts_conf)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 60 | return false; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 61 | |
| 62 | conf->config = ts_conf; |
| 63 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 64 | return true; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 67 | static void string_mt_destroy(const struct xt_match *match, void *matchinfo) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 68 | { |
| 69 | textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config); |
| 70 | } |
| 71 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 72 | static struct xt_match string_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 73 | { |
| 74 | .name = "string", |
| 75 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 76 | .checkentry = string_mt_check, |
| 77 | .match = string_mt, |
| 78 | .destroy = string_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 79 | .matchsize = sizeof(struct xt_string_info), |
| 80 | .me = THIS_MODULE |
| 81 | }, |
| 82 | { |
| 83 | .name = "string", |
| 84 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 85 | .checkentry = string_mt_check, |
| 86 | .match = string_mt, |
| 87 | .destroy = string_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 88 | .matchsize = sizeof(struct xt_string_info), |
| 89 | .me = THIS_MODULE |
| 90 | }, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 93 | static int __init string_mt_init(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 94 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 95 | return xt_register_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg)); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 98 | static void __exit string_mt_exit(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 99 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 100 | xt_unregister_matches(string_mt_reg, ARRAY_SIZE(string_mt_reg)); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 103 | module_init(string_mt_init); |
| 104 | module_exit(string_mt_exit); |