1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
6 * This is a replacement of the old ipt_recent module, which carried the
7 * following copyright notice:
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
15 #include <linux/ipv6.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <linux/list.h>
23 #include <linux/random.h>
24 #include <linux/jhash.h>
25 #include <linux/bitops.h>
26 #include <linux/skbuff.h>
27 #include <linux/inet.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.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>
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 __read_mostly = 100;
44 static unsigned int ip_list_hash_size __read_mostly;
45 static unsigned int ip_list_perms __read_mostly = 0644;
46 static unsigned int ip_list_uid __read_mostly;
47 static unsigned int ip_list_gid __read_mostly;
48 module_param(ip_list_tot, uint, 0400);
49 module_param(ip_list_hash_size, uint, 0400);
50 module_param(ip_list_perms, uint, 0400);
51 module_param(ip_list_uid, uint, 0644);
52 module_param(ip_list_gid, uint, 0644);
53 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
54 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
55 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
56 MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files");
57 MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files");
59 /* retained for backwards compatibility */
60 static unsigned int ip_pkt_list_tot __read_mostly;
61 module_param(ip_pkt_list_tot, uint, 0400);
62 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
64 #define XT_RECENT_MAX_NSTAMPS 256
67 struct list_head list;
68 struct list_head lru_list;
69 union nf_inet_addr addr;
74 unsigned long stamps[0];
78 struct list_head list;
79 char name[XT_RECENT_NAME_LEN];
80 union nf_inet_addr mask;
84 struct list_head lru_list;
85 struct list_head iphash[0];
89 struct list_head tables;
91 struct proc_dir_entry *xt_recent;
95 static unsigned int recent_net_id __read_mostly;
97 static inline struct recent_net *recent_pernet(struct net *net)
99 return net_generic(net, recent_net_id);
102 static DEFINE_SPINLOCK(recent_lock);
103 static DEFINE_MUTEX(recent_mutex);
105 #ifdef CONFIG_PROC_FS
106 static const struct proc_ops recent_mt_proc_ops;
109 static u_int32_t hash_rnd __read_mostly;
111 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
113 return jhash_1word((__force u32)addr->ip, hash_rnd) &
114 (ip_list_hash_size - 1);
117 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
119 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
120 (ip_list_hash_size - 1);
123 static struct recent_entry *
124 recent_entry_lookup(const struct recent_table *table,
125 const union nf_inet_addr *addrp, u_int16_t family,
128 struct recent_entry *e;
131 if (family == NFPROTO_IPV4)
132 h = recent_entry_hash4(addrp);
134 h = recent_entry_hash6(addrp);
136 list_for_each_entry(e, &table->iphash[h], list)
137 if (e->family == family &&
138 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
139 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
144 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
147 list_del(&e->lru_list);
153 * Drop entries with timestamps older then 'time'.
155 static void recent_entry_reap(struct recent_table *t, unsigned long time)
157 struct recent_entry *e;
160 * The head of the LRU list is always the oldest entry.
162 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
165 * The last time stamp is the most recent.
167 if (time_after(time, e->stamps[e->index-1]))
168 recent_entry_remove(t, e);
171 static struct recent_entry *
172 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
173 u_int16_t family, u_int8_t ttl)
175 struct recent_entry *e;
176 unsigned int nstamps_max = t->nstamps_max_mask;
178 if (t->entries >= ip_list_tot) {
179 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
180 recent_entry_remove(t, e);
184 e = kmalloc(struct_size(e, stamps, nstamps_max), GFP_ATOMIC);
187 memcpy(&e->addr, addr, sizeof(e->addr));
189 e->stamps[0] = jiffies;
193 if (family == NFPROTO_IPV4)
194 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
196 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
197 list_add_tail(&e->lru_list, &t->lru_list);
202 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
204 e->index &= t->nstamps_max_mask;
205 e->stamps[e->index++] = jiffies;
206 if (e->index > e->nstamps)
207 e->nstamps = e->index;
208 list_move_tail(&e->lru_list, &t->lru_list);
211 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
214 struct recent_table *t;
216 list_for_each_entry(t, &recent_net->tables, list)
217 if (!strcmp(t->name, name))
222 static void recent_table_flush(struct recent_table *t)
224 struct recent_entry *e, *next;
227 for (i = 0; i < ip_list_hash_size; i++)
228 list_for_each_entry_safe(e, next, &t->iphash[i], list)
229 recent_entry_remove(t, e);
233 recent_mt(const struct sk_buff *skb, struct xt_action_param *par)
235 struct net *net = xt_net(par);
236 struct recent_net *recent_net = recent_pernet(net);
237 const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
238 struct recent_table *t;
239 struct recent_entry *e;
240 union nf_inet_addr addr = {}, addr_mask;
242 bool ret = info->invert;
244 if (xt_family(par) == NFPROTO_IPV4) {
245 const struct iphdr *iph = ip_hdr(skb);
247 if (info->side == XT_RECENT_DEST)
248 addr.ip = iph->daddr;
250 addr.ip = iph->saddr;
254 const struct ipv6hdr *iph = ipv6_hdr(skb);
256 if (info->side == XT_RECENT_DEST)
257 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
259 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
261 ttl = iph->hop_limit;
264 /* use TTL as seen before forwarding */
265 if (xt_out(par) != NULL &&
266 (!skb->sk || !net_eq(net, sock_net(skb->sk))))
269 spin_lock_bh(&recent_lock);
270 t = recent_table_lookup(recent_net, info->name);
272 nf_inet_addr_mask(&addr, &addr_mask, &t->mask);
274 e = recent_entry_lookup(t, &addr_mask, xt_family(par),
275 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
277 if (!(info->check_set & XT_RECENT_SET))
279 e = recent_entry_init(t, &addr_mask, xt_family(par), ttl);
286 if (info->check_set & XT_RECENT_SET)
288 else if (info->check_set & XT_RECENT_REMOVE) {
289 recent_entry_remove(t, e);
291 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
292 unsigned long time = jiffies - info->seconds * HZ;
293 unsigned int i, hits = 0;
295 for (i = 0; i < e->nstamps; i++) {
296 if (info->seconds && time_after(time, e->stamps[i]))
298 if (!info->hit_count || ++hits >= info->hit_count) {
304 /* info->seconds must be non-zero */
305 if (info->check_set & XT_RECENT_REAP)
306 recent_entry_reap(t, time);
309 if (info->check_set & XT_RECENT_SET ||
310 (info->check_set & XT_RECENT_UPDATE && ret)) {
311 recent_entry_update(t, e);
315 spin_unlock_bh(&recent_lock);
319 static void recent_table_free(void *addr)
324 static int recent_mt_check(const struct xt_mtchk_param *par,
325 const struct xt_recent_mtinfo_v1 *info)
327 struct recent_net *recent_net = recent_pernet(par->net);
328 struct recent_table *t;
329 #ifdef CONFIG_PROC_FS
330 struct proc_dir_entry *pde;
334 unsigned int nstamp_mask;
338 net_get_random_once(&hash_rnd, sizeof(hash_rnd));
340 if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
341 pr_info_ratelimited("Unsupported userspace flags (%08x)\n",
345 if (hweight8(info->check_set &
346 (XT_RECENT_SET | XT_RECENT_REMOVE |
347 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
349 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
350 (info->seconds || info->hit_count ||
351 (info->check_set & XT_RECENT_MODIFIERS)))
353 if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
355 if (info->hit_count >= XT_RECENT_MAX_NSTAMPS) {
356 pr_info_ratelimited("hitcount (%u) is larger than allowed maximum (%u)\n",
357 info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
360 ret = xt_check_proc_name(info->name, sizeof(info->name));
364 if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot)
365 nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1;
366 else if (info->hit_count)
367 nstamp_mask = roundup_pow_of_two(info->hit_count) - 1;
369 nstamp_mask = 32 - 1;
371 mutex_lock(&recent_mutex);
372 t = recent_table_lookup(recent_net, info->name);
374 if (nstamp_mask > t->nstamps_max_mask) {
375 spin_lock_bh(&recent_lock);
376 recent_table_flush(t);
377 t->nstamps_max_mask = nstamp_mask;
378 spin_unlock_bh(&recent_lock);
386 t = kvzalloc(struct_size(t, iphash, ip_list_hash_size), GFP_KERNEL);
392 t->nstamps_max_mask = nstamp_mask;
394 memcpy(&t->mask, &info->mask, sizeof(t->mask));
395 strcpy(t->name, info->name);
396 INIT_LIST_HEAD(&t->lru_list);
397 for (i = 0; i < ip_list_hash_size; i++)
398 INIT_LIST_HEAD(&t->iphash[i]);
399 #ifdef CONFIG_PROC_FS
400 uid = make_kuid(&init_user_ns, ip_list_uid);
401 gid = make_kgid(&init_user_ns, ip_list_gid);
402 if (!uid_valid(uid) || !gid_valid(gid)) {
403 recent_table_free(t);
407 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
408 &recent_mt_proc_ops, t);
410 recent_table_free(t);
414 proc_set_user(pde, uid, gid);
416 spin_lock_bh(&recent_lock);
417 list_add_tail(&t->list, &recent_net->tables);
418 spin_unlock_bh(&recent_lock);
421 mutex_unlock(&recent_mutex);
425 static int recent_mt_check_v0(const struct xt_mtchk_param *par)
427 const struct xt_recent_mtinfo_v0 *info_v0 = par->matchinfo;
428 struct xt_recent_mtinfo_v1 info_v1;
430 /* Copy revision 0 structure to revision 1 */
431 memcpy(&info_v1, info_v0, sizeof(struct xt_recent_mtinfo));
432 /* Set default mask to ensure backward compatible behaviour */
433 memset(info_v1.mask.all, 0xFF, sizeof(info_v1.mask.all));
435 return recent_mt_check(par, &info_v1);
438 static int recent_mt_check_v1(const struct xt_mtchk_param *par)
440 return recent_mt_check(par, par->matchinfo);
443 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
445 struct recent_net *recent_net = recent_pernet(par->net);
446 const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
447 struct recent_table *t;
449 mutex_lock(&recent_mutex);
450 t = recent_table_lookup(recent_net, info->name);
451 if (--t->refcnt == 0) {
452 spin_lock_bh(&recent_lock);
454 spin_unlock_bh(&recent_lock);
455 #ifdef CONFIG_PROC_FS
456 if (recent_net->xt_recent != NULL)
457 remove_proc_entry(t->name, recent_net->xt_recent);
459 recent_table_flush(t);
460 recent_table_free(t);
462 mutex_unlock(&recent_mutex);
465 #ifdef CONFIG_PROC_FS
466 struct recent_iter_state {
467 const struct recent_table *table;
471 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
472 __acquires(recent_lock)
474 struct recent_iter_state *st = seq->private;
475 const struct recent_table *t = st->table;
476 struct recent_entry *e;
479 spin_lock_bh(&recent_lock);
481 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
482 list_for_each_entry(e, &t->iphash[st->bucket], list)
488 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
490 struct recent_iter_state *st = seq->private;
491 const struct recent_table *t = st->table;
492 const struct recent_entry *e = v;
493 const struct list_head *head = e->list.next;
495 while (head == &t->iphash[st->bucket]) {
496 if (++st->bucket >= ip_list_hash_size)
498 head = t->iphash[st->bucket].next;
501 return list_entry(head, struct recent_entry, list);
504 static void recent_seq_stop(struct seq_file *s, void *v)
505 __releases(recent_lock)
507 spin_unlock_bh(&recent_lock);
510 static int recent_seq_show(struct seq_file *seq, void *v)
512 const struct recent_entry *e = v;
513 struct recent_iter_state *st = seq->private;
514 const struct recent_table *t = st->table;
517 i = (e->index - 1) & t->nstamps_max_mask;
519 if (e->family == NFPROTO_IPV4)
520 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
521 &e->addr.ip, e->ttl, e->stamps[i], e->index);
523 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
524 &e->addr.in6, e->ttl, e->stamps[i], e->index);
525 for (i = 0; i < e->nstamps; i++)
526 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
531 static const struct seq_operations recent_seq_ops = {
532 .start = recent_seq_start,
533 .next = recent_seq_next,
534 .stop = recent_seq_stop,
535 .show = recent_seq_show,
538 static int recent_seq_open(struct inode *inode, struct file *file)
540 struct recent_iter_state *st;
542 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
546 st->table = PDE_DATA(inode);
551 recent_mt_proc_write(struct file *file, const char __user *input,
552 size_t size, loff_t *loff)
554 struct recent_table *t = PDE_DATA(file_inode(file));
555 struct recent_entry *e;
556 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
558 union nf_inet_addr addr = {};
564 if (size > sizeof(buf))
566 if (copy_from_user(buf, input, size) != 0)
569 /* Strict protocol! */
573 case '/': /* flush table */
574 spin_lock_bh(&recent_lock);
575 recent_table_flush(t);
576 spin_unlock_bh(&recent_lock);
578 case '-': /* remove address */
581 case '+': /* add address */
585 pr_info_ratelimited("Need \"+ip\", \"-ip\" or \"/\"\n");
591 if (strnchr(c, size, ':') != NULL) {
592 family = NFPROTO_IPV6;
593 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
595 family = NFPROTO_IPV4;
596 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
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 proc_ops recent_mt_proc_ops = {
620 .proc_open = recent_seq_open,
621 .proc_read = seq_read,
622 .proc_write = recent_mt_proc_write,
623 .proc_release = seq_release_private,
624 .proc_lseek = seq_lseek,
627 static int __net_init recent_proc_net_init(struct net *net)
629 struct recent_net *recent_net = recent_pernet(net);
631 recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
632 if (!recent_net->xt_recent)
637 static void __net_exit recent_proc_net_exit(struct net *net)
639 struct recent_net *recent_net = recent_pernet(net);
640 struct recent_table *t;
642 /* recent_net_exit() is called before recent_mt_destroy(). Make sure
643 * that the parent xt_recent proc entry is is empty before trying to
646 spin_lock_bh(&recent_lock);
647 list_for_each_entry(t, &recent_net->tables, list)
648 remove_proc_entry(t->name, recent_net->xt_recent);
650 recent_net->xt_recent = NULL;
651 spin_unlock_bh(&recent_lock);
653 remove_proc_entry("xt_recent", net->proc_net);
656 static inline int recent_proc_net_init(struct net *net)
661 static inline void recent_proc_net_exit(struct net *net)
664 #endif /* CONFIG_PROC_FS */
666 static int __net_init recent_net_init(struct net *net)
668 struct recent_net *recent_net = recent_pernet(net);
670 INIT_LIST_HEAD(&recent_net->tables);
671 return recent_proc_net_init(net);
674 static void __net_exit recent_net_exit(struct net *net)
676 recent_proc_net_exit(net);
679 static struct pernet_operations recent_net_ops = {
680 .init = recent_net_init,
681 .exit = recent_net_exit,
682 .id = &recent_net_id,
683 .size = sizeof(struct recent_net),
686 static struct xt_match recent_mt_reg[] __read_mostly = {
690 .family = NFPROTO_IPV4,
692 .matchsize = sizeof(struct xt_recent_mtinfo),
693 .checkentry = recent_mt_check_v0,
694 .destroy = recent_mt_destroy,
700 .family = NFPROTO_IPV6,
702 .matchsize = sizeof(struct xt_recent_mtinfo),
703 .checkentry = recent_mt_check_v0,
704 .destroy = recent_mt_destroy,
710 .family = NFPROTO_IPV4,
712 .matchsize = sizeof(struct xt_recent_mtinfo_v1),
713 .checkentry = recent_mt_check_v1,
714 .destroy = recent_mt_destroy,
720 .family = NFPROTO_IPV6,
722 .matchsize = sizeof(struct xt_recent_mtinfo_v1),
723 .checkentry = recent_mt_check_v1,
724 .destroy = recent_mt_destroy,
729 static int __init recent_mt_init(void)
733 BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS);
735 if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
737 ip_list_hash_size = 1 << fls(ip_list_tot);
739 err = register_pernet_subsys(&recent_net_ops);
742 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
744 unregister_pernet_subsys(&recent_net_ops);
748 static void __exit recent_mt_exit(void)
750 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
751 unregister_pernet_subsys(&recent_net_ops);
754 module_init(recent_mt_init);
755 module_exit(recent_mt_exit);