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>
21 #include <net/netfilter/nf_conntrack_acct.h>
23 struct ct_iter_state
{
24 struct seq_net_private p
;
28 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
30 struct net
*net
= seq_file_net(seq
);
31 struct ct_iter_state
*st
= seq
->private;
32 struct hlist_nulls_node
*n
;
35 st
->bucket
< net
->ct
.htable_size
;
37 n
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
44 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
45 struct hlist_nulls_node
*head
)
47 struct net
*net
= seq_file_net(seq
);
48 struct ct_iter_state
*st
= seq
->private;
50 head
= rcu_dereference(head
->next
);
51 while (is_a_nulls(head
)) {
52 if (likely(get_nulls_value(head
) == st
->bucket
)) {
53 if (++st
->bucket
>= net
->ct
.htable_size
)
56 head
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
61 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
63 struct hlist_nulls_node
*head
= ct_get_first(seq
);
66 while (pos
&& (head
= ct_get_next(seq
, head
)))
68 return pos
? NULL
: head
;
71 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
75 return ct_get_idx(seq
, *pos
);
78 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
81 return ct_get_next(s
, v
);
84 static void ct_seq_stop(struct seq_file
*s
, void *v
)
90 static int ct_seq_show(struct seq_file
*s
, void *v
)
92 struct nf_conntrack_tuple_hash
*hash
= v
;
93 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
94 const struct nf_conntrack_l3proto
*l3proto
;
95 const struct nf_conntrack_l4proto
*l4proto
;
99 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
103 /* we only want to print DIR_ORIGINAL */
104 if (NF_CT_DIRECTION(hash
))
106 if (nf_ct_l3num(ct
) != AF_INET
)
109 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
110 NF_CT_ASSERT(l3proto
);
111 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
112 NF_CT_ASSERT(l4proto
);
115 if (seq_printf(s
, "%-8s %u %ld ",
116 l4proto
->name
, nf_ct_protonum(ct
),
117 timer_pending(&ct
->timeout
)
118 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
121 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
124 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
128 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
131 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
132 if (seq_printf(s
, "[UNREPLIED] "))
135 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
139 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
142 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
143 if (seq_printf(s
, "[ASSURED] "))
146 #ifdef CONFIG_NF_CONNTRACK_MARK
147 if (seq_printf(s
, "mark=%u ", ct
->mark
))
151 #ifdef CONFIG_NF_CONNTRACK_SECMARK
152 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
156 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
164 static const struct seq_operations ct_seq_ops
= {
165 .start
= ct_seq_start
,
171 static int ct_open(struct inode
*inode
, struct file
*file
)
173 return seq_open_net(inode
, file
, &ct_seq_ops
,
174 sizeof(struct ct_iter_state
));
177 static const struct file_operations ct_file_ops
= {
178 .owner
= THIS_MODULE
,
182 .release
= seq_release_net
,
186 struct ct_expect_iter_state
{
187 struct seq_net_private p
;
191 static struct hlist_node
*ct_expect_get_first(struct seq_file
*seq
)
193 struct net
*net
= seq_file_net(seq
);
194 struct ct_expect_iter_state
*st
= seq
->private;
195 struct hlist_node
*n
;
197 for (st
->bucket
= 0; st
->bucket
< nf_ct_expect_hsize
; st
->bucket
++) {
198 n
= rcu_dereference(net
->ct
.expect_hash
[st
->bucket
].first
);
205 static struct hlist_node
*ct_expect_get_next(struct seq_file
*seq
,
206 struct hlist_node
*head
)
208 struct net
*net
= seq_file_net(seq
);
209 struct ct_expect_iter_state
*st
= seq
->private;
211 head
= rcu_dereference(head
->next
);
212 while (head
== NULL
) {
213 if (++st
->bucket
>= nf_ct_expect_hsize
)
215 head
= rcu_dereference(net
->ct
.expect_hash
[st
->bucket
].first
);
220 static struct hlist_node
*ct_expect_get_idx(struct seq_file
*seq
, loff_t pos
)
222 struct hlist_node
*head
= ct_expect_get_first(seq
);
225 while (pos
&& (head
= ct_expect_get_next(seq
, head
)))
227 return pos
? NULL
: head
;
230 static void *exp_seq_start(struct seq_file
*seq
, loff_t
*pos
)
234 return ct_expect_get_idx(seq
, *pos
);
237 static void *exp_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
240 return ct_expect_get_next(seq
, v
);
243 static void exp_seq_stop(struct seq_file
*seq
, void *v
)
249 static int exp_seq_show(struct seq_file
*s
, void *v
)
251 struct nf_conntrack_expect
*exp
;
252 const struct hlist_node
*n
= v
;
254 exp
= hlist_entry(n
, struct nf_conntrack_expect
, hnode
);
256 if (exp
->tuple
.src
.l3num
!= AF_INET
)
259 if (exp
->timeout
.function
)
260 seq_printf(s
, "%ld ", timer_pending(&exp
->timeout
)
261 ? (long)(exp
->timeout
.expires
- jiffies
)/HZ
: 0);
265 seq_printf(s
, "proto=%u ", exp
->tuple
.dst
.protonum
);
267 print_tuple(s
, &exp
->tuple
,
268 __nf_ct_l3proto_find(exp
->tuple
.src
.l3num
),
269 __nf_ct_l4proto_find(exp
->tuple
.src
.l3num
,
270 exp
->tuple
.dst
.protonum
));
271 return seq_putc(s
, '\n');
274 static const struct seq_operations exp_seq_ops
= {
275 .start
= exp_seq_start
,
276 .next
= exp_seq_next
,
277 .stop
= exp_seq_stop
,
281 static int exp_open(struct inode
*inode
, struct file
*file
)
283 return seq_open_net(inode
, file
, &exp_seq_ops
,
284 sizeof(struct ct_expect_iter_state
));
287 static const struct file_operations ip_exp_file_ops
= {
288 .owner
= THIS_MODULE
,
292 .release
= seq_release_net
,
295 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
297 struct net
*net
= seq_file_net(seq
);
301 return SEQ_START_TOKEN
;
303 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
304 if (!cpu_possible(cpu
))
307 return per_cpu_ptr(net
->ct
.stat
, cpu
);
313 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
315 struct net
*net
= seq_file_net(seq
);
318 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
319 if (!cpu_possible(cpu
))
322 return per_cpu_ptr(net
->ct
.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 struct net
*net
= seq_file_net(seq
);
335 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
336 const struct ip_conntrack_stat
*st
= v
;
338 if (v
== SEQ_START_TOKEN
) {
339 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 search_restart\n");
343 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
344 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
367 static const struct seq_operations ct_cpu_seq_ops
= {
368 .start
= ct_cpu_seq_start
,
369 .next
= ct_cpu_seq_next
,
370 .stop
= ct_cpu_seq_stop
,
371 .show
= ct_cpu_seq_show
,
374 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
376 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
377 sizeof(struct seq_net_private
));
380 static const struct file_operations ct_cpu_seq_fops
= {
381 .owner
= THIS_MODULE
,
382 .open
= ct_cpu_seq_open
,
385 .release
= seq_release_net
,
388 static int __net_init
ip_conntrack_net_init(struct net
*net
)
390 struct proc_dir_entry
*proc
, *proc_exp
, *proc_stat
;
392 proc
= proc_net_fops_create(net
, "ip_conntrack", 0440, &ct_file_ops
);
396 proc_exp
= proc_net_fops_create(net
, "ip_conntrack_expect", 0440,
401 proc_stat
= proc_create("ip_conntrack", S_IRUGO
,
402 net
->proc_net_stat
, &ct_cpu_seq_fops
);
408 proc_net_remove(net
, "ip_conntrack_expect");
410 proc_net_remove(net
, "ip_conntrack");
415 static void __net_exit
ip_conntrack_net_exit(struct net
*net
)
417 remove_proc_entry("ip_conntrack", net
->proc_net_stat
);
418 proc_net_remove(net
, "ip_conntrack_expect");
419 proc_net_remove(net
, "ip_conntrack");
422 static struct pernet_operations ip_conntrack_net_ops
= {
423 .init
= ip_conntrack_net_init
,
424 .exit
= ip_conntrack_net_exit
,
427 int __init
nf_conntrack_ipv4_compat_init(void)
429 return register_pernet_subsys(&ip_conntrack_net_ops
);
432 void __exit
nf_conntrack_ipv4_compat_fini(void)
434 unregister_pernet_subsys(&ip_conntrack_net_ops
);