netfilter: xt_recent: remove old proc directory
[pandora-kernel.git] / net / netfilter / xt_recent.c
1 /*
2  * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
3  * Copyright © CC Computer Consultants GmbH, 2007 - 2008
4  *
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.
8  *
9  * This is a replacement of the old ipt_recent module, which carried the
10  * following copyright notice:
11  *
12  * Author: Stephen Frost <sfrost@snowman.net>
13  * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
14  */
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>
31 #include <net/netns/generic.h>
32
33 #include <linux/netfilter/x_tables.h>
34 #include <linux/netfilter/xt_recent.h>
35
36 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
37 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
38 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("ipt_recent");
41 MODULE_ALIAS("ip6t_recent");
42
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");
61
62 struct recent_entry {
63         struct list_head        list;
64         struct list_head        lru_list;
65         union nf_inet_addr      addr;
66         u_int16_t               family;
67         u_int8_t                ttl;
68         u_int8_t                index;
69         u_int16_t               nstamps;
70         unsigned long           stamps[0];
71 };
72
73 struct recent_table {
74         struct list_head        list;
75         char                    name[XT_RECENT_NAME_LEN];
76         unsigned int            refcnt;
77         unsigned int            entries;
78         struct list_head        lru_list;
79         struct list_head        iphash[0];
80 };
81
82 struct recent_net {
83         struct list_head        tables;
84 #ifdef CONFIG_PROC_FS
85         struct proc_dir_entry   *xt_recent;
86 #endif
87 };
88
89 static int recent_net_id;
90 static inline struct recent_net *recent_pernet(struct net *net)
91 {
92         return net_generic(net, recent_net_id);
93 }
94
95 static DEFINE_SPINLOCK(recent_lock);
96 static DEFINE_MUTEX(recent_mutex);
97
98 #ifdef CONFIG_PROC_FS
99 static const struct file_operations recent_old_fops, recent_mt_fops;
100 #endif
101
102 static u_int32_t hash_rnd __read_mostly;
103 static bool hash_rnd_inited __read_mostly;
104
105 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
106 {
107         return jhash_1word((__force u32)addr->ip, hash_rnd) &
108                (ip_list_hash_size - 1);
109 }
110
111 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
112 {
113         return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
114                (ip_list_hash_size - 1);
115 }
116
117 static struct recent_entry *
118 recent_entry_lookup(const struct recent_table *table,
119                     const union nf_inet_addr *addrp, u_int16_t family,
120                     u_int8_t ttl)
121 {
122         struct recent_entry *e;
123         unsigned int h;
124
125         if (family == NFPROTO_IPV4)
126                 h = recent_entry_hash4(addrp);
127         else
128                 h = recent_entry_hash6(addrp);
129
130         list_for_each_entry(e, &table->iphash[h], list)
131                 if (e->family == family &&
132                     memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
133                     (ttl == e->ttl || ttl == 0 || e->ttl == 0))
134                         return e;
135         return NULL;
136 }
137
138 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
139 {
140         list_del(&e->list);
141         list_del(&e->lru_list);
142         kfree(e);
143         t->entries--;
144 }
145
146 static struct recent_entry *
147 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
148                   u_int16_t family, u_int8_t ttl)
149 {
150         struct recent_entry *e;
151
152         if (t->entries >= ip_list_tot) {
153                 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
154                 recent_entry_remove(t, e);
155         }
156         e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
157                     GFP_ATOMIC);
158         if (e == NULL)
159                 return NULL;
160         memcpy(&e->addr, addr, sizeof(e->addr));
161         e->ttl       = ttl;
162         e->stamps[0] = jiffies;
163         e->nstamps   = 1;
164         e->index     = 1;
165         e->family    = family;
166         if (family == NFPROTO_IPV4)
167                 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
168         else
169                 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
170         list_add_tail(&e->lru_list, &t->lru_list);
171         t->entries++;
172         return e;
173 }
174
175 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
176 {
177         e->index %= ip_pkt_list_tot;
178         e->stamps[e->index++] = jiffies;
179         if (e->index > e->nstamps)
180                 e->nstamps = e->index;
181         list_move_tail(&e->lru_list, &t->lru_list);
182 }
183
184 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
185                                                 const char *name)
186 {
187         struct recent_table *t;
188
189         list_for_each_entry(t, &recent_net->tables, list)
190                 if (!strcmp(t->name, name))
191                         return t;
192         return NULL;
193 }
194
195 static void recent_table_flush(struct recent_table *t)
196 {
197         struct recent_entry *e, *next;
198         unsigned int i;
199
200         for (i = 0; i < ip_list_hash_size; i++)
201                 list_for_each_entry_safe(e, next, &t->iphash[i], list)
202                         recent_entry_remove(t, e);
203 }
204
205 static bool
206 recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
207 {
208         struct net *net = dev_net(par->in ? par->in : par->out);
209         struct recent_net *recent_net = recent_pernet(net);
210         const struct xt_recent_mtinfo *info = par->matchinfo;
211         struct recent_table *t;
212         struct recent_entry *e;
213         union nf_inet_addr addr = {};
214         u_int8_t ttl;
215         bool ret = info->invert;
216
217         if (par->match->family == NFPROTO_IPV4) {
218                 const struct iphdr *iph = ip_hdr(skb);
219
220                 if (info->side == XT_RECENT_DEST)
221                         addr.ip = iph->daddr;
222                 else
223                         addr.ip = iph->saddr;
224
225                 ttl = iph->ttl;
226         } else {
227                 const struct ipv6hdr *iph = ipv6_hdr(skb);
228
229                 if (info->side == XT_RECENT_DEST)
230                         memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
231                 else
232                         memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
233
234                 ttl = iph->hop_limit;
235         }
236
237         /* use TTL as seen before forwarding */
238         if (par->out != NULL && skb->sk == NULL)
239                 ttl++;
240
241         spin_lock_bh(&recent_lock);
242         t = recent_table_lookup(recent_net, info->name);
243         e = recent_entry_lookup(t, &addr, par->match->family,
244                                 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
245         if (e == NULL) {
246                 if (!(info->check_set & XT_RECENT_SET))
247                         goto out;
248                 e = recent_entry_init(t, &addr, par->match->family, ttl);
249                 if (e == NULL)
250                         *par->hotdrop = true;
251                 ret = !ret;
252                 goto out;
253         }
254
255         if (info->check_set & XT_RECENT_SET)
256                 ret = !ret;
257         else if (info->check_set & XT_RECENT_REMOVE) {
258                 recent_entry_remove(t, e);
259                 ret = !ret;
260         } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
261                 unsigned long time = jiffies - info->seconds * HZ;
262                 unsigned int i, hits = 0;
263
264                 for (i = 0; i < e->nstamps; i++) {
265                         if (info->seconds && time_after(time, e->stamps[i]))
266                                 continue;
267                         if (info->hit_count && ++hits >= info->hit_count) {
268                                 ret = !ret;
269                                 break;
270                         }
271                 }
272         }
273
274         if (info->check_set & XT_RECENT_SET ||
275             (info->check_set & XT_RECENT_UPDATE && ret)) {
276                 recent_entry_update(t, e);
277                 e->ttl = ttl;
278         }
279 out:
280         spin_unlock_bh(&recent_lock);
281         return ret;
282 }
283
284 static bool recent_mt_check(const struct xt_mtchk_param *par)
285 {
286         struct recent_net *recent_net = recent_pernet(par->net);
287         const struct xt_recent_mtinfo *info = par->matchinfo;
288         struct recent_table *t;
289 #ifdef CONFIG_PROC_FS
290         struct proc_dir_entry *pde;
291 #endif
292         unsigned i;
293         bool ret = false;
294
295         if (unlikely(!hash_rnd_inited)) {
296                 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
297                 hash_rnd_inited = true;
298         }
299         if (hweight8(info->check_set &
300                      (XT_RECENT_SET | XT_RECENT_REMOVE |
301                       XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
302                 return false;
303         if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
304             (info->seconds || info->hit_count))
305                 return false;
306         if (info->hit_count > ip_pkt_list_tot) {
307                 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
308                         "packets to be remembered (%u)\n",
309                         info->hit_count, ip_pkt_list_tot);
310                 return false;
311         }
312         if (info->name[0] == '\0' ||
313             strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
314                 return false;
315
316         mutex_lock(&recent_mutex);
317         t = recent_table_lookup(recent_net, info->name);
318         if (t != NULL) {
319                 t->refcnt++;
320                 ret = true;
321                 goto out;
322         }
323
324         t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
325                     GFP_KERNEL);
326         if (t == NULL)
327                 goto out;
328         t->refcnt = 1;
329         strcpy(t->name, info->name);
330         INIT_LIST_HEAD(&t->lru_list);
331         for (i = 0; i < ip_list_hash_size; i++)
332                 INIT_LIST_HEAD(&t->iphash[i]);
333 #ifdef CONFIG_PROC_FS
334         pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
335                   &recent_mt_fops, t);
336         if (pde == NULL) {
337                 kfree(t);
338                 goto out;
339         }
340         pde->uid = ip_list_uid;
341         pde->gid = ip_list_gid;
342 #endif
343         spin_lock_bh(&recent_lock);
344         list_add_tail(&t->list, &recent_net->tables);
345         spin_unlock_bh(&recent_lock);
346         ret = true;
347 out:
348         mutex_unlock(&recent_mutex);
349         return ret;
350 }
351
352 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
353 {
354         struct recent_net *recent_net = recent_pernet(par->net);
355         const struct xt_recent_mtinfo *info = par->matchinfo;
356         struct recent_table *t;
357
358         mutex_lock(&recent_mutex);
359         t = recent_table_lookup(recent_net, info->name);
360         if (--t->refcnt == 0) {
361                 spin_lock_bh(&recent_lock);
362                 list_del(&t->list);
363                 spin_unlock_bh(&recent_lock);
364 #ifdef CONFIG_PROC_FS
365                 remove_proc_entry(t->name, recent_net->xt_recent);
366 #endif
367                 recent_table_flush(t);
368                 kfree(t);
369         }
370         mutex_unlock(&recent_mutex);
371 }
372
373 #ifdef CONFIG_PROC_FS
374 struct recent_iter_state {
375         const struct recent_table *table;
376         unsigned int            bucket;
377 };
378
379 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
380         __acquires(recent_lock)
381 {
382         struct recent_iter_state *st = seq->private;
383         const struct recent_table *t = st->table;
384         struct recent_entry *e;
385         loff_t p = *pos;
386
387         spin_lock_bh(&recent_lock);
388
389         for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
390                 list_for_each_entry(e, &t->iphash[st->bucket], list)
391                         if (p-- == 0)
392                                 return e;
393         return NULL;
394 }
395
396 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
397 {
398         struct recent_iter_state *st = seq->private;
399         const struct recent_table *t = st->table;
400         const struct recent_entry *e = v;
401         const struct list_head *head = e->list.next;
402
403         while (head == &t->iphash[st->bucket]) {
404                 if (++st->bucket >= ip_list_hash_size)
405                         return NULL;
406                 head = t->iphash[st->bucket].next;
407         }
408         (*pos)++;
409         return list_entry(head, struct recent_entry, list);
410 }
411
412 static void recent_seq_stop(struct seq_file *s, void *v)
413         __releases(recent_lock)
414 {
415         spin_unlock_bh(&recent_lock);
416 }
417
418 static int recent_seq_show(struct seq_file *seq, void *v)
419 {
420         const struct recent_entry *e = v;
421         unsigned int i;
422
423         i = (e->index - 1) % ip_pkt_list_tot;
424         if (e->family == NFPROTO_IPV4)
425                 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
426                            &e->addr.ip, e->ttl, e->stamps[i], e->index);
427         else
428                 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
429                            &e->addr.in6, e->ttl, e->stamps[i], e->index);
430         for (i = 0; i < e->nstamps; i++)
431                 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
432         seq_printf(seq, "\n");
433         return 0;
434 }
435
436 static const struct seq_operations recent_seq_ops = {
437         .start          = recent_seq_start,
438         .next           = recent_seq_next,
439         .stop           = recent_seq_stop,
440         .show           = recent_seq_show,
441 };
442
443 static int recent_seq_open(struct inode *inode, struct file *file)
444 {
445         struct proc_dir_entry *pde = PDE(inode);
446         struct recent_iter_state *st;
447
448         st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
449         if (st == NULL)
450                 return -ENOMEM;
451
452         st->table    = pde->data;
453         return 0;
454 }
455
456 static ssize_t
457 recent_mt_proc_write(struct file *file, const char __user *input,
458                      size_t size, loff_t *loff)
459 {
460         const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
461         struct recent_table *t = pde->data;
462         struct recent_entry *e;
463         char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
464         const char *c = buf;
465         union nf_inet_addr addr = {};
466         u_int16_t family;
467         bool add, succ;
468
469         if (size == 0)
470                 return 0;
471         if (size > sizeof(buf))
472                 size = sizeof(buf);
473         if (copy_from_user(buf, input, size) != 0)
474                 return -EFAULT;
475
476         /* Strict protocol! */
477         if (*loff != 0)
478                 return -ESPIPE;
479         switch (*c) {
480         case '/': /* flush table */
481                 spin_lock_bh(&recent_lock);
482                 recent_table_flush(t);
483                 spin_unlock_bh(&recent_lock);
484                 return size;
485         case '-': /* remove address */
486                 add = false;
487                 break;
488         case '+': /* add address */
489                 add = true;
490                 break;
491         default:
492                 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
493                 return -EINVAL;
494         }
495
496         ++c;
497         --size;
498         if (strnchr(c, size, ':') != NULL) {
499                 family = NFPROTO_IPV6;
500                 succ   = in6_pton(c, size, (void *)&addr, '\n', NULL);
501         } else {
502                 family = NFPROTO_IPV4;
503                 succ   = in4_pton(c, size, (void *)&addr, '\n', NULL);
504         }
505
506         if (!succ) {
507                 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
508                        "to procfs\n");
509                 return -EINVAL;
510         }
511
512         spin_lock_bh(&recent_lock);
513         e = recent_entry_lookup(t, &addr, family, 0);
514         if (e == NULL) {
515                 if (add)
516                         recent_entry_init(t, &addr, family, 0);
517         } else {
518                 if (add)
519                         recent_entry_update(t, e);
520                 else
521                         recent_entry_remove(t, e);
522         }
523         spin_unlock_bh(&recent_lock);
524         /* Note we removed one above */
525         *loff += size + 1;
526         return size + 1;
527 }
528
529 static const struct file_operations recent_mt_fops = {
530         .open    = recent_seq_open,
531         .read    = seq_read,
532         .write   = recent_mt_proc_write,
533         .release = seq_release_private,
534         .owner   = THIS_MODULE,
535 };
536
537 static int __net_init recent_proc_net_init(struct net *net)
538 {
539         struct recent_net *recent_net = recent_pernet(net);
540
541         recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
542         if (!recent_net->xt_recent)
543                 return -ENOMEM;
544         return 0;
545 }
546
547 static void __net_exit recent_proc_net_exit(struct net *net)
548 {
549         proc_net_remove(net, "xt_recent");
550 }
551 #else
552 static inline int recent_proc_net_init(struct net *net)
553 {
554         return 0;
555 }
556
557 static inline void recent_proc_net_exit(struct net *net)
558 {
559 }
560 #endif /* CONFIG_PROC_FS */
561
562 static int __net_init recent_net_init(struct net *net)
563 {
564         struct recent_net *recent_net = recent_pernet(net);
565
566         INIT_LIST_HEAD(&recent_net->tables);
567         return recent_proc_net_init(net);
568 }
569
570 static void __net_exit recent_net_exit(struct net *net)
571 {
572         struct recent_net *recent_net = recent_pernet(net);
573
574         BUG_ON(!list_empty(&recent_net->tables));
575         recent_proc_net_exit(net);
576 }
577
578 static struct pernet_operations recent_net_ops = {
579         .init   = recent_net_init,
580         .exit   = recent_net_exit,
581         .id     = &recent_net_id,
582         .size   = sizeof(struct recent_net),
583 };
584
585 static struct xt_match recent_mt_reg[] __read_mostly = {
586         {
587                 .name       = "recent",
588                 .revision   = 0,
589                 .family     = NFPROTO_IPV4,
590                 .match      = recent_mt,
591                 .matchsize  = sizeof(struct xt_recent_mtinfo),
592                 .checkentry = recent_mt_check,
593                 .destroy    = recent_mt_destroy,
594                 .me         = THIS_MODULE,
595         },
596         {
597                 .name       = "recent",
598                 .revision   = 0,
599                 .family     = NFPROTO_IPV6,
600                 .match      = recent_mt,
601                 .matchsize  = sizeof(struct xt_recent_mtinfo),
602                 .checkentry = recent_mt_check,
603                 .destroy    = recent_mt_destroy,
604                 .me         = THIS_MODULE,
605         },
606 };
607
608 static int __init recent_mt_init(void)
609 {
610         int err;
611
612         if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
613                 return -EINVAL;
614         ip_list_hash_size = 1 << fls(ip_list_tot);
615
616         err = register_pernet_subsys(&recent_net_ops);
617         if (err)
618                 return err;
619         err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
620         if (err)
621                 unregister_pernet_subsys(&recent_net_ops);
622         return err;
623 }
624
625 static void __exit recent_mt_exit(void)
626 {
627         xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
628         unregister_pernet_subsys(&recent_net_ops);
629 }
630
631 module_init(recent_mt_init);
632 module_exit(recent_mt_exit);