1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 19cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 20cf7da0d6SPeter Krystad #include <net/transp_v6.h> 21cf7da0d6SPeter Krystad #endif 22f870fa0bSMat Martineau #include <net/mptcp.h> 23f870fa0bSMat Martineau #include "protocol.h" 24fc518953SFlorian Westphal #include "mib.h" 25f870fa0bSMat Martineau 262303f994SPeter Krystad #define MPTCP_SAME_STATE TCP_MAX_STATES 272303f994SPeter Krystad 28b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 29b0519de8SFlorian Westphal struct mptcp6_sock { 30b0519de8SFlorian Westphal struct mptcp_sock msk; 31b0519de8SFlorian Westphal struct ipv6_pinfo np; 32b0519de8SFlorian Westphal }; 33b0519de8SFlorian Westphal #endif 34b0519de8SFlorian Westphal 356771bfd9SFlorian Westphal struct mptcp_skb_cb { 366771bfd9SFlorian Westphal u32 offset; 376771bfd9SFlorian Westphal }; 386771bfd9SFlorian Westphal 396771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 406771bfd9SFlorian Westphal 41d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated; 42d027236cSPaolo Abeni 432303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 442303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 452303f994SPeter Krystad * Otherwise return NULL. 462303f994SPeter Krystad */ 472303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 482303f994SPeter Krystad { 49d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 502303f994SPeter Krystad return NULL; 512303f994SPeter Krystad 522303f994SPeter Krystad return msk->subflow; 532303f994SPeter Krystad } 542303f994SPeter Krystad 558ab183deSPaolo Abeni static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk) 568ab183deSPaolo Abeni { 578ab183deSPaolo Abeni return msk->first && !sk_is_mptcp(msk->first); 588ab183deSPaolo Abeni } 598ab183deSPaolo Abeni 600b4f33deSFlorian Westphal static struct socket *mptcp_is_tcpsk(struct sock *sk) 610b4f33deSFlorian Westphal { 620b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 630b4f33deSFlorian Westphal 640b4f33deSFlorian Westphal if (sock->sk != sk) 650b4f33deSFlorian Westphal return NULL; 660b4f33deSFlorian Westphal 670b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 680b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 690b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 700b4f33deSFlorian Westphal * not an mptcp one. 710b4f33deSFlorian Westphal * 720b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 730b4f33deSFlorian Westphal * bypass mptcp. 740b4f33deSFlorian Westphal */ 750b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 760b4f33deSFlorian Westphal return sock; 770b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 780b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 790b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 800b4f33deSFlorian Westphal return sock; 810b4f33deSFlorian Westphal #endif 820b4f33deSFlorian Westphal } 830b4f33deSFlorian Westphal 840b4f33deSFlorian Westphal return NULL; 850b4f33deSFlorian Westphal } 860b4f33deSFlorian Westphal 878ab183deSPaolo Abeni static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk) 88cec37a6eSPeter Krystad { 890b4f33deSFlorian Westphal struct socket *sock; 900b4f33deSFlorian Westphal 91cec37a6eSPeter Krystad sock_owned_by_me((const struct sock *)msk); 92cec37a6eSPeter Krystad 930b4f33deSFlorian Westphal sock = mptcp_is_tcpsk((struct sock *)msk); 940b4f33deSFlorian Westphal if (unlikely(sock)) 950b4f33deSFlorian Westphal return sock; 960b4f33deSFlorian Westphal 978ab183deSPaolo Abeni if (likely(!__mptcp_needs_tcp_fallback(msk))) 98cec37a6eSPeter Krystad return NULL; 99cec37a6eSPeter Krystad 1002c22c06cSFlorian Westphal return msk->subflow; 1018ab183deSPaolo Abeni } 1028ab183deSPaolo Abeni 1032303f994SPeter Krystad static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk) 1042303f994SPeter Krystad { 1058ab183deSPaolo Abeni return !msk->first; 1062303f994SPeter Krystad } 1072303f994SPeter Krystad 1082303f994SPeter Krystad static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) 1092303f994SPeter Krystad { 1102303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1112303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1122303f994SPeter Krystad struct socket *ssock; 1132303f994SPeter Krystad int err; 1142303f994SPeter Krystad 1150b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 1160b4f33deSFlorian Westphal if (unlikely(ssock)) 1170b4f33deSFlorian Westphal return ssock; 1180b4f33deSFlorian Westphal 1192303f994SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1202303f994SPeter Krystad if (ssock) 1212303f994SPeter Krystad goto set_state; 1222303f994SPeter Krystad 1232303f994SPeter Krystad if (!__mptcp_can_create_subflow(msk)) 1242303f994SPeter Krystad return ERR_PTR(-EINVAL); 1252303f994SPeter Krystad 1262303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1272303f994SPeter Krystad if (err) 1282303f994SPeter Krystad return ERR_PTR(err); 1292303f994SPeter Krystad 1308ab183deSPaolo Abeni msk->first = ssock->sk; 1312303f994SPeter Krystad msk->subflow = ssock; 1322303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 133cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 1342303f994SPeter Krystad subflow->request_mptcp = 1; 1352303f994SPeter Krystad 1362303f994SPeter Krystad set_state: 1372303f994SPeter Krystad if (state != MPTCP_SAME_STATE) 1382303f994SPeter Krystad inet_sk_state_store(sk, state); 1392303f994SPeter Krystad return ssock; 1402303f994SPeter Krystad } 1412303f994SPeter Krystad 1426771bfd9SFlorian Westphal static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 1436771bfd9SFlorian Westphal struct sk_buff *skb, 1446771bfd9SFlorian Westphal unsigned int offset, size_t copy_len) 1456771bfd9SFlorian Westphal { 1466771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 1474e637c70SFlorian Westphal struct sk_buff *tail; 1486771bfd9SFlorian Westphal 1496771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 1504e637c70SFlorian Westphal 1514e637c70SFlorian Westphal skb_ext_reset(skb); 1524e637c70SFlorian Westphal skb_orphan(skb); 1534e637c70SFlorian Westphal msk->ack_seq += copy_len; 1544e637c70SFlorian Westphal 1554e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 1564e637c70SFlorian Westphal if (offset == 0 && tail) { 1574e637c70SFlorian Westphal bool fragstolen; 1584e637c70SFlorian Westphal int delta; 1594e637c70SFlorian Westphal 1604e637c70SFlorian Westphal if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) { 1614e637c70SFlorian Westphal kfree_skb_partial(skb, fragstolen); 1624e637c70SFlorian Westphal atomic_add(delta, &sk->sk_rmem_alloc); 1634e637c70SFlorian Westphal sk_mem_charge(sk, delta); 1644e637c70SFlorian Westphal return; 1654e637c70SFlorian Westphal } 1664e637c70SFlorian Westphal } 1674e637c70SFlorian Westphal 168600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 1696771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 1706771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset = offset; 1716771bfd9SFlorian Westphal } 1726771bfd9SFlorian Westphal 173de06f573SFlorian Westphal /* both sockets must be locked */ 174de06f573SFlorian Westphal static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, 175de06f573SFlorian Westphal struct sock *ssk) 176de06f573SFlorian Westphal { 177de06f573SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 178de06f573SFlorian Westphal u64 dsn = mptcp_subflow_get_mapped_dsn(subflow); 179de06f573SFlorian Westphal 180de06f573SFlorian Westphal /* revalidate data sequence number. 181de06f573SFlorian Westphal * 182de06f573SFlorian Westphal * mptcp_subflow_data_available() is usually called 183de06f573SFlorian Westphal * without msk lock. Its unlikely (but possible) 184de06f573SFlorian Westphal * that msk->ack_seq has been advanced since the last 185de06f573SFlorian Westphal * call found in-sequence data. 186de06f573SFlorian Westphal */ 187de06f573SFlorian Westphal if (likely(dsn == msk->ack_seq)) 188de06f573SFlorian Westphal return true; 189de06f573SFlorian Westphal 190de06f573SFlorian Westphal subflow->data_avail = 0; 191de06f573SFlorian Westphal return mptcp_subflow_data_available(ssk); 192de06f573SFlorian Westphal } 193de06f573SFlorian Westphal 1946771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 1956771bfd9SFlorian Westphal struct sock *ssk, 1966771bfd9SFlorian Westphal unsigned int *bytes) 1976771bfd9SFlorian Westphal { 1986771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 199600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 2006771bfd9SFlorian Westphal unsigned int moved = 0; 2016771bfd9SFlorian Westphal bool more_data_avail; 2026771bfd9SFlorian Westphal struct tcp_sock *tp; 2036771bfd9SFlorian Westphal bool done = false; 204600911ffSFlorian Westphal 205de06f573SFlorian Westphal if (!mptcp_subflow_dsn_valid(msk, ssk)) { 206de06f573SFlorian Westphal *bytes = 0; 207de06f573SFlorian Westphal return false; 208de06f573SFlorian Westphal } 209de06f573SFlorian Westphal 210ec33916dSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 211ec33916dSFlorian Westphal int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf); 212ec33916dSFlorian Westphal 213600911ffSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) 214600911ffSFlorian Westphal sk->sk_rcvbuf = rcvbuf; 215ec33916dSFlorian Westphal } 2166771bfd9SFlorian Westphal 2176771bfd9SFlorian Westphal tp = tcp_sk(ssk); 2186771bfd9SFlorian Westphal do { 2196771bfd9SFlorian Westphal u32 map_remaining, offset; 2206771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 2216771bfd9SFlorian Westphal struct sk_buff *skb; 2226771bfd9SFlorian Westphal bool fin; 2236771bfd9SFlorian Westphal 2246771bfd9SFlorian Westphal /* try to move as much data as available */ 2256771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 2266771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 2276771bfd9SFlorian Westphal 2286771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 2296771bfd9SFlorian Westphal if (!skb) 2306771bfd9SFlorian Westphal break; 2316771bfd9SFlorian Westphal 2326771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 2336771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 2346771bfd9SFlorian Westphal if (fin) { 2356771bfd9SFlorian Westphal done = true; 2366771bfd9SFlorian Westphal seq++; 2376771bfd9SFlorian Westphal } 2386771bfd9SFlorian Westphal 2396771bfd9SFlorian Westphal if (offset < skb->len) { 2406771bfd9SFlorian Westphal size_t len = skb->len - offset; 2416771bfd9SFlorian Westphal 2426771bfd9SFlorian Westphal if (tp->urg_data) 2436771bfd9SFlorian Westphal done = true; 2446771bfd9SFlorian Westphal 2456771bfd9SFlorian Westphal __mptcp_move_skb(msk, ssk, skb, offset, len); 2466771bfd9SFlorian Westphal seq += len; 2476771bfd9SFlorian Westphal moved += len; 2486771bfd9SFlorian Westphal 2496771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 2506771bfd9SFlorian Westphal break; 2516771bfd9SFlorian Westphal } else { 2526771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 2536771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 2546771bfd9SFlorian Westphal done = true; 2556771bfd9SFlorian Westphal } 2566771bfd9SFlorian Westphal 2576771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 2586771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 259600911ffSFlorian Westphal 260600911ffSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) { 261600911ffSFlorian Westphal done = true; 262600911ffSFlorian Westphal break; 263600911ffSFlorian Westphal } 2646771bfd9SFlorian Westphal } while (more_data_avail); 2656771bfd9SFlorian Westphal 2666771bfd9SFlorian Westphal *bytes = moved; 2676771bfd9SFlorian Westphal 2686771bfd9SFlorian Westphal return done; 2696771bfd9SFlorian Westphal } 2706771bfd9SFlorian Westphal 2712e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 2722e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 2732e52213cSFlorian Westphal */ 2742e52213cSFlorian Westphal static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 2752e52213cSFlorian Westphal { 2762e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 2772e52213cSFlorian Westphal unsigned int moved = 0; 2782e52213cSFlorian Westphal 2792e52213cSFlorian Westphal if (READ_ONCE(sk->sk_lock.owned)) 2802e52213cSFlorian Westphal return false; 2812e52213cSFlorian Westphal 2822e52213cSFlorian Westphal if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 2832e52213cSFlorian Westphal return false; 2842e52213cSFlorian Westphal 2852e52213cSFlorian Westphal /* must re-check after taking the lock */ 2862e52213cSFlorian Westphal if (!READ_ONCE(sk->sk_lock.owned)) 2872e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 2882e52213cSFlorian Westphal 2892e52213cSFlorian Westphal spin_unlock_bh(&sk->sk_lock.slock); 2902e52213cSFlorian Westphal 2912e52213cSFlorian Westphal return moved > 0; 2922e52213cSFlorian Westphal } 2932e52213cSFlorian Westphal 2942e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 295101f6f85SFlorian Westphal { 296101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 297101f6f85SFlorian Westphal 298101f6f85SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 2996771bfd9SFlorian Westphal 3002e52213cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) && 3012e52213cSFlorian Westphal move_skbs_to_msk(msk, ssk)) 3022e52213cSFlorian Westphal goto wake; 3032e52213cSFlorian Westphal 304600911ffSFlorian Westphal /* don't schedule if mptcp sk is (still) over limit */ 305600911ffSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) 306600911ffSFlorian Westphal goto wake; 307600911ffSFlorian Westphal 30814c441b5SPaolo Abeni /* mptcp socket is owned, release_cb should retry */ 30914c441b5SPaolo Abeni if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 31014c441b5SPaolo Abeni &sk->sk_tsq_flags)) { 31114c441b5SPaolo Abeni sock_hold(sk); 3126771bfd9SFlorian Westphal 31314c441b5SPaolo Abeni /* need to try again, its possible release_cb() has already 31414c441b5SPaolo Abeni * been called after the test_and_set_bit() above. 31514c441b5SPaolo Abeni */ 31614c441b5SPaolo Abeni move_skbs_to_msk(msk, ssk); 31714c441b5SPaolo Abeni } 318600911ffSFlorian Westphal wake: 319101f6f85SFlorian Westphal sk->sk_data_ready(sk); 320101f6f85SFlorian Westphal } 321101f6f85SFlorian Westphal 322ec3edaa7SPeter Krystad static void __mptcp_flush_join_list(struct mptcp_sock *msk) 323ec3edaa7SPeter Krystad { 324ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 325ec3edaa7SPeter Krystad return; 326ec3edaa7SPeter Krystad 327ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 328ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 329ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 330ec3edaa7SPeter Krystad } 331ec3edaa7SPeter Krystad 332b51f9b80SPaolo Abeni static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 333b51f9b80SPaolo Abeni { 334b51f9b80SPaolo Abeni long tout = ssk && inet_csk(ssk)->icsk_pending ? 335b51f9b80SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 336b51f9b80SPaolo Abeni 337b51f9b80SPaolo Abeni if (tout <= 0) 338b51f9b80SPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 339b51f9b80SPaolo Abeni mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 340b51f9b80SPaolo Abeni } 341b51f9b80SPaolo Abeni 342b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 343b51f9b80SPaolo Abeni { 344b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 345b51f9b80SPaolo Abeni } 346b51f9b80SPaolo Abeni 347b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 348b51f9b80SPaolo Abeni { 349b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 350b51f9b80SPaolo Abeni unsigned long tout; 351b51f9b80SPaolo Abeni 352b51f9b80SPaolo Abeni /* should never be called with mptcp level timer cleared */ 353b51f9b80SPaolo Abeni tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 354b51f9b80SPaolo Abeni if (WARN_ON_ONCE(!tout)) 355b51f9b80SPaolo Abeni tout = TCP_RTO_MIN; 356b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 357b51f9b80SPaolo Abeni } 358b51f9b80SPaolo Abeni 359b51f9b80SPaolo Abeni void mptcp_data_acked(struct sock *sk) 360b51f9b80SPaolo Abeni { 361b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 3623b1d6210SPaolo Abeni 3633b1d6210SPaolo Abeni if (!sk_stream_is_writeable(sk) && 3643b1d6210SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) 3653b1d6210SPaolo Abeni sock_hold(sk); 366b51f9b80SPaolo Abeni } 367b51f9b80SPaolo Abeni 36859832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 36959832e24SFlorian Westphal { 37059832e24SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 37159832e24SFlorian Westphal 37259832e24SFlorian Westphal if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) && 37359832e24SFlorian Westphal schedule_work(&msk->work)) 37459832e24SFlorian Westphal sock_hold(sk); 37559832e24SFlorian Westphal } 37659832e24SFlorian Westphal 377*5969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 378*5969856aSPaolo Abeni { 379*5969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 380*5969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 381*5969856aSPaolo Abeni int receivers = 0; 382*5969856aSPaolo Abeni 383*5969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 384*5969856aSPaolo Abeni receivers += !subflow->rx_eof; 385*5969856aSPaolo Abeni 386*5969856aSPaolo Abeni if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 387*5969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 388*5969856aSPaolo Abeni * to msk, when all subflows agree on it 389*5969856aSPaolo Abeni */ 390*5969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 391*5969856aSPaolo Abeni 392*5969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 393*5969856aSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 394*5969856aSPaolo Abeni sk->sk_data_ready(sk); 395*5969856aSPaolo Abeni } 396*5969856aSPaolo Abeni } 397*5969856aSPaolo Abeni 398b51f9b80SPaolo Abeni static void mptcp_stop_timer(struct sock *sk) 399b51f9b80SPaolo Abeni { 400b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 401b51f9b80SPaolo Abeni 402b51f9b80SPaolo Abeni sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 403b51f9b80SPaolo Abeni mptcp_sk(sk)->timer_ival = 0; 404b51f9b80SPaolo Abeni } 405b51f9b80SPaolo Abeni 4066d0060f6SMat Martineau static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 4076d0060f6SMat Martineau { 4084930f483SFlorian Westphal const struct sock *sk = (const struct sock *)msk; 4094930f483SFlorian Westphal 4106d0060f6SMat Martineau if (!msk->cached_ext) 4114930f483SFlorian Westphal msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); 4126d0060f6SMat Martineau 4136d0060f6SMat Martineau return !!msk->cached_ext; 4146d0060f6SMat Martineau } 4156d0060f6SMat Martineau 4167a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 4177a6a6cbcSPaolo Abeni { 4187a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 4197a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 4207a6a6cbcSPaolo Abeni 4217a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 4227a6a6cbcSPaolo Abeni 4237a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 4247a6a6cbcSPaolo Abeni if (subflow->data_avail) 4257a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 4267a6a6cbcSPaolo Abeni } 4277a6a6cbcSPaolo Abeni 4287a6a6cbcSPaolo Abeni return NULL; 4297a6a6cbcSPaolo Abeni } 4307a6a6cbcSPaolo Abeni 4313f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 43257040755SPaolo Abeni const struct sk_buff *skb, 43357040755SPaolo Abeni const struct mptcp_ext *mpext) 4346d0060f6SMat Martineau { 43557040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 43657040755SPaolo Abeni return false; 43757040755SPaolo Abeni 43857040755SPaolo Abeni /* can collapse only if MPTCP level sequence is in order */ 4393f8e0aaeSPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq; 44057040755SPaolo Abeni } 44157040755SPaolo Abeni 44218b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 44318b683bfSPaolo Abeni const struct page_frag *pfrag, 44418b683bfSPaolo Abeni const struct mptcp_data_frag *df) 44518b683bfSPaolo Abeni { 44618b683bfSPaolo Abeni return df && pfrag->page == df->page && 44718b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 44818b683bfSPaolo Abeni } 44918b683bfSPaolo Abeni 450d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 45118b683bfSPaolo Abeni { 452d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 4537948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 454d027236cSPaolo Abeni } 455d027236cSPaolo Abeni 456d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 457d027236cSPaolo Abeni { 458d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 459d027236cSPaolo Abeni 46018b683bfSPaolo Abeni list_del(&dfrag->list); 461d027236cSPaolo Abeni dfrag_uncharge(sk, len); 46218b683bfSPaolo Abeni put_page(dfrag->page); 46318b683bfSPaolo Abeni } 46418b683bfSPaolo Abeni 46518b683bfSPaolo Abeni static void mptcp_clean_una(struct sock *sk) 46618b683bfSPaolo Abeni { 46718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 46818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 46918b683bfSPaolo Abeni u64 snd_una = atomic64_read(&msk->snd_una); 470d027236cSPaolo Abeni bool cleaned = false; 47118b683bfSPaolo Abeni 47218b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 47318b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 47418b683bfSPaolo Abeni break; 47518b683bfSPaolo Abeni 476d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 477d027236cSPaolo Abeni cleaned = true; 478d027236cSPaolo Abeni } 479d027236cSPaolo Abeni 4807948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 4817948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 4827948f6ccSFlorian Westphal u64 delta = dfrag->data_seq + dfrag->data_len - snd_una; 4837948f6ccSFlorian Westphal 4847948f6ccSFlorian Westphal dfrag->data_seq += delta; 4857948f6ccSFlorian Westphal dfrag->data_len -= delta; 4867948f6ccSFlorian Westphal 4877948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 4887948f6ccSFlorian Westphal cleaned = true; 4897948f6ccSFlorian Westphal } 4907948f6ccSFlorian Westphal 491d027236cSPaolo Abeni if (cleaned) { 492d027236cSPaolo Abeni sk_mem_reclaim_partial(sk); 4937948f6ccSFlorian Westphal 4947948f6ccSFlorian Westphal /* Only wake up writers if a subflow is ready */ 4957948f6ccSFlorian Westphal if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 4967948f6ccSFlorian Westphal sk_stream_write_space(sk); 49718b683bfSPaolo Abeni } 49818b683bfSPaolo Abeni } 49918b683bfSPaolo Abeni 50018b683bfSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 50118b683bfSPaolo Abeni * data 50218b683bfSPaolo Abeni */ 50318b683bfSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 50418b683bfSPaolo Abeni { 50518b683bfSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 50618b683bfSPaolo Abeni pfrag, sk->sk_allocation))) 50718b683bfSPaolo Abeni return true; 50818b683bfSPaolo Abeni 50918b683bfSPaolo Abeni sk->sk_prot->enter_memory_pressure(sk); 51018b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 51118b683bfSPaolo Abeni return false; 51218b683bfSPaolo Abeni } 51318b683bfSPaolo Abeni 51418b683bfSPaolo Abeni static struct mptcp_data_frag * 51518b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 51618b683bfSPaolo Abeni int orig_offset) 51718b683bfSPaolo Abeni { 51818b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 51918b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 52018b683bfSPaolo Abeni 52118b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 52218b683bfSPaolo Abeni dfrag->data_len = 0; 52318b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 52418b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 52518b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 52618b683bfSPaolo Abeni dfrag->page = pfrag->page; 52718b683bfSPaolo Abeni 52818b683bfSPaolo Abeni return dfrag; 52918b683bfSPaolo Abeni } 53018b683bfSPaolo Abeni 53157040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 5323f8e0aaeSPaolo Abeni struct msghdr *msg, struct mptcp_data_frag *dfrag, 5333f8e0aaeSPaolo Abeni long *timeo, int *pmss_now, 53457040755SPaolo Abeni int *ps_goal) 53557040755SPaolo Abeni { 53618b683bfSPaolo Abeni int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0; 53718b683bfSPaolo Abeni bool dfrag_collapsed, can_collapse = false; 5386d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5396d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 5403f8e0aaeSPaolo Abeni bool retransmission = !!dfrag; 54157040755SPaolo Abeni struct sk_buff *skb, *tail; 5426d0060f6SMat Martineau struct page_frag *pfrag; 5433f8e0aaeSPaolo Abeni struct page *page; 5443f8e0aaeSPaolo Abeni u64 *write_seq; 5456d0060f6SMat Martineau size_t psize; 5466d0060f6SMat Martineau 5476d0060f6SMat Martineau /* use the mptcp page cache so that we can easily move the data 5486d0060f6SMat Martineau * from one substream to another, but do per subflow memory accounting 5493f8e0aaeSPaolo Abeni * Note: pfrag is used only !retransmission, but the compiler if 5503f8e0aaeSPaolo Abeni * fooled into a warning if we don't init here 5516d0060f6SMat Martineau */ 5526d0060f6SMat Martineau pfrag = sk_page_frag(sk); 5533f8e0aaeSPaolo Abeni if (!retransmission) { 5543f8e0aaeSPaolo Abeni write_seq = &msk->write_seq; 5553f8e0aaeSPaolo Abeni page = pfrag->page; 5563f8e0aaeSPaolo Abeni } else { 5573f8e0aaeSPaolo Abeni write_seq = &dfrag->data_seq; 5583f8e0aaeSPaolo Abeni page = dfrag->page; 5593f8e0aaeSPaolo Abeni } 5606d0060f6SMat Martineau 5616d0060f6SMat Martineau /* compute copy limit */ 5626d0060f6SMat Martineau mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); 56357040755SPaolo Abeni *pmss_now = mss_now; 56457040755SPaolo Abeni *ps_goal = size_goal; 56557040755SPaolo Abeni avail_size = size_goal; 56657040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 56757040755SPaolo Abeni if (skb) { 56857040755SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 5696d0060f6SMat Martineau 57057040755SPaolo Abeni /* Limit the write to the size available in the 57157040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 57257040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 57357040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 57457040755SPaolo Abeni * SSN association set here 57557040755SPaolo Abeni */ 57657040755SPaolo Abeni can_collapse = (size_goal - skb->len > 0) && 5773f8e0aaeSPaolo Abeni mptcp_skb_can_collapse_to(*write_seq, skb, mpext); 57857040755SPaolo Abeni if (!can_collapse) 57957040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 58057040755SPaolo Abeni else 58157040755SPaolo Abeni avail_size = size_goal - skb->len; 58257040755SPaolo Abeni } 58318b683bfSPaolo Abeni 5843f8e0aaeSPaolo Abeni if (!retransmission) { 5853f8e0aaeSPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 5863f8e0aaeSPaolo Abeni * page allocator 58718b683bfSPaolo Abeni */ 58818b683bfSPaolo Abeni dfrag = mptcp_rtx_tail(sk); 58918b683bfSPaolo Abeni offset = pfrag->offset; 59018b683bfSPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 59118b683bfSPaolo Abeni if (!dfrag_collapsed) { 59218b683bfSPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, offset); 59318b683bfSPaolo Abeni offset = dfrag->offset; 59418b683bfSPaolo Abeni frag_truesize = dfrag->overhead; 59518b683bfSPaolo Abeni } 59618b683bfSPaolo Abeni psize = min_t(size_t, pfrag->size - offset, avail_size); 59757040755SPaolo Abeni 59857040755SPaolo Abeni /* Copy to page */ 5996d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 60018b683bfSPaolo Abeni psize = copy_page_from_iter(pfrag->page, offset, 6013f8e0aaeSPaolo Abeni min_t(size_t, msg_data_left(msg), 6023f8e0aaeSPaolo Abeni psize), 6036d0060f6SMat Martineau &msg->msg_iter); 6046d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 6056d0060f6SMat Martineau if (!psize) 6066d0060f6SMat Martineau return -EINVAL; 6076d0060f6SMat Martineau 608d027236cSPaolo Abeni if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) 609d027236cSPaolo Abeni return -ENOMEM; 6103f8e0aaeSPaolo Abeni } else { 6113f8e0aaeSPaolo Abeni offset = dfrag->offset; 6123f8e0aaeSPaolo Abeni psize = min_t(size_t, dfrag->data_len, avail_size); 6133f8e0aaeSPaolo Abeni } 614d027236cSPaolo Abeni 61557040755SPaolo Abeni /* tell the TCP stack to delay the push so that we can safely 61657040755SPaolo Abeni * access the skb after the sendpages call 6176d0060f6SMat Martineau */ 6183f8e0aaeSPaolo Abeni ret = do_tcp_sendpages(ssk, page, offset, psize, 61972511aabSFlorian Westphal msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT); 6206d0060f6SMat Martineau if (ret <= 0) 6216d0060f6SMat Martineau return ret; 62218b683bfSPaolo Abeni 62318b683bfSPaolo Abeni frag_truesize += ret; 6243f8e0aaeSPaolo Abeni if (!retransmission) { 6256d0060f6SMat Martineau if (unlikely(ret < psize)) 6266d0060f6SMat Martineau iov_iter_revert(&msg->msg_iter, psize - ret); 6276d0060f6SMat Martineau 62818b683bfSPaolo Abeni /* send successful, keep track of sent data for mptcp-level 62918b683bfSPaolo Abeni * retransmission 63018b683bfSPaolo Abeni */ 63118b683bfSPaolo Abeni dfrag->data_len += ret; 63218b683bfSPaolo Abeni if (!dfrag_collapsed) { 63318b683bfSPaolo Abeni get_page(dfrag->page); 63418b683bfSPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 6353f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 6363f8e0aaeSPaolo Abeni } else { 6373f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, ret); 63818b683bfSPaolo Abeni } 63918b683bfSPaolo Abeni 640d027236cSPaolo Abeni /* charge data on mptcp rtx queue to the master socket 641d027236cSPaolo Abeni * Note: we charge such data both to sk and ssk 642d027236cSPaolo Abeni */ 643d027236cSPaolo Abeni sk->sk_forward_alloc -= frag_truesize; 6443f8e0aaeSPaolo Abeni } 645d027236cSPaolo Abeni 64657040755SPaolo Abeni /* if the tail skb extension is still the cached one, collapsing 64757040755SPaolo Abeni * really happened. Note: we can't check for 'same skb' as the sk_buff 64857040755SPaolo Abeni * hdr on tail can be transmitted, freed and re-allocated by the 64957040755SPaolo Abeni * do_tcp_sendpages() call 65057040755SPaolo Abeni */ 65157040755SPaolo Abeni tail = tcp_write_queue_tail(ssk); 65257040755SPaolo Abeni if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { 65357040755SPaolo Abeni WARN_ON_ONCE(!can_collapse); 65457040755SPaolo Abeni mpext->data_len += ret; 65557040755SPaolo Abeni goto out; 65657040755SPaolo Abeni } 65757040755SPaolo Abeni 6586d0060f6SMat Martineau skb = tcp_write_queue_tail(ssk); 6596d0060f6SMat Martineau mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); 6606d0060f6SMat Martineau msk->cached_ext = NULL; 6616d0060f6SMat Martineau 6626d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 6633f8e0aaeSPaolo Abeni mpext->data_seq = *write_seq; 6646d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 6656d0060f6SMat Martineau mpext->data_len = ret; 6666d0060f6SMat Martineau mpext->use_map = 1; 6676d0060f6SMat Martineau mpext->dsn64 = 1; 6686d0060f6SMat Martineau 6696d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 6706d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 6716d0060f6SMat Martineau mpext->dsn64); 6726d0060f6SMat Martineau 67357040755SPaolo Abeni out: 6743f8e0aaeSPaolo Abeni if (!retransmission) 67518b683bfSPaolo Abeni pfrag->offset += frag_truesize; 6763f8e0aaeSPaolo Abeni *write_seq += ret; 6776d0060f6SMat Martineau mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 6786d0060f6SMat Martineau 6796d0060f6SMat Martineau return ret; 6806d0060f6SMat Martineau } 6816d0060f6SMat Martineau 682a0e17064SFlorian Westphal static void mptcp_nospace(struct mptcp_sock *msk, struct socket *sock) 683a0e17064SFlorian Westphal { 684a0e17064SFlorian Westphal clear_bit(MPTCP_SEND_SPACE, &msk->flags); 685a0e17064SFlorian Westphal smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 686a0e17064SFlorian Westphal 687a0e17064SFlorian Westphal /* enables sk->write_space() callbacks */ 688a0e17064SFlorian Westphal set_bit(SOCK_NOSPACE, &sock->flags); 689a0e17064SFlorian Westphal } 690a0e17064SFlorian Westphal 691f296234cSPeter Krystad static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 692f296234cSPeter Krystad { 693f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 694f296234cSPeter Krystad struct sock *backup = NULL; 695f296234cSPeter Krystad 696f296234cSPeter Krystad sock_owned_by_me((const struct sock *)msk); 697f296234cSPeter Krystad 698149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 699149f7c71SFlorian Westphal return NULL; 700149f7c71SFlorian Westphal 701f296234cSPeter Krystad mptcp_for_each_subflow(msk, subflow) { 702f296234cSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 703f296234cSPeter Krystad 704f296234cSPeter Krystad if (!sk_stream_memory_free(ssk)) { 705f296234cSPeter Krystad struct socket *sock = ssk->sk_socket; 706f296234cSPeter Krystad 707a0e17064SFlorian Westphal if (sock) 708a0e17064SFlorian Westphal mptcp_nospace(msk, sock); 709f296234cSPeter Krystad 710f296234cSPeter Krystad return NULL; 711f296234cSPeter Krystad } 712f296234cSPeter Krystad 713f296234cSPeter Krystad if (subflow->backup) { 714f296234cSPeter Krystad if (!backup) 715f296234cSPeter Krystad backup = ssk; 716f296234cSPeter Krystad 717f296234cSPeter Krystad continue; 718f296234cSPeter Krystad } 719f296234cSPeter Krystad 720f296234cSPeter Krystad return ssk; 721f296234cSPeter Krystad } 722f296234cSPeter Krystad 723f296234cSPeter Krystad return backup; 724f296234cSPeter Krystad } 725f296234cSPeter Krystad 7261891c4a0SFlorian Westphal static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) 7271891c4a0SFlorian Westphal { 7281891c4a0SFlorian Westphal struct socket *sock; 7291891c4a0SFlorian Westphal 7301891c4a0SFlorian Westphal if (likely(sk_stream_is_writeable(ssk))) 7311891c4a0SFlorian Westphal return; 7321891c4a0SFlorian Westphal 7331891c4a0SFlorian Westphal sock = READ_ONCE(ssk->sk_socket); 734a0e17064SFlorian Westphal if (sock) 735a0e17064SFlorian Westphal mptcp_nospace(msk, sock); 7361891c4a0SFlorian Westphal } 7371891c4a0SFlorian Westphal 738f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 739f870fa0bSMat Martineau { 74057040755SPaolo Abeni int mss_now = 0, size_goal = 0, ret = 0; 741f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 74217091708SFlorian Westphal struct page_frag *pfrag; 743cec37a6eSPeter Krystad struct socket *ssock; 7446d0060f6SMat Martineau size_t copied = 0; 745cec37a6eSPeter Krystad struct sock *ssk; 74672511aabSFlorian Westphal bool tx_ok; 7476d0060f6SMat Martineau long timeo; 748f870fa0bSMat Martineau 749f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 750f870fa0bSMat Martineau return -EOPNOTSUPP; 751f870fa0bSMat Martineau 752cec37a6eSPeter Krystad lock_sock(sk); 7531954b860SMat Martineau 7541954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 7551954b860SMat Martineau 7561954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 7571954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 7581954b860SMat Martineau if (ret) 7591954b860SMat Martineau goto out; 7601954b860SMat Martineau } 7611954b860SMat Martineau 762e154659bSFlorian Westphal fallback: 763cec37a6eSPeter Krystad ssock = __mptcp_tcp_fallback(msk); 7648ab183deSPaolo Abeni if (unlikely(ssock)) { 765e154659bSFlorian Westphal release_sock(sk); 766cec37a6eSPeter Krystad pr_debug("fallback passthrough"); 767cec37a6eSPeter Krystad ret = sock_sendmsg(ssock, msg); 7688ab183deSPaolo Abeni return ret >= 0 ? ret + copied : (copied ? copied : ret); 769cec37a6eSPeter Krystad } 770cec37a6eSPeter Krystad 77117091708SFlorian Westphal pfrag = sk_page_frag(sk); 77272511aabSFlorian Westphal restart: 77318b683bfSPaolo Abeni mptcp_clean_una(sk); 77418b683bfSPaolo Abeni 775fb529e62SFlorian Westphal wait_for_sndbuf: 776ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 777f296234cSPeter Krystad ssk = mptcp_subflow_get_send(msk); 77817091708SFlorian Westphal while (!sk_stream_memory_free(sk) || 77917091708SFlorian Westphal !ssk || 78017091708SFlorian Westphal !mptcp_page_frag_refill(ssk, pfrag)) { 781fb529e62SFlorian Westphal if (ssk) { 782fb529e62SFlorian Westphal /* make sure retransmit timer is 783fb529e62SFlorian Westphal * running before we wait for memory. 784fb529e62SFlorian Westphal * 785fb529e62SFlorian Westphal * The retransmit timer might be needed 786fb529e62SFlorian Westphal * to make the peer send an up-to-date 787fb529e62SFlorian Westphal * MPTCP Ack. 788fb529e62SFlorian Westphal */ 789fb529e62SFlorian Westphal mptcp_set_timeout(sk, ssk); 790fb529e62SFlorian Westphal if (!mptcp_timer_pending(sk)) 791fb529e62SFlorian Westphal mptcp_reset_timer(sk); 792fb529e62SFlorian Westphal } 793fb529e62SFlorian Westphal 794f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 795f296234cSPeter Krystad if (ret) 796f296234cSPeter Krystad goto out; 797f296234cSPeter Krystad 79818b683bfSPaolo Abeni mptcp_clean_una(sk); 79918b683bfSPaolo Abeni 800f296234cSPeter Krystad ssk = mptcp_subflow_get_send(msk); 801f296234cSPeter Krystad if (list_empty(&msk->conn_list)) { 802f296234cSPeter Krystad ret = -ENOTCONN; 803f296234cSPeter Krystad goto out; 804f296234cSPeter Krystad } 805cec37a6eSPeter Krystad } 806cec37a6eSPeter Krystad 8076d0060f6SMat Martineau pr_debug("conn_list->subflow=%p", ssk); 808cec37a6eSPeter Krystad 8096d0060f6SMat Martineau lock_sock(ssk); 81072511aabSFlorian Westphal tx_ok = msg_data_left(msg); 81172511aabSFlorian Westphal while (tx_ok) { 8123f8e0aaeSPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now, 81357040755SPaolo Abeni &size_goal); 81472511aabSFlorian Westphal if (ret < 0) { 81572511aabSFlorian Westphal if (ret == -EAGAIN && timeo > 0) { 81672511aabSFlorian Westphal mptcp_set_timeout(sk, ssk); 81772511aabSFlorian Westphal release_sock(ssk); 81872511aabSFlorian Westphal goto restart; 81972511aabSFlorian Westphal } 8206d0060f6SMat Martineau break; 82172511aabSFlorian Westphal } 8228ab183deSPaolo Abeni if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) { 823e154659bSFlorian Westphal /* Can happen for passive sockets: 824e154659bSFlorian Westphal * 3WHS negotiated MPTCP, but first packet after is 825e154659bSFlorian Westphal * plain TCP (e.g. due to middlebox filtering unknown 826e154659bSFlorian Westphal * options). 827e154659bSFlorian Westphal * 828e154659bSFlorian Westphal * Fall back to TCP. 829e154659bSFlorian Westphal */ 8308ab183deSPaolo Abeni release_sock(ssk); 8318ab183deSPaolo Abeni goto fallback; 8328ab183deSPaolo Abeni } 8336d0060f6SMat Martineau 8346d0060f6SMat Martineau copied += ret; 835fb529e62SFlorian Westphal 83672511aabSFlorian Westphal tx_ok = msg_data_left(msg); 83772511aabSFlorian Westphal if (!tx_ok) 83872511aabSFlorian Westphal break; 83972511aabSFlorian Westphal 840149f7c71SFlorian Westphal if (!sk_stream_memory_free(ssk) || 84117091708SFlorian Westphal !mptcp_page_frag_refill(ssk, pfrag) || 842149f7c71SFlorian Westphal !mptcp_ext_cache_refill(msk)) { 84372511aabSFlorian Westphal set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 84472511aabSFlorian Westphal tcp_push(ssk, msg->msg_flags, mss_now, 84572511aabSFlorian Westphal tcp_sk(ssk)->nonagle, size_goal); 84672511aabSFlorian Westphal mptcp_set_timeout(sk, ssk); 84772511aabSFlorian Westphal release_sock(ssk); 84872511aabSFlorian Westphal goto restart; 84972511aabSFlorian Westphal } 85072511aabSFlorian Westphal 851fb529e62SFlorian Westphal /* memory is charged to mptcp level socket as well, i.e. 852fb529e62SFlorian Westphal * if msg is very large, mptcp socket may run out of buffer 853fb529e62SFlorian Westphal * space. mptcp_clean_una() will release data that has 854fb529e62SFlorian Westphal * been acked at mptcp level in the mean time, so there is 855fb529e62SFlorian Westphal * a good chance we can continue sending data right away. 85672511aabSFlorian Westphal * 85772511aabSFlorian Westphal * Normally, when the tcp subflow can accept more data, then 85872511aabSFlorian Westphal * so can the MPTCP socket. However, we need to cope with 85972511aabSFlorian Westphal * peers that might lag behind in their MPTCP-level 86072511aabSFlorian Westphal * acknowledgements, i.e. data might have been acked at 86172511aabSFlorian Westphal * tcp level only. So, we must also check the MPTCP socket 86272511aabSFlorian Westphal * limits before we send more data. 863fb529e62SFlorian Westphal */ 864fb529e62SFlorian Westphal if (unlikely(!sk_stream_memory_free(sk))) { 865fb529e62SFlorian Westphal tcp_push(ssk, msg->msg_flags, mss_now, 866fb529e62SFlorian Westphal tcp_sk(ssk)->nonagle, size_goal); 867fb529e62SFlorian Westphal mptcp_clean_una(sk); 868fb529e62SFlorian Westphal if (!sk_stream_memory_free(sk)) { 869fb529e62SFlorian Westphal /* can't send more for now, need to wait for 870fb529e62SFlorian Westphal * MPTCP-level ACKs from peer. 871fb529e62SFlorian Westphal * 872fb529e62SFlorian Westphal * Wakeup will happen via mptcp_clean_una(). 873fb529e62SFlorian Westphal */ 874fb529e62SFlorian Westphal mptcp_set_timeout(sk, ssk); 875fb529e62SFlorian Westphal release_sock(ssk); 876fb529e62SFlorian Westphal goto wait_for_sndbuf; 877fb529e62SFlorian Westphal } 878fb529e62SFlorian Westphal } 8796d0060f6SMat Martineau } 8806d0060f6SMat Martineau 881b51f9b80SPaolo Abeni mptcp_set_timeout(sk, ssk); 88257040755SPaolo Abeni if (copied) { 8836d0060f6SMat Martineau ret = copied; 88457040755SPaolo Abeni tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, 88557040755SPaolo Abeni size_goal); 886b51f9b80SPaolo Abeni 887b51f9b80SPaolo Abeni /* start the timer, if it's not pending */ 888b51f9b80SPaolo Abeni if (!mptcp_timer_pending(sk)) 889b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 89057040755SPaolo Abeni } 8916d0060f6SMat Martineau 8921891c4a0SFlorian Westphal ssk_check_wmem(msk, ssk); 8936d0060f6SMat Martineau release_sock(ssk); 8941954b860SMat Martineau out: 895cec37a6eSPeter Krystad release_sock(sk); 896cec37a6eSPeter Krystad return ret; 897f870fa0bSMat Martineau } 898f870fa0bSMat Martineau 8997a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 9007a6a6cbcSPaolo Abeni { 9017a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 9027a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 9037a6a6cbcSPaolo Abeni 9047a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 9057a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 9067a6a6cbcSPaolo Abeni 9077a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 9087a6a6cbcSPaolo Abeni test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 9097a6a6cbcSPaolo Abeni 9107a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 9117a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 9127a6a6cbcSPaolo Abeni } 9137a6a6cbcSPaolo Abeni 9146771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 9156771bfd9SFlorian Westphal struct msghdr *msg, 9166771bfd9SFlorian Westphal size_t len) 9176771bfd9SFlorian Westphal { 9186771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 9196771bfd9SFlorian Westphal struct sk_buff *skb; 9206771bfd9SFlorian Westphal int copied = 0; 9216771bfd9SFlorian Westphal 9226771bfd9SFlorian Westphal while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 9236771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 9246771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 9256771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 9266771bfd9SFlorian Westphal int err; 9276771bfd9SFlorian Westphal 9286771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 9296771bfd9SFlorian Westphal if (unlikely(err < 0)) { 9306771bfd9SFlorian Westphal if (!copied) 9316771bfd9SFlorian Westphal return err; 9326771bfd9SFlorian Westphal break; 9336771bfd9SFlorian Westphal } 9346771bfd9SFlorian Westphal 9356771bfd9SFlorian Westphal copied += count; 9366771bfd9SFlorian Westphal 9376771bfd9SFlorian Westphal if (count < data_len) { 9386771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 9396771bfd9SFlorian Westphal break; 9406771bfd9SFlorian Westphal } 9416771bfd9SFlorian Westphal 9426771bfd9SFlorian Westphal __skb_unlink(skb, &sk->sk_receive_queue); 9436771bfd9SFlorian Westphal __kfree_skb(skb); 9446771bfd9SFlorian Westphal 9456771bfd9SFlorian Westphal if (copied >= len) 9466771bfd9SFlorian Westphal break; 9476771bfd9SFlorian Westphal } 9486771bfd9SFlorian Westphal 9496771bfd9SFlorian Westphal return copied; 9506771bfd9SFlorian Westphal } 9516771bfd9SFlorian Westphal 9526771bfd9SFlorian Westphal static bool __mptcp_move_skbs(struct mptcp_sock *msk) 9536771bfd9SFlorian Westphal { 9546771bfd9SFlorian Westphal unsigned int moved = 0; 9556771bfd9SFlorian Westphal bool done; 9566771bfd9SFlorian Westphal 9576771bfd9SFlorian Westphal do { 9586771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 9596771bfd9SFlorian Westphal 9606771bfd9SFlorian Westphal if (!ssk) 9616771bfd9SFlorian Westphal break; 9626771bfd9SFlorian Westphal 9636771bfd9SFlorian Westphal lock_sock(ssk); 9646771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 9656771bfd9SFlorian Westphal release_sock(ssk); 9666771bfd9SFlorian Westphal } while (!done); 9676771bfd9SFlorian Westphal 9686771bfd9SFlorian Westphal return moved > 0; 9696771bfd9SFlorian Westphal } 9706771bfd9SFlorian Westphal 971f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 972f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 973f870fa0bSMat Martineau { 974f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 975cec37a6eSPeter Krystad struct socket *ssock; 976cec37a6eSPeter Krystad int copied = 0; 9777a6a6cbcSPaolo Abeni int target; 9787a6a6cbcSPaolo Abeni long timeo; 979f870fa0bSMat Martineau 980f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 981f870fa0bSMat Martineau return -EOPNOTSUPP; 982f870fa0bSMat Martineau 983cec37a6eSPeter Krystad lock_sock(sk); 984cec37a6eSPeter Krystad ssock = __mptcp_tcp_fallback(msk); 9858ab183deSPaolo Abeni if (unlikely(ssock)) { 9868ab183deSPaolo Abeni fallback: 987e154659bSFlorian Westphal release_sock(sk); 988cec37a6eSPeter Krystad pr_debug("fallback-read subflow=%p", 989cec37a6eSPeter Krystad mptcp_subflow_ctx(ssock->sk)); 990cec37a6eSPeter Krystad copied = sock_recvmsg(ssock, msg, flags); 991cec37a6eSPeter Krystad return copied; 992cec37a6eSPeter Krystad } 993cec37a6eSPeter Krystad 9947a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 9957a6a6cbcSPaolo Abeni 9967a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 9977a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 998ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 9997a6a6cbcSPaolo Abeni 10006771bfd9SFlorian Westphal while (len > (size_t)copied) { 10017a6a6cbcSPaolo Abeni int bytes_read; 10027a6a6cbcSPaolo Abeni 10036771bfd9SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 10046771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 10057a6a6cbcSPaolo Abeni if (!copied) 10067a6a6cbcSPaolo Abeni copied = bytes_read; 10076771bfd9SFlorian Westphal goto out_err; 1008cec37a6eSPeter Krystad } 1009cec37a6eSPeter Krystad 10107a6a6cbcSPaolo Abeni copied += bytes_read; 10117a6a6cbcSPaolo Abeni 10126771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue) && 10136771bfd9SFlorian Westphal __mptcp_move_skbs(msk)) 10146771bfd9SFlorian Westphal continue; 10157a6a6cbcSPaolo Abeni 10167a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 10177a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 10187a6a6cbcSPaolo Abeni */ 10197a6a6cbcSPaolo Abeni if (copied >= target) 10207a6a6cbcSPaolo Abeni break; 10217a6a6cbcSPaolo Abeni 10227a6a6cbcSPaolo Abeni if (copied) { 10237a6a6cbcSPaolo Abeni if (sk->sk_err || 10247a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 10257a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 10267a6a6cbcSPaolo Abeni !timeo || 10277a6a6cbcSPaolo Abeni signal_pending(current)) 10287a6a6cbcSPaolo Abeni break; 10297a6a6cbcSPaolo Abeni } else { 10307a6a6cbcSPaolo Abeni if (sk->sk_err) { 10317a6a6cbcSPaolo Abeni copied = sock_error(sk); 10327a6a6cbcSPaolo Abeni break; 10337a6a6cbcSPaolo Abeni } 10347a6a6cbcSPaolo Abeni 1035*5969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 1036*5969856aSPaolo Abeni mptcp_check_for_eof(msk); 1037*5969856aSPaolo Abeni 10387a6a6cbcSPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) 10397a6a6cbcSPaolo Abeni break; 10407a6a6cbcSPaolo Abeni 10417a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 10427a6a6cbcSPaolo Abeni copied = -ENOTCONN; 10437a6a6cbcSPaolo Abeni break; 10447a6a6cbcSPaolo Abeni } 10457a6a6cbcSPaolo Abeni 10467a6a6cbcSPaolo Abeni if (!timeo) { 10477a6a6cbcSPaolo Abeni copied = -EAGAIN; 10487a6a6cbcSPaolo Abeni break; 10497a6a6cbcSPaolo Abeni } 10507a6a6cbcSPaolo Abeni 10517a6a6cbcSPaolo Abeni if (signal_pending(current)) { 10527a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 10537a6a6cbcSPaolo Abeni break; 10547a6a6cbcSPaolo Abeni } 10557a6a6cbcSPaolo Abeni } 10567a6a6cbcSPaolo Abeni 10577a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 10587a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 10590a82e230SPaolo Abeni ssock = __mptcp_tcp_fallback(msk); 10600a82e230SPaolo Abeni if (unlikely(ssock)) 10618ab183deSPaolo Abeni goto fallback; 10627a6a6cbcSPaolo Abeni } 10637a6a6cbcSPaolo Abeni 10646771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue)) { 10656771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 10667a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 10677a6a6cbcSPaolo Abeni 10686771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 10696771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 10707a6a6cbcSPaolo Abeni */ 10716771bfd9SFlorian Westphal if (unlikely(__mptcp_move_skbs(msk))) 10726771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 10736771bfd9SFlorian Westphal } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 10746771bfd9SFlorian Westphal /* data to read but mptcp_wait_data() cleared DATA_READY */ 10757a6a6cbcSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 10767a6a6cbcSPaolo Abeni } 10776771bfd9SFlorian Westphal out_err: 1078cec37a6eSPeter Krystad release_sock(sk); 1079cec37a6eSPeter Krystad return copied; 1080cec37a6eSPeter Krystad } 1081cec37a6eSPeter Krystad 1082b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk) 1083b51f9b80SPaolo Abeni { 1084b51f9b80SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1085b51f9b80SPaolo Abeni 10863b1d6210SPaolo Abeni if (atomic64_read(&msk->snd_una) == msk->write_seq) { 1087b51f9b80SPaolo Abeni mptcp_stop_timer(sk); 10883b1d6210SPaolo Abeni } else { 10893b1d6210SPaolo Abeni set_bit(MPTCP_WORK_RTX, &msk->flags); 10903b1d6210SPaolo Abeni if (schedule_work(&msk->work)) 10913b1d6210SPaolo Abeni sock_hold(sk); 10923b1d6210SPaolo Abeni } 1093b51f9b80SPaolo Abeni } 1094b51f9b80SPaolo Abeni 1095b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 1096b51f9b80SPaolo Abeni { 1097b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 1098b51f9b80SPaolo Abeni icsk_retransmit_timer); 1099b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 1100b51f9b80SPaolo Abeni 1101b51f9b80SPaolo Abeni bh_lock_sock(sk); 1102b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 1103b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1104b51f9b80SPaolo Abeni } else { 1105b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 1106b51f9b80SPaolo Abeni if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1107b51f9b80SPaolo Abeni &sk->sk_tsq_flags)) 1108b51f9b80SPaolo Abeni sock_hold(sk); 1109b51f9b80SPaolo Abeni } 1110b51f9b80SPaolo Abeni bh_unlock_sock(sk); 1111b51f9b80SPaolo Abeni sock_put(sk); 1112b51f9b80SPaolo Abeni } 1113b51f9b80SPaolo Abeni 11143b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 11153b1d6210SPaolo Abeni * level. 11163b1d6210SPaolo Abeni * 11173b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 11183b1d6210SPaolo Abeni */ 11193b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 11203b1d6210SPaolo Abeni { 11213b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 11223b1d6210SPaolo Abeni struct sock *backup = NULL; 11233b1d6210SPaolo Abeni 11243b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 11253b1d6210SPaolo Abeni 11263b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 11273b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 11283b1d6210SPaolo Abeni 11293b1d6210SPaolo Abeni /* still data outstanding at TCP level? Don't retransmit. */ 11303b1d6210SPaolo Abeni if (!tcp_write_queue_empty(ssk)) 11313b1d6210SPaolo Abeni return NULL; 11323b1d6210SPaolo Abeni 11333b1d6210SPaolo Abeni if (subflow->backup) { 11343b1d6210SPaolo Abeni if (!backup) 11353b1d6210SPaolo Abeni backup = ssk; 11363b1d6210SPaolo Abeni continue; 11373b1d6210SPaolo Abeni } 11383b1d6210SPaolo Abeni 11393b1d6210SPaolo Abeni return ssk; 11403b1d6210SPaolo Abeni } 11413b1d6210SPaolo Abeni 11423b1d6210SPaolo Abeni return backup; 11433b1d6210SPaolo Abeni } 11443b1d6210SPaolo Abeni 1145cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 1146cec37a6eSPeter Krystad * (accept). 1147cec37a6eSPeter Krystad * 1148cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 1149cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 1150cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 1151cec37a6eSPeter Krystad * parent socket. 1152cec37a6eSPeter Krystad */ 1153cec37a6eSPeter Krystad static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1154cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, 1155cec37a6eSPeter Krystad long timeout) 1156cec37a6eSPeter Krystad { 1157cec37a6eSPeter Krystad struct socket *sock = READ_ONCE(ssk->sk_socket); 1158cec37a6eSPeter Krystad 1159cec37a6eSPeter Krystad list_del(&subflow->node); 1160cec37a6eSPeter Krystad 1161cec37a6eSPeter Krystad if (sock && sock != sk->sk_socket) { 1162cec37a6eSPeter Krystad /* outgoing subflow */ 1163cec37a6eSPeter Krystad sock_release(sock); 1164cec37a6eSPeter Krystad } else { 1165cec37a6eSPeter Krystad /* incoming subflow */ 1166cec37a6eSPeter Krystad tcp_close(ssk, timeout); 1167cec37a6eSPeter Krystad } 1168f870fa0bSMat Martineau } 1169f870fa0bSMat Martineau 1170dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1171dc24f8b4SPaolo Abeni { 1172dc24f8b4SPaolo Abeni return 0; 1173dc24f8b4SPaolo Abeni } 1174dc24f8b4SPaolo Abeni 117580992017SPaolo Abeni static void mptcp_worker(struct work_struct *work) 117680992017SPaolo Abeni { 117780992017SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 11783b1d6210SPaolo Abeni struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 1179149f7c71SFlorian Westphal int orig_len, orig_offset, mss_now = 0, size_goal = 0; 11803b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 11813b1d6210SPaolo Abeni u64 orig_write_seq; 11823b1d6210SPaolo Abeni size_t copied = 0; 11833b1d6210SPaolo Abeni struct msghdr msg; 11843b1d6210SPaolo Abeni long timeo = 0; 118580992017SPaolo Abeni 118680992017SPaolo Abeni lock_sock(sk); 11873b1d6210SPaolo Abeni mptcp_clean_una(sk); 1188ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 11896771bfd9SFlorian Westphal __mptcp_move_skbs(msk); 11903b1d6210SPaolo Abeni 119159832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 119259832e24SFlorian Westphal mptcp_check_for_eof(msk); 119359832e24SFlorian Westphal 11943b1d6210SPaolo Abeni if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 11953b1d6210SPaolo Abeni goto unlock; 11963b1d6210SPaolo Abeni 11973b1d6210SPaolo Abeni dfrag = mptcp_rtx_head(sk); 11983b1d6210SPaolo Abeni if (!dfrag) 11993b1d6210SPaolo Abeni goto unlock; 12003b1d6210SPaolo Abeni 1201149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1202149f7c71SFlorian Westphal goto reset_unlock; 1203149f7c71SFlorian Westphal 12043b1d6210SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 12053b1d6210SPaolo Abeni if (!ssk) 12063b1d6210SPaolo Abeni goto reset_unlock; 12073b1d6210SPaolo Abeni 12083b1d6210SPaolo Abeni lock_sock(ssk); 12093b1d6210SPaolo Abeni 12103b1d6210SPaolo Abeni msg.msg_flags = MSG_DONTWAIT; 12113b1d6210SPaolo Abeni orig_len = dfrag->data_len; 12123b1d6210SPaolo Abeni orig_offset = dfrag->offset; 12133b1d6210SPaolo Abeni orig_write_seq = dfrag->data_seq; 12143b1d6210SPaolo Abeni while (dfrag->data_len > 0) { 1215149f7c71SFlorian Westphal int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, 1216149f7c71SFlorian Westphal &mss_now, &size_goal); 12173b1d6210SPaolo Abeni if (ret < 0) 12183b1d6210SPaolo Abeni break; 12193b1d6210SPaolo Abeni 1220fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 12213b1d6210SPaolo Abeni copied += ret; 12223b1d6210SPaolo Abeni dfrag->data_len -= ret; 12233b1d6210SPaolo Abeni dfrag->offset += ret; 1224149f7c71SFlorian Westphal 1225149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1226149f7c71SFlorian Westphal break; 12273b1d6210SPaolo Abeni } 12283b1d6210SPaolo Abeni if (copied) 12293b1d6210SPaolo Abeni tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle, 12303b1d6210SPaolo Abeni size_goal); 12313b1d6210SPaolo Abeni 12323b1d6210SPaolo Abeni dfrag->data_seq = orig_write_seq; 12333b1d6210SPaolo Abeni dfrag->offset = orig_offset; 12343b1d6210SPaolo Abeni dfrag->data_len = orig_len; 12353b1d6210SPaolo Abeni 12363b1d6210SPaolo Abeni mptcp_set_timeout(sk, ssk); 12373b1d6210SPaolo Abeni release_sock(ssk); 12383b1d6210SPaolo Abeni 12393b1d6210SPaolo Abeni reset_unlock: 12403b1d6210SPaolo Abeni if (!mptcp_timer_pending(sk)) 12413b1d6210SPaolo Abeni mptcp_reset_timer(sk); 12423b1d6210SPaolo Abeni 12433b1d6210SPaolo Abeni unlock: 124480992017SPaolo Abeni release_sock(sk); 124580992017SPaolo Abeni sock_put(sk); 124680992017SPaolo Abeni } 124780992017SPaolo Abeni 1248784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 1249f870fa0bSMat Martineau { 1250cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1251cec37a6eSPeter Krystad 1252ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 1253ec3edaa7SPeter Krystad 1254cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 1255ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 125618b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 12571891c4a0SFlorian Westphal __set_bit(MPTCP_SEND_SPACE, &msk->flags); 125880992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 1259cec37a6eSPeter Krystad 12608ab183deSPaolo Abeni msk->first = NULL; 1261dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 12628ab183deSPaolo Abeni 12631b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 12641b1c7a0eSPeter Krystad 1265b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 1266b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1267b51f9b80SPaolo Abeni 1268f870fa0bSMat Martineau return 0; 1269f870fa0bSMat Martineau } 1270f870fa0bSMat Martineau 1271784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 1272784325e9SMatthieu Baerts { 1273fc518953SFlorian Westphal struct net *net = sock_net(sk); 1274fc518953SFlorian Westphal int ret; 127518b683bfSPaolo Abeni 1276fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 1277fc518953SFlorian Westphal return -ENOPROTOOPT; 1278fc518953SFlorian Westphal 1279fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1280fc518953SFlorian Westphal return -ENOMEM; 1281fc518953SFlorian Westphal 1282fc518953SFlorian Westphal ret = __mptcp_init_sock(sk); 128318b683bfSPaolo Abeni if (ret) 128418b683bfSPaolo Abeni return ret; 128518b683bfSPaolo Abeni 1286d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 12873f8e0aaeSPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2]; 1288d027236cSPaolo Abeni 128918b683bfSPaolo Abeni return 0; 129018b683bfSPaolo Abeni } 129118b683bfSPaolo Abeni 129218b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 129318b683bfSPaolo Abeni { 129418b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 129518b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 129618b683bfSPaolo Abeni 1297b51f9b80SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1298b51f9b80SPaolo Abeni 129918b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 1300d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1301784325e9SMatthieu Baerts } 1302784325e9SMatthieu Baerts 130380992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 130480992017SPaolo Abeni { 130580992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 130680992017SPaolo Abeni 130780992017SPaolo Abeni if (cancel_work_sync(&msk->work)) 130880992017SPaolo Abeni sock_put(sk); 130980992017SPaolo Abeni } 131080992017SPaolo Abeni 131176c42a29SMat Martineau static void mptcp_subflow_shutdown(struct sock *ssk, int how, 131276c42a29SMat Martineau bool data_fin_tx_enable, u64 data_fin_tx_seq) 131321498490SPeter Krystad { 131421498490SPeter Krystad lock_sock(ssk); 131521498490SPeter Krystad 131621498490SPeter Krystad switch (ssk->sk_state) { 131721498490SPeter Krystad case TCP_LISTEN: 131821498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 131921498490SPeter Krystad break; 132021498490SPeter Krystad /* fall through */ 132121498490SPeter Krystad case TCP_SYN_SENT: 132221498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 132321498490SPeter Krystad break; 132421498490SPeter Krystad default: 132576c42a29SMat Martineau if (data_fin_tx_enable) { 132676c42a29SMat Martineau struct mptcp_subflow_context *subflow; 132776c42a29SMat Martineau 132876c42a29SMat Martineau subflow = mptcp_subflow_ctx(ssk); 132976c42a29SMat Martineau subflow->data_fin_tx_seq = data_fin_tx_seq; 133076c42a29SMat Martineau subflow->data_fin_tx_enable = 1; 133176c42a29SMat Martineau } 133276c42a29SMat Martineau 133321498490SPeter Krystad ssk->sk_shutdown |= how; 133421498490SPeter Krystad tcp_shutdown(ssk, how); 133521498490SPeter Krystad break; 133621498490SPeter Krystad } 133721498490SPeter Krystad 133821498490SPeter Krystad /* Wake up anyone sleeping in poll. */ 133921498490SPeter Krystad ssk->sk_state_change(ssk); 134021498490SPeter Krystad release_sock(ssk); 134121498490SPeter Krystad } 134221498490SPeter Krystad 13438ab183deSPaolo Abeni /* Called with msk lock held, releases such lock before returning */ 13442c22c06cSFlorian Westphal static void mptcp_close(struct sock *sk, long timeout) 1345f870fa0bSMat Martineau { 1346cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 1347f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1348b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 134976c42a29SMat Martineau u64 data_fin_tx_seq; 1350f870fa0bSMat Martineau 13512c22c06cSFlorian Westphal lock_sock(sk); 13522c22c06cSFlorian Westphal 1353f870fa0bSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 1354f870fa0bSMat Martineau 135510f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 135610f6d46cSPaolo Abeni * mptcp_finish_join(). 135710f6d46cSPaolo Abeni */ 135810f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 135910f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 136010f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 1361b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 1362b2c5b614SFlorian Westphal 136376c42a29SMat Martineau data_fin_tx_seq = msk->write_seq; 136476c42a29SMat Martineau 136518b683bfSPaolo Abeni __mptcp_clear_xmit(sk); 136618b683bfSPaolo Abeni 1367b2c5b614SFlorian Westphal release_sock(sk); 1368b2c5b614SFlorian Westphal 1369b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 1370cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1371cec37a6eSPeter Krystad 137276c42a29SMat Martineau subflow->data_fin_tx_seq = data_fin_tx_seq; 137376c42a29SMat Martineau subflow->data_fin_tx_enable = 1; 1374cec37a6eSPeter Krystad __mptcp_close_ssk(sk, ssk, subflow, timeout); 1375f870fa0bSMat Martineau } 1376f870fa0bSMat Martineau 137780992017SPaolo Abeni mptcp_cancel_work(sk); 1378926bdeabSPeter Krystad mptcp_pm_close(msk); 137980992017SPaolo Abeni 13806771bfd9SFlorian Westphal __skb_queue_purge(&sk->sk_receive_queue); 13816771bfd9SFlorian Westphal 1382cec37a6eSPeter Krystad sk_common_release(sk); 1383f870fa0bSMat Martineau } 1384f870fa0bSMat Martineau 1385cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 1386cf7da0d6SPeter Krystad { 1387cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1388cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 1389cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 1390cf7da0d6SPeter Krystad 1391cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 1392cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 1393cf7da0d6SPeter Krystad 1394cf7da0d6SPeter Krystad if (msk6 && ssk6) { 1395cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 1396cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 1397cf7da0d6SPeter Krystad } 1398cf7da0d6SPeter Krystad #endif 1399cf7da0d6SPeter Krystad 1400cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 1401cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 1402cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 1403cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 1404cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 1405cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 1406cf7da0d6SPeter Krystad } 1407cf7da0d6SPeter Krystad 140818b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 140918b683bfSPaolo Abeni { 141042c556feSFlorian Westphal /* Should never be called. 141142c556feSFlorian Westphal * inet_stream_connect() calls ->disconnect, but that 141242c556feSFlorian Westphal * refers to the subflow socket, not the mptcp one. 141342c556feSFlorian Westphal */ 141442c556feSFlorian Westphal WARN_ON_ONCE(1); 141542c556feSFlorian Westphal return 0; 141618b683bfSPaolo Abeni } 141718b683bfSPaolo Abeni 1418b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1419b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 1420b0519de8SFlorian Westphal { 1421b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 1422b0519de8SFlorian Westphal 1423b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 1424b0519de8SFlorian Westphal } 1425b0519de8SFlorian Westphal #endif 1426b0519de8SFlorian Westphal 1427fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 1428cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 1429fca5c82cSPaolo Abeni struct request_sock *req) 1430b0519de8SFlorian Westphal { 143158b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1432b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 143358b09919SPaolo Abeni struct mptcp_sock *msk; 143458b09919SPaolo Abeni u64 ack_seq; 1435b0519de8SFlorian Westphal 1436b0519de8SFlorian Westphal if (!nsk) 1437b0519de8SFlorian Westphal return NULL; 1438b0519de8SFlorian Westphal 1439b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1440b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 1441b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 1442b0519de8SFlorian Westphal #endif 1443b0519de8SFlorian Westphal 144458b09919SPaolo Abeni __mptcp_init_sock(nsk); 144558b09919SPaolo Abeni 144658b09919SPaolo Abeni msk = mptcp_sk(nsk); 144758b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 144858b09919SPaolo Abeni msk->token = subflow_req->token; 144958b09919SPaolo Abeni msk->subflow = NULL; 145058b09919SPaolo Abeni 145158b09919SPaolo Abeni if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) { 14529f5ca6a5SFlorian Westphal nsk->sk_state = TCP_CLOSE; 145358b09919SPaolo Abeni bh_unlock_sock(nsk); 145458b09919SPaolo Abeni 145558b09919SPaolo Abeni /* we can't call into mptcp_close() here - possible BH context 14569f5ca6a5SFlorian Westphal * free the sock directly. 14579f5ca6a5SFlorian Westphal * sk_clone_lock() sets nsk refcnt to two, hence call sk_free() 14589f5ca6a5SFlorian Westphal * too. 145958b09919SPaolo Abeni */ 14609f5ca6a5SFlorian Westphal sk_common_release(nsk); 146158b09919SPaolo Abeni sk_free(nsk); 146258b09919SPaolo Abeni return NULL; 146358b09919SPaolo Abeni } 146458b09919SPaolo Abeni 146558b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 1466cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 1467cfde141eSPaolo Abeni if (mp_opt->mp_capable) { 146858b09919SPaolo Abeni msk->can_ack = true; 1469cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 147058b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 147158b09919SPaolo Abeni ack_seq++; 147258b09919SPaolo Abeni msk->ack_seq = ack_seq; 147358b09919SPaolo Abeni } 14747f20d5fcSPaolo Abeni 14755e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 14767f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 14777f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 147858b09919SPaolo Abeni bh_unlock_sock(nsk); 147958b09919SPaolo Abeni 148058b09919SPaolo Abeni /* keep a single reference */ 148158b09919SPaolo Abeni __sock_put(nsk); 1482b0519de8SFlorian Westphal return nsk; 1483b0519de8SFlorian Westphal } 1484b0519de8SFlorian Westphal 1485cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 1486cf7da0d6SPeter Krystad bool kern) 1487cf7da0d6SPeter Krystad { 1488cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1489cf7da0d6SPeter Krystad struct socket *listener; 1490cf7da0d6SPeter Krystad struct sock *newsk; 1491cf7da0d6SPeter Krystad 1492cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 1493cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 1494cf7da0d6SPeter Krystad *err = -EINVAL; 1495cf7da0d6SPeter Krystad return NULL; 1496cf7da0d6SPeter Krystad } 1497cf7da0d6SPeter Krystad 1498cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 1499cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 1500cf7da0d6SPeter Krystad if (!newsk) 1501cf7da0d6SPeter Krystad return NULL; 1502cf7da0d6SPeter Krystad 1503cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 1504cf7da0d6SPeter Krystad 1505cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 1506cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 1507cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 1508cf7da0d6SPeter Krystad struct sock *ssk = newsk; 1509cf7da0d6SPeter Krystad 1510cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 151158b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 151258b09919SPaolo Abeni 151358b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 151458b09919SPaolo Abeni * subflow_syn_recv_sock() 151558b09919SPaolo Abeni */ 151658b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 151758b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 151858b09919SPaolo Abeni return newsk; 151958b09919SPaolo Abeni } 152058b09919SPaolo Abeni 152158b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 152258b09919SPaolo Abeni sock_hold(new_mptcp_sock); 1523cf7da0d6SPeter Krystad 1524cf7da0d6SPeter Krystad local_bh_disable(); 152558b09919SPaolo Abeni bh_lock_sock(new_mptcp_sock); 1526cf7da0d6SPeter Krystad msk = mptcp_sk(new_mptcp_sock); 15278ab183deSPaolo Abeni msk->first = newsk; 1528cf7da0d6SPeter Krystad 1529cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 1530cf7da0d6SPeter Krystad mptcp_copy_inaddrs(newsk, ssk); 1531cf7da0d6SPeter Krystad list_add(&subflow->node, &msk->conn_list); 1532df1036daSFlorian Westphal inet_sk_state_store(newsk, TCP_ESTABLISHED); 1533cf7da0d6SPeter Krystad 1534cf7da0d6SPeter Krystad bh_unlock_sock(new_mptcp_sock); 1535fc518953SFlorian Westphal 1536fc518953SFlorian Westphal __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 1537cf7da0d6SPeter Krystad local_bh_enable(); 1538fc518953SFlorian Westphal } else { 1539fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 1540fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 1541cf7da0d6SPeter Krystad } 1542cf7da0d6SPeter Krystad 1543cf7da0d6SPeter Krystad return newsk; 1544cf7da0d6SPeter Krystad } 1545cf7da0d6SPeter Krystad 154679c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 154779c0949eSPeter Krystad { 1548c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 1549c9fd9c5fSFlorian Westphal 1550c5c79763SPaolo Abeni mptcp_token_destroy(msk->token); 1551c9fd9c5fSFlorian Westphal if (msk->cached_ext) 1552c9fd9c5fSFlorian Westphal __skb_ext_put(msk->cached_ext); 1553d027236cSPaolo Abeni 1554d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 155579c0949eSPeter Krystad } 155679c0949eSPeter Krystad 1557717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname, 155850e741bbSFlorian Westphal char __user *optval, unsigned int optlen) 1559717e79c8SPeter Krystad { 1560717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1561717e79c8SPeter Krystad struct socket *ssock; 1562717e79c8SPeter Krystad 1563717e79c8SPeter Krystad pr_debug("msk=%p", msk); 1564717e79c8SPeter Krystad 1565717e79c8SPeter Krystad /* @@ the meaning of setsockopt() when the socket is connected and 1566b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 1567b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 1568b6e4a1aeSMat Martineau * is in TCP fallback, when TCP socket options are passed through 1569b6e4a1aeSMat Martineau * to the one remaining subflow. 1570717e79c8SPeter Krystad */ 1571717e79c8SPeter Krystad lock_sock(sk); 1572b6e4a1aeSMat Martineau ssock = __mptcp_tcp_fallback(msk); 1573e154659bSFlorian Westphal release_sock(sk); 1574b6e4a1aeSMat Martineau if (ssock) 1575b6e4a1aeSMat Martineau return tcp_setsockopt(ssock->sk, level, optname, optval, 1576b6e4a1aeSMat Martineau optlen); 157750e741bbSFlorian Westphal 1578b6e4a1aeSMat Martineau return -EOPNOTSUPP; 1579717e79c8SPeter Krystad } 1580717e79c8SPeter Krystad 1581717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname, 158250e741bbSFlorian Westphal char __user *optval, int __user *option) 1583717e79c8SPeter Krystad { 1584717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1585717e79c8SPeter Krystad struct socket *ssock; 1586717e79c8SPeter Krystad 1587717e79c8SPeter Krystad pr_debug("msk=%p", msk); 1588717e79c8SPeter Krystad 1589b6e4a1aeSMat Martineau /* @@ the meaning of setsockopt() when the socket is connected and 1590b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 1591b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 1592b6e4a1aeSMat Martineau * is in TCP fallback, when socket options are passed through 1593b6e4a1aeSMat Martineau * to the one remaining subflow. 1594717e79c8SPeter Krystad */ 1595717e79c8SPeter Krystad lock_sock(sk); 1596b6e4a1aeSMat Martineau ssock = __mptcp_tcp_fallback(msk); 1597e154659bSFlorian Westphal release_sock(sk); 1598b6e4a1aeSMat Martineau if (ssock) 1599b6e4a1aeSMat Martineau return tcp_getsockopt(ssock->sk, level, optname, optval, 1600b6e4a1aeSMat Martineau option); 160150e741bbSFlorian Westphal 1602b6e4a1aeSMat Martineau return -EOPNOTSUPP; 1603717e79c8SPeter Krystad } 1604717e79c8SPeter Krystad 1605b51f9b80SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 1606b51f9b80SPaolo Abeni TCPF_WRITE_TIMER_DEFERRED) 160714c441b5SPaolo Abeni 160814c441b5SPaolo Abeni /* this is very alike tcp_release_cb() but we must handle differently a 160914c441b5SPaolo Abeni * different set of events 161014c441b5SPaolo Abeni */ 161114c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 161214c441b5SPaolo Abeni { 161314c441b5SPaolo Abeni unsigned long flags, nflags; 161414c441b5SPaolo Abeni 161514c441b5SPaolo Abeni do { 161614c441b5SPaolo Abeni flags = sk->sk_tsq_flags; 161714c441b5SPaolo Abeni if (!(flags & MPTCP_DEFERRED_ALL)) 161814c441b5SPaolo Abeni return; 161914c441b5SPaolo Abeni nflags = flags & ~MPTCP_DEFERRED_ALL; 162014c441b5SPaolo Abeni } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 162114c441b5SPaolo Abeni 1622b51f9b80SPaolo Abeni sock_release_ownership(sk); 1623b51f9b80SPaolo Abeni 162414c441b5SPaolo Abeni if (flags & TCPF_DELACK_TIMER_DEFERRED) { 162514c441b5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 162614c441b5SPaolo Abeni struct sock *ssk; 162714c441b5SPaolo Abeni 162814c441b5SPaolo Abeni ssk = mptcp_subflow_recv_lookup(msk); 162914c441b5SPaolo Abeni if (!ssk || !schedule_work(&msk->work)) 163014c441b5SPaolo Abeni __sock_put(sk); 163114c441b5SPaolo Abeni } 1632b51f9b80SPaolo Abeni 1633b51f9b80SPaolo Abeni if (flags & TCPF_WRITE_TIMER_DEFERRED) { 1634b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1635b51f9b80SPaolo Abeni __sock_put(sk); 1636b51f9b80SPaolo Abeni } 163714c441b5SPaolo Abeni } 163814c441b5SPaolo Abeni 1639cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 1640f870fa0bSMat Martineau { 1641f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1642cec37a6eSPeter Krystad struct socket *ssock; 1643f870fa0bSMat Martineau 1644cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1645cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 1646cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 1647cec37a6eSPeter Krystad return -EINVAL; 1648f870fa0bSMat Martineau 1649cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 1650cec37a6eSPeter Krystad } 1651f870fa0bSMat Martineau 1652cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 1653cec37a6eSPeter Krystad { 1654cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 1655cec37a6eSPeter Krystad struct mptcp_sock *msk; 1656cec37a6eSPeter Krystad struct sock *sk; 16576d0060f6SMat Martineau u64 ack_seq; 1658f870fa0bSMat Martineau 1659cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 1660cec37a6eSPeter Krystad sk = subflow->conn; 1661cec37a6eSPeter Krystad msk = mptcp_sk(sk); 1662cec37a6eSPeter Krystad 1663fc518953SFlorian Westphal if (!subflow->mp_capable) { 1664fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 1665fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); 1666fc518953SFlorian Westphal return; 1667fc518953SFlorian Westphal } 1668fc518953SFlorian Westphal 1669648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 1670648ef4b8SMat Martineau 16716d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 16726d0060f6SMat Martineau ack_seq++; 1673648ef4b8SMat Martineau subflow->map_seq = ack_seq; 1674648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 16756d0060f6SMat Martineau subflow->rel_write_seq = 1; 16766d0060f6SMat Martineau 1677cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 1678cec37a6eSPeter Krystad * accessing the field below 1679cec37a6eSPeter Krystad */ 1680cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 1681cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 168279c0949eSPeter Krystad WRITE_ONCE(msk->token, subflow->token); 16836d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 16846d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 1685d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 1686cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 16871b1c7a0eSPeter Krystad 16881b1c7a0eSPeter Krystad mptcp_pm_new_connection(msk, 0); 1689f870fa0bSMat Martineau } 1690f870fa0bSMat Martineau 1691cf7da0d6SPeter Krystad static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 1692cf7da0d6SPeter Krystad { 1693cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 1694cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 1695cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 1696cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 1697cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 1698cf7da0d6SPeter Krystad } 1699cf7da0d6SPeter Krystad 1700f296234cSPeter Krystad bool mptcp_finish_join(struct sock *sk) 1701f296234cSPeter Krystad { 1702f296234cSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1703f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 1704f296234cSPeter Krystad struct sock *parent = (void *)msk; 1705f296234cSPeter Krystad struct socket *parent_sock; 1706ec3edaa7SPeter Krystad bool ret; 1707f296234cSPeter Krystad 1708f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 1709f296234cSPeter Krystad 1710f296234cSPeter Krystad /* mptcp socket already closing? */ 1711f296234cSPeter Krystad if (inet_sk_state_load(parent) != TCP_ESTABLISHED) 1712f296234cSPeter Krystad return false; 1713f296234cSPeter Krystad 1714f296234cSPeter Krystad if (!msk->pm.server_side) 1715f296234cSPeter Krystad return true; 1716f296234cSPeter Krystad 171710f6d46cSPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 171810f6d46cSPaolo Abeni return false; 171910f6d46cSPaolo Abeni 172010f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 172110f6d46cSPaolo Abeni * msk lock here. 172210f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 172310f6d46cSPaolo Abeni * msk status to avoid racing with mptcp_close() 172410f6d46cSPaolo Abeni */ 172510f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 172610f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 172710f6d46cSPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) 172810f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 172910f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 173010f6d46cSPaolo Abeni if (!ret) 173110f6d46cSPaolo Abeni return false; 173210f6d46cSPaolo Abeni 173310f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 173410f6d46cSPaolo Abeni * at close time 173510f6d46cSPaolo Abeni */ 1736f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 1737f296234cSPeter Krystad if (parent_sock && !sk->sk_socket) 1738f296234cSPeter Krystad mptcp_sock_graft(sk, parent_sock); 173964d950aeSChristoph Paasch subflow->map_seq = msk->ack_seq; 174010f6d46cSPaolo Abeni return true; 1741f296234cSPeter Krystad } 1742f296234cSPeter Krystad 17431891c4a0SFlorian Westphal static bool mptcp_memory_free(const struct sock *sk, int wake) 17441891c4a0SFlorian Westphal { 17451891c4a0SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 17461891c4a0SFlorian Westphal 17471891c4a0SFlorian Westphal return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 17481891c4a0SFlorian Westphal } 17491891c4a0SFlorian Westphal 1750f870fa0bSMat Martineau static struct proto mptcp_prot = { 1751f870fa0bSMat Martineau .name = "MPTCP", 1752f870fa0bSMat Martineau .owner = THIS_MODULE, 1753f870fa0bSMat Martineau .init = mptcp_init_sock, 175418b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 1755f870fa0bSMat Martineau .close = mptcp_close, 1756cf7da0d6SPeter Krystad .accept = mptcp_accept, 1757717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 1758717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 1759f870fa0bSMat Martineau .shutdown = tcp_shutdown, 176079c0949eSPeter Krystad .destroy = mptcp_destroy, 1761f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 1762f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 176314c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 1764f870fa0bSMat Martineau .hash = inet_hash, 1765f870fa0bSMat Martineau .unhash = inet_unhash, 1766cec37a6eSPeter Krystad .get_port = mptcp_get_port, 1767d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 1768d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 1769d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 17701891c4a0SFlorian Westphal .stream_memory_free = mptcp_memory_free, 1771d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 1772d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 1773f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 1774f870fa0bSMat Martineau .no_autobind = true, 1775f870fa0bSMat Martineau }; 1776f870fa0bSMat Martineau 17772303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 17782303f994SPeter Krystad { 17792303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 17802303f994SPeter Krystad struct socket *ssock; 1781cf7da0d6SPeter Krystad int err; 17822303f994SPeter Krystad 17832303f994SPeter Krystad lock_sock(sock->sk); 17842303f994SPeter Krystad ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); 17852303f994SPeter Krystad if (IS_ERR(ssock)) { 17862303f994SPeter Krystad err = PTR_ERR(ssock); 17872303f994SPeter Krystad goto unlock; 17882303f994SPeter Krystad } 17892303f994SPeter Krystad 17902303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 1791cf7da0d6SPeter Krystad if (!err) 1792cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 17932303f994SPeter Krystad 17942303f994SPeter Krystad unlock: 17952303f994SPeter Krystad release_sock(sock->sk); 17962303f994SPeter Krystad return err; 17972303f994SPeter Krystad } 17982303f994SPeter Krystad 17992303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 18002303f994SPeter Krystad int addr_len, int flags) 18012303f994SPeter Krystad { 18022303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 18032303f994SPeter Krystad struct socket *ssock; 18042303f994SPeter Krystad int err; 18052303f994SPeter Krystad 18062303f994SPeter Krystad lock_sock(sock->sk); 180741be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 180841be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 180941be81a8SPaolo Abeni * cope with that 181041be81a8SPaolo Abeni */ 181141be81a8SPaolo Abeni ssock = msk->subflow; 181241be81a8SPaolo Abeni goto do_connect; 181341be81a8SPaolo Abeni } 181441be81a8SPaolo Abeni 18152303f994SPeter Krystad ssock = __mptcp_socket_create(msk, TCP_SYN_SENT); 18162303f994SPeter Krystad if (IS_ERR(ssock)) { 18172303f994SPeter Krystad err = PTR_ERR(ssock); 18182303f994SPeter Krystad goto unlock; 18192303f994SPeter Krystad } 18202303f994SPeter Krystad 1821cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 1822cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 1823cf7da0d6SPeter Krystad * TCP option space. 1824cf7da0d6SPeter Krystad */ 1825cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 1826cf7da0d6SPeter Krystad mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0; 1827cf7da0d6SPeter Krystad #endif 1828cf7da0d6SPeter Krystad 182941be81a8SPaolo Abeni do_connect: 18302303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 183141be81a8SPaolo Abeni sock->state = ssock->state; 183241be81a8SPaolo Abeni 183341be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 183441be81a8SPaolo Abeni * subflow_finish_connect() 183541be81a8SPaolo Abeni */ 183641be81a8SPaolo Abeni if (!err || err == EINPROGRESS) 1837cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 183841be81a8SPaolo Abeni else 183941be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 18402303f994SPeter Krystad 18412303f994SPeter Krystad unlock: 18422303f994SPeter Krystad release_sock(sock->sk); 18432303f994SPeter Krystad return err; 18442303f994SPeter Krystad } 18452303f994SPeter Krystad 1846cf7da0d6SPeter Krystad static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr, 1847cf7da0d6SPeter Krystad int peer) 1848cf7da0d6SPeter Krystad { 1849cf7da0d6SPeter Krystad if (sock->sk->sk_prot == &tcp_prot) { 1850cf7da0d6SPeter Krystad /* we are being invoked from __sys_accept4, after 1851cf7da0d6SPeter Krystad * mptcp_accept() has just accepted a non-mp-capable 1852cf7da0d6SPeter Krystad * flow: sk is a tcp_sk, not an mptcp one. 1853cf7da0d6SPeter Krystad * 1854cf7da0d6SPeter Krystad * Hand the socket over to tcp so all further socket ops 1855cf7da0d6SPeter Krystad * bypass mptcp. 1856cf7da0d6SPeter Krystad */ 1857cf7da0d6SPeter Krystad sock->ops = &inet_stream_ops; 1858cf7da0d6SPeter Krystad } 1859cf7da0d6SPeter Krystad 1860cf7da0d6SPeter Krystad return inet_getname(sock, uaddr, peer); 1861cf7da0d6SPeter Krystad } 1862cf7da0d6SPeter Krystad 1863cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1864cf7da0d6SPeter Krystad static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr, 1865cf7da0d6SPeter Krystad int peer) 1866cf7da0d6SPeter Krystad { 1867cf7da0d6SPeter Krystad if (sock->sk->sk_prot == &tcpv6_prot) { 1868cf7da0d6SPeter Krystad /* we are being invoked from __sys_accept4 after 1869cf7da0d6SPeter Krystad * mptcp_accept() has accepted a non-mp-capable 1870cf7da0d6SPeter Krystad * subflow: sk is a tcp_sk, not mptcp. 1871cf7da0d6SPeter Krystad * 1872cf7da0d6SPeter Krystad * Hand the socket over to tcp so all further 1873cf7da0d6SPeter Krystad * socket ops bypass mptcp. 1874cf7da0d6SPeter Krystad */ 1875cf7da0d6SPeter Krystad sock->ops = &inet6_stream_ops; 1876cf7da0d6SPeter Krystad } 1877cf7da0d6SPeter Krystad 1878cf7da0d6SPeter Krystad return inet6_getname(sock, uaddr, peer); 1879cf7da0d6SPeter Krystad } 1880cf7da0d6SPeter Krystad #endif 1881cf7da0d6SPeter Krystad 1882cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 1883cf7da0d6SPeter Krystad { 1884cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 1885cf7da0d6SPeter Krystad struct socket *ssock; 1886cf7da0d6SPeter Krystad int err; 1887cf7da0d6SPeter Krystad 1888cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 1889cf7da0d6SPeter Krystad 1890cf7da0d6SPeter Krystad lock_sock(sock->sk); 1891cf7da0d6SPeter Krystad ssock = __mptcp_socket_create(msk, TCP_LISTEN); 1892cf7da0d6SPeter Krystad if (IS_ERR(ssock)) { 1893cf7da0d6SPeter Krystad err = PTR_ERR(ssock); 1894cf7da0d6SPeter Krystad goto unlock; 1895cf7da0d6SPeter Krystad } 1896cf7da0d6SPeter Krystad 18975e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 18985e20087dSFlorian Westphal 1899cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 1900cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 1901cf7da0d6SPeter Krystad if (!err) 1902cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 1903cf7da0d6SPeter Krystad 1904cf7da0d6SPeter Krystad unlock: 1905cf7da0d6SPeter Krystad release_sock(sock->sk); 1906cf7da0d6SPeter Krystad return err; 1907cf7da0d6SPeter Krystad } 1908cf7da0d6SPeter Krystad 1909cf7da0d6SPeter Krystad static bool is_tcp_proto(const struct proto *p) 1910cf7da0d6SPeter Krystad { 1911cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1912cf7da0d6SPeter Krystad return p == &tcp_prot || p == &tcpv6_prot; 1913cf7da0d6SPeter Krystad #else 1914cf7da0d6SPeter Krystad return p == &tcp_prot; 1915cf7da0d6SPeter Krystad #endif 1916cf7da0d6SPeter Krystad } 1917cf7da0d6SPeter Krystad 1918cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 1919cf7da0d6SPeter Krystad int flags, bool kern) 1920cf7da0d6SPeter Krystad { 1921cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 1922cf7da0d6SPeter Krystad struct socket *ssock; 1923cf7da0d6SPeter Krystad int err; 1924cf7da0d6SPeter Krystad 1925cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 1926cf7da0d6SPeter Krystad 1927cf7da0d6SPeter Krystad lock_sock(sock->sk); 1928cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 1929cf7da0d6SPeter Krystad goto unlock_fail; 1930cf7da0d6SPeter Krystad 1931cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1932cf7da0d6SPeter Krystad if (!ssock) 1933cf7da0d6SPeter Krystad goto unlock_fail; 1934cf7da0d6SPeter Krystad 1935cf7da0d6SPeter Krystad sock_hold(ssock->sk); 1936cf7da0d6SPeter Krystad release_sock(sock->sk); 1937cf7da0d6SPeter Krystad 1938cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 1939cf7da0d6SPeter Krystad if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) { 1940cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 1941cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 1942cf7da0d6SPeter Krystad 1943cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 1944cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 1945cf7da0d6SPeter Krystad */ 1946ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 1947cf7da0d6SPeter Krystad list_for_each_entry(subflow, &msk->conn_list, node) { 1948cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1949cf7da0d6SPeter Krystad 1950cf7da0d6SPeter Krystad if (!ssk->sk_socket) 1951cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 1952cf7da0d6SPeter Krystad } 1953cf7da0d6SPeter Krystad } 1954cf7da0d6SPeter Krystad 1955cf7da0d6SPeter Krystad sock_put(ssock->sk); 1956cf7da0d6SPeter Krystad return err; 1957cf7da0d6SPeter Krystad 1958cf7da0d6SPeter Krystad unlock_fail: 1959cf7da0d6SPeter Krystad release_sock(sock->sk); 1960cf7da0d6SPeter Krystad return -EINVAL; 1961cf7da0d6SPeter Krystad } 1962cf7da0d6SPeter Krystad 19632303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 19642303f994SPeter Krystad struct poll_table_struct *wait) 19652303f994SPeter Krystad { 19661891c4a0SFlorian Westphal struct sock *sk = sock->sk; 19678ab183deSPaolo Abeni struct mptcp_sock *msk; 19681891c4a0SFlorian Westphal struct socket *ssock; 19692303f994SPeter Krystad __poll_t mask = 0; 19702303f994SPeter Krystad 19711891c4a0SFlorian Westphal msk = mptcp_sk(sk); 19721891c4a0SFlorian Westphal lock_sock(sk); 19730b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 19740b4f33deSFlorian Westphal if (!ssock) 19751891c4a0SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 19761891c4a0SFlorian Westphal if (ssock) { 19771891c4a0SFlorian Westphal mask = ssock->ops->poll(file, ssock, wait); 19781891c4a0SFlorian Westphal release_sock(sk); 19791891c4a0SFlorian Westphal return mask; 19801891c4a0SFlorian Westphal } 19811891c4a0SFlorian Westphal 19821891c4a0SFlorian Westphal release_sock(sk); 19831891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 19841891c4a0SFlorian Westphal lock_sock(sk); 19851891c4a0SFlorian Westphal 19861891c4a0SFlorian Westphal if (test_bit(MPTCP_DATA_READY, &msk->flags)) 19871891c4a0SFlorian Westphal mask = EPOLLIN | EPOLLRDNORM; 19881891c4a0SFlorian Westphal if (sk_stream_is_writeable(sk) && 19891891c4a0SFlorian Westphal test_bit(MPTCP_SEND_SPACE, &msk->flags)) 19901891c4a0SFlorian Westphal mask |= EPOLLOUT | EPOLLWRNORM; 19911891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 19921891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 19931891c4a0SFlorian Westphal 19941891c4a0SFlorian Westphal release_sock(sk); 19951891c4a0SFlorian Westphal 19962303f994SPeter Krystad return mask; 19972303f994SPeter Krystad } 19982303f994SPeter Krystad 199921498490SPeter Krystad static int mptcp_shutdown(struct socket *sock, int how) 200021498490SPeter Krystad { 200121498490SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 200221498490SPeter Krystad struct mptcp_subflow_context *subflow; 20030b4f33deSFlorian Westphal struct socket *ssock; 200421498490SPeter Krystad int ret = 0; 200521498490SPeter Krystad 200621498490SPeter Krystad pr_debug("sk=%p, how=%d", msk, how); 200721498490SPeter Krystad 200821498490SPeter Krystad lock_sock(sock->sk); 20090b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 20100b4f33deSFlorian Westphal if (ssock) { 20110b4f33deSFlorian Westphal release_sock(sock->sk); 20120b4f33deSFlorian Westphal return inet_shutdown(ssock, how); 20130b4f33deSFlorian Westphal } 201421498490SPeter Krystad 201521498490SPeter Krystad if (how == SHUT_WR || how == SHUT_RDWR) 201621498490SPeter Krystad inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); 201721498490SPeter Krystad 201821498490SPeter Krystad how++; 201921498490SPeter Krystad 202021498490SPeter Krystad if ((how & ~SHUTDOWN_MASK) || !how) { 202121498490SPeter Krystad ret = -EINVAL; 202221498490SPeter Krystad goto out_unlock; 202321498490SPeter Krystad } 202421498490SPeter Krystad 202521498490SPeter Krystad if (sock->state == SS_CONNECTING) { 202621498490SPeter Krystad if ((1 << sock->sk->sk_state) & 202721498490SPeter Krystad (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 202821498490SPeter Krystad sock->state = SS_DISCONNECTING; 202921498490SPeter Krystad else 203021498490SPeter Krystad sock->state = SS_CONNECTED; 203121498490SPeter Krystad } 203221498490SPeter Krystad 2033ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 203421498490SPeter Krystad mptcp_for_each_subflow(msk, subflow) { 203521498490SPeter Krystad struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 203621498490SPeter Krystad 203776c42a29SMat Martineau mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq); 203821498490SPeter Krystad } 203921498490SPeter Krystad 204021498490SPeter Krystad out_unlock: 204121498490SPeter Krystad release_sock(sock->sk); 204221498490SPeter Krystad 204321498490SPeter Krystad return ret; 204421498490SPeter Krystad } 204521498490SPeter Krystad 2046e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 2047e42f1ac6SFlorian Westphal .family = PF_INET, 2048e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2049e42f1ac6SFlorian Westphal .release = inet_release, 2050e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2051e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2052e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2053e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2054e42f1ac6SFlorian Westphal .getname = mptcp_v4_getname, 2055e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2056e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 2057e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2058e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2059e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2060e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2061e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2062e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 2063e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 2064e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2065e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2066e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 2067e42f1ac6SFlorian Westphal .compat_setsockopt = compat_sock_common_setsockopt, 2068e42f1ac6SFlorian Westphal .compat_getsockopt = compat_sock_common_getsockopt, 2069e42f1ac6SFlorian Westphal #endif 2070e42f1ac6SFlorian Westphal }; 20712303f994SPeter Krystad 2072f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 2073f870fa0bSMat Martineau .type = SOCK_STREAM, 2074f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2075f870fa0bSMat Martineau .prot = &mptcp_prot, 20762303f994SPeter Krystad .ops = &mptcp_stream_ops, 20772303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 2078f870fa0bSMat Martineau }; 2079f870fa0bSMat Martineau 2080784325e9SMatthieu Baerts void mptcp_proto_init(void) 2081f870fa0bSMat Martineau { 20822303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 20832303f994SPeter Krystad 2084d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 2085d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 2086d027236cSPaolo Abeni 20872303f994SPeter Krystad mptcp_subflow_init(); 20881b1c7a0eSPeter Krystad mptcp_pm_init(); 20892303f994SPeter Krystad 2090f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 2091f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 2092f870fa0bSMat Martineau 2093f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 20946771bfd9SFlorian Westphal 20956771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 2096f870fa0bSMat Martineau } 2097f870fa0bSMat Martineau 2098f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2099e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 2100e42f1ac6SFlorian Westphal .family = PF_INET6, 2101e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2102e42f1ac6SFlorian Westphal .release = inet6_release, 2103e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2104e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2105e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2106e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2107e42f1ac6SFlorian Westphal .getname = mptcp_v6_getname, 2108e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2109e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 2110e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2111e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2112e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2113e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2114e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2115e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 2116e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 2117e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2118e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2119e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 21203986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 2121e42f1ac6SFlorian Westphal .compat_setsockopt = compat_sock_common_setsockopt, 2122e42f1ac6SFlorian Westphal .compat_getsockopt = compat_sock_common_getsockopt, 2123e42f1ac6SFlorian Westphal #endif 2124e42f1ac6SFlorian Westphal }; 2125e42f1ac6SFlorian Westphal 2126f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 2127f870fa0bSMat Martineau 212879c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 212979c0949eSPeter Krystad { 213079c0949eSPeter Krystad mptcp_destroy(sk); 213179c0949eSPeter Krystad inet6_destroy_sock(sk); 213279c0949eSPeter Krystad } 213379c0949eSPeter Krystad 2134f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 2135f870fa0bSMat Martineau .type = SOCK_STREAM, 2136f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2137f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 21382303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 2139f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 2140f870fa0bSMat Martineau }; 2141f870fa0bSMat Martineau 2142784325e9SMatthieu Baerts int mptcp_proto_v6_init(void) 2143f870fa0bSMat Martineau { 2144f870fa0bSMat Martineau int err; 2145f870fa0bSMat Martineau 2146f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 2147f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 2148f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 214979c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 2150b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 2151f870fa0bSMat Martineau 2152f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 2153f870fa0bSMat Martineau if (err) 2154f870fa0bSMat Martineau return err; 2155f870fa0bSMat Martineau 2156f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 2157f870fa0bSMat Martineau if (err) 2158f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 2159f870fa0bSMat Martineau 2160f870fa0bSMat Martineau return err; 2161f870fa0bSMat Martineau } 2162f870fa0bSMat Martineau #endif 2163