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));
294 /* return 0 on success, 1 in case of error */
295 static int ct_seq_show(struct seq_file *s, void *v)
297 struct nf_conntrack_tuple_hash *hash = v;
298 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
299 const struct nf_conntrack_l4proto *l4proto;
300 struct net *net = seq_file_net(s);
304 if (unlikely(!refcount_inc_not_zero(&ct->ct_general.use)))
307 /* load ->status after refcount increase */
308 smp_acquire__after_ctrl_dep();
310 if (nf_ct_should_gc(ct)) {
315 /* we only want to print DIR_ORIGINAL */
316 if (NF_CT_DIRECTION(hash))
319 if (!net_eq(nf_ct_net(ct), net))
322 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
325 seq_printf(s, "%-8s %u %-8s %u ",
326 l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct),
327 l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
329 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
330 seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);
332 if (l4proto->print_conntrack)
333 l4proto->print_conntrack(s, ct);
335 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
338 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
340 if (seq_has_overflowed(s))
343 seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL);
345 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
346 seq_puts(s, "[UNREPLIED] ");
348 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
350 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
352 seq_print_acct(s, ct, IP_CT_DIR_REPLY);
354 if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
355 seq_puts(s, "[HW_OFFLOAD] ");
356 else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
357 seq_puts(s, "[OFFLOAD] ");
358 else if (test_bit(IPS_ASSURED_BIT, &ct->status))
359 seq_puts(s, "[ASSURED] ");
361 if (seq_has_overflowed(s))
364 #if defined(CONFIG_NF_CONNTRACK_MARK)
365 seq_printf(s, "mark=%u ", READ_ONCE(ct->mark));
368 ct_show_secctx(s, ct);
369 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
370 ct_show_delta_time(s, ct);
372 seq_printf(s, "use=%u\n", refcount_read(&ct->ct_general.use));
374 if (seq_has_overflowed(s))
383 static const struct seq_operations ct_seq_ops = {
384 .start = ct_seq_start,
390 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
392 struct net *net = seq_file_net(seq);
396 return SEQ_START_TOKEN;
398 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
399 if (!cpu_possible(cpu))
402 return per_cpu_ptr(net->ct.stat, cpu);
408 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
410 struct net *net = seq_file_net(seq);
413 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
414 if (!cpu_possible(cpu))
417 return per_cpu_ptr(net->ct.stat, cpu);
423 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
427 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
429 struct net *net = seq_file_net(seq);
430 const struct ip_conntrack_stat *st = v;
431 unsigned int nr_conntracks;
433 if (v == SEQ_START_TOKEN) {
434 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");
438 nr_conntracks = nf_conntrack_count(net);
440 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
441 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
464 static const struct seq_operations ct_cpu_seq_ops = {
465 .start = ct_cpu_seq_start,
466 .next = ct_cpu_seq_next,
467 .stop = ct_cpu_seq_stop,
468 .show = ct_cpu_seq_show,
471 static int nf_conntrack_standalone_init_proc(struct net *net)
473 struct proc_dir_entry *pde;
477 pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
478 sizeof(struct ct_iter_state));
480 goto out_nf_conntrack;
482 root_uid = make_kuid(net->user_ns, 0);
483 root_gid = make_kgid(net->user_ns, 0);
484 if (uid_valid(root_uid) && gid_valid(root_gid))
485 proc_set_user(pde, root_uid, root_gid);
487 pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
488 &ct_cpu_seq_ops, sizeof(struct seq_net_private));
490 goto out_stat_nf_conntrack;
493 out_stat_nf_conntrack:
494 remove_proc_entry("nf_conntrack", net->proc_net);
499 static void nf_conntrack_standalone_fini_proc(struct net *net)
501 remove_proc_entry("nf_conntrack", net->proc_net_stat);
502 remove_proc_entry("nf_conntrack", net->proc_net);
505 static int nf_conntrack_standalone_init_proc(struct net *net)
510 static void nf_conntrack_standalone_fini_proc(struct net *net)
513 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
515 u32 nf_conntrack_count(const struct net *net)
517 const struct nf_conntrack_net *cnet = nf_ct_pernet(net);
519 return atomic_read(&cnet->count);
521 EXPORT_SYMBOL_GPL(nf_conntrack_count);
526 /* size the user *wants to set */
527 static unsigned int nf_conntrack_htable_size_user __read_mostly;
530 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
531 void *buffer, size_t *lenp, loff_t *ppos)
535 /* module_param hashsize could have changed value */
536 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
538 ret = proc_dointvec(table, write, buffer, lenp, ppos);
539 if (ret < 0 || !write)
542 /* update ret, we might not be able to satisfy request */
543 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
545 /* update it to the actual value used by conntrack */
546 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
550 static struct ctl_table_header *nf_ct_netfilter_header;
552 enum nf_ct_sysctl_index {
555 NF_SYSCTL_CT_BUCKETS,
556 NF_SYSCTL_CT_CHECKSUM,
557 NF_SYSCTL_CT_LOG_INVALID,
558 NF_SYSCTL_CT_EXPECT_MAX,
560 #ifdef CONFIG_NF_CONNTRACK_EVENTS
563 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
564 NF_SYSCTL_CT_TIMESTAMP,
566 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
567 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
568 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
569 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
571 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
577 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
578 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
580 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
581 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
582 NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST,
583 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
585 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
586 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
587 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
591 #ifdef CONFIG_NF_CT_PROTO_SCTP
592 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
595 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
596 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
597 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
598 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
601 #ifdef CONFIG_NF_CT_PROTO_DCCP
602 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
603 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
604 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
605 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
606 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
607 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
608 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
609 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
611 #ifdef CONFIG_NF_CT_PROTO_GRE
612 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
613 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
615 #ifdef CONFIG_LWTUNNEL
616 NF_SYSCTL_CT_LWTUNNEL,
619 __NF_SYSCTL_CT_LAST_SYSCTL,
622 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
624 static struct ctl_table nf_ct_sysctl_table[] = {
625 [NF_SYSCTL_CT_MAX] = {
626 .procname = "nf_conntrack_max",
627 .data = &nf_conntrack_max,
628 .maxlen = sizeof(int),
630 .proc_handler = proc_dointvec,
632 [NF_SYSCTL_CT_COUNT] = {
633 .procname = "nf_conntrack_count",
634 .maxlen = sizeof(int),
636 .proc_handler = proc_dointvec,
638 [NF_SYSCTL_CT_BUCKETS] = {
639 .procname = "nf_conntrack_buckets",
640 .data = &nf_conntrack_htable_size_user,
641 .maxlen = sizeof(unsigned int),
643 .proc_handler = nf_conntrack_hash_sysctl,
645 [NF_SYSCTL_CT_CHECKSUM] = {
646 .procname = "nf_conntrack_checksum",
647 .data = &init_net.ct.sysctl_checksum,
648 .maxlen = sizeof(u8),
650 .proc_handler = proc_dou8vec_minmax,
651 .extra1 = SYSCTL_ZERO,
652 .extra2 = SYSCTL_ONE,
654 [NF_SYSCTL_CT_LOG_INVALID] = {
655 .procname = "nf_conntrack_log_invalid",
656 .data = &init_net.ct.sysctl_log_invalid,
657 .maxlen = sizeof(u8),
659 .proc_handler = proc_dou8vec_minmax,
661 [NF_SYSCTL_CT_EXPECT_MAX] = {
662 .procname = "nf_conntrack_expect_max",
663 .data = &nf_ct_expect_max,
664 .maxlen = sizeof(int),
666 .proc_handler = proc_dointvec,
668 [NF_SYSCTL_CT_ACCT] = {
669 .procname = "nf_conntrack_acct",
670 .data = &init_net.ct.sysctl_acct,
671 .maxlen = sizeof(u8),
673 .proc_handler = proc_dou8vec_minmax,
674 .extra1 = SYSCTL_ZERO,
675 .extra2 = SYSCTL_ONE,
677 #ifdef CONFIG_NF_CONNTRACK_EVENTS
678 [NF_SYSCTL_CT_EVENTS] = {
679 .procname = "nf_conntrack_events",
680 .data = &init_net.ct.sysctl_events,
681 .maxlen = sizeof(u8),
683 .proc_handler = proc_dou8vec_minmax,
684 .extra1 = SYSCTL_ZERO,
685 .extra2 = SYSCTL_TWO,
688 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
689 [NF_SYSCTL_CT_TIMESTAMP] = {
690 .procname = "nf_conntrack_timestamp",
691 .data = &init_net.ct.sysctl_tstamp,
692 .maxlen = sizeof(u8),
694 .proc_handler = proc_dou8vec_minmax,
695 .extra1 = SYSCTL_ZERO,
696 .extra2 = SYSCTL_ONE,
699 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
700 .procname = "nf_conntrack_generic_timeout",
701 .maxlen = sizeof(unsigned int),
703 .proc_handler = proc_dointvec_jiffies,
705 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
706 .procname = "nf_conntrack_tcp_timeout_syn_sent",
707 .maxlen = sizeof(unsigned int),
709 .proc_handler = proc_dointvec_jiffies,
711 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
712 .procname = "nf_conntrack_tcp_timeout_syn_recv",
713 .maxlen = sizeof(unsigned int),
715 .proc_handler = proc_dointvec_jiffies,
717 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
718 .procname = "nf_conntrack_tcp_timeout_established",
719 .maxlen = sizeof(unsigned int),
721 .proc_handler = proc_dointvec_jiffies,
723 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
724 .procname = "nf_conntrack_tcp_timeout_fin_wait",
725 .maxlen = sizeof(unsigned int),
727 .proc_handler = proc_dointvec_jiffies,
729 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
730 .procname = "nf_conntrack_tcp_timeout_close_wait",
731 .maxlen = sizeof(unsigned int),
733 .proc_handler = proc_dointvec_jiffies,
735 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
736 .procname = "nf_conntrack_tcp_timeout_last_ack",
737 .maxlen = sizeof(unsigned int),
739 .proc_handler = proc_dointvec_jiffies,
741 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
742 .procname = "nf_conntrack_tcp_timeout_time_wait",
743 .maxlen = sizeof(unsigned int),
745 .proc_handler = proc_dointvec_jiffies,
747 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
748 .procname = "nf_conntrack_tcp_timeout_close",
749 .maxlen = sizeof(unsigned int),
751 .proc_handler = proc_dointvec_jiffies,
753 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
754 .procname = "nf_conntrack_tcp_timeout_max_retrans",
755 .maxlen = sizeof(unsigned int),
757 .proc_handler = proc_dointvec_jiffies,
759 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
760 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
761 .maxlen = sizeof(unsigned int),
763 .proc_handler = proc_dointvec_jiffies,
765 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
766 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
767 .procname = "nf_flowtable_tcp_timeout",
768 .maxlen = sizeof(unsigned int),
770 .proc_handler = proc_dointvec_jiffies,
773 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
774 .procname = "nf_conntrack_tcp_loose",
775 .maxlen = sizeof(u8),
777 .proc_handler = proc_dou8vec_minmax,
778 .extra1 = SYSCTL_ZERO,
779 .extra2 = SYSCTL_ONE,
781 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
782 .procname = "nf_conntrack_tcp_be_liberal",
783 .maxlen = sizeof(u8),
785 .proc_handler = proc_dou8vec_minmax,
786 .extra1 = SYSCTL_ZERO,
787 .extra2 = SYSCTL_ONE,
789 [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
790 .procname = "nf_conntrack_tcp_ignore_invalid_rst",
791 .maxlen = sizeof(u8),
793 .proc_handler = proc_dou8vec_minmax,
794 .extra1 = SYSCTL_ZERO,
795 .extra2 = SYSCTL_ONE,
797 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
798 .procname = "nf_conntrack_tcp_max_retrans",
799 .maxlen = sizeof(u8),
801 .proc_handler = proc_dou8vec_minmax,
803 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
804 .procname = "nf_conntrack_udp_timeout",
805 .maxlen = sizeof(unsigned int),
807 .proc_handler = proc_dointvec_jiffies,
809 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
810 .procname = "nf_conntrack_udp_timeout_stream",
811 .maxlen = sizeof(unsigned int),
813 .proc_handler = proc_dointvec_jiffies,
815 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
816 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
817 .procname = "nf_flowtable_udp_timeout",
818 .maxlen = sizeof(unsigned int),
820 .proc_handler = proc_dointvec_jiffies,
823 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
824 .procname = "nf_conntrack_icmp_timeout",
825 .maxlen = sizeof(unsigned int),
827 .proc_handler = proc_dointvec_jiffies,
829 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
830 .procname = "nf_conntrack_icmpv6_timeout",
831 .maxlen = sizeof(unsigned int),
833 .proc_handler = proc_dointvec_jiffies,
835 #ifdef CONFIG_NF_CT_PROTO_SCTP
836 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
837 .procname = "nf_conntrack_sctp_timeout_closed",
838 .maxlen = sizeof(unsigned int),
840 .proc_handler = proc_dointvec_jiffies,
842 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
843 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
844 .maxlen = sizeof(unsigned int),
846 .proc_handler = proc_dointvec_jiffies,
848 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
849 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
850 .maxlen = sizeof(unsigned int),
852 .proc_handler = proc_dointvec_jiffies,
854 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
855 .procname = "nf_conntrack_sctp_timeout_established",
856 .maxlen = sizeof(unsigned int),
858 .proc_handler = proc_dointvec_jiffies,
860 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
861 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
862 .maxlen = sizeof(unsigned int),
864 .proc_handler = proc_dointvec_jiffies,
866 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
867 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
868 .maxlen = sizeof(unsigned int),
870 .proc_handler = proc_dointvec_jiffies,
872 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
873 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
874 .maxlen = sizeof(unsigned int),
876 .proc_handler = proc_dointvec_jiffies,
878 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
879 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
880 .maxlen = sizeof(unsigned int),
882 .proc_handler = proc_dointvec_jiffies,
885 #ifdef CONFIG_NF_CT_PROTO_DCCP
886 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
887 .procname = "nf_conntrack_dccp_timeout_request",
888 .maxlen = sizeof(unsigned int),
890 .proc_handler = proc_dointvec_jiffies,
892 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
893 .procname = "nf_conntrack_dccp_timeout_respond",
894 .maxlen = sizeof(unsigned int),
896 .proc_handler = proc_dointvec_jiffies,
898 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
899 .procname = "nf_conntrack_dccp_timeout_partopen",
900 .maxlen = sizeof(unsigned int),
902 .proc_handler = proc_dointvec_jiffies,
904 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
905 .procname = "nf_conntrack_dccp_timeout_open",
906 .maxlen = sizeof(unsigned int),
908 .proc_handler = proc_dointvec_jiffies,
910 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
911 .procname = "nf_conntrack_dccp_timeout_closereq",
912 .maxlen = sizeof(unsigned int),
914 .proc_handler = proc_dointvec_jiffies,
916 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
917 .procname = "nf_conntrack_dccp_timeout_closing",
918 .maxlen = sizeof(unsigned int),
920 .proc_handler = proc_dointvec_jiffies,
922 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
923 .procname = "nf_conntrack_dccp_timeout_timewait",
924 .maxlen = sizeof(unsigned int),
926 .proc_handler = proc_dointvec_jiffies,
928 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
929 .procname = "nf_conntrack_dccp_loose",
930 .maxlen = sizeof(u8),
932 .proc_handler = proc_dou8vec_minmax,
933 .extra1 = SYSCTL_ZERO,
934 .extra2 = SYSCTL_ONE,
937 #ifdef CONFIG_NF_CT_PROTO_GRE
938 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
939 .procname = "nf_conntrack_gre_timeout",
940 .maxlen = sizeof(unsigned int),
942 .proc_handler = proc_dointvec_jiffies,
944 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
945 .procname = "nf_conntrack_gre_timeout_stream",
946 .maxlen = sizeof(unsigned int),
948 .proc_handler = proc_dointvec_jiffies,
951 #ifdef CONFIG_LWTUNNEL
952 [NF_SYSCTL_CT_LWTUNNEL] = {
953 .procname = "nf_hooks_lwtunnel",
955 .maxlen = sizeof(int),
957 .proc_handler = nf_hooks_lwtunnel_sysctl_handler,
963 static struct ctl_table nf_ct_netfilter_table[] = {
965 .procname = "nf_conntrack_max",
966 .data = &nf_conntrack_max,
967 .maxlen = sizeof(int),
969 .proc_handler = proc_dointvec,
974 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
975 struct ctl_table *table)
977 struct nf_tcp_net *tn = nf_tcp_pernet(net);
979 #define XASSIGN(XNAME, tn) \
980 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
981 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
983 XASSIGN(SYN_SENT, tn);
984 XASSIGN(SYN_RECV, tn);
985 XASSIGN(ESTABLISHED, tn);
986 XASSIGN(FIN_WAIT, tn);
987 XASSIGN(CLOSE_WAIT, tn);
988 XASSIGN(LAST_ACK, tn);
989 XASSIGN(TIME_WAIT, tn);
991 XASSIGN(RETRANS, tn);
994 #define XASSIGN(XNAME, rval) \
995 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
997 XASSIGN(LOOSE, &tn->tcp_loose);
998 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
999 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1000 XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
1003 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1004 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1009 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1010 struct ctl_table *table)
1012 #ifdef CONFIG_NF_CT_PROTO_SCTP
1013 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1015 #define XASSIGN(XNAME, sn) \
1016 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1017 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1019 XASSIGN(CLOSED, sn);
1020 XASSIGN(COOKIE_WAIT, sn);
1021 XASSIGN(COOKIE_ECHOED, sn);
1022 XASSIGN(ESTABLISHED, sn);
1023 XASSIGN(SHUTDOWN_SENT, sn);
1024 XASSIGN(SHUTDOWN_RECD, sn);
1025 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1026 XASSIGN(HEARTBEAT_SENT, sn);
1031 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1032 struct ctl_table *table)
1034 #ifdef CONFIG_NF_CT_PROTO_DCCP
1035 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1037 #define XASSIGN(XNAME, dn) \
1038 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1039 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1041 XASSIGN(REQUEST, dn);
1042 XASSIGN(RESPOND, dn);
1043 XASSIGN(PARTOPEN, dn);
1045 XASSIGN(CLOSEREQ, dn);
1046 XASSIGN(CLOSING, dn);
1047 XASSIGN(TIMEWAIT, dn);
1050 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1054 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1055 struct ctl_table *table)
1057 #ifdef CONFIG_NF_CT_PROTO_GRE
1058 struct nf_gre_net *gn = nf_gre_pernet(net);
1060 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1061 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1065 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1067 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1068 struct nf_udp_net *un = nf_udp_pernet(net);
1069 struct ctl_table *table;
1071 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1073 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1078 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1079 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1080 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1081 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1082 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1083 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1085 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1086 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1088 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1089 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1090 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1091 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1092 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1093 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1094 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1097 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1098 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1099 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1100 nf_conntrack_standalone_init_gre_sysctl(net, table);
1102 /* Don't allow non-init_net ns to alter global sysctls */
1103 if (!net_eq(&init_net, net)) {
1104 table[NF_SYSCTL_CT_MAX].mode = 0444;
1105 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1106 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1109 cnet->sysctl_header = register_net_sysctl_sz(net, "net/netfilter",
1111 ARRAY_SIZE(nf_ct_sysctl_table));
1112 if (!cnet->sysctl_header)
1113 goto out_unregister_netfilter;
1117 out_unregister_netfilter:
1122 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1124 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1125 struct ctl_table *table;
1127 table = cnet->sysctl_header->ctl_table_arg;
1128 unregister_net_sysctl_table(cnet->sysctl_header);
1132 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1137 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1140 #endif /* CONFIG_SYSCTL */
1142 static void nf_conntrack_fini_net(struct net *net)
1145 nf_ct_netns_put(net, NFPROTO_INET);
1147 nf_conntrack_standalone_fini_proc(net);
1148 nf_conntrack_standalone_fini_sysctl(net);
1151 static int nf_conntrack_pernet_init(struct net *net)
1155 net->ct.sysctl_checksum = 1;
1157 ret = nf_conntrack_standalone_init_sysctl(net);
1161 ret = nf_conntrack_standalone_init_proc(net);
1165 ret = nf_conntrack_init_net(net);
1170 ret = nf_ct_netns_get(net, NFPROTO_INET);
1178 nf_conntrack_cleanup_net(net);
1180 nf_conntrack_standalone_fini_proc(net);
1182 nf_conntrack_standalone_fini_sysctl(net);
1186 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1190 list_for_each_entry(net, net_exit_list, exit_list)
1191 nf_conntrack_fini_net(net);
1193 nf_conntrack_cleanup_net_list(net_exit_list);
1196 static struct pernet_operations nf_conntrack_net_ops = {
1197 .init = nf_conntrack_pernet_init,
1198 .exit_batch = nf_conntrack_pernet_exit,
1199 .id = &nf_conntrack_net_id,
1200 .size = sizeof(struct nf_conntrack_net),
1203 static int __init nf_conntrack_standalone_init(void)
1205 int ret = nf_conntrack_init_start();
1209 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1211 #ifdef CONFIG_SYSCTL
1212 nf_ct_netfilter_header =
1213 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1214 if (!nf_ct_netfilter_header) {
1215 pr_err("nf_conntrack: can't register to sysctl.\n");
1220 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1223 nf_conntrack_init_end();
1225 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1232 #ifdef CONFIG_SYSCTL
1233 unregister_net_sysctl_table(nf_ct_netfilter_header);
1236 nf_conntrack_cleanup_end();
1241 static void __exit nf_conntrack_standalone_fini(void)
1243 nf_conntrack_cleanup_start();
1244 unregister_pernet_subsys(&nf_conntrack_net_ops);
1245 #ifdef CONFIG_SYSCTL
1246 unregister_net_sysctl_table(nf_ct_netfilter_header);
1248 nf_conntrack_cleanup_end();
1251 module_init(nf_conntrack_standalone_init);
1252 module_exit(nf_conntrack_standalone_fini);