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>
21 #ifdef CONFIG_NF_CT_ACCT
23 seq_print_counters(struct seq_file *s,
24 const struct ip_conntrack_counter *counter)
26 return seq_printf(s, "packets=%llu bytes=%llu ",
27 (unsigned long long)counter->packets,
28 (unsigned long long)counter->bytes);
31 #define seq_print_counters(x, y) 0
34 struct ct_iter_state {
38 static struct hlist_node *ct_get_first(struct seq_file *seq)
40 struct ct_iter_state *st = seq->private;
43 st->bucket < nf_conntrack_htable_size;
45 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
46 return nf_conntrack_hash[st->bucket].first;
51 static struct hlist_node *ct_get_next(struct seq_file *seq,
52 struct hlist_node *head)
54 struct ct_iter_state *st = seq->private;
57 while (head == NULL) {
58 if (++st->bucket >= nf_conntrack_htable_size)
60 head = nf_conntrack_hash[st->bucket].first;
65 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
67 struct hlist_node *head = ct_get_first(seq);
70 while (pos && (head = ct_get_next(seq, head)))
72 return pos ? NULL : head;
75 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
77 read_lock_bh(&nf_conntrack_lock);
78 return ct_get_idx(seq, *pos);
81 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
84 return ct_get_next(s, v);
87 static void ct_seq_stop(struct seq_file *s, void *v)
89 read_unlock_bh(&nf_conntrack_lock);
92 static int ct_seq_show(struct seq_file *s, void *v)
94 const struct nf_conntrack_tuple_hash *hash = v;
95 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
96 struct nf_conntrack_l3proto *l3proto;
97 struct nf_conntrack_l4proto *l4proto;
101 /* we only want to print DIR_ORIGINAL */
102 if (NF_CT_DIRECTION(hash))
104 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
107 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
109 NF_CT_ASSERT(l3proto);
110 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
112 ct->tuplehash[IP_CT_DIR_ORIGINAL]
113 .tuple.dst.protonum);
114 NF_CT_ASSERT(l4proto);
116 if (seq_printf(s, "%-8s %u %ld ",
118 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
119 timer_pending(&ct->timeout)
120 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
123 if (l3proto->print_conntrack(s, ct))
126 if (l4proto->print_conntrack(s, ct))
129 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
133 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
136 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
137 if (seq_printf(s, "[UNREPLIED] "))
140 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
144 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
147 if (test_bit(IPS_ASSURED_BIT, &ct->status))
148 if (seq_printf(s, "[ASSURED] "))
151 #ifdef CONFIG_NF_CONNTRACK_MARK
152 if (seq_printf(s, "mark=%u ", ct->mark))
156 #ifdef CONFIG_NF_CONNTRACK_SECMARK
157 if (seq_printf(s, "secmark=%u ", ct->secmark))
161 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
167 static const struct seq_operations ct_seq_ops = {
168 .start = ct_seq_start,
174 static int ct_open(struct inode *inode, struct file *file)
176 struct seq_file *seq;
177 struct ct_iter_state *st;
180 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
183 ret = seq_open(file, &ct_seq_ops);
186 seq = file->private_data;
188 memset(st, 0, sizeof(struct ct_iter_state));
195 static const struct file_operations ct_file_ops = {
196 .owner = THIS_MODULE,
200 .release = seq_release_private,
204 struct ct_expect_iter_state {
208 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
210 struct ct_expect_iter_state *st = seq->private;
212 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
213 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
214 return nf_ct_expect_hash[st->bucket].first;
219 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
220 struct hlist_node *head)
222 struct ct_expect_iter_state *st = seq->private;
225 while (head == NULL) {
226 if (++st->bucket >= nf_ct_expect_hsize)
228 head = nf_ct_expect_hash[st->bucket].first;
233 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
235 struct hlist_node *head = ct_expect_get_first(seq);
238 while (pos && (head = ct_expect_get_next(seq, head)))
240 return pos ? NULL : head;
243 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
245 read_lock_bh(&nf_conntrack_lock);
246 return ct_expect_get_idx(seq, *pos);
249 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
252 return ct_expect_get_next(seq, v);
255 static void exp_seq_stop(struct seq_file *seq, void *v)
257 read_unlock_bh(&nf_conntrack_lock);
260 static int exp_seq_show(struct seq_file *s, void *v)
262 struct nf_conntrack_expect *exp;
263 struct hlist_node *n = v;
265 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
267 if (exp->tuple.src.l3num != AF_INET)
270 if (exp->timeout.function)
271 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
272 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
276 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
278 print_tuple(s, &exp->tuple,
279 __nf_ct_l3proto_find(exp->tuple.src.l3num),
280 __nf_ct_l4proto_find(exp->tuple.src.l3num,
281 exp->tuple.dst.protonum));
282 return seq_putc(s, '\n');
285 static const struct seq_operations exp_seq_ops = {
286 .start = exp_seq_start,
287 .next = exp_seq_next,
288 .stop = exp_seq_stop,
292 static int exp_open(struct inode *inode, struct file *file)
294 struct seq_file *seq;
295 struct ct_expect_iter_state *st;
298 st = kmalloc(sizeof(struct ct_expect_iter_state), GFP_KERNEL);
301 ret = seq_open(file, &exp_seq_ops);
304 seq = file->private_data;
306 memset(st, 0, sizeof(struct ct_expect_iter_state));
313 static const struct file_operations ip_exp_file_ops = {
314 .owner = THIS_MODULE,
318 .release = seq_release_private,
321 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
326 return SEQ_START_TOKEN;
328 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
329 if (!cpu_possible(cpu))
332 return &per_cpu(nf_conntrack_stat, cpu);
338 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
342 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
343 if (!cpu_possible(cpu))
346 return &per_cpu(nf_conntrack_stat, cpu);
352 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
356 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
358 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
359 struct ip_conntrack_stat *st = v;
361 if (v == SEQ_START_TOKEN) {
362 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");
366 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
367 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
389 static const struct seq_operations ct_cpu_seq_ops = {
390 .start = ct_cpu_seq_start,
391 .next = ct_cpu_seq_next,
392 .stop = ct_cpu_seq_stop,
393 .show = ct_cpu_seq_show,
396 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
398 return seq_open(file, &ct_cpu_seq_ops);
401 static const struct file_operations ct_cpu_seq_fops = {
402 .owner = THIS_MODULE,
403 .open = ct_cpu_seq_open,
406 .release = seq_release_private,
409 int __init nf_conntrack_ipv4_compat_init(void)
411 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
413 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
417 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
422 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
426 proc_stat->proc_fops = &ct_cpu_seq_fops;
427 proc_stat->owner = THIS_MODULE;
432 proc_net_remove("ip_conntrack_expect");
434 proc_net_remove("ip_conntrack");
439 void __exit nf_conntrack_ipv4_compat_fini(void)
441 remove_proc_entry("ip_conntrack", proc_net_stat);
442 proc_net_remove("ip_conntrack_expect");
443 proc_net_remove("ip_conntrack");