1 /* ip_conntrack proc compat - based on ip_conntrack_standalone.c
3 * (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/percpu.h>
15 #include <linux/security.h>
16 #include <net/net_namespace.h>
18 #include <linux/netfilter.h>
19 #include <net/netfilter/nf_conntrack_core.h>
20 #include <net/netfilter/nf_conntrack_l3proto.h>
21 #include <net/netfilter/nf_conntrack_l4proto.h>
22 #include <net/netfilter/nf_conntrack_expect.h>
23 #include <net/netfilter/nf_conntrack_acct.h>
24 #include <linux/rculist_nulls.h>
25 #include <linux/export.h>
27 struct ct_iter_state {
28 struct seq_net_private p;
32 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
34 struct net *net = seq_file_net(seq);
35 struct ct_iter_state *st = seq->private;
36 struct hlist_nulls_node *n;
39 st->bucket < net->ct.htable_size;
42 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
49 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
50 struct hlist_nulls_node *head)
52 struct net *net = seq_file_net(seq);
53 struct ct_iter_state *st = seq->private;
55 head = rcu_dereference(hlist_nulls_next_rcu(head));
56 while (is_a_nulls(head)) {
57 if (likely(get_nulls_value(head) == st->bucket)) {
58 if (++st->bucket >= net->ct.htable_size)
61 head = rcu_dereference(
62 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
67 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
69 struct hlist_nulls_node *head = ct_get_first(seq);
72 while (pos && (head = ct_get_next(seq, head)))
74 return pos ? NULL : head;
77 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
81 return ct_get_idx(seq, *pos);
84 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
87 return ct_get_next(s, v);
90 static void ct_seq_stop(struct seq_file *s, void *v)
96 #ifdef CONFIG_NF_CONNTRACK_SECMARK
97 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
103 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
107 seq_printf(s, "secctx=%s ", secctx);
109 security_release_secctx(secctx, len);
112 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
117 static int ct_seq_show(struct seq_file *s, void *v)
119 struct nf_conntrack_tuple_hash *hash = v;
120 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
121 const struct nf_conntrack_l3proto *l3proto;
122 const struct nf_conntrack_l4proto *l4proto;
126 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
130 /* we only want to print DIR_ORIGINAL */
131 if (NF_CT_DIRECTION(hash))
133 if (nf_ct_l3num(ct) != AF_INET)
136 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
137 NF_CT_ASSERT(l3proto);
138 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
139 NF_CT_ASSERT(l4proto);
142 seq_printf(s, "%-8s %u %ld ",
143 l4proto->name, nf_ct_protonum(ct),
144 timer_pending(&ct->timeout)
145 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
147 if (l4proto->print_conntrack)
148 l4proto->print_conntrack(s, ct);
150 if (seq_has_overflowed(s))
153 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
156 if (seq_has_overflowed(s))
159 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
162 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
163 seq_printf(s, "[UNREPLIED] ");
165 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
168 if (seq_has_overflowed(s))
171 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
174 if (test_bit(IPS_ASSURED_BIT, &ct->status))
175 seq_printf(s, "[ASSURED] ");
177 #ifdef CONFIG_NF_CONNTRACK_MARK
178 seq_printf(s, "mark=%u ", ct->mark);
181 ct_show_secctx(s, ct);
183 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
185 if (seq_has_overflowed(s))
194 static const struct seq_operations ct_seq_ops = {
195 .start = ct_seq_start,
201 static int ct_open(struct inode *inode, struct file *file)
203 return seq_open_net(inode, file, &ct_seq_ops,
204 sizeof(struct ct_iter_state));
207 static const struct file_operations ct_file_ops = {
208 .owner = THIS_MODULE,
212 .release = seq_release_net,
216 struct ct_expect_iter_state {
217 struct seq_net_private p;
221 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
223 struct net *net = seq_file_net(seq);
224 struct ct_expect_iter_state *st = seq->private;
225 struct hlist_node *n;
227 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
229 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
236 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
237 struct hlist_node *head)
239 struct net *net = seq_file_net(seq);
240 struct ct_expect_iter_state *st = seq->private;
242 head = rcu_dereference(hlist_next_rcu(head));
243 while (head == NULL) {
244 if (++st->bucket >= nf_ct_expect_hsize)
246 head = rcu_dereference(
247 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
252 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
254 struct hlist_node *head = ct_expect_get_first(seq);
257 while (pos && (head = ct_expect_get_next(seq, head)))
259 return pos ? NULL : head;
262 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
266 return ct_expect_get_idx(seq, *pos);
269 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
272 return ct_expect_get_next(seq, v);
275 static void exp_seq_stop(struct seq_file *seq, void *v)
281 static int exp_seq_show(struct seq_file *s, void *v)
283 struct nf_conntrack_expect *exp;
284 const struct hlist_node *n = v;
286 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
288 if (exp->tuple.src.l3num != AF_INET)
291 if (exp->timeout.function)
292 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
293 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
297 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
299 print_tuple(s, &exp->tuple,
300 __nf_ct_l3proto_find(exp->tuple.src.l3num),
301 __nf_ct_l4proto_find(exp->tuple.src.l3num,
302 exp->tuple.dst.protonum));
308 static const struct seq_operations exp_seq_ops = {
309 .start = exp_seq_start,
310 .next = exp_seq_next,
311 .stop = exp_seq_stop,
315 static int exp_open(struct inode *inode, struct file *file)
317 return seq_open_net(inode, file, &exp_seq_ops,
318 sizeof(struct ct_expect_iter_state));
321 static const struct file_operations ip_exp_file_ops = {
322 .owner = THIS_MODULE,
326 .release = seq_release_net,
329 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
331 struct net *net = seq_file_net(seq);
335 return SEQ_START_TOKEN;
337 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
338 if (!cpu_possible(cpu))
341 return per_cpu_ptr(net->ct.stat, cpu);
347 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
349 struct net *net = seq_file_net(seq);
352 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
353 if (!cpu_possible(cpu))
356 return per_cpu_ptr(net->ct.stat, cpu);
362 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
366 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
368 struct net *net = seq_file_net(seq);
369 unsigned int nr_conntracks = atomic_read(&net->ct.count);
370 const struct ip_conntrack_stat *st = v;
372 if (v == SEQ_START_TOKEN) {
373 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");
377 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
378 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
401 static const struct seq_operations ct_cpu_seq_ops = {
402 .start = ct_cpu_seq_start,
403 .next = ct_cpu_seq_next,
404 .stop = ct_cpu_seq_stop,
405 .show = ct_cpu_seq_show,
408 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
410 return seq_open_net(inode, file, &ct_cpu_seq_ops,
411 sizeof(struct seq_net_private));
414 static const struct file_operations ct_cpu_seq_fops = {
415 .owner = THIS_MODULE,
416 .open = ct_cpu_seq_open,
419 .release = seq_release_net,
422 static int __net_init ip_conntrack_net_init(struct net *net)
424 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
426 proc = proc_create("ip_conntrack", 0440, net->proc_net, &ct_file_ops);
430 proc_exp = proc_create("ip_conntrack_expect", 0440, net->proc_net,
435 proc_stat = proc_create("ip_conntrack", S_IRUGO,
436 net->proc_net_stat, &ct_cpu_seq_fops);
442 remove_proc_entry("ip_conntrack_expect", net->proc_net);
444 remove_proc_entry("ip_conntrack", net->proc_net);
449 static void __net_exit ip_conntrack_net_exit(struct net *net)
451 remove_proc_entry("ip_conntrack", net->proc_net_stat);
452 remove_proc_entry("ip_conntrack_expect", net->proc_net);
453 remove_proc_entry("ip_conntrack", net->proc_net);
456 static struct pernet_operations ip_conntrack_net_ops = {
457 .init = ip_conntrack_net_init,
458 .exit = ip_conntrack_net_exit,
461 int __init nf_conntrack_ipv4_compat_init(void)
463 return register_pernet_subsys(&ip_conntrack_net_ops);
466 void __exit nf_conntrack_ipv4_compat_fini(void)
468 unregister_pernet_subsys(&ip_conntrack_net_ops);