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/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>
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netfilter_ipv4/ipt_recent.h>
30 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
31 MODULE_DESCRIPTION("IP tables recently seen matching module");
32 MODULE_LICENSE("GPL");
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");
49 struct list_head list;
50 struct list_head lru_list;
55 unsigned long stamps[0];
59 struct list_head list;
60 char name[IPT_RECENT_NAME_LEN];
62 struct proc_dir_entry *proc;
66 struct list_head lru_list;
67 struct list_head iphash[0];
70 static LIST_HEAD(tables);
71 static DEFINE_SPINLOCK(recent_lock);
72 static DEFINE_MUTEX(recent_mutex);
75 static struct proc_dir_entry *proc_dir;
76 static struct file_operations recent_fops;
79 static u_int32_t hash_rnd;
80 static int hash_rnd_initted;
82 static unsigned int recent_entry_hash(u_int32_t addr)
84 if (!hash_rnd_initted) {
85 get_random_bytes(&hash_rnd, 4);
88 return jhash_1word(addr, hash_rnd) & (ip_list_hash_size - 1);
91 static struct recent_entry *
92 recent_entry_lookup(const struct recent_table *table, u_int32_t addr, u_int8_t ttl)
94 struct recent_entry *e;
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))
104 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
107 list_del(&e->lru_list);
112 static struct recent_entry *
113 recent_entry_init(struct recent_table *t, u_int32_t addr, u_int8_t ttl)
115 struct recent_entry *e;
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);
121 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
127 e->stamps[0] = jiffies;
130 list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
131 list_add_tail(&e->lru_list, &t->lru_list);
136 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
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);
145 static struct recent_table *recent_table_lookup(const char *name)
147 struct recent_table *t;
149 list_for_each_entry(t, &tables, list)
150 if (!strcmp(t->name, name))
155 static void recent_table_flush(struct recent_table *t)
157 struct recent_entry *e, *next;
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);
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)
172 const struct ipt_recent_info *info = matchinfo;
173 struct recent_table *t;
174 struct recent_entry *e;
177 int ret = info->invert;
179 if (info->side == IPT_RECENT_DEST)
180 addr = skb->nh.iph->daddr;
182 addr = skb->nh.iph->saddr;
184 ttl = skb->nh.iph->ttl;
185 /* use TTL as seen before forwarding */
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);
194 if (!(info->check_set & IPT_RECENT_SET))
196 e = recent_entry_init(t, addr, ttl);
203 if (info->check_set & IPT_RECENT_SET)
205 else if (info->check_set & IPT_RECENT_REMOVE) {
206 recent_entry_remove(t, e);
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;
212 for (i = 0; i < e->nstamps; i++) {
213 if (info->seconds && time_after(t, e->stamps[i]))
215 if (++hits >= info->hit_count) {
222 if (info->check_set & IPT_RECENT_SET ||
223 (info->check_set & IPT_RECENT_UPDATE && ret)) {
224 recent_entry_update(t, e);
228 spin_unlock_bh(&recent_lock);
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)
237 const struct ipt_recent_info *info = matchinfo;
238 struct recent_table *t;
242 if (hweight8(info->check_set &
243 (IPT_RECENT_SET | IPT_RECENT_REMOVE |
244 IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
246 if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
247 (info->seconds || info->hit_count))
249 if (info->name[0] == '\0' ||
250 strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
253 mutex_lock(&recent_mutex);
254 t = recent_table_lookup(info->name);
261 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
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) {
276 t->proc->proc_fops = &recent_fops;
279 spin_lock_bh(&recent_lock);
280 list_add_tail(&t->list, &tables);
281 spin_unlock_bh(&recent_lock);
284 mutex_unlock(&recent_mutex);
289 ipt_recent_destroy(const struct xt_match *match, void *matchinfo,
290 unsigned int matchsize)
292 const struct ipt_recent_info *info = matchinfo;
293 struct recent_table *t;
295 mutex_lock(&recent_mutex);
296 t = recent_table_lookup(info->name);
297 if (--t->refcnt == 0) {
298 spin_lock_bh(&recent_lock);
300 spin_unlock_bh(&recent_lock);
301 recent_table_flush(t);
302 #ifdef CONFIG_PROC_FS
303 remove_proc_entry(t->name, proc_dir);
307 mutex_unlock(&recent_mutex);
310 #ifdef CONFIG_PROC_FS
311 struct recent_iter_state {
312 struct recent_table *table;
316 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
318 struct recent_iter_state *st = seq->private;
319 struct recent_table *t = st->table;
320 struct recent_entry *e;
323 spin_lock_bh(&recent_lock);
325 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
326 list_for_each_entry(e, &t->iphash[st->bucket], list) {
334 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
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;
341 while (head == &t->iphash[st->bucket]) {
342 if (++st->bucket >= ip_list_hash_size)
344 head = t->iphash[st->bucket].next;
347 return list_entry(head, struct recent_entry, list);
350 static void recent_seq_stop(struct seq_file *s, void *v)
352 spin_unlock_bh(&recent_lock);
355 static int recent_seq_show(struct seq_file *seq, void *v)
357 struct recent_entry *e = v;
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");
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,
376 static int recent_seq_open(struct inode *inode, struct file *file)
378 struct proc_dir_entry *pde = PDE(inode);
379 struct seq_file *seq;
380 struct recent_iter_state *st;
383 st = kzalloc(sizeof(*st), GFP_KERNEL);
386 ret = seq_open(file, &recent_seq_ops);
389 st->table = pde->data;
390 seq = file->private_data;
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_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;
405 if (size > sizeof(buf))
407 if (copy_from_user(buf, input, size))
412 if (size - (c - buf) < 5)
414 if (!strncmp(c, "clear", 5)) {
416 spin_lock_bh(&recent_lock);
417 recent_table_flush(t);
418 spin_unlock_bh(&recent_lock);
435 spin_lock_bh(&recent_lock);
436 e = recent_entry_lookup(t, addr, 0);
439 recent_entry_init(t, addr, 0);
442 recent_entry_update(t, e);
444 recent_entry_remove(t, e);
446 spin_unlock_bh(&recent_lock);
450 static struct file_operations recent_fops = {
451 .open = recent_seq_open,
453 .write = recent_proc_write,
454 .release = seq_release_private,
455 .owner = THIS_MODULE,
457 #endif /* CONFIG_PROC_FS */
459 static struct ipt_match recent_match = {
461 .match = ipt_recent_match,
462 .matchsize = sizeof(struct ipt_recent_info),
463 .checkentry = ipt_recent_checkentry,
464 .destroy = ipt_recent_destroy,
468 static int __init ipt_recent_init(void)
472 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
474 ip_list_hash_size = 1 << fls(ip_list_tot);
476 err = ipt_register_match(&recent_match);
477 #ifdef CONFIG_PROC_FS
480 proc_dir = proc_mkdir("ipt_recent", proc_net);
481 if (proc_dir == NULL) {
482 ipt_unregister_match(&recent_match);
489 static void __exit ipt_recent_exit(void)
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);
498 module_init(ipt_recent_init);
499 module_exit(ipt_recent_exit);