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