1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25644807e3SFlorian Westphal #include <asm/ioctls.h> 26f870fa0bSMat Martineau #include "protocol.h" 27fc518953SFlorian Westphal #include "mib.h" 28f870fa0bSMat Martineau 29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 30e10a9892SGeliang Tang #include <trace/events/mptcp.h> 31e10a9892SGeliang Tang 32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33b0519de8SFlorian Westphal struct mptcp6_sock { 34b0519de8SFlorian Westphal struct mptcp_sock msk; 35b0519de8SFlorian Westphal struct ipv6_pinfo np; 36b0519de8SFlorian Westphal }; 37b0519de8SFlorian Westphal #endif 38b0519de8SFlorian Westphal 39b7f653b2SFlorian Westphal enum { 40b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 412c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 42b7f653b2SFlorian Westphal }; 43b7f653b2SFlorian Westphal 4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45d027236cSPaolo Abeni 46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 4781c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk); 48e16163b6SPaolo Abeni 49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 51b19bc294SPaolo Abeni 526f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 536f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 546f8a612aSFlorian Westphal { 557439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 566f8a612aSFlorian Westphal } 576f8a612aSFlorian Westphal 5812f353faSDavide Caratti static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk) 590b4f33deSFlorian Westphal { 600b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 6112f353faSDavide Caratti if (sk->sk_prot == &tcpv6_prot) 6212f353faSDavide Caratti return &inet6_stream_ops; 630b4f33deSFlorian Westphal #endif 6412f353faSDavide Caratti WARN_ON_ONCE(sk->sk_prot != &tcp_prot); 6512f353faSDavide Caratti return &inet_stream_ops; 660b4f33deSFlorian Westphal } 670b4f33deSFlorian Westphal 68fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 692303f994SPeter Krystad { 702303f994SPeter Krystad struct mptcp_subflow_context *subflow; 712303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 722303f994SPeter Krystad struct socket *ssock; 732303f994SPeter Krystad int err; 742303f994SPeter Krystad 756bc1fe7dSPaolo Abeni err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock); 762303f994SPeter Krystad if (err) 77fa68018dSPaolo Abeni return err; 782303f994SPeter Krystad 79b8dc6d6cSPaolo Abeni msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio; 801b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, ssock->sk); 812303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 82cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 83e16163b6SPaolo Abeni sock_hold(ssock->sk); 842303f994SPeter Krystad subflow->request_mptcp = 1; 856f06b4d4SPaolo Abeni subflow->subflow_id = msk->subflow_id++; 864cf86ae8SPaolo Abeni 874cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 88ba2cf922SPaolo Abeni WRITE_ONCE(subflow->local_id, 0); 89866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 9039880bd8SPaolo Abeni iput(SOCK_INODE(ssock)); 91e1ff9e82SDavide Caratti 92fa68018dSPaolo Abeni return 0; 932303f994SPeter Krystad } 942303f994SPeter Krystad 95ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow, 96ddb1a072SPaolo Abeni * eventually allocating it. 97ddb1a072SPaolo Abeni */ 983f326a82SPaolo Abeni struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk) 99ddb1a072SPaolo Abeni { 100ddb1a072SPaolo Abeni struct sock *sk = (struct sock *)msk; 101ddb1a072SPaolo Abeni int ret; 102ddb1a072SPaolo Abeni 103ddb1a072SPaolo Abeni if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 104ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 105ddb1a072SPaolo Abeni 1063f326a82SPaolo Abeni if (!msk->first) { 107ddb1a072SPaolo Abeni ret = __mptcp_socket_create(msk); 108ddb1a072SPaolo Abeni if (ret) 109ddb1a072SPaolo Abeni return ERR_PTR(ret); 110ddb1a072SPaolo Abeni 111ddb1a072SPaolo Abeni mptcp_sockopt_sync(msk, msk->first); 112ddb1a072SPaolo Abeni } 113ddb1a072SPaolo Abeni 1143f326a82SPaolo Abeni return msk->first; 115ddb1a072SPaolo Abeni } 116ddb1a072SPaolo Abeni 117ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 118ab174ad8SPaolo Abeni { 119ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 120ab174ad8SPaolo Abeni __kfree_skb(skb); 121ab174ad8SPaolo Abeni } 122ab174ad8SPaolo Abeni 1239531e4a8SEric Dumazet static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size) 1249531e4a8SEric Dumazet { 1259531e4a8SEric Dumazet WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc, 1269531e4a8SEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc + size); 1279531e4a8SEric Dumazet } 1289531e4a8SEric Dumazet 1296511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1306511882cSPaolo Abeni { 1319531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, -size); 1326511882cSPaolo Abeni } 1336511882cSPaolo Abeni 1348268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1358268ed4cSPaolo Abeni struct sk_buff *from) 1368268ed4cSPaolo Abeni { 1378268ed4cSPaolo Abeni bool fragstolen; 1388268ed4cSPaolo Abeni int delta; 1398268ed4cSPaolo Abeni 1408268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1418268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1428268ed4cSPaolo Abeni return false; 1438268ed4cSPaolo Abeni 14406242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 14506242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 14606242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 147ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1487288ff6eSPaolo Abeni 1497288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1507288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1517288ff6eSPaolo Abeni * negative one 1527288ff6eSPaolo Abeni */ 1538268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1546511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1557288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1567288ff6eSPaolo Abeni 1578268ed4cSPaolo Abeni return true; 1588268ed4cSPaolo Abeni } 1598268ed4cSPaolo Abeni 160ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 161ab174ad8SPaolo Abeni struct sk_buff *from) 1626771bfd9SFlorian Westphal { 163ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 164ab174ad8SPaolo Abeni return false; 165ab174ad8SPaolo Abeni 166ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 167ab174ad8SPaolo Abeni } 168ab174ad8SPaolo Abeni 1696511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1706511882cSPaolo Abeni { 171100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 1729531e4a8SEric Dumazet mptcp_rmem_charge(sk, amount << PAGE_SHIFT); 1736511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1746511882cSPaolo Abeni } 1756511882cSPaolo Abeni 1766511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1776511882cSPaolo Abeni { 1786511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1796511882cSPaolo Abeni int reclaimable; 1806511882cSPaolo Abeni 1819531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, size); 1826511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1836511882cSPaolo Abeni 1846511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 185d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 186d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1876511882cSPaolo Abeni } 1886511882cSPaolo Abeni 1896511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 1906511882cSPaolo Abeni { 1916511882cSPaolo Abeni unsigned int len = skb->truesize; 1926511882cSPaolo Abeni struct sock *sk = skb->sk; 1936511882cSPaolo Abeni 1946511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 1956511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 1966511882cSPaolo Abeni } 1976511882cSPaolo Abeni 19836b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 1996511882cSPaolo Abeni { 2006511882cSPaolo Abeni skb_orphan(skb); 2016511882cSPaolo Abeni skb->sk = sk; 2026511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2036511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2046511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2056511882cSPaolo Abeni } 2066511882cSPaolo Abeni 207ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 208ab174ad8SPaolo Abeni * - use mptcp seqs 209ab174ad8SPaolo Abeni * - don't cope with sacks 210ab174ad8SPaolo Abeni */ 211ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 212ab174ad8SPaolo Abeni { 213ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 214ab174ad8SPaolo Abeni struct rb_node **p, *parent; 215ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 216ab174ad8SPaolo Abeni struct sk_buff *skb1; 217ab174ad8SPaolo Abeni 218ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 219ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 220f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 221ab174ad8SPaolo Abeni 22206242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 22306242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 224fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 225ab174ad8SPaolo Abeni /* out of window */ 226ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 227fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 228fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 229f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 23006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 231ab174ad8SPaolo Abeni return; 232ab174ad8SPaolo Abeni } 233ab174ad8SPaolo Abeni 234ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 23506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 236ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 237ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 238ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 239ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 240ab174ad8SPaolo Abeni goto end; 241ab174ad8SPaolo Abeni } 242ab174ad8SPaolo Abeni 243ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 244ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 245ab174ad8SPaolo Abeni */ 24606242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 24706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 24806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 249ab174ad8SPaolo Abeni return; 25006242e44SPaolo Abeni } 251ab174ad8SPaolo Abeni 252ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 253ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 25406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 255ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 256ab174ad8SPaolo Abeni p = &parent->rb_right; 257ab174ad8SPaolo Abeni goto insert; 258ab174ad8SPaolo Abeni } 259ab174ad8SPaolo Abeni 260ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 261ab174ad8SPaolo Abeni parent = NULL; 262ab174ad8SPaolo Abeni while (*p) { 263ab174ad8SPaolo Abeni parent = *p; 264ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 265ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 266ab174ad8SPaolo Abeni p = &parent->rb_left; 267ab174ad8SPaolo Abeni continue; 268ab174ad8SPaolo Abeni } 269ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 270ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 271ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 272ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 27306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 274ab174ad8SPaolo Abeni return; 275ab174ad8SPaolo Abeni } 276ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 277ab174ad8SPaolo Abeni /* partial overlap: 278ab174ad8SPaolo Abeni * | skb | 279ab174ad8SPaolo Abeni * | skb1 | 280ab174ad8SPaolo Abeni * continue traversing 281ab174ad8SPaolo Abeni */ 282ab174ad8SPaolo Abeni } else { 283ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 284ab174ad8SPaolo Abeni * Replace skb1 with skb. 285ab174ad8SPaolo Abeni */ 286ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 287ab174ad8SPaolo Abeni &msk->out_of_order_queue); 288ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 28906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 290ab174ad8SPaolo Abeni goto merge_right; 291ab174ad8SPaolo Abeni } 292ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 29306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 294ab174ad8SPaolo Abeni return; 295ab174ad8SPaolo Abeni } 296ab174ad8SPaolo Abeni p = &parent->rb_right; 297ab174ad8SPaolo Abeni } 29806242e44SPaolo Abeni 299ab174ad8SPaolo Abeni insert: 300ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 301ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 302ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 303ab174ad8SPaolo Abeni 304ab174ad8SPaolo Abeni merge_right: 305ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 306ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 307ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 308ab174ad8SPaolo Abeni break; 309ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 310ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 31106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 312ab174ad8SPaolo Abeni } 313ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 314ab174ad8SPaolo Abeni if (!skb1) 315ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 316ab174ad8SPaolo Abeni 317ab174ad8SPaolo Abeni end: 318ab174ad8SPaolo Abeni skb_condense(skb); 3196511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3206511882cSPaolo Abeni } 3216511882cSPaolo Abeni 3226511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3236511882cSPaolo Abeni { 3246511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3256511882cSPaolo Abeni int amt, amount; 3266511882cSPaolo Abeni 32769d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3286511882cSPaolo Abeni return true; 3296511882cSPaolo Abeni 33069d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3316511882cSPaolo Abeni amt = sk_mem_pages(size); 332100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3334aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3346511882cSPaolo Abeni return false; 3356511882cSPaolo Abeni 3369531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, amount); 3376511882cSPaolo Abeni return true; 338ab174ad8SPaolo Abeni } 339ab174ad8SPaolo Abeni 340ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 341ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 342ab174ad8SPaolo Abeni size_t copy_len) 343ab174ad8SPaolo Abeni { 344ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3456771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3464e637c70SFlorian Westphal struct sk_buff *tail; 347b7f653b2SFlorian Westphal bool has_rxtstamp; 3486771bfd9SFlorian Westphal 3496771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3504e637c70SFlorian Westphal 3514e637c70SFlorian Westphal skb_ext_reset(skb); 3524e637c70SFlorian Westphal skb_orphan(skb); 3534e637c70SFlorian Westphal 3549c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 35519bf3292SPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) { 35619bf3292SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 3579c3f94e1SPaolo Abeni goto drop; 35819bf3292SPaolo Abeni } 35972f96132SPaolo Abeni 360b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 361b7f653b2SFlorian Westphal 362ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 363ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 364ab174ad8SPaolo Abeni * value 365ab174ad8SPaolo Abeni */ 366ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 367ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 368ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 369b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 370ab174ad8SPaolo Abeni 371ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 372ab174ad8SPaolo Abeni /* in sequence */ 37338967f42SPaolo Abeni msk->bytes_received += copy_len; 374917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3754e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3768268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 377ab174ad8SPaolo Abeni return true; 3784e637c70SFlorian Westphal 3796511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3806771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 381ab174ad8SPaolo Abeni return true; 382ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 383ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 384ab174ad8SPaolo Abeni return false; 385ab174ad8SPaolo Abeni } 386ab174ad8SPaolo Abeni 387ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 388ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 389ab174ad8SPaolo Abeni */ 39006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3919c3f94e1SPaolo Abeni drop: 392ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 393ab174ad8SPaolo Abeni return false; 3946771bfd9SFlorian Westphal } 3956771bfd9SFlorian Westphal 396f6909dc1SPaolo Abeni static void mptcp_stop_rtx_timer(struct sock *sk) 39716a9a9daSMat Martineau { 39816a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 39916a9a9daSMat Martineau 40016a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 40116a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 40216a9a9daSMat Martineau } 40316a9a9daSMat Martineau 404e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 405e16163b6SPaolo Abeni { 406e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 407e16163b6SPaolo Abeni return; 408e16163b6SPaolo Abeni 409e16163b6SPaolo Abeni sk->sk_state_change(sk); 410e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 411e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 412e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 413e16163b6SPaolo Abeni else 414e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 415e16163b6SPaolo Abeni } 416e16163b6SPaolo Abeni 4176e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4186e628cd3SPaolo Abeni { 4196e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4206e628cd3SPaolo Abeni 42181c1d029SPaolo Abeni return ((1 << sk->sk_state) & 4226e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4236e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4246e628cd3SPaolo Abeni } 4256e628cd3SPaolo Abeni 42616a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 42716a9a9daSMat Martineau { 42816a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 42916a9a9daSMat Martineau 43016a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4316e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 43216a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 43316a9a9daSMat Martineau 43416a9a9daSMat Martineau switch (sk->sk_state) { 43516a9a9daSMat Martineau case TCP_FIN_WAIT1: 436d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_FIN_WAIT2); 43716a9a9daSMat Martineau break; 43816a9a9daSMat Martineau case TCP_CLOSING: 43916a9a9daSMat Martineau case TCP_LAST_ACK: 440d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 44116a9a9daSMat Martineau break; 44216a9a9daSMat Martineau } 44316a9a9daSMat Martineau 444e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 44516a9a9daSMat Martineau } 44616a9a9daSMat Martineau } 44716a9a9daSMat Martineau 4483721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4493721b9b6SMat Martineau { 4503721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4513721b9b6SMat Martineau 4523721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4533721b9b6SMat Martineau ((1 << sk->sk_state) & 4543721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4553721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4563721b9b6SMat Martineau 4573721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4583721b9b6SMat Martineau if (seq) 4593721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4603721b9b6SMat Martineau 4613721b9b6SMat Martineau return true; 4623721b9b6SMat Martineau } 4633721b9b6SMat Martineau } 4643721b9b6SMat Martineau 4653721b9b6SMat Martineau return false; 4663721b9b6SMat Martineau } 4673721b9b6SMat Martineau 468403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk) 4696477dd39SMat Martineau { 4706477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 471877d11f0SMat Martineau u32 retransmits; 4726477dd39SMat Martineau 473877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 474877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 475877d11f0SMat Martineau 476877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4776477dd39SMat Martineau } 4786477dd39SMat Martineau 47933d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4803721b9b6SMat Martineau { 4813721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4823721b9b6SMat Martineau } 4833721b9b6SMat Martineau 48433d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 48533d41c9cSPaolo Abeni { 48633d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 48733d41c9cSPaolo Abeni 48871b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 48971b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 49033d41c9cSPaolo Abeni } 49133d41c9cSPaolo Abeni 49233d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 49333d41c9cSPaolo Abeni { 49433d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 49533d41c9cSPaolo Abeni long tout = 0; 49633d41c9cSPaolo Abeni 49733d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 49833d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 49933d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 50033d41c9cSPaolo Abeni } 50133d41c9cSPaolo Abeni 50276a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 503fd897679SPaolo Abeni { 504fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 50520bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 506fd897679SPaolo Abeni } 507fd897679SPaolo Abeni 508a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 509a6574302SMat Martineau { 510a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 511a6574302SMat Martineau tcp_send_ack(ssk); 512a6574302SMat Martineau } 513a6574302SMat Martineau 514f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5157ed90803SPaolo Abeni { 51675e908c3SPaolo Abeni bool slow; 5177ed90803SPaolo Abeni 51875e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 519a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 52075e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 521fd897679SPaolo Abeni } 522340fa666SMat Martineau 523340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 524340fa666SMat Martineau { 525340fa666SMat Martineau struct mptcp_subflow_context *subflow; 526340fa666SMat Martineau 527340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 528340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 529ea4ca586SPaolo Abeni } 530ea4ca586SPaolo Abeni 531fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 532fd897679SPaolo Abeni { 53375e908c3SPaolo Abeni bool slow; 534fd897679SPaolo Abeni 53575e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 536fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 537fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 53875e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 539fde56eeaSPaolo Abeni } 540fde56eeaSPaolo Abeni 541fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 542fde56eeaSPaolo Abeni { 543fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 544c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 545fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 546fde56eeaSPaolo Abeni 547fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 548fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 549fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 550fde56eeaSPaolo Abeni (rx_empty && ack_pending & 551fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 552ea4ca586SPaolo Abeni } 553fd897679SPaolo Abeni 554fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 555fd897679SPaolo Abeni { 556e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 557fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 558e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 559fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 560fde56eeaSPaolo Abeni bool cleanup, rx_empty; 561e3859603SPaolo Abeni 562fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 563ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 564fd897679SPaolo Abeni 565fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 566fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 567fd897679SPaolo Abeni 568fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 569fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5707ed90803SPaolo Abeni } 5717ed90803SPaolo Abeni } 5727ed90803SPaolo Abeni 5737ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5743721b9b6SMat Martineau { 5753721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5763721b9b6SMat Martineau u64 rcv_data_fin_seq; 5777ed90803SPaolo Abeni bool ret = false; 5783721b9b6SMat Martineau 5793721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5803721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5813721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5823721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5833721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5843721b9b6SMat Martineau * the msk state. 5853721b9b6SMat Martineau * 5863721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5873721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5883721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5893721b9b6SMat Martineau * when catching up. 5903721b9b6SMat Martineau */ 5913721b9b6SMat Martineau 5923721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 593917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5943721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5953721b9b6SMat Martineau 5966b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 59716a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 5983721b9b6SMat Martineau 5993721b9b6SMat Martineau switch (sk->sk_state) { 6003721b9b6SMat Martineau case TCP_ESTABLISHED: 601d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE_WAIT); 6023721b9b6SMat Martineau break; 6033721b9b6SMat Martineau case TCP_FIN_WAIT1: 604d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSING); 6053721b9b6SMat Martineau break; 6063721b9b6SMat Martineau case TCP_FIN_WAIT2: 607d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 6083721b9b6SMat Martineau break; 6093721b9b6SMat Martineau default: 6103721b9b6SMat Martineau /* Other states not expected */ 6113721b9b6SMat Martineau WARN_ON_ONCE(1); 6123721b9b6SMat Martineau break; 6133721b9b6SMat Martineau } 6143721b9b6SMat Martineau 6157ed90803SPaolo Abeni ret = true; 61681c1d029SPaolo Abeni if (!__mptcp_check_fallback(msk)) 617fd897679SPaolo Abeni mptcp_send_ack(msk); 618e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6193721b9b6SMat Martineau } 6207ed90803SPaolo Abeni return ret; 6213721b9b6SMat Martineau } 6223721b9b6SMat Martineau 6236771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6246771bfd9SFlorian Westphal struct sock *ssk, 6256771bfd9SFlorian Westphal unsigned int *bytes) 6266771bfd9SFlorian Westphal { 6276771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 628600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6296771bfd9SFlorian Westphal unsigned int moved = 0; 6306771bfd9SFlorian Westphal bool more_data_avail; 6316771bfd9SFlorian Westphal struct tcp_sock *tp; 6326771bfd9SFlorian Westphal bool done = false; 63313c7ba0cSFlorian Westphal int sk_rbuf; 63413c7ba0cSFlorian Westphal 63513c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 63613c7ba0cSFlorian Westphal 63713c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 63813c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 63913c7ba0cSFlorian Westphal 64013c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 64113c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 64213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 64313c7ba0cSFlorian Westphal } 64413c7ba0cSFlorian Westphal } 645600911ffSFlorian Westphal 646ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6476771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6486771bfd9SFlorian Westphal do { 6496771bfd9SFlorian Westphal u32 map_remaining, offset; 6506771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6516771bfd9SFlorian Westphal struct sk_buff *skb; 6526771bfd9SFlorian Westphal bool fin; 6536771bfd9SFlorian Westphal 6546771bfd9SFlorian Westphal /* try to move as much data as available */ 6556771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6566771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6576771bfd9SFlorian Westphal 6586771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 659d9fb8c50SPaolo Abeni if (!skb) { 660d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 661d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 662d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 663d9fb8c50SPaolo Abeni */ 664d9fb8c50SPaolo Abeni if (!moved) 665d9fb8c50SPaolo Abeni done = true; 6666771bfd9SFlorian Westphal break; 667d9fb8c50SPaolo Abeni } 6686771bfd9SFlorian Westphal 669e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 670d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 671d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 672d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 673e1ff9e82SDavide Caratti */ 674e1ff9e82SDavide Caratti map_remaining = skb->len; 675e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 676e1ff9e82SDavide Caratti } 677e1ff9e82SDavide Caratti 6786771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6796771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6806771bfd9SFlorian Westphal if (fin) { 6816771bfd9SFlorian Westphal done = true; 6826771bfd9SFlorian Westphal seq++; 6836771bfd9SFlorian Westphal } 6846771bfd9SFlorian Westphal 6856771bfd9SFlorian Westphal if (offset < skb->len) { 6866771bfd9SFlorian Westphal size_t len = skb->len - offset; 6876771bfd9SFlorian Westphal 6886771bfd9SFlorian Westphal if (tp->urg_data) 6896771bfd9SFlorian Westphal done = true; 6906771bfd9SFlorian Westphal 691ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6926771bfd9SFlorian Westphal moved += len; 693ab174ad8SPaolo Abeni seq += len; 6946771bfd9SFlorian Westphal 6956771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6966771bfd9SFlorian Westphal break; 6976771bfd9SFlorian Westphal } else { 6986771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6996771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 7006771bfd9SFlorian Westphal done = true; 7016771bfd9SFlorian Westphal } 7026771bfd9SFlorian Westphal 7036771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7046771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 705600911ffSFlorian Westphal 70613c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 707600911ffSFlorian Westphal done = true; 708600911ffSFlorian Westphal break; 709600911ffSFlorian Westphal } 7106771bfd9SFlorian Westphal } while (more_data_avail); 7116771bfd9SFlorian Westphal 7126719331cSPaolo Abeni *bytes += moved; 7136771bfd9SFlorian Westphal return done; 7146771bfd9SFlorian Westphal } 7156771bfd9SFlorian Westphal 71687952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 717ab174ad8SPaolo Abeni { 718ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 719ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 720ab174ad8SPaolo Abeni bool moved = false; 721ab174ad8SPaolo Abeni struct rb_node *p; 722ab174ad8SPaolo Abeni u64 end_seq; 723ab174ad8SPaolo Abeni 724ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 72506242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 726ab174ad8SPaolo Abeni while (p) { 727ab174ad8SPaolo Abeni skb = rb_to_skb(p); 728ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 729ab174ad8SPaolo Abeni break; 730ab174ad8SPaolo Abeni 731ab174ad8SPaolo Abeni p = rb_next(p); 732ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 733ab174ad8SPaolo Abeni 734ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 735ab174ad8SPaolo Abeni msk->ack_seq))) { 736ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 73706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 738ab174ad8SPaolo Abeni continue; 739ab174ad8SPaolo Abeni } 740ab174ad8SPaolo Abeni 741ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 742ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 743ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 744ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 745ab174ad8SPaolo Abeni 746ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 74706242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 74806242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 74906242e44SPaolo Abeni delta); 750ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7512c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 752ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 753ab174ad8SPaolo Abeni } 75438967f42SPaolo Abeni msk->bytes_received += end_seq - msk->ack_seq; 755ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 756ab174ad8SPaolo Abeni moved = true; 757ab174ad8SPaolo Abeni } 758ab174ad8SPaolo Abeni return moved; 759ab174ad8SPaolo Abeni } 760ab174ad8SPaolo Abeni 7619f1a9881SPaolo Abeni static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk) 762d5fbeff1SPaolo Abeni { 763d5fbeff1SPaolo Abeni int err = sock_error(ssk); 764d5fbeff1SPaolo Abeni int ssk_state; 765d5fbeff1SPaolo Abeni 766d5fbeff1SPaolo Abeni if (!err) 7679f1a9881SPaolo Abeni return false; 768d5fbeff1SPaolo Abeni 769d5fbeff1SPaolo Abeni /* only propagate errors on fallen-back sockets or 770d5fbeff1SPaolo Abeni * on MPC connect 771d5fbeff1SPaolo Abeni */ 7729f1a9881SPaolo Abeni if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk))) 7739f1a9881SPaolo Abeni return false; 774d5fbeff1SPaolo Abeni 775d5fbeff1SPaolo Abeni /* We need to propagate only transition to CLOSE state. 776d5fbeff1SPaolo Abeni * Orphaned socket will see such state change via 777d5fbeff1SPaolo Abeni * subflow_sched_work_if_closed() and that path will properly 778d5fbeff1SPaolo Abeni * destroy the msk as needed. 779d5fbeff1SPaolo Abeni */ 780d5fbeff1SPaolo Abeni ssk_state = inet_sk_state_load(ssk); 781d5fbeff1SPaolo Abeni if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD)) 782d52b3c2bSGeliang Tang mptcp_set_state(sk, ssk_state); 783d5fbeff1SPaolo Abeni WRITE_ONCE(sk->sk_err, -err); 784d5fbeff1SPaolo Abeni 785d5fbeff1SPaolo Abeni /* This barrier is coupled with smp_rmb() in mptcp_poll() */ 786d5fbeff1SPaolo Abeni smp_wmb(); 787d5fbeff1SPaolo Abeni sk_error_report(sk); 7889f1a9881SPaolo Abeni return true; 789d5fbeff1SPaolo Abeni } 7909f1a9881SPaolo Abeni 7919f1a9881SPaolo Abeni void __mptcp_error_report(struct sock *sk) 7929f1a9881SPaolo Abeni { 7939f1a9881SPaolo Abeni struct mptcp_subflow_context *subflow; 7949f1a9881SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 7959f1a9881SPaolo Abeni 7969f1a9881SPaolo Abeni mptcp_for_each_subflow(msk, subflow) 7979f1a9881SPaolo Abeni if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow))) 7989f1a9881SPaolo Abeni break; 799d5fbeff1SPaolo Abeni } 800d5fbeff1SPaolo Abeni 8012e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 8022e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 8032e52213cSFlorian Westphal */ 80499d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 8052e52213cSFlorian Westphal { 8062e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 8072e52213cSFlorian Westphal unsigned int moved = 0; 8082e52213cSFlorian Westphal 8092e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 81087952603SPaolo Abeni __mptcp_ofo_queue(msk); 811499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 812499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 813499ada50SPaolo Abeni __mptcp_error_report(sk); 814499ada50SPaolo Abeni else 815e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 816499ada50SPaolo Abeni } 817ab174ad8SPaolo Abeni 818ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 819ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 820ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 821ab174ad8SPaolo Abeni * do it. 822ab174ad8SPaolo Abeni */ 823ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 824ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 82599d1055cSPaolo Abeni return moved > 0; 8262e52213cSFlorian Westphal } 8272e52213cSFlorian Westphal 8282e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 829101f6f85SFlorian Westphal { 8306719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 831101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 83213c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 833101f6f85SFlorian Westphal 834d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 835d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 836d7b1bfd0SPaolo Abeni * more data to the msk receive queue 837d7b1bfd0SPaolo Abeni */ 838d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 839d7b1bfd0SPaolo Abeni return; 840d7b1bfd0SPaolo Abeni 84113c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 84213c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 84313c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 84413c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 84513c7ba0cSFlorian Westphal 84699d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 84719bf3292SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) 84899d1055cSPaolo Abeni return; 8492e52213cSFlorian Westphal 85099d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 85199d1055cSPaolo Abeni mptcp_data_lock(sk); 852124a4885SPaolo Abeni if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk)) 853101f6f85SFlorian Westphal sk->sk_data_ready(sk); 85499d1055cSPaolo Abeni mptcp_data_unlock(sk); 85599d1055cSPaolo Abeni } 856101f6f85SFlorian Westphal 85755b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 85855b47ca7SPaolo Abeni { 85955b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 86055b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 86155b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 86255b47ca7SPaolo Abeni } 86355b47ca7SPaolo Abeni 8643e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 865ec3edaa7SPeter Krystad { 8663e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8675cf92bbaSPaolo Abeni 8683e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 86978962489SFlorian Westphal return false; 870ec3edaa7SPeter Krystad 8713e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8723e501490SPaolo Abeni * at close time 8733e501490SPaolo Abeni */ 8743e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8753e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 87678962489SFlorian Westphal 8776f06b4d4SPaolo Abeni mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; 8783e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 87955b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 88027e5ccc2SPaolo Abeni mptcp_stop_tout_timer(sk); 88134c7757aSPaolo Abeni __mptcp_propagate_sndbuf(sk, ssk); 8823e501490SPaolo Abeni return true; 88378962489SFlorian Westphal } 88478962489SFlorian Westphal 88556a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 88678962489SFlorian Westphal { 8873e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8883e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 88978962489SFlorian Westphal 89056a666c4SPaolo Abeni list_for_each_entry_safe(subflow, tmp, join_list, node) { 8913e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8923e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8933e501490SPaolo Abeni 8943e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8953e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8963e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8973e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 89878962489SFlorian Westphal } 899ec3edaa7SPeter Krystad } 900ec3edaa7SPeter Krystad 901f6909dc1SPaolo Abeni static bool mptcp_rtx_timer_pending(struct sock *sk) 902b51f9b80SPaolo Abeni { 903b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 904b51f9b80SPaolo Abeni } 905b51f9b80SPaolo Abeni 906f6909dc1SPaolo Abeni static void mptcp_reset_rtx_timer(struct sock *sk) 907b51f9b80SPaolo Abeni { 908b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 909b51f9b80SPaolo Abeni unsigned long tout; 910b51f9b80SPaolo Abeni 911e16163b6SPaolo Abeni /* prevent rescheduling on close */ 912e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 913e16163b6SPaolo Abeni return; 914e16163b6SPaolo Abeni 91533d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 916b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 917b51f9b80SPaolo Abeni } 918b51f9b80SPaolo Abeni 919ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 920ba8f48f7SPaolo Abeni { 921ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 922ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 923ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 924ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 925ba8f48f7SPaolo Abeni */ 926ba8f48f7SPaolo Abeni sock_hold(sk); 927ba8f48f7SPaolo Abeni return true; 928ba8f48f7SPaolo Abeni } 929ba8f48f7SPaolo Abeni return false; 930ba8f48f7SPaolo Abeni } 931ba8f48f7SPaolo Abeni 9327a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9337a6a6cbcSPaolo Abeni { 9347a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9357a6a6cbcSPaolo Abeni 936109cdeb8SGeliang Tang msk_owned_by_me(msk); 9377a6a6cbcSPaolo Abeni 9387a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 93999d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9407a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9417a6a6cbcSPaolo Abeni } 9427a6a6cbcSPaolo Abeni 9437a6a6cbcSPaolo Abeni return NULL; 9447a6a6cbcSPaolo Abeni } 9457a6a6cbcSPaolo Abeni 9463f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 94757040755SPaolo Abeni const struct sk_buff *skb, 94857040755SPaolo Abeni const struct mptcp_ext *mpext) 9496d0060f6SMat Martineau { 95057040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 95157040755SPaolo Abeni return false; 95257040755SPaolo Abeni 9535a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9545a369ca6SPaolo Abeni * mapping has not been xmitted yet 9555a369ca6SPaolo Abeni */ 9565a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9575a369ca6SPaolo Abeni !mpext->frozen; 95857040755SPaolo Abeni } 95957040755SPaolo Abeni 96029249eacSPaolo Abeni /* we can append data to the given data frag if: 96129249eacSPaolo Abeni * - there is space available in the backing page_frag 96229249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 96329249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 96429249eacSPaolo Abeni */ 96518b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 96618b683bfSPaolo Abeni const struct page_frag *pfrag, 96718b683bfSPaolo Abeni const struct mptcp_data_frag *df) 96818b683bfSPaolo Abeni { 96918b683bfSPaolo Abeni return df && pfrag->page == df->page && 970d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 97129249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 97218b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 97318b683bfSPaolo Abeni } 97418b683bfSPaolo Abeni 975d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 97618b683bfSPaolo Abeni { 977d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9787948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 979d027236cSPaolo Abeni } 980d027236cSPaolo Abeni 981d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 982d027236cSPaolo Abeni { 983d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 984d027236cSPaolo Abeni 98518b683bfSPaolo Abeni list_del(&dfrag->list); 986d027236cSPaolo Abeni dfrag_uncharge(sk, len); 98718b683bfSPaolo Abeni put_page(dfrag->page); 98818b683bfSPaolo Abeni } 98918b683bfSPaolo Abeni 9906e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 99118b683bfSPaolo Abeni { 99218b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 99318b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 994e1ff9e82SDavide Caratti u64 snd_una; 995e1ff9e82SDavide Caratti 9967439d687SPaolo Abeni snd_una = msk->snd_una; 99718b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 99818b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 99918b683bfSPaolo Abeni break; 100018b683bfSPaolo Abeni 10011e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10021e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10031e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1004d9ca1de8SPaolo Abeni break; 10051e1d9d6fSPaolo Abeni 10061e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10071e1d9d6fSPaolo Abeni } 10081e1d9d6fSPaolo Abeni 1009d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1010d027236cSPaolo Abeni } 1011d027236cSPaolo Abeni 10127948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10137948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 101453eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 101553eb4c38SPaolo Abeni 10161e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10171e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10181e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 101953eb4c38SPaolo Abeni goto out; 10201e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10211e1d9d6fSPaolo Abeni goto out; 10221e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10231e1d9d6fSPaolo Abeni } 10247948f6ccSFlorian Westphal 10257948f6ccSFlorian Westphal dfrag->data_seq += delta; 102653eb4c38SPaolo Abeni dfrag->offset += delta; 10277948f6ccSFlorian Westphal dfrag->data_len -= delta; 1028d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10297948f6ccSFlorian Westphal 10307948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10317948f6ccSFlorian Westphal } 10327948f6ccSFlorian Westphal 10331e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10341e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10351e1d9d6fSPaolo Abeni msk->recovery = false; 10361e1d9d6fSPaolo Abeni 103753eb4c38SPaolo Abeni out: 10383241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10393241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 1040f6909dc1SPaolo Abeni if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 1041f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 10426e628cd3SPaolo Abeni } else { 1043f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 10446e628cd3SPaolo Abeni } 10456e628cd3SPaolo Abeni } 10466e628cd3SPaolo Abeni 1047417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1048417789dfSPaolo Abeni { 1049765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1050765ff425SPaolo Abeni 1051417789dfSPaolo Abeni __mptcp_clean_una(sk); 1052417789dfSPaolo Abeni mptcp_write_space(sk); 1053417789dfSPaolo Abeni } 1054417789dfSPaolo Abeni 1055b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1056b5941f06SPaolo Abeni { 1057b5941f06SPaolo Abeni mptcp_data_lock(sk); 1058b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1059b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1060b5941f06SPaolo Abeni } 1061b5941f06SPaolo Abeni 1062724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 106318b683bfSPaolo Abeni { 1064d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1065d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1066d9ca1de8SPaolo Abeni bool first = true; 1067d9ca1de8SPaolo Abeni 1068d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1069d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1070d9ca1de8SPaolo Abeni 1071d9ca1de8SPaolo Abeni if (first) 1072d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1073d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 107434c7757aSPaolo Abeni 1075d9ca1de8SPaolo Abeni first = false; 1076d9ca1de8SPaolo Abeni } 107734c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 1078724cfd2eSPaolo Abeni } 1079724cfd2eSPaolo Abeni 1080724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1081724cfd2eSPaolo Abeni * data 1082724cfd2eSPaolo Abeni */ 1083724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1084724cfd2eSPaolo Abeni { 1085724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1086724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1087724cfd2eSPaolo Abeni return true; 1088724cfd2eSPaolo Abeni 1089724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 109018b683bfSPaolo Abeni return false; 109118b683bfSPaolo Abeni } 109218b683bfSPaolo Abeni 109318b683bfSPaolo Abeni static struct mptcp_data_frag * 109418b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 109518b683bfSPaolo Abeni int orig_offset) 109618b683bfSPaolo Abeni { 109718b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 109818b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 109918b683bfSPaolo Abeni 110018b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 110118b683bfSPaolo Abeni dfrag->data_len = 0; 110218b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 110318b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 110418b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1105d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 110618b683bfSPaolo Abeni dfrag->page = pfrag->page; 110718b683bfSPaolo Abeni 110818b683bfSPaolo Abeni return dfrag; 110918b683bfSPaolo Abeni } 111018b683bfSPaolo Abeni 1111caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1112caf971dfSPaolo Abeni int mss_now; 1113caf971dfSPaolo Abeni int size_goal; 1114d9ca1de8SPaolo Abeni u16 limit; 1115d9ca1de8SPaolo Abeni u16 sent; 1116d9ca1de8SPaolo Abeni unsigned int flags; 11171094c6feSPaolo Abeni bool data_lock_held; 1118caf971dfSPaolo Abeni }; 1119caf971dfSPaolo Abeni 1120b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1121b713d006SPaolo Abeni u64 data_seq, int avail_size) 11226f8a612aSFlorian Westphal { 11236f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1124b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11256f8a612aSFlorian Westphal 11266f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11276f8a612aSFlorian Westphal return avail_size; 11286f8a612aSFlorian Westphal 1129b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1130b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11316f8a612aSFlorian Westphal 113292be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1133b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 113492be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 113592be2f52SPaolo Abeni } 11366f8a612aSFlorian Westphal 11376f8a612aSFlorian Westphal return avail_size; 11386f8a612aSFlorian Westphal } 11396f8a612aSFlorian Westphal 1140724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1141724cfd2eSPaolo Abeni { 1142724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1143724cfd2eSPaolo Abeni 1144724cfd2eSPaolo Abeni if (!mpext) 1145724cfd2eSPaolo Abeni return false; 1146724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1147724cfd2eSPaolo Abeni return true; 1148724cfd2eSPaolo Abeni } 1149724cfd2eSPaolo Abeni 11506e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1151724cfd2eSPaolo Abeni { 1152724cfd2eSPaolo Abeni struct sk_buff *skb; 1153724cfd2eSPaolo Abeni 11546e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1155724cfd2eSPaolo Abeni if (likely(skb)) { 11566e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1157724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1158a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1159f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1160724cfd2eSPaolo Abeni return skb; 1161724cfd2eSPaolo Abeni } 1162724cfd2eSPaolo Abeni __kfree_skb(skb); 1163724cfd2eSPaolo Abeni } else { 1164724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1165724cfd2eSPaolo Abeni } 1166724cfd2eSPaolo Abeni return NULL; 1167724cfd2eSPaolo Abeni } 1168724cfd2eSPaolo Abeni 1169f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1170724cfd2eSPaolo Abeni { 1171724cfd2eSPaolo Abeni struct sk_buff *skb; 1172724cfd2eSPaolo Abeni 11736e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1174724cfd2eSPaolo Abeni if (!skb) 1175f70cad10SPaolo Abeni return NULL; 1176724cfd2eSPaolo Abeni 1177724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1178f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1179f70cad10SPaolo Abeni return skb; 1180724cfd2eSPaolo Abeni } 11813ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1182724cfd2eSPaolo Abeni kfree_skb(skb); 1183f70cad10SPaolo Abeni return NULL; 1184724cfd2eSPaolo Abeni } 1185724cfd2eSPaolo Abeni 1186f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1187724cfd2eSPaolo Abeni { 11881094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1189724cfd2eSPaolo Abeni 11901094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1191724cfd2eSPaolo Abeni } 1192724cfd2eSPaolo Abeni 1193d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1194d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1195d0cc2987SGeliang Tang */ 1196d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1197d0cc2987SGeliang Tang { 1198d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1199d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1200d0cc2987SGeliang Tang int offset = skb->len - added; 1201d0cc2987SGeliang Tang 1202d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1203d0cc2987SGeliang Tang } 1204d0cc2987SGeliang Tang 12051e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12061e39e5a3SGeliang Tang struct sock *ssk, 12071e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12081e39e5a3SGeliang Tang { 12091e39e5a3SGeliang Tang if (!mpext) 12101e39e5a3SGeliang Tang return; 12111e39e5a3SGeliang Tang 12121e39e5a3SGeliang Tang mpext->infinite_map = 1; 12131e39e5a3SGeliang Tang mpext->data_len = 0; 12141e39e5a3SGeliang Tang 1215104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12161e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12171e39e5a3SGeliang Tang pr_fallback(msk); 1218d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12191e39e5a3SGeliang Tang } 12201e39e5a3SGeliang Tang 122157ced2ebSPaolo Abeni #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1)) 122257ced2ebSPaolo Abeni 122357040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1224d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1225caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 122657040755SPaolo Abeni { 1227d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1228f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12296d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12306f8a612aSFlorian Westphal bool zero_window_probe = false; 12316d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1232f70cad10SPaolo Abeni bool can_coalesce = false; 1233f70cad10SPaolo Abeni bool reuse_skb = true; 1234f70cad10SPaolo Abeni struct sk_buff *skb; 1235f70cad10SPaolo Abeni size_t copy; 1236f70cad10SPaolo Abeni int i; 12376d0060f6SMat Martineau 1238e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1239d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12406d0060f6SMat Martineau 1241f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1242f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1243f70cad10SPaolo Abeni return 0; 1244f70cad10SPaolo Abeni 1245c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1246c886d702SPaolo Abeni return -EAGAIN; 1247c886d702SPaolo Abeni 1248d9ca1de8SPaolo Abeni /* compute send limit */ 124957ced2ebSPaolo Abeni if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE)) 125057ced2ebSPaolo Abeni ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE; 1251d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1252f70cad10SPaolo Abeni copy = info->size_goal; 1253f70cad10SPaolo Abeni 125457040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1255f70cad10SPaolo Abeni if (skb && copy > skb->len) { 125657040755SPaolo Abeni /* Limit the write to the size available in the 125757040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 125857040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 125957040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 126057040755SPaolo Abeni * SSN association set here 126157040755SPaolo Abeni */ 1262d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12631094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126457040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 126548428b07SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 12661094c6feSPaolo Abeni goto alloc_skb; 12671094c6feSPaolo Abeni } 12681094c6feSPaolo Abeni 1269f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1270f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1271657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1272f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1273f70cad10SPaolo Abeni goto alloc_skb; 127415e6ca97SPaolo Abeni } 127518b683bfSPaolo Abeni 1276f70cad10SPaolo Abeni copy -= skb->len; 1277f70cad10SPaolo Abeni } else { 12781094c6feSPaolo Abeni alloc_skb: 1279f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1280f70cad10SPaolo Abeni if (!skb) 1281f70cad10SPaolo Abeni return -ENOMEM; 1282f70cad10SPaolo Abeni 1283f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1284f70cad10SPaolo Abeni reuse_skb = false; 1285f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1286f70cad10SPaolo Abeni } 12871094c6feSPaolo Abeni 12886f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1289b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1290f70cad10SPaolo Abeni if (copy == 0) { 12917439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12927439d687SPaolo Abeni 129372377ab2SPaolo Abeni if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) { 129427728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12956f8a612aSFlorian Westphal return 0; 12966f8a612aSFlorian Westphal } 12976f8a612aSFlorian Westphal 1298f70cad10SPaolo Abeni zero_window_probe = true; 1299f70cad10SPaolo Abeni data_seq = snd_una - 1; 1300f70cad10SPaolo Abeni copy = 1; 1301f70cad10SPaolo Abeni } 1302f70cad10SPaolo Abeni 1303f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1304f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 130527728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1306e2223995SPaolo Abeni return -ENOMEM; 130735759383SFlorian Westphal } 130818b683bfSPaolo Abeni 1309f70cad10SPaolo Abeni if (can_coalesce) { 1310f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1311f70cad10SPaolo Abeni } else { 1312f70cad10SPaolo Abeni get_page(dfrag->page); 1313f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 131457040755SPaolo Abeni } 131557040755SPaolo Abeni 1316f70cad10SPaolo Abeni skb->len += copy; 1317f70cad10SPaolo Abeni skb->data_len += copy; 1318f70cad10SPaolo Abeni skb->truesize += copy; 1319f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1320f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1321f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1322f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1323f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1324f70cad10SPaolo Abeni 1325f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1326f70cad10SPaolo Abeni if (reuse_skb) { 1327f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1328f70cad10SPaolo Abeni mpext->data_len += copy; 1329f70cad10SPaolo Abeni goto out; 1330724cfd2eSPaolo Abeni } 13316d0060f6SMat Martineau 13326d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1333d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13346d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1335f70cad10SPaolo Abeni mpext->data_len = copy; 13366d0060f6SMat Martineau mpext->use_map = 1; 13376d0060f6SMat Martineau mpext->dsn64 = 1; 13386d0060f6SMat Martineau 13396d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13406d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13416d0060f6SMat Martineau mpext->dsn64); 13426d0060f6SMat Martineau 13436f8a612aSFlorian Westphal if (zero_window_probe) { 1344f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13456f8a612aSFlorian Westphal mpext->frozen = 1; 1346d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1347f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13486f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1349d0cc2987SGeliang Tang return 0; 13506f8a612aSFlorian Westphal } 135157040755SPaolo Abeni out: 1352d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1353f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13541e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13551e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13560eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1357f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1358f70cad10SPaolo Abeni return copy; 13596d0060f6SMat Martineau } 13606d0060f6SMat Martineau 1361d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1362d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1363d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1364d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1365d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1366d5f49190SPaolo Abeni 1367d5f49190SPaolo Abeni struct subflow_send_info { 1368d5f49190SPaolo Abeni struct sock *ssk; 13693ce0852cSPaolo Abeni u64 linger_time; 1370d5f49190SPaolo Abeni }; 1371d5f49190SPaolo Abeni 1372ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1373ff5a0b42SPaolo Abeni { 1374ff5a0b42SPaolo Abeni if (!subflow->stale) 1375ff5a0b42SPaolo Abeni return; 1376ff5a0b42SPaolo Abeni 1377ff5a0b42SPaolo Abeni subflow->stale = 0; 1378fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1379ff5a0b42SPaolo Abeni } 1380ff5a0b42SPaolo Abeni 1381ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1382ff5a0b42SPaolo Abeni { 1383ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1384ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1385ff5a0b42SPaolo Abeni 1386ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1387ff5a0b42SPaolo Abeni return false; 1388ff5a0b42SPaolo Abeni 1389ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1390ff5a0b42SPaolo Abeni } 1391ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1392ff5a0b42SPaolo Abeni } 1393ff5a0b42SPaolo Abeni 13943ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13953ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13963ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13973ce0852cSPaolo Abeni 139833d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 139933d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140033d41c9cSPaolo Abeni * additionally updates the rtx timeout 140133d41c9cSPaolo Abeni */ 140207336a87SGeliang Tang struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1403f296234cSPeter Krystad { 14043ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1405f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 140633d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14073ce0852cSPaolo Abeni u32 pace, burst, wmem; 1408d5f49190SPaolo Abeni int i, nr_active = 0; 1409d5f49190SPaolo Abeni struct sock *ssk; 14103ce0852cSPaolo Abeni u64 linger_time; 141133d41c9cSPaolo Abeni long tout = 0; 1412f296234cSPeter Krystad 1413d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14143ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1415d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14163ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1417d5f49190SPaolo Abeni } 14183ce0852cSPaolo Abeni 1419d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1420add243b7SMatthieu Baerts (NGI0) bool backup = subflow->backup || subflow->request_bkup; 1421add243b7SMatthieu Baerts (NGI0) 1422e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1423d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1424d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1425f296234cSPeter Krystad continue; 1426d5f49190SPaolo Abeni 142733d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1428add243b7SMatthieu Baerts (NGI0) nr_active += !backup; 14293ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14303ce0852cSPaolo Abeni if (unlikely(!pace)) { 14313ce0852cSPaolo Abeni /* init pacing rate from socket */ 14323ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14333ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1434d5f49190SPaolo Abeni if (!pace) 1435d5f49190SPaolo Abeni continue; 14363ce0852cSPaolo Abeni } 1437d5f49190SPaolo Abeni 14383ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 1439add243b7SMatthieu Baerts (NGI0) if (linger_time < send_info[backup].linger_time) { 1440add243b7SMatthieu Baerts (NGI0) send_info[backup].ssk = ssk; 1441add243b7SMatthieu Baerts (NGI0) send_info[backup].linger_time = linger_time; 1442d5f49190SPaolo Abeni } 1443f296234cSPeter Krystad } 144433d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1445f296234cSPeter Krystad 1446d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1447d5f49190SPaolo Abeni if (!nr_active) 14483ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1449d5f49190SPaolo Abeni 14503ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14513ce0852cSPaolo Abeni * faster flow, we need to: 14523ce0852cSPaolo Abeni * - estimate the faster flow linger time 14533ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14543ce0852cSPaolo Abeni * by the faster flow 14553ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14563ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14573ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14583ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14593ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14603ce0852cSPaolo Abeni */ 14613ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1462b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1463d5f49190SPaolo Abeni return NULL; 14643ce0852cSPaolo Abeni 1465b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14663ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1467ebc1e08fSGeliang Tang if (!burst) 1468b713d006SPaolo Abeni return ssk; 1469b713d006SPaolo Abeni 14703ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14713ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14723ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14733ce0852cSPaolo Abeni burst + wmem); 14743ce0852cSPaolo Abeni msk->snd_burst = burst; 14753ce0852cSPaolo Abeni return ssk; 1476f296234cSPeter Krystad } 1477f296234cSPeter Krystad 1478b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1479d9ca1de8SPaolo Abeni { 1480d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1481d9ca1de8SPaolo Abeni release_sock(ssk); 1482d9ca1de8SPaolo Abeni } 1483d9ca1de8SPaolo Abeni 14840d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14850d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14860d199e43SFlorian Westphal u32 sent) 14870d199e43SFlorian Westphal { 14880d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 14890d199e43SFlorian Westphal 14900d199e43SFlorian Westphal dfrag->already_sent += sent; 14910d199e43SFlorian Westphal 14920d199e43SFlorian Westphal msk->snd_burst -= sent; 14930d199e43SFlorian Westphal 14940d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 14950d199e43SFlorian Westphal 14960d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 14970d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 14980d199e43SFlorian Westphal * old segments. 14990d199e43SFlorian Westphal * 15000d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15010d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15020d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15030d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15040d199e43SFlorian Westphal */ 150538967f42SPaolo Abeni if (likely(after64(snd_nxt_new, msk->snd_nxt))) { 150638967f42SPaolo Abeni msk->bytes_sent += snd_nxt_new - msk->snd_nxt; 15070d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15080d199e43SFlorian Westphal } 150938967f42SPaolo Abeni } 15100d199e43SFlorian Westphal 15118b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15123241a9c0SFlorian Westphal { 1513877a72e3SPaolo Abeni if (mptcp_send_head(sk)) { 1514877a72e3SPaolo Abeni mptcp_data_lock(sk); 1515877a72e3SPaolo Abeni mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING); 1516877a72e3SPaolo Abeni mptcp_data_unlock(sk); 1517877a72e3SPaolo Abeni } 15183241a9c0SFlorian Westphal } 15193241a9c0SFlorian Westphal 1520c5b4297dSGeliang Tang static int __subflow_push_pending(struct sock *sk, struct sock *ssk, 1521c5b4297dSGeliang Tang struct mptcp_sendmsg_info *info) 1522c5b4297dSGeliang Tang { 1523c5b4297dSGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 1524c5b4297dSGeliang Tang struct mptcp_data_frag *dfrag; 1525c5b4297dSGeliang Tang int len, copied = 0, err = 0; 1526c5b4297dSGeliang Tang 1527c5b4297dSGeliang Tang while ((dfrag = mptcp_send_head(sk))) { 1528c5b4297dSGeliang Tang info->sent = dfrag->already_sent; 1529c5b4297dSGeliang Tang info->limit = dfrag->data_len; 1530c5b4297dSGeliang Tang len = dfrag->data_len - dfrag->already_sent; 1531c5b4297dSGeliang Tang while (len > 0) { 1532c5b4297dSGeliang Tang int ret = 0; 1533c5b4297dSGeliang Tang 1534c5b4297dSGeliang Tang ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info); 1535c5b4297dSGeliang Tang if (ret <= 0) { 1536c5b4297dSGeliang Tang err = copied ? : ret; 1537c5b4297dSGeliang Tang goto out; 1538c5b4297dSGeliang Tang } 1539c5b4297dSGeliang Tang 1540c5b4297dSGeliang Tang info->sent += ret; 1541c5b4297dSGeliang Tang copied += ret; 1542c5b4297dSGeliang Tang len -= ret; 1543c5b4297dSGeliang Tang 1544c5b4297dSGeliang Tang mptcp_update_post_push(msk, dfrag, ret); 1545c5b4297dSGeliang Tang } 1546c5b4297dSGeliang Tang WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1547c5b4297dSGeliang Tang 1548c5b4297dSGeliang Tang if (msk->snd_burst <= 0 || 1549c5b4297dSGeliang Tang !sk_stream_memory_free(ssk) || 1550c5b4297dSGeliang Tang !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) { 1551c5b4297dSGeliang Tang err = copied; 1552c5b4297dSGeliang Tang goto out; 1553c5b4297dSGeliang Tang } 1554c5b4297dSGeliang Tang mptcp_set_timeout(sk); 1555c5b4297dSGeliang Tang } 1556c5b4297dSGeliang Tang err = copied; 1557c5b4297dSGeliang Tang 1558c5b4297dSGeliang Tang out: 1559c5b4297dSGeliang Tang return err; 1560c5b4297dSGeliang Tang } 1561c5b4297dSGeliang Tang 1562ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1563d9ca1de8SPaolo Abeni { 1564d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1565d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1566d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1567d9ca1de8SPaolo Abeni .flags = flags, 1568d9ca1de8SPaolo Abeni }; 15690522b424SGeliang Tang bool do_check_data_fin = false; 15700fa1b378SGeliang Tang int push_count = 1; 1571d9ca1de8SPaolo Abeni 15720fa1b378SGeliang Tang while (mptcp_send_head(sk) && (push_count > 0)) { 15730fa1b378SGeliang Tang struct mptcp_subflow_context *subflow; 1574d9ca1de8SPaolo Abeni int ret = 0; 1575d9ca1de8SPaolo Abeni 15760fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 15770fa1b378SGeliang Tang break; 1578d9ca1de8SPaolo Abeni 15790fa1b378SGeliang Tang push_count = 0; 15800fa1b378SGeliang Tang 15810fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 15820fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 15830fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 15840fa1b378SGeliang Tang 15850fa1b378SGeliang Tang prev_ssk = ssk; 15860fa1b378SGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 15870fa1b378SGeliang Tang if (ssk != prev_ssk) { 15889758f40eSPaolo Abeni /* First check. If the ssk has changed since 15899758f40eSPaolo Abeni * the last round, release prev_ssk 1590d9ca1de8SPaolo Abeni */ 15910fa1b378SGeliang Tang if (prev_ssk) 1592b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1593d9ca1de8SPaolo Abeni 15949758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15959758f40eSPaolo Abeni * from the previous one, otherwise we are still 15969758f40eSPaolo Abeni * helding the relevant lock 15979758f40eSPaolo Abeni */ 1598d9ca1de8SPaolo Abeni lock_sock(ssk); 15990fa1b378SGeliang Tang } 16000fa1b378SGeliang Tang 16010fa1b378SGeliang Tang push_count++; 1602d9ca1de8SPaolo Abeni 1603c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1604d9ca1de8SPaolo Abeni if (ret <= 0) { 16050fa1b378SGeliang Tang if (ret != -EAGAIN || 16060fa1b378SGeliang Tang (1 << ssk->sk_state) & 16070fa1b378SGeliang Tang (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE)) 16080fa1b378SGeliang Tang push_count--; 1609c886d702SPaolo Abeni continue; 1610d9ca1de8SPaolo Abeni } 16110522b424SGeliang Tang do_check_data_fin = true; 1612d9ca1de8SPaolo Abeni } 16130fa1b378SGeliang Tang } 16140fa1b378SGeliang Tang } 1615d9ca1de8SPaolo Abeni 1616d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1617d9ca1de8SPaolo Abeni if (ssk) 1618b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1619d9ca1de8SPaolo Abeni 162033d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1621f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 1622f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 16230522b424SGeliang Tang if (do_check_data_fin) 162481c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 1625d9ca1de8SPaolo Abeni } 1626d9ca1de8SPaolo Abeni 162773a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 16286e628cd3SPaolo Abeni { 16296e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16301094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16311094c6feSPaolo Abeni .data_lock_held = true, 16321094c6feSPaolo Abeni }; 16330fa1b378SGeliang Tang bool keep_pushing = true; 1634b19bc294SPaolo Abeni struct sock *xmit_ssk; 1635c5b4297dSGeliang Tang int copied = 0; 16366e628cd3SPaolo Abeni 16376e628cd3SPaolo Abeni info.flags = 0; 16380fa1b378SGeliang Tang while (mptcp_send_head(sk) && keep_pushing) { 16390fa1b378SGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 16406e628cd3SPaolo Abeni int ret = 0; 16416e628cd3SPaolo Abeni 164273a0052aSGeliang Tang /* check for a different subflow usage only after 1643b19bc294SPaolo Abeni * spooling the first chunk of data 1644b19bc294SPaolo Abeni */ 16450fa1b378SGeliang Tang if (first) { 16460fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 1647c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1648b19bc294SPaolo Abeni first = false; 1649c5b4297dSGeliang Tang if (ret <= 0) 1650c5b4297dSGeliang Tang break; 1651c5b4297dSGeliang Tang copied += ret; 16520fa1b378SGeliang Tang continue; 16530fa1b378SGeliang Tang } 16540fa1b378SGeliang Tang 16550fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 16560fa1b378SGeliang Tang goto out; 16570fa1b378SGeliang Tang 16580fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16590fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 16600fa1b378SGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 16610fa1b378SGeliang Tang if (ret <= 0) 16620fa1b378SGeliang Tang keep_pushing = false; 16630fa1b378SGeliang Tang copied += ret; 16640fa1b378SGeliang Tang } 16650fa1b378SGeliang Tang 16660fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 16670fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16680fa1b378SGeliang Tang xmit_ssk = mptcp_subflow_tcp_sock(subflow); 16690fa1b378SGeliang Tang if (xmit_ssk != ssk) { 16700fa1b378SGeliang Tang mptcp_subflow_delegate(subflow, 16710fa1b378SGeliang Tang MPTCP_DELEGATE_SEND); 16720fa1b378SGeliang Tang keep_pushing = false; 16730fa1b378SGeliang Tang } 16740fa1b378SGeliang Tang } 16750fa1b378SGeliang Tang } 16766e628cd3SPaolo Abeni } 16776e628cd3SPaolo Abeni 16786e628cd3SPaolo Abeni out: 16796e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16806e628cd3SPaolo Abeni * not going to flush it via release_sock() 16816e628cd3SPaolo Abeni */ 16826e628cd3SPaolo Abeni if (copied) { 16836e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16846e628cd3SPaolo Abeni info.size_goal); 1685f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 1686f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 1687d09d818eSPaolo Abeni 16886e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16896e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16906e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16916e628cd3SPaolo Abeni } 16926e628cd3SPaolo Abeni } 16936e628cd3SPaolo Abeni 16945cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16955cf92bbaSPaolo Abeni { 16965cf92bbaSPaolo Abeni /* enable autotune */ 16975cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16985cf92bbaSPaolo Abeni 16995cf92bbaSPaolo Abeni /* will be cleared on avail space */ 17005cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 17015cf92bbaSPaolo Abeni } 17025cf92bbaSPaolo Abeni 17037d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 17047d803344SPaolo Abeni 1705a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1706fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1707fa9e5746SPaolo Abeni { 1708fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1709fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1710a2702a07SPaolo Abeni struct sock *ssk; 1711fa9e5746SPaolo Abeni int ret; 1712fa9e5746SPaolo Abeni 1713a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1714a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1715a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1716a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1717a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1718a2702a07SPaolo Abeni */ 1719ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 17203f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 17213f326a82SPaolo Abeni if (IS_ERR(ssk)) 17223f326a82SPaolo Abeni return PTR_ERR(ssk); 1723ddb1a072SPaolo Abeni } 1724a2702a07SPaolo Abeni if (!msk->first) 1725a2702a07SPaolo Abeni return -EINVAL; 1726a2702a07SPaolo Abeni 1727a2702a07SPaolo Abeni ssk = msk->first; 1728a2702a07SPaolo Abeni 1729fa9e5746SPaolo Abeni lock_sock(ssk); 1730fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 17317d803344SPaolo Abeni msk->fastopening = 1; 1732fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 17337d803344SPaolo Abeni msk->fastopening = 0; 1734fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1735fa9e5746SPaolo Abeni release_sock(ssk); 1736fa9e5746SPaolo Abeni 1737fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1738fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1739fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1740fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1741fa9e5746SPaolo Abeni 1742fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1743fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1744fa9e5746SPaolo Abeni */ 1745fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1746fa9e5746SPaolo Abeni *copied_syn = 0; 17477d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 1748c2b2ae39SPaolo Abeni /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1749c2b2ae39SPaolo Abeni * __inet_stream_connect() can fail, due to looking check, 1750c2b2ae39SPaolo Abeni * see mptcp_disconnect(). 1751c2b2ae39SPaolo Abeni * Attempt it again outside the problematic scope. 1752c2b2ae39SPaolo Abeni */ 1753c2b2ae39SPaolo Abeni if (!mptcp_disconnect(sk, 0)) 1754c2b2ae39SPaolo Abeni sk->sk_socket->state = SS_UNCONNECTED; 1755fa9e5746SPaolo Abeni } 175608e39c0dSEric Dumazet inet_clear_bit(DEFER_CONNECT, sk); 1757fa9e5746SPaolo Abeni 1758fa9e5746SPaolo Abeni return ret; 1759fa9e5746SPaolo Abeni } 1760fa9e5746SPaolo Abeni 1761f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1762f870fa0bSMat Martineau { 1763f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 176417091708SFlorian Westphal struct page_frag *pfrag; 17656d0060f6SMat Martineau size_t copied = 0; 1766caf971dfSPaolo Abeni int ret = 0; 17676d0060f6SMat Martineau long timeo; 1768f870fa0bSMat Martineau 1769987858e5SPaolo Abeni /* silently ignore everything else */ 17701e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1771987858e5SPaolo Abeni 17726511882cSPaolo Abeni lock_sock(sk); 17731954b860SMat Martineau 177408e39c0dSEric Dumazet if (unlikely(inet_test_bit(DEFER_CONNECT, sk) || 177508e39c0dSEric Dumazet msg->msg_flags & MSG_FASTOPEN)) { 1776d98a82a6SDmytro Shytyi int copied_syn = 0; 1777d98a82a6SDmytro Shytyi 1778a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1779d98a82a6SDmytro Shytyi copied += copied_syn; 1780fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1781d98a82a6SDmytro Shytyi goto out; 1782fa9e5746SPaolo Abeni else if (ret) 178369800e51SPaolo Abeni goto do_error; 1784d98a82a6SDmytro Shytyi } 1785d98a82a6SDmytro Shytyi 17861954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17871954b860SMat Martineau 17881954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17891954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17901954b860SMat Martineau if (ret) 179169800e51SPaolo Abeni goto do_error; 17921954b860SMat Martineau } 17931954b860SMat Martineau 179469800e51SPaolo Abeni ret = -EPIPE; 179569800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 179669800e51SPaolo Abeni goto do_error; 179769800e51SPaolo Abeni 179817091708SFlorian Westphal pfrag = sk_page_frag(sk); 179918b683bfSPaolo Abeni 1800d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1801724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1802d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1803d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1804d9ca1de8SPaolo Abeni size_t psize, offset; 1805d9ca1de8SPaolo Abeni 1806d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1807d9ca1de8SPaolo Abeni * page allocator 1808fb529e62SFlorian Westphal */ 1809d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1810d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1811d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1812d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1813d9ca1de8SPaolo Abeni goto wait_for_memory; 18146e628cd3SPaolo Abeni 1815d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1816d9ca1de8SPaolo Abeni goto wait_for_memory; 1817d9ca1de8SPaolo Abeni 1818d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1819d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1820fb529e62SFlorian Westphal } 1821fb529e62SFlorian Westphal 1822d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1823d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1824d9ca1de8SPaolo Abeni * anyway 1825d9ca1de8SPaolo Abeni */ 1826d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1827d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1828d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1829724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 18308ce568edSPaolo Abeni 18316511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1832d9ca1de8SPaolo Abeni goto wait_for_memory; 1833d9ca1de8SPaolo Abeni 1834d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1835d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1836d9ca1de8SPaolo Abeni ret = -EFAULT; 183769800e51SPaolo Abeni goto do_error; 1838d9ca1de8SPaolo Abeni } 1839d9ca1de8SPaolo Abeni 1840d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 18415e6300e7SEric Dumazet sk_forward_alloc_add(sk, -total_ts); 1842d9ca1de8SPaolo Abeni copied += psize; 1843d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1844d9ca1de8SPaolo Abeni frag_truesize += psize; 1845d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1846d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1847d9ca1de8SPaolo Abeni 1848d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1849d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1850d9ca1de8SPaolo Abeni */ 1851d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1852d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1853d9ca1de8SPaolo Abeni get_page(dfrag->page); 1854d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1855d9ca1de8SPaolo Abeni if (!msk->first_pending) 1856d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1857d9ca1de8SPaolo Abeni } 1858e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1859d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1860d9ca1de8SPaolo Abeni !dfrag_collapsed); 1861d9ca1de8SPaolo Abeni 1862d9ca1de8SPaolo Abeni continue; 1863d9ca1de8SPaolo Abeni 1864d9ca1de8SPaolo Abeni wait_for_memory: 18655cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1866c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1867f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1868f296234cSPeter Krystad if (ret) 186969800e51SPaolo Abeni goto do_error; 1870cec37a6eSPeter Krystad } 1871cec37a6eSPeter Krystad 187213e16037SPaolo Abeni if (copied) 1873c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1874da51aef5SPaolo Abeni 18751954b860SMat Martineau out: 1876cec37a6eSPeter Krystad release_sock(sk); 187769800e51SPaolo Abeni return copied; 187869800e51SPaolo Abeni 187969800e51SPaolo Abeni do_error: 188069800e51SPaolo Abeni if (copied) 188169800e51SPaolo Abeni goto out; 188269800e51SPaolo Abeni 188369800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 188469800e51SPaolo Abeni goto out; 1885f870fa0bSMat Martineau } 1886f870fa0bSMat Martineau 18876771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18886771bfd9SFlorian Westphal struct msghdr *msg, 1889b7f653b2SFlorian Westphal size_t len, int flags, 1890b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1891b7f653b2SFlorian Westphal int *cmsg_flags) 18926771bfd9SFlorian Westphal { 1893ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18946771bfd9SFlorian Westphal int copied = 0; 18956771bfd9SFlorian Westphal 1896ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18976771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18986771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18996771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 19006771bfd9SFlorian Westphal int err; 19016771bfd9SFlorian Westphal 1902d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 19036771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 19046771bfd9SFlorian Westphal if (unlikely(err < 0)) { 19056771bfd9SFlorian Westphal if (!copied) 19066771bfd9SFlorian Westphal return err; 19076771bfd9SFlorian Westphal break; 19086771bfd9SFlorian Westphal } 1909d976092cSPaolo Abeni } 19106771bfd9SFlorian Westphal 1911b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1912b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1913b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1914b7f653b2SFlorian Westphal } 1915b7f653b2SFlorian Westphal 19166771bfd9SFlorian Westphal copied += count; 19176771bfd9SFlorian Westphal 19186771bfd9SFlorian Westphal if (count < data_len) { 19192c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 19206771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 19212c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 1922124a4885SPaolo Abeni msk->bytes_consumed += count; 19232c9e7765SFlorian Westphal } 19246771bfd9SFlorian Westphal break; 19256771bfd9SFlorian Westphal } 19266771bfd9SFlorian Westphal 1927ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 192887952603SPaolo Abeni /* we will bulk release the skb memory later */ 192987952603SPaolo Abeni skb->destructor = NULL; 1930ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 193187952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 19326771bfd9SFlorian Westphal __kfree_skb(skb); 1933124a4885SPaolo Abeni msk->bytes_consumed += count; 1934ca4fb892SYonglong Li } 19356771bfd9SFlorian Westphal 19366771bfd9SFlorian Westphal if (copied >= len) 19376771bfd9SFlorian Westphal break; 19386771bfd9SFlorian Westphal } 19396771bfd9SFlorian Westphal 19406771bfd9SFlorian Westphal return copied; 19416771bfd9SFlorian Westphal } 19426771bfd9SFlorian Westphal 1943a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1944a6b118feSFlorian Westphal * 1945a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1946a6b118feSFlorian Westphal */ 1947a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1948a6b118feSFlorian Westphal { 1949a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1950a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1951b8dc6d6cSPaolo Abeni u8 scaling_ratio = U8_MAX; 1952a6b118feSFlorian Westphal u32 time, advmss = 1; 1953a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1954a6b118feSFlorian Westphal 1955109cdeb8SGeliang Tang msk_owned_by_me(msk); 1956a6b118feSFlorian Westphal 1957a6b118feSFlorian Westphal if (copied <= 0) 1958a6b118feSFlorian Westphal return; 1959a6b118feSFlorian Westphal 1960a2d74374SPaolo Abeni if (!msk->rcvspace_init) 1961a2d74374SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 1962a2d74374SPaolo Abeni 1963a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1964a6b118feSFlorian Westphal 1965a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1966a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1967a6b118feSFlorian Westphal 1968a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1969a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1970a6b118feSFlorian Westphal return; 1971a6b118feSFlorian Westphal 1972a6b118feSFlorian Westphal rtt_us = 0; 1973a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1974a6b118feSFlorian Westphal const struct tcp_sock *tp; 1975a6b118feSFlorian Westphal u64 sf_rtt_us; 1976a6b118feSFlorian Westphal u32 sf_advmss; 1977a6b118feSFlorian Westphal 1978a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1979a6b118feSFlorian Westphal 1980a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1981a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1982a6b118feSFlorian Westphal 1983a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1984a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1985b8dc6d6cSPaolo Abeni scaling_ratio = min(tp->scaling_ratio, scaling_ratio); 1986a6b118feSFlorian Westphal } 1987a6b118feSFlorian Westphal 1988a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1989b8dc6d6cSPaolo Abeni msk->scaling_ratio = scaling_ratio; 1990a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1991a6b118feSFlorian Westphal return; 1992a6b118feSFlorian Westphal 1993a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1994a6b118feSFlorian Westphal goto new_measure; 1995a6b118feSFlorian Westphal 199678047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1997a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1998a6b118feSFlorian Westphal u64 rcvwin, grow; 1999b8dc6d6cSPaolo Abeni int rcvbuf; 2000a6b118feSFlorian Westphal 2001a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 2002a6b118feSFlorian Westphal 2003a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 2004a6b118feSFlorian Westphal 2005a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 2006a6b118feSFlorian Westphal rcvwin += (grow << 1); 2007a6b118feSFlorian Westphal 2008b8dc6d6cSPaolo Abeni rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin), 200902739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 2010a6b118feSFlorian Westphal 2011a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 2012a6b118feSFlorian Westphal u32 window_clamp; 2013a6b118feSFlorian Westphal 2014b8dc6d6cSPaolo Abeni window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf); 2015a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 2016a6b118feSFlorian Westphal 2017a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 2018a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 2019a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 2020a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 2021a6b118feSFlorian Westphal */ 2022a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 2023a6b118feSFlorian Westphal struct sock *ssk; 2024c76c6956SPaolo Abeni bool slow; 2025a6b118feSFlorian Westphal 2026a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 2027c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 2028a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 2029f9fef23aSEric Dumazet WRITE_ONCE(tcp_sk(ssk)->window_clamp, window_clamp); 2030c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 2031c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 2032a6b118feSFlorian Westphal } 2033a6b118feSFlorian Westphal } 2034a6b118feSFlorian Westphal } 2035a6b118feSFlorian Westphal 2036a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 2037a6b118feSFlorian Westphal new_measure: 2038a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2039a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 2040a6b118feSFlorian Westphal } 2041a6b118feSFlorian Westphal 204287952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 204387952603SPaolo Abeni { 204487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 204587952603SPaolo Abeni 204687952603SPaolo Abeni if (!msk->rmem_released) 204787952603SPaolo Abeni return; 204887952603SPaolo Abeni 204987952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 20506511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 2051ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 205287952603SPaolo Abeni } 205387952603SPaolo Abeni 205487952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 205587952603SPaolo Abeni { 205687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 205787952603SPaolo Abeni 205887952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 205987952603SPaolo Abeni } 206087952603SPaolo Abeni 2061e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20626771bfd9SFlorian Westphal { 206387952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20646771bfd9SFlorian Westphal unsigned int moved = 0; 206587952603SPaolo Abeni bool ret, done; 2066d5f49190SPaolo Abeni 20676771bfd9SFlorian Westphal do { 20686771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 206965f49fe7SFlorian Westphal bool slowpath; 20706771bfd9SFlorian Westphal 207187952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 207287952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 207387952603SPaolo Abeni * that is an unlikely slow path. 207487952603SPaolo Abeni */ 207587952603SPaolo Abeni if (likely(!ssk)) 20766771bfd9SFlorian Westphal break; 20776771bfd9SFlorian Westphal 207865f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 207987952603SPaolo Abeni mptcp_data_lock(sk); 2080e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20816771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 208287952603SPaolo Abeni mptcp_data_unlock(sk); 2083499ada50SPaolo Abeni 2084499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2085499ada50SPaolo Abeni __mptcp_error_report(sk); 208665f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20876771bfd9SFlorian Westphal } while (!done); 20886771bfd9SFlorian Westphal 208987952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 209087952603SPaolo Abeni ret = moved > 0; 209187952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 209287952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 209387952603SPaolo Abeni mptcp_data_lock(sk); 209487952603SPaolo Abeni __mptcp_update_rmem(sk); 209587952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 209687952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 209787952603SPaolo Abeni mptcp_data_unlock(sk); 2098ab174ad8SPaolo Abeni } 209987952603SPaolo Abeni if (ret) 210087952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 210187952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 21026771bfd9SFlorian Westphal } 21036771bfd9SFlorian Westphal 21042c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 21052c9e7765SFlorian Westphal { 21062c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 21072c9e7765SFlorian Westphal const struct sk_buff *skb; 21082c9e7765SFlorian Westphal 21092c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 21102c9e7765SFlorian Westphal if (skb) { 21112c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 21122c9e7765SFlorian Westphal 21132c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 21142c9e7765SFlorian Westphal return INT_MAX; 21152c9e7765SFlorian Westphal 21162c9e7765SFlorian Westphal return (unsigned int)hint_val; 21172c9e7765SFlorian Westphal } 21182c9e7765SFlorian Westphal 21192c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 21202c9e7765SFlorian Westphal return 1; 21212c9e7765SFlorian Westphal 21222c9e7765SFlorian Westphal return 0; 21232c9e7765SFlorian Westphal } 21242c9e7765SFlorian Westphal 2125f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2126ec095263SOliver Hartkopp int flags, int *addr_len) 2127f870fa0bSMat Martineau { 2128f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2129b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2130b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 21317a6a6cbcSPaolo Abeni int target; 21327a6a6cbcSPaolo Abeni long timeo; 2133f870fa0bSMat Martineau 2134cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2135cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2136cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2137cb9d80f4SPaolo Abeni 21386511882cSPaolo Abeni lock_sock(sk); 2139fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2140fd897679SPaolo Abeni copied = -ENOTCONN; 2141fd897679SPaolo Abeni goto out_err; 2142fd897679SPaolo Abeni } 2143fd897679SPaolo Abeni 2144ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 21457a6a6cbcSPaolo Abeni 21467a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 21477a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 21487a6a6cbcSPaolo Abeni 21492c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 21502c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 21512c9e7765SFlorian Westphal 215205e3eceaSEric Dumazet while (copied < len) { 2153e3859603SPaolo Abeni int bytes_read; 21547a6a6cbcSPaolo Abeni 2155b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21566771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21577a6a6cbcSPaolo Abeni if (!copied) 21587a6a6cbcSPaolo Abeni copied = bytes_read; 21596771bfd9SFlorian Westphal goto out_err; 2160cec37a6eSPeter Krystad } 2161cec37a6eSPeter Krystad 21627a6a6cbcSPaolo Abeni copied += bytes_read; 21637a6a6cbcSPaolo Abeni 2164ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2165fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2166ea4ca586SPaolo Abeni 2167e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2168e3859603SPaolo Abeni continue; 2169e3859603SPaolo Abeni 21707a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21717a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21727a6a6cbcSPaolo Abeni */ 21737a6a6cbcSPaolo Abeni if (copied >= target) 21747a6a6cbcSPaolo Abeni break; 21757a6a6cbcSPaolo Abeni 21767a6a6cbcSPaolo Abeni if (copied) { 21777a6a6cbcSPaolo Abeni if (sk->sk_err || 21787a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21797a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21807a6a6cbcSPaolo Abeni !timeo || 21817a6a6cbcSPaolo Abeni signal_pending(current)) 21827a6a6cbcSPaolo Abeni break; 21837a6a6cbcSPaolo Abeni } else { 21847a6a6cbcSPaolo Abeni if (sk->sk_err) { 21857a6a6cbcSPaolo Abeni copied = sock_error(sk); 21867a6a6cbcSPaolo Abeni break; 21877a6a6cbcSPaolo Abeni } 21887a6a6cbcSPaolo Abeni 218987952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 219087952603SPaolo Abeni /* race breaker: the shutdown could be after the 219187952603SPaolo Abeni * previous receive queue check 219287952603SPaolo Abeni */ 2193e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 219487952603SPaolo Abeni continue; 21957a6a6cbcSPaolo Abeni break; 219687952603SPaolo Abeni } 21977a6a6cbcSPaolo Abeni 21987a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21997a6a6cbcSPaolo Abeni copied = -ENOTCONN; 22007a6a6cbcSPaolo Abeni break; 22017a6a6cbcSPaolo Abeni } 22027a6a6cbcSPaolo Abeni 22037a6a6cbcSPaolo Abeni if (!timeo) { 22047a6a6cbcSPaolo Abeni copied = -EAGAIN; 22057a6a6cbcSPaolo Abeni break; 22067a6a6cbcSPaolo Abeni } 22077a6a6cbcSPaolo Abeni 22087a6a6cbcSPaolo Abeni if (signal_pending(current)) { 22097a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 22107a6a6cbcSPaolo Abeni break; 22117a6a6cbcSPaolo Abeni } 22127a6a6cbcSPaolo Abeni } 22137a6a6cbcSPaolo Abeni 22147a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2215612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 22167a6a6cbcSPaolo Abeni } 22173c90e377SPaolo Abeni 22186771bfd9SFlorian Westphal out_err: 2219b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2220b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2221b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 22222c9e7765SFlorian Westphal 22232c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 22242c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 22252c9e7765SFlorian Westphal 22262c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 22272c9e7765SFlorian Westphal } 2228b7f653b2SFlorian Westphal } 2229b7f653b2SFlorian Westphal 2230612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2231612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2232612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2233ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2234a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2235a6b118feSFlorian Westphal 2236cec37a6eSPeter Krystad release_sock(sk); 2237cec37a6eSPeter Krystad return copied; 2238cec37a6eSPeter Krystad } 2239cec37a6eSPeter Krystad 2240b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2241b51f9b80SPaolo Abeni { 2242b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2243b51f9b80SPaolo Abeni icsk_retransmit_timer); 2244b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 22452d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2246b51f9b80SPaolo Abeni 2247b51f9b80SPaolo Abeni bh_lock_sock(sk); 2248b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22492d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22502d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22512d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2252b51f9b80SPaolo Abeni } else { 2253b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2254e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2255b51f9b80SPaolo Abeni } 2256b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2257b51f9b80SPaolo Abeni sock_put(sk); 2258b51f9b80SPaolo Abeni } 2259b51f9b80SPaolo Abeni 2260f6909dc1SPaolo Abeni static void mptcp_tout_timer(struct timer_list *t) 2261e16163b6SPaolo Abeni { 2262e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2263e16163b6SPaolo Abeni 2264e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2265b6d69fc8SFlorian Westphal sock_put(sk); 2266e16163b6SPaolo Abeni } 2267e16163b6SPaolo Abeni 22683b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22693b1d6210SPaolo Abeni * level. 22703b1d6210SPaolo Abeni * 22713b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22723b1d6210SPaolo Abeni */ 227307336a87SGeliang Tang struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22743b1d6210SPaolo Abeni { 227571b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22763b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 227771b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22783b1d6210SPaolo Abeni 22793b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22803b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22813b1d6210SPaolo Abeni 2282ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2283d5f49190SPaolo Abeni continue; 2284d5f49190SPaolo Abeni 228571b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 228671b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 228771b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 228871b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2289860975c6SFlorian Westphal continue; 2290860975c6SFlorian Westphal } 22913b1d6210SPaolo Abeni 2292*9e40cd79SMatthieu Baerts (NGI0) if (subflow->backup || subflow->request_bkup) { 22933b1d6210SPaolo Abeni if (!backup) 22943b1d6210SPaolo Abeni backup = ssk; 22953b1d6210SPaolo Abeni continue; 22963b1d6210SPaolo Abeni } 22973b1d6210SPaolo Abeni 229871b7dec2SPaolo Abeni if (!pick) 229971b7dec2SPaolo Abeni pick = ssk; 23003b1d6210SPaolo Abeni } 23013b1d6210SPaolo Abeni 230271b7dec2SPaolo Abeni if (pick) 230371b7dec2SPaolo Abeni return pick; 230471b7dec2SPaolo Abeni 230571b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 230671b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 23073b1d6210SPaolo Abeni } 23083b1d6210SPaolo Abeni 23091e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 23101e1d9d6fSPaolo Abeni { 23111e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 23121e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 23131e1d9d6fSPaolo Abeni 231400df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 23151e1d9d6fSPaolo Abeni return false; 23161e1d9d6fSPaolo Abeni 23171e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 23181e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 23191e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 23201e1d9d6fSPaolo Abeni */ 23211e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 23221e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 23231e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 23241e1d9d6fSPaolo Abeni if (!rtx_head) { 23251e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23261e1d9d6fSPaolo Abeni return false; 23271e1d9d6fSPaolo Abeni } 23281e1d9d6fSPaolo Abeni 23291e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 23301e1d9d6fSPaolo Abeni msk->recovery = true; 23311e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23321e1d9d6fSPaolo Abeni 23331e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23341e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23351e1d9d6fSPaolo Abeni 23361e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23371e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23381e1d9d6fSPaolo Abeni if (!cur->already_sent) 23391e1d9d6fSPaolo Abeni break; 23401e1d9d6fSPaolo Abeni cur->already_sent = 0; 23411e1d9d6fSPaolo Abeni } 23421e1d9d6fSPaolo Abeni 23431e1d9d6fSPaolo Abeni return true; 23441e1d9d6fSPaolo Abeni } 23451e1d9d6fSPaolo Abeni 2346b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2347b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2348b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2349b29fcfb5SPaolo Abeni 235014c56686SGeliang Tang /* be sure to send a reset only if the caller asked for it, also 235114c56686SGeliang Tang * clean completely the subflow status when the subflow reaches 235214c56686SGeliang Tang * TCP_CLOSE state 235314c56686SGeliang Tang */ 235414c56686SGeliang Tang static void __mptcp_subflow_disconnect(struct sock *ssk, 235514c56686SGeliang Tang struct mptcp_subflow_context *subflow, 235614c56686SGeliang Tang unsigned int flags) 235714c56686SGeliang Tang { 235814c56686SGeliang Tang if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) || 235914c56686SGeliang Tang (flags & MPTCP_CF_FASTCLOSE)) { 236014c56686SGeliang Tang /* The MPTCP code never wait on the subflow sockets, TCP-level 236114c56686SGeliang Tang * disconnect should never fail 236214c56686SGeliang Tang */ 236314c56686SGeliang Tang WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 236414c56686SGeliang Tang mptcp_subflow_ctx_reset(subflow); 236514c56686SGeliang Tang } else { 236614c56686SGeliang Tang tcp_shutdown(ssk, SEND_SHUTDOWN); 236714c56686SGeliang Tang } 236814c56686SGeliang Tang } 236914c56686SGeliang Tang 2370cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2371cec37a6eSPeter Krystad * (accept). 2372cec37a6eSPeter Krystad * 2373cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2374cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2375cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2376cec37a6eSPeter Krystad * parent socket. 2377cec37a6eSPeter Krystad */ 2378a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2379b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2380b29fcfb5SPaolo Abeni unsigned int flags) 2381cec37a6eSPeter Krystad { 2382e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 238363740448SPaolo Abeni bool dispose_it, need_push = false; 238463740448SPaolo Abeni 238563740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 238627e5ccc2SPaolo Abeni * to an incoming reset or listener shutdown, the subflow socket is 238727e5ccc2SPaolo Abeni * already deleted by inet_child_forget() and the mptcp socket can't 238827e5ccc2SPaolo Abeni * survive too. 238963740448SPaolo Abeni */ 239027e5ccc2SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk && 239127e5ccc2SPaolo Abeni (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) { 239263740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 239327e5ccc2SPaolo Abeni mptcp_set_close_tout(sk, tcp_jiffies32 - (TCP_TIMEWAIT_LEN + 1)); 239463740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 239563740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 239663740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 239763740448SPaolo Abeni goto out_release; 239863740448SPaolo Abeni } 2399e0be4931SFlorian Westphal 240039880bd8SPaolo Abeni dispose_it = msk->free_first || ssk != msk->first; 2401b29fcfb5SPaolo Abeni if (dispose_it) 2402cec37a6eSPeter Krystad list_del(&subflow->node); 2403cec37a6eSPeter Krystad 24043f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2405e16163b6SPaolo Abeni 2406ff18f9efSPaolo Abeni if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) { 240714c56686SGeliang Tang /* be sure to force the tcp_close path 2408d21f8348SPaolo Abeni * to generate the egress reset 2409d21f8348SPaolo Abeni */ 2410d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2411d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2412b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2413d21f8348SPaolo Abeni } 2414b29fcfb5SPaolo Abeni 2415b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2416b29fcfb5SPaolo Abeni if (!dispose_it) { 241714c56686SGeliang Tang __mptcp_subflow_disconnect(ssk, subflow, flags); 2418b29fcfb5SPaolo Abeni release_sock(ssk); 2419b29fcfb5SPaolo Abeni 2420b29fcfb5SPaolo Abeni goto out; 2421b29fcfb5SPaolo Abeni } 2422b29fcfb5SPaolo Abeni 2423d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2424d7b1bfd0SPaolo Abeni 2425e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2426e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2427e16163b6SPaolo Abeni * reference owned by msk; 2428e16163b6SPaolo Abeni */ 2429e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2430b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2431e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2432e16163b6SPaolo Abeni } else { 2433d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2434e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2435e16163b6SPaolo Abeni 2436e16163b6SPaolo Abeni /* close acquired an extra ref */ 2437e16163b6SPaolo Abeni __sock_put(ssk); 2438e16163b6SPaolo Abeni } 243963740448SPaolo Abeni 244063740448SPaolo Abeni out_release: 24419f1a9881SPaolo Abeni __mptcp_subflow_error_report(sk, ssk); 2442e16163b6SPaolo Abeni release_sock(ssk); 2443e16163b6SPaolo Abeni 2444e16163b6SPaolo Abeni sock_put(ssk); 2445e0be4931SFlorian Westphal 2446c8fe62f0SFlorian Westphal if (ssk == msk->first) 24471b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2448c8fe62f0SFlorian Westphal 2449b29fcfb5SPaolo Abeni out: 245034c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 24511e1d9d6fSPaolo Abeni if (need_push) 24521e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 245327e5ccc2SPaolo Abeni 245427e5ccc2SPaolo Abeni /* Catch every 'all subflows closed' scenario, including peers silently 245527e5ccc2SPaolo Abeni * closing them, e.g. due to timeout. 245627e5ccc2SPaolo Abeni * For established sockets, allow an additional timeout before closing, 245727e5ccc2SPaolo Abeni * as the protocol can still create more subflows. 245827e5ccc2SPaolo Abeni */ 245927e5ccc2SPaolo Abeni if (list_is_singular(&msk->conn_list) && msk->first && 246027e5ccc2SPaolo Abeni inet_sk_state_load(msk->first) == TCP_CLOSE) { 246127e5ccc2SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED || 246227e5ccc2SPaolo Abeni msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) { 2463d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 246427e5ccc2SPaolo Abeni mptcp_close_wake_up(sk); 246527e5ccc2SPaolo Abeni } else { 246627e5ccc2SPaolo Abeni mptcp_start_tout_timer(sk); 246727e5ccc2SPaolo Abeni } 246827e5ccc2SPaolo Abeni } 2469f870fa0bSMat Martineau } 2470f870fa0bSMat Martineau 2471a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2472a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2473a141e02eSFlorian Westphal { 2474b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2475b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2476a88c9e49SPaolo Abeni 2477a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2478a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2479a88c9e49SPaolo Abeni */ 2480a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2481a88c9e49SPaolo Abeni 2482b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2483a141e02eSFlorian Westphal } 2484a141e02eSFlorian Westphal 2485dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2486dc24f8b4SPaolo Abeni { 2487dc24f8b4SPaolo Abeni return 0; 2488dc24f8b4SPaolo Abeni } 2489dc24f8b4SPaolo Abeni 2490b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24910e4f35d7SPaolo Abeni { 24920e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2493b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24940e4f35d7SPaolo Abeni 24953abc05d9SFlorian Westphal might_sleep(); 24963abc05d9SFlorian Westphal 24975efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24980e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2499255bc4fcSMatthieu Baerts (NGI0) int ssk_state = inet_sk_state_load(ssk); 25000e4f35d7SPaolo Abeni 2501255bc4fcSMatthieu Baerts (NGI0) if (ssk_state != TCP_CLOSE && 2502255bc4fcSMatthieu Baerts (NGI0) (ssk_state != TCP_CLOSE_WAIT || 2503255bc4fcSMatthieu Baerts (NGI0) inet_sk_state_load(sk) != TCP_ESTABLISHED)) 25040e4f35d7SPaolo Abeni continue; 25050e4f35d7SPaolo Abeni 250640947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 250740947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 250840947e13SFlorian Westphal continue; 250940947e13SFlorian Westphal 2510b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2511b6985b9bSPaolo Abeni } 2512b6985b9bSPaolo Abeni 25130e4f35d7SPaolo Abeni } 25140e4f35d7SPaolo Abeni 251527e5ccc2SPaolo Abeni static bool mptcp_close_tout_expired(const struct sock *sk) 2516e16163b6SPaolo Abeni { 251727e5ccc2SPaolo Abeni if (!inet_csk(sk)->icsk_mtup.probe_timestamp || 251827e5ccc2SPaolo Abeni sk->sk_state == TCP_CLOSE) 2519e16163b6SPaolo Abeni return false; 252027e5ccc2SPaolo Abeni 252127e5ccc2SPaolo Abeni return time_after32(tcp_jiffies32, 252227e5ccc2SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp + TCP_TIMEWAIT_LEN); 2523e16163b6SPaolo Abeni } 2524e16163b6SPaolo Abeni 252550c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 252650c504a2SFlorian Westphal { 252750c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 252880638684SGeliang Tang struct sock *sk = (struct sock *)msk; 252950c504a2SFlorian Westphal 253050c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 253150c504a2SFlorian Westphal return; 253250c504a2SFlorian Westphal 253350c504a2SFlorian Westphal mptcp_token_destroy(msk); 253450c504a2SFlorian Westphal 25355efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 253650c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 253775e908c3SPaolo Abeni bool slow; 253850c504a2SFlorian Westphal 253975e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 254050c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 254150c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 254250c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 254350c504a2SFlorian Westphal } 254475e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 254550c504a2SFlorian Westphal } 254650c504a2SFlorian Westphal 254769800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 254869800e51SPaolo Abeni switch (sk->sk_state) { 254969800e51SPaolo Abeni case TCP_SYN_SENT: 25509ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 255169800e51SPaolo Abeni break; 255269800e51SPaolo Abeni case TCP_CLOSE_WAIT: 25539ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 255469800e51SPaolo Abeni break; 255569800e51SPaolo Abeni case TCP_CLOSE: 255669800e51SPaolo Abeni return; 255769800e51SPaolo Abeni default: 25589ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 255969800e51SPaolo Abeni } 256069800e51SPaolo Abeni 2561d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 25626b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 256350c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 256450c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 256550c504a2SFlorian Westphal 256669800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 256769800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 256869800e51SPaolo Abeni return; 256969800e51SPaolo Abeni 257069800e51SPaolo Abeni sk->sk_state_change(sk); 257169800e51SPaolo Abeni sk_error_report(sk); 257250c504a2SFlorian Westphal } 257350c504a2SFlorian Westphal 25742948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 257580992017SPaolo Abeni { 25762948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2577ee2708aeSGeliang Tang struct mptcp_subflow_context *subflow; 2578caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25793b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25802948d0a1SPaolo Abeni struct sock *ssk; 2581ee2708aeSGeliang Tang int ret, err; 2582ee2708aeSGeliang Tang u16 len = 0; 25832948d0a1SPaolo Abeni 2584b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25853241a9c0SFlorian Westphal 25863241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 2587ee2708aeSGeliang Tang err = mptcp_sched_get_retrans(msk); 25882948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25896477dd39SMat Martineau if (!dfrag) { 25906477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25916477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25926477dd39SMat Martineau 25936477dd39SMat Martineau icsk->icsk_retransmits++; 25946477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25956477dd39SMat Martineau mptcp_send_ack(msk); 25966477dd39SMat Martineau 25976477dd39SMat Martineau goto reset_timer; 25986477dd39SMat Martineau } 25996477dd39SMat Martineau 26003241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 26012948d0a1SPaolo Abeni return; 26023241a9c0SFlorian Westphal 26033241a9c0SFlorian Westphal goto reset_timer; 26046477dd39SMat Martineau } 26052948d0a1SPaolo Abeni 2606ee2708aeSGeliang Tang if (err) 26072948d0a1SPaolo Abeni goto reset_timer; 26082948d0a1SPaolo Abeni 2609ee2708aeSGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2610ee2708aeSGeliang Tang if (READ_ONCE(subflow->scheduled)) { 2611ee2708aeSGeliang Tang u16 copied = 0; 2612ee2708aeSGeliang Tang 2613ee2708aeSGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 2614ee2708aeSGeliang Tang 2615ee2708aeSGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 2616ee2708aeSGeliang Tang 26172948d0a1SPaolo Abeni lock_sock(ssk); 26182948d0a1SPaolo Abeni 26192948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 26202948d0a1SPaolo Abeni info.sent = 0; 2621ee2708aeSGeliang Tang info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : 2622ee2708aeSGeliang Tang dfrag->already_sent; 26234e14867dSPaolo Abeni while (info.sent < info.limit) { 26242948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 26252948d0a1SPaolo Abeni if (ret <= 0) 26262948d0a1SPaolo Abeni break; 26272948d0a1SPaolo Abeni 26282948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 26292948d0a1SPaolo Abeni copied += ret; 26302948d0a1SPaolo Abeni info.sent += ret; 26312948d0a1SPaolo Abeni } 26324e14867dSPaolo Abeni if (copied) { 2633ee2708aeSGeliang Tang len = max(copied, len); 26342948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 26352948d0a1SPaolo Abeni info.size_goal); 26360530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 26374e14867dSPaolo Abeni } 26382948d0a1SPaolo Abeni 26392948d0a1SPaolo Abeni release_sock(ssk); 2640ee2708aeSGeliang Tang } 2641ee2708aeSGeliang Tang } 2642ee2708aeSGeliang Tang 2643ee2708aeSGeliang Tang msk->bytes_retrans += len; 2644ee2708aeSGeliang Tang dfrag->already_sent = max(dfrag->already_sent, len); 26452948d0a1SPaolo Abeni 26462948d0a1SPaolo Abeni reset_timer: 26473241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 26483241a9c0SFlorian Westphal 2649f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 2650f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 26512948d0a1SPaolo Abeni } 26522948d0a1SPaolo Abeni 265376a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 265476a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 265576a13b31SGeliang Tang */ 2656f6909dc1SPaolo Abeni void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout) 265776a13b31SGeliang Tang { 265876a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 265976a13b31SGeliang Tang unsigned long timeout, close_timeout; 266076a13b31SGeliang Tang 266127e5ccc2SPaolo Abeni if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp) 266276a13b31SGeliang Tang return; 266376a13b31SGeliang Tang 266427e5ccc2SPaolo Abeni close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + 266527e5ccc2SPaolo Abeni TCP_TIMEWAIT_LEN; 266676a13b31SGeliang Tang 266776a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 266876a13b31SGeliang Tang * them is active 266976a13b31SGeliang Tang */ 267027e5ccc2SPaolo Abeni timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout; 267176a13b31SGeliang Tang 267276a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 267376a13b31SGeliang Tang } 267476a13b31SGeliang Tang 267549fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 267649fa1919SGeliang Tang { 267776a13b31SGeliang Tang struct sock *ssk = msk->first; 267849fa1919SGeliang Tang bool slow; 267949fa1919SGeliang Tang 268076a13b31SGeliang Tang if (!ssk) 268176a13b31SGeliang Tang return; 268276a13b31SGeliang Tang 268349fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 268449fa1919SGeliang Tang 268549fa1919SGeliang Tang slow = lock_sock_fast(ssk); 268649fa1919SGeliang Tang mptcp_subflow_reset(ssk); 268776a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 268849fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 268949fa1919SGeliang Tang } 269049fa1919SGeliang Tang 2691d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2692d21f8348SPaolo Abeni { 2693d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2694d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2695d21f8348SPaolo Abeni 2696d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 2697d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2698d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2699d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2700d21f8348SPaolo Abeni } 2701d21f8348SPaolo Abeni 27022948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 27032948d0a1SPaolo Abeni { 27042948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 270580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 270676a13b31SGeliang Tang unsigned long fail_tout; 27072948d0a1SPaolo Abeni int state; 270880992017SPaolo Abeni 270980992017SPaolo Abeni lock_sock(sk); 2710e16163b6SPaolo Abeni state = sk->sk_state; 2711d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2712e16163b6SPaolo Abeni goto unlock; 2713e16163b6SPaolo Abeni 271450c504a2SFlorian Westphal mptcp_check_fastclose(msk); 271550c504a2SFlorian Westphal 2716e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2717b416268bSFlorian Westphal 271881c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 271981c1d029SPaolo Abeni mptcp_check_data_fin_ack(sk); 272043b54c6eSMat Martineau mptcp_check_data_fin(sk); 272143b54c6eSMat Martineau 2722b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2723b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2724b6985b9bSPaolo Abeni 272527e5ccc2SPaolo Abeni if (mptcp_close_tout_expired(sk)) { 2726d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 272727e5ccc2SPaolo Abeni mptcp_close_wake_up(sk); 272827e5ccc2SPaolo Abeni } 2729bbd49d11SPaolo Abeni 273027e5ccc2SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) { 2731e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2732e16163b6SPaolo Abeni goto unlock; 2733e16163b6SPaolo Abeni } 2734e16163b6SPaolo Abeni 27352948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 27362948d0a1SPaolo Abeni __mptcp_retrans(sk); 27373b1d6210SPaolo Abeni 273876a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 273976a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 274049fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 274149fa1919SGeliang Tang 27423b1d6210SPaolo Abeni unlock: 274380992017SPaolo Abeni release_sock(sk); 274480992017SPaolo Abeni sock_put(sk); 274580992017SPaolo Abeni } 274680992017SPaolo Abeni 2747e2636917SKuniyuki Iwashima static void __mptcp_init_sock(struct sock *sk) 2748f870fa0bSMat Martineau { 2749cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2750cec37a6eSPeter Krystad 2751cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2752ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 275318b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 275480992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 275587952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2756ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2757f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 27586511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2759ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 276033d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2761124a4885SPaolo Abeni msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO; 2762cec37a6eSPeter Krystad 27631b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2764dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2765752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 27660530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 27671e1d9d6fSPaolo Abeni msk->recovery = false; 27686f06b4d4SPaolo Abeni msk->subflow_id = 1; 27698ab183deSPaolo Abeni 27701b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27711b1c7a0eSPeter Krystad 2772b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2773b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2774f6909dc1SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_tout_timer, 0); 2775f870fa0bSMat Martineau } 2776f870fa0bSMat Martineau 2777b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2778784325e9SMatthieu Baerts { 277920b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2780b29fcfb5SPaolo Abeni 2781b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2782b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2783b29fcfb5SPaolo Abeni 2784b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2785b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2786b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2787b29fcfb5SPaolo Abeni } 2788b29fcfb5SPaolo Abeni 2789b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2790b29fcfb5SPaolo Abeni { 2791fc518953SFlorian Westphal struct net *net = sock_net(sk); 27921730b2b2SGeliang Tang int ret; 279318b683bfSPaolo Abeni 2794e2636917SKuniyuki Iwashima __mptcp_init_sock(sk); 2795b6c08380SGeliang Tang 2796fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2797fc518953SFlorian Westphal return -ENOPROTOOPT; 2798fc518953SFlorian Westphal 2799fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2800fc518953SFlorian Westphal return -ENOMEM; 2801fc518953SFlorian Westphal 28021730b2b2SGeliang Tang ret = mptcp_init_sched(mptcp_sk(sk), 28031730b2b2SGeliang Tang mptcp_sched_find(mptcp_get_scheduler(net))); 28041730b2b2SGeliang Tang if (ret) 28051730b2b2SGeliang Tang return ret; 28061730b2b2SGeliang Tang 2807a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2808a5ef058dSPaolo Abeni 280920b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 281020b5759fSPaolo Abeni * propagate the correct value 281120b5759fSPaolo Abeni */ 2812b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 281320b5759fSPaolo Abeni 2814d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2815a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2816a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2817d027236cSPaolo Abeni 281818b683bfSPaolo Abeni return 0; 281918b683bfSPaolo Abeni } 282018b683bfSPaolo Abeni 282118b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 282218b683bfSPaolo Abeni { 282318b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 282418b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 282518b683bfSPaolo Abeni 2826d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 282718b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2828d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2829784325e9SMatthieu Baerts } 2830784325e9SMatthieu Baerts 283130e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 283280992017SPaolo Abeni { 283380992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 283480992017SPaolo Abeni 2835b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2836e16163b6SPaolo Abeni __sock_put(sk); 283780992017SPaolo Abeni } 283880992017SPaolo Abeni 2839d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 284021498490SPeter Krystad { 284121498490SPeter Krystad lock_sock(ssk); 284221498490SPeter Krystad 284321498490SPeter Krystad switch (ssk->sk_state) { 284421498490SPeter Krystad case TCP_LISTEN: 284521498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 284621498490SPeter Krystad break; 2847df561f66SGustavo A. R. Silva fallthrough; 284821498490SPeter Krystad case TCP_SYN_SENT: 2849c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 285021498490SPeter Krystad break; 285121498490SPeter Krystad default: 285243b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 285343b54c6eSMat Martineau pr_debug("Fallback"); 285421498490SPeter Krystad ssk->sk_shutdown |= how; 285521498490SPeter Krystad tcp_shutdown(ssk, how); 285681c1d029SPaolo Abeni 285781c1d029SPaolo Abeni /* simulate the data_fin ack reception to let the state 285881c1d029SPaolo Abeni * machine move forward 285981c1d029SPaolo Abeni */ 286081c1d029SPaolo Abeni WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 286181c1d029SPaolo Abeni mptcp_schedule_work(sk); 286243b54c6eSMat Martineau } else { 286343b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 286443b54c6eSMat Martineau tcp_send_ack(ssk); 2865f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 2866f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 286743b54c6eSMat Martineau } 286821498490SPeter Krystad break; 286921498490SPeter Krystad } 287021498490SPeter Krystad 287121498490SPeter Krystad release_sock(ssk); 287221498490SPeter Krystad } 287321498490SPeter Krystad 287456557547SGeliang Tang void mptcp_set_state(struct sock *sk, int state) 287556557547SGeliang Tang { 287656557547SGeliang Tang int oldstate = sk->sk_state; 287756557547SGeliang Tang 287856557547SGeliang Tang switch (state) { 287956557547SGeliang Tang case TCP_ESTABLISHED: 288056557547SGeliang Tang if (oldstate != TCP_ESTABLISHED) 288156557547SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB); 288256557547SGeliang Tang break; 28833dd41669SJason Xing case TCP_CLOSE_WAIT: 28843dd41669SJason Xing /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state: 28853dd41669SJason Xing * MPTCP "accepted" sockets will be created later on. So no 28863dd41669SJason Xing * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT. 28873dd41669SJason Xing */ 28883dd41669SJason Xing break; 288956557547SGeliang Tang default: 28903dd41669SJason Xing if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT) 289156557547SGeliang Tang MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB); 289256557547SGeliang Tang } 289356557547SGeliang Tang 289456557547SGeliang Tang inet_sk_state_store(sk, state); 289556557547SGeliang Tang } 289656557547SGeliang Tang 28976920b851SMat Martineau static const unsigned char new_state[16] = { 28986920b851SMat Martineau /* current state: new state: action: */ 28996920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 29006920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 29016920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 29026920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 29036920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 29046920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 29056920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 29066920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 29076920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 29086920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 29096920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 29106920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 29116920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 29126920b851SMat Martineau }; 29136920b851SMat Martineau 29146920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 29156920b851SMat Martineau { 29166920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 29176920b851SMat Martineau int ns = next & TCP_STATE_MASK; 29186920b851SMat Martineau 2919d52b3c2bSGeliang Tang mptcp_set_state(sk, ns); 29206920b851SMat Martineau 29216920b851SMat Martineau return next & TCP_ACTION_FIN; 29226920b851SMat Martineau } 29236920b851SMat Martineau 292481c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk) 2925e16163b6SPaolo Abeni { 2926e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2927e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2928e16163b6SPaolo Abeni 2929e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2930e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2931e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2932e16163b6SPaolo Abeni 2933e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2934e16163b6SPaolo Abeni * skip this 2935e16163b6SPaolo Abeni */ 2936e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2937e16163b6SPaolo Abeni mptcp_send_head(sk)) 2938e16163b6SPaolo Abeni return; 2939e16163b6SPaolo Abeni 2940e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2941e16163b6SPaolo Abeni 2942e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2943e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2944e16163b6SPaolo Abeni 2945e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2946e16163b6SPaolo Abeni } 2947e16163b6SPaolo Abeni } 2948e16163b6SPaolo Abeni 2949e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2950e16163b6SPaolo Abeni { 2951e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2952e16163b6SPaolo Abeni 2953e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2954e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2955e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2956e16163b6SPaolo Abeni 2957e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2958e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2959e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2960e16163b6SPaolo Abeni 296181c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 2962e16163b6SPaolo Abeni } 2963e16163b6SPaolo Abeni 2964e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2965f870fa0bSMat Martineau { 2966f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2967f870fa0bSMat Martineau 2968e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 29692c22c06cSFlorian Westphal 29703abc05d9SFlorian Westphal might_sleep(); 29713abc05d9SFlorian Westphal 2972f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 2973e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2974e16163b6SPaolo Abeni msk->pm.status = 0; 29751730b2b2SGeliang Tang mptcp_release_sched(msk); 2976b2c5b614SFlorian Westphal 2977e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2978e16163b6SPaolo Abeni 29796511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 298087952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2981e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2982e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2983aa1fbd94SFlorian Westphal 2984e16163b6SPaolo Abeni sock_put(sk); 2985e16163b6SPaolo Abeni } 2986e16163b6SPaolo Abeni 298763740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 298863740448SPaolo Abeni { 298963740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 299063740448SPaolo Abeni mptcp_do_fastclose(sk); 299163740448SPaolo Abeni __mptcp_destroy_sock(sk); 299263740448SPaolo Abeni } 299363740448SPaolo Abeni 2994124a4885SPaolo Abeni static __poll_t mptcp_check_readable(struct sock *sk) 2995d21f8348SPaolo Abeni { 2996124a4885SPaolo Abeni return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0; 2997d21f8348SPaolo Abeni } 2998d21f8348SPaolo Abeni 299957fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk) 3000c558246eSMenglong Dong { 300157fc0f1cSPaolo Abeni struct sock *ssk; 300257fc0f1cSPaolo Abeni 300357fc0f1cSPaolo Abeni if (inet_sk_state_load(sk) != TCP_LISTEN) 300457fc0f1cSPaolo Abeni return; 300557fc0f1cSPaolo Abeni 3006c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 300757fc0f1cSPaolo Abeni ssk = mptcp_sk(sk)->first; 300857fc0f1cSPaolo Abeni if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 300957fc0f1cSPaolo Abeni return; 301057fc0f1cSPaolo Abeni 301157fc0f1cSPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 30123fffa15bSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 301357fc0f1cSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 301457fc0f1cSPaolo Abeni inet_csk_listen_stop(ssk); 301557fc0f1cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 301657fc0f1cSPaolo Abeni release_sock(ssk); 3017c558246eSMenglong Dong } 3018c558246eSMenglong Dong 301926d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 3020e16163b6SPaolo Abeni { 3021e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 302276a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 3023e16163b6SPaolo Abeni bool do_cancel_work = false; 3024d4e85922SPaolo Abeni int subflows_alive = 0; 3025e16163b6SPaolo Abeni 30266b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 3027e16163b6SPaolo Abeni 3028e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 302957fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3030d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3031e16163b6SPaolo Abeni goto cleanup; 3032e16163b6SPaolo Abeni } 3033e16163b6SPaolo Abeni 3034124a4885SPaolo Abeni if (mptcp_data_avail(msk) || timeout < 0) { 30358d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 30368d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 30378d547809SPaolo Abeni */ 3038d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 30398d547809SPaolo Abeni timeout = 0; 3040d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 3041e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 3042d21f8348SPaolo Abeni } 3043e16163b6SPaolo Abeni 3044e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 3045e16163b6SPaolo Abeni 3046e16163b6SPaolo Abeni cleanup: 3047e16163b6SPaolo Abeni /* orphan all the subflows */ 304876a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3049e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 305049054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 3051e16163b6SPaolo Abeni 3052d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 3053d4e85922SPaolo Abeni 305476a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 305576a13b31SGeliang Tang * cancel the latter 305676a13b31SGeliang Tang */ 305776a13b31SGeliang Tang if (ssk == msk->first) 305876a13b31SGeliang Tang subflow->fail_tout = 0; 305976a13b31SGeliang Tang 3060fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 3061fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 3062fe948001SMenglong Dong */ 3063fe948001SMenglong Dong ssk->sk_socket = NULL; 3064fe948001SMenglong Dong ssk->sk_wq = NULL; 3065e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 3066e16163b6SPaolo Abeni } 3067e16163b6SPaolo Abeni sock_orphan(sk); 3068e16163b6SPaolo Abeni 3069d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 3070d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 3071d4e85922SPaolo Abeni */ 3072d4e85922SPaolo Abeni if (subflows_alive == 0) 3073d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3074d4e85922SPaolo Abeni 3075e16163b6SPaolo Abeni sock_hold(sk); 3076e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 307700df24f1SGeliang Tang if (msk->token) 307876a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3079b29fcfb5SPaolo Abeni 3080e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 3081e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 3082e16163b6SPaolo Abeni do_cancel_work = true; 3083e16163b6SPaolo Abeni } else { 308427e5ccc2SPaolo Abeni mptcp_start_tout_timer(sk); 3085e16163b6SPaolo Abeni } 308626d3e21cSMenglong Dong 308726d3e21cSMenglong Dong return do_cancel_work; 308826d3e21cSMenglong Dong } 308926d3e21cSMenglong Dong 309026d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 309126d3e21cSMenglong Dong { 309226d3e21cSMenglong Dong bool do_cancel_work; 309326d3e21cSMenglong Dong 309426d3e21cSMenglong Dong lock_sock(sk); 309526d3e21cSMenglong Dong 309626d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 3097e16163b6SPaolo Abeni release_sock(sk); 3098e16163b6SPaolo Abeni if (do_cancel_work) 309980992017SPaolo Abeni mptcp_cancel_work(sk); 3100b911c97cSFlorian Westphal 3101e16163b6SPaolo Abeni sock_put(sk); 3102f870fa0bSMat Martineau } 3103f870fa0bSMat Martineau 31047e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3105cf7da0d6SPeter Krystad { 3106cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3107cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3108cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3109cf7da0d6SPeter Krystad 3110cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3111cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3112cf7da0d6SPeter Krystad 3113cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3114cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3115cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3116cf7da0d6SPeter Krystad } 3117cf7da0d6SPeter Krystad #endif 3118cf7da0d6SPeter Krystad 3119cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3120cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3121cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3122cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3123cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3124cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3125cf7da0d6SPeter Krystad } 3126cf7da0d6SPeter Krystad 312718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 312818b683bfSPaolo Abeni { 312976e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 313076e2a55dSPaolo Abeni 31317d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 31327d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 3133c2b2ae39SPaolo Abeni * msk->firstsocket lock). 31347d803344SPaolo Abeni */ 31357d803344SPaolo Abeni if (msk->fastopening) 3136c2b2ae39SPaolo Abeni return -EBUSY; 31377d803344SPaolo Abeni 313857fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3139d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3140b29fcfb5SPaolo Abeni 3141f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 314227e5ccc2SPaolo Abeni mptcp_stop_tout_timer(sk); 3143b29fcfb5SPaolo Abeni 314400df24f1SGeliang Tang if (msk->token) 314500df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3146b29fcfb5SPaolo Abeni 3147c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3148c0bf3c6aSPaolo Abeni * subflow 3149c0bf3c6aSPaolo Abeni */ 3150c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3151e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3152e9d09bacSPaolo Abeni msk->cb_flags = 0; 3153b29fcfb5SPaolo Abeni msk->recovery = false; 3154b29fcfb5SPaolo Abeni msk->can_ack = false; 3155b29fcfb5SPaolo Abeni msk->fully_established = false; 3156b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3157b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3158b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3159b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3160124a4885SPaolo Abeni msk->bytes_consumed = 0; 3161b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3162b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3163b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 316438967f42SPaolo Abeni msk->bytes_acked = 0; 316538967f42SPaolo Abeni msk->bytes_received = 0; 316638967f42SPaolo Abeni msk->bytes_sent = 0; 316738967f42SPaolo Abeni msk->bytes_retrans = 0; 3168a2d74374SPaolo Abeni msk->rcvspace_init = 0; 3169b29fcfb5SPaolo Abeni 31706b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3171b29fcfb5SPaolo Abeni sk_error_report(sk); 317242c556feSFlorian Westphal return 0; 317318b683bfSPaolo Abeni } 317418b683bfSPaolo Abeni 3175b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3176b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3177b0519de8SFlorian Westphal { 3178b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3179b0519de8SFlorian Westphal 3180b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3181b0519de8SFlorian Westphal } 3182ce0809adSDavide Caratti 3183ce0809adSDavide Caratti static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk) 3184ce0809adSDavide Caratti { 3185ce0809adSDavide Caratti const struct ipv6_pinfo *np = inet6_sk(sk); 3186ce0809adSDavide Caratti struct ipv6_txoptions *opt; 3187ce0809adSDavide Caratti struct ipv6_pinfo *newnp; 3188ce0809adSDavide Caratti 3189ce0809adSDavide Caratti newnp = inet6_sk(newsk); 3190ce0809adSDavide Caratti 3191ce0809adSDavide Caratti rcu_read_lock(); 3192ce0809adSDavide Caratti opt = rcu_dereference(np->opt); 3193ce0809adSDavide Caratti if (opt) { 3194ce0809adSDavide Caratti opt = ipv6_dup_options(newsk, opt); 3195ce0809adSDavide Caratti if (!opt) 3196ce0809adSDavide Caratti net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__); 3197ce0809adSDavide Caratti } 3198ce0809adSDavide Caratti RCU_INIT_POINTER(newnp->opt, opt); 3199ce0809adSDavide Caratti rcu_read_unlock(); 3200ce0809adSDavide Caratti } 3201b0519de8SFlorian Westphal #endif 3202b0519de8SFlorian Westphal 3203ce0809adSDavide Caratti static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk) 3204ce0809adSDavide Caratti { 3205ce0809adSDavide Caratti struct ip_options_rcu *inet_opt, *newopt = NULL; 3206ce0809adSDavide Caratti const struct inet_sock *inet = inet_sk(sk); 3207ce0809adSDavide Caratti struct inet_sock *newinet; 3208ce0809adSDavide Caratti 3209ce0809adSDavide Caratti newinet = inet_sk(newsk); 3210ce0809adSDavide Caratti 3211ce0809adSDavide Caratti rcu_read_lock(); 3212ce0809adSDavide Caratti inet_opt = rcu_dereference(inet->inet_opt); 3213ce0809adSDavide Caratti if (inet_opt) { 3214ce0809adSDavide Caratti newopt = sock_kmalloc(newsk, sizeof(*inet_opt) + 3215ce0809adSDavide Caratti inet_opt->opt.optlen, GFP_ATOMIC); 3216ce0809adSDavide Caratti if (newopt) 3217ce0809adSDavide Caratti memcpy(newopt, inet_opt, sizeof(*inet_opt) + 3218ce0809adSDavide Caratti inet_opt->opt.optlen); 3219ce0809adSDavide Caratti else 3220ce0809adSDavide Caratti net_warn_ratelimited("%s: Failed to copy ip options\n", __func__); 3221ce0809adSDavide Caratti } 3222ce0809adSDavide Caratti RCU_INIT_POINTER(newinet->inet_opt, newopt); 3223ce0809adSDavide Caratti rcu_read_unlock(); 3224ce0809adSDavide Caratti } 3225ce0809adSDavide Caratti 32267e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3227cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 32287e8b88ecSPaolo Abeni struct sock *ssk, 3229fca5c82cSPaolo Abeni struct request_sock *req) 3230b0519de8SFlorian Westphal { 323158b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3232b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 3233b45df837SPaolo Abeni struct mptcp_subflow_context *subflow; 323458b09919SPaolo Abeni struct mptcp_sock *msk; 3235b0519de8SFlorian Westphal 3236b0519de8SFlorian Westphal if (!nsk) 3237b0519de8SFlorian Westphal return NULL; 3238b0519de8SFlorian Westphal 3239b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3240b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3241b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3242b0519de8SFlorian Westphal #endif 3243b0519de8SFlorian Westphal 324458b09919SPaolo Abeni __mptcp_init_sock(nsk); 324558b09919SPaolo Abeni 3246ce0809adSDavide Caratti #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3247ce0809adSDavide Caratti if (nsk->sk_family == AF_INET6) 3248ce0809adSDavide Caratti mptcp_copy_ip6_options(nsk, sk); 3249ce0809adSDavide Caratti else 3250ce0809adSDavide Caratti #endif 3251ce0809adSDavide Caratti mptcp_copy_ip_options(nsk, sk); 3252ce0809adSDavide Caratti 325358b09919SPaolo Abeni msk = mptcp_sk(nsk); 325458b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 325558b09919SPaolo Abeni msk->token = subflow_req->token; 3256b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3257b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 325874c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 325906251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 326058b09919SPaolo Abeni 326158b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3262eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 32637439d687SPaolo Abeni msk->snd_una = msk->write_seq; 3264ae5530ccSPaolo Abeni msk->wnd_end = msk->snd_nxt + tcp_sk(ssk)->snd_wnd; 3265df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 32661730b2b2SGeliang Tang mptcp_init_sched(msk, mptcp_sk(sk)->sched); 32676f8a612aSFlorian Westphal 32686f06b4d4SPaolo Abeni /* passive msk is created after the first/MPC subflow */ 32696f06b4d4SPaolo Abeni msk->subflow_id = 2; 32706f06b4d4SPaolo Abeni 32715e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 32720c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 32737e8b88ecSPaolo Abeni 32747e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 32757e8b88ecSPaolo Abeni * not yet exposted to user-space 32767e8b88ecSPaolo Abeni */ 3277d52b3c2bSGeliang Tang mptcp_set_state(nsk, TCP_ESTABLISHED); 32787e8b88ecSPaolo Abeni 32797e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 32807e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 3281b45df837SPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 3282b45df837SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 32837e8b88ecSPaolo Abeni sock_hold(ssk); 32847e8b88ecSPaolo Abeni 32857e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 32867e8b88ecSPaolo Abeni * created mptcp socket 32877e8b88ecSPaolo Abeni */ 32887e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 32897e8b88ecSPaolo Abeni 32907e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 32917e8b88ecSPaolo Abeni * uses the correct data 32927e8b88ecSPaolo Abeni */ 32937e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 329434c7757aSPaolo Abeni __mptcp_propagate_sndbuf(nsk, ssk); 32957e8b88ecSPaolo Abeni 32967e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 3297b45df837SPaolo Abeni 3298b45df837SPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK) 3299b45df837SPaolo Abeni __mptcp_subflow_fully_established(msk, subflow, mp_opt); 330058b09919SPaolo Abeni bh_unlock_sock(nsk); 330158b09919SPaolo Abeni 3302b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3303b0519de8SFlorian Westphal return nsk; 3304b0519de8SFlorian Westphal } 3305b0519de8SFlorian Westphal 3306a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3307a6b118feSFlorian Westphal { 3308a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3309a6b118feSFlorian Westphal 3310a2d74374SPaolo Abeni msk->rcvspace_init = 1; 3311a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3312a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3313a6b118feSFlorian Westphal 3314a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3315a6b118feSFlorian Westphal 3316a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3317a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3318a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3319a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3320a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 3321a6b118feSFlorian Westphal } 3322a6b118feSFlorian Westphal 3323c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 33245c8c1640SGeliang Tang { 3325c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 332687952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 332787952603SPaolo Abeni 33286e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 33296e628cd3SPaolo Abeni 3330c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 33315efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3332c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3333c0bf3c6aSPaolo Abeni 333487952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3335b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 333687952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 33376511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 33385c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3339b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 33406511882cSPaolo Abeni 33416511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 33426511882cSPaolo Abeni * inet_sock_destruct() will dispose it 33436511882cSPaolo Abeni */ 33445e6300e7SEric Dumazet sk_forward_alloc_add(sk, msk->rmem_fwd_alloc); 33459531e4a8SEric Dumazet WRITE_ONCE(msk->rmem_fwd_alloc, 0); 33465c8c1640SGeliang Tang mptcp_token_destroy(msk); 33475c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 33484638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 33495c8c1640SGeliang Tang } 33505c8c1640SGeliang Tang 335179c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 335279c0949eSPeter Krystad { 3353c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3354c9fd9c5fSFlorian Westphal 335539880bd8SPaolo Abeni /* allow the following to close even the initial subflow */ 335639880bd8SPaolo Abeni msk->free_first = 1; 3357c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3358d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 335979c0949eSPeter Krystad } 336079c0949eSPeter Krystad 33616e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 33626e628cd3SPaolo Abeni { 33636e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 33646e628cd3SPaolo Abeni __mptcp_clean_una(sk); 33656e628cd3SPaolo Abeni else 3366e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 33676e628cd3SPaolo Abeni 33686e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 33696e628cd3SPaolo Abeni mptcp_schedule_work(sk); 33706e628cd3SPaolo Abeni } 33716e628cd3SPaolo Abeni 3372219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 33736e628cd3SPaolo Abeni { 33746e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 33756e628cd3SPaolo Abeni return; 33766e628cd3SPaolo Abeni 337773a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 337873a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 337973a0052aSGeliang Tang else 3380e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 33816e628cd3SPaolo Abeni } 33826e628cd3SPaolo Abeni 3383e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3384e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3385e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3386e9d09bacSPaolo Abeni 3387e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 338814c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3389e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 339014c441b5SPaolo Abeni { 3391e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 33922d6f5a2bSPaolo Abeni 3393e9d09bacSPaolo Abeni for (;;) { 3394877a72e3SPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED); 339556a666c4SPaolo Abeni struct list_head join_list; 339656a666c4SPaolo Abeni 3397c2e6048fSPaolo Abeni if (!flags) 3398c2e6048fSPaolo Abeni break; 3399c2e6048fSPaolo Abeni 340056a666c4SPaolo Abeni INIT_LIST_HEAD(&join_list); 340156a666c4SPaolo Abeni list_splice_init(&msk->join_list, &join_list); 340256a666c4SPaolo Abeni 3403c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 34046e628cd3SPaolo Abeni * 34056e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 34066e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 34076e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 34086e628cd3SPaolo Abeni * the subflow socket lock 34096e628cd3SPaolo Abeni */ 3410e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 34116e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 341256a666c4SPaolo Abeni 34133e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 341456a666c4SPaolo Abeni __mptcp_flush_join_list(sk, &join_list); 34152e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3416c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 34172d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 34182d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3419c2e6048fSPaolo Abeni 3420c2e6048fSPaolo Abeni cond_resched(); 34216e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 34226e628cd3SPaolo Abeni } 3423c2e6048fSPaolo Abeni 342465a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 342565a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 342617ebf8a4SXiang Yang if (unlikely(msk->cb_flags)) { 3427cf742d09SPaolo Abeni /* be sure to sync the msk state before taking actions 342844ee4764SPaolo Abeni * depending on sk_state (MPTCP_ERROR_REPORT) 342944ee4764SPaolo Abeni * On sk release avoid actions depending on the first subflow 3430490274b4SPaolo Abeni */ 3431cf742d09SPaolo Abeni if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first) 3432cf742d09SPaolo Abeni __mptcp_sync_state(sk, msk->pending_state); 3433e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 343415cc1045SPaolo Abeni __mptcp_error_report(sk); 343534c7757aSPaolo Abeni if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags)) 343634c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 343765a569b0SPaolo Abeni } 34386e628cd3SPaolo Abeni 343987952603SPaolo Abeni __mptcp_update_rmem(sk); 344014c441b5SPaolo Abeni } 344114c441b5SPaolo Abeni 3442bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3443bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3444bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3445bcd97734SPaolo Abeni */ 3446bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3447bcd97734SPaolo Abeni { 3448bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3449bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3450bcd97734SPaolo Abeni unsigned long timeout; 3451bcd97734SPaolo Abeni 3452bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3453bcd97734SPaolo Abeni return; 3454bcd97734SPaolo Abeni 3455bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3456bcd97734SPaolo Abeni if (tp->srtt_us) 3457bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3458bcd97734SPaolo Abeni else 3459bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3460bcd97734SPaolo Abeni timeout += jiffies; 3461bcd97734SPaolo Abeni 3462bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3463bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3464bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3465bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3466bcd97734SPaolo Abeni } 3467bcd97734SPaolo Abeni 3468a5efdbceSPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk, long status) 3469b19bc294SPaolo Abeni { 3470b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3471b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3472b19bc294SPaolo Abeni 3473a5efdbceSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_SEND)) { 3474b19bc294SPaolo Abeni mptcp_data_lock(sk); 3475b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 347673a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3477b19bc294SPaolo Abeni else 3478e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3479b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3480bcd97734SPaolo Abeni } 348134c7757aSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_SNDBUF)) { 348234c7757aSPaolo Abeni mptcp_data_lock(sk); 348334c7757aSPaolo Abeni if (!sock_owned_by_user(sk)) 348434c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 348534c7757aSPaolo Abeni else 348634c7757aSPaolo Abeni __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags); 348734c7757aSPaolo Abeni mptcp_data_unlock(sk); 348834c7757aSPaolo Abeni } 3489a5efdbceSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_ACK)) 3490bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3491b19bc294SPaolo Abeni } 3492b19bc294SPaolo Abeni 34932c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 34942c5ebd00SPaolo Abeni { 34952c5ebd00SPaolo Abeni /* should never be called, 34962c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 34972c5ebd00SPaolo Abeni */ 34982c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 34992c5ebd00SPaolo Abeni return 0; 35002c5ebd00SPaolo Abeni } 35012c5ebd00SPaolo Abeni 35022c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 35032c5ebd00SPaolo Abeni { 35042c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 35052c5ebd00SPaolo Abeni } 35062c5ebd00SPaolo Abeni 3507cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3508f870fa0bSMat Martineau { 3509f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3510f870fa0bSMat Martineau 3511cfb63e50SPaolo Abeni pr_debug("msk=%p, ssk=%p", msk, msk->first); 3512cfb63e50SPaolo Abeni if (WARN_ON_ONCE(!msk->first)) 3513cec37a6eSPeter Krystad return -EINVAL; 3514f870fa0bSMat Martineau 3515cfb63e50SPaolo Abeni return inet_csk_get_port(msk->first, snum); 3516cec37a6eSPeter Krystad } 3517f870fa0bSMat Martineau 3518cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3519cec37a6eSPeter Krystad { 3520cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3521cec37a6eSPeter Krystad struct mptcp_sock *msk; 3522cec37a6eSPeter Krystad struct sock *sk; 3523f870fa0bSMat Martineau 3524cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3525cec37a6eSPeter Krystad sk = subflow->conn; 3526cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3527cec37a6eSPeter Krystad 3528648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3529648ef4b8SMat Martineau 3530b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3531648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 35326d0060f6SMat Martineau 3533cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3534cec37a6eSPeter Krystad * accessing the field below 3535cec37a6eSPeter Krystad */ 3536cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 35371b1c7a0eSPeter Krystad 35386c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3539f870fa0bSMat Martineau } 3540f870fa0bSMat Martineau 3541866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3542cf7da0d6SPeter Krystad { 3543cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3544cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3545cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3546cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3547cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3548cf7da0d6SPeter Krystad } 3549cf7da0d6SPeter Krystad 3550e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3551f296234cSPeter Krystad { 3552e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3553f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3554f296234cSPeter Krystad struct sock *parent = (void *)msk; 35553e501490SPaolo Abeni bool ret = true; 3556f296234cSPeter Krystad 3557f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3558f296234cSPeter Krystad 3559f296234cSPeter Krystad /* mptcp socket already closing? */ 3560dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3561dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3562f296234cSPeter Krystad return false; 3563dc87efdbSFlorian Westphal } 3564f296234cSPeter Krystad 356555b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 356655b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 356755b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 356834c7757aSPaolo Abeni mptcp_propagate_sndbuf(parent, ssk); 356955b47ca7SPaolo Abeni return true; 357055b47ca7SPaolo Abeni } 3571f296234cSPeter Krystad 35723e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 35733e501490SPaolo Abeni goto err_prohibited; 357410f6d46cSPaolo Abeni 357555b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 35763e501490SPaolo Abeni * handle it 357710f6d46cSPaolo Abeni */ 35783e501490SPaolo Abeni mptcp_data_lock(parent); 35793e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 35803e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 35813e501490SPaolo Abeni if (ret) { 3582e16163b6SPaolo Abeni sock_hold(ssk); 35833e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3584e16163b6SPaolo Abeni } 35853e501490SPaolo Abeni } else { 35863e501490SPaolo Abeni sock_hold(ssk); 35873e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3588e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 35893e501490SPaolo Abeni } 35903e501490SPaolo Abeni mptcp_data_unlock(parent); 35913e501490SPaolo Abeni 3592dc87efdbSFlorian Westphal if (!ret) { 35933e501490SPaolo Abeni err_prohibited: 3594dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 359510f6d46cSPaolo Abeni return false; 3596dc87efdbSFlorian Westphal } 359710f6d46cSPaolo Abeni 359810f6d46cSPaolo Abeni return true; 3599f296234cSPeter Krystad } 3600f296234cSPeter Krystad 360176e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 360276e2a55dSPaolo Abeni { 360376e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 360476e2a55dSPaolo Abeni 360576e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 360676e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 360776e2a55dSPaolo Abeni } 360876e2a55dSPaolo Abeni 36096511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 36106511882cSPaolo Abeni { 36119531e4a8SEric Dumazet return READ_ONCE(sk->sk_forward_alloc) + 36129531e4a8SEric Dumazet READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc); 36136511882cSPaolo Abeni } 36146511882cSPaolo Abeni 3615644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3616644807e3SFlorian Westphal { 3617644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3618644807e3SFlorian Westphal u64 delta; 3619644807e3SFlorian Westphal 3620644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3621644807e3SFlorian Westphal return -EINVAL; 3622644807e3SFlorian Westphal 3623644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3624644807e3SFlorian Westphal return 0; 3625644807e3SFlorian Westphal 3626644807e3SFlorian Westphal delta = msk->write_seq - v; 362707c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 362807c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 362907c2c7a3SPaolo Abeni 363007c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 363107c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 363207c2c7a3SPaolo Abeni * so ignore that status, too. 363307c2c7a3SPaolo Abeni */ 363407c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 363507c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 363607c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 363707c2c7a3SPaolo Abeni } 3638644807e3SFlorian Westphal if (delta > INT_MAX) 3639644807e3SFlorian Westphal delta = INT_MAX; 3640644807e3SFlorian Westphal 3641644807e3SFlorian Westphal return (int)delta; 3642644807e3SFlorian Westphal } 3643644807e3SFlorian Westphal 3644e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) 3645644807e3SFlorian Westphal { 3646644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3647644807e3SFlorian Westphal bool slow; 3648644807e3SFlorian Westphal 3649644807e3SFlorian Westphal switch (cmd) { 3650644807e3SFlorian Westphal case SIOCINQ: 3651644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3652644807e3SFlorian Westphal return -EINVAL; 3653644807e3SFlorian Westphal 3654644807e3SFlorian Westphal lock_sock(sk); 3655644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3656e1d001faSBreno Leitao *karg = mptcp_inq_hint(sk); 3657644807e3SFlorian Westphal release_sock(sk); 3658644807e3SFlorian Westphal break; 3659644807e3SFlorian Westphal case SIOCOUTQ: 3660644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3661e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3662644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3663644807e3SFlorian Westphal break; 3664644807e3SFlorian Westphal case SIOCOUTQNSD: 3665644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3666e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, msk->snd_nxt); 3667644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3668644807e3SFlorian Westphal break; 3669644807e3SFlorian Westphal default: 3670644807e3SFlorian Westphal return -ENOIOCTLCMD; 3671644807e3SFlorian Westphal } 3672644807e3SFlorian Westphal 3673e1d001faSBreno Leitao return 0; 3674644807e3SFlorian Westphal } 3675644807e3SFlorian Westphal 367654f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 367754f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 367854f1944eSPaolo Abeni { 367954f1944eSPaolo Abeni subflow->request_mptcp = 0; 368054f1944eSPaolo Abeni __mptcp_do_fallback(msk); 368154f1944eSPaolo Abeni } 368254f1944eSPaolo Abeni 368354f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 368454f1944eSPaolo Abeni { 368554f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 368654f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 368754f1944eSPaolo Abeni int err = -EINVAL; 3688ccae357cSPaolo Abeni struct sock *ssk; 368954f1944eSPaolo Abeni 36903f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 36913f326a82SPaolo Abeni if (IS_ERR(ssk)) 36923f326a82SPaolo Abeni return PTR_ERR(ssk); 369354f1944eSPaolo Abeni 3694d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_SYN_SENT); 3695ccae357cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 369654f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 369754f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 369854f1944eSPaolo Abeni * TCP option space. 369954f1944eSPaolo Abeni */ 3700ccae357cSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info)) 370154f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 370254f1944eSPaolo Abeni #endif 3703ccae357cSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) { 3704ccae357cSPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT); 370554f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 370654f1944eSPaolo Abeni } 3707aa0c07c1SPaolo Abeni 3708aa0c07c1SPaolo Abeni WRITE_ONCE(msk->write_seq, subflow->idsn); 3709aa0c07c1SPaolo Abeni WRITE_ONCE(msk->snd_nxt, subflow->idsn); 3710f1f0a46fSPaolo Abeni WRITE_ONCE(msk->snd_una, subflow->idsn); 371154f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 371254f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 371354f1944eSPaolo Abeni 371454f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 371554f1944eSPaolo Abeni * acquired the subflow socket lock, too. 371654f1944eSPaolo Abeni */ 3717ccae357cSPaolo Abeni if (!msk->fastopening) 3718ccae357cSPaolo Abeni lock_sock(ssk); 3719ccae357cSPaolo Abeni 3720ccae357cSPaolo Abeni /* the following mirrors closely a very small chunk of code from 3721ccae357cSPaolo Abeni * __inet_stream_connect() 3722ccae357cSPaolo Abeni */ 3723ccae357cSPaolo Abeni if (ssk->sk_state != TCP_CLOSE) 3724ccae357cSPaolo Abeni goto out; 3725ccae357cSPaolo Abeni 3726ccae357cSPaolo Abeni if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) { 3727ccae357cSPaolo Abeni err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len); 3728ccae357cSPaolo Abeni if (err) 3729ccae357cSPaolo Abeni goto out; 3730ccae357cSPaolo Abeni } 3731ccae357cSPaolo Abeni 3732ccae357cSPaolo Abeni err = ssk->sk_prot->connect(ssk, uaddr, addr_len); 3733ccae357cSPaolo Abeni if (err < 0) 3734ccae357cSPaolo Abeni goto out; 3735ccae357cSPaolo Abeni 373608e39c0dSEric Dumazet inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk)); 3737ccae357cSPaolo Abeni 3738ccae357cSPaolo Abeni out: 3739ccae357cSPaolo Abeni if (!msk->fastopening) 3740ccae357cSPaolo Abeni release_sock(ssk); 374154f1944eSPaolo Abeni 374254f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 374354f1944eSPaolo Abeni * subflow_finish_connect() 374454f1944eSPaolo Abeni */ 3745ccae357cSPaolo Abeni if (unlikely(err)) { 3746131a6277SPaolo Abeni /* avoid leaving a dangling token in an unconnected socket */ 3747131a6277SPaolo Abeni mptcp_token_destroy(msk); 3748d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 374954f1944eSPaolo Abeni return err; 375054f1944eSPaolo Abeni } 375154f1944eSPaolo Abeni 3752ccae357cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 3753786fc124SPaolo Abeni return 0; 375454f1944eSPaolo Abeni } 375554f1944eSPaolo Abeni 3756f870fa0bSMat Martineau static struct proto mptcp_prot = { 3757f870fa0bSMat Martineau .name = "MPTCP", 3758f870fa0bSMat Martineau .owner = THIS_MODULE, 3759f870fa0bSMat Martineau .init = mptcp_init_sock, 376054f1944eSPaolo Abeni .connect = mptcp_connect, 376118b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3762f870fa0bSMat Martineau .close = mptcp_close, 3763717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3764717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 376576e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 376679c0949eSPeter Krystad .destroy = mptcp_destroy, 3767f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3768644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3769f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 377014c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 37712c5ebd00SPaolo Abeni .hash = mptcp_hash, 37722c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3773cec37a6eSPeter Krystad .get_port = mptcp_get_port, 37746511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3775d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 37760defbb0aSEric Dumazet 3777d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 37780defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 37790defbb0aSEric Dumazet 3780d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3781d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3782989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3783d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3784f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 37852c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3786f870fa0bSMat Martineau .no_autobind = true, 3787f870fa0bSMat Martineau }; 3788f870fa0bSMat Martineau 37892303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 37902303f994SPeter Krystad { 37912303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 37928cf2ebdcSPaolo Abeni struct sock *ssk, *sk = sock->sk; 37938cf2ebdcSPaolo Abeni int err = -EINVAL; 37942303f994SPeter Krystad 37958cf2ebdcSPaolo Abeni lock_sock(sk); 37963f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 37973f326a82SPaolo Abeni if (IS_ERR(ssk)) { 37983f326a82SPaolo Abeni err = PTR_ERR(ssk); 37992303f994SPeter Krystad goto unlock; 38002303f994SPeter Krystad } 38012303f994SPeter Krystad 38028cf2ebdcSPaolo Abeni if (sk->sk_family == AF_INET) 38038cf2ebdcSPaolo Abeni err = inet_bind_sk(ssk, uaddr, addr_len); 38048cf2ebdcSPaolo Abeni #if IS_ENABLED(CONFIG_MPTCP_IPV6) 38058cf2ebdcSPaolo Abeni else if (sk->sk_family == AF_INET6) 38068cf2ebdcSPaolo Abeni err = inet6_bind_sk(ssk, uaddr, addr_len); 38078cf2ebdcSPaolo Abeni #endif 3808cf7da0d6SPeter Krystad if (!err) 38098cf2ebdcSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 38102303f994SPeter Krystad 38112303f994SPeter Krystad unlock: 38128cf2ebdcSPaolo Abeni release_sock(sk); 38132303f994SPeter Krystad return err; 38142303f994SPeter Krystad } 38152303f994SPeter Krystad 3816cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3817cf7da0d6SPeter Krystad { 3818cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3819cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 382040f56d0cSPaolo Abeni struct sock *ssk; 3821cf7da0d6SPeter Krystad int err; 3822cf7da0d6SPeter Krystad 3823cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3824cf7da0d6SPeter Krystad 3825cfdcfeedSMenglong Dong lock_sock(sk); 38260226436aSPaolo Abeni 38270226436aSPaolo Abeni err = -EINVAL; 38280226436aSPaolo Abeni if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 38290226436aSPaolo Abeni goto unlock; 38300226436aSPaolo Abeni 38313f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 38323f326a82SPaolo Abeni if (IS_ERR(ssk)) { 38333f326a82SPaolo Abeni err = PTR_ERR(ssk); 3834cf7da0d6SPeter Krystad goto unlock; 3835cf7da0d6SPeter Krystad } 3836cf7da0d6SPeter Krystad 3837d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_LISTEN); 3838cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 38395e20087dSFlorian Westphal 384040f56d0cSPaolo Abeni lock_sock(ssk); 384140f56d0cSPaolo Abeni err = __inet_listen_sk(ssk, backlog); 384240f56d0cSPaolo Abeni release_sock(ssk); 3843d52b3c2bSGeliang Tang mptcp_set_state(sk, inet_sk_state_load(ssk)); 384440f56d0cSPaolo Abeni 3845c558246eSMenglong Dong if (!err) { 3846c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 384740f56d0cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 384840f56d0cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED); 384921d9b73aSPaolo Abeni } 3850f8c9dfbdSGeliang Tang 3851cf7da0d6SPeter Krystad unlock: 3852cfdcfeedSMenglong Dong release_sock(sk); 3853cf7da0d6SPeter Krystad return err; 3854cf7da0d6SPeter Krystad } 3855cf7da0d6SPeter Krystad 3856cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3857cf7da0d6SPeter Krystad int flags, bool kern) 3858cf7da0d6SPeter Krystad { 3859cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 38601f6610b9SPaolo Abeni struct sock *ssk, *newsk; 3861cf7da0d6SPeter Krystad int err; 3862cf7da0d6SPeter Krystad 3863cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3864cf7da0d6SPeter Krystad 38655b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 386661761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 386761761231SPaolo Abeni */ 38681f6610b9SPaolo Abeni ssk = READ_ONCE(msk->first); 38691f6610b9SPaolo Abeni if (!ssk) 387071ba088cSPaolo Abeni return -EINVAL; 3871cf7da0d6SPeter Krystad 387212f353faSDavide Caratti pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk)); 387312f353faSDavide Caratti newsk = inet_csk_accept(ssk, flags, &err, kern); 3874e76c8ef5SPaolo Abeni if (!newsk) 3875e76c8ef5SPaolo Abeni return err; 3876e76c8ef5SPaolo Abeni 387712f353faSDavide Caratti pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk)); 387812f353faSDavide Caratti if (sk_is_mptcp(newsk)) { 3879cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 388012f353faSDavide Caratti struct sock *new_mptcp_sock; 388112f353faSDavide Caratti 388212f353faSDavide Caratti subflow = mptcp_subflow_ctx(newsk); 388312f353faSDavide Caratti new_mptcp_sock = subflow->conn; 388412f353faSDavide Caratti 388512f353faSDavide Caratti /* is_mptcp should be false if subflow->conn is missing, see 388612f353faSDavide Caratti * subflow_syn_recv_sock() 388712f353faSDavide Caratti */ 388812f353faSDavide Caratti if (WARN_ON_ONCE(!new_mptcp_sock)) { 388912f353faSDavide Caratti tcp_sk(newsk)->is_mptcp = 0; 389012f353faSDavide Caratti goto tcpfallback; 389112f353faSDavide Caratti } 389212f353faSDavide Caratti 389312f353faSDavide Caratti newsk = new_mptcp_sock; 389412f353faSDavide Caratti MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 389512f353faSDavide Caratti 389612f353faSDavide Caratti newsk->sk_kern_sock = kern; 389712f353faSDavide Caratti lock_sock(newsk); 389812f353faSDavide Caratti __inet_accept(sock, newsock, newsk); 38990397c6d8SPaolo Abeni 3900a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 390112f353faSDavide Caratti msk = mptcp_sk(newsk); 3902b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3903a5ef058dSPaolo Abeni 3904cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3905cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3906cf7da0d6SPeter Krystad */ 3907190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3908cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3909cf7da0d6SPeter Krystad 3910cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3911cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3912cf7da0d6SPeter Krystad } 391363740448SPaolo Abeni 391463740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 391563740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 391663740448SPaolo Abeni */ 391739880bd8SPaolo Abeni if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 391863740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 391963740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 392039880bd8SPaolo Abeni if (unlikely(list_is_singular(&msk->conn_list))) 3921d52b3c2bSGeliang Tang mptcp_set_state(newsk, TCP_CLOSE); 392263740448SPaolo Abeni } 392312f353faSDavide Caratti } else { 392412f353faSDavide Caratti tcpfallback: 392512f353faSDavide Caratti newsk->sk_kern_sock = kern; 392612f353faSDavide Caratti lock_sock(newsk); 392712f353faSDavide Caratti __inet_accept(sock, newsock, newsk); 392812f353faSDavide Caratti /* we are being invoked after accepting a non-mp-capable 392912f353faSDavide Caratti * flow: sk is a tcp_sk, not an mptcp one. 393012f353faSDavide Caratti * 393112f353faSDavide Caratti * Hand the socket over to tcp so all further socket ops 393212f353faSDavide Caratti * bypass mptcp. 393312f353faSDavide Caratti */ 393412f353faSDavide Caratti WRITE_ONCE(newsock->sk->sk_socket->ops, 393512f353faSDavide Caratti mptcp_fallback_tcp_ops(newsock->sk)); 3936cf7da0d6SPeter Krystad } 3937e76c8ef5SPaolo Abeni release_sock(newsk); 3938cf7da0d6SPeter Krystad 3939e76c8ef5SPaolo Abeni return 0; 3940cf7da0d6SPeter Krystad } 3941cf7da0d6SPeter Krystad 39428edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 39438edf0864SFlorian Westphal { 39448edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 39458edf0864SFlorian Westphal 39468edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 39478edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 39488edf0864SFlorian Westphal 39495cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 39506e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 39516e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 39526e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 39538edf0864SFlorian Westphal 39546e628cd3SPaolo Abeni return 0; 39558edf0864SFlorian Westphal } 39568edf0864SFlorian Westphal 39572303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 39582303f994SPeter Krystad struct poll_table_struct *wait) 39592303f994SPeter Krystad { 39601891c4a0SFlorian Westphal struct sock *sk = sock->sk; 39618ab183deSPaolo Abeni struct mptcp_sock *msk; 39622303f994SPeter Krystad __poll_t mask = 0; 39636b9831bfSPaolo Abeni u8 shutdown; 39648a05661bSPaolo Abeni int state; 39652303f994SPeter Krystad 39661891c4a0SFlorian Westphal msk = mptcp_sk(sk); 39671891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 39681891c4a0SFlorian Westphal 39698a05661bSPaolo Abeni state = inet_sk_state_load(sk); 39706719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 397171ba088cSPaolo Abeni if (state == TCP_LISTEN) { 39725426a4efSPaolo Abeni struct sock *ssk = READ_ONCE(msk->first); 39735b825727SPaolo Abeni 39745426a4efSPaolo Abeni if (WARN_ON_ONCE(!ssk)) 397571ba088cSPaolo Abeni return 0; 397671ba088cSPaolo Abeni 39775426a4efSPaolo Abeni return inet_csk_listen_poll(ssk); 397871ba088cSPaolo Abeni } 39798a05661bSPaolo Abeni 39806b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 39816b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 39826b9831bfSPaolo Abeni mask |= EPOLLHUP; 39836b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 39846b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 39856b9831bfSPaolo Abeni 39868a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 3987124a4885SPaolo Abeni mask |= mptcp_check_readable(sk); 39886b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 39896b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 39906b9831bfSPaolo Abeni else 39918edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 399208e39c0dSEric Dumazet } else if (state == TCP_SYN_SENT && 399308e39c0dSEric Dumazet inet_test_bit(DEFER_CONNECT, sk)) { 3994a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3995a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 39968a05661bSPaolo Abeni } 39971891c4a0SFlorian Westphal 3998d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 399915cc1045SPaolo Abeni smp_rmb(); 40009ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 400115cc1045SPaolo Abeni mask |= EPOLLERR; 400215cc1045SPaolo Abeni 40032303f994SPeter Krystad return mask; 40042303f994SPeter Krystad } 40052303f994SPeter Krystad 4006e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 4007e42f1ac6SFlorian Westphal .family = PF_INET, 4008e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 40090a3cc579SPaolo Abeni .release = inet_release, 4010e42f1ac6SFlorian Westphal .bind = mptcp_bind, 4011786fc124SPaolo Abeni .connect = inet_stream_connect, 4012e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 4013e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 4014d2f77c53SPaolo Abeni .getname = inet_getname, 4015e42f1ac6SFlorian Westphal .poll = mptcp_poll, 4016e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 4017e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 4018e42f1ac6SFlorian Westphal .listen = mptcp_listen, 401976e2a55dSPaolo Abeni .shutdown = inet_shutdown, 4020e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 4021e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 4022e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 4023e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 4024e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 4025124a4885SPaolo Abeni .set_rcvlowat = mptcp_set_rcvlowat, 4026e42f1ac6SFlorian Westphal }; 40272303f994SPeter Krystad 4028f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 4029f870fa0bSMat Martineau .type = SOCK_STREAM, 4030f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 4031f870fa0bSMat Martineau .prot = &mptcp_prot, 40322303f994SPeter Krystad .ops = &mptcp_stream_ops, 40332303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 4034f870fa0bSMat Martineau }; 4035f870fa0bSMat Martineau 4036b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 4037b19bc294SPaolo Abeni { 4038b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 4039b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 4040b19bc294SPaolo Abeni int work_done = 0; 4041b19bc294SPaolo Abeni 4042b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 4043b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 4044b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 4045b19bc294SPaolo Abeni 4046b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 4047a5efdbceSPaolo Abeni if (!sock_owned_by_user(ssk)) { 4048a5efdbceSPaolo Abeni mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0)); 4049a5efdbceSPaolo Abeni } else { 4050a5efdbceSPaolo Abeni /* tcp_release_cb_override already processed 4051b19bc294SPaolo Abeni * the action or will do at next release_sock(). 4052b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 4053b19bc294SPaolo Abeni * CPU that scheduled it. 4054b19bc294SPaolo Abeni */ 4055a5efdbceSPaolo Abeni smp_wmb(); 4056a5efdbceSPaolo Abeni clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status); 4057a5efdbceSPaolo Abeni } 4058b19bc294SPaolo Abeni bh_unlock_sock(ssk); 4059b19bc294SPaolo Abeni sock_put(ssk); 4060b19bc294SPaolo Abeni 4061b19bc294SPaolo Abeni if (++work_done == budget) 4062b19bc294SPaolo Abeni return budget; 4063b19bc294SPaolo Abeni } 4064b19bc294SPaolo Abeni 4065b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 4066b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 4067b19bc294SPaolo Abeni */ 4068b19bc294SPaolo Abeni napi_complete_done(napi, 0); 4069b19bc294SPaolo Abeni return work_done; 4070b19bc294SPaolo Abeni } 4071b19bc294SPaolo Abeni 4072d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 4073f870fa0bSMat Martineau { 4074b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 4075b19bc294SPaolo Abeni int cpu; 4076b19bc294SPaolo Abeni 40772303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 40782303f994SPeter Krystad 4079d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 4080d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 4081d027236cSPaolo Abeni 4082b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 4083b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 4084b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 4085b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 408616d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 408716d083e2SJakub Kicinski mptcp_napi_poll); 4088b19bc294SPaolo Abeni napi_enable(&delegated->napi); 4089b19bc294SPaolo Abeni } 4090b19bc294SPaolo Abeni 40912303f994SPeter Krystad mptcp_subflow_init(); 40921b1c7a0eSPeter Krystad mptcp_pm_init(); 4093ed1ad86bSGeliang Tang mptcp_sched_init(); 40942c5ebd00SPaolo Abeni mptcp_token_init(); 40952303f994SPeter Krystad 4096f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 4097f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 4098f870fa0bSMat Martineau 4099f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 41006771bfd9SFlorian Westphal 41016771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 4102f870fa0bSMat Martineau } 4103f870fa0bSMat Martineau 4104f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 4105e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 4106e42f1ac6SFlorian Westphal .family = PF_INET6, 4107e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 41080a3cc579SPaolo Abeni .release = inet6_release, 4109e42f1ac6SFlorian Westphal .bind = mptcp_bind, 4110786fc124SPaolo Abeni .connect = inet_stream_connect, 4111e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 4112e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 4113d2f77c53SPaolo Abeni .getname = inet6_getname, 4114e42f1ac6SFlorian Westphal .poll = mptcp_poll, 4115e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 4116e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 4117e42f1ac6SFlorian Westphal .listen = mptcp_listen, 411876e2a55dSPaolo Abeni .shutdown = inet_shutdown, 4119e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 4120e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 4121e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 4122e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 4123e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 4124e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 41253986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 4126e42f1ac6SFlorian Westphal #endif 4127124a4885SPaolo Abeni .set_rcvlowat = mptcp_set_rcvlowat, 4128e42f1ac6SFlorian Westphal }; 4129e42f1ac6SFlorian Westphal 4130f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 4131f870fa0bSMat Martineau 4132f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 4133f870fa0bSMat Martineau .type = SOCK_STREAM, 4134f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 4135f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 41362303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 4137f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 4138f870fa0bSMat Martineau }; 4139f870fa0bSMat Martineau 4140d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 4141f870fa0bSMat Martineau { 4142f870fa0bSMat Martineau int err; 4143f870fa0bSMat Martineau 4144f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 4145f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 4146f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 4147b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 4148f5f80e32SEric Dumazet mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np); 4149f870fa0bSMat Martineau 4150f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 4151f870fa0bSMat Martineau if (err) 4152f870fa0bSMat Martineau return err; 4153f870fa0bSMat Martineau 4154f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 4155f870fa0bSMat Martineau if (err) 4156f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 4157f870fa0bSMat Martineau 4158f870fa0bSMat Martineau return err; 4159f870fa0bSMat Martineau } 4160f870fa0bSMat Martineau #endif 4161