Ali Abdallah a5af9d
From 3684346961e68b512591ca27c5422180054b2c73 Mon Sep 17 00:00:00 2001
Ali Abdallah a5af9d
From: Ali Abdallah <aabdallah@suse.de>
Ali Abdallah a5af9d
Date: Fri, 28 May 2021 11:52:21 +0200
Ali Abdallah a5af9d
Subject: [PATCH] netfilter: conntrack: add new sysctl to disable RST check
Ali Abdallah a5af9d
Patch-mainline: Not yet, submitted
Ali Abdallah a5af9d
References: bsc#1183947 bsc#1185950
Ali Abdallah a5af9d
Ali Abdallah a5af9d
This patch adds a new sysctl tcp_ignore_invalid_rst to disable marking
Ali Abdallah a5af9d
out of segments RSTs as INVALID.
Ali Abdallah a5af9d
Ali Abdallah a5af9d
Signed-off-by: Ali Abdallah <aabdallah@suse.de>
Ali Abdallah a5af9d
Reviewed-by: Florian Westphal <fw@strlen.de>
Ali Abdallah a5af9d
Ali Abdallah a5af9d
Acked-by: Ali Abdallah <aabdallah@suse.de>
Ali Abdallah a5af9d
---
Ali Abdallah a5af9d
 Documentation/networking/nf_conntrack-sysctl.txt |  6 ++++++
Ali Abdallah a5af9d
 include/net/netns/conntrack.h                    |  3 +++
Ali Abdallah a5af9d
 net/netfilter/nf_conntrack_proto_tcp.c           | 14 +++++++++++++-
Ali Abdallah a5af9d
 3 files changed, 22 insertions(+), 1 deletion(-)
Ali Abdallah a5af9d
Ali Abdallah a5af9d
diff --git a/Documentation/networking/nf_conntrack-sysctl.txt b/Documentation/networking/nf_conntrack-sysctl.txt
Ali Abdallah a5af9d
index 497d66828..ae1f28f98 100644
Ali Abdallah a5af9d
--- a/Documentation/networking/nf_conntrack-sysctl.txt
Ali Abdallah a5af9d
+++ b/Documentation/networking/nf_conntrack-sysctl.txt
Ali Abdallah a5af9d
@@ -114,6 +114,12 @@ nf_conntrack_tcp_be_liberal - BOOLEAN
Ali Abdallah a5af9d
 	Be conservative in what you do, be liberal in what you accept from others.
Ali Abdallah a5af9d
 	If it's non-zero, we mark only out of window RST segments as INVALID.
Ali Abdallah a5af9d
 
Ali Abdallah a5af9d
+nf_conntrack_tcp_ignore_invalid_rst - BOOLEAN
Ali Abdallah a5af9d
+	- 0 - disabled (default)
Ali Abdallah a5af9d
+	- 1 - enabled
Ali Abdallah a5af9d
+
Ali Abdallah a5af9d
+	If it's non-zero, we don't mark out of window RST segments as INVALID.
Ali Abdallah a5af9d
+
Ali Abdallah a5af9d
 nf_conntrack_tcp_loose - BOOLEAN
Ali Abdallah a5af9d
 	0 - disabled
Ali Abdallah a5af9d
 	not 0 - enabled (default)
Ali Abdallah a5af9d
diff --git a/include/net/netns/conntrack.h b/include/net/netns/conntrack.h
Ali Abdallah a5af9d
index 17724c62d..d78d2d088 100644
Ali Abdallah a5af9d
--- a/include/net/netns/conntrack.h
Ali Abdallah a5af9d
+++ b/include/net/netns/conntrack.h
Ali Abdallah a5af9d
@@ -36,6 +36,9 @@ struct nf_tcp_net {
Ali Abdallah a5af9d
 	unsigned int tcp_loose;
Ali Abdallah a5af9d
 	unsigned int tcp_be_liberal;
Ali Abdallah a5af9d
 	unsigned int tcp_max_retrans;
Ali Abdallah a5af9d
+#ifndef __GENKSYMS__
Ali Abdallah a5af9d
+	unsigned int tcp_ignore_invalid_rst;
Ali Abdallah a5af9d
+#endif
Ali Abdallah a5af9d
 };
Ali Abdallah a5af9d
 
Ali Abdallah a5af9d
 enum udp_conntrack {
Ali Abdallah a5af9d
diff --git a/net/netfilter/nf_conntrack_proto_tcp.c b/net/netfilter/nf_conntrack_proto_tcp.c
Ali Abdallah a5af9d
index d08e64776..751e551c4 100644
Ali Abdallah a5af9d
--- a/net/netfilter/nf_conntrack_proto_tcp.c
Ali Abdallah a5af9d
+++ b/net/netfilter/nf_conntrack_proto_tcp.c
Ali Abdallah a5af9d
@@ -38,6 +38,9 @@
Ali Abdallah a5af9d
     If it's non-zero, we mark only out of window RST segments as INVALID. */
Ali Abdallah a5af9d
 static int nf_ct_tcp_be_liberal __read_mostly = 0;
Ali Abdallah a5af9d
 
Ali Abdallah a5af9d
+/* If it's non-zero, we turn off RST sequence number check */
Ali Abdallah a5af9d
+static int nf_ct_tcp_ignore_invalid_rst __read_mostly = 0;
Ali Abdallah a5af9d
+
Ali Abdallah a5af9d
 /* If it is set to zero, we disable picking up already established
Ali Abdallah a5af9d
    connections. */
Ali Abdallah a5af9d
 static int nf_ct_tcp_loose __read_mostly = 1;
Ali Abdallah a5af9d
@@ -1044,7 +1047,8 @@ static int tcp_packet(struct nf_conn *ct,
Ali Abdallah a5af9d
 			if (seq == 0 && !nf_conntrack_tcp_established(ct))
Ali Abdallah a5af9d
 				break;
Ali Abdallah a5af9d
 
Ali Abdallah a5af9d
-			if (before(seq, ct->proto.tcp.seen[!dir].td_maxack)) {
Ali Abdallah a5af9d
+			if (before(seq, ct->proto.tcp.seen[!dir].td_maxack) &&
Ali Abdallah a5af9d
+				   !tn->tcp_ignore_invalid_rst) {
Ali Abdallah a5af9d
 				/* Invalid RST  */
Ali Abdallah a5af9d
 				spin_unlock_bh(&ct->lock);
Ali Abdallah a5af9d
 				nf_log_packet(net, pf, 0, skb, NULL, NULL,
Ali Abdallah a5af9d
@@ -1544,6 +1548,12 @@ static struct ctl_table tcp_sysctl_table[] = {
Ali Abdallah a5af9d
 		.mode		= 0644,
Ali Abdallah a5af9d
 		.proc_handler	= proc_dointvec,
Ali Abdallah a5af9d
 	},
Ali Abdallah a5af9d
+	{
Ali Abdallah a5af9d
+		.procname       = "nf_conntrack_tcp_ignore_invalid_rst",
Ali Abdallah a5af9d
+		.maxlen         = sizeof(unsigned int),
Ali Abdallah a5af9d
+		.mode           = 0644,
Ali Abdallah a5af9d
+		.proc_handler   = proc_dointvec,
Ali Abdallah a5af9d
+	},
Ali Abdallah a5af9d
 	{ }
Ali Abdallah a5af9d
 };
Ali Abdallah a5af9d
 #endif /* CONFIG_SYSCTL */
Ali Abdallah a5af9d
@@ -1574,6 +1584,7 @@ static int tcp_kmemdup_sysctl_table(struct nf_proto_net *pn,
Ali Abdallah a5af9d
 	pn->ctl_table[10].data = &tn->tcp_loose;
Ali Abdallah a5af9d
 	pn->ctl_table[11].data = &tn->tcp_be_liberal;
Ali Abdallah a5af9d
 	pn->ctl_table[12].data = &tn->tcp_max_retrans;
Ali Abdallah a5af9d
+	pn->ctl_table[13].data = &tn->tcp_ignore_invalid_rst;
Ali Abdallah a5af9d
 #endif
Ali Abdallah a5af9d
 	return 0;
Ali Abdallah a5af9d
 }
Ali Abdallah a5af9d
@@ -1592,6 +1603,7 @@ static int tcp_init_net(struct net *net, u_int16_t proto)
Ali Abdallah a5af9d
 		tn->tcp_loose = nf_ct_tcp_loose;
Ali Abdallah a5af9d
 		tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
Ali Abdallah a5af9d
 		tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
Ali Abdallah a5af9d
+		tn->tcp_ignore_invalid_rst = nf_ct_tcp_ignore_invalid_rst;
Ali Abdallah a5af9d
 	}
Ali Abdallah a5af9d
 
Ali Abdallah a5af9d
 	return tcp_kmemdup_sysctl_table(pn, tn);
Ali Abdallah a5af9d
-- 
Ali Abdallah a5af9d
2.26.2
Ali Abdallah a5af9d
Ali Abdallah a5af9d