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.
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>
18 #include <linux/sysctl.h>
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_l3proto.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
28 MODULE_LICENSE("GPL");
32 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
33 struct nf_conntrack_l3proto *l3proto,
34 struct nf_conntrack_l4proto *l4proto)
36 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
38 EXPORT_SYMBOL_GPL(print_tuple);
40 #ifdef CONFIG_NF_CT_ACCT
42 seq_print_counters(struct seq_file *s,
43 const struct ip_conntrack_counter *counter)
45 return seq_printf(s, "packets=%llu bytes=%llu ",
46 (unsigned long long)counter->packets,
47 (unsigned long long)counter->bytes);
50 #define seq_print_counters(x, y) 0
53 struct ct_iter_state {
57 static struct hlist_node *ct_get_first(struct seq_file *seq)
59 struct ct_iter_state *st = seq->private;
62 st->bucket < nf_conntrack_htable_size;
64 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
65 return nf_conntrack_hash[st->bucket].first;
70 static struct hlist_node *ct_get_next(struct seq_file *seq,
71 struct hlist_node *head)
73 struct ct_iter_state *st = seq->private;
76 while (head == NULL) {
77 if (++st->bucket >= nf_conntrack_htable_size)
79 head = nf_conntrack_hash[st->bucket].first;
84 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
86 struct hlist_node *head = ct_get_first(seq);
89 while (pos && (head = ct_get_next(seq, head)))
91 return pos ? NULL : head;
94 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
96 read_lock_bh(&nf_conntrack_lock);
97 return ct_get_idx(seq, *pos);
100 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
103 return ct_get_next(s, v);
106 static void ct_seq_stop(struct seq_file *s, void *v)
108 read_unlock_bh(&nf_conntrack_lock);
111 /* return 0 on success, 1 in case of error */
112 static int ct_seq_show(struct seq_file *s, void *v)
114 const struct nf_conntrack_tuple_hash *hash = v;
115 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
116 struct nf_conntrack_l3proto *l3proto;
117 struct nf_conntrack_l4proto *l4proto;
119 NF_CT_ASSERT(conntrack);
121 /* we only want to print DIR_ORIGINAL */
122 if (NF_CT_DIRECTION(hash))
125 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
128 NF_CT_ASSERT(l3proto);
129 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
131 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
132 .tuple.dst.protonum);
133 NF_CT_ASSERT(l4proto);
135 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
137 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
139 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
140 timer_pending(&conntrack->timeout)
141 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
144 if (l3proto->print_conntrack(s, conntrack))
147 if (l4proto->print_conntrack(s, conntrack))
150 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
154 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
157 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
158 if (seq_printf(s, "[UNREPLIED] "))
161 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
165 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
168 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
169 if (seq_printf(s, "[ASSURED] "))
172 #if defined(CONFIG_NF_CONNTRACK_MARK)
173 if (seq_printf(s, "mark=%u ", conntrack->mark))
177 #ifdef CONFIG_NF_CONNTRACK_SECMARK
178 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
182 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
188 static const struct seq_operations ct_seq_ops = {
189 .start = ct_seq_start,
195 static int ct_open(struct inode *inode, struct file *file)
197 struct seq_file *seq;
198 struct ct_iter_state *st;
201 st = kzalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
204 ret = seq_open(file, &ct_seq_ops);
207 seq = file->private_data;
215 static const struct file_operations ct_file_ops = {
216 .owner = THIS_MODULE,
220 .release = seq_release_private,
223 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
228 return SEQ_START_TOKEN;
230 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
231 if (!cpu_possible(cpu))
234 return &per_cpu(nf_conntrack_stat, cpu);
240 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
244 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
245 if (!cpu_possible(cpu))
248 return &per_cpu(nf_conntrack_stat, cpu);
254 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
258 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
260 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
261 struct ip_conntrack_stat *st = v;
263 if (v == SEQ_START_TOKEN) {
264 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");
268 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
269 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
291 static const struct seq_operations ct_cpu_seq_ops = {
292 .start = ct_cpu_seq_start,
293 .next = ct_cpu_seq_next,
294 .stop = ct_cpu_seq_stop,
295 .show = ct_cpu_seq_show,
298 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
300 return seq_open(file, &ct_cpu_seq_ops);
303 static const struct file_operations ct_cpu_seq_fops = {
304 .owner = THIS_MODULE,
305 .open = ct_cpu_seq_open,
308 .release = seq_release_private,
310 #endif /* CONFIG_PROC_FS */
314 int nf_conntrack_checksum __read_mostly = 1;
315 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
318 /* Log invalid packets of a given protocol */
319 static int log_invalid_proto_min = 0;
320 static int log_invalid_proto_max = 255;
322 static struct ctl_table_header *nf_ct_sysctl_header;
324 static ctl_table nf_ct_sysctl_table[] = {
326 .ctl_name = NET_NF_CONNTRACK_MAX,
327 .procname = "nf_conntrack_max",
328 .data = &nf_conntrack_max,
329 .maxlen = sizeof(int),
331 .proc_handler = &proc_dointvec,
334 .ctl_name = NET_NF_CONNTRACK_COUNT,
335 .procname = "nf_conntrack_count",
336 .data = &nf_conntrack_count,
337 .maxlen = sizeof(int),
339 .proc_handler = &proc_dointvec,
342 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
343 .procname = "nf_conntrack_buckets",
344 .data = &nf_conntrack_htable_size,
345 .maxlen = sizeof(unsigned int),
347 .proc_handler = &proc_dointvec,
350 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
351 .procname = "nf_conntrack_checksum",
352 .data = &nf_conntrack_checksum,
353 .maxlen = sizeof(unsigned int),
355 .proc_handler = &proc_dointvec,
358 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
359 .procname = "nf_conntrack_log_invalid",
360 .data = &nf_ct_log_invalid,
361 .maxlen = sizeof(unsigned int),
363 .proc_handler = &proc_dointvec_minmax,
364 .strategy = &sysctl_intvec,
365 .extra1 = &log_invalid_proto_min,
366 .extra2 = &log_invalid_proto_max,
369 .ctl_name = CTL_UNNUMBERED,
370 .procname = "nf_conntrack_expect_max",
371 .data = &nf_ct_expect_max,
372 .maxlen = sizeof(int),
374 .proc_handler = &proc_dointvec,
379 #define NET_NF_CONNTRACK_MAX 2089
381 static ctl_table nf_ct_netfilter_table[] = {
383 .ctl_name = NET_NETFILTER,
384 .procname = "netfilter",
386 .child = nf_ct_sysctl_table,
389 .ctl_name = NET_NF_CONNTRACK_MAX,
390 .procname = "nf_conntrack_max",
391 .data = &nf_conntrack_max,
392 .maxlen = sizeof(int),
394 .proc_handler = &proc_dointvec,
399 static ctl_table nf_ct_net_table[] = {
404 .child = nf_ct_netfilter_table,
408 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
409 #endif /* CONFIG_SYSCTL */
411 static int __init nf_conntrack_standalone_init(void)
413 #ifdef CONFIG_PROC_FS
414 struct proc_dir_entry *proc, *proc_stat;
418 ret = nf_conntrack_init();
422 #ifdef CONFIG_PROC_FS
423 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
424 if (!proc) goto cleanup_init;
426 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
430 proc_stat->proc_fops = &ct_cpu_seq_fops;
431 proc_stat->owner = THIS_MODULE;
434 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table);
435 if (nf_ct_sysctl_header == NULL) {
436 printk("nf_conntrack: can't register to sysctl.\n");
438 goto cleanup_proc_stat;
446 #ifdef CONFIG_PROC_FS
447 remove_proc_entry("nf_conntrack", proc_net_stat);
449 proc_net_remove("nf_conntrack");
451 #endif /* CNFIG_PROC_FS */
452 nf_conntrack_cleanup();
456 static void __exit nf_conntrack_standalone_fini(void)
459 unregister_sysctl_table(nf_ct_sysctl_header);
461 #ifdef CONFIG_PROC_FS
462 remove_proc_entry("nf_conntrack", proc_net_stat);
463 proc_net_remove("nf_conntrack");
464 #endif /* CNFIG_PROC_FS */
465 nf_conntrack_cleanup();
468 module_init(nf_conntrack_standalone_init);
469 module_exit(nf_conntrack_standalone_fini);
471 /* Some modules need us, but don't depend directly on any symbol.
472 They should call this. */
473 void need_conntrack(void)
476 EXPORT_SYMBOL_GPL(need_conntrack);