Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module to control the rate |
| 2 | * |
| 3 | * 2 September 1999: Changed from the target RATE to the match |
| 4 | * `limit', removed logging. Did I mention that |
| 5 | * Alexey is a fucking genius? |
| 6 | * Rusty Russell (rusty@rustcorp.com.au). */ |
| 7 | |
| 8 | /* (C) 1999 Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr> |
| 9 | * (C) 1999 Hervé Eychenne <eychenne@info.enserb.u-bordeaux.fr> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 21 | #include <linux/netfilter/x_tables.h> |
| 22 | #include <linux/netfilter/xt_limit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>"); |
| 26 | MODULE_DESCRIPTION("iptables rate limit match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 27 | MODULE_ALIAS("ipt_limit"); |
| 28 | MODULE_ALIAS("ip6t_limit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* The algorithm used is the Simple Token Bucket Filter (TBF) |
| 31 | * see net/sched/sch_tbf.c in the linux source tree |
| 32 | */ |
| 33 | |
| 34 | static DEFINE_SPINLOCK(limit_lock); |
| 35 | |
| 36 | /* Rusty: This is my (non-mathematically-inclined) understanding of |
| 37 | this algorithm. The `average rate' in jiffies becomes your initial |
| 38 | amount of credit `credit' and the most credit you can ever have |
| 39 | `credit_cap'. The `peak rate' becomes the cost of passing the |
| 40 | test, `cost'. |
| 41 | |
| 42 | `prev' tracks the last packet hit: you gain one credit per jiffy. |
| 43 | If you get credit balance more than this, the extra credit is |
| 44 | discarded. Every time the match passes, you lose `cost' credits; |
| 45 | if you don't have that many, the test fails. |
| 46 | |
| 47 | See Alexey's formal explanation in net/sched/sch_tbf.c. |
| 48 | |
| 49 | To get the maxmum range, we multiply by this factor (ie. you get N |
| 50 | credits per jiffy). We want to allow a rate as low as 1 per day |
| 51 | (slowest userspace tool allows), which means |
| 52 | CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */ |
| 53 | #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) |
| 54 | |
| 55 | /* Repeated shift and or gives us all 1s, final shift and add 1 gives |
| 56 | * us the power of 2 below the theoretical max, so GCC simply does a |
| 57 | * shift. */ |
| 58 | #define _POW2_BELOW2(x) ((x)|((x)>>1)) |
| 59 | #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) |
| 60 | #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) |
| 61 | #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) |
| 62 | #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) |
| 63 | #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) |
| 64 | |
| 65 | #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) |
| 66 | |
| 67 | static int |
| 68 | ipt_limit_match(const struct sk_buff *skb, |
| 69 | const struct net_device *in, |
| 70 | const struct net_device *out, |
| 71 | const void *matchinfo, |
| 72 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 73 | unsigned int protoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int *hotdrop) |
| 75 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 76 | struct xt_rateinfo *r = ((struct xt_rateinfo *)matchinfo)->master; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned long now = jiffies; |
| 78 | |
| 79 | spin_lock_bh(&limit_lock); |
| 80 | r->credit += (now - xchg(&r->prev, now)) * CREDITS_PER_JIFFY; |
| 81 | if (r->credit > r->credit_cap) |
| 82 | r->credit = r->credit_cap; |
| 83 | |
| 84 | if (r->credit >= r->cost) { |
| 85 | /* We're not limited. */ |
| 86 | r->credit -= r->cost; |
| 87 | spin_unlock_bh(&limit_lock); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | spin_unlock_bh(&limit_lock); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* Precision saver. */ |
| 96 | static u_int32_t |
| 97 | user2credits(u_int32_t user) |
| 98 | { |
| 99 | /* If multiplying would overflow... */ |
| 100 | if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) |
| 101 | /* Divide first. */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 102 | return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 104 | return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int |
| 108 | ipt_limit_checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 109 | const void *inf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | void *matchinfo, |
| 111 | unsigned int matchsize, |
| 112 | unsigned int hook_mask) |
| 113 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 114 | struct xt_rateinfo *r = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 116 | if (matchsize != XT_ALIGN(sizeof(struct xt_rateinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 0; |
| 118 | |
| 119 | /* Check for overflow. */ |
| 120 | if (r->burst == 0 |
| 121 | || user2credits(r->avg * r->burst) < user2credits(r->avg)) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 122 | printk("Overflow in xt_limit, try lower: %u/%u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | r->avg, r->burst); |
| 124 | return 0; |
| 125 | } |
| 126 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 128. */ |
| 129 | r->prev = jiffies; |
| 130 | r->credit = user2credits(r->avg * r->burst); /* Credits full. */ |
| 131 | r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */ |
| 132 | r->cost = user2credits(r->avg); |
| 133 | |
| 134 | /* For SMP, we only want to use one set of counters. */ |
| 135 | r->master = r; |
| 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 140 | static struct xt_match ipt_limit_reg = { |
| 141 | .name = "limit", |
| 142 | .match = ipt_limit_match, |
| 143 | .checkentry = ipt_limit_checkentry, |
| 144 | .me = THIS_MODULE, |
| 145 | }; |
| 146 | static struct xt_match limit6_reg = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | .name = "limit", |
| 148 | .match = ipt_limit_match, |
| 149 | .checkentry = ipt_limit_checkentry, |
| 150 | .me = THIS_MODULE, |
| 151 | }; |
| 152 | |
| 153 | static int __init init(void) |
| 154 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 155 | int ret; |
| 156 | |
| 157 | ret = xt_register_match(AF_INET, &ipt_limit_reg); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
| 161 | ret = xt_register_match(AF_INET6, &limit6_reg); |
| 162 | if (ret) |
| 163 | xt_unregister_match(AF_INET, &ipt_limit_reg); |
| 164 | |
| 165 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void __exit fini(void) |
| 169 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 170 | xt_unregister_match(AF_INET, &ipt_limit_reg); |
| 171 | xt_unregister_match(AF_INET6, &limit6_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | module_init(init); |
| 175 | module_exit(fini); |