Subject: [PATCH 21/21] tcp: allow MSG_ZEROCOPY transmission also in CLOSE_WAIT state

From: Willem de Bruijn <[email-masked]>
Date: Thu, 10 Jan 2019 14:40:33 -0500

[ Upstream commit 13d7f46386e060df31b727c9975e38306fa51e7a ]

TCP transmission with MSG_ZEROCOPY fails if the peer closes its end of
the connection and so transitions this socket to CLOSE_WAIT state.

Transmission in close wait state is acceptable. Other similar tests in
the stack (e.g., in FastOpen) accept both states. Relax this test, too.

Link: https://www.mail-archive.com/[email-masked]/msg276886.html
Link: https://www.mail-archive.com/[email-masked]/msg227390.html
Fixes: f214f915e7db ("tcp: enable MSG_ZEROCOPY")
Reported-by: Marek Majkowski <[email-masked]>
Signed-off-by: Willem de Bruijn <[email-masked]>
CC: Yuchung Cheng <[email-masked]>
CC: Neal Cardwell <[email-masked]>
CC: Soheil Hassas Yeganeh <[email-masked]>
CC: Alexey Kodanev <[email-masked]>
Acked-by: Soheil Hassas Yeganeh <[email-masked]>
Reviewed-by: Eric Dumazet <[email-masked]>
Signed-off-by: David S. Miller <[email-masked]>
---
 net/ipv4/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 9e6bc4d6daa7..40cbe5609663 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -1186,7 +1186,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 	flags = msg->msg_flags;
 
 	if (flags & MSG_ZEROCOPY && size && sock_flag(sk, SOCK_ZEROCOPY)) {
-		if (sk->sk_state != TCP_ESTABLISHED) {
+		if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
 			err = -EINVAL;
 			goto out_err;
 		}
-- 
2.20.1