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 <net/netfilter/nf_conntrack_timestamp.h>
33 #include <linux/rculist_nulls.h>
35 MODULE_LICENSE("GPL");
39 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
40 const struct nf_conntrack_l3proto *l3proto,
41 const struct nf_conntrack_l4proto *l4proto)
43 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
45 EXPORT_SYMBOL_GPL(print_tuple);
47 struct ct_iter_state {
48 struct seq_net_private p;
53 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
55 struct net *net = seq_file_net(seq);
56 struct ct_iter_state *st = seq->private;
57 struct hlist_nulls_node *n;
60 st->bucket < net->ct.htable_size;
62 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
69 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
70 struct hlist_nulls_node *head)
72 struct net *net = seq_file_net(seq);
73 struct ct_iter_state *st = seq->private;
75 head = rcu_dereference(hlist_nulls_next_rcu(head));
76 while (is_a_nulls(head)) {
77 if (likely(get_nulls_value(head) == st->bucket)) {
78 if (++st->bucket >= net->ct.htable_size)
81 head = rcu_dereference(
82 hlist_nulls_first_rcu(
83 &net->ct.hash[st->bucket]));
88 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
90 struct hlist_nulls_node *head = ct_get_first(seq);
93 while (pos && (head = ct_get_next(seq, head)))
95 return pos ? NULL : head;
98 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
101 struct ct_iter_state *st = seq->private;
103 st->time_now = ktime_to_ns(ktime_get_real());
105 return ct_get_idx(seq, *pos);
108 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
111 return ct_get_next(s, v);
114 static void ct_seq_stop(struct seq_file *s, void *v)
120 #ifdef CONFIG_NF_CONNTRACK_SECMARK
121 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
127 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
131 ret = seq_printf(s, "secctx=%s ", secctx);
133 security_release_secctx(secctx, len);
137 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
143 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
144 static u_int64_t ct_delta_time(u_int64_t time_now, const struct nf_conn *ct)
146 struct nf_conn_tstamp *tstamp;
148 tstamp = nf_conn_tstamp_find(ct);
150 u_int64_t delta_time = time_now - tstamp->start;
151 return delta_time > 0 ? div_s64(delta_time, NSEC_PER_SEC) : 0;
156 static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
158 struct ct_iter_state *st = s->private;
159 u_int64_t delta_time;
161 delta_time = ct_delta_time(st->time_now, ct);
165 return seq_printf(s, "delta-time=%llu ",
166 (unsigned long long)delta_time);
170 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
176 /* return 0 on success, 1 in case of error */
177 static int ct_seq_show(struct seq_file *s, void *v)
179 struct nf_conntrack_tuple_hash *hash = v;
180 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
181 const struct nf_conntrack_l3proto *l3proto;
182 const struct nf_conntrack_l4proto *l4proto;
186 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
189 /* we only want to print DIR_ORIGINAL */
190 if (NF_CT_DIRECTION(hash))
193 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
194 NF_CT_ASSERT(l3proto);
195 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
196 NF_CT_ASSERT(l4proto);
199 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
200 l3proto->name, nf_ct_l3num(ct),
201 l4proto->name, nf_ct_protonum(ct),
202 timer_pending(&ct->timeout)
203 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
206 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
209 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
213 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
216 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
217 if (seq_printf(s, "[UNREPLIED] "))
220 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
224 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
227 if (test_bit(IPS_ASSURED_BIT, &ct->status))
228 if (seq_printf(s, "[ASSURED] "))
231 #if defined(CONFIG_NF_CONNTRACK_MARK)
232 if (seq_printf(s, "mark=%u ", ct->mark))
236 if (ct_show_secctx(s, ct))
239 #ifdef CONFIG_NF_CONNTRACK_ZONES
240 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
244 if (ct_show_delta_time(s, ct))
247 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
256 static const struct seq_operations ct_seq_ops = {
257 .start = ct_seq_start,
263 static int ct_open(struct inode *inode, struct file *file)
265 return seq_open_net(inode, file, &ct_seq_ops,
266 sizeof(struct ct_iter_state));
269 static const struct file_operations ct_file_ops = {
270 .owner = THIS_MODULE,
274 .release = seq_release_net,
277 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
279 struct net *net = seq_file_net(seq);
283 return SEQ_START_TOKEN;
285 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
286 if (!cpu_possible(cpu))
289 return per_cpu_ptr(net->ct.stat, cpu);
295 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
297 struct net *net = seq_file_net(seq);
300 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
301 if (!cpu_possible(cpu))
304 return per_cpu_ptr(net->ct.stat, cpu);
310 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
314 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
316 struct net *net = seq_file_net(seq);
317 unsigned int nr_conntracks = atomic_read(&net->ct.count);
318 const struct ip_conntrack_stat *st = v;
320 if (v == SEQ_START_TOKEN) {
321 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");
325 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
326 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
349 static const struct seq_operations ct_cpu_seq_ops = {
350 .start = ct_cpu_seq_start,
351 .next = ct_cpu_seq_next,
352 .stop = ct_cpu_seq_stop,
353 .show = ct_cpu_seq_show,
356 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
358 return seq_open_net(inode, file, &ct_cpu_seq_ops,
359 sizeof(struct seq_net_private));
362 static const struct file_operations ct_cpu_seq_fops = {
363 .owner = THIS_MODULE,
364 .open = ct_cpu_seq_open,
367 .release = seq_release_net,
370 static int nf_conntrack_standalone_init_proc(struct net *net)
372 struct proc_dir_entry *pde;
374 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
376 goto out_nf_conntrack;
378 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
381 goto out_stat_nf_conntrack;
384 out_stat_nf_conntrack:
385 proc_net_remove(net, "nf_conntrack");
390 static void nf_conntrack_standalone_fini_proc(struct net *net)
392 remove_proc_entry("nf_conntrack", net->proc_net_stat);
393 proc_net_remove(net, "nf_conntrack");
396 static int nf_conntrack_standalone_init_proc(struct net *net)
401 static void nf_conntrack_standalone_fini_proc(struct net *net)
404 #endif /* CONFIG_PROC_FS */
409 /* Log invalid packets of a given protocol */
410 static int log_invalid_proto_min = 0;
411 static int log_invalid_proto_max = 255;
413 static struct ctl_table_header *nf_ct_netfilter_header;
415 static ctl_table nf_ct_sysctl_table[] = {
417 .procname = "nf_conntrack_max",
418 .data = &nf_conntrack_max,
419 .maxlen = sizeof(int),
421 .proc_handler = proc_dointvec,
424 .procname = "nf_conntrack_count",
425 .data = &init_net.ct.count,
426 .maxlen = sizeof(int),
428 .proc_handler = proc_dointvec,
431 .procname = "nf_conntrack_buckets",
432 .data = &init_net.ct.htable_size,
433 .maxlen = sizeof(unsigned int),
435 .proc_handler = proc_dointvec,
438 .procname = "nf_conntrack_checksum",
439 .data = &init_net.ct.sysctl_checksum,
440 .maxlen = sizeof(unsigned int),
442 .proc_handler = proc_dointvec,
445 .procname = "nf_conntrack_log_invalid",
446 .data = &init_net.ct.sysctl_log_invalid,
447 .maxlen = sizeof(unsigned int),
449 .proc_handler = proc_dointvec_minmax,
450 .extra1 = &log_invalid_proto_min,
451 .extra2 = &log_invalid_proto_max,
454 .procname = "nf_conntrack_expect_max",
455 .data = &nf_ct_expect_max,
456 .maxlen = sizeof(int),
458 .proc_handler = proc_dointvec,
463 #define NET_NF_CONNTRACK_MAX 2089
465 static ctl_table nf_ct_netfilter_table[] = {
467 .procname = "nf_conntrack_max",
468 .data = &nf_conntrack_max,
469 .maxlen = sizeof(int),
471 .proc_handler = proc_dointvec,
476 static struct ctl_path nf_ct_path[] = {
477 { .procname = "net", },
481 static int nf_conntrack_standalone_init_sysctl(struct net *net)
483 struct ctl_table *table;
485 if (net_eq(net, &init_net)) {
486 nf_ct_netfilter_header =
487 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
488 if (!nf_ct_netfilter_header)
492 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
497 table[1].data = &net->ct.count;
498 table[2].data = &net->ct.htable_size;
499 table[3].data = &net->ct.sysctl_checksum;
500 table[4].data = &net->ct.sysctl_log_invalid;
502 net->ct.sysctl_header = register_net_sysctl_table(net,
503 nf_net_netfilter_sysctl_path, table);
504 if (!net->ct.sysctl_header)
505 goto out_unregister_netfilter;
509 out_unregister_netfilter:
512 if (net_eq(net, &init_net))
513 unregister_sysctl_table(nf_ct_netfilter_header);
515 printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
519 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
521 struct ctl_table *table;
523 if (net_eq(net, &init_net))
524 unregister_sysctl_table(nf_ct_netfilter_header);
525 table = net->ct.sysctl_header->ctl_table_arg;
526 unregister_net_sysctl_table(net->ct.sysctl_header);
530 static int nf_conntrack_standalone_init_sysctl(struct net *net)
535 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
538 #endif /* CONFIG_SYSCTL */
540 static int nf_conntrack_net_init(struct net *net)
544 ret = nf_conntrack_init(net);
547 ret = nf_conntrack_standalone_init_proc(net);
550 net->ct.sysctl_checksum = 1;
551 net->ct.sysctl_log_invalid = 0;
552 ret = nf_conntrack_standalone_init_sysctl(net);
558 nf_conntrack_standalone_fini_proc(net);
560 nf_conntrack_cleanup(net);
565 static void nf_conntrack_net_exit(struct net *net)
567 nf_conntrack_standalone_fini_sysctl(net);
568 nf_conntrack_standalone_fini_proc(net);
569 nf_conntrack_cleanup(net);
572 static struct pernet_operations nf_conntrack_net_ops = {
573 .init = nf_conntrack_net_init,
574 .exit = nf_conntrack_net_exit,
577 static int __init nf_conntrack_standalone_init(void)
579 return register_pernet_subsys(&nf_conntrack_net_ops);
582 static void __exit nf_conntrack_standalone_fini(void)
584 unregister_pernet_subsys(&nf_conntrack_net_ops);
587 module_init(nf_conntrack_standalone_init);
588 module_exit(nf_conntrack_standalone_fini);
590 /* Some modules need us, but don't depend directly on any symbol.
591 They should call this. */
592 void need_conntrack(void)
595 EXPORT_SYMBOL_GPL(need_conntrack);