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/slab.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/percpu.h>
17 #include <linux/netdevice.h>
18 #include <linux/security.h>
19 #include <net/net_namespace.h>
21 #include <linux/sysctl.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_acct.h>
31 #include <net/netfilter/nf_conntrack_zones.h>
32 #include <linux/rculist_nulls.h>
34 MODULE_LICENSE("GPL");
38 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
39 const struct nf_conntrack_l3proto *l3proto,
40 const struct nf_conntrack_l4proto *l4proto)
42 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
44 EXPORT_SYMBOL_GPL(print_tuple);
46 struct ct_iter_state {
47 struct seq_net_private p;
51 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
53 struct net *net = seq_file_net(seq);
54 struct ct_iter_state *st = seq->private;
55 struct hlist_nulls_node *n;
58 st->bucket < net->ct.htable_size;
60 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
67 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
68 struct hlist_nulls_node *head)
70 struct net *net = seq_file_net(seq);
71 struct ct_iter_state *st = seq->private;
73 head = rcu_dereference(hlist_nulls_next_rcu(head));
74 while (is_a_nulls(head)) {
75 if (likely(get_nulls_value(head) == st->bucket)) {
76 if (++st->bucket >= net->ct.htable_size)
79 head = rcu_dereference(
80 hlist_nulls_first_rcu(
81 &net->ct.hash[st->bucket]));
86 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
88 struct hlist_nulls_node *head = ct_get_first(seq);
91 while (pos && (head = ct_get_next(seq, head)))
93 return pos ? NULL : head;
96 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
100 return ct_get_idx(seq, *pos);
103 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
106 return ct_get_next(s, v);
109 static void ct_seq_stop(struct seq_file *s, void *v)
115 #ifdef CONFIG_NF_CONNTRACK_SECMARK
116 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
122 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
126 ret = seq_printf(s, "secctx=%s ", secctx);
128 security_release_secctx(secctx, len);
132 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
138 /* return 0 on success, 1 in case of error */
139 static int ct_seq_show(struct seq_file *s, void *v)
141 struct nf_conntrack_tuple_hash *hash = v;
142 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
143 const struct nf_conntrack_l3proto *l3proto;
144 const struct nf_conntrack_l4proto *l4proto;
148 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
151 /* we only want to print DIR_ORIGINAL */
152 if (NF_CT_DIRECTION(hash))
155 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
156 NF_CT_ASSERT(l3proto);
157 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
158 NF_CT_ASSERT(l4proto);
161 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
162 l3proto->name, nf_ct_l3num(ct),
163 l4proto->name, nf_ct_protonum(ct),
164 timer_pending(&ct->timeout)
165 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
168 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
171 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
175 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
178 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
179 if (seq_printf(s, "[UNREPLIED] "))
182 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
186 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
189 if (test_bit(IPS_ASSURED_BIT, &ct->status))
190 if (seq_printf(s, "[ASSURED] "))
193 #if defined(CONFIG_NF_CONNTRACK_MARK)
194 if (seq_printf(s, "mark=%u ", ct->mark))
198 if (ct_show_secctx(s, ct))
201 #ifdef CONFIG_NF_CONNTRACK_ZONES
202 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
206 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
215 static const struct seq_operations ct_seq_ops = {
216 .start = ct_seq_start,
222 static int ct_open(struct inode *inode, struct file *file)
224 return seq_open_net(inode, file, &ct_seq_ops,
225 sizeof(struct ct_iter_state));
228 static const struct file_operations ct_file_ops = {
229 .owner = THIS_MODULE,
233 .release = seq_release_net,
236 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
238 struct net *net = seq_file_net(seq);
242 return SEQ_START_TOKEN;
244 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
245 if (!cpu_possible(cpu))
248 return per_cpu_ptr(net->ct.stat, cpu);
254 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
256 struct net *net = seq_file_net(seq);
259 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
260 if (!cpu_possible(cpu))
263 return per_cpu_ptr(net->ct.stat, cpu);
269 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
273 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
275 struct net *net = seq_file_net(seq);
276 unsigned int nr_conntracks = atomic_read(&net->ct.count);
277 const struct ip_conntrack_stat *st = v;
279 if (v == SEQ_START_TOKEN) {
280 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 search_restart\n");
284 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
285 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
308 static const 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_net(inode, file, &ct_cpu_seq_ops,
318 sizeof(struct seq_net_private));
321 static const struct file_operations ct_cpu_seq_fops = {
322 .owner = THIS_MODULE,
323 .open = ct_cpu_seq_open,
326 .release = seq_release_net,
329 static int nf_conntrack_standalone_init_proc(struct net *net)
331 struct proc_dir_entry *pde;
333 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
335 goto out_nf_conntrack;
337 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
340 goto out_stat_nf_conntrack;
343 out_stat_nf_conntrack:
344 proc_net_remove(net, "nf_conntrack");
349 static void nf_conntrack_standalone_fini_proc(struct net *net)
351 remove_proc_entry("nf_conntrack", net->proc_net_stat);
352 proc_net_remove(net, "nf_conntrack");
355 static int nf_conntrack_standalone_init_proc(struct net *net)
360 static void nf_conntrack_standalone_fini_proc(struct net *net)
363 #endif /* CONFIG_PROC_FS */
368 /* Log invalid packets of a given protocol */
369 static int log_invalid_proto_min = 0;
370 static int log_invalid_proto_max = 255;
372 static struct ctl_table_header *nf_ct_netfilter_header;
374 static ctl_table nf_ct_sysctl_table[] = {
376 .procname = "nf_conntrack_max",
377 .data = &nf_conntrack_max,
378 .maxlen = sizeof(int),
380 .proc_handler = proc_dointvec,
383 .procname = "nf_conntrack_count",
384 .data = &init_net.ct.count,
385 .maxlen = sizeof(int),
387 .proc_handler = proc_dointvec,
390 .procname = "nf_conntrack_buckets",
391 .data = &init_net.ct.htable_size,
392 .maxlen = sizeof(unsigned int),
394 .proc_handler = proc_dointvec,
397 .procname = "nf_conntrack_checksum",
398 .data = &init_net.ct.sysctl_checksum,
399 .maxlen = sizeof(unsigned int),
401 .proc_handler = proc_dointvec,
404 .procname = "nf_conntrack_log_invalid",
405 .data = &init_net.ct.sysctl_log_invalid,
406 .maxlen = sizeof(unsigned int),
408 .proc_handler = proc_dointvec_minmax,
409 .extra1 = &log_invalid_proto_min,
410 .extra2 = &log_invalid_proto_max,
413 .procname = "nf_conntrack_expect_max",
414 .data = &nf_ct_expect_max,
415 .maxlen = sizeof(int),
417 .proc_handler = proc_dointvec,
422 #define NET_NF_CONNTRACK_MAX 2089
424 static ctl_table nf_ct_netfilter_table[] = {
426 .procname = "nf_conntrack_max",
427 .data = &nf_conntrack_max,
428 .maxlen = sizeof(int),
430 .proc_handler = proc_dointvec,
435 static struct ctl_path nf_ct_path[] = {
436 { .procname = "net", },
440 static int nf_conntrack_standalone_init_sysctl(struct net *net)
442 struct ctl_table *table;
444 if (net_eq(net, &init_net)) {
445 nf_ct_netfilter_header =
446 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
447 if (!nf_ct_netfilter_header)
451 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
456 table[1].data = &net->ct.count;
457 table[2].data = &net->ct.htable_size;
458 table[3].data = &net->ct.sysctl_checksum;
459 table[4].data = &net->ct.sysctl_log_invalid;
461 net->ct.sysctl_header = register_net_sysctl_table(net,
462 nf_net_netfilter_sysctl_path, table);
463 if (!net->ct.sysctl_header)
464 goto out_unregister_netfilter;
468 out_unregister_netfilter:
471 if (net_eq(net, &init_net))
472 unregister_sysctl_table(nf_ct_netfilter_header);
474 printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
478 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
480 struct ctl_table *table;
482 if (net_eq(net, &init_net))
483 unregister_sysctl_table(nf_ct_netfilter_header);
484 table = net->ct.sysctl_header->ctl_table_arg;
485 unregister_net_sysctl_table(net->ct.sysctl_header);
489 static int nf_conntrack_standalone_init_sysctl(struct net *net)
494 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
497 #endif /* CONFIG_SYSCTL */
499 static int nf_conntrack_net_init(struct net *net)
503 ret = nf_conntrack_init(net);
506 ret = nf_conntrack_standalone_init_proc(net);
509 net->ct.sysctl_checksum = 1;
510 net->ct.sysctl_log_invalid = 0;
511 ret = nf_conntrack_standalone_init_sysctl(net);
517 nf_conntrack_standalone_fini_proc(net);
519 nf_conntrack_cleanup(net);
524 static void nf_conntrack_net_exit(struct net *net)
526 nf_conntrack_standalone_fini_sysctl(net);
527 nf_conntrack_standalone_fini_proc(net);
528 nf_conntrack_cleanup(net);
531 static struct pernet_operations nf_conntrack_net_ops = {
532 .init = nf_conntrack_net_init,
533 .exit = nf_conntrack_net_exit,
536 static int __init nf_conntrack_standalone_init(void)
538 return register_pernet_subsys(&nf_conntrack_net_ops);
541 static void __exit nf_conntrack_standalone_fini(void)
543 unregister_pernet_subsys(&nf_conntrack_net_ops);
546 module_init(nf_conntrack_standalone_init);
547 module_exit(nf_conntrack_standalone_fini);
549 /* Some modules need us, but don't depend directly on any symbol.
550 They should call this. */
551 void need_conntrack(void)
554 EXPORT_SYMBOL_GPL(need_conntrack);