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 40816a9a9daSMat Martineau static void mptcp_stop_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: 44816a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 44916a9a9daSMat Martineau break; 45016a9a9daSMat Martineau case TCP_CLOSING: 45116a9a9daSMat Martineau case TCP_LAST_ACK: 45216a9a9daSMat Martineau inet_sk_state_store(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: 6133721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6143721b9b6SMat Martineau break; 6153721b9b6SMat Martineau case TCP_FIN_WAIT1: 6163721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6173721b9b6SMat Martineau break; 6183721b9b6SMat Martineau case TCP_FIN_WAIT2: 6193721b9b6SMat Martineau inet_sk_state_store(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 773*d5fbeff1SPaolo Abeni void __mptcp_error_report(struct sock *sk) 774*d5fbeff1SPaolo Abeni { 775*d5fbeff1SPaolo Abeni struct mptcp_subflow_context *subflow; 776*d5fbeff1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 777*d5fbeff1SPaolo Abeni 778*d5fbeff1SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 779*d5fbeff1SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 780*d5fbeff1SPaolo Abeni int err = sock_error(ssk); 781*d5fbeff1SPaolo Abeni int ssk_state; 782*d5fbeff1SPaolo Abeni 783*d5fbeff1SPaolo Abeni if (!err) 784*d5fbeff1SPaolo Abeni continue; 785*d5fbeff1SPaolo Abeni 786*d5fbeff1SPaolo Abeni /* only propagate errors on fallen-back sockets or 787*d5fbeff1SPaolo Abeni * on MPC connect 788*d5fbeff1SPaolo Abeni */ 789*d5fbeff1SPaolo Abeni if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk)) 790*d5fbeff1SPaolo Abeni continue; 791*d5fbeff1SPaolo Abeni 792*d5fbeff1SPaolo Abeni /* We need to propagate only transition to CLOSE state. 793*d5fbeff1SPaolo Abeni * Orphaned socket will see such state change via 794*d5fbeff1SPaolo Abeni * subflow_sched_work_if_closed() and that path will properly 795*d5fbeff1SPaolo Abeni * destroy the msk as needed. 796*d5fbeff1SPaolo Abeni */ 797*d5fbeff1SPaolo Abeni ssk_state = inet_sk_state_load(ssk); 798*d5fbeff1SPaolo Abeni if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD)) 799*d5fbeff1SPaolo Abeni inet_sk_state_store(sk, ssk_state); 800*d5fbeff1SPaolo Abeni WRITE_ONCE(sk->sk_err, -err); 801*d5fbeff1SPaolo Abeni 802*d5fbeff1SPaolo Abeni /* This barrier is coupled with smp_rmb() in mptcp_poll() */ 803*d5fbeff1SPaolo Abeni smp_wmb(); 804*d5fbeff1SPaolo Abeni sk_error_report(sk); 805*d5fbeff1SPaolo Abeni break; 806*d5fbeff1SPaolo Abeni } 807*d5fbeff1SPaolo Abeni } 808*d5fbeff1SPaolo Abeni 8092e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 8102e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 8112e52213cSFlorian Westphal */ 81299d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 8132e52213cSFlorian Westphal { 8142e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 8152e52213cSFlorian Westphal unsigned int moved = 0; 8162e52213cSFlorian Westphal 8172e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 81887952603SPaolo Abeni __mptcp_ofo_queue(msk); 819499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 820499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 821499ada50SPaolo Abeni __mptcp_error_report(sk); 822499ada50SPaolo Abeni else 823e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 824499ada50SPaolo Abeni } 825ab174ad8SPaolo Abeni 826ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 827ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 828ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 829ab174ad8SPaolo Abeni * do it. 830ab174ad8SPaolo Abeni */ 831ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 832ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 83399d1055cSPaolo Abeni return moved > 0; 8342e52213cSFlorian Westphal } 8352e52213cSFlorian Westphal 8362e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 837101f6f85SFlorian Westphal { 8386719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 839101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 84013c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 841101f6f85SFlorian Westphal 842d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 843d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 844d7b1bfd0SPaolo Abeni * more data to the msk receive queue 845d7b1bfd0SPaolo Abeni */ 846d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 847d7b1bfd0SPaolo Abeni return; 848d7b1bfd0SPaolo Abeni 84913c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 85013c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 85113c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 85213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 85313c7ba0cSFlorian Westphal 85499d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 855ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 856ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 85799d1055cSPaolo Abeni return; 858ce599c51SPaolo Abeni } 8592e52213cSFlorian Westphal 86099d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 86199d1055cSPaolo Abeni mptcp_data_lock(sk); 862612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 863101f6f85SFlorian Westphal sk->sk_data_ready(sk); 864612f71d7SPaolo Abeni 86599d1055cSPaolo Abeni mptcp_data_unlock(sk); 86699d1055cSPaolo Abeni } 867101f6f85SFlorian Westphal 86855b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 86955b47ca7SPaolo Abeni { 87055b47ca7SPaolo Abeni mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 87155b47ca7SPaolo Abeni WRITE_ONCE(msk->allow_infinite_fallback, false); 87255b47ca7SPaolo Abeni mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 87355b47ca7SPaolo Abeni } 87455b47ca7SPaolo Abeni 8753e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 876ec3edaa7SPeter Krystad { 8773e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8785cf92bbaSPaolo Abeni 8793e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 88078962489SFlorian Westphal return false; 881ec3edaa7SPeter Krystad 8823e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8833e501490SPaolo Abeni * at close time 8843e501490SPaolo Abeni */ 8853e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8863e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 88778962489SFlorian Westphal 8886f06b4d4SPaolo Abeni mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++; 8893e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 89055b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 8913e501490SPaolo Abeni return true; 89278962489SFlorian Westphal } 89378962489SFlorian Westphal 89456a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 89578962489SFlorian Westphal { 8963e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8973e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 89878962489SFlorian Westphal 89956a666c4SPaolo Abeni list_for_each_entry_safe(subflow, tmp, join_list, node) { 9003e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 9013e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 9023e501490SPaolo Abeni 9033e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 9043e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 9053e501490SPaolo Abeni mptcp_subflow_reset(ssk); 9063e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 90778962489SFlorian Westphal } 908ec3edaa7SPeter Krystad } 909ec3edaa7SPeter Krystad 910b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 911b51f9b80SPaolo Abeni { 912b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 913b51f9b80SPaolo Abeni } 914b51f9b80SPaolo Abeni 915b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 916b51f9b80SPaolo Abeni { 917b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 918b51f9b80SPaolo Abeni unsigned long tout; 919b51f9b80SPaolo Abeni 920e16163b6SPaolo Abeni /* prevent rescheduling on close */ 921e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 922e16163b6SPaolo Abeni return; 923e16163b6SPaolo Abeni 92433d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 925b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 926b51f9b80SPaolo Abeni } 927b51f9b80SPaolo Abeni 928ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 929ba8f48f7SPaolo Abeni { 930ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 931ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 932ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 933ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 934ba8f48f7SPaolo Abeni */ 935ba8f48f7SPaolo Abeni sock_hold(sk); 936ba8f48f7SPaolo Abeni return true; 937ba8f48f7SPaolo Abeni } 938ba8f48f7SPaolo Abeni return false; 939ba8f48f7SPaolo Abeni } 940ba8f48f7SPaolo Abeni 9417a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9427a6a6cbcSPaolo Abeni { 9437a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9447a6a6cbcSPaolo Abeni 945109cdeb8SGeliang Tang msk_owned_by_me(msk); 9467a6a6cbcSPaolo Abeni 9477a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 94899d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9497a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9507a6a6cbcSPaolo Abeni } 9517a6a6cbcSPaolo Abeni 9527a6a6cbcSPaolo Abeni return NULL; 9537a6a6cbcSPaolo Abeni } 9547a6a6cbcSPaolo Abeni 9553f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 95657040755SPaolo Abeni const struct sk_buff *skb, 95757040755SPaolo Abeni const struct mptcp_ext *mpext) 9586d0060f6SMat Martineau { 95957040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 96057040755SPaolo Abeni return false; 96157040755SPaolo Abeni 9625a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9635a369ca6SPaolo Abeni * mapping has not been xmitted yet 9645a369ca6SPaolo Abeni */ 9655a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9665a369ca6SPaolo Abeni !mpext->frozen; 96757040755SPaolo Abeni } 96857040755SPaolo Abeni 96929249eacSPaolo Abeni /* we can append data to the given data frag if: 97029249eacSPaolo Abeni * - there is space available in the backing page_frag 97129249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 97229249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 97329249eacSPaolo Abeni */ 97418b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 97518b683bfSPaolo Abeni const struct page_frag *pfrag, 97618b683bfSPaolo Abeni const struct mptcp_data_frag *df) 97718b683bfSPaolo Abeni { 97818b683bfSPaolo Abeni return df && pfrag->page == df->page && 979d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 98029249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 98118b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 98218b683bfSPaolo Abeni } 98318b683bfSPaolo Abeni 984d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 98518b683bfSPaolo Abeni { 986d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9877948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 988d027236cSPaolo Abeni } 989d027236cSPaolo Abeni 990d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 991d027236cSPaolo Abeni { 992d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 993d027236cSPaolo Abeni 99418b683bfSPaolo Abeni list_del(&dfrag->list); 995d027236cSPaolo Abeni dfrag_uncharge(sk, len); 99618b683bfSPaolo Abeni put_page(dfrag->page); 99718b683bfSPaolo Abeni } 99818b683bfSPaolo Abeni 9996e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 100018b683bfSPaolo Abeni { 100118b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100218b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1003e1ff9e82SDavide Caratti u64 snd_una; 1004e1ff9e82SDavide Caratti 10057439d687SPaolo Abeni snd_una = msk->snd_una; 100618b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 100718b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 100818b683bfSPaolo Abeni break; 100918b683bfSPaolo Abeni 10101e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10111e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10121e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1013d9ca1de8SPaolo Abeni break; 10141e1d9d6fSPaolo Abeni 10151e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10161e1d9d6fSPaolo Abeni } 10171e1d9d6fSPaolo Abeni 1018d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1019d027236cSPaolo Abeni } 1020d027236cSPaolo Abeni 10217948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10227948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 102353eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 102453eb4c38SPaolo Abeni 10251e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10261e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10271e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 102853eb4c38SPaolo Abeni goto out; 10291e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10301e1d9d6fSPaolo Abeni goto out; 10311e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10321e1d9d6fSPaolo Abeni } 10337948f6ccSFlorian Westphal 10347948f6ccSFlorian Westphal dfrag->data_seq += delta; 103553eb4c38SPaolo Abeni dfrag->offset += delta; 10367948f6ccSFlorian Westphal dfrag->data_len -= delta; 1037d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10387948f6ccSFlorian Westphal 10397948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10407948f6ccSFlorian Westphal } 10417948f6ccSFlorian Westphal 10421e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10431e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10441e1d9d6fSPaolo Abeni msk->recovery = false; 10451e1d9d6fSPaolo Abeni 104653eb4c38SPaolo Abeni out: 10473241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10483241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 104933d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10506e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10516e628cd3SPaolo Abeni } else { 10526e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10536e628cd3SPaolo Abeni } 10546e628cd3SPaolo Abeni } 10556e628cd3SPaolo Abeni 1056417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1057417789dfSPaolo Abeni { 1058765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1059765ff425SPaolo Abeni 1060417789dfSPaolo Abeni __mptcp_clean_una(sk); 1061417789dfSPaolo Abeni mptcp_write_space(sk); 1062417789dfSPaolo Abeni } 1063417789dfSPaolo Abeni 1064b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1065b5941f06SPaolo Abeni { 1066b5941f06SPaolo Abeni mptcp_data_lock(sk); 1067b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1068b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1069b5941f06SPaolo Abeni } 1070b5941f06SPaolo Abeni 1071724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 107218b683bfSPaolo Abeni { 1073d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1074d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1075d9ca1de8SPaolo Abeni bool first = true; 1076d9ca1de8SPaolo Abeni 107718b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1078d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1079d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1080d9ca1de8SPaolo Abeni 1081d9ca1de8SPaolo Abeni if (first) 1082d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1083d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1084d9ca1de8SPaolo Abeni first = false; 1085d9ca1de8SPaolo Abeni } 1086724cfd2eSPaolo Abeni } 1087724cfd2eSPaolo Abeni 1088724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1089724cfd2eSPaolo Abeni * data 1090724cfd2eSPaolo Abeni */ 1091724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1092724cfd2eSPaolo Abeni { 1093724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1094724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1095724cfd2eSPaolo Abeni return true; 1096724cfd2eSPaolo Abeni 1097724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 109818b683bfSPaolo Abeni return false; 109918b683bfSPaolo Abeni } 110018b683bfSPaolo Abeni 110118b683bfSPaolo Abeni static struct mptcp_data_frag * 110218b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 110318b683bfSPaolo Abeni int orig_offset) 110418b683bfSPaolo Abeni { 110518b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 110618b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 110718b683bfSPaolo Abeni 110818b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 110918b683bfSPaolo Abeni dfrag->data_len = 0; 111018b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 111118b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 111218b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1113d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 111418b683bfSPaolo Abeni dfrag->page = pfrag->page; 111518b683bfSPaolo Abeni 111618b683bfSPaolo Abeni return dfrag; 111718b683bfSPaolo Abeni } 111818b683bfSPaolo Abeni 1119caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1120caf971dfSPaolo Abeni int mss_now; 1121caf971dfSPaolo Abeni int size_goal; 1122d9ca1de8SPaolo Abeni u16 limit; 1123d9ca1de8SPaolo Abeni u16 sent; 1124d9ca1de8SPaolo Abeni unsigned int flags; 11251094c6feSPaolo Abeni bool data_lock_held; 1126caf971dfSPaolo Abeni }; 1127caf971dfSPaolo Abeni 1128b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1129b713d006SPaolo Abeni u64 data_seq, int avail_size) 11306f8a612aSFlorian Westphal { 11316f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1132b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11336f8a612aSFlorian Westphal 11346f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11356f8a612aSFlorian Westphal return avail_size; 11366f8a612aSFlorian Westphal 1137b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1138b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11396f8a612aSFlorian Westphal 114092be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1141b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 114292be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 114392be2f52SPaolo Abeni } 11446f8a612aSFlorian Westphal 11456f8a612aSFlorian Westphal return avail_size; 11466f8a612aSFlorian Westphal } 11476f8a612aSFlorian Westphal 1148724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1149724cfd2eSPaolo Abeni { 1150724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1151724cfd2eSPaolo Abeni 1152724cfd2eSPaolo Abeni if (!mpext) 1153724cfd2eSPaolo Abeni return false; 1154724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1155724cfd2eSPaolo Abeni return true; 1156724cfd2eSPaolo Abeni } 1157724cfd2eSPaolo Abeni 11586e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1159724cfd2eSPaolo Abeni { 1160724cfd2eSPaolo Abeni struct sk_buff *skb; 1161724cfd2eSPaolo Abeni 11626e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1163724cfd2eSPaolo Abeni if (likely(skb)) { 11646e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1165724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1166a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1167f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1168724cfd2eSPaolo Abeni return skb; 1169724cfd2eSPaolo Abeni } 1170724cfd2eSPaolo Abeni __kfree_skb(skb); 1171724cfd2eSPaolo Abeni } else { 1172724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1173724cfd2eSPaolo Abeni } 1174724cfd2eSPaolo Abeni return NULL; 1175724cfd2eSPaolo Abeni } 1176724cfd2eSPaolo Abeni 1177f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1178724cfd2eSPaolo Abeni { 1179724cfd2eSPaolo Abeni struct sk_buff *skb; 1180724cfd2eSPaolo Abeni 11816e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1182724cfd2eSPaolo Abeni if (!skb) 1183f70cad10SPaolo Abeni return NULL; 1184724cfd2eSPaolo Abeni 1185724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1186f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1187f70cad10SPaolo Abeni return skb; 1188724cfd2eSPaolo Abeni } 11893ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1190724cfd2eSPaolo Abeni kfree_skb(skb); 1191f70cad10SPaolo Abeni return NULL; 1192724cfd2eSPaolo Abeni } 1193724cfd2eSPaolo Abeni 1194f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1195724cfd2eSPaolo Abeni { 11961094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1197724cfd2eSPaolo Abeni 11981094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1199724cfd2eSPaolo Abeni } 1200724cfd2eSPaolo Abeni 1201d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1202d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1203d0cc2987SGeliang Tang */ 1204d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1205d0cc2987SGeliang Tang { 1206d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1207d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1208d0cc2987SGeliang Tang int offset = skb->len - added; 1209d0cc2987SGeliang Tang 1210d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1211d0cc2987SGeliang Tang } 1212d0cc2987SGeliang Tang 12131e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12141e39e5a3SGeliang Tang struct sock *ssk, 12151e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12161e39e5a3SGeliang Tang { 12171e39e5a3SGeliang Tang if (!mpext) 12181e39e5a3SGeliang Tang return; 12191e39e5a3SGeliang Tang 12201e39e5a3SGeliang Tang mpext->infinite_map = 1; 12211e39e5a3SGeliang Tang mpext->data_len = 0; 12221e39e5a3SGeliang Tang 1223104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12241e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12251e39e5a3SGeliang Tang pr_fallback(msk); 1226d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12271e39e5a3SGeliang Tang } 12281e39e5a3SGeliang Tang 122957040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1230d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1231caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 123257040755SPaolo Abeni { 1233d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1234f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12356d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12366f8a612aSFlorian Westphal bool zero_window_probe = false; 12376d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1238f70cad10SPaolo Abeni bool can_coalesce = false; 1239f70cad10SPaolo Abeni bool reuse_skb = true; 1240f70cad10SPaolo Abeni struct sk_buff *skb; 1241f70cad10SPaolo Abeni size_t copy; 1242f70cad10SPaolo Abeni int i; 12436d0060f6SMat Martineau 1244e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1245d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12466d0060f6SMat Martineau 1247f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1248f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1249f70cad10SPaolo Abeni return 0; 1250f70cad10SPaolo Abeni 1251c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1252c886d702SPaolo Abeni return -EAGAIN; 1253c886d702SPaolo Abeni 1254d9ca1de8SPaolo Abeni /* compute send limit */ 1255d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1256f70cad10SPaolo Abeni copy = info->size_goal; 1257f70cad10SPaolo Abeni 125857040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1259f70cad10SPaolo Abeni if (skb && copy > skb->len) { 126057040755SPaolo Abeni /* Limit the write to the size available in the 126157040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 126257040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 126357040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 126457040755SPaolo Abeni * SSN association set here 126557040755SPaolo Abeni */ 1266d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12671094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126857040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12691094c6feSPaolo Abeni goto alloc_skb; 12701094c6feSPaolo Abeni } 12711094c6feSPaolo Abeni 1272f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1273f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1274657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1275f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1276f70cad10SPaolo Abeni goto alloc_skb; 127715e6ca97SPaolo Abeni } 127818b683bfSPaolo Abeni 1279f70cad10SPaolo Abeni copy -= skb->len; 1280f70cad10SPaolo Abeni } else { 12811094c6feSPaolo Abeni alloc_skb: 1282f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1283f70cad10SPaolo Abeni if (!skb) 1284f70cad10SPaolo Abeni return -ENOMEM; 1285f70cad10SPaolo Abeni 1286f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1287f70cad10SPaolo Abeni reuse_skb = false; 1288f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1289f70cad10SPaolo Abeni } 12901094c6feSPaolo Abeni 12916f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1292b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1293f70cad10SPaolo Abeni if (copy == 0) { 12947439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12957439d687SPaolo Abeni 1296f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 129727728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12986f8a612aSFlorian Westphal return 0; 12996f8a612aSFlorian Westphal } 13006f8a612aSFlorian Westphal 1301f70cad10SPaolo Abeni zero_window_probe = true; 1302f70cad10SPaolo Abeni data_seq = snd_una - 1; 1303f70cad10SPaolo Abeni copy = 1; 130457040755SPaolo Abeni 1305f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1306f70cad10SPaolo Abeni * ssk write queue 1307f70cad10SPaolo Abeni */ 1308f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1309f70cad10SPaolo Abeni } 1310f70cad10SPaolo Abeni 1311f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1312f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 131327728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1314e2223995SPaolo Abeni return -ENOMEM; 131535759383SFlorian Westphal } 131618b683bfSPaolo Abeni 1317f70cad10SPaolo Abeni if (can_coalesce) { 1318f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1319f70cad10SPaolo Abeni } else { 1320f70cad10SPaolo Abeni get_page(dfrag->page); 1321f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 132257040755SPaolo Abeni } 132357040755SPaolo Abeni 1324f70cad10SPaolo Abeni skb->len += copy; 1325f70cad10SPaolo Abeni skb->data_len += copy; 1326f70cad10SPaolo Abeni skb->truesize += copy; 1327f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1328f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1329f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1330f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1331f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1332f70cad10SPaolo Abeni 1333f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1334f70cad10SPaolo Abeni if (reuse_skb) { 1335f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1336f70cad10SPaolo Abeni mpext->data_len += copy; 1337f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1338f70cad10SPaolo Abeni goto out; 1339724cfd2eSPaolo Abeni } 13406d0060f6SMat Martineau 13416d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1342d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13436d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1344f70cad10SPaolo Abeni mpext->data_len = copy; 13456d0060f6SMat Martineau mpext->use_map = 1; 13466d0060f6SMat Martineau mpext->dsn64 = 1; 13476d0060f6SMat Martineau 13486d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13496d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13506d0060f6SMat Martineau mpext->dsn64); 13516d0060f6SMat Martineau 13526f8a612aSFlorian Westphal if (zero_window_probe) { 1353f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13546f8a612aSFlorian Westphal mpext->frozen = 1; 1355d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1356f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13576f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1358d0cc2987SGeliang Tang return 0; 13596f8a612aSFlorian Westphal } 136057040755SPaolo Abeni out: 1361d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1362f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13631e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13641e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13650eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1366f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1367f70cad10SPaolo Abeni return copy; 13686d0060f6SMat Martineau } 13696d0060f6SMat Martineau 1370d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1371d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1372d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1373d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1374d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1375d5f49190SPaolo Abeni 1376d5f49190SPaolo Abeni struct subflow_send_info { 1377d5f49190SPaolo Abeni struct sock *ssk; 13783ce0852cSPaolo Abeni u64 linger_time; 1379d5f49190SPaolo Abeni }; 1380d5f49190SPaolo Abeni 1381ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1382ff5a0b42SPaolo Abeni { 1383ff5a0b42SPaolo Abeni if (!subflow->stale) 1384ff5a0b42SPaolo Abeni return; 1385ff5a0b42SPaolo Abeni 1386ff5a0b42SPaolo Abeni subflow->stale = 0; 1387fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1388ff5a0b42SPaolo Abeni } 1389ff5a0b42SPaolo Abeni 1390ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1391ff5a0b42SPaolo Abeni { 1392ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1393ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1394ff5a0b42SPaolo Abeni 1395ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1396ff5a0b42SPaolo Abeni return false; 1397ff5a0b42SPaolo Abeni 1398ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1399ff5a0b42SPaolo Abeni } 1400ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1401ff5a0b42SPaolo Abeni } 1402ff5a0b42SPaolo Abeni 14033ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14043ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14053ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14063ce0852cSPaolo Abeni 140733d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140833d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140933d41c9cSPaolo Abeni * additionally updates the rtx timeout 141033d41c9cSPaolo Abeni */ 141107336a87SGeliang Tang struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1412f296234cSPeter Krystad { 14133ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1414f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 141533d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14163ce0852cSPaolo Abeni u32 pace, burst, wmem; 1417d5f49190SPaolo Abeni int i, nr_active = 0; 1418d5f49190SPaolo Abeni struct sock *ssk; 14193ce0852cSPaolo Abeni u64 linger_time; 142033d41c9cSPaolo Abeni long tout = 0; 1421f296234cSPeter Krystad 1422d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14233ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1424d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14253ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1426d5f49190SPaolo Abeni } 14273ce0852cSPaolo Abeni 1428d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1429e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1430d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1431d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1432f296234cSPeter Krystad continue; 1433d5f49190SPaolo Abeni 143433d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1435d5f49190SPaolo Abeni nr_active += !subflow->backup; 14363ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14373ce0852cSPaolo Abeni if (unlikely(!pace)) { 14383ce0852cSPaolo Abeni /* init pacing rate from socket */ 14393ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14403ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1441d5f49190SPaolo Abeni if (!pace) 1442d5f49190SPaolo Abeni continue; 14433ce0852cSPaolo Abeni } 1444d5f49190SPaolo Abeni 14453ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14463ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1447d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14483ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1449d5f49190SPaolo Abeni } 1450f296234cSPeter Krystad } 145133d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1452f296234cSPeter Krystad 1453d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1454d5f49190SPaolo Abeni if (!nr_active) 14553ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1456d5f49190SPaolo Abeni 14573ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14583ce0852cSPaolo Abeni * faster flow, we need to: 14593ce0852cSPaolo Abeni * - estimate the faster flow linger time 14603ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14613ce0852cSPaolo Abeni * by the faster flow 14623ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14633ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14643ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14653ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14663ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14673ce0852cSPaolo Abeni */ 14683ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1469b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1470d5f49190SPaolo Abeni return NULL; 14713ce0852cSPaolo Abeni 1472b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14733ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1474ebc1e08fSGeliang Tang if (!burst) 1475b713d006SPaolo Abeni return ssk; 1476b713d006SPaolo Abeni 14773ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14783ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14793ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14803ce0852cSPaolo Abeni burst + wmem); 14813ce0852cSPaolo Abeni msk->snd_burst = burst; 14823ce0852cSPaolo Abeni return ssk; 1483f296234cSPeter Krystad } 1484f296234cSPeter Krystad 1485b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1486d9ca1de8SPaolo Abeni { 1487d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1488d9ca1de8SPaolo Abeni release_sock(ssk); 1489d9ca1de8SPaolo Abeni } 1490d9ca1de8SPaolo Abeni 14910d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 14920d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 14930d199e43SFlorian Westphal u32 sent) 14940d199e43SFlorian Westphal { 14950d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 14960d199e43SFlorian Westphal 14970d199e43SFlorian Westphal dfrag->already_sent += sent; 14980d199e43SFlorian Westphal 14990d199e43SFlorian Westphal msk->snd_burst -= sent; 15000d199e43SFlorian Westphal 15010d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15020d199e43SFlorian Westphal 15030d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15040d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15050d199e43SFlorian Westphal * old segments. 15060d199e43SFlorian Westphal * 15070d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15080d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15090d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15100d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15110d199e43SFlorian Westphal */ 151238967f42SPaolo Abeni if (likely(after64(snd_nxt_new, msk->snd_nxt))) { 151338967f42SPaolo Abeni msk->bytes_sent += snd_nxt_new - msk->snd_nxt; 15140d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15150d199e43SFlorian Westphal } 151638967f42SPaolo Abeni } 15170d199e43SFlorian Westphal 15188b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15193241a9c0SFlorian Westphal { 1520e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1521e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15223241a9c0SFlorian Westphal } 15233241a9c0SFlorian Westphal 1524c5b4297dSGeliang Tang static int __subflow_push_pending(struct sock *sk, struct sock *ssk, 1525c5b4297dSGeliang Tang struct mptcp_sendmsg_info *info) 1526c5b4297dSGeliang Tang { 1527c5b4297dSGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 1528c5b4297dSGeliang Tang struct mptcp_data_frag *dfrag; 1529c5b4297dSGeliang Tang int len, copied = 0, err = 0; 1530c5b4297dSGeliang Tang 1531c5b4297dSGeliang Tang while ((dfrag = mptcp_send_head(sk))) { 1532c5b4297dSGeliang Tang info->sent = dfrag->already_sent; 1533c5b4297dSGeliang Tang info->limit = dfrag->data_len; 1534c5b4297dSGeliang Tang len = dfrag->data_len - dfrag->already_sent; 1535c5b4297dSGeliang Tang while (len > 0) { 1536c5b4297dSGeliang Tang int ret = 0; 1537c5b4297dSGeliang Tang 1538c5b4297dSGeliang Tang ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info); 1539c5b4297dSGeliang Tang if (ret <= 0) { 1540c5b4297dSGeliang Tang err = copied ? : ret; 1541c5b4297dSGeliang Tang goto out; 1542c5b4297dSGeliang Tang } 1543c5b4297dSGeliang Tang 1544c5b4297dSGeliang Tang info->sent += ret; 1545c5b4297dSGeliang Tang copied += ret; 1546c5b4297dSGeliang Tang len -= ret; 1547c5b4297dSGeliang Tang 1548c5b4297dSGeliang Tang mptcp_update_post_push(msk, dfrag, ret); 1549c5b4297dSGeliang Tang } 1550c5b4297dSGeliang Tang WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1551c5b4297dSGeliang Tang 1552c5b4297dSGeliang Tang if (msk->snd_burst <= 0 || 1553c5b4297dSGeliang Tang !sk_stream_memory_free(ssk) || 1554c5b4297dSGeliang Tang !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) { 1555c5b4297dSGeliang Tang err = copied; 1556c5b4297dSGeliang Tang goto out; 1557c5b4297dSGeliang Tang } 1558c5b4297dSGeliang Tang mptcp_set_timeout(sk); 1559c5b4297dSGeliang Tang } 1560c5b4297dSGeliang Tang err = copied; 1561c5b4297dSGeliang Tang 1562c5b4297dSGeliang Tang out: 1563c5b4297dSGeliang Tang return err; 1564c5b4297dSGeliang Tang } 1565c5b4297dSGeliang Tang 1566ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1567d9ca1de8SPaolo Abeni { 1568d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1569d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1570d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1571d9ca1de8SPaolo Abeni .flags = flags, 1572d9ca1de8SPaolo Abeni }; 15730522b424SGeliang Tang bool do_check_data_fin = false; 15740fa1b378SGeliang Tang int push_count = 1; 1575d9ca1de8SPaolo Abeni 15760fa1b378SGeliang Tang while (mptcp_send_head(sk) && (push_count > 0)) { 15770fa1b378SGeliang Tang struct mptcp_subflow_context *subflow; 1578d9ca1de8SPaolo Abeni int ret = 0; 1579d9ca1de8SPaolo Abeni 15800fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 15810fa1b378SGeliang Tang break; 1582d9ca1de8SPaolo Abeni 15830fa1b378SGeliang Tang push_count = 0; 15840fa1b378SGeliang Tang 15850fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 15860fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 15870fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 15880fa1b378SGeliang Tang 15890fa1b378SGeliang Tang prev_ssk = ssk; 15900fa1b378SGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 15910fa1b378SGeliang Tang if (ssk != prev_ssk) { 15929758f40eSPaolo Abeni /* First check. If the ssk has changed since 15939758f40eSPaolo Abeni * the last round, release prev_ssk 1594d9ca1de8SPaolo Abeni */ 15950fa1b378SGeliang Tang if (prev_ssk) 1596b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1597d9ca1de8SPaolo Abeni 15989758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15999758f40eSPaolo Abeni * from the previous one, otherwise we are still 16009758f40eSPaolo Abeni * helding the relevant lock 16019758f40eSPaolo Abeni */ 1602d9ca1de8SPaolo Abeni lock_sock(ssk); 16030fa1b378SGeliang Tang } 16040fa1b378SGeliang Tang 16050fa1b378SGeliang Tang push_count++; 1606d9ca1de8SPaolo Abeni 1607c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1608d9ca1de8SPaolo Abeni if (ret <= 0) { 16090fa1b378SGeliang Tang if (ret != -EAGAIN || 16100fa1b378SGeliang Tang (1 << ssk->sk_state) & 16110fa1b378SGeliang Tang (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE)) 16120fa1b378SGeliang Tang push_count--; 1613c886d702SPaolo Abeni continue; 1614d9ca1de8SPaolo Abeni } 16150522b424SGeliang Tang do_check_data_fin = true; 1616d9ca1de8SPaolo Abeni } 16170fa1b378SGeliang Tang } 16180fa1b378SGeliang Tang } 1619d9ca1de8SPaolo Abeni 1620d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1621d9ca1de8SPaolo Abeni if (ssk) 1622b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1623d9ca1de8SPaolo Abeni 162433d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1625d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1626d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 16270522b424SGeliang Tang if (do_check_data_fin) 162881c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 1629d9ca1de8SPaolo Abeni } 1630d9ca1de8SPaolo Abeni 163173a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 16326e628cd3SPaolo Abeni { 16336e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16341094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16351094c6feSPaolo Abeni .data_lock_held = true, 16361094c6feSPaolo Abeni }; 16370fa1b378SGeliang Tang bool keep_pushing = true; 1638b19bc294SPaolo Abeni struct sock *xmit_ssk; 1639c5b4297dSGeliang Tang int copied = 0; 16406e628cd3SPaolo Abeni 16416e628cd3SPaolo Abeni info.flags = 0; 16420fa1b378SGeliang Tang while (mptcp_send_head(sk) && keep_pushing) { 16430fa1b378SGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 16446e628cd3SPaolo Abeni int ret = 0; 16456e628cd3SPaolo Abeni 164673a0052aSGeliang Tang /* check for a different subflow usage only after 1647b19bc294SPaolo Abeni * spooling the first chunk of data 1648b19bc294SPaolo Abeni */ 16490fa1b378SGeliang Tang if (first) { 16500fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 1651c5b4297dSGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 1652b19bc294SPaolo Abeni first = false; 1653c5b4297dSGeliang Tang if (ret <= 0) 1654c5b4297dSGeliang Tang break; 1655c5b4297dSGeliang Tang copied += ret; 16560fa1b378SGeliang Tang continue; 16570fa1b378SGeliang Tang } 16580fa1b378SGeliang Tang 16590fa1b378SGeliang Tang if (mptcp_sched_get_send(msk)) 16600fa1b378SGeliang Tang goto out; 16610fa1b378SGeliang Tang 16620fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16630fa1b378SGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 16640fa1b378SGeliang Tang ret = __subflow_push_pending(sk, ssk, &info); 16650fa1b378SGeliang Tang if (ret <= 0) 16660fa1b378SGeliang Tang keep_pushing = false; 16670fa1b378SGeliang Tang copied += ret; 16680fa1b378SGeliang Tang } 16690fa1b378SGeliang Tang 16700fa1b378SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 16710fa1b378SGeliang Tang if (READ_ONCE(subflow->scheduled)) { 16720fa1b378SGeliang Tang xmit_ssk = mptcp_subflow_tcp_sock(subflow); 16730fa1b378SGeliang Tang if (xmit_ssk != ssk) { 16740fa1b378SGeliang Tang mptcp_subflow_delegate(subflow, 16750fa1b378SGeliang Tang MPTCP_DELEGATE_SEND); 16760fa1b378SGeliang Tang keep_pushing = false; 16770fa1b378SGeliang Tang } 16780fa1b378SGeliang Tang } 16790fa1b378SGeliang Tang } 16806e628cd3SPaolo Abeni } 16816e628cd3SPaolo Abeni 16826e628cd3SPaolo Abeni out: 16836e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16846e628cd3SPaolo Abeni * not going to flush it via release_sock() 16856e628cd3SPaolo Abeni */ 16866e628cd3SPaolo Abeni if (copied) { 16876e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16886e628cd3SPaolo Abeni info.size_goal); 1689d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1690d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1691d09d818eSPaolo Abeni 16926e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16936e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16946e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16956e628cd3SPaolo Abeni } 16966e628cd3SPaolo Abeni } 16976e628cd3SPaolo Abeni 16985cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16995cf92bbaSPaolo Abeni { 17005cf92bbaSPaolo Abeni /* enable autotune */ 17015cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 17025cf92bbaSPaolo Abeni 17035cf92bbaSPaolo Abeni /* will be cleared on avail space */ 17045cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 17055cf92bbaSPaolo Abeni } 17065cf92bbaSPaolo Abeni 17077d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags); 17087d803344SPaolo Abeni 1709a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1710fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1711fa9e5746SPaolo Abeni { 1712fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1713fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1714a2702a07SPaolo Abeni struct sock *ssk; 1715fa9e5746SPaolo Abeni int ret; 1716fa9e5746SPaolo Abeni 1717a2702a07SPaolo Abeni /* on flags based fastopen the mptcp is supposed to create the 1718a2702a07SPaolo Abeni * first subflow right now. Otherwise we are in the defer_connect 1719a2702a07SPaolo Abeni * path, and the first subflow must be already present. 1720a2702a07SPaolo Abeni * Since the defer_connect flag is cleared after the first succsful 1721a2702a07SPaolo Abeni * fastopen attempt, no need to check for additional subflow status. 1722a2702a07SPaolo Abeni */ 1723ddb1a072SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) { 17243f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 17253f326a82SPaolo Abeni if (IS_ERR(ssk)) 17263f326a82SPaolo Abeni return PTR_ERR(ssk); 1727ddb1a072SPaolo Abeni } 1728a2702a07SPaolo Abeni if (!msk->first) 1729a2702a07SPaolo Abeni return -EINVAL; 1730a2702a07SPaolo Abeni 1731a2702a07SPaolo Abeni ssk = msk->first; 1732a2702a07SPaolo Abeni 1733fa9e5746SPaolo Abeni lock_sock(ssk); 1734fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 17357d803344SPaolo Abeni msk->fastopening = 1; 1736fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 17377d803344SPaolo Abeni msk->fastopening = 0; 1738fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1739fa9e5746SPaolo Abeni release_sock(ssk); 1740fa9e5746SPaolo Abeni 1741fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1742fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1743fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1744fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1745fa9e5746SPaolo Abeni 1746fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1747fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1748fa9e5746SPaolo Abeni */ 1749fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1750fa9e5746SPaolo Abeni *copied_syn = 0; 17517d803344SPaolo Abeni } else if (ret && ret != -EINPROGRESS) { 1752c2b2ae39SPaolo Abeni /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1753c2b2ae39SPaolo Abeni * __inet_stream_connect() can fail, due to looking check, 1754c2b2ae39SPaolo Abeni * see mptcp_disconnect(). 1755c2b2ae39SPaolo Abeni * Attempt it again outside the problematic scope. 1756c2b2ae39SPaolo Abeni */ 1757c2b2ae39SPaolo Abeni if (!mptcp_disconnect(sk, 0)) 1758c2b2ae39SPaolo Abeni sk->sk_socket->state = SS_UNCONNECTED; 1759fa9e5746SPaolo Abeni } 176008e39c0dSEric Dumazet inet_clear_bit(DEFER_CONNECT, sk); 1761fa9e5746SPaolo Abeni 1762fa9e5746SPaolo Abeni return ret; 1763fa9e5746SPaolo Abeni } 1764fa9e5746SPaolo Abeni 1765f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1766f870fa0bSMat Martineau { 1767f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 176817091708SFlorian Westphal struct page_frag *pfrag; 17696d0060f6SMat Martineau size_t copied = 0; 1770caf971dfSPaolo Abeni int ret = 0; 17716d0060f6SMat Martineau long timeo; 1772f870fa0bSMat Martineau 1773987858e5SPaolo Abeni /* silently ignore everything else */ 17741e777f39SDmytro Shytyi msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1775987858e5SPaolo Abeni 17766511882cSPaolo Abeni lock_sock(sk); 17771954b860SMat Martineau 177808e39c0dSEric Dumazet if (unlikely(inet_test_bit(DEFER_CONNECT, sk) || 177908e39c0dSEric Dumazet msg->msg_flags & MSG_FASTOPEN)) { 1780d98a82a6SDmytro Shytyi int copied_syn = 0; 1781d98a82a6SDmytro Shytyi 1782a2702a07SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1783d98a82a6SDmytro Shytyi copied += copied_syn; 1784fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1785d98a82a6SDmytro Shytyi goto out; 1786fa9e5746SPaolo Abeni else if (ret) 178769800e51SPaolo Abeni goto do_error; 1788d98a82a6SDmytro Shytyi } 1789d98a82a6SDmytro Shytyi 17901954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17911954b860SMat Martineau 17921954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17931954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17941954b860SMat Martineau if (ret) 179569800e51SPaolo Abeni goto do_error; 17961954b860SMat Martineau } 17971954b860SMat Martineau 179869800e51SPaolo Abeni ret = -EPIPE; 179969800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 180069800e51SPaolo Abeni goto do_error; 180169800e51SPaolo Abeni 180217091708SFlorian Westphal pfrag = sk_page_frag(sk); 180318b683bfSPaolo Abeni 1804d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1805724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1806d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1807d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1808d9ca1de8SPaolo Abeni size_t psize, offset; 1809d9ca1de8SPaolo Abeni 1810d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1811d9ca1de8SPaolo Abeni * page allocator 1812fb529e62SFlorian Westphal */ 1813d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1814d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1815d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1816d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1817d9ca1de8SPaolo Abeni goto wait_for_memory; 18186e628cd3SPaolo Abeni 1819d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1820d9ca1de8SPaolo Abeni goto wait_for_memory; 1821d9ca1de8SPaolo Abeni 1822d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1823d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1824fb529e62SFlorian Westphal } 1825fb529e62SFlorian Westphal 1826d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1827d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1828d9ca1de8SPaolo Abeni * anyway 1829d9ca1de8SPaolo Abeni */ 1830d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1831d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1832d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1833724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 18348ce568edSPaolo Abeni 18356511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1836d9ca1de8SPaolo Abeni goto wait_for_memory; 1837d9ca1de8SPaolo Abeni 1838d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1839d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1840d9ca1de8SPaolo Abeni ret = -EFAULT; 184169800e51SPaolo Abeni goto do_error; 1842d9ca1de8SPaolo Abeni } 1843d9ca1de8SPaolo Abeni 1844d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 18455e6300e7SEric Dumazet sk_forward_alloc_add(sk, -total_ts); 1846d9ca1de8SPaolo Abeni copied += psize; 1847d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1848d9ca1de8SPaolo Abeni frag_truesize += psize; 1849d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1850d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1851d9ca1de8SPaolo Abeni 1852d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1853d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1854d9ca1de8SPaolo Abeni */ 1855d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1856d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1857d9ca1de8SPaolo Abeni get_page(dfrag->page); 1858d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1859d9ca1de8SPaolo Abeni if (!msk->first_pending) 1860d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1861d9ca1de8SPaolo Abeni } 1862e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1863d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1864d9ca1de8SPaolo Abeni !dfrag_collapsed); 1865d9ca1de8SPaolo Abeni 1866d9ca1de8SPaolo Abeni continue; 1867d9ca1de8SPaolo Abeni 1868d9ca1de8SPaolo Abeni wait_for_memory: 18695cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1870c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1871f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1872f296234cSPeter Krystad if (ret) 187369800e51SPaolo Abeni goto do_error; 1874cec37a6eSPeter Krystad } 1875cec37a6eSPeter Krystad 187613e16037SPaolo Abeni if (copied) 1877c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1878da51aef5SPaolo Abeni 18791954b860SMat Martineau out: 1880cec37a6eSPeter Krystad release_sock(sk); 188169800e51SPaolo Abeni return copied; 188269800e51SPaolo Abeni 188369800e51SPaolo Abeni do_error: 188469800e51SPaolo Abeni if (copied) 188569800e51SPaolo Abeni goto out; 188669800e51SPaolo Abeni 188769800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 188869800e51SPaolo Abeni goto out; 1889f870fa0bSMat Martineau } 1890f870fa0bSMat Martineau 18916771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18926771bfd9SFlorian Westphal struct msghdr *msg, 1893b7f653b2SFlorian Westphal size_t len, int flags, 1894b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1895b7f653b2SFlorian Westphal int *cmsg_flags) 18966771bfd9SFlorian Westphal { 1897ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18986771bfd9SFlorian Westphal int copied = 0; 18996771bfd9SFlorian Westphal 1900ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 19016771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 19026771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 19036771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 19046771bfd9SFlorian Westphal int err; 19056771bfd9SFlorian Westphal 1906d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 19076771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 19086771bfd9SFlorian Westphal if (unlikely(err < 0)) { 19096771bfd9SFlorian Westphal if (!copied) 19106771bfd9SFlorian Westphal return err; 19116771bfd9SFlorian Westphal break; 19126771bfd9SFlorian Westphal } 1913d976092cSPaolo Abeni } 19146771bfd9SFlorian Westphal 1915b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1916b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1917b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1918b7f653b2SFlorian Westphal } 1919b7f653b2SFlorian Westphal 19206771bfd9SFlorian Westphal copied += count; 19216771bfd9SFlorian Westphal 19226771bfd9SFlorian Westphal if (count < data_len) { 19232c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 19246771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 19252c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 19262c9e7765SFlorian Westphal } 19276771bfd9SFlorian Westphal break; 19286771bfd9SFlorian Westphal } 19296771bfd9SFlorian Westphal 1930ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 193187952603SPaolo Abeni /* we will bulk release the skb memory later */ 193287952603SPaolo Abeni skb->destructor = NULL; 1933ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 193487952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 19356771bfd9SFlorian Westphal __kfree_skb(skb); 1936ca4fb892SYonglong Li } 19376771bfd9SFlorian Westphal 19386771bfd9SFlorian Westphal if (copied >= len) 19396771bfd9SFlorian Westphal break; 19406771bfd9SFlorian Westphal } 19416771bfd9SFlorian Westphal 19426771bfd9SFlorian Westphal return copied; 19436771bfd9SFlorian Westphal } 19446771bfd9SFlorian Westphal 1945a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1946a6b118feSFlorian Westphal * 1947a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1948a6b118feSFlorian Westphal */ 1949a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1950a6b118feSFlorian Westphal { 1951a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1952a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1953b8dc6d6cSPaolo Abeni u8 scaling_ratio = U8_MAX; 1954a6b118feSFlorian Westphal u32 time, advmss = 1; 1955a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1956a6b118feSFlorian Westphal 1957109cdeb8SGeliang Tang msk_owned_by_me(msk); 1958a6b118feSFlorian Westphal 1959a6b118feSFlorian Westphal if (copied <= 0) 1960a6b118feSFlorian Westphal return; 1961a6b118feSFlorian Westphal 1962a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1963a6b118feSFlorian Westphal 1964a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1965a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1966a6b118feSFlorian Westphal 1967a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1968a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1969a6b118feSFlorian Westphal return; 1970a6b118feSFlorian Westphal 1971a6b118feSFlorian Westphal rtt_us = 0; 1972a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1973a6b118feSFlorian Westphal const struct tcp_sock *tp; 1974a6b118feSFlorian Westphal u64 sf_rtt_us; 1975a6b118feSFlorian Westphal u32 sf_advmss; 1976a6b118feSFlorian Westphal 1977a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1978a6b118feSFlorian Westphal 1979a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1980a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1981a6b118feSFlorian Westphal 1982a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1983a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1984b8dc6d6cSPaolo Abeni scaling_ratio = min(tp->scaling_ratio, scaling_ratio); 1985a6b118feSFlorian Westphal } 1986a6b118feSFlorian Westphal 1987a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1988b8dc6d6cSPaolo Abeni msk->scaling_ratio = scaling_ratio; 1989a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1990a6b118feSFlorian Westphal return; 1991a6b118feSFlorian Westphal 1992a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1993a6b118feSFlorian Westphal goto new_measure; 1994a6b118feSFlorian Westphal 199578047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1996a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1997a6b118feSFlorian Westphal u64 rcvwin, grow; 1998b8dc6d6cSPaolo Abeni int rcvbuf; 1999a6b118feSFlorian Westphal 2000a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 2001a6b118feSFlorian Westphal 2002a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 2003a6b118feSFlorian Westphal 2004a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 2005a6b118feSFlorian Westphal rcvwin += (grow << 1); 2006a6b118feSFlorian Westphal 2007b8dc6d6cSPaolo Abeni rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin), 200802739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 2009a6b118feSFlorian Westphal 2010a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 2011a6b118feSFlorian Westphal u32 window_clamp; 2012a6b118feSFlorian Westphal 2013b8dc6d6cSPaolo Abeni window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf); 2014a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 2015a6b118feSFlorian Westphal 2016a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 2017a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 2018a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 2019a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 2020a6b118feSFlorian Westphal */ 2021a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 2022a6b118feSFlorian Westphal struct sock *ssk; 2023c76c6956SPaolo Abeni bool slow; 2024a6b118feSFlorian Westphal 2025a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 2026c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 2027a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 2028a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 2029c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 2030c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 2031a6b118feSFlorian Westphal } 2032a6b118feSFlorian Westphal } 2033a6b118feSFlorian Westphal } 2034a6b118feSFlorian Westphal 2035a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 2036a6b118feSFlorian Westphal new_measure: 2037a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2038a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 2039a6b118feSFlorian Westphal } 2040a6b118feSFlorian Westphal 204187952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 204287952603SPaolo Abeni { 204387952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 204487952603SPaolo Abeni 204587952603SPaolo Abeni if (!msk->rmem_released) 204687952603SPaolo Abeni return; 204787952603SPaolo Abeni 204887952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 20496511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 2050ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 205187952603SPaolo Abeni } 205287952603SPaolo Abeni 205387952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 205487952603SPaolo Abeni { 205587952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 205687952603SPaolo Abeni 205787952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 205887952603SPaolo Abeni } 205987952603SPaolo Abeni 2060e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20616771bfd9SFlorian Westphal { 206287952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20636771bfd9SFlorian Westphal unsigned int moved = 0; 206487952603SPaolo Abeni bool ret, done; 2065d5f49190SPaolo Abeni 20666771bfd9SFlorian Westphal do { 20676771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 206865f49fe7SFlorian Westphal bool slowpath; 20696771bfd9SFlorian Westphal 207087952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 207187952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 207287952603SPaolo Abeni * that is an unlikely slow path. 207387952603SPaolo Abeni */ 207487952603SPaolo Abeni if (likely(!ssk)) 20756771bfd9SFlorian Westphal break; 20766771bfd9SFlorian Westphal 207765f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 207887952603SPaolo Abeni mptcp_data_lock(sk); 2079e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20806771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 208187952603SPaolo Abeni mptcp_data_unlock(sk); 2082499ada50SPaolo Abeni 2083499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2084499ada50SPaolo Abeni __mptcp_error_report(sk); 208565f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20866771bfd9SFlorian Westphal } while (!done); 20876771bfd9SFlorian Westphal 208887952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 208987952603SPaolo Abeni ret = moved > 0; 209087952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 209187952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 209287952603SPaolo Abeni mptcp_data_lock(sk); 209387952603SPaolo Abeni __mptcp_update_rmem(sk); 209487952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 209587952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 209687952603SPaolo Abeni mptcp_data_unlock(sk); 2097ab174ad8SPaolo Abeni } 209887952603SPaolo Abeni if (ret) 209987952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 210087952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 21016771bfd9SFlorian Westphal } 21026771bfd9SFlorian Westphal 21032c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 21042c9e7765SFlorian Westphal { 21052c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 21062c9e7765SFlorian Westphal const struct sk_buff *skb; 21072c9e7765SFlorian Westphal 21082c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 21092c9e7765SFlorian Westphal if (skb) { 21102c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 21112c9e7765SFlorian Westphal 21122c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 21132c9e7765SFlorian Westphal return INT_MAX; 21142c9e7765SFlorian Westphal 21152c9e7765SFlorian Westphal return (unsigned int)hint_val; 21162c9e7765SFlorian Westphal } 21172c9e7765SFlorian Westphal 21182c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 21192c9e7765SFlorian Westphal return 1; 21202c9e7765SFlorian Westphal 21212c9e7765SFlorian Westphal return 0; 21222c9e7765SFlorian Westphal } 21232c9e7765SFlorian Westphal 2124f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2125ec095263SOliver Hartkopp int flags, int *addr_len) 2126f870fa0bSMat Martineau { 2127f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2128b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2129b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 21307a6a6cbcSPaolo Abeni int target; 21317a6a6cbcSPaolo Abeni long timeo; 2132f870fa0bSMat Martineau 2133cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2134cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2135cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2136cb9d80f4SPaolo Abeni 21376511882cSPaolo Abeni lock_sock(sk); 2138fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2139fd897679SPaolo Abeni copied = -ENOTCONN; 2140fd897679SPaolo Abeni goto out_err; 2141fd897679SPaolo Abeni } 2142fd897679SPaolo Abeni 2143ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 21447a6a6cbcSPaolo Abeni 21457a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 21467a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 21477a6a6cbcSPaolo Abeni 21482c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 21492c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 21502c9e7765SFlorian Westphal 215105e3eceaSEric Dumazet while (copied < len) { 2152e3859603SPaolo Abeni int bytes_read; 21537a6a6cbcSPaolo Abeni 2154b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21556771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21567a6a6cbcSPaolo Abeni if (!copied) 21577a6a6cbcSPaolo Abeni copied = bytes_read; 21586771bfd9SFlorian Westphal goto out_err; 2159cec37a6eSPeter Krystad } 2160cec37a6eSPeter Krystad 21617a6a6cbcSPaolo Abeni copied += bytes_read; 21627a6a6cbcSPaolo Abeni 2163ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2164fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2165ea4ca586SPaolo Abeni 2166e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2167e3859603SPaolo Abeni continue; 2168e3859603SPaolo Abeni 21697a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21707a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21717a6a6cbcSPaolo Abeni */ 21727a6a6cbcSPaolo Abeni if (copied >= target) 21737a6a6cbcSPaolo Abeni break; 21747a6a6cbcSPaolo Abeni 21757a6a6cbcSPaolo Abeni if (copied) { 21767a6a6cbcSPaolo Abeni if (sk->sk_err || 21777a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21787a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21797a6a6cbcSPaolo Abeni !timeo || 21807a6a6cbcSPaolo Abeni signal_pending(current)) 21817a6a6cbcSPaolo Abeni break; 21827a6a6cbcSPaolo Abeni } else { 21837a6a6cbcSPaolo Abeni if (sk->sk_err) { 21847a6a6cbcSPaolo Abeni copied = sock_error(sk); 21857a6a6cbcSPaolo Abeni break; 21867a6a6cbcSPaolo Abeni } 21877a6a6cbcSPaolo Abeni 218887952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 218987952603SPaolo Abeni /* race breaker: the shutdown could be after the 219087952603SPaolo Abeni * previous receive queue check 219187952603SPaolo Abeni */ 2192e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 219387952603SPaolo Abeni continue; 21947a6a6cbcSPaolo Abeni break; 219587952603SPaolo Abeni } 21967a6a6cbcSPaolo Abeni 21977a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21987a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21997a6a6cbcSPaolo Abeni break; 22007a6a6cbcSPaolo Abeni } 22017a6a6cbcSPaolo Abeni 22027a6a6cbcSPaolo Abeni if (!timeo) { 22037a6a6cbcSPaolo Abeni copied = -EAGAIN; 22047a6a6cbcSPaolo Abeni break; 22057a6a6cbcSPaolo Abeni } 22067a6a6cbcSPaolo Abeni 22077a6a6cbcSPaolo Abeni if (signal_pending(current)) { 22087a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 22097a6a6cbcSPaolo Abeni break; 22107a6a6cbcSPaolo Abeni } 22117a6a6cbcSPaolo Abeni } 22127a6a6cbcSPaolo Abeni 22137a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2214612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 22157a6a6cbcSPaolo Abeni } 22163c90e377SPaolo Abeni 22176771bfd9SFlorian Westphal out_err: 2218b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2219b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2220b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 22212c9e7765SFlorian Westphal 22222c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 22232c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 22242c9e7765SFlorian Westphal 22252c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 22262c9e7765SFlorian Westphal } 2227b7f653b2SFlorian Westphal } 2228b7f653b2SFlorian Westphal 2229612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2230612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2231612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2232ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2233a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2234a6b118feSFlorian Westphal 2235cec37a6eSPeter Krystad release_sock(sk); 2236cec37a6eSPeter Krystad return copied; 2237cec37a6eSPeter Krystad } 2238cec37a6eSPeter Krystad 2239b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2240b51f9b80SPaolo Abeni { 2241b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2242b51f9b80SPaolo Abeni icsk_retransmit_timer); 2243b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 22442d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2245b51f9b80SPaolo Abeni 2246b51f9b80SPaolo Abeni bh_lock_sock(sk); 2247b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22482d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22492d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22502d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2251b51f9b80SPaolo Abeni } else { 2252b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2253e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2254b51f9b80SPaolo Abeni } 2255b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2256b51f9b80SPaolo Abeni sock_put(sk); 2257b51f9b80SPaolo Abeni } 2258b51f9b80SPaolo Abeni 2259e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2260e16163b6SPaolo Abeni { 2261e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2262e16163b6SPaolo Abeni 2263e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2264b6d69fc8SFlorian Westphal sock_put(sk); 2265e16163b6SPaolo Abeni } 2266e16163b6SPaolo Abeni 22673b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22683b1d6210SPaolo Abeni * level. 22693b1d6210SPaolo Abeni * 22703b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22713b1d6210SPaolo Abeni */ 227207336a87SGeliang Tang struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22733b1d6210SPaolo Abeni { 227471b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22753b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 227671b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22773b1d6210SPaolo Abeni 22783b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22793b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22803b1d6210SPaolo Abeni 2281ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2282d5f49190SPaolo Abeni continue; 2283d5f49190SPaolo Abeni 228471b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 228571b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 228671b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 228771b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2288860975c6SFlorian Westphal continue; 2289860975c6SFlorian Westphal } 22903b1d6210SPaolo Abeni 22913b1d6210SPaolo Abeni if (subflow->backup) { 22923b1d6210SPaolo Abeni if (!backup) 22933b1d6210SPaolo Abeni backup = ssk; 22943b1d6210SPaolo Abeni continue; 22953b1d6210SPaolo Abeni } 22963b1d6210SPaolo Abeni 229771b7dec2SPaolo Abeni if (!pick) 229871b7dec2SPaolo Abeni pick = ssk; 22993b1d6210SPaolo Abeni } 23003b1d6210SPaolo Abeni 230171b7dec2SPaolo Abeni if (pick) 230271b7dec2SPaolo Abeni return pick; 230371b7dec2SPaolo Abeni 230471b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 230571b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 23063b1d6210SPaolo Abeni } 23073b1d6210SPaolo Abeni 23081e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 23091e1d9d6fSPaolo Abeni { 23101e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 23111e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 23121e1d9d6fSPaolo Abeni 231300df24f1SGeliang Tang if (__mptcp_check_fallback(msk)) 23141e1d9d6fSPaolo Abeni return false; 23151e1d9d6fSPaolo Abeni 23161e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 23171e1d9d6fSPaolo Abeni return false; 23181e1d9d6fSPaolo Abeni 23191e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 23201e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 23211e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 23221e1d9d6fSPaolo Abeni */ 23231e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 23241e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 23251e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 23261e1d9d6fSPaolo Abeni if (!rtx_head) { 23271e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23281e1d9d6fSPaolo Abeni return false; 23291e1d9d6fSPaolo Abeni } 23301e1d9d6fSPaolo Abeni 23311e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 23321e1d9d6fSPaolo Abeni msk->recovery = true; 23331e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 23341e1d9d6fSPaolo Abeni 23351e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23361e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23371e1d9d6fSPaolo Abeni 23381e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23391e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23401e1d9d6fSPaolo Abeni if (!cur->already_sent) 23411e1d9d6fSPaolo Abeni break; 23421e1d9d6fSPaolo Abeni cur->already_sent = 0; 23431e1d9d6fSPaolo Abeni } 23441e1d9d6fSPaolo Abeni 23451e1d9d6fSPaolo Abeni return true; 23461e1d9d6fSPaolo Abeni } 23471e1d9d6fSPaolo Abeni 2348b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2349b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2350b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2351b29fcfb5SPaolo Abeni 2352cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2353cec37a6eSPeter Krystad * (accept). 2354cec37a6eSPeter Krystad * 2355cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2356cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2357cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2358cec37a6eSPeter Krystad * parent socket. 2359cec37a6eSPeter Krystad */ 2360a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2361b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2362b29fcfb5SPaolo Abeni unsigned int flags) 2363cec37a6eSPeter Krystad { 2364e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 236563740448SPaolo Abeni bool dispose_it, need_push = false; 236663740448SPaolo Abeni 236763740448SPaolo Abeni /* If the first subflow moved to a close state before accept, e.g. due 236863740448SPaolo Abeni * to an incoming reset, mptcp either: 236963740448SPaolo Abeni * - if either the subflow or the msk are dead, destroy the context 237063740448SPaolo Abeni * (the subflow socket is deleted by inet_child_forget) and the msk 237163740448SPaolo Abeni * - otherwise do nothing at the moment and take action at accept and/or 237263740448SPaolo Abeni * listener shutdown - user-space must be able to accept() the closed 237363740448SPaolo Abeni * socket. 237463740448SPaolo Abeni */ 237563740448SPaolo Abeni if (msk->in_accept_queue && msk->first == ssk) { 237663740448SPaolo Abeni if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 237763740448SPaolo Abeni return; 237863740448SPaolo Abeni 237963740448SPaolo Abeni /* ensure later check in mptcp_worker() will dispose the msk */ 238063740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 238163740448SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 238263740448SPaolo Abeni mptcp_subflow_drop_ctx(ssk); 238363740448SPaolo Abeni goto out_release; 238463740448SPaolo Abeni } 2385e0be4931SFlorian Westphal 238639880bd8SPaolo Abeni dispose_it = msk->free_first || ssk != msk->first; 2387b29fcfb5SPaolo Abeni if (dispose_it) 2388cec37a6eSPeter Krystad list_del(&subflow->node); 2389cec37a6eSPeter Krystad 23903f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2391e16163b6SPaolo Abeni 2392ff18f9efSPaolo Abeni if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) { 2393d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2394d21f8348SPaolo Abeni * to generate the egress reset 2395d21f8348SPaolo Abeni */ 2396d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2397d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2398b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2399d21f8348SPaolo Abeni } 2400b29fcfb5SPaolo Abeni 2401b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2402b29fcfb5SPaolo Abeni if (!dispose_it) { 2403c2b2ae39SPaolo Abeni /* The MPTCP code never wait on the subflow sockets, TCP-level 2404c2b2ae39SPaolo Abeni * disconnect should never fail 2405c2b2ae39SPaolo Abeni */ 2406c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 2407b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2408b29fcfb5SPaolo Abeni release_sock(ssk); 2409b29fcfb5SPaolo Abeni 2410b29fcfb5SPaolo Abeni goto out; 2411b29fcfb5SPaolo Abeni } 2412b29fcfb5SPaolo Abeni 2413d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2414d7b1bfd0SPaolo Abeni 2415e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2416e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2417e16163b6SPaolo Abeni * reference owned by msk; 2418e16163b6SPaolo Abeni */ 2419e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2420b6985b9bSPaolo Abeni WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2421e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2422e16163b6SPaolo Abeni } else { 2423d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2424e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2425e16163b6SPaolo Abeni 2426e16163b6SPaolo Abeni /* close acquired an extra ref */ 2427e16163b6SPaolo Abeni __sock_put(ssk); 2428e16163b6SPaolo Abeni } 242963740448SPaolo Abeni 243063740448SPaolo Abeni out_release: 2431e16163b6SPaolo Abeni release_sock(ssk); 2432e16163b6SPaolo Abeni 2433e16163b6SPaolo Abeni sock_put(ssk); 2434e0be4931SFlorian Westphal 2435c8fe62f0SFlorian Westphal if (ssk == msk->first) 24361b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2437c8fe62f0SFlorian Westphal 2438b29fcfb5SPaolo Abeni out: 24391e1d9d6fSPaolo Abeni if (need_push) 24401e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2441f870fa0bSMat Martineau } 2442f870fa0bSMat Martineau 2443a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2444a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2445a141e02eSFlorian Westphal { 2446b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2447b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2448a88c9e49SPaolo Abeni 2449a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2450a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2451a88c9e49SPaolo Abeni */ 2452a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2453a88c9e49SPaolo Abeni 2454b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2455a141e02eSFlorian Westphal } 2456a141e02eSFlorian Westphal 2457dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2458dc24f8b4SPaolo Abeni { 2459dc24f8b4SPaolo Abeni return 0; 2460dc24f8b4SPaolo Abeni } 2461dc24f8b4SPaolo Abeni 2462b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk) 24630e4f35d7SPaolo Abeni { 24640e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2465b6985b9bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 24660e4f35d7SPaolo Abeni 24673abc05d9SFlorian Westphal might_sleep(); 24683abc05d9SFlorian Westphal 24695efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24700e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24710e4f35d7SPaolo Abeni 24720e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24730e4f35d7SPaolo Abeni continue; 24740e4f35d7SPaolo Abeni 247540947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 247640947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 247740947e13SFlorian Westphal continue; 247840947e13SFlorian Westphal 2479b6985b9bSPaolo Abeni mptcp_close_ssk(sk, ssk, subflow); 2480b6985b9bSPaolo Abeni } 2481b6985b9bSPaolo Abeni 24820e4f35d7SPaolo Abeni } 24830e4f35d7SPaolo Abeni 248463740448SPaolo Abeni static bool mptcp_should_close(const struct sock *sk) 2485e16163b6SPaolo Abeni { 2486e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2487e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2488e16163b6SPaolo Abeni 248963740448SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2490e16163b6SPaolo Abeni return true; 2491e16163b6SPaolo Abeni 2492e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2493e16163b6SPaolo Abeni * timeout 2494e16163b6SPaolo Abeni */ 2495e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2496e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2497e16163b6SPaolo Abeni TCP_CLOSE) 2498e16163b6SPaolo Abeni return false; 2499e16163b6SPaolo Abeni } 2500e16163b6SPaolo Abeni return true; 2501e16163b6SPaolo Abeni } 2502e16163b6SPaolo Abeni 250350c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 250450c504a2SFlorian Westphal { 250550c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 250680638684SGeliang Tang struct sock *sk = (struct sock *)msk; 250750c504a2SFlorian Westphal 250850c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 250950c504a2SFlorian Westphal return; 251050c504a2SFlorian Westphal 251150c504a2SFlorian Westphal mptcp_token_destroy(msk); 251250c504a2SFlorian Westphal 25135efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 251450c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 251575e908c3SPaolo Abeni bool slow; 251650c504a2SFlorian Westphal 251775e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 251850c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 251950c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 252050c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 252150c504a2SFlorian Westphal } 252275e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 252350c504a2SFlorian Westphal } 252450c504a2SFlorian Westphal 252569800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 252669800e51SPaolo Abeni switch (sk->sk_state) { 252769800e51SPaolo Abeni case TCP_SYN_SENT: 25289ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNREFUSED); 252969800e51SPaolo Abeni break; 253069800e51SPaolo Abeni case TCP_CLOSE_WAIT: 25319ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, EPIPE); 253269800e51SPaolo Abeni break; 253369800e51SPaolo Abeni case TCP_CLOSE: 253469800e51SPaolo Abeni return; 253569800e51SPaolo Abeni default: 25369ae8e5adSEric Dumazet WRITE_ONCE(sk->sk_err, ECONNRESET); 253769800e51SPaolo Abeni } 253869800e51SPaolo Abeni 253950c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 25406b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 254150c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 254250c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 254350c504a2SFlorian Westphal 254469800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 254569800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 254669800e51SPaolo Abeni return; 254769800e51SPaolo Abeni 254869800e51SPaolo Abeni sk->sk_state_change(sk); 254969800e51SPaolo Abeni sk_error_report(sk); 255050c504a2SFlorian Westphal } 255150c504a2SFlorian Westphal 25522948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 255380992017SPaolo Abeni { 25542948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2555ee2708aeSGeliang Tang struct mptcp_subflow_context *subflow; 2556caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25573b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25582948d0a1SPaolo Abeni struct sock *ssk; 2559ee2708aeSGeliang Tang int ret, err; 2560ee2708aeSGeliang Tang u16 len = 0; 25612948d0a1SPaolo Abeni 2562b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25633241a9c0SFlorian Westphal 25643241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 2565ee2708aeSGeliang Tang err = mptcp_sched_get_retrans(msk); 25662948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25676477dd39SMat Martineau if (!dfrag) { 25686477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25696477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25706477dd39SMat Martineau 25716477dd39SMat Martineau icsk->icsk_retransmits++; 25726477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25736477dd39SMat Martineau mptcp_send_ack(msk); 25746477dd39SMat Martineau 25756477dd39SMat Martineau goto reset_timer; 25766477dd39SMat Martineau } 25776477dd39SMat Martineau 25783241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25792948d0a1SPaolo Abeni return; 25803241a9c0SFlorian Westphal 25813241a9c0SFlorian Westphal goto reset_timer; 25826477dd39SMat Martineau } 25832948d0a1SPaolo Abeni 2584ee2708aeSGeliang Tang if (err) 25852948d0a1SPaolo Abeni goto reset_timer; 25862948d0a1SPaolo Abeni 2587ee2708aeSGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2588ee2708aeSGeliang Tang if (READ_ONCE(subflow->scheduled)) { 2589ee2708aeSGeliang Tang u16 copied = 0; 2590ee2708aeSGeliang Tang 2591ee2708aeSGeliang Tang mptcp_subflow_set_scheduled(subflow, false); 2592ee2708aeSGeliang Tang 2593ee2708aeSGeliang Tang ssk = mptcp_subflow_tcp_sock(subflow); 2594ee2708aeSGeliang Tang 25952948d0a1SPaolo Abeni lock_sock(ssk); 25962948d0a1SPaolo Abeni 25972948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25982948d0a1SPaolo Abeni info.sent = 0; 2599ee2708aeSGeliang Tang info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : 2600ee2708aeSGeliang Tang dfrag->already_sent; 26014e14867dSPaolo Abeni while (info.sent < info.limit) { 26022948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 26032948d0a1SPaolo Abeni if (ret <= 0) 26042948d0a1SPaolo Abeni break; 26052948d0a1SPaolo Abeni 26062948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 26072948d0a1SPaolo Abeni copied += ret; 26082948d0a1SPaolo Abeni info.sent += ret; 26092948d0a1SPaolo Abeni } 26104e14867dSPaolo Abeni if (copied) { 2611ee2708aeSGeliang Tang len = max(copied, len); 26122948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 26132948d0a1SPaolo Abeni info.size_goal); 26140530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 26154e14867dSPaolo Abeni } 26162948d0a1SPaolo Abeni 26172948d0a1SPaolo Abeni release_sock(ssk); 2618ee2708aeSGeliang Tang } 2619ee2708aeSGeliang Tang } 2620ee2708aeSGeliang Tang 2621ee2708aeSGeliang Tang msk->bytes_retrans += len; 2622ee2708aeSGeliang Tang dfrag->already_sent = max(dfrag->already_sent, len); 26232948d0a1SPaolo Abeni 26242948d0a1SPaolo Abeni reset_timer: 26253241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 26263241a9c0SFlorian Westphal 26272948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 26282948d0a1SPaolo Abeni mptcp_reset_timer(sk); 26292948d0a1SPaolo Abeni } 26302948d0a1SPaolo Abeni 263176a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 263276a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 263376a13b31SGeliang Tang */ 263476a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 263576a13b31SGeliang Tang { 263676a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 263776a13b31SGeliang Tang unsigned long timeout, close_timeout; 263876a13b31SGeliang Tang 263976a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 264076a13b31SGeliang Tang return; 264176a13b31SGeliang Tang 264276a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 264376a13b31SGeliang Tang 264476a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 264576a13b31SGeliang Tang * them is active 264676a13b31SGeliang Tang */ 264776a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 264876a13b31SGeliang Tang 264976a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 265076a13b31SGeliang Tang } 265176a13b31SGeliang Tang 265249fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 265349fa1919SGeliang Tang { 265476a13b31SGeliang Tang struct sock *ssk = msk->first; 265549fa1919SGeliang Tang bool slow; 265649fa1919SGeliang Tang 265776a13b31SGeliang Tang if (!ssk) 265876a13b31SGeliang Tang return; 265976a13b31SGeliang Tang 266049fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 266149fa1919SGeliang Tang 266249fa1919SGeliang Tang slow = lock_sock_fast(ssk); 266349fa1919SGeliang Tang mptcp_subflow_reset(ssk); 266476a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 266549fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 266676a13b31SGeliang Tang 266776a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 266849fa1919SGeliang Tang } 266949fa1919SGeliang Tang 2670d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2671d21f8348SPaolo Abeni { 2672d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2673d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2674d21f8348SPaolo Abeni 2675bbd49d11SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2676d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2677d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2678d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2679d21f8348SPaolo Abeni } 2680d21f8348SPaolo Abeni 26812948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26822948d0a1SPaolo Abeni { 26832948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 268480638684SGeliang Tang struct sock *sk = (struct sock *)msk; 268576a13b31SGeliang Tang unsigned long fail_tout; 26862948d0a1SPaolo Abeni int state; 268780992017SPaolo Abeni 268880992017SPaolo Abeni lock_sock(sk); 2689e16163b6SPaolo Abeni state = sk->sk_state; 2690d6a04437SPaolo Abeni if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2691e16163b6SPaolo Abeni goto unlock; 2692e16163b6SPaolo Abeni 269350c504a2SFlorian Westphal mptcp_check_fastclose(msk); 269450c504a2SFlorian Westphal 2695e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2696b416268bSFlorian Westphal 269781c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 269881c1d029SPaolo Abeni mptcp_check_data_fin_ack(sk); 269943b54c6eSMat Martineau mptcp_check_data_fin(sk); 270043b54c6eSMat Martineau 2701b6985b9bSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2702b6985b9bSPaolo Abeni __mptcp_close_subflow(sk); 2703b6985b9bSPaolo Abeni 2704341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2705341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2706341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2707e16163b6SPaolo Abeni */ 2708d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 2709bbd49d11SPaolo Abeni if (mptcp_should_close(sk)) 2710d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2711bbd49d11SPaolo Abeni 2712d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2713e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2714e16163b6SPaolo Abeni goto unlock; 2715e16163b6SPaolo Abeni } 2716d21f8348SPaolo Abeni } 2717e16163b6SPaolo Abeni 27182948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 27192948d0a1SPaolo Abeni __mptcp_retrans(sk); 27203b1d6210SPaolo Abeni 272176a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 272276a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 272349fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 272449fa1919SGeliang Tang 27253b1d6210SPaolo Abeni unlock: 272680992017SPaolo Abeni release_sock(sk); 272780992017SPaolo Abeni sock_put(sk); 272880992017SPaolo Abeni } 272980992017SPaolo Abeni 2730e2636917SKuniyuki Iwashima static void __mptcp_init_sock(struct sock *sk) 2731f870fa0bSMat Martineau { 2732cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2733cec37a6eSPeter Krystad 2734cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2735ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 273618b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 273780992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 273887952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2739ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2740f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 27416511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2742ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 274333d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2744cec37a6eSPeter Krystad 27451b1b43eeSPaolo Abeni WRITE_ONCE(msk->first, NULL); 2746dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2747752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 27480530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 27491e1d9d6fSPaolo Abeni msk->recovery = false; 27506f06b4d4SPaolo Abeni msk->subflow_id = 1; 27518ab183deSPaolo Abeni 27521b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 27531b1c7a0eSPeter Krystad 2754b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2755b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2756e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2757f870fa0bSMat Martineau } 2758f870fa0bSMat Martineau 2759b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2760784325e9SMatthieu Baerts { 276120b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2762b29fcfb5SPaolo Abeni 2763b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2764b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2765b29fcfb5SPaolo Abeni 2766b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2767b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2768b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2769b29fcfb5SPaolo Abeni } 2770b29fcfb5SPaolo Abeni 2771b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2772b29fcfb5SPaolo Abeni { 2773fc518953SFlorian Westphal struct net *net = sock_net(sk); 27741730b2b2SGeliang Tang int ret; 277518b683bfSPaolo Abeni 2776e2636917SKuniyuki Iwashima __mptcp_init_sock(sk); 2777b6c08380SGeliang Tang 2778fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2779fc518953SFlorian Westphal return -ENOPROTOOPT; 2780fc518953SFlorian Westphal 2781fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2782fc518953SFlorian Westphal return -ENOMEM; 2783fc518953SFlorian Westphal 27841730b2b2SGeliang Tang ret = mptcp_init_sched(mptcp_sk(sk), 27851730b2b2SGeliang Tang mptcp_sched_find(mptcp_get_scheduler(net))); 27861730b2b2SGeliang Tang if (ret) 27871730b2b2SGeliang Tang return ret; 27881730b2b2SGeliang Tang 2789a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2790a5ef058dSPaolo Abeni 279120b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 279220b5759fSPaolo Abeni * propagate the correct value 279320b5759fSPaolo Abeni */ 2794b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 279520b5759fSPaolo Abeni 2796d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2797a963853fSGeliang Tang sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2798a963853fSGeliang Tang sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2799d027236cSPaolo Abeni 280018b683bfSPaolo Abeni return 0; 280118b683bfSPaolo Abeni } 280218b683bfSPaolo Abeni 280318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 280418b683bfSPaolo Abeni { 280518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 280618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 280718b683bfSPaolo Abeni 2808d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 280918b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2810d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2811784325e9SMatthieu Baerts } 2812784325e9SMatthieu Baerts 281330e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 281480992017SPaolo Abeni { 281580992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 281680992017SPaolo Abeni 2817b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2818e16163b6SPaolo Abeni __sock_put(sk); 281980992017SPaolo Abeni } 282080992017SPaolo Abeni 2821d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 282221498490SPeter Krystad { 282321498490SPeter Krystad lock_sock(ssk); 282421498490SPeter Krystad 282521498490SPeter Krystad switch (ssk->sk_state) { 282621498490SPeter Krystad case TCP_LISTEN: 282721498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 282821498490SPeter Krystad break; 2829df561f66SGustavo A. R. Silva fallthrough; 283021498490SPeter Krystad case TCP_SYN_SENT: 2831c2b2ae39SPaolo Abeni WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 283221498490SPeter Krystad break; 283321498490SPeter Krystad default: 283443b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 283543b54c6eSMat Martineau pr_debug("Fallback"); 283621498490SPeter Krystad ssk->sk_shutdown |= how; 283721498490SPeter Krystad tcp_shutdown(ssk, how); 283881c1d029SPaolo Abeni 283981c1d029SPaolo Abeni /* simulate the data_fin ack reception to let the state 284081c1d029SPaolo Abeni * machine move forward 284181c1d029SPaolo Abeni */ 284281c1d029SPaolo Abeni WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 284381c1d029SPaolo Abeni mptcp_schedule_work(sk); 284443b54c6eSMat Martineau } else { 284543b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 284643b54c6eSMat Martineau tcp_send_ack(ssk); 28476477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 28486477dd39SMat Martineau mptcp_reset_timer(sk); 284943b54c6eSMat Martineau } 285021498490SPeter Krystad break; 285121498490SPeter Krystad } 285221498490SPeter Krystad 285321498490SPeter Krystad release_sock(ssk); 285421498490SPeter Krystad } 285521498490SPeter Krystad 28566920b851SMat Martineau static const unsigned char new_state[16] = { 28576920b851SMat Martineau /* current state: new state: action: */ 28586920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 28596920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28606920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 28616920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 28626920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 28636920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 28646920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28656920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28666920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28676920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28686920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28696920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28706920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28716920b851SMat Martineau }; 28726920b851SMat Martineau 28736920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28746920b851SMat Martineau { 28756920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28766920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28776920b851SMat Martineau 28786920b851SMat Martineau inet_sk_state_store(sk, ns); 28796920b851SMat Martineau 28806920b851SMat Martineau return next & TCP_ACTION_FIN; 28816920b851SMat Martineau } 28826920b851SMat Martineau 288381c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk) 2884e16163b6SPaolo Abeni { 2885e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2886e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2887e16163b6SPaolo Abeni 2888e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2889e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2890e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2891e16163b6SPaolo Abeni 2892e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2893e16163b6SPaolo Abeni * skip this 2894e16163b6SPaolo Abeni */ 2895e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2896e16163b6SPaolo Abeni mptcp_send_head(sk)) 2897e16163b6SPaolo Abeni return; 2898e16163b6SPaolo Abeni 2899e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2900e16163b6SPaolo Abeni 2901e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2902e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2903e16163b6SPaolo Abeni 2904e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2905e16163b6SPaolo Abeni } 2906e16163b6SPaolo Abeni } 2907e16163b6SPaolo Abeni 2908e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2909e16163b6SPaolo Abeni { 2910e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2911e16163b6SPaolo Abeni 2912e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2913e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2914e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2915e16163b6SPaolo Abeni 2916e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2917e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2918e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2919e16163b6SPaolo Abeni 292081c1d029SPaolo Abeni mptcp_check_send_data_fin(sk); 2921e16163b6SPaolo Abeni } 2922e16163b6SPaolo Abeni 2923e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2924f870fa0bSMat Martineau { 2925f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2926f870fa0bSMat Martineau 2927e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 29282c22c06cSFlorian Westphal 29293abc05d9SFlorian Westphal might_sleep(); 29303abc05d9SFlorian Westphal 2931bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2932e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2933e16163b6SPaolo Abeni msk->pm.status = 0; 29341730b2b2SGeliang Tang mptcp_release_sched(msk); 2935b2c5b614SFlorian Westphal 2936e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2937e16163b6SPaolo Abeni 29386511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 293987952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2940e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2941e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2942aa1fbd94SFlorian Westphal 2943e16163b6SPaolo Abeni sock_put(sk); 2944e16163b6SPaolo Abeni } 2945e16163b6SPaolo Abeni 294663740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk) 294763740448SPaolo Abeni { 294863740448SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 294963740448SPaolo Abeni mptcp_do_fastclose(sk); 295063740448SPaolo Abeni __mptcp_destroy_sock(sk); 295163740448SPaolo Abeni } 295263740448SPaolo Abeni 2953d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2954d21f8348SPaolo Abeni { 2955d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2956d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2957d21f8348SPaolo Abeni */ 2958d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2959d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2960d21f8348SPaolo Abeni return 0; 2961d21f8348SPaolo Abeni 2962d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2963d21f8348SPaolo Abeni } 2964d21f8348SPaolo Abeni 296557fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk) 2966c558246eSMenglong Dong { 296757fc0f1cSPaolo Abeni struct sock *ssk; 296857fc0f1cSPaolo Abeni 296957fc0f1cSPaolo Abeni if (inet_sk_state_load(sk) != TCP_LISTEN) 297057fc0f1cSPaolo Abeni return; 297157fc0f1cSPaolo Abeni 2972c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 297357fc0f1cSPaolo Abeni ssk = mptcp_sk(sk)->first; 297457fc0f1cSPaolo Abeni if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 297557fc0f1cSPaolo Abeni return; 297657fc0f1cSPaolo Abeni 297757fc0f1cSPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 29783fffa15bSPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 297957fc0f1cSPaolo Abeni mptcp_subflow_queue_clean(sk, ssk); 298057fc0f1cSPaolo Abeni inet_csk_listen_stop(ssk); 298157fc0f1cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 298257fc0f1cSPaolo Abeni release_sock(ssk); 2983c558246eSMenglong Dong } 2984c558246eSMenglong Dong 298526d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2986e16163b6SPaolo Abeni { 2987e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 298876a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2989e16163b6SPaolo Abeni bool do_cancel_work = false; 2990d4e85922SPaolo Abeni int subflows_alive = 0; 2991e16163b6SPaolo Abeni 29926b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2993e16163b6SPaolo Abeni 2994e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 299557fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 2996e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2997e16163b6SPaolo Abeni goto cleanup; 2998e16163b6SPaolo Abeni } 2999e16163b6SPaolo Abeni 30008d547809SPaolo Abeni if (mptcp_check_readable(msk) || timeout < 0) { 30018d547809SPaolo Abeni /* If the msk has read data, or the caller explicitly ask it, 30028d547809SPaolo Abeni * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 30038d547809SPaolo Abeni */ 3004d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 30058d547809SPaolo Abeni timeout = 0; 3006d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 3007e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 3008d21f8348SPaolo Abeni } 3009e16163b6SPaolo Abeni 3010e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 3011e16163b6SPaolo Abeni 3012e16163b6SPaolo Abeni cleanup: 3013e16163b6SPaolo Abeni /* orphan all the subflows */ 3014e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 301576a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3016e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 301749054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 3018e16163b6SPaolo Abeni 3019d4e85922SPaolo Abeni subflows_alive += ssk->sk_state != TCP_CLOSE; 3020d4e85922SPaolo Abeni 302176a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 302276a13b31SGeliang Tang * cancel the latter 302376a13b31SGeliang Tang */ 302476a13b31SGeliang Tang if (ssk == msk->first) 302576a13b31SGeliang Tang subflow->fail_tout = 0; 302676a13b31SGeliang Tang 3027fe948001SMenglong Dong /* detach from the parent socket, but allow data_ready to 3028fe948001SMenglong Dong * push incoming data into the mptcp stack, to properly ack it 3029fe948001SMenglong Dong */ 3030fe948001SMenglong Dong ssk->sk_socket = NULL; 3031fe948001SMenglong Dong ssk->sk_wq = NULL; 3032e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 3033e16163b6SPaolo Abeni } 3034e16163b6SPaolo Abeni sock_orphan(sk); 3035e16163b6SPaolo Abeni 3036d4e85922SPaolo Abeni /* all the subflows are closed, only timeout can change the msk 3037d4e85922SPaolo Abeni * state, let's not keep resources busy for no reasons 3038d4e85922SPaolo Abeni */ 3039d4e85922SPaolo Abeni if (subflows_alive == 0) 3040d4e85922SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3041d4e85922SPaolo Abeni 3042e16163b6SPaolo Abeni sock_hold(sk); 3043e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 304400df24f1SGeliang Tang if (msk->token) 304576a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3046b29fcfb5SPaolo Abeni 3047e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 3048e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 3049e16163b6SPaolo Abeni do_cancel_work = true; 3050e16163b6SPaolo Abeni } else { 305176a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 3052e16163b6SPaolo Abeni } 305326d3e21cSMenglong Dong 305426d3e21cSMenglong Dong return do_cancel_work; 305526d3e21cSMenglong Dong } 305626d3e21cSMenglong Dong 305726d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 305826d3e21cSMenglong Dong { 305926d3e21cSMenglong Dong bool do_cancel_work; 306026d3e21cSMenglong Dong 306126d3e21cSMenglong Dong lock_sock(sk); 306226d3e21cSMenglong Dong 306326d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 3064e16163b6SPaolo Abeni release_sock(sk); 3065e16163b6SPaolo Abeni if (do_cancel_work) 306680992017SPaolo Abeni mptcp_cancel_work(sk); 3067b911c97cSFlorian Westphal 3068e16163b6SPaolo Abeni sock_put(sk); 3069f870fa0bSMat Martineau } 3070f870fa0bSMat Martineau 30717e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3072cf7da0d6SPeter Krystad { 3073cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3074cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3075cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 3076cf7da0d6SPeter Krystad 3077cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 3078cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3079cf7da0d6SPeter Krystad 3080cf7da0d6SPeter Krystad if (msk6 && ssk6) { 3081cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 3082cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 3083cf7da0d6SPeter Krystad } 3084cf7da0d6SPeter Krystad #endif 3085cf7da0d6SPeter Krystad 3086cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3087cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3088cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3089cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3090cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3091cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3092cf7da0d6SPeter Krystad } 3093cf7da0d6SPeter Krystad 309418b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 309518b683bfSPaolo Abeni { 309676e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 309776e2a55dSPaolo Abeni 30980ad529d9SPaolo Abeni /* Deny disconnect if other threads are blocked in sk_wait_event() 30990ad529d9SPaolo Abeni * or inet_wait_for_connect(). 31000ad529d9SPaolo Abeni */ 31010ad529d9SPaolo Abeni if (sk->sk_wait_pending) 31020ad529d9SPaolo Abeni return -EBUSY; 31030ad529d9SPaolo Abeni 31047d803344SPaolo Abeni /* We are on the fastopen error path. We can't call straight into the 31057d803344SPaolo Abeni * subflows cleanup code due to lock nesting (we are already under 3106c2b2ae39SPaolo Abeni * msk->firstsocket lock). 31077d803344SPaolo Abeni */ 31087d803344SPaolo Abeni if (msk->fastopening) 3109c2b2ae39SPaolo Abeni return -EBUSY; 31107d803344SPaolo Abeni 311157fc0f1cSPaolo Abeni mptcp_check_listen_stop(sk); 3112b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3113b29fcfb5SPaolo Abeni 3114bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3115b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3116b29fcfb5SPaolo Abeni 311700df24f1SGeliang Tang if (msk->token) 311800df24f1SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3119b29fcfb5SPaolo Abeni 3120c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3121c0bf3c6aSPaolo Abeni * subflow 3122c0bf3c6aSPaolo Abeni */ 3123c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3124e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3125e9d09bacSPaolo Abeni msk->cb_flags = 0; 3126e9d09bacSPaolo Abeni msk->push_pending = 0; 3127b29fcfb5SPaolo Abeni msk->recovery = false; 3128b29fcfb5SPaolo Abeni msk->can_ack = false; 3129b29fcfb5SPaolo Abeni msk->fully_established = false; 3130b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3131b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3132b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3133b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3134b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3135b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3136b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 313738967f42SPaolo Abeni msk->bytes_acked = 0; 313838967f42SPaolo Abeni msk->bytes_received = 0; 313938967f42SPaolo Abeni msk->bytes_sent = 0; 314038967f42SPaolo Abeni msk->bytes_retrans = 0; 3141b29fcfb5SPaolo Abeni 31426b9831bfSPaolo Abeni WRITE_ONCE(sk->sk_shutdown, 0); 3143b29fcfb5SPaolo Abeni sk_error_report(sk); 314442c556feSFlorian Westphal return 0; 314518b683bfSPaolo Abeni } 314618b683bfSPaolo Abeni 3147b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3148b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3149b0519de8SFlorian Westphal { 3150b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3151b0519de8SFlorian Westphal 3152b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3153b0519de8SFlorian Westphal } 3154b0519de8SFlorian Westphal #endif 3155b0519de8SFlorian Westphal 31567e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk, 3157cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 31587e8b88ecSPaolo Abeni struct sock *ssk, 3159fca5c82cSPaolo Abeni struct request_sock *req) 3160b0519de8SFlorian Westphal { 316158b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3162b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 316358b09919SPaolo Abeni struct mptcp_sock *msk; 3164b0519de8SFlorian Westphal 3165b0519de8SFlorian Westphal if (!nsk) 3166b0519de8SFlorian Westphal return NULL; 3167b0519de8SFlorian Westphal 3168b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3169b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3170b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3171b0519de8SFlorian Westphal #endif 3172b0519de8SFlorian Westphal 31730ad529d9SPaolo Abeni nsk->sk_wait_pending = 0; 317458b09919SPaolo Abeni __mptcp_init_sock(nsk); 317558b09919SPaolo Abeni 317658b09919SPaolo Abeni msk = mptcp_sk(nsk); 317758b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 317858b09919SPaolo Abeni msk->token = subflow_req->token; 3179b6985b9bSPaolo Abeni msk->in_accept_queue = 1; 3180b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 318174c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 318206251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 318358b09919SPaolo Abeni 318458b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3185eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 31867439d687SPaolo Abeni msk->snd_una = msk->write_seq; 31877439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3188df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 31891730b2b2SGeliang Tang mptcp_init_sched(msk, mptcp_sk(sk)->sched); 31906f8a612aSFlorian Westphal 31916f06b4d4SPaolo Abeni /* passive msk is created after the first/MPC subflow */ 31926f06b4d4SPaolo Abeni msk->subflow_id = 2; 31936f06b4d4SPaolo Abeni 31945e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 31950c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 31967e8b88ecSPaolo Abeni 31977e8b88ecSPaolo Abeni /* this can't race with mptcp_close(), as the msk is 31987e8b88ecSPaolo Abeni * not yet exposted to user-space 31997e8b88ecSPaolo Abeni */ 32007e8b88ecSPaolo Abeni inet_sk_state_store(nsk, TCP_ESTABLISHED); 32017e8b88ecSPaolo Abeni 32027e8b88ecSPaolo Abeni /* The msk maintain a ref to each subflow in the connections list */ 32037e8b88ecSPaolo Abeni WRITE_ONCE(msk->first, ssk); 32047e8b88ecSPaolo Abeni list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list); 32057e8b88ecSPaolo Abeni sock_hold(ssk); 32067e8b88ecSPaolo Abeni 32077e8b88ecSPaolo Abeni /* new mpc subflow takes ownership of the newly 32087e8b88ecSPaolo Abeni * created mptcp socket 32097e8b88ecSPaolo Abeni */ 32107e8b88ecSPaolo Abeni mptcp_token_accept(subflow_req, msk); 32117e8b88ecSPaolo Abeni 32127e8b88ecSPaolo Abeni /* set msk addresses early to ensure mptcp_pm_get_local_id() 32137e8b88ecSPaolo Abeni * uses the correct data 32147e8b88ecSPaolo Abeni */ 32157e8b88ecSPaolo Abeni mptcp_copy_inaddrs(nsk, ssk); 32167e8b88ecSPaolo Abeni mptcp_propagate_sndbuf(nsk, ssk); 32177e8b88ecSPaolo Abeni 32187e8b88ecSPaolo Abeni mptcp_rcv_space_init(msk, ssk); 321958b09919SPaolo Abeni bh_unlock_sock(nsk); 322058b09919SPaolo Abeni 3221b6985b9bSPaolo Abeni /* note: the newly allocated socket refcount is 2 now */ 3222b0519de8SFlorian Westphal return nsk; 3223b0519de8SFlorian Westphal } 3224b0519de8SFlorian Westphal 3225a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3226a6b118feSFlorian Westphal { 3227a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3228a6b118feSFlorian Westphal 3229a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3230a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3231a6b118feSFlorian Westphal 3232a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3233a6b118feSFlorian Westphal 3234a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3235a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3236a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3237a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3238a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 32396f8a612aSFlorian Westphal 32407439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3241a6b118feSFlorian Westphal } 3242a6b118feSFlorian Westphal 32431f6610b9SPaolo Abeni static struct sock *mptcp_accept(struct sock *ssk, int flags, int *err, 3244cf7da0d6SPeter Krystad bool kern) 3245cf7da0d6SPeter Krystad { 3246cf7da0d6SPeter Krystad struct sock *newsk; 3247cf7da0d6SPeter Krystad 32481f6610b9SPaolo Abeni pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk)); 32491f6610b9SPaolo Abeni newsk = inet_csk_accept(ssk, flags, err, kern); 3250cf7da0d6SPeter Krystad if (!newsk) 3251cf7da0d6SPeter Krystad return NULL; 3252cf7da0d6SPeter Krystad 32531f6610b9SPaolo Abeni pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk)); 3254cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3255cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3256cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3257cf7da0d6SPeter Krystad 3258cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 325958b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 326058b09919SPaolo Abeni 326158b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 326258b09919SPaolo Abeni * subflow_syn_recv_sock() 326358b09919SPaolo Abeni */ 326458b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 326558b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3266d6692b3bSFlorian Westphal goto out; 326758b09919SPaolo Abeni } 326858b09919SPaolo Abeni 3269cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 32701f6610b9SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3271fc518953SFlorian Westphal } else { 32721f6610b9SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), 3273fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3274cf7da0d6SPeter Krystad } 3275cf7da0d6SPeter Krystad 3276d6692b3bSFlorian Westphal out: 3277d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3278cf7da0d6SPeter Krystad return newsk; 3279cf7da0d6SPeter Krystad } 3280cf7da0d6SPeter Krystad 3281c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 32825c8c1640SGeliang Tang { 3283c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 328487952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 328587952603SPaolo Abeni 32866e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 32876e628cd3SPaolo Abeni 3288c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 32895efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3290c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3291c0bf3c6aSPaolo Abeni 329287952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3293b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 329487952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 32956511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 32965c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3297b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 32986511882cSPaolo Abeni 32996511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 33006511882cSPaolo Abeni * inet_sock_destruct() will dispose it 33016511882cSPaolo Abeni */ 33025e6300e7SEric Dumazet sk_forward_alloc_add(sk, msk->rmem_fwd_alloc); 33039531e4a8SEric Dumazet WRITE_ONCE(msk->rmem_fwd_alloc, 0); 33045c8c1640SGeliang Tang mptcp_token_destroy(msk); 33055c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 33064638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 33075c8c1640SGeliang Tang } 33085c8c1640SGeliang Tang 330979c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 331079c0949eSPeter Krystad { 3311c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3312c9fd9c5fSFlorian Westphal 331339880bd8SPaolo Abeni /* allow the following to close even the initial subflow */ 331439880bd8SPaolo Abeni msk->free_first = 1; 3315c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3316d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 331779c0949eSPeter Krystad } 331879c0949eSPeter Krystad 33196e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 33206e628cd3SPaolo Abeni { 33216e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 33226e628cd3SPaolo Abeni __mptcp_clean_una(sk); 33236e628cd3SPaolo Abeni else 3324e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 33256e628cd3SPaolo Abeni 33266e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 33276e628cd3SPaolo Abeni mptcp_schedule_work(sk); 33286e628cd3SPaolo Abeni } 33296e628cd3SPaolo Abeni 3330219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 33316e628cd3SPaolo Abeni { 33326e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 33336e628cd3SPaolo Abeni return; 33346e628cd3SPaolo Abeni 333573a0052aSGeliang Tang if (!sock_owned_by_user(sk)) 333673a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, false); 333773a0052aSGeliang Tang else 3338e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 33396e628cd3SPaolo Abeni } 33406e628cd3SPaolo Abeni 3341e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3342e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3343e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3344e9d09bacSPaolo Abeni 3345e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 334614c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3347e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 334814c441b5SPaolo Abeni { 3349e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 33502d6f5a2bSPaolo Abeni 3351e9d09bacSPaolo Abeni for (;;) { 3352e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3353e9d09bacSPaolo Abeni msk->push_pending; 335456a666c4SPaolo Abeni struct list_head join_list; 335556a666c4SPaolo Abeni 3356c2e6048fSPaolo Abeni if (!flags) 3357c2e6048fSPaolo Abeni break; 3358c2e6048fSPaolo Abeni 335956a666c4SPaolo Abeni INIT_LIST_HEAD(&join_list); 336056a666c4SPaolo Abeni list_splice_init(&msk->join_list, &join_list); 336156a666c4SPaolo Abeni 3362c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 33636e628cd3SPaolo Abeni * 33646e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 33656e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 33666e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 33676e628cd3SPaolo Abeni * the subflow socket lock 33686e628cd3SPaolo Abeni */ 3369e9d09bacSPaolo Abeni msk->push_pending = 0; 3370e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 33716e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 337256a666c4SPaolo Abeni 33733e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 337456a666c4SPaolo Abeni __mptcp_flush_join_list(sk, &join_list); 33752e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3376c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 33772d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 33782d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3379c2e6048fSPaolo Abeni 3380c2e6048fSPaolo Abeni cond_resched(); 33816e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 33826e628cd3SPaolo Abeni } 3383c2e6048fSPaolo Abeni 338465a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 338565a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 338617ebf8a4SXiang Yang if (unlikely(msk->cb_flags)) { 3387490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 338865a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3389490274b4SPaolo Abeni */ 3390e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3391490274b4SPaolo Abeni __mptcp_set_connected(sk); 3392e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 339315cc1045SPaolo Abeni __mptcp_error_report(sk); 339465a569b0SPaolo Abeni } 33956e628cd3SPaolo Abeni 339687952603SPaolo Abeni __mptcp_update_rmem(sk); 339714c441b5SPaolo Abeni } 339814c441b5SPaolo Abeni 3399bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3400bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3401bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3402bcd97734SPaolo Abeni */ 3403bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3404bcd97734SPaolo Abeni { 3405bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3406bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3407bcd97734SPaolo Abeni unsigned long timeout; 3408bcd97734SPaolo Abeni 3409bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3410bcd97734SPaolo Abeni return; 3411bcd97734SPaolo Abeni 3412bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3413bcd97734SPaolo Abeni if (tp->srtt_us) 3414bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3415bcd97734SPaolo Abeni else 3416bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3417bcd97734SPaolo Abeni timeout += jiffies; 3418bcd97734SPaolo Abeni 3419bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3420bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3421bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3422bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3423bcd97734SPaolo Abeni } 3424bcd97734SPaolo Abeni 3425b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3426b19bc294SPaolo Abeni { 3427b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3428b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3429b19bc294SPaolo Abeni 3430bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3431b19bc294SPaolo Abeni mptcp_data_lock(sk); 3432b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 343373a0052aSGeliang Tang __mptcp_subflow_push_pending(sk, ssk, true); 3434b19bc294SPaolo Abeni else 3435e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3436b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3437bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3438bcd97734SPaolo Abeni } 3439bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3440bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3441bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3442bcd97734SPaolo Abeni } 3443b19bc294SPaolo Abeni } 3444b19bc294SPaolo Abeni 34452c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 34462c5ebd00SPaolo Abeni { 34472c5ebd00SPaolo Abeni /* should never be called, 34482c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 34492c5ebd00SPaolo Abeni */ 34502c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 34512c5ebd00SPaolo Abeni return 0; 34522c5ebd00SPaolo Abeni } 34532c5ebd00SPaolo Abeni 34542c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 34552c5ebd00SPaolo Abeni { 34562c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 34572c5ebd00SPaolo Abeni } 34582c5ebd00SPaolo Abeni 3459cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3460f870fa0bSMat Martineau { 3461f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3462f870fa0bSMat Martineau 3463cfb63e50SPaolo Abeni pr_debug("msk=%p, ssk=%p", msk, msk->first); 3464cfb63e50SPaolo Abeni if (WARN_ON_ONCE(!msk->first)) 3465cec37a6eSPeter Krystad return -EINVAL; 3466f870fa0bSMat Martineau 3467cfb63e50SPaolo Abeni return inet_csk_get_port(msk->first, snum); 3468cec37a6eSPeter Krystad } 3469f870fa0bSMat Martineau 3470cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3471cec37a6eSPeter Krystad { 3472cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3473cec37a6eSPeter Krystad struct mptcp_sock *msk; 3474cec37a6eSPeter Krystad struct sock *sk; 3475f870fa0bSMat Martineau 3476cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3477cec37a6eSPeter Krystad sk = subflow->conn; 3478cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3479cec37a6eSPeter Krystad 3480648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3481648ef4b8SMat Martineau 3482b3ea6b27SPaolo Abeni subflow->map_seq = subflow->iasn; 3483648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 34846d0060f6SMat Martineau 3485cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3486cec37a6eSPeter Krystad * accessing the field below 3487cec37a6eSPeter Krystad */ 3488cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 34896d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3490eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 34917439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 34921b1c7a0eSPeter Krystad 34936c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3494a6b118feSFlorian Westphal 3495a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3496f870fa0bSMat Martineau } 3497f870fa0bSMat Martineau 3498866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3499cf7da0d6SPeter Krystad { 3500cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3501cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3502cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3503cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3504cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3505cf7da0d6SPeter Krystad } 3506cf7da0d6SPeter Krystad 3507e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3508f296234cSPeter Krystad { 3509e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3510f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3511f296234cSPeter Krystad struct sock *parent = (void *)msk; 35123e501490SPaolo Abeni bool ret = true; 3513f296234cSPeter Krystad 3514f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3515f296234cSPeter Krystad 3516f296234cSPeter Krystad /* mptcp socket already closing? */ 3517dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3518dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3519f296234cSPeter Krystad return false; 3520dc87efdbSFlorian Westphal } 3521f296234cSPeter Krystad 352255b47ca7SPaolo Abeni /* active subflow, already present inside the conn_list */ 352355b47ca7SPaolo Abeni if (!list_empty(&subflow->node)) { 352455b47ca7SPaolo Abeni mptcp_subflow_joined(msk, ssk); 352555b47ca7SPaolo Abeni return true; 352655b47ca7SPaolo Abeni } 3527f296234cSPeter Krystad 35283e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 35293e501490SPaolo Abeni goto err_prohibited; 353010f6d46cSPaolo Abeni 353155b47ca7SPaolo Abeni /* If we can't acquire msk socket lock here, let the release callback 35323e501490SPaolo Abeni * handle it 353310f6d46cSPaolo Abeni */ 35343e501490SPaolo Abeni mptcp_data_lock(parent); 35353e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 35363e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 35373e501490SPaolo Abeni if (ret) { 3538e16163b6SPaolo Abeni sock_hold(ssk); 35393e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3540e16163b6SPaolo Abeni } 35413e501490SPaolo Abeni } else { 35423e501490SPaolo Abeni sock_hold(ssk); 35433e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3544e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 35453e501490SPaolo Abeni } 35463e501490SPaolo Abeni mptcp_data_unlock(parent); 35473e501490SPaolo Abeni 3548dc87efdbSFlorian Westphal if (!ret) { 35493e501490SPaolo Abeni err_prohibited: 3550dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 355110f6d46cSPaolo Abeni return false; 3552dc87efdbSFlorian Westphal } 355310f6d46cSPaolo Abeni 355410f6d46cSPaolo Abeni return true; 3555f296234cSPeter Krystad } 3556f296234cSPeter Krystad 355776e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 355876e2a55dSPaolo Abeni { 355976e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 356076e2a55dSPaolo Abeni 356176e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 356276e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 356376e2a55dSPaolo Abeni } 356476e2a55dSPaolo Abeni 35656511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 35666511882cSPaolo Abeni { 35679531e4a8SEric Dumazet return READ_ONCE(sk->sk_forward_alloc) + 35689531e4a8SEric Dumazet READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc); 35696511882cSPaolo Abeni } 35706511882cSPaolo Abeni 3571644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3572644807e3SFlorian Westphal { 3573644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3574644807e3SFlorian Westphal u64 delta; 3575644807e3SFlorian Westphal 3576644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3577644807e3SFlorian Westphal return -EINVAL; 3578644807e3SFlorian Westphal 3579644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3580644807e3SFlorian Westphal return 0; 3581644807e3SFlorian Westphal 3582644807e3SFlorian Westphal delta = msk->write_seq - v; 358307c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 358407c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 358507c2c7a3SPaolo Abeni 358607c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 358707c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 358807c2c7a3SPaolo Abeni * so ignore that status, too. 358907c2c7a3SPaolo Abeni */ 359007c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 359107c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 359207c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 359307c2c7a3SPaolo Abeni } 3594644807e3SFlorian Westphal if (delta > INT_MAX) 3595644807e3SFlorian Westphal delta = INT_MAX; 3596644807e3SFlorian Westphal 3597644807e3SFlorian Westphal return (int)delta; 3598644807e3SFlorian Westphal } 3599644807e3SFlorian Westphal 3600e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) 3601644807e3SFlorian Westphal { 3602644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3603644807e3SFlorian Westphal bool slow; 3604644807e3SFlorian Westphal 3605644807e3SFlorian Westphal switch (cmd) { 3606644807e3SFlorian Westphal case SIOCINQ: 3607644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3608644807e3SFlorian Westphal return -EINVAL; 3609644807e3SFlorian Westphal 3610644807e3SFlorian Westphal lock_sock(sk); 3611644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3612e1d001faSBreno Leitao *karg = mptcp_inq_hint(sk); 3613644807e3SFlorian Westphal release_sock(sk); 3614644807e3SFlorian Westphal break; 3615644807e3SFlorian Westphal case SIOCOUTQ: 3616644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3617e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3618644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3619644807e3SFlorian Westphal break; 3620644807e3SFlorian Westphal case SIOCOUTQNSD: 3621644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3622e1d001faSBreno Leitao *karg = mptcp_ioctl_outq(msk, msk->snd_nxt); 3623644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3624644807e3SFlorian Westphal break; 3625644807e3SFlorian Westphal default: 3626644807e3SFlorian Westphal return -ENOIOCTLCMD; 3627644807e3SFlorian Westphal } 3628644807e3SFlorian Westphal 3629e1d001faSBreno Leitao return 0; 3630644807e3SFlorian Westphal } 3631644807e3SFlorian Westphal 363254f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 363354f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 363454f1944eSPaolo Abeni { 363554f1944eSPaolo Abeni subflow->request_mptcp = 0; 363654f1944eSPaolo Abeni __mptcp_do_fallback(msk); 363754f1944eSPaolo Abeni } 363854f1944eSPaolo Abeni 363954f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 364054f1944eSPaolo Abeni { 364154f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 364254f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 364354f1944eSPaolo Abeni int err = -EINVAL; 3644ccae357cSPaolo Abeni struct sock *ssk; 364554f1944eSPaolo Abeni 36463f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 36473f326a82SPaolo Abeni if (IS_ERR(ssk)) 36483f326a82SPaolo Abeni return PTR_ERR(ssk); 364954f1944eSPaolo Abeni 365054f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 3651ccae357cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 365254f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 365354f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 365454f1944eSPaolo Abeni * TCP option space. 365554f1944eSPaolo Abeni */ 3656ccae357cSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info)) 365754f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 365854f1944eSPaolo Abeni #endif 3659ccae357cSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) { 3660ccae357cSPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT); 366154f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 366254f1944eSPaolo Abeni } 366354f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 366454f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 366554f1944eSPaolo Abeni 366654f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 366754f1944eSPaolo Abeni * acquired the subflow socket lock, too. 366854f1944eSPaolo Abeni */ 3669ccae357cSPaolo Abeni if (!msk->fastopening) 3670ccae357cSPaolo Abeni lock_sock(ssk); 3671ccae357cSPaolo Abeni 3672ccae357cSPaolo Abeni /* the following mirrors closely a very small chunk of code from 3673ccae357cSPaolo Abeni * __inet_stream_connect() 3674ccae357cSPaolo Abeni */ 3675ccae357cSPaolo Abeni if (ssk->sk_state != TCP_CLOSE) 3676ccae357cSPaolo Abeni goto out; 3677ccae357cSPaolo Abeni 3678ccae357cSPaolo Abeni if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) { 3679ccae357cSPaolo Abeni err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len); 3680ccae357cSPaolo Abeni if (err) 3681ccae357cSPaolo Abeni goto out; 3682ccae357cSPaolo Abeni } 3683ccae357cSPaolo Abeni 3684ccae357cSPaolo Abeni err = ssk->sk_prot->connect(ssk, uaddr, addr_len); 3685ccae357cSPaolo Abeni if (err < 0) 3686ccae357cSPaolo Abeni goto out; 3687ccae357cSPaolo Abeni 368808e39c0dSEric Dumazet inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk)); 3689ccae357cSPaolo Abeni 3690ccae357cSPaolo Abeni out: 3691ccae357cSPaolo Abeni if (!msk->fastopening) 3692ccae357cSPaolo Abeni release_sock(ssk); 369354f1944eSPaolo Abeni 369454f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 369554f1944eSPaolo Abeni * subflow_finish_connect() 369654f1944eSPaolo Abeni */ 3697ccae357cSPaolo Abeni if (unlikely(err)) { 3698131a6277SPaolo Abeni /* avoid leaving a dangling token in an unconnected socket */ 3699131a6277SPaolo Abeni mptcp_token_destroy(msk); 3700ccae357cSPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 370154f1944eSPaolo Abeni return err; 370254f1944eSPaolo Abeni } 370354f1944eSPaolo Abeni 3704ccae357cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 3705786fc124SPaolo Abeni return 0; 370654f1944eSPaolo Abeni } 370754f1944eSPaolo Abeni 3708f870fa0bSMat Martineau static struct proto mptcp_prot = { 3709f870fa0bSMat Martineau .name = "MPTCP", 3710f870fa0bSMat Martineau .owner = THIS_MODULE, 3711f870fa0bSMat Martineau .init = mptcp_init_sock, 371254f1944eSPaolo Abeni .connect = mptcp_connect, 371318b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3714f870fa0bSMat Martineau .close = mptcp_close, 3715cf7da0d6SPeter Krystad .accept = mptcp_accept, 3716717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3717717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 371876e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 371979c0949eSPeter Krystad .destroy = mptcp_destroy, 3720f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3721644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3722f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 372314c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 37242c5ebd00SPaolo Abeni .hash = mptcp_hash, 37252c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3726cec37a6eSPeter Krystad .get_port = mptcp_get_port, 37276511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3728d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 37290defbb0aSEric Dumazet 3730d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 37310defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 37320defbb0aSEric Dumazet 3733d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3734d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3735989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3736d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3737f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 37382c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3739f870fa0bSMat Martineau .no_autobind = true, 3740f870fa0bSMat Martineau }; 3741f870fa0bSMat Martineau 37422303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 37432303f994SPeter Krystad { 37442303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 37458cf2ebdcSPaolo Abeni struct sock *ssk, *sk = sock->sk; 37468cf2ebdcSPaolo Abeni int err = -EINVAL; 37472303f994SPeter Krystad 37488cf2ebdcSPaolo Abeni lock_sock(sk); 37493f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 37503f326a82SPaolo Abeni if (IS_ERR(ssk)) { 37513f326a82SPaolo Abeni err = PTR_ERR(ssk); 37522303f994SPeter Krystad goto unlock; 37532303f994SPeter Krystad } 37542303f994SPeter Krystad 37558cf2ebdcSPaolo Abeni if (sk->sk_family == AF_INET) 37568cf2ebdcSPaolo Abeni err = inet_bind_sk(ssk, uaddr, addr_len); 37578cf2ebdcSPaolo Abeni #if IS_ENABLED(CONFIG_MPTCP_IPV6) 37588cf2ebdcSPaolo Abeni else if (sk->sk_family == AF_INET6) 37598cf2ebdcSPaolo Abeni err = inet6_bind_sk(ssk, uaddr, addr_len); 37608cf2ebdcSPaolo Abeni #endif 3761cf7da0d6SPeter Krystad if (!err) 37628cf2ebdcSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 37632303f994SPeter Krystad 37642303f994SPeter Krystad unlock: 37658cf2ebdcSPaolo Abeni release_sock(sk); 37662303f994SPeter Krystad return err; 37672303f994SPeter Krystad } 37682303f994SPeter Krystad 3769cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3770cf7da0d6SPeter Krystad { 3771cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3772cfdcfeedSMenglong Dong struct sock *sk = sock->sk; 377340f56d0cSPaolo Abeni struct sock *ssk; 3774cf7da0d6SPeter Krystad int err; 3775cf7da0d6SPeter Krystad 3776cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3777cf7da0d6SPeter Krystad 3778cfdcfeedSMenglong Dong lock_sock(sk); 37790226436aSPaolo Abeni 37800226436aSPaolo Abeni err = -EINVAL; 37810226436aSPaolo Abeni if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) 37820226436aSPaolo Abeni goto unlock; 37830226436aSPaolo Abeni 37843f326a82SPaolo Abeni ssk = __mptcp_nmpc_sk(msk); 37853f326a82SPaolo Abeni if (IS_ERR(ssk)) { 37863f326a82SPaolo Abeni err = PTR_ERR(ssk); 3787cf7da0d6SPeter Krystad goto unlock; 3788cf7da0d6SPeter Krystad } 3789cf7da0d6SPeter Krystad 3790cfdcfeedSMenglong Dong inet_sk_state_store(sk, TCP_LISTEN); 3791cfdcfeedSMenglong Dong sock_set_flag(sk, SOCK_RCU_FREE); 37925e20087dSFlorian Westphal 379340f56d0cSPaolo Abeni lock_sock(ssk); 379440f56d0cSPaolo Abeni err = __inet_listen_sk(ssk, backlog); 379540f56d0cSPaolo Abeni release_sock(ssk); 379640f56d0cSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssk)); 379740f56d0cSPaolo Abeni 3798c558246eSMenglong Dong if (!err) { 3799c558246eSMenglong Dong sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 380040f56d0cSPaolo Abeni mptcp_copy_inaddrs(sk, ssk); 380140f56d0cSPaolo Abeni mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED); 380221d9b73aSPaolo Abeni } 3803f8c9dfbdSGeliang Tang 3804cf7da0d6SPeter Krystad unlock: 3805cfdcfeedSMenglong Dong release_sock(sk); 3806cf7da0d6SPeter Krystad return err; 3807cf7da0d6SPeter Krystad } 3808cf7da0d6SPeter Krystad 3809cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3810cf7da0d6SPeter Krystad int flags, bool kern) 3811cf7da0d6SPeter Krystad { 3812cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 38131f6610b9SPaolo Abeni struct sock *ssk, *newsk; 3814cf7da0d6SPeter Krystad int err; 3815cf7da0d6SPeter Krystad 3816cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3817cf7da0d6SPeter Krystad 38185b825727SPaolo Abeni /* Buggy applications can call accept on socket states other then LISTEN 381961761231SPaolo Abeni * but no need to allocate the first subflow just to error out. 382061761231SPaolo Abeni */ 38211f6610b9SPaolo Abeni ssk = READ_ONCE(msk->first); 38221f6610b9SPaolo Abeni if (!ssk) 382371ba088cSPaolo Abeni return -EINVAL; 3824cf7da0d6SPeter Krystad 38251f6610b9SPaolo Abeni newsk = mptcp_accept(ssk, flags, &err, kern); 3826e76c8ef5SPaolo Abeni if (!newsk) 3827e76c8ef5SPaolo Abeni return err; 3828e76c8ef5SPaolo Abeni 3829e76c8ef5SPaolo Abeni lock_sock(newsk); 3830e76c8ef5SPaolo Abeni 3831e76c8ef5SPaolo Abeni __inet_accept(sock, newsock, newsk); 3832e76c8ef5SPaolo Abeni if (!mptcp_is_tcpsk(newsock->sk)) { 3833e76c8ef5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(newsk); 3834cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 38350397c6d8SPaolo Abeni 3836a5ef058dSPaolo Abeni set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3837b6985b9bSPaolo Abeni msk->in_accept_queue = 0; 3838a5ef058dSPaolo Abeni 3839cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3840cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3841cf7da0d6SPeter Krystad */ 3842190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3843cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3844cf7da0d6SPeter Krystad 3845cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3846cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3847cf7da0d6SPeter Krystad } 384863740448SPaolo Abeni 384963740448SPaolo Abeni /* Do late cleanup for the first subflow as necessary. Also 385063740448SPaolo Abeni * deal with bad peers not doing a complete shutdown. 385163740448SPaolo Abeni */ 385239880bd8SPaolo Abeni if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 385363740448SPaolo Abeni __mptcp_close_ssk(newsk, msk->first, 385463740448SPaolo Abeni mptcp_subflow_ctx(msk->first), 0); 385539880bd8SPaolo Abeni if (unlikely(list_is_singular(&msk->conn_list))) 385663740448SPaolo Abeni inet_sk_state_store(newsk, TCP_CLOSE); 385763740448SPaolo Abeni } 3858cf7da0d6SPeter Krystad } 3859e76c8ef5SPaolo Abeni release_sock(newsk); 3860cf7da0d6SPeter Krystad 3861e76c8ef5SPaolo Abeni return 0; 3862cf7da0d6SPeter Krystad } 3863cf7da0d6SPeter Krystad 38648edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 38658edf0864SFlorian Westphal { 38668edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 38678edf0864SFlorian Westphal 38688edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 38698edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 38708edf0864SFlorian Westphal 38715cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 38726e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 38736e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 38746e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 38758edf0864SFlorian Westphal 38766e628cd3SPaolo Abeni return 0; 38778edf0864SFlorian Westphal } 38788edf0864SFlorian Westphal 38792303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 38802303f994SPeter Krystad struct poll_table_struct *wait) 38812303f994SPeter Krystad { 38821891c4a0SFlorian Westphal struct sock *sk = sock->sk; 38838ab183deSPaolo Abeni struct mptcp_sock *msk; 38842303f994SPeter Krystad __poll_t mask = 0; 38856b9831bfSPaolo Abeni u8 shutdown; 38868a05661bSPaolo Abeni int state; 38872303f994SPeter Krystad 38881891c4a0SFlorian Westphal msk = mptcp_sk(sk); 38891891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 38901891c4a0SFlorian Westphal 38918a05661bSPaolo Abeni state = inet_sk_state_load(sk); 38926719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 389371ba088cSPaolo Abeni if (state == TCP_LISTEN) { 38945426a4efSPaolo Abeni struct sock *ssk = READ_ONCE(msk->first); 38955b825727SPaolo Abeni 38965426a4efSPaolo Abeni if (WARN_ON_ONCE(!ssk)) 389771ba088cSPaolo Abeni return 0; 389871ba088cSPaolo Abeni 38995426a4efSPaolo Abeni return inet_csk_listen_poll(ssk); 390071ba088cSPaolo Abeni } 39018a05661bSPaolo Abeni 39026b9831bfSPaolo Abeni shutdown = READ_ONCE(sk->sk_shutdown); 39036b9831bfSPaolo Abeni if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 39046b9831bfSPaolo Abeni mask |= EPOLLHUP; 39056b9831bfSPaolo Abeni if (shutdown & RCV_SHUTDOWN) 39066b9831bfSPaolo Abeni mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 39076b9831bfSPaolo Abeni 39088a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 39098a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 39106b9831bfSPaolo Abeni if (shutdown & SEND_SHUTDOWN) 39116b9831bfSPaolo Abeni mask |= EPOLLOUT | EPOLLWRNORM; 39126b9831bfSPaolo Abeni else 39138edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 391408e39c0dSEric Dumazet } else if (state == TCP_SYN_SENT && 391508e39c0dSEric Dumazet inet_test_bit(DEFER_CONNECT, sk)) { 3916a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3917a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 39188a05661bSPaolo Abeni } 39191891c4a0SFlorian Westphal 3920d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 392115cc1045SPaolo Abeni smp_rmb(); 39229ae8e5adSEric Dumazet if (READ_ONCE(sk->sk_err)) 392315cc1045SPaolo Abeni mask |= EPOLLERR; 392415cc1045SPaolo Abeni 39252303f994SPeter Krystad return mask; 39262303f994SPeter Krystad } 39272303f994SPeter Krystad 3928e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3929e42f1ac6SFlorian Westphal .family = PF_INET, 3930e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 39310a3cc579SPaolo Abeni .release = inet_release, 3932e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3933786fc124SPaolo Abeni .connect = inet_stream_connect, 3934e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3935e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3936d2f77c53SPaolo Abeni .getname = inet_getname, 3937e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3938e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3939e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3940e42f1ac6SFlorian Westphal .listen = mptcp_listen, 394176e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3942e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3943e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3944e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3945e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3946e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3947e42f1ac6SFlorian Westphal }; 39482303f994SPeter Krystad 3949f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3950f870fa0bSMat Martineau .type = SOCK_STREAM, 3951f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3952f870fa0bSMat Martineau .prot = &mptcp_prot, 39532303f994SPeter Krystad .ops = &mptcp_stream_ops, 39542303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3955f870fa0bSMat Martineau }; 3956f870fa0bSMat Martineau 3957b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3958b19bc294SPaolo Abeni { 3959b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3960b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3961b19bc294SPaolo Abeni int work_done = 0; 3962b19bc294SPaolo Abeni 3963b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3964b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3965b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3966b19bc294SPaolo Abeni 3967b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3968b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3969b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3970b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3971b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3972b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3973b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3974b19bc294SPaolo Abeni * CPU that scheduled it. 3975b19bc294SPaolo Abeni */ 3976b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3977b19bc294SPaolo Abeni sock_put(ssk); 3978b19bc294SPaolo Abeni 3979b19bc294SPaolo Abeni if (++work_done == budget) 3980b19bc294SPaolo Abeni return budget; 3981b19bc294SPaolo Abeni } 3982b19bc294SPaolo Abeni 3983b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3984b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3985b19bc294SPaolo Abeni */ 3986b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3987b19bc294SPaolo Abeni return work_done; 3988b19bc294SPaolo Abeni } 3989b19bc294SPaolo Abeni 3990d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3991f870fa0bSMat Martineau { 3992b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3993b19bc294SPaolo Abeni int cpu; 3994b19bc294SPaolo Abeni 39952303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 39962303f994SPeter Krystad 3997d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3998d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3999d027236cSPaolo Abeni 4000b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 4001b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 4002b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 4003b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 400416d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 400516d083e2SJakub Kicinski mptcp_napi_poll); 4006b19bc294SPaolo Abeni napi_enable(&delegated->napi); 4007b19bc294SPaolo Abeni } 4008b19bc294SPaolo Abeni 40092303f994SPeter Krystad mptcp_subflow_init(); 40101b1c7a0eSPeter Krystad mptcp_pm_init(); 4011ed1ad86bSGeliang Tang mptcp_sched_init(); 40122c5ebd00SPaolo Abeni mptcp_token_init(); 40132303f994SPeter Krystad 4014f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 4015f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 4016f870fa0bSMat Martineau 4017f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 40186771bfd9SFlorian Westphal 40196771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 4020f870fa0bSMat Martineau } 4021f870fa0bSMat Martineau 4022f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 4023e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 4024e42f1ac6SFlorian Westphal .family = PF_INET6, 4025e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 40260a3cc579SPaolo Abeni .release = inet6_release, 4027e42f1ac6SFlorian Westphal .bind = mptcp_bind, 4028786fc124SPaolo Abeni .connect = inet_stream_connect, 4029e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 4030e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 4031d2f77c53SPaolo Abeni .getname = inet6_getname, 4032e42f1ac6SFlorian Westphal .poll = mptcp_poll, 4033e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 4034e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 4035e42f1ac6SFlorian Westphal .listen = mptcp_listen, 403676e2a55dSPaolo Abeni .shutdown = inet_shutdown, 4037e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 4038e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 4039e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 4040e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 4041e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 4042e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 40433986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 4044e42f1ac6SFlorian Westphal #endif 4045e42f1ac6SFlorian Westphal }; 4046e42f1ac6SFlorian Westphal 4047f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 4048f870fa0bSMat Martineau 4049f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 4050f870fa0bSMat Martineau .type = SOCK_STREAM, 4051f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 4052f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 40532303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 4054f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 4055f870fa0bSMat Martineau }; 4056f870fa0bSMat Martineau 4057d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 4058f870fa0bSMat Martineau { 4059f870fa0bSMat Martineau int err; 4060f870fa0bSMat Martineau 4061f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 4062f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 4063f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 4064b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 4065f5f80e32SEric Dumazet mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np); 4066f870fa0bSMat Martineau 4067f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 4068f870fa0bSMat Martineau if (err) 4069f870fa0bSMat Martineau return err; 4070f870fa0bSMat Martineau 4071f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 4072f870fa0bSMat Martineau if (err) 4073f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 4074f870fa0bSMat Martineau 4075f870fa0bSMat Martineau return err; 4076f870fa0bSMat Martineau } 4077f870fa0bSMat Martineau #endif 4078