From ed999b65aac44fcb68fc533e8bd5a23cf2d09e7c Mon Sep 17 00:00:00 2001
From: wuchangsheng <wuchangsheng2@huawei.com>
Date: Wed, 26 May 2021 19:09:41 +0800
Subject: [PATCH] fix-error-of-deleting-conn-table-in-connect

---
 src/include/lwip/priv/tcp_priv.h | 42 ++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/include/lwip/priv/tcp_priv.h b/src/include/lwip/priv/tcp_priv.h
index 192edc4..599289f 100644
--- a/src/include/lwip/priv/tcp_priv.h
+++ b/src/include/lwip/priv/tcp_priv.h
@@ -358,6 +358,28 @@ static inline int vdev_reg_done(enum reg_ring_type reg_type, const struct tcp_pc
 
   return vdev_reg_xmit(reg_type, &qtuple);
 }
+
+/* TCP_RMV pcb whether to call vdev_reg_xmit to reg conn-sock table.
+   fix the error of adding conn table in connect func and deleting conn table 
+   when moving pcb from tcp_bound_pcbs to tcp_listen_pcbs  */
+static inline int need_vdev_reg(struct tcp_pcb *pcb_list, const struct tcp_pcb *pcb)
+{
+  /* tw_pcbs_list and tcp_listen_pcbs will not change pcb to other list always reg */
+  if ((pcb_list == tcp_tw_pcbs) || (pcb_list == tcp_listen_pcbs.pcbs)) {
+    return 1;
+  }
+
+  /* tcp_active_pcbs in FIN_WAIT_1,FIN_WAIT_2,CLOSING state will change pcb to tw_pcbs_list don't reg.
+     detail info see func tcp_process in tcp_in.c */
+  if (pcb_list == tcp_active_pcbs) {
+    if ((pcb->state != FIN_WAIT_1) && (pcb->state != FIN_WAIT_2) && (pcb->state != CLOSING)) {
+      return 1;
+    }
+  }
+
+  /* tcp_bound_pcbs and others don't reg */
+  return 0;
+}
 #endif
 
 /* Axioms about the above lists:
@@ -392,10 +414,12 @@ static inline int vdev_reg_done(enum reg_ring_type reg_type, const struct tcp_pc
               tcp_timer_needed(); \
                             } while(0)
 #define TCP_RMV(pcbs, npcb) do { \
-                            if (pcb->state == LISTEN)                         \
-                              vdev_reg_done(REG_RING_TCP_LISTEN_CLOSE, npcb); \
-                            else                                              \
-                              vdev_reg_done(REG_RING_TCP_CONNECT_CLOSE, npcb);\
+                            if (need_vdev_reg(*pcbs, npcb)) {               \
+                              if (npcb->state == LISTEN)                    \
+                                vdev_reg_done(REG_RING_TCP_LISTEN_CLOSE, npcb);  \
+                              else                                          \
+                                vdev_reg_done(REG_RING_TCP_CONNECT_CLOSE, npcb); \
+                            }                                               \
                             struct tcp_pcb *tcp_tmp_pcb; \
                             LWIP_ASSERT("TCP_RMV: pcbs != NULL", *(pcbs) != NULL); \
                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", (npcb), *(pcbs))); \
@@ -488,10 +512,12 @@ static inline int vdev_reg_done(enum reg_ring_type reg_type, const struct tcp_pc
 
 #define TCP_RMV(pcbs, npcb)                        \
   do {                                             \
-    if (pcb->state == LISTEN)                         \
-      vdev_reg_done(REG_RING_TCP_LISTEN_CLOSE, npcb); \
-    else                                              \
-      vdev_reg_done(REG_RING_TCP_CONNECT_CLOSE, npcb);\
+    if (need_vdev_reg(*pcbs, npcb)) {              \
+      if (npcb->state == LISTEN)                   \
+        vdev_reg_done(REG_RING_TCP_LISTEN_CLOSE, npcb); \
+      else                                         \
+        vdev_reg_done(REG_RING_TCP_CONNECT_CLOSE, npcb);\
+    }                                              \
     if(*(pcbs) == (npcb)) {                        \
       (*(pcbs)) = (*pcbs)->next;                   \
       if (*pcbs)                                   \
-- 
2.23.0

