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