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>
5 * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/percpu.h>
15 #include <linux/security.h>
16 #include <net/net_namespace.h>
18 #include <linux/netfilter.h>
19 #include <net/netfilter/nf_conntrack_core.h>
20 #include <net/netfilter/nf_conntrack_l3proto.h>
21 #include <net/netfilter/nf_conntrack_l4proto.h>
22 #include <net/netfilter/nf_conntrack_expect.h>
23 #include <net/netfilter/nf_conntrack_acct.h>
24 #include <linux/rculist_nulls.h>
25 #include <linux/export.h>
27 struct ct_iter_state {
28 struct seq_net_private p;
32 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
34 struct net *net = seq_file_net(seq);
35 struct ct_iter_state *st = seq->private;
36 struct hlist_nulls_node *n;
39 st->bucket < net->ct.htable_size;
42 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
49 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
50 struct hlist_nulls_node *head)
52 struct net *net = seq_file_net(seq);
53 struct ct_iter_state *st = seq->private;
55 head = rcu_dereference(hlist_nulls_next_rcu(head));
56 while (is_a_nulls(head)) {
57 if (likely(get_nulls_value(head) == st->bucket)) {
58 if (++st->bucket >= net->ct.htable_size)
61 head = rcu_dereference(
62 hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
67 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
69 struct hlist_nulls_node *head = ct_get_first(seq);
72 while (pos && (head = ct_get_next(seq, head)))
74 return pos ? NULL : head;
77 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
81 return ct_get_idx(seq, *pos);
84 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
87 return ct_get_next(s, v);
90 static void ct_seq_stop(struct seq_file *s, void *v)
96 #ifdef CONFIG_NF_CONNTRACK_SECMARK
97 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
103 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
107 ret = seq_printf(s, "secctx=%s ", secctx);
109 security_release_secctx(secctx, len);
113 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
119 static int ct_seq_show(struct seq_file *s, void *v)
121 struct nf_conntrack_tuple_hash *hash = v;
122 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
123 const struct nf_conntrack_l3proto *l3proto;
124 const struct nf_conntrack_l4proto *l4proto;
128 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
132 /* we only want to print DIR_ORIGINAL */
133 if (NF_CT_DIRECTION(hash))
135 if (nf_ct_l3num(ct) != AF_INET)
138 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
139 NF_CT_ASSERT(l3proto);
140 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
141 NF_CT_ASSERT(l4proto);
144 if (seq_printf(s, "%-8s %u %ld ",
145 l4proto->name, nf_ct_protonum(ct),
146 timer_pending(&ct->timeout)
147 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
150 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
153 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
157 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
160 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
161 if (seq_printf(s, "[UNREPLIED] "))
164 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
168 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
171 if (test_bit(IPS_ASSURED_BIT, &ct->status))
172 if (seq_printf(s, "[ASSURED] "))
175 #ifdef CONFIG_NF_CONNTRACK_MARK
176 if (seq_printf(s, "mark=%u ", ct->mark))
180 if (ct_show_secctx(s, ct))
183 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
191 static const struct seq_operations ct_seq_ops = {
192 .start = ct_seq_start,
198 static int ct_open(struct inode *inode, struct file *file)
200 return seq_open_net(inode, file, &ct_seq_ops,
201 sizeof(struct ct_iter_state));
204 static const struct file_operations ct_file_ops = {
205 .owner = THIS_MODULE,
209 .release = seq_release_net,
213 struct ct_expect_iter_state {
214 struct seq_net_private p;
218 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
220 struct net *net = seq_file_net(seq);
221 struct ct_expect_iter_state *st = seq->private;
222 struct hlist_node *n;
224 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
226 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
233 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
234 struct hlist_node *head)
236 struct net *net = seq_file_net(seq);
237 struct ct_expect_iter_state *st = seq->private;
239 head = rcu_dereference(hlist_next_rcu(head));
240 while (head == NULL) {
241 if (++st->bucket >= nf_ct_expect_hsize)
243 head = rcu_dereference(
244 hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
249 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
251 struct hlist_node *head = ct_expect_get_first(seq);
254 while (pos && (head = ct_expect_get_next(seq, head)))
256 return pos ? NULL : head;
259 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
263 return ct_expect_get_idx(seq, *pos);
266 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
269 return ct_expect_get_next(seq, v);
272 static void exp_seq_stop(struct seq_file *seq, void *v)
278 static int exp_seq_show(struct seq_file *s, void *v)
280 struct nf_conntrack_expect *exp;
281 const struct hlist_node *n = v;
283 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
285 if (exp->tuple.src.l3num != AF_INET)
288 if (exp->timeout.function)
289 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
290 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
294 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
296 print_tuple(s, &exp->tuple,
297 __nf_ct_l3proto_find(exp->tuple.src.l3num),
298 __nf_ct_l4proto_find(exp->tuple.src.l3num,
299 exp->tuple.dst.protonum));
300 return seq_putc(s, '\n');
303 static const struct seq_operations exp_seq_ops = {
304 .start = exp_seq_start,
305 .next = exp_seq_next,
306 .stop = exp_seq_stop,
310 static int exp_open(struct inode *inode, struct file *file)
312 return seq_open_net(inode, file, &exp_seq_ops,
313 sizeof(struct ct_expect_iter_state));
316 static const struct file_operations ip_exp_file_ops = {
317 .owner = THIS_MODULE,
321 .release = seq_release_net,
324 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
326 struct net *net = seq_file_net(seq);
330 return SEQ_START_TOKEN;
332 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
333 if (!cpu_possible(cpu))
336 return per_cpu_ptr(net->ct.stat, cpu);
342 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
344 struct net *net = seq_file_net(seq);
347 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
348 if (!cpu_possible(cpu))
351 return per_cpu_ptr(net->ct.stat, cpu);
357 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
361 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
363 struct net *net = seq_file_net(seq);
364 unsigned int nr_conntracks = atomic_read(&net->ct.count);
365 const struct ip_conntrack_stat *st = v;
367 if (v == SEQ_START_TOKEN) {
368 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");
372 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
373 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
396 static const struct seq_operations ct_cpu_seq_ops = {
397 .start = ct_cpu_seq_start,
398 .next = ct_cpu_seq_next,
399 .stop = ct_cpu_seq_stop,
400 .show = ct_cpu_seq_show,
403 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
405 return seq_open_net(inode, file, &ct_cpu_seq_ops,
406 sizeof(struct seq_net_private));
409 static const struct file_operations ct_cpu_seq_fops = {
410 .owner = THIS_MODULE,
411 .open = ct_cpu_seq_open,
414 .release = seq_release_net,
417 static int __net_init ip_conntrack_net_init(struct net *net)
419 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
421 proc = proc_create("ip_conntrack", 0440, net->proc_net, &ct_file_ops);
425 proc_exp = proc_create("ip_conntrack_expect", 0440, net->proc_net,
430 proc_stat = proc_create("ip_conntrack", S_IRUGO,
431 net->proc_net_stat, &ct_cpu_seq_fops);
437 remove_proc_entry("ip_conntrack_expect", net->proc_net);
439 remove_proc_entry("ip_conntrack", net->proc_net);
444 static void __net_exit ip_conntrack_net_exit(struct net *net)
446 remove_proc_entry("ip_conntrack", net->proc_net_stat);
447 remove_proc_entry("ip_conntrack_expect", net->proc_net);
448 remove_proc_entry("ip_conntrack", net->proc_net);
451 static struct pernet_operations ip_conntrack_net_ops = {
452 .init = ip_conntrack_net_init,
453 .exit = ip_conntrack_net_exit,
456 int __init nf_conntrack_ipv4_compat_init(void)
458 return register_pernet_subsys(&ip_conntrack_net_ops);
461 void __exit nf_conntrack_ipv4_compat_fini(void)
463 unregister_pernet_subsys(&ip_conntrack_net_ops);