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 (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 (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
131 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
135 if (seq_print_counters(s
, &ct
->counters
[IP_CT_DIR_ORIGINAL
]))
138 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
139 if (seq_printf(s
, "[UNREPLIED] "))
142 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
146 if (seq_print_counters(s
, &ct
->counters
[IP_CT_DIR_REPLY
]))
149 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
150 if (seq_printf(s
, "[ASSURED] "))
153 #ifdef CONFIG_NF_CONNTRACK_MARK
154 if (seq_printf(s
, "mark=%u ", ct
->mark
))
158 #ifdef CONFIG_NF_CONNTRACK_SECMARK
159 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
163 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
169 static const struct seq_operations ct_seq_ops
= {
170 .start
= ct_seq_start
,
176 static int ct_open(struct inode
*inode
, struct file
*file
)
178 return seq_open_private(file
, &ct_seq_ops
,
179 sizeof(struct ct_iter_state
));
182 static const struct file_operations ct_file_ops
= {
183 .owner
= THIS_MODULE
,
187 .release
= seq_release_private
,
191 struct ct_expect_iter_state
{
195 static struct hlist_node
*ct_expect_get_first(struct seq_file
*seq
)
197 struct ct_expect_iter_state
*st
= seq
->private;
198 struct hlist_node
*n
;
200 for (st
->bucket
= 0; st
->bucket
< nf_ct_expect_hsize
; st
->bucket
++) {
201 n
= rcu_dereference(nf_ct_expect_hash
[st
->bucket
].first
);
208 static struct hlist_node
*ct_expect_get_next(struct seq_file
*seq
,
209 struct hlist_node
*head
)
211 struct ct_expect_iter_state
*st
= seq
->private;
213 head
= rcu_dereference(head
->next
);
214 while (head
== NULL
) {
215 if (++st
->bucket
>= nf_ct_expect_hsize
)
217 head
= rcu_dereference(nf_ct_expect_hash
[st
->bucket
].first
);
222 static struct hlist_node
*ct_expect_get_idx(struct seq_file
*seq
, loff_t pos
)
224 struct hlist_node
*head
= ct_expect_get_first(seq
);
227 while (pos
&& (head
= ct_expect_get_next(seq
, head
)))
229 return pos
? NULL
: head
;
232 static void *exp_seq_start(struct seq_file
*seq
, loff_t
*pos
)
236 return ct_expect_get_idx(seq
, *pos
);
239 static void *exp_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
242 return ct_expect_get_next(seq
, v
);
245 static void exp_seq_stop(struct seq_file
*seq
, void *v
)
251 static int exp_seq_show(struct seq_file
*s
, void *v
)
253 struct nf_conntrack_expect
*exp
;
254 const struct hlist_node
*n
= v
;
256 exp
= hlist_entry(n
, struct nf_conntrack_expect
, hnode
);
258 if (exp
->tuple
.src
.l3num
!= AF_INET
)
261 if (exp
->timeout
.function
)
262 seq_printf(s
, "%ld ", timer_pending(&exp
->timeout
)
263 ? (long)(exp
->timeout
.expires
- jiffies
)/HZ
: 0);
267 seq_printf(s
, "proto=%u ", exp
->tuple
.dst
.protonum
);
269 print_tuple(s
, &exp
->tuple
,
270 __nf_ct_l3proto_find(exp
->tuple
.src
.l3num
),
271 __nf_ct_l4proto_find(exp
->tuple
.src
.l3num
,
272 exp
->tuple
.dst
.protonum
));
273 return seq_putc(s
, '\n');
276 static const struct seq_operations exp_seq_ops
= {
277 .start
= exp_seq_start
,
278 .next
= exp_seq_next
,
279 .stop
= exp_seq_stop
,
283 static int exp_open(struct inode
*inode
, struct file
*file
)
285 return seq_open_private(file
, &exp_seq_ops
,
286 sizeof(struct ct_expect_iter_state
));
289 static const struct file_operations ip_exp_file_ops
= {
290 .owner
= THIS_MODULE
,
294 .release
= seq_release_private
,
297 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
302 return SEQ_START_TOKEN
;
304 for (cpu
= *pos
-1; cpu
< NR_CPUS
; ++cpu
) {
305 if (!cpu_possible(cpu
))
308 return &per_cpu(nf_conntrack_stat
, cpu
);
314 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
318 for (cpu
= *pos
; cpu
< NR_CPUS
; ++cpu
) {
319 if (!cpu_possible(cpu
))
322 return &per_cpu(nf_conntrack_stat
, cpu
);
328 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
332 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
334 unsigned int nr_conntracks
= atomic_read(&nf_conntrack_count
);
335 const struct ip_conntrack_stat
*st
= v
;
337 if (v
== SEQ_START_TOKEN
) {
338 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");
342 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
343 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
365 static const struct seq_operations ct_cpu_seq_ops
= {
366 .start
= ct_cpu_seq_start
,
367 .next
= ct_cpu_seq_next
,
368 .stop
= ct_cpu_seq_stop
,
369 .show
= ct_cpu_seq_show
,
372 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
374 return seq_open(file
, &ct_cpu_seq_ops
);
377 static const struct file_operations ct_cpu_seq_fops
= {
378 .owner
= THIS_MODULE
,
379 .open
= ct_cpu_seq_open
,
382 .release
= seq_release_private
,
385 int __init
nf_conntrack_ipv4_compat_init(void)
387 struct proc_dir_entry
*proc
, *proc_exp
, *proc_stat
;
389 proc
= proc_net_fops_create(&init_net
, "ip_conntrack", 0440, &ct_file_ops
);
393 proc_exp
= proc_net_fops_create(&init_net
, "ip_conntrack_expect", 0440,
398 proc_stat
= create_proc_entry("ip_conntrack", S_IRUGO
, init_net
.proc_net_stat
);
402 proc_stat
->proc_fops
= &ct_cpu_seq_fops
;
403 proc_stat
->owner
= THIS_MODULE
;
408 proc_net_remove(&init_net
, "ip_conntrack_expect");
410 proc_net_remove(&init_net
, "ip_conntrack");
415 void __exit
nf_conntrack_ipv4_compat_fini(void)
417 remove_proc_entry("ip_conntrack", init_net
.proc_net_stat
);
418 proc_net_remove(&init_net
, "ip_conntrack_expect");
419 proc_net_remove(&init_net
, "ip_conntrack");