Commit | Line | Data |
---|---|---|
404bdbfd PM |
1 | /* |
2 | * Copyright (c) 2006 Patrick McHardy <kaber@trash.net> | |
3 | * | |
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. | |
7 | * | |
8 | * This is a replacement of the old ipt_recent module, which carried the | |
9 | * following copyright notice: | |
10 | * | |
11 | * Author: Stephen Frost <sfrost@snowman.net> | |
12 | * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org | |
13 | */ | |
14 | #include <linux/init.h> | |
15 | #include <linux/moduleparam.h> | |
1da177e4 | 16 | #include <linux/proc_fs.h> |
404bdbfd PM |
17 | #include <linux/seq_file.h> |
18 | #include <linux/string.h> | |
1da177e4 | 19 | #include <linux/ctype.h> |
404bdbfd PM |
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> | |
1da177e4 LT |
26 | |
27 | #include <linux/netfilter_ipv4/ip_tables.h> | |
28 | #include <linux/netfilter_ipv4/ipt_recent.h> | |
29 | ||
404bdbfd PM |
30 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
31 | MODULE_DESCRIPTION("IP tables recently seen matching module"); | |
32 | MODULE_LICENSE("GPL"); | |
1da177e4 | 33 | |
e7be6994 PM |
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; | |
e7be6994 PM |
38 | module_param(ip_list_tot, uint, 0400); |
39 | module_param(ip_pkt_list_tot, uint, 0400); | |
40 | module_param(ip_list_hash_size, uint, 0400); | |
41 | module_param(ip_list_perms, uint, 0400); | |
404bdbfd PM |
42 | MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list"); |
43 | MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)"); | |
44 | MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs"); | |
45 | MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files"); | |
46 | ||
47 | ||
48 | struct recent_entry { | |
49 | struct list_head list; | |
50 | struct list_head lru_list; | |
51 | u_int32_t addr; | |
52 | u_int8_t ttl; | |
53 | u_int8_t index; | |
54 | u_int16_t nstamps; | |
55 | unsigned long stamps[0]; | |
1da177e4 LT |
56 | }; |
57 | ||
404bdbfd PM |
58 | struct recent_table { |
59 | struct list_head list; | |
60 | char name[IPT_RECENT_NAME_LEN]; | |
1da177e4 | 61 | #ifdef CONFIG_PROC_FS |
404bdbfd PM |
62 | struct proc_dir_entry *proc; |
63 | #endif | |
64 | unsigned int refcnt; | |
65 | unsigned int entries; | |
66 | struct list_head lru_list; | |
67 | struct list_head iphash[0]; | |
1da177e4 LT |
68 | }; |
69 | ||
404bdbfd | 70 | static LIST_HEAD(tables); |
1da177e4 | 71 | static DEFINE_SPINLOCK(recent_lock); |
a0e889bb | 72 | static DEFINE_MUTEX(recent_mutex); |
1da177e4 LT |
73 | |
74 | #ifdef CONFIG_PROC_FS | |
404bdbfd PM |
75 | static struct proc_dir_entry *proc_dir; |
76 | static struct file_operations recent_fops; | |
1da177e4 LT |
77 | #endif |
78 | ||
404bdbfd PM |
79 | static u_int32_t hash_rnd; |
80 | static int hash_rnd_initted; | |
1da177e4 | 81 | |
404bdbfd | 82 | static unsigned int recent_entry_hash(u_int32_t addr) |
1da177e4 | 83 | { |
404bdbfd PM |
84 | if (!hash_rnd_initted) { |
85 | get_random_bytes(&hash_rnd, 4); | |
86 | hash_rnd_initted = 1; | |
1da177e4 | 87 | } |
404bdbfd | 88 | return jhash_1word(addr, hash_rnd) & (ip_list_hash_size - 1); |
1da177e4 LT |
89 | } |
90 | ||
404bdbfd PM |
91 | static struct recent_entry * |
92 | recent_entry_lookup(const struct recent_table *table, u_int32_t addr, u_int8_t ttl) | |
1da177e4 | 93 | { |
404bdbfd PM |
94 | struct recent_entry *e; |
95 | unsigned int h; | |
96 | ||
97 | h = recent_entry_hash(addr); | |
98 | list_for_each_entry(e, &table->iphash[h], list) | |
99 | if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl)) | |
100 | return e; | |
101 | return NULL; | |
102 | } | |
1da177e4 | 103 | |
404bdbfd PM |
104 | static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) |
105 | { | |
106 | list_del(&e->list); | |
107 | list_del(&e->lru_list); | |
108 | kfree(e); | |
109 | t->entries--; | |
110 | } | |
1da177e4 | 111 | |
404bdbfd PM |
112 | static struct recent_entry * |
113 | recent_entry_init(struct recent_table *t, u_int32_t addr, u_int8_t ttl) | |
114 | { | |
115 | struct recent_entry *e; | |
1da177e4 | 116 | |
404bdbfd PM |
117 | if (t->entries >= ip_list_tot) { |
118 | e = list_entry(t->lru_list.next, struct recent_entry, lru_list); | |
119 | recent_entry_remove(t, e); | |
1da177e4 | 120 | } |
404bdbfd PM |
121 | e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot, |
122 | GFP_ATOMIC); | |
123 | if (e == NULL) | |
124 | return NULL; | |
125 | e->addr = addr; | |
126 | e->ttl = ttl; | |
127 | e->stamps[0] = jiffies; | |
128 | e->nstamps = 1; | |
129 | e->index = 1; | |
130 | list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]); | |
131 | list_add_tail(&e->lru_list, &t->lru_list); | |
132 | t->entries++; | |
133 | return e; | |
134 | } | |
1da177e4 | 135 | |
404bdbfd PM |
136 | static void recent_entry_update(struct recent_table *t, struct recent_entry *e) |
137 | { | |
138 | e->stamps[e->index++] = jiffies; | |
139 | if (e->index > e->nstamps) | |
140 | e->nstamps = e->index; | |
141 | e->index %= ip_pkt_list_tot; | |
142 | list_move_tail(&e->lru_list, &t->lru_list); | |
143 | } | |
1da177e4 | 144 | |
404bdbfd PM |
145 | static struct recent_table *recent_table_lookup(const char *name) |
146 | { | |
147 | struct recent_table *t; | |
1da177e4 | 148 | |
404bdbfd PM |
149 | list_for_each_entry(t, &tables, list) |
150 | if (!strcmp(t->name, name)) | |
151 | return t; | |
152 | return NULL; | |
153 | } | |
1da177e4 | 154 | |
404bdbfd PM |
155 | static void recent_table_flush(struct recent_table *t) |
156 | { | |
157 | struct recent_entry *e, *next; | |
158 | unsigned int i; | |
1da177e4 | 159 | |
404bdbfd PM |
160 | for (i = 0; i < ip_list_hash_size; i++) { |
161 | list_for_each_entry_safe(e, next, &t->iphash[i], list) | |
162 | recent_entry_remove(t, e); | |
1da177e4 | 163 | } |
1da177e4 LT |
164 | } |
165 | ||
1da177e4 | 166 | static int |
404bdbfd PM |
167 | ipt_recent_match(const struct sk_buff *skb, |
168 | const struct net_device *in, const struct net_device *out, | |
169 | const struct xt_match *match, const void *matchinfo, | |
170 | int offset, unsigned int protoff, int *hotdrop) | |
1da177e4 | 171 | { |
1da177e4 | 172 | const struct ipt_recent_info *info = matchinfo; |
404bdbfd PM |
173 | struct recent_table *t; |
174 | struct recent_entry *e; | |
175 | u_int32_t addr; | |
176 | u_int8_t ttl; | |
177 | int ret = info->invert; | |
1da177e4 | 178 | |
404bdbfd PM |
179 | if (info->side == IPT_RECENT_DEST) |
180 | addr = skb->nh.iph->daddr; | |
181 | else | |
182 | addr = skb->nh.iph->saddr; | |
1da177e4 | 183 | |
404bdbfd PM |
184 | ttl = skb->nh.iph->ttl; |
185 | /* use TTL as seen before forwarding */ | |
186 | if (out && !skb->sk) | |
187 | ttl++; | |
1da177e4 | 188 | |
1da177e4 | 189 | spin_lock_bh(&recent_lock); |
404bdbfd PM |
190 | t = recent_table_lookup(info->name); |
191 | e = recent_entry_lookup(t, addr, | |
192 | info->check_set & IPT_RECENT_TTL ? ttl : 0); | |
193 | if (e == NULL) { | |
194 | if (!(info->check_set & IPT_RECENT_SET)) | |
195 | goto out; | |
196 | e = recent_entry_init(t, addr, ttl); | |
197 | if (e == NULL) | |
198 | *hotdrop = 1; | |
199 | ret ^= 1; | |
200 | goto out; | |
1da177e4 LT |
201 | } |
202 | ||
404bdbfd PM |
203 | if (info->check_set & IPT_RECENT_SET) |
204 | ret ^= 1; | |
205 | else if (info->check_set & IPT_RECENT_REMOVE) { | |
206 | recent_entry_remove(t, e); | |
207 | ret ^= 1; | |
208 | } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) { | |
209 | unsigned long t = jiffies - info->seconds * HZ; | |
210 | unsigned int i, hits = 0; | |
211 | ||
212 | for (i = 0; i < e->nstamps; i++) { | |
213 | if (info->seconds && time_after(t, e->stamps[i])) | |
214 | continue; | |
215 | if (++hits >= info->hit_count) { | |
216 | ret ^= 1; | |
217 | break; | |
1da177e4 | 218 | } |
1da177e4 | 219 | } |
1da177e4 LT |
220 | } |
221 | ||
404bdbfd PM |
222 | if (info->check_set & IPT_RECENT_SET || |
223 | (info->check_set & IPT_RECENT_UPDATE && ret)) { | |
224 | recent_entry_update(t, e); | |
225 | e->ttl = ttl; | |
226 | } | |
227 | out: | |
228 | spin_unlock_bh(&recent_lock); | |
229 | return ret; | |
1da177e4 LT |
230 | } |
231 | ||
1da177e4 | 232 | static int |
404bdbfd PM |
233 | ipt_recent_checkentry(const char *tablename, const void *ip, |
234 | const struct xt_match *match, void *matchinfo, | |
235 | unsigned int matchsize, unsigned int hook_mask) | |
1da177e4 | 236 | { |
1da177e4 | 237 | const struct ipt_recent_info *info = matchinfo; |
404bdbfd PM |
238 | struct recent_table *t; |
239 | unsigned i; | |
240 | int ret = 0; | |
1da177e4 | 241 | |
404bdbfd PM |
242 | if (hweight8(info->check_set & |
243 | (IPT_RECENT_SET | IPT_RECENT_REMOVE | | |
244 | IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1) | |
245 | return 0; | |
246 | if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) && | |
247 | (info->seconds || info->hit_count)) | |
248 | return 0; | |
249 | if (info->name[0] == '\0' || | |
250 | strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN) | |
251 | return 0; | |
1da177e4 | 252 | |
a0e889bb | 253 | mutex_lock(&recent_mutex); |
404bdbfd PM |
254 | t = recent_table_lookup(info->name); |
255 | if (t != NULL) { | |
256 | t->refcnt++; | |
257 | ret = 1; | |
258 | goto out; | |
1da177e4 LT |
259 | } |
260 | ||
404bdbfd | 261 | t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size, |
a0e889bb | 262 | GFP_KERNEL); |
404bdbfd PM |
263 | if (t == NULL) |
264 | goto out; | |
265 | strcpy(t->name, info->name); | |
266 | INIT_LIST_HEAD(&t->lru_list); | |
267 | for (i = 0; i < ip_list_hash_size; i++) | |
268 | INIT_LIST_HEAD(&t->iphash[i]); | |
269 | #ifdef CONFIG_PROC_FS | |
270 | t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir); | |
271 | if (t->proc == NULL) { | |
272 | kfree(t); | |
273 | goto out; | |
1da177e4 | 274 | } |
404bdbfd PM |
275 | t->proc->proc_fops = &recent_fops; |
276 | t->proc->data = t; | |
1da177e4 | 277 | #endif |
a0e889bb | 278 | spin_lock_bh(&recent_lock); |
404bdbfd | 279 | list_add_tail(&t->list, &tables); |
a0e889bb | 280 | spin_unlock_bh(&recent_lock); |
404bdbfd PM |
281 | ret = 1; |
282 | out: | |
a0e889bb | 283 | mutex_unlock(&recent_mutex); |
404bdbfd PM |
284 | return ret; |
285 | } | |
1da177e4 | 286 | |
404bdbfd PM |
287 | static void |
288 | ipt_recent_destroy(const struct xt_match *match, void *matchinfo, | |
289 | unsigned int matchsize) | |
290 | { | |
291 | const struct ipt_recent_info *info = matchinfo; | |
292 | struct recent_table *t; | |
1da177e4 | 293 | |
a0e889bb | 294 | mutex_lock(&recent_mutex); |
404bdbfd PM |
295 | t = recent_table_lookup(info->name); |
296 | if (--t->refcnt == 0) { | |
a0e889bb | 297 | spin_lock_bh(&recent_lock); |
404bdbfd | 298 | list_del(&t->list); |
a0e889bb | 299 | spin_unlock_bh(&recent_lock); |
404bdbfd PM |
300 | recent_table_flush(t); |
301 | #ifdef CONFIG_PROC_FS | |
302 | remove_proc_entry(t->name, proc_dir); | |
1da177e4 | 303 | #endif |
404bdbfd | 304 | kfree(t); |
1da177e4 | 305 | } |
a0e889bb | 306 | mutex_unlock(&recent_mutex); |
404bdbfd | 307 | } |
1da177e4 | 308 | |
404bdbfd PM |
309 | #ifdef CONFIG_PROC_FS |
310 | struct recent_iter_state { | |
311 | struct recent_table *table; | |
312 | unsigned int bucket; | |
313 | }; | |
1da177e4 | 314 | |
404bdbfd PM |
315 | static void *recent_seq_start(struct seq_file *seq, loff_t *pos) |
316 | { | |
317 | struct recent_iter_state *st = seq->private; | |
318 | struct recent_table *t = st->table; | |
319 | struct recent_entry *e; | |
320 | loff_t p = *pos; | |
1da177e4 | 321 | |
1da177e4 | 322 | spin_lock_bh(&recent_lock); |
1da177e4 | 323 | |
404bdbfd PM |
324 | for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) { |
325 | list_for_each_entry(e, &t->iphash[st->bucket], list) { | |
326 | if (p-- == 0) | |
327 | return e; | |
1da177e4 | 328 | } |
1da177e4 | 329 | } |
404bdbfd PM |
330 | return NULL; |
331 | } | |
1da177e4 | 332 | |
404bdbfd PM |
333 | static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
334 | { | |
335 | struct recent_iter_state *st = seq->private; | |
336 | struct recent_table *t = st->table; | |
337 | struct recent_entry *e = v; | |
338 | struct list_head *head = e->list.next; | |
339 | ||
340 | while (head == &t->iphash[st->bucket]) { | |
341 | if (++st->bucket >= ip_list_hash_size) | |
342 | return NULL; | |
343 | head = t->iphash[st->bucket].next; | |
344 | } | |
345 | (*pos)++; | |
346 | return list_entry(head, struct recent_entry, list); | |
1da177e4 LT |
347 | } |
348 | ||
404bdbfd | 349 | static void recent_seq_stop(struct seq_file *s, void *v) |
1da177e4 | 350 | { |
404bdbfd PM |
351 | spin_unlock_bh(&recent_lock); |
352 | } | |
1da177e4 | 353 | |
404bdbfd PM |
354 | static int recent_seq_show(struct seq_file *seq, void *v) |
355 | { | |
356 | struct recent_entry *e = v; | |
357 | unsigned int i; | |
358 | ||
359 | i = (e->index - 1) % ip_pkt_list_tot; | |
360 | seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u", | |
361 | NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index); | |
362 | for (i = 0; i < e->nstamps; i++) | |
363 | seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]); | |
364 | seq_printf(seq, "\n"); | |
365 | return 0; | |
366 | } | |
1da177e4 | 367 | |
404bdbfd PM |
368 | static struct seq_operations recent_seq_ops = { |
369 | .start = recent_seq_start, | |
370 | .next = recent_seq_next, | |
371 | .stop = recent_seq_stop, | |
372 | .show = recent_seq_show, | |
373 | }; | |
1da177e4 | 374 | |
404bdbfd PM |
375 | static int recent_seq_open(struct inode *inode, struct file *file) |
376 | { | |
377 | struct proc_dir_entry *pde = PDE(inode); | |
378 | struct seq_file *seq; | |
379 | struct recent_iter_state *st; | |
380 | int ret; | |
381 | ||
382 | st = kzalloc(sizeof(*st), GFP_KERNEL); | |
383 | if (st == NULL) | |
384 | return -ENOMEM; | |
385 | ret = seq_open(file, &recent_seq_ops); | |
386 | if (ret) | |
387 | kfree(st); | |
388 | st->table = pde->data; | |
389 | seq = file->private_data; | |
390 | seq->private = st; | |
391 | return ret; | |
392 | } | |
1da177e4 | 393 | |
404bdbfd PM |
394 | static ssize_t recent_proc_write(struct file *file, const char __user *input, |
395 | size_t size, loff_t *loff) | |
396 | { | |
397 | struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode); | |
398 | struct recent_table *t = pde->data; | |
399 | struct recent_entry *e; | |
400 | char buf[sizeof("+255.255.255.255")], *c = buf; | |
401 | u_int32_t addr; | |
402 | int add; | |
403 | ||
404 | if (size > sizeof(buf)) | |
405 | size = sizeof(buf); | |
406 | if (copy_from_user(buf, input, size)) | |
407 | return -EFAULT; | |
408 | while (isspace(*c)) | |
409 | c++; | |
410 | ||
411 | if (size - (c - buf) < 5) | |
412 | return c - buf; | |
413 | if (!strncmp(c, "clear", 5)) { | |
414 | c += 5; | |
415 | spin_lock_bh(&recent_lock); | |
416 | recent_table_flush(t); | |
1da177e4 | 417 | spin_unlock_bh(&recent_lock); |
404bdbfd | 418 | return c - buf; |
1da177e4 | 419 | } |
1da177e4 | 420 | |
404bdbfd PM |
421 | switch (*c) { |
422 | case '-': | |
423 | add = 0; | |
424 | c++; | |
425 | break; | |
426 | case '+': | |
427 | c++; | |
428 | default: | |
429 | add = 1; | |
430 | break; | |
1da177e4 | 431 | } |
404bdbfd | 432 | addr = in_aton(c); |
1da177e4 | 433 | |
404bdbfd PM |
434 | spin_lock_bh(&recent_lock); |
435 | e = recent_entry_lookup(t, addr, 0); | |
436 | if (e == NULL) { | |
437 | if (add) | |
438 | recent_entry_init(t, addr, 0); | |
439 | } else { | |
440 | if (add) | |
441 | recent_entry_update(t, e); | |
442 | else | |
443 | recent_entry_remove(t, e); | |
1da177e4 | 444 | } |
1da177e4 | 445 | spin_unlock_bh(&recent_lock); |
404bdbfd PM |
446 | return size; |
447 | } | |
1da177e4 | 448 | |
404bdbfd PM |
449 | static struct file_operations recent_fops = { |
450 | .open = recent_seq_open, | |
451 | .read = seq_read, | |
452 | .write = recent_proc_write, | |
453 | .release = seq_release_private, | |
454 | .owner = THIS_MODULE, | |
455 | }; | |
1da177e4 | 456 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 457 | |
1d5cd909 PM |
458 | static struct ipt_match recent_match = { |
459 | .name = "recent", | |
404bdbfd | 460 | .match = ipt_recent_match, |
1d5cd909 | 461 | .matchsize = sizeof(struct ipt_recent_info), |
404bdbfd PM |
462 | .checkentry = ipt_recent_checkentry, |
463 | .destroy = ipt_recent_destroy, | |
464 | .me = THIS_MODULE, | |
1da177e4 LT |
465 | }; |
466 | ||
65b4b4e8 | 467 | static int __init ipt_recent_init(void) |
1da177e4 | 468 | { |
404bdbfd | 469 | int err; |
1da177e4 | 470 | |
404bdbfd PM |
471 | if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255) |
472 | return -EINVAL; | |
473 | ip_list_hash_size = 1 << fls(ip_list_tot); | |
1da177e4 LT |
474 | |
475 | err = ipt_register_match(&recent_match); | |
404bdbfd | 476 | #ifdef CONFIG_PROC_FS |
1da177e4 | 477 | if (err) |
404bdbfd PM |
478 | return err; |
479 | proc_dir = proc_mkdir("ipt_recent", proc_net); | |
480 | if (proc_dir == NULL) { | |
481 | ipt_unregister_match(&recent_match); | |
482 | err = -ENOMEM; | |
483 | } | |
484 | #endif | |
1da177e4 LT |
485 | return err; |
486 | } | |
487 | ||
404bdbfd | 488 | static void __exit ipt_recent_exit(void) |
1da177e4 | 489 | { |
404bdbfd | 490 | BUG_ON(!list_empty(&tables)); |
1da177e4 | 491 | ipt_unregister_match(&recent_match); |
404bdbfd PM |
492 | #ifdef CONFIG_PROC_FS |
493 | remove_proc_entry("ipt_recent", proc_net); | |
494 | #endif | |
1da177e4 LT |
495 | } |
496 | ||
65b4b4e8 | 497 | module_init(ipt_recent_init); |
404bdbfd | 498 | module_exit(ipt_recent_exit); |