Once an MPTCP socket has received a DATA_FIN, recv() returns the remaining data, then EOF. But if the connection was then reset, e.g. by an MP_FASTCLOSE, recv() returned an error after the data, e.g. EPIPE, instead of EOF as TCP does. A proxy resetting the other connection on read errors then dropped the data it still had to forward. As TCP does with a FIN, set SOCK_DONE when processing the DATA_FIN, and check it before the socket error in recvmsg(). With an MP_FASTCLOSE, the data still in the backlog is moved first, not to miss the DATA_FIN. After a fallback, a DATA_FIN is also emulated after a reset: only set SOCK_DONE after a FIN, not to hide a reset behind an EOF. This FIN is cleared when the first subflow is disconnected: emulate this DATA_FIN before scheduling the disconnection, and process it before disconnecting. As with TCP, splice() then returns EOF too, and the error after an MP_FASTCLOSE depends on the state reached with the DATA_FIN, e.g. EPIPE in CLOSE_WAIT. This was reported on GitHub, and is validated with new Packetdrill tests: data and a DATA_FIN are received, then an MP_FASTCLOSE; and after a fallback, data, a FIN and a reset. Without this patch, the second read() returned EPIPE in both cases: receive_fastclose_after_data_fin.pkt:27: runtime error in read call: Expected result 0 but got -1 with errno 32 (Broken pipe) v1_connect_tcpfallback_fin_reset.pkt:19: runtime error in read call: Expected result 0 but got -1 with errno 32 (Broken pipe) Note for the backports: the issue with an MP_FASTCLOSE comes from commit 69800e516e96 ("mptcp: propagate fastclose error"), in v6.1. Before that, the modification in mptcp_check_fastclose() is not needed. Fixes: 15cc10453398 ("mptcp: deliver ssk errors to msk") Cc: stable@vger.kernel.org Link: https://github.com/multipath-tcp/mptcp_net-next/issues/647 Reviewed-by: Mat Martineau Acked-by: Geliang Tang Signed-off-by: Matthieu Baerts (NGI0) --- net/mptcp/protocol.c | 31 +++++++++++++++++++++++++++++-- net/mptcp/subflow.c | 7 ++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index fc8aad7e2fde..1f045348764a 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -707,6 +707,10 @@ static void mptcp_check_data_fin(struct sock *sk) WRITE_ONCE(msk->rcv_data_fin, 0); WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); + /* on fallback, also emulated after a reset: check for a FIN */ + if (!__mptcp_check_fallback(msk) || + (msk->first && sock_flag(msk->first, SOCK_DONE))) + sock_set_flag(sk, SOCK_DONE); smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ switch (sk->sk_state) { @@ -934,6 +938,9 @@ static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk) * destroy the msk as needed. */ ssk_state = inet_sk_state_load(ssk); + /* a FIN received before still leads to EOF, as with TCP */ + if (sock_flag(ssk, SOCK_DONE) && mptcp_pending_data_fin(sk, NULL)) + sock_set_flag(sk, SOCK_DONE); if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD)) mptcp_set_state(sk, ssk_state); WRITE_ONCE(sk->sk_err, -err); @@ -2437,6 +2444,9 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, !timeo) break; } else { + if (sock_flag(sk, SOCK_DONE)) + break; + copied = sock_error(sk); if (copied) break; @@ -2756,6 +2766,8 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, void mptcp_close_ssk(struct sock *sk, struct sock *ssk, struct mptcp_subflow_context *subflow) { + struct mptcp_sock *msk = mptcp_sk(sk); + /* The first subflow can already be closed or disconnected */ if (subflow->close_event_done || READ_ONCE(subflow->local_id) < 0) return; @@ -2763,12 +2775,21 @@ void mptcp_close_ssk(struct sock *sk, struct sock *ssk, subflow->close_event_done = true; if (sk->sk_state == TCP_ESTABLISHED) - mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); + mptcp_event(MPTCP_EVENT_SUB_CLOSED, msk, ssk, GFP_KERNEL); /* subflow aborted before reaching the fully_established status * attempt the creation of the next subflow */ - mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow); + mptcp_pm_subflow_check_next(msk, subflow); + + /* On fallback, the FIN of the first subflow is cleared when it is + * disconnected: process the DATA_FIN emulated from it before. + */ + if (ssk == msk->first && __mptcp_check_fallback(msk) && + READ_ONCE(msk->rcv_data_fin) && !sock_flag(sk, SOCK_DEAD)) { + mptcp_move_skbs(sk); + mptcp_check_data_fin(sk); + } __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); } @@ -2881,6 +2902,11 @@ static void mptcp_check_fastclose(struct mptcp_sock *msk) if (likely(!READ_ONCE(msk->rcv_fastclose))) return; + /* a DATA_FIN received before still leads to EOF, as with TCP */ + if (READ_ONCE(msk->rcv_data_fin)) + mptcp_move_skbs(sk); + mptcp_check_data_fin(sk); + mptcp_token_destroy(msk); mptcp_for_each_subflow_safe(msk, subflow, tmp) { @@ -3740,6 +3766,7 @@ static int mptcp_disconnect(struct sock *sk, int flags) atomic64_set(&msk->rcv_wnd_sent, 0); WRITE_ONCE(sk->sk_shutdown, 0); + sock_reset_flag(sk, SOCK_DONE); sk_error_report(sk); return 0; } diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c index 054ee992904e..3c2aff563575 100644 --- a/net/mptcp/subflow.c +++ b/net/mptcp/subflow.c @@ -1379,9 +1379,6 @@ static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ss if (!skb_queue_empty(&ssk->sk_receive_queue)) return; - if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) - mptcp_schedule_work(sk); - /* when the fallback subflow closes the rx side, trigger a 'dummy' * ingress data fin, so that the msk state will follow along */ @@ -1390,6 +1387,10 @@ static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ss mptcp_update_rcv_data_fin(msk, subflow->map_seq + subflow->map_data_len, true)) mptcp_schedule_work(sk); + + /* after the 'dummy' data fin: mptcp_close_ssk() checks it */ + if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) + mptcp_schedule_work(sk); } static bool mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk) -- 2.55.0