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);
147 * Drop entries with timestamps older then 'time'.
149 static void recent_entry_reap(struct recent_table *t, unsigned long time)
151 struct recent_entry *e;
154 * The head of the LRU list is always the oldest entry.
156 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
159 * The last time stamp is the most recent.
161 if (time_after(time, e->stamps[e->index-1]))
162 recent_entry_remove(t, e);
165 static struct recent_entry *
166 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
167 u_int16_t family, u_int8_t ttl)
169 struct recent_entry *e;
171 if (t->entries >= ip_list_tot) {
172 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
173 recent_entry_remove(t, e);
175 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
179 memcpy(&e->addr, addr, sizeof(e->addr));
181 e->stamps[0] = jiffies;
185 if (family == NFPROTO_IPV4)
186 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
188 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
189 list_add_tail(&e->lru_list, &t->lru_list);
194 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
196 e->index %= ip_pkt_list_tot;
197 e->stamps[e->index++] = jiffies;
198 if (e->index > e->nstamps)
199 e->nstamps = e->index;
200 list_move_tail(&e->lru_list, &t->lru_list);
203 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
206 struct recent_table *t;
208 list_for_each_entry(t, &recent_net->tables, list)
209 if (!strcmp(t->name, name))
214 static void recent_table_flush(struct recent_table *t)
216 struct recent_entry *e, *next;
219 for (i = 0; i < ip_list_hash_size; i++)
220 list_for_each_entry_safe(e, next, &t->iphash[i], list)
221 recent_entry_remove(t, e);
225 recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
227 struct net *net = dev_net(par->in ? par->in : par->out);
228 struct recent_net *recent_net = recent_pernet(net);
229 const struct xt_recent_mtinfo *info = par->matchinfo;
230 struct recent_table *t;
231 struct recent_entry *e;
232 union nf_inet_addr addr = {};
234 bool ret = info->invert;
236 if (par->match->family == NFPROTO_IPV4) {
237 const struct iphdr *iph = ip_hdr(skb);
239 if (info->side == XT_RECENT_DEST)
240 addr.ip = iph->daddr;
242 addr.ip = iph->saddr;
246 const struct ipv6hdr *iph = ipv6_hdr(skb);
248 if (info->side == XT_RECENT_DEST)
249 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
251 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
253 ttl = iph->hop_limit;
256 /* use TTL as seen before forwarding */
257 if (par->out != NULL && skb->sk == NULL)
260 spin_lock_bh(&recent_lock);
261 t = recent_table_lookup(recent_net, info->name);
262 e = recent_entry_lookup(t, &addr, par->match->family,
263 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
265 if (!(info->check_set & XT_RECENT_SET))
267 e = recent_entry_init(t, &addr, par->match->family, ttl);
269 *par->hotdrop = true;
274 if (info->check_set & XT_RECENT_SET)
276 else if (info->check_set & XT_RECENT_REMOVE) {
277 recent_entry_remove(t, e);
279 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
280 unsigned long time = jiffies - info->seconds * HZ;
281 unsigned int i, hits = 0;
283 for (i = 0; i < e->nstamps; i++) {
284 if (info->seconds && time_after(time, e->stamps[i]))
286 if (info->hit_count && ++hits >= info->hit_count) {
292 /* info->seconds must be non-zero */
293 if (info->check_set & XT_RECENT_REAP)
294 recent_entry_reap(t, time);
297 if (info->check_set & XT_RECENT_SET ||
298 (info->check_set & XT_RECENT_UPDATE && ret)) {
299 recent_entry_update(t, e);
303 spin_unlock_bh(&recent_lock);
307 static bool recent_mt_check(const struct xt_mtchk_param *par)
309 struct recent_net *recent_net = recent_pernet(par->net);
310 const struct xt_recent_mtinfo *info = par->matchinfo;
311 struct recent_table *t;
312 #ifdef CONFIG_PROC_FS
313 struct proc_dir_entry *pde;
318 if (unlikely(!hash_rnd_inited)) {
319 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
320 hash_rnd_inited = true;
322 if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
323 pr_info(KBUILD_MODNAME ": Unsupported user space flags "
324 "(%08x)\n", info->check_set);
327 if (hweight8(info->check_set &
328 (XT_RECENT_SET | XT_RECENT_REMOVE |
329 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
331 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
332 (info->seconds || info->hit_count ||
333 (info->check_set & XT_RECENT_MODIFIERS)))
335 if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
337 if (info->hit_count > ip_pkt_list_tot) {
338 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
339 "packets to be remembered (%u)\n",
340 info->hit_count, ip_pkt_list_tot);
343 if (info->name[0] == '\0' ||
344 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
347 mutex_lock(&recent_mutex);
348 t = recent_table_lookup(recent_net, info->name);
355 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
360 strcpy(t->name, info->name);
361 INIT_LIST_HEAD(&t->lru_list);
362 for (i = 0; i < ip_list_hash_size; i++)
363 INIT_LIST_HEAD(&t->iphash[i]);
364 #ifdef CONFIG_PROC_FS
365 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
371 pde->uid = ip_list_uid;
372 pde->gid = ip_list_gid;
374 spin_lock_bh(&recent_lock);
375 list_add_tail(&t->list, &recent_net->tables);
376 spin_unlock_bh(&recent_lock);
379 mutex_unlock(&recent_mutex);
383 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
385 struct recent_net *recent_net = recent_pernet(par->net);
386 const struct xt_recent_mtinfo *info = par->matchinfo;
387 struct recent_table *t;
389 mutex_lock(&recent_mutex);
390 t = recent_table_lookup(recent_net, info->name);
391 if (--t->refcnt == 0) {
392 spin_lock_bh(&recent_lock);
394 spin_unlock_bh(&recent_lock);
395 #ifdef CONFIG_PROC_FS
396 remove_proc_entry(t->name, recent_net->xt_recent);
398 recent_table_flush(t);
401 mutex_unlock(&recent_mutex);
404 #ifdef CONFIG_PROC_FS
405 struct recent_iter_state {
406 const struct recent_table *table;
410 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
411 __acquires(recent_lock)
413 struct recent_iter_state *st = seq->private;
414 const struct recent_table *t = st->table;
415 struct recent_entry *e;
418 spin_lock_bh(&recent_lock);
420 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
421 list_for_each_entry(e, &t->iphash[st->bucket], list)
427 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
429 struct recent_iter_state *st = seq->private;
430 const struct recent_table *t = st->table;
431 const struct recent_entry *e = v;
432 const struct list_head *head = e->list.next;
434 while (head == &t->iphash[st->bucket]) {
435 if (++st->bucket >= ip_list_hash_size)
437 head = t->iphash[st->bucket].next;
440 return list_entry(head, struct recent_entry, list);
443 static void recent_seq_stop(struct seq_file *s, void *v)
444 __releases(recent_lock)
446 spin_unlock_bh(&recent_lock);
449 static int recent_seq_show(struct seq_file *seq, void *v)
451 const struct recent_entry *e = v;
454 i = (e->index - 1) % ip_pkt_list_tot;
455 if (e->family == NFPROTO_IPV4)
456 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
457 &e->addr.ip, e->ttl, e->stamps[i], e->index);
459 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
460 &e->addr.in6, e->ttl, e->stamps[i], e->index);
461 for (i = 0; i < e->nstamps; i++)
462 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
463 seq_printf(seq, "\n");
467 static const struct seq_operations recent_seq_ops = {
468 .start = recent_seq_start,
469 .next = recent_seq_next,
470 .stop = recent_seq_stop,
471 .show = recent_seq_show,
474 static int recent_seq_open(struct inode *inode, struct file *file)
476 struct proc_dir_entry *pde = PDE(inode);
477 struct recent_iter_state *st;
479 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
483 st->table = pde->data;
488 recent_mt_proc_write(struct file *file, 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("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
496 union nf_inet_addr addr = {};
502 if (size > sizeof(buf))
504 if (copy_from_user(buf, input, size) != 0)
507 /* Strict protocol! */
511 case '/': /* flush table */
512 spin_lock_bh(&recent_lock);
513 recent_table_flush(t);
514 spin_unlock_bh(&recent_lock);
516 case '-': /* remove address */
519 case '+': /* add address */
523 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
529 if (strnchr(c, size, ':') != NULL) {
530 family = NFPROTO_IPV6;
531 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
533 family = NFPROTO_IPV4;
534 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
538 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
543 spin_lock_bh(&recent_lock);
544 e = recent_entry_lookup(t, &addr, family, 0);
547 recent_entry_init(t, &addr, family, 0);
550 recent_entry_update(t, e);
552 recent_entry_remove(t, e);
554 spin_unlock_bh(&recent_lock);
555 /* Note we removed one above */
560 static const struct file_operations recent_mt_fops = {
561 .open = recent_seq_open,
563 .write = recent_mt_proc_write,
564 .release = seq_release_private,
565 .owner = THIS_MODULE,
568 static int __net_init recent_proc_net_init(struct net *net)
570 struct recent_net *recent_net = recent_pernet(net);
572 recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
573 if (!recent_net->xt_recent)
578 static void __net_exit recent_proc_net_exit(struct net *net)
580 proc_net_remove(net, "xt_recent");
583 static inline int recent_proc_net_init(struct net *net)
588 static inline void recent_proc_net_exit(struct net *net)
591 #endif /* CONFIG_PROC_FS */
593 static int __net_init recent_net_init(struct net *net)
595 struct recent_net *recent_net = recent_pernet(net);
597 INIT_LIST_HEAD(&recent_net->tables);
598 return recent_proc_net_init(net);
601 static void __net_exit recent_net_exit(struct net *net)
603 struct recent_net *recent_net = recent_pernet(net);
605 BUG_ON(!list_empty(&recent_net->tables));
606 recent_proc_net_exit(net);
609 static struct pernet_operations recent_net_ops = {
610 .init = recent_net_init,
611 .exit = recent_net_exit,
612 .id = &recent_net_id,
613 .size = sizeof(struct recent_net),
616 static struct xt_match recent_mt_reg[] __read_mostly = {
620 .family = NFPROTO_IPV4,
622 .matchsize = sizeof(struct xt_recent_mtinfo),
623 .checkentry = recent_mt_check,
624 .destroy = recent_mt_destroy,
630 .family = NFPROTO_IPV6,
632 .matchsize = sizeof(struct xt_recent_mtinfo),
633 .checkentry = recent_mt_check,
634 .destroy = recent_mt_destroy,
639 static int __init recent_mt_init(void)
643 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
645 ip_list_hash_size = 1 << fls(ip_list_tot);
647 err = register_pernet_subsys(&recent_net_ops);
650 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
652 unregister_pernet_subsys(&recent_net_ops);
656 static void __exit recent_mt_exit(void)
658 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
659 unregister_pernet_subsys(&recent_net_ops);
662 module_init(recent_mt_init);
663 module_exit(recent_mt_exit);