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