RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / xt_recent.c
blob30516cb730a5a38a18db1f619d01919d860f1601
1 /*
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>
16 #include <linux/ip.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>
31 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter/xt_recent.h>
34 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
35 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
36 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS("ipt_recent");
39 MODULE_ALIAS("ip6t_recent");
41 static unsigned int ip_list_tot = 100;
42 static unsigned int ip_pkt_list_tot = 20;
43 static unsigned int ip_list_hash_size = 0;
44 static unsigned int ip_list_perms = 0644;
45 static unsigned int ip_list_uid = 0;
46 static unsigned int ip_list_gid = 0;
47 module_param(ip_list_tot, uint, 0400);
48 module_param(ip_pkt_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, S_IRUGO | S_IWUSR);
52 module_param(ip_list_gid, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
54 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
55 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
56 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
57 MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files");
58 MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files");
60 struct recent_entry {
61 struct list_head list;
62 struct list_head lru_list;
63 union nf_inet_addr addr;
64 u_int16_t family;
65 u_int8_t ttl;
66 u_int8_t index;
67 u_int16_t nstamps;
68 unsigned long stamps[0];
71 struct recent_table {
72 struct list_head list;
73 char name[XT_RECENT_NAME_LEN];
74 unsigned int refcnt;
75 unsigned int entries;
76 struct list_head lru_list;
77 struct list_head iphash[0];
80 static LIST_HEAD(tables);
81 static DEFINE_SPINLOCK(recent_lock);
82 static DEFINE_MUTEX(recent_mutex);
84 #ifdef CONFIG_PROC_FS
85 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
86 static struct proc_dir_entry *proc_old_dir;
87 static const struct file_operations recent_old_fops;
88 #endif
89 static struct proc_dir_entry *recent_proc_dir;
90 static const struct file_operations recent_mt_fops;
91 #endif
93 static u_int32_t hash_rnd __read_mostly;
94 static bool hash_rnd_inited __read_mostly;
96 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
98 return jhash_1word((__force u32)addr->ip, hash_rnd) &
99 (ip_list_hash_size - 1);
102 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
104 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
105 (ip_list_hash_size - 1);
108 static struct recent_entry *
109 recent_entry_lookup(const struct recent_table *table,
110 const union nf_inet_addr *addrp, u_int16_t family,
111 u_int8_t ttl)
113 struct recent_entry *e;
114 unsigned int h;
116 if (family == AF_INET)
117 h = recent_entry_hash4(addrp);
118 else
119 h = recent_entry_hash6(addrp);
121 list_for_each_entry(e, &table->iphash[h], list)
122 if (e->family == family &&
123 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
124 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
125 return e;
126 return NULL;
129 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
131 list_del(&e->list);
132 list_del(&e->lru_list);
133 kfree(e);
134 t->entries--;
138 * Drop entries with timestamps older then 'time'.
140 static void recent_entry_reap(struct recent_table *t, unsigned long time)
142 struct recent_entry *e;
145 * The head of the LRU list is always the oldest entry.
147 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
150 * The last time stamp is the most recent.
152 if (time_after(time, e->stamps[e->index-1]))
153 recent_entry_remove(t, e);
156 static struct recent_entry *
157 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
158 u_int16_t family, u_int8_t ttl)
160 struct recent_entry *e;
162 if (t->entries >= ip_list_tot) {
163 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
164 recent_entry_remove(t, e);
166 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
167 GFP_ATOMIC);
168 if (e == NULL)
169 return NULL;
170 memcpy(&e->addr, addr, sizeof(e->addr));
171 e->ttl = ttl;
172 e->stamps[0] = jiffies;
173 e->nstamps = 1;
174 e->index = 1;
175 e->family = family;
176 if (family == AF_INET)
177 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
178 else
179 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
180 list_add_tail(&e->lru_list, &t->lru_list);
181 t->entries++;
182 return e;
185 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
187 e->index %= ip_pkt_list_tot;
188 e->stamps[e->index++] = jiffies;
189 if (e->index > e->nstamps)
190 e->nstamps = e->index;
191 list_move_tail(&e->lru_list, &t->lru_list);
194 static struct recent_table *recent_table_lookup(const char *name)
196 struct recent_table *t;
198 list_for_each_entry(t, &tables, list)
199 if (!strcmp(t->name, name))
200 return t;
201 return NULL;
204 static void recent_table_flush(struct recent_table *t)
206 struct recent_entry *e, *next;
207 unsigned int i;
209 for (i = 0; i < ip_list_hash_size; i++) {
210 list_for_each_entry_safe(e, next, &t->iphash[i], list)
211 recent_entry_remove(t, e);
215 static int
216 recent_mt(const struct sk_buff *skb,
217 const struct net_device *in, const struct net_device *out,
218 const struct xt_match *match, const void *matchinfo,
219 int offset, unsigned int protoff, int *hotdrop)
221 const struct xt_recent_mtinfo *info = matchinfo;
222 struct recent_table *t;
223 struct recent_entry *e;
224 union nf_inet_addr addr = {};
225 u_int8_t ttl;
226 int ret = info->invert;
228 if (match->family == AF_INET) {
229 const struct iphdr *iph = ip_hdr(skb);
231 if (info->side == XT_RECENT_DEST)
232 addr.ip = iph->daddr;
233 else
234 addr.ip = iph->saddr;
236 ttl = iph->ttl;
237 } else {
238 const struct ipv6hdr *iph = ipv6_hdr(skb);
240 if (info->side == XT_RECENT_DEST)
241 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
242 else
243 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
245 ttl = iph->hop_limit;
248 /* use TTL as seen before forwarding */
249 if (out && !skb->sk)
250 ttl++;
252 spin_lock_bh(&recent_lock);
253 t = recent_table_lookup(info->name);
254 e = recent_entry_lookup(t, &addr, match->family,
255 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
256 if (e == NULL) {
257 if (!(info->check_set & XT_RECENT_SET))
258 goto out;
259 e = recent_entry_init(t, &addr, match->family, ttl);
260 if (e == NULL)
261 *hotdrop = 1;
262 ret ^= 1;
263 goto out;
266 if (info->check_set & XT_RECENT_SET)
267 ret ^= 1;
268 else if (info->check_set & XT_RECENT_REMOVE) {
269 recent_entry_remove(t, e);
270 ret ^= 1;
271 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
272 unsigned long time = jiffies - info->seconds * HZ;
273 unsigned int i, hits = 0;
275 for (i = 0; i < e->nstamps; i++) {
276 if (info->seconds && time_after(time, e->stamps[i]))
277 continue;
278 if (!info->hit_count || ++hits >= info->hit_count) {
279 ret ^= 1;
280 break;
284 /* info->seconds must be non-zero */
285 if (info->check_set & XT_RECENT_REAP)
286 recent_entry_reap(t, time);
289 if (info->check_set & XT_RECENT_SET ||
290 (info->check_set & XT_RECENT_UPDATE && ret)) {
291 recent_entry_update(t, e);
292 e->ttl = ttl;
294 out:
295 spin_unlock_bh(&recent_lock);
296 return ret;
299 static int
300 recent_mt_check(const char *tablename, const void *ip,
301 const struct xt_match *match, void *matchinfo,
302 unsigned int hook_mask)
304 const struct xt_recent_mtinfo *info = matchinfo;
305 struct recent_table *t;
306 #ifdef CONFIG_PROC_FS
307 struct proc_dir_entry *pde;
308 #endif
309 unsigned i;
310 int ret = 0;
312 if (unlikely(!hash_rnd_inited)) {
313 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
314 hash_rnd_inited = true;
316 if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
317 pr_info(KBUILD_MODNAME ": Unsupported user space flags "
318 "(%08x)\n", info->check_set);
319 return 0;
321 if (hweight8(info->check_set &
322 (XT_RECENT_SET | XT_RECENT_REMOVE |
323 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
324 return 0;
325 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
326 (info->seconds || info->hit_count ||
327 (info->check_set & XT_RECENT_MODIFIERS)))
328 return 0;
329 if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
330 return 0;
331 if (info->hit_count > ip_pkt_list_tot) {
332 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
333 "packets to be remembered (%u)\n",
334 info->hit_count, ip_pkt_list_tot);
335 return 0;
337 if (info->name[0] == '\0' ||
338 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
339 return 0;
341 mutex_lock(&recent_mutex);
342 t = recent_table_lookup(info->name);
343 if (t != NULL) {
344 t->refcnt++;
345 ret = 1;
346 goto out;
349 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
350 GFP_KERNEL);
351 if (t == NULL)
352 goto out;
353 t->refcnt = 1;
354 strcpy(t->name, info->name);
355 INIT_LIST_HEAD(&t->lru_list);
356 for (i = 0; i < ip_list_hash_size; i++)
357 INIT_LIST_HEAD(&t->iphash[i]);
358 #ifdef CONFIG_PROC_FS
359 pde = create_proc_entry(t->name, ip_list_perms, recent_proc_dir);
360 if (pde == NULL) {
361 kfree(t);
362 goto out;
364 pde->proc_fops = &recent_mt_fops;
365 pde->uid = ip_list_uid;
366 pde->gid = ip_list_gid;
367 pde->data = t;
368 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
369 pde = create_proc_entry(t->name, ip_list_perms, proc_old_dir);
370 if (pde == NULL) {
371 remove_proc_entry(t->name, recent_proc_dir);
372 kfree(t);
373 goto out;
375 pde->proc_fops = &recent_old_fops;
376 pde->uid = ip_list_uid;
377 pde->gid = ip_list_gid;
378 pde->data = t;
379 #endif
380 #endif
381 spin_lock_bh(&recent_lock);
382 list_add_tail(&t->list, &tables);
383 spin_unlock_bh(&recent_lock);
384 ret = 1;
385 out:
386 mutex_unlock(&recent_mutex);
387 return ret;
390 static void
391 recent_mt_destroy(const struct xt_match *match, void *matchinfo)
393 const struct xt_recent_mtinfo *info = matchinfo;
394 struct recent_table *t;
396 mutex_lock(&recent_mutex);
397 t = recent_table_lookup(info->name);
398 if (--t->refcnt == 0) {
399 spin_lock_bh(&recent_lock);
400 list_del(&t->list);
401 spin_unlock_bh(&recent_lock);
402 #ifdef CONFIG_PROC_FS
403 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
404 remove_proc_entry(t->name, proc_old_dir);
405 #endif
406 remove_proc_entry(t->name, recent_proc_dir);
407 #endif
408 recent_table_flush(t);
409 kfree(t);
411 mutex_unlock(&recent_mutex);
414 #ifdef CONFIG_PROC_FS
415 struct recent_iter_state {
416 const struct recent_table *table;
417 unsigned int bucket;
420 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
421 __acquires(recent_lock)
423 struct recent_iter_state *st = seq->private;
424 const struct recent_table *t = st->table;
425 struct recent_entry *e;
426 loff_t p = *pos;
428 spin_lock_bh(&recent_lock);
430 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) {
431 list_for_each_entry(e, &t->iphash[st->bucket], list) {
432 if (p-- == 0)
433 return e;
436 return NULL;
439 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
441 struct recent_iter_state *st = seq->private;
442 const struct recent_table *t = st->table;
443 const struct recent_entry *e = v;
444 const struct list_head *head = e->list.next;
446 while (head == &t->iphash[st->bucket]) {
447 if (++st->bucket >= ip_list_hash_size)
448 return NULL;
449 head = t->iphash[st->bucket].next;
451 (*pos)++;
452 return list_entry(head, struct recent_entry, list);
455 static void recent_seq_stop(struct seq_file *s, void *v)
456 __releases(recent_lock)
458 spin_unlock_bh(&recent_lock);
461 static int recent_seq_show(struct seq_file *seq, void *v)
463 struct recent_entry *e = v;
464 unsigned int i;
466 i = (e->index - 1) % ip_pkt_list_tot;
467 if (e->family == AF_INET)
468 seq_printf(seq, "src=" NIPQUAD_FMT " ttl: %u last_seen: %lu "
469 "oldest_pkt: %u", NIPQUAD(e->addr.ip), e->ttl,
470 e->stamps[i], e->index);
471 else
472 seq_printf(seq, "src=" NIP6_FMT " ttl: %u last_seen: %lu "
473 "oldest_pkt: %u", NIP6(e->addr.in6), e->ttl,
474 e->stamps[i], e->index);
475 for (i = 0; i < e->nstamps; i++)
476 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
477 seq_printf(seq, "\n");
478 return 0;
481 static struct seq_operations recent_seq_ops = {
482 .start = recent_seq_start,
483 .next = recent_seq_next,
484 .stop = recent_seq_stop,
485 .show = recent_seq_show,
488 static int recent_seq_open(struct inode *inode, struct file *file)
490 struct proc_dir_entry *pde = PDE(inode);
491 struct recent_iter_state *st;
493 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
494 if (st == NULL)
495 return -ENOMEM;
497 st->table = pde->data;
498 return 0;
501 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
502 static int recent_old_seq_open(struct inode *inode, struct file *filp)
504 static bool warned_of_old;
506 if (unlikely(!warned_of_old)) {
507 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
508 " is deprecated; use /proc/net/xt_recent.\n");
509 warned_of_old = true;
511 return recent_seq_open(inode, filp);
514 static ssize_t recent_old_proc_write(struct file *file,
515 const char __user *input,
516 size_t size, loff_t *loff)
518 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
519 struct recent_table *t = pde->data;
520 struct recent_entry *e;
521 char buf[sizeof("+255.255.255.255")], *c = buf;
522 union nf_inet_addr addr = {};
523 int add;
525 if (size > sizeof(buf))
526 size = sizeof(buf);
527 if (copy_from_user(buf, input, size))
528 return -EFAULT;
530 while (isspace(*c))
531 c++;
533 if (size - (c - buf) < 5)
534 return c - buf;
535 if (!strncmp(c, "clear", 5)) {
536 c += 5;
537 spin_lock_bh(&recent_lock);
538 recent_table_flush(t);
539 spin_unlock_bh(&recent_lock);
540 return c - buf;
543 switch (*c) {
544 case '-':
545 add = 0;
546 c++;
547 break;
548 case '+':
549 c++;
550 default:
551 add = 1;
552 break;
554 addr.ip = in_aton(c);
556 spin_lock_bh(&recent_lock);
557 e = recent_entry_lookup(t, &addr, PF_INET, 0);
558 if (e == NULL) {
559 if (add)
560 recent_entry_init(t, &addr, PF_INET, 0);
561 } else {
562 if (add)
563 recent_entry_update(t, e);
564 else
565 recent_entry_remove(t, e);
567 spin_unlock_bh(&recent_lock);
568 return size;
571 static const struct file_operations recent_old_fops = {
572 .open = recent_old_seq_open,
573 .read = seq_read,
574 .write = recent_old_proc_write,
575 .release = seq_release_private,
576 .owner = THIS_MODULE,
578 #endif
580 static ssize_t
581 recent_mt_proc_write(struct file *file, const char __user *input,
582 size_t size, loff_t *loff)
584 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
585 struct recent_table *t = pde->data;
586 struct recent_entry *e;
587 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
588 const char *c = buf;
589 union nf_inet_addr addr = {};
590 u_int16_t family;
591 bool add, succ;
593 if (size == 0)
594 return 0;
595 if (size > sizeof(buf))
596 size = sizeof(buf);
597 if (copy_from_user(buf, input, size) != 0)
598 return -EFAULT;
600 /* Strict protocol! */
601 if (*loff != 0)
602 return -ESPIPE;
603 switch (*c) {
604 case '/': /* flush table */
605 spin_lock_bh(&recent_lock);
606 recent_table_flush(t);
607 spin_unlock_bh(&recent_lock);
608 return size;
609 case '-': /* remove address */
610 add = false;
611 break;
612 case '+': /* add address */
613 add = true;
614 break;
615 default:
616 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
617 return -EINVAL;
620 ++c;
621 --size;
622 if (strnchr(c, size, ':') != NULL) {
623 family = AF_INET6;
624 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
625 } else {
626 family = AF_INET;
627 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
630 if (!succ) {
631 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
632 "to procfs\n");
633 return -EINVAL;
636 spin_lock_bh(&recent_lock);
637 e = recent_entry_lookup(t, &addr, family, 0);
638 if (e == NULL) {
639 if (add)
640 recent_entry_init(t, &addr, family, 0);
641 } else {
642 if (add)
643 recent_entry_update(t, e);
644 else
645 recent_entry_remove(t, e);
647 spin_unlock_bh(&recent_lock);
648 /* Note we removed one above */
649 *loff += size + 1;
650 return size + 1;
653 static const struct file_operations recent_mt_fops = {
654 .open = recent_seq_open,
655 .read = seq_read,
656 .write = recent_mt_proc_write,
657 .release = seq_release_private,
658 .owner = THIS_MODULE,
660 #endif /* CONFIG_PROC_FS */
662 static struct xt_match recent_mt_reg[] __read_mostly = {
664 .name = "recent",
665 .family = AF_INET,
666 .match = recent_mt,
667 .matchsize = sizeof(struct xt_recent_mtinfo),
668 .checkentry = recent_mt_check,
669 .destroy = recent_mt_destroy,
670 .me = THIS_MODULE,
673 .name = "recent",
674 .family = AF_INET6,
675 .match = recent_mt,
676 .matchsize = sizeof(struct xt_recent_mtinfo),
677 .checkentry = recent_mt_check,
678 .destroy = recent_mt_destroy,
679 .me = THIS_MODULE,
683 static int __init recent_mt_init(void)
685 int err;
687 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
688 return -EINVAL;
689 ip_list_hash_size = 1 << fls(ip_list_tot);
691 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
692 #ifdef CONFIG_PROC_FS
693 if (err)
694 return err;
695 recent_proc_dir = proc_mkdir("xt_recent", proc_net);
696 if (recent_proc_dir == NULL) {
697 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
698 err = -ENOMEM;
700 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
701 if (err < 0)
702 return err;
703 proc_old_dir = proc_mkdir("ipt_recent", proc_net);
704 if (proc_old_dir == NULL) {
705 remove_proc_entry("xt_recent", proc_net);
706 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
707 err = -ENOMEM;
709 #endif
710 #endif
711 return err;
714 static void __exit recent_mt_exit(void)
716 BUG_ON(!list_empty(&tables));
717 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
718 #ifdef CONFIG_PROC_FS
719 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
720 remove_proc_entry("ipt_recent", proc_net);
721 #endif
722 remove_proc_entry("xt_recent", proc_net);
723 #endif
726 module_init(recent_mt_init);
727 module_exit(recent_mt_exit);