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 396771bfd9SFlorian Westphal struct mptcp_skb_cb { 40ab174ad8SPaolo Abeni u64 map_seq; 41ab174ad8SPaolo Abeni u64 end_seq; 426771bfd9SFlorian Westphal u32 offset; 43b7f653b2SFlorian Westphal u8 has_rxtstamp:1; 446771bfd9SFlorian Westphal }; 456771bfd9SFlorian Westphal 466771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 476771bfd9SFlorian Westphal 48b7f653b2SFlorian Westphal enum { 49b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 502c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 51b7f653b2SFlorian Westphal }; 52b7f653b2SFlorian Westphal 5391b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 54d027236cSPaolo Abeni 55e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 56d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 57e16163b6SPaolo Abeni 58b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 59b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 60b19bc294SPaolo Abeni 612303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 622303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 632303f994SPeter Krystad * Otherwise return NULL. 642303f994SPeter Krystad */ 651729cf18SGeliang Tang struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 662303f994SPeter Krystad { 67d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 682303f994SPeter Krystad return NULL; 692303f994SPeter Krystad 702303f994SPeter Krystad return msk->subflow; 712303f994SPeter Krystad } 722303f994SPeter Krystad 736f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 746f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 756f8a612aSFlorian Westphal { 767439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 776f8a612aSFlorian Westphal } 786f8a612aSFlorian Westphal 79d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 800b4f33deSFlorian Westphal { 810b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 820b4f33deSFlorian Westphal 830b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 840b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 850b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 860b4f33deSFlorian Westphal * not an mptcp one. 870b4f33deSFlorian Westphal * 880b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 890b4f33deSFlorian Westphal * bypass mptcp. 900b4f33deSFlorian Westphal */ 910b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 92d2f77c53SPaolo Abeni return true; 930b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 940b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 950b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 96d2f77c53SPaolo Abeni return true; 970b4f33deSFlorian Westphal #endif 980b4f33deSFlorian Westphal } 990b4f33deSFlorian Westphal 100d2f77c53SPaolo Abeni return false; 1010b4f33deSFlorian Westphal } 1020b4f33deSFlorian Westphal 103fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 1042303f994SPeter Krystad { 1052303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1062303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1072303f994SPeter Krystad struct socket *ssock; 1082303f994SPeter Krystad int err; 1092303f994SPeter Krystad 1102303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1112303f994SPeter Krystad if (err) 112fa68018dSPaolo Abeni return err; 1132303f994SPeter Krystad 1148ab183deSPaolo Abeni msk->first = ssock->sk; 1152303f994SPeter Krystad msk->subflow = ssock; 1162303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 117cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 118e16163b6SPaolo Abeni sock_hold(ssock->sk); 1192303f994SPeter Krystad subflow->request_mptcp = 1; 120866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 121e1ff9e82SDavide Caratti 122fa68018dSPaolo Abeni return 0; 1232303f994SPeter Krystad } 1242303f994SPeter Krystad 125ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 126ab174ad8SPaolo Abeni { 127ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 128ab174ad8SPaolo Abeni __kfree_skb(skb); 129ab174ad8SPaolo Abeni } 130ab174ad8SPaolo Abeni 1316511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1326511882cSPaolo Abeni { 1336511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1346511882cSPaolo Abeni } 1356511882cSPaolo Abeni 1368268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1378268ed4cSPaolo Abeni struct sk_buff *from) 1388268ed4cSPaolo Abeni { 1398268ed4cSPaolo Abeni bool fragstolen; 1408268ed4cSPaolo Abeni int delta; 1418268ed4cSPaolo Abeni 1428268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1438268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1448268ed4cSPaolo Abeni return false; 1458268ed4cSPaolo Abeni 14606242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 14706242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 14806242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 149ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1508268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1518268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1526511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1538268ed4cSPaolo Abeni return true; 1548268ed4cSPaolo Abeni } 1558268ed4cSPaolo Abeni 156ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 157ab174ad8SPaolo Abeni struct sk_buff *from) 1586771bfd9SFlorian Westphal { 159ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 160ab174ad8SPaolo Abeni return false; 161ab174ad8SPaolo Abeni 162ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 163ab174ad8SPaolo Abeni } 164ab174ad8SPaolo Abeni 1656511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1666511882cSPaolo Abeni { 1676511882cSPaolo Abeni amount >>= SK_MEM_QUANTUM_SHIFT; 1686511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT; 1696511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1706511882cSPaolo Abeni } 1716511882cSPaolo Abeni 1726511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1736511882cSPaolo Abeni { 1746511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1756511882cSPaolo Abeni int reclaimable; 1766511882cSPaolo Abeni 1776511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1786511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1796511882cSPaolo Abeni 1806511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 1816511882cSPaolo Abeni if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD)) 1826511882cSPaolo Abeni __mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK); 1836511882cSPaolo Abeni } 1846511882cSPaolo Abeni 1856511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 1866511882cSPaolo Abeni { 1876511882cSPaolo Abeni unsigned int len = skb->truesize; 1886511882cSPaolo Abeni struct sock *sk = skb->sk; 1896511882cSPaolo Abeni 1906511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 1916511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 1926511882cSPaolo Abeni } 1936511882cSPaolo Abeni 1946511882cSPaolo Abeni static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 1956511882cSPaolo Abeni { 1966511882cSPaolo Abeni skb_orphan(skb); 1976511882cSPaolo Abeni skb->sk = sk; 1986511882cSPaolo Abeni skb->destructor = mptcp_rfree; 1996511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2006511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2016511882cSPaolo Abeni } 2026511882cSPaolo Abeni 203ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 204ab174ad8SPaolo Abeni * - use mptcp seqs 205ab174ad8SPaolo Abeni * - don't cope with sacks 206ab174ad8SPaolo Abeni */ 207ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 208ab174ad8SPaolo Abeni { 209ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 210ab174ad8SPaolo Abeni struct rb_node **p, *parent; 211ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 212ab174ad8SPaolo Abeni struct sk_buff *skb1; 213ab174ad8SPaolo Abeni 214ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 215ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 216fa3fe2b1SFlorian Westphal max_seq = READ_ONCE(msk->rcv_wnd_sent); 217ab174ad8SPaolo Abeni 21806242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 21906242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 220fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 221ab174ad8SPaolo Abeni /* out of window */ 222ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 223fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 224fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 225fa3fe2b1SFlorian Westphal (unsigned long long)msk->rcv_wnd_sent); 22606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 227ab174ad8SPaolo Abeni return; 228ab174ad8SPaolo Abeni } 229ab174ad8SPaolo Abeni 230ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 23106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 232ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 233ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 234ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 235ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 236ab174ad8SPaolo Abeni goto end; 237ab174ad8SPaolo Abeni } 238ab174ad8SPaolo Abeni 239ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 240ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 241ab174ad8SPaolo Abeni */ 24206242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 24306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 24406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 245ab174ad8SPaolo Abeni return; 24606242e44SPaolo Abeni } 247ab174ad8SPaolo Abeni 248ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 249ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 25006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 251ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 252ab174ad8SPaolo Abeni p = &parent->rb_right; 253ab174ad8SPaolo Abeni goto insert; 254ab174ad8SPaolo Abeni } 255ab174ad8SPaolo Abeni 256ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 257ab174ad8SPaolo Abeni parent = NULL; 258ab174ad8SPaolo Abeni while (*p) { 259ab174ad8SPaolo Abeni parent = *p; 260ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 261ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 262ab174ad8SPaolo Abeni p = &parent->rb_left; 263ab174ad8SPaolo Abeni continue; 264ab174ad8SPaolo Abeni } 265ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 266ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 267ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 268ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 26906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 270ab174ad8SPaolo Abeni return; 271ab174ad8SPaolo Abeni } 272ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 273ab174ad8SPaolo Abeni /* partial overlap: 274ab174ad8SPaolo Abeni * | skb | 275ab174ad8SPaolo Abeni * | skb1 | 276ab174ad8SPaolo Abeni * continue traversing 277ab174ad8SPaolo Abeni */ 278ab174ad8SPaolo Abeni } else { 279ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 280ab174ad8SPaolo Abeni * Replace skb1 with skb. 281ab174ad8SPaolo Abeni */ 282ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 283ab174ad8SPaolo Abeni &msk->out_of_order_queue); 284ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 28506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 286ab174ad8SPaolo Abeni goto merge_right; 287ab174ad8SPaolo Abeni } 288ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 28906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 290ab174ad8SPaolo Abeni return; 291ab174ad8SPaolo Abeni } 292ab174ad8SPaolo Abeni p = &parent->rb_right; 293ab174ad8SPaolo Abeni } 29406242e44SPaolo Abeni 295ab174ad8SPaolo Abeni insert: 296ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 297ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 298ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 299ab174ad8SPaolo Abeni 300ab174ad8SPaolo Abeni merge_right: 301ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 302ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 303ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 304ab174ad8SPaolo Abeni break; 305ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 306ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 30706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 308ab174ad8SPaolo Abeni } 309ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 310ab174ad8SPaolo Abeni if (!skb1) 311ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 312ab174ad8SPaolo Abeni 313ab174ad8SPaolo Abeni end: 314ab174ad8SPaolo Abeni skb_condense(skb); 3156511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3166511882cSPaolo Abeni } 3176511882cSPaolo Abeni 3186511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3196511882cSPaolo Abeni { 3206511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3216511882cSPaolo Abeni int amt, amount; 3226511882cSPaolo Abeni 3236511882cSPaolo Abeni if (size < msk->rmem_fwd_alloc) 3246511882cSPaolo Abeni return true; 3256511882cSPaolo Abeni 3266511882cSPaolo Abeni amt = sk_mem_pages(size); 3276511882cSPaolo Abeni amount = amt << SK_MEM_QUANTUM_SHIFT; 3286511882cSPaolo Abeni msk->rmem_fwd_alloc += amount; 3296511882cSPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) { 3306511882cSPaolo Abeni if (ssk->sk_forward_alloc < amount) { 3316511882cSPaolo Abeni msk->rmem_fwd_alloc -= amount; 3326511882cSPaolo Abeni return false; 3336511882cSPaolo Abeni } 3346511882cSPaolo Abeni 3356511882cSPaolo Abeni ssk->sk_forward_alloc -= amount; 3366511882cSPaolo Abeni } 3376511882cSPaolo Abeni return true; 338ab174ad8SPaolo Abeni } 339ab174ad8SPaolo Abeni 340ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 341ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 342ab174ad8SPaolo Abeni size_t copy_len) 343ab174ad8SPaolo Abeni { 344ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3456771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3464e637c70SFlorian Westphal struct sk_buff *tail; 347b7f653b2SFlorian Westphal bool has_rxtstamp; 3486771bfd9SFlorian Westphal 3496771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3504e637c70SFlorian Westphal 3514e637c70SFlorian Westphal skb_ext_reset(skb); 3524e637c70SFlorian Westphal skb_orphan(skb); 3534e637c70SFlorian Westphal 3549c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3556511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3569c3f94e1SPaolo Abeni goto drop; 35772f96132SPaolo Abeni 358b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 359b7f653b2SFlorian Westphal 360ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 361ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 362ab174ad8SPaolo Abeni * value 363ab174ad8SPaolo Abeni */ 364ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 365ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 366ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 367b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 368ab174ad8SPaolo Abeni 369ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 370ab174ad8SPaolo Abeni /* in sequence */ 371917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3724e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3738268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 374ab174ad8SPaolo Abeni return true; 3754e637c70SFlorian Westphal 3766511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3776771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 378ab174ad8SPaolo Abeni return true; 379ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 380ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 381ab174ad8SPaolo Abeni return false; 382ab174ad8SPaolo Abeni } 383ab174ad8SPaolo Abeni 384ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 385ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 386ab174ad8SPaolo Abeni */ 38706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3889c3f94e1SPaolo Abeni drop: 389ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 390ab174ad8SPaolo Abeni return false; 3916771bfd9SFlorian Westphal } 3926771bfd9SFlorian Westphal 39316a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 39416a9a9daSMat Martineau { 39516a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 39616a9a9daSMat Martineau 39716a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 39816a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 39916a9a9daSMat Martineau } 40016a9a9daSMat Martineau 401e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 402e16163b6SPaolo Abeni { 403e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 404e16163b6SPaolo Abeni return; 405e16163b6SPaolo Abeni 406e16163b6SPaolo Abeni sk->sk_state_change(sk); 407e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 408e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 409e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 410e16163b6SPaolo Abeni else 411e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 412e16163b6SPaolo Abeni } 413e16163b6SPaolo Abeni 4146e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4156e628cd3SPaolo Abeni { 4166e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4176e628cd3SPaolo Abeni 4186e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 4196e628cd3SPaolo Abeni ((1 << sk->sk_state) & 4206e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4216e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4226e628cd3SPaolo Abeni } 4236e628cd3SPaolo Abeni 42416a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 42516a9a9daSMat Martineau { 42616a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 42716a9a9daSMat Martineau 42816a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4296e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 43016a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 43116a9a9daSMat Martineau 43216a9a9daSMat Martineau switch (sk->sk_state) { 43316a9a9daSMat Martineau case TCP_FIN_WAIT1: 43416a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 43516a9a9daSMat Martineau break; 43616a9a9daSMat Martineau case TCP_CLOSING: 43716a9a9daSMat Martineau case TCP_LAST_ACK: 43816a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 43916a9a9daSMat Martineau break; 44016a9a9daSMat Martineau } 44116a9a9daSMat Martineau 442e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 44316a9a9daSMat Martineau } 44416a9a9daSMat Martineau } 44516a9a9daSMat Martineau 4463721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4473721b9b6SMat Martineau { 4483721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4493721b9b6SMat Martineau 4503721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4513721b9b6SMat Martineau ((1 << sk->sk_state) & 4523721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4533721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4543721b9b6SMat Martineau 4553721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4563721b9b6SMat Martineau if (seq) 4573721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4583721b9b6SMat Martineau 4593721b9b6SMat Martineau return true; 4603721b9b6SMat Martineau } 4613721b9b6SMat Martineau } 4623721b9b6SMat Martineau 4633721b9b6SMat Martineau return false; 4643721b9b6SMat Martineau } 4653721b9b6SMat Martineau 4666477dd39SMat Martineau static void mptcp_set_datafin_timeout(const struct sock *sk) 4676477dd39SMat Martineau { 4686477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 4696477dd39SMat Martineau 4706477dd39SMat Martineau mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX, 4716477dd39SMat Martineau TCP_RTO_MIN << icsk->icsk_retransmits); 4726477dd39SMat Martineau } 4736477dd39SMat Martineau 47433d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4753721b9b6SMat Martineau { 4763721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4773721b9b6SMat Martineau } 4783721b9b6SMat Martineau 47933d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 48033d41c9cSPaolo Abeni { 48133d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 48233d41c9cSPaolo Abeni 48371b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 48471b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 48533d41c9cSPaolo Abeni } 48633d41c9cSPaolo Abeni 48733d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 48833d41c9cSPaolo Abeni { 48933d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 49033d41c9cSPaolo Abeni long tout = 0; 49133d41c9cSPaolo Abeni 49233d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 49333d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 49433d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 49533d41c9cSPaolo Abeni } 49633d41c9cSPaolo Abeni 497fd897679SPaolo Abeni static bool tcp_can_send_ack(const struct sock *ssk) 498fd897679SPaolo Abeni { 499fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 50020bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 501fd897679SPaolo Abeni } 502fd897679SPaolo Abeni 503340fa666SMat Martineau void mptcp_subflow_send_ack(struct sock *ssk) 5047ed90803SPaolo Abeni { 50575e908c3SPaolo Abeni bool slow; 5067ed90803SPaolo Abeni 50775e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 508fd897679SPaolo Abeni if (tcp_can_send_ack(ssk)) 5097ed90803SPaolo Abeni tcp_send_ack(ssk); 51075e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 511fd897679SPaolo Abeni } 512340fa666SMat Martineau 513340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 514340fa666SMat Martineau { 515340fa666SMat Martineau struct mptcp_subflow_context *subflow; 516340fa666SMat Martineau 517340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 518340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 519ea4ca586SPaolo Abeni } 520ea4ca586SPaolo Abeni 521fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 522fd897679SPaolo Abeni { 52375e908c3SPaolo Abeni bool slow; 524fd897679SPaolo Abeni 52575e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 526fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 527fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 52875e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 529fde56eeaSPaolo Abeni } 530fde56eeaSPaolo Abeni 531fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 532fde56eeaSPaolo Abeni { 533fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 534c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 535fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 536fde56eeaSPaolo Abeni 537fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 538fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 539fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 540fde56eeaSPaolo Abeni (rx_empty && ack_pending & 541fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 542ea4ca586SPaolo Abeni } 543fd897679SPaolo Abeni 544fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 545fd897679SPaolo Abeni { 546e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 547fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 548e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 549fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 550fde56eeaSPaolo Abeni bool cleanup, rx_empty; 551e3859603SPaolo Abeni 552fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 553ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 554fd897679SPaolo Abeni 555fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 556fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 557fd897679SPaolo Abeni 558fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 559fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5607ed90803SPaolo Abeni } 5617ed90803SPaolo Abeni } 5627ed90803SPaolo Abeni 5637ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5643721b9b6SMat Martineau { 5653721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5663721b9b6SMat Martineau u64 rcv_data_fin_seq; 5677ed90803SPaolo Abeni bool ret = false; 5683721b9b6SMat Martineau 569781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5707ed90803SPaolo Abeni return ret; 5713721b9b6SMat Martineau 5723721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5733721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5743721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5753721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5763721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5773721b9b6SMat Martineau * the msk state. 5783721b9b6SMat Martineau * 5793721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5803721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5813721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5823721b9b6SMat Martineau * when catching up. 5833721b9b6SMat Martineau */ 5843721b9b6SMat Martineau 5853721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 586917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5873721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5883721b9b6SMat Martineau 5893721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 59016a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 5913721b9b6SMat Martineau 5923721b9b6SMat Martineau switch (sk->sk_state) { 5933721b9b6SMat Martineau case TCP_ESTABLISHED: 5943721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 5953721b9b6SMat Martineau break; 5963721b9b6SMat Martineau case TCP_FIN_WAIT1: 5973721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 5983721b9b6SMat Martineau break; 5993721b9b6SMat Martineau case TCP_FIN_WAIT2: 6003721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6013721b9b6SMat Martineau break; 6023721b9b6SMat Martineau default: 6033721b9b6SMat Martineau /* Other states not expected */ 6043721b9b6SMat Martineau WARN_ON_ONCE(1); 6053721b9b6SMat Martineau break; 6063721b9b6SMat Martineau } 6073721b9b6SMat Martineau 6087ed90803SPaolo Abeni ret = true; 609fd897679SPaolo Abeni mptcp_send_ack(msk); 610e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6113721b9b6SMat Martineau } 6127ed90803SPaolo Abeni return ret; 6133721b9b6SMat Martineau } 6143721b9b6SMat Martineau 6156771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6166771bfd9SFlorian Westphal struct sock *ssk, 6176771bfd9SFlorian Westphal unsigned int *bytes) 6186771bfd9SFlorian Westphal { 6196771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 620600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6216771bfd9SFlorian Westphal unsigned int moved = 0; 6226771bfd9SFlorian Westphal bool more_data_avail; 6236771bfd9SFlorian Westphal struct tcp_sock *tp; 6246771bfd9SFlorian Westphal bool done = false; 62513c7ba0cSFlorian Westphal int sk_rbuf; 62613c7ba0cSFlorian Westphal 62713c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 62813c7ba0cSFlorian Westphal 62913c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 63013c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 63113c7ba0cSFlorian Westphal 63213c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 63313c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 63413c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 63513c7ba0cSFlorian Westphal } 63613c7ba0cSFlorian Westphal } 637600911ffSFlorian Westphal 638ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6396771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6406771bfd9SFlorian Westphal do { 6416771bfd9SFlorian Westphal u32 map_remaining, offset; 6426771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6436771bfd9SFlorian Westphal struct sk_buff *skb; 6446771bfd9SFlorian Westphal bool fin; 6456771bfd9SFlorian Westphal 6466771bfd9SFlorian Westphal /* try to move as much data as available */ 6476771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6486771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6496771bfd9SFlorian Westphal 6506771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 651d9fb8c50SPaolo Abeni if (!skb) { 652d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 653d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 654d9fb8c50SPaolo Abeni * can enter an infinite loop 655d9fb8c50SPaolo Abeni */ 656d9fb8c50SPaolo Abeni if (!moved) 657d9fb8c50SPaolo Abeni done = true; 6586771bfd9SFlorian Westphal break; 659d9fb8c50SPaolo Abeni } 6606771bfd9SFlorian Westphal 661e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 662e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 663e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 664e1ff9e82SDavide Caratti * be sure to adjust the size 665e1ff9e82SDavide Caratti */ 666e1ff9e82SDavide Caratti map_remaining = skb->len; 667e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 668e1ff9e82SDavide Caratti } 669e1ff9e82SDavide Caratti 6706771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6716771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6726771bfd9SFlorian Westphal if (fin) { 6736771bfd9SFlorian Westphal done = true; 6746771bfd9SFlorian Westphal seq++; 6756771bfd9SFlorian Westphal } 6766771bfd9SFlorian Westphal 6776771bfd9SFlorian Westphal if (offset < skb->len) { 6786771bfd9SFlorian Westphal size_t len = skb->len - offset; 6796771bfd9SFlorian Westphal 6806771bfd9SFlorian Westphal if (tp->urg_data) 6816771bfd9SFlorian Westphal done = true; 6826771bfd9SFlorian Westphal 683ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6846771bfd9SFlorian Westphal moved += len; 685ab174ad8SPaolo Abeni seq += len; 6866771bfd9SFlorian Westphal 6876771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6886771bfd9SFlorian Westphal break; 6896771bfd9SFlorian Westphal } else { 6906771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6916771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 6926771bfd9SFlorian Westphal done = true; 6936771bfd9SFlorian Westphal } 6946771bfd9SFlorian Westphal 6956771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 6966771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 697600911ffSFlorian Westphal 69813c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 699600911ffSFlorian Westphal done = true; 700600911ffSFlorian Westphal break; 701600911ffSFlorian Westphal } 7026771bfd9SFlorian Westphal } while (more_data_avail); 7036771bfd9SFlorian Westphal 7046719331cSPaolo Abeni *bytes += moved; 7056771bfd9SFlorian Westphal return done; 7066771bfd9SFlorian Westphal } 7076771bfd9SFlorian Westphal 70887952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 709ab174ad8SPaolo Abeni { 710ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 711ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 712ab174ad8SPaolo Abeni bool moved = false; 713ab174ad8SPaolo Abeni struct rb_node *p; 714ab174ad8SPaolo Abeni u64 end_seq; 715ab174ad8SPaolo Abeni 716ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 71706242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 718ab174ad8SPaolo Abeni while (p) { 719ab174ad8SPaolo Abeni skb = rb_to_skb(p); 720ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 721ab174ad8SPaolo Abeni break; 722ab174ad8SPaolo Abeni 723ab174ad8SPaolo Abeni p = rb_next(p); 724ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 725ab174ad8SPaolo Abeni 726ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 727ab174ad8SPaolo Abeni msk->ack_seq))) { 728ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 72906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 730ab174ad8SPaolo Abeni continue; 731ab174ad8SPaolo Abeni } 732ab174ad8SPaolo Abeni 733ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 734ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 735ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 736ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 737ab174ad8SPaolo Abeni 738ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 73906242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 74006242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 74106242e44SPaolo Abeni delta); 742ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7432c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 744ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 745ab174ad8SPaolo Abeni } 746ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 747ab174ad8SPaolo Abeni moved = true; 748ab174ad8SPaolo Abeni } 749ab174ad8SPaolo Abeni return moved; 750ab174ad8SPaolo Abeni } 751ab174ad8SPaolo Abeni 7522e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7532e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7542e52213cSFlorian Westphal */ 75599d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7562e52213cSFlorian Westphal { 7572e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7582e52213cSFlorian Westphal unsigned int moved = 0; 7592e52213cSFlorian Westphal 7602e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 76187952603SPaolo Abeni __mptcp_ofo_queue(msk); 762499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 763499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 764499ada50SPaolo Abeni __mptcp_error_report(sk); 765499ada50SPaolo Abeni else 766499ada50SPaolo Abeni set_bit(MPTCP_ERROR_REPORT, &msk->flags); 767499ada50SPaolo Abeni } 768ab174ad8SPaolo Abeni 769ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 770ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 771ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 772ab174ad8SPaolo Abeni * do it. 773ab174ad8SPaolo Abeni */ 774ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 775ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 77699d1055cSPaolo Abeni return moved > 0; 7772e52213cSFlorian Westphal } 7782e52213cSFlorian Westphal 7792e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 780101f6f85SFlorian Westphal { 7816719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 782101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 78313c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 784101f6f85SFlorian Westphal 785d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 786d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 787d7b1bfd0SPaolo Abeni * more data to the msk receive queue 788d7b1bfd0SPaolo Abeni */ 789d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 790d7b1bfd0SPaolo Abeni return; 791d7b1bfd0SPaolo Abeni 79213c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 79313c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 79413c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 79513c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 79613c7ba0cSFlorian Westphal 79799d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 798ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 799ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 80099d1055cSPaolo Abeni return; 801ce599c51SPaolo Abeni } 8022e52213cSFlorian Westphal 80399d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 80499d1055cSPaolo Abeni mptcp_data_lock(sk); 805612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 806101f6f85SFlorian Westphal sk->sk_data_ready(sk); 807612f71d7SPaolo Abeni 80899d1055cSPaolo Abeni mptcp_data_unlock(sk); 80999d1055cSPaolo Abeni } 810101f6f85SFlorian Westphal 81178962489SFlorian Westphal static bool mptcp_do_flush_join_list(struct mptcp_sock *msk) 812ec3edaa7SPeter Krystad { 8135cf92bbaSPaolo Abeni struct mptcp_subflow_context *subflow; 814df00b087SFlorian Westphal bool ret = false; 8155cf92bbaSPaolo Abeni 816ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 81778962489SFlorian Westphal return false; 818ec3edaa7SPeter Krystad 819ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 820df00b087SFlorian Westphal list_for_each_entry(subflow, &msk->join_list, node) { 821df00b087SFlorian Westphal u32 sseq = READ_ONCE(subflow->setsockopt_seq); 82278962489SFlorian Westphal 823df00b087SFlorian Westphal mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow)); 824df00b087SFlorian Westphal if (READ_ONCE(msk->setsockopt_seq) != sseq) 825df00b087SFlorian Westphal ret = true; 826df00b087SFlorian Westphal } 827ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 828ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 82978962489SFlorian Westphal 830df00b087SFlorian Westphal return ret; 83178962489SFlorian Westphal } 83278962489SFlorian Westphal 83378962489SFlorian Westphal void __mptcp_flush_join_list(struct mptcp_sock *msk) 83478962489SFlorian Westphal { 83578962489SFlorian Westphal if (likely(!mptcp_do_flush_join_list(msk))) 83678962489SFlorian Westphal return; 83778962489SFlorian Westphal 83878962489SFlorian Westphal if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags)) 83978962489SFlorian Westphal mptcp_schedule_work((struct sock *)msk); 84078962489SFlorian Westphal } 84178962489SFlorian Westphal 84278962489SFlorian Westphal static void mptcp_flush_join_list(struct mptcp_sock *msk) 84378962489SFlorian Westphal { 84478962489SFlorian Westphal bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags); 84578962489SFlorian Westphal 84678962489SFlorian Westphal might_sleep(); 84778962489SFlorian Westphal 84878962489SFlorian Westphal if (!mptcp_do_flush_join_list(msk) && !sync_needed) 84978962489SFlorian Westphal return; 85078962489SFlorian Westphal 85178962489SFlorian Westphal mptcp_sockopt_sync_all(msk); 852ec3edaa7SPeter Krystad } 853ec3edaa7SPeter Krystad 854b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 855b51f9b80SPaolo Abeni { 856b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 857b51f9b80SPaolo Abeni } 858b51f9b80SPaolo Abeni 859b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 860b51f9b80SPaolo Abeni { 861b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 862b51f9b80SPaolo Abeni unsigned long tout; 863b51f9b80SPaolo Abeni 864e16163b6SPaolo Abeni /* prevent rescheduling on close */ 865e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 866e16163b6SPaolo Abeni return; 867e16163b6SPaolo Abeni 86833d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 869b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 870b51f9b80SPaolo Abeni } 871b51f9b80SPaolo Abeni 872ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 873ba8f48f7SPaolo Abeni { 874ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 875ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 876ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 877ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 878ba8f48f7SPaolo Abeni */ 879ba8f48f7SPaolo Abeni sock_hold(sk); 880ba8f48f7SPaolo Abeni return true; 881ba8f48f7SPaolo Abeni } 882ba8f48f7SPaolo Abeni return false; 883ba8f48f7SPaolo Abeni } 884ba8f48f7SPaolo Abeni 88559832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 88659832e24SFlorian Westphal { 887ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 888ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 88959832e24SFlorian Westphal } 89059832e24SFlorian Westphal 8915969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8925969856aSPaolo Abeni { 8935969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 8945969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 8955969856aSPaolo Abeni int receivers = 0; 8965969856aSPaolo Abeni 8975969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 8985969856aSPaolo Abeni receivers += !subflow->rx_eof; 899e16163b6SPaolo Abeni if (receivers) 900e16163b6SPaolo Abeni return; 9015969856aSPaolo Abeni 902e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 9035969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 9045969856aSPaolo Abeni * to msk, when all subflows agree on it 9055969856aSPaolo Abeni */ 9065969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 9075969856aSPaolo Abeni 9085969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9095969856aSPaolo Abeni sk->sk_data_ready(sk); 9105969856aSPaolo Abeni } 911e16163b6SPaolo Abeni 912e16163b6SPaolo Abeni switch (sk->sk_state) { 913e16163b6SPaolo Abeni case TCP_ESTABLISHED: 914e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 915e16163b6SPaolo Abeni break; 916e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 91726aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 91826aa2314SPaolo Abeni break; 91926aa2314SPaolo Abeni case TCP_FIN_WAIT2: 920e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 921e16163b6SPaolo Abeni break; 922e16163b6SPaolo Abeni default: 923e16163b6SPaolo Abeni return; 924e16163b6SPaolo Abeni } 925e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9265969856aSPaolo Abeni } 9275969856aSPaolo Abeni 9287a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9297a6a6cbcSPaolo Abeni { 9307a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9317a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 9327a6a6cbcSPaolo Abeni 9337a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 9347a6a6cbcSPaolo Abeni 9357a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 93699d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9377a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9387a6a6cbcSPaolo Abeni } 9397a6a6cbcSPaolo Abeni 9407a6a6cbcSPaolo Abeni return NULL; 9417a6a6cbcSPaolo Abeni } 9427a6a6cbcSPaolo Abeni 9433f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 94457040755SPaolo Abeni const struct sk_buff *skb, 94557040755SPaolo Abeni const struct mptcp_ext *mpext) 9466d0060f6SMat Martineau { 94757040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 94857040755SPaolo Abeni return false; 94957040755SPaolo Abeni 9505a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9515a369ca6SPaolo Abeni * mapping has not been xmitted yet 9525a369ca6SPaolo Abeni */ 9535a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9545a369ca6SPaolo Abeni !mpext->frozen; 95557040755SPaolo Abeni } 95657040755SPaolo Abeni 95729249eacSPaolo Abeni /* we can append data to the given data frag if: 95829249eacSPaolo Abeni * - there is space available in the backing page_frag 95929249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 96029249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 96129249eacSPaolo Abeni */ 96218b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 96318b683bfSPaolo Abeni const struct page_frag *pfrag, 96418b683bfSPaolo Abeni const struct mptcp_data_frag *df) 96518b683bfSPaolo Abeni { 96618b683bfSPaolo Abeni return df && pfrag->page == df->page && 967d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 96829249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 96918b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 97018b683bfSPaolo Abeni } 97118b683bfSPaolo Abeni 9721094c6feSPaolo Abeni static void __mptcp_mem_reclaim_partial(struct sock *sk) 9731094c6feSPaolo Abeni { 9746511882cSPaolo Abeni int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 9756511882cSPaolo Abeni 9761094c6feSPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 9776511882cSPaolo Abeni 9786511882cSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable - 1); 9791094c6feSPaolo Abeni sk_mem_reclaim_partial(sk); 9801094c6feSPaolo Abeni } 9811094c6feSPaolo Abeni 982724cfd2eSPaolo Abeni static void mptcp_mem_reclaim_partial(struct sock *sk) 983724cfd2eSPaolo Abeni { 984724cfd2eSPaolo Abeni mptcp_data_lock(sk); 9856511882cSPaolo Abeni __mptcp_mem_reclaim_partial(sk); 986724cfd2eSPaolo Abeni mptcp_data_unlock(sk); 987724cfd2eSPaolo Abeni } 988724cfd2eSPaolo Abeni 989d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 99018b683bfSPaolo Abeni { 991d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9927948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 993d027236cSPaolo Abeni } 994d027236cSPaolo Abeni 995d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 996d027236cSPaolo Abeni { 997d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 998d027236cSPaolo Abeni 99918b683bfSPaolo Abeni list_del(&dfrag->list); 1000d027236cSPaolo Abeni dfrag_uncharge(sk, len); 100118b683bfSPaolo Abeni put_page(dfrag->page); 100218b683bfSPaolo Abeni } 100318b683bfSPaolo Abeni 10046e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 100518b683bfSPaolo Abeni { 100618b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100718b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1008d027236cSPaolo Abeni bool cleaned = false; 1009e1ff9e82SDavide Caratti u64 snd_una; 1010e1ff9e82SDavide Caratti 1011e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 1012e1ff9e82SDavide Caratti * plain TCP 1013e1ff9e82SDavide Caratti */ 1014e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10157439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10167439d687SPaolo Abeni 10177439d687SPaolo Abeni snd_una = msk->snd_una; 101818b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 101918b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 102018b683bfSPaolo Abeni break; 102118b683bfSPaolo Abeni 10221e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10231e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10241e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1025d9ca1de8SPaolo Abeni break; 10261e1d9d6fSPaolo Abeni 10271e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10281e1d9d6fSPaolo Abeni } 10291e1d9d6fSPaolo Abeni 1030d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1031d027236cSPaolo Abeni cleaned = true; 1032d027236cSPaolo Abeni } 1033d027236cSPaolo Abeni 10347948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10357948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 103653eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 103753eb4c38SPaolo Abeni 10381e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10391e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10401e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 104153eb4c38SPaolo Abeni goto out; 10421e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10431e1d9d6fSPaolo Abeni goto out; 10441e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10451e1d9d6fSPaolo Abeni } 10467948f6ccSFlorian Westphal 10477948f6ccSFlorian Westphal dfrag->data_seq += delta; 104853eb4c38SPaolo Abeni dfrag->offset += delta; 10497948f6ccSFlorian Westphal dfrag->data_len -= delta; 1050d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10517948f6ccSFlorian Westphal 10527948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10537948f6ccSFlorian Westphal cleaned = true; 10547948f6ccSFlorian Westphal } 10557948f6ccSFlorian Westphal 10561e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10571e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10581e1d9d6fSPaolo Abeni msk->recovery = false; 10591e1d9d6fSPaolo Abeni 106053eb4c38SPaolo Abeni out: 10611094c6feSPaolo Abeni if (cleaned && tcp_under_memory_pressure(sk)) 10621094c6feSPaolo Abeni __mptcp_mem_reclaim_partial(sk); 106318b683bfSPaolo Abeni 10643241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10653241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 106633d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10676e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10686e628cd3SPaolo Abeni } else { 10696e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10706e628cd3SPaolo Abeni } 10716e628cd3SPaolo Abeni } 10726e628cd3SPaolo Abeni 1073417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1074417789dfSPaolo Abeni { 1075765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1076765ff425SPaolo Abeni 1077417789dfSPaolo Abeni __mptcp_clean_una(sk); 1078417789dfSPaolo Abeni mptcp_write_space(sk); 1079417789dfSPaolo Abeni } 1080417789dfSPaolo Abeni 1081b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1082b5941f06SPaolo Abeni { 1083b5941f06SPaolo Abeni mptcp_data_lock(sk); 1084b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1085b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1086b5941f06SPaolo Abeni } 1087b5941f06SPaolo Abeni 1088724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 108918b683bfSPaolo Abeni { 1090d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1091d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1092d9ca1de8SPaolo Abeni bool first = true; 1093d9ca1de8SPaolo Abeni 109418b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1095d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1096d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1097d9ca1de8SPaolo Abeni 1098d9ca1de8SPaolo Abeni if (first) 1099d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1100d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1101d9ca1de8SPaolo Abeni first = false; 1102d9ca1de8SPaolo Abeni } 1103724cfd2eSPaolo Abeni } 1104724cfd2eSPaolo Abeni 1105724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1106724cfd2eSPaolo Abeni * data 1107724cfd2eSPaolo Abeni */ 1108724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1109724cfd2eSPaolo Abeni { 1110724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1111724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1112724cfd2eSPaolo Abeni return true; 1113724cfd2eSPaolo Abeni 1114724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 111518b683bfSPaolo Abeni return false; 111618b683bfSPaolo Abeni } 111718b683bfSPaolo Abeni 111818b683bfSPaolo Abeni static struct mptcp_data_frag * 111918b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 112018b683bfSPaolo Abeni int orig_offset) 112118b683bfSPaolo Abeni { 112218b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 112318b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 112418b683bfSPaolo Abeni 112518b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 112618b683bfSPaolo Abeni dfrag->data_len = 0; 112718b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 112818b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 112918b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1130d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 113118b683bfSPaolo Abeni dfrag->page = pfrag->page; 113218b683bfSPaolo Abeni 113318b683bfSPaolo Abeni return dfrag; 113418b683bfSPaolo Abeni } 113518b683bfSPaolo Abeni 1136caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1137caf971dfSPaolo Abeni int mss_now; 1138caf971dfSPaolo Abeni int size_goal; 1139d9ca1de8SPaolo Abeni u16 limit; 1140d9ca1de8SPaolo Abeni u16 sent; 1141d9ca1de8SPaolo Abeni unsigned int flags; 11421094c6feSPaolo Abeni bool data_lock_held; 1143caf971dfSPaolo Abeni }; 1144caf971dfSPaolo Abeni 11456f8a612aSFlorian Westphal static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq, 11466f8a612aSFlorian Westphal int avail_size) 11476f8a612aSFlorian Westphal { 11486f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 11496f8a612aSFlorian Westphal 11506f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11516f8a612aSFlorian Westphal return avail_size; 11526f8a612aSFlorian Westphal 11536f8a612aSFlorian Westphal if (!before64(data_seq + avail_size, window_end)) { 11546f8a612aSFlorian Westphal u64 allowed_size = window_end - data_seq; 11556f8a612aSFlorian Westphal 11566f8a612aSFlorian Westphal return min_t(unsigned int, allowed_size, avail_size); 11576f8a612aSFlorian Westphal } 11586f8a612aSFlorian Westphal 11596f8a612aSFlorian Westphal return avail_size; 11606f8a612aSFlorian Westphal } 11616f8a612aSFlorian Westphal 1162724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1163724cfd2eSPaolo Abeni { 1164724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1165724cfd2eSPaolo Abeni 1166724cfd2eSPaolo Abeni if (!mpext) 1167724cfd2eSPaolo Abeni return false; 1168724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1169724cfd2eSPaolo Abeni return true; 1170724cfd2eSPaolo Abeni } 1171724cfd2eSPaolo Abeni 11726e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1173724cfd2eSPaolo Abeni { 1174724cfd2eSPaolo Abeni struct sk_buff *skb; 1175724cfd2eSPaolo Abeni 11766e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1177724cfd2eSPaolo Abeni if (likely(skb)) { 11786e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1179724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1180a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1181f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1182724cfd2eSPaolo Abeni return skb; 1183724cfd2eSPaolo Abeni } 1184724cfd2eSPaolo Abeni __kfree_skb(skb); 1185724cfd2eSPaolo Abeni } else { 1186724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1187724cfd2eSPaolo Abeni } 1188724cfd2eSPaolo Abeni return NULL; 1189724cfd2eSPaolo Abeni } 1190724cfd2eSPaolo Abeni 1191f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1192724cfd2eSPaolo Abeni { 1193724cfd2eSPaolo Abeni struct sk_buff *skb; 1194724cfd2eSPaolo Abeni 11956e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1196724cfd2eSPaolo Abeni if (!skb) 1197f70cad10SPaolo Abeni return NULL; 1198724cfd2eSPaolo Abeni 1199724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1200f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1201f70cad10SPaolo Abeni return skb; 1202724cfd2eSPaolo Abeni } 1203724cfd2eSPaolo Abeni kfree_skb(skb); 1204f70cad10SPaolo Abeni return NULL; 1205724cfd2eSPaolo Abeni } 1206724cfd2eSPaolo Abeni 1207f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1208724cfd2eSPaolo Abeni { 12091094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1210724cfd2eSPaolo Abeni 12111094c6feSPaolo Abeni if (unlikely(tcp_under_memory_pressure(sk))) { 12121094c6feSPaolo Abeni if (data_lock_held) 12131094c6feSPaolo Abeni __mptcp_mem_reclaim_partial(sk); 12141094c6feSPaolo Abeni else 1215724cfd2eSPaolo Abeni mptcp_mem_reclaim_partial(sk); 12161094c6feSPaolo Abeni } 12171094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1218724cfd2eSPaolo Abeni } 1219724cfd2eSPaolo Abeni 1220d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1221d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1222d0cc2987SGeliang Tang */ 1223d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1224d0cc2987SGeliang Tang { 1225d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1226d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1227d0cc2987SGeliang Tang int offset = skb->len - added; 1228d0cc2987SGeliang Tang 1229d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1230d0cc2987SGeliang Tang } 1231d0cc2987SGeliang Tang 123257040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1233d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1234caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 123557040755SPaolo Abeni { 1236d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1237f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12386d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12396f8a612aSFlorian Westphal bool zero_window_probe = false; 12406d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1241f70cad10SPaolo Abeni bool can_coalesce = false; 1242f70cad10SPaolo Abeni bool reuse_skb = true; 1243f70cad10SPaolo Abeni struct sk_buff *skb; 1244f70cad10SPaolo Abeni size_t copy; 1245f70cad10SPaolo Abeni int i; 12466d0060f6SMat Martineau 1247e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1248d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12496d0060f6SMat Martineau 1250f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1251f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1252f70cad10SPaolo Abeni return 0; 1253f70cad10SPaolo Abeni 1254d9ca1de8SPaolo Abeni /* compute send limit */ 1255d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1256f70cad10SPaolo Abeni copy = info->size_goal; 1257f70cad10SPaolo Abeni 125857040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1259f70cad10SPaolo Abeni if (skb && copy > skb->len) { 126057040755SPaolo Abeni /* Limit the write to the size available in the 126157040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 126257040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 126357040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 126457040755SPaolo Abeni * SSN association set here 126557040755SPaolo Abeni */ 1266d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12671094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126857040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12691094c6feSPaolo Abeni goto alloc_skb; 12701094c6feSPaolo Abeni } 12711094c6feSPaolo Abeni 1272f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1273f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1274f70cad10SPaolo Abeni if (!can_coalesce && i >= sysctl_max_skb_frags) { 1275f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1276f70cad10SPaolo Abeni goto alloc_skb; 127715e6ca97SPaolo Abeni } 127818b683bfSPaolo Abeni 1279f70cad10SPaolo Abeni copy -= skb->len; 1280f70cad10SPaolo Abeni } else { 12811094c6feSPaolo Abeni alloc_skb: 1282f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1283f70cad10SPaolo Abeni if (!skb) 1284f70cad10SPaolo Abeni return -ENOMEM; 1285f70cad10SPaolo Abeni 1286f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1287f70cad10SPaolo Abeni reuse_skb = false; 1288f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1289f70cad10SPaolo Abeni } 12901094c6feSPaolo Abeni 12916f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1292f70cad10SPaolo Abeni copy = mptcp_check_allowed_size(msk, data_seq, copy); 1293f70cad10SPaolo Abeni if (copy == 0) { 12947439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12957439d687SPaolo Abeni 1296f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 129727728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12986f8a612aSFlorian Westphal return 0; 12996f8a612aSFlorian Westphal } 13006f8a612aSFlorian Westphal 1301f70cad10SPaolo Abeni zero_window_probe = true; 1302f70cad10SPaolo Abeni data_seq = snd_una - 1; 1303f70cad10SPaolo Abeni copy = 1; 130457040755SPaolo Abeni 1305f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1306f70cad10SPaolo Abeni * ssk write queue 1307f70cad10SPaolo Abeni */ 1308f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1309f70cad10SPaolo Abeni } 1310f70cad10SPaolo Abeni 1311f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1312f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 131327728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1314e2223995SPaolo Abeni return -ENOMEM; 131535759383SFlorian Westphal } 131618b683bfSPaolo Abeni 1317f70cad10SPaolo Abeni if (can_coalesce) { 1318f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1319f70cad10SPaolo Abeni } else { 1320f70cad10SPaolo Abeni get_page(dfrag->page); 1321f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 132257040755SPaolo Abeni } 132357040755SPaolo Abeni 1324f70cad10SPaolo Abeni skb->len += copy; 1325f70cad10SPaolo Abeni skb->data_len += copy; 1326f70cad10SPaolo Abeni skb->truesize += copy; 1327f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1328f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1329f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1330f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1331f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1332f70cad10SPaolo Abeni 1333f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1334f70cad10SPaolo Abeni if (reuse_skb) { 1335f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1336f70cad10SPaolo Abeni mpext->data_len += copy; 1337f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1338f70cad10SPaolo Abeni goto out; 1339724cfd2eSPaolo Abeni } 13406d0060f6SMat Martineau 13416d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1342d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13436d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1344f70cad10SPaolo Abeni mpext->data_len = copy; 13456d0060f6SMat Martineau mpext->use_map = 1; 13466d0060f6SMat Martineau mpext->dsn64 = 1; 13476d0060f6SMat Martineau 13486d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13496d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13506d0060f6SMat Martineau mpext->dsn64); 13516d0060f6SMat Martineau 13526f8a612aSFlorian Westphal if (zero_window_probe) { 1353f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13546f8a612aSFlorian Westphal mpext->frozen = 1; 1355d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1356f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13576f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1358d0cc2987SGeliang Tang return 0; 13596f8a612aSFlorian Westphal } 136057040755SPaolo Abeni out: 1361d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1362f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 1363f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1364f70cad10SPaolo Abeni return copy; 13656d0060f6SMat Martineau } 13666d0060f6SMat Martineau 1367d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1368d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1369d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1370d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1371d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1372d5f49190SPaolo Abeni 1373d5f49190SPaolo Abeni struct subflow_send_info { 1374d5f49190SPaolo Abeni struct sock *ssk; 13753ce0852cSPaolo Abeni u64 linger_time; 1376d5f49190SPaolo Abeni }; 1377d5f49190SPaolo Abeni 1378ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1379ff5a0b42SPaolo Abeni { 1380ff5a0b42SPaolo Abeni if (!subflow->stale) 1381ff5a0b42SPaolo Abeni return; 1382ff5a0b42SPaolo Abeni 1383ff5a0b42SPaolo Abeni subflow->stale = 0; 1384fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1385ff5a0b42SPaolo Abeni } 1386ff5a0b42SPaolo Abeni 1387ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1388ff5a0b42SPaolo Abeni { 1389ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1390ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1391ff5a0b42SPaolo Abeni 1392ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1393ff5a0b42SPaolo Abeni return false; 1394ff5a0b42SPaolo Abeni 1395ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1396ff5a0b42SPaolo Abeni } 1397ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1398ff5a0b42SPaolo Abeni } 1399ff5a0b42SPaolo Abeni 14003ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14013ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14023ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14033ce0852cSPaolo Abeni 140433d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140533d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140633d41c9cSPaolo Abeni * additionally updates the rtx timeout 140733d41c9cSPaolo Abeni */ 14085cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1409f296234cSPeter Krystad { 14103ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1411f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 141233d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14133ce0852cSPaolo Abeni u32 pace, burst, wmem; 1414d5f49190SPaolo Abeni int i, nr_active = 0; 1415d5f49190SPaolo Abeni struct sock *ssk; 14163ce0852cSPaolo Abeni u64 linger_time; 141733d41c9cSPaolo Abeni long tout = 0; 1418f296234cSPeter Krystad 141933d41c9cSPaolo Abeni sock_owned_by_me(sk); 1420f296234cSPeter Krystad 1421d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1422d5f49190SPaolo Abeni if (!msk->first) 1423f296234cSPeter Krystad return NULL; 1424d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1425f296234cSPeter Krystad } 1426f296234cSPeter Krystad 1427d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1428d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1429d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 143033d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 143133d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1432d5f49190SPaolo Abeni return msk->last_snd; 143333d41c9cSPaolo Abeni } 1434f296234cSPeter Krystad 1435d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14363ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1437d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14383ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1439d5f49190SPaolo Abeni } 14403ce0852cSPaolo Abeni 1441d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1442e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1443d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1444d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1445f296234cSPeter Krystad continue; 1446d5f49190SPaolo Abeni 144733d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1448d5f49190SPaolo Abeni nr_active += !subflow->backup; 14493ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14503ce0852cSPaolo Abeni if (unlikely(!pace)) { 14513ce0852cSPaolo Abeni /* init pacing rate from socket */ 14523ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14533ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1454d5f49190SPaolo Abeni if (!pace) 1455d5f49190SPaolo Abeni continue; 14563ce0852cSPaolo Abeni } 1457d5f49190SPaolo Abeni 14583ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14593ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1460d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14613ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1462d5f49190SPaolo Abeni } 1463f296234cSPeter Krystad } 146433d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1465f296234cSPeter Krystad 1466d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1467d5f49190SPaolo Abeni if (!nr_active) 14683ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1469d5f49190SPaolo Abeni 14703ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14713ce0852cSPaolo Abeni * faster flow, we need to: 14723ce0852cSPaolo Abeni * - estimate the faster flow linger time 14733ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14743ce0852cSPaolo Abeni * by the faster flow 14753ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14763ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14773ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14783ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14793ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14803ce0852cSPaolo Abeni */ 14813ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 14823ce0852cSPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk) || !tcp_sk(ssk)->snd_wnd) 1483d5f49190SPaolo Abeni return NULL; 14843ce0852cSPaolo Abeni 14853ce0852cSPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, tcp_sk(ssk)->snd_wnd); 14863ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 14873ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14883ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14893ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14903ce0852cSPaolo Abeni burst + wmem); 14913ce0852cSPaolo Abeni msk->last_snd = ssk; 14923ce0852cSPaolo Abeni msk->snd_burst = burst; 14933ce0852cSPaolo Abeni return ssk; 1494f296234cSPeter Krystad } 1495f296234cSPeter Krystad 1496b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1497d9ca1de8SPaolo Abeni { 1498d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1499d9ca1de8SPaolo Abeni release_sock(ssk); 1500d9ca1de8SPaolo Abeni } 1501d9ca1de8SPaolo Abeni 15020d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15030d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15040d199e43SFlorian Westphal u32 sent) 15050d199e43SFlorian Westphal { 15060d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15070d199e43SFlorian Westphal 15080d199e43SFlorian Westphal dfrag->already_sent += sent; 15090d199e43SFlorian Westphal 15100d199e43SFlorian Westphal msk->snd_burst -= sent; 15110d199e43SFlorian Westphal 15120d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15130d199e43SFlorian Westphal 15140d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15150d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15160d199e43SFlorian Westphal * old segments. 15170d199e43SFlorian Westphal * 15180d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15190d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15200d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15210d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15220d199e43SFlorian Westphal */ 15230d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15240d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15250d199e43SFlorian Westphal } 15260d199e43SFlorian Westphal 15278b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15283241a9c0SFlorian Westphal { 15293241a9c0SFlorian Westphal if (mptcp_send_head(sk) && 15303241a9c0SFlorian Westphal !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 15313241a9c0SFlorian Westphal set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 15323241a9c0SFlorian Westphal } 15333241a9c0SFlorian Westphal 1534ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1535d9ca1de8SPaolo Abeni { 1536d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1537d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1538d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1539d9ca1de8SPaolo Abeni .flags = flags, 1540d9ca1de8SPaolo Abeni }; 1541d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1542d9ca1de8SPaolo Abeni int len, copied = 0; 1543d9ca1de8SPaolo Abeni 1544d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1545d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1546d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1547d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1548d9ca1de8SPaolo Abeni while (len > 0) { 1549d9ca1de8SPaolo Abeni int ret = 0; 1550d9ca1de8SPaolo Abeni 1551d9ca1de8SPaolo Abeni prev_ssk = ssk; 15523d79e375SMaxim Galaganov __mptcp_flush_join_list(msk); 15535cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1554d9ca1de8SPaolo Abeni 15559758f40eSPaolo Abeni /* First check. If the ssk has changed since 15569758f40eSPaolo Abeni * the last round, release prev_ssk 1557d9ca1de8SPaolo Abeni */ 1558d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1559b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1560d9ca1de8SPaolo Abeni if (!ssk) 1561d9ca1de8SPaolo Abeni goto out; 1562d9ca1de8SPaolo Abeni 15639758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15649758f40eSPaolo Abeni * from the previous one, otherwise we are still 15659758f40eSPaolo Abeni * helding the relevant lock 15669758f40eSPaolo Abeni */ 15679758f40eSPaolo Abeni if (ssk != prev_ssk) 1568d9ca1de8SPaolo Abeni lock_sock(ssk); 1569d9ca1de8SPaolo Abeni 1570d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1571d9ca1de8SPaolo Abeni if (ret <= 0) { 1572b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1573d9ca1de8SPaolo Abeni goto out; 1574d9ca1de8SPaolo Abeni } 1575d9ca1de8SPaolo Abeni 1576d9ca1de8SPaolo Abeni info.sent += ret; 1577d9ca1de8SPaolo Abeni copied += ret; 1578d9ca1de8SPaolo Abeni len -= ret; 15790d199e43SFlorian Westphal 15800d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1581d9ca1de8SPaolo Abeni } 1582d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1583d9ca1de8SPaolo Abeni } 1584d9ca1de8SPaolo Abeni 1585d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1586d9ca1de8SPaolo Abeni if (ssk) 1587b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1588d9ca1de8SPaolo Abeni 1589d9ca1de8SPaolo Abeni out: 159033d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1591d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1592d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 159333d41c9cSPaolo Abeni if (copied) 1594d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1595d9ca1de8SPaolo Abeni } 1596d9ca1de8SPaolo Abeni 15976e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 15986e628cd3SPaolo Abeni { 15996e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16001094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16011094c6feSPaolo Abeni .data_lock_held = true, 16021094c6feSPaolo Abeni }; 16036e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1604b19bc294SPaolo Abeni struct sock *xmit_ssk; 16056e628cd3SPaolo Abeni int len, copied = 0; 1606b19bc294SPaolo Abeni bool first = true; 16076e628cd3SPaolo Abeni 16086e628cd3SPaolo Abeni info.flags = 0; 16096e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16106e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16116e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16126e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16136e628cd3SPaolo Abeni while (len > 0) { 16146e628cd3SPaolo Abeni int ret = 0; 16156e628cd3SPaolo Abeni 1616b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1617b19bc294SPaolo Abeni * check for a different subflow usage only after 1618b19bc294SPaolo Abeni * spooling the first chunk of data 1619b19bc294SPaolo Abeni */ 1620b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1621b19bc294SPaolo Abeni if (!xmit_ssk) 1622b19bc294SPaolo Abeni goto out; 1623b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1624bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1625bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1626b19bc294SPaolo Abeni goto out; 1627b19bc294SPaolo Abeni } 1628b19bc294SPaolo Abeni 16296e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16306e628cd3SPaolo Abeni if (ret <= 0) 16316e628cd3SPaolo Abeni goto out; 16326e628cd3SPaolo Abeni 16336e628cd3SPaolo Abeni info.sent += ret; 16346e628cd3SPaolo Abeni copied += ret; 16356e628cd3SPaolo Abeni len -= ret; 1636b19bc294SPaolo Abeni first = false; 16370d199e43SFlorian Westphal 16380d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16396e628cd3SPaolo Abeni } 16406e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16416e628cd3SPaolo Abeni } 16426e628cd3SPaolo Abeni 16436e628cd3SPaolo Abeni out: 16446e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16456e628cd3SPaolo Abeni * not going to flush it via release_sock() 16466e628cd3SPaolo Abeni */ 16476e628cd3SPaolo Abeni if (copied) { 16486e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16496e628cd3SPaolo Abeni info.size_goal); 1650d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1651d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1652d09d818eSPaolo Abeni 16536e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16546e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16556e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16566e628cd3SPaolo Abeni } 16576e628cd3SPaolo Abeni } 16586e628cd3SPaolo Abeni 16595cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16605cf92bbaSPaolo Abeni { 16615cf92bbaSPaolo Abeni /* enable autotune */ 16625cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16635cf92bbaSPaolo Abeni 16645cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16655cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16665cf92bbaSPaolo Abeni } 16675cf92bbaSPaolo Abeni 1668f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1669f870fa0bSMat Martineau { 1670f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 167117091708SFlorian Westphal struct page_frag *pfrag; 16726d0060f6SMat Martineau size_t copied = 0; 1673caf971dfSPaolo Abeni int ret = 0; 16746d0060f6SMat Martineau long timeo; 1675f870fa0bSMat Martineau 1676987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1677987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1678f870fa0bSMat Martineau return -EOPNOTSUPP; 1679f870fa0bSMat Martineau 1680987858e5SPaolo Abeni /* silently ignore everything else */ 1681987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1682987858e5SPaolo Abeni 16836511882cSPaolo Abeni lock_sock(sk); 16841954b860SMat Martineau 16851954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 16861954b860SMat Martineau 16871954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 16881954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 16891954b860SMat Martineau if (ret) 16901954b860SMat Martineau goto out; 16911954b860SMat Martineau } 16921954b860SMat Martineau 169317091708SFlorian Westphal pfrag = sk_page_frag(sk); 169418b683bfSPaolo Abeni 1695d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1696724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1697d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1698d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1699d9ca1de8SPaolo Abeni size_t psize, offset; 1700d9ca1de8SPaolo Abeni 170157baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 170257baaf28SMat Martineau ret = -EPIPE; 170357baaf28SMat Martineau goto out; 170457baaf28SMat Martineau } 170557baaf28SMat Martineau 1706d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1707d9ca1de8SPaolo Abeni * page allocator 1708fb529e62SFlorian Westphal */ 1709d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1710d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1711d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1712d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1713d9ca1de8SPaolo Abeni goto wait_for_memory; 17146e628cd3SPaolo Abeni 1715d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1716d9ca1de8SPaolo Abeni goto wait_for_memory; 1717d9ca1de8SPaolo Abeni 1718d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1719d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1720fb529e62SFlorian Westphal } 1721fb529e62SFlorian Westphal 1722d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1723d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1724d9ca1de8SPaolo Abeni * anyway 1725d9ca1de8SPaolo Abeni */ 1726d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1727d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1728d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1729724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17308ce568edSPaolo Abeni 17316511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1732d9ca1de8SPaolo Abeni goto wait_for_memory; 1733d9ca1de8SPaolo Abeni 1734d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1735d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1736d9ca1de8SPaolo Abeni ret = -EFAULT; 1737d9ca1de8SPaolo Abeni goto out; 1738d9ca1de8SPaolo Abeni } 1739d9ca1de8SPaolo Abeni 1740d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17416511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1742d9ca1de8SPaolo Abeni copied += psize; 1743d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1744d9ca1de8SPaolo Abeni frag_truesize += psize; 1745d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1746d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1747d9ca1de8SPaolo Abeni 1748d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1749d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1750d9ca1de8SPaolo Abeni */ 1751d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1752d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1753d9ca1de8SPaolo Abeni get_page(dfrag->page); 1754d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1755d9ca1de8SPaolo Abeni if (!msk->first_pending) 1756d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1757d9ca1de8SPaolo Abeni } 1758e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1759d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1760d9ca1de8SPaolo Abeni !dfrag_collapsed); 1761d9ca1de8SPaolo Abeni 1762d9ca1de8SPaolo Abeni continue; 1763d9ca1de8SPaolo Abeni 1764d9ca1de8SPaolo Abeni wait_for_memory: 17655cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1766c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1767f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1768f296234cSPeter Krystad if (ret) 1769f296234cSPeter Krystad goto out; 1770cec37a6eSPeter Krystad } 1771cec37a6eSPeter Krystad 177213e16037SPaolo Abeni if (copied) 1773c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1774da51aef5SPaolo Abeni 17751954b860SMat Martineau out: 1776cec37a6eSPeter Krystad release_sock(sk); 17778555c6bfSPaolo Abeni return copied ? : ret; 1778f870fa0bSMat Martineau } 1779f870fa0bSMat Martineau 17806771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 17816771bfd9SFlorian Westphal struct msghdr *msg, 1782b7f653b2SFlorian Westphal size_t len, int flags, 1783b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1784b7f653b2SFlorian Westphal int *cmsg_flags) 17856771bfd9SFlorian Westphal { 1786ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 17876771bfd9SFlorian Westphal int copied = 0; 17886771bfd9SFlorian Westphal 1789ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 17906771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 17916771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 17926771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 17936771bfd9SFlorian Westphal int err; 17946771bfd9SFlorian Westphal 1795d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 17966771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 17976771bfd9SFlorian Westphal if (unlikely(err < 0)) { 17986771bfd9SFlorian Westphal if (!copied) 17996771bfd9SFlorian Westphal return err; 18006771bfd9SFlorian Westphal break; 18016771bfd9SFlorian Westphal } 1802d976092cSPaolo Abeni } 18036771bfd9SFlorian Westphal 1804b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1805b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1806b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1807b7f653b2SFlorian Westphal } 1808b7f653b2SFlorian Westphal 18096771bfd9SFlorian Westphal copied += count; 18106771bfd9SFlorian Westphal 18116771bfd9SFlorian Westphal if (count < data_len) { 18122c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18136771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18142c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18152c9e7765SFlorian Westphal } 18166771bfd9SFlorian Westphal break; 18176771bfd9SFlorian Westphal } 18186771bfd9SFlorian Westphal 1819ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 182087952603SPaolo Abeni /* we will bulk release the skb memory later */ 182187952603SPaolo Abeni skb->destructor = NULL; 1822ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 182387952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18246771bfd9SFlorian Westphal __kfree_skb(skb); 1825ca4fb892SYonglong Li } 18266771bfd9SFlorian Westphal 18276771bfd9SFlorian Westphal if (copied >= len) 18286771bfd9SFlorian Westphal break; 18296771bfd9SFlorian Westphal } 18306771bfd9SFlorian Westphal 18316771bfd9SFlorian Westphal return copied; 18326771bfd9SFlorian Westphal } 18336771bfd9SFlorian Westphal 1834a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1835a6b118feSFlorian Westphal * 1836a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1837a6b118feSFlorian Westphal */ 1838a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1839a6b118feSFlorian Westphal { 1840a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1841a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1842a6b118feSFlorian Westphal u32 time, advmss = 1; 1843a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1844a6b118feSFlorian Westphal 1845a6b118feSFlorian Westphal sock_owned_by_me(sk); 1846a6b118feSFlorian Westphal 1847a6b118feSFlorian Westphal if (copied <= 0) 1848a6b118feSFlorian Westphal return; 1849a6b118feSFlorian Westphal 1850a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1851a6b118feSFlorian Westphal 1852a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1853a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1854a6b118feSFlorian Westphal 1855a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1856a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1857a6b118feSFlorian Westphal return; 1858a6b118feSFlorian Westphal 1859a6b118feSFlorian Westphal rtt_us = 0; 1860a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1861a6b118feSFlorian Westphal const struct tcp_sock *tp; 1862a6b118feSFlorian Westphal u64 sf_rtt_us; 1863a6b118feSFlorian Westphal u32 sf_advmss; 1864a6b118feSFlorian Westphal 1865a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1866a6b118feSFlorian Westphal 1867a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1868a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1869a6b118feSFlorian Westphal 1870a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1871a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1872a6b118feSFlorian Westphal } 1873a6b118feSFlorian Westphal 1874a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1875a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1876a6b118feSFlorian Westphal return; 1877a6b118feSFlorian Westphal 1878a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1879a6b118feSFlorian Westphal goto new_measure; 1880a6b118feSFlorian Westphal 1881a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1882a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1883a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1884a6b118feSFlorian Westphal u64 rcvwin, grow; 1885a6b118feSFlorian Westphal 1886a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1887a6b118feSFlorian Westphal 1888a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1889a6b118feSFlorian Westphal 1890a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1891a6b118feSFlorian Westphal rcvwin += (grow << 1); 1892a6b118feSFlorian Westphal 1893a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1894a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1895a6b118feSFlorian Westphal rcvmem += 128; 1896a6b118feSFlorian Westphal 1897a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1898a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1899a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1900a6b118feSFlorian Westphal 1901a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1902a6b118feSFlorian Westphal u32 window_clamp; 1903a6b118feSFlorian Westphal 1904a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1905a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1906a6b118feSFlorian Westphal 1907a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1908a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1909a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1910a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1911a6b118feSFlorian Westphal */ 1912a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1913a6b118feSFlorian Westphal struct sock *ssk; 1914c76c6956SPaolo Abeni bool slow; 1915a6b118feSFlorian Westphal 1916a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1917c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1918a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1919a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1920c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1921c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1922a6b118feSFlorian Westphal } 1923a6b118feSFlorian Westphal } 1924a6b118feSFlorian Westphal } 1925a6b118feSFlorian Westphal 1926a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1927a6b118feSFlorian Westphal new_measure: 1928a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1929a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1930a6b118feSFlorian Westphal } 1931a6b118feSFlorian Westphal 193287952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 193387952603SPaolo Abeni { 193487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 193587952603SPaolo Abeni 193687952603SPaolo Abeni if (!msk->rmem_released) 193787952603SPaolo Abeni return; 193887952603SPaolo Abeni 193987952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19406511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1941ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 194287952603SPaolo Abeni } 194387952603SPaolo Abeni 194487952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 194587952603SPaolo Abeni { 194687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 194787952603SPaolo Abeni 194887952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 194987952603SPaolo Abeni } 195087952603SPaolo Abeni 1951e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19526771bfd9SFlorian Westphal { 195387952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19546771bfd9SFlorian Westphal unsigned int moved = 0; 195587952603SPaolo Abeni bool ret, done; 1956d5f49190SPaolo Abeni 195778962489SFlorian Westphal mptcp_flush_join_list(msk); 19586771bfd9SFlorian Westphal do { 19596771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 196065f49fe7SFlorian Westphal bool slowpath; 19616771bfd9SFlorian Westphal 196287952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 196387952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 196487952603SPaolo Abeni * that is an unlikely slow path. 196587952603SPaolo Abeni */ 196687952603SPaolo Abeni if (likely(!ssk)) 19676771bfd9SFlorian Westphal break; 19686771bfd9SFlorian Westphal 196965f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 197087952603SPaolo Abeni mptcp_data_lock(sk); 1971e3859603SPaolo Abeni __mptcp_update_rmem(sk); 19726771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 197387952603SPaolo Abeni mptcp_data_unlock(sk); 1974499ada50SPaolo Abeni 1975499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 1976499ada50SPaolo Abeni __mptcp_error_report(sk); 197765f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 19786771bfd9SFlorian Westphal } while (!done); 19796771bfd9SFlorian Westphal 198087952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 198187952603SPaolo Abeni ret = moved > 0; 198287952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 198387952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 198487952603SPaolo Abeni mptcp_data_lock(sk); 198587952603SPaolo Abeni __mptcp_update_rmem(sk); 198687952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 198787952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 198887952603SPaolo Abeni mptcp_data_unlock(sk); 1989ab174ad8SPaolo Abeni } 199087952603SPaolo Abeni if (ret) 199187952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 199287952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 19936771bfd9SFlorian Westphal } 19946771bfd9SFlorian Westphal 19952c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 19962c9e7765SFlorian Westphal { 19972c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 19982c9e7765SFlorian Westphal const struct sk_buff *skb; 19992c9e7765SFlorian Westphal 20002c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20012c9e7765SFlorian Westphal if (skb) { 20022c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20032c9e7765SFlorian Westphal 20042c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20052c9e7765SFlorian Westphal return INT_MAX; 20062c9e7765SFlorian Westphal 20072c9e7765SFlorian Westphal return (unsigned int)hint_val; 20082c9e7765SFlorian Westphal } 20092c9e7765SFlorian Westphal 20102c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20112c9e7765SFlorian Westphal return 1; 20122c9e7765SFlorian Westphal 20132c9e7765SFlorian Westphal return 0; 20142c9e7765SFlorian Westphal } 20152c9e7765SFlorian Westphal 2016f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2017f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 2018f870fa0bSMat Martineau { 2019f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2020b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2021b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20227a6a6cbcSPaolo Abeni int target; 20237a6a6cbcSPaolo Abeni long timeo; 2024f870fa0bSMat Martineau 2025cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2026cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2027cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2028cb9d80f4SPaolo Abeni 20296511882cSPaolo Abeni lock_sock(sk); 2030fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2031fd897679SPaolo Abeni copied = -ENOTCONN; 2032fd897679SPaolo Abeni goto out_err; 2033fd897679SPaolo Abeni } 2034fd897679SPaolo Abeni 20357a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 20367a6a6cbcSPaolo Abeni 20377a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20387a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20397a6a6cbcSPaolo Abeni 20402c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20412c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20422c9e7765SFlorian Westphal 204305e3eceaSEric Dumazet while (copied < len) { 2044e3859603SPaolo Abeni int bytes_read; 20457a6a6cbcSPaolo Abeni 2046b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20476771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20487a6a6cbcSPaolo Abeni if (!copied) 20497a6a6cbcSPaolo Abeni copied = bytes_read; 20506771bfd9SFlorian Westphal goto out_err; 2051cec37a6eSPeter Krystad } 2052cec37a6eSPeter Krystad 20537a6a6cbcSPaolo Abeni copied += bytes_read; 20547a6a6cbcSPaolo Abeni 2055ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2056fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2057ea4ca586SPaolo Abeni 2058e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2059e3859603SPaolo Abeni continue; 2060e3859603SPaolo Abeni 20617a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 20627a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 20637a6a6cbcSPaolo Abeni */ 20647a6a6cbcSPaolo Abeni if (copied >= target) 20657a6a6cbcSPaolo Abeni break; 20667a6a6cbcSPaolo Abeni 20677a6a6cbcSPaolo Abeni if (copied) { 20687a6a6cbcSPaolo Abeni if (sk->sk_err || 20697a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 20707a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 20717a6a6cbcSPaolo Abeni !timeo || 20727a6a6cbcSPaolo Abeni signal_pending(current)) 20737a6a6cbcSPaolo Abeni break; 20747a6a6cbcSPaolo Abeni } else { 20757a6a6cbcSPaolo Abeni if (sk->sk_err) { 20767a6a6cbcSPaolo Abeni copied = sock_error(sk); 20777a6a6cbcSPaolo Abeni break; 20787a6a6cbcSPaolo Abeni } 20797a6a6cbcSPaolo Abeni 20805969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 20815969856aSPaolo Abeni mptcp_check_for_eof(msk); 20825969856aSPaolo Abeni 208387952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 208487952603SPaolo Abeni /* race breaker: the shutdown could be after the 208587952603SPaolo Abeni * previous receive queue check 208687952603SPaolo Abeni */ 2087e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 208887952603SPaolo Abeni continue; 20897a6a6cbcSPaolo Abeni break; 209087952603SPaolo Abeni } 20917a6a6cbcSPaolo Abeni 20927a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 20937a6a6cbcSPaolo Abeni copied = -ENOTCONN; 20947a6a6cbcSPaolo Abeni break; 20957a6a6cbcSPaolo Abeni } 20967a6a6cbcSPaolo Abeni 20977a6a6cbcSPaolo Abeni if (!timeo) { 20987a6a6cbcSPaolo Abeni copied = -EAGAIN; 20997a6a6cbcSPaolo Abeni break; 21007a6a6cbcSPaolo Abeni } 21017a6a6cbcSPaolo Abeni 21027a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21037a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21047a6a6cbcSPaolo Abeni break; 21057a6a6cbcSPaolo Abeni } 21067a6a6cbcSPaolo Abeni } 21077a6a6cbcSPaolo Abeni 21087a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2109612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21107a6a6cbcSPaolo Abeni } 21113c90e377SPaolo Abeni 21126771bfd9SFlorian Westphal out_err: 2113b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2114b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2115b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21162c9e7765SFlorian Westphal 21172c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21182c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21192c9e7765SFlorian Westphal 21202c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21212c9e7765SFlorian Westphal } 2122b7f653b2SFlorian Westphal } 2123b7f653b2SFlorian Westphal 2124612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2125612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2126612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2127ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2128a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2129a6b118feSFlorian Westphal 2130cec37a6eSPeter Krystad release_sock(sk); 2131cec37a6eSPeter Krystad return copied; 2132cec37a6eSPeter Krystad } 2133cec37a6eSPeter Krystad 2134b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2135b51f9b80SPaolo Abeni { 2136b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2137b51f9b80SPaolo Abeni icsk_retransmit_timer); 2138b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21392d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2140b51f9b80SPaolo Abeni 2141b51f9b80SPaolo Abeni bh_lock_sock(sk); 2142b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21432d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21442d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21452d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2146b51f9b80SPaolo Abeni } else { 2147b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 21482d6f5a2bSPaolo Abeni set_bit(MPTCP_RETRANSMIT, &msk->flags); 2149b51f9b80SPaolo Abeni } 2150b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2151b51f9b80SPaolo Abeni sock_put(sk); 2152b51f9b80SPaolo Abeni } 2153b51f9b80SPaolo Abeni 2154e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2155e16163b6SPaolo Abeni { 2156e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2157e16163b6SPaolo Abeni 2158e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2159b6d69fc8SFlorian Westphal sock_put(sk); 2160e16163b6SPaolo Abeni } 2161e16163b6SPaolo Abeni 21623b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 21633b1d6210SPaolo Abeni * level. 21643b1d6210SPaolo Abeni * 21653b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 21663b1d6210SPaolo Abeni */ 2167ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 21683b1d6210SPaolo Abeni { 216971b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 21703b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 217171b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 21723b1d6210SPaolo Abeni 21733b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 21743b1d6210SPaolo Abeni 2175d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2176d9ca1de8SPaolo Abeni return NULL; 2177d5f49190SPaolo Abeni 21783b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 21793b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21803b1d6210SPaolo Abeni 2181ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2182d5f49190SPaolo Abeni continue; 2183d5f49190SPaolo Abeni 218471b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 218571b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 218671b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 218771b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2188860975c6SFlorian Westphal continue; 2189860975c6SFlorian Westphal } 21903b1d6210SPaolo Abeni 21913b1d6210SPaolo Abeni if (subflow->backup) { 21923b1d6210SPaolo Abeni if (!backup) 21933b1d6210SPaolo Abeni backup = ssk; 21943b1d6210SPaolo Abeni continue; 21953b1d6210SPaolo Abeni } 21963b1d6210SPaolo Abeni 219771b7dec2SPaolo Abeni if (!pick) 219871b7dec2SPaolo Abeni pick = ssk; 21993b1d6210SPaolo Abeni } 22003b1d6210SPaolo Abeni 220171b7dec2SPaolo Abeni if (pick) 220271b7dec2SPaolo Abeni return pick; 220371b7dec2SPaolo Abeni 220471b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 220571b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22063b1d6210SPaolo Abeni } 22073b1d6210SPaolo Abeni 220817aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 220917aee05dSFlorian Westphal { 221017aee05dSFlorian Westphal if (msk->subflow) { 221117aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 221217aee05dSFlorian Westphal msk->subflow = NULL; 221317aee05dSFlorian Westphal } 221417aee05dSFlorian Westphal } 221517aee05dSFlorian Westphal 22161e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22171e1d9d6fSPaolo Abeni { 22181e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22191e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22201e1d9d6fSPaolo Abeni 22211e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22221e1d9d6fSPaolo Abeni return false; 22231e1d9d6fSPaolo Abeni 22241e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22251e1d9d6fSPaolo Abeni return false; 22261e1d9d6fSPaolo Abeni 22271e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22281e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22291e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22301e1d9d6fSPaolo Abeni */ 22311e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22321e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22331e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22341e1d9d6fSPaolo Abeni if (!rtx_head) { 22351e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22361e1d9d6fSPaolo Abeni return false; 22371e1d9d6fSPaolo Abeni } 22381e1d9d6fSPaolo Abeni 22391e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22401e1d9d6fSPaolo Abeni msk->recovery = true; 22411e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22421e1d9d6fSPaolo Abeni 22431e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22441e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22451e1d9d6fSPaolo Abeni 22461e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22471e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22481e1d9d6fSPaolo Abeni if (!cur->already_sent) 22491e1d9d6fSPaolo Abeni break; 22501e1d9d6fSPaolo Abeni cur->already_sent = 0; 22511e1d9d6fSPaolo Abeni } 22521e1d9d6fSPaolo Abeni 22531e1d9d6fSPaolo Abeni return true; 22541e1d9d6fSPaolo Abeni } 22551e1d9d6fSPaolo Abeni 2256b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2257b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2258b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2259b29fcfb5SPaolo Abeni 2260cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2261cec37a6eSPeter Krystad * (accept). 2262cec37a6eSPeter Krystad * 2263cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2264cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2265cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2266cec37a6eSPeter Krystad * parent socket. 2267cec37a6eSPeter Krystad */ 2268a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2269b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2270b29fcfb5SPaolo Abeni unsigned int flags) 2271cec37a6eSPeter Krystad { 2272e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2273b29fcfb5SPaolo Abeni bool need_push, dispose_it; 2274e0be4931SFlorian Westphal 2275b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2276b29fcfb5SPaolo Abeni if (dispose_it) 2277cec37a6eSPeter Krystad list_del(&subflow->node); 2278cec37a6eSPeter Krystad 22793f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2280e16163b6SPaolo Abeni 2281b29fcfb5SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) 2282b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2283b29fcfb5SPaolo Abeni 2284b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2285b29fcfb5SPaolo Abeni if (!dispose_it) { 2286b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2287b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2288b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2289b29fcfb5SPaolo Abeni release_sock(ssk); 2290b29fcfb5SPaolo Abeni 2291b29fcfb5SPaolo Abeni goto out; 2292b29fcfb5SPaolo Abeni } 2293b29fcfb5SPaolo Abeni 2294e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2295e16163b6SPaolo Abeni * already orphaned 2296e16163b6SPaolo Abeni */ 2297866f26f2SPaolo Abeni if (ssk->sk_socket) 2298e16163b6SPaolo Abeni sock_orphan(ssk); 2299e16163b6SPaolo Abeni 2300d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2301d7b1bfd0SPaolo Abeni 2302e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2303e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2304e16163b6SPaolo Abeni * reference owned by msk; 2305e16163b6SPaolo Abeni */ 2306e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2307e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2308e16163b6SPaolo Abeni } else { 2309d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2310e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2311e16163b6SPaolo Abeni 2312e16163b6SPaolo Abeni /* close acquired an extra ref */ 2313e16163b6SPaolo Abeni __sock_put(ssk); 2314e16163b6SPaolo Abeni } 2315e16163b6SPaolo Abeni release_sock(ssk); 2316e16163b6SPaolo Abeni 2317e16163b6SPaolo Abeni sock_put(ssk); 2318e0be4931SFlorian Westphal 2319c8fe62f0SFlorian Westphal if (ssk == msk->first) 2320c8fe62f0SFlorian Westphal msk->first = NULL; 2321c8fe62f0SFlorian Westphal 2322b29fcfb5SPaolo Abeni out: 2323b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2324b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23251e1d9d6fSPaolo Abeni 23261e1d9d6fSPaolo Abeni if (need_push) 23271e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2328f870fa0bSMat Martineau } 2329f870fa0bSMat Martineau 2330a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2331a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2332a141e02eSFlorian Westphal { 2333b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2334b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2335b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2336a141e02eSFlorian Westphal } 2337a141e02eSFlorian Westphal 2338dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2339dc24f8b4SPaolo Abeni { 2340dc24f8b4SPaolo Abeni return 0; 2341dc24f8b4SPaolo Abeni } 2342dc24f8b4SPaolo Abeni 23430e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 23440e4f35d7SPaolo Abeni { 23450e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 23460e4f35d7SPaolo Abeni 23473abc05d9SFlorian Westphal might_sleep(); 23483abc05d9SFlorian Westphal 23490e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 23500e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 23510e4f35d7SPaolo Abeni 23520e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 23530e4f35d7SPaolo Abeni continue; 23540e4f35d7SPaolo Abeni 235540947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 235640947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 235740947e13SFlorian Westphal continue; 235840947e13SFlorian Westphal 2359a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 23600e4f35d7SPaolo Abeni } 23610e4f35d7SPaolo Abeni } 23620e4f35d7SPaolo Abeni 2363e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2364e16163b6SPaolo Abeni { 2365e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2366e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2367e16163b6SPaolo Abeni 2368e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2369e16163b6SPaolo Abeni return true; 2370e16163b6SPaolo Abeni 2371e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2372e16163b6SPaolo Abeni * timeout 2373e16163b6SPaolo Abeni */ 2374e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2375e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2376e16163b6SPaolo Abeni TCP_CLOSE) 2377e16163b6SPaolo Abeni return false; 2378e16163b6SPaolo Abeni } 2379e16163b6SPaolo Abeni return true; 2380e16163b6SPaolo Abeni } 2381e16163b6SPaolo Abeni 238250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 238350c504a2SFlorian Westphal { 238450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 238550c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 238650c504a2SFlorian Westphal 238750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 238850c504a2SFlorian Westphal return; 238950c504a2SFlorian Westphal 239050c504a2SFlorian Westphal mptcp_token_destroy(msk); 239150c504a2SFlorian Westphal 239250c504a2SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 239350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 239475e908c3SPaolo Abeni bool slow; 239550c504a2SFlorian Westphal 239675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 239750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 239850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 239950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 240050c504a2SFlorian Westphal } 240175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 240250c504a2SFlorian Westphal } 240350c504a2SFlorian Westphal 240450c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 240550c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 240650c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 240750c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 240850c504a2SFlorian Westphal 240950c504a2SFlorian Westphal mptcp_close_wake_up(sk); 241050c504a2SFlorian Westphal } 241150c504a2SFlorian Westphal 24122948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 241380992017SPaolo Abeni { 24142948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2415caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 24163b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 24173b1d6210SPaolo Abeni size_t copied = 0; 24182948d0a1SPaolo Abeni struct sock *ssk; 24192948d0a1SPaolo Abeni int ret; 24202948d0a1SPaolo Abeni 2421b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 24223241a9c0SFlorian Westphal 24233241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 24243241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 24252948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 24266477dd39SMat Martineau if (!dfrag) { 24276477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 24286477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 24296477dd39SMat Martineau 24306477dd39SMat Martineau icsk->icsk_retransmits++; 24316477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 24326477dd39SMat Martineau mptcp_send_ack(msk); 24336477dd39SMat Martineau 24346477dd39SMat Martineau goto reset_timer; 24356477dd39SMat Martineau } 24366477dd39SMat Martineau 24373241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 24382948d0a1SPaolo Abeni return; 24393241a9c0SFlorian Westphal 24403241a9c0SFlorian Westphal goto reset_timer; 24416477dd39SMat Martineau } 24422948d0a1SPaolo Abeni 24432948d0a1SPaolo Abeni if (!ssk) 24442948d0a1SPaolo Abeni goto reset_timer; 24452948d0a1SPaolo Abeni 24462948d0a1SPaolo Abeni lock_sock(ssk); 24472948d0a1SPaolo Abeni 24482948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 24492948d0a1SPaolo Abeni info.sent = 0; 24504e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 24514e14867dSPaolo Abeni while (info.sent < info.limit) { 24522948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 24532948d0a1SPaolo Abeni if (ret <= 0) 24542948d0a1SPaolo Abeni break; 24552948d0a1SPaolo Abeni 24562948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 24572948d0a1SPaolo Abeni copied += ret; 24582948d0a1SPaolo Abeni info.sent += ret; 24592948d0a1SPaolo Abeni } 24604e14867dSPaolo Abeni if (copied) { 24614e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 24622948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 24632948d0a1SPaolo Abeni info.size_goal); 24644e14867dSPaolo Abeni } 24652948d0a1SPaolo Abeni 24662948d0a1SPaolo Abeni release_sock(ssk); 24672948d0a1SPaolo Abeni 24682948d0a1SPaolo Abeni reset_timer: 24693241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 24703241a9c0SFlorian Westphal 24712948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 24722948d0a1SPaolo Abeni mptcp_reset_timer(sk); 24732948d0a1SPaolo Abeni } 24742948d0a1SPaolo Abeni 24752948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 24762948d0a1SPaolo Abeni { 24772948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 24782948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 24792948d0a1SPaolo Abeni int state; 248080992017SPaolo Abeni 248180992017SPaolo Abeni lock_sock(sk); 2482e16163b6SPaolo Abeni state = sk->sk_state; 2483e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2484e16163b6SPaolo Abeni goto unlock; 2485e16163b6SPaolo Abeni 248643b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 248778962489SFlorian Westphal mptcp_flush_join_list(msk); 248850c504a2SFlorian Westphal 248950c504a2SFlorian Westphal mptcp_check_fastclose(msk); 249050c504a2SFlorian Westphal 2491b416268bSFlorian Westphal if (msk->pm.status) 2492e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2493b416268bSFlorian Westphal 249459832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 249559832e24SFlorian Westphal mptcp_check_for_eof(msk); 249659832e24SFlorian Westphal 24976e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 249843b54c6eSMat Martineau mptcp_check_data_fin(sk); 249943b54c6eSMat Martineau 2500341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2501341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2502341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2503e16163b6SPaolo Abeni */ 2504e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2505341c6524SPaolo Abeni (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2506e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2507e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2508e16163b6SPaolo Abeni goto unlock; 2509e16163b6SPaolo Abeni } 2510e16163b6SPaolo Abeni 2511b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2512b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2513b263b0d7SFlorian Westphal 25142948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 25152948d0a1SPaolo Abeni __mptcp_retrans(sk); 25163b1d6210SPaolo Abeni 25173b1d6210SPaolo Abeni unlock: 251880992017SPaolo Abeni release_sock(sk); 251980992017SPaolo Abeni sock_put(sk); 252080992017SPaolo Abeni } 252180992017SPaolo Abeni 2522784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2523f870fa0bSMat Martineau { 2524cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2525cec37a6eSPeter Krystad 2526ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 2527ec3edaa7SPeter Krystad 2528cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2529ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 253018b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 253180992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 253287952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2533ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2534f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 25356511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2536ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 253733d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2538cec37a6eSPeter Krystad 25398ab183deSPaolo Abeni msk->first = NULL; 2540dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2541752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 25421e1d9d6fSPaolo Abeni msk->recovery = false; 25438ab183deSPaolo Abeni 25441b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 25451b1c7a0eSPeter Krystad 2546b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2547b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2548e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2549aa1fbd94SFlorian Westphal 2550f870fa0bSMat Martineau return 0; 2551f870fa0bSMat Martineau } 2552f870fa0bSMat Martineau 2553b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2554784325e9SMatthieu Baerts { 255520b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2556b29fcfb5SPaolo Abeni 2557b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2558b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2559b29fcfb5SPaolo Abeni 2560b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2561b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2562b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2563b29fcfb5SPaolo Abeni } 2564b29fcfb5SPaolo Abeni 2565b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2566b29fcfb5SPaolo Abeni { 2567fc518953SFlorian Westphal struct net *net = sock_net(sk); 2568fc518953SFlorian Westphal int ret; 256918b683bfSPaolo Abeni 2570b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2571b6c08380SGeliang Tang if (ret) 2572b6c08380SGeliang Tang return ret; 2573b6c08380SGeliang Tang 2574fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2575fc518953SFlorian Westphal return -ENOPROTOOPT; 2576fc518953SFlorian Westphal 2577fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2578fc518953SFlorian Westphal return -ENOMEM; 2579fc518953SFlorian Westphal 2580fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2581fa68018dSPaolo Abeni if (ret) 2582fa68018dSPaolo Abeni return ret; 2583fa68018dSPaolo Abeni 258420b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 258520b5759fSPaolo Abeni * propagate the correct value 258620b5759fSPaolo Abeni */ 2587b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 258820b5759fSPaolo Abeni 2589d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2590a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2591da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2592d027236cSPaolo Abeni 259318b683bfSPaolo Abeni return 0; 259418b683bfSPaolo Abeni } 259518b683bfSPaolo Abeni 259618b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 259718b683bfSPaolo Abeni { 259818b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 259918b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 260018b683bfSPaolo Abeni 2601d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 260218b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2603d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2604784325e9SMatthieu Baerts } 2605784325e9SMatthieu Baerts 260680992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 260780992017SPaolo Abeni { 260880992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 260980992017SPaolo Abeni 2610b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2611e16163b6SPaolo Abeni __sock_put(sk); 261280992017SPaolo Abeni } 261380992017SPaolo Abeni 2614d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 261521498490SPeter Krystad { 261621498490SPeter Krystad lock_sock(ssk); 261721498490SPeter Krystad 261821498490SPeter Krystad switch (ssk->sk_state) { 261921498490SPeter Krystad case TCP_LISTEN: 262021498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 262121498490SPeter Krystad break; 2622df561f66SGustavo A. R. Silva fallthrough; 262321498490SPeter Krystad case TCP_SYN_SENT: 262421498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 262521498490SPeter Krystad break; 262621498490SPeter Krystad default: 262743b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 262843b54c6eSMat Martineau pr_debug("Fallback"); 262921498490SPeter Krystad ssk->sk_shutdown |= how; 263021498490SPeter Krystad tcp_shutdown(ssk, how); 263143b54c6eSMat Martineau } else { 263243b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 263343b54c6eSMat Martineau tcp_send_ack(ssk); 26346477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 26356477dd39SMat Martineau mptcp_reset_timer(sk); 263643b54c6eSMat Martineau } 263721498490SPeter Krystad break; 263821498490SPeter Krystad } 263921498490SPeter Krystad 264021498490SPeter Krystad release_sock(ssk); 264121498490SPeter Krystad } 264221498490SPeter Krystad 26436920b851SMat Martineau static const unsigned char new_state[16] = { 26446920b851SMat Martineau /* current state: new state: action: */ 26456920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 26466920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26476920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 26486920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26496920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 26506920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 26516920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 26526920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 26536920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 26546920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 26556920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 26566920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 26576920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 26586920b851SMat Martineau }; 26596920b851SMat Martineau 26606920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 26616920b851SMat Martineau { 26626920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 26636920b851SMat Martineau int ns = next & TCP_STATE_MASK; 26646920b851SMat Martineau 26656920b851SMat Martineau inet_sk_state_store(sk, ns); 26666920b851SMat Martineau 26676920b851SMat Martineau return next & TCP_ACTION_FIN; 26686920b851SMat Martineau } 26696920b851SMat Martineau 2670e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2671e16163b6SPaolo Abeni { 2672e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2673e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2674e16163b6SPaolo Abeni 2675e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2676e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2677e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2678e16163b6SPaolo Abeni 2679e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2680e16163b6SPaolo Abeni * skip this 2681e16163b6SPaolo Abeni */ 2682e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2683e16163b6SPaolo Abeni mptcp_send_head(sk)) 2684e16163b6SPaolo Abeni return; 2685e16163b6SPaolo Abeni 2686e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2687e16163b6SPaolo Abeni 268826aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 268926aa2314SPaolo Abeni * state now 269026aa2314SPaolo Abeni */ 269126aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 269258cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 269326aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2694e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2695e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 269626aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 269726aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 269826aa2314SPaolo Abeni } 2699e16163b6SPaolo Abeni } 2700e16163b6SPaolo Abeni 270178962489SFlorian Westphal mptcp_flush_join_list(msk); 2702e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2703e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2704e16163b6SPaolo Abeni 2705e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2706e16163b6SPaolo Abeni } 2707e16163b6SPaolo Abeni } 2708e16163b6SPaolo Abeni 2709e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2710e16163b6SPaolo Abeni { 2711e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2712e16163b6SPaolo Abeni 2713e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2714e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2715e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2716e16163b6SPaolo Abeni 2717e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2718e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2719e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2720e16163b6SPaolo Abeni 2721e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2722e16163b6SPaolo Abeni } 2723e16163b6SPaolo Abeni 2724e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2725f870fa0bSMat Martineau { 2726cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2727f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2728b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2729f870fa0bSMat Martineau 2730e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 27312c22c06cSFlorian Westphal 27323abc05d9SFlorian Westphal might_sleep(); 27333abc05d9SFlorian Westphal 273410f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 273510f6d46cSPaolo Abeni * mptcp_finish_join(). 273610f6d46cSPaolo Abeni */ 273710f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 273810f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 273910f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2740b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2741b2c5b614SFlorian Westphal 27426e628cd3SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2743e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2744e16163b6SPaolo Abeni msk->pm.status = 0; 2745b2c5b614SFlorian Westphal 2746b29fcfb5SPaolo Abeni /* clears msk->subflow, allowing the following loop to close 2747b29fcfb5SPaolo Abeni * even the initial subflow 2748b29fcfb5SPaolo Abeni */ 2749b29fcfb5SPaolo Abeni mptcp_dispose_initial_subflow(msk); 2750b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2751cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2752b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, 0); 2753f870fa0bSMat Martineau } 2754f870fa0bSMat Martineau 2755e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2756e16163b6SPaolo Abeni 27576511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 275887952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2759e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2760e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2761aa1fbd94SFlorian Westphal 2762e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2763e16163b6SPaolo Abeni sock_put(sk); 2764e16163b6SPaolo Abeni } 2765e16163b6SPaolo Abeni 2766e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2767e16163b6SPaolo Abeni { 2768e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2769e16163b6SPaolo Abeni bool do_cancel_work = false; 2770e16163b6SPaolo Abeni 2771e16163b6SPaolo Abeni lock_sock(sk); 2772e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2773e16163b6SPaolo Abeni 2774e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2775e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2776e16163b6SPaolo Abeni goto cleanup; 2777e16163b6SPaolo Abeni } 2778e16163b6SPaolo Abeni 2779e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2780e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2781e16163b6SPaolo Abeni 2782e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2783e16163b6SPaolo Abeni 2784e16163b6SPaolo Abeni cleanup: 2785e16163b6SPaolo Abeni /* orphan all the subflows */ 2786e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 278744227915SGeliang Tang mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2788e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 278949054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2790e16163b6SPaolo Abeni 2791e16163b6SPaolo Abeni sock_orphan(ssk); 2792e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2793e16163b6SPaolo Abeni } 2794e16163b6SPaolo Abeni sock_orphan(sk); 2795e16163b6SPaolo Abeni 2796e16163b6SPaolo Abeni sock_hold(sk); 2797e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2798b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 2799b29fcfb5SPaolo Abeni mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2800b29fcfb5SPaolo Abeni 2801e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2802e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2803e16163b6SPaolo Abeni do_cancel_work = true; 2804e16163b6SPaolo Abeni } else { 2805e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2806e16163b6SPaolo Abeni } 2807e16163b6SPaolo Abeni release_sock(sk); 2808e16163b6SPaolo Abeni if (do_cancel_work) 280980992017SPaolo Abeni mptcp_cancel_work(sk); 2810b911c97cSFlorian Westphal 2811e16163b6SPaolo Abeni sock_put(sk); 2812f870fa0bSMat Martineau } 2813f870fa0bSMat Martineau 2814cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2815cf7da0d6SPeter Krystad { 2816cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2817cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2818cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2819cf7da0d6SPeter Krystad 2820cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2821cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2822cf7da0d6SPeter Krystad 2823cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2824cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2825cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2826cf7da0d6SPeter Krystad } 2827cf7da0d6SPeter Krystad #endif 2828cf7da0d6SPeter Krystad 2829cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2830cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2831cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2832cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2833cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2834cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2835cf7da0d6SPeter Krystad } 2836cf7da0d6SPeter Krystad 283718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 283818b683bfSPaolo Abeni { 283976e2a55dSPaolo Abeni struct mptcp_subflow_context *subflow; 284076e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 284176e2a55dSPaolo Abeni 284278962489SFlorian Westphal mptcp_do_flush_join_list(msk); 284378962489SFlorian Westphal 2844b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2845b29fcfb5SPaolo Abeni 284613a9499eSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 284713a9499eSPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 284813a9499eSPaolo Abeni 2849b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_FASTCLOSE); 285013a9499eSPaolo Abeni } 2851b29fcfb5SPaolo Abeni 2852b29fcfb5SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2853b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2854b29fcfb5SPaolo Abeni 2855b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 2856b29fcfb5SPaolo Abeni mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2857b29fcfb5SPaolo Abeni 2858b29fcfb5SPaolo Abeni mptcp_destroy_common(msk); 2859b29fcfb5SPaolo Abeni msk->last_snd = NULL; 2860b29fcfb5SPaolo Abeni msk->flags = 0; 2861b29fcfb5SPaolo Abeni msk->recovery = false; 2862b29fcfb5SPaolo Abeni msk->can_ack = false; 2863b29fcfb5SPaolo Abeni msk->fully_established = false; 2864b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 2865b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 2866b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 2867b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 2868b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2869b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 2870b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 2871b29fcfb5SPaolo Abeni 2872b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 2873b29fcfb5SPaolo Abeni sk_error_report(sk); 287442c556feSFlorian Westphal return 0; 287518b683bfSPaolo Abeni } 287618b683bfSPaolo Abeni 2877b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2878b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2879b0519de8SFlorian Westphal { 2880b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2881b0519de8SFlorian Westphal 2882b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2883b0519de8SFlorian Westphal } 2884b0519de8SFlorian Westphal #endif 2885b0519de8SFlorian Westphal 2886fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2887cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2888fca5c82cSPaolo Abeni struct request_sock *req) 2889b0519de8SFlorian Westphal { 289058b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2891b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 289258b09919SPaolo Abeni struct mptcp_sock *msk; 289358b09919SPaolo Abeni u64 ack_seq; 2894b0519de8SFlorian Westphal 2895b0519de8SFlorian Westphal if (!nsk) 2896b0519de8SFlorian Westphal return NULL; 2897b0519de8SFlorian Westphal 2898b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2899b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2900b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2901b0519de8SFlorian Westphal #endif 2902b0519de8SFlorian Westphal 290358b09919SPaolo Abeni __mptcp_init_sock(nsk); 290458b09919SPaolo Abeni 290558b09919SPaolo Abeni msk = mptcp_sk(nsk); 290658b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 290758b09919SPaolo Abeni msk->token = subflow_req->token; 290858b09919SPaolo Abeni msk->subflow = NULL; 2909b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 291074c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 291106251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 291258b09919SPaolo Abeni 291358b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2914eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 29157439d687SPaolo Abeni msk->snd_una = msk->write_seq; 29167439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2917df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 29186f8a612aSFlorian Westphal 291974c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 292058b09919SPaolo Abeni msk->can_ack = true; 2921cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 292258b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 292358b09919SPaolo Abeni ack_seq++; 2924917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2925fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 292658b09919SPaolo Abeni } 29277f20d5fcSPaolo Abeni 29285e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 29297f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 29307f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 29310c148460SPaolo Abeni 29320c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 293358b09919SPaolo Abeni bh_unlock_sock(nsk); 293458b09919SPaolo Abeni 293558b09919SPaolo Abeni /* keep a single reference */ 293658b09919SPaolo Abeni __sock_put(nsk); 2937b0519de8SFlorian Westphal return nsk; 2938b0519de8SFlorian Westphal } 2939b0519de8SFlorian Westphal 2940a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2941a6b118feSFlorian Westphal { 2942a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2943a6b118feSFlorian Westphal 2944a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2945a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2946a6b118feSFlorian Westphal 2947a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2948a6b118feSFlorian Westphal 2949a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2950a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2951a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2952a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2953a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 29546f8a612aSFlorian Westphal 29557439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2956a6b118feSFlorian Westphal } 2957a6b118feSFlorian Westphal 2958cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2959cf7da0d6SPeter Krystad bool kern) 2960cf7da0d6SPeter Krystad { 2961cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2962cf7da0d6SPeter Krystad struct socket *listener; 2963cf7da0d6SPeter Krystad struct sock *newsk; 2964cf7da0d6SPeter Krystad 2965cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2966cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2967cf7da0d6SPeter Krystad *err = -EINVAL; 2968cf7da0d6SPeter Krystad return NULL; 2969cf7da0d6SPeter Krystad } 2970cf7da0d6SPeter Krystad 2971cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2972cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2973cf7da0d6SPeter Krystad if (!newsk) 2974cf7da0d6SPeter Krystad return NULL; 2975cf7da0d6SPeter Krystad 2976cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2977cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2978cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2979cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2980cf7da0d6SPeter Krystad 2981cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 298258b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 298358b09919SPaolo Abeni 298458b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 298558b09919SPaolo Abeni * subflow_syn_recv_sock() 298658b09919SPaolo Abeni */ 298758b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 298858b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 2989d6692b3bSFlorian Westphal goto out; 299058b09919SPaolo Abeni } 299158b09919SPaolo Abeni 299258b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 299358b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2994cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 29950397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2996fc518953SFlorian Westphal } else { 2997fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2998fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2999cf7da0d6SPeter Krystad } 3000cf7da0d6SPeter Krystad 3001d6692b3bSFlorian Westphal out: 3002d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3003cf7da0d6SPeter Krystad return newsk; 3004cf7da0d6SPeter Krystad } 3005cf7da0d6SPeter Krystad 30065c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 30075c8c1640SGeliang Tang { 300887952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 300987952603SPaolo Abeni 30106e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 30116e628cd3SPaolo Abeni 301287952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3013b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 301487952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 30156511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 30165c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3017b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 30186511882cSPaolo Abeni 30196511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 30206511882cSPaolo Abeni * inet_sock_destruct() will dispose it 30216511882cSPaolo Abeni */ 30226511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 30236511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 30245c8c1640SGeliang Tang mptcp_token_destroy(msk); 30255c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 30265c8c1640SGeliang Tang } 30275c8c1640SGeliang Tang 302879c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 302979c0949eSPeter Krystad { 3030c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3031c9fd9c5fSFlorian Westphal 30325c8c1640SGeliang Tang mptcp_destroy_common(msk); 3033d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 303479c0949eSPeter Krystad } 303579c0949eSPeter Krystad 30366e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 30376e628cd3SPaolo Abeni { 30386e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 30396e628cd3SPaolo Abeni __mptcp_clean_una(sk); 30406e628cd3SPaolo Abeni else 30416e628cd3SPaolo Abeni set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 30426e628cd3SPaolo Abeni 30436e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 30446e628cd3SPaolo Abeni mptcp_schedule_work(sk); 30456e628cd3SPaolo Abeni } 30466e628cd3SPaolo Abeni 3047219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 30486e628cd3SPaolo Abeni { 30496e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 30506e628cd3SPaolo Abeni return; 30516e628cd3SPaolo Abeni 305240dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 3053b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 3054b19bc294SPaolo Abeni 3055b19bc294SPaolo Abeni if (xmit_ssk == ssk) 30566e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3057b19bc294SPaolo Abeni else if (xmit_ssk) 3058bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND); 305940dc9416SPaolo Abeni } else { 30606e628cd3SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 30616e628cd3SPaolo Abeni } 306240dc9416SPaolo Abeni } 30636e628cd3SPaolo Abeni 3064e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 306514c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 306614c441b5SPaolo Abeni { 3067c2e6048fSPaolo Abeni for (;;) { 30682d6f5a2bSPaolo Abeni unsigned long flags = 0; 30692d6f5a2bSPaolo Abeni 3070c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 30712e5de7e0SDan Carpenter flags |= BIT(MPTCP_PUSH_PENDING); 30722d6f5a2bSPaolo Abeni if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) 30732d6f5a2bSPaolo Abeni flags |= BIT(MPTCP_RETRANSMIT); 3074c2e6048fSPaolo Abeni if (!flags) 3075c2e6048fSPaolo Abeni break; 3076c2e6048fSPaolo Abeni 3077c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 30786e628cd3SPaolo Abeni * 30796e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 30806e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 30816e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 30826e628cd3SPaolo Abeni * the subflow socket lock 30836e628cd3SPaolo Abeni */ 30846e628cd3SPaolo Abeni 30856e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 30862e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3087c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 30882d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 30892d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3090c2e6048fSPaolo Abeni 3091c2e6048fSPaolo Abeni cond_resched(); 30926e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 30936e628cd3SPaolo Abeni } 3094c2e6048fSPaolo Abeni 3095490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 3096490274b4SPaolo Abeni * depending on sk_state 3097490274b4SPaolo Abeni */ 3098490274b4SPaolo Abeni if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags)) 3099490274b4SPaolo Abeni __mptcp_set_connected(sk); 3100c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 3101417789dfSPaolo Abeni __mptcp_clean_una_wakeup(sk); 310215cc1045SPaolo Abeni if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) 310315cc1045SPaolo Abeni __mptcp_error_report(sk); 31046e628cd3SPaolo Abeni 310587952603SPaolo Abeni __mptcp_update_rmem(sk); 310614c441b5SPaolo Abeni } 310714c441b5SPaolo Abeni 3108bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3109bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3110bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3111bcd97734SPaolo Abeni */ 3112bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3113bcd97734SPaolo Abeni { 3114bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3115bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3116bcd97734SPaolo Abeni unsigned long timeout; 3117bcd97734SPaolo Abeni 3118bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3119bcd97734SPaolo Abeni return; 3120bcd97734SPaolo Abeni 3121bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3122bcd97734SPaolo Abeni if (tp->srtt_us) 3123bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3124bcd97734SPaolo Abeni else 3125bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3126bcd97734SPaolo Abeni timeout += jiffies; 3127bcd97734SPaolo Abeni 3128bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3129bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3130bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3131bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3132bcd97734SPaolo Abeni } 3133bcd97734SPaolo Abeni 3134b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3135b19bc294SPaolo Abeni { 3136b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3137b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3138b19bc294SPaolo Abeni 3139bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3140b19bc294SPaolo Abeni mptcp_data_lock(sk); 3141b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3142b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3143b19bc294SPaolo Abeni else 3144b19bc294SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 3145b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3146bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3147bcd97734SPaolo Abeni } 3148bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3149bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3150bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3151bcd97734SPaolo Abeni } 3152b19bc294SPaolo Abeni } 3153b19bc294SPaolo Abeni 31542c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 31552c5ebd00SPaolo Abeni { 31562c5ebd00SPaolo Abeni /* should never be called, 31572c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 31582c5ebd00SPaolo Abeni */ 31592c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 31602c5ebd00SPaolo Abeni return 0; 31612c5ebd00SPaolo Abeni } 31622c5ebd00SPaolo Abeni 31632c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 31642c5ebd00SPaolo Abeni { 31652c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 31662c5ebd00SPaolo Abeni } 31672c5ebd00SPaolo Abeni 3168cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3169f870fa0bSMat Martineau { 3170f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3171cec37a6eSPeter Krystad struct socket *ssock; 3172f870fa0bSMat Martineau 3173cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3174cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3175cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3176cec37a6eSPeter Krystad return -EINVAL; 3177f870fa0bSMat Martineau 3178cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3179cec37a6eSPeter Krystad } 3180f870fa0bSMat Martineau 3181cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3182cec37a6eSPeter Krystad { 3183cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3184cec37a6eSPeter Krystad struct mptcp_sock *msk; 3185cec37a6eSPeter Krystad struct sock *sk; 31866d0060f6SMat Martineau u64 ack_seq; 3187f870fa0bSMat Martineau 3188cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3189cec37a6eSPeter Krystad sk = subflow->conn; 3190cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3191cec37a6eSPeter Krystad 3192648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3193648ef4b8SMat Martineau 31946d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 31956d0060f6SMat Martineau ack_seq++; 3196648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3197648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 31986d0060f6SMat Martineau 3199cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3200cec37a6eSPeter Krystad * accessing the field below 3201cec37a6eSPeter Krystad */ 3202cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3203cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 32046d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3205eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 32066d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3207fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3208d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 32097439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 32101b1c7a0eSPeter Krystad 32116c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3212a6b118feSFlorian Westphal 3213a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3214f870fa0bSMat Martineau } 3215f870fa0bSMat Martineau 3216866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3217cf7da0d6SPeter Krystad { 3218cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3219cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3220cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3221cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3222cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3223cf7da0d6SPeter Krystad } 3224cf7da0d6SPeter Krystad 3225e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3226f296234cSPeter Krystad { 3227e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3228f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3229f296234cSPeter Krystad struct sock *parent = (void *)msk; 3230f296234cSPeter Krystad struct socket *parent_sock; 3231ec3edaa7SPeter Krystad bool ret; 3232f296234cSPeter Krystad 3233f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3234f296234cSPeter Krystad 3235f296234cSPeter Krystad /* mptcp socket already closing? */ 3236dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3237dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3238f296234cSPeter Krystad return false; 3239dc87efdbSFlorian Westphal } 3240f296234cSPeter Krystad 3241f296234cSPeter Krystad if (!msk->pm.server_side) 3242b911c97cSFlorian Westphal goto out; 3243f296234cSPeter Krystad 3244dc87efdbSFlorian Westphal if (!mptcp_pm_allow_new_subflow(msk)) { 3245dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 324610f6d46cSPaolo Abeni return false; 3247dc87efdbSFlorian Westphal } 324810f6d46cSPaolo Abeni 324910f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 325010f6d46cSPaolo Abeni * msk lock here. 325110f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 3252e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 325310f6d46cSPaolo Abeni */ 325410f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 325510f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3256e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 325710f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3258e16163b6SPaolo Abeni sock_hold(ssk); 3259e16163b6SPaolo Abeni } 326010f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 3261dc87efdbSFlorian Westphal if (!ret) { 3262dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 326310f6d46cSPaolo Abeni return false; 3264dc87efdbSFlorian Westphal } 326510f6d46cSPaolo Abeni 326610f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 326710f6d46cSPaolo Abeni * at close time 326810f6d46cSPaolo Abeni */ 3269f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 3270e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 3271e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 3272917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 3273b911c97cSFlorian Westphal out: 3274b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 327510f6d46cSPaolo Abeni return true; 3276f296234cSPeter Krystad } 3277f296234cSPeter Krystad 327876e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 327976e2a55dSPaolo Abeni { 328076e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 328176e2a55dSPaolo Abeni 328276e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 328376e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 328476e2a55dSPaolo Abeni } 328576e2a55dSPaolo Abeni 32866511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 32876511882cSPaolo Abeni { 32886511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 32896511882cSPaolo Abeni } 32906511882cSPaolo Abeni 3291644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3292644807e3SFlorian Westphal { 3293644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3294644807e3SFlorian Westphal u64 delta; 3295644807e3SFlorian Westphal 3296644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3297644807e3SFlorian Westphal return -EINVAL; 3298644807e3SFlorian Westphal 3299644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3300644807e3SFlorian Westphal return 0; 3301644807e3SFlorian Westphal 3302644807e3SFlorian Westphal delta = msk->write_seq - v; 3303644807e3SFlorian Westphal if (delta > INT_MAX) 3304644807e3SFlorian Westphal delta = INT_MAX; 3305644807e3SFlorian Westphal 3306644807e3SFlorian Westphal return (int)delta; 3307644807e3SFlorian Westphal } 3308644807e3SFlorian Westphal 3309644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3310644807e3SFlorian Westphal { 3311644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3312644807e3SFlorian Westphal bool slow; 3313644807e3SFlorian Westphal int answ; 3314644807e3SFlorian Westphal 3315644807e3SFlorian Westphal switch (cmd) { 3316644807e3SFlorian Westphal case SIOCINQ: 3317644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3318644807e3SFlorian Westphal return -EINVAL; 3319644807e3SFlorian Westphal 3320644807e3SFlorian Westphal lock_sock(sk); 3321644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3322644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3323644807e3SFlorian Westphal release_sock(sk); 3324644807e3SFlorian Westphal break; 3325644807e3SFlorian Westphal case SIOCOUTQ: 3326644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3327644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3328644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3329644807e3SFlorian Westphal break; 3330644807e3SFlorian Westphal case SIOCOUTQNSD: 3331644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3332644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3333644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3334644807e3SFlorian Westphal break; 3335644807e3SFlorian Westphal default: 3336644807e3SFlorian Westphal return -ENOIOCTLCMD; 3337644807e3SFlorian Westphal } 3338644807e3SFlorian Westphal 3339644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3340644807e3SFlorian Westphal } 3341644807e3SFlorian Westphal 3342f870fa0bSMat Martineau static struct proto mptcp_prot = { 3343f870fa0bSMat Martineau .name = "MPTCP", 3344f870fa0bSMat Martineau .owner = THIS_MODULE, 3345f870fa0bSMat Martineau .init = mptcp_init_sock, 334618b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3347f870fa0bSMat Martineau .close = mptcp_close, 3348cf7da0d6SPeter Krystad .accept = mptcp_accept, 3349717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3350717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 335176e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 335279c0949eSPeter Krystad .destroy = mptcp_destroy, 3353f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3354644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3355f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 335614c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 33572c5ebd00SPaolo Abeni .hash = mptcp_hash, 33582c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3359cec37a6eSPeter Krystad .get_port = mptcp_get_port, 33606511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3361d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 3362d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 3363d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3364d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3365989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3366d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3367f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 33682c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3369f870fa0bSMat Martineau .no_autobind = true, 3370f870fa0bSMat Martineau }; 3371f870fa0bSMat Martineau 33722303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 33732303f994SPeter Krystad { 33742303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 33752303f994SPeter Krystad struct socket *ssock; 3376cf7da0d6SPeter Krystad int err; 33772303f994SPeter Krystad 33782303f994SPeter Krystad lock_sock(sock->sk); 3379fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3380fa68018dSPaolo Abeni if (!ssock) { 3381fa68018dSPaolo Abeni err = -EINVAL; 33822303f994SPeter Krystad goto unlock; 33832303f994SPeter Krystad } 33842303f994SPeter Krystad 33852303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3386cf7da0d6SPeter Krystad if (!err) 3387cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 33882303f994SPeter Krystad 33892303f994SPeter Krystad unlock: 33902303f994SPeter Krystad release_sock(sock->sk); 33912303f994SPeter Krystad return err; 33922303f994SPeter Krystad } 33932303f994SPeter Krystad 33940235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 33950235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 33960235d075SPaolo Abeni { 33970235d075SPaolo Abeni subflow->request_mptcp = 0; 33980235d075SPaolo Abeni __mptcp_do_fallback(msk); 33990235d075SPaolo Abeni } 34000235d075SPaolo Abeni 34012303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 34022303f994SPeter Krystad int addr_len, int flags) 34032303f994SPeter Krystad { 34042303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 34052c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 34062303f994SPeter Krystad struct socket *ssock; 34072303f994SPeter Krystad int err; 34082303f994SPeter Krystad 34092303f994SPeter Krystad lock_sock(sock->sk); 341041be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 341141be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 341241be81a8SPaolo Abeni * cope with that 341341be81a8SPaolo Abeni */ 341441be81a8SPaolo Abeni ssock = msk->subflow; 341541be81a8SPaolo Abeni goto do_connect; 341641be81a8SPaolo Abeni } 341741be81a8SPaolo Abeni 3418fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3419fa68018dSPaolo Abeni if (!ssock) { 3420fa68018dSPaolo Abeni err = -EINVAL; 34212303f994SPeter Krystad goto unlock; 34222303f994SPeter Krystad } 34232303f994SPeter Krystad 3424fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3425fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 34262c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3427cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3428cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3429cf7da0d6SPeter Krystad * TCP option space. 3430cf7da0d6SPeter Krystad */ 3431cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 34320235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3433cf7da0d6SPeter Krystad #endif 3434a16195e3SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3435a16195e3SPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 34360235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3437a16195e3SPaolo Abeni } 34385695eb88SPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 34395695eb88SPaolo Abeni MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3440cf7da0d6SPeter Krystad 344141be81a8SPaolo Abeni do_connect: 34422303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 344341be81a8SPaolo Abeni sock->state = ssock->state; 344441be81a8SPaolo Abeni 344541be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 344641be81a8SPaolo Abeni * subflow_finish_connect() 344741be81a8SPaolo Abeni */ 3448367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3449cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 345041be81a8SPaolo Abeni else 345141be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 34522303f994SPeter Krystad 34532303f994SPeter Krystad unlock: 34542303f994SPeter Krystad release_sock(sock->sk); 34552303f994SPeter Krystad return err; 34562303f994SPeter Krystad } 34572303f994SPeter Krystad 3458cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3459cf7da0d6SPeter Krystad { 3460cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3461cf7da0d6SPeter Krystad struct socket *ssock; 3462cf7da0d6SPeter Krystad int err; 3463cf7da0d6SPeter Krystad 3464cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3465cf7da0d6SPeter Krystad 3466cf7da0d6SPeter Krystad lock_sock(sock->sk); 3467fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3468fa68018dSPaolo Abeni if (!ssock) { 3469fa68018dSPaolo Abeni err = -EINVAL; 3470cf7da0d6SPeter Krystad goto unlock; 3471cf7da0d6SPeter Krystad } 3472cf7da0d6SPeter Krystad 3473fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3474fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 34755e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 34765e20087dSFlorian Westphal 3477cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3478cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3479cf7da0d6SPeter Krystad if (!err) 3480cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3481cf7da0d6SPeter Krystad 3482cf7da0d6SPeter Krystad unlock: 3483cf7da0d6SPeter Krystad release_sock(sock->sk); 3484cf7da0d6SPeter Krystad return err; 3485cf7da0d6SPeter Krystad } 3486cf7da0d6SPeter Krystad 3487cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3488cf7da0d6SPeter Krystad int flags, bool kern) 3489cf7da0d6SPeter Krystad { 3490cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3491cf7da0d6SPeter Krystad struct socket *ssock; 3492cf7da0d6SPeter Krystad int err; 3493cf7da0d6SPeter Krystad 3494cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3495cf7da0d6SPeter Krystad 3496cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3497cf7da0d6SPeter Krystad if (!ssock) 3498*71ba088cSPaolo Abeni return -EINVAL; 3499cf7da0d6SPeter Krystad 3500cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3501d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3502cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3503cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 35040397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 35050397c6d8SPaolo Abeni 35064d54cc32SFlorian Westphal lock_sock(newsk); 35075b950ff4SPaolo Abeni 35085b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 35095b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 35105b950ff4SPaolo Abeni * we can notify it, if needed. 3511c8fe62f0SFlorian Westphal * 3512c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3513c8fe62f0SFlorian Westphal * the refcnt is still at least one. 35145b950ff4SPaolo Abeni */ 35155b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 35165b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 35175b950ff4SPaolo Abeni sock_hold(msk->first); 35185b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 35196c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 35205b950ff4SPaolo Abeni 35210397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 35220397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 35235cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3524cf7da0d6SPeter Krystad 3525cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3526cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3527cf7da0d6SPeter Krystad */ 352878962489SFlorian Westphal mptcp_flush_join_list(msk); 3529190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3530cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3531cf7da0d6SPeter Krystad 3532cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3533cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3534cf7da0d6SPeter Krystad } 35354d54cc32SFlorian Westphal release_sock(newsk); 3536cf7da0d6SPeter Krystad } 3537cf7da0d6SPeter Krystad 3538cf7da0d6SPeter Krystad return err; 3539cf7da0d6SPeter Krystad } 3540cf7da0d6SPeter Krystad 35418a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 35428a05661bSPaolo Abeni { 3543612f71d7SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 3544612f71d7SPaolo Abeni * always show at least one non-empty queue when checked in this order. 3545612f71d7SPaolo Abeni */ 3546612f71d7SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 3547612f71d7SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 3548612f71d7SPaolo Abeni return 0; 3549612f71d7SPaolo Abeni 3550612f71d7SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 35518a05661bSPaolo Abeni } 35528a05661bSPaolo Abeni 35538edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 35548edf0864SFlorian Westphal { 35558edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 35568edf0864SFlorian Westphal 35578edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3558dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 35598edf0864SFlorian Westphal 35608edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 35618edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 35628edf0864SFlorian Westphal 35635cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 35646e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 35656e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 35666e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 35678edf0864SFlorian Westphal 35686e628cd3SPaolo Abeni return 0; 35698edf0864SFlorian Westphal } 35708edf0864SFlorian Westphal 35712303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 35722303f994SPeter Krystad struct poll_table_struct *wait) 35732303f994SPeter Krystad { 35741891c4a0SFlorian Westphal struct sock *sk = sock->sk; 35758ab183deSPaolo Abeni struct mptcp_sock *msk; 35762303f994SPeter Krystad __poll_t mask = 0; 35778a05661bSPaolo Abeni int state; 35782303f994SPeter Krystad 35791891c4a0SFlorian Westphal msk = mptcp_sk(sk); 35801891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 35811891c4a0SFlorian Westphal 35828a05661bSPaolo Abeni state = inet_sk_state_load(sk); 35836719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 3584*71ba088cSPaolo Abeni if (state == TCP_LISTEN) { 3585*71ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 3586*71ba088cSPaolo Abeni return 0; 3587*71ba088cSPaolo Abeni 3588*71ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 3589*71ba088cSPaolo Abeni } 35908a05661bSPaolo Abeni 35918a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 35928a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 35938edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 35948a05661bSPaolo Abeni } 3595dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3596dd913410SPaolo Abeni mask |= EPOLLHUP; 35971891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 35981891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 35991891c4a0SFlorian Westphal 360015cc1045SPaolo Abeni /* This barrier is coupled with smp_wmb() in tcp_reset() */ 360115cc1045SPaolo Abeni smp_rmb(); 360215cc1045SPaolo Abeni if (sk->sk_err) 360315cc1045SPaolo Abeni mask |= EPOLLERR; 360415cc1045SPaolo Abeni 36052303f994SPeter Krystad return mask; 36062303f994SPeter Krystad } 36072303f994SPeter Krystad 3608e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3609e42f1ac6SFlorian Westphal .family = PF_INET, 3610e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 36110a3cc579SPaolo Abeni .release = inet_release, 3612e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3613e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3614e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3615e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3616d2f77c53SPaolo Abeni .getname = inet_getname, 3617e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3618e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3619e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3620e42f1ac6SFlorian Westphal .listen = mptcp_listen, 362176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3622e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3623e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3624e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3625e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3626e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3627e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3628e42f1ac6SFlorian Westphal }; 36292303f994SPeter Krystad 3630f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3631f870fa0bSMat Martineau .type = SOCK_STREAM, 3632f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3633f870fa0bSMat Martineau .prot = &mptcp_prot, 36342303f994SPeter Krystad .ops = &mptcp_stream_ops, 36352303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3636f870fa0bSMat Martineau }; 3637f870fa0bSMat Martineau 3638b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3639b19bc294SPaolo Abeni { 3640b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3641b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3642b19bc294SPaolo Abeni int work_done = 0; 3643b19bc294SPaolo Abeni 3644b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3645b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3646b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3647b19bc294SPaolo Abeni 3648b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3649b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3650b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3651b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3652b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3653b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3654b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3655b19bc294SPaolo Abeni * CPU that scheduled it. 3656b19bc294SPaolo Abeni */ 3657b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3658b19bc294SPaolo Abeni sock_put(ssk); 3659b19bc294SPaolo Abeni 3660b19bc294SPaolo Abeni if (++work_done == budget) 3661b19bc294SPaolo Abeni return budget; 3662b19bc294SPaolo Abeni } 3663b19bc294SPaolo Abeni 3664b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3665b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3666b19bc294SPaolo Abeni */ 3667b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3668b19bc294SPaolo Abeni return work_done; 3669b19bc294SPaolo Abeni } 3670b19bc294SPaolo Abeni 3671d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3672f870fa0bSMat Martineau { 3673b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3674b19bc294SPaolo Abeni int cpu; 3675b19bc294SPaolo Abeni 36762303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 36772303f994SPeter Krystad 3678d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3679d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3680d027236cSPaolo Abeni 3681b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3682b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3683b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3684b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 3685b19bc294SPaolo Abeni netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3686b19bc294SPaolo Abeni NAPI_POLL_WEIGHT); 3687b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3688b19bc294SPaolo Abeni } 3689b19bc294SPaolo Abeni 36902303f994SPeter Krystad mptcp_subflow_init(); 36911b1c7a0eSPeter Krystad mptcp_pm_init(); 36922c5ebd00SPaolo Abeni mptcp_token_init(); 36932303f994SPeter Krystad 3694f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3695f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3696f870fa0bSMat Martineau 3697f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 36986771bfd9SFlorian Westphal 36996771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3700f870fa0bSMat Martineau } 3701f870fa0bSMat Martineau 3702f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3703e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3704e42f1ac6SFlorian Westphal .family = PF_INET6, 3705e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 37060a3cc579SPaolo Abeni .release = inet6_release, 3707e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3708e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3709e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3710e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3711d2f77c53SPaolo Abeni .getname = inet6_getname, 3712e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3713e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3714e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3715e42f1ac6SFlorian Westphal .listen = mptcp_listen, 371676e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3717e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3718e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3719e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3720e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3721e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3722e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3723e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 37243986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3725e42f1ac6SFlorian Westphal #endif 3726e42f1ac6SFlorian Westphal }; 3727e42f1ac6SFlorian Westphal 3728f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3729f870fa0bSMat Martineau 373079c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 373179c0949eSPeter Krystad { 373279c0949eSPeter Krystad mptcp_destroy(sk); 373379c0949eSPeter Krystad inet6_destroy_sock(sk); 373479c0949eSPeter Krystad } 373579c0949eSPeter Krystad 3736f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3737f870fa0bSMat Martineau .type = SOCK_STREAM, 3738f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3739f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 37402303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3741f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3742f870fa0bSMat Martineau }; 3743f870fa0bSMat Martineau 3744d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3745f870fa0bSMat Martineau { 3746f870fa0bSMat Martineau int err; 3747f870fa0bSMat Martineau 3748f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3749f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3750f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 375179c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3752b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3753f870fa0bSMat Martineau 3754f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3755f870fa0bSMat Martineau if (err) 3756f870fa0bSMat Martineau return err; 3757f870fa0bSMat Martineau 3758f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3759f870fa0bSMat Martineau if (err) 3760f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3761f870fa0bSMat Martineau 3762f870fa0bSMat Martineau return err; 3763f870fa0bSMat Martineau } 3764f870fa0bSMat Martineau #endif 3765