1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25644807e3SFlorian Westphal #include <asm/ioctls.h> 26f870fa0bSMat Martineau #include "protocol.h" 27fc518953SFlorian Westphal #include "mib.h" 28f870fa0bSMat Martineau 29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 30e10a9892SGeliang Tang #include <trace/events/mptcp.h> 31e10a9892SGeliang Tang 32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33b0519de8SFlorian Westphal struct mptcp6_sock { 34b0519de8SFlorian Westphal struct mptcp_sock msk; 35b0519de8SFlorian Westphal struct ipv6_pinfo np; 36b0519de8SFlorian Westphal }; 37b0519de8SFlorian Westphal #endif 38b0519de8SFlorian Westphal 39b7f653b2SFlorian Westphal enum { 40b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 412c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 42b7f653b2SFlorian Westphal }; 43b7f653b2SFlorian Westphal 4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45d027236cSPaolo Abeni 46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 4781c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk); 48e16163b6SPaolo Abeni 49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 51b19bc294SPaolo Abeni 526f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 536f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 546f8a612aSFlorian Westphal { 557439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 566f8a612aSFlorian Westphal } 576f8a612aSFlorian Westphal 58d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 590b4f33deSFlorian Westphal { 600b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 610b4f33deSFlorian Westphal 620b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 630b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 640b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 650b4f33deSFlorian Westphal * not an mptcp one. 660b4f33deSFlorian Westphal * 670b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 680b4f33deSFlorian Westphal * bypass mptcp. 690b4f33deSFlorian Westphal */ 701ded5e5aSEric Dumazet WRITE_ONCE(sock->ops, &inet_stream_ops); 71d2f77c53SPaolo Abeni return true; 720b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 730b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 741ded5e5aSEric Dumazet WRITE_ONCE(sock->ops, &inet6_stream_ops); 75d2f77c53SPaolo Abeni return true; 760b4f33deSFlorian Westphal #endif 770b4f33deSFlorian Westphal } 780b4f33deSFlorian Westphal 79d2f77c53SPaolo Abeni return false; 800b4f33deSFlorian Westphal } 810b4f33deSFlorian Westphal 82fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 832303f994SPeter Krystad { 842303f994SPeter Krystad struct mptcp_subflow_context *subflow; 852303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 862303f994SPeter Krystad struct socket *ssock; 872303f994SPeter Krystad int err; 882303f994SPeter Krystad 896bc1fe7dSPaolo Abeni err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock); 902303f994SPeter Krystad if (err) 91fa68018dSPaolo Abeni return err; 922303f994SPeter Krystad 93b8dc6d6cSPaolo Abeni msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio; 941b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, ssock->sk); 952303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 96cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 97e16163b6SPaolo Abeni sock_hold(ssock->sk); 982303f994SPeter Krystad subflow->request_mptcp = 1; 996f06b4d4SPaolo Abeni subflow->subflow_id = msk->subflow_id++; 1004cf86ae8SPaolo Abeni 1014cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1024cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 103866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 10439880bd8SPaolo Abeni iput(SOCK_INODE(ssock)); 105e1ff9e82SDavide Caratti 106fa68018dSPaolo Abeni return 0; 1072303f994SPeter Krystad } 1082303f994SPeter Krystad 109ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow, 110ddb1a072SPaolo Abeni * eventually allocating it. 111ddb1a072SPaolo Abeni */ 1123f326a82SPaolo Abeni struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk) 113ddb1a072SPaolo Abeni { 114ddb1a072SPaolo Abeni struct sock *sk = (struct sock *)msk; 115ddb1a072SPaolo Abeni int ret; 116ddb1a072SPaolo Abeni 117ddb1a072SPaolo Abeni if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 118ddb1a072SPaolo Abeni return ERR_PTR(-EINVAL); 119ddb1a072SPaolo Abeni 1203f326a82SPaolo Abeni if (!msk->first) { 121ddb1a072SPaolo Abeni ret = __mptcp_socket_create(msk); 122ddb1a072SPaolo Abeni if (ret) 123ddb1a072SPaolo Abeni return ERR_PTR(ret); 124ddb1a072SPaolo Abeni 125ddb1a072SPaolo Abeni mptcp_sockopt_sync(msk, msk->first); 126ddb1a072SPaolo Abeni } 127ddb1a072SPaolo Abeni 1283f326a82SPaolo Abeni return msk->first; 129ddb1a072SPaolo Abeni } 130ddb1a072SPaolo Abeni 131ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 132ab174ad8SPaolo Abeni { 133ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 134ab174ad8SPaolo Abeni __kfree_skb(skb); 135ab174ad8SPaolo Abeni } 136ab174ad8SPaolo Abeni 1379531e4a8SEric Dumazet static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size) 1389531e4a8SEric Dumazet { 1399531e4a8SEric Dumazet WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc, 1409531e4a8SEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc + size); 1419531e4a8SEric Dumazet } 1429531e4a8SEric Dumazet 1436511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1446511882cSPaolo Abeni { 1459531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, -size); 1466511882cSPaolo Abeni } 1476511882cSPaolo Abeni 1488268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1498268ed4cSPaolo Abeni struct sk_buff *from) 1508268ed4cSPaolo Abeni { 1518268ed4cSPaolo Abeni bool fragstolen; 1528268ed4cSPaolo Abeni int delta; 1538268ed4cSPaolo Abeni 1548268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1558268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1568268ed4cSPaolo Abeni return false; 1578268ed4cSPaolo Abeni 15806242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 15906242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 16006242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 161ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1627288ff6eSPaolo Abeni 1637288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1647288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1657288ff6eSPaolo Abeni * negative one 1667288ff6eSPaolo Abeni */ 1678268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1686511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1697288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1707288ff6eSPaolo Abeni 1718268ed4cSPaolo Abeni return true; 1728268ed4cSPaolo Abeni } 1738268ed4cSPaolo Abeni 174ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 175ab174ad8SPaolo Abeni struct sk_buff *from) 1766771bfd9SFlorian Westphal { 177ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 178ab174ad8SPaolo Abeni return false; 179ab174ad8SPaolo Abeni 180ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 181ab174ad8SPaolo Abeni } 182ab174ad8SPaolo Abeni 1836511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1846511882cSPaolo Abeni { 185100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 1869531e4a8SEric Dumazet mptcp_rmem_charge(sk, amount << PAGE_SHIFT); 1876511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1886511882cSPaolo Abeni } 1896511882cSPaolo Abeni 1906511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1916511882cSPaolo Abeni { 1926511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1936511882cSPaolo Abeni int reclaimable; 1946511882cSPaolo Abeni 1959531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, size); 1966511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1976511882cSPaolo Abeni 1986511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 199d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 200d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 2016511882cSPaolo Abeni } 2026511882cSPaolo Abeni 2036511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 2046511882cSPaolo Abeni { 2056511882cSPaolo Abeni unsigned int len = skb->truesize; 2066511882cSPaolo Abeni struct sock *sk = skb->sk; 2076511882cSPaolo Abeni 2086511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 2096511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 2106511882cSPaolo Abeni } 2116511882cSPaolo Abeni 21236b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 2136511882cSPaolo Abeni { 2146511882cSPaolo Abeni skb_orphan(skb); 2156511882cSPaolo Abeni skb->sk = sk; 2166511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2176511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2186511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2196511882cSPaolo Abeni } 2206511882cSPaolo Abeni 221ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 222ab174ad8SPaolo Abeni * - use mptcp seqs 223ab174ad8SPaolo Abeni * - don't cope with sacks 224ab174ad8SPaolo Abeni */ 225ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 226ab174ad8SPaolo Abeni { 227ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 228ab174ad8SPaolo Abeni struct rb_node **p, *parent; 229ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 230ab174ad8SPaolo Abeni struct sk_buff *skb1; 231ab174ad8SPaolo Abeni 232ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 233ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 234f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 235ab174ad8SPaolo Abeni 23606242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 23706242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 238fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 239ab174ad8SPaolo Abeni /* out of window */ 240ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 241fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 242fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 243f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 24406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 245ab174ad8SPaolo Abeni return; 246ab174ad8SPaolo Abeni } 247ab174ad8SPaolo Abeni 248ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 24906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 250ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 251ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 252ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 253ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 254ab174ad8SPaolo Abeni goto end; 255ab174ad8SPaolo Abeni } 256ab174ad8SPaolo Abeni 257ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 258ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 259ab174ad8SPaolo Abeni */ 26006242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 26106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 26206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 263ab174ad8SPaolo Abeni return; 26406242e44SPaolo Abeni } 265ab174ad8SPaolo Abeni 266ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 267ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 26806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 269ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 270ab174ad8SPaolo Abeni p = &parent->rb_right; 271ab174ad8SPaolo Abeni goto insert; 272ab174ad8SPaolo Abeni } 273ab174ad8SPaolo Abeni 274ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 275ab174ad8SPaolo Abeni parent = NULL; 276ab174ad8SPaolo Abeni while (*p) { 277ab174ad8SPaolo Abeni parent = *p; 278ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 279ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 280ab174ad8SPaolo Abeni p = &parent->rb_left; 281ab174ad8SPaolo Abeni continue; 282ab174ad8SPaolo Abeni } 283ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 284ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 285ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 286ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 28706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 288ab174ad8SPaolo Abeni return; 289ab174ad8SPaolo Abeni } 290ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 291ab174ad8SPaolo Abeni /* partial overlap: 292ab174ad8SPaolo Abeni * | skb | 293ab174ad8SPaolo Abeni * | skb1 | 294ab174ad8SPaolo Abeni * continue traversing 295ab174ad8SPaolo Abeni */ 296ab174ad8SPaolo Abeni } else { 297ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 298ab174ad8SPaolo Abeni * Replace skb1 with skb. 299ab174ad8SPaolo Abeni */ 300ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 301ab174ad8SPaolo Abeni &msk->out_of_order_queue); 302ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 30306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 304ab174ad8SPaolo Abeni goto merge_right; 305ab174ad8SPaolo Abeni } 306ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 30706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 308ab174ad8SPaolo Abeni return; 309ab174ad8SPaolo Abeni } 310ab174ad8SPaolo Abeni p = &parent->rb_right; 311ab174ad8SPaolo Abeni } 31206242e44SPaolo Abeni 313ab174ad8SPaolo Abeni insert: 314ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 315ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 316ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 317ab174ad8SPaolo Abeni 318ab174ad8SPaolo Abeni merge_right: 319ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 320ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 321ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 322ab174ad8SPaolo Abeni break; 323ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 324ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 32506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 326ab174ad8SPaolo Abeni } 327ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 328ab174ad8SPaolo Abeni if (!skb1) 329ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 330ab174ad8SPaolo Abeni 331ab174ad8SPaolo Abeni end: 332ab174ad8SPaolo Abeni skb_condense(skb); 3336511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3346511882cSPaolo Abeni } 3356511882cSPaolo Abeni 3366511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3376511882cSPaolo Abeni { 3386511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3396511882cSPaolo Abeni int amt, amount; 3406511882cSPaolo Abeni 34169d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3426511882cSPaolo Abeni return true; 3436511882cSPaolo Abeni 34469d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3456511882cSPaolo Abeni amt = sk_mem_pages(size); 346100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3474aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3486511882cSPaolo Abeni return false; 3496511882cSPaolo Abeni 3509531e4a8SEric Dumazet mptcp_rmem_fwd_alloc_add(sk, amount); 3516511882cSPaolo Abeni return true; 352ab174ad8SPaolo Abeni } 353ab174ad8SPaolo Abeni 354ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 355ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 356ab174ad8SPaolo Abeni size_t copy_len) 357ab174ad8SPaolo Abeni { 358ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3596771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3604e637c70SFlorian Westphal struct sk_buff *tail; 361b7f653b2SFlorian Westphal bool has_rxtstamp; 3626771bfd9SFlorian Westphal 3636771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3644e637c70SFlorian Westphal 3654e637c70SFlorian Westphal skb_ext_reset(skb); 3664e637c70SFlorian Westphal skb_orphan(skb); 3674e637c70SFlorian Westphal 3689c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3696511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3709c3f94e1SPaolo Abeni goto drop; 37172f96132SPaolo Abeni 372b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 373b7f653b2SFlorian Westphal 374ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 375ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 376ab174ad8SPaolo Abeni * value 377ab174ad8SPaolo Abeni */ 378ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 379ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 380ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 381b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 382ab174ad8SPaolo Abeni 383ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 384ab174ad8SPaolo Abeni /* in sequence */ 38538967f42SPaolo Abeni msk->bytes_received += copy_len; 386917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3874e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3888268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 389ab174ad8SPaolo Abeni return true; 3904e637c70SFlorian Westphal 3916511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3926771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 393ab174ad8SPaolo Abeni return true; 394ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 395ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 396ab174ad8SPaolo Abeni return false; 397ab174ad8SPaolo Abeni } 398ab174ad8SPaolo Abeni 399ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 400ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 401ab174ad8SPaolo Abeni */ 40206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 4039c3f94e1SPaolo Abeni drop: 404ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 405ab174ad8SPaolo Abeni return false; 4066771bfd9SFlorian Westphal } 4076771bfd9SFlorian Westphal 408f6909dc1SPaolo Abeni static void mptcp_stop_rtx_timer(struct sock *sk) 40916a9a9daSMat Martineau { 41016a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 41116a9a9daSMat Martineau 41216a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 41316a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 41416a9a9daSMat Martineau } 41516a9a9daSMat Martineau 416e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 417e16163b6SPaolo Abeni { 418e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 419e16163b6SPaolo Abeni return; 420e16163b6SPaolo Abeni 421e16163b6SPaolo Abeni sk->sk_state_change(sk); 422e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 423e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 424e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 425e16163b6SPaolo Abeni else 426e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 427e16163b6SPaolo Abeni } 428e16163b6SPaolo Abeni 4296e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4306e628cd3SPaolo Abeni { 4316e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4326e628cd3SPaolo Abeni 43381c1d029SPaolo Abeni return ((1 << sk->sk_state) & 4346e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4356e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4366e628cd3SPaolo Abeni } 4376e628cd3SPaolo Abeni 43816a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 43916a9a9daSMat Martineau { 44016a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 44116a9a9daSMat Martineau 44216a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4436e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 44416a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 44516a9a9daSMat Martineau 44616a9a9daSMat Martineau switch (sk->sk_state) { 44716a9a9daSMat Martineau case TCP_FIN_WAIT1: 448d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_FIN_WAIT2); 44916a9a9daSMat Martineau break; 45016a9a9daSMat Martineau case TCP_CLOSING: 45116a9a9daSMat Martineau case TCP_LAST_ACK: 452d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 45316a9a9daSMat Martineau break; 45416a9a9daSMat Martineau } 45516a9a9daSMat Martineau 456e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 45716a9a9daSMat Martineau } 45816a9a9daSMat Martineau } 45916a9a9daSMat Martineau 4603721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4613721b9b6SMat Martineau { 4623721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4633721b9b6SMat Martineau 4643721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4653721b9b6SMat Martineau ((1 << sk->sk_state) & 4663721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4673721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4683721b9b6SMat Martineau 4693721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4703721b9b6SMat Martineau if (seq) 4713721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4723721b9b6SMat Martineau 4733721b9b6SMat Martineau return true; 4743721b9b6SMat Martineau } 4753721b9b6SMat Martineau } 4763721b9b6SMat Martineau 4773721b9b6SMat Martineau return false; 4783721b9b6SMat Martineau } 4793721b9b6SMat Martineau 480403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk) 4816477dd39SMat Martineau { 4826477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 483877d11f0SMat Martineau u32 retransmits; 4846477dd39SMat Martineau 485877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 486877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 487877d11f0SMat Martineau 488877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4896477dd39SMat Martineau } 4906477dd39SMat Martineau 49133d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4923721b9b6SMat Martineau { 4933721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4943721b9b6SMat Martineau } 4953721b9b6SMat Martineau 49633d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 49733d41c9cSPaolo Abeni { 49833d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 49933d41c9cSPaolo Abeni 50071b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 50171b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 50233d41c9cSPaolo Abeni } 50333d41c9cSPaolo Abeni 50433d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 50533d41c9cSPaolo Abeni { 50633d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 50733d41c9cSPaolo Abeni long tout = 0; 50833d41c9cSPaolo Abeni 50933d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 51033d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 51133d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 51233d41c9cSPaolo Abeni } 51333d41c9cSPaolo Abeni 51476a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 515fd897679SPaolo Abeni { 516fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 51720bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 518fd897679SPaolo Abeni } 519fd897679SPaolo Abeni 520a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 521a6574302SMat Martineau { 522a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 523a6574302SMat Martineau tcp_send_ack(ssk); 524a6574302SMat Martineau } 525a6574302SMat Martineau 526f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5277ed90803SPaolo Abeni { 52875e908c3SPaolo Abeni bool slow; 5297ed90803SPaolo Abeni 53075e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 531a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 53275e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 533fd897679SPaolo Abeni } 534340fa666SMat Martineau 535340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 536340fa666SMat Martineau { 537340fa666SMat Martineau struct mptcp_subflow_context *subflow; 538340fa666SMat Martineau 539340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 540340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 541ea4ca586SPaolo Abeni } 542ea4ca586SPaolo Abeni 543fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 544fd897679SPaolo Abeni { 54575e908c3SPaolo Abeni bool slow; 546fd897679SPaolo Abeni 54775e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 548fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 549fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 55075e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 551fde56eeaSPaolo Abeni } 552fde56eeaSPaolo Abeni 553fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 554fde56eeaSPaolo Abeni { 555fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 556c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 557fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 558fde56eeaSPaolo Abeni 559fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 560fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 561fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 562fde56eeaSPaolo Abeni (rx_empty && ack_pending & 563fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 564ea4ca586SPaolo Abeni } 565fd897679SPaolo Abeni 566fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 567fd897679SPaolo Abeni { 568e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 569fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 570e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 571fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 572fde56eeaSPaolo Abeni bool cleanup, rx_empty; 573e3859603SPaolo Abeni 574fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 575ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 576fd897679SPaolo Abeni 577fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 578fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 579fd897679SPaolo Abeni 580fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 581fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5827ed90803SPaolo Abeni } 5837ed90803SPaolo Abeni } 5847ed90803SPaolo Abeni 5857ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5863721b9b6SMat Martineau { 5873721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5883721b9b6SMat Martineau u64 rcv_data_fin_seq; 5897ed90803SPaolo Abeni bool ret = false; 5903721b9b6SMat Martineau 5913721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5923721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5933721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5943721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5953721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5963721b9b6SMat Martineau * the msk state. 5973721b9b6SMat Martineau * 5983721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5993721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 6003721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 6013721b9b6SMat Martineau * when catching up. 6023721b9b6SMat Martineau */ 6033721b9b6SMat Martineau 6043721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 605917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 6063721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 6073721b9b6SMat Martineau 6086b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 60916a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 6103721b9b6SMat Martineau 6113721b9b6SMat Martineau switch (sk->sk_state) { 6123721b9b6SMat Martineau case TCP_ESTABLISHED: 613d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE_WAIT); 6143721b9b6SMat Martineau break; 6153721b9b6SMat Martineau case TCP_FIN_WAIT1: 616d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSING); 6173721b9b6SMat Martineau break; 6183721b9b6SMat Martineau case TCP_FIN_WAIT2: 619d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 6203721b9b6SMat Martineau break; 6213721b9b6SMat Martineau default: 6223721b9b6SMat Martineau /* Other states not expected */ 6233721b9b6SMat Martineau WARN_ON_ONCE(1); 6243721b9b6SMat Martineau break; 6253721b9b6SMat Martineau } 6263721b9b6SMat Martineau 6277ed90803SPaolo Abeni ret = true; 62881c1d029SPaolo Abeni if (!__mptcp_check_fallback(msk)) 629fd897679SPaolo Abeni mptcp_send_ack(msk); 630e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6313721b9b6SMat Martineau } 6327ed90803SPaolo Abeni return ret; 6333721b9b6SMat Martineau } 6343721b9b6SMat Martineau 6356771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6366771bfd9SFlorian Westphal struct sock *ssk, 6376771bfd9SFlorian Westphal unsigned int *bytes) 6386771bfd9SFlorian Westphal { 6396771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 640600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6416771bfd9SFlorian Westphal unsigned int moved = 0; 6426771bfd9SFlorian Westphal bool more_data_avail; 6436771bfd9SFlorian Westphal struct tcp_sock *tp; 6446771bfd9SFlorian Westphal bool done = false; 64513c7ba0cSFlorian Westphal int sk_rbuf; 64613c7ba0cSFlorian Westphal 64713c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 64813c7ba0cSFlorian Westphal 64913c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 65013c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 65113c7ba0cSFlorian Westphal 65213c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 65313c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 65413c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 65513c7ba0cSFlorian Westphal } 65613c7ba0cSFlorian Westphal } 657600911ffSFlorian Westphal 658ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6596771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6606771bfd9SFlorian Westphal do { 6616771bfd9SFlorian Westphal u32 map_remaining, offset; 6626771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6636771bfd9SFlorian Westphal struct sk_buff *skb; 6646771bfd9SFlorian Westphal bool fin; 6656771bfd9SFlorian Westphal 6666771bfd9SFlorian Westphal /* try to move as much data as available */ 6676771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6686771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6696771bfd9SFlorian Westphal 6706771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 671d9fb8c50SPaolo Abeni if (!skb) { 672d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 673d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 674d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 675d9fb8c50SPaolo Abeni */ 676d9fb8c50SPaolo Abeni if (!moved) 677d9fb8c50SPaolo Abeni done = true; 6786771bfd9SFlorian Westphal break; 679d9fb8c50SPaolo Abeni } 6806771bfd9SFlorian Westphal 681e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 682d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 683d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 684d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 685e1ff9e82SDavide Caratti */ 686e1ff9e82SDavide Caratti map_remaining = skb->len; 687e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 688e1ff9e82SDavide Caratti } 689e1ff9e82SDavide Caratti 6906771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6916771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6926771bfd9SFlorian Westphal if (fin) { 6936771bfd9SFlorian Westphal done = true; 6946771bfd9SFlorian Westphal seq++; 6956771bfd9SFlorian Westphal } 6966771bfd9SFlorian Westphal 6976771bfd9SFlorian Westphal if (offset < skb->len) { 6986771bfd9SFlorian Westphal size_t len = skb->len - offset; 6996771bfd9SFlorian Westphal 7006771bfd9SFlorian Westphal if (tp->urg_data) 7016771bfd9SFlorian Westphal done = true; 7026771bfd9SFlorian Westphal 703ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 7046771bfd9SFlorian Westphal moved += len; 705ab174ad8SPaolo Abeni seq += len; 7066771bfd9SFlorian Westphal 7076771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 7086771bfd9SFlorian Westphal break; 7096771bfd9SFlorian Westphal } else { 7106771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 7116771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 7126771bfd9SFlorian Westphal done = true; 7136771bfd9SFlorian Westphal } 7146771bfd9SFlorian Westphal 7156771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7166771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 717600911ffSFlorian Westphal 71813c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 719600911ffSFlorian Westphal done = true; 720600911ffSFlorian Westphal break; 721600911ffSFlorian Westphal } 7226771bfd9SFlorian Westphal } while (more_data_avail); 7236771bfd9SFlorian Westphal 7246719331cSPaolo Abeni *bytes += moved; 7256771bfd9SFlorian Westphal return done; 7266771bfd9SFlorian Westphal } 7276771bfd9SFlorian Westphal 72887952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 729ab174ad8SPaolo Abeni { 730ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 731ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 732ab174ad8SPaolo Abeni bool moved = false; 733ab174ad8SPaolo Abeni struct rb_node *p; 734ab174ad8SPaolo Abeni u64 end_seq; 735ab174ad8SPaolo Abeni 736ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 73706242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 738ab174ad8SPaolo Abeni while (p) { 739ab174ad8SPaolo Abeni skb = rb_to_skb(p); 740ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 741ab174ad8SPaolo Abeni break; 742ab174ad8SPaolo Abeni 743ab174ad8SPaolo Abeni p = rb_next(p); 744ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 745ab174ad8SPaolo Abeni 746ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 747ab174ad8SPaolo Abeni msk->ack_seq))) { 748ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 74906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 750ab174ad8SPaolo Abeni continue; 751ab174ad8SPaolo Abeni } 752ab174ad8SPaolo Abeni 753ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 754ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 755ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 756ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 757ab174ad8SPaolo Abeni 758ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 75906242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 76006242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 76106242e44SPaolo Abeni delta); 762ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7632c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 764ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 765ab174ad8SPaolo Abeni } 76638967f42SPaolo Abeni msk->bytes_received += end_seq - msk->ack_seq; 767ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 768ab174ad8SPaolo Abeni moved = true; 769ab174ad8SPaolo Abeni } 770ab174ad8SPaolo Abeni return moved; 771ab174ad8SPaolo Abeni } 772ab174ad8SPaolo Abeni 7739f1a9881SPaolo Abeni static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk) 774d5fbeff1SPaolo Abeni { 775d5fbeff1SPaolo Abeni int err = sock_error(ssk); 776d5fbeff1SPaolo Abeni int ssk_state; 777d5fbeff1SPaolo Abeni 778d5fbeff1SPaolo Abeni if (!err) 7799f1a9881SPaolo Abeni return false; 780d5fbeff1SPaolo Abeni 781d5fbeff1SPaolo Abeni /* only propagate errors on fallen-back sockets or 782d5fbeff1SPaolo Abeni * on MPC connect 783d5fbeff1SPaolo Abeni */ 7849f1a9881SPaolo Abeni if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk))) 7859f1a9881SPaolo Abeni return false; 786d5fbeff1SPaolo Abeni 787d5fbeff1SPaolo Abeni /* We need to propagate only transition to CLOSE state. 788d5fbeff1SPaolo Abeni * Orphaned socket will see such state change via 789d5fbeff1SPaolo Abeni * subflow_sched_work_if_closed() and that path will properly 790d5fbeff1SPaolo Abeni * destroy the msk as needed. 791d5fbeff1SPaolo Abeni */ 792d5fbeff1SPaolo Abeni ssk_state = inet_sk_state_load(ssk); 793d5fbeff1SPaolo Abeni if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD)) 794d52b3c2bSGeliang Tang mptcp_set_state(sk, ssk_state); 795d5fbeff1SPaolo Abeni WRITE_ONCE(sk->sk_err, -err); 796d5fbeff1SPaolo Abeni 797d5fbeff1SPaolo Abeni /* This barrier is coupled with smp_rmb() in mptcp_poll() */ 798d5fbeff1SPaolo Abeni smp_wmb(); 799d5fbeff1SPaolo Abeni sk_error_report(sk); 8009f1a9881SPaolo Abeni return true; 801d5fbeff1SPaolo Abeni } 8029f1a9881SPaolo Abeni 8039f1a9881SPaolo Abeni void __mptcp_error_report(struct sock *sk) 8049f1a9881SPaolo Abeni { 8059f1a9881SPaolo Abeni struct mptcp_subflow_context *subflow; 8069f1a9881SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 8079f1a9881SPaolo Abeni 8089f1a9881SPaolo Abeni mptcp_for_each_subflow(msk, subflow) 8099f1a9881SPaolo Abeni if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow))) 8109f1a9881SPaolo Abeni break; 811d5fbeff1SPaolo Abeni } 812d5fbeff1SPaolo Abeni 8132e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 8142e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 8152e52213cSFlorian Westphal */ 81699d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 8172e52213cSFlorian Westphal { 8182e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 8192e52213cSFlorian Westphal unsigned int moved = 0; 8202e52213cSFlorian Westphal 8212e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 82287952603SPaolo Abeni __mptcp_ofo_queue(msk); 823499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 824499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 825499ada50SPaolo Abeni __mptcp_error_report(sk); 826499ada50SPaolo Abeni else 827e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 828499ada50SPaolo Abeni } 829ab174ad8SPaolo Abeni 830ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 831ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 832ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 833ab174ad8SPaolo Abeni * do it. 834ab174ad8SPaolo Abeni */ 835ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 836ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 83799d1055cSPaolo Abeni return moved > 0; 8382e52213cSFlorian Westphal } 8392e52213cSFlorian Westphal 8402e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 841101f6f85SFlorian Westphal { 8426719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 843101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 84413c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 845101f6f85SFlorian Westphal 846d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 847d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 848d7b1bfd0SPaolo Abeni * more data to the msk receive queue 849d7b1bfd0SPaolo Abeni */ 850d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 851d7b1bfd0SPaolo Abeni return; 852d7b1bfd0SPaolo Abeni 85313c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 85413c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 85513c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 85613c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 85713c7ba0cSFlorian Westphal 85899d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 859ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 860ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 86199d1055cSPaolo Abeni return; 862ce599c51SPaolo Abeni } 8632e52213cSFlorian Westphal 86499d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 86599d1055cSPaolo Abeni mptcp_data_lock(sk); 866612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 867101f6f85SFlorian Westphal sk->sk_data_ready(sk); 868612f71d7SPaolo Abeni 86999d1055cSPaolo Abeni mptcp_data_unlock(sk); 87099d1055cSPaolo Abeni } 871101f6f85SFlorian Westphal 87255b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 87355b47ca7SPaolo Abeni { 87455b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 87555b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 87655b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 87755b47ca7SPaolo Abeni } 87855b47ca7SPaolo Abeni 8793e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 880ec3edaa7SPeter Krystad { 8813e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8825cf92bbaSPaolo Abeni 8833e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 88478962489SFlorian Westphal return false; 885ec3edaa7SPeter Krystad 8863e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8873e501490SPaolo Abeni * at close time 8883e501490SPaolo Abeni */ 8893e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8903e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 89178962489SFlorian Westphal 8926f06b4d4SPaolo Abeni mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; 8933e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 89455b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 89527e5ccc2SPaolo Abeni mptcp_stop_tout_timer(sk); 89634c7757aSPaolo Abeni __mptcp_propagate_sndbuf(sk, ssk); 8973e501490SPaolo Abeni return true; 89878962489SFlorian Westphal } 89978962489SFlorian Westphal 90056a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 90178962489SFlorian Westphal { 9023e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 9033e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 90478962489SFlorian Westphal 90556a666c4SPaolo Abeni list_for_each_entry_safe(subflow, tmp, join_list, node) { 9063e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 9073e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 9083e501490SPaolo Abeni 9093e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 9103e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 9113e501490SPaolo Abeni mptcp_subflow_reset(ssk); 9123e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 91378962489SFlorian Westphal } 914ec3edaa7SPeter Krystad } 915ec3edaa7SPeter Krystad 916f6909dc1SPaolo Abeni static bool mptcp_rtx_timer_pending(struct sock *sk) 917b51f9b80SPaolo Abeni { 918b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 919b51f9b80SPaolo Abeni } 920b51f9b80SPaolo Abeni 921f6909dc1SPaolo Abeni static void mptcp_reset_rtx_timer(struct sock *sk) 922b51f9b80SPaolo Abeni { 923b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 924b51f9b80SPaolo Abeni unsigned long tout; 925b51f9b80SPaolo Abeni 926e16163b6SPaolo Abeni /* prevent rescheduling on close */ 927e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 928e16163b6SPaolo Abeni return; 929e16163b6SPaolo Abeni 93033d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 931b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 932b51f9b80SPaolo Abeni } 933b51f9b80SPaolo Abeni 934ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 935ba8f48f7SPaolo Abeni { 936ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 937ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 938ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 939ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 940ba8f48f7SPaolo Abeni */ 941ba8f48f7SPaolo Abeni sock_hold(sk); 942ba8f48f7SPaolo Abeni return true; 943ba8f48f7SPaolo Abeni } 944ba8f48f7SPaolo Abeni return false; 945ba8f48f7SPaolo Abeni } 946ba8f48f7SPaolo Abeni 9477a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9487a6a6cbcSPaolo Abeni { 9497a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9507a6a6cbcSPaolo Abeni 951109cdeb8SGeliang Tang msk_owned_by_me(msk); 9527a6a6cbcSPaolo Abeni 9537a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 95499d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9557a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9567a6a6cbcSPaolo Abeni } 9577a6a6cbcSPaolo Abeni 9587a6a6cbcSPaolo Abeni return NULL; 9597a6a6cbcSPaolo Abeni } 9607a6a6cbcSPaolo Abeni 9613f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 96257040755SPaolo Abeni const struct sk_buff *skb, 96357040755SPaolo Abeni const struct mptcp_ext *mpext) 9646d0060f6SMat Martineau { 96557040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 96657040755SPaolo Abeni return false; 96757040755SPaolo Abeni 9685a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9695a369ca6SPaolo Abeni * mapping has not been xmitted yet 9705a369ca6SPaolo Abeni */ 9715a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9725a369ca6SPaolo Abeni !mpext->frozen; 97357040755SPaolo Abeni } 97457040755SPaolo Abeni 97529249eacSPaolo Abeni /* we can append data to the given data frag if: 97629249eacSPaolo Abeni * - there is space available in the backing page_frag 97729249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 97829249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 97929249eacSPaolo Abeni */ 98018b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 98118b683bfSPaolo Abeni const struct page_frag *pfrag, 98218b683bfSPaolo Abeni const struct mptcp_data_frag *df) 98318b683bfSPaolo Abeni { 98418b683bfSPaolo Abeni return df && pfrag->page == df->page && 985d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 98629249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 98718b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 98818b683bfSPaolo Abeni } 98918b683bfSPaolo Abeni 990d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 99118b683bfSPaolo Abeni { 992d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9937948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 994d027236cSPaolo Abeni } 995d027236cSPaolo Abeni 996d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 997d027236cSPaolo Abeni { 998d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 999d027236cSPaolo Abeni 100018b683bfSPaolo Abeni list_del(&dfrag->list); 1001d027236cSPaolo Abeni dfrag_uncharge(sk, len); 100218b683bfSPaolo Abeni put_page(dfrag->page); 100318b683bfSPaolo Abeni } 100418b683bfSPaolo Abeni 10056e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 100618b683bfSPaolo Abeni { 100718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1009e1ff9e82SDavide Caratti u64 snd_una; 1010e1ff9e82SDavide Caratti 10117439d687SPaolo Abeni snd_una = msk->snd_una; 101218b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 101318b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 101418b683bfSPaolo Abeni break; 101518b683bfSPaolo Abeni 10161e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10171e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10181e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1019d9ca1de8SPaolo Abeni break; 10201e1d9d6fSPaolo Abeni 10211e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10221e1d9d6fSPaolo Abeni } 10231e1d9d6fSPaolo Abeni 1024d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1025d027236cSPaolo Abeni } 1026d027236cSPaolo Abeni 10277948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10287948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 102953eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 103053eb4c38SPaolo Abeni 10311e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10321e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10331e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 103453eb4c38SPaolo Abeni goto out; 10351e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10361e1d9d6fSPaolo Abeni goto out; 10371e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10381e1d9d6fSPaolo Abeni } 10397948f6ccSFlorian Westphal 10407948f6ccSFlorian Westphal dfrag->data_seq += delta; 104153eb4c38SPaolo Abeni dfrag->offset += delta; 10427948f6ccSFlorian Westphal dfrag->data_len -= delta; 1043d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10447948f6ccSFlorian Westphal 10457948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10467948f6ccSFlorian Westphal } 10477948f6ccSFlorian Westphal 10481e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10491e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10501e1d9d6fSPaolo Abeni msk->recovery = false; 10511e1d9d6fSPaolo Abeni 105253eb4c38SPaolo Abeni out: 10533241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10543241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 1055f6909dc1SPaolo Abeni if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 1056f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 10576e628cd3SPaolo Abeni } else { 1058f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 10596e628cd3SPaolo Abeni } 10606e628cd3SPaolo Abeni } 10616e628cd3SPaolo Abeni 1062417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1063417789dfSPaolo Abeni { 1064765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1065765ff425SPaolo Abeni 1066417789dfSPaolo Abeni __mptcp_clean_una(sk); 1067417789dfSPaolo Abeni mptcp_write_space(sk); 1068417789dfSPaolo Abeni } 1069417789dfSPaolo Abeni 1070b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1071b5941f06SPaolo Abeni { 1072b5941f06SPaolo Abeni mptcp_data_lock(sk); 1073b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1074b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1075b5941f06SPaolo Abeni } 1076b5941f06SPaolo Abeni 1077724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 107818b683bfSPaolo Abeni { 1079d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1080d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1081d9ca1de8SPaolo Abeni bool first = true; 1082d9ca1de8SPaolo Abeni 1083d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1084d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1085d9ca1de8SPaolo Abeni 1086d9ca1de8SPaolo Abeni if (first) 1087d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1088d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 108934c7757aSPaolo Abeni 1090d9ca1de8SPaolo Abeni first = false; 1091d9ca1de8SPaolo Abeni } 109234c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 1093724cfd2eSPaolo Abeni } 1094724cfd2eSPaolo Abeni 1095724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1096724cfd2eSPaolo Abeni * data 1097724cfd2eSPaolo Abeni */ 1098724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1099724cfd2eSPaolo Abeni { 1100724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1101724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1102724cfd2eSPaolo Abeni return true; 1103724cfd2eSPaolo Abeni 1104724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 110518b683bfSPaolo Abeni return false; 110618b683bfSPaolo Abeni } 110718b683bfSPaolo Abeni 110818b683bfSPaolo Abeni static struct mptcp_data_frag * 110918b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 111018b683bfSPaolo Abeni int orig_offset) 111118b683bfSPaolo Abeni { 111218b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 111318b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 111418b683bfSPaolo Abeni 111518b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 111618b683bfSPaolo Abeni dfrag->data_len = 0; 111718b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 111818b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 111918b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1120d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 112118b683bfSPaolo Abeni dfrag->page = pfrag->page; 112218b683bfSPaolo Abeni 112318b683bfSPaolo Abeni return dfrag; 112418b683bfSPaolo Abeni } 112518b683bfSPaolo Abeni 1126caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1127caf971dfSPaolo Abeni int mss_now; 1128caf971dfSPaolo Abeni int size_goal; 1129d9ca1de8SPaolo Abeni u16 limit; 1130d9ca1de8SPaolo Abeni u16 sent; 1131d9ca1de8SPaolo Abeni unsigned int flags; 11321094c6feSPaolo Abeni bool data_lock_held; 1133caf971dfSPaolo Abeni }; 1134caf971dfSPaolo Abeni 1135b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1136b713d006SPaolo Abeni u64 data_seq, int avail_size) 11376f8a612aSFlorian Westphal { 11386f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1139b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11406f8a612aSFlorian Westphal 11416f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11426f8a612aSFlorian Westphal return avail_size; 11436f8a612aSFlorian Westphal 1144b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1145b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11466f8a612aSFlorian Westphal 114792be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1148b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 114992be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 115092be2f52SPaolo Abeni } 11516f8a612aSFlorian Westphal 11526f8a612aSFlorian Westphal return avail_size; 11536f8a612aSFlorian Westphal } 11546f8a612aSFlorian Westphal 1155724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1156724cfd2eSPaolo Abeni { 1157724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1158724cfd2eSPaolo Abeni 1159724cfd2eSPaolo Abeni if (!mpext) 1160724cfd2eSPaolo Abeni return false; 1161724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1162724cfd2eSPaolo Abeni return true; 1163724cfd2eSPaolo Abeni } 1164724cfd2eSPaolo Abeni 11656e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1166724cfd2eSPaolo Abeni { 1167724cfd2eSPaolo Abeni struct sk_buff *skb; 1168724cfd2eSPaolo Abeni 11696e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1170724cfd2eSPaolo Abeni if (likely(skb)) { 11716e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1172724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1173a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1174f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1175724cfd2eSPaolo Abeni return skb; 1176724cfd2eSPaolo Abeni } 1177724cfd2eSPaolo Abeni __kfree_skb(skb); 1178724cfd2eSPaolo Abeni } else { 1179724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1180724cfd2eSPaolo Abeni } 1181724cfd2eSPaolo Abeni return NULL; 1182724cfd2eSPaolo Abeni } 1183724cfd2eSPaolo Abeni 1184f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1185724cfd2eSPaolo Abeni { 1186724cfd2eSPaolo Abeni struct sk_buff *skb; 1187724cfd2eSPaolo Abeni 11886e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1189724cfd2eSPaolo Abeni if (!skb) 1190f70cad10SPaolo Abeni return NULL; 1191724cfd2eSPaolo Abeni 1192724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1193f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1194f70cad10SPaolo Abeni return skb; 1195724cfd2eSPaolo Abeni } 11963ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1197724cfd2eSPaolo Abeni kfree_skb(skb); 1198f70cad10SPaolo Abeni return NULL; 1199724cfd2eSPaolo Abeni } 1200724cfd2eSPaolo Abeni 1201f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1202724cfd2eSPaolo Abeni { 12031094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1204724cfd2eSPaolo Abeni 12051094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1206724cfd2eSPaolo Abeni } 1207724cfd2eSPaolo Abeni 1208d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1209d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1210d0cc2987SGeliang Tang */ 1211d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1212d0cc2987SGeliang Tang { 1213d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1214d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1215d0cc2987SGeliang Tang int offset = skb->len - added; 1216d0cc2987SGeliang Tang 1217d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1218d0cc2987SGeliang Tang } 1219d0cc2987SGeliang Tang 12201e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12211e39e5a3SGeliang Tang struct sock *ssk, 12221e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12231e39e5a3SGeliang Tang { 12241e39e5a3SGeliang Tang if (!mpext) 12251e39e5a3SGeliang Tang return; 12261e39e5a3SGeliang Tang 12271e39e5a3SGeliang Tang mpext->infinite_map = 1; 12281e39e5a3SGeliang Tang mpext->data_len = 0; 12291e39e5a3SGeliang Tang 1230104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12311e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12321e39e5a3SGeliang Tang pr_fallback(msk); 1233d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12341e39e5a3SGeliang Tang } 12351e39e5a3SGeliang Tang 123657ced2ebSPaolo Abeni #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1)) 123757ced2ebSPaolo Abeni 123857040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1239d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1240caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 124157040755SPaolo Abeni { 1242d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1243f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12446d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12456f8a612aSFlorian Westphal bool zero_window_probe = false; 12466d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1247f70cad10SPaolo Abeni bool can_coalesce = false; 1248f70cad10SPaolo Abeni bool reuse_skb = true; 1249f70cad10SPaolo Abeni struct sk_buff *skb; 1250f70cad10SPaolo Abeni size_t copy; 1251f70cad10SPaolo Abeni int i; 12526d0060f6SMat Martineau 1253e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1254d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12556d0060f6SMat Martineau 1256f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1257f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1258f70cad10SPaolo Abeni return 0; 1259f70cad10SPaolo Abeni 1260c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1261c886d702SPaolo Abeni return -EAGAIN; 1262c886d702SPaolo Abeni 1263d9ca1de8SPaolo Abeni /* compute send limit */ 126457ced2ebSPaolo Abeni if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE)) 126557ced2ebSPaolo Abeni ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE; 1266d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1267f70cad10SPaolo Abeni copy = info->size_goal; 1268f70cad10SPaolo Abeni 126957040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1270f70cad10SPaolo Abeni if (skb && copy > skb->len) { 127157040755SPaolo Abeni /* Limit the write to the size available in the 127257040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 127357040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 127457040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 127557040755SPaolo Abeni * SSN association set here 127657040755SPaolo Abeni */ 1277d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12781094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 127957040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12801094c6feSPaolo Abeni goto alloc_skb; 12811094c6feSPaolo Abeni } 12821094c6feSPaolo Abeni 1283f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1284f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1285657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1286f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1287f70cad10SPaolo Abeni goto alloc_skb; 128815e6ca97SPaolo Abeni } 128918b683bfSPaolo Abeni 1290f70cad10SPaolo Abeni copy -= skb->len; 1291f70cad10SPaolo Abeni } else { 12921094c6feSPaolo Abeni alloc_skb: 1293f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1294f70cad10SPaolo Abeni if (!skb) 1295f70cad10SPaolo Abeni return -ENOMEM; 1296f70cad10SPaolo Abeni 1297f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1298f70cad10SPaolo Abeni reuse_skb = false; 1299f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1300f70cad10SPaolo Abeni } 13011094c6feSPaolo Abeni 13026f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1303b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1304f70cad10SPaolo Abeni if (copy == 0) { 13057439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 13067439d687SPaolo Abeni 130772377ab2SPaolo Abeni if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) { 130827728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 13096f8a612aSFlorian Westphal return 0; 13106f8a612aSFlorian Westphal } 13116f8a612aSFlorian Westphal 1312f70cad10SPaolo Abeni zero_window_probe = true; 1313f70cad10SPaolo Abeni data_seq = snd_una - 1; 1314f70cad10SPaolo Abeni copy = 1; 1315f70cad10SPaolo Abeni } 1316f70cad10SPaolo Abeni 1317f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1318f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 131927728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1320e2223995SPaolo Abeni return -ENOMEM; 132135759383SFlorian Westphal } 132218b683bfSPaolo Abeni 1323f70cad10SPaolo Abeni if (can_coalesce) { 1324f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1325f70cad10SPaolo Abeni } else { 1326f70cad10SPaolo Abeni get_page(dfrag->page); 1327f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 132857040755SPaolo Abeni } 132957040755SPaolo Abeni 1330f70cad10SPaolo Abeni skb->len += copy; 1331f70cad10SPaolo Abeni skb->data_len += copy; 1332f70cad10SPaolo Abeni skb->truesize += copy; 1333f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1334f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1335f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1336f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1337f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1338f70cad10SPaolo Abeni 1339f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1340f70cad10SPaolo Abeni if (reuse_skb) { 1341f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1342f70cad10SPaolo Abeni mpext->data_len += copy; 1343f70cad10SPaolo Abeni goto out; 1344724cfd2eSPaolo Abeni } 13456d0060f6SMat Martineau 13466d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1347d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13486d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1349f70cad10SPaolo Abeni mpext->data_len = copy; 13506d0060f6SMat Martineau mpext->use_map = 1; 13516d0060f6SMat Martineau mpext->dsn64 = 1; 13526d0060f6SMat Martineau 13536d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13546d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13556d0060f6SMat Martineau mpext->dsn64); 13566d0060f6SMat Martineau 13576f8a612aSFlorian Westphal if (zero_window_probe) { 1358f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13596f8a612aSFlorian Westphal mpext->frozen = 1; 1360d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1361f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13626f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1363d0cc2987SGeliang Tang return 0; 13646f8a612aSFlorian Westphal } 136557040755SPaolo Abeni out: 1366d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1367f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13681e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13691e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13700eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1371f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1372f70cad10SPaolo Abeni return copy; 13736d0060f6SMat Martineau } 13746d0060f6SMat Martineau 1375d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1376d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1377d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1378d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1379d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1380d5f49190SPaolo Abeni 1381d5f49190SPaolo Abeni struct subflow_send_info { 1382d5f49190SPaolo Abeni struct sock *ssk; 13833ce0852cSPaolo Abeni u64 linger_time; 1384d5f49190SPaolo Abeni }; 1385d5f49190SPaolo Abeni 1386ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1387ff5a0b42SPaolo Abeni { 1388ff5a0b42SPaolo Abeni if (!subflow->stale) 1389ff5a0b42SPaolo Abeni return; 1390ff5a0b42SPaolo Abeni 1391ff5a0b42SPaolo Abeni subflow->stale = 0; 1392fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1393ff5a0b42SPaolo Abeni } 1394ff5a0b42SPaolo Abeni 1395ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1396ff5a0b42SPaolo Abeni { 1397ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1398ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1399ff5a0b42SPaolo Abeni 1400ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1401ff5a0b42SPaolo Abeni return false; 1402ff5a0b42SPaolo Abeni 1403ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1404ff5a0b42SPaolo Abeni } 1405ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1406ff5a0b42SPaolo Abeni } 1407ff5a0b42SPaolo Abeni 14083ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14093ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14103ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14113ce0852cSPaolo Abeni 141233d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 141333d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 141433d41c9cSPaolo Abeni * additionally updates the rtx timeout 141533d41c9cSPaolo Abeni */ 141607336a87SGeliang Tang struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1417f296234cSPeter Krystad { 14183ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1419f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 142033d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14213ce0852cSPaolo Abeni u32 pace, burst, wmem; 1422d5f49190SPaolo Abeni int i, nr_active = 0; 1423d5f49190SPaolo Abeni struct sock *ssk; 14243ce0852cSPaolo Abeni u64 linger_time; 142533d41c9cSPaolo Abeni long tout = 0; 1426f296234cSPeter Krystad 1427d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14283ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1429d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14303ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1431d5f49190SPaolo Abeni } 14323ce0852cSPaolo Abeni 1433d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1434e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1435d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1436d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1437f296234cSPeter Krystad continue; 1438d5f49190SPaolo Abeni 143933d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1440d5f49190SPaolo Abeni nr_active += !subflow->backup; 14413ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14423ce0852cSPaolo Abeni if (unlikely(!pace)) { 14433ce0852cSPaolo Abeni /* init pacing rate from socket */ 14443ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14453ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1446d5f49190SPaolo Abeni if (!pace) 1447d5f49190SPaolo Abeni continue; 14483ce0852cSPaolo Abeni } 1449d5f49190SPaolo Abeni 14503ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14513ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1452d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14533ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1454d5f49190SPaolo Abeni } 1455f296234cSPeter Krystad } 145633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1457f296234cSPeter Krystad 1458d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1459d5f49190SPaolo Abeni if (!nr_active) 14603ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1461d5f49190SPaolo Abeni 14623ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14633ce0852cSPaolo Abeni * faster flow, we need to: 14643ce0852cSPaolo Abeni * - estimate the faster flow linger time 14653ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14663ce0852cSPaolo Abeni * by the faster flow 14673ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14683ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14693ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14703ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14713ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14723ce0852cSPaolo Abeni */ 14733ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1474b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1475d5f49190SPaolo Abeni return NULL; 14763ce0852cSPaolo Abeni 1477b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14783ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1479ebc1e08fSGeliang Tang if (!burst) 1480b713d006SPaolo Abeni return ssk; 1481b713d006SPaolo Abeni 14823ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14833ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14843ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14853ce0852cSPaolo Abeni burst + wmem); 14863ce0852cSPaolo Abeni msk->snd_burst = burst; 14873ce0852cSPaolo Abeni return ssk; 1488f296234cSPeter Krystad } 1489f296234cSPeter Krystad 1490b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1491d9ca1de8SPaolo Abeni { 1492d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1493d9ca1de8SPaolo Abeni release_sock(ssk); 1494d9ca1de8SPaolo Abeni } 1495d9ca1de8SPaolo Abeni 14960d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14970d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14980d199e43SFlorian Westphal u32 sent) 14990d199e43SFlorian Westphal { 15000d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15010d199e43SFlorian Westphal 15020d199e43SFlorian Westphal dfrag->already_sent += sent; 15030d199e43SFlorian Westphal 15040d199e43SFlorian Westphal msk->snd_burst -= sent; 15050d199e43SFlorian Westphal 15060d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15070d199e43SFlorian Westphal 15080d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15090d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15100d199e43SFlorian Westphal * old segments. 15110d199e43SFlorian Westphal * 15120d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15130d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15140d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15150d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15160d199e43SFlorian Westphal */ 151738967f42SPaolo Abeni if (likely(after64(snd_nxt_new, msk->snd_nxt))) { 151838967f42SPaolo Abeni msk->bytes_sent += snd_nxt_new - msk->snd_nxt; 15190d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15200d199e43SFlorian Westphal } 152138967f42SPaolo Abeni } 15220d199e43SFlorian Westphal 15238b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15243241a9c0SFlorian Westphal { 1525877a72e3SPaolo Abeni if (mptcp_send_head(sk)) { 1526877a72e3SPaolo Abeni mptcp_data_lock(sk); 1527877a72e3SPaolo Abeni mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING); 1528877a72e3SPaolo Abeni mptcp_data_unlock(sk); 1529877a72e3SPaolo Abeni } 15303241a9c0SFlorian Westphal } 15313241a9c0SFlorian Westphal 1532c5b4297dSGeliang Tang static int __subflow_push_pending(struct sock *sk, struct sock *ssk, 1533c5b4297dSGeliang Tang struct mptcp_sendmsg_info *info) 1534c5b4297dSGeliang Tang { 1535c5b4297dSGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 1536c5b4297dSGeliang Tang struct mptcp_data_frag *dfrag; 1537c5b4297dSGeliang Tang int len, copied = 0, err = 0; 1538c5b4297dSGeliang Tang 1539c5b4297dSGeliang Tang while ((dfrag = mptcp_send_head(sk))) { 1540c5b4297dSGeliang Tang info->sent = dfrag->already_sent; 1541c5b4297dSGeliang Tang info->limit = dfrag->data_len; 1542c5b4297dSGeliang Tang len = dfrag->data_len - dfrag->already_sent; 1543c5b4297dSGeliang Tang while (len > 0) { 1544c5b4297dSGeliang Tang int ret = 0; 1545c5b4297dSGeliang Tang 1546c5b4297dSGeliang Tang ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info); 1547c5b4297dSGeliang Tang if (ret <= 0) { 1548c5b4297dSGeliang Tang err = copied ? : ret; 1549c5b4297dSGeliang Tang goto out; 1550c5b4297dSGeliang Tang } 1551c5b4297dSGeliang Tang 1552c5b4297dSGeliang Tang info->sent += ret; 1553c5b4297dSGeliang Tang copied += ret; 1554c5b4297dSGeliang Tang len -= ret; 1555c5b4297dSGeliang Tang 1556c5b4297dSGeliang Tang mptcp_update_post_push(msk, dfrag, ret); 1557c5b4297dSGeliang Tang } 1558c5b4297dSGeliang Tang WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1559c5b4297dSGeliang Tang 1560c5b4297dSGeliang Tang if (msk->snd_burst <= 0 || 1561c5b4297dSGeliang Tang !sk_stream_memory_free(ssk) || 1562c5b4297dSGeliang Tang !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) { 1563c5b4297dSGeliang Tang err = copied; 1564c5b4297dSGeliang Tang goto out; 1565c5b4297dSGeliang Tang } 1566c5b4297dSGeliang Tang mptcp_set_timeout(sk); 1567c5b4297dSGeliang Tang } 1568c5b4297dSGeliang Tang err = copied; 1569c5b4297dSGeliang Tang 1570c5b4297dSGeliang Tang out: 1571c5b4297dSGeliang Tang return err; 1572c5b4297dSGeliang Tang } 1573c5b4297dSGeliang Tang 1574ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1575d9ca1de8SPaolo Abeni { 1576d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1577d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1578d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1579d9ca1de8SPaolo Abeni .flags = flags, 1580d9ca1de8SPaolo Abeni }; 15810522b424SGeliang Tang bool do_check_data_fin = false; 15820fa1b378SGeliang Tang int push_count = 1; 1583d9ca1de8SPaolo Abeni 15840fa1b378SGeliang Tang while (mptcp_send_head(sk) && (push_count > 0)) { 15850fa1b378SGeliang Tang struct mptcp_subflow_context *subflow; 1586d9ca1de8SPaolo Abeni int ret = 0; 1587d9ca1de8SPaolo Abeni 15880fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 15890fa1b378SGeliang Tang break; 1590d9ca1de8SPaolo Abeni 15910fa1b378SGeliang Tang push_count = 0; 15920fa1b378SGeliang Tang 15930fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 15940fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 15950fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 15960fa1b378SGeliang Tang 15970fa1b378SGeliang Tang prev_ssk = ssk; 15980fa1b378SGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 15990fa1b378SGeliang Tang if (ssk != prev_ssk) { 16009758f40eSPaolo Abeni /* First check. If the ssk has changed since 16019758f40eSPaolo Abeni * the last round, release prev_ssk 1602d9ca1de8SPaolo Abeni */ 16030fa1b378SGeliang Tang if (prev_ssk) 1604b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1605d9ca1de8SPaolo Abeni 16069758f40eSPaolo Abeni /* Need to lock the new subflow only if different 16079758f40eSPaolo Abeni * from the previous one, otherwise we are still 16089758f40eSPaolo Abeni * helding the relevant lock 16099758f40eSPaolo Abeni */ 1610d9ca1de8SPaolo Abeni lock_sock(ssk); 16110fa1b378SGeliang Tang } 16120fa1b378SGeliang Tang 16130fa1b378SGeliang Tang push_count++; 1614d9ca1de8SPaolo Abeni 1615c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1616d9ca1de8SPaolo Abeni if (ret <= 0) { 16170fa1b378SGeliang Tang if (ret != -EAGAIN || 16180fa1b378SGeliang Tang (1 << ssk->sk_state) & 16190fa1b378SGeliang Tang (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE)) 16200fa1b378SGeliang Tang push_count--; 1621c886d702SPaolo Abeni continue; 1622d9ca1de8SPaolo Abeni } 16230522b424SGeliang Tang do_check_data_fin = true; 1624d9ca1de8SPaolo Abeni } 16250fa1b378SGeliang Tang } 16260fa1b378SGeliang Tang } 1627d9ca1de8SPaolo Abeni 1628d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1629d9ca1de8SPaolo Abeni if (ssk) 1630b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1631d9ca1de8SPaolo Abeni 163233d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1633f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 1634f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 16350522b424SGeliang Tang if (do_check_data_fin) 163681c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 1637d9ca1de8SPaolo Abeni } 1638d9ca1de8SPaolo Abeni 163973a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 16406e628cd3SPaolo Abeni { 16416e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16421094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16431094c6feSPaolo Abeni .data_lock_held = true, 16441094c6feSPaolo Abeni }; 16450fa1b378SGeliang Tang bool keep_pushing = true; 1646b19bc294SPaolo Abeni struct sock *xmit_ssk; 1647c5b4297dSGeliang Tang int copied = 0; 16486e628cd3SPaolo Abeni 16496e628cd3SPaolo Abeni info.flags = 0; 16500fa1b378SGeliang Tang while (mptcp_send_head(sk) && keep_pushing) { 16510fa1b378SGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 16526e628cd3SPaolo Abeni int ret = 0; 16536e628cd3SPaolo Abeni 165473a0052aSGeliang Tang /* check for a different subflow usage only after 1655b19bc294SPaolo Abeni * spooling the first chunk of data 1656b19bc294SPaolo Abeni */ 16570fa1b378SGeliang Tang if (first) { 16580fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 1659c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1660b19bc294SPaolo Abeni first = false; 1661c5b4297dSGeliang Tang if (ret <= 0) 1662c5b4297dSGeliang Tang break; 1663c5b4297dSGeliang Tang copied += ret; 16640fa1b378SGeliang Tang continue; 16650fa1b378SGeliang Tang } 16660fa1b378SGeliang Tang 16670fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 16680fa1b378SGeliang Tang goto out; 16690fa1b378SGeliang Tang 16700fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16710fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 16720fa1b378SGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 16730fa1b378SGeliang Tang if (ret <= 0) 16740fa1b378SGeliang Tang keep_pushing = false; 16750fa1b378SGeliang Tang copied += ret; 16760fa1b378SGeliang Tang } 16770fa1b378SGeliang Tang 16780fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 16790fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16800fa1b378SGeliang Tang xmit_ssk = mptcp_subflow_tcp_sock(subflow); 16810fa1b378SGeliang Tang if (xmit_ssk != ssk) { 16820fa1b378SGeliang Tang mptcp_subflow_delegate(subflow, 16830fa1b378SGeliang Tang MPTCP_DELEGATE_SEND); 16840fa1b378SGeliang Tang keep_pushing = false; 16850fa1b378SGeliang Tang } 16860fa1b378SGeliang Tang } 16870fa1b378SGeliang Tang } 16886e628cd3SPaolo Abeni } 16896e628cd3SPaolo Abeni 16906e628cd3SPaolo Abeni out: 16916e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16926e628cd3SPaolo Abeni * not going to flush it via release_sock() 16936e628cd3SPaolo Abeni */ 16946e628cd3SPaolo Abeni if (copied) { 16956e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16966e628cd3SPaolo Abeni info.size_goal); 1697f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 1698f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 1699d09d818eSPaolo Abeni 17006e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 17016e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 17026e628cd3SPaolo Abeni mptcp_schedule_work(sk); 17036e628cd3SPaolo Abeni } 17046e628cd3SPaolo Abeni } 17056e628cd3SPaolo Abeni 17065cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 17075cf92bbaSPaolo Abeni { 17085cf92bbaSPaolo Abeni /* enable autotune */ 17095cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 17105cf92bbaSPaolo Abeni 17115cf92bbaSPaolo Abeni /* will be cleared on avail space */ 17125cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 17135cf92bbaSPaolo Abeni } 17145cf92bbaSPaolo Abeni 17157d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 17167d803344SPaolo Abeni 1717a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1718fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1719fa9e5746SPaolo Abeni { 1720fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1721fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1722a2702a07SPaolo Abeni struct sock *ssk; 1723fa9e5746SPaolo Abeni int ret; 1724fa9e5746SPaolo Abeni 1725a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1726a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1727a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1728a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1729a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1730a2702a07SPaolo Abeni */ 1731ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 17323f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 17333f326a82SPaolo Abeni if (IS_ERR(ssk)) 17343f326a82SPaolo Abeni return PTR_ERR(ssk); 1735ddb1a072SPaolo Abeni } 1736a2702a07SPaolo Abeni if (!msk->first) 1737a2702a07SPaolo Abeni return -EINVAL; 1738a2702a07SPaolo Abeni 1739a2702a07SPaolo Abeni ssk = msk->first; 1740a2702a07SPaolo Abeni 1741fa9e5746SPaolo Abeni lock_sock(ssk); 1742fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 17437d803344SPaolo Abeni msk->fastopening = 1; 1744fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 17457d803344SPaolo Abeni msk->fastopening = 0; 1746fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1747fa9e5746SPaolo Abeni release_sock(ssk); 1748fa9e5746SPaolo Abeni 1749fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1750fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1751fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1752fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1753fa9e5746SPaolo Abeni 1754fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1755fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1756fa9e5746SPaolo Abeni */ 1757fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1758fa9e5746SPaolo Abeni *copied_syn = 0; 17597d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 1760c2b2ae39SPaolo Abeni /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1761c2b2ae39SPaolo Abeni * __inet_stream_connect() can fail, due to looking check, 1762c2b2ae39SPaolo Abeni * see mptcp_disconnect(). 1763c2b2ae39SPaolo Abeni * Attempt it again outside the problematic scope. 1764c2b2ae39SPaolo Abeni */ 1765c2b2ae39SPaolo Abeni if (!mptcp_disconnect(sk, 0)) 1766c2b2ae39SPaolo Abeni sk->sk_socket->state = SS_UNCONNECTED; 1767fa9e5746SPaolo Abeni } 176808e39c0dSEric Dumazet inet_clear_bit(DEFER_CONNECT, sk); 1769fa9e5746SPaolo Abeni 1770fa9e5746SPaolo Abeni return ret; 1771fa9e5746SPaolo Abeni } 1772fa9e5746SPaolo Abeni 1773f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1774f870fa0bSMat Martineau { 1775f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 177617091708SFlorian Westphal struct page_frag *pfrag; 17776d0060f6SMat Martineau size_t copied = 0; 1778caf971dfSPaolo Abeni int ret = 0; 17796d0060f6SMat Martineau long timeo; 1780f870fa0bSMat Martineau 1781987858e5SPaolo Abeni /* silently ignore everything else */ 17821e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1783987858e5SPaolo Abeni 17846511882cSPaolo Abeni lock_sock(sk); 17851954b860SMat Martineau 178608e39c0dSEric Dumazet if (unlikely(inet_test_bit(DEFER_CONNECT, sk) || 178708e39c0dSEric Dumazet msg->msg_flags & MSG_FASTOPEN)) { 1788d98a82a6SDmytro Shytyi int copied_syn = 0; 1789d98a82a6SDmytro Shytyi 1790a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1791d98a82a6SDmytro Shytyi copied += copied_syn; 1792fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1793d98a82a6SDmytro Shytyi goto out; 1794fa9e5746SPaolo Abeni else if (ret) 179569800e51SPaolo Abeni goto do_error; 1796d98a82a6SDmytro Shytyi } 1797d98a82a6SDmytro Shytyi 17981954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17991954b860SMat Martineau 18001954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 18011954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 18021954b860SMat Martineau if (ret) 180369800e51SPaolo Abeni goto do_error; 18041954b860SMat Martineau } 18051954b860SMat Martineau 180669800e51SPaolo Abeni ret = -EPIPE; 180769800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 180869800e51SPaolo Abeni goto do_error; 180969800e51SPaolo Abeni 181017091708SFlorian Westphal pfrag = sk_page_frag(sk); 181118b683bfSPaolo Abeni 1812d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1813724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1814d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1815d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1816d9ca1de8SPaolo Abeni size_t psize, offset; 1817d9ca1de8SPaolo Abeni 1818d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1819d9ca1de8SPaolo Abeni * page allocator 1820fb529e62SFlorian Westphal */ 1821d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1822d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1823d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1824d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1825d9ca1de8SPaolo Abeni goto wait_for_memory; 18266e628cd3SPaolo Abeni 1827d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1828d9ca1de8SPaolo Abeni goto wait_for_memory; 1829d9ca1de8SPaolo Abeni 1830d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1831d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1832fb529e62SFlorian Westphal } 1833fb529e62SFlorian Westphal 1834d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1835d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1836d9ca1de8SPaolo Abeni * anyway 1837d9ca1de8SPaolo Abeni */ 1838d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1839d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1840d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1841724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 18428ce568edSPaolo Abeni 18436511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1844d9ca1de8SPaolo Abeni goto wait_for_memory; 1845d9ca1de8SPaolo Abeni 1846d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1847d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1848d9ca1de8SPaolo Abeni ret = -EFAULT; 184969800e51SPaolo Abeni goto do_error; 1850d9ca1de8SPaolo Abeni } 1851d9ca1de8SPaolo Abeni 1852d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 18535e6300e7SEric Dumazet sk_forward_alloc_add(sk, -total_ts); 1854d9ca1de8SPaolo Abeni copied += psize; 1855d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1856d9ca1de8SPaolo Abeni frag_truesize += psize; 1857d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1858d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1859d9ca1de8SPaolo Abeni 1860d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1861d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1862d9ca1de8SPaolo Abeni */ 1863d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1864d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1865d9ca1de8SPaolo Abeni get_page(dfrag->page); 1866d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1867d9ca1de8SPaolo Abeni if (!msk->first_pending) 1868d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1869d9ca1de8SPaolo Abeni } 1870e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1871d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1872d9ca1de8SPaolo Abeni !dfrag_collapsed); 1873d9ca1de8SPaolo Abeni 1874d9ca1de8SPaolo Abeni continue; 1875d9ca1de8SPaolo Abeni 1876d9ca1de8SPaolo Abeni wait_for_memory: 18775cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1878c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1879f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1880f296234cSPeter Krystad if (ret) 188169800e51SPaolo Abeni goto do_error; 1882cec37a6eSPeter Krystad } 1883cec37a6eSPeter Krystad 188413e16037SPaolo Abeni if (copied) 1885c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1886da51aef5SPaolo Abeni 18871954b860SMat Martineau out: 1888cec37a6eSPeter Krystad release_sock(sk); 188969800e51SPaolo Abeni return copied; 189069800e51SPaolo Abeni 189169800e51SPaolo Abeni do_error: 189269800e51SPaolo Abeni if (copied) 189369800e51SPaolo Abeni goto out; 189469800e51SPaolo Abeni 189569800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 189669800e51SPaolo Abeni goto out; 1897f870fa0bSMat Martineau } 1898f870fa0bSMat Martineau 18996771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 19006771bfd9SFlorian Westphal struct msghdr *msg, 1901b7f653b2SFlorian Westphal size_t len, int flags, 1902b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1903b7f653b2SFlorian Westphal int *cmsg_flags) 19046771bfd9SFlorian Westphal { 1905ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 19066771bfd9SFlorian Westphal int copied = 0; 19076771bfd9SFlorian Westphal 1908ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 19096771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 19106771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 19116771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 19126771bfd9SFlorian Westphal int err; 19136771bfd9SFlorian Westphal 1914d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 19156771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 19166771bfd9SFlorian Westphal if (unlikely(err < 0)) { 19176771bfd9SFlorian Westphal if (!copied) 19186771bfd9SFlorian Westphal return err; 19196771bfd9SFlorian Westphal break; 19206771bfd9SFlorian Westphal } 1921d976092cSPaolo Abeni } 19226771bfd9SFlorian Westphal 1923b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1924b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1925b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1926b7f653b2SFlorian Westphal } 1927b7f653b2SFlorian Westphal 19286771bfd9SFlorian Westphal copied += count; 19296771bfd9SFlorian Westphal 19306771bfd9SFlorian Westphal if (count < data_len) { 19312c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 19326771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 19332c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 19342c9e7765SFlorian Westphal } 19356771bfd9SFlorian Westphal break; 19366771bfd9SFlorian Westphal } 19376771bfd9SFlorian Westphal 1938ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 193987952603SPaolo Abeni /* we will bulk release the skb memory later */ 194087952603SPaolo Abeni skb->destructor = NULL; 1941ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 194287952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 19436771bfd9SFlorian Westphal __kfree_skb(skb); 1944ca4fb892SYonglong Li } 19456771bfd9SFlorian Westphal 19466771bfd9SFlorian Westphal if (copied >= len) 19476771bfd9SFlorian Westphal break; 19486771bfd9SFlorian Westphal } 19496771bfd9SFlorian Westphal 19506771bfd9SFlorian Westphal return copied; 19516771bfd9SFlorian Westphal } 19526771bfd9SFlorian Westphal 1953a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1954a6b118feSFlorian Westphal * 1955a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1956a6b118feSFlorian Westphal */ 1957a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1958a6b118feSFlorian Westphal { 1959a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1960a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1961b8dc6d6cSPaolo Abeni u8 scaling_ratio = U8_MAX; 1962a6b118feSFlorian Westphal u32 time, advmss = 1; 1963a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1964a6b118feSFlorian Westphal 1965109cdeb8SGeliang Tang msk_owned_by_me(msk); 1966a6b118feSFlorian Westphal 1967a6b118feSFlorian Westphal if (copied <= 0) 1968a6b118feSFlorian Westphal return; 1969a6b118feSFlorian Westphal 1970a2d74374SPaolo Abeni if (!msk->rcvspace_init) 1971a2d74374SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 1972a2d74374SPaolo Abeni 1973a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1974a6b118feSFlorian Westphal 1975a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1976a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1977a6b118feSFlorian Westphal 1978a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1979a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1980a6b118feSFlorian Westphal return; 1981a6b118feSFlorian Westphal 1982a6b118feSFlorian Westphal rtt_us = 0; 1983a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1984a6b118feSFlorian Westphal const struct tcp_sock *tp; 1985a6b118feSFlorian Westphal u64 sf_rtt_us; 1986a6b118feSFlorian Westphal u32 sf_advmss; 1987a6b118feSFlorian Westphal 1988a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1989a6b118feSFlorian Westphal 1990a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1991a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1992a6b118feSFlorian Westphal 1993a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1994a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1995b8dc6d6cSPaolo Abeni scaling_ratio = min(tp->scaling_ratio, scaling_ratio); 1996a6b118feSFlorian Westphal } 1997a6b118feSFlorian Westphal 1998a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1999b8dc6d6cSPaolo Abeni msk->scaling_ratio = scaling_ratio; 2000a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 2001a6b118feSFlorian Westphal return; 2002a6b118feSFlorian Westphal 2003a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 2004a6b118feSFlorian Westphal goto new_measure; 2005a6b118feSFlorian Westphal 200678047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 2007a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 2008a6b118feSFlorian Westphal u64 rcvwin, grow; 2009b8dc6d6cSPaolo Abeni int rcvbuf; 2010a6b118feSFlorian Westphal 2011a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 2012a6b118feSFlorian Westphal 2013a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 2014a6b118feSFlorian Westphal 2015a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 2016a6b118feSFlorian Westphal rcvwin += (grow << 1); 2017a6b118feSFlorian Westphal 2018b8dc6d6cSPaolo Abeni rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin), 201902739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 2020a6b118feSFlorian Westphal 2021a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 2022a6b118feSFlorian Westphal u32 window_clamp; 2023a6b118feSFlorian Westphal 2024b8dc6d6cSPaolo Abeni window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf); 2025a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 2026a6b118feSFlorian Westphal 2027a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 2028a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 2029a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 2030a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 2031a6b118feSFlorian Westphal */ 2032a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 2033a6b118feSFlorian Westphal struct sock *ssk; 2034c76c6956SPaolo Abeni bool slow; 2035a6b118feSFlorian Westphal 2036a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 2037c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 2038a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 2039a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 2040c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 2041c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 2042a6b118feSFlorian Westphal } 2043a6b118feSFlorian Westphal } 2044a6b118feSFlorian Westphal } 2045a6b118feSFlorian Westphal 2046a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 2047a6b118feSFlorian Westphal new_measure: 2048a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2049a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 2050a6b118feSFlorian Westphal } 2051a6b118feSFlorian Westphal 205287952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 205387952603SPaolo Abeni { 205487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 205587952603SPaolo Abeni 205687952603SPaolo Abeni if (!msk->rmem_released) 205787952603SPaolo Abeni return; 205887952603SPaolo Abeni 205987952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 20606511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 2061ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 206287952603SPaolo Abeni } 206387952603SPaolo Abeni 206487952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 206587952603SPaolo Abeni { 206687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 206787952603SPaolo Abeni 206887952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 206987952603SPaolo Abeni } 207087952603SPaolo Abeni 2071e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20726771bfd9SFlorian Westphal { 207387952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20746771bfd9SFlorian Westphal unsigned int moved = 0; 207587952603SPaolo Abeni bool ret, done; 2076d5f49190SPaolo Abeni 20776771bfd9SFlorian Westphal do { 20786771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 207965f49fe7SFlorian Westphal bool slowpath; 20806771bfd9SFlorian Westphal 208187952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 208287952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 208387952603SPaolo Abeni * that is an unlikely slow path. 208487952603SPaolo Abeni */ 208587952603SPaolo Abeni if (likely(!ssk)) 20866771bfd9SFlorian Westphal break; 20876771bfd9SFlorian Westphal 208865f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 208987952603SPaolo Abeni mptcp_data_lock(sk); 2090e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20916771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 209287952603SPaolo Abeni mptcp_data_unlock(sk); 2093499ada50SPaolo Abeni 2094499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2095499ada50SPaolo Abeni __mptcp_error_report(sk); 209665f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20976771bfd9SFlorian Westphal } while (!done); 20986771bfd9SFlorian Westphal 209987952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 210087952603SPaolo Abeni ret = moved > 0; 210187952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 210287952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 210387952603SPaolo Abeni mptcp_data_lock(sk); 210487952603SPaolo Abeni __mptcp_update_rmem(sk); 210587952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 210687952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 210787952603SPaolo Abeni mptcp_data_unlock(sk); 2108ab174ad8SPaolo Abeni } 210987952603SPaolo Abeni if (ret) 211087952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 211187952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 21126771bfd9SFlorian Westphal } 21136771bfd9SFlorian Westphal 21142c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 21152c9e7765SFlorian Westphal { 21162c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 21172c9e7765SFlorian Westphal const struct sk_buff *skb; 21182c9e7765SFlorian Westphal 21192c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 21202c9e7765SFlorian Westphal if (skb) { 21212c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 21222c9e7765SFlorian Westphal 21232c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 21242c9e7765SFlorian Westphal return INT_MAX; 21252c9e7765SFlorian Westphal 21262c9e7765SFlorian Westphal return (unsigned int)hint_val; 21272c9e7765SFlorian Westphal } 21282c9e7765SFlorian Westphal 21292c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 21302c9e7765SFlorian Westphal return 1; 21312c9e7765SFlorian Westphal 21322c9e7765SFlorian Westphal return 0; 21332c9e7765SFlorian Westphal } 21342c9e7765SFlorian Westphal 2135f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2136ec095263SOliver Hartkopp int flags, int *addr_len) 2137f870fa0bSMat Martineau { 2138f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2139b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2140b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 21417a6a6cbcSPaolo Abeni int target; 21427a6a6cbcSPaolo Abeni long timeo; 2143f870fa0bSMat Martineau 2144cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2145cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2146cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2147cb9d80f4SPaolo Abeni 21486511882cSPaolo Abeni lock_sock(sk); 2149fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2150fd897679SPaolo Abeni copied = -ENOTCONN; 2151fd897679SPaolo Abeni goto out_err; 2152fd897679SPaolo Abeni } 2153fd897679SPaolo Abeni 2154ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 21557a6a6cbcSPaolo Abeni 21567a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 21577a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 21587a6a6cbcSPaolo Abeni 21592c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 21602c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 21612c9e7765SFlorian Westphal 216205e3eceaSEric Dumazet while (copied < len) { 2163e3859603SPaolo Abeni int bytes_read; 21647a6a6cbcSPaolo Abeni 2165b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21666771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21677a6a6cbcSPaolo Abeni if (!copied) 21687a6a6cbcSPaolo Abeni copied = bytes_read; 21696771bfd9SFlorian Westphal goto out_err; 2170cec37a6eSPeter Krystad } 2171cec37a6eSPeter Krystad 21727a6a6cbcSPaolo Abeni copied += bytes_read; 21737a6a6cbcSPaolo Abeni 2174ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2175fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2176ea4ca586SPaolo Abeni 2177e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2178e3859603SPaolo Abeni continue; 2179e3859603SPaolo Abeni 21807a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21817a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21827a6a6cbcSPaolo Abeni */ 21837a6a6cbcSPaolo Abeni if (copied >= target) 21847a6a6cbcSPaolo Abeni break; 21857a6a6cbcSPaolo Abeni 21867a6a6cbcSPaolo Abeni if (copied) { 21877a6a6cbcSPaolo Abeni if (sk->sk_err || 21887a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21897a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21907a6a6cbcSPaolo Abeni !timeo || 21917a6a6cbcSPaolo Abeni signal_pending(current)) 21927a6a6cbcSPaolo Abeni break; 21937a6a6cbcSPaolo Abeni } else { 21947a6a6cbcSPaolo Abeni if (sk->sk_err) { 21957a6a6cbcSPaolo Abeni copied = sock_error(sk); 21967a6a6cbcSPaolo Abeni break; 21977a6a6cbcSPaolo Abeni } 21987a6a6cbcSPaolo Abeni 219987952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 220087952603SPaolo Abeni /* race breaker: the shutdown could be after the 220187952603SPaolo Abeni * previous receive queue check 220287952603SPaolo Abeni */ 2203e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 220487952603SPaolo Abeni continue; 22057a6a6cbcSPaolo Abeni break; 220687952603SPaolo Abeni } 22077a6a6cbcSPaolo Abeni 22087a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 22097a6a6cbcSPaolo Abeni copied = -ENOTCONN; 22107a6a6cbcSPaolo Abeni break; 22117a6a6cbcSPaolo Abeni } 22127a6a6cbcSPaolo Abeni 22137a6a6cbcSPaolo Abeni if (!timeo) { 22147a6a6cbcSPaolo Abeni copied = -EAGAIN; 22157a6a6cbcSPaolo Abeni break; 22167a6a6cbcSPaolo Abeni } 22177a6a6cbcSPaolo Abeni 22187a6a6cbcSPaolo Abeni if (signal_pending(current)) { 22197a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 22207a6a6cbcSPaolo Abeni break; 22217a6a6cbcSPaolo Abeni } 22227a6a6cbcSPaolo Abeni } 22237a6a6cbcSPaolo Abeni 22247a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2225612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 22267a6a6cbcSPaolo Abeni } 22273c90e377SPaolo Abeni 22286771bfd9SFlorian Westphal out_err: 2229b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2230b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2231b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 22322c9e7765SFlorian Westphal 22332c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 22342c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 22352c9e7765SFlorian Westphal 22362c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 22372c9e7765SFlorian Westphal } 2238b7f653b2SFlorian Westphal } 2239b7f653b2SFlorian Westphal 2240612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2241612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2242612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2243ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2244a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2245a6b118feSFlorian Westphal 2246cec37a6eSPeter Krystad release_sock(sk); 2247cec37a6eSPeter Krystad return copied; 2248cec37a6eSPeter Krystad } 2249cec37a6eSPeter Krystad 2250b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2251b51f9b80SPaolo Abeni { 2252b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2253b51f9b80SPaolo Abeni icsk_retransmit_timer); 2254b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 22552d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2256b51f9b80SPaolo Abeni 2257b51f9b80SPaolo Abeni bh_lock_sock(sk); 2258b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22592d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22602d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22612d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2262b51f9b80SPaolo Abeni } else { 2263b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2264e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2265b51f9b80SPaolo Abeni } 2266b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2267b51f9b80SPaolo Abeni sock_put(sk); 2268b51f9b80SPaolo Abeni } 2269b51f9b80SPaolo Abeni 2270f6909dc1SPaolo Abeni static void mptcp_tout_timer(struct timer_list *t) 2271e16163b6SPaolo Abeni { 2272e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2273e16163b6SPaolo Abeni 2274e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2275b6d69fc8SFlorian Westphal sock_put(sk); 2276e16163b6SPaolo Abeni } 2277e16163b6SPaolo Abeni 22783b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22793b1d6210SPaolo Abeni * level. 22803b1d6210SPaolo Abeni * 22813b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22823b1d6210SPaolo Abeni */ 228307336a87SGeliang Tang struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22843b1d6210SPaolo Abeni { 228571b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22863b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 228771b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22883b1d6210SPaolo Abeni 22893b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22903b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22913b1d6210SPaolo Abeni 2292ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2293d5f49190SPaolo Abeni continue; 2294d5f49190SPaolo Abeni 229571b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 229671b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 229771b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 229871b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2299860975c6SFlorian Westphal continue; 2300860975c6SFlorian Westphal } 23013b1d6210SPaolo Abeni 23023b1d6210SPaolo Abeni if (subflow->backup) { 23033b1d6210SPaolo Abeni if (!backup) 23043b1d6210SPaolo Abeni backup = ssk; 23053b1d6210SPaolo Abeni continue; 23063b1d6210SPaolo Abeni } 23073b1d6210SPaolo Abeni 230871b7dec2SPaolo Abeni if (!pick) 230971b7dec2SPaolo Abeni pick = ssk; 23103b1d6210SPaolo Abeni } 23113b1d6210SPaolo Abeni 231271b7dec2SPaolo Abeni if (pick) 231371b7dec2SPaolo Abeni return pick; 231471b7dec2SPaolo Abeni 231571b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 231671b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 23173b1d6210SPaolo Abeni } 23183b1d6210SPaolo Abeni 23191e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 23201e1d9d6fSPaolo Abeni { 23211e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 23221e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 23231e1d9d6fSPaolo Abeni 232400df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 23251e1d9d6fSPaolo Abeni return false; 23261e1d9d6fSPaolo Abeni 23271e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 23281e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 23291e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 23301e1d9d6fSPaolo Abeni */ 23311e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 23321e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 23331e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 23341e1d9d6fSPaolo Abeni if (!rtx_head) { 23351e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23361e1d9d6fSPaolo Abeni return false; 23371e1d9d6fSPaolo Abeni } 23381e1d9d6fSPaolo Abeni 23391e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 23401e1d9d6fSPaolo Abeni msk->recovery = true; 23411e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23421e1d9d6fSPaolo Abeni 23431e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23441e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23451e1d9d6fSPaolo Abeni 23461e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23471e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23481e1d9d6fSPaolo Abeni if (!cur->already_sent) 23491e1d9d6fSPaolo Abeni break; 23501e1d9d6fSPaolo Abeni cur->already_sent = 0; 23511e1d9d6fSPaolo Abeni } 23521e1d9d6fSPaolo Abeni 23531e1d9d6fSPaolo Abeni return true; 23541e1d9d6fSPaolo Abeni } 23551e1d9d6fSPaolo Abeni 2356b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2357b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2358b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2359b29fcfb5SPaolo Abeni 236014c56686SGeliang Tang /* be sure to send a reset only if the caller asked for it, also 236114c56686SGeliang Tang * clean completely the subflow status when the subflow reaches 236214c56686SGeliang Tang * TCP_CLOSE state 236314c56686SGeliang Tang */ 236414c56686SGeliang Tang static void __mptcp_subflow_disconnect(struct sock *ssk, 236514c56686SGeliang Tang struct mptcp_subflow_context *subflow, 236614c56686SGeliang Tang unsigned int flags) 236714c56686SGeliang Tang { 236814c56686SGeliang Tang if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) || 236914c56686SGeliang Tang (flags & MPTCP_CF_FASTCLOSE)) { 237014c56686SGeliang Tang /* The MPTCP code never wait on the subflow sockets, TCP-level 237114c56686SGeliang Tang * disconnect should never fail 237214c56686SGeliang Tang */ 237314c56686SGeliang Tang WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 237414c56686SGeliang Tang mptcp_subflow_ctx_reset(subflow); 237514c56686SGeliang Tang } else { 237614c56686SGeliang Tang tcp_shutdown(ssk, SEND_SHUTDOWN); 237714c56686SGeliang Tang } 237814c56686SGeliang Tang } 237914c56686SGeliang Tang 2380cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2381cec37a6eSPeter Krystad * (accept). 2382cec37a6eSPeter Krystad * 2383cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2384cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2385cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2386cec37a6eSPeter Krystad * parent socket. 2387cec37a6eSPeter Krystad */ 2388a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2389b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2390b29fcfb5SPaolo Abeni unsigned int flags) 2391cec37a6eSPeter Krystad { 2392e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 239363740448SPaolo Abeni bool dispose_it, need_push = false; 239463740448SPaolo Abeni 239563740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 239627e5ccc2SPaolo Abeni * to an incoming reset or listener shutdown, the subflow socket is 239727e5ccc2SPaolo Abeni * already deleted by inet_child_forget() and the mptcp socket can't 239827e5ccc2SPaolo Abeni * survive too. 239963740448SPaolo Abeni */ 240027e5ccc2SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk && 240127e5ccc2SPaolo Abeni (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) { 240263740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 240327e5ccc2SPaolo Abeni mptcp_set_close_tout(sk, tcp_jiffies32 - (TCP_TIMEWAIT_LEN + 1)); 240463740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 240563740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 240663740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 240763740448SPaolo Abeni goto out_release; 240863740448SPaolo Abeni } 2409e0be4931SFlorian Westphal 241039880bd8SPaolo Abeni dispose_it = msk->free_first || ssk != msk->first; 2411b29fcfb5SPaolo Abeni if (dispose_it) 2412cec37a6eSPeter Krystad list_del(&subflow->node); 2413cec37a6eSPeter Krystad 24143f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2415e16163b6SPaolo Abeni 2416ff18f9efSPaolo Abeni if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) { 241714c56686SGeliang Tang /* be sure to force the tcp_close path 2418d21f8348SPaolo Abeni * to generate the egress reset 2419d21f8348SPaolo Abeni */ 2420d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2421d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2422b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2423d21f8348SPaolo Abeni } 2424b29fcfb5SPaolo Abeni 2425b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2426b29fcfb5SPaolo Abeni if (!dispose_it) { 242714c56686SGeliang Tang __mptcp_subflow_disconnect(ssk, subflow, flags); 2428b29fcfb5SPaolo Abeni release_sock(ssk); 2429b29fcfb5SPaolo Abeni 2430b29fcfb5SPaolo Abeni goto out; 2431b29fcfb5SPaolo Abeni } 2432b29fcfb5SPaolo Abeni 2433d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2434d7b1bfd0SPaolo Abeni 2435e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2436e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2437e16163b6SPaolo Abeni * reference owned by msk; 2438e16163b6SPaolo Abeni */ 2439e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2440b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2441e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2442e16163b6SPaolo Abeni } else { 2443d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2444e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2445e16163b6SPaolo Abeni 2446e16163b6SPaolo Abeni /* close acquired an extra ref */ 2447e16163b6SPaolo Abeni __sock_put(ssk); 2448e16163b6SPaolo Abeni } 244963740448SPaolo Abeni 245063740448SPaolo Abeni out_release: 24519f1a9881SPaolo Abeni __mptcp_subflow_error_report(sk, ssk); 2452e16163b6SPaolo Abeni release_sock(ssk); 2453e16163b6SPaolo Abeni 2454e16163b6SPaolo Abeni sock_put(ssk); 2455e0be4931SFlorian Westphal 2456c8fe62f0SFlorian Westphal if (ssk == msk->first) 24571b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2458c8fe62f0SFlorian Westphal 2459b29fcfb5SPaolo Abeni out: 246034c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 24611e1d9d6fSPaolo Abeni if (need_push) 24621e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 246327e5ccc2SPaolo Abeni 246427e5ccc2SPaolo Abeni /* Catch every 'all subflows closed' scenario, including peers silently 246527e5ccc2SPaolo Abeni * closing them, e.g. due to timeout. 246627e5ccc2SPaolo Abeni * For established sockets, allow an additional timeout before closing, 246727e5ccc2SPaolo Abeni * as the protocol can still create more subflows. 246827e5ccc2SPaolo Abeni */ 246927e5ccc2SPaolo Abeni if (list_is_singular(&msk->conn_list) && msk->first && 247027e5ccc2SPaolo Abeni inet_sk_state_load(msk->first) == TCP_CLOSE) { 247127e5ccc2SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED || 247227e5ccc2SPaolo Abeni msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) { 2473d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 247427e5ccc2SPaolo Abeni mptcp_close_wake_up(sk); 247527e5ccc2SPaolo Abeni } else { 247627e5ccc2SPaolo Abeni mptcp_start_tout_timer(sk); 247727e5ccc2SPaolo Abeni } 247827e5ccc2SPaolo Abeni } 2479f870fa0bSMat Martineau } 2480f870fa0bSMat Martineau 2481a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2482a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2483a141e02eSFlorian Westphal { 2484b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2485b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2486a88c9e49SPaolo Abeni 2487a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2488a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2489a88c9e49SPaolo Abeni */ 2490a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2491a88c9e49SPaolo Abeni 2492b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2493a141e02eSFlorian Westphal } 2494a141e02eSFlorian Westphal 2495dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2496dc24f8b4SPaolo Abeni { 2497dc24f8b4SPaolo Abeni return 0; 2498dc24f8b4SPaolo Abeni } 2499dc24f8b4SPaolo Abeni 2500b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 25010e4f35d7SPaolo Abeni { 25020e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2503b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 25040e4f35d7SPaolo Abeni 25053abc05d9SFlorian Westphal might_sleep(); 25063abc05d9SFlorian Westphal 25075efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 25080e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 25090e4f35d7SPaolo Abeni 25100e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 25110e4f35d7SPaolo Abeni continue; 25120e4f35d7SPaolo Abeni 251340947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 251440947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 251540947e13SFlorian Westphal continue; 251640947e13SFlorian Westphal 2517b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2518b6985b9bSPaolo Abeni } 2519b6985b9bSPaolo Abeni 25200e4f35d7SPaolo Abeni } 25210e4f35d7SPaolo Abeni 252227e5ccc2SPaolo Abeni static bool mptcp_close_tout_expired(const struct sock *sk) 2523e16163b6SPaolo Abeni { 252427e5ccc2SPaolo Abeni if (!inet_csk(sk)->icsk_mtup.probe_timestamp || 252527e5ccc2SPaolo Abeni sk->sk_state == TCP_CLOSE) 2526e16163b6SPaolo Abeni return false; 252727e5ccc2SPaolo Abeni 252827e5ccc2SPaolo Abeni return time_after32(tcp_jiffies32, 252927e5ccc2SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp + TCP_TIMEWAIT_LEN); 2530e16163b6SPaolo Abeni } 2531e16163b6SPaolo Abeni 253250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 253350c504a2SFlorian Westphal { 253450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 253580638684SGeliang Tang struct sock *sk = (struct sock *)msk; 253650c504a2SFlorian Westphal 253750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 253850c504a2SFlorian Westphal return; 253950c504a2SFlorian Westphal 254050c504a2SFlorian Westphal mptcp_token_destroy(msk); 254150c504a2SFlorian Westphal 25425efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 254350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 254475e908c3SPaolo Abeni bool slow; 254550c504a2SFlorian Westphal 254675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 254750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 254850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 254950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 255050c504a2SFlorian Westphal } 255175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 255250c504a2SFlorian Westphal } 255350c504a2SFlorian Westphal 255469800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 255569800e51SPaolo Abeni switch (sk->sk_state) { 255669800e51SPaolo Abeni case TCP_SYN_SENT: 25579ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 255869800e51SPaolo Abeni break; 255969800e51SPaolo Abeni case TCP_CLOSE_WAIT: 25609ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 256169800e51SPaolo Abeni break; 256269800e51SPaolo Abeni case TCP_CLOSE: 256369800e51SPaolo Abeni return; 256469800e51SPaolo Abeni default: 25659ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 256669800e51SPaolo Abeni } 256769800e51SPaolo Abeni 2568d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 25696b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 257050c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 257150c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 257250c504a2SFlorian Westphal 257369800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 257469800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 257569800e51SPaolo Abeni return; 257669800e51SPaolo Abeni 257769800e51SPaolo Abeni sk->sk_state_change(sk); 257869800e51SPaolo Abeni sk_error_report(sk); 257950c504a2SFlorian Westphal } 258050c504a2SFlorian Westphal 25812948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 258280992017SPaolo Abeni { 25832948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2584ee2708aeSGeliang Tang struct mptcp_subflow_context *subflow; 2585caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25863b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25872948d0a1SPaolo Abeni struct sock *ssk; 2588ee2708aeSGeliang Tang int ret, err; 2589ee2708aeSGeliang Tang u16 len = 0; 25902948d0a1SPaolo Abeni 2591b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25923241a9c0SFlorian Westphal 25933241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 2594ee2708aeSGeliang Tang err = mptcp_sched_get_retrans(msk); 25952948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25966477dd39SMat Martineau if (!dfrag) { 25976477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25986477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25996477dd39SMat Martineau 26006477dd39SMat Martineau icsk->icsk_retransmits++; 26016477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 26026477dd39SMat Martineau mptcp_send_ack(msk); 26036477dd39SMat Martineau 26046477dd39SMat Martineau goto reset_timer; 26056477dd39SMat Martineau } 26066477dd39SMat Martineau 26073241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 26082948d0a1SPaolo Abeni return; 26093241a9c0SFlorian Westphal 26103241a9c0SFlorian Westphal goto reset_timer; 26116477dd39SMat Martineau } 26122948d0a1SPaolo Abeni 2613ee2708aeSGeliang Tang if (err) 26142948d0a1SPaolo Abeni goto reset_timer; 26152948d0a1SPaolo Abeni 2616ee2708aeSGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2617ee2708aeSGeliang Tang if (READ_ONCE(subflow->scheduled)) { 2618ee2708aeSGeliang Tang u16 copied = 0; 2619ee2708aeSGeliang Tang 2620ee2708aeSGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 2621ee2708aeSGeliang Tang 2622ee2708aeSGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 2623ee2708aeSGeliang Tang 26242948d0a1SPaolo Abeni lock_sock(ssk); 26252948d0a1SPaolo Abeni 26262948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 26272948d0a1SPaolo Abeni info.sent = 0; 2628ee2708aeSGeliang Tang info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : 2629ee2708aeSGeliang Tang dfrag->already_sent; 26304e14867dSPaolo Abeni while (info.sent < info.limit) { 26312948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 26322948d0a1SPaolo Abeni if (ret <= 0) 26332948d0a1SPaolo Abeni break; 26342948d0a1SPaolo Abeni 26352948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 26362948d0a1SPaolo Abeni copied += ret; 26372948d0a1SPaolo Abeni info.sent += ret; 26382948d0a1SPaolo Abeni } 26394e14867dSPaolo Abeni if (copied) { 2640ee2708aeSGeliang Tang len = max(copied, len); 26412948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 26422948d0a1SPaolo Abeni info.size_goal); 26430530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 26444e14867dSPaolo Abeni } 26452948d0a1SPaolo Abeni 26462948d0a1SPaolo Abeni release_sock(ssk); 2647ee2708aeSGeliang Tang } 2648ee2708aeSGeliang Tang } 2649ee2708aeSGeliang Tang 2650ee2708aeSGeliang Tang msk->bytes_retrans += len; 2651ee2708aeSGeliang Tang dfrag->already_sent = max(dfrag->already_sent, len); 26522948d0a1SPaolo Abeni 26532948d0a1SPaolo Abeni reset_timer: 26543241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 26553241a9c0SFlorian Westphal 2656f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 2657f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 26582948d0a1SPaolo Abeni } 26592948d0a1SPaolo Abeni 266076a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 266176a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 266276a13b31SGeliang Tang */ 2663f6909dc1SPaolo Abeni void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout) 266476a13b31SGeliang Tang { 266576a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 266676a13b31SGeliang Tang unsigned long timeout, close_timeout; 266776a13b31SGeliang Tang 266827e5ccc2SPaolo Abeni if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp) 266976a13b31SGeliang Tang return; 267076a13b31SGeliang Tang 267127e5ccc2SPaolo Abeni close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + 267227e5ccc2SPaolo Abeni TCP_TIMEWAIT_LEN; 267376a13b31SGeliang Tang 267476a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 267576a13b31SGeliang Tang * them is active 267676a13b31SGeliang Tang */ 267727e5ccc2SPaolo Abeni timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout; 267876a13b31SGeliang Tang 267976a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 268076a13b31SGeliang Tang } 268176a13b31SGeliang Tang 268249fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 268349fa1919SGeliang Tang { 268476a13b31SGeliang Tang struct sock *ssk = msk->first; 268549fa1919SGeliang Tang bool slow; 268649fa1919SGeliang Tang 268776a13b31SGeliang Tang if (!ssk) 268876a13b31SGeliang Tang return; 268976a13b31SGeliang Tang 269049fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 269149fa1919SGeliang Tang 269249fa1919SGeliang Tang slow = lock_sock_fast(ssk); 269349fa1919SGeliang Tang mptcp_subflow_reset(ssk); 269476a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 269549fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 269649fa1919SGeliang Tang } 269749fa1919SGeliang Tang 2698d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2699d21f8348SPaolo Abeni { 2700d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2701d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2702d21f8348SPaolo Abeni 2703d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 2704d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2705d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2706d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2707d21f8348SPaolo Abeni } 2708d21f8348SPaolo Abeni 27092948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 27102948d0a1SPaolo Abeni { 27112948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 271280638684SGeliang Tang struct sock *sk = (struct sock *)msk; 271376a13b31SGeliang Tang unsigned long fail_tout; 27142948d0a1SPaolo Abeni int state; 271580992017SPaolo Abeni 271680992017SPaolo Abeni lock_sock(sk); 2717e16163b6SPaolo Abeni state = sk->sk_state; 2718d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2719e16163b6SPaolo Abeni goto unlock; 2720e16163b6SPaolo Abeni 272150c504a2SFlorian Westphal mptcp_check_fastclose(msk); 272250c504a2SFlorian Westphal 2723e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2724b416268bSFlorian Westphal 272581c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 272681c1d029SPaolo Abeni mptcp_check_data_fin_ack(sk); 272743b54c6eSMat Martineau mptcp_check_data_fin(sk); 272843b54c6eSMat Martineau 2729b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2730b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2731b6985b9bSPaolo Abeni 273227e5ccc2SPaolo Abeni if (mptcp_close_tout_expired(sk)) { 2733d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 273427e5ccc2SPaolo Abeni mptcp_close_wake_up(sk); 273527e5ccc2SPaolo Abeni } 2736bbd49d11SPaolo Abeni 273727e5ccc2SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) { 2738e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2739e16163b6SPaolo Abeni goto unlock; 2740e16163b6SPaolo Abeni } 2741e16163b6SPaolo Abeni 27422948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 27432948d0a1SPaolo Abeni __mptcp_retrans(sk); 27443b1d6210SPaolo Abeni 274576a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 274676a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 274749fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 274849fa1919SGeliang Tang 27493b1d6210SPaolo Abeni unlock: 275080992017SPaolo Abeni release_sock(sk); 275180992017SPaolo Abeni sock_put(sk); 275280992017SPaolo Abeni } 275380992017SPaolo Abeni 2754e2636917SKuniyuki Iwashima static void __mptcp_init_sock(struct sock *sk) 2755f870fa0bSMat Martineau { 2756cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2757cec37a6eSPeter Krystad 2758cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2759ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 276018b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 276180992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 276287952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2763ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2764f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 27656511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2766ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 276733d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2768cec37a6eSPeter Krystad 27691b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2770dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2771752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 27720530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 27731e1d9d6fSPaolo Abeni msk->recovery = false; 27746f06b4d4SPaolo Abeni msk->subflow_id = 1; 27758ab183deSPaolo Abeni 27761b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27771b1c7a0eSPeter Krystad 2778b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2779b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2780f6909dc1SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_tout_timer, 0); 2781f870fa0bSMat Martineau } 2782f870fa0bSMat Martineau 2783b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2784784325e9SMatthieu Baerts { 278520b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2786b29fcfb5SPaolo Abeni 2787b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2788b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2789b29fcfb5SPaolo Abeni 2790b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2791b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2792b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2793b29fcfb5SPaolo Abeni } 2794b29fcfb5SPaolo Abeni 2795b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2796b29fcfb5SPaolo Abeni { 2797fc518953SFlorian Westphal struct net *net = sock_net(sk); 27981730b2b2SGeliang Tang int ret; 279918b683bfSPaolo Abeni 2800e2636917SKuniyuki Iwashima __mptcp_init_sock(sk); 2801b6c08380SGeliang Tang 2802fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2803fc518953SFlorian Westphal return -ENOPROTOOPT; 2804fc518953SFlorian Westphal 2805fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2806fc518953SFlorian Westphal return -ENOMEM; 2807fc518953SFlorian Westphal 28081730b2b2SGeliang Tang ret = mptcp_init_sched(mptcp_sk(sk), 28091730b2b2SGeliang Tang mptcp_sched_find(mptcp_get_scheduler(net))); 28101730b2b2SGeliang Tang if (ret) 28111730b2b2SGeliang Tang return ret; 28121730b2b2SGeliang Tang 2813a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2814a5ef058dSPaolo Abeni 281520b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 281620b5759fSPaolo Abeni * propagate the correct value 281720b5759fSPaolo Abeni */ 2818b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 281920b5759fSPaolo Abeni 2820d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2821a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2822a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2823d027236cSPaolo Abeni 282418b683bfSPaolo Abeni return 0; 282518b683bfSPaolo Abeni } 282618b683bfSPaolo Abeni 282718b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 282818b683bfSPaolo Abeni { 282918b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 283018b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 283118b683bfSPaolo Abeni 2832d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 283318b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2834d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2835784325e9SMatthieu Baerts } 2836784325e9SMatthieu Baerts 283730e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 283880992017SPaolo Abeni { 283980992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 284080992017SPaolo Abeni 2841b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2842e16163b6SPaolo Abeni __sock_put(sk); 284380992017SPaolo Abeni } 284480992017SPaolo Abeni 2845d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 284621498490SPeter Krystad { 284721498490SPeter Krystad lock_sock(ssk); 284821498490SPeter Krystad 284921498490SPeter Krystad switch (ssk->sk_state) { 285021498490SPeter Krystad case TCP_LISTEN: 285121498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 285221498490SPeter Krystad break; 2853df561f66SGustavo A. R. Silva fallthrough; 285421498490SPeter Krystad case TCP_SYN_SENT: 2855c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 285621498490SPeter Krystad break; 285721498490SPeter Krystad default: 285843b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 285943b54c6eSMat Martineau pr_debug("Fallback"); 286021498490SPeter Krystad ssk->sk_shutdown |= how; 286121498490SPeter Krystad tcp_shutdown(ssk, how); 286281c1d029SPaolo Abeni 286381c1d029SPaolo Abeni /* simulate the data_fin ack reception to let the state 286481c1d029SPaolo Abeni * machine move forward 286581c1d029SPaolo Abeni */ 286681c1d029SPaolo Abeni WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 286781c1d029SPaolo Abeni mptcp_schedule_work(sk); 286843b54c6eSMat Martineau } else { 286943b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 287043b54c6eSMat Martineau tcp_send_ack(ssk); 2871f6909dc1SPaolo Abeni if (!mptcp_rtx_timer_pending(sk)) 2872f6909dc1SPaolo Abeni mptcp_reset_rtx_timer(sk); 287343b54c6eSMat Martineau } 287421498490SPeter Krystad break; 287521498490SPeter Krystad } 287621498490SPeter Krystad 287721498490SPeter Krystad release_sock(ssk); 287821498490SPeter Krystad } 287921498490SPeter Krystad 288056557547SGeliang Tang void mptcp_set_state(struct sock *sk, int state) 288156557547SGeliang Tang { 288256557547SGeliang Tang int oldstate = sk->sk_state; 288356557547SGeliang Tang 288456557547SGeliang Tang switch (state) { 288556557547SGeliang Tang case TCP_ESTABLISHED: 288656557547SGeliang Tang if (oldstate != TCP_ESTABLISHED) 288756557547SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB); 288856557547SGeliang Tang break; 288956557547SGeliang Tang 289056557547SGeliang Tang default: 289156557547SGeliang Tang if (oldstate == TCP_ESTABLISHED) 289256557547SGeliang Tang MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB); 289356557547SGeliang Tang } 289456557547SGeliang Tang 289556557547SGeliang Tang inet_sk_state_store(sk, state); 289656557547SGeliang Tang } 289756557547SGeliang Tang 28986920b851SMat Martineau static const unsigned char new_state[16] = { 28996920b851SMat Martineau /* current state: new state: action: */ 29006920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 29016920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 29026920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 29036920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 29046920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 29056920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 29066920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 29076920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 29086920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 29096920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 29106920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 29116920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 29126920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 29136920b851SMat Martineau }; 29146920b851SMat Martineau 29156920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 29166920b851SMat Martineau { 29176920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 29186920b851SMat Martineau int ns = next & TCP_STATE_MASK; 29196920b851SMat Martineau 2920d52b3c2bSGeliang Tang mptcp_set_state(sk, ns); 29216920b851SMat Martineau 29226920b851SMat Martineau return next & TCP_ACTION_FIN; 29236920b851SMat Martineau } 29246920b851SMat Martineau 292581c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk) 2926e16163b6SPaolo Abeni { 2927e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2928e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2929e16163b6SPaolo Abeni 2930e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2931e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2932e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2933e16163b6SPaolo Abeni 2934e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2935e16163b6SPaolo Abeni * skip this 2936e16163b6SPaolo Abeni */ 2937e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2938e16163b6SPaolo Abeni mptcp_send_head(sk)) 2939e16163b6SPaolo Abeni return; 2940e16163b6SPaolo Abeni 2941e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2942e16163b6SPaolo Abeni 2943e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2944e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2945e16163b6SPaolo Abeni 2946e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2947e16163b6SPaolo Abeni } 2948e16163b6SPaolo Abeni } 2949e16163b6SPaolo Abeni 2950e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2951e16163b6SPaolo Abeni { 2952e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2953e16163b6SPaolo Abeni 2954e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2955e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2956e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2957e16163b6SPaolo Abeni 2958e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2959e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2960e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2961e16163b6SPaolo Abeni 296281c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 2963e16163b6SPaolo Abeni } 2964e16163b6SPaolo Abeni 2965e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2966f870fa0bSMat Martineau { 2967f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2968f870fa0bSMat Martineau 2969e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 29702c22c06cSFlorian Westphal 29713abc05d9SFlorian Westphal might_sleep(); 29723abc05d9SFlorian Westphal 2973f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 2974e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2975e16163b6SPaolo Abeni msk->pm.status = 0; 29761730b2b2SGeliang Tang mptcp_release_sched(msk); 2977b2c5b614SFlorian Westphal 2978e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2979e16163b6SPaolo Abeni 29806511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 298187952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2982e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2983e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2984aa1fbd94SFlorian Westphal 2985e16163b6SPaolo Abeni sock_put(sk); 2986e16163b6SPaolo Abeni } 2987e16163b6SPaolo Abeni 298863740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 298963740448SPaolo Abeni { 299063740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 299163740448SPaolo Abeni mptcp_do_fastclose(sk); 299263740448SPaolo Abeni __mptcp_destroy_sock(sk); 299363740448SPaolo Abeni } 299463740448SPaolo Abeni 2995d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2996d21f8348SPaolo Abeni { 2997d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2998d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2999d21f8348SPaolo Abeni */ 3000d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 3001d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 3002d21f8348SPaolo Abeni return 0; 3003d21f8348SPaolo Abeni 3004d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 3005d21f8348SPaolo Abeni } 3006d21f8348SPaolo Abeni 300757fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk) 3008c558246eSMenglong Dong { 300957fc0f1cSPaolo Abeni struct sock *ssk; 301057fc0f1cSPaolo Abeni 301157fc0f1cSPaolo Abeni if (inet_sk_state_load(sk) != TCP_LISTEN) 301257fc0f1cSPaolo Abeni return; 301357fc0f1cSPaolo Abeni 3014c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 301557fc0f1cSPaolo Abeni ssk = mptcp_sk(sk)->first; 301657fc0f1cSPaolo Abeni if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 301757fc0f1cSPaolo Abeni return; 301857fc0f1cSPaolo Abeni 301957fc0f1cSPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 30203fffa15bSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 302157fc0f1cSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 302257fc0f1cSPaolo Abeni inet_csk_listen_stop(ssk); 302357fc0f1cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 302457fc0f1cSPaolo Abeni release_sock(ssk); 3025c558246eSMenglong Dong } 3026c558246eSMenglong Dong 302726d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 3028e16163b6SPaolo Abeni { 3029e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 303076a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 3031e16163b6SPaolo Abeni bool do_cancel_work = false; 3032d4e85922SPaolo Abeni int subflows_alive = 0; 3033e16163b6SPaolo Abeni 30346b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 3035e16163b6SPaolo Abeni 3036e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 303757fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3038d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3039e16163b6SPaolo Abeni goto cleanup; 3040e16163b6SPaolo Abeni } 3041e16163b6SPaolo Abeni 30428d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 30438d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 30448d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 30458d547809SPaolo Abeni */ 3046d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 30478d547809SPaolo Abeni timeout = 0; 3048d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 3049e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 3050d21f8348SPaolo Abeni } 3051e16163b6SPaolo Abeni 3052e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 3053e16163b6SPaolo Abeni 3054e16163b6SPaolo Abeni cleanup: 3055e16163b6SPaolo Abeni /* orphan all the subflows */ 305676a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3057e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 305849054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 3059e16163b6SPaolo Abeni 3060d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 3061d4e85922SPaolo Abeni 306276a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 306376a13b31SGeliang Tang * cancel the latter 306476a13b31SGeliang Tang */ 306576a13b31SGeliang Tang if (ssk == msk->first) 306676a13b31SGeliang Tang subflow->fail_tout = 0; 306776a13b31SGeliang Tang 3068fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 3069fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 3070fe948001SMenglong Dong */ 3071fe948001SMenglong Dong ssk->sk_socket = NULL; 3072fe948001SMenglong Dong ssk->sk_wq = NULL; 3073e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 3074e16163b6SPaolo Abeni } 3075e16163b6SPaolo Abeni sock_orphan(sk); 3076e16163b6SPaolo Abeni 3077d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 3078d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 3079d4e85922SPaolo Abeni */ 3080d4e85922SPaolo Abeni if (subflows_alive == 0) 3081d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3082d4e85922SPaolo Abeni 3083e16163b6SPaolo Abeni sock_hold(sk); 3084e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 308500df24f1SGeliang Tang if (msk->token) 308676a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3087b29fcfb5SPaolo Abeni 3088e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 3089e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 3090e16163b6SPaolo Abeni do_cancel_work = true; 3091e16163b6SPaolo Abeni } else { 309227e5ccc2SPaolo Abeni mptcp_start_tout_timer(sk); 3093e16163b6SPaolo Abeni } 309426d3e21cSMenglong Dong 309526d3e21cSMenglong Dong return do_cancel_work; 309626d3e21cSMenglong Dong } 309726d3e21cSMenglong Dong 309826d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 309926d3e21cSMenglong Dong { 310026d3e21cSMenglong Dong bool do_cancel_work; 310126d3e21cSMenglong Dong 310226d3e21cSMenglong Dong lock_sock(sk); 310326d3e21cSMenglong Dong 310426d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 3105e16163b6SPaolo Abeni release_sock(sk); 3106e16163b6SPaolo Abeni if (do_cancel_work) 310780992017SPaolo Abeni mptcp_cancel_work(sk); 3108b911c97cSFlorian Westphal 3109e16163b6SPaolo Abeni sock_put(sk); 3110f870fa0bSMat Martineau } 3111f870fa0bSMat Martineau 31127e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3113cf7da0d6SPeter Krystad { 3114cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3115cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3116cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3117cf7da0d6SPeter Krystad 3118cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3119cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3120cf7da0d6SPeter Krystad 3121cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3122cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3123cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3124cf7da0d6SPeter Krystad } 3125cf7da0d6SPeter Krystad #endif 3126cf7da0d6SPeter Krystad 3127cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3128cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3129cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3130cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3131cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3132cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3133cf7da0d6SPeter Krystad } 3134cf7da0d6SPeter Krystad 313518b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 313618b683bfSPaolo Abeni { 313776e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 313876e2a55dSPaolo Abeni 31397d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 31407d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 3141c2b2ae39SPaolo Abeni * msk->firstsocket lock). 31427d803344SPaolo Abeni */ 31437d803344SPaolo Abeni if (msk->fastopening) 3144c2b2ae39SPaolo Abeni return -EBUSY; 31457d803344SPaolo Abeni 314657fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3147d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 3148b29fcfb5SPaolo Abeni 3149f6909dc1SPaolo Abeni mptcp_stop_rtx_timer(sk); 315027e5ccc2SPaolo Abeni mptcp_stop_tout_timer(sk); 3151b29fcfb5SPaolo Abeni 315200df24f1SGeliang Tang if (msk->token) 315300df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3154b29fcfb5SPaolo Abeni 3155c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3156c0bf3c6aSPaolo Abeni * subflow 3157c0bf3c6aSPaolo Abeni */ 3158c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3159e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3160e9d09bacSPaolo Abeni msk->cb_flags = 0; 3161b29fcfb5SPaolo Abeni msk->recovery = false; 3162b29fcfb5SPaolo Abeni msk->can_ack = false; 3163b29fcfb5SPaolo Abeni msk->fully_established = false; 3164b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3165b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3166b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3167b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3168b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3169b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3170b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 317138967f42SPaolo Abeni msk->bytes_acked = 0; 317238967f42SPaolo Abeni msk->bytes_received = 0; 317338967f42SPaolo Abeni msk->bytes_sent = 0; 317438967f42SPaolo Abeni msk->bytes_retrans = 0; 3175a2d74374SPaolo Abeni msk->rcvspace_init = 0; 3176b29fcfb5SPaolo Abeni 31776b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3178b29fcfb5SPaolo Abeni sk_error_report(sk); 317942c556feSFlorian Westphal return 0; 318018b683bfSPaolo Abeni } 318118b683bfSPaolo Abeni 3182b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3183b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3184b0519de8SFlorian Westphal { 3185b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3186b0519de8SFlorian Westphal 3187b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3188b0519de8SFlorian Westphal } 3189b0519de8SFlorian Westphal #endif 3190b0519de8SFlorian Westphal 31917e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3192cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 31937e8b88ecSPaolo Abeni struct sock *ssk, 3194fca5c82cSPaolo Abeni struct request_sock *req) 3195b0519de8SFlorian Westphal { 319658b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3197b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 3198*b45df837SPaolo Abeni struct mptcp_subflow_context *subflow; 319958b09919SPaolo Abeni struct mptcp_sock *msk; 3200b0519de8SFlorian Westphal 3201b0519de8SFlorian Westphal if (!nsk) 3202b0519de8SFlorian Westphal return NULL; 3203b0519de8SFlorian Westphal 3204b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3205b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3206b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3207b0519de8SFlorian Westphal #endif 3208b0519de8SFlorian Westphal 320958b09919SPaolo Abeni __mptcp_init_sock(nsk); 321058b09919SPaolo Abeni 321158b09919SPaolo Abeni msk = mptcp_sk(nsk); 321258b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 321358b09919SPaolo Abeni msk->token = subflow_req->token; 3214b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3215b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 321674c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 321706251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 321858b09919SPaolo Abeni 321958b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3220eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 32217439d687SPaolo Abeni msk->snd_una = msk->write_seq; 32227439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3223df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 32241730b2b2SGeliang Tang mptcp_init_sched(msk, mptcp_sk(sk)->sched); 32256f8a612aSFlorian Westphal 32266f06b4d4SPaolo Abeni /* passive msk is created after the first/MPC subflow */ 32276f06b4d4SPaolo Abeni msk->subflow_id = 2; 32286f06b4d4SPaolo Abeni 32295e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 32300c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 32317e8b88ecSPaolo Abeni 32327e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 32337e8b88ecSPaolo Abeni * not yet exposted to user-space 32347e8b88ecSPaolo Abeni */ 3235d52b3c2bSGeliang Tang mptcp_set_state(nsk, TCP_ESTABLISHED); 32367e8b88ecSPaolo Abeni 32377e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 32387e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 3239*b45df837SPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 3240*b45df837SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 32417e8b88ecSPaolo Abeni sock_hold(ssk); 32427e8b88ecSPaolo Abeni 32437e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 32447e8b88ecSPaolo Abeni * created mptcp socket 32457e8b88ecSPaolo Abeni */ 32467e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 32477e8b88ecSPaolo Abeni 32487e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 32497e8b88ecSPaolo Abeni * uses the correct data 32507e8b88ecSPaolo Abeni */ 32517e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 325234c7757aSPaolo Abeni __mptcp_propagate_sndbuf(nsk, ssk); 32537e8b88ecSPaolo Abeni 32547e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 3255*b45df837SPaolo Abeni 3256*b45df837SPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK) 3257*b45df837SPaolo Abeni __mptcp_subflow_fully_established(msk, subflow, mp_opt); 325858b09919SPaolo Abeni bh_unlock_sock(nsk); 325958b09919SPaolo Abeni 3260b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3261b0519de8SFlorian Westphal return nsk; 3262b0519de8SFlorian Westphal } 3263b0519de8SFlorian Westphal 3264a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3265a6b118feSFlorian Westphal { 3266a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3267a6b118feSFlorian Westphal 3268a2d74374SPaolo Abeni msk->rcvspace_init = 1; 3269a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3270a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3271a6b118feSFlorian Westphal 3272a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3273a6b118feSFlorian Westphal 3274a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3275a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3276a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3277a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3278a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 3279a6b118feSFlorian Westphal } 3280a6b118feSFlorian Westphal 32811f6610b9SPaolo Abeni static struct sock *mptcp_accept(struct sock *ssk, int flags, int *err, 3282cf7da0d6SPeter Krystad bool kern) 3283cf7da0d6SPeter Krystad { 3284cf7da0d6SPeter Krystad struct sock *newsk; 3285cf7da0d6SPeter Krystad 32861f6610b9SPaolo Abeni pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk)); 32871f6610b9SPaolo Abeni newsk = inet_csk_accept(ssk, flags, err, kern); 3288cf7da0d6SPeter Krystad if (!newsk) 3289cf7da0d6SPeter Krystad return NULL; 3290cf7da0d6SPeter Krystad 32911f6610b9SPaolo Abeni pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk)); 3292cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3293cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3294cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3295cf7da0d6SPeter Krystad 3296cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 329758b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 329858b09919SPaolo Abeni 329958b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 330058b09919SPaolo Abeni * subflow_syn_recv_sock() 330158b09919SPaolo Abeni */ 330258b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 330358b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3304d6692b3bSFlorian Westphal goto out; 330558b09919SPaolo Abeni } 330658b09919SPaolo Abeni 3307cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 33081f6610b9SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3309fc518953SFlorian Westphal } else { 33101f6610b9SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), 3311fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3312cf7da0d6SPeter Krystad } 3313cf7da0d6SPeter Krystad 3314d6692b3bSFlorian Westphal out: 3315d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3316cf7da0d6SPeter Krystad return newsk; 3317cf7da0d6SPeter Krystad } 3318cf7da0d6SPeter Krystad 3319c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 33205c8c1640SGeliang Tang { 3321c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 332287952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 332387952603SPaolo Abeni 33246e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 33256e628cd3SPaolo Abeni 3326c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 33275efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3328c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3329c0bf3c6aSPaolo Abeni 333087952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3331b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 333287952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 33336511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 33345c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3335b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 33366511882cSPaolo Abeni 33376511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 33386511882cSPaolo Abeni * inet_sock_destruct() will dispose it 33396511882cSPaolo Abeni */ 33405e6300e7SEric Dumazet sk_forward_alloc_add(sk, msk->rmem_fwd_alloc); 33419531e4a8SEric Dumazet WRITE_ONCE(msk->rmem_fwd_alloc, 0); 33425c8c1640SGeliang Tang mptcp_token_destroy(msk); 33435c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 33444638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 33455c8c1640SGeliang Tang } 33465c8c1640SGeliang Tang 334779c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 334879c0949eSPeter Krystad { 3349c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3350c9fd9c5fSFlorian Westphal 335139880bd8SPaolo Abeni /* allow the following to close even the initial subflow */ 335239880bd8SPaolo Abeni msk->free_first = 1; 3353c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3354d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 335579c0949eSPeter Krystad } 335679c0949eSPeter Krystad 33576e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 33586e628cd3SPaolo Abeni { 33596e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 33606e628cd3SPaolo Abeni __mptcp_clean_una(sk); 33616e628cd3SPaolo Abeni else 3362e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 33636e628cd3SPaolo Abeni 33646e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 33656e628cd3SPaolo Abeni mptcp_schedule_work(sk); 33666e628cd3SPaolo Abeni } 33676e628cd3SPaolo Abeni 3368219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 33696e628cd3SPaolo Abeni { 33706e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 33716e628cd3SPaolo Abeni return; 33726e628cd3SPaolo Abeni 337373a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 337473a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 337573a0052aSGeliang Tang else 3376e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 33776e628cd3SPaolo Abeni } 33786e628cd3SPaolo Abeni 3379e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3380e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3381e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3382e9d09bacSPaolo Abeni 3383e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 338414c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3385e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 338614c441b5SPaolo Abeni { 3387e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 33882d6f5a2bSPaolo Abeni 3389e9d09bacSPaolo Abeni for (;;) { 3390877a72e3SPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED); 339156a666c4SPaolo Abeni struct list_head join_list; 339256a666c4SPaolo Abeni 3393c2e6048fSPaolo Abeni if (!flags) 3394c2e6048fSPaolo Abeni break; 3395c2e6048fSPaolo Abeni 339656a666c4SPaolo Abeni INIT_LIST_HEAD(&join_list); 339756a666c4SPaolo Abeni list_splice_init(&msk->join_list, &join_list); 339856a666c4SPaolo Abeni 3399c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 34006e628cd3SPaolo Abeni * 34016e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 34026e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 34036e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 34046e628cd3SPaolo Abeni * the subflow socket lock 34056e628cd3SPaolo Abeni */ 3406e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 34076e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 340856a666c4SPaolo Abeni 34093e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 341056a666c4SPaolo Abeni __mptcp_flush_join_list(sk, &join_list); 34112e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3412c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 34132d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 34142d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3415c2e6048fSPaolo Abeni 3416c2e6048fSPaolo Abeni cond_resched(); 34176e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 34186e628cd3SPaolo Abeni } 3419c2e6048fSPaolo Abeni 342065a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 342165a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 342217ebf8a4SXiang Yang if (unlikely(msk->cb_flags)) { 3423cf742d09SPaolo Abeni /* be sure to sync the msk state before taking actions 342444ee4764SPaolo Abeni * depending on sk_state (MPTCP_ERROR_REPORT) 342544ee4764SPaolo Abeni * On sk release avoid actions depending on the first subflow 3426490274b4SPaolo Abeni */ 3427cf742d09SPaolo Abeni if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first) 3428cf742d09SPaolo Abeni __mptcp_sync_state(sk, msk->pending_state); 3429e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 343015cc1045SPaolo Abeni __mptcp_error_report(sk); 343134c7757aSPaolo Abeni if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags)) 343234c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 343365a569b0SPaolo Abeni } 34346e628cd3SPaolo Abeni 343587952603SPaolo Abeni __mptcp_update_rmem(sk); 343614c441b5SPaolo Abeni } 343714c441b5SPaolo Abeni 3438bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3439bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3440bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3441bcd97734SPaolo Abeni */ 3442bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3443bcd97734SPaolo Abeni { 3444bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3445bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3446bcd97734SPaolo Abeni unsigned long timeout; 3447bcd97734SPaolo Abeni 3448bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3449bcd97734SPaolo Abeni return; 3450bcd97734SPaolo Abeni 3451bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3452bcd97734SPaolo Abeni if (tp->srtt_us) 3453bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3454bcd97734SPaolo Abeni else 3455bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3456bcd97734SPaolo Abeni timeout += jiffies; 3457bcd97734SPaolo Abeni 3458bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3459bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3460bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3461bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3462bcd97734SPaolo Abeni } 3463bcd97734SPaolo Abeni 3464a5efdbceSPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk, long status) 3465b19bc294SPaolo Abeni { 3466b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3467b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3468b19bc294SPaolo Abeni 3469a5efdbceSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_SEND)) { 3470b19bc294SPaolo Abeni mptcp_data_lock(sk); 3471b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 347273a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3473b19bc294SPaolo Abeni else 3474e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3475b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3476bcd97734SPaolo Abeni } 347734c7757aSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_SNDBUF)) { 347834c7757aSPaolo Abeni mptcp_data_lock(sk); 347934c7757aSPaolo Abeni if (!sock_owned_by_user(sk)) 348034c7757aSPaolo Abeni __mptcp_sync_sndbuf(sk); 348134c7757aSPaolo Abeni else 348234c7757aSPaolo Abeni __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags); 348334c7757aSPaolo Abeni mptcp_data_unlock(sk); 348434c7757aSPaolo Abeni } 3485a5efdbceSPaolo Abeni if (status & BIT(MPTCP_DELEGATE_ACK)) 3486bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3487b19bc294SPaolo Abeni } 3488b19bc294SPaolo Abeni 34892c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 34902c5ebd00SPaolo Abeni { 34912c5ebd00SPaolo Abeni /* should never be called, 34922c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 34932c5ebd00SPaolo Abeni */ 34942c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 34952c5ebd00SPaolo Abeni return 0; 34962c5ebd00SPaolo Abeni } 34972c5ebd00SPaolo Abeni 34982c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 34992c5ebd00SPaolo Abeni { 35002c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 35012c5ebd00SPaolo Abeni } 35022c5ebd00SPaolo Abeni 3503cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3504f870fa0bSMat Martineau { 3505f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3506f870fa0bSMat Martineau 3507cfb63e50SPaolo Abeni pr_debug("msk=%p, ssk=%p", msk, msk->first); 3508cfb63e50SPaolo Abeni if (WARN_ON_ONCE(!msk->first)) 3509cec37a6eSPeter Krystad return -EINVAL; 3510f870fa0bSMat Martineau 3511cfb63e50SPaolo Abeni return inet_csk_get_port(msk->first, snum); 3512cec37a6eSPeter Krystad } 3513f870fa0bSMat Martineau 3514cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3515cec37a6eSPeter Krystad { 3516cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3517cec37a6eSPeter Krystad struct mptcp_sock *msk; 3518cec37a6eSPeter Krystad struct sock *sk; 3519f870fa0bSMat Martineau 3520cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3521cec37a6eSPeter Krystad sk = subflow->conn; 3522cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3523cec37a6eSPeter Krystad 3524648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3525648ef4b8SMat Martineau 3526b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3527648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 35286d0060f6SMat Martineau 3529cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3530cec37a6eSPeter Krystad * accessing the field below 3531cec37a6eSPeter Krystad */ 3532cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 35331b1c7a0eSPeter Krystad 35346c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3535f870fa0bSMat Martineau } 3536f870fa0bSMat Martineau 3537866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3538cf7da0d6SPeter Krystad { 3539cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3540cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3541cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3542cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3543cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3544cf7da0d6SPeter Krystad } 3545cf7da0d6SPeter Krystad 3546e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3547f296234cSPeter Krystad { 3548e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3549f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3550f296234cSPeter Krystad struct sock *parent = (void *)msk; 35513e501490SPaolo Abeni bool ret = true; 3552f296234cSPeter Krystad 3553f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3554f296234cSPeter Krystad 3555f296234cSPeter Krystad /* mptcp socket already closing? */ 3556dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3557dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3558f296234cSPeter Krystad return false; 3559dc87efdbSFlorian Westphal } 3560f296234cSPeter Krystad 356155b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 356255b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 356355b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 356434c7757aSPaolo Abeni mptcp_propagate_sndbuf(parent, ssk); 356555b47ca7SPaolo Abeni return true; 356655b47ca7SPaolo Abeni } 3567f296234cSPeter Krystad 35683e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 35693e501490SPaolo Abeni goto err_prohibited; 357010f6d46cSPaolo Abeni 357155b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 35723e501490SPaolo Abeni * handle it 357310f6d46cSPaolo Abeni */ 35743e501490SPaolo Abeni mptcp_data_lock(parent); 35753e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 35763e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 35773e501490SPaolo Abeni if (ret) { 3578e16163b6SPaolo Abeni sock_hold(ssk); 35793e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3580e16163b6SPaolo Abeni } 35813e501490SPaolo Abeni } else { 35823e501490SPaolo Abeni sock_hold(ssk); 35833e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3584e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 35853e501490SPaolo Abeni } 35863e501490SPaolo Abeni mptcp_data_unlock(parent); 35873e501490SPaolo Abeni 3588dc87efdbSFlorian Westphal if (!ret) { 35893e501490SPaolo Abeni err_prohibited: 3590dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 359110f6d46cSPaolo Abeni return false; 3592dc87efdbSFlorian Westphal } 359310f6d46cSPaolo Abeni 359410f6d46cSPaolo Abeni return true; 3595f296234cSPeter Krystad } 3596f296234cSPeter Krystad 359776e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 359876e2a55dSPaolo Abeni { 359976e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 360076e2a55dSPaolo Abeni 360176e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 360276e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 360376e2a55dSPaolo Abeni } 360476e2a55dSPaolo Abeni 36056511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 36066511882cSPaolo Abeni { 36079531e4a8SEric Dumazet return READ_ONCE(sk->sk_forward_alloc) + 36089531e4a8SEric Dumazet READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc); 36096511882cSPaolo Abeni } 36106511882cSPaolo Abeni 3611644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3612644807e3SFlorian Westphal { 3613644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3614644807e3SFlorian Westphal u64 delta; 3615644807e3SFlorian Westphal 3616644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3617644807e3SFlorian Westphal return -EINVAL; 3618644807e3SFlorian Westphal 3619644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3620644807e3SFlorian Westphal return 0; 3621644807e3SFlorian Westphal 3622644807e3SFlorian Westphal delta = msk->write_seq - v; 362307c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 362407c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 362507c2c7a3SPaolo Abeni 362607c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 362707c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 362807c2c7a3SPaolo Abeni * so ignore that status, too. 362907c2c7a3SPaolo Abeni */ 363007c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 363107c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 363207c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 363307c2c7a3SPaolo Abeni } 3634644807e3SFlorian Westphal if (delta > INT_MAX) 3635644807e3SFlorian Westphal delta = INT_MAX; 3636644807e3SFlorian Westphal 3637644807e3SFlorian Westphal return (int)delta; 3638644807e3SFlorian Westphal } 3639644807e3SFlorian Westphal 3640e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) 3641644807e3SFlorian Westphal { 3642644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3643644807e3SFlorian Westphal bool slow; 3644644807e3SFlorian Westphal 3645644807e3SFlorian Westphal switch (cmd) { 3646644807e3SFlorian Westphal case SIOCINQ: 3647644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3648644807e3SFlorian Westphal return -EINVAL; 3649644807e3SFlorian Westphal 3650644807e3SFlorian Westphal lock_sock(sk); 3651644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3652e1d001faSBreno Leitao *karg = mptcp_inq_hint(sk); 3653644807e3SFlorian Westphal release_sock(sk); 3654644807e3SFlorian Westphal break; 3655644807e3SFlorian Westphal case SIOCOUTQ: 3656644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3657e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3658644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3659644807e3SFlorian Westphal break; 3660644807e3SFlorian Westphal case SIOCOUTQNSD: 3661644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3662e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, msk->snd_nxt); 3663644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3664644807e3SFlorian Westphal break; 3665644807e3SFlorian Westphal default: 3666644807e3SFlorian Westphal return -ENOIOCTLCMD; 3667644807e3SFlorian Westphal } 3668644807e3SFlorian Westphal 3669e1d001faSBreno Leitao return 0; 3670644807e3SFlorian Westphal } 3671644807e3SFlorian Westphal 367254f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 367354f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 367454f1944eSPaolo Abeni { 367554f1944eSPaolo Abeni subflow->request_mptcp = 0; 367654f1944eSPaolo Abeni __mptcp_do_fallback(msk); 367754f1944eSPaolo Abeni } 367854f1944eSPaolo Abeni 367954f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 368054f1944eSPaolo Abeni { 368154f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 368254f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 368354f1944eSPaolo Abeni int err = -EINVAL; 3684ccae357cSPaolo Abeni struct sock *ssk; 368554f1944eSPaolo Abeni 36863f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 36873f326a82SPaolo Abeni if (IS_ERR(ssk)) 36883f326a82SPaolo Abeni return PTR_ERR(ssk); 368954f1944eSPaolo Abeni 3690d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_SYN_SENT); 3691ccae357cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 369254f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 369354f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 369454f1944eSPaolo Abeni * TCP option space. 369554f1944eSPaolo Abeni */ 3696ccae357cSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info)) 369754f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 369854f1944eSPaolo Abeni #endif 3699ccae357cSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) { 3700ccae357cSPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT); 370154f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 370254f1944eSPaolo Abeni } 370354f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 370454f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 370554f1944eSPaolo Abeni 370654f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 370754f1944eSPaolo Abeni * acquired the subflow socket lock, too. 370854f1944eSPaolo Abeni */ 3709ccae357cSPaolo Abeni if (!msk->fastopening) 3710ccae357cSPaolo Abeni lock_sock(ssk); 3711ccae357cSPaolo Abeni 3712ccae357cSPaolo Abeni /* the following mirrors closely a very small chunk of code from 3713ccae357cSPaolo Abeni * __inet_stream_connect() 3714ccae357cSPaolo Abeni */ 3715ccae357cSPaolo Abeni if (ssk->sk_state != TCP_CLOSE) 3716ccae357cSPaolo Abeni goto out; 3717ccae357cSPaolo Abeni 3718ccae357cSPaolo Abeni if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) { 3719ccae357cSPaolo Abeni err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len); 3720ccae357cSPaolo Abeni if (err) 3721ccae357cSPaolo Abeni goto out; 3722ccae357cSPaolo Abeni } 3723ccae357cSPaolo Abeni 3724ccae357cSPaolo Abeni err = ssk->sk_prot->connect(ssk, uaddr, addr_len); 3725ccae357cSPaolo Abeni if (err < 0) 3726ccae357cSPaolo Abeni goto out; 3727ccae357cSPaolo Abeni 372808e39c0dSEric Dumazet inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk)); 3729ccae357cSPaolo Abeni 3730ccae357cSPaolo Abeni out: 3731ccae357cSPaolo Abeni if (!msk->fastopening) 3732ccae357cSPaolo Abeni release_sock(ssk); 373354f1944eSPaolo Abeni 373454f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 373554f1944eSPaolo Abeni * subflow_finish_connect() 373654f1944eSPaolo Abeni */ 3737ccae357cSPaolo Abeni if (unlikely(err)) { 3738131a6277SPaolo Abeni /* avoid leaving a dangling token in an unconnected socket */ 3739131a6277SPaolo Abeni mptcp_token_destroy(msk); 3740d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_CLOSE); 374154f1944eSPaolo Abeni return err; 374254f1944eSPaolo Abeni } 374354f1944eSPaolo Abeni 3744ccae357cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 3745786fc124SPaolo Abeni return 0; 374654f1944eSPaolo Abeni } 374754f1944eSPaolo Abeni 3748f870fa0bSMat Martineau static struct proto mptcp_prot = { 3749f870fa0bSMat Martineau .name = "MPTCP", 3750f870fa0bSMat Martineau .owner = THIS_MODULE, 3751f870fa0bSMat Martineau .init = mptcp_init_sock, 375254f1944eSPaolo Abeni .connect = mptcp_connect, 375318b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3754f870fa0bSMat Martineau .close = mptcp_close, 3755cf7da0d6SPeter Krystad .accept = mptcp_accept, 3756717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3757717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 375876e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 375979c0949eSPeter Krystad .destroy = mptcp_destroy, 3760f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3761644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3762f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 376314c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 37642c5ebd00SPaolo Abeni .hash = mptcp_hash, 37652c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3766cec37a6eSPeter Krystad .get_port = mptcp_get_port, 37676511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3768d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 37690defbb0aSEric Dumazet 3770d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 37710defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 37720defbb0aSEric Dumazet 3773d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3774d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3775989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3776d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3777f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 37782c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3779f870fa0bSMat Martineau .no_autobind = true, 3780f870fa0bSMat Martineau }; 3781f870fa0bSMat Martineau 37822303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 37832303f994SPeter Krystad { 37842303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 37858cf2ebdcSPaolo Abeni struct sock *ssk, *sk = sock->sk; 37868cf2ebdcSPaolo Abeni int err = -EINVAL; 37872303f994SPeter Krystad 37888cf2ebdcSPaolo Abeni lock_sock(sk); 37893f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 37903f326a82SPaolo Abeni if (IS_ERR(ssk)) { 37913f326a82SPaolo Abeni err = PTR_ERR(ssk); 37922303f994SPeter Krystad goto unlock; 37932303f994SPeter Krystad } 37942303f994SPeter Krystad 37958cf2ebdcSPaolo Abeni if (sk->sk_family == AF_INET) 37968cf2ebdcSPaolo Abeni err = inet_bind_sk(ssk, uaddr, addr_len); 37978cf2ebdcSPaolo Abeni #if IS_ENABLED(CONFIG_MPTCP_IPV6) 37988cf2ebdcSPaolo Abeni else if (sk->sk_family == AF_INET6) 37998cf2ebdcSPaolo Abeni err = inet6_bind_sk(ssk, uaddr, addr_len); 38008cf2ebdcSPaolo Abeni #endif 3801cf7da0d6SPeter Krystad if (!err) 38028cf2ebdcSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 38032303f994SPeter Krystad 38042303f994SPeter Krystad unlock: 38058cf2ebdcSPaolo Abeni release_sock(sk); 38062303f994SPeter Krystad return err; 38072303f994SPeter Krystad } 38082303f994SPeter Krystad 3809cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3810cf7da0d6SPeter Krystad { 3811cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3812cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 381340f56d0cSPaolo Abeni struct sock *ssk; 3814cf7da0d6SPeter Krystad int err; 3815cf7da0d6SPeter Krystad 3816cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3817cf7da0d6SPeter Krystad 3818cfdcfeedSMenglong Dong lock_sock(sk); 38190226436aSPaolo Abeni 38200226436aSPaolo Abeni err = -EINVAL; 38210226436aSPaolo Abeni if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 38220226436aSPaolo Abeni goto unlock; 38230226436aSPaolo Abeni 38243f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 38253f326a82SPaolo Abeni if (IS_ERR(ssk)) { 38263f326a82SPaolo Abeni err = PTR_ERR(ssk); 3827cf7da0d6SPeter Krystad goto unlock; 3828cf7da0d6SPeter Krystad } 3829cf7da0d6SPeter Krystad 3830d52b3c2bSGeliang Tang mptcp_set_state(sk, TCP_LISTEN); 3831cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 38325e20087dSFlorian Westphal 383340f56d0cSPaolo Abeni lock_sock(ssk); 383440f56d0cSPaolo Abeni err = __inet_listen_sk(ssk, backlog); 383540f56d0cSPaolo Abeni release_sock(ssk); 3836d52b3c2bSGeliang Tang mptcp_set_state(sk, inet_sk_state_load(ssk)); 383740f56d0cSPaolo Abeni 3838c558246eSMenglong Dong if (!err) { 3839c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 384040f56d0cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 384140f56d0cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED); 384221d9b73aSPaolo Abeni } 3843f8c9dfbdSGeliang Tang 3844cf7da0d6SPeter Krystad unlock: 3845cfdcfeedSMenglong Dong release_sock(sk); 3846cf7da0d6SPeter Krystad return err; 3847cf7da0d6SPeter Krystad } 3848cf7da0d6SPeter Krystad 3849cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3850cf7da0d6SPeter Krystad int flags, bool kern) 3851cf7da0d6SPeter Krystad { 3852cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 38531f6610b9SPaolo Abeni struct sock *ssk, *newsk; 3854cf7da0d6SPeter Krystad int err; 3855cf7da0d6SPeter Krystad 3856cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3857cf7da0d6SPeter Krystad 38585b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 385961761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 386061761231SPaolo Abeni */ 38611f6610b9SPaolo Abeni ssk = READ_ONCE(msk->first); 38621f6610b9SPaolo Abeni if (!ssk) 386371ba088cSPaolo Abeni return -EINVAL; 3864cf7da0d6SPeter Krystad 38651f6610b9SPaolo Abeni newsk = mptcp_accept(ssk, flags, &err, kern); 3866e76c8ef5SPaolo Abeni if (!newsk) 3867e76c8ef5SPaolo Abeni return err; 3868e76c8ef5SPaolo Abeni 3869e76c8ef5SPaolo Abeni lock_sock(newsk); 3870e76c8ef5SPaolo Abeni 3871e76c8ef5SPaolo Abeni __inet_accept(sock, newsock, newsk); 3872e76c8ef5SPaolo Abeni if (!mptcp_is_tcpsk(newsock->sk)) { 3873e76c8ef5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(newsk); 3874cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 38750397c6d8SPaolo Abeni 3876a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3877b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3878a5ef058dSPaolo Abeni 3879cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3880cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3881cf7da0d6SPeter Krystad */ 3882190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3883cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3884cf7da0d6SPeter Krystad 3885cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3886cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3887cf7da0d6SPeter Krystad } 388863740448SPaolo Abeni 388963740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 389063740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 389163740448SPaolo Abeni */ 389239880bd8SPaolo Abeni if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 389363740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 389463740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 389539880bd8SPaolo Abeni if (unlikely(list_is_singular(&msk->conn_list))) 3896d52b3c2bSGeliang Tang mptcp_set_state(newsk, TCP_CLOSE); 389763740448SPaolo Abeni } 3898cf7da0d6SPeter Krystad } 3899e76c8ef5SPaolo Abeni release_sock(newsk); 3900cf7da0d6SPeter Krystad 3901e76c8ef5SPaolo Abeni return 0; 3902cf7da0d6SPeter Krystad } 3903cf7da0d6SPeter Krystad 39048edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 39058edf0864SFlorian Westphal { 39068edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 39078edf0864SFlorian Westphal 39088edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 39098edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 39108edf0864SFlorian Westphal 39115cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 39126e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 39136e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 39146e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 39158edf0864SFlorian Westphal 39166e628cd3SPaolo Abeni return 0; 39178edf0864SFlorian Westphal } 39188edf0864SFlorian Westphal 39192303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 39202303f994SPeter Krystad struct poll_table_struct *wait) 39212303f994SPeter Krystad { 39221891c4a0SFlorian Westphal struct sock *sk = sock->sk; 39238ab183deSPaolo Abeni struct mptcp_sock *msk; 39242303f994SPeter Krystad __poll_t mask = 0; 39256b9831bfSPaolo Abeni u8 shutdown; 39268a05661bSPaolo Abeni int state; 39272303f994SPeter Krystad 39281891c4a0SFlorian Westphal msk = mptcp_sk(sk); 39291891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 39301891c4a0SFlorian Westphal 39318a05661bSPaolo Abeni state = inet_sk_state_load(sk); 39326719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 393371ba088cSPaolo Abeni if (state == TCP_LISTEN) { 39345426a4efSPaolo Abeni struct sock *ssk = READ_ONCE(msk->first); 39355b825727SPaolo Abeni 39365426a4efSPaolo Abeni if (WARN_ON_ONCE(!ssk)) 393771ba088cSPaolo Abeni return 0; 393871ba088cSPaolo Abeni 39395426a4efSPaolo Abeni return inet_csk_listen_poll(ssk); 394071ba088cSPaolo Abeni } 39418a05661bSPaolo Abeni 39426b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 39436b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 39446b9831bfSPaolo Abeni mask |= EPOLLHUP; 39456b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 39466b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 39476b9831bfSPaolo Abeni 39488a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 39498a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 39506b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 39516b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 39526b9831bfSPaolo Abeni else 39538edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 395408e39c0dSEric Dumazet } else if (state == TCP_SYN_SENT && 395508e39c0dSEric Dumazet inet_test_bit(DEFER_CONNECT, sk)) { 3956a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3957a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 39588a05661bSPaolo Abeni } 39591891c4a0SFlorian Westphal 3960d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 396115cc1045SPaolo Abeni smp_rmb(); 39629ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 396315cc1045SPaolo Abeni mask |= EPOLLERR; 396415cc1045SPaolo Abeni 39652303f994SPeter Krystad return mask; 39662303f994SPeter Krystad } 39672303f994SPeter Krystad 3968e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3969e42f1ac6SFlorian Westphal .family = PF_INET, 3970e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39710a3cc579SPaolo Abeni .release = inet_release, 3972e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3973786fc124SPaolo Abeni .connect = inet_stream_connect, 3974e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3975e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3976d2f77c53SPaolo Abeni .getname = inet_getname, 3977e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3978e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3979e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3980e42f1ac6SFlorian Westphal .listen = mptcp_listen, 398176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3982e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3983e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3984e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3985e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3986e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3987e42f1ac6SFlorian Westphal }; 39882303f994SPeter Krystad 3989f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3990f870fa0bSMat Martineau .type = SOCK_STREAM, 3991f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3992f870fa0bSMat Martineau .prot = &mptcp_prot, 39932303f994SPeter Krystad .ops = &mptcp_stream_ops, 39942303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3995f870fa0bSMat Martineau }; 3996f870fa0bSMat Martineau 3997b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3998b19bc294SPaolo Abeni { 3999b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 4000b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 4001b19bc294SPaolo Abeni int work_done = 0; 4002b19bc294SPaolo Abeni 4003b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 4004b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 4005b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 4006b19bc294SPaolo Abeni 4007b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 4008a5efdbceSPaolo Abeni if (!sock_owned_by_user(ssk)) { 4009a5efdbceSPaolo Abeni mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0)); 4010a5efdbceSPaolo Abeni } else { 4011a5efdbceSPaolo Abeni /* tcp_release_cb_override already processed 4012b19bc294SPaolo Abeni * the action or will do at next release_sock(). 4013b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 4014b19bc294SPaolo Abeni * CPU that scheduled it. 4015b19bc294SPaolo Abeni */ 4016a5efdbceSPaolo Abeni smp_wmb(); 4017a5efdbceSPaolo Abeni clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status); 4018a5efdbceSPaolo Abeni } 4019b19bc294SPaolo Abeni bh_unlock_sock(ssk); 4020b19bc294SPaolo Abeni sock_put(ssk); 4021b19bc294SPaolo Abeni 4022b19bc294SPaolo Abeni if (++work_done == budget) 4023b19bc294SPaolo Abeni return budget; 4024b19bc294SPaolo Abeni } 4025b19bc294SPaolo Abeni 4026b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 4027b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 4028b19bc294SPaolo Abeni */ 4029b19bc294SPaolo Abeni napi_complete_done(napi, 0); 4030b19bc294SPaolo Abeni return work_done; 4031b19bc294SPaolo Abeni } 4032b19bc294SPaolo Abeni 4033d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 4034f870fa0bSMat Martineau { 4035b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 4036b19bc294SPaolo Abeni int cpu; 4037b19bc294SPaolo Abeni 40382303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 40392303f994SPeter Krystad 4040d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 4041d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 4042d027236cSPaolo Abeni 4043b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 4044b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 4045b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 4046b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 404716d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 404816d083e2SJakub Kicinski mptcp_napi_poll); 4049b19bc294SPaolo Abeni napi_enable(&delegated->napi); 4050b19bc294SPaolo Abeni } 4051b19bc294SPaolo Abeni 40522303f994SPeter Krystad mptcp_subflow_init(); 40531b1c7a0eSPeter Krystad mptcp_pm_init(); 4054ed1ad86bSGeliang Tang mptcp_sched_init(); 40552c5ebd00SPaolo Abeni mptcp_token_init(); 40562303f994SPeter Krystad 4057f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 4058f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 4059f870fa0bSMat Martineau 4060f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 40616771bfd9SFlorian Westphal 40626771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 4063f870fa0bSMat Martineau } 4064f870fa0bSMat Martineau 4065f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 4066e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 4067e42f1ac6SFlorian Westphal .family = PF_INET6, 4068e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 40690a3cc579SPaolo Abeni .release = inet6_release, 4070e42f1ac6SFlorian Westphal .bind = mptcp_bind, 4071786fc124SPaolo Abeni .connect = inet_stream_connect, 4072e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 4073e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 4074d2f77c53SPaolo Abeni .getname = inet6_getname, 4075e42f1ac6SFlorian Westphal .poll = mptcp_poll, 4076e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 4077e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 4078e42f1ac6SFlorian Westphal .listen = mptcp_listen, 407976e2a55dSPaolo Abeni .shutdown = inet_shutdown, 4080e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 4081e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 4082e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 4083e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 4084e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 4085e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 40863986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 4087e42f1ac6SFlorian Westphal #endif 4088e42f1ac6SFlorian Westphal }; 4089e42f1ac6SFlorian Westphal 4090f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 4091f870fa0bSMat Martineau 4092f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 4093f870fa0bSMat Martineau .type = SOCK_STREAM, 4094f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 4095f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 40962303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 4097f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 4098f870fa0bSMat Martineau }; 4099f870fa0bSMat Martineau 4100d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 4101f870fa0bSMat Martineau { 4102f870fa0bSMat Martineau int err; 4103f870fa0bSMat Martineau 4104f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 4105f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 4106f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 4107b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 4108f5f80e32SEric Dumazet mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np); 4109f870fa0bSMat Martineau 4110f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 4111f870fa0bSMat Martineau if (err) 4112f870fa0bSMat Martineau return err; 4113f870fa0bSMat Martineau 4114f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 4115f870fa0bSMat Martineau if (err) 4116f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 4117f870fa0bSMat Martineau 4118f870fa0bSMat Martineau return err; 4119f870fa0bSMat Martineau } 4120f870fa0bSMat Martineau #endif 4121