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@computergmbh.de>");
38 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
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
;
86 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
87 struct proc_dir_entry
*ipt_recent
;
92 static int recent_net_id
;
93 static inline struct recent_net
*recent_pernet(struct net
*net
)
95 return net_generic(net
, recent_net_id
);
98 static DEFINE_SPINLOCK(recent_lock
);
99 static DEFINE_MUTEX(recent_mutex
);
101 #ifdef CONFIG_PROC_FS
102 static const struct file_operations recent_old_fops
, recent_mt_fops
;
105 static u_int32_t hash_rnd __read_mostly
;
106 static bool hash_rnd_inited __read_mostly
;
108 static inline unsigned int recent_entry_hash4(const union nf_inet_addr
*addr
)
110 return jhash_1word((__force u32
)addr
->ip
, hash_rnd
) &
111 (ip_list_hash_size
- 1);
114 static inline unsigned int recent_entry_hash6(const union nf_inet_addr
*addr
)
116 return jhash2((u32
*)addr
->ip6
, ARRAY_SIZE(addr
->ip6
), hash_rnd
) &
117 (ip_list_hash_size
- 1);
120 static struct recent_entry
*
121 recent_entry_lookup(const struct recent_table
*table
,
122 const union nf_inet_addr
*addrp
, u_int16_t family
,
125 struct recent_entry
*e
;
128 if (family
== NFPROTO_IPV4
)
129 h
= recent_entry_hash4(addrp
);
131 h
= recent_entry_hash6(addrp
);
133 list_for_each_entry(e
, &table
->iphash
[h
], list
)
134 if (e
->family
== family
&&
135 memcmp(&e
->addr
, addrp
, sizeof(e
->addr
)) == 0 &&
136 (ttl
== e
->ttl
|| ttl
== 0 || e
->ttl
== 0))
141 static void recent_entry_remove(struct recent_table
*t
, struct recent_entry
*e
)
144 list_del(&e
->lru_list
);
149 static struct recent_entry
*
150 recent_entry_init(struct recent_table
*t
, const union nf_inet_addr
*addr
,
151 u_int16_t family
, u_int8_t ttl
)
153 struct recent_entry
*e
;
155 if (t
->entries
>= ip_list_tot
) {
156 e
= list_entry(t
->lru_list
.next
, struct recent_entry
, lru_list
);
157 recent_entry_remove(t
, e
);
159 e
= kmalloc(sizeof(*e
) + sizeof(e
->stamps
[0]) * ip_pkt_list_tot
,
163 memcpy(&e
->addr
, addr
, sizeof(e
->addr
));
165 e
->stamps
[0] = jiffies
;
169 if (family
== NFPROTO_IPV4
)
170 list_add_tail(&e
->list
, &t
->iphash
[recent_entry_hash4(addr
)]);
172 list_add_tail(&e
->list
, &t
->iphash
[recent_entry_hash6(addr
)]);
173 list_add_tail(&e
->lru_list
, &t
->lru_list
);
178 static void recent_entry_update(struct recent_table
*t
, struct recent_entry
*e
)
180 e
->stamps
[e
->index
++] = jiffies
;
181 if (e
->index
> e
->nstamps
)
182 e
->nstamps
= e
->index
;
183 e
->index
%= ip_pkt_list_tot
;
184 list_move_tail(&e
->lru_list
, &t
->lru_list
);
187 static struct recent_table
*recent_table_lookup(struct recent_net
*recent_net
,
190 struct recent_table
*t
;
192 list_for_each_entry(t
, &recent_net
->tables
, list
)
193 if (!strcmp(t
->name
, name
))
198 static void recent_table_flush(struct recent_table
*t
)
200 struct recent_entry
*e
, *next
;
203 for (i
= 0; i
< ip_list_hash_size
; i
++)
204 list_for_each_entry_safe(e
, next
, &t
->iphash
[i
], list
)
205 recent_entry_remove(t
, e
);
209 recent_mt(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
211 struct net
*net
= dev_net(par
->in
? par
->in
: par
->out
);
212 struct recent_net
*recent_net
= recent_pernet(net
);
213 const struct xt_recent_mtinfo
*info
= par
->matchinfo
;
214 struct recent_table
*t
;
215 struct recent_entry
*e
;
216 union nf_inet_addr addr
= {};
218 bool ret
= info
->invert
;
220 if (par
->match
->family
== NFPROTO_IPV4
) {
221 const struct iphdr
*iph
= ip_hdr(skb
);
223 if (info
->side
== XT_RECENT_DEST
)
224 addr
.ip
= iph
->daddr
;
226 addr
.ip
= iph
->saddr
;
230 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
232 if (info
->side
== XT_RECENT_DEST
)
233 memcpy(&addr
.in6
, &iph
->daddr
, sizeof(addr
.in6
));
235 memcpy(&addr
.in6
, &iph
->saddr
, sizeof(addr
.in6
));
237 ttl
= iph
->hop_limit
;
240 /* use TTL as seen before forwarding */
241 if (par
->out
!= NULL
&& skb
->sk
== NULL
)
244 spin_lock_bh(&recent_lock
);
245 t
= recent_table_lookup(recent_net
, info
->name
);
246 e
= recent_entry_lookup(t
, &addr
, par
->match
->family
,
247 (info
->check_set
& XT_RECENT_TTL
) ? ttl
: 0);
249 if (!(info
->check_set
& XT_RECENT_SET
))
251 e
= recent_entry_init(t
, &addr
, par
->match
->family
, ttl
);
253 *par
->hotdrop
= true;
258 if (info
->check_set
& XT_RECENT_SET
)
260 else if (info
->check_set
& XT_RECENT_REMOVE
) {
261 recent_entry_remove(t
, e
);
263 } else if (info
->check_set
& (XT_RECENT_CHECK
| XT_RECENT_UPDATE
)) {
264 unsigned long time
= jiffies
- info
->seconds
* HZ
;
265 unsigned int i
, hits
= 0;
267 for (i
= 0; i
< e
->nstamps
; i
++) {
268 if (info
->seconds
&& time_after(time
, e
->stamps
[i
]))
270 if (++hits
>= info
->hit_count
) {
277 if (info
->check_set
& XT_RECENT_SET
||
278 (info
->check_set
& XT_RECENT_UPDATE
&& ret
)) {
279 recent_entry_update(t
, e
);
283 spin_unlock_bh(&recent_lock
);
287 static bool recent_mt_check(const struct xt_mtchk_param
*par
)
289 struct recent_net
*recent_net
= recent_pernet(par
->net
);
290 const struct xt_recent_mtinfo
*info
= par
->matchinfo
;
291 struct recent_table
*t
;
292 #ifdef CONFIG_PROC_FS
293 struct proc_dir_entry
*pde
;
298 if (unlikely(!hash_rnd_inited
)) {
299 get_random_bytes(&hash_rnd
, sizeof(hash_rnd
));
300 hash_rnd_inited
= true;
302 if (hweight8(info
->check_set
&
303 (XT_RECENT_SET
| XT_RECENT_REMOVE
|
304 XT_RECENT_CHECK
| XT_RECENT_UPDATE
)) != 1)
306 if ((info
->check_set
& (XT_RECENT_SET
| XT_RECENT_REMOVE
)) &&
307 (info
->seconds
|| info
->hit_count
))
309 if (info
->hit_count
> ip_pkt_list_tot
) {
310 pr_info(KBUILD_MODNAME
": hitcount (%u) is larger than "
311 "packets to be remembered (%u)\n",
312 info
->hit_count
, ip_pkt_list_tot
);
315 if (info
->name
[0] == '\0' ||
316 strnlen(info
->name
, XT_RECENT_NAME_LEN
) == XT_RECENT_NAME_LEN
)
319 mutex_lock(&recent_mutex
);
320 t
= recent_table_lookup(recent_net
, info
->name
);
327 t
= kzalloc(sizeof(*t
) + sizeof(t
->iphash
[0]) * ip_list_hash_size
,
332 strcpy(t
->name
, info
->name
);
333 INIT_LIST_HEAD(&t
->lru_list
);
334 for (i
= 0; i
< ip_list_hash_size
; i
++)
335 INIT_LIST_HEAD(&t
->iphash
[i
]);
336 #ifdef CONFIG_PROC_FS
337 pde
= proc_create_data(t
->name
, ip_list_perms
, recent_net
->xt_recent
,
343 pde
->uid
= ip_list_uid
;
344 pde
->gid
= ip_list_gid
;
345 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
346 pde
= proc_create_data(t
->name
, ip_list_perms
, recent_net
->ipt_recent
,
347 &recent_old_fops
, t
);
349 remove_proc_entry(t
->name
, recent_net
->xt_recent
);
353 pde
->uid
= ip_list_uid
;
354 pde
->gid
= ip_list_gid
;
357 spin_lock_bh(&recent_lock
);
358 list_add_tail(&t
->list
, &recent_net
->tables
);
359 spin_unlock_bh(&recent_lock
);
362 mutex_unlock(&recent_mutex
);
366 static void recent_mt_destroy(const struct xt_mtdtor_param
*par
)
368 struct recent_net
*recent_net
= recent_pernet(par
->net
);
369 const struct xt_recent_mtinfo
*info
= par
->matchinfo
;
370 struct recent_table
*t
;
372 mutex_lock(&recent_mutex
);
373 t
= recent_table_lookup(recent_net
, info
->name
);
374 if (--t
->refcnt
== 0) {
375 spin_lock_bh(&recent_lock
);
377 spin_unlock_bh(&recent_lock
);
378 #ifdef CONFIG_PROC_FS
379 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
380 remove_proc_entry(t
->name
, recent_net
->ipt_recent
);
382 remove_proc_entry(t
->name
, recent_net
->xt_recent
);
384 recent_table_flush(t
);
387 mutex_unlock(&recent_mutex
);
390 #ifdef CONFIG_PROC_FS
391 struct recent_iter_state
{
392 const struct recent_table
*table
;
396 static void *recent_seq_start(struct seq_file
*seq
, loff_t
*pos
)
397 __acquires(recent_lock
)
399 struct recent_iter_state
*st
= seq
->private;
400 const struct recent_table
*t
= st
->table
;
401 struct recent_entry
*e
;
404 spin_lock_bh(&recent_lock
);
406 for (st
->bucket
= 0; st
->bucket
< ip_list_hash_size
; st
->bucket
++)
407 list_for_each_entry(e
, &t
->iphash
[st
->bucket
], list
)
413 static void *recent_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
415 struct recent_iter_state
*st
= seq
->private;
416 const struct recent_table
*t
= st
->table
;
417 const struct recent_entry
*e
= v
;
418 const struct list_head
*head
= e
->list
.next
;
420 while (head
== &t
->iphash
[st
->bucket
]) {
421 if (++st
->bucket
>= ip_list_hash_size
)
423 head
= t
->iphash
[st
->bucket
].next
;
426 return list_entry(head
, struct recent_entry
, list
);
429 static void recent_seq_stop(struct seq_file
*s
, void *v
)
430 __releases(recent_lock
)
432 spin_unlock_bh(&recent_lock
);
435 static int recent_seq_show(struct seq_file
*seq
, void *v
)
437 const struct recent_entry
*e
= v
;
440 i
= (e
->index
- 1) % ip_pkt_list_tot
;
441 if (e
->family
== NFPROTO_IPV4
)
442 seq_printf(seq
, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
443 &e
->addr
.ip
, e
->ttl
, e
->stamps
[i
], e
->index
);
445 seq_printf(seq
, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
446 &e
->addr
.in6
, e
->ttl
, e
->stamps
[i
], e
->index
);
447 for (i
= 0; i
< e
->nstamps
; i
++)
448 seq_printf(seq
, "%s %lu", i
? "," : "", e
->stamps
[i
]);
449 seq_printf(seq
, "\n");
453 static const struct seq_operations recent_seq_ops
= {
454 .start
= recent_seq_start
,
455 .next
= recent_seq_next
,
456 .stop
= recent_seq_stop
,
457 .show
= recent_seq_show
,
460 static int recent_seq_open(struct inode
*inode
, struct file
*file
)
462 struct proc_dir_entry
*pde
= PDE(inode
);
463 struct recent_iter_state
*st
;
465 st
= __seq_open_private(file
, &recent_seq_ops
, sizeof(*st
));
469 st
->table
= pde
->data
;
473 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
474 static int recent_old_seq_open(struct inode
*inode
, struct file
*filp
)
476 static bool warned_of_old
;
478 if (unlikely(!warned_of_old
)) {
479 printk(KERN_INFO KBUILD_MODNAME
": Use of /proc/net/ipt_recent"
480 " is deprecated; use /proc/net/xt_recent.\n");
481 warned_of_old
= true;
483 return recent_seq_open(inode
, filp
);
486 static ssize_t
recent_old_proc_write(struct file
*file
,
487 const char __user
*input
,
488 size_t size
, loff_t
*loff
)
490 const struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
491 struct recent_table
*t
= pde
->data
;
492 struct recent_entry
*e
;
493 char buf
[sizeof("+255.255.255.255")], *c
= buf
;
494 union nf_inet_addr addr
= {};
497 if (size
> sizeof(buf
))
499 if (copy_from_user(buf
, input
, size
))
504 if (size
- (c
- buf
) < 5)
506 if (!strncmp(c
, "clear", 5)) {
508 spin_lock_bh(&recent_lock
);
509 recent_table_flush(t
);
510 spin_unlock_bh(&recent_lock
);
525 addr
.ip
= in_aton(c
);
527 spin_lock_bh(&recent_lock
);
528 e
= recent_entry_lookup(t
, &addr
, NFPROTO_IPV4
, 0);
531 recent_entry_init(t
, &addr
, NFPROTO_IPV4
, 0);
534 recent_entry_update(t
, e
);
536 recent_entry_remove(t
, e
);
538 spin_unlock_bh(&recent_lock
);
542 static const struct file_operations recent_old_fops
= {
543 .open
= recent_old_seq_open
,
545 .write
= recent_old_proc_write
,
546 .release
= seq_release_private
,
547 .owner
= THIS_MODULE
,
552 recent_mt_proc_write(struct file
*file
, const char __user
*input
,
553 size_t size
, loff_t
*loff
)
555 const struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
556 struct recent_table
*t
= pde
->data
;
557 struct recent_entry
*e
;
558 char buf
[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
560 union nf_inet_addr addr
= {};
566 if (size
> sizeof(buf
))
568 if (copy_from_user(buf
, input
, size
) != 0)
571 /* Strict protocol! */
575 case '/': /* flush table */
576 spin_lock_bh(&recent_lock
);
577 recent_table_flush(t
);
578 spin_unlock_bh(&recent_lock
);
580 case '-': /* remove address */
583 case '+': /* add address */
587 printk(KERN_INFO KBUILD_MODNAME
": Need +ip, -ip or /\n");
593 if (strnchr(c
, size
, ':') != NULL
) {
594 family
= NFPROTO_IPV6
;
595 succ
= in6_pton(c
, size
, (void *)&addr
, '\n', NULL
);
597 family
= NFPROTO_IPV4
;
598 succ
= in4_pton(c
, size
, (void *)&addr
, '\n', NULL
);
602 printk(KERN_INFO KBUILD_MODNAME
": illegal address written "
607 spin_lock_bh(&recent_lock
);
608 e
= recent_entry_lookup(t
, &addr
, family
, 0);
611 recent_entry_init(t
, &addr
, family
, 0);
614 recent_entry_update(t
, e
);
616 recent_entry_remove(t
, e
);
618 spin_unlock_bh(&recent_lock
);
619 /* Note we removed one above */
624 static const struct file_operations recent_mt_fops
= {
625 .open
= recent_seq_open
,
627 .write
= recent_mt_proc_write
,
628 .release
= seq_release_private
,
629 .owner
= THIS_MODULE
,
632 static int __net_init
recent_proc_net_init(struct net
*net
)
634 struct recent_net
*recent_net
= recent_pernet(net
);
636 recent_net
->xt_recent
= proc_mkdir("xt_recent", net
->proc_net
);
637 if (!recent_net
->xt_recent
)
639 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
640 recent_net
->ipt_recent
= proc_mkdir("ipt_recent", net
->proc_net
);
641 if (!recent_net
->ipt_recent
) {
642 proc_net_remove(net
, "xt_recent");
649 static void __net_exit
recent_proc_net_exit(struct net
*net
)
651 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
652 proc_net_remove(net
, "ipt_recent");
654 proc_net_remove(net
, "xt_recent");
657 static inline int recent_proc_net_init(struct net
*net
)
662 static inline void recent_proc_net_exit(struct net
*net
)
665 #endif /* CONFIG_PROC_FS */
667 static int __net_init
recent_net_init(struct net
*net
)
669 struct recent_net
*recent_net
= recent_pernet(net
);
671 INIT_LIST_HEAD(&recent_net
->tables
);
672 return recent_proc_net_init(net
);
675 static void __net_exit
recent_net_exit(struct net
*net
)
677 struct recent_net
*recent_net
= recent_pernet(net
);
679 BUG_ON(!list_empty(&recent_net
->tables
));
680 recent_proc_net_exit(net
);
683 static struct pernet_operations recent_net_ops
= {
684 .init
= recent_net_init
,
685 .exit
= recent_net_exit
,
686 .id
= &recent_net_id
,
687 .size
= sizeof(struct recent_net
),
690 static struct xt_match recent_mt_reg
[] __read_mostly
= {
694 .family
= NFPROTO_IPV4
,
696 .matchsize
= sizeof(struct xt_recent_mtinfo
),
697 .checkentry
= recent_mt_check
,
698 .destroy
= recent_mt_destroy
,
704 .family
= NFPROTO_IPV6
,
706 .matchsize
= sizeof(struct xt_recent_mtinfo
),
707 .checkentry
= recent_mt_check
,
708 .destroy
= recent_mt_destroy
,
713 static int __init
recent_mt_init(void)
717 if (!ip_list_tot
|| !ip_pkt_list_tot
|| ip_pkt_list_tot
> 255)
719 ip_list_hash_size
= 1 << fls(ip_list_tot
);
721 err
= register_pernet_subsys(&recent_net_ops
);
724 err
= xt_register_matches(recent_mt_reg
, ARRAY_SIZE(recent_mt_reg
));
726 unregister_pernet_subsys(&recent_net_ops
);
730 static void __exit
recent_mt_exit(void)
732 xt_unregister_matches(recent_mt_reg
, ARRAY_SIZE(recent_mt_reg
));
733 unregister_pernet_subsys(&recent_net_ops
);
736 module_init(recent_mt_init
);
737 module_exit(recent_mt_exit
);