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 #ifdef CONFIG_LWTUNNEL
26 #include <net/netfilter/nf_hooks_lwtunnel.h>
28 #include <linux/rculist_nulls.h>
30 static bool enable_hooks __read_mostly;
31 MODULE_PARM_DESC(enable_hooks, "Always enable conntrack hooks");
32 module_param(enable_hooks, bool, 0000);
34 unsigned int nf_conntrack_net_id __read_mostly;
36 #ifdef CONFIG_NF_CONNTRACK_PROCFS
38 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
39 const struct nf_conntrack_l4proto *l4proto)
41 switch (tuple->src.l3num) {
43 seq_printf(s, "src=%pI4 dst=%pI4 ",
44 &tuple->src.u3.ip, &tuple->dst.u3.ip);
47 seq_printf(s, "src=%pI6 dst=%pI6 ",
48 tuple->src.u3.ip6, tuple->dst.u3.ip6);
54 switch (l4proto->l4proto) {
56 seq_printf(s, "type=%u code=%u id=%u ",
57 tuple->dst.u.icmp.type,
58 tuple->dst.u.icmp.code,
59 ntohs(tuple->src.u.icmp.id));
62 seq_printf(s, "sport=%hu dport=%hu ",
63 ntohs(tuple->src.u.tcp.port),
64 ntohs(tuple->dst.u.tcp.port));
68 seq_printf(s, "sport=%hu dport=%hu ",
69 ntohs(tuple->src.u.udp.port),
70 ntohs(tuple->dst.u.udp.port));
74 seq_printf(s, "sport=%hu dport=%hu ",
75 ntohs(tuple->src.u.dccp.port),
76 ntohs(tuple->dst.u.dccp.port));
79 seq_printf(s, "sport=%hu dport=%hu ",
80 ntohs(tuple->src.u.sctp.port),
81 ntohs(tuple->dst.u.sctp.port));
84 seq_printf(s, "type=%u code=%u id=%u ",
85 tuple->dst.u.icmp.type,
86 tuple->dst.u.icmp.code,
87 ntohs(tuple->src.u.icmp.id));
90 seq_printf(s, "srckey=0x%x dstkey=0x%x ",
91 ntohs(tuple->src.u.gre.key),
92 ntohs(tuple->dst.u.gre.key));
98 EXPORT_SYMBOL_GPL(print_tuple);
100 struct ct_iter_state {
101 struct seq_net_private p;
102 struct hlist_nulls_head *hash;
103 unsigned int htable_size;
108 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
110 struct ct_iter_state *st = seq->private;
111 struct hlist_nulls_node *n;
114 st->bucket < st->htable_size;
117 hlist_nulls_first_rcu(&st->hash[st->bucket]));
124 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
125 struct hlist_nulls_node *head)
127 struct ct_iter_state *st = seq->private;
129 head = rcu_dereference(hlist_nulls_next_rcu(head));
130 while (is_a_nulls(head)) {
131 if (likely(get_nulls_value(head) == st->bucket)) {
132 if (++st->bucket >= st->htable_size)
135 head = rcu_dereference(
136 hlist_nulls_first_rcu(&st->hash[st->bucket]));
141 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
143 struct hlist_nulls_node *head = ct_get_first(seq);
146 while (pos && (head = ct_get_next(seq, head)))
148 return pos ? NULL : head;
151 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
154 struct ct_iter_state *st = seq->private;
156 st->time_now = ktime_get_real_ns();
159 nf_conntrack_get_ht(&st->hash, &st->htable_size);
160 return ct_get_idx(seq, *pos);
163 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
166 return ct_get_next(s, v);
169 static void ct_seq_stop(struct seq_file *s, void *v)
175 #ifdef CONFIG_NF_CONNTRACK_SECMARK
176 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
182 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
186 seq_printf(s, "secctx=%s ", secctx);
188 security_release_secctx(secctx, len);
191 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
196 #ifdef CONFIG_NF_CONNTRACK_ZONES
197 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
200 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
202 if (zone->dir != dir)
205 case NF_CT_DEFAULT_ZONE_DIR:
206 seq_printf(s, "zone=%u ", zone->id);
208 case NF_CT_ZONE_DIR_ORIG:
209 seq_printf(s, "zone-orig=%u ", zone->id);
211 case NF_CT_ZONE_DIR_REPL:
212 seq_printf(s, "zone-reply=%u ", zone->id);
219 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
225 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
226 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
228 struct ct_iter_state *st = s->private;
229 struct nf_conn_tstamp *tstamp;
232 tstamp = nf_conn_tstamp_find(ct);
234 delta_time = st->time_now - tstamp->start;
236 delta_time = div_s64(delta_time, NSEC_PER_SEC);
240 seq_printf(s, "delta-time=%llu ",
241 (unsigned long long)delta_time);
247 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
252 static const char* l3proto_name(u16 proto)
255 case AF_INET: return "ipv4";
256 case AF_INET6: return "ipv6";
262 static const char* l4proto_name(u16 proto)
265 case IPPROTO_ICMP: return "icmp";
266 case IPPROTO_TCP: return "tcp";
267 case IPPROTO_UDP: return "udp";
268 case IPPROTO_DCCP: return "dccp";
269 case IPPROTO_GRE: return "gre";
270 case IPPROTO_SCTP: return "sctp";
271 case IPPROTO_UDPLITE: return "udplite";
272 case IPPROTO_ICMPV6: return "icmpv6";
279 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
281 struct nf_conn_acct *acct;
282 struct nf_conn_counter *counter;
284 acct = nf_conn_acct_find(ct);
288 counter = acct->counter;
289 seq_printf(s, "packets=%llu bytes=%llu ",
290 (unsigned long long)atomic64_read(&counter[dir].packets),
291 (unsigned long long)atomic64_read(&counter[dir].bytes));
296 /* return 0 on success, 1 in case of error */
297 static int ct_seq_show(struct seq_file *s, void *v)
299 struct nf_conntrack_tuple_hash *hash = v;
300 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
301 const struct nf_conntrack_l4proto *l4proto;
302 struct net *net = seq_file_net(s);
306 if (unlikely(!refcount_inc_not_zero(&ct->ct_general.use)))
309 /* load ->status after refcount increase */
310 smp_acquire__after_ctrl_dep();
312 if (nf_ct_should_gc(ct)) {
317 /* we only want to print DIR_ORIGINAL */
318 if (NF_CT_DIRECTION(hash))
321 if (!net_eq(nf_ct_net(ct), net))
324 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
327 seq_printf(s, "%-8s %u %-8s %u ",
328 l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct),
329 l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
331 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
332 seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);
334 if (l4proto->print_conntrack)
335 l4proto->print_conntrack(s, ct);
337 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
340 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
342 if (seq_has_overflowed(s))
345 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
348 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
349 seq_puts(s, "[UNREPLIED] ");
351 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
353 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
355 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
358 if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
359 seq_puts(s, "[HW_OFFLOAD] ");
360 else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
361 seq_puts(s, "[OFFLOAD] ");
362 else if (test_bit(IPS_ASSURED_BIT, &ct->status))
363 seq_puts(s, "[ASSURED] ");
365 if (seq_has_overflowed(s))
368 #if defined(CONFIG_NF_CONNTRACK_MARK)
369 seq_printf(s, "mark=%u ", READ_ONCE(ct->mark));
372 ct_show_secctx(s, ct);
373 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
374 ct_show_delta_time(s, ct);
376 seq_printf(s, "use=%u\n", refcount_read(&ct->ct_general.use));
378 if (seq_has_overflowed(s))
387 static const struct seq_operations ct_seq_ops = {
388 .start = ct_seq_start,
394 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
396 struct net *net = seq_file_net(seq);
400 return SEQ_START_TOKEN;
402 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
403 if (!cpu_possible(cpu))
406 return per_cpu_ptr(net->ct.stat, cpu);
412 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
414 struct net *net = seq_file_net(seq);
417 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
418 if (!cpu_possible(cpu))
421 return per_cpu_ptr(net->ct.stat, cpu);
427 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
431 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
433 struct net *net = seq_file_net(seq);
434 const struct ip_conntrack_stat *st = v;
435 unsigned int nr_conntracks;
437 if (v == SEQ_START_TOKEN) {
438 seq_puts(seq, "entries clashres found new invalid ignore delete chainlength insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n");
442 nr_conntracks = nf_conntrack_count(net);
444 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
445 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
468 static const struct seq_operations ct_cpu_seq_ops = {
469 .start = ct_cpu_seq_start,
470 .next = ct_cpu_seq_next,
471 .stop = ct_cpu_seq_stop,
472 .show = ct_cpu_seq_show,
475 static int nf_conntrack_standalone_init_proc(struct net *net)
477 struct proc_dir_entry *pde;
481 pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
482 sizeof(struct ct_iter_state));
484 goto out_nf_conntrack;
486 root_uid = make_kuid(net->user_ns, 0);
487 root_gid = make_kgid(net->user_ns, 0);
488 if (uid_valid(root_uid) && gid_valid(root_gid))
489 proc_set_user(pde, root_uid, root_gid);
491 pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
492 &ct_cpu_seq_ops, sizeof(struct seq_net_private));
494 goto out_stat_nf_conntrack;
497 out_stat_nf_conntrack:
498 remove_proc_entry("nf_conntrack", net->proc_net);
503 static void nf_conntrack_standalone_fini_proc(struct net *net)
505 remove_proc_entry("nf_conntrack", net->proc_net_stat);
506 remove_proc_entry("nf_conntrack", net->proc_net);
509 static int nf_conntrack_standalone_init_proc(struct net *net)
514 static void nf_conntrack_standalone_fini_proc(struct net *net)
517 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
519 u32 nf_conntrack_count(const struct net *net)
521 const struct nf_conntrack_net *cnet = nf_ct_pernet(net);
523 return atomic_read(&cnet->count);
525 EXPORT_SYMBOL_GPL(nf_conntrack_count);
530 /* size the user *wants to set */
531 static unsigned int nf_conntrack_htable_size_user __read_mostly;
534 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
535 void *buffer, size_t *lenp, loff_t *ppos)
539 /* module_param hashsize could have changed value */
540 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
542 ret = proc_dointvec(table, write, buffer, lenp, ppos);
543 if (ret < 0 || !write)
546 /* update ret, we might not be able to satisfy request */
547 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
549 /* update it to the actual value used by conntrack */
550 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
554 static struct ctl_table_header *nf_ct_netfilter_header;
556 enum nf_ct_sysctl_index {
559 NF_SYSCTL_CT_BUCKETS,
560 NF_SYSCTL_CT_CHECKSUM,
561 NF_SYSCTL_CT_LOG_INVALID,
562 NF_SYSCTL_CT_EXPECT_MAX,
564 #ifdef CONFIG_NF_CONNTRACK_EVENTS
567 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
568 NF_SYSCTL_CT_TIMESTAMP,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
571 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
577 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
581 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
582 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
584 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
585 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
586 NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST,
587 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
588 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
590 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
591 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
595 #ifdef CONFIG_NF_CT_PROTO_SCTP
596 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
597 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
598 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
600 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
601 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
602 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
603 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
605 #ifdef CONFIG_NF_CT_PROTO_DCCP
606 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
607 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
608 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
609 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
610 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
611 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
612 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
613 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
615 #ifdef CONFIG_NF_CT_PROTO_GRE
616 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
617 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
619 #ifdef CONFIG_LWTUNNEL
620 NF_SYSCTL_CT_LWTUNNEL,
623 __NF_SYSCTL_CT_LAST_SYSCTL,
626 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
628 static struct ctl_table nf_ct_sysctl_table[] = {
629 [NF_SYSCTL_CT_MAX] = {
630 .procname = "nf_conntrack_max",
631 .data = &nf_conntrack_max,
632 .maxlen = sizeof(int),
634 .proc_handler = proc_dointvec,
636 [NF_SYSCTL_CT_COUNT] = {
637 .procname = "nf_conntrack_count",
638 .maxlen = sizeof(int),
640 .proc_handler = proc_dointvec,
642 [NF_SYSCTL_CT_BUCKETS] = {
643 .procname = "nf_conntrack_buckets",
644 .data = &nf_conntrack_htable_size_user,
645 .maxlen = sizeof(unsigned int),
647 .proc_handler = nf_conntrack_hash_sysctl,
649 [NF_SYSCTL_CT_CHECKSUM] = {
650 .procname = "nf_conntrack_checksum",
651 .data = &init_net.ct.sysctl_checksum,
652 .maxlen = sizeof(u8),
654 .proc_handler = proc_dou8vec_minmax,
655 .extra1 = SYSCTL_ZERO,
656 .extra2 = SYSCTL_ONE,
658 [NF_SYSCTL_CT_LOG_INVALID] = {
659 .procname = "nf_conntrack_log_invalid",
660 .data = &init_net.ct.sysctl_log_invalid,
661 .maxlen = sizeof(u8),
663 .proc_handler = proc_dou8vec_minmax,
665 [NF_SYSCTL_CT_EXPECT_MAX] = {
666 .procname = "nf_conntrack_expect_max",
667 .data = &nf_ct_expect_max,
668 .maxlen = sizeof(int),
670 .proc_handler = proc_dointvec,
672 [NF_SYSCTL_CT_ACCT] = {
673 .procname = "nf_conntrack_acct",
674 .data = &init_net.ct.sysctl_acct,
675 .maxlen = sizeof(u8),
677 .proc_handler = proc_dou8vec_minmax,
678 .extra1 = SYSCTL_ZERO,
679 .extra2 = SYSCTL_ONE,
681 #ifdef CONFIG_NF_CONNTRACK_EVENTS
682 [NF_SYSCTL_CT_EVENTS] = {
683 .procname = "nf_conntrack_events",
684 .data = &init_net.ct.sysctl_events,
685 .maxlen = sizeof(u8),
687 .proc_handler = proc_dou8vec_minmax,
688 .extra1 = SYSCTL_ZERO,
689 .extra2 = SYSCTL_TWO,
692 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
693 [NF_SYSCTL_CT_TIMESTAMP] = {
694 .procname = "nf_conntrack_timestamp",
695 .data = &init_net.ct.sysctl_tstamp,
696 .maxlen = sizeof(u8),
698 .proc_handler = proc_dou8vec_minmax,
699 .extra1 = SYSCTL_ZERO,
700 .extra2 = SYSCTL_ONE,
703 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
704 .procname = "nf_conntrack_generic_timeout",
705 .maxlen = sizeof(unsigned int),
707 .proc_handler = proc_dointvec_jiffies,
709 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
710 .procname = "nf_conntrack_tcp_timeout_syn_sent",
711 .maxlen = sizeof(unsigned int),
713 .proc_handler = proc_dointvec_jiffies,
715 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
716 .procname = "nf_conntrack_tcp_timeout_syn_recv",
717 .maxlen = sizeof(unsigned int),
719 .proc_handler = proc_dointvec_jiffies,
721 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
722 .procname = "nf_conntrack_tcp_timeout_established",
723 .maxlen = sizeof(unsigned int),
725 .proc_handler = proc_dointvec_jiffies,
727 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
728 .procname = "nf_conntrack_tcp_timeout_fin_wait",
729 .maxlen = sizeof(unsigned int),
731 .proc_handler = proc_dointvec_jiffies,
733 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
734 .procname = "nf_conntrack_tcp_timeout_close_wait",
735 .maxlen = sizeof(unsigned int),
737 .proc_handler = proc_dointvec_jiffies,
739 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
740 .procname = "nf_conntrack_tcp_timeout_last_ack",
741 .maxlen = sizeof(unsigned int),
743 .proc_handler = proc_dointvec_jiffies,
745 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
746 .procname = "nf_conntrack_tcp_timeout_time_wait",
747 .maxlen = sizeof(unsigned int),
749 .proc_handler = proc_dointvec_jiffies,
751 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
752 .procname = "nf_conntrack_tcp_timeout_close",
753 .maxlen = sizeof(unsigned int),
755 .proc_handler = proc_dointvec_jiffies,
757 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
758 .procname = "nf_conntrack_tcp_timeout_max_retrans",
759 .maxlen = sizeof(unsigned int),
761 .proc_handler = proc_dointvec_jiffies,
763 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
764 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
765 .maxlen = sizeof(unsigned int),
767 .proc_handler = proc_dointvec_jiffies,
769 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
770 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
771 .procname = "nf_flowtable_tcp_timeout",
772 .maxlen = sizeof(unsigned int),
774 .proc_handler = proc_dointvec_jiffies,
777 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
778 .procname = "nf_conntrack_tcp_loose",
779 .maxlen = sizeof(u8),
781 .proc_handler = proc_dou8vec_minmax,
782 .extra1 = SYSCTL_ZERO,
783 .extra2 = SYSCTL_ONE,
785 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
786 .procname = "nf_conntrack_tcp_be_liberal",
787 .maxlen = sizeof(u8),
789 .proc_handler = proc_dou8vec_minmax,
790 .extra1 = SYSCTL_ZERO,
791 .extra2 = SYSCTL_ONE,
793 [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
794 .procname = "nf_conntrack_tcp_ignore_invalid_rst",
795 .maxlen = sizeof(u8),
797 .proc_handler = proc_dou8vec_minmax,
798 .extra1 = SYSCTL_ZERO,
799 .extra2 = SYSCTL_ONE,
801 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
802 .procname = "nf_conntrack_tcp_max_retrans",
803 .maxlen = sizeof(u8),
805 .proc_handler = proc_dou8vec_minmax,
807 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
808 .procname = "nf_conntrack_udp_timeout",
809 .maxlen = sizeof(unsigned int),
811 .proc_handler = proc_dointvec_jiffies,
813 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
814 .procname = "nf_conntrack_udp_timeout_stream",
815 .maxlen = sizeof(unsigned int),
817 .proc_handler = proc_dointvec_jiffies,
819 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
820 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
821 .procname = "nf_flowtable_udp_timeout",
822 .maxlen = sizeof(unsigned int),
824 .proc_handler = proc_dointvec_jiffies,
827 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
828 .procname = "nf_conntrack_icmp_timeout",
829 .maxlen = sizeof(unsigned int),
831 .proc_handler = proc_dointvec_jiffies,
833 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
834 .procname = "nf_conntrack_icmpv6_timeout",
835 .maxlen = sizeof(unsigned int),
837 .proc_handler = proc_dointvec_jiffies,
839 #ifdef CONFIG_NF_CT_PROTO_SCTP
840 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
841 .procname = "nf_conntrack_sctp_timeout_closed",
842 .maxlen = sizeof(unsigned int),
844 .proc_handler = proc_dointvec_jiffies,
846 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
847 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
848 .maxlen = sizeof(unsigned int),
850 .proc_handler = proc_dointvec_jiffies,
852 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
853 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
854 .maxlen = sizeof(unsigned int),
856 .proc_handler = proc_dointvec_jiffies,
858 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
859 .procname = "nf_conntrack_sctp_timeout_established",
860 .maxlen = sizeof(unsigned int),
862 .proc_handler = proc_dointvec_jiffies,
864 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
865 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
866 .maxlen = sizeof(unsigned int),
868 .proc_handler = proc_dointvec_jiffies,
870 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
871 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
872 .maxlen = sizeof(unsigned int),
874 .proc_handler = proc_dointvec_jiffies,
876 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
877 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
878 .maxlen = sizeof(unsigned int),
880 .proc_handler = proc_dointvec_jiffies,
882 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
883 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
884 .maxlen = sizeof(unsigned int),
886 .proc_handler = proc_dointvec_jiffies,
889 #ifdef CONFIG_NF_CT_PROTO_DCCP
890 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
891 .procname = "nf_conntrack_dccp_timeout_request",
892 .maxlen = sizeof(unsigned int),
894 .proc_handler = proc_dointvec_jiffies,
896 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
897 .procname = "nf_conntrack_dccp_timeout_respond",
898 .maxlen = sizeof(unsigned int),
900 .proc_handler = proc_dointvec_jiffies,
902 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
903 .procname = "nf_conntrack_dccp_timeout_partopen",
904 .maxlen = sizeof(unsigned int),
906 .proc_handler = proc_dointvec_jiffies,
908 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
909 .procname = "nf_conntrack_dccp_timeout_open",
910 .maxlen = sizeof(unsigned int),
912 .proc_handler = proc_dointvec_jiffies,
914 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
915 .procname = "nf_conntrack_dccp_timeout_closereq",
916 .maxlen = sizeof(unsigned int),
918 .proc_handler = proc_dointvec_jiffies,
920 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
921 .procname = "nf_conntrack_dccp_timeout_closing",
922 .maxlen = sizeof(unsigned int),
924 .proc_handler = proc_dointvec_jiffies,
926 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
927 .procname = "nf_conntrack_dccp_timeout_timewait",
928 .maxlen = sizeof(unsigned int),
930 .proc_handler = proc_dointvec_jiffies,
932 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
933 .procname = "nf_conntrack_dccp_loose",
934 .maxlen = sizeof(u8),
936 .proc_handler = proc_dou8vec_minmax,
937 .extra1 = SYSCTL_ZERO,
938 .extra2 = SYSCTL_ONE,
941 #ifdef CONFIG_NF_CT_PROTO_GRE
942 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
943 .procname = "nf_conntrack_gre_timeout",
944 .maxlen = sizeof(unsigned int),
946 .proc_handler = proc_dointvec_jiffies,
948 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
949 .procname = "nf_conntrack_gre_timeout_stream",
950 .maxlen = sizeof(unsigned int),
952 .proc_handler = proc_dointvec_jiffies,
955 #ifdef CONFIG_LWTUNNEL
956 [NF_SYSCTL_CT_LWTUNNEL] = {
957 .procname = "nf_hooks_lwtunnel",
959 .maxlen = sizeof(int),
961 .proc_handler = nf_hooks_lwtunnel_sysctl_handler,
967 static struct ctl_table nf_ct_netfilter_table[] = {
969 .procname = "nf_conntrack_max",
970 .data = &nf_conntrack_max,
971 .maxlen = sizeof(int),
973 .proc_handler = proc_dointvec,
978 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
979 struct ctl_table *table)
981 struct nf_tcp_net *tn = nf_tcp_pernet(net);
983 #define XASSIGN(XNAME, tn) \
984 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
985 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
987 XASSIGN(SYN_SENT, tn);
988 XASSIGN(SYN_RECV, tn);
989 XASSIGN(ESTABLISHED, tn);
990 XASSIGN(FIN_WAIT, tn);
991 XASSIGN(CLOSE_WAIT, tn);
992 XASSIGN(LAST_ACK, tn);
993 XASSIGN(TIME_WAIT, tn);
995 XASSIGN(RETRANS, tn);
998 #define XASSIGN(XNAME, rval) \
999 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
1001 XASSIGN(LOOSE, &tn->tcp_loose);
1002 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
1003 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1004 XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
1007 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1008 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1013 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1014 struct ctl_table *table)
1016 #ifdef CONFIG_NF_CT_PROTO_SCTP
1017 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1019 #define XASSIGN(XNAME, sn) \
1020 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1021 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1023 XASSIGN(CLOSED, sn);
1024 XASSIGN(COOKIE_WAIT, sn);
1025 XASSIGN(COOKIE_ECHOED, sn);
1026 XASSIGN(ESTABLISHED, sn);
1027 XASSIGN(SHUTDOWN_SENT, sn);
1028 XASSIGN(SHUTDOWN_RECD, sn);
1029 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1030 XASSIGN(HEARTBEAT_SENT, sn);
1035 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1036 struct ctl_table *table)
1038 #ifdef CONFIG_NF_CT_PROTO_DCCP
1039 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1041 #define XASSIGN(XNAME, dn) \
1042 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1043 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1045 XASSIGN(REQUEST, dn);
1046 XASSIGN(RESPOND, dn);
1047 XASSIGN(PARTOPEN, dn);
1049 XASSIGN(CLOSEREQ, dn);
1050 XASSIGN(CLOSING, dn);
1051 XASSIGN(TIMEWAIT, dn);
1054 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1058 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1059 struct ctl_table *table)
1061 #ifdef CONFIG_NF_CT_PROTO_GRE
1062 struct nf_gre_net *gn = nf_gre_pernet(net);
1064 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1065 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1069 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1071 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1072 struct nf_udp_net *un = nf_udp_pernet(net);
1073 struct ctl_table *table;
1075 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1077 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1082 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1083 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1084 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1085 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1086 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1087 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1089 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1090 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1092 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1093 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1094 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1095 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1096 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1097 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1098 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1101 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1102 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1103 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1104 nf_conntrack_standalone_init_gre_sysctl(net, table);
1106 /* Don't allow non-init_net ns to alter global sysctls */
1107 if (!net_eq(&init_net, net)) {
1108 table[NF_SYSCTL_CT_MAX].mode = 0444;
1109 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1110 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1113 cnet->sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1114 if (!cnet->sysctl_header)
1115 goto out_unregister_netfilter;
1119 out_unregister_netfilter:
1124 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1126 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1127 struct ctl_table *table;
1129 table = cnet->sysctl_header->ctl_table_arg;
1130 unregister_net_sysctl_table(cnet->sysctl_header);
1134 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1139 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1142 #endif /* CONFIG_SYSCTL */
1144 static void nf_conntrack_fini_net(struct net *net)
1147 nf_ct_netns_put(net, NFPROTO_INET);
1149 nf_conntrack_standalone_fini_proc(net);
1150 nf_conntrack_standalone_fini_sysctl(net);
1153 static int nf_conntrack_pernet_init(struct net *net)
1157 net->ct.sysctl_checksum = 1;
1159 ret = nf_conntrack_standalone_init_sysctl(net);
1163 ret = nf_conntrack_standalone_init_proc(net);
1167 ret = nf_conntrack_init_net(net);
1172 ret = nf_ct_netns_get(net, NFPROTO_INET);
1180 nf_conntrack_cleanup_net(net);
1182 nf_conntrack_standalone_fini_proc(net);
1184 nf_conntrack_standalone_fini_sysctl(net);
1188 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1192 list_for_each_entry(net, net_exit_list, exit_list)
1193 nf_conntrack_fini_net(net);
1195 nf_conntrack_cleanup_net_list(net_exit_list);
1198 static struct pernet_operations nf_conntrack_net_ops = {
1199 .init = nf_conntrack_pernet_init,
1200 .exit_batch = nf_conntrack_pernet_exit,
1201 .id = &nf_conntrack_net_id,
1202 .size = sizeof(struct nf_conntrack_net),
1205 static int __init nf_conntrack_standalone_init(void)
1207 int ret = nf_conntrack_init_start();
1211 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1213 #ifdef CONFIG_SYSCTL
1214 nf_ct_netfilter_header =
1215 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1216 if (!nf_ct_netfilter_header) {
1217 pr_err("nf_conntrack: can't register to sysctl.\n");
1222 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1225 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1229 nf_conntrack_init_end();
1233 #ifdef CONFIG_SYSCTL
1234 unregister_net_sysctl_table(nf_ct_netfilter_header);
1237 nf_conntrack_cleanup_end();
1242 static void __exit nf_conntrack_standalone_fini(void)
1244 nf_conntrack_cleanup_start();
1245 unregister_pernet_subsys(&nf_conntrack_net_ops);
1246 #ifdef CONFIG_SYSCTL
1247 unregister_net_sysctl_table(nf_ct_netfilter_header);
1249 nf_conntrack_cleanup_end();
1252 module_init(nf_conntrack_standalone_init);
1253 module_exit(nf_conntrack_standalone_fini);