Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / net / ipv4 / netfilter / ipt_recent.c
1 /*
2  * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
3  *
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.
7  *
8  * This is a replacement of the old ipt_recent module, which carried the
9  * following copyright notice:
10  *
11  * Author: Stephen Frost <sfrost@snowman.net>
12  * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
13  */
14 #include <linux/init.h>
15 #include <linux/moduleparam.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/list.h>
21 #include <linux/random.h>
22 #include <linux/jhash.h>
23 #include <linux/bitops.h>
24 #include <linux/skbuff.h>
25 #include <linux/inet.h>
26
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netfilter_ipv4/ipt_recent.h>
29
30 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
31 MODULE_DESCRIPTION("IP tables recently seen matching module");
32 MODULE_LICENSE("GPL");
33
34 static unsigned int ip_list_tot = 100;
35 static unsigned int ip_pkt_list_tot = 20;
36 static unsigned int ip_list_hash_size = 0;
37 static unsigned int ip_list_perms = 0644;
38 module_param(ip_list_tot, uint, 0400);
39 module_param(ip_pkt_list_tot, uint, 0400);
40 module_param(ip_list_hash_size, uint, 0400);
41 module_param(ip_list_perms, uint, 0400);
42 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
43 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
44 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
45 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
46
47
48 struct recent_entry {
49         struct list_head        list;
50         struct list_head        lru_list;
51         u_int32_t               addr;
52         u_int8_t                ttl;
53         u_int8_t                index;
54         u_int16_t               nstamps;
55         unsigned long           stamps[0];
56 };
57
58 struct recent_table {
59         struct list_head        list;
60         char                    name[IPT_RECENT_NAME_LEN];
61 #ifdef CONFIG_PROC_FS
62         struct proc_dir_entry   *proc;
63 #endif
64         unsigned int            refcnt;
65         unsigned int            entries;
66         struct list_head        lru_list;
67         struct list_head        iphash[0];
68 };
69
70 static LIST_HEAD(tables);
71 static DEFINE_SPINLOCK(recent_lock);
72 static DEFINE_MUTEX(recent_mutex);
73
74 #ifdef CONFIG_PROC_FS
75 static struct proc_dir_entry    *proc_dir;
76 static struct file_operations   recent_fops;
77 #endif
78
79 static u_int32_t hash_rnd;
80 static int hash_rnd_initted;
81
82 static unsigned int recent_entry_hash(u_int32_t addr)
83 {
84         if (!hash_rnd_initted) {
85                 get_random_bytes(&hash_rnd, 4);
86                 hash_rnd_initted = 1;
87         }
88         return jhash_1word(addr, hash_rnd) & (ip_list_hash_size - 1);
89 }
90
91 static struct recent_entry *
92 recent_entry_lookup(const struct recent_table *table, u_int32_t addr, u_int8_t ttl)
93 {
94         struct recent_entry *e;
95         unsigned int h;
96
97         h = recent_entry_hash(addr);
98         list_for_each_entry(e, &table->iphash[h], list)
99                 if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl))
100                         return e;
101         return NULL;
102 }
103
104 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
105 {
106         list_del(&e->list);
107         list_del(&e->lru_list);
108         kfree(e);
109         t->entries--;
110 }
111
112 static struct recent_entry *
113 recent_entry_init(struct recent_table *t, u_int32_t addr, u_int8_t ttl)
114 {
115         struct recent_entry *e;
116
117         if (t->entries >= ip_list_tot) {
118                 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
119                 recent_entry_remove(t, e);
120         }
121         e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
122                     GFP_ATOMIC);
123         if (e == NULL)
124                 return NULL;
125         e->addr      = addr;
126         e->ttl       = ttl;
127         e->stamps[0] = jiffies;
128         e->nstamps   = 1;
129         e->index     = 1;
130         list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
131         list_add_tail(&e->lru_list, &t->lru_list);
132         t->entries++;
133         return e;
134 }
135
136 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
137 {
138         e->stamps[e->index++] = jiffies;
139         if (e->index > e->nstamps)
140                 e->nstamps = e->index;
141         e->index %= ip_pkt_list_tot;
142         list_move_tail(&e->lru_list, &t->lru_list);
143 }
144
145 static struct recent_table *recent_table_lookup(const char *name)
146 {
147         struct recent_table *t;
148
149         list_for_each_entry(t, &tables, list)
150                 if (!strcmp(t->name, name))
151                         return t;
152         return NULL;
153 }
154
155 static void recent_table_flush(struct recent_table *t)
156 {
157         struct recent_entry *e, *next;
158         unsigned int i;
159
160         for (i = 0; i < ip_list_hash_size; i++) {
161                 list_for_each_entry_safe(e, next, &t->iphash[i], list)
162                         recent_entry_remove(t, e);
163         }
164 }
165
166 static int
167 ipt_recent_match(const struct sk_buff *skb,
168                  const struct net_device *in, const struct net_device *out,
169                  const struct xt_match *match, const void *matchinfo,
170                  int offset, unsigned int protoff, int *hotdrop)
171 {
172         const struct ipt_recent_info *info = matchinfo;
173         struct recent_table *t;
174         struct recent_entry *e;
175         u_int32_t addr;
176         u_int8_t ttl;
177         int ret = info->invert;
178
179         if (info->side == IPT_RECENT_DEST)
180                 addr = skb->nh.iph->daddr;
181         else
182                 addr = skb->nh.iph->saddr;
183
184         ttl = skb->nh.iph->ttl;
185         /* use TTL as seen before forwarding */
186         if (out && !skb->sk)
187                 ttl++;
188
189         spin_lock_bh(&recent_lock);
190         t = recent_table_lookup(info->name);
191         e = recent_entry_lookup(t, addr,
192                                 info->check_set & IPT_RECENT_TTL ? ttl : 0);
193         if (e == NULL) {
194                 if (!(info->check_set & IPT_RECENT_SET))
195                         goto out;
196                 e = recent_entry_init(t, addr, ttl);
197                 if (e == NULL)
198                         *hotdrop = 1;
199                 ret ^= 1;
200                 goto out;
201         }
202
203         if (info->check_set & IPT_RECENT_SET)
204                 ret ^= 1;
205         else if (info->check_set & IPT_RECENT_REMOVE) {
206                 recent_entry_remove(t, e);
207                 ret ^= 1;
208         } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) {
209                 unsigned long t = jiffies - info->seconds * HZ;
210                 unsigned int i, hits = 0;
211
212                 for (i = 0; i < e->nstamps; i++) {
213                         if (info->seconds && time_after(t, e->stamps[i]))
214                                 continue;
215                         if (++hits >= info->hit_count) {
216                                 ret ^= 1;
217                                 break;
218                         }
219                 }
220         }
221
222         if (info->check_set & IPT_RECENT_SET ||
223             (info->check_set & IPT_RECENT_UPDATE && ret)) {
224                 recent_entry_update(t, e);
225                 e->ttl = ttl;
226         }
227 out:
228         spin_unlock_bh(&recent_lock);
229         return ret;
230 }
231
232 static int
233 ipt_recent_checkentry(const char *tablename, const void *ip,
234                       const struct xt_match *match, void *matchinfo,
235                       unsigned int matchsize, unsigned int hook_mask)
236 {
237         const struct ipt_recent_info *info = matchinfo;
238         struct recent_table *t;
239         unsigned i;
240         int ret = 0;
241
242         if (hweight8(info->check_set &
243                      (IPT_RECENT_SET | IPT_RECENT_REMOVE |
244                       IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
245                 return 0;
246         if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
247             (info->seconds || info->hit_count))
248                 return 0;
249         if (info->name[0] == '\0' ||
250             strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
251                 return 0;
252
253         mutex_lock(&recent_mutex);
254         t = recent_table_lookup(info->name);
255         if (t != NULL) {
256                 t->refcnt++;
257                 ret = 1;
258                 goto out;
259         }
260
261         t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
262                     GFP_KERNEL);
263         if (t == NULL)
264                 goto out;
265         t->refcnt = 1;
266         strcpy(t->name, info->name);
267         INIT_LIST_HEAD(&t->lru_list);
268         for (i = 0; i < ip_list_hash_size; i++)
269                 INIT_LIST_HEAD(&t->iphash[i]);
270 #ifdef CONFIG_PROC_FS
271         t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
272         if (t->proc == NULL) {
273                 kfree(t);
274                 goto out;
275         }
276         t->proc->proc_fops = &recent_fops;
277         t->proc->data      = t;
278 #endif
279         spin_lock_bh(&recent_lock);
280         list_add_tail(&t->list, &tables);
281         spin_unlock_bh(&recent_lock);
282         ret = 1;
283 out:
284         mutex_unlock(&recent_mutex);
285         return ret;
286 }
287
288 static void
289 ipt_recent_destroy(const struct xt_match *match, void *matchinfo,
290                    unsigned int matchsize)
291 {
292         const struct ipt_recent_info *info = matchinfo;
293         struct recent_table *t;
294
295         mutex_lock(&recent_mutex);
296         t = recent_table_lookup(info->name);
297         if (--t->refcnt == 0) {
298                 spin_lock_bh(&recent_lock);
299                 list_del(&t->list);
300                 spin_unlock_bh(&recent_lock);
301                 recent_table_flush(t);
302 #ifdef CONFIG_PROC_FS
303                 remove_proc_entry(t->name, proc_dir);
304 #endif
305                 kfree(t);
306         }
307         mutex_unlock(&recent_mutex);
308 }
309
310 #ifdef CONFIG_PROC_FS
311 struct recent_iter_state {
312         struct recent_table     *table;
313         unsigned int            bucket;
314 };
315
316 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
317 {
318         struct recent_iter_state *st = seq->private;
319         struct recent_table *t = st->table;
320         struct recent_entry *e;
321         loff_t p = *pos;
322
323         spin_lock_bh(&recent_lock);
324
325         for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
326                 list_for_each_entry(e, &t->iphash[st->bucket], list) {
327                         if (p-- == 0)
328                                 return e;
329                 }
330         }
331         return NULL;
332 }
333
334 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
335 {
336         struct recent_iter_state *st = seq->private;
337         struct recent_table *t = st->table;
338         struct recent_entry *e = v;
339         struct list_head *head = e->list.next;
340
341         while (head == &t->iphash[st->bucket]) {
342                 if (++st->bucket >= ip_list_hash_size)
343                         return NULL;
344                 head = t->iphash[st->bucket].next;
345         }
346         (*pos)++;
347         return list_entry(head, struct recent_entry, list);
348 }
349
350 static void recent_seq_stop(struct seq_file *s, void *v)
351 {
352         spin_unlock_bh(&recent_lock);
353 }
354
355 static int recent_seq_show(struct seq_file *seq, void *v)
356 {
357         struct recent_entry *e = v;
358         unsigned int i;
359
360         i = (e->index - 1) % ip_pkt_list_tot;
361         seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
362                    NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index);
363         for (i = 0; i < e->nstamps; i++)
364                 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
365         seq_printf(seq, "\n");
366         return 0;
367 }
368
369 static struct seq_operations recent_seq_ops = {
370         .start          = recent_seq_start,
371         .next           = recent_seq_next,
372         .stop           = recent_seq_stop,
373         .show           = recent_seq_show,
374 };
375
376 static int recent_seq_open(struct inode *inode, struct file *file)
377 {
378         struct proc_dir_entry *pde = PDE(inode);
379         struct seq_file *seq;
380         struct recent_iter_state *st;
381         int ret;
382
383         st = kzalloc(sizeof(*st), GFP_KERNEL);
384         if (st == NULL)
385                 return -ENOMEM;
386         ret = seq_open(file, &recent_seq_ops);
387         if (ret)
388                 kfree(st);
389         st->table    = pde->data;
390         seq          = file->private_data;
391         seq->private = st;
392         return ret;
393 }
394
395 static ssize_t recent_proc_write(struct file *file, const char __user *input,
396                                  size_t size, loff_t *loff)
397 {
398         struct proc_dir_entry *pde = PDE(file->f_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         u_int32_t addr;
403         int add;
404
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++;
411
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;
420         }
421
422         switch (*c) {
423         case '-':
424                 add = 0;
425                 c++;
426                 break;
427         case '+':
428                 c++;
429         default:
430                 add = 1;
431                 break;
432         }
433         addr = in_aton(c);
434
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);
445         }
446         spin_unlock_bh(&recent_lock);
447         return size;
448 }
449
450 static 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,
456 };
457 #endif /* CONFIG_PROC_FS */
458
459 static struct ipt_match recent_match = {
460         .name           = "recent",
461         .match          = ipt_recent_match,
462         .matchsize      = sizeof(struct ipt_recent_info),
463         .checkentry     = ipt_recent_checkentry,
464         .destroy        = ipt_recent_destroy,
465         .me             = THIS_MODULE,
466 };
467
468 static int __init ipt_recent_init(void)
469 {
470         int err;
471
472         if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
473                 return -EINVAL;
474         ip_list_hash_size = 1 << fls(ip_list_tot);
475
476         err = ipt_register_match(&recent_match);
477 #ifdef CONFIG_PROC_FS
478         if (err)
479                 return err;
480         proc_dir = proc_mkdir("ipt_recent", proc_net);
481         if (proc_dir == NULL) {
482                 ipt_unregister_match(&recent_match);
483                 err = -ENOMEM;
484         }
485 #endif
486         return err;
487 }
488
489 static void __exit ipt_recent_exit(void)
490 {
491         BUG_ON(!list_empty(&tables));
492         ipt_unregister_match(&recent_match);
493 #ifdef CONFIG_PROC_FS
494         remove_proc_entry("ipt_recent", proc_net);
495 #endif
496 }
497
498 module_init(ipt_recent_init);
499 module_exit(ipt_recent_exit);