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 <linux/slab.h>
31 #include <net/net_namespace.h>
32 #include <net/netns/generic.h>
34 #include <linux/netfilter/x_tables.h>
35 #include <linux/netfilter/xt_recent.h>
37 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
38 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
39 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
40 MODULE_LICENSE("GPL");
41 MODULE_ALIAS("ipt_recent");
42 MODULE_ALIAS("ip6t_recent");
44 static unsigned int ip_list_tot = 100;
45 static unsigned int ip_pkt_list_tot = 20;
46 static unsigned int ip_list_hash_size = 0;
47 static unsigned int ip_list_perms = 0644;
48 static unsigned int ip_list_uid = 0;
49 static unsigned int ip_list_gid = 0;
50 module_param(ip_list_tot, uint, 0400);
51 module_param(ip_pkt_list_tot, uint, 0400);
52 module_param(ip_list_hash_size, uint, 0400);
53 module_param(ip_list_perms, uint, 0400);
54 module_param(ip_list_uid, uint, 0400);
55 module_param(ip_list_gid, uint, 0400);
56 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
57 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
58 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
59 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
60 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
61 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
64 struct list_head list;
65 struct list_head lru_list;
66 union nf_inet_addr addr;
71 unsigned long stamps[0];
75 struct list_head list;
76 char name[XT_RECENT_NAME_LEN];
79 struct list_head lru_list;
80 struct list_head iphash[0];
84 struct list_head tables;
86 struct proc_dir_entry *xt_recent;
87 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
88 struct proc_dir_entry *ipt_recent;
93 static int recent_net_id;
94 static inline struct recent_net *recent_pernet(struct net *net)
96 return net_generic(net, recent_net_id);
99 static DEFINE_SPINLOCK(recent_lock);
100 static DEFINE_MUTEX(recent_mutex);
102 #ifdef CONFIG_PROC_FS
103 static const struct file_operations recent_old_fops, recent_mt_fops;
106 static u_int32_t hash_rnd __read_mostly;
107 static bool hash_rnd_inited __read_mostly;
109 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
111 return jhash_1word((__force u32)addr->ip, hash_rnd) &
112 (ip_list_hash_size - 1);
115 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
117 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
118 (ip_list_hash_size - 1);
121 static struct recent_entry *
122 recent_entry_lookup(const struct recent_table *table,
123 const union nf_inet_addr *addrp, u_int16_t family,
126 struct recent_entry *e;
129 if (family == NFPROTO_IPV4)
130 h = recent_entry_hash4(addrp);
132 h = recent_entry_hash6(addrp);
134 list_for_each_entry(e, &table->iphash[h], list)
135 if (e->family == family &&
136 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
137 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
142 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
145 list_del(&e->lru_list);
150 static struct recent_entry *
151 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
152 u_int16_t family, u_int8_t ttl)
154 struct recent_entry *e;
156 if (t->entries >= ip_list_tot) {
157 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
158 recent_entry_remove(t, e);
160 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
164 memcpy(&e->addr, addr, sizeof(e->addr));
166 e->stamps[0] = jiffies;
170 if (family == NFPROTO_IPV4)
171 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
173 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
174 list_add_tail(&e->lru_list, &t->lru_list);
179 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
181 e->index %= ip_pkt_list_tot;
182 e->stamps[e->index++] = jiffies;
183 if (e->index > e->nstamps)
184 e->nstamps = e->index;
185 list_move_tail(&e->lru_list, &t->lru_list);
188 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
191 struct recent_table *t;
193 list_for_each_entry(t, &recent_net->tables, list)
194 if (!strcmp(t->name, name))
199 static void recent_table_flush(struct recent_table *t)
201 struct recent_entry *e, *next;
204 for (i = 0; i < ip_list_hash_size; i++)
205 list_for_each_entry_safe(e, next, &t->iphash[i], list)
206 recent_entry_remove(t, e);
210 recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
212 struct net *net = dev_net(par->in ? par->in : par->out);
213 struct recent_net *recent_net = recent_pernet(net);
214 const struct xt_recent_mtinfo *info = par->matchinfo;
215 struct recent_table *t;
216 struct recent_entry *e;
217 union nf_inet_addr addr = {};
219 bool ret = info->invert;
221 if (par->match->family == NFPROTO_IPV4) {
222 const struct iphdr *iph = ip_hdr(skb);
224 if (info->side == XT_RECENT_DEST)
225 addr.ip = iph->daddr;
227 addr.ip = iph->saddr;
231 const struct ipv6hdr *iph = ipv6_hdr(skb);
233 if (info->side == XT_RECENT_DEST)
234 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
236 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
238 ttl = iph->hop_limit;
241 /* use TTL as seen before forwarding */
242 if (par->out != NULL && skb->sk == NULL)
245 spin_lock_bh(&recent_lock);
246 t = recent_table_lookup(recent_net, info->name);
247 e = recent_entry_lookup(t, &addr, par->match->family,
248 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
250 if (!(info->check_set & XT_RECENT_SET))
252 e = recent_entry_init(t, &addr, par->match->family, ttl);
254 *par->hotdrop = true;
259 if (info->check_set & XT_RECENT_SET)
261 else if (info->check_set & XT_RECENT_REMOVE) {
262 recent_entry_remove(t, e);
264 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
265 unsigned long time = jiffies - info->seconds * HZ;
266 unsigned int i, hits = 0;
268 for (i = 0; i < e->nstamps; i++) {
269 if (info->seconds && time_after(time, e->stamps[i]))
271 if (!info->hit_count || ++hits >= info->hit_count) {
278 if (info->check_set & XT_RECENT_SET ||
279 (info->check_set & XT_RECENT_UPDATE && ret)) {
280 recent_entry_update(t, e);
284 spin_unlock_bh(&recent_lock);
288 static bool recent_mt_check(const struct xt_mtchk_param *par)
290 struct recent_net *recent_net = recent_pernet(par->net);
291 const struct xt_recent_mtinfo *info = par->matchinfo;
292 struct recent_table *t;
293 #ifdef CONFIG_PROC_FS
294 struct proc_dir_entry *pde;
299 if (unlikely(!hash_rnd_inited)) {
300 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
301 hash_rnd_inited = true;
303 if (hweight8(info->check_set &
304 (XT_RECENT_SET | XT_RECENT_REMOVE |
305 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
307 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
308 (info->seconds || info->hit_count))
310 if (info->hit_count > ip_pkt_list_tot) {
311 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
312 "packets to be remembered (%u)\n",
313 info->hit_count, ip_pkt_list_tot);
316 if (info->name[0] == '\0' ||
317 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
320 mutex_lock(&recent_mutex);
321 t = recent_table_lookup(recent_net, info->name);
328 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
333 strcpy(t->name, info->name);
334 INIT_LIST_HEAD(&t->lru_list);
335 for (i = 0; i < ip_list_hash_size; i++)
336 INIT_LIST_HEAD(&t->iphash[i]);
337 #ifdef CONFIG_PROC_FS
338 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
344 pde->uid = ip_list_uid;
345 pde->gid = ip_list_gid;
346 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
347 pde = proc_create_data(t->name, ip_list_perms, recent_net->ipt_recent,
348 &recent_old_fops, t);
350 remove_proc_entry(t->name, recent_net->xt_recent);
354 pde->uid = ip_list_uid;
355 pde->gid = ip_list_gid;
358 spin_lock_bh(&recent_lock);
359 list_add_tail(&t->list, &recent_net->tables);
360 spin_unlock_bh(&recent_lock);
363 mutex_unlock(&recent_mutex);
367 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
369 struct recent_net *recent_net = recent_pernet(par->net);
370 const struct xt_recent_mtinfo *info = par->matchinfo;
371 struct recent_table *t;
373 mutex_lock(&recent_mutex);
374 t = recent_table_lookup(recent_net, info->name);
375 if (--t->refcnt == 0) {
376 spin_lock_bh(&recent_lock);
378 spin_unlock_bh(&recent_lock);
379 #ifdef CONFIG_PROC_FS
380 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
381 remove_proc_entry(t->name, recent_net->ipt_recent);
383 remove_proc_entry(t->name, recent_net->xt_recent);
385 recent_table_flush(t);
388 mutex_unlock(&recent_mutex);
391 #ifdef CONFIG_PROC_FS
392 struct recent_iter_state {
393 const struct recent_table *table;
397 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
398 __acquires(recent_lock)
400 struct recent_iter_state *st = seq->private;
401 const struct recent_table *t = st->table;
402 struct recent_entry *e;
405 spin_lock_bh(&recent_lock);
407 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
408 list_for_each_entry(e, &t->iphash[st->bucket], list)
414 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
416 struct recent_iter_state *st = seq->private;
417 const struct recent_table *t = st->table;
418 const struct recent_entry *e = v;
419 const struct list_head *head = e->list.next;
421 while (head == &t->iphash[st->bucket]) {
422 if (++st->bucket >= ip_list_hash_size)
424 head = t->iphash[st->bucket].next;
427 return list_entry(head, struct recent_entry, list);
430 static void recent_seq_stop(struct seq_file *s, void *v)
431 __releases(recent_lock)
433 spin_unlock_bh(&recent_lock);
436 static int recent_seq_show(struct seq_file *seq, void *v)
438 const struct recent_entry *e = v;
441 i = (e->index - 1) % ip_pkt_list_tot;
442 if (e->family == NFPROTO_IPV4)
443 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
444 &e->addr.ip, e->ttl, e->stamps[i], e->index);
446 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
447 &e->addr.in6, e->ttl, e->stamps[i], e->index);
448 for (i = 0; i < e->nstamps; i++)
449 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
450 seq_printf(seq, "\n");
454 static const struct seq_operations recent_seq_ops = {
455 .start = recent_seq_start,
456 .next = recent_seq_next,
457 .stop = recent_seq_stop,
458 .show = recent_seq_show,
461 static int recent_seq_open(struct inode *inode, struct file *file)
463 struct proc_dir_entry *pde = PDE(inode);
464 struct recent_iter_state *st;
466 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
470 st->table = pde->data;
474 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
475 static int recent_old_seq_open(struct inode *inode, struct file *filp)
477 static bool warned_of_old;
479 if (unlikely(!warned_of_old)) {
480 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
481 " is deprecated; use /proc/net/xt_recent.\n");
482 warned_of_old = true;
484 return recent_seq_open(inode, filp);
487 static ssize_t recent_old_proc_write(struct file *file,
488 const char __user *input,
489 size_t size, loff_t *loff)
491 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
492 struct recent_table *t = pde->data;
493 struct recent_entry *e;
494 char buf[sizeof("+255.255.255.255")], *c = buf;
495 union nf_inet_addr addr = {};
498 if (size > sizeof(buf))
500 if (copy_from_user(buf, input, size))
505 if (size - (c - buf) < 5)
507 if (!strncmp(c, "clear", 5)) {
509 spin_lock_bh(&recent_lock);
510 recent_table_flush(t);
511 spin_unlock_bh(&recent_lock);
526 addr.ip = in_aton(c);
528 spin_lock_bh(&recent_lock);
529 e = recent_entry_lookup(t, &addr, NFPROTO_IPV4, 0);
532 recent_entry_init(t, &addr, NFPROTO_IPV4, 0);
535 recent_entry_update(t, e);
537 recent_entry_remove(t, e);
539 spin_unlock_bh(&recent_lock);
543 static const struct file_operations recent_old_fops = {
544 .open = recent_old_seq_open,
546 .write = recent_old_proc_write,
547 .release = seq_release_private,
548 .owner = THIS_MODULE,
553 recent_mt_proc_write(struct file *file, const char __user *input,
554 size_t size, loff_t *loff)
556 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
557 struct recent_table *t = pde->data;
558 struct recent_entry *e;
559 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
561 union nf_inet_addr addr = {};
567 if (size > sizeof(buf))
569 if (copy_from_user(buf, input, size) != 0)
572 /* Strict protocol! */
576 case '/': /* flush table */
577 spin_lock_bh(&recent_lock);
578 recent_table_flush(t);
579 spin_unlock_bh(&recent_lock);
581 case '-': /* remove address */
584 case '+': /* add address */
588 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
594 if (strnchr(c, size, ':') != NULL) {
595 family = NFPROTO_IPV6;
596 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
598 family = NFPROTO_IPV4;
599 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
603 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
608 spin_lock_bh(&recent_lock);
609 e = recent_entry_lookup(t, &addr, family, 0);
612 recent_entry_init(t, &addr, family, 0);
615 recent_entry_update(t, e);
617 recent_entry_remove(t, e);
619 spin_unlock_bh(&recent_lock);
620 /* Note we removed one above */
625 static const struct file_operations recent_mt_fops = {
626 .open = recent_seq_open,
628 .write = recent_mt_proc_write,
629 .release = seq_release_private,
630 .owner = THIS_MODULE,
633 static int __net_init recent_proc_net_init(struct net *net)
635 struct recent_net *recent_net = recent_pernet(net);
637 recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
638 if (!recent_net->xt_recent)
640 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
641 recent_net->ipt_recent = proc_mkdir("ipt_recent", net->proc_net);
642 if (!recent_net->ipt_recent) {
643 proc_net_remove(net, "xt_recent");
650 static void __net_exit recent_proc_net_exit(struct net *net)
652 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
653 proc_net_remove(net, "ipt_recent");
655 proc_net_remove(net, "xt_recent");
658 static inline int recent_proc_net_init(struct net *net)
663 static inline void recent_proc_net_exit(struct net *net)
666 #endif /* CONFIG_PROC_FS */
668 static int __net_init recent_net_init(struct net *net)
670 struct recent_net *recent_net = recent_pernet(net);
672 INIT_LIST_HEAD(&recent_net->tables);
673 return recent_proc_net_init(net);
676 static void __net_exit recent_net_exit(struct net *net)
678 struct recent_net *recent_net = recent_pernet(net);
680 BUG_ON(!list_empty(&recent_net->tables));
681 recent_proc_net_exit(net);
684 static struct pernet_operations recent_net_ops = {
685 .init = recent_net_init,
686 .exit = recent_net_exit,
687 .id = &recent_net_id,
688 .size = sizeof(struct recent_net),
691 static struct xt_match recent_mt_reg[] __read_mostly = {
695 .family = NFPROTO_IPV4,
697 .matchsize = sizeof(struct xt_recent_mtinfo),
698 .checkentry = recent_mt_check,
699 .destroy = recent_mt_destroy,
705 .family = NFPROTO_IPV6,
707 .matchsize = sizeof(struct xt_recent_mtinfo),
708 .checkentry = recent_mt_check,
709 .destroy = recent_mt_destroy,
714 static int __init recent_mt_init(void)
718 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
720 ip_list_hash_size = 1 << fls(ip_list_tot);
722 err = register_pernet_subsys(&recent_net_ops);
725 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
727 unregister_pernet_subsys(&recent_net_ops);
731 static void __exit recent_mt_exit(void)
733 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
734 unregister_pernet_subsys(&recent_net_ops);
737 module_init(recent_mt_init);
738 module_exit(recent_mt_exit);