1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25644807e3SFlorian Westphal #include <asm/ioctls.h> 26f870fa0bSMat Martineau #include "protocol.h" 27fc518953SFlorian Westphal #include "mib.h" 28f870fa0bSMat Martineau 29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 30e10a9892SGeliang Tang #include <trace/events/mptcp.h> 31e10a9892SGeliang Tang 32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33b0519de8SFlorian Westphal struct mptcp6_sock { 34b0519de8SFlorian Westphal struct mptcp_sock msk; 35b0519de8SFlorian Westphal struct ipv6_pinfo np; 36b0519de8SFlorian Westphal }; 37b0519de8SFlorian Westphal #endif 38b0519de8SFlorian Westphal 39b7f653b2SFlorian Westphal enum { 40b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 412c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 42b7f653b2SFlorian Westphal }; 43b7f653b2SFlorian Westphal 4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45d027236cSPaolo Abeni 46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 47d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 48e16163b6SPaolo Abeni 49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 51b19bc294SPaolo Abeni 522303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 532303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 542303f994SPeter Krystad * Otherwise return NULL. 552303f994SPeter Krystad */ 561729cf18SGeliang Tang struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 572303f994SPeter Krystad { 58d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 592303f994SPeter Krystad return NULL; 602303f994SPeter Krystad 612303f994SPeter Krystad return msk->subflow; 622303f994SPeter Krystad } 632303f994SPeter Krystad 646f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 656f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 666f8a612aSFlorian Westphal { 677439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 686f8a612aSFlorian Westphal } 696f8a612aSFlorian Westphal 70d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 710b4f33deSFlorian Westphal { 720b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 730b4f33deSFlorian Westphal 740b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 750b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 760b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 770b4f33deSFlorian Westphal * not an mptcp one. 780b4f33deSFlorian Westphal * 790b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 800b4f33deSFlorian Westphal * bypass mptcp. 810b4f33deSFlorian Westphal */ 820b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 83d2f77c53SPaolo Abeni return true; 840b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 850b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 860b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 87d2f77c53SPaolo Abeni return true; 880b4f33deSFlorian Westphal #endif 890b4f33deSFlorian Westphal } 900b4f33deSFlorian Westphal 91d2f77c53SPaolo Abeni return false; 920b4f33deSFlorian Westphal } 930b4f33deSFlorian Westphal 94fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 952303f994SPeter Krystad { 962303f994SPeter Krystad struct mptcp_subflow_context *subflow; 972303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 982303f994SPeter Krystad struct socket *ssock; 992303f994SPeter Krystad int err; 1002303f994SPeter Krystad 1016bc1fe7dSPaolo Abeni err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock); 1022303f994SPeter Krystad if (err) 103fa68018dSPaolo Abeni return err; 1042303f994SPeter Krystad 1058ab183deSPaolo Abeni msk->first = ssock->sk; 1062303f994SPeter Krystad msk->subflow = ssock; 1072303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 108cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 109e16163b6SPaolo Abeni sock_hold(ssock->sk); 1102303f994SPeter Krystad subflow->request_mptcp = 1; 1114cf86ae8SPaolo Abeni 1124cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1134cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 114866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 115e1ff9e82SDavide Caratti 116fa68018dSPaolo Abeni return 0; 1172303f994SPeter Krystad } 1182303f994SPeter Krystad 119ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 120ab174ad8SPaolo Abeni { 121ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 122ab174ad8SPaolo Abeni __kfree_skb(skb); 123ab174ad8SPaolo Abeni } 124ab174ad8SPaolo Abeni 1256511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1266511882cSPaolo Abeni { 1276511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1286511882cSPaolo Abeni } 1296511882cSPaolo Abeni 1308268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1318268ed4cSPaolo Abeni struct sk_buff *from) 1328268ed4cSPaolo Abeni { 1338268ed4cSPaolo Abeni bool fragstolen; 1348268ed4cSPaolo Abeni int delta; 1358268ed4cSPaolo Abeni 1368268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1378268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1388268ed4cSPaolo Abeni return false; 1398268ed4cSPaolo Abeni 14006242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 14106242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 14206242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 143ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1447288ff6eSPaolo Abeni 1457288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1467288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1477288ff6eSPaolo Abeni * negative one 1487288ff6eSPaolo Abeni */ 1498268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1506511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1517288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1527288ff6eSPaolo Abeni 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 { 167100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 168100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_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 */ 181d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 182d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 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 19436b122baSDmytro Shytyi 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; 216f3589be0SPaolo Abeni max_seq = atomic64_read(&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, 225f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&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 32369d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3246511882cSPaolo Abeni return true; 3256511882cSPaolo Abeni 32669d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3276511882cSPaolo Abeni amt = sk_mem_pages(size); 328100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3294aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3306511882cSPaolo Abeni return false; 3316511882cSPaolo Abeni 3324aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3336511882cSPaolo Abeni return true; 334ab174ad8SPaolo Abeni } 335ab174ad8SPaolo Abeni 336ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 337ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 338ab174ad8SPaolo Abeni size_t copy_len) 339ab174ad8SPaolo Abeni { 340ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3416771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3424e637c70SFlorian Westphal struct sk_buff *tail; 343b7f653b2SFlorian Westphal bool has_rxtstamp; 3446771bfd9SFlorian Westphal 3456771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3464e637c70SFlorian Westphal 3474e637c70SFlorian Westphal skb_ext_reset(skb); 3484e637c70SFlorian Westphal skb_orphan(skb); 3494e637c70SFlorian Westphal 3509c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3516511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3529c3f94e1SPaolo Abeni goto drop; 35372f96132SPaolo Abeni 354b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 355b7f653b2SFlorian Westphal 356ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 357ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 358ab174ad8SPaolo Abeni * value 359ab174ad8SPaolo Abeni */ 360ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 361ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 362ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 363b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 364ab174ad8SPaolo Abeni 365ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 366ab174ad8SPaolo Abeni /* in sequence */ 367917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3684e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3698268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 370ab174ad8SPaolo Abeni return true; 3714e637c70SFlorian Westphal 3726511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3736771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 374ab174ad8SPaolo Abeni return true; 375ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 376ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 377ab174ad8SPaolo Abeni return false; 378ab174ad8SPaolo Abeni } 379ab174ad8SPaolo Abeni 380ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 381ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 382ab174ad8SPaolo Abeni */ 38306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3849c3f94e1SPaolo Abeni drop: 385ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 386ab174ad8SPaolo Abeni return false; 3876771bfd9SFlorian Westphal } 3886771bfd9SFlorian Westphal 38916a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 39016a9a9daSMat Martineau { 39116a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 39216a9a9daSMat Martineau 39316a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 39416a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 39516a9a9daSMat Martineau } 39616a9a9daSMat Martineau 397e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 398e16163b6SPaolo Abeni { 399e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 400e16163b6SPaolo Abeni return; 401e16163b6SPaolo Abeni 402e16163b6SPaolo Abeni sk->sk_state_change(sk); 403e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 404e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 405e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 406e16163b6SPaolo Abeni else 407e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 408e16163b6SPaolo Abeni } 409e16163b6SPaolo Abeni 4106e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4116e628cd3SPaolo Abeni { 4126e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4136e628cd3SPaolo Abeni 4146e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 4156e628cd3SPaolo Abeni ((1 << sk->sk_state) & 4166e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4176e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4186e628cd3SPaolo Abeni } 4196e628cd3SPaolo Abeni 42016a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 42116a9a9daSMat Martineau { 42216a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 42316a9a9daSMat Martineau 42416a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4256e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 42616a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 42716a9a9daSMat Martineau 42816a9a9daSMat Martineau switch (sk->sk_state) { 42916a9a9daSMat Martineau case TCP_FIN_WAIT1: 43016a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 43116a9a9daSMat Martineau break; 43216a9a9daSMat Martineau case TCP_CLOSING: 43316a9a9daSMat Martineau case TCP_LAST_ACK: 43416a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 43516a9a9daSMat Martineau break; 43616a9a9daSMat Martineau } 43716a9a9daSMat Martineau 438e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 43916a9a9daSMat Martineau } 44016a9a9daSMat Martineau } 44116a9a9daSMat Martineau 4423721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4433721b9b6SMat Martineau { 4443721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4453721b9b6SMat Martineau 4463721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4473721b9b6SMat Martineau ((1 << sk->sk_state) & 4483721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4493721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4503721b9b6SMat Martineau 4513721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4523721b9b6SMat Martineau if (seq) 4533721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4543721b9b6SMat Martineau 4553721b9b6SMat Martineau return true; 4563721b9b6SMat Martineau } 4573721b9b6SMat Martineau } 4583721b9b6SMat Martineau 4593721b9b6SMat Martineau return false; 4603721b9b6SMat Martineau } 4613721b9b6SMat Martineau 4626477dd39SMat Martineau static void mptcp_set_datafin_timeout(const struct sock *sk) 4636477dd39SMat Martineau { 4646477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 465877d11f0SMat Martineau u32 retransmits; 4666477dd39SMat Martineau 467877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 468877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 469877d11f0SMat Martineau 470877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4716477dd39SMat Martineau } 4726477dd39SMat Martineau 47333d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4743721b9b6SMat Martineau { 4753721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4763721b9b6SMat Martineau } 4773721b9b6SMat Martineau 47833d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 47933d41c9cSPaolo Abeni { 48033d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 48133d41c9cSPaolo Abeni 48271b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 48371b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 48433d41c9cSPaolo Abeni } 48533d41c9cSPaolo Abeni 48633d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 48733d41c9cSPaolo Abeni { 48833d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 48933d41c9cSPaolo Abeni long tout = 0; 49033d41c9cSPaolo Abeni 49133d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 49233d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 49333d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 49433d41c9cSPaolo Abeni } 49533d41c9cSPaolo Abeni 49676a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 497fd897679SPaolo Abeni { 498fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 49920bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 500fd897679SPaolo Abeni } 501fd897679SPaolo Abeni 502a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 503a6574302SMat Martineau { 504a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 505a6574302SMat Martineau tcp_send_ack(ssk); 506a6574302SMat Martineau } 507a6574302SMat Martineau 508f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5097ed90803SPaolo Abeni { 51075e908c3SPaolo Abeni bool slow; 5117ed90803SPaolo Abeni 51275e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 513a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 51475e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 515fd897679SPaolo Abeni } 516340fa666SMat Martineau 517340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 518340fa666SMat Martineau { 519340fa666SMat Martineau struct mptcp_subflow_context *subflow; 520340fa666SMat Martineau 521340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 522340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 523ea4ca586SPaolo Abeni } 524ea4ca586SPaolo Abeni 525fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 526fd897679SPaolo Abeni { 52775e908c3SPaolo Abeni bool slow; 528fd897679SPaolo Abeni 52975e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 530fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 531fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 53275e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 533fde56eeaSPaolo Abeni } 534fde56eeaSPaolo Abeni 535fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 536fde56eeaSPaolo Abeni { 537fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 538c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 539fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 540fde56eeaSPaolo Abeni 541fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 542fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 543fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 544fde56eeaSPaolo Abeni (rx_empty && ack_pending & 545fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 546ea4ca586SPaolo Abeni } 547fd897679SPaolo Abeni 548fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 549fd897679SPaolo Abeni { 550e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 551fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 552e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 553fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 554fde56eeaSPaolo Abeni bool cleanup, rx_empty; 555e3859603SPaolo Abeni 556fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 557ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 558fd897679SPaolo Abeni 559fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 560fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 561fd897679SPaolo Abeni 562fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 563fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5647ed90803SPaolo Abeni } 5657ed90803SPaolo Abeni } 5667ed90803SPaolo Abeni 5677ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5683721b9b6SMat Martineau { 5693721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5703721b9b6SMat Martineau u64 rcv_data_fin_seq; 5717ed90803SPaolo Abeni bool ret = false; 5723721b9b6SMat Martineau 573781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5747ed90803SPaolo Abeni return ret; 5753721b9b6SMat Martineau 5763721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5773721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5783721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5793721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5803721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5813721b9b6SMat Martineau * the msk state. 5823721b9b6SMat Martineau * 5833721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5843721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5853721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5863721b9b6SMat Martineau * when catching up. 5873721b9b6SMat Martineau */ 5883721b9b6SMat Martineau 5893721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 590917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5913721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5923721b9b6SMat Martineau 5933721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 59416a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 5953721b9b6SMat Martineau 5963721b9b6SMat Martineau switch (sk->sk_state) { 5973721b9b6SMat Martineau case TCP_ESTABLISHED: 5983721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 5993721b9b6SMat Martineau break; 6003721b9b6SMat Martineau case TCP_FIN_WAIT1: 6013721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6023721b9b6SMat Martineau break; 6033721b9b6SMat Martineau case TCP_FIN_WAIT2: 6043721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6053721b9b6SMat Martineau break; 6063721b9b6SMat Martineau default: 6073721b9b6SMat Martineau /* Other states not expected */ 6083721b9b6SMat Martineau WARN_ON_ONCE(1); 6093721b9b6SMat Martineau break; 6103721b9b6SMat Martineau } 6113721b9b6SMat Martineau 6127ed90803SPaolo Abeni ret = true; 613fd897679SPaolo Abeni mptcp_send_ack(msk); 614e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6153721b9b6SMat Martineau } 6167ed90803SPaolo Abeni return ret; 6173721b9b6SMat Martineau } 6183721b9b6SMat Martineau 6196771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6206771bfd9SFlorian Westphal struct sock *ssk, 6216771bfd9SFlorian Westphal unsigned int *bytes) 6226771bfd9SFlorian Westphal { 6236771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 624600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6256771bfd9SFlorian Westphal unsigned int moved = 0; 6266771bfd9SFlorian Westphal bool more_data_avail; 6276771bfd9SFlorian Westphal struct tcp_sock *tp; 6286771bfd9SFlorian Westphal bool done = false; 62913c7ba0cSFlorian Westphal int sk_rbuf; 63013c7ba0cSFlorian Westphal 63113c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 63213c7ba0cSFlorian Westphal 63313c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 63413c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 63513c7ba0cSFlorian Westphal 63613c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 63713c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 63813c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 63913c7ba0cSFlorian Westphal } 64013c7ba0cSFlorian Westphal } 641600911ffSFlorian Westphal 642ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6436771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6446771bfd9SFlorian Westphal do { 6456771bfd9SFlorian Westphal u32 map_remaining, offset; 6466771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6476771bfd9SFlorian Westphal struct sk_buff *skb; 6486771bfd9SFlorian Westphal bool fin; 6496771bfd9SFlorian Westphal 6506771bfd9SFlorian Westphal /* try to move as much data as available */ 6516771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6526771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6536771bfd9SFlorian Westphal 6546771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 655d9fb8c50SPaolo Abeni if (!skb) { 656d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 657d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 658d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 659d9fb8c50SPaolo Abeni */ 660d9fb8c50SPaolo Abeni if (!moved) 661d9fb8c50SPaolo Abeni done = true; 6626771bfd9SFlorian Westphal break; 663d9fb8c50SPaolo Abeni } 6646771bfd9SFlorian Westphal 665e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 666d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 667d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 668d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 669e1ff9e82SDavide Caratti */ 670e1ff9e82SDavide Caratti map_remaining = skb->len; 671e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 672e1ff9e82SDavide Caratti } 673e1ff9e82SDavide Caratti 6746771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6756771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6766771bfd9SFlorian Westphal if (fin) { 6776771bfd9SFlorian Westphal done = true; 6786771bfd9SFlorian Westphal seq++; 6796771bfd9SFlorian Westphal } 6806771bfd9SFlorian Westphal 6816771bfd9SFlorian Westphal if (offset < skb->len) { 6826771bfd9SFlorian Westphal size_t len = skb->len - offset; 6836771bfd9SFlorian Westphal 6846771bfd9SFlorian Westphal if (tp->urg_data) 6856771bfd9SFlorian Westphal done = true; 6866771bfd9SFlorian Westphal 687ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6886771bfd9SFlorian Westphal moved += len; 689ab174ad8SPaolo Abeni seq += len; 6906771bfd9SFlorian Westphal 6916771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6926771bfd9SFlorian Westphal break; 6936771bfd9SFlorian Westphal } else { 6946771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6956771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 6966771bfd9SFlorian Westphal done = true; 6976771bfd9SFlorian Westphal } 6986771bfd9SFlorian Westphal 6996771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7006771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 701600911ffSFlorian Westphal 70213c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 703600911ffSFlorian Westphal done = true; 704600911ffSFlorian Westphal break; 705600911ffSFlorian Westphal } 7066771bfd9SFlorian Westphal } while (more_data_avail); 7076771bfd9SFlorian Westphal 7086719331cSPaolo Abeni *bytes += moved; 7096771bfd9SFlorian Westphal return done; 7106771bfd9SFlorian Westphal } 7116771bfd9SFlorian Westphal 71287952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 713ab174ad8SPaolo Abeni { 714ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 715ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 716ab174ad8SPaolo Abeni bool moved = false; 717ab174ad8SPaolo Abeni struct rb_node *p; 718ab174ad8SPaolo Abeni u64 end_seq; 719ab174ad8SPaolo Abeni 720ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 72106242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 722ab174ad8SPaolo Abeni while (p) { 723ab174ad8SPaolo Abeni skb = rb_to_skb(p); 724ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 725ab174ad8SPaolo Abeni break; 726ab174ad8SPaolo Abeni 727ab174ad8SPaolo Abeni p = rb_next(p); 728ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 729ab174ad8SPaolo Abeni 730ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 731ab174ad8SPaolo Abeni msk->ack_seq))) { 732ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 73306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 734ab174ad8SPaolo Abeni continue; 735ab174ad8SPaolo Abeni } 736ab174ad8SPaolo Abeni 737ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 738ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 739ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 740ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 741ab174ad8SPaolo Abeni 742ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 74306242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 74406242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 74506242e44SPaolo Abeni delta); 746ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7472c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 748ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 749ab174ad8SPaolo Abeni } 750ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 751ab174ad8SPaolo Abeni moved = true; 752ab174ad8SPaolo Abeni } 753ab174ad8SPaolo Abeni return moved; 754ab174ad8SPaolo Abeni } 755ab174ad8SPaolo Abeni 7562e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7572e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7582e52213cSFlorian Westphal */ 75999d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7602e52213cSFlorian Westphal { 7612e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7622e52213cSFlorian Westphal unsigned int moved = 0; 7632e52213cSFlorian Westphal 7642e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 76587952603SPaolo Abeni __mptcp_ofo_queue(msk); 766499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 767499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 768499ada50SPaolo Abeni __mptcp_error_report(sk); 769499ada50SPaolo Abeni else 770e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 771499ada50SPaolo Abeni } 772ab174ad8SPaolo Abeni 773ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 774ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 775ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 776ab174ad8SPaolo Abeni * do it. 777ab174ad8SPaolo Abeni */ 778ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 779ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 78099d1055cSPaolo Abeni return moved > 0; 7812e52213cSFlorian Westphal } 7822e52213cSFlorian Westphal 7832e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 784101f6f85SFlorian Westphal { 7856719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 786101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 78713c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 788101f6f85SFlorian Westphal 789d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 790d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 791d7b1bfd0SPaolo Abeni * more data to the msk receive queue 792d7b1bfd0SPaolo Abeni */ 793d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 794d7b1bfd0SPaolo Abeni return; 795d7b1bfd0SPaolo Abeni 79613c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 79713c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 79813c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 79913c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 80013c7ba0cSFlorian Westphal 80199d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 802ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 803ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 80499d1055cSPaolo Abeni return; 805ce599c51SPaolo Abeni } 8062e52213cSFlorian Westphal 80799d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 80899d1055cSPaolo Abeni mptcp_data_lock(sk); 809612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 810101f6f85SFlorian Westphal sk->sk_data_ready(sk); 811612f71d7SPaolo Abeni 81299d1055cSPaolo Abeni mptcp_data_unlock(sk); 81399d1055cSPaolo Abeni } 814101f6f85SFlorian Westphal 8153e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 816ec3edaa7SPeter Krystad { 8173e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8185cf92bbaSPaolo Abeni 8193e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 82078962489SFlorian Westphal return false; 821ec3edaa7SPeter Krystad 8223e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8233e501490SPaolo Abeni * at close time 8243e501490SPaolo Abeni */ 8253e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8263e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 82778962489SFlorian Westphal 8283e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 8293e501490SPaolo Abeni return true; 83078962489SFlorian Westphal } 83178962489SFlorian Westphal 8323e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk) 83378962489SFlorian Westphal { 8343e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8353e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 83678962489SFlorian Westphal 8373e501490SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 8383e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8393e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8403e501490SPaolo Abeni 8413e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8423e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8433e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8443e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 84578962489SFlorian Westphal } 846ec3edaa7SPeter Krystad } 847ec3edaa7SPeter Krystad 848b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 849b51f9b80SPaolo Abeni { 850b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 851b51f9b80SPaolo Abeni } 852b51f9b80SPaolo Abeni 853b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 854b51f9b80SPaolo Abeni { 855b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 856b51f9b80SPaolo Abeni unsigned long tout; 857b51f9b80SPaolo Abeni 858e16163b6SPaolo Abeni /* prevent rescheduling on close */ 859e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 860e16163b6SPaolo Abeni return; 861e16163b6SPaolo Abeni 86233d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 863b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 864b51f9b80SPaolo Abeni } 865b51f9b80SPaolo Abeni 866ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 867ba8f48f7SPaolo Abeni { 868ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 869ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 870ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 871ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 872ba8f48f7SPaolo Abeni */ 873ba8f48f7SPaolo Abeni sock_hold(sk); 874ba8f48f7SPaolo Abeni return true; 875ba8f48f7SPaolo Abeni } 876ba8f48f7SPaolo Abeni return false; 877ba8f48f7SPaolo Abeni } 878ba8f48f7SPaolo Abeni 87959832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 88059832e24SFlorian Westphal { 881ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 882ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 88359832e24SFlorian Westphal } 88459832e24SFlorian Westphal 8855969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8865969856aSPaolo Abeni { 8875969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 8885969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 8895969856aSPaolo Abeni int receivers = 0; 8905969856aSPaolo Abeni 8915969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 8925969856aSPaolo Abeni receivers += !subflow->rx_eof; 893e16163b6SPaolo Abeni if (receivers) 894e16163b6SPaolo Abeni return; 8955969856aSPaolo Abeni 896e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 8975969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 8985969856aSPaolo Abeni * to msk, when all subflows agree on it 8995969856aSPaolo Abeni */ 9005969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 9015969856aSPaolo Abeni 9025969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9035969856aSPaolo Abeni sk->sk_data_ready(sk); 9045969856aSPaolo Abeni } 905e16163b6SPaolo Abeni 906e16163b6SPaolo Abeni switch (sk->sk_state) { 907e16163b6SPaolo Abeni case TCP_ESTABLISHED: 908e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 909e16163b6SPaolo Abeni break; 910e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 91126aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 91226aa2314SPaolo Abeni break; 91326aa2314SPaolo Abeni case TCP_FIN_WAIT2: 914e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 915e16163b6SPaolo Abeni break; 916e16163b6SPaolo Abeni default: 917e16163b6SPaolo Abeni return; 918e16163b6SPaolo Abeni } 919e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9205969856aSPaolo Abeni } 9215969856aSPaolo Abeni 9227a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9237a6a6cbcSPaolo Abeni { 9247a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9257a6a6cbcSPaolo Abeni 926109cdeb8SGeliang Tang msk_owned_by_me(msk); 9277a6a6cbcSPaolo Abeni 9287a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 92999d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9307a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9317a6a6cbcSPaolo Abeni } 9327a6a6cbcSPaolo Abeni 9337a6a6cbcSPaolo Abeni return NULL; 9347a6a6cbcSPaolo Abeni } 9357a6a6cbcSPaolo Abeni 9363f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 93757040755SPaolo Abeni const struct sk_buff *skb, 93857040755SPaolo Abeni const struct mptcp_ext *mpext) 9396d0060f6SMat Martineau { 94057040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 94157040755SPaolo Abeni return false; 94257040755SPaolo Abeni 9435a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9445a369ca6SPaolo Abeni * mapping has not been xmitted yet 9455a369ca6SPaolo Abeni */ 9465a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9475a369ca6SPaolo Abeni !mpext->frozen; 94857040755SPaolo Abeni } 94957040755SPaolo Abeni 95029249eacSPaolo Abeni /* we can append data to the given data frag if: 95129249eacSPaolo Abeni * - there is space available in the backing page_frag 95229249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 95329249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 95429249eacSPaolo Abeni */ 95518b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 95618b683bfSPaolo Abeni const struct page_frag *pfrag, 95718b683bfSPaolo Abeni const struct mptcp_data_frag *df) 95818b683bfSPaolo Abeni { 95918b683bfSPaolo Abeni return df && pfrag->page == df->page && 960d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 96129249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 96218b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 96318b683bfSPaolo Abeni } 96418b683bfSPaolo Abeni 965d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 96618b683bfSPaolo Abeni { 967d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9687948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 969d027236cSPaolo Abeni } 970d027236cSPaolo Abeni 971d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 972d027236cSPaolo Abeni { 973d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 974d027236cSPaolo Abeni 97518b683bfSPaolo Abeni list_del(&dfrag->list); 976d027236cSPaolo Abeni dfrag_uncharge(sk, len); 97718b683bfSPaolo Abeni put_page(dfrag->page); 97818b683bfSPaolo Abeni } 97918b683bfSPaolo Abeni 9806e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 98118b683bfSPaolo Abeni { 98218b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 98318b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 984e1ff9e82SDavide Caratti u64 snd_una; 985e1ff9e82SDavide Caratti 986e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 987e1ff9e82SDavide Caratti * plain TCP 988e1ff9e82SDavide Caratti */ 989e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 9907439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 9917439d687SPaolo Abeni 9927439d687SPaolo Abeni snd_una = msk->snd_una; 99318b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 99418b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 99518b683bfSPaolo Abeni break; 99618b683bfSPaolo Abeni 9971e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 9981e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 9991e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1000d9ca1de8SPaolo Abeni break; 10011e1d9d6fSPaolo Abeni 10021e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10031e1d9d6fSPaolo Abeni } 10041e1d9d6fSPaolo Abeni 1005d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1006d027236cSPaolo Abeni } 1007d027236cSPaolo Abeni 10087948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10097948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 101053eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 101153eb4c38SPaolo Abeni 10121e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10131e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10141e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 101553eb4c38SPaolo Abeni goto out; 10161e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10171e1d9d6fSPaolo Abeni goto out; 10181e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10191e1d9d6fSPaolo Abeni } 10207948f6ccSFlorian Westphal 10217948f6ccSFlorian Westphal dfrag->data_seq += delta; 102253eb4c38SPaolo Abeni dfrag->offset += delta; 10237948f6ccSFlorian Westphal dfrag->data_len -= delta; 1024d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10257948f6ccSFlorian Westphal 10267948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10277948f6ccSFlorian Westphal } 10287948f6ccSFlorian Westphal 10291e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10301e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10311e1d9d6fSPaolo Abeni msk->recovery = false; 10321e1d9d6fSPaolo Abeni 103353eb4c38SPaolo Abeni out: 10343241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10353241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 103633d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10376e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10386e628cd3SPaolo Abeni } else { 10396e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10406e628cd3SPaolo Abeni } 10416e628cd3SPaolo Abeni } 10426e628cd3SPaolo Abeni 1043417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1044417789dfSPaolo Abeni { 1045765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1046765ff425SPaolo Abeni 1047417789dfSPaolo Abeni __mptcp_clean_una(sk); 1048417789dfSPaolo Abeni mptcp_write_space(sk); 1049417789dfSPaolo Abeni } 1050417789dfSPaolo Abeni 1051b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1052b5941f06SPaolo Abeni { 1053b5941f06SPaolo Abeni mptcp_data_lock(sk); 1054b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1055b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1056b5941f06SPaolo Abeni } 1057b5941f06SPaolo Abeni 1058724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 105918b683bfSPaolo Abeni { 1060d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1061d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1062d9ca1de8SPaolo Abeni bool first = true; 1063d9ca1de8SPaolo Abeni 106418b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1065d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1066d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1067d9ca1de8SPaolo Abeni 1068d9ca1de8SPaolo Abeni if (first) 1069d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1070d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1071d9ca1de8SPaolo Abeni first = false; 1072d9ca1de8SPaolo Abeni } 1073724cfd2eSPaolo Abeni } 1074724cfd2eSPaolo Abeni 1075724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1076724cfd2eSPaolo Abeni * data 1077724cfd2eSPaolo Abeni */ 1078724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1079724cfd2eSPaolo Abeni { 1080724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1081724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1082724cfd2eSPaolo Abeni return true; 1083724cfd2eSPaolo Abeni 1084724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 108518b683bfSPaolo Abeni return false; 108618b683bfSPaolo Abeni } 108718b683bfSPaolo Abeni 108818b683bfSPaolo Abeni static struct mptcp_data_frag * 108918b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 109018b683bfSPaolo Abeni int orig_offset) 109118b683bfSPaolo Abeni { 109218b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 109318b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 109418b683bfSPaolo Abeni 109518b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 109618b683bfSPaolo Abeni dfrag->data_len = 0; 109718b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 109818b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 109918b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1100d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 110118b683bfSPaolo Abeni dfrag->page = pfrag->page; 110218b683bfSPaolo Abeni 110318b683bfSPaolo Abeni return dfrag; 110418b683bfSPaolo Abeni } 110518b683bfSPaolo Abeni 1106caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1107caf971dfSPaolo Abeni int mss_now; 1108caf971dfSPaolo Abeni int size_goal; 1109d9ca1de8SPaolo Abeni u16 limit; 1110d9ca1de8SPaolo Abeni u16 sent; 1111d9ca1de8SPaolo Abeni unsigned int flags; 11121094c6feSPaolo Abeni bool data_lock_held; 1113caf971dfSPaolo Abeni }; 1114caf971dfSPaolo Abeni 1115b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1116b713d006SPaolo Abeni u64 data_seq, int avail_size) 11176f8a612aSFlorian Westphal { 11186f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1119b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11206f8a612aSFlorian Westphal 11216f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11226f8a612aSFlorian Westphal return avail_size; 11236f8a612aSFlorian Westphal 1124b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1125b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11266f8a612aSFlorian Westphal 112792be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1128b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 112992be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 113092be2f52SPaolo Abeni } 11316f8a612aSFlorian Westphal 11326f8a612aSFlorian Westphal return avail_size; 11336f8a612aSFlorian Westphal } 11346f8a612aSFlorian Westphal 1135724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1136724cfd2eSPaolo Abeni { 1137724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1138724cfd2eSPaolo Abeni 1139724cfd2eSPaolo Abeni if (!mpext) 1140724cfd2eSPaolo Abeni return false; 1141724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1142724cfd2eSPaolo Abeni return true; 1143724cfd2eSPaolo Abeni } 1144724cfd2eSPaolo Abeni 11456e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1146724cfd2eSPaolo Abeni { 1147724cfd2eSPaolo Abeni struct sk_buff *skb; 1148724cfd2eSPaolo Abeni 11496e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1150724cfd2eSPaolo Abeni if (likely(skb)) { 11516e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1152724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1153a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1154f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1155724cfd2eSPaolo Abeni return skb; 1156724cfd2eSPaolo Abeni } 1157724cfd2eSPaolo Abeni __kfree_skb(skb); 1158724cfd2eSPaolo Abeni } else { 1159724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1160724cfd2eSPaolo Abeni } 1161724cfd2eSPaolo Abeni return NULL; 1162724cfd2eSPaolo Abeni } 1163724cfd2eSPaolo Abeni 1164f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1165724cfd2eSPaolo Abeni { 1166724cfd2eSPaolo Abeni struct sk_buff *skb; 1167724cfd2eSPaolo Abeni 11686e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1169724cfd2eSPaolo Abeni if (!skb) 1170f70cad10SPaolo Abeni return NULL; 1171724cfd2eSPaolo Abeni 1172724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1173f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1174f70cad10SPaolo Abeni return skb; 1175724cfd2eSPaolo Abeni } 11763ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1177724cfd2eSPaolo Abeni kfree_skb(skb); 1178f70cad10SPaolo Abeni return NULL; 1179724cfd2eSPaolo Abeni } 1180724cfd2eSPaolo Abeni 1181f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1182724cfd2eSPaolo Abeni { 11831094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1184724cfd2eSPaolo Abeni 11851094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1186724cfd2eSPaolo Abeni } 1187724cfd2eSPaolo Abeni 1188d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1189d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1190d0cc2987SGeliang Tang */ 1191d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1192d0cc2987SGeliang Tang { 1193d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1194d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1195d0cc2987SGeliang Tang int offset = skb->len - added; 1196d0cc2987SGeliang Tang 1197d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1198d0cc2987SGeliang Tang } 1199d0cc2987SGeliang Tang 12001e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12011e39e5a3SGeliang Tang struct sock *ssk, 12021e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12031e39e5a3SGeliang Tang { 12041e39e5a3SGeliang Tang if (!mpext) 12051e39e5a3SGeliang Tang return; 12061e39e5a3SGeliang Tang 12071e39e5a3SGeliang Tang mpext->infinite_map = 1; 12081e39e5a3SGeliang Tang mpext->data_len = 0; 12091e39e5a3SGeliang Tang 1210104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12111e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12121e39e5a3SGeliang Tang pr_fallback(msk); 1213d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12141e39e5a3SGeliang Tang } 12151e39e5a3SGeliang Tang 121657040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1217d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1218caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 121957040755SPaolo Abeni { 1220d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1221f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12226d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12236f8a612aSFlorian Westphal bool zero_window_probe = false; 12246d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1225f70cad10SPaolo Abeni bool can_coalesce = false; 1226f70cad10SPaolo Abeni bool reuse_skb = true; 1227f70cad10SPaolo Abeni struct sk_buff *skb; 1228f70cad10SPaolo Abeni size_t copy; 1229f70cad10SPaolo Abeni int i; 12306d0060f6SMat Martineau 1231e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1232d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12336d0060f6SMat Martineau 1234f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1235f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1236f70cad10SPaolo Abeni return 0; 1237f70cad10SPaolo Abeni 1238c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1239c886d702SPaolo Abeni return -EAGAIN; 1240c886d702SPaolo Abeni 1241d9ca1de8SPaolo Abeni /* compute send limit */ 1242d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1243f70cad10SPaolo Abeni copy = info->size_goal; 1244f70cad10SPaolo Abeni 124557040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1246f70cad10SPaolo Abeni if (skb && copy > skb->len) { 124757040755SPaolo Abeni /* Limit the write to the size available in the 124857040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 124957040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 125057040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 125157040755SPaolo Abeni * SSN association set here 125257040755SPaolo Abeni */ 1253d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12541094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 125557040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12561094c6feSPaolo Abeni goto alloc_skb; 12571094c6feSPaolo Abeni } 12581094c6feSPaolo Abeni 1259f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1260f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1261657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1262f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1263f70cad10SPaolo Abeni goto alloc_skb; 126415e6ca97SPaolo Abeni } 126518b683bfSPaolo Abeni 1266f70cad10SPaolo Abeni copy -= skb->len; 1267f70cad10SPaolo Abeni } else { 12681094c6feSPaolo Abeni alloc_skb: 1269f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1270f70cad10SPaolo Abeni if (!skb) 1271f70cad10SPaolo Abeni return -ENOMEM; 1272f70cad10SPaolo Abeni 1273f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1274f70cad10SPaolo Abeni reuse_skb = false; 1275f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1276f70cad10SPaolo Abeni } 12771094c6feSPaolo Abeni 12786f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1279b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1280f70cad10SPaolo Abeni if (copy == 0) { 12817439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12827439d687SPaolo Abeni 1283f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 128427728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12856f8a612aSFlorian Westphal return 0; 12866f8a612aSFlorian Westphal } 12876f8a612aSFlorian Westphal 1288f70cad10SPaolo Abeni zero_window_probe = true; 1289f70cad10SPaolo Abeni data_seq = snd_una - 1; 1290f70cad10SPaolo Abeni copy = 1; 129157040755SPaolo Abeni 1292f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1293f70cad10SPaolo Abeni * ssk write queue 1294f70cad10SPaolo Abeni */ 1295f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1296f70cad10SPaolo Abeni } 1297f70cad10SPaolo Abeni 1298f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1299f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 130027728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1301e2223995SPaolo Abeni return -ENOMEM; 130235759383SFlorian Westphal } 130318b683bfSPaolo Abeni 1304f70cad10SPaolo Abeni if (can_coalesce) { 1305f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1306f70cad10SPaolo Abeni } else { 1307f70cad10SPaolo Abeni get_page(dfrag->page); 1308f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 130957040755SPaolo Abeni } 131057040755SPaolo Abeni 1311f70cad10SPaolo Abeni skb->len += copy; 1312f70cad10SPaolo Abeni skb->data_len += copy; 1313f70cad10SPaolo Abeni skb->truesize += copy; 1314f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1315f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1316f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1317f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1318f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1319f70cad10SPaolo Abeni 1320f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1321f70cad10SPaolo Abeni if (reuse_skb) { 1322f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1323f70cad10SPaolo Abeni mpext->data_len += copy; 1324f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1325f70cad10SPaolo Abeni goto out; 1326724cfd2eSPaolo Abeni } 13276d0060f6SMat Martineau 13286d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1329d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13306d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1331f70cad10SPaolo Abeni mpext->data_len = copy; 13326d0060f6SMat Martineau mpext->use_map = 1; 13336d0060f6SMat Martineau mpext->dsn64 = 1; 13346d0060f6SMat Martineau 13356d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13366d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13376d0060f6SMat Martineau mpext->dsn64); 13386d0060f6SMat Martineau 13396f8a612aSFlorian Westphal if (zero_window_probe) { 1340f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13416f8a612aSFlorian Westphal mpext->frozen = 1; 1342d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1343f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13446f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1345d0cc2987SGeliang Tang return 0; 13466f8a612aSFlorian Westphal } 134757040755SPaolo Abeni out: 1348d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1349f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13501e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13511e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13520eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1353f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1354f70cad10SPaolo Abeni return copy; 13556d0060f6SMat Martineau } 13566d0060f6SMat Martineau 1357d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1358d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1359d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1360d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1361d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1362d5f49190SPaolo Abeni 1363d5f49190SPaolo Abeni struct subflow_send_info { 1364d5f49190SPaolo Abeni struct sock *ssk; 13653ce0852cSPaolo Abeni u64 linger_time; 1366d5f49190SPaolo Abeni }; 1367d5f49190SPaolo Abeni 1368ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1369ff5a0b42SPaolo Abeni { 1370ff5a0b42SPaolo Abeni if (!subflow->stale) 1371ff5a0b42SPaolo Abeni return; 1372ff5a0b42SPaolo Abeni 1373ff5a0b42SPaolo Abeni subflow->stale = 0; 1374fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1375ff5a0b42SPaolo Abeni } 1376ff5a0b42SPaolo Abeni 1377ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1378ff5a0b42SPaolo Abeni { 1379ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1380ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1381ff5a0b42SPaolo Abeni 1382ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1383ff5a0b42SPaolo Abeni return false; 1384ff5a0b42SPaolo Abeni 1385ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1386ff5a0b42SPaolo Abeni } 1387ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1388ff5a0b42SPaolo Abeni } 1389ff5a0b42SPaolo Abeni 13903ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13913ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13923ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13933ce0852cSPaolo Abeni 139433d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 139533d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 139633d41c9cSPaolo Abeni * additionally updates the rtx timeout 139733d41c9cSPaolo Abeni */ 13985cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1399f296234cSPeter Krystad { 14003ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1401f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 140233d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14033ce0852cSPaolo Abeni u32 pace, burst, wmem; 1404d5f49190SPaolo Abeni int i, nr_active = 0; 1405d5f49190SPaolo Abeni struct sock *ssk; 14063ce0852cSPaolo Abeni u64 linger_time; 140733d41c9cSPaolo Abeni long tout = 0; 1408f296234cSPeter Krystad 1409109cdeb8SGeliang Tang msk_owned_by_me(msk); 1410f296234cSPeter Krystad 1411d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1412d5f49190SPaolo Abeni if (!msk->first) 1413f296234cSPeter Krystad return NULL; 1414c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1415c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1416f296234cSPeter Krystad } 1417f296234cSPeter Krystad 1418d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1419d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1420d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 142133d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 142233d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1423d5f49190SPaolo Abeni return msk->last_snd; 142433d41c9cSPaolo Abeni } 1425f296234cSPeter Krystad 1426d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14273ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1428d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14293ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1430d5f49190SPaolo Abeni } 14313ce0852cSPaolo Abeni 1432d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1433e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1434d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1435d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1436f296234cSPeter Krystad continue; 1437d5f49190SPaolo Abeni 143833d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1439d5f49190SPaolo Abeni nr_active += !subflow->backup; 14403ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14413ce0852cSPaolo Abeni if (unlikely(!pace)) { 14423ce0852cSPaolo Abeni /* init pacing rate from socket */ 14433ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14443ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1445d5f49190SPaolo Abeni if (!pace) 1446d5f49190SPaolo Abeni continue; 14473ce0852cSPaolo Abeni } 1448d5f49190SPaolo Abeni 14493ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14503ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1451d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14523ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1453d5f49190SPaolo Abeni } 1454f296234cSPeter Krystad } 145533d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1456f296234cSPeter Krystad 1457d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1458d5f49190SPaolo Abeni if (!nr_active) 14593ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1460d5f49190SPaolo Abeni 14613ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14623ce0852cSPaolo Abeni * faster flow, we need to: 14633ce0852cSPaolo Abeni * - estimate the faster flow linger time 14643ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14653ce0852cSPaolo Abeni * by the faster flow 14663ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14673ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14683ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14693ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14703ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14713ce0852cSPaolo Abeni */ 14723ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1473b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1474d5f49190SPaolo Abeni return NULL; 14753ce0852cSPaolo Abeni 1476b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14773ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1478b713d006SPaolo Abeni if (!burst) { 1479b713d006SPaolo Abeni msk->last_snd = NULL; 1480b713d006SPaolo Abeni return ssk; 1481b713d006SPaolo Abeni } 1482b713d006SPaolo Abeni 14833ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14843ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14853ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14863ce0852cSPaolo Abeni burst + wmem); 14873ce0852cSPaolo Abeni msk->last_snd = ssk; 14883ce0852cSPaolo Abeni msk->snd_burst = burst; 14893ce0852cSPaolo Abeni return ssk; 1490f296234cSPeter Krystad } 1491f296234cSPeter Krystad 1492b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1493d9ca1de8SPaolo Abeni { 1494d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1495d9ca1de8SPaolo Abeni release_sock(ssk); 1496d9ca1de8SPaolo Abeni } 1497d9ca1de8SPaolo Abeni 14980d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14990d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15000d199e43SFlorian Westphal u32 sent) 15010d199e43SFlorian Westphal { 15020d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15030d199e43SFlorian Westphal 15040d199e43SFlorian Westphal dfrag->already_sent += sent; 15050d199e43SFlorian Westphal 15060d199e43SFlorian Westphal msk->snd_burst -= sent; 15070d199e43SFlorian Westphal 15080d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15090d199e43SFlorian Westphal 15100d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15110d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15120d199e43SFlorian Westphal * old segments. 15130d199e43SFlorian Westphal * 15140d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15150d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15160d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15170d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15180d199e43SFlorian Westphal */ 15190d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15200d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15210d199e43SFlorian Westphal } 15220d199e43SFlorian Westphal 15238b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15243241a9c0SFlorian Westphal { 1525e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1526e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15273241a9c0SFlorian Westphal } 15283241a9c0SFlorian Westphal 1529ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1530d9ca1de8SPaolo Abeni { 1531d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1532d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1533d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1534d9ca1de8SPaolo Abeni .flags = flags, 1535d9ca1de8SPaolo Abeni }; 15360522b424SGeliang Tang bool do_check_data_fin = false; 1537d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15380522b424SGeliang Tang int len; 1539d9ca1de8SPaolo Abeni 1540d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1541d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1542d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1543d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1544d9ca1de8SPaolo Abeni while (len > 0) { 1545d9ca1de8SPaolo Abeni int ret = 0; 1546d9ca1de8SPaolo Abeni 1547d9ca1de8SPaolo Abeni prev_ssk = ssk; 15485cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1549d9ca1de8SPaolo Abeni 15509758f40eSPaolo Abeni /* First check. If the ssk has changed since 15519758f40eSPaolo Abeni * the last round, release prev_ssk 1552d9ca1de8SPaolo Abeni */ 1553d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1554b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1555d9ca1de8SPaolo Abeni if (!ssk) 1556d9ca1de8SPaolo Abeni goto out; 1557d9ca1de8SPaolo Abeni 15589758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15599758f40eSPaolo Abeni * from the previous one, otherwise we are still 15609758f40eSPaolo Abeni * helding the relevant lock 15619758f40eSPaolo Abeni */ 15629758f40eSPaolo Abeni if (ssk != prev_ssk) 1563d9ca1de8SPaolo Abeni lock_sock(ssk); 1564d9ca1de8SPaolo Abeni 1565d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1566d9ca1de8SPaolo Abeni if (ret <= 0) { 1567c886d702SPaolo Abeni if (ret == -EAGAIN) 1568c886d702SPaolo Abeni continue; 1569b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1570d9ca1de8SPaolo Abeni goto out; 1571d9ca1de8SPaolo Abeni } 1572d9ca1de8SPaolo Abeni 15730522b424SGeliang Tang do_check_data_fin = true; 1574d9ca1de8SPaolo Abeni info.sent += ret; 1575d9ca1de8SPaolo Abeni len -= ret; 15760d199e43SFlorian Westphal 15770d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1578d9ca1de8SPaolo Abeni } 1579d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1580d9ca1de8SPaolo Abeni } 1581d9ca1de8SPaolo Abeni 1582d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1583d9ca1de8SPaolo Abeni if (ssk) 1584b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1585d9ca1de8SPaolo Abeni 1586d9ca1de8SPaolo Abeni out: 158733d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1588d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1589d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 15900522b424SGeliang Tang if (do_check_data_fin) 1591d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1592d9ca1de8SPaolo Abeni } 1593d9ca1de8SPaolo Abeni 159473a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 15956e628cd3SPaolo Abeni { 15966e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15971094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 15981094c6feSPaolo Abeni .data_lock_held = true, 15991094c6feSPaolo Abeni }; 16006e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1601b19bc294SPaolo Abeni struct sock *xmit_ssk; 16026e628cd3SPaolo Abeni int len, copied = 0; 16036e628cd3SPaolo Abeni 16046e628cd3SPaolo Abeni info.flags = 0; 16056e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16066e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16076e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16086e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16096e628cd3SPaolo Abeni while (len > 0) { 16106e628cd3SPaolo Abeni int ret = 0; 16116e628cd3SPaolo Abeni 161273a0052aSGeliang Tang /* check for a different subflow usage only after 1613b19bc294SPaolo Abeni * spooling the first chunk of data 1614b19bc294SPaolo Abeni */ 161500df24f1SGeliang Tang xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1616b19bc294SPaolo Abeni if (!xmit_ssk) 1617b19bc294SPaolo Abeni goto out; 1618b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1619bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1620bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1621b19bc294SPaolo Abeni goto out; 1622b19bc294SPaolo Abeni } 1623b19bc294SPaolo Abeni 16246e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16256e628cd3SPaolo Abeni if (ret <= 0) 16266e628cd3SPaolo Abeni goto out; 16276e628cd3SPaolo Abeni 16286e628cd3SPaolo Abeni info.sent += ret; 16296e628cd3SPaolo Abeni copied += ret; 16306e628cd3SPaolo Abeni len -= ret; 1631b19bc294SPaolo Abeni first = false; 16320d199e43SFlorian Westphal 16330d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16346e628cd3SPaolo Abeni } 16356e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16366e628cd3SPaolo Abeni } 16376e628cd3SPaolo Abeni 16386e628cd3SPaolo Abeni out: 16396e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16406e628cd3SPaolo Abeni * not going to flush it via release_sock() 16416e628cd3SPaolo Abeni */ 16426e628cd3SPaolo Abeni if (copied) { 16436e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16446e628cd3SPaolo Abeni info.size_goal); 1645d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1646d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1647d09d818eSPaolo Abeni 16486e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16496e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16506e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16516e628cd3SPaolo Abeni } 16526e628cd3SPaolo Abeni } 16536e628cd3SPaolo Abeni 16545cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16555cf92bbaSPaolo Abeni { 16565cf92bbaSPaolo Abeni /* enable autotune */ 16575cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16585cf92bbaSPaolo Abeni 16595cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16605cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16615cf92bbaSPaolo Abeni } 16625cf92bbaSPaolo Abeni 16637d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 16647d803344SPaolo Abeni 1665fa9e5746SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg, 1666fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1667fa9e5746SPaolo Abeni { 1668fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1669fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1670fa9e5746SPaolo Abeni int ret; 1671fa9e5746SPaolo Abeni 1672fa9e5746SPaolo Abeni lock_sock(ssk); 1673fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 1674fa9e5746SPaolo Abeni msk->connect_flags = O_NONBLOCK; 16757d803344SPaolo Abeni msk->fastopening = 1; 1676fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 16777d803344SPaolo Abeni msk->fastopening = 0; 1678fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1679fa9e5746SPaolo Abeni release_sock(ssk); 1680fa9e5746SPaolo Abeni 1681fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1682fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1683fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1684fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1685fa9e5746SPaolo Abeni 1686fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1687fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1688fa9e5746SPaolo Abeni */ 1689fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1690fa9e5746SPaolo Abeni *copied_syn = 0; 16917d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 16927d803344SPaolo Abeni mptcp_disconnect(sk, 0); 1693fa9e5746SPaolo Abeni } 1694fa9e5746SPaolo Abeni 1695fa9e5746SPaolo Abeni return ret; 1696fa9e5746SPaolo Abeni } 1697fa9e5746SPaolo Abeni 1698f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1699f870fa0bSMat Martineau { 1700f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 170117091708SFlorian Westphal struct page_frag *pfrag; 1702d98a82a6SDmytro Shytyi struct socket *ssock; 17036d0060f6SMat Martineau size_t copied = 0; 1704caf971dfSPaolo Abeni int ret = 0; 17056d0060f6SMat Martineau long timeo; 1706f870fa0bSMat Martineau 1707987858e5SPaolo Abeni /* silently ignore everything else */ 17081e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1709987858e5SPaolo Abeni 17106511882cSPaolo Abeni lock_sock(sk); 17111954b860SMat Martineau 1712d98a82a6SDmytro Shytyi ssock = __mptcp_nmpc_socket(msk); 17131e777f39SDmytro Shytyi if (unlikely(ssock && (inet_sk(ssock->sk)->defer_connect || 17141e777f39SDmytro Shytyi msg->msg_flags & MSG_FASTOPEN))) { 1715d98a82a6SDmytro Shytyi int copied_syn = 0; 1716d98a82a6SDmytro Shytyi 1717fa9e5746SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn); 1718d98a82a6SDmytro Shytyi copied += copied_syn; 1719fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1720d98a82a6SDmytro Shytyi goto out; 1721fa9e5746SPaolo Abeni else if (ret) 172269800e51SPaolo Abeni goto do_error; 1723d98a82a6SDmytro Shytyi } 1724d98a82a6SDmytro Shytyi 17251954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17261954b860SMat Martineau 17271954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17281954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17291954b860SMat Martineau if (ret) 173069800e51SPaolo Abeni goto do_error; 17311954b860SMat Martineau } 17321954b860SMat Martineau 173369800e51SPaolo Abeni ret = -EPIPE; 173469800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 173569800e51SPaolo Abeni goto do_error; 173669800e51SPaolo Abeni 173717091708SFlorian Westphal pfrag = sk_page_frag(sk); 173818b683bfSPaolo Abeni 1739d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1740724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1741d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1742d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1743d9ca1de8SPaolo Abeni size_t psize, offset; 1744d9ca1de8SPaolo Abeni 1745d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1746d9ca1de8SPaolo Abeni * page allocator 1747fb529e62SFlorian Westphal */ 1748d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1749d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1750d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1751d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1752d9ca1de8SPaolo Abeni goto wait_for_memory; 17536e628cd3SPaolo Abeni 1754d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1755d9ca1de8SPaolo Abeni goto wait_for_memory; 1756d9ca1de8SPaolo Abeni 1757d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1758d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1759fb529e62SFlorian Westphal } 1760fb529e62SFlorian Westphal 1761d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1762d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1763d9ca1de8SPaolo Abeni * anyway 1764d9ca1de8SPaolo Abeni */ 1765d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1766d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1767d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1768724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17698ce568edSPaolo Abeni 17706511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1771d9ca1de8SPaolo Abeni goto wait_for_memory; 1772d9ca1de8SPaolo Abeni 1773d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1774d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1775d9ca1de8SPaolo Abeni ret = -EFAULT; 177669800e51SPaolo Abeni goto do_error; 1777d9ca1de8SPaolo Abeni } 1778d9ca1de8SPaolo Abeni 1779d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17806511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1781d9ca1de8SPaolo Abeni copied += psize; 1782d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1783d9ca1de8SPaolo Abeni frag_truesize += psize; 1784d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1785d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1786d9ca1de8SPaolo Abeni 1787d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1788d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1789d9ca1de8SPaolo Abeni */ 1790d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1791d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1792d9ca1de8SPaolo Abeni get_page(dfrag->page); 1793d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1794d9ca1de8SPaolo Abeni if (!msk->first_pending) 1795d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1796d9ca1de8SPaolo Abeni } 1797e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1798d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1799d9ca1de8SPaolo Abeni !dfrag_collapsed); 1800d9ca1de8SPaolo Abeni 1801d9ca1de8SPaolo Abeni continue; 1802d9ca1de8SPaolo Abeni 1803d9ca1de8SPaolo Abeni wait_for_memory: 18045cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1805c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1806f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1807f296234cSPeter Krystad if (ret) 180869800e51SPaolo Abeni goto do_error; 1809cec37a6eSPeter Krystad } 1810cec37a6eSPeter Krystad 181113e16037SPaolo Abeni if (copied) 1812c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1813da51aef5SPaolo Abeni 18141954b860SMat Martineau out: 1815cec37a6eSPeter Krystad release_sock(sk); 181669800e51SPaolo Abeni return copied; 181769800e51SPaolo Abeni 181869800e51SPaolo Abeni do_error: 181969800e51SPaolo Abeni if (copied) 182069800e51SPaolo Abeni goto out; 182169800e51SPaolo Abeni 182269800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 182369800e51SPaolo Abeni goto out; 1824f870fa0bSMat Martineau } 1825f870fa0bSMat Martineau 18266771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18276771bfd9SFlorian Westphal struct msghdr *msg, 1828b7f653b2SFlorian Westphal size_t len, int flags, 1829b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1830b7f653b2SFlorian Westphal int *cmsg_flags) 18316771bfd9SFlorian Westphal { 1832ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18336771bfd9SFlorian Westphal int copied = 0; 18346771bfd9SFlorian Westphal 1835ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18366771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18376771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18386771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18396771bfd9SFlorian Westphal int err; 18406771bfd9SFlorian Westphal 1841d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18426771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18436771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18446771bfd9SFlorian Westphal if (!copied) 18456771bfd9SFlorian Westphal return err; 18466771bfd9SFlorian Westphal break; 18476771bfd9SFlorian Westphal } 1848d976092cSPaolo Abeni } 18496771bfd9SFlorian Westphal 1850b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1851b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1852b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1853b7f653b2SFlorian Westphal } 1854b7f653b2SFlorian Westphal 18556771bfd9SFlorian Westphal copied += count; 18566771bfd9SFlorian Westphal 18576771bfd9SFlorian Westphal if (count < data_len) { 18582c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18596771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18602c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18612c9e7765SFlorian Westphal } 18626771bfd9SFlorian Westphal break; 18636771bfd9SFlorian Westphal } 18646771bfd9SFlorian Westphal 1865ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 186687952603SPaolo Abeni /* we will bulk release the skb memory later */ 186787952603SPaolo Abeni skb->destructor = NULL; 1868ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 186987952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18706771bfd9SFlorian Westphal __kfree_skb(skb); 1871ca4fb892SYonglong Li } 18726771bfd9SFlorian Westphal 18736771bfd9SFlorian Westphal if (copied >= len) 18746771bfd9SFlorian Westphal break; 18756771bfd9SFlorian Westphal } 18766771bfd9SFlorian Westphal 18776771bfd9SFlorian Westphal return copied; 18786771bfd9SFlorian Westphal } 18796771bfd9SFlorian Westphal 1880a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1881a6b118feSFlorian Westphal * 1882a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1883a6b118feSFlorian Westphal */ 1884a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1885a6b118feSFlorian Westphal { 1886a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1887a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1888a6b118feSFlorian Westphal u32 time, advmss = 1; 1889a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1890a6b118feSFlorian Westphal 1891109cdeb8SGeliang Tang msk_owned_by_me(msk); 1892a6b118feSFlorian Westphal 1893a6b118feSFlorian Westphal if (copied <= 0) 1894a6b118feSFlorian Westphal return; 1895a6b118feSFlorian Westphal 1896a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1897a6b118feSFlorian Westphal 1898a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1899a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1900a6b118feSFlorian Westphal 1901a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1902a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1903a6b118feSFlorian Westphal return; 1904a6b118feSFlorian Westphal 1905a6b118feSFlorian Westphal rtt_us = 0; 1906a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1907a6b118feSFlorian Westphal const struct tcp_sock *tp; 1908a6b118feSFlorian Westphal u64 sf_rtt_us; 1909a6b118feSFlorian Westphal u32 sf_advmss; 1910a6b118feSFlorian Westphal 1911a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1912a6b118feSFlorian Westphal 1913a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1914a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1915a6b118feSFlorian Westphal 1916a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1917a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1918a6b118feSFlorian Westphal } 1919a6b118feSFlorian Westphal 1920a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1921a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1922a6b118feSFlorian Westphal return; 1923a6b118feSFlorian Westphal 1924a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1925a6b118feSFlorian Westphal goto new_measure; 1926a6b118feSFlorian Westphal 192778047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1928a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1929a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1930a6b118feSFlorian Westphal u64 rcvwin, grow; 1931a6b118feSFlorian Westphal 1932a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1933a6b118feSFlorian Westphal 1934a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1935a6b118feSFlorian Westphal 1936a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1937a6b118feSFlorian Westphal rcvwin += (grow << 1); 1938a6b118feSFlorian Westphal 1939a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1940a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1941a6b118feSFlorian Westphal rcvmem += 128; 1942a6b118feSFlorian Westphal 1943a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1944a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 194502739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1946a6b118feSFlorian Westphal 1947a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1948a6b118feSFlorian Westphal u32 window_clamp; 1949a6b118feSFlorian Westphal 1950a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1951a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1952a6b118feSFlorian Westphal 1953a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1954a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1955a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1956a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1957a6b118feSFlorian Westphal */ 1958a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1959a6b118feSFlorian Westphal struct sock *ssk; 1960c76c6956SPaolo Abeni bool slow; 1961a6b118feSFlorian Westphal 1962a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1963c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1964a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1965a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1966c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1967c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1968a6b118feSFlorian Westphal } 1969a6b118feSFlorian Westphal } 1970a6b118feSFlorian Westphal } 1971a6b118feSFlorian Westphal 1972a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1973a6b118feSFlorian Westphal new_measure: 1974a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1975a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1976a6b118feSFlorian Westphal } 1977a6b118feSFlorian Westphal 197887952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 197987952603SPaolo Abeni { 198087952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 198187952603SPaolo Abeni 198287952603SPaolo Abeni if (!msk->rmem_released) 198387952603SPaolo Abeni return; 198487952603SPaolo Abeni 198587952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19866511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1987ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 198887952603SPaolo Abeni } 198987952603SPaolo Abeni 199087952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 199187952603SPaolo Abeni { 199287952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 199387952603SPaolo Abeni 199487952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 199587952603SPaolo Abeni } 199687952603SPaolo Abeni 1997e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19986771bfd9SFlorian Westphal { 199987952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20006771bfd9SFlorian Westphal unsigned int moved = 0; 200187952603SPaolo Abeni bool ret, done; 2002d5f49190SPaolo Abeni 20036771bfd9SFlorian Westphal do { 20046771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 200565f49fe7SFlorian Westphal bool slowpath; 20066771bfd9SFlorian Westphal 200787952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 200887952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 200987952603SPaolo Abeni * that is an unlikely slow path. 201087952603SPaolo Abeni */ 201187952603SPaolo Abeni if (likely(!ssk)) 20126771bfd9SFlorian Westphal break; 20136771bfd9SFlorian Westphal 201465f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 201587952603SPaolo Abeni mptcp_data_lock(sk); 2016e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20176771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 201887952603SPaolo Abeni mptcp_data_unlock(sk); 2019499ada50SPaolo Abeni 2020499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2021499ada50SPaolo Abeni __mptcp_error_report(sk); 202265f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20236771bfd9SFlorian Westphal } while (!done); 20246771bfd9SFlorian Westphal 202587952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 202687952603SPaolo Abeni ret = moved > 0; 202787952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 202887952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 202987952603SPaolo Abeni mptcp_data_lock(sk); 203087952603SPaolo Abeni __mptcp_update_rmem(sk); 203187952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 203287952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 203387952603SPaolo Abeni mptcp_data_unlock(sk); 2034ab174ad8SPaolo Abeni } 203587952603SPaolo Abeni if (ret) 203687952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 203787952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20386771bfd9SFlorian Westphal } 20396771bfd9SFlorian Westphal 20402c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20412c9e7765SFlorian Westphal { 20422c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20432c9e7765SFlorian Westphal const struct sk_buff *skb; 20442c9e7765SFlorian Westphal 20452c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20462c9e7765SFlorian Westphal if (skb) { 20472c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20482c9e7765SFlorian Westphal 20492c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20502c9e7765SFlorian Westphal return INT_MAX; 20512c9e7765SFlorian Westphal 20522c9e7765SFlorian Westphal return (unsigned int)hint_val; 20532c9e7765SFlorian Westphal } 20542c9e7765SFlorian Westphal 20552c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20562c9e7765SFlorian Westphal return 1; 20572c9e7765SFlorian Westphal 20582c9e7765SFlorian Westphal return 0; 20592c9e7765SFlorian Westphal } 20602c9e7765SFlorian Westphal 2061f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2062ec095263SOliver Hartkopp int flags, int *addr_len) 2063f870fa0bSMat Martineau { 2064f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2065b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2066b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20677a6a6cbcSPaolo Abeni int target; 20687a6a6cbcSPaolo Abeni long timeo; 2069f870fa0bSMat Martineau 2070cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2071cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2072cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2073cb9d80f4SPaolo Abeni 20746511882cSPaolo Abeni lock_sock(sk); 2075fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2076fd897679SPaolo Abeni copied = -ENOTCONN; 2077fd897679SPaolo Abeni goto out_err; 2078fd897679SPaolo Abeni } 2079fd897679SPaolo Abeni 2080ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20817a6a6cbcSPaolo Abeni 20827a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20837a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20847a6a6cbcSPaolo Abeni 20852c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20862c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20872c9e7765SFlorian Westphal 208805e3eceaSEric Dumazet while (copied < len) { 2089e3859603SPaolo Abeni int bytes_read; 20907a6a6cbcSPaolo Abeni 2091b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20926771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20937a6a6cbcSPaolo Abeni if (!copied) 20947a6a6cbcSPaolo Abeni copied = bytes_read; 20956771bfd9SFlorian Westphal goto out_err; 2096cec37a6eSPeter Krystad } 2097cec37a6eSPeter Krystad 20987a6a6cbcSPaolo Abeni copied += bytes_read; 20997a6a6cbcSPaolo Abeni 2100ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2101fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2102ea4ca586SPaolo Abeni 2103e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2104e3859603SPaolo Abeni continue; 2105e3859603SPaolo Abeni 21067a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21077a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21087a6a6cbcSPaolo Abeni */ 21097a6a6cbcSPaolo Abeni if (copied >= target) 21107a6a6cbcSPaolo Abeni break; 21117a6a6cbcSPaolo Abeni 21127a6a6cbcSPaolo Abeni if (copied) { 21137a6a6cbcSPaolo Abeni if (sk->sk_err || 21147a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21157a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21167a6a6cbcSPaolo Abeni !timeo || 21177a6a6cbcSPaolo Abeni signal_pending(current)) 21187a6a6cbcSPaolo Abeni break; 21197a6a6cbcSPaolo Abeni } else { 21207a6a6cbcSPaolo Abeni if (sk->sk_err) { 21217a6a6cbcSPaolo Abeni copied = sock_error(sk); 21227a6a6cbcSPaolo Abeni break; 21237a6a6cbcSPaolo Abeni } 21247a6a6cbcSPaolo Abeni 21255969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 21265969856aSPaolo Abeni mptcp_check_for_eof(msk); 21275969856aSPaolo Abeni 212887952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 212987952603SPaolo Abeni /* race breaker: the shutdown could be after the 213087952603SPaolo Abeni * previous receive queue check 213187952603SPaolo Abeni */ 2132e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 213387952603SPaolo Abeni continue; 21347a6a6cbcSPaolo Abeni break; 213587952603SPaolo Abeni } 21367a6a6cbcSPaolo Abeni 21377a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21387a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21397a6a6cbcSPaolo Abeni break; 21407a6a6cbcSPaolo Abeni } 21417a6a6cbcSPaolo Abeni 21427a6a6cbcSPaolo Abeni if (!timeo) { 21437a6a6cbcSPaolo Abeni copied = -EAGAIN; 21447a6a6cbcSPaolo Abeni break; 21457a6a6cbcSPaolo Abeni } 21467a6a6cbcSPaolo Abeni 21477a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21487a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21497a6a6cbcSPaolo Abeni break; 21507a6a6cbcSPaolo Abeni } 21517a6a6cbcSPaolo Abeni } 21527a6a6cbcSPaolo Abeni 21537a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2154612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21557a6a6cbcSPaolo Abeni } 21563c90e377SPaolo Abeni 21576771bfd9SFlorian Westphal out_err: 2158b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2159b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2160b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21612c9e7765SFlorian Westphal 21622c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21632c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21642c9e7765SFlorian Westphal 21652c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21662c9e7765SFlorian Westphal } 2167b7f653b2SFlorian Westphal } 2168b7f653b2SFlorian Westphal 2169612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2170612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2171612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2172ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2173a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2174a6b118feSFlorian Westphal 2175cec37a6eSPeter Krystad release_sock(sk); 2176cec37a6eSPeter Krystad return copied; 2177cec37a6eSPeter Krystad } 2178cec37a6eSPeter Krystad 2179b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2180b51f9b80SPaolo Abeni { 2181b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2182b51f9b80SPaolo Abeni icsk_retransmit_timer); 2183b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21842d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2185b51f9b80SPaolo Abeni 2186b51f9b80SPaolo Abeni bh_lock_sock(sk); 2187b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21882d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21892d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21902d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2191b51f9b80SPaolo Abeni } else { 2192b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2193e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2194b51f9b80SPaolo Abeni } 2195b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2196b51f9b80SPaolo Abeni sock_put(sk); 2197b51f9b80SPaolo Abeni } 2198b51f9b80SPaolo Abeni 2199e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2200e16163b6SPaolo Abeni { 2201e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2202e16163b6SPaolo Abeni 2203e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2204b6d69fc8SFlorian Westphal sock_put(sk); 2205e16163b6SPaolo Abeni } 2206e16163b6SPaolo Abeni 22073b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22083b1d6210SPaolo Abeni * level. 22093b1d6210SPaolo Abeni * 22103b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22113b1d6210SPaolo Abeni */ 2212ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22133b1d6210SPaolo Abeni { 221471b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22153b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 221671b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22173b1d6210SPaolo Abeni 2218109cdeb8SGeliang Tang msk_owned_by_me(msk); 22193b1d6210SPaolo Abeni 2220d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2221d9ca1de8SPaolo Abeni return NULL; 2222d5f49190SPaolo Abeni 22233b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22243b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22253b1d6210SPaolo Abeni 2226ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2227d5f49190SPaolo Abeni continue; 2228d5f49190SPaolo Abeni 222971b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 223071b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 223171b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 223271b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2233860975c6SFlorian Westphal continue; 2234860975c6SFlorian Westphal } 22353b1d6210SPaolo Abeni 22363b1d6210SPaolo Abeni if (subflow->backup) { 22373b1d6210SPaolo Abeni if (!backup) 22383b1d6210SPaolo Abeni backup = ssk; 22393b1d6210SPaolo Abeni continue; 22403b1d6210SPaolo Abeni } 22413b1d6210SPaolo Abeni 224271b7dec2SPaolo Abeni if (!pick) 224371b7dec2SPaolo Abeni pick = ssk; 22443b1d6210SPaolo Abeni } 22453b1d6210SPaolo Abeni 224671b7dec2SPaolo Abeni if (pick) 224771b7dec2SPaolo Abeni return pick; 224871b7dec2SPaolo Abeni 224971b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 225071b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22513b1d6210SPaolo Abeni } 22523b1d6210SPaolo Abeni 225317aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 225417aee05dSFlorian Westphal { 225517aee05dSFlorian Westphal if (msk->subflow) { 225617aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 225717aee05dSFlorian Westphal msk->subflow = NULL; 225817aee05dSFlorian Westphal } 225917aee05dSFlorian Westphal } 226017aee05dSFlorian Westphal 22611e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22621e1d9d6fSPaolo Abeni { 22631e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22641e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22651e1d9d6fSPaolo Abeni 226600df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 22671e1d9d6fSPaolo Abeni return false; 22681e1d9d6fSPaolo Abeni 22691e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22701e1d9d6fSPaolo Abeni return false; 22711e1d9d6fSPaolo Abeni 22721e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22731e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22741e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22751e1d9d6fSPaolo Abeni */ 22761e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22771e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22781e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22791e1d9d6fSPaolo Abeni if (!rtx_head) { 22801e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22811e1d9d6fSPaolo Abeni return false; 22821e1d9d6fSPaolo Abeni } 22831e1d9d6fSPaolo Abeni 22841e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22851e1d9d6fSPaolo Abeni msk->recovery = true; 22861e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22871e1d9d6fSPaolo Abeni 22881e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22891e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22901e1d9d6fSPaolo Abeni 22911e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22921e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22931e1d9d6fSPaolo Abeni if (!cur->already_sent) 22941e1d9d6fSPaolo Abeni break; 22951e1d9d6fSPaolo Abeni cur->already_sent = 0; 22961e1d9d6fSPaolo Abeni } 22971e1d9d6fSPaolo Abeni 22981e1d9d6fSPaolo Abeni return true; 22991e1d9d6fSPaolo Abeni } 23001e1d9d6fSPaolo Abeni 2301b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2302b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2303b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2304b29fcfb5SPaolo Abeni 2305cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2306cec37a6eSPeter Krystad * (accept). 2307cec37a6eSPeter Krystad * 2308cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2309cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2310cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2311cec37a6eSPeter Krystad * parent socket. 2312cec37a6eSPeter Krystad */ 2313a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2314b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2315b29fcfb5SPaolo Abeni unsigned int flags) 2316cec37a6eSPeter Krystad { 2317e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2318b29fcfb5SPaolo Abeni bool need_push, dispose_it; 2319e0be4931SFlorian Westphal 2320b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2321b29fcfb5SPaolo Abeni if (dispose_it) 2322cec37a6eSPeter Krystad list_del(&subflow->node); 2323cec37a6eSPeter Krystad 23243f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2325e16163b6SPaolo Abeni 2326d21f8348SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) { 2327d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2328d21f8348SPaolo Abeni * to generate the egress reset 2329d21f8348SPaolo Abeni */ 2330d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2331d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2332b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2333d21f8348SPaolo Abeni } 2334b29fcfb5SPaolo Abeni 2335b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2336b29fcfb5SPaolo Abeni if (!dispose_it) { 2337b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2338b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2339b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2340b29fcfb5SPaolo Abeni release_sock(ssk); 2341b29fcfb5SPaolo Abeni 2342b29fcfb5SPaolo Abeni goto out; 2343b29fcfb5SPaolo Abeni } 2344b29fcfb5SPaolo Abeni 2345d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2346d7b1bfd0SPaolo Abeni 2347e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2348e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2349e16163b6SPaolo Abeni * reference owned by msk; 2350e16163b6SPaolo Abeni */ 2351e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2352b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2353e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2354b6985b9bSPaolo Abeni } else if (msk->in_accept_queue && msk->first == ssk) { 2355b6985b9bSPaolo Abeni /* if the first subflow moved to a close state, e.g. due to 2356b6985b9bSPaolo Abeni * incoming reset and we reach here before inet_child_forget() 2357b6985b9bSPaolo Abeni * the TCP stack could later try to close it via 2358b6985b9bSPaolo Abeni * inet_csk_listen_stop(), or deliver it to the user space via 2359b6985b9bSPaolo Abeni * accept(). 2360b6985b9bSPaolo Abeni * We can't delete the subflow - or risk a double free - nor let 2361b6985b9bSPaolo Abeni * the msk survive - or will be leaked in the non accept scenario: 2362b6985b9bSPaolo Abeni * fallback and let TCP cope with the subflow cleanup. 2363b6985b9bSPaolo Abeni */ 2364b6985b9bSPaolo Abeni WARN_ON_ONCE(sock_flag(ssk, SOCK_DEAD)); 2365b6985b9bSPaolo Abeni mptcp_subflow_drop_ctx(ssk); 2366e16163b6SPaolo Abeni } else { 2367d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2368*2a6a870eSPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 2369*2a6a870eSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 2370*2a6a870eSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 2371*2a6a870eSPaolo Abeni inet_csk_listen_stop(ssk); 2372f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 2373*2a6a870eSPaolo Abeni } 23740a3f4f1fSPaolo Abeni 2375e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2376e16163b6SPaolo Abeni 2377e16163b6SPaolo Abeni /* close acquired an extra ref */ 2378e16163b6SPaolo Abeni __sock_put(ssk); 2379e16163b6SPaolo Abeni } 2380e16163b6SPaolo Abeni release_sock(ssk); 2381e16163b6SPaolo Abeni 2382e16163b6SPaolo Abeni sock_put(ssk); 2383e0be4931SFlorian Westphal 2384c8fe62f0SFlorian Westphal if (ssk == msk->first) 2385c8fe62f0SFlorian Westphal msk->first = NULL; 2386c8fe62f0SFlorian Westphal 2387b29fcfb5SPaolo Abeni out: 2388b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2389b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23901e1d9d6fSPaolo Abeni 23911e1d9d6fSPaolo Abeni if (need_push) 23921e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2393f870fa0bSMat Martineau } 2394f870fa0bSMat Martineau 2395a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2396a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2397a141e02eSFlorian Westphal { 2398b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2399b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2400a88c9e49SPaolo Abeni 2401a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2402a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2403a88c9e49SPaolo Abeni */ 2404a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2405a88c9e49SPaolo Abeni 2406b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2407a141e02eSFlorian Westphal } 2408a141e02eSFlorian Westphal 2409dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2410dc24f8b4SPaolo Abeni { 2411dc24f8b4SPaolo Abeni return 0; 2412dc24f8b4SPaolo Abeni } 2413dc24f8b4SPaolo Abeni 2414b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24150e4f35d7SPaolo Abeni { 24160e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2417b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24180e4f35d7SPaolo Abeni 24193abc05d9SFlorian Westphal might_sleep(); 24203abc05d9SFlorian Westphal 24215efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24220e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24230e4f35d7SPaolo Abeni 24240e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24250e4f35d7SPaolo Abeni continue; 24260e4f35d7SPaolo Abeni 242740947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 242840947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 242940947e13SFlorian Westphal continue; 243040947e13SFlorian Westphal 2431b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2432b6985b9bSPaolo Abeni } 2433b6985b9bSPaolo Abeni 2434b6985b9bSPaolo Abeni /* if the MPC subflow has been closed before the msk is accepted, 2435b6985b9bSPaolo Abeni * msk will never be accept-ed, close it now 2436b6985b9bSPaolo Abeni */ 2437b6985b9bSPaolo Abeni if (!msk->first && msk->in_accept_queue) { 2438b6985b9bSPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 2439b6985b9bSPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 24400e4f35d7SPaolo Abeni } 24410e4f35d7SPaolo Abeni } 24420e4f35d7SPaolo Abeni 2443e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2444e16163b6SPaolo Abeni { 2445e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2446e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2447e16163b6SPaolo Abeni 2448e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2449e16163b6SPaolo Abeni return true; 2450e16163b6SPaolo Abeni 2451e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2452e16163b6SPaolo Abeni * timeout 2453e16163b6SPaolo Abeni */ 2454e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2455e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2456e16163b6SPaolo Abeni TCP_CLOSE) 2457e16163b6SPaolo Abeni return false; 2458e16163b6SPaolo Abeni } 2459e16163b6SPaolo Abeni return true; 2460e16163b6SPaolo Abeni } 2461e16163b6SPaolo Abeni 246250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 246350c504a2SFlorian Westphal { 246450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 246580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 246650c504a2SFlorian Westphal 246750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 246850c504a2SFlorian Westphal return; 246950c504a2SFlorian Westphal 247050c504a2SFlorian Westphal mptcp_token_destroy(msk); 247150c504a2SFlorian Westphal 24725efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 247350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 247475e908c3SPaolo Abeni bool slow; 247550c504a2SFlorian Westphal 247675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 247750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 247850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 247950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 248050c504a2SFlorian Westphal } 248175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 248250c504a2SFlorian Westphal } 248350c504a2SFlorian Westphal 248469800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 248569800e51SPaolo Abeni switch (sk->sk_state) { 248669800e51SPaolo Abeni case TCP_SYN_SENT: 248769800e51SPaolo Abeni sk->sk_err = ECONNREFUSED; 248869800e51SPaolo Abeni break; 248969800e51SPaolo Abeni case TCP_CLOSE_WAIT: 249069800e51SPaolo Abeni sk->sk_err = EPIPE; 249169800e51SPaolo Abeni break; 249269800e51SPaolo Abeni case TCP_CLOSE: 249369800e51SPaolo Abeni return; 249469800e51SPaolo Abeni default: 249569800e51SPaolo Abeni sk->sk_err = ECONNRESET; 249669800e51SPaolo Abeni } 249769800e51SPaolo Abeni 249850c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 249950c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 250050c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 250150c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 250250c504a2SFlorian Westphal 250369800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 250469800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 250569800e51SPaolo Abeni return; 250669800e51SPaolo Abeni 250769800e51SPaolo Abeni sk->sk_state_change(sk); 250869800e51SPaolo Abeni sk_error_report(sk); 250950c504a2SFlorian Westphal } 251050c504a2SFlorian Westphal 25112948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 251280992017SPaolo Abeni { 25132948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2514caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25153b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25163b1d6210SPaolo Abeni size_t copied = 0; 25172948d0a1SPaolo Abeni struct sock *ssk; 25182948d0a1SPaolo Abeni int ret; 25192948d0a1SPaolo Abeni 2520b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25213241a9c0SFlorian Westphal 25223241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25233241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25242948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25256477dd39SMat Martineau if (!dfrag) { 25266477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25276477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25286477dd39SMat Martineau 25296477dd39SMat Martineau icsk->icsk_retransmits++; 25306477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25316477dd39SMat Martineau mptcp_send_ack(msk); 25326477dd39SMat Martineau 25336477dd39SMat Martineau goto reset_timer; 25346477dd39SMat Martineau } 25356477dd39SMat Martineau 25363241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25372948d0a1SPaolo Abeni return; 25383241a9c0SFlorian Westphal 25393241a9c0SFlorian Westphal goto reset_timer; 25406477dd39SMat Martineau } 25412948d0a1SPaolo Abeni 25422948d0a1SPaolo Abeni if (!ssk) 25432948d0a1SPaolo Abeni goto reset_timer; 25442948d0a1SPaolo Abeni 25452948d0a1SPaolo Abeni lock_sock(ssk); 25462948d0a1SPaolo Abeni 25472948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25482948d0a1SPaolo Abeni info.sent = 0; 25494e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25504e14867dSPaolo Abeni while (info.sent < info.limit) { 25512948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25522948d0a1SPaolo Abeni if (ret <= 0) 25532948d0a1SPaolo Abeni break; 25542948d0a1SPaolo Abeni 25552948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25562948d0a1SPaolo Abeni copied += ret; 25572948d0a1SPaolo Abeni info.sent += ret; 25582948d0a1SPaolo Abeni } 25594e14867dSPaolo Abeni if (copied) { 25604e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 25612948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 25622948d0a1SPaolo Abeni info.size_goal); 25630530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 25644e14867dSPaolo Abeni } 25652948d0a1SPaolo Abeni 25662948d0a1SPaolo Abeni release_sock(ssk); 25672948d0a1SPaolo Abeni 25682948d0a1SPaolo Abeni reset_timer: 25693241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 25703241a9c0SFlorian Westphal 25712948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 25722948d0a1SPaolo Abeni mptcp_reset_timer(sk); 25732948d0a1SPaolo Abeni } 25742948d0a1SPaolo Abeni 257576a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 257676a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 257776a13b31SGeliang Tang */ 257876a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 257976a13b31SGeliang Tang { 258076a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 258176a13b31SGeliang Tang unsigned long timeout, close_timeout; 258276a13b31SGeliang Tang 258376a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 258476a13b31SGeliang Tang return; 258576a13b31SGeliang Tang 258676a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 258776a13b31SGeliang Tang 258876a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 258976a13b31SGeliang Tang * them is active 259076a13b31SGeliang Tang */ 259176a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 259276a13b31SGeliang Tang 259376a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 259476a13b31SGeliang Tang } 259576a13b31SGeliang Tang 259649fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 259749fa1919SGeliang Tang { 259876a13b31SGeliang Tang struct sock *ssk = msk->first; 259949fa1919SGeliang Tang bool slow; 260049fa1919SGeliang Tang 260176a13b31SGeliang Tang if (!ssk) 260276a13b31SGeliang Tang return; 260376a13b31SGeliang Tang 260449fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 260549fa1919SGeliang Tang 260649fa1919SGeliang Tang slow = lock_sock_fast(ssk); 260749fa1919SGeliang Tang mptcp_subflow_reset(ssk); 260876a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 260949fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 261076a13b31SGeliang Tang 261176a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 261249fa1919SGeliang Tang } 261349fa1919SGeliang Tang 2614d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2615d21f8348SPaolo Abeni { 2616d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2617d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2618d21f8348SPaolo Abeni 2619d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2620d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2621d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2622d21f8348SPaolo Abeni } 2623d21f8348SPaolo Abeni 26242948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26252948d0a1SPaolo Abeni { 26262948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 262780638684SGeliang Tang struct sock *sk = (struct sock *)msk; 262876a13b31SGeliang Tang unsigned long fail_tout; 26292948d0a1SPaolo Abeni int state; 263080992017SPaolo Abeni 263180992017SPaolo Abeni lock_sock(sk); 2632e16163b6SPaolo Abeni state = sk->sk_state; 2633d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2634e16163b6SPaolo Abeni goto unlock; 2635e16163b6SPaolo Abeni 263643b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 263750c504a2SFlorian Westphal 263850c504a2SFlorian Westphal mptcp_check_fastclose(msk); 263950c504a2SFlorian Westphal 2640e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2641b416268bSFlorian Westphal 264259832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 264359832e24SFlorian Westphal mptcp_check_for_eof(msk); 264459832e24SFlorian Westphal 26456e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 264643b54c6eSMat Martineau mptcp_check_data_fin(sk); 264743b54c6eSMat Martineau 2648b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2649b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2650b6985b9bSPaolo Abeni 2651341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2652341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2653341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2654e16163b6SPaolo Abeni */ 2655d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 2656d21f8348SPaolo Abeni if (mptcp_check_close_timeout(sk)) { 2657e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2658d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2659d21f8348SPaolo Abeni } 2660d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2661e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2662e16163b6SPaolo Abeni goto unlock; 2663e16163b6SPaolo Abeni } 2664d21f8348SPaolo Abeni } 2665e16163b6SPaolo Abeni 26662948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 26672948d0a1SPaolo Abeni __mptcp_retrans(sk); 26683b1d6210SPaolo Abeni 266976a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 267076a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 267149fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 267249fa1919SGeliang Tang 26733b1d6210SPaolo Abeni unlock: 267480992017SPaolo Abeni release_sock(sk); 267580992017SPaolo Abeni sock_put(sk); 267680992017SPaolo Abeni } 267780992017SPaolo Abeni 2678784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2679f870fa0bSMat Martineau { 2680cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2681cec37a6eSPeter Krystad 2682cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2683ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 268418b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 268580992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 268687952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2687ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2688f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 26896511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2690ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 269133d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2692cec37a6eSPeter Krystad 26938ab183deSPaolo Abeni msk->first = NULL; 2694dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2695752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 26960530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 26971e1d9d6fSPaolo Abeni msk->recovery = false; 26988ab183deSPaolo Abeni 26991b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27001b1c7a0eSPeter Krystad 2701b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2702b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2703e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2704aa1fbd94SFlorian Westphal 2705f870fa0bSMat Martineau return 0; 2706f870fa0bSMat Martineau } 2707f870fa0bSMat Martineau 2708b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2709784325e9SMatthieu Baerts { 271020b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2711b29fcfb5SPaolo Abeni 2712b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2713b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2714b29fcfb5SPaolo Abeni 2715b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2716b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2717b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2718b29fcfb5SPaolo Abeni } 2719b29fcfb5SPaolo Abeni 2720b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2721b29fcfb5SPaolo Abeni { 2722fc518953SFlorian Westphal struct net *net = sock_net(sk); 2723fc518953SFlorian Westphal int ret; 272418b683bfSPaolo Abeni 2725b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2726b6c08380SGeliang Tang if (ret) 2727b6c08380SGeliang Tang return ret; 2728b6c08380SGeliang Tang 2729fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2730fc518953SFlorian Westphal return -ENOPROTOOPT; 2731fc518953SFlorian Westphal 2732fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2733fc518953SFlorian Westphal return -ENOMEM; 2734fc518953SFlorian Westphal 2735fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2736fa68018dSPaolo Abeni if (ret) 2737fa68018dSPaolo Abeni return ret; 2738fa68018dSPaolo Abeni 2739a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2740a5ef058dSPaolo Abeni 274120b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 274220b5759fSPaolo Abeni * propagate the correct value 274320b5759fSPaolo Abeni */ 2744b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 274520b5759fSPaolo Abeni 2746d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2747a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2748a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2749d027236cSPaolo Abeni 275018b683bfSPaolo Abeni return 0; 275118b683bfSPaolo Abeni } 275218b683bfSPaolo Abeni 275318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 275418b683bfSPaolo Abeni { 275518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 275618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 275718b683bfSPaolo Abeni 2758d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 275918b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2760d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2761784325e9SMatthieu Baerts } 2762784325e9SMatthieu Baerts 276330e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 276480992017SPaolo Abeni { 276580992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 276680992017SPaolo Abeni 2767b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2768e16163b6SPaolo Abeni __sock_put(sk); 276980992017SPaolo Abeni } 277080992017SPaolo Abeni 2771d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 277221498490SPeter Krystad { 277321498490SPeter Krystad lock_sock(ssk); 277421498490SPeter Krystad 277521498490SPeter Krystad switch (ssk->sk_state) { 277621498490SPeter Krystad case TCP_LISTEN: 277721498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 277821498490SPeter Krystad break; 2779df561f66SGustavo A. R. Silva fallthrough; 278021498490SPeter Krystad case TCP_SYN_SENT: 278121498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 278221498490SPeter Krystad break; 278321498490SPeter Krystad default: 278443b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 278543b54c6eSMat Martineau pr_debug("Fallback"); 278621498490SPeter Krystad ssk->sk_shutdown |= how; 278721498490SPeter Krystad tcp_shutdown(ssk, how); 278843b54c6eSMat Martineau } else { 278943b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 279043b54c6eSMat Martineau tcp_send_ack(ssk); 27916477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 27926477dd39SMat Martineau mptcp_reset_timer(sk); 279343b54c6eSMat Martineau } 279421498490SPeter Krystad break; 279521498490SPeter Krystad } 279621498490SPeter Krystad 279721498490SPeter Krystad release_sock(ssk); 279821498490SPeter Krystad } 279921498490SPeter Krystad 28006920b851SMat Martineau static const unsigned char new_state[16] = { 28016920b851SMat Martineau /* current state: new state: action: */ 28026920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 28036920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28046920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 28056920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28066920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 28076920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 28086920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28096920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28106920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28116920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28126920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28136920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28146920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28156920b851SMat Martineau }; 28166920b851SMat Martineau 28176920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28186920b851SMat Martineau { 28196920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28206920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28216920b851SMat Martineau 28226920b851SMat Martineau inet_sk_state_store(sk, ns); 28236920b851SMat Martineau 28246920b851SMat Martineau return next & TCP_ACTION_FIN; 28256920b851SMat Martineau } 28266920b851SMat Martineau 2827e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2828e16163b6SPaolo Abeni { 2829e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2830e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2831e16163b6SPaolo Abeni 2832e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2833e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2834e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2835e16163b6SPaolo Abeni 2836e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2837e16163b6SPaolo Abeni * skip this 2838e16163b6SPaolo Abeni */ 2839e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2840e16163b6SPaolo Abeni mptcp_send_head(sk)) 2841e16163b6SPaolo Abeni return; 2842e16163b6SPaolo Abeni 2843e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2844e16163b6SPaolo Abeni 284526aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 284626aa2314SPaolo Abeni * state now 284726aa2314SPaolo Abeni */ 284826aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 284958cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 285026aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2851e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2852e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 285326aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 285426aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 285526aa2314SPaolo Abeni } 2856e16163b6SPaolo Abeni } 2857e16163b6SPaolo Abeni 2858e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2859e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2860e16163b6SPaolo Abeni 2861e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2862e16163b6SPaolo Abeni } 2863e16163b6SPaolo Abeni } 2864e16163b6SPaolo Abeni 2865e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2866e16163b6SPaolo Abeni { 2867e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2868e16163b6SPaolo Abeni 2869e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2870e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2871e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2872e16163b6SPaolo Abeni 2873e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2874e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2875e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2876e16163b6SPaolo Abeni 2877e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2878e16163b6SPaolo Abeni } 2879e16163b6SPaolo Abeni 2880e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2881f870fa0bSMat Martineau { 2882f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2883f870fa0bSMat Martineau 2884e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 28852c22c06cSFlorian Westphal 28863abc05d9SFlorian Westphal might_sleep(); 28873abc05d9SFlorian Westphal 2888bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2889e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2890e16163b6SPaolo Abeni msk->pm.status = 0; 2891b2c5b614SFlorian Westphal 2892e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2893e16163b6SPaolo Abeni 28946511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 289587952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2896e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2897e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2898aa1fbd94SFlorian Westphal 2899e16163b6SPaolo Abeni sock_put(sk); 2900e16163b6SPaolo Abeni } 2901e16163b6SPaolo Abeni 2902d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2903d21f8348SPaolo Abeni { 2904d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2905d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2906d21f8348SPaolo Abeni */ 2907d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2908d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2909d21f8348SPaolo Abeni return 0; 2910d21f8348SPaolo Abeni 2911d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2912d21f8348SPaolo Abeni } 2913d21f8348SPaolo Abeni 2914c558246eSMenglong Dong static void mptcp_listen_inuse_dec(struct sock *sk) 2915c558246eSMenglong Dong { 2916c558246eSMenglong Dong if (inet_sk_state_load(sk) == TCP_LISTEN) 2917c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2918c558246eSMenglong Dong } 2919c558246eSMenglong Dong 292026d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2921e16163b6SPaolo Abeni { 2922e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 292376a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2924e16163b6SPaolo Abeni bool do_cancel_work = false; 2925d4e85922SPaolo Abeni int subflows_alive = 0; 2926e16163b6SPaolo Abeni 2927e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2928e16163b6SPaolo Abeni 2929e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2930c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 2931e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2932e16163b6SPaolo Abeni goto cleanup; 2933e16163b6SPaolo Abeni } 2934e16163b6SPaolo Abeni 2935d21f8348SPaolo Abeni if (mptcp_check_readable(msk)) { 2936d21f8348SPaolo Abeni /* the msk has read data, do the MPTCP equivalent of TCP reset */ 2937d21f8348SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2938d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2939d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2940e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2941d21f8348SPaolo Abeni } 2942e16163b6SPaolo Abeni 2943e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2944e16163b6SPaolo Abeni 2945e16163b6SPaolo Abeni cleanup: 2946e16163b6SPaolo Abeni /* orphan all the subflows */ 2947e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 294876a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2949e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 295049054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2951e16163b6SPaolo Abeni 2952d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 2953d4e85922SPaolo Abeni 295476a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 295576a13b31SGeliang Tang * cancel the latter 295676a13b31SGeliang Tang */ 295776a13b31SGeliang Tang if (ssk == msk->first) 295876a13b31SGeliang Tang subflow->fail_tout = 0; 295976a13b31SGeliang Tang 2960fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 2961fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 2962fe948001SMenglong Dong */ 2963fe948001SMenglong Dong ssk->sk_socket = NULL; 2964fe948001SMenglong Dong ssk->sk_wq = NULL; 2965e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2966e16163b6SPaolo Abeni } 2967e16163b6SPaolo Abeni sock_orphan(sk); 2968e16163b6SPaolo Abeni 2969d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 2970d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 2971d4e85922SPaolo Abeni */ 2972d4e85922SPaolo Abeni if (subflows_alive == 0) 2973d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2974d4e85922SPaolo Abeni 2975e16163b6SPaolo Abeni sock_hold(sk); 2976e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 297700df24f1SGeliang Tang if (msk->token) 297876a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2979b29fcfb5SPaolo Abeni 2980e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2981e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2982e16163b6SPaolo Abeni do_cancel_work = true; 2983e16163b6SPaolo Abeni } else { 298476a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2985e16163b6SPaolo Abeni } 298626d3e21cSMenglong Dong 298726d3e21cSMenglong Dong return do_cancel_work; 298826d3e21cSMenglong Dong } 298926d3e21cSMenglong Dong 299026d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 299126d3e21cSMenglong Dong { 299226d3e21cSMenglong Dong bool do_cancel_work; 299326d3e21cSMenglong Dong 299426d3e21cSMenglong Dong lock_sock(sk); 299526d3e21cSMenglong Dong 299626d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 2997e16163b6SPaolo Abeni release_sock(sk); 2998e16163b6SPaolo Abeni if (do_cancel_work) 299980992017SPaolo Abeni mptcp_cancel_work(sk); 3000b911c97cSFlorian Westphal 3001e16163b6SPaolo Abeni sock_put(sk); 3002f870fa0bSMat Martineau } 3003f870fa0bSMat Martineau 3004e72e4032SPaolo Abeni void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3005cf7da0d6SPeter Krystad { 3006cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3007cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3008cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3009cf7da0d6SPeter Krystad 3010cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3011cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3012cf7da0d6SPeter Krystad 3013cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3014cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3015cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3016cf7da0d6SPeter Krystad } 3017cf7da0d6SPeter Krystad #endif 3018cf7da0d6SPeter Krystad 3019cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3020cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3021cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3022cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3023cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3024cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3025cf7da0d6SPeter Krystad } 3026cf7da0d6SPeter Krystad 302718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 302818b683bfSPaolo Abeni { 302976e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 303076e2a55dSPaolo Abeni 30317d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 30327d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 30337d803344SPaolo Abeni * msk->firstsocket lock). Do nothing and leave the cleanup to the 30347d803344SPaolo Abeni * caller. 30357d803344SPaolo Abeni */ 30367d803344SPaolo Abeni if (msk->fastopening) 30377d803344SPaolo Abeni return 0; 30387d803344SPaolo Abeni 3039c558246eSMenglong Dong mptcp_listen_inuse_dec(sk); 3040b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3041b29fcfb5SPaolo Abeni 3042bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3043b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3044b29fcfb5SPaolo Abeni 304500df24f1SGeliang Tang if (msk->token) 304600df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3047b29fcfb5SPaolo Abeni 3048c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3049c0bf3c6aSPaolo Abeni * subflow 3050c0bf3c6aSPaolo Abeni */ 3051c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3052b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3053e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3054e9d09bacSPaolo Abeni msk->cb_flags = 0; 3055e9d09bacSPaolo Abeni msk->push_pending = 0; 3056b29fcfb5SPaolo Abeni msk->recovery = false; 3057b29fcfb5SPaolo Abeni msk->can_ack = false; 3058b29fcfb5SPaolo Abeni msk->fully_established = false; 3059b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3060b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3061b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3062b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3063b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3064b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3065b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 3066b29fcfb5SPaolo Abeni 3067b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 3068b29fcfb5SPaolo Abeni sk_error_report(sk); 306942c556feSFlorian Westphal return 0; 307018b683bfSPaolo Abeni } 307118b683bfSPaolo Abeni 3072b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3073b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3074b0519de8SFlorian Westphal { 3075b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3076b0519de8SFlorian Westphal 3077b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3078b0519de8SFlorian Westphal } 3079b0519de8SFlorian Westphal #endif 3080b0519de8SFlorian Westphal 3081fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 3082cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 3083fca5c82cSPaolo Abeni struct request_sock *req) 3084b0519de8SFlorian Westphal { 308558b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3086b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 308758b09919SPaolo Abeni struct mptcp_sock *msk; 3088b0519de8SFlorian Westphal 3089b0519de8SFlorian Westphal if (!nsk) 3090b0519de8SFlorian Westphal return NULL; 3091b0519de8SFlorian Westphal 3092b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3093b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3094b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3095b0519de8SFlorian Westphal #endif 3096b0519de8SFlorian Westphal 309758b09919SPaolo Abeni __mptcp_init_sock(nsk); 309858b09919SPaolo Abeni 309958b09919SPaolo Abeni msk = mptcp_sk(nsk); 310058b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 310158b09919SPaolo Abeni msk->token = subflow_req->token; 310258b09919SPaolo Abeni msk->subflow = NULL; 3103b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3104b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 310574c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 310606251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 310758b09919SPaolo Abeni 310858b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3109eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31107439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31117439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3112df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31136f8a612aSFlorian Westphal 31145e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31157f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 31167f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 31170c148460SPaolo Abeni 31180c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 311958b09919SPaolo Abeni bh_unlock_sock(nsk); 312058b09919SPaolo Abeni 3121b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3122b0519de8SFlorian Westphal return nsk; 3123b0519de8SFlorian Westphal } 3124b0519de8SFlorian Westphal 3125a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3126a6b118feSFlorian Westphal { 3127a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3128a6b118feSFlorian Westphal 3129a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3130a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3131a6b118feSFlorian Westphal 3132a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3133a6b118feSFlorian Westphal 3134a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3135a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3136a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3137a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3138a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 31396f8a612aSFlorian Westphal 31407439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3141a6b118feSFlorian Westphal } 3142a6b118feSFlorian Westphal 3143cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3144cf7da0d6SPeter Krystad bool kern) 3145cf7da0d6SPeter Krystad { 3146cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3147cf7da0d6SPeter Krystad struct socket *listener; 3148cf7da0d6SPeter Krystad struct sock *newsk; 3149cf7da0d6SPeter Krystad 3150cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 3151cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3152cf7da0d6SPeter Krystad *err = -EINVAL; 3153cf7da0d6SPeter Krystad return NULL; 3154cf7da0d6SPeter Krystad } 3155cf7da0d6SPeter Krystad 3156cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3157cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3158cf7da0d6SPeter Krystad if (!newsk) 3159cf7da0d6SPeter Krystad return NULL; 3160cf7da0d6SPeter Krystad 3161cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3162cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3163cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3164cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3165cf7da0d6SPeter Krystad 3166cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 316758b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 316858b09919SPaolo Abeni 316958b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 317058b09919SPaolo Abeni * subflow_syn_recv_sock() 317158b09919SPaolo Abeni */ 317258b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 317358b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3174d6692b3bSFlorian Westphal goto out; 317558b09919SPaolo Abeni } 317658b09919SPaolo Abeni 3177cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 31780397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3179fc518953SFlorian Westphal } else { 3180fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3181fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3182cf7da0d6SPeter Krystad } 3183cf7da0d6SPeter Krystad 3184d6692b3bSFlorian Westphal out: 3185d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3186cf7da0d6SPeter Krystad return newsk; 3187cf7da0d6SPeter Krystad } 3188cf7da0d6SPeter Krystad 3189c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 31905c8c1640SGeliang Tang { 3191c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 319287952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 319387952603SPaolo Abeni 31946e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 31956e628cd3SPaolo Abeni 3196c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 31975efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3198c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3199c0bf3c6aSPaolo Abeni 320087952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3201b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 320287952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32036511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32045c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3205b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32066511882cSPaolo Abeni 32076511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 32086511882cSPaolo Abeni * inet_sock_destruct() will dispose it 32096511882cSPaolo Abeni */ 32106511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 32116511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 32125c8c1640SGeliang Tang mptcp_token_destroy(msk); 32135c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 32144638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 32155c8c1640SGeliang Tang } 32165c8c1640SGeliang Tang 321779c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 321879c0949eSPeter Krystad { 3219c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3220c9fd9c5fSFlorian Westphal 3221c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3222c0bf3c6aSPaolo Abeni * even the initial subflow 3223c0bf3c6aSPaolo Abeni */ 3224c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3225c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3226d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 322779c0949eSPeter Krystad } 322879c0949eSPeter Krystad 32296e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32306e628cd3SPaolo Abeni { 32316e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32326e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32336e628cd3SPaolo Abeni else 3234e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 32356e628cd3SPaolo Abeni 32366e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 32376e628cd3SPaolo Abeni mptcp_schedule_work(sk); 32386e628cd3SPaolo Abeni } 32396e628cd3SPaolo Abeni 3240219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 32416e628cd3SPaolo Abeni { 32426e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 32436e628cd3SPaolo Abeni return; 32446e628cd3SPaolo Abeni 324573a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 324673a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 324773a0052aSGeliang Tang else 3248e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 32496e628cd3SPaolo Abeni } 32506e628cd3SPaolo Abeni 3251e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3252e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3253e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3254e9d09bacSPaolo Abeni 3255e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 325614c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3257e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 325814c441b5SPaolo Abeni { 3259e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 32602d6f5a2bSPaolo Abeni 3261e9d09bacSPaolo Abeni for (;;) { 3262e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3263e9d09bacSPaolo Abeni msk->push_pending; 3264c2e6048fSPaolo Abeni if (!flags) 3265c2e6048fSPaolo Abeni break; 3266c2e6048fSPaolo Abeni 3267c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 32686e628cd3SPaolo Abeni * 32696e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 32706e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 32716e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 32726e628cd3SPaolo Abeni * the subflow socket lock 32736e628cd3SPaolo Abeni */ 3274e9d09bacSPaolo Abeni msk->push_pending = 0; 3275e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 32766e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 32773e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 32783e501490SPaolo Abeni __mptcp_flush_join_list(sk); 32792e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3280c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 32812d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 32822d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3283c2e6048fSPaolo Abeni 3284c2e6048fSPaolo Abeni cond_resched(); 32856e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 32866e628cd3SPaolo Abeni } 3287c2e6048fSPaolo Abeni 328865a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 328965a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 329065a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3291490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 329265a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3293490274b4SPaolo Abeni */ 3294e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3295490274b4SPaolo Abeni __mptcp_set_connected(sk); 3296e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 329715cc1045SPaolo Abeni __mptcp_error_report(sk); 329843f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 329943f5b111SPaolo Abeni msk->last_snd = NULL; 330065a569b0SPaolo Abeni } 33016e628cd3SPaolo Abeni 330287952603SPaolo Abeni __mptcp_update_rmem(sk); 330314c441b5SPaolo Abeni } 330414c441b5SPaolo Abeni 3305bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3306bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3307bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3308bcd97734SPaolo Abeni */ 3309bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3310bcd97734SPaolo Abeni { 3311bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3312bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3313bcd97734SPaolo Abeni unsigned long timeout; 3314bcd97734SPaolo Abeni 3315bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3316bcd97734SPaolo Abeni return; 3317bcd97734SPaolo Abeni 3318bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3319bcd97734SPaolo Abeni if (tp->srtt_us) 3320bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3321bcd97734SPaolo Abeni else 3322bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3323bcd97734SPaolo Abeni timeout += jiffies; 3324bcd97734SPaolo Abeni 3325bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3326bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3327bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3328bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3329bcd97734SPaolo Abeni } 3330bcd97734SPaolo Abeni 3331b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3332b19bc294SPaolo Abeni { 3333b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3334b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3335b19bc294SPaolo Abeni 3336bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3337b19bc294SPaolo Abeni mptcp_data_lock(sk); 3338b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 333973a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3340b19bc294SPaolo Abeni else 3341e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3342b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3343bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3344bcd97734SPaolo Abeni } 3345bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3346bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3347bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3348bcd97734SPaolo Abeni } 3349b19bc294SPaolo Abeni } 3350b19bc294SPaolo Abeni 33512c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 33522c5ebd00SPaolo Abeni { 33532c5ebd00SPaolo Abeni /* should never be called, 33542c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 33552c5ebd00SPaolo Abeni */ 33562c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 33572c5ebd00SPaolo Abeni return 0; 33582c5ebd00SPaolo Abeni } 33592c5ebd00SPaolo Abeni 33602c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 33612c5ebd00SPaolo Abeni { 33622c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 33632c5ebd00SPaolo Abeni } 33642c5ebd00SPaolo Abeni 3365cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3366f870fa0bSMat Martineau { 3367f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3368cec37a6eSPeter Krystad struct socket *ssock; 3369f870fa0bSMat Martineau 3370cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3371cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3372cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3373cec37a6eSPeter Krystad return -EINVAL; 3374f870fa0bSMat Martineau 3375cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3376cec37a6eSPeter Krystad } 3377f870fa0bSMat Martineau 3378cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3379cec37a6eSPeter Krystad { 3380cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3381cec37a6eSPeter Krystad struct mptcp_sock *msk; 3382cec37a6eSPeter Krystad struct sock *sk; 3383f870fa0bSMat Martineau 3384cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3385cec37a6eSPeter Krystad sk = subflow->conn; 3386cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3387cec37a6eSPeter Krystad 3388648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3389648ef4b8SMat Martineau 3390b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3391648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 33926d0060f6SMat Martineau 3393cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3394cec37a6eSPeter Krystad * accessing the field below 3395cec37a6eSPeter Krystad */ 3396cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 33976d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3398eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 33997439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34001b1c7a0eSPeter Krystad 34016c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3402a6b118feSFlorian Westphal 3403a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3404f870fa0bSMat Martineau } 3405f870fa0bSMat Martineau 3406866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3407cf7da0d6SPeter Krystad { 3408cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3409cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3410cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3411cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3412cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3413cf7da0d6SPeter Krystad } 3414cf7da0d6SPeter Krystad 3415e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3416f296234cSPeter Krystad { 3417e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3418f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3419f296234cSPeter Krystad struct sock *parent = (void *)msk; 34203e501490SPaolo Abeni bool ret = true; 3421f296234cSPeter Krystad 3422f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3423f296234cSPeter Krystad 3424f296234cSPeter Krystad /* mptcp socket already closing? */ 3425dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3426dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3427f296234cSPeter Krystad return false; 3428dc87efdbSFlorian Westphal } 3429f296234cSPeter Krystad 343070c708e8SKishen Maloor if (!list_empty(&subflow->node)) 3431b911c97cSFlorian Westphal goto out; 3432f296234cSPeter Krystad 34333e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 34343e501490SPaolo Abeni goto err_prohibited; 343510f6d46cSPaolo Abeni 34363e501490SPaolo Abeni /* active connections are already on conn_list. 34373e501490SPaolo Abeni * If we can't acquire msk socket lock here, let the release callback 34383e501490SPaolo Abeni * handle it 343910f6d46cSPaolo Abeni */ 34403e501490SPaolo Abeni mptcp_data_lock(parent); 34413e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 34423e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 34433e501490SPaolo Abeni if (ret) { 3444e16163b6SPaolo Abeni sock_hold(ssk); 34453e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3446e16163b6SPaolo Abeni } 34473e501490SPaolo Abeni } else { 34483e501490SPaolo Abeni sock_hold(ssk); 34493e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3450e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 34513e501490SPaolo Abeni } 34523e501490SPaolo Abeni mptcp_data_unlock(parent); 34533e501490SPaolo Abeni 3454dc87efdbSFlorian Westphal if (!ret) { 34553e501490SPaolo Abeni err_prohibited: 3456dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 345710f6d46cSPaolo Abeni return false; 3458dc87efdbSFlorian Westphal } 345910f6d46cSPaolo Abeni 3460917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 34610530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 34623e501490SPaolo Abeni 3463b911c97cSFlorian Westphal out: 3464b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 346510f6d46cSPaolo Abeni return true; 3466f296234cSPeter Krystad } 3467f296234cSPeter Krystad 346876e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 346976e2a55dSPaolo Abeni { 347076e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 347176e2a55dSPaolo Abeni 347276e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 347376e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 347476e2a55dSPaolo Abeni } 347576e2a55dSPaolo Abeni 34766511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 34776511882cSPaolo Abeni { 34786511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 34796511882cSPaolo Abeni } 34806511882cSPaolo Abeni 3481644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3482644807e3SFlorian Westphal { 3483644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3484644807e3SFlorian Westphal u64 delta; 3485644807e3SFlorian Westphal 3486644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3487644807e3SFlorian Westphal return -EINVAL; 3488644807e3SFlorian Westphal 3489644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3490644807e3SFlorian Westphal return 0; 3491644807e3SFlorian Westphal 3492644807e3SFlorian Westphal delta = msk->write_seq - v; 349307c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 349407c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 349507c2c7a3SPaolo Abeni 349607c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 349707c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 349807c2c7a3SPaolo Abeni * so ignore that status, too. 349907c2c7a3SPaolo Abeni */ 350007c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 350107c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 350207c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 350307c2c7a3SPaolo Abeni } 3504644807e3SFlorian Westphal if (delta > INT_MAX) 3505644807e3SFlorian Westphal delta = INT_MAX; 3506644807e3SFlorian Westphal 3507644807e3SFlorian Westphal return (int)delta; 3508644807e3SFlorian Westphal } 3509644807e3SFlorian Westphal 3510644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3511644807e3SFlorian Westphal { 3512644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3513644807e3SFlorian Westphal bool slow; 3514644807e3SFlorian Westphal int answ; 3515644807e3SFlorian Westphal 3516644807e3SFlorian Westphal switch (cmd) { 3517644807e3SFlorian Westphal case SIOCINQ: 3518644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3519644807e3SFlorian Westphal return -EINVAL; 3520644807e3SFlorian Westphal 3521644807e3SFlorian Westphal lock_sock(sk); 3522644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3523644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3524644807e3SFlorian Westphal release_sock(sk); 3525644807e3SFlorian Westphal break; 3526644807e3SFlorian Westphal case SIOCOUTQ: 3527644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3528644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3529644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3530644807e3SFlorian Westphal break; 3531644807e3SFlorian Westphal case SIOCOUTQNSD: 3532644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3533644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3534644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3535644807e3SFlorian Westphal break; 3536644807e3SFlorian Westphal default: 3537644807e3SFlorian Westphal return -ENOIOCTLCMD; 3538644807e3SFlorian Westphal } 3539644807e3SFlorian Westphal 3540644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3541644807e3SFlorian Westphal } 3542644807e3SFlorian Westphal 354354f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 354454f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 354554f1944eSPaolo Abeni { 354654f1944eSPaolo Abeni subflow->request_mptcp = 0; 354754f1944eSPaolo Abeni __mptcp_do_fallback(msk); 354854f1944eSPaolo Abeni } 354954f1944eSPaolo Abeni 355054f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 355154f1944eSPaolo Abeni { 355254f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 355354f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 355454f1944eSPaolo Abeni struct socket *ssock; 355554f1944eSPaolo Abeni int err = -EINVAL; 355654f1944eSPaolo Abeni 355754f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 355854f1944eSPaolo Abeni if (!ssock) 355954f1944eSPaolo Abeni return -EINVAL; 356054f1944eSPaolo Abeni 356154f1944eSPaolo Abeni mptcp_token_destroy(msk); 356254f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 356354f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 356454f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 356554f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 356654f1944eSPaolo Abeni * TCP option space. 356754f1944eSPaolo Abeni */ 356854f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 356954f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 357054f1944eSPaolo Abeni #endif 357154f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 357254f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 357354f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 357454f1944eSPaolo Abeni } 357554f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 357654f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 357754f1944eSPaolo Abeni 357854f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 357954f1944eSPaolo Abeni * acquired the subflow socket lock, too. 358054f1944eSPaolo Abeni */ 35817d803344SPaolo Abeni if (msk->fastopening) 358254f1944eSPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags, 1); 358354f1944eSPaolo Abeni else 358454f1944eSPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags); 358554f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 358654f1944eSPaolo Abeni 358754f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 358854f1944eSPaolo Abeni * subflow_finish_connect() 358954f1944eSPaolo Abeni */ 359054f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 359154f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 359254f1944eSPaolo Abeni return err; 359354f1944eSPaolo Abeni } 359454f1944eSPaolo Abeni 359554f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 359654f1944eSPaolo Abeni 359754f1944eSPaolo Abeni /* unblocking connect, mptcp-level inet_stream_connect will error out 359854f1944eSPaolo Abeni * without changing the socket state, update it here. 359954f1944eSPaolo Abeni */ 360054f1944eSPaolo Abeni if (err == -EINPROGRESS) 360154f1944eSPaolo Abeni sk->sk_socket->state = ssock->state; 360254f1944eSPaolo Abeni return err; 360354f1944eSPaolo Abeni } 360454f1944eSPaolo Abeni 3605f870fa0bSMat Martineau static struct proto mptcp_prot = { 3606f870fa0bSMat Martineau .name = "MPTCP", 3607f870fa0bSMat Martineau .owner = THIS_MODULE, 3608f870fa0bSMat Martineau .init = mptcp_init_sock, 360954f1944eSPaolo Abeni .connect = mptcp_connect, 361018b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3611f870fa0bSMat Martineau .close = mptcp_close, 3612cf7da0d6SPeter Krystad .accept = mptcp_accept, 3613717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3614717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 361576e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 361679c0949eSPeter Krystad .destroy = mptcp_destroy, 3617f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3618644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3619f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 362014c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36212c5ebd00SPaolo Abeni .hash = mptcp_hash, 36222c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3623cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36246511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3625d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36260defbb0aSEric Dumazet 3627d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36280defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36290defbb0aSEric Dumazet 3630d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3631d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3632989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3633d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3634f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36352c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3636f870fa0bSMat Martineau .no_autobind = true, 3637f870fa0bSMat Martineau }; 3638f870fa0bSMat Martineau 36392303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 36402303f994SPeter Krystad { 36412303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 36422303f994SPeter Krystad struct socket *ssock; 3643cf7da0d6SPeter Krystad int err; 36442303f994SPeter Krystad 36452303f994SPeter Krystad lock_sock(sock->sk); 3646fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3647fa68018dSPaolo Abeni if (!ssock) { 3648fa68018dSPaolo Abeni err = -EINVAL; 36492303f994SPeter Krystad goto unlock; 36502303f994SPeter Krystad } 36512303f994SPeter Krystad 36522303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3653cf7da0d6SPeter Krystad if (!err) 3654cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 36552303f994SPeter Krystad 36562303f994SPeter Krystad unlock: 36572303f994SPeter Krystad release_sock(sock->sk); 36582303f994SPeter Krystad return err; 36592303f994SPeter Krystad } 36602303f994SPeter Krystad 36612303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 36622303f994SPeter Krystad int addr_len, int flags) 36632303f994SPeter Krystad { 366454f1944eSPaolo Abeni int ret; 36652303f994SPeter Krystad 36662303f994SPeter Krystad lock_sock(sock->sk); 366754f1944eSPaolo Abeni mptcp_sk(sock->sk)->connect_flags = flags; 366854f1944eSPaolo Abeni ret = __inet_stream_connect(sock, uaddr, addr_len, flags, 0); 36692303f994SPeter Krystad release_sock(sock->sk); 367054f1944eSPaolo Abeni return ret; 36712303f994SPeter Krystad } 36722303f994SPeter Krystad 3673cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3674cf7da0d6SPeter Krystad { 3675cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3676cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 3677cf7da0d6SPeter Krystad struct socket *ssock; 3678cf7da0d6SPeter Krystad int err; 3679cf7da0d6SPeter Krystad 3680cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3681cf7da0d6SPeter Krystad 3682cfdcfeedSMenglong Dong lock_sock(sk); 3683fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3684fa68018dSPaolo Abeni if (!ssock) { 3685fa68018dSPaolo Abeni err = -EINVAL; 3686cf7da0d6SPeter Krystad goto unlock; 3687cf7da0d6SPeter Krystad } 3688cf7da0d6SPeter Krystad 3689fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3690cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3691cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 36925e20087dSFlorian Westphal 3693cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3694cfdcfeedSMenglong Dong inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3695c558246eSMenglong Dong if (!err) { 3696c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3697cfdcfeedSMenglong Dong mptcp_copy_inaddrs(sk, ssock->sk); 3698c558246eSMenglong Dong } 3699cf7da0d6SPeter Krystad 3700f8c9dfbdSGeliang Tang mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 3701f8c9dfbdSGeliang Tang 3702cf7da0d6SPeter Krystad unlock: 3703cfdcfeedSMenglong Dong release_sock(sk); 3704cf7da0d6SPeter Krystad return err; 3705cf7da0d6SPeter Krystad } 3706cf7da0d6SPeter Krystad 3707cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3708cf7da0d6SPeter Krystad int flags, bool kern) 3709cf7da0d6SPeter Krystad { 3710cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3711cf7da0d6SPeter Krystad struct socket *ssock; 3712cf7da0d6SPeter Krystad int err; 3713cf7da0d6SPeter Krystad 3714cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3715cf7da0d6SPeter Krystad 3716cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3717cf7da0d6SPeter Krystad if (!ssock) 371871ba088cSPaolo Abeni return -EINVAL; 3719cf7da0d6SPeter Krystad 3720cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3721d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3722cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3723cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37240397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 37250397c6d8SPaolo Abeni 3726a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3727b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3728a5ef058dSPaolo Abeni 37294d54cc32SFlorian Westphal lock_sock(newsk); 37305b950ff4SPaolo Abeni 3731cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3732cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3733cf7da0d6SPeter Krystad */ 3734190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3735cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3736cf7da0d6SPeter Krystad 3737cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3738cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3739cf7da0d6SPeter Krystad } 37404d54cc32SFlorian Westphal release_sock(newsk); 3741cf7da0d6SPeter Krystad } 3742cf7da0d6SPeter Krystad 3743cf7da0d6SPeter Krystad return err; 3744cf7da0d6SPeter Krystad } 3745cf7da0d6SPeter Krystad 37468edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 37478edf0864SFlorian Westphal { 37488edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 37498edf0864SFlorian Westphal 37508edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3751dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37528edf0864SFlorian Westphal 37538edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 37548edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 37558edf0864SFlorian Westphal 37565cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 37576e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 37586e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 37596e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37608edf0864SFlorian Westphal 37616e628cd3SPaolo Abeni return 0; 37628edf0864SFlorian Westphal } 37638edf0864SFlorian Westphal 37642303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 37652303f994SPeter Krystad struct poll_table_struct *wait) 37662303f994SPeter Krystad { 37671891c4a0SFlorian Westphal struct sock *sk = sock->sk; 37688ab183deSPaolo Abeni struct mptcp_sock *msk; 37692303f994SPeter Krystad __poll_t mask = 0; 37708a05661bSPaolo Abeni int state; 37712303f994SPeter Krystad 37721891c4a0SFlorian Westphal msk = mptcp_sk(sk); 37731891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 37741891c4a0SFlorian Westphal 37758a05661bSPaolo Abeni state = inet_sk_state_load(sk); 37766719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 377771ba088cSPaolo Abeni if (state == TCP_LISTEN) { 377871ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 377971ba088cSPaolo Abeni return 0; 378071ba088cSPaolo Abeni 378171ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 378271ba088cSPaolo Abeni } 37838a05661bSPaolo Abeni 37848a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 37858a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 37868edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3787a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3788a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3789a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 37908a05661bSPaolo Abeni } 3791dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3792dd913410SPaolo Abeni mask |= EPOLLHUP; 37931891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 37941891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 37951891c4a0SFlorian Westphal 3796d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 379715cc1045SPaolo Abeni smp_rmb(); 379815cc1045SPaolo Abeni if (sk->sk_err) 379915cc1045SPaolo Abeni mask |= EPOLLERR; 380015cc1045SPaolo Abeni 38012303f994SPeter Krystad return mask; 38022303f994SPeter Krystad } 38032303f994SPeter Krystad 3804e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3805e42f1ac6SFlorian Westphal .family = PF_INET, 3806e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38070a3cc579SPaolo Abeni .release = inet_release, 3808e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3809e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3810e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3811e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3812d2f77c53SPaolo Abeni .getname = inet_getname, 3813e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3814e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3815e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3816e42f1ac6SFlorian Westphal .listen = mptcp_listen, 381776e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3818e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3819e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3820e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3821e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3822e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3823e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3824e42f1ac6SFlorian Westphal }; 38252303f994SPeter Krystad 3826f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3827f870fa0bSMat Martineau .type = SOCK_STREAM, 3828f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3829f870fa0bSMat Martineau .prot = &mptcp_prot, 38302303f994SPeter Krystad .ops = &mptcp_stream_ops, 38312303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3832f870fa0bSMat Martineau }; 3833f870fa0bSMat Martineau 3834b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3835b19bc294SPaolo Abeni { 3836b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3837b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3838b19bc294SPaolo Abeni int work_done = 0; 3839b19bc294SPaolo Abeni 3840b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3841b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3842b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3843b19bc294SPaolo Abeni 3844b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3845b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3846b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3847b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3848b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3849b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3850b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3851b19bc294SPaolo Abeni * CPU that scheduled it. 3852b19bc294SPaolo Abeni */ 3853b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3854b19bc294SPaolo Abeni sock_put(ssk); 3855b19bc294SPaolo Abeni 3856b19bc294SPaolo Abeni if (++work_done == budget) 3857b19bc294SPaolo Abeni return budget; 3858b19bc294SPaolo Abeni } 3859b19bc294SPaolo Abeni 3860b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3861b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3862b19bc294SPaolo Abeni */ 3863b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3864b19bc294SPaolo Abeni return work_done; 3865b19bc294SPaolo Abeni } 3866b19bc294SPaolo Abeni 3867d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3868f870fa0bSMat Martineau { 3869b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3870b19bc294SPaolo Abeni int cpu; 3871b19bc294SPaolo Abeni 38722303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 38732303f994SPeter Krystad 3874d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3875d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3876d027236cSPaolo Abeni 3877b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3878b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3879b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3880b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 388116d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 388216d083e2SJakub Kicinski mptcp_napi_poll); 3883b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3884b19bc294SPaolo Abeni } 3885b19bc294SPaolo Abeni 38862303f994SPeter Krystad mptcp_subflow_init(); 38871b1c7a0eSPeter Krystad mptcp_pm_init(); 38882c5ebd00SPaolo Abeni mptcp_token_init(); 38892303f994SPeter Krystad 3890f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3891f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3892f870fa0bSMat Martineau 3893f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 38946771bfd9SFlorian Westphal 38956771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3896f870fa0bSMat Martineau } 3897f870fa0bSMat Martineau 3898f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3899e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3900e42f1ac6SFlorian Westphal .family = PF_INET6, 3901e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39020a3cc579SPaolo Abeni .release = inet6_release, 3903e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3904e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3905e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3906e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3907d2f77c53SPaolo Abeni .getname = inet6_getname, 3908e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3909e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3910e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3911e42f1ac6SFlorian Westphal .listen = mptcp_listen, 391276e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3913e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3914e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3915e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3916e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3917e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3918e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3919e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39203986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3921e42f1ac6SFlorian Westphal #endif 3922e42f1ac6SFlorian Westphal }; 3923e42f1ac6SFlorian Westphal 3924f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3925f870fa0bSMat Martineau 3926f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3927f870fa0bSMat Martineau .type = SOCK_STREAM, 3928f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3929f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39302303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3931f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3932f870fa0bSMat Martineau }; 3933f870fa0bSMat Martineau 3934d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3935f870fa0bSMat Martineau { 3936f870fa0bSMat Martineau int err; 3937f870fa0bSMat Martineau 3938f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3939f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3940f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 3941b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3942f870fa0bSMat Martineau 3943f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3944f870fa0bSMat Martineau if (err) 3945f870fa0bSMat Martineau return err; 3946f870fa0bSMat Martineau 3947f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3948f870fa0bSMat Martineau if (err) 3949f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3950f870fa0bSMat Martineau 3951f870fa0bSMat Martineau return err; 3952f870fa0bSMat Martineau } 3953f870fa0bSMat Martineau #endif 3954