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 return l3proto
->print_tuple(s
, tuple
) || l4proto
->print_tuple(s
, tuple
);
46 EXPORT_SYMBOL_GPL(print_tuple
);
48 struct ct_iter_state
{
49 struct seq_net_private p
;
54 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
56 struct net
*net
= seq_file_net(seq
);
57 struct ct_iter_state
*st
= seq
->private;
58 struct hlist_nulls_node
*n
;
61 st
->bucket
< net
->ct
.htable_size
;
63 n
= rcu_dereference(hlist_nulls_first_rcu(&net
->ct
.hash
[st
->bucket
]));
70 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
71 struct hlist_nulls_node
*head
)
73 struct net
*net
= seq_file_net(seq
);
74 struct ct_iter_state
*st
= seq
->private;
76 head
= rcu_dereference(hlist_nulls_next_rcu(head
));
77 while (is_a_nulls(head
)) {
78 if (likely(get_nulls_value(head
) == st
->bucket
)) {
79 if (++st
->bucket
>= net
->ct
.htable_size
)
82 head
= rcu_dereference(
83 hlist_nulls_first_rcu(
84 &net
->ct
.hash
[st
->bucket
]));
89 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
91 struct hlist_nulls_node
*head
= ct_get_first(seq
);
94 while (pos
&& (head
= ct_get_next(seq
, head
)))
96 return pos
? NULL
: head
;
99 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
102 struct ct_iter_state
*st
= seq
->private;
104 st
->time_now
= ktime_to_ns(ktime_get_real());
106 return ct_get_idx(seq
, *pos
);
109 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
112 return ct_get_next(s
, v
);
115 static void ct_seq_stop(struct seq_file
*s
, void *v
)
121 #ifdef CONFIG_NF_CONNTRACK_SECMARK
122 static int ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
128 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
132 ret
= seq_printf(s
, "secctx=%s ", secctx
);
134 security_release_secctx(secctx
, len
);
138 static inline int ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
144 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
145 static int ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
147 struct ct_iter_state
*st
= s
->private;
148 struct nf_conn_tstamp
*tstamp
;
151 tstamp
= nf_conn_tstamp_find(ct
);
153 delta_time
= st
->time_now
- tstamp
->start
;
155 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
159 return seq_printf(s
, "delta-time=%llu ",
160 (unsigned long long)delta_time
);
166 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
172 /* return 0 on success, 1 in case of error */
173 static int ct_seq_show(struct seq_file
*s
, void *v
)
175 struct nf_conntrack_tuple_hash
*hash
= v
;
176 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
177 const struct nf_conntrack_l3proto
*l3proto
;
178 const struct nf_conntrack_l4proto
*l4proto
;
182 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
185 /* we only want to print DIR_ORIGINAL */
186 if (NF_CT_DIRECTION(hash
))
189 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
190 NF_CT_ASSERT(l3proto
);
191 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
192 NF_CT_ASSERT(l4proto
);
195 if (seq_printf(s
, "%-8s %u %-8s %u %ld ",
196 l3proto
->name
, nf_ct_l3num(ct
),
197 l4proto
->name
, nf_ct_protonum(ct
),
198 timer_pending(&ct
->timeout
)
199 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
202 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
205 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
209 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
212 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
213 if (seq_printf(s
, "[UNREPLIED] "))
216 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
220 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
223 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
224 if (seq_printf(s
, "[ASSURED] "))
227 #if defined(CONFIG_NF_CONNTRACK_MARK)
228 if (seq_printf(s
, "mark=%u ", ct
->mark
))
232 if (ct_show_secctx(s
, ct
))
235 #ifdef CONFIG_NF_CONNTRACK_ZONES
236 if (seq_printf(s
, "zone=%u ", nf_ct_zone(ct
)))
240 if (ct_show_delta_time(s
, ct
))
243 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
252 static const struct seq_operations ct_seq_ops
= {
253 .start
= ct_seq_start
,
259 static int ct_open(struct inode
*inode
, struct file
*file
)
261 return seq_open_net(inode
, file
, &ct_seq_ops
,
262 sizeof(struct ct_iter_state
));
265 static const struct file_operations ct_file_ops
= {
266 .owner
= THIS_MODULE
,
270 .release
= seq_release_net
,
273 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
275 struct net
*net
= seq_file_net(seq
);
279 return SEQ_START_TOKEN
;
281 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
282 if (!cpu_possible(cpu
))
285 return per_cpu_ptr(net
->ct
.stat
, cpu
);
291 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
293 struct net
*net
= seq_file_net(seq
);
296 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
297 if (!cpu_possible(cpu
))
300 return per_cpu_ptr(net
->ct
.stat
, cpu
);
306 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
310 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
312 struct net
*net
= seq_file_net(seq
);
313 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
314 const struct ip_conntrack_stat
*st
= v
;
316 if (v
== SEQ_START_TOKEN
) {
317 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");
321 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
322 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
345 static const struct seq_operations ct_cpu_seq_ops
= {
346 .start
= ct_cpu_seq_start
,
347 .next
= ct_cpu_seq_next
,
348 .stop
= ct_cpu_seq_stop
,
349 .show
= ct_cpu_seq_show
,
352 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
354 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
355 sizeof(struct seq_net_private
));
358 static const struct file_operations ct_cpu_seq_fops
= {
359 .owner
= THIS_MODULE
,
360 .open
= ct_cpu_seq_open
,
363 .release
= seq_release_net
,
366 static int nf_conntrack_standalone_init_proc(struct net
*net
)
368 struct proc_dir_entry
*pde
;
370 pde
= proc_create("nf_conntrack", 0440, net
->proc_net
, &ct_file_ops
);
372 goto out_nf_conntrack
;
374 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
377 goto out_stat_nf_conntrack
;
380 out_stat_nf_conntrack
:
381 remove_proc_entry("nf_conntrack", net
->proc_net
);
386 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
388 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
389 remove_proc_entry("nf_conntrack", net
->proc_net
);
392 static int nf_conntrack_standalone_init_proc(struct net
*net
)
397 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
400 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
405 /* Log invalid packets of a given protocol */
406 static int log_invalid_proto_min
= 0;
407 static int log_invalid_proto_max
= 255;
409 static struct ctl_table_header
*nf_ct_netfilter_header
;
411 static struct ctl_table nf_ct_sysctl_table
[] = {
413 .procname
= "nf_conntrack_max",
414 .data
= &nf_conntrack_max
,
415 .maxlen
= sizeof(int),
417 .proc_handler
= proc_dointvec
,
420 .procname
= "nf_conntrack_count",
421 .data
= &init_net
.ct
.count
,
422 .maxlen
= sizeof(int),
424 .proc_handler
= proc_dointvec
,
427 .procname
= "nf_conntrack_buckets",
428 .data
= &init_net
.ct
.htable_size
,
429 .maxlen
= sizeof(unsigned int),
431 .proc_handler
= proc_dointvec
,
434 .procname
= "nf_conntrack_checksum",
435 .data
= &init_net
.ct
.sysctl_checksum
,
436 .maxlen
= sizeof(unsigned int),
438 .proc_handler
= proc_dointvec
,
441 .procname
= "nf_conntrack_log_invalid",
442 .data
= &init_net
.ct
.sysctl_log_invalid
,
443 .maxlen
= sizeof(unsigned int),
445 .proc_handler
= proc_dointvec_minmax
,
446 .extra1
= &log_invalid_proto_min
,
447 .extra2
= &log_invalid_proto_max
,
450 .procname
= "nf_conntrack_expect_max",
451 .data
= &nf_ct_expect_max
,
452 .maxlen
= sizeof(int),
454 .proc_handler
= proc_dointvec
,
459 #define NET_NF_CONNTRACK_MAX 2089
461 static struct ctl_table nf_ct_netfilter_table
[] = {
463 .procname
= "nf_conntrack_max",
464 .data
= &nf_conntrack_max
,
465 .maxlen
= sizeof(int),
467 .proc_handler
= proc_dointvec
,
472 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
474 struct ctl_table
*table
;
476 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
481 table
[1].data
= &net
->ct
.count
;
482 table
[2].data
= &net
->ct
.htable_size
;
483 table
[3].data
= &net
->ct
.sysctl_checksum
;
484 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
486 /* Don't export sysctls to unprivileged users */
487 if (net
->user_ns
!= &init_user_ns
)
488 table
[0].procname
= NULL
;
490 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
491 if (!net
->ct
.sysctl_header
)
492 goto out_unregister_netfilter
;
496 out_unregister_netfilter
:
502 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
504 struct ctl_table
*table
;
506 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
507 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
511 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
516 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
519 #endif /* CONFIG_SYSCTL */
521 static int nf_conntrack_pernet_init(struct net
*net
)
525 ret
= nf_conntrack_init_net(net
);
529 ret
= nf_conntrack_standalone_init_proc(net
);
533 net
->ct
.sysctl_checksum
= 1;
534 net
->ct
.sysctl_log_invalid
= 0;
535 ret
= nf_conntrack_standalone_init_sysctl(net
);
542 nf_conntrack_standalone_fini_proc(net
);
544 nf_conntrack_cleanup_net(net
);
549 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
553 list_for_each_entry(net
, net_exit_list
, exit_list
) {
554 nf_conntrack_standalone_fini_sysctl(net
);
555 nf_conntrack_standalone_fini_proc(net
);
557 nf_conntrack_cleanup_net_list(net_exit_list
);
560 static struct pernet_operations nf_conntrack_net_ops
= {
561 .init
= nf_conntrack_pernet_init
,
562 .exit_batch
= nf_conntrack_pernet_exit
,
565 static int __init
nf_conntrack_standalone_init(void)
567 int ret
= nf_conntrack_init_start();
572 nf_ct_netfilter_header
=
573 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
574 if (!nf_ct_netfilter_header
) {
575 pr_err("nf_conntrack: can't register to sysctl.\n");
581 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
585 nf_conntrack_init_end();
590 unregister_net_sysctl_table(nf_ct_netfilter_header
);
593 nf_conntrack_cleanup_end();
598 static void __exit
nf_conntrack_standalone_fini(void)
600 nf_conntrack_cleanup_start();
601 unregister_pernet_subsys(&nf_conntrack_net_ops
);
603 unregister_net_sysctl_table(nf_ct_netfilter_header
);
605 nf_conntrack_cleanup_end();
608 module_init(nf_conntrack_standalone_init
);
609 module_exit(nf_conntrack_standalone_fini
);
611 /* Some modules need us, but don't depend directly on any symbol.
612 They should call this. */
613 void need_conntrack(void)
616 EXPORT_SYMBOL_GPL(need_conntrack
);