NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4_compat.c
blob45339eb1bd85bcfa2ae21209bd873d57596aeb48
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.
9 */
10 #include <linux/types.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/percpu.h>
15 #include <linux/netfilter.h>
16 #include <net/netfilter/nf_conntrack_core.h>
17 #include <net/netfilter/nf_conntrack_l3proto.h>
18 #include <net/netfilter/nf_conntrack_l4proto.h>
19 #include <net/netfilter/nf_conntrack_expect.h>
21 #if 0
22 #define DEBUGP printk
23 #else
24 #define DEBUGP(format, args...)
25 #endif
27 #ifdef CONFIG_NF_CT_ACCT
28 static unsigned int
29 seq_print_counters(struct seq_file *s,
30 const struct ip_conntrack_counter *counter)
32 return seq_printf(s, "packets=%llu bytes=%llu ",
33 (unsigned long long)counter->packets,
34 (unsigned long long)counter->bytes);
36 #else
37 #define seq_print_counters(x, y) 0
38 #endif
40 struct ct_iter_state {
41 unsigned int bucket;
44 static struct list_head *ct_get_first(struct seq_file *seq)
46 struct ct_iter_state *st = seq->private;
48 for (st->bucket = 0;
49 st->bucket < nf_conntrack_htable_size;
50 st->bucket++) {
51 if (!list_empty(&nf_conntrack_hash[st->bucket]))
52 return nf_conntrack_hash[st->bucket].next;
54 return NULL;
57 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
59 struct ct_iter_state *st = seq->private;
61 head = head->next;
62 while (head == &nf_conntrack_hash[st->bucket]) {
63 if (++st->bucket >= nf_conntrack_htable_size)
64 return NULL;
65 head = nf_conntrack_hash[st->bucket].next;
67 return head;
70 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
72 struct list_head *head = ct_get_first(seq);
74 if (head)
75 while (pos && (head = ct_get_next(seq, head)))
76 pos--;
77 return pos ? NULL : head;
80 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
82 read_lock_bh(&nf_conntrack_lock);
83 return ct_get_idx(seq, *pos);
86 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
88 (*pos)++;
89 return ct_get_next(s, v);
92 static void ct_seq_stop(struct seq_file *s, void *v)
94 read_unlock_bh(&nf_conntrack_lock);
97 static int ct_seq_show(struct seq_file *s, void *v)
99 const struct nf_conntrack_tuple_hash *hash = v;
100 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
101 struct nf_conntrack_l3proto *l3proto;
102 struct nf_conntrack_l4proto *l4proto;
104 NF_CT_ASSERT(ct);
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;
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);
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;
128 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
129 return -ENOSPC;
131 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
132 l3proto, l4proto))
133 return -ENOSPC;
135 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
136 return -ENOSPC;
138 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
139 if (seq_printf(s, "[UNREPLIED] "))
140 return -ENOSPC;
142 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
143 l3proto, l4proto))
144 return -ENOSPC;
146 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
147 return -ENOSPC;
149 if (test_bit(IPS_ASSURED_BIT, &ct->status))
150 if (seq_printf(s, "[ASSURED] "))
151 return -ENOSPC;
153 #ifdef CONFIG_NF_CONNTRACK_MARK
154 if (seq_printf(s, "mark=%u ", ct->mark))
155 return -ENOSPC;
156 #endif
158 #ifdef CONFIG_NF_CONNTRACK_SECMARK
159 if (seq_printf(s, "secmark=%u ", ct->secmark))
160 return -ENOSPC;
161 #endif
163 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
164 return -ENOSPC;
166 return 0;
169 static struct seq_operations ct_seq_ops = {
170 .start = ct_seq_start,
171 .next = ct_seq_next,
172 .stop = ct_seq_stop,
173 .show = ct_seq_show
176 static int ct_open(struct inode *inode, struct file *file)
178 return seq_open_private(file, &ct_seq_ops,
179 sizeof(struct ct_iter_state));
182 static const struct file_operations ct_file_ops = {
183 .owner = THIS_MODULE,
184 .open = ct_open,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = seq_release_private,
190 /* expects */
191 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
193 struct list_head *e = &nf_conntrack_expect_list;
194 loff_t i;
196 /* strange seq_file api calls stop even if we fail,
197 * thus we need to grab lock since stop unlocks */
198 read_lock_bh(&nf_conntrack_lock);
200 if (list_empty(e))
201 return NULL;
203 for (i = 0; i <= *pos; i++) {
204 e = e->next;
205 if (e == &nf_conntrack_expect_list)
206 return NULL;
208 return e;
211 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
213 struct list_head *e = v;
215 ++*pos;
216 e = e->next;
218 if (e == &nf_conntrack_expect_list)
219 return NULL;
221 return e;
224 static void exp_seq_stop(struct seq_file *s, void *v)
226 read_unlock_bh(&nf_conntrack_lock);
229 static int exp_seq_show(struct seq_file *s, void *v)
231 struct nf_conntrack_expect *exp = v;
233 if (exp->tuple.src.l3num != AF_INET)
234 return 0;
236 if (exp->timeout.function)
237 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
238 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
239 else
240 seq_printf(s, "- ");
242 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
244 print_tuple(s, &exp->tuple,
245 __nf_ct_l3proto_find(exp->tuple.src.l3num),
246 __nf_ct_l4proto_find(exp->tuple.src.l3num,
247 exp->tuple.dst.protonum));
248 return seq_putc(s, '\n');
251 static struct seq_operations exp_seq_ops = {
252 .start = exp_seq_start,
253 .next = exp_seq_next,
254 .stop = exp_seq_stop,
255 .show = exp_seq_show
258 static int exp_open(struct inode *inode, struct file *file)
260 return seq_open(file, &exp_seq_ops);
263 static const struct file_operations ip_exp_file_ops = {
264 .owner = THIS_MODULE,
265 .open = exp_open,
266 .read = seq_read,
267 .llseek = seq_lseek,
268 .release = seq_release
271 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
273 int cpu;
275 if (*pos == 0)
276 return SEQ_START_TOKEN;
278 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
279 if (!cpu_possible(cpu))
280 continue;
281 *pos = cpu+1;
282 return &per_cpu(nf_conntrack_stat, cpu);
285 return NULL;
288 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
290 int cpu;
292 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
293 if (!cpu_possible(cpu))
294 continue;
295 *pos = cpu+1;
296 return &per_cpu(nf_conntrack_stat, cpu);
299 return NULL;
302 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
306 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
308 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
309 struct ip_conntrack_stat *st = v;
311 if (v == SEQ_START_TOKEN) {
312 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");
313 return 0;
316 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
317 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
318 nr_conntracks,
319 st->searched,
320 st->found,
321 st->new,
322 st->invalid,
323 st->ignore,
324 st->delete,
325 st->delete_list,
326 st->insert,
327 st->insert_failed,
328 st->drop,
329 st->early_drop,
330 st->error,
332 st->expect_new,
333 st->expect_create,
334 st->expect_delete
336 return 0;
339 static struct seq_operations ct_cpu_seq_ops = {
340 .start = ct_cpu_seq_start,
341 .next = ct_cpu_seq_next,
342 .stop = ct_cpu_seq_stop,
343 .show = ct_cpu_seq_show,
346 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
348 return seq_open(file, &ct_cpu_seq_ops);
351 static const struct file_operations ct_cpu_seq_fops = {
352 .owner = THIS_MODULE,
353 .open = ct_cpu_seq_open,
354 .read = seq_read,
355 .llseek = seq_lseek,
356 .release = seq_release_private,
359 int __init nf_conntrack_ipv4_compat_init(void)
361 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
363 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
364 if (!proc)
365 goto err1;
367 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
368 &ip_exp_file_ops);
369 if (!proc_exp)
370 goto err2;
372 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
373 if (!proc_stat)
374 goto err3;
376 proc_stat->proc_fops = &ct_cpu_seq_fops;
377 proc_stat->owner = THIS_MODULE;
379 return 0;
381 err3:
382 proc_net_remove("ip_conntrack_expect");
383 err2:
384 proc_net_remove("ip_conntrack");
385 err1:
386 return -ENOMEM;
389 void __exit nf_conntrack_ipv4_compat_fini(void)
391 remove_proc_entry("ip_conntrack", proc_net_stat);
392 proc_net_remove("ip_conntrack_expect");
393 proc_net_remove("ip_conntrack");