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 938ab183deSPaolo Abeni msk->first = ssock->sk; 942303f994SPeter Krystad 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 6063721b9b6SMat Martineau 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 8283e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 829ec3edaa7SPeter Krystad { 8303e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8315cf92bbaSPaolo Abeni 8323e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 83378962489SFlorian Westphal return false; 834ec3edaa7SPeter Krystad 8353e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8363e501490SPaolo Abeni * at close time 8373e501490SPaolo Abeni */ 8383e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8393e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 84078962489SFlorian Westphal 8413e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 8423e501490SPaolo Abeni return true; 84378962489SFlorian Westphal } 84478962489SFlorian Westphal 8453e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk) 84678962489SFlorian Westphal { 8473e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8483e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 84978962489SFlorian Westphal 8503e501490SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 8513e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8523e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8533e501490SPaolo Abeni 8543e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8553e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8563e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8573e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 85878962489SFlorian Westphal } 859ec3edaa7SPeter Krystad } 860ec3edaa7SPeter Krystad 861b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 862b51f9b80SPaolo Abeni { 863b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 864b51f9b80SPaolo Abeni } 865b51f9b80SPaolo Abeni 866b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 867b51f9b80SPaolo Abeni { 868b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 869b51f9b80SPaolo Abeni unsigned long tout; 870b51f9b80SPaolo Abeni 871e16163b6SPaolo Abeni /* prevent rescheduling on close */ 872e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 873e16163b6SPaolo Abeni return; 874e16163b6SPaolo Abeni 87533d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 876b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 877b51f9b80SPaolo Abeni } 878b51f9b80SPaolo Abeni 879ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 880ba8f48f7SPaolo Abeni { 881ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 882ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 883ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 884ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 885ba8f48f7SPaolo Abeni */ 886ba8f48f7SPaolo Abeni sock_hold(sk); 887ba8f48f7SPaolo Abeni return true; 888ba8f48f7SPaolo Abeni } 889ba8f48f7SPaolo Abeni return false; 890ba8f48f7SPaolo Abeni } 891ba8f48f7SPaolo Abeni 89259832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 89359832e24SFlorian Westphal { 894ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 895ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 89659832e24SFlorian Westphal } 89759832e24SFlorian Westphal 8985969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8995969856aSPaolo Abeni { 9005969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 9015969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 9025969856aSPaolo Abeni int receivers = 0; 9035969856aSPaolo Abeni 9045969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 9055969856aSPaolo Abeni receivers += !subflow->rx_eof; 906e16163b6SPaolo Abeni if (receivers) 907e16163b6SPaolo Abeni return; 9085969856aSPaolo Abeni 909e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 9105969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 9115969856aSPaolo Abeni * to msk, when all subflows agree on it 9125969856aSPaolo Abeni */ 9135969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 9145969856aSPaolo Abeni 9155969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9165969856aSPaolo Abeni sk->sk_data_ready(sk); 9175969856aSPaolo Abeni } 918e16163b6SPaolo Abeni 919e16163b6SPaolo Abeni switch (sk->sk_state) { 920e16163b6SPaolo Abeni case TCP_ESTABLISHED: 921e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 922e16163b6SPaolo Abeni break; 923e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 92426aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 92526aa2314SPaolo Abeni break; 92626aa2314SPaolo Abeni case TCP_FIN_WAIT2: 927e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 928e16163b6SPaolo Abeni break; 929e16163b6SPaolo Abeni default: 930e16163b6SPaolo Abeni return; 931e16163b6SPaolo Abeni } 932e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9335969856aSPaolo Abeni } 9345969856aSPaolo Abeni 9357a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9367a6a6cbcSPaolo Abeni { 9377a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9387a6a6cbcSPaolo Abeni 939109cdeb8SGeliang Tang msk_owned_by_me(msk); 9407a6a6cbcSPaolo Abeni 9417a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 94299d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9437a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9447a6a6cbcSPaolo Abeni } 9457a6a6cbcSPaolo Abeni 9467a6a6cbcSPaolo Abeni return NULL; 9477a6a6cbcSPaolo Abeni } 9487a6a6cbcSPaolo Abeni 9493f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 95057040755SPaolo Abeni const struct sk_buff *skb, 95157040755SPaolo Abeni const struct mptcp_ext *mpext) 9526d0060f6SMat Martineau { 95357040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 95457040755SPaolo Abeni return false; 95557040755SPaolo Abeni 9565a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9575a369ca6SPaolo Abeni * mapping has not been xmitted yet 9585a369ca6SPaolo Abeni */ 9595a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9605a369ca6SPaolo Abeni !mpext->frozen; 96157040755SPaolo Abeni } 96257040755SPaolo Abeni 96329249eacSPaolo Abeni /* we can append data to the given data frag if: 96429249eacSPaolo Abeni * - there is space available in the backing page_frag 96529249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 96629249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 96729249eacSPaolo Abeni */ 96818b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 96918b683bfSPaolo Abeni const struct page_frag *pfrag, 97018b683bfSPaolo Abeni const struct mptcp_data_frag *df) 97118b683bfSPaolo Abeni { 97218b683bfSPaolo Abeni return df && pfrag->page == df->page && 973d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 97429249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 97518b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 97618b683bfSPaolo Abeni } 97718b683bfSPaolo Abeni 978d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 97918b683bfSPaolo Abeni { 980d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9817948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 982d027236cSPaolo Abeni } 983d027236cSPaolo Abeni 984d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 985d027236cSPaolo Abeni { 986d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 987d027236cSPaolo Abeni 98818b683bfSPaolo Abeni list_del(&dfrag->list); 989d027236cSPaolo Abeni dfrag_uncharge(sk, len); 99018b683bfSPaolo Abeni put_page(dfrag->page); 99118b683bfSPaolo Abeni } 99218b683bfSPaolo Abeni 9936e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 99418b683bfSPaolo Abeni { 99518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 99618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 997e1ff9e82SDavide Caratti u64 snd_una; 998e1ff9e82SDavide Caratti 999e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 1000e1ff9e82SDavide Caratti * plain TCP 1001e1ff9e82SDavide Caratti */ 1002e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10037439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10047439d687SPaolo Abeni 10057439d687SPaolo Abeni snd_una = msk->snd_una; 100618b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 100718b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 100818b683bfSPaolo Abeni break; 100918b683bfSPaolo Abeni 10101e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10111e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10121e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1013d9ca1de8SPaolo Abeni break; 10141e1d9d6fSPaolo Abeni 10151e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10161e1d9d6fSPaolo Abeni } 10171e1d9d6fSPaolo Abeni 1018d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1019d027236cSPaolo Abeni } 1020d027236cSPaolo Abeni 10217948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10227948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 102353eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 102453eb4c38SPaolo Abeni 10251e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10261e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10271e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 102853eb4c38SPaolo Abeni goto out; 10291e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10301e1d9d6fSPaolo Abeni goto out; 10311e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10321e1d9d6fSPaolo Abeni } 10337948f6ccSFlorian Westphal 10347948f6ccSFlorian Westphal dfrag->data_seq += delta; 103553eb4c38SPaolo Abeni dfrag->offset += delta; 10367948f6ccSFlorian Westphal dfrag->data_len -= delta; 1037d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10387948f6ccSFlorian Westphal 10397948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10407948f6ccSFlorian Westphal } 10417948f6ccSFlorian Westphal 10421e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10431e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10441e1d9d6fSPaolo Abeni msk->recovery = false; 10451e1d9d6fSPaolo Abeni 104653eb4c38SPaolo Abeni out: 10473241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10483241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 104933d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10506e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10516e628cd3SPaolo Abeni } else { 10526e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10536e628cd3SPaolo Abeni } 10546e628cd3SPaolo Abeni } 10556e628cd3SPaolo Abeni 1056417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1057417789dfSPaolo Abeni { 1058765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1059765ff425SPaolo Abeni 1060417789dfSPaolo Abeni __mptcp_clean_una(sk); 1061417789dfSPaolo Abeni mptcp_write_space(sk); 1062417789dfSPaolo Abeni } 1063417789dfSPaolo Abeni 1064b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1065b5941f06SPaolo Abeni { 1066b5941f06SPaolo Abeni mptcp_data_lock(sk); 1067b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1068b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1069b5941f06SPaolo Abeni } 1070b5941f06SPaolo Abeni 1071724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 107218b683bfSPaolo Abeni { 1073d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1074d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1075d9ca1de8SPaolo Abeni bool first = true; 1076d9ca1de8SPaolo Abeni 107718b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1078d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1079d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1080d9ca1de8SPaolo Abeni 1081d9ca1de8SPaolo Abeni if (first) 1082d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1083d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1084d9ca1de8SPaolo Abeni first = false; 1085d9ca1de8SPaolo Abeni } 1086724cfd2eSPaolo Abeni } 1087724cfd2eSPaolo Abeni 1088724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1089724cfd2eSPaolo Abeni * data 1090724cfd2eSPaolo Abeni */ 1091724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1092724cfd2eSPaolo Abeni { 1093724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1094724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1095724cfd2eSPaolo Abeni return true; 1096724cfd2eSPaolo Abeni 1097724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 109818b683bfSPaolo Abeni return false; 109918b683bfSPaolo Abeni } 110018b683bfSPaolo Abeni 110118b683bfSPaolo Abeni static struct mptcp_data_frag * 110218b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 110318b683bfSPaolo Abeni int orig_offset) 110418b683bfSPaolo Abeni { 110518b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 110618b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 110718b683bfSPaolo Abeni 110818b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 110918b683bfSPaolo Abeni dfrag->data_len = 0; 111018b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 111118b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 111218b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1113d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 111418b683bfSPaolo Abeni dfrag->page = pfrag->page; 111518b683bfSPaolo Abeni 111618b683bfSPaolo Abeni return dfrag; 111718b683bfSPaolo Abeni } 111818b683bfSPaolo Abeni 1119caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1120caf971dfSPaolo Abeni int mss_now; 1121caf971dfSPaolo Abeni int size_goal; 1122d9ca1de8SPaolo Abeni u16 limit; 1123d9ca1de8SPaolo Abeni u16 sent; 1124d9ca1de8SPaolo Abeni unsigned int flags; 11251094c6feSPaolo Abeni bool data_lock_held; 1126caf971dfSPaolo Abeni }; 1127caf971dfSPaolo Abeni 1128b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1129b713d006SPaolo Abeni u64 data_seq, int avail_size) 11306f8a612aSFlorian Westphal { 11316f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1132b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11336f8a612aSFlorian Westphal 11346f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11356f8a612aSFlorian Westphal return avail_size; 11366f8a612aSFlorian Westphal 1137b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1138b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11396f8a612aSFlorian Westphal 114092be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1141b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 114292be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 114392be2f52SPaolo Abeni } 11446f8a612aSFlorian Westphal 11456f8a612aSFlorian Westphal return avail_size; 11466f8a612aSFlorian Westphal } 11476f8a612aSFlorian Westphal 1148724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1149724cfd2eSPaolo Abeni { 1150724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1151724cfd2eSPaolo Abeni 1152724cfd2eSPaolo Abeni if (!mpext) 1153724cfd2eSPaolo Abeni return false; 1154724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1155724cfd2eSPaolo Abeni return true; 1156724cfd2eSPaolo Abeni } 1157724cfd2eSPaolo Abeni 11586e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1159724cfd2eSPaolo Abeni { 1160724cfd2eSPaolo Abeni struct sk_buff *skb; 1161724cfd2eSPaolo Abeni 11626e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1163724cfd2eSPaolo Abeni if (likely(skb)) { 11646e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1165724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1166a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1167f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1168724cfd2eSPaolo Abeni return skb; 1169724cfd2eSPaolo Abeni } 1170724cfd2eSPaolo Abeni __kfree_skb(skb); 1171724cfd2eSPaolo Abeni } else { 1172724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1173724cfd2eSPaolo Abeni } 1174724cfd2eSPaolo Abeni return NULL; 1175724cfd2eSPaolo Abeni } 1176724cfd2eSPaolo Abeni 1177f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1178724cfd2eSPaolo Abeni { 1179724cfd2eSPaolo Abeni struct sk_buff *skb; 1180724cfd2eSPaolo Abeni 11816e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1182724cfd2eSPaolo Abeni if (!skb) 1183f70cad10SPaolo Abeni return NULL; 1184724cfd2eSPaolo Abeni 1185724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1186f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1187f70cad10SPaolo Abeni return skb; 1188724cfd2eSPaolo Abeni } 11893ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1190724cfd2eSPaolo Abeni kfree_skb(skb); 1191f70cad10SPaolo Abeni return NULL; 1192724cfd2eSPaolo Abeni } 1193724cfd2eSPaolo Abeni 1194f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1195724cfd2eSPaolo Abeni { 11961094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1197724cfd2eSPaolo Abeni 11981094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1199724cfd2eSPaolo Abeni } 1200724cfd2eSPaolo Abeni 1201d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1202d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1203d0cc2987SGeliang Tang */ 1204d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1205d0cc2987SGeliang Tang { 1206d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1207d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1208d0cc2987SGeliang Tang int offset = skb->len - added; 1209d0cc2987SGeliang Tang 1210d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1211d0cc2987SGeliang Tang } 1212d0cc2987SGeliang Tang 12131e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12141e39e5a3SGeliang Tang struct sock *ssk, 12151e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12161e39e5a3SGeliang Tang { 12171e39e5a3SGeliang Tang if (!mpext) 12181e39e5a3SGeliang Tang return; 12191e39e5a3SGeliang Tang 12201e39e5a3SGeliang Tang mpext->infinite_map = 1; 12211e39e5a3SGeliang Tang mpext->data_len = 0; 12221e39e5a3SGeliang Tang 1223104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12241e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12251e39e5a3SGeliang Tang pr_fallback(msk); 1226d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12271e39e5a3SGeliang Tang } 12281e39e5a3SGeliang Tang 122957040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1230d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1231caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 123257040755SPaolo Abeni { 1233d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1234f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12356d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12366f8a612aSFlorian Westphal bool zero_window_probe = false; 12376d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1238f70cad10SPaolo Abeni bool can_coalesce = false; 1239f70cad10SPaolo Abeni bool reuse_skb = true; 1240f70cad10SPaolo Abeni struct sk_buff *skb; 1241f70cad10SPaolo Abeni size_t copy; 1242f70cad10SPaolo Abeni int i; 12436d0060f6SMat Martineau 1244e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1245d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12466d0060f6SMat Martineau 1247f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1248f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1249f70cad10SPaolo Abeni return 0; 1250f70cad10SPaolo Abeni 1251c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1252c886d702SPaolo Abeni return -EAGAIN; 1253c886d702SPaolo Abeni 1254d9ca1de8SPaolo Abeni /* compute send limit */ 1255d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1256f70cad10SPaolo Abeni copy = info->size_goal; 1257f70cad10SPaolo Abeni 125857040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1259f70cad10SPaolo Abeni if (skb && copy > skb->len) { 126057040755SPaolo Abeni /* Limit the write to the size available in the 126157040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 126257040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 126357040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 126457040755SPaolo Abeni * SSN association set here 126557040755SPaolo Abeni */ 1266d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12671094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126857040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12691094c6feSPaolo Abeni goto alloc_skb; 12701094c6feSPaolo Abeni } 12711094c6feSPaolo Abeni 1272f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1273f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1274657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1275f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1276f70cad10SPaolo Abeni goto alloc_skb; 127715e6ca97SPaolo Abeni } 127818b683bfSPaolo Abeni 1279f70cad10SPaolo Abeni copy -= skb->len; 1280f70cad10SPaolo Abeni } else { 12811094c6feSPaolo Abeni alloc_skb: 1282f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1283f70cad10SPaolo Abeni if (!skb) 1284f70cad10SPaolo Abeni return -ENOMEM; 1285f70cad10SPaolo Abeni 1286f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1287f70cad10SPaolo Abeni reuse_skb = false; 1288f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1289f70cad10SPaolo Abeni } 12901094c6feSPaolo Abeni 12916f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1292b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1293f70cad10SPaolo Abeni if (copy == 0) { 12947439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12957439d687SPaolo Abeni 1296f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 129727728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12986f8a612aSFlorian Westphal return 0; 12996f8a612aSFlorian Westphal } 13006f8a612aSFlorian Westphal 1301f70cad10SPaolo Abeni zero_window_probe = true; 1302f70cad10SPaolo Abeni data_seq = snd_una - 1; 1303f70cad10SPaolo Abeni copy = 1; 130457040755SPaolo Abeni 1305f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1306f70cad10SPaolo Abeni * ssk write queue 1307f70cad10SPaolo Abeni */ 1308f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1309f70cad10SPaolo Abeni } 1310f70cad10SPaolo Abeni 1311f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1312f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 131327728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1314e2223995SPaolo Abeni return -ENOMEM; 131535759383SFlorian Westphal } 131618b683bfSPaolo Abeni 1317f70cad10SPaolo Abeni if (can_coalesce) { 1318f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1319f70cad10SPaolo Abeni } else { 1320f70cad10SPaolo Abeni get_page(dfrag->page); 1321f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 132257040755SPaolo Abeni } 132357040755SPaolo Abeni 1324f70cad10SPaolo Abeni skb->len += copy; 1325f70cad10SPaolo Abeni skb->data_len += copy; 1326f70cad10SPaolo Abeni skb->truesize += copy; 1327f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1328f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1329f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1330f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1331f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1332f70cad10SPaolo Abeni 1333f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1334f70cad10SPaolo Abeni if (reuse_skb) { 1335f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1336f70cad10SPaolo Abeni mpext->data_len += copy; 1337f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1338f70cad10SPaolo Abeni goto out; 1339724cfd2eSPaolo Abeni } 13406d0060f6SMat Martineau 13416d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1342d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13436d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1344f70cad10SPaolo Abeni mpext->data_len = copy; 13456d0060f6SMat Martineau mpext->use_map = 1; 13466d0060f6SMat Martineau mpext->dsn64 = 1; 13476d0060f6SMat Martineau 13486d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13496d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13506d0060f6SMat Martineau mpext->dsn64); 13516d0060f6SMat Martineau 13526f8a612aSFlorian Westphal if (zero_window_probe) { 1353f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13546f8a612aSFlorian Westphal mpext->frozen = 1; 1355d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1356f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13576f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1358d0cc2987SGeliang Tang return 0; 13596f8a612aSFlorian Westphal } 136057040755SPaolo Abeni out: 1361d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1362f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13631e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13641e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13650eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1366f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1367f70cad10SPaolo Abeni return copy; 13686d0060f6SMat Martineau } 13696d0060f6SMat Martineau 1370d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1371d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1372d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1373d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1374d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1375d5f49190SPaolo Abeni 1376d5f49190SPaolo Abeni struct subflow_send_info { 1377d5f49190SPaolo Abeni struct sock *ssk; 13783ce0852cSPaolo Abeni u64 linger_time; 1379d5f49190SPaolo Abeni }; 1380d5f49190SPaolo Abeni 1381ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1382ff5a0b42SPaolo Abeni { 1383ff5a0b42SPaolo Abeni if (!subflow->stale) 1384ff5a0b42SPaolo Abeni return; 1385ff5a0b42SPaolo Abeni 1386ff5a0b42SPaolo Abeni subflow->stale = 0; 1387fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1388ff5a0b42SPaolo Abeni } 1389ff5a0b42SPaolo Abeni 1390ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1391ff5a0b42SPaolo Abeni { 1392ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1393ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1394ff5a0b42SPaolo Abeni 1395ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1396ff5a0b42SPaolo Abeni return false; 1397ff5a0b42SPaolo Abeni 1398ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1399ff5a0b42SPaolo Abeni } 1400ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1401ff5a0b42SPaolo Abeni } 1402ff5a0b42SPaolo Abeni 14033ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14043ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14053ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14063ce0852cSPaolo Abeni 140733d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140833d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140933d41c9cSPaolo Abeni * additionally updates the rtx timeout 141033d41c9cSPaolo Abeni */ 14115cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1412f296234cSPeter Krystad { 14133ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1414f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 141533d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14163ce0852cSPaolo Abeni u32 pace, burst, wmem; 1417d5f49190SPaolo Abeni int i, nr_active = 0; 1418d5f49190SPaolo Abeni struct sock *ssk; 14193ce0852cSPaolo Abeni u64 linger_time; 142033d41c9cSPaolo Abeni long tout = 0; 1421f296234cSPeter Krystad 1422109cdeb8SGeliang Tang msk_owned_by_me(msk); 1423f296234cSPeter Krystad 1424d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1425d5f49190SPaolo Abeni if (!msk->first) 1426f296234cSPeter Krystad return NULL; 1427c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1428c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1429f296234cSPeter Krystad } 1430f296234cSPeter Krystad 1431d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1432d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1433d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 143433d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 143533d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1436d5f49190SPaolo Abeni return msk->last_snd; 143733d41c9cSPaolo Abeni } 1438f296234cSPeter Krystad 1439d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14403ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1441d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14423ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1443d5f49190SPaolo Abeni } 14443ce0852cSPaolo Abeni 1445d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1446e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1447d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1448d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1449f296234cSPeter Krystad continue; 1450d5f49190SPaolo Abeni 145133d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1452d5f49190SPaolo Abeni nr_active += !subflow->backup; 14533ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14543ce0852cSPaolo Abeni if (unlikely(!pace)) { 14553ce0852cSPaolo Abeni /* init pacing rate from socket */ 14563ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14573ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1458d5f49190SPaolo Abeni if (!pace) 1459d5f49190SPaolo Abeni continue; 14603ce0852cSPaolo Abeni } 1461d5f49190SPaolo Abeni 14623ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14633ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1464d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14653ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1466d5f49190SPaolo Abeni } 1467f296234cSPeter Krystad } 146833d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1469f296234cSPeter Krystad 1470d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1471d5f49190SPaolo Abeni if (!nr_active) 14723ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1473d5f49190SPaolo Abeni 14743ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14753ce0852cSPaolo Abeni * faster flow, we need to: 14763ce0852cSPaolo Abeni * - estimate the faster flow linger time 14773ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14783ce0852cSPaolo Abeni * by the faster flow 14793ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14803ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14813ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14823ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14833ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14843ce0852cSPaolo Abeni */ 14853ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1486b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1487d5f49190SPaolo Abeni return NULL; 14883ce0852cSPaolo Abeni 1489b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14903ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1491b713d006SPaolo Abeni if (!burst) { 1492b713d006SPaolo Abeni msk->last_snd = NULL; 1493b713d006SPaolo Abeni return ssk; 1494b713d006SPaolo Abeni } 1495b713d006SPaolo Abeni 14963ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14973ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14983ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14993ce0852cSPaolo Abeni burst + wmem); 15003ce0852cSPaolo Abeni msk->last_snd = ssk; 15013ce0852cSPaolo Abeni msk->snd_burst = burst; 15023ce0852cSPaolo Abeni return ssk; 1503f296234cSPeter Krystad } 1504f296234cSPeter Krystad 1505b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1506d9ca1de8SPaolo Abeni { 1507d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1508d9ca1de8SPaolo Abeni release_sock(ssk); 1509d9ca1de8SPaolo Abeni } 1510d9ca1de8SPaolo Abeni 15110d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15120d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15130d199e43SFlorian Westphal u32 sent) 15140d199e43SFlorian Westphal { 15150d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15160d199e43SFlorian Westphal 15170d199e43SFlorian Westphal dfrag->already_sent += sent; 15180d199e43SFlorian Westphal 15190d199e43SFlorian Westphal msk->snd_burst -= sent; 15200d199e43SFlorian Westphal 15210d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15220d199e43SFlorian Westphal 15230d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15240d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15250d199e43SFlorian Westphal * old segments. 15260d199e43SFlorian Westphal * 15270d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15280d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15290d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15300d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15310d199e43SFlorian Westphal */ 15320d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15330d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15340d199e43SFlorian Westphal } 15350d199e43SFlorian Westphal 15368b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15373241a9c0SFlorian Westphal { 1538e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1539e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15403241a9c0SFlorian Westphal } 15413241a9c0SFlorian Westphal 1542ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1543d9ca1de8SPaolo Abeni { 1544d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1545d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1546d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1547d9ca1de8SPaolo Abeni .flags = flags, 1548d9ca1de8SPaolo Abeni }; 15490522b424SGeliang Tang bool do_check_data_fin = false; 1550d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15510522b424SGeliang Tang int len; 1552d9ca1de8SPaolo Abeni 1553d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1554d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1555d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1556d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1557d9ca1de8SPaolo Abeni while (len > 0) { 1558d9ca1de8SPaolo Abeni int ret = 0; 1559d9ca1de8SPaolo Abeni 1560d9ca1de8SPaolo Abeni prev_ssk = ssk; 15615cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1562d9ca1de8SPaolo Abeni 15639758f40eSPaolo Abeni /* First check. If the ssk has changed since 15649758f40eSPaolo Abeni * the last round, release prev_ssk 1565d9ca1de8SPaolo Abeni */ 1566d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1567b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1568d9ca1de8SPaolo Abeni if (!ssk) 1569d9ca1de8SPaolo Abeni goto out; 1570d9ca1de8SPaolo Abeni 15719758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15729758f40eSPaolo Abeni * from the previous one, otherwise we are still 15739758f40eSPaolo Abeni * helding the relevant lock 15749758f40eSPaolo Abeni */ 15759758f40eSPaolo Abeni if (ssk != prev_ssk) 1576d9ca1de8SPaolo Abeni lock_sock(ssk); 1577d9ca1de8SPaolo Abeni 1578d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1579d9ca1de8SPaolo Abeni if (ret <= 0) { 1580c886d702SPaolo Abeni if (ret == -EAGAIN) 1581c886d702SPaolo Abeni continue; 1582b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1583d9ca1de8SPaolo Abeni goto out; 1584d9ca1de8SPaolo Abeni } 1585d9ca1de8SPaolo Abeni 15860522b424SGeliang Tang do_check_data_fin = true; 1587d9ca1de8SPaolo Abeni info.sent += ret; 1588d9ca1de8SPaolo Abeni len -= ret; 15890d199e43SFlorian Westphal 15900d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1591d9ca1de8SPaolo Abeni } 1592d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1593d9ca1de8SPaolo Abeni } 1594d9ca1de8SPaolo Abeni 1595d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1596d9ca1de8SPaolo Abeni if (ssk) 1597b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1598d9ca1de8SPaolo Abeni 1599d9ca1de8SPaolo Abeni out: 160033d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1601d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1602d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 16030522b424SGeliang Tang if (do_check_data_fin) 1604d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1605d9ca1de8SPaolo Abeni } 1606d9ca1de8SPaolo Abeni 160773a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 16086e628cd3SPaolo Abeni { 16096e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16101094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16111094c6feSPaolo Abeni .data_lock_held = true, 16121094c6feSPaolo Abeni }; 16136e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1614b19bc294SPaolo Abeni struct sock *xmit_ssk; 16156e628cd3SPaolo Abeni int len, copied = 0; 16166e628cd3SPaolo Abeni 16176e628cd3SPaolo Abeni info.flags = 0; 16186e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16196e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16206e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16216e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16226e628cd3SPaolo Abeni while (len > 0) { 16236e628cd3SPaolo Abeni int ret = 0; 16246e628cd3SPaolo Abeni 162573a0052aSGeliang Tang /* check for a different subflow usage only after 1626b19bc294SPaolo Abeni * spooling the first chunk of data 1627b19bc294SPaolo Abeni */ 162800df24f1SGeliang Tang xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1629b19bc294SPaolo Abeni if (!xmit_ssk) 1630b19bc294SPaolo Abeni goto out; 1631b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1632bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1633bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1634b19bc294SPaolo Abeni goto out; 1635b19bc294SPaolo Abeni } 1636b19bc294SPaolo Abeni 16376e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16386e628cd3SPaolo Abeni if (ret <= 0) 16396e628cd3SPaolo Abeni goto out; 16406e628cd3SPaolo Abeni 16416e628cd3SPaolo Abeni info.sent += ret; 16426e628cd3SPaolo Abeni copied += ret; 16436e628cd3SPaolo Abeni len -= ret; 1644b19bc294SPaolo Abeni first = false; 16450d199e43SFlorian Westphal 16460d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16476e628cd3SPaolo Abeni } 16486e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16496e628cd3SPaolo Abeni } 16506e628cd3SPaolo Abeni 16516e628cd3SPaolo Abeni out: 16526e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16536e628cd3SPaolo Abeni * not going to flush it via release_sock() 16546e628cd3SPaolo Abeni */ 16556e628cd3SPaolo Abeni if (copied) { 16566e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16576e628cd3SPaolo Abeni info.size_goal); 1658d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1659d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1660d09d818eSPaolo Abeni 16616e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16626e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16636e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16646e628cd3SPaolo Abeni } 16656e628cd3SPaolo Abeni } 16666e628cd3SPaolo Abeni 16675cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16685cf92bbaSPaolo Abeni { 16695cf92bbaSPaolo Abeni /* enable autotune */ 16705cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16715cf92bbaSPaolo Abeni 16725cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16735cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16745cf92bbaSPaolo Abeni } 16755cf92bbaSPaolo Abeni 16767d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 16777d803344SPaolo Abeni 1678a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1679fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1680fa9e5746SPaolo Abeni { 1681fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1682fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1683ddb1a072SPaolo Abeni struct socket *ssock; 1684a2702a07SPaolo Abeni struct sock *ssk; 1685fa9e5746SPaolo Abeni int ret; 1686fa9e5746SPaolo Abeni 1687a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1688a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1689a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1690a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1691a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1692a2702a07SPaolo Abeni */ 1693ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 1694ddb1a072SPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 1695ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 1696ddb1a072SPaolo Abeni return PTR_ERR(ssock); 1697ddb1a072SPaolo Abeni } 1698a2702a07SPaolo Abeni if (!msk->first) 1699a2702a07SPaolo Abeni return -EINVAL; 1700a2702a07SPaolo Abeni 1701a2702a07SPaolo Abeni ssk = msk->first; 1702a2702a07SPaolo Abeni 1703fa9e5746SPaolo Abeni lock_sock(ssk); 1704fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 17057d803344SPaolo Abeni msk->fastopening = 1; 1706fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 17077d803344SPaolo Abeni msk->fastopening = 0; 1708fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1709fa9e5746SPaolo Abeni release_sock(ssk); 1710fa9e5746SPaolo Abeni 1711fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1712fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1713fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1714fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1715fa9e5746SPaolo Abeni 1716fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1717fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1718fa9e5746SPaolo Abeni */ 1719fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1720fa9e5746SPaolo Abeni *copied_syn = 0; 17217d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 17227d803344SPaolo Abeni mptcp_disconnect(sk, 0); 1723fa9e5746SPaolo Abeni } 1724a2702a07SPaolo Abeni inet_sk(sk)->defer_connect = 0; 1725fa9e5746SPaolo Abeni 1726fa9e5746SPaolo Abeni return ret; 1727fa9e5746SPaolo Abeni } 1728fa9e5746SPaolo Abeni 1729f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1730f870fa0bSMat Martineau { 1731f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 173217091708SFlorian Westphal struct page_frag *pfrag; 17336d0060f6SMat Martineau size_t copied = 0; 1734caf971dfSPaolo Abeni int ret = 0; 17356d0060f6SMat Martineau long timeo; 1736f870fa0bSMat Martineau 1737987858e5SPaolo Abeni /* silently ignore everything else */ 17381e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1739987858e5SPaolo Abeni 17406511882cSPaolo Abeni lock_sock(sk); 17411954b860SMat Martineau 1742a2702a07SPaolo Abeni if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) { 1743d98a82a6SDmytro Shytyi int copied_syn = 0; 1744d98a82a6SDmytro Shytyi 1745a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1746d98a82a6SDmytro Shytyi copied += copied_syn; 1747fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1748d98a82a6SDmytro Shytyi goto out; 1749fa9e5746SPaolo Abeni else if (ret) 175069800e51SPaolo Abeni goto do_error; 1751d98a82a6SDmytro Shytyi } 1752d98a82a6SDmytro Shytyi 17531954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17541954b860SMat Martineau 17551954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17561954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17571954b860SMat Martineau if (ret) 175869800e51SPaolo Abeni goto do_error; 17591954b860SMat Martineau } 17601954b860SMat Martineau 176169800e51SPaolo Abeni ret = -EPIPE; 176269800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 176369800e51SPaolo Abeni goto do_error; 176469800e51SPaolo Abeni 176517091708SFlorian Westphal pfrag = sk_page_frag(sk); 176618b683bfSPaolo Abeni 1767d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1768724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1769d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1770d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1771d9ca1de8SPaolo Abeni size_t psize, offset; 1772d9ca1de8SPaolo Abeni 1773d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1774d9ca1de8SPaolo Abeni * page allocator 1775fb529e62SFlorian Westphal */ 1776d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1777d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1778d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1779d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1780d9ca1de8SPaolo Abeni goto wait_for_memory; 17816e628cd3SPaolo Abeni 1782d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1783d9ca1de8SPaolo Abeni goto wait_for_memory; 1784d9ca1de8SPaolo Abeni 1785d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1786d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1787fb529e62SFlorian Westphal } 1788fb529e62SFlorian Westphal 1789d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1790d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1791d9ca1de8SPaolo Abeni * anyway 1792d9ca1de8SPaolo Abeni */ 1793d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1794d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1795d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1796724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17978ce568edSPaolo Abeni 17986511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1799d9ca1de8SPaolo Abeni goto wait_for_memory; 1800d9ca1de8SPaolo Abeni 1801d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1802d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1803d9ca1de8SPaolo Abeni ret = -EFAULT; 180469800e51SPaolo Abeni goto do_error; 1805d9ca1de8SPaolo Abeni } 1806d9ca1de8SPaolo Abeni 1807d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 18086511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1809d9ca1de8SPaolo Abeni copied += psize; 1810d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1811d9ca1de8SPaolo Abeni frag_truesize += psize; 1812d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1813d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1814d9ca1de8SPaolo Abeni 1815d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1816d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1817d9ca1de8SPaolo Abeni */ 1818d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1819d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1820d9ca1de8SPaolo Abeni get_page(dfrag->page); 1821d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1822d9ca1de8SPaolo Abeni if (!msk->first_pending) 1823d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1824d9ca1de8SPaolo Abeni } 1825e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1826d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1827d9ca1de8SPaolo Abeni !dfrag_collapsed); 1828d9ca1de8SPaolo Abeni 1829d9ca1de8SPaolo Abeni continue; 1830d9ca1de8SPaolo Abeni 1831d9ca1de8SPaolo Abeni wait_for_memory: 18325cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1833c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1834f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1835f296234cSPeter Krystad if (ret) 183669800e51SPaolo Abeni goto do_error; 1837cec37a6eSPeter Krystad } 1838cec37a6eSPeter Krystad 183913e16037SPaolo Abeni if (copied) 1840c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1841da51aef5SPaolo Abeni 18421954b860SMat Martineau out: 1843cec37a6eSPeter Krystad release_sock(sk); 184469800e51SPaolo Abeni return copied; 184569800e51SPaolo Abeni 184669800e51SPaolo Abeni do_error: 184769800e51SPaolo Abeni if (copied) 184869800e51SPaolo Abeni goto out; 184969800e51SPaolo Abeni 185069800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 185169800e51SPaolo Abeni goto out; 1852f870fa0bSMat Martineau } 1853f870fa0bSMat Martineau 18546771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18556771bfd9SFlorian Westphal struct msghdr *msg, 1856b7f653b2SFlorian Westphal size_t len, int flags, 1857b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1858b7f653b2SFlorian Westphal int *cmsg_flags) 18596771bfd9SFlorian Westphal { 1860ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18616771bfd9SFlorian Westphal int copied = 0; 18626771bfd9SFlorian Westphal 1863ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18646771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18656771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18666771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18676771bfd9SFlorian Westphal int err; 18686771bfd9SFlorian Westphal 1869d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18706771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18716771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18726771bfd9SFlorian Westphal if (!copied) 18736771bfd9SFlorian Westphal return err; 18746771bfd9SFlorian Westphal break; 18756771bfd9SFlorian Westphal } 1876d976092cSPaolo Abeni } 18776771bfd9SFlorian Westphal 1878b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1879b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1880b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1881b7f653b2SFlorian Westphal } 1882b7f653b2SFlorian Westphal 18836771bfd9SFlorian Westphal copied += count; 18846771bfd9SFlorian Westphal 18856771bfd9SFlorian Westphal if (count < data_len) { 18862c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18876771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18882c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18892c9e7765SFlorian Westphal } 18906771bfd9SFlorian Westphal break; 18916771bfd9SFlorian Westphal } 18926771bfd9SFlorian Westphal 1893ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 189487952603SPaolo Abeni /* we will bulk release the skb memory later */ 189587952603SPaolo Abeni skb->destructor = NULL; 1896ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 189787952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18986771bfd9SFlorian Westphal __kfree_skb(skb); 1899ca4fb892SYonglong Li } 19006771bfd9SFlorian Westphal 19016771bfd9SFlorian Westphal if (copied >= len) 19026771bfd9SFlorian Westphal break; 19036771bfd9SFlorian Westphal } 19046771bfd9SFlorian Westphal 19056771bfd9SFlorian Westphal return copied; 19066771bfd9SFlorian Westphal } 19076771bfd9SFlorian Westphal 1908a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1909a6b118feSFlorian Westphal * 1910a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1911a6b118feSFlorian Westphal */ 1912a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1913a6b118feSFlorian Westphal { 1914a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1915a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1916a6b118feSFlorian Westphal u32 time, advmss = 1; 1917a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1918a6b118feSFlorian Westphal 1919109cdeb8SGeliang Tang msk_owned_by_me(msk); 1920a6b118feSFlorian Westphal 1921a6b118feSFlorian Westphal if (copied <= 0) 1922a6b118feSFlorian Westphal return; 1923a6b118feSFlorian Westphal 1924a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1925a6b118feSFlorian Westphal 1926a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1927a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1928a6b118feSFlorian Westphal 1929a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1930a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1931a6b118feSFlorian Westphal return; 1932a6b118feSFlorian Westphal 1933a6b118feSFlorian Westphal rtt_us = 0; 1934a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1935a6b118feSFlorian Westphal const struct tcp_sock *tp; 1936a6b118feSFlorian Westphal u64 sf_rtt_us; 1937a6b118feSFlorian Westphal u32 sf_advmss; 1938a6b118feSFlorian Westphal 1939a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1940a6b118feSFlorian Westphal 1941a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1942a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1943a6b118feSFlorian Westphal 1944a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1945a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1946a6b118feSFlorian Westphal } 1947a6b118feSFlorian Westphal 1948a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1949a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1950a6b118feSFlorian Westphal return; 1951a6b118feSFlorian Westphal 1952a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1953a6b118feSFlorian Westphal goto new_measure; 1954a6b118feSFlorian Westphal 195578047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1956a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1957a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1958a6b118feSFlorian Westphal u64 rcvwin, grow; 1959a6b118feSFlorian Westphal 1960a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1961a6b118feSFlorian Westphal 1962a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1963a6b118feSFlorian Westphal 1964a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1965a6b118feSFlorian Westphal rcvwin += (grow << 1); 1966a6b118feSFlorian Westphal 1967a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1968a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1969a6b118feSFlorian Westphal rcvmem += 128; 1970a6b118feSFlorian Westphal 1971a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1972a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 197302739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1974a6b118feSFlorian Westphal 1975a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1976a6b118feSFlorian Westphal u32 window_clamp; 1977a6b118feSFlorian Westphal 1978a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1979a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1980a6b118feSFlorian Westphal 1981a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1982a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1983a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1984a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1985a6b118feSFlorian Westphal */ 1986a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1987a6b118feSFlorian Westphal struct sock *ssk; 1988c76c6956SPaolo Abeni bool slow; 1989a6b118feSFlorian Westphal 1990a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1991c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1992a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1993a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1994c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1995c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1996a6b118feSFlorian Westphal } 1997a6b118feSFlorian Westphal } 1998a6b118feSFlorian Westphal } 1999a6b118feSFlorian Westphal 2000a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 2001a6b118feSFlorian Westphal new_measure: 2002a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2003a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 2004a6b118feSFlorian Westphal } 2005a6b118feSFlorian Westphal 200687952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 200787952603SPaolo Abeni { 200887952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 200987952603SPaolo Abeni 201087952603SPaolo Abeni if (!msk->rmem_released) 201187952603SPaolo Abeni return; 201287952603SPaolo Abeni 201387952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 20146511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 2015ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 201687952603SPaolo Abeni } 201787952603SPaolo Abeni 201887952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 201987952603SPaolo Abeni { 202087952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 202187952603SPaolo Abeni 202287952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 202387952603SPaolo Abeni } 202487952603SPaolo Abeni 2025e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20266771bfd9SFlorian Westphal { 202787952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20286771bfd9SFlorian Westphal unsigned int moved = 0; 202987952603SPaolo Abeni bool ret, done; 2030d5f49190SPaolo Abeni 20316771bfd9SFlorian Westphal do { 20326771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 203365f49fe7SFlorian Westphal bool slowpath; 20346771bfd9SFlorian Westphal 203587952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 203687952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 203787952603SPaolo Abeni * that is an unlikely slow path. 203887952603SPaolo Abeni */ 203987952603SPaolo Abeni if (likely(!ssk)) 20406771bfd9SFlorian Westphal break; 20416771bfd9SFlorian Westphal 204265f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 204387952603SPaolo Abeni mptcp_data_lock(sk); 2044e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20456771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 204687952603SPaolo Abeni mptcp_data_unlock(sk); 2047499ada50SPaolo Abeni 2048499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2049499ada50SPaolo Abeni __mptcp_error_report(sk); 205065f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20516771bfd9SFlorian Westphal } while (!done); 20526771bfd9SFlorian Westphal 205387952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 205487952603SPaolo Abeni ret = moved > 0; 205587952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 205687952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 205787952603SPaolo Abeni mptcp_data_lock(sk); 205887952603SPaolo Abeni __mptcp_update_rmem(sk); 205987952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 206087952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 206187952603SPaolo Abeni mptcp_data_unlock(sk); 2062ab174ad8SPaolo Abeni } 206387952603SPaolo Abeni if (ret) 206487952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 206587952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20666771bfd9SFlorian Westphal } 20676771bfd9SFlorian Westphal 20682c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20692c9e7765SFlorian Westphal { 20702c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20712c9e7765SFlorian Westphal const struct sk_buff *skb; 20722c9e7765SFlorian Westphal 20732c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20742c9e7765SFlorian Westphal if (skb) { 20752c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20762c9e7765SFlorian Westphal 20772c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20782c9e7765SFlorian Westphal return INT_MAX; 20792c9e7765SFlorian Westphal 20802c9e7765SFlorian Westphal return (unsigned int)hint_val; 20812c9e7765SFlorian Westphal } 20822c9e7765SFlorian Westphal 20832c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20842c9e7765SFlorian Westphal return 1; 20852c9e7765SFlorian Westphal 20862c9e7765SFlorian Westphal return 0; 20872c9e7765SFlorian Westphal } 20882c9e7765SFlorian Westphal 2089f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2090ec095263SOliver Hartkopp int flags, int *addr_len) 2091f870fa0bSMat Martineau { 2092f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2093b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2094b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20957a6a6cbcSPaolo Abeni int target; 20967a6a6cbcSPaolo Abeni long timeo; 2097f870fa0bSMat Martineau 2098cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2099cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2100cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2101cb9d80f4SPaolo Abeni 21026511882cSPaolo Abeni lock_sock(sk); 2103fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2104fd897679SPaolo Abeni copied = -ENOTCONN; 2105fd897679SPaolo Abeni goto out_err; 2106fd897679SPaolo Abeni } 2107fd897679SPaolo Abeni 2108ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 21097a6a6cbcSPaolo Abeni 21107a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 21117a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 21127a6a6cbcSPaolo Abeni 21132c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 21142c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 21152c9e7765SFlorian Westphal 211605e3eceaSEric Dumazet while (copied < len) { 2117e3859603SPaolo Abeni int bytes_read; 21187a6a6cbcSPaolo Abeni 2119b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21206771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21217a6a6cbcSPaolo Abeni if (!copied) 21227a6a6cbcSPaolo Abeni copied = bytes_read; 21236771bfd9SFlorian Westphal goto out_err; 2124cec37a6eSPeter Krystad } 2125cec37a6eSPeter Krystad 21267a6a6cbcSPaolo Abeni copied += bytes_read; 21277a6a6cbcSPaolo Abeni 2128ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2129fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2130ea4ca586SPaolo Abeni 2131e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2132e3859603SPaolo Abeni continue; 2133e3859603SPaolo Abeni 21347a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21357a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21367a6a6cbcSPaolo Abeni */ 21377a6a6cbcSPaolo Abeni if (copied >= target) 21387a6a6cbcSPaolo Abeni break; 21397a6a6cbcSPaolo Abeni 21407a6a6cbcSPaolo Abeni if (copied) { 21417a6a6cbcSPaolo Abeni if (sk->sk_err || 21427a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21437a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21447a6a6cbcSPaolo Abeni !timeo || 21457a6a6cbcSPaolo Abeni signal_pending(current)) 21467a6a6cbcSPaolo Abeni break; 21477a6a6cbcSPaolo Abeni } else { 21487a6a6cbcSPaolo Abeni if (sk->sk_err) { 21497a6a6cbcSPaolo Abeni copied = sock_error(sk); 21507a6a6cbcSPaolo Abeni break; 21517a6a6cbcSPaolo Abeni } 21527a6a6cbcSPaolo Abeni 21535969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 21545969856aSPaolo Abeni mptcp_check_for_eof(msk); 21555969856aSPaolo Abeni 215687952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 215787952603SPaolo Abeni /* race breaker: the shutdown could be after the 215887952603SPaolo Abeni * previous receive queue check 215987952603SPaolo Abeni */ 2160e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 216187952603SPaolo Abeni continue; 21627a6a6cbcSPaolo Abeni break; 216387952603SPaolo Abeni } 21647a6a6cbcSPaolo Abeni 21657a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21667a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21677a6a6cbcSPaolo Abeni break; 21687a6a6cbcSPaolo Abeni } 21697a6a6cbcSPaolo Abeni 21707a6a6cbcSPaolo Abeni if (!timeo) { 21717a6a6cbcSPaolo Abeni copied = -EAGAIN; 21727a6a6cbcSPaolo Abeni break; 21737a6a6cbcSPaolo Abeni } 21747a6a6cbcSPaolo Abeni 21757a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21767a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21777a6a6cbcSPaolo Abeni break; 21787a6a6cbcSPaolo Abeni } 21797a6a6cbcSPaolo Abeni } 21807a6a6cbcSPaolo Abeni 21817a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2182612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21837a6a6cbcSPaolo Abeni } 21843c90e377SPaolo Abeni 21856771bfd9SFlorian Westphal out_err: 2186b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2187b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2188b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21892c9e7765SFlorian Westphal 21902c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21912c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21922c9e7765SFlorian Westphal 21932c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21942c9e7765SFlorian Westphal } 2195b7f653b2SFlorian Westphal } 2196b7f653b2SFlorian Westphal 2197612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2198612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2199612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2200ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2201a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2202a6b118feSFlorian Westphal 2203cec37a6eSPeter Krystad release_sock(sk); 2204cec37a6eSPeter Krystad return copied; 2205cec37a6eSPeter Krystad } 2206cec37a6eSPeter Krystad 2207b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2208b51f9b80SPaolo Abeni { 2209b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2210b51f9b80SPaolo Abeni icsk_retransmit_timer); 2211b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 22122d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2213b51f9b80SPaolo Abeni 2214b51f9b80SPaolo Abeni bh_lock_sock(sk); 2215b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22162d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22172d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22182d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2219b51f9b80SPaolo Abeni } else { 2220b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2221e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2222b51f9b80SPaolo Abeni } 2223b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2224b51f9b80SPaolo Abeni sock_put(sk); 2225b51f9b80SPaolo Abeni } 2226b51f9b80SPaolo Abeni 2227e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2228e16163b6SPaolo Abeni { 2229e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2230e16163b6SPaolo Abeni 2231e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2232b6d69fc8SFlorian Westphal sock_put(sk); 2233e16163b6SPaolo Abeni } 2234e16163b6SPaolo Abeni 22353b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22363b1d6210SPaolo Abeni * level. 22373b1d6210SPaolo Abeni * 22383b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22393b1d6210SPaolo Abeni */ 2240ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22413b1d6210SPaolo Abeni { 224271b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22433b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 224471b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22453b1d6210SPaolo Abeni 2246109cdeb8SGeliang Tang msk_owned_by_me(msk); 22473b1d6210SPaolo Abeni 2248d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2249d9ca1de8SPaolo Abeni return NULL; 2250d5f49190SPaolo Abeni 22513b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22523b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22533b1d6210SPaolo Abeni 2254ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2255d5f49190SPaolo Abeni continue; 2256d5f49190SPaolo Abeni 225771b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 225871b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 225971b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 226071b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2261860975c6SFlorian Westphal continue; 2262860975c6SFlorian Westphal } 22633b1d6210SPaolo Abeni 22643b1d6210SPaolo Abeni if (subflow->backup) { 22653b1d6210SPaolo Abeni if (!backup) 22663b1d6210SPaolo Abeni backup = ssk; 22673b1d6210SPaolo Abeni continue; 22683b1d6210SPaolo Abeni } 22693b1d6210SPaolo Abeni 227071b7dec2SPaolo Abeni if (!pick) 227171b7dec2SPaolo Abeni pick = ssk; 22723b1d6210SPaolo Abeni } 22733b1d6210SPaolo Abeni 227471b7dec2SPaolo Abeni if (pick) 227571b7dec2SPaolo Abeni return pick; 227671b7dec2SPaolo Abeni 227771b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 227871b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22793b1d6210SPaolo Abeni } 22803b1d6210SPaolo Abeni 228117aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 228217aee05dSFlorian Westphal { 228317aee05dSFlorian Westphal if (msk->subflow) { 228417aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 228517aee05dSFlorian Westphal msk->subflow = NULL; 228617aee05dSFlorian Westphal } 228717aee05dSFlorian Westphal } 228817aee05dSFlorian Westphal 22891e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22901e1d9d6fSPaolo Abeni { 22911e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22921e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22931e1d9d6fSPaolo Abeni 229400df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 22951e1d9d6fSPaolo Abeni return false; 22961e1d9d6fSPaolo Abeni 22971e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22981e1d9d6fSPaolo Abeni return false; 22991e1d9d6fSPaolo Abeni 23001e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 23011e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 23021e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 23031e1d9d6fSPaolo Abeni */ 23041e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 23051e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 23061e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 23071e1d9d6fSPaolo Abeni if (!rtx_head) { 23081e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23091e1d9d6fSPaolo Abeni return false; 23101e1d9d6fSPaolo Abeni } 23111e1d9d6fSPaolo Abeni 23121e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 23131e1d9d6fSPaolo Abeni msk->recovery = true; 23141e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23151e1d9d6fSPaolo Abeni 23161e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23171e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23181e1d9d6fSPaolo Abeni 23191e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23201e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23211e1d9d6fSPaolo Abeni if (!cur->already_sent) 23221e1d9d6fSPaolo Abeni break; 23231e1d9d6fSPaolo Abeni cur->already_sent = 0; 23241e1d9d6fSPaolo Abeni } 23251e1d9d6fSPaolo Abeni 23261e1d9d6fSPaolo Abeni return true; 23271e1d9d6fSPaolo Abeni } 23281e1d9d6fSPaolo Abeni 2329b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2330b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2331b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2332b29fcfb5SPaolo Abeni 2333cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2334cec37a6eSPeter Krystad * (accept). 2335cec37a6eSPeter Krystad * 2336cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2337cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2338cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2339cec37a6eSPeter Krystad * parent socket. 2340cec37a6eSPeter Krystad */ 2341a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2342b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2343b29fcfb5SPaolo Abeni unsigned int flags) 2344cec37a6eSPeter Krystad { 2345e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 234663740448SPaolo Abeni bool dispose_it, need_push = false; 234763740448SPaolo Abeni 234863740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 234963740448SPaolo Abeni * to an incoming reset, mptcp either: 235063740448SPaolo Abeni * - if either the subflow or the msk are dead, destroy the context 235163740448SPaolo Abeni * (the subflow socket is deleted by inet_child_forget) and the msk 235263740448SPaolo Abeni * - otherwise do nothing at the moment and take action at accept and/or 235363740448SPaolo Abeni * listener shutdown - user-space must be able to accept() the closed 235463740448SPaolo Abeni * socket. 235563740448SPaolo Abeni */ 235663740448SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk) { 235763740448SPaolo Abeni if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 235863740448SPaolo Abeni return; 235963740448SPaolo Abeni 236063740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 236163740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 236263740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 236363740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 236463740448SPaolo Abeni goto out_release; 236563740448SPaolo Abeni } 2366e0be4931SFlorian Westphal 2367b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2368b29fcfb5SPaolo Abeni if (dispose_it) 2369cec37a6eSPeter Krystad list_del(&subflow->node); 2370cec37a6eSPeter Krystad 23713f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2372e16163b6SPaolo Abeni 2373d21f8348SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) { 2374d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2375d21f8348SPaolo Abeni * to generate the egress reset 2376d21f8348SPaolo Abeni */ 2377d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2378d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2379b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2380d21f8348SPaolo Abeni } 2381b29fcfb5SPaolo Abeni 2382b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2383b29fcfb5SPaolo Abeni if (!dispose_it) { 2384b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2385b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2386b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2387b29fcfb5SPaolo Abeni release_sock(ssk); 2388b29fcfb5SPaolo Abeni 2389b29fcfb5SPaolo Abeni goto out; 2390b29fcfb5SPaolo Abeni } 2391b29fcfb5SPaolo Abeni 2392d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2393d7b1bfd0SPaolo Abeni 2394e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2395e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2396e16163b6SPaolo Abeni * reference owned by msk; 2397e16163b6SPaolo Abeni */ 2398e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2399b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2400e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2401e16163b6SPaolo Abeni } else { 2402d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 24032a6a870eSPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 24042a6a870eSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 24052a6a870eSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 24062a6a870eSPaolo Abeni inet_csk_listen_stop(ssk); 2407f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 24082a6a870eSPaolo Abeni } 24090a3f4f1fSPaolo Abeni 2410e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2411e16163b6SPaolo Abeni 2412e16163b6SPaolo Abeni /* close acquired an extra ref */ 2413e16163b6SPaolo Abeni __sock_put(ssk); 2414e16163b6SPaolo Abeni } 241563740448SPaolo Abeni 241663740448SPaolo Abeni out_release: 2417e16163b6SPaolo Abeni release_sock(ssk); 2418e16163b6SPaolo Abeni 2419e16163b6SPaolo Abeni sock_put(ssk); 2420e0be4931SFlorian Westphal 2421c8fe62f0SFlorian Westphal if (ssk == msk->first) 2422c8fe62f0SFlorian Westphal msk->first = NULL; 2423c8fe62f0SFlorian Westphal 2424b29fcfb5SPaolo Abeni out: 2425b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2426b29fcfb5SPaolo Abeni msk->last_snd = NULL; 24271e1d9d6fSPaolo Abeni 24281e1d9d6fSPaolo Abeni if (need_push) 24291e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2430f870fa0bSMat Martineau } 2431f870fa0bSMat Martineau 2432a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2433a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2434a141e02eSFlorian Westphal { 2435b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2436b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2437a88c9e49SPaolo Abeni 2438a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2439a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2440a88c9e49SPaolo Abeni */ 2441a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2442a88c9e49SPaolo Abeni 2443b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2444a141e02eSFlorian Westphal } 2445a141e02eSFlorian Westphal 2446dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2447dc24f8b4SPaolo Abeni { 2448dc24f8b4SPaolo Abeni return 0; 2449dc24f8b4SPaolo Abeni } 2450dc24f8b4SPaolo Abeni 2451b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24520e4f35d7SPaolo Abeni { 24530e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2454b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24550e4f35d7SPaolo Abeni 24563abc05d9SFlorian Westphal might_sleep(); 24573abc05d9SFlorian Westphal 24585efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24590e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24600e4f35d7SPaolo Abeni 24610e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24620e4f35d7SPaolo Abeni continue; 24630e4f35d7SPaolo Abeni 246440947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 246540947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 246640947e13SFlorian Westphal continue; 246740947e13SFlorian Westphal 2468b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2469b6985b9bSPaolo Abeni } 2470b6985b9bSPaolo Abeni 24710e4f35d7SPaolo Abeni } 24720e4f35d7SPaolo Abeni 247363740448SPaolo Abeni static bool mptcp_should_close(const struct sock *sk) 2474e16163b6SPaolo Abeni { 2475e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2476e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2477e16163b6SPaolo Abeni 247863740448SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2479e16163b6SPaolo Abeni return true; 2480e16163b6SPaolo Abeni 2481e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2482e16163b6SPaolo Abeni * timeout 2483e16163b6SPaolo Abeni */ 2484e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2485e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2486e16163b6SPaolo Abeni TCP_CLOSE) 2487e16163b6SPaolo Abeni return false; 2488e16163b6SPaolo Abeni } 2489e16163b6SPaolo Abeni return true; 2490e16163b6SPaolo Abeni } 2491e16163b6SPaolo Abeni 249250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 249350c504a2SFlorian Westphal { 249450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 249580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 249650c504a2SFlorian Westphal 249750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 249850c504a2SFlorian Westphal return; 249950c504a2SFlorian Westphal 250050c504a2SFlorian Westphal mptcp_token_destroy(msk); 250150c504a2SFlorian Westphal 25025efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 250350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 250475e908c3SPaolo Abeni bool slow; 250550c504a2SFlorian Westphal 250675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 250750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 250850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 250950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 251050c504a2SFlorian Westphal } 251175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 251250c504a2SFlorian Westphal } 251350c504a2SFlorian Westphal 251469800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 251569800e51SPaolo Abeni switch (sk->sk_state) { 251669800e51SPaolo Abeni case TCP_SYN_SENT: 25179ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 251869800e51SPaolo Abeni break; 251969800e51SPaolo Abeni case TCP_CLOSE_WAIT: 25209ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 252169800e51SPaolo Abeni break; 252269800e51SPaolo Abeni case TCP_CLOSE: 252369800e51SPaolo Abeni return; 252469800e51SPaolo Abeni default: 25259ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 252669800e51SPaolo Abeni } 252769800e51SPaolo Abeni 252850c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 252950c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 253050c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 253150c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 253250c504a2SFlorian Westphal 253369800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 253469800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 253569800e51SPaolo Abeni return; 253669800e51SPaolo Abeni 253769800e51SPaolo Abeni sk->sk_state_change(sk); 253869800e51SPaolo Abeni sk_error_report(sk); 253950c504a2SFlorian Westphal } 254050c504a2SFlorian Westphal 25412948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 254280992017SPaolo Abeni { 25432948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2544caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25453b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25463b1d6210SPaolo Abeni size_t copied = 0; 25472948d0a1SPaolo Abeni struct sock *ssk; 25482948d0a1SPaolo Abeni int ret; 25492948d0a1SPaolo Abeni 2550b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25513241a9c0SFlorian Westphal 25523241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25533241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25542948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25556477dd39SMat Martineau if (!dfrag) { 25566477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25576477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25586477dd39SMat Martineau 25596477dd39SMat Martineau icsk->icsk_retransmits++; 25606477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25616477dd39SMat Martineau mptcp_send_ack(msk); 25626477dd39SMat Martineau 25636477dd39SMat Martineau goto reset_timer; 25646477dd39SMat Martineau } 25656477dd39SMat Martineau 25663241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25672948d0a1SPaolo Abeni return; 25683241a9c0SFlorian Westphal 25693241a9c0SFlorian Westphal goto reset_timer; 25706477dd39SMat Martineau } 25712948d0a1SPaolo Abeni 25722948d0a1SPaolo Abeni if (!ssk) 25732948d0a1SPaolo Abeni goto reset_timer; 25742948d0a1SPaolo Abeni 25752948d0a1SPaolo Abeni lock_sock(ssk); 25762948d0a1SPaolo Abeni 25772948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25782948d0a1SPaolo Abeni info.sent = 0; 25794e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25804e14867dSPaolo Abeni while (info.sent < info.limit) { 25812948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25822948d0a1SPaolo Abeni if (ret <= 0) 25832948d0a1SPaolo Abeni break; 25842948d0a1SPaolo Abeni 25852948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25862948d0a1SPaolo Abeni copied += ret; 25872948d0a1SPaolo Abeni info.sent += ret; 25882948d0a1SPaolo Abeni } 25894e14867dSPaolo Abeni if (copied) { 25904e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 25912948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 25922948d0a1SPaolo Abeni info.size_goal); 25930530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 25944e14867dSPaolo Abeni } 25952948d0a1SPaolo Abeni 25962948d0a1SPaolo Abeni release_sock(ssk); 25972948d0a1SPaolo Abeni 25982948d0a1SPaolo Abeni reset_timer: 25993241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 26003241a9c0SFlorian Westphal 26012948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 26022948d0a1SPaolo Abeni mptcp_reset_timer(sk); 26032948d0a1SPaolo Abeni } 26042948d0a1SPaolo Abeni 260576a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 260676a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 260776a13b31SGeliang Tang */ 260876a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 260976a13b31SGeliang Tang { 261076a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 261176a13b31SGeliang Tang unsigned long timeout, close_timeout; 261276a13b31SGeliang Tang 261376a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 261476a13b31SGeliang Tang return; 261576a13b31SGeliang Tang 261676a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 261776a13b31SGeliang Tang 261876a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 261976a13b31SGeliang Tang * them is active 262076a13b31SGeliang Tang */ 262176a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 262276a13b31SGeliang Tang 262376a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 262476a13b31SGeliang Tang } 262576a13b31SGeliang Tang 262649fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 262749fa1919SGeliang Tang { 262876a13b31SGeliang Tang struct sock *ssk = msk->first; 262949fa1919SGeliang Tang bool slow; 263049fa1919SGeliang Tang 263176a13b31SGeliang Tang if (!ssk) 263276a13b31SGeliang Tang return; 263376a13b31SGeliang Tang 263449fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 263549fa1919SGeliang Tang 263649fa1919SGeliang Tang slow = lock_sock_fast(ssk); 263749fa1919SGeliang Tang mptcp_subflow_reset(ssk); 263876a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 263949fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 264076a13b31SGeliang Tang 264176a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 264249fa1919SGeliang Tang } 264349fa1919SGeliang Tang 2644d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2645d21f8348SPaolo Abeni { 2646d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2647d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2648d21f8348SPaolo Abeni 2649d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2650d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2651d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2652d21f8348SPaolo Abeni } 2653d21f8348SPaolo Abeni 26542948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26552948d0a1SPaolo Abeni { 26562948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 265780638684SGeliang Tang struct sock *sk = (struct sock *)msk; 265876a13b31SGeliang Tang unsigned long fail_tout; 26592948d0a1SPaolo Abeni int state; 266080992017SPaolo Abeni 266180992017SPaolo Abeni lock_sock(sk); 2662e16163b6SPaolo Abeni state = sk->sk_state; 2663d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2664e16163b6SPaolo Abeni goto unlock; 2665e16163b6SPaolo Abeni 266643b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 266750c504a2SFlorian Westphal 266850c504a2SFlorian Westphal mptcp_check_fastclose(msk); 266950c504a2SFlorian Westphal 2670e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2671b416268bSFlorian Westphal 267259832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 267359832e24SFlorian Westphal mptcp_check_for_eof(msk); 267459832e24SFlorian Westphal 26756e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 267643b54c6eSMat Martineau mptcp_check_data_fin(sk); 267743b54c6eSMat Martineau 2678b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2679b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2680b6985b9bSPaolo Abeni 2681341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2682341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2683341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2684e16163b6SPaolo Abeni */ 2685d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 268663740448SPaolo Abeni if (mptcp_should_close(sk)) { 2687e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2688d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2689d21f8348SPaolo Abeni } 2690d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2691e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2692e16163b6SPaolo Abeni goto unlock; 2693e16163b6SPaolo Abeni } 2694d21f8348SPaolo Abeni } 2695e16163b6SPaolo Abeni 26962948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 26972948d0a1SPaolo Abeni __mptcp_retrans(sk); 26983b1d6210SPaolo Abeni 269976a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 270076a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 270149fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 270249fa1919SGeliang Tang 27033b1d6210SPaolo Abeni unlock: 270480992017SPaolo Abeni release_sock(sk); 270580992017SPaolo Abeni sock_put(sk); 270680992017SPaolo Abeni } 270780992017SPaolo Abeni 2708784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2709f870fa0bSMat Martineau { 2710cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2711cec37a6eSPeter Krystad 2712cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2713ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 271418b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 271580992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 271687952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2717ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2718f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 27196511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2720ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 272133d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2722cec37a6eSPeter Krystad 27238ab183deSPaolo Abeni msk->first = NULL; 2724dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2725752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 27260530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 27271e1d9d6fSPaolo Abeni msk->recovery = false; 27288ab183deSPaolo Abeni 27291b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27301b1c7a0eSPeter Krystad 2731b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2732b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2733e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2734aa1fbd94SFlorian Westphal 2735f870fa0bSMat Martineau return 0; 2736f870fa0bSMat Martineau } 2737f870fa0bSMat Martineau 2738b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2739784325e9SMatthieu Baerts { 274020b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2741b29fcfb5SPaolo Abeni 2742b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2743b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2744b29fcfb5SPaolo Abeni 2745b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2746b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2747b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2748b29fcfb5SPaolo Abeni } 2749b29fcfb5SPaolo Abeni 2750b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2751b29fcfb5SPaolo Abeni { 2752fc518953SFlorian Westphal struct net *net = sock_net(sk); 2753fc518953SFlorian Westphal int ret; 275418b683bfSPaolo Abeni 2755b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2756b6c08380SGeliang Tang if (ret) 2757b6c08380SGeliang Tang return ret; 2758b6c08380SGeliang Tang 2759fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2760fc518953SFlorian Westphal return -ENOPROTOOPT; 2761fc518953SFlorian Westphal 2762fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2763fc518953SFlorian Westphal return -ENOMEM; 2764fc518953SFlorian Westphal 2765a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2766a5ef058dSPaolo Abeni 276720b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 276820b5759fSPaolo Abeni * propagate the correct value 276920b5759fSPaolo Abeni */ 2770b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 277120b5759fSPaolo Abeni 2772d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2773a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2774a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2775d027236cSPaolo Abeni 277618b683bfSPaolo Abeni return 0; 277718b683bfSPaolo Abeni } 277818b683bfSPaolo Abeni 277918b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 278018b683bfSPaolo Abeni { 278118b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 278218b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 278318b683bfSPaolo Abeni 2784d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 278518b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2786d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2787784325e9SMatthieu Baerts } 2788784325e9SMatthieu Baerts 278930e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 279080992017SPaolo Abeni { 279180992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 279280992017SPaolo Abeni 2793b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2794e16163b6SPaolo Abeni __sock_put(sk); 279580992017SPaolo Abeni } 279680992017SPaolo Abeni 2797d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 279821498490SPeter Krystad { 279921498490SPeter Krystad lock_sock(ssk); 280021498490SPeter Krystad 280121498490SPeter Krystad switch (ssk->sk_state) { 280221498490SPeter Krystad case TCP_LISTEN: 280321498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 280421498490SPeter Krystad break; 2805df561f66SGustavo A. R. Silva fallthrough; 280621498490SPeter Krystad case TCP_SYN_SENT: 280721498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 280821498490SPeter Krystad break; 280921498490SPeter Krystad default: 281043b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 281143b54c6eSMat Martineau pr_debug("Fallback"); 281221498490SPeter Krystad ssk->sk_shutdown |= how; 281321498490SPeter Krystad tcp_shutdown(ssk, how); 281443b54c6eSMat Martineau } else { 281543b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 281643b54c6eSMat Martineau tcp_send_ack(ssk); 28176477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 28186477dd39SMat Martineau mptcp_reset_timer(sk); 281943b54c6eSMat Martineau } 282021498490SPeter Krystad break; 282121498490SPeter Krystad } 282221498490SPeter Krystad 282321498490SPeter Krystad release_sock(ssk); 282421498490SPeter Krystad } 282521498490SPeter Krystad 28266920b851SMat Martineau static const unsigned char new_state[16] = { 28276920b851SMat Martineau /* current state: new state: action: */ 28286920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 28296920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28306920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 28316920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28326920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 28336920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 28346920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28356920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28366920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28376920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28386920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28396920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28406920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28416920b851SMat Martineau }; 28426920b851SMat Martineau 28436920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28446920b851SMat Martineau { 28456920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28466920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28476920b851SMat Martineau 28486920b851SMat Martineau inet_sk_state_store(sk, ns); 28496920b851SMat Martineau 28506920b851SMat Martineau return next & TCP_ACTION_FIN; 28516920b851SMat Martineau } 28526920b851SMat Martineau 2853e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2854e16163b6SPaolo Abeni { 2855e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2856e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2857e16163b6SPaolo Abeni 2858e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2859e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2860e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2861e16163b6SPaolo Abeni 2862e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2863e16163b6SPaolo Abeni * skip this 2864e16163b6SPaolo Abeni */ 2865e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2866e16163b6SPaolo Abeni mptcp_send_head(sk)) 2867e16163b6SPaolo Abeni return; 2868e16163b6SPaolo Abeni 2869e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2870e16163b6SPaolo Abeni 287126aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 287226aa2314SPaolo Abeni * state now 287326aa2314SPaolo Abeni */ 287426aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 287558cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 287626aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2877e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2878e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 287926aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 288026aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 288126aa2314SPaolo Abeni } 2882e16163b6SPaolo Abeni } 2883e16163b6SPaolo Abeni 2884e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2885e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2886e16163b6SPaolo Abeni 2887e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2888e16163b6SPaolo Abeni } 2889e16163b6SPaolo Abeni } 2890e16163b6SPaolo Abeni 2891e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2892e16163b6SPaolo Abeni { 2893e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2894e16163b6SPaolo Abeni 2895e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2896e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2897e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2898e16163b6SPaolo Abeni 2899e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2900e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2901e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2902e16163b6SPaolo Abeni 2903e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2904e16163b6SPaolo Abeni } 2905e16163b6SPaolo Abeni 2906e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2907f870fa0bSMat Martineau { 2908f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2909f870fa0bSMat Martineau 2910e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 29112c22c06cSFlorian Westphal 29123abc05d9SFlorian Westphal might_sleep(); 29133abc05d9SFlorian Westphal 2914bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2915e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2916e16163b6SPaolo Abeni msk->pm.status = 0; 2917b2c5b614SFlorian Westphal 2918e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2919e16163b6SPaolo Abeni 29206511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 292187952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2922e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2923e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2924aa1fbd94SFlorian Westphal 2925e16163b6SPaolo Abeni sock_put(sk); 2926e16163b6SPaolo Abeni } 2927e16163b6SPaolo Abeni 292863740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 292963740448SPaolo Abeni { 293063740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 293163740448SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 293263740448SPaolo Abeni mptcp_do_fastclose(sk); 293363740448SPaolo Abeni __mptcp_destroy_sock(sk); 293463740448SPaolo Abeni } 293563740448SPaolo Abeni 2936d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2937d21f8348SPaolo Abeni { 2938d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2939d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2940d21f8348SPaolo Abeni */ 2941d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2942d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2943d21f8348SPaolo Abeni return 0; 2944d21f8348SPaolo Abeni 2945d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2946d21f8348SPaolo Abeni } 2947d21f8348SPaolo Abeni 2948c558246eSMenglong Dong static void mptcp_listen_inuse_dec(struct sock *sk) 2949c558246eSMenglong Dong { 2950c558246eSMenglong Dong if (inet_sk_state_load(sk) == TCP_LISTEN) 2951c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2952c558246eSMenglong Dong } 2953c558246eSMenglong Dong 295426d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2955e16163b6SPaolo Abeni { 2956e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 295776a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2958e16163b6SPaolo Abeni bool do_cancel_work = false; 2959d4e85922SPaolo Abeni int subflows_alive = 0; 2960e16163b6SPaolo Abeni 2961e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2962e16163b6SPaolo Abeni 2963e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2964c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 2965e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2966e16163b6SPaolo Abeni goto cleanup; 2967e16163b6SPaolo Abeni } 2968e16163b6SPaolo Abeni 29698d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 29708d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 29718d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 29728d547809SPaolo Abeni */ 2973d21f8348SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2974d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 29758d547809SPaolo Abeni timeout = 0; 2976d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2977e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2978d21f8348SPaolo Abeni } 2979e16163b6SPaolo Abeni 2980e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2981e16163b6SPaolo Abeni 2982e16163b6SPaolo Abeni cleanup: 2983e16163b6SPaolo Abeni /* orphan all the subflows */ 2984e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 298576a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2986e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 298749054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2988e16163b6SPaolo Abeni 2989d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 2990d4e85922SPaolo Abeni 299176a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 299276a13b31SGeliang Tang * cancel the latter 299376a13b31SGeliang Tang */ 299476a13b31SGeliang Tang if (ssk == msk->first) 299576a13b31SGeliang Tang subflow->fail_tout = 0; 299676a13b31SGeliang Tang 2997fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 2998fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 2999fe948001SMenglong Dong */ 3000fe948001SMenglong Dong ssk->sk_socket = NULL; 3001fe948001SMenglong Dong ssk->sk_wq = NULL; 3002e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 3003e16163b6SPaolo Abeni } 3004e16163b6SPaolo Abeni sock_orphan(sk); 3005e16163b6SPaolo Abeni 3006d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 3007d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 3008d4e85922SPaolo Abeni */ 3009d4e85922SPaolo Abeni if (subflows_alive == 0) 3010d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3011d4e85922SPaolo Abeni 3012e16163b6SPaolo Abeni sock_hold(sk); 3013e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 301400df24f1SGeliang Tang if (msk->token) 301576a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3016b29fcfb5SPaolo Abeni 3017e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 3018e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 3019e16163b6SPaolo Abeni do_cancel_work = true; 3020e16163b6SPaolo Abeni } else { 302176a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 3022e16163b6SPaolo Abeni } 302326d3e21cSMenglong Dong 302426d3e21cSMenglong Dong return do_cancel_work; 302526d3e21cSMenglong Dong } 302626d3e21cSMenglong Dong 302726d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 302826d3e21cSMenglong Dong { 302926d3e21cSMenglong Dong bool do_cancel_work; 303026d3e21cSMenglong Dong 303126d3e21cSMenglong Dong lock_sock(sk); 303226d3e21cSMenglong Dong 303326d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 3034e16163b6SPaolo Abeni release_sock(sk); 3035e16163b6SPaolo Abeni if (do_cancel_work) 303680992017SPaolo Abeni mptcp_cancel_work(sk); 3037b911c97cSFlorian Westphal 3038e16163b6SPaolo Abeni sock_put(sk); 3039f870fa0bSMat Martineau } 3040f870fa0bSMat Martineau 3041e72e4032SPaolo Abeni void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3042cf7da0d6SPeter Krystad { 3043cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3044cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3045cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3046cf7da0d6SPeter Krystad 3047cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3048cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3049cf7da0d6SPeter Krystad 3050cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3051cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3052cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3053cf7da0d6SPeter Krystad } 3054cf7da0d6SPeter Krystad #endif 3055cf7da0d6SPeter Krystad 3056cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3057cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3058cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3059cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3060cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3061cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3062cf7da0d6SPeter Krystad } 3063cf7da0d6SPeter Krystad 306418b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 306518b683bfSPaolo Abeni { 306676e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 306776e2a55dSPaolo Abeni 30687d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 30697d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 30707d803344SPaolo Abeni * msk->firstsocket lock). Do nothing and leave the cleanup to the 30717d803344SPaolo Abeni * caller. 30727d803344SPaolo Abeni */ 30737d803344SPaolo Abeni if (msk->fastopening) 30747d803344SPaolo Abeni return 0; 30757d803344SPaolo Abeni 3076c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 3077b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3078b29fcfb5SPaolo Abeni 3079bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3080b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3081b29fcfb5SPaolo Abeni 308200df24f1SGeliang Tang if (msk->token) 308300df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3084b29fcfb5SPaolo Abeni 3085c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3086c0bf3c6aSPaolo Abeni * subflow 3087c0bf3c6aSPaolo Abeni */ 3088c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3089b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3090e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3091e9d09bacSPaolo Abeni msk->cb_flags = 0; 3092e9d09bacSPaolo Abeni msk->push_pending = 0; 3093b29fcfb5SPaolo Abeni msk->recovery = false; 3094b29fcfb5SPaolo Abeni msk->can_ack = false; 3095b29fcfb5SPaolo Abeni msk->fully_established = false; 3096b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3097b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3098b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3099b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3100b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3101b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3102b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 3103b29fcfb5SPaolo Abeni 3104b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 3105b29fcfb5SPaolo Abeni sk_error_report(sk); 310642c556feSFlorian Westphal return 0; 310718b683bfSPaolo Abeni } 310818b683bfSPaolo Abeni 3109b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3110b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3111b0519de8SFlorian Westphal { 3112b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3113b0519de8SFlorian Westphal 3114b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3115b0519de8SFlorian Westphal } 3116b0519de8SFlorian Westphal #endif 3117b0519de8SFlorian Westphal 3118fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 3119cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 3120fca5c82cSPaolo Abeni struct request_sock *req) 3121b0519de8SFlorian Westphal { 312258b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3123b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 312458b09919SPaolo Abeni struct mptcp_sock *msk; 3125b0519de8SFlorian Westphal 3126b0519de8SFlorian Westphal if (!nsk) 3127b0519de8SFlorian Westphal return NULL; 3128b0519de8SFlorian Westphal 3129b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3130b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3131b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3132b0519de8SFlorian Westphal #endif 3133b0519de8SFlorian Westphal 313458b09919SPaolo Abeni __mptcp_init_sock(nsk); 313558b09919SPaolo Abeni 313658b09919SPaolo Abeni msk = mptcp_sk(nsk); 313758b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 313858b09919SPaolo Abeni msk->token = subflow_req->token; 313958b09919SPaolo Abeni msk->subflow = NULL; 3140b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3141b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 314274c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 314306251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 314458b09919SPaolo Abeni 314558b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3146eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31477439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31487439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3149df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31506f8a612aSFlorian Westphal 31515e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31527f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 31537f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 31540c148460SPaolo Abeni 31550c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 315658b09919SPaolo Abeni bh_unlock_sock(nsk); 315758b09919SPaolo Abeni 3158b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3159b0519de8SFlorian Westphal return nsk; 3160b0519de8SFlorian Westphal } 3161b0519de8SFlorian Westphal 3162a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3163a6b118feSFlorian Westphal { 3164a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3165a6b118feSFlorian Westphal 3166a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3167a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3168a6b118feSFlorian Westphal 3169a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3170a6b118feSFlorian Westphal 3171a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3172a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3173a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3174a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3175a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 31766f8a612aSFlorian Westphal 31777439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3178a6b118feSFlorian Westphal } 3179a6b118feSFlorian Westphal 3180cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3181cf7da0d6SPeter Krystad bool kern) 3182cf7da0d6SPeter Krystad { 3183cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3184cf7da0d6SPeter Krystad struct socket *listener; 3185cf7da0d6SPeter Krystad struct sock *newsk; 3186cf7da0d6SPeter Krystad 318761761231SPaolo Abeni listener = msk->subflow; 3188cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3189cf7da0d6SPeter Krystad *err = -EINVAL; 3190cf7da0d6SPeter Krystad return NULL; 3191cf7da0d6SPeter Krystad } 3192cf7da0d6SPeter Krystad 3193cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3194cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3195cf7da0d6SPeter Krystad if (!newsk) 3196cf7da0d6SPeter Krystad return NULL; 3197cf7da0d6SPeter Krystad 3198cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3199cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3200cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3201cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3202cf7da0d6SPeter Krystad 3203cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 320458b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 320558b09919SPaolo Abeni 320658b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 320758b09919SPaolo Abeni * subflow_syn_recv_sock() 320858b09919SPaolo Abeni */ 320958b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 321058b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3211d6692b3bSFlorian Westphal goto out; 321258b09919SPaolo Abeni } 321358b09919SPaolo Abeni 3214cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 32150397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3216fc518953SFlorian Westphal } else { 3217fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3218fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3219cf7da0d6SPeter Krystad } 3220cf7da0d6SPeter Krystad 3221d6692b3bSFlorian Westphal out: 3222d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3223cf7da0d6SPeter Krystad return newsk; 3224cf7da0d6SPeter Krystad } 3225cf7da0d6SPeter Krystad 3226c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 32275c8c1640SGeliang Tang { 3228c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 322987952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 323087952603SPaolo Abeni 32316e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 32326e628cd3SPaolo Abeni 3233c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 32345efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3235c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3236c0bf3c6aSPaolo Abeni 323787952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3238b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 323987952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32406511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32415c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3242b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32436511882cSPaolo Abeni 32446511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 32456511882cSPaolo Abeni * inet_sock_destruct() will dispose it 32466511882cSPaolo Abeni */ 32476511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 32486511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 32495c8c1640SGeliang Tang mptcp_token_destroy(msk); 32505c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 32514638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 32525c8c1640SGeliang Tang } 32535c8c1640SGeliang Tang 325479c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 325579c0949eSPeter Krystad { 3256c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3257c9fd9c5fSFlorian Westphal 3258c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3259c0bf3c6aSPaolo Abeni * even the initial subflow 3260c0bf3c6aSPaolo Abeni */ 3261c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3262c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3263d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 326479c0949eSPeter Krystad } 326579c0949eSPeter Krystad 32666e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32676e628cd3SPaolo Abeni { 32686e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32696e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32706e628cd3SPaolo Abeni else 3271e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 32726e628cd3SPaolo Abeni 32736e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 32746e628cd3SPaolo Abeni mptcp_schedule_work(sk); 32756e628cd3SPaolo Abeni } 32766e628cd3SPaolo Abeni 3277219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 32786e628cd3SPaolo Abeni { 32796e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 32806e628cd3SPaolo Abeni return; 32816e628cd3SPaolo Abeni 328273a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 328373a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 328473a0052aSGeliang Tang else 3285e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 32866e628cd3SPaolo Abeni } 32876e628cd3SPaolo Abeni 3288e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3289e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3290e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3291e9d09bacSPaolo Abeni 3292e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 329314c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3294e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 329514c441b5SPaolo Abeni { 3296e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 32972d6f5a2bSPaolo Abeni 3298e9d09bacSPaolo Abeni for (;;) { 3299e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3300e9d09bacSPaolo Abeni msk->push_pending; 3301c2e6048fSPaolo Abeni if (!flags) 3302c2e6048fSPaolo Abeni break; 3303c2e6048fSPaolo Abeni 3304c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 33056e628cd3SPaolo Abeni * 33066e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 33076e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 33086e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 33096e628cd3SPaolo Abeni * the subflow socket lock 33106e628cd3SPaolo Abeni */ 3311e9d09bacSPaolo Abeni msk->push_pending = 0; 3312e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 33136e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 33143e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 33153e501490SPaolo Abeni __mptcp_flush_join_list(sk); 33162e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3317c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 33182d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 33192d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3320c2e6048fSPaolo Abeni 3321c2e6048fSPaolo Abeni cond_resched(); 33226e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 33236e628cd3SPaolo Abeni } 3324c2e6048fSPaolo Abeni 332565a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 332665a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 332765a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3328490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 332965a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3330490274b4SPaolo Abeni */ 3331e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3332490274b4SPaolo Abeni __mptcp_set_connected(sk); 3333e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 333415cc1045SPaolo Abeni __mptcp_error_report(sk); 333543f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 333643f5b111SPaolo Abeni msk->last_snd = NULL; 333765a569b0SPaolo Abeni } 33386e628cd3SPaolo Abeni 333987952603SPaolo Abeni __mptcp_update_rmem(sk); 334014c441b5SPaolo Abeni } 334114c441b5SPaolo Abeni 3342bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3343bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3344bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3345bcd97734SPaolo Abeni */ 3346bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3347bcd97734SPaolo Abeni { 3348bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3349bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3350bcd97734SPaolo Abeni unsigned long timeout; 3351bcd97734SPaolo Abeni 3352bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3353bcd97734SPaolo Abeni return; 3354bcd97734SPaolo Abeni 3355bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3356bcd97734SPaolo Abeni if (tp->srtt_us) 3357bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3358bcd97734SPaolo Abeni else 3359bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3360bcd97734SPaolo Abeni timeout += jiffies; 3361bcd97734SPaolo Abeni 3362bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3363bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3364bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3365bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3366bcd97734SPaolo Abeni } 3367bcd97734SPaolo Abeni 3368b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3369b19bc294SPaolo Abeni { 3370b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3371b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3372b19bc294SPaolo Abeni 3373bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3374b19bc294SPaolo Abeni mptcp_data_lock(sk); 3375b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 337673a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3377b19bc294SPaolo Abeni else 3378e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3379b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3380bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3381bcd97734SPaolo Abeni } 3382bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3383bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3384bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3385bcd97734SPaolo Abeni } 3386b19bc294SPaolo Abeni } 3387b19bc294SPaolo Abeni 33882c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 33892c5ebd00SPaolo Abeni { 33902c5ebd00SPaolo Abeni /* should never be called, 33912c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 33922c5ebd00SPaolo Abeni */ 33932c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 33942c5ebd00SPaolo Abeni return 0; 33952c5ebd00SPaolo Abeni } 33962c5ebd00SPaolo Abeni 33972c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 33982c5ebd00SPaolo Abeni { 33992c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 34002c5ebd00SPaolo Abeni } 34012c5ebd00SPaolo Abeni 3402cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3403f870fa0bSMat Martineau { 3404f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3405cec37a6eSPeter Krystad struct socket *ssock; 3406f870fa0bSMat Martineau 340761761231SPaolo Abeni ssock = msk->subflow; 3408cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3409cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3410cec37a6eSPeter Krystad return -EINVAL; 3411f870fa0bSMat Martineau 3412cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3413cec37a6eSPeter Krystad } 3414f870fa0bSMat Martineau 3415cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3416cec37a6eSPeter Krystad { 3417cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3418cec37a6eSPeter Krystad struct mptcp_sock *msk; 3419cec37a6eSPeter Krystad struct sock *sk; 3420f870fa0bSMat Martineau 3421cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3422cec37a6eSPeter Krystad sk = subflow->conn; 3423cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3424cec37a6eSPeter Krystad 3425648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3426648ef4b8SMat Martineau 3427b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3428648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 34296d0060f6SMat Martineau 3430cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3431cec37a6eSPeter Krystad * accessing the field below 3432cec37a6eSPeter Krystad */ 3433cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 34346d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3435eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 34367439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34371b1c7a0eSPeter Krystad 34386c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3439a6b118feSFlorian Westphal 3440a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3441f870fa0bSMat Martineau } 3442f870fa0bSMat Martineau 3443866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3444cf7da0d6SPeter Krystad { 3445cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3446cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3447cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3448cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3449cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3450cf7da0d6SPeter Krystad } 3451cf7da0d6SPeter Krystad 3452e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3453f296234cSPeter Krystad { 3454e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3455f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3456f296234cSPeter Krystad struct sock *parent = (void *)msk; 34573e501490SPaolo Abeni bool ret = true; 3458f296234cSPeter Krystad 3459f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3460f296234cSPeter Krystad 3461f296234cSPeter Krystad /* mptcp socket already closing? */ 3462dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3463dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3464f296234cSPeter Krystad return false; 3465dc87efdbSFlorian Westphal } 3466f296234cSPeter Krystad 346770c708e8SKishen Maloor if (!list_empty(&subflow->node)) 3468b911c97cSFlorian Westphal goto out; 3469f296234cSPeter Krystad 34703e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 34713e501490SPaolo Abeni goto err_prohibited; 347210f6d46cSPaolo Abeni 34733e501490SPaolo Abeni /* active connections are already on conn_list. 34743e501490SPaolo Abeni * If we can't acquire msk socket lock here, let the release callback 34753e501490SPaolo Abeni * handle it 347610f6d46cSPaolo Abeni */ 34773e501490SPaolo Abeni mptcp_data_lock(parent); 34783e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 34793e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 34803e501490SPaolo Abeni if (ret) { 3481e16163b6SPaolo Abeni sock_hold(ssk); 34823e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3483e16163b6SPaolo Abeni } 34843e501490SPaolo Abeni } else { 34853e501490SPaolo Abeni sock_hold(ssk); 34863e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3487e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 34883e501490SPaolo Abeni } 34893e501490SPaolo Abeni mptcp_data_unlock(parent); 34903e501490SPaolo Abeni 3491dc87efdbSFlorian Westphal if (!ret) { 34923e501490SPaolo Abeni err_prohibited: 3493dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 349410f6d46cSPaolo Abeni return false; 3495dc87efdbSFlorian Westphal } 349610f6d46cSPaolo Abeni 3497917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 34980530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 34993e501490SPaolo Abeni 3500b911c97cSFlorian Westphal out: 3501b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 350210f6d46cSPaolo Abeni return true; 3503f296234cSPeter Krystad } 3504f296234cSPeter Krystad 350576e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 350676e2a55dSPaolo Abeni { 350776e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 350876e2a55dSPaolo Abeni 350976e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 351076e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 351176e2a55dSPaolo Abeni } 351276e2a55dSPaolo Abeni 35136511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 35146511882cSPaolo Abeni { 35156511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 35166511882cSPaolo Abeni } 35176511882cSPaolo Abeni 3518644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3519644807e3SFlorian Westphal { 3520644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3521644807e3SFlorian Westphal u64 delta; 3522644807e3SFlorian Westphal 3523644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3524644807e3SFlorian Westphal return -EINVAL; 3525644807e3SFlorian Westphal 3526644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3527644807e3SFlorian Westphal return 0; 3528644807e3SFlorian Westphal 3529644807e3SFlorian Westphal delta = msk->write_seq - v; 353007c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 353107c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 353207c2c7a3SPaolo Abeni 353307c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 353407c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 353507c2c7a3SPaolo Abeni * so ignore that status, too. 353607c2c7a3SPaolo Abeni */ 353707c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 353807c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 353907c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 354007c2c7a3SPaolo Abeni } 3541644807e3SFlorian Westphal if (delta > INT_MAX) 3542644807e3SFlorian Westphal delta = INT_MAX; 3543644807e3SFlorian Westphal 3544644807e3SFlorian Westphal return (int)delta; 3545644807e3SFlorian Westphal } 3546644807e3SFlorian Westphal 3547644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3548644807e3SFlorian Westphal { 3549644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3550644807e3SFlorian Westphal bool slow; 3551644807e3SFlorian Westphal int answ; 3552644807e3SFlorian Westphal 3553644807e3SFlorian Westphal switch (cmd) { 3554644807e3SFlorian Westphal case SIOCINQ: 3555644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3556644807e3SFlorian Westphal return -EINVAL; 3557644807e3SFlorian Westphal 3558644807e3SFlorian Westphal lock_sock(sk); 3559644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3560644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3561644807e3SFlorian Westphal release_sock(sk); 3562644807e3SFlorian Westphal break; 3563644807e3SFlorian Westphal case SIOCOUTQ: 3564644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3565644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3566644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3567644807e3SFlorian Westphal break; 3568644807e3SFlorian Westphal case SIOCOUTQNSD: 3569644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3570644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3571644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3572644807e3SFlorian Westphal break; 3573644807e3SFlorian Westphal default: 3574644807e3SFlorian Westphal return -ENOIOCTLCMD; 3575644807e3SFlorian Westphal } 3576644807e3SFlorian Westphal 3577644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3578644807e3SFlorian Westphal } 3579644807e3SFlorian Westphal 358054f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 358154f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 358254f1944eSPaolo Abeni { 358354f1944eSPaolo Abeni subflow->request_mptcp = 0; 358454f1944eSPaolo Abeni __mptcp_do_fallback(msk); 358554f1944eSPaolo Abeni } 358654f1944eSPaolo Abeni 358754f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 358854f1944eSPaolo Abeni { 358954f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 359054f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 359154f1944eSPaolo Abeni struct socket *ssock; 359254f1944eSPaolo Abeni int err = -EINVAL; 359354f1944eSPaolo Abeni 359454f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3595ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 3596ddb1a072SPaolo Abeni return PTR_ERR(ssock); 359754f1944eSPaolo Abeni 359854f1944eSPaolo Abeni mptcp_token_destroy(msk); 359954f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 360054f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 360154f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 360254f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 360354f1944eSPaolo Abeni * TCP option space. 360454f1944eSPaolo Abeni */ 360554f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 360654f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 360754f1944eSPaolo Abeni #endif 360854f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 360954f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 361054f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 361154f1944eSPaolo Abeni } 361254f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 361354f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 361454f1944eSPaolo Abeni 361554f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 361654f1944eSPaolo Abeni * acquired the subflow socket lock, too. 361754f1944eSPaolo Abeni */ 36187d803344SPaolo Abeni if (msk->fastopening) 3619*786fc124SPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1); 362054f1944eSPaolo Abeni else 3621*786fc124SPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK); 362254f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 362354f1944eSPaolo Abeni 362454f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 362554f1944eSPaolo Abeni * subflow_finish_connect() 362654f1944eSPaolo Abeni */ 362754f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 362854f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 362954f1944eSPaolo Abeni return err; 363054f1944eSPaolo Abeni } 363154f1944eSPaolo Abeni 363254f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 363354f1944eSPaolo Abeni 3634*786fc124SPaolo Abeni /* silence EINPROGRESS and let the caller inet_stream_connect 3635*786fc124SPaolo Abeni * handle the connection in progress 363654f1944eSPaolo Abeni */ 3637*786fc124SPaolo Abeni return 0; 363854f1944eSPaolo Abeni } 363954f1944eSPaolo Abeni 3640f870fa0bSMat Martineau static struct proto mptcp_prot = { 3641f870fa0bSMat Martineau .name = "MPTCP", 3642f870fa0bSMat Martineau .owner = THIS_MODULE, 3643f870fa0bSMat Martineau .init = mptcp_init_sock, 364454f1944eSPaolo Abeni .connect = mptcp_connect, 364518b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3646f870fa0bSMat Martineau .close = mptcp_close, 3647cf7da0d6SPeter Krystad .accept = mptcp_accept, 3648717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3649717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 365076e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 365179c0949eSPeter Krystad .destroy = mptcp_destroy, 3652f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3653644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3654f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 365514c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36562c5ebd00SPaolo Abeni .hash = mptcp_hash, 36572c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3658cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36596511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3660d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36610defbb0aSEric Dumazet 3662d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36630defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36640defbb0aSEric Dumazet 3665d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3666d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3667989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3668d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3669f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36702c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3671f870fa0bSMat Martineau .no_autobind = true, 3672f870fa0bSMat Martineau }; 3673f870fa0bSMat Martineau 36742303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 36752303f994SPeter Krystad { 36762303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 36772303f994SPeter Krystad struct socket *ssock; 3678cf7da0d6SPeter Krystad int err; 36792303f994SPeter Krystad 36802303f994SPeter Krystad lock_sock(sock->sk); 3681fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3682ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3683ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 36842303f994SPeter Krystad goto unlock; 36852303f994SPeter Krystad } 36862303f994SPeter Krystad 36872303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3688cf7da0d6SPeter Krystad if (!err) 3689cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 36902303f994SPeter Krystad 36912303f994SPeter Krystad unlock: 36922303f994SPeter Krystad release_sock(sock->sk); 36932303f994SPeter Krystad return err; 36942303f994SPeter Krystad } 36952303f994SPeter Krystad 3696cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3697cf7da0d6SPeter Krystad { 3698cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3699cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 3700cf7da0d6SPeter Krystad struct socket *ssock; 3701cf7da0d6SPeter Krystad int err; 3702cf7da0d6SPeter Krystad 3703cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3704cf7da0d6SPeter Krystad 3705cfdcfeedSMenglong Dong lock_sock(sk); 3706fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3707ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3708ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 3709cf7da0d6SPeter Krystad goto unlock; 3710cf7da0d6SPeter Krystad } 3711cf7da0d6SPeter Krystad 3712fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3713cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3714cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 37155e20087dSFlorian Westphal 3716cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3717cfdcfeedSMenglong Dong inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3718c558246eSMenglong Dong if (!err) { 3719c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3720cfdcfeedSMenglong Dong mptcp_copy_inaddrs(sk, ssock->sk); 3721c558246eSMenglong Dong } 3722cf7da0d6SPeter Krystad 3723f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 3724f8c9dfbdSGeliang Tang 3725cf7da0d6SPeter Krystad unlock: 3726cfdcfeedSMenglong Dong release_sock(sk); 3727cf7da0d6SPeter Krystad return err; 3728cf7da0d6SPeter Krystad } 3729cf7da0d6SPeter Krystad 3730cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3731cf7da0d6SPeter Krystad int flags, bool kern) 3732cf7da0d6SPeter Krystad { 3733cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3734cf7da0d6SPeter Krystad struct socket *ssock; 3735cf7da0d6SPeter Krystad int err; 3736cf7da0d6SPeter Krystad 3737cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3738cf7da0d6SPeter Krystad 373961761231SPaolo Abeni /* buggy applications can call accept on socket states other then LISTEN 374061761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 374161761231SPaolo Abeni */ 374261761231SPaolo Abeni ssock = msk->subflow; 3743cf7da0d6SPeter Krystad if (!ssock) 374471ba088cSPaolo Abeni return -EINVAL; 3745cf7da0d6SPeter Krystad 3746cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3747d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3748cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3749cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37500397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 37510397c6d8SPaolo Abeni 3752a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3753b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3754a5ef058dSPaolo Abeni 37554d54cc32SFlorian Westphal lock_sock(newsk); 37565b950ff4SPaolo Abeni 3757cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3758cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3759cf7da0d6SPeter Krystad */ 3760190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3761cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3762cf7da0d6SPeter Krystad 3763cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3764cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3765cf7da0d6SPeter Krystad } 376663740448SPaolo Abeni 376763740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 376863740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 376963740448SPaolo Abeni */ 377063740448SPaolo Abeni if (msk->first && 377163740448SPaolo Abeni unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 377263740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 377363740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 377463740448SPaolo Abeni if (unlikely(list_empty(&msk->conn_list))) 377563740448SPaolo Abeni inet_sk_state_store(newsk, TCP_CLOSE); 377663740448SPaolo Abeni } 377763740448SPaolo Abeni 37784d54cc32SFlorian Westphal release_sock(newsk); 3779cf7da0d6SPeter Krystad } 3780cf7da0d6SPeter Krystad 3781cf7da0d6SPeter Krystad return err; 3782cf7da0d6SPeter Krystad } 3783cf7da0d6SPeter Krystad 37848edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 37858edf0864SFlorian Westphal { 37868edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 37878edf0864SFlorian Westphal 37888edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3789dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37908edf0864SFlorian Westphal 37918edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 37928edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 37938edf0864SFlorian Westphal 37945cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 37956e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 37966e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 37976e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37988edf0864SFlorian Westphal 37996e628cd3SPaolo Abeni return 0; 38008edf0864SFlorian Westphal } 38018edf0864SFlorian Westphal 38022303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 38032303f994SPeter Krystad struct poll_table_struct *wait) 38042303f994SPeter Krystad { 38051891c4a0SFlorian Westphal struct sock *sk = sock->sk; 38068ab183deSPaolo Abeni struct mptcp_sock *msk; 38072303f994SPeter Krystad __poll_t mask = 0; 38088a05661bSPaolo Abeni int state; 38092303f994SPeter Krystad 38101891c4a0SFlorian Westphal msk = mptcp_sk(sk); 38111891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 38121891c4a0SFlorian Westphal 38138a05661bSPaolo Abeni state = inet_sk_state_load(sk); 38146719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 381571ba088cSPaolo Abeni if (state == TCP_LISTEN) { 381671ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 381771ba088cSPaolo Abeni return 0; 381871ba088cSPaolo Abeni 381971ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 382071ba088cSPaolo Abeni } 38218a05661bSPaolo Abeni 38228a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 38238a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 38248edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3825a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3826a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3827a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 38288a05661bSPaolo Abeni } 3829dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3830dd913410SPaolo Abeni mask |= EPOLLHUP; 38311891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 38321891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 38331891c4a0SFlorian Westphal 3834d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 383515cc1045SPaolo Abeni smp_rmb(); 38369ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 383715cc1045SPaolo Abeni mask |= EPOLLERR; 383815cc1045SPaolo Abeni 38392303f994SPeter Krystad return mask; 38402303f994SPeter Krystad } 38412303f994SPeter Krystad 3842e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3843e42f1ac6SFlorian Westphal .family = PF_INET, 3844e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38450a3cc579SPaolo Abeni .release = inet_release, 3846e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3847*786fc124SPaolo Abeni .connect = inet_stream_connect, 3848e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3849e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3850d2f77c53SPaolo Abeni .getname = inet_getname, 3851e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3852e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3853e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3854e42f1ac6SFlorian Westphal .listen = mptcp_listen, 385576e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3856e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3857e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3858e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3859e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3860e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3861e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3862e42f1ac6SFlorian Westphal }; 38632303f994SPeter Krystad 3864f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3865f870fa0bSMat Martineau .type = SOCK_STREAM, 3866f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3867f870fa0bSMat Martineau .prot = &mptcp_prot, 38682303f994SPeter Krystad .ops = &mptcp_stream_ops, 38692303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3870f870fa0bSMat Martineau }; 3871f870fa0bSMat Martineau 3872b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3873b19bc294SPaolo Abeni { 3874b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3875b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3876b19bc294SPaolo Abeni int work_done = 0; 3877b19bc294SPaolo Abeni 3878b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3879b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3880b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3881b19bc294SPaolo Abeni 3882b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3883b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3884b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3885b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3886b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3887b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3888b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3889b19bc294SPaolo Abeni * CPU that scheduled it. 3890b19bc294SPaolo Abeni */ 3891b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3892b19bc294SPaolo Abeni sock_put(ssk); 3893b19bc294SPaolo Abeni 3894b19bc294SPaolo Abeni if (++work_done == budget) 3895b19bc294SPaolo Abeni return budget; 3896b19bc294SPaolo Abeni } 3897b19bc294SPaolo Abeni 3898b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3899b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3900b19bc294SPaolo Abeni */ 3901b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3902b19bc294SPaolo Abeni return work_done; 3903b19bc294SPaolo Abeni } 3904b19bc294SPaolo Abeni 3905d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3906f870fa0bSMat Martineau { 3907b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3908b19bc294SPaolo Abeni int cpu; 3909b19bc294SPaolo Abeni 39102303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 39112303f994SPeter Krystad 3912d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3913d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3914d027236cSPaolo Abeni 3915b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3916b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3917b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3918b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 391916d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 392016d083e2SJakub Kicinski mptcp_napi_poll); 3921b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3922b19bc294SPaolo Abeni } 3923b19bc294SPaolo Abeni 39242303f994SPeter Krystad mptcp_subflow_init(); 39251b1c7a0eSPeter Krystad mptcp_pm_init(); 39262c5ebd00SPaolo Abeni mptcp_token_init(); 39272303f994SPeter Krystad 3928f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3929f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3930f870fa0bSMat Martineau 3931f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 39326771bfd9SFlorian Westphal 39336771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3934f870fa0bSMat Martineau } 3935f870fa0bSMat Martineau 3936f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3937e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3938e42f1ac6SFlorian Westphal .family = PF_INET6, 3939e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39400a3cc579SPaolo Abeni .release = inet6_release, 3941e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3942*786fc124SPaolo Abeni .connect = inet_stream_connect, 3943e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3944e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3945d2f77c53SPaolo Abeni .getname = inet6_getname, 3946e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3947e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3948e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3949e42f1ac6SFlorian Westphal .listen = mptcp_listen, 395076e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3951e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3952e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3953e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3954e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3955e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3956e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3957e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39583986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3959e42f1ac6SFlorian Westphal #endif 3960e42f1ac6SFlorian Westphal }; 3961e42f1ac6SFlorian Westphal 3962f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3963f870fa0bSMat Martineau 3964f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3965f870fa0bSMat Martineau .type = SOCK_STREAM, 3966f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3967f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39682303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3969f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3970f870fa0bSMat Martineau }; 3971f870fa0bSMat Martineau 3972d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3973f870fa0bSMat Martineau { 3974f870fa0bSMat Martineau int err; 3975f870fa0bSMat Martineau 3976f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3977f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3978f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 3979b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3980f870fa0bSMat Martineau 3981f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3982f870fa0bSMat Martineau if (err) 3983f870fa0bSMat Martineau return err; 3984f870fa0bSMat Martineau 3985f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3986f870fa0bSMat Martineau if (err) 3987f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3988f870fa0bSMat Martineau 3989f870fa0bSMat Martineau return err; 3990f870fa0bSMat Martineau } 3991f870fa0bSMat Martineau #endif 3992