1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25644807e3SFlorian Westphal #include <asm/ioctls.h> 26f870fa0bSMat Martineau #include "protocol.h" 27fc518953SFlorian Westphal #include "mib.h" 28f870fa0bSMat Martineau 29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 30e10a9892SGeliang Tang #include <trace/events/mptcp.h> 31e10a9892SGeliang Tang 32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33b0519de8SFlorian Westphal struct mptcp6_sock { 34b0519de8SFlorian Westphal struct mptcp_sock msk; 35b0519de8SFlorian Westphal struct ipv6_pinfo np; 36b0519de8SFlorian Westphal }; 37b0519de8SFlorian Westphal #endif 38b0519de8SFlorian Westphal 39b7f653b2SFlorian Westphal enum { 40b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 412c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 42b7f653b2SFlorian Westphal }; 43b7f653b2SFlorian Westphal 4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45d027236cSPaolo Abeni 46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 4781c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk); 48e16163b6SPaolo Abeni 49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 51b19bc294SPaolo Abeni 526f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 536f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 546f8a612aSFlorian Westphal { 557439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 566f8a612aSFlorian Westphal } 576f8a612aSFlorian Westphal 58d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 590b4f33deSFlorian Westphal { 600b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 610b4f33deSFlorian Westphal 620b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 630b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 640b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 650b4f33deSFlorian Westphal * not an mptcp one. 660b4f33deSFlorian Westphal * 670b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 680b4f33deSFlorian Westphal * bypass mptcp. 690b4f33deSFlorian Westphal */ 701ded5e5aSEric Dumazet WRITE_ONCE(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)) { 741ded5e5aSEric Dumazet WRITE_ONCE(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 93b8dc6d6cSPaolo Abeni msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio; 941b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, ssock->sk); 955b825727SPaolo Abeni WRITE_ONCE(msk->subflow, ssock); 962303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 97cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 98e16163b6SPaolo Abeni sock_hold(ssock->sk); 992303f994SPeter Krystad subflow->request_mptcp = 1; 1006f06b4d4SPaolo Abeni subflow->subflow_id = msk->subflow_id++; 1014cf86ae8SPaolo Abeni 1024cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1034cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 104866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 105e1ff9e82SDavide Caratti 106fa68018dSPaolo Abeni return 0; 1072303f994SPeter Krystad } 1082303f994SPeter Krystad 109ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow, 110ddb1a072SPaolo Abeni * eventually allocating it. 111ddb1a072SPaolo Abeni */ 112ddb1a072SPaolo Abeni struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk) 113ddb1a072SPaolo Abeni { 114ddb1a072SPaolo Abeni struct sock *sk = (struct sock *)msk; 115ddb1a072SPaolo Abeni int ret; 116ddb1a072SPaolo Abeni 117ddb1a072SPaolo Abeni if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 118ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 119ddb1a072SPaolo Abeni 120ddb1a072SPaolo Abeni if (!msk->subflow) { 121ddb1a072SPaolo Abeni if (msk->first) 122ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 123ddb1a072SPaolo Abeni 124ddb1a072SPaolo Abeni ret = __mptcp_socket_create(msk); 125ddb1a072SPaolo Abeni if (ret) 126ddb1a072SPaolo Abeni return ERR_PTR(ret); 127ddb1a072SPaolo Abeni 128ddb1a072SPaolo Abeni mptcp_sockopt_sync(msk, msk->first); 129ddb1a072SPaolo Abeni } 130ddb1a072SPaolo Abeni 131ddb1a072SPaolo Abeni return msk->subflow; 132ddb1a072SPaolo Abeni } 133ddb1a072SPaolo Abeni 134ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 135ab174ad8SPaolo Abeni { 136ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 137ab174ad8SPaolo Abeni __kfree_skb(skb); 138ab174ad8SPaolo Abeni } 139ab174ad8SPaolo Abeni 1406511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1416511882cSPaolo Abeni { 1426511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1436511882cSPaolo Abeni } 1446511882cSPaolo Abeni 1458268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1468268ed4cSPaolo Abeni struct sk_buff *from) 1478268ed4cSPaolo Abeni { 1488268ed4cSPaolo Abeni bool fragstolen; 1498268ed4cSPaolo Abeni int delta; 1508268ed4cSPaolo Abeni 1518268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1528268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1538268ed4cSPaolo Abeni return false; 1548268ed4cSPaolo Abeni 15506242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 15606242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 15706242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 158ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1597288ff6eSPaolo Abeni 1607288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1617288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1627288ff6eSPaolo Abeni * negative one 1637288ff6eSPaolo Abeni */ 1648268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1656511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1667288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1677288ff6eSPaolo Abeni 1688268ed4cSPaolo Abeni return true; 1698268ed4cSPaolo Abeni } 1708268ed4cSPaolo Abeni 171ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 172ab174ad8SPaolo Abeni struct sk_buff *from) 1736771bfd9SFlorian Westphal { 174ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 175ab174ad8SPaolo Abeni return false; 176ab174ad8SPaolo Abeni 177ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 178ab174ad8SPaolo Abeni } 179ab174ad8SPaolo Abeni 1806511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1816511882cSPaolo Abeni { 182100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 183100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 1846511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1856511882cSPaolo Abeni } 1866511882cSPaolo Abeni 1876511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1886511882cSPaolo Abeni { 1896511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1906511882cSPaolo Abeni int reclaimable; 1916511882cSPaolo Abeni 1926511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1936511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1946511882cSPaolo Abeni 1956511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 196d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 197d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1986511882cSPaolo Abeni } 1996511882cSPaolo Abeni 2006511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 2016511882cSPaolo Abeni { 2026511882cSPaolo Abeni unsigned int len = skb->truesize; 2036511882cSPaolo Abeni struct sock *sk = skb->sk; 2046511882cSPaolo Abeni 2056511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 2066511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 2076511882cSPaolo Abeni } 2086511882cSPaolo Abeni 20936b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 2106511882cSPaolo Abeni { 2116511882cSPaolo Abeni skb_orphan(skb); 2126511882cSPaolo Abeni skb->sk = sk; 2136511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2146511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2156511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2166511882cSPaolo Abeni } 2176511882cSPaolo Abeni 218ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 219ab174ad8SPaolo Abeni * - use mptcp seqs 220ab174ad8SPaolo Abeni * - don't cope with sacks 221ab174ad8SPaolo Abeni */ 222ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 223ab174ad8SPaolo Abeni { 224ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 225ab174ad8SPaolo Abeni struct rb_node **p, *parent; 226ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 227ab174ad8SPaolo Abeni struct sk_buff *skb1; 228ab174ad8SPaolo Abeni 229ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 230ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 231f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 232ab174ad8SPaolo Abeni 23306242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 23406242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 235fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 236ab174ad8SPaolo Abeni /* out of window */ 237ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 238fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 239fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 240f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 24106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 242ab174ad8SPaolo Abeni return; 243ab174ad8SPaolo Abeni } 244ab174ad8SPaolo Abeni 245ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 24606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 247ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 248ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 249ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 250ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 251ab174ad8SPaolo Abeni goto end; 252ab174ad8SPaolo Abeni } 253ab174ad8SPaolo Abeni 254ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 255ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 256ab174ad8SPaolo Abeni */ 25706242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 25806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 25906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 260ab174ad8SPaolo Abeni return; 26106242e44SPaolo Abeni } 262ab174ad8SPaolo Abeni 263ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 264ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 26506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 266ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 267ab174ad8SPaolo Abeni p = &parent->rb_right; 268ab174ad8SPaolo Abeni goto insert; 269ab174ad8SPaolo Abeni } 270ab174ad8SPaolo Abeni 271ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 272ab174ad8SPaolo Abeni parent = NULL; 273ab174ad8SPaolo Abeni while (*p) { 274ab174ad8SPaolo Abeni parent = *p; 275ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 276ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 277ab174ad8SPaolo Abeni p = &parent->rb_left; 278ab174ad8SPaolo Abeni continue; 279ab174ad8SPaolo Abeni } 280ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 281ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 282ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 283ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 28406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 285ab174ad8SPaolo Abeni return; 286ab174ad8SPaolo Abeni } 287ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 288ab174ad8SPaolo Abeni /* partial overlap: 289ab174ad8SPaolo Abeni * | skb | 290ab174ad8SPaolo Abeni * | skb1 | 291ab174ad8SPaolo Abeni * continue traversing 292ab174ad8SPaolo Abeni */ 293ab174ad8SPaolo Abeni } else { 294ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 295ab174ad8SPaolo Abeni * Replace skb1 with skb. 296ab174ad8SPaolo Abeni */ 297ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 298ab174ad8SPaolo Abeni &msk->out_of_order_queue); 299ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 30006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 301ab174ad8SPaolo Abeni goto merge_right; 302ab174ad8SPaolo Abeni } 303ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 30406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 305ab174ad8SPaolo Abeni return; 306ab174ad8SPaolo Abeni } 307ab174ad8SPaolo Abeni p = &parent->rb_right; 308ab174ad8SPaolo Abeni } 30906242e44SPaolo Abeni 310ab174ad8SPaolo Abeni insert: 311ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 312ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 313ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 314ab174ad8SPaolo Abeni 315ab174ad8SPaolo Abeni merge_right: 316ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 317ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 318ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 319ab174ad8SPaolo Abeni break; 320ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 321ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 32206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 323ab174ad8SPaolo Abeni } 324ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 325ab174ad8SPaolo Abeni if (!skb1) 326ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 327ab174ad8SPaolo Abeni 328ab174ad8SPaolo Abeni end: 329ab174ad8SPaolo Abeni skb_condense(skb); 3306511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3316511882cSPaolo Abeni } 3326511882cSPaolo Abeni 3336511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3346511882cSPaolo Abeni { 3356511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3366511882cSPaolo Abeni int amt, amount; 3376511882cSPaolo Abeni 33869d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3396511882cSPaolo Abeni return true; 3406511882cSPaolo Abeni 34169d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3426511882cSPaolo Abeni amt = sk_mem_pages(size); 343100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3444aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3456511882cSPaolo Abeni return false; 3466511882cSPaolo Abeni 3474aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3486511882cSPaolo Abeni return true; 349ab174ad8SPaolo Abeni } 350ab174ad8SPaolo Abeni 351ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 352ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 353ab174ad8SPaolo Abeni size_t copy_len) 354ab174ad8SPaolo Abeni { 355ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3566771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3574e637c70SFlorian Westphal struct sk_buff *tail; 358b7f653b2SFlorian Westphal bool has_rxtstamp; 3596771bfd9SFlorian Westphal 3606771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3614e637c70SFlorian Westphal 3624e637c70SFlorian Westphal skb_ext_reset(skb); 3634e637c70SFlorian Westphal skb_orphan(skb); 3644e637c70SFlorian Westphal 3659c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3666511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3679c3f94e1SPaolo Abeni goto drop; 36872f96132SPaolo Abeni 369b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 370b7f653b2SFlorian Westphal 371ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 372ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 373ab174ad8SPaolo Abeni * value 374ab174ad8SPaolo Abeni */ 375ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 376ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 377ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 378b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 379ab174ad8SPaolo Abeni 380ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 381ab174ad8SPaolo Abeni /* in sequence */ 38238967f42SPaolo Abeni msk->bytes_received += copy_len; 383917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3844e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3858268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 386ab174ad8SPaolo Abeni return true; 3874e637c70SFlorian Westphal 3886511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3896771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 390ab174ad8SPaolo Abeni return true; 391ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 392ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 393ab174ad8SPaolo Abeni return false; 394ab174ad8SPaolo Abeni } 395ab174ad8SPaolo Abeni 396ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 397ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 398ab174ad8SPaolo Abeni */ 39906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 4009c3f94e1SPaolo Abeni drop: 401ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 402ab174ad8SPaolo Abeni return false; 4036771bfd9SFlorian Westphal } 4046771bfd9SFlorian Westphal 40516a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 40616a9a9daSMat Martineau { 40716a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 40816a9a9daSMat Martineau 40916a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 41016a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 41116a9a9daSMat Martineau } 41216a9a9daSMat Martineau 413e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 414e16163b6SPaolo Abeni { 415e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 416e16163b6SPaolo Abeni return; 417e16163b6SPaolo Abeni 418e16163b6SPaolo Abeni sk->sk_state_change(sk); 419e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 420e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 421e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 422e16163b6SPaolo Abeni else 423e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 424e16163b6SPaolo Abeni } 425e16163b6SPaolo Abeni 4266e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4276e628cd3SPaolo Abeni { 4286e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4296e628cd3SPaolo Abeni 43081c1d029SPaolo Abeni return ((1 << sk->sk_state) & 4316e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4326e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4336e628cd3SPaolo Abeni } 4346e628cd3SPaolo Abeni 43516a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 43616a9a9daSMat Martineau { 43716a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 43816a9a9daSMat Martineau 43916a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4406e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 44116a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 44216a9a9daSMat Martineau 44316a9a9daSMat Martineau switch (sk->sk_state) { 44416a9a9daSMat Martineau case TCP_FIN_WAIT1: 44516a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 44616a9a9daSMat Martineau break; 44716a9a9daSMat Martineau case TCP_CLOSING: 44816a9a9daSMat Martineau case TCP_LAST_ACK: 44916a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 45016a9a9daSMat Martineau break; 45116a9a9daSMat Martineau } 45216a9a9daSMat Martineau 453e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 45416a9a9daSMat Martineau } 45516a9a9daSMat Martineau } 45616a9a9daSMat Martineau 4573721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4583721b9b6SMat Martineau { 4593721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4603721b9b6SMat Martineau 4613721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4623721b9b6SMat Martineau ((1 << sk->sk_state) & 4633721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4643721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4653721b9b6SMat Martineau 4663721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4673721b9b6SMat Martineau if (seq) 4683721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4693721b9b6SMat Martineau 4703721b9b6SMat Martineau return true; 4713721b9b6SMat Martineau } 4723721b9b6SMat Martineau } 4733721b9b6SMat Martineau 4743721b9b6SMat Martineau return false; 4753721b9b6SMat Martineau } 4763721b9b6SMat Martineau 477403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk) 4786477dd39SMat Martineau { 4796477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 480877d11f0SMat Martineau u32 retransmits; 4816477dd39SMat Martineau 482877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 483877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 484877d11f0SMat Martineau 485877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4866477dd39SMat Martineau } 4876477dd39SMat Martineau 48833d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4893721b9b6SMat Martineau { 4903721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4913721b9b6SMat Martineau } 4923721b9b6SMat Martineau 49333d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 49433d41c9cSPaolo Abeni { 49533d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 49633d41c9cSPaolo Abeni 49771b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 49871b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 49933d41c9cSPaolo Abeni } 50033d41c9cSPaolo Abeni 50133d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 50233d41c9cSPaolo Abeni { 50333d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 50433d41c9cSPaolo Abeni long tout = 0; 50533d41c9cSPaolo Abeni 50633d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 50733d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 50833d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 50933d41c9cSPaolo Abeni } 51033d41c9cSPaolo Abeni 51176a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 512fd897679SPaolo Abeni { 513fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 51420bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 515fd897679SPaolo Abeni } 516fd897679SPaolo Abeni 517a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 518a6574302SMat Martineau { 519a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 520a6574302SMat Martineau tcp_send_ack(ssk); 521a6574302SMat Martineau } 522a6574302SMat Martineau 523f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5247ed90803SPaolo Abeni { 52575e908c3SPaolo Abeni bool slow; 5267ed90803SPaolo Abeni 52775e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 528a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 52975e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 530fd897679SPaolo Abeni } 531340fa666SMat Martineau 532340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 533340fa666SMat Martineau { 534340fa666SMat Martineau struct mptcp_subflow_context *subflow; 535340fa666SMat Martineau 536340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 537340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 538ea4ca586SPaolo Abeni } 539ea4ca586SPaolo Abeni 540fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 541fd897679SPaolo Abeni { 54275e908c3SPaolo Abeni bool slow; 543fd897679SPaolo Abeni 54475e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 545fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 546fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 54775e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 548fde56eeaSPaolo Abeni } 549fde56eeaSPaolo Abeni 550fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 551fde56eeaSPaolo Abeni { 552fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 553c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 554fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 555fde56eeaSPaolo Abeni 556fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 557fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 558fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 559fde56eeaSPaolo Abeni (rx_empty && ack_pending & 560fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 561ea4ca586SPaolo Abeni } 562fd897679SPaolo Abeni 563fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 564fd897679SPaolo Abeni { 565e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 566fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 567e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 568fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 569fde56eeaSPaolo Abeni bool cleanup, rx_empty; 570e3859603SPaolo Abeni 571fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 572ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 573fd897679SPaolo Abeni 574fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 575fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 576fd897679SPaolo Abeni 577fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 578fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5797ed90803SPaolo Abeni } 5807ed90803SPaolo Abeni } 5817ed90803SPaolo Abeni 5827ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5833721b9b6SMat Martineau { 5843721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5853721b9b6SMat Martineau u64 rcv_data_fin_seq; 5867ed90803SPaolo Abeni bool ret = false; 5873721b9b6SMat Martineau 5883721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5893721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5903721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5913721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5923721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5933721b9b6SMat Martineau * the msk state. 5943721b9b6SMat Martineau * 5953721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5963721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5973721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5983721b9b6SMat Martineau * when catching up. 5993721b9b6SMat Martineau */ 6003721b9b6SMat Martineau 6013721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 602917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 6033721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 6043721b9b6SMat Martineau 6056b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 60616a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 6073721b9b6SMat Martineau 6083721b9b6SMat Martineau switch (sk->sk_state) { 6093721b9b6SMat Martineau case TCP_ESTABLISHED: 6103721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6113721b9b6SMat Martineau break; 6123721b9b6SMat Martineau case TCP_FIN_WAIT1: 6133721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6143721b9b6SMat Martineau break; 6153721b9b6SMat Martineau case TCP_FIN_WAIT2: 6163721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6173721b9b6SMat Martineau break; 6183721b9b6SMat Martineau default: 6193721b9b6SMat Martineau /* Other states not expected */ 6203721b9b6SMat Martineau WARN_ON_ONCE(1); 6213721b9b6SMat Martineau break; 6223721b9b6SMat Martineau } 6233721b9b6SMat Martineau 6247ed90803SPaolo Abeni ret = true; 62581c1d029SPaolo Abeni if (!__mptcp_check_fallback(msk)) 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 } 76338967f42SPaolo Abeni msk->bytes_received += end_seq - msk->ack_seq; 764ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 765ab174ad8SPaolo Abeni moved = true; 766ab174ad8SPaolo Abeni } 767ab174ad8SPaolo Abeni return moved; 768ab174ad8SPaolo Abeni } 769ab174ad8SPaolo Abeni 7702e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7712e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7722e52213cSFlorian Westphal */ 77399d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7742e52213cSFlorian Westphal { 7752e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7762e52213cSFlorian Westphal unsigned int moved = 0; 7772e52213cSFlorian Westphal 7782e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 77987952603SPaolo Abeni __mptcp_ofo_queue(msk); 780499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 781499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 782499ada50SPaolo Abeni __mptcp_error_report(sk); 783499ada50SPaolo Abeni else 784e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 785499ada50SPaolo Abeni } 786ab174ad8SPaolo Abeni 787ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 788ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 789ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 790ab174ad8SPaolo Abeni * do it. 791ab174ad8SPaolo Abeni */ 792ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 793ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 79499d1055cSPaolo Abeni return moved > 0; 7952e52213cSFlorian Westphal } 7962e52213cSFlorian Westphal 7972e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 798101f6f85SFlorian Westphal { 7996719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 800101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 80113c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 802101f6f85SFlorian Westphal 803d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 804d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 805d7b1bfd0SPaolo Abeni * more data to the msk receive queue 806d7b1bfd0SPaolo Abeni */ 807d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 808d7b1bfd0SPaolo Abeni return; 809d7b1bfd0SPaolo Abeni 81013c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 81113c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 81213c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 81313c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 81413c7ba0cSFlorian Westphal 81599d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 816ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 817ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 81899d1055cSPaolo Abeni return; 819ce599c51SPaolo Abeni } 8202e52213cSFlorian Westphal 82199d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 82299d1055cSPaolo Abeni mptcp_data_lock(sk); 823612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 824101f6f85SFlorian Westphal sk->sk_data_ready(sk); 825612f71d7SPaolo Abeni 82699d1055cSPaolo Abeni mptcp_data_unlock(sk); 82799d1055cSPaolo Abeni } 828101f6f85SFlorian Westphal 82955b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 83055b47ca7SPaolo Abeni { 83155b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 83255b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 83355b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 83455b47ca7SPaolo Abeni } 83555b47ca7SPaolo Abeni 8363e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 837ec3edaa7SPeter Krystad { 8383e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8395cf92bbaSPaolo Abeni 8403e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 84178962489SFlorian Westphal return false; 842ec3edaa7SPeter Krystad 8433e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8443e501490SPaolo Abeni * at close time 8453e501490SPaolo Abeni */ 8463e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8473e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 84878962489SFlorian Westphal 8496f06b4d4SPaolo Abeni mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; 8503e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 85155b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 8523e501490SPaolo Abeni return true; 85378962489SFlorian Westphal } 85478962489SFlorian Westphal 85556a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 85678962489SFlorian Westphal { 8573e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8583e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 85978962489SFlorian Westphal 86056a666c4SPaolo Abeni list_for_each_entry_safe(subflow, tmp, join_list, node) { 8613e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8623e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8633e501490SPaolo Abeni 8643e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8653e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8663e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8673e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 86878962489SFlorian Westphal } 869ec3edaa7SPeter Krystad } 870ec3edaa7SPeter Krystad 871b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 872b51f9b80SPaolo Abeni { 873b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 874b51f9b80SPaolo Abeni } 875b51f9b80SPaolo Abeni 876b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 877b51f9b80SPaolo Abeni { 878b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 879b51f9b80SPaolo Abeni unsigned long tout; 880b51f9b80SPaolo Abeni 881e16163b6SPaolo Abeni /* prevent rescheduling on close */ 882e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 883e16163b6SPaolo Abeni return; 884e16163b6SPaolo Abeni 88533d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 886b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 887b51f9b80SPaolo Abeni } 888b51f9b80SPaolo Abeni 889ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 890ba8f48f7SPaolo Abeni { 891ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 892ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 893ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 894ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 895ba8f48f7SPaolo Abeni */ 896ba8f48f7SPaolo Abeni sock_hold(sk); 897ba8f48f7SPaolo Abeni return true; 898ba8f48f7SPaolo Abeni } 899ba8f48f7SPaolo Abeni return false; 900ba8f48f7SPaolo Abeni } 901ba8f48f7SPaolo Abeni 9027a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9037a6a6cbcSPaolo Abeni { 9047a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9057a6a6cbcSPaolo Abeni 906109cdeb8SGeliang Tang msk_owned_by_me(msk); 9077a6a6cbcSPaolo Abeni 9087a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 90999d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9107a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9117a6a6cbcSPaolo Abeni } 9127a6a6cbcSPaolo Abeni 9137a6a6cbcSPaolo Abeni return NULL; 9147a6a6cbcSPaolo Abeni } 9157a6a6cbcSPaolo Abeni 9163f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 91757040755SPaolo Abeni const struct sk_buff *skb, 91857040755SPaolo Abeni const struct mptcp_ext *mpext) 9196d0060f6SMat Martineau { 92057040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 92157040755SPaolo Abeni return false; 92257040755SPaolo Abeni 9235a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9245a369ca6SPaolo Abeni * mapping has not been xmitted yet 9255a369ca6SPaolo Abeni */ 9265a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9275a369ca6SPaolo Abeni !mpext->frozen; 92857040755SPaolo Abeni } 92957040755SPaolo Abeni 93029249eacSPaolo Abeni /* we can append data to the given data frag if: 93129249eacSPaolo Abeni * - there is space available in the backing page_frag 93229249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 93329249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 93429249eacSPaolo Abeni */ 93518b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 93618b683bfSPaolo Abeni const struct page_frag *pfrag, 93718b683bfSPaolo Abeni const struct mptcp_data_frag *df) 93818b683bfSPaolo Abeni { 93918b683bfSPaolo Abeni return df && pfrag->page == df->page && 940d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 94129249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 94218b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 94318b683bfSPaolo Abeni } 94418b683bfSPaolo Abeni 945d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 94618b683bfSPaolo Abeni { 947d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9487948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 949d027236cSPaolo Abeni } 950d027236cSPaolo Abeni 951d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 952d027236cSPaolo Abeni { 953d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 954d027236cSPaolo Abeni 95518b683bfSPaolo Abeni list_del(&dfrag->list); 956d027236cSPaolo Abeni dfrag_uncharge(sk, len); 95718b683bfSPaolo Abeni put_page(dfrag->page); 95818b683bfSPaolo Abeni } 95918b683bfSPaolo Abeni 9606e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 96118b683bfSPaolo Abeni { 96218b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 96318b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 964e1ff9e82SDavide Caratti u64 snd_una; 965e1ff9e82SDavide Caratti 9667439d687SPaolo Abeni snd_una = msk->snd_una; 96718b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 96818b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 96918b683bfSPaolo Abeni break; 97018b683bfSPaolo Abeni 9711e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 9721e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 9731e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 974d9ca1de8SPaolo Abeni break; 9751e1d9d6fSPaolo Abeni 9761e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 9771e1d9d6fSPaolo Abeni } 9781e1d9d6fSPaolo Abeni 979d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 980d027236cSPaolo Abeni } 981d027236cSPaolo Abeni 9827948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 9837948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 98453eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 98553eb4c38SPaolo Abeni 9861e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 9871e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 9881e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 98953eb4c38SPaolo Abeni goto out; 9901e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 9911e1d9d6fSPaolo Abeni goto out; 9921e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 9931e1d9d6fSPaolo Abeni } 9947948f6ccSFlorian Westphal 9957948f6ccSFlorian Westphal dfrag->data_seq += delta; 99653eb4c38SPaolo Abeni dfrag->offset += delta; 9977948f6ccSFlorian Westphal dfrag->data_len -= delta; 998d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 9997948f6ccSFlorian Westphal 10007948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10017948f6ccSFlorian Westphal } 10027948f6ccSFlorian Westphal 10031e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10041e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10051e1d9d6fSPaolo Abeni msk->recovery = false; 10061e1d9d6fSPaolo Abeni 100753eb4c38SPaolo Abeni out: 10083241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10093241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 101033d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10116e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10126e628cd3SPaolo Abeni } else { 10136e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10146e628cd3SPaolo Abeni } 10156e628cd3SPaolo Abeni } 10166e628cd3SPaolo Abeni 1017417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1018417789dfSPaolo Abeni { 1019765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1020765ff425SPaolo Abeni 1021417789dfSPaolo Abeni __mptcp_clean_una(sk); 1022417789dfSPaolo Abeni mptcp_write_space(sk); 1023417789dfSPaolo Abeni } 1024417789dfSPaolo Abeni 1025b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1026b5941f06SPaolo Abeni { 1027b5941f06SPaolo Abeni mptcp_data_lock(sk); 1028b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1029b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1030b5941f06SPaolo Abeni } 1031b5941f06SPaolo Abeni 1032724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 103318b683bfSPaolo Abeni { 1034d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1035d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1036d9ca1de8SPaolo Abeni bool first = true; 1037d9ca1de8SPaolo Abeni 103818b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1039d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1040d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1041d9ca1de8SPaolo Abeni 1042d9ca1de8SPaolo Abeni if (first) 1043d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1044d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1045d9ca1de8SPaolo Abeni first = false; 1046d9ca1de8SPaolo Abeni } 1047724cfd2eSPaolo Abeni } 1048724cfd2eSPaolo Abeni 1049724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1050724cfd2eSPaolo Abeni * data 1051724cfd2eSPaolo Abeni */ 1052724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1053724cfd2eSPaolo Abeni { 1054724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1055724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1056724cfd2eSPaolo Abeni return true; 1057724cfd2eSPaolo Abeni 1058724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 105918b683bfSPaolo Abeni return false; 106018b683bfSPaolo Abeni } 106118b683bfSPaolo Abeni 106218b683bfSPaolo Abeni static struct mptcp_data_frag * 106318b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 106418b683bfSPaolo Abeni int orig_offset) 106518b683bfSPaolo Abeni { 106618b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 106718b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 106818b683bfSPaolo Abeni 106918b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 107018b683bfSPaolo Abeni dfrag->data_len = 0; 107118b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 107218b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 107318b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1074d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 107518b683bfSPaolo Abeni dfrag->page = pfrag->page; 107618b683bfSPaolo Abeni 107718b683bfSPaolo Abeni return dfrag; 107818b683bfSPaolo Abeni } 107918b683bfSPaolo Abeni 1080caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1081caf971dfSPaolo Abeni int mss_now; 1082caf971dfSPaolo Abeni int size_goal; 1083d9ca1de8SPaolo Abeni u16 limit; 1084d9ca1de8SPaolo Abeni u16 sent; 1085d9ca1de8SPaolo Abeni unsigned int flags; 10861094c6feSPaolo Abeni bool data_lock_held; 1087caf971dfSPaolo Abeni }; 1088caf971dfSPaolo Abeni 1089b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1090b713d006SPaolo Abeni u64 data_seq, int avail_size) 10916f8a612aSFlorian Westphal { 10926f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1093b713d006SPaolo Abeni u64 mptcp_snd_wnd; 10946f8a612aSFlorian Westphal 10956f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 10966f8a612aSFlorian Westphal return avail_size; 10976f8a612aSFlorian Westphal 1098b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1099b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11006f8a612aSFlorian Westphal 110192be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1102b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 110392be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 110492be2f52SPaolo Abeni } 11056f8a612aSFlorian Westphal 11066f8a612aSFlorian Westphal return avail_size; 11076f8a612aSFlorian Westphal } 11086f8a612aSFlorian Westphal 1109724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1110724cfd2eSPaolo Abeni { 1111724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1112724cfd2eSPaolo Abeni 1113724cfd2eSPaolo Abeni if (!mpext) 1114724cfd2eSPaolo Abeni return false; 1115724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1116724cfd2eSPaolo Abeni return true; 1117724cfd2eSPaolo Abeni } 1118724cfd2eSPaolo Abeni 11196e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1120724cfd2eSPaolo Abeni { 1121724cfd2eSPaolo Abeni struct sk_buff *skb; 1122724cfd2eSPaolo Abeni 11236e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1124724cfd2eSPaolo Abeni if (likely(skb)) { 11256e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1126724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1127a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1128f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1129724cfd2eSPaolo Abeni return skb; 1130724cfd2eSPaolo Abeni } 1131724cfd2eSPaolo Abeni __kfree_skb(skb); 1132724cfd2eSPaolo Abeni } else { 1133724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1134724cfd2eSPaolo Abeni } 1135724cfd2eSPaolo Abeni return NULL; 1136724cfd2eSPaolo Abeni } 1137724cfd2eSPaolo Abeni 1138f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1139724cfd2eSPaolo Abeni { 1140724cfd2eSPaolo Abeni struct sk_buff *skb; 1141724cfd2eSPaolo Abeni 11426e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1143724cfd2eSPaolo Abeni if (!skb) 1144f70cad10SPaolo Abeni return NULL; 1145724cfd2eSPaolo Abeni 1146724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1147f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1148f70cad10SPaolo Abeni return skb; 1149724cfd2eSPaolo Abeni } 11503ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1151724cfd2eSPaolo Abeni kfree_skb(skb); 1152f70cad10SPaolo Abeni return NULL; 1153724cfd2eSPaolo Abeni } 1154724cfd2eSPaolo Abeni 1155f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1156724cfd2eSPaolo Abeni { 11571094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1158724cfd2eSPaolo Abeni 11591094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1160724cfd2eSPaolo Abeni } 1161724cfd2eSPaolo Abeni 1162d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1163d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1164d0cc2987SGeliang Tang */ 1165d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1166d0cc2987SGeliang Tang { 1167d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1168d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1169d0cc2987SGeliang Tang int offset = skb->len - added; 1170d0cc2987SGeliang Tang 1171d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1172d0cc2987SGeliang Tang } 1173d0cc2987SGeliang Tang 11741e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 11751e39e5a3SGeliang Tang struct sock *ssk, 11761e39e5a3SGeliang Tang struct mptcp_ext *mpext) 11771e39e5a3SGeliang Tang { 11781e39e5a3SGeliang Tang if (!mpext) 11791e39e5a3SGeliang Tang return; 11801e39e5a3SGeliang Tang 11811e39e5a3SGeliang Tang mpext->infinite_map = 1; 11821e39e5a3SGeliang Tang mpext->data_len = 0; 11831e39e5a3SGeliang Tang 1184104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 11851e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 11861e39e5a3SGeliang Tang pr_fallback(msk); 1187d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 11881e39e5a3SGeliang Tang } 11891e39e5a3SGeliang Tang 119057040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1191d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1192caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 119357040755SPaolo Abeni { 1194d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1195f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 11966d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 11976f8a612aSFlorian Westphal bool zero_window_probe = false; 11986d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1199f70cad10SPaolo Abeni bool can_coalesce = false; 1200f70cad10SPaolo Abeni bool reuse_skb = true; 1201f70cad10SPaolo Abeni struct sk_buff *skb; 1202f70cad10SPaolo Abeni size_t copy; 1203f70cad10SPaolo Abeni int i; 12046d0060f6SMat Martineau 1205e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1206d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12076d0060f6SMat Martineau 1208f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1209f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1210f70cad10SPaolo Abeni return 0; 1211f70cad10SPaolo Abeni 1212c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1213c886d702SPaolo Abeni return -EAGAIN; 1214c886d702SPaolo Abeni 1215d9ca1de8SPaolo Abeni /* compute send limit */ 1216d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1217f70cad10SPaolo Abeni copy = info->size_goal; 1218f70cad10SPaolo Abeni 121957040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1220f70cad10SPaolo Abeni if (skb && copy > skb->len) { 122157040755SPaolo Abeni /* Limit the write to the size available in the 122257040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 122357040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 122457040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 122557040755SPaolo Abeni * SSN association set here 122657040755SPaolo Abeni */ 1227d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12281094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 122957040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12301094c6feSPaolo Abeni goto alloc_skb; 12311094c6feSPaolo Abeni } 12321094c6feSPaolo Abeni 1233f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1234f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1235657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1236f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1237f70cad10SPaolo Abeni goto alloc_skb; 123815e6ca97SPaolo Abeni } 123918b683bfSPaolo Abeni 1240f70cad10SPaolo Abeni copy -= skb->len; 1241f70cad10SPaolo Abeni } else { 12421094c6feSPaolo Abeni alloc_skb: 1243f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1244f70cad10SPaolo Abeni if (!skb) 1245f70cad10SPaolo Abeni return -ENOMEM; 1246f70cad10SPaolo Abeni 1247f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1248f70cad10SPaolo Abeni reuse_skb = false; 1249f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1250f70cad10SPaolo Abeni } 12511094c6feSPaolo Abeni 12526f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1253b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1254f70cad10SPaolo Abeni if (copy == 0) { 12557439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12567439d687SPaolo Abeni 1257f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 125827728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12596f8a612aSFlorian Westphal return 0; 12606f8a612aSFlorian Westphal } 12616f8a612aSFlorian Westphal 1262f70cad10SPaolo Abeni zero_window_probe = true; 1263f70cad10SPaolo Abeni data_seq = snd_una - 1; 1264f70cad10SPaolo Abeni copy = 1; 126557040755SPaolo Abeni 1266f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1267f70cad10SPaolo Abeni * ssk write queue 1268f70cad10SPaolo Abeni */ 1269f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1270f70cad10SPaolo Abeni } 1271f70cad10SPaolo Abeni 1272f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1273f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 127427728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1275e2223995SPaolo Abeni return -ENOMEM; 127635759383SFlorian Westphal } 127718b683bfSPaolo Abeni 1278f70cad10SPaolo Abeni if (can_coalesce) { 1279f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1280f70cad10SPaolo Abeni } else { 1281f70cad10SPaolo Abeni get_page(dfrag->page); 1282f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 128357040755SPaolo Abeni } 128457040755SPaolo Abeni 1285f70cad10SPaolo Abeni skb->len += copy; 1286f70cad10SPaolo Abeni skb->data_len += copy; 1287f70cad10SPaolo Abeni skb->truesize += copy; 1288f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1289f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1290f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1291f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1292f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1293f70cad10SPaolo Abeni 1294f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1295f70cad10SPaolo Abeni if (reuse_skb) { 1296f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1297f70cad10SPaolo Abeni mpext->data_len += copy; 1298f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1299f70cad10SPaolo Abeni goto out; 1300724cfd2eSPaolo Abeni } 13016d0060f6SMat Martineau 13026d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1303d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13046d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1305f70cad10SPaolo Abeni mpext->data_len = copy; 13066d0060f6SMat Martineau mpext->use_map = 1; 13076d0060f6SMat Martineau mpext->dsn64 = 1; 13086d0060f6SMat Martineau 13096d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13106d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13116d0060f6SMat Martineau mpext->dsn64); 13126d0060f6SMat Martineau 13136f8a612aSFlorian Westphal if (zero_window_probe) { 1314f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13156f8a612aSFlorian Westphal mpext->frozen = 1; 1316d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1317f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13186f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1319d0cc2987SGeliang Tang return 0; 13206f8a612aSFlorian Westphal } 132157040755SPaolo Abeni out: 1322d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1323f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13241e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13251e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13260eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1327f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1328f70cad10SPaolo Abeni return copy; 13296d0060f6SMat Martineau } 13306d0060f6SMat Martineau 1331d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1332d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1333d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1334d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1335d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1336d5f49190SPaolo Abeni 1337d5f49190SPaolo Abeni struct subflow_send_info { 1338d5f49190SPaolo Abeni struct sock *ssk; 13393ce0852cSPaolo Abeni u64 linger_time; 1340d5f49190SPaolo Abeni }; 1341d5f49190SPaolo Abeni 1342ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1343ff5a0b42SPaolo Abeni { 1344ff5a0b42SPaolo Abeni if (!subflow->stale) 1345ff5a0b42SPaolo Abeni return; 1346ff5a0b42SPaolo Abeni 1347ff5a0b42SPaolo Abeni subflow->stale = 0; 1348fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1349ff5a0b42SPaolo Abeni } 1350ff5a0b42SPaolo Abeni 1351ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1352ff5a0b42SPaolo Abeni { 1353ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1354ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1355ff5a0b42SPaolo Abeni 1356ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1357ff5a0b42SPaolo Abeni return false; 1358ff5a0b42SPaolo Abeni 1359ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1360ff5a0b42SPaolo Abeni } 1361ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1362ff5a0b42SPaolo Abeni } 1363ff5a0b42SPaolo Abeni 13643ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13653ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13663ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13673ce0852cSPaolo Abeni 136833d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 136933d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 137033d41c9cSPaolo Abeni * additionally updates the rtx timeout 137133d41c9cSPaolo Abeni */ 13725cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1373f296234cSPeter Krystad { 13743ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1375f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 137633d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 13773ce0852cSPaolo Abeni u32 pace, burst, wmem; 1378d5f49190SPaolo Abeni int i, nr_active = 0; 1379d5f49190SPaolo Abeni struct sock *ssk; 13803ce0852cSPaolo Abeni u64 linger_time; 138133d41c9cSPaolo Abeni long tout = 0; 1382f296234cSPeter Krystad 1383109cdeb8SGeliang Tang msk_owned_by_me(msk); 1384f296234cSPeter Krystad 1385d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1386d5f49190SPaolo Abeni if (!msk->first) 1387f296234cSPeter Krystad return NULL; 1388c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1389c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1390f296234cSPeter Krystad } 1391f296234cSPeter Krystad 1392d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1393d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1394d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 139533d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 139633d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1397d5f49190SPaolo Abeni return msk->last_snd; 139833d41c9cSPaolo Abeni } 1399f296234cSPeter Krystad 1400d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14013ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1402d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14033ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1404d5f49190SPaolo Abeni } 14053ce0852cSPaolo Abeni 1406d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1407e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1408d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1409d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1410f296234cSPeter Krystad continue; 1411d5f49190SPaolo Abeni 141233d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1413d5f49190SPaolo Abeni nr_active += !subflow->backup; 14143ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14153ce0852cSPaolo Abeni if (unlikely(!pace)) { 14163ce0852cSPaolo Abeni /* init pacing rate from socket */ 14173ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14183ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1419d5f49190SPaolo Abeni if (!pace) 1420d5f49190SPaolo Abeni continue; 14213ce0852cSPaolo Abeni } 1422d5f49190SPaolo Abeni 14233ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14243ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1425d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14263ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1427d5f49190SPaolo Abeni } 1428f296234cSPeter Krystad } 142933d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1430f296234cSPeter Krystad 1431d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1432d5f49190SPaolo Abeni if (!nr_active) 14333ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1434d5f49190SPaolo Abeni 14353ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14363ce0852cSPaolo Abeni * faster flow, we need to: 14373ce0852cSPaolo Abeni * - estimate the faster flow linger time 14383ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14393ce0852cSPaolo Abeni * by the faster flow 14403ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14413ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14423ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14433ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14443ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14453ce0852cSPaolo Abeni */ 14463ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1447b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1448d5f49190SPaolo Abeni return NULL; 14493ce0852cSPaolo Abeni 1450b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14513ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1452b713d006SPaolo Abeni if (!burst) { 1453b713d006SPaolo Abeni msk->last_snd = NULL; 1454b713d006SPaolo Abeni return ssk; 1455b713d006SPaolo Abeni } 1456b713d006SPaolo Abeni 14573ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14583ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14593ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14603ce0852cSPaolo Abeni burst + wmem); 14613ce0852cSPaolo Abeni msk->last_snd = ssk; 14623ce0852cSPaolo Abeni msk->snd_burst = burst; 14633ce0852cSPaolo Abeni return ssk; 1464f296234cSPeter Krystad } 1465f296234cSPeter Krystad 1466b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1467d9ca1de8SPaolo Abeni { 1468d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1469d9ca1de8SPaolo Abeni release_sock(ssk); 1470d9ca1de8SPaolo Abeni } 1471d9ca1de8SPaolo Abeni 14720d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14730d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14740d199e43SFlorian Westphal u32 sent) 14750d199e43SFlorian Westphal { 14760d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 14770d199e43SFlorian Westphal 14780d199e43SFlorian Westphal dfrag->already_sent += sent; 14790d199e43SFlorian Westphal 14800d199e43SFlorian Westphal msk->snd_burst -= sent; 14810d199e43SFlorian Westphal 14820d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 14830d199e43SFlorian Westphal 14840d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 14850d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 14860d199e43SFlorian Westphal * old segments. 14870d199e43SFlorian Westphal * 14880d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 14890d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 14900d199e43SFlorian Westphal * that has been handed to the subflow for transmission 14910d199e43SFlorian Westphal * and skip update in case it was old dfrag. 14920d199e43SFlorian Westphal */ 149338967f42SPaolo Abeni if (likely(after64(snd_nxt_new, msk->snd_nxt))) { 149438967f42SPaolo Abeni msk->bytes_sent += snd_nxt_new - msk->snd_nxt; 14950d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 14960d199e43SFlorian Westphal } 149738967f42SPaolo Abeni } 14980d199e43SFlorian Westphal 14998b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15003241a9c0SFlorian Westphal { 1501e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1502e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15033241a9c0SFlorian Westphal } 15043241a9c0SFlorian Westphal 1505ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1506d9ca1de8SPaolo Abeni { 1507d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1508d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1509d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1510d9ca1de8SPaolo Abeni .flags = flags, 1511d9ca1de8SPaolo Abeni }; 15120522b424SGeliang Tang bool do_check_data_fin = false; 1513d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15140522b424SGeliang Tang int len; 1515d9ca1de8SPaolo Abeni 1516d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1517d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1518d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1519d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1520d9ca1de8SPaolo Abeni while (len > 0) { 1521d9ca1de8SPaolo Abeni int ret = 0; 1522d9ca1de8SPaolo Abeni 1523d9ca1de8SPaolo Abeni prev_ssk = ssk; 15245cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1525d9ca1de8SPaolo Abeni 15269758f40eSPaolo Abeni /* First check. If the ssk has changed since 15279758f40eSPaolo Abeni * the last round, release prev_ssk 1528d9ca1de8SPaolo Abeni */ 1529d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1530b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1531d9ca1de8SPaolo Abeni if (!ssk) 1532d9ca1de8SPaolo Abeni goto out; 1533d9ca1de8SPaolo Abeni 15349758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15359758f40eSPaolo Abeni * from the previous one, otherwise we are still 15369758f40eSPaolo Abeni * helding the relevant lock 15379758f40eSPaolo Abeni */ 15389758f40eSPaolo Abeni if (ssk != prev_ssk) 1539d9ca1de8SPaolo Abeni lock_sock(ssk); 1540d9ca1de8SPaolo Abeni 1541d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1542d9ca1de8SPaolo Abeni if (ret <= 0) { 1543c886d702SPaolo Abeni if (ret == -EAGAIN) 1544c886d702SPaolo Abeni continue; 1545b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1546d9ca1de8SPaolo Abeni goto out; 1547d9ca1de8SPaolo Abeni } 1548d9ca1de8SPaolo Abeni 15490522b424SGeliang Tang do_check_data_fin = true; 1550d9ca1de8SPaolo Abeni info.sent += ret; 1551d9ca1de8SPaolo Abeni len -= ret; 15520d199e43SFlorian Westphal 15530d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1554d9ca1de8SPaolo Abeni } 1555d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1556d9ca1de8SPaolo Abeni } 1557d9ca1de8SPaolo Abeni 1558d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1559d9ca1de8SPaolo Abeni if (ssk) 1560b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1561d9ca1de8SPaolo Abeni 1562d9ca1de8SPaolo Abeni out: 156333d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1564d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1565d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 15660522b424SGeliang Tang if (do_check_data_fin) 156781c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 1568d9ca1de8SPaolo Abeni } 1569d9ca1de8SPaolo Abeni 157073a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 15716e628cd3SPaolo Abeni { 15726e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15731094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 15741094c6feSPaolo Abeni .data_lock_held = true, 15751094c6feSPaolo Abeni }; 15766e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1577b19bc294SPaolo Abeni struct sock *xmit_ssk; 15786e628cd3SPaolo Abeni int len, copied = 0; 15796e628cd3SPaolo Abeni 15806e628cd3SPaolo Abeni info.flags = 0; 15816e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 15826e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 15836e628cd3SPaolo Abeni info.limit = dfrag->data_len; 15846e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 15856e628cd3SPaolo Abeni while (len > 0) { 15866e628cd3SPaolo Abeni int ret = 0; 15876e628cd3SPaolo Abeni 158873a0052aSGeliang Tang /* check for a different subflow usage only after 1589b19bc294SPaolo Abeni * spooling the first chunk of data 1590b19bc294SPaolo Abeni */ 159100df24f1SGeliang Tang xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1592b19bc294SPaolo Abeni if (!xmit_ssk) 1593b19bc294SPaolo Abeni goto out; 1594b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1595bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1596bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1597b19bc294SPaolo Abeni goto out; 1598b19bc294SPaolo Abeni } 1599b19bc294SPaolo Abeni 16006e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16016e628cd3SPaolo Abeni if (ret <= 0) 16026e628cd3SPaolo Abeni goto out; 16036e628cd3SPaolo Abeni 16046e628cd3SPaolo Abeni info.sent += ret; 16056e628cd3SPaolo Abeni copied += ret; 16066e628cd3SPaolo Abeni len -= ret; 1607b19bc294SPaolo Abeni first = false; 16080d199e43SFlorian Westphal 16090d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16106e628cd3SPaolo Abeni } 16116e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16126e628cd3SPaolo Abeni } 16136e628cd3SPaolo Abeni 16146e628cd3SPaolo Abeni out: 16156e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16166e628cd3SPaolo Abeni * not going to flush it via release_sock() 16176e628cd3SPaolo Abeni */ 16186e628cd3SPaolo Abeni if (copied) { 16196e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16206e628cd3SPaolo Abeni info.size_goal); 1621d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1622d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1623d09d818eSPaolo Abeni 16246e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16256e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16266e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16276e628cd3SPaolo Abeni } 16286e628cd3SPaolo Abeni } 16296e628cd3SPaolo Abeni 16305cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16315cf92bbaSPaolo Abeni { 16325cf92bbaSPaolo Abeni /* enable autotune */ 16335cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16345cf92bbaSPaolo Abeni 16355cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16365cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16375cf92bbaSPaolo Abeni } 16385cf92bbaSPaolo Abeni 16397d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 16407d803344SPaolo Abeni 1641a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1642fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1643fa9e5746SPaolo Abeni { 1644fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1645fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1646ddb1a072SPaolo Abeni struct socket *ssock; 1647a2702a07SPaolo Abeni struct sock *ssk; 1648fa9e5746SPaolo Abeni int ret; 1649fa9e5746SPaolo Abeni 1650a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1651a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1652a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1653a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1654a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1655a2702a07SPaolo Abeni */ 1656ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 1657ddb1a072SPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 1658ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 1659ddb1a072SPaolo Abeni return PTR_ERR(ssock); 1660ddb1a072SPaolo Abeni } 1661a2702a07SPaolo Abeni if (!msk->first) 1662a2702a07SPaolo Abeni return -EINVAL; 1663a2702a07SPaolo Abeni 1664a2702a07SPaolo Abeni ssk = msk->first; 1665a2702a07SPaolo Abeni 1666fa9e5746SPaolo Abeni lock_sock(ssk); 1667fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 16687d803344SPaolo Abeni msk->fastopening = 1; 1669fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 16707d803344SPaolo Abeni msk->fastopening = 0; 1671fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1672fa9e5746SPaolo Abeni release_sock(ssk); 1673fa9e5746SPaolo Abeni 1674fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1675fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1676fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1677fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1678fa9e5746SPaolo Abeni 1679fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1680fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1681fa9e5746SPaolo Abeni */ 1682fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1683fa9e5746SPaolo Abeni *copied_syn = 0; 16847d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 1685c2b2ae39SPaolo Abeni /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1686c2b2ae39SPaolo Abeni * __inet_stream_connect() can fail, due to looking check, 1687c2b2ae39SPaolo Abeni * see mptcp_disconnect(). 1688c2b2ae39SPaolo Abeni * Attempt it again outside the problematic scope. 1689c2b2ae39SPaolo Abeni */ 1690c2b2ae39SPaolo Abeni if (!mptcp_disconnect(sk, 0)) 1691c2b2ae39SPaolo Abeni sk->sk_socket->state = SS_UNCONNECTED; 1692fa9e5746SPaolo Abeni } 1693a2702a07SPaolo Abeni inet_sk(sk)->defer_connect = 0; 1694fa9e5746SPaolo Abeni 1695fa9e5746SPaolo Abeni return ret; 1696fa9e5746SPaolo Abeni } 1697fa9e5746SPaolo Abeni 1698f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1699f870fa0bSMat Martineau { 1700f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 170117091708SFlorian Westphal struct page_frag *pfrag; 17026d0060f6SMat Martineau size_t copied = 0; 1703caf971dfSPaolo Abeni int ret = 0; 17046d0060f6SMat Martineau long timeo; 1705f870fa0bSMat Martineau 1706987858e5SPaolo Abeni /* silently ignore everything else */ 17071e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1708987858e5SPaolo Abeni 17096511882cSPaolo Abeni lock_sock(sk); 17101954b860SMat Martineau 1711a2702a07SPaolo Abeni if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) { 1712d98a82a6SDmytro Shytyi int copied_syn = 0; 1713d98a82a6SDmytro Shytyi 1714a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1715d98a82a6SDmytro Shytyi copied += copied_syn; 1716fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1717d98a82a6SDmytro Shytyi goto out; 1718fa9e5746SPaolo Abeni else if (ret) 171969800e51SPaolo Abeni goto do_error; 1720d98a82a6SDmytro Shytyi } 1721d98a82a6SDmytro Shytyi 17221954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17231954b860SMat Martineau 17241954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17251954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17261954b860SMat Martineau if (ret) 172769800e51SPaolo Abeni goto do_error; 17281954b860SMat Martineau } 17291954b860SMat Martineau 173069800e51SPaolo Abeni ret = -EPIPE; 173169800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 173269800e51SPaolo Abeni goto do_error; 173369800e51SPaolo Abeni 173417091708SFlorian Westphal pfrag = sk_page_frag(sk); 173518b683bfSPaolo Abeni 1736d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1737724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1738d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1739d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1740d9ca1de8SPaolo Abeni size_t psize, offset; 1741d9ca1de8SPaolo Abeni 1742d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1743d9ca1de8SPaolo Abeni * page allocator 1744fb529e62SFlorian Westphal */ 1745d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1746d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1747d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1748d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1749d9ca1de8SPaolo Abeni goto wait_for_memory; 17506e628cd3SPaolo Abeni 1751d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1752d9ca1de8SPaolo Abeni goto wait_for_memory; 1753d9ca1de8SPaolo Abeni 1754d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1755d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1756fb529e62SFlorian Westphal } 1757fb529e62SFlorian Westphal 1758d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1759d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1760d9ca1de8SPaolo Abeni * anyway 1761d9ca1de8SPaolo Abeni */ 1762d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1763d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1764d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1765724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17668ce568edSPaolo Abeni 17676511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1768d9ca1de8SPaolo Abeni goto wait_for_memory; 1769d9ca1de8SPaolo Abeni 1770d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1771d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1772d9ca1de8SPaolo Abeni ret = -EFAULT; 177369800e51SPaolo Abeni goto do_error; 1774d9ca1de8SPaolo Abeni } 1775d9ca1de8SPaolo Abeni 1776d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17776511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1778d9ca1de8SPaolo Abeni copied += psize; 1779d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1780d9ca1de8SPaolo Abeni frag_truesize += psize; 1781d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1782d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1783d9ca1de8SPaolo Abeni 1784d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1785d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1786d9ca1de8SPaolo Abeni */ 1787d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1788d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1789d9ca1de8SPaolo Abeni get_page(dfrag->page); 1790d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1791d9ca1de8SPaolo Abeni if (!msk->first_pending) 1792d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1793d9ca1de8SPaolo Abeni } 1794e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1795d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1796d9ca1de8SPaolo Abeni !dfrag_collapsed); 1797d9ca1de8SPaolo Abeni 1798d9ca1de8SPaolo Abeni continue; 1799d9ca1de8SPaolo Abeni 1800d9ca1de8SPaolo Abeni wait_for_memory: 18015cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1802c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1803f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1804f296234cSPeter Krystad if (ret) 180569800e51SPaolo Abeni goto do_error; 1806cec37a6eSPeter Krystad } 1807cec37a6eSPeter Krystad 180813e16037SPaolo Abeni if (copied) 1809c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1810da51aef5SPaolo Abeni 18111954b860SMat Martineau out: 1812cec37a6eSPeter Krystad release_sock(sk); 181369800e51SPaolo Abeni return copied; 181469800e51SPaolo Abeni 181569800e51SPaolo Abeni do_error: 181669800e51SPaolo Abeni if (copied) 181769800e51SPaolo Abeni goto out; 181869800e51SPaolo Abeni 181969800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 182069800e51SPaolo Abeni goto out; 1821f870fa0bSMat Martineau } 1822f870fa0bSMat Martineau 18236771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18246771bfd9SFlorian Westphal struct msghdr *msg, 1825b7f653b2SFlorian Westphal size_t len, int flags, 1826b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1827b7f653b2SFlorian Westphal int *cmsg_flags) 18286771bfd9SFlorian Westphal { 1829ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18306771bfd9SFlorian Westphal int copied = 0; 18316771bfd9SFlorian Westphal 1832ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18336771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18346771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18356771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18366771bfd9SFlorian Westphal int err; 18376771bfd9SFlorian Westphal 1838d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18396771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18406771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18416771bfd9SFlorian Westphal if (!copied) 18426771bfd9SFlorian Westphal return err; 18436771bfd9SFlorian Westphal break; 18446771bfd9SFlorian Westphal } 1845d976092cSPaolo Abeni } 18466771bfd9SFlorian Westphal 1847b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1848b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1849b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1850b7f653b2SFlorian Westphal } 1851b7f653b2SFlorian Westphal 18526771bfd9SFlorian Westphal copied += count; 18536771bfd9SFlorian Westphal 18546771bfd9SFlorian Westphal if (count < data_len) { 18552c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18566771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18572c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18582c9e7765SFlorian Westphal } 18596771bfd9SFlorian Westphal break; 18606771bfd9SFlorian Westphal } 18616771bfd9SFlorian Westphal 1862ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 186387952603SPaolo Abeni /* we will bulk release the skb memory later */ 186487952603SPaolo Abeni skb->destructor = NULL; 1865ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 186687952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18676771bfd9SFlorian Westphal __kfree_skb(skb); 1868ca4fb892SYonglong Li } 18696771bfd9SFlorian Westphal 18706771bfd9SFlorian Westphal if (copied >= len) 18716771bfd9SFlorian Westphal break; 18726771bfd9SFlorian Westphal } 18736771bfd9SFlorian Westphal 18746771bfd9SFlorian Westphal return copied; 18756771bfd9SFlorian Westphal } 18766771bfd9SFlorian Westphal 1877a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1878a6b118feSFlorian Westphal * 1879a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1880a6b118feSFlorian Westphal */ 1881a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1882a6b118feSFlorian Westphal { 1883a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1884a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1885b8dc6d6cSPaolo Abeni u8 scaling_ratio = U8_MAX; 1886a6b118feSFlorian Westphal u32 time, advmss = 1; 1887a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1888a6b118feSFlorian Westphal 1889109cdeb8SGeliang Tang msk_owned_by_me(msk); 1890a6b118feSFlorian Westphal 1891a6b118feSFlorian Westphal if (copied <= 0) 1892a6b118feSFlorian Westphal return; 1893a6b118feSFlorian Westphal 1894a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1895a6b118feSFlorian Westphal 1896a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1897a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1898a6b118feSFlorian Westphal 1899a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1900a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1901a6b118feSFlorian Westphal return; 1902a6b118feSFlorian Westphal 1903a6b118feSFlorian Westphal rtt_us = 0; 1904a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1905a6b118feSFlorian Westphal const struct tcp_sock *tp; 1906a6b118feSFlorian Westphal u64 sf_rtt_us; 1907a6b118feSFlorian Westphal u32 sf_advmss; 1908a6b118feSFlorian Westphal 1909a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1910a6b118feSFlorian Westphal 1911a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1912a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1913a6b118feSFlorian Westphal 1914a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1915a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1916b8dc6d6cSPaolo Abeni scaling_ratio = min(tp->scaling_ratio, scaling_ratio); 1917a6b118feSFlorian Westphal } 1918a6b118feSFlorian Westphal 1919a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1920b8dc6d6cSPaolo Abeni msk->scaling_ratio = scaling_ratio; 1921a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1922a6b118feSFlorian Westphal return; 1923a6b118feSFlorian Westphal 1924a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1925a6b118feSFlorian Westphal goto new_measure; 1926a6b118feSFlorian Westphal 192778047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1928a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1929a6b118feSFlorian Westphal u64 rcvwin, grow; 1930b8dc6d6cSPaolo Abeni int rcvbuf; 1931a6b118feSFlorian Westphal 1932a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1933a6b118feSFlorian Westphal 1934a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1935a6b118feSFlorian Westphal 1936a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1937a6b118feSFlorian Westphal rcvwin += (grow << 1); 1938a6b118feSFlorian Westphal 1939b8dc6d6cSPaolo Abeni rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin), 194002739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1941a6b118feSFlorian Westphal 1942a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1943a6b118feSFlorian Westphal u32 window_clamp; 1944a6b118feSFlorian Westphal 1945b8dc6d6cSPaolo Abeni window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf); 1946a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1947a6b118feSFlorian Westphal 1948a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1949a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1950a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1951a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1952a6b118feSFlorian Westphal */ 1953a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1954a6b118feSFlorian Westphal struct sock *ssk; 1955c76c6956SPaolo Abeni bool slow; 1956a6b118feSFlorian Westphal 1957a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1958c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1959a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1960a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1961c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1962c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1963a6b118feSFlorian Westphal } 1964a6b118feSFlorian Westphal } 1965a6b118feSFlorian Westphal } 1966a6b118feSFlorian Westphal 1967a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1968a6b118feSFlorian Westphal new_measure: 1969a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1970a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1971a6b118feSFlorian Westphal } 1972a6b118feSFlorian Westphal 197387952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 197487952603SPaolo Abeni { 197587952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 197687952603SPaolo Abeni 197787952603SPaolo Abeni if (!msk->rmem_released) 197887952603SPaolo Abeni return; 197987952603SPaolo Abeni 198087952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19816511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1982ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 198387952603SPaolo Abeni } 198487952603SPaolo Abeni 198587952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 198687952603SPaolo Abeni { 198787952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 198887952603SPaolo Abeni 198987952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 199087952603SPaolo Abeni } 199187952603SPaolo Abeni 1992e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19936771bfd9SFlorian Westphal { 199487952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19956771bfd9SFlorian Westphal unsigned int moved = 0; 199687952603SPaolo Abeni bool ret, done; 1997d5f49190SPaolo Abeni 19986771bfd9SFlorian Westphal do { 19996771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 200065f49fe7SFlorian Westphal bool slowpath; 20016771bfd9SFlorian Westphal 200287952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 200387952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 200487952603SPaolo Abeni * that is an unlikely slow path. 200587952603SPaolo Abeni */ 200687952603SPaolo Abeni if (likely(!ssk)) 20076771bfd9SFlorian Westphal break; 20086771bfd9SFlorian Westphal 200965f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 201087952603SPaolo Abeni mptcp_data_lock(sk); 2011e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20126771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 201387952603SPaolo Abeni mptcp_data_unlock(sk); 2014499ada50SPaolo Abeni 2015499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2016499ada50SPaolo Abeni __mptcp_error_report(sk); 201765f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20186771bfd9SFlorian Westphal } while (!done); 20196771bfd9SFlorian Westphal 202087952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 202187952603SPaolo Abeni ret = moved > 0; 202287952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 202387952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 202487952603SPaolo Abeni mptcp_data_lock(sk); 202587952603SPaolo Abeni __mptcp_update_rmem(sk); 202687952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 202787952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 202887952603SPaolo Abeni mptcp_data_unlock(sk); 2029ab174ad8SPaolo Abeni } 203087952603SPaolo Abeni if (ret) 203187952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 203287952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20336771bfd9SFlorian Westphal } 20346771bfd9SFlorian Westphal 20352c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20362c9e7765SFlorian Westphal { 20372c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20382c9e7765SFlorian Westphal const struct sk_buff *skb; 20392c9e7765SFlorian Westphal 20402c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20412c9e7765SFlorian Westphal if (skb) { 20422c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20432c9e7765SFlorian Westphal 20442c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20452c9e7765SFlorian Westphal return INT_MAX; 20462c9e7765SFlorian Westphal 20472c9e7765SFlorian Westphal return (unsigned int)hint_val; 20482c9e7765SFlorian Westphal } 20492c9e7765SFlorian Westphal 20502c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20512c9e7765SFlorian Westphal return 1; 20522c9e7765SFlorian Westphal 20532c9e7765SFlorian Westphal return 0; 20542c9e7765SFlorian Westphal } 20552c9e7765SFlorian Westphal 2056f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2057ec095263SOliver Hartkopp int flags, int *addr_len) 2058f870fa0bSMat Martineau { 2059f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2060b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2061b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20627a6a6cbcSPaolo Abeni int target; 20637a6a6cbcSPaolo Abeni long timeo; 2064f870fa0bSMat Martineau 2065cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2066cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2067cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2068cb9d80f4SPaolo Abeni 20696511882cSPaolo Abeni lock_sock(sk); 2070fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2071fd897679SPaolo Abeni copied = -ENOTCONN; 2072fd897679SPaolo Abeni goto out_err; 2073fd897679SPaolo Abeni } 2074fd897679SPaolo Abeni 2075ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20767a6a6cbcSPaolo Abeni 20777a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20787a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20797a6a6cbcSPaolo Abeni 20802c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20812c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20822c9e7765SFlorian Westphal 208305e3eceaSEric Dumazet while (copied < len) { 2084e3859603SPaolo Abeni int bytes_read; 20857a6a6cbcSPaolo Abeni 2086b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20876771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20887a6a6cbcSPaolo Abeni if (!copied) 20897a6a6cbcSPaolo Abeni copied = bytes_read; 20906771bfd9SFlorian Westphal goto out_err; 2091cec37a6eSPeter Krystad } 2092cec37a6eSPeter Krystad 20937a6a6cbcSPaolo Abeni copied += bytes_read; 20947a6a6cbcSPaolo Abeni 2095ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2096fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2097ea4ca586SPaolo Abeni 2098e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2099e3859603SPaolo Abeni continue; 2100e3859603SPaolo Abeni 21017a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21027a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21037a6a6cbcSPaolo Abeni */ 21047a6a6cbcSPaolo Abeni if (copied >= target) 21057a6a6cbcSPaolo Abeni break; 21067a6a6cbcSPaolo Abeni 21077a6a6cbcSPaolo Abeni if (copied) { 21087a6a6cbcSPaolo Abeni if (sk->sk_err || 21097a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21107a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21117a6a6cbcSPaolo Abeni !timeo || 21127a6a6cbcSPaolo Abeni signal_pending(current)) 21137a6a6cbcSPaolo Abeni break; 21147a6a6cbcSPaolo Abeni } else { 21157a6a6cbcSPaolo Abeni if (sk->sk_err) { 21167a6a6cbcSPaolo Abeni copied = sock_error(sk); 21177a6a6cbcSPaolo Abeni break; 21187a6a6cbcSPaolo Abeni } 21197a6a6cbcSPaolo Abeni 212087952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 212187952603SPaolo Abeni /* race breaker: the shutdown could be after the 212287952603SPaolo Abeni * previous receive queue check 212387952603SPaolo Abeni */ 2124e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 212587952603SPaolo Abeni continue; 21267a6a6cbcSPaolo Abeni break; 212787952603SPaolo Abeni } 21287a6a6cbcSPaolo Abeni 21297a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21307a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21317a6a6cbcSPaolo Abeni break; 21327a6a6cbcSPaolo Abeni } 21337a6a6cbcSPaolo Abeni 21347a6a6cbcSPaolo Abeni if (!timeo) { 21357a6a6cbcSPaolo Abeni copied = -EAGAIN; 21367a6a6cbcSPaolo Abeni break; 21377a6a6cbcSPaolo Abeni } 21387a6a6cbcSPaolo Abeni 21397a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21407a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21417a6a6cbcSPaolo Abeni break; 21427a6a6cbcSPaolo Abeni } 21437a6a6cbcSPaolo Abeni } 21447a6a6cbcSPaolo Abeni 21457a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2146612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21477a6a6cbcSPaolo Abeni } 21483c90e377SPaolo Abeni 21496771bfd9SFlorian Westphal out_err: 2150b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2151b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2152b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21532c9e7765SFlorian Westphal 21542c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21552c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21562c9e7765SFlorian Westphal 21572c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21582c9e7765SFlorian Westphal } 2159b7f653b2SFlorian Westphal } 2160b7f653b2SFlorian Westphal 2161612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2162612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2163612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2164ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2165a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2166a6b118feSFlorian Westphal 2167cec37a6eSPeter Krystad release_sock(sk); 2168cec37a6eSPeter Krystad return copied; 2169cec37a6eSPeter Krystad } 2170cec37a6eSPeter Krystad 2171b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2172b51f9b80SPaolo Abeni { 2173b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2174b51f9b80SPaolo Abeni icsk_retransmit_timer); 2175b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21762d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2177b51f9b80SPaolo Abeni 2178b51f9b80SPaolo Abeni bh_lock_sock(sk); 2179b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21802d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21812d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21822d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2183b51f9b80SPaolo Abeni } else { 2184b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2185e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2186b51f9b80SPaolo Abeni } 2187b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2188b51f9b80SPaolo Abeni sock_put(sk); 2189b51f9b80SPaolo Abeni } 2190b51f9b80SPaolo Abeni 2191e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2192e16163b6SPaolo Abeni { 2193e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2194e16163b6SPaolo Abeni 2195e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2196b6d69fc8SFlorian Westphal sock_put(sk); 2197e16163b6SPaolo Abeni } 2198e16163b6SPaolo Abeni 21993b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22003b1d6210SPaolo Abeni * level. 22013b1d6210SPaolo Abeni * 22023b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22033b1d6210SPaolo Abeni */ 2204ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22053b1d6210SPaolo Abeni { 220671b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22073b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 220871b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22093b1d6210SPaolo Abeni 2210109cdeb8SGeliang Tang msk_owned_by_me(msk); 22113b1d6210SPaolo Abeni 2212d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2213d9ca1de8SPaolo Abeni return NULL; 2214d5f49190SPaolo Abeni 22153b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22163b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22173b1d6210SPaolo Abeni 2218ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2219d5f49190SPaolo Abeni continue; 2220d5f49190SPaolo Abeni 222171b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 222271b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 222371b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 222471b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2225860975c6SFlorian Westphal continue; 2226860975c6SFlorian Westphal } 22273b1d6210SPaolo Abeni 22283b1d6210SPaolo Abeni if (subflow->backup) { 22293b1d6210SPaolo Abeni if (!backup) 22303b1d6210SPaolo Abeni backup = ssk; 22313b1d6210SPaolo Abeni continue; 22323b1d6210SPaolo Abeni } 22333b1d6210SPaolo Abeni 223471b7dec2SPaolo Abeni if (!pick) 223571b7dec2SPaolo Abeni pick = ssk; 22363b1d6210SPaolo Abeni } 22373b1d6210SPaolo Abeni 223871b7dec2SPaolo Abeni if (pick) 223971b7dec2SPaolo Abeni return pick; 224071b7dec2SPaolo Abeni 224171b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 224271b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22433b1d6210SPaolo Abeni } 22443b1d6210SPaolo Abeni 224517aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 224617aee05dSFlorian Westphal { 224717aee05dSFlorian Westphal if (msk->subflow) { 224817aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 22495b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 225017aee05dSFlorian Westphal } 225117aee05dSFlorian Westphal } 225217aee05dSFlorian Westphal 22531e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22541e1d9d6fSPaolo Abeni { 22551e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22561e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22571e1d9d6fSPaolo Abeni 225800df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 22591e1d9d6fSPaolo Abeni return false; 22601e1d9d6fSPaolo Abeni 22611e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22621e1d9d6fSPaolo Abeni return false; 22631e1d9d6fSPaolo Abeni 22641e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22651e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22661e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22671e1d9d6fSPaolo Abeni */ 22681e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22691e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22701e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22711e1d9d6fSPaolo Abeni if (!rtx_head) { 22721e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22731e1d9d6fSPaolo Abeni return false; 22741e1d9d6fSPaolo Abeni } 22751e1d9d6fSPaolo Abeni 22761e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22771e1d9d6fSPaolo Abeni msk->recovery = true; 22781e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22791e1d9d6fSPaolo Abeni 22801e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22811e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22821e1d9d6fSPaolo Abeni 22831e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22841e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22851e1d9d6fSPaolo Abeni if (!cur->already_sent) 22861e1d9d6fSPaolo Abeni break; 22871e1d9d6fSPaolo Abeni cur->already_sent = 0; 22881e1d9d6fSPaolo Abeni } 22891e1d9d6fSPaolo Abeni 22901e1d9d6fSPaolo Abeni return true; 22911e1d9d6fSPaolo Abeni } 22921e1d9d6fSPaolo Abeni 2293b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2294b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2295b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2296b29fcfb5SPaolo Abeni 2297cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2298cec37a6eSPeter Krystad * (accept). 2299cec37a6eSPeter Krystad * 2300cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2301cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2302cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2303cec37a6eSPeter Krystad * parent socket. 2304cec37a6eSPeter Krystad */ 2305a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2306b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2307b29fcfb5SPaolo Abeni unsigned int flags) 2308cec37a6eSPeter Krystad { 2309e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 231063740448SPaolo Abeni bool dispose_it, need_push = false; 231163740448SPaolo Abeni 231263740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 231363740448SPaolo Abeni * to an incoming reset, mptcp either: 231463740448SPaolo Abeni * - if either the subflow or the msk are dead, destroy the context 231563740448SPaolo Abeni * (the subflow socket is deleted by inet_child_forget) and the msk 231663740448SPaolo Abeni * - otherwise do nothing at the moment and take action at accept and/or 231763740448SPaolo Abeni * listener shutdown - user-space must be able to accept() the closed 231863740448SPaolo Abeni * socket. 231963740448SPaolo Abeni */ 232063740448SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk) { 232163740448SPaolo Abeni if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 232263740448SPaolo Abeni return; 232363740448SPaolo Abeni 232463740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 232563740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 232663740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 232763740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 232863740448SPaolo Abeni goto out_release; 232963740448SPaolo Abeni } 2330e0be4931SFlorian Westphal 2331b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2332b29fcfb5SPaolo Abeni if (dispose_it) 2333cec37a6eSPeter Krystad list_del(&subflow->node); 2334cec37a6eSPeter Krystad 23353f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2336e16163b6SPaolo Abeni 2337ff18f9efSPaolo Abeni if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) { 2338d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2339d21f8348SPaolo Abeni * to generate the egress reset 2340d21f8348SPaolo Abeni */ 2341d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2342d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2343b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2344d21f8348SPaolo Abeni } 2345b29fcfb5SPaolo Abeni 2346b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2347b29fcfb5SPaolo Abeni if (!dispose_it) { 2348c2b2ae39SPaolo Abeni /* The MPTCP code never wait on the subflow sockets, TCP-level 2349c2b2ae39SPaolo Abeni * disconnect should never fail 2350c2b2ae39SPaolo Abeni */ 2351c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 2352b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2353b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2354b29fcfb5SPaolo Abeni release_sock(ssk); 2355b29fcfb5SPaolo Abeni 2356b29fcfb5SPaolo Abeni goto out; 2357b29fcfb5SPaolo Abeni } 2358b29fcfb5SPaolo Abeni 2359d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2360d7b1bfd0SPaolo Abeni 2361e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2362e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2363e16163b6SPaolo Abeni * reference owned by msk; 2364e16163b6SPaolo Abeni */ 2365e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2366b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2367e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2368e16163b6SPaolo Abeni } else { 2369d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2370e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2371e16163b6SPaolo Abeni 2372e16163b6SPaolo Abeni /* close acquired an extra ref */ 2373e16163b6SPaolo Abeni __sock_put(ssk); 2374e16163b6SPaolo Abeni } 237563740448SPaolo Abeni 237663740448SPaolo Abeni out_release: 2377e16163b6SPaolo Abeni release_sock(ssk); 2378e16163b6SPaolo Abeni 2379e16163b6SPaolo Abeni sock_put(ssk); 2380e0be4931SFlorian Westphal 2381c8fe62f0SFlorian Westphal if (ssk == msk->first) 23821b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2383c8fe62f0SFlorian Westphal 2384b29fcfb5SPaolo Abeni out: 2385b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2386b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23871e1d9d6fSPaolo Abeni 23881e1d9d6fSPaolo Abeni if (need_push) 23891e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2390f870fa0bSMat Martineau } 2391f870fa0bSMat Martineau 2392a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2393a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2394a141e02eSFlorian Westphal { 2395b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2396b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2397a88c9e49SPaolo Abeni 2398a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2399a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2400a88c9e49SPaolo Abeni */ 2401a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2402a88c9e49SPaolo Abeni 2403b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2404a141e02eSFlorian Westphal } 2405a141e02eSFlorian Westphal 2406dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2407dc24f8b4SPaolo Abeni { 2408dc24f8b4SPaolo Abeni return 0; 2409dc24f8b4SPaolo Abeni } 2410dc24f8b4SPaolo Abeni 2411b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24120e4f35d7SPaolo Abeni { 24130e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2414b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24150e4f35d7SPaolo Abeni 24163abc05d9SFlorian Westphal might_sleep(); 24173abc05d9SFlorian Westphal 24185efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24190e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24200e4f35d7SPaolo Abeni 24210e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24220e4f35d7SPaolo Abeni continue; 24230e4f35d7SPaolo Abeni 242440947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 242540947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 242640947e13SFlorian Westphal continue; 242740947e13SFlorian Westphal 2428b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2429b6985b9bSPaolo Abeni } 2430b6985b9bSPaolo Abeni 24310e4f35d7SPaolo Abeni } 24320e4f35d7SPaolo Abeni 243363740448SPaolo Abeni static bool mptcp_should_close(const struct sock *sk) 2434e16163b6SPaolo Abeni { 2435e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2436e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2437e16163b6SPaolo Abeni 243863740448SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2439e16163b6SPaolo Abeni return true; 2440e16163b6SPaolo Abeni 2441e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2442e16163b6SPaolo Abeni * timeout 2443e16163b6SPaolo Abeni */ 2444e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2445e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2446e16163b6SPaolo Abeni TCP_CLOSE) 2447e16163b6SPaolo Abeni return false; 2448e16163b6SPaolo Abeni } 2449e16163b6SPaolo Abeni return true; 2450e16163b6SPaolo Abeni } 2451e16163b6SPaolo Abeni 245250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 245350c504a2SFlorian Westphal { 245450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 245580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 245650c504a2SFlorian Westphal 245750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 245850c504a2SFlorian Westphal return; 245950c504a2SFlorian Westphal 246050c504a2SFlorian Westphal mptcp_token_destroy(msk); 246150c504a2SFlorian Westphal 24625efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 246350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 246475e908c3SPaolo Abeni bool slow; 246550c504a2SFlorian Westphal 246675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 246750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 246850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 246950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 247050c504a2SFlorian Westphal } 247175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 247250c504a2SFlorian Westphal } 247350c504a2SFlorian Westphal 247469800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 247569800e51SPaolo Abeni switch (sk->sk_state) { 247669800e51SPaolo Abeni case TCP_SYN_SENT: 24779ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 247869800e51SPaolo Abeni break; 247969800e51SPaolo Abeni case TCP_CLOSE_WAIT: 24809ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 248169800e51SPaolo Abeni break; 248269800e51SPaolo Abeni case TCP_CLOSE: 248369800e51SPaolo Abeni return; 248469800e51SPaolo Abeni default: 24859ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 248669800e51SPaolo Abeni } 248769800e51SPaolo Abeni 248850c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 24896b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 249050c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 249150c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 249250c504a2SFlorian Westphal 249369800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 249469800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 249569800e51SPaolo Abeni return; 249669800e51SPaolo Abeni 249769800e51SPaolo Abeni sk->sk_state_change(sk); 249869800e51SPaolo Abeni sk_error_report(sk); 249950c504a2SFlorian Westphal } 250050c504a2SFlorian Westphal 25012948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 250280992017SPaolo Abeni { 25032948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2504caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25053b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25063b1d6210SPaolo Abeni size_t copied = 0; 25072948d0a1SPaolo Abeni struct sock *ssk; 25082948d0a1SPaolo Abeni int ret; 25092948d0a1SPaolo Abeni 2510b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25113241a9c0SFlorian Westphal 25123241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25133241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25142948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25156477dd39SMat Martineau if (!dfrag) { 25166477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25176477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25186477dd39SMat Martineau 25196477dd39SMat Martineau icsk->icsk_retransmits++; 25206477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25216477dd39SMat Martineau mptcp_send_ack(msk); 25226477dd39SMat Martineau 25236477dd39SMat Martineau goto reset_timer; 25246477dd39SMat Martineau } 25256477dd39SMat Martineau 25263241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25272948d0a1SPaolo Abeni return; 25283241a9c0SFlorian Westphal 25293241a9c0SFlorian Westphal goto reset_timer; 25306477dd39SMat Martineau } 25312948d0a1SPaolo Abeni 25322948d0a1SPaolo Abeni if (!ssk) 25332948d0a1SPaolo Abeni goto reset_timer; 25342948d0a1SPaolo Abeni 25352948d0a1SPaolo Abeni lock_sock(ssk); 25362948d0a1SPaolo Abeni 25372948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25382948d0a1SPaolo Abeni info.sent = 0; 25394e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25404e14867dSPaolo Abeni while (info.sent < info.limit) { 25412948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25422948d0a1SPaolo Abeni if (ret <= 0) 25432948d0a1SPaolo Abeni break; 25442948d0a1SPaolo Abeni 25452948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25462948d0a1SPaolo Abeni copied += ret; 25472948d0a1SPaolo Abeni info.sent += ret; 25482948d0a1SPaolo Abeni } 25494e14867dSPaolo Abeni if (copied) { 25504e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 255138967f42SPaolo Abeni msk->bytes_retrans += copied; 25522948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 25532948d0a1SPaolo Abeni info.size_goal); 25540530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 25554e14867dSPaolo Abeni } 25562948d0a1SPaolo Abeni 25572948d0a1SPaolo Abeni release_sock(ssk); 25582948d0a1SPaolo Abeni 25592948d0a1SPaolo Abeni reset_timer: 25603241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 25613241a9c0SFlorian Westphal 25622948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 25632948d0a1SPaolo Abeni mptcp_reset_timer(sk); 25642948d0a1SPaolo Abeni } 25652948d0a1SPaolo Abeni 256676a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 256776a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 256876a13b31SGeliang Tang */ 256976a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 257076a13b31SGeliang Tang { 257176a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 257276a13b31SGeliang Tang unsigned long timeout, close_timeout; 257376a13b31SGeliang Tang 257476a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 257576a13b31SGeliang Tang return; 257676a13b31SGeliang Tang 257776a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 257876a13b31SGeliang Tang 257976a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 258076a13b31SGeliang Tang * them is active 258176a13b31SGeliang Tang */ 258276a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 258376a13b31SGeliang Tang 258476a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 258576a13b31SGeliang Tang } 258676a13b31SGeliang Tang 258749fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 258849fa1919SGeliang Tang { 258976a13b31SGeliang Tang struct sock *ssk = msk->first; 259049fa1919SGeliang Tang bool slow; 259149fa1919SGeliang Tang 259276a13b31SGeliang Tang if (!ssk) 259376a13b31SGeliang Tang return; 259476a13b31SGeliang Tang 259549fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 259649fa1919SGeliang Tang 259749fa1919SGeliang Tang slow = lock_sock_fast(ssk); 259849fa1919SGeliang Tang mptcp_subflow_reset(ssk); 259976a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 260049fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 260176a13b31SGeliang Tang 260276a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 260349fa1919SGeliang Tang } 260449fa1919SGeliang Tang 2605d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2606d21f8348SPaolo Abeni { 2607d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2608d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2609d21f8348SPaolo Abeni 2610bbd49d11SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2611d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2612d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2613d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2614d21f8348SPaolo Abeni } 2615d21f8348SPaolo Abeni 26162948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26172948d0a1SPaolo Abeni { 26182948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 261980638684SGeliang Tang struct sock *sk = (struct sock *)msk; 262076a13b31SGeliang Tang unsigned long fail_tout; 26212948d0a1SPaolo Abeni int state; 262280992017SPaolo Abeni 262380992017SPaolo Abeni lock_sock(sk); 2624e16163b6SPaolo Abeni state = sk->sk_state; 2625d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2626e16163b6SPaolo Abeni goto unlock; 2627e16163b6SPaolo Abeni 262850c504a2SFlorian Westphal mptcp_check_fastclose(msk); 262950c504a2SFlorian Westphal 2630e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2631b416268bSFlorian Westphal 263281c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 263381c1d029SPaolo Abeni mptcp_check_data_fin_ack(sk); 263443b54c6eSMat Martineau mptcp_check_data_fin(sk); 263543b54c6eSMat Martineau 2636b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2637b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2638b6985b9bSPaolo Abeni 2639341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2640341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2641341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2642e16163b6SPaolo Abeni */ 2643d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 2644bbd49d11SPaolo Abeni if (mptcp_should_close(sk)) 2645d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2646bbd49d11SPaolo Abeni 2647d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2648e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2649e16163b6SPaolo Abeni goto unlock; 2650e16163b6SPaolo Abeni } 2651d21f8348SPaolo Abeni } 2652e16163b6SPaolo Abeni 26532948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 26542948d0a1SPaolo Abeni __mptcp_retrans(sk); 26553b1d6210SPaolo Abeni 265676a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 265776a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 265849fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 265949fa1919SGeliang Tang 26603b1d6210SPaolo Abeni unlock: 266180992017SPaolo Abeni release_sock(sk); 266280992017SPaolo Abeni sock_put(sk); 266380992017SPaolo Abeni } 266480992017SPaolo Abeni 2665784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2666f870fa0bSMat Martineau { 2667cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2668cec37a6eSPeter Krystad 2669cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2670ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 267118b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 267280992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 267387952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2674ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2675f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 26766511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2677ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 267833d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2679cec37a6eSPeter Krystad 26801b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2681dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2682752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 26830530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 26841e1d9d6fSPaolo Abeni msk->recovery = false; 26856f06b4d4SPaolo Abeni msk->subflow_id = 1; 26868ab183deSPaolo Abeni 26871b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 26881b1c7a0eSPeter Krystad 2689b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2690b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2691e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2692aa1fbd94SFlorian Westphal 2693f870fa0bSMat Martineau return 0; 2694f870fa0bSMat Martineau } 2695f870fa0bSMat Martineau 2696b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2697784325e9SMatthieu Baerts { 269820b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2699b29fcfb5SPaolo Abeni 2700b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2701b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2702b29fcfb5SPaolo Abeni 2703b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2704b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2705b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2706b29fcfb5SPaolo Abeni } 2707b29fcfb5SPaolo Abeni 2708b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2709b29fcfb5SPaolo Abeni { 2710fc518953SFlorian Westphal struct net *net = sock_net(sk); 2711fc518953SFlorian Westphal int ret; 271218b683bfSPaolo Abeni 2713b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2714b6c08380SGeliang Tang if (ret) 2715b6c08380SGeliang Tang return ret; 2716b6c08380SGeliang Tang 2717fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2718fc518953SFlorian Westphal return -ENOPROTOOPT; 2719fc518953SFlorian Westphal 2720fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2721fc518953SFlorian Westphal return -ENOMEM; 2722fc518953SFlorian Westphal 2723a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2724a5ef058dSPaolo Abeni 272520b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 272620b5759fSPaolo Abeni * propagate the correct value 272720b5759fSPaolo Abeni */ 2728b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 272920b5759fSPaolo Abeni 2730d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2731a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2732a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2733d027236cSPaolo Abeni 273418b683bfSPaolo Abeni return 0; 273518b683bfSPaolo Abeni } 273618b683bfSPaolo Abeni 273718b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 273818b683bfSPaolo Abeni { 273918b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 274018b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 274118b683bfSPaolo Abeni 2742d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 274318b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2744d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2745784325e9SMatthieu Baerts } 2746784325e9SMatthieu Baerts 274730e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 274880992017SPaolo Abeni { 274980992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 275080992017SPaolo Abeni 2751b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2752e16163b6SPaolo Abeni __sock_put(sk); 275380992017SPaolo Abeni } 275480992017SPaolo Abeni 2755d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 275621498490SPeter Krystad { 275721498490SPeter Krystad lock_sock(ssk); 275821498490SPeter Krystad 275921498490SPeter Krystad switch (ssk->sk_state) { 276021498490SPeter Krystad case TCP_LISTEN: 276121498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 276221498490SPeter Krystad break; 2763df561f66SGustavo A. R. Silva fallthrough; 276421498490SPeter Krystad case TCP_SYN_SENT: 2765c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 276621498490SPeter Krystad break; 276721498490SPeter Krystad default: 276843b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 276943b54c6eSMat Martineau pr_debug("Fallback"); 277021498490SPeter Krystad ssk->sk_shutdown |= how; 277121498490SPeter Krystad tcp_shutdown(ssk, how); 277281c1d029SPaolo Abeni 277381c1d029SPaolo Abeni /* simulate the data_fin ack reception to let the state 277481c1d029SPaolo Abeni * machine move forward 277581c1d029SPaolo Abeni */ 277681c1d029SPaolo Abeni WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 277781c1d029SPaolo Abeni mptcp_schedule_work(sk); 277843b54c6eSMat Martineau } else { 277943b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 278043b54c6eSMat Martineau tcp_send_ack(ssk); 27816477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 27826477dd39SMat Martineau mptcp_reset_timer(sk); 278343b54c6eSMat Martineau } 278421498490SPeter Krystad break; 278521498490SPeter Krystad } 278621498490SPeter Krystad 278721498490SPeter Krystad release_sock(ssk); 278821498490SPeter Krystad } 278921498490SPeter Krystad 27906920b851SMat Martineau static const unsigned char new_state[16] = { 27916920b851SMat Martineau /* current state: new state: action: */ 27926920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 27936920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27946920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 27956920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27966920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 27976920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 27986920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 27996920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28006920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28016920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28026920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28036920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28046920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28056920b851SMat Martineau }; 28066920b851SMat Martineau 28076920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28086920b851SMat Martineau { 28096920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28106920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28116920b851SMat Martineau 28126920b851SMat Martineau inet_sk_state_store(sk, ns); 28136920b851SMat Martineau 28146920b851SMat Martineau return next & TCP_ACTION_FIN; 28156920b851SMat Martineau } 28166920b851SMat Martineau 281781c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk) 2818e16163b6SPaolo Abeni { 2819e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2820e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2821e16163b6SPaolo Abeni 2822e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2823e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2824e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2825e16163b6SPaolo Abeni 2826e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2827e16163b6SPaolo Abeni * skip this 2828e16163b6SPaolo Abeni */ 2829e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2830e16163b6SPaolo Abeni mptcp_send_head(sk)) 2831e16163b6SPaolo Abeni return; 2832e16163b6SPaolo Abeni 2833e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2834e16163b6SPaolo Abeni 2835e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2836e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2837e16163b6SPaolo Abeni 2838e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2839e16163b6SPaolo Abeni } 2840e16163b6SPaolo Abeni } 2841e16163b6SPaolo Abeni 2842e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2843e16163b6SPaolo Abeni { 2844e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2845e16163b6SPaolo Abeni 2846e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2847e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2848e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2849e16163b6SPaolo Abeni 2850e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2851e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2852e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2853e16163b6SPaolo Abeni 285481c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 2855e16163b6SPaolo Abeni } 2856e16163b6SPaolo Abeni 2857e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2858f870fa0bSMat Martineau { 2859f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2860f870fa0bSMat Martineau 2861e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 28622c22c06cSFlorian Westphal 28633abc05d9SFlorian Westphal might_sleep(); 28643abc05d9SFlorian Westphal 2865bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2866e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2867e16163b6SPaolo Abeni msk->pm.status = 0; 2868b2c5b614SFlorian Westphal 2869e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2870e16163b6SPaolo Abeni 28716511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 287287952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2873e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2874e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2875aa1fbd94SFlorian Westphal 2876e16163b6SPaolo Abeni sock_put(sk); 2877e16163b6SPaolo Abeni } 2878e16163b6SPaolo Abeni 287963740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 288063740448SPaolo Abeni { 288163740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 288263740448SPaolo Abeni mptcp_do_fastclose(sk); 288363740448SPaolo Abeni __mptcp_destroy_sock(sk); 288463740448SPaolo Abeni } 288563740448SPaolo Abeni 2886d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2887d21f8348SPaolo Abeni { 2888d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2889d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2890d21f8348SPaolo Abeni */ 2891d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2892d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2893d21f8348SPaolo Abeni return 0; 2894d21f8348SPaolo Abeni 2895d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2896d21f8348SPaolo Abeni } 2897d21f8348SPaolo Abeni 289857fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk) 2899c558246eSMenglong Dong { 290057fc0f1cSPaolo Abeni struct sock *ssk; 290157fc0f1cSPaolo Abeni 290257fc0f1cSPaolo Abeni if (inet_sk_state_load(sk) != TCP_LISTEN) 290357fc0f1cSPaolo Abeni return; 290457fc0f1cSPaolo Abeni 2905c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 290657fc0f1cSPaolo Abeni ssk = mptcp_sk(sk)->first; 290757fc0f1cSPaolo Abeni if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 290857fc0f1cSPaolo Abeni return; 290957fc0f1cSPaolo Abeni 291057fc0f1cSPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 29113fffa15bSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 291257fc0f1cSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 291357fc0f1cSPaolo Abeni inet_csk_listen_stop(ssk); 291457fc0f1cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 291557fc0f1cSPaolo Abeni release_sock(ssk); 2916c558246eSMenglong Dong } 2917c558246eSMenglong Dong 291826d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2919e16163b6SPaolo Abeni { 2920e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 292176a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2922e16163b6SPaolo Abeni bool do_cancel_work = false; 2923d4e85922SPaolo Abeni int subflows_alive = 0; 2924e16163b6SPaolo Abeni 29256b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2926e16163b6SPaolo Abeni 2927e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 292857fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 2929e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2930e16163b6SPaolo Abeni goto cleanup; 2931e16163b6SPaolo Abeni } 2932e16163b6SPaolo Abeni 29338d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 29348d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 29358d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 29368d547809SPaolo Abeni */ 2937d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 29388d547809SPaolo Abeni timeout = 0; 2939d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2940e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2941d21f8348SPaolo Abeni } 2942e16163b6SPaolo Abeni 2943e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2944e16163b6SPaolo Abeni 2945e16163b6SPaolo Abeni cleanup: 2946e16163b6SPaolo Abeni /* orphan all the subflows */ 2947e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 294876a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2949e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 295049054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2951e16163b6SPaolo Abeni 2952d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 2953d4e85922SPaolo Abeni 295476a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 295576a13b31SGeliang Tang * cancel the latter 295676a13b31SGeliang Tang */ 295776a13b31SGeliang Tang if (ssk == msk->first) 295876a13b31SGeliang Tang subflow->fail_tout = 0; 295976a13b31SGeliang Tang 2960fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 2961fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 2962fe948001SMenglong Dong */ 2963fe948001SMenglong Dong ssk->sk_socket = NULL; 2964fe948001SMenglong Dong ssk->sk_wq = NULL; 2965e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2966e16163b6SPaolo Abeni } 2967e16163b6SPaolo Abeni sock_orphan(sk); 2968e16163b6SPaolo Abeni 2969d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 2970d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 2971d4e85922SPaolo Abeni */ 2972d4e85922SPaolo Abeni if (subflows_alive == 0) 2973d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2974d4e85922SPaolo Abeni 2975e16163b6SPaolo Abeni sock_hold(sk); 2976e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 297700df24f1SGeliang Tang if (msk->token) 297876a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2979b29fcfb5SPaolo Abeni 2980e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2981e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2982e16163b6SPaolo Abeni do_cancel_work = true; 2983e16163b6SPaolo Abeni } else { 298476a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2985e16163b6SPaolo Abeni } 298626d3e21cSMenglong Dong 298726d3e21cSMenglong Dong return do_cancel_work; 298826d3e21cSMenglong Dong } 298926d3e21cSMenglong Dong 299026d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 299126d3e21cSMenglong Dong { 299226d3e21cSMenglong Dong bool do_cancel_work; 299326d3e21cSMenglong Dong 299426d3e21cSMenglong Dong lock_sock(sk); 299526d3e21cSMenglong Dong 299626d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 2997e16163b6SPaolo Abeni release_sock(sk); 2998e16163b6SPaolo Abeni if (do_cancel_work) 299980992017SPaolo Abeni mptcp_cancel_work(sk); 3000b911c97cSFlorian Westphal 3001e16163b6SPaolo Abeni sock_put(sk); 3002f870fa0bSMat Martineau } 3003f870fa0bSMat Martineau 30047e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3005cf7da0d6SPeter Krystad { 3006cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3007cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3008cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3009cf7da0d6SPeter Krystad 3010cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3011cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3012cf7da0d6SPeter Krystad 3013cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3014cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3015cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3016cf7da0d6SPeter Krystad } 3017cf7da0d6SPeter Krystad #endif 3018cf7da0d6SPeter Krystad 3019cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3020cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3021cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3022cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3023cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3024cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3025cf7da0d6SPeter Krystad } 3026cf7da0d6SPeter Krystad 302718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 302818b683bfSPaolo Abeni { 302976e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 303076e2a55dSPaolo Abeni 30310ad529d9SPaolo Abeni /* Deny disconnect if other threads are blocked in sk_wait_event() 30320ad529d9SPaolo Abeni * or inet_wait_for_connect(). 30330ad529d9SPaolo Abeni */ 30340ad529d9SPaolo Abeni if (sk->sk_wait_pending) 30350ad529d9SPaolo Abeni return -EBUSY; 30360ad529d9SPaolo Abeni 30377d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 30387d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 3039c2b2ae39SPaolo Abeni * msk->firstsocket lock). 30407d803344SPaolo Abeni */ 30417d803344SPaolo Abeni if (msk->fastopening) 3042c2b2ae39SPaolo Abeni return -EBUSY; 30437d803344SPaolo Abeni 304457fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3045b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3046b29fcfb5SPaolo Abeni 3047bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3048b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3049b29fcfb5SPaolo Abeni 305000df24f1SGeliang Tang if (msk->token) 305100df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3052b29fcfb5SPaolo Abeni 3053c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3054c0bf3c6aSPaolo Abeni * subflow 3055c0bf3c6aSPaolo Abeni */ 3056c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3057b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3058e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3059e9d09bacSPaolo Abeni msk->cb_flags = 0; 3060e9d09bacSPaolo Abeni msk->push_pending = 0; 3061b29fcfb5SPaolo Abeni msk->recovery = false; 3062b29fcfb5SPaolo Abeni msk->can_ack = false; 3063b29fcfb5SPaolo Abeni msk->fully_established = false; 3064b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3065b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3066b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3067b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3068b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3069b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3070b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 307138967f42SPaolo Abeni msk->bytes_acked = 0; 307238967f42SPaolo Abeni msk->bytes_received = 0; 307338967f42SPaolo Abeni msk->bytes_sent = 0; 307438967f42SPaolo Abeni msk->bytes_retrans = 0; 3075b29fcfb5SPaolo Abeni 30766b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3077b29fcfb5SPaolo Abeni sk_error_report(sk); 307842c556feSFlorian Westphal return 0; 307918b683bfSPaolo Abeni } 308018b683bfSPaolo Abeni 3081b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3082b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3083b0519de8SFlorian Westphal { 3084b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3085b0519de8SFlorian Westphal 3086b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3087b0519de8SFlorian Westphal } 3088b0519de8SFlorian Westphal #endif 3089b0519de8SFlorian Westphal 30907e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3091cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 30927e8b88ecSPaolo Abeni struct sock *ssk, 3093fca5c82cSPaolo Abeni struct request_sock *req) 3094b0519de8SFlorian Westphal { 309558b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3096b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 309758b09919SPaolo Abeni struct mptcp_sock *msk; 3098b0519de8SFlorian Westphal 3099b0519de8SFlorian Westphal if (!nsk) 3100b0519de8SFlorian Westphal return NULL; 3101b0519de8SFlorian Westphal 3102b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3103b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3104b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3105b0519de8SFlorian Westphal #endif 3106b0519de8SFlorian Westphal 31070ad529d9SPaolo Abeni nsk->sk_wait_pending = 0; 310858b09919SPaolo Abeni __mptcp_init_sock(nsk); 310958b09919SPaolo Abeni 311058b09919SPaolo Abeni msk = mptcp_sk(nsk); 311158b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 311258b09919SPaolo Abeni msk->token = subflow_req->token; 31135b825727SPaolo Abeni WRITE_ONCE(msk->subflow, NULL); 3114b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3115b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 311674c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 311706251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 311858b09919SPaolo Abeni 311958b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3120eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31217439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31227439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3123df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31246f8a612aSFlorian Westphal 31256f06b4d4SPaolo Abeni /* passive msk is created after the first/MPC subflow */ 31266f06b4d4SPaolo Abeni msk->subflow_id = 2; 31276f06b4d4SPaolo Abeni 31285e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31290c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 31307e8b88ecSPaolo Abeni 31317e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 31327e8b88ecSPaolo Abeni * not yet exposted to user-space 31337e8b88ecSPaolo Abeni */ 31347e8b88ecSPaolo Abeni inet_sk_state_store(nsk, TCP_ESTABLISHED); 31357e8b88ecSPaolo Abeni 31367e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 31377e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 31387e8b88ecSPaolo Abeni list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list); 31397e8b88ecSPaolo Abeni sock_hold(ssk); 31407e8b88ecSPaolo Abeni 31417e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 31427e8b88ecSPaolo Abeni * created mptcp socket 31437e8b88ecSPaolo Abeni */ 31447e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 31457e8b88ecSPaolo Abeni 31467e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 31477e8b88ecSPaolo Abeni * uses the correct data 31487e8b88ecSPaolo Abeni */ 31497e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 31507e8b88ecSPaolo Abeni mptcp_propagate_sndbuf(nsk, ssk); 31517e8b88ecSPaolo Abeni 31527e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 315358b09919SPaolo Abeni bh_unlock_sock(nsk); 315458b09919SPaolo Abeni 3155b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3156b0519de8SFlorian Westphal return nsk; 3157b0519de8SFlorian Westphal } 3158b0519de8SFlorian Westphal 3159a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3160a6b118feSFlorian Westphal { 3161a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3162a6b118feSFlorian Westphal 3163a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3164a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3165a6b118feSFlorian Westphal 3166a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3167a6b118feSFlorian Westphal 3168a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3169a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3170a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3171a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3172a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 31736f8a612aSFlorian Westphal 31747439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3175a6b118feSFlorian Westphal } 3176a6b118feSFlorian Westphal 3177cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3178cf7da0d6SPeter Krystad bool kern) 3179cf7da0d6SPeter Krystad { 3180cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3181cf7da0d6SPeter Krystad struct socket *listener; 3182cf7da0d6SPeter Krystad struct sock *newsk; 3183cf7da0d6SPeter Krystad 31845b825727SPaolo Abeni listener = READ_ONCE(msk->subflow); 3185cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3186cf7da0d6SPeter Krystad *err = -EINVAL; 3187cf7da0d6SPeter Krystad return NULL; 3188cf7da0d6SPeter Krystad } 3189cf7da0d6SPeter Krystad 3190cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3191cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3192cf7da0d6SPeter Krystad if (!newsk) 3193cf7da0d6SPeter Krystad return NULL; 3194cf7da0d6SPeter Krystad 3195cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3196cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3197cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3198cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3199cf7da0d6SPeter Krystad 3200cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 320158b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 320258b09919SPaolo Abeni 320358b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 320458b09919SPaolo Abeni * subflow_syn_recv_sock() 320558b09919SPaolo Abeni */ 320658b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 320758b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3208d6692b3bSFlorian Westphal goto out; 320958b09919SPaolo Abeni } 321058b09919SPaolo Abeni 3211cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 32120397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3213fc518953SFlorian Westphal } else { 3214fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3215fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3216cf7da0d6SPeter Krystad } 3217cf7da0d6SPeter Krystad 3218d6692b3bSFlorian Westphal out: 3219d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3220cf7da0d6SPeter Krystad return newsk; 3221cf7da0d6SPeter Krystad } 3222cf7da0d6SPeter Krystad 3223c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 32245c8c1640SGeliang Tang { 3225c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 322687952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 322787952603SPaolo Abeni 32286e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 32296e628cd3SPaolo Abeni 3230c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 32315efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3232c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3233c0bf3c6aSPaolo Abeni 323487952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3235b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 323687952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32376511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32385c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3239b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32406511882cSPaolo Abeni 32416511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 32426511882cSPaolo Abeni * inet_sock_destruct() will dispose it 32436511882cSPaolo Abeni */ 32446511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 32456511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 32465c8c1640SGeliang Tang mptcp_token_destroy(msk); 32475c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 32484638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 32495c8c1640SGeliang Tang } 32505c8c1640SGeliang Tang 325179c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 325279c0949eSPeter Krystad { 3253c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3254c9fd9c5fSFlorian Westphal 3255c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3256c0bf3c6aSPaolo Abeni * even the initial subflow 3257c0bf3c6aSPaolo Abeni */ 3258c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3259c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3260d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 326179c0949eSPeter Krystad } 326279c0949eSPeter Krystad 32636e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32646e628cd3SPaolo Abeni { 32656e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32666e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32676e628cd3SPaolo Abeni else 3268e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 32696e628cd3SPaolo Abeni 32706e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 32716e628cd3SPaolo Abeni mptcp_schedule_work(sk); 32726e628cd3SPaolo Abeni } 32736e628cd3SPaolo Abeni 3274219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 32756e628cd3SPaolo Abeni { 32766e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 32776e628cd3SPaolo Abeni return; 32786e628cd3SPaolo Abeni 327973a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 328073a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 328173a0052aSGeliang Tang else 3282e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 32836e628cd3SPaolo Abeni } 32846e628cd3SPaolo Abeni 3285e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3286e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3287e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3288e9d09bacSPaolo Abeni 3289e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 329014c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3291e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 329214c441b5SPaolo Abeni { 3293e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 32942d6f5a2bSPaolo Abeni 3295e9d09bacSPaolo Abeni for (;;) { 3296e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3297e9d09bacSPaolo Abeni msk->push_pending; 329856a666c4SPaolo Abeni struct list_head join_list; 329956a666c4SPaolo Abeni 3300c2e6048fSPaolo Abeni if (!flags) 3301c2e6048fSPaolo Abeni break; 3302c2e6048fSPaolo Abeni 330356a666c4SPaolo Abeni INIT_LIST_HEAD(&join_list); 330456a666c4SPaolo Abeni list_splice_init(&msk->join_list, &join_list); 330556a666c4SPaolo Abeni 3306c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 33076e628cd3SPaolo Abeni * 33086e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 33096e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 33106e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 33116e628cd3SPaolo Abeni * the subflow socket lock 33126e628cd3SPaolo Abeni */ 3313e9d09bacSPaolo Abeni msk->push_pending = 0; 3314e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 33156e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 331656a666c4SPaolo Abeni 33173e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 331856a666c4SPaolo Abeni __mptcp_flush_join_list(sk, &join_list); 33192e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3320c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 33212d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 33222d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3323c2e6048fSPaolo Abeni 3324c2e6048fSPaolo Abeni cond_resched(); 33256e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 33266e628cd3SPaolo Abeni } 3327c2e6048fSPaolo Abeni 332865a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 332965a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 333017ebf8a4SXiang Yang if (unlikely(msk->cb_flags)) { 3331490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 333265a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3333490274b4SPaolo Abeni */ 3334e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3335490274b4SPaolo Abeni __mptcp_set_connected(sk); 3336e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 333715cc1045SPaolo Abeni __mptcp_error_report(sk); 333843f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 333943f5b111SPaolo Abeni msk->last_snd = NULL; 334065a569b0SPaolo Abeni } 33416e628cd3SPaolo Abeni 334287952603SPaolo Abeni __mptcp_update_rmem(sk); 334314c441b5SPaolo Abeni } 334414c441b5SPaolo Abeni 3345bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3346bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3347bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3348bcd97734SPaolo Abeni */ 3349bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3350bcd97734SPaolo Abeni { 3351bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3352bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3353bcd97734SPaolo Abeni unsigned long timeout; 3354bcd97734SPaolo Abeni 3355bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3356bcd97734SPaolo Abeni return; 3357bcd97734SPaolo Abeni 3358bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3359bcd97734SPaolo Abeni if (tp->srtt_us) 3360bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3361bcd97734SPaolo Abeni else 3362bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3363bcd97734SPaolo Abeni timeout += jiffies; 3364bcd97734SPaolo Abeni 3365bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3366bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3367bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3368bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3369bcd97734SPaolo Abeni } 3370bcd97734SPaolo Abeni 3371b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3372b19bc294SPaolo Abeni { 3373b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3374b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3375b19bc294SPaolo Abeni 3376bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3377b19bc294SPaolo Abeni mptcp_data_lock(sk); 3378b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 337973a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3380b19bc294SPaolo Abeni else 3381e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3382b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3383bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3384bcd97734SPaolo Abeni } 3385bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3386bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3387bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3388bcd97734SPaolo Abeni } 3389b19bc294SPaolo Abeni } 3390b19bc294SPaolo Abeni 33912c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 33922c5ebd00SPaolo Abeni { 33932c5ebd00SPaolo Abeni /* should never be called, 33942c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 33952c5ebd00SPaolo Abeni */ 33962c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 33972c5ebd00SPaolo Abeni return 0; 33982c5ebd00SPaolo Abeni } 33992c5ebd00SPaolo Abeni 34002c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 34012c5ebd00SPaolo Abeni { 34022c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 34032c5ebd00SPaolo Abeni } 34042c5ebd00SPaolo Abeni 3405cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3406f870fa0bSMat Martineau { 3407f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3408cec37a6eSPeter Krystad struct socket *ssock; 3409f870fa0bSMat Martineau 341061761231SPaolo Abeni ssock = msk->subflow; 3411cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3412cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3413cec37a6eSPeter Krystad return -EINVAL; 3414f870fa0bSMat Martineau 3415cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3416cec37a6eSPeter Krystad } 3417f870fa0bSMat Martineau 3418cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3419cec37a6eSPeter Krystad { 3420cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3421cec37a6eSPeter Krystad struct mptcp_sock *msk; 3422cec37a6eSPeter Krystad struct sock *sk; 3423f870fa0bSMat Martineau 3424cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3425cec37a6eSPeter Krystad sk = subflow->conn; 3426cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3427cec37a6eSPeter Krystad 3428648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3429648ef4b8SMat Martineau 3430b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3431648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 34326d0060f6SMat Martineau 3433cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3434cec37a6eSPeter Krystad * accessing the field below 3435cec37a6eSPeter Krystad */ 3436cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 34376d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3438eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 34397439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34401b1c7a0eSPeter Krystad 34416c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3442a6b118feSFlorian Westphal 3443a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3444f870fa0bSMat Martineau } 3445f870fa0bSMat Martineau 3446866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3447cf7da0d6SPeter Krystad { 3448cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3449cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3450cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3451cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3452cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3453cf7da0d6SPeter Krystad } 3454cf7da0d6SPeter Krystad 3455e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3456f296234cSPeter Krystad { 3457e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3458f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3459f296234cSPeter Krystad struct sock *parent = (void *)msk; 34603e501490SPaolo Abeni bool ret = true; 3461f296234cSPeter Krystad 3462f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3463f296234cSPeter Krystad 3464f296234cSPeter Krystad /* mptcp socket already closing? */ 3465dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3466dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3467f296234cSPeter Krystad return false; 3468dc87efdbSFlorian Westphal } 3469f296234cSPeter Krystad 347055b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 347155b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 347255b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 347355b47ca7SPaolo Abeni return true; 347455b47ca7SPaolo Abeni } 3475f296234cSPeter Krystad 34763e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 34773e501490SPaolo Abeni goto err_prohibited; 347810f6d46cSPaolo Abeni 347955b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 34803e501490SPaolo Abeni * handle it 348110f6d46cSPaolo Abeni */ 34823e501490SPaolo Abeni mptcp_data_lock(parent); 34833e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 34843e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 34853e501490SPaolo Abeni if (ret) { 3486e16163b6SPaolo Abeni sock_hold(ssk); 34873e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3488e16163b6SPaolo Abeni } 34893e501490SPaolo Abeni } else { 34903e501490SPaolo Abeni sock_hold(ssk); 34913e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3492e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 34933e501490SPaolo Abeni } 34943e501490SPaolo Abeni mptcp_data_unlock(parent); 34953e501490SPaolo Abeni 3496dc87efdbSFlorian Westphal if (!ret) { 34973e501490SPaolo Abeni err_prohibited: 3498dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 349910f6d46cSPaolo Abeni return false; 3500dc87efdbSFlorian Westphal } 350110f6d46cSPaolo Abeni 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 3547e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) 3548644807e3SFlorian Westphal { 3549644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3550644807e3SFlorian Westphal bool slow; 3551644807e3SFlorian Westphal 3552644807e3SFlorian Westphal switch (cmd) { 3553644807e3SFlorian Westphal case SIOCINQ: 3554644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3555644807e3SFlorian Westphal return -EINVAL; 3556644807e3SFlorian Westphal 3557644807e3SFlorian Westphal lock_sock(sk); 3558644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3559e1d001faSBreno Leitao *karg = mptcp_inq_hint(sk); 3560644807e3SFlorian Westphal release_sock(sk); 3561644807e3SFlorian Westphal break; 3562644807e3SFlorian Westphal case SIOCOUTQ: 3563644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3564e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3565644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3566644807e3SFlorian Westphal break; 3567644807e3SFlorian Westphal case SIOCOUTQNSD: 3568644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3569e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, msk->snd_nxt); 3570644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3571644807e3SFlorian Westphal break; 3572644807e3SFlorian Westphal default: 3573644807e3SFlorian Westphal return -ENOIOCTLCMD; 3574644807e3SFlorian Westphal } 3575644807e3SFlorian Westphal 3576e1d001faSBreno Leitao return 0; 3577644807e3SFlorian Westphal } 3578644807e3SFlorian Westphal 357954f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 358054f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 358154f1944eSPaolo Abeni { 358254f1944eSPaolo Abeni subflow->request_mptcp = 0; 358354f1944eSPaolo Abeni __mptcp_do_fallback(msk); 358454f1944eSPaolo Abeni } 358554f1944eSPaolo Abeni 358654f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 358754f1944eSPaolo Abeni { 358854f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 358954f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 359054f1944eSPaolo Abeni struct socket *ssock; 359154f1944eSPaolo Abeni int err = -EINVAL; 359254f1944eSPaolo Abeni 359354f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3594ddb1a072SPaolo Abeni if (IS_ERR(ssock)) 3595ddb1a072SPaolo Abeni return PTR_ERR(ssock); 359654f1944eSPaolo Abeni 359754f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 359854f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 359954f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 360054f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 360154f1944eSPaolo Abeni * TCP option space. 360254f1944eSPaolo Abeni */ 360354f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 360454f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 360554f1944eSPaolo Abeni #endif 360654f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 360754f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 360854f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 360954f1944eSPaolo Abeni } 361054f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 361154f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 361254f1944eSPaolo Abeni 361354f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 361454f1944eSPaolo Abeni * acquired the subflow socket lock, too. 361554f1944eSPaolo Abeni */ 36167d803344SPaolo Abeni if (msk->fastopening) 3617786fc124SPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1); 361854f1944eSPaolo Abeni else 3619786fc124SPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK); 362054f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 362154f1944eSPaolo Abeni 362254f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 362354f1944eSPaolo Abeni * subflow_finish_connect() 362454f1944eSPaolo Abeni */ 362554f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 3626*131a6277SPaolo Abeni /* avoid leaving a dangling token in an unconnected socket */ 3627*131a6277SPaolo Abeni mptcp_token_destroy(msk); 362854f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 362954f1944eSPaolo Abeni return err; 363054f1944eSPaolo Abeni } 363154f1944eSPaolo Abeni 363254f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 363354f1944eSPaolo Abeni 3634786fc124SPaolo Abeni /* silence EINPROGRESS and let the caller inet_stream_connect 3635786fc124SPaolo Abeni * handle the connection in progress 363654f1944eSPaolo Abeni */ 3637786fc124SPaolo Abeni return 0; 363854f1944eSPaolo Abeni } 363954f1944eSPaolo Abeni 3640f870fa0bSMat Martineau static struct proto mptcp_prot = { 3641f870fa0bSMat Martineau .name = "MPTCP", 3642f870fa0bSMat Martineau .owner = THIS_MODULE, 3643f870fa0bSMat Martineau .init = mptcp_init_sock, 364454f1944eSPaolo Abeni .connect = mptcp_connect, 364518b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3646f870fa0bSMat Martineau .close = mptcp_close, 3647cf7da0d6SPeter Krystad .accept = mptcp_accept, 3648717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3649717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 365076e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 365179c0949eSPeter Krystad .destroy = mptcp_destroy, 3652f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3653644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3654f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 365514c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36562c5ebd00SPaolo Abeni .hash = mptcp_hash, 36572c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3658cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36596511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3660d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36610defbb0aSEric Dumazet 3662d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36630defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36640defbb0aSEric Dumazet 3665d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3666d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3667989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3668d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3669f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36702c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3671f870fa0bSMat Martineau .no_autobind = true, 3672f870fa0bSMat Martineau }; 3673f870fa0bSMat Martineau 36742303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 36752303f994SPeter Krystad { 36762303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 36772303f994SPeter Krystad struct socket *ssock; 3678cf7da0d6SPeter Krystad int err; 36792303f994SPeter Krystad 36802303f994SPeter Krystad lock_sock(sock->sk); 3681fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3682ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3683ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 36842303f994SPeter Krystad goto unlock; 36852303f994SPeter Krystad } 36862303f994SPeter Krystad 36871ded5e5aSEric Dumazet err = READ_ONCE(ssock->ops)->bind(ssock, uaddr, addr_len); 3688cf7da0d6SPeter Krystad if (!err) 3689cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 36902303f994SPeter Krystad 36912303f994SPeter Krystad unlock: 36922303f994SPeter Krystad release_sock(sock->sk); 36932303f994SPeter Krystad return err; 36942303f994SPeter Krystad } 36952303f994SPeter Krystad 3696cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3697cf7da0d6SPeter Krystad { 3698cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3699cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 3700cf7da0d6SPeter Krystad struct socket *ssock; 3701cf7da0d6SPeter Krystad int err; 3702cf7da0d6SPeter Krystad 3703cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3704cf7da0d6SPeter Krystad 3705cfdcfeedSMenglong Dong lock_sock(sk); 37060226436aSPaolo Abeni 37070226436aSPaolo Abeni err = -EINVAL; 37080226436aSPaolo Abeni if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 37090226436aSPaolo Abeni goto unlock; 37100226436aSPaolo Abeni 3711fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3712ddb1a072SPaolo Abeni if (IS_ERR(ssock)) { 3713ddb1a072SPaolo Abeni err = PTR_ERR(ssock); 3714cf7da0d6SPeter Krystad goto unlock; 3715cf7da0d6SPeter Krystad } 3716cf7da0d6SPeter Krystad 3717cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3718cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 37195e20087dSFlorian Westphal 37201ded5e5aSEric Dumazet err = READ_ONCE(ssock->ops)->listen(ssock, backlog); 3721cfdcfeedSMenglong Dong inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3722c558246eSMenglong Dong if (!err) { 3723c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3724cfdcfeedSMenglong Dong mptcp_copy_inaddrs(sk, ssock->sk); 3725f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 372621d9b73aSPaolo Abeni } 3727f8c9dfbdSGeliang Tang 3728cf7da0d6SPeter Krystad unlock: 3729cfdcfeedSMenglong Dong release_sock(sk); 3730cf7da0d6SPeter Krystad return err; 3731cf7da0d6SPeter Krystad } 3732cf7da0d6SPeter Krystad 3733cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3734cf7da0d6SPeter Krystad int flags, bool kern) 3735cf7da0d6SPeter Krystad { 3736cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3737cf7da0d6SPeter Krystad struct socket *ssock; 3738e76c8ef5SPaolo Abeni struct sock *newsk; 3739cf7da0d6SPeter Krystad int err; 3740cf7da0d6SPeter Krystad 3741cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3742cf7da0d6SPeter Krystad 37435b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 374461761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 374561761231SPaolo Abeni */ 37465b825727SPaolo Abeni ssock = READ_ONCE(msk->subflow); 3747cf7da0d6SPeter Krystad if (!ssock) 374871ba088cSPaolo Abeni return -EINVAL; 3749cf7da0d6SPeter Krystad 3750e76c8ef5SPaolo Abeni newsk = mptcp_accept(sock->sk, flags, &err, kern); 3751e76c8ef5SPaolo Abeni if (!newsk) 3752e76c8ef5SPaolo Abeni return err; 3753e76c8ef5SPaolo Abeni 3754e76c8ef5SPaolo Abeni lock_sock(newsk); 3755e76c8ef5SPaolo Abeni 3756e76c8ef5SPaolo Abeni __inet_accept(sock, newsock, newsk); 3757e76c8ef5SPaolo Abeni if (!mptcp_is_tcpsk(newsock->sk)) { 3758e76c8ef5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(newsk); 3759cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37600397c6d8SPaolo Abeni 3761a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3762b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3763a5ef058dSPaolo Abeni 3764cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3765cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3766cf7da0d6SPeter Krystad */ 3767190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3768cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3769cf7da0d6SPeter Krystad 3770cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3771cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3772cf7da0d6SPeter Krystad } 377363740448SPaolo Abeni 377463740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 377563740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 377663740448SPaolo Abeni */ 377763740448SPaolo Abeni if (msk->first && 377863740448SPaolo Abeni unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 377963740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 378063740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 378163740448SPaolo Abeni if (unlikely(list_empty(&msk->conn_list))) 378263740448SPaolo Abeni inet_sk_state_store(newsk, TCP_CLOSE); 378363740448SPaolo Abeni } 3784cf7da0d6SPeter Krystad } 3785e76c8ef5SPaolo Abeni release_sock(newsk); 3786cf7da0d6SPeter Krystad 3787e76c8ef5SPaolo Abeni return 0; 3788cf7da0d6SPeter Krystad } 3789cf7da0d6SPeter Krystad 37908edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 37918edf0864SFlorian Westphal { 37928edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 37938edf0864SFlorian Westphal 37948edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 37958edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 37968edf0864SFlorian Westphal 37975cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 37986e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 37996e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 38006e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 38018edf0864SFlorian Westphal 38026e628cd3SPaolo Abeni return 0; 38038edf0864SFlorian Westphal } 38048edf0864SFlorian Westphal 38052303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 38062303f994SPeter Krystad struct poll_table_struct *wait) 38072303f994SPeter Krystad { 38081891c4a0SFlorian Westphal struct sock *sk = sock->sk; 38098ab183deSPaolo Abeni struct mptcp_sock *msk; 38102303f994SPeter Krystad __poll_t mask = 0; 38116b9831bfSPaolo Abeni u8 shutdown; 38128a05661bSPaolo Abeni int state; 38132303f994SPeter Krystad 38141891c4a0SFlorian Westphal msk = mptcp_sk(sk); 38151891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 38161891c4a0SFlorian Westphal 38178a05661bSPaolo Abeni state = inet_sk_state_load(sk); 38186719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 381971ba088cSPaolo Abeni if (state == TCP_LISTEN) { 38205b825727SPaolo Abeni struct socket *ssock = READ_ONCE(msk->subflow); 38215b825727SPaolo Abeni 38225b825727SPaolo Abeni if (WARN_ON_ONCE(!ssock || !ssock->sk)) 382371ba088cSPaolo Abeni return 0; 382471ba088cSPaolo Abeni 38255b825727SPaolo Abeni return inet_csk_listen_poll(ssock->sk); 382671ba088cSPaolo Abeni } 38278a05661bSPaolo Abeni 38286b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 38296b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 38306b9831bfSPaolo Abeni mask |= EPOLLHUP; 38316b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 38326b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 38336b9831bfSPaolo Abeni 38348a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 38358a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 38366b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 38376b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 38386b9831bfSPaolo Abeni else 38398edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3840a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3841a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3842a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 38438a05661bSPaolo Abeni } 38441891c4a0SFlorian Westphal 3845d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 384615cc1045SPaolo Abeni smp_rmb(); 38479ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 384815cc1045SPaolo Abeni mask |= EPOLLERR; 384915cc1045SPaolo Abeni 38502303f994SPeter Krystad return mask; 38512303f994SPeter Krystad } 38522303f994SPeter Krystad 3853e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3854e42f1ac6SFlorian Westphal .family = PF_INET, 3855e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38560a3cc579SPaolo Abeni .release = inet_release, 3857e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3858786fc124SPaolo Abeni .connect = inet_stream_connect, 3859e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3860e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3861d2f77c53SPaolo Abeni .getname = inet_getname, 3862e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3863e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3864e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3865e42f1ac6SFlorian Westphal .listen = mptcp_listen, 386676e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3867e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3868e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3869e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3870e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3871e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3872e42f1ac6SFlorian Westphal }; 38732303f994SPeter Krystad 3874f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3875f870fa0bSMat Martineau .type = SOCK_STREAM, 3876f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3877f870fa0bSMat Martineau .prot = &mptcp_prot, 38782303f994SPeter Krystad .ops = &mptcp_stream_ops, 38792303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3880f870fa0bSMat Martineau }; 3881f870fa0bSMat Martineau 3882b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3883b19bc294SPaolo Abeni { 3884b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3885b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3886b19bc294SPaolo Abeni int work_done = 0; 3887b19bc294SPaolo Abeni 3888b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3889b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3890b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3891b19bc294SPaolo Abeni 3892b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3893b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3894b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3895b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3896b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3897b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3898b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3899b19bc294SPaolo Abeni * CPU that scheduled it. 3900b19bc294SPaolo Abeni */ 3901b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3902b19bc294SPaolo Abeni sock_put(ssk); 3903b19bc294SPaolo Abeni 3904b19bc294SPaolo Abeni if (++work_done == budget) 3905b19bc294SPaolo Abeni return budget; 3906b19bc294SPaolo Abeni } 3907b19bc294SPaolo Abeni 3908b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3909b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3910b19bc294SPaolo Abeni */ 3911b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3912b19bc294SPaolo Abeni return work_done; 3913b19bc294SPaolo Abeni } 3914b19bc294SPaolo Abeni 3915d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3916f870fa0bSMat Martineau { 3917b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3918b19bc294SPaolo Abeni int cpu; 3919b19bc294SPaolo Abeni 39202303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 39212303f994SPeter Krystad 3922d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3923d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3924d027236cSPaolo Abeni 3925b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3926b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3927b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3928b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 392916d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 393016d083e2SJakub Kicinski mptcp_napi_poll); 3931b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3932b19bc294SPaolo Abeni } 3933b19bc294SPaolo Abeni 39342303f994SPeter Krystad mptcp_subflow_init(); 39351b1c7a0eSPeter Krystad mptcp_pm_init(); 39362c5ebd00SPaolo Abeni mptcp_token_init(); 39372303f994SPeter Krystad 3938f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3939f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3940f870fa0bSMat Martineau 3941f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 39426771bfd9SFlorian Westphal 39436771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3944f870fa0bSMat Martineau } 3945f870fa0bSMat Martineau 3946f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3947e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3948e42f1ac6SFlorian Westphal .family = PF_INET6, 3949e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39500a3cc579SPaolo Abeni .release = inet6_release, 3951e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3952786fc124SPaolo Abeni .connect = inet_stream_connect, 3953e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3954e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3955d2f77c53SPaolo Abeni .getname = inet6_getname, 3956e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3957e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3958e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3959e42f1ac6SFlorian Westphal .listen = mptcp_listen, 396076e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3961e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3962e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3963e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3964e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3965e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3966e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39673986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3968e42f1ac6SFlorian Westphal #endif 3969e42f1ac6SFlorian Westphal }; 3970e42f1ac6SFlorian Westphal 3971f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3972f870fa0bSMat Martineau 3973f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3974f870fa0bSMat Martineau .type = SOCK_STREAM, 3975f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3976f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39772303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3978f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3979f870fa0bSMat Martineau }; 3980f870fa0bSMat Martineau 3981d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3982f870fa0bSMat Martineau { 3983f870fa0bSMat Martineau int err; 3984f870fa0bSMat Martineau 3985f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3986f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3987f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 3988b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3989f5f80e32SEric Dumazet mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np); 3990f870fa0bSMat Martineau 3991f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3992f870fa0bSMat Martineau if (err) 3993f870fa0bSMat Martineau return err; 3994f870fa0bSMat Martineau 3995f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3996f870fa0bSMat Martineau if (err) 3997f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3998f870fa0bSMat Martineau 3999f870fa0bSMat Martineau return err; 4000f870fa0bSMat Martineau } 4001f870fa0bSMat Martineau #endif 4002