[NETFILTER]: Switch nf_register_hook/nf_unregister_hook to mutex
[linux-2.6.22.y-op.git] / net / netfilter / nf_conntrack_standalone.c
blob04ac12431db7103f1653deaafe5e6e3b8fcab8b6
1 /* This file contains all the functions required for the standalone
2 nf_conntrack module.
4 These are not required by the compatibility layer.
5 */
7 /* (C) 1999-2001 Paul `Rusty' Russell
8 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
15 * - generalize L3 protocol dependent part.
17 * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
20 #include <linux/types.h>
21 #include <linux/netfilter.h>
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/percpu.h>
27 #include <linux/netdevice.h>
28 #ifdef CONFIG_SYSCTL
29 #include <linux/sysctl.h>
30 #endif
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_l3proto.h>
35 #include <net/netfilter/nf_conntrack_l4proto.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
39 #if 0
40 #define DEBUGP printk
41 #else
42 #define DEBUGP(format, args...)
43 #endif
45 MODULE_LICENSE("GPL");
47 #ifdef CONFIG_PROC_FS
48 int
49 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
50 struct nf_conntrack_l3proto *l3proto,
51 struct nf_conntrack_l4proto *l4proto)
53 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
55 EXPORT_SYMBOL_GPL(print_tuple);
57 #ifdef CONFIG_NF_CT_ACCT
58 static unsigned int
59 seq_print_counters(struct seq_file *s,
60 const struct ip_conntrack_counter *counter)
62 return seq_printf(s, "packets=%llu bytes=%llu ",
63 (unsigned long long)counter->packets,
64 (unsigned long long)counter->bytes);
66 #else
67 #define seq_print_counters(x, y) 0
68 #endif
70 struct ct_iter_state {
71 unsigned int bucket;
74 static struct list_head *ct_get_first(struct seq_file *seq)
76 struct ct_iter_state *st = seq->private;
78 for (st->bucket = 0;
79 st->bucket < nf_conntrack_htable_size;
80 st->bucket++) {
81 if (!list_empty(&nf_conntrack_hash[st->bucket]))
82 return nf_conntrack_hash[st->bucket].next;
84 return NULL;
87 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
89 struct ct_iter_state *st = seq->private;
91 head = head->next;
92 while (head == &nf_conntrack_hash[st->bucket]) {
93 if (++st->bucket >= nf_conntrack_htable_size)
94 return NULL;
95 head = nf_conntrack_hash[st->bucket].next;
97 return head;
100 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
102 struct list_head *head = ct_get_first(seq);
104 if (head)
105 while (pos && (head = ct_get_next(seq, head)))
106 pos--;
107 return pos ? NULL : head;
110 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
112 read_lock_bh(&nf_conntrack_lock);
113 return ct_get_idx(seq, *pos);
116 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
118 (*pos)++;
119 return ct_get_next(s, v);
122 static void ct_seq_stop(struct seq_file *s, void *v)
124 read_unlock_bh(&nf_conntrack_lock);
127 /* return 0 on success, 1 in case of error */
128 static int ct_seq_show(struct seq_file *s, void *v)
130 const struct nf_conntrack_tuple_hash *hash = v;
131 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
132 struct nf_conntrack_l3proto *l3proto;
133 struct nf_conntrack_l4proto *l4proto;
135 NF_CT_ASSERT(conntrack);
137 /* we only want to print DIR_ORIGINAL */
138 if (NF_CT_DIRECTION(hash))
139 return 0;
141 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
142 .tuple.src.l3num);
144 NF_CT_ASSERT(l3proto);
145 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
146 .tuple.src.l3num,
147 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
148 .tuple.dst.protonum);
149 NF_CT_ASSERT(l4proto);
151 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
152 l3proto->name,
153 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
154 l4proto->name,
155 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
156 timer_pending(&conntrack->timeout)
157 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
158 return -ENOSPC;
160 if (l3proto->print_conntrack(s, conntrack))
161 return -ENOSPC;
163 if (l4proto->print_conntrack(s, conntrack))
164 return -ENOSPC;
166 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
167 l3proto, l4proto))
168 return -ENOSPC;
170 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
171 return -ENOSPC;
173 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
174 if (seq_printf(s, "[UNREPLIED] "))
175 return -ENOSPC;
177 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
178 l3proto, l4proto))
179 return -ENOSPC;
181 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
182 return -ENOSPC;
184 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
185 if (seq_printf(s, "[ASSURED] "))
186 return -ENOSPC;
188 #if defined(CONFIG_NF_CONNTRACK_MARK)
189 if (seq_printf(s, "mark=%u ", conntrack->mark))
190 return -ENOSPC;
191 #endif
193 #ifdef CONFIG_NF_CONNTRACK_SECMARK
194 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
195 return -ENOSPC;
196 #endif
198 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
199 return -ENOSPC;
201 return 0;
204 static struct seq_operations ct_seq_ops = {
205 .start = ct_seq_start,
206 .next = ct_seq_next,
207 .stop = ct_seq_stop,
208 .show = ct_seq_show
211 static int ct_open(struct inode *inode, struct file *file)
213 struct seq_file *seq;
214 struct ct_iter_state *st;
215 int ret;
217 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
218 if (st == NULL)
219 return -ENOMEM;
220 ret = seq_open(file, &ct_seq_ops);
221 if (ret)
222 goto out_free;
223 seq = file->private_data;
224 seq->private = st;
225 memset(st, 0, sizeof(struct ct_iter_state));
226 return ret;
227 out_free:
228 kfree(st);
229 return ret;
232 static const struct file_operations ct_file_ops = {
233 .owner = THIS_MODULE,
234 .open = ct_open,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = seq_release_private,
240 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
242 int cpu;
244 if (*pos == 0)
245 return SEQ_START_TOKEN;
247 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
248 if (!cpu_possible(cpu))
249 continue;
250 *pos = cpu + 1;
251 return &per_cpu(nf_conntrack_stat, cpu);
254 return NULL;
257 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
259 int cpu;
261 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
262 if (!cpu_possible(cpu))
263 continue;
264 *pos = cpu + 1;
265 return &per_cpu(nf_conntrack_stat, cpu);
268 return NULL;
271 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
275 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
277 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
278 struct ip_conntrack_stat *st = v;
280 if (v == SEQ_START_TOKEN) {
281 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");
282 return 0;
285 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
286 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
287 nr_conntracks,
288 st->searched,
289 st->found,
290 st->new,
291 st->invalid,
292 st->ignore,
293 st->delete,
294 st->delete_list,
295 st->insert,
296 st->insert_failed,
297 st->drop,
298 st->early_drop,
299 st->error,
301 st->expect_new,
302 st->expect_create,
303 st->expect_delete
305 return 0;
308 static struct seq_operations ct_cpu_seq_ops = {
309 .start = ct_cpu_seq_start,
310 .next = ct_cpu_seq_next,
311 .stop = ct_cpu_seq_stop,
312 .show = ct_cpu_seq_show,
315 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
317 return seq_open(file, &ct_cpu_seq_ops);
320 static const struct file_operations ct_cpu_seq_fops = {
321 .owner = THIS_MODULE,
322 .open = ct_cpu_seq_open,
323 .read = seq_read,
324 .llseek = seq_lseek,
325 .release = seq_release_private,
327 #endif /* CONFIG_PROC_FS */
329 /* Sysctl support */
331 int nf_conntrack_checksum __read_mostly = 1;
332 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
334 #ifdef CONFIG_SYSCTL
335 /* Log invalid packets of a given protocol */
336 static int log_invalid_proto_min = 0;
337 static int log_invalid_proto_max = 255;
339 static struct ctl_table_header *nf_ct_sysctl_header;
341 static ctl_table nf_ct_sysctl_table[] = {
343 .ctl_name = NET_NF_CONNTRACK_MAX,
344 .procname = "nf_conntrack_max",
345 .data = &nf_conntrack_max,
346 .maxlen = sizeof(int),
347 .mode = 0644,
348 .proc_handler = &proc_dointvec,
351 .ctl_name = NET_NF_CONNTRACK_COUNT,
352 .procname = "nf_conntrack_count",
353 .data = &nf_conntrack_count,
354 .maxlen = sizeof(int),
355 .mode = 0444,
356 .proc_handler = &proc_dointvec,
359 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
360 .procname = "nf_conntrack_buckets",
361 .data = &nf_conntrack_htable_size,
362 .maxlen = sizeof(unsigned int),
363 .mode = 0444,
364 .proc_handler = &proc_dointvec,
367 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
368 .procname = "nf_conntrack_checksum",
369 .data = &nf_conntrack_checksum,
370 .maxlen = sizeof(unsigned int),
371 .mode = 0644,
372 .proc_handler = &proc_dointvec,
375 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
376 .procname = "nf_conntrack_log_invalid",
377 .data = &nf_ct_log_invalid,
378 .maxlen = sizeof(unsigned int),
379 .mode = 0644,
380 .proc_handler = &proc_dointvec_minmax,
381 .strategy = &sysctl_intvec,
382 .extra1 = &log_invalid_proto_min,
383 .extra2 = &log_invalid_proto_max,
386 { .ctl_name = 0 }
389 #define NET_NF_CONNTRACK_MAX 2089
391 static ctl_table nf_ct_netfilter_table[] = {
393 .ctl_name = NET_NETFILTER,
394 .procname = "netfilter",
395 .mode = 0555,
396 .child = nf_ct_sysctl_table,
399 .ctl_name = NET_NF_CONNTRACK_MAX,
400 .procname = "nf_conntrack_max",
401 .data = &nf_conntrack_max,
402 .maxlen = sizeof(int),
403 .mode = 0644,
404 .proc_handler = &proc_dointvec,
406 { .ctl_name = 0 }
409 static ctl_table nf_ct_net_table[] = {
411 .ctl_name = CTL_NET,
412 .procname = "net",
413 .mode = 0555,
414 .child = nf_ct_netfilter_table,
416 { .ctl_name = 0 }
418 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
419 #endif /* CONFIG_SYSCTL */
421 static int __init nf_conntrack_standalone_init(void)
423 #ifdef CONFIG_PROC_FS
424 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
425 #endif
426 int ret = 0;
428 ret = nf_conntrack_init();
429 if (ret < 0)
430 return ret;
432 #ifdef CONFIG_PROC_FS
433 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
434 if (!proc) goto cleanup_init;
436 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
437 &exp_file_ops);
438 if (!proc_exp) goto cleanup_proc;
440 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
441 if (!proc_stat)
442 goto cleanup_proc_exp;
444 proc_stat->proc_fops = &ct_cpu_seq_fops;
445 proc_stat->owner = THIS_MODULE;
446 #endif
447 #ifdef CONFIG_SYSCTL
448 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
449 if (nf_ct_sysctl_header == NULL) {
450 printk("nf_conntrack: can't register to sysctl.\n");
451 ret = -ENOMEM;
452 goto cleanup_proc_stat;
454 #endif
455 return ret;
457 #ifdef CONFIG_SYSCTL
458 cleanup_proc_stat:
459 #endif
460 #ifdef CONFIG_PROC_FS
461 remove_proc_entry("nf_conntrack", proc_net_stat);
462 cleanup_proc_exp:
463 proc_net_remove("nf_conntrack_expect");
464 cleanup_proc:
465 proc_net_remove("nf_conntrack");
466 cleanup_init:
467 #endif /* CNFIG_PROC_FS */
468 nf_conntrack_cleanup();
469 return ret;
472 static void __exit nf_conntrack_standalone_fini(void)
474 #ifdef CONFIG_SYSCTL
475 unregister_sysctl_table(nf_ct_sysctl_header);
476 #endif
477 #ifdef CONFIG_PROC_FS
478 remove_proc_entry("nf_conntrack", proc_net_stat);
479 proc_net_remove("nf_conntrack_expect");
480 proc_net_remove("nf_conntrack");
481 #endif /* CNFIG_PROC_FS */
482 nf_conntrack_cleanup();
485 module_init(nf_conntrack_standalone_init);
486 module_exit(nf_conntrack_standalone_fini);
488 /* Some modules need us, but don't depend directly on any symbol.
489 They should call this. */
490 void need_conntrack(void)
493 EXPORT_SYMBOL_GPL(need_conntrack);