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 511*f5360e9bSPaolo 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 1243d9ca1de8SPaolo Abeni /* compute send limit */ 1244d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1245f70cad10SPaolo Abeni copy = info->size_goal; 1246f70cad10SPaolo Abeni 124757040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1248f70cad10SPaolo Abeni if (skb && copy > skb->len) { 124957040755SPaolo Abeni /* Limit the write to the size available in the 125057040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 125157040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 125257040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 125357040755SPaolo Abeni * SSN association set here 125457040755SPaolo Abeni */ 1255d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 12561094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 125757040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 12581094c6feSPaolo Abeni goto alloc_skb; 12591094c6feSPaolo Abeni } 12601094c6feSPaolo Abeni 1261f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1262f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1263f70cad10SPaolo Abeni if (!can_coalesce && i >= sysctl_max_skb_frags) { 1264f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1265f70cad10SPaolo Abeni goto alloc_skb; 126615e6ca97SPaolo Abeni } 126718b683bfSPaolo Abeni 1268f70cad10SPaolo Abeni copy -= skb->len; 1269f70cad10SPaolo Abeni } else { 12701094c6feSPaolo Abeni alloc_skb: 1271f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1272f70cad10SPaolo Abeni if (!skb) 1273f70cad10SPaolo Abeni return -ENOMEM; 1274f70cad10SPaolo Abeni 1275f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1276f70cad10SPaolo Abeni reuse_skb = false; 1277f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1278f70cad10SPaolo Abeni } 12791094c6feSPaolo Abeni 12806f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1281b713d006SPaolo Abeni copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1282f70cad10SPaolo Abeni if (copy == 0) { 12837439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12847439d687SPaolo Abeni 1285f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 128627728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 12876f8a612aSFlorian Westphal return 0; 12886f8a612aSFlorian Westphal } 12896f8a612aSFlorian Westphal 1290f70cad10SPaolo Abeni zero_window_probe = true; 1291f70cad10SPaolo Abeni data_seq = snd_una - 1; 1292f70cad10SPaolo Abeni copy = 1; 129357040755SPaolo Abeni 1294f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1295f70cad10SPaolo Abeni * ssk write queue 1296f70cad10SPaolo Abeni */ 1297f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1298f70cad10SPaolo Abeni } 1299f70cad10SPaolo Abeni 1300f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1301f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 130227728ba8SEric Dumazet tcp_remove_empty_skb(ssk); 1303e2223995SPaolo Abeni return -ENOMEM; 130435759383SFlorian Westphal } 130518b683bfSPaolo Abeni 1306f70cad10SPaolo Abeni if (can_coalesce) { 1307f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1308f70cad10SPaolo Abeni } else { 1309f70cad10SPaolo Abeni get_page(dfrag->page); 1310f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 131157040755SPaolo Abeni } 131257040755SPaolo Abeni 1313f70cad10SPaolo Abeni skb->len += copy; 1314f70cad10SPaolo Abeni skb->data_len += copy; 1315f70cad10SPaolo Abeni skb->truesize += copy; 1316f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1317f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1318f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1319f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1320f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1321f70cad10SPaolo Abeni 1322f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1323f70cad10SPaolo Abeni if (reuse_skb) { 1324f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1325f70cad10SPaolo Abeni mpext->data_len += copy; 1326f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1327f70cad10SPaolo Abeni goto out; 1328724cfd2eSPaolo Abeni } 13296d0060f6SMat Martineau 13306d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1331d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13326d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1333f70cad10SPaolo Abeni mpext->data_len = copy; 13346d0060f6SMat Martineau mpext->use_map = 1; 13356d0060f6SMat Martineau mpext->dsn64 = 1; 13366d0060f6SMat Martineau 13376d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13386d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13396d0060f6SMat Martineau mpext->dsn64); 13406d0060f6SMat Martineau 13416f8a612aSFlorian Westphal if (zero_window_probe) { 1342f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 13436f8a612aSFlorian Westphal mpext->frozen = 1; 1344d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1345f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13466f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1347d0cc2987SGeliang Tang return 0; 13486f8a612aSFlorian Westphal } 134957040755SPaolo Abeni out: 1350d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1351f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 13521e39e5a3SGeliang Tang if (mptcp_subflow_ctx(ssk)->send_infinite_map) 13531e39e5a3SGeliang Tang mptcp_update_infinite_map(msk, ssk, mpext); 13540eb4e7eeSGeliang Tang trace_mptcp_sendmsg_frag(mpext); 1355f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1356f70cad10SPaolo Abeni return copy; 13576d0060f6SMat Martineau } 13586d0060f6SMat Martineau 1359d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1360d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1361d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1362d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1363d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1364d5f49190SPaolo Abeni 1365d5f49190SPaolo Abeni struct subflow_send_info { 1366d5f49190SPaolo Abeni struct sock *ssk; 13673ce0852cSPaolo Abeni u64 linger_time; 1368d5f49190SPaolo Abeni }; 1369d5f49190SPaolo Abeni 1370ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1371ff5a0b42SPaolo Abeni { 1372ff5a0b42SPaolo Abeni if (!subflow->stale) 1373ff5a0b42SPaolo Abeni return; 1374ff5a0b42SPaolo Abeni 1375ff5a0b42SPaolo Abeni subflow->stale = 0; 1376fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1377ff5a0b42SPaolo Abeni } 1378ff5a0b42SPaolo Abeni 1379ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1380ff5a0b42SPaolo Abeni { 1381ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1382ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1383ff5a0b42SPaolo Abeni 1384ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1385ff5a0b42SPaolo Abeni return false; 1386ff5a0b42SPaolo Abeni 1387ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1388ff5a0b42SPaolo Abeni } 1389ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1390ff5a0b42SPaolo Abeni } 1391ff5a0b42SPaolo Abeni 13923ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE 0 13933ce0852cSPaolo Abeni #define SSK_MODE_BACKUP 1 13943ce0852cSPaolo Abeni #define SSK_MODE_MAX 2 13953ce0852cSPaolo Abeni 139633d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 139733d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 139833d41c9cSPaolo Abeni * additionally updates the rtx timeout 139933d41c9cSPaolo Abeni */ 14005cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1401f296234cSPeter Krystad { 14023ce0852cSPaolo Abeni struct subflow_send_info send_info[SSK_MODE_MAX]; 1403f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 140433d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 14053ce0852cSPaolo Abeni u32 pace, burst, wmem; 1406d5f49190SPaolo Abeni int i, nr_active = 0; 1407d5f49190SPaolo Abeni struct sock *ssk; 14083ce0852cSPaolo Abeni u64 linger_time; 140933d41c9cSPaolo Abeni long tout = 0; 1410f296234cSPeter Krystad 141133d41c9cSPaolo Abeni sock_owned_by_me(sk); 1412f296234cSPeter Krystad 1413d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1414d5f49190SPaolo Abeni if (!msk->first) 1415f296234cSPeter Krystad return NULL; 1416d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1417f296234cSPeter Krystad } 1418f296234cSPeter Krystad 1419d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1420d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1421d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 142233d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 142333d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1424d5f49190SPaolo Abeni return msk->last_snd; 142533d41c9cSPaolo Abeni } 1426f296234cSPeter Krystad 1427d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 14283ce0852cSPaolo Abeni for (i = 0; i < SSK_MODE_MAX; ++i) { 1429d5f49190SPaolo Abeni send_info[i].ssk = NULL; 14303ce0852cSPaolo Abeni send_info[i].linger_time = -1; 1431d5f49190SPaolo Abeni } 14323ce0852cSPaolo Abeni 1433d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1434e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1435d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1436d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1437f296234cSPeter Krystad continue; 1438d5f49190SPaolo Abeni 143933d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1440d5f49190SPaolo Abeni nr_active += !subflow->backup; 14413ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 14423ce0852cSPaolo Abeni if (unlikely(!pace)) { 14433ce0852cSPaolo Abeni /* init pacing rate from socket */ 14443ce0852cSPaolo Abeni subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 14453ce0852cSPaolo Abeni pace = subflow->avg_pacing_rate; 1446d5f49190SPaolo Abeni if (!pace) 1447d5f49190SPaolo Abeni continue; 14483ce0852cSPaolo Abeni } 1449d5f49190SPaolo Abeni 14503ce0852cSPaolo Abeni linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 14513ce0852cSPaolo Abeni if (linger_time < send_info[subflow->backup].linger_time) { 1452d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 14533ce0852cSPaolo Abeni send_info[subflow->backup].linger_time = linger_time; 1454d5f49190SPaolo Abeni } 1455f296234cSPeter Krystad } 145633d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1457f296234cSPeter Krystad 1458d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1459d5f49190SPaolo Abeni if (!nr_active) 14603ce0852cSPaolo Abeni send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1461d5f49190SPaolo Abeni 14623ce0852cSPaolo Abeni /* According to the blest algorithm, to avoid HoL blocking for the 14633ce0852cSPaolo Abeni * faster flow, we need to: 14643ce0852cSPaolo Abeni * - estimate the faster flow linger time 14653ce0852cSPaolo Abeni * - use the above to estimate the amount of byte transferred 14663ce0852cSPaolo Abeni * by the faster flow 14673ce0852cSPaolo Abeni * - check that the amount of queued data is greter than the above, 14683ce0852cSPaolo Abeni * otherwise do not use the picked, slower, subflow 14693ce0852cSPaolo Abeni * We select the subflow with the shorter estimated time to flush 14703ce0852cSPaolo Abeni * the queued mem, which basically ensure the above. We just need 14713ce0852cSPaolo Abeni * to check that subflow has a non empty cwin. 14723ce0852cSPaolo Abeni */ 14733ce0852cSPaolo Abeni ssk = send_info[SSK_MODE_ACTIVE].ssk; 1474b713d006SPaolo Abeni if (!ssk || !sk_stream_memory_free(ssk)) 1475d5f49190SPaolo Abeni return NULL; 14763ce0852cSPaolo Abeni 1477b713d006SPaolo Abeni burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 14783ce0852cSPaolo Abeni wmem = READ_ONCE(ssk->sk_wmem_queued); 1479b713d006SPaolo Abeni if (!burst) { 1480b713d006SPaolo Abeni msk->last_snd = NULL; 1481b713d006SPaolo Abeni return ssk; 1482b713d006SPaolo Abeni } 1483b713d006SPaolo Abeni 14843ce0852cSPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 14853ce0852cSPaolo Abeni subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 14863ce0852cSPaolo Abeni READ_ONCE(ssk->sk_pacing_rate) * burst, 14873ce0852cSPaolo Abeni burst + wmem); 14883ce0852cSPaolo Abeni msk->last_snd = ssk; 14893ce0852cSPaolo Abeni msk->snd_burst = burst; 14903ce0852cSPaolo Abeni return ssk; 1491f296234cSPeter Krystad } 1492f296234cSPeter Krystad 1493b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1494d9ca1de8SPaolo Abeni { 1495d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1496d9ca1de8SPaolo Abeni release_sock(ssk); 1497d9ca1de8SPaolo Abeni } 1498d9ca1de8SPaolo Abeni 14990d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 15000d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 15010d199e43SFlorian Westphal u32 sent) 15020d199e43SFlorian Westphal { 15030d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 15040d199e43SFlorian Westphal 15050d199e43SFlorian Westphal dfrag->already_sent += sent; 15060d199e43SFlorian Westphal 15070d199e43SFlorian Westphal msk->snd_burst -= sent; 15080d199e43SFlorian Westphal 15090d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 15100d199e43SFlorian Westphal 15110d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 15120d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 15130d199e43SFlorian Westphal * old segments. 15140d199e43SFlorian Westphal * 15150d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 15160d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 15170d199e43SFlorian Westphal * that has been handed to the subflow for transmission 15180d199e43SFlorian Westphal * and skip update in case it was old dfrag. 15190d199e43SFlorian Westphal */ 15200d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 15210d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 15220d199e43SFlorian Westphal } 15230d199e43SFlorian Westphal 15248b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk) 15253241a9c0SFlorian Westphal { 1526e9d09bacSPaolo Abeni if (mptcp_send_head(sk)) 1527e9d09bacSPaolo Abeni mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 15283241a9c0SFlorian Westphal } 15293241a9c0SFlorian Westphal 1530ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1531d9ca1de8SPaolo Abeni { 1532d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1533d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1534d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1535d9ca1de8SPaolo Abeni .flags = flags, 1536d9ca1de8SPaolo Abeni }; 1537d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1538d9ca1de8SPaolo Abeni int len, copied = 0; 1539d9ca1de8SPaolo Abeni 1540d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1541d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1542d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1543d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1544d9ca1de8SPaolo Abeni while (len > 0) { 1545d9ca1de8SPaolo Abeni int ret = 0; 1546d9ca1de8SPaolo Abeni 1547d9ca1de8SPaolo Abeni prev_ssk = ssk; 15485cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1549d9ca1de8SPaolo Abeni 15509758f40eSPaolo Abeni /* First check. If the ssk has changed since 15519758f40eSPaolo Abeni * the last round, release prev_ssk 1552d9ca1de8SPaolo Abeni */ 1553d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1554b8e0def3SGeliang Tang mptcp_push_release(prev_ssk, &info); 1555d9ca1de8SPaolo Abeni if (!ssk) 1556d9ca1de8SPaolo Abeni goto out; 1557d9ca1de8SPaolo Abeni 15589758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15599758f40eSPaolo Abeni * from the previous one, otherwise we are still 15609758f40eSPaolo Abeni * helding the relevant lock 15619758f40eSPaolo Abeni */ 15629758f40eSPaolo Abeni if (ssk != prev_ssk) 1563d9ca1de8SPaolo Abeni lock_sock(ssk); 1564d9ca1de8SPaolo Abeni 1565d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1566d9ca1de8SPaolo Abeni if (ret <= 0) { 1567b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1568d9ca1de8SPaolo Abeni goto out; 1569d9ca1de8SPaolo Abeni } 1570d9ca1de8SPaolo Abeni 1571d9ca1de8SPaolo Abeni info.sent += ret; 1572d9ca1de8SPaolo Abeni copied += ret; 1573d9ca1de8SPaolo Abeni len -= ret; 15740d199e43SFlorian Westphal 15750d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1576d9ca1de8SPaolo Abeni } 1577d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1578d9ca1de8SPaolo Abeni } 1579d9ca1de8SPaolo Abeni 1580d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1581d9ca1de8SPaolo Abeni if (ssk) 1582b8e0def3SGeliang Tang mptcp_push_release(ssk, &info); 1583d9ca1de8SPaolo Abeni 1584d9ca1de8SPaolo Abeni out: 158533d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1586d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1587d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 158833d41c9cSPaolo Abeni if (copied) 1589d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1590d9ca1de8SPaolo Abeni } 1591d9ca1de8SPaolo Abeni 15926e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 15936e628cd3SPaolo Abeni { 15946e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15951094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 15961094c6feSPaolo Abeni .data_lock_held = true, 15971094c6feSPaolo Abeni }; 15986e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1599b19bc294SPaolo Abeni struct sock *xmit_ssk; 16006e628cd3SPaolo Abeni int len, copied = 0; 1601b19bc294SPaolo Abeni bool first = true; 16026e628cd3SPaolo Abeni 16036e628cd3SPaolo Abeni info.flags = 0; 16046e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16056e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16066e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16076e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16086e628cd3SPaolo Abeni while (len > 0) { 16096e628cd3SPaolo Abeni int ret = 0; 16106e628cd3SPaolo Abeni 1611b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1612b19bc294SPaolo Abeni * check for a different subflow usage only after 1613b19bc294SPaolo Abeni * spooling the first chunk of data 1614b19bc294SPaolo Abeni */ 1615b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1616b19bc294SPaolo Abeni if (!xmit_ssk) 1617b19bc294SPaolo Abeni goto out; 1618b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1619bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1620bcd97734SPaolo Abeni MPTCP_DELEGATE_SEND); 1621b19bc294SPaolo Abeni goto out; 1622b19bc294SPaolo Abeni } 1623b19bc294SPaolo Abeni 16246e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16256e628cd3SPaolo Abeni if (ret <= 0) 16266e628cd3SPaolo Abeni goto out; 16276e628cd3SPaolo Abeni 16286e628cd3SPaolo Abeni info.sent += ret; 16296e628cd3SPaolo Abeni copied += ret; 16306e628cd3SPaolo Abeni len -= ret; 1631b19bc294SPaolo Abeni first = false; 16320d199e43SFlorian Westphal 16330d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16346e628cd3SPaolo Abeni } 16356e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16366e628cd3SPaolo Abeni } 16376e628cd3SPaolo Abeni 16386e628cd3SPaolo Abeni out: 16396e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16406e628cd3SPaolo Abeni * not going to flush it via release_sock() 16416e628cd3SPaolo Abeni */ 16426e628cd3SPaolo Abeni if (copied) { 16436e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16446e628cd3SPaolo Abeni info.size_goal); 1645d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1646d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1647d09d818eSPaolo Abeni 16486e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16496e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16506e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16516e628cd3SPaolo Abeni } 16526e628cd3SPaolo Abeni } 16536e628cd3SPaolo Abeni 16545cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16555cf92bbaSPaolo Abeni { 16565cf92bbaSPaolo Abeni /* enable autotune */ 16575cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16585cf92bbaSPaolo Abeni 16595cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16605cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16615cf92bbaSPaolo Abeni } 16625cf92bbaSPaolo Abeni 1663f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1664f870fa0bSMat Martineau { 1665f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 166617091708SFlorian Westphal struct page_frag *pfrag; 16676d0060f6SMat Martineau size_t copied = 0; 1668caf971dfSPaolo Abeni int ret = 0; 16696d0060f6SMat Martineau long timeo; 1670f870fa0bSMat Martineau 1671987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1672987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1673f870fa0bSMat Martineau return -EOPNOTSUPP; 1674f870fa0bSMat Martineau 1675987858e5SPaolo Abeni /* silently ignore everything else */ 1676987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1677987858e5SPaolo Abeni 16786511882cSPaolo Abeni lock_sock(sk); 16791954b860SMat Martineau 16801954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 16811954b860SMat Martineau 16821954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 16831954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 16841954b860SMat Martineau if (ret) 16851954b860SMat Martineau goto out; 16861954b860SMat Martineau } 16871954b860SMat Martineau 168817091708SFlorian Westphal pfrag = sk_page_frag(sk); 168918b683bfSPaolo Abeni 1690d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1691724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1692d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1693d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1694d9ca1de8SPaolo Abeni size_t psize, offset; 1695d9ca1de8SPaolo Abeni 169657baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 169757baaf28SMat Martineau ret = -EPIPE; 169857baaf28SMat Martineau goto out; 169957baaf28SMat Martineau } 170057baaf28SMat Martineau 1701d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1702d9ca1de8SPaolo Abeni * page allocator 1703fb529e62SFlorian Westphal */ 1704d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1705d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1706d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1707d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1708d9ca1de8SPaolo Abeni goto wait_for_memory; 17096e628cd3SPaolo Abeni 1710d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1711d9ca1de8SPaolo Abeni goto wait_for_memory; 1712d9ca1de8SPaolo Abeni 1713d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1714d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1715fb529e62SFlorian Westphal } 1716fb529e62SFlorian Westphal 1717d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1718d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1719d9ca1de8SPaolo Abeni * anyway 1720d9ca1de8SPaolo Abeni */ 1721d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1722d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1723d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1724724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17258ce568edSPaolo Abeni 17266511882cSPaolo Abeni if (!sk_wmem_schedule(sk, total_ts)) 1727d9ca1de8SPaolo Abeni goto wait_for_memory; 1728d9ca1de8SPaolo Abeni 1729d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1730d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1731d9ca1de8SPaolo Abeni ret = -EFAULT; 1732d9ca1de8SPaolo Abeni goto out; 1733d9ca1de8SPaolo Abeni } 1734d9ca1de8SPaolo Abeni 1735d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 17366511882cSPaolo Abeni sk->sk_forward_alloc -= total_ts; 1737d9ca1de8SPaolo Abeni copied += psize; 1738d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1739d9ca1de8SPaolo Abeni frag_truesize += psize; 1740d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1741d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1742d9ca1de8SPaolo Abeni 1743d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1744d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1745d9ca1de8SPaolo Abeni */ 1746d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1747d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1748d9ca1de8SPaolo Abeni get_page(dfrag->page); 1749d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1750d9ca1de8SPaolo Abeni if (!msk->first_pending) 1751d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1752d9ca1de8SPaolo Abeni } 1753e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1754d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1755d9ca1de8SPaolo Abeni !dfrag_collapsed); 1756d9ca1de8SPaolo Abeni 1757d9ca1de8SPaolo Abeni continue; 1758d9ca1de8SPaolo Abeni 1759d9ca1de8SPaolo Abeni wait_for_memory: 17605cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1761c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1762f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1763f296234cSPeter Krystad if (ret) 1764f296234cSPeter Krystad goto out; 1765cec37a6eSPeter Krystad } 1766cec37a6eSPeter Krystad 176713e16037SPaolo Abeni if (copied) 1768c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1769da51aef5SPaolo Abeni 17701954b860SMat Martineau out: 1771cec37a6eSPeter Krystad release_sock(sk); 17728555c6bfSPaolo Abeni return copied ? : ret; 1773f870fa0bSMat Martineau } 1774f870fa0bSMat Martineau 17756771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 17766771bfd9SFlorian Westphal struct msghdr *msg, 1777b7f653b2SFlorian Westphal size_t len, int flags, 1778b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1779b7f653b2SFlorian Westphal int *cmsg_flags) 17806771bfd9SFlorian Westphal { 1781ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 17826771bfd9SFlorian Westphal int copied = 0; 17836771bfd9SFlorian Westphal 1784ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 17856771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 17866771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 17876771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 17886771bfd9SFlorian Westphal int err; 17896771bfd9SFlorian Westphal 1790d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 17916771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 17926771bfd9SFlorian Westphal if (unlikely(err < 0)) { 17936771bfd9SFlorian Westphal if (!copied) 17946771bfd9SFlorian Westphal return err; 17956771bfd9SFlorian Westphal break; 17966771bfd9SFlorian Westphal } 1797d976092cSPaolo Abeni } 17986771bfd9SFlorian Westphal 1799b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1800b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1801b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1802b7f653b2SFlorian Westphal } 1803b7f653b2SFlorian Westphal 18046771bfd9SFlorian Westphal copied += count; 18056771bfd9SFlorian Westphal 18066771bfd9SFlorian Westphal if (count < data_len) { 18072c9e7765SFlorian Westphal if (!(flags & MSG_PEEK)) { 18086771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18092c9e7765SFlorian Westphal MPTCP_SKB_CB(skb)->map_seq += count; 18102c9e7765SFlorian Westphal } 18116771bfd9SFlorian Westphal break; 18126771bfd9SFlorian Westphal } 18136771bfd9SFlorian Westphal 1814ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 181587952603SPaolo Abeni /* we will bulk release the skb memory later */ 181687952603SPaolo Abeni skb->destructor = NULL; 1817ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 181887952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18196771bfd9SFlorian Westphal __kfree_skb(skb); 1820ca4fb892SYonglong Li } 18216771bfd9SFlorian Westphal 18226771bfd9SFlorian Westphal if (copied >= len) 18236771bfd9SFlorian Westphal break; 18246771bfd9SFlorian Westphal } 18256771bfd9SFlorian Westphal 18266771bfd9SFlorian Westphal return copied; 18276771bfd9SFlorian Westphal } 18286771bfd9SFlorian Westphal 1829a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1830a6b118feSFlorian Westphal * 1831a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1832a6b118feSFlorian Westphal */ 1833a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1834a6b118feSFlorian Westphal { 1835a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1836a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1837a6b118feSFlorian Westphal u32 time, advmss = 1; 1838a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1839a6b118feSFlorian Westphal 1840a6b118feSFlorian Westphal sock_owned_by_me(sk); 1841a6b118feSFlorian Westphal 1842a6b118feSFlorian Westphal if (copied <= 0) 1843a6b118feSFlorian Westphal return; 1844a6b118feSFlorian Westphal 1845a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1846a6b118feSFlorian Westphal 1847a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1848a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1849a6b118feSFlorian Westphal 1850a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1851a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1852a6b118feSFlorian Westphal return; 1853a6b118feSFlorian Westphal 1854a6b118feSFlorian Westphal rtt_us = 0; 1855a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1856a6b118feSFlorian Westphal const struct tcp_sock *tp; 1857a6b118feSFlorian Westphal u64 sf_rtt_us; 1858a6b118feSFlorian Westphal u32 sf_advmss; 1859a6b118feSFlorian Westphal 1860a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1861a6b118feSFlorian Westphal 1862a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1863a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1864a6b118feSFlorian Westphal 1865a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1866a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1867a6b118feSFlorian Westphal } 1868a6b118feSFlorian Westphal 1869a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1870a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1871a6b118feSFlorian Westphal return; 1872a6b118feSFlorian Westphal 1873a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1874a6b118feSFlorian Westphal goto new_measure; 1875a6b118feSFlorian Westphal 1876a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1877a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1878a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1879a6b118feSFlorian Westphal u64 rcvwin, grow; 1880a6b118feSFlorian Westphal 1881a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1882a6b118feSFlorian Westphal 1883a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1884a6b118feSFlorian Westphal 1885a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1886a6b118feSFlorian Westphal rcvwin += (grow << 1); 1887a6b118feSFlorian Westphal 1888a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1889a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1890a6b118feSFlorian Westphal rcvmem += 128; 1891a6b118feSFlorian Westphal 1892a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1893a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1894a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1895a6b118feSFlorian Westphal 1896a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1897a6b118feSFlorian Westphal u32 window_clamp; 1898a6b118feSFlorian Westphal 1899a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1900a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1901a6b118feSFlorian Westphal 1902a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1903a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1904a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1905a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1906a6b118feSFlorian Westphal */ 1907a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1908a6b118feSFlorian Westphal struct sock *ssk; 1909c76c6956SPaolo Abeni bool slow; 1910a6b118feSFlorian Westphal 1911a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1912c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1913a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1914a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1915c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1916c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1917a6b118feSFlorian Westphal } 1918a6b118feSFlorian Westphal } 1919a6b118feSFlorian Westphal } 1920a6b118feSFlorian Westphal 1921a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1922a6b118feSFlorian Westphal new_measure: 1923a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1924a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1925a6b118feSFlorian Westphal } 1926a6b118feSFlorian Westphal 192787952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 192887952603SPaolo Abeni { 192987952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 193087952603SPaolo Abeni 193187952603SPaolo Abeni if (!msk->rmem_released) 193287952603SPaolo Abeni return; 193387952603SPaolo Abeni 193487952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 19356511882cSPaolo Abeni mptcp_rmem_uncharge(sk, msk->rmem_released); 1936ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 193787952603SPaolo Abeni } 193887952603SPaolo Abeni 193987952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 194087952603SPaolo Abeni { 194187952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 194287952603SPaolo Abeni 194387952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 194487952603SPaolo Abeni } 194587952603SPaolo Abeni 1946e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19476771bfd9SFlorian Westphal { 194887952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19496771bfd9SFlorian Westphal unsigned int moved = 0; 195087952603SPaolo Abeni bool ret, done; 1951d5f49190SPaolo Abeni 19526771bfd9SFlorian Westphal do { 19536771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 195465f49fe7SFlorian Westphal bool slowpath; 19556771bfd9SFlorian Westphal 195687952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 195787952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 195887952603SPaolo Abeni * that is an unlikely slow path. 195987952603SPaolo Abeni */ 196087952603SPaolo Abeni if (likely(!ssk)) 19616771bfd9SFlorian Westphal break; 19626771bfd9SFlorian Westphal 196365f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 196487952603SPaolo Abeni mptcp_data_lock(sk); 1965e3859603SPaolo Abeni __mptcp_update_rmem(sk); 19666771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 196787952603SPaolo Abeni mptcp_data_unlock(sk); 1968499ada50SPaolo Abeni 1969499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 1970499ada50SPaolo Abeni __mptcp_error_report(sk); 197165f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 19726771bfd9SFlorian Westphal } while (!done); 19736771bfd9SFlorian Westphal 197487952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 197587952603SPaolo Abeni ret = moved > 0; 197687952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 197787952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 197887952603SPaolo Abeni mptcp_data_lock(sk); 197987952603SPaolo Abeni __mptcp_update_rmem(sk); 198087952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 198187952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 198287952603SPaolo Abeni mptcp_data_unlock(sk); 1983ab174ad8SPaolo Abeni } 198487952603SPaolo Abeni if (ret) 198587952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 198687952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 19876771bfd9SFlorian Westphal } 19886771bfd9SFlorian Westphal 19892c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk) 19902c9e7765SFlorian Westphal { 19912c9e7765SFlorian Westphal const struct mptcp_sock *msk = mptcp_sk(sk); 19922c9e7765SFlorian Westphal const struct sk_buff *skb; 19932c9e7765SFlorian Westphal 19942c9e7765SFlorian Westphal skb = skb_peek(&msk->receive_queue); 19952c9e7765SFlorian Westphal if (skb) { 19962c9e7765SFlorian Westphal u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 19972c9e7765SFlorian Westphal 19982c9e7765SFlorian Westphal if (hint_val >= INT_MAX) 19992c9e7765SFlorian Westphal return INT_MAX; 20002c9e7765SFlorian Westphal 20012c9e7765SFlorian Westphal return (unsigned int)hint_val; 20022c9e7765SFlorian Westphal } 20032c9e7765SFlorian Westphal 20042c9e7765SFlorian Westphal if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 20052c9e7765SFlorian Westphal return 1; 20062c9e7765SFlorian Westphal 20072c9e7765SFlorian Westphal return 0; 20082c9e7765SFlorian Westphal } 20092c9e7765SFlorian Westphal 2010f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2011ec095263SOliver Hartkopp int flags, int *addr_len) 2012f870fa0bSMat Martineau { 2013f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2014b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2015b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20167a6a6cbcSPaolo Abeni int target; 20177a6a6cbcSPaolo Abeni long timeo; 2018f870fa0bSMat Martineau 2019cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2020cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2021cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2022cb9d80f4SPaolo Abeni 20236511882cSPaolo Abeni lock_sock(sk); 2024fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2025fd897679SPaolo Abeni copied = -ENOTCONN; 2026fd897679SPaolo Abeni goto out_err; 2027fd897679SPaolo Abeni } 2028fd897679SPaolo Abeni 2029ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 20307a6a6cbcSPaolo Abeni 20317a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20327a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20337a6a6cbcSPaolo Abeni 20342c9e7765SFlorian Westphal if (unlikely(msk->recvmsg_inq)) 20352c9e7765SFlorian Westphal cmsg_flags = MPTCP_CMSG_INQ; 20362c9e7765SFlorian Westphal 203705e3eceaSEric Dumazet while (copied < len) { 2038e3859603SPaolo Abeni int bytes_read; 20397a6a6cbcSPaolo Abeni 2040b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20416771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20427a6a6cbcSPaolo Abeni if (!copied) 20437a6a6cbcSPaolo Abeni copied = bytes_read; 20446771bfd9SFlorian Westphal goto out_err; 2045cec37a6eSPeter Krystad } 2046cec37a6eSPeter Krystad 20477a6a6cbcSPaolo Abeni copied += bytes_read; 20487a6a6cbcSPaolo Abeni 2049ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2050fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2051ea4ca586SPaolo Abeni 2052e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2053e3859603SPaolo Abeni continue; 2054e3859603SPaolo Abeni 20557a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 20567a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 20577a6a6cbcSPaolo Abeni */ 20587a6a6cbcSPaolo Abeni if (copied >= target) 20597a6a6cbcSPaolo Abeni break; 20607a6a6cbcSPaolo Abeni 20617a6a6cbcSPaolo Abeni if (copied) { 20627a6a6cbcSPaolo Abeni if (sk->sk_err || 20637a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 20647a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 20657a6a6cbcSPaolo Abeni !timeo || 20667a6a6cbcSPaolo Abeni signal_pending(current)) 20677a6a6cbcSPaolo Abeni break; 20687a6a6cbcSPaolo Abeni } else { 20697a6a6cbcSPaolo Abeni if (sk->sk_err) { 20707a6a6cbcSPaolo Abeni copied = sock_error(sk); 20717a6a6cbcSPaolo Abeni break; 20727a6a6cbcSPaolo Abeni } 20737a6a6cbcSPaolo Abeni 20745969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 20755969856aSPaolo Abeni mptcp_check_for_eof(msk); 20765969856aSPaolo Abeni 207787952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 207887952603SPaolo Abeni /* race breaker: the shutdown could be after the 207987952603SPaolo Abeni * previous receive queue check 208087952603SPaolo Abeni */ 2081e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 208287952603SPaolo Abeni continue; 20837a6a6cbcSPaolo Abeni break; 208487952603SPaolo Abeni } 20857a6a6cbcSPaolo Abeni 20867a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 20877a6a6cbcSPaolo Abeni copied = -ENOTCONN; 20887a6a6cbcSPaolo Abeni break; 20897a6a6cbcSPaolo Abeni } 20907a6a6cbcSPaolo Abeni 20917a6a6cbcSPaolo Abeni if (!timeo) { 20927a6a6cbcSPaolo Abeni copied = -EAGAIN; 20937a6a6cbcSPaolo Abeni break; 20947a6a6cbcSPaolo Abeni } 20957a6a6cbcSPaolo Abeni 20967a6a6cbcSPaolo Abeni if (signal_pending(current)) { 20977a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 20987a6a6cbcSPaolo Abeni break; 20997a6a6cbcSPaolo Abeni } 21007a6a6cbcSPaolo Abeni } 21017a6a6cbcSPaolo Abeni 21027a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 2103612f71d7SPaolo Abeni sk_wait_data(sk, &timeo, NULL); 21047a6a6cbcSPaolo Abeni } 21053c90e377SPaolo Abeni 21066771bfd9SFlorian Westphal out_err: 2107b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2108b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2109b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 21102c9e7765SFlorian Westphal 21112c9e7765SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_INQ) { 21122c9e7765SFlorian Westphal unsigned int inq = mptcp_inq_hint(sk); 21132c9e7765SFlorian Westphal 21142c9e7765SFlorian Westphal put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 21152c9e7765SFlorian Westphal } 2116b7f653b2SFlorian Westphal } 2117b7f653b2SFlorian Westphal 2118612f71d7SPaolo Abeni pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2119612f71d7SPaolo Abeni msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2120612f71d7SPaolo Abeni skb_queue_empty(&msk->receive_queue), copied); 2121ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2122a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2123a6b118feSFlorian Westphal 2124cec37a6eSPeter Krystad release_sock(sk); 2125cec37a6eSPeter Krystad return copied; 2126cec37a6eSPeter Krystad } 2127cec37a6eSPeter Krystad 2128b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2129b51f9b80SPaolo Abeni { 2130b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2131b51f9b80SPaolo Abeni icsk_retransmit_timer); 2132b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21332d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2134b51f9b80SPaolo Abeni 2135b51f9b80SPaolo Abeni bh_lock_sock(sk); 2136b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21372d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21382d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21392d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2140b51f9b80SPaolo Abeni } else { 2141b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2142e9d09bacSPaolo Abeni __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2143b51f9b80SPaolo Abeni } 2144b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2145b51f9b80SPaolo Abeni sock_put(sk); 2146b51f9b80SPaolo Abeni } 2147b51f9b80SPaolo Abeni 2148e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2149e16163b6SPaolo Abeni { 2150e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2151e16163b6SPaolo Abeni 2152e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2153b6d69fc8SFlorian Westphal sock_put(sk); 2154e16163b6SPaolo Abeni } 2155e16163b6SPaolo Abeni 21563b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 21573b1d6210SPaolo Abeni * level. 21583b1d6210SPaolo Abeni * 21593b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 21603b1d6210SPaolo Abeni */ 2161ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 21623b1d6210SPaolo Abeni { 216371b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 21643b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 216571b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 21663b1d6210SPaolo Abeni 21673b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 21683b1d6210SPaolo Abeni 2169d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2170d9ca1de8SPaolo Abeni return NULL; 2171d5f49190SPaolo Abeni 21723b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 21733b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21743b1d6210SPaolo Abeni 2175ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2176d5f49190SPaolo Abeni continue; 2177d5f49190SPaolo Abeni 217871b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 217971b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 218071b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 218171b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2182860975c6SFlorian Westphal continue; 2183860975c6SFlorian Westphal } 21843b1d6210SPaolo Abeni 21853b1d6210SPaolo Abeni if (subflow->backup) { 21863b1d6210SPaolo Abeni if (!backup) 21873b1d6210SPaolo Abeni backup = ssk; 21883b1d6210SPaolo Abeni continue; 21893b1d6210SPaolo Abeni } 21903b1d6210SPaolo Abeni 219171b7dec2SPaolo Abeni if (!pick) 219271b7dec2SPaolo Abeni pick = ssk; 21933b1d6210SPaolo Abeni } 21943b1d6210SPaolo Abeni 219571b7dec2SPaolo Abeni if (pick) 219671b7dec2SPaolo Abeni return pick; 219771b7dec2SPaolo Abeni 219871b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 219971b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22003b1d6210SPaolo Abeni } 22013b1d6210SPaolo Abeni 220217aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 220317aee05dSFlorian Westphal { 220417aee05dSFlorian Westphal if (msk->subflow) { 220517aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 220617aee05dSFlorian Westphal msk->subflow = NULL; 220717aee05dSFlorian Westphal } 220817aee05dSFlorian Westphal } 220917aee05dSFlorian Westphal 22101e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22111e1d9d6fSPaolo Abeni { 22121e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22131e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22141e1d9d6fSPaolo Abeni 22151e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22161e1d9d6fSPaolo Abeni return false; 22171e1d9d6fSPaolo Abeni 22181e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22191e1d9d6fSPaolo Abeni return false; 22201e1d9d6fSPaolo Abeni 22211e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22221e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22231e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22241e1d9d6fSPaolo Abeni */ 22251e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22261e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22271e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22281e1d9d6fSPaolo Abeni if (!rtx_head) { 22291e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22301e1d9d6fSPaolo Abeni return false; 22311e1d9d6fSPaolo Abeni } 22321e1d9d6fSPaolo Abeni 22331e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22341e1d9d6fSPaolo Abeni msk->recovery = true; 22351e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22361e1d9d6fSPaolo Abeni 22371e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22381e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22391e1d9d6fSPaolo Abeni 22401e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22411e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22421e1d9d6fSPaolo Abeni if (!cur->already_sent) 22431e1d9d6fSPaolo Abeni break; 22441e1d9d6fSPaolo Abeni cur->already_sent = 0; 22451e1d9d6fSPaolo Abeni } 22461e1d9d6fSPaolo Abeni 22471e1d9d6fSPaolo Abeni return true; 22481e1d9d6fSPaolo Abeni } 22491e1d9d6fSPaolo Abeni 2250b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */ 2251b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH BIT(1) 2252b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE BIT(2) 2253b29fcfb5SPaolo Abeni 2254cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2255cec37a6eSPeter Krystad * (accept). 2256cec37a6eSPeter Krystad * 2257cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2258cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2259cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2260cec37a6eSPeter Krystad * parent socket. 2261cec37a6eSPeter Krystad */ 2262a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2263b29fcfb5SPaolo Abeni struct mptcp_subflow_context *subflow, 2264b29fcfb5SPaolo Abeni unsigned int flags) 2265cec37a6eSPeter Krystad { 2266e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2267b29fcfb5SPaolo Abeni bool need_push, dispose_it; 2268e0be4931SFlorian Westphal 2269b29fcfb5SPaolo Abeni dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2270b29fcfb5SPaolo Abeni if (dispose_it) 2271cec37a6eSPeter Krystad list_del(&subflow->node); 2272cec37a6eSPeter Krystad 22733f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2274e16163b6SPaolo Abeni 2275b29fcfb5SPaolo Abeni if (flags & MPTCP_CF_FASTCLOSE) 2276b29fcfb5SPaolo Abeni subflow->send_fastclose = 1; 2277b29fcfb5SPaolo Abeni 2278b29fcfb5SPaolo Abeni need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2279b29fcfb5SPaolo Abeni if (!dispose_it) { 2280b29fcfb5SPaolo Abeni tcp_disconnect(ssk, 0); 2281b29fcfb5SPaolo Abeni msk->subflow->state = SS_UNCONNECTED; 2282b29fcfb5SPaolo Abeni mptcp_subflow_ctx_reset(subflow); 2283b29fcfb5SPaolo Abeni release_sock(ssk); 2284b29fcfb5SPaolo Abeni 2285b29fcfb5SPaolo Abeni goto out; 2286b29fcfb5SPaolo Abeni } 2287b29fcfb5SPaolo Abeni 2288e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2289e16163b6SPaolo Abeni * already orphaned 2290e16163b6SPaolo Abeni */ 2291866f26f2SPaolo Abeni if (ssk->sk_socket) 2292e16163b6SPaolo Abeni sock_orphan(ssk); 2293e16163b6SPaolo Abeni 2294d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2295d7b1bfd0SPaolo Abeni 2296e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2297e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2298e16163b6SPaolo Abeni * reference owned by msk; 2299e16163b6SPaolo Abeni */ 2300e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2301e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2302e16163b6SPaolo Abeni } else { 2303d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 23046aeed904SPaolo Abeni if (ssk->sk_state == TCP_LISTEN) { 23056aeed904SPaolo Abeni tcp_set_state(ssk, TCP_CLOSE); 23066aeed904SPaolo Abeni mptcp_subflow_queue_clean(ssk); 23076aeed904SPaolo Abeni inet_csk_listen_stop(ssk); 23086aeed904SPaolo Abeni } 2309e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2310e16163b6SPaolo Abeni 2311e16163b6SPaolo Abeni /* close acquired an extra ref */ 2312e16163b6SPaolo Abeni __sock_put(ssk); 2313e16163b6SPaolo Abeni } 2314e16163b6SPaolo Abeni release_sock(ssk); 2315e16163b6SPaolo Abeni 2316e16163b6SPaolo Abeni sock_put(ssk); 2317e0be4931SFlorian Westphal 2318c8fe62f0SFlorian Westphal if (ssk == msk->first) 2319c8fe62f0SFlorian Westphal msk->first = NULL; 2320c8fe62f0SFlorian Westphal 2321b29fcfb5SPaolo Abeni out: 2322b29fcfb5SPaolo Abeni if (ssk == msk->last_snd) 2323b29fcfb5SPaolo Abeni msk->last_snd = NULL; 23241e1d9d6fSPaolo Abeni 23251e1d9d6fSPaolo Abeni if (need_push) 23261e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2327f870fa0bSMat Martineau } 2328f870fa0bSMat Martineau 2329a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2330a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2331a141e02eSFlorian Westphal { 2332b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2333b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2334a88c9e49SPaolo Abeni 2335a88c9e49SPaolo Abeni /* subflow aborted before reaching the fully_established status 2336a88c9e49SPaolo Abeni * attempt the creation of the next subflow 2337a88c9e49SPaolo Abeni */ 2338a88c9e49SPaolo Abeni mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2339a88c9e49SPaolo Abeni 2340b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2341a141e02eSFlorian Westphal } 2342a141e02eSFlorian Westphal 2343dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2344dc24f8b4SPaolo Abeni { 2345dc24f8b4SPaolo Abeni return 0; 2346dc24f8b4SPaolo Abeni } 2347dc24f8b4SPaolo Abeni 23480e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 23490e4f35d7SPaolo Abeni { 23500e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 23510e4f35d7SPaolo Abeni 23523abc05d9SFlorian Westphal might_sleep(); 23533abc05d9SFlorian Westphal 23540e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 23550e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 23560e4f35d7SPaolo Abeni 23570e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 23580e4f35d7SPaolo Abeni continue; 23590e4f35d7SPaolo Abeni 236040947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 236140947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 236240947e13SFlorian Westphal continue; 236340947e13SFlorian Westphal 2364a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 23650e4f35d7SPaolo Abeni } 23660e4f35d7SPaolo Abeni } 23670e4f35d7SPaolo Abeni 2368e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2369e16163b6SPaolo Abeni { 2370e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2371e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2372e16163b6SPaolo Abeni 2373e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2374e16163b6SPaolo Abeni return true; 2375e16163b6SPaolo Abeni 2376e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2377e16163b6SPaolo Abeni * timeout 2378e16163b6SPaolo Abeni */ 2379e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2380e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2381e16163b6SPaolo Abeni TCP_CLOSE) 2382e16163b6SPaolo Abeni return false; 2383e16163b6SPaolo Abeni } 2384e16163b6SPaolo Abeni return true; 2385e16163b6SPaolo Abeni } 2386e16163b6SPaolo Abeni 238750c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 238850c504a2SFlorian Westphal { 238950c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 239050c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 239150c504a2SFlorian Westphal 239250c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 239350c504a2SFlorian Westphal return; 239450c504a2SFlorian Westphal 239550c504a2SFlorian Westphal mptcp_token_destroy(msk); 239650c504a2SFlorian Westphal 239750c504a2SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 239850c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 239975e908c3SPaolo Abeni bool slow; 240050c504a2SFlorian Westphal 240175e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 240250c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 240350c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 240450c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 240550c504a2SFlorian Westphal } 240675e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 240750c504a2SFlorian Westphal } 240850c504a2SFlorian Westphal 240950c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 241050c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 241150c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 241250c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 241350c504a2SFlorian Westphal 241450c504a2SFlorian Westphal mptcp_close_wake_up(sk); 241550c504a2SFlorian Westphal } 241650c504a2SFlorian Westphal 24172948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 241880992017SPaolo Abeni { 24192948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2420caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 24213b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 24223b1d6210SPaolo Abeni size_t copied = 0; 24232948d0a1SPaolo Abeni struct sock *ssk; 24242948d0a1SPaolo Abeni int ret; 24252948d0a1SPaolo Abeni 2426b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 24273241a9c0SFlorian Westphal 24283241a9c0SFlorian Westphal /* first check ssk: need to kick "stale" logic */ 24293241a9c0SFlorian Westphal ssk = mptcp_subflow_get_retrans(msk); 24302948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 24316477dd39SMat Martineau if (!dfrag) { 24326477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 24336477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 24346477dd39SMat Martineau 24356477dd39SMat Martineau icsk->icsk_retransmits++; 24366477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 24376477dd39SMat Martineau mptcp_send_ack(msk); 24386477dd39SMat Martineau 24396477dd39SMat Martineau goto reset_timer; 24406477dd39SMat Martineau } 24416477dd39SMat Martineau 24423241a9c0SFlorian Westphal if (!mptcp_send_head(sk)) 24432948d0a1SPaolo Abeni return; 24443241a9c0SFlorian Westphal 24453241a9c0SFlorian Westphal goto reset_timer; 24466477dd39SMat Martineau } 24472948d0a1SPaolo Abeni 24482948d0a1SPaolo Abeni if (!ssk) 24492948d0a1SPaolo Abeni goto reset_timer; 24502948d0a1SPaolo Abeni 24512948d0a1SPaolo Abeni lock_sock(ssk); 24522948d0a1SPaolo Abeni 24532948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 24542948d0a1SPaolo Abeni info.sent = 0; 24554e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 24564e14867dSPaolo Abeni while (info.sent < info.limit) { 24572948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 24582948d0a1SPaolo Abeni if (ret <= 0) 24592948d0a1SPaolo Abeni break; 24602948d0a1SPaolo Abeni 24612948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 24622948d0a1SPaolo Abeni copied += ret; 24632948d0a1SPaolo Abeni info.sent += ret; 24642948d0a1SPaolo Abeni } 24654e14867dSPaolo Abeni if (copied) { 24664e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 24672948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 24682948d0a1SPaolo Abeni info.size_goal); 24690530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 24704e14867dSPaolo Abeni } 24712948d0a1SPaolo Abeni 24722948d0a1SPaolo Abeni release_sock(ssk); 24732948d0a1SPaolo Abeni 24742948d0a1SPaolo Abeni reset_timer: 24753241a9c0SFlorian Westphal mptcp_check_and_set_pending(sk); 24763241a9c0SFlorian Westphal 24772948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 24782948d0a1SPaolo Abeni mptcp_reset_timer(sk); 24792948d0a1SPaolo Abeni } 24802948d0a1SPaolo Abeni 248176a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout 248276a13b31SGeliang Tang * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 248376a13b31SGeliang Tang */ 248476a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 248576a13b31SGeliang Tang { 248676a13b31SGeliang Tang struct sock *sk = (struct sock *)msk; 248776a13b31SGeliang Tang unsigned long timeout, close_timeout; 248876a13b31SGeliang Tang 248976a13b31SGeliang Tang if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 249076a13b31SGeliang Tang return; 249176a13b31SGeliang Tang 249276a13b31SGeliang Tang close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 249376a13b31SGeliang Tang 249476a13b31SGeliang Tang /* the close timeout takes precedence on the fail one, and here at least one of 249576a13b31SGeliang Tang * them is active 249676a13b31SGeliang Tang */ 249776a13b31SGeliang Tang timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 249876a13b31SGeliang Tang 249976a13b31SGeliang Tang sk_reset_timer(sk, &sk->sk_timer, timeout); 250076a13b31SGeliang Tang } 250176a13b31SGeliang Tang 250249fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 250349fa1919SGeliang Tang { 250476a13b31SGeliang Tang struct sock *ssk = msk->first; 250549fa1919SGeliang Tang bool slow; 250649fa1919SGeliang Tang 250776a13b31SGeliang Tang if (!ssk) 250876a13b31SGeliang Tang return; 250976a13b31SGeliang Tang 251049fa1919SGeliang Tang pr_debug("MP_FAIL doesn't respond, reset the subflow"); 251149fa1919SGeliang Tang 251249fa1919SGeliang Tang slow = lock_sock_fast(ssk); 251349fa1919SGeliang Tang mptcp_subflow_reset(ssk); 251476a13b31SGeliang Tang WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 251549fa1919SGeliang Tang unlock_sock_fast(ssk, slow); 251676a13b31SGeliang Tang 251776a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 251849fa1919SGeliang Tang } 251949fa1919SGeliang Tang 25202948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 25212948d0a1SPaolo Abeni { 25222948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 25232948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 252476a13b31SGeliang Tang unsigned long fail_tout; 25252948d0a1SPaolo Abeni int state; 252680992017SPaolo Abeni 252780992017SPaolo Abeni lock_sock(sk); 2528e16163b6SPaolo Abeni state = sk->sk_state; 2529e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2530e16163b6SPaolo Abeni goto unlock; 2531e16163b6SPaolo Abeni 253243b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 253350c504a2SFlorian Westphal 253450c504a2SFlorian Westphal mptcp_check_fastclose(msk); 253550c504a2SFlorian Westphal 2536e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2537b416268bSFlorian Westphal 253859832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 253959832e24SFlorian Westphal mptcp_check_for_eof(msk); 254059832e24SFlorian Westphal 25416e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 254243b54c6eSMat Martineau mptcp_check_data_fin(sk); 254343b54c6eSMat Martineau 2544341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2545341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2546341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2547e16163b6SPaolo Abeni */ 2548e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2549341c6524SPaolo Abeni (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2550e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2551e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2552e16163b6SPaolo Abeni goto unlock; 2553e16163b6SPaolo Abeni } 2554e16163b6SPaolo Abeni 2555b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2556b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2557b263b0d7SFlorian Westphal 25582948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 25592948d0a1SPaolo Abeni __mptcp_retrans(sk); 25603b1d6210SPaolo Abeni 256176a13b31SGeliang Tang fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 256276a13b31SGeliang Tang if (fail_tout && time_after(jiffies, fail_tout)) 256349fa1919SGeliang Tang mptcp_mp_fail_no_response(msk); 256449fa1919SGeliang Tang 25653b1d6210SPaolo Abeni unlock: 256680992017SPaolo Abeni release_sock(sk); 256780992017SPaolo Abeni sock_put(sk); 256880992017SPaolo Abeni } 256980992017SPaolo Abeni 2570784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2571f870fa0bSMat Martineau { 2572cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2573cec37a6eSPeter Krystad 2574cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2575ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 257618b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 257780992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 257887952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2579ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2580f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 25816511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 2582ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 258333d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2584cec37a6eSPeter Krystad 25858ab183deSPaolo Abeni msk->first = NULL; 2586dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2587752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 25880530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, true); 25891e1d9d6fSPaolo Abeni msk->recovery = false; 25908ab183deSPaolo Abeni 25911b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 25921b1c7a0eSPeter Krystad 2593b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2594b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2595e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2596aa1fbd94SFlorian Westphal 2597f870fa0bSMat Martineau return 0; 2598f870fa0bSMat Martineau } 2599f870fa0bSMat Martineau 2600b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk) 2601784325e9SMatthieu Baerts { 260220b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2603b29fcfb5SPaolo Abeni 2604b29fcfb5SPaolo Abeni tcp_assign_congestion_control(sk); 2605b29fcfb5SPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2606b29fcfb5SPaolo Abeni 2607b29fcfb5SPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 2608b29fcfb5SPaolo Abeni tcp_cleanup_congestion_control(sk); 2609b29fcfb5SPaolo Abeni icsk->icsk_ca_ops = NULL; 2610b29fcfb5SPaolo Abeni } 2611b29fcfb5SPaolo Abeni 2612b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk) 2613b29fcfb5SPaolo Abeni { 2614fc518953SFlorian Westphal struct net *net = sock_net(sk); 2615fc518953SFlorian Westphal int ret; 261618b683bfSPaolo Abeni 2617b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2618b6c08380SGeliang Tang if (ret) 2619b6c08380SGeliang Tang return ret; 2620b6c08380SGeliang Tang 2621fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2622fc518953SFlorian Westphal return -ENOPROTOOPT; 2623fc518953SFlorian Westphal 2624fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2625fc518953SFlorian Westphal return -ENOMEM; 2626fc518953SFlorian Westphal 2627fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2628fa68018dSPaolo Abeni if (ret) 2629fa68018dSPaolo Abeni return ret; 2630fa68018dSPaolo Abeni 263120b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 263220b5759fSPaolo Abeni * propagate the correct value 263320b5759fSPaolo Abeni */ 2634b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 263520b5759fSPaolo Abeni 2636d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2637a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2638da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2639d027236cSPaolo Abeni 264018b683bfSPaolo Abeni return 0; 264118b683bfSPaolo Abeni } 264218b683bfSPaolo Abeni 264318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 264418b683bfSPaolo Abeni { 264518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 264618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 264718b683bfSPaolo Abeni 2648d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 264918b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2650d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2651784325e9SMatthieu Baerts } 2652784325e9SMatthieu Baerts 265380992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 265480992017SPaolo Abeni { 265580992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 265680992017SPaolo Abeni 2657b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2658e16163b6SPaolo Abeni __sock_put(sk); 265980992017SPaolo Abeni } 266080992017SPaolo Abeni 2661d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 266221498490SPeter Krystad { 266321498490SPeter Krystad lock_sock(ssk); 266421498490SPeter Krystad 266521498490SPeter Krystad switch (ssk->sk_state) { 266621498490SPeter Krystad case TCP_LISTEN: 266721498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 266821498490SPeter Krystad break; 2669df561f66SGustavo A. R. Silva fallthrough; 267021498490SPeter Krystad case TCP_SYN_SENT: 267121498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 267221498490SPeter Krystad break; 267321498490SPeter Krystad default: 267443b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 267543b54c6eSMat Martineau pr_debug("Fallback"); 267621498490SPeter Krystad ssk->sk_shutdown |= how; 267721498490SPeter Krystad tcp_shutdown(ssk, how); 267843b54c6eSMat Martineau } else { 267943b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 268043b54c6eSMat Martineau tcp_send_ack(ssk); 26816477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 26826477dd39SMat Martineau mptcp_reset_timer(sk); 268343b54c6eSMat Martineau } 268421498490SPeter Krystad break; 268521498490SPeter Krystad } 268621498490SPeter Krystad 268721498490SPeter Krystad release_sock(ssk); 268821498490SPeter Krystad } 268921498490SPeter Krystad 26906920b851SMat Martineau static const unsigned char new_state[16] = { 26916920b851SMat Martineau /* current state: new state: action: */ 26926920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 26936920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26946920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 26956920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26966920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 26976920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 26986920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 26996920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 27006920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 27016920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 27026920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 27036920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 27046920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 27056920b851SMat Martineau }; 27066920b851SMat Martineau 27076920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 27086920b851SMat Martineau { 27096920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 27106920b851SMat Martineau int ns = next & TCP_STATE_MASK; 27116920b851SMat Martineau 27126920b851SMat Martineau inet_sk_state_store(sk, ns); 27136920b851SMat Martineau 27146920b851SMat Martineau return next & TCP_ACTION_FIN; 27156920b851SMat Martineau } 27166920b851SMat Martineau 2717e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2718e16163b6SPaolo Abeni { 2719e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2720e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2721e16163b6SPaolo Abeni 2722e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2723e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2724e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2725e16163b6SPaolo Abeni 2726e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2727e16163b6SPaolo Abeni * skip this 2728e16163b6SPaolo Abeni */ 2729e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2730e16163b6SPaolo Abeni mptcp_send_head(sk)) 2731e16163b6SPaolo Abeni return; 2732e16163b6SPaolo Abeni 2733e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2734e16163b6SPaolo Abeni 273526aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 273626aa2314SPaolo Abeni * state now 273726aa2314SPaolo Abeni */ 273826aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 273958cd405bSPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 274026aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2741e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2742e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 274326aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 274426aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 274526aa2314SPaolo Abeni } 2746e16163b6SPaolo Abeni } 2747e16163b6SPaolo Abeni 2748e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2749e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2750e16163b6SPaolo Abeni 2751e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2752e16163b6SPaolo Abeni } 2753e16163b6SPaolo Abeni } 2754e16163b6SPaolo Abeni 2755e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2756e16163b6SPaolo Abeni { 2757e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2758e16163b6SPaolo Abeni 2759e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2760e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2761e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2762e16163b6SPaolo Abeni 2763e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2764e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2765e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2766e16163b6SPaolo Abeni 2767e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2768e16163b6SPaolo Abeni } 2769e16163b6SPaolo Abeni 2770e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2771f870fa0bSMat Martineau { 2772cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2773f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2774b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2775f870fa0bSMat Martineau 2776e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 27772c22c06cSFlorian Westphal 27783abc05d9SFlorian Westphal might_sleep(); 27793abc05d9SFlorian Westphal 27803e501490SPaolo Abeni /* join list will be eventually flushed (with rst) at sock lock release time*/ 2781b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2782b2c5b614SFlorian Westphal 2783bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2784e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2785e16163b6SPaolo Abeni msk->pm.status = 0; 2786b2c5b614SFlorian Westphal 2787b29fcfb5SPaolo Abeni /* clears msk->subflow, allowing the following loop to close 2788b29fcfb5SPaolo Abeni * even the initial subflow 2789b29fcfb5SPaolo Abeni */ 2790b29fcfb5SPaolo Abeni mptcp_dispose_initial_subflow(msk); 2791b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2792cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2793b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, 0); 2794f870fa0bSMat Martineau } 2795f870fa0bSMat Martineau 2796e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2797e16163b6SPaolo Abeni 27986511882cSPaolo Abeni WARN_ON_ONCE(msk->rmem_fwd_alloc); 279987952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2800e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2801e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2802aa1fbd94SFlorian Westphal 2803e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2804e16163b6SPaolo Abeni sock_put(sk); 2805e16163b6SPaolo Abeni } 2806e16163b6SPaolo Abeni 2807e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2808e16163b6SPaolo Abeni { 2809e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 281076a13b31SGeliang Tang struct mptcp_sock *msk = mptcp_sk(sk); 2811e16163b6SPaolo Abeni bool do_cancel_work = false; 2812e16163b6SPaolo Abeni 2813e16163b6SPaolo Abeni lock_sock(sk); 2814e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2815e16163b6SPaolo Abeni 2816e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2817e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2818e16163b6SPaolo Abeni goto cleanup; 2819e16163b6SPaolo Abeni } 2820e16163b6SPaolo Abeni 2821e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2822e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2823e16163b6SPaolo Abeni 2824e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2825e16163b6SPaolo Abeni 2826e16163b6SPaolo Abeni cleanup: 2827e16163b6SPaolo Abeni /* orphan all the subflows */ 2828e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 282976a13b31SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2830e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 283149054556SPaolo Abeni bool slow = lock_sock_fast_nested(ssk); 2832e16163b6SPaolo Abeni 283376a13b31SGeliang Tang /* since the close timeout takes precedence on the fail one, 283476a13b31SGeliang Tang * cancel the latter 283576a13b31SGeliang Tang */ 283676a13b31SGeliang Tang if (ssk == msk->first) 283776a13b31SGeliang Tang subflow->fail_tout = 0; 283876a13b31SGeliang Tang 2839e16163b6SPaolo Abeni sock_orphan(ssk); 2840e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2841e16163b6SPaolo Abeni } 2842e16163b6SPaolo Abeni sock_orphan(sk); 2843e16163b6SPaolo Abeni 2844e16163b6SPaolo Abeni sock_hold(sk); 2845e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2846b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 284776a13b31SGeliang Tang mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2848b29fcfb5SPaolo Abeni 2849e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2850e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2851e16163b6SPaolo Abeni do_cancel_work = true; 2852e16163b6SPaolo Abeni } else { 285376a13b31SGeliang Tang mptcp_reset_timeout(msk, 0); 2854e16163b6SPaolo Abeni } 2855e16163b6SPaolo Abeni release_sock(sk); 2856e16163b6SPaolo Abeni if (do_cancel_work) 285780992017SPaolo Abeni mptcp_cancel_work(sk); 2858b911c97cSFlorian Westphal 2859e16163b6SPaolo Abeni sock_put(sk); 2860f870fa0bSMat Martineau } 2861f870fa0bSMat Martineau 2862cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2863cf7da0d6SPeter Krystad { 2864cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2865cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2866cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2867cf7da0d6SPeter Krystad 2868cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2869cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2870cf7da0d6SPeter Krystad 2871cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2872cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2873cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2874cf7da0d6SPeter Krystad } 2875cf7da0d6SPeter Krystad #endif 2876cf7da0d6SPeter Krystad 2877cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2878cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2879cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2880cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2881cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2882cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2883cf7da0d6SPeter Krystad } 2884cf7da0d6SPeter Krystad 288518b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 288618b683bfSPaolo Abeni { 288776e2a55dSPaolo Abeni struct mptcp_subflow_context *subflow; 288876e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 288976e2a55dSPaolo Abeni 2890b29fcfb5SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2891b29fcfb5SPaolo Abeni 289213a9499eSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 289313a9499eSPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 289413a9499eSPaolo Abeni 2895b29fcfb5SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_FASTCLOSE); 289613a9499eSPaolo Abeni } 2897b29fcfb5SPaolo Abeni 2898bcf3cf93SGeliang Tang mptcp_stop_timer(sk); 2899b29fcfb5SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2900b29fcfb5SPaolo Abeni 2901b29fcfb5SPaolo Abeni if (mptcp_sk(sk)->token) 2902b29fcfb5SPaolo Abeni mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2903b29fcfb5SPaolo Abeni 2904b29fcfb5SPaolo Abeni mptcp_destroy_common(msk); 2905b29fcfb5SPaolo Abeni msk->last_snd = NULL; 2906e9d09bacSPaolo Abeni WRITE_ONCE(msk->flags, 0); 2907e9d09bacSPaolo Abeni msk->cb_flags = 0; 2908e9d09bacSPaolo Abeni msk->push_pending = 0; 2909b29fcfb5SPaolo Abeni msk->recovery = false; 2910b29fcfb5SPaolo Abeni msk->can_ack = false; 2911b29fcfb5SPaolo Abeni msk->fully_established = false; 2912b29fcfb5SPaolo Abeni msk->rcv_data_fin = false; 2913b29fcfb5SPaolo Abeni msk->snd_data_fin_enable = false; 2914b29fcfb5SPaolo Abeni msk->rcv_fastclose = false; 2915b29fcfb5SPaolo Abeni msk->use_64bit_ack = false; 2916b29fcfb5SPaolo Abeni WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2917b29fcfb5SPaolo Abeni mptcp_pm_data_reset(msk); 2918b29fcfb5SPaolo Abeni mptcp_ca_reset(sk); 2919b29fcfb5SPaolo Abeni 2920b29fcfb5SPaolo Abeni sk->sk_shutdown = 0; 2921b29fcfb5SPaolo Abeni sk_error_report(sk); 292242c556feSFlorian Westphal return 0; 292318b683bfSPaolo Abeni } 292418b683bfSPaolo Abeni 2925b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2926b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2927b0519de8SFlorian Westphal { 2928b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2929b0519de8SFlorian Westphal 2930b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2931b0519de8SFlorian Westphal } 2932b0519de8SFlorian Westphal #endif 2933b0519de8SFlorian Westphal 2934fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2935cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2936fca5c82cSPaolo Abeni struct request_sock *req) 2937b0519de8SFlorian Westphal { 293858b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2939b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 294058b09919SPaolo Abeni struct mptcp_sock *msk; 294158b09919SPaolo Abeni u64 ack_seq; 2942b0519de8SFlorian Westphal 2943b0519de8SFlorian Westphal if (!nsk) 2944b0519de8SFlorian Westphal return NULL; 2945b0519de8SFlorian Westphal 2946b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2947b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2948b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2949b0519de8SFlorian Westphal #endif 2950b0519de8SFlorian Westphal 295158b09919SPaolo Abeni __mptcp_init_sock(nsk); 295258b09919SPaolo Abeni 295358b09919SPaolo Abeni msk = mptcp_sk(nsk); 295458b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 295558b09919SPaolo Abeni msk->token = subflow_req->token; 295658b09919SPaolo Abeni msk->subflow = NULL; 2957b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 295874c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 295906251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 296058b09919SPaolo Abeni 296158b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2962eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 29637439d687SPaolo Abeni msk->snd_una = msk->write_seq; 29647439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2965df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 29666f8a612aSFlorian Westphal 296774c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 296858b09919SPaolo Abeni msk->can_ack = true; 2969cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 297058b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 297158b09919SPaolo Abeni ack_seq++; 2972917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2973f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 297458b09919SPaolo Abeni } 29757f20d5fcSPaolo Abeni 29765e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 29777f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 29787f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 29790c148460SPaolo Abeni 29800c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 298158b09919SPaolo Abeni bh_unlock_sock(nsk); 298258b09919SPaolo Abeni 298358b09919SPaolo Abeni /* keep a single reference */ 298458b09919SPaolo Abeni __sock_put(nsk); 2985b0519de8SFlorian Westphal return nsk; 2986b0519de8SFlorian Westphal } 2987b0519de8SFlorian Westphal 2988a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2989a6b118feSFlorian Westphal { 2990a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2991a6b118feSFlorian Westphal 2992a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2993a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2994a6b118feSFlorian Westphal 2995a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2996a6b118feSFlorian Westphal 2997a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2998a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2999a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 3000a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 3001a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 30026f8a612aSFlorian Westphal 30037439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3004a6b118feSFlorian Westphal } 3005a6b118feSFlorian Westphal 3006cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3007cf7da0d6SPeter Krystad bool kern) 3008cf7da0d6SPeter Krystad { 3009cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 3010cf7da0d6SPeter Krystad struct socket *listener; 3011cf7da0d6SPeter Krystad struct sock *newsk; 3012cf7da0d6SPeter Krystad 3013cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 3014cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 3015cf7da0d6SPeter Krystad *err = -EINVAL; 3016cf7da0d6SPeter Krystad return NULL; 3017cf7da0d6SPeter Krystad } 3018cf7da0d6SPeter Krystad 3019cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3020cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 3021cf7da0d6SPeter Krystad if (!newsk) 3022cf7da0d6SPeter Krystad return NULL; 3023cf7da0d6SPeter Krystad 3024cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3025cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 3026cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 3027cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 3028cf7da0d6SPeter Krystad 3029cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 303058b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 303158b09919SPaolo Abeni 303258b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 303358b09919SPaolo Abeni * subflow_syn_recv_sock() 303458b09919SPaolo Abeni */ 303558b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 303658b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 3037d6692b3bSFlorian Westphal goto out; 303858b09919SPaolo Abeni } 303958b09919SPaolo Abeni 304058b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 304158b09919SPaolo Abeni sock_hold(new_mptcp_sock); 3042cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 30430397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3044fc518953SFlorian Westphal } else { 3045fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 3046fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3047cf7da0d6SPeter Krystad } 3048cf7da0d6SPeter Krystad 3049d6692b3bSFlorian Westphal out: 3050d6692b3bSFlorian Westphal newsk->sk_kern_sock = kern; 3051cf7da0d6SPeter Krystad return newsk; 3052cf7da0d6SPeter Krystad } 3053cf7da0d6SPeter Krystad 30545c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 30555c8c1640SGeliang Tang { 305687952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 305787952603SPaolo Abeni 30586e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 30596e628cd3SPaolo Abeni 306087952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3061b29fcfb5SPaolo Abeni mptcp_data_lock(sk); 306287952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 30636511882cSPaolo Abeni __skb_queue_purge(&sk->sk_receive_queue); 30645c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 3065b29fcfb5SPaolo Abeni mptcp_data_unlock(sk); 30666511882cSPaolo Abeni 30676511882cSPaolo Abeni /* move all the rx fwd alloc into the sk_mem_reclaim_final in 30686511882cSPaolo Abeni * inet_sock_destruct() will dispose it 30696511882cSPaolo Abeni */ 30706511882cSPaolo Abeni sk->sk_forward_alloc += msk->rmem_fwd_alloc; 30716511882cSPaolo Abeni msk->rmem_fwd_alloc = 0; 30725c8c1640SGeliang Tang mptcp_token_destroy(msk); 30735c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 30744638de5aSKishen Maloor mptcp_free_local_addr_list(msk); 30755c8c1640SGeliang Tang } 30765c8c1640SGeliang Tang 307779c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 307879c0949eSPeter Krystad { 3079c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3080c9fd9c5fSFlorian Westphal 30815c8c1640SGeliang Tang mptcp_destroy_common(msk); 3082d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 308379c0949eSPeter Krystad } 308479c0949eSPeter Krystad 30856e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 30866e628cd3SPaolo Abeni { 30876e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 30886e628cd3SPaolo Abeni __mptcp_clean_una(sk); 30896e628cd3SPaolo Abeni else 3090e9d09bacSPaolo Abeni __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 30916e628cd3SPaolo Abeni 30926e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 30936e628cd3SPaolo Abeni mptcp_schedule_work(sk); 30946e628cd3SPaolo Abeni } 30956e628cd3SPaolo Abeni 3096219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 30976e628cd3SPaolo Abeni { 30986e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 30996e628cd3SPaolo Abeni return; 31006e628cd3SPaolo Abeni 310140dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 3102b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 3103b19bc294SPaolo Abeni 3104b19bc294SPaolo Abeni if (xmit_ssk == ssk) 31056e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3106b19bc294SPaolo Abeni else if (xmit_ssk) 3107bcd97734SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND); 310840dc9416SPaolo Abeni } else { 3109e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 31106e628cd3SPaolo Abeni } 311140dc9416SPaolo Abeni } 31126e628cd3SPaolo Abeni 3113e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3114e9d09bacSPaolo Abeni BIT(MPTCP_RETRANSMIT) | \ 3115e9d09bacSPaolo Abeni BIT(MPTCP_FLUSH_JOIN_LIST)) 3116e9d09bacSPaolo Abeni 3117e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 311814c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 3119e9d09bacSPaolo Abeni __must_hold(&sk->sk_lock.slock) 312014c441b5SPaolo Abeni { 3121e9d09bacSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 31222d6f5a2bSPaolo Abeni 3123e9d09bacSPaolo Abeni for (;;) { 3124e9d09bacSPaolo Abeni unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3125e9d09bacSPaolo Abeni msk->push_pending; 3126c2e6048fSPaolo Abeni if (!flags) 3127c2e6048fSPaolo Abeni break; 3128c2e6048fSPaolo Abeni 3129c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 31306e628cd3SPaolo Abeni * 31316e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 31326e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 31336e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 31346e628cd3SPaolo Abeni * the subflow socket lock 31356e628cd3SPaolo Abeni */ 3136e9d09bacSPaolo Abeni msk->push_pending = 0; 3137e9d09bacSPaolo Abeni msk->cb_flags &= ~flags; 31386e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 31393e501490SPaolo Abeni if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 31403e501490SPaolo Abeni __mptcp_flush_join_list(sk); 31412e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3142c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 31432d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 31442d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3145c2e6048fSPaolo Abeni 3146c2e6048fSPaolo Abeni cond_resched(); 31476e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 31486e628cd3SPaolo Abeni } 3149c2e6048fSPaolo Abeni 315065a569b0SPaolo Abeni if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 315165a569b0SPaolo Abeni __mptcp_clean_una_wakeup(sk); 315265a569b0SPaolo Abeni if (unlikely(&msk->cb_flags)) { 3153490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 315465a569b0SPaolo Abeni * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3155490274b4SPaolo Abeni */ 3156e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3157490274b4SPaolo Abeni __mptcp_set_connected(sk); 3158e9d09bacSPaolo Abeni if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 315915cc1045SPaolo Abeni __mptcp_error_report(sk); 316043f5b111SPaolo Abeni if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 316143f5b111SPaolo Abeni msk->last_snd = NULL; 316265a569b0SPaolo Abeni } 31636e628cd3SPaolo Abeni 316487952603SPaolo Abeni __mptcp_update_rmem(sk); 316514c441b5SPaolo Abeni } 316614c441b5SPaolo Abeni 3167bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3168bcd97734SPaolo Abeni * TCP can't schedule delack timer before the subflow is fully established. 3169bcd97734SPaolo Abeni * MPTCP uses the delack timer to do 3rd ack retransmissions 3170bcd97734SPaolo Abeni */ 3171bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk) 3172bcd97734SPaolo Abeni { 3173bcd97734SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(ssk); 3174bcd97734SPaolo Abeni struct tcp_sock *tp = tcp_sk(ssk); 3175bcd97734SPaolo Abeni unsigned long timeout; 3176bcd97734SPaolo Abeni 3177bcd97734SPaolo Abeni if (mptcp_subflow_ctx(ssk)->fully_established) 3178bcd97734SPaolo Abeni return; 3179bcd97734SPaolo Abeni 3180bcd97734SPaolo Abeni /* reschedule with a timeout above RTT, as we must look only for drop */ 3181bcd97734SPaolo Abeni if (tp->srtt_us) 3182bcd97734SPaolo Abeni timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3183bcd97734SPaolo Abeni else 3184bcd97734SPaolo Abeni timeout = TCP_TIMEOUT_INIT; 3185bcd97734SPaolo Abeni timeout += jiffies; 3186bcd97734SPaolo Abeni 3187bcd97734SPaolo Abeni WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3188bcd97734SPaolo Abeni icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3189bcd97734SPaolo Abeni icsk->icsk_ack.timeout = timeout; 3190bcd97734SPaolo Abeni sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3191bcd97734SPaolo Abeni } 3192bcd97734SPaolo Abeni 3193b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3194b19bc294SPaolo Abeni { 3195b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3196b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3197b19bc294SPaolo Abeni 3198bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3199b19bc294SPaolo Abeni mptcp_data_lock(sk); 3200b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3201b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3202b19bc294SPaolo Abeni else 3203e9d09bacSPaolo Abeni __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3204b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3205bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3206bcd97734SPaolo Abeni } 3207bcd97734SPaolo Abeni if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3208bcd97734SPaolo Abeni schedule_3rdack_retransmission(ssk); 3209bcd97734SPaolo Abeni mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3210bcd97734SPaolo Abeni } 3211b19bc294SPaolo Abeni } 3212b19bc294SPaolo Abeni 32132c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 32142c5ebd00SPaolo Abeni { 32152c5ebd00SPaolo Abeni /* should never be called, 32162c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 32172c5ebd00SPaolo Abeni */ 32182c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 32192c5ebd00SPaolo Abeni return 0; 32202c5ebd00SPaolo Abeni } 32212c5ebd00SPaolo Abeni 32222c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 32232c5ebd00SPaolo Abeni { 32242c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 32252c5ebd00SPaolo Abeni } 32262c5ebd00SPaolo Abeni 3227cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3228f870fa0bSMat Martineau { 3229f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3230cec37a6eSPeter Krystad struct socket *ssock; 3231f870fa0bSMat Martineau 3232cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3233cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3234cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3235cec37a6eSPeter Krystad return -EINVAL; 3236f870fa0bSMat Martineau 3237cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3238cec37a6eSPeter Krystad } 3239f870fa0bSMat Martineau 3240cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3241cec37a6eSPeter Krystad { 3242cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3243cec37a6eSPeter Krystad struct mptcp_sock *msk; 3244cec37a6eSPeter Krystad struct sock *sk; 32456d0060f6SMat Martineau u64 ack_seq; 3246f870fa0bSMat Martineau 3247cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3248cec37a6eSPeter Krystad sk = subflow->conn; 3249cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3250cec37a6eSPeter Krystad 3251648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3252648ef4b8SMat Martineau 32536d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 32546d0060f6SMat Martineau ack_seq++; 3255648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3256648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 32576d0060f6SMat Martineau 3258cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3259cec37a6eSPeter Krystad * accessing the field below 3260cec37a6eSPeter Krystad */ 3261cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3262cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 32636d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3264eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 32656d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3266d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 32677439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 3268f3589be0SPaolo Abeni atomic64_set(&msk->rcv_wnd_sent, ack_seq); 32691b1c7a0eSPeter Krystad 32706c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3271a6b118feSFlorian Westphal 3272a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3273f870fa0bSMat Martineau } 3274f870fa0bSMat Martineau 3275866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3276cf7da0d6SPeter Krystad { 3277cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3278cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3279cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3280cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3281cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3282cf7da0d6SPeter Krystad } 3283cf7da0d6SPeter Krystad 3284e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3285f296234cSPeter Krystad { 3286e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3287f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3288f296234cSPeter Krystad struct sock *parent = (void *)msk; 32893e501490SPaolo Abeni bool ret = true; 3290f296234cSPeter Krystad 3291f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3292f296234cSPeter Krystad 3293f296234cSPeter Krystad /* mptcp socket already closing? */ 3294dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3295dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3296f296234cSPeter Krystad return false; 3297dc87efdbSFlorian Westphal } 3298f296234cSPeter Krystad 329970c708e8SKishen Maloor if (!list_empty(&subflow->node)) 3300b911c97cSFlorian Westphal goto out; 3301f296234cSPeter Krystad 33023e501490SPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 33033e501490SPaolo Abeni goto err_prohibited; 330410f6d46cSPaolo Abeni 33053e501490SPaolo Abeni /* active connections are already on conn_list. 33063e501490SPaolo Abeni * If we can't acquire msk socket lock here, let the release callback 33073e501490SPaolo Abeni * handle it 330810f6d46cSPaolo Abeni */ 33093e501490SPaolo Abeni mptcp_data_lock(parent); 33103e501490SPaolo Abeni if (!sock_owned_by_user(parent)) { 33113e501490SPaolo Abeni ret = __mptcp_finish_join(msk, ssk); 33123e501490SPaolo Abeni if (ret) { 3313e16163b6SPaolo Abeni sock_hold(ssk); 33143e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->conn_list); 3315e16163b6SPaolo Abeni } 33163e501490SPaolo Abeni } else { 33173e501490SPaolo Abeni sock_hold(ssk); 33183e501490SPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3319e9d09bacSPaolo Abeni __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 33203e501490SPaolo Abeni } 33213e501490SPaolo Abeni mptcp_data_unlock(parent); 33223e501490SPaolo Abeni 3323dc87efdbSFlorian Westphal if (!ret) { 33243e501490SPaolo Abeni err_prohibited: 3325dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 332610f6d46cSPaolo Abeni return false; 3327dc87efdbSFlorian Westphal } 332810f6d46cSPaolo Abeni 3329917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 33300530020aSGeliang Tang WRITE_ONCE(msk->allow_infinite_fallback, false); 33313e501490SPaolo Abeni 3332b911c97cSFlorian Westphal out: 3333b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 333410f6d46cSPaolo Abeni return true; 3335f296234cSPeter Krystad } 3336f296234cSPeter Krystad 333776e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 333876e2a55dSPaolo Abeni { 333976e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 334076e2a55dSPaolo Abeni 334176e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 334276e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 334376e2a55dSPaolo Abeni } 334476e2a55dSPaolo Abeni 33456511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk) 33466511882cSPaolo Abeni { 33476511882cSPaolo Abeni return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 33486511882cSPaolo Abeni } 33496511882cSPaolo Abeni 3350644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3351644807e3SFlorian Westphal { 3352644807e3SFlorian Westphal const struct sock *sk = (void *)msk; 3353644807e3SFlorian Westphal u64 delta; 3354644807e3SFlorian Westphal 3355644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3356644807e3SFlorian Westphal return -EINVAL; 3357644807e3SFlorian Westphal 3358644807e3SFlorian Westphal if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3359644807e3SFlorian Westphal return 0; 3360644807e3SFlorian Westphal 3361644807e3SFlorian Westphal delta = msk->write_seq - v; 336207c2c7a3SPaolo Abeni if (__mptcp_check_fallback(msk) && msk->first) { 336307c2c7a3SPaolo Abeni struct tcp_sock *tp = tcp_sk(msk->first); 336407c2c7a3SPaolo Abeni 336507c2c7a3SPaolo Abeni /* the first subflow is disconnected after close - see 336607c2c7a3SPaolo Abeni * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 336707c2c7a3SPaolo Abeni * so ignore that status, too. 336807c2c7a3SPaolo Abeni */ 336907c2c7a3SPaolo Abeni if (!((1 << msk->first->sk_state) & 337007c2c7a3SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 337107c2c7a3SPaolo Abeni delta += READ_ONCE(tp->write_seq) - tp->snd_una; 337207c2c7a3SPaolo Abeni } 3373644807e3SFlorian Westphal if (delta > INT_MAX) 3374644807e3SFlorian Westphal delta = INT_MAX; 3375644807e3SFlorian Westphal 3376644807e3SFlorian Westphal return (int)delta; 3377644807e3SFlorian Westphal } 3378644807e3SFlorian Westphal 3379644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3380644807e3SFlorian Westphal { 3381644807e3SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 3382644807e3SFlorian Westphal bool slow; 3383644807e3SFlorian Westphal int answ; 3384644807e3SFlorian Westphal 3385644807e3SFlorian Westphal switch (cmd) { 3386644807e3SFlorian Westphal case SIOCINQ: 3387644807e3SFlorian Westphal if (sk->sk_state == TCP_LISTEN) 3388644807e3SFlorian Westphal return -EINVAL; 3389644807e3SFlorian Westphal 3390644807e3SFlorian Westphal lock_sock(sk); 3391644807e3SFlorian Westphal __mptcp_move_skbs(msk); 3392644807e3SFlorian Westphal answ = mptcp_inq_hint(sk); 3393644807e3SFlorian Westphal release_sock(sk); 3394644807e3SFlorian Westphal break; 3395644807e3SFlorian Westphal case SIOCOUTQ: 3396644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3397644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3398644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3399644807e3SFlorian Westphal break; 3400644807e3SFlorian Westphal case SIOCOUTQNSD: 3401644807e3SFlorian Westphal slow = lock_sock_fast(sk); 3402644807e3SFlorian Westphal answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3403644807e3SFlorian Westphal unlock_sock_fast(sk, slow); 3404644807e3SFlorian Westphal break; 3405644807e3SFlorian Westphal default: 3406644807e3SFlorian Westphal return -ENOIOCTLCMD; 3407644807e3SFlorian Westphal } 3408644807e3SFlorian Westphal 3409644807e3SFlorian Westphal return put_user(answ, (int __user *)arg); 3410644807e3SFlorian Westphal } 3411644807e3SFlorian Westphal 3412f870fa0bSMat Martineau static struct proto mptcp_prot = { 3413f870fa0bSMat Martineau .name = "MPTCP", 3414f870fa0bSMat Martineau .owner = THIS_MODULE, 3415f870fa0bSMat Martineau .init = mptcp_init_sock, 341618b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3417f870fa0bSMat Martineau .close = mptcp_close, 3418cf7da0d6SPeter Krystad .accept = mptcp_accept, 3419717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3420717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 342176e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 342279c0949eSPeter Krystad .destroy = mptcp_destroy, 3423f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3424644807e3SFlorian Westphal .ioctl = mptcp_ioctl, 3425f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 342614c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 34272c5ebd00SPaolo Abeni .hash = mptcp_hash, 34282c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3429cec37a6eSPeter Krystad .get_port = mptcp_get_port, 34306511882cSPaolo Abeni .forward_alloc_get = mptcp_forward_alloc_get, 3431d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 34320defbb0aSEric Dumazet 3433d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 34340defbb0aSEric Dumazet .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 34350defbb0aSEric Dumazet 3436d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3437d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3438989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3439d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3440f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 34412c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3442f870fa0bSMat Martineau .no_autobind = true, 3443f870fa0bSMat Martineau }; 3444f870fa0bSMat Martineau 34452303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 34462303f994SPeter Krystad { 34472303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 34482303f994SPeter Krystad struct socket *ssock; 3449cf7da0d6SPeter Krystad int err; 34502303f994SPeter Krystad 34512303f994SPeter Krystad lock_sock(sock->sk); 3452fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3453fa68018dSPaolo Abeni if (!ssock) { 3454fa68018dSPaolo Abeni err = -EINVAL; 34552303f994SPeter Krystad goto unlock; 34562303f994SPeter Krystad } 34572303f994SPeter Krystad 34582303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3459cf7da0d6SPeter Krystad if (!err) 3460cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 34612303f994SPeter Krystad 34622303f994SPeter Krystad unlock: 34632303f994SPeter Krystad release_sock(sock->sk); 34642303f994SPeter Krystad return err; 34652303f994SPeter Krystad } 34662303f994SPeter Krystad 34670235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 34680235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 34690235d075SPaolo Abeni { 34700235d075SPaolo Abeni subflow->request_mptcp = 0; 34710235d075SPaolo Abeni __mptcp_do_fallback(msk); 34720235d075SPaolo Abeni } 34730235d075SPaolo Abeni 34742303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 34752303f994SPeter Krystad int addr_len, int flags) 34762303f994SPeter Krystad { 34772303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 34782c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 34792303f994SPeter Krystad struct socket *ssock; 34803d1d6d66SPaolo Abeni int err = -EINVAL; 34812303f994SPeter Krystad 34822303f994SPeter Krystad lock_sock(sock->sk); 34833d1d6d66SPaolo Abeni if (uaddr) { 34843d1d6d66SPaolo Abeni if (addr_len < sizeof(uaddr->sa_family)) 34853d1d6d66SPaolo Abeni goto unlock; 34863d1d6d66SPaolo Abeni 34873d1d6d66SPaolo Abeni if (uaddr->sa_family == AF_UNSPEC) { 34883d1d6d66SPaolo Abeni err = mptcp_disconnect(sock->sk, flags); 34893d1d6d66SPaolo Abeni sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; 34903d1d6d66SPaolo Abeni goto unlock; 34913d1d6d66SPaolo Abeni } 34923d1d6d66SPaolo Abeni } 34933d1d6d66SPaolo Abeni 349441be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 349541be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 349641be81a8SPaolo Abeni * cope with that 349741be81a8SPaolo Abeni */ 349841be81a8SPaolo Abeni ssock = msk->subflow; 349941be81a8SPaolo Abeni goto do_connect; 350041be81a8SPaolo Abeni } 350141be81a8SPaolo Abeni 3502fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 35033d1d6d66SPaolo Abeni if (!ssock) 35042303f994SPeter Krystad goto unlock; 35052303f994SPeter Krystad 3506fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3507fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 35082c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3509cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3510cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3511cf7da0d6SPeter Krystad * TCP option space. 3512cf7da0d6SPeter Krystad */ 3513cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 35140235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3515cf7da0d6SPeter Krystad #endif 3516a16195e3SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3517a16195e3SPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 35180235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3519a16195e3SPaolo Abeni } 35205695eb88SPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 35215695eb88SPaolo Abeni MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3522cf7da0d6SPeter Krystad 352341be81a8SPaolo Abeni do_connect: 35242303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 352541be81a8SPaolo Abeni sock->state = ssock->state; 352641be81a8SPaolo Abeni 352741be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 352841be81a8SPaolo Abeni * subflow_finish_connect() 352941be81a8SPaolo Abeni */ 3530367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3531cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 353241be81a8SPaolo Abeni else 353341be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 35342303f994SPeter Krystad 35352303f994SPeter Krystad unlock: 35362303f994SPeter Krystad release_sock(sock->sk); 35372303f994SPeter Krystad return err; 35382303f994SPeter Krystad } 35392303f994SPeter Krystad 3540cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3541cf7da0d6SPeter Krystad { 3542cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3543cf7da0d6SPeter Krystad struct socket *ssock; 3544cf7da0d6SPeter Krystad int err; 3545cf7da0d6SPeter Krystad 3546cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3547cf7da0d6SPeter Krystad 3548cf7da0d6SPeter Krystad lock_sock(sock->sk); 3549fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3550fa68018dSPaolo Abeni if (!ssock) { 3551fa68018dSPaolo Abeni err = -EINVAL; 3552cf7da0d6SPeter Krystad goto unlock; 3553cf7da0d6SPeter Krystad } 3554cf7da0d6SPeter Krystad 3555fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3556fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 35575e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 35585e20087dSFlorian Westphal 3559cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3560cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3561cf7da0d6SPeter Krystad if (!err) 3562cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3563cf7da0d6SPeter Krystad 3564cf7da0d6SPeter Krystad unlock: 3565cf7da0d6SPeter Krystad release_sock(sock->sk); 3566cf7da0d6SPeter Krystad return err; 3567cf7da0d6SPeter Krystad } 3568cf7da0d6SPeter Krystad 3569cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3570cf7da0d6SPeter Krystad int flags, bool kern) 3571cf7da0d6SPeter Krystad { 3572cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3573cf7da0d6SPeter Krystad struct socket *ssock; 3574cf7da0d6SPeter Krystad int err; 3575cf7da0d6SPeter Krystad 3576cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3577cf7da0d6SPeter Krystad 3578cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3579cf7da0d6SPeter Krystad if (!ssock) 358071ba088cSPaolo Abeni return -EINVAL; 3581cf7da0d6SPeter Krystad 3582cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3583d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3584cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3585cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 35860397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 35870397c6d8SPaolo Abeni 35884d54cc32SFlorian Westphal lock_sock(newsk); 35895b950ff4SPaolo Abeni 35905b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 35915b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 35925b950ff4SPaolo Abeni * we can notify it, if needed. 3593c8fe62f0SFlorian Westphal * 3594c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3595c8fe62f0SFlorian Westphal * the refcnt is still at least one. 35965b950ff4SPaolo Abeni */ 35975b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 35985b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 35995b950ff4SPaolo Abeni sock_hold(msk->first); 36005b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 36016c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 36025b950ff4SPaolo Abeni 36030397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 36040397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 36055cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3606cf7da0d6SPeter Krystad 3607cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3608cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3609cf7da0d6SPeter Krystad */ 3610190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3611cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3612cf7da0d6SPeter Krystad 3613cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3614cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3615cf7da0d6SPeter Krystad } 36164d54cc32SFlorian Westphal release_sock(newsk); 3617cf7da0d6SPeter Krystad } 3618cf7da0d6SPeter Krystad 3619cf7da0d6SPeter Krystad return err; 3620cf7da0d6SPeter Krystad } 3621cf7da0d6SPeter Krystad 36228a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 36238a05661bSPaolo Abeni { 3624612f71d7SPaolo Abeni /* Concurrent splices from sk_receive_queue into receive_queue will 3625612f71d7SPaolo Abeni * always show at least one non-empty queue when checked in this order. 3626612f71d7SPaolo Abeni */ 3627612f71d7SPaolo Abeni if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 3628612f71d7SPaolo Abeni skb_queue_empty_lockless(&msk->receive_queue)) 3629612f71d7SPaolo Abeni return 0; 3630612f71d7SPaolo Abeni 3631612f71d7SPaolo Abeni return EPOLLIN | EPOLLRDNORM; 36328a05661bSPaolo Abeni } 36338a05661bSPaolo Abeni 36348edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 36358edf0864SFlorian Westphal { 36368edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 36378edf0864SFlorian Westphal 36388edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3639dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 36408edf0864SFlorian Westphal 36418edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 36428edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 36438edf0864SFlorian Westphal 36445cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 36456e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 36466e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 36476e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 36488edf0864SFlorian Westphal 36496e628cd3SPaolo Abeni return 0; 36508edf0864SFlorian Westphal } 36518edf0864SFlorian Westphal 36522303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 36532303f994SPeter Krystad struct poll_table_struct *wait) 36542303f994SPeter Krystad { 36551891c4a0SFlorian Westphal struct sock *sk = sock->sk; 36568ab183deSPaolo Abeni struct mptcp_sock *msk; 36572303f994SPeter Krystad __poll_t mask = 0; 36588a05661bSPaolo Abeni int state; 36592303f994SPeter Krystad 36601891c4a0SFlorian Westphal msk = mptcp_sk(sk); 36611891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 36621891c4a0SFlorian Westphal 36638a05661bSPaolo Abeni state = inet_sk_state_load(sk); 36646719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 366571ba088cSPaolo Abeni if (state == TCP_LISTEN) { 366671ba088cSPaolo Abeni if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk)) 366771ba088cSPaolo Abeni return 0; 366871ba088cSPaolo Abeni 366971ba088cSPaolo Abeni return inet_csk_listen_poll(msk->subflow->sk); 367071ba088cSPaolo Abeni } 36718a05661bSPaolo Abeni 36728a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 36738a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 36748edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 36758a05661bSPaolo Abeni } 3676dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3677dd913410SPaolo Abeni mask |= EPOLLHUP; 36781891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 36791891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 36801891c4a0SFlorian Westphal 368115cc1045SPaolo Abeni /* This barrier is coupled with smp_wmb() in tcp_reset() */ 368215cc1045SPaolo Abeni smp_rmb(); 368315cc1045SPaolo Abeni if (sk->sk_err) 368415cc1045SPaolo Abeni mask |= EPOLLERR; 368515cc1045SPaolo Abeni 36862303f994SPeter Krystad return mask; 36872303f994SPeter Krystad } 36882303f994SPeter Krystad 3689e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3690e42f1ac6SFlorian Westphal .family = PF_INET, 3691e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 36920a3cc579SPaolo Abeni .release = inet_release, 3693e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3694e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3695e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3696e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3697d2f77c53SPaolo Abeni .getname = inet_getname, 3698e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3699e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3700e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3701e42f1ac6SFlorian Westphal .listen = mptcp_listen, 370276e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3703e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3704e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3705e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3706e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3707e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3708e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3709e42f1ac6SFlorian Westphal }; 37102303f994SPeter Krystad 3711f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3712f870fa0bSMat Martineau .type = SOCK_STREAM, 3713f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3714f870fa0bSMat Martineau .prot = &mptcp_prot, 37152303f994SPeter Krystad .ops = &mptcp_stream_ops, 37162303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3717f870fa0bSMat Martineau }; 3718f870fa0bSMat Martineau 3719b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3720b19bc294SPaolo Abeni { 3721b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3722b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3723b19bc294SPaolo Abeni int work_done = 0; 3724b19bc294SPaolo Abeni 3725b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3726b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3727b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3728b19bc294SPaolo Abeni 3729b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3730b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3731b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3732b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3733b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3734b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3735b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3736b19bc294SPaolo Abeni * CPU that scheduled it. 3737b19bc294SPaolo Abeni */ 3738b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3739b19bc294SPaolo Abeni sock_put(ssk); 3740b19bc294SPaolo Abeni 3741b19bc294SPaolo Abeni if (++work_done == budget) 3742b19bc294SPaolo Abeni return budget; 3743b19bc294SPaolo Abeni } 3744b19bc294SPaolo Abeni 3745b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3746b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3747b19bc294SPaolo Abeni */ 3748b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3749b19bc294SPaolo Abeni return work_done; 3750b19bc294SPaolo Abeni } 3751b19bc294SPaolo Abeni 3752d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3753f870fa0bSMat Martineau { 3754b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3755b19bc294SPaolo Abeni int cpu; 3756b19bc294SPaolo Abeni 37572303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 37582303f994SPeter Krystad 3759d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3760d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3761d027236cSPaolo Abeni 3762b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3763b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3764b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3765b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 376616d083e2SJakub Kicinski netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 376716d083e2SJakub Kicinski mptcp_napi_poll); 3768b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3769b19bc294SPaolo Abeni } 3770b19bc294SPaolo Abeni 37712303f994SPeter Krystad mptcp_subflow_init(); 37721b1c7a0eSPeter Krystad mptcp_pm_init(); 37732c5ebd00SPaolo Abeni mptcp_token_init(); 37742303f994SPeter Krystad 3775f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3776f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3777f870fa0bSMat Martineau 3778f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 37796771bfd9SFlorian Westphal 37806771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3781f870fa0bSMat Martineau } 3782f870fa0bSMat Martineau 3783f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3784e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3785e42f1ac6SFlorian Westphal .family = PF_INET6, 3786e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 37870a3cc579SPaolo Abeni .release = inet6_release, 3788e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3789e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3790e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3791e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3792d2f77c53SPaolo Abeni .getname = inet6_getname, 3793e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3794e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3795e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3796e42f1ac6SFlorian Westphal .listen = mptcp_listen, 379776e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3798e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3799e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3800e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3801e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3802e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3803e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3804e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 38053986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3806e42f1ac6SFlorian Westphal #endif 3807e42f1ac6SFlorian Westphal }; 3808e42f1ac6SFlorian Westphal 3809f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3810f870fa0bSMat Martineau 381179c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 381279c0949eSPeter Krystad { 381379c0949eSPeter Krystad mptcp_destroy(sk); 381479c0949eSPeter Krystad inet6_destroy_sock(sk); 381579c0949eSPeter Krystad } 381679c0949eSPeter Krystad 3817f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3818f870fa0bSMat Martineau .type = SOCK_STREAM, 3819f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3820f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 38212303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3822f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3823f870fa0bSMat Martineau }; 3824f870fa0bSMat Martineau 3825d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3826f870fa0bSMat Martineau { 3827f870fa0bSMat Martineau int err; 3828f870fa0bSMat Martineau 3829f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3830f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3831f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 383279c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3833b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3834f870fa0bSMat Martineau 3835f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3836f870fa0bSMat Martineau if (err) 3837f870fa0bSMat Martineau return err; 3838f870fa0bSMat Martineau 3839f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3840f870fa0bSMat Martineau if (err) 3841f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3842f870fa0bSMat Martineau 3843f870fa0bSMat Martineau return err; 3844f870fa0bSMat Martineau } 3845f870fa0bSMat Martineau #endif 3846