0001
0002
0003
0004
0005 #include <linux/module.h>
0006 #include <linux/skbuff.h>
0007 #include <linux/gen_stats.h>
0008 #include <linux/jhash.h>
0009 #include <linux/rtnetlink.h>
0010 #include <linux/random.h>
0011 #include <linux/slab.h>
0012 #include <net/gen_stats.h>
0013 #include <net/netlink.h>
0014 #include <net/netns/generic.h>
0015
0016 #include <linux/netfilter/x_tables.h>
0017 #include <linux/netfilter/xt_RATEEST.h>
0018 #include <net/netfilter/xt_rateest.h>
0019
0020 #define RATEEST_HSIZE 16
0021
0022 struct xt_rateest_net {
0023 struct mutex hash_lock;
0024 struct hlist_head hash[RATEEST_HSIZE];
0025 };
0026
0027 static unsigned int xt_rateest_id;
0028
0029 static unsigned int jhash_rnd __read_mostly;
0030
0031 static unsigned int xt_rateest_hash(const char *name)
0032 {
0033 return jhash(name, sizeof_field(struct xt_rateest, name), jhash_rnd) &
0034 (RATEEST_HSIZE - 1);
0035 }
0036
0037 static void xt_rateest_hash_insert(struct xt_rateest_net *xn,
0038 struct xt_rateest *est)
0039 {
0040 unsigned int h;
0041
0042 h = xt_rateest_hash(est->name);
0043 hlist_add_head(&est->list, &xn->hash[h]);
0044 }
0045
0046 static struct xt_rateest *__xt_rateest_lookup(struct xt_rateest_net *xn,
0047 const char *name)
0048 {
0049 struct xt_rateest *est;
0050 unsigned int h;
0051
0052 h = xt_rateest_hash(name);
0053 hlist_for_each_entry(est, &xn->hash[h], list) {
0054 if (strcmp(est->name, name) == 0) {
0055 est->refcnt++;
0056 return est;
0057 }
0058 }
0059
0060 return NULL;
0061 }
0062
0063 struct xt_rateest *xt_rateest_lookup(struct net *net, const char *name)
0064 {
0065 struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
0066 struct xt_rateest *est;
0067
0068 mutex_lock(&xn->hash_lock);
0069 est = __xt_rateest_lookup(xn, name);
0070 mutex_unlock(&xn->hash_lock);
0071 return est;
0072 }
0073 EXPORT_SYMBOL_GPL(xt_rateest_lookup);
0074
0075 void xt_rateest_put(struct net *net, struct xt_rateest *est)
0076 {
0077 struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
0078
0079 mutex_lock(&xn->hash_lock);
0080 if (--est->refcnt == 0) {
0081 hlist_del(&est->list);
0082 gen_kill_estimator(&est->rate_est);
0083
0084
0085
0086
0087 kfree_rcu(est, rcu);
0088 }
0089 mutex_unlock(&xn->hash_lock);
0090 }
0091 EXPORT_SYMBOL_GPL(xt_rateest_put);
0092
0093 static unsigned int
0094 xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
0095 {
0096 const struct xt_rateest_target_info *info = par->targinfo;
0097 struct gnet_stats_basic_sync *stats = &info->est->bstats;
0098
0099 spin_lock_bh(&info->est->lock);
0100 u64_stats_add(&stats->bytes, skb->len);
0101 u64_stats_inc(&stats->packets);
0102 spin_unlock_bh(&info->est->lock);
0103
0104 return XT_CONTINUE;
0105 }
0106
0107 static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
0108 {
0109 struct xt_rateest_net *xn = net_generic(par->net, xt_rateest_id);
0110 struct xt_rateest_target_info *info = par->targinfo;
0111 struct xt_rateest *est;
0112 struct {
0113 struct nlattr opt;
0114 struct gnet_estimator est;
0115 } cfg;
0116 int ret;
0117
0118 if (strnlen(info->name, sizeof(est->name)) >= sizeof(est->name))
0119 return -ENAMETOOLONG;
0120
0121 net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
0122
0123 mutex_lock(&xn->hash_lock);
0124 est = __xt_rateest_lookup(xn, info->name);
0125 if (est) {
0126 mutex_unlock(&xn->hash_lock);
0127
0128
0129
0130
0131 if ((!info->interval && !info->ewma_log) ||
0132 (info->interval != est->params.interval ||
0133 info->ewma_log != est->params.ewma_log)) {
0134 xt_rateest_put(par->net, est);
0135 return -EINVAL;
0136 }
0137 info->est = est;
0138 return 0;
0139 }
0140
0141 ret = -ENOMEM;
0142 est = kzalloc(sizeof(*est), GFP_KERNEL);
0143 if (!est)
0144 goto err1;
0145
0146 gnet_stats_basic_sync_init(&est->bstats);
0147 strlcpy(est->name, info->name, sizeof(est->name));
0148 spin_lock_init(&est->lock);
0149 est->refcnt = 1;
0150 est->params.interval = info->interval;
0151 est->params.ewma_log = info->ewma_log;
0152
0153 cfg.opt.nla_len = nla_attr_size(sizeof(cfg.est));
0154 cfg.opt.nla_type = TCA_STATS_RATE_EST;
0155 cfg.est.interval = info->interval;
0156 cfg.est.ewma_log = info->ewma_log;
0157
0158 ret = gen_new_estimator(&est->bstats, NULL, &est->rate_est,
0159 &est->lock, NULL, &cfg.opt);
0160 if (ret < 0)
0161 goto err2;
0162
0163 info->est = est;
0164 xt_rateest_hash_insert(xn, est);
0165 mutex_unlock(&xn->hash_lock);
0166 return 0;
0167
0168 err2:
0169 kfree(est);
0170 err1:
0171 mutex_unlock(&xn->hash_lock);
0172 return ret;
0173 }
0174
0175 static void xt_rateest_tg_destroy(const struct xt_tgdtor_param *par)
0176 {
0177 struct xt_rateest_target_info *info = par->targinfo;
0178
0179 xt_rateest_put(par->net, info->est);
0180 }
0181
0182 static struct xt_target xt_rateest_tg_reg __read_mostly = {
0183 .name = "RATEEST",
0184 .revision = 0,
0185 .family = NFPROTO_UNSPEC,
0186 .target = xt_rateest_tg,
0187 .checkentry = xt_rateest_tg_checkentry,
0188 .destroy = xt_rateest_tg_destroy,
0189 .targetsize = sizeof(struct xt_rateest_target_info),
0190 .usersize = offsetof(struct xt_rateest_target_info, est),
0191 .me = THIS_MODULE,
0192 };
0193
0194 static __net_init int xt_rateest_net_init(struct net *net)
0195 {
0196 struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
0197 int i;
0198
0199 mutex_init(&xn->hash_lock);
0200 for (i = 0; i < ARRAY_SIZE(xn->hash); i++)
0201 INIT_HLIST_HEAD(&xn->hash[i]);
0202 return 0;
0203 }
0204
0205 static struct pernet_operations xt_rateest_net_ops = {
0206 .init = xt_rateest_net_init,
0207 .id = &xt_rateest_id,
0208 .size = sizeof(struct xt_rateest_net),
0209 };
0210
0211 static int __init xt_rateest_tg_init(void)
0212 {
0213 int err = register_pernet_subsys(&xt_rateest_net_ops);
0214
0215 if (err)
0216 return err;
0217 return xt_register_target(&xt_rateest_tg_reg);
0218 }
0219
0220 static void __exit xt_rateest_tg_fini(void)
0221 {
0222 xt_unregister_target(&xt_rateest_tg_reg);
0223 unregister_pernet_subsys(&xt_rateest_net_ops);
0224 }
0225
0226
0227 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
0228 MODULE_LICENSE("GPL");
0229 MODULE_DESCRIPTION("Xtables: packet rate estimator");
0230 MODULE_ALIAS("ipt_RATEEST");
0231 MODULE_ALIAS("ip6t_RATEEST");
0232 module_init(xt_rateest_tg_init);
0233 module_exit(xt_rateest_tg_fini);