[RFC PATCH] net: wget: Take in account tcp sequence number wrap around

Michael Trimarchi michael at amarulasolutions.com
Sun Jan 7 17:34:24 CET 2024


Coming from some discussion on mailing about wget unconsistent. It's
just and idea to play around

Signed-off-by: Michael Trimarchi <michael at amarulasolutions.com>
---

I'm on a train ;). No board to test

---
 include/net/tcp.h | 10 ++++++++++
 net/wget.c        |  8 ++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index c29d4ce24a..584813b637 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -88,6 +88,16 @@ struct ip_tcp_hdr {
 #define TCP_MSS		1460		/* Max segment size		*/
 #define TCP_SCALE	0x01		/* Scale			*/
 
+/*
+ * The next routines deal with comparing 32 bit unsigned ints
+ * and worry about wraparound (automatic with unsigned arithmetic).
+ */
+static inline bool before(__u32 seq1, __u32 seq2)
+{
+	return (__s32)(seq1-seq2) < 0;
+}
+#define after(seq2, seq1)	before(seq1, seq2)
+
 /**
  * struct tcp_mss - TCP option structure for MSS (Max segment size)
  * @kind: Field ID
diff --git a/net/wget.c b/net/wget.c
index 8bb4d72db1..99a688846f 100644
--- a/net/wget.c
+++ b/net/wget.c
@@ -260,8 +260,8 @@ static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
 					(phys_addr_t)(pkt_q[i].pkt),
 					pkt_q[i].len);
 				store_block(ptr1,
-					    pkt_q[i].tcp_seq_num -
-					    initial_data_seq_num,
+					    (unsigned int)((s32)pkt_q[i].tcp_seq_num -
+					    (s32)initial_data_seq_num),
 					    pkt_q[i].len);
 				unmap_sysmem(ptr1);
 				debug_cond(DEBUG_WGET,
@@ -333,8 +333,8 @@ static void wget_handler(uchar *pkt, u16 dport,
 			   "wget: Transferring, seq=%x, ack=%x,len=%x\n",
 			   tcp_seq_num, tcp_ack_num, len);
 
-		if (tcp_seq_num >= initial_data_seq_num &&
-		    store_block(pkt, tcp_seq_num - initial_data_seq_num,
+		if (!before(initial_data_seq_num, tcp_seq_num) &&
+		    store_block(pkt, (unsigned int)((s32)tcp_seq_num - (s32)initial_data_seq_num),
 				len) != 0) {
 			wget_fail("wget: store error\n",
 				  tcp_seq_num, tcp_ack_num, action);
-- 
2.40.1



More information about the U-Boot mailing list