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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/skbuff.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 15 | #include <linux/netfilter/x_tables.h> |
| 16 | #include <linux/netfilter/xt_string.h> |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 17 | #include <linux/textsearch.h> |
| 18 | |
| 19 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 20 | MODULE_DESCRIPTION("Xtables: string-based matching"); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 21 | MODULE_LICENSE("GPL"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 22 | MODULE_ALIAS("ipt_string"); |
| 23 | MODULE_ALIAS("ip6t_string"); |
Bernie Harris | 1be3ac9 | 2018-03-21 15:42:16 +1300 | [diff] [blame] | 24 | MODULE_ALIAS("ebt_string"); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 25 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 26 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 27 | string_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 28 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 29 | const struct xt_string_info *conf = par->matchinfo; |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 30 | bool invert; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 31 | |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 32 | invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT; |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 33 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 34 | return (skb_find_text((struct sk_buff *)skb, conf->from_offset, |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame] | 35 | conf->to_offset, conf->config) |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 36 | != UINT_MAX) ^ invert; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jan Engelhardt | e79ec50 | 2007-12-17 22:44:06 -0800 | [diff] [blame] | 39 | #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 40 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 41 | static int string_mt_check(const struct xt_mtchk_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 42 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 43 | struct xt_string_info *conf = par->matchinfo; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 44 | struct ts_config *ts_conf; |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 45 | int flags = TS_AUTOLOAD; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 46 | |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 47 | /* Damn, can't handle this case properly with iptables... */ |
| 48 | if (conf->from_offset > conf->to_offset) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 49 | return -EINVAL; |
Patrick McHardy | 3ab7208 | 2006-07-31 23:47:31 -0700 | [diff] [blame] | 50 | if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') |
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->patlen > XT_STRING_MAX_PATTERN_SIZE) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 53 | return -EINVAL; |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 54 | if (conf->u.v1.flags & |
| 55 | ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT)) |
| 56 | return -EINVAL; |
| 57 | if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE) |
| 58 | flags |= TS_IGNORECASE; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 59 | ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, |
Joonwoo Park | 4ad3f26 | 2008-07-08 02:38:56 -0700 | [diff] [blame] | 60 | GFP_KERNEL, flags); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 61 | if (IS_ERR(ts_conf)) |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 62 | return PTR_ERR(ts_conf); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 63 | |
| 64 | conf->config = ts_conf; |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 65 | return 0; |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 68 | static void string_mt_destroy(const struct xt_mtdtor_param *par) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 69 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 70 | textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 73 | static struct xt_match xt_string_mt_reg __read_mostly = { |
| 74 | .name = "string", |
| 75 | .revision = 1, |
| 76 | .family = NFPROTO_UNSPEC, |
| 77 | .checkentry = string_mt_check, |
| 78 | .match = string_mt, |
| 79 | .destroy = string_mt_destroy, |
| 80 | .matchsize = sizeof(struct xt_string_info), |
Willem de Bruijn | ec23189 | 2017-01-02 17:19:46 -0500 | [diff] [blame] | 81 | .usersize = offsetof(struct xt_string_info, config), |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 82 | .me = THIS_MODULE, |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 85 | static int __init string_mt_init(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 86 | { |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 87 | return xt_register_match(&xt_string_mt_reg); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 90 | static void __exit string_mt_exit(void) |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 91 | { |
Jan Engelhardt | d879e19 | 2010-03-22 19:39:04 +0100 | [diff] [blame] | 92 | xt_unregister_match(&xt_string_mt_reg); |
Pablo Neira Ayuso | 7567662 | 2005-08-21 23:30:34 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 95 | module_init(string_mt_init); |
| 96 | module_exit(string_mt_exit); |