Kernel/Net: Send RST packet when socket receives in closed state

RFC9293 states that a closed socket should reply to all non-RST
packets with an RST. This change implements this behaviour as
specified in section 3.5.2 in bullet point 1.
This commit is contained in:
Tom Finet 2024-02-21 23:15:01 +00:00 committed by Andrew Kaster
parent 29ef508b4e
commit a7db718ffb
Notes: sideshowbarker 2024-07-18 08:59:31 +09:00

View File

@ -447,7 +447,12 @@ void handle_tcp(IPv4Packet const& ipv4_packet, UnixDateTime const& packet_timest
switch (socket->state()) {
case TCPSocket::State::Closed:
dbgln("handle_tcp: unexpected flags in Closed state ({:x}) for socket with tuple {}", tcp_packet.flags(), tuple.to_string());
// TODO: we may want to send an RST here, maybe as a configurable option
if (tcp_packet.has_rst()) {
return;
}
socket->set_sequence_number(tcp_packet.has_ack() ? tcp_packet.ack_number() : 0);
socket->set_ack_number(tcp_packet.sequence_number() + payload_size + 1);
(void)socket->send_tcp_packet(TCPFlags::RST | TCPFlags::ACK);
return;
case TCPSocket::State::TimeWait:
dbgln("handle_tcp: unexpected flags in TimeWait state ({:x}) for socket with tuple {}", tcp_packet.flags(), tuple.to_string());