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> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25644807e3SFlorian Westphal #include <asm/ioctls.h> 26f870fa0bSMat Martineau #include "protocol.h" 27fc518953SFlorian Westphal #include "mib.h" 28f870fa0bSMat Martineau 29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 30e10a9892SGeliang Tang #include <trace/events/mptcp.h> 31e10a9892SGeliang Tang 32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33b0519de8SFlorian Westphal struct mptcp6_sock { 34b0519de8SFlorian Westphal struct mptcp_sock msk; 35b0519de8SFlorian Westphal struct ipv6_pinfo np; 36b0519de8SFlorian Westphal }; 37b0519de8SFlorian Westphal #endif 38b0519de8SFlorian Westphal 39b7f653b2SFlorian Westphal enum { 40b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 412c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 42b7f653b2SFlorian Westphal }; 43b7f653b2SFlorian Westphal 4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45d027236cSPaolo Abeni 46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 4781c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk); 48e16163b6SPaolo Abeni 49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 51b19bc294SPaolo Abeni 526f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 536f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 546f8a612aSFlorian Westphal { 557439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 566f8a612aSFlorian Westphal } 576f8a612aSFlorian Westphal 58d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 590b4f33deSFlorian Westphal { 600b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 610b4f33deSFlorian Westphal 620b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 630b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 640b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 650b4f33deSFlorian Westphal * not an mptcp one. 660b4f33deSFlorian Westphal * 670b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 680b4f33deSFlorian Westphal * bypass mptcp. 690b4f33deSFlorian Westphal */ 700b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 71d2f77c53SPaolo Abeni return true; 720b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 730b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 740b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 75d2f77c53SPaolo Abeni return true; 760b4f33deSFlorian Westphal #endif 770b4f33deSFlorian Westphal } 780b4f33deSFlorian Westphal 79d2f77c53SPaolo Abeni return false; 800b4f33deSFlorian Westphal } 810b4f33deSFlorian Westphal 82fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 832303f994SPeter Krystad { 842303f994SPeter Krystad struct mptcp_subflow_context *subflow; 852303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 862303f994SPeter Krystad struct socket *ssock; 872303f994SPeter Krystad int err; 882303f994SPeter Krystad 896bc1fe7dSPaolo Abeni err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock); 902303f994SPeter Krystad if (err) 91fa68018dSPaolo Abeni return err; 922303f994SPeter Krystad 931b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, ssock->sk); 945b825727SPaolo Abeni WRITE_ONCE(msk->subflow, ssock); 952303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 96cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 97e16163b6SPaolo Abeni sock_hold(ssock->sk); 982303f994SPeter Krystad subflow->request_mptcp = 1; 996f06b4d4SPaolo Abeni subflow->subflow_id = msk->subflow_id++; 1004cf86ae8SPaolo Abeni 1014cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1024cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 103866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 104e1ff9e82SDavide Caratti 105fa68018dSPaolo Abeni return 0; 1062303f994SPeter Krystad } 1072303f994SPeter Krystad 108ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow, 109ddb1a072SPaolo Abeni * eventually allocating it. 110ddb1a072SPaolo Abeni */ 111ddb1a072SPaolo Abeni struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk) 112ddb1a072SPaolo Abeni { 113ddb1a072SPaolo Abeni struct sock *sk = (struct sock *)msk; 114ddb1a072SPaolo Abeni int ret; 115ddb1a072SPaolo Abeni 116ddb1a072SPaolo Abeni if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 117ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 118ddb1a072SPaolo Abeni 119ddb1a072SPaolo Abeni if (!msk->subflow) { 120ddb1a072SPaolo Abeni if (msk->first) 121ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 122ddb1a072SPaolo Abeni 123ddb1a072SPaolo Abeni ret = __mptcp_socket_create(msk); 124ddb1a072SPaolo Abeni if (ret) 125ddb1a072SPaolo Abeni return ERR_PTR(ret); 126ddb1a072SPaolo Abeni 127ddb1a072SPaolo Abeni mptcp_sockopt_sync(msk, msk->first); 128ddb1a072SPaolo Abeni } 129ddb1a072SPaolo Abeni 130ddb1a072SPaolo Abeni return msk->subflow; 131ddb1a072SPaolo Abeni } 132ddb1a072SPaolo Abeni 133ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 134ab174ad8SPaolo Abeni { 135ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 136ab174ad8SPaolo Abeni __kfree_skb(skb); 137ab174ad8SPaolo Abeni } 138ab174ad8SPaolo Abeni 1396511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1406511882cSPaolo Abeni { 1416511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1426511882cSPaolo Abeni } 1436511882cSPaolo Abeni 1448268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1458268ed4cSPaolo Abeni struct sk_buff *from) 1468268ed4cSPaolo Abeni { 1478268ed4cSPaolo Abeni bool fragstolen; 1488268ed4cSPaolo Abeni int delta; 1498268ed4cSPaolo Abeni 1508268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1518268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1528268ed4cSPaolo Abeni return false; 1538268ed4cSPaolo Abeni 15406242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 15506242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 15606242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 157ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1587288ff6eSPaolo Abeni 1597288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1607288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1617288ff6eSPaolo Abeni * negative one 1627288ff6eSPaolo Abeni */ 1638268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1646511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1657288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1667288ff6eSPaolo Abeni 1678268ed4cSPaolo Abeni return true; 1688268ed4cSPaolo Abeni } 1698268ed4cSPaolo Abeni 170ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 171ab174ad8SPaolo Abeni struct sk_buff *from) 1726771bfd9SFlorian Westphal { 173ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 174ab174ad8SPaolo Abeni return false; 175ab174ad8SPaolo Abeni 176ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 177ab174ad8SPaolo Abeni } 178ab174ad8SPaolo Abeni 1796511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1806511882cSPaolo Abeni { 181100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 182100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 1836511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1846511882cSPaolo Abeni } 1856511882cSPaolo Abeni 1866511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1876511882cSPaolo Abeni { 1886511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1896511882cSPaolo Abeni int reclaimable; 1906511882cSPaolo Abeni 1916511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1926511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1936511882cSPaolo Abeni 1946511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 195d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 196d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1976511882cSPaolo Abeni } 1986511882cSPaolo Abeni 1996511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 2006511882cSPaolo Abeni { 2016511882cSPaolo Abeni unsigned int len = skb->truesize; 2026511882cSPaolo Abeni struct sock *sk = skb->sk; 2036511882cSPaolo Abeni 2046511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 2056511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 2066511882cSPaolo Abeni } 2076511882cSPaolo Abeni 20836b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 2096511882cSPaolo Abeni { 2106511882cSPaolo Abeni skb_orphan(skb); 2116511882cSPaolo Abeni skb->sk = sk; 2126511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2136511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2146511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2156511882cSPaolo Abeni } 2166511882cSPaolo Abeni 217ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 218ab174ad8SPaolo Abeni * - use mptcp seqs 219ab174ad8SPaolo Abeni * - don't cope with sacks 220ab174ad8SPaolo Abeni */ 221ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 222ab174ad8SPaolo Abeni { 223ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 224ab174ad8SPaolo Abeni struct rb_node **p, *parent; 225ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 226ab174ad8SPaolo Abeni struct sk_buff *skb1; 227ab174ad8SPaolo Abeni 228ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 229ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 230f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 231ab174ad8SPaolo Abeni 23206242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 23306242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 234fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 235ab174ad8SPaolo Abeni /* out of window */ 236ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 237fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 238fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 239f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 24006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 241ab174ad8SPaolo Abeni return; 242ab174ad8SPaolo Abeni } 243ab174ad8SPaolo Abeni 244ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 24506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 246ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 247ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 248ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 249ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 250ab174ad8SPaolo Abeni goto end; 251ab174ad8SPaolo Abeni } 252ab174ad8SPaolo Abeni 253ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 254ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 255ab174ad8SPaolo Abeni */ 25606242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 25706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 25806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 259ab174ad8SPaolo Abeni return; 26006242e44SPaolo Abeni } 261ab174ad8SPaolo Abeni 262ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 263ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 26406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 265ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 266ab174ad8SPaolo Abeni p = &parent->rb_right; 267ab174ad8SPaolo Abeni goto insert; 268ab174ad8SPaolo Abeni } 269ab174ad8SPaolo Abeni 270ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 271ab174ad8SPaolo Abeni parent = NULL; 272ab174ad8SPaolo Abeni while (*p) { 273ab174ad8SPaolo Abeni parent = *p; 274ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 275ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 276ab174ad8SPaolo Abeni p = &parent->rb_left; 277ab174ad8SPaolo Abeni continue; 278ab174ad8SPaolo Abeni } 279ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 280ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 281ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 282ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 28306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 284ab174ad8SPaolo Abeni return; 285ab174ad8SPaolo Abeni } 286ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 287ab174ad8SPaolo Abeni /* partial overlap: 288ab174ad8SPaolo Abeni * | skb | 289ab174ad8SPaolo Abeni * | skb1 | 290ab174ad8SPaolo Abeni * continue traversing 291ab174ad8SPaolo Abeni */ 292ab174ad8SPaolo Abeni } else { 293ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 294ab174ad8SPaolo Abeni * Replace skb1 with skb. 295ab174ad8SPaolo Abeni */ 296ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 297ab174ad8SPaolo Abeni &msk->out_of_order_queue); 298ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 29906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 300ab174ad8SPaolo Abeni goto merge_right; 301ab174ad8SPaolo Abeni } 302ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 30306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 304ab174ad8SPaolo Abeni return; 305ab174ad8SPaolo Abeni } 306ab174ad8SPaolo Abeni p = &parent->rb_right; 307ab174ad8SPaolo Abeni } 30806242e44SPaolo Abeni 309ab174ad8SPaolo Abeni insert: 310ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 311ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 312ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 313ab174ad8SPaolo Abeni 314ab174ad8SPaolo Abeni merge_right: 315ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 316ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 317ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 318ab174ad8SPaolo Abeni break; 319ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 320ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 32106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 322ab174ad8SPaolo Abeni } 323ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 324ab174ad8SPaolo Abeni if (!skb1) 325ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 326ab174ad8SPaolo Abeni 327ab174ad8SPaolo Abeni end: 328ab174ad8SPaolo Abeni skb_condense(skb); 3296511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3306511882cSPaolo Abeni } 3316511882cSPaolo Abeni 3326511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3336511882cSPaolo Abeni { 3346511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3356511882cSPaolo Abeni int amt, amount; 3366511882cSPaolo Abeni 33769d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3386511882cSPaolo Abeni return true; 3396511882cSPaolo Abeni 34069d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3416511882cSPaolo Abeni amt = sk_mem_pages(size); 342100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3434aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3446511882cSPaolo Abeni return false; 3456511882cSPaolo Abeni 3464aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3476511882cSPaolo Abeni return true; 348ab174ad8SPaolo Abeni } 349ab174ad8SPaolo Abeni 350ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 351ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 352ab174ad8SPaolo Abeni size_t copy_len) 353ab174ad8SPaolo Abeni { 354ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3556771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3564e637c70SFlorian Westphal struct sk_buff *tail; 357b7f653b2SFlorian Westphal bool has_rxtstamp; 3586771bfd9SFlorian Westphal 3596771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3604e637c70SFlorian Westphal 3614e637c70SFlorian Westphal skb_ext_reset(skb); 3624e637c70SFlorian Westphal skb_orphan(skb); 3634e637c70SFlorian Westphal 3649c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3656511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3669c3f94e1SPaolo Abeni goto drop; 36772f96132SPaolo Abeni 368b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 369b7f653b2SFlorian Westphal 370ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 371ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 372ab174ad8SPaolo Abeni * value 373ab174ad8SPaolo Abeni */ 374ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 375ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 376ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 377b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 378ab174ad8SPaolo Abeni 379ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 380ab174ad8SPaolo Abeni /* in sequence */ 38138967f42SPaolo Abeni msk->bytes_received += copy_len; 382917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3834e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3848268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 385ab174ad8SPaolo Abeni return true; 3864e637c70SFlorian Westphal 3876511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3886771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 389ab174ad8SPaolo Abeni return true; 390ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 391ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 392ab174ad8SPaolo Abeni return false; 393ab174ad8SPaolo Abeni } 394ab174ad8SPaolo Abeni 395ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 396ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 397ab174ad8SPaolo Abeni */ 39806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3999c3f94e1SPaolo Abeni drop: 400ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 401ab174ad8SPaolo Abeni return false; 4026771bfd9SFlorian Westphal } 4036771bfd9SFlorian Westphal 40416a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 40516a9a9daSMat Martineau { 40616a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 40716a9a9daSMat Martineau 40816a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 40916a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 41016a9a9daSMat Martineau } 41116a9a9daSMat Martineau 412e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 413e16163b6SPaolo Abeni { 414e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 415e16163b6SPaolo Abeni return; 416e16163b6SPaolo Abeni 417e16163b6SPaolo Abeni sk->sk_state_change(sk); 418e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 419e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 420e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 421e16163b6SPaolo Abeni else 422e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 423e16163b6SPaolo Abeni } 424e16163b6SPaolo Abeni 4256e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4266e628cd3SPaolo Abeni { 4276e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4286e628cd3SPaolo Abeni 42981c1d029SPaolo Abeni return ((1 << sk->sk_state) & 4306e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4316e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4326e628cd3SPaolo Abeni } 4336e628cd3SPaolo Abeni 43416a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 43516a9a9daSMat Martineau { 43616a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 43716a9a9daSMat Martineau 43816a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4396e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 44016a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 44116a9a9daSMat Martineau 44216a9a9daSMat Martineau switch (sk->sk_state) { 44316a9a9daSMat Martineau case TCP_FIN_WAIT1: 44416a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 44516a9a9daSMat Martineau break; 44616a9a9daSMat Martineau case TCP_CLOSING: 44716a9a9daSMat Martineau case TCP_LAST_ACK: 44816a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 44916a9a9daSMat Martineau break; 45016a9a9daSMat Martineau } 45116a9a9daSMat Martineau 452e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 45316a9a9daSMat Martineau } 45416a9a9daSMat Martineau } 45516a9a9daSMat Martineau 4563721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4573721b9b6SMat Martineau { 4583721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4593721b9b6SMat Martineau 4603721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4613721b9b6SMat Martineau ((1 << sk->sk_state) & 4623721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4633721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4643721b9b6SMat Martineau 4653721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4663721b9b6SMat Martineau if (seq) 4673721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4683721b9b6SMat Martineau 4693721b9b6SMat Martineau return true; 4703721b9b6SMat Martineau } 4713721b9b6SMat Martineau } 4723721b9b6SMat Martineau 4733721b9b6SMat Martineau return false; 4743721b9b6SMat Martineau } 4753721b9b6SMat Martineau 476403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk) 4776477dd39SMat Martineau { 4786477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 479877d11f0SMat Martineau u32 retransmits; 4806477dd39SMat Martineau 481877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 482877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 483877d11f0SMat Martineau 484877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4856477dd39SMat Martineau } 4866477dd39SMat Martineau 48733d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4883721b9b6SMat Martineau { 4893721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4903721b9b6SMat Martineau } 4913721b9b6SMat Martineau 49233d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 49333d41c9cSPaolo Abeni { 49433d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 49533d41c9cSPaolo Abeni 49671b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 49771b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 49833d41c9cSPaolo Abeni } 49933d41c9cSPaolo Abeni 50033d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 50133d41c9cSPaolo Abeni { 50233d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 50333d41c9cSPaolo Abeni long tout = 0; 50433d41c9cSPaolo Abeni 50533d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 50633d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 50733d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 50833d41c9cSPaolo Abeni } 50933d41c9cSPaolo Abeni 51076a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 511fd897679SPaolo Abeni { 512fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 51320bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 514fd897679SPaolo Abeni } 515fd897679SPaolo Abeni 516a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 517a6574302SMat Martineau { 518a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 519a6574302SMat Martineau tcp_send_ack(ssk); 520a6574302SMat Martineau } 521a6574302SMat Martineau 522f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5237ed90803SPaolo Abeni { 52475e908c3SPaolo Abeni bool slow; 5257ed90803SPaolo Abeni 52675e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 527a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 52875e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 529fd897679SPaolo Abeni } 530340fa666SMat Martineau 531340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 532340fa666SMat Martineau { 533340fa666SMat Martineau struct mptcp_subflow_context *subflow; 534340fa666SMat Martineau 535340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 536340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 537ea4ca586SPaolo Abeni } 538ea4ca586SPaolo Abeni 539fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 540fd897679SPaolo Abeni { 54175e908c3SPaolo Abeni bool slow; 542fd897679SPaolo Abeni 54375e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 544fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 545fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 54675e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 547fde56eeaSPaolo Abeni } 548fde56eeaSPaolo Abeni 549fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 550fde56eeaSPaolo Abeni { 551fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 552c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 553fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 554fde56eeaSPaolo Abeni 555fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 556fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 557fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 558fde56eeaSPaolo Abeni (rx_empty && ack_pending & 559fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 560ea4ca586SPaolo Abeni } 561fd897679SPaolo Abeni 562fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 563fd897679SPaolo Abeni { 564e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 565fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 566e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 567fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 568fde56eeaSPaolo Abeni bool cleanup, rx_empty; 569e3859603SPaolo Abeni 570fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 571ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 572fd897679SPaolo Abeni 573fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 574fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 575fd897679SPaolo Abeni 576fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 577fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5787ed90803SPaolo Abeni } 5797ed90803SPaolo Abeni } 5807ed90803SPaolo Abeni 5817ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5823721b9b6SMat Martineau { 5833721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5843721b9b6SMat Martineau u64 rcv_data_fin_seq; 5857ed90803SPaolo Abeni bool ret = false; 5863721b9b6SMat Martineau 5873721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5883721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5893721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5903721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5913721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5923721b9b6SMat Martineau * the msk state. 5933721b9b6SMat Martineau * 5943721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5953721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5963721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5973721b9b6SMat Martineau * when catching up. 5983721b9b6SMat Martineau */ 5993721b9b6SMat Martineau 6003721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 601917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 6023721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 6033721b9b6SMat Martineau 6046b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 60516a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 6063721b9b6SMat Martineau 6073721b9b6SMat Martineau switch (sk->sk_state) { 6083721b9b6SMat Martineau case TCP_ESTABLISHED: 6093721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6103721b9b6SMat Martineau break; 6113721b9b6SMat Martineau case TCP_FIN_WAIT1: 6123721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6133721b9b6SMat Martineau break; 6143721b9b6SMat Martineau case TCP_FIN_WAIT2: 6153721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6163721b9b6SMat Martineau break; 6173721b9b6SMat Martineau default: 6183721b9b6SMat Martineau /* Other states not expected */ 6193721b9b6SMat Martineau WARN_ON_ONCE(1); 6203721b9b6SMat Martineau break; 6213721b9b6SMat Martineau } 6223721b9b6SMat Martineau 6237ed90803SPaolo Abeni ret = true; 62481c1d029SPaolo Abeni if (!__mptcp_check_fallback(msk)) 625fd897679SPaolo Abeni mptcp_send_ack(msk); 626e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6273721b9b6SMat Martineau } 6287ed90803SPaolo Abeni return ret; 6293721b9b6SMat Martineau } 6303721b9b6SMat Martineau 6316771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6326771bfd9SFlorian Westphal struct sock *ssk, 6336771bfd9SFlorian Westphal unsigned int *bytes) 6346771bfd9SFlorian Westphal { 6356771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 636600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6376771bfd9SFlorian Westphal unsigned int moved = 0; 6386771bfd9SFlorian Westphal bool more_data_avail; 6396771bfd9SFlorian Westphal struct tcp_sock *tp; 6406771bfd9SFlorian Westphal bool done = false; 64113c7ba0cSFlorian Westphal int sk_rbuf; 64213c7ba0cSFlorian Westphal 64313c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 64413c7ba0cSFlorian Westphal 64513c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 64613c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 64713c7ba0cSFlorian Westphal 64813c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 64913c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 65013c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 65113c7ba0cSFlorian Westphal } 65213c7ba0cSFlorian Westphal } 653600911ffSFlorian Westphal 654ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6556771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6566771bfd9SFlorian Westphal do { 6576771bfd9SFlorian Westphal u32 map_remaining, offset; 6586771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6596771bfd9SFlorian Westphal struct sk_buff *skb; 6606771bfd9SFlorian Westphal bool fin; 6616771bfd9SFlorian Westphal 6626771bfd9SFlorian Westphal /* try to move as much data as available */ 6636771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6646771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6656771bfd9SFlorian Westphal 6666771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 667d9fb8c50SPaolo Abeni if (!skb) { 668d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 669d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 670d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 671d9fb8c50SPaolo Abeni */ 672d9fb8c50SPaolo Abeni if (!moved) 673d9fb8c50SPaolo Abeni done = true; 6746771bfd9SFlorian Westphal break; 675d9fb8c50SPaolo Abeni } 6766771bfd9SFlorian Westphal 677e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 678d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 679d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 680d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 681e1ff9e82SDavide Caratti */ 682e1ff9e82SDavide Caratti map_remaining = skb->len; 683e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 684e1ff9e82SDavide Caratti } 685e1ff9e82SDavide Caratti 6866771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6876771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6886771bfd9SFlorian Westphal if (fin) { 6896771bfd9SFlorian Westphal done = true; 6906771bfd9SFlorian Westphal seq++; 6916771bfd9SFlorian Westphal } 6926771bfd9SFlorian Westphal 6936771bfd9SFlorian Westphal if (offset < skb->len) { 6946771bfd9SFlorian Westphal size_t len = skb->len - offset; 6956771bfd9SFlorian Westphal 6966771bfd9SFlorian Westphal if (tp->urg_data) 6976771bfd9SFlorian Westphal done = true; 6986771bfd9SFlorian Westphal 699ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 7006771bfd9SFlorian Westphal moved += len; 701ab174ad8SPaolo Abeni seq += len; 7026771bfd9SFlorian Westphal 7036771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 7046771bfd9SFlorian Westphal break; 7056771bfd9SFlorian Westphal } else { 7066771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 7076771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 7086771bfd9SFlorian Westphal done = true; 7096771bfd9SFlorian Westphal } 7106771bfd9SFlorian Westphal 7116771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7126771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 713600911ffSFlorian Westphal 71413c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 715600911ffSFlorian Westphal done = true; 716600911ffSFlorian Westphal break; 717600911ffSFlorian Westphal } 7186771bfd9SFlorian Westphal } while (more_data_avail); 7196771bfd9SFlorian Westphal 7206719331cSPaolo Abeni *bytes += moved; 7216771bfd9SFlorian Westphal return done; 7226771bfd9SFlorian Westphal } 7236771bfd9SFlorian Westphal 72487952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 725ab174ad8SPaolo Abeni { 726ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 727ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 728ab174ad8SPaolo Abeni bool moved = false; 729ab174ad8SPaolo Abeni struct rb_node *p; 730ab174ad8SPaolo Abeni u64 end_seq; 731ab174ad8SPaolo Abeni 732ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 73306242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 734ab174ad8SPaolo Abeni while (p) { 735ab174ad8SPaolo Abeni skb = rb_to_skb(p); 736ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 737ab174ad8SPaolo Abeni break; 738ab174ad8SPaolo Abeni 739ab174ad8SPaolo Abeni p = rb_next(p); 740ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 741ab174ad8SPaolo Abeni 742ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 743ab174ad8SPaolo Abeni msk->ack_seq))) { 744ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 74506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 746ab174ad8SPaolo Abeni continue; 747ab174ad8SPaolo Abeni } 748ab174ad8SPaolo Abeni 749ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 750ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 751ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 752ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 753ab174ad8SPaolo Abeni 754ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 75506242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 75606242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 75706242e44SPaolo Abeni delta); 758ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7592c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 760ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 761ab174ad8SPaolo Abeni } 76238967f42SPaolo Abeni msk->bytes_received += end_seq - msk->ack_seq; 763ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 764ab174ad8SPaolo Abeni moved = true; 765ab174ad8SPaolo Abeni } 766ab174ad8SPaolo Abeni return moved; 767ab174ad8SPaolo Abeni } 768ab174ad8SPaolo Abeni 7692e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7702e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7712e52213cSFlorian Westphal */ 77299d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7732e52213cSFlorian Westphal { 7742e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7752e52213cSFlorian Westphal unsigned int moved = 0; 7762e52213cSFlorian Westphal 7772e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 77887952603SPaolo Abeni __mptcp_ofo_queue(msk); 779499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 780499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 781499ada50SPaolo Abeni __mptcp_error_report(sk); 782499ada50SPaolo Abeni else 783e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 784499ada50SPaolo Abeni } 785ab174ad8SPaolo Abeni 786ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 787ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 788ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 789ab174ad8SPaolo Abeni * do it. 790ab174ad8SPaolo Abeni */ 791ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 792ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 79399d1055cSPaolo Abeni return moved > 0; 7942e52213cSFlorian Westphal } 7952e52213cSFlorian Westphal 7962e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 797101f6f85SFlorian Westphal { 7986719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 799101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 80013c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 801101f6f85SFlorian Westphal 802d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 803d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 804d7b1bfd0SPaolo Abeni * more data to the msk receive queue 805d7b1bfd0SPaolo Abeni */ 806d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 807d7b1bfd0SPaolo Abeni return; 808d7b1bfd0SPaolo Abeni 80913c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 81013c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 81113c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 81213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 81313c7ba0cSFlorian Westphal 81499d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 815ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 816ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 81799d1055cSPaolo Abeni return; 818ce599c51SPaolo Abeni } 8192e52213cSFlorian Westphal 82099d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 82199d1055cSPaolo Abeni mptcp_data_lock(sk); 822612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 823101f6f85SFlorian Westphal sk->sk_data_ready(sk); 824612f71d7SPaolo Abeni 82599d1055cSPaolo Abeni mptcp_data_unlock(sk); 82699d1055cSPaolo Abeni } 827101f6f85SFlorian Westphal 82855b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 82955b47ca7SPaolo Abeni { 83055b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 83155b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 83255b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 83355b47ca7SPaolo Abeni } 83455b47ca7SPaolo Abeni 8353e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 836ec3edaa7SPeter Krystad { 8373e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8385cf92bbaSPaolo Abeni 8393e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 84078962489SFlorian Westphal return false; 841ec3edaa7SPeter Krystad 8423e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8433e501490SPaolo Abeni * at close time 8443e501490SPaolo Abeni */ 8453e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8463e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 84778962489SFlorian Westphal 8486f06b4d4SPaolo Abeni mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; 8493e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 85055b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 8513e501490SPaolo Abeni return true; 85278962489SFlorian Westphal } 85378962489SFlorian Westphal 85456a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 85578962489SFlorian Westphal { 8563e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8573e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 85878962489SFlorian Westphal 85956a666c4SPaolo Abeni list_for_each_entry_safe(subflow, tmp, join_list, node) { 8603e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8613e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8623e501490SPaolo Abeni 8633e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8643e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8653e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8663e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 86778962489SFlorian Westphal } 868ec3edaa7SPeter Krystad } 869ec3edaa7SPeter Krystad 870b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 871b51f9b80SPaolo Abeni { 872b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 873b51f9b80SPaolo Abeni } 874b51f9b80SPaolo Abeni 875b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 876b51f9b80SPaolo Abeni { 877b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 878b51f9b80SPaolo Abeni unsigned long tout; 879b51f9b80SPaolo Abeni 880e16163b6SPaolo Abeni /* prevent rescheduling on close */ 881e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 882e16163b6SPaolo Abeni return; 883e16163b6SPaolo Abeni 88433d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 885b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 886b51f9b80SPaolo Abeni } 887b51f9b80SPaolo Abeni 888ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 889ba8f48f7SPaolo Abeni { 890ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 891ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 892ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 893ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 894ba8f48f7SPaolo Abeni */ 895ba8f48f7SPaolo Abeni sock_hold(sk); 896ba8f48f7SPaolo Abeni return true; 897ba8f48f7SPaolo Abeni } 898ba8f48f7SPaolo Abeni return false; 899ba8f48f7SPaolo Abeni } 900ba8f48f7SPaolo Abeni 9017a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9027a6a6cbcSPaolo Abeni { 9037a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9047a6a6cbcSPaolo Abeni 905109cdeb8SGeliang Tang msk_owned_by_me(msk); 9067a6a6cbcSPaolo Abeni 9077a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 90899d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9097a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9107a6a6cbcSPaolo Abeni } 9117a6a6cbcSPaolo Abeni 9127a6a6cbcSPaolo Abeni return NULL; 9137a6a6cbcSPaolo Abeni } 9147a6a6cbcSPaolo Abeni 9153f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 91657040755SPaolo Abeni const struct sk_buff *skb, 91757040755SPaolo Abeni const struct mptcp_ext *mpext) 9186d0060f6SMat Martineau { 91957040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 92057040755SPaolo Abeni return false; 92157040755SPaolo Abeni 9225a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9235a369ca6SPaolo Abeni * mapping has not been xmitted yet 9245a369ca6SPaolo Abeni */ 9255a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9265a369ca6SPaolo Abeni !mpext->frozen; 92757040755SPaolo Abeni } 92857040755SPaolo Abeni 92929249eacSPaolo Abeni /* we can append data to the given data frag if: 93029249eacSPaolo Abeni * - there is space available in the backing page_frag 93129249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 93229249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 93329249eacSPaolo Abeni */ 93418b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 93518b683bfSPaolo Abeni const struct page_frag *pfrag, 93618b683bfSPaolo Abeni const struct mptcp_data_frag *df) 93718b683bfSPaolo Abeni { 93818b683bfSPaolo Abeni return df && pfrag->page == df->page && 939d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 94029249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 94118b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 94218b683bfSPaolo Abeni } 94318b683bfSPaolo Abeni 944d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 94518b683bfSPaolo Abeni { 946d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9477948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 948d027236cSPaolo Abeni } 949d027236cSPaolo Abeni 950d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 951d027236cSPaolo Abeni { 952d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 953d027236cSPaolo Abeni 95418b683bfSPaolo Abeni list_del(&dfrag->list); 955d027236cSPaolo Abeni dfrag_uncharge(sk, len); 95618b683bfSPaolo Abeni put_page(dfrag->page); 95718b683bfSPaolo Abeni } 95818b683bfSPaolo Abeni 9596e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 96018b683bfSPaolo Abeni { 96118b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 96218b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 963e1ff9e82SDavide Caratti u64 snd_una; 964e1ff9e82SDavide Caratti 9657439d687SPaolo Abeni snd_una = msk->snd_una; 96618b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 96718b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 96818b683bfSPaolo Abeni break; 96918b683bfSPaolo Abeni 9701e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 9711e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 9721e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 973d9ca1de8SPaolo Abeni break; 9741e1d9d6fSPaolo Abeni 9751e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 9761e1d9d6fSPaolo Abeni } 9771e1d9d6fSPaolo Abeni 978d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 979d027236cSPaolo Abeni } 980d027236cSPaolo Abeni 9817948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 9827948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 98353eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 98453eb4c38SPaolo Abeni 9851e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 9861e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 9871e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 98853eb4c38SPaolo Abeni goto out; 9891e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 9901e1d9d6fSPaolo Abeni goto out; 9911e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 9921e1d9d6fSPaolo Abeni } 9937948f6ccSFlorian Westphal 9947948f6ccSFlorian Westphal dfrag->data_seq += delta; 99553eb4c38SPaolo Abeni dfrag->offset += delta; 9967948f6ccSFlorian Westphal dfrag->data_len -= delta; 997d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 9987948f6ccSFlorian Westphal 9997948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10007948f6ccSFlorian Westphal } 10017948f6ccSFlorian Westphal 10021e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10031e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10041e1d9d6fSPaolo Abeni msk->recovery = false; 10051e1d9d6fSPaolo Abeni 100653eb4c38SPaolo Abeni out: 10073241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10083241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 100933d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10106e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10116e628cd3SPaolo Abeni } else { 10126e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10136e628cd3SPaolo Abeni } 10146e628cd3SPaolo Abeni } 10156e628cd3SPaolo Abeni 1016417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1017417789dfSPaolo Abeni { 1018765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1019765ff425SPaolo Abeni 1020417789dfSPaolo Abeni __mptcp_clean_una(sk); 1021417789dfSPaolo Abeni mptcp_write_space(sk); 1022417789dfSPaolo Abeni } 1023417789dfSPaolo Abeni 1024b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1025b5941f06SPaolo Abeni { 1026b5941f06SPaolo Abeni mptcp_data_lock(sk); 1027b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1028b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1029b5941f06SPaolo Abeni } 1030b5941f06SPaolo Abeni 1031724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 103218b683bfSPaolo Abeni { 1033d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1034d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1035d9ca1de8SPaolo Abeni bool first = true; 1036d9ca1de8SPaolo Abeni 103718b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1038d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1039d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1040d9ca1de8SPaolo Abeni 1041d9ca1de8SPaolo Abeni if (first) 1042d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1043d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1044d9ca1de8SPaolo Abeni first = false; 1045d9ca1de8SPaolo Abeni } 1046724cfd2eSPaolo Abeni } 1047724cfd2eSPaolo Abeni 1048724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1049724cfd2eSPaolo Abeni * data 1050724cfd2eSPaolo Abeni */ 1051724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1052724cfd2eSPaolo Abeni { 1053724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1054724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1055724cfd2eSPaolo Abeni return true; 1056724cfd2eSPaolo Abeni 1057724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 105818b683bfSPaolo Abeni return false; 105918b683bfSPaolo Abeni } 106018b683bfSPaolo Abeni 106118b683bfSPaolo Abeni static struct mptcp_data_frag * 106218b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 106318b683bfSPaolo Abeni int orig_offset) 106418b683bfSPaolo Abeni { 106518b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 106618b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 106718b683bfSPaolo Abeni 106818b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 106918b683bfSPaolo Abeni dfrag->data_len = 0; 107018b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 107118b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 107218b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1073d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 107418b683bfSPaolo Abeni dfrag->page = pfrag->page; 107518b683bfSPaolo Abeni 107618b683bfSPaolo Abeni return dfrag; 107718b683bfSPaolo Abeni } 107818b683bfSPaolo Abeni 1079caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1080caf971dfSPaolo Abeni int mss_now; 1081caf971dfSPaolo Abeni int size_goal; 1082d9ca1de8SPaolo Abeni u16 limit; 1083d9ca1de8SPaolo Abeni u16 sent; 1084d9ca1de8SPaolo Abeni unsigned int flags; 10851094c6feSPaolo Abeni bool data_lock_held; 1086caf971dfSPaolo Abeni }; 1087caf971dfSPaolo Abeni 1088b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1089b713d006SPaolo Abeni u64 data_seq, int avail_size) 10906f8a612aSFlorian Westphal { 10916f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1092b713d006SPaolo Abeni u64 mptcp_snd_wnd; 10936f8a612aSFlorian Westphal 10946f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 10956f8a612aSFlorian Westphal return avail_size; 10966f8a612aSFlorian Westphal 1097b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1098b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 10996f8a612aSFlorian Westphal 110092be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1101b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 110292be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 110392be2f52SPaolo Abeni } 11046f8a612aSFlorian Westphal 11056f8a612aSFlorian Westphal return avail_size; 11066f8a612aSFlorian Westphal } 11076f8a612aSFlorian Westphal 1108724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1109724cfd2eSPaolo Abeni { 1110724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1111724cfd2eSPaolo Abeni 1112724cfd2eSPaolo Abeni if (!mpext) 1113724cfd2eSPaolo Abeni return false; 1114724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1115724cfd2eSPaolo Abeni return true; 1116724cfd2eSPaolo Abeni } 1117724cfd2eSPaolo Abeni 11186e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1119724cfd2eSPaolo Abeni { 1120724cfd2eSPaolo Abeni struct sk_buff *skb; 1121724cfd2eSPaolo Abeni 11226e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1123724cfd2eSPaolo Abeni if (likely(skb)) { 11246e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1125724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1126a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1127f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1128724cfd2eSPaolo Abeni return skb; 1129724cfd2eSPaolo Abeni } 1130724cfd2eSPaolo Abeni __kfree_skb(skb); 1131724cfd2eSPaolo Abeni } else { 1132724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1133724cfd2eSPaolo Abeni } 1134724cfd2eSPaolo Abeni return NULL; 1135724cfd2eSPaolo Abeni } 1136724cfd2eSPaolo Abeni 1137f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1138724cfd2eSPaolo Abeni { 1139724cfd2eSPaolo Abeni struct sk_buff *skb; 1140724cfd2eSPaolo Abeni 11416e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1142724cfd2eSPaolo Abeni if (!skb) 1143f70cad10SPaolo Abeni return NULL; 1144724cfd2eSPaolo Abeni 1145724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1146f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1147f70cad10SPaolo Abeni return skb; 1148724cfd2eSPaolo Abeni } 11493ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1150724cfd2eSPaolo Abeni kfree_skb(skb); 1151f70cad10SPaolo Abeni return NULL; 1152724cfd2eSPaolo Abeni } 1153724cfd2eSPaolo Abeni 1154f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1155724cfd2eSPaolo Abeni { 11561094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1157724cfd2eSPaolo Abeni 11581094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1159724cfd2eSPaolo Abeni } 1160724cfd2eSPaolo Abeni 1161d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1162d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1163d0cc2987SGeliang Tang */ 1164d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1165d0cc2987SGeliang Tang { 1166d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1167d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1168d0cc2987SGeliang Tang int offset = skb->len - added; 1169d0cc2987SGeliang Tang 1170d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1171d0cc2987SGeliang Tang } 1172d0cc2987SGeliang Tang 11731e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 11741e39e5a3SGeliang Tang struct sock *ssk, 11751e39e5a3SGeliang Tang struct mptcp_ext *mpext) 11761e39e5a3SGeliang Tang { 11771e39e5a3SGeliang Tang if (!mpext) 11781e39e5a3SGeliang Tang return; 11791e39e5a3SGeliang Tang 11801e39e5a3SGeliang Tang mpext->infinite_map = 1; 11811e39e5a3SGeliang Tang mpext->data_len = 0; 11821e39e5a3SGeliang Tang 1183104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 11841e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 11851e39e5a3SGeliang Tang pr_fallback(msk); 1186d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 11871e39e5a3SGeliang Tang } 11881e39e5a3SGeliang Tang 118957040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1190d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1191caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 119257040755SPaolo Abeni { 1193d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1194f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 11956d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 11966f8a612aSFlorian Westphal bool zero_window_probe = false; 11976d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1198f70cad10SPaolo Abeni bool can_coalesce = false; 1199f70cad10SPaolo Abeni bool reuse_skb = true; 1200f70cad10SPaolo Abeni struct sk_buff *skb; 1201f70cad10SPaolo Abeni size_t copy; 1202f70cad10SPaolo Abeni int i; 12036d0060f6SMat Martineau 1204e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1205d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12066d0060f6SMat Martineau 1207f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1208f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1209f70cad10SPaolo Abeni return 0; 1210f70cad10SPaolo Abeni 1211c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1212c886d702SPaolo Abeni return -EAGAIN; 1213c886d702SPaolo Abeni 1214d9ca1de8SPaolo Abeni /* compute send limit */ 1215d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1216f70cad10SPaolo Abeni copy = info->size_goal; 1217f70cad10SPaolo Abeni 121857040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1219f70cad10SPaolo Abeni if (skb && copy > skb->len) { 122057040755SPaolo Abeni /* Limit the write to the size available in the 122157040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 122257040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 122357040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 122457040755SPaolo Abeni * SSN association set here 122557040755SPaolo Abeni */ 1226d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12271094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 122857040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12291094c6feSPaolo Abeni goto alloc_skb; 12301094c6feSPaolo Abeni } 12311094c6feSPaolo Abeni 1232f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1233f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1234657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1235f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1236f70cad10SPaolo Abeni goto alloc_skb; 123715e6ca97SPaolo Abeni } 123818b683bfSPaolo Abeni 1239f70cad10SPaolo Abeni copy -= skb->len; 1240f70cad10SPaolo Abeni } else { 12411094c6feSPaolo Abeni alloc_skb: 1242f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1243f70cad10SPaolo Abeni if (!skb) 1244f70cad10SPaolo Abeni return -ENOMEM; 1245f70cad10SPaolo Abeni 1246f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1247f70cad10SPaolo Abeni reuse_skb = false; 1248f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1249f70cad10SPaolo Abeni } 12501094c6feSPaolo Abeni 12516f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1252b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1253f70cad10SPaolo Abeni if (copy == 0) { 12547439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12557439d687SPaolo Abeni 1256f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 125727728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12586f8a612aSFlorian Westphal return 0; 12596f8a612aSFlorian Westphal } 12606f8a612aSFlorian Westphal 1261f70cad10SPaolo Abeni zero_window_probe = true; 1262f70cad10SPaolo Abeni data_seq = snd_una - 1; 1263f70cad10SPaolo Abeni copy = 1; 126457040755SPaolo Abeni 1265f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1266f70cad10SPaolo Abeni * ssk write queue 1267f70cad10SPaolo Abeni */ 1268f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1269f70cad10SPaolo Abeni } 1270f70cad10SPaolo Abeni 1271f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1272f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 127327728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1274e2223995SPaolo Abeni return -ENOMEM; 127535759383SFlorian Westphal } 127618b683bfSPaolo Abeni 1277f70cad10SPaolo Abeni if (can_coalesce) { 1278f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1279f70cad10SPaolo Abeni } else { 1280f70cad10SPaolo Abeni get_page(dfrag->page); 1281f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 128257040755SPaolo Abeni } 128357040755SPaolo Abeni 1284f70cad10SPaolo Abeni skb->len += copy; 1285f70cad10SPaolo Abeni skb->data_len += copy; 1286f70cad10SPaolo Abeni skb->truesize += copy; 1287f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1288f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1289f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1290f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1291f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1292f70cad10SPaolo Abeni 1293f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1294f70cad10SPaolo Abeni if (reuse_skb) { 1295f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1296f70cad10SPaolo Abeni mpext->data_len += copy; 1297f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1298f70cad10SPaolo Abeni goto out; 1299724cfd2eSPaolo Abeni } 13006d0060f6SMat Martineau 13016d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1302d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13036d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1304f70cad10SPaolo Abeni mpext->data_len = copy; 13056d0060f6SMat Martineau mpext->use_map = 1; 13066d0060f6SMat Martineau mpext->dsn64 = 1; 13076d0060f6SMat Martineau 13086d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13096d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13106d0060f6SMat Martineau mpext->dsn64); 13116d0060f6SMat Martineau 13126f8a612aSFlorian Westphal if (zero_window_probe) { 1313f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13146f8a612aSFlorian Westphal mpext->frozen = 1; 1315d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1316f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13176f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1318d0cc2987SGeliang Tang return 0; 13196f8a612aSFlorian Westphal } 132057040755SPaolo Abeni out: 1321d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1322f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13231e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13241e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13250eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1326f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1327f70cad10SPaolo Abeni return copy; 13286d0060f6SMat Martineau } 13296d0060f6SMat Martineau 1330d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1331d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1332d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1333d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1334d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1335d5f49190SPaolo Abeni 1336d5f49190SPaolo Abeni struct subflow_send_info { 1337d5f49190SPaolo Abeni struct sock *ssk; 13383ce0852cSPaolo Abeni u64 linger_time; 1339d5f49190SPaolo Abeni }; 1340d5f49190SPaolo Abeni 1341ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1342ff5a0b42SPaolo Abeni { 1343ff5a0b42SPaolo Abeni if (!subflow->stale) 1344ff5a0b42SPaolo Abeni return; 1345ff5a0b42SPaolo Abeni 1346ff5a0b42SPaolo Abeni subflow->stale = 0; 1347fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1348ff5a0b42SPaolo Abeni } 1349ff5a0b42SPaolo Abeni 1350ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1351ff5a0b42SPaolo Abeni { 1352ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1353ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1354ff5a0b42SPaolo Abeni 1355ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1356ff5a0b42SPaolo Abeni return false; 1357ff5a0b42SPaolo Abeni 1358ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1359ff5a0b42SPaolo Abeni } 1360ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1361ff5a0b42SPaolo Abeni } 1362ff5a0b42SPaolo Abeni 13633ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13643ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13653ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13663ce0852cSPaolo Abeni 136733d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 136833d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 136933d41c9cSPaolo Abeni * additionally updates the rtx timeout 137033d41c9cSPaolo Abeni */ 13715cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1372f296234cSPeter Krystad { 13733ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1374f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 137533d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 13763ce0852cSPaolo Abeni u32 pace, burst, wmem; 1377d5f49190SPaolo Abeni int i, nr_active = 0; 1378d5f49190SPaolo Abeni struct sock *ssk; 13793ce0852cSPaolo Abeni u64 linger_time; 138033d41c9cSPaolo Abeni long tout = 0; 1381f296234cSPeter Krystad 1382109cdeb8SGeliang Tang msk_owned_by_me(msk); 1383f296234cSPeter Krystad 1384d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1385d5f49190SPaolo Abeni if (!msk->first) 1386f296234cSPeter Krystad return NULL; 1387c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1388c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1389f296234cSPeter Krystad } 1390f296234cSPeter Krystad 1391d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1392d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1393d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 139433d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 139533d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1396d5f49190SPaolo Abeni return msk->last_snd; 139733d41c9cSPaolo Abeni } 1398f296234cSPeter Krystad 1399d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14003ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1401d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14023ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1403d5f49190SPaolo Abeni } 14043ce0852cSPaolo Abeni 1405d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1406e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1407d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1408d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1409f296234cSPeter Krystad continue; 1410d5f49190SPaolo Abeni 141133d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1412d5f49190SPaolo Abeni nr_active += !subflow->backup; 14133ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14143ce0852cSPaolo Abeni if (unlikely(!pace)) { 14153ce0852cSPaolo Abeni /* init pacing rate from socket */ 14163ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14173ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1418d5f49190SPaolo Abeni if (!pace) 1419d5f49190SPaolo Abeni continue; 14203ce0852cSPaolo Abeni } 1421d5f49190SPaolo Abeni 14223ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14233ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1424d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14253ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1426d5f49190SPaolo Abeni } 1427f296234cSPeter Krystad } 142833d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1429f296234cSPeter Krystad 1430d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1431d5f49190SPaolo Abeni if (!nr_active) 14323ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1433d5f49190SPaolo Abeni 14343ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14353ce0852cSPaolo Abeni * faster flow, we need to: 14363ce0852cSPaolo Abeni * - estimate the faster flow linger time 14373ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14383ce0852cSPaolo Abeni * by the faster flow 14393ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14403ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14413ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14423ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14433ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14443ce0852cSPaolo Abeni */ 14453ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1446b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1447d5f49190SPaolo Abeni return NULL; 14483ce0852cSPaolo Abeni 1449b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14503ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1451b713d006SPaolo Abeni if (!burst) { 1452b713d006SPaolo Abeni msk->last_snd = NULL; 1453b713d006SPaolo Abeni return ssk; 1454b713d006SPaolo Abeni } 1455b713d006SPaolo Abeni 14563ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14573ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14583ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14593ce0852cSPaolo Abeni burst + wmem); 14603ce0852cSPaolo Abeni msk->last_snd = ssk; 14613ce0852cSPaolo Abeni msk->snd_burst = burst; 14623ce0852cSPaolo Abeni return ssk; 1463f296234cSPeter Krystad } 1464f296234cSPeter Krystad 1465b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1466d9ca1de8SPaolo Abeni { 1467d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1468d9ca1de8SPaolo Abeni release_sock(ssk); 1469d9ca1de8SPaolo Abeni } 1470d9ca1de8SPaolo Abeni 14710d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14720d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14730d199e43SFlorian Westphal u32 sent) 14740d199e43SFlorian Westphal { 14750d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 14760d199e43SFlorian Westphal 14770d199e43SFlorian Westphal dfrag->already_sent += sent; 14780d199e43SFlorian Westphal 14790d199e43SFlorian Westphal msk->snd_burst -= sent; 14800d199e43SFlorian Westphal 14810d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 14820d199e43SFlorian Westphal 14830d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 14840d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 14850d199e43SFlorian Westphal * old segments. 14860d199e43SFlorian Westphal * 14870d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 14880d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 14890d199e43SFlorian Westphal * that has been handed to the subflow for transmission 14900d199e43SFlorian Westphal * and skip update in case it was old dfrag. 14910d199e43SFlorian Westphal */ 149238967f42SPaolo Abeni if (likely(after64(snd_nxt_new, msk->snd_nxt))) { 149338967f42SPaolo Abeni msk->bytes_sent += snd_nxt_new - msk->snd_nxt; 14940d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 14950d199e43SFlorian Westphal } 149638967f42SPaolo Abeni } 14970d199e43SFlorian Westphal 14988b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 14993241a9c0SFlorian Westphal { 1500e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1501e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15023241a9c0SFlorian Westphal } 15033241a9c0SFlorian Westphal 1504ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1505d9ca1de8SPaolo Abeni { 1506d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1507d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1508d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1509d9ca1de8SPaolo Abeni .flags = flags, 1510d9ca1de8SPaolo Abeni }; 15110522b424SGeliang Tang bool do_check_data_fin = false; 1512d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15130522b424SGeliang Tang int len; 1514d9ca1de8SPaolo Abeni 1515d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1516d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1517d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1518d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1519d9ca1de8SPaolo Abeni while (len > 0) { 1520d9ca1de8SPaolo Abeni int ret = 0; 1521d9ca1de8SPaolo Abeni 1522d9ca1de8SPaolo Abeni prev_ssk = ssk; 15235cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1524d9ca1de8SPaolo Abeni 15259758f40eSPaolo Abeni /* First check. If the ssk has changed since 15269758f40eSPaolo Abeni * the last round, release prev_ssk 1527d9ca1de8SPaolo Abeni */ 1528d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1529b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1530d9ca1de8SPaolo Abeni if (!ssk) 1531d9ca1de8SPaolo Abeni goto out; 1532d9ca1de8SPaolo Abeni 15339758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15349758f40eSPaolo Abeni * from the previous one, otherwise we are still 15359758f40eSPaolo Abeni * helding the relevant lock 15369758f40eSPaolo Abeni */ 15379758f40eSPaolo Abeni if (ssk != prev_ssk) 1538d9ca1de8SPaolo Abeni lock_sock(ssk); 1539d9ca1de8SPaolo Abeni 1540d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1541d9ca1de8SPaolo Abeni if (ret <= 0) { 1542c886d702SPaolo Abeni if (ret == -EAGAIN) 1543c886d702SPaolo Abeni continue; 1544b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1545d9ca1de8SPaolo Abeni goto out; 1546d9ca1de8SPaolo Abeni } 1547d9ca1de8SPaolo Abeni 15480522b424SGeliang Tang do_check_data_fin = true; 1549d9ca1de8SPaolo Abeni info.sent += ret; 1550d9ca1de8SPaolo Abeni len -= ret; 15510d199e43SFlorian Westphal 15520d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1553d9ca1de8SPaolo Abeni } 1554d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1555d9ca1de8SPaolo Abeni } 1556d9ca1de8SPaolo Abeni 1557d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1558d9ca1de8SPaolo Abeni if (ssk) 1559b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1560d9ca1de8SPaolo Abeni 1561d9ca1de8SPaolo Abeni out: 156233d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1563d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1564d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 15650522b424SGeliang Tang if (do_check_data_fin) 156681c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 1567d9ca1de8SPaolo Abeni } 1568d9ca1de8SPaolo Abeni 156973a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 15706e628cd3SPaolo Abeni { 15716e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15721094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 15731094c6feSPaolo Abeni .data_lock_held = true, 15741094c6feSPaolo Abeni }; 15756e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1576b19bc294SPaolo Abeni struct sock *xmit_ssk; 15776e628cd3SPaolo Abeni int len, copied = 0; 15786e628cd3SPaolo Abeni 15796e628cd3SPaolo Abeni info.flags = 0; 15806e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 15816e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 15826e628cd3SPaolo Abeni info.limit = dfrag->data_len; 15836e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 15846e628cd3SPaolo Abeni while (len > 0) { 15856e628cd3SPaolo Abeni int ret = 0; 15866e628cd3SPaolo Abeni 158773a0052aSGeliang Tang /* check for a different subflow usage only after 1588b19bc294SPaolo Abeni * spooling the first chunk of data 1589b19bc294SPaolo Abeni */ 159000df24f1SGeliang Tang xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1591b19bc294SPaolo Abeni if (!xmit_ssk) 1592b19bc294SPaolo Abeni goto out; 1593b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1594bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1595bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1596b19bc294SPaolo Abeni goto out; 1597b19bc294SPaolo Abeni } 1598b19bc294SPaolo Abeni 15996e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16006e628cd3SPaolo Abeni if (ret <= 0) 16016e628cd3SPaolo Abeni goto out; 16026e628cd3SPaolo Abeni 16036e628cd3SPaolo Abeni info.sent += ret; 16046e628cd3SPaolo Abeni copied += ret; 16056e628cd3SPaolo Abeni len -= ret; 1606b19bc294SPaolo Abeni first = false; 16070d199e43SFlorian Westphal 16080d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16096e628cd3SPaolo Abeni } 16106e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16116e628cd3SPaolo Abeni } 16126e628cd3SPaolo Abeni 16136e628cd3SPaolo Abeni out: 16146e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16156e628cd3SPaolo Abeni * not going to flush it via release_sock() 16166e628cd3SPaolo Abeni */ 16176e628cd3SPaolo Abeni if (copied) { 16186e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16196e628cd3SPaolo Abeni info.size_goal); 1620d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1621d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1622d09d818eSPaolo Abeni 16236e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16246e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16256e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16266e628cd3SPaolo Abeni } 16276e628cd3SPaolo Abeni } 16286e628cd3SPaolo Abeni 16295cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16305cf92bbaSPaolo Abeni { 16315cf92bbaSPaolo Abeni /* enable autotune */ 16325cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16335cf92bbaSPaolo Abeni 16345cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16355cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16365cf92bbaSPaolo Abeni } 16375cf92bbaSPaolo Abeni 16387d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 16397d803344SPaolo Abeni 1640a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1641fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1642fa9e5746SPaolo Abeni { 1643fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1644fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1645ddb1a072SPaolo Abeni struct socket *ssock; 1646a2702a07SPaolo Abeni struct sock *ssk; 1647fa9e5746SPaolo Abeni int ret; 1648fa9e5746SPaolo Abeni 1649a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1650a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1651a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1652a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1653a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1654a2702a07SPaolo Abeni */ 1655ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 1656ddb1a072SPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 1657ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 1658ddb1a072SPaolo Abeni return PTR_ERR(ssock); 1659ddb1a072SPaolo Abeni } 1660a2702a07SPaolo Abeni if (!msk->first) 1661a2702a07SPaolo Abeni return -EINVAL; 1662a2702a07SPaolo Abeni 1663a2702a07SPaolo Abeni ssk = msk->first; 1664a2702a07SPaolo Abeni 1665fa9e5746SPaolo Abeni lock_sock(ssk); 1666fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 16677d803344SPaolo Abeni msk->fastopening = 1; 1668fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 16697d803344SPaolo Abeni msk->fastopening = 0; 1670fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1671fa9e5746SPaolo Abeni release_sock(ssk); 1672fa9e5746SPaolo Abeni 1673fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1674fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1675fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1676fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1677fa9e5746SPaolo Abeni 1678fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1679fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1680fa9e5746SPaolo Abeni */ 1681fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1682fa9e5746SPaolo Abeni *copied_syn = 0; 16837d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 1684c2b2ae39SPaolo Abeni /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1685c2b2ae39SPaolo Abeni * __inet_stream_connect() can fail, due to looking check, 1686c2b2ae39SPaolo Abeni * see mptcp_disconnect(). 1687c2b2ae39SPaolo Abeni * Attempt it again outside the problematic scope. 1688c2b2ae39SPaolo Abeni */ 1689c2b2ae39SPaolo Abeni if (!mptcp_disconnect(sk, 0)) 1690c2b2ae39SPaolo Abeni sk->sk_socket->state = SS_UNCONNECTED; 1691fa9e5746SPaolo Abeni } 1692a2702a07SPaolo Abeni inet_sk(sk)->defer_connect = 0; 1693fa9e5746SPaolo Abeni 1694fa9e5746SPaolo Abeni return ret; 1695fa9e5746SPaolo Abeni } 1696fa9e5746SPaolo Abeni 1697f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1698f870fa0bSMat Martineau { 1699f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 170017091708SFlorian Westphal struct page_frag *pfrag; 17016d0060f6SMat Martineau size_t copied = 0; 1702caf971dfSPaolo Abeni int ret = 0; 17036d0060f6SMat Martineau long timeo; 1704f870fa0bSMat Martineau 1705987858e5SPaolo Abeni /* silently ignore everything else */ 17061e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1707987858e5SPaolo Abeni 17086511882cSPaolo Abeni lock_sock(sk); 17091954b860SMat Martineau 1710a2702a07SPaolo Abeni if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) { 1711d98a82a6SDmytro Shytyi int copied_syn = 0; 1712d98a82a6SDmytro Shytyi 1713a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1714d98a82a6SDmytro Shytyi copied += copied_syn; 1715fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1716d98a82a6SDmytro Shytyi goto out; 1717fa9e5746SPaolo Abeni else if (ret) 171869800e51SPaolo Abeni goto do_error; 1719d98a82a6SDmytro Shytyi } 1720d98a82a6SDmytro Shytyi 17211954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17221954b860SMat Martineau 17231954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17241954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17251954b860SMat Martineau if (ret) 172669800e51SPaolo Abeni goto do_error; 17271954b860SMat Martineau } 17281954b860SMat Martineau 172969800e51SPaolo Abeni ret = -EPIPE; 173069800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 173169800e51SPaolo Abeni goto do_error; 173269800e51SPaolo Abeni 173317091708SFlorian Westphal pfrag = sk_page_frag(sk); 173418b683bfSPaolo Abeni 1735d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1736724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1737d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1738d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1739d9ca1de8SPaolo Abeni size_t psize, offset; 1740d9ca1de8SPaolo Abeni 1741d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1742d9ca1de8SPaolo Abeni * page allocator 1743fb529e62SFlorian Westphal */ 1744d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1745d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1746d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1747d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1748d9ca1de8SPaolo Abeni goto wait_for_memory; 17496e628cd3SPaolo Abeni 1750d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1751d9ca1de8SPaolo Abeni goto wait_for_memory; 1752d9ca1de8SPaolo Abeni 1753d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1754d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1755fb529e62SFlorian Westphal } 1756fb529e62SFlorian Westphal 1757d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1758d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1759d9ca1de8SPaolo Abeni * anyway 1760d9ca1de8SPaolo Abeni */ 1761d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1762d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1763d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1764724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17658ce568edSPaolo Abeni 17666511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1767d9ca1de8SPaolo Abeni goto wait_for_memory; 1768d9ca1de8SPaolo Abeni 1769d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1770d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1771d9ca1de8SPaolo Abeni ret = -EFAULT; 177269800e51SPaolo Abeni goto do_error; 1773d9ca1de8SPaolo Abeni } 1774d9ca1de8SPaolo Abeni 1775d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17766511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1777d9ca1de8SPaolo Abeni copied += psize; 1778d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1779d9ca1de8SPaolo Abeni frag_truesize += psize; 1780d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1781d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1782d9ca1de8SPaolo Abeni 1783d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1784d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1785d9ca1de8SPaolo Abeni */ 1786d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1787d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1788d9ca1de8SPaolo Abeni get_page(dfrag->page); 1789d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1790d9ca1de8SPaolo Abeni if (!msk->first_pending) 1791d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1792d9ca1de8SPaolo Abeni } 1793e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1794d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1795d9ca1de8SPaolo Abeni !dfrag_collapsed); 1796d9ca1de8SPaolo Abeni 1797d9ca1de8SPaolo Abeni continue; 1798d9ca1de8SPaolo Abeni 1799d9ca1de8SPaolo Abeni wait_for_memory: 18005cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1801c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1802f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1803f296234cSPeter Krystad if (ret) 180469800e51SPaolo Abeni goto do_error; 1805cec37a6eSPeter Krystad } 1806cec37a6eSPeter Krystad 180713e16037SPaolo Abeni if (copied) 1808c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1809da51aef5SPaolo Abeni 18101954b860SMat Martineau out: 1811cec37a6eSPeter Krystad release_sock(sk); 181269800e51SPaolo Abeni return copied; 181369800e51SPaolo Abeni 181469800e51SPaolo Abeni do_error: 181569800e51SPaolo Abeni if (copied) 181669800e51SPaolo Abeni goto out; 181769800e51SPaolo Abeni 181869800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 181969800e51SPaolo Abeni goto out; 1820f870fa0bSMat Martineau } 1821f870fa0bSMat Martineau 18226771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18236771bfd9SFlorian Westphal struct msghdr *msg, 1824b7f653b2SFlorian Westphal size_t len, int flags, 1825b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1826b7f653b2SFlorian Westphal int *cmsg_flags) 18276771bfd9SFlorian Westphal { 1828ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18296771bfd9SFlorian Westphal int copied = 0; 18306771bfd9SFlorian Westphal 1831ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18326771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18336771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18346771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18356771bfd9SFlorian Westphal int err; 18366771bfd9SFlorian Westphal 1837d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18386771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18396771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18406771bfd9SFlorian Westphal if (!copied) 18416771bfd9SFlorian Westphal return err; 18426771bfd9SFlorian Westphal break; 18436771bfd9SFlorian Westphal } 1844d976092cSPaolo Abeni } 18456771bfd9SFlorian Westphal 1846b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1847b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1848b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1849b7f653b2SFlorian Westphal } 1850b7f653b2SFlorian Westphal 18516771bfd9SFlorian Westphal copied += count; 18526771bfd9SFlorian Westphal 18536771bfd9SFlorian Westphal if (count < data_len) { 18542c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18556771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18562c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18572c9e7765SFlorian Westphal } 18586771bfd9SFlorian Westphal break; 18596771bfd9SFlorian Westphal } 18606771bfd9SFlorian Westphal 1861ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 186287952603SPaolo Abeni /* we will bulk release the skb memory later */ 186387952603SPaolo Abeni skb->destructor = NULL; 1864ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 186587952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18666771bfd9SFlorian Westphal __kfree_skb(skb); 1867ca4fb892SYonglong Li } 18686771bfd9SFlorian Westphal 18696771bfd9SFlorian Westphal if (copied >= len) 18706771bfd9SFlorian Westphal break; 18716771bfd9SFlorian Westphal } 18726771bfd9SFlorian Westphal 18736771bfd9SFlorian Westphal return copied; 18746771bfd9SFlorian Westphal } 18756771bfd9SFlorian Westphal 1876a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1877a6b118feSFlorian Westphal * 1878a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1879a6b118feSFlorian Westphal */ 1880a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1881a6b118feSFlorian Westphal { 1882a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1883a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1884a6b118feSFlorian Westphal u32 time, advmss = 1; 1885a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1886a6b118feSFlorian Westphal 1887109cdeb8SGeliang Tang msk_owned_by_me(msk); 1888a6b118feSFlorian Westphal 1889a6b118feSFlorian Westphal if (copied <= 0) 1890a6b118feSFlorian Westphal return; 1891a6b118feSFlorian Westphal 1892a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1893a6b118feSFlorian Westphal 1894a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1895a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1896a6b118feSFlorian Westphal 1897a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1898a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1899a6b118feSFlorian Westphal return; 1900a6b118feSFlorian Westphal 1901a6b118feSFlorian Westphal rtt_us = 0; 1902a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1903a6b118feSFlorian Westphal const struct tcp_sock *tp; 1904a6b118feSFlorian Westphal u64 sf_rtt_us; 1905a6b118feSFlorian Westphal u32 sf_advmss; 1906a6b118feSFlorian Westphal 1907a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1908a6b118feSFlorian Westphal 1909a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1910a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1911a6b118feSFlorian Westphal 1912a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1913a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1914a6b118feSFlorian Westphal } 1915a6b118feSFlorian Westphal 1916a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1917a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1918a6b118feSFlorian Westphal return; 1919a6b118feSFlorian Westphal 1920a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1921a6b118feSFlorian Westphal goto new_measure; 1922a6b118feSFlorian Westphal 192378047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1924a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1925a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1926a6b118feSFlorian Westphal u64 rcvwin, grow; 1927a6b118feSFlorian Westphal 1928a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1929a6b118feSFlorian Westphal 1930a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1931a6b118feSFlorian Westphal 1932a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1933a6b118feSFlorian Westphal rcvwin += (grow << 1); 1934a6b118feSFlorian Westphal 1935a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1936a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1937a6b118feSFlorian Westphal rcvmem += 128; 1938a6b118feSFlorian Westphal 1939a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1940a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 194102739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1942a6b118feSFlorian Westphal 1943a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1944a6b118feSFlorian Westphal u32 window_clamp; 1945a6b118feSFlorian Westphal 1946a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1947a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1948a6b118feSFlorian Westphal 1949a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1950a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1951a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1952a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1953a6b118feSFlorian Westphal */ 1954a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1955a6b118feSFlorian Westphal struct sock *ssk; 1956c76c6956SPaolo Abeni bool slow; 1957a6b118feSFlorian Westphal 1958a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1959c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1960a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1961a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1962c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1963c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1964a6b118feSFlorian Westphal } 1965a6b118feSFlorian Westphal } 1966a6b118feSFlorian Westphal } 1967a6b118feSFlorian Westphal 1968a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1969a6b118feSFlorian Westphal new_measure: 1970a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1971a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1972a6b118feSFlorian Westphal } 1973a6b118feSFlorian Westphal 197487952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 197587952603SPaolo Abeni { 197687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 197787952603SPaolo Abeni 197887952603SPaolo Abeni if (!msk->rmem_released) 197987952603SPaolo Abeni return; 198087952603SPaolo Abeni 198187952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19826511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1983ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 198487952603SPaolo Abeni } 198587952603SPaolo Abeni 198687952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 198787952603SPaolo Abeni { 198887952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 198987952603SPaolo Abeni 199087952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 199187952603SPaolo Abeni } 199287952603SPaolo Abeni 1993e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19946771bfd9SFlorian Westphal { 199587952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19966771bfd9SFlorian Westphal unsigned int moved = 0; 199787952603SPaolo Abeni bool ret, done; 1998d5f49190SPaolo Abeni 19996771bfd9SFlorian Westphal do { 20006771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 200165f49fe7SFlorian Westphal bool slowpath; 20026771bfd9SFlorian Westphal 200387952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 200487952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 200587952603SPaolo Abeni * that is an unlikely slow path. 200687952603SPaolo Abeni */ 200787952603SPaolo Abeni if (likely(!ssk)) 20086771bfd9SFlorian Westphal break; 20096771bfd9SFlorian Westphal 201065f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 201187952603SPaolo Abeni mptcp_data_lock(sk); 2012e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20136771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 201487952603SPaolo Abeni mptcp_data_unlock(sk); 2015499ada50SPaolo Abeni 2016499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2017499ada50SPaolo Abeni __mptcp_error_report(sk); 201865f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20196771bfd9SFlorian Westphal } while (!done); 20206771bfd9SFlorian Westphal 202187952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 202287952603SPaolo Abeni ret = moved > 0; 202387952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 202487952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 202587952603SPaolo Abeni mptcp_data_lock(sk); 202687952603SPaolo Abeni __mptcp_update_rmem(sk); 202787952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 202887952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 202987952603SPaolo Abeni mptcp_data_unlock(sk); 2030ab174ad8SPaolo Abeni } 203187952603SPaolo Abeni if (ret) 203287952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 203387952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20346771bfd9SFlorian Westphal } 20356771bfd9SFlorian Westphal 20362c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20372c9e7765SFlorian Westphal { 20382c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20392c9e7765SFlorian Westphal const struct sk_buff *skb; 20402c9e7765SFlorian Westphal 20412c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20422c9e7765SFlorian Westphal if (skb) { 20432c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20442c9e7765SFlorian Westphal 20452c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20462c9e7765SFlorian Westphal return INT_MAX; 20472c9e7765SFlorian Westphal 20482c9e7765SFlorian Westphal return (unsigned int)hint_val; 20492c9e7765SFlorian Westphal } 20502c9e7765SFlorian Westphal 20512c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20522c9e7765SFlorian Westphal return 1; 20532c9e7765SFlorian Westphal 20542c9e7765SFlorian Westphal return 0; 20552c9e7765SFlorian Westphal } 20562c9e7765SFlorian Westphal 2057f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2058ec095263SOliver Hartkopp int flags, int *addr_len) 2059f870fa0bSMat Martineau { 2060f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2061b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2062b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20637a6a6cbcSPaolo Abeni int target; 20647a6a6cbcSPaolo Abeni long timeo; 2065f870fa0bSMat Martineau 2066cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2067cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2068cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2069cb9d80f4SPaolo Abeni 20706511882cSPaolo Abeni lock_sock(sk); 2071fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2072fd897679SPaolo Abeni copied = -ENOTCONN; 2073fd897679SPaolo Abeni goto out_err; 2074fd897679SPaolo Abeni } 2075fd897679SPaolo Abeni 2076ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20777a6a6cbcSPaolo Abeni 20787a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20797a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20807a6a6cbcSPaolo Abeni 20812c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20822c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20832c9e7765SFlorian Westphal 208405e3eceaSEric Dumazet while (copied < len) { 2085e3859603SPaolo Abeni int bytes_read; 20867a6a6cbcSPaolo Abeni 2087b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20886771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20897a6a6cbcSPaolo Abeni if (!copied) 20907a6a6cbcSPaolo Abeni copied = bytes_read; 20916771bfd9SFlorian Westphal goto out_err; 2092cec37a6eSPeter Krystad } 2093cec37a6eSPeter Krystad 20947a6a6cbcSPaolo Abeni copied += bytes_read; 20957a6a6cbcSPaolo Abeni 2096ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2097fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2098ea4ca586SPaolo Abeni 2099e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2100e3859603SPaolo Abeni continue; 2101e3859603SPaolo Abeni 21027a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21037a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21047a6a6cbcSPaolo Abeni */ 21057a6a6cbcSPaolo Abeni if (copied >= target) 21067a6a6cbcSPaolo Abeni break; 21077a6a6cbcSPaolo Abeni 21087a6a6cbcSPaolo Abeni if (copied) { 21097a6a6cbcSPaolo Abeni if (sk->sk_err || 21107a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21117a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21127a6a6cbcSPaolo Abeni !timeo || 21137a6a6cbcSPaolo Abeni signal_pending(current)) 21147a6a6cbcSPaolo Abeni break; 21157a6a6cbcSPaolo Abeni } else { 21167a6a6cbcSPaolo Abeni if (sk->sk_err) { 21177a6a6cbcSPaolo Abeni copied = sock_error(sk); 21187a6a6cbcSPaolo Abeni break; 21197a6a6cbcSPaolo Abeni } 21207a6a6cbcSPaolo Abeni 212187952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 212287952603SPaolo Abeni /* race breaker: the shutdown could be after the 212387952603SPaolo Abeni * previous receive queue check 212487952603SPaolo Abeni */ 2125e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 212687952603SPaolo Abeni continue; 21277a6a6cbcSPaolo Abeni break; 212887952603SPaolo Abeni } 21297a6a6cbcSPaolo Abeni 21307a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21317a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21327a6a6cbcSPaolo Abeni break; 21337a6a6cbcSPaolo Abeni } 21347a6a6cbcSPaolo Abeni 21357a6a6cbcSPaolo Abeni if (!timeo) { 21367a6a6cbcSPaolo Abeni copied = -EAGAIN; 21377a6a6cbcSPaolo Abeni break; 21387a6a6cbcSPaolo Abeni } 21397a6a6cbcSPaolo Abeni 21407a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21417a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21427a6a6cbcSPaolo Abeni break; 21437a6a6cbcSPaolo Abeni } 21447a6a6cbcSPaolo Abeni } 21457a6a6cbcSPaolo Abeni 21467a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2147612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21487a6a6cbcSPaolo Abeni } 21493c90e377SPaolo Abeni 21506771bfd9SFlorian Westphal out_err: 2151b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2152b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2153b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21542c9e7765SFlorian Westphal 21552c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21562c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21572c9e7765SFlorian Westphal 21582c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21592c9e7765SFlorian Westphal } 2160b7f653b2SFlorian Westphal } 2161b7f653b2SFlorian Westphal 2162612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2163612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2164612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2165ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2166a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2167a6b118feSFlorian Westphal 2168cec37a6eSPeter Krystad release_sock(sk); 2169cec37a6eSPeter Krystad return copied; 2170cec37a6eSPeter Krystad } 2171cec37a6eSPeter Krystad 2172b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2173b51f9b80SPaolo Abeni { 2174b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2175b51f9b80SPaolo Abeni icsk_retransmit_timer); 2176b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21772d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2178b51f9b80SPaolo Abeni 2179b51f9b80SPaolo Abeni bh_lock_sock(sk); 2180b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21812d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21822d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21832d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2184b51f9b80SPaolo Abeni } else { 2185b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2186e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2187b51f9b80SPaolo Abeni } 2188b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2189b51f9b80SPaolo Abeni sock_put(sk); 2190b51f9b80SPaolo Abeni } 2191b51f9b80SPaolo Abeni 2192e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2193e16163b6SPaolo Abeni { 2194e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2195e16163b6SPaolo Abeni 2196e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2197b6d69fc8SFlorian Westphal sock_put(sk); 2198e16163b6SPaolo Abeni } 2199e16163b6SPaolo Abeni 22003b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22013b1d6210SPaolo Abeni * level. 22023b1d6210SPaolo Abeni * 22033b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22043b1d6210SPaolo Abeni */ 2205ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22063b1d6210SPaolo Abeni { 220771b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22083b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 220971b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22103b1d6210SPaolo Abeni 2211109cdeb8SGeliang Tang msk_owned_by_me(msk); 22123b1d6210SPaolo Abeni 2213d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2214d9ca1de8SPaolo Abeni return NULL; 2215d5f49190SPaolo Abeni 22163b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22173b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22183b1d6210SPaolo Abeni 2219ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2220d5f49190SPaolo Abeni continue; 2221d5f49190SPaolo Abeni 222271b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 222371b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 222471b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 222571b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2226860975c6SFlorian Westphal continue; 2227860975c6SFlorian Westphal } 22283b1d6210SPaolo Abeni 22293b1d6210SPaolo Abeni if (subflow->backup) { 22303b1d6210SPaolo Abeni if (!backup) 22313b1d6210SPaolo Abeni backup = ssk; 22323b1d6210SPaolo Abeni continue; 22333b1d6210SPaolo Abeni } 22343b1d6210SPaolo Abeni 223571b7dec2SPaolo Abeni if (!pick) 223671b7dec2SPaolo Abeni pick = ssk; 22373b1d6210SPaolo Abeni } 22383b1d6210SPaolo Abeni 223971b7dec2SPaolo Abeni if (pick) 224071b7dec2SPaolo Abeni return pick; 224171b7dec2SPaolo Abeni 224271b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 224371b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22443b1d6210SPaolo Abeni } 22453b1d6210SPaolo Abeni 224617aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 224717aee05dSFlorian Westphal { 224817aee05dSFlorian Westphal if (msk->subflow) { 224917aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 22505b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 225117aee05dSFlorian Westphal } 225217aee05dSFlorian Westphal } 225317aee05dSFlorian Westphal 22541e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22551e1d9d6fSPaolo Abeni { 22561e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22571e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22581e1d9d6fSPaolo Abeni 225900df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 22601e1d9d6fSPaolo Abeni return false; 22611e1d9d6fSPaolo Abeni 22621e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22631e1d9d6fSPaolo Abeni return false; 22641e1d9d6fSPaolo Abeni 22651e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22661e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22671e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22681e1d9d6fSPaolo Abeni */ 22691e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22701e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22711e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22721e1d9d6fSPaolo Abeni if (!rtx_head) { 22731e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22741e1d9d6fSPaolo Abeni return false; 22751e1d9d6fSPaolo Abeni } 22761e1d9d6fSPaolo Abeni 22771e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22781e1d9d6fSPaolo Abeni msk->recovery = true; 22791e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22801e1d9d6fSPaolo Abeni 22811e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22821e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22831e1d9d6fSPaolo Abeni 22841e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22851e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22861e1d9d6fSPaolo Abeni if (!cur->already_sent) 22871e1d9d6fSPaolo Abeni break; 22881e1d9d6fSPaolo Abeni cur->already_sent = 0; 22891e1d9d6fSPaolo Abeni } 22901e1d9d6fSPaolo Abeni 22911e1d9d6fSPaolo Abeni return true; 22921e1d9d6fSPaolo Abeni } 22931e1d9d6fSPaolo Abeni 2294b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2295b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2296b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2297b29fcfb5SPaolo Abeni 2298cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2299cec37a6eSPeter Krystad * (accept). 2300cec37a6eSPeter Krystad * 2301cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2302cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2303cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2304cec37a6eSPeter Krystad * parent socket. 2305cec37a6eSPeter Krystad */ 2306a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2307b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2308b29fcfb5SPaolo Abeni unsigned int flags) 2309cec37a6eSPeter Krystad { 2310e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 231163740448SPaolo Abeni bool dispose_it, need_push = false; 231263740448SPaolo Abeni 231363740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 231463740448SPaolo Abeni * to an incoming reset, mptcp either: 231563740448SPaolo Abeni * - if either the subflow or the msk are dead, destroy the context 231663740448SPaolo Abeni * (the subflow socket is deleted by inet_child_forget) and the msk 231763740448SPaolo Abeni * - otherwise do nothing at the moment and take action at accept and/or 231863740448SPaolo Abeni * listener shutdown - user-space must be able to accept() the closed 231963740448SPaolo Abeni * socket. 232063740448SPaolo Abeni */ 232163740448SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk) { 232263740448SPaolo Abeni if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 232363740448SPaolo Abeni return; 232463740448SPaolo Abeni 232563740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 232663740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 232763740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 232863740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 232963740448SPaolo Abeni goto out_release; 233063740448SPaolo Abeni } 2331e0be4931SFlorian Westphal 2332b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2333b29fcfb5SPaolo Abeni if (dispose_it) 2334cec37a6eSPeter Krystad list_del(&subflow->node); 2335cec37a6eSPeter Krystad 23363f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2337e16163b6SPaolo Abeni 2338*ff18f9efSPaolo Abeni if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) { 2339d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2340d21f8348SPaolo Abeni * to generate the egress reset 2341d21f8348SPaolo Abeni */ 2342d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2343d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2344b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2345d21f8348SPaolo Abeni } 2346b29fcfb5SPaolo Abeni 2347b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2348b29fcfb5SPaolo Abeni if (!dispose_it) { 2349c2b2ae39SPaolo Abeni /* The MPTCP code never wait on the subflow sockets, TCP-level 2350c2b2ae39SPaolo Abeni * disconnect should never fail 2351c2b2ae39SPaolo Abeni */ 2352c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 2353b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2354b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2355b29fcfb5SPaolo Abeni release_sock(ssk); 2356b29fcfb5SPaolo Abeni 2357b29fcfb5SPaolo Abeni goto out; 2358b29fcfb5SPaolo Abeni } 2359b29fcfb5SPaolo Abeni 2360d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2361d7b1bfd0SPaolo Abeni 2362e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2363e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2364e16163b6SPaolo Abeni * reference owned by msk; 2365e16163b6SPaolo Abeni */ 2366e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2367b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2368e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2369e16163b6SPaolo Abeni } else { 2370d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2371e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2372e16163b6SPaolo Abeni 2373e16163b6SPaolo Abeni /* close acquired an extra ref */ 2374e16163b6SPaolo Abeni __sock_put(ssk); 2375e16163b6SPaolo Abeni } 237663740448SPaolo Abeni 237763740448SPaolo Abeni out_release: 2378e16163b6SPaolo Abeni release_sock(ssk); 2379e16163b6SPaolo Abeni 2380e16163b6SPaolo Abeni sock_put(ssk); 2381e0be4931SFlorian Westphal 2382c8fe62f0SFlorian Westphal if (ssk == msk->first) 23831b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2384c8fe62f0SFlorian Westphal 2385b29fcfb5SPaolo Abeni out: 2386b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2387b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23881e1d9d6fSPaolo Abeni 23891e1d9d6fSPaolo Abeni if (need_push) 23901e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2391f870fa0bSMat Martineau } 2392f870fa0bSMat Martineau 2393a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2394a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2395a141e02eSFlorian Westphal { 2396b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2397b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2398a88c9e49SPaolo Abeni 2399a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2400a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2401a88c9e49SPaolo Abeni */ 2402a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2403a88c9e49SPaolo Abeni 2404b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2405a141e02eSFlorian Westphal } 2406a141e02eSFlorian Westphal 2407dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2408dc24f8b4SPaolo Abeni { 2409dc24f8b4SPaolo Abeni return 0; 2410dc24f8b4SPaolo Abeni } 2411dc24f8b4SPaolo Abeni 2412b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24130e4f35d7SPaolo Abeni { 24140e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2415b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24160e4f35d7SPaolo Abeni 24173abc05d9SFlorian Westphal might_sleep(); 24183abc05d9SFlorian Westphal 24195efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24200e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24210e4f35d7SPaolo Abeni 24220e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24230e4f35d7SPaolo Abeni continue; 24240e4f35d7SPaolo Abeni 242540947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 242640947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 242740947e13SFlorian Westphal continue; 242840947e13SFlorian Westphal 2429b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2430b6985b9bSPaolo Abeni } 2431b6985b9bSPaolo Abeni 24320e4f35d7SPaolo Abeni } 24330e4f35d7SPaolo Abeni 243463740448SPaolo Abeni static bool mptcp_should_close(const struct sock *sk) 2435e16163b6SPaolo Abeni { 2436e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2437e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2438e16163b6SPaolo Abeni 243963740448SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2440e16163b6SPaolo Abeni return true; 2441e16163b6SPaolo Abeni 2442e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2443e16163b6SPaolo Abeni * timeout 2444e16163b6SPaolo Abeni */ 2445e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2446e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2447e16163b6SPaolo Abeni TCP_CLOSE) 2448e16163b6SPaolo Abeni return false; 2449e16163b6SPaolo Abeni } 2450e16163b6SPaolo Abeni return true; 2451e16163b6SPaolo Abeni } 2452e16163b6SPaolo Abeni 245350c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 245450c504a2SFlorian Westphal { 245550c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 245680638684SGeliang Tang struct sock *sk = (struct sock *)msk; 245750c504a2SFlorian Westphal 245850c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 245950c504a2SFlorian Westphal return; 246050c504a2SFlorian Westphal 246150c504a2SFlorian Westphal mptcp_token_destroy(msk); 246250c504a2SFlorian Westphal 24635efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 246450c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 246575e908c3SPaolo Abeni bool slow; 246650c504a2SFlorian Westphal 246775e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 246850c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 246950c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 247050c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 247150c504a2SFlorian Westphal } 247275e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 247350c504a2SFlorian Westphal } 247450c504a2SFlorian Westphal 247569800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 247669800e51SPaolo Abeni switch (sk->sk_state) { 247769800e51SPaolo Abeni case TCP_SYN_SENT: 24789ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 247969800e51SPaolo Abeni break; 248069800e51SPaolo Abeni case TCP_CLOSE_WAIT: 24819ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 248269800e51SPaolo Abeni break; 248369800e51SPaolo Abeni case TCP_CLOSE: 248469800e51SPaolo Abeni return; 248569800e51SPaolo Abeni default: 24869ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 248769800e51SPaolo Abeni } 248869800e51SPaolo Abeni 248950c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 24906b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 249150c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 249250c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 249350c504a2SFlorian Westphal 249469800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 249569800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 249669800e51SPaolo Abeni return; 249769800e51SPaolo Abeni 249869800e51SPaolo Abeni sk->sk_state_change(sk); 249969800e51SPaolo Abeni sk_error_report(sk); 250050c504a2SFlorian Westphal } 250150c504a2SFlorian Westphal 25022948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 250380992017SPaolo Abeni { 25042948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2505caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25063b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25073b1d6210SPaolo Abeni size_t copied = 0; 25082948d0a1SPaolo Abeni struct sock *ssk; 25092948d0a1SPaolo Abeni int ret; 25102948d0a1SPaolo Abeni 2511b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25123241a9c0SFlorian Westphal 25133241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25143241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25152948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25166477dd39SMat Martineau if (!dfrag) { 25176477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25186477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25196477dd39SMat Martineau 25206477dd39SMat Martineau icsk->icsk_retransmits++; 25216477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25226477dd39SMat Martineau mptcp_send_ack(msk); 25236477dd39SMat Martineau 25246477dd39SMat Martineau goto reset_timer; 25256477dd39SMat Martineau } 25266477dd39SMat Martineau 25273241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25282948d0a1SPaolo Abeni return; 25293241a9c0SFlorian Westphal 25303241a9c0SFlorian Westphal goto reset_timer; 25316477dd39SMat Martineau } 25322948d0a1SPaolo Abeni 25332948d0a1SPaolo Abeni if (!ssk) 25342948d0a1SPaolo Abeni goto reset_timer; 25352948d0a1SPaolo Abeni 25362948d0a1SPaolo Abeni lock_sock(ssk); 25372948d0a1SPaolo Abeni 25382948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25392948d0a1SPaolo Abeni info.sent = 0; 25404e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25414e14867dSPaolo Abeni while (info.sent < info.limit) { 25422948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25432948d0a1SPaolo Abeni if (ret <= 0) 25442948d0a1SPaolo Abeni break; 25452948d0a1SPaolo Abeni 25462948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25472948d0a1SPaolo Abeni copied += ret; 25482948d0a1SPaolo Abeni info.sent += ret; 25492948d0a1SPaolo Abeni } 25504e14867dSPaolo Abeni if (copied) { 25514e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 255238967f42SPaolo Abeni msk->bytes_retrans += copied; 25532948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 25542948d0a1SPaolo Abeni info.size_goal); 25550530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 25564e14867dSPaolo Abeni } 25572948d0a1SPaolo Abeni 25582948d0a1SPaolo Abeni release_sock(ssk); 25592948d0a1SPaolo Abeni 25602948d0a1SPaolo Abeni reset_timer: 25613241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 25623241a9c0SFlorian Westphal 25632948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 25642948d0a1SPaolo Abeni mptcp_reset_timer(sk); 25652948d0a1SPaolo Abeni } 25662948d0a1SPaolo Abeni 256776a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 256876a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 256976a13b31SGeliang Tang */ 257076a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 257176a13b31SGeliang Tang { 257276a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 257376a13b31SGeliang Tang unsigned long timeout, close_timeout; 257476a13b31SGeliang Tang 257576a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 257676a13b31SGeliang Tang return; 257776a13b31SGeliang Tang 257876a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 257976a13b31SGeliang Tang 258076a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 258176a13b31SGeliang Tang * them is active 258276a13b31SGeliang Tang */ 258376a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 258476a13b31SGeliang Tang 258576a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 258676a13b31SGeliang Tang } 258776a13b31SGeliang Tang 258849fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 258949fa1919SGeliang Tang { 259076a13b31SGeliang Tang struct sock *ssk = msk->first; 259149fa1919SGeliang Tang bool slow; 259249fa1919SGeliang Tang 259376a13b31SGeliang Tang if (!ssk) 259476a13b31SGeliang Tang return; 259576a13b31SGeliang Tang 259649fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 259749fa1919SGeliang Tang 259849fa1919SGeliang Tang slow = lock_sock_fast(ssk); 259949fa1919SGeliang Tang mptcp_subflow_reset(ssk); 260076a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 260149fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 260276a13b31SGeliang Tang 260376a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 260449fa1919SGeliang Tang } 260549fa1919SGeliang Tang 2606d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2607d21f8348SPaolo Abeni { 2608d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2609d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2610d21f8348SPaolo Abeni 2611bbd49d11SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2612d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2613d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2614d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2615d21f8348SPaolo Abeni } 2616d21f8348SPaolo Abeni 26172948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26182948d0a1SPaolo Abeni { 26192948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 262080638684SGeliang Tang struct sock *sk = (struct sock *)msk; 262176a13b31SGeliang Tang unsigned long fail_tout; 26222948d0a1SPaolo Abeni int state; 262380992017SPaolo Abeni 262480992017SPaolo Abeni lock_sock(sk); 2625e16163b6SPaolo Abeni state = sk->sk_state; 2626d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2627e16163b6SPaolo Abeni goto unlock; 2628e16163b6SPaolo Abeni 262950c504a2SFlorian Westphal mptcp_check_fastclose(msk); 263050c504a2SFlorian Westphal 2631e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2632b416268bSFlorian Westphal 263381c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 263481c1d029SPaolo Abeni mptcp_check_data_fin_ack(sk); 263543b54c6eSMat Martineau mptcp_check_data_fin(sk); 263643b54c6eSMat Martineau 2637b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2638b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2639b6985b9bSPaolo Abeni 2640341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2641341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2642341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2643e16163b6SPaolo Abeni */ 2644d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 2645bbd49d11SPaolo Abeni if (mptcp_should_close(sk)) 2646d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2647bbd49d11SPaolo Abeni 2648d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2649e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2650e16163b6SPaolo Abeni goto unlock; 2651e16163b6SPaolo Abeni } 2652d21f8348SPaolo Abeni } 2653e16163b6SPaolo Abeni 26542948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 26552948d0a1SPaolo Abeni __mptcp_retrans(sk); 26563b1d6210SPaolo Abeni 265776a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 265876a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 265949fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 266049fa1919SGeliang Tang 26613b1d6210SPaolo Abeni unlock: 266280992017SPaolo Abeni release_sock(sk); 266380992017SPaolo Abeni sock_put(sk); 266480992017SPaolo Abeni } 266580992017SPaolo Abeni 2666784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2667f870fa0bSMat Martineau { 2668cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2669cec37a6eSPeter Krystad 2670cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2671ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 267218b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 267380992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 267487952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2675ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2676f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 26776511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2678ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 267933d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2680cec37a6eSPeter Krystad 26811b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2682dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2683752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 26840530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 26851e1d9d6fSPaolo Abeni msk->recovery = false; 26866f06b4d4SPaolo Abeni msk->subflow_id = 1; 26878ab183deSPaolo Abeni 26881b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 26891b1c7a0eSPeter Krystad 2690b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2691b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2692e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2693aa1fbd94SFlorian Westphal 2694f870fa0bSMat Martineau return 0; 2695f870fa0bSMat Martineau } 2696f870fa0bSMat Martineau 2697b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2698784325e9SMatthieu Baerts { 269920b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2700b29fcfb5SPaolo Abeni 2701b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2702b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2703b29fcfb5SPaolo Abeni 2704b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2705b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2706b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2707b29fcfb5SPaolo Abeni } 2708b29fcfb5SPaolo Abeni 2709b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2710b29fcfb5SPaolo Abeni { 2711fc518953SFlorian Westphal struct net *net = sock_net(sk); 2712fc518953SFlorian Westphal int ret; 271318b683bfSPaolo Abeni 2714b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2715b6c08380SGeliang Tang if (ret) 2716b6c08380SGeliang Tang return ret; 2717b6c08380SGeliang Tang 2718fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2719fc518953SFlorian Westphal return -ENOPROTOOPT; 2720fc518953SFlorian Westphal 2721fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2722fc518953SFlorian Westphal return -ENOMEM; 2723fc518953SFlorian Westphal 2724a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2725a5ef058dSPaolo Abeni 272620b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 272720b5759fSPaolo Abeni * propagate the correct value 272820b5759fSPaolo Abeni */ 2729b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 273020b5759fSPaolo Abeni 2731d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2732a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2733a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2734d027236cSPaolo Abeni 273518b683bfSPaolo Abeni return 0; 273618b683bfSPaolo Abeni } 273718b683bfSPaolo Abeni 273818b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 273918b683bfSPaolo Abeni { 274018b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 274118b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 274218b683bfSPaolo Abeni 2743d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 274418b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2745d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2746784325e9SMatthieu Baerts } 2747784325e9SMatthieu Baerts 274830e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 274980992017SPaolo Abeni { 275080992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 275180992017SPaolo Abeni 2752b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2753e16163b6SPaolo Abeni __sock_put(sk); 275480992017SPaolo Abeni } 275580992017SPaolo Abeni 2756d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 275721498490SPeter Krystad { 275821498490SPeter Krystad lock_sock(ssk); 275921498490SPeter Krystad 276021498490SPeter Krystad switch (ssk->sk_state) { 276121498490SPeter Krystad case TCP_LISTEN: 276221498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 276321498490SPeter Krystad break; 2764df561f66SGustavo A. R. Silva fallthrough; 276521498490SPeter Krystad case TCP_SYN_SENT: 2766c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 276721498490SPeter Krystad break; 276821498490SPeter Krystad default: 276943b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 277043b54c6eSMat Martineau pr_debug("Fallback"); 277121498490SPeter Krystad ssk->sk_shutdown |= how; 277221498490SPeter Krystad tcp_shutdown(ssk, how); 277381c1d029SPaolo Abeni 277481c1d029SPaolo Abeni /* simulate the data_fin ack reception to let the state 277581c1d029SPaolo Abeni * machine move forward 277681c1d029SPaolo Abeni */ 277781c1d029SPaolo Abeni WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 277881c1d029SPaolo Abeni mptcp_schedule_work(sk); 277943b54c6eSMat Martineau } else { 278043b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 278143b54c6eSMat Martineau tcp_send_ack(ssk); 27826477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 27836477dd39SMat Martineau mptcp_reset_timer(sk); 278443b54c6eSMat Martineau } 278521498490SPeter Krystad break; 278621498490SPeter Krystad } 278721498490SPeter Krystad 278821498490SPeter Krystad release_sock(ssk); 278921498490SPeter Krystad } 279021498490SPeter Krystad 27916920b851SMat Martineau static const unsigned char new_state[16] = { 27926920b851SMat Martineau /* current state: new state: action: */ 27936920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 27946920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27956920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 27966920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27976920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 27986920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 27996920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28006920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28016920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28026920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28036920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28046920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28056920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28066920b851SMat Martineau }; 28076920b851SMat Martineau 28086920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28096920b851SMat Martineau { 28106920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28116920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28126920b851SMat Martineau 28136920b851SMat Martineau inet_sk_state_store(sk, ns); 28146920b851SMat Martineau 28156920b851SMat Martineau return next & TCP_ACTION_FIN; 28166920b851SMat Martineau } 28176920b851SMat Martineau 281881c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk) 2819e16163b6SPaolo Abeni { 2820e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2821e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2822e16163b6SPaolo Abeni 2823e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2824e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2825e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2826e16163b6SPaolo Abeni 2827e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2828e16163b6SPaolo Abeni * skip this 2829e16163b6SPaolo Abeni */ 2830e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2831e16163b6SPaolo Abeni mptcp_send_head(sk)) 2832e16163b6SPaolo Abeni return; 2833e16163b6SPaolo Abeni 2834e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2835e16163b6SPaolo Abeni 2836e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2837e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2838e16163b6SPaolo Abeni 2839e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2840e16163b6SPaolo Abeni } 2841e16163b6SPaolo Abeni } 2842e16163b6SPaolo Abeni 2843e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2844e16163b6SPaolo Abeni { 2845e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2846e16163b6SPaolo Abeni 2847e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2848e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2849e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2850e16163b6SPaolo Abeni 2851e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2852e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2853e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2854e16163b6SPaolo Abeni 285581c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 2856e16163b6SPaolo Abeni } 2857e16163b6SPaolo Abeni 2858e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2859f870fa0bSMat Martineau { 2860f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2861f870fa0bSMat Martineau 2862e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 28632c22c06cSFlorian Westphal 28643abc05d9SFlorian Westphal might_sleep(); 28653abc05d9SFlorian Westphal 2866bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2867e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2868e16163b6SPaolo Abeni msk->pm.status = 0; 2869b2c5b614SFlorian Westphal 2870e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2871e16163b6SPaolo Abeni 28726511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 287387952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2874e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2875e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2876aa1fbd94SFlorian Westphal 2877e16163b6SPaolo Abeni sock_put(sk); 2878e16163b6SPaolo Abeni } 2879e16163b6SPaolo Abeni 288063740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 288163740448SPaolo Abeni { 288263740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 288363740448SPaolo Abeni mptcp_do_fastclose(sk); 288463740448SPaolo Abeni __mptcp_destroy_sock(sk); 288563740448SPaolo Abeni } 288663740448SPaolo Abeni 2887d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2888d21f8348SPaolo Abeni { 2889d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2890d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2891d21f8348SPaolo Abeni */ 2892d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2893d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2894d21f8348SPaolo Abeni return 0; 2895d21f8348SPaolo Abeni 2896d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2897d21f8348SPaolo Abeni } 2898d21f8348SPaolo Abeni 289957fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk) 2900c558246eSMenglong Dong { 290157fc0f1cSPaolo Abeni struct sock *ssk; 290257fc0f1cSPaolo Abeni 290357fc0f1cSPaolo Abeni if (inet_sk_state_load(sk) != TCP_LISTEN) 290457fc0f1cSPaolo Abeni return; 290557fc0f1cSPaolo Abeni 2906c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 290757fc0f1cSPaolo Abeni ssk = mptcp_sk(sk)->first; 290857fc0f1cSPaolo Abeni if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 290957fc0f1cSPaolo Abeni return; 291057fc0f1cSPaolo Abeni 291157fc0f1cSPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 29123fffa15bSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 291357fc0f1cSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 291457fc0f1cSPaolo Abeni inet_csk_listen_stop(ssk); 291557fc0f1cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 291657fc0f1cSPaolo Abeni release_sock(ssk); 2917c558246eSMenglong Dong } 2918c558246eSMenglong Dong 291926d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2920e16163b6SPaolo Abeni { 2921e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 292276a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2923e16163b6SPaolo Abeni bool do_cancel_work = false; 2924d4e85922SPaolo Abeni int subflows_alive = 0; 2925e16163b6SPaolo Abeni 29266b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2927e16163b6SPaolo Abeni 2928e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 292957fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 2930e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2931e16163b6SPaolo Abeni goto cleanup; 2932e16163b6SPaolo Abeni } 2933e16163b6SPaolo Abeni 29348d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 29358d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 29368d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 29378d547809SPaolo Abeni */ 2938d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 29398d547809SPaolo Abeni timeout = 0; 2940d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2941e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2942d21f8348SPaolo Abeni } 2943e16163b6SPaolo Abeni 2944e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2945e16163b6SPaolo Abeni 2946e16163b6SPaolo Abeni cleanup: 2947e16163b6SPaolo Abeni /* orphan all the subflows */ 2948e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 294976a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2950e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 295149054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2952e16163b6SPaolo Abeni 2953d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 2954d4e85922SPaolo Abeni 295576a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 295676a13b31SGeliang Tang * cancel the latter 295776a13b31SGeliang Tang */ 295876a13b31SGeliang Tang if (ssk == msk->first) 295976a13b31SGeliang Tang subflow->fail_tout = 0; 296076a13b31SGeliang Tang 2961fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 2962fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 2963fe948001SMenglong Dong */ 2964fe948001SMenglong Dong ssk->sk_socket = NULL; 2965fe948001SMenglong Dong ssk->sk_wq = NULL; 2966e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2967e16163b6SPaolo Abeni } 2968e16163b6SPaolo Abeni sock_orphan(sk); 2969e16163b6SPaolo Abeni 2970d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 2971d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 2972d4e85922SPaolo Abeni */ 2973d4e85922SPaolo Abeni if (subflows_alive == 0) 2974d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2975d4e85922SPaolo Abeni 2976e16163b6SPaolo Abeni sock_hold(sk); 2977e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 297800df24f1SGeliang Tang if (msk->token) 297976a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2980b29fcfb5SPaolo Abeni 2981e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2982e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2983e16163b6SPaolo Abeni do_cancel_work = true; 2984e16163b6SPaolo Abeni } else { 298576a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2986e16163b6SPaolo Abeni } 298726d3e21cSMenglong Dong 298826d3e21cSMenglong Dong return do_cancel_work; 298926d3e21cSMenglong Dong } 299026d3e21cSMenglong Dong 299126d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 299226d3e21cSMenglong Dong { 299326d3e21cSMenglong Dong bool do_cancel_work; 299426d3e21cSMenglong Dong 299526d3e21cSMenglong Dong lock_sock(sk); 299626d3e21cSMenglong Dong 299726d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 2998e16163b6SPaolo Abeni release_sock(sk); 2999e16163b6SPaolo Abeni if (do_cancel_work) 300080992017SPaolo Abeni mptcp_cancel_work(sk); 3001b911c97cSFlorian Westphal 3002e16163b6SPaolo Abeni sock_put(sk); 3003f870fa0bSMat Martineau } 3004f870fa0bSMat Martineau 30057e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3006cf7da0d6SPeter Krystad { 3007cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3008cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3009cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3010cf7da0d6SPeter Krystad 3011cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3012cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3013cf7da0d6SPeter Krystad 3014cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3015cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3016cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3017cf7da0d6SPeter Krystad } 3018cf7da0d6SPeter Krystad #endif 3019cf7da0d6SPeter Krystad 3020cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3021cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3022cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3023cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3024cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3025cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3026cf7da0d6SPeter Krystad } 3027cf7da0d6SPeter Krystad 302818b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 302918b683bfSPaolo Abeni { 303076e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 303176e2a55dSPaolo Abeni 30320ad529d9SPaolo Abeni /* Deny disconnect if other threads are blocked in sk_wait_event() 30330ad529d9SPaolo Abeni * or inet_wait_for_connect(). 30340ad529d9SPaolo Abeni */ 30350ad529d9SPaolo Abeni if (sk->sk_wait_pending) 30360ad529d9SPaolo Abeni return -EBUSY; 30370ad529d9SPaolo Abeni 30387d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 30397d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 3040c2b2ae39SPaolo Abeni * msk->firstsocket lock). 30417d803344SPaolo Abeni */ 30427d803344SPaolo Abeni if (msk->fastopening) 3043c2b2ae39SPaolo Abeni return -EBUSY; 30447d803344SPaolo Abeni 304557fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3046b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3047b29fcfb5SPaolo Abeni 3048bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3049b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3050b29fcfb5SPaolo Abeni 305100df24f1SGeliang Tang if (msk->token) 305200df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3053b29fcfb5SPaolo Abeni 3054c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3055c0bf3c6aSPaolo Abeni * subflow 3056c0bf3c6aSPaolo Abeni */ 3057c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3058b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3059e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3060e9d09bacSPaolo Abeni msk->cb_flags = 0; 3061e9d09bacSPaolo Abeni msk->push_pending = 0; 3062b29fcfb5SPaolo Abeni msk->recovery = false; 3063b29fcfb5SPaolo Abeni msk->can_ack = false; 3064b29fcfb5SPaolo Abeni msk->fully_established = false; 3065b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3066b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3067b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3068b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3069b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3070b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3071b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 307238967f42SPaolo Abeni msk->bytes_acked = 0; 307338967f42SPaolo Abeni msk->bytes_received = 0; 307438967f42SPaolo Abeni msk->bytes_sent = 0; 307538967f42SPaolo Abeni msk->bytes_retrans = 0; 3076b29fcfb5SPaolo Abeni 30776b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3078b29fcfb5SPaolo Abeni sk_error_report(sk); 307942c556feSFlorian Westphal return 0; 308018b683bfSPaolo Abeni } 308118b683bfSPaolo Abeni 3082b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3083b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3084b0519de8SFlorian Westphal { 3085b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3086b0519de8SFlorian Westphal 3087b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3088b0519de8SFlorian Westphal } 3089b0519de8SFlorian Westphal #endif 3090b0519de8SFlorian Westphal 30917e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3092cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 30937e8b88ecSPaolo Abeni struct sock *ssk, 3094fca5c82cSPaolo Abeni struct request_sock *req) 3095b0519de8SFlorian Westphal { 309658b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3097b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 309858b09919SPaolo Abeni struct mptcp_sock *msk; 3099b0519de8SFlorian Westphal 3100b0519de8SFlorian Westphal if (!nsk) 3101b0519de8SFlorian Westphal return NULL; 3102b0519de8SFlorian Westphal 3103b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3104b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3105b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3106b0519de8SFlorian Westphal #endif 3107b0519de8SFlorian Westphal 31080ad529d9SPaolo Abeni nsk->sk_wait_pending = 0; 310958b09919SPaolo Abeni __mptcp_init_sock(nsk); 311058b09919SPaolo Abeni 311158b09919SPaolo Abeni msk = mptcp_sk(nsk); 311258b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 311358b09919SPaolo Abeni msk->token = subflow_req->token; 31145b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 3115b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3116b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 311774c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 311806251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 311958b09919SPaolo Abeni 312058b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3121eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31227439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31237439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3124df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31256f8a612aSFlorian Westphal 31266f06b4d4SPaolo Abeni /* passive msk is created after the first/MPC subflow */ 31276f06b4d4SPaolo Abeni msk->subflow_id = 2; 31286f06b4d4SPaolo Abeni 31295e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31300c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 31317e8b88ecSPaolo Abeni 31327e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 31337e8b88ecSPaolo Abeni * not yet exposted to user-space 31347e8b88ecSPaolo Abeni */ 31357e8b88ecSPaolo Abeni inet_sk_state_store(nsk, TCP_ESTABLISHED); 31367e8b88ecSPaolo Abeni 31377e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 31387e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 31397e8b88ecSPaolo Abeni list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list); 31407e8b88ecSPaolo Abeni sock_hold(ssk); 31417e8b88ecSPaolo Abeni 31427e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 31437e8b88ecSPaolo Abeni * created mptcp socket 31447e8b88ecSPaolo Abeni */ 31457e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 31467e8b88ecSPaolo Abeni 31477e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 31487e8b88ecSPaolo Abeni * uses the correct data 31497e8b88ecSPaolo Abeni */ 31507e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 31517e8b88ecSPaolo Abeni mptcp_propagate_sndbuf(nsk, ssk); 31527e8b88ecSPaolo Abeni 31537e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 315458b09919SPaolo Abeni bh_unlock_sock(nsk); 315558b09919SPaolo Abeni 3156b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3157b0519de8SFlorian Westphal return nsk; 3158b0519de8SFlorian Westphal } 3159b0519de8SFlorian Westphal 3160a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3161a6b118feSFlorian Westphal { 3162a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3163a6b118feSFlorian Westphal 3164a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3165a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3166a6b118feSFlorian Westphal 3167a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3168a6b118feSFlorian Westphal 3169a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3170a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3171a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3172a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3173a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 31746f8a612aSFlorian Westphal 31757439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3176a6b118feSFlorian Westphal } 3177a6b118feSFlorian Westphal 3178cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3179cf7da0d6SPeter Krystad bool kern) 3180cf7da0d6SPeter Krystad { 3181cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3182cf7da0d6SPeter Krystad struct socket *listener; 3183cf7da0d6SPeter Krystad struct sock *newsk; 3184cf7da0d6SPeter Krystad 31855b825727SPaolo Abeni listener = READ_ONCE(msk->subflow); 3186cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3187cf7da0d6SPeter Krystad *err = -EINVAL; 3188cf7da0d6SPeter Krystad return NULL; 3189cf7da0d6SPeter Krystad } 3190cf7da0d6SPeter Krystad 3191cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3192cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3193cf7da0d6SPeter Krystad if (!newsk) 3194cf7da0d6SPeter Krystad return NULL; 3195cf7da0d6SPeter Krystad 3196cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3197cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3198cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3199cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3200cf7da0d6SPeter Krystad 3201cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 320258b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 320358b09919SPaolo Abeni 320458b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 320558b09919SPaolo Abeni * subflow_syn_recv_sock() 320658b09919SPaolo Abeni */ 320758b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 320858b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3209d6692b3bSFlorian Westphal goto out; 321058b09919SPaolo Abeni } 321158b09919SPaolo Abeni 3212cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 32130397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3214fc518953SFlorian Westphal } else { 3215fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3216fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3217cf7da0d6SPeter Krystad } 3218cf7da0d6SPeter Krystad 3219d6692b3bSFlorian Westphal out: 3220d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3221cf7da0d6SPeter Krystad return newsk; 3222cf7da0d6SPeter Krystad } 3223cf7da0d6SPeter Krystad 3224c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 32255c8c1640SGeliang Tang { 3226c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 322787952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 322887952603SPaolo Abeni 32296e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 32306e628cd3SPaolo Abeni 3231c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 32325efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3233c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3234c0bf3c6aSPaolo Abeni 323587952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3236b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 323787952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32386511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32395c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3240b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32416511882cSPaolo Abeni 32426511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 32436511882cSPaolo Abeni * inet_sock_destruct() will dispose it 32446511882cSPaolo Abeni */ 32456511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 32466511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 32475c8c1640SGeliang Tang mptcp_token_destroy(msk); 32485c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 32494638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 32505c8c1640SGeliang Tang } 32515c8c1640SGeliang Tang 325279c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 325379c0949eSPeter Krystad { 3254c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3255c9fd9c5fSFlorian Westphal 3256c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3257c0bf3c6aSPaolo Abeni * even the initial subflow 3258c0bf3c6aSPaolo Abeni */ 3259c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3260c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3261d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 326279c0949eSPeter Krystad } 326379c0949eSPeter Krystad 32646e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32656e628cd3SPaolo Abeni { 32666e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32676e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32686e628cd3SPaolo Abeni else 3269e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 32706e628cd3SPaolo Abeni 32716e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 32726e628cd3SPaolo Abeni mptcp_schedule_work(sk); 32736e628cd3SPaolo Abeni } 32746e628cd3SPaolo Abeni 3275219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 32766e628cd3SPaolo Abeni { 32776e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 32786e628cd3SPaolo Abeni return; 32796e628cd3SPaolo Abeni 328073a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 328173a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 328273a0052aSGeliang Tang else 3283e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 32846e628cd3SPaolo Abeni } 32856e628cd3SPaolo Abeni 3286e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3287e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3288e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3289e9d09bacSPaolo Abeni 3290e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 329114c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3292e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 329314c441b5SPaolo Abeni { 3294e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 32952d6f5a2bSPaolo Abeni 3296e9d09bacSPaolo Abeni for (;;) { 3297e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3298e9d09bacSPaolo Abeni msk->push_pending; 329956a666c4SPaolo Abeni struct list_head join_list; 330056a666c4SPaolo Abeni 3301c2e6048fSPaolo Abeni if (!flags) 3302c2e6048fSPaolo Abeni break; 3303c2e6048fSPaolo Abeni 330456a666c4SPaolo Abeni INIT_LIST_HEAD(&join_list); 330556a666c4SPaolo Abeni list_splice_init(&msk->join_list, &join_list); 330656a666c4SPaolo Abeni 3307c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 33086e628cd3SPaolo Abeni * 33096e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 33106e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 33116e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 33126e628cd3SPaolo Abeni * the subflow socket lock 33136e628cd3SPaolo Abeni */ 3314e9d09bacSPaolo Abeni msk->push_pending = 0; 3315e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 33166e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 331756a666c4SPaolo Abeni 33183e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 331956a666c4SPaolo Abeni __mptcp_flush_join_list(sk, &join_list); 33202e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3321c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 33222d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 33232d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3324c2e6048fSPaolo Abeni 3325c2e6048fSPaolo Abeni cond_resched(); 33266e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 33276e628cd3SPaolo Abeni } 3328c2e6048fSPaolo Abeni 332965a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 333065a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 333117ebf8a4SXiang Yang if (unlikely(msk->cb_flags)) { 3332490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 333365a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3334490274b4SPaolo Abeni */ 3335e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3336490274b4SPaolo Abeni __mptcp_set_connected(sk); 3337e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 333815cc1045SPaolo Abeni __mptcp_error_report(sk); 333943f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 334043f5b111SPaolo Abeni msk->last_snd = NULL; 334165a569b0SPaolo Abeni } 33426e628cd3SPaolo Abeni 334387952603SPaolo Abeni __mptcp_update_rmem(sk); 334414c441b5SPaolo Abeni } 334514c441b5SPaolo Abeni 3346bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3347bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3348bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3349bcd97734SPaolo Abeni */ 3350bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3351bcd97734SPaolo Abeni { 3352bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3353bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3354bcd97734SPaolo Abeni unsigned long timeout; 3355bcd97734SPaolo Abeni 3356bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3357bcd97734SPaolo Abeni return; 3358bcd97734SPaolo Abeni 3359bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3360bcd97734SPaolo Abeni if (tp->srtt_us) 3361bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3362bcd97734SPaolo Abeni else 3363bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3364bcd97734SPaolo Abeni timeout += jiffies; 3365bcd97734SPaolo Abeni 3366bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3367bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3368bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3369bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3370bcd97734SPaolo Abeni } 3371bcd97734SPaolo Abeni 3372b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3373b19bc294SPaolo Abeni { 3374b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3375b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3376b19bc294SPaolo Abeni 3377bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3378b19bc294SPaolo Abeni mptcp_data_lock(sk); 3379b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 338073a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3381b19bc294SPaolo Abeni else 3382e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3383b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3384bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3385bcd97734SPaolo Abeni } 3386bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3387bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3388bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3389bcd97734SPaolo Abeni } 3390b19bc294SPaolo Abeni } 3391b19bc294SPaolo Abeni 33922c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 33932c5ebd00SPaolo Abeni { 33942c5ebd00SPaolo Abeni /* should never be called, 33952c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 33962c5ebd00SPaolo Abeni */ 33972c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 33982c5ebd00SPaolo Abeni return 0; 33992c5ebd00SPaolo Abeni } 34002c5ebd00SPaolo Abeni 34012c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 34022c5ebd00SPaolo Abeni { 34032c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 34042c5ebd00SPaolo Abeni } 34052c5ebd00SPaolo Abeni 3406cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3407f870fa0bSMat Martineau { 3408f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3409cec37a6eSPeter Krystad struct socket *ssock; 3410f870fa0bSMat Martineau 341161761231SPaolo Abeni ssock = msk->subflow; 3412cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3413cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3414cec37a6eSPeter Krystad return -EINVAL; 3415f870fa0bSMat Martineau 3416cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3417cec37a6eSPeter Krystad } 3418f870fa0bSMat Martineau 3419cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3420cec37a6eSPeter Krystad { 3421cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3422cec37a6eSPeter Krystad struct mptcp_sock *msk; 3423cec37a6eSPeter Krystad struct sock *sk; 3424f870fa0bSMat Martineau 3425cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3426cec37a6eSPeter Krystad sk = subflow->conn; 3427cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3428cec37a6eSPeter Krystad 3429648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3430648ef4b8SMat Martineau 3431b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3432648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 34336d0060f6SMat Martineau 3434cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3435cec37a6eSPeter Krystad * accessing the field below 3436cec37a6eSPeter Krystad */ 3437cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 34386d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3439eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 34407439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34411b1c7a0eSPeter Krystad 34426c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3443a6b118feSFlorian Westphal 3444a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3445f870fa0bSMat Martineau } 3446f870fa0bSMat Martineau 3447866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3448cf7da0d6SPeter Krystad { 3449cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3450cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3451cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3452cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3453cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3454cf7da0d6SPeter Krystad } 3455cf7da0d6SPeter Krystad 3456e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3457f296234cSPeter Krystad { 3458e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3459f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3460f296234cSPeter Krystad struct sock *parent = (void *)msk; 34613e501490SPaolo Abeni bool ret = true; 3462f296234cSPeter Krystad 3463f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3464f296234cSPeter Krystad 3465f296234cSPeter Krystad /* mptcp socket already closing? */ 3466dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3467dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3468f296234cSPeter Krystad return false; 3469dc87efdbSFlorian Westphal } 3470f296234cSPeter Krystad 347155b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 347255b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 347355b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 347455b47ca7SPaolo Abeni return true; 347555b47ca7SPaolo Abeni } 3476f296234cSPeter Krystad 34773e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 34783e501490SPaolo Abeni goto err_prohibited; 347910f6d46cSPaolo Abeni 348055b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 34813e501490SPaolo Abeni * handle it 348210f6d46cSPaolo Abeni */ 34833e501490SPaolo Abeni mptcp_data_lock(parent); 34843e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 34853e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 34863e501490SPaolo Abeni if (ret) { 3487e16163b6SPaolo Abeni sock_hold(ssk); 34883e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3489e16163b6SPaolo Abeni } 34903e501490SPaolo Abeni } else { 34913e501490SPaolo Abeni sock_hold(ssk); 34923e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3493e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 34943e501490SPaolo Abeni } 34953e501490SPaolo Abeni mptcp_data_unlock(parent); 34963e501490SPaolo Abeni 3497dc87efdbSFlorian Westphal if (!ret) { 34983e501490SPaolo Abeni err_prohibited: 3499dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 350010f6d46cSPaolo Abeni return false; 3501dc87efdbSFlorian Westphal } 350210f6d46cSPaolo Abeni 350310f6d46cSPaolo Abeni return true; 3504f296234cSPeter Krystad } 3505f296234cSPeter Krystad 350676e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 350776e2a55dSPaolo Abeni { 350876e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 350976e2a55dSPaolo Abeni 351076e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 351176e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 351276e2a55dSPaolo Abeni } 351376e2a55dSPaolo Abeni 35146511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 35156511882cSPaolo Abeni { 35166511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 35176511882cSPaolo Abeni } 35186511882cSPaolo Abeni 3519644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3520644807e3SFlorian Westphal { 3521644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3522644807e3SFlorian Westphal u64 delta; 3523644807e3SFlorian Westphal 3524644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3525644807e3SFlorian Westphal return -EINVAL; 3526644807e3SFlorian Westphal 3527644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3528644807e3SFlorian Westphal return 0; 3529644807e3SFlorian Westphal 3530644807e3SFlorian Westphal delta = msk->write_seq - v; 353107c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 353207c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 353307c2c7a3SPaolo Abeni 353407c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 353507c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 353607c2c7a3SPaolo Abeni * so ignore that status, too. 353707c2c7a3SPaolo Abeni */ 353807c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 353907c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 354007c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 354107c2c7a3SPaolo Abeni } 3542644807e3SFlorian Westphal if (delta > INT_MAX) 3543644807e3SFlorian Westphal delta = INT_MAX; 3544644807e3SFlorian Westphal 3545644807e3SFlorian Westphal return (int)delta; 3546644807e3SFlorian Westphal } 3547644807e3SFlorian Westphal 3548e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) 3549644807e3SFlorian Westphal { 3550644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3551644807e3SFlorian Westphal bool slow; 3552644807e3SFlorian Westphal 3553644807e3SFlorian Westphal switch (cmd) { 3554644807e3SFlorian Westphal case SIOCINQ: 3555644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3556644807e3SFlorian Westphal return -EINVAL; 3557644807e3SFlorian Westphal 3558644807e3SFlorian Westphal lock_sock(sk); 3559644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3560e1d001faSBreno Leitao *karg = mptcp_inq_hint(sk); 3561644807e3SFlorian Westphal release_sock(sk); 3562644807e3SFlorian Westphal break; 3563644807e3SFlorian Westphal case SIOCOUTQ: 3564644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3565e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3566644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3567644807e3SFlorian Westphal break; 3568644807e3SFlorian Westphal case SIOCOUTQNSD: 3569644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3570e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, msk->snd_nxt); 3571644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3572644807e3SFlorian Westphal break; 3573644807e3SFlorian Westphal default: 3574644807e3SFlorian Westphal return -ENOIOCTLCMD; 3575644807e3SFlorian Westphal } 3576644807e3SFlorian Westphal 3577e1d001faSBreno Leitao return 0; 3578644807e3SFlorian Westphal } 3579644807e3SFlorian Westphal 358054f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 358154f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 358254f1944eSPaolo Abeni { 358354f1944eSPaolo Abeni subflow->request_mptcp = 0; 358454f1944eSPaolo Abeni __mptcp_do_fallback(msk); 358554f1944eSPaolo Abeni } 358654f1944eSPaolo Abeni 358754f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 358854f1944eSPaolo Abeni { 358954f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 359054f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 359154f1944eSPaolo Abeni struct socket *ssock; 359254f1944eSPaolo Abeni int err = -EINVAL; 359354f1944eSPaolo Abeni 359454f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3595ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 3596ddb1a072SPaolo Abeni return PTR_ERR(ssock); 359754f1944eSPaolo Abeni 359854f1944eSPaolo Abeni mptcp_token_destroy(msk); 359954f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 360054f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 360154f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 360254f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 360354f1944eSPaolo Abeni * TCP option space. 360454f1944eSPaolo Abeni */ 360554f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 360654f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 360754f1944eSPaolo Abeni #endif 360854f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 360954f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 361054f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 361154f1944eSPaolo Abeni } 361254f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 361354f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 361454f1944eSPaolo Abeni 361554f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 361654f1944eSPaolo Abeni * acquired the subflow socket lock, too. 361754f1944eSPaolo Abeni */ 36187d803344SPaolo Abeni if (msk->fastopening) 3619786fc124SPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1); 362054f1944eSPaolo Abeni else 3621786fc124SPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK); 362254f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 362354f1944eSPaolo Abeni 362454f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 362554f1944eSPaolo Abeni * subflow_finish_connect() 362654f1944eSPaolo Abeni */ 362754f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 362854f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 362954f1944eSPaolo Abeni return err; 363054f1944eSPaolo Abeni } 363154f1944eSPaolo Abeni 363254f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 363354f1944eSPaolo Abeni 3634786fc124SPaolo Abeni /* silence EINPROGRESS and let the caller inet_stream_connect 3635786fc124SPaolo Abeni * handle the connection in progress 363654f1944eSPaolo Abeni */ 3637786fc124SPaolo Abeni return 0; 363854f1944eSPaolo Abeni } 363954f1944eSPaolo Abeni 3640f870fa0bSMat Martineau static struct proto mptcp_prot = { 3641f870fa0bSMat Martineau .name = "MPTCP", 3642f870fa0bSMat Martineau .owner = THIS_MODULE, 3643f870fa0bSMat Martineau .init = mptcp_init_sock, 364454f1944eSPaolo Abeni .connect = mptcp_connect, 364518b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3646f870fa0bSMat Martineau .close = mptcp_close, 3647cf7da0d6SPeter Krystad .accept = mptcp_accept, 3648717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3649717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 365076e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 365179c0949eSPeter Krystad .destroy = mptcp_destroy, 3652f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3653644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3654f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 365514c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36562c5ebd00SPaolo Abeni .hash = mptcp_hash, 36572c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3658cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36596511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3660d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36610defbb0aSEric Dumazet 3662d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36630defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36640defbb0aSEric Dumazet 3665d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3666d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3667989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3668d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3669f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36702c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3671f870fa0bSMat Martineau .no_autobind = true, 3672f870fa0bSMat Martineau }; 3673f870fa0bSMat Martineau 36742303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 36752303f994SPeter Krystad { 36762303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 36772303f994SPeter Krystad struct socket *ssock; 3678cf7da0d6SPeter Krystad int err; 36792303f994SPeter Krystad 36802303f994SPeter Krystad lock_sock(sock->sk); 3681fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3682ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3683ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 36842303f994SPeter Krystad goto unlock; 36852303f994SPeter Krystad } 36862303f994SPeter Krystad 36872303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3688cf7da0d6SPeter Krystad if (!err) 3689cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 36902303f994SPeter Krystad 36912303f994SPeter Krystad unlock: 36922303f994SPeter Krystad release_sock(sock->sk); 36932303f994SPeter Krystad return err; 36942303f994SPeter Krystad } 36952303f994SPeter Krystad 3696cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3697cf7da0d6SPeter Krystad { 3698cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3699cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 3700cf7da0d6SPeter Krystad struct socket *ssock; 3701cf7da0d6SPeter Krystad int err; 3702cf7da0d6SPeter Krystad 3703cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3704cf7da0d6SPeter Krystad 3705cfdcfeedSMenglong Dong lock_sock(sk); 37060226436aSPaolo Abeni 37070226436aSPaolo Abeni err = -EINVAL; 37080226436aSPaolo Abeni if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 37090226436aSPaolo Abeni goto unlock; 37100226436aSPaolo Abeni 3711fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3712ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3713ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 3714cf7da0d6SPeter Krystad goto unlock; 3715cf7da0d6SPeter Krystad } 3716cf7da0d6SPeter Krystad 3717fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3718cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3719cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 37205e20087dSFlorian Westphal 3721cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3722cfdcfeedSMenglong Dong inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3723c558246eSMenglong Dong if (!err) { 3724c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3725cfdcfeedSMenglong Dong mptcp_copy_inaddrs(sk, ssock->sk); 3726f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 372721d9b73aSPaolo Abeni } 3728f8c9dfbdSGeliang Tang 3729cf7da0d6SPeter Krystad unlock: 3730cfdcfeedSMenglong Dong release_sock(sk); 3731cf7da0d6SPeter Krystad return err; 3732cf7da0d6SPeter Krystad } 3733cf7da0d6SPeter Krystad 3734cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3735cf7da0d6SPeter Krystad int flags, bool kern) 3736cf7da0d6SPeter Krystad { 3737cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3738cf7da0d6SPeter Krystad struct socket *ssock; 3739e76c8ef5SPaolo Abeni struct sock *newsk; 3740cf7da0d6SPeter Krystad int err; 3741cf7da0d6SPeter Krystad 3742cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3743cf7da0d6SPeter Krystad 37445b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 374561761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 374661761231SPaolo Abeni */ 37475b825727SPaolo Abeni ssock = READ_ONCE(msk->subflow); 3748cf7da0d6SPeter Krystad if (!ssock) 374971ba088cSPaolo Abeni return -EINVAL; 3750cf7da0d6SPeter Krystad 3751e76c8ef5SPaolo Abeni newsk = mptcp_accept(sock->sk, flags, &err, kern); 3752e76c8ef5SPaolo Abeni if (!newsk) 3753e76c8ef5SPaolo Abeni return err; 3754e76c8ef5SPaolo Abeni 3755e76c8ef5SPaolo Abeni lock_sock(newsk); 3756e76c8ef5SPaolo Abeni 3757e76c8ef5SPaolo Abeni __inet_accept(sock, newsock, newsk); 3758e76c8ef5SPaolo Abeni if (!mptcp_is_tcpsk(newsock->sk)) { 3759e76c8ef5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(newsk); 3760cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37610397c6d8SPaolo Abeni 3762a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3763b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3764a5ef058dSPaolo Abeni 3765cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3766cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3767cf7da0d6SPeter Krystad */ 3768190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3769cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3770cf7da0d6SPeter Krystad 3771cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3772cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3773cf7da0d6SPeter Krystad } 377463740448SPaolo Abeni 377563740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 377663740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 377763740448SPaolo Abeni */ 377863740448SPaolo Abeni if (msk->first && 377963740448SPaolo Abeni unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 378063740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 378163740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 378263740448SPaolo Abeni if (unlikely(list_empty(&msk->conn_list))) 378363740448SPaolo Abeni inet_sk_state_store(newsk, TCP_CLOSE); 378463740448SPaolo Abeni } 3785cf7da0d6SPeter Krystad } 3786e76c8ef5SPaolo Abeni release_sock(newsk); 3787cf7da0d6SPeter Krystad 3788e76c8ef5SPaolo Abeni return 0; 3789cf7da0d6SPeter Krystad } 3790cf7da0d6SPeter Krystad 37918edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 37928edf0864SFlorian Westphal { 37938edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 37948edf0864SFlorian Westphal 37958edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 37968edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 37978edf0864SFlorian Westphal 37985cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 37996e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 38006e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 38016e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 38028edf0864SFlorian Westphal 38036e628cd3SPaolo Abeni return 0; 38048edf0864SFlorian Westphal } 38058edf0864SFlorian Westphal 38062303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 38072303f994SPeter Krystad struct poll_table_struct *wait) 38082303f994SPeter Krystad { 38091891c4a0SFlorian Westphal struct sock *sk = sock->sk; 38108ab183deSPaolo Abeni struct mptcp_sock *msk; 38112303f994SPeter Krystad __poll_t mask = 0; 38126b9831bfSPaolo Abeni u8 shutdown; 38138a05661bSPaolo Abeni int state; 38142303f994SPeter Krystad 38151891c4a0SFlorian Westphal msk = mptcp_sk(sk); 38161891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 38171891c4a0SFlorian Westphal 38188a05661bSPaolo Abeni state = inet_sk_state_load(sk); 38196719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 382071ba088cSPaolo Abeni if (state == TCP_LISTEN) { 38215b825727SPaolo Abeni struct socket *ssock = READ_ONCE(msk->subflow); 38225b825727SPaolo Abeni 38235b825727SPaolo Abeni if (WARN_ON_ONCE(!ssock || !ssock->sk)) 382471ba088cSPaolo Abeni return 0; 382571ba088cSPaolo Abeni 38265b825727SPaolo Abeni return inet_csk_listen_poll(ssock->sk); 382771ba088cSPaolo Abeni } 38288a05661bSPaolo Abeni 38296b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 38306b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 38316b9831bfSPaolo Abeni mask |= EPOLLHUP; 38326b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 38336b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 38346b9831bfSPaolo Abeni 38358a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 38368a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 38376b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 38386b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 38396b9831bfSPaolo Abeni else 38408edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3841a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3842a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3843a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 38448a05661bSPaolo Abeni } 38451891c4a0SFlorian Westphal 3846d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 384715cc1045SPaolo Abeni smp_rmb(); 38489ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 384915cc1045SPaolo Abeni mask |= EPOLLERR; 385015cc1045SPaolo Abeni 38512303f994SPeter Krystad return mask; 38522303f994SPeter Krystad } 38532303f994SPeter Krystad 3854e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3855e42f1ac6SFlorian Westphal .family = PF_INET, 3856e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38570a3cc579SPaolo Abeni .release = inet_release, 3858e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3859786fc124SPaolo Abeni .connect = inet_stream_connect, 3860e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3861e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3862d2f77c53SPaolo Abeni .getname = inet_getname, 3863e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3864e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3865e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3866e42f1ac6SFlorian Westphal .listen = mptcp_listen, 386776e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3868e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3869e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3870e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3871e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3872e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3873e42f1ac6SFlorian Westphal }; 38742303f994SPeter Krystad 3875f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3876f870fa0bSMat Martineau .type = SOCK_STREAM, 3877f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3878f870fa0bSMat Martineau .prot = &mptcp_prot, 38792303f994SPeter Krystad .ops = &mptcp_stream_ops, 38802303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3881f870fa0bSMat Martineau }; 3882f870fa0bSMat Martineau 3883b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3884b19bc294SPaolo Abeni { 3885b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3886b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3887b19bc294SPaolo Abeni int work_done = 0; 3888b19bc294SPaolo Abeni 3889b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3890b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3891b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3892b19bc294SPaolo Abeni 3893b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3894b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3895b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3896b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3897b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3898b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3899b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3900b19bc294SPaolo Abeni * CPU that scheduled it. 3901b19bc294SPaolo Abeni */ 3902b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3903b19bc294SPaolo Abeni sock_put(ssk); 3904b19bc294SPaolo Abeni 3905b19bc294SPaolo Abeni if (++work_done == budget) 3906b19bc294SPaolo Abeni return budget; 3907b19bc294SPaolo Abeni } 3908b19bc294SPaolo Abeni 3909b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3910b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3911b19bc294SPaolo Abeni */ 3912b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3913b19bc294SPaolo Abeni return work_done; 3914b19bc294SPaolo Abeni } 3915b19bc294SPaolo Abeni 3916d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3917f870fa0bSMat Martineau { 3918b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3919b19bc294SPaolo Abeni int cpu; 3920b19bc294SPaolo Abeni 39212303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 39222303f994SPeter Krystad 3923d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3924d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3925d027236cSPaolo Abeni 3926b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3927b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3928b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3929b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 393016d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 393116d083e2SJakub Kicinski mptcp_napi_poll); 3932b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3933b19bc294SPaolo Abeni } 3934b19bc294SPaolo Abeni 39352303f994SPeter Krystad mptcp_subflow_init(); 39361b1c7a0eSPeter Krystad mptcp_pm_init(); 39372c5ebd00SPaolo Abeni mptcp_token_init(); 39382303f994SPeter Krystad 3939f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3940f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3941f870fa0bSMat Martineau 3942f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 39436771bfd9SFlorian Westphal 39446771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3945f870fa0bSMat Martineau } 3946f870fa0bSMat Martineau 3947f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3948e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3949e42f1ac6SFlorian Westphal .family = PF_INET6, 3950e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39510a3cc579SPaolo Abeni .release = inet6_release, 3952e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3953786fc124SPaolo Abeni .connect = inet_stream_connect, 3954e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3955e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3956d2f77c53SPaolo Abeni .getname = inet6_getname, 3957e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3958e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3959e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3960e42f1ac6SFlorian Westphal .listen = mptcp_listen, 396176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3962e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3963e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3964e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3965e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3966e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3967e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39683986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3969e42f1ac6SFlorian Westphal #endif 3970e42f1ac6SFlorian Westphal }; 3971e42f1ac6SFlorian Westphal 3972f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3973f870fa0bSMat Martineau 3974f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3975f870fa0bSMat Martineau .type = SOCK_STREAM, 3976f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3977f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39782303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3979f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3980f870fa0bSMat Martineau }; 3981f870fa0bSMat Martineau 3982d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3983f870fa0bSMat Martineau { 3984f870fa0bSMat Martineau int err; 3985f870fa0bSMat Martineau 3986f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3987f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3988f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 3989b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3990f870fa0bSMat Martineau 3991f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3992f870fa0bSMat Martineau if (err) 3993f870fa0bSMat Martineau return err; 3994f870fa0bSMat Martineau 3995f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3996f870fa0bSMat Martineau if (err) 3997f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3998f870fa0bSMat Martineau 3999f870fa0bSMat Martineau return err; 4000f870fa0bSMat Martineau } 4001f870fa0bSMat Martineau #endif 4002