net: tcp: Set errno properly if connecting to non listening port

If we try to connect to a port which no socket is listening to,
we will get a packet with "ACK | RST" flags set. In this case
the errno should be ECONNREFUSED instead of ETIMEDOUT like we
used to return earlier.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2023-10-18 14:19:45 +03:00 committed by Chris Friedt
commit ec4973dd15

View file

@ -2417,6 +2417,18 @@ static enum net_verdict tcp_in(struct tcp *conn, struct net_pkt *pkt)
net_stats_update_tcp_seg_rst(net_pkt_iface(pkt));
do_close = true;
close_status = -ECONNRESET;
/* If we receive RST and ACK for the sent SYN, it means
* that there is no socket listening the port we are trying
* to connect to. Set the errno properly in this case.
*/
if (conn->in_connect) {
fl = th_flags(th);
if (FL(&fl, ==, RST | ACK)) {
close_status = -ECONNREFUSED;
}
}
goto out;
}