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); 47d9ca1de8SPaolo 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; 994cf86ae8SPaolo Abeni 1004cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1014cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 102866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 103e1ff9e82SDavide Caratti 104fa68018dSPaolo Abeni return 0; 1052303f994SPeter Krystad } 1062303f994SPeter Krystad 107ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow, 108ddb1a072SPaolo Abeni * eventually allocating it. 109ddb1a072SPaolo Abeni */ 110ddb1a072SPaolo Abeni struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk) 111ddb1a072SPaolo Abeni { 112ddb1a072SPaolo Abeni struct sock *sk = (struct sock *)msk; 113ddb1a072SPaolo Abeni int ret; 114ddb1a072SPaolo Abeni 115ddb1a072SPaolo Abeni if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 116ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 117ddb1a072SPaolo Abeni 118ddb1a072SPaolo Abeni if (!msk->subflow) { 119ddb1a072SPaolo Abeni if (msk->first) 120ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 121ddb1a072SPaolo Abeni 122ddb1a072SPaolo Abeni ret = __mptcp_socket_create(msk); 123ddb1a072SPaolo Abeni if (ret) 124ddb1a072SPaolo Abeni return ERR_PTR(ret); 125ddb1a072SPaolo Abeni 126ddb1a072SPaolo Abeni mptcp_sockopt_sync(msk, msk->first); 127ddb1a072SPaolo Abeni } 128ddb1a072SPaolo Abeni 129ddb1a072SPaolo Abeni return msk->subflow; 130ddb1a072SPaolo Abeni } 131ddb1a072SPaolo Abeni 132ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 133ab174ad8SPaolo Abeni { 134ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 135ab174ad8SPaolo Abeni __kfree_skb(skb); 136ab174ad8SPaolo Abeni } 137ab174ad8SPaolo Abeni 1386511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1396511882cSPaolo Abeni { 1406511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1416511882cSPaolo Abeni } 1426511882cSPaolo Abeni 1438268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1448268ed4cSPaolo Abeni struct sk_buff *from) 1458268ed4cSPaolo Abeni { 1468268ed4cSPaolo Abeni bool fragstolen; 1478268ed4cSPaolo Abeni int delta; 1488268ed4cSPaolo Abeni 1498268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1508268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1518268ed4cSPaolo Abeni return false; 1528268ed4cSPaolo Abeni 15306242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 15406242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 15506242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 156ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1577288ff6eSPaolo Abeni 1587288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1597288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1607288ff6eSPaolo Abeni * negative one 1617288ff6eSPaolo Abeni */ 1628268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1636511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1647288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1657288ff6eSPaolo Abeni 1668268ed4cSPaolo Abeni return true; 1678268ed4cSPaolo Abeni } 1688268ed4cSPaolo Abeni 169ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 170ab174ad8SPaolo Abeni struct sk_buff *from) 1716771bfd9SFlorian Westphal { 172ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 173ab174ad8SPaolo Abeni return false; 174ab174ad8SPaolo Abeni 175ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 176ab174ad8SPaolo Abeni } 177ab174ad8SPaolo Abeni 1786511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1796511882cSPaolo Abeni { 180100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 181100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 1826511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1836511882cSPaolo Abeni } 1846511882cSPaolo Abeni 1856511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1866511882cSPaolo Abeni { 1876511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1886511882cSPaolo Abeni int reclaimable; 1896511882cSPaolo Abeni 1906511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1916511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1926511882cSPaolo Abeni 1936511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 194d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 195d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1966511882cSPaolo Abeni } 1976511882cSPaolo Abeni 1986511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 1996511882cSPaolo Abeni { 2006511882cSPaolo Abeni unsigned int len = skb->truesize; 2016511882cSPaolo Abeni struct sock *sk = skb->sk; 2026511882cSPaolo Abeni 2036511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 2046511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 2056511882cSPaolo Abeni } 2066511882cSPaolo Abeni 20736b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 2086511882cSPaolo Abeni { 2096511882cSPaolo Abeni skb_orphan(skb); 2106511882cSPaolo Abeni skb->sk = sk; 2116511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2126511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2136511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2146511882cSPaolo Abeni } 2156511882cSPaolo Abeni 216ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 217ab174ad8SPaolo Abeni * - use mptcp seqs 218ab174ad8SPaolo Abeni * - don't cope with sacks 219ab174ad8SPaolo Abeni */ 220ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 221ab174ad8SPaolo Abeni { 222ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 223ab174ad8SPaolo Abeni struct rb_node **p, *parent; 224ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 225ab174ad8SPaolo Abeni struct sk_buff *skb1; 226ab174ad8SPaolo Abeni 227ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 228ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 229f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 230ab174ad8SPaolo Abeni 23106242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 23206242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 233fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 234ab174ad8SPaolo Abeni /* out of window */ 235ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 236fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 237fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 238f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 23906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 240ab174ad8SPaolo Abeni return; 241ab174ad8SPaolo Abeni } 242ab174ad8SPaolo Abeni 243ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 24406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 245ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 246ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 247ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 248ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 249ab174ad8SPaolo Abeni goto end; 250ab174ad8SPaolo Abeni } 251ab174ad8SPaolo Abeni 252ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 253ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 254ab174ad8SPaolo Abeni */ 25506242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 25606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 25706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 258ab174ad8SPaolo Abeni return; 25906242e44SPaolo Abeni } 260ab174ad8SPaolo Abeni 261ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 262ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 26306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 264ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 265ab174ad8SPaolo Abeni p = &parent->rb_right; 266ab174ad8SPaolo Abeni goto insert; 267ab174ad8SPaolo Abeni } 268ab174ad8SPaolo Abeni 269ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 270ab174ad8SPaolo Abeni parent = NULL; 271ab174ad8SPaolo Abeni while (*p) { 272ab174ad8SPaolo Abeni parent = *p; 273ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 274ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 275ab174ad8SPaolo Abeni p = &parent->rb_left; 276ab174ad8SPaolo Abeni continue; 277ab174ad8SPaolo Abeni } 278ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 279ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 280ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 281ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 28206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 283ab174ad8SPaolo Abeni return; 284ab174ad8SPaolo Abeni } 285ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 286ab174ad8SPaolo Abeni /* partial overlap: 287ab174ad8SPaolo Abeni * | skb | 288ab174ad8SPaolo Abeni * | skb1 | 289ab174ad8SPaolo Abeni * continue traversing 290ab174ad8SPaolo Abeni */ 291ab174ad8SPaolo Abeni } else { 292ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 293ab174ad8SPaolo Abeni * Replace skb1 with skb. 294ab174ad8SPaolo Abeni */ 295ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 296ab174ad8SPaolo Abeni &msk->out_of_order_queue); 297ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 29806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 299ab174ad8SPaolo Abeni goto merge_right; 300ab174ad8SPaolo Abeni } 301ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 30206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 303ab174ad8SPaolo Abeni return; 304ab174ad8SPaolo Abeni } 305ab174ad8SPaolo Abeni p = &parent->rb_right; 306ab174ad8SPaolo Abeni } 30706242e44SPaolo Abeni 308ab174ad8SPaolo Abeni insert: 309ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 310ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 311ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 312ab174ad8SPaolo Abeni 313ab174ad8SPaolo Abeni merge_right: 314ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 315ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 316ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 317ab174ad8SPaolo Abeni break; 318ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 319ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 32006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 321ab174ad8SPaolo Abeni } 322ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 323ab174ad8SPaolo Abeni if (!skb1) 324ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 325ab174ad8SPaolo Abeni 326ab174ad8SPaolo Abeni end: 327ab174ad8SPaolo Abeni skb_condense(skb); 3286511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3296511882cSPaolo Abeni } 3306511882cSPaolo Abeni 3316511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3326511882cSPaolo Abeni { 3336511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3346511882cSPaolo Abeni int amt, amount; 3356511882cSPaolo Abeni 33669d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3376511882cSPaolo Abeni return true; 3386511882cSPaolo Abeni 33969d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3406511882cSPaolo Abeni amt = sk_mem_pages(size); 341100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3424aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3436511882cSPaolo Abeni return false; 3446511882cSPaolo Abeni 3454aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3466511882cSPaolo Abeni return true; 347ab174ad8SPaolo Abeni } 348ab174ad8SPaolo Abeni 349ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 350ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 351ab174ad8SPaolo Abeni size_t copy_len) 352ab174ad8SPaolo Abeni { 353ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3546771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3554e637c70SFlorian Westphal struct sk_buff *tail; 356b7f653b2SFlorian Westphal bool has_rxtstamp; 3576771bfd9SFlorian Westphal 3586771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3594e637c70SFlorian Westphal 3604e637c70SFlorian Westphal skb_ext_reset(skb); 3614e637c70SFlorian Westphal skb_orphan(skb); 3624e637c70SFlorian Westphal 3639c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3646511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3659c3f94e1SPaolo Abeni goto drop; 36672f96132SPaolo Abeni 367b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 368b7f653b2SFlorian Westphal 369ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 370ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 371ab174ad8SPaolo Abeni * value 372ab174ad8SPaolo Abeni */ 373ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 374ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 375ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 376b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 377ab174ad8SPaolo Abeni 378ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 379ab174ad8SPaolo Abeni /* in sequence */ 380917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3814e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3828268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 383ab174ad8SPaolo Abeni return true; 3844e637c70SFlorian Westphal 3856511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3866771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 387ab174ad8SPaolo Abeni return true; 388ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 389ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 390ab174ad8SPaolo Abeni return false; 391ab174ad8SPaolo Abeni } 392ab174ad8SPaolo Abeni 393ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 394ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 395ab174ad8SPaolo Abeni */ 39606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3979c3f94e1SPaolo Abeni drop: 398ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 399ab174ad8SPaolo Abeni return false; 4006771bfd9SFlorian Westphal } 4016771bfd9SFlorian Westphal 40216a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 40316a9a9daSMat Martineau { 40416a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 40516a9a9daSMat Martineau 40616a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 40716a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 40816a9a9daSMat Martineau } 40916a9a9daSMat Martineau 410e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 411e16163b6SPaolo Abeni { 412e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 413e16163b6SPaolo Abeni return; 414e16163b6SPaolo Abeni 415e16163b6SPaolo Abeni sk->sk_state_change(sk); 416e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 417e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 418e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 419e16163b6SPaolo Abeni else 420e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 421e16163b6SPaolo Abeni } 422e16163b6SPaolo Abeni 4236e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4246e628cd3SPaolo Abeni { 4256e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4266e628cd3SPaolo Abeni 4276e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 4286e628cd3SPaolo Abeni ((1 << sk->sk_state) & 4296e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4306e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4316e628cd3SPaolo Abeni } 4326e628cd3SPaolo Abeni 43316a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 43416a9a9daSMat Martineau { 43516a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 43616a9a9daSMat Martineau 43716a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4386e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 43916a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 44016a9a9daSMat Martineau 44116a9a9daSMat Martineau switch (sk->sk_state) { 44216a9a9daSMat Martineau case TCP_FIN_WAIT1: 44316a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 44416a9a9daSMat Martineau break; 44516a9a9daSMat Martineau case TCP_CLOSING: 44616a9a9daSMat Martineau case TCP_LAST_ACK: 44716a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 44816a9a9daSMat Martineau break; 44916a9a9daSMat Martineau } 45016a9a9daSMat Martineau 451e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 45216a9a9daSMat Martineau } 45316a9a9daSMat Martineau } 45416a9a9daSMat Martineau 4553721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4563721b9b6SMat Martineau { 4573721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4583721b9b6SMat Martineau 4593721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4603721b9b6SMat Martineau ((1 << sk->sk_state) & 4613721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4623721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4633721b9b6SMat Martineau 4643721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4653721b9b6SMat Martineau if (seq) 4663721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4673721b9b6SMat Martineau 4683721b9b6SMat Martineau return true; 4693721b9b6SMat Martineau } 4703721b9b6SMat Martineau } 4713721b9b6SMat Martineau 4723721b9b6SMat Martineau return false; 4733721b9b6SMat Martineau } 4743721b9b6SMat Martineau 475403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk) 4766477dd39SMat Martineau { 4776477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 478877d11f0SMat Martineau u32 retransmits; 4796477dd39SMat Martineau 480877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 481877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 482877d11f0SMat Martineau 483877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4846477dd39SMat Martineau } 4856477dd39SMat Martineau 48633d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4873721b9b6SMat Martineau { 4883721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4893721b9b6SMat Martineau } 4903721b9b6SMat Martineau 49133d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 49233d41c9cSPaolo Abeni { 49333d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 49433d41c9cSPaolo Abeni 49571b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 49671b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 49733d41c9cSPaolo Abeni } 49833d41c9cSPaolo Abeni 49933d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 50033d41c9cSPaolo Abeni { 50133d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 50233d41c9cSPaolo Abeni long tout = 0; 50333d41c9cSPaolo Abeni 50433d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 50533d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 50633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 50733d41c9cSPaolo Abeni } 50833d41c9cSPaolo Abeni 50976a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 510fd897679SPaolo Abeni { 511fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 51220bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 513fd897679SPaolo Abeni } 514fd897679SPaolo Abeni 515a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 516a6574302SMat Martineau { 517a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 518a6574302SMat Martineau tcp_send_ack(ssk); 519a6574302SMat Martineau } 520a6574302SMat Martineau 521f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5227ed90803SPaolo Abeni { 52375e908c3SPaolo Abeni bool slow; 5247ed90803SPaolo Abeni 52575e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 526a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 52775e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 528fd897679SPaolo Abeni } 529340fa666SMat Martineau 530340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 531340fa666SMat Martineau { 532340fa666SMat Martineau struct mptcp_subflow_context *subflow; 533340fa666SMat Martineau 534340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 535340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 536ea4ca586SPaolo Abeni } 537ea4ca586SPaolo Abeni 538fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 539fd897679SPaolo Abeni { 54075e908c3SPaolo Abeni bool slow; 541fd897679SPaolo Abeni 54275e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 543fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 544fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 54575e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 546fde56eeaSPaolo Abeni } 547fde56eeaSPaolo Abeni 548fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 549fde56eeaSPaolo Abeni { 550fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 551c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 552fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 553fde56eeaSPaolo Abeni 554fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 555fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 556fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 557fde56eeaSPaolo Abeni (rx_empty && ack_pending & 558fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 559ea4ca586SPaolo Abeni } 560fd897679SPaolo Abeni 561fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 562fd897679SPaolo Abeni { 563e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 564fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 565e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 566fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 567fde56eeaSPaolo Abeni bool cleanup, rx_empty; 568e3859603SPaolo Abeni 569fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 570ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 571fd897679SPaolo Abeni 572fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 573fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 574fd897679SPaolo Abeni 575fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 576fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5777ed90803SPaolo Abeni } 5787ed90803SPaolo Abeni } 5797ed90803SPaolo Abeni 5807ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5813721b9b6SMat Martineau { 5823721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5833721b9b6SMat Martineau u64 rcv_data_fin_seq; 5847ed90803SPaolo Abeni bool ret = false; 5853721b9b6SMat Martineau 586781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5877ed90803SPaolo Abeni return ret; 5883721b9b6SMat Martineau 5893721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5903721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5913721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5923721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5933721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5943721b9b6SMat Martineau * the msk state. 5953721b9b6SMat Martineau * 5963721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5973721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5983721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5993721b9b6SMat Martineau * when catching up. 6003721b9b6SMat Martineau */ 6013721b9b6SMat Martineau 6023721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 603917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 6043721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 6053721b9b6SMat Martineau 6066b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 60716a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 6083721b9b6SMat Martineau 6093721b9b6SMat Martineau switch (sk->sk_state) { 6103721b9b6SMat Martineau case TCP_ESTABLISHED: 6113721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6123721b9b6SMat Martineau break; 6133721b9b6SMat Martineau case TCP_FIN_WAIT1: 6143721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6153721b9b6SMat Martineau break; 6163721b9b6SMat Martineau case TCP_FIN_WAIT2: 6173721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6183721b9b6SMat Martineau break; 6193721b9b6SMat Martineau default: 6203721b9b6SMat Martineau /* Other states not expected */ 6213721b9b6SMat Martineau WARN_ON_ONCE(1); 6223721b9b6SMat Martineau break; 6233721b9b6SMat Martineau } 6243721b9b6SMat Martineau 6257ed90803SPaolo Abeni ret = true; 626fd897679SPaolo Abeni mptcp_send_ack(msk); 627e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6283721b9b6SMat Martineau } 6297ed90803SPaolo Abeni return ret; 6303721b9b6SMat Martineau } 6313721b9b6SMat Martineau 6326771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6336771bfd9SFlorian Westphal struct sock *ssk, 6346771bfd9SFlorian Westphal unsigned int *bytes) 6356771bfd9SFlorian Westphal { 6366771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 637600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6386771bfd9SFlorian Westphal unsigned int moved = 0; 6396771bfd9SFlorian Westphal bool more_data_avail; 6406771bfd9SFlorian Westphal struct tcp_sock *tp; 6416771bfd9SFlorian Westphal bool done = false; 64213c7ba0cSFlorian Westphal int sk_rbuf; 64313c7ba0cSFlorian Westphal 64413c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 64513c7ba0cSFlorian Westphal 64613c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 64713c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 64813c7ba0cSFlorian Westphal 64913c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 65013c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 65113c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 65213c7ba0cSFlorian Westphal } 65313c7ba0cSFlorian Westphal } 654600911ffSFlorian Westphal 655ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6566771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6576771bfd9SFlorian Westphal do { 6586771bfd9SFlorian Westphal u32 map_remaining, offset; 6596771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6606771bfd9SFlorian Westphal struct sk_buff *skb; 6616771bfd9SFlorian Westphal bool fin; 6626771bfd9SFlorian Westphal 6636771bfd9SFlorian Westphal /* try to move as much data as available */ 6646771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6656771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6666771bfd9SFlorian Westphal 6676771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 668d9fb8c50SPaolo Abeni if (!skb) { 669d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 670d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 671d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 672d9fb8c50SPaolo Abeni */ 673d9fb8c50SPaolo Abeni if (!moved) 674d9fb8c50SPaolo Abeni done = true; 6756771bfd9SFlorian Westphal break; 676d9fb8c50SPaolo Abeni } 6776771bfd9SFlorian Westphal 678e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 679d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 680d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 681d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 682e1ff9e82SDavide Caratti */ 683e1ff9e82SDavide Caratti map_remaining = skb->len; 684e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 685e1ff9e82SDavide Caratti } 686e1ff9e82SDavide Caratti 6876771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6886771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6896771bfd9SFlorian Westphal if (fin) { 6906771bfd9SFlorian Westphal done = true; 6916771bfd9SFlorian Westphal seq++; 6926771bfd9SFlorian Westphal } 6936771bfd9SFlorian Westphal 6946771bfd9SFlorian Westphal if (offset < skb->len) { 6956771bfd9SFlorian Westphal size_t len = skb->len - offset; 6966771bfd9SFlorian Westphal 6976771bfd9SFlorian Westphal if (tp->urg_data) 6986771bfd9SFlorian Westphal done = true; 6996771bfd9SFlorian Westphal 700ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 7016771bfd9SFlorian Westphal moved += len; 702ab174ad8SPaolo Abeni seq += len; 7036771bfd9SFlorian Westphal 7046771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 7056771bfd9SFlorian Westphal break; 7066771bfd9SFlorian Westphal } else { 7076771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 7086771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 7096771bfd9SFlorian Westphal done = true; 7106771bfd9SFlorian Westphal } 7116771bfd9SFlorian Westphal 7126771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7136771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 714600911ffSFlorian Westphal 71513c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 716600911ffSFlorian Westphal done = true; 717600911ffSFlorian Westphal break; 718600911ffSFlorian Westphal } 7196771bfd9SFlorian Westphal } while (more_data_avail); 7206771bfd9SFlorian Westphal 7216719331cSPaolo Abeni *bytes += moved; 7226771bfd9SFlorian Westphal return done; 7236771bfd9SFlorian Westphal } 7246771bfd9SFlorian Westphal 72587952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 726ab174ad8SPaolo Abeni { 727ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 728ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 729ab174ad8SPaolo Abeni bool moved = false; 730ab174ad8SPaolo Abeni struct rb_node *p; 731ab174ad8SPaolo Abeni u64 end_seq; 732ab174ad8SPaolo Abeni 733ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 73406242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 735ab174ad8SPaolo Abeni while (p) { 736ab174ad8SPaolo Abeni skb = rb_to_skb(p); 737ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 738ab174ad8SPaolo Abeni break; 739ab174ad8SPaolo Abeni 740ab174ad8SPaolo Abeni p = rb_next(p); 741ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 742ab174ad8SPaolo Abeni 743ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 744ab174ad8SPaolo Abeni msk->ack_seq))) { 745ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 74606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 747ab174ad8SPaolo Abeni continue; 748ab174ad8SPaolo Abeni } 749ab174ad8SPaolo Abeni 750ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 751ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 752ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 753ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 754ab174ad8SPaolo Abeni 755ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 75606242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 75706242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 75806242e44SPaolo Abeni delta); 759ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7602c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 761ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 762ab174ad8SPaolo Abeni } 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 828*55b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 829*55b47ca7SPaolo Abeni { 830*55b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 831*55b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 832*55b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 833*55b47ca7SPaolo Abeni } 834*55b47ca7SPaolo 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 8483e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 849*55b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 8503e501490SPaolo Abeni return true; 85178962489SFlorian Westphal } 85278962489SFlorian Westphal 8533e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk) 85478962489SFlorian Westphal { 8553e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8563e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 85778962489SFlorian Westphal 8583e501490SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 8593e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8603e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8613e501490SPaolo Abeni 8623e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8633e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8643e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8653e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 86678962489SFlorian Westphal } 867ec3edaa7SPeter Krystad } 868ec3edaa7SPeter Krystad 869b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 870b51f9b80SPaolo Abeni { 871b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 872b51f9b80SPaolo Abeni } 873b51f9b80SPaolo Abeni 874b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 875b51f9b80SPaolo Abeni { 876b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 877b51f9b80SPaolo Abeni unsigned long tout; 878b51f9b80SPaolo Abeni 879e16163b6SPaolo Abeni /* prevent rescheduling on close */ 880e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 881e16163b6SPaolo Abeni return; 882e16163b6SPaolo Abeni 88333d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 884b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 885b51f9b80SPaolo Abeni } 886b51f9b80SPaolo Abeni 887ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 888ba8f48f7SPaolo Abeni { 889ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 890ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 891ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 892ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 893ba8f48f7SPaolo Abeni */ 894ba8f48f7SPaolo Abeni sock_hold(sk); 895ba8f48f7SPaolo Abeni return true; 896ba8f48f7SPaolo Abeni } 897ba8f48f7SPaolo Abeni return false; 898ba8f48f7SPaolo Abeni } 899ba8f48f7SPaolo Abeni 90059832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 90159832e24SFlorian Westphal { 902ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 903ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 90459832e24SFlorian Westphal } 90559832e24SFlorian Westphal 9065969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 9075969856aSPaolo Abeni { 9085969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 9095969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 9105969856aSPaolo Abeni int receivers = 0; 9115969856aSPaolo Abeni 9125969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 9135969856aSPaolo Abeni receivers += !subflow->rx_eof; 914e16163b6SPaolo Abeni if (receivers) 915e16163b6SPaolo Abeni return; 9165969856aSPaolo Abeni 917e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 9185969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 9195969856aSPaolo Abeni * to msk, when all subflows agree on it 9205969856aSPaolo Abeni */ 9216b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 9225969856aSPaolo Abeni 9235969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9245969856aSPaolo Abeni sk->sk_data_ready(sk); 9255969856aSPaolo Abeni } 926e16163b6SPaolo Abeni 927e16163b6SPaolo Abeni switch (sk->sk_state) { 928e16163b6SPaolo Abeni case TCP_ESTABLISHED: 929e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 930e16163b6SPaolo Abeni break; 931e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 93226aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 93326aa2314SPaolo Abeni break; 93426aa2314SPaolo Abeni case TCP_FIN_WAIT2: 935e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 936e16163b6SPaolo Abeni break; 937e16163b6SPaolo Abeni default: 938e16163b6SPaolo Abeni return; 939e16163b6SPaolo Abeni } 940e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9415969856aSPaolo Abeni } 9425969856aSPaolo Abeni 9437a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9447a6a6cbcSPaolo Abeni { 9457a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9467a6a6cbcSPaolo Abeni 947109cdeb8SGeliang Tang msk_owned_by_me(msk); 9487a6a6cbcSPaolo Abeni 9497a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 95099d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9517a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9527a6a6cbcSPaolo Abeni } 9537a6a6cbcSPaolo Abeni 9547a6a6cbcSPaolo Abeni return NULL; 9557a6a6cbcSPaolo Abeni } 9567a6a6cbcSPaolo Abeni 9573f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 95857040755SPaolo Abeni const struct sk_buff *skb, 95957040755SPaolo Abeni const struct mptcp_ext *mpext) 9606d0060f6SMat Martineau { 96157040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 96257040755SPaolo Abeni return false; 96357040755SPaolo Abeni 9645a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9655a369ca6SPaolo Abeni * mapping has not been xmitted yet 9665a369ca6SPaolo Abeni */ 9675a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9685a369ca6SPaolo Abeni !mpext->frozen; 96957040755SPaolo Abeni } 97057040755SPaolo Abeni 97129249eacSPaolo Abeni /* we can append data to the given data frag if: 97229249eacSPaolo Abeni * - there is space available in the backing page_frag 97329249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 97429249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 97529249eacSPaolo Abeni */ 97618b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 97718b683bfSPaolo Abeni const struct page_frag *pfrag, 97818b683bfSPaolo Abeni const struct mptcp_data_frag *df) 97918b683bfSPaolo Abeni { 98018b683bfSPaolo Abeni return df && pfrag->page == df->page && 981d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 98229249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 98318b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 98418b683bfSPaolo Abeni } 98518b683bfSPaolo Abeni 986d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 98718b683bfSPaolo Abeni { 988d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9897948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 990d027236cSPaolo Abeni } 991d027236cSPaolo Abeni 992d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 993d027236cSPaolo Abeni { 994d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 995d027236cSPaolo Abeni 99618b683bfSPaolo Abeni list_del(&dfrag->list); 997d027236cSPaolo Abeni dfrag_uncharge(sk, len); 99818b683bfSPaolo Abeni put_page(dfrag->page); 99918b683bfSPaolo Abeni } 100018b683bfSPaolo Abeni 10016e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 100218b683bfSPaolo Abeni { 100318b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100418b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1005e1ff9e82SDavide Caratti u64 snd_una; 1006e1ff9e82SDavide Caratti 1007e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 1008e1ff9e82SDavide Caratti * plain TCP 1009e1ff9e82SDavide Caratti */ 1010e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10117439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10127439d687SPaolo Abeni 10137439d687SPaolo Abeni snd_una = msk->snd_una; 101418b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 101518b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 101618b683bfSPaolo Abeni break; 101718b683bfSPaolo Abeni 10181e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10191e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10201e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1021d9ca1de8SPaolo Abeni break; 10221e1d9d6fSPaolo Abeni 10231e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10241e1d9d6fSPaolo Abeni } 10251e1d9d6fSPaolo Abeni 1026d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1027d027236cSPaolo Abeni } 1028d027236cSPaolo Abeni 10297948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10307948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 103153eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 103253eb4c38SPaolo Abeni 10331e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10341e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10351e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 103653eb4c38SPaolo Abeni goto out; 10371e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10381e1d9d6fSPaolo Abeni goto out; 10391e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10401e1d9d6fSPaolo Abeni } 10417948f6ccSFlorian Westphal 10427948f6ccSFlorian Westphal dfrag->data_seq += delta; 104353eb4c38SPaolo Abeni dfrag->offset += delta; 10447948f6ccSFlorian Westphal dfrag->data_len -= delta; 1045d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10467948f6ccSFlorian Westphal 10477948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10487948f6ccSFlorian Westphal } 10497948f6ccSFlorian Westphal 10501e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10511e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10521e1d9d6fSPaolo Abeni msk->recovery = false; 10531e1d9d6fSPaolo Abeni 105453eb4c38SPaolo Abeni out: 10553241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10563241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 105733d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10586e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10596e628cd3SPaolo Abeni } else { 10606e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10616e628cd3SPaolo Abeni } 10626e628cd3SPaolo Abeni } 10636e628cd3SPaolo Abeni 1064417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1065417789dfSPaolo Abeni { 1066765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1067765ff425SPaolo Abeni 1068417789dfSPaolo Abeni __mptcp_clean_una(sk); 1069417789dfSPaolo Abeni mptcp_write_space(sk); 1070417789dfSPaolo Abeni } 1071417789dfSPaolo Abeni 1072b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1073b5941f06SPaolo Abeni { 1074b5941f06SPaolo Abeni mptcp_data_lock(sk); 1075b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1076b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1077b5941f06SPaolo Abeni } 1078b5941f06SPaolo Abeni 1079724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 108018b683bfSPaolo Abeni { 1081d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1082d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1083d9ca1de8SPaolo Abeni bool first = true; 1084d9ca1de8SPaolo Abeni 108518b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1086d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1087d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1088d9ca1de8SPaolo Abeni 1089d9ca1de8SPaolo Abeni if (first) 1090d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1091d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1092d9ca1de8SPaolo Abeni first = false; 1093d9ca1de8SPaolo Abeni } 1094724cfd2eSPaolo Abeni } 1095724cfd2eSPaolo Abeni 1096724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1097724cfd2eSPaolo Abeni * data 1098724cfd2eSPaolo Abeni */ 1099724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1100724cfd2eSPaolo Abeni { 1101724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1102724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1103724cfd2eSPaolo Abeni return true; 1104724cfd2eSPaolo Abeni 1105724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 110618b683bfSPaolo Abeni return false; 110718b683bfSPaolo Abeni } 110818b683bfSPaolo Abeni 110918b683bfSPaolo Abeni static struct mptcp_data_frag * 111018b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 111118b683bfSPaolo Abeni int orig_offset) 111218b683bfSPaolo Abeni { 111318b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 111418b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 111518b683bfSPaolo Abeni 111618b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 111718b683bfSPaolo Abeni dfrag->data_len = 0; 111818b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 111918b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 112018b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1121d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 112218b683bfSPaolo Abeni dfrag->page = pfrag->page; 112318b683bfSPaolo Abeni 112418b683bfSPaolo Abeni return dfrag; 112518b683bfSPaolo Abeni } 112618b683bfSPaolo Abeni 1127caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1128caf971dfSPaolo Abeni int mss_now; 1129caf971dfSPaolo Abeni int size_goal; 1130d9ca1de8SPaolo Abeni u16 limit; 1131d9ca1de8SPaolo Abeni u16 sent; 1132d9ca1de8SPaolo Abeni unsigned int flags; 11331094c6feSPaolo Abeni bool data_lock_held; 1134caf971dfSPaolo Abeni }; 1135caf971dfSPaolo Abeni 1136b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1137b713d006SPaolo Abeni u64 data_seq, int avail_size) 11386f8a612aSFlorian Westphal { 11396f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1140b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11416f8a612aSFlorian Westphal 11426f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11436f8a612aSFlorian Westphal return avail_size; 11446f8a612aSFlorian Westphal 1145b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1146b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11476f8a612aSFlorian Westphal 114892be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1149b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 115092be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 115192be2f52SPaolo Abeni } 11526f8a612aSFlorian Westphal 11536f8a612aSFlorian Westphal return avail_size; 11546f8a612aSFlorian Westphal } 11556f8a612aSFlorian Westphal 1156724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1157724cfd2eSPaolo Abeni { 1158724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1159724cfd2eSPaolo Abeni 1160724cfd2eSPaolo Abeni if (!mpext) 1161724cfd2eSPaolo Abeni return false; 1162724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1163724cfd2eSPaolo Abeni return true; 1164724cfd2eSPaolo Abeni } 1165724cfd2eSPaolo Abeni 11666e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1167724cfd2eSPaolo Abeni { 1168724cfd2eSPaolo Abeni struct sk_buff *skb; 1169724cfd2eSPaolo Abeni 11706e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1171724cfd2eSPaolo Abeni if (likely(skb)) { 11726e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1173724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1174a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1175f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1176724cfd2eSPaolo Abeni return skb; 1177724cfd2eSPaolo Abeni } 1178724cfd2eSPaolo Abeni __kfree_skb(skb); 1179724cfd2eSPaolo Abeni } else { 1180724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1181724cfd2eSPaolo Abeni } 1182724cfd2eSPaolo Abeni return NULL; 1183724cfd2eSPaolo Abeni } 1184724cfd2eSPaolo Abeni 1185f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1186724cfd2eSPaolo Abeni { 1187724cfd2eSPaolo Abeni struct sk_buff *skb; 1188724cfd2eSPaolo Abeni 11896e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1190724cfd2eSPaolo Abeni if (!skb) 1191f70cad10SPaolo Abeni return NULL; 1192724cfd2eSPaolo Abeni 1193724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1194f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1195f70cad10SPaolo Abeni return skb; 1196724cfd2eSPaolo Abeni } 11973ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1198724cfd2eSPaolo Abeni kfree_skb(skb); 1199f70cad10SPaolo Abeni return NULL; 1200724cfd2eSPaolo Abeni } 1201724cfd2eSPaolo Abeni 1202f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1203724cfd2eSPaolo Abeni { 12041094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1205724cfd2eSPaolo Abeni 12061094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1207724cfd2eSPaolo Abeni } 1208724cfd2eSPaolo Abeni 1209d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1210d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1211d0cc2987SGeliang Tang */ 1212d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1213d0cc2987SGeliang Tang { 1214d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1215d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1216d0cc2987SGeliang Tang int offset = skb->len - added; 1217d0cc2987SGeliang Tang 1218d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1219d0cc2987SGeliang Tang } 1220d0cc2987SGeliang Tang 12211e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12221e39e5a3SGeliang Tang struct sock *ssk, 12231e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12241e39e5a3SGeliang Tang { 12251e39e5a3SGeliang Tang if (!mpext) 12261e39e5a3SGeliang Tang return; 12271e39e5a3SGeliang Tang 12281e39e5a3SGeliang Tang mpext->infinite_map = 1; 12291e39e5a3SGeliang Tang mpext->data_len = 0; 12301e39e5a3SGeliang Tang 1231104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12321e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12331e39e5a3SGeliang Tang pr_fallback(msk); 1234d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12351e39e5a3SGeliang Tang } 12361e39e5a3SGeliang Tang 123757040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1238d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1239caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 124057040755SPaolo Abeni { 1241d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1242f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12436d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12446f8a612aSFlorian Westphal bool zero_window_probe = false; 12456d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1246f70cad10SPaolo Abeni bool can_coalesce = false; 1247f70cad10SPaolo Abeni bool reuse_skb = true; 1248f70cad10SPaolo Abeni struct sk_buff *skb; 1249f70cad10SPaolo Abeni size_t copy; 1250f70cad10SPaolo Abeni int i; 12516d0060f6SMat Martineau 1252e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1253d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12546d0060f6SMat Martineau 1255f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1256f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1257f70cad10SPaolo Abeni return 0; 1258f70cad10SPaolo Abeni 1259c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1260c886d702SPaolo Abeni return -EAGAIN; 1261c886d702SPaolo Abeni 1262d9ca1de8SPaolo Abeni /* compute send limit */ 1263d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1264f70cad10SPaolo Abeni copy = info->size_goal; 1265f70cad10SPaolo Abeni 126657040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1267f70cad10SPaolo Abeni if (skb && copy > skb->len) { 126857040755SPaolo Abeni /* Limit the write to the size available in the 126957040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 127057040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 127157040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 127257040755SPaolo Abeni * SSN association set here 127357040755SPaolo Abeni */ 1274d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12751094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 127657040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12771094c6feSPaolo Abeni goto alloc_skb; 12781094c6feSPaolo Abeni } 12791094c6feSPaolo Abeni 1280f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1281f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1282657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1283f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1284f70cad10SPaolo Abeni goto alloc_skb; 128515e6ca97SPaolo Abeni } 128618b683bfSPaolo Abeni 1287f70cad10SPaolo Abeni copy -= skb->len; 1288f70cad10SPaolo Abeni } else { 12891094c6feSPaolo Abeni alloc_skb: 1290f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1291f70cad10SPaolo Abeni if (!skb) 1292f70cad10SPaolo Abeni return -ENOMEM; 1293f70cad10SPaolo Abeni 1294f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1295f70cad10SPaolo Abeni reuse_skb = false; 1296f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1297f70cad10SPaolo Abeni } 12981094c6feSPaolo Abeni 12996f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1300b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1301f70cad10SPaolo Abeni if (copy == 0) { 13027439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 13037439d687SPaolo Abeni 1304f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 130527728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 13066f8a612aSFlorian Westphal return 0; 13076f8a612aSFlorian Westphal } 13086f8a612aSFlorian Westphal 1309f70cad10SPaolo Abeni zero_window_probe = true; 1310f70cad10SPaolo Abeni data_seq = snd_una - 1; 1311f70cad10SPaolo Abeni copy = 1; 131257040755SPaolo Abeni 1313f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1314f70cad10SPaolo Abeni * ssk write queue 1315f70cad10SPaolo Abeni */ 1316f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1317f70cad10SPaolo Abeni } 1318f70cad10SPaolo Abeni 1319f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1320f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 132127728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1322e2223995SPaolo Abeni return -ENOMEM; 132335759383SFlorian Westphal } 132418b683bfSPaolo Abeni 1325f70cad10SPaolo Abeni if (can_coalesce) { 1326f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1327f70cad10SPaolo Abeni } else { 1328f70cad10SPaolo Abeni get_page(dfrag->page); 1329f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 133057040755SPaolo Abeni } 133157040755SPaolo Abeni 1332f70cad10SPaolo Abeni skb->len += copy; 1333f70cad10SPaolo Abeni skb->data_len += copy; 1334f70cad10SPaolo Abeni skb->truesize += copy; 1335f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1336f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1337f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1338f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1339f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1340f70cad10SPaolo Abeni 1341f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1342f70cad10SPaolo Abeni if (reuse_skb) { 1343f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1344f70cad10SPaolo Abeni mpext->data_len += copy; 1345f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1346f70cad10SPaolo Abeni goto out; 1347724cfd2eSPaolo Abeni } 13486d0060f6SMat Martineau 13496d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1350d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13516d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1352f70cad10SPaolo Abeni mpext->data_len = copy; 13536d0060f6SMat Martineau mpext->use_map = 1; 13546d0060f6SMat Martineau mpext->dsn64 = 1; 13556d0060f6SMat Martineau 13566d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13576d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13586d0060f6SMat Martineau mpext->dsn64); 13596d0060f6SMat Martineau 13606f8a612aSFlorian Westphal if (zero_window_probe) { 1361f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13626f8a612aSFlorian Westphal mpext->frozen = 1; 1363d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1364f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13656f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1366d0cc2987SGeliang Tang return 0; 13676f8a612aSFlorian Westphal } 136857040755SPaolo Abeni out: 1369d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1370f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13711e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13721e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13730eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1374f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1375f70cad10SPaolo Abeni return copy; 13766d0060f6SMat Martineau } 13776d0060f6SMat Martineau 1378d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1379d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1380d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1381d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1382d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1383d5f49190SPaolo Abeni 1384d5f49190SPaolo Abeni struct subflow_send_info { 1385d5f49190SPaolo Abeni struct sock *ssk; 13863ce0852cSPaolo Abeni u64 linger_time; 1387d5f49190SPaolo Abeni }; 1388d5f49190SPaolo Abeni 1389ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1390ff5a0b42SPaolo Abeni { 1391ff5a0b42SPaolo Abeni if (!subflow->stale) 1392ff5a0b42SPaolo Abeni return; 1393ff5a0b42SPaolo Abeni 1394ff5a0b42SPaolo Abeni subflow->stale = 0; 1395fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1396ff5a0b42SPaolo Abeni } 1397ff5a0b42SPaolo Abeni 1398ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1399ff5a0b42SPaolo Abeni { 1400ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1401ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1402ff5a0b42SPaolo Abeni 1403ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1404ff5a0b42SPaolo Abeni return false; 1405ff5a0b42SPaolo Abeni 1406ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1407ff5a0b42SPaolo Abeni } 1408ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1409ff5a0b42SPaolo Abeni } 1410ff5a0b42SPaolo Abeni 14113ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14123ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14133ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14143ce0852cSPaolo Abeni 141533d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 141633d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 141733d41c9cSPaolo Abeni * additionally updates the rtx timeout 141833d41c9cSPaolo Abeni */ 14195cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1420f296234cSPeter Krystad { 14213ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1422f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 142333d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14243ce0852cSPaolo Abeni u32 pace, burst, wmem; 1425d5f49190SPaolo Abeni int i, nr_active = 0; 1426d5f49190SPaolo Abeni struct sock *ssk; 14273ce0852cSPaolo Abeni u64 linger_time; 142833d41c9cSPaolo Abeni long tout = 0; 1429f296234cSPeter Krystad 1430109cdeb8SGeliang Tang msk_owned_by_me(msk); 1431f296234cSPeter Krystad 1432d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1433d5f49190SPaolo Abeni if (!msk->first) 1434f296234cSPeter Krystad return NULL; 1435c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1436c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1437f296234cSPeter Krystad } 1438f296234cSPeter Krystad 1439d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1440d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1441d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 144233d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 144333d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1444d5f49190SPaolo Abeni return msk->last_snd; 144533d41c9cSPaolo Abeni } 1446f296234cSPeter Krystad 1447d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14483ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1449d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14503ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1451d5f49190SPaolo Abeni } 14523ce0852cSPaolo Abeni 1453d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1454e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1455d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1456d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1457f296234cSPeter Krystad continue; 1458d5f49190SPaolo Abeni 145933d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1460d5f49190SPaolo Abeni nr_active += !subflow->backup; 14613ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14623ce0852cSPaolo Abeni if (unlikely(!pace)) { 14633ce0852cSPaolo Abeni /* init pacing rate from socket */ 14643ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14653ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1466d5f49190SPaolo Abeni if (!pace) 1467d5f49190SPaolo Abeni continue; 14683ce0852cSPaolo Abeni } 1469d5f49190SPaolo Abeni 14703ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14713ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1472d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14733ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1474d5f49190SPaolo Abeni } 1475f296234cSPeter Krystad } 147633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1477f296234cSPeter Krystad 1478d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1479d5f49190SPaolo Abeni if (!nr_active) 14803ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1481d5f49190SPaolo Abeni 14823ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14833ce0852cSPaolo Abeni * faster flow, we need to: 14843ce0852cSPaolo Abeni * - estimate the faster flow linger time 14853ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14863ce0852cSPaolo Abeni * by the faster flow 14873ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14883ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14893ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14903ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14913ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14923ce0852cSPaolo Abeni */ 14933ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1494b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1495d5f49190SPaolo Abeni return NULL; 14963ce0852cSPaolo Abeni 1497b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14983ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1499b713d006SPaolo Abeni if (!burst) { 1500b713d006SPaolo Abeni msk->last_snd = NULL; 1501b713d006SPaolo Abeni return ssk; 1502b713d006SPaolo Abeni } 1503b713d006SPaolo Abeni 15043ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 15053ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 15063ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 15073ce0852cSPaolo Abeni burst + wmem); 15083ce0852cSPaolo Abeni msk->last_snd = ssk; 15093ce0852cSPaolo Abeni msk->snd_burst = burst; 15103ce0852cSPaolo Abeni return ssk; 1511f296234cSPeter Krystad } 1512f296234cSPeter Krystad 1513b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1514d9ca1de8SPaolo Abeni { 1515d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1516d9ca1de8SPaolo Abeni release_sock(ssk); 1517d9ca1de8SPaolo Abeni } 1518d9ca1de8SPaolo Abeni 15190d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15200d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15210d199e43SFlorian Westphal u32 sent) 15220d199e43SFlorian Westphal { 15230d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15240d199e43SFlorian Westphal 15250d199e43SFlorian Westphal dfrag->already_sent += sent; 15260d199e43SFlorian Westphal 15270d199e43SFlorian Westphal msk->snd_burst -= sent; 15280d199e43SFlorian Westphal 15290d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15300d199e43SFlorian Westphal 15310d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15320d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15330d199e43SFlorian Westphal * old segments. 15340d199e43SFlorian Westphal * 15350d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15360d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15370d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15380d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15390d199e43SFlorian Westphal */ 15400d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15410d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15420d199e43SFlorian Westphal } 15430d199e43SFlorian Westphal 15448b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15453241a9c0SFlorian Westphal { 1546e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1547e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15483241a9c0SFlorian Westphal } 15493241a9c0SFlorian Westphal 1550ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1551d9ca1de8SPaolo Abeni { 1552d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1553d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1554d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1555d9ca1de8SPaolo Abeni .flags = flags, 1556d9ca1de8SPaolo Abeni }; 15570522b424SGeliang Tang bool do_check_data_fin = false; 1558d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15590522b424SGeliang Tang int len; 1560d9ca1de8SPaolo Abeni 1561d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1562d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1563d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1564d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1565d9ca1de8SPaolo Abeni while (len > 0) { 1566d9ca1de8SPaolo Abeni int ret = 0; 1567d9ca1de8SPaolo Abeni 1568d9ca1de8SPaolo Abeni prev_ssk = ssk; 15695cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1570d9ca1de8SPaolo Abeni 15719758f40eSPaolo Abeni /* First check. If the ssk has changed since 15729758f40eSPaolo Abeni * the last round, release prev_ssk 1573d9ca1de8SPaolo Abeni */ 1574d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1575b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1576d9ca1de8SPaolo Abeni if (!ssk) 1577d9ca1de8SPaolo Abeni goto out; 1578d9ca1de8SPaolo Abeni 15799758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15809758f40eSPaolo Abeni * from the previous one, otherwise we are still 15819758f40eSPaolo Abeni * helding the relevant lock 15829758f40eSPaolo Abeni */ 15839758f40eSPaolo Abeni if (ssk != prev_ssk) 1584d9ca1de8SPaolo Abeni lock_sock(ssk); 1585d9ca1de8SPaolo Abeni 1586d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1587d9ca1de8SPaolo Abeni if (ret <= 0) { 1588c886d702SPaolo Abeni if (ret == -EAGAIN) 1589c886d702SPaolo Abeni continue; 1590b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1591d9ca1de8SPaolo Abeni goto out; 1592d9ca1de8SPaolo Abeni } 1593d9ca1de8SPaolo Abeni 15940522b424SGeliang Tang do_check_data_fin = true; 1595d9ca1de8SPaolo Abeni info.sent += ret; 1596d9ca1de8SPaolo Abeni len -= ret; 15970d199e43SFlorian Westphal 15980d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1599d9ca1de8SPaolo Abeni } 1600d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1601d9ca1de8SPaolo Abeni } 1602d9ca1de8SPaolo Abeni 1603d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1604d9ca1de8SPaolo Abeni if (ssk) 1605b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1606d9ca1de8SPaolo Abeni 1607d9ca1de8SPaolo Abeni out: 160833d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1609d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1610d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 16110522b424SGeliang Tang if (do_check_data_fin) 1612d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1613d9ca1de8SPaolo Abeni } 1614d9ca1de8SPaolo Abeni 161573a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 16166e628cd3SPaolo Abeni { 16176e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16181094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16191094c6feSPaolo Abeni .data_lock_held = true, 16201094c6feSPaolo Abeni }; 16216e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1622b19bc294SPaolo Abeni struct sock *xmit_ssk; 16236e628cd3SPaolo Abeni int len, copied = 0; 16246e628cd3SPaolo Abeni 16256e628cd3SPaolo Abeni info.flags = 0; 16266e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16276e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16286e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16296e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16306e628cd3SPaolo Abeni while (len > 0) { 16316e628cd3SPaolo Abeni int ret = 0; 16326e628cd3SPaolo Abeni 163373a0052aSGeliang Tang /* check for a different subflow usage only after 1634b19bc294SPaolo Abeni * spooling the first chunk of data 1635b19bc294SPaolo Abeni */ 163600df24f1SGeliang Tang xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1637b19bc294SPaolo Abeni if (!xmit_ssk) 1638b19bc294SPaolo Abeni goto out; 1639b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1640bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1641bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1642b19bc294SPaolo Abeni goto out; 1643b19bc294SPaolo Abeni } 1644b19bc294SPaolo Abeni 16456e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16466e628cd3SPaolo Abeni if (ret <= 0) 16476e628cd3SPaolo Abeni goto out; 16486e628cd3SPaolo Abeni 16496e628cd3SPaolo Abeni info.sent += ret; 16506e628cd3SPaolo Abeni copied += ret; 16516e628cd3SPaolo Abeni len -= ret; 1652b19bc294SPaolo Abeni first = false; 16530d199e43SFlorian Westphal 16540d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16556e628cd3SPaolo Abeni } 16566e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16576e628cd3SPaolo Abeni } 16586e628cd3SPaolo Abeni 16596e628cd3SPaolo Abeni out: 16606e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16616e628cd3SPaolo Abeni * not going to flush it via release_sock() 16626e628cd3SPaolo Abeni */ 16636e628cd3SPaolo Abeni if (copied) { 16646e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16656e628cd3SPaolo Abeni info.size_goal); 1666d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1667d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1668d09d818eSPaolo Abeni 16696e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16706e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16716e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16726e628cd3SPaolo Abeni } 16736e628cd3SPaolo Abeni } 16746e628cd3SPaolo Abeni 16755cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16765cf92bbaSPaolo Abeni { 16775cf92bbaSPaolo Abeni /* enable autotune */ 16785cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16795cf92bbaSPaolo Abeni 16805cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16815cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16825cf92bbaSPaolo Abeni } 16835cf92bbaSPaolo Abeni 16847d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 16857d803344SPaolo Abeni 1686a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1687fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1688fa9e5746SPaolo Abeni { 1689fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1690fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1691ddb1a072SPaolo Abeni struct socket *ssock; 1692a2702a07SPaolo Abeni struct sock *ssk; 1693fa9e5746SPaolo Abeni int ret; 1694fa9e5746SPaolo Abeni 1695a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1696a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1697a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1698a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1699a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1700a2702a07SPaolo Abeni */ 1701ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 1702ddb1a072SPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 1703ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 1704ddb1a072SPaolo Abeni return PTR_ERR(ssock); 1705ddb1a072SPaolo Abeni } 1706a2702a07SPaolo Abeni if (!msk->first) 1707a2702a07SPaolo Abeni return -EINVAL; 1708a2702a07SPaolo Abeni 1709a2702a07SPaolo Abeni ssk = msk->first; 1710a2702a07SPaolo Abeni 1711fa9e5746SPaolo Abeni lock_sock(ssk); 1712fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 17137d803344SPaolo Abeni msk->fastopening = 1; 1714fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 17157d803344SPaolo Abeni msk->fastopening = 0; 1716fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1717fa9e5746SPaolo Abeni release_sock(ssk); 1718fa9e5746SPaolo Abeni 1719fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1720fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1721fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1722fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1723fa9e5746SPaolo Abeni 1724fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1725fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1726fa9e5746SPaolo Abeni */ 1727fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1728fa9e5746SPaolo Abeni *copied_syn = 0; 17297d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 17307d803344SPaolo Abeni mptcp_disconnect(sk, 0); 1731fa9e5746SPaolo Abeni } 1732a2702a07SPaolo Abeni inet_sk(sk)->defer_connect = 0; 1733fa9e5746SPaolo Abeni 1734fa9e5746SPaolo Abeni return ret; 1735fa9e5746SPaolo Abeni } 1736fa9e5746SPaolo Abeni 1737f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1738f870fa0bSMat Martineau { 1739f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 174017091708SFlorian Westphal struct page_frag *pfrag; 17416d0060f6SMat Martineau size_t copied = 0; 1742caf971dfSPaolo Abeni int ret = 0; 17436d0060f6SMat Martineau long timeo; 1744f870fa0bSMat Martineau 1745987858e5SPaolo Abeni /* silently ignore everything else */ 17461e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1747987858e5SPaolo Abeni 17486511882cSPaolo Abeni lock_sock(sk); 17491954b860SMat Martineau 1750a2702a07SPaolo Abeni if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) { 1751d98a82a6SDmytro Shytyi int copied_syn = 0; 1752d98a82a6SDmytro Shytyi 1753a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1754d98a82a6SDmytro Shytyi copied += copied_syn; 1755fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1756d98a82a6SDmytro Shytyi goto out; 1757fa9e5746SPaolo Abeni else if (ret) 175869800e51SPaolo Abeni goto do_error; 1759d98a82a6SDmytro Shytyi } 1760d98a82a6SDmytro Shytyi 17611954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17621954b860SMat Martineau 17631954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17641954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17651954b860SMat Martineau if (ret) 176669800e51SPaolo Abeni goto do_error; 17671954b860SMat Martineau } 17681954b860SMat Martineau 176969800e51SPaolo Abeni ret = -EPIPE; 177069800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 177169800e51SPaolo Abeni goto do_error; 177269800e51SPaolo Abeni 177317091708SFlorian Westphal pfrag = sk_page_frag(sk); 177418b683bfSPaolo Abeni 1775d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1776724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1777d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1778d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1779d9ca1de8SPaolo Abeni size_t psize, offset; 1780d9ca1de8SPaolo Abeni 1781d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1782d9ca1de8SPaolo Abeni * page allocator 1783fb529e62SFlorian Westphal */ 1784d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1785d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1786d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1787d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1788d9ca1de8SPaolo Abeni goto wait_for_memory; 17896e628cd3SPaolo Abeni 1790d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1791d9ca1de8SPaolo Abeni goto wait_for_memory; 1792d9ca1de8SPaolo Abeni 1793d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1794d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1795fb529e62SFlorian Westphal } 1796fb529e62SFlorian Westphal 1797d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1798d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1799d9ca1de8SPaolo Abeni * anyway 1800d9ca1de8SPaolo Abeni */ 1801d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1802d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1803d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1804724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 18058ce568edSPaolo Abeni 18066511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1807d9ca1de8SPaolo Abeni goto wait_for_memory; 1808d9ca1de8SPaolo Abeni 1809d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1810d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1811d9ca1de8SPaolo Abeni ret = -EFAULT; 181269800e51SPaolo Abeni goto do_error; 1813d9ca1de8SPaolo Abeni } 1814d9ca1de8SPaolo Abeni 1815d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 18166511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1817d9ca1de8SPaolo Abeni copied += psize; 1818d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1819d9ca1de8SPaolo Abeni frag_truesize += psize; 1820d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1821d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1822d9ca1de8SPaolo Abeni 1823d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1824d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1825d9ca1de8SPaolo Abeni */ 1826d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1827d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1828d9ca1de8SPaolo Abeni get_page(dfrag->page); 1829d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1830d9ca1de8SPaolo Abeni if (!msk->first_pending) 1831d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1832d9ca1de8SPaolo Abeni } 1833e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1834d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1835d9ca1de8SPaolo Abeni !dfrag_collapsed); 1836d9ca1de8SPaolo Abeni 1837d9ca1de8SPaolo Abeni continue; 1838d9ca1de8SPaolo Abeni 1839d9ca1de8SPaolo Abeni wait_for_memory: 18405cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1841c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1842f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1843f296234cSPeter Krystad if (ret) 184469800e51SPaolo Abeni goto do_error; 1845cec37a6eSPeter Krystad } 1846cec37a6eSPeter Krystad 184713e16037SPaolo Abeni if (copied) 1848c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1849da51aef5SPaolo Abeni 18501954b860SMat Martineau out: 1851cec37a6eSPeter Krystad release_sock(sk); 185269800e51SPaolo Abeni return copied; 185369800e51SPaolo Abeni 185469800e51SPaolo Abeni do_error: 185569800e51SPaolo Abeni if (copied) 185669800e51SPaolo Abeni goto out; 185769800e51SPaolo Abeni 185869800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 185969800e51SPaolo Abeni goto out; 1860f870fa0bSMat Martineau } 1861f870fa0bSMat Martineau 18626771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18636771bfd9SFlorian Westphal struct msghdr *msg, 1864b7f653b2SFlorian Westphal size_t len, int flags, 1865b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1866b7f653b2SFlorian Westphal int *cmsg_flags) 18676771bfd9SFlorian Westphal { 1868ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18696771bfd9SFlorian Westphal int copied = 0; 18706771bfd9SFlorian Westphal 1871ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18726771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18736771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18746771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18756771bfd9SFlorian Westphal int err; 18766771bfd9SFlorian Westphal 1877d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18786771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18796771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18806771bfd9SFlorian Westphal if (!copied) 18816771bfd9SFlorian Westphal return err; 18826771bfd9SFlorian Westphal break; 18836771bfd9SFlorian Westphal } 1884d976092cSPaolo Abeni } 18856771bfd9SFlorian Westphal 1886b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1887b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1888b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1889b7f653b2SFlorian Westphal } 1890b7f653b2SFlorian Westphal 18916771bfd9SFlorian Westphal copied += count; 18926771bfd9SFlorian Westphal 18936771bfd9SFlorian Westphal if (count < data_len) { 18942c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18956771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18962c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18972c9e7765SFlorian Westphal } 18986771bfd9SFlorian Westphal break; 18996771bfd9SFlorian Westphal } 19006771bfd9SFlorian Westphal 1901ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 190287952603SPaolo Abeni /* we will bulk release the skb memory later */ 190387952603SPaolo Abeni skb->destructor = NULL; 1904ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 190587952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 19066771bfd9SFlorian Westphal __kfree_skb(skb); 1907ca4fb892SYonglong Li } 19086771bfd9SFlorian Westphal 19096771bfd9SFlorian Westphal if (copied >= len) 19106771bfd9SFlorian Westphal break; 19116771bfd9SFlorian Westphal } 19126771bfd9SFlorian Westphal 19136771bfd9SFlorian Westphal return copied; 19146771bfd9SFlorian Westphal } 19156771bfd9SFlorian Westphal 1916a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1917a6b118feSFlorian Westphal * 1918a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1919a6b118feSFlorian Westphal */ 1920a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1921a6b118feSFlorian Westphal { 1922a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1923a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1924a6b118feSFlorian Westphal u32 time, advmss = 1; 1925a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1926a6b118feSFlorian Westphal 1927109cdeb8SGeliang Tang msk_owned_by_me(msk); 1928a6b118feSFlorian Westphal 1929a6b118feSFlorian Westphal if (copied <= 0) 1930a6b118feSFlorian Westphal return; 1931a6b118feSFlorian Westphal 1932a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1933a6b118feSFlorian Westphal 1934a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1935a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1936a6b118feSFlorian Westphal 1937a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1938a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1939a6b118feSFlorian Westphal return; 1940a6b118feSFlorian Westphal 1941a6b118feSFlorian Westphal rtt_us = 0; 1942a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1943a6b118feSFlorian Westphal const struct tcp_sock *tp; 1944a6b118feSFlorian Westphal u64 sf_rtt_us; 1945a6b118feSFlorian Westphal u32 sf_advmss; 1946a6b118feSFlorian Westphal 1947a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1948a6b118feSFlorian Westphal 1949a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1950a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1951a6b118feSFlorian Westphal 1952a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1953a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1954a6b118feSFlorian Westphal } 1955a6b118feSFlorian Westphal 1956a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1957a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1958a6b118feSFlorian Westphal return; 1959a6b118feSFlorian Westphal 1960a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1961a6b118feSFlorian Westphal goto new_measure; 1962a6b118feSFlorian Westphal 196378047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1964a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1965a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1966a6b118feSFlorian Westphal u64 rcvwin, grow; 1967a6b118feSFlorian Westphal 1968a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1969a6b118feSFlorian Westphal 1970a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1971a6b118feSFlorian Westphal 1972a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1973a6b118feSFlorian Westphal rcvwin += (grow << 1); 1974a6b118feSFlorian Westphal 1975a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1976a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1977a6b118feSFlorian Westphal rcvmem += 128; 1978a6b118feSFlorian Westphal 1979a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1980a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 198102739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1982a6b118feSFlorian Westphal 1983a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1984a6b118feSFlorian Westphal u32 window_clamp; 1985a6b118feSFlorian Westphal 1986a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1987a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1988a6b118feSFlorian Westphal 1989a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1990a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1991a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1992a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1993a6b118feSFlorian Westphal */ 1994a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1995a6b118feSFlorian Westphal struct sock *ssk; 1996c76c6956SPaolo Abeni bool slow; 1997a6b118feSFlorian Westphal 1998a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1999c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 2000a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 2001a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 2002c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 2003c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 2004a6b118feSFlorian Westphal } 2005a6b118feSFlorian Westphal } 2006a6b118feSFlorian Westphal } 2007a6b118feSFlorian Westphal 2008a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 2009a6b118feSFlorian Westphal new_measure: 2010a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2011a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 2012a6b118feSFlorian Westphal } 2013a6b118feSFlorian Westphal 201487952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 201587952603SPaolo Abeni { 201687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 201787952603SPaolo Abeni 201887952603SPaolo Abeni if (!msk->rmem_released) 201987952603SPaolo Abeni return; 202087952603SPaolo Abeni 202187952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 20226511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 2023ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 202487952603SPaolo Abeni } 202587952603SPaolo Abeni 202687952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 202787952603SPaolo Abeni { 202887952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 202987952603SPaolo Abeni 203087952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 203187952603SPaolo Abeni } 203287952603SPaolo Abeni 2033e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20346771bfd9SFlorian Westphal { 203587952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20366771bfd9SFlorian Westphal unsigned int moved = 0; 203787952603SPaolo Abeni bool ret, done; 2038d5f49190SPaolo Abeni 20396771bfd9SFlorian Westphal do { 20406771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 204165f49fe7SFlorian Westphal bool slowpath; 20426771bfd9SFlorian Westphal 204387952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 204487952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 204587952603SPaolo Abeni * that is an unlikely slow path. 204687952603SPaolo Abeni */ 204787952603SPaolo Abeni if (likely(!ssk)) 20486771bfd9SFlorian Westphal break; 20496771bfd9SFlorian Westphal 205065f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 205187952603SPaolo Abeni mptcp_data_lock(sk); 2052e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20536771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 205487952603SPaolo Abeni mptcp_data_unlock(sk); 2055499ada50SPaolo Abeni 2056499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2057499ada50SPaolo Abeni __mptcp_error_report(sk); 205865f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20596771bfd9SFlorian Westphal } while (!done); 20606771bfd9SFlorian Westphal 206187952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 206287952603SPaolo Abeni ret = moved > 0; 206387952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 206487952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 206587952603SPaolo Abeni mptcp_data_lock(sk); 206687952603SPaolo Abeni __mptcp_update_rmem(sk); 206787952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 206887952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 206987952603SPaolo Abeni mptcp_data_unlock(sk); 2070ab174ad8SPaolo Abeni } 207187952603SPaolo Abeni if (ret) 207287952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 207387952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20746771bfd9SFlorian Westphal } 20756771bfd9SFlorian Westphal 20762c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20772c9e7765SFlorian Westphal { 20782c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20792c9e7765SFlorian Westphal const struct sk_buff *skb; 20802c9e7765SFlorian Westphal 20812c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20822c9e7765SFlorian Westphal if (skb) { 20832c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20842c9e7765SFlorian Westphal 20852c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20862c9e7765SFlorian Westphal return INT_MAX; 20872c9e7765SFlorian Westphal 20882c9e7765SFlorian Westphal return (unsigned int)hint_val; 20892c9e7765SFlorian Westphal } 20902c9e7765SFlorian Westphal 20912c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20922c9e7765SFlorian Westphal return 1; 20932c9e7765SFlorian Westphal 20942c9e7765SFlorian Westphal return 0; 20952c9e7765SFlorian Westphal } 20962c9e7765SFlorian Westphal 2097f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2098ec095263SOliver Hartkopp int flags, int *addr_len) 2099f870fa0bSMat Martineau { 2100f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2101b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2102b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 21037a6a6cbcSPaolo Abeni int target; 21047a6a6cbcSPaolo Abeni long timeo; 2105f870fa0bSMat Martineau 2106cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2107cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2108cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2109cb9d80f4SPaolo Abeni 21106511882cSPaolo Abeni lock_sock(sk); 2111fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2112fd897679SPaolo Abeni copied = -ENOTCONN; 2113fd897679SPaolo Abeni goto out_err; 2114fd897679SPaolo Abeni } 2115fd897679SPaolo Abeni 2116ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 21177a6a6cbcSPaolo Abeni 21187a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 21197a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 21207a6a6cbcSPaolo Abeni 21212c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 21222c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 21232c9e7765SFlorian Westphal 212405e3eceaSEric Dumazet while (copied < len) { 2125e3859603SPaolo Abeni int bytes_read; 21267a6a6cbcSPaolo Abeni 2127b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21286771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21297a6a6cbcSPaolo Abeni if (!copied) 21307a6a6cbcSPaolo Abeni copied = bytes_read; 21316771bfd9SFlorian Westphal goto out_err; 2132cec37a6eSPeter Krystad } 2133cec37a6eSPeter Krystad 21347a6a6cbcSPaolo Abeni copied += bytes_read; 21357a6a6cbcSPaolo Abeni 2136ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2137fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2138ea4ca586SPaolo Abeni 2139e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2140e3859603SPaolo Abeni continue; 2141e3859603SPaolo Abeni 21427a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21437a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21447a6a6cbcSPaolo Abeni */ 21457a6a6cbcSPaolo Abeni if (copied >= target) 21467a6a6cbcSPaolo Abeni break; 21477a6a6cbcSPaolo Abeni 21487a6a6cbcSPaolo Abeni if (copied) { 21497a6a6cbcSPaolo Abeni if (sk->sk_err || 21507a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21517a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21527a6a6cbcSPaolo Abeni !timeo || 21537a6a6cbcSPaolo Abeni signal_pending(current)) 21547a6a6cbcSPaolo Abeni break; 21557a6a6cbcSPaolo Abeni } else { 21567a6a6cbcSPaolo Abeni if (sk->sk_err) { 21577a6a6cbcSPaolo Abeni copied = sock_error(sk); 21587a6a6cbcSPaolo Abeni break; 21597a6a6cbcSPaolo Abeni } 21607a6a6cbcSPaolo Abeni 21615969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 21625969856aSPaolo Abeni mptcp_check_for_eof(msk); 21635969856aSPaolo Abeni 216487952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 216587952603SPaolo Abeni /* race breaker: the shutdown could be after the 216687952603SPaolo Abeni * previous receive queue check 216787952603SPaolo Abeni */ 2168e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 216987952603SPaolo Abeni continue; 21707a6a6cbcSPaolo Abeni break; 217187952603SPaolo Abeni } 21727a6a6cbcSPaolo Abeni 21737a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21747a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21757a6a6cbcSPaolo Abeni break; 21767a6a6cbcSPaolo Abeni } 21777a6a6cbcSPaolo Abeni 21787a6a6cbcSPaolo Abeni if (!timeo) { 21797a6a6cbcSPaolo Abeni copied = -EAGAIN; 21807a6a6cbcSPaolo Abeni break; 21817a6a6cbcSPaolo Abeni } 21827a6a6cbcSPaolo Abeni 21837a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21847a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21857a6a6cbcSPaolo Abeni break; 21867a6a6cbcSPaolo Abeni } 21877a6a6cbcSPaolo Abeni } 21887a6a6cbcSPaolo Abeni 21897a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2190612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21917a6a6cbcSPaolo Abeni } 21923c90e377SPaolo Abeni 21936771bfd9SFlorian Westphal out_err: 2194b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2195b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2196b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21972c9e7765SFlorian Westphal 21982c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21992c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 22002c9e7765SFlorian Westphal 22012c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 22022c9e7765SFlorian Westphal } 2203b7f653b2SFlorian Westphal } 2204b7f653b2SFlorian Westphal 2205612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2206612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2207612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2208ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2209a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2210a6b118feSFlorian Westphal 2211cec37a6eSPeter Krystad release_sock(sk); 2212cec37a6eSPeter Krystad return copied; 2213cec37a6eSPeter Krystad } 2214cec37a6eSPeter Krystad 2215b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2216b51f9b80SPaolo Abeni { 2217b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2218b51f9b80SPaolo Abeni icsk_retransmit_timer); 2219b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 22202d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2221b51f9b80SPaolo Abeni 2222b51f9b80SPaolo Abeni bh_lock_sock(sk); 2223b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22242d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22252d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22262d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2227b51f9b80SPaolo Abeni } else { 2228b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2229e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2230b51f9b80SPaolo Abeni } 2231b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2232b51f9b80SPaolo Abeni sock_put(sk); 2233b51f9b80SPaolo Abeni } 2234b51f9b80SPaolo Abeni 2235e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2236e16163b6SPaolo Abeni { 2237e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2238e16163b6SPaolo Abeni 2239e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2240b6d69fc8SFlorian Westphal sock_put(sk); 2241e16163b6SPaolo Abeni } 2242e16163b6SPaolo Abeni 22433b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22443b1d6210SPaolo Abeni * level. 22453b1d6210SPaolo Abeni * 22463b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22473b1d6210SPaolo Abeni */ 2248ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22493b1d6210SPaolo Abeni { 225071b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22513b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 225271b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22533b1d6210SPaolo Abeni 2254109cdeb8SGeliang Tang msk_owned_by_me(msk); 22553b1d6210SPaolo Abeni 2256d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2257d9ca1de8SPaolo Abeni return NULL; 2258d5f49190SPaolo Abeni 22593b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22603b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22613b1d6210SPaolo Abeni 2262ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2263d5f49190SPaolo Abeni continue; 2264d5f49190SPaolo Abeni 226571b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 226671b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 226771b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 226871b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2269860975c6SFlorian Westphal continue; 2270860975c6SFlorian Westphal } 22713b1d6210SPaolo Abeni 22723b1d6210SPaolo Abeni if (subflow->backup) { 22733b1d6210SPaolo Abeni if (!backup) 22743b1d6210SPaolo Abeni backup = ssk; 22753b1d6210SPaolo Abeni continue; 22763b1d6210SPaolo Abeni } 22773b1d6210SPaolo Abeni 227871b7dec2SPaolo Abeni if (!pick) 227971b7dec2SPaolo Abeni pick = ssk; 22803b1d6210SPaolo Abeni } 22813b1d6210SPaolo Abeni 228271b7dec2SPaolo Abeni if (pick) 228371b7dec2SPaolo Abeni return pick; 228471b7dec2SPaolo Abeni 228571b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 228671b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22873b1d6210SPaolo Abeni } 22883b1d6210SPaolo Abeni 228917aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 229017aee05dSFlorian Westphal { 229117aee05dSFlorian Westphal if (msk->subflow) { 229217aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 22935b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 229417aee05dSFlorian Westphal } 229517aee05dSFlorian Westphal } 229617aee05dSFlorian Westphal 22971e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22981e1d9d6fSPaolo Abeni { 22991e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 23001e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 23011e1d9d6fSPaolo Abeni 230200df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 23031e1d9d6fSPaolo Abeni return false; 23041e1d9d6fSPaolo Abeni 23051e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 23061e1d9d6fSPaolo Abeni return false; 23071e1d9d6fSPaolo Abeni 23081e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 23091e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 23101e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 23111e1d9d6fSPaolo Abeni */ 23121e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 23131e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 23141e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 23151e1d9d6fSPaolo Abeni if (!rtx_head) { 23161e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23171e1d9d6fSPaolo Abeni return false; 23181e1d9d6fSPaolo Abeni } 23191e1d9d6fSPaolo Abeni 23201e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 23211e1d9d6fSPaolo Abeni msk->recovery = true; 23221e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23231e1d9d6fSPaolo Abeni 23241e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23251e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23261e1d9d6fSPaolo Abeni 23271e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23281e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23291e1d9d6fSPaolo Abeni if (!cur->already_sent) 23301e1d9d6fSPaolo Abeni break; 23311e1d9d6fSPaolo Abeni cur->already_sent = 0; 23321e1d9d6fSPaolo Abeni } 23331e1d9d6fSPaolo Abeni 23341e1d9d6fSPaolo Abeni return true; 23351e1d9d6fSPaolo Abeni } 23361e1d9d6fSPaolo Abeni 2337b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2338b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2339b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2340b29fcfb5SPaolo Abeni 2341cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2342cec37a6eSPeter Krystad * (accept). 2343cec37a6eSPeter Krystad * 2344cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2345cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2346cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2347cec37a6eSPeter Krystad * parent socket. 2348cec37a6eSPeter Krystad */ 2349a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2350b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2351b29fcfb5SPaolo Abeni unsigned int flags) 2352cec37a6eSPeter Krystad { 2353e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 235463740448SPaolo Abeni bool dispose_it, need_push = false; 235563740448SPaolo Abeni 235663740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 235763740448SPaolo Abeni * to an incoming reset, mptcp either: 235863740448SPaolo Abeni * - if either the subflow or the msk are dead, destroy the context 235963740448SPaolo Abeni * (the subflow socket is deleted by inet_child_forget) and the msk 236063740448SPaolo Abeni * - otherwise do nothing at the moment and take action at accept and/or 236163740448SPaolo Abeni * listener shutdown - user-space must be able to accept() the closed 236263740448SPaolo Abeni * socket. 236363740448SPaolo Abeni */ 236463740448SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk) { 236563740448SPaolo Abeni if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 236663740448SPaolo Abeni return; 236763740448SPaolo Abeni 236863740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 236963740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 237063740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 237163740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 237263740448SPaolo Abeni goto out_release; 237363740448SPaolo Abeni } 2374e0be4931SFlorian Westphal 2375b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2376b29fcfb5SPaolo Abeni if (dispose_it) 2377cec37a6eSPeter Krystad list_del(&subflow->node); 2378cec37a6eSPeter Krystad 23793f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2380e16163b6SPaolo Abeni 2381d21f8348SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) { 2382d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2383d21f8348SPaolo Abeni * to generate the egress reset 2384d21f8348SPaolo Abeni */ 2385d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2386d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2387b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2388d21f8348SPaolo Abeni } 2389b29fcfb5SPaolo Abeni 2390b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2391b29fcfb5SPaolo Abeni if (!dispose_it) { 2392b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2393b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2394b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2395b29fcfb5SPaolo Abeni release_sock(ssk); 2396b29fcfb5SPaolo Abeni 2397b29fcfb5SPaolo Abeni goto out; 2398b29fcfb5SPaolo Abeni } 2399b29fcfb5SPaolo Abeni 2400d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2401d7b1bfd0SPaolo Abeni 2402e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2403e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2404e16163b6SPaolo Abeni * reference owned by msk; 2405e16163b6SPaolo Abeni */ 2406e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2407b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2408e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2409e16163b6SPaolo Abeni } else { 2410d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 24112a6a870eSPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 24122a6a870eSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 24132a6a870eSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 24142a6a870eSPaolo Abeni inet_csk_listen_stop(ssk); 2415f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 24162a6a870eSPaolo Abeni } 24170a3f4f1fSPaolo Abeni 2418e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2419e16163b6SPaolo Abeni 2420e16163b6SPaolo Abeni /* close acquired an extra ref */ 2421e16163b6SPaolo Abeni __sock_put(ssk); 2422e16163b6SPaolo Abeni } 242363740448SPaolo Abeni 242463740448SPaolo Abeni out_release: 2425e16163b6SPaolo Abeni release_sock(ssk); 2426e16163b6SPaolo Abeni 2427e16163b6SPaolo Abeni sock_put(ssk); 2428e0be4931SFlorian Westphal 2429c8fe62f0SFlorian Westphal if (ssk == msk->first) 24301b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2431c8fe62f0SFlorian Westphal 2432b29fcfb5SPaolo Abeni out: 2433b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2434b29fcfb5SPaolo Abeni msk->last_snd = NULL; 24351e1d9d6fSPaolo Abeni 24361e1d9d6fSPaolo Abeni if (need_push) 24371e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2438f870fa0bSMat Martineau } 2439f870fa0bSMat Martineau 2440a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2441a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2442a141e02eSFlorian Westphal { 2443b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2444b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2445a88c9e49SPaolo Abeni 2446a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2447a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2448a88c9e49SPaolo Abeni */ 2449a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2450a88c9e49SPaolo Abeni 2451b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2452a141e02eSFlorian Westphal } 2453a141e02eSFlorian Westphal 2454dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2455dc24f8b4SPaolo Abeni { 2456dc24f8b4SPaolo Abeni return 0; 2457dc24f8b4SPaolo Abeni } 2458dc24f8b4SPaolo Abeni 2459b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24600e4f35d7SPaolo Abeni { 24610e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2462b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24630e4f35d7SPaolo Abeni 24643abc05d9SFlorian Westphal might_sleep(); 24653abc05d9SFlorian Westphal 24665efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24670e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24680e4f35d7SPaolo Abeni 24690e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24700e4f35d7SPaolo Abeni continue; 24710e4f35d7SPaolo Abeni 247240947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 247340947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 247440947e13SFlorian Westphal continue; 247540947e13SFlorian Westphal 2476b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2477b6985b9bSPaolo Abeni } 2478b6985b9bSPaolo Abeni 24790e4f35d7SPaolo Abeni } 24800e4f35d7SPaolo Abeni 248163740448SPaolo Abeni static bool mptcp_should_close(const struct sock *sk) 2482e16163b6SPaolo Abeni { 2483e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2484e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2485e16163b6SPaolo Abeni 248663740448SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2487e16163b6SPaolo Abeni return true; 2488e16163b6SPaolo Abeni 2489e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2490e16163b6SPaolo Abeni * timeout 2491e16163b6SPaolo Abeni */ 2492e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2493e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2494e16163b6SPaolo Abeni TCP_CLOSE) 2495e16163b6SPaolo Abeni return false; 2496e16163b6SPaolo Abeni } 2497e16163b6SPaolo Abeni return true; 2498e16163b6SPaolo Abeni } 2499e16163b6SPaolo Abeni 250050c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 250150c504a2SFlorian Westphal { 250250c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 250380638684SGeliang Tang struct sock *sk = (struct sock *)msk; 250450c504a2SFlorian Westphal 250550c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 250650c504a2SFlorian Westphal return; 250750c504a2SFlorian Westphal 250850c504a2SFlorian Westphal mptcp_token_destroy(msk); 250950c504a2SFlorian Westphal 25105efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 251150c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 251275e908c3SPaolo Abeni bool slow; 251350c504a2SFlorian Westphal 251475e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 251550c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 251650c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 251750c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 251850c504a2SFlorian Westphal } 251975e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 252050c504a2SFlorian Westphal } 252150c504a2SFlorian Westphal 252269800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 252369800e51SPaolo Abeni switch (sk->sk_state) { 252469800e51SPaolo Abeni case TCP_SYN_SENT: 25259ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 252669800e51SPaolo Abeni break; 252769800e51SPaolo Abeni case TCP_CLOSE_WAIT: 25289ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 252969800e51SPaolo Abeni break; 253069800e51SPaolo Abeni case TCP_CLOSE: 253169800e51SPaolo Abeni return; 253269800e51SPaolo Abeni default: 25339ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 253469800e51SPaolo Abeni } 253569800e51SPaolo Abeni 253650c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 25376b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 253850c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 253950c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 254050c504a2SFlorian Westphal 254169800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 254269800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 254369800e51SPaolo Abeni return; 254469800e51SPaolo Abeni 254569800e51SPaolo Abeni sk->sk_state_change(sk); 254669800e51SPaolo Abeni sk_error_report(sk); 254750c504a2SFlorian Westphal } 254850c504a2SFlorian Westphal 25492948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 255080992017SPaolo Abeni { 25512948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2552caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25533b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25543b1d6210SPaolo Abeni size_t copied = 0; 25552948d0a1SPaolo Abeni struct sock *ssk; 25562948d0a1SPaolo Abeni int ret; 25572948d0a1SPaolo Abeni 2558b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25593241a9c0SFlorian Westphal 25603241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25613241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25622948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25636477dd39SMat Martineau if (!dfrag) { 25646477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25656477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25666477dd39SMat Martineau 25676477dd39SMat Martineau icsk->icsk_retransmits++; 25686477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25696477dd39SMat Martineau mptcp_send_ack(msk); 25706477dd39SMat Martineau 25716477dd39SMat Martineau goto reset_timer; 25726477dd39SMat Martineau } 25736477dd39SMat Martineau 25743241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25752948d0a1SPaolo Abeni return; 25763241a9c0SFlorian Westphal 25773241a9c0SFlorian Westphal goto reset_timer; 25786477dd39SMat Martineau } 25792948d0a1SPaolo Abeni 25802948d0a1SPaolo Abeni if (!ssk) 25812948d0a1SPaolo Abeni goto reset_timer; 25822948d0a1SPaolo Abeni 25832948d0a1SPaolo Abeni lock_sock(ssk); 25842948d0a1SPaolo Abeni 25852948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25862948d0a1SPaolo Abeni info.sent = 0; 25874e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25884e14867dSPaolo Abeni while (info.sent < info.limit) { 25892948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25902948d0a1SPaolo Abeni if (ret <= 0) 25912948d0a1SPaolo Abeni break; 25922948d0a1SPaolo Abeni 25932948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25942948d0a1SPaolo Abeni copied += ret; 25952948d0a1SPaolo Abeni info.sent += ret; 25962948d0a1SPaolo Abeni } 25974e14867dSPaolo Abeni if (copied) { 25984e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 25992948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 26002948d0a1SPaolo Abeni info.size_goal); 26010530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 26024e14867dSPaolo Abeni } 26032948d0a1SPaolo Abeni 26042948d0a1SPaolo Abeni release_sock(ssk); 26052948d0a1SPaolo Abeni 26062948d0a1SPaolo Abeni reset_timer: 26073241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 26083241a9c0SFlorian Westphal 26092948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 26102948d0a1SPaolo Abeni mptcp_reset_timer(sk); 26112948d0a1SPaolo Abeni } 26122948d0a1SPaolo Abeni 261376a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 261476a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 261576a13b31SGeliang Tang */ 261676a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 261776a13b31SGeliang Tang { 261876a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 261976a13b31SGeliang Tang unsigned long timeout, close_timeout; 262076a13b31SGeliang Tang 262176a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 262276a13b31SGeliang Tang return; 262376a13b31SGeliang Tang 262476a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 262576a13b31SGeliang Tang 262676a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 262776a13b31SGeliang Tang * them is active 262876a13b31SGeliang Tang */ 262976a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 263076a13b31SGeliang Tang 263176a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 263276a13b31SGeliang Tang } 263376a13b31SGeliang Tang 263449fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 263549fa1919SGeliang Tang { 263676a13b31SGeliang Tang struct sock *ssk = msk->first; 263749fa1919SGeliang Tang bool slow; 263849fa1919SGeliang Tang 263976a13b31SGeliang Tang if (!ssk) 264076a13b31SGeliang Tang return; 264176a13b31SGeliang Tang 264249fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 264349fa1919SGeliang Tang 264449fa1919SGeliang Tang slow = lock_sock_fast(ssk); 264549fa1919SGeliang Tang mptcp_subflow_reset(ssk); 264676a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 264749fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 264876a13b31SGeliang Tang 264976a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 265049fa1919SGeliang Tang } 265149fa1919SGeliang Tang 2652d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2653d21f8348SPaolo Abeni { 2654d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2655d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2656d21f8348SPaolo Abeni 2657d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2658d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2659d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2660d21f8348SPaolo Abeni } 2661d21f8348SPaolo Abeni 26622948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26632948d0a1SPaolo Abeni { 26642948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 266580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 266676a13b31SGeliang Tang unsigned long fail_tout; 26672948d0a1SPaolo Abeni int state; 266880992017SPaolo Abeni 266980992017SPaolo Abeni lock_sock(sk); 2670e16163b6SPaolo Abeni state = sk->sk_state; 2671d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2672e16163b6SPaolo Abeni goto unlock; 2673e16163b6SPaolo Abeni 267443b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 267550c504a2SFlorian Westphal 267650c504a2SFlorian Westphal mptcp_check_fastclose(msk); 267750c504a2SFlorian Westphal 2678e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2679b416268bSFlorian Westphal 268059832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 268159832e24SFlorian Westphal mptcp_check_for_eof(msk); 268259832e24SFlorian Westphal 26836e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 268443b54c6eSMat Martineau mptcp_check_data_fin(sk); 268543b54c6eSMat Martineau 2686b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2687b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2688b6985b9bSPaolo Abeni 2689341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2690341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2691341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2692e16163b6SPaolo Abeni */ 2693d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 269463740448SPaolo Abeni if (mptcp_should_close(sk)) { 2695e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2696d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2697d21f8348SPaolo Abeni } 2698d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2699e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2700e16163b6SPaolo Abeni goto unlock; 2701e16163b6SPaolo Abeni } 2702d21f8348SPaolo Abeni } 2703e16163b6SPaolo Abeni 27042948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 27052948d0a1SPaolo Abeni __mptcp_retrans(sk); 27063b1d6210SPaolo Abeni 270776a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 270876a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 270949fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 271049fa1919SGeliang Tang 27113b1d6210SPaolo Abeni unlock: 271280992017SPaolo Abeni release_sock(sk); 271380992017SPaolo Abeni sock_put(sk); 271480992017SPaolo Abeni } 271580992017SPaolo Abeni 2716784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2717f870fa0bSMat Martineau { 2718cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2719cec37a6eSPeter Krystad 2720cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2721ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 272218b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 272380992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 272487952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2725ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2726f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 27276511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2728ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 272933d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2730cec37a6eSPeter Krystad 27311b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2732dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2733752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 27340530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 27351e1d9d6fSPaolo Abeni msk->recovery = false; 27368ab183deSPaolo Abeni 27371b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27381b1c7a0eSPeter Krystad 2739b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2740b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2741e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2742aa1fbd94SFlorian Westphal 2743f870fa0bSMat Martineau return 0; 2744f870fa0bSMat Martineau } 2745f870fa0bSMat Martineau 2746b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2747784325e9SMatthieu Baerts { 274820b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2749b29fcfb5SPaolo Abeni 2750b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2751b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2752b29fcfb5SPaolo Abeni 2753b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2754b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2755b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2756b29fcfb5SPaolo Abeni } 2757b29fcfb5SPaolo Abeni 2758b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2759b29fcfb5SPaolo Abeni { 2760fc518953SFlorian Westphal struct net *net = sock_net(sk); 2761fc518953SFlorian Westphal int ret; 276218b683bfSPaolo Abeni 2763b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2764b6c08380SGeliang Tang if (ret) 2765b6c08380SGeliang Tang return ret; 2766b6c08380SGeliang Tang 2767fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2768fc518953SFlorian Westphal return -ENOPROTOOPT; 2769fc518953SFlorian Westphal 2770fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2771fc518953SFlorian Westphal return -ENOMEM; 2772fc518953SFlorian Westphal 2773a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2774a5ef058dSPaolo Abeni 277520b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 277620b5759fSPaolo Abeni * propagate the correct value 277720b5759fSPaolo Abeni */ 2778b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 277920b5759fSPaolo Abeni 2780d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2781a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2782a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2783d027236cSPaolo Abeni 278418b683bfSPaolo Abeni return 0; 278518b683bfSPaolo Abeni } 278618b683bfSPaolo Abeni 278718b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 278818b683bfSPaolo Abeni { 278918b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 279018b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 279118b683bfSPaolo Abeni 2792d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 279318b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2794d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2795784325e9SMatthieu Baerts } 2796784325e9SMatthieu Baerts 279730e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 279880992017SPaolo Abeni { 279980992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 280080992017SPaolo Abeni 2801b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2802e16163b6SPaolo Abeni __sock_put(sk); 280380992017SPaolo Abeni } 280480992017SPaolo Abeni 2805d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 280621498490SPeter Krystad { 280721498490SPeter Krystad lock_sock(ssk); 280821498490SPeter Krystad 280921498490SPeter Krystad switch (ssk->sk_state) { 281021498490SPeter Krystad case TCP_LISTEN: 281121498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 281221498490SPeter Krystad break; 2813df561f66SGustavo A. R. Silva fallthrough; 281421498490SPeter Krystad case TCP_SYN_SENT: 281521498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 281621498490SPeter Krystad break; 281721498490SPeter Krystad default: 281843b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 281943b54c6eSMat Martineau pr_debug("Fallback"); 282021498490SPeter Krystad ssk->sk_shutdown |= how; 282121498490SPeter Krystad tcp_shutdown(ssk, how); 282243b54c6eSMat Martineau } else { 282343b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 282443b54c6eSMat Martineau tcp_send_ack(ssk); 28256477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 28266477dd39SMat Martineau mptcp_reset_timer(sk); 282743b54c6eSMat Martineau } 282821498490SPeter Krystad break; 282921498490SPeter Krystad } 283021498490SPeter Krystad 283121498490SPeter Krystad release_sock(ssk); 283221498490SPeter Krystad } 283321498490SPeter Krystad 28346920b851SMat Martineau static const unsigned char new_state[16] = { 28356920b851SMat Martineau /* current state: new state: action: */ 28366920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 28376920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28386920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 28396920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28406920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 28416920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 28426920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28436920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28446920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28456920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28466920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28476920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28486920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28496920b851SMat Martineau }; 28506920b851SMat Martineau 28516920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28526920b851SMat Martineau { 28536920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28546920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28556920b851SMat Martineau 28566920b851SMat Martineau inet_sk_state_store(sk, ns); 28576920b851SMat Martineau 28586920b851SMat Martineau return next & TCP_ACTION_FIN; 28596920b851SMat Martineau } 28606920b851SMat Martineau 2861e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2862e16163b6SPaolo Abeni { 2863e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2864e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2865e16163b6SPaolo Abeni 2866e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2867e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2868e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2869e16163b6SPaolo Abeni 2870e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2871e16163b6SPaolo Abeni * skip this 2872e16163b6SPaolo Abeni */ 2873e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2874e16163b6SPaolo Abeni mptcp_send_head(sk)) 2875e16163b6SPaolo Abeni return; 2876e16163b6SPaolo Abeni 2877e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2878e16163b6SPaolo Abeni 287926aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 288026aa2314SPaolo Abeni * state now 288126aa2314SPaolo Abeni */ 288226aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 288358cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 288426aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2885e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2886e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 288726aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 288826aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 288926aa2314SPaolo Abeni } 2890e16163b6SPaolo Abeni } 2891e16163b6SPaolo Abeni 2892e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2893e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2894e16163b6SPaolo Abeni 2895e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2896e16163b6SPaolo Abeni } 2897e16163b6SPaolo Abeni } 2898e16163b6SPaolo Abeni 2899e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2900e16163b6SPaolo Abeni { 2901e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2902e16163b6SPaolo Abeni 2903e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2904e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2905e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2906e16163b6SPaolo Abeni 2907e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2908e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2909e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2910e16163b6SPaolo Abeni 2911e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2912e16163b6SPaolo Abeni } 2913e16163b6SPaolo Abeni 2914e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2915f870fa0bSMat Martineau { 2916f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2917f870fa0bSMat Martineau 2918e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 29192c22c06cSFlorian Westphal 29203abc05d9SFlorian Westphal might_sleep(); 29213abc05d9SFlorian Westphal 2922bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2923e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2924e16163b6SPaolo Abeni msk->pm.status = 0; 2925b2c5b614SFlorian Westphal 2926e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2927e16163b6SPaolo Abeni 29286511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 292987952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2930e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2931e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2932aa1fbd94SFlorian Westphal 2933e16163b6SPaolo Abeni sock_put(sk); 2934e16163b6SPaolo Abeni } 2935e16163b6SPaolo Abeni 293663740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 293763740448SPaolo Abeni { 293863740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 293963740448SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 294063740448SPaolo Abeni mptcp_do_fastclose(sk); 294163740448SPaolo Abeni __mptcp_destroy_sock(sk); 294263740448SPaolo Abeni } 294363740448SPaolo Abeni 2944d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2945d21f8348SPaolo Abeni { 2946d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2947d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2948d21f8348SPaolo Abeni */ 2949d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2950d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2951d21f8348SPaolo Abeni return 0; 2952d21f8348SPaolo Abeni 2953d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2954d21f8348SPaolo Abeni } 2955d21f8348SPaolo Abeni 2956c558246eSMenglong Dong static void mptcp_listen_inuse_dec(struct sock *sk) 2957c558246eSMenglong Dong { 2958c558246eSMenglong Dong if (inet_sk_state_load(sk) == TCP_LISTEN) 2959c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2960c558246eSMenglong Dong } 2961c558246eSMenglong Dong 296226d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2963e16163b6SPaolo Abeni { 2964e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 296576a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2966e16163b6SPaolo Abeni bool do_cancel_work = false; 2967d4e85922SPaolo Abeni int subflows_alive = 0; 2968e16163b6SPaolo Abeni 29696b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2970e16163b6SPaolo Abeni 2971e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2972c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 2973e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2974e16163b6SPaolo Abeni goto cleanup; 2975e16163b6SPaolo Abeni } 2976e16163b6SPaolo Abeni 29778d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 29788d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 29798d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 29808d547809SPaolo Abeni */ 2981d21f8348SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2982d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 29838d547809SPaolo Abeni timeout = 0; 2984d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2985e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2986d21f8348SPaolo Abeni } 2987e16163b6SPaolo Abeni 2988e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2989e16163b6SPaolo Abeni 2990e16163b6SPaolo Abeni cleanup: 2991e16163b6SPaolo Abeni /* orphan all the subflows */ 2992e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 299376a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2994e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 299549054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2996e16163b6SPaolo Abeni 2997d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 2998d4e85922SPaolo Abeni 299976a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 300076a13b31SGeliang Tang * cancel the latter 300176a13b31SGeliang Tang */ 300276a13b31SGeliang Tang if (ssk == msk->first) 300376a13b31SGeliang Tang subflow->fail_tout = 0; 300476a13b31SGeliang Tang 3005fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 3006fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 3007fe948001SMenglong Dong */ 3008fe948001SMenglong Dong ssk->sk_socket = NULL; 3009fe948001SMenglong Dong ssk->sk_wq = NULL; 3010e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 3011e16163b6SPaolo Abeni } 3012e16163b6SPaolo Abeni sock_orphan(sk); 3013e16163b6SPaolo Abeni 3014d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 3015d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 3016d4e85922SPaolo Abeni */ 3017d4e85922SPaolo Abeni if (subflows_alive == 0) 3018d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3019d4e85922SPaolo Abeni 3020e16163b6SPaolo Abeni sock_hold(sk); 3021e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 302200df24f1SGeliang Tang if (msk->token) 302376a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3024b29fcfb5SPaolo Abeni 3025e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 3026e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 3027e16163b6SPaolo Abeni do_cancel_work = true; 3028e16163b6SPaolo Abeni } else { 302976a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 3030e16163b6SPaolo Abeni } 303126d3e21cSMenglong Dong 303226d3e21cSMenglong Dong return do_cancel_work; 303326d3e21cSMenglong Dong } 303426d3e21cSMenglong Dong 303526d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 303626d3e21cSMenglong Dong { 303726d3e21cSMenglong Dong bool do_cancel_work; 303826d3e21cSMenglong Dong 303926d3e21cSMenglong Dong lock_sock(sk); 304026d3e21cSMenglong Dong 304126d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 3042e16163b6SPaolo Abeni release_sock(sk); 3043e16163b6SPaolo Abeni if (do_cancel_work) 304480992017SPaolo Abeni mptcp_cancel_work(sk); 3045b911c97cSFlorian Westphal 3046e16163b6SPaolo Abeni sock_put(sk); 3047f870fa0bSMat Martineau } 3048f870fa0bSMat Martineau 30497e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3050cf7da0d6SPeter Krystad { 3051cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3052cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3053cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3054cf7da0d6SPeter Krystad 3055cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3056cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3057cf7da0d6SPeter Krystad 3058cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3059cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3060cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3061cf7da0d6SPeter Krystad } 3062cf7da0d6SPeter Krystad #endif 3063cf7da0d6SPeter Krystad 3064cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3065cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3066cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3067cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3068cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3069cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3070cf7da0d6SPeter Krystad } 3071cf7da0d6SPeter Krystad 307218b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 307318b683bfSPaolo Abeni { 307476e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 307576e2a55dSPaolo Abeni 30767d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 30777d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 30787d803344SPaolo Abeni * msk->firstsocket lock). Do nothing and leave the cleanup to the 30797d803344SPaolo Abeni * caller. 30807d803344SPaolo Abeni */ 30817d803344SPaolo Abeni if (msk->fastopening) 30827d803344SPaolo Abeni return 0; 30837d803344SPaolo Abeni 3084c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 3085b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3086b29fcfb5SPaolo Abeni 3087bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3088b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3089b29fcfb5SPaolo Abeni 309000df24f1SGeliang Tang if (msk->token) 309100df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3092b29fcfb5SPaolo Abeni 3093c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3094c0bf3c6aSPaolo Abeni * subflow 3095c0bf3c6aSPaolo Abeni */ 3096c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3097b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3098e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3099e9d09bacSPaolo Abeni msk->cb_flags = 0; 3100e9d09bacSPaolo Abeni msk->push_pending = 0; 3101b29fcfb5SPaolo Abeni msk->recovery = false; 3102b29fcfb5SPaolo Abeni msk->can_ack = false; 3103b29fcfb5SPaolo Abeni msk->fully_established = false; 3104b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3105b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3106b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3107b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3108b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3109b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3110b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 3111b29fcfb5SPaolo Abeni 31126b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3113b29fcfb5SPaolo Abeni sk_error_report(sk); 311442c556feSFlorian Westphal return 0; 311518b683bfSPaolo Abeni } 311618b683bfSPaolo Abeni 3117b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3118b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3119b0519de8SFlorian Westphal { 3120b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3121b0519de8SFlorian Westphal 3122b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3123b0519de8SFlorian Westphal } 3124b0519de8SFlorian Westphal #endif 3125b0519de8SFlorian Westphal 31267e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3127cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 31287e8b88ecSPaolo Abeni struct sock *ssk, 3129fca5c82cSPaolo Abeni struct request_sock *req) 3130b0519de8SFlorian Westphal { 313158b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3132b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 313358b09919SPaolo Abeni struct mptcp_sock *msk; 3134b0519de8SFlorian Westphal 3135b0519de8SFlorian Westphal if (!nsk) 3136b0519de8SFlorian Westphal return NULL; 3137b0519de8SFlorian Westphal 3138b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3139b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3140b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3141b0519de8SFlorian Westphal #endif 3142b0519de8SFlorian Westphal 314358b09919SPaolo Abeni __mptcp_init_sock(nsk); 314458b09919SPaolo Abeni 314558b09919SPaolo Abeni msk = mptcp_sk(nsk); 314658b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 314758b09919SPaolo Abeni msk->token = subflow_req->token; 31485b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 3149b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3150b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 315174c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 315206251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 315358b09919SPaolo Abeni 315458b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3155eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31567439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31577439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3158df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31596f8a612aSFlorian Westphal 31605e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31610c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 31627e8b88ecSPaolo Abeni 31637e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 31647e8b88ecSPaolo Abeni * not yet exposted to user-space 31657e8b88ecSPaolo Abeni */ 31667e8b88ecSPaolo Abeni inet_sk_state_store(nsk, TCP_ESTABLISHED); 31677e8b88ecSPaolo Abeni 31687e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 31697e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 31707e8b88ecSPaolo Abeni list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list); 31717e8b88ecSPaolo Abeni sock_hold(ssk); 31727e8b88ecSPaolo Abeni 31737e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 31747e8b88ecSPaolo Abeni * created mptcp socket 31757e8b88ecSPaolo Abeni */ 31767e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 31777e8b88ecSPaolo Abeni 31787e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 31797e8b88ecSPaolo Abeni * uses the correct data 31807e8b88ecSPaolo Abeni */ 31817e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 31827e8b88ecSPaolo Abeni mptcp_propagate_sndbuf(nsk, ssk); 31837e8b88ecSPaolo Abeni 31847e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 318558b09919SPaolo Abeni bh_unlock_sock(nsk); 318658b09919SPaolo Abeni 3187b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3188b0519de8SFlorian Westphal return nsk; 3189b0519de8SFlorian Westphal } 3190b0519de8SFlorian Westphal 3191a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3192a6b118feSFlorian Westphal { 3193a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3194a6b118feSFlorian Westphal 3195a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3196a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3197a6b118feSFlorian Westphal 3198a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3199a6b118feSFlorian Westphal 3200a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3201a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3202a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3203a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3204a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 32056f8a612aSFlorian Westphal 32067439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3207a6b118feSFlorian Westphal } 3208a6b118feSFlorian Westphal 3209cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3210cf7da0d6SPeter Krystad bool kern) 3211cf7da0d6SPeter Krystad { 3212cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3213cf7da0d6SPeter Krystad struct socket *listener; 3214cf7da0d6SPeter Krystad struct sock *newsk; 3215cf7da0d6SPeter Krystad 32165b825727SPaolo Abeni listener = READ_ONCE(msk->subflow); 3217cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3218cf7da0d6SPeter Krystad *err = -EINVAL; 3219cf7da0d6SPeter Krystad return NULL; 3220cf7da0d6SPeter Krystad } 3221cf7da0d6SPeter Krystad 3222cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3223cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3224cf7da0d6SPeter Krystad if (!newsk) 3225cf7da0d6SPeter Krystad return NULL; 3226cf7da0d6SPeter Krystad 3227cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3228cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3229cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3230cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3231cf7da0d6SPeter Krystad 3232cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 323358b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 323458b09919SPaolo Abeni 323558b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 323658b09919SPaolo Abeni * subflow_syn_recv_sock() 323758b09919SPaolo Abeni */ 323858b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 323958b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3240d6692b3bSFlorian Westphal goto out; 324158b09919SPaolo Abeni } 324258b09919SPaolo Abeni 3243cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 32440397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3245fc518953SFlorian Westphal } else { 3246fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3247fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3248cf7da0d6SPeter Krystad } 3249cf7da0d6SPeter Krystad 3250d6692b3bSFlorian Westphal out: 3251d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3252cf7da0d6SPeter Krystad return newsk; 3253cf7da0d6SPeter Krystad } 3254cf7da0d6SPeter Krystad 3255c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 32565c8c1640SGeliang Tang { 3257c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 325887952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 325987952603SPaolo Abeni 32606e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 32616e628cd3SPaolo Abeni 3262c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 32635efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3264c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3265c0bf3c6aSPaolo Abeni 326687952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3267b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 326887952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32696511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32705c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3271b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32726511882cSPaolo Abeni 32736511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 32746511882cSPaolo Abeni * inet_sock_destruct() will dispose it 32756511882cSPaolo Abeni */ 32766511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 32776511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 32785c8c1640SGeliang Tang mptcp_token_destroy(msk); 32795c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 32804638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 32815c8c1640SGeliang Tang } 32825c8c1640SGeliang Tang 328379c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 328479c0949eSPeter Krystad { 3285c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3286c9fd9c5fSFlorian Westphal 3287c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3288c0bf3c6aSPaolo Abeni * even the initial subflow 3289c0bf3c6aSPaolo Abeni */ 3290c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3291c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3292d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 329379c0949eSPeter Krystad } 329479c0949eSPeter Krystad 32956e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32966e628cd3SPaolo Abeni { 32976e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32986e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32996e628cd3SPaolo Abeni else 3300e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 33016e628cd3SPaolo Abeni 33026e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 33036e628cd3SPaolo Abeni mptcp_schedule_work(sk); 33046e628cd3SPaolo Abeni } 33056e628cd3SPaolo Abeni 3306219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 33076e628cd3SPaolo Abeni { 33086e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 33096e628cd3SPaolo Abeni return; 33106e628cd3SPaolo Abeni 331173a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 331273a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 331373a0052aSGeliang Tang else 3314e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 33156e628cd3SPaolo Abeni } 33166e628cd3SPaolo Abeni 3317e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3318e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3319e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3320e9d09bacSPaolo Abeni 3321e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 332214c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3323e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 332414c441b5SPaolo Abeni { 3325e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 33262d6f5a2bSPaolo Abeni 3327e9d09bacSPaolo Abeni for (;;) { 3328e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3329e9d09bacSPaolo Abeni msk->push_pending; 3330c2e6048fSPaolo Abeni if (!flags) 3331c2e6048fSPaolo Abeni break; 3332c2e6048fSPaolo Abeni 3333c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 33346e628cd3SPaolo Abeni * 33356e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 33366e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 33376e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 33386e628cd3SPaolo Abeni * the subflow socket lock 33396e628cd3SPaolo Abeni */ 3340e9d09bacSPaolo Abeni msk->push_pending = 0; 3341e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 33426e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 33433e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 33443e501490SPaolo Abeni __mptcp_flush_join_list(sk); 33452e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3346c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 33472d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 33482d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3349c2e6048fSPaolo Abeni 3350c2e6048fSPaolo Abeni cond_resched(); 33516e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 33526e628cd3SPaolo Abeni } 3353c2e6048fSPaolo Abeni 335465a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 335565a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 335665a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3357490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 335865a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3359490274b4SPaolo Abeni */ 3360e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3361490274b4SPaolo Abeni __mptcp_set_connected(sk); 3362e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 336315cc1045SPaolo Abeni __mptcp_error_report(sk); 336443f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 336543f5b111SPaolo Abeni msk->last_snd = NULL; 336665a569b0SPaolo Abeni } 33676e628cd3SPaolo Abeni 336887952603SPaolo Abeni __mptcp_update_rmem(sk); 336914c441b5SPaolo Abeni } 337014c441b5SPaolo Abeni 3371bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3372bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3373bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3374bcd97734SPaolo Abeni */ 3375bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3376bcd97734SPaolo Abeni { 3377bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3378bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3379bcd97734SPaolo Abeni unsigned long timeout; 3380bcd97734SPaolo Abeni 3381bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3382bcd97734SPaolo Abeni return; 3383bcd97734SPaolo Abeni 3384bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3385bcd97734SPaolo Abeni if (tp->srtt_us) 3386bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3387bcd97734SPaolo Abeni else 3388bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3389bcd97734SPaolo Abeni timeout += jiffies; 3390bcd97734SPaolo Abeni 3391bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3392bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3393bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3394bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3395bcd97734SPaolo Abeni } 3396bcd97734SPaolo Abeni 3397b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3398b19bc294SPaolo Abeni { 3399b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3400b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3401b19bc294SPaolo Abeni 3402bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3403b19bc294SPaolo Abeni mptcp_data_lock(sk); 3404b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 340573a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3406b19bc294SPaolo Abeni else 3407e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3408b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3409bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3410bcd97734SPaolo Abeni } 3411bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3412bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3413bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3414bcd97734SPaolo Abeni } 3415b19bc294SPaolo Abeni } 3416b19bc294SPaolo Abeni 34172c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 34182c5ebd00SPaolo Abeni { 34192c5ebd00SPaolo Abeni /* should never be called, 34202c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 34212c5ebd00SPaolo Abeni */ 34222c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 34232c5ebd00SPaolo Abeni return 0; 34242c5ebd00SPaolo Abeni } 34252c5ebd00SPaolo Abeni 34262c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 34272c5ebd00SPaolo Abeni { 34282c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 34292c5ebd00SPaolo Abeni } 34302c5ebd00SPaolo Abeni 3431cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3432f870fa0bSMat Martineau { 3433f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3434cec37a6eSPeter Krystad struct socket *ssock; 3435f870fa0bSMat Martineau 343661761231SPaolo Abeni ssock = msk->subflow; 3437cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3438cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3439cec37a6eSPeter Krystad return -EINVAL; 3440f870fa0bSMat Martineau 3441cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3442cec37a6eSPeter Krystad } 3443f870fa0bSMat Martineau 3444cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3445cec37a6eSPeter Krystad { 3446cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3447cec37a6eSPeter Krystad struct mptcp_sock *msk; 3448cec37a6eSPeter Krystad struct sock *sk; 3449f870fa0bSMat Martineau 3450cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3451cec37a6eSPeter Krystad sk = subflow->conn; 3452cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3453cec37a6eSPeter Krystad 3454648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3455648ef4b8SMat Martineau 3456b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3457648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 34586d0060f6SMat Martineau 3459cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3460cec37a6eSPeter Krystad * accessing the field below 3461cec37a6eSPeter Krystad */ 3462cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 34636d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3464eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 34657439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34661b1c7a0eSPeter Krystad 34676c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3468a6b118feSFlorian Westphal 3469a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3470f870fa0bSMat Martineau } 3471f870fa0bSMat Martineau 3472866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3473cf7da0d6SPeter Krystad { 3474cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3475cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3476cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3477cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3478cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3479cf7da0d6SPeter Krystad } 3480cf7da0d6SPeter Krystad 3481e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3482f296234cSPeter Krystad { 3483e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3484f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3485f296234cSPeter Krystad struct sock *parent = (void *)msk; 34863e501490SPaolo Abeni bool ret = true; 3487f296234cSPeter Krystad 3488f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3489f296234cSPeter Krystad 3490f296234cSPeter Krystad /* mptcp socket already closing? */ 3491dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3492dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3493f296234cSPeter Krystad return false; 3494dc87efdbSFlorian Westphal } 3495f296234cSPeter Krystad 3496*55b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 3497*55b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 3498*55b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 3499*55b47ca7SPaolo Abeni return true; 3500*55b47ca7SPaolo Abeni } 3501f296234cSPeter Krystad 35023e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 35033e501490SPaolo Abeni goto err_prohibited; 350410f6d46cSPaolo Abeni 3505*55b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 35063e501490SPaolo Abeni * handle it 350710f6d46cSPaolo Abeni */ 35083e501490SPaolo Abeni mptcp_data_lock(parent); 35093e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 35103e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 35113e501490SPaolo Abeni if (ret) { 3512e16163b6SPaolo Abeni sock_hold(ssk); 35133e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3514e16163b6SPaolo Abeni } 35153e501490SPaolo Abeni } else { 35163e501490SPaolo Abeni sock_hold(ssk); 35173e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3518e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 35193e501490SPaolo Abeni } 35203e501490SPaolo Abeni mptcp_data_unlock(parent); 35213e501490SPaolo Abeni 3522dc87efdbSFlorian Westphal if (!ret) { 35233e501490SPaolo Abeni err_prohibited: 3524dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 352510f6d46cSPaolo Abeni return false; 3526dc87efdbSFlorian Westphal } 352710f6d46cSPaolo Abeni 352810f6d46cSPaolo Abeni return true; 3529f296234cSPeter Krystad } 3530f296234cSPeter Krystad 353176e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 353276e2a55dSPaolo Abeni { 353376e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 353476e2a55dSPaolo Abeni 353576e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 353676e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 353776e2a55dSPaolo Abeni } 353876e2a55dSPaolo Abeni 35396511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 35406511882cSPaolo Abeni { 35416511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 35426511882cSPaolo Abeni } 35436511882cSPaolo Abeni 3544644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3545644807e3SFlorian Westphal { 3546644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3547644807e3SFlorian Westphal u64 delta; 3548644807e3SFlorian Westphal 3549644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3550644807e3SFlorian Westphal return -EINVAL; 3551644807e3SFlorian Westphal 3552644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3553644807e3SFlorian Westphal return 0; 3554644807e3SFlorian Westphal 3555644807e3SFlorian Westphal delta = msk->write_seq - v; 355607c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 355707c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 355807c2c7a3SPaolo Abeni 355907c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 356007c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 356107c2c7a3SPaolo Abeni * so ignore that status, too. 356207c2c7a3SPaolo Abeni */ 356307c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 356407c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 356507c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 356607c2c7a3SPaolo Abeni } 3567644807e3SFlorian Westphal if (delta > INT_MAX) 3568644807e3SFlorian Westphal delta = INT_MAX; 3569644807e3SFlorian Westphal 3570644807e3SFlorian Westphal return (int)delta; 3571644807e3SFlorian Westphal } 3572644807e3SFlorian Westphal 3573644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3574644807e3SFlorian Westphal { 3575644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3576644807e3SFlorian Westphal bool slow; 3577644807e3SFlorian Westphal int answ; 3578644807e3SFlorian Westphal 3579644807e3SFlorian Westphal switch (cmd) { 3580644807e3SFlorian Westphal case SIOCINQ: 3581644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3582644807e3SFlorian Westphal return -EINVAL; 3583644807e3SFlorian Westphal 3584644807e3SFlorian Westphal lock_sock(sk); 3585644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3586644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3587644807e3SFlorian Westphal release_sock(sk); 3588644807e3SFlorian Westphal break; 3589644807e3SFlorian Westphal case SIOCOUTQ: 3590644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3591644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3592644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3593644807e3SFlorian Westphal break; 3594644807e3SFlorian Westphal case SIOCOUTQNSD: 3595644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3596644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3597644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3598644807e3SFlorian Westphal break; 3599644807e3SFlorian Westphal default: 3600644807e3SFlorian Westphal return -ENOIOCTLCMD; 3601644807e3SFlorian Westphal } 3602644807e3SFlorian Westphal 3603644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3604644807e3SFlorian Westphal } 3605644807e3SFlorian Westphal 360654f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 360754f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 360854f1944eSPaolo Abeni { 360954f1944eSPaolo Abeni subflow->request_mptcp = 0; 361054f1944eSPaolo Abeni __mptcp_do_fallback(msk); 361154f1944eSPaolo Abeni } 361254f1944eSPaolo Abeni 361354f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 361454f1944eSPaolo Abeni { 361554f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 361654f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 361754f1944eSPaolo Abeni struct socket *ssock; 361854f1944eSPaolo Abeni int err = -EINVAL; 361954f1944eSPaolo Abeni 362054f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3621ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 3622ddb1a072SPaolo Abeni return PTR_ERR(ssock); 362354f1944eSPaolo Abeni 362454f1944eSPaolo Abeni mptcp_token_destroy(msk); 362554f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 362654f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 362754f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 362854f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 362954f1944eSPaolo Abeni * TCP option space. 363054f1944eSPaolo Abeni */ 363154f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 363254f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 363354f1944eSPaolo Abeni #endif 363454f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 363554f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 363654f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 363754f1944eSPaolo Abeni } 363854f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 363954f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 364054f1944eSPaolo Abeni 364154f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 364254f1944eSPaolo Abeni * acquired the subflow socket lock, too. 364354f1944eSPaolo Abeni */ 36447d803344SPaolo Abeni if (msk->fastopening) 3645786fc124SPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1); 364654f1944eSPaolo Abeni else 3647786fc124SPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK); 364854f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 364954f1944eSPaolo Abeni 365054f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 365154f1944eSPaolo Abeni * subflow_finish_connect() 365254f1944eSPaolo Abeni */ 365354f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 365454f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 365554f1944eSPaolo Abeni return err; 365654f1944eSPaolo Abeni } 365754f1944eSPaolo Abeni 365854f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 365954f1944eSPaolo Abeni 3660786fc124SPaolo Abeni /* silence EINPROGRESS and let the caller inet_stream_connect 3661786fc124SPaolo Abeni * handle the connection in progress 366254f1944eSPaolo Abeni */ 3663786fc124SPaolo Abeni return 0; 366454f1944eSPaolo Abeni } 366554f1944eSPaolo Abeni 3666f870fa0bSMat Martineau static struct proto mptcp_prot = { 3667f870fa0bSMat Martineau .name = "MPTCP", 3668f870fa0bSMat Martineau .owner = THIS_MODULE, 3669f870fa0bSMat Martineau .init = mptcp_init_sock, 367054f1944eSPaolo Abeni .connect = mptcp_connect, 367118b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3672f870fa0bSMat Martineau .close = mptcp_close, 3673cf7da0d6SPeter Krystad .accept = mptcp_accept, 3674717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3675717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 367676e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 367779c0949eSPeter Krystad .destroy = mptcp_destroy, 3678f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3679644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3680f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 368114c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36822c5ebd00SPaolo Abeni .hash = mptcp_hash, 36832c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3684cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36856511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3686d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36870defbb0aSEric Dumazet 3688d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36890defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36900defbb0aSEric Dumazet 3691d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3692d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3693989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3694d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3695f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36962c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3697f870fa0bSMat Martineau .no_autobind = true, 3698f870fa0bSMat Martineau }; 3699f870fa0bSMat Martineau 37002303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 37012303f994SPeter Krystad { 37022303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 37032303f994SPeter Krystad struct socket *ssock; 3704cf7da0d6SPeter Krystad int err; 37052303f994SPeter Krystad 37062303f994SPeter Krystad lock_sock(sock->sk); 3707fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3708ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3709ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 37102303f994SPeter Krystad goto unlock; 37112303f994SPeter Krystad } 37122303f994SPeter Krystad 37132303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3714cf7da0d6SPeter Krystad if (!err) 3715cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 37162303f994SPeter Krystad 37172303f994SPeter Krystad unlock: 37182303f994SPeter Krystad release_sock(sock->sk); 37192303f994SPeter Krystad return err; 37202303f994SPeter Krystad } 37212303f994SPeter Krystad 3722cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3723cf7da0d6SPeter Krystad { 3724cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3725cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 3726cf7da0d6SPeter Krystad struct socket *ssock; 3727cf7da0d6SPeter Krystad int err; 3728cf7da0d6SPeter Krystad 3729cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3730cf7da0d6SPeter Krystad 3731cfdcfeedSMenglong Dong lock_sock(sk); 3732fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3733ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3734ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 3735cf7da0d6SPeter Krystad goto unlock; 3736cf7da0d6SPeter Krystad } 3737cf7da0d6SPeter Krystad 3738fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3739cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3740cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 37415e20087dSFlorian Westphal 3742cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3743cfdcfeedSMenglong Dong inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3744c558246eSMenglong Dong if (!err) { 3745c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3746cfdcfeedSMenglong Dong mptcp_copy_inaddrs(sk, ssock->sk); 3747c558246eSMenglong Dong } 3748cf7da0d6SPeter Krystad 3749f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 3750f8c9dfbdSGeliang Tang 3751cf7da0d6SPeter Krystad unlock: 3752cfdcfeedSMenglong Dong release_sock(sk); 3753cf7da0d6SPeter Krystad return err; 3754cf7da0d6SPeter Krystad } 3755cf7da0d6SPeter Krystad 3756cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3757cf7da0d6SPeter Krystad int flags, bool kern) 3758cf7da0d6SPeter Krystad { 3759cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3760cf7da0d6SPeter Krystad struct socket *ssock; 3761cf7da0d6SPeter Krystad int err; 3762cf7da0d6SPeter Krystad 3763cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3764cf7da0d6SPeter Krystad 37655b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 376661761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 376761761231SPaolo Abeni */ 37685b825727SPaolo Abeni ssock = READ_ONCE(msk->subflow); 3769cf7da0d6SPeter Krystad if (!ssock) 377071ba088cSPaolo Abeni return -EINVAL; 3771cf7da0d6SPeter Krystad 3772cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3773d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3774cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3775cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37760397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 37770397c6d8SPaolo Abeni 3778a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3779b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3780a5ef058dSPaolo Abeni 37814d54cc32SFlorian Westphal lock_sock(newsk); 37825b950ff4SPaolo Abeni 3783cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3784cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3785cf7da0d6SPeter Krystad */ 3786190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3787cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3788cf7da0d6SPeter Krystad 3789cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3790cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3791cf7da0d6SPeter Krystad } 379263740448SPaolo Abeni 379363740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 379463740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 379563740448SPaolo Abeni */ 379663740448SPaolo Abeni if (msk->first && 379763740448SPaolo Abeni unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 379863740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 379963740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 380063740448SPaolo Abeni if (unlikely(list_empty(&msk->conn_list))) 380163740448SPaolo Abeni inet_sk_state_store(newsk, TCP_CLOSE); 380263740448SPaolo Abeni } 380363740448SPaolo Abeni 38044d54cc32SFlorian Westphal release_sock(newsk); 3805cf7da0d6SPeter Krystad } 3806cf7da0d6SPeter Krystad 3807cf7da0d6SPeter Krystad return err; 3808cf7da0d6SPeter Krystad } 3809cf7da0d6SPeter Krystad 38108edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 38118edf0864SFlorian Westphal { 38128edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 38138edf0864SFlorian Westphal 38148edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 38158edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 38168edf0864SFlorian Westphal 38175cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 38186e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 38196e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 38206e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 38218edf0864SFlorian Westphal 38226e628cd3SPaolo Abeni return 0; 38238edf0864SFlorian Westphal } 38248edf0864SFlorian Westphal 38252303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 38262303f994SPeter Krystad struct poll_table_struct *wait) 38272303f994SPeter Krystad { 38281891c4a0SFlorian Westphal struct sock *sk = sock->sk; 38298ab183deSPaolo Abeni struct mptcp_sock *msk; 38302303f994SPeter Krystad __poll_t mask = 0; 38316b9831bfSPaolo Abeni u8 shutdown; 38328a05661bSPaolo Abeni int state; 38332303f994SPeter Krystad 38341891c4a0SFlorian Westphal msk = mptcp_sk(sk); 38351891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 38361891c4a0SFlorian Westphal 38378a05661bSPaolo Abeni state = inet_sk_state_load(sk); 38386719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 383971ba088cSPaolo Abeni if (state == TCP_LISTEN) { 38405b825727SPaolo Abeni struct socket *ssock = READ_ONCE(msk->subflow); 38415b825727SPaolo Abeni 38425b825727SPaolo Abeni if (WARN_ON_ONCE(!ssock || !ssock->sk)) 384371ba088cSPaolo Abeni return 0; 384471ba088cSPaolo Abeni 38455b825727SPaolo Abeni return inet_csk_listen_poll(ssock->sk); 384671ba088cSPaolo Abeni } 38478a05661bSPaolo Abeni 38486b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 38496b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 38506b9831bfSPaolo Abeni mask |= EPOLLHUP; 38516b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 38526b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 38536b9831bfSPaolo Abeni 38548a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 38558a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 38566b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 38576b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 38586b9831bfSPaolo Abeni else 38598edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3860a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3861a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3862a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 38638a05661bSPaolo Abeni } 38641891c4a0SFlorian Westphal 3865d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 386615cc1045SPaolo Abeni smp_rmb(); 38679ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 386815cc1045SPaolo Abeni mask |= EPOLLERR; 386915cc1045SPaolo Abeni 38702303f994SPeter Krystad return mask; 38712303f994SPeter Krystad } 38722303f994SPeter Krystad 3873e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3874e42f1ac6SFlorian Westphal .family = PF_INET, 3875e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38760a3cc579SPaolo Abeni .release = inet_release, 3877e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3878786fc124SPaolo Abeni .connect = inet_stream_connect, 3879e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3880e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3881d2f77c53SPaolo Abeni .getname = inet_getname, 3882e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3883e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3884e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3885e42f1ac6SFlorian Westphal .listen = mptcp_listen, 388676e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3887e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3888e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3889e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3890e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3891e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3892e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3893e42f1ac6SFlorian Westphal }; 38942303f994SPeter Krystad 3895f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3896f870fa0bSMat Martineau .type = SOCK_STREAM, 3897f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3898f870fa0bSMat Martineau .prot = &mptcp_prot, 38992303f994SPeter Krystad .ops = &mptcp_stream_ops, 39002303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3901f870fa0bSMat Martineau }; 3902f870fa0bSMat Martineau 3903b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3904b19bc294SPaolo Abeni { 3905b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3906b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3907b19bc294SPaolo Abeni int work_done = 0; 3908b19bc294SPaolo Abeni 3909b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3910b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3911b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3912b19bc294SPaolo Abeni 3913b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3914b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3915b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3916b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3917b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3918b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3919b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3920b19bc294SPaolo Abeni * CPU that scheduled it. 3921b19bc294SPaolo Abeni */ 3922b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3923b19bc294SPaolo Abeni sock_put(ssk); 3924b19bc294SPaolo Abeni 3925b19bc294SPaolo Abeni if (++work_done == budget) 3926b19bc294SPaolo Abeni return budget; 3927b19bc294SPaolo Abeni } 3928b19bc294SPaolo Abeni 3929b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3930b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3931b19bc294SPaolo Abeni */ 3932b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3933b19bc294SPaolo Abeni return work_done; 3934b19bc294SPaolo Abeni } 3935b19bc294SPaolo Abeni 3936d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3937f870fa0bSMat Martineau { 3938b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3939b19bc294SPaolo Abeni int cpu; 3940b19bc294SPaolo Abeni 39412303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 39422303f994SPeter Krystad 3943d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3944d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3945d027236cSPaolo Abeni 3946b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3947b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3948b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3949b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 395016d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 395116d083e2SJakub Kicinski mptcp_napi_poll); 3952b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3953b19bc294SPaolo Abeni } 3954b19bc294SPaolo Abeni 39552303f994SPeter Krystad mptcp_subflow_init(); 39561b1c7a0eSPeter Krystad mptcp_pm_init(); 39572c5ebd00SPaolo Abeni mptcp_token_init(); 39582303f994SPeter Krystad 3959f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3960f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3961f870fa0bSMat Martineau 3962f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 39636771bfd9SFlorian Westphal 39646771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3965f870fa0bSMat Martineau } 3966f870fa0bSMat Martineau 3967f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3968e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3969e42f1ac6SFlorian Westphal .family = PF_INET6, 3970e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39710a3cc579SPaolo Abeni .release = inet6_release, 3972e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3973786fc124SPaolo Abeni .connect = inet_stream_connect, 3974e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3975e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3976d2f77c53SPaolo Abeni .getname = inet6_getname, 3977e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3978e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3979e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3980e42f1ac6SFlorian Westphal .listen = mptcp_listen, 398176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3982e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3983e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3984e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3985e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3986e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3987e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3988e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39893986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3990e42f1ac6SFlorian Westphal #endif 3991e42f1ac6SFlorian Westphal }; 3992e42f1ac6SFlorian Westphal 3993f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3994f870fa0bSMat Martineau 3995f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3996f870fa0bSMat Martineau .type = SOCK_STREAM, 3997f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3998f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39992303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 4000f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 4001f870fa0bSMat Martineau }; 4002f870fa0bSMat Martineau 4003d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 4004f870fa0bSMat Martineau { 4005f870fa0bSMat Martineau int err; 4006f870fa0bSMat Martineau 4007f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 4008f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 4009f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 4010b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 4011f870fa0bSMat Martineau 4012f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 4013f870fa0bSMat Martineau if (err) 4014f870fa0bSMat Martineau return err; 4015f870fa0bSMat Martineau 4016f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 4017f870fa0bSMat Martineau if (err) 4018f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 4019f870fa0bSMat Martineau 4020f870fa0bSMat Martineau return err; 4021f870fa0bSMat Martineau } 4022f870fa0bSMat Martineau #endif 4023