Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* String matching match for iptables
0003  *
0004  * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
0005  */
0006 
0007 #include <linux/gfp.h>
0008 #include <linux/init.h>
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/netfilter/x_tables.h>
0013 #include <linux/netfilter/xt_string.h>
0014 #include <linux/textsearch.h>
0015 
0016 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
0017 MODULE_DESCRIPTION("Xtables: string-based matching");
0018 MODULE_LICENSE("GPL");
0019 MODULE_ALIAS("ipt_string");
0020 MODULE_ALIAS("ip6t_string");
0021 MODULE_ALIAS("ebt_string");
0022 
0023 static bool
0024 string_mt(const struct sk_buff *skb, struct xt_action_param *par)
0025 {
0026     const struct xt_string_info *conf = par->matchinfo;
0027     bool invert;
0028 
0029     invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
0030 
0031     return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
0032                  conf->to_offset, conf->config)
0033                  != UINT_MAX) ^ invert;
0034 }
0035 
0036 #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
0037 
0038 static int string_mt_check(const struct xt_mtchk_param *par)
0039 {
0040     struct xt_string_info *conf = par->matchinfo;
0041     struct ts_config *ts_conf;
0042     int flags = TS_AUTOLOAD;
0043 
0044     /* Damn, can't handle this case properly with iptables... */
0045     if (conf->from_offset > conf->to_offset)
0046         return -EINVAL;
0047     if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
0048         return -EINVAL;
0049     if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
0050         return -EINVAL;
0051     if (conf->u.v1.flags &
0052         ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
0053         return -EINVAL;
0054     if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
0055         flags |= TS_IGNORECASE;
0056     ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
0057                      GFP_KERNEL, flags);
0058     if (IS_ERR(ts_conf))
0059         return PTR_ERR(ts_conf);
0060 
0061     conf->config = ts_conf;
0062     return 0;
0063 }
0064 
0065 static void string_mt_destroy(const struct xt_mtdtor_param *par)
0066 {
0067     textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
0068 }
0069 
0070 static struct xt_match xt_string_mt_reg __read_mostly = {
0071     .name       = "string",
0072     .revision   = 1,
0073     .family     = NFPROTO_UNSPEC,
0074     .checkentry = string_mt_check,
0075     .match      = string_mt,
0076     .destroy    = string_mt_destroy,
0077     .matchsize  = sizeof(struct xt_string_info),
0078     .usersize   = offsetof(struct xt_string_info, config),
0079     .me         = THIS_MODULE,
0080 };
0081 
0082 static int __init string_mt_init(void)
0083 {
0084     return xt_register_match(&xt_string_mt_reg);
0085 }
0086 
0087 static void __exit string_mt_exit(void)
0088 {
0089     xt_unregister_match(&xt_string_mt_reg);
0090 }
0091 
0092 module_init(string_mt_init);
0093 module_exit(string_mt_exit);