[NETFILTER]: recent match: fix "sleeping function called from invalid context"
[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 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         strcpy(t->name, info->name);
266         INIT_LIST_HEAD(&t->lru_list);
267         for (i = 0; i < ip_list_hash_size; i++)
268                 INIT_LIST_HEAD(&t->iphash[i]);
269 #ifdef CONFIG_PROC_FS
270         t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
271         if (t->proc == NULL) {
272                 kfree(t);
273                 goto out;
274         }
275         t->proc->proc_fops = &recent_fops;
276         t->proc->data      = t;
277 #endif
278         spin_lock_bh(&recent_lock);
279         list_add_tail(&t->list, &tables);
280         spin_unlock_bh(&recent_lock);
281         ret = 1;
282 out:
283         mutex_unlock(&recent_mutex);
284         return ret;
285 }
286
287 static void
288 ipt_recent_destroy(const struct xt_match *match, void *matchinfo,
289                    unsigned int matchsize)
290 {
291         const struct ipt_recent_info *info = matchinfo;
292         struct recent_table *t;
293
294         mutex_lock(&recent_mutex);
295         t = recent_table_lookup(info->name);
296         if (--t->refcnt == 0) {
297                 spin_lock_bh(&recent_lock);
298                 list_del(&t->list);
299                 spin_unlock_bh(&recent_lock);
300                 recent_table_flush(t);
301 #ifdef CONFIG_PROC_FS
302                 remove_proc_entry(t->name, proc_dir);
303 #endif
304                 kfree(t);
305         }
306         mutex_unlock(&recent_mutex);
307 }
308
309 #ifdef CONFIG_PROC_FS
310 struct recent_iter_state {
311         struct recent_table     *table;
312         unsigned int            bucket;
313 };
314
315 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
316 {
317         struct recent_iter_state *st = seq->private;
318         struct recent_table *t = st->table;
319         struct recent_entry *e;
320         loff_t p = *pos;
321
322         spin_lock_bh(&recent_lock);
323
324         for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
325                 list_for_each_entry(e, &t->iphash[st->bucket], list) {
326                         if (p-- == 0)
327                                 return e;
328                 }
329         }
330         return NULL;
331 }
332
333 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
334 {
335         struct recent_iter_state *st = seq->private;
336         struct recent_table *t = st->table;
337         struct recent_entry *e = v;
338         struct list_head *head = e->list.next;
339
340         while (head == &t->iphash[st->bucket]) {
341                 if (++st->bucket >= ip_list_hash_size)
342                         return NULL;
343                 head = t->iphash[st->bucket].next;
344         }
345         (*pos)++;
346         return list_entry(head, struct recent_entry, list);
347 }
348
349 static void recent_seq_stop(struct seq_file *s, void *v)
350 {
351         spin_unlock_bh(&recent_lock);
352 }
353
354 static int recent_seq_show(struct seq_file *seq, void *v)
355 {
356         struct recent_entry *e = v;
357         unsigned int i;
358
359         i = (e->index - 1) % ip_pkt_list_tot;
360         seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
361                    NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index);
362         for (i = 0; i < e->nstamps; i++)
363                 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
364         seq_printf(seq, "\n");
365         return 0;
366 }
367
368 static struct seq_operations recent_seq_ops = {
369         .start          = recent_seq_start,
370         .next           = recent_seq_next,
371         .stop           = recent_seq_stop,
372         .show           = recent_seq_show,
373 };
374
375 static int recent_seq_open(struct inode *inode, struct file *file)
376 {
377         struct proc_dir_entry *pde = PDE(inode);
378         struct seq_file *seq;
379         struct recent_iter_state *st;
380         int ret;
381
382         st = kzalloc(sizeof(*st), GFP_KERNEL);
383         if (st == NULL)
384                 return -ENOMEM;
385         ret = seq_open(file, &recent_seq_ops);
386         if (ret)
387                 kfree(st);
388         st->table    = pde->data;
389         seq          = file->private_data;
390         seq->private = st;
391         return ret;
392 }
393
394 static ssize_t recent_proc_write(struct file *file, const char __user *input,
395                                  size_t size, loff_t *loff)
396 {
397         struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
398         struct recent_table *t = pde->data;
399         struct recent_entry *e;
400         char buf[sizeof("+255.255.255.255")], *c = buf;
401         u_int32_t addr;
402         int add;
403
404         if (size > sizeof(buf))
405                 size = sizeof(buf);
406         if (copy_from_user(buf, input, size))
407                 return -EFAULT;
408         while (isspace(*c))
409                 c++;
410
411         if (size - (c - buf) < 5)
412                 return c - buf;
413         if (!strncmp(c, "clear", 5)) {
414                 c += 5;
415                 spin_lock_bh(&recent_lock);
416                 recent_table_flush(t);
417                 spin_unlock_bh(&recent_lock);
418                 return c - buf;
419         }
420
421         switch (*c) {
422         case '-':
423                 add = 0;
424                 c++;
425                 break;
426         case '+':
427                 c++;
428         default:
429                 add = 1;
430                 break;
431         }
432         addr = in_aton(c);
433
434         spin_lock_bh(&recent_lock);
435         e = recent_entry_lookup(t, addr, 0);
436         if (e == NULL) {
437                 if (add)
438                         recent_entry_init(t, addr, 0);
439         } else {
440                 if (add)
441                         recent_entry_update(t, e);
442                 else
443                         recent_entry_remove(t, e);
444         }
445         spin_unlock_bh(&recent_lock);
446         return size;
447 }
448
449 static struct file_operations recent_fops = {
450         .open           = recent_seq_open,
451         .read           = seq_read,
452         .write          = recent_proc_write,
453         .release        = seq_release_private,
454         .owner          = THIS_MODULE,
455 };
456 #endif /* CONFIG_PROC_FS */
457
458 static struct ipt_match recent_match = {
459         .name           = "recent",
460         .match          = ipt_recent_match,
461         .matchsize      = sizeof(struct ipt_recent_info),
462         .checkentry     = ipt_recent_checkentry,
463         .destroy        = ipt_recent_destroy,
464         .me             = THIS_MODULE,
465 };
466
467 static int __init ipt_recent_init(void)
468 {
469         int err;
470
471         if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
472                 return -EINVAL;
473         ip_list_hash_size = 1 << fls(ip_list_tot);
474
475         err = ipt_register_match(&recent_match);
476 #ifdef CONFIG_PROC_FS
477         if (err)
478                 return err;
479         proc_dir = proc_mkdir("ipt_recent", proc_net);
480         if (proc_dir == NULL) {
481                 ipt_unregister_match(&recent_match);
482                 err = -ENOMEM;
483         }
484 #endif
485         return err;
486 }
487
488 static void __exit ipt_recent_exit(void)
489 {
490         BUG_ON(!list_empty(&tables));
491         ipt_unregister_match(&recent_match);
492 #ifdef CONFIG_PROC_FS
493         remove_proc_entry("ipt_recent", proc_net);
494 #endif
495 }
496
497 module_init(ipt_recent_init);
498 module_exit(ipt_recent_exit);