Skip to content

Commit 1772089

Browse files
committed
issue: 4398221 Fix connection closure on SYN_RCVD
When closing a connection in SYN_RCVD state, we should transition directly to CLOSED without sending FIN or RST packets, as specified in the RFC. This patch properly handles this case by immediately changing the state to CLOSED instead of going through the normal shutdown procedure. This prevents unnecessary control packets when a connection receives a SYN and is immediately closed without exchanging data. Signed-off-by: Tomer Cabouly <[email protected]>
1 parent 0d8f272 commit 1772089

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/core/lwip/tcp.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ static err_t tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
148148
{
149149
err_t err;
150150

151+
if (get_tcp_state(pcb) == SYN_RCVD) {
152+
// according to the RFC, in case we get a SYN and no more data
153+
// we should just close w/o FIN or RST
154+
tcp_pcb_purge(pcb);
155+
set_tcp_state(pcb, CLOSED);
156+
return ERR_OK;
157+
}
158+
151159
if (rst_on_unacked_data &&
152160
((get_tcp_state(pcb) == ESTABLISHED) || (get_tcp_state(pcb) == CLOSE_WAIT))) {
153161
if (pcb->rcv_wnd != pcb->rcv_wnd_max) {
@@ -194,12 +202,6 @@ static err_t tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
194202
tcp_pcb_remove(pcb);
195203
pcb = NULL;
196204
break;
197-
case SYN_RCVD:
198-
err = tcp_send_fin(pcb);
199-
if (err == ERR_OK) {
200-
set_tcp_state(pcb, FIN_WAIT_1);
201-
}
202-
break;
203205
case ESTABLISHED:
204206
err = tcp_send_fin(pcb);
205207
if (err == ERR_OK) {
@@ -258,7 +260,7 @@ err_t tcp_close(struct tcp_pcb *pcb)
258260
/* Set a flag not to receive any more data... */
259261
pcb->flags |= TF_RXCLOSED;
260262
}
261-
/* ... and close */
263+
262264
return tcp_close_shutdown(pcb, 1);
263265
}
264266

0 commit comments

Comments
 (0)