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