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 {
27 static struct hlist_node *ct_get_first(struct seq_file *seq)
29 struct ct_iter_state *st = seq->private;
33 st->bucket < nf_conntrack_htable_size;
35 n = rcu_dereference(nf_conntrack_hash[st->bucket].first);
42 static struct hlist_node *ct_get_next(struct seq_file *seq,
43 struct hlist_node *head)
45 struct ct_iter_state *st = seq->private;
47 head = rcu_dereference(head->next);
48 while (head == NULL) {
49 if (++st->bucket >= nf_conntrack_htable_size)
51 head = rcu_dereference(nf_conntrack_hash[st->bucket].first);
56 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
58 struct hlist_node *head = ct_get_first(seq);
61 while (pos && (head = ct_get_next(seq, head)))
63 return pos ? NULL : head;
66 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
70 return ct_get_idx(seq, *pos);
73 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
76 return ct_get_next(s, v);
79 static void ct_seq_stop(struct seq_file *s, void *v)
85 static int ct_seq_show(struct seq_file *s, void *v)
87 const struct nf_conntrack_tuple_hash *hash = v;
88 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
89 const struct nf_conntrack_l3proto *l3proto;
90 const struct nf_conntrack_l4proto *l4proto;
94 /* we only want to print DIR_ORIGINAL */
95 if (NF_CT_DIRECTION(hash))
97 if (nf_ct_l3num(ct) != AF_INET)
100 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
101 NF_CT_ASSERT(l3proto);
102 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
103 NF_CT_ASSERT(l4proto);
105 if (seq_printf(s, "%-8s %u %ld ",
106 l4proto->name, nf_ct_protonum(ct),
107 timer_pending(&ct->timeout)
108 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
111 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
114 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
118 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
121 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
122 if (seq_printf(s, "[UNREPLIED] "))
125 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
129 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
132 if (test_bit(IPS_ASSURED_BIT, &ct->status))
133 if (seq_printf(s, "[ASSURED] "))
136 #ifdef CONFIG_NF_CONNTRACK_MARK
137 if (seq_printf(s, "mark=%u ", ct->mark))
141 #ifdef CONFIG_NF_CONNTRACK_SECMARK
142 if (seq_printf(s, "secmark=%u ", ct->secmark))
146 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
152 static const struct seq_operations ct_seq_ops = {
153 .start = ct_seq_start,
159 static int ct_open(struct inode *inode, struct file *file)
161 return seq_open_private(file, &ct_seq_ops,
162 sizeof(struct ct_iter_state));
165 static const struct file_operations ct_file_ops = {
166 .owner = THIS_MODULE,
170 .release = seq_release_private,
174 struct ct_expect_iter_state {
178 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
180 struct ct_expect_iter_state *st = seq->private;
181 struct hlist_node *n;
183 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
184 n = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
191 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
192 struct hlist_node *head)
194 struct ct_expect_iter_state *st = seq->private;
196 head = rcu_dereference(head->next);
197 while (head == NULL) {
198 if (++st->bucket >= nf_ct_expect_hsize)
200 head = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
205 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
207 struct hlist_node *head = ct_expect_get_first(seq);
210 while (pos && (head = ct_expect_get_next(seq, head)))
212 return pos ? NULL : head;
215 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
219 return ct_expect_get_idx(seq, *pos);
222 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
225 return ct_expect_get_next(seq, v);
228 static void exp_seq_stop(struct seq_file *seq, void *v)
234 static int exp_seq_show(struct seq_file *s, void *v)
236 struct nf_conntrack_expect *exp;
237 const struct hlist_node *n = v;
239 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
241 if (exp->tuple.src.l3num != AF_INET)
244 if (exp->timeout.function)
245 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
246 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
250 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
252 print_tuple(s, &exp->tuple,
253 __nf_ct_l3proto_find(exp->tuple.src.l3num),
254 __nf_ct_l4proto_find(exp->tuple.src.l3num,
255 exp->tuple.dst.protonum));
256 return seq_putc(s, '\n');
259 static const struct seq_operations exp_seq_ops = {
260 .start = exp_seq_start,
261 .next = exp_seq_next,
262 .stop = exp_seq_stop,
266 static int exp_open(struct inode *inode, struct file *file)
268 return seq_open_private(file, &exp_seq_ops,
269 sizeof(struct ct_expect_iter_state));
272 static const struct file_operations ip_exp_file_ops = {
273 .owner = THIS_MODULE,
277 .release = seq_release_private,
280 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
285 return SEQ_START_TOKEN;
287 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
288 if (!cpu_possible(cpu))
291 return &per_cpu(nf_conntrack_stat, cpu);
297 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
301 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
302 if (!cpu_possible(cpu))
305 return &per_cpu(nf_conntrack_stat, cpu);
311 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
315 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
317 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
318 const struct ip_conntrack_stat *st = v;
320 if (v == SEQ_START_TOKEN) {
321 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\n");
325 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
326 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
348 static const struct seq_operations ct_cpu_seq_ops = {
349 .start = ct_cpu_seq_start,
350 .next = ct_cpu_seq_next,
351 .stop = ct_cpu_seq_stop,
352 .show = ct_cpu_seq_show,
355 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
357 return seq_open(file, &ct_cpu_seq_ops);
360 static const struct file_operations ct_cpu_seq_fops = {
361 .owner = THIS_MODULE,
362 .open = ct_cpu_seq_open,
365 .release = seq_release,
368 int __init nf_conntrack_ipv4_compat_init(void)
370 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
372 proc = proc_net_fops_create(&init_net, "ip_conntrack", 0440, &ct_file_ops);
376 proc_exp = proc_net_fops_create(&init_net, "ip_conntrack_expect", 0440,
381 proc_stat = proc_create("ip_conntrack", S_IRUGO,
382 init_net.proc_net_stat, &ct_cpu_seq_fops);
388 proc_net_remove(&init_net, "ip_conntrack_expect");
390 proc_net_remove(&init_net, "ip_conntrack");
395 void __exit nf_conntrack_ipv4_compat_fini(void)
397 remove_proc_entry("ip_conntrack", init_net.proc_net_stat);
398 proc_net_remove(&init_net, "ip_conntrack_expect");
399 proc_net_remove(&init_net, "ip_conntrack");