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>
15 #include <linux/netfilter.h>
16 #include <net/netfilter/nf_conntrack_core.h>
17 #include <net/netfilter/nf_conntrack_l3proto.h>
18 #include <net/netfilter/nf_conntrack_l4proto.h>
19 #include <net/netfilter/nf_conntrack_expect.h>
24 #define DEBUGP(format, args...)
27 #ifdef CONFIG_NF_CT_ACCT
29 seq_print_counters(struct seq_file *s,
30 const struct ip_conntrack_counter *counter)
32 return seq_printf(s, "packets=%llu bytes=%llu ",
33 (unsigned long long)counter->packets,
34 (unsigned long long)counter->bytes);
37 #define seq_print_counters(x, y) 0
40 struct ct_iter_state {
44 static struct hlist_node *ct_get_first(struct seq_file *seq)
46 struct ct_iter_state *st = seq->private;
49 st->bucket < nf_conntrack_htable_size;
51 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
52 return nf_conntrack_hash[st->bucket].first;
57 static struct hlist_node *ct_get_next(struct seq_file *seq,
58 struct hlist_node *head)
60 struct ct_iter_state *st = seq->private;
63 while (head == NULL) {
64 if (++st->bucket >= nf_conntrack_htable_size)
66 head = nf_conntrack_hash[st->bucket].first;
71 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
73 struct hlist_node *head = ct_get_first(seq);
76 while (pos && (head = ct_get_next(seq, head)))
78 return pos ? NULL : head;
81 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
83 read_lock_bh(&nf_conntrack_lock);
84 return ct_get_idx(seq, *pos);
87 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
90 return ct_get_next(s, v);
93 static void ct_seq_stop(struct seq_file *s, void *v)
95 read_unlock_bh(&nf_conntrack_lock);
98 static int ct_seq_show(struct seq_file *s, void *v)
100 const struct nf_conntrack_tuple_hash *hash = v;
101 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
102 struct nf_conntrack_l3proto *l3proto;
103 struct nf_conntrack_l4proto *l4proto;
107 /* we only want to print DIR_ORIGINAL */
108 if (NF_CT_DIRECTION(hash))
110 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
113 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
115 NF_CT_ASSERT(l3proto);
116 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
118 ct->tuplehash[IP_CT_DIR_ORIGINAL]
119 .tuple.dst.protonum);
120 NF_CT_ASSERT(l4proto);
122 if (seq_printf(s, "%-8s %u %ld ",
124 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
125 timer_pending(&ct->timeout)
126 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
129 if (l3proto->print_conntrack(s, ct))
132 if (l4proto->print_conntrack(s, ct))
135 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
139 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
142 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
143 if (seq_printf(s, "[UNREPLIED] "))
146 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
150 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
153 if (test_bit(IPS_ASSURED_BIT, &ct->status))
154 if (seq_printf(s, "[ASSURED] "))
157 #ifdef CONFIG_NF_CONNTRACK_MARK
158 if (seq_printf(s, "mark=%u ", ct->mark))
162 #ifdef CONFIG_NF_CONNTRACK_SECMARK
163 if (seq_printf(s, "secmark=%u ", ct->secmark))
167 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
173 static struct seq_operations ct_seq_ops = {
174 .start = ct_seq_start,
180 static int ct_open(struct inode *inode, struct file *file)
182 struct seq_file *seq;
183 struct ct_iter_state *st;
186 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
189 ret = seq_open(file, &ct_seq_ops);
192 seq = file->private_data;
194 memset(st, 0, sizeof(struct ct_iter_state));
201 static const struct file_operations ct_file_ops = {
202 .owner = THIS_MODULE,
206 .release = seq_release_private,
210 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
212 struct list_head *e = &nf_ct_expect_list;
215 /* strange seq_file api calls stop even if we fail,
216 * thus we need to grab lock since stop unlocks */
217 read_lock_bh(&nf_conntrack_lock);
222 for (i = 0; i <= *pos; i++) {
224 if (e == &nf_ct_expect_list)
230 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
232 struct list_head *e = v;
237 if (e == &nf_ct_expect_list)
243 static void exp_seq_stop(struct seq_file *s, void *v)
245 read_unlock_bh(&nf_conntrack_lock);
248 static int exp_seq_show(struct seq_file *s, void *v)
250 struct nf_conntrack_expect *exp = v;
252 if (exp->tuple.src.l3num != AF_INET)
255 if (exp->timeout.function)
256 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
257 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
261 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
263 print_tuple(s, &exp->tuple,
264 __nf_ct_l3proto_find(exp->tuple.src.l3num),
265 __nf_ct_l4proto_find(exp->tuple.src.l3num,
266 exp->tuple.dst.protonum));
267 return seq_putc(s, '\n');
270 static struct seq_operations exp_seq_ops = {
271 .start = exp_seq_start,
272 .next = exp_seq_next,
273 .stop = exp_seq_stop,
277 static int exp_open(struct inode *inode, struct file *file)
279 return seq_open(file, &exp_seq_ops);
282 static const struct file_operations ip_exp_file_ops = {
283 .owner = THIS_MODULE,
287 .release = seq_release
290 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
295 return SEQ_START_TOKEN;
297 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
298 if (!cpu_possible(cpu))
301 return &per_cpu(nf_conntrack_stat, cpu);
307 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
311 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
312 if (!cpu_possible(cpu))
315 return &per_cpu(nf_conntrack_stat, cpu);
321 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
325 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
327 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
328 struct ip_conntrack_stat *st = v;
330 if (v == SEQ_START_TOKEN) {
331 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");
335 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
336 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
358 static struct seq_operations ct_cpu_seq_ops = {
359 .start = ct_cpu_seq_start,
360 .next = ct_cpu_seq_next,
361 .stop = ct_cpu_seq_stop,
362 .show = ct_cpu_seq_show,
365 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
367 return seq_open(file, &ct_cpu_seq_ops);
370 static const struct file_operations ct_cpu_seq_fops = {
371 .owner = THIS_MODULE,
372 .open = ct_cpu_seq_open,
375 .release = seq_release_private,
378 int __init nf_conntrack_ipv4_compat_init(void)
380 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
382 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
386 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
391 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
395 proc_stat->proc_fops = &ct_cpu_seq_fops;
396 proc_stat->owner = THIS_MODULE;
401 proc_net_remove("ip_conntrack_expect");
403 proc_net_remove("ip_conntrack");
408 void __exit nf_conntrack_ipv4_compat_fini(void)
410 remove_proc_entry("ip_conntrack", proc_net_stat);
411 proc_net_remove("ip_conntrack_expect");
412 proc_net_remove("ip_conntrack");