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>
24 struct ct_iter_state {
25 struct seq_net_private p;
29 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
31 struct net *net = seq_file_net(seq);
32 struct ct_iter_state *st = seq->private;
33 struct hlist_nulls_node *n;
36 st->bucket < net->ct.htable_size;
38 n = rcu_dereference(net->ct.hash[st->bucket].first);
45 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
46 struct hlist_nulls_node *head)
48 struct net *net = seq_file_net(seq);
49 struct ct_iter_state *st = seq->private;
51 head = rcu_dereference(head->next);
52 while (is_a_nulls(head)) {
53 if (likely(get_nulls_value(head) == st->bucket)) {
54 if (++st->bucket >= net->ct.htable_size)
57 head = rcu_dereference(net->ct.hash[st->bucket].first);
62 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
64 struct hlist_nulls_node *head = ct_get_first(seq);
67 while (pos && (head = ct_get_next(seq, head)))
69 return pos ? NULL : head;
72 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76 return ct_get_idx(seq, *pos);
79 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
82 return ct_get_next(s, v);
85 static void ct_seq_stop(struct seq_file *s, void *v)
91 #ifdef CONFIG_NF_CONNTRACK_SECMARK
92 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
98 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
102 ret = seq_printf(s, "secctx=%s ", secctx);
104 security_release_secctx(secctx, len);
108 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
114 static int ct_seq_show(struct seq_file *s, void *v)
116 struct nf_conntrack_tuple_hash *hash = v;
117 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
118 const struct nf_conntrack_l3proto *l3proto;
119 const struct nf_conntrack_l4proto *l4proto;
123 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
127 /* we only want to print DIR_ORIGINAL */
128 if (NF_CT_DIRECTION(hash))
130 if (nf_ct_l3num(ct) != AF_INET)
133 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
134 NF_CT_ASSERT(l3proto);
135 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
136 NF_CT_ASSERT(l4proto);
139 if (seq_printf(s, "%-8s %u %ld ",
140 l4proto->name, nf_ct_protonum(ct),
141 timer_pending(&ct->timeout)
142 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
145 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
148 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
152 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
155 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
156 if (seq_printf(s, "[UNREPLIED] "))
159 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
163 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
166 if (test_bit(IPS_ASSURED_BIT, &ct->status))
167 if (seq_printf(s, "[ASSURED] "))
170 #ifdef CONFIG_NF_CONNTRACK_MARK
171 if (seq_printf(s, "mark=%u ", ct->mark))
175 if (ct_show_secctx(s, ct))
178 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
186 static const struct seq_operations ct_seq_ops = {
187 .start = ct_seq_start,
193 static int ct_open(struct inode *inode, struct file *file)
195 return seq_open_net(inode, file, &ct_seq_ops,
196 sizeof(struct ct_iter_state));
199 static const struct file_operations ct_file_ops = {
200 .owner = THIS_MODULE,
204 .release = seq_release_net,
208 struct ct_expect_iter_state {
209 struct seq_net_private p;
213 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
215 struct net *net = seq_file_net(seq);
216 struct ct_expect_iter_state *st = seq->private;
217 struct hlist_node *n;
219 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
220 n = rcu_dereference(net->ct.expect_hash[st->bucket].first);
227 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
228 struct hlist_node *head)
230 struct net *net = seq_file_net(seq);
231 struct ct_expect_iter_state *st = seq->private;
233 head = rcu_dereference(head->next);
234 while (head == NULL) {
235 if (++st->bucket >= nf_ct_expect_hsize)
237 head = rcu_dereference(net->ct.expect_hash[st->bucket].first);
242 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
244 struct hlist_node *head = ct_expect_get_first(seq);
247 while (pos && (head = ct_expect_get_next(seq, head)))
249 return pos ? NULL : head;
252 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
256 return ct_expect_get_idx(seq, *pos);
259 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
262 return ct_expect_get_next(seq, v);
265 static void exp_seq_stop(struct seq_file *seq, void *v)
271 static int exp_seq_show(struct seq_file *s, void *v)
273 struct nf_conntrack_expect *exp;
274 const struct hlist_node *n = v;
276 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
278 if (exp->tuple.src.l3num != AF_INET)
281 if (exp->timeout.function)
282 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
283 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
287 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
289 print_tuple(s, &exp->tuple,
290 __nf_ct_l3proto_find(exp->tuple.src.l3num),
291 __nf_ct_l4proto_find(exp->tuple.src.l3num,
292 exp->tuple.dst.protonum));
293 return seq_putc(s, '\n');
296 static const struct seq_operations exp_seq_ops = {
297 .start = exp_seq_start,
298 .next = exp_seq_next,
299 .stop = exp_seq_stop,
303 static int exp_open(struct inode *inode, struct file *file)
305 return seq_open_net(inode, file, &exp_seq_ops,
306 sizeof(struct ct_expect_iter_state));
309 static const struct file_operations ip_exp_file_ops = {
310 .owner = THIS_MODULE,
314 .release = seq_release_net,
317 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
319 struct net *net = seq_file_net(seq);
323 return SEQ_START_TOKEN;
325 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
326 if (!cpu_possible(cpu))
329 return per_cpu_ptr(net->ct.stat, cpu);
335 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
337 struct net *net = seq_file_net(seq);
340 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
341 if (!cpu_possible(cpu))
344 return per_cpu_ptr(net->ct.stat, cpu);
350 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
354 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
356 struct net *net = seq_file_net(seq);
357 unsigned int nr_conntracks = atomic_read(&net->ct.count);
358 const struct ip_conntrack_stat *st = v;
360 if (v == SEQ_START_TOKEN) {
361 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");
365 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
366 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
389 static const struct seq_operations ct_cpu_seq_ops = {
390 .start = ct_cpu_seq_start,
391 .next = ct_cpu_seq_next,
392 .stop = ct_cpu_seq_stop,
393 .show = ct_cpu_seq_show,
396 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
398 return seq_open_net(inode, file, &ct_cpu_seq_ops,
399 sizeof(struct seq_net_private));
402 static const struct file_operations ct_cpu_seq_fops = {
403 .owner = THIS_MODULE,
404 .open = ct_cpu_seq_open,
407 .release = seq_release_net,
410 static int __net_init ip_conntrack_net_init(struct net *net)
412 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
414 proc = proc_net_fops_create(net, "ip_conntrack", 0440, &ct_file_ops);
418 proc_exp = proc_net_fops_create(net, "ip_conntrack_expect", 0440,
423 proc_stat = proc_create("ip_conntrack", S_IRUGO,
424 net->proc_net_stat, &ct_cpu_seq_fops);
430 proc_net_remove(net, "ip_conntrack_expect");
432 proc_net_remove(net, "ip_conntrack");
437 static void __net_exit ip_conntrack_net_exit(struct net *net)
439 remove_proc_entry("ip_conntrack", net->proc_net_stat);
440 proc_net_remove(net, "ip_conntrack_expect");
441 proc_net_remove(net, "ip_conntrack");
444 static struct pernet_operations ip_conntrack_net_ops = {
445 .init = ip_conntrack_net_init,
446 .exit = ip_conntrack_net_exit,
449 int __init nf_conntrack_ipv4_compat_init(void)
451 return register_pernet_subsys(&ip_conntrack_net_ops);
454 void __exit nf_conntrack_ipv4_compat_fini(void)
456 unregister_pernet_subsys(&ip_conntrack_net_ops);