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 struct ct_expect_iter_state {
214 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
216 struct ct_expect_iter_state *st = seq->private;
218 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
219 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
220 return nf_ct_expect_hash[st->bucket].first;
225 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
226 struct hlist_node *head)
228 struct ct_expect_iter_state *st = seq->private;
231 while (head == NULL) {
232 if (++st->bucket >= nf_ct_expect_hsize)
234 head = nf_ct_expect_hash[st->bucket].first;
239 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
241 struct hlist_node *head = ct_expect_get_first(seq);
244 while (pos && (head = ct_expect_get_next(seq, head)))
246 return pos ? NULL : head;
249 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
251 read_lock_bh(&nf_conntrack_lock);
252 return ct_expect_get_idx(seq, *pos);
255 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
258 return ct_expect_get_next(seq, v);
261 static void exp_seq_stop(struct seq_file *seq, void *v)
263 read_unlock_bh(&nf_conntrack_lock);
266 static int exp_seq_show(struct seq_file *s, void *v)
268 struct nf_conntrack_expect *exp;
269 struct hlist_node *n = v;
271 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
273 if (exp->tuple.src.l3num != AF_INET)
276 if (exp->timeout.function)
277 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
278 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
282 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
284 print_tuple(s, &exp->tuple,
285 __nf_ct_l3proto_find(exp->tuple.src.l3num),
286 __nf_ct_l4proto_find(exp->tuple.src.l3num,
287 exp->tuple.dst.protonum));
288 return seq_putc(s, '\n');
291 static struct seq_operations exp_seq_ops = {
292 .start = exp_seq_start,
293 .next = exp_seq_next,
294 .stop = exp_seq_stop,
298 static int exp_open(struct inode *inode, struct file *file)
300 struct seq_file *seq;
301 struct ct_expect_iter_state *st;
304 st = kmalloc(sizeof(struct ct_expect_iter_state), GFP_KERNEL);
307 ret = seq_open(file, &exp_seq_ops);
310 seq = file->private_data;
312 memset(st, 0, sizeof(struct ct_expect_iter_state));
319 static const struct file_operations ip_exp_file_ops = {
320 .owner = THIS_MODULE,
324 .release = seq_release_private,
327 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
332 return SEQ_START_TOKEN;
334 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
335 if (!cpu_possible(cpu))
338 return &per_cpu(nf_conntrack_stat, cpu);
344 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
348 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
349 if (!cpu_possible(cpu))
352 return &per_cpu(nf_conntrack_stat, cpu);
358 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
362 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
364 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
365 struct ip_conntrack_stat *st = v;
367 if (v == SEQ_START_TOKEN) {
368 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");
372 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
373 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
395 static struct seq_operations ct_cpu_seq_ops = {
396 .start = ct_cpu_seq_start,
397 .next = ct_cpu_seq_next,
398 .stop = ct_cpu_seq_stop,
399 .show = ct_cpu_seq_show,
402 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
404 return seq_open(file, &ct_cpu_seq_ops);
407 static const struct file_operations ct_cpu_seq_fops = {
408 .owner = THIS_MODULE,
409 .open = ct_cpu_seq_open,
412 .release = seq_release_private,
415 int __init nf_conntrack_ipv4_compat_init(void)
417 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
419 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
423 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
428 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
432 proc_stat->proc_fops = &ct_cpu_seq_fops;
433 proc_stat->owner = THIS_MODULE;
438 proc_net_remove("ip_conntrack_expect");
440 proc_net_remove("ip_conntrack");
445 void __exit nf_conntrack_ipv4_compat_fini(void)
447 remove_proc_entry("ip_conntrack", proc_net_stat);
448 proc_net_remove("ip_conntrack_expect");
449 proc_net_remove("ip_conntrack");