netfilter: evict stale entries when user reads /proc/net/nf_conntrack
[linux-2.6/btrfs-unstable.git] / net / netfilter / nf_conntrack_standalone.c
blob5f446cd9f3fd4ef5116741f0732f39775a4b65df
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
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 */
10 #include <linux/types.h>
11 #include <linux/netfilter.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/percpu.h>
18 #include <linux/netdevice.h>
19 #include <linux/security.h>
20 #include <net/net_namespace.h>
21 #ifdef CONFIG_SYSCTL
22 #include <linux/sysctl.h>
23 #endif
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_acct.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_timestamp.h>
34 #include <linux/rculist_nulls.h>
36 MODULE_LICENSE("GPL");
38 #ifdef CONFIG_NF_CONNTRACK_PROCFS
39 void
40 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
41 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
44 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
47 EXPORT_SYMBOL_GPL(print_tuple);
49 struct ct_iter_state {
50 struct seq_net_private p;
51 struct hlist_nulls_head *hash;
52 unsigned int htable_size;
53 unsigned int bucket;
54 u_int64_t time_now;
57 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
59 struct ct_iter_state *st = seq->private;
60 struct hlist_nulls_node *n;
62 for (st->bucket = 0;
63 st->bucket < st->htable_size;
64 st->bucket++) {
65 n = rcu_dereference(
66 hlist_nulls_first_rcu(&st->hash[st->bucket]));
67 if (!is_a_nulls(n))
68 return n;
70 return NULL;
73 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
74 struct hlist_nulls_node *head)
76 struct ct_iter_state *st = seq->private;
78 head = rcu_dereference(hlist_nulls_next_rcu(head));
79 while (is_a_nulls(head)) {
80 if (likely(get_nulls_value(head) == st->bucket)) {
81 if (++st->bucket >= st->htable_size)
82 return NULL;
84 head = rcu_dereference(
85 hlist_nulls_first_rcu(&st->hash[st->bucket]));
87 return head;
90 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
92 struct hlist_nulls_node *head = ct_get_first(seq);
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
100 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
101 __acquires(RCU)
103 struct ct_iter_state *st = seq->private;
105 st->time_now = ktime_get_real_ns();
106 rcu_read_lock();
108 nf_conntrack_get_ht(&st->hash, &st->htable_size);
109 return ct_get_idx(seq, *pos);
112 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
114 (*pos)++;
115 return ct_get_next(s, v);
118 static void ct_seq_stop(struct seq_file *s, void *v)
119 __releases(RCU)
121 rcu_read_unlock();
124 #ifdef CONFIG_NF_CONNTRACK_SECMARK
125 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
127 int ret;
128 u32 len;
129 char *secctx;
131 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
132 if (ret)
133 return;
135 seq_printf(s, "secctx=%s ", secctx);
137 security_release_secctx(secctx, len);
139 #else
140 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
143 #endif
145 #ifdef CONFIG_NF_CONNTRACK_ZONES
146 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
147 int dir)
149 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
151 if (zone->dir != dir)
152 return;
153 switch (zone->dir) {
154 case NF_CT_DEFAULT_ZONE_DIR:
155 seq_printf(s, "zone=%u ", zone->id);
156 break;
157 case NF_CT_ZONE_DIR_ORIG:
158 seq_printf(s, "zone-orig=%u ", zone->id);
159 break;
160 case NF_CT_ZONE_DIR_REPL:
161 seq_printf(s, "zone-reply=%u ", zone->id);
162 break;
163 default:
164 break;
167 #else
168 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
169 int dir)
172 #endif
174 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
175 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
177 struct ct_iter_state *st = s->private;
178 struct nf_conn_tstamp *tstamp;
179 s64 delta_time;
181 tstamp = nf_conn_tstamp_find(ct);
182 if (tstamp) {
183 delta_time = st->time_now - tstamp->start;
184 if (delta_time > 0)
185 delta_time = div_s64(delta_time, NSEC_PER_SEC);
186 else
187 delta_time = 0;
189 seq_printf(s, "delta-time=%llu ",
190 (unsigned long long)delta_time);
192 return;
194 #else
195 static inline void
196 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
199 #endif
201 /* return 0 on success, 1 in case of error */
202 static int ct_seq_show(struct seq_file *s, void *v)
204 struct nf_conntrack_tuple_hash *hash = v;
205 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
206 const struct nf_conntrack_l3proto *l3proto;
207 const struct nf_conntrack_l4proto *l4proto;
208 struct net *net = seq_file_net(s);
209 int ret = 0;
211 NF_CT_ASSERT(ct);
212 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
213 return 0;
215 if (nf_ct_should_gc(ct)) {
216 nf_ct_kill(ct);
217 goto release;
220 /* we only want to print DIR_ORIGINAL */
221 if (NF_CT_DIRECTION(hash))
222 goto release;
224 if (!net_eq(nf_ct_net(ct), net))
225 goto release;
227 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
228 NF_CT_ASSERT(l3proto);
229 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
230 NF_CT_ASSERT(l4proto);
232 ret = -ENOSPC;
233 seq_printf(s, "%-8s %u %-8s %u %ld ",
234 l3proto->name, nf_ct_l3num(ct),
235 l4proto->name, nf_ct_protonum(ct),
236 nf_ct_expires(ct) / HZ);
238 if (l4proto->print_conntrack)
239 l4proto->print_conntrack(s, ct);
241 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
242 l3proto, l4proto);
244 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
246 if (seq_has_overflowed(s))
247 goto release;
249 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
250 goto release;
252 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
253 seq_printf(s, "[UNREPLIED] ");
255 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
256 l3proto, l4proto);
258 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
260 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
261 goto release;
263 if (test_bit(IPS_ASSURED_BIT, &ct->status))
264 seq_printf(s, "[ASSURED] ");
266 if (seq_has_overflowed(s))
267 goto release;
269 #if defined(CONFIG_NF_CONNTRACK_MARK)
270 seq_printf(s, "mark=%u ", ct->mark);
271 #endif
273 ct_show_secctx(s, ct);
274 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
275 ct_show_delta_time(s, ct);
277 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
279 if (seq_has_overflowed(s))
280 goto release;
282 ret = 0;
283 release:
284 nf_ct_put(ct);
285 return ret;
288 static const struct seq_operations ct_seq_ops = {
289 .start = ct_seq_start,
290 .next = ct_seq_next,
291 .stop = ct_seq_stop,
292 .show = ct_seq_show
295 static int ct_open(struct inode *inode, struct file *file)
297 return seq_open_net(inode, file, &ct_seq_ops,
298 sizeof(struct ct_iter_state));
301 static const struct file_operations ct_file_ops = {
302 .owner = THIS_MODULE,
303 .open = ct_open,
304 .read = seq_read,
305 .llseek = seq_lseek,
306 .release = seq_release_net,
309 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
311 struct net *net = seq_file_net(seq);
312 int cpu;
314 if (*pos == 0)
315 return SEQ_START_TOKEN;
317 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
318 if (!cpu_possible(cpu))
319 continue;
320 *pos = cpu + 1;
321 return per_cpu_ptr(net->ct.stat, cpu);
324 return NULL;
327 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
329 struct net *net = seq_file_net(seq);
330 int cpu;
332 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
333 if (!cpu_possible(cpu))
334 continue;
335 *pos = cpu + 1;
336 return per_cpu_ptr(net->ct.stat, cpu);
339 return NULL;
342 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
346 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
348 struct net *net = seq_file_net(seq);
349 unsigned int nr_conntracks = atomic_read(&net->ct.count);
350 const struct ip_conntrack_stat *st = v;
352 if (v == SEQ_START_TOKEN) {
353 seq_printf(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n");
354 return 0;
357 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
358 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
359 nr_conntracks,
361 st->found,
363 st->invalid,
364 st->ignore,
367 st->insert,
368 st->insert_failed,
369 st->drop,
370 st->early_drop,
371 st->error,
373 st->expect_new,
374 st->expect_create,
375 st->expect_delete,
376 st->search_restart
378 return 0;
381 static const struct seq_operations ct_cpu_seq_ops = {
382 .start = ct_cpu_seq_start,
383 .next = ct_cpu_seq_next,
384 .stop = ct_cpu_seq_stop,
385 .show = ct_cpu_seq_show,
388 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
390 return seq_open_net(inode, file, &ct_cpu_seq_ops,
391 sizeof(struct seq_net_private));
394 static const struct file_operations ct_cpu_seq_fops = {
395 .owner = THIS_MODULE,
396 .open = ct_cpu_seq_open,
397 .read = seq_read,
398 .llseek = seq_lseek,
399 .release = seq_release_net,
402 static int nf_conntrack_standalone_init_proc(struct net *net)
404 struct proc_dir_entry *pde;
405 kuid_t root_uid;
406 kgid_t root_gid;
408 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
409 if (!pde)
410 goto out_nf_conntrack;
412 root_uid = make_kuid(net->user_ns, 0);
413 root_gid = make_kgid(net->user_ns, 0);
414 if (uid_valid(root_uid) && gid_valid(root_gid))
415 proc_set_user(pde, root_uid, root_gid);
417 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
418 &ct_cpu_seq_fops);
419 if (!pde)
420 goto out_stat_nf_conntrack;
421 return 0;
423 out_stat_nf_conntrack:
424 remove_proc_entry("nf_conntrack", net->proc_net);
425 out_nf_conntrack:
426 return -ENOMEM;
429 static void nf_conntrack_standalone_fini_proc(struct net *net)
431 remove_proc_entry("nf_conntrack", net->proc_net_stat);
432 remove_proc_entry("nf_conntrack", net->proc_net);
434 #else
435 static int nf_conntrack_standalone_init_proc(struct net *net)
437 return 0;
440 static void nf_conntrack_standalone_fini_proc(struct net *net)
443 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
445 /* Sysctl support */
447 #ifdef CONFIG_SYSCTL
448 /* Log invalid packets of a given protocol */
449 static int log_invalid_proto_min __read_mostly;
450 static int log_invalid_proto_max __read_mostly = 255;
452 /* size the user *wants to set */
453 static unsigned int nf_conntrack_htable_size_user __read_mostly;
455 static int
456 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
457 void __user *buffer, size_t *lenp, loff_t *ppos)
459 int ret;
461 ret = proc_dointvec(table, write, buffer, lenp, ppos);
462 if (ret < 0 || !write)
463 return ret;
465 /* update ret, we might not be able to satisfy request */
466 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
468 /* update it to the actual value used by conntrack */
469 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
470 return ret;
473 static struct ctl_table_header *nf_ct_netfilter_header;
475 static struct ctl_table nf_ct_sysctl_table[] = {
477 .procname = "nf_conntrack_max",
478 .data = &nf_conntrack_max,
479 .maxlen = sizeof(int),
480 .mode = 0644,
481 .proc_handler = proc_dointvec,
484 .procname = "nf_conntrack_count",
485 .data = &init_net.ct.count,
486 .maxlen = sizeof(int),
487 .mode = 0444,
488 .proc_handler = proc_dointvec,
491 .procname = "nf_conntrack_buckets",
492 .data = &nf_conntrack_htable_size_user,
493 .maxlen = sizeof(unsigned int),
494 .mode = 0644,
495 .proc_handler = nf_conntrack_hash_sysctl,
498 .procname = "nf_conntrack_checksum",
499 .data = &init_net.ct.sysctl_checksum,
500 .maxlen = sizeof(unsigned int),
501 .mode = 0644,
502 .proc_handler = proc_dointvec,
505 .procname = "nf_conntrack_log_invalid",
506 .data = &init_net.ct.sysctl_log_invalid,
507 .maxlen = sizeof(unsigned int),
508 .mode = 0644,
509 .proc_handler = proc_dointvec_minmax,
510 .extra1 = &log_invalid_proto_min,
511 .extra2 = &log_invalid_proto_max,
514 .procname = "nf_conntrack_expect_max",
515 .data = &nf_ct_expect_max,
516 .maxlen = sizeof(int),
517 .mode = 0644,
518 .proc_handler = proc_dointvec,
523 static struct ctl_table nf_ct_netfilter_table[] = {
525 .procname = "nf_conntrack_max",
526 .data = &nf_conntrack_max,
527 .maxlen = sizeof(int),
528 .mode = 0644,
529 .proc_handler = proc_dointvec,
534 static int nf_conntrack_standalone_init_sysctl(struct net *net)
536 struct ctl_table *table;
538 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
539 GFP_KERNEL);
540 if (!table)
541 goto out_kmemdup;
543 table[1].data = &net->ct.count;
544 table[3].data = &net->ct.sysctl_checksum;
545 table[4].data = &net->ct.sysctl_log_invalid;
547 /* Don't export sysctls to unprivileged users */
548 if (net->user_ns != &init_user_ns)
549 table[0].procname = NULL;
551 if (!net_eq(&init_net, net))
552 table[2].mode = 0444;
554 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
555 if (!net->ct.sysctl_header)
556 goto out_unregister_netfilter;
558 return 0;
560 out_unregister_netfilter:
561 kfree(table);
562 out_kmemdup:
563 return -ENOMEM;
566 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
568 struct ctl_table *table;
570 table = net->ct.sysctl_header->ctl_table_arg;
571 unregister_net_sysctl_table(net->ct.sysctl_header);
572 kfree(table);
574 #else
575 static int nf_conntrack_standalone_init_sysctl(struct net *net)
577 return 0;
580 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
583 #endif /* CONFIG_SYSCTL */
585 static int nf_conntrack_pernet_init(struct net *net)
587 int ret;
589 ret = nf_conntrack_init_net(net);
590 if (ret < 0)
591 goto out_init;
593 ret = nf_conntrack_standalone_init_proc(net);
594 if (ret < 0)
595 goto out_proc;
597 net->ct.sysctl_checksum = 1;
598 net->ct.sysctl_log_invalid = 0;
599 ret = nf_conntrack_standalone_init_sysctl(net);
600 if (ret < 0)
601 goto out_sysctl;
603 return 0;
605 out_sysctl:
606 nf_conntrack_standalone_fini_proc(net);
607 out_proc:
608 nf_conntrack_cleanup_net(net);
609 out_init:
610 return ret;
613 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
615 struct net *net;
617 list_for_each_entry(net, net_exit_list, exit_list) {
618 nf_conntrack_standalone_fini_sysctl(net);
619 nf_conntrack_standalone_fini_proc(net);
621 nf_conntrack_cleanup_net_list(net_exit_list);
624 static struct pernet_operations nf_conntrack_net_ops = {
625 .init = nf_conntrack_pernet_init,
626 .exit_batch = nf_conntrack_pernet_exit,
629 static int __init nf_conntrack_standalone_init(void)
631 int ret = nf_conntrack_init_start();
632 if (ret < 0)
633 goto out_start;
635 #ifdef CONFIG_SYSCTL
636 nf_ct_netfilter_header =
637 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
638 if (!nf_ct_netfilter_header) {
639 pr_err("nf_conntrack: can't register to sysctl.\n");
640 ret = -ENOMEM;
641 goto out_sysctl;
644 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
645 #endif
647 ret = register_pernet_subsys(&nf_conntrack_net_ops);
648 if (ret < 0)
649 goto out_pernet;
651 nf_conntrack_init_end();
652 return 0;
654 out_pernet:
655 #ifdef CONFIG_SYSCTL
656 unregister_net_sysctl_table(nf_ct_netfilter_header);
657 out_sysctl:
658 #endif
659 nf_conntrack_cleanup_end();
660 out_start:
661 return ret;
664 static void __exit nf_conntrack_standalone_fini(void)
666 nf_conntrack_cleanup_start();
667 unregister_pernet_subsys(&nf_conntrack_net_ops);
668 #ifdef CONFIG_SYSCTL
669 unregister_net_sysctl_table(nf_ct_netfilter_header);
670 #endif
671 nf_conntrack_cleanup_end();
674 module_init(nf_conntrack_standalone_init);
675 module_exit(nf_conntrack_standalone_fini);
677 /* Some modules need us, but don't depend directly on any symbol.
678 They should call this. */
679 void need_conntrack(void)
682 EXPORT_SYMBOL_GPL(need_conntrack);