1 // SPDX-License-Identifier: GPL-2.0-only
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/spinlock.h>
12 #include <linux/interrupt.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/xt_limit.h>
17 struct xt_limit_priv {
23 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("Xtables: rate-limit match");
26 MODULE_ALIAS("ipt_limit");
27 MODULE_ALIAS("ip6t_limit");
29 /* The algorithm used is the Simple Token Bucket Filter (TBF)
30 * see net/sched/sch_tbf.c in the linux source tree
33 /* Rusty: This is my (non-mathematically-inclined) understanding of
34 this algorithm. The `average rate' in jiffies becomes your initial
35 amount of credit `credit' and the most credit you can ever have
36 `credit_cap'. The `peak rate' becomes the cost of passing the
39 `prev' tracks the last packet hit: you gain one credit per jiffy.
40 If you get credit balance more than this, the extra credit is
41 discarded. Every time the match passes, you lose `cost' credits;
42 if you don't have that many, the test fails.
44 See Alexey's formal explanation in net/sched/sch_tbf.c.
46 To get the maximum range, we multiply by this factor (ie. you get N
47 credits per jiffy). We want to allow a rate as low as 1 per day
48 (slowest userspace tool allows), which means
49 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */
50 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
52 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
53 * us the power of 2 below the theoretical max, so GCC simply does a
55 #define _POW2_BELOW2(x) ((x)|((x)>>1))
56 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
57 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
58 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
59 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
60 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
62 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
65 limit_mt(const struct sk_buff *skb, struct xt_action_param *par)
67 const struct xt_rateinfo *r = par->matchinfo;
68 struct xt_limit_priv *priv = r->master;
69 unsigned long now = jiffies;
71 spin_lock_bh(&priv->lock);
72 priv->credit += (now - xchg(&priv->prev, now)) * CREDITS_PER_JIFFY;
73 if (priv->credit > r->credit_cap)
74 priv->credit = r->credit_cap;
76 if (priv->credit >= r->cost) {
77 /* We're not limited. */
78 priv->credit -= r->cost;
79 spin_unlock_bh(&priv->lock);
83 spin_unlock_bh(&priv->lock);
87 /* Precision saver. */
88 static u32 user2credits(u32 user)
90 /* If multiplying would overflow... */
91 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
93 return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
95 return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE;
98 static int limit_mt_check(const struct xt_mtchk_param *par)
100 struct xt_rateinfo *r = par->matchinfo;
101 struct xt_limit_priv *priv;
103 /* Check for overflow. */
105 || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
106 pr_info_ratelimited("Overflow, try lower: %u/%u\n",
111 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
115 /* For SMP, we only want to use one set of state. */
117 /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
119 priv->prev = jiffies;
120 priv->credit = user2credits(r->avg * r->burst); /* Credits full. */
122 r->credit_cap = priv->credit; /* Credits full. */
123 r->cost = user2credits(r->avg);
125 spin_lock_init(&priv->lock);
130 static void limit_mt_destroy(const struct xt_mtdtor_param *par)
132 const struct xt_rateinfo *info = par->matchinfo;
138 struct compat_xt_rateinfo {
144 u_int32_t credit_cap, cost;
149 /* To keep the full "prev" timestamp, the upper 32 bits are stored in the
150 * master pointer, which does not need to be preserved. */
151 static void limit_mt_compat_from_user(void *dst, const void *src)
153 const struct compat_xt_rateinfo *cm = src;
154 struct xt_rateinfo m = {
157 .prev = cm->prev | (unsigned long)cm->master << 32,
158 .credit = cm->credit,
159 .credit_cap = cm->credit_cap,
162 memcpy(dst, &m, sizeof(m));
165 static int limit_mt_compat_to_user(void __user *dst, const void *src)
167 const struct xt_rateinfo *m = src;
168 struct compat_xt_rateinfo cm = {
173 .credit_cap = m->credit_cap,
175 .master = m->prev >> 32,
177 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
179 #endif /* CONFIG_COMPAT */
181 static struct xt_match limit_mt_reg __read_mostly = {
184 .family = NFPROTO_UNSPEC,
186 .checkentry = limit_mt_check,
187 .destroy = limit_mt_destroy,
188 .matchsize = sizeof(struct xt_rateinfo),
190 .compatsize = sizeof(struct compat_xt_rateinfo),
191 .compat_from_user = limit_mt_compat_from_user,
192 .compat_to_user = limit_mt_compat_to_user,
194 .usersize = offsetof(struct xt_rateinfo, prev),
198 static int __init limit_mt_init(void)
200 return xt_register_match(&limit_mt_reg);
203 static void __exit limit_mt_exit(void)
205 xt_unregister_match(&limit_mt_reg);
208 module_init(limit_mt_init);
209 module_exit(limit_mt_exit);