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
,
147 const struct nf_conntrack_zone
*zone
= nf_ct_zone(ct
);
149 if (zone
->dir
!= dir
)
152 case NF_CT_DEFAULT_ZONE_DIR
:
153 seq_printf(s
, "zone=%u ", zone
->id
);
155 case NF_CT_ZONE_DIR_ORIG
:
156 seq_printf(s
, "zone-orig=%u ", zone
->id
);
158 case NF_CT_ZONE_DIR_REPL
:
159 seq_printf(s
, "zone-reply=%u ", zone
->id
);
166 static inline void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
172 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
173 static void ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
175 struct ct_iter_state
*st
= s
->private;
176 struct nf_conn_tstamp
*tstamp
;
179 tstamp
= nf_conn_tstamp_find(ct
);
181 delta_time
= st
->time_now
- tstamp
->start
;
183 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
187 seq_printf(s
, "delta-time=%llu ",
188 (unsigned long long)delta_time
);
194 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
199 /* return 0 on success, 1 in case of error */
200 static int ct_seq_show(struct seq_file
*s
, void *v
)
202 struct nf_conntrack_tuple_hash
*hash
= v
;
203 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
204 const struct nf_conntrack_l3proto
*l3proto
;
205 const struct nf_conntrack_l4proto
*l4proto
;
209 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
212 /* we only want to print DIR_ORIGINAL */
213 if (NF_CT_DIRECTION(hash
))
216 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
217 NF_CT_ASSERT(l3proto
);
218 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
219 NF_CT_ASSERT(l4proto
);
222 seq_printf(s
, "%-8s %u %-8s %u %ld ",
223 l3proto
->name
, nf_ct_l3num(ct
),
224 l4proto
->name
, nf_ct_protonum(ct
),
225 timer_pending(&ct
->timeout
)
226 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0);
228 if (l4proto
->print_conntrack
)
229 l4proto
->print_conntrack(s
, ct
);
231 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
234 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_ORIG
);
236 if (seq_has_overflowed(s
))
239 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
242 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
243 seq_printf(s
, "[UNREPLIED] ");
245 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
248 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_REPL
);
250 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
253 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
254 seq_printf(s
, "[ASSURED] ");
256 if (seq_has_overflowed(s
))
259 #if defined(CONFIG_NF_CONNTRACK_MARK)
260 seq_printf(s
, "mark=%u ", ct
->mark
);
263 ct_show_secctx(s
, ct
);
264 ct_show_zone(s
, ct
, NF_CT_DEFAULT_ZONE_DIR
);
265 ct_show_delta_time(s
, ct
);
267 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
269 if (seq_has_overflowed(s
))
278 static const struct seq_operations ct_seq_ops
= {
279 .start
= ct_seq_start
,
285 static int ct_open(struct inode
*inode
, struct file
*file
)
287 return seq_open_net(inode
, file
, &ct_seq_ops
,
288 sizeof(struct ct_iter_state
));
291 static const struct file_operations ct_file_ops
= {
292 .owner
= THIS_MODULE
,
296 .release
= seq_release_net
,
299 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
301 struct net
*net
= seq_file_net(seq
);
305 return SEQ_START_TOKEN
;
307 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
308 if (!cpu_possible(cpu
))
311 return per_cpu_ptr(net
->ct
.stat
, cpu
);
317 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
319 struct net
*net
= seq_file_net(seq
);
322 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
323 if (!cpu_possible(cpu
))
326 return per_cpu_ptr(net
->ct
.stat
, cpu
);
332 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
336 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
338 struct net
*net
= seq_file_net(seq
);
339 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
340 const struct ip_conntrack_stat
*st
= v
;
342 if (v
== SEQ_START_TOKEN
) {
343 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");
347 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
348 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
371 static const struct seq_operations ct_cpu_seq_ops
= {
372 .start
= ct_cpu_seq_start
,
373 .next
= ct_cpu_seq_next
,
374 .stop
= ct_cpu_seq_stop
,
375 .show
= ct_cpu_seq_show
,
378 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
380 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
381 sizeof(struct seq_net_private
));
384 static const struct file_operations ct_cpu_seq_fops
= {
385 .owner
= THIS_MODULE
,
386 .open
= ct_cpu_seq_open
,
389 .release
= seq_release_net
,
392 static int nf_conntrack_standalone_init_proc(struct net
*net
)
394 struct proc_dir_entry
*pde
;
398 pde
= proc_create("nf_conntrack", 0440, net
->proc_net
, &ct_file_ops
);
400 goto out_nf_conntrack
;
402 root_uid
= make_kuid(net
->user_ns
, 0);
403 root_gid
= make_kgid(net
->user_ns
, 0);
404 if (uid_valid(root_uid
) && gid_valid(root_gid
))
405 proc_set_user(pde
, root_uid
, root_gid
);
407 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
410 goto out_stat_nf_conntrack
;
413 out_stat_nf_conntrack
:
414 remove_proc_entry("nf_conntrack", net
->proc_net
);
419 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
421 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
422 remove_proc_entry("nf_conntrack", net
->proc_net
);
425 static int nf_conntrack_standalone_init_proc(struct net
*net
)
430 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
433 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
438 /* Log invalid packets of a given protocol */
439 static int log_invalid_proto_min
= 0;
440 static int log_invalid_proto_max
= 255;
442 static struct ctl_table_header
*nf_ct_netfilter_header
;
444 static struct ctl_table nf_ct_sysctl_table
[] = {
446 .procname
= "nf_conntrack_max",
447 .data
= &nf_conntrack_max
,
448 .maxlen
= sizeof(int),
450 .proc_handler
= proc_dointvec
,
453 .procname
= "nf_conntrack_count",
454 .data
= &init_net
.ct
.count
,
455 .maxlen
= sizeof(int),
457 .proc_handler
= proc_dointvec
,
460 .procname
= "nf_conntrack_buckets",
461 .data
= &init_net
.ct
.htable_size
,
462 .maxlen
= sizeof(unsigned int),
464 .proc_handler
= proc_dointvec
,
467 .procname
= "nf_conntrack_checksum",
468 .data
= &init_net
.ct
.sysctl_checksum
,
469 .maxlen
= sizeof(unsigned int),
471 .proc_handler
= proc_dointvec
,
474 .procname
= "nf_conntrack_log_invalid",
475 .data
= &init_net
.ct
.sysctl_log_invalid
,
476 .maxlen
= sizeof(unsigned int),
478 .proc_handler
= proc_dointvec_minmax
,
479 .extra1
= &log_invalid_proto_min
,
480 .extra2
= &log_invalid_proto_max
,
483 .procname
= "nf_conntrack_expect_max",
484 .data
= &nf_ct_expect_max
,
485 .maxlen
= sizeof(int),
487 .proc_handler
= proc_dointvec
,
492 #define NET_NF_CONNTRACK_MAX 2089
494 static struct ctl_table nf_ct_netfilter_table
[] = {
496 .procname
= "nf_conntrack_max",
497 .data
= &nf_conntrack_max
,
498 .maxlen
= sizeof(int),
500 .proc_handler
= proc_dointvec
,
505 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
507 struct ctl_table
*table
;
509 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
514 table
[1].data
= &net
->ct
.count
;
515 table
[2].data
= &net
->ct
.htable_size
;
516 table
[3].data
= &net
->ct
.sysctl_checksum
;
517 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
519 /* Don't export sysctls to unprivileged users */
520 if (net
->user_ns
!= &init_user_ns
)
521 table
[0].procname
= NULL
;
523 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
524 if (!net
->ct
.sysctl_header
)
525 goto out_unregister_netfilter
;
529 out_unregister_netfilter
:
535 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
537 struct ctl_table
*table
;
539 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
540 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
544 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
549 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
552 #endif /* CONFIG_SYSCTL */
554 static int nf_conntrack_pernet_init(struct net
*net
)
558 ret
= nf_conntrack_init_net(net
);
562 ret
= nf_conntrack_standalone_init_proc(net
);
566 net
->ct
.sysctl_checksum
= 1;
567 net
->ct
.sysctl_log_invalid
= 0;
568 ret
= nf_conntrack_standalone_init_sysctl(net
);
575 nf_conntrack_standalone_fini_proc(net
);
577 nf_conntrack_cleanup_net(net
);
582 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
586 list_for_each_entry(net
, net_exit_list
, exit_list
) {
587 nf_conntrack_standalone_fini_sysctl(net
);
588 nf_conntrack_standalone_fini_proc(net
);
590 nf_conntrack_cleanup_net_list(net_exit_list
);
593 static struct pernet_operations nf_conntrack_net_ops
= {
594 .init
= nf_conntrack_pernet_init
,
595 .exit_batch
= nf_conntrack_pernet_exit
,
598 static int __init
nf_conntrack_standalone_init(void)
600 int ret
= nf_conntrack_init_start();
605 nf_ct_netfilter_header
=
606 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
607 if (!nf_ct_netfilter_header
) {
608 pr_err("nf_conntrack: can't register to sysctl.\n");
614 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
618 nf_conntrack_init_end();
623 unregister_net_sysctl_table(nf_ct_netfilter_header
);
626 nf_conntrack_cleanup_end();
631 static void __exit
nf_conntrack_standalone_fini(void)
633 nf_conntrack_cleanup_start();
634 unregister_pernet_subsys(&nf_conntrack_net_ops
);
636 unregister_net_sysctl_table(nf_ct_netfilter_header
);
638 nf_conntrack_cleanup_end();
641 module_init(nf_conntrack_standalone_init
);
642 module_exit(nf_conntrack_standalone_fini
);
644 /* Some modules need us, but don't depend directly on any symbol.
645 They should call this. */
646 void need_conntrack(void)
649 EXPORT_SYMBOL_GPL(need_conntrack
);