drm/cred: wrap task credential accesses in the drm driver.
[linux-2.6/linux-2.6-openrd.git] / net / netfilter / xt_recent.c
blob4ebd4ca9a991bf871a23bf672ea2b30d5a9095d7
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>
30 #include <net/net_namespace.h>
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter/xt_recent.h>
35 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
36 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
37 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
38 MODULE_LICENSE("GPL");
39 MODULE_ALIAS("ipt_recent");
40 MODULE_ALIAS("ip6t_recent");
42 static unsigned int ip_list_tot = 100;
43 static unsigned int ip_pkt_list_tot = 20;
44 static unsigned int ip_list_hash_size = 0;
45 static unsigned int ip_list_perms = 0644;
46 static unsigned int ip_list_uid = 0;
47 static unsigned int ip_list_gid = 0;
48 module_param(ip_list_tot, uint, 0400);
49 module_param(ip_pkt_list_tot, uint, 0400);
50 module_param(ip_list_hash_size, uint, 0400);
51 module_param(ip_list_perms, uint, 0400);
52 module_param(ip_list_uid, uint, 0400);
53 module_param(ip_list_gid, uint, 0400);
54 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
55 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
56 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
57 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
58 MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
59 MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
61 struct recent_entry {
62 struct list_head list;
63 struct list_head lru_list;
64 union nf_inet_addr addr;
65 u_int16_t family;
66 u_int8_t ttl;
67 u_int8_t index;
68 u_int16_t nstamps;
69 unsigned long stamps[0];
72 struct recent_table {
73 struct list_head list;
74 char name[XT_RECENT_NAME_LEN];
75 #ifdef CONFIG_PROC_FS
76 struct proc_dir_entry *proc_old, *proc;
77 #endif
78 unsigned int refcnt;
79 unsigned int entries;
80 struct list_head lru_list;
81 struct list_head iphash[0];
84 static LIST_HEAD(tables);
85 static DEFINE_SPINLOCK(recent_lock);
86 static DEFINE_MUTEX(recent_mutex);
88 #ifdef CONFIG_PROC_FS
89 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
90 static struct proc_dir_entry *proc_old_dir;
91 #endif
92 static struct proc_dir_entry *recent_proc_dir;
93 static const struct file_operations recent_old_fops, recent_mt_fops;
94 #endif
96 static u_int32_t hash_rnd;
97 static bool hash_rnd_initted;
99 static unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
101 if (!hash_rnd_initted) {
102 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
103 hash_rnd_initted = true;
105 return jhash_1word((__force u32)addr->ip, hash_rnd) &
106 (ip_list_hash_size - 1);
109 static unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
111 if (!hash_rnd_initted) {
112 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
113 hash_rnd_initted = true;
115 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
116 (ip_list_hash_size - 1);
119 static struct recent_entry *
120 recent_entry_lookup(const struct recent_table *table,
121 const union nf_inet_addr *addrp, u_int16_t family,
122 u_int8_t ttl)
124 struct recent_entry *e;
125 unsigned int h;
127 if (family == NFPROTO_IPV4)
128 h = recent_entry_hash4(addrp);
129 else
130 h = recent_entry_hash6(addrp);
132 list_for_each_entry(e, &table->iphash[h], list)
133 if (e->family == family &&
134 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
135 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
136 return e;
137 return NULL;
140 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
142 list_del(&e->list);
143 list_del(&e->lru_list);
144 kfree(e);
145 t->entries--;
148 static struct recent_entry *
149 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
150 u_int16_t family, u_int8_t ttl)
152 struct recent_entry *e;
154 if (t->entries >= ip_list_tot) {
155 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
156 recent_entry_remove(t, e);
158 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
159 GFP_ATOMIC);
160 if (e == NULL)
161 return NULL;
162 memcpy(&e->addr, addr, sizeof(e->addr));
163 e->ttl = ttl;
164 e->stamps[0] = jiffies;
165 e->nstamps = 1;
166 e->index = 1;
167 e->family = family;
168 if (family == NFPROTO_IPV4)
169 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
170 else
171 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
172 list_add_tail(&e->lru_list, &t->lru_list);
173 t->entries++;
174 return e;
177 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
179 e->stamps[e->index++] = jiffies;
180 if (e->index > e->nstamps)
181 e->nstamps = e->index;
182 e->index %= ip_pkt_list_tot;
183 list_move_tail(&e->lru_list, &t->lru_list);
186 static struct recent_table *recent_table_lookup(const char *name)
188 struct recent_table *t;
190 list_for_each_entry(t, &tables, list)
191 if (!strcmp(t->name, name))
192 return t;
193 return NULL;
196 static void recent_table_flush(struct recent_table *t)
198 struct recent_entry *e, *next;
199 unsigned int i;
201 for (i = 0; i < ip_list_hash_size; i++)
202 list_for_each_entry_safe(e, next, &t->iphash[i], list)
203 recent_entry_remove(t, e);
206 static bool
207 recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
209 const struct xt_recent_mtinfo *info = par->matchinfo;
210 struct recent_table *t;
211 struct recent_entry *e;
212 union nf_inet_addr addr = {};
213 u_int8_t ttl;
214 bool ret = info->invert;
216 if (par->match->family == NFPROTO_IPV4) {
217 const struct iphdr *iph = ip_hdr(skb);
219 if (info->side == XT_RECENT_DEST)
220 addr.ip = iph->daddr;
221 else
222 addr.ip = iph->saddr;
224 ttl = iph->ttl;
225 } else {
226 const struct ipv6hdr *iph = ipv6_hdr(skb);
228 if (info->side == XT_RECENT_DEST)
229 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
230 else
231 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
233 ttl = iph->hop_limit;
236 /* use TTL as seen before forwarding */
237 if (par->out != NULL && skb->sk == NULL)
238 ttl++;
240 spin_lock_bh(&recent_lock);
241 t = recent_table_lookup(info->name);
242 e = recent_entry_lookup(t, &addr, par->match->family,
243 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
244 if (e == NULL) {
245 if (!(info->check_set & XT_RECENT_SET))
246 goto out;
247 e = recent_entry_init(t, &addr, par->match->family, ttl);
248 if (e == NULL)
249 *par->hotdrop = true;
250 ret = !ret;
251 goto out;
254 if (info->check_set & XT_RECENT_SET)
255 ret = !ret;
256 else if (info->check_set & XT_RECENT_REMOVE) {
257 recent_entry_remove(t, e);
258 ret = !ret;
259 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
260 unsigned long time = jiffies - info->seconds * HZ;
261 unsigned int i, hits = 0;
263 for (i = 0; i < e->nstamps; i++) {
264 if (info->seconds && time_after(time, e->stamps[i]))
265 continue;
266 if (++hits >= info->hit_count) {
267 ret = !ret;
268 break;
273 if (info->check_set & XT_RECENT_SET ||
274 (info->check_set & XT_RECENT_UPDATE && ret)) {
275 recent_entry_update(t, e);
276 e->ttl = ttl;
278 out:
279 spin_unlock_bh(&recent_lock);
280 return ret;
283 static bool recent_mt_check(const struct xt_mtchk_param *par)
285 const struct xt_recent_mtinfo *info = par->matchinfo;
286 struct recent_table *t;
287 unsigned i;
288 bool ret = false;
290 if (hweight8(info->check_set &
291 (XT_RECENT_SET | XT_RECENT_REMOVE |
292 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
293 return false;
294 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
295 (info->seconds || info->hit_count))
296 return false;
297 if (info->hit_count > ip_pkt_list_tot)
298 return false;
299 if (info->name[0] == '\0' ||
300 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
301 return false;
303 mutex_lock(&recent_mutex);
304 t = recent_table_lookup(info->name);
305 if (t != NULL) {
306 t->refcnt++;
307 ret = true;
308 goto out;
311 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
312 GFP_KERNEL);
313 if (t == NULL)
314 goto out;
315 t->refcnt = 1;
316 strcpy(t->name, info->name);
317 INIT_LIST_HEAD(&t->lru_list);
318 for (i = 0; i < ip_list_hash_size; i++)
319 INIT_LIST_HEAD(&t->iphash[i]);
320 #ifdef CONFIG_PROC_FS
321 t->proc = proc_create(t->name, ip_list_perms, recent_proc_dir,
322 &recent_mt_fops);
323 if (t->proc == NULL) {
324 kfree(t);
325 goto out;
327 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
328 t->proc_old = proc_create(t->name, ip_list_perms, proc_old_dir,
329 &recent_old_fops);
330 if (t->proc_old == NULL) {
331 remove_proc_entry(t->name, proc_old_dir);
332 kfree(t);
333 goto out;
335 t->proc_old->uid = ip_list_uid;
336 t->proc_old->gid = ip_list_gid;
337 t->proc_old->data = t;
338 #endif
339 t->proc->uid = ip_list_uid;
340 t->proc->gid = ip_list_gid;
341 t->proc->data = t;
342 #endif
343 spin_lock_bh(&recent_lock);
344 list_add_tail(&t->list, &tables);
345 spin_unlock_bh(&recent_lock);
346 ret = true;
347 out:
348 mutex_unlock(&recent_mutex);
349 return ret;
352 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
354 const struct xt_recent_mtinfo *info = par->matchinfo;
355 struct recent_table *t;
357 mutex_lock(&recent_mutex);
358 t = recent_table_lookup(info->name);
359 if (--t->refcnt == 0) {
360 spin_lock_bh(&recent_lock);
361 list_del(&t->list);
362 spin_unlock_bh(&recent_lock);
363 #ifdef CONFIG_PROC_FS
364 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
365 remove_proc_entry(t->name, proc_old_dir);
366 #endif
367 remove_proc_entry(t->name, recent_proc_dir);
368 #endif
369 recent_table_flush(t);
370 kfree(t);
372 mutex_unlock(&recent_mutex);
375 #ifdef CONFIG_PROC_FS
376 struct recent_iter_state {
377 const struct recent_table *table;
378 unsigned int bucket;
381 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
382 __acquires(recent_lock)
384 struct recent_iter_state *st = seq->private;
385 const struct recent_table *t = st->table;
386 struct recent_entry *e;
387 loff_t p = *pos;
389 spin_lock_bh(&recent_lock);
391 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
392 list_for_each_entry(e, &t->iphash[st->bucket], list)
393 if (p-- == 0)
394 return e;
395 return NULL;
398 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
400 struct recent_iter_state *st = seq->private;
401 const struct recent_table *t = st->table;
402 const struct recent_entry *e = v;
403 const struct list_head *head = e->list.next;
405 while (head == &t->iphash[st->bucket]) {
406 if (++st->bucket >= ip_list_hash_size)
407 return NULL;
408 head = t->iphash[st->bucket].next;
410 (*pos)++;
411 return list_entry(head, struct recent_entry, list);
414 static void recent_seq_stop(struct seq_file *s, void *v)
415 __releases(recent_lock)
417 spin_unlock_bh(&recent_lock);
420 static int recent_seq_show(struct seq_file *seq, void *v)
422 const struct recent_entry *e = v;
423 unsigned int i;
425 i = (e->index - 1) % ip_pkt_list_tot;
426 if (e->family == NFPROTO_IPV4)
427 seq_printf(seq, "src=" NIPQUAD_FMT " ttl: %u last_seen: %lu "
428 "oldest_pkt: %u", NIPQUAD(e->addr.ip), e->ttl,
429 e->stamps[i], e->index);
430 else
431 seq_printf(seq, "src=" NIP6_FMT " ttl: %u last_seen: %lu "
432 "oldest_pkt: %u", NIP6(e->addr.in6), e->ttl,
433 e->stamps[i], e->index);
434 for (i = 0; i < e->nstamps; i++)
435 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
436 seq_printf(seq, "\n");
437 return 0;
440 static const struct seq_operations recent_seq_ops = {
441 .start = recent_seq_start,
442 .next = recent_seq_next,
443 .stop = recent_seq_stop,
444 .show = recent_seq_show,
447 static int recent_seq_open(struct inode *inode, struct file *file)
449 struct proc_dir_entry *pde = PDE(inode);
450 struct recent_iter_state *st;
452 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
453 if (st == NULL)
454 return -ENOMEM;
456 st->table = pde->data;
457 return 0;
460 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
461 static int recent_old_seq_open(struct inode *inode, struct file *filp)
463 static bool warned_of_old;
465 if (unlikely(!warned_of_old)) {
466 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
467 " is deprecated; use /proc/net/xt_recent.\n");
468 warned_of_old = true;
470 return recent_seq_open(inode, filp);
473 static ssize_t recent_old_proc_write(struct file *file,
474 const char __user *input,
475 size_t size, loff_t *loff)
477 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
478 struct recent_table *t = pde->data;
479 struct recent_entry *e;
480 char buf[sizeof("+255.255.255.255")], *c = buf;
481 __be32 addr;
482 int add;
484 if (size > sizeof(buf))
485 size = sizeof(buf);
486 if (copy_from_user(buf, input, size))
487 return -EFAULT;
489 while (isspace(*c))
490 c++;
492 if (size - (c - buf) < 5)
493 return c - buf;
494 if (!strncmp(c, "clear", 5)) {
495 c += 5;
496 spin_lock_bh(&recent_lock);
497 recent_table_flush(t);
498 spin_unlock_bh(&recent_lock);
499 return c - buf;
502 switch (*c) {
503 case '-':
504 add = 0;
505 c++;
506 break;
507 case '+':
508 c++;
509 default:
510 add = 1;
511 break;
513 addr = in_aton(c);
515 spin_lock_bh(&recent_lock);
516 e = recent_entry_lookup(t, (const void *)&addr, NFPROTO_IPV4, 0);
517 if (e == NULL) {
518 if (add)
519 recent_entry_init(t, (const void *)&addr,
520 NFPROTO_IPV4, 0);
521 } else {
522 if (add)
523 recent_entry_update(t, e);
524 else
525 recent_entry_remove(t, e);
527 spin_unlock_bh(&recent_lock);
528 return size;
531 static const struct file_operations recent_old_fops = {
532 .open = recent_old_seq_open,
533 .read = seq_read,
534 .write = recent_old_proc_write,
535 .release = seq_release_private,
536 .owner = THIS_MODULE,
538 #endif
540 static ssize_t
541 recent_mt_proc_write(struct file *file, const char __user *input,
542 size_t size, loff_t *loff)
544 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
545 struct recent_table *t = pde->data;
546 struct recent_entry *e;
547 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
548 const char *c = buf;
549 union nf_inet_addr addr;
550 u_int16_t family;
551 bool add, succ;
553 if (size == 0)
554 return 0;
555 if (size > sizeof(buf))
556 size = sizeof(buf);
557 if (copy_from_user(buf, input, size) != 0)
558 return -EFAULT;
560 /* Strict protocol! */
561 if (*loff != 0)
562 return -ESPIPE;
563 switch (*c) {
564 case '/': /* flush table */
565 spin_lock_bh(&recent_lock);
566 recent_table_flush(t);
567 spin_unlock_bh(&recent_lock);
568 return size;
569 case '-': /* remove address */
570 add = false;
571 break;
572 case '+': /* add address */
573 add = true;
574 break;
575 default:
576 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
577 return -EINVAL;
580 ++c;
581 --size;
582 if (strnchr(c, size, ':') != NULL) {
583 family = NFPROTO_IPV6;
584 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
585 } else {
586 family = NFPROTO_IPV4;
587 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
590 if (!succ) {
591 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
592 "to procfs\n");
593 return -EINVAL;
596 spin_lock_bh(&recent_lock);
597 e = recent_entry_lookup(t, &addr, family, 0);
598 if (e == NULL) {
599 if (add)
600 recent_entry_init(t, &addr, family, 0);
601 } else {
602 if (add)
603 recent_entry_update(t, e);
604 else
605 recent_entry_remove(t, e);
607 spin_unlock_bh(&recent_lock);
608 /* Note we removed one above */
609 *loff += size + 1;
610 return size + 1;
613 static const struct file_operations recent_mt_fops = {
614 .open = recent_seq_open,
615 .read = seq_read,
616 .write = recent_mt_proc_write,
617 .release = seq_release_private,
618 .owner = THIS_MODULE,
620 #endif /* CONFIG_PROC_FS */
622 static struct xt_match recent_mt_reg[] __read_mostly = {
624 .name = "recent",
625 .revision = 0,
626 .family = NFPROTO_IPV4,
627 .match = recent_mt,
628 .matchsize = sizeof(struct xt_recent_mtinfo),
629 .checkentry = recent_mt_check,
630 .destroy = recent_mt_destroy,
631 .me = THIS_MODULE,
634 .name = "recent",
635 .revision = 0,
636 .family = NFPROTO_IPV6,
637 .match = recent_mt,
638 .matchsize = sizeof(struct xt_recent_mtinfo),
639 .checkentry = recent_mt_check,
640 .destroy = recent_mt_destroy,
641 .me = THIS_MODULE,
645 static int __init recent_mt_init(void)
647 int err;
649 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
650 return -EINVAL;
651 ip_list_hash_size = 1 << fls(ip_list_tot);
653 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
654 #ifdef CONFIG_PROC_FS
655 if (err)
656 return err;
657 recent_proc_dir = proc_mkdir("xt_recent", init_net.proc_net);
658 if (recent_proc_dir == NULL) {
659 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
660 err = -ENOMEM;
662 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
663 if (err < 0)
664 return err;
665 proc_old_dir = proc_mkdir("ipt_recent", init_net.proc_net);
666 if (proc_old_dir == NULL) {
667 remove_proc_entry("xt_recent", init_net.proc_net);
668 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
669 err = -ENOMEM;
671 #endif
672 #endif
673 return err;
676 static void __exit recent_mt_exit(void)
678 BUG_ON(!list_empty(&tables));
679 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
680 #ifdef CONFIG_PROC_FS
681 #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
682 remove_proc_entry("ipt_recent", init_net.proc_net);
683 #endif
684 remove_proc_entry("xt_recent", init_net.proc_net);
685 #endif
688 module_init(recent_mt_init);
689 module_exit(recent_mt_exit);