2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This is a replacement of the old ipt_recent module, which carried the
9 * following copyright notice:
11 * Author: Stephen Frost <sfrost@snowman.net>
12 * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
14 #include <linux/init.h>
15 #include <linux/moduleparam.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/list.h>
21 #include <linux/random.h>
22 #include <linux/jhash.h>
23 #include <linux/bitops.h>
24 #include <linux/skbuff.h>
25 #include <linux/inet.h>
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netfilter_ipv4/ipt_recent.h>
30 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
31 MODULE_DESCRIPTION("IP tables recently seen matching module");
32 MODULE_LICENSE("GPL");
34 static unsigned int ip_list_tot
= 100;
35 static unsigned int ip_pkt_list_tot
= 20;
36 static unsigned int ip_list_hash_size
= 0;
37 static unsigned int ip_list_perms
= 0644;
38 static unsigned int ip_list_uid
= 0;
39 static unsigned int ip_list_gid
= 0;
40 module_param(ip_list_tot
, uint
, 0400);
41 module_param(ip_pkt_list_tot
, uint
, 0400);
42 module_param(ip_list_hash_size
, uint
, 0400);
43 module_param(ip_list_perms
, uint
, 0400);
44 module_param(ip_list_uid
, uint
, 0400);
45 module_param(ip_list_gid
, uint
, 0400);
46 MODULE_PARM_DESC(ip_list_tot
, "number of IPs to remember per list");
47 MODULE_PARM_DESC(ip_pkt_list_tot
, "number of packets per IP to remember (max. 255)");
48 MODULE_PARM_DESC(ip_list_hash_size
, "size of hash table used to look up IPs");
49 MODULE_PARM_DESC(ip_list_perms
, "permissions on /proc/net/ipt_recent/* files");
50 MODULE_PARM_DESC(ip_list_uid
,"owner of /proc/net/ipt_recent/* files");
51 MODULE_PARM_DESC(ip_list_gid
,"owning group of /proc/net/ipt_recent/* files");
55 struct list_head list
;
56 struct list_head lru_list
;
61 unsigned long stamps
[0];
65 struct list_head list
;
66 char name
[IPT_RECENT_NAME_LEN
];
68 struct proc_dir_entry
*proc
;
72 struct list_head lru_list
;
73 struct list_head iphash
[0];
76 static LIST_HEAD(tables
);
77 static DEFINE_SPINLOCK(recent_lock
);
78 static DEFINE_MUTEX(recent_mutex
);
81 static struct proc_dir_entry
*proc_dir
;
82 static struct file_operations recent_fops
;
85 static u_int32_t hash_rnd
;
86 static int hash_rnd_initted
;
88 static unsigned int recent_entry_hash(u_int32_t addr
)
90 if (!hash_rnd_initted
) {
91 get_random_bytes(&hash_rnd
, 4);
94 return jhash_1word(addr
, hash_rnd
) & (ip_list_hash_size
- 1);
97 static struct recent_entry
*
98 recent_entry_lookup(const struct recent_table
*table
, u_int32_t addr
, u_int8_t ttl
)
100 struct recent_entry
*e
;
103 h
= recent_entry_hash(addr
);
104 list_for_each_entry(e
, &table
->iphash
[h
], list
)
105 if (e
->addr
== addr
&& (ttl
== e
->ttl
|| !ttl
|| !e
->ttl
))
110 static void recent_entry_remove(struct recent_table
*t
, struct recent_entry
*e
)
113 list_del(&e
->lru_list
);
118 static struct recent_entry
*
119 recent_entry_init(struct recent_table
*t
, u_int32_t addr
, u_int8_t ttl
)
121 struct recent_entry
*e
;
123 if (t
->entries
>= ip_list_tot
) {
124 e
= list_entry(t
->lru_list
.next
, struct recent_entry
, lru_list
);
125 recent_entry_remove(t
, e
);
127 e
= kmalloc(sizeof(*e
) + sizeof(e
->stamps
[0]) * ip_pkt_list_tot
,
133 e
->stamps
[0] = jiffies
;
136 list_add_tail(&e
->list
, &t
->iphash
[recent_entry_hash(addr
)]);
137 list_add_tail(&e
->lru_list
, &t
->lru_list
);
142 static void recent_entry_update(struct recent_table
*t
, struct recent_entry
*e
)
144 e
->stamps
[e
->index
++] = jiffies
;
145 if (e
->index
> e
->nstamps
)
146 e
->nstamps
= e
->index
;
147 e
->index
%= ip_pkt_list_tot
;
148 list_move_tail(&e
->lru_list
, &t
->lru_list
);
151 static struct recent_table
*recent_table_lookup(const char *name
)
153 struct recent_table
*t
;
155 list_for_each_entry(t
, &tables
, list
)
156 if (!strcmp(t
->name
, name
))
161 static void recent_table_flush(struct recent_table
*t
)
163 struct recent_entry
*e
, *next
;
166 for (i
= 0; i
< ip_list_hash_size
; i
++) {
167 list_for_each_entry_safe(e
, next
, &t
->iphash
[i
], list
)
168 recent_entry_remove(t
, e
);
173 ipt_recent_match(const struct sk_buff
*skb
,
174 const struct net_device
*in
, const struct net_device
*out
,
175 const struct xt_match
*match
, const void *matchinfo
,
176 int offset
, unsigned int protoff
, int *hotdrop
)
178 const struct ipt_recent_info
*info
= matchinfo
;
179 struct recent_table
*t
;
180 struct recent_entry
*e
;
183 int ret
= info
->invert
;
185 if (info
->side
== IPT_RECENT_DEST
)
186 addr
= skb
->nh
.iph
->daddr
;
188 addr
= skb
->nh
.iph
->saddr
;
190 ttl
= skb
->nh
.iph
->ttl
;
191 /* use TTL as seen before forwarding */
195 spin_lock_bh(&recent_lock
);
196 t
= recent_table_lookup(info
->name
);
197 e
= recent_entry_lookup(t
, addr
,
198 info
->check_set
& IPT_RECENT_TTL
? ttl
: 0);
200 if (!(info
->check_set
& IPT_RECENT_SET
))
202 e
= recent_entry_init(t
, addr
, ttl
);
209 if (info
->check_set
& IPT_RECENT_SET
)
211 else if (info
->check_set
& IPT_RECENT_REMOVE
) {
212 recent_entry_remove(t
, e
);
214 } else if (info
->check_set
& (IPT_RECENT_CHECK
| IPT_RECENT_UPDATE
)) {
215 unsigned long t
= jiffies
- info
->seconds
* HZ
;
216 unsigned int i
, hits
= 0;
218 for (i
= 0; i
< e
->nstamps
; i
++) {
219 if (info
->seconds
&& time_after(t
, e
->stamps
[i
]))
221 if (++hits
>= info
->hit_count
) {
228 if (info
->check_set
& IPT_RECENT_SET
||
229 (info
->check_set
& IPT_RECENT_UPDATE
&& ret
)) {
230 recent_entry_update(t
, e
);
234 spin_unlock_bh(&recent_lock
);
239 ipt_recent_checkentry(const char *tablename
, const void *ip
,
240 const struct xt_match
*match
, void *matchinfo
,
241 unsigned int matchsize
, unsigned int hook_mask
)
243 const struct ipt_recent_info
*info
= matchinfo
;
244 struct recent_table
*t
;
248 if (hweight8(info
->check_set
&
249 (IPT_RECENT_SET
| IPT_RECENT_REMOVE
|
250 IPT_RECENT_CHECK
| IPT_RECENT_UPDATE
)) != 1)
252 if ((info
->check_set
& (IPT_RECENT_SET
| IPT_RECENT_REMOVE
)) &&
253 (info
->seconds
|| info
->hit_count
))
255 if (info
->name
[0] == '\0' ||
256 strnlen(info
->name
, IPT_RECENT_NAME_LEN
) == IPT_RECENT_NAME_LEN
)
259 mutex_lock(&recent_mutex
);
260 t
= recent_table_lookup(info
->name
);
267 t
= kzalloc(sizeof(*t
) + sizeof(t
->iphash
[0]) * ip_list_hash_size
,
272 strcpy(t
->name
, info
->name
);
273 INIT_LIST_HEAD(&t
->lru_list
);
274 for (i
= 0; i
< ip_list_hash_size
; i
++)
275 INIT_LIST_HEAD(&t
->iphash
[i
]);
276 #ifdef CONFIG_PROC_FS
277 t
->proc
= create_proc_entry(t
->name
, ip_list_perms
, proc_dir
);
278 if (t
->proc
== NULL
) {
282 t
->proc
->proc_fops
= &recent_fops
;
283 t
->proc
->uid
= ip_list_uid
;
284 t
->proc
->gid
= ip_list_gid
;
287 spin_lock_bh(&recent_lock
);
288 list_add_tail(&t
->list
, &tables
);
289 spin_unlock_bh(&recent_lock
);
292 mutex_unlock(&recent_mutex
);
297 ipt_recent_destroy(const struct xt_match
*match
, void *matchinfo
,
298 unsigned int matchsize
)
300 const struct ipt_recent_info
*info
= matchinfo
;
301 struct recent_table
*t
;
303 mutex_lock(&recent_mutex
);
304 t
= recent_table_lookup(info
->name
);
305 if (--t
->refcnt
== 0) {
306 spin_lock_bh(&recent_lock
);
308 spin_unlock_bh(&recent_lock
);
309 recent_table_flush(t
);
310 #ifdef CONFIG_PROC_FS
311 remove_proc_entry(t
->name
, proc_dir
);
315 mutex_unlock(&recent_mutex
);
318 #ifdef CONFIG_PROC_FS
319 struct recent_iter_state
{
320 struct recent_table
*table
;
324 static void *recent_seq_start(struct seq_file
*seq
, loff_t
*pos
)
326 struct recent_iter_state
*st
= seq
->private;
327 struct recent_table
*t
= st
->table
;
328 struct recent_entry
*e
;
331 spin_lock_bh(&recent_lock
);
333 for (st
->bucket
= 0; st
->bucket
< ip_list_hash_size
; st
->bucket
++) {
334 list_for_each_entry(e
, &t
->iphash
[st
->bucket
], list
) {
342 static void *recent_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
344 struct recent_iter_state
*st
= seq
->private;
345 struct recent_table
*t
= st
->table
;
346 struct recent_entry
*e
= v
;
347 struct list_head
*head
= e
->list
.next
;
349 while (head
== &t
->iphash
[st
->bucket
]) {
350 if (++st
->bucket
>= ip_list_hash_size
)
352 head
= t
->iphash
[st
->bucket
].next
;
355 return list_entry(head
, struct recent_entry
, list
);
358 static void recent_seq_stop(struct seq_file
*s
, void *v
)
360 spin_unlock_bh(&recent_lock
);
363 static int recent_seq_show(struct seq_file
*seq
, void *v
)
365 struct recent_entry
*e
= v
;
368 i
= (e
->index
- 1) % ip_pkt_list_tot
;
369 seq_printf(seq
, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
370 NIPQUAD(e
->addr
), e
->ttl
, e
->stamps
[i
], e
->index
);
371 for (i
= 0; i
< e
->nstamps
; i
++)
372 seq_printf(seq
, "%s %lu", i
? "," : "", e
->stamps
[i
]);
373 seq_printf(seq
, "\n");
377 static struct seq_operations recent_seq_ops
= {
378 .start
= recent_seq_start
,
379 .next
= recent_seq_next
,
380 .stop
= recent_seq_stop
,
381 .show
= recent_seq_show
,
384 static int recent_seq_open(struct inode
*inode
, struct file
*file
)
386 struct proc_dir_entry
*pde
= PDE(inode
);
387 struct seq_file
*seq
;
388 struct recent_iter_state
*st
;
391 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
394 ret
= seq_open(file
, &recent_seq_ops
);
397 st
->table
= pde
->data
;
398 seq
= file
->private_data
;
403 static ssize_t
recent_proc_write(struct file
*file
, const char __user
*input
,
404 size_t size
, loff_t
*loff
)
406 struct proc_dir_entry
*pde
= PDE(file
->f_dentry
->d_inode
);
407 struct recent_table
*t
= pde
->data
;
408 struct recent_entry
*e
;
409 char buf
[sizeof("+255.255.255.255")], *c
= buf
;
413 if (size
> sizeof(buf
))
415 if (copy_from_user(buf
, input
, size
))
420 if (size
- (c
- buf
) < 5)
422 if (!strncmp(c
, "clear", 5)) {
424 spin_lock_bh(&recent_lock
);
425 recent_table_flush(t
);
426 spin_unlock_bh(&recent_lock
);
443 spin_lock_bh(&recent_lock
);
444 e
= recent_entry_lookup(t
, addr
, 0);
447 recent_entry_init(t
, addr
, 0);
450 recent_entry_update(t
, e
);
452 recent_entry_remove(t
, e
);
454 spin_unlock_bh(&recent_lock
);
458 static struct file_operations recent_fops
= {
459 .open
= recent_seq_open
,
461 .write
= recent_proc_write
,
462 .release
= seq_release_private
,
463 .owner
= THIS_MODULE
,
465 #endif /* CONFIG_PROC_FS */
467 static struct ipt_match recent_match
= {
469 .match
= ipt_recent_match
,
470 .matchsize
= sizeof(struct ipt_recent_info
),
471 .checkentry
= ipt_recent_checkentry
,
472 .destroy
= ipt_recent_destroy
,
476 static int __init
ipt_recent_init(void)
480 if (!ip_list_tot
|| !ip_pkt_list_tot
|| ip_pkt_list_tot
> 255)
482 ip_list_hash_size
= 1 << fls(ip_list_tot
);
484 err
= ipt_register_match(&recent_match
);
485 #ifdef CONFIG_PROC_FS
488 proc_dir
= proc_mkdir("ipt_recent", proc_net
);
489 if (proc_dir
== NULL
) {
490 ipt_unregister_match(&recent_match
);
497 static void __exit
ipt_recent_exit(void)
499 BUG_ON(!list_empty(&tables
));
500 ipt_unregister_match(&recent_match
);
501 #ifdef CONFIG_PROC_FS
502 remove_proc_entry("ipt_recent", proc_net
);
506 module_init(ipt_recent_init
);
507 module_exit(ipt_recent_exit
);