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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/types.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/percpu.h>
14 #include <net/net_namespace.h>
16 #include <linux/netfilter.h>
17 #include <net/netfilter/nf_conntrack_core.h>
18 #include <net/netfilter/nf_conntrack_l3proto.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <net/netfilter/nf_conntrack_acct.h>
23 struct ct_iter_state {
24 struct seq_net_private p;
28 static struct hlist_node *ct_get_first(struct seq_file *seq)
30 struct net *net = seq_file_net(seq);
31 struct ct_iter_state *st = seq->private;
35 st->bucket < nf_conntrack_htable_size;
37 n = rcu_dereference(net->ct.hash[st->bucket].first);
44 static struct hlist_node *ct_get_next(struct seq_file *seq,
45 struct hlist_node *head)
47 struct net *net = seq_file_net(seq);
48 struct ct_iter_state *st = seq->private;
50 head = rcu_dereference(head->next);
51 while (head == NULL) {
52 if (++st->bucket >= nf_conntrack_htable_size)
54 head = rcu_dereference(net->ct.hash[st->bucket].first);
59 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
61 struct hlist_node *head = ct_get_first(seq);
64 while (pos && (head = ct_get_next(seq, head)))
66 return pos ? NULL : head;
69 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
73 return ct_get_idx(seq, *pos);
76 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
79 return ct_get_next(s, v);
82 static void ct_seq_stop(struct seq_file *s, void *v)
88 static int ct_seq_show(struct seq_file *s, void *v)
90 const struct nf_conntrack_tuple_hash *hash = v;
91 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
92 const struct nf_conntrack_l3proto *l3proto;
93 const struct nf_conntrack_l4proto *l4proto;
97 /* we only want to print DIR_ORIGINAL */
98 if (NF_CT_DIRECTION(hash))
100 if (nf_ct_l3num(ct) != AF_INET)
103 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
104 NF_CT_ASSERT(l3proto);
105 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
106 NF_CT_ASSERT(l4proto);
108 if (seq_printf(s, "%-8s %u %ld ",
109 l4proto->name, nf_ct_protonum(ct),
110 timer_pending(&ct->timeout)
111 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
114 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
117 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
121 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
124 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
125 if (seq_printf(s, "[UNREPLIED] "))
128 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
132 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
135 if (test_bit(IPS_ASSURED_BIT, &ct->status))
136 if (seq_printf(s, "[ASSURED] "))
139 #ifdef CONFIG_NF_CONNTRACK_MARK
140 if (seq_printf(s, "mark=%u ", ct->mark))
144 #ifdef CONFIG_NF_CONNTRACK_SECMARK
145 if (seq_printf(s, "secmark=%u ", ct->secmark))
149 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
155 static const struct seq_operations ct_seq_ops = {
156 .start = ct_seq_start,
162 static int ct_open(struct inode *inode, struct file *file)
164 return seq_open_net(inode, file, &ct_seq_ops,
165 sizeof(struct ct_iter_state));
168 static const struct file_operations ct_file_ops = {
169 .owner = THIS_MODULE,
173 .release = seq_release_net,
177 struct ct_expect_iter_state {
178 struct seq_net_private p;
182 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
184 struct net *net = seq_file_net(seq);
185 struct ct_expect_iter_state *st = seq->private;
186 struct hlist_node *n;
188 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
189 n = rcu_dereference(net->ct.expect_hash[st->bucket].first);
196 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
197 struct hlist_node *head)
199 struct net *net = seq_file_net(seq);
200 struct ct_expect_iter_state *st = seq->private;
202 head = rcu_dereference(head->next);
203 while (head == NULL) {
204 if (++st->bucket >= nf_ct_expect_hsize)
206 head = rcu_dereference(net->ct.expect_hash[st->bucket].first);
211 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
213 struct hlist_node *head = ct_expect_get_first(seq);
216 while (pos && (head = ct_expect_get_next(seq, head)))
218 return pos ? NULL : head;
221 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
225 return ct_expect_get_idx(seq, *pos);
228 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
231 return ct_expect_get_next(seq, v);
234 static void exp_seq_stop(struct seq_file *seq, void *v)
240 static int exp_seq_show(struct seq_file *s, void *v)
242 struct nf_conntrack_expect *exp;
243 const struct hlist_node *n = v;
245 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
247 if (exp->tuple.src.l3num != AF_INET)
250 if (exp->timeout.function)
251 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
252 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
256 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
258 print_tuple(s, &exp->tuple,
259 __nf_ct_l3proto_find(exp->tuple.src.l3num),
260 __nf_ct_l4proto_find(exp->tuple.src.l3num,
261 exp->tuple.dst.protonum));
262 return seq_putc(s, '\n');
265 static const struct seq_operations exp_seq_ops = {
266 .start = exp_seq_start,
267 .next = exp_seq_next,
268 .stop = exp_seq_stop,
272 static int exp_open(struct inode *inode, struct file *file)
274 return seq_open_net(inode, file, &exp_seq_ops,
275 sizeof(struct ct_expect_iter_state));
278 static const struct file_operations ip_exp_file_ops = {
279 .owner = THIS_MODULE,
283 .release = seq_release_net,
286 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
288 struct net *net = seq_file_net(seq);
292 return SEQ_START_TOKEN;
294 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
295 if (!cpu_possible(cpu))
298 return per_cpu_ptr(net->ct.stat, cpu);
304 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
306 struct net *net = seq_file_net(seq);
309 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
310 if (!cpu_possible(cpu))
313 return per_cpu_ptr(net->ct.stat, cpu);
319 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
323 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
325 struct net *net = seq_file_net(seq);
326 unsigned int nr_conntracks = atomic_read(&net->ct.count);
327 const struct ip_conntrack_stat *st = v;
329 if (v == SEQ_START_TOKEN) {
330 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\n");
334 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
335 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
357 static const struct seq_operations ct_cpu_seq_ops = {
358 .start = ct_cpu_seq_start,
359 .next = ct_cpu_seq_next,
360 .stop = ct_cpu_seq_stop,
361 .show = ct_cpu_seq_show,
364 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
366 return seq_open_net(inode, file, &ct_cpu_seq_ops,
367 sizeof(struct seq_net_private));
370 static const struct file_operations ct_cpu_seq_fops = {
371 .owner = THIS_MODULE,
372 .open = ct_cpu_seq_open,
375 .release = seq_release_net,
378 static int __net_init ip_conntrack_net_init(struct net *net)
380 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
382 proc = proc_net_fops_create(net, "ip_conntrack", 0440, &ct_file_ops);
386 proc_exp = proc_net_fops_create(net, "ip_conntrack_expect", 0440,
391 proc_stat = proc_create("ip_conntrack", S_IRUGO,
392 net->proc_net_stat, &ct_cpu_seq_fops);
398 proc_net_remove(net, "ip_conntrack_expect");
400 proc_net_remove(net, "ip_conntrack");
405 static void __net_exit ip_conntrack_net_exit(struct net *net)
407 remove_proc_entry("ip_conntrack", net->proc_net_stat);
408 proc_net_remove(net, "ip_conntrack_expect");
409 proc_net_remove(net, "ip_conntrack");
412 static struct pernet_operations ip_conntrack_net_ops = {
413 .init = ip_conntrack_net_init,
414 .exit = ip_conntrack_net_exit,
417 int __init nf_conntrack_ipv4_compat_init(void)
419 return register_pernet_subsys(&ip_conntrack_net_ops);
422 void __exit nf_conntrack_ipv4_compat_fini(void)
424 unregister_pernet_subsys(&ip_conntrack_net_ops);