blob: 61a2139f9cfd24f5b138b5cbacfe526b3e4fec0f [file] [log] [blame]
Patrick McHardy404bdbf2006-05-29 18:21:34 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/moduleparam.h>
Patrick McHardy404bdbf2006-05-29 18:21:34 -070016#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/netfilter_ipv4/ip_tables.h>
28#include <linux/netfilter_ipv4/ipt_recent.h>
29
Patrick McHardy404bdbf2006-05-29 18:21:34 -070030MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
31MODULE_DESCRIPTION("IP tables recently seen matching module");
32MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Patrick McHardye7be6992006-01-05 12:19:46 -080034static unsigned int ip_list_tot = 100;
35static unsigned int ip_pkt_list_tot = 20;
36static unsigned int ip_list_hash_size = 0;
37static unsigned int ip_list_perms = 0644;
Patrick McHardye7be6992006-01-05 12:19:46 -080038module_param(ip_list_tot, uint, 0400);
39module_param(ip_pkt_list_tot, uint, 0400);
40module_param(ip_list_hash_size, uint, 0400);
41module_param(ip_list_perms, uint, 0400);
Patrick McHardy404bdbf2006-05-29 18:21:34 -070042MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
43MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
44MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
45MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Patrick McHardy404bdbf2006-05-29 18:21:34 -070047
48struct 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Patrick McHardy404bdbf2006-05-29 18:21:34 -070058struct recent_table {
59 struct list_head list;
60 char name[IPT_RECENT_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_PROC_FS
Patrick McHardy404bdbf2006-05-29 18:21:34 -070062 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Patrick McHardy404bdbf2006-05-29 18:21:34 -070070static LIST_HEAD(tables);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static DEFINE_SPINLOCK(recent_lock);
Patrick McHardya0e889b2006-06-09 12:17:41 -070072static DEFINE_MUTEX(recent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#ifdef CONFIG_PROC_FS
Patrick McHardy404bdbf2006-05-29 18:21:34 -070075static struct proc_dir_entry *proc_dir;
76static struct file_operations recent_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
Patrick McHardy404bdbf2006-05-29 18:21:34 -070079static u_int32_t hash_rnd;
80static int hash_rnd_initted;
81
82static 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
91static struct recent_entry *
92recent_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
104static 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
112static struct recent_entry *
113recent_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
136static 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
145static 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
155static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700167ipt_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 const struct ipt_recent_info *info = matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700173 struct recent_table *t;
174 struct recent_entry *e;
175 u_int32_t addr;
176 u_int8_t ttl;
177 int ret = info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700179 if (info->side == IPT_RECENT_DEST)
180 addr = skb->nh.iph->daddr;
181 else
182 addr = skb->nh.iph->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700184 ttl = skb->nh.iph->ttl;
185 /* use TTL as seen before forwarding */
186 if (out && !skb->sk)
187 ttl++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700190 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700203 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700212 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 }
227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700233ipt_recent_checkentry(const char *tablename, const void *ip,
234 const struct xt_match *match, void *matchinfo,
235 unsigned int matchsize, unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 const struct ipt_recent_info *info = matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700238 struct recent_table *t;
239 unsigned i;
240 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700242 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Patrick McHardya0e889b2006-06-09 12:17:41 -0700253 mutex_lock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700254 t = recent_table_lookup(info->name);
255 if (t != NULL) {
256 t->refcnt++;
257 ret = 1;
258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700261 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
Patrick McHardya0e889b2006-06-09 12:17:41 -0700262 GFP_KERNEL);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700263 if (t == NULL)
264 goto out;
Patrick McHardy2b2283d2006-06-09 12:18:17 -0700265 t->refcnt = 1;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700266 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#ifdef CONFIG_PROC_FS
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700271 t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
272 if (t->proc == NULL) {
273 kfree(t);
274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700276 t->proc->proc_fops = &recent_fops;
277 t->proc->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif
Patrick McHardya0e889b2006-06-09 12:17:41 -0700279 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700280 list_add_tail(&t->list, &tables);
Patrick McHardya0e889b2006-06-09 12:17:41 -0700281 spin_unlock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700282 ret = 1;
283out:
Patrick McHardya0e889b2006-06-09 12:17:41 -0700284 mutex_unlock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700289ipt_recent_destroy(const struct xt_match *match, void *matchinfo,
290 unsigned int matchsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 const struct ipt_recent_info *info = matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700293 struct recent_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Patrick McHardya0e889b2006-06-09 12:17:41 -0700295 mutex_lock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700296 t = recent_table_lookup(info->name);
297 if (--t->refcnt == 0) {
Patrick McHardya0e889b2006-06-09 12:17:41 -0700298 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700299 list_del(&t->list);
Patrick McHardya0e889b2006-06-09 12:17:41 -0700300 spin_unlock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700301 recent_table_flush(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#ifdef CONFIG_PROC_FS
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700303 remove_proc_entry(t->name, proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700305 kfree(t);
306 }
Patrick McHardya0e889b2006-06-09 12:17:41 -0700307 mutex_unlock(&recent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700310#ifdef CONFIG_PROC_FS
311struct recent_iter_state {
312 struct recent_table *table;
313 unsigned int bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700316static 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
334static 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
350static void recent_seq_stop(struct seq_file *s, void *v)
351{
352 spin_unlock_bh(&recent_lock);
353}
354
355static 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
369static 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
376static 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
395static 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
450static 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
459static 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
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800468static int __init ipt_recent_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700470 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700472 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 err = ipt_register_match(&recent_match);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700477#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (err)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700479 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return err;
487}
488
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700489static void __exit ipt_recent_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700491 BUG_ON(!list_empty(&tables));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 ipt_unregister_match(&recent_match);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700493#ifdef CONFIG_PROC_FS
494 remove_proc_entry("ipt_recent", proc_net);
495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800498module_init(ipt_recent_init);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700499module_exit(ipt_recent_exit);