Skip to contentSkip to conversation

tcp: support rstreasons in the passive logic

PATCH Superseded Jason Xing 1 participant 7 messages May 9, 2024 – May 10, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

cover tcp: support rstreasons in the passive logic

From: Jason Xing <[email protected]>

In this series, I split all kinds of reasons into five part which, I think, can be easily reviewed. I respectively implement corresponding rstreasons in those functions. After this, we can trace the whole tcp passive reset with clear reasons.

Jason Xing (5):
tcp: fully support sk reset reasons in tcp_rcv_synsent_state_process()
tcp: fully support sk reset reason in tcp_ack()
tcp: fully support sk reset reason in tcp_rcv_state_process()
tcp: handle timewait cases in rstreason logic
tcp: handle rstreason in tcp_check_req()

 include/net/rstreason.h  | 58 ++++++++++++++++++++++++++++++++++++++++
 net/ipv4/tcp_ipv4.c      |  2 +-
 net/ipv4/tcp_minisocks.c |  2 +-
 net/ipv6/tcp_ipv6.c      |  2 +-
 4 files changed, 61 insertions(+), 3 deletions(-)
Signature · 1 line
2.37.3
author
#1 lore raw

PATCH 1/5 tcp: fully support sk reset reasons in tcp_rcv_synsent_state_process()

From: Jason Xing <[email protected]>

In this function, only updating the map can finish the job for socket reset reason because the corresponding drop reasons are ready.

---
 include/net/rstreason.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/net/rstreason.h +17 -0
index df3b6ac0c9b3..f87814a60205 100644
@@ -8,6 +8,8 @@
 #define DEFINE_RST_REASON(FN, FNe)	\
 	FN(NOT_SPECIFIED)		\
 	FN(NO_SOCKET)			\
+	FN(TCP_INVALID_ACK_SEQUENCE)	\
+	FN(TCP_RFC7323_PAWS)		\
 	FN(MPTCP_RST_EUNSPEC)		\
 	FN(MPTCP_RST_EMPTCP)		\
 	FN(MPTCP_RST_ERESOURCE)		\
@@ -37,6 +39,17 @@ enum sk_rst_reason {
 	SK_RST_REASON_NOT_SPECIFIED,
 	/** @SK_RST_REASON_NO_SOCKET: no valid socket that can be used */
 	SK_RST_REASON_NO_SOCKET,
+	/**
+	 * @SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE: Not acceptable ACK SEQ
+	 * field because ack sequence is not in the window between snd_una
+	 * and snd_nxt
+	 */
+	SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE,
+	/**
+	 * @SK_RST_REASON_TCP_RFC7323_PAWS: PAWS check, corresponding to
+	 * LINUX_MIB_PAWSESTABREJECTED, LINUX_MIB_PAWSACTIVEREJECTED
+	 */
+	SK_RST_REASON_TCP_RFC7323_PAWS,
 
 	/* Copy from include/uapi/linux/mptcp.h.
 	 * These reset fields will not be changed since they adhere to
@@ -113,6 +126,10 @@ sk_rst_convert_drop_reason(enum skb_drop_reason reason)
 		return SK_RST_REASON_NOT_SPECIFIED;
 	case SKB_DROP_REASON_NO_SOCKET:
 		return SK_RST_REASON_NO_SOCKET;
+	case SKB_DROP_REASON_TCP_INVALID_ACK_SEQUENCE:
+		return SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE;
+	case SKB_DROP_REASON_TCP_RFC7323_PAWS:
+		return SK_RST_REASON_TCP_RFC7323_PAWS;
 	default:
 		/* If we don't have our own corresponding reason */
 		return SK_RST_REASON_NOT_SPECIFIED;
Signature · 1 line
2.37.3
author
#2 lore raw

PATCH 2/5 tcp: fully support sk reset reason in tcp_ack()

In reply to Jason Xing

From: Jason Xing <[email protected]>

Based on the existing skb drop reason, updating the rstreason map can help us finish the rstreason job in this function.

---
 include/net/rstreason.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/net/rstreason.h +10 -0
index f87814a60205..a32b0fa17de2 100644
@@ -10,6 +10,8 @@
 	FN(NO_SOCKET)			\
 	FN(TCP_INVALID_ACK_SEQUENCE)	\
 	FN(TCP_RFC7323_PAWS)		\
+	FN(TCP_TOO_OLD_ACK)		\
+	FN(TCP_ACK_UNSENT_DATA)		\
 	FN(MPTCP_RST_EUNSPEC)		\
 	FN(MPTCP_RST_EMPTCP)		\
 	FN(MPTCP_RST_ERESOURCE)		\
@@ -50,6 +52,10 @@ enum sk_rst_reason {
 	 * LINUX_MIB_PAWSESTABREJECTED, LINUX_MIB_PAWSACTIVEREJECTED
 	 */
 	SK_RST_REASON_TCP_RFC7323_PAWS,
+	/** @SK_RST_REASON_TCP_TOO_OLD_ACK: TCP ACK is too old */
+	SK_RST_REASON_TCP_TOO_OLD_ACK,
+	/** @SK_RST_REASON_TCP_ACK_UNSENT_DATA */
+	SK_RST_REASON_TCP_ACK_UNSENT_DATA,
 
 	/* Copy from include/uapi/linux/mptcp.h.
 	 * These reset fields will not be changed since they adhere to
@@ -130,6 +136,10 @@ sk_rst_convert_drop_reason(enum skb_drop_reason reason)
 		return SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE;
 	case SKB_DROP_REASON_TCP_RFC7323_PAWS:
 		return SK_RST_REASON_TCP_RFC7323_PAWS;
+	case SKB_DROP_REASON_TCP_TOO_OLD_ACK:
+		return SK_RST_REASON_TCP_TOO_OLD_ACK;
+	case SKB_DROP_REASON_TCP_ACK_UNSENT_DATA:
+		return SK_RST_REASON_TCP_ACK_UNSENT_DATA;
 	default:
 		/* If we don't have our own corresponding reason */
 		return SK_RST_REASON_NOT_SPECIFIED;
Signature · 1 line
2.37.3
author
#3 lore raw

Re: [PATCH net-next 2/5] tcp: fully support sk reset reason in tcp_ack()

Quoted 25 lines · Jason Xing

From: Jason Xing <[email protected]>

Based on the existing skb drop reason, updating the rstreason map can help us finish the rstreason job in this function.

---
 include/net/rstreason.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/net/rstreason.h +2 -0
index f87814a60205..a32b0fa17de2 100644
@@ -10,6 +10,8 @@
        FN(NO_SOCKET)                   \
        FN(TCP_INVALID_ACK_SEQUENCE)    \
        FN(TCP_RFC7323_PAWS)            \
+       FN(TCP_TOO_OLD_ACK)             \
+       FN(TCP_ACK_UNSENT_DATA)         \
        FN(MPTCP_RST_EUNSPEC)           \
        FN(MPTCP_RST_EMPTCP)            \
        FN(MPTCP_RST_ERESOURCE)         \

include/net/rstreason.h enum sk_rst_reason { · line 57

Quoted 6 lines
include/net/rstreason.h +3 -0
@@ -50,6 +52,10 @@ enum sk_rst_reason {
         * LINUX_MIB_PAWSESTABREJECTED, LINUX_MIB_PAWSACTIVEREJECTED
         */
        SK_RST_REASON_TCP_RFC7323_PAWS,
+       /** @SK_RST_REASON_TCP_TOO_OLD_ACK: TCP ACK is too old */
+       SK_RST_REASON_TCP_TOO_OLD_ACK,
+       /** @SK_RST_REASON_TCP_ACK_UNSENT_DATA */

I'll add the detailed comment into this kdoc which was found by patchwork in V2 series to make sure every item has its own explanation.

Thanks,
Jason

Quoted 18 lines
include/net/rstreason.h +5 -1
+       SK_RST_REASON_TCP_ACK_UNSENT_DATA,
 
        /* Copy from include/uapi/linux/mptcp.h.
         * These reset fields will not be changed since they adhere to
@@ -130,6 +136,10 @@ sk_rst_convert_drop_reason(enum skb_drop_reason reason)
                return SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE;
        case SKB_DROP_REASON_TCP_RFC7323_PAWS:
                return SK_RST_REASON_TCP_RFC7323_PAWS;
+       case SKB_DROP_REASON_TCP_TOO_OLD_ACK:
+               return SK_RST_REASON_TCP_TOO_OLD_ACK;
+       case SKB_DROP_REASON_TCP_ACK_UNSENT_DATA:
+               return SK_RST_REASON_TCP_ACK_UNSENT_DATA;
        default:
                /* If we don't have our own corresponding reason */
                return SK_RST_REASON_NOT_SPECIFIED;
--

2.37.3

author
#4 lore raw

PATCH 3/5 tcp: fully support sk reset reason in tcp_rcv_state_process()

In reply to Jason Xing

From: Jason Xing <[email protected]>

Like the previous patch does in this series, finish the conversion map is enough to let rstreason mechanism work in this function.

---
 include/net/rstreason.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/net/rstreason.h +18 -0
index a32b0fa17de2..61855d4b27e2 100644
@@ -12,6 +12,9 @@
 	FN(TCP_RFC7323_PAWS)		\
 	FN(TCP_TOO_OLD_ACK)		\
 	FN(TCP_ACK_UNSENT_DATA)		\
+	FN(TCP_FLAGS)			\
+	FN(TCP_OLD_ACK)			\
+	FN(TCP_ABORT_ON_DATA)		\
 	FN(MPTCP_RST_EUNSPEC)		\
 	FN(MPTCP_RST_EMPTCP)		\
 	FN(MPTCP_RST_ERESOURCE)		\
@@ -56,6 +59,15 @@ enum sk_rst_reason {
 	SK_RST_REASON_TCP_TOO_OLD_ACK,
 	/** @SK_RST_REASON_TCP_ACK_UNSENT_DATA */
 	SK_RST_REASON_TCP_ACK_UNSENT_DATA,
+	/** @SK_RST_REASON_TCP_FLAGS: TCP flags invalid */
+	SK_RST_REASON_TCP_FLAGS,
+	/** @SK_RST_REASON_TCP_OLD_ACK: TCP ACK is old, but in window */
+	SK_RST_REASON_TCP_OLD_ACK,
+	/**
+	 * @SK_RST_REASON_TCP_ABORT_ON_DATA: abort on data
+	 * corresponding to LINUX_MIB_TCPABORTONDATA
+	 */
+	SK_RST_REASON_TCP_ABORT_ON_DATA,
 
 	/* Copy from include/uapi/linux/mptcp.h.
 	 * These reset fields will not be changed since they adhere to
@@ -140,6 +152,12 @@ sk_rst_convert_drop_reason(enum skb_drop_reason reason)
 		return SK_RST_REASON_TCP_TOO_OLD_ACK;
 	case SKB_DROP_REASON_TCP_ACK_UNSENT_DATA:
 		return SK_RST_REASON_TCP_ACK_UNSENT_DATA;
+	case SKB_DROP_REASON_TCP_FLAGS:
+		return SK_RST_REASON_TCP_FLAGS;
+	case SKB_DROP_REASON_TCP_OLD_ACK:
+		return SK_RST_REASON_TCP_OLD_ACK;
+	case SKB_DROP_REASON_TCP_ABORT_ON_DATA:
+		return SK_RST_REASON_TCP_ABORT_ON_DATA;
 	default:
 		/* If we don't have our own corresponding reason */
 		return SK_RST_REASON_NOT_SPECIFIED;
Signature · 1 line
2.37.3
author
#5 lore raw

PATCH 4/5 tcp: handle timewait cases in rstreason logic

In reply to Jason Xing

From: Jason Xing <[email protected]>

There are two possible cases where TCP layer can send an RST. Since they happen in the same place, I think using one independent reason is enough to identify this special situation.

---
 include/net/rstreason.h | 5 +++++
 net/ipv4/tcp_ipv4.c     | 2 +-
 net/ipv6/tcp_ipv6.c     | 2 +-
 3 files changed, 7 insertions(+), 2 deletions(-)
include/net/rstreason.h +5 -0
index 61855d4b27e2..62e869089da4 100644
@@ -15,6 +15,7 @@
 	FN(TCP_FLAGS)			\
 	FN(TCP_OLD_ACK)			\
 	FN(TCP_ABORT_ON_DATA)		\
+	FN(TCP_TIMEWAIT_SOCKET)		\
 	FN(MPTCP_RST_EUNSPEC)		\
 	FN(MPTCP_RST_EMPTCP)		\
 	FN(MPTCP_RST_ERESOURCE)		\
@@ -69,6 +70,10 @@ enum sk_rst_reason {
 	 */
 	SK_RST_REASON_TCP_ABORT_ON_DATA,
 
+	/* Here start with the independent reasons */
+	/** @SK_RST_REASON_TCP_TIMEWAIT_SOCKET: happen on the timewait socket */
+	SK_RST_REASON_TCP_TIMEWAIT_SOCKET,
+
 	/* Copy from include/uapi/linux/mptcp.h.
 	 * These reset fields will not be changed since they adhere to
 	 * RFC 8684. So do not touch them. I'm going to list each definition
net/ipv4/tcp_ipv4.c +1 -1
index 0427deca3e0e..d35cdb77d7b5 100644
@@ -2421,7 +2421,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
 		tcp_v4_timewait_ack(sk, skb);
 		break;
 	case TCP_TW_RST:
-		tcp_v4_send_reset(sk, skb, sk_rst_convert_drop_reason(drop_reason));
+		tcp_v4_send_reset(sk, skb, SK_RST_REASON_TCP_TIMEWAIT_SOCKET);
 		inet_twsk_deschedule_put(inet_twsk(sk));
 		goto discard_it;
 	case TCP_TW_SUCCESS:;
net/ipv6/tcp_ipv6.c +1 -1
index 37201c4fb393..bee26b961835 100644
@@ -1999,7 +1999,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
 		tcp_v6_timewait_ack(sk, skb);
 		break;
 	case TCP_TW_RST:
-		tcp_v6_send_reset(sk, skb, sk_rst_convert_drop_reason(drop_reason));
+		tcp_v6_send_reset(sk, skb, SK_RST_REASON_TCP_TIMEWAIT_SOCKET);
 		inet_twsk_deschedule_put(inet_twsk(sk));
 		goto discard_it;
 	case TCP_TW_SUCCESS:
Signature · 1 line
2.37.3
author
#6 lore raw

PATCH 5/5 tcp: handle rstreason in tcp_check_req()

In reply to Jason Xing

From: Jason Xing <[email protected]>

We're going to send an RST due to invalid syn packet which is already checked whether 1) it is in sequence, 2) it is a retransmitted skb.

As RFC 793 says, if the state of socket is not CLOSED/LISTEN/SYN-SENT, then we should send an RST when receiving bad syn packet:
"fourth, check the SYN bit,...If the SYN is in the window it is an error, send a reset"

---
 include/net/rstreason.h  | 8 ++++++++
 net/ipv4/tcp_minisocks.c | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
include/net/rstreason.h +8 -0
index 62e869089da4..bfea05ff8d88 100644
@@ -16,6 +16,7 @@
 	FN(TCP_OLD_ACK)			\
 	FN(TCP_ABORT_ON_DATA)		\
 	FN(TCP_TIMEWAIT_SOCKET)		\
+	FN(INVALID_SYN)			\
 	FN(MPTCP_RST_EUNSPEC)		\
 	FN(MPTCP_RST_EMPTCP)		\
 	FN(MPTCP_RST_ERESOURCE)		\
@@ -73,6 +74,13 @@ enum sk_rst_reason {
 	/* Here start with the independent reasons */
 	/** @SK_RST_REASON_TCP_TIMEWAIT_SOCKET: happen on the timewait socket */
 	SK_RST_REASON_TCP_TIMEWAIT_SOCKET,
+	/**
+	 * @SK_RST_REASON_INVALID_SYN: receive bad syn packet
+	 * RFC 793 says if the state is not CLOSED/LISTEN/SYN-SENT then
+	 * "fourth, check the SYN bit,...If the SYN is in the window it is
+	 * an error, send a reset"
+	 */
+	SK_RST_REASON_INVALID_SYN,
 
 	/* Copy from include/uapi/linux/mptcp.h.
 	 * These reset fields will not be changed since they adhere to
net/ipv4/tcp_minisocks.c +1 -1
index 7d543569a180..b93619b2384b 100644
@@ -879,7 +879,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
 		 * avoid becoming vulnerable to outside attack aiming at
 		 * resetting legit local connections.
 		 */
-		req->rsk_ops->send_reset(sk, skb, SK_RST_REASON_NOT_SPECIFIED);
+		req->rsk_ops->send_reset(sk, skb, SK_RST_REASON_INVALID_SYN);
 	} else if (fastopen) { /* received a valid RST pkt */
 		reqsk_fastopen_remove(sk, req, true);
 		tcp_reset(sk, skb);
Signature · 1 line
2.37.3