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>
17 #include <net/net_namespace.h>
19 #include <linux/sysctl.h>
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>
28 #include <net/netfilter/nf_conntrack_acct.h>
30 MODULE_LICENSE("GPL");
34 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
35 const struct nf_conntrack_l3proto *l3proto,
36 const struct nf_conntrack_l4proto *l4proto)
38 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
40 EXPORT_SYMBOL_GPL(print_tuple);
42 struct ct_iter_state {
43 struct seq_net_private p;
47 static struct hlist_node *ct_get_first(struct seq_file *seq)
49 struct net *net = seq_file_net(seq);
50 struct ct_iter_state *st = seq->private;
54 st->bucket < nf_conntrack_htable_size;
56 n = rcu_dereference(net->ct.hash[st->bucket].first);
63 static struct hlist_node *ct_get_next(struct seq_file *seq,
64 struct hlist_node *head)
66 struct net *net = seq_file_net(seq);
67 struct ct_iter_state *st = seq->private;
69 head = rcu_dereference(head->next);
70 while (head == NULL) {
71 if (++st->bucket >= nf_conntrack_htable_size)
73 head = rcu_dereference(net->ct.hash[st->bucket].first);
78 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
80 struct hlist_node *head = ct_get_first(seq);
83 while (pos && (head = ct_get_next(seq, head)))
85 return pos ? NULL : head;
88 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
92 return ct_get_idx(seq, *pos);
95 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
98 return ct_get_next(s, v);
101 static void ct_seq_stop(struct seq_file *s, void *v)
107 /* return 0 on success, 1 in case of error */
108 static int ct_seq_show(struct seq_file *s, void *v)
110 const struct nf_conntrack_tuple_hash *hash = v;
111 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
112 const struct nf_conntrack_l3proto *l3proto;
113 const struct nf_conntrack_l4proto *l4proto;
117 /* we only want to print DIR_ORIGINAL */
118 if (NF_CT_DIRECTION(hash))
121 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
122 NF_CT_ASSERT(l3proto);
123 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
124 NF_CT_ASSERT(l4proto);
126 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
127 l3proto->name, nf_ct_l3num(ct),
128 l4proto->name, nf_ct_protonum(ct),
129 timer_pending(&ct->timeout)
130 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
133 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
136 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
140 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
143 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
144 if (seq_printf(s, "[UNREPLIED] "))
147 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
151 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
154 if (test_bit(IPS_ASSURED_BIT, &ct->status))
155 if (seq_printf(s, "[ASSURED] "))
158 #if defined(CONFIG_NF_CONNTRACK_MARK)
159 if (seq_printf(s, "mark=%u ", ct->mark))
163 #ifdef CONFIG_NF_CONNTRACK_SECMARK
164 if (seq_printf(s, "secmark=%u ", ct->secmark))
168 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
174 static const struct seq_operations ct_seq_ops = {
175 .start = ct_seq_start,
181 static int ct_open(struct inode *inode, struct file *file)
183 return seq_open_net(inode, file, &ct_seq_ops,
184 sizeof(struct ct_iter_state));
187 static const struct file_operations ct_file_ops = {
188 .owner = THIS_MODULE,
192 .release = seq_release_net,
195 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
197 struct net *net = seq_file_net(seq);
201 return SEQ_START_TOKEN;
203 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
204 if (!cpu_possible(cpu))
207 return per_cpu_ptr(net->ct.stat, cpu);
213 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
215 struct net *net = seq_file_net(seq);
218 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
219 if (!cpu_possible(cpu))
222 return per_cpu_ptr(net->ct.stat, cpu);
228 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
232 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
234 struct net *net = seq_file_net(seq);
235 unsigned int nr_conntracks = atomic_read(&net->ct.count);
236 const struct ip_conntrack_stat *st = v;
238 if (v == SEQ_START_TOKEN) {
239 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");
243 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
244 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
266 static const struct seq_operations ct_cpu_seq_ops = {
267 .start = ct_cpu_seq_start,
268 .next = ct_cpu_seq_next,
269 .stop = ct_cpu_seq_stop,
270 .show = ct_cpu_seq_show,
273 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
275 return seq_open_net(inode, file, &ct_cpu_seq_ops,
276 sizeof(struct seq_net_private));
279 static const struct file_operations ct_cpu_seq_fops = {
280 .owner = THIS_MODULE,
281 .open = ct_cpu_seq_open,
284 .release = seq_release_net,
287 static int nf_conntrack_standalone_init_proc(struct net *net)
289 struct proc_dir_entry *pde;
291 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
293 goto out_nf_conntrack;
295 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
298 goto out_stat_nf_conntrack;
301 out_stat_nf_conntrack:
302 proc_net_remove(net, "nf_conntrack");
307 static void nf_conntrack_standalone_fini_proc(struct net *net)
309 remove_proc_entry("nf_conntrack", net->proc_net_stat);
310 proc_net_remove(net, "nf_conntrack");
313 static int nf_conntrack_standalone_init_proc(struct net *net)
318 static void nf_conntrack_standalone_fini_proc(struct net *net)
321 #endif /* CONFIG_PROC_FS */
325 int nf_conntrack_checksum __read_mostly = 1;
326 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
329 /* Log invalid packets of a given protocol */
330 static int log_invalid_proto_min = 0;
331 static int log_invalid_proto_max = 255;
333 static struct ctl_table_header *nf_ct_sysctl_header;
334 static struct ctl_table_header *nf_ct_netfilter_header;
336 static ctl_table nf_ct_sysctl_table[] = {
338 .ctl_name = NET_NF_CONNTRACK_MAX,
339 .procname = "nf_conntrack_max",
340 .data = &nf_conntrack_max,
341 .maxlen = sizeof(int),
343 .proc_handler = &proc_dointvec,
346 .ctl_name = NET_NF_CONNTRACK_COUNT,
347 .procname = "nf_conntrack_count",
348 .data = &init_net.ct.count,
349 .maxlen = sizeof(int),
351 .proc_handler = &proc_dointvec,
354 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
355 .procname = "nf_conntrack_buckets",
356 .data = &nf_conntrack_htable_size,
357 .maxlen = sizeof(unsigned int),
359 .proc_handler = &proc_dointvec,
362 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
363 .procname = "nf_conntrack_checksum",
364 .data = &nf_conntrack_checksum,
365 .maxlen = sizeof(unsigned int),
367 .proc_handler = &proc_dointvec,
370 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
371 .procname = "nf_conntrack_log_invalid",
372 .data = &nf_ct_log_invalid,
373 .maxlen = sizeof(unsigned int),
375 .proc_handler = &proc_dointvec_minmax,
376 .strategy = &sysctl_intvec,
377 .extra1 = &log_invalid_proto_min,
378 .extra2 = &log_invalid_proto_max,
381 .ctl_name = CTL_UNNUMBERED,
382 .procname = "nf_conntrack_expect_max",
383 .data = &nf_ct_expect_max,
384 .maxlen = sizeof(int),
386 .proc_handler = &proc_dointvec,
391 #define NET_NF_CONNTRACK_MAX 2089
393 static ctl_table nf_ct_netfilter_table[] = {
395 .ctl_name = NET_NF_CONNTRACK_MAX,
396 .procname = "nf_conntrack_max",
397 .data = &nf_conntrack_max,
398 .maxlen = sizeof(int),
400 .proc_handler = &proc_dointvec,
405 static struct ctl_path nf_ct_path[] = {
406 { .procname = "net", .ctl_name = CTL_NET, },
410 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
412 static int nf_conntrack_standalone_init_sysctl(void)
414 nf_ct_netfilter_header =
415 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
416 if (!nf_ct_netfilter_header)
419 nf_ct_sysctl_header =
420 register_sysctl_paths(nf_net_netfilter_sysctl_path,
422 if (!nf_ct_sysctl_header)
423 goto out_unregister_netfilter;
427 out_unregister_netfilter:
428 unregister_sysctl_table(nf_ct_netfilter_header);
430 printk("nf_conntrack: can't register to sysctl.\n");
434 static void nf_conntrack_standalone_fini_sysctl(void)
436 unregister_sysctl_table(nf_ct_netfilter_header);
437 unregister_sysctl_table(nf_ct_sysctl_header);
440 static int nf_conntrack_standalone_init_sysctl(void)
445 static void nf_conntrack_standalone_fini_sysctl(void)
448 #endif /* CONFIG_SYSCTL */
450 static int nf_conntrack_net_init(struct net *net)
454 ret = nf_conntrack_init(net);
457 ret = nf_conntrack_standalone_init_proc(net);
463 nf_conntrack_cleanup(net);
468 static void nf_conntrack_net_exit(struct net *net)
470 nf_conntrack_standalone_fini_proc(net);
471 nf_conntrack_cleanup(net);
474 static struct pernet_operations nf_conntrack_net_ops = {
475 .init = nf_conntrack_net_init,
476 .exit = nf_conntrack_net_exit,
479 static int __init nf_conntrack_standalone_init(void)
483 ret = register_pernet_subsys(&nf_conntrack_net_ops);
486 ret = nf_conntrack_standalone_init_sysctl();
492 unregister_pernet_subsys(&nf_conntrack_net_ops);
497 static void __exit nf_conntrack_standalone_fini(void)
499 nf_conntrack_standalone_fini_sysctl();
500 unregister_pernet_subsys(&nf_conntrack_net_ops);
503 module_init(nf_conntrack_standalone_init);
504 module_exit(nf_conntrack_standalone_fini);
506 /* Some modules need us, but don't depend directly on any symbol.
507 They should call this. */
508 void need_conntrack(void)
511 EXPORT_SYMBOL_GPL(need_conntrack);