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; 1538268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1548268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1556511882cSPaolo Abeni mptcp_rmem_charge(sk, delta); 1568268ed4cSPaolo Abeni return true; 1578268ed4cSPaolo Abeni } 1588268ed4cSPaolo Abeni 159ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 160ab174ad8SPaolo Abeni struct sk_buff *from) 1616771bfd9SFlorian Westphal { 162ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 163ab174ad8SPaolo Abeni return false; 164ab174ad8SPaolo Abeni 165ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 166ab174ad8SPaolo Abeni } 167ab174ad8SPaolo Abeni 1686511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 1696511882cSPaolo Abeni { 170100fdd1fSEric Dumazet amount >>= PAGE_SHIFT; 171100fdd1fSEric Dumazet mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 1726511882cSPaolo Abeni __sk_mem_reduce_allocated(sk, amount); 1736511882cSPaolo Abeni } 1746511882cSPaolo Abeni 1756511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size) 1766511882cSPaolo Abeni { 1776511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1786511882cSPaolo Abeni int reclaimable; 1796511882cSPaolo Abeni 1806511882cSPaolo Abeni msk->rmem_fwd_alloc += size; 1816511882cSPaolo Abeni reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 1826511882cSPaolo Abeni 1836511882cSPaolo Abeni /* see sk_mem_uncharge() for the rationale behind the following schema */ 184d24141feSPaolo Abeni if (unlikely(reclaimable >= PAGE_SIZE)) 185d24141feSPaolo Abeni __mptcp_rmem_reclaim(sk, reclaimable); 1866511882cSPaolo Abeni } 1876511882cSPaolo Abeni 1886511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb) 1896511882cSPaolo Abeni { 1906511882cSPaolo Abeni unsigned int len = skb->truesize; 1916511882cSPaolo Abeni struct sock *sk = skb->sk; 1926511882cSPaolo Abeni 1936511882cSPaolo Abeni atomic_sub(len, &sk->sk_rmem_alloc); 1946511882cSPaolo Abeni mptcp_rmem_uncharge(sk, len); 1956511882cSPaolo Abeni } 1966511882cSPaolo Abeni 1976511882cSPaolo Abeni static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 1986511882cSPaolo Abeni { 1996511882cSPaolo Abeni skb_orphan(skb); 2006511882cSPaolo Abeni skb->sk = sk; 2016511882cSPaolo Abeni skb->destructor = mptcp_rfree; 2026511882cSPaolo Abeni atomic_add(skb->truesize, &sk->sk_rmem_alloc); 2036511882cSPaolo Abeni mptcp_rmem_charge(sk, skb->truesize); 2046511882cSPaolo Abeni } 2056511882cSPaolo Abeni 206ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 207ab174ad8SPaolo Abeni * - use mptcp seqs 208ab174ad8SPaolo Abeni * - don't cope with sacks 209ab174ad8SPaolo Abeni */ 210ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 211ab174ad8SPaolo Abeni { 212ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 213ab174ad8SPaolo Abeni struct rb_node **p, *parent; 214ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 215ab174ad8SPaolo Abeni struct sk_buff *skb1; 216ab174ad8SPaolo Abeni 217ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 218ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 219f3589be0SPaolo Abeni max_seq = atomic64_read(&msk->rcv_wnd_sent); 220ab174ad8SPaolo Abeni 22106242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 22206242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 223fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 224ab174ad8SPaolo Abeni /* out of window */ 225ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 226fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 227fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 228f3589be0SPaolo Abeni (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 22906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 230ab174ad8SPaolo Abeni return; 231ab174ad8SPaolo Abeni } 232ab174ad8SPaolo Abeni 233ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 23406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 235ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 236ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 237ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 238ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 239ab174ad8SPaolo Abeni goto end; 240ab174ad8SPaolo Abeni } 241ab174ad8SPaolo Abeni 242ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 243ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 244ab174ad8SPaolo Abeni */ 24506242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 24606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 24706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 248ab174ad8SPaolo Abeni return; 24906242e44SPaolo Abeni } 250ab174ad8SPaolo Abeni 251ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 252ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 25306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 254ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 255ab174ad8SPaolo Abeni p = &parent->rb_right; 256ab174ad8SPaolo Abeni goto insert; 257ab174ad8SPaolo Abeni } 258ab174ad8SPaolo Abeni 259ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 260ab174ad8SPaolo Abeni parent = NULL; 261ab174ad8SPaolo Abeni while (*p) { 262ab174ad8SPaolo Abeni parent = *p; 263ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 264ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 265ab174ad8SPaolo Abeni p = &parent->rb_left; 266ab174ad8SPaolo Abeni continue; 267ab174ad8SPaolo Abeni } 268ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 269ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 270ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 271ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 27206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 273ab174ad8SPaolo Abeni return; 274ab174ad8SPaolo Abeni } 275ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 276ab174ad8SPaolo Abeni /* partial overlap: 277ab174ad8SPaolo Abeni * | skb | 278ab174ad8SPaolo Abeni * | skb1 | 279ab174ad8SPaolo Abeni * continue traversing 280ab174ad8SPaolo Abeni */ 281ab174ad8SPaolo Abeni } else { 282ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 283ab174ad8SPaolo Abeni * Replace skb1 with skb. 284ab174ad8SPaolo Abeni */ 285ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 286ab174ad8SPaolo Abeni &msk->out_of_order_queue); 287ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 28806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 289ab174ad8SPaolo Abeni goto merge_right; 290ab174ad8SPaolo Abeni } 291ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 29206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 293ab174ad8SPaolo Abeni return; 294ab174ad8SPaolo Abeni } 295ab174ad8SPaolo Abeni p = &parent->rb_right; 296ab174ad8SPaolo Abeni } 29706242e44SPaolo Abeni 298ab174ad8SPaolo Abeni insert: 299ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 300ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 301ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 302ab174ad8SPaolo Abeni 303ab174ad8SPaolo Abeni merge_right: 304ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 305ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 306ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 307ab174ad8SPaolo Abeni break; 308ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 309ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 31006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 311ab174ad8SPaolo Abeni } 312ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 313ab174ad8SPaolo Abeni if (!skb1) 314ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 315ab174ad8SPaolo Abeni 316ab174ad8SPaolo Abeni end: 317ab174ad8SPaolo Abeni skb_condense(skb); 3186511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3196511882cSPaolo Abeni } 3206511882cSPaolo Abeni 3216511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 3226511882cSPaolo Abeni { 3236511882cSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3246511882cSPaolo Abeni int amt, amount; 3256511882cSPaolo Abeni 32669d93daeSPaolo Abeni if (size <= msk->rmem_fwd_alloc) 3276511882cSPaolo Abeni return true; 3286511882cSPaolo Abeni 32969d93daeSPaolo Abeni size -= msk->rmem_fwd_alloc; 3306511882cSPaolo Abeni amt = sk_mem_pages(size); 331100fdd1fSEric Dumazet amount = amt << PAGE_SHIFT; 3324aaa1685SPaolo Abeni if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 3336511882cSPaolo Abeni return false; 3346511882cSPaolo Abeni 3354aaa1685SPaolo Abeni msk->rmem_fwd_alloc += amount; 3366511882cSPaolo Abeni return true; 337ab174ad8SPaolo Abeni } 338ab174ad8SPaolo Abeni 339ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 340ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 341ab174ad8SPaolo Abeni size_t copy_len) 342ab174ad8SPaolo Abeni { 343ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3446771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 3454e637c70SFlorian Westphal struct sk_buff *tail; 346b7f653b2SFlorian Westphal bool has_rxtstamp; 3476771bfd9SFlorian Westphal 3486771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 3494e637c70SFlorian Westphal 3504e637c70SFlorian Westphal skb_ext_reset(skb); 3514e637c70SFlorian Westphal skb_orphan(skb); 3524e637c70SFlorian Westphal 3539c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 3546511882cSPaolo Abeni if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 3559c3f94e1SPaolo Abeni goto drop; 35672f96132SPaolo Abeni 357b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 358b7f653b2SFlorian Westphal 359ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 360ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 361ab174ad8SPaolo Abeni * value 362ab174ad8SPaolo Abeni */ 363ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 364ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 365ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 366b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 367ab174ad8SPaolo Abeni 368ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 369ab174ad8SPaolo Abeni /* in sequence */ 370917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3714e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3728268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 373ab174ad8SPaolo Abeni return true; 3744e637c70SFlorian Westphal 3756511882cSPaolo Abeni mptcp_set_owner_r(skb, sk); 3766771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 377ab174ad8SPaolo Abeni return true; 378ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 379ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 380ab174ad8SPaolo Abeni return false; 381ab174ad8SPaolo Abeni } 382ab174ad8SPaolo Abeni 383ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 384ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 385ab174ad8SPaolo Abeni */ 38606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3879c3f94e1SPaolo Abeni drop: 388ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 389ab174ad8SPaolo Abeni return false; 3906771bfd9SFlorian Westphal } 3916771bfd9SFlorian Westphal 39216a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 39316a9a9daSMat Martineau { 39416a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 39516a9a9daSMat Martineau 39616a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 39716a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 39816a9a9daSMat Martineau } 39916a9a9daSMat Martineau 400e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 401e16163b6SPaolo Abeni { 402e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 403e16163b6SPaolo Abeni return; 404e16163b6SPaolo Abeni 405e16163b6SPaolo Abeni sk->sk_state_change(sk); 406e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 407e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 408e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 409e16163b6SPaolo Abeni else 410e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 411e16163b6SPaolo Abeni } 412e16163b6SPaolo Abeni 4136e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 4146e628cd3SPaolo Abeni { 4156e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 4166e628cd3SPaolo Abeni 4176e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 4186e628cd3SPaolo Abeni ((1 << sk->sk_state) & 4196e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 4206e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 4216e628cd3SPaolo Abeni } 4226e628cd3SPaolo Abeni 42316a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 42416a9a9daSMat Martineau { 42516a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 42616a9a9daSMat Martineau 42716a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 4286e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 42916a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 43016a9a9daSMat Martineau 43116a9a9daSMat Martineau switch (sk->sk_state) { 43216a9a9daSMat Martineau case TCP_FIN_WAIT1: 43316a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 43416a9a9daSMat Martineau break; 43516a9a9daSMat Martineau case TCP_CLOSING: 43616a9a9daSMat Martineau case TCP_LAST_ACK: 43716a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 43816a9a9daSMat Martineau break; 43916a9a9daSMat Martineau } 44016a9a9daSMat Martineau 441e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 44216a9a9daSMat Martineau } 44316a9a9daSMat Martineau } 44416a9a9daSMat Martineau 4453721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 4463721b9b6SMat Martineau { 4473721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4483721b9b6SMat Martineau 4493721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 4503721b9b6SMat Martineau ((1 << sk->sk_state) & 4513721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 4523721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 4533721b9b6SMat Martineau 4543721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 4553721b9b6SMat Martineau if (seq) 4563721b9b6SMat Martineau *seq = rcv_data_fin_seq; 4573721b9b6SMat Martineau 4583721b9b6SMat Martineau return true; 4593721b9b6SMat Martineau } 4603721b9b6SMat Martineau } 4613721b9b6SMat Martineau 4623721b9b6SMat Martineau return false; 4633721b9b6SMat Martineau } 4643721b9b6SMat Martineau 4656477dd39SMat Martineau static void mptcp_set_datafin_timeout(const struct sock *sk) 4666477dd39SMat Martineau { 4676477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 468877d11f0SMat Martineau u32 retransmits; 4696477dd39SMat Martineau 470877d11f0SMat Martineau retransmits = min_t(u32, icsk->icsk_retransmits, 471877d11f0SMat Martineau ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 472877d11f0SMat Martineau 473877d11f0SMat Martineau mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 4746477dd39SMat Martineau } 4756477dd39SMat Martineau 47633d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4773721b9b6SMat Martineau { 4783721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4793721b9b6SMat Martineau } 4803721b9b6SMat Martineau 48133d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 48233d41c9cSPaolo Abeni { 48333d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 48433d41c9cSPaolo Abeni 48571b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 48671b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 48733d41c9cSPaolo Abeni } 48833d41c9cSPaolo Abeni 48933d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 49033d41c9cSPaolo Abeni { 49133d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 49233d41c9cSPaolo Abeni long tout = 0; 49333d41c9cSPaolo Abeni 49433d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 49533d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 49633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 49733d41c9cSPaolo Abeni } 49833d41c9cSPaolo Abeni 49976a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk) 500fd897679SPaolo Abeni { 501fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 50220bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 503fd897679SPaolo Abeni } 504fd897679SPaolo Abeni 505a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk) 506a6574302SMat Martineau { 507a6574302SMat Martineau if (tcp_can_send_ack(ssk)) 508a6574302SMat Martineau tcp_send_ack(ssk); 509a6574302SMat Martineau } 510a6574302SMat Martineau 511f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk) 5127ed90803SPaolo Abeni { 51375e908c3SPaolo Abeni bool slow; 5147ed90803SPaolo Abeni 51575e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 516a6574302SMat Martineau __mptcp_subflow_send_ack(ssk); 51775e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 518fd897679SPaolo Abeni } 519340fa666SMat Martineau 520340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 521340fa666SMat Martineau { 522340fa666SMat Martineau struct mptcp_subflow_context *subflow; 523340fa666SMat Martineau 524340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 525340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 526ea4ca586SPaolo Abeni } 527ea4ca586SPaolo Abeni 528fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 529fd897679SPaolo Abeni { 53075e908c3SPaolo Abeni bool slow; 531fd897679SPaolo Abeni 53275e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 533fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 534fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 53575e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 536fde56eeaSPaolo Abeni } 537fde56eeaSPaolo Abeni 538fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 539fde56eeaSPaolo Abeni { 540fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 541c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 542fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 543fde56eeaSPaolo Abeni 544fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 545fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 546fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 547fde56eeaSPaolo Abeni (rx_empty && ack_pending & 548fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 549ea4ca586SPaolo Abeni } 550fd897679SPaolo Abeni 551fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 552fd897679SPaolo Abeni { 553e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 554fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 555e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 556fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 557fde56eeaSPaolo Abeni bool cleanup, rx_empty; 558e3859603SPaolo Abeni 559fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 560ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 561fd897679SPaolo Abeni 562fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 563fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 564fd897679SPaolo Abeni 565fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 566fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5677ed90803SPaolo Abeni } 5687ed90803SPaolo Abeni } 5697ed90803SPaolo Abeni 5707ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5713721b9b6SMat Martineau { 5723721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5733721b9b6SMat Martineau u64 rcv_data_fin_seq; 5747ed90803SPaolo Abeni bool ret = false; 5753721b9b6SMat Martineau 576781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5777ed90803SPaolo Abeni return ret; 5783721b9b6SMat Martineau 5793721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5803721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5813721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5823721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5833721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5843721b9b6SMat Martineau * the msk state. 5853721b9b6SMat Martineau * 5863721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5873721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5883721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5893721b9b6SMat Martineau * when catching up. 5903721b9b6SMat Martineau */ 5913721b9b6SMat Martineau 5923721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 593917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5943721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5953721b9b6SMat Martineau 5963721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 59716a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 5983721b9b6SMat Martineau 5993721b9b6SMat Martineau switch (sk->sk_state) { 6003721b9b6SMat Martineau case TCP_ESTABLISHED: 6013721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 6023721b9b6SMat Martineau break; 6033721b9b6SMat Martineau case TCP_FIN_WAIT1: 6043721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 6053721b9b6SMat Martineau break; 6063721b9b6SMat Martineau case TCP_FIN_WAIT2: 6073721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 6083721b9b6SMat Martineau break; 6093721b9b6SMat Martineau default: 6103721b9b6SMat Martineau /* Other states not expected */ 6113721b9b6SMat Martineau WARN_ON_ONCE(1); 6123721b9b6SMat Martineau break; 6133721b9b6SMat Martineau } 6143721b9b6SMat Martineau 6157ed90803SPaolo Abeni ret = true; 616fd897679SPaolo Abeni mptcp_send_ack(msk); 617e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 6183721b9b6SMat Martineau } 6197ed90803SPaolo Abeni return ret; 6203721b9b6SMat Martineau } 6213721b9b6SMat Martineau 6226771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 6236771bfd9SFlorian Westphal struct sock *ssk, 6246771bfd9SFlorian Westphal unsigned int *bytes) 6256771bfd9SFlorian Westphal { 6266771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 627600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 6286771bfd9SFlorian Westphal unsigned int moved = 0; 6296771bfd9SFlorian Westphal bool more_data_avail; 6306771bfd9SFlorian Westphal struct tcp_sock *tp; 6316771bfd9SFlorian Westphal bool done = false; 63213c7ba0cSFlorian Westphal int sk_rbuf; 63313c7ba0cSFlorian Westphal 63413c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 63513c7ba0cSFlorian Westphal 63613c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 63713c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 63813c7ba0cSFlorian Westphal 63913c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 64013c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 64113c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 64213c7ba0cSFlorian Westphal } 64313c7ba0cSFlorian Westphal } 644600911ffSFlorian Westphal 645ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 6466771bfd9SFlorian Westphal tp = tcp_sk(ssk); 6476771bfd9SFlorian Westphal do { 6486771bfd9SFlorian Westphal u32 map_remaining, offset; 6496771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 6506771bfd9SFlorian Westphal struct sk_buff *skb; 6516771bfd9SFlorian Westphal bool fin; 6526771bfd9SFlorian Westphal 6536771bfd9SFlorian Westphal /* try to move as much data as available */ 6546771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 6556771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 6566771bfd9SFlorian Westphal 6576771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 658d9fb8c50SPaolo Abeni if (!skb) { 659d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 660d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 661d9fb8c50SPaolo Abeni * can enter an infinite loop 662d9fb8c50SPaolo Abeni */ 663d9fb8c50SPaolo Abeni if (!moved) 664d9fb8c50SPaolo Abeni done = true; 6656771bfd9SFlorian Westphal break; 666d9fb8c50SPaolo Abeni } 6676771bfd9SFlorian Westphal 668e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 669e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 670e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 671e1ff9e82SDavide Caratti * be sure to adjust the size 672e1ff9e82SDavide Caratti */ 673e1ff9e82SDavide Caratti map_remaining = skb->len; 674e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 675e1ff9e82SDavide Caratti } 676e1ff9e82SDavide Caratti 6776771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6786771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6796771bfd9SFlorian Westphal if (fin) { 6806771bfd9SFlorian Westphal done = true; 6816771bfd9SFlorian Westphal seq++; 6826771bfd9SFlorian Westphal } 6836771bfd9SFlorian Westphal 6846771bfd9SFlorian Westphal if (offset < skb->len) { 6856771bfd9SFlorian Westphal size_t len = skb->len - offset; 6866771bfd9SFlorian Westphal 6876771bfd9SFlorian Westphal if (tp->urg_data) 6886771bfd9SFlorian Westphal done = true; 6896771bfd9SFlorian Westphal 690ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6916771bfd9SFlorian Westphal moved += len; 692ab174ad8SPaolo Abeni seq += len; 6936771bfd9SFlorian Westphal 6946771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6956771bfd9SFlorian Westphal break; 6966771bfd9SFlorian Westphal } else { 6976771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6986771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 6996771bfd9SFlorian Westphal done = true; 7006771bfd9SFlorian Westphal } 7016771bfd9SFlorian Westphal 7026771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 7036771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 704600911ffSFlorian Westphal 70513c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 706600911ffSFlorian Westphal done = true; 707600911ffSFlorian Westphal break; 708600911ffSFlorian Westphal } 7096771bfd9SFlorian Westphal } while (more_data_avail); 7106771bfd9SFlorian Westphal 7116719331cSPaolo Abeni *bytes += moved; 7126771bfd9SFlorian Westphal return done; 7136771bfd9SFlorian Westphal } 7146771bfd9SFlorian Westphal 71587952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 716ab174ad8SPaolo Abeni { 717ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 718ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 719ab174ad8SPaolo Abeni bool moved = false; 720ab174ad8SPaolo Abeni struct rb_node *p; 721ab174ad8SPaolo Abeni u64 end_seq; 722ab174ad8SPaolo Abeni 723ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 72406242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 725ab174ad8SPaolo Abeni while (p) { 726ab174ad8SPaolo Abeni skb = rb_to_skb(p); 727ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 728ab174ad8SPaolo Abeni break; 729ab174ad8SPaolo Abeni 730ab174ad8SPaolo Abeni p = rb_next(p); 731ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 732ab174ad8SPaolo Abeni 733ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 734ab174ad8SPaolo Abeni msk->ack_seq))) { 735ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 73606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 737ab174ad8SPaolo Abeni continue; 738ab174ad8SPaolo Abeni } 739ab174ad8SPaolo Abeni 740ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 741ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 742ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 743ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 744ab174ad8SPaolo Abeni 745ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 74606242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 74706242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 74806242e44SPaolo Abeni delta); 749ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 7502c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += delta; 751ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 752ab174ad8SPaolo Abeni } 753ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 754ab174ad8SPaolo Abeni moved = true; 755ab174ad8SPaolo Abeni } 756ab174ad8SPaolo Abeni return moved; 757ab174ad8SPaolo Abeni } 758ab174ad8SPaolo Abeni 7592e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 7602e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 7612e52213cSFlorian Westphal */ 76299d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 7632e52213cSFlorian Westphal { 7642e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 7652e52213cSFlorian Westphal unsigned int moved = 0; 7662e52213cSFlorian Westphal 7672e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 76887952603SPaolo Abeni __mptcp_ofo_queue(msk); 769499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 770499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 771499ada50SPaolo Abeni __mptcp_error_report(sk); 772499ada50SPaolo Abeni else 773e9d09bacSPaolo Abeni __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 774499ada50SPaolo Abeni } 775ab174ad8SPaolo Abeni 776ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 777ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 778ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 779ab174ad8SPaolo Abeni * do it. 780ab174ad8SPaolo Abeni */ 781ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 782ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 78399d1055cSPaolo Abeni return moved > 0; 7842e52213cSFlorian Westphal } 7852e52213cSFlorian Westphal 7862e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 787101f6f85SFlorian Westphal { 7886719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 789101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 79013c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 791101f6f85SFlorian Westphal 792d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 793d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 794d7b1bfd0SPaolo Abeni * more data to the msk receive queue 795d7b1bfd0SPaolo Abeni */ 796d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 797d7b1bfd0SPaolo Abeni return; 798d7b1bfd0SPaolo Abeni 79913c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 80013c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 80113c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 80213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 80313c7ba0cSFlorian Westphal 80499d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 805ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 806ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 80799d1055cSPaolo Abeni return; 808ce599c51SPaolo Abeni } 8092e52213cSFlorian Westphal 81099d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 81199d1055cSPaolo Abeni mptcp_data_lock(sk); 812612f71d7SPaolo Abeni if (move_skbs_to_msk(msk, ssk)) 813101f6f85SFlorian Westphal sk->sk_data_ready(sk); 814612f71d7SPaolo Abeni 81599d1055cSPaolo Abeni mptcp_data_unlock(sk); 81699d1055cSPaolo Abeni } 817101f6f85SFlorian Westphal 8183e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 819ec3edaa7SPeter Krystad { 8203e501490SPaolo Abeni struct sock *sk = (struct sock *)msk; 8215cf92bbaSPaolo Abeni 8223e501490SPaolo Abeni if (sk->sk_state != TCP_ESTABLISHED) 82378962489SFlorian Westphal return false; 824ec3edaa7SPeter Krystad 8253e501490SPaolo Abeni /* attach to msk socket only after we are sure we will deal with it 8263e501490SPaolo Abeni * at close time 8273e501490SPaolo Abeni */ 8283e501490SPaolo Abeni if (sk->sk_socket && !ssk->sk_socket) 8293e501490SPaolo Abeni mptcp_sock_graft(ssk, sk->sk_socket); 83078962489SFlorian Westphal 8313e501490SPaolo Abeni mptcp_propagate_sndbuf((struct sock *)msk, ssk); 8323e501490SPaolo Abeni mptcp_sockopt_sync_locked(msk, ssk); 8333e501490SPaolo Abeni return true; 83478962489SFlorian Westphal } 83578962489SFlorian Westphal 8363e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk) 83778962489SFlorian Westphal { 8383e501490SPaolo Abeni struct mptcp_subflow_context *tmp, *subflow; 8393e501490SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 84078962489SFlorian Westphal 8413e501490SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 8423e501490SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 8433e501490SPaolo Abeni bool slow = lock_sock_fast(ssk); 8443e501490SPaolo Abeni 8453e501490SPaolo Abeni list_move_tail(&subflow->node, &msk->conn_list); 8463e501490SPaolo Abeni if (!__mptcp_finish_join(msk, ssk)) 8473e501490SPaolo Abeni mptcp_subflow_reset(ssk); 8483e501490SPaolo Abeni unlock_sock_fast(ssk, slow); 84978962489SFlorian Westphal } 850ec3edaa7SPeter Krystad } 851ec3edaa7SPeter Krystad 852b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 853b51f9b80SPaolo Abeni { 854b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 855b51f9b80SPaolo Abeni } 856b51f9b80SPaolo Abeni 857b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 858b51f9b80SPaolo Abeni { 859b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 860b51f9b80SPaolo Abeni unsigned long tout; 861b51f9b80SPaolo Abeni 862e16163b6SPaolo Abeni /* prevent rescheduling on close */ 863e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 864e16163b6SPaolo Abeni return; 865e16163b6SPaolo Abeni 86633d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 867b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 868b51f9b80SPaolo Abeni } 869b51f9b80SPaolo Abeni 870ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 871ba8f48f7SPaolo Abeni { 872ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 873ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 874ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 875ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 876ba8f48f7SPaolo Abeni */ 877ba8f48f7SPaolo Abeni sock_hold(sk); 878ba8f48f7SPaolo Abeni return true; 879ba8f48f7SPaolo Abeni } 880ba8f48f7SPaolo Abeni return false; 881ba8f48f7SPaolo Abeni } 882ba8f48f7SPaolo Abeni 88359832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 88459832e24SFlorian Westphal { 885ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 886ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 88759832e24SFlorian Westphal } 88859832e24SFlorian Westphal 8895969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8905969856aSPaolo Abeni { 8915969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 8925969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 8935969856aSPaolo Abeni int receivers = 0; 8945969856aSPaolo Abeni 8955969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 8965969856aSPaolo Abeni receivers += !subflow->rx_eof; 897e16163b6SPaolo Abeni if (receivers) 898e16163b6SPaolo Abeni return; 8995969856aSPaolo Abeni 900e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 9015969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 9025969856aSPaolo Abeni * to msk, when all subflows agree on it 9035969856aSPaolo Abeni */ 9045969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 9055969856aSPaolo Abeni 9065969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 9075969856aSPaolo Abeni sk->sk_data_ready(sk); 9085969856aSPaolo Abeni } 909e16163b6SPaolo Abeni 910e16163b6SPaolo Abeni switch (sk->sk_state) { 911e16163b6SPaolo Abeni case TCP_ESTABLISHED: 912e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 913e16163b6SPaolo Abeni break; 914e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 91526aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 91626aa2314SPaolo Abeni break; 91726aa2314SPaolo Abeni case TCP_FIN_WAIT2: 918e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 919e16163b6SPaolo Abeni break; 920e16163b6SPaolo Abeni default: 921e16163b6SPaolo Abeni return; 922e16163b6SPaolo Abeni } 923e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 9245969856aSPaolo Abeni } 9255969856aSPaolo Abeni 9267a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 9277a6a6cbcSPaolo Abeni { 9287a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 9297a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 9307a6a6cbcSPaolo Abeni 9317a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 9327a6a6cbcSPaolo Abeni 9337a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 93499d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 9357a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 9367a6a6cbcSPaolo Abeni } 9377a6a6cbcSPaolo Abeni 9387a6a6cbcSPaolo Abeni return NULL; 9397a6a6cbcSPaolo Abeni } 9407a6a6cbcSPaolo Abeni 9413f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 94257040755SPaolo Abeni const struct sk_buff *skb, 94357040755SPaolo Abeni const struct mptcp_ext *mpext) 9446d0060f6SMat Martineau { 94557040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 94657040755SPaolo Abeni return false; 94757040755SPaolo Abeni 9485a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 9495a369ca6SPaolo Abeni * mapping has not been xmitted yet 9505a369ca6SPaolo Abeni */ 9515a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 9525a369ca6SPaolo Abeni !mpext->frozen; 95357040755SPaolo Abeni } 95457040755SPaolo Abeni 95529249eacSPaolo Abeni /* we can append data to the given data frag if: 95629249eacSPaolo Abeni * - there is space available in the backing page_frag 95729249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 95829249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 95929249eacSPaolo Abeni */ 96018b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 96118b683bfSPaolo Abeni const struct page_frag *pfrag, 96218b683bfSPaolo Abeni const struct mptcp_data_frag *df) 96318b683bfSPaolo Abeni { 96418b683bfSPaolo Abeni return df && pfrag->page == df->page && 965d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 96629249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 96718b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 96818b683bfSPaolo Abeni } 96918b683bfSPaolo Abeni 970d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 97118b683bfSPaolo Abeni { 972d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9737948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 974d027236cSPaolo Abeni } 975d027236cSPaolo Abeni 976d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 977d027236cSPaolo Abeni { 978d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 979d027236cSPaolo Abeni 98018b683bfSPaolo Abeni list_del(&dfrag->list); 981d027236cSPaolo Abeni dfrag_uncharge(sk, len); 98218b683bfSPaolo Abeni put_page(dfrag->page); 98318b683bfSPaolo Abeni } 98418b683bfSPaolo Abeni 9856e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 98618b683bfSPaolo Abeni { 98718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 98818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 989e1ff9e82SDavide Caratti u64 snd_una; 990e1ff9e82SDavide Caratti 991e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 992e1ff9e82SDavide Caratti * plain TCP 993e1ff9e82SDavide Caratti */ 994e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 9957439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 9967439d687SPaolo Abeni 9977439d687SPaolo Abeni snd_una = msk->snd_una; 99818b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 99918b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 100018b683bfSPaolo Abeni break; 100118b683bfSPaolo Abeni 10021e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10031e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10041e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1005d9ca1de8SPaolo Abeni break; 10061e1d9d6fSPaolo Abeni 10071e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10081e1d9d6fSPaolo Abeni } 10091e1d9d6fSPaolo Abeni 1010d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1011d027236cSPaolo Abeni } 1012d027236cSPaolo Abeni 10137948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10147948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 101553eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 101653eb4c38SPaolo Abeni 10171e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10181e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10191e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 102053eb4c38SPaolo Abeni goto out; 10211e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10221e1d9d6fSPaolo Abeni goto out; 10231e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10241e1d9d6fSPaolo Abeni } 10257948f6ccSFlorian Westphal 10267948f6ccSFlorian Westphal dfrag->data_seq += delta; 102753eb4c38SPaolo Abeni dfrag->offset += delta; 10287948f6ccSFlorian Westphal dfrag->data_len -= delta; 1029d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10307948f6ccSFlorian Westphal 10317948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10327948f6ccSFlorian Westphal } 10337948f6ccSFlorian Westphal 10341e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 10351e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 10361e1d9d6fSPaolo Abeni msk->recovery = false; 10371e1d9d6fSPaolo Abeni 103853eb4c38SPaolo Abeni out: 10393241a9c0SFlorian Westphal if (snd_una == READ_ONCE(msk->snd_nxt) && 10403241a9c0SFlorian Westphal snd_una == READ_ONCE(msk->write_seq)) { 104133d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 10426e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10436e628cd3SPaolo Abeni } else { 10446e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10456e628cd3SPaolo Abeni } 10466e628cd3SPaolo Abeni } 10476e628cd3SPaolo Abeni 1048417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1049417789dfSPaolo Abeni { 1050765ff425SPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 1051765ff425SPaolo Abeni 1052417789dfSPaolo Abeni __mptcp_clean_una(sk); 1053417789dfSPaolo Abeni mptcp_write_space(sk); 1054417789dfSPaolo Abeni } 1055417789dfSPaolo Abeni 1056b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1057b5941f06SPaolo Abeni { 1058b5941f06SPaolo Abeni mptcp_data_lock(sk); 1059b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1060b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1061b5941f06SPaolo Abeni } 1062b5941f06SPaolo Abeni 1063724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 106418b683bfSPaolo Abeni { 1065d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1066d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1067d9ca1de8SPaolo Abeni bool first = true; 1068d9ca1de8SPaolo Abeni 106918b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1070d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1071d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1072d9ca1de8SPaolo Abeni 1073d9ca1de8SPaolo Abeni if (first) 1074d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1075d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1076d9ca1de8SPaolo Abeni first = false; 1077d9ca1de8SPaolo Abeni } 1078724cfd2eSPaolo Abeni } 1079724cfd2eSPaolo Abeni 1080724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1081724cfd2eSPaolo Abeni * data 1082724cfd2eSPaolo Abeni */ 1083724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1084724cfd2eSPaolo Abeni { 1085724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1086724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1087724cfd2eSPaolo Abeni return true; 1088724cfd2eSPaolo Abeni 1089724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 109018b683bfSPaolo Abeni return false; 109118b683bfSPaolo Abeni } 109218b683bfSPaolo Abeni 109318b683bfSPaolo Abeni static struct mptcp_data_frag * 109418b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 109518b683bfSPaolo Abeni int orig_offset) 109618b683bfSPaolo Abeni { 109718b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 109818b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 109918b683bfSPaolo Abeni 110018b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 110118b683bfSPaolo Abeni dfrag->data_len = 0; 110218b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 110318b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 110418b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1105d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 110618b683bfSPaolo Abeni dfrag->page = pfrag->page; 110718b683bfSPaolo Abeni 110818b683bfSPaolo Abeni return dfrag; 110918b683bfSPaolo Abeni } 111018b683bfSPaolo Abeni 1111caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1112caf971dfSPaolo Abeni int mss_now; 1113caf971dfSPaolo Abeni int size_goal; 1114d9ca1de8SPaolo Abeni u16 limit; 1115d9ca1de8SPaolo Abeni u16 sent; 1116d9ca1de8SPaolo Abeni unsigned int flags; 11171094c6feSPaolo Abeni bool data_lock_held; 1118caf971dfSPaolo Abeni }; 1119caf971dfSPaolo Abeni 1120b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1121b713d006SPaolo Abeni u64 data_seq, int avail_size) 11226f8a612aSFlorian Westphal { 11236f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 1124b713d006SPaolo Abeni u64 mptcp_snd_wnd; 11256f8a612aSFlorian Westphal 11266f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11276f8a612aSFlorian Westphal return avail_size; 11286f8a612aSFlorian Westphal 1129b713d006SPaolo Abeni mptcp_snd_wnd = window_end - data_seq; 1130b713d006SPaolo Abeni avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 11316f8a612aSFlorian Westphal 113292be2f52SPaolo Abeni if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1133b713d006SPaolo Abeni tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 113492be2f52SPaolo Abeni MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 113592be2f52SPaolo Abeni } 11366f8a612aSFlorian Westphal 11376f8a612aSFlorian Westphal return avail_size; 11386f8a612aSFlorian Westphal } 11396f8a612aSFlorian Westphal 1140724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1141724cfd2eSPaolo Abeni { 1142724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1143724cfd2eSPaolo Abeni 1144724cfd2eSPaolo Abeni if (!mpext) 1145724cfd2eSPaolo Abeni return false; 1146724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1147724cfd2eSPaolo Abeni return true; 1148724cfd2eSPaolo Abeni } 1149724cfd2eSPaolo Abeni 11506e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1151724cfd2eSPaolo Abeni { 1152724cfd2eSPaolo Abeni struct sk_buff *skb; 1153724cfd2eSPaolo Abeni 11546e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1155724cfd2eSPaolo Abeni if (likely(skb)) { 11566e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1157724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1158a52fe46eSEric Dumazet skb->ip_summed = CHECKSUM_PARTIAL; 1159f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1160724cfd2eSPaolo Abeni return skb; 1161724cfd2eSPaolo Abeni } 1162724cfd2eSPaolo Abeni __kfree_skb(skb); 1163724cfd2eSPaolo Abeni } else { 1164724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1165724cfd2eSPaolo Abeni } 1166724cfd2eSPaolo Abeni return NULL; 1167724cfd2eSPaolo Abeni } 1168724cfd2eSPaolo Abeni 1169f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1170724cfd2eSPaolo Abeni { 1171724cfd2eSPaolo Abeni struct sk_buff *skb; 1172724cfd2eSPaolo Abeni 11736e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1174724cfd2eSPaolo Abeni if (!skb) 1175f70cad10SPaolo Abeni return NULL; 1176724cfd2eSPaolo Abeni 1177724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1178f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1179f70cad10SPaolo Abeni return skb; 1180724cfd2eSPaolo Abeni } 11813ef3905aSYonglong Li tcp_skb_tsorted_anchor_cleanup(skb); 1182724cfd2eSPaolo Abeni kfree_skb(skb); 1183f70cad10SPaolo Abeni return NULL; 1184724cfd2eSPaolo Abeni } 1185724cfd2eSPaolo Abeni 1186f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1187724cfd2eSPaolo Abeni { 11881094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1189724cfd2eSPaolo Abeni 11901094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1191724cfd2eSPaolo Abeni } 1192724cfd2eSPaolo Abeni 1193d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1194d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1195d0cc2987SGeliang Tang */ 1196d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1197d0cc2987SGeliang Tang { 1198d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1199d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1200d0cc2987SGeliang Tang int offset = skb->len - added; 1201d0cc2987SGeliang Tang 1202d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1203d0cc2987SGeliang Tang } 1204d0cc2987SGeliang Tang 12051e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk, 12061e39e5a3SGeliang Tang struct sock *ssk, 12071e39e5a3SGeliang Tang struct mptcp_ext *mpext) 12081e39e5a3SGeliang Tang { 12091e39e5a3SGeliang Tang if (!mpext) 12101e39e5a3SGeliang Tang return; 12111e39e5a3SGeliang Tang 12121e39e5a3SGeliang Tang mpext->infinite_map = 1; 12131e39e5a3SGeliang Tang mpext->data_len = 0; 12141e39e5a3SGeliang Tang 1215104125b8SGeliang Tang MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 12161e39e5a3SGeliang Tang mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 12171e39e5a3SGeliang Tang pr_fallback(msk); 1218d51991e2SPaolo Abeni mptcp_do_fallback(ssk); 12191e39e5a3SGeliang Tang } 12201e39e5a3SGeliang Tang 122157040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1222d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1223caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 122457040755SPaolo Abeni { 1225d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1226f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12276d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12286f8a612aSFlorian Westphal bool zero_window_probe = false; 12296d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1230f70cad10SPaolo Abeni bool can_coalesce = false; 1231f70cad10SPaolo Abeni bool reuse_skb = true; 1232f70cad10SPaolo Abeni struct sk_buff *skb; 1233f70cad10SPaolo Abeni size_t copy; 1234f70cad10SPaolo Abeni int i; 12356d0060f6SMat Martineau 1236e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1237d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12386d0060f6SMat Martineau 1239f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1240f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1241f70cad10SPaolo Abeni return 0; 1242f70cad10SPaolo Abeni 1243c886d702SPaolo Abeni if (unlikely(!__tcp_can_send(ssk))) 1244c886d702SPaolo Abeni return -EAGAIN; 1245c886d702SPaolo Abeni 1246d9ca1de8SPaolo Abeni /* compute send limit */ 1247d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1248f70cad10SPaolo Abeni copy = info->size_goal; 1249f70cad10SPaolo Abeni 125057040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1251f70cad10SPaolo Abeni if (skb && copy > skb->len) { 125257040755SPaolo Abeni /* Limit the write to the size available in the 125357040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 125457040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 125557040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 125657040755SPaolo Abeni * SSN association set here 125757040755SPaolo Abeni */ 1258d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12591094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 126057040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12611094c6feSPaolo Abeni goto alloc_skb; 12621094c6feSPaolo Abeni } 12631094c6feSPaolo Abeni 1264f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1265f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1266657b991aSKuniyuki Iwashima if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1267f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1268f70cad10SPaolo Abeni goto alloc_skb; 126915e6ca97SPaolo Abeni } 127018b683bfSPaolo Abeni 1271f70cad10SPaolo Abeni copy -= skb->len; 1272f70cad10SPaolo Abeni } else { 12731094c6feSPaolo Abeni alloc_skb: 1274f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1275f70cad10SPaolo Abeni if (!skb) 1276f70cad10SPaolo Abeni return -ENOMEM; 1277f70cad10SPaolo Abeni 1278f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1279f70cad10SPaolo Abeni reuse_skb = false; 1280f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1281f70cad10SPaolo Abeni } 12821094c6feSPaolo Abeni 12836f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1284b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1285f70cad10SPaolo Abeni if (copy == 0) { 12867439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12877439d687SPaolo Abeni 1288f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 128927728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12906f8a612aSFlorian Westphal return 0; 12916f8a612aSFlorian Westphal } 12926f8a612aSFlorian Westphal 1293f70cad10SPaolo Abeni zero_window_probe = true; 1294f70cad10SPaolo Abeni data_seq = snd_una - 1; 1295f70cad10SPaolo Abeni copy = 1; 129657040755SPaolo Abeni 1297f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1298f70cad10SPaolo Abeni * ssk write queue 1299f70cad10SPaolo Abeni */ 1300f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1301f70cad10SPaolo Abeni } 1302f70cad10SPaolo Abeni 1303f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1304f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 130527728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1306e2223995SPaolo Abeni return -ENOMEM; 130735759383SFlorian Westphal } 130818b683bfSPaolo Abeni 1309f70cad10SPaolo Abeni if (can_coalesce) { 1310f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1311f70cad10SPaolo Abeni } else { 1312f70cad10SPaolo Abeni get_page(dfrag->page); 1313f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 131457040755SPaolo Abeni } 131557040755SPaolo Abeni 1316f70cad10SPaolo Abeni skb->len += copy; 1317f70cad10SPaolo Abeni skb->data_len += copy; 1318f70cad10SPaolo Abeni skb->truesize += copy; 1319f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1320f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1321f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1322f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1323f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1324f70cad10SPaolo Abeni 1325f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1326f70cad10SPaolo Abeni if (reuse_skb) { 1327f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1328f70cad10SPaolo Abeni mpext->data_len += copy; 1329f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1330f70cad10SPaolo Abeni goto out; 1331724cfd2eSPaolo Abeni } 13326d0060f6SMat Martineau 13336d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1334d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13356d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1336f70cad10SPaolo Abeni mpext->data_len = copy; 13376d0060f6SMat Martineau mpext->use_map = 1; 13386d0060f6SMat Martineau mpext->dsn64 = 1; 13396d0060f6SMat Martineau 13406d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13416d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13426d0060f6SMat Martineau mpext->dsn64); 13436d0060f6SMat Martineau 13446f8a612aSFlorian Westphal if (zero_window_probe) { 1345f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13466f8a612aSFlorian Westphal mpext->frozen = 1; 1347d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1348f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13496f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1350d0cc2987SGeliang Tang return 0; 13516f8a612aSFlorian Westphal } 135257040755SPaolo Abeni out: 1353d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1354f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13551e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13561e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13570eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1358f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1359f70cad10SPaolo Abeni return copy; 13606d0060f6SMat Martineau } 13616d0060f6SMat Martineau 1362d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1363d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1364d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1365d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1366d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1367d5f49190SPaolo Abeni 1368d5f49190SPaolo Abeni struct subflow_send_info { 1369d5f49190SPaolo Abeni struct sock *ssk; 13703ce0852cSPaolo Abeni u64 linger_time; 1371d5f49190SPaolo Abeni }; 1372d5f49190SPaolo Abeni 1373ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1374ff5a0b42SPaolo Abeni { 1375ff5a0b42SPaolo Abeni if (!subflow->stale) 1376ff5a0b42SPaolo Abeni return; 1377ff5a0b42SPaolo Abeni 1378ff5a0b42SPaolo Abeni subflow->stale = 0; 1379fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1380ff5a0b42SPaolo Abeni } 1381ff5a0b42SPaolo Abeni 1382ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1383ff5a0b42SPaolo Abeni { 1384ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1385ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1386ff5a0b42SPaolo Abeni 1387ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1388ff5a0b42SPaolo Abeni return false; 1389ff5a0b42SPaolo Abeni 1390ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1391ff5a0b42SPaolo Abeni } 1392ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1393ff5a0b42SPaolo Abeni } 1394ff5a0b42SPaolo Abeni 13953ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13963ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13973ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13983ce0852cSPaolo Abeni 139933d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 140033d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 140133d41c9cSPaolo Abeni * additionally updates the rtx timeout 140233d41c9cSPaolo Abeni */ 14035cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1404f296234cSPeter Krystad { 14053ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1406f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 140733d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14083ce0852cSPaolo Abeni u32 pace, burst, wmem; 1409d5f49190SPaolo Abeni int i, nr_active = 0; 1410d5f49190SPaolo Abeni struct sock *ssk; 14113ce0852cSPaolo Abeni u64 linger_time; 141233d41c9cSPaolo Abeni long tout = 0; 1413f296234cSPeter Krystad 141433d41c9cSPaolo Abeni sock_owned_by_me(sk); 1415f296234cSPeter Krystad 1416d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1417d5f49190SPaolo Abeni if (!msk->first) 1418f296234cSPeter Krystad return NULL; 1419c886d702SPaolo Abeni return __tcp_can_send(msk->first) && 1420c886d702SPaolo Abeni sk_stream_memory_free(msk->first) ? msk->first : NULL; 1421f296234cSPeter Krystad } 1422f296234cSPeter Krystad 1423d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1424d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1425d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 142633d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 142733d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1428d5f49190SPaolo Abeni return msk->last_snd; 142933d41c9cSPaolo Abeni } 1430f296234cSPeter Krystad 1431d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14323ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1433d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14343ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1435d5f49190SPaolo Abeni } 14363ce0852cSPaolo Abeni 1437d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1438e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1439d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1440d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1441f296234cSPeter Krystad continue; 1442d5f49190SPaolo Abeni 144333d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1444d5f49190SPaolo Abeni nr_active += !subflow->backup; 14453ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14463ce0852cSPaolo Abeni if (unlikely(!pace)) { 14473ce0852cSPaolo Abeni /* init pacing rate from socket */ 14483ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14493ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1450d5f49190SPaolo Abeni if (!pace) 1451d5f49190SPaolo Abeni continue; 14523ce0852cSPaolo Abeni } 1453d5f49190SPaolo Abeni 14543ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14553ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1456d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14573ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1458d5f49190SPaolo Abeni } 1459f296234cSPeter Krystad } 146033d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1461f296234cSPeter Krystad 1462d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1463d5f49190SPaolo Abeni if (!nr_active) 14643ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1465d5f49190SPaolo Abeni 14663ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14673ce0852cSPaolo Abeni * faster flow, we need to: 14683ce0852cSPaolo Abeni * - estimate the faster flow linger time 14693ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14703ce0852cSPaolo Abeni * by the faster flow 14713ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14723ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14733ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14743ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14753ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14763ce0852cSPaolo Abeni */ 14773ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1478b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1479d5f49190SPaolo Abeni return NULL; 14803ce0852cSPaolo Abeni 1481b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14823ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1483b713d006SPaolo Abeni if (!burst) { 1484b713d006SPaolo Abeni msk->last_snd = NULL; 1485b713d006SPaolo Abeni return ssk; 1486b713d006SPaolo Abeni } 1487b713d006SPaolo Abeni 14883ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14893ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14903ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14913ce0852cSPaolo Abeni burst + wmem); 14923ce0852cSPaolo Abeni msk->last_snd = ssk; 14933ce0852cSPaolo Abeni msk->snd_burst = burst; 14943ce0852cSPaolo Abeni return ssk; 1495f296234cSPeter Krystad } 1496f296234cSPeter Krystad 1497b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1498d9ca1de8SPaolo Abeni { 1499d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1500d9ca1de8SPaolo Abeni release_sock(ssk); 1501d9ca1de8SPaolo Abeni } 1502d9ca1de8SPaolo Abeni 15030d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15040d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15050d199e43SFlorian Westphal u32 sent) 15060d199e43SFlorian Westphal { 15070d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15080d199e43SFlorian Westphal 15090d199e43SFlorian Westphal dfrag->already_sent += sent; 15100d199e43SFlorian Westphal 15110d199e43SFlorian Westphal msk->snd_burst -= sent; 15120d199e43SFlorian Westphal 15130d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15140d199e43SFlorian Westphal 15150d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15160d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15170d199e43SFlorian Westphal * old segments. 15180d199e43SFlorian Westphal * 15190d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15200d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15210d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15220d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15230d199e43SFlorian Westphal */ 15240d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15250d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15260d199e43SFlorian Westphal } 15270d199e43SFlorian Westphal 15288b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15293241a9c0SFlorian Westphal { 1530e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1531e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15323241a9c0SFlorian Westphal } 15333241a9c0SFlorian Westphal 1534ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1535d9ca1de8SPaolo Abeni { 1536d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1537d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1538d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1539d9ca1de8SPaolo Abeni .flags = flags, 1540d9ca1de8SPaolo Abeni }; 1541*0522b424SGeliang Tang bool do_check_data_fin = false; 1542d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1543*0522b424SGeliang Tang int len; 1544d9ca1de8SPaolo Abeni 1545d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1546d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1547d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1548d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1549d9ca1de8SPaolo Abeni while (len > 0) { 1550d9ca1de8SPaolo Abeni int ret = 0; 1551d9ca1de8SPaolo Abeni 1552d9ca1de8SPaolo Abeni prev_ssk = ssk; 15535cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1554d9ca1de8SPaolo Abeni 15559758f40eSPaolo Abeni /* First check. If the ssk has changed since 15569758f40eSPaolo Abeni * the last round, release prev_ssk 1557d9ca1de8SPaolo Abeni */ 1558d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1559b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1560d9ca1de8SPaolo Abeni if (!ssk) 1561d9ca1de8SPaolo Abeni goto out; 1562d9ca1de8SPaolo Abeni 15639758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15649758f40eSPaolo Abeni * from the previous one, otherwise we are still 15659758f40eSPaolo Abeni * helding the relevant lock 15669758f40eSPaolo Abeni */ 15679758f40eSPaolo Abeni if (ssk != prev_ssk) 1568d9ca1de8SPaolo Abeni lock_sock(ssk); 1569d9ca1de8SPaolo Abeni 1570d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1571d9ca1de8SPaolo Abeni if (ret <= 0) { 1572c886d702SPaolo Abeni if (ret == -EAGAIN) 1573c886d702SPaolo Abeni continue; 1574b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1575d9ca1de8SPaolo Abeni goto out; 1576d9ca1de8SPaolo Abeni } 1577d9ca1de8SPaolo Abeni 1578*0522b424SGeliang Tang do_check_data_fin = true; 1579d9ca1de8SPaolo Abeni info.sent += ret; 1580d9ca1de8SPaolo Abeni len -= ret; 15810d199e43SFlorian Westphal 15820d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1583d9ca1de8SPaolo Abeni } 1584d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1585d9ca1de8SPaolo Abeni } 1586d9ca1de8SPaolo Abeni 1587d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1588d9ca1de8SPaolo Abeni if (ssk) 1589b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1590d9ca1de8SPaolo Abeni 1591d9ca1de8SPaolo Abeni out: 159233d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1593d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1594d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 1595*0522b424SGeliang Tang if (do_check_data_fin) 1596d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1597d9ca1de8SPaolo Abeni } 1598d9ca1de8SPaolo Abeni 15996e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 16006e628cd3SPaolo Abeni { 16016e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16021094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16031094c6feSPaolo Abeni .data_lock_held = true, 16041094c6feSPaolo Abeni }; 16056e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1606b19bc294SPaolo Abeni struct sock *xmit_ssk; 16076e628cd3SPaolo Abeni int len, copied = 0; 1608b19bc294SPaolo Abeni bool first = true; 16096e628cd3SPaolo Abeni 16106e628cd3SPaolo Abeni info.flags = 0; 16116e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16126e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16136e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16146e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16156e628cd3SPaolo Abeni while (len > 0) { 16166e628cd3SPaolo Abeni int ret = 0; 16176e628cd3SPaolo Abeni 1618b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1619b19bc294SPaolo Abeni * check for a different subflow usage only after 1620b19bc294SPaolo Abeni * spooling the first chunk of data 1621b19bc294SPaolo Abeni */ 1622b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1623b19bc294SPaolo Abeni if (!xmit_ssk) 1624b19bc294SPaolo Abeni goto out; 1625b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1626bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1627bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1628b19bc294SPaolo Abeni goto out; 1629b19bc294SPaolo Abeni } 1630b19bc294SPaolo Abeni 16316e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16326e628cd3SPaolo Abeni if (ret <= 0) 16336e628cd3SPaolo Abeni goto out; 16346e628cd3SPaolo Abeni 16356e628cd3SPaolo Abeni info.sent += ret; 16366e628cd3SPaolo Abeni copied += ret; 16376e628cd3SPaolo Abeni len -= ret; 1638b19bc294SPaolo Abeni first = false; 16390d199e43SFlorian Westphal 16400d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16416e628cd3SPaolo Abeni } 16426e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16436e628cd3SPaolo Abeni } 16446e628cd3SPaolo Abeni 16456e628cd3SPaolo Abeni out: 16466e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16476e628cd3SPaolo Abeni * not going to flush it via release_sock() 16486e628cd3SPaolo Abeni */ 16496e628cd3SPaolo Abeni if (copied) { 16506e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16516e628cd3SPaolo Abeni info.size_goal); 1652d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1653d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1654d09d818eSPaolo Abeni 16556e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16566e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16576e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16586e628cd3SPaolo Abeni } 16596e628cd3SPaolo Abeni } 16606e628cd3SPaolo Abeni 16615cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16625cf92bbaSPaolo Abeni { 16635cf92bbaSPaolo Abeni /* enable autotune */ 16645cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16655cf92bbaSPaolo Abeni 16665cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16675cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16685cf92bbaSPaolo Abeni } 16695cf92bbaSPaolo Abeni 1670f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1671f870fa0bSMat Martineau { 1672f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 167317091708SFlorian Westphal struct page_frag *pfrag; 16746d0060f6SMat Martineau size_t copied = 0; 1675caf971dfSPaolo Abeni int ret = 0; 16766d0060f6SMat Martineau long timeo; 1677f870fa0bSMat Martineau 1678987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1679987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1680f870fa0bSMat Martineau return -EOPNOTSUPP; 1681f870fa0bSMat Martineau 1682987858e5SPaolo Abeni /* silently ignore everything else */ 1683987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1684987858e5SPaolo Abeni 16856511882cSPaolo Abeni lock_sock(sk); 16861954b860SMat Martineau 16871954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 16881954b860SMat Martineau 16891954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 16901954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 16911954b860SMat Martineau if (ret) 16921954b860SMat Martineau goto out; 16931954b860SMat Martineau } 16941954b860SMat Martineau 169517091708SFlorian Westphal pfrag = sk_page_frag(sk); 169618b683bfSPaolo Abeni 1697d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1698724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1699d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1700d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1701d9ca1de8SPaolo Abeni size_t psize, offset; 1702d9ca1de8SPaolo Abeni 170357baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 170457baaf28SMat Martineau ret = -EPIPE; 170557baaf28SMat Martineau goto out; 170657baaf28SMat Martineau } 170757baaf28SMat Martineau 1708d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1709d9ca1de8SPaolo Abeni * page allocator 1710fb529e62SFlorian Westphal */ 1711d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1712d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1713d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1714d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1715d9ca1de8SPaolo Abeni goto wait_for_memory; 17166e628cd3SPaolo Abeni 1717d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1718d9ca1de8SPaolo Abeni goto wait_for_memory; 1719d9ca1de8SPaolo Abeni 1720d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1721d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1722fb529e62SFlorian Westphal } 1723fb529e62SFlorian Westphal 1724d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1725d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1726d9ca1de8SPaolo Abeni * anyway 1727d9ca1de8SPaolo Abeni */ 1728d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1729d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1730d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1731724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17328ce568edSPaolo Abeni 17336511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1734d9ca1de8SPaolo Abeni goto wait_for_memory; 1735d9ca1de8SPaolo Abeni 1736d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1737d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1738d9ca1de8SPaolo Abeni ret = -EFAULT; 1739d9ca1de8SPaolo Abeni goto out; 1740d9ca1de8SPaolo Abeni } 1741d9ca1de8SPaolo Abeni 1742d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17436511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1744d9ca1de8SPaolo Abeni copied += psize; 1745d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1746d9ca1de8SPaolo Abeni frag_truesize += psize; 1747d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1748d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1749d9ca1de8SPaolo Abeni 1750d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1751d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1752d9ca1de8SPaolo Abeni */ 1753d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1754d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1755d9ca1de8SPaolo Abeni get_page(dfrag->page); 1756d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1757d9ca1de8SPaolo Abeni if (!msk->first_pending) 1758d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1759d9ca1de8SPaolo Abeni } 1760e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1761d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1762d9ca1de8SPaolo Abeni !dfrag_collapsed); 1763d9ca1de8SPaolo Abeni 1764d9ca1de8SPaolo Abeni continue; 1765d9ca1de8SPaolo Abeni 1766d9ca1de8SPaolo Abeni wait_for_memory: 17675cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1768c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1769f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1770f296234cSPeter Krystad if (ret) 1771f296234cSPeter Krystad goto out; 1772cec37a6eSPeter Krystad } 1773cec37a6eSPeter Krystad 177413e16037SPaolo Abeni if (copied) 1775c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1776da51aef5SPaolo Abeni 17771954b860SMat Martineau out: 1778cec37a6eSPeter Krystad release_sock(sk); 17798555c6bfSPaolo Abeni return copied ? : ret; 1780f870fa0bSMat Martineau } 1781f870fa0bSMat Martineau 17826771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 17836771bfd9SFlorian Westphal struct msghdr *msg, 1784b7f653b2SFlorian Westphal size_t len, int flags, 1785b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1786b7f653b2SFlorian Westphal int *cmsg_flags) 17876771bfd9SFlorian Westphal { 1788ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 17896771bfd9SFlorian Westphal int copied = 0; 17906771bfd9SFlorian Westphal 1791ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 17926771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 17936771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 17946771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 17956771bfd9SFlorian Westphal int err; 17966771bfd9SFlorian Westphal 1797d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 17986771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 17996771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18006771bfd9SFlorian Westphal if (!copied) 18016771bfd9SFlorian Westphal return err; 18026771bfd9SFlorian Westphal break; 18036771bfd9SFlorian Westphal } 1804d976092cSPaolo Abeni } 18056771bfd9SFlorian Westphal 1806b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1807b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1808b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1809b7f653b2SFlorian Westphal } 1810b7f653b2SFlorian Westphal 18116771bfd9SFlorian Westphal copied += count; 18126771bfd9SFlorian Westphal 18136771bfd9SFlorian Westphal if (count < data_len) { 18142c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18156771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18162c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18172c9e7765SFlorian Westphal } 18186771bfd9SFlorian Westphal break; 18196771bfd9SFlorian Westphal } 18206771bfd9SFlorian Westphal 1821ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 182287952603SPaolo Abeni /* we will bulk release the skb memory later */ 182387952603SPaolo Abeni skb->destructor = NULL; 1824ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 182587952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18266771bfd9SFlorian Westphal __kfree_skb(skb); 1827ca4fb892SYonglong Li } 18286771bfd9SFlorian Westphal 18296771bfd9SFlorian Westphal if (copied >= len) 18306771bfd9SFlorian Westphal break; 18316771bfd9SFlorian Westphal } 18326771bfd9SFlorian Westphal 18336771bfd9SFlorian Westphal return copied; 18346771bfd9SFlorian Westphal } 18356771bfd9SFlorian Westphal 1836a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1837a6b118feSFlorian Westphal * 1838a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1839a6b118feSFlorian Westphal */ 1840a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1841a6b118feSFlorian Westphal { 1842a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1843a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1844a6b118feSFlorian Westphal u32 time, advmss = 1; 1845a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1846a6b118feSFlorian Westphal 1847a6b118feSFlorian Westphal sock_owned_by_me(sk); 1848a6b118feSFlorian Westphal 1849a6b118feSFlorian Westphal if (copied <= 0) 1850a6b118feSFlorian Westphal return; 1851a6b118feSFlorian Westphal 1852a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1853a6b118feSFlorian Westphal 1854a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1855a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1856a6b118feSFlorian Westphal 1857a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1858a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1859a6b118feSFlorian Westphal return; 1860a6b118feSFlorian Westphal 1861a6b118feSFlorian Westphal rtt_us = 0; 1862a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1863a6b118feSFlorian Westphal const struct tcp_sock *tp; 1864a6b118feSFlorian Westphal u64 sf_rtt_us; 1865a6b118feSFlorian Westphal u32 sf_advmss; 1866a6b118feSFlorian Westphal 1867a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1868a6b118feSFlorian Westphal 1869a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1870a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1871a6b118feSFlorian Westphal 1872a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1873a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1874a6b118feSFlorian Westphal } 1875a6b118feSFlorian Westphal 1876a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1877a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1878a6b118feSFlorian Westphal return; 1879a6b118feSFlorian Westphal 1880a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1881a6b118feSFlorian Westphal goto new_measure; 1882a6b118feSFlorian Westphal 188378047648SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1884a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1885a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1886a6b118feSFlorian Westphal u64 rcvwin, grow; 1887a6b118feSFlorian Westphal 1888a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1889a6b118feSFlorian Westphal 1890a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1891a6b118feSFlorian Westphal 1892a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1893a6b118feSFlorian Westphal rcvwin += (grow << 1); 1894a6b118feSFlorian Westphal 1895a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1896a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1897a6b118feSFlorian Westphal rcvmem += 128; 1898a6b118feSFlorian Westphal 1899a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1900a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 190102739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1902a6b118feSFlorian Westphal 1903a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1904a6b118feSFlorian Westphal u32 window_clamp; 1905a6b118feSFlorian Westphal 1906a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1907a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1908a6b118feSFlorian Westphal 1909a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1910a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1911a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1912a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1913a6b118feSFlorian Westphal */ 1914a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1915a6b118feSFlorian Westphal struct sock *ssk; 1916c76c6956SPaolo Abeni bool slow; 1917a6b118feSFlorian Westphal 1918a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1919c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1920a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1921a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1922c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1923c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1924a6b118feSFlorian Westphal } 1925a6b118feSFlorian Westphal } 1926a6b118feSFlorian Westphal } 1927a6b118feSFlorian Westphal 1928a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1929a6b118feSFlorian Westphal new_measure: 1930a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1931a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1932a6b118feSFlorian Westphal } 1933a6b118feSFlorian Westphal 193487952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 193587952603SPaolo Abeni { 193687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 193787952603SPaolo Abeni 193887952603SPaolo Abeni if (!msk->rmem_released) 193987952603SPaolo Abeni return; 194087952603SPaolo Abeni 194187952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19426511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1943ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 194487952603SPaolo Abeni } 194587952603SPaolo Abeni 194687952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 194787952603SPaolo Abeni { 194887952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 194987952603SPaolo Abeni 195087952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 195187952603SPaolo Abeni } 195287952603SPaolo Abeni 1953e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19546771bfd9SFlorian Westphal { 195587952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19566771bfd9SFlorian Westphal unsigned int moved = 0; 195787952603SPaolo Abeni bool ret, done; 1958d5f49190SPaolo Abeni 19596771bfd9SFlorian Westphal do { 19606771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 196165f49fe7SFlorian Westphal bool slowpath; 19626771bfd9SFlorian Westphal 196387952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 196487952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 196587952603SPaolo Abeni * that is an unlikely slow path. 196687952603SPaolo Abeni */ 196787952603SPaolo Abeni if (likely(!ssk)) 19686771bfd9SFlorian Westphal break; 19696771bfd9SFlorian Westphal 197065f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 197187952603SPaolo Abeni mptcp_data_lock(sk); 1972e3859603SPaolo Abeni __mptcp_update_rmem(sk); 19736771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 197487952603SPaolo Abeni mptcp_data_unlock(sk); 1975499ada50SPaolo Abeni 1976499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 1977499ada50SPaolo Abeni __mptcp_error_report(sk); 197865f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 19796771bfd9SFlorian Westphal } while (!done); 19806771bfd9SFlorian Westphal 198187952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 198287952603SPaolo Abeni ret = moved > 0; 198387952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 198487952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 198587952603SPaolo Abeni mptcp_data_lock(sk); 198687952603SPaolo Abeni __mptcp_update_rmem(sk); 198787952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 198887952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 198987952603SPaolo Abeni mptcp_data_unlock(sk); 1990ab174ad8SPaolo Abeni } 199187952603SPaolo Abeni if (ret) 199287952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 199387952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 19946771bfd9SFlorian Westphal } 19956771bfd9SFlorian Westphal 19962c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 19972c9e7765SFlorian Westphal { 19982c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 19992c9e7765SFlorian Westphal const struct sk_buff *skb; 20002c9e7765SFlorian Westphal 20012c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 20022c9e7765SFlorian Westphal if (skb) { 20032c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 20042c9e7765SFlorian Westphal 20052c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 20062c9e7765SFlorian Westphal return INT_MAX; 20072c9e7765SFlorian Westphal 20082c9e7765SFlorian Westphal return (unsigned int)hint_val; 20092c9e7765SFlorian Westphal } 20102c9e7765SFlorian Westphal 20112c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20122c9e7765SFlorian Westphal return 1; 20132c9e7765SFlorian Westphal 20142c9e7765SFlorian Westphal return 0; 20152c9e7765SFlorian Westphal } 20162c9e7765SFlorian Westphal 2017f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2018ec095263SOliver Hartkopp int flags, int *addr_len) 2019f870fa0bSMat Martineau { 2020f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2021b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2022b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20237a6a6cbcSPaolo Abeni int target; 20247a6a6cbcSPaolo Abeni long timeo; 2025f870fa0bSMat Martineau 2026cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2027cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2028cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2029cb9d80f4SPaolo Abeni 20306511882cSPaolo Abeni lock_sock(sk); 2031fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2032fd897679SPaolo Abeni copied = -ENOTCONN; 2033fd897679SPaolo Abeni goto out_err; 2034fd897679SPaolo Abeni } 2035fd897679SPaolo Abeni 2036ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20377a6a6cbcSPaolo Abeni 20387a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20397a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20407a6a6cbcSPaolo Abeni 20412c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20422c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20432c9e7765SFlorian Westphal 204405e3eceaSEric Dumazet while (copied < len) { 2045e3859603SPaolo Abeni int bytes_read; 20467a6a6cbcSPaolo Abeni 2047b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20486771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20497a6a6cbcSPaolo Abeni if (!copied) 20507a6a6cbcSPaolo Abeni copied = bytes_read; 20516771bfd9SFlorian Westphal goto out_err; 2052cec37a6eSPeter Krystad } 2053cec37a6eSPeter Krystad 20547a6a6cbcSPaolo Abeni copied += bytes_read; 20557a6a6cbcSPaolo Abeni 2056ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2057fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2058ea4ca586SPaolo Abeni 2059e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2060e3859603SPaolo Abeni continue; 2061e3859603SPaolo Abeni 20627a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 20637a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 20647a6a6cbcSPaolo Abeni */ 20657a6a6cbcSPaolo Abeni if (copied >= target) 20667a6a6cbcSPaolo Abeni break; 20677a6a6cbcSPaolo Abeni 20687a6a6cbcSPaolo Abeni if (copied) { 20697a6a6cbcSPaolo Abeni if (sk->sk_err || 20707a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 20717a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 20727a6a6cbcSPaolo Abeni !timeo || 20737a6a6cbcSPaolo Abeni signal_pending(current)) 20747a6a6cbcSPaolo Abeni break; 20757a6a6cbcSPaolo Abeni } else { 20767a6a6cbcSPaolo Abeni if (sk->sk_err) { 20777a6a6cbcSPaolo Abeni copied = sock_error(sk); 20787a6a6cbcSPaolo Abeni break; 20797a6a6cbcSPaolo Abeni } 20807a6a6cbcSPaolo Abeni 20815969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 20825969856aSPaolo Abeni mptcp_check_for_eof(msk); 20835969856aSPaolo Abeni 208487952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 208587952603SPaolo Abeni /* race breaker: the shutdown could be after the 208687952603SPaolo Abeni * previous receive queue check 208787952603SPaolo Abeni */ 2088e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 208987952603SPaolo Abeni continue; 20907a6a6cbcSPaolo Abeni break; 209187952603SPaolo Abeni } 20927a6a6cbcSPaolo Abeni 20937a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 20947a6a6cbcSPaolo Abeni copied = -ENOTCONN; 20957a6a6cbcSPaolo Abeni break; 20967a6a6cbcSPaolo Abeni } 20977a6a6cbcSPaolo Abeni 20987a6a6cbcSPaolo Abeni if (!timeo) { 20997a6a6cbcSPaolo Abeni copied = -EAGAIN; 21007a6a6cbcSPaolo Abeni break; 21017a6a6cbcSPaolo Abeni } 21027a6a6cbcSPaolo Abeni 21037a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21047a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21057a6a6cbcSPaolo Abeni break; 21067a6a6cbcSPaolo Abeni } 21077a6a6cbcSPaolo Abeni } 21087a6a6cbcSPaolo Abeni 21097a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2110612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21117a6a6cbcSPaolo Abeni } 21123c90e377SPaolo Abeni 21136771bfd9SFlorian Westphal out_err: 2114b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2115b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2116b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21172c9e7765SFlorian Westphal 21182c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21192c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21202c9e7765SFlorian Westphal 21212c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21222c9e7765SFlorian Westphal } 2123b7f653b2SFlorian Westphal } 2124b7f653b2SFlorian Westphal 2125612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2126612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2127612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2128ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2129a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2130a6b118feSFlorian Westphal 2131cec37a6eSPeter Krystad release_sock(sk); 2132cec37a6eSPeter Krystad return copied; 2133cec37a6eSPeter Krystad } 2134cec37a6eSPeter Krystad 2135b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2136b51f9b80SPaolo Abeni { 2137b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2138b51f9b80SPaolo Abeni icsk_retransmit_timer); 2139b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21402d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2141b51f9b80SPaolo Abeni 2142b51f9b80SPaolo Abeni bh_lock_sock(sk); 2143b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21442d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21452d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21462d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2147b51f9b80SPaolo Abeni } else { 2148b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2149e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2150b51f9b80SPaolo Abeni } 2151b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2152b51f9b80SPaolo Abeni sock_put(sk); 2153b51f9b80SPaolo Abeni } 2154b51f9b80SPaolo Abeni 2155e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2156e16163b6SPaolo Abeni { 2157e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2158e16163b6SPaolo Abeni 2159e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2160b6d69fc8SFlorian Westphal sock_put(sk); 2161e16163b6SPaolo Abeni } 2162e16163b6SPaolo Abeni 21633b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 21643b1d6210SPaolo Abeni * level. 21653b1d6210SPaolo Abeni * 21663b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 21673b1d6210SPaolo Abeni */ 2168ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 21693b1d6210SPaolo Abeni { 217071b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 21713b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 217271b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 21733b1d6210SPaolo Abeni 21743b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 21753b1d6210SPaolo Abeni 2176d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2177d9ca1de8SPaolo Abeni return NULL; 2178d5f49190SPaolo Abeni 21793b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 21803b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21813b1d6210SPaolo Abeni 2182ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2183d5f49190SPaolo Abeni continue; 2184d5f49190SPaolo Abeni 218571b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 218671b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 218771b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 218871b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2189860975c6SFlorian Westphal continue; 2190860975c6SFlorian Westphal } 21913b1d6210SPaolo Abeni 21923b1d6210SPaolo Abeni if (subflow->backup) { 21933b1d6210SPaolo Abeni if (!backup) 21943b1d6210SPaolo Abeni backup = ssk; 21953b1d6210SPaolo Abeni continue; 21963b1d6210SPaolo Abeni } 21973b1d6210SPaolo Abeni 219871b7dec2SPaolo Abeni if (!pick) 219971b7dec2SPaolo Abeni pick = ssk; 22003b1d6210SPaolo Abeni } 22013b1d6210SPaolo Abeni 220271b7dec2SPaolo Abeni if (pick) 220371b7dec2SPaolo Abeni return pick; 220471b7dec2SPaolo Abeni 220571b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 220671b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22073b1d6210SPaolo Abeni } 22083b1d6210SPaolo Abeni 220917aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 221017aee05dSFlorian Westphal { 221117aee05dSFlorian Westphal if (msk->subflow) { 221217aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 221317aee05dSFlorian Westphal msk->subflow = NULL; 221417aee05dSFlorian Westphal } 221517aee05dSFlorian Westphal } 221617aee05dSFlorian Westphal 22171e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22181e1d9d6fSPaolo Abeni { 22191e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22201e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22211e1d9d6fSPaolo Abeni 22221e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22231e1d9d6fSPaolo Abeni return false; 22241e1d9d6fSPaolo Abeni 22251e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22261e1d9d6fSPaolo Abeni return false; 22271e1d9d6fSPaolo Abeni 22281e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22291e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22301e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22311e1d9d6fSPaolo Abeni */ 22321e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22331e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22341e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22351e1d9d6fSPaolo Abeni if (!rtx_head) { 22361e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22371e1d9d6fSPaolo Abeni return false; 22381e1d9d6fSPaolo Abeni } 22391e1d9d6fSPaolo Abeni 22401e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22411e1d9d6fSPaolo Abeni msk->recovery = true; 22421e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22431e1d9d6fSPaolo Abeni 22441e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22451e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22461e1d9d6fSPaolo Abeni 22471e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22481e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22491e1d9d6fSPaolo Abeni if (!cur->already_sent) 22501e1d9d6fSPaolo Abeni break; 22511e1d9d6fSPaolo Abeni cur->already_sent = 0; 22521e1d9d6fSPaolo Abeni } 22531e1d9d6fSPaolo Abeni 22541e1d9d6fSPaolo Abeni return true; 22551e1d9d6fSPaolo Abeni } 22561e1d9d6fSPaolo Abeni 2257b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2258b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2259b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2260b29fcfb5SPaolo Abeni 2261cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2262cec37a6eSPeter Krystad * (accept). 2263cec37a6eSPeter Krystad * 2264cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2265cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2266cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2267cec37a6eSPeter Krystad * parent socket. 2268cec37a6eSPeter Krystad */ 2269a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2270b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2271b29fcfb5SPaolo Abeni unsigned int flags) 2272cec37a6eSPeter Krystad { 2273e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2274b29fcfb5SPaolo Abeni bool need_push, dispose_it; 2275e0be4931SFlorian Westphal 2276b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2277b29fcfb5SPaolo Abeni if (dispose_it) 2278cec37a6eSPeter Krystad list_del(&subflow->node); 2279cec37a6eSPeter Krystad 22803f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2281e16163b6SPaolo Abeni 2282b29fcfb5SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) 2283b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2284b29fcfb5SPaolo Abeni 2285b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2286b29fcfb5SPaolo Abeni if (!dispose_it) { 2287b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2288b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2289b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2290b29fcfb5SPaolo Abeni release_sock(ssk); 2291b29fcfb5SPaolo Abeni 2292b29fcfb5SPaolo Abeni goto out; 2293b29fcfb5SPaolo Abeni } 2294b29fcfb5SPaolo Abeni 2295e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2296e16163b6SPaolo Abeni * already orphaned 2297e16163b6SPaolo Abeni */ 2298866f26f2SPaolo Abeni if (ssk->sk_socket) 2299e16163b6SPaolo Abeni sock_orphan(ssk); 2300e16163b6SPaolo Abeni 2301d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2302d7b1bfd0SPaolo Abeni 2303e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2304e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2305e16163b6SPaolo Abeni * reference owned by msk; 2306e16163b6SPaolo Abeni */ 2307e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2308e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2309e16163b6SPaolo Abeni } else { 2310d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 23116aeed904SPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 23126aeed904SPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 23136aeed904SPaolo Abeni mptcp_subflow_queue_clean(ssk); 23146aeed904SPaolo Abeni inet_csk_listen_stop(ssk); 23156aeed904SPaolo Abeni } 2316e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2317e16163b6SPaolo Abeni 2318e16163b6SPaolo Abeni /* close acquired an extra ref */ 2319e16163b6SPaolo Abeni __sock_put(ssk); 2320e16163b6SPaolo Abeni } 2321e16163b6SPaolo Abeni release_sock(ssk); 2322e16163b6SPaolo Abeni 2323e16163b6SPaolo Abeni sock_put(ssk); 2324e0be4931SFlorian Westphal 2325c8fe62f0SFlorian Westphal if (ssk == msk->first) 2326c8fe62f0SFlorian Westphal msk->first = NULL; 2327c8fe62f0SFlorian Westphal 2328b29fcfb5SPaolo Abeni out: 2329b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2330b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23311e1d9d6fSPaolo Abeni 23321e1d9d6fSPaolo Abeni if (need_push) 23331e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2334f870fa0bSMat Martineau } 2335f870fa0bSMat Martineau 2336a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2337a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2338a141e02eSFlorian Westphal { 2339b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2340b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2341a88c9e49SPaolo Abeni 2342a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2343a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2344a88c9e49SPaolo Abeni */ 2345a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2346a88c9e49SPaolo Abeni 2347b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2348a141e02eSFlorian Westphal } 2349a141e02eSFlorian Westphal 2350dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2351dc24f8b4SPaolo Abeni { 2352dc24f8b4SPaolo Abeni return 0; 2353dc24f8b4SPaolo Abeni } 2354dc24f8b4SPaolo Abeni 23550e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 23560e4f35d7SPaolo Abeni { 23570e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 23580e4f35d7SPaolo Abeni 23593abc05d9SFlorian Westphal might_sleep(); 23603abc05d9SFlorian Westphal 23615efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 23620e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 23630e4f35d7SPaolo Abeni 23640e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 23650e4f35d7SPaolo Abeni continue; 23660e4f35d7SPaolo Abeni 236740947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 236840947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 236940947e13SFlorian Westphal continue; 237040947e13SFlorian Westphal 2371a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 23720e4f35d7SPaolo Abeni } 23730e4f35d7SPaolo Abeni } 23740e4f35d7SPaolo Abeni 2375e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2376e16163b6SPaolo Abeni { 2377e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2378e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2379e16163b6SPaolo Abeni 2380e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2381e16163b6SPaolo Abeni return true; 2382e16163b6SPaolo Abeni 2383e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2384e16163b6SPaolo Abeni * timeout 2385e16163b6SPaolo Abeni */ 2386e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2387e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2388e16163b6SPaolo Abeni TCP_CLOSE) 2389e16163b6SPaolo Abeni return false; 2390e16163b6SPaolo Abeni } 2391e16163b6SPaolo Abeni return true; 2392e16163b6SPaolo Abeni } 2393e16163b6SPaolo Abeni 239450c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 239550c504a2SFlorian Westphal { 239650c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 239750c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 239850c504a2SFlorian Westphal 239950c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 240050c504a2SFlorian Westphal return; 240150c504a2SFlorian Westphal 240250c504a2SFlorian Westphal mptcp_token_destroy(msk); 240350c504a2SFlorian Westphal 24045efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) { 240550c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 240675e908c3SPaolo Abeni bool slow; 240750c504a2SFlorian Westphal 240875e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 240950c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 241050c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 241150c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 241250c504a2SFlorian Westphal } 241375e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 241450c504a2SFlorian Westphal } 241550c504a2SFlorian Westphal 241650c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 241750c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 241850c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 241950c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 242050c504a2SFlorian Westphal 242150c504a2SFlorian Westphal mptcp_close_wake_up(sk); 242250c504a2SFlorian Westphal } 242350c504a2SFlorian Westphal 24242948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 242580992017SPaolo Abeni { 24262948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2427caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 24283b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 24293b1d6210SPaolo Abeni size_t copied = 0; 24302948d0a1SPaolo Abeni struct sock *ssk; 24312948d0a1SPaolo Abeni int ret; 24322948d0a1SPaolo Abeni 2433b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 24343241a9c0SFlorian Westphal 24353241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 24363241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 24372948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 24386477dd39SMat Martineau if (!dfrag) { 24396477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 24406477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 24416477dd39SMat Martineau 24426477dd39SMat Martineau icsk->icsk_retransmits++; 24436477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 24446477dd39SMat Martineau mptcp_send_ack(msk); 24456477dd39SMat Martineau 24466477dd39SMat Martineau goto reset_timer; 24476477dd39SMat Martineau } 24486477dd39SMat Martineau 24493241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 24502948d0a1SPaolo Abeni return; 24513241a9c0SFlorian Westphal 24523241a9c0SFlorian Westphal goto reset_timer; 24536477dd39SMat Martineau } 24542948d0a1SPaolo Abeni 24552948d0a1SPaolo Abeni if (!ssk) 24562948d0a1SPaolo Abeni goto reset_timer; 24572948d0a1SPaolo Abeni 24582948d0a1SPaolo Abeni lock_sock(ssk); 24592948d0a1SPaolo Abeni 24602948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 24612948d0a1SPaolo Abeni info.sent = 0; 24624e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 24634e14867dSPaolo Abeni while (info.sent < info.limit) { 24642948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 24652948d0a1SPaolo Abeni if (ret <= 0) 24662948d0a1SPaolo Abeni break; 24672948d0a1SPaolo Abeni 24682948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 24692948d0a1SPaolo Abeni copied += ret; 24702948d0a1SPaolo Abeni info.sent += ret; 24712948d0a1SPaolo Abeni } 24724e14867dSPaolo Abeni if (copied) { 24734e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 24742948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 24752948d0a1SPaolo Abeni info.size_goal); 24760530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 24774e14867dSPaolo Abeni } 24782948d0a1SPaolo Abeni 24792948d0a1SPaolo Abeni release_sock(ssk); 24802948d0a1SPaolo Abeni 24812948d0a1SPaolo Abeni reset_timer: 24823241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 24833241a9c0SFlorian Westphal 24842948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 24852948d0a1SPaolo Abeni mptcp_reset_timer(sk); 24862948d0a1SPaolo Abeni } 24872948d0a1SPaolo Abeni 248876a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 248976a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 249076a13b31SGeliang Tang */ 249176a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 249276a13b31SGeliang Tang { 249376a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 249476a13b31SGeliang Tang unsigned long timeout, close_timeout; 249576a13b31SGeliang Tang 249676a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 249776a13b31SGeliang Tang return; 249876a13b31SGeliang Tang 249976a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 250076a13b31SGeliang Tang 250176a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 250276a13b31SGeliang Tang * them is active 250376a13b31SGeliang Tang */ 250476a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 250576a13b31SGeliang Tang 250676a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 250776a13b31SGeliang Tang } 250876a13b31SGeliang Tang 250949fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 251049fa1919SGeliang Tang { 251176a13b31SGeliang Tang struct sock *ssk = msk->first; 251249fa1919SGeliang Tang bool slow; 251349fa1919SGeliang Tang 251476a13b31SGeliang Tang if (!ssk) 251576a13b31SGeliang Tang return; 251676a13b31SGeliang Tang 251749fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 251849fa1919SGeliang Tang 251949fa1919SGeliang Tang slow = lock_sock_fast(ssk); 252049fa1919SGeliang Tang mptcp_subflow_reset(ssk); 252176a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 252249fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 252376a13b31SGeliang Tang 252476a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 252549fa1919SGeliang Tang } 252649fa1919SGeliang Tang 25272948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 25282948d0a1SPaolo Abeni { 25292948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 25302948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 253176a13b31SGeliang Tang unsigned long fail_tout; 25322948d0a1SPaolo Abeni int state; 253380992017SPaolo Abeni 253480992017SPaolo Abeni lock_sock(sk); 2535e16163b6SPaolo Abeni state = sk->sk_state; 2536e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2537e16163b6SPaolo Abeni goto unlock; 2538e16163b6SPaolo Abeni 253943b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 254050c504a2SFlorian Westphal 254150c504a2SFlorian Westphal mptcp_check_fastclose(msk); 254250c504a2SFlorian Westphal 2543e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2544b416268bSFlorian Westphal 254559832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 254659832e24SFlorian Westphal mptcp_check_for_eof(msk); 254759832e24SFlorian Westphal 25486e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 254943b54c6eSMat Martineau mptcp_check_data_fin(sk); 255043b54c6eSMat Martineau 2551341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2552341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2553341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2554e16163b6SPaolo Abeni */ 2555e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2556341c6524SPaolo Abeni (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2557e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2558e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2559e16163b6SPaolo Abeni goto unlock; 2560e16163b6SPaolo Abeni } 2561e16163b6SPaolo Abeni 2562b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2563b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2564b263b0d7SFlorian Westphal 25652948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 25662948d0a1SPaolo Abeni __mptcp_retrans(sk); 25673b1d6210SPaolo Abeni 256876a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 256976a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 257049fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 257149fa1919SGeliang Tang 25723b1d6210SPaolo Abeni unlock: 257380992017SPaolo Abeni release_sock(sk); 257480992017SPaolo Abeni sock_put(sk); 257580992017SPaolo Abeni } 257680992017SPaolo Abeni 2577784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2578f870fa0bSMat Martineau { 2579cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2580cec37a6eSPeter Krystad 2581cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2582ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 258318b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 258480992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 258587952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2586ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2587f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 25886511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2589ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 259033d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2591cec37a6eSPeter Krystad 25928ab183deSPaolo Abeni msk->first = NULL; 2593dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2594752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 25950530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 25961e1d9d6fSPaolo Abeni msk->recovery = false; 25978ab183deSPaolo Abeni 25981b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 25991b1c7a0eSPeter Krystad 2600b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2601b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2602e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2603aa1fbd94SFlorian Westphal 2604f870fa0bSMat Martineau return 0; 2605f870fa0bSMat Martineau } 2606f870fa0bSMat Martineau 2607b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2608784325e9SMatthieu Baerts { 260920b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2610b29fcfb5SPaolo Abeni 2611b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2612b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2613b29fcfb5SPaolo Abeni 2614b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2615b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2616b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2617b29fcfb5SPaolo Abeni } 2618b29fcfb5SPaolo Abeni 2619b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2620b29fcfb5SPaolo Abeni { 2621fc518953SFlorian Westphal struct net *net = sock_net(sk); 2622fc518953SFlorian Westphal int ret; 262318b683bfSPaolo Abeni 2624b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2625b6c08380SGeliang Tang if (ret) 2626b6c08380SGeliang Tang return ret; 2627b6c08380SGeliang Tang 2628fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2629fc518953SFlorian Westphal return -ENOPROTOOPT; 2630fc518953SFlorian Westphal 2631fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2632fc518953SFlorian Westphal return -ENOMEM; 2633fc518953SFlorian Westphal 2634fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2635fa68018dSPaolo Abeni if (ret) 2636fa68018dSPaolo Abeni return ret; 2637fa68018dSPaolo Abeni 263820b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 263920b5759fSPaolo Abeni * propagate the correct value 264020b5759fSPaolo Abeni */ 2641b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 264220b5759fSPaolo Abeni 2643d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 264402739545SKuniyuki Iwashima sk->sk_rcvbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[1]); 264502739545SKuniyuki Iwashima sk->sk_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[1]); 2646d027236cSPaolo Abeni 264718b683bfSPaolo Abeni return 0; 264818b683bfSPaolo Abeni } 264918b683bfSPaolo Abeni 265018b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 265118b683bfSPaolo Abeni { 265218b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 265318b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 265418b683bfSPaolo Abeni 2655d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 265618b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2657d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2658784325e9SMatthieu Baerts } 2659784325e9SMatthieu Baerts 266080992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 266180992017SPaolo Abeni { 266280992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 266380992017SPaolo Abeni 2664b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2665e16163b6SPaolo Abeni __sock_put(sk); 266680992017SPaolo Abeni } 266780992017SPaolo Abeni 2668d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 266921498490SPeter Krystad { 267021498490SPeter Krystad lock_sock(ssk); 267121498490SPeter Krystad 267221498490SPeter Krystad switch (ssk->sk_state) { 267321498490SPeter Krystad case TCP_LISTEN: 267421498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 267521498490SPeter Krystad break; 2676df561f66SGustavo A. R. Silva fallthrough; 267721498490SPeter Krystad case TCP_SYN_SENT: 267821498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 267921498490SPeter Krystad break; 268021498490SPeter Krystad default: 268143b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 268243b54c6eSMat Martineau pr_debug("Fallback"); 268321498490SPeter Krystad ssk->sk_shutdown |= how; 268421498490SPeter Krystad tcp_shutdown(ssk, how); 268543b54c6eSMat Martineau } else { 268643b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 268743b54c6eSMat Martineau tcp_send_ack(ssk); 26886477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 26896477dd39SMat Martineau mptcp_reset_timer(sk); 269043b54c6eSMat Martineau } 269121498490SPeter Krystad break; 269221498490SPeter Krystad } 269321498490SPeter Krystad 269421498490SPeter Krystad release_sock(ssk); 269521498490SPeter Krystad } 269621498490SPeter Krystad 26976920b851SMat Martineau static const unsigned char new_state[16] = { 26986920b851SMat Martineau /* current state: new state: action: */ 26996920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 27006920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27016920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 27026920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 27036920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 27046920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 27056920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 27066920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 27076920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 27086920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 27096920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 27106920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 27116920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 27126920b851SMat Martineau }; 27136920b851SMat Martineau 27146920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 27156920b851SMat Martineau { 27166920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 27176920b851SMat Martineau int ns = next & TCP_STATE_MASK; 27186920b851SMat Martineau 27196920b851SMat Martineau inet_sk_state_store(sk, ns); 27206920b851SMat Martineau 27216920b851SMat Martineau return next & TCP_ACTION_FIN; 27226920b851SMat Martineau } 27236920b851SMat Martineau 2724e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2725e16163b6SPaolo Abeni { 2726e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2727e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2728e16163b6SPaolo Abeni 2729e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2730e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2731e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2732e16163b6SPaolo Abeni 2733e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2734e16163b6SPaolo Abeni * skip this 2735e16163b6SPaolo Abeni */ 2736e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2737e16163b6SPaolo Abeni mptcp_send_head(sk)) 2738e16163b6SPaolo Abeni return; 2739e16163b6SPaolo Abeni 2740e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2741e16163b6SPaolo Abeni 274226aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 274326aa2314SPaolo Abeni * state now 274426aa2314SPaolo Abeni */ 274526aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 274658cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 274726aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2748e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2749e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 275026aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 275126aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 275226aa2314SPaolo Abeni } 2753e16163b6SPaolo Abeni } 2754e16163b6SPaolo Abeni 2755e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2756e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2757e16163b6SPaolo Abeni 2758e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2759e16163b6SPaolo Abeni } 2760e16163b6SPaolo Abeni } 2761e16163b6SPaolo Abeni 2762e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2763e16163b6SPaolo Abeni { 2764e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2765e16163b6SPaolo Abeni 2766e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2767e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2768e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2769e16163b6SPaolo Abeni 2770e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2771e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2772e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2773e16163b6SPaolo Abeni 2774e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2775e16163b6SPaolo Abeni } 2776e16163b6SPaolo Abeni 2777e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2778f870fa0bSMat Martineau { 2779f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2780f870fa0bSMat Martineau 2781e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 27822c22c06cSFlorian Westphal 27833abc05d9SFlorian Westphal might_sleep(); 27843abc05d9SFlorian Westphal 2785bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2786e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2787e16163b6SPaolo Abeni msk->pm.status = 0; 2788b2c5b614SFlorian Westphal 2789e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2790e16163b6SPaolo Abeni 27916511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 279287952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2793e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2794e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2795aa1fbd94SFlorian Westphal 2796e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2797e16163b6SPaolo Abeni sock_put(sk); 2798e16163b6SPaolo Abeni } 2799e16163b6SPaolo Abeni 2800e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2801e16163b6SPaolo Abeni { 2802e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 280376a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2804e16163b6SPaolo Abeni bool do_cancel_work = false; 2805e16163b6SPaolo Abeni 2806e16163b6SPaolo Abeni lock_sock(sk); 2807e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2808e16163b6SPaolo Abeni 2809e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2810e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2811e16163b6SPaolo Abeni goto cleanup; 2812e16163b6SPaolo Abeni } 2813e16163b6SPaolo Abeni 2814e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2815e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2816e16163b6SPaolo Abeni 2817e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2818e16163b6SPaolo Abeni 2819e16163b6SPaolo Abeni cleanup: 2820e16163b6SPaolo Abeni /* orphan all the subflows */ 2821e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 282276a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2823e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 282449054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2825e16163b6SPaolo Abeni 282676a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 282776a13b31SGeliang Tang * cancel the latter 282876a13b31SGeliang Tang */ 282976a13b31SGeliang Tang if (ssk == msk->first) 283076a13b31SGeliang Tang subflow->fail_tout = 0; 283176a13b31SGeliang Tang 2832e16163b6SPaolo Abeni sock_orphan(ssk); 2833e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2834e16163b6SPaolo Abeni } 2835e16163b6SPaolo Abeni sock_orphan(sk); 2836e16163b6SPaolo Abeni 2837e16163b6SPaolo Abeni sock_hold(sk); 2838e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2839b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 284076a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2841b29fcfb5SPaolo Abeni 2842e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2843e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2844e16163b6SPaolo Abeni do_cancel_work = true; 2845e16163b6SPaolo Abeni } else { 284676a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2847e16163b6SPaolo Abeni } 2848e16163b6SPaolo Abeni release_sock(sk); 2849e16163b6SPaolo Abeni if (do_cancel_work) 285080992017SPaolo Abeni mptcp_cancel_work(sk); 2851b911c97cSFlorian Westphal 2852e16163b6SPaolo Abeni sock_put(sk); 2853f870fa0bSMat Martineau } 2854f870fa0bSMat Martineau 2855cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2856cf7da0d6SPeter Krystad { 2857cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2858cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2859cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2860cf7da0d6SPeter Krystad 2861cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2862cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2863cf7da0d6SPeter Krystad 2864cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2865cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2866cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2867cf7da0d6SPeter Krystad } 2868cf7da0d6SPeter Krystad #endif 2869cf7da0d6SPeter Krystad 2870cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2871cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2872cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2873cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2874cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2875cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2876cf7da0d6SPeter Krystad } 2877cf7da0d6SPeter Krystad 287818b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 287918b683bfSPaolo Abeni { 288076e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 288176e2a55dSPaolo Abeni 2882b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2883b29fcfb5SPaolo Abeni 2884bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2885b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2886b29fcfb5SPaolo Abeni 2887b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 2888b29fcfb5SPaolo Abeni mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2889b29fcfb5SPaolo Abeni 2890c0bf3c6aSPaolo Abeni /* msk->subflow is still intact, the following will not free the first 2891c0bf3c6aSPaolo Abeni * subflow 2892c0bf3c6aSPaolo Abeni */ 2893c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 2894b29fcfb5SPaolo Abeni msk->last_snd = NULL; 2895e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 2896e9d09bacSPaolo Abeni msk->cb_flags = 0; 2897e9d09bacSPaolo Abeni msk->push_pending = 0; 2898b29fcfb5SPaolo Abeni msk->recovery = false; 2899b29fcfb5SPaolo Abeni msk->can_ack = false; 2900b29fcfb5SPaolo Abeni msk->fully_established = false; 2901b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 2902b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 2903b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 2904b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 2905b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2906b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 2907b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 2908b29fcfb5SPaolo Abeni 2909b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 2910b29fcfb5SPaolo Abeni sk_error_report(sk); 291142c556feSFlorian Westphal return 0; 291218b683bfSPaolo Abeni } 291318b683bfSPaolo Abeni 2914b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2915b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2916b0519de8SFlorian Westphal { 2917b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2918b0519de8SFlorian Westphal 2919b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2920b0519de8SFlorian Westphal } 2921b0519de8SFlorian Westphal #endif 2922b0519de8SFlorian Westphal 2923fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2924cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2925fca5c82cSPaolo Abeni struct request_sock *req) 2926b0519de8SFlorian Westphal { 292758b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2928b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 292958b09919SPaolo Abeni struct mptcp_sock *msk; 293058b09919SPaolo Abeni u64 ack_seq; 2931b0519de8SFlorian Westphal 2932b0519de8SFlorian Westphal if (!nsk) 2933b0519de8SFlorian Westphal return NULL; 2934b0519de8SFlorian Westphal 2935b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2936b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2937b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2938b0519de8SFlorian Westphal #endif 2939b0519de8SFlorian Westphal 294058b09919SPaolo Abeni __mptcp_init_sock(nsk); 294158b09919SPaolo Abeni 294258b09919SPaolo Abeni msk = mptcp_sk(nsk); 294358b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 294458b09919SPaolo Abeni msk->token = subflow_req->token; 294558b09919SPaolo Abeni msk->subflow = NULL; 2946b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 294774c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 294806251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 294958b09919SPaolo Abeni 295058b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2951eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 29527439d687SPaolo Abeni msk->snd_una = msk->write_seq; 29537439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2954df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 29556f8a612aSFlorian Westphal 295674c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 295758b09919SPaolo Abeni msk->can_ack = true; 2958cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 295958b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 296058b09919SPaolo Abeni ack_seq++; 2961917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2962f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 296358b09919SPaolo Abeni } 29647f20d5fcSPaolo Abeni 29655e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 29667f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 29677f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 29680c148460SPaolo Abeni 29690c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 297058b09919SPaolo Abeni bh_unlock_sock(nsk); 297158b09919SPaolo Abeni 297258b09919SPaolo Abeni /* keep a single reference */ 297358b09919SPaolo Abeni __sock_put(nsk); 2974b0519de8SFlorian Westphal return nsk; 2975b0519de8SFlorian Westphal } 2976b0519de8SFlorian Westphal 2977a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2978a6b118feSFlorian Westphal { 2979a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2980a6b118feSFlorian Westphal 2981a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2982a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2983a6b118feSFlorian Westphal 2984a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2985a6b118feSFlorian Westphal 2986a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2987a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2988a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2989a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2990a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 29916f8a612aSFlorian Westphal 29927439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2993a6b118feSFlorian Westphal } 2994a6b118feSFlorian Westphal 2995cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2996cf7da0d6SPeter Krystad bool kern) 2997cf7da0d6SPeter Krystad { 2998cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2999cf7da0d6SPeter Krystad struct socket *listener; 3000cf7da0d6SPeter Krystad struct sock *newsk; 3001cf7da0d6SPeter Krystad 3002cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 3003cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3004cf7da0d6SPeter Krystad *err = -EINVAL; 3005cf7da0d6SPeter Krystad return NULL; 3006cf7da0d6SPeter Krystad } 3007cf7da0d6SPeter Krystad 3008cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3009cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3010cf7da0d6SPeter Krystad if (!newsk) 3011cf7da0d6SPeter Krystad return NULL; 3012cf7da0d6SPeter Krystad 3013cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3014cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3015cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3016cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3017cf7da0d6SPeter Krystad 3018cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 301958b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 302058b09919SPaolo Abeni 302158b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 302258b09919SPaolo Abeni * subflow_syn_recv_sock() 302358b09919SPaolo Abeni */ 302458b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 302558b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3026d6692b3bSFlorian Westphal goto out; 302758b09919SPaolo Abeni } 302858b09919SPaolo Abeni 302958b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 303058b09919SPaolo Abeni sock_hold(new_mptcp_sock); 3031cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 30320397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3033fc518953SFlorian Westphal } else { 3034fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3035fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3036cf7da0d6SPeter Krystad } 3037cf7da0d6SPeter Krystad 3038d6692b3bSFlorian Westphal out: 3039d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3040cf7da0d6SPeter Krystad return newsk; 3041cf7da0d6SPeter Krystad } 3042cf7da0d6SPeter Krystad 3043c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 30445c8c1640SGeliang Tang { 3045c0bf3c6aSPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 304687952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 304787952603SPaolo Abeni 30486e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 30496e628cd3SPaolo Abeni 3050c0bf3c6aSPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time */ 30515efbf6f7SMatthieu Baerts mptcp_for_each_subflow_safe(msk, subflow, tmp) 3052c0bf3c6aSPaolo Abeni __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3053c0bf3c6aSPaolo Abeni 305487952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3055b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 305687952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 30576511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 30585c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3059b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 30606511882cSPaolo Abeni 30616511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 30626511882cSPaolo Abeni * inet_sock_destruct() will dispose it 30636511882cSPaolo Abeni */ 30646511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 30656511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 30665c8c1640SGeliang Tang mptcp_token_destroy(msk); 30675c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 30684638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 30695c8c1640SGeliang Tang } 30705c8c1640SGeliang Tang 307179c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 307279c0949eSPeter Krystad { 3073c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3074c9fd9c5fSFlorian Westphal 3075c0bf3c6aSPaolo Abeni /* clears msk->subflow, allowing the following to close 3076c0bf3c6aSPaolo Abeni * even the initial subflow 3077c0bf3c6aSPaolo Abeni */ 3078c0bf3c6aSPaolo Abeni mptcp_dispose_initial_subflow(msk); 3079c0bf3c6aSPaolo Abeni mptcp_destroy_common(msk, 0); 3080d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 308179c0949eSPeter Krystad } 308279c0949eSPeter Krystad 30836e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 30846e628cd3SPaolo Abeni { 30856e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 30866e628cd3SPaolo Abeni __mptcp_clean_una(sk); 30876e628cd3SPaolo Abeni else 3088e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 30896e628cd3SPaolo Abeni 30906e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 30916e628cd3SPaolo Abeni mptcp_schedule_work(sk); 30926e628cd3SPaolo Abeni } 30936e628cd3SPaolo Abeni 3094219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 30956e628cd3SPaolo Abeni { 30966e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 30976e628cd3SPaolo Abeni return; 30986e628cd3SPaolo Abeni 309940dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 3100b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 3101b19bc294SPaolo Abeni 3102b19bc294SPaolo Abeni if (xmit_ssk == ssk) 31036e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3104b19bc294SPaolo Abeni else if (xmit_ssk) 3105bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND); 310640dc9416SPaolo Abeni } else { 3107e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 31086e628cd3SPaolo Abeni } 310940dc9416SPaolo Abeni } 31106e628cd3SPaolo Abeni 3111e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3112e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3113e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3114e9d09bacSPaolo Abeni 3115e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 311614c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3117e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 311814c441b5SPaolo Abeni { 3119e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 31202d6f5a2bSPaolo Abeni 3121e9d09bacSPaolo Abeni for (;;) { 3122e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3123e9d09bacSPaolo Abeni msk->push_pending; 3124c2e6048fSPaolo Abeni if (!flags) 3125c2e6048fSPaolo Abeni break; 3126c2e6048fSPaolo Abeni 3127c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 31286e628cd3SPaolo Abeni * 31296e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 31306e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 31316e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 31326e628cd3SPaolo Abeni * the subflow socket lock 31336e628cd3SPaolo Abeni */ 3134e9d09bacSPaolo Abeni msk->push_pending = 0; 3135e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 31366e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 31373e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 31383e501490SPaolo Abeni __mptcp_flush_join_list(sk); 31392e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3140c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 31412d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 31422d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3143c2e6048fSPaolo Abeni 3144c2e6048fSPaolo Abeni cond_resched(); 31456e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 31466e628cd3SPaolo Abeni } 3147c2e6048fSPaolo Abeni 314865a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 314965a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 315065a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3151490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 315265a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3153490274b4SPaolo Abeni */ 3154e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3155490274b4SPaolo Abeni __mptcp_set_connected(sk); 3156e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 315715cc1045SPaolo Abeni __mptcp_error_report(sk); 315843f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 315943f5b111SPaolo Abeni msk->last_snd = NULL; 316065a569b0SPaolo Abeni } 31616e628cd3SPaolo Abeni 316287952603SPaolo Abeni __mptcp_update_rmem(sk); 316314c441b5SPaolo Abeni } 316414c441b5SPaolo Abeni 3165bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3166bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3167bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3168bcd97734SPaolo Abeni */ 3169bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3170bcd97734SPaolo Abeni { 3171bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3172bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3173bcd97734SPaolo Abeni unsigned long timeout; 3174bcd97734SPaolo Abeni 3175bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3176bcd97734SPaolo Abeni return; 3177bcd97734SPaolo Abeni 3178bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3179bcd97734SPaolo Abeni if (tp->srtt_us) 3180bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3181bcd97734SPaolo Abeni else 3182bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3183bcd97734SPaolo Abeni timeout += jiffies; 3184bcd97734SPaolo Abeni 3185bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3186bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3187bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3188bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3189bcd97734SPaolo Abeni } 3190bcd97734SPaolo Abeni 3191b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3192b19bc294SPaolo Abeni { 3193b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3194b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3195b19bc294SPaolo Abeni 3196bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3197b19bc294SPaolo Abeni mptcp_data_lock(sk); 3198b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3199b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3200b19bc294SPaolo Abeni else 3201e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3202b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3203bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3204bcd97734SPaolo Abeni } 3205bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3206bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3207bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3208bcd97734SPaolo Abeni } 3209b19bc294SPaolo Abeni } 3210b19bc294SPaolo Abeni 32112c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 32122c5ebd00SPaolo Abeni { 32132c5ebd00SPaolo Abeni /* should never be called, 32142c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 32152c5ebd00SPaolo Abeni */ 32162c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 32172c5ebd00SPaolo Abeni return 0; 32182c5ebd00SPaolo Abeni } 32192c5ebd00SPaolo Abeni 32202c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 32212c5ebd00SPaolo Abeni { 32222c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 32232c5ebd00SPaolo Abeni } 32242c5ebd00SPaolo Abeni 3225cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3226f870fa0bSMat Martineau { 3227f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3228cec37a6eSPeter Krystad struct socket *ssock; 3229f870fa0bSMat Martineau 3230cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3231cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3232cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3233cec37a6eSPeter Krystad return -EINVAL; 3234f870fa0bSMat Martineau 3235cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3236cec37a6eSPeter Krystad } 3237f870fa0bSMat Martineau 3238cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3239cec37a6eSPeter Krystad { 3240cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3241cec37a6eSPeter Krystad struct mptcp_sock *msk; 3242cec37a6eSPeter Krystad struct sock *sk; 32436d0060f6SMat Martineau u64 ack_seq; 3244f870fa0bSMat Martineau 3245cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3246cec37a6eSPeter Krystad sk = subflow->conn; 3247cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3248cec37a6eSPeter Krystad 3249648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3250648ef4b8SMat Martineau 32516d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 32526d0060f6SMat Martineau ack_seq++; 3253648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3254648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 32556d0060f6SMat Martineau 3256cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3257cec37a6eSPeter Krystad * accessing the field below 3258cec37a6eSPeter Krystad */ 3259cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3260cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 32616d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3262eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 32636d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3264d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 32657439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 3266f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 32671b1c7a0eSPeter Krystad 32686c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3269a6b118feSFlorian Westphal 3270a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3271f870fa0bSMat Martineau } 3272f870fa0bSMat Martineau 3273866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3274cf7da0d6SPeter Krystad { 3275cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3276cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3277cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3278cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3279cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3280cf7da0d6SPeter Krystad } 3281cf7da0d6SPeter Krystad 3282e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3283f296234cSPeter Krystad { 3284e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3285f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3286f296234cSPeter Krystad struct sock *parent = (void *)msk; 32873e501490SPaolo Abeni bool ret = true; 3288f296234cSPeter Krystad 3289f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3290f296234cSPeter Krystad 3291f296234cSPeter Krystad /* mptcp socket already closing? */ 3292dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3293dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3294f296234cSPeter Krystad return false; 3295dc87efdbSFlorian Westphal } 3296f296234cSPeter Krystad 329770c708e8SKishen Maloor if (!list_empty(&subflow->node)) 3298b911c97cSFlorian Westphal goto out; 3299f296234cSPeter Krystad 33003e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 33013e501490SPaolo Abeni goto err_prohibited; 330210f6d46cSPaolo Abeni 33033e501490SPaolo Abeni /* active connections are already on conn_list. 33043e501490SPaolo Abeni * If we can't acquire msk socket lock here, let the release callback 33053e501490SPaolo Abeni * handle it 330610f6d46cSPaolo Abeni */ 33073e501490SPaolo Abeni mptcp_data_lock(parent); 33083e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 33093e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 33103e501490SPaolo Abeni if (ret) { 3311e16163b6SPaolo Abeni sock_hold(ssk); 33123e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3313e16163b6SPaolo Abeni } 33143e501490SPaolo Abeni } else { 33153e501490SPaolo Abeni sock_hold(ssk); 33163e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3317e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 33183e501490SPaolo Abeni } 33193e501490SPaolo Abeni mptcp_data_unlock(parent); 33203e501490SPaolo Abeni 3321dc87efdbSFlorian Westphal if (!ret) { 33223e501490SPaolo Abeni err_prohibited: 3323dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 332410f6d46cSPaolo Abeni return false; 3325dc87efdbSFlorian Westphal } 332610f6d46cSPaolo Abeni 3327917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 33280530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 33293e501490SPaolo Abeni 3330b911c97cSFlorian Westphal out: 3331b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 333210f6d46cSPaolo Abeni return true; 3333f296234cSPeter Krystad } 3334f296234cSPeter Krystad 333576e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 333676e2a55dSPaolo Abeni { 333776e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 333876e2a55dSPaolo Abeni 333976e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 334076e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 334176e2a55dSPaolo Abeni } 334276e2a55dSPaolo Abeni 33436511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 33446511882cSPaolo Abeni { 33456511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 33466511882cSPaolo Abeni } 33476511882cSPaolo Abeni 3348644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3349644807e3SFlorian Westphal { 3350644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3351644807e3SFlorian Westphal u64 delta; 3352644807e3SFlorian Westphal 3353644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3354644807e3SFlorian Westphal return -EINVAL; 3355644807e3SFlorian Westphal 3356644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3357644807e3SFlorian Westphal return 0; 3358644807e3SFlorian Westphal 3359644807e3SFlorian Westphal delta = msk->write_seq - v; 336007c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 336107c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 336207c2c7a3SPaolo Abeni 336307c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 336407c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 336507c2c7a3SPaolo Abeni * so ignore that status, too. 336607c2c7a3SPaolo Abeni */ 336707c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 336807c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 336907c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 337007c2c7a3SPaolo Abeni } 3371644807e3SFlorian Westphal if (delta > INT_MAX) 3372644807e3SFlorian Westphal delta = INT_MAX; 3373644807e3SFlorian Westphal 3374644807e3SFlorian Westphal return (int)delta; 3375644807e3SFlorian Westphal } 3376644807e3SFlorian Westphal 3377644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3378644807e3SFlorian Westphal { 3379644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3380644807e3SFlorian Westphal bool slow; 3381644807e3SFlorian Westphal int answ; 3382644807e3SFlorian Westphal 3383644807e3SFlorian Westphal switch (cmd) { 3384644807e3SFlorian Westphal case SIOCINQ: 3385644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3386644807e3SFlorian Westphal return -EINVAL; 3387644807e3SFlorian Westphal 3388644807e3SFlorian Westphal lock_sock(sk); 3389644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3390644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3391644807e3SFlorian Westphal release_sock(sk); 3392644807e3SFlorian Westphal break; 3393644807e3SFlorian Westphal case SIOCOUTQ: 3394644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3395644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3396644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3397644807e3SFlorian Westphal break; 3398644807e3SFlorian Westphal case SIOCOUTQNSD: 3399644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3400644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3401644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3402644807e3SFlorian Westphal break; 3403644807e3SFlorian Westphal default: 3404644807e3SFlorian Westphal return -ENOIOCTLCMD; 3405644807e3SFlorian Westphal } 3406644807e3SFlorian Westphal 3407644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3408644807e3SFlorian Westphal } 3409644807e3SFlorian Westphal 3410f870fa0bSMat Martineau static struct proto mptcp_prot = { 3411f870fa0bSMat Martineau .name = "MPTCP", 3412f870fa0bSMat Martineau .owner = THIS_MODULE, 3413f870fa0bSMat Martineau .init = mptcp_init_sock, 341418b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3415f870fa0bSMat Martineau .close = mptcp_close, 3416cf7da0d6SPeter Krystad .accept = mptcp_accept, 3417717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3418717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 341976e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 342079c0949eSPeter Krystad .destroy = mptcp_destroy, 3421f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3422644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3423f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 342414c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 34252c5ebd00SPaolo Abeni .hash = mptcp_hash, 34262c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3427cec37a6eSPeter Krystad .get_port = mptcp_get_port, 34286511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3429d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 34300defbb0aSEric Dumazet 3431d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 34320defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 34330defbb0aSEric Dumazet 3434d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3435d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3436989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3437d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3438f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 34392c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3440f870fa0bSMat Martineau .no_autobind = true, 3441f870fa0bSMat Martineau }; 3442f870fa0bSMat Martineau 34432303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 34442303f994SPeter Krystad { 34452303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 34462303f994SPeter Krystad struct socket *ssock; 3447cf7da0d6SPeter Krystad int err; 34482303f994SPeter Krystad 34492303f994SPeter Krystad lock_sock(sock->sk); 3450fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3451fa68018dSPaolo Abeni if (!ssock) { 3452fa68018dSPaolo Abeni err = -EINVAL; 34532303f994SPeter Krystad goto unlock; 34542303f994SPeter Krystad } 34552303f994SPeter Krystad 34562303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3457cf7da0d6SPeter Krystad if (!err) 3458cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 34592303f994SPeter Krystad 34602303f994SPeter Krystad unlock: 34612303f994SPeter Krystad release_sock(sock->sk); 34622303f994SPeter Krystad return err; 34632303f994SPeter Krystad } 34642303f994SPeter Krystad 34650235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 34660235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 34670235d075SPaolo Abeni { 34680235d075SPaolo Abeni subflow->request_mptcp = 0; 34690235d075SPaolo Abeni __mptcp_do_fallback(msk); 34700235d075SPaolo Abeni } 34710235d075SPaolo Abeni 34722303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 34732303f994SPeter Krystad int addr_len, int flags) 34742303f994SPeter Krystad { 34752303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 34762c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 34772303f994SPeter Krystad struct socket *ssock; 34783d1d6d66SPaolo Abeni int err = -EINVAL; 34792303f994SPeter Krystad 34802303f994SPeter Krystad lock_sock(sock->sk); 34813d1d6d66SPaolo Abeni if (uaddr) { 34823d1d6d66SPaolo Abeni if (addr_len < sizeof(uaddr->sa_family)) 34833d1d6d66SPaolo Abeni goto unlock; 34843d1d6d66SPaolo Abeni 34853d1d6d66SPaolo Abeni if (uaddr->sa_family == AF_UNSPEC) { 34863d1d6d66SPaolo Abeni err = mptcp_disconnect(sock->sk, flags); 34873d1d6d66SPaolo Abeni sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 34883d1d6d66SPaolo Abeni goto unlock; 34893d1d6d66SPaolo Abeni } 34903d1d6d66SPaolo Abeni } 34913d1d6d66SPaolo Abeni 349241be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 349341be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 349441be81a8SPaolo Abeni * cope with that 349541be81a8SPaolo Abeni */ 349641be81a8SPaolo Abeni ssock = msk->subflow; 349741be81a8SPaolo Abeni goto do_connect; 349841be81a8SPaolo Abeni } 349941be81a8SPaolo Abeni 3500fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 35013d1d6d66SPaolo Abeni if (!ssock) 35022303f994SPeter Krystad goto unlock; 35032303f994SPeter Krystad 3504fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3505fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 35062c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3507cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3508cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3509cf7da0d6SPeter Krystad * TCP option space. 3510cf7da0d6SPeter Krystad */ 3511cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 35120235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3513cf7da0d6SPeter Krystad #endif 3514a16195e3SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3515a16195e3SPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 35160235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3517a16195e3SPaolo Abeni } 35185695eb88SPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 35195695eb88SPaolo Abeni MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3520cf7da0d6SPeter Krystad 352141be81a8SPaolo Abeni do_connect: 35222303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 352341be81a8SPaolo Abeni sock->state = ssock->state; 352441be81a8SPaolo Abeni 352541be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 352641be81a8SPaolo Abeni * subflow_finish_connect() 352741be81a8SPaolo Abeni */ 3528367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3529cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 353041be81a8SPaolo Abeni else 353141be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 35322303f994SPeter Krystad 35332303f994SPeter Krystad unlock: 35342303f994SPeter Krystad release_sock(sock->sk); 35352303f994SPeter Krystad return err; 35362303f994SPeter Krystad } 35372303f994SPeter Krystad 3538cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3539cf7da0d6SPeter Krystad { 3540cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3541cf7da0d6SPeter Krystad struct socket *ssock; 3542cf7da0d6SPeter Krystad int err; 3543cf7da0d6SPeter Krystad 3544cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3545cf7da0d6SPeter Krystad 3546cf7da0d6SPeter Krystad lock_sock(sock->sk); 3547fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3548fa68018dSPaolo Abeni if (!ssock) { 3549fa68018dSPaolo Abeni err = -EINVAL; 3550cf7da0d6SPeter Krystad goto unlock; 3551cf7da0d6SPeter Krystad } 3552cf7da0d6SPeter Krystad 3553fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3554fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 35555e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 35565e20087dSFlorian Westphal 3557cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3558cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3559cf7da0d6SPeter Krystad if (!err) 3560cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3561cf7da0d6SPeter Krystad 3562cf7da0d6SPeter Krystad unlock: 3563cf7da0d6SPeter Krystad release_sock(sock->sk); 3564cf7da0d6SPeter Krystad return err; 3565cf7da0d6SPeter Krystad } 3566cf7da0d6SPeter Krystad 3567cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3568cf7da0d6SPeter Krystad int flags, bool kern) 3569cf7da0d6SPeter Krystad { 3570cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3571cf7da0d6SPeter Krystad struct socket *ssock; 3572cf7da0d6SPeter Krystad int err; 3573cf7da0d6SPeter Krystad 3574cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3575cf7da0d6SPeter Krystad 3576cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3577cf7da0d6SPeter Krystad if (!ssock) 357871ba088cSPaolo Abeni return -EINVAL; 3579cf7da0d6SPeter Krystad 3580cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3581d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3582cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3583cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 35840397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 35850397c6d8SPaolo Abeni 35864d54cc32SFlorian Westphal lock_sock(newsk); 35875b950ff4SPaolo Abeni 35885b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 35895b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 35905b950ff4SPaolo Abeni * we can notify it, if needed. 3591c8fe62f0SFlorian Westphal * 3592c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3593c8fe62f0SFlorian Westphal * the refcnt is still at least one. 35945b950ff4SPaolo Abeni */ 35955b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 35965b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 35975b950ff4SPaolo Abeni sock_hold(msk->first); 35985b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 35996c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 36005b950ff4SPaolo Abeni 36010397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 36020397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 36035cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3604cf7da0d6SPeter Krystad 3605cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3606cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3607cf7da0d6SPeter Krystad */ 3608190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3609cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3610cf7da0d6SPeter Krystad 3611cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3612cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3613cf7da0d6SPeter Krystad } 36144d54cc32SFlorian Westphal release_sock(newsk); 3615cf7da0d6SPeter Krystad } 3616cf7da0d6SPeter Krystad 3617cf7da0d6SPeter Krystad return err; 3618cf7da0d6SPeter Krystad } 3619cf7da0d6SPeter Krystad 36208a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 36218a05661bSPaolo Abeni { 3622612f71d7SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 3623612f71d7SPaolo Abeni * always show at least one non-empty queue when checked in this order. 3624612f71d7SPaolo Abeni */ 3625612f71d7SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 3626612f71d7SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 3627612f71d7SPaolo Abeni return 0; 3628612f71d7SPaolo Abeni 3629612f71d7SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 36308a05661bSPaolo Abeni } 36318a05661bSPaolo Abeni 36328edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 36338edf0864SFlorian Westphal { 36348edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 36358edf0864SFlorian Westphal 36368edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3637dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 36388edf0864SFlorian Westphal 36398edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 36408edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 36418edf0864SFlorian Westphal 36425cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 36436e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 36446e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 36456e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 36468edf0864SFlorian Westphal 36476e628cd3SPaolo Abeni return 0; 36488edf0864SFlorian Westphal } 36498edf0864SFlorian Westphal 36502303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 36512303f994SPeter Krystad struct poll_table_struct *wait) 36522303f994SPeter Krystad { 36531891c4a0SFlorian Westphal struct sock *sk = sock->sk; 36548ab183deSPaolo Abeni struct mptcp_sock *msk; 36552303f994SPeter Krystad __poll_t mask = 0; 36568a05661bSPaolo Abeni int state; 36572303f994SPeter Krystad 36581891c4a0SFlorian Westphal msk = mptcp_sk(sk); 36591891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 36601891c4a0SFlorian Westphal 36618a05661bSPaolo Abeni state = inet_sk_state_load(sk); 36626719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 366371ba088cSPaolo Abeni if (state == TCP_LISTEN) { 366471ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 366571ba088cSPaolo Abeni return 0; 366671ba088cSPaolo Abeni 366771ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 366871ba088cSPaolo Abeni } 36698a05661bSPaolo Abeni 36708a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 36718a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 36728edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 36738a05661bSPaolo Abeni } 3674dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3675dd913410SPaolo Abeni mask |= EPOLLHUP; 36761891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 36771891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 36781891c4a0SFlorian Westphal 367915cc1045SPaolo Abeni /* This barrier is coupled with smp_wmb() in tcp_reset() */ 368015cc1045SPaolo Abeni smp_rmb(); 368115cc1045SPaolo Abeni if (sk->sk_err) 368215cc1045SPaolo Abeni mask |= EPOLLERR; 368315cc1045SPaolo Abeni 36842303f994SPeter Krystad return mask; 36852303f994SPeter Krystad } 36862303f994SPeter Krystad 3687e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3688e42f1ac6SFlorian Westphal .family = PF_INET, 3689e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 36900a3cc579SPaolo Abeni .release = inet_release, 3691e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3692e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3693e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3694e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3695d2f77c53SPaolo Abeni .getname = inet_getname, 3696e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3697e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3698e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3699e42f1ac6SFlorian Westphal .listen = mptcp_listen, 370076e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3701e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3702e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3703e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3704e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3705e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3706e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3707e42f1ac6SFlorian Westphal }; 37082303f994SPeter Krystad 3709f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3710f870fa0bSMat Martineau .type = SOCK_STREAM, 3711f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3712f870fa0bSMat Martineau .prot = &mptcp_prot, 37132303f994SPeter Krystad .ops = &mptcp_stream_ops, 37142303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3715f870fa0bSMat Martineau }; 3716f870fa0bSMat Martineau 3717b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3718b19bc294SPaolo Abeni { 3719b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3720b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3721b19bc294SPaolo Abeni int work_done = 0; 3722b19bc294SPaolo Abeni 3723b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3724b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3725b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3726b19bc294SPaolo Abeni 3727b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3728b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3729b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3730b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3731b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3732b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3733b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3734b19bc294SPaolo Abeni * CPU that scheduled it. 3735b19bc294SPaolo Abeni */ 3736b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3737b19bc294SPaolo Abeni sock_put(ssk); 3738b19bc294SPaolo Abeni 3739b19bc294SPaolo Abeni if (++work_done == budget) 3740b19bc294SPaolo Abeni return budget; 3741b19bc294SPaolo Abeni } 3742b19bc294SPaolo Abeni 3743b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3744b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3745b19bc294SPaolo Abeni */ 3746b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3747b19bc294SPaolo Abeni return work_done; 3748b19bc294SPaolo Abeni } 3749b19bc294SPaolo Abeni 3750d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3751f870fa0bSMat Martineau { 3752b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3753b19bc294SPaolo Abeni int cpu; 3754b19bc294SPaolo Abeni 37552303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 37562303f994SPeter Krystad 3757d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3758d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3759d027236cSPaolo Abeni 3760b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3761b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3762b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3763b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 376416d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 376516d083e2SJakub Kicinski mptcp_napi_poll); 3766b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3767b19bc294SPaolo Abeni } 3768b19bc294SPaolo Abeni 37692303f994SPeter Krystad mptcp_subflow_init(); 37701b1c7a0eSPeter Krystad mptcp_pm_init(); 37712c5ebd00SPaolo Abeni mptcp_token_init(); 37722303f994SPeter Krystad 3773f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3774f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3775f870fa0bSMat Martineau 3776f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 37776771bfd9SFlorian Westphal 37786771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3779f870fa0bSMat Martineau } 3780f870fa0bSMat Martineau 3781f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3782e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3783e42f1ac6SFlorian Westphal .family = PF_INET6, 3784e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 37850a3cc579SPaolo Abeni .release = inet6_release, 3786e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3787e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3788e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3789e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3790d2f77c53SPaolo Abeni .getname = inet6_getname, 3791e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3792e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3793e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3794e42f1ac6SFlorian Westphal .listen = mptcp_listen, 379576e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3796e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3797e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3798e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3799e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3800e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3801e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3802e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 38033986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3804e42f1ac6SFlorian Westphal #endif 3805e42f1ac6SFlorian Westphal }; 3806e42f1ac6SFlorian Westphal 3807f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3808f870fa0bSMat Martineau 380979c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 381079c0949eSPeter Krystad { 381179c0949eSPeter Krystad mptcp_destroy(sk); 381279c0949eSPeter Krystad inet6_destroy_sock(sk); 381379c0949eSPeter Krystad } 381479c0949eSPeter Krystad 3815f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3816f870fa0bSMat Martineau .type = SOCK_STREAM, 3817f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3818f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 38192303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3820f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3821f870fa0bSMat Martineau }; 3822f870fa0bSMat Martineau 3823d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3824f870fa0bSMat Martineau { 3825f870fa0bSMat Martineau int err; 3826f870fa0bSMat Martineau 3827f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3828f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3829f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 383079c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3831b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3832f870fa0bSMat Martineau 3833f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3834f870fa0bSMat Martineau if (err) 3835f870fa0bSMat Martineau return err; 3836f870fa0bSMat Martineau 3837f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3838f870fa0bSMat Martineau if (err) 3839f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3840f870fa0bSMat Martineau 3841f870fa0bSMat Martineau return err; 3842f870fa0bSMat Martineau } 3843f870fa0bSMat Martineau #endif 3844