Skip to content

Commit 7e8b88e

Browse files
Paolo Abenikuba-moo
authored andcommitted
mptcp: consolidate passive msk socket initialization
When the msk socket is cloned at MPC handshake time, a few fields are initialized in a racy way outside mptcp_sk_clone() and the msk socket lock. The above is due historical reasons: before commit a88d009 ("mptcp: simplify subflow_syn_recv_sock()") as the first subflow socket carrying all the needed date was not available yet at msk creation time We can now refactor the code moving the missing initialization bit under the socket lock, removing the init race and avoiding some code duplication. This will also simplify the next patch, as all msk->first write access are now under the msk socket lock. Fixes: 0397c6d ("mptcp: keep unaccepted MPC subflow into join list") Reviewed-by: Mat Martineau <martineau@kernel.org> Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Mat Martineau <martineau@kernel.org> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 5b82572 commit 7e8b88e

3 files changed

Lines changed: 33 additions & 38 deletions

File tree

net/mptcp/protocol.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@ static void mptcp_close(struct sock *sk, long timeout)
30383038
sock_put(sk);
30393039
}
30403040

3041-
void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3041+
static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
30423042
{
30433043
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
30443044
const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
@@ -3115,9 +3115,10 @@ static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
31153115
}
31163116
#endif
31173117

3118-
struct sock *mptcp_sk_clone(const struct sock *sk,
3119-
const struct mptcp_options_received *mp_opt,
3120-
struct request_sock *req)
3118+
struct sock *mptcp_sk_clone_init(const struct sock *sk,
3119+
const struct mptcp_options_received *mp_opt,
3120+
struct sock *ssk,
3121+
struct request_sock *req)
31213122
{
31223123
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
31233124
struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
@@ -3149,10 +3150,30 @@ struct sock *mptcp_sk_clone(const struct sock *sk,
31493150
msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
31503151

31513152
sock_reset_flag(nsk, SOCK_RCU_FREE);
3152-
/* will be fully established after successful MPC subflow creation */
3153-
inet_sk_state_store(nsk, TCP_SYN_RECV);
3154-
31553153
security_inet_csk_clone(nsk, req);
3154+
3155+
/* this can't race with mptcp_close(), as the msk is
3156+
* not yet exposted to user-space
3157+
*/
3158+
inet_sk_state_store(nsk, TCP_ESTABLISHED);
3159+
3160+
/* The msk maintain a ref to each subflow in the connections list */
3161+
WRITE_ONCE(msk->first, ssk);
3162+
list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3163+
sock_hold(ssk);
3164+
3165+
/* new mpc subflow takes ownership of the newly
3166+
* created mptcp socket
3167+
*/
3168+
mptcp_token_accept(subflow_req, msk);
3169+
3170+
/* set msk addresses early to ensure mptcp_pm_get_local_id()
3171+
* uses the correct data
3172+
*/
3173+
mptcp_copy_inaddrs(nsk, ssk);
3174+
mptcp_propagate_sndbuf(nsk, ssk);
3175+
3176+
mptcp_rcv_space_init(msk, ssk);
31563177
bh_unlock_sock(nsk);
31573178

31583179
/* note: the newly allocated socket refcount is 2 now */

net/mptcp/protocol.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ int mptcp_is_checksum_enabled(const struct net *net);
616616
int mptcp_allow_join_id0(const struct net *net);
617617
unsigned int mptcp_stale_loss_cnt(const struct net *net);
618618
int mptcp_get_pm_type(const struct net *net);
619-
void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk);
620619
void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
621620
const struct mptcp_options_received *mp_opt);
622621
bool __mptcp_retransmit_pending_data(struct sock *sk);
@@ -686,9 +685,10 @@ void __init mptcp_proto_init(void);
686685
int __init mptcp_proto_v6_init(void);
687686
#endif
688687

689-
struct sock *mptcp_sk_clone(const struct sock *sk,
690-
const struct mptcp_options_received *mp_opt,
691-
struct request_sock *req);
688+
struct sock *mptcp_sk_clone_init(const struct sock *sk,
689+
const struct mptcp_options_received *mp_opt,
690+
struct sock *ssk,
691+
struct request_sock *req);
692692
void mptcp_get_options(const struct sk_buff *skb,
693693
struct mptcp_options_received *mp_opt);
694694

net/mptcp/subflow.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -815,38 +815,12 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
815815
ctx->setsockopt_seq = listener->setsockopt_seq;
816816

817817
if (ctx->mp_capable) {
818-
ctx->conn = mptcp_sk_clone(listener->conn, &mp_opt, req);
818+
ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
819819
if (!ctx->conn)
820820
goto fallback;
821821

822822
owner = mptcp_sk(ctx->conn);
823-
824-
/* this can't race with mptcp_close(), as the msk is
825-
* not yet exposted to user-space
826-
*/
827-
inet_sk_state_store(ctx->conn, TCP_ESTABLISHED);
828-
829-
/* record the newly created socket as the first msk
830-
* subflow, but don't link it yet into conn_list
831-
*/
832-
WRITE_ONCE(owner->first, child);
833-
834-
/* new mpc subflow takes ownership of the newly
835-
* created mptcp socket
836-
*/
837-
owner->setsockopt_seq = ctx->setsockopt_seq;
838823
mptcp_pm_new_connection(owner, child, 1);
839-
mptcp_token_accept(subflow_req, owner);
840-
841-
/* set msk addresses early to ensure mptcp_pm_get_local_id()
842-
* uses the correct data
843-
*/
844-
mptcp_copy_inaddrs(ctx->conn, child);
845-
mptcp_propagate_sndbuf(ctx->conn, child);
846-
847-
mptcp_rcv_space_init(owner, child);
848-
list_add(&ctx->node, &owner->conn_list);
849-
sock_hold(child);
850824

851825
/* with OoO packets we can reach here without ingress
852826
* mpc option

0 commit comments

Comments
 (0)