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> 25*644807e3SFlorian 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; 1375d5f49190SPaolo Abeni u64 ratio; 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 140033d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140133d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140233d41c9cSPaolo Abeni * additionally updates the rtx timeout 140333d41c9cSPaolo Abeni */ 14045cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1405f296234cSPeter Krystad { 1406d5f49190SPaolo Abeni struct subflow_send_info send_info[2]; 1407f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 140833d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 1409d5f49190SPaolo Abeni int i, nr_active = 0; 1410d5f49190SPaolo Abeni struct sock *ssk; 141133d41c9cSPaolo Abeni long tout = 0; 1412d5f49190SPaolo Abeni u64 ratio; 1413d5f49190SPaolo Abeni u32 pace; 1414f296234cSPeter Krystad 141533d41c9cSPaolo Abeni sock_owned_by_me(sk); 1416f296234cSPeter Krystad 1417d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1418d5f49190SPaolo Abeni if (!msk->first) 1419f296234cSPeter Krystad return NULL; 1420d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1421f296234cSPeter Krystad } 1422f296234cSPeter Krystad 1423d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1424d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1425d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 142633d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 142733d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1428d5f49190SPaolo Abeni return msk->last_snd; 142933d41c9cSPaolo Abeni } 1430f296234cSPeter Krystad 1431d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 1432d5f49190SPaolo Abeni for (i = 0; i < 2; ++i) { 1433d5f49190SPaolo Abeni send_info[i].ssk = NULL; 1434d5f49190SPaolo Abeni send_info[i].ratio = -1; 1435d5f49190SPaolo Abeni } 1436d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1437e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1438d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1439d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1440f296234cSPeter Krystad continue; 1441d5f49190SPaolo Abeni 144233d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1443d5f49190SPaolo Abeni nr_active += !subflow->backup; 1444ec369c3aSPaolo Abeni if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd) 1445d5f49190SPaolo Abeni continue; 1446d5f49190SPaolo Abeni 1447d5f49190SPaolo Abeni pace = READ_ONCE(ssk->sk_pacing_rate); 1448d5f49190SPaolo Abeni if (!pace) 1449d5f49190SPaolo Abeni continue; 1450d5f49190SPaolo Abeni 1451d5f49190SPaolo Abeni ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1452d5f49190SPaolo Abeni pace); 1453d5f49190SPaolo Abeni if (ratio < send_info[subflow->backup].ratio) { 1454d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 1455d5f49190SPaolo Abeni send_info[subflow->backup].ratio = ratio; 1456d5f49190SPaolo Abeni } 1457f296234cSPeter Krystad } 145833d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1459f296234cSPeter Krystad 1460d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1461d5f49190SPaolo Abeni if (!nr_active) 1462d5f49190SPaolo Abeni send_info[0].ssk = send_info[1].ssk; 1463d5f49190SPaolo Abeni 1464d5f49190SPaolo Abeni if (send_info[0].ssk) { 1465d5f49190SPaolo Abeni msk->last_snd = send_info[0].ssk; 1466d5f49190SPaolo Abeni msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1467ec369c3aSPaolo Abeni tcp_sk(msk->last_snd)->snd_wnd); 1468d5f49190SPaolo Abeni return msk->last_snd; 1469d5f49190SPaolo Abeni } 14705cf92bbaSPaolo Abeni 1471d5f49190SPaolo Abeni return NULL; 1472f296234cSPeter Krystad } 1473f296234cSPeter Krystad 1474b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1475d9ca1de8SPaolo Abeni { 1476d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1477d9ca1de8SPaolo Abeni release_sock(ssk); 1478d9ca1de8SPaolo Abeni } 1479d9ca1de8SPaolo Abeni 14800d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14810d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14820d199e43SFlorian Westphal u32 sent) 14830d199e43SFlorian Westphal { 14840d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 14850d199e43SFlorian Westphal 14860d199e43SFlorian Westphal dfrag->already_sent += sent; 14870d199e43SFlorian Westphal 14880d199e43SFlorian Westphal msk->snd_burst -= sent; 14890d199e43SFlorian Westphal 14900d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 14910d199e43SFlorian Westphal 14920d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 14930d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 14940d199e43SFlorian Westphal * old segments. 14950d199e43SFlorian Westphal * 14960d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 14970d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 14980d199e43SFlorian Westphal * that has been handed to the subflow for transmission 14990d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15000d199e43SFlorian Westphal */ 15010d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15020d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15030d199e43SFlorian Westphal } 15040d199e43SFlorian Westphal 15053241a9c0SFlorian Westphal static void mptcp_check_and_set_pending(struct sock *sk) 15063241a9c0SFlorian Westphal { 15073241a9c0SFlorian Westphal if (mptcp_send_head(sk) && 15083241a9c0SFlorian Westphal !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 15093241a9c0SFlorian Westphal set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 15103241a9c0SFlorian Westphal } 15113241a9c0SFlorian Westphal 1512ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1513d9ca1de8SPaolo Abeni { 1514d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1515d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1516d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1517d9ca1de8SPaolo Abeni .flags = flags, 1518d9ca1de8SPaolo Abeni }; 1519d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1520d9ca1de8SPaolo Abeni int len, copied = 0; 1521d9ca1de8SPaolo Abeni 1522d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1523d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1524d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1525d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1526d9ca1de8SPaolo Abeni while (len > 0) { 1527d9ca1de8SPaolo Abeni int ret = 0; 1528d9ca1de8SPaolo Abeni 1529d9ca1de8SPaolo Abeni prev_ssk = ssk; 153078962489SFlorian Westphal mptcp_flush_join_list(msk); 15315cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1532d9ca1de8SPaolo Abeni 15339758f40eSPaolo Abeni /* First check. If the ssk has changed since 15349758f40eSPaolo Abeni * the last round, release prev_ssk 1535d9ca1de8SPaolo Abeni */ 1536d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1537b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1538d9ca1de8SPaolo Abeni if (!ssk) 1539d9ca1de8SPaolo Abeni goto out; 1540d9ca1de8SPaolo Abeni 15419758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15429758f40eSPaolo Abeni * from the previous one, otherwise we are still 15439758f40eSPaolo Abeni * helding the relevant lock 15449758f40eSPaolo Abeni */ 15459758f40eSPaolo Abeni if (ssk != prev_ssk) 1546d9ca1de8SPaolo Abeni lock_sock(ssk); 1547d9ca1de8SPaolo Abeni 1548d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1549d9ca1de8SPaolo Abeni if (ret <= 0) { 1550b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1551d9ca1de8SPaolo Abeni goto out; 1552d9ca1de8SPaolo Abeni } 1553d9ca1de8SPaolo Abeni 1554d9ca1de8SPaolo Abeni info.sent += ret; 1555d9ca1de8SPaolo Abeni copied += ret; 1556d9ca1de8SPaolo Abeni len -= ret; 15570d199e43SFlorian Westphal 15580d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1559d9ca1de8SPaolo Abeni } 1560d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1561d9ca1de8SPaolo Abeni } 1562d9ca1de8SPaolo Abeni 1563d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1564d9ca1de8SPaolo Abeni if (ssk) 1565b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1566d9ca1de8SPaolo Abeni 1567d9ca1de8SPaolo Abeni out: 156833d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1569d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1570d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 157133d41c9cSPaolo Abeni if (copied) 1572d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1573d9ca1de8SPaolo Abeni } 1574d9ca1de8SPaolo Abeni 15756e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 15766e628cd3SPaolo Abeni { 15776e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15781094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 15791094c6feSPaolo Abeni .data_lock_held = true, 15801094c6feSPaolo Abeni }; 15816e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1582b19bc294SPaolo Abeni struct sock *xmit_ssk; 15836e628cd3SPaolo Abeni int len, copied = 0; 1584b19bc294SPaolo Abeni bool first = true; 15856e628cd3SPaolo Abeni 15866e628cd3SPaolo Abeni info.flags = 0; 15876e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 15886e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 15896e628cd3SPaolo Abeni info.limit = dfrag->data_len; 15906e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 15916e628cd3SPaolo Abeni while (len > 0) { 15926e628cd3SPaolo Abeni int ret = 0; 15936e628cd3SPaolo Abeni 1594b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1595b19bc294SPaolo Abeni * check for a different subflow usage only after 1596b19bc294SPaolo Abeni * spooling the first chunk of data 1597b19bc294SPaolo Abeni */ 1598b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1599b19bc294SPaolo Abeni if (!xmit_ssk) 1600b19bc294SPaolo Abeni goto out; 1601b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1602bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1603bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1604b19bc294SPaolo Abeni goto out; 1605b19bc294SPaolo Abeni } 1606b19bc294SPaolo Abeni 16076e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16086e628cd3SPaolo Abeni if (ret <= 0) 16096e628cd3SPaolo Abeni goto out; 16106e628cd3SPaolo Abeni 16116e628cd3SPaolo Abeni info.sent += ret; 16126e628cd3SPaolo Abeni copied += ret; 16136e628cd3SPaolo Abeni len -= ret; 1614b19bc294SPaolo Abeni first = false; 16150d199e43SFlorian Westphal 16160d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16176e628cd3SPaolo Abeni } 16186e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16196e628cd3SPaolo Abeni } 16206e628cd3SPaolo Abeni 16216e628cd3SPaolo Abeni out: 16226e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16236e628cd3SPaolo Abeni * not going to flush it via release_sock() 16246e628cd3SPaolo Abeni */ 16256e628cd3SPaolo Abeni if (copied) { 16266e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16276e628cd3SPaolo Abeni info.size_goal); 1628d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1629d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1630d09d818eSPaolo Abeni 16316e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16326e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16336e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16346e628cd3SPaolo Abeni } 16356e628cd3SPaolo Abeni } 16366e628cd3SPaolo Abeni 16375cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16385cf92bbaSPaolo Abeni { 16395cf92bbaSPaolo Abeni /* enable autotune */ 16405cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16415cf92bbaSPaolo Abeni 16425cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16435cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16445cf92bbaSPaolo Abeni } 16455cf92bbaSPaolo Abeni 1646f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1647f870fa0bSMat Martineau { 1648f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 164917091708SFlorian Westphal struct page_frag *pfrag; 16506d0060f6SMat Martineau size_t copied = 0; 1651caf971dfSPaolo Abeni int ret = 0; 16526d0060f6SMat Martineau long timeo; 1653f870fa0bSMat Martineau 1654987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1655987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1656f870fa0bSMat Martineau return -EOPNOTSUPP; 1657f870fa0bSMat Martineau 1658987858e5SPaolo Abeni /* silently ignore everything else */ 1659987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1660987858e5SPaolo Abeni 16616511882cSPaolo Abeni lock_sock(sk); 16621954b860SMat Martineau 16631954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 16641954b860SMat Martineau 16651954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 16661954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 16671954b860SMat Martineau if (ret) 16681954b860SMat Martineau goto out; 16691954b860SMat Martineau } 16701954b860SMat Martineau 167117091708SFlorian Westphal pfrag = sk_page_frag(sk); 167218b683bfSPaolo Abeni 1673d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1674724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1675d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1676d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1677d9ca1de8SPaolo Abeni size_t psize, offset; 1678d9ca1de8SPaolo Abeni 167957baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 168057baaf28SMat Martineau ret = -EPIPE; 168157baaf28SMat Martineau goto out; 168257baaf28SMat Martineau } 168357baaf28SMat Martineau 1684d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1685d9ca1de8SPaolo Abeni * page allocator 1686fb529e62SFlorian Westphal */ 1687d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1688d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1689d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1690d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1691d9ca1de8SPaolo Abeni goto wait_for_memory; 16926e628cd3SPaolo Abeni 1693d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1694d9ca1de8SPaolo Abeni goto wait_for_memory; 1695d9ca1de8SPaolo Abeni 1696d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1697d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1698fb529e62SFlorian Westphal } 1699fb529e62SFlorian Westphal 1700d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1701d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1702d9ca1de8SPaolo Abeni * anyway 1703d9ca1de8SPaolo Abeni */ 1704d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1705d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1706d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1707724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17088ce568edSPaolo Abeni 17096511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1710d9ca1de8SPaolo Abeni goto wait_for_memory; 1711d9ca1de8SPaolo Abeni 1712d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1713d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1714d9ca1de8SPaolo Abeni ret = -EFAULT; 1715d9ca1de8SPaolo Abeni goto out; 1716d9ca1de8SPaolo Abeni } 1717d9ca1de8SPaolo Abeni 1718d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17196511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1720d9ca1de8SPaolo Abeni copied += psize; 1721d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1722d9ca1de8SPaolo Abeni frag_truesize += psize; 1723d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1724d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1725d9ca1de8SPaolo Abeni 1726d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1727d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1728d9ca1de8SPaolo Abeni */ 1729d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1730d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1731d9ca1de8SPaolo Abeni get_page(dfrag->page); 1732d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1733d9ca1de8SPaolo Abeni if (!msk->first_pending) 1734d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1735d9ca1de8SPaolo Abeni } 1736e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1737d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1738d9ca1de8SPaolo Abeni !dfrag_collapsed); 1739d9ca1de8SPaolo Abeni 1740d9ca1de8SPaolo Abeni continue; 1741d9ca1de8SPaolo Abeni 1742d9ca1de8SPaolo Abeni wait_for_memory: 17435cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1744c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1745f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1746f296234cSPeter Krystad if (ret) 1747f296234cSPeter Krystad goto out; 1748cec37a6eSPeter Krystad } 1749cec37a6eSPeter Krystad 175013e16037SPaolo Abeni if (copied) 1751c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1752da51aef5SPaolo Abeni 17531954b860SMat Martineau out: 1754cec37a6eSPeter Krystad release_sock(sk); 17558555c6bfSPaolo Abeni return copied ? : ret; 1756f870fa0bSMat Martineau } 1757f870fa0bSMat Martineau 17586771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 17596771bfd9SFlorian Westphal struct msghdr *msg, 1760b7f653b2SFlorian Westphal size_t len, int flags, 1761b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1762b7f653b2SFlorian Westphal int *cmsg_flags) 17636771bfd9SFlorian Westphal { 1764ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 17656771bfd9SFlorian Westphal int copied = 0; 17666771bfd9SFlorian Westphal 1767ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 17686771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 17696771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 17706771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 17716771bfd9SFlorian Westphal int err; 17726771bfd9SFlorian Westphal 1773d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 17746771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 17756771bfd9SFlorian Westphal if (unlikely(err < 0)) { 17766771bfd9SFlorian Westphal if (!copied) 17776771bfd9SFlorian Westphal return err; 17786771bfd9SFlorian Westphal break; 17796771bfd9SFlorian Westphal } 1780d976092cSPaolo Abeni } 17816771bfd9SFlorian Westphal 1782b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1783b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1784b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1785b7f653b2SFlorian Westphal } 1786b7f653b2SFlorian Westphal 17876771bfd9SFlorian Westphal copied += count; 17886771bfd9SFlorian Westphal 17896771bfd9SFlorian Westphal if (count < data_len) { 17902c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 17916771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 17922c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 17932c9e7765SFlorian Westphal } 17946771bfd9SFlorian Westphal break; 17956771bfd9SFlorian Westphal } 17966771bfd9SFlorian Westphal 1797ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 179887952603SPaolo Abeni /* we will bulk release the skb memory later */ 179987952603SPaolo Abeni skb->destructor = NULL; 1800ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 180187952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18026771bfd9SFlorian Westphal __kfree_skb(skb); 1803ca4fb892SYonglong Li } 18046771bfd9SFlorian Westphal 18056771bfd9SFlorian Westphal if (copied >= len) 18066771bfd9SFlorian Westphal break; 18076771bfd9SFlorian Westphal } 18086771bfd9SFlorian Westphal 18096771bfd9SFlorian Westphal return copied; 18106771bfd9SFlorian Westphal } 18116771bfd9SFlorian Westphal 1812a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1813a6b118feSFlorian Westphal * 1814a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1815a6b118feSFlorian Westphal */ 1816a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1817a6b118feSFlorian Westphal { 1818a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1819a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1820a6b118feSFlorian Westphal u32 time, advmss = 1; 1821a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1822a6b118feSFlorian Westphal 1823a6b118feSFlorian Westphal sock_owned_by_me(sk); 1824a6b118feSFlorian Westphal 1825a6b118feSFlorian Westphal if (copied <= 0) 1826a6b118feSFlorian Westphal return; 1827a6b118feSFlorian Westphal 1828a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1829a6b118feSFlorian Westphal 1830a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1831a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1832a6b118feSFlorian Westphal 1833a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1834a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1835a6b118feSFlorian Westphal return; 1836a6b118feSFlorian Westphal 1837a6b118feSFlorian Westphal rtt_us = 0; 1838a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1839a6b118feSFlorian Westphal const struct tcp_sock *tp; 1840a6b118feSFlorian Westphal u64 sf_rtt_us; 1841a6b118feSFlorian Westphal u32 sf_advmss; 1842a6b118feSFlorian Westphal 1843a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1844a6b118feSFlorian Westphal 1845a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1846a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1847a6b118feSFlorian Westphal 1848a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1849a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1850a6b118feSFlorian Westphal } 1851a6b118feSFlorian Westphal 1852a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1853a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1854a6b118feSFlorian Westphal return; 1855a6b118feSFlorian Westphal 1856a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1857a6b118feSFlorian Westphal goto new_measure; 1858a6b118feSFlorian Westphal 1859a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1860a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1861a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1862a6b118feSFlorian Westphal u64 rcvwin, grow; 1863a6b118feSFlorian Westphal 1864a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1865a6b118feSFlorian Westphal 1866a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1867a6b118feSFlorian Westphal 1868a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1869a6b118feSFlorian Westphal rcvwin += (grow << 1); 1870a6b118feSFlorian Westphal 1871a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1872a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1873a6b118feSFlorian Westphal rcvmem += 128; 1874a6b118feSFlorian Westphal 1875a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1876a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1877a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1878a6b118feSFlorian Westphal 1879a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1880a6b118feSFlorian Westphal u32 window_clamp; 1881a6b118feSFlorian Westphal 1882a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1883a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1884a6b118feSFlorian Westphal 1885a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1886a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1887a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1888a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1889a6b118feSFlorian Westphal */ 1890a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1891a6b118feSFlorian Westphal struct sock *ssk; 1892c76c6956SPaolo Abeni bool slow; 1893a6b118feSFlorian Westphal 1894a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1895c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1896a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1897a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1898c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1899c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1900a6b118feSFlorian Westphal } 1901a6b118feSFlorian Westphal } 1902a6b118feSFlorian Westphal } 1903a6b118feSFlorian Westphal 1904a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1905a6b118feSFlorian Westphal new_measure: 1906a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1907a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1908a6b118feSFlorian Westphal } 1909a6b118feSFlorian Westphal 191087952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 191187952603SPaolo Abeni { 191287952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 191387952603SPaolo Abeni 191487952603SPaolo Abeni if (!msk->rmem_released) 191587952603SPaolo Abeni return; 191687952603SPaolo Abeni 191787952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19186511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1919ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 192087952603SPaolo Abeni } 192187952603SPaolo Abeni 192287952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 192387952603SPaolo Abeni { 192487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 192587952603SPaolo Abeni 192687952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 192787952603SPaolo Abeni } 192887952603SPaolo Abeni 1929e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19306771bfd9SFlorian Westphal { 193187952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19326771bfd9SFlorian Westphal unsigned int moved = 0; 193387952603SPaolo Abeni bool ret, done; 1934d5f49190SPaolo Abeni 193578962489SFlorian Westphal mptcp_flush_join_list(msk); 19366771bfd9SFlorian Westphal do { 19376771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 193865f49fe7SFlorian Westphal bool slowpath; 19396771bfd9SFlorian Westphal 194087952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 194187952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 194287952603SPaolo Abeni * that is an unlikely slow path. 194387952603SPaolo Abeni */ 194487952603SPaolo Abeni if (likely(!ssk)) 19456771bfd9SFlorian Westphal break; 19466771bfd9SFlorian Westphal 194765f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 194887952603SPaolo Abeni mptcp_data_lock(sk); 1949e3859603SPaolo Abeni __mptcp_update_rmem(sk); 19506771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 195187952603SPaolo Abeni mptcp_data_unlock(sk); 1952499ada50SPaolo Abeni 1953499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 1954499ada50SPaolo Abeni __mptcp_error_report(sk); 195565f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 19566771bfd9SFlorian Westphal } while (!done); 19576771bfd9SFlorian Westphal 195887952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 195987952603SPaolo Abeni ret = moved > 0; 196087952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 196187952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 196287952603SPaolo Abeni mptcp_data_lock(sk); 196387952603SPaolo Abeni __mptcp_update_rmem(sk); 196487952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 196587952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 196687952603SPaolo Abeni mptcp_data_unlock(sk); 1967ab174ad8SPaolo Abeni } 196887952603SPaolo Abeni if (ret) 196987952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 197087952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 19716771bfd9SFlorian Westphal } 19726771bfd9SFlorian Westphal 19732c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 19742c9e7765SFlorian Westphal { 19752c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 19762c9e7765SFlorian Westphal const struct sk_buff *skb; 19772c9e7765SFlorian Westphal 19782c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 19792c9e7765SFlorian Westphal if (skb) { 19802c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 19812c9e7765SFlorian Westphal 19822c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 19832c9e7765SFlorian Westphal return INT_MAX; 19842c9e7765SFlorian Westphal 19852c9e7765SFlorian Westphal return (unsigned int)hint_val; 19862c9e7765SFlorian Westphal } 19872c9e7765SFlorian Westphal 19882c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 19892c9e7765SFlorian Westphal return 1; 19902c9e7765SFlorian Westphal 19912c9e7765SFlorian Westphal return 0; 19922c9e7765SFlorian Westphal } 19932c9e7765SFlorian Westphal 1994f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 1995f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 1996f870fa0bSMat Martineau { 1997f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1998b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 1999b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20007a6a6cbcSPaolo Abeni int target; 20017a6a6cbcSPaolo Abeni long timeo; 2002f870fa0bSMat Martineau 2003cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2004cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2005cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2006cb9d80f4SPaolo Abeni 20076511882cSPaolo Abeni lock_sock(sk); 2008fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2009fd897679SPaolo Abeni copied = -ENOTCONN; 2010fd897679SPaolo Abeni goto out_err; 2011fd897679SPaolo Abeni } 2012fd897679SPaolo Abeni 20137a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 20147a6a6cbcSPaolo Abeni 20157a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20167a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20177a6a6cbcSPaolo Abeni 20182c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20192c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20202c9e7765SFlorian Westphal 202105e3eceaSEric Dumazet while (copied < len) { 2022e3859603SPaolo Abeni int bytes_read; 20237a6a6cbcSPaolo Abeni 2024b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20256771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20267a6a6cbcSPaolo Abeni if (!copied) 20277a6a6cbcSPaolo Abeni copied = bytes_read; 20286771bfd9SFlorian Westphal goto out_err; 2029cec37a6eSPeter Krystad } 2030cec37a6eSPeter Krystad 20317a6a6cbcSPaolo Abeni copied += bytes_read; 20327a6a6cbcSPaolo Abeni 2033ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2034fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2035ea4ca586SPaolo Abeni 2036e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2037e3859603SPaolo Abeni continue; 2038e3859603SPaolo Abeni 20397a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 20407a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 20417a6a6cbcSPaolo Abeni */ 20427a6a6cbcSPaolo Abeni if (copied >= target) 20437a6a6cbcSPaolo Abeni break; 20447a6a6cbcSPaolo Abeni 20457a6a6cbcSPaolo Abeni if (copied) { 20467a6a6cbcSPaolo Abeni if (sk->sk_err || 20477a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 20487a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 20497a6a6cbcSPaolo Abeni !timeo || 20507a6a6cbcSPaolo Abeni signal_pending(current)) 20517a6a6cbcSPaolo Abeni break; 20527a6a6cbcSPaolo Abeni } else { 20537a6a6cbcSPaolo Abeni if (sk->sk_err) { 20547a6a6cbcSPaolo Abeni copied = sock_error(sk); 20557a6a6cbcSPaolo Abeni break; 20567a6a6cbcSPaolo Abeni } 20577a6a6cbcSPaolo Abeni 20585969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 20595969856aSPaolo Abeni mptcp_check_for_eof(msk); 20605969856aSPaolo Abeni 206187952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 206287952603SPaolo Abeni /* race breaker: the shutdown could be after the 206387952603SPaolo Abeni * previous receive queue check 206487952603SPaolo Abeni */ 2065e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 206687952603SPaolo Abeni continue; 20677a6a6cbcSPaolo Abeni break; 206887952603SPaolo Abeni } 20697a6a6cbcSPaolo Abeni 20707a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 20717a6a6cbcSPaolo Abeni copied = -ENOTCONN; 20727a6a6cbcSPaolo Abeni break; 20737a6a6cbcSPaolo Abeni } 20747a6a6cbcSPaolo Abeni 20757a6a6cbcSPaolo Abeni if (!timeo) { 20767a6a6cbcSPaolo Abeni copied = -EAGAIN; 20777a6a6cbcSPaolo Abeni break; 20787a6a6cbcSPaolo Abeni } 20797a6a6cbcSPaolo Abeni 20807a6a6cbcSPaolo Abeni if (signal_pending(current)) { 20817a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 20827a6a6cbcSPaolo Abeni break; 20837a6a6cbcSPaolo Abeni } 20847a6a6cbcSPaolo Abeni } 20857a6a6cbcSPaolo Abeni 20867a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2087612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 20887a6a6cbcSPaolo Abeni } 20893c90e377SPaolo Abeni 20906771bfd9SFlorian Westphal out_err: 2091b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2092b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2093b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 20942c9e7765SFlorian Westphal 20952c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 20962c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 20972c9e7765SFlorian Westphal 20982c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 20992c9e7765SFlorian Westphal } 2100b7f653b2SFlorian Westphal } 2101b7f653b2SFlorian Westphal 2102612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2103612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2104612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2105ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2106a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2107a6b118feSFlorian Westphal 2108cec37a6eSPeter Krystad release_sock(sk); 2109cec37a6eSPeter Krystad return copied; 2110cec37a6eSPeter Krystad } 2111cec37a6eSPeter Krystad 2112b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2113b51f9b80SPaolo Abeni { 2114b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2115b51f9b80SPaolo Abeni icsk_retransmit_timer); 2116b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21172d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2118b51f9b80SPaolo Abeni 2119b51f9b80SPaolo Abeni bh_lock_sock(sk); 2120b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21212d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21222d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21232d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2124b51f9b80SPaolo Abeni } else { 2125b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 21262d6f5a2bSPaolo Abeni set_bit(MPTCP_RETRANSMIT, &msk->flags); 2127b51f9b80SPaolo Abeni } 2128b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2129b51f9b80SPaolo Abeni sock_put(sk); 2130b51f9b80SPaolo Abeni } 2131b51f9b80SPaolo Abeni 2132e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2133e16163b6SPaolo Abeni { 2134e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2135e16163b6SPaolo Abeni 2136e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2137b6d69fc8SFlorian Westphal sock_put(sk); 2138e16163b6SPaolo Abeni } 2139e16163b6SPaolo Abeni 21403b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 21413b1d6210SPaolo Abeni * level. 21423b1d6210SPaolo Abeni * 21433b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 21443b1d6210SPaolo Abeni */ 2145ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 21463b1d6210SPaolo Abeni { 214771b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 21483b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 214971b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 21503b1d6210SPaolo Abeni 21513b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 21523b1d6210SPaolo Abeni 2153d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2154d9ca1de8SPaolo Abeni return NULL; 2155d5f49190SPaolo Abeni 21563b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 21573b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21583b1d6210SPaolo Abeni 2159ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2160d5f49190SPaolo Abeni continue; 2161d5f49190SPaolo Abeni 216271b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 216371b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 216471b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 216571b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2166860975c6SFlorian Westphal continue; 2167860975c6SFlorian Westphal } 21683b1d6210SPaolo Abeni 21693b1d6210SPaolo Abeni if (subflow->backup) { 21703b1d6210SPaolo Abeni if (!backup) 21713b1d6210SPaolo Abeni backup = ssk; 21723b1d6210SPaolo Abeni continue; 21733b1d6210SPaolo Abeni } 21743b1d6210SPaolo Abeni 217571b7dec2SPaolo Abeni if (!pick) 217671b7dec2SPaolo Abeni pick = ssk; 21773b1d6210SPaolo Abeni } 21783b1d6210SPaolo Abeni 217971b7dec2SPaolo Abeni if (pick) 218071b7dec2SPaolo Abeni return pick; 218171b7dec2SPaolo Abeni 218271b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 218371b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 21843b1d6210SPaolo Abeni } 21853b1d6210SPaolo Abeni 218617aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 218717aee05dSFlorian Westphal { 218817aee05dSFlorian Westphal if (msk->subflow) { 218917aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 219017aee05dSFlorian Westphal msk->subflow = NULL; 219117aee05dSFlorian Westphal } 219217aee05dSFlorian Westphal } 219317aee05dSFlorian Westphal 21941e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 21951e1d9d6fSPaolo Abeni { 21961e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 21971e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 21981e1d9d6fSPaolo Abeni 21991e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22001e1d9d6fSPaolo Abeni return false; 22011e1d9d6fSPaolo Abeni 22021e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22031e1d9d6fSPaolo Abeni return false; 22041e1d9d6fSPaolo Abeni 22051e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22061e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22071e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22081e1d9d6fSPaolo Abeni */ 22091e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22101e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22111e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22121e1d9d6fSPaolo Abeni if (!rtx_head) { 22131e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22141e1d9d6fSPaolo Abeni return false; 22151e1d9d6fSPaolo Abeni } 22161e1d9d6fSPaolo Abeni 22171e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22181e1d9d6fSPaolo Abeni msk->recovery = true; 22191e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22201e1d9d6fSPaolo Abeni 22211e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22221e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22231e1d9d6fSPaolo Abeni 22241e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22251e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22261e1d9d6fSPaolo Abeni if (!cur->already_sent) 22271e1d9d6fSPaolo Abeni break; 22281e1d9d6fSPaolo Abeni cur->already_sent = 0; 22291e1d9d6fSPaolo Abeni } 22301e1d9d6fSPaolo Abeni 22311e1d9d6fSPaolo Abeni return true; 22321e1d9d6fSPaolo Abeni } 22331e1d9d6fSPaolo Abeni 2234cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2235cec37a6eSPeter Krystad * (accept). 2236cec37a6eSPeter Krystad * 2237cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2238cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2239cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2240cec37a6eSPeter Krystad * parent socket. 2241cec37a6eSPeter Krystad */ 2242a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2243e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow) 2244cec37a6eSPeter Krystad { 2245e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 22461e1d9d6fSPaolo Abeni bool need_push; 2247e0be4931SFlorian Westphal 2248cec37a6eSPeter Krystad list_del(&subflow->node); 2249cec37a6eSPeter Krystad 22503f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2251e16163b6SPaolo Abeni 2252e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2253e16163b6SPaolo Abeni * already orphaned 2254e16163b6SPaolo Abeni */ 2255866f26f2SPaolo Abeni if (ssk->sk_socket) 2256e16163b6SPaolo Abeni sock_orphan(ssk); 2257e16163b6SPaolo Abeni 22581e1d9d6fSPaolo Abeni need_push = __mptcp_retransmit_pending_data(sk); 2259d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2260d7b1bfd0SPaolo Abeni 2261e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2262e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2263e16163b6SPaolo Abeni * reference owned by msk; 2264e16163b6SPaolo Abeni */ 2265e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2266e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2267e16163b6SPaolo Abeni } else { 2268d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2269e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2270e16163b6SPaolo Abeni 2271e16163b6SPaolo Abeni /* close acquired an extra ref */ 2272e16163b6SPaolo Abeni __sock_put(ssk); 2273e16163b6SPaolo Abeni } 2274e16163b6SPaolo Abeni release_sock(ssk); 2275e16163b6SPaolo Abeni 2276e16163b6SPaolo Abeni sock_put(ssk); 2277e0be4931SFlorian Westphal 2278e0be4931SFlorian Westphal if (ssk == msk->last_snd) 2279e0be4931SFlorian Westphal msk->last_snd = NULL; 228017aee05dSFlorian Westphal 2281c8fe62f0SFlorian Westphal if (ssk == msk->first) 2282c8fe62f0SFlorian Westphal msk->first = NULL; 2283c8fe62f0SFlorian Westphal 228417aee05dSFlorian Westphal if (msk->subflow && ssk == msk->subflow->sk) 228517aee05dSFlorian Westphal mptcp_dispose_initial_subflow(msk); 22861e1d9d6fSPaolo Abeni 22871e1d9d6fSPaolo Abeni if (need_push) 22881e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2289f870fa0bSMat Martineau } 2290f870fa0bSMat Martineau 2291a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2292a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2293a141e02eSFlorian Westphal { 2294b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2295b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2296a141e02eSFlorian Westphal __mptcp_close_ssk(sk, ssk, subflow); 2297a141e02eSFlorian Westphal } 2298a141e02eSFlorian Westphal 2299dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2300dc24f8b4SPaolo Abeni { 2301dc24f8b4SPaolo Abeni return 0; 2302dc24f8b4SPaolo Abeni } 2303dc24f8b4SPaolo Abeni 23040e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 23050e4f35d7SPaolo Abeni { 23060e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 23070e4f35d7SPaolo Abeni 23083abc05d9SFlorian Westphal might_sleep(); 23093abc05d9SFlorian Westphal 23100e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 23110e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 23120e4f35d7SPaolo Abeni 23130e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 23140e4f35d7SPaolo Abeni continue; 23150e4f35d7SPaolo Abeni 231640947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 231740947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 231840947e13SFlorian Westphal continue; 231940947e13SFlorian Westphal 2320a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 23210e4f35d7SPaolo Abeni } 23220e4f35d7SPaolo Abeni } 23230e4f35d7SPaolo Abeni 2324e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2325e16163b6SPaolo Abeni { 2326e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2327e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2328e16163b6SPaolo Abeni 2329e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2330e16163b6SPaolo Abeni return true; 2331e16163b6SPaolo Abeni 2332e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2333e16163b6SPaolo Abeni * timeout 2334e16163b6SPaolo Abeni */ 2335e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2336e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2337e16163b6SPaolo Abeni TCP_CLOSE) 2338e16163b6SPaolo Abeni return false; 2339e16163b6SPaolo Abeni } 2340e16163b6SPaolo Abeni return true; 2341e16163b6SPaolo Abeni } 2342e16163b6SPaolo Abeni 234350c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 234450c504a2SFlorian Westphal { 234550c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 234650c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 234750c504a2SFlorian Westphal 234850c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 234950c504a2SFlorian Westphal return; 235050c504a2SFlorian Westphal 235150c504a2SFlorian Westphal mptcp_token_destroy(msk); 235250c504a2SFlorian Westphal 235350c504a2SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 235450c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 235575e908c3SPaolo Abeni bool slow; 235650c504a2SFlorian Westphal 235775e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 235850c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 235950c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 236050c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 236150c504a2SFlorian Westphal } 236275e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 236350c504a2SFlorian Westphal } 236450c504a2SFlorian Westphal 236550c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 236650c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 236750c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 236850c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 236950c504a2SFlorian Westphal 237050c504a2SFlorian Westphal mptcp_close_wake_up(sk); 237150c504a2SFlorian Westphal } 237250c504a2SFlorian Westphal 23732948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 237480992017SPaolo Abeni { 23752948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2376caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 23773b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 23783b1d6210SPaolo Abeni size_t copied = 0; 23792948d0a1SPaolo Abeni struct sock *ssk; 23802948d0a1SPaolo Abeni int ret; 23812948d0a1SPaolo Abeni 2382b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 23833241a9c0SFlorian Westphal 23843241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 23853241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 23862948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 23876477dd39SMat Martineau if (!dfrag) { 23886477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 23896477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 23906477dd39SMat Martineau 23916477dd39SMat Martineau icsk->icsk_retransmits++; 23926477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 23936477dd39SMat Martineau mptcp_send_ack(msk); 23946477dd39SMat Martineau 23956477dd39SMat Martineau goto reset_timer; 23966477dd39SMat Martineau } 23976477dd39SMat Martineau 23983241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 23992948d0a1SPaolo Abeni return; 24003241a9c0SFlorian Westphal 24013241a9c0SFlorian Westphal goto reset_timer; 24026477dd39SMat Martineau } 24032948d0a1SPaolo Abeni 24042948d0a1SPaolo Abeni if (!ssk) 24052948d0a1SPaolo Abeni goto reset_timer; 24062948d0a1SPaolo Abeni 24072948d0a1SPaolo Abeni lock_sock(ssk); 24082948d0a1SPaolo Abeni 24092948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 24102948d0a1SPaolo Abeni info.sent = 0; 24114e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 24124e14867dSPaolo Abeni while (info.sent < info.limit) { 24132948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 24142948d0a1SPaolo Abeni if (ret <= 0) 24152948d0a1SPaolo Abeni break; 24162948d0a1SPaolo Abeni 24172948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 24182948d0a1SPaolo Abeni copied += ret; 24192948d0a1SPaolo Abeni info.sent += ret; 24202948d0a1SPaolo Abeni } 24214e14867dSPaolo Abeni if (copied) { 24224e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 24232948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 24242948d0a1SPaolo Abeni info.size_goal); 24254e14867dSPaolo Abeni } 24262948d0a1SPaolo Abeni 24272948d0a1SPaolo Abeni release_sock(ssk); 24282948d0a1SPaolo Abeni 24292948d0a1SPaolo Abeni reset_timer: 24303241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 24313241a9c0SFlorian Westphal 24322948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 24332948d0a1SPaolo Abeni mptcp_reset_timer(sk); 24342948d0a1SPaolo Abeni } 24352948d0a1SPaolo Abeni 24362948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 24372948d0a1SPaolo Abeni { 24382948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 24392948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 24402948d0a1SPaolo Abeni int state; 244180992017SPaolo Abeni 244280992017SPaolo Abeni lock_sock(sk); 2443e16163b6SPaolo Abeni state = sk->sk_state; 2444e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2445e16163b6SPaolo Abeni goto unlock; 2446e16163b6SPaolo Abeni 244743b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 244878962489SFlorian Westphal mptcp_flush_join_list(msk); 244950c504a2SFlorian Westphal 245050c504a2SFlorian Westphal mptcp_check_fastclose(msk); 245150c504a2SFlorian Westphal 2452b416268bSFlorian Westphal if (msk->pm.status) 2453e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2454b416268bSFlorian Westphal 245559832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 245659832e24SFlorian Westphal mptcp_check_for_eof(msk); 245759832e24SFlorian Westphal 24586e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 245943b54c6eSMat Martineau mptcp_check_data_fin(sk); 246043b54c6eSMat Martineau 2461341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2462341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2463341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2464e16163b6SPaolo Abeni */ 2465e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2466341c6524SPaolo Abeni (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2467e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2468e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2469e16163b6SPaolo Abeni goto unlock; 2470e16163b6SPaolo Abeni } 2471e16163b6SPaolo Abeni 2472b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2473b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2474b263b0d7SFlorian Westphal 24752948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 24762948d0a1SPaolo Abeni __mptcp_retrans(sk); 24773b1d6210SPaolo Abeni 24783b1d6210SPaolo Abeni unlock: 247980992017SPaolo Abeni release_sock(sk); 248080992017SPaolo Abeni sock_put(sk); 248180992017SPaolo Abeni } 248280992017SPaolo Abeni 2483784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2484f870fa0bSMat Martineau { 2485cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2486cec37a6eSPeter Krystad 2487ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 2488ec3edaa7SPeter Krystad 2489cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2490ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 249118b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 249280992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 249387952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2494ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2495f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 24966511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2497ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 249833d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2499cec37a6eSPeter Krystad 25008ab183deSPaolo Abeni msk->first = NULL; 2501dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2502752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 25031e1d9d6fSPaolo Abeni msk->recovery = false; 25048ab183deSPaolo Abeni 25051b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 25061b1c7a0eSPeter Krystad 2507b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2508b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2509e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2510aa1fbd94SFlorian Westphal 2511f870fa0bSMat Martineau return 0; 2512f870fa0bSMat Martineau } 2513f870fa0bSMat Martineau 2514784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 2515784325e9SMatthieu Baerts { 251620b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2517fc518953SFlorian Westphal struct net *net = sock_net(sk); 2518fc518953SFlorian Westphal int ret; 251918b683bfSPaolo Abeni 2520b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2521b6c08380SGeliang Tang if (ret) 2522b6c08380SGeliang Tang return ret; 2523b6c08380SGeliang Tang 2524fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2525fc518953SFlorian Westphal return -ENOPROTOOPT; 2526fc518953SFlorian Westphal 2527fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2528fc518953SFlorian Westphal return -ENOMEM; 2529fc518953SFlorian Westphal 2530fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2531fa68018dSPaolo Abeni if (ret) 2532fa68018dSPaolo Abeni return ret; 2533fa68018dSPaolo Abeni 253420b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 253520b5759fSPaolo Abeni * propagate the correct value 253620b5759fSPaolo Abeni */ 253720b5759fSPaolo Abeni tcp_assign_congestion_control(sk); 253820b5759fSPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 253920b5759fSPaolo Abeni 254020b5759fSPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 254120b5759fSPaolo Abeni tcp_cleanup_congestion_control(sk); 254220b5759fSPaolo Abeni icsk->icsk_ca_ops = NULL; 254320b5759fSPaolo Abeni 2544d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2545a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2546da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2547d027236cSPaolo Abeni 254818b683bfSPaolo Abeni return 0; 254918b683bfSPaolo Abeni } 255018b683bfSPaolo Abeni 255118b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 255218b683bfSPaolo Abeni { 255318b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 255418b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 255518b683bfSPaolo Abeni 2556d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 255718b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2558d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2559784325e9SMatthieu Baerts } 2560784325e9SMatthieu Baerts 256180992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 256280992017SPaolo Abeni { 256380992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 256480992017SPaolo Abeni 2565b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2566e16163b6SPaolo Abeni __sock_put(sk); 256780992017SPaolo Abeni } 256880992017SPaolo Abeni 2569d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 257021498490SPeter Krystad { 257121498490SPeter Krystad lock_sock(ssk); 257221498490SPeter Krystad 257321498490SPeter Krystad switch (ssk->sk_state) { 257421498490SPeter Krystad case TCP_LISTEN: 257521498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 257621498490SPeter Krystad break; 2577df561f66SGustavo A. R. Silva fallthrough; 257821498490SPeter Krystad case TCP_SYN_SENT: 257921498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 258021498490SPeter Krystad break; 258121498490SPeter Krystad default: 258243b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 258343b54c6eSMat Martineau pr_debug("Fallback"); 258421498490SPeter Krystad ssk->sk_shutdown |= how; 258521498490SPeter Krystad tcp_shutdown(ssk, how); 258643b54c6eSMat Martineau } else { 258743b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 258843b54c6eSMat Martineau tcp_send_ack(ssk); 25896477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 25906477dd39SMat Martineau mptcp_reset_timer(sk); 259143b54c6eSMat Martineau } 259221498490SPeter Krystad break; 259321498490SPeter Krystad } 259421498490SPeter Krystad 259521498490SPeter Krystad release_sock(ssk); 259621498490SPeter Krystad } 259721498490SPeter Krystad 25986920b851SMat Martineau static const unsigned char new_state[16] = { 25996920b851SMat Martineau /* current state: new state: action: */ 26006920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 26016920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26026920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 26036920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26046920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 26056920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 26066920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 26076920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 26086920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 26096920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 26106920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 26116920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 26126920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 26136920b851SMat Martineau }; 26146920b851SMat Martineau 26156920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 26166920b851SMat Martineau { 26176920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 26186920b851SMat Martineau int ns = next & TCP_STATE_MASK; 26196920b851SMat Martineau 26206920b851SMat Martineau inet_sk_state_store(sk, ns); 26216920b851SMat Martineau 26226920b851SMat Martineau return next & TCP_ACTION_FIN; 26236920b851SMat Martineau } 26246920b851SMat Martineau 2625e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2626e16163b6SPaolo Abeni { 2627e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2628e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2629e16163b6SPaolo Abeni 2630e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2631e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2632e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2633e16163b6SPaolo Abeni 2634e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2635e16163b6SPaolo Abeni * skip this 2636e16163b6SPaolo Abeni */ 2637e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2638e16163b6SPaolo Abeni mptcp_send_head(sk)) 2639e16163b6SPaolo Abeni return; 2640e16163b6SPaolo Abeni 2641e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2642e16163b6SPaolo Abeni 264326aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 264426aa2314SPaolo Abeni * state now 264526aa2314SPaolo Abeni */ 264626aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 264726aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2648e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2649e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 265026aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 265126aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 265226aa2314SPaolo Abeni } 2653e16163b6SPaolo Abeni } 2654e16163b6SPaolo Abeni 265578962489SFlorian Westphal mptcp_flush_join_list(msk); 2656e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2657e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2658e16163b6SPaolo Abeni 2659e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2660e16163b6SPaolo Abeni } 2661e16163b6SPaolo Abeni } 2662e16163b6SPaolo Abeni 2663e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2664e16163b6SPaolo Abeni { 2665e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2666e16163b6SPaolo Abeni 2667e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2668e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2669e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2670e16163b6SPaolo Abeni 2671e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2672e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2673e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2674e16163b6SPaolo Abeni 2675e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2676e16163b6SPaolo Abeni } 2677e16163b6SPaolo Abeni 2678e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2679f870fa0bSMat Martineau { 2680cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2681f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2682b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2683f870fa0bSMat Martineau 2684e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 26852c22c06cSFlorian Westphal 26863abc05d9SFlorian Westphal might_sleep(); 26873abc05d9SFlorian Westphal 268810f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 268910f6d46cSPaolo Abeni * mptcp_finish_join(). 269010f6d46cSPaolo Abeni */ 269110f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 269210f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 269310f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2694b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2695b2c5b614SFlorian Westphal 26966e628cd3SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2697e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2698e16163b6SPaolo Abeni msk->pm.status = 0; 2699b2c5b614SFlorian Westphal 2700b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2701cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2702e16163b6SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow); 2703f870fa0bSMat Martineau } 2704f870fa0bSMat Martineau 2705e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2706e16163b6SPaolo Abeni 27076511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 270887952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2709e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2710e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2711aa1fbd94SFlorian Westphal 2712e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 271317aee05dSFlorian Westphal mptcp_dispose_initial_subflow(msk); 2714e16163b6SPaolo Abeni sock_put(sk); 2715e16163b6SPaolo Abeni } 2716e16163b6SPaolo Abeni 2717e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2718e16163b6SPaolo Abeni { 2719e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2720e16163b6SPaolo Abeni bool do_cancel_work = false; 2721e16163b6SPaolo Abeni 2722e16163b6SPaolo Abeni lock_sock(sk); 2723e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2724e16163b6SPaolo Abeni 2725e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2726e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2727e16163b6SPaolo Abeni goto cleanup; 2728e16163b6SPaolo Abeni } 2729e16163b6SPaolo Abeni 2730e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2731e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2732e16163b6SPaolo Abeni 2733e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2734e16163b6SPaolo Abeni 2735e16163b6SPaolo Abeni cleanup: 2736e16163b6SPaolo Abeni /* orphan all the subflows */ 2737e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 273844227915SGeliang Tang mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2739e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 274049054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2741e16163b6SPaolo Abeni 2742e16163b6SPaolo Abeni sock_orphan(ssk); 2743e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2744e16163b6SPaolo Abeni } 2745e16163b6SPaolo Abeni sock_orphan(sk); 2746e16163b6SPaolo Abeni 2747e16163b6SPaolo Abeni sock_hold(sk); 2748e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2749e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2750e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2751e16163b6SPaolo Abeni do_cancel_work = true; 2752e16163b6SPaolo Abeni } else { 2753e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2754e16163b6SPaolo Abeni } 2755e16163b6SPaolo Abeni release_sock(sk); 2756e16163b6SPaolo Abeni if (do_cancel_work) 275780992017SPaolo Abeni mptcp_cancel_work(sk); 2758b911c97cSFlorian Westphal 2759b911c97cSFlorian Westphal if (mptcp_sk(sk)->token) 2760b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2761b911c97cSFlorian Westphal 2762e16163b6SPaolo Abeni sock_put(sk); 2763f870fa0bSMat Martineau } 2764f870fa0bSMat Martineau 2765cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2766cf7da0d6SPeter Krystad { 2767cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2768cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2769cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2770cf7da0d6SPeter Krystad 2771cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2772cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2773cf7da0d6SPeter Krystad 2774cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2775cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2776cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2777cf7da0d6SPeter Krystad } 2778cf7da0d6SPeter Krystad #endif 2779cf7da0d6SPeter Krystad 2780cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2781cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2782cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2783cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2784cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2785cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2786cf7da0d6SPeter Krystad } 2787cf7da0d6SPeter Krystad 278818b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 278918b683bfSPaolo Abeni { 279076e2a55dSPaolo Abeni struct mptcp_subflow_context *subflow; 279176e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 279276e2a55dSPaolo Abeni 279378962489SFlorian Westphal mptcp_do_flush_join_list(msk); 279478962489SFlorian Westphal 279513a9499eSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 279613a9499eSPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 279713a9499eSPaolo Abeni 279813a9499eSPaolo Abeni lock_sock(ssk); 279913a9499eSPaolo Abeni tcp_disconnect(ssk, flags); 280013a9499eSPaolo Abeni release_sock(ssk); 280113a9499eSPaolo Abeni } 280242c556feSFlorian Westphal return 0; 280318b683bfSPaolo Abeni } 280418b683bfSPaolo Abeni 2805b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2806b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2807b0519de8SFlorian Westphal { 2808b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2809b0519de8SFlorian Westphal 2810b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2811b0519de8SFlorian Westphal } 2812b0519de8SFlorian Westphal #endif 2813b0519de8SFlorian Westphal 2814fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2815cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2816fca5c82cSPaolo Abeni struct request_sock *req) 2817b0519de8SFlorian Westphal { 281858b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2819b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 282058b09919SPaolo Abeni struct mptcp_sock *msk; 282158b09919SPaolo Abeni u64 ack_seq; 2822b0519de8SFlorian Westphal 2823b0519de8SFlorian Westphal if (!nsk) 2824b0519de8SFlorian Westphal return NULL; 2825b0519de8SFlorian Westphal 2826b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2827b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2828b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2829b0519de8SFlorian Westphal #endif 2830b0519de8SFlorian Westphal 283158b09919SPaolo Abeni __mptcp_init_sock(nsk); 283258b09919SPaolo Abeni 283358b09919SPaolo Abeni msk = mptcp_sk(nsk); 283458b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 283558b09919SPaolo Abeni msk->token = subflow_req->token; 283658b09919SPaolo Abeni msk->subflow = NULL; 2837b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 283874c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 283906251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 284058b09919SPaolo Abeni 284158b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2842eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 28437439d687SPaolo Abeni msk->snd_una = msk->write_seq; 28447439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2845df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 28466f8a612aSFlorian Westphal 284774c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 284858b09919SPaolo Abeni msk->can_ack = true; 2849cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 285058b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 285158b09919SPaolo Abeni ack_seq++; 2852917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2853fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 285458b09919SPaolo Abeni } 28557f20d5fcSPaolo Abeni 28565e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 28577f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 28587f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 28590c148460SPaolo Abeni 28600c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 286158b09919SPaolo Abeni bh_unlock_sock(nsk); 286258b09919SPaolo Abeni 286358b09919SPaolo Abeni /* keep a single reference */ 286458b09919SPaolo Abeni __sock_put(nsk); 2865b0519de8SFlorian Westphal return nsk; 2866b0519de8SFlorian Westphal } 2867b0519de8SFlorian Westphal 2868a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2869a6b118feSFlorian Westphal { 2870a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2871a6b118feSFlorian Westphal 2872a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2873a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2874a6b118feSFlorian Westphal 2875a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2876a6b118feSFlorian Westphal 2877a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2878a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2879a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2880a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2881a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 28826f8a612aSFlorian Westphal 28837439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2884a6b118feSFlorian Westphal } 2885a6b118feSFlorian Westphal 2886cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2887cf7da0d6SPeter Krystad bool kern) 2888cf7da0d6SPeter Krystad { 2889cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2890cf7da0d6SPeter Krystad struct socket *listener; 2891cf7da0d6SPeter Krystad struct sock *newsk; 2892cf7da0d6SPeter Krystad 2893cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2894cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2895cf7da0d6SPeter Krystad *err = -EINVAL; 2896cf7da0d6SPeter Krystad return NULL; 2897cf7da0d6SPeter Krystad } 2898cf7da0d6SPeter Krystad 2899cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2900cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2901cf7da0d6SPeter Krystad if (!newsk) 2902cf7da0d6SPeter Krystad return NULL; 2903cf7da0d6SPeter Krystad 2904cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2905cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2906cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2907cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2908cf7da0d6SPeter Krystad 2909cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 291058b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 291158b09919SPaolo Abeni 291258b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 291358b09919SPaolo Abeni * subflow_syn_recv_sock() 291458b09919SPaolo Abeni */ 291558b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 291658b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 291758b09919SPaolo Abeni return newsk; 291858b09919SPaolo Abeni } 291958b09919SPaolo Abeni 292058b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 292158b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2922cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 29230397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2924fc518953SFlorian Westphal } else { 2925fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2926fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2927cf7da0d6SPeter Krystad } 2928cf7da0d6SPeter Krystad 2929cf7da0d6SPeter Krystad return newsk; 2930cf7da0d6SPeter Krystad } 2931cf7da0d6SPeter Krystad 29325c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 29335c8c1640SGeliang Tang { 293487952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 293587952603SPaolo Abeni 29366e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 29376e628cd3SPaolo Abeni 293887952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 293987952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 29406511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 29415c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 29426511882cSPaolo Abeni 29436511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 29446511882cSPaolo Abeni * inet_sock_destruct() will dispose it 29456511882cSPaolo Abeni */ 29466511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 29476511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 29485c8c1640SGeliang Tang mptcp_token_destroy(msk); 29495c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 29505c8c1640SGeliang Tang } 29515c8c1640SGeliang Tang 295279c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 295379c0949eSPeter Krystad { 2954c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2955c9fd9c5fSFlorian Westphal 29565c8c1640SGeliang Tang mptcp_destroy_common(msk); 2957d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 295879c0949eSPeter Krystad } 295979c0949eSPeter Krystad 29606e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 29616e628cd3SPaolo Abeni { 29626e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 29636e628cd3SPaolo Abeni __mptcp_clean_una(sk); 29646e628cd3SPaolo Abeni else 29656e628cd3SPaolo Abeni set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 29666e628cd3SPaolo Abeni 29676e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 29686e628cd3SPaolo Abeni mptcp_schedule_work(sk); 29696e628cd3SPaolo Abeni } 29706e628cd3SPaolo Abeni 2971219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 29726e628cd3SPaolo Abeni { 29736e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 29746e628cd3SPaolo Abeni return; 29756e628cd3SPaolo Abeni 297640dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 2977b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 2978b19bc294SPaolo Abeni 2979b19bc294SPaolo Abeni if (xmit_ssk == ssk) 29806e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 2981b19bc294SPaolo Abeni else if (xmit_ssk) 2982bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND); 298340dc9416SPaolo Abeni } else { 29846e628cd3SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 29856e628cd3SPaolo Abeni } 298640dc9416SPaolo Abeni } 29876e628cd3SPaolo Abeni 2988e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 298914c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 299014c441b5SPaolo Abeni { 2991c2e6048fSPaolo Abeni for (;;) { 29922d6f5a2bSPaolo Abeni unsigned long flags = 0; 29932d6f5a2bSPaolo Abeni 2994c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 29952e5de7e0SDan Carpenter flags |= BIT(MPTCP_PUSH_PENDING); 29962d6f5a2bSPaolo Abeni if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) 29972d6f5a2bSPaolo Abeni flags |= BIT(MPTCP_RETRANSMIT); 2998c2e6048fSPaolo Abeni if (!flags) 2999c2e6048fSPaolo Abeni break; 3000c2e6048fSPaolo Abeni 3001c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 30026e628cd3SPaolo Abeni * 30036e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 30046e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 30056e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 30066e628cd3SPaolo Abeni * the subflow socket lock 30076e628cd3SPaolo Abeni */ 30086e628cd3SPaolo Abeni 30096e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 30102e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3011c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 30122d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 30132d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3014c2e6048fSPaolo Abeni 3015c2e6048fSPaolo Abeni cond_resched(); 30166e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 30176e628cd3SPaolo Abeni } 3018c2e6048fSPaolo Abeni 3019490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 3020490274b4SPaolo Abeni * depending on sk_state 3021490274b4SPaolo Abeni */ 3022490274b4SPaolo Abeni if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags)) 3023490274b4SPaolo Abeni __mptcp_set_connected(sk); 3024c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 3025417789dfSPaolo Abeni __mptcp_clean_una_wakeup(sk); 302615cc1045SPaolo Abeni if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) 302715cc1045SPaolo Abeni __mptcp_error_report(sk); 30286e628cd3SPaolo Abeni 302987952603SPaolo Abeni __mptcp_update_rmem(sk); 303014c441b5SPaolo Abeni } 303114c441b5SPaolo Abeni 3032bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3033bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3034bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3035bcd97734SPaolo Abeni */ 3036bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3037bcd97734SPaolo Abeni { 3038bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3039bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3040bcd97734SPaolo Abeni unsigned long timeout; 3041bcd97734SPaolo Abeni 3042bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3043bcd97734SPaolo Abeni return; 3044bcd97734SPaolo Abeni 3045bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3046bcd97734SPaolo Abeni if (tp->srtt_us) 3047bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3048bcd97734SPaolo Abeni else 3049bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3050bcd97734SPaolo Abeni timeout += jiffies; 3051bcd97734SPaolo Abeni 3052bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3053bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3054bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3055bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3056bcd97734SPaolo Abeni } 3057bcd97734SPaolo Abeni 3058b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3059b19bc294SPaolo Abeni { 3060b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3061b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3062b19bc294SPaolo Abeni 3063bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3064b19bc294SPaolo Abeni mptcp_data_lock(sk); 3065b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3066b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3067b19bc294SPaolo Abeni else 3068b19bc294SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 3069b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3070bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3071bcd97734SPaolo Abeni } 3072bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3073bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3074bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3075bcd97734SPaolo Abeni } 3076b19bc294SPaolo Abeni } 3077b19bc294SPaolo Abeni 30782c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 30792c5ebd00SPaolo Abeni { 30802c5ebd00SPaolo Abeni /* should never be called, 30812c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 30822c5ebd00SPaolo Abeni */ 30832c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 30842c5ebd00SPaolo Abeni return 0; 30852c5ebd00SPaolo Abeni } 30862c5ebd00SPaolo Abeni 30872c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 30882c5ebd00SPaolo Abeni { 30892c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 30902c5ebd00SPaolo Abeni } 30912c5ebd00SPaolo Abeni 3092cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3093f870fa0bSMat Martineau { 3094f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3095cec37a6eSPeter Krystad struct socket *ssock; 3096f870fa0bSMat Martineau 3097cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3098cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3099cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3100cec37a6eSPeter Krystad return -EINVAL; 3101f870fa0bSMat Martineau 3102cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3103cec37a6eSPeter Krystad } 3104f870fa0bSMat Martineau 3105cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3106cec37a6eSPeter Krystad { 3107cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3108cec37a6eSPeter Krystad struct mptcp_sock *msk; 3109cec37a6eSPeter Krystad struct sock *sk; 31106d0060f6SMat Martineau u64 ack_seq; 3111f870fa0bSMat Martineau 3112cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3113cec37a6eSPeter Krystad sk = subflow->conn; 3114cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3115cec37a6eSPeter Krystad 3116648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3117648ef4b8SMat Martineau 31186d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 31196d0060f6SMat Martineau ack_seq++; 3120648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3121648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 31226d0060f6SMat Martineau 3123cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3124cec37a6eSPeter Krystad * accessing the field below 3125cec37a6eSPeter Krystad */ 3126cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3127cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 31286d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3129eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 31306d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3131fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3132d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 31337439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 31341b1c7a0eSPeter Krystad 31356c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3136a6b118feSFlorian Westphal 3137a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3138f870fa0bSMat Martineau } 3139f870fa0bSMat Martineau 3140866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3141cf7da0d6SPeter Krystad { 3142cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3143cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3144cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3145cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3146cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3147cf7da0d6SPeter Krystad } 3148cf7da0d6SPeter Krystad 3149e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3150f296234cSPeter Krystad { 3151e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3152f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3153f296234cSPeter Krystad struct sock *parent = (void *)msk; 3154f296234cSPeter Krystad struct socket *parent_sock; 3155ec3edaa7SPeter Krystad bool ret; 3156f296234cSPeter Krystad 3157f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3158f296234cSPeter Krystad 3159f296234cSPeter Krystad /* mptcp socket already closing? */ 3160dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3161dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3162f296234cSPeter Krystad return false; 3163dc87efdbSFlorian Westphal } 3164f296234cSPeter Krystad 3165f296234cSPeter Krystad if (!msk->pm.server_side) 3166b911c97cSFlorian Westphal goto out; 3167f296234cSPeter Krystad 3168dc87efdbSFlorian Westphal if (!mptcp_pm_allow_new_subflow(msk)) { 3169dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 317010f6d46cSPaolo Abeni return false; 3171dc87efdbSFlorian Westphal } 317210f6d46cSPaolo Abeni 317310f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 317410f6d46cSPaolo Abeni * msk lock here. 317510f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 3176e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 317710f6d46cSPaolo Abeni */ 317810f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 317910f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3180e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 318110f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3182e16163b6SPaolo Abeni sock_hold(ssk); 3183e16163b6SPaolo Abeni } 318410f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 3185dc87efdbSFlorian Westphal if (!ret) { 3186dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 318710f6d46cSPaolo Abeni return false; 3188dc87efdbSFlorian Westphal } 318910f6d46cSPaolo Abeni 319010f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 319110f6d46cSPaolo Abeni * at close time 319210f6d46cSPaolo Abeni */ 3193f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 3194e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 3195e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 3196917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 3197b911c97cSFlorian Westphal out: 3198b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 319910f6d46cSPaolo Abeni return true; 3200f296234cSPeter Krystad } 3201f296234cSPeter Krystad 320276e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 320376e2a55dSPaolo Abeni { 320476e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 320576e2a55dSPaolo Abeni 320676e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 320776e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 320876e2a55dSPaolo Abeni } 320976e2a55dSPaolo Abeni 32106511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 32116511882cSPaolo Abeni { 32126511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 32136511882cSPaolo Abeni } 32146511882cSPaolo Abeni 3215*644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3216*644807e3SFlorian Westphal { 3217*644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3218*644807e3SFlorian Westphal u64 delta; 3219*644807e3SFlorian Westphal 3220*644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3221*644807e3SFlorian Westphal return -EINVAL; 3222*644807e3SFlorian Westphal 3223*644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3224*644807e3SFlorian Westphal return 0; 3225*644807e3SFlorian Westphal 3226*644807e3SFlorian Westphal delta = msk->write_seq - v; 3227*644807e3SFlorian Westphal if (delta > INT_MAX) 3228*644807e3SFlorian Westphal delta = INT_MAX; 3229*644807e3SFlorian Westphal 3230*644807e3SFlorian Westphal return (int)delta; 3231*644807e3SFlorian Westphal } 3232*644807e3SFlorian Westphal 3233*644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3234*644807e3SFlorian Westphal { 3235*644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3236*644807e3SFlorian Westphal bool slow; 3237*644807e3SFlorian Westphal int answ; 3238*644807e3SFlorian Westphal 3239*644807e3SFlorian Westphal switch (cmd) { 3240*644807e3SFlorian Westphal case SIOCINQ: 3241*644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3242*644807e3SFlorian Westphal return -EINVAL; 3243*644807e3SFlorian Westphal 3244*644807e3SFlorian Westphal lock_sock(sk); 3245*644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3246*644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3247*644807e3SFlorian Westphal release_sock(sk); 3248*644807e3SFlorian Westphal break; 3249*644807e3SFlorian Westphal case SIOCOUTQ: 3250*644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3251*644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3252*644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3253*644807e3SFlorian Westphal break; 3254*644807e3SFlorian Westphal case SIOCOUTQNSD: 3255*644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3256*644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3257*644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3258*644807e3SFlorian Westphal break; 3259*644807e3SFlorian Westphal default: 3260*644807e3SFlorian Westphal return -ENOIOCTLCMD; 3261*644807e3SFlorian Westphal } 3262*644807e3SFlorian Westphal 3263*644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3264*644807e3SFlorian Westphal } 3265*644807e3SFlorian Westphal 3266f870fa0bSMat Martineau static struct proto mptcp_prot = { 3267f870fa0bSMat Martineau .name = "MPTCP", 3268f870fa0bSMat Martineau .owner = THIS_MODULE, 3269f870fa0bSMat Martineau .init = mptcp_init_sock, 327018b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3271f870fa0bSMat Martineau .close = mptcp_close, 3272cf7da0d6SPeter Krystad .accept = mptcp_accept, 3273717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3274717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 327576e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 327679c0949eSPeter Krystad .destroy = mptcp_destroy, 3277f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3278*644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3279f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 328014c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 32812c5ebd00SPaolo Abeni .hash = mptcp_hash, 32822c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3283cec37a6eSPeter Krystad .get_port = mptcp_get_port, 32846511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3285d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 3286d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 3287d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3288d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3289989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3290d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3291f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 32922c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3293f870fa0bSMat Martineau .no_autobind = true, 3294f870fa0bSMat Martineau }; 3295f870fa0bSMat Martineau 32962303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 32972303f994SPeter Krystad { 32982303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 32992303f994SPeter Krystad struct socket *ssock; 3300cf7da0d6SPeter Krystad int err; 33012303f994SPeter Krystad 33022303f994SPeter Krystad lock_sock(sock->sk); 3303fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3304fa68018dSPaolo Abeni if (!ssock) { 3305fa68018dSPaolo Abeni err = -EINVAL; 33062303f994SPeter Krystad goto unlock; 33072303f994SPeter Krystad } 33082303f994SPeter Krystad 33092303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3310cf7da0d6SPeter Krystad if (!err) 3311cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 33122303f994SPeter Krystad 33132303f994SPeter Krystad unlock: 33142303f994SPeter Krystad release_sock(sock->sk); 33152303f994SPeter Krystad return err; 33162303f994SPeter Krystad } 33172303f994SPeter Krystad 33180235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 33190235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 33200235d075SPaolo Abeni { 33210235d075SPaolo Abeni subflow->request_mptcp = 0; 33220235d075SPaolo Abeni __mptcp_do_fallback(msk); 33230235d075SPaolo Abeni } 33240235d075SPaolo Abeni 33252303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 33262303f994SPeter Krystad int addr_len, int flags) 33272303f994SPeter Krystad { 33282303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 33292c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 33302303f994SPeter Krystad struct socket *ssock; 33312303f994SPeter Krystad int err; 33322303f994SPeter Krystad 33332303f994SPeter Krystad lock_sock(sock->sk); 333441be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 333541be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 333641be81a8SPaolo Abeni * cope with that 333741be81a8SPaolo Abeni */ 333841be81a8SPaolo Abeni ssock = msk->subflow; 333941be81a8SPaolo Abeni goto do_connect; 334041be81a8SPaolo Abeni } 334141be81a8SPaolo Abeni 3342fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3343fa68018dSPaolo Abeni if (!ssock) { 3344fa68018dSPaolo Abeni err = -EINVAL; 33452303f994SPeter Krystad goto unlock; 33462303f994SPeter Krystad } 33472303f994SPeter Krystad 3348fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3349fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 33502c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3351cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3352cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3353cf7da0d6SPeter Krystad * TCP option space. 3354cf7da0d6SPeter Krystad */ 3355cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 33560235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3357cf7da0d6SPeter Krystad #endif 3358a16195e3SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3359a16195e3SPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 33600235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3361a16195e3SPaolo Abeni } 33625695eb88SPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 33635695eb88SPaolo Abeni MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3364cf7da0d6SPeter Krystad 336541be81a8SPaolo Abeni do_connect: 33662303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 336741be81a8SPaolo Abeni sock->state = ssock->state; 336841be81a8SPaolo Abeni 336941be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 337041be81a8SPaolo Abeni * subflow_finish_connect() 337141be81a8SPaolo Abeni */ 3372367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3373cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 337441be81a8SPaolo Abeni else 337541be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 33762303f994SPeter Krystad 33772303f994SPeter Krystad unlock: 33782303f994SPeter Krystad release_sock(sock->sk); 33792303f994SPeter Krystad return err; 33802303f994SPeter Krystad } 33812303f994SPeter Krystad 3382cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3383cf7da0d6SPeter Krystad { 3384cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3385cf7da0d6SPeter Krystad struct socket *ssock; 3386cf7da0d6SPeter Krystad int err; 3387cf7da0d6SPeter Krystad 3388cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3389cf7da0d6SPeter Krystad 3390cf7da0d6SPeter Krystad lock_sock(sock->sk); 3391fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3392fa68018dSPaolo Abeni if (!ssock) { 3393fa68018dSPaolo Abeni err = -EINVAL; 3394cf7da0d6SPeter Krystad goto unlock; 3395cf7da0d6SPeter Krystad } 3396cf7da0d6SPeter Krystad 3397fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3398fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 33995e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 34005e20087dSFlorian Westphal 3401cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3402cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3403cf7da0d6SPeter Krystad if (!err) 3404cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3405cf7da0d6SPeter Krystad 3406cf7da0d6SPeter Krystad unlock: 3407cf7da0d6SPeter Krystad release_sock(sock->sk); 3408cf7da0d6SPeter Krystad return err; 3409cf7da0d6SPeter Krystad } 3410cf7da0d6SPeter Krystad 3411cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3412cf7da0d6SPeter Krystad int flags, bool kern) 3413cf7da0d6SPeter Krystad { 3414cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3415cf7da0d6SPeter Krystad struct socket *ssock; 3416cf7da0d6SPeter Krystad int err; 3417cf7da0d6SPeter Krystad 3418cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3419cf7da0d6SPeter Krystad 3420cf7da0d6SPeter Krystad lock_sock(sock->sk); 3421cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 3422cf7da0d6SPeter Krystad goto unlock_fail; 3423cf7da0d6SPeter Krystad 3424cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3425cf7da0d6SPeter Krystad if (!ssock) 3426cf7da0d6SPeter Krystad goto unlock_fail; 3427cf7da0d6SPeter Krystad 34288a05661bSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 3429cf7da0d6SPeter Krystad sock_hold(ssock->sk); 3430cf7da0d6SPeter Krystad release_sock(sock->sk); 3431cf7da0d6SPeter Krystad 3432cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3433d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3434cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3435cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 34360397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 34370397c6d8SPaolo Abeni 34384d54cc32SFlorian Westphal lock_sock(newsk); 34395b950ff4SPaolo Abeni 34405b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 34415b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 34425b950ff4SPaolo Abeni * we can notify it, if needed. 3443c8fe62f0SFlorian Westphal * 3444c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3445c8fe62f0SFlorian Westphal * the refcnt is still at least one. 34465b950ff4SPaolo Abeni */ 34475b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 34485b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 34495b950ff4SPaolo Abeni sock_hold(msk->first); 34505b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 34516c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 34525b950ff4SPaolo Abeni 34530397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 34540397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 34555cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3456cf7da0d6SPeter Krystad 3457cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3458cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3459cf7da0d6SPeter Krystad */ 346078962489SFlorian Westphal mptcp_flush_join_list(msk); 3461190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3462cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3463cf7da0d6SPeter Krystad 3464cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3465cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3466cf7da0d6SPeter Krystad } 34674d54cc32SFlorian Westphal release_sock(newsk); 3468cf7da0d6SPeter Krystad } 3469cf7da0d6SPeter Krystad 34708a05661bSPaolo Abeni if (inet_csk_listen_poll(ssock->sk)) 34718a05661bSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 3472cf7da0d6SPeter Krystad sock_put(ssock->sk); 3473cf7da0d6SPeter Krystad return err; 3474cf7da0d6SPeter Krystad 3475cf7da0d6SPeter Krystad unlock_fail: 3476cf7da0d6SPeter Krystad release_sock(sock->sk); 3477cf7da0d6SPeter Krystad return -EINVAL; 3478cf7da0d6SPeter Krystad } 3479cf7da0d6SPeter Krystad 34808a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 34818a05661bSPaolo Abeni { 3482612f71d7SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 3483612f71d7SPaolo Abeni * always show at least one non-empty queue when checked in this order. 3484612f71d7SPaolo Abeni */ 3485612f71d7SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 3486612f71d7SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 3487612f71d7SPaolo Abeni return 0; 3488612f71d7SPaolo Abeni 3489612f71d7SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 34908a05661bSPaolo Abeni } 34918a05661bSPaolo Abeni 34928edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 34938edf0864SFlorian Westphal { 34948edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 34958edf0864SFlorian Westphal 34968edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3497dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 34988edf0864SFlorian Westphal 34998edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 35008edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 35018edf0864SFlorian Westphal 35025cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 35036e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 35046e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 35056e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 35068edf0864SFlorian Westphal 35076e628cd3SPaolo Abeni return 0; 35088edf0864SFlorian Westphal } 35098edf0864SFlorian Westphal 35102303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 35112303f994SPeter Krystad struct poll_table_struct *wait) 35122303f994SPeter Krystad { 35131891c4a0SFlorian Westphal struct sock *sk = sock->sk; 35148ab183deSPaolo Abeni struct mptcp_sock *msk; 35152303f994SPeter Krystad __poll_t mask = 0; 35168a05661bSPaolo Abeni int state; 35172303f994SPeter Krystad 35181891c4a0SFlorian Westphal msk = mptcp_sk(sk); 35191891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 35201891c4a0SFlorian Westphal 35218a05661bSPaolo Abeni state = inet_sk_state_load(sk); 35226719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 35238a05661bSPaolo Abeni if (state == TCP_LISTEN) 3524612f71d7SPaolo Abeni return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0; 35258a05661bSPaolo Abeni 35268a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 35278a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 35288edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 35298a05661bSPaolo Abeni } 3530dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3531dd913410SPaolo Abeni mask |= EPOLLHUP; 35321891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 35331891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 35341891c4a0SFlorian Westphal 353515cc1045SPaolo Abeni /* This barrier is coupled with smp_wmb() in tcp_reset() */ 353615cc1045SPaolo Abeni smp_rmb(); 353715cc1045SPaolo Abeni if (sk->sk_err) 353815cc1045SPaolo Abeni mask |= EPOLLERR; 353915cc1045SPaolo Abeni 35402303f994SPeter Krystad return mask; 35412303f994SPeter Krystad } 35422303f994SPeter Krystad 3543e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3544e42f1ac6SFlorian Westphal .family = PF_INET, 3545e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 35460a3cc579SPaolo Abeni .release = inet_release, 3547e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3548e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3549e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3550e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3551d2f77c53SPaolo Abeni .getname = inet_getname, 3552e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3553e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3554e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3555e42f1ac6SFlorian Westphal .listen = mptcp_listen, 355676e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3557e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3558e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3559e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3560e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3561e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3562e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3563e42f1ac6SFlorian Westphal }; 35642303f994SPeter Krystad 3565f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3566f870fa0bSMat Martineau .type = SOCK_STREAM, 3567f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3568f870fa0bSMat Martineau .prot = &mptcp_prot, 35692303f994SPeter Krystad .ops = &mptcp_stream_ops, 35702303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3571f870fa0bSMat Martineau }; 3572f870fa0bSMat Martineau 3573b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3574b19bc294SPaolo Abeni { 3575b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3576b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3577b19bc294SPaolo Abeni int work_done = 0; 3578b19bc294SPaolo Abeni 3579b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3580b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3581b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3582b19bc294SPaolo Abeni 3583b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3584b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3585b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3586b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3587b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3588b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3589b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3590b19bc294SPaolo Abeni * CPU that scheduled it. 3591b19bc294SPaolo Abeni */ 3592b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3593b19bc294SPaolo Abeni sock_put(ssk); 3594b19bc294SPaolo Abeni 3595b19bc294SPaolo Abeni if (++work_done == budget) 3596b19bc294SPaolo Abeni return budget; 3597b19bc294SPaolo Abeni } 3598b19bc294SPaolo Abeni 3599b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3600b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3601b19bc294SPaolo Abeni */ 3602b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3603b19bc294SPaolo Abeni return work_done; 3604b19bc294SPaolo Abeni } 3605b19bc294SPaolo Abeni 3606d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3607f870fa0bSMat Martineau { 3608b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3609b19bc294SPaolo Abeni int cpu; 3610b19bc294SPaolo Abeni 36112303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 36122303f994SPeter Krystad 3613d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3614d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3615d027236cSPaolo Abeni 3616b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3617b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3618b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3619b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 3620b19bc294SPaolo Abeni netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3621b19bc294SPaolo Abeni NAPI_POLL_WEIGHT); 3622b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3623b19bc294SPaolo Abeni } 3624b19bc294SPaolo Abeni 36252303f994SPeter Krystad mptcp_subflow_init(); 36261b1c7a0eSPeter Krystad mptcp_pm_init(); 36272c5ebd00SPaolo Abeni mptcp_token_init(); 36282303f994SPeter Krystad 3629f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3630f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3631f870fa0bSMat Martineau 3632f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 36336771bfd9SFlorian Westphal 36346771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3635f870fa0bSMat Martineau } 3636f870fa0bSMat Martineau 3637f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3638e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3639e42f1ac6SFlorian Westphal .family = PF_INET6, 3640e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 36410a3cc579SPaolo Abeni .release = inet6_release, 3642e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3643e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3644e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3645e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3646d2f77c53SPaolo Abeni .getname = inet6_getname, 3647e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3648e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3649e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3650e42f1ac6SFlorian Westphal .listen = mptcp_listen, 365176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3652e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3653e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3654e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3655e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3656e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3657e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3658e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 36593986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3660e42f1ac6SFlorian Westphal #endif 3661e42f1ac6SFlorian Westphal }; 3662e42f1ac6SFlorian Westphal 3663f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3664f870fa0bSMat Martineau 366579c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 366679c0949eSPeter Krystad { 366779c0949eSPeter Krystad mptcp_destroy(sk); 366879c0949eSPeter Krystad inet6_destroy_sock(sk); 366979c0949eSPeter Krystad } 367079c0949eSPeter Krystad 3671f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3672f870fa0bSMat Martineau .type = SOCK_STREAM, 3673f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3674f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 36752303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3676f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3677f870fa0bSMat Martineau }; 3678f870fa0bSMat Martineau 3679d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3680f870fa0bSMat Martineau { 3681f870fa0bSMat Martineau int err; 3682f870fa0bSMat Martineau 3683f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3684f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3685f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 368679c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3687b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3688f870fa0bSMat Martineau 3689f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3690f870fa0bSMat Martineau if (err) 3691f870fa0bSMat Martineau return err; 3692f870fa0bSMat Martineau 3693f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3694f870fa0bSMat Martineau if (err) 3695f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3696f870fa0bSMat Martineau 3697f870fa0bSMat Martineau return err; 3698f870fa0bSMat Martineau } 3699f870fa0bSMat Martineau #endif 3700