5 * Tom Marshall <tommy@home.tig-grr.com>
7 * Mostly copied from netfilter's ipt_limit.c, see that file for
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/spinlock.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter_bridge/ebtables.h>
19 #include <linux/netfilter_bridge/ebt_limit.h>
21 static DEFINE_SPINLOCK(limit_lock
);
23 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
25 #define _POW2_BELOW2(x) ((x)|((x)>>1))
26 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
27 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
28 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
29 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
30 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
32 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
35 ebt_limit_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
37 struct ebt_limit_info
*info
= (void *)par
->matchinfo
;
38 unsigned long now
= jiffies
;
40 spin_lock_bh(&limit_lock
);
41 info
->credit
+= (now
- xchg(&info
->prev
, now
)) * CREDITS_PER_JIFFY
;
42 if (info
->credit
> info
->credit_cap
)
43 info
->credit
= info
->credit_cap
;
45 if (info
->credit
>= info
->cost
) {
46 /* We're not limited. */
47 info
->credit
-= info
->cost
;
48 spin_unlock_bh(&limit_lock
);
52 spin_unlock_bh(&limit_lock
);
56 /* Precision saver. */
58 user2credits(u_int32_t user
)
60 /* If multiplying would overflow... */
61 if (user
> 0xFFFFFFFF / (HZ
*CREDITS_PER_JIFFY
))
63 return (user
/ EBT_LIMIT_SCALE
) * HZ
* CREDITS_PER_JIFFY
;
65 return (user
* HZ
* CREDITS_PER_JIFFY
) / EBT_LIMIT_SCALE
;
68 static int ebt_limit_mt_check(const struct xt_mtchk_param
*par
)
70 struct ebt_limit_info
*info
= par
->matchinfo
;
72 /* Check for overflow. */
73 if (info
->burst
== 0 ||
74 user2credits(info
->avg
* info
->burst
) < user2credits(info
->avg
)) {
75 pr_info("overflow, try lower: %u/%u\n",
76 info
->avg
, info
->burst
);
80 /* User avg in seconds * EBT_LIMIT_SCALE: convert to jiffies * 128. */
82 info
->credit
= user2credits(info
->avg
* info
->burst
);
83 info
->credit_cap
= user2credits(info
->avg
* info
->burst
);
84 info
->cost
= user2credits(info
->avg
);
91 * no conversion function needed --
92 * only avg/burst have meaningful values in userspace.
94 struct ebt_compat_limit_info
{
95 compat_uint_t avg
, burst
;
97 compat_uint_t credit
, credit_cap
, cost
;
101 static struct xt_match ebt_limit_mt_reg __read_mostly
= {
104 .family
= NFPROTO_BRIDGE
,
105 .match
= ebt_limit_mt
,
106 .checkentry
= ebt_limit_mt_check
,
107 .matchsize
= sizeof(struct ebt_limit_info
),
109 .compatsize
= sizeof(struct ebt_compat_limit_info
),
114 static int __init
ebt_limit_init(void)
116 return xt_register_match(&ebt_limit_mt_reg
);
119 static void __exit
ebt_limit_fini(void)
121 xt_unregister_match(&ebt_limit_mt_reg
);
124 module_init(ebt_limit_init
);
125 module_exit(ebt_limit_fini
);
126 MODULE_DESCRIPTION("Ebtables: Rate-limit match");
127 MODULE_LICENSE("GPL");