Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[deliverable/linux.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4_compat.c
CommitLineData
e4bd8bce
PM
1/* ip_conntrack proc compat - based on ip_conntrack_standalone.c
2 *
3 * (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 *
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.
9 */
10#include <linux/types.h>
11#include <linux/proc_fs.h>
12#include <linux/seq_file.h>
13#include <linux/percpu.h>
457c4cbc 14#include <net/net_namespace.h>
e4bd8bce
PM
15
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
e4bd8bce
PM
22#ifdef CONFIG_NF_CT_ACCT
23static unsigned int
24seq_print_counters(struct seq_file *s,
25 const struct ip_conntrack_counter *counter)
26{
27 return seq_printf(s, "packets=%llu bytes=%llu ",
28 (unsigned long long)counter->packets,
29 (unsigned long long)counter->bytes);
30}
31#else
32#define seq_print_counters(x, y) 0
33#endif
34
35struct ct_iter_state {
36 unsigned int bucket;
37};
38
f205c5e0 39static struct hlist_node *ct_get_first(struct seq_file *seq)
e4bd8bce
PM
40{
41 struct ct_iter_state *st = seq->private;
76507f69 42 struct hlist_node *n;
e4bd8bce
PM
43
44 for (st->bucket = 0;
45 st->bucket < nf_conntrack_htable_size;
46 st->bucket++) {
76507f69
PM
47 n = rcu_dereference(nf_conntrack_hash[st->bucket].first);
48 if (n)
49 return n;
e4bd8bce
PM
50 }
51 return NULL;
52}
53
f205c5e0
PM
54static struct hlist_node *ct_get_next(struct seq_file *seq,
55 struct hlist_node *head)
e4bd8bce
PM
56{
57 struct ct_iter_state *st = seq->private;
58
76507f69 59 head = rcu_dereference(head->next);
f205c5e0 60 while (head == NULL) {
e4bd8bce
PM
61 if (++st->bucket >= nf_conntrack_htable_size)
62 return NULL;
76507f69 63 head = rcu_dereference(nf_conntrack_hash[st->bucket].first);
e4bd8bce
PM
64 }
65 return head;
66}
67
f205c5e0 68static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
e4bd8bce 69{
f205c5e0 70 struct hlist_node *head = ct_get_first(seq);
e4bd8bce
PM
71
72 if (head)
73 while (pos && (head = ct_get_next(seq, head)))
74 pos--;
75 return pos ? NULL : head;
76}
77
78static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 79 __acquires(RCU)
e4bd8bce 80{
76507f69 81 rcu_read_lock();
e4bd8bce
PM
82 return ct_get_idx(seq, *pos);
83}
84
85static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
86{
87 (*pos)++;
88 return ct_get_next(s, v);
89}
90
91static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 92 __releases(RCU)
e4bd8bce 93{
76507f69 94 rcu_read_unlock();
e4bd8bce
PM
95}
96
97static int ct_seq_show(struct seq_file *s, void *v)
98{
99 const struct nf_conntrack_tuple_hash *hash = v;
100 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
101 const struct nf_conntrack_l3proto *l3proto;
102 const struct nf_conntrack_l4proto *l4proto;
e4bd8bce
PM
103
104 NF_CT_ASSERT(ct);
105
106 /* we only want to print DIR_ORIGINAL */
107 if (NF_CT_DIRECTION(hash))
108 return 0;
5e8fbe2a 109 if (nf_ct_l3num(ct) != AF_INET)
e4bd8bce
PM
110 return 0;
111
5e8fbe2a 112 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
e4bd8bce 113 NF_CT_ASSERT(l3proto);
5e8fbe2a 114 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
e4bd8bce
PM
115 NF_CT_ASSERT(l4proto);
116
117 if (seq_printf(s, "%-8s %u %ld ",
5e8fbe2a 118 l4proto->name, nf_ct_protonum(ct),
e4bd8bce
PM
119 timer_pending(&ct->timeout)
120 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
121 return -ENOSPC;
122
c71e9167 123 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
e4bd8bce
PM
124 return -ENOSPC;
125
126 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
127 l3proto, l4proto))
128 return -ENOSPC;
129
e905a9ed 130 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
e4bd8bce
PM
131 return -ENOSPC;
132
133 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
134 if (seq_printf(s, "[UNREPLIED] "))
135 return -ENOSPC;
136
137 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
138 l3proto, l4proto))
139 return -ENOSPC;
140
e905a9ed 141 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
e4bd8bce
PM
142 return -ENOSPC;
143
144 if (test_bit(IPS_ASSURED_BIT, &ct->status))
145 if (seq_printf(s, "[ASSURED] "))
146 return -ENOSPC;
147
148#ifdef CONFIG_NF_CONNTRACK_MARK
149 if (seq_printf(s, "mark=%u ", ct->mark))
150 return -ENOSPC;
151#endif
152
153#ifdef CONFIG_NF_CONNTRACK_SECMARK
154 if (seq_printf(s, "secmark=%u ", ct->secmark))
155 return -ENOSPC;
156#endif
157
158 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
159 return -ENOSPC;
160
161 return 0;
162}
163
56b3d975 164static const struct seq_operations ct_seq_ops = {
e4bd8bce
PM
165 .start = ct_seq_start,
166 .next = ct_seq_next,
167 .stop = ct_seq_stop,
168 .show = ct_seq_show
169};
170
171static int ct_open(struct inode *inode, struct file *file)
172{
e2da5913
PE
173 return seq_open_private(file, &ct_seq_ops,
174 sizeof(struct ct_iter_state));
e4bd8bce
PM
175}
176
9a32144e 177static const struct file_operations ct_file_ops = {
e4bd8bce
PM
178 .owner = THIS_MODULE,
179 .open = ct_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = seq_release_private,
183};
184
185/* expects */
5d08ad44
PM
186struct ct_expect_iter_state {
187 unsigned int bucket;
188};
189
190static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
e4bd8bce 191{
5d08ad44 192 struct ct_expect_iter_state *st = seq->private;
7d0742da 193 struct hlist_node *n;
e4bd8bce 194
5d08ad44 195 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
7d0742da
PM
196 n = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
197 if (n)
198 return n;
5d08ad44
PM
199 }
200 return NULL;
201}
e4bd8bce 202
5d08ad44
PM
203static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
204 struct hlist_node *head)
205{
206 struct ct_expect_iter_state *st = seq->private;
e4bd8bce 207
7d0742da 208 head = rcu_dereference(head->next);
5d08ad44
PM
209 while (head == NULL) {
210 if (++st->bucket >= nf_ct_expect_hsize)
e4bd8bce 211 return NULL;
7d0742da 212 head = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
e4bd8bce 213 }
5d08ad44 214 return head;
e4bd8bce
PM
215}
216
5d08ad44 217static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
e4bd8bce 218{
5d08ad44 219 struct hlist_node *head = ct_expect_get_first(seq);
e4bd8bce 220
5d08ad44
PM
221 if (head)
222 while (pos && (head = ct_expect_get_next(seq, head)))
223 pos--;
224 return pos ? NULL : head;
225}
e4bd8bce 226
5d08ad44 227static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 228 __acquires(RCU)
5d08ad44 229{
7d0742da 230 rcu_read_lock();
5d08ad44
PM
231 return ct_expect_get_idx(seq, *pos);
232}
e4bd8bce 233
5d08ad44
PM
234static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
235{
236 (*pos)++;
237 return ct_expect_get_next(seq, v);
e4bd8bce
PM
238}
239
5d08ad44 240static void exp_seq_stop(struct seq_file *seq, void *v)
76507f69 241 __releases(RCU)
e4bd8bce 242{
7d0742da 243 rcu_read_unlock();
e4bd8bce
PM
244}
245
246static int exp_seq_show(struct seq_file *s, void *v)
247{
5d08ad44 248 struct nf_conntrack_expect *exp;
32948588 249 const struct hlist_node *n = v;
5d08ad44
PM
250
251 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
e4bd8bce
PM
252
253 if (exp->tuple.src.l3num != AF_INET)
254 return 0;
255
256 if (exp->timeout.function)
257 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
258 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
259 else
260 seq_printf(s, "- ");
261
262 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
263
264 print_tuple(s, &exp->tuple,
265 __nf_ct_l3proto_find(exp->tuple.src.l3num),
266 __nf_ct_l4proto_find(exp->tuple.src.l3num,
e905a9ed 267 exp->tuple.dst.protonum));
e4bd8bce
PM
268 return seq_putc(s, '\n');
269}
270
56b3d975 271static const struct seq_operations exp_seq_ops = {
e4bd8bce
PM
272 .start = exp_seq_start,
273 .next = exp_seq_next,
274 .stop = exp_seq_stop,
275 .show = exp_seq_show
276};
277
278static int exp_open(struct inode *inode, struct file *file)
279{
e2da5913
PE
280 return seq_open_private(file, &exp_seq_ops,
281 sizeof(struct ct_expect_iter_state));
e4bd8bce
PM
282}
283
9a32144e 284static const struct file_operations ip_exp_file_ops = {
e4bd8bce
PM
285 .owner = THIS_MODULE,
286 .open = exp_open,
287 .read = seq_read,
288 .llseek = seq_lseek,
5d08ad44 289 .release = seq_release_private,
e4bd8bce
PM
290};
291
292static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
293{
294 int cpu;
295
296 if (*pos == 0)
297 return SEQ_START_TOKEN;
298
299 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
300 if (!cpu_possible(cpu))
301 continue;
302 *pos = cpu+1;
303 return &per_cpu(nf_conntrack_stat, cpu);
304 }
305
306 return NULL;
307}
308
309static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
310{
311 int cpu;
312
313 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
314 if (!cpu_possible(cpu))
315 continue;
316 *pos = cpu+1;
317 return &per_cpu(nf_conntrack_stat, cpu);
318 }
319
320 return NULL;
321}
322
323static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
324{
325}
326
327static int ct_cpu_seq_show(struct seq_file *seq, void *v)
328{
329 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
32948588 330 const struct ip_conntrack_stat *st = v;
e4bd8bce
PM
331
332 if (v == SEQ_START_TOKEN) {
333 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");
334 return 0;
335 }
336
337 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
338 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
339 nr_conntracks,
340 st->searched,
341 st->found,
342 st->new,
343 st->invalid,
344 st->ignore,
345 st->delete,
346 st->delete_list,
347 st->insert,
348 st->insert_failed,
349 st->drop,
350 st->early_drop,
351 st->error,
352
353 st->expect_new,
354 st->expect_create,
355 st->expect_delete
356 );
357 return 0;
358}
359
56b3d975 360static const struct seq_operations ct_cpu_seq_ops = {
e4bd8bce
PM
361 .start = ct_cpu_seq_start,
362 .next = ct_cpu_seq_next,
363 .stop = ct_cpu_seq_stop,
364 .show = ct_cpu_seq_show,
365};
366
367static int ct_cpu_seq_open(struct inode *inode, struct file *file)
368{
369 return seq_open(file, &ct_cpu_seq_ops);
370}
371
9a32144e 372static const struct file_operations ct_cpu_seq_fops = {
e4bd8bce
PM
373 .owner = THIS_MODULE,
374 .open = ct_cpu_seq_open,
375 .read = seq_read,
376 .llseek = seq_lseek,
665bba10 377 .release = seq_release,
e4bd8bce
PM
378};
379
380int __init nf_conntrack_ipv4_compat_init(void)
381{
382 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
383
457c4cbc 384 proc = proc_net_fops_create(&init_net, "ip_conntrack", 0440, &ct_file_ops);
e4bd8bce
PM
385 if (!proc)
386 goto err1;
387
457c4cbc 388 proc_exp = proc_net_fops_create(&init_net, "ip_conntrack_expect", 0440,
e4bd8bce
PM
389 &ip_exp_file_ops);
390 if (!proc_exp)
391 goto err2;
392
8eeee8b1
DL
393 proc_stat = proc_create("ip_conntrack", S_IRUGO,
394 init_net.proc_net_stat, &ct_cpu_seq_fops);
e4bd8bce
PM
395 if (!proc_stat)
396 goto err3;
e4bd8bce
PM
397 return 0;
398
399err3:
457c4cbc 400 proc_net_remove(&init_net, "ip_conntrack_expect");
e4bd8bce 401err2:
457c4cbc 402 proc_net_remove(&init_net, "ip_conntrack");
e4bd8bce
PM
403err1:
404 return -ENOMEM;
405}
406
407void __exit nf_conntrack_ipv4_compat_fini(void)
408{
457c4cbc
EB
409 remove_proc_entry("ip_conntrack", init_net.proc_net_stat);
410 proc_net_remove(&init_net, "ip_conntrack_expect");
411 proc_net_remove(&init_net, "ip_conntrack");
e4bd8bce 412}
This page took 0.237581 seconds and 5 git commands to generate.