1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/netfilter.h>
4 #include <linux/slab.h>
5 #include <linux/module.h>
6 #include <linux/skbuff.h>
7 #include <linux/proc_fs.h>
8 #include <linux/seq_file.h>
9 #include <linux/percpu.h>
10 #include <linux/netdevice.h>
11 #include <linux/security.h>
12 #include <net/net_namespace.h>
14 #include <linux/sysctl.h>
17 #include <net/netfilter/nf_conntrack.h>
18 #include <net/netfilter/nf_conntrack_core.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_acct.h>
23 #include <net/netfilter/nf_conntrack_zones.h>
24 #include <net/netfilter/nf_conntrack_timestamp.h>
25 #include <linux/rculist_nulls.h>
27 unsigned int nf_conntrack_net_id __read_mostly
;
29 #ifdef CONFIG_NF_CONNTRACK_PROCFS
31 print_tuple(struct seq_file
*s
, const struct nf_conntrack_tuple
*tuple
,
32 const struct nf_conntrack_l4proto
*l4proto
)
34 switch (tuple
->src
.l3num
) {
36 seq_printf(s
, "src=%pI4 dst=%pI4 ",
37 &tuple
->src
.u3
.ip
, &tuple
->dst
.u3
.ip
);
40 seq_printf(s
, "src=%pI6 dst=%pI6 ",
41 tuple
->src
.u3
.ip6
, tuple
->dst
.u3
.ip6
);
47 switch (l4proto
->l4proto
) {
49 seq_printf(s
, "type=%u code=%u id=%u ",
50 tuple
->dst
.u
.icmp
.type
,
51 tuple
->dst
.u
.icmp
.code
,
52 ntohs(tuple
->src
.u
.icmp
.id
));
55 seq_printf(s
, "sport=%hu dport=%hu ",
56 ntohs(tuple
->src
.u
.tcp
.port
),
57 ntohs(tuple
->dst
.u
.tcp
.port
));
59 case IPPROTO_UDPLITE
: /* fallthrough */
61 seq_printf(s
, "sport=%hu dport=%hu ",
62 ntohs(tuple
->src
.u
.udp
.port
),
63 ntohs(tuple
->dst
.u
.udp
.port
));
67 seq_printf(s
, "sport=%hu dport=%hu ",
68 ntohs(tuple
->src
.u
.dccp
.port
),
69 ntohs(tuple
->dst
.u
.dccp
.port
));
72 seq_printf(s
, "sport=%hu dport=%hu ",
73 ntohs(tuple
->src
.u
.sctp
.port
),
74 ntohs(tuple
->dst
.u
.sctp
.port
));
77 seq_printf(s
, "type=%u code=%u id=%u ",
78 tuple
->dst
.u
.icmp
.type
,
79 tuple
->dst
.u
.icmp
.code
,
80 ntohs(tuple
->src
.u
.icmp
.id
));
83 seq_printf(s
, "srckey=0x%x dstkey=0x%x ",
84 ntohs(tuple
->src
.u
.gre
.key
),
85 ntohs(tuple
->dst
.u
.gre
.key
));
91 EXPORT_SYMBOL_GPL(print_tuple
);
93 struct ct_iter_state
{
94 struct seq_net_private p
;
95 struct hlist_nulls_head
*hash
;
96 unsigned int htable_size
;
101 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
103 struct ct_iter_state
*st
= seq
->private;
104 struct hlist_nulls_node
*n
;
107 st
->bucket
< st
->htable_size
;
110 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
117 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
118 struct hlist_nulls_node
*head
)
120 struct ct_iter_state
*st
= seq
->private;
122 head
= rcu_dereference(hlist_nulls_next_rcu(head
));
123 while (is_a_nulls(head
)) {
124 if (likely(get_nulls_value(head
) == st
->bucket
)) {
125 if (++st
->bucket
>= st
->htable_size
)
128 head
= rcu_dereference(
129 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
134 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
136 struct hlist_nulls_node
*head
= ct_get_first(seq
);
139 while (pos
&& (head
= ct_get_next(seq
, head
)))
141 return pos
? NULL
: head
;
144 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
147 struct ct_iter_state
*st
= seq
->private;
149 st
->time_now
= ktime_get_real_ns();
152 nf_conntrack_get_ht(&st
->hash
, &st
->htable_size
);
153 return ct_get_idx(seq
, *pos
);
156 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
159 return ct_get_next(s
, v
);
162 static void ct_seq_stop(struct seq_file
*s
, void *v
)
168 #ifdef CONFIG_NF_CONNTRACK_SECMARK
169 static void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
175 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
179 seq_printf(s
, "secctx=%s ", secctx
);
181 security_release_secctx(secctx
, len
);
184 static inline void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
189 #ifdef CONFIG_NF_CONNTRACK_ZONES
190 static void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
193 const struct nf_conntrack_zone
*zone
= nf_ct_zone(ct
);
195 if (zone
->dir
!= dir
)
198 case NF_CT_DEFAULT_ZONE_DIR
:
199 seq_printf(s
, "zone=%u ", zone
->id
);
201 case NF_CT_ZONE_DIR_ORIG
:
202 seq_printf(s
, "zone-orig=%u ", zone
->id
);
204 case NF_CT_ZONE_DIR_REPL
:
205 seq_printf(s
, "zone-reply=%u ", zone
->id
);
212 static inline void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
218 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
219 static void ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
221 struct ct_iter_state
*st
= s
->private;
222 struct nf_conn_tstamp
*tstamp
;
225 tstamp
= nf_conn_tstamp_find(ct
);
227 delta_time
= st
->time_now
- tstamp
->start
;
229 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
233 seq_printf(s
, "delta-time=%llu ",
234 (unsigned long long)delta_time
);
240 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
245 static const char* l3proto_name(u16 proto
)
248 case AF_INET
: return "ipv4";
249 case AF_INET6
: return "ipv6";
255 static const char* l4proto_name(u16 proto
)
258 case IPPROTO_ICMP
: return "icmp";
259 case IPPROTO_TCP
: return "tcp";
260 case IPPROTO_UDP
: return "udp";
261 case IPPROTO_DCCP
: return "dccp";
262 case IPPROTO_GRE
: return "gre";
263 case IPPROTO_SCTP
: return "sctp";
264 case IPPROTO_UDPLITE
: return "udplite";
271 seq_print_acct(struct seq_file
*s
, const struct nf_conn
*ct
, int dir
)
273 struct nf_conn_acct
*acct
;
274 struct nf_conn_counter
*counter
;
276 acct
= nf_conn_acct_find(ct
);
280 counter
= acct
->counter
;
281 seq_printf(s
, "packets=%llu bytes=%llu ",
282 (unsigned long long)atomic64_read(&counter
[dir
].packets
),
283 (unsigned long long)atomic64_read(&counter
[dir
].bytes
));
288 /* return 0 on success, 1 in case of error */
289 static int ct_seq_show(struct seq_file
*s
, void *v
)
291 struct nf_conntrack_tuple_hash
*hash
= v
;
292 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
293 const struct nf_conntrack_l4proto
*l4proto
;
294 struct net
*net
= seq_file_net(s
);
298 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
301 if (nf_ct_should_gc(ct
)) {
306 /* we only want to print DIR_ORIGINAL */
307 if (NF_CT_DIRECTION(hash
))
310 if (!net_eq(nf_ct_net(ct
), net
))
313 l4proto
= __nf_ct_l4proto_find(nf_ct_protonum(ct
));
317 seq_printf(s
, "%-8s %u %-8s %u ",
318 l3proto_name(nf_ct_l3num(ct
)), nf_ct_l3num(ct
),
319 l4proto_name(l4proto
->l4proto
), nf_ct_protonum(ct
));
321 if (!test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
322 seq_printf(s
, "%ld ", nf_ct_expires(ct
) / HZ
);
324 if (l4proto
->print_conntrack
)
325 l4proto
->print_conntrack(s
, ct
);
327 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
330 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_ORIG
);
332 if (seq_has_overflowed(s
))
335 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
338 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
339 seq_puts(s
, "[UNREPLIED] ");
341 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
, l4proto
);
343 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_REPL
);
345 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
348 if (test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
349 seq_puts(s
, "[OFFLOAD] ");
350 else if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
351 seq_puts(s
, "[ASSURED] ");
353 if (seq_has_overflowed(s
))
356 #if defined(CONFIG_NF_CONNTRACK_MARK)
357 seq_printf(s
, "mark=%u ", ct
->mark
);
360 ct_show_secctx(s
, ct
);
361 ct_show_zone(s
, ct
, NF_CT_DEFAULT_ZONE_DIR
);
362 ct_show_delta_time(s
, ct
);
364 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
366 if (seq_has_overflowed(s
))
375 static const struct seq_operations ct_seq_ops
= {
376 .start
= ct_seq_start
,
382 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
384 struct net
*net
= seq_file_net(seq
);
388 return SEQ_START_TOKEN
;
390 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
391 if (!cpu_possible(cpu
))
394 return per_cpu_ptr(net
->ct
.stat
, cpu
);
400 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
402 struct net
*net
= seq_file_net(seq
);
405 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
406 if (!cpu_possible(cpu
))
409 return per_cpu_ptr(net
->ct
.stat
, cpu
);
415 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
419 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
421 struct net
*net
= seq_file_net(seq
);
422 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
423 const struct ip_conntrack_stat
*st
= v
;
425 if (v
== SEQ_START_TOKEN
) {
426 seq_puts(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");
430 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
431 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
454 static const struct seq_operations ct_cpu_seq_ops
= {
455 .start
= ct_cpu_seq_start
,
456 .next
= ct_cpu_seq_next
,
457 .stop
= ct_cpu_seq_stop
,
458 .show
= ct_cpu_seq_show
,
461 static int nf_conntrack_standalone_init_proc(struct net
*net
)
463 struct proc_dir_entry
*pde
;
467 pde
= proc_create_net("nf_conntrack", 0440, net
->proc_net
, &ct_seq_ops
,
468 sizeof(struct ct_iter_state
));
470 goto out_nf_conntrack
;
472 root_uid
= make_kuid(net
->user_ns
, 0);
473 root_gid
= make_kgid(net
->user_ns
, 0);
474 if (uid_valid(root_uid
) && gid_valid(root_gid
))
475 proc_set_user(pde
, root_uid
, root_gid
);
477 pde
= proc_create_net("nf_conntrack", 0444, net
->proc_net_stat
,
478 &ct_cpu_seq_ops
, sizeof(struct seq_net_private
));
480 goto out_stat_nf_conntrack
;
483 out_stat_nf_conntrack
:
484 remove_proc_entry("nf_conntrack", net
->proc_net
);
489 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
491 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
492 remove_proc_entry("nf_conntrack", net
->proc_net
);
495 static int nf_conntrack_standalone_init_proc(struct net
*net
)
500 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
503 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
508 /* Log invalid packets of a given protocol */
509 static int log_invalid_proto_min __read_mostly
;
510 static int log_invalid_proto_max __read_mostly
= 255;
512 /* size the user *wants to set */
513 static unsigned int nf_conntrack_htable_size_user __read_mostly
;
516 nf_conntrack_hash_sysctl(struct ctl_table
*table
, int write
,
517 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
521 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
522 if (ret
< 0 || !write
)
525 /* update ret, we might not be able to satisfy request */
526 ret
= nf_conntrack_hash_resize(nf_conntrack_htable_size_user
);
528 /* update it to the actual value used by conntrack */
529 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
533 static struct ctl_table_header
*nf_ct_netfilter_header
;
535 enum nf_ct_sysctl_index
{
538 NF_SYSCTL_CT_BUCKETS
,
539 NF_SYSCTL_CT_CHECKSUM
,
540 NF_SYSCTL_CT_LOG_INVALID
,
541 NF_SYSCTL_CT_EXPECT_MAX
,
544 #ifdef CONFIG_NF_CONNTRACK_EVENTS
547 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
548 NF_SYSCTL_CT_TIMESTAMP
,
552 static struct ctl_table nf_ct_sysctl_table
[] = {
553 [NF_SYSCTL_CT_MAX
] = {
554 .procname
= "nf_conntrack_max",
555 .data
= &nf_conntrack_max
,
556 .maxlen
= sizeof(int),
558 .proc_handler
= proc_dointvec
,
560 [NF_SYSCTL_CT_COUNT
] = {
561 .procname
= "nf_conntrack_count",
562 .data
= &init_net
.ct
.count
,
563 .maxlen
= sizeof(int),
565 .proc_handler
= proc_dointvec
,
567 [NF_SYSCTL_CT_BUCKETS
] = {
568 .procname
= "nf_conntrack_buckets",
569 .data
= &nf_conntrack_htable_size_user
,
570 .maxlen
= sizeof(unsigned int),
572 .proc_handler
= nf_conntrack_hash_sysctl
,
574 [NF_SYSCTL_CT_CHECKSUM
] = {
575 .procname
= "nf_conntrack_checksum",
576 .data
= &init_net
.ct
.sysctl_checksum
,
577 .maxlen
= sizeof(unsigned int),
579 .proc_handler
= proc_dointvec
,
581 [NF_SYSCTL_CT_LOG_INVALID
] = {
582 .procname
= "nf_conntrack_log_invalid",
583 .data
= &init_net
.ct
.sysctl_log_invalid
,
584 .maxlen
= sizeof(unsigned int),
586 .proc_handler
= proc_dointvec_minmax
,
587 .extra1
= &log_invalid_proto_min
,
588 .extra2
= &log_invalid_proto_max
,
590 [NF_SYSCTL_CT_EXPECT_MAX
] = {
591 .procname
= "nf_conntrack_expect_max",
592 .data
= &nf_ct_expect_max
,
593 .maxlen
= sizeof(int),
595 .proc_handler
= proc_dointvec
,
597 [NF_SYSCTL_CT_ACCT
] = {
598 .procname
= "nf_conntrack_acct",
599 .data
= &init_net
.ct
.sysctl_acct
,
600 .maxlen
= sizeof(unsigned int),
602 .proc_handler
= proc_dointvec
,
604 [NF_SYSCTL_CT_HELPER
] = {
605 .procname
= "nf_conntrack_helper",
606 .data
= &init_net
.ct
.sysctl_auto_assign_helper
,
607 .maxlen
= sizeof(unsigned int),
609 .proc_handler
= proc_dointvec
,
611 #ifdef CONFIG_NF_CONNTRACK_EVENTS
612 [NF_SYSCTL_CT_EVENTS
] = {
613 .procname
= "nf_conntrack_events",
614 .data
= &init_net
.ct
.sysctl_events
,
615 .maxlen
= sizeof(unsigned int),
617 .proc_handler
= proc_dointvec
,
620 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
621 [NF_SYSCTL_CT_TIMESTAMP
] = {
622 .procname
= "nf_conntrack_timestamp",
623 .data
= &init_net
.ct
.sysctl_tstamp
,
624 .maxlen
= sizeof(unsigned int),
626 .proc_handler
= proc_dointvec
,
632 static struct ctl_table nf_ct_netfilter_table
[] = {
634 .procname
= "nf_conntrack_max",
635 .data
= &nf_conntrack_max
,
636 .maxlen
= sizeof(int),
638 .proc_handler
= proc_dointvec
,
643 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
645 struct ctl_table
*table
;
647 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
652 table
[NF_SYSCTL_CT_COUNT
].data
= &net
->ct
.count
;
653 table
[NF_SYSCTL_CT_CHECKSUM
].data
= &net
->ct
.sysctl_checksum
;
654 table
[NF_SYSCTL_CT_LOG_INVALID
].data
= &net
->ct
.sysctl_log_invalid
;
655 #ifdef CONFIG_NF_CONNTRACK_EVENTS
656 table
[NF_SYSCTL_CT_EVENTS
].data
= &net
->ct
.sysctl_events
;
659 /* Don't export sysctls to unprivileged users */
660 if (net
->user_ns
!= &init_user_ns
) {
661 table
[NF_SYSCTL_CT_MAX
].procname
= NULL
;
662 table
[NF_SYSCTL_CT_ACCT
].procname
= NULL
;
663 table
[NF_SYSCTL_CT_HELPER
].procname
= NULL
;
664 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
665 table
[NF_SYSCTL_CT_TIMESTAMP
].procname
= NULL
;
667 #ifdef CONFIG_NF_CONNTRACK_EVENTS
668 table
[NF_SYSCTL_CT_EVENTS
].procname
= NULL
;
672 if (!net_eq(&init_net
, net
))
673 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
675 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
676 if (!net
->ct
.sysctl_header
)
677 goto out_unregister_netfilter
;
681 out_unregister_netfilter
:
687 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
689 struct ctl_table
*table
;
691 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
692 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
696 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
701 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
704 #endif /* CONFIG_SYSCTL */
706 static int nf_conntrack_pernet_init(struct net
*net
)
710 ret
= nf_conntrack_init_net(net
);
714 ret
= nf_conntrack_standalone_init_proc(net
);
718 net
->ct
.sysctl_checksum
= 1;
719 net
->ct
.sysctl_log_invalid
= 0;
720 ret
= nf_conntrack_standalone_init_sysctl(net
);
727 nf_conntrack_standalone_fini_proc(net
);
729 nf_conntrack_cleanup_net(net
);
734 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
738 list_for_each_entry(net
, net_exit_list
, exit_list
) {
739 nf_conntrack_standalone_fini_sysctl(net
);
740 nf_conntrack_standalone_fini_proc(net
);
742 nf_conntrack_cleanup_net_list(net_exit_list
);
745 static struct pernet_operations nf_conntrack_net_ops
= {
746 .init
= nf_conntrack_pernet_init
,
747 .exit_batch
= nf_conntrack_pernet_exit
,
748 .id
= &nf_conntrack_net_id
,
749 .size
= sizeof(struct nf_conntrack_net
),
752 static int __init
nf_conntrack_standalone_init(void)
754 int ret
= nf_conntrack_init_start();
758 BUILD_BUG_ON(SKB_NFCT_PTRMASK
!= NFCT_PTRMASK
);
759 BUILD_BUG_ON(NFCT_INFOMASK
<= IP_CT_NUMBER
);
762 nf_ct_netfilter_header
=
763 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
764 if (!nf_ct_netfilter_header
) {
765 pr_err("nf_conntrack: can't register to sysctl.\n");
770 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
773 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
777 nf_conntrack_init_end();
782 unregister_net_sysctl_table(nf_ct_netfilter_header
);
785 nf_conntrack_cleanup_end();
790 static void __exit
nf_conntrack_standalone_fini(void)
792 nf_conntrack_cleanup_start();
793 unregister_pernet_subsys(&nf_conntrack_net_ops
);
795 unregister_net_sysctl_table(nf_ct_netfilter_header
);
797 nf_conntrack_cleanup_end();
800 module_init(nf_conntrack_standalone_init
);
801 module_exit(nf_conntrack_standalone_fini
);