Driver core: Fix cleanup when failing device_add().
[wrt350n-kernel.git] / net / ipv4 / netfilter / ipt_recent.c
blob68cbe3ca01ced13ea0cc77d7d3f6057fa63855d3
1 /*
2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This is a replacement of the old ipt_recent module, which carried the
9 * following copyright notice:
11 * Author: Stephen Frost <sfrost@snowman.net>
12 * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
14 #include <linux/init.h>
15 #include <linux/ip.h>
16 #include <linux/moduleparam.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/list.h>
22 #include <linux/random.h>
23 #include <linux/jhash.h>
24 #include <linux/bitops.h>
25 #include <linux/skbuff.h>
26 #include <linux/inet.h>
27 #include <net/net_namespace.h>
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_ipv4/ipt_recent.h>
32 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
33 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
34 MODULE_LICENSE("GPL");
36 static unsigned int ip_list_tot = 100;
37 static unsigned int ip_pkt_list_tot = 20;
38 static unsigned int ip_list_hash_size = 0;
39 static unsigned int ip_list_perms = 0644;
40 static unsigned int ip_list_uid = 0;
41 static unsigned int ip_list_gid = 0;
42 module_param(ip_list_tot, uint, 0400);
43 module_param(ip_pkt_list_tot, uint, 0400);
44 module_param(ip_list_hash_size, uint, 0400);
45 module_param(ip_list_perms, uint, 0400);
46 module_param(ip_list_uid, uint, 0400);
47 module_param(ip_list_gid, uint, 0400);
48 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
49 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
50 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
51 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
52 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/ipt_recent/* files");
53 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/ipt_recent/* files");
55 struct recent_entry {
56 struct list_head list;
57 struct list_head lru_list;
58 __be32 addr;
59 u_int8_t ttl;
60 u_int8_t index;
61 u_int16_t nstamps;
62 unsigned long stamps[0];
65 struct recent_table {
66 struct list_head list;
67 char name[IPT_RECENT_NAME_LEN];
68 #ifdef CONFIG_PROC_FS
69 struct proc_dir_entry *proc;
70 #endif
71 unsigned int refcnt;
72 unsigned int entries;
73 struct list_head lru_list;
74 struct list_head iphash[0];
77 static LIST_HEAD(tables);
78 static DEFINE_SPINLOCK(recent_lock);
79 static DEFINE_MUTEX(recent_mutex);
81 #ifdef CONFIG_PROC_FS
82 static struct proc_dir_entry *proc_dir;
83 static const struct file_operations recent_fops;
84 #endif
86 static u_int32_t hash_rnd;
87 static int hash_rnd_initted;
89 static unsigned int recent_entry_hash(__be32 addr)
91 if (!hash_rnd_initted) {
92 get_random_bytes(&hash_rnd, 4);
93 hash_rnd_initted = 1;
95 return jhash_1word((__force u32)addr, hash_rnd) & (ip_list_hash_size - 1);
98 static struct recent_entry *
99 recent_entry_lookup(const struct recent_table *table, __be32 addr, u_int8_t ttl)
101 struct recent_entry *e;
102 unsigned int h;
104 h = recent_entry_hash(addr);
105 list_for_each_entry(e, &table->iphash[h], list)
106 if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl))
107 return e;
108 return NULL;
111 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
113 list_del(&e->list);
114 list_del(&e->lru_list);
115 kfree(e);
116 t->entries--;
119 static struct recent_entry *
120 recent_entry_init(struct recent_table *t, __be32 addr, u_int8_t ttl)
122 struct recent_entry *e;
124 if (t->entries >= ip_list_tot) {
125 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
126 recent_entry_remove(t, e);
128 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
129 GFP_ATOMIC);
130 if (e == NULL)
131 return NULL;
132 e->addr = addr;
133 e->ttl = ttl;
134 e->stamps[0] = jiffies;
135 e->nstamps = 1;
136 e->index = 1;
137 list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
138 list_add_tail(&e->lru_list, &t->lru_list);
139 t->entries++;
140 return e;
143 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
145 e->stamps[e->index++] = jiffies;
146 if (e->index > e->nstamps)
147 e->nstamps = e->index;
148 e->index %= ip_pkt_list_tot;
149 list_move_tail(&e->lru_list, &t->lru_list);
152 static struct recent_table *recent_table_lookup(const char *name)
154 struct recent_table *t;
156 list_for_each_entry(t, &tables, list)
157 if (!strcmp(t->name, name))
158 return t;
159 return NULL;
162 static void recent_table_flush(struct recent_table *t)
164 struct recent_entry *e, *next;
165 unsigned int i;
167 for (i = 0; i < ip_list_hash_size; i++)
168 list_for_each_entry_safe(e, next, &t->iphash[i], list)
169 recent_entry_remove(t, e);
172 static bool
173 recent_mt(const struct sk_buff *skb, const struct net_device *in,
174 const struct net_device *out, const struct xt_match *match,
175 const void *matchinfo, int offset, unsigned int protoff,
176 bool *hotdrop)
178 const struct ipt_recent_info *info = matchinfo;
179 struct recent_table *t;
180 struct recent_entry *e;
181 __be32 addr;
182 u_int8_t ttl;
183 bool ret = info->invert;
185 if (info->side == IPT_RECENT_DEST)
186 addr = ip_hdr(skb)->daddr;
187 else
188 addr = ip_hdr(skb)->saddr;
190 ttl = ip_hdr(skb)->ttl;
191 /* use TTL as seen before forwarding */
192 if (out && !skb->sk)
193 ttl++;
195 spin_lock_bh(&recent_lock);
196 t = recent_table_lookup(info->name);
197 e = recent_entry_lookup(t, addr,
198 info->check_set & IPT_RECENT_TTL ? ttl : 0);
199 if (e == NULL) {
200 if (!(info->check_set & IPT_RECENT_SET))
201 goto out;
202 e = recent_entry_init(t, addr, ttl);
203 if (e == NULL)
204 *hotdrop = true;
205 ret = !ret;
206 goto out;
209 if (info->check_set & IPT_RECENT_SET)
210 ret = !ret;
211 else if (info->check_set & IPT_RECENT_REMOVE) {
212 recent_entry_remove(t, e);
213 ret = !ret;
214 } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) {
215 unsigned long time = jiffies - info->seconds * HZ;
216 unsigned int i, hits = 0;
218 for (i = 0; i < e->nstamps; i++) {
219 if (info->seconds && time_after(time, e->stamps[i]))
220 continue;
221 if (++hits >= info->hit_count) {
222 ret = !ret;
223 break;
228 if (info->check_set & IPT_RECENT_SET ||
229 (info->check_set & IPT_RECENT_UPDATE && ret)) {
230 recent_entry_update(t, e);
231 e->ttl = ttl;
233 out:
234 spin_unlock_bh(&recent_lock);
235 return ret;
238 static bool
239 recent_mt_check(const char *tablename, const void *ip,
240 const struct xt_match *match, void *matchinfo,
241 unsigned int hook_mask)
243 const struct ipt_recent_info *info = matchinfo;
244 struct recent_table *t;
245 unsigned i;
246 bool ret = false;
248 if (hweight8(info->check_set &
249 (IPT_RECENT_SET | IPT_RECENT_REMOVE |
250 IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
251 return false;
252 if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
253 (info->seconds || info->hit_count))
254 return false;
255 if (info->name[0] == '\0' ||
256 strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
257 return false;
259 mutex_lock(&recent_mutex);
260 t = recent_table_lookup(info->name);
261 if (t != NULL) {
262 t->refcnt++;
263 ret = true;
264 goto out;
267 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
268 GFP_KERNEL);
269 if (t == NULL)
270 goto out;
271 t->refcnt = 1;
272 strcpy(t->name, info->name);
273 INIT_LIST_HEAD(&t->lru_list);
274 for (i = 0; i < ip_list_hash_size; i++)
275 INIT_LIST_HEAD(&t->iphash[i]);
276 #ifdef CONFIG_PROC_FS
277 t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
278 if (t->proc == NULL) {
279 kfree(t);
280 goto out;
282 t->proc->proc_fops = &recent_fops;
283 t->proc->uid = ip_list_uid;
284 t->proc->gid = ip_list_gid;
285 t->proc->data = t;
286 #endif
287 spin_lock_bh(&recent_lock);
288 list_add_tail(&t->list, &tables);
289 spin_unlock_bh(&recent_lock);
290 ret = true;
291 out:
292 mutex_unlock(&recent_mutex);
293 return ret;
296 static void recent_mt_destroy(const struct xt_match *match, void *matchinfo)
298 const struct ipt_recent_info *info = matchinfo;
299 struct recent_table *t;
301 mutex_lock(&recent_mutex);
302 t = recent_table_lookup(info->name);
303 if (--t->refcnt == 0) {
304 spin_lock_bh(&recent_lock);
305 list_del(&t->list);
306 spin_unlock_bh(&recent_lock);
307 recent_table_flush(t);
308 #ifdef CONFIG_PROC_FS
309 remove_proc_entry(t->name, proc_dir);
310 #endif
311 kfree(t);
313 mutex_unlock(&recent_mutex);
316 #ifdef CONFIG_PROC_FS
317 struct recent_iter_state {
318 struct recent_table *table;
319 unsigned int bucket;
322 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
323 __acquires(recent_lock)
325 struct recent_iter_state *st = seq->private;
326 const struct recent_table *t = st->table;
327 struct recent_entry *e;
328 loff_t p = *pos;
330 spin_lock_bh(&recent_lock);
332 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
333 list_for_each_entry(e, &t->iphash[st->bucket], list)
334 if (p-- == 0)
335 return e;
336 return NULL;
339 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
341 struct recent_iter_state *st = seq->private;
342 struct recent_table *t = st->table;
343 struct recent_entry *e = v;
344 struct list_head *head = e->list.next;
346 while (head == &t->iphash[st->bucket]) {
347 if (++st->bucket >= ip_list_hash_size)
348 return NULL;
349 head = t->iphash[st->bucket].next;
351 (*pos)++;
352 return list_entry(head, struct recent_entry, list);
355 static void recent_seq_stop(struct seq_file *s, void *v)
356 __releases(recent_lock)
358 spin_unlock_bh(&recent_lock);
361 static int recent_seq_show(struct seq_file *seq, void *v)
363 struct recent_entry *e = v;
364 unsigned int i;
366 i = (e->index - 1) % ip_pkt_list_tot;
367 seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
368 NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index);
369 for (i = 0; i < e->nstamps; i++)
370 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
371 seq_printf(seq, "\n");
372 return 0;
375 static const struct seq_operations recent_seq_ops = {
376 .start = recent_seq_start,
377 .next = recent_seq_next,
378 .stop = recent_seq_stop,
379 .show = recent_seq_show,
382 static int recent_seq_open(struct inode *inode, struct file *file)
384 struct proc_dir_entry *pde = PDE(inode);
385 struct recent_iter_state *st;
387 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
388 if (st == NULL)
389 return -ENOMEM;
391 st->table = pde->data;
392 return 0;
395 static ssize_t recent_proc_write(struct file *file, const char __user *input,
396 size_t size, loff_t *loff)
398 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
399 struct recent_table *t = pde->data;
400 struct recent_entry *e;
401 char buf[sizeof("+255.255.255.255")], *c = buf;
402 __be32 addr;
403 int add;
405 if (size > sizeof(buf))
406 size = sizeof(buf);
407 if (copy_from_user(buf, input, size))
408 return -EFAULT;
409 while (isspace(*c))
410 c++;
412 if (size - (c - buf) < 5)
413 return c - buf;
414 if (!strncmp(c, "clear", 5)) {
415 c += 5;
416 spin_lock_bh(&recent_lock);
417 recent_table_flush(t);
418 spin_unlock_bh(&recent_lock);
419 return c - buf;
422 switch (*c) {
423 case '-':
424 add = 0;
425 c++;
426 break;
427 case '+':
428 c++;
429 default:
430 add = 1;
431 break;
433 addr = in_aton(c);
435 spin_lock_bh(&recent_lock);
436 e = recent_entry_lookup(t, addr, 0);
437 if (e == NULL) {
438 if (add)
439 recent_entry_init(t, addr, 0);
440 } else {
441 if (add)
442 recent_entry_update(t, e);
443 else
444 recent_entry_remove(t, e);
446 spin_unlock_bh(&recent_lock);
447 return size;
450 static const struct file_operations recent_fops = {
451 .open = recent_seq_open,
452 .read = seq_read,
453 .write = recent_proc_write,
454 .release = seq_release_private,
455 .owner = THIS_MODULE,
457 #endif /* CONFIG_PROC_FS */
459 static struct xt_match recent_mt_reg __read_mostly = {
460 .name = "recent",
461 .family = AF_INET,
462 .match = recent_mt,
463 .matchsize = sizeof(struct ipt_recent_info),
464 .checkentry = recent_mt_check,
465 .destroy = recent_mt_destroy,
466 .me = THIS_MODULE,
469 static int __init recent_mt_init(void)
471 int err;
473 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
474 return -EINVAL;
475 ip_list_hash_size = 1 << fls(ip_list_tot);
477 err = xt_register_match(&recent_mt_reg);
478 #ifdef CONFIG_PROC_FS
479 if (err)
480 return err;
481 proc_dir = proc_mkdir("ipt_recent", init_net.proc_net);
482 if (proc_dir == NULL) {
483 xt_unregister_match(&recent_mt_reg);
484 err = -ENOMEM;
486 #endif
487 return err;
490 static void __exit recent_mt_exit(void)
492 BUG_ON(!list_empty(&tables));
493 xt_unregister_match(&recent_mt_reg);
494 #ifdef CONFIG_PROC_FS
495 remove_proc_entry("ipt_recent", init_net.proc_net);
496 #endif
499 module_init(recent_mt_init);
500 module_exit(recent_mt_exit);