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