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