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 396771bfd9SFlorian Westphal struct mptcp_skb_cb { 40ab174ad8SPaolo Abeni u64 map_seq; 41ab174ad8SPaolo Abeni u64 end_seq; 426771bfd9SFlorian Westphal u32 offset; 43b7f653b2SFlorian Westphal u8 has_rxtstamp:1; 446771bfd9SFlorian Westphal }; 456771bfd9SFlorian Westphal 466771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 476771bfd9SFlorian Westphal 48b7f653b2SFlorian Westphal enum { 49b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 502c9e7765SFlorian Westphal MPTCP_CMSG_INQ = BIT(1), 51b7f653b2SFlorian Westphal }; 52b7f653b2SFlorian Westphal 5391b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 54d027236cSPaolo Abeni 55e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 56d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 57e16163b6SPaolo Abeni 58b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 59b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 60b19bc294SPaolo Abeni 612303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 622303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 632303f994SPeter Krystad * Otherwise return NULL. 642303f994SPeter Krystad */ 651729cf18SGeliang Tang struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 662303f994SPeter Krystad { 67d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 682303f994SPeter Krystad return NULL; 692303f994SPeter Krystad 702303f994SPeter Krystad return msk->subflow; 712303f994SPeter Krystad } 722303f994SPeter Krystad 736f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 746f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 756f8a612aSFlorian Westphal { 767439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 776f8a612aSFlorian Westphal } 786f8a612aSFlorian Westphal 79d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 800b4f33deSFlorian Westphal { 810b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 820b4f33deSFlorian Westphal 830b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 840b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 850b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 860b4f33deSFlorian Westphal * not an mptcp one. 870b4f33deSFlorian Westphal * 880b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 890b4f33deSFlorian Westphal * bypass mptcp. 900b4f33deSFlorian Westphal */ 910b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 92d2f77c53SPaolo Abeni return true; 930b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 940b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 950b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 96d2f77c53SPaolo Abeni return true; 970b4f33deSFlorian Westphal #endif 980b4f33deSFlorian Westphal } 990b4f33deSFlorian Westphal 100d2f77c53SPaolo Abeni return false; 1010b4f33deSFlorian Westphal } 1020b4f33deSFlorian Westphal 103fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 1042303f994SPeter Krystad { 1052303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1062303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1072303f994SPeter Krystad struct socket *ssock; 1082303f994SPeter Krystad int err; 1092303f994SPeter Krystad 1102303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1112303f994SPeter Krystad if (err) 112fa68018dSPaolo Abeni return err; 1132303f994SPeter Krystad 1148ab183deSPaolo Abeni msk->first = ssock->sk; 1152303f994SPeter Krystad msk->subflow = ssock; 1162303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 117cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 118e16163b6SPaolo Abeni sock_hold(ssock->sk); 1192303f994SPeter Krystad subflow->request_mptcp = 1; 1204cf86ae8SPaolo Abeni 1214cf86ae8SPaolo Abeni /* This is the first subflow, always with id 0 */ 1224cf86ae8SPaolo Abeni subflow->local_id_valid = 1; 123866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 124e1ff9e82SDavide Caratti 125fa68018dSPaolo Abeni return 0; 1262303f994SPeter Krystad } 1272303f994SPeter Krystad 128ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 129ab174ad8SPaolo Abeni { 130ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 131ab174ad8SPaolo Abeni __kfree_skb(skb); 132ab174ad8SPaolo Abeni } 133ab174ad8SPaolo Abeni 1346511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size) 1356511882cSPaolo Abeni { 1366511882cSPaolo Abeni mptcp_sk(sk)->rmem_fwd_alloc -= size; 1376511882cSPaolo Abeni } 1386511882cSPaolo Abeni 1398268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1408268ed4cSPaolo Abeni struct sk_buff *from) 1418268ed4cSPaolo Abeni { 1428268ed4cSPaolo Abeni bool fragstolen; 1438268ed4cSPaolo Abeni int delta; 1448268ed4cSPaolo Abeni 1458268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1468268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1478268ed4cSPaolo Abeni return false; 1488268ed4cSPaolo Abeni 14906242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 15006242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 15106242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 152ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1537288ff6eSPaolo Abeni 1547288ff6eSPaolo Abeni /* note the fwd memory can reach a negative value after accounting 1557288ff6eSPaolo Abeni * for the delta, but the later skb free will restore a non 1567288ff6eSPaolo Abeni * negative one 1577288ff6eSPaolo Abeni */ 1588268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1596511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1607288ff6eSPaolo Abeni kfree_skb_partial(from, fragstolen); 1617288ff6eSPaolo Abeni 1628268ed4cSPaolo Abeni return true; 1638268ed4cSPaolo Abeni } 1648268ed4cSPaolo Abeni 165ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 166ab174ad8SPaolo Abeni struct sk_buff *from) 1676771bfd9SFlorian Westphal { 168ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 169ab174ad8SPaolo Abeni return false; 170ab174ad8SPaolo Abeni 171ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 172ab174ad8SPaolo Abeni } 173ab174ad8SPaolo Abeni 1746511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1756511882cSPaolo Abeni { 176100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 177100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 1786511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1796511882cSPaolo Abeni } 1806511882cSPaolo Abeni 1816511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1826511882cSPaolo Abeni { 1836511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1846511882cSPaolo Abeni int reclaimable; 1856511882cSPaolo Abeni 1866511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1876511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1886511882cSPaolo Abeni 1896511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 190d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 191d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1926511882cSPaolo Abeni } 1936511882cSPaolo Abeni 1946511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 1956511882cSPaolo Abeni { 1966511882cSPaolo Abeni unsigned int len = skb->truesize; 1976511882cSPaolo Abeni struct sock *sk = skb->sk; 1986511882cSPaolo Abeni 1996511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 2006511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 2016511882cSPaolo Abeni } 2026511882cSPaolo Abeni 2036511882cSPaolo Abeni static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 2046511882cSPaolo Abeni { 2056511882cSPaolo Abeni skb_orphan(skb); 2066511882cSPaolo Abeni skb->sk = sk; 2076511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2086511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2096511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2106511882cSPaolo Abeni } 2116511882cSPaolo Abeni 212ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 213ab174ad8SPaolo Abeni * - use mptcp seqs 214ab174ad8SPaolo Abeni * - don't cope with sacks 215ab174ad8SPaolo Abeni */ 216ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 217ab174ad8SPaolo Abeni { 218ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 219ab174ad8SPaolo Abeni struct rb_node **p, *parent; 220ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 221ab174ad8SPaolo Abeni struct sk_buff *skb1; 222ab174ad8SPaolo Abeni 223ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 224ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 225f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 226ab174ad8SPaolo Abeni 22706242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 22806242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 229fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 230ab174ad8SPaolo Abeni /* out of window */ 231ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 232fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 233fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 234f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 23506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 236ab174ad8SPaolo Abeni return; 237ab174ad8SPaolo Abeni } 238ab174ad8SPaolo Abeni 239ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 24006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 241ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 242ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 243ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 244ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 245ab174ad8SPaolo Abeni goto end; 246ab174ad8SPaolo Abeni } 247ab174ad8SPaolo Abeni 248ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 249ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 250ab174ad8SPaolo Abeni */ 25106242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 25206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 25306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 254ab174ad8SPaolo Abeni return; 25506242e44SPaolo Abeni } 256ab174ad8SPaolo Abeni 257ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 258ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 25906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 260ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 261ab174ad8SPaolo Abeni p = &parent->rb_right; 262ab174ad8SPaolo Abeni goto insert; 263ab174ad8SPaolo Abeni } 264ab174ad8SPaolo Abeni 265ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 266ab174ad8SPaolo Abeni parent = NULL; 267ab174ad8SPaolo Abeni while (*p) { 268ab174ad8SPaolo Abeni parent = *p; 269ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 270ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 271ab174ad8SPaolo Abeni p = &parent->rb_left; 272ab174ad8SPaolo Abeni continue; 273ab174ad8SPaolo Abeni } 274ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 275ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 276ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 277ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 27806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 279ab174ad8SPaolo Abeni return; 280ab174ad8SPaolo Abeni } 281ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 282ab174ad8SPaolo Abeni /* partial overlap: 283ab174ad8SPaolo Abeni * | skb | 284ab174ad8SPaolo Abeni * | skb1 | 285ab174ad8SPaolo Abeni * continue traversing 286ab174ad8SPaolo Abeni */ 287ab174ad8SPaolo Abeni } else { 288ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 289ab174ad8SPaolo Abeni * Replace skb1 with skb. 290ab174ad8SPaolo Abeni */ 291ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 292ab174ad8SPaolo Abeni &msk->out_of_order_queue); 293ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 29406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 295ab174ad8SPaolo Abeni goto merge_right; 296ab174ad8SPaolo Abeni } 297ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 29806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 299ab174ad8SPaolo Abeni return; 300ab174ad8SPaolo Abeni } 301ab174ad8SPaolo Abeni p = &parent->rb_right; 302ab174ad8SPaolo Abeni } 30306242e44SPaolo Abeni 304ab174ad8SPaolo Abeni insert: 305ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 306ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 307ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 308ab174ad8SPaolo Abeni 309ab174ad8SPaolo Abeni merge_right: 310ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 311ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 312ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 313ab174ad8SPaolo Abeni break; 314ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 315ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 31606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 317ab174ad8SPaolo Abeni } 318ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 319ab174ad8SPaolo Abeni if (!skb1) 320ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 321ab174ad8SPaolo Abeni 322ab174ad8SPaolo Abeni end: 323ab174ad8SPaolo Abeni skb_condense(skb); 3246511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3256511882cSPaolo Abeni } 3266511882cSPaolo Abeni 3276511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3286511882cSPaolo Abeni { 3296511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3306511882cSPaolo Abeni int amt, amount; 3316511882cSPaolo Abeni 33269d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3336511882cSPaolo Abeni return true; 3346511882cSPaolo Abeni 33569d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3366511882cSPaolo Abeni amt = sk_mem_pages(size); 337100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3384aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3396511882cSPaolo Abeni return false; 3406511882cSPaolo Abeni 3414aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3426511882cSPaolo Abeni return true; 343ab174ad8SPaolo Abeni } 344ab174ad8SPaolo Abeni 345ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 346ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 347ab174ad8SPaolo Abeni size_t copy_len) 348ab174ad8SPaolo Abeni { 349ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3506771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3514e637c70SFlorian Westphal struct sk_buff *tail; 352b7f653b2SFlorian Westphal bool has_rxtstamp; 3536771bfd9SFlorian Westphal 3546771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3554e637c70SFlorian Westphal 3564e637c70SFlorian Westphal skb_ext_reset(skb); 3574e637c70SFlorian Westphal skb_orphan(skb); 3584e637c70SFlorian Westphal 3599c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3606511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3619c3f94e1SPaolo Abeni goto drop; 36272f96132SPaolo Abeni 363b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 364b7f653b2SFlorian Westphal 365ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 366ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 367ab174ad8SPaolo Abeni * value 368ab174ad8SPaolo Abeni */ 369ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 370ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 371ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 372b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 373ab174ad8SPaolo Abeni 374ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 375ab174ad8SPaolo Abeni /* in sequence */ 376917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3774e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3788268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 379ab174ad8SPaolo Abeni return true; 3804e637c70SFlorian Westphal 3816511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3826771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 383ab174ad8SPaolo Abeni return true; 384ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 385ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 386ab174ad8SPaolo Abeni return false; 387ab174ad8SPaolo Abeni } 388ab174ad8SPaolo Abeni 389ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 390ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 391ab174ad8SPaolo Abeni */ 39206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3939c3f94e1SPaolo Abeni drop: 394ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 395ab174ad8SPaolo Abeni return false; 3966771bfd9SFlorian Westphal } 3976771bfd9SFlorian Westphal 39816a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 39916a9a9daSMat Martineau { 40016a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 40116a9a9daSMat Martineau 40216a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 40316a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 40416a9a9daSMat Martineau } 40516a9a9daSMat Martineau 406e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 407e16163b6SPaolo Abeni { 408e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 409e16163b6SPaolo Abeni return; 410e16163b6SPaolo Abeni 411e16163b6SPaolo Abeni sk->sk_state_change(sk); 412e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 413e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 414e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 415e16163b6SPaolo Abeni else 416e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 417e16163b6SPaolo Abeni } 418e16163b6SPaolo Abeni 4196e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4206e628cd3SPaolo Abeni { 4216e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4226e628cd3SPaolo Abeni 4236e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 4246e628cd3SPaolo Abeni ((1 << sk->sk_state) & 4256e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4266e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4276e628cd3SPaolo Abeni } 4286e628cd3SPaolo Abeni 42916a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 43016a9a9daSMat Martineau { 43116a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 43216a9a9daSMat Martineau 43316a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4346e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 43516a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 43616a9a9daSMat Martineau 43716a9a9daSMat Martineau switch (sk->sk_state) { 43816a9a9daSMat Martineau case TCP_FIN_WAIT1: 43916a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 44016a9a9daSMat Martineau break; 44116a9a9daSMat Martineau case TCP_CLOSING: 44216a9a9daSMat Martineau case TCP_LAST_ACK: 44316a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 44416a9a9daSMat Martineau break; 44516a9a9daSMat Martineau } 44616a9a9daSMat Martineau 447e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 44816a9a9daSMat Martineau } 44916a9a9daSMat Martineau } 45016a9a9daSMat Martineau 4513721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4523721b9b6SMat Martineau { 4533721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4543721b9b6SMat Martineau 4553721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4563721b9b6SMat Martineau ((1 << sk->sk_state) & 4573721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4583721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4593721b9b6SMat Martineau 4603721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4613721b9b6SMat Martineau if (seq) 4623721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4633721b9b6SMat Martineau 4643721b9b6SMat Martineau return true; 4653721b9b6SMat Martineau } 4663721b9b6SMat Martineau } 4673721b9b6SMat Martineau 4683721b9b6SMat Martineau return false; 4693721b9b6SMat Martineau } 4703721b9b6SMat Martineau 4716477dd39SMat Martineau static void mptcp_set_datafin_timeout(const struct sock *sk) 4726477dd39SMat Martineau { 4736477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 474877d11f0SMat Martineau u32 retransmits; 4756477dd39SMat Martineau 476877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 477877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 478877d11f0SMat Martineau 479877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4806477dd39SMat Martineau } 4816477dd39SMat Martineau 48233d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4833721b9b6SMat Martineau { 4843721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4853721b9b6SMat Martineau } 4863721b9b6SMat Martineau 48733d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 48833d41c9cSPaolo Abeni { 48933d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 49033d41c9cSPaolo Abeni 49171b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 49271b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 49333d41c9cSPaolo Abeni } 49433d41c9cSPaolo Abeni 49533d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 49633d41c9cSPaolo Abeni { 49733d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 49833d41c9cSPaolo Abeni long tout = 0; 49933d41c9cSPaolo Abeni 50033d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 50133d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 50233d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 50333d41c9cSPaolo Abeni } 50433d41c9cSPaolo Abeni 50576a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 506fd897679SPaolo Abeni { 507fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 50820bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 509fd897679SPaolo Abeni } 510fd897679SPaolo Abeni 511a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 512a6574302SMat Martineau { 513a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 514a6574302SMat Martineau tcp_send_ack(ssk); 515a6574302SMat Martineau } 516a6574302SMat Martineau 517f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5187ed90803SPaolo Abeni { 51975e908c3SPaolo Abeni bool slow; 5207ed90803SPaolo Abeni 52175e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 522a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 52375e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 524fd897679SPaolo Abeni } 525340fa666SMat Martineau 526340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 527340fa666SMat Martineau { 528340fa666SMat Martineau struct mptcp_subflow_context *subflow; 529340fa666SMat Martineau 530340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 531340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 532ea4ca586SPaolo Abeni } 533ea4ca586SPaolo Abeni 534fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 535fd897679SPaolo Abeni { 53675e908c3SPaolo Abeni bool slow; 537fd897679SPaolo Abeni 53875e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 539fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 540fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 54175e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 542fde56eeaSPaolo Abeni } 543fde56eeaSPaolo Abeni 544fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 545fde56eeaSPaolo Abeni { 546fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 547c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 548fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 549fde56eeaSPaolo Abeni 550fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 551fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 552fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 553fde56eeaSPaolo Abeni (rx_empty && ack_pending & 554fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 555ea4ca586SPaolo Abeni } 556fd897679SPaolo Abeni 557fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 558fd897679SPaolo Abeni { 559e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 560fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 561e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 562fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 563fde56eeaSPaolo Abeni bool cleanup, rx_empty; 564e3859603SPaolo Abeni 565fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 566ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 567fd897679SPaolo Abeni 568fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 569fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 570fd897679SPaolo Abeni 571fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 572fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5737ed90803SPaolo Abeni } 5747ed90803SPaolo Abeni } 5757ed90803SPaolo Abeni 5767ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5773721b9b6SMat Martineau { 5783721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5793721b9b6SMat Martineau u64 rcv_data_fin_seq; 5807ed90803SPaolo Abeni bool ret = false; 5813721b9b6SMat Martineau 582781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5837ed90803SPaolo Abeni return ret; 5843721b9b6SMat Martineau 5853721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5863721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5873721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5883721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5893721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5903721b9b6SMat Martineau * the msk state. 5913721b9b6SMat Martineau * 5923721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5933721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5943721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5953721b9b6SMat Martineau * when catching up. 5963721b9b6SMat Martineau */ 5973721b9b6SMat Martineau 5983721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 599917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 6003721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 6013721b9b6SMat Martineau 6023721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 60316a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 6043721b9b6SMat Martineau 6053721b9b6SMat Martineau switch (sk->sk_state) { 6063721b9b6SMat Martineau case TCP_ESTABLISHED: 6073721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6083721b9b6SMat Martineau break; 6093721b9b6SMat Martineau case TCP_FIN_WAIT1: 6103721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6113721b9b6SMat Martineau break; 6123721b9b6SMat Martineau case TCP_FIN_WAIT2: 6133721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6143721b9b6SMat Martineau break; 6153721b9b6SMat Martineau default: 6163721b9b6SMat Martineau /* Other states not expected */ 6173721b9b6SMat Martineau WARN_ON_ONCE(1); 6183721b9b6SMat Martineau break; 6193721b9b6SMat Martineau } 6203721b9b6SMat Martineau 6217ed90803SPaolo Abeni ret = true; 622fd897679SPaolo Abeni mptcp_send_ack(msk); 623e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6243721b9b6SMat Martineau } 6257ed90803SPaolo Abeni return ret; 6263721b9b6SMat Martineau } 6273721b9b6SMat Martineau 6286771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6296771bfd9SFlorian Westphal struct sock *ssk, 6306771bfd9SFlorian Westphal unsigned int *bytes) 6316771bfd9SFlorian Westphal { 6326771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 633600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6346771bfd9SFlorian Westphal unsigned int moved = 0; 6356771bfd9SFlorian Westphal bool more_data_avail; 6366771bfd9SFlorian Westphal struct tcp_sock *tp; 6376771bfd9SFlorian Westphal bool done = false; 63813c7ba0cSFlorian Westphal int sk_rbuf; 63913c7ba0cSFlorian Westphal 64013c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 64113c7ba0cSFlorian Westphal 64213c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 64313c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 64413c7ba0cSFlorian Westphal 64513c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 64613c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 64713c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 64813c7ba0cSFlorian Westphal } 64913c7ba0cSFlorian Westphal } 650600911ffSFlorian Westphal 651ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6526771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6536771bfd9SFlorian Westphal do { 6546771bfd9SFlorian Westphal u32 map_remaining, offset; 6556771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6566771bfd9SFlorian Westphal struct sk_buff *skb; 6576771bfd9SFlorian Westphal bool fin; 6586771bfd9SFlorian Westphal 6596771bfd9SFlorian Westphal /* try to move as much data as available */ 6606771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6616771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6626771bfd9SFlorian Westphal 6636771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 664d9fb8c50SPaolo Abeni if (!skb) { 665d89e3ed7SPaolo Abeni /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 666d89e3ed7SPaolo Abeni * a different CPU can have already processed the pending 667d89e3ed7SPaolo Abeni * data, stop here or we can enter an infinite loop 668d9fb8c50SPaolo Abeni */ 669d9fb8c50SPaolo Abeni if (!moved) 670d9fb8c50SPaolo Abeni done = true; 6716771bfd9SFlorian Westphal break; 672d9fb8c50SPaolo Abeni } 6736771bfd9SFlorian Westphal 674e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 675d89e3ed7SPaolo Abeni /* Under fallback skbs have no MPTCP extension and TCP could 676d89e3ed7SPaolo Abeni * collapse them between the dummy map creation and the 677d89e3ed7SPaolo Abeni * current dequeue. Be sure to adjust the map size. 678e1ff9e82SDavide Caratti */ 679e1ff9e82SDavide Caratti map_remaining = skb->len; 680e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 681e1ff9e82SDavide Caratti } 682e1ff9e82SDavide Caratti 6836771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6846771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6856771bfd9SFlorian Westphal if (fin) { 6866771bfd9SFlorian Westphal done = true; 6876771bfd9SFlorian Westphal seq++; 6886771bfd9SFlorian Westphal } 6896771bfd9SFlorian Westphal 6906771bfd9SFlorian Westphal if (offset < skb->len) { 6916771bfd9SFlorian Westphal size_t len = skb->len - offset; 6926771bfd9SFlorian Westphal 6936771bfd9SFlorian Westphal if (tp->urg_data) 6946771bfd9SFlorian Westphal done = true; 6956771bfd9SFlorian Westphal 696ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6976771bfd9SFlorian Westphal moved += len; 698ab174ad8SPaolo Abeni seq += len; 6996771bfd9SFlorian Westphal 7006771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 7016771bfd9SFlorian Westphal break; 7026771bfd9SFlorian Westphal } else { 7036771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 7046771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 7056771bfd9SFlorian Westphal done = true; 7066771bfd9SFlorian Westphal } 7076771bfd9SFlorian Westphal 7086771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7096771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 710600911ffSFlorian Westphal 71113c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 712600911ffSFlorian Westphal done = true; 713600911ffSFlorian Westphal break; 714600911ffSFlorian Westphal } 7156771bfd9SFlorian Westphal } while (more_data_avail); 7166771bfd9SFlorian Westphal 7176719331cSPaolo Abeni *bytes += moved; 7186771bfd9SFlorian Westphal return done; 7196771bfd9SFlorian Westphal } 7206771bfd9SFlorian Westphal 72187952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 722ab174ad8SPaolo Abeni { 723ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 724ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 725ab174ad8SPaolo Abeni bool moved = false; 726ab174ad8SPaolo Abeni struct rb_node *p; 727ab174ad8SPaolo Abeni u64 end_seq; 728ab174ad8SPaolo Abeni 729ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 73006242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 731ab174ad8SPaolo Abeni while (p) { 732ab174ad8SPaolo Abeni skb = rb_to_skb(p); 733ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 734ab174ad8SPaolo Abeni break; 735ab174ad8SPaolo Abeni 736ab174ad8SPaolo Abeni p = rb_next(p); 737ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 738ab174ad8SPaolo Abeni 739ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 740ab174ad8SPaolo Abeni msk->ack_seq))) { 741ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 74206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 743ab174ad8SPaolo Abeni continue; 744ab174ad8SPaolo Abeni } 745ab174ad8SPaolo Abeni 746ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 747ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 748ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 749ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 750ab174ad8SPaolo Abeni 751ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 75206242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 75306242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 75406242e44SPaolo Abeni delta); 755ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7562c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 757ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 758ab174ad8SPaolo Abeni } 759ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 760ab174ad8SPaolo Abeni moved = true; 761ab174ad8SPaolo Abeni } 762ab174ad8SPaolo Abeni return moved; 763ab174ad8SPaolo Abeni } 764ab174ad8SPaolo Abeni 7652e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7662e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7672e52213cSFlorian Westphal */ 76899d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7692e52213cSFlorian Westphal { 7702e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7712e52213cSFlorian Westphal unsigned int moved = 0; 7722e52213cSFlorian Westphal 7732e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 77487952603SPaolo Abeni __mptcp_ofo_queue(msk); 775499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 776499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 777499ada50SPaolo Abeni __mptcp_error_report(sk); 778499ada50SPaolo Abeni else 779e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 780499ada50SPaolo Abeni } 781ab174ad8SPaolo Abeni 782ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 783ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 784ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 785ab174ad8SPaolo Abeni * do it. 786ab174ad8SPaolo Abeni */ 787ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 788ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 78999d1055cSPaolo Abeni return moved > 0; 7902e52213cSFlorian Westphal } 7912e52213cSFlorian Westphal 7922e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 793101f6f85SFlorian Westphal { 7946719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 795101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 79613c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 797101f6f85SFlorian Westphal 798d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 799d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 800d7b1bfd0SPaolo Abeni * more data to the msk receive queue 801d7b1bfd0SPaolo Abeni */ 802d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 803d7b1bfd0SPaolo Abeni return; 804d7b1bfd0SPaolo Abeni 80513c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 80613c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 80713c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 80813c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 80913c7ba0cSFlorian Westphal 81099d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 811ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 812ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 81399d1055cSPaolo Abeni return; 814ce599c51SPaolo Abeni } 8152e52213cSFlorian Westphal 81699d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 81799d1055cSPaolo Abeni mptcp_data_lock(sk); 818612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 819101f6f85SFlorian Westphal sk->sk_data_ready(sk); 820612f71d7SPaolo Abeni 82199d1055cSPaolo Abeni mptcp_data_unlock(sk); 82299d1055cSPaolo Abeni } 823101f6f85SFlorian Westphal 8243e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 825ec3edaa7SPeter Krystad { 8263e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8275cf92bbaSPaolo Abeni 8283e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 82978962489SFlorian Westphal return false; 830ec3edaa7SPeter Krystad 8313e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8323e501490SPaolo Abeni * at close time 8333e501490SPaolo Abeni */ 8343e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8353e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 83678962489SFlorian Westphal 8373e501490SPaolo Abeni mptcp_propagate_sndbuf((struct sock *)msk, ssk); 8383e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 8393e501490SPaolo Abeni return true; 84078962489SFlorian Westphal } 84178962489SFlorian Westphal 8423e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk) 84378962489SFlorian Westphal { 8443e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8453e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 84678962489SFlorian Westphal 8473e501490SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 8483e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8493e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8503e501490SPaolo Abeni 8513e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8523e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8533e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8543e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 85578962489SFlorian Westphal } 856ec3edaa7SPeter Krystad } 857ec3edaa7SPeter Krystad 858b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 859b51f9b80SPaolo Abeni { 860b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 861b51f9b80SPaolo Abeni } 862b51f9b80SPaolo Abeni 863b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 864b51f9b80SPaolo Abeni { 865b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 866b51f9b80SPaolo Abeni unsigned long tout; 867b51f9b80SPaolo Abeni 868e16163b6SPaolo Abeni /* prevent rescheduling on close */ 869e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 870e16163b6SPaolo Abeni return; 871e16163b6SPaolo Abeni 87233d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 873b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 874b51f9b80SPaolo Abeni } 875b51f9b80SPaolo Abeni 876ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 877ba8f48f7SPaolo Abeni { 878ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 879ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 880ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 881ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 882ba8f48f7SPaolo Abeni */ 883ba8f48f7SPaolo Abeni sock_hold(sk); 884ba8f48f7SPaolo Abeni return true; 885ba8f48f7SPaolo Abeni } 886ba8f48f7SPaolo Abeni return false; 887ba8f48f7SPaolo Abeni } 888ba8f48f7SPaolo Abeni 88959832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 89059832e24SFlorian Westphal { 891ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 892ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 89359832e24SFlorian Westphal } 89459832e24SFlorian Westphal 8955969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8965969856aSPaolo Abeni { 8975969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 8985969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 8995969856aSPaolo Abeni int receivers = 0; 9005969856aSPaolo Abeni 9015969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 9025969856aSPaolo Abeni receivers += !subflow->rx_eof; 903e16163b6SPaolo Abeni if (receivers) 904e16163b6SPaolo Abeni return; 9055969856aSPaolo Abeni 906e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 9075969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 9085969856aSPaolo Abeni * to msk, when all subflows agree on it 9095969856aSPaolo Abeni */ 9105969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 9115969856aSPaolo Abeni 9125969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9135969856aSPaolo Abeni sk->sk_data_ready(sk); 9145969856aSPaolo Abeni } 915e16163b6SPaolo Abeni 916e16163b6SPaolo Abeni switch (sk->sk_state) { 917e16163b6SPaolo Abeni case TCP_ESTABLISHED: 918e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 919e16163b6SPaolo Abeni break; 920e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 92126aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 92226aa2314SPaolo Abeni break; 92326aa2314SPaolo Abeni case TCP_FIN_WAIT2: 924e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 925e16163b6SPaolo Abeni break; 926e16163b6SPaolo Abeni default: 927e16163b6SPaolo Abeni return; 928e16163b6SPaolo Abeni } 929e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9305969856aSPaolo Abeni } 9315969856aSPaolo Abeni 9327a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9337a6a6cbcSPaolo Abeni { 9347a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9357a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 9367a6a6cbcSPaolo Abeni 9377a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 9387a6a6cbcSPaolo Abeni 9397a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 94099d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9417a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9427a6a6cbcSPaolo Abeni } 9437a6a6cbcSPaolo Abeni 9447a6a6cbcSPaolo Abeni return NULL; 9457a6a6cbcSPaolo Abeni } 9467a6a6cbcSPaolo Abeni 9473f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 94857040755SPaolo Abeni const struct sk_buff *skb, 94957040755SPaolo Abeni const struct mptcp_ext *mpext) 9506d0060f6SMat Martineau { 95157040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 95257040755SPaolo Abeni return false; 95357040755SPaolo Abeni 9545a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9555a369ca6SPaolo Abeni * mapping has not been xmitted yet 9565a369ca6SPaolo Abeni */ 9575a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9585a369ca6SPaolo Abeni !mpext->frozen; 95957040755SPaolo Abeni } 96057040755SPaolo Abeni 96129249eacSPaolo Abeni /* we can append data to the given data frag if: 96229249eacSPaolo Abeni * - there is space available in the backing page_frag 96329249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 96429249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 96529249eacSPaolo Abeni */ 96618b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 96718b683bfSPaolo Abeni const struct page_frag *pfrag, 96818b683bfSPaolo Abeni const struct mptcp_data_frag *df) 96918b683bfSPaolo Abeni { 97018b683bfSPaolo Abeni return df && pfrag->page == df->page && 971d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 97229249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 97318b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 97418b683bfSPaolo Abeni } 97518b683bfSPaolo Abeni 976d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 97718b683bfSPaolo Abeni { 978d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9797948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 980d027236cSPaolo Abeni } 981d027236cSPaolo Abeni 982d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 983d027236cSPaolo Abeni { 984d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 985d027236cSPaolo Abeni 98618b683bfSPaolo Abeni list_del(&dfrag->list); 987d027236cSPaolo Abeni dfrag_uncharge(sk, len); 98818b683bfSPaolo Abeni put_page(dfrag->page); 98918b683bfSPaolo Abeni } 99018b683bfSPaolo Abeni 9916e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 99218b683bfSPaolo Abeni { 99318b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 99418b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 995e1ff9e82SDavide Caratti u64 snd_una; 996e1ff9e82SDavide Caratti 997e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 998e1ff9e82SDavide Caratti * plain TCP 999e1ff9e82SDavide Caratti */ 1000e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10017439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10027439d687SPaolo Abeni 10037439d687SPaolo Abeni snd_una = msk->snd_una; 100418b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 100518b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 100618b683bfSPaolo Abeni break; 100718b683bfSPaolo Abeni 10081e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10091e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10101e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1011d9ca1de8SPaolo Abeni break; 10121e1d9d6fSPaolo Abeni 10131e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10141e1d9d6fSPaolo Abeni } 10151e1d9d6fSPaolo Abeni 1016d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1017d027236cSPaolo Abeni } 1018d027236cSPaolo Abeni 10197948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10207948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 102153eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 102253eb4c38SPaolo Abeni 10231e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10241e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10251e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 102653eb4c38SPaolo Abeni goto out; 10271e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10281e1d9d6fSPaolo Abeni goto out; 10291e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10301e1d9d6fSPaolo Abeni } 10317948f6ccSFlorian Westphal 10327948f6ccSFlorian Westphal dfrag->data_seq += delta; 103353eb4c38SPaolo Abeni dfrag->offset += delta; 10347948f6ccSFlorian Westphal dfrag->data_len -= delta; 1035d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10367948f6ccSFlorian Westphal 10377948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10387948f6ccSFlorian Westphal } 10397948f6ccSFlorian Westphal 10401e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10411e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10421e1d9d6fSPaolo Abeni msk->recovery = false; 10431e1d9d6fSPaolo Abeni 104453eb4c38SPaolo Abeni out: 10453241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10463241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 104733d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10486e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10496e628cd3SPaolo Abeni } else { 10506e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10516e628cd3SPaolo Abeni } 10526e628cd3SPaolo Abeni } 10536e628cd3SPaolo Abeni 1054417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1055417789dfSPaolo Abeni { 1056765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1057765ff425SPaolo Abeni 1058417789dfSPaolo Abeni __mptcp_clean_una(sk); 1059417789dfSPaolo Abeni mptcp_write_space(sk); 1060417789dfSPaolo Abeni } 1061417789dfSPaolo Abeni 1062b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1063b5941f06SPaolo Abeni { 1064b5941f06SPaolo Abeni mptcp_data_lock(sk); 1065b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1066b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1067b5941f06SPaolo Abeni } 1068b5941f06SPaolo Abeni 1069724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 107018b683bfSPaolo Abeni { 1071d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1072d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1073d9ca1de8SPaolo Abeni bool first = true; 1074d9ca1de8SPaolo Abeni 107518b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1076d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1077d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1078d9ca1de8SPaolo Abeni 1079d9ca1de8SPaolo Abeni if (first) 1080d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1081d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1082d9ca1de8SPaolo Abeni first = false; 1083d9ca1de8SPaolo Abeni } 1084724cfd2eSPaolo Abeni } 1085724cfd2eSPaolo Abeni 1086724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1087724cfd2eSPaolo Abeni * data 1088724cfd2eSPaolo Abeni */ 1089724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1090724cfd2eSPaolo Abeni { 1091724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1092724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1093724cfd2eSPaolo Abeni return true; 1094724cfd2eSPaolo Abeni 1095724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 109618b683bfSPaolo Abeni return false; 109718b683bfSPaolo Abeni } 109818b683bfSPaolo Abeni 109918b683bfSPaolo Abeni static struct mptcp_data_frag * 110018b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 110118b683bfSPaolo Abeni int orig_offset) 110218b683bfSPaolo Abeni { 110318b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 110418b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 110518b683bfSPaolo Abeni 110618b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 110718b683bfSPaolo Abeni dfrag->data_len = 0; 110818b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 110918b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 111018b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1111d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 111218b683bfSPaolo Abeni dfrag->page = pfrag->page; 111318b683bfSPaolo Abeni 111418b683bfSPaolo Abeni return dfrag; 111518b683bfSPaolo Abeni } 111618b683bfSPaolo Abeni 1117caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1118caf971dfSPaolo Abeni int mss_now; 1119caf971dfSPaolo Abeni int size_goal; 1120d9ca1de8SPaolo Abeni u16 limit; 1121d9ca1de8SPaolo Abeni u16 sent; 1122d9ca1de8SPaolo Abeni unsigned int flags; 11231094c6feSPaolo Abeni bool data_lock_held; 1124caf971dfSPaolo Abeni }; 1125caf971dfSPaolo Abeni 1126b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1127b713d006SPaolo Abeni u64 data_seq, int avail_size) 11286f8a612aSFlorian Westphal { 11296f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1130b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11316f8a612aSFlorian Westphal 11326f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11336f8a612aSFlorian Westphal return avail_size; 11346f8a612aSFlorian Westphal 1135b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1136b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11376f8a612aSFlorian Westphal 113892be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1139b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 114092be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 114192be2f52SPaolo Abeni } 11426f8a612aSFlorian Westphal 11436f8a612aSFlorian Westphal return avail_size; 11446f8a612aSFlorian Westphal } 11456f8a612aSFlorian Westphal 1146724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1147724cfd2eSPaolo Abeni { 1148724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1149724cfd2eSPaolo Abeni 1150724cfd2eSPaolo Abeni if (!mpext) 1151724cfd2eSPaolo Abeni return false; 1152724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1153724cfd2eSPaolo Abeni return true; 1154724cfd2eSPaolo Abeni } 1155724cfd2eSPaolo Abeni 11566e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1157724cfd2eSPaolo Abeni { 1158724cfd2eSPaolo Abeni struct sk_buff *skb; 1159724cfd2eSPaolo Abeni 11606e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1161724cfd2eSPaolo Abeni if (likely(skb)) { 11626e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1163724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1164a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1165f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1166724cfd2eSPaolo Abeni return skb; 1167724cfd2eSPaolo Abeni } 1168724cfd2eSPaolo Abeni __kfree_skb(skb); 1169724cfd2eSPaolo Abeni } else { 1170724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1171724cfd2eSPaolo Abeni } 1172724cfd2eSPaolo Abeni return NULL; 1173724cfd2eSPaolo Abeni } 1174724cfd2eSPaolo Abeni 1175f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1176724cfd2eSPaolo Abeni { 1177724cfd2eSPaolo Abeni struct sk_buff *skb; 1178724cfd2eSPaolo Abeni 11796e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1180724cfd2eSPaolo Abeni if (!skb) 1181f70cad10SPaolo Abeni return NULL; 1182724cfd2eSPaolo Abeni 1183724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1184f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1185f70cad10SPaolo Abeni return skb; 1186724cfd2eSPaolo Abeni } 11873ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1188724cfd2eSPaolo Abeni kfree_skb(skb); 1189f70cad10SPaolo Abeni return NULL; 1190724cfd2eSPaolo Abeni } 1191724cfd2eSPaolo Abeni 1192f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1193724cfd2eSPaolo Abeni { 11941094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1195724cfd2eSPaolo Abeni 11961094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1197724cfd2eSPaolo Abeni } 1198724cfd2eSPaolo Abeni 1199d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1200d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1201d0cc2987SGeliang Tang */ 1202d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1203d0cc2987SGeliang Tang { 1204d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1205d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1206d0cc2987SGeliang Tang int offset = skb->len - added; 1207d0cc2987SGeliang Tang 1208d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1209d0cc2987SGeliang Tang } 1210d0cc2987SGeliang Tang 12111e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12121e39e5a3SGeliang Tang struct sock *ssk, 12131e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12141e39e5a3SGeliang Tang { 12151e39e5a3SGeliang Tang if (!mpext) 12161e39e5a3SGeliang Tang return; 12171e39e5a3SGeliang Tang 12181e39e5a3SGeliang Tang mpext->infinite_map = 1; 12191e39e5a3SGeliang Tang mpext->data_len = 0; 12201e39e5a3SGeliang Tang 1221104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12221e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12231e39e5a3SGeliang Tang pr_fallback(msk); 1224d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12251e39e5a3SGeliang Tang } 12261e39e5a3SGeliang Tang 122757040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1228d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1229caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 123057040755SPaolo Abeni { 1231d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1232f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12336d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12346f8a612aSFlorian Westphal bool zero_window_probe = false; 12356d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1236f70cad10SPaolo Abeni bool can_coalesce = false; 1237f70cad10SPaolo Abeni bool reuse_skb = true; 1238f70cad10SPaolo Abeni struct sk_buff *skb; 1239f70cad10SPaolo Abeni size_t copy; 1240f70cad10SPaolo Abeni int i; 12416d0060f6SMat Martineau 1242e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1243d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12446d0060f6SMat Martineau 1245f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1246f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1247f70cad10SPaolo Abeni return 0; 1248f70cad10SPaolo Abeni 1249c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1250c886d702SPaolo Abeni return -EAGAIN; 1251c886d702SPaolo Abeni 1252d9ca1de8SPaolo Abeni /* compute send limit */ 1253d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1254f70cad10SPaolo Abeni copy = info->size_goal; 1255f70cad10SPaolo Abeni 125657040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1257f70cad10SPaolo Abeni if (skb && copy > skb->len) { 125857040755SPaolo Abeni /* Limit the write to the size available in the 125957040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 126057040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 126157040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 126257040755SPaolo Abeni * SSN association set here 126357040755SPaolo Abeni */ 1264d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12651094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126657040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12671094c6feSPaolo Abeni goto alloc_skb; 12681094c6feSPaolo Abeni } 12691094c6feSPaolo Abeni 1270f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1271f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1272657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1273f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1274f70cad10SPaolo Abeni goto alloc_skb; 127515e6ca97SPaolo Abeni } 127618b683bfSPaolo Abeni 1277f70cad10SPaolo Abeni copy -= skb->len; 1278f70cad10SPaolo Abeni } else { 12791094c6feSPaolo Abeni alloc_skb: 1280f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1281f70cad10SPaolo Abeni if (!skb) 1282f70cad10SPaolo Abeni return -ENOMEM; 1283f70cad10SPaolo Abeni 1284f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1285f70cad10SPaolo Abeni reuse_skb = false; 1286f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1287f70cad10SPaolo Abeni } 12881094c6feSPaolo Abeni 12896f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1290b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1291f70cad10SPaolo Abeni if (copy == 0) { 12927439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12937439d687SPaolo Abeni 1294f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 129527728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12966f8a612aSFlorian Westphal return 0; 12976f8a612aSFlorian Westphal } 12986f8a612aSFlorian Westphal 1299f70cad10SPaolo Abeni zero_window_probe = true; 1300f70cad10SPaolo Abeni data_seq = snd_una - 1; 1301f70cad10SPaolo Abeni copy = 1; 130257040755SPaolo Abeni 1303f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1304f70cad10SPaolo Abeni * ssk write queue 1305f70cad10SPaolo Abeni */ 1306f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1307f70cad10SPaolo Abeni } 1308f70cad10SPaolo Abeni 1309f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1310f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 131127728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1312e2223995SPaolo Abeni return -ENOMEM; 131335759383SFlorian Westphal } 131418b683bfSPaolo Abeni 1315f70cad10SPaolo Abeni if (can_coalesce) { 1316f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1317f70cad10SPaolo Abeni } else { 1318f70cad10SPaolo Abeni get_page(dfrag->page); 1319f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 132057040755SPaolo Abeni } 132157040755SPaolo Abeni 1322f70cad10SPaolo Abeni skb->len += copy; 1323f70cad10SPaolo Abeni skb->data_len += copy; 1324f70cad10SPaolo Abeni skb->truesize += copy; 1325f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1326f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1327f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1328f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1329f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1330f70cad10SPaolo Abeni 1331f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1332f70cad10SPaolo Abeni if (reuse_skb) { 1333f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1334f70cad10SPaolo Abeni mpext->data_len += copy; 1335f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1336f70cad10SPaolo Abeni goto out; 1337724cfd2eSPaolo Abeni } 13386d0060f6SMat Martineau 13396d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1340d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13416d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1342f70cad10SPaolo Abeni mpext->data_len = copy; 13436d0060f6SMat Martineau mpext->use_map = 1; 13446d0060f6SMat Martineau mpext->dsn64 = 1; 13456d0060f6SMat Martineau 13466d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13476d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13486d0060f6SMat Martineau mpext->dsn64); 13496d0060f6SMat Martineau 13506f8a612aSFlorian Westphal if (zero_window_probe) { 1351f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13526f8a612aSFlorian Westphal mpext->frozen = 1; 1353d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1354f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13556f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1356d0cc2987SGeliang Tang return 0; 13576f8a612aSFlorian Westphal } 135857040755SPaolo Abeni out: 1359d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1360f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13611e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13621e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13630eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1364f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1365f70cad10SPaolo Abeni return copy; 13666d0060f6SMat Martineau } 13676d0060f6SMat Martineau 1368d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1369d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1370d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1371d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1372d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1373d5f49190SPaolo Abeni 1374d5f49190SPaolo Abeni struct subflow_send_info { 1375d5f49190SPaolo Abeni struct sock *ssk; 13763ce0852cSPaolo Abeni u64 linger_time; 1377d5f49190SPaolo Abeni }; 1378d5f49190SPaolo Abeni 1379ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1380ff5a0b42SPaolo Abeni { 1381ff5a0b42SPaolo Abeni if (!subflow->stale) 1382ff5a0b42SPaolo Abeni return; 1383ff5a0b42SPaolo Abeni 1384ff5a0b42SPaolo Abeni subflow->stale = 0; 1385fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1386ff5a0b42SPaolo Abeni } 1387ff5a0b42SPaolo Abeni 1388ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1389ff5a0b42SPaolo Abeni { 1390ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1391ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1392ff5a0b42SPaolo Abeni 1393ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1394ff5a0b42SPaolo Abeni return false; 1395ff5a0b42SPaolo Abeni 1396ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1397ff5a0b42SPaolo Abeni } 1398ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1399ff5a0b42SPaolo Abeni } 1400ff5a0b42SPaolo Abeni 14013ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 14023ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 14033ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 14043ce0852cSPaolo Abeni 140533d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140633d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140733d41c9cSPaolo Abeni * additionally updates the rtx timeout 140833d41c9cSPaolo Abeni */ 14095cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1410f296234cSPeter Krystad { 14113ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1412f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 141333d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14143ce0852cSPaolo Abeni u32 pace, burst, wmem; 1415d5f49190SPaolo Abeni int i, nr_active = 0; 1416d5f49190SPaolo Abeni struct sock *ssk; 14173ce0852cSPaolo Abeni u64 linger_time; 141833d41c9cSPaolo Abeni long tout = 0; 1419f296234cSPeter Krystad 142033d41c9cSPaolo Abeni sock_owned_by_me(sk); 1421f296234cSPeter Krystad 1422d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1423d5f49190SPaolo Abeni if (!msk->first) 1424f296234cSPeter Krystad return NULL; 1425c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1426c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1427f296234cSPeter Krystad } 1428f296234cSPeter Krystad 1429d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1430d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1431d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 143233d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 143333d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1434d5f49190SPaolo Abeni return msk->last_snd; 143533d41c9cSPaolo Abeni } 1436f296234cSPeter Krystad 1437d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14383ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1439d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14403ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1441d5f49190SPaolo Abeni } 14423ce0852cSPaolo Abeni 1443d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1444e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1445d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1446d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1447f296234cSPeter Krystad continue; 1448d5f49190SPaolo Abeni 144933d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1450d5f49190SPaolo Abeni nr_active += !subflow->backup; 14513ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14523ce0852cSPaolo Abeni if (unlikely(!pace)) { 14533ce0852cSPaolo Abeni /* init pacing rate from socket */ 14543ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14553ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1456d5f49190SPaolo Abeni if (!pace) 1457d5f49190SPaolo Abeni continue; 14583ce0852cSPaolo Abeni } 1459d5f49190SPaolo Abeni 14603ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14613ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1462d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14633ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1464d5f49190SPaolo Abeni } 1465f296234cSPeter Krystad } 146633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1467f296234cSPeter Krystad 1468d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1469d5f49190SPaolo Abeni if (!nr_active) 14703ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1471d5f49190SPaolo Abeni 14723ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14733ce0852cSPaolo Abeni * faster flow, we need to: 14743ce0852cSPaolo Abeni * - estimate the faster flow linger time 14753ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14763ce0852cSPaolo Abeni * by the faster flow 14773ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14783ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14793ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14803ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14813ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14823ce0852cSPaolo Abeni */ 14833ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1484b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1485d5f49190SPaolo Abeni return NULL; 14863ce0852cSPaolo Abeni 1487b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14883ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1489b713d006SPaolo Abeni if (!burst) { 1490b713d006SPaolo Abeni msk->last_snd = NULL; 1491b713d006SPaolo Abeni return ssk; 1492b713d006SPaolo Abeni } 1493b713d006SPaolo Abeni 14943ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14953ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14963ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14973ce0852cSPaolo Abeni burst + wmem); 14983ce0852cSPaolo Abeni msk->last_snd = ssk; 14993ce0852cSPaolo Abeni msk->snd_burst = burst; 15003ce0852cSPaolo Abeni return ssk; 1501f296234cSPeter Krystad } 1502f296234cSPeter Krystad 1503b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1504d9ca1de8SPaolo Abeni { 1505d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1506d9ca1de8SPaolo Abeni release_sock(ssk); 1507d9ca1de8SPaolo Abeni } 1508d9ca1de8SPaolo Abeni 15090d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15100d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15110d199e43SFlorian Westphal u32 sent) 15120d199e43SFlorian Westphal { 15130d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15140d199e43SFlorian Westphal 15150d199e43SFlorian Westphal dfrag->already_sent += sent; 15160d199e43SFlorian Westphal 15170d199e43SFlorian Westphal msk->snd_burst -= sent; 15180d199e43SFlorian Westphal 15190d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15200d199e43SFlorian Westphal 15210d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15220d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15230d199e43SFlorian Westphal * old segments. 15240d199e43SFlorian Westphal * 15250d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15260d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15270d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15280d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15290d199e43SFlorian Westphal */ 15300d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15310d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15320d199e43SFlorian Westphal } 15330d199e43SFlorian Westphal 15348b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15353241a9c0SFlorian Westphal { 1536e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1537e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15383241a9c0SFlorian Westphal } 15393241a9c0SFlorian Westphal 1540ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1541d9ca1de8SPaolo Abeni { 1542d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1543d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1544d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1545d9ca1de8SPaolo Abeni .flags = flags, 1546d9ca1de8SPaolo Abeni }; 15470522b424SGeliang Tang bool do_check_data_fin = false; 1548d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 15490522b424SGeliang Tang int len; 1550d9ca1de8SPaolo Abeni 1551d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1552d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1553d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1554d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1555d9ca1de8SPaolo Abeni while (len > 0) { 1556d9ca1de8SPaolo Abeni int ret = 0; 1557d9ca1de8SPaolo Abeni 1558d9ca1de8SPaolo Abeni prev_ssk = ssk; 15595cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1560d9ca1de8SPaolo Abeni 15619758f40eSPaolo Abeni /* First check. If the ssk has changed since 15629758f40eSPaolo Abeni * the last round, release prev_ssk 1563d9ca1de8SPaolo Abeni */ 1564d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1565b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1566d9ca1de8SPaolo Abeni if (!ssk) 1567d9ca1de8SPaolo Abeni goto out; 1568d9ca1de8SPaolo Abeni 15699758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15709758f40eSPaolo Abeni * from the previous one, otherwise we are still 15719758f40eSPaolo Abeni * helding the relevant lock 15729758f40eSPaolo Abeni */ 15739758f40eSPaolo Abeni if (ssk != prev_ssk) 1574d9ca1de8SPaolo Abeni lock_sock(ssk); 1575d9ca1de8SPaolo Abeni 1576d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1577d9ca1de8SPaolo Abeni if (ret <= 0) { 1578c886d702SPaolo Abeni if (ret == -EAGAIN) 1579c886d702SPaolo Abeni continue; 1580b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1581d9ca1de8SPaolo Abeni goto out; 1582d9ca1de8SPaolo Abeni } 1583d9ca1de8SPaolo Abeni 15840522b424SGeliang Tang do_check_data_fin = true; 1585d9ca1de8SPaolo Abeni info.sent += ret; 1586d9ca1de8SPaolo Abeni len -= ret; 15870d199e43SFlorian Westphal 15880d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1589d9ca1de8SPaolo Abeni } 1590d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1591d9ca1de8SPaolo Abeni } 1592d9ca1de8SPaolo Abeni 1593d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1594d9ca1de8SPaolo Abeni if (ssk) 1595b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1596d9ca1de8SPaolo Abeni 1597d9ca1de8SPaolo Abeni out: 159833d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1599d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1600d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 16010522b424SGeliang Tang if (do_check_data_fin) 1602d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1603d9ca1de8SPaolo Abeni } 1604d9ca1de8SPaolo Abeni 16056e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 16066e628cd3SPaolo Abeni { 16076e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16081094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16091094c6feSPaolo Abeni .data_lock_held = true, 16101094c6feSPaolo Abeni }; 16116e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1612b19bc294SPaolo Abeni struct sock *xmit_ssk; 16136e628cd3SPaolo Abeni int len, copied = 0; 1614b19bc294SPaolo Abeni bool first = true; 16156e628cd3SPaolo Abeni 16166e628cd3SPaolo Abeni info.flags = 0; 16176e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16186e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16196e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16206e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16216e628cd3SPaolo Abeni while (len > 0) { 16226e628cd3SPaolo Abeni int ret = 0; 16236e628cd3SPaolo Abeni 1624b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1625b19bc294SPaolo Abeni * check for a different subflow usage only after 1626b19bc294SPaolo Abeni * spooling the first chunk of data 1627b19bc294SPaolo Abeni */ 1628b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1629b19bc294SPaolo Abeni if (!xmit_ssk) 1630b19bc294SPaolo Abeni goto out; 1631b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1632bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1633bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1634b19bc294SPaolo Abeni goto out; 1635b19bc294SPaolo Abeni } 1636b19bc294SPaolo Abeni 16376e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16386e628cd3SPaolo Abeni if (ret <= 0) 16396e628cd3SPaolo Abeni goto out; 16406e628cd3SPaolo Abeni 16416e628cd3SPaolo Abeni info.sent += ret; 16426e628cd3SPaolo Abeni copied += ret; 16436e628cd3SPaolo Abeni len -= ret; 1644b19bc294SPaolo Abeni first = false; 16450d199e43SFlorian Westphal 16460d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16476e628cd3SPaolo Abeni } 16486e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16496e628cd3SPaolo Abeni } 16506e628cd3SPaolo Abeni 16516e628cd3SPaolo Abeni out: 16526e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16536e628cd3SPaolo Abeni * not going to flush it via release_sock() 16546e628cd3SPaolo Abeni */ 16556e628cd3SPaolo Abeni if (copied) { 16566e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16576e628cd3SPaolo Abeni info.size_goal); 1658d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1659d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1660d09d818eSPaolo Abeni 16616e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16626e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16636e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16646e628cd3SPaolo Abeni } 16656e628cd3SPaolo Abeni } 16666e628cd3SPaolo Abeni 16675cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16685cf92bbaSPaolo Abeni { 16695cf92bbaSPaolo Abeni /* enable autotune */ 16705cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16715cf92bbaSPaolo Abeni 16725cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16735cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16745cf92bbaSPaolo Abeni } 16755cf92bbaSPaolo Abeni 1676*fa9e5746SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg, 1677*fa9e5746SPaolo Abeni size_t len, int *copied_syn) 1678*fa9e5746SPaolo Abeni { 1679*fa9e5746SPaolo Abeni unsigned int saved_flags = msg->msg_flags; 1680*fa9e5746SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1681*fa9e5746SPaolo Abeni int ret; 1682*fa9e5746SPaolo Abeni 1683*fa9e5746SPaolo Abeni lock_sock(ssk); 1684*fa9e5746SPaolo Abeni msg->msg_flags |= MSG_DONTWAIT; 1685*fa9e5746SPaolo Abeni msk->connect_flags = O_NONBLOCK; 1686*fa9e5746SPaolo Abeni msk->is_sendmsg = 1; 1687*fa9e5746SPaolo Abeni ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 1688*fa9e5746SPaolo Abeni msk->is_sendmsg = 0; 1689*fa9e5746SPaolo Abeni msg->msg_flags = saved_flags; 1690*fa9e5746SPaolo Abeni release_sock(ssk); 1691*fa9e5746SPaolo Abeni 1692*fa9e5746SPaolo Abeni /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1693*fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1694*fa9e5746SPaolo Abeni ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1695*fa9e5746SPaolo Abeni msg->msg_namelen, msg->msg_flags, 1); 1696*fa9e5746SPaolo Abeni 1697*fa9e5746SPaolo Abeni /* Keep the same behaviour of plain TCP: zero the copied bytes in 1698*fa9e5746SPaolo Abeni * case of any error, except timeout or signal 1699*fa9e5746SPaolo Abeni */ 1700*fa9e5746SPaolo Abeni if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1701*fa9e5746SPaolo Abeni *copied_syn = 0; 1702*fa9e5746SPaolo Abeni } 1703*fa9e5746SPaolo Abeni 1704*fa9e5746SPaolo Abeni return ret; 1705*fa9e5746SPaolo Abeni } 1706*fa9e5746SPaolo Abeni 1707f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1708f870fa0bSMat Martineau { 1709f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 171017091708SFlorian Westphal struct page_frag *pfrag; 1711d98a82a6SDmytro Shytyi struct socket *ssock; 17126d0060f6SMat Martineau size_t copied = 0; 1713caf971dfSPaolo Abeni int ret = 0; 17146d0060f6SMat Martineau long timeo; 1715f870fa0bSMat Martineau 1716987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1717987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1718f870fa0bSMat Martineau return -EOPNOTSUPP; 1719f870fa0bSMat Martineau 1720987858e5SPaolo Abeni /* silently ignore everything else */ 1721987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1722987858e5SPaolo Abeni 17236511882cSPaolo Abeni lock_sock(sk); 17241954b860SMat Martineau 1725d98a82a6SDmytro Shytyi ssock = __mptcp_nmpc_socket(msk); 1726d98a82a6SDmytro Shytyi if (unlikely(ssock && inet_sk(ssock->sk)->defer_connect)) { 1727d98a82a6SDmytro Shytyi int copied_syn = 0; 1728d98a82a6SDmytro Shytyi 1729*fa9e5746SPaolo Abeni ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn); 1730d98a82a6SDmytro Shytyi copied += copied_syn; 1731*fa9e5746SPaolo Abeni if (ret == -EINPROGRESS && copied_syn > 0) 1732d98a82a6SDmytro Shytyi goto out; 1733*fa9e5746SPaolo Abeni else if (ret) 173469800e51SPaolo Abeni goto do_error; 1735d98a82a6SDmytro Shytyi } 1736d98a82a6SDmytro Shytyi 17371954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17381954b860SMat Martineau 17391954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17401954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17411954b860SMat Martineau if (ret) 174269800e51SPaolo Abeni goto do_error; 17431954b860SMat Martineau } 17441954b860SMat Martineau 174569800e51SPaolo Abeni ret = -EPIPE; 174669800e51SPaolo Abeni if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 174769800e51SPaolo Abeni goto do_error; 174869800e51SPaolo Abeni 174917091708SFlorian Westphal pfrag = sk_page_frag(sk); 175018b683bfSPaolo Abeni 1751d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1752724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1753d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1754d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1755d9ca1de8SPaolo Abeni size_t psize, offset; 1756d9ca1de8SPaolo Abeni 1757d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1758d9ca1de8SPaolo Abeni * page allocator 1759fb529e62SFlorian Westphal */ 1760d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1761d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1762d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1763d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1764d9ca1de8SPaolo Abeni goto wait_for_memory; 17656e628cd3SPaolo Abeni 1766d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1767d9ca1de8SPaolo Abeni goto wait_for_memory; 1768d9ca1de8SPaolo Abeni 1769d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1770d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1771fb529e62SFlorian Westphal } 1772fb529e62SFlorian Westphal 1773d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1774d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1775d9ca1de8SPaolo Abeni * anyway 1776d9ca1de8SPaolo Abeni */ 1777d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1778d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1779d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1780724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17818ce568edSPaolo Abeni 17826511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1783d9ca1de8SPaolo Abeni goto wait_for_memory; 1784d9ca1de8SPaolo Abeni 1785d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1786d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1787d9ca1de8SPaolo Abeni ret = -EFAULT; 178869800e51SPaolo Abeni goto do_error; 1789d9ca1de8SPaolo Abeni } 1790d9ca1de8SPaolo Abeni 1791d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17926511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1793d9ca1de8SPaolo Abeni copied += psize; 1794d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1795d9ca1de8SPaolo Abeni frag_truesize += psize; 1796d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1797d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1798d9ca1de8SPaolo Abeni 1799d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1800d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1801d9ca1de8SPaolo Abeni */ 1802d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1803d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1804d9ca1de8SPaolo Abeni get_page(dfrag->page); 1805d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1806d9ca1de8SPaolo Abeni if (!msk->first_pending) 1807d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1808d9ca1de8SPaolo Abeni } 1809e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1810d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1811d9ca1de8SPaolo Abeni !dfrag_collapsed); 1812d9ca1de8SPaolo Abeni 1813d9ca1de8SPaolo Abeni continue; 1814d9ca1de8SPaolo Abeni 1815d9ca1de8SPaolo Abeni wait_for_memory: 18165cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1817c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1818f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1819f296234cSPeter Krystad if (ret) 182069800e51SPaolo Abeni goto do_error; 1821cec37a6eSPeter Krystad } 1822cec37a6eSPeter Krystad 182313e16037SPaolo Abeni if (copied) 1824c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1825da51aef5SPaolo Abeni 18261954b860SMat Martineau out: 1827cec37a6eSPeter Krystad release_sock(sk); 182869800e51SPaolo Abeni return copied; 182969800e51SPaolo Abeni 183069800e51SPaolo Abeni do_error: 183169800e51SPaolo Abeni if (copied) 183269800e51SPaolo Abeni goto out; 183369800e51SPaolo Abeni 183469800e51SPaolo Abeni copied = sk_stream_error(sk, msg->msg_flags, ret); 183569800e51SPaolo Abeni goto out; 1836f870fa0bSMat Martineau } 1837f870fa0bSMat Martineau 18386771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18396771bfd9SFlorian Westphal struct msghdr *msg, 1840b7f653b2SFlorian Westphal size_t len, int flags, 1841b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1842b7f653b2SFlorian Westphal int *cmsg_flags) 18436771bfd9SFlorian Westphal { 1844ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18456771bfd9SFlorian Westphal int copied = 0; 18466771bfd9SFlorian Westphal 1847ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18486771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18496771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18506771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18516771bfd9SFlorian Westphal int err; 18526771bfd9SFlorian Westphal 1853d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18546771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18556771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18566771bfd9SFlorian Westphal if (!copied) 18576771bfd9SFlorian Westphal return err; 18586771bfd9SFlorian Westphal break; 18596771bfd9SFlorian Westphal } 1860d976092cSPaolo Abeni } 18616771bfd9SFlorian Westphal 1862b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1863b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1864b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1865b7f653b2SFlorian Westphal } 1866b7f653b2SFlorian Westphal 18676771bfd9SFlorian Westphal copied += count; 18686771bfd9SFlorian Westphal 18696771bfd9SFlorian Westphal if (count < data_len) { 18702c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18716771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18722c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18732c9e7765SFlorian Westphal } 18746771bfd9SFlorian Westphal break; 18756771bfd9SFlorian Westphal } 18766771bfd9SFlorian Westphal 1877ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 187887952603SPaolo Abeni /* we will bulk release the skb memory later */ 187987952603SPaolo Abeni skb->destructor = NULL; 1880ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 188187952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18826771bfd9SFlorian Westphal __kfree_skb(skb); 1883ca4fb892SYonglong Li } 18846771bfd9SFlorian Westphal 18856771bfd9SFlorian Westphal if (copied >= len) 18866771bfd9SFlorian Westphal break; 18876771bfd9SFlorian Westphal } 18886771bfd9SFlorian Westphal 18896771bfd9SFlorian Westphal return copied; 18906771bfd9SFlorian Westphal } 18916771bfd9SFlorian Westphal 1892a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1893a6b118feSFlorian Westphal * 1894a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1895a6b118feSFlorian Westphal */ 1896a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1897a6b118feSFlorian Westphal { 1898a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1899a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1900a6b118feSFlorian Westphal u32 time, advmss = 1; 1901a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1902a6b118feSFlorian Westphal 1903a6b118feSFlorian Westphal sock_owned_by_me(sk); 1904a6b118feSFlorian Westphal 1905a6b118feSFlorian Westphal if (copied <= 0) 1906a6b118feSFlorian Westphal return; 1907a6b118feSFlorian Westphal 1908a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1909a6b118feSFlorian Westphal 1910a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1911a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1912a6b118feSFlorian Westphal 1913a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1914a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1915a6b118feSFlorian Westphal return; 1916a6b118feSFlorian Westphal 1917a6b118feSFlorian Westphal rtt_us = 0; 1918a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1919a6b118feSFlorian Westphal const struct tcp_sock *tp; 1920a6b118feSFlorian Westphal u64 sf_rtt_us; 1921a6b118feSFlorian Westphal u32 sf_advmss; 1922a6b118feSFlorian Westphal 1923a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1924a6b118feSFlorian Westphal 1925a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1926a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1927a6b118feSFlorian Westphal 1928a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1929a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1930a6b118feSFlorian Westphal } 1931a6b118feSFlorian Westphal 1932a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1933a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1934a6b118feSFlorian Westphal return; 1935a6b118feSFlorian Westphal 1936a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1937a6b118feSFlorian Westphal goto new_measure; 1938a6b118feSFlorian Westphal 193978047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1940a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1941a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1942a6b118feSFlorian Westphal u64 rcvwin, grow; 1943a6b118feSFlorian Westphal 1944a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1945a6b118feSFlorian Westphal 1946a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1947a6b118feSFlorian Westphal 1948a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1949a6b118feSFlorian Westphal rcvwin += (grow << 1); 1950a6b118feSFlorian Westphal 1951a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1952a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1953a6b118feSFlorian Westphal rcvmem += 128; 1954a6b118feSFlorian Westphal 1955a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1956a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 195702739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1958a6b118feSFlorian Westphal 1959a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1960a6b118feSFlorian Westphal u32 window_clamp; 1961a6b118feSFlorian Westphal 1962a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1963a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1964a6b118feSFlorian Westphal 1965a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1966a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1967a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1968a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1969a6b118feSFlorian Westphal */ 1970a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1971a6b118feSFlorian Westphal struct sock *ssk; 1972c76c6956SPaolo Abeni bool slow; 1973a6b118feSFlorian Westphal 1974a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1975c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1976a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1977a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1978c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1979c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1980a6b118feSFlorian Westphal } 1981a6b118feSFlorian Westphal } 1982a6b118feSFlorian Westphal } 1983a6b118feSFlorian Westphal 1984a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1985a6b118feSFlorian Westphal new_measure: 1986a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1987a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1988a6b118feSFlorian Westphal } 1989a6b118feSFlorian Westphal 199087952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 199187952603SPaolo Abeni { 199287952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 199387952603SPaolo Abeni 199487952603SPaolo Abeni if (!msk->rmem_released) 199587952603SPaolo Abeni return; 199687952603SPaolo Abeni 199787952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19986511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1999ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 200087952603SPaolo Abeni } 200187952603SPaolo Abeni 200287952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 200387952603SPaolo Abeni { 200487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 200587952603SPaolo Abeni 200687952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 200787952603SPaolo Abeni } 200887952603SPaolo Abeni 2009e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 20106771bfd9SFlorian Westphal { 201187952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 20126771bfd9SFlorian Westphal unsigned int moved = 0; 201387952603SPaolo Abeni bool ret, done; 2014d5f49190SPaolo Abeni 20156771bfd9SFlorian Westphal do { 20166771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 201765f49fe7SFlorian Westphal bool slowpath; 20186771bfd9SFlorian Westphal 201987952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 202087952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 202187952603SPaolo Abeni * that is an unlikely slow path. 202287952603SPaolo Abeni */ 202387952603SPaolo Abeni if (likely(!ssk)) 20246771bfd9SFlorian Westphal break; 20256771bfd9SFlorian Westphal 202665f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 202787952603SPaolo Abeni mptcp_data_lock(sk); 2028e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20296771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 203087952603SPaolo Abeni mptcp_data_unlock(sk); 2031499ada50SPaolo Abeni 2032499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2033499ada50SPaolo Abeni __mptcp_error_report(sk); 203465f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20356771bfd9SFlorian Westphal } while (!done); 20366771bfd9SFlorian Westphal 203787952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 203887952603SPaolo Abeni ret = moved > 0; 203987952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 204087952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 204187952603SPaolo Abeni mptcp_data_lock(sk); 204287952603SPaolo Abeni __mptcp_update_rmem(sk); 204387952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 204487952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 204587952603SPaolo Abeni mptcp_data_unlock(sk); 2046ab174ad8SPaolo Abeni } 204787952603SPaolo Abeni if (ret) 204887952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 204987952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20506771bfd9SFlorian Westphal } 20516771bfd9SFlorian Westphal 20522c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 20532c9e7765SFlorian Westphal { 20542c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 20552c9e7765SFlorian Westphal const struct sk_buff *skb; 20562c9e7765SFlorian Westphal 20572c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20582c9e7765SFlorian Westphal if (skb) { 20592c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20602c9e7765SFlorian Westphal 20612c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20622c9e7765SFlorian Westphal return INT_MAX; 20632c9e7765SFlorian Westphal 20642c9e7765SFlorian Westphal return (unsigned int)hint_val; 20652c9e7765SFlorian Westphal } 20662c9e7765SFlorian Westphal 20672c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20682c9e7765SFlorian Westphal return 1; 20692c9e7765SFlorian Westphal 20702c9e7765SFlorian Westphal return 0; 20712c9e7765SFlorian Westphal } 20722c9e7765SFlorian Westphal 2073f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2074ec095263SOliver Hartkopp int flags, int *addr_len) 2075f870fa0bSMat Martineau { 2076f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2077b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2078b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20797a6a6cbcSPaolo Abeni int target; 20807a6a6cbcSPaolo Abeni long timeo; 2081f870fa0bSMat Martineau 2082cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2083cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2084cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2085cb9d80f4SPaolo Abeni 20866511882cSPaolo Abeni lock_sock(sk); 2087fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2088fd897679SPaolo Abeni copied = -ENOTCONN; 2089fd897679SPaolo Abeni goto out_err; 2090fd897679SPaolo Abeni } 2091fd897679SPaolo Abeni 2092ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20937a6a6cbcSPaolo Abeni 20947a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20957a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20967a6a6cbcSPaolo Abeni 20972c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20982c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20992c9e7765SFlorian Westphal 210005e3eceaSEric Dumazet while (copied < len) { 2101e3859603SPaolo Abeni int bytes_read; 21027a6a6cbcSPaolo Abeni 2103b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 21046771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 21057a6a6cbcSPaolo Abeni if (!copied) 21067a6a6cbcSPaolo Abeni copied = bytes_read; 21076771bfd9SFlorian Westphal goto out_err; 2108cec37a6eSPeter Krystad } 2109cec37a6eSPeter Krystad 21107a6a6cbcSPaolo Abeni copied += bytes_read; 21117a6a6cbcSPaolo Abeni 2112ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2113fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2114ea4ca586SPaolo Abeni 2115e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2116e3859603SPaolo Abeni continue; 2117e3859603SPaolo Abeni 21187a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 21197a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 21207a6a6cbcSPaolo Abeni */ 21217a6a6cbcSPaolo Abeni if (copied >= target) 21227a6a6cbcSPaolo Abeni break; 21237a6a6cbcSPaolo Abeni 21247a6a6cbcSPaolo Abeni if (copied) { 21257a6a6cbcSPaolo Abeni if (sk->sk_err || 21267a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 21277a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 21287a6a6cbcSPaolo Abeni !timeo || 21297a6a6cbcSPaolo Abeni signal_pending(current)) 21307a6a6cbcSPaolo Abeni break; 21317a6a6cbcSPaolo Abeni } else { 21327a6a6cbcSPaolo Abeni if (sk->sk_err) { 21337a6a6cbcSPaolo Abeni copied = sock_error(sk); 21347a6a6cbcSPaolo Abeni break; 21357a6a6cbcSPaolo Abeni } 21367a6a6cbcSPaolo Abeni 21375969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 21385969856aSPaolo Abeni mptcp_check_for_eof(msk); 21395969856aSPaolo Abeni 214087952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 214187952603SPaolo Abeni /* race breaker: the shutdown could be after the 214287952603SPaolo Abeni * previous receive queue check 214387952603SPaolo Abeni */ 2144e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 214587952603SPaolo Abeni continue; 21467a6a6cbcSPaolo Abeni break; 214787952603SPaolo Abeni } 21487a6a6cbcSPaolo Abeni 21497a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21507a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21517a6a6cbcSPaolo Abeni break; 21527a6a6cbcSPaolo Abeni } 21537a6a6cbcSPaolo Abeni 21547a6a6cbcSPaolo Abeni if (!timeo) { 21557a6a6cbcSPaolo Abeni copied = -EAGAIN; 21567a6a6cbcSPaolo Abeni break; 21577a6a6cbcSPaolo Abeni } 21587a6a6cbcSPaolo Abeni 21597a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21607a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21617a6a6cbcSPaolo Abeni break; 21627a6a6cbcSPaolo Abeni } 21637a6a6cbcSPaolo Abeni } 21647a6a6cbcSPaolo Abeni 21657a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2166612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21677a6a6cbcSPaolo Abeni } 21683c90e377SPaolo Abeni 21696771bfd9SFlorian Westphal out_err: 2170b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2171b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2172b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21732c9e7765SFlorian Westphal 21742c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21752c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21762c9e7765SFlorian Westphal 21772c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21782c9e7765SFlorian Westphal } 2179b7f653b2SFlorian Westphal } 2180b7f653b2SFlorian Westphal 2181612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2182612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2183612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2184ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2185a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2186a6b118feSFlorian Westphal 2187cec37a6eSPeter Krystad release_sock(sk); 2188cec37a6eSPeter Krystad return copied; 2189cec37a6eSPeter Krystad } 2190cec37a6eSPeter Krystad 2191b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2192b51f9b80SPaolo Abeni { 2193b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2194b51f9b80SPaolo Abeni icsk_retransmit_timer); 2195b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21962d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2197b51f9b80SPaolo Abeni 2198b51f9b80SPaolo Abeni bh_lock_sock(sk); 2199b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 22002d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 22012d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 22022d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2203b51f9b80SPaolo Abeni } else { 2204b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2205e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2206b51f9b80SPaolo Abeni } 2207b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2208b51f9b80SPaolo Abeni sock_put(sk); 2209b51f9b80SPaolo Abeni } 2210b51f9b80SPaolo Abeni 2211e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2212e16163b6SPaolo Abeni { 2213e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2214e16163b6SPaolo Abeni 2215e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2216b6d69fc8SFlorian Westphal sock_put(sk); 2217e16163b6SPaolo Abeni } 2218e16163b6SPaolo Abeni 22193b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 22203b1d6210SPaolo Abeni * level. 22213b1d6210SPaolo Abeni * 22223b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 22233b1d6210SPaolo Abeni */ 2224ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 22253b1d6210SPaolo Abeni { 222671b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 22273b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 222871b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 22293b1d6210SPaolo Abeni 22303b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 22313b1d6210SPaolo Abeni 2232d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2233d9ca1de8SPaolo Abeni return NULL; 2234d5f49190SPaolo Abeni 22353b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 22363b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 22373b1d6210SPaolo Abeni 2238ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2239d5f49190SPaolo Abeni continue; 2240d5f49190SPaolo Abeni 224171b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 224271b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 224371b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 224471b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2245860975c6SFlorian Westphal continue; 2246860975c6SFlorian Westphal } 22473b1d6210SPaolo Abeni 22483b1d6210SPaolo Abeni if (subflow->backup) { 22493b1d6210SPaolo Abeni if (!backup) 22503b1d6210SPaolo Abeni backup = ssk; 22513b1d6210SPaolo Abeni continue; 22523b1d6210SPaolo Abeni } 22533b1d6210SPaolo Abeni 225471b7dec2SPaolo Abeni if (!pick) 225571b7dec2SPaolo Abeni pick = ssk; 22563b1d6210SPaolo Abeni } 22573b1d6210SPaolo Abeni 225871b7dec2SPaolo Abeni if (pick) 225971b7dec2SPaolo Abeni return pick; 226071b7dec2SPaolo Abeni 226171b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 226271b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22633b1d6210SPaolo Abeni } 22643b1d6210SPaolo Abeni 226517aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 226617aee05dSFlorian Westphal { 226717aee05dSFlorian Westphal if (msk->subflow) { 226817aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 226917aee05dSFlorian Westphal msk->subflow = NULL; 227017aee05dSFlorian Westphal } 227117aee05dSFlorian Westphal } 227217aee05dSFlorian Westphal 22731e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22741e1d9d6fSPaolo Abeni { 22751e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22761e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22771e1d9d6fSPaolo Abeni 22781e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22791e1d9d6fSPaolo Abeni return false; 22801e1d9d6fSPaolo Abeni 22811e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22821e1d9d6fSPaolo Abeni return false; 22831e1d9d6fSPaolo Abeni 22841e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22851e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22861e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22871e1d9d6fSPaolo Abeni */ 22881e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22891e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22901e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22911e1d9d6fSPaolo Abeni if (!rtx_head) { 22921e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22931e1d9d6fSPaolo Abeni return false; 22941e1d9d6fSPaolo Abeni } 22951e1d9d6fSPaolo Abeni 22961e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22971e1d9d6fSPaolo Abeni msk->recovery = true; 22981e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22991e1d9d6fSPaolo Abeni 23001e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 23011e1d9d6fSPaolo Abeni msk->snd_burst = 0; 23021e1d9d6fSPaolo Abeni 23031e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 23041e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 23051e1d9d6fSPaolo Abeni if (!cur->already_sent) 23061e1d9d6fSPaolo Abeni break; 23071e1d9d6fSPaolo Abeni cur->already_sent = 0; 23081e1d9d6fSPaolo Abeni } 23091e1d9d6fSPaolo Abeni 23101e1d9d6fSPaolo Abeni return true; 23111e1d9d6fSPaolo Abeni } 23121e1d9d6fSPaolo Abeni 2313b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2314b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2315b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2316b29fcfb5SPaolo Abeni 2317cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2318cec37a6eSPeter Krystad * (accept). 2319cec37a6eSPeter Krystad * 2320cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2321cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2322cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2323cec37a6eSPeter Krystad * parent socket. 2324cec37a6eSPeter Krystad */ 2325a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2326b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2327b29fcfb5SPaolo Abeni unsigned int flags) 2328cec37a6eSPeter Krystad { 2329e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2330b29fcfb5SPaolo Abeni bool need_push, dispose_it; 2331e0be4931SFlorian Westphal 2332b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2333b29fcfb5SPaolo Abeni if (dispose_it) 2334cec37a6eSPeter Krystad list_del(&subflow->node); 2335cec37a6eSPeter Krystad 23363f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2337e16163b6SPaolo Abeni 2338d21f8348SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) { 2339d21f8348SPaolo Abeni /* be sure to force the tcp_disconnect() path, 2340d21f8348SPaolo Abeni * to generate the egress reset 2341d21f8348SPaolo Abeni */ 2342d21f8348SPaolo Abeni ssk->sk_lingertime = 0; 2343d21f8348SPaolo Abeni sock_set_flag(ssk, SOCK_LINGER); 2344b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2345d21f8348SPaolo Abeni } 2346b29fcfb5SPaolo Abeni 2347b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2348b29fcfb5SPaolo Abeni if (!dispose_it) { 2349b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2350b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2351b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2352b29fcfb5SPaolo Abeni release_sock(ssk); 2353b29fcfb5SPaolo Abeni 2354b29fcfb5SPaolo Abeni goto out; 2355b29fcfb5SPaolo Abeni } 2356b29fcfb5SPaolo Abeni 2357e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2358e16163b6SPaolo Abeni * already orphaned 2359e16163b6SPaolo Abeni */ 2360866f26f2SPaolo Abeni if (ssk->sk_socket) 2361e16163b6SPaolo Abeni sock_orphan(ssk); 2362e16163b6SPaolo Abeni 2363d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2364d7b1bfd0SPaolo Abeni 2365e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2366e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2367e16163b6SPaolo Abeni * reference owned by msk; 2368e16163b6SPaolo Abeni */ 2369e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2370e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2371e16163b6SPaolo Abeni } else { 2372d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 23736aeed904SPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 23746aeed904SPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 23756aeed904SPaolo Abeni mptcp_subflow_queue_clean(ssk); 23766aeed904SPaolo Abeni inet_csk_listen_stop(ssk); 23776aeed904SPaolo Abeni } 2378e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2379e16163b6SPaolo Abeni 2380e16163b6SPaolo Abeni /* close acquired an extra ref */ 2381e16163b6SPaolo Abeni __sock_put(ssk); 2382e16163b6SPaolo Abeni } 2383e16163b6SPaolo Abeni release_sock(ssk); 2384e16163b6SPaolo Abeni 2385e16163b6SPaolo Abeni sock_put(ssk); 2386e0be4931SFlorian Westphal 2387c8fe62f0SFlorian Westphal if (ssk == msk->first) 2388c8fe62f0SFlorian Westphal msk->first = NULL; 2389c8fe62f0SFlorian Westphal 2390b29fcfb5SPaolo Abeni out: 2391b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2392b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23931e1d9d6fSPaolo Abeni 23941e1d9d6fSPaolo Abeni if (need_push) 23951e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2396f870fa0bSMat Martineau } 2397f870fa0bSMat Martineau 2398a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2399a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2400a141e02eSFlorian Westphal { 2401b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2402b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2403a88c9e49SPaolo Abeni 2404a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2405a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2406a88c9e49SPaolo Abeni */ 2407a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2408a88c9e49SPaolo Abeni 2409b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2410a141e02eSFlorian Westphal } 2411a141e02eSFlorian Westphal 2412dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2413dc24f8b4SPaolo Abeni { 2414dc24f8b4SPaolo Abeni return 0; 2415dc24f8b4SPaolo Abeni } 2416dc24f8b4SPaolo Abeni 24170e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 24180e4f35d7SPaolo Abeni { 24190e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 24200e4f35d7SPaolo Abeni 24213abc05d9SFlorian Westphal might_sleep(); 24223abc05d9SFlorian Westphal 24235efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 24240e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 24250e4f35d7SPaolo Abeni 24260e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 24270e4f35d7SPaolo Abeni continue; 24280e4f35d7SPaolo Abeni 242940947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 243040947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 243140947e13SFlorian Westphal continue; 243240947e13SFlorian Westphal 2433a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 24340e4f35d7SPaolo Abeni } 24350e4f35d7SPaolo Abeni } 24360e4f35d7SPaolo Abeni 2437e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2438e16163b6SPaolo Abeni { 2439e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2440e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2441e16163b6SPaolo Abeni 2442e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2443e16163b6SPaolo Abeni return true; 2444e16163b6SPaolo Abeni 2445e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2446e16163b6SPaolo Abeni * timeout 2447e16163b6SPaolo Abeni */ 2448e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2449e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2450e16163b6SPaolo Abeni TCP_CLOSE) 2451e16163b6SPaolo Abeni return false; 2452e16163b6SPaolo Abeni } 2453e16163b6SPaolo Abeni return true; 2454e16163b6SPaolo Abeni } 2455e16163b6SPaolo Abeni 245650c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 245750c504a2SFlorian Westphal { 245850c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 245950c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 246050c504a2SFlorian Westphal 246150c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 246250c504a2SFlorian Westphal return; 246350c504a2SFlorian Westphal 246450c504a2SFlorian Westphal mptcp_token_destroy(msk); 246550c504a2SFlorian Westphal 24665efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 246750c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 246875e908c3SPaolo Abeni bool slow; 246950c504a2SFlorian Westphal 247075e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 247150c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 247250c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 247350c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 247450c504a2SFlorian Westphal } 247575e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 247650c504a2SFlorian Westphal } 247750c504a2SFlorian Westphal 247869800e51SPaolo Abeni /* Mirror the tcp_reset() error propagation */ 247969800e51SPaolo Abeni switch (sk->sk_state) { 248069800e51SPaolo Abeni case TCP_SYN_SENT: 248169800e51SPaolo Abeni sk->sk_err = ECONNREFUSED; 248269800e51SPaolo Abeni break; 248369800e51SPaolo Abeni case TCP_CLOSE_WAIT: 248469800e51SPaolo Abeni sk->sk_err = EPIPE; 248569800e51SPaolo Abeni break; 248669800e51SPaolo Abeni case TCP_CLOSE: 248769800e51SPaolo Abeni return; 248869800e51SPaolo Abeni default: 248969800e51SPaolo Abeni sk->sk_err = ECONNRESET; 249069800e51SPaolo Abeni } 249169800e51SPaolo Abeni 249250c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 249350c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 249450c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 249550c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 249650c504a2SFlorian Westphal 249769800e51SPaolo Abeni /* the calling mptcp_worker will properly destroy the socket */ 249869800e51SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 249969800e51SPaolo Abeni return; 250069800e51SPaolo Abeni 250169800e51SPaolo Abeni sk->sk_state_change(sk); 250269800e51SPaolo Abeni sk_error_report(sk); 250350c504a2SFlorian Westphal } 250450c504a2SFlorian Westphal 25052948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 250680992017SPaolo Abeni { 25072948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2508caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 25093b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 25103b1d6210SPaolo Abeni size_t copied = 0; 25112948d0a1SPaolo Abeni struct sock *ssk; 25122948d0a1SPaolo Abeni int ret; 25132948d0a1SPaolo Abeni 2514b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 25153241a9c0SFlorian Westphal 25163241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 25173241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 25182948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 25196477dd39SMat Martineau if (!dfrag) { 25206477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 25216477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 25226477dd39SMat Martineau 25236477dd39SMat Martineau icsk->icsk_retransmits++; 25246477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 25256477dd39SMat Martineau mptcp_send_ack(msk); 25266477dd39SMat Martineau 25276477dd39SMat Martineau goto reset_timer; 25286477dd39SMat Martineau } 25296477dd39SMat Martineau 25303241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 25312948d0a1SPaolo Abeni return; 25323241a9c0SFlorian Westphal 25333241a9c0SFlorian Westphal goto reset_timer; 25346477dd39SMat Martineau } 25352948d0a1SPaolo Abeni 25362948d0a1SPaolo Abeni if (!ssk) 25372948d0a1SPaolo Abeni goto reset_timer; 25382948d0a1SPaolo Abeni 25392948d0a1SPaolo Abeni lock_sock(ssk); 25402948d0a1SPaolo Abeni 25412948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 25422948d0a1SPaolo Abeni info.sent = 0; 25434e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 25444e14867dSPaolo Abeni while (info.sent < info.limit) { 25452948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 25462948d0a1SPaolo Abeni if (ret <= 0) 25472948d0a1SPaolo Abeni break; 25482948d0a1SPaolo Abeni 25492948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 25502948d0a1SPaolo Abeni copied += ret; 25512948d0a1SPaolo Abeni info.sent += ret; 25522948d0a1SPaolo Abeni } 25534e14867dSPaolo Abeni if (copied) { 25544e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 25552948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 25562948d0a1SPaolo Abeni info.size_goal); 25570530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 25584e14867dSPaolo Abeni } 25592948d0a1SPaolo Abeni 25602948d0a1SPaolo Abeni release_sock(ssk); 25612948d0a1SPaolo Abeni 25622948d0a1SPaolo Abeni reset_timer: 25633241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 25643241a9c0SFlorian Westphal 25652948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 25662948d0a1SPaolo Abeni mptcp_reset_timer(sk); 25672948d0a1SPaolo Abeni } 25682948d0a1SPaolo Abeni 256976a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 257076a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 257176a13b31SGeliang Tang */ 257276a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 257376a13b31SGeliang Tang { 257476a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 257576a13b31SGeliang Tang unsigned long timeout, close_timeout; 257676a13b31SGeliang Tang 257776a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 257876a13b31SGeliang Tang return; 257976a13b31SGeliang Tang 258076a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 258176a13b31SGeliang Tang 258276a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 258376a13b31SGeliang Tang * them is active 258476a13b31SGeliang Tang */ 258576a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 258676a13b31SGeliang Tang 258776a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 258876a13b31SGeliang Tang } 258976a13b31SGeliang Tang 259049fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 259149fa1919SGeliang Tang { 259276a13b31SGeliang Tang struct sock *ssk = msk->first; 259349fa1919SGeliang Tang bool slow; 259449fa1919SGeliang Tang 259576a13b31SGeliang Tang if (!ssk) 259676a13b31SGeliang Tang return; 259776a13b31SGeliang Tang 259849fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 259949fa1919SGeliang Tang 260049fa1919SGeliang Tang slow = lock_sock_fast(ssk); 260149fa1919SGeliang Tang mptcp_subflow_reset(ssk); 260276a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 260349fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 260476a13b31SGeliang Tang 260576a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 260649fa1919SGeliang Tang } 260749fa1919SGeliang Tang 2608d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk) 2609d21f8348SPaolo Abeni { 2610d21f8348SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 2611d21f8348SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2612d21f8348SPaolo Abeni 2613d21f8348SPaolo Abeni mptcp_for_each_subflow_safe(msk, subflow, tmp) 2614d21f8348SPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2615d21f8348SPaolo Abeni subflow, MPTCP_CF_FASTCLOSE); 2616d21f8348SPaolo Abeni } 2617d21f8348SPaolo Abeni 26182948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 26192948d0a1SPaolo Abeni { 26202948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 26212948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 262276a13b31SGeliang Tang unsigned long fail_tout; 26232948d0a1SPaolo Abeni int state; 262480992017SPaolo Abeni 262580992017SPaolo Abeni lock_sock(sk); 2626e16163b6SPaolo Abeni state = sk->sk_state; 2627e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2628e16163b6SPaolo Abeni goto unlock; 2629e16163b6SPaolo Abeni 263043b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 263150c504a2SFlorian Westphal 263250c504a2SFlorian Westphal mptcp_check_fastclose(msk); 263350c504a2SFlorian Westphal 2634e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2635b416268bSFlorian Westphal 263659832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 263759832e24SFlorian Westphal mptcp_check_for_eof(msk); 263859832e24SFlorian Westphal 26396e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 264043b54c6eSMat Martineau mptcp_check_data_fin(sk); 264143b54c6eSMat Martineau 2642341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2643341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2644341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2645e16163b6SPaolo Abeni */ 2646d21f8348SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) { 2647d21f8348SPaolo Abeni if (mptcp_check_close_timeout(sk)) { 2648e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2649d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2650d21f8348SPaolo Abeni } 2651d21f8348SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2652e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2653e16163b6SPaolo Abeni goto unlock; 2654e16163b6SPaolo Abeni } 2655d21f8348SPaolo Abeni } 2656e16163b6SPaolo Abeni 2657b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2658b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2659b263b0d7SFlorian Westphal 26602948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 26612948d0a1SPaolo Abeni __mptcp_retrans(sk); 26623b1d6210SPaolo Abeni 266376a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 266476a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 266549fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 266649fa1919SGeliang Tang 26673b1d6210SPaolo Abeni unlock: 266880992017SPaolo Abeni release_sock(sk); 266980992017SPaolo Abeni sock_put(sk); 267080992017SPaolo Abeni } 267180992017SPaolo Abeni 2672784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2673f870fa0bSMat Martineau { 2674cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2675cec37a6eSPeter Krystad 2676cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2677ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 267818b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 267980992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 268087952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2681ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2682f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 26836511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2684ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 268533d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2686cec37a6eSPeter Krystad 26878ab183deSPaolo Abeni msk->first = NULL; 2688dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2689752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 26900530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 26911e1d9d6fSPaolo Abeni msk->recovery = false; 26928ab183deSPaolo Abeni 26931b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 26941b1c7a0eSPeter Krystad 2695b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2696b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2697e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2698aa1fbd94SFlorian Westphal 2699f870fa0bSMat Martineau return 0; 2700f870fa0bSMat Martineau } 2701f870fa0bSMat Martineau 2702b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2703784325e9SMatthieu Baerts { 270420b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2705b29fcfb5SPaolo Abeni 2706b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2707b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2708b29fcfb5SPaolo Abeni 2709b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2710b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2711b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2712b29fcfb5SPaolo Abeni } 2713b29fcfb5SPaolo Abeni 2714b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2715b29fcfb5SPaolo Abeni { 2716fc518953SFlorian Westphal struct net *net = sock_net(sk); 2717fc518953SFlorian Westphal int ret; 271818b683bfSPaolo Abeni 2719b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2720b6c08380SGeliang Tang if (ret) 2721b6c08380SGeliang Tang return ret; 2722b6c08380SGeliang Tang 2723fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2724fc518953SFlorian Westphal return -ENOPROTOOPT; 2725fc518953SFlorian Westphal 2726fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2727fc518953SFlorian Westphal return -ENOMEM; 2728fc518953SFlorian Westphal 2729fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2730fa68018dSPaolo Abeni if (ret) 2731fa68018dSPaolo Abeni return ret; 2732fa68018dSPaolo Abeni 273320b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 273420b5759fSPaolo Abeni * propagate the correct value 273520b5759fSPaolo Abeni */ 2736b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 273720b5759fSPaolo Abeni 2738d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 273902739545SKuniyuki Iwashima sk->sk_rcvbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[1]); 274002739545SKuniyuki Iwashima sk->sk_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[1]); 2741d027236cSPaolo Abeni 274218b683bfSPaolo Abeni return 0; 274318b683bfSPaolo Abeni } 274418b683bfSPaolo Abeni 274518b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 274618b683bfSPaolo Abeni { 274718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 274818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 274918b683bfSPaolo Abeni 2750d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 275118b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2752d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2753784325e9SMatthieu Baerts } 2754784325e9SMatthieu Baerts 275530e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk) 275680992017SPaolo Abeni { 275780992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 275880992017SPaolo Abeni 2759b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2760e16163b6SPaolo Abeni __sock_put(sk); 276180992017SPaolo Abeni } 276280992017SPaolo Abeni 2763d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 276421498490SPeter Krystad { 276521498490SPeter Krystad lock_sock(ssk); 276621498490SPeter Krystad 276721498490SPeter Krystad switch (ssk->sk_state) { 276821498490SPeter Krystad case TCP_LISTEN: 276921498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 277021498490SPeter Krystad break; 2771df561f66SGustavo A. R. Silva fallthrough; 277221498490SPeter Krystad case TCP_SYN_SENT: 277321498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 277421498490SPeter Krystad break; 277521498490SPeter Krystad default: 277643b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 277743b54c6eSMat Martineau pr_debug("Fallback"); 277821498490SPeter Krystad ssk->sk_shutdown |= how; 277921498490SPeter Krystad tcp_shutdown(ssk, how); 278043b54c6eSMat Martineau } else { 278143b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 278243b54c6eSMat Martineau tcp_send_ack(ssk); 27836477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 27846477dd39SMat Martineau mptcp_reset_timer(sk); 278543b54c6eSMat Martineau } 278621498490SPeter Krystad break; 278721498490SPeter Krystad } 278821498490SPeter Krystad 278921498490SPeter Krystad release_sock(ssk); 279021498490SPeter Krystad } 279121498490SPeter Krystad 27926920b851SMat Martineau static const unsigned char new_state[16] = { 27936920b851SMat Martineau /* current state: new state: action: */ 27946920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 27956920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27966920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 27976920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27986920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 27996920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 28006920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 28016920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 28026920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 28036920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 28046920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 28056920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 28066920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 28076920b851SMat Martineau }; 28086920b851SMat Martineau 28096920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 28106920b851SMat Martineau { 28116920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 28126920b851SMat Martineau int ns = next & TCP_STATE_MASK; 28136920b851SMat Martineau 28146920b851SMat Martineau inet_sk_state_store(sk, ns); 28156920b851SMat Martineau 28166920b851SMat Martineau return next & TCP_ACTION_FIN; 28176920b851SMat Martineau } 28186920b851SMat Martineau 2819e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2820e16163b6SPaolo Abeni { 2821e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2822e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2823e16163b6SPaolo Abeni 2824e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2825e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2826e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2827e16163b6SPaolo Abeni 2828e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2829e16163b6SPaolo Abeni * skip this 2830e16163b6SPaolo Abeni */ 2831e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2832e16163b6SPaolo Abeni mptcp_send_head(sk)) 2833e16163b6SPaolo Abeni return; 2834e16163b6SPaolo Abeni 2835e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2836e16163b6SPaolo Abeni 283726aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 283826aa2314SPaolo Abeni * state now 283926aa2314SPaolo Abeni */ 284026aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 284158cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 284226aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2843e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2844e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 284526aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 284626aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 284726aa2314SPaolo Abeni } 2848e16163b6SPaolo Abeni } 2849e16163b6SPaolo Abeni 2850e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2851e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2852e16163b6SPaolo Abeni 2853e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2854e16163b6SPaolo Abeni } 2855e16163b6SPaolo Abeni } 2856e16163b6SPaolo Abeni 2857e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2858e16163b6SPaolo Abeni { 2859e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2860e16163b6SPaolo Abeni 2861e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2862e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2863e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2864e16163b6SPaolo Abeni 2865e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2866e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2867e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2868e16163b6SPaolo Abeni 2869e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2870e16163b6SPaolo Abeni } 2871e16163b6SPaolo Abeni 2872e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2873f870fa0bSMat Martineau { 2874f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2875f870fa0bSMat Martineau 2876e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 28772c22c06cSFlorian Westphal 28783abc05d9SFlorian Westphal might_sleep(); 28793abc05d9SFlorian Westphal 2880bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2881e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2882e16163b6SPaolo Abeni msk->pm.status = 0; 2883b2c5b614SFlorian Westphal 2884e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2885e16163b6SPaolo Abeni 28866511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 288787952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2888e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2889e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2890aa1fbd94SFlorian Westphal 2891e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2892e16163b6SPaolo Abeni sock_put(sk); 2893e16163b6SPaolo Abeni } 2894e16163b6SPaolo Abeni 2895d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2896d21f8348SPaolo Abeni { 2897d21f8348SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 2898d21f8348SPaolo Abeni * always show at least one non-empty queue when checked in this order. 2899d21f8348SPaolo Abeni */ 2900d21f8348SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2901d21f8348SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 2902d21f8348SPaolo Abeni return 0; 2903d21f8348SPaolo Abeni 2904d21f8348SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 2905d21f8348SPaolo Abeni } 2906d21f8348SPaolo Abeni 290726d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout) 2908e16163b6SPaolo Abeni { 2909e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 291076a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2911e16163b6SPaolo Abeni bool do_cancel_work = false; 2912e16163b6SPaolo Abeni 2913e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2914e16163b6SPaolo Abeni 2915e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2916e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2917e16163b6SPaolo Abeni goto cleanup; 2918e16163b6SPaolo Abeni } 2919e16163b6SPaolo Abeni 2920d21f8348SPaolo Abeni if (mptcp_check_readable(msk)) { 2921d21f8348SPaolo Abeni /* the msk has read data, do the MPTCP equivalent of TCP reset */ 2922d21f8348SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2923d21f8348SPaolo Abeni mptcp_do_fastclose(sk); 2924d21f8348SPaolo Abeni } else if (mptcp_close_state(sk)) { 2925e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2926d21f8348SPaolo Abeni } 2927e16163b6SPaolo Abeni 2928e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2929e16163b6SPaolo Abeni 2930e16163b6SPaolo Abeni cleanup: 2931e16163b6SPaolo Abeni /* orphan all the subflows */ 2932e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 293376a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2934e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 293549054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2936e16163b6SPaolo Abeni 293776a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 293876a13b31SGeliang Tang * cancel the latter 293976a13b31SGeliang Tang */ 294076a13b31SGeliang Tang if (ssk == msk->first) 294176a13b31SGeliang Tang subflow->fail_tout = 0; 294276a13b31SGeliang Tang 2943e16163b6SPaolo Abeni sock_orphan(ssk); 2944e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2945e16163b6SPaolo Abeni } 2946e16163b6SPaolo Abeni sock_orphan(sk); 2947e16163b6SPaolo Abeni 2948e16163b6SPaolo Abeni sock_hold(sk); 2949e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2950b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 295176a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2952b29fcfb5SPaolo Abeni 2953e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2954e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2955e16163b6SPaolo Abeni do_cancel_work = true; 2956e16163b6SPaolo Abeni } else { 295776a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2958e16163b6SPaolo Abeni } 295926d3e21cSMenglong Dong 296026d3e21cSMenglong Dong return do_cancel_work; 296126d3e21cSMenglong Dong } 296226d3e21cSMenglong Dong 296326d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout) 296426d3e21cSMenglong Dong { 296526d3e21cSMenglong Dong bool do_cancel_work; 296626d3e21cSMenglong Dong 296726d3e21cSMenglong Dong lock_sock(sk); 296826d3e21cSMenglong Dong 296926d3e21cSMenglong Dong do_cancel_work = __mptcp_close(sk, timeout); 2970e16163b6SPaolo Abeni release_sock(sk); 2971e16163b6SPaolo Abeni if (do_cancel_work) 297280992017SPaolo Abeni mptcp_cancel_work(sk); 2973b911c97cSFlorian Westphal 2974e16163b6SPaolo Abeni sock_put(sk); 2975f870fa0bSMat Martineau } 2976f870fa0bSMat Martineau 2977e72e4032SPaolo Abeni void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2978cf7da0d6SPeter Krystad { 2979cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2980cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2981cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2982cf7da0d6SPeter Krystad 2983cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2984cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2985cf7da0d6SPeter Krystad 2986cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2987cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2988cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2989cf7da0d6SPeter Krystad } 2990cf7da0d6SPeter Krystad #endif 2991cf7da0d6SPeter Krystad 2992cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2993cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2994cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2995cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2996cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2997cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2998cf7da0d6SPeter Krystad } 2999cf7da0d6SPeter Krystad 300018b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 300118b683bfSPaolo Abeni { 300276e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 300376e2a55dSPaolo Abeni 3004b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 3005b29fcfb5SPaolo Abeni 3006bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 3007b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 3008b29fcfb5SPaolo Abeni 3009b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 3010b29fcfb5SPaolo Abeni mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 3011b29fcfb5SPaolo Abeni 3012c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 3013c0bf3c6aSPaolo Abeni * subflow 3014c0bf3c6aSPaolo Abeni */ 3015c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3016b29fcfb5SPaolo Abeni msk->last_snd = NULL; 3017e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 3018e9d09bacSPaolo Abeni msk->cb_flags = 0; 3019e9d09bacSPaolo Abeni msk->push_pending = 0; 3020b29fcfb5SPaolo Abeni msk->recovery = false; 3021b29fcfb5SPaolo Abeni msk->can_ack = false; 3022b29fcfb5SPaolo Abeni msk->fully_established = false; 3023b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 3024b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 3025b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 3026b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 3027b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3028b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 3029b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 3030b29fcfb5SPaolo Abeni 3031b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 3032b29fcfb5SPaolo Abeni sk_error_report(sk); 303342c556feSFlorian Westphal return 0; 303418b683bfSPaolo Abeni } 303518b683bfSPaolo Abeni 3036b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3037b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3038b0519de8SFlorian Westphal { 3039b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3040b0519de8SFlorian Westphal 3041b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3042b0519de8SFlorian Westphal } 3043b0519de8SFlorian Westphal #endif 3044b0519de8SFlorian Westphal 3045fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 3046cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 3047fca5c82cSPaolo Abeni struct request_sock *req) 3048b0519de8SFlorian Westphal { 304958b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3050b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 305158b09919SPaolo Abeni struct mptcp_sock *msk; 305258b09919SPaolo Abeni u64 ack_seq; 3053b0519de8SFlorian Westphal 3054b0519de8SFlorian Westphal if (!nsk) 3055b0519de8SFlorian Westphal return NULL; 3056b0519de8SFlorian Westphal 3057b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3058b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 3059b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3060b0519de8SFlorian Westphal #endif 3061b0519de8SFlorian Westphal 306258b09919SPaolo Abeni __mptcp_init_sock(nsk); 306358b09919SPaolo Abeni 306458b09919SPaolo Abeni msk = mptcp_sk(nsk); 306558b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 306658b09919SPaolo Abeni msk->token = subflow_req->token; 306758b09919SPaolo Abeni msk->subflow = NULL; 3068b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 306974c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 307006251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 307158b09919SPaolo Abeni 307258b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 3073eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 30747439d687SPaolo Abeni msk->snd_una = msk->write_seq; 30757439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3076df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 30776f8a612aSFlorian Westphal 307874c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 307958b09919SPaolo Abeni msk->can_ack = true; 3080cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 308158b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 308258b09919SPaolo Abeni ack_seq++; 3083917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3084f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 308558b09919SPaolo Abeni } 30867f20d5fcSPaolo Abeni 30875e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 30887f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 30897f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 30900c148460SPaolo Abeni 30910c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 309258b09919SPaolo Abeni bh_unlock_sock(nsk); 309358b09919SPaolo Abeni 309458b09919SPaolo Abeni /* keep a single reference */ 309558b09919SPaolo Abeni __sock_put(nsk); 3096b0519de8SFlorian Westphal return nsk; 3097b0519de8SFlorian Westphal } 3098b0519de8SFlorian Westphal 3099a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3100a6b118feSFlorian Westphal { 3101a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 3102a6b118feSFlorian Westphal 3103a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 3104a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 3105a6b118feSFlorian Westphal 3106a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 3107a6b118feSFlorian Westphal 3108a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 3109a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3110a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3111a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3112a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 31136f8a612aSFlorian Westphal 31147439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3115a6b118feSFlorian Westphal } 3116a6b118feSFlorian Westphal 3117cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3118cf7da0d6SPeter Krystad bool kern) 3119cf7da0d6SPeter Krystad { 3120cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3121cf7da0d6SPeter Krystad struct socket *listener; 3122cf7da0d6SPeter Krystad struct sock *newsk; 3123cf7da0d6SPeter Krystad 3124cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 3125cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3126cf7da0d6SPeter Krystad *err = -EINVAL; 3127cf7da0d6SPeter Krystad return NULL; 3128cf7da0d6SPeter Krystad } 3129cf7da0d6SPeter Krystad 3130cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3131cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3132cf7da0d6SPeter Krystad if (!newsk) 3133cf7da0d6SPeter Krystad return NULL; 3134cf7da0d6SPeter Krystad 3135cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3136cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3137cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3138cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3139cf7da0d6SPeter Krystad 3140cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 314158b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 314258b09919SPaolo Abeni 314358b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 314458b09919SPaolo Abeni * subflow_syn_recv_sock() 314558b09919SPaolo Abeni */ 314658b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 314758b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3148d6692b3bSFlorian Westphal goto out; 314958b09919SPaolo Abeni } 315058b09919SPaolo Abeni 315158b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 315258b09919SPaolo Abeni sock_hold(new_mptcp_sock); 3153cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 31540397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3155fc518953SFlorian Westphal } else { 3156fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3157fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3158cf7da0d6SPeter Krystad } 3159cf7da0d6SPeter Krystad 3160d6692b3bSFlorian Westphal out: 3161d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3162cf7da0d6SPeter Krystad return newsk; 3163cf7da0d6SPeter Krystad } 3164cf7da0d6SPeter Krystad 3165c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 31665c8c1640SGeliang Tang { 3167c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 316887952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 316987952603SPaolo Abeni 31706e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 31716e628cd3SPaolo Abeni 3172c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 31735efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3174c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3175c0bf3c6aSPaolo Abeni 317687952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3177b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 317887952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 31796511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 31805c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3181b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 31826511882cSPaolo Abeni 31836511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 31846511882cSPaolo Abeni * inet_sock_destruct() will dispose it 31856511882cSPaolo Abeni */ 31866511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 31876511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 31885c8c1640SGeliang Tang mptcp_token_destroy(msk); 31895c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 31904638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 31915c8c1640SGeliang Tang } 31925c8c1640SGeliang Tang 319379c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 319479c0949eSPeter Krystad { 3195c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3196c9fd9c5fSFlorian Westphal 3197c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3198c0bf3c6aSPaolo Abeni * even the initial subflow 3199c0bf3c6aSPaolo Abeni */ 3200c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3201c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3202d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 320379c0949eSPeter Krystad } 320479c0949eSPeter Krystad 32056e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 32066e628cd3SPaolo Abeni { 32076e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 32086e628cd3SPaolo Abeni __mptcp_clean_una(sk); 32096e628cd3SPaolo Abeni else 3210e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 32116e628cd3SPaolo Abeni 32126e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 32136e628cd3SPaolo Abeni mptcp_schedule_work(sk); 32146e628cd3SPaolo Abeni } 32156e628cd3SPaolo Abeni 3216219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 32176e628cd3SPaolo Abeni { 32186e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 32196e628cd3SPaolo Abeni return; 32206e628cd3SPaolo Abeni 322140dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 3222b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 3223b19bc294SPaolo Abeni 3224b19bc294SPaolo Abeni if (xmit_ssk == ssk) 32256e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3226b19bc294SPaolo Abeni else if (xmit_ssk) 3227bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND); 322840dc9416SPaolo Abeni } else { 3229e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 32306e628cd3SPaolo Abeni } 323140dc9416SPaolo Abeni } 32326e628cd3SPaolo Abeni 3233e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3234e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3235e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3236e9d09bacSPaolo Abeni 3237e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 323814c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3239e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 324014c441b5SPaolo Abeni { 3241e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 32422d6f5a2bSPaolo Abeni 3243e9d09bacSPaolo Abeni for (;;) { 3244e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3245e9d09bacSPaolo Abeni msk->push_pending; 3246c2e6048fSPaolo Abeni if (!flags) 3247c2e6048fSPaolo Abeni break; 3248c2e6048fSPaolo Abeni 3249c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 32506e628cd3SPaolo Abeni * 32516e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 32526e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 32536e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 32546e628cd3SPaolo Abeni * the subflow socket lock 32556e628cd3SPaolo Abeni */ 3256e9d09bacSPaolo Abeni msk->push_pending = 0; 3257e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 32586e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 32593e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 32603e501490SPaolo Abeni __mptcp_flush_join_list(sk); 32612e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3262c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 32632d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 32642d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3265c2e6048fSPaolo Abeni 3266c2e6048fSPaolo Abeni cond_resched(); 32676e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 32686e628cd3SPaolo Abeni } 3269c2e6048fSPaolo Abeni 327065a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 327165a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 327265a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3273490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 327465a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3275490274b4SPaolo Abeni */ 3276e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3277490274b4SPaolo Abeni __mptcp_set_connected(sk); 3278e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 327915cc1045SPaolo Abeni __mptcp_error_report(sk); 328043f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 328143f5b111SPaolo Abeni msk->last_snd = NULL; 328265a569b0SPaolo Abeni } 32836e628cd3SPaolo Abeni 328487952603SPaolo Abeni __mptcp_update_rmem(sk); 328514c441b5SPaolo Abeni } 328614c441b5SPaolo Abeni 3287bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3288bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3289bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3290bcd97734SPaolo Abeni */ 3291bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3292bcd97734SPaolo Abeni { 3293bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3294bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3295bcd97734SPaolo Abeni unsigned long timeout; 3296bcd97734SPaolo Abeni 3297bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3298bcd97734SPaolo Abeni return; 3299bcd97734SPaolo Abeni 3300bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3301bcd97734SPaolo Abeni if (tp->srtt_us) 3302bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3303bcd97734SPaolo Abeni else 3304bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3305bcd97734SPaolo Abeni timeout += jiffies; 3306bcd97734SPaolo Abeni 3307bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3308bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3309bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3310bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3311bcd97734SPaolo Abeni } 3312bcd97734SPaolo Abeni 3313b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3314b19bc294SPaolo Abeni { 3315b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3316b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3317b19bc294SPaolo Abeni 3318bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3319b19bc294SPaolo Abeni mptcp_data_lock(sk); 3320b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3321b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3322b19bc294SPaolo Abeni else 3323e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3324b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3325bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3326bcd97734SPaolo Abeni } 3327bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3328bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3329bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3330bcd97734SPaolo Abeni } 3331b19bc294SPaolo Abeni } 3332b19bc294SPaolo Abeni 33332c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 33342c5ebd00SPaolo Abeni { 33352c5ebd00SPaolo Abeni /* should never be called, 33362c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 33372c5ebd00SPaolo Abeni */ 33382c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 33392c5ebd00SPaolo Abeni return 0; 33402c5ebd00SPaolo Abeni } 33412c5ebd00SPaolo Abeni 33422c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 33432c5ebd00SPaolo Abeni { 33442c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 33452c5ebd00SPaolo Abeni } 33462c5ebd00SPaolo Abeni 3347cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3348f870fa0bSMat Martineau { 3349f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3350cec37a6eSPeter Krystad struct socket *ssock; 3351f870fa0bSMat Martineau 3352cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3353cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3354cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3355cec37a6eSPeter Krystad return -EINVAL; 3356f870fa0bSMat Martineau 3357cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3358cec37a6eSPeter Krystad } 3359f870fa0bSMat Martineau 3360cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3361cec37a6eSPeter Krystad { 3362cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3363cec37a6eSPeter Krystad struct mptcp_sock *msk; 3364cec37a6eSPeter Krystad struct sock *sk; 33656d0060f6SMat Martineau u64 ack_seq; 3366f870fa0bSMat Martineau 3367cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3368cec37a6eSPeter Krystad sk = subflow->conn; 3369cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3370cec37a6eSPeter Krystad 3371648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3372648ef4b8SMat Martineau 33736d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 33746d0060f6SMat Martineau ack_seq++; 3375648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3376648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 33776d0060f6SMat Martineau 3378cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3379cec37a6eSPeter Krystad * accessing the field below 3380cec37a6eSPeter Krystad */ 3381cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3382cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 33836d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3384eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 33856d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3386d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 33877439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 3388f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 33891b1c7a0eSPeter Krystad 33906c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3391a6b118feSFlorian Westphal 3392a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3393f870fa0bSMat Martineau } 3394f870fa0bSMat Martineau 3395866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3396cf7da0d6SPeter Krystad { 3397cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3398cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3399cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3400cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3401cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3402cf7da0d6SPeter Krystad } 3403cf7da0d6SPeter Krystad 3404e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3405f296234cSPeter Krystad { 3406e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3407f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3408f296234cSPeter Krystad struct sock *parent = (void *)msk; 34093e501490SPaolo Abeni bool ret = true; 3410f296234cSPeter Krystad 3411f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3412f296234cSPeter Krystad 3413f296234cSPeter Krystad /* mptcp socket already closing? */ 3414dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3415dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3416f296234cSPeter Krystad return false; 3417dc87efdbSFlorian Westphal } 3418f296234cSPeter Krystad 341970c708e8SKishen Maloor if (!list_empty(&subflow->node)) 3420b911c97cSFlorian Westphal goto out; 3421f296234cSPeter Krystad 34223e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 34233e501490SPaolo Abeni goto err_prohibited; 342410f6d46cSPaolo Abeni 34253e501490SPaolo Abeni /* active connections are already on conn_list. 34263e501490SPaolo Abeni * If we can't acquire msk socket lock here, let the release callback 34273e501490SPaolo Abeni * handle it 342810f6d46cSPaolo Abeni */ 34293e501490SPaolo Abeni mptcp_data_lock(parent); 34303e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 34313e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 34323e501490SPaolo Abeni if (ret) { 3433e16163b6SPaolo Abeni sock_hold(ssk); 34343e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3435e16163b6SPaolo Abeni } 34363e501490SPaolo Abeni } else { 34373e501490SPaolo Abeni sock_hold(ssk); 34383e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3439e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 34403e501490SPaolo Abeni } 34413e501490SPaolo Abeni mptcp_data_unlock(parent); 34423e501490SPaolo Abeni 3443dc87efdbSFlorian Westphal if (!ret) { 34443e501490SPaolo Abeni err_prohibited: 3445dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 344610f6d46cSPaolo Abeni return false; 3447dc87efdbSFlorian Westphal } 344810f6d46cSPaolo Abeni 3449917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 34500530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 34513e501490SPaolo Abeni 3452b911c97cSFlorian Westphal out: 3453b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 345410f6d46cSPaolo Abeni return true; 3455f296234cSPeter Krystad } 3456f296234cSPeter Krystad 345776e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 345876e2a55dSPaolo Abeni { 345976e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 346076e2a55dSPaolo Abeni 346176e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 346276e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 346376e2a55dSPaolo Abeni } 346476e2a55dSPaolo Abeni 34656511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 34666511882cSPaolo Abeni { 34676511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 34686511882cSPaolo Abeni } 34696511882cSPaolo Abeni 3470644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3471644807e3SFlorian Westphal { 3472644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3473644807e3SFlorian Westphal u64 delta; 3474644807e3SFlorian Westphal 3475644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3476644807e3SFlorian Westphal return -EINVAL; 3477644807e3SFlorian Westphal 3478644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3479644807e3SFlorian Westphal return 0; 3480644807e3SFlorian Westphal 3481644807e3SFlorian Westphal delta = msk->write_seq - v; 348207c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 348307c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 348407c2c7a3SPaolo Abeni 348507c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 348607c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 348707c2c7a3SPaolo Abeni * so ignore that status, too. 348807c2c7a3SPaolo Abeni */ 348907c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 349007c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 349107c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 349207c2c7a3SPaolo Abeni } 3493644807e3SFlorian Westphal if (delta > INT_MAX) 3494644807e3SFlorian Westphal delta = INT_MAX; 3495644807e3SFlorian Westphal 3496644807e3SFlorian Westphal return (int)delta; 3497644807e3SFlorian Westphal } 3498644807e3SFlorian Westphal 3499644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3500644807e3SFlorian Westphal { 3501644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3502644807e3SFlorian Westphal bool slow; 3503644807e3SFlorian Westphal int answ; 3504644807e3SFlorian Westphal 3505644807e3SFlorian Westphal switch (cmd) { 3506644807e3SFlorian Westphal case SIOCINQ: 3507644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3508644807e3SFlorian Westphal return -EINVAL; 3509644807e3SFlorian Westphal 3510644807e3SFlorian Westphal lock_sock(sk); 3511644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3512644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3513644807e3SFlorian Westphal release_sock(sk); 3514644807e3SFlorian Westphal break; 3515644807e3SFlorian Westphal case SIOCOUTQ: 3516644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3517644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3518644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3519644807e3SFlorian Westphal break; 3520644807e3SFlorian Westphal case SIOCOUTQNSD: 3521644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3522644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3523644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3524644807e3SFlorian Westphal break; 3525644807e3SFlorian Westphal default: 3526644807e3SFlorian Westphal return -ENOIOCTLCMD; 3527644807e3SFlorian Westphal } 3528644807e3SFlorian Westphal 3529644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3530644807e3SFlorian Westphal } 3531644807e3SFlorian Westphal 353254f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 353354f1944eSPaolo Abeni struct mptcp_subflow_context *subflow) 353454f1944eSPaolo Abeni { 353554f1944eSPaolo Abeni subflow->request_mptcp = 0; 353654f1944eSPaolo Abeni __mptcp_do_fallback(msk); 353754f1944eSPaolo Abeni } 353854f1944eSPaolo Abeni 353954f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 354054f1944eSPaolo Abeni { 354154f1944eSPaolo Abeni struct mptcp_subflow_context *subflow; 354254f1944eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 354354f1944eSPaolo Abeni struct socket *ssock; 354454f1944eSPaolo Abeni int err = -EINVAL; 354554f1944eSPaolo Abeni 354654f1944eSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 354754f1944eSPaolo Abeni if (!ssock) 354854f1944eSPaolo Abeni return -EINVAL; 354954f1944eSPaolo Abeni 355054f1944eSPaolo Abeni mptcp_token_destroy(msk); 355154f1944eSPaolo Abeni inet_sk_state_store(sk, TCP_SYN_SENT); 355254f1944eSPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 355354f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG 355454f1944eSPaolo Abeni /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 355554f1944eSPaolo Abeni * TCP option space. 355654f1944eSPaolo Abeni */ 355754f1944eSPaolo Abeni if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 355854f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 355954f1944eSPaolo Abeni #endif 356054f1944eSPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 356154f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 356254f1944eSPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 356354f1944eSPaolo Abeni } 356454f1944eSPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 356554f1944eSPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 356654f1944eSPaolo Abeni 356754f1944eSPaolo Abeni /* if reaching here via the fastopen/sendmsg path, the caller already 356854f1944eSPaolo Abeni * acquired the subflow socket lock, too. 356954f1944eSPaolo Abeni */ 357054f1944eSPaolo Abeni if (msk->is_sendmsg) 357154f1944eSPaolo Abeni err = __inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags, 1); 357254f1944eSPaolo Abeni else 357354f1944eSPaolo Abeni err = inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags); 357454f1944eSPaolo Abeni inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 357554f1944eSPaolo Abeni 357654f1944eSPaolo Abeni /* on successful connect, the msk state will be moved to established by 357754f1944eSPaolo Abeni * subflow_finish_connect() 357854f1944eSPaolo Abeni */ 357954f1944eSPaolo Abeni if (unlikely(err && err != -EINPROGRESS)) { 358054f1944eSPaolo Abeni inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 358154f1944eSPaolo Abeni return err; 358254f1944eSPaolo Abeni } 358354f1944eSPaolo Abeni 358454f1944eSPaolo Abeni mptcp_copy_inaddrs(sk, ssock->sk); 358554f1944eSPaolo Abeni 358654f1944eSPaolo Abeni /* unblocking connect, mptcp-level inet_stream_connect will error out 358754f1944eSPaolo Abeni * without changing the socket state, update it here. 358854f1944eSPaolo Abeni */ 358954f1944eSPaolo Abeni if (err == -EINPROGRESS) 359054f1944eSPaolo Abeni sk->sk_socket->state = ssock->state; 359154f1944eSPaolo Abeni return err; 359254f1944eSPaolo Abeni } 359354f1944eSPaolo Abeni 3594f870fa0bSMat Martineau static struct proto mptcp_prot = { 3595f870fa0bSMat Martineau .name = "MPTCP", 3596f870fa0bSMat Martineau .owner = THIS_MODULE, 3597f870fa0bSMat Martineau .init = mptcp_init_sock, 359854f1944eSPaolo Abeni .connect = mptcp_connect, 359918b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3600f870fa0bSMat Martineau .close = mptcp_close, 3601cf7da0d6SPeter Krystad .accept = mptcp_accept, 3602717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3603717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 360476e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 360579c0949eSPeter Krystad .destroy = mptcp_destroy, 3606f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3607644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3608f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 360914c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 36102c5ebd00SPaolo Abeni .hash = mptcp_hash, 36112c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3612cec37a6eSPeter Krystad .get_port = mptcp_get_port, 36136511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3614d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 36150defbb0aSEric Dumazet 3616d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 36170defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 36180defbb0aSEric Dumazet 3619d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3620d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3621989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3622d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3623f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 36242c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3625f870fa0bSMat Martineau .no_autobind = true, 3626f870fa0bSMat Martineau }; 3627f870fa0bSMat Martineau 36282303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 36292303f994SPeter Krystad { 36302303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 36312303f994SPeter Krystad struct socket *ssock; 3632cf7da0d6SPeter Krystad int err; 36332303f994SPeter Krystad 36342303f994SPeter Krystad lock_sock(sock->sk); 3635fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3636fa68018dSPaolo Abeni if (!ssock) { 3637fa68018dSPaolo Abeni err = -EINVAL; 36382303f994SPeter Krystad goto unlock; 36392303f994SPeter Krystad } 36402303f994SPeter Krystad 36412303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3642cf7da0d6SPeter Krystad if (!err) 3643cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 36442303f994SPeter Krystad 36452303f994SPeter Krystad unlock: 36462303f994SPeter Krystad release_sock(sock->sk); 36472303f994SPeter Krystad return err; 36482303f994SPeter Krystad } 36492303f994SPeter Krystad 36502303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 36512303f994SPeter Krystad int addr_len, int flags) 36522303f994SPeter Krystad { 365354f1944eSPaolo Abeni int ret; 36542303f994SPeter Krystad 36552303f994SPeter Krystad lock_sock(sock->sk); 365654f1944eSPaolo Abeni mptcp_sk(sock->sk)->connect_flags = flags; 365754f1944eSPaolo Abeni ret = __inet_stream_connect(sock, uaddr, addr_len, flags, 0); 36582303f994SPeter Krystad release_sock(sock->sk); 365954f1944eSPaolo Abeni return ret; 36602303f994SPeter Krystad } 36612303f994SPeter Krystad 3662cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3663cf7da0d6SPeter Krystad { 3664cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3665cf7da0d6SPeter Krystad struct socket *ssock; 3666cf7da0d6SPeter Krystad int err; 3667cf7da0d6SPeter Krystad 3668cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3669cf7da0d6SPeter Krystad 3670cf7da0d6SPeter Krystad lock_sock(sock->sk); 3671fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3672fa68018dSPaolo Abeni if (!ssock) { 3673fa68018dSPaolo Abeni err = -EINVAL; 3674cf7da0d6SPeter Krystad goto unlock; 3675cf7da0d6SPeter Krystad } 3676cf7da0d6SPeter Krystad 3677fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3678fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 36795e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 36805e20087dSFlorian Westphal 3681cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3682cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3683cf7da0d6SPeter Krystad if (!err) 3684cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3685cf7da0d6SPeter Krystad 3686cf7da0d6SPeter Krystad unlock: 3687cf7da0d6SPeter Krystad release_sock(sock->sk); 3688cf7da0d6SPeter Krystad return err; 3689cf7da0d6SPeter Krystad } 3690cf7da0d6SPeter Krystad 3691cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3692cf7da0d6SPeter Krystad int flags, bool kern) 3693cf7da0d6SPeter Krystad { 3694cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3695cf7da0d6SPeter Krystad struct socket *ssock; 3696cf7da0d6SPeter Krystad int err; 3697cf7da0d6SPeter Krystad 3698cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3699cf7da0d6SPeter Krystad 3700cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3701cf7da0d6SPeter Krystad if (!ssock) 370271ba088cSPaolo Abeni return -EINVAL; 3703cf7da0d6SPeter Krystad 3704cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3705d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3706cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3707cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 37080397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 37090397c6d8SPaolo Abeni 37104d54cc32SFlorian Westphal lock_sock(newsk); 37115b950ff4SPaolo Abeni 37125b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 37135b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 37145b950ff4SPaolo Abeni * we can notify it, if needed. 3715c8fe62f0SFlorian Westphal * 3716c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3717c8fe62f0SFlorian Westphal * the refcnt is still at least one. 37185b950ff4SPaolo Abeni */ 37195b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 37205b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 37215b950ff4SPaolo Abeni sock_hold(msk->first); 37225b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 37236c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 37245b950ff4SPaolo Abeni 37250397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 37265cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3727cf7da0d6SPeter Krystad 3728cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3729cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3730cf7da0d6SPeter Krystad */ 3731190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3732cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3733cf7da0d6SPeter Krystad 3734cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3735cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3736cf7da0d6SPeter Krystad } 37374d54cc32SFlorian Westphal release_sock(newsk); 3738cf7da0d6SPeter Krystad } 3739cf7da0d6SPeter Krystad 3740cf7da0d6SPeter Krystad return err; 3741cf7da0d6SPeter Krystad } 3742cf7da0d6SPeter Krystad 37438edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 37448edf0864SFlorian Westphal { 37458edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 37468edf0864SFlorian Westphal 37478edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3748dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37498edf0864SFlorian Westphal 37508edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 37518edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 37528edf0864SFlorian Westphal 37535cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 37546e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 37556e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 37566e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 37578edf0864SFlorian Westphal 37586e628cd3SPaolo Abeni return 0; 37598edf0864SFlorian Westphal } 37608edf0864SFlorian Westphal 37612303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 37622303f994SPeter Krystad struct poll_table_struct *wait) 37632303f994SPeter Krystad { 37641891c4a0SFlorian Westphal struct sock *sk = sock->sk; 37658ab183deSPaolo Abeni struct mptcp_sock *msk; 37662303f994SPeter Krystad __poll_t mask = 0; 37678a05661bSPaolo Abeni int state; 37682303f994SPeter Krystad 37691891c4a0SFlorian Westphal msk = mptcp_sk(sk); 37701891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 37711891c4a0SFlorian Westphal 37728a05661bSPaolo Abeni state = inet_sk_state_load(sk); 37736719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 377471ba088cSPaolo Abeni if (state == TCP_LISTEN) { 377571ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 377671ba088cSPaolo Abeni return 0; 377771ba088cSPaolo Abeni 377871ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 377971ba088cSPaolo Abeni } 37808a05661bSPaolo Abeni 37818a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 37828a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 37838edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 3784a42cf9d1SBenjamin Hesmans } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3785a42cf9d1SBenjamin Hesmans /* cf tcp_poll() note about TFO */ 3786a42cf9d1SBenjamin Hesmans mask |= EPOLLOUT | EPOLLWRNORM; 37878a05661bSPaolo Abeni } 3788dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3789dd913410SPaolo Abeni mask |= EPOLLHUP; 37901891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 37911891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 37921891c4a0SFlorian Westphal 3793d89e3ed7SPaolo Abeni /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 379415cc1045SPaolo Abeni smp_rmb(); 379515cc1045SPaolo Abeni if (sk->sk_err) 379615cc1045SPaolo Abeni mask |= EPOLLERR; 379715cc1045SPaolo Abeni 37982303f994SPeter Krystad return mask; 37992303f994SPeter Krystad } 38002303f994SPeter Krystad 3801e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3802e42f1ac6SFlorian Westphal .family = PF_INET, 3803e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38040a3cc579SPaolo Abeni .release = inet_release, 3805e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3806e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3807e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3808e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3809d2f77c53SPaolo Abeni .getname = inet_getname, 3810e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3811e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3812e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3813e42f1ac6SFlorian Westphal .listen = mptcp_listen, 381476e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3815e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3816e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3817e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3818e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3819e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3820e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3821e42f1ac6SFlorian Westphal }; 38222303f994SPeter Krystad 3823f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3824f870fa0bSMat Martineau .type = SOCK_STREAM, 3825f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3826f870fa0bSMat Martineau .prot = &mptcp_prot, 38272303f994SPeter Krystad .ops = &mptcp_stream_ops, 38282303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3829f870fa0bSMat Martineau }; 3830f870fa0bSMat Martineau 3831b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3832b19bc294SPaolo Abeni { 3833b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3834b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3835b19bc294SPaolo Abeni int work_done = 0; 3836b19bc294SPaolo Abeni 3837b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3838b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3839b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3840b19bc294SPaolo Abeni 3841b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3842b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3843b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3844b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3845b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3846b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3847b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3848b19bc294SPaolo Abeni * CPU that scheduled it. 3849b19bc294SPaolo Abeni */ 3850b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3851b19bc294SPaolo Abeni sock_put(ssk); 3852b19bc294SPaolo Abeni 3853b19bc294SPaolo Abeni if (++work_done == budget) 3854b19bc294SPaolo Abeni return budget; 3855b19bc294SPaolo Abeni } 3856b19bc294SPaolo Abeni 3857b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3858b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3859b19bc294SPaolo Abeni */ 3860b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3861b19bc294SPaolo Abeni return work_done; 3862b19bc294SPaolo Abeni } 3863b19bc294SPaolo Abeni 3864d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3865f870fa0bSMat Martineau { 3866b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3867b19bc294SPaolo Abeni int cpu; 3868b19bc294SPaolo Abeni 38692303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 38702303f994SPeter Krystad 3871d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3872d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3873d027236cSPaolo Abeni 3874b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3875b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3876b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3877b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 387816d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 387916d083e2SJakub Kicinski mptcp_napi_poll); 3880b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3881b19bc294SPaolo Abeni } 3882b19bc294SPaolo Abeni 38832303f994SPeter Krystad mptcp_subflow_init(); 38841b1c7a0eSPeter Krystad mptcp_pm_init(); 38852c5ebd00SPaolo Abeni mptcp_token_init(); 38862303f994SPeter Krystad 3887f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3888f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3889f870fa0bSMat Martineau 3890f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 38916771bfd9SFlorian Westphal 38926771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3893f870fa0bSMat Martineau } 3894f870fa0bSMat Martineau 3895f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3896e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3897e42f1ac6SFlorian Westphal .family = PF_INET6, 3898e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 38990a3cc579SPaolo Abeni .release = inet6_release, 3900e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3901e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3902e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3903e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3904d2f77c53SPaolo Abeni .getname = inet6_getname, 3905e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3906e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3907e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3908e42f1ac6SFlorian Westphal .listen = mptcp_listen, 390976e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3910e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3911e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3912e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3913e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3914e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3915e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3916e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 39173986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3918e42f1ac6SFlorian Westphal #endif 3919e42f1ac6SFlorian Westphal }; 3920e42f1ac6SFlorian Westphal 3921f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3922f870fa0bSMat Martineau 392379c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 392479c0949eSPeter Krystad { 392579c0949eSPeter Krystad mptcp_destroy(sk); 392679c0949eSPeter Krystad inet6_destroy_sock(sk); 392779c0949eSPeter Krystad } 392879c0949eSPeter Krystad 3929f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3930f870fa0bSMat Martineau .type = SOCK_STREAM, 3931f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3932f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 39332303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3934f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3935f870fa0bSMat Martineau }; 3936f870fa0bSMat Martineau 3937d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3938f870fa0bSMat Martineau { 3939f870fa0bSMat Martineau int err; 3940f870fa0bSMat Martineau 3941f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3942f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3943f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 394479c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3945b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3946f870fa0bSMat Martineau 3947f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3948f870fa0bSMat Martineau if (err) 3949f870fa0bSMat Martineau return err; 3950f870fa0bSMat Martineau 3951f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3952f870fa0bSMat Martineau if (err) 3953f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3954f870fa0bSMat Martineau 3955f870fa0bSMat Martineau return err; 3956f870fa0bSMat Martineau } 3957f870fa0bSMat Martineau #endif 3958