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/gfp.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/skbuff.h> 15 #include <linux/netfilter/x_tables.h> 16 #include <linux/netfilter/xt_string.h> 17 #include <linux/textsearch.h> 18 19 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>"); 20 MODULE_DESCRIPTION("Xtables: string-based matching"); 21 MODULE_LICENSE("GPL"); 22 MODULE_ALIAS("ipt_string"); 23 MODULE_ALIAS("ip6t_string"); 24 MODULE_ALIAS("ebt_string"); 25 26 static bool 27 string_mt(const struct sk_buff *skb, struct xt_action_param *par) 28 { 29 const struct xt_string_info *conf = par->matchinfo; 30 bool invert; 31 32 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT; 33 34 return (skb_find_text((struct sk_buff *)skb, conf->from_offset, 35 conf->to_offset, conf->config) 36 != UINT_MAX) ^ invert; 37 } 38 39 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m)) 40 41 static int string_mt_check(const struct xt_mtchk_param *par) 42 { 43 struct xt_string_info *conf = par->matchinfo; 44 struct ts_config *ts_conf; 45 int flags = TS_AUTOLOAD; 46 47 /* Damn, can't handle this case properly with iptables... */ 48 if (conf->from_offset > conf->to_offset) 49 return -EINVAL; 50 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0') 51 return -EINVAL; 52 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE) 53 return -EINVAL; 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; 59 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen, 60 GFP_KERNEL, flags); 61 if (IS_ERR(ts_conf)) 62 return PTR_ERR(ts_conf); 63 64 conf->config = ts_conf; 65 return 0; 66 } 67 68 static void string_mt_destroy(const struct xt_mtdtor_param *par) 69 { 70 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config); 71 } 72 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), 81 .usersize = offsetof(struct xt_string_info, config), 82 .me = THIS_MODULE, 83 }; 84 85 static int __init string_mt_init(void) 86 { 87 return xt_register_match(&xt_string_mt_reg); 88 } 89 90 static void __exit string_mt_exit(void) 91 { 92 xt_unregister_match(&xt_string_mt_reg); 93 } 94 95 module_init(string_mt_init); 96 module_exit(string_mt_exit); 97