drivers/misc: elide a non-zero test on a result that is never 0
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / netfilter / nf_conntrack_standalone.c
blobb59871f6bdda3b6bcfddfcd24925df64e70e15f2
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/types.h>
10 #include <linux/netfilter.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/percpu.h>
16 #include <linux/netdevice.h>
17 #include <net/net_namespace.h>
18 #ifdef CONFIG_SYSCTL
19 #include <linux/sysctl.h>
20 #endif
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_l3proto.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_expect.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
29 MODULE_LICENSE("GPL");
31 #ifdef CONFIG_PROC_FS
32 int
33 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
34 const struct nf_conntrack_l3proto *l3proto,
35 const struct nf_conntrack_l4proto *l4proto)
37 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
39 EXPORT_SYMBOL_GPL(print_tuple);
41 #ifdef CONFIG_NF_CT_ACCT
42 static unsigned int
43 seq_print_counters(struct seq_file *s,
44 const struct ip_conntrack_counter *counter)
46 return seq_printf(s, "packets=%llu bytes=%llu ",
47 (unsigned long long)counter->packets,
48 (unsigned long long)counter->bytes);
50 #else
51 #define seq_print_counters(x, y) 0
52 #endif
54 struct ct_iter_state {
55 unsigned int bucket;
58 static struct hlist_node *ct_get_first(struct seq_file *seq)
60 struct ct_iter_state *st = seq->private;
61 struct hlist_node *n;
63 for (st->bucket = 0;
64 st->bucket < nf_conntrack_htable_size;
65 st->bucket++) {
66 n = rcu_dereference(nf_conntrack_hash[st->bucket].first);
67 if (n)
68 return n;
70 return NULL;
73 static struct hlist_node *ct_get_next(struct seq_file *seq,
74 struct hlist_node *head)
76 struct ct_iter_state *st = seq->private;
78 head = rcu_dereference(head->next);
79 while (head == NULL) {
80 if (++st->bucket >= nf_conntrack_htable_size)
81 return NULL;
82 head = rcu_dereference(nf_conntrack_hash[st->bucket].first);
84 return head;
87 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
89 struct hlist_node *head = ct_get_first(seq);
91 if (head)
92 while (pos && (head = ct_get_next(seq, head)))
93 pos--;
94 return pos ? NULL : head;
97 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
98 __acquires(RCU)
100 rcu_read_lock();
101 return ct_get_idx(seq, *pos);
104 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
106 (*pos)++;
107 return ct_get_next(s, v);
110 static void ct_seq_stop(struct seq_file *s, void *v)
111 __releases(RCU)
113 rcu_read_unlock();
116 /* return 0 on success, 1 in case of error */
117 static int ct_seq_show(struct seq_file *s, void *v)
119 const struct nf_conntrack_tuple_hash *hash = v;
120 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
121 const struct nf_conntrack_l3proto *l3proto;
122 const struct nf_conntrack_l4proto *l4proto;
124 NF_CT_ASSERT(ct);
126 /* we only want to print DIR_ORIGINAL */
127 if (NF_CT_DIRECTION(hash))
128 return 0;
130 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
131 NF_CT_ASSERT(l3proto);
132 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
133 NF_CT_ASSERT(l4proto);
135 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
136 l3proto->name, nf_ct_l3num(ct),
137 l4proto->name, nf_ct_protonum(ct),
138 timer_pending(&ct->timeout)
139 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
140 return -ENOSPC;
142 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
143 return -ENOSPC;
145 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
146 l3proto, l4proto))
147 return -ENOSPC;
149 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
150 return -ENOSPC;
152 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
153 if (seq_printf(s, "[UNREPLIED] "))
154 return -ENOSPC;
156 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
157 l3proto, l4proto))
158 return -ENOSPC;
160 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
161 return -ENOSPC;
163 if (test_bit(IPS_ASSURED_BIT, &ct->status))
164 if (seq_printf(s, "[ASSURED] "))
165 return -ENOSPC;
167 #if defined(CONFIG_NF_CONNTRACK_MARK)
168 if (seq_printf(s, "mark=%u ", ct->mark))
169 return -ENOSPC;
170 #endif
172 #ifdef CONFIG_NF_CONNTRACK_SECMARK
173 if (seq_printf(s, "secmark=%u ", ct->secmark))
174 return -ENOSPC;
175 #endif
177 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
178 return -ENOSPC;
180 return 0;
183 static const struct seq_operations ct_seq_ops = {
184 .start = ct_seq_start,
185 .next = ct_seq_next,
186 .stop = ct_seq_stop,
187 .show = ct_seq_show
190 static int ct_open(struct inode *inode, struct file *file)
192 return seq_open_private(file, &ct_seq_ops,
193 sizeof(struct ct_iter_state));
196 static const struct file_operations ct_file_ops = {
197 .owner = THIS_MODULE,
198 .open = ct_open,
199 .read = seq_read,
200 .llseek = seq_lseek,
201 .release = seq_release_private,
204 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
206 int cpu;
208 if (*pos == 0)
209 return SEQ_START_TOKEN;
211 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
212 if (!cpu_possible(cpu))
213 continue;
214 *pos = cpu + 1;
215 return &per_cpu(nf_conntrack_stat, cpu);
218 return NULL;
221 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
223 int cpu;
225 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
226 if (!cpu_possible(cpu))
227 continue;
228 *pos = cpu + 1;
229 return &per_cpu(nf_conntrack_stat, cpu);
232 return NULL;
235 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
239 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
241 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
242 const struct ip_conntrack_stat *st = v;
244 if (v == SEQ_START_TOKEN) {
245 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");
246 return 0;
249 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
250 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
251 nr_conntracks,
252 st->searched,
253 st->found,
254 st->new,
255 st->invalid,
256 st->ignore,
257 st->delete,
258 st->delete_list,
259 st->insert,
260 st->insert_failed,
261 st->drop,
262 st->early_drop,
263 st->error,
265 st->expect_new,
266 st->expect_create,
267 st->expect_delete
269 return 0;
272 static const struct seq_operations ct_cpu_seq_ops = {
273 .start = ct_cpu_seq_start,
274 .next = ct_cpu_seq_next,
275 .stop = ct_cpu_seq_stop,
276 .show = ct_cpu_seq_show,
279 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
281 return seq_open(file, &ct_cpu_seq_ops);
284 static const struct file_operations ct_cpu_seq_fops = {
285 .owner = THIS_MODULE,
286 .open = ct_cpu_seq_open,
287 .read = seq_read,
288 .llseek = seq_lseek,
289 .release = seq_release,
292 static int nf_conntrack_standalone_init_proc(void)
294 struct proc_dir_entry *pde;
296 pde = proc_net_fops_create(&init_net, "nf_conntrack", 0440, &ct_file_ops);
297 if (!pde)
298 goto out_nf_conntrack;
299 pde = create_proc_entry("nf_conntrack", S_IRUGO, init_net.proc_net_stat);
300 if (!pde)
301 goto out_stat_nf_conntrack;
302 pde->proc_fops = &ct_cpu_seq_fops;
303 pde->owner = THIS_MODULE;
304 return 0;
306 out_stat_nf_conntrack:
307 proc_net_remove(&init_net, "nf_conntrack");
308 out_nf_conntrack:
309 return -ENOMEM;
312 static void nf_conntrack_standalone_fini_proc(void)
314 remove_proc_entry("nf_conntrack", init_net.proc_net_stat);
315 proc_net_remove(&init_net, "nf_conntrack");
317 #else
318 static int nf_conntrack_standalone_init_proc(void)
320 return 0;
323 static void nf_conntrack_standalone_fini_proc(void)
326 #endif /* CONFIG_PROC_FS */
328 /* Sysctl support */
330 int nf_conntrack_checksum __read_mostly = 1;
331 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
333 #ifdef CONFIG_SYSCTL
334 /* Log invalid packets of a given protocol */
335 static int log_invalid_proto_min = 0;
336 static int log_invalid_proto_max = 255;
338 static struct ctl_table_header *nf_ct_sysctl_header;
340 static ctl_table nf_ct_sysctl_table[] = {
342 .ctl_name = NET_NF_CONNTRACK_MAX,
343 .procname = "nf_conntrack_max",
344 .data = &nf_conntrack_max,
345 .maxlen = sizeof(int),
346 .mode = 0644,
347 .proc_handler = &proc_dointvec,
350 .ctl_name = NET_NF_CONNTRACK_COUNT,
351 .procname = "nf_conntrack_count",
352 .data = &nf_conntrack_count,
353 .maxlen = sizeof(int),
354 .mode = 0444,
355 .proc_handler = &proc_dointvec,
358 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
359 .procname = "nf_conntrack_buckets",
360 .data = &nf_conntrack_htable_size,
361 .maxlen = sizeof(unsigned int),
362 .mode = 0444,
363 .proc_handler = &proc_dointvec,
366 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
367 .procname = "nf_conntrack_checksum",
368 .data = &nf_conntrack_checksum,
369 .maxlen = sizeof(unsigned int),
370 .mode = 0644,
371 .proc_handler = &proc_dointvec,
374 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
375 .procname = "nf_conntrack_log_invalid",
376 .data = &nf_ct_log_invalid,
377 .maxlen = sizeof(unsigned int),
378 .mode = 0644,
379 .proc_handler = &proc_dointvec_minmax,
380 .strategy = &sysctl_intvec,
381 .extra1 = &log_invalid_proto_min,
382 .extra2 = &log_invalid_proto_max,
385 .ctl_name = CTL_UNNUMBERED,
386 .procname = "nf_conntrack_expect_max",
387 .data = &nf_ct_expect_max,
388 .maxlen = sizeof(int),
389 .mode = 0644,
390 .proc_handler = &proc_dointvec,
392 { .ctl_name = 0 }
395 #define NET_NF_CONNTRACK_MAX 2089
397 static ctl_table nf_ct_netfilter_table[] = {
399 .ctl_name = NET_NETFILTER,
400 .procname = "netfilter",
401 .mode = 0555,
402 .child = nf_ct_sysctl_table,
405 .ctl_name = NET_NF_CONNTRACK_MAX,
406 .procname = "nf_conntrack_max",
407 .data = &nf_conntrack_max,
408 .maxlen = sizeof(int),
409 .mode = 0644,
410 .proc_handler = &proc_dointvec,
412 { .ctl_name = 0 }
415 static struct ctl_path nf_ct_path[] = {
416 { .procname = "net", .ctl_name = CTL_NET, },
420 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
422 static int nf_conntrack_standalone_init_sysctl(void)
424 nf_ct_sysctl_header =
425 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
426 if (nf_ct_sysctl_header == NULL) {
427 printk("nf_conntrack: can't register to sysctl.\n");
428 return -ENOMEM;
430 return 0;
434 static void nf_conntrack_standalone_fini_sysctl(void)
436 unregister_sysctl_table(nf_ct_sysctl_header);
438 #else
439 static int nf_conntrack_standalone_init_sysctl(void)
441 return 0;
444 static void nf_conntrack_standalone_fini_sysctl(void)
447 #endif /* CONFIG_SYSCTL */
449 static int __init nf_conntrack_standalone_init(void)
451 int ret;
453 ret = nf_conntrack_init();
454 if (ret < 0)
455 goto out;
456 ret = nf_conntrack_standalone_init_proc();
457 if (ret < 0)
458 goto out_proc;
459 ret = nf_conntrack_standalone_init_sysctl();
460 if (ret < 0)
461 goto out_sysctl;
462 return 0;
464 out_sysctl:
465 nf_conntrack_standalone_fini_proc();
466 out_proc:
467 nf_conntrack_cleanup();
468 out:
469 return ret;
472 static void __exit nf_conntrack_standalone_fini(void)
474 nf_conntrack_standalone_fini_sysctl();
475 nf_conntrack_standalone_fini_proc();
476 nf_conntrack_cleanup();
479 module_init(nf_conntrack_standalone_init);
480 module_exit(nf_conntrack_standalone_fini);
482 /* Some modules need us, but don't depend directly on any symbol.
483 They should call this. */
484 void need_conntrack(void)
487 EXPORT_SYMBOL_GPL(need_conntrack);