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>
22 #ifdef CONFIG_NF_CT_ACCT
24 seq_print_counters(struct seq_file *s,
25 const struct ip_conntrack_counter *counter)
27 return seq_printf(s, "packets=%llu bytes=%llu ",
28 (unsigned long long)counter->packets,
29 (unsigned long long)counter->bytes);
32 #define seq_print_counters(x, y) 0
35 struct ct_iter_state {
39 static struct hlist_node *ct_get_first(struct seq_file *seq)
41 struct ct_iter_state *st = seq->private;
45 st->bucket < nf_conntrack_htable_size;
47 n = rcu_dereference(nf_conntrack_hash[st->bucket].first);
54 static struct hlist_node *ct_get_next(struct seq_file *seq,
55 struct hlist_node *head)
57 struct ct_iter_state *st = seq->private;
59 head = rcu_dereference(head->next);
60 while (head == NULL) {
61 if (++st->bucket >= nf_conntrack_htable_size)
63 head = rcu_dereference(nf_conntrack_hash[st->bucket].first);
68 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
70 struct hlist_node *head = ct_get_first(seq);
73 while (pos && (head = ct_get_next(seq, head)))
75 return pos ? NULL : head;
78 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
82 return ct_get_idx(seq, *pos);
85 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
88 return ct_get_next(s, v);
91 static void ct_seq_stop(struct seq_file *s, void *v)
97 static int ct_seq_show(struct seq_file *s, void *v)
99 const struct nf_conntrack_tuple_hash *hash = v;
100 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
101 const struct nf_conntrack_l3proto *l3proto;
102 const struct nf_conntrack_l4proto *l4proto;
106 /* we only want to print DIR_ORIGINAL */
107 if (NF_CT_DIRECTION(hash))
109 if (nf_ct_l3num(ct) != AF_INET)
112 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
113 NF_CT_ASSERT(l3proto);
114 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
115 NF_CT_ASSERT(l4proto);
117 if (seq_printf(s, "%-8s %u %ld ",
118 l4proto->name, nf_ct_protonum(ct),
119 timer_pending(&ct->timeout)
120 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
123 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
126 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
130 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
133 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
134 if (seq_printf(s, "[UNREPLIED] "))
137 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
141 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
144 if (test_bit(IPS_ASSURED_BIT, &ct->status))
145 if (seq_printf(s, "[ASSURED] "))
148 #ifdef CONFIG_NF_CONNTRACK_MARK
149 if (seq_printf(s, "mark=%u ", ct->mark))
153 #ifdef CONFIG_NF_CONNTRACK_SECMARK
154 if (seq_printf(s, "secmark=%u ", ct->secmark))
158 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
164 static const struct seq_operations ct_seq_ops = {
165 .start = ct_seq_start,
171 static int ct_open(struct inode *inode, struct file *file)
173 return seq_open_private(file, &ct_seq_ops,
174 sizeof(struct ct_iter_state));
177 static const struct file_operations ct_file_ops = {
178 .owner = THIS_MODULE,
182 .release = seq_release_private,
186 struct ct_expect_iter_state {
190 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
192 struct ct_expect_iter_state *st = seq->private;
193 struct hlist_node *n;
195 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
196 n = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
203 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
204 struct hlist_node *head)
206 struct ct_expect_iter_state *st = seq->private;
208 head = rcu_dereference(head->next);
209 while (head == NULL) {
210 if (++st->bucket >= nf_ct_expect_hsize)
212 head = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
217 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
219 struct hlist_node *head = ct_expect_get_first(seq);
222 while (pos && (head = ct_expect_get_next(seq, head)))
224 return pos ? NULL : head;
227 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
231 return ct_expect_get_idx(seq, *pos);
234 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
237 return ct_expect_get_next(seq, v);
240 static void exp_seq_stop(struct seq_file *seq, void *v)
246 static int exp_seq_show(struct seq_file *s, void *v)
248 struct nf_conntrack_expect *exp;
249 const struct hlist_node *n = v;
251 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
253 if (exp->tuple.src.l3num != AF_INET)
256 if (exp->timeout.function)
257 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
258 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
262 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
264 print_tuple(s, &exp->tuple,
265 __nf_ct_l3proto_find(exp->tuple.src.l3num),
266 __nf_ct_l4proto_find(exp->tuple.src.l3num,
267 exp->tuple.dst.protonum));
268 return seq_putc(s, '\n');
271 static const struct seq_operations exp_seq_ops = {
272 .start = exp_seq_start,
273 .next = exp_seq_next,
274 .stop = exp_seq_stop,
278 static int exp_open(struct inode *inode, struct file *file)
280 return seq_open_private(file, &exp_seq_ops,
281 sizeof(struct ct_expect_iter_state));
284 static const struct file_operations ip_exp_file_ops = {
285 .owner = THIS_MODULE,
289 .release = seq_release_private,
292 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
297 return SEQ_START_TOKEN;
299 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
300 if (!cpu_possible(cpu))
303 return &per_cpu(nf_conntrack_stat, cpu);
309 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
313 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
314 if (!cpu_possible(cpu))
317 return &per_cpu(nf_conntrack_stat, cpu);
323 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
327 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
329 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
330 const struct ip_conntrack_stat *st = v;
332 if (v == SEQ_START_TOKEN) {
333 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");
337 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
338 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
360 static const struct seq_operations ct_cpu_seq_ops = {
361 .start = ct_cpu_seq_start,
362 .next = ct_cpu_seq_next,
363 .stop = ct_cpu_seq_stop,
364 .show = ct_cpu_seq_show,
367 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
369 return seq_open(file, &ct_cpu_seq_ops);
372 static const struct file_operations ct_cpu_seq_fops = {
373 .owner = THIS_MODULE,
374 .open = ct_cpu_seq_open,
377 .release = seq_release,
380 int __init nf_conntrack_ipv4_compat_init(void)
382 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
384 proc = proc_net_fops_create(&init_net, "ip_conntrack", 0440, &ct_file_ops);
388 proc_exp = proc_net_fops_create(&init_net, "ip_conntrack_expect", 0440,
393 proc_stat = proc_create("ip_conntrack", S_IRUGO,
394 init_net.proc_net_stat, &ct_cpu_seq_fops);
400 proc_net_remove(&init_net, "ip_conntrack_expect");
402 proc_net_remove(&init_net, "ip_conntrack");
407 void __exit nf_conntrack_ipv4_compat_fini(void)
409 remove_proc_entry("ip_conntrack", init_net.proc_net_stat);
410 proc_net_remove(&init_net, "ip_conntrack_expect");
411 proc_net_remove(&init_net, "ip_conntrack");