2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
3 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This is a replacement of the old ipt_recent module, which carried the
10 * following copyright notice:
12 * Author: Stephen Frost <sfrost@snowman.net>
13 * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
15 #include <linux/init.h>
17 #include <linux/ipv6.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/list.h>
25 #include <linux/random.h>
26 #include <linux/jhash.h>
27 #include <linux/bitops.h>
28 #include <linux/skbuff.h>
29 #include <linux/inet.h>
30 #include <net/net_namespace.h>
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter/xt_recent.h>
35 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
36 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
37 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
38 MODULE_LICENSE("GPL");
39 MODULE_ALIAS("ipt_recent");
40 MODULE_ALIAS("ip6t_recent");
42 static unsigned int ip_list_tot = 100;
43 static unsigned int ip_pkt_list_tot = 20;
44 static unsigned int ip_list_hash_size = 0;
45 static unsigned int ip_list_perms = 0644;
46 static unsigned int ip_list_uid = 0;
47 static unsigned int ip_list_gid = 0;
48 module_param(ip_list_tot, uint, 0400);
49 module_param(ip_pkt_list_tot, uint, 0400);
50 module_param(ip_list_hash_size, uint, 0400);
51 module_param(ip_list_perms, uint, 0400);
52 module_param(ip_list_uid, uint, 0400);
53 module_param(ip_list_gid, uint, 0400);
54 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
55 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
56 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
57 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
58 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
59 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
62 struct list_head list;
63 struct list_head lru_list;
64 union nf_inet_addr addr;
69 unsigned long stamps[0];
73 struct list_head list;
74 char name[XT_RECENT_NAME_LEN];
76 struct proc_dir_entry *proc_old, *proc;
80 struct list_head lru_list;
81 struct list_head iphash[0];
84 static LIST_HEAD(tables);
85 static DEFINE_SPINLOCK(recent_lock);
86 static DEFINE_MUTEX(recent_mutex);
89 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
90 static struct proc_dir_entry *proc_old_dir;
92 static struct proc_dir_entry *recent_proc_dir;
93 static const struct file_operations recent_old_fops, recent_mt_fops;
96 static u_int32_t hash_rnd;
97 static bool hash_rnd_initted;
99 static unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
101 if (!hash_rnd_initted) {
102 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
103 hash_rnd_initted = true;
105 return jhash_1word((__force u32)addr->ip, hash_rnd) &
106 (ip_list_hash_size - 1);
109 static unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
111 if (!hash_rnd_initted) {
112 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
113 hash_rnd_initted = true;
115 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
116 (ip_list_hash_size - 1);
119 static struct recent_entry *
120 recent_entry_lookup(const struct recent_table *table,
121 const union nf_inet_addr *addrp, u_int16_t family,
124 struct recent_entry *e;
127 if (family == NFPROTO_IPV4)
128 h = recent_entry_hash4(addrp);
130 h = recent_entry_hash6(addrp);
132 list_for_each_entry(e, &table->iphash[h], list)
133 if (e->family == family &&
134 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
135 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
140 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
143 list_del(&e->lru_list);
148 static struct recent_entry *
149 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
150 u_int16_t family, u_int8_t ttl)
152 struct recent_entry *e;
154 if (t->entries >= ip_list_tot) {
155 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
156 recent_entry_remove(t, e);
158 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
162 memcpy(&e->addr, addr, sizeof(e->addr));
164 e->stamps[0] = jiffies;
168 if (family == NFPROTO_IPV4)
169 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
171 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
172 list_add_tail(&e->lru_list, &t->lru_list);
177 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
179 e->stamps[e->index++] = jiffies;
180 if (e->index > e->nstamps)
181 e->nstamps = e->index;
182 e->index %= ip_pkt_list_tot;
183 list_move_tail(&e->lru_list, &t->lru_list);
186 static struct recent_table *recent_table_lookup(const char *name)
188 struct recent_table *t;
190 list_for_each_entry(t, &tables, list)
191 if (!strcmp(t->name, name))
196 static void recent_table_flush(struct recent_table *t)
198 struct recent_entry *e, *next;
201 for (i = 0; i < ip_list_hash_size; i++)
202 list_for_each_entry_safe(e, next, &t->iphash[i], list)
203 recent_entry_remove(t, e);
207 recent_mt(const struct sk_buff *skb, const struct net_device *in,
208 const struct net_device *out, const struct xt_match *match,
209 const void *matchinfo, int offset, unsigned int protoff,
212 const struct xt_recent_mtinfo *info = matchinfo;
213 struct recent_table *t;
214 struct recent_entry *e;
215 union nf_inet_addr addr = {};
217 bool ret = info->invert;
219 if (match->family == NFPROTO_IPV4) {
220 const struct iphdr *iph = ip_hdr(skb);
222 if (info->side == XT_RECENT_DEST)
223 addr.ip = iph->daddr;
225 addr.ip = iph->saddr;
229 const struct ipv6hdr *iph = ipv6_hdr(skb);
231 if (info->side == XT_RECENT_DEST)
232 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
234 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
236 ttl = iph->hop_limit;
239 /* use TTL as seen before forwarding */
243 spin_lock_bh(&recent_lock);
244 t = recent_table_lookup(info->name);
245 e = recent_entry_lookup(t, &addr, match->family,
246 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
248 if (!(info->check_set & XT_RECENT_SET))
250 e = recent_entry_init(t, &addr, match->family, ttl);
257 if (info->check_set & XT_RECENT_SET)
259 else if (info->check_set & XT_RECENT_REMOVE) {
260 recent_entry_remove(t, e);
262 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
263 unsigned long time = jiffies - info->seconds * HZ;
264 unsigned int i, hits = 0;
266 for (i = 0; i < e->nstamps; i++) {
267 if (info->seconds && time_after(time, e->stamps[i]))
269 if (++hits >= info->hit_count) {
276 if (info->check_set & XT_RECENT_SET ||
277 (info->check_set & XT_RECENT_UPDATE && ret)) {
278 recent_entry_update(t, e);
282 spin_unlock_bh(&recent_lock);
287 recent_mt_check(const char *tablename, const void *ip,
288 const struct xt_match *match, void *matchinfo,
289 unsigned int hook_mask)
291 const struct xt_recent_mtinfo *info = matchinfo;
292 struct recent_table *t;
296 if (hweight8(info->check_set &
297 (XT_RECENT_SET | XT_RECENT_REMOVE |
298 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
300 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
301 (info->seconds || info->hit_count))
303 if (info->hit_count > ip_pkt_list_tot)
305 if (info->name[0] == '\0' ||
306 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
309 mutex_lock(&recent_mutex);
310 t = recent_table_lookup(info->name);
317 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
322 strcpy(t->name, info->name);
323 INIT_LIST_HEAD(&t->lru_list);
324 for (i = 0; i < ip_list_hash_size; i++)
325 INIT_LIST_HEAD(&t->iphash[i]);
326 #ifdef CONFIG_PROC_FS
327 t->proc = proc_create(t->name, ip_list_perms, recent_proc_dir,
329 if (t->proc == NULL) {
333 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
334 t->proc_old = proc_create(t->name, ip_list_perms, proc_old_dir,
336 if (t->proc_old == NULL) {
337 remove_proc_entry(t->name, proc_old_dir);
341 t->proc_old->uid = ip_list_uid;
342 t->proc_old->gid = ip_list_gid;
343 t->proc_old->data = t;
345 t->proc->uid = ip_list_uid;
346 t->proc->gid = ip_list_gid;
349 spin_lock_bh(&recent_lock);
350 list_add_tail(&t->list, &tables);
351 spin_unlock_bh(&recent_lock);
354 mutex_unlock(&recent_mutex);
358 static void recent_mt_destroy(const struct xt_match *match, void *matchinfo)
360 const struct xt_recent_mtinfo *info = matchinfo;
361 struct recent_table *t;
363 mutex_lock(&recent_mutex);
364 t = recent_table_lookup(info->name);
365 if (--t->refcnt == 0) {
366 spin_lock_bh(&recent_lock);
368 spin_unlock_bh(&recent_lock);
369 #ifdef CONFIG_PROC_FS
370 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
371 remove_proc_entry(t->name, proc_old_dir);
373 remove_proc_entry(t->name, recent_proc_dir);
375 recent_table_flush(t);
378 mutex_unlock(&recent_mutex);
381 #ifdef CONFIG_PROC_FS
382 struct recent_iter_state {
383 const struct recent_table *table;
387 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
388 __acquires(recent_lock)
390 struct recent_iter_state *st = seq->private;
391 const struct recent_table *t = st->table;
392 struct recent_entry *e;
395 spin_lock_bh(&recent_lock);
397 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
398 list_for_each_entry(e, &t->iphash[st->bucket], list)
404 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
406 struct recent_iter_state *st = seq->private;
407 const struct recent_table *t = st->table;
408 const struct recent_entry *e = v;
409 const struct list_head *head = e->list.next;
411 while (head == &t->iphash[st->bucket]) {
412 if (++st->bucket >= ip_list_hash_size)
414 head = t->iphash[st->bucket].next;
417 return list_entry(head, struct recent_entry, list);
420 static void recent_seq_stop(struct seq_file *s, void *v)
421 __releases(recent_lock)
423 spin_unlock_bh(&recent_lock);
426 static int recent_seq_show(struct seq_file *seq, void *v)
428 const struct recent_entry *e = v;
431 i = (e->index - 1) % ip_pkt_list_tot;
432 if (e->family == NFPROTO_IPV4)
433 seq_printf(seq, "src=" NIPQUAD_FMT " ttl: %u last_seen: %lu "
434 "oldest_pkt: %u", NIPQUAD(e->addr.ip), e->ttl,
435 e->stamps[i], e->index);
437 seq_printf(seq, "src=" NIP6_FMT " ttl: %u last_seen: %lu "
438 "oldest_pkt: %u", NIP6(e->addr.in6), e->ttl,
439 e->stamps[i], e->index);
440 for (i = 0; i < e->nstamps; i++)
441 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
442 seq_printf(seq, "\n");
446 static const struct seq_operations recent_seq_ops = {
447 .start = recent_seq_start,
448 .next = recent_seq_next,
449 .stop = recent_seq_stop,
450 .show = recent_seq_show,
453 static int recent_seq_open(struct inode *inode, struct file *file)
455 struct proc_dir_entry *pde = PDE(inode);
456 struct recent_iter_state *st;
458 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
462 st->table = pde->data;
466 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
467 static int recent_old_seq_open(struct inode *inode, struct file *filp)
469 static bool warned_of_old;
471 if (unlikely(!warned_of_old)) {
472 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
473 " is deprecated; use /proc/net/xt_recent.\n");
474 warned_of_old = true;
476 return recent_seq_open(inode, filp);
479 static ssize_t recent_old_proc_write(struct file *file,
480 const char __user *input,
481 size_t size, loff_t *loff)
483 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
484 struct recent_table *t = pde->data;
485 struct recent_entry *e;
486 char buf[sizeof("+255.255.255.255")], *c = buf;
490 if (size > sizeof(buf))
492 if (copy_from_user(buf, input, size))
498 if (size - (c - buf) < 5)
500 if (!strncmp(c, "clear", 5)) {
502 spin_lock_bh(&recent_lock);
503 recent_table_flush(t);
504 spin_unlock_bh(&recent_lock);
521 spin_lock_bh(&recent_lock);
522 e = recent_entry_lookup(t, (const void *)&addr, NFPROTO_IPV4, 0);
525 recent_entry_init(t, (const void *)&addr,
529 recent_entry_update(t, e);
531 recent_entry_remove(t, e);
533 spin_unlock_bh(&recent_lock);
537 static const struct file_operations recent_old_fops = {
538 .open = recent_old_seq_open,
540 .write = recent_old_proc_write,
541 .release = seq_release_private,
542 .owner = THIS_MODULE,
547 recent_mt_proc_write(struct file *file, const char __user *input,
548 size_t size, loff_t *loff)
550 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
551 struct recent_table *t = pde->data;
552 struct recent_entry *e;
553 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
555 union nf_inet_addr addr;
561 if (size > sizeof(buf))
563 if (copy_from_user(buf, input, size) != 0)
566 /* Strict protocol! */
570 case '/': /* flush table */
571 spin_lock_bh(&recent_lock);
572 recent_table_flush(t);
573 spin_unlock_bh(&recent_lock);
575 case '-': /* remove address */
578 case '+': /* add address */
582 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
588 if (strnchr(c, size, ':') != NULL) {
589 family = NFPROTO_IPV6;
590 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
592 family = NFPROTO_IPV4;
593 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
597 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
602 spin_lock_bh(&recent_lock);
603 e = recent_entry_lookup(t, &addr, family, 0);
606 recent_entry_init(t, &addr, family, 0);
609 recent_entry_update(t, e);
611 recent_entry_remove(t, e);
613 spin_unlock_bh(&recent_lock);
614 /* Note we removed one above */
619 static const struct file_operations recent_mt_fops = {
620 .open = recent_seq_open,
622 .write = recent_mt_proc_write,
623 .release = seq_release_private,
624 .owner = THIS_MODULE,
626 #endif /* CONFIG_PROC_FS */
628 static struct xt_match recent_mt_reg[] __read_mostly = {
632 .family = NFPROTO_IPV4,
634 .matchsize = sizeof(struct xt_recent_mtinfo),
635 .checkentry = recent_mt_check,
636 .destroy = recent_mt_destroy,
642 .family = NFPROTO_IPV6,
644 .matchsize = sizeof(struct xt_recent_mtinfo),
645 .checkentry = recent_mt_check,
646 .destroy = recent_mt_destroy,
651 static int __init recent_mt_init(void)
655 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
657 ip_list_hash_size = 1 << fls(ip_list_tot);
659 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
660 #ifdef CONFIG_PROC_FS
663 recent_proc_dir = proc_mkdir("xt_recent", init_net.proc_net);
664 if (recent_proc_dir == NULL) {
665 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
668 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
671 proc_old_dir = proc_mkdir("ipt_recent", init_net.proc_net);
672 if (proc_old_dir == NULL) {
673 remove_proc_entry("xt_recent", init_net.proc_net);
674 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
682 static void __exit recent_mt_exit(void)
684 BUG_ON(!list_empty(&tables));
685 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
686 #ifdef CONFIG_PROC_FS
687 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
688 remove_proc_entry("ipt_recent", init_net.proc_net);
690 remove_proc_entry("xt_recent", init_net.proc_net);
694 module_init(recent_mt_init);
695 module_exit(recent_mt_exit);