Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1 | /* iptables match extension to limit the number of packets per second |
| 2 | * seperately for each hashbucket (sourceip/sourceport/dstip/dstport) |
| 3 | * |
| 4 | * (C) 2003-2004 by Harald Welte <laforge@netfilter.org> |
| 5 | * |
| 6 | * $Id: ipt_hashlimit.c 3244 2004-10-20 16:24:29Z laforge@netfilter.org $ |
| 7 | * |
| 8 | * Development of this code was funded by Astaro AG, http://www.astaro.com/ |
| 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/random.h> |
| 13 | #include <linux/jhash.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/proc_fs.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/skbuff.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 20 | #include <linux/mm.h> |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 21 | #include <linux/in.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/ipv6.h> |
| 24 | |
| 25 | #include <linux/netfilter/x_tables.h> |
| 26 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 27 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 28 | #include <linux/netfilter/xt_hashlimit.h> |
| 29 | #include <linux/mutex.h> |
| 30 | |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 33 | MODULE_DESCRIPTION("iptables match for limiting per hash-bucket"); |
| 34 | MODULE_ALIAS("ipt_hashlimit"); |
| 35 | MODULE_ALIAS("ip6t_hashlimit"); |
| 36 | |
| 37 | /* need to declare this at the top */ |
| 38 | static struct proc_dir_entry *hashlimit_procdir4; |
| 39 | static struct proc_dir_entry *hashlimit_procdir6; |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 40 | static const struct file_operations dl_file_ops; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 41 | |
| 42 | /* hash table crap */ |
| 43 | struct dsthash_dst { |
| 44 | union { |
| 45 | struct { |
| 46 | __be32 src; |
| 47 | __be32 dst; |
| 48 | } ip; |
| 49 | struct { |
| 50 | __be32 src[4]; |
| 51 | __be32 dst[4]; |
| 52 | } ip6; |
| 53 | } addr; |
| 54 | __be16 src_port; |
| 55 | __be16 dst_port; |
| 56 | }; |
| 57 | |
| 58 | struct dsthash_ent { |
| 59 | /* static / read-only parts in the beginning */ |
| 60 | struct hlist_node node; |
| 61 | struct dsthash_dst dst; |
| 62 | |
| 63 | /* modified structure members in the end */ |
| 64 | unsigned long expires; /* precalculated expiry time */ |
| 65 | struct { |
| 66 | unsigned long prev; /* last modification */ |
| 67 | u_int32_t credit; |
| 68 | u_int32_t credit_cap, cost; |
| 69 | } rateinfo; |
| 70 | }; |
| 71 | |
| 72 | struct xt_hashlimit_htable { |
| 73 | struct hlist_node node; /* global list of all htables */ |
| 74 | atomic_t use; |
| 75 | int family; |
| 76 | |
| 77 | struct hashlimit_cfg cfg; /* config */ |
| 78 | |
| 79 | /* used internally */ |
| 80 | spinlock_t lock; /* lock for list_head */ |
| 81 | u_int32_t rnd; /* random seed for hash */ |
| 82 | int rnd_initialized; |
| 83 | unsigned int count; /* number entries in table */ |
| 84 | struct timer_list timer; /* timer for gc */ |
| 85 | |
| 86 | /* seq_file stuff */ |
| 87 | struct proc_dir_entry *pde; |
| 88 | |
| 89 | struct hlist_head hash[0]; /* hashtable itself */ |
| 90 | }; |
| 91 | |
| 92 | static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */ |
| 93 | static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */ |
| 94 | static HLIST_HEAD(hashlimit_htables); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 95 | static struct kmem_cache *hashlimit_cachep __read_mostly; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 96 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 97 | static inline bool dst_cmp(const struct dsthash_ent *ent, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 98 | const struct dsthash_dst *b) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 99 | { |
| 100 | return !memcmp(&ent->dst, b, sizeof(ent->dst)); |
| 101 | } |
| 102 | |
| 103 | static u_int32_t |
| 104 | hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst) |
| 105 | { |
| 106 | return jhash(dst, sizeof(*dst), ht->rnd) % ht->cfg.size; |
| 107 | } |
| 108 | |
| 109 | static struct dsthash_ent * |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 110 | dsthash_find(const struct xt_hashlimit_htable *ht, |
| 111 | const struct dsthash_dst *dst) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 112 | { |
| 113 | struct dsthash_ent *ent; |
| 114 | struct hlist_node *pos; |
| 115 | u_int32_t hash = hash_dst(ht, dst); |
| 116 | |
| 117 | if (!hlist_empty(&ht->hash[hash])) { |
| 118 | hlist_for_each_entry(ent, pos, &ht->hash[hash], node) |
| 119 | if (dst_cmp(ent, dst)) |
| 120 | return ent; |
| 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | /* allocate dsthash_ent, initialize dst, put in htable and lock it */ |
| 126 | static struct dsthash_ent * |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 127 | dsthash_alloc_init(struct xt_hashlimit_htable *ht, |
| 128 | const struct dsthash_dst *dst) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 129 | { |
| 130 | struct dsthash_ent *ent; |
| 131 | |
| 132 | /* initialize hash with random val at the time we allocate |
| 133 | * the first hashtable entry */ |
| 134 | if (!ht->rnd_initialized) { |
| 135 | get_random_bytes(&ht->rnd, 4); |
| 136 | ht->rnd_initialized = 1; |
| 137 | } |
| 138 | |
| 139 | if (ht->cfg.max && ht->count >= ht->cfg.max) { |
| 140 | /* FIXME: do something. question is what.. */ |
| 141 | if (net_ratelimit()) |
| 142 | printk(KERN_WARNING |
| 143 | "xt_hashlimit: max count of %u reached\n", |
| 144 | ht->cfg.max); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC); |
| 149 | if (!ent) { |
| 150 | if (net_ratelimit()) |
| 151 | printk(KERN_ERR |
| 152 | "xt_hashlimit: can't allocate dsthash_ent\n"); |
| 153 | return NULL; |
| 154 | } |
| 155 | memcpy(&ent->dst, dst, sizeof(ent->dst)); |
| 156 | |
| 157 | hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]); |
| 158 | ht->count++; |
| 159 | return ent; |
| 160 | } |
| 161 | |
| 162 | static inline void |
| 163 | dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent) |
| 164 | { |
| 165 | hlist_del(&ent->node); |
| 166 | kmem_cache_free(hashlimit_cachep, ent); |
| 167 | ht->count--; |
| 168 | } |
| 169 | static void htable_gc(unsigned long htlong); |
| 170 | |
| 171 | static int htable_create(struct xt_hashlimit_info *minfo, int family) |
| 172 | { |
| 173 | struct xt_hashlimit_htable *hinfo; |
| 174 | unsigned int size; |
| 175 | unsigned int i; |
| 176 | |
| 177 | if (minfo->cfg.size) |
| 178 | size = minfo->cfg.size; |
| 179 | else { |
| 180 | size = ((num_physpages << PAGE_SHIFT) / 16384) / |
| 181 | sizeof(struct list_head); |
| 182 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
| 183 | size = 8192; |
| 184 | if (size < 16) |
| 185 | size = 16; |
| 186 | } |
| 187 | /* FIXME: don't use vmalloc() here or anywhere else -HW */ |
| 188 | hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + |
| 189 | sizeof(struct list_head) * size); |
| 190 | if (!hinfo) { |
| 191 | printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); |
| 192 | return -1; |
| 193 | } |
| 194 | minfo->hinfo = hinfo; |
| 195 | |
| 196 | /* copy match config into hashtable config */ |
| 197 | memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg)); |
| 198 | hinfo->cfg.size = size; |
| 199 | if (!hinfo->cfg.max) |
| 200 | hinfo->cfg.max = 8 * hinfo->cfg.size; |
| 201 | else if (hinfo->cfg.max < hinfo->cfg.size) |
| 202 | hinfo->cfg.max = hinfo->cfg.size; |
| 203 | |
| 204 | for (i = 0; i < hinfo->cfg.size; i++) |
| 205 | INIT_HLIST_HEAD(&hinfo->hash[i]); |
| 206 | |
| 207 | atomic_set(&hinfo->use, 1); |
| 208 | hinfo->count = 0; |
| 209 | hinfo->family = family; |
| 210 | hinfo->rnd_initialized = 0; |
| 211 | spin_lock_init(&hinfo->lock); |
| 212 | hinfo->pde = create_proc_entry(minfo->name, 0, |
| 213 | family == AF_INET ? hashlimit_procdir4 : |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 214 | hashlimit_procdir6); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 215 | if (!hinfo->pde) { |
| 216 | vfree(hinfo); |
| 217 | return -1; |
| 218 | } |
| 219 | hinfo->pde->proc_fops = &dl_file_ops; |
| 220 | hinfo->pde->data = hinfo; |
| 221 | |
Patrick McHardy | e6f689d | 2007-03-23 11:16:30 -0700 | [diff] [blame] | 222 | setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 223 | hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 224 | add_timer(&hinfo->timer); |
| 225 | |
| 226 | spin_lock_bh(&hashlimit_lock); |
| 227 | hlist_add_head(&hinfo->node, &hashlimit_htables); |
| 228 | spin_unlock_bh(&hashlimit_lock); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 233 | static bool select_all(const struct xt_hashlimit_htable *ht, |
| 234 | const struct dsthash_ent *he) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 235 | { |
| 236 | return 1; |
| 237 | } |
| 238 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 239 | static bool select_gc(const struct xt_hashlimit_htable *ht, |
| 240 | const struct dsthash_ent *he) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 241 | { |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 242 | return jiffies >= he->expires; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void htable_selective_cleanup(struct xt_hashlimit_htable *ht, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 246 | bool (*select)(const struct xt_hashlimit_htable *ht, |
| 247 | const struct dsthash_ent *he)) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 248 | { |
| 249 | unsigned int i; |
| 250 | |
| 251 | /* lock hash table and iterate over it */ |
| 252 | spin_lock_bh(&ht->lock); |
| 253 | for (i = 0; i < ht->cfg.size; i++) { |
| 254 | struct dsthash_ent *dh; |
| 255 | struct hlist_node *pos, *n; |
| 256 | hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) { |
| 257 | if ((*select)(ht, dh)) |
| 258 | dsthash_free(ht, dh); |
| 259 | } |
| 260 | } |
| 261 | spin_unlock_bh(&ht->lock); |
| 262 | } |
| 263 | |
| 264 | /* hash table garbage collector, run by timer */ |
| 265 | static void htable_gc(unsigned long htlong) |
| 266 | { |
| 267 | struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong; |
| 268 | |
| 269 | htable_selective_cleanup(ht, select_gc); |
| 270 | |
| 271 | /* re-add the timer accordingly */ |
| 272 | ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval); |
| 273 | add_timer(&ht->timer); |
| 274 | } |
| 275 | |
| 276 | static void htable_destroy(struct xt_hashlimit_htable *hinfo) |
| 277 | { |
| 278 | /* remove timer, if it is pending */ |
| 279 | if (timer_pending(&hinfo->timer)) |
| 280 | del_timer(&hinfo->timer); |
| 281 | |
| 282 | /* remove proc entry */ |
| 283 | remove_proc_entry(hinfo->pde->name, |
| 284 | hinfo->family == AF_INET ? hashlimit_procdir4 : |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 285 | hashlimit_procdir6); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 286 | htable_selective_cleanup(hinfo, select_all); |
| 287 | vfree(hinfo); |
| 288 | } |
| 289 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 290 | static struct xt_hashlimit_htable *htable_find_get(const char *name, |
| 291 | int family) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 292 | { |
| 293 | struct xt_hashlimit_htable *hinfo; |
| 294 | struct hlist_node *pos; |
| 295 | |
| 296 | spin_lock_bh(&hashlimit_lock); |
| 297 | hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) { |
| 298 | if (!strcmp(name, hinfo->pde->name) && |
| 299 | hinfo->family == family) { |
| 300 | atomic_inc(&hinfo->use); |
| 301 | spin_unlock_bh(&hashlimit_lock); |
| 302 | return hinfo; |
| 303 | } |
| 304 | } |
| 305 | spin_unlock_bh(&hashlimit_lock); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | static void htable_put(struct xt_hashlimit_htable *hinfo) |
| 310 | { |
| 311 | if (atomic_dec_and_test(&hinfo->use)) { |
| 312 | spin_lock_bh(&hashlimit_lock); |
| 313 | hlist_del(&hinfo->node); |
| 314 | spin_unlock_bh(&hashlimit_lock); |
| 315 | htable_destroy(hinfo); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* The algorithm used is the Simple Token Bucket Filter (TBF) |
| 320 | * see net/sched/sch_tbf.c in the linux source tree |
| 321 | */ |
| 322 | |
| 323 | /* Rusty: This is my (non-mathematically-inclined) understanding of |
| 324 | this algorithm. The `average rate' in jiffies becomes your initial |
| 325 | amount of credit `credit' and the most credit you can ever have |
| 326 | `credit_cap'. The `peak rate' becomes the cost of passing the |
| 327 | test, `cost'. |
| 328 | |
| 329 | `prev' tracks the last packet hit: you gain one credit per jiffy. |
| 330 | If you get credit balance more than this, the extra credit is |
| 331 | discarded. Every time the match passes, you lose `cost' credits; |
| 332 | if you don't have that many, the test fails. |
| 333 | |
| 334 | See Alexey's formal explanation in net/sched/sch_tbf.c. |
| 335 | |
| 336 | To get the maximum range, we multiply by this factor (ie. you get N |
| 337 | credits per jiffy). We want to allow a rate as low as 1 per day |
| 338 | (slowest userspace tool allows), which means |
| 339 | CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie. |
| 340 | */ |
| 341 | #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) |
| 342 | |
| 343 | /* Repeated shift and or gives us all 1s, final shift and add 1 gives |
| 344 | * us the power of 2 below the theoretical max, so GCC simply does a |
| 345 | * shift. */ |
| 346 | #define _POW2_BELOW2(x) ((x)|((x)>>1)) |
| 347 | #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) |
| 348 | #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) |
| 349 | #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) |
| 350 | #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) |
| 351 | #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) |
| 352 | |
| 353 | #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) |
| 354 | |
| 355 | /* Precision saver. */ |
| 356 | static inline u_int32_t |
| 357 | user2credits(u_int32_t user) |
| 358 | { |
| 359 | /* If multiplying would overflow... */ |
| 360 | if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) |
| 361 | /* Divide first. */ |
| 362 | return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; |
| 363 | |
| 364 | return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE; |
| 365 | } |
| 366 | |
| 367 | static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) |
| 368 | { |
| 369 | dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY; |
| 370 | if (dh->rateinfo.credit > dh->rateinfo.credit_cap) |
| 371 | dh->rateinfo.credit = dh->rateinfo.credit_cap; |
| 372 | dh->rateinfo.prev = now; |
| 373 | } |
| 374 | |
| 375 | static int |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 376 | hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo, |
| 377 | struct dsthash_dst *dst, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 378 | const struct sk_buff *skb, unsigned int protoff) |
| 379 | { |
| 380 | __be16 _ports[2], *ports; |
| 381 | int nexthdr; |
| 382 | |
| 383 | memset(dst, 0, sizeof(*dst)); |
| 384 | |
| 385 | switch (hinfo->family) { |
| 386 | case AF_INET: |
| 387 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 388 | dst->addr.ip.dst = ip_hdr(skb)->daddr; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 389 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 390 | dst->addr.ip.src = ip_hdr(skb)->saddr; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 391 | |
| 392 | if (!(hinfo->cfg.mode & |
| 393 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) |
| 394 | return 0; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 395 | nexthdr = ip_hdr(skb)->protocol; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 396 | break; |
Yasuyuki Kozakai | 02dba02 | 2006-12-02 22:19:01 -0800 | [diff] [blame] | 397 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 398 | case AF_INET6: |
| 399 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 400 | memcpy(&dst->addr.ip6.dst, &ipv6_hdr(skb)->daddr, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 401 | sizeof(dst->addr.ip6.dst)); |
| 402 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 403 | memcpy(&dst->addr.ip6.src, &ipv6_hdr(skb)->saddr, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 404 | sizeof(dst->addr.ip6.src)); |
| 405 | |
| 406 | if (!(hinfo->cfg.mode & |
| 407 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) |
| 408 | return 0; |
| 409 | nexthdr = ipv6_find_hdr(skb, &protoff, -1, NULL); |
| 410 | if (nexthdr < 0) |
| 411 | return -1; |
| 412 | break; |
| 413 | #endif |
| 414 | default: |
| 415 | BUG(); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | switch (nexthdr) { |
| 420 | case IPPROTO_TCP: |
| 421 | case IPPROTO_UDP: |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 422 | case IPPROTO_UDPLITE: |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 423 | case IPPROTO_SCTP: |
| 424 | case IPPROTO_DCCP: |
| 425 | ports = skb_header_pointer(skb, protoff, sizeof(_ports), |
| 426 | &_ports); |
| 427 | break; |
| 428 | default: |
| 429 | _ports[0] = _ports[1] = 0; |
| 430 | ports = _ports; |
| 431 | break; |
| 432 | } |
| 433 | if (!ports) |
| 434 | return -1; |
| 435 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT) |
| 436 | dst->src_port = ports[0]; |
| 437 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT) |
| 438 | dst->dst_port = ports[1]; |
| 439 | return 0; |
| 440 | } |
| 441 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 442 | static bool |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 443 | hashlimit_match(const struct sk_buff *skb, |
| 444 | const struct net_device *in, |
| 445 | const struct net_device *out, |
| 446 | const struct xt_match *match, |
| 447 | const void *matchinfo, |
| 448 | int offset, |
| 449 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 450 | bool *hotdrop) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 451 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 452 | const struct xt_hashlimit_info *r = |
| 453 | ((const struct xt_hashlimit_info *)matchinfo)->u.master; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 454 | struct xt_hashlimit_htable *hinfo = r->hinfo; |
| 455 | unsigned long now = jiffies; |
| 456 | struct dsthash_ent *dh; |
| 457 | struct dsthash_dst dst; |
| 458 | |
| 459 | if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) |
| 460 | goto hotdrop; |
| 461 | |
| 462 | spin_lock_bh(&hinfo->lock); |
| 463 | dh = dsthash_find(hinfo, &dst); |
| 464 | if (!dh) { |
| 465 | dh = dsthash_alloc_init(hinfo, &dst); |
| 466 | if (!dh) { |
| 467 | spin_unlock_bh(&hinfo->lock); |
| 468 | goto hotdrop; |
| 469 | } |
| 470 | |
| 471 | dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); |
| 472 | dh->rateinfo.prev = jiffies; |
| 473 | dh->rateinfo.credit = user2credits(hinfo->cfg.avg * |
| 474 | hinfo->cfg.burst); |
| 475 | dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * |
| 476 | hinfo->cfg.burst); |
| 477 | dh->rateinfo.cost = user2credits(hinfo->cfg.avg); |
| 478 | } else { |
| 479 | /* update expiration timeout */ |
| 480 | dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); |
| 481 | rateinfo_recalc(dh, now); |
| 482 | } |
| 483 | |
| 484 | if (dh->rateinfo.credit >= dh->rateinfo.cost) { |
| 485 | /* We're underlimit. */ |
| 486 | dh->rateinfo.credit -= dh->rateinfo.cost; |
| 487 | spin_unlock_bh(&hinfo->lock); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 488 | return true; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 489 | } |
| 490 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 491 | spin_unlock_bh(&hinfo->lock); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 492 | |
| 493 | /* default case: we're overlimit, thus don't match */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 494 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 495 | |
| 496 | hotdrop: |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 497 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 498 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 501 | static bool |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 502 | hashlimit_checkentry(const char *tablename, |
| 503 | const void *inf, |
| 504 | const struct xt_match *match, |
| 505 | void *matchinfo, |
| 506 | unsigned int hook_mask) |
| 507 | { |
| 508 | struct xt_hashlimit_info *r = matchinfo; |
| 509 | |
| 510 | /* Check for overflow. */ |
| 511 | if (r->cfg.burst == 0 || |
| 512 | user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) { |
| 513 | printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", |
| 514 | r->cfg.avg, r->cfg.burst); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 515 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 516 | } |
| 517 | if (r->cfg.mode == 0 || |
| 518 | r->cfg.mode > (XT_HASHLIMIT_HASH_DPT | |
| 519 | XT_HASHLIMIT_HASH_DIP | |
| 520 | XT_HASHLIMIT_HASH_SIP | |
| 521 | XT_HASHLIMIT_HASH_SPT)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 522 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 523 | if (!r->cfg.gc_interval) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 524 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 525 | if (!r->cfg.expire) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 526 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 527 | if (r->name[sizeof(r->name) - 1] != '\0') |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 528 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 529 | |
| 530 | /* This is the best we've got: We cannot release and re-grab lock, |
| 531 | * since checkentry() is called before x_tables.c grabs xt_mutex. |
| 532 | * We also cannot grab the hashtable spinlock, since htable_create will |
| 533 | * call vmalloc, and that can sleep. And we cannot just re-search |
| 534 | * the list of htable's in htable_create(), since then we would |
| 535 | * create duplicate proc files. -HW */ |
| 536 | mutex_lock(&hlimit_mutex); |
| 537 | r->hinfo = htable_find_get(r->name, match->family); |
| 538 | if (!r->hinfo && htable_create(r, match->family) != 0) { |
| 539 | mutex_unlock(&hlimit_mutex); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 540 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 541 | } |
| 542 | mutex_unlock(&hlimit_mutex); |
| 543 | |
| 544 | /* Ugly hack: For SMP, we only want to use one set */ |
| 545 | r->u.master = r; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 546 | return true; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static void |
| 550 | hashlimit_destroy(const struct xt_match *match, void *matchinfo) |
| 551 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 552 | const struct xt_hashlimit_info *r = matchinfo; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 553 | |
| 554 | htable_put(r->hinfo); |
| 555 | } |
| 556 | |
| 557 | #ifdef CONFIG_COMPAT |
| 558 | struct compat_xt_hashlimit_info { |
| 559 | char name[IFNAMSIZ]; |
| 560 | struct hashlimit_cfg cfg; |
| 561 | compat_uptr_t hinfo; |
| 562 | compat_uptr_t master; |
| 563 | }; |
| 564 | |
| 565 | static void compat_from_user(void *dst, void *src) |
| 566 | { |
| 567 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
| 568 | |
| 569 | memcpy(dst, src, off); |
| 570 | memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off); |
| 571 | } |
| 572 | |
| 573 | static int compat_to_user(void __user *dst, void *src) |
| 574 | { |
| 575 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
| 576 | |
| 577 | return copy_to_user(dst, src, off) ? -EFAULT : 0; |
| 578 | } |
| 579 | #endif |
| 580 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 581 | static struct xt_match xt_hashlimit[] __read_mostly = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 582 | { |
| 583 | .name = "hashlimit", |
| 584 | .family = AF_INET, |
| 585 | .match = hashlimit_match, |
| 586 | .matchsize = sizeof(struct xt_hashlimit_info), |
| 587 | #ifdef CONFIG_COMPAT |
| 588 | .compatsize = sizeof(struct compat_xt_hashlimit_info), |
| 589 | .compat_from_user = compat_from_user, |
| 590 | .compat_to_user = compat_to_user, |
| 591 | #endif |
| 592 | .checkentry = hashlimit_checkentry, |
| 593 | .destroy = hashlimit_destroy, |
| 594 | .me = THIS_MODULE |
| 595 | }, |
| 596 | { |
| 597 | .name = "hashlimit", |
| 598 | .family = AF_INET6, |
| 599 | .match = hashlimit_match, |
| 600 | .matchsize = sizeof(struct xt_hashlimit_info), |
| 601 | #ifdef CONFIG_COMPAT |
| 602 | .compatsize = sizeof(struct compat_xt_hashlimit_info), |
| 603 | .compat_from_user = compat_from_user, |
| 604 | .compat_to_user = compat_to_user, |
| 605 | #endif |
| 606 | .checkentry = hashlimit_checkentry, |
| 607 | .destroy = hashlimit_destroy, |
| 608 | .me = THIS_MODULE |
| 609 | }, |
| 610 | }; |
| 611 | |
| 612 | /* PROC stuff */ |
| 613 | static void *dl_seq_start(struct seq_file *s, loff_t *pos) |
| 614 | { |
| 615 | struct proc_dir_entry *pde = s->private; |
| 616 | struct xt_hashlimit_htable *htable = pde->data; |
| 617 | unsigned int *bucket; |
| 618 | |
| 619 | spin_lock_bh(&htable->lock); |
| 620 | if (*pos >= htable->cfg.size) |
| 621 | return NULL; |
| 622 | |
| 623 | bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC); |
| 624 | if (!bucket) |
| 625 | return ERR_PTR(-ENOMEM); |
| 626 | |
| 627 | *bucket = *pos; |
| 628 | return bucket; |
| 629 | } |
| 630 | |
| 631 | static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 632 | { |
| 633 | struct proc_dir_entry *pde = s->private; |
| 634 | struct xt_hashlimit_htable *htable = pde->data; |
| 635 | unsigned int *bucket = (unsigned int *)v; |
| 636 | |
| 637 | *pos = ++(*bucket); |
| 638 | if (*pos >= htable->cfg.size) { |
| 639 | kfree(v); |
| 640 | return NULL; |
| 641 | } |
| 642 | return bucket; |
| 643 | } |
| 644 | |
| 645 | static void dl_seq_stop(struct seq_file *s, void *v) |
| 646 | { |
| 647 | struct proc_dir_entry *pde = s->private; |
| 648 | struct xt_hashlimit_htable *htable = pde->data; |
| 649 | unsigned int *bucket = (unsigned int *)v; |
| 650 | |
| 651 | kfree(bucket); |
| 652 | spin_unlock_bh(&htable->lock); |
| 653 | } |
| 654 | |
| 655 | static int dl_seq_real_show(struct dsthash_ent *ent, int family, |
| 656 | struct seq_file *s) |
| 657 | { |
| 658 | /* recalculate to show accurate numbers */ |
| 659 | rateinfo_recalc(ent, jiffies); |
| 660 | |
| 661 | switch (family) { |
| 662 | case AF_INET: |
| 663 | return seq_printf(s, "%ld %u.%u.%u.%u:%u->" |
| 664 | "%u.%u.%u.%u:%u %u %u %u\n", |
| 665 | (long)(ent->expires - jiffies)/HZ, |
| 666 | NIPQUAD(ent->dst.addr.ip.src), |
| 667 | ntohs(ent->dst.src_port), |
| 668 | NIPQUAD(ent->dst.addr.ip.dst), |
| 669 | ntohs(ent->dst.dst_port), |
| 670 | ent->rateinfo.credit, ent->rateinfo.credit_cap, |
| 671 | ent->rateinfo.cost); |
| 672 | case AF_INET6: |
| 673 | return seq_printf(s, "%ld " NIP6_FMT ":%u->" |
| 674 | NIP6_FMT ":%u %u %u %u\n", |
| 675 | (long)(ent->expires - jiffies)/HZ, |
| 676 | NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.src), |
| 677 | ntohs(ent->dst.src_port), |
| 678 | NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.dst), |
| 679 | ntohs(ent->dst.dst_port), |
| 680 | ent->rateinfo.credit, ent->rateinfo.credit_cap, |
| 681 | ent->rateinfo.cost); |
| 682 | default: |
| 683 | BUG(); |
| 684 | return 0; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static int dl_seq_show(struct seq_file *s, void *v) |
| 689 | { |
| 690 | struct proc_dir_entry *pde = s->private; |
| 691 | struct xt_hashlimit_htable *htable = pde->data; |
| 692 | unsigned int *bucket = (unsigned int *)v; |
| 693 | struct dsthash_ent *ent; |
| 694 | struct hlist_node *pos; |
| 695 | |
| 696 | if (!hlist_empty(&htable->hash[*bucket])) { |
| 697 | hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node) |
| 698 | if (dl_seq_real_show(ent, htable->family, s)) |
| 699 | return 1; |
| 700 | } |
| 701 | return 0; |
| 702 | } |
| 703 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 704 | static const struct seq_operations dl_seq_ops = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 705 | .start = dl_seq_start, |
| 706 | .next = dl_seq_next, |
| 707 | .stop = dl_seq_stop, |
| 708 | .show = dl_seq_show |
| 709 | }; |
| 710 | |
| 711 | static int dl_proc_open(struct inode *inode, struct file *file) |
| 712 | { |
| 713 | int ret = seq_open(file, &dl_seq_ops); |
| 714 | |
| 715 | if (!ret) { |
| 716 | struct seq_file *sf = file->private_data; |
| 717 | sf->private = PDE(inode); |
| 718 | } |
| 719 | return ret; |
| 720 | } |
| 721 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 722 | static const struct file_operations dl_file_ops = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 723 | .owner = THIS_MODULE, |
| 724 | .open = dl_proc_open, |
| 725 | .read = seq_read, |
| 726 | .llseek = seq_lseek, |
| 727 | .release = seq_release |
| 728 | }; |
| 729 | |
| 730 | static int __init xt_hashlimit_init(void) |
| 731 | { |
| 732 | int err; |
| 733 | |
| 734 | err = xt_register_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit)); |
| 735 | if (err < 0) |
| 736 | goto err1; |
| 737 | |
| 738 | err = -ENOMEM; |
| 739 | hashlimit_cachep = kmem_cache_create("xt_hashlimit", |
| 740 | sizeof(struct dsthash_ent), 0, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame^] | 741 | NULL); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 742 | if (!hashlimit_cachep) { |
| 743 | printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n"); |
| 744 | goto err2; |
| 745 | } |
| 746 | hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", proc_net); |
| 747 | if (!hashlimit_procdir4) { |
| 748 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " |
| 749 | "entry\n"); |
| 750 | goto err3; |
| 751 | } |
| 752 | hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", proc_net); |
| 753 | if (!hashlimit_procdir6) { |
Alexey Dobriyan | 9c2440b | 2007-01-02 00:42:00 -0800 | [diff] [blame] | 754 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 755 | "entry\n"); |
| 756 | goto err4; |
| 757 | } |
| 758 | return 0; |
| 759 | err4: |
| 760 | remove_proc_entry("ipt_hashlimit", proc_net); |
| 761 | err3: |
| 762 | kmem_cache_destroy(hashlimit_cachep); |
| 763 | err2: |
| 764 | xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit)); |
| 765 | err1: |
| 766 | return err; |
| 767 | |
| 768 | } |
| 769 | |
| 770 | static void __exit xt_hashlimit_fini(void) |
| 771 | { |
| 772 | remove_proc_entry("ipt_hashlimit", proc_net); |
| 773 | remove_proc_entry("ip6t_hashlimit", proc_net); |
| 774 | kmem_cache_destroy(hashlimit_cachep); |
| 775 | xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit)); |
| 776 | } |
| 777 | |
| 778 | module_init(xt_hashlimit_init); |
| 779 | module_exit(xt_hashlimit_fini); |