1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/types.h>
11 #include <linux/netfilter.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/percpu.h>
18 #include <linux/netdevice.h>
19 #include <linux/security.h>
20 #include <net/net_namespace.h>
22 #include <linux/sysctl.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_acct.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_timestamp.h>
34 #include <linux/rculist_nulls.h>
36 MODULE_LICENSE("GPL");
38 #ifdef CONFIG_NF_CONNTRACK_PROCFS
40 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
41 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
44 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
47 EXPORT_SYMBOL_GPL(print_tuple);
49 struct ct_iter_state {
50 struct seq_net_private p;
55 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
57 struct ct_iter_state *st = seq->private;
58 struct hlist_nulls_node *n;
61 st->bucket < nf_conntrack_htable_size;
63 n = rcu_dereference(hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
70 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
71 struct hlist_nulls_node *head)
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 >= nf_conntrack_htable_size)
81 head = rcu_dereference(
82 hlist_nulls_first_rcu(
83 &nf_conntrack_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_get_real_ns();
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 void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
127 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
131 seq_printf(s, "secctx=%s ", secctx);
133 security_release_secctx(secctx, len);
136 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
141 #ifdef CONFIG_NF_CONNTRACK_ZONES
142 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
145 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
147 if (zone->dir != dir)
150 case NF_CT_DEFAULT_ZONE_DIR:
151 seq_printf(s, "zone=%u ", zone->id);
153 case NF_CT_ZONE_DIR_ORIG:
154 seq_printf(s, "zone-orig=%u ", zone->id);
156 case NF_CT_ZONE_DIR_REPL:
157 seq_printf(s, "zone-reply=%u ", zone->id);
164 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
170 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
171 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
173 struct ct_iter_state *st = s->private;
174 struct nf_conn_tstamp *tstamp;
177 tstamp = nf_conn_tstamp_find(ct);
179 delta_time = st->time_now - tstamp->start;
181 delta_time = div_s64(delta_time, NSEC_PER_SEC);
185 seq_printf(s, "delta-time=%llu ",
186 (unsigned long long)delta_time);
192 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
197 /* return 0 on success, 1 in case of error */
198 static int ct_seq_show(struct seq_file *s, void *v)
200 struct nf_conntrack_tuple_hash *hash = v;
201 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
202 const struct nf_conntrack_l3proto *l3proto;
203 const struct nf_conntrack_l4proto *l4proto;
207 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
210 /* we only want to print DIR_ORIGINAL */
211 if (NF_CT_DIRECTION(hash))
214 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
215 NF_CT_ASSERT(l3proto);
216 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
217 NF_CT_ASSERT(l4proto);
220 seq_printf(s, "%-8s %u %-8s %u %ld ",
221 l3proto->name, nf_ct_l3num(ct),
222 l4proto->name, nf_ct_protonum(ct),
223 timer_pending(&ct->timeout)
224 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
226 if (l4proto->print_conntrack)
227 l4proto->print_conntrack(s, ct);
229 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
232 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
234 if (seq_has_overflowed(s))
237 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
240 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
241 seq_printf(s, "[UNREPLIED] ");
243 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
246 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
248 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
251 if (test_bit(IPS_ASSURED_BIT, &ct->status))
252 seq_printf(s, "[ASSURED] ");
254 if (seq_has_overflowed(s))
257 #if defined(CONFIG_NF_CONNTRACK_MARK)
258 seq_printf(s, "mark=%u ", ct->mark);
261 ct_show_secctx(s, ct);
262 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
263 ct_show_delta_time(s, ct);
265 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
267 if (seq_has_overflowed(s))
276 static const struct seq_operations ct_seq_ops = {
277 .start = ct_seq_start,
283 static int ct_open(struct inode *inode, struct file *file)
285 return seq_open_net(inode, file, &ct_seq_ops,
286 sizeof(struct ct_iter_state));
289 static const struct file_operations ct_file_ops = {
290 .owner = THIS_MODULE,
294 .release = seq_release_net,
297 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
299 struct net *net = seq_file_net(seq);
303 return SEQ_START_TOKEN;
305 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
306 if (!cpu_possible(cpu))
309 return per_cpu_ptr(net->ct.stat, cpu);
315 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
317 struct net *net = seq_file_net(seq);
320 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
321 if (!cpu_possible(cpu))
324 return per_cpu_ptr(net->ct.stat, cpu);
330 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
334 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
336 struct net *net = seq_file_net(seq);
337 unsigned int nr_conntracks = atomic_read(&net->ct.count);
338 const struct ip_conntrack_stat *st = v;
340 if (v == SEQ_START_TOKEN) {
341 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");
345 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
346 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
369 static const struct seq_operations ct_cpu_seq_ops = {
370 .start = ct_cpu_seq_start,
371 .next = ct_cpu_seq_next,
372 .stop = ct_cpu_seq_stop,
373 .show = ct_cpu_seq_show,
376 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
378 return seq_open_net(inode, file, &ct_cpu_seq_ops,
379 sizeof(struct seq_net_private));
382 static const struct file_operations ct_cpu_seq_fops = {
383 .owner = THIS_MODULE,
384 .open = ct_cpu_seq_open,
387 .release = seq_release_net,
390 static int nf_conntrack_standalone_init_proc(struct net *net)
392 struct proc_dir_entry *pde;
396 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
398 goto out_nf_conntrack;
400 root_uid = make_kuid(net->user_ns, 0);
401 root_gid = make_kgid(net->user_ns, 0);
402 if (uid_valid(root_uid) && gid_valid(root_gid))
403 proc_set_user(pde, root_uid, root_gid);
405 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
408 goto out_stat_nf_conntrack;
411 out_stat_nf_conntrack:
412 remove_proc_entry("nf_conntrack", net->proc_net);
417 static void nf_conntrack_standalone_fini_proc(struct net *net)
419 remove_proc_entry("nf_conntrack", net->proc_net_stat);
420 remove_proc_entry("nf_conntrack", net->proc_net);
423 static int nf_conntrack_standalone_init_proc(struct net *net)
428 static void nf_conntrack_standalone_fini_proc(struct net *net)
431 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
436 /* Log invalid packets of a given protocol */
437 static int log_invalid_proto_min = 0;
438 static int log_invalid_proto_max = 255;
440 static struct ctl_table_header *nf_ct_netfilter_header;
442 static struct ctl_table nf_ct_sysctl_table[] = {
444 .procname = "nf_conntrack_max",
445 .data = &nf_conntrack_max,
446 .maxlen = sizeof(int),
448 .proc_handler = proc_dointvec,
451 .procname = "nf_conntrack_count",
452 .data = &init_net.ct.count,
453 .maxlen = sizeof(int),
455 .proc_handler = proc_dointvec,
458 .procname = "nf_conntrack_buckets",
459 .data = &nf_conntrack_htable_size,
460 .maxlen = sizeof(unsigned int),
462 .proc_handler = proc_dointvec,
465 .procname = "nf_conntrack_checksum",
466 .data = &init_net.ct.sysctl_checksum,
467 .maxlen = sizeof(unsigned int),
469 .proc_handler = proc_dointvec,
472 .procname = "nf_conntrack_log_invalid",
473 .data = &init_net.ct.sysctl_log_invalid,
474 .maxlen = sizeof(unsigned int),
476 .proc_handler = proc_dointvec_minmax,
477 .extra1 = &log_invalid_proto_min,
478 .extra2 = &log_invalid_proto_max,
481 .procname = "nf_conntrack_expect_max",
482 .data = &nf_ct_expect_max,
483 .maxlen = sizeof(int),
485 .proc_handler = proc_dointvec,
490 static struct ctl_table nf_ct_netfilter_table[] = {
492 .procname = "nf_conntrack_max",
493 .data = &nf_conntrack_max,
494 .maxlen = sizeof(int),
496 .proc_handler = proc_dointvec,
501 static int nf_conntrack_standalone_init_sysctl(struct net *net)
503 struct ctl_table *table;
505 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
510 table[1].data = &net->ct.count;
511 table[3].data = &net->ct.sysctl_checksum;
512 table[4].data = &net->ct.sysctl_log_invalid;
514 /* Don't export sysctls to unprivileged users */
515 if (net->user_ns != &init_user_ns)
516 table[0].procname = NULL;
518 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
519 if (!net->ct.sysctl_header)
520 goto out_unregister_netfilter;
524 out_unregister_netfilter:
530 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
532 struct ctl_table *table;
534 table = net->ct.sysctl_header->ctl_table_arg;
535 unregister_net_sysctl_table(net->ct.sysctl_header);
539 static int nf_conntrack_standalone_init_sysctl(struct net *net)
544 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
547 #endif /* CONFIG_SYSCTL */
549 static int nf_conntrack_pernet_init(struct net *net)
553 ret = nf_conntrack_init_net(net);
557 ret = nf_conntrack_standalone_init_proc(net);
561 net->ct.sysctl_checksum = 1;
562 net->ct.sysctl_log_invalid = 0;
563 ret = nf_conntrack_standalone_init_sysctl(net);
570 nf_conntrack_standalone_fini_proc(net);
572 nf_conntrack_cleanup_net(net);
577 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
581 list_for_each_entry(net, net_exit_list, exit_list) {
582 nf_conntrack_standalone_fini_sysctl(net);
583 nf_conntrack_standalone_fini_proc(net);
585 nf_conntrack_cleanup_net_list(net_exit_list);
588 static struct pernet_operations nf_conntrack_net_ops = {
589 .init = nf_conntrack_pernet_init,
590 .exit_batch = nf_conntrack_pernet_exit,
593 static int __init nf_conntrack_standalone_init(void)
595 int ret = nf_conntrack_init_start();
600 nf_ct_netfilter_header =
601 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
602 if (!nf_ct_netfilter_header) {
603 pr_err("nf_conntrack: can't register to sysctl.\n");
609 ret = register_pernet_subsys(&nf_conntrack_net_ops);
613 nf_conntrack_init_end();
618 unregister_net_sysctl_table(nf_ct_netfilter_header);
621 nf_conntrack_cleanup_end();
626 static void __exit nf_conntrack_standalone_fini(void)
628 nf_conntrack_cleanup_start();
629 unregister_pernet_subsys(&nf_conntrack_net_ops);
631 unregister_net_sysctl_table(nf_ct_netfilter_header);
633 nf_conntrack_cleanup_end();
636 module_init(nf_conntrack_standalone_init);
637 module_exit(nf_conntrack_standalone_fini);
639 /* Some modules need us, but don't depend directly on any symbol.
640 They should call this. */
641 void need_conntrack(void)
644 EXPORT_SYMBOL_GPL(need_conntrack);