blob: 517e78befcb2688a76e307ba7e7064e334700cb1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_limit
3 *
4 * Authors:
5 * Tom Marshall <tommy@home.tig-grr.com>
6 *
7 * Mostly copied from netfilter's ipt_limit.c, see that file for
8 * more explanation
9 *
10 * September, 2003
11 *
12 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/netdevice.h>
16#include <linux/spinlock.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020017#include <linux/netfilter/x_tables.h>
18#include <linux/netfilter_bridge/ebtables.h>
19#include <linux/netfilter_bridge/ebt_limit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static DEFINE_SPINLOCK(limit_lock);
22
23#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
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)
31
32#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
33
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020034static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020035ebt_limit_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020037 struct ebt_limit_info *info = (void *)par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 unsigned long now = jiffies;
39
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;
44
45 if (info->credit >= info->cost) {
46 /* We're not limited. */
47 info->credit -= info->cost;
48 spin_unlock_bh(&limit_lock);
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020049 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51
52 spin_unlock_bh(&limit_lock);
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020053 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56/* Precision saver. */
57static u_int32_t
58user2credits(u_int32_t user)
59{
60 /* If multiplying would overflow... */
61 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
62 /* Divide first. */
63 return (user / EBT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
64
65 return (user * HZ * CREDITS_PER_JIFFY) / EBT_LIMIT_SCALE;
66}
67
Jan Engelhardtb0f38452010-03-19 17:16:42 +010068static int ebt_limit_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020070 struct ebt_limit_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* Check for overflow. */
73 if (info->burst == 0 ||
74 user2credits(info->avg * info->burst) < user2credits(info->avg)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010075 pr_info("overflow, try lower: %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 info->avg, info->burst);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
80 /* User avg in seconds * EBT_LIMIT_SCALE: convert to jiffies * 128. */
81 info->prev = jiffies;
82 info->credit = user2credits(info->avg * info->burst);
83 info->credit_cap = user2credits(info->avg * info->burst);
84 info->cost = user2credits(info->avg);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Florian Westphal314ddca2010-01-27 14:38:32 +010088
89#ifdef CONFIG_COMPAT
90/*
91 * no conversion function needed --
92 * only avg/burst have meaningful values in userspace.
93 */
94struct ebt_compat_limit_info {
95 compat_uint_t avg, burst;
96 compat_ulong_t prev;
97 compat_uint_t credit, credit_cap, cost;
98};
99#endif
100
Jan Engelhardt043ef462008-10-08 11:35:15 +0200101static struct xt_match ebt_limit_mt_reg __read_mostly = {
102 .name = "limit",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200103 .revision = 0,
104 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200105 .match = ebt_limit_mt,
106 .checkentry = ebt_limit_mt_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100107 .matchsize = sizeof(struct ebt_limit_info),
Florian Westphal314ddca2010-01-27 14:38:32 +0100108#ifdef CONFIG_COMPAT
109 .compatsize = sizeof(struct ebt_compat_limit_info),
110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .me = THIS_MODULE,
112};
113
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800114static int __init ebt_limit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200116 return xt_register_match(&ebt_limit_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800119static void __exit ebt_limit_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200121 xt_unregister_match(&ebt_limit_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800124module_init(ebt_limit_init);
125module_exit(ebt_limit_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800126MODULE_DESCRIPTION("Ebtables: Rate-limit match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127MODULE_LICENSE("GPL");