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