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);
74 static struct proc_dir_entry *proc_dir;
75 static struct file_operations recent_fops;
78 static u_int32_t hash_rnd;
79 static int hash_rnd_initted;
81 static unsigned int recent_entry_hash(u_int32_t addr)
83 if (!hash_rnd_initted) {
84 get_random_bytes(&hash_rnd, 4);
87 return jhash_1word(addr, hash_rnd) & (ip_list_hash_size - 1);
90 static struct recent_entry *
91 recent_entry_lookup(const struct recent_table *table, u_int32_t addr, u_int8_t ttl)
93 struct recent_entry *e;
96 h = recent_entry_hash(addr);
97 list_for_each_entry(e, &table->iphash[h], list)
98 if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl))
103 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
106 list_del(&e->lru_list);
111 static struct recent_entry *
112 recent_entry_init(struct recent_table *t, u_int32_t addr, u_int8_t ttl)
114 struct recent_entry *e;
116 if (t->entries >= ip_list_tot) {
117 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
118 recent_entry_remove(t, e);
120 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
126 e->stamps[0] = jiffies;
129 list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
130 list_add_tail(&e->lru_list, &t->lru_list);
135 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
137 e->stamps[e->index++] = jiffies;
138 if (e->index > e->nstamps)
139 e->nstamps = e->index;
140 e->index %= ip_pkt_list_tot;
141 list_move_tail(&e->lru_list, &t->lru_list);
144 static struct recent_table *recent_table_lookup(const char *name)
146 struct recent_table *t;
148 list_for_each_entry(t, &tables, list)
149 if (!strcmp(t->name, name))
154 static void recent_table_flush(struct recent_table *t)
156 struct recent_entry *e, *next;
159 for (i = 0; i < ip_list_hash_size; i++) {
160 list_for_each_entry_safe(e, next, &t->iphash[i], list)
161 recent_entry_remove(t, e);
166 ipt_recent_match(const struct sk_buff *skb,
167 const struct net_device *in, const struct net_device *out,
168 const struct xt_match *match, const void *matchinfo,
169 int offset, unsigned int protoff, int *hotdrop)
171 const struct ipt_recent_info *info = matchinfo;
172 struct recent_table *t;
173 struct recent_entry *e;
176 int ret = info->invert;
178 if (info->side == IPT_RECENT_DEST)
179 addr = skb->nh.iph->daddr;
181 addr = skb->nh.iph->saddr;
183 ttl = skb->nh.iph->ttl;
184 /* use TTL as seen before forwarding */
188 spin_lock_bh(&recent_lock);
189 t = recent_table_lookup(info->name);
190 e = recent_entry_lookup(t, addr,
191 info->check_set & IPT_RECENT_TTL ? ttl : 0);
193 if (!(info->check_set & IPT_RECENT_SET))
195 e = recent_entry_init(t, addr, ttl);
202 if (info->check_set & IPT_RECENT_SET)
204 else if (info->check_set & IPT_RECENT_REMOVE) {
205 recent_entry_remove(t, e);
207 } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) {
208 unsigned long t = jiffies - info->seconds * HZ;
209 unsigned int i, hits = 0;
211 for (i = 0; i < e->nstamps; i++) {
212 if (info->seconds && time_after(t, e->stamps[i]))
214 if (++hits >= info->hit_count) {
221 if (info->check_set & IPT_RECENT_SET ||
222 (info->check_set & IPT_RECENT_UPDATE && ret)) {
223 recent_entry_update(t, e);
227 spin_unlock_bh(&recent_lock);
232 ipt_recent_checkentry(const char *tablename, const void *ip,
233 const struct xt_match *match, void *matchinfo,
234 unsigned int matchsize, unsigned int hook_mask)
236 const struct ipt_recent_info *info = matchinfo;
237 struct recent_table *t;
241 if (hweight8(info->check_set &
242 (IPT_RECENT_SET | IPT_RECENT_REMOVE |
243 IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
245 if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
246 (info->seconds || info->hit_count))
248 if (info->name[0] == '\0' ||
249 strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
252 spin_lock_bh(&recent_lock);
253 t = recent_table_lookup(info->name);
260 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
264 strcpy(t->name, info->name);
265 INIT_LIST_HEAD(&t->lru_list);
266 for (i = 0; i < ip_list_hash_size; i++)
267 INIT_LIST_HEAD(&t->iphash[i]);
268 #ifdef CONFIG_PROC_FS
269 t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir);
270 if (t->proc == NULL) {
274 t->proc->proc_fops = &recent_fops;
277 list_add_tail(&t->list, &tables);
280 spin_unlock_bh(&recent_lock);
285 ipt_recent_destroy(const struct xt_match *match, void *matchinfo,
286 unsigned int matchsize)
288 const struct ipt_recent_info *info = matchinfo;
289 struct recent_table *t;
291 spin_lock_bh(&recent_lock);
292 t = recent_table_lookup(info->name);
293 if (--t->refcnt == 0) {
295 recent_table_flush(t);
296 #ifdef CONFIG_PROC_FS
297 remove_proc_entry(t->name, proc_dir);
301 spin_unlock_bh(&recent_lock);
304 #ifdef CONFIG_PROC_FS
305 struct recent_iter_state {
306 struct recent_table *table;
310 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
312 struct recent_iter_state *st = seq->private;
313 struct recent_table *t = st->table;
314 struct recent_entry *e;
317 spin_lock_bh(&recent_lock);
319 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
320 list_for_each_entry(e, &t->iphash[st->bucket], list) {
328 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
330 struct recent_iter_state *st = seq->private;
331 struct recent_table *t = st->table;
332 struct recent_entry *e = v;
333 struct list_head *head = e->list.next;
335 while (head == &t->iphash[st->bucket]) {
336 if (++st->bucket >= ip_list_hash_size)
338 head = t->iphash[st->bucket].next;
341 return list_entry(head, struct recent_entry, list);
344 static void recent_seq_stop(struct seq_file *s, void *v)
346 spin_unlock_bh(&recent_lock);
349 static int recent_seq_show(struct seq_file *seq, void *v)
351 struct recent_entry *e = v;
354 i = (e->index - 1) % ip_pkt_list_tot;
355 seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
356 NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index);
357 for (i = 0; i < e->nstamps; i++)
358 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
359 seq_printf(seq, "\n");
363 static struct seq_operations recent_seq_ops = {
364 .start = recent_seq_start,
365 .next = recent_seq_next,
366 .stop = recent_seq_stop,
367 .show = recent_seq_show,
370 static int recent_seq_open(struct inode *inode, struct file *file)
372 struct proc_dir_entry *pde = PDE(inode);
373 struct seq_file *seq;
374 struct recent_iter_state *st;
377 st = kzalloc(sizeof(*st), GFP_KERNEL);
380 ret = seq_open(file, &recent_seq_ops);
383 st->table = pde->data;
384 seq = file->private_data;
389 static ssize_t recent_proc_write(struct file *file, const char __user *input,
390 size_t size, loff_t *loff)
392 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
393 struct recent_table *t = pde->data;
394 struct recent_entry *e;
395 char buf[sizeof("+255.255.255.255")], *c = buf;
399 if (size > sizeof(buf))
401 if (copy_from_user(buf, input, size))
406 if (size - (c - buf) < 5)
408 if (!strncmp(c, "clear", 5)) {
410 spin_lock_bh(&recent_lock);
411 recent_table_flush(t);
412 spin_unlock_bh(&recent_lock);
429 spin_lock_bh(&recent_lock);
430 e = recent_entry_lookup(t, addr, 0);
433 recent_entry_init(t, addr, 0);
436 recent_entry_update(t, e);
438 recent_entry_remove(t, e);
440 spin_unlock_bh(&recent_lock);
444 static struct file_operations recent_fops = {
445 .open = recent_seq_open,
447 .write = recent_proc_write,
448 .release = seq_release_private,
449 .owner = THIS_MODULE,
451 #endif /* CONFIG_PROC_FS */
453 static struct ipt_match recent_match = {
455 .match = ipt_recent_match,
456 .matchsize = sizeof(struct ipt_recent_info),
457 .checkentry = ipt_recent_checkentry,
458 .destroy = ipt_recent_destroy,
462 static int __init ipt_recent_init(void)
466 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
468 ip_list_hash_size = 1 << fls(ip_list_tot);
470 err = ipt_register_match(&recent_match);
471 #ifdef CONFIG_PROC_FS
474 proc_dir = proc_mkdir("ipt_recent", proc_net);
475 if (proc_dir == NULL) {
476 ipt_unregister_match(&recent_match);
483 static void __exit ipt_recent_exit(void)
485 BUG_ON(!list_empty(&tables));
486 ipt_unregister_match(&recent_match);
487 #ifdef CONFIG_PROC_FS
488 remove_proc_entry("ipt_recent", proc_net);
492 module_init(ipt_recent_init);
493 module_exit(ipt_recent_exit);