Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at d430a227d272fa514bade388bf511dba4ec2962a 1038 lines 29 kB view raw
1/* 2 * xt_hashlimit - Netfilter module to limit the number of packets per time 3 * seperately for each hashbucket (sourceip/sourceport/dstip/dstport) 4 * 5 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org> 6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008 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> 20#include <linux/mm.h> 21#include <linux/in.h> 22#include <linux/ip.h> 23#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 24#include <linux/ipv6.h> 25#include <net/ipv6.h> 26#endif 27 28#include <net/net_namespace.h> 29 30#include <linux/netfilter/x_tables.h> 31#include <linux/netfilter_ipv4/ip_tables.h> 32#include <linux/netfilter_ipv6/ip6_tables.h> 33#include <linux/netfilter/xt_hashlimit.h> 34#include <linux/mutex.h> 35 36MODULE_LICENSE("GPL"); 37MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); 38MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>"); 39MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match"); 40MODULE_ALIAS("ipt_hashlimit"); 41MODULE_ALIAS("ip6t_hashlimit"); 42 43/* need to declare this at the top */ 44static struct proc_dir_entry *hashlimit_procdir4; 45static struct proc_dir_entry *hashlimit_procdir6; 46static const struct file_operations dl_file_ops; 47 48/* hash table crap */ 49struct dsthash_dst { 50 union { 51 struct { 52 __be32 src; 53 __be32 dst; 54 } ip; 55#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 56 struct { 57 __be32 src[4]; 58 __be32 dst[4]; 59 } ip6; 60#endif 61 }; 62 __be16 src_port; 63 __be16 dst_port; 64}; 65 66struct dsthash_ent { 67 /* static / read-only parts in the beginning */ 68 struct hlist_node node; 69 struct dsthash_dst dst; 70 71 /* modified structure members in the end */ 72 unsigned long expires; /* precalculated expiry time */ 73 struct { 74 unsigned long prev; /* last modification */ 75 u_int32_t credit; 76 u_int32_t credit_cap, cost; 77 } rateinfo; 78}; 79 80struct xt_hashlimit_htable { 81 struct hlist_node node; /* global list of all htables */ 82 atomic_t use; 83 int family; 84 85 struct hashlimit_cfg1 cfg; /* config */ 86 87 /* used internally */ 88 spinlock_t lock; /* lock for list_head */ 89 u_int32_t rnd; /* random seed for hash */ 90 int rnd_initialized; 91 unsigned int count; /* number entries in table */ 92 struct timer_list timer; /* timer for gc */ 93 94 /* seq_file stuff */ 95 struct proc_dir_entry *pde; 96 97 struct hlist_head hash[0]; /* hashtable itself */ 98}; 99 100static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */ 101static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */ 102static HLIST_HEAD(hashlimit_htables); 103static struct kmem_cache *hashlimit_cachep __read_mostly; 104 105static inline bool dst_cmp(const struct dsthash_ent *ent, 106 const struct dsthash_dst *b) 107{ 108 return !memcmp(&ent->dst, b, sizeof(ent->dst)); 109} 110 111static u_int32_t 112hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst) 113{ 114 u_int32_t hash = jhash2((const u32 *)dst, 115 sizeof(*dst)/sizeof(u32), 116 ht->rnd); 117 /* 118 * Instead of returning hash % ht->cfg.size (implying a divide) 119 * we return the high 32 bits of the (hash * ht->cfg.size) that will 120 * give results between [0 and cfg.size-1] and same hash distribution, 121 * but using a multiply, less expensive than a divide 122 */ 123 return ((u64)hash * ht->cfg.size) >> 32; 124} 125 126static struct dsthash_ent * 127dsthash_find(const struct xt_hashlimit_htable *ht, 128 const struct dsthash_dst *dst) 129{ 130 struct dsthash_ent *ent; 131 struct hlist_node *pos; 132 u_int32_t hash = hash_dst(ht, dst); 133 134 if (!hlist_empty(&ht->hash[hash])) { 135 hlist_for_each_entry(ent, pos, &ht->hash[hash], node) 136 if (dst_cmp(ent, dst)) 137 return ent; 138 } 139 return NULL; 140} 141 142/* allocate dsthash_ent, initialize dst, put in htable and lock it */ 143static struct dsthash_ent * 144dsthash_alloc_init(struct xt_hashlimit_htable *ht, 145 const struct dsthash_dst *dst) 146{ 147 struct dsthash_ent *ent; 148 149 /* initialize hash with random val at the time we allocate 150 * the first hashtable entry */ 151 if (!ht->rnd_initialized) { 152 get_random_bytes(&ht->rnd, 4); 153 ht->rnd_initialized = 1; 154 } 155 156 if (ht->cfg.max && ht->count >= ht->cfg.max) { 157 /* FIXME: do something. question is what.. */ 158 if (net_ratelimit()) 159 printk(KERN_WARNING 160 "xt_hashlimit: max count of %u reached\n", 161 ht->cfg.max); 162 return NULL; 163 } 164 165 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC); 166 if (!ent) { 167 if (net_ratelimit()) 168 printk(KERN_ERR 169 "xt_hashlimit: can't allocate dsthash_ent\n"); 170 return NULL; 171 } 172 memcpy(&ent->dst, dst, sizeof(ent->dst)); 173 174 hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]); 175 ht->count++; 176 return ent; 177} 178 179static inline void 180dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent) 181{ 182 hlist_del(&ent->node); 183 kmem_cache_free(hashlimit_cachep, ent); 184 ht->count--; 185} 186static void htable_gc(unsigned long htlong); 187 188static int htable_create_v0(struct xt_hashlimit_info *minfo, int family) 189{ 190 struct xt_hashlimit_htable *hinfo; 191 unsigned int size; 192 unsigned int i; 193 194 if (minfo->cfg.size) 195 size = minfo->cfg.size; 196 else { 197 size = ((num_physpages << PAGE_SHIFT) / 16384) / 198 sizeof(struct list_head); 199 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) 200 size = 8192; 201 if (size < 16) 202 size = 16; 203 } 204 /* FIXME: don't use vmalloc() here or anywhere else -HW */ 205 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + 206 sizeof(struct list_head) * size); 207 if (!hinfo) { 208 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); 209 return -1; 210 } 211 minfo->hinfo = hinfo; 212 213 /* copy match config into hashtable config */ 214 hinfo->cfg.mode = minfo->cfg.mode; 215 hinfo->cfg.avg = minfo->cfg.avg; 216 hinfo->cfg.burst = minfo->cfg.burst; 217 hinfo->cfg.max = minfo->cfg.max; 218 hinfo->cfg.gc_interval = minfo->cfg.gc_interval; 219 hinfo->cfg.expire = minfo->cfg.expire; 220 221 if (family == AF_INET) 222 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 32; 223 else 224 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 128; 225 226 hinfo->cfg.size = size; 227 if (!hinfo->cfg.max) 228 hinfo->cfg.max = 8 * hinfo->cfg.size; 229 else if (hinfo->cfg.max < hinfo->cfg.size) 230 hinfo->cfg.max = hinfo->cfg.size; 231 232 for (i = 0; i < hinfo->cfg.size; i++) 233 INIT_HLIST_HEAD(&hinfo->hash[i]); 234 235 atomic_set(&hinfo->use, 1); 236 hinfo->count = 0; 237 hinfo->family = family; 238 hinfo->rnd_initialized = 0; 239 spin_lock_init(&hinfo->lock); 240 hinfo->pde = 241 proc_create_data(minfo->name, 0, 242 family == AF_INET ? hashlimit_procdir4 : 243 hashlimit_procdir6, 244 &dl_file_ops, hinfo); 245 if (!hinfo->pde) { 246 vfree(hinfo); 247 return -1; 248 } 249 250 setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo); 251 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); 252 add_timer(&hinfo->timer); 253 254 spin_lock_bh(&hashlimit_lock); 255 hlist_add_head(&hinfo->node, &hashlimit_htables); 256 spin_unlock_bh(&hashlimit_lock); 257 258 return 0; 259} 260 261static int htable_create(struct xt_hashlimit_mtinfo1 *minfo, 262 unsigned int family) 263{ 264 struct xt_hashlimit_htable *hinfo; 265 unsigned int size; 266 unsigned int i; 267 268 if (minfo->cfg.size) { 269 size = minfo->cfg.size; 270 } else { 271 size = (num_physpages << PAGE_SHIFT) / 16384 / 272 sizeof(struct list_head); 273 if (num_physpages > 1024 * 1024 * 1024 / PAGE_SIZE) 274 size = 8192; 275 if (size < 16) 276 size = 16; 277 } 278 /* FIXME: don't use vmalloc() here or anywhere else -HW */ 279 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + 280 sizeof(struct list_head) * size); 281 if (hinfo == NULL) { 282 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); 283 return -1; 284 } 285 minfo->hinfo = hinfo; 286 287 /* copy match config into hashtable config */ 288 memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg)); 289 hinfo->cfg.size = size; 290 if (hinfo->cfg.max == 0) 291 hinfo->cfg.max = 8 * hinfo->cfg.size; 292 else if (hinfo->cfg.max < hinfo->cfg.size) 293 hinfo->cfg.max = hinfo->cfg.size; 294 295 for (i = 0; i < hinfo->cfg.size; i++) 296 INIT_HLIST_HEAD(&hinfo->hash[i]); 297 298 atomic_set(&hinfo->use, 1); 299 hinfo->count = 0; 300 hinfo->family = family; 301 hinfo->rnd_initialized = 0; 302 spin_lock_init(&hinfo->lock); 303 304 hinfo->pde = 305 proc_create_data(minfo->name, 0, 306 family == AF_INET ? hashlimit_procdir4 : 307 hashlimit_procdir6, 308 &dl_file_ops, hinfo); 309 if (hinfo->pde == NULL) { 310 vfree(hinfo); 311 return -1; 312 } 313 314 setup_timer(&hinfo->timer, htable_gc, (unsigned long)hinfo); 315 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); 316 add_timer(&hinfo->timer); 317 318 spin_lock_bh(&hashlimit_lock); 319 hlist_add_head(&hinfo->node, &hashlimit_htables); 320 spin_unlock_bh(&hashlimit_lock); 321 322 return 0; 323} 324 325static bool select_all(const struct xt_hashlimit_htable *ht, 326 const struct dsthash_ent *he) 327{ 328 return 1; 329} 330 331static bool select_gc(const struct xt_hashlimit_htable *ht, 332 const struct dsthash_ent *he) 333{ 334 return time_after_eq(jiffies, he->expires); 335} 336 337static void htable_selective_cleanup(struct xt_hashlimit_htable *ht, 338 bool (*select)(const struct xt_hashlimit_htable *ht, 339 const struct dsthash_ent *he)) 340{ 341 unsigned int i; 342 343 /* lock hash table and iterate over it */ 344 spin_lock_bh(&ht->lock); 345 for (i = 0; i < ht->cfg.size; i++) { 346 struct dsthash_ent *dh; 347 struct hlist_node *pos, *n; 348 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) { 349 if ((*select)(ht, dh)) 350 dsthash_free(ht, dh); 351 } 352 } 353 spin_unlock_bh(&ht->lock); 354} 355 356/* hash table garbage collector, run by timer */ 357static void htable_gc(unsigned long htlong) 358{ 359 struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong; 360 361 htable_selective_cleanup(ht, select_gc); 362 363 /* re-add the timer accordingly */ 364 ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval); 365 add_timer(&ht->timer); 366} 367 368static void htable_destroy(struct xt_hashlimit_htable *hinfo) 369{ 370 /* remove timer, if it is pending */ 371 if (timer_pending(&hinfo->timer)) 372 del_timer(&hinfo->timer); 373 374 /* remove proc entry */ 375 remove_proc_entry(hinfo->pde->name, 376 hinfo->family == AF_INET ? hashlimit_procdir4 : 377 hashlimit_procdir6); 378 htable_selective_cleanup(hinfo, select_all); 379 vfree(hinfo); 380} 381 382static struct xt_hashlimit_htable *htable_find_get(const char *name, 383 int family) 384{ 385 struct xt_hashlimit_htable *hinfo; 386 struct hlist_node *pos; 387 388 spin_lock_bh(&hashlimit_lock); 389 hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) { 390 if (!strcmp(name, hinfo->pde->name) && 391 hinfo->family == family) { 392 atomic_inc(&hinfo->use); 393 spin_unlock_bh(&hashlimit_lock); 394 return hinfo; 395 } 396 } 397 spin_unlock_bh(&hashlimit_lock); 398 return NULL; 399} 400 401static void htable_put(struct xt_hashlimit_htable *hinfo) 402{ 403 if (atomic_dec_and_test(&hinfo->use)) { 404 spin_lock_bh(&hashlimit_lock); 405 hlist_del(&hinfo->node); 406 spin_unlock_bh(&hashlimit_lock); 407 htable_destroy(hinfo); 408 } 409} 410 411/* The algorithm used is the Simple Token Bucket Filter (TBF) 412 * see net/sched/sch_tbf.c in the linux source tree 413 */ 414 415/* Rusty: This is my (non-mathematically-inclined) understanding of 416 this algorithm. The `average rate' in jiffies becomes your initial 417 amount of credit `credit' and the most credit you can ever have 418 `credit_cap'. The `peak rate' becomes the cost of passing the 419 test, `cost'. 420 421 `prev' tracks the last packet hit: you gain one credit per jiffy. 422 If you get credit balance more than this, the extra credit is 423 discarded. Every time the match passes, you lose `cost' credits; 424 if you don't have that many, the test fails. 425 426 See Alexey's formal explanation in net/sched/sch_tbf.c. 427 428 To get the maximum range, we multiply by this factor (ie. you get N 429 credits per jiffy). We want to allow a rate as low as 1 per day 430 (slowest userspace tool allows), which means 431 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie. 432*/ 433#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) 434 435/* Repeated shift and or gives us all 1s, final shift and add 1 gives 436 * us the power of 2 below the theoretical max, so GCC simply does a 437 * shift. */ 438#define _POW2_BELOW2(x) ((x)|((x)>>1)) 439#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) 440#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) 441#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) 442#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) 443#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) 444 445#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) 446 447/* Precision saver. */ 448static inline u_int32_t 449user2credits(u_int32_t user) 450{ 451 /* If multiplying would overflow... */ 452 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) 453 /* Divide first. */ 454 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; 455 456 return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE; 457} 458 459static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) 460{ 461 dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY; 462 if (dh->rateinfo.credit > dh->rateinfo.credit_cap) 463 dh->rateinfo.credit = dh->rateinfo.credit_cap; 464 dh->rateinfo.prev = now; 465} 466 467static inline __be32 maskl(__be32 a, unsigned int l) 468{ 469 return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0; 470} 471 472#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 473static void hashlimit_ipv6_mask(__be32 *i, unsigned int p) 474{ 475 switch (p) { 476 case 0 ... 31: 477 i[0] = maskl(i[0], p); 478 i[1] = i[2] = i[3] = 0; 479 break; 480 case 32 ... 63: 481 i[1] = maskl(i[1], p - 32); 482 i[2] = i[3] = 0; 483 break; 484 case 64 ... 95: 485 i[2] = maskl(i[2], p - 64); 486 i[3] = 0; 487 case 96 ... 127: 488 i[3] = maskl(i[3], p - 96); 489 break; 490 case 128: 491 break; 492 } 493} 494#endif 495 496static int 497hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo, 498 struct dsthash_dst *dst, 499 const struct sk_buff *skb, unsigned int protoff) 500{ 501 __be16 _ports[2], *ports; 502 u8 nexthdr; 503 504 memset(dst, 0, sizeof(*dst)); 505 506 switch (hinfo->family) { 507 case AF_INET: 508 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) 509 dst->ip.dst = maskl(ip_hdr(skb)->daddr, 510 hinfo->cfg.dstmask); 511 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) 512 dst->ip.src = maskl(ip_hdr(skb)->saddr, 513 hinfo->cfg.srcmask); 514 515 if (!(hinfo->cfg.mode & 516 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) 517 return 0; 518 nexthdr = ip_hdr(skb)->protocol; 519 break; 520#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 521 case AF_INET6: 522 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) { 523 memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr, 524 sizeof(dst->ip6.dst)); 525 hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask); 526 } 527 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) { 528 memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr, 529 sizeof(dst->ip6.src)); 530 hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask); 531 } 532 533 if (!(hinfo->cfg.mode & 534 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) 535 return 0; 536 nexthdr = ipv6_hdr(skb)->nexthdr; 537 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr); 538 if ((int)protoff < 0) 539 return -1; 540 break; 541#endif 542 default: 543 BUG(); 544 return 0; 545 } 546 547 switch (nexthdr) { 548 case IPPROTO_TCP: 549 case IPPROTO_UDP: 550 case IPPROTO_UDPLITE: 551 case IPPROTO_SCTP: 552 case IPPROTO_DCCP: 553 ports = skb_header_pointer(skb, protoff, sizeof(_ports), 554 &_ports); 555 break; 556 default: 557 _ports[0] = _ports[1] = 0; 558 ports = _ports; 559 break; 560 } 561 if (!ports) 562 return -1; 563 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT) 564 dst->src_port = ports[0]; 565 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT) 566 dst->dst_port = ports[1]; 567 return 0; 568} 569 570static bool 571hashlimit_mt_v0(const struct sk_buff *skb, const struct net_device *in, 572 const struct net_device *out, const struct xt_match *match, 573 const void *matchinfo, int offset, unsigned int protoff, 574 bool *hotdrop) 575{ 576 const struct xt_hashlimit_info *r = 577 ((const struct xt_hashlimit_info *)matchinfo)->u.master; 578 struct xt_hashlimit_htable *hinfo = r->hinfo; 579 unsigned long now = jiffies; 580 struct dsthash_ent *dh; 581 struct dsthash_dst dst; 582 583 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) 584 goto hotdrop; 585 586 spin_lock_bh(&hinfo->lock); 587 dh = dsthash_find(hinfo, &dst); 588 if (!dh) { 589 dh = dsthash_alloc_init(hinfo, &dst); 590 if (!dh) { 591 spin_unlock_bh(&hinfo->lock); 592 goto hotdrop; 593 } 594 595 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); 596 dh->rateinfo.prev = jiffies; 597 dh->rateinfo.credit = user2credits(hinfo->cfg.avg * 598 hinfo->cfg.burst); 599 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * 600 hinfo->cfg.burst); 601 dh->rateinfo.cost = user2credits(hinfo->cfg.avg); 602 } else { 603 /* update expiration timeout */ 604 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); 605 rateinfo_recalc(dh, now); 606 } 607 608 if (dh->rateinfo.credit >= dh->rateinfo.cost) { 609 /* We're underlimit. */ 610 dh->rateinfo.credit -= dh->rateinfo.cost; 611 spin_unlock_bh(&hinfo->lock); 612 return true; 613 } 614 615 spin_unlock_bh(&hinfo->lock); 616 617 /* default case: we're overlimit, thus don't match */ 618 return false; 619 620hotdrop: 621 *hotdrop = true; 622 return false; 623} 624 625static bool 626hashlimit_mt(const struct sk_buff *skb, const struct net_device *in, 627 const struct net_device *out, const struct xt_match *match, 628 const void *matchinfo, int offset, unsigned int protoff, 629 bool *hotdrop) 630{ 631 const struct xt_hashlimit_mtinfo1 *info = matchinfo; 632 struct xt_hashlimit_htable *hinfo = info->hinfo; 633 unsigned long now = jiffies; 634 struct dsthash_ent *dh; 635 struct dsthash_dst dst; 636 637 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) 638 goto hotdrop; 639 640 spin_lock_bh(&hinfo->lock); 641 dh = dsthash_find(hinfo, &dst); 642 if (dh == NULL) { 643 dh = dsthash_alloc_init(hinfo, &dst); 644 if (dh == NULL) { 645 spin_unlock_bh(&hinfo->lock); 646 goto hotdrop; 647 } 648 649 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); 650 dh->rateinfo.prev = jiffies; 651 dh->rateinfo.credit = user2credits(hinfo->cfg.avg * 652 hinfo->cfg.burst); 653 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * 654 hinfo->cfg.burst); 655 dh->rateinfo.cost = user2credits(hinfo->cfg.avg); 656 } else { 657 /* update expiration timeout */ 658 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); 659 rateinfo_recalc(dh, now); 660 } 661 662 if (dh->rateinfo.credit >= dh->rateinfo.cost) { 663 /* below the limit */ 664 dh->rateinfo.credit -= dh->rateinfo.cost; 665 spin_unlock_bh(&hinfo->lock); 666 return !(info->cfg.mode & XT_HASHLIMIT_INVERT); 667 } 668 669 spin_unlock_bh(&hinfo->lock); 670 /* default match is underlimit - so over the limit, we need to invert */ 671 return info->cfg.mode & XT_HASHLIMIT_INVERT; 672 673 hotdrop: 674 *hotdrop = true; 675 return false; 676} 677 678static bool 679hashlimit_mt_check_v0(const char *tablename, const void *inf, 680 const struct xt_match *match, void *matchinfo, 681 unsigned int hook_mask) 682{ 683 struct xt_hashlimit_info *r = matchinfo; 684 685 /* Check for overflow. */ 686 if (r->cfg.burst == 0 || 687 user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) { 688 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", 689 r->cfg.avg, r->cfg.burst); 690 return false; 691 } 692 if (r->cfg.mode == 0 || 693 r->cfg.mode > (XT_HASHLIMIT_HASH_DPT | 694 XT_HASHLIMIT_HASH_DIP | 695 XT_HASHLIMIT_HASH_SIP | 696 XT_HASHLIMIT_HASH_SPT)) 697 return false; 698 if (!r->cfg.gc_interval) 699 return false; 700 if (!r->cfg.expire) 701 return false; 702 if (r->name[sizeof(r->name) - 1] != '\0') 703 return false; 704 705 /* This is the best we've got: We cannot release and re-grab lock, 706 * since checkentry() is called before x_tables.c grabs xt_mutex. 707 * We also cannot grab the hashtable spinlock, since htable_create will 708 * call vmalloc, and that can sleep. And we cannot just re-search 709 * the list of htable's in htable_create(), since then we would 710 * create duplicate proc files. -HW */ 711 mutex_lock(&hlimit_mutex); 712 r->hinfo = htable_find_get(r->name, match->family); 713 if (!r->hinfo && htable_create_v0(r, match->family) != 0) { 714 mutex_unlock(&hlimit_mutex); 715 return false; 716 } 717 mutex_unlock(&hlimit_mutex); 718 719 /* Ugly hack: For SMP, we only want to use one set */ 720 r->u.master = r; 721 return true; 722} 723 724static bool 725hashlimit_mt_check(const char *tablename, const void *inf, 726 const struct xt_match *match, void *matchinfo, 727 unsigned int hook_mask) 728{ 729 struct xt_hashlimit_mtinfo1 *info = matchinfo; 730 731 /* Check for overflow. */ 732 if (info->cfg.burst == 0 || 733 user2credits(info->cfg.avg * info->cfg.burst) < 734 user2credits(info->cfg.avg)) { 735 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", 736 info->cfg.avg, info->cfg.burst); 737 return false; 738 } 739 if (info->cfg.gc_interval == 0 || info->cfg.expire == 0) 740 return false; 741 if (info->name[sizeof(info->name)-1] != '\0') 742 return false; 743 if (match->family == AF_INET) { 744 if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32) 745 return false; 746 } else { 747 if (info->cfg.srcmask > 128 || info->cfg.dstmask > 128) 748 return false; 749 } 750 751 /* This is the best we've got: We cannot release and re-grab lock, 752 * since checkentry() is called before x_tables.c grabs xt_mutex. 753 * We also cannot grab the hashtable spinlock, since htable_create will 754 * call vmalloc, and that can sleep. And we cannot just re-search 755 * the list of htable's in htable_create(), since then we would 756 * create duplicate proc files. -HW */ 757 mutex_lock(&hlimit_mutex); 758 info->hinfo = htable_find_get(info->name, match->family); 759 if (!info->hinfo && htable_create(info, match->family) != 0) { 760 mutex_unlock(&hlimit_mutex); 761 return false; 762 } 763 mutex_unlock(&hlimit_mutex); 764 return true; 765} 766 767static void 768hashlimit_mt_destroy_v0(const struct xt_match *match, void *matchinfo) 769{ 770 const struct xt_hashlimit_info *r = matchinfo; 771 772 htable_put(r->hinfo); 773} 774 775static void 776hashlimit_mt_destroy(const struct xt_match *match, void *matchinfo) 777{ 778 const struct xt_hashlimit_mtinfo1 *info = matchinfo; 779 780 htable_put(info->hinfo); 781} 782 783#ifdef CONFIG_COMPAT 784struct compat_xt_hashlimit_info { 785 char name[IFNAMSIZ]; 786 struct hashlimit_cfg cfg; 787 compat_uptr_t hinfo; 788 compat_uptr_t master; 789}; 790 791static void hashlimit_mt_compat_from_user(void *dst, void *src) 792{ 793 int off = offsetof(struct compat_xt_hashlimit_info, hinfo); 794 795 memcpy(dst, src, off); 796 memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off); 797} 798 799static int hashlimit_mt_compat_to_user(void __user *dst, void *src) 800{ 801 int off = offsetof(struct compat_xt_hashlimit_info, hinfo); 802 803 return copy_to_user(dst, src, off) ? -EFAULT : 0; 804} 805#endif 806 807static struct xt_match hashlimit_mt_reg[] __read_mostly = { 808 { 809 .name = "hashlimit", 810 .revision = 0, 811 .family = AF_INET, 812 .match = hashlimit_mt_v0, 813 .matchsize = sizeof(struct xt_hashlimit_info), 814#ifdef CONFIG_COMPAT 815 .compatsize = sizeof(struct compat_xt_hashlimit_info), 816 .compat_from_user = hashlimit_mt_compat_from_user, 817 .compat_to_user = hashlimit_mt_compat_to_user, 818#endif 819 .checkentry = hashlimit_mt_check_v0, 820 .destroy = hashlimit_mt_destroy_v0, 821 .me = THIS_MODULE 822 }, 823 { 824 .name = "hashlimit", 825 .revision = 1, 826 .family = AF_INET, 827 .match = hashlimit_mt, 828 .matchsize = sizeof(struct xt_hashlimit_mtinfo1), 829 .checkentry = hashlimit_mt_check, 830 .destroy = hashlimit_mt_destroy, 831 .me = THIS_MODULE, 832 }, 833#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 834 { 835 .name = "hashlimit", 836 .family = AF_INET6, 837 .match = hashlimit_mt_v0, 838 .matchsize = sizeof(struct xt_hashlimit_info), 839#ifdef CONFIG_COMPAT 840 .compatsize = sizeof(struct compat_xt_hashlimit_info), 841 .compat_from_user = hashlimit_mt_compat_from_user, 842 .compat_to_user = hashlimit_mt_compat_to_user, 843#endif 844 .checkentry = hashlimit_mt_check_v0, 845 .destroy = hashlimit_mt_destroy_v0, 846 .me = THIS_MODULE 847 }, 848 { 849 .name = "hashlimit", 850 .revision = 1, 851 .family = AF_INET6, 852 .match = hashlimit_mt, 853 .matchsize = sizeof(struct xt_hashlimit_mtinfo1), 854 .checkentry = hashlimit_mt_check, 855 .destroy = hashlimit_mt_destroy, 856 .me = THIS_MODULE, 857 }, 858#endif 859}; 860 861/* PROC stuff */ 862static void *dl_seq_start(struct seq_file *s, loff_t *pos) 863 __acquires(htable->lock) 864{ 865 struct proc_dir_entry *pde = s->private; 866 struct xt_hashlimit_htable *htable = pde->data; 867 unsigned int *bucket; 868 869 spin_lock_bh(&htable->lock); 870 if (*pos >= htable->cfg.size) 871 return NULL; 872 873 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC); 874 if (!bucket) 875 return ERR_PTR(-ENOMEM); 876 877 *bucket = *pos; 878 return bucket; 879} 880 881static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos) 882{ 883 struct proc_dir_entry *pde = s->private; 884 struct xt_hashlimit_htable *htable = pde->data; 885 unsigned int *bucket = (unsigned int *)v; 886 887 *pos = ++(*bucket); 888 if (*pos >= htable->cfg.size) { 889 kfree(v); 890 return NULL; 891 } 892 return bucket; 893} 894 895static void dl_seq_stop(struct seq_file *s, void *v) 896 __releases(htable->lock) 897{ 898 struct proc_dir_entry *pde = s->private; 899 struct xt_hashlimit_htable *htable = pde->data; 900 unsigned int *bucket = (unsigned int *)v; 901 902 kfree(bucket); 903 spin_unlock_bh(&htable->lock); 904} 905 906static int dl_seq_real_show(struct dsthash_ent *ent, int family, 907 struct seq_file *s) 908{ 909 /* recalculate to show accurate numbers */ 910 rateinfo_recalc(ent, jiffies); 911 912 switch (family) { 913 case AF_INET: 914 return seq_printf(s, "%ld %u.%u.%u.%u:%u->" 915 "%u.%u.%u.%u:%u %u %u %u\n", 916 (long)(ent->expires - jiffies)/HZ, 917 NIPQUAD(ent->dst.ip.src), 918 ntohs(ent->dst.src_port), 919 NIPQUAD(ent->dst.ip.dst), 920 ntohs(ent->dst.dst_port), 921 ent->rateinfo.credit, ent->rateinfo.credit_cap, 922 ent->rateinfo.cost); 923#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 924 case AF_INET6: 925 return seq_printf(s, "%ld " NIP6_FMT ":%u->" 926 NIP6_FMT ":%u %u %u %u\n", 927 (long)(ent->expires - jiffies)/HZ, 928 NIP6(*(struct in6_addr *)&ent->dst.ip6.src), 929 ntohs(ent->dst.src_port), 930 NIP6(*(struct in6_addr *)&ent->dst.ip6.dst), 931 ntohs(ent->dst.dst_port), 932 ent->rateinfo.credit, ent->rateinfo.credit_cap, 933 ent->rateinfo.cost); 934#endif 935 default: 936 BUG(); 937 return 0; 938 } 939} 940 941static int dl_seq_show(struct seq_file *s, void *v) 942{ 943 struct proc_dir_entry *pde = s->private; 944 struct xt_hashlimit_htable *htable = pde->data; 945 unsigned int *bucket = (unsigned int *)v; 946 struct dsthash_ent *ent; 947 struct hlist_node *pos; 948 949 if (!hlist_empty(&htable->hash[*bucket])) { 950 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node) 951 if (dl_seq_real_show(ent, htable->family, s)) 952 return 1; 953 } 954 return 0; 955} 956 957static const struct seq_operations dl_seq_ops = { 958 .start = dl_seq_start, 959 .next = dl_seq_next, 960 .stop = dl_seq_stop, 961 .show = dl_seq_show 962}; 963 964static int dl_proc_open(struct inode *inode, struct file *file) 965{ 966 int ret = seq_open(file, &dl_seq_ops); 967 968 if (!ret) { 969 struct seq_file *sf = file->private_data; 970 sf->private = PDE(inode); 971 } 972 return ret; 973} 974 975static const struct file_operations dl_file_ops = { 976 .owner = THIS_MODULE, 977 .open = dl_proc_open, 978 .read = seq_read, 979 .llseek = seq_lseek, 980 .release = seq_release 981}; 982 983static int __init hashlimit_mt_init(void) 984{ 985 int err; 986 987 err = xt_register_matches(hashlimit_mt_reg, 988 ARRAY_SIZE(hashlimit_mt_reg)); 989 if (err < 0) 990 goto err1; 991 992 err = -ENOMEM; 993 hashlimit_cachep = kmem_cache_create("xt_hashlimit", 994 sizeof(struct dsthash_ent), 0, 0, 995 NULL); 996 if (!hashlimit_cachep) { 997 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n"); 998 goto err2; 999 } 1000 hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", init_net.proc_net); 1001 if (!hashlimit_procdir4) { 1002 printk(KERN_ERR "xt_hashlimit: unable to create proc dir " 1003 "entry\n"); 1004 goto err3; 1005 } 1006 err = 0; 1007#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 1008 hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", init_net.proc_net); 1009 if (!hashlimit_procdir6) { 1010 printk(KERN_ERR "xt_hashlimit: unable to create proc dir " 1011 "entry\n"); 1012 err = -ENOMEM; 1013 } 1014#endif 1015 if (!err) 1016 return 0; 1017 remove_proc_entry("ipt_hashlimit", init_net.proc_net); 1018err3: 1019 kmem_cache_destroy(hashlimit_cachep); 1020err2: 1021 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); 1022err1: 1023 return err; 1024 1025} 1026 1027static void __exit hashlimit_mt_exit(void) 1028{ 1029 remove_proc_entry("ipt_hashlimit", init_net.proc_net); 1030#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) 1031 remove_proc_entry("ip6t_hashlimit", init_net.proc_net); 1032#endif 1033 kmem_cache_destroy(hashlimit_cachep); 1034 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); 1035} 1036 1037module_init(hashlimit_mt_init); 1038module_exit(hashlimit_mt_exit);