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>
31 #include <net/netns/generic.h>
33 #include <linux/netfilter/x_tables.h>
34 #include <linux/netfilter/xt_recent.h>
36 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
37 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
38 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("ipt_recent");
41 MODULE_ALIAS("ip6t_recent");
43 static unsigned int ip_list_tot = 100;
44 static unsigned int ip_pkt_list_tot = 20;
45 static unsigned int ip_list_hash_size = 0;
46 static unsigned int ip_list_perms = 0644;
47 static unsigned int ip_list_uid = 0;
48 static unsigned int ip_list_gid = 0;
49 module_param(ip_list_tot, uint, 0400);
50 module_param(ip_pkt_list_tot, uint, 0400);
51 module_param(ip_list_hash_size, uint, 0400);
52 module_param(ip_list_perms, uint, 0400);
53 module_param(ip_list_uid, uint, 0400);
54 module_param(ip_list_gid, uint, 0400);
55 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
56 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
57 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
58 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
59 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
60 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
63 struct list_head list;
64 struct list_head lru_list;
65 union nf_inet_addr addr;
70 unsigned long stamps[0];
74 struct list_head list;
75 char name[XT_RECENT_NAME_LEN];
78 struct list_head lru_list;
79 struct list_head iphash[0];
83 struct list_head tables;
85 struct proc_dir_entry *xt_recent;
89 static int recent_net_id;
90 static inline struct recent_net *recent_pernet(struct net *net)
92 return net_generic(net, recent_net_id);
95 static DEFINE_SPINLOCK(recent_lock);
96 static DEFINE_MUTEX(recent_mutex);
99 static const struct file_operations recent_old_fops, recent_mt_fops;
102 static u_int32_t hash_rnd __read_mostly;
103 static bool hash_rnd_inited __read_mostly;
105 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
107 return jhash_1word((__force u32)addr->ip, hash_rnd) &
108 (ip_list_hash_size - 1);
111 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
113 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
114 (ip_list_hash_size - 1);
117 static struct recent_entry *
118 recent_entry_lookup(const struct recent_table *table,
119 const union nf_inet_addr *addrp, u_int16_t family,
122 struct recent_entry *e;
125 if (family == NFPROTO_IPV4)
126 h = recent_entry_hash4(addrp);
128 h = recent_entry_hash6(addrp);
130 list_for_each_entry(e, &table->iphash[h], list)
131 if (e->family == family &&
132 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
133 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
138 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
141 list_del(&e->lru_list);
146 static struct recent_entry *
147 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
148 u_int16_t family, u_int8_t ttl)
150 struct recent_entry *e;
152 if (t->entries >= ip_list_tot) {
153 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
154 recent_entry_remove(t, e);
156 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
160 memcpy(&e->addr, addr, sizeof(e->addr));
162 e->stamps[0] = jiffies;
166 if (family == NFPROTO_IPV4)
167 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
169 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
170 list_add_tail(&e->lru_list, &t->lru_list);
175 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
177 e->index %= ip_pkt_list_tot;
178 e->stamps[e->index++] = jiffies;
179 if (e->index > e->nstamps)
180 e->nstamps = e->index;
181 list_move_tail(&e->lru_list, &t->lru_list);
184 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
187 struct recent_table *t;
189 list_for_each_entry(t, &recent_net->tables, list)
190 if (!strcmp(t->name, name))
195 static void recent_table_flush(struct recent_table *t)
197 struct recent_entry *e, *next;
200 for (i = 0; i < ip_list_hash_size; i++)
201 list_for_each_entry_safe(e, next, &t->iphash[i], list)
202 recent_entry_remove(t, e);
206 recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
208 struct net *net = dev_net(par->in ? par->in : par->out);
209 struct recent_net *recent_net = recent_pernet(net);
210 const struct xt_recent_mtinfo *info = par->matchinfo;
211 struct recent_table *t;
212 struct recent_entry *e;
213 union nf_inet_addr addr = {};
215 bool ret = info->invert;
217 if (par->match->family == NFPROTO_IPV4) {
218 const struct iphdr *iph = ip_hdr(skb);
220 if (info->side == XT_RECENT_DEST)
221 addr.ip = iph->daddr;
223 addr.ip = iph->saddr;
227 const struct ipv6hdr *iph = ipv6_hdr(skb);
229 if (info->side == XT_RECENT_DEST)
230 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
232 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
234 ttl = iph->hop_limit;
237 /* use TTL as seen before forwarding */
238 if (par->out != NULL && skb->sk == NULL)
241 spin_lock_bh(&recent_lock);
242 t = recent_table_lookup(recent_net, info->name);
243 e = recent_entry_lookup(t, &addr, par->match->family,
244 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
246 if (!(info->check_set & XT_RECENT_SET))
248 e = recent_entry_init(t, &addr, par->match->family, ttl);
250 *par->hotdrop = true;
255 if (info->check_set & XT_RECENT_SET)
257 else if (info->check_set & XT_RECENT_REMOVE) {
258 recent_entry_remove(t, e);
260 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
261 unsigned long time = jiffies - info->seconds * HZ;
262 unsigned int i, hits = 0;
264 for (i = 0; i < e->nstamps; i++) {
265 if (info->seconds && time_after(time, e->stamps[i]))
267 if (info->hit_count && ++hits >= info->hit_count) {
274 if (info->check_set & XT_RECENT_SET ||
275 (info->check_set & XT_RECENT_UPDATE && ret)) {
276 recent_entry_update(t, e);
280 spin_unlock_bh(&recent_lock);
284 static bool recent_mt_check(const struct xt_mtchk_param *par)
286 struct recent_net *recent_net = recent_pernet(par->net);
287 const struct xt_recent_mtinfo *info = par->matchinfo;
288 struct recent_table *t;
289 #ifdef CONFIG_PROC_FS
290 struct proc_dir_entry *pde;
295 if (unlikely(!hash_rnd_inited)) {
296 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
297 hash_rnd_inited = true;
299 if (hweight8(info->check_set &
300 (XT_RECENT_SET | XT_RECENT_REMOVE |
301 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
303 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
304 (info->seconds || info->hit_count))
306 if (info->hit_count > ip_pkt_list_tot) {
307 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
308 "packets to be remembered (%u)\n",
309 info->hit_count, ip_pkt_list_tot);
312 if (info->name[0] == '\0' ||
313 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
316 mutex_lock(&recent_mutex);
317 t = recent_table_lookup(recent_net, info->name);
324 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
329 strcpy(t->name, info->name);
330 INIT_LIST_HEAD(&t->lru_list);
331 for (i = 0; i < ip_list_hash_size; i++)
332 INIT_LIST_HEAD(&t->iphash[i]);
333 #ifdef CONFIG_PROC_FS
334 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
340 pde->uid = ip_list_uid;
341 pde->gid = ip_list_gid;
343 spin_lock_bh(&recent_lock);
344 list_add_tail(&t->list, &recent_net->tables);
345 spin_unlock_bh(&recent_lock);
348 mutex_unlock(&recent_mutex);
352 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
354 struct recent_net *recent_net = recent_pernet(par->net);
355 const struct xt_recent_mtinfo *info = par->matchinfo;
356 struct recent_table *t;
358 mutex_lock(&recent_mutex);
359 t = recent_table_lookup(recent_net, info->name);
360 if (--t->refcnt == 0) {
361 spin_lock_bh(&recent_lock);
363 spin_unlock_bh(&recent_lock);
364 #ifdef CONFIG_PROC_FS
365 remove_proc_entry(t->name, recent_net->xt_recent);
367 recent_table_flush(t);
370 mutex_unlock(&recent_mutex);
373 #ifdef CONFIG_PROC_FS
374 struct recent_iter_state {
375 const struct recent_table *table;
379 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
380 __acquires(recent_lock)
382 struct recent_iter_state *st = seq->private;
383 const struct recent_table *t = st->table;
384 struct recent_entry *e;
387 spin_lock_bh(&recent_lock);
389 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
390 list_for_each_entry(e, &t->iphash[st->bucket], list)
396 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
398 struct recent_iter_state *st = seq->private;
399 const struct recent_table *t = st->table;
400 const struct recent_entry *e = v;
401 const struct list_head *head = e->list.next;
403 while (head == &t->iphash[st->bucket]) {
404 if (++st->bucket >= ip_list_hash_size)
406 head = t->iphash[st->bucket].next;
409 return list_entry(head, struct recent_entry, list);
412 static void recent_seq_stop(struct seq_file *s, void *v)
413 __releases(recent_lock)
415 spin_unlock_bh(&recent_lock);
418 static int recent_seq_show(struct seq_file *seq, void *v)
420 const struct recent_entry *e = v;
423 i = (e->index - 1) % ip_pkt_list_tot;
424 if (e->family == NFPROTO_IPV4)
425 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
426 &e->addr.ip, e->ttl, e->stamps[i], e->index);
428 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
429 &e->addr.in6, e->ttl, e->stamps[i], e->index);
430 for (i = 0; i < e->nstamps; i++)
431 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
432 seq_printf(seq, "\n");
436 static const struct seq_operations recent_seq_ops = {
437 .start = recent_seq_start,
438 .next = recent_seq_next,
439 .stop = recent_seq_stop,
440 .show = recent_seq_show,
443 static int recent_seq_open(struct inode *inode, struct file *file)
445 struct proc_dir_entry *pde = PDE(inode);
446 struct recent_iter_state *st;
448 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
452 st->table = pde->data;
457 recent_mt_proc_write(struct file *file, const char __user *input,
458 size_t size, loff_t *loff)
460 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
461 struct recent_table *t = pde->data;
462 struct recent_entry *e;
463 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
465 union nf_inet_addr addr = {};
471 if (size > sizeof(buf))
473 if (copy_from_user(buf, input, size) != 0)
476 /* Strict protocol! */
480 case '/': /* flush table */
481 spin_lock_bh(&recent_lock);
482 recent_table_flush(t);
483 spin_unlock_bh(&recent_lock);
485 case '-': /* remove address */
488 case '+': /* add address */
492 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
498 if (strnchr(c, size, ':') != NULL) {
499 family = NFPROTO_IPV6;
500 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
502 family = NFPROTO_IPV4;
503 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
507 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
512 spin_lock_bh(&recent_lock);
513 e = recent_entry_lookup(t, &addr, family, 0);
516 recent_entry_init(t, &addr, family, 0);
519 recent_entry_update(t, e);
521 recent_entry_remove(t, e);
523 spin_unlock_bh(&recent_lock);
524 /* Note we removed one above */
529 static const struct file_operations recent_mt_fops = {
530 .open = recent_seq_open,
532 .write = recent_mt_proc_write,
533 .release = seq_release_private,
534 .owner = THIS_MODULE,
537 static int __net_init recent_proc_net_init(struct net *net)
539 struct recent_net *recent_net = recent_pernet(net);
541 recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
542 if (!recent_net->xt_recent)
547 static void __net_exit recent_proc_net_exit(struct net *net)
549 proc_net_remove(net, "xt_recent");
552 static inline int recent_proc_net_init(struct net *net)
557 static inline void recent_proc_net_exit(struct net *net)
560 #endif /* CONFIG_PROC_FS */
562 static int __net_init recent_net_init(struct net *net)
564 struct recent_net *recent_net = recent_pernet(net);
566 INIT_LIST_HEAD(&recent_net->tables);
567 return recent_proc_net_init(net);
570 static void __net_exit recent_net_exit(struct net *net)
572 struct recent_net *recent_net = recent_pernet(net);
574 BUG_ON(!list_empty(&recent_net->tables));
575 recent_proc_net_exit(net);
578 static struct pernet_operations recent_net_ops = {
579 .init = recent_net_init,
580 .exit = recent_net_exit,
581 .id = &recent_net_id,
582 .size = sizeof(struct recent_net),
585 static struct xt_match recent_mt_reg[] __read_mostly = {
589 .family = NFPROTO_IPV4,
591 .matchsize = sizeof(struct xt_recent_mtinfo),
592 .checkentry = recent_mt_check,
593 .destroy = recent_mt_destroy,
599 .family = NFPROTO_IPV6,
601 .matchsize = sizeof(struct xt_recent_mtinfo),
602 .checkentry = recent_mt_check,
603 .destroy = recent_mt_destroy,
608 static int __init recent_mt_init(void)
612 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
614 ip_list_hash_size = 1 << fls(ip_list_tot);
616 err = register_pernet_subsys(&recent_net_ops);
619 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
621 unregister_pernet_subsys(&recent_net_ops);
625 static void __exit recent_mt_exit(void)
627 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
628 unregister_pernet_subsys(&recent_net_ops);
631 module_init(recent_mt_init);
632 module_exit(recent_mt_exit);