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