126db44e71a8eaa3fd91fa5ba36f8bbd27db46fe
[safe/jmp/linux-2.6] / 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 static unsigned int ip_list_uid = 0;
39 static unsigned int ip_list_gid = 0;
40 module_param(ip_list_tot, uint, 0400);
41 module_param(ip_pkt_list_tot, uint, 0400);
42 module_param(ip_list_hash_size, uint, 0400);
43 module_param(ip_list_perms, uint, 0400);
44 module_param(ip_list_uid, uint, 0400);
45 module_param(ip_list_gid, uint, 0400);
46 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
47 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
48 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
49 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
50 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/ipt_recent/* files");
51 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/ipt_recent/* files");
52
53 struct recent_entry {
54         struct list_head        list;
55         struct list_head        lru_list;
56         __be32                  addr;
57         u_int8_t                ttl;
58         u_int8_t                index;
59         u_int16_t               nstamps;
60         unsigned long           stamps[0];
61 };
62
63 struct recent_table {
64         struct list_head        list;
65         char                    name[IPT_RECENT_NAME_LEN];
66 #ifdef CONFIG_PROC_FS
67         struct proc_dir_entry   *proc;
68 #endif
69         unsigned int            refcnt;
70         unsigned int            entries;
71         struct list_head        lru_list;
72         struct list_head        iphash[0];
73 };
74
75 static LIST_HEAD(tables);
76 static DEFINE_SPINLOCK(recent_lock);
77 static DEFINE_MUTEX(recent_mutex);
78
79 #ifdef CONFIG_PROC_FS
80 static struct proc_dir_entry    *proc_dir;
81 static struct file_operations   recent_fops;
82 #endif
83
84 static u_int32_t hash_rnd;
85 static int hash_rnd_initted;
86
87 static unsigned int recent_entry_hash(__be32 addr)
88 {
89         if (!hash_rnd_initted) {
90                 get_random_bytes(&hash_rnd, 4);
91                 hash_rnd_initted = 1;
92         }
93         return jhash_1word((__force u32)addr, hash_rnd) & (ip_list_hash_size - 1);
94 }
95
96 static struct recent_entry *
97 recent_entry_lookup(const struct recent_table *table, __be32 addr, u_int8_t ttl)
98 {
99         struct recent_entry *e;
100         unsigned int h;
101
102         h = recent_entry_hash(addr);
103         list_for_each_entry(e, &table->iphash[h], list)
104                 if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl))
105                         return e;
106         return NULL;
107 }
108
109 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
110 {
111         list_del(&e->list);
112         list_del(&e->lru_list);
113         kfree(e);
114         t->entries--;
115 }
116
117 static struct recent_entry *
118 recent_entry_init(struct recent_table *t, __be32 addr, u_int8_t ttl)
119 {
120         struct recent_entry *e;
121
122         if (t->entries >= ip_list_tot) {
123                 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
124                 recent_entry_remove(t, e);
125         }
126         e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
127                     GFP_ATOMIC);
128         if (e == NULL)
129                 return NULL;
130         e->addr      = addr;
131         e->ttl       = ttl;
132         e->stamps[0] = jiffies;
133         e->nstamps   = 1;
134         e->index     = 1;
135         list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
136         list_add_tail(&e->lru_list, &t->lru_list);
137         t->entries++;
138         return e;
139 }
140
141 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
142 {
143         e->stamps[e->index++] = jiffies;
144         if (e->index > e->nstamps)
145                 e->nstamps = e->index;
146         e->index %= ip_pkt_list_tot;
147         list_move_tail(&e->lru_list, &t->lru_list);
148 }
149
150 static struct recent_table *recent_table_lookup(const char *name)
151 {
152         struct recent_table *t;
153
154         list_for_each_entry(t, &tables, list)
155                 if (!strcmp(t->name, name))
156                         return t;
157         return NULL;
158 }
159
160 static void recent_table_flush(struct recent_table *t)
161 {
162         struct recent_entry *e, *next;
163         unsigned int i;
164
165         for (i = 0; i < ip_list_hash_size; i++) {
166                 list_for_each_entry_safe(e, next, &t->iphash[i], list)
167                         recent_entry_remove(t, e);
168         }
169 }
170
171 static int
172 ipt_recent_match(const struct sk_buff *skb,
173                  const struct net_device *in, const struct net_device *out,
174                  const struct xt_match *match, const void *matchinfo,
175                  int offset, unsigned int protoff, int *hotdrop)
176 {
177         const struct ipt_recent_info *info = matchinfo;
178         struct recent_table *t;
179         struct recent_entry *e;
180         __be32 addr;
181         u_int8_t ttl;
182         int ret = info->invert;
183
184         if (info->side == IPT_RECENT_DEST)
185                 addr = skb->nh.iph->daddr;
186         else
187                 addr = skb->nh.iph->saddr;
188
189         ttl = skb->nh.iph->ttl;
190         /* use TTL as seen before forwarding */
191         if (out && !skb->sk)
192                 ttl++;
193
194         spin_lock_bh(&recent_lock);
195         t = recent_table_lookup(info->name);
196         e = recent_entry_lookup(t, addr,
197                                 info->check_set & IPT_RECENT_TTL ? ttl : 0);
198         if (e == NULL) {
199                 if (!(info->check_set & IPT_RECENT_SET))
200                         goto out;
201                 e = recent_entry_init(t, addr, ttl);
202                 if (e == NULL)
203                         *hotdrop = 1;
204                 ret ^= 1;
205                 goto out;
206         }
207
208         if (info->check_set & IPT_RECENT_SET)
209                 ret ^= 1;
210         else if (info->check_set & IPT_RECENT_REMOVE) {
211                 recent_entry_remove(t, e);
212                 ret ^= 1;
213         } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) {
214                 unsigned long t = jiffies - info->seconds * HZ;
215                 unsigned int i, hits = 0;
216
217                 for (i = 0; i < e->nstamps; i++) {
218                         if (info->seconds && time_after(t, e->stamps[i]))
219                                 continue;
220                         if (++hits >= info->hit_count) {
221                                 ret ^= 1;
222                                 break;
223                         }
224                 }
225         }
226
227         if (info->check_set & IPT_RECENT_SET ||
228             (info->check_set & IPT_RECENT_UPDATE && ret)) {
229                 recent_entry_update(t, e);
230                 e->ttl = ttl;
231         }
232 out:
233         spin_unlock_bh(&recent_lock);
234         return ret;
235 }
236
237 static int
238 ipt_recent_checkentry(const char *tablename, const void *ip,
239                       const struct xt_match *match, void *matchinfo,
240                       unsigned int hook_mask)
241 {
242         const struct ipt_recent_info *info = matchinfo;
243         struct recent_table *t;
244         unsigned i;
245         int ret = 0;
246
247         if (hweight8(info->check_set &
248                      (IPT_RECENT_SET | IPT_RECENT_REMOVE |
249                       IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
250                 return 0;
251         if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
252             (info->seconds || info->hit_count))
253                 return 0;
254         if (info->name[0] == '\0' ||
255             strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
256                 return 0;
257
258         mutex_lock(&recent_mutex);
259         t = recent_table_lookup(info->name);
260         if (t != NULL) {
261                 t->refcnt++;
262                 ret = 1;
263                 goto out;
264         }
265
266         t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
267                     GFP_KERNEL);
268         if (t == NULL)
269                 goto out;
270         t->refcnt = 1;
271         strcpy(t->name, info->name);
272         INIT_LIST_HEAD(&t->lru_list);
273         for (i = 0; i < ip_list_hash_size; i++)
274                 INIT_LIST_HEAD(&t->iphash[i]);
275 #ifdef CONFIG_PROC_FS
276         t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
277         if (t->proc == NULL) {
278                 kfree(t);
279                 goto out;
280         }
281         t->proc->proc_fops = &recent_fops;
282         t->proc->uid       = ip_list_uid;
283         t->proc->gid       = ip_list_gid;
284         t->proc->data      = t;
285 #endif
286         spin_lock_bh(&recent_lock);
287         list_add_tail(&t->list, &tables);
288         spin_unlock_bh(&recent_lock);
289         ret = 1;
290 out:
291         mutex_unlock(&recent_mutex);
292         return ret;
293 }
294
295 static void
296 ipt_recent_destroy(const struct xt_match *match, void *matchinfo)
297 {
298         const struct ipt_recent_info *info = matchinfo;
299         struct recent_table *t;
300
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);
312         }
313         mutex_unlock(&recent_mutex);
314 }
315
316 #ifdef CONFIG_PROC_FS
317 struct recent_iter_state {
318         struct recent_table     *table;
319         unsigned int            bucket;
320 };
321
322 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
323 {
324         struct recent_iter_state *st = seq->private;
325         struct recent_table *t = st->table;
326         struct recent_entry *e;
327         loff_t p = *pos;
328
329         spin_lock_bh(&recent_lock);
330
331         for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
332                 list_for_each_entry(e, &t->iphash[st->bucket], list) {
333                         if (p-- == 0)
334                                 return e;
335                 }
336         }
337         return NULL;
338 }
339
340 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
341 {
342         struct recent_iter_state *st = seq->private;
343         struct recent_table *t = st->table;
344         struct recent_entry *e = v;
345         struct list_head *head = e->list.next;
346
347         while (head == &t->iphash[st->bucket]) {
348                 if (++st->bucket >= ip_list_hash_size)
349                         return NULL;
350                 head = t->iphash[st->bucket].next;
351         }
352         (*pos)++;
353         return list_entry(head, struct recent_entry, list);
354 }
355
356 static void recent_seq_stop(struct seq_file *s, void *v)
357 {
358         spin_unlock_bh(&recent_lock);
359 }
360
361 static int recent_seq_show(struct seq_file *seq, void *v)
362 {
363         struct recent_entry *e = v;
364         unsigned int i;
365
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;
373 }
374
375 static 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,
380 };
381
382 static int recent_seq_open(struct inode *inode, struct file *file)
383 {
384         struct proc_dir_entry *pde = PDE(inode);
385         struct seq_file *seq;
386         struct recent_iter_state *st;
387         int ret;
388
389         st = kzalloc(sizeof(*st), GFP_KERNEL);
390         if (st == NULL)
391                 return -ENOMEM;
392         ret = seq_open(file, &recent_seq_ops);
393         if (ret)
394                 kfree(st);
395         st->table    = pde->data;
396         seq          = file->private_data;
397         seq->private = st;
398         return ret;
399 }
400
401 static ssize_t recent_proc_write(struct file *file, const char __user *input,
402                                  size_t size, loff_t *loff)
403 {
404         struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
405         struct recent_table *t = pde->data;
406         struct recent_entry *e;
407         char buf[sizeof("+255.255.255.255")], *c = buf;
408         __be32 addr;
409         int add;
410
411         if (size > sizeof(buf))
412                 size = sizeof(buf);
413         if (copy_from_user(buf, input, size))
414                 return -EFAULT;
415         while (isspace(*c))
416                 c++;
417
418         if (size - (c - buf) < 5)
419                 return c - buf;
420         if (!strncmp(c, "clear", 5)) {
421                 c += 5;
422                 spin_lock_bh(&recent_lock);
423                 recent_table_flush(t);
424                 spin_unlock_bh(&recent_lock);
425                 return c - buf;
426         }
427
428         switch (*c) {
429         case '-':
430                 add = 0;
431                 c++;
432                 break;
433         case '+':
434                 c++;
435         default:
436                 add = 1;
437                 break;
438         }
439         addr = in_aton(c);
440
441         spin_lock_bh(&recent_lock);
442         e = recent_entry_lookup(t, addr, 0);
443         if (e == NULL) {
444                 if (add)
445                         recent_entry_init(t, addr, 0);
446         } else {
447                 if (add)
448                         recent_entry_update(t, e);
449                 else
450                         recent_entry_remove(t, e);
451         }
452         spin_unlock_bh(&recent_lock);
453         return size;
454 }
455
456 static struct file_operations recent_fops = {
457         .open           = recent_seq_open,
458         .read           = seq_read,
459         .write          = recent_proc_write,
460         .release        = seq_release_private,
461         .owner          = THIS_MODULE,
462 };
463 #endif /* CONFIG_PROC_FS */
464
465 static struct ipt_match recent_match = {
466         .name           = "recent",
467         .match          = ipt_recent_match,
468         .matchsize      = sizeof(struct ipt_recent_info),
469         .checkentry     = ipt_recent_checkentry,
470         .destroy        = ipt_recent_destroy,
471         .me             = THIS_MODULE,
472 };
473
474 static int __init ipt_recent_init(void)
475 {
476         int err;
477
478         if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
479                 return -EINVAL;
480         ip_list_hash_size = 1 << fls(ip_list_tot);
481
482         err = ipt_register_match(&recent_match);
483 #ifdef CONFIG_PROC_FS
484         if (err)
485                 return err;
486         proc_dir = proc_mkdir("ipt_recent", proc_net);
487         if (proc_dir == NULL) {
488                 ipt_unregister_match(&recent_match);
489                 err = -ENOMEM;
490         }
491 #endif
492         return err;
493 }
494
495 static void __exit ipt_recent_exit(void)
496 {
497         BUG_ON(!list_empty(&tables));
498         ipt_unregister_match(&recent_match);
499 #ifdef CONFIG_PROC_FS
500         remove_proc_entry("ipt_recent", proc_net);
501 #endif
502 }
503
504 module_init(ipt_recent_init);
505 module_exit(ipt_recent_exit);