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 ct_iter_state *st = seq->private;
35 struct hlist_nulls_node *n;
38 st->bucket < nf_conntrack_htable_size;
41 hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
48 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
49 struct hlist_nulls_node *head)
51 struct ct_iter_state *st = seq->private;
53 head = rcu_dereference(hlist_nulls_next_rcu(head));
54 while (is_a_nulls(head)) {
55 if (likely(get_nulls_value(head) == st->bucket)) {
56 if (++st->bucket >= nf_conntrack_htable_size)
59 head = rcu_dereference(
60 hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
65 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
67 struct hlist_nulls_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)
79 return ct_get_idx(seq, *pos);
82 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
85 return ct_get_next(s, v);
88 static void ct_seq_stop(struct seq_file *s, void *v)
94 #ifdef CONFIG_NF_CONNTRACK_SECMARK
95 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
101 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
105 seq_printf(s, "secctx=%s ", secctx);
107 security_release_secctx(secctx, len);
110 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
115 static bool ct_seq_should_skip(const struct nf_conn *ct,
116 const struct net *net,
117 const struct nf_conntrack_tuple_hash *hash)
119 /* we only want to print DIR_ORIGINAL */
120 if (NF_CT_DIRECTION(hash))
123 if (nf_ct_l3num(ct) != AF_INET)
126 if (!net_eq(nf_ct_net(ct), net))
132 static int ct_seq_show(struct seq_file *s, void *v)
134 struct nf_conntrack_tuple_hash *hash = v;
135 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
136 const struct nf_conntrack_l3proto *l3proto;
137 const struct nf_conntrack_l4proto *l4proto;
141 if (ct_seq_should_skip(ct, seq_file_net(s), hash))
144 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
147 /* check if we raced w. object reuse */
148 if (!nf_ct_is_confirmed(ct) ||
149 ct_seq_should_skip(ct, seq_file_net(s), hash))
152 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
153 NF_CT_ASSERT(l3proto);
154 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
155 NF_CT_ASSERT(l4proto);
158 seq_printf(s, "%-8s %u %ld ",
159 l4proto->name, nf_ct_protonum(ct),
160 timer_pending(&ct->timeout)
161 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
163 if (l4proto->print_conntrack)
164 l4proto->print_conntrack(s, ct);
166 if (seq_has_overflowed(s))
169 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
172 if (seq_has_overflowed(s))
175 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
178 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
179 seq_printf(s, "[UNREPLIED] ");
181 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
184 if (seq_has_overflowed(s))
187 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
190 if (test_bit(IPS_ASSURED_BIT, &ct->status))
191 seq_printf(s, "[ASSURED] ");
193 #ifdef CONFIG_NF_CONNTRACK_MARK
194 seq_printf(s, "mark=%u ", ct->mark);
197 ct_show_secctx(s, ct);
199 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
201 if (seq_has_overflowed(s))
210 static const struct seq_operations ct_seq_ops = {
211 .start = ct_seq_start,
217 static int ct_open(struct inode *inode, struct file *file)
219 return seq_open_net(inode, file, &ct_seq_ops,
220 sizeof(struct ct_iter_state));
223 static const struct file_operations ct_file_ops = {
224 .owner = THIS_MODULE,
228 .release = seq_release_net,
232 struct ct_expect_iter_state {
233 struct seq_net_private p;
237 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
239 struct ct_expect_iter_state *st = seq->private;
240 struct hlist_node *n;
242 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
244 hlist_first_rcu(&nf_ct_expect_hash[st->bucket]));
251 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
252 struct hlist_node *head)
254 struct ct_expect_iter_state *st = seq->private;
256 head = rcu_dereference(hlist_next_rcu(head));
257 while (head == NULL) {
258 if (++st->bucket >= nf_ct_expect_hsize)
260 head = rcu_dereference(
261 hlist_first_rcu(&nf_ct_expect_hash[st->bucket]));
266 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
268 struct hlist_node *head = ct_expect_get_first(seq);
271 while (pos && (head = ct_expect_get_next(seq, head)))
273 return pos ? NULL : head;
276 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
280 return ct_expect_get_idx(seq, *pos);
283 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
286 return ct_expect_get_next(seq, v);
289 static void exp_seq_stop(struct seq_file *seq, void *v)
295 static int exp_seq_show(struct seq_file *s, void *v)
297 struct nf_conntrack_expect *exp;
298 const struct hlist_node *n = v;
300 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
302 if (!net_eq(nf_ct_net(exp->master), seq_file_net(s)))
305 if (exp->tuple.src.l3num != AF_INET)
308 if (exp->timeout.function)
309 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
310 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
314 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
316 print_tuple(s, &exp->tuple,
317 __nf_ct_l3proto_find(exp->tuple.src.l3num),
318 __nf_ct_l4proto_find(exp->tuple.src.l3num,
319 exp->tuple.dst.protonum));
325 static const struct seq_operations exp_seq_ops = {
326 .start = exp_seq_start,
327 .next = exp_seq_next,
328 .stop = exp_seq_stop,
332 static int exp_open(struct inode *inode, struct file *file)
334 return seq_open_net(inode, file, &exp_seq_ops,
335 sizeof(struct ct_expect_iter_state));
338 static const struct file_operations ip_exp_file_ops = {
339 .owner = THIS_MODULE,
343 .release = seq_release_net,
346 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
348 struct net *net = seq_file_net(seq);
352 return SEQ_START_TOKEN;
354 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
355 if (!cpu_possible(cpu))
358 return per_cpu_ptr(net->ct.stat, cpu);
364 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
366 struct net *net = seq_file_net(seq);
369 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
370 if (!cpu_possible(cpu))
373 return per_cpu_ptr(net->ct.stat, cpu);
379 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
383 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
385 struct net *net = seq_file_net(seq);
386 unsigned int nr_conntracks = atomic_read(&net->ct.count);
387 const struct ip_conntrack_stat *st = v;
389 if (v == SEQ_START_TOKEN) {
390 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");
394 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
395 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
418 static const struct seq_operations ct_cpu_seq_ops = {
419 .start = ct_cpu_seq_start,
420 .next = ct_cpu_seq_next,
421 .stop = ct_cpu_seq_stop,
422 .show = ct_cpu_seq_show,
425 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
427 return seq_open_net(inode, file, &ct_cpu_seq_ops,
428 sizeof(struct seq_net_private));
431 static const struct file_operations ct_cpu_seq_fops = {
432 .owner = THIS_MODULE,
433 .open = ct_cpu_seq_open,
436 .release = seq_release_net,
439 static int __net_init ip_conntrack_net_init(struct net *net)
441 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
443 proc = proc_create("ip_conntrack", 0440, net->proc_net, &ct_file_ops);
447 proc_exp = proc_create("ip_conntrack_expect", 0440, net->proc_net,
452 proc_stat = proc_create("ip_conntrack", S_IRUGO,
453 net->proc_net_stat, &ct_cpu_seq_fops);
459 remove_proc_entry("ip_conntrack_expect", net->proc_net);
461 remove_proc_entry("ip_conntrack", net->proc_net);
466 static void __net_exit ip_conntrack_net_exit(struct net *net)
468 remove_proc_entry("ip_conntrack", net->proc_net_stat);
469 remove_proc_entry("ip_conntrack_expect", net->proc_net);
470 remove_proc_entry("ip_conntrack", net->proc_net);
473 static struct pernet_operations ip_conntrack_net_ops = {
474 .init = ip_conntrack_net_init,
475 .exit = ip_conntrack_net_exit,
478 int __init nf_conntrack_ipv4_compat_init(void)
480 return register_pernet_subsys(&ip_conntrack_net_ops);
483 void __exit nf_conntrack_ipv4_compat_fini(void)
485 unregister_pernet_subsys(&ip_conntrack_net_ops);