[UDP]: Make full use of proto.h.udp_hash innovation.
[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;
109 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
110 return 0;
111
112 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
113 .tuple.src.l3num);
114 NF_CT_ASSERT(l3proto);
115 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
116 .tuple.src.l3num,
117 ct->tuplehash[IP_CT_DIR_ORIGINAL]
118 .tuple.dst.protonum);
119 NF_CT_ASSERT(l4proto);
120
121 if (seq_printf(s, "%-8s %u %ld ",
122 l4proto->name,
123 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
124 timer_pending(&ct->timeout)
125 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
126 return -ENOSPC;
127
c71e9167 128 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
e4bd8bce
PM
129 return -ENOSPC;
130
131 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
132 l3proto, l4proto))
133 return -ENOSPC;
134
e905a9ed 135 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
e4bd8bce
PM
136 return -ENOSPC;
137
138 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
139 if (seq_printf(s, "[UNREPLIED] "))
140 return -ENOSPC;
141
142 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
143 l3proto, l4proto))
144 return -ENOSPC;
145
e905a9ed 146 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
e4bd8bce
PM
147 return -ENOSPC;
148
149 if (test_bit(IPS_ASSURED_BIT, &ct->status))
150 if (seq_printf(s, "[ASSURED] "))
151 return -ENOSPC;
152
153#ifdef CONFIG_NF_CONNTRACK_MARK
154 if (seq_printf(s, "mark=%u ", ct->mark))
155 return -ENOSPC;
156#endif
157
158#ifdef CONFIG_NF_CONNTRACK_SECMARK
159 if (seq_printf(s, "secmark=%u ", ct->secmark))
160 return -ENOSPC;
161#endif
162
163 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
164 return -ENOSPC;
165
166 return 0;
167}
168
56b3d975 169static const struct seq_operations ct_seq_ops = {
e4bd8bce
PM
170 .start = ct_seq_start,
171 .next = ct_seq_next,
172 .stop = ct_seq_stop,
173 .show = ct_seq_show
174};
175
176static int ct_open(struct inode *inode, struct file *file)
177{
e2da5913
PE
178 return seq_open_private(file, &ct_seq_ops,
179 sizeof(struct ct_iter_state));
e4bd8bce
PM
180}
181
9a32144e 182static const struct file_operations ct_file_ops = {
e4bd8bce
PM
183 .owner = THIS_MODULE,
184 .open = ct_open,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = seq_release_private,
188};
189
190/* expects */
5d08ad44
PM
191struct ct_expect_iter_state {
192 unsigned int bucket;
193};
194
195static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
e4bd8bce 196{
5d08ad44 197 struct ct_expect_iter_state *st = seq->private;
7d0742da 198 struct hlist_node *n;
e4bd8bce 199
5d08ad44 200 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
7d0742da
PM
201 n = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
202 if (n)
203 return n;
5d08ad44
PM
204 }
205 return NULL;
206}
e4bd8bce 207
5d08ad44
PM
208static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
209 struct hlist_node *head)
210{
211 struct ct_expect_iter_state *st = seq->private;
e4bd8bce 212
7d0742da 213 head = rcu_dereference(head->next);
5d08ad44
PM
214 while (head == NULL) {
215 if (++st->bucket >= nf_ct_expect_hsize)
e4bd8bce 216 return NULL;
7d0742da 217 head = rcu_dereference(nf_ct_expect_hash[st->bucket].first);
e4bd8bce 218 }
5d08ad44 219 return head;
e4bd8bce
PM
220}
221
5d08ad44 222static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
e4bd8bce 223{
5d08ad44 224 struct hlist_node *head = ct_expect_get_first(seq);
e4bd8bce 225
5d08ad44
PM
226 if (head)
227 while (pos && (head = ct_expect_get_next(seq, head)))
228 pos--;
229 return pos ? NULL : head;
230}
e4bd8bce 231
5d08ad44 232static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 233 __acquires(RCU)
5d08ad44 234{
7d0742da 235 rcu_read_lock();
5d08ad44
PM
236 return ct_expect_get_idx(seq, *pos);
237}
e4bd8bce 238
5d08ad44
PM
239static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
240{
241 (*pos)++;
242 return ct_expect_get_next(seq, v);
e4bd8bce
PM
243}
244
5d08ad44 245static void exp_seq_stop(struct seq_file *seq, void *v)
76507f69 246 __releases(RCU)
e4bd8bce 247{
7d0742da 248 rcu_read_unlock();
e4bd8bce
PM
249}
250
251static int exp_seq_show(struct seq_file *s, void *v)
252{
5d08ad44 253 struct nf_conntrack_expect *exp;
32948588 254 const struct hlist_node *n = v;
5d08ad44
PM
255
256 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
e4bd8bce
PM
257
258 if (exp->tuple.src.l3num != AF_INET)
259 return 0;
260
261 if (exp->timeout.function)
262 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
263 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
264 else
265 seq_printf(s, "- ");
266
267 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
268
269 print_tuple(s, &exp->tuple,
270 __nf_ct_l3proto_find(exp->tuple.src.l3num),
271 __nf_ct_l4proto_find(exp->tuple.src.l3num,
e905a9ed 272 exp->tuple.dst.protonum));
e4bd8bce
PM
273 return seq_putc(s, '\n');
274}
275
56b3d975 276static const struct seq_operations exp_seq_ops = {
e4bd8bce
PM
277 .start = exp_seq_start,
278 .next = exp_seq_next,
279 .stop = exp_seq_stop,
280 .show = exp_seq_show
281};
282
283static int exp_open(struct inode *inode, struct file *file)
284{
e2da5913
PE
285 return seq_open_private(file, &exp_seq_ops,
286 sizeof(struct ct_expect_iter_state));
e4bd8bce
PM
287}
288
9a32144e 289static const struct file_operations ip_exp_file_ops = {
e4bd8bce
PM
290 .owner = THIS_MODULE,
291 .open = exp_open,
292 .read = seq_read,
293 .llseek = seq_lseek,
5d08ad44 294 .release = seq_release_private,
e4bd8bce
PM
295};
296
297static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
298{
299 int cpu;
300
301 if (*pos == 0)
302 return SEQ_START_TOKEN;
303
304 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
305 if (!cpu_possible(cpu))
306 continue;
307 *pos = cpu+1;
308 return &per_cpu(nf_conntrack_stat, cpu);
309 }
310
311 return NULL;
312}
313
314static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
315{
316 int cpu;
317
318 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
319 if (!cpu_possible(cpu))
320 continue;
321 *pos = cpu+1;
322 return &per_cpu(nf_conntrack_stat, cpu);
323 }
324
325 return NULL;
326}
327
328static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
329{
330}
331
332static int ct_cpu_seq_show(struct seq_file *seq, void *v)
333{
334 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
32948588 335 const struct ip_conntrack_stat *st = v;
e4bd8bce
PM
336
337 if (v == SEQ_START_TOKEN) {
338 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");
339 return 0;
340 }
341
342 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
343 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
344 nr_conntracks,
345 st->searched,
346 st->found,
347 st->new,
348 st->invalid,
349 st->ignore,
350 st->delete,
351 st->delete_list,
352 st->insert,
353 st->insert_failed,
354 st->drop,
355 st->early_drop,
356 st->error,
357
358 st->expect_new,
359 st->expect_create,
360 st->expect_delete
361 );
362 return 0;
363}
364
56b3d975 365static const struct seq_operations ct_cpu_seq_ops = {
e4bd8bce
PM
366 .start = ct_cpu_seq_start,
367 .next = ct_cpu_seq_next,
368 .stop = ct_cpu_seq_stop,
369 .show = ct_cpu_seq_show,
370};
371
372static int ct_cpu_seq_open(struct inode *inode, struct file *file)
373{
374 return seq_open(file, &ct_cpu_seq_ops);
375}
376
9a32144e 377static const struct file_operations ct_cpu_seq_fops = {
e4bd8bce
PM
378 .owner = THIS_MODULE,
379 .open = ct_cpu_seq_open,
380 .read = seq_read,
381 .llseek = seq_lseek,
665bba10 382 .release = seq_release,
e4bd8bce
PM
383};
384
385int __init nf_conntrack_ipv4_compat_init(void)
386{
387 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
388
457c4cbc 389 proc = proc_net_fops_create(&init_net, "ip_conntrack", 0440, &ct_file_ops);
e4bd8bce
PM
390 if (!proc)
391 goto err1;
392
457c4cbc 393 proc_exp = proc_net_fops_create(&init_net, "ip_conntrack_expect", 0440,
e4bd8bce
PM
394 &ip_exp_file_ops);
395 if (!proc_exp)
396 goto err2;
397
457c4cbc 398 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, init_net.proc_net_stat);
e4bd8bce
PM
399 if (!proc_stat)
400 goto err3;
401
402 proc_stat->proc_fops = &ct_cpu_seq_fops;
403 proc_stat->owner = THIS_MODULE;
404
405 return 0;
406
407err3:
457c4cbc 408 proc_net_remove(&init_net, "ip_conntrack_expect");
e4bd8bce 409err2:
457c4cbc 410 proc_net_remove(&init_net, "ip_conntrack");
e4bd8bce
PM
411err1:
412 return -ENOMEM;
413}
414
415void __exit nf_conntrack_ipv4_compat_fini(void)
416{
457c4cbc
EB
417 remove_proc_entry("ip_conntrack", init_net.proc_net_stat);
418 proc_net_remove(&init_net, "ip_conntrack_expect");
419 proc_net_remove(&init_net, "ip_conntrack");
e4bd8bce 420}
This page took 0.271303 seconds and 5 git commands to generate.