In tcp_tap_handler(), the TAP_SYN_RCVD branch handles the segment
completing the handshake from the guest, but it also handles a FIN in
the same segment on its own, without checking that its sequence is the
expected one, or that the segment carries no data: on a FIN, we
increase seq_from_tap, shut down the socket for writing, send an ACK,
and return.
If the FIN is out of order, for example because it overtook the data
preceding it, we acknowledge data we never received, the server gets
an EOF without any data, and once the data arrives, we reset the
connection. If the FIN comes together with data, the data is lost. A
FIN without ACK is also accepted, as we only check for the ACK flag
afterwards. In both cases, we don't set TAP_FIN_RCVD either.
Further, if the segment is the last one in a batch, we return right
after processing the handshake, without looking at any data it might
carry: the data is neither forwarded nor acknowledged, and the guest
needs to retransmit it.
Check for the ACK flag before considering the connection established,
and only return early for a pure ACK: otherwise, let
tcp_data_from_tap() handle data and FIN, which already checks that the
FIN is in sequence, and sets TAP_FIN_RCVD, so that we shut down the
socket once all the data was forwarded.
With the reproducer attached to the bug report, which opens 100
connections through pasta with reordering on the tap interface, 34
connections end up with an empty EOF without this change, and none
with it.
Link: https://bugs.passt.top/show_bug.cgi?id=226
Reported-by: Zhenrong (Daniel) Gu