Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/spinlock.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 16 | #include <linux/netfilter/x_tables.h> |
| 17 | #include <linux/netfilter_bridge/ebtables.h> |
| 18 | #include <linux/netfilter_bridge/ebt_limit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | static DEFINE_SPINLOCK(limit_lock); |
| 21 | |
| 22 | #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) |
| 23 | |
| 24 | #define _POW2_BELOW2(x) ((x)|((x)>>1)) |
| 25 | #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) |
| 26 | #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) |
| 27 | #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) |
| 28 | #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) |
| 29 | #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) |
| 30 | |
| 31 | #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) |
| 32 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 33 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 34 | ebt_limit_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 36 | struct ebt_limit_info *info = (void *)par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | unsigned long now = jiffies; |
| 38 | |
| 39 | spin_lock_bh(&limit_lock); |
| 40 | info->credit += (now - xchg(&info->prev, now)) * CREDITS_PER_JIFFY; |
| 41 | if (info->credit > info->credit_cap) |
| 42 | info->credit = info->credit_cap; |
| 43 | |
| 44 | if (info->credit >= info->cost) { |
| 45 | /* We're not limited. */ |
| 46 | info->credit -= info->cost; |
| 47 | spin_unlock_bh(&limit_lock); |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 48 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | spin_unlock_bh(&limit_lock); |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 52 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* Precision saver. */ |
| 56 | static u_int32_t |
| 57 | user2credits(u_int32_t user) |
| 58 | { |
| 59 | /* If multiplying would overflow... */ |
| 60 | if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) |
| 61 | /* Divide first. */ |
| 62 | return (user / EBT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; |
| 63 | |
| 64 | return (user * HZ * CREDITS_PER_JIFFY) / EBT_LIMIT_SCALE; |
| 65 | } |
| 66 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 67 | static bool ebt_limit_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 69 | struct ebt_limit_info *info = par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* Check for overflow. */ |
| 72 | if (info->burst == 0 || |
| 73 | user2credits(info->avg * info->burst) < user2credits(info->avg)) { |
| 74 | printk("Overflow in ebt_limit, try lower: %u/%u\n", |
| 75 | info->avg, info->burst); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 76 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* User avg in seconds * EBT_LIMIT_SCALE: convert to jiffies * 128. */ |
| 80 | info->prev = jiffies; |
| 81 | info->credit = user2credits(info->avg * info->burst); |
| 82 | info->credit_cap = user2credits(info->avg * info->burst); |
| 83 | info->cost = user2credits(info->avg); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 84 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Florian Westphal | 314ddca | 2010-01-27 14:38:32 +0100 | [diff] [blame] | 87 | |
| 88 | #ifdef CONFIG_COMPAT |
| 89 | /* |
| 90 | * no conversion function needed -- |
| 91 | * only avg/burst have meaningful values in userspace. |
| 92 | */ |
| 93 | struct ebt_compat_limit_info { |
| 94 | compat_uint_t avg, burst; |
| 95 | compat_ulong_t prev; |
| 96 | compat_uint_t credit, credit_cap, cost; |
| 97 | }; |
| 98 | #endif |
| 99 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 100 | static struct xt_match ebt_limit_mt_reg __read_mostly = { |
| 101 | .name = "limit", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 102 | .revision = 0, |
| 103 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 104 | .match = ebt_limit_mt, |
| 105 | .checkentry = ebt_limit_mt_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 106 | .matchsize = sizeof(struct ebt_limit_info), |
Florian Westphal | 314ddca | 2010-01-27 14:38:32 +0100 | [diff] [blame] | 107 | #ifdef CONFIG_COMPAT |
| 108 | .compatsize = sizeof(struct ebt_compat_limit_info), |
| 109 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | .me = THIS_MODULE, |
| 111 | }; |
| 112 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 113 | static int __init ebt_limit_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 115 | return xt_register_match(&ebt_limit_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 118 | static void __exit ebt_limit_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 120 | xt_unregister_match(&ebt_limit_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 123 | module_init(ebt_limit_init); |
| 124 | module_exit(ebt_limit_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 125 | MODULE_DESCRIPTION("Ebtables: Rate-limit match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | MODULE_LICENSE("GPL"); |