Skip to content

Commit 8e2b8a9

Browse files
dcarattidavem330
authored andcommitted
mptcp: don't overwrite sock_ops in mptcp_is_tcpsk()
Eric Dumazet suggests: > The fact that mptcp_is_tcpsk() was able to write over sock->ops was a > bit strange to me. > mptcp_is_tcpsk() should answer a question, with a read-only argument. re-factor code to avoid overwriting sock_ops inside that function. Also, change the helper name to reflect the semantics and to disambiguate from its dual, sk_is_mptcp(). While at it, collapse mptcp_stream_accept() and mptcp_accept() into a single function, where fallback / non-fallback are separated into a single sk_is_mptcp() conditional. Link: multipath-tcp/mptcp_net-next#432 Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Davide Caratti <[email protected]> Acked-by: Paolo Abeni <[email protected]> Signed-off-by: Matthieu Baerts <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7961ef1 commit 8e2b8a9

File tree

1 file changed

+44
-64
lines changed

1 file changed

+44
-64
lines changed

net/mptcp/protocol.c

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,14 @@ static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
5555
return READ_ONCE(msk->wnd_end);
5656
}
5757

58-
static bool mptcp_is_tcpsk(struct sock *sk)
58+
static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
5959
{
60-
struct socket *sock = sk->sk_socket;
61-
62-
if (unlikely(sk->sk_prot == &tcp_prot)) {
63-
/* we are being invoked after mptcp_accept() has
64-
* accepted a non-mp-capable flow: sk is a tcp_sk,
65-
* not an mptcp one.
66-
*
67-
* Hand the socket over to tcp so all further socket ops
68-
* bypass mptcp.
69-
*/
70-
WRITE_ONCE(sock->ops, &inet_stream_ops);
71-
return true;
7260
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
73-
} else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
74-
WRITE_ONCE(sock->ops, &inet6_stream_ops);
75-
return true;
61+
if (sk->sk_prot == &tcpv6_prot)
62+
return &inet6_stream_ops;
7663
#endif
77-
}
78-
79-
return false;
64+
WARN_ON_ONCE(sk->sk_prot != &tcp_prot);
65+
return &inet_stream_ops;
8066
}
8167

8268
static int __mptcp_socket_create(struct mptcp_sock *msk)
@@ -3258,44 +3244,6 @@ void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
32583244
WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
32593245
}
32603246

3261-
static struct sock *mptcp_accept(struct sock *ssk, int flags, int *err,
3262-
bool kern)
3263-
{
3264-
struct sock *newsk;
3265-
3266-
pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk));
3267-
newsk = inet_csk_accept(ssk, flags, err, kern);
3268-
if (!newsk)
3269-
return NULL;
3270-
3271-
pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk));
3272-
if (sk_is_mptcp(newsk)) {
3273-
struct mptcp_subflow_context *subflow;
3274-
struct sock *new_mptcp_sock;
3275-
3276-
subflow = mptcp_subflow_ctx(newsk);
3277-
new_mptcp_sock = subflow->conn;
3278-
3279-
/* is_mptcp should be false if subflow->conn is missing, see
3280-
* subflow_syn_recv_sock()
3281-
*/
3282-
if (WARN_ON_ONCE(!new_mptcp_sock)) {
3283-
tcp_sk(newsk)->is_mptcp = 0;
3284-
goto out;
3285-
}
3286-
3287-
newsk = new_mptcp_sock;
3288-
MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3289-
} else {
3290-
MPTCP_INC_STATS(sock_net(ssk),
3291-
MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3292-
}
3293-
3294-
out:
3295-
newsk->sk_kern_sock = kern;
3296-
return newsk;
3297-
}
3298-
32993247
void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
33003248
{
33013249
struct mptcp_subflow_context *subflow, *tmp;
@@ -3739,7 +3687,6 @@ static struct proto mptcp_prot = {
37393687
.connect = mptcp_connect,
37403688
.disconnect = mptcp_disconnect,
37413689
.close = mptcp_close,
3742-
.accept = mptcp_accept,
37433690
.setsockopt = mptcp_setsockopt,
37443691
.getsockopt = mptcp_getsockopt,
37453692
.shutdown = mptcp_shutdown,
@@ -3849,18 +3796,36 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
38493796
if (!ssk)
38503797
return -EINVAL;
38513798

3852-
newsk = mptcp_accept(ssk, flags, &err, kern);
3799+
pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk));
3800+
newsk = inet_csk_accept(ssk, flags, &err, kern);
38533801
if (!newsk)
38543802
return err;
38553803

3856-
lock_sock(newsk);
3857-
3858-
__inet_accept(sock, newsock, newsk);
3859-
if (!mptcp_is_tcpsk(newsock->sk)) {
3860-
struct mptcp_sock *msk = mptcp_sk(newsk);
3804+
pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk));
3805+
if (sk_is_mptcp(newsk)) {
38613806
struct mptcp_subflow_context *subflow;
3807+
struct sock *new_mptcp_sock;
3808+
3809+
subflow = mptcp_subflow_ctx(newsk);
3810+
new_mptcp_sock = subflow->conn;
3811+
3812+
/* is_mptcp should be false if subflow->conn is missing, see
3813+
* subflow_syn_recv_sock()
3814+
*/
3815+
if (WARN_ON_ONCE(!new_mptcp_sock)) {
3816+
tcp_sk(newsk)->is_mptcp = 0;
3817+
goto tcpfallback;
3818+
}
3819+
3820+
newsk = new_mptcp_sock;
3821+
MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3822+
3823+
newsk->sk_kern_sock = kern;
3824+
lock_sock(newsk);
3825+
__inet_accept(sock, newsock, newsk);
38623826

38633827
set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3828+
msk = mptcp_sk(newsk);
38643829
msk->in_accept_queue = 0;
38653830

38663831
/* set ssk->sk_socket of accept()ed flows to mptcp socket.
@@ -3882,6 +3847,21 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
38823847
if (unlikely(list_is_singular(&msk->conn_list)))
38833848
inet_sk_state_store(newsk, TCP_CLOSE);
38843849
}
3850+
} else {
3851+
MPTCP_INC_STATS(sock_net(ssk),
3852+
MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3853+
tcpfallback:
3854+
newsk->sk_kern_sock = kern;
3855+
lock_sock(newsk);
3856+
__inet_accept(sock, newsock, newsk);
3857+
/* we are being invoked after accepting a non-mp-capable
3858+
* flow: sk is a tcp_sk, not an mptcp one.
3859+
*
3860+
* Hand the socket over to tcp so all further socket ops
3861+
* bypass mptcp.
3862+
*/
3863+
WRITE_ONCE(newsock->sk->sk_socket->ops,
3864+
mptcp_fallback_tcp_ops(newsock->sk));
38853865
}
38863866
release_sock(newsk);
38873867

0 commit comments

Comments
 (0)