1 /* This file contains all the functions required for the standalone
4 These are not required by the compatibility layer.
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>
29 #include <linux/sysctl.h>
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>
42 #define DEBUGP(format, args...)
45 MODULE_LICENSE("GPL");
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
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);
67 #define seq_print_counters(x, y) 0
70 struct ct_iter_state {
74 static struct list_head *ct_get_first(struct seq_file *seq)
76 struct ct_iter_state *st = seq->private;
79 st->bucket < nf_conntrack_htable_size;
81 if (!list_empty(&nf_conntrack_hash[st->bucket]))
82 return nf_conntrack_hash[st->bucket].next;
87 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
89 struct ct_iter_state *st = seq->private;
92 while (head == &nf_conntrack_hash[st->bucket]) {
93 if (++st->bucket >= nf_conntrack_htable_size)
95 head = nf_conntrack_hash[st->bucket].next;
100 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
102 struct list_head *head = ct_get_first(seq);
105 while (pos && (head = ct_get_next(seq, head)))
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)
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))
141 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
144 NF_CT_ASSERT(l3proto);
145 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
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 ",
153 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
155 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
156 timer_pending(&conntrack->timeout)
157 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
160 if (l3proto->print_conntrack(s, conntrack))
163 if (l4proto->print_conntrack(s, conntrack))
166 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
170 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
173 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
174 if (seq_printf(s, "[UNREPLIED] "))
177 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
181 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
184 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
185 if (seq_printf(s, "[ASSURED] "))
188 #if defined(CONFIG_NF_CONNTRACK_MARK)
189 if (seq_printf(s, "mark=%u ", conntrack->mark))
193 #ifdef CONFIG_NF_CONNTRACK_SECMARK
194 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
198 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
204 static struct seq_operations ct_seq_ops = {
205 .start = ct_seq_start,
211 static int ct_open(struct inode *inode, struct file *file)
213 struct seq_file *seq;
214 struct ct_iter_state *st;
217 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
220 ret = seq_open(file, &ct_seq_ops);
223 seq = file->private_data;
225 memset(st, 0, sizeof(struct ct_iter_state));
232 static const struct file_operations ct_file_ops = {
233 .owner = THIS_MODULE,
237 .release = seq_release_private,
240 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
245 return SEQ_START_TOKEN;
247 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
248 if (!cpu_possible(cpu))
251 return &per_cpu(nf_conntrack_stat, cpu);
257 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
261 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
262 if (!cpu_possible(cpu))
265 return &per_cpu(nf_conntrack_stat, cpu);
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");
285 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
286 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
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,
325 .release = seq_release_private,
327 #endif /* CONFIG_PROC_FS */
331 int nf_conntrack_checksum __read_mostly = 1;
332 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
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),
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),
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),
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),
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),
380 .proc_handler = &proc_dointvec_minmax,
381 .strategy = &sysctl_intvec,
382 .extra1 = &log_invalid_proto_min,
383 .extra2 = &log_invalid_proto_max,
389 #define NET_NF_CONNTRACK_MAX 2089
391 static ctl_table nf_ct_netfilter_table[] = {
393 .ctl_name = NET_NETFILTER,
394 .procname = "netfilter",
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),
404 .proc_handler = &proc_dointvec,
409 static ctl_table nf_ct_net_table[] = {
414 .child = nf_ct_netfilter_table,
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;
428 ret = nf_conntrack_init();
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,
438 if (!proc_exp) goto cleanup_proc;
440 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
442 goto cleanup_proc_exp;
444 proc_stat->proc_fops = &ct_cpu_seq_fops;
445 proc_stat->owner = THIS_MODULE;
448 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table);
449 if (nf_ct_sysctl_header == NULL) {
450 printk("nf_conntrack: can't register to sysctl.\n");
452 goto cleanup_proc_stat;
460 #ifdef CONFIG_PROC_FS
461 remove_proc_entry("nf_conntrack", proc_net_stat);
463 proc_net_remove("nf_conntrack_expect");
465 proc_net_remove("nf_conntrack");
467 #endif /* CNFIG_PROC_FS */
468 nf_conntrack_cleanup();
472 static void __exit nf_conntrack_standalone_fini(void)
475 unregister_sysctl_table(nf_ct_sysctl_header);
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);