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 list_head *ct_get_first(struct seq_file *seq)
46 struct ct_iter_state *st = seq->private;
49 st->bucket < nf_conntrack_htable_size;
51 if (!list_empty(&nf_conntrack_hash[st->bucket]))
52 return nf_conntrack_hash[st->bucket].next;
57 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
59 struct ct_iter_state *st = seq->private;
62 while (head == &nf_conntrack_hash[st->bucket]) {
63 if (++st->bucket >= nf_conntrack_htable_size)
65 head = nf_conntrack_hash[st->bucket].next;
70 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
72 struct list_head *head = ct_get_first(seq);
75 while (pos && (head = ct_get_next(seq, head)))
77 return pos ? NULL : head;
80 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
82 read_lock_bh(&nf_conntrack_lock);
83 return ct_get_idx(seq, *pos);
86 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
89 return ct_get_next(s, v);
92 static void ct_seq_stop(struct seq_file *s, void *v)
94 read_unlock_bh(&nf_conntrack_lock);
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 struct nf_conntrack_l3proto *l3proto;
102 struct nf_conntrack_l4proto *l4proto;
106 /* we only want to print DIR_ORIGINAL */
107 if (NF_CT_DIRECTION(hash))
109 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
112 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
114 NF_CT_ASSERT(l3proto);
115 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
117 ct->tuplehash[IP_CT_DIR_ORIGINAL]
118 .tuple.dst.protonum);
119 NF_CT_ASSERT(l4proto);
121 if (seq_printf(s, "%-8s %u %ld ",
123 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
124 timer_pending(&ct->timeout)
125 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
128 if (l3proto->print_conntrack(s, ct))
131 if (l4proto->print_conntrack(s, ct))
134 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
138 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
141 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
142 if (seq_printf(s, "[UNREPLIED] "))
145 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
149 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
152 if (test_bit(IPS_ASSURED_BIT, &ct->status))
153 if (seq_printf(s, "[ASSURED] "))
156 #ifdef CONFIG_NF_CONNTRACK_MARK
157 if (seq_printf(s, "mark=%u ", ct->mark))
161 #ifdef CONFIG_NF_CONNTRACK_SECMARK
162 if (seq_printf(s, "secmark=%u ", ct->secmark))
166 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
172 static struct seq_operations ct_seq_ops = {
173 .start = ct_seq_start,
179 static int ct_open(struct inode *inode, struct file *file)
181 struct seq_file *seq;
182 struct ct_iter_state *st;
185 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
188 ret = seq_open(file, &ct_seq_ops);
191 seq = file->private_data;
193 memset(st, 0, sizeof(struct ct_iter_state));
200 static const struct file_operations ct_file_ops = {
201 .owner = THIS_MODULE,
205 .release = seq_release_private,
209 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
211 struct list_head *e = &nf_conntrack_expect_list;
214 /* strange seq_file api calls stop even if we fail,
215 * thus we need to grab lock since stop unlocks */
216 read_lock_bh(&nf_conntrack_lock);
221 for (i = 0; i <= *pos; i++) {
223 if (e == &nf_conntrack_expect_list)
229 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
231 struct list_head *e = v;
236 if (e == &nf_conntrack_expect_list)
242 static void exp_seq_stop(struct seq_file *s, void *v)
244 read_unlock_bh(&nf_conntrack_lock);
247 static int exp_seq_show(struct seq_file *s, void *v)
249 struct nf_conntrack_expect *exp = v;
251 if (exp->tuple.src.l3num != AF_INET)
254 if (exp->timeout.function)
255 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
256 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
260 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
262 print_tuple(s, &exp->tuple,
263 __nf_ct_l3proto_find(exp->tuple.src.l3num),
264 __nf_ct_l4proto_find(exp->tuple.src.l3num,
265 exp->tuple.dst.protonum));
266 return seq_putc(s, '\n');
269 static struct seq_operations exp_seq_ops = {
270 .start = exp_seq_start,
271 .next = exp_seq_next,
272 .stop = exp_seq_stop,
276 static int exp_open(struct inode *inode, struct file *file)
278 return seq_open(file, &exp_seq_ops);
281 static const struct file_operations ip_exp_file_ops = {
282 .owner = THIS_MODULE,
286 .release = seq_release
289 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
294 return SEQ_START_TOKEN;
296 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
297 if (!cpu_possible(cpu))
300 return &per_cpu(nf_conntrack_stat, cpu);
306 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
310 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
311 if (!cpu_possible(cpu))
314 return &per_cpu(nf_conntrack_stat, cpu);
320 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
324 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
326 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
327 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 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(file, &ct_cpu_seq_ops);
369 static const struct file_operations ct_cpu_seq_fops = {
370 .owner = THIS_MODULE,
371 .open = ct_cpu_seq_open,
374 .release = seq_release_private,
377 int __init nf_conntrack_ipv4_compat_init(void)
379 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
381 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
385 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
390 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
394 proc_stat->proc_fops = &ct_cpu_seq_fops;
395 proc_stat->owner = THIS_MODULE;
400 proc_net_remove("ip_conntrack_expect");
402 proc_net_remove("ip_conntrack");
407 void __exit nf_conntrack_ipv4_compat_fini(void)
409 remove_proc_entry("ip_conntrack", proc_net_stat);
410 proc_net_remove("ip_conntrack_expect");
411 proc_net_remove("ip_conntrack");