Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * (C) 2007 Patrick McHardy <kaber@trash.net>
0004  */
0005 #include <linux/module.h>
0006 #include <linux/skbuff.h>
0007 #include <linux/gen_stats.h>
0008 
0009 #include <linux/netfilter/x_tables.h>
0010 #include <linux/netfilter/xt_rateest.h>
0011 #include <net/netfilter/xt_rateest.h>
0012 
0013 
0014 static bool
0015 xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par)
0016 {
0017     const struct xt_rateest_match_info *info = par->matchinfo;
0018     struct gnet_stats_rate_est64 sample = {0};
0019     u_int32_t bps1, bps2, pps1, pps2;
0020     bool ret = true;
0021 
0022     gen_estimator_read(&info->est1->rate_est, &sample);
0023 
0024     if (info->flags & XT_RATEEST_MATCH_DELTA) {
0025         bps1 = info->bps1 >= sample.bps ? info->bps1 - sample.bps : 0;
0026         pps1 = info->pps1 >= sample.pps ? info->pps1 - sample.pps : 0;
0027     } else {
0028         bps1 = sample.bps;
0029         pps1 = sample.pps;
0030     }
0031 
0032     if (info->flags & XT_RATEEST_MATCH_ABS) {
0033         bps2 = info->bps2;
0034         pps2 = info->pps2;
0035     } else {
0036         gen_estimator_read(&info->est2->rate_est, &sample);
0037 
0038         if (info->flags & XT_RATEEST_MATCH_DELTA) {
0039             bps2 = info->bps2 >= sample.bps ? info->bps2 - sample.bps : 0;
0040             pps2 = info->pps2 >= sample.pps ? info->pps2 - sample.pps : 0;
0041         } else {
0042             bps2 = sample.bps;
0043             pps2 = sample.pps;
0044         }
0045     }
0046 
0047     switch (info->mode) {
0048     case XT_RATEEST_MATCH_LT:
0049         if (info->flags & XT_RATEEST_MATCH_BPS)
0050             ret &= bps1 < bps2;
0051         if (info->flags & XT_RATEEST_MATCH_PPS)
0052             ret &= pps1 < pps2;
0053         break;
0054     case XT_RATEEST_MATCH_GT:
0055         if (info->flags & XT_RATEEST_MATCH_BPS)
0056             ret &= bps1 > bps2;
0057         if (info->flags & XT_RATEEST_MATCH_PPS)
0058             ret &= pps1 > pps2;
0059         break;
0060     case XT_RATEEST_MATCH_EQ:
0061         if (info->flags & XT_RATEEST_MATCH_BPS)
0062             ret &= bps1 == bps2;
0063         if (info->flags & XT_RATEEST_MATCH_PPS)
0064             ret &= pps1 == pps2;
0065         break;
0066     }
0067 
0068     ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
0069     return ret;
0070 }
0071 
0072 static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
0073 {
0074     struct xt_rateest_match_info *info = par->matchinfo;
0075     struct xt_rateest *est1, *est2;
0076     int ret = -EINVAL;
0077 
0078     if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
0079                      XT_RATEEST_MATCH_REL)) != 1)
0080         goto err1;
0081 
0082     if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
0083         goto err1;
0084 
0085     switch (info->mode) {
0086     case XT_RATEEST_MATCH_EQ:
0087     case XT_RATEEST_MATCH_LT:
0088     case XT_RATEEST_MATCH_GT:
0089         break;
0090     default:
0091         goto err1;
0092     }
0093 
0094     ret  = -ENOENT;
0095     est1 = xt_rateest_lookup(par->net, info->name1);
0096     if (!est1)
0097         goto err1;
0098 
0099     est2 = NULL;
0100     if (info->flags & XT_RATEEST_MATCH_REL) {
0101         est2 = xt_rateest_lookup(par->net, info->name2);
0102         if (!est2)
0103             goto err2;
0104     }
0105 
0106     info->est1 = est1;
0107     info->est2 = est2;
0108     return 0;
0109 
0110 err2:
0111     xt_rateest_put(par->net, est1);
0112 err1:
0113     return ret;
0114 }
0115 
0116 static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
0117 {
0118     struct xt_rateest_match_info *info = par->matchinfo;
0119 
0120     xt_rateest_put(par->net, info->est1);
0121     if (info->est2)
0122         xt_rateest_put(par->net, info->est2);
0123 }
0124 
0125 static struct xt_match xt_rateest_mt_reg __read_mostly = {
0126     .name       = "rateest",
0127     .revision   = 0,
0128     .family     = NFPROTO_UNSPEC,
0129     .match      = xt_rateest_mt,
0130     .checkentry = xt_rateest_mt_checkentry,
0131     .destroy    = xt_rateest_mt_destroy,
0132     .matchsize  = sizeof(struct xt_rateest_match_info),
0133     .usersize   = offsetof(struct xt_rateest_match_info, est1),
0134     .me         = THIS_MODULE,
0135 };
0136 
0137 static int __init xt_rateest_mt_init(void)
0138 {
0139     return xt_register_match(&xt_rateest_mt_reg);
0140 }
0141 
0142 static void __exit xt_rateest_mt_fini(void)
0143 {
0144     xt_unregister_match(&xt_rateest_mt_reg);
0145 }
0146 
0147 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
0148 MODULE_LICENSE("GPL");
0149 MODULE_DESCRIPTION("xtables rate estimator match");
0150 MODULE_ALIAS("ipt_rateest");
0151 MODULE_ALIAS("ip6t_rateest");
0152 module_init(xt_rateest_mt_init);
0153 module_exit(xt_rateest_mt_fini);