Skip to contentSkip to conversation

mptcp: [try to] fix armegaddon on late tcp fallback

RFC PATCH In review Paolo Abeni 1 participant 7 messages Dec 13, 2019

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

cover mptcp: [try to] fix armegaddon on late tcp fallback

Passive connection can fallback to TCP after that the subflow is established. That leads to disaster in several nasty ways.

Address the issue explicitly falling back to TCP (converting the socket) as soon as we notice the unsuccessful MPC hanshake completion.

Sent as a very early RFC - completely untested yet.

Paolo Abeni (4):
mptcp: add helper to forcefully fallback the msk socket to TCP
mptcp: cache the first subflow sock
mptcp: late fallback support
mptcp: deal with fallback in additional places.

 net/mptcp/protocol.c | 92 +++++++++++++++++++++++++++++++++++++-------
 net/mptcp/protocol.h |  1 +
 2 files changed, 79 insertions(+), 14 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

RFC PATCH 1/4 mptcp: add helper to forcefully fallback the msk socket to TCP

will be used in later patch

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +39 -3
index c15232461a61..b6d753ed8397 100644
@@ -24,6 +24,38 @@
 
 #define MPTCP_SAME_STATE TCP_MAX_STATES
 
+static void __mptcp_close(struct sock *sk, long timeout);
+
+/* MP_CAPABLE handshake failed, convert msk to plain tcp, replacing
+ * socket->sk and stream ops and destroying msk
+ * return the msk socket, as we can't access msk anymore after this function
+ * completes
+ */
+static struct socket *__mptcp_fallback_to_tcp(struct mptcp_sock *msk,
+					      struct sock *ssk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct socket *sock;
+	struct sock *sk;
+
+	sk = (struct sock *)msk;
+	sock = sk->sk_socket;
+	subflow = mptcp_subflow_ctx(ssk);
+
+	/* detach the msk socket */
+	list_del_init(&subflow->node);
+	sock_orphan(sk);
+
+	/* socket is now TCP */
+	sock_graft(ssk, sock);
+	sock->ops = sk->sk_family == AF_INET6 ? &inet6_stream_ops :
+						&inet_stream_ops;
+
+	/* destroy the left-over msk sock */
+	__mptcp_close(sk, 0);
+	return sock;
+}
+
 /* if msk has a single subflow socket, and the mp_capable handshake is not
  * completed yet or has failed - that is, the socket is Not MP Capable,
  * returns it.
@@ -561,7 +593,7 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 	release_sock(ssk);
 }
 
-static void mptcp_close(struct sock *sk, long timeout)
+static void __mptcp_close(struct sock *sk, long timeout)
 {
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -569,8 +601,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 	mptcp_token_destroy(msk->token);
 	inet_sk_state_store(sk, TCP_CLOSE);
 
-	lock_sock(sk);
-
 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
 
@@ -583,6 +613,12 @@ static void mptcp_close(struct sock *sk, long timeout)
 	sk_common_release(sk);
 }
 
+static void mptcp_close(struct sock *sk, long timeout)
+{
+	lock_sock(sk);
+	__mptcp_close(sk, timeout);
+}
+
 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
 {
 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
Signature · 1 line
2.21.0
author
#3 lore raw

RFC PATCH 2/4 mptcp: cache the first subflow sock

In reply to Paolo Abeni

so that we can easily check for TCP fallback

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 6 +++++-
 net/mptcp/protocol.h | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +5 -1
index b6d753ed8397..1d21b64b2149 100644
@@ -87,7 +87,7 @@ static struct socket *__mptcp_tcp_fallback(const struct mptcp_sock *msk)
 
 static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
 {
-	return ((struct sock *)msk)->sk_state == TCP_CLOSE;
+	return !msk->first;
 }
 
 static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
@@ -108,6 +108,7 @@ static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
 	if (err)
 		return ERR_PTR(err);
 
+	msk->first = ssock->sk;
 	msk->subflow = ssock;
 	subflow = mptcp_subflow_ctx(ssock->sk);
 	list_add(&subflow->node, &msk->conn_list);
@@ -559,6 +560,8 @@ static int __mptcp_init_sock(struct sock *sk)
 	INIT_LIST_HEAD(&msk->conn_list);
 	__set_bit(MPTCP_SEND_SPACE, &msk->flags);
 
+	msk->first = NULL;
+
 	return 0;
 }
 
@@ -688,6 +691,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		msk->local_key = subflow->local_key;
 		msk->token = subflow->token;
 		msk->subflow = NULL;
+		msk->first = newsk;
 
 		mptcp_token_update_accept(newsk, new_mptcp_sock);
 
net/mptcp/protocol.h +1 -0
index 0e5ba5ac1443..b4cf88258b6b 100644
@@ -73,6 +73,7 @@ struct mptcp_sock {
 	struct list_head conn_list;
 	struct skb_ext	*cached_ext;	/* for the next sendmsg */
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
+	struct sock	*first;
 };
 
 #define mptcp_for_each_subflow(__msk, __subflow)			\
Signature · 1 line
2.21.0
author
#4 lore raw

RFC PATCH 3/4 mptcp: late fallback support

In reply to Paolo Abeni

passive connection can fallback tcp, too. Additionally it can happens after that the subflow become established:

syn+ MP_CAPABLE ->
<- syn, ack + MP_CAPABLE

ack, seq = 3 ->

        // OoO packet is accepted because in-sequence
        // passive socket is created, is in ESTABLISHED
	// status and tentatively as MP_CAPABLE

ack, seq = 2 ->

        // no MP_CAPABLE opt, subflow should fallback to TCP

Currently that will lead to disaster, in several nasty ways.

Deal with that "transitioning" the socket to tcp, replacing socket->sk with the tcp subflow and rearranging all the link, than calling the relevant operation on the now-tcp socket

Co-developed-by Florian Westphal <fw(a)strlen.de>
Signed-off-by Florian Westphal <fw(a)strlen.de>
Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
net/mptcp/protocol.c +18 -8
index 1d21b64b2149..999b1f89b3d6 100644
@@ -69,20 +69,32 @@ static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
 	return msk->subflow;
 }
 
-/* if msk has a single subflow, and the mp_capable handshake is failed,
+static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk)
+{
+	return msk->first && !tcp_sk(msk->first)->is_mptcp;
+}
+
+/* if the mp_capable handshake is failed, return a tcp socket
  * return it.
  * Otherwise returns NULL
  */
-static struct socket *__mptcp_tcp_fallback(const struct mptcp_sock *msk)
+static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
 {
-	struct socket *ssock = __mptcp_nmpc_socket(msk);
-
 	sock_owned_by_me((const struct sock *)msk);
 
-	if (!ssock || tcp_sk(ssock->sk)->is_mptcp)
+	if (!likely(__mptcp_needs_tcp_fallback(msk)))
 		return NULL;
 
-	return ssock;
+	if (msk->subflow) {
+		/* the first subflow is an active connection, discart the
+		 * paired socket
+		 */
+		msk->subflow->sk = NULL;
+		sock_release(msk->subflow);
+		msk->subflow = NULL;
+	}
+
+	return __mptcp_fallback_to_tcp(msk, msk->first);
 }
 
 static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
@@ -301,7 +313,6 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	if (ssock) {
 		pr_debug("fallback passthrough");
 		ret = sock_sendmsg(ssock, msg);
-		release_sock(sk);
 		return ret;
 	}
 
@@ -404,7 +415,6 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		pr_debug("fallback-read subflow=%p",
 			 mptcp_subflow_ctx(ssock->sk));
 		copied = sock_recvmsg(ssock, msg, flags);
-		release_sock(sk);
 		return copied;
 	}
 
Signature · 1 line
2.21.0
author
#5 lore raw

RFC PATCH 4/4 mptcp: deal with fallback in additional places.

In reply to Paolo Abeni

Everytime the msk socket ops block, a passive connection can complete with failure the MP_CAPABLE handshake, requiring transitioning to TCP.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com
---
 net/mptcp/protocol.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +16 -2
index 999b1f89b3d6..04cbfca8966c 100644
@@ -200,6 +200,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		ret = sk_stream_wait_memory(ssk, timeo);
 		if (ret)
 			return ret;
+		if (unlikely(__mptcp_needs_tcp_fallback(msk)))
+			return 0;
 	}
 
 	/* compute copy limit */
@@ -311,9 +313,10 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	lock_sock(sk);
 	ssock = __mptcp_tcp_fallback(msk);
 	if (ssock) {
+fallback:
 		pr_debug("fallback passthrough");
 		ret = sock_sendmsg(ssock, msg);
-		return ret;
+		return ret >= 0 ? ret + copied : (copied ? copied : ret);
 	}
 
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
@@ -332,6 +335,11 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 					 &size_goal);
 		if (ret < 0)
 			break;
+		if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) {
+			release_sock(ssk);
+			ssock = __mptcp_tcp_fallback(msk);
+			goto fallback;
+		}
 
 		copied += ret;
 	}
@@ -412,6 +420,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	lock_sock(sk);
 	ssock = __mptcp_tcp_fallback(msk);
 	if (ssock) {
+fallback:
 		pr_debug("fallback-read subflow=%p",
 			 mptcp_subflow_ctx(ssock->sk));
 		copied = sock_recvmsg(ssock, msg, flags);
@@ -518,6 +527,8 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		pr_debug("block timeout %ld", timeo);
 		wait_data = true;
 		mptcp_wait_data(sk, &timeo);
+		if (unlikely(__mptcp_tcp_fallback(msk)))
+			goto fallback;
 	}
 
 	if (more_data_avail) {
@@ -1057,8 +1068,8 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 			   struct poll_table_struct *wait)
 {
-	const struct mptcp_sock *msk;
 	struct sock *sk = sock->sk;
+	struct mptcp_sock *msk;
 	struct socket *ssock;
 	__poll_t mask = 0;
 
@@ -1074,6 +1085,9 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 	release_sock(sk);
 	sock_poll_wait(file, sock, wait);
 	lock_sock(sk);
+	ssock = __mptcp_tcp_fallback(msk);
+	if (unlikely(ssock))
+		return ssock->ops->poll(file, ssock, NULL);
 
 	if (test_bit(MPTCP_DATA_READY, &msk->flags))
 		mask = EPOLLIN | EPOLLRDNORM;
Signature · 1 line
2.21.0
author
#6 lore raw
In reply to Paolo Abeni
Quoted 14 lines · Paolo Abeni

Passive connection can fallback to TCP after that the subflow is established. That leads to disaster in several nasty ways.

Address the issue explicitly falling back to TCP (converting the socket) as soon as we notice the unsuccessful MPC hanshake completion.

Sent as a very early RFC - completely untested yet.

Paolo Abeni (4):
mptcp: add helper to forcefully fallback the msk socket to TCP
mptcp: cache the first subflow sock
mptcp: late fallback support
mptcp: deal with fallback in additional places.

Quoted 3 lines
 net/mptcp/protocol.c | 92 +++++++++++++++++++++++++++++++++++++-------
 net/mptcp/protocol.h |  1 +
 2 files changed, 79 insertions(+), 14 deletions(-)

Addendum: the code is split in several patches to hopefully simplify the review/feedback. Iif there is agreement on that, idea would be squashing these 4 patches in a single one and append it to part 3 (v1 support), as the issue fixed here arise only with MPTCP v1.

Thanks,

Paolo

author
#2 lore raw

[MPTCP] Re: [RFC PATCH 1/4] mptcp: add helper to forcefully fallback the msk socket to TCP

In reply to Paolo Abeni
Quoted 26 lines · Paolo Abeni

will be used in later patch

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +11 -0
index c15232461a61..b6d753ed8397 100644
@@ -24,6 +24,38 @@
 
 #define MPTCP_SAME_STATE TCP_MAX_STATES
 
+static void __mptcp_close(struct sock *sk, long timeout);
+
+/* MP_CAPABLE handshake failed, convert msk to plain tcp, replacing
+ * socket->sk and stream ops and destroying msk
+ * return the msk socket, as we can't access msk anymore after this function
+ * completes
+ */
+static struct socket *__mptcp_fallback_to_tcp(struct mptcp_sock *msk,
+					      struct sock *ssk)
+{
+	struct mptcp_subflow_context *subflow;

net/mptcp/protocol.c line 47

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+	struct socket *sock;
+	struct sock *sk;
+
+	sk = (struct sock *)msk;
+	sock = sk->sk_socket;
+	subflow = mptcp_subflow_ctx(ssk);
+
+	/* detach the msk socket */
+	list_del_init(&subflow->node);
+	sock_orphan(sk);

Here we need an additional

	sock->sk = NULL;

to avoid a later WARN_ON(parent->sk); in sock_graft()

Cheers,

Paolo