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 net
*net
= seq_file_net(seq
);
58 struct ct_iter_state
*st
= seq
->private;
59 struct hlist_nulls_node
*n
;
62 st
->bucket
< net
->ct
.htable_size
;
64 n
= rcu_dereference(hlist_nulls_first_rcu(&net
->ct
.hash
[st
->bucket
]));
71 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
72 struct hlist_nulls_node
*head
)
74 struct net
*net
= seq_file_net(seq
);
75 struct ct_iter_state
*st
= seq
->private;
77 head
= rcu_dereference(hlist_nulls_next_rcu(head
));
78 while (is_a_nulls(head
)) {
79 if (likely(get_nulls_value(head
) == st
->bucket
)) {
80 if (++st
->bucket
>= net
->ct
.htable_size
)
83 head
= rcu_dereference(
84 hlist_nulls_first_rcu(
85 &net
->ct
.hash
[st
->bucket
]));
90 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
92 struct hlist_nulls_node
*head
= ct_get_first(seq
);
95 while (pos
&& (head
= ct_get_next(seq
, head
)))
97 return pos
? NULL
: head
;
100 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
103 struct ct_iter_state
*st
= seq
->private;
105 st
->time_now
= ktime_get_real_ns();
107 return ct_get_idx(seq
, *pos
);
110 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
113 return ct_get_next(s
, v
);
116 static void ct_seq_stop(struct seq_file
*s
, void *v
)
122 #ifdef CONFIG_NF_CONNTRACK_SECMARK
123 static void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
129 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
133 seq_printf(s
, "secctx=%s ", secctx
);
135 security_release_secctx(secctx
, len
);
138 static inline void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
143 #ifdef CONFIG_NF_CONNTRACK_ZONES
144 static void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
)
146 seq_printf(s
, "zone=%u ", nf_ct_zone(ct
)->id
);
149 static inline void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
)
154 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
155 static void ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
157 struct ct_iter_state
*st
= s
->private;
158 struct nf_conn_tstamp
*tstamp
;
161 tstamp
= nf_conn_tstamp_find(ct
);
163 delta_time
= st
->time_now
- tstamp
->start
;
165 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
169 seq_printf(s
, "delta-time=%llu ",
170 (unsigned long long)delta_time
);
176 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
181 /* return 0 on success, 1 in case of error */
182 static int ct_seq_show(struct seq_file
*s
, void *v
)
184 struct nf_conntrack_tuple_hash
*hash
= v
;
185 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
186 const struct nf_conntrack_l3proto
*l3proto
;
187 const struct nf_conntrack_l4proto
*l4proto
;
191 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
194 /* we only want to print DIR_ORIGINAL */
195 if (NF_CT_DIRECTION(hash
))
198 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
199 NF_CT_ASSERT(l3proto
);
200 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
201 NF_CT_ASSERT(l4proto
);
204 seq_printf(s
, "%-8s %u %-8s %u %ld ",
205 l3proto
->name
, nf_ct_l3num(ct
),
206 l4proto
->name
, nf_ct_protonum(ct
),
207 timer_pending(&ct
->timeout
)
208 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0);
210 if (l4proto
->print_conntrack
)
211 l4proto
->print_conntrack(s
, ct
);
213 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
216 if (seq_has_overflowed(s
))
219 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
222 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
223 seq_printf(s
, "[UNREPLIED] ");
225 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
228 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
231 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
232 seq_printf(s
, "[ASSURED] ");
234 if (seq_has_overflowed(s
))
237 #if defined(CONFIG_NF_CONNTRACK_MARK)
238 seq_printf(s
, "mark=%u ", ct
->mark
);
241 ct_show_secctx(s
, ct
);
243 ct_show_delta_time(s
, ct
);
245 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
247 if (seq_has_overflowed(s
))
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_create("nf_conntrack", 0440, net
->proc_net
, &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 remove_proc_entry("nf_conntrack", net
->proc_net
);
390 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
392 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
393 remove_proc_entry("nf_conntrack", net
->proc_net
);
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_NF_CONNTRACK_PROCFS */
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 struct 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 struct 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 int nf_conntrack_standalone_init_sysctl(struct net
*net
)
478 struct ctl_table
*table
;
480 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
485 table
[1].data
= &net
->ct
.count
;
486 table
[2].data
= &net
->ct
.htable_size
;
487 table
[3].data
= &net
->ct
.sysctl_checksum
;
488 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
490 /* Don't export sysctls to unprivileged users */
491 if (net
->user_ns
!= &init_user_ns
)
492 table
[0].procname
= NULL
;
494 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
495 if (!net
->ct
.sysctl_header
)
496 goto out_unregister_netfilter
;
500 out_unregister_netfilter
:
506 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
508 struct ctl_table
*table
;
510 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
511 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
515 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
520 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
523 #endif /* CONFIG_SYSCTL */
525 static int nf_conntrack_pernet_init(struct net
*net
)
529 ret
= nf_conntrack_init_net(net
);
533 ret
= nf_conntrack_standalone_init_proc(net
);
537 net
->ct
.sysctl_checksum
= 1;
538 net
->ct
.sysctl_log_invalid
= 0;
539 ret
= nf_conntrack_standalone_init_sysctl(net
);
546 nf_conntrack_standalone_fini_proc(net
);
548 nf_conntrack_cleanup_net(net
);
553 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
557 list_for_each_entry(net
, net_exit_list
, exit_list
) {
558 nf_conntrack_standalone_fini_sysctl(net
);
559 nf_conntrack_standalone_fini_proc(net
);
561 nf_conntrack_cleanup_net_list(net_exit_list
);
564 static struct pernet_operations nf_conntrack_net_ops
= {
565 .init
= nf_conntrack_pernet_init
,
566 .exit_batch
= nf_conntrack_pernet_exit
,
569 static int __init
nf_conntrack_standalone_init(void)
571 int ret
= nf_conntrack_init_start();
576 nf_ct_netfilter_header
=
577 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
578 if (!nf_ct_netfilter_header
) {
579 pr_err("nf_conntrack: can't register to sysctl.\n");
585 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
589 nf_conntrack_init_end();
594 unregister_net_sysctl_table(nf_ct_netfilter_header
);
597 nf_conntrack_cleanup_end();
602 static void __exit
nf_conntrack_standalone_fini(void)
604 nf_conntrack_cleanup_start();
605 unregister_pernet_subsys(&nf_conntrack_net_ops
);
607 unregister_net_sysctl_table(nf_ct_netfilter_header
);
609 nf_conntrack_cleanup_end();
612 module_init(nf_conntrack_standalone_init
);
613 module_exit(nf_conntrack_standalone_fini
);
615 /* Some modules need us, but don't depend directly on any symbol.
616 They should call this. */
617 void need_conntrack(void)
620 EXPORT_SYMBOL_GPL(need_conntrack
);