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 static struct ctl_table nf_ct_sysctl_table[] = {
623 [NF_SYSCTL_CT_MAX] = {
624 .procname = "nf_conntrack_max",
625 .data = &nf_conntrack_max,
626 .maxlen = sizeof(int),
628 .proc_handler = proc_dointvec,
630 [NF_SYSCTL_CT_COUNT] = {
631 .procname = "nf_conntrack_count",
632 .maxlen = sizeof(int),
634 .proc_handler = proc_dointvec,
636 [NF_SYSCTL_CT_BUCKETS] = {
637 .procname = "nf_conntrack_buckets",
638 .data = &nf_conntrack_htable_size_user,
639 .maxlen = sizeof(unsigned int),
641 .proc_handler = nf_conntrack_hash_sysctl,
643 [NF_SYSCTL_CT_CHECKSUM] = {
644 .procname = "nf_conntrack_checksum",
645 .data = &init_net.ct.sysctl_checksum,
646 .maxlen = sizeof(u8),
648 .proc_handler = proc_dou8vec_minmax,
649 .extra1 = SYSCTL_ZERO,
650 .extra2 = SYSCTL_ONE,
652 [NF_SYSCTL_CT_LOG_INVALID] = {
653 .procname = "nf_conntrack_log_invalid",
654 .data = &init_net.ct.sysctl_log_invalid,
655 .maxlen = sizeof(u8),
657 .proc_handler = proc_dou8vec_minmax,
659 [NF_SYSCTL_CT_EXPECT_MAX] = {
660 .procname = "nf_conntrack_expect_max",
661 .data = &nf_ct_expect_max,
662 .maxlen = sizeof(int),
664 .proc_handler = proc_dointvec,
666 [NF_SYSCTL_CT_ACCT] = {
667 .procname = "nf_conntrack_acct",
668 .data = &init_net.ct.sysctl_acct,
669 .maxlen = sizeof(u8),
671 .proc_handler = proc_dou8vec_minmax,
672 .extra1 = SYSCTL_ZERO,
673 .extra2 = SYSCTL_ONE,
675 #ifdef CONFIG_NF_CONNTRACK_EVENTS
676 [NF_SYSCTL_CT_EVENTS] = {
677 .procname = "nf_conntrack_events",
678 .data = &init_net.ct.sysctl_events,
679 .maxlen = sizeof(u8),
681 .proc_handler = proc_dou8vec_minmax,
682 .extra1 = SYSCTL_ZERO,
683 .extra2 = SYSCTL_TWO,
686 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
687 [NF_SYSCTL_CT_TIMESTAMP] = {
688 .procname = "nf_conntrack_timestamp",
689 .data = &init_net.ct.sysctl_tstamp,
690 .maxlen = sizeof(u8),
692 .proc_handler = proc_dou8vec_minmax,
693 .extra1 = SYSCTL_ZERO,
694 .extra2 = SYSCTL_ONE,
697 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
698 .procname = "nf_conntrack_generic_timeout",
699 .maxlen = sizeof(unsigned int),
701 .proc_handler = proc_dointvec_jiffies,
703 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
704 .procname = "nf_conntrack_tcp_timeout_syn_sent",
705 .maxlen = sizeof(unsigned int),
707 .proc_handler = proc_dointvec_jiffies,
709 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
710 .procname = "nf_conntrack_tcp_timeout_syn_recv",
711 .maxlen = sizeof(unsigned int),
713 .proc_handler = proc_dointvec_jiffies,
715 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
716 .procname = "nf_conntrack_tcp_timeout_established",
717 .maxlen = sizeof(unsigned int),
719 .proc_handler = proc_dointvec_jiffies,
721 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
722 .procname = "nf_conntrack_tcp_timeout_fin_wait",
723 .maxlen = sizeof(unsigned int),
725 .proc_handler = proc_dointvec_jiffies,
727 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
728 .procname = "nf_conntrack_tcp_timeout_close_wait",
729 .maxlen = sizeof(unsigned int),
731 .proc_handler = proc_dointvec_jiffies,
733 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
734 .procname = "nf_conntrack_tcp_timeout_last_ack",
735 .maxlen = sizeof(unsigned int),
737 .proc_handler = proc_dointvec_jiffies,
739 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
740 .procname = "nf_conntrack_tcp_timeout_time_wait",
741 .maxlen = sizeof(unsigned int),
743 .proc_handler = proc_dointvec_jiffies,
745 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
746 .procname = "nf_conntrack_tcp_timeout_close",
747 .maxlen = sizeof(unsigned int),
749 .proc_handler = proc_dointvec_jiffies,
751 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
752 .procname = "nf_conntrack_tcp_timeout_max_retrans",
753 .maxlen = sizeof(unsigned int),
755 .proc_handler = proc_dointvec_jiffies,
757 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
758 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
759 .maxlen = sizeof(unsigned int),
761 .proc_handler = proc_dointvec_jiffies,
763 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
764 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
765 .procname = "nf_flowtable_tcp_timeout",
766 .maxlen = sizeof(unsigned int),
768 .proc_handler = proc_dointvec_jiffies,
771 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
772 .procname = "nf_conntrack_tcp_loose",
773 .maxlen = sizeof(u8),
775 .proc_handler = proc_dou8vec_minmax,
776 .extra1 = SYSCTL_ZERO,
777 .extra2 = SYSCTL_ONE,
779 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
780 .procname = "nf_conntrack_tcp_be_liberal",
781 .maxlen = sizeof(u8),
783 .proc_handler = proc_dou8vec_minmax,
784 .extra1 = SYSCTL_ZERO,
785 .extra2 = SYSCTL_ONE,
787 [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
788 .procname = "nf_conntrack_tcp_ignore_invalid_rst",
789 .maxlen = sizeof(u8),
791 .proc_handler = proc_dou8vec_minmax,
792 .extra1 = SYSCTL_ZERO,
793 .extra2 = SYSCTL_ONE,
795 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
796 .procname = "nf_conntrack_tcp_max_retrans",
797 .maxlen = sizeof(u8),
799 .proc_handler = proc_dou8vec_minmax,
801 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
802 .procname = "nf_conntrack_udp_timeout",
803 .maxlen = sizeof(unsigned int),
805 .proc_handler = proc_dointvec_jiffies,
807 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
808 .procname = "nf_conntrack_udp_timeout_stream",
809 .maxlen = sizeof(unsigned int),
811 .proc_handler = proc_dointvec_jiffies,
813 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
814 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
815 .procname = "nf_flowtable_udp_timeout",
816 .maxlen = sizeof(unsigned int),
818 .proc_handler = proc_dointvec_jiffies,
821 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
822 .procname = "nf_conntrack_icmp_timeout",
823 .maxlen = sizeof(unsigned int),
825 .proc_handler = proc_dointvec_jiffies,
827 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
828 .procname = "nf_conntrack_icmpv6_timeout",
829 .maxlen = sizeof(unsigned int),
831 .proc_handler = proc_dointvec_jiffies,
833 #ifdef CONFIG_NF_CT_PROTO_SCTP
834 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
835 .procname = "nf_conntrack_sctp_timeout_closed",
836 .maxlen = sizeof(unsigned int),
838 .proc_handler = proc_dointvec_jiffies,
840 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
841 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
842 .maxlen = sizeof(unsigned int),
844 .proc_handler = proc_dointvec_jiffies,
846 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
847 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
848 .maxlen = sizeof(unsigned int),
850 .proc_handler = proc_dointvec_jiffies,
852 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
853 .procname = "nf_conntrack_sctp_timeout_established",
854 .maxlen = sizeof(unsigned int),
856 .proc_handler = proc_dointvec_jiffies,
858 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
859 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
860 .maxlen = sizeof(unsigned int),
862 .proc_handler = proc_dointvec_jiffies,
864 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
865 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
866 .maxlen = sizeof(unsigned int),
868 .proc_handler = proc_dointvec_jiffies,
870 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
871 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
872 .maxlen = sizeof(unsigned int),
874 .proc_handler = proc_dointvec_jiffies,
876 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
877 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
878 .maxlen = sizeof(unsigned int),
880 .proc_handler = proc_dointvec_jiffies,
883 #ifdef CONFIG_NF_CT_PROTO_DCCP
884 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
885 .procname = "nf_conntrack_dccp_timeout_request",
886 .maxlen = sizeof(unsigned int),
888 .proc_handler = proc_dointvec_jiffies,
890 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
891 .procname = "nf_conntrack_dccp_timeout_respond",
892 .maxlen = sizeof(unsigned int),
894 .proc_handler = proc_dointvec_jiffies,
896 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
897 .procname = "nf_conntrack_dccp_timeout_partopen",
898 .maxlen = sizeof(unsigned int),
900 .proc_handler = proc_dointvec_jiffies,
902 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
903 .procname = "nf_conntrack_dccp_timeout_open",
904 .maxlen = sizeof(unsigned int),
906 .proc_handler = proc_dointvec_jiffies,
908 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
909 .procname = "nf_conntrack_dccp_timeout_closereq",
910 .maxlen = sizeof(unsigned int),
912 .proc_handler = proc_dointvec_jiffies,
914 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
915 .procname = "nf_conntrack_dccp_timeout_closing",
916 .maxlen = sizeof(unsigned int),
918 .proc_handler = proc_dointvec_jiffies,
920 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
921 .procname = "nf_conntrack_dccp_timeout_timewait",
922 .maxlen = sizeof(unsigned int),
924 .proc_handler = proc_dointvec_jiffies,
926 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
927 .procname = "nf_conntrack_dccp_loose",
928 .maxlen = sizeof(u8),
930 .proc_handler = proc_dou8vec_minmax,
931 .extra1 = SYSCTL_ZERO,
932 .extra2 = SYSCTL_ONE,
935 #ifdef CONFIG_NF_CT_PROTO_GRE
936 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
937 .procname = "nf_conntrack_gre_timeout",
938 .maxlen = sizeof(unsigned int),
940 .proc_handler = proc_dointvec_jiffies,
942 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
943 .procname = "nf_conntrack_gre_timeout_stream",
944 .maxlen = sizeof(unsigned int),
946 .proc_handler = proc_dointvec_jiffies,
949 #ifdef CONFIG_LWTUNNEL
950 [NF_SYSCTL_CT_LWTUNNEL] = {
951 .procname = "nf_hooks_lwtunnel",
953 .maxlen = sizeof(int),
955 .proc_handler = nf_hooks_lwtunnel_sysctl_handler,
960 static struct ctl_table nf_ct_netfilter_table[] = {
962 .procname = "nf_conntrack_max",
963 .data = &nf_conntrack_max,
964 .maxlen = sizeof(int),
966 .proc_handler = proc_dointvec,
970 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
971 struct ctl_table *table)
973 struct nf_tcp_net *tn = nf_tcp_pernet(net);
975 #define XASSIGN(XNAME, tn) \
976 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
977 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
979 XASSIGN(SYN_SENT, tn);
980 XASSIGN(SYN_RECV, tn);
981 XASSIGN(ESTABLISHED, tn);
982 XASSIGN(FIN_WAIT, tn);
983 XASSIGN(CLOSE_WAIT, tn);
984 XASSIGN(LAST_ACK, tn);
985 XASSIGN(TIME_WAIT, tn);
987 XASSIGN(RETRANS, tn);
990 #define XASSIGN(XNAME, rval) \
991 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
993 XASSIGN(LOOSE, &tn->tcp_loose);
994 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
995 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
996 XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
999 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1000 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1005 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1006 struct ctl_table *table)
1008 #ifdef CONFIG_NF_CT_PROTO_SCTP
1009 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1011 #define XASSIGN(XNAME, sn) \
1012 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1013 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1015 XASSIGN(CLOSED, sn);
1016 XASSIGN(COOKIE_WAIT, sn);
1017 XASSIGN(COOKIE_ECHOED, sn);
1018 XASSIGN(ESTABLISHED, sn);
1019 XASSIGN(SHUTDOWN_SENT, sn);
1020 XASSIGN(SHUTDOWN_RECD, sn);
1021 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1022 XASSIGN(HEARTBEAT_SENT, sn);
1027 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1028 struct ctl_table *table)
1030 #ifdef CONFIG_NF_CT_PROTO_DCCP
1031 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1033 #define XASSIGN(XNAME, dn) \
1034 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1035 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1037 XASSIGN(REQUEST, dn);
1038 XASSIGN(RESPOND, dn);
1039 XASSIGN(PARTOPEN, dn);
1041 XASSIGN(CLOSEREQ, dn);
1042 XASSIGN(CLOSING, dn);
1043 XASSIGN(TIMEWAIT, dn);
1046 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1050 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1051 struct ctl_table *table)
1053 #ifdef CONFIG_NF_CT_PROTO_GRE
1054 struct nf_gre_net *gn = nf_gre_pernet(net);
1056 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1057 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1061 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1063 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1064 struct nf_udp_net *un = nf_udp_pernet(net);
1065 struct ctl_table *table;
1067 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1069 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1074 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1075 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1076 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1077 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1078 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1079 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1081 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1082 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1084 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1085 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1086 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1087 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1088 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1089 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1090 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1093 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1094 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1095 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1096 nf_conntrack_standalone_init_gre_sysctl(net, table);
1098 /* Don't allow non-init_net ns to alter global sysctls */
1099 if (!net_eq(&init_net, net)) {
1100 table[NF_SYSCTL_CT_MAX].mode = 0444;
1101 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1102 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1105 cnet->sysctl_header = register_net_sysctl_sz(net, "net/netfilter",
1107 ARRAY_SIZE(nf_ct_sysctl_table));
1108 if (!cnet->sysctl_header)
1109 goto out_unregister_netfilter;
1113 out_unregister_netfilter:
1118 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1120 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1121 const struct ctl_table *table;
1123 table = cnet->sysctl_header->ctl_table_arg;
1124 unregister_net_sysctl_table(cnet->sysctl_header);
1128 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1133 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1136 #endif /* CONFIG_SYSCTL */
1138 static void nf_conntrack_fini_net(struct net *net)
1141 nf_ct_netns_put(net, NFPROTO_INET);
1143 nf_conntrack_standalone_fini_proc(net);
1144 nf_conntrack_standalone_fini_sysctl(net);
1147 static int nf_conntrack_pernet_init(struct net *net)
1151 net->ct.sysctl_checksum = 1;
1153 ret = nf_conntrack_standalone_init_sysctl(net);
1157 ret = nf_conntrack_standalone_init_proc(net);
1161 ret = nf_conntrack_init_net(net);
1166 ret = nf_ct_netns_get(net, NFPROTO_INET);
1174 nf_conntrack_cleanup_net(net);
1176 nf_conntrack_standalone_fini_proc(net);
1178 nf_conntrack_standalone_fini_sysctl(net);
1182 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1186 list_for_each_entry(net, net_exit_list, exit_list)
1187 nf_conntrack_fini_net(net);
1189 nf_conntrack_cleanup_net_list(net_exit_list);
1192 static struct pernet_operations nf_conntrack_net_ops = {
1193 .init = nf_conntrack_pernet_init,
1194 .exit_batch = nf_conntrack_pernet_exit,
1195 .id = &nf_conntrack_net_id,
1196 .size = sizeof(struct nf_conntrack_net),
1199 static int __init nf_conntrack_standalone_init(void)
1201 int ret = nf_conntrack_init_start();
1205 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1207 #ifdef CONFIG_SYSCTL
1208 nf_ct_netfilter_header =
1209 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1210 if (!nf_ct_netfilter_header) {
1211 pr_err("nf_conntrack: can't register to sysctl.\n");
1216 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1219 nf_conntrack_init_end();
1221 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1228 #ifdef CONFIG_SYSCTL
1229 unregister_net_sysctl_table(nf_ct_netfilter_header);
1232 nf_conntrack_cleanup_end();
1237 static void __exit nf_conntrack_standalone_fini(void)
1239 nf_conntrack_cleanup_start();
1240 unregister_pernet_subsys(&nf_conntrack_net_ops);
1241 #ifdef CONFIG_SYSCTL
1242 unregister_net_sysctl_table(nf_ct_netfilter_header);
1244 nf_conntrack_cleanup_end();
1247 module_init(nf_conntrack_standalone_init);
1248 module_exit(nf_conntrack_standalone_fini);