Created attachment 173093 [details]
tcp state diagram for quick reference
1) For TCPS_SYN_SENT:
As per TCP state diagram, TCPS_SYN_SENT should switch to TCPS_CLOSED state on appl:close call or timeout. In current code, at SYN_SENT state, a FIN message is initiated instead of call to tcp_close() to release PCB and TCP control block, which leads to incorrect states.
To fix the issue, "case TCPS_SYN_SENT:" must be shifted with the case "case TCPS_LISTEN:".
2) For TCPS_SYN_RECEIVED:
As per TCP state diagram, on appl:close call, if we are at TCPS_SYN_RECEIVED state then we just need to send FIN and switch to FIN_WAIT_1 state. In current code, we are not switching to TCPS_FIN_WAIT_1 state.
To fix this issue, "break;" statement should be removed in the case "case TCPS_SYN_RECEIVED:" so that it falls through "case TCPS_ESTABLISHED:" where we are changing state to TCPS_FIN_WAIT_1.
tp->t_flags |= TF_NEEDFIN;
CCing Michael as he has fixed some state transition bugs in TCP lately.
Michael, would you be able to take a look? If not, let me know and I'll try to do it.
I can take care of it.