Skip to contentSkip to conversation

netfilter: nf_conntrack_tcp: preserve liberal flag in tcp options

PATCH Superseded Pablo Neira Ayuso 1 participant 4 messages Mar 21, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_conntrack_tcp: preserve liberal flag in tcp options

When tcp_options is called all flags are cleared.
When the IP_CT_TCP_FLAG_BE_LIBERAL is set it should be preserved otherwise such connections will fail in the window check.

---
Supersedes: https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/ which is not correct, it breaks TCP FIN handling.

 net/netfilter/nf_conntrack_proto_tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +4 -2
index d1582b888c0d..d8599ab5f767 100644
@@ -342,7 +342,7 @@ static void tcp_options(const struct sk_buff *skb,
 		return;
 
 	state->td_scale =
-	state->flags = 0;
+	state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL;
 
 	while (length > 0) {
 		int opcode=*ptr++;
@@ -873,7 +873,7 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 	struct nf_tcp_net *tn = nf_tcp_pernet(net);
 	struct nf_conntrack_tuple *tuple;
 	enum tcp_conntrack new_state, old_state;
-	unsigned int index, *timeouts;
+	unsigned int index, flags, *timeouts;
 	enum ip_conntrack_dir dir;
 	const struct tcphdr *th;
 	struct tcphdr _tcph;
@@ -968,8 +968,10 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
 			ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
 				ct->proto.tcp.last_flags;
+			flags = ct->proto.tcp.seen[dir].flags;
 			memset(&ct->proto.tcp.seen[dir], 0,
 			       sizeof(struct ip_ct_tcp_state));
+			ct->proto.tcp.seen[dir].flags = flags & IP_CT_TCP_FLAG_BE_LIBERAL;
 			break;
 		}
 		ct->proto.tcp.last_index = index;
Signature · 1 line
2.30.2
author maintainer
#1 lore raw
Quoted 3 lines · Pablo Neira Ayuso

When tcp_options is called all flags are cleared.
When the IP_CT_TCP_FLAG_BE_LIBERAL is set it should be preserved otherwise such connections will fail in the window check.

This patch completes 8437a6209f76 ("netfilter: nft_flow_offload: set liberal tracking mode for tcp")

I'm going to send v2 to add a wrapper function to update these flags, so we do not accidentally reset IP_CT_TCP_FLAG_BE_LIBERAL.

Quoted 44 lines

---
Supersedes: https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/ which is not correct, it breaks TCP FIN handling.

 net/netfilter/nf_conntrack_proto_tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +4 -2
index d1582b888c0d..d8599ab5f767 100644
@@ -342,7 +342,7 @@ static void tcp_options(const struct sk_buff *skb,
 		return;
 
 	state->td_scale =
-	state->flags = 0;
+	state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL;
 
 	while (length > 0) {
 		int opcode=*ptr++;
@@ -873,7 +873,7 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 	struct nf_tcp_net *tn = nf_tcp_pernet(net);
 	struct nf_conntrack_tuple *tuple;
 	enum tcp_conntrack new_state, old_state;
-	unsigned int index, *timeouts;
+	unsigned int index, flags, *timeouts;
 	enum ip_conntrack_dir dir;
 	const struct tcphdr *th;
 	struct tcphdr _tcph;
@@ -968,8 +968,10 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
 			ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
 				ct->proto.tcp.last_flags;
+			flags = ct->proto.tcp.seen[dir].flags;
 			memset(&ct->proto.tcp.seen[dir], 0,
 			       sizeof(struct ip_ct_tcp_state));
+			ct->proto.tcp.seen[dir].flags = flags & IP_CT_TCP_FLAG_BE_LIBERAL;
 			break;
 		}
 		ct->proto.tcp.last_index = index;
Signature · 1 line
2.30.2
author maintainer
#2 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Pablo Neira Ayuso

When tcp_options is called all flags are cleared.
When the IP_CT_TCP_FLAG_BE_LIBERAL is set it should be preserved otherwise such connections will fail in the window check.

This patch completes 8437a6209f76 ("netfilter: nft_flow_offload: set liberal tracking mode for tcp")

I'm going to send v2 to add a wrapper function to update these flags, so we do not accidentally reset IP_CT_TCP_FLAG_BE_LIBERAL.

Hm. Actually I don't see a clear way to add such wrapper function, so patch LGTM as is.

Quoted 44 lines
Quoted 44 lines

---
Supersedes: https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/ which is not correct, it breaks TCP FIN handling.

 net/netfilter/nf_conntrack_proto_tcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +4 -2
index d1582b888c0d..d8599ab5f767 100644
@@ -342,7 +342,7 @@ static void tcp_options(const struct sk_buff *skb,
 		return;
 
 	state->td_scale =
-	state->flags = 0;
+	state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL;
 
 	while (length > 0) {
 		int opcode=*ptr++;
@@ -873,7 +873,7 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 	struct nf_tcp_net *tn = nf_tcp_pernet(net);
 	struct nf_conntrack_tuple *tuple;
 	enum tcp_conntrack new_state, old_state;
-	unsigned int index, *timeouts;
+	unsigned int index, flags, *timeouts;
 	enum ip_conntrack_dir dir;
 	const struct tcphdr *th;
 	struct tcphdr _tcph;
@@ -968,8 +968,10 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
 			ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
 				ct->proto.tcp.last_flags;
+			flags = ct->proto.tcp.seen[dir].flags;
 			memset(&ct->proto.tcp.seen[dir], 0,
 			       sizeof(struct ip_ct_tcp_state));
+			ct->proto.tcp.seen[dir].flags = flags & IP_CT_TCP_FLAG_BE_LIBERAL;
 			break;
 		}
 		ct->proto.tcp.last_index = index;
Signature · 1 line
2.30.2
author maintainer
#3 lore raw
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Pablo Neira Ayuso

When tcp_options is called all flags are cleared.
When the IP_CT_TCP_FLAG_BE_LIBERAL is set it should be preserved otherwise such connections will fail in the window check.

Quoted 7 lines
Quoted 4 lines

This patch completes 8437a6209f76 ("netfilter: nft_flow_offload: set liberal tracking mode for tcp")

I'm going to send v2 to add a wrapper function to update these flags, so we do not accidentally reset IP_CT_TCP_FLAG_BE_LIBERAL.

Hm. Actually I don't see a clear way to add such wrapper function, so patch LGTM as is.

Maybe something like this patch that is attached by adding a more explicit function that resets the window tracking without touch the liberal flag, it might help to make it more evident to the reader.

From 4c42d6ca218a9b3da324224a027b753f6acb72c3 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Mon, 21 Mar 2022 10:40:07 +0100
Subject: [PATCH] netfilter: nf_conntrack_tcp: preserve liberal flag in tcp
options

When tcp_options is called all flags are cleared.
When the IP_CT_TCP_FLAG_BE_LIBERAL is set it should be preserved otherwise such connections will fail in the window check.

---
 net/netfilter/nf_conntrack_proto_tcp.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +13 -4
index d1582b888c0d..8ec55cd72572 100644
@@ -341,8 +341,8 @@ static void tcp_options(const struct sk_buff *skb,
 	if (!ptr)
 		return;
 
-	state->td_scale =
-	state->flags = 0;
+	state->td_scale = 0;
+	state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL;
 
 	while (length > 0) {
 		int opcode=*ptr++;
@@ -862,6 +862,16 @@ static bool tcp_can_early_drop(const struct nf_conn *ct)
 	return false;
 }
 
+static void nf_ct_tcp_state_reset(struct ip_ct_tcp_state *state)
+{
+	state->td_end		= 0;
+	state->td_maxend	= 0;
+	state->td_maxwin	= 0;
+	state->td_maxack	= 0;
+	state->td_scale		= 0;
+	state->flags		&= IP_CT_TCP_FLAG_BE_LIBERAL;
+}
+
 /* Returns verdict for packet, or -1 for invalid. */
 int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			    struct sk_buff *skb,
@@ -968,8 +978,7 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,
 			ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
 			ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
 				ct->proto.tcp.last_flags;
-			memset(&ct->proto.tcp.seen[dir], 0,
-			       sizeof(struct ip_ct_tcp_state));
+			nf_ct_tcp_state_reset(&ct->proto.tcp.seen[dir]);
 			break;
 		}
 		ct->proto.tcp.last_index = index;
Signature · 1 line
2.30.2