tcp: fix fin in syn-rcvd
[vpp.git] / src / vnet / tcp / tcp.c
index a3cd1f3..b1ed99a 100644 (file)
@@ -246,7 +246,7 @@ tcp_connection_del (tcp_connection_t * tc)
 }
 
 tcp_connection_t *
-tcp_connection_new (u8 thread_index)
+tcp_connection_alloc (u8 thread_index)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
   tcp_connection_t *tc;
@@ -258,6 +258,15 @@ tcp_connection_new (u8 thread_index)
   return tc;
 }
 
+void
+tcp_connection_free (tcp_connection_t * tc)
+{
+  tcp_main_t *tm = &tcp_main;
+  pool_put (tm->connections[tc->c_thread_index], tc);
+  if (CLIB_DEBUG > 0)
+    clib_memset (tc, 0xFA, sizeof (*tc));
+}
+
 /** Notify session that connection has been reset.
  *
  * Switch state to closed and wait for session to call cleanup.
@@ -281,22 +290,21 @@ tcp_connection_reset (tcp_connection_t * tc)
       tcp_connection_timers_reset (tc);
       /* Set the cleanup timer, in case the session layer/app don't
        * cleanly close the connection */
-      tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
+      tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
       stream_session_reset_notify (&tc->connection);
       break;
     case TCP_STATE_CLOSE_WAIT:
     case TCP_STATE_FIN_WAIT_1:
     case TCP_STATE_FIN_WAIT_2:
     case TCP_STATE_CLOSING:
-      tc->state = TCP_STATE_CLOSED;
-      TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
       tcp_connection_timers_reset (tc);
-      tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
+      tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
       break;
     case TCP_STATE_CLOSED:
       return;
     }
   tc->state = TCP_STATE_CLOSED;
+  TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
 }
 
 /**
@@ -327,7 +335,7 @@ tcp_connection_close (tcp_connection_t * tc)
       tcp_connection_timers_reset (tc);
       tcp_send_fin (tc);
       tc->state = TCP_STATE_FIN_WAIT_1;
-      tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
+      tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_FINWAIT1_TIME);
       break;
     case TCP_STATE_ESTABLISHED:
       if (!session_tx_fifo_max_dequeue (&tc->connection))
@@ -335,6 +343,9 @@ tcp_connection_close (tcp_connection_t * tc)
       else
        tc->flags |= TCP_CONN_FINPNDG;
       tc->state = TCP_STATE_FIN_WAIT_1;
+      /* Set a timer in case the peer stops responding. Otherwise the
+       * connection will be stuck here forever. */
+      tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_FINWAIT1_TIME);
       break;
     case TCP_STATE_CLOSE_WAIT:
       if (!session_tx_fifo_max_dequeue (&tc->connection))
@@ -364,7 +375,7 @@ tcp_connection_close (tcp_connection_t * tc)
    * the session layer a chance to clear unhandled events */
   if (!tcp_timer_is_active (tc, TCP_TIMER_WAITCLOSE)
       && tc->state == TCP_STATE_CLOSED)
-    tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, 1);
+    tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
 }
 
 static void
@@ -1240,13 +1251,8 @@ tcp_timer_waitclose_handler (u32 conn_index)
 
   /* Session didn't come back with a close(). Send FIN either way
    * and switch to LAST_ACK. */
-  if (tc->state == TCP_STATE_CLOSE_WAIT)
+  if (tc->state == TCP_STATE_CLOSE_WAIT && (tc->flags & TCP_CONN_FINPNDG))
     {
-      if (tc->flags & TCP_CONN_FINSNT)
-       {
-         clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
-       }
-
       /* Make sure we don't try to send unsent data */
       tcp_connection_timers_reset (tc);
       tcp_cong_recovery_off (tc);
@@ -1260,6 +1266,14 @@ tcp_timer_waitclose_handler (u32 conn_index)
       /* Don't delete the connection yet */
       return;
     }
+  else if (tc->state == TCP_STATE_FIN_WAIT_1)
+    {
+      tcp_connection_timers_reset (tc);
+      tc->state = TCP_STATE_CLOSED;
+      /* Wait for session layer to clean up tx events */
+      tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
+      return;
+    }
 
   tcp_connection_del (tc);
 }