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 <linux/security.h>
15 #include <net/net_namespace.h>
17 #include <linux/netfilter.h>
18 #include <net/netfilter/nf_conntrack_core.h>
19 #include <net/netfilter/nf_conntrack_l3proto.h>
20 #include <net/netfilter/nf_conntrack_l4proto.h>
21 #include <net/netfilter/nf_conntrack_expect.h>
22 #include <net/netfilter/nf_conntrack_acct.h>
23 #include <linux/rculist_nulls.h>
24 #include <linux/export.h>
26 struct ct_iter_state {
27 struct seq_net_private p;
31 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
33 struct net *net = seq_file_net(seq);
34 struct ct_iter_state *st = seq->private;
35 struct hlist_nulls_node *n;
38 st->bucket < net->ct.htable_size;
41 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
48 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
49 struct hlist_nulls_node *head)
51 struct net *net = seq_file_net(seq);
52 struct ct_iter_state *st = seq->private;
54 head = rcu_dereference(hlist_nulls_next_rcu(head));
55 while (is_a_nulls(head)) {
56 if (likely(get_nulls_value(head) == st->bucket)) {
57 if (++st->bucket >= net->ct.htable_size)
60 head = rcu_dereference(
61 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
66 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
68 struct hlist_nulls_node *head = ct_get_first(seq);
71 while (pos && (head = ct_get_next(seq, head)))
73 return pos ? NULL : head;
76 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
80 return ct_get_idx(seq, *pos);
83 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
86 return ct_get_next(s, v);
89 static void ct_seq_stop(struct seq_file *s, void *v)
95 #ifdef CONFIG_NF_CONNTRACK_SECMARK
96 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
102 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
106 ret = seq_printf(s, "secctx=%s ", secctx);
108 security_release_secctx(secctx, len);
112 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
118 static int ct_seq_show(struct seq_file *s, void *v)
120 struct nf_conntrack_tuple_hash *hash = v;
121 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
122 const struct nf_conntrack_l3proto *l3proto;
123 const struct nf_conntrack_l4proto *l4proto;
127 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
131 /* we only want to print DIR_ORIGINAL */
132 if (NF_CT_DIRECTION(hash))
134 if (nf_ct_l3num(ct) != AF_INET)
137 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
138 NF_CT_ASSERT(l3proto);
139 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
140 NF_CT_ASSERT(l4proto);
143 if (seq_printf(s, "%-8s %u %ld ",
144 l4proto->name, nf_ct_protonum(ct),
145 timer_pending(&ct->timeout)
146 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
149 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
152 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
156 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
159 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
160 if (seq_printf(s, "[UNREPLIED] "))
163 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
167 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
170 if (test_bit(IPS_ASSURED_BIT, &ct->status))
171 if (seq_printf(s, "[ASSURED] "))
174 #ifdef CONFIG_NF_CONNTRACK_MARK
175 if (seq_printf(s, "mark=%u ", ct->mark))
179 if (ct_show_secctx(s, ct))
182 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
190 static const struct seq_operations ct_seq_ops = {
191 .start = ct_seq_start,
197 static int ct_open(struct inode *inode, struct file *file)
199 return seq_open_net(inode, file, &ct_seq_ops,
200 sizeof(struct ct_iter_state));
203 static const struct file_operations ct_file_ops = {
204 .owner = THIS_MODULE,
208 .release = seq_release_net,
212 struct ct_expect_iter_state {
213 struct seq_net_private p;
217 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
219 struct net *net = seq_file_net(seq);
220 struct ct_expect_iter_state *st = seq->private;
221 struct hlist_node *n;
223 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
225 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
232 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
233 struct hlist_node *head)
235 struct net *net = seq_file_net(seq);
236 struct ct_expect_iter_state *st = seq->private;
238 head = rcu_dereference(hlist_next_rcu(head));
239 while (head == NULL) {
240 if (++st->bucket >= nf_ct_expect_hsize)
242 head = rcu_dereference(
243 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
248 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
250 struct hlist_node *head = ct_expect_get_first(seq);
253 while (pos && (head = ct_expect_get_next(seq, head)))
255 return pos ? NULL : head;
258 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
262 return ct_expect_get_idx(seq, *pos);
265 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
268 return ct_expect_get_next(seq, v);
271 static void exp_seq_stop(struct seq_file *seq, void *v)
277 static int exp_seq_show(struct seq_file *s, void *v)
279 struct nf_conntrack_expect *exp;
280 const struct hlist_node *n = v;
282 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
284 if (exp->tuple.src.l3num != AF_INET)
287 if (exp->timeout.function)
288 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
289 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
293 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
295 print_tuple(s, &exp->tuple,
296 __nf_ct_l3proto_find(exp->tuple.src.l3num),
297 __nf_ct_l4proto_find(exp->tuple.src.l3num,
298 exp->tuple.dst.protonum));
299 return seq_putc(s, '\n');
302 static const struct seq_operations exp_seq_ops = {
303 .start = exp_seq_start,
304 .next = exp_seq_next,
305 .stop = exp_seq_stop,
309 static int exp_open(struct inode *inode, struct file *file)
311 return seq_open_net(inode, file, &exp_seq_ops,
312 sizeof(struct ct_expect_iter_state));
315 static const struct file_operations ip_exp_file_ops = {
316 .owner = THIS_MODULE,
320 .release = seq_release_net,
323 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
325 struct net *net = seq_file_net(seq);
329 return SEQ_START_TOKEN;
331 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
332 if (!cpu_possible(cpu))
335 return per_cpu_ptr(net->ct.stat, cpu);
341 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
343 struct net *net = seq_file_net(seq);
346 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
347 if (!cpu_possible(cpu))
350 return per_cpu_ptr(net->ct.stat, cpu);
356 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
360 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
362 struct net *net = seq_file_net(seq);
363 unsigned int nr_conntracks = atomic_read(&net->ct.count);
364 const struct ip_conntrack_stat *st = v;
366 if (v == SEQ_START_TOKEN) {
367 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");
371 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
372 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
395 static const 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_net(inode, file, &ct_cpu_seq_ops,
405 sizeof(struct seq_net_private));
408 static const struct file_operations ct_cpu_seq_fops = {
409 .owner = THIS_MODULE,
410 .open = ct_cpu_seq_open,
413 .release = seq_release_net,
416 static int __net_init ip_conntrack_net_init(struct net *net)
418 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
420 proc = proc_net_fops_create(net, "ip_conntrack", 0440, &ct_file_ops);
424 proc_exp = proc_net_fops_create(net, "ip_conntrack_expect", 0440,
429 proc_stat = proc_create("ip_conntrack", S_IRUGO,
430 net->proc_net_stat, &ct_cpu_seq_fops);
436 proc_net_remove(net, "ip_conntrack_expect");
438 proc_net_remove(net, "ip_conntrack");
443 static void __net_exit ip_conntrack_net_exit(struct net *net)
445 remove_proc_entry("ip_conntrack", net->proc_net_stat);
446 proc_net_remove(net, "ip_conntrack_expect");
447 proc_net_remove(net, "ip_conntrack");
450 static struct pernet_operations ip_conntrack_net_ops = {
451 .init = ip_conntrack_net_init,
452 .exit = ip_conntrack_net_exit,
455 int __init nf_conntrack_ipv4_compat_init(void)
457 return register_pernet_subsys(&ip_conntrack_net_ops);
460 void __exit nf_conntrack_ipv4_compat_fini(void)
462 unregister_pernet_subsys(&ip_conntrack_net_ops);