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> 25f870fa0bSMat Martineau #include "protocol.h" 26fc518953SFlorian Westphal #include "mib.h" 27f870fa0bSMat Martineau 28e10a9892SGeliang Tang #define CREATE_TRACE_POINTS 29e10a9892SGeliang Tang #include <trace/events/mptcp.h> 30e10a9892SGeliang Tang 31b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 32b0519de8SFlorian Westphal struct mptcp6_sock { 33b0519de8SFlorian Westphal struct mptcp_sock msk; 34b0519de8SFlorian Westphal struct ipv6_pinfo np; 35b0519de8SFlorian Westphal }; 36b0519de8SFlorian Westphal #endif 37b0519de8SFlorian Westphal 386771bfd9SFlorian Westphal struct mptcp_skb_cb { 39ab174ad8SPaolo Abeni u64 map_seq; 40ab174ad8SPaolo Abeni u64 end_seq; 416771bfd9SFlorian Westphal u32 offset; 42b7f653b2SFlorian Westphal u8 has_rxtstamp:1; 436771bfd9SFlorian Westphal }; 446771bfd9SFlorian Westphal 456771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 466771bfd9SFlorian Westphal 47b7f653b2SFlorian Westphal enum { 48b7f653b2SFlorian Westphal MPTCP_CMSG_TS = BIT(0), 49b7f653b2SFlorian Westphal }; 50b7f653b2SFlorian Westphal 51d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated; 52d027236cSPaolo Abeni 53e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 54d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 55e16163b6SPaolo Abeni 56b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 57b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 58b19bc294SPaolo Abeni 592303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 602303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 612303f994SPeter Krystad * Otherwise return NULL. 622303f994SPeter Krystad */ 631729cf18SGeliang Tang struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 642303f994SPeter Krystad { 65d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 662303f994SPeter Krystad return NULL; 672303f994SPeter Krystad 682303f994SPeter Krystad return msk->subflow; 692303f994SPeter Krystad } 702303f994SPeter Krystad 716f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 726f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 736f8a612aSFlorian Westphal { 747439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 756f8a612aSFlorian Westphal } 766f8a612aSFlorian Westphal 77d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 780b4f33deSFlorian Westphal { 790b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 800b4f33deSFlorian Westphal 810b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 820b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 830b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 840b4f33deSFlorian Westphal * not an mptcp one. 850b4f33deSFlorian Westphal * 860b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 870b4f33deSFlorian Westphal * bypass mptcp. 880b4f33deSFlorian Westphal */ 890b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 90d2f77c53SPaolo Abeni return true; 910b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 920b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 930b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 94d2f77c53SPaolo Abeni return true; 950b4f33deSFlorian Westphal #endif 960b4f33deSFlorian Westphal } 970b4f33deSFlorian Westphal 98d2f77c53SPaolo Abeni return false; 990b4f33deSFlorian Westphal } 1000b4f33deSFlorian Westphal 101fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 1022303f994SPeter Krystad { 1032303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1042303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1052303f994SPeter Krystad struct socket *ssock; 1062303f994SPeter Krystad int err; 1072303f994SPeter Krystad 1082303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1092303f994SPeter Krystad if (err) 110fa68018dSPaolo Abeni return err; 1112303f994SPeter Krystad 1128ab183deSPaolo Abeni msk->first = ssock->sk; 1132303f994SPeter Krystad msk->subflow = ssock; 1142303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 115cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 116e16163b6SPaolo Abeni sock_hold(ssock->sk); 1172303f994SPeter Krystad subflow->request_mptcp = 1; 118866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 119e1ff9e82SDavide Caratti 120fa68018dSPaolo Abeni return 0; 1212303f994SPeter Krystad } 1222303f994SPeter Krystad 123ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 124ab174ad8SPaolo Abeni { 125ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 126ab174ad8SPaolo Abeni __kfree_skb(skb); 127ab174ad8SPaolo Abeni } 128ab174ad8SPaolo Abeni 1298268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1308268ed4cSPaolo Abeni struct sk_buff *from) 1318268ed4cSPaolo Abeni { 1328268ed4cSPaolo Abeni bool fragstolen; 1338268ed4cSPaolo Abeni int delta; 1348268ed4cSPaolo Abeni 1358268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1368268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1378268ed4cSPaolo Abeni return false; 1388268ed4cSPaolo Abeni 13906242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 14006242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 14106242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 142ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1438268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1448268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1458268ed4cSPaolo Abeni sk_mem_charge(sk, delta); 1468268ed4cSPaolo Abeni return true; 1478268ed4cSPaolo Abeni } 1488268ed4cSPaolo Abeni 149ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 150ab174ad8SPaolo Abeni struct sk_buff *from) 1516771bfd9SFlorian Westphal { 152ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 153ab174ad8SPaolo Abeni return false; 154ab174ad8SPaolo Abeni 155ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 156ab174ad8SPaolo Abeni } 157ab174ad8SPaolo Abeni 158ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 159ab174ad8SPaolo Abeni * - use mptcp seqs 160ab174ad8SPaolo Abeni * - don't cope with sacks 161ab174ad8SPaolo Abeni */ 162ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 163ab174ad8SPaolo Abeni { 164ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 165ab174ad8SPaolo Abeni struct rb_node **p, *parent; 166ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 167ab174ad8SPaolo Abeni struct sk_buff *skb1; 168ab174ad8SPaolo Abeni 169ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 170ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 171fa3fe2b1SFlorian Westphal max_seq = READ_ONCE(msk->rcv_wnd_sent); 172ab174ad8SPaolo Abeni 17306242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 17406242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 175fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 176ab174ad8SPaolo Abeni /* out of window */ 177ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 178fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 179fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 180fa3fe2b1SFlorian Westphal (unsigned long long)msk->rcv_wnd_sent); 18106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 182ab174ad8SPaolo Abeni return; 183ab174ad8SPaolo Abeni } 184ab174ad8SPaolo Abeni 185ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 18606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 187ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 188ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 189ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 190ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 191ab174ad8SPaolo Abeni goto end; 192ab174ad8SPaolo Abeni } 193ab174ad8SPaolo Abeni 194ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 195ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 196ab174ad8SPaolo Abeni */ 19706242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 19806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 19906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 200ab174ad8SPaolo Abeni return; 20106242e44SPaolo Abeni } 202ab174ad8SPaolo Abeni 203ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 204ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 20506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 206ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 207ab174ad8SPaolo Abeni p = &parent->rb_right; 208ab174ad8SPaolo Abeni goto insert; 209ab174ad8SPaolo Abeni } 210ab174ad8SPaolo Abeni 211ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 212ab174ad8SPaolo Abeni parent = NULL; 213ab174ad8SPaolo Abeni while (*p) { 214ab174ad8SPaolo Abeni parent = *p; 215ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 216ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 217ab174ad8SPaolo Abeni p = &parent->rb_left; 218ab174ad8SPaolo Abeni continue; 219ab174ad8SPaolo Abeni } 220ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 221ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 222ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 223ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 22406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 225ab174ad8SPaolo Abeni return; 226ab174ad8SPaolo Abeni } 227ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 228ab174ad8SPaolo Abeni /* partial overlap: 229ab174ad8SPaolo Abeni * | skb | 230ab174ad8SPaolo Abeni * | skb1 | 231ab174ad8SPaolo Abeni * continue traversing 232ab174ad8SPaolo Abeni */ 233ab174ad8SPaolo Abeni } else { 234ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 235ab174ad8SPaolo Abeni * Replace skb1 with skb. 236ab174ad8SPaolo Abeni */ 237ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 238ab174ad8SPaolo Abeni &msk->out_of_order_queue); 239ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 24006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 241ab174ad8SPaolo Abeni goto merge_right; 242ab174ad8SPaolo Abeni } 243ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 24406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 245ab174ad8SPaolo Abeni return; 246ab174ad8SPaolo Abeni } 247ab174ad8SPaolo Abeni p = &parent->rb_right; 248ab174ad8SPaolo Abeni } 24906242e44SPaolo Abeni 250ab174ad8SPaolo Abeni insert: 251ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 252ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 253ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 254ab174ad8SPaolo Abeni 255ab174ad8SPaolo Abeni merge_right: 256ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 257ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 258ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 259ab174ad8SPaolo Abeni break; 260ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 261ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 26206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 263ab174ad8SPaolo Abeni } 264ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 265ab174ad8SPaolo Abeni if (!skb1) 266ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 267ab174ad8SPaolo Abeni 268ab174ad8SPaolo Abeni end: 269ab174ad8SPaolo Abeni skb_condense(skb); 270ab174ad8SPaolo Abeni skb_set_owner_r(skb, sk); 271ab174ad8SPaolo Abeni } 272ab174ad8SPaolo Abeni 273ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 274ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 275ab174ad8SPaolo Abeni size_t copy_len) 276ab174ad8SPaolo Abeni { 277ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2786771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 2794e637c70SFlorian Westphal struct sk_buff *tail; 280b7f653b2SFlorian Westphal bool has_rxtstamp; 2816771bfd9SFlorian Westphal 2826771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 2834e637c70SFlorian Westphal 2844e637c70SFlorian Westphal skb_ext_reset(skb); 2854e637c70SFlorian Westphal skb_orphan(skb); 2864e637c70SFlorian Westphal 2879c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 2889c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) { 28972f96132SPaolo Abeni int amount = sk_mem_pages(skb->truesize) << SK_MEM_QUANTUM_SHIFT; 29072f96132SPaolo Abeni 29172f96132SPaolo Abeni if (ssk->sk_forward_alloc < amount) 2929c3f94e1SPaolo Abeni goto drop; 29372f96132SPaolo Abeni 29472f96132SPaolo Abeni ssk->sk_forward_alloc -= amount; 29572f96132SPaolo Abeni sk->sk_forward_alloc += amount; 2969c3f94e1SPaolo Abeni } 2979c3f94e1SPaolo Abeni 298b7f653b2SFlorian Westphal has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 299b7f653b2SFlorian Westphal 300ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 301ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 302ab174ad8SPaolo Abeni * value 303ab174ad8SPaolo Abeni */ 304ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 305ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 306ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 307b7f653b2SFlorian Westphal MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 308ab174ad8SPaolo Abeni 309ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 310ab174ad8SPaolo Abeni /* in sequence */ 311917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3124e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3138268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 314ab174ad8SPaolo Abeni return true; 3154e637c70SFlorian Westphal 316600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 3176771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 318ab174ad8SPaolo Abeni return true; 319ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 320ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 321ab174ad8SPaolo Abeni return false; 322ab174ad8SPaolo Abeni } 323ab174ad8SPaolo Abeni 324ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 325ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 326ab174ad8SPaolo Abeni */ 32706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3289c3f94e1SPaolo Abeni drop: 329ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 330ab174ad8SPaolo Abeni return false; 3316771bfd9SFlorian Westphal } 3326771bfd9SFlorian Westphal 33316a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 33416a9a9daSMat Martineau { 33516a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 33616a9a9daSMat Martineau 33716a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 33816a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 33916a9a9daSMat Martineau } 34016a9a9daSMat Martineau 341e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 342e16163b6SPaolo Abeni { 343e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 344e16163b6SPaolo Abeni return; 345e16163b6SPaolo Abeni 346e16163b6SPaolo Abeni sk->sk_state_change(sk); 347e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 348e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 349e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 350e16163b6SPaolo Abeni else 351e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 352e16163b6SPaolo Abeni } 353e16163b6SPaolo Abeni 3546e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 3556e628cd3SPaolo Abeni { 3566e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3576e628cd3SPaolo Abeni 3586e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 3596e628cd3SPaolo Abeni ((1 << sk->sk_state) & 3606e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 3616e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 3626e628cd3SPaolo Abeni } 3636e628cd3SPaolo Abeni 36416a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 36516a9a9daSMat Martineau { 36616a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 36716a9a9daSMat Martineau 36816a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 3696e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 37016a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 37116a9a9daSMat Martineau 37216a9a9daSMat Martineau switch (sk->sk_state) { 37316a9a9daSMat Martineau case TCP_FIN_WAIT1: 37416a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 37516a9a9daSMat Martineau break; 37616a9a9daSMat Martineau case TCP_CLOSING: 37716a9a9daSMat Martineau case TCP_LAST_ACK: 37816a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 37916a9a9daSMat Martineau break; 38016a9a9daSMat Martineau } 38116a9a9daSMat Martineau 382e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 38316a9a9daSMat Martineau } 38416a9a9daSMat Martineau } 38516a9a9daSMat Martineau 3863721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 3873721b9b6SMat Martineau { 3883721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3893721b9b6SMat Martineau 3903721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 3913721b9b6SMat Martineau ((1 << sk->sk_state) & 3923721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 3933721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 3943721b9b6SMat Martineau 3953721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 3963721b9b6SMat Martineau if (seq) 3973721b9b6SMat Martineau *seq = rcv_data_fin_seq; 3983721b9b6SMat Martineau 3993721b9b6SMat Martineau return true; 4003721b9b6SMat Martineau } 4013721b9b6SMat Martineau } 4023721b9b6SMat Martineau 4033721b9b6SMat Martineau return false; 4043721b9b6SMat Martineau } 4053721b9b6SMat Martineau 4066477dd39SMat Martineau static void mptcp_set_datafin_timeout(const struct sock *sk) 4076477dd39SMat Martineau { 4086477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 4096477dd39SMat Martineau 4106477dd39SMat Martineau mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX, 4116477dd39SMat Martineau TCP_RTO_MIN << icsk->icsk_retransmits); 4126477dd39SMat Martineau } 4136477dd39SMat Martineau 41433d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout) 4153721b9b6SMat Martineau { 4163721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4173721b9b6SMat Martineau } 4183721b9b6SMat Martineau 41933d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 42033d41c9cSPaolo Abeni { 42133d41c9cSPaolo Abeni const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 42233d41c9cSPaolo Abeni 42371b7dec2SPaolo Abeni return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 42471b7dec2SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 42533d41c9cSPaolo Abeni } 42633d41c9cSPaolo Abeni 42733d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk) 42833d41c9cSPaolo Abeni { 42933d41c9cSPaolo Abeni struct mptcp_subflow_context *subflow; 43033d41c9cSPaolo Abeni long tout = 0; 43133d41c9cSPaolo Abeni 43233d41c9cSPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) 43333d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 43433d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 43533d41c9cSPaolo Abeni } 43633d41c9cSPaolo Abeni 437fd897679SPaolo Abeni static bool tcp_can_send_ack(const struct sock *ssk) 438fd897679SPaolo Abeni { 439fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 44020bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 441fd897679SPaolo Abeni } 442fd897679SPaolo Abeni 443340fa666SMat Martineau void mptcp_subflow_send_ack(struct sock *ssk) 4447ed90803SPaolo Abeni { 44575e908c3SPaolo Abeni bool slow; 4467ed90803SPaolo Abeni 44775e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 448fd897679SPaolo Abeni if (tcp_can_send_ack(ssk)) 4497ed90803SPaolo Abeni tcp_send_ack(ssk); 45075e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 451fd897679SPaolo Abeni } 452340fa666SMat Martineau 453340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk) 454340fa666SMat Martineau { 455340fa666SMat Martineau struct mptcp_subflow_context *subflow; 456340fa666SMat Martineau 457340fa666SMat Martineau mptcp_for_each_subflow(msk, subflow) 458340fa666SMat Martineau mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 459ea4ca586SPaolo Abeni } 460ea4ca586SPaolo Abeni 461fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 462fd897679SPaolo Abeni { 46375e908c3SPaolo Abeni bool slow; 464fd897679SPaolo Abeni 46575e908c3SPaolo Abeni slow = lock_sock_fast(ssk); 466fde56eeaSPaolo Abeni if (tcp_can_send_ack(ssk)) 467fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 46875e908c3SPaolo Abeni unlock_sock_fast(ssk, slow); 469fde56eeaSPaolo Abeni } 470fde56eeaSPaolo Abeni 471fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 472fde56eeaSPaolo Abeni { 473fde56eeaSPaolo Abeni const struct inet_connection_sock *icsk = inet_csk(ssk); 474c4512c63SMatthieu Baerts u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 475fde56eeaSPaolo Abeni const struct tcp_sock *tp = tcp_sk(ssk); 476fde56eeaSPaolo Abeni 477fde56eeaSPaolo Abeni return (ack_pending & ICSK_ACK_SCHED) && 478fde56eeaSPaolo Abeni ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 479fde56eeaSPaolo Abeni READ_ONCE(icsk->icsk_ack.rcv_mss)) || 480fde56eeaSPaolo Abeni (rx_empty && ack_pending & 481fde56eeaSPaolo Abeni (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 482ea4ca586SPaolo Abeni } 483fd897679SPaolo Abeni 484fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 485fd897679SPaolo Abeni { 486e3859603SPaolo Abeni int old_space = READ_ONCE(msk->old_wspace); 487fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 488e3859603SPaolo Abeni struct sock *sk = (struct sock *)msk; 489fde56eeaSPaolo Abeni int space = __mptcp_space(sk); 490fde56eeaSPaolo Abeni bool cleanup, rx_empty; 491e3859603SPaolo Abeni 492fde56eeaSPaolo Abeni cleanup = (space > 0) && (space >= (old_space << 1)); 493ce599c51SPaolo Abeni rx_empty = !__mptcp_rmem(sk); 494fd897679SPaolo Abeni 495fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 496fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 497fd897679SPaolo Abeni 498fde56eeaSPaolo Abeni if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 499fde56eeaSPaolo Abeni mptcp_subflow_cleanup_rbuf(ssk); 5007ed90803SPaolo Abeni } 5017ed90803SPaolo Abeni } 5027ed90803SPaolo Abeni 5037ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 5043721b9b6SMat Martineau { 5053721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 5063721b9b6SMat Martineau u64 rcv_data_fin_seq; 5077ed90803SPaolo Abeni bool ret = false; 5083721b9b6SMat Martineau 509781bf13dSPaolo Abeni if (__mptcp_check_fallback(msk)) 5107ed90803SPaolo Abeni return ret; 5113721b9b6SMat Martineau 5123721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 5133721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 5143721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 5153721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 5163721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 5173721b9b6SMat Martineau * the msk state. 5183721b9b6SMat Martineau * 5193721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 5203721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 5213721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 5223721b9b6SMat Martineau * when catching up. 5233721b9b6SMat Martineau */ 5243721b9b6SMat Martineau 5253721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 526917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5273721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5283721b9b6SMat Martineau 5293721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 53016a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 53116a9a9daSMat Martineau set_bit(MPTCP_DATA_READY, &msk->flags); 5323721b9b6SMat Martineau 5333721b9b6SMat Martineau switch (sk->sk_state) { 5343721b9b6SMat Martineau case TCP_ESTABLISHED: 5353721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 5363721b9b6SMat Martineau break; 5373721b9b6SMat Martineau case TCP_FIN_WAIT1: 5383721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 5393721b9b6SMat Martineau break; 5403721b9b6SMat Martineau case TCP_FIN_WAIT2: 5413721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 5423721b9b6SMat Martineau break; 5433721b9b6SMat Martineau default: 5443721b9b6SMat Martineau /* Other states not expected */ 5453721b9b6SMat Martineau WARN_ON_ONCE(1); 5463721b9b6SMat Martineau break; 5473721b9b6SMat Martineau } 5483721b9b6SMat Martineau 5497ed90803SPaolo Abeni ret = true; 550fd897679SPaolo Abeni mptcp_send_ack(msk); 551e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 5523721b9b6SMat Martineau } 5537ed90803SPaolo Abeni return ret; 5543721b9b6SMat Martineau } 5553721b9b6SMat Martineau 5566771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 5576771bfd9SFlorian Westphal struct sock *ssk, 5586771bfd9SFlorian Westphal unsigned int *bytes) 5596771bfd9SFlorian Westphal { 5606771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 561600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 5626771bfd9SFlorian Westphal unsigned int moved = 0; 5636771bfd9SFlorian Westphal bool more_data_avail; 5646771bfd9SFlorian Westphal struct tcp_sock *tp; 5656771bfd9SFlorian Westphal bool done = false; 56613c7ba0cSFlorian Westphal int sk_rbuf; 56713c7ba0cSFlorian Westphal 56813c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 56913c7ba0cSFlorian Westphal 57013c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 57113c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 57213c7ba0cSFlorian Westphal 57313c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 57413c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 57513c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 57613c7ba0cSFlorian Westphal } 57713c7ba0cSFlorian Westphal } 578600911ffSFlorian Westphal 579ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 5806771bfd9SFlorian Westphal tp = tcp_sk(ssk); 5816771bfd9SFlorian Westphal do { 5826771bfd9SFlorian Westphal u32 map_remaining, offset; 5836771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 5846771bfd9SFlorian Westphal struct sk_buff *skb; 5856771bfd9SFlorian Westphal bool fin; 5866771bfd9SFlorian Westphal 5876771bfd9SFlorian Westphal /* try to move as much data as available */ 5886771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 5896771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 5906771bfd9SFlorian Westphal 5916771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 592d9fb8c50SPaolo Abeni if (!skb) { 593d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 594d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 595d9fb8c50SPaolo Abeni * can enter an infinite loop 596d9fb8c50SPaolo Abeni */ 597d9fb8c50SPaolo Abeni if (!moved) 598d9fb8c50SPaolo Abeni done = true; 5996771bfd9SFlorian Westphal break; 600d9fb8c50SPaolo Abeni } 6016771bfd9SFlorian Westphal 602e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 603e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 604e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 605e1ff9e82SDavide Caratti * be sure to adjust the size 606e1ff9e82SDavide Caratti */ 607e1ff9e82SDavide Caratti map_remaining = skb->len; 608e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 609e1ff9e82SDavide Caratti } 610e1ff9e82SDavide Caratti 6116771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 6126771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 6136771bfd9SFlorian Westphal if (fin) { 6146771bfd9SFlorian Westphal done = true; 6156771bfd9SFlorian Westphal seq++; 6166771bfd9SFlorian Westphal } 6176771bfd9SFlorian Westphal 6186771bfd9SFlorian Westphal if (offset < skb->len) { 6196771bfd9SFlorian Westphal size_t len = skb->len - offset; 6206771bfd9SFlorian Westphal 6216771bfd9SFlorian Westphal if (tp->urg_data) 6226771bfd9SFlorian Westphal done = true; 6236771bfd9SFlorian Westphal 624ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6256771bfd9SFlorian Westphal moved += len; 626ab174ad8SPaolo Abeni seq += len; 6276771bfd9SFlorian Westphal 6286771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6296771bfd9SFlorian Westphal break; 6306771bfd9SFlorian Westphal } else { 6316771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6326771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 6336771bfd9SFlorian Westphal done = true; 6346771bfd9SFlorian Westphal } 6356771bfd9SFlorian Westphal 6366771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 6376771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 638600911ffSFlorian Westphal 63913c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 640600911ffSFlorian Westphal done = true; 641600911ffSFlorian Westphal break; 642600911ffSFlorian Westphal } 6436771bfd9SFlorian Westphal } while (more_data_avail); 6446771bfd9SFlorian Westphal 6456719331cSPaolo Abeni *bytes += moved; 6466771bfd9SFlorian Westphal return done; 6476771bfd9SFlorian Westphal } 6486771bfd9SFlorian Westphal 64987952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 650ab174ad8SPaolo Abeni { 651ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 652ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 653ab174ad8SPaolo Abeni bool moved = false; 654ab174ad8SPaolo Abeni struct rb_node *p; 655ab174ad8SPaolo Abeni u64 end_seq; 656ab174ad8SPaolo Abeni 657ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 65806242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 659ab174ad8SPaolo Abeni while (p) { 660ab174ad8SPaolo Abeni skb = rb_to_skb(p); 661ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 662ab174ad8SPaolo Abeni break; 663ab174ad8SPaolo Abeni 664ab174ad8SPaolo Abeni p = rb_next(p); 665ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 666ab174ad8SPaolo Abeni 667ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 668ab174ad8SPaolo Abeni msk->ack_seq))) { 669ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 67006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 671ab174ad8SPaolo Abeni continue; 672ab174ad8SPaolo Abeni } 673ab174ad8SPaolo Abeni 674ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 675ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 676ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 677ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 678ab174ad8SPaolo Abeni 679ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 68006242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 68106242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 68206242e44SPaolo Abeni delta); 683ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 684ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 685ab174ad8SPaolo Abeni } 686ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 687ab174ad8SPaolo Abeni moved = true; 688ab174ad8SPaolo Abeni } 689ab174ad8SPaolo Abeni return moved; 690ab174ad8SPaolo Abeni } 691ab174ad8SPaolo Abeni 6922e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 6932e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 6942e52213cSFlorian Westphal */ 69599d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 6962e52213cSFlorian Westphal { 6972e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 6982e52213cSFlorian Westphal unsigned int moved = 0; 6992e52213cSFlorian Westphal 7002e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 70187952603SPaolo Abeni __mptcp_ofo_queue(msk); 702499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) { 703499ada50SPaolo Abeni if (!sock_owned_by_user(sk)) 704499ada50SPaolo Abeni __mptcp_error_report(sk); 705499ada50SPaolo Abeni else 706499ada50SPaolo Abeni set_bit(MPTCP_ERROR_REPORT, &msk->flags); 707499ada50SPaolo Abeni } 708ab174ad8SPaolo Abeni 709ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 710ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 711ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 712ab174ad8SPaolo Abeni * do it. 713ab174ad8SPaolo Abeni */ 714ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 715ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 71699d1055cSPaolo Abeni return moved > 0; 7172e52213cSFlorian Westphal } 7182e52213cSFlorian Westphal 7192e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 720101f6f85SFlorian Westphal { 7216719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 722101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 72313c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 724101f6f85SFlorian Westphal 725d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 726d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 727d7b1bfd0SPaolo Abeni * more data to the msk receive queue 728d7b1bfd0SPaolo Abeni */ 729d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 730d7b1bfd0SPaolo Abeni return; 731d7b1bfd0SPaolo Abeni 73213c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 73313c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 73413c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 73513c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 73613c7ba0cSFlorian Westphal 73799d1055cSPaolo Abeni /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 738ce599c51SPaolo Abeni if (__mptcp_rmem(sk) > sk_rbuf) { 739ce599c51SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 74099d1055cSPaolo Abeni return; 741ce599c51SPaolo Abeni } 7422e52213cSFlorian Westphal 74399d1055cSPaolo Abeni /* Wake-up the reader only for in-sequence data */ 74499d1055cSPaolo Abeni mptcp_data_lock(sk); 74599d1055cSPaolo Abeni if (move_skbs_to_msk(msk, ssk)) { 74699d1055cSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 747101f6f85SFlorian Westphal sk->sk_data_ready(sk); 748101f6f85SFlorian Westphal } 74999d1055cSPaolo Abeni mptcp_data_unlock(sk); 75099d1055cSPaolo Abeni } 751101f6f85SFlorian Westphal 75278962489SFlorian Westphal static bool mptcp_do_flush_join_list(struct mptcp_sock *msk) 753ec3edaa7SPeter Krystad { 7545cf92bbaSPaolo Abeni struct mptcp_subflow_context *subflow; 755df00b087SFlorian Westphal bool ret = false; 7565cf92bbaSPaolo Abeni 757ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 75878962489SFlorian Westphal return false; 759ec3edaa7SPeter Krystad 760ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 761df00b087SFlorian Westphal list_for_each_entry(subflow, &msk->join_list, node) { 762df00b087SFlorian Westphal u32 sseq = READ_ONCE(subflow->setsockopt_seq); 76378962489SFlorian Westphal 764df00b087SFlorian Westphal mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow)); 765df00b087SFlorian Westphal if (READ_ONCE(msk->setsockopt_seq) != sseq) 766df00b087SFlorian Westphal ret = true; 767df00b087SFlorian Westphal } 768ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 769ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 77078962489SFlorian Westphal 771df00b087SFlorian Westphal return ret; 77278962489SFlorian Westphal } 77378962489SFlorian Westphal 77478962489SFlorian Westphal void __mptcp_flush_join_list(struct mptcp_sock *msk) 77578962489SFlorian Westphal { 77678962489SFlorian Westphal if (likely(!mptcp_do_flush_join_list(msk))) 77778962489SFlorian Westphal return; 77878962489SFlorian Westphal 77978962489SFlorian Westphal if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags)) 78078962489SFlorian Westphal mptcp_schedule_work((struct sock *)msk); 78178962489SFlorian Westphal } 78278962489SFlorian Westphal 78378962489SFlorian Westphal static void mptcp_flush_join_list(struct mptcp_sock *msk) 78478962489SFlorian Westphal { 78578962489SFlorian Westphal bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags); 78678962489SFlorian Westphal 78778962489SFlorian Westphal might_sleep(); 78878962489SFlorian Westphal 78978962489SFlorian Westphal if (!mptcp_do_flush_join_list(msk) && !sync_needed) 79078962489SFlorian Westphal return; 79178962489SFlorian Westphal 79278962489SFlorian Westphal mptcp_sockopt_sync_all(msk); 793ec3edaa7SPeter Krystad } 794ec3edaa7SPeter Krystad 795b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 796b51f9b80SPaolo Abeni { 797b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 798b51f9b80SPaolo Abeni } 799b51f9b80SPaolo Abeni 800b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 801b51f9b80SPaolo Abeni { 802b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 803b51f9b80SPaolo Abeni unsigned long tout; 804b51f9b80SPaolo Abeni 805e16163b6SPaolo Abeni /* prevent rescheduling on close */ 806e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 807e16163b6SPaolo Abeni return; 808e16163b6SPaolo Abeni 80933d41c9cSPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 810b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 811b51f9b80SPaolo Abeni } 812b51f9b80SPaolo Abeni 813ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 814ba8f48f7SPaolo Abeni { 815ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 816ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 817ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 818ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 819ba8f48f7SPaolo Abeni */ 820ba8f48f7SPaolo Abeni sock_hold(sk); 821ba8f48f7SPaolo Abeni return true; 822ba8f48f7SPaolo Abeni } 823ba8f48f7SPaolo Abeni return false; 824ba8f48f7SPaolo Abeni } 825ba8f48f7SPaolo Abeni 82659832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 82759832e24SFlorian Westphal { 828ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 829ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 83059832e24SFlorian Westphal } 83159832e24SFlorian Westphal 8325969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 8335969856aSPaolo Abeni { 8345969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 8355969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 8365969856aSPaolo Abeni int receivers = 0; 8375969856aSPaolo Abeni 8385969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 8395969856aSPaolo Abeni receivers += !subflow->rx_eof; 840e16163b6SPaolo Abeni if (receivers) 841e16163b6SPaolo Abeni return; 8425969856aSPaolo Abeni 843e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 8445969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 8455969856aSPaolo Abeni * to msk, when all subflows agree on it 8465969856aSPaolo Abeni */ 8475969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 8485969856aSPaolo Abeni 8495969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 8505969856aSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 8515969856aSPaolo Abeni sk->sk_data_ready(sk); 8525969856aSPaolo Abeni } 853e16163b6SPaolo Abeni 854e16163b6SPaolo Abeni switch (sk->sk_state) { 855e16163b6SPaolo Abeni case TCP_ESTABLISHED: 856e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 857e16163b6SPaolo Abeni break; 858e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 85926aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 86026aa2314SPaolo Abeni break; 86126aa2314SPaolo Abeni case TCP_FIN_WAIT2: 862e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 863e16163b6SPaolo Abeni break; 864e16163b6SPaolo Abeni default: 865e16163b6SPaolo Abeni return; 866e16163b6SPaolo Abeni } 867e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 8685969856aSPaolo Abeni } 8695969856aSPaolo Abeni 8707a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 8717a6a6cbcSPaolo Abeni { 8727a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 8737a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 8747a6a6cbcSPaolo Abeni 8757a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 8767a6a6cbcSPaolo Abeni 8777a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 87899d1055cSPaolo Abeni if (READ_ONCE(subflow->data_avail)) 8797a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 8807a6a6cbcSPaolo Abeni } 8817a6a6cbcSPaolo Abeni 8827a6a6cbcSPaolo Abeni return NULL; 8837a6a6cbcSPaolo Abeni } 8847a6a6cbcSPaolo Abeni 8853f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 88657040755SPaolo Abeni const struct sk_buff *skb, 88757040755SPaolo Abeni const struct mptcp_ext *mpext) 8886d0060f6SMat Martineau { 88957040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 89057040755SPaolo Abeni return false; 89157040755SPaolo Abeni 8925a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 8935a369ca6SPaolo Abeni * mapping has not been xmitted yet 8945a369ca6SPaolo Abeni */ 8955a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 8965a369ca6SPaolo Abeni !mpext->frozen; 89757040755SPaolo Abeni } 89857040755SPaolo Abeni 89929249eacSPaolo Abeni /* we can append data to the given data frag if: 90029249eacSPaolo Abeni * - there is space available in the backing page_frag 90129249eacSPaolo Abeni * - the data frag tail matches the current page_frag free offset 90229249eacSPaolo Abeni * - the data frag end sequence number matches the current write seq 90329249eacSPaolo Abeni */ 90418b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 90518b683bfSPaolo Abeni const struct page_frag *pfrag, 90618b683bfSPaolo Abeni const struct mptcp_data_frag *df) 90718b683bfSPaolo Abeni { 90818b683bfSPaolo Abeni return df && pfrag->page == df->page && 909d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 91029249eacSPaolo Abeni pfrag->offset == (df->offset + df->data_len) && 91118b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 91218b683bfSPaolo Abeni } 91318b683bfSPaolo Abeni 9148ce568edSPaolo Abeni static int mptcp_wmem_with_overhead(int size) 915e93da928SPaolo Abeni { 9168ce568edSPaolo Abeni return size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT); 917e93da928SPaolo Abeni } 918e93da928SPaolo Abeni 919e93da928SPaolo Abeni static void __mptcp_wmem_reserve(struct sock *sk, int size) 920e93da928SPaolo Abeni { 9218ce568edSPaolo Abeni int amount = mptcp_wmem_with_overhead(size); 922e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 923e93da928SPaolo Abeni 924e93da928SPaolo Abeni WARN_ON_ONCE(msk->wmem_reserved); 925e7579d5dSDavide Caratti if (WARN_ON_ONCE(amount < 0)) 926e7579d5dSDavide Caratti amount = 0; 927e7579d5dSDavide Caratti 928e93da928SPaolo Abeni if (amount <= sk->sk_forward_alloc) 929e93da928SPaolo Abeni goto reserve; 930e93da928SPaolo Abeni 931e93da928SPaolo Abeni /* under memory pressure try to reserve at most a single page 932e93da928SPaolo Abeni * otherwise try to reserve the full estimate and fallback 933e93da928SPaolo Abeni * to a single page before entering the error path 934e93da928SPaolo Abeni */ 935e93da928SPaolo Abeni if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) || 936e93da928SPaolo Abeni !sk_wmem_schedule(sk, amount)) { 937e93da928SPaolo Abeni if (amount <= PAGE_SIZE) 938e93da928SPaolo Abeni goto nomem; 939e93da928SPaolo Abeni 940e93da928SPaolo Abeni amount = PAGE_SIZE; 941e93da928SPaolo Abeni if (!sk_wmem_schedule(sk, amount)) 942e93da928SPaolo Abeni goto nomem; 943e93da928SPaolo Abeni } 944e93da928SPaolo Abeni 945e93da928SPaolo Abeni reserve: 946e93da928SPaolo Abeni msk->wmem_reserved = amount; 947e93da928SPaolo Abeni sk->sk_forward_alloc -= amount; 948e93da928SPaolo Abeni return; 949e93da928SPaolo Abeni 950e93da928SPaolo Abeni nomem: 951e93da928SPaolo Abeni /* we will wait for memory on next allocation */ 952e93da928SPaolo Abeni msk->wmem_reserved = -1; 953e93da928SPaolo Abeni } 954e93da928SPaolo Abeni 955e93da928SPaolo Abeni static void __mptcp_update_wmem(struct sock *sk) 956e93da928SPaolo Abeni { 957e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 958e93da928SPaolo Abeni 959b5941f06SPaolo Abeni #ifdef CONFIG_LOCKDEP 960b5941f06SPaolo Abeni WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock)); 961b5941f06SPaolo Abeni #endif 962b5941f06SPaolo Abeni 963e93da928SPaolo Abeni if (!msk->wmem_reserved) 964e93da928SPaolo Abeni return; 965e93da928SPaolo Abeni 966e93da928SPaolo Abeni if (msk->wmem_reserved < 0) 967e93da928SPaolo Abeni msk->wmem_reserved = 0; 968e93da928SPaolo Abeni if (msk->wmem_reserved > 0) { 969e93da928SPaolo Abeni sk->sk_forward_alloc += msk->wmem_reserved; 970e93da928SPaolo Abeni msk->wmem_reserved = 0; 971e93da928SPaolo Abeni } 972e93da928SPaolo Abeni } 973e93da928SPaolo Abeni 974e93da928SPaolo Abeni static bool mptcp_wmem_alloc(struct sock *sk, int size) 975e93da928SPaolo Abeni { 976e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 977e93da928SPaolo Abeni 978e93da928SPaolo Abeni /* check for pre-existing error condition */ 979e93da928SPaolo Abeni if (msk->wmem_reserved < 0) 980e93da928SPaolo Abeni return false; 981e93da928SPaolo Abeni 982e93da928SPaolo Abeni if (msk->wmem_reserved >= size) 983e93da928SPaolo Abeni goto account; 984e93da928SPaolo Abeni 98587952603SPaolo Abeni mptcp_data_lock(sk); 98687952603SPaolo Abeni if (!sk_wmem_schedule(sk, size)) { 98787952603SPaolo Abeni mptcp_data_unlock(sk); 988e93da928SPaolo Abeni return false; 98987952603SPaolo Abeni } 990e93da928SPaolo Abeni 991e93da928SPaolo Abeni sk->sk_forward_alloc -= size; 992e93da928SPaolo Abeni msk->wmem_reserved += size; 99387952603SPaolo Abeni mptcp_data_unlock(sk); 994e93da928SPaolo Abeni 995e93da928SPaolo Abeni account: 996e93da928SPaolo Abeni msk->wmem_reserved -= size; 997e93da928SPaolo Abeni return true; 998e93da928SPaolo Abeni } 999e93da928SPaolo Abeni 100087952603SPaolo Abeni static void mptcp_wmem_uncharge(struct sock *sk, int size) 100187952603SPaolo Abeni { 100287952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100387952603SPaolo Abeni 100487952603SPaolo Abeni if (msk->wmem_reserved < 0) 100587952603SPaolo Abeni msk->wmem_reserved = 0; 100687952603SPaolo Abeni msk->wmem_reserved += size; 100787952603SPaolo Abeni } 100887952603SPaolo Abeni 10091094c6feSPaolo Abeni static void __mptcp_mem_reclaim_partial(struct sock *sk) 10101094c6feSPaolo Abeni { 10111094c6feSPaolo Abeni lockdep_assert_held_once(&sk->sk_lock.slock); 10121094c6feSPaolo Abeni __mptcp_update_wmem(sk); 10131094c6feSPaolo Abeni sk_mem_reclaim_partial(sk); 10141094c6feSPaolo Abeni } 10151094c6feSPaolo Abeni 1016724cfd2eSPaolo Abeni static void mptcp_mem_reclaim_partial(struct sock *sk) 1017724cfd2eSPaolo Abeni { 1018724cfd2eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1019724cfd2eSPaolo Abeni 1020724cfd2eSPaolo Abeni /* if we are experiencing a transint allocation error, 1021724cfd2eSPaolo Abeni * the forward allocation memory has been already 1022724cfd2eSPaolo Abeni * released 1023724cfd2eSPaolo Abeni */ 1024724cfd2eSPaolo Abeni if (msk->wmem_reserved < 0) 1025724cfd2eSPaolo Abeni return; 1026724cfd2eSPaolo Abeni 1027724cfd2eSPaolo Abeni mptcp_data_lock(sk); 1028724cfd2eSPaolo Abeni sk->sk_forward_alloc += msk->wmem_reserved; 1029724cfd2eSPaolo Abeni sk_mem_reclaim_partial(sk); 1030724cfd2eSPaolo Abeni msk->wmem_reserved = sk->sk_forward_alloc; 1031724cfd2eSPaolo Abeni sk->sk_forward_alloc = 0; 1032724cfd2eSPaolo Abeni mptcp_data_unlock(sk); 1033724cfd2eSPaolo Abeni } 1034724cfd2eSPaolo Abeni 1035d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 103618b683bfSPaolo Abeni { 1037d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 10387948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 1039d027236cSPaolo Abeni } 1040d027236cSPaolo Abeni 1041d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 1042d027236cSPaolo Abeni { 1043d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 1044d027236cSPaolo Abeni 104518b683bfSPaolo Abeni list_del(&dfrag->list); 1046d027236cSPaolo Abeni dfrag_uncharge(sk, len); 104718b683bfSPaolo Abeni put_page(dfrag->page); 104818b683bfSPaolo Abeni } 104918b683bfSPaolo Abeni 10506e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 105118b683bfSPaolo Abeni { 105218b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 105318b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1054d027236cSPaolo Abeni bool cleaned = false; 1055e1ff9e82SDavide Caratti u64 snd_una; 1056e1ff9e82SDavide Caratti 1057e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 1058e1ff9e82SDavide Caratti * plain TCP 1059e1ff9e82SDavide Caratti */ 1060e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10617439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10627439d687SPaolo Abeni 10637439d687SPaolo Abeni snd_una = msk->snd_una; 106418b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 106518b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 106618b683bfSPaolo Abeni break; 106718b683bfSPaolo Abeni 10681e1d9d6fSPaolo Abeni if (unlikely(dfrag == msk->first_pending)) { 10691e1d9d6fSPaolo Abeni /* in recovery mode can see ack after the current snd head */ 10701e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 1071d9ca1de8SPaolo Abeni break; 10721e1d9d6fSPaolo Abeni 10731e1d9d6fSPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 10741e1d9d6fSPaolo Abeni } 10751e1d9d6fSPaolo Abeni 1076d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1077d027236cSPaolo Abeni cleaned = true; 1078d027236cSPaolo Abeni } 1079d027236cSPaolo Abeni 10807948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10817948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 108253eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 108353eb4c38SPaolo Abeni 10841e1d9d6fSPaolo Abeni /* prevent wrap around in recovery mode */ 10851e1d9d6fSPaolo Abeni if (unlikely(delta > dfrag->already_sent)) { 10861e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(!msk->recovery)) 108753eb4c38SPaolo Abeni goto out; 10881e1d9d6fSPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 10891e1d9d6fSPaolo Abeni goto out; 10901e1d9d6fSPaolo Abeni dfrag->already_sent += delta - dfrag->already_sent; 10911e1d9d6fSPaolo Abeni } 10927948f6ccSFlorian Westphal 10937948f6ccSFlorian Westphal dfrag->data_seq += delta; 109453eb4c38SPaolo Abeni dfrag->offset += delta; 10957948f6ccSFlorian Westphal dfrag->data_len -= delta; 1096d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10977948f6ccSFlorian Westphal 10987948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10997948f6ccSFlorian Westphal cleaned = true; 11007948f6ccSFlorian Westphal } 11017948f6ccSFlorian Westphal 11021e1d9d6fSPaolo Abeni /* all retransmitted data acked, recovery completed */ 11031e1d9d6fSPaolo Abeni if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 11041e1d9d6fSPaolo Abeni msk->recovery = false; 11051e1d9d6fSPaolo Abeni 110653eb4c38SPaolo Abeni out: 11071094c6feSPaolo Abeni if (cleaned && tcp_under_memory_pressure(sk)) 11081094c6feSPaolo Abeni __mptcp_mem_reclaim_partial(sk); 110918b683bfSPaolo Abeni 11101e1d9d6fSPaolo Abeni if (snd_una == READ_ONCE(msk->snd_nxt) && !msk->recovery) { 111133d41c9cSPaolo Abeni if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 11126e628cd3SPaolo Abeni mptcp_stop_timer(sk); 11136e628cd3SPaolo Abeni } else { 11146e628cd3SPaolo Abeni mptcp_reset_timer(sk); 11156e628cd3SPaolo Abeni } 11166e628cd3SPaolo Abeni } 11176e628cd3SPaolo Abeni 1118417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk) 1119417789dfSPaolo Abeni { 1120b5941f06SPaolo Abeni #ifdef CONFIG_LOCKDEP 1121b5941f06SPaolo Abeni WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock)); 1122b5941f06SPaolo Abeni #endif 1123417789dfSPaolo Abeni __mptcp_clean_una(sk); 1124417789dfSPaolo Abeni mptcp_write_space(sk); 1125417789dfSPaolo Abeni } 1126417789dfSPaolo Abeni 1127b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk) 1128b5941f06SPaolo Abeni { 1129b5941f06SPaolo Abeni mptcp_data_lock(sk); 1130b5941f06SPaolo Abeni __mptcp_clean_una_wakeup(sk); 1131b5941f06SPaolo Abeni mptcp_data_unlock(sk); 1132b5941f06SPaolo Abeni } 1133b5941f06SPaolo Abeni 1134724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 113518b683bfSPaolo Abeni { 1136d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1137d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1138d9ca1de8SPaolo Abeni bool first = true; 1139d9ca1de8SPaolo Abeni 114018b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1141d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1142d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1143d9ca1de8SPaolo Abeni 1144d9ca1de8SPaolo Abeni if (first) 1145d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1146d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1147d9ca1de8SPaolo Abeni first = false; 1148d9ca1de8SPaolo Abeni } 1149724cfd2eSPaolo Abeni } 1150724cfd2eSPaolo Abeni 1151724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1152724cfd2eSPaolo Abeni * data 1153724cfd2eSPaolo Abeni */ 1154724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1155724cfd2eSPaolo Abeni { 1156724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1157724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1158724cfd2eSPaolo Abeni return true; 1159724cfd2eSPaolo Abeni 1160724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 116118b683bfSPaolo Abeni return false; 116218b683bfSPaolo Abeni } 116318b683bfSPaolo Abeni 116418b683bfSPaolo Abeni static struct mptcp_data_frag * 116518b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 116618b683bfSPaolo Abeni int orig_offset) 116718b683bfSPaolo Abeni { 116818b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 116918b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 117018b683bfSPaolo Abeni 117118b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 117218b683bfSPaolo Abeni dfrag->data_len = 0; 117318b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 117418b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 117518b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1176d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 117718b683bfSPaolo Abeni dfrag->page = pfrag->page; 117818b683bfSPaolo Abeni 117918b683bfSPaolo Abeni return dfrag; 118018b683bfSPaolo Abeni } 118118b683bfSPaolo Abeni 1182caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1183caf971dfSPaolo Abeni int mss_now; 1184caf971dfSPaolo Abeni int size_goal; 1185d9ca1de8SPaolo Abeni u16 limit; 1186d9ca1de8SPaolo Abeni u16 sent; 1187d9ca1de8SPaolo Abeni unsigned int flags; 11881094c6feSPaolo Abeni bool data_lock_held; 1189caf971dfSPaolo Abeni }; 1190caf971dfSPaolo Abeni 11916f8a612aSFlorian Westphal static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq, 11926f8a612aSFlorian Westphal int avail_size) 11936f8a612aSFlorian Westphal { 11946f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 11956f8a612aSFlorian Westphal 11966f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11976f8a612aSFlorian Westphal return avail_size; 11986f8a612aSFlorian Westphal 11996f8a612aSFlorian Westphal if (!before64(data_seq + avail_size, window_end)) { 12006f8a612aSFlorian Westphal u64 allowed_size = window_end - data_seq; 12016f8a612aSFlorian Westphal 12026f8a612aSFlorian Westphal return min_t(unsigned int, allowed_size, avail_size); 12036f8a612aSFlorian Westphal } 12046f8a612aSFlorian Westphal 12056f8a612aSFlorian Westphal return avail_size; 12066f8a612aSFlorian Westphal } 12076f8a612aSFlorian Westphal 1208724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1209724cfd2eSPaolo Abeni { 1210724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1211724cfd2eSPaolo Abeni 1212724cfd2eSPaolo Abeni if (!mpext) 1213724cfd2eSPaolo Abeni return false; 1214724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1215724cfd2eSPaolo Abeni return true; 1216724cfd2eSPaolo Abeni } 1217724cfd2eSPaolo Abeni 12186e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1219724cfd2eSPaolo Abeni { 1220724cfd2eSPaolo Abeni struct sk_buff *skb; 1221724cfd2eSPaolo Abeni 12226e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1223724cfd2eSPaolo Abeni if (likely(skb)) { 12246e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1225724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1226724cfd2eSPaolo Abeni skb->reserved_tailroom = skb->end - skb->tail; 1227f70cad10SPaolo Abeni INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1228724cfd2eSPaolo Abeni return skb; 1229724cfd2eSPaolo Abeni } 1230724cfd2eSPaolo Abeni __kfree_skb(skb); 1231724cfd2eSPaolo Abeni } else { 1232724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1233724cfd2eSPaolo Abeni } 1234724cfd2eSPaolo Abeni return NULL; 1235724cfd2eSPaolo Abeni } 1236724cfd2eSPaolo Abeni 1237f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1238724cfd2eSPaolo Abeni { 1239724cfd2eSPaolo Abeni struct sk_buff *skb; 1240724cfd2eSPaolo Abeni 12416e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1242724cfd2eSPaolo Abeni if (!skb) 1243f70cad10SPaolo Abeni return NULL; 1244724cfd2eSPaolo Abeni 1245724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1246f70cad10SPaolo Abeni tcp_skb_entail(ssk, skb); 1247f70cad10SPaolo Abeni return skb; 1248724cfd2eSPaolo Abeni } 1249724cfd2eSPaolo Abeni kfree_skb(skb); 1250f70cad10SPaolo Abeni return NULL; 1251724cfd2eSPaolo Abeni } 1252724cfd2eSPaolo Abeni 1253f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1254724cfd2eSPaolo Abeni { 12551094c6feSPaolo Abeni gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1256724cfd2eSPaolo Abeni 12571094c6feSPaolo Abeni if (unlikely(tcp_under_memory_pressure(sk))) { 12581094c6feSPaolo Abeni if (data_lock_held) 12591094c6feSPaolo Abeni __mptcp_mem_reclaim_partial(sk); 12601094c6feSPaolo Abeni else 1261724cfd2eSPaolo Abeni mptcp_mem_reclaim_partial(sk); 12621094c6feSPaolo Abeni } 12631094c6feSPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1264724cfd2eSPaolo Abeni } 1265724cfd2eSPaolo Abeni 1266d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even 1267d0cc2987SGeliang Tang * if we just appended a single frag. More status info needed 1268d0cc2987SGeliang Tang */ 1269d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1270d0cc2987SGeliang Tang { 1271d0cc2987SGeliang Tang struct mptcp_ext *mpext = mptcp_get_ext(skb); 1272d0cc2987SGeliang Tang __wsum csum = ~csum_unfold(mpext->csum); 1273d0cc2987SGeliang Tang int offset = skb->len - added; 1274d0cc2987SGeliang Tang 1275d0cc2987SGeliang Tang mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1276d0cc2987SGeliang Tang } 1277d0cc2987SGeliang Tang 127857040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1279d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1280caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 128157040755SPaolo Abeni { 1282d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 1283f70cad10SPaolo Abeni int offset = dfrag->offset + info->sent; 12846d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12856f8a612aSFlorian Westphal bool zero_window_probe = false; 12866d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 1287f70cad10SPaolo Abeni bool can_coalesce = false; 1288f70cad10SPaolo Abeni bool reuse_skb = true; 1289f70cad10SPaolo Abeni struct sk_buff *skb; 1290f70cad10SPaolo Abeni size_t copy; 1291f70cad10SPaolo Abeni int i; 12926d0060f6SMat Martineau 1293e4b61351SGeliang Tang pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1294d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12956d0060f6SMat Martineau 1296f70cad10SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1297f70cad10SPaolo Abeni info->limit > dfrag->data_len)) 1298f70cad10SPaolo Abeni return 0; 1299f70cad10SPaolo Abeni 1300d9ca1de8SPaolo Abeni /* compute send limit */ 1301d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1302f70cad10SPaolo Abeni copy = info->size_goal; 1303f70cad10SPaolo Abeni 130457040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 1305f70cad10SPaolo Abeni if (skb && copy > skb->len) { 130657040755SPaolo Abeni /* Limit the write to the size available in the 130757040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 130857040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 130957040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 131057040755SPaolo Abeni * SSN association set here 131157040755SPaolo Abeni */ 1312d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 13131094c6feSPaolo Abeni if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 131457040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 13151094c6feSPaolo Abeni goto alloc_skb; 13161094c6feSPaolo Abeni } 13171094c6feSPaolo Abeni 1318f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1319f70cad10SPaolo Abeni can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1320f70cad10SPaolo Abeni if (!can_coalesce && i >= sysctl_max_skb_frags) { 1321f70cad10SPaolo Abeni tcp_mark_push(tcp_sk(ssk), skb); 1322f70cad10SPaolo Abeni goto alloc_skb; 132315e6ca97SPaolo Abeni } 132418b683bfSPaolo Abeni 1325f70cad10SPaolo Abeni copy -= skb->len; 1326f70cad10SPaolo Abeni } else { 13271094c6feSPaolo Abeni alloc_skb: 1328f70cad10SPaolo Abeni skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1329f70cad10SPaolo Abeni if (!skb) 1330f70cad10SPaolo Abeni return -ENOMEM; 1331f70cad10SPaolo Abeni 1332f70cad10SPaolo Abeni i = skb_shinfo(skb)->nr_frags; 1333f70cad10SPaolo Abeni reuse_skb = false; 1334f70cad10SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1335f70cad10SPaolo Abeni } 13361094c6feSPaolo Abeni 13376f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 1338f70cad10SPaolo Abeni copy = mptcp_check_allowed_size(msk, data_seq, copy); 1339f70cad10SPaolo Abeni if (copy == 0) { 13407439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 13417439d687SPaolo Abeni 1342f70cad10SPaolo Abeni if (snd_una != msk->snd_nxt) { 1343f70cad10SPaolo Abeni tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk)); 13446f8a612aSFlorian Westphal return 0; 13456f8a612aSFlorian Westphal } 13466f8a612aSFlorian Westphal 1347f70cad10SPaolo Abeni zero_window_probe = true; 1348f70cad10SPaolo Abeni data_seq = snd_una - 1; 1349f70cad10SPaolo Abeni copy = 1; 135057040755SPaolo Abeni 1351f70cad10SPaolo Abeni /* all mptcp-level data is acked, no skbs should be present into the 1352f70cad10SPaolo Abeni * ssk write queue 1353f70cad10SPaolo Abeni */ 1354f70cad10SPaolo Abeni WARN_ON_ONCE(reuse_skb); 1355f70cad10SPaolo Abeni } 1356f70cad10SPaolo Abeni 1357f70cad10SPaolo Abeni copy = min_t(size_t, copy, info->limit - info->sent); 1358f70cad10SPaolo Abeni if (!sk_wmem_schedule(ssk, copy)) { 1359f70cad10SPaolo Abeni tcp_remove_empty_skb(ssk, tcp_write_queue_tail(ssk)); 1360e2223995SPaolo Abeni return -ENOMEM; 136135759383SFlorian Westphal } 136218b683bfSPaolo Abeni 1363f70cad10SPaolo Abeni if (can_coalesce) { 1364f70cad10SPaolo Abeni skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1365f70cad10SPaolo Abeni } else { 1366f70cad10SPaolo Abeni get_page(dfrag->page); 1367f70cad10SPaolo Abeni skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 136857040755SPaolo Abeni } 136957040755SPaolo Abeni 1370f70cad10SPaolo Abeni skb->len += copy; 1371f70cad10SPaolo Abeni skb->data_len += copy; 1372f70cad10SPaolo Abeni skb->truesize += copy; 1373f70cad10SPaolo Abeni sk_wmem_queued_add(ssk, copy); 1374f70cad10SPaolo Abeni sk_mem_charge(ssk, copy); 1375f70cad10SPaolo Abeni skb->ip_summed = CHECKSUM_PARTIAL; 1376f70cad10SPaolo Abeni WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1377f70cad10SPaolo Abeni TCP_SKB_CB(skb)->end_seq += copy; 1378f70cad10SPaolo Abeni tcp_skb_pcount_set(skb, 0); 1379f70cad10SPaolo Abeni 1380f70cad10SPaolo Abeni /* on skb reuse we just need to update the DSS len */ 1381f70cad10SPaolo Abeni if (reuse_skb) { 1382f70cad10SPaolo Abeni TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1383f70cad10SPaolo Abeni mpext->data_len += copy; 1384f70cad10SPaolo Abeni WARN_ON_ONCE(zero_window_probe); 1385f70cad10SPaolo Abeni goto out; 1386724cfd2eSPaolo Abeni } 13876d0060f6SMat Martineau 13886d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1389d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13906d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1391f70cad10SPaolo Abeni mpext->data_len = copy; 13926d0060f6SMat Martineau mpext->use_map = 1; 13936d0060f6SMat Martineau mpext->dsn64 = 1; 13946d0060f6SMat Martineau 13956d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13966d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13976d0060f6SMat Martineau mpext->dsn64); 13986d0060f6SMat Martineau 13996f8a612aSFlorian Westphal if (zero_window_probe) { 1400f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 14016f8a612aSFlorian Westphal mpext->frozen = 1; 1402d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1403f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 14046f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 1405d0cc2987SGeliang Tang return 0; 14066f8a612aSFlorian Westphal } 140757040755SPaolo Abeni out: 1408d0cc2987SGeliang Tang if (READ_ONCE(msk->csum_enabled)) 1409f70cad10SPaolo Abeni mptcp_update_data_checksum(skb, copy); 1410f70cad10SPaolo Abeni mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1411f70cad10SPaolo Abeni return copy; 14126d0060f6SMat Martineau } 14136d0060f6SMat Martineau 1414d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1415d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1416d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1417d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1418d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1419d5f49190SPaolo Abeni 1420d5f49190SPaolo Abeni struct subflow_send_info { 1421d5f49190SPaolo Abeni struct sock *ssk; 1422d5f49190SPaolo Abeni u64 ratio; 1423d5f49190SPaolo Abeni }; 1424d5f49190SPaolo Abeni 1425ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1426ff5a0b42SPaolo Abeni { 1427ff5a0b42SPaolo Abeni if (!subflow->stale) 1428ff5a0b42SPaolo Abeni return; 1429ff5a0b42SPaolo Abeni 1430ff5a0b42SPaolo Abeni subflow->stale = 0; 1431fc1b4e3bSPaolo Abeni MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1432ff5a0b42SPaolo Abeni } 1433ff5a0b42SPaolo Abeni 1434ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1435ff5a0b42SPaolo Abeni { 1436ff5a0b42SPaolo Abeni if (unlikely(subflow->stale)) { 1437ff5a0b42SPaolo Abeni u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1438ff5a0b42SPaolo Abeni 1439ff5a0b42SPaolo Abeni if (subflow->stale_rcv_tstamp == rcv_tstamp) 1440ff5a0b42SPaolo Abeni return false; 1441ff5a0b42SPaolo Abeni 1442ff5a0b42SPaolo Abeni mptcp_subflow_set_active(subflow); 1443ff5a0b42SPaolo Abeni } 1444ff5a0b42SPaolo Abeni return __mptcp_subflow_active(subflow); 1445ff5a0b42SPaolo Abeni } 1446ff5a0b42SPaolo Abeni 144733d41c9cSPaolo Abeni /* implement the mptcp packet scheduler; 144833d41c9cSPaolo Abeni * returns the subflow that will transmit the next DSS 144933d41c9cSPaolo Abeni * additionally updates the rtx timeout 145033d41c9cSPaolo Abeni */ 14515cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1452f296234cSPeter Krystad { 1453d5f49190SPaolo Abeni struct subflow_send_info send_info[2]; 1454f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 145533d41c9cSPaolo Abeni struct sock *sk = (struct sock *)msk; 1456d5f49190SPaolo Abeni int i, nr_active = 0; 1457d5f49190SPaolo Abeni struct sock *ssk; 145833d41c9cSPaolo Abeni long tout = 0; 1459d5f49190SPaolo Abeni u64 ratio; 1460d5f49190SPaolo Abeni u32 pace; 1461f296234cSPeter Krystad 146233d41c9cSPaolo Abeni sock_owned_by_me(sk); 1463f296234cSPeter Krystad 1464d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1465d5f49190SPaolo Abeni if (!msk->first) 1466f296234cSPeter Krystad return NULL; 1467d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1468f296234cSPeter Krystad } 1469f296234cSPeter Krystad 1470d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1471d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1472d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 147333d41c9cSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 147433d41c9cSPaolo Abeni mptcp_set_timeout(sk); 1475d5f49190SPaolo Abeni return msk->last_snd; 147633d41c9cSPaolo Abeni } 1477f296234cSPeter Krystad 1478d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 1479d5f49190SPaolo Abeni for (i = 0; i < 2; ++i) { 1480d5f49190SPaolo Abeni send_info[i].ssk = NULL; 1481d5f49190SPaolo Abeni send_info[i].ratio = -1; 1482d5f49190SPaolo Abeni } 1483d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1484e10a9892SGeliang Tang trace_mptcp_subflow_get_send(subflow); 1485d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1486d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1487f296234cSPeter Krystad continue; 1488d5f49190SPaolo Abeni 148933d41c9cSPaolo Abeni tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1490d5f49190SPaolo Abeni nr_active += !subflow->backup; 1491ec369c3aSPaolo Abeni if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd) 1492d5f49190SPaolo Abeni continue; 1493d5f49190SPaolo Abeni 1494d5f49190SPaolo Abeni pace = READ_ONCE(ssk->sk_pacing_rate); 1495d5f49190SPaolo Abeni if (!pace) 1496d5f49190SPaolo Abeni continue; 1497d5f49190SPaolo Abeni 1498d5f49190SPaolo Abeni ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1499d5f49190SPaolo Abeni pace); 1500d5f49190SPaolo Abeni if (ratio < send_info[subflow->backup].ratio) { 1501d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 1502d5f49190SPaolo Abeni send_info[subflow->backup].ratio = ratio; 1503d5f49190SPaolo Abeni } 1504f296234cSPeter Krystad } 150533d41c9cSPaolo Abeni __mptcp_set_timeout(sk, tout); 1506f296234cSPeter Krystad 1507d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1508d5f49190SPaolo Abeni if (!nr_active) 1509d5f49190SPaolo Abeni send_info[0].ssk = send_info[1].ssk; 1510d5f49190SPaolo Abeni 1511d5f49190SPaolo Abeni if (send_info[0].ssk) { 1512d5f49190SPaolo Abeni msk->last_snd = send_info[0].ssk; 1513d5f49190SPaolo Abeni msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1514ec369c3aSPaolo Abeni tcp_sk(msk->last_snd)->snd_wnd); 1515d5f49190SPaolo Abeni return msk->last_snd; 1516d5f49190SPaolo Abeni } 15175cf92bbaSPaolo Abeni 1518d5f49190SPaolo Abeni return NULL; 1519f296234cSPeter Krystad } 1520f296234cSPeter Krystad 1521d9ca1de8SPaolo Abeni static void mptcp_push_release(struct sock *sk, struct sock *ssk, 1522d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info *info) 1523d9ca1de8SPaolo Abeni { 1524d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1525d9ca1de8SPaolo Abeni release_sock(ssk); 1526d9ca1de8SPaolo Abeni } 1527d9ca1de8SPaolo Abeni 1528*0d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk, 1529*0d199e43SFlorian Westphal struct mptcp_data_frag *dfrag, 1530*0d199e43SFlorian Westphal u32 sent) 1531*0d199e43SFlorian Westphal { 1532*0d199e43SFlorian Westphal u64 snd_nxt_new = dfrag->data_seq; 1533*0d199e43SFlorian Westphal 1534*0d199e43SFlorian Westphal dfrag->already_sent += sent; 1535*0d199e43SFlorian Westphal 1536*0d199e43SFlorian Westphal msk->snd_burst -= sent; 1537*0d199e43SFlorian Westphal msk->tx_pending_data -= sent; 1538*0d199e43SFlorian Westphal 1539*0d199e43SFlorian Westphal snd_nxt_new += dfrag->already_sent; 1540*0d199e43SFlorian Westphal 1541*0d199e43SFlorian Westphal /* snd_nxt_new can be smaller than snd_nxt in case mptcp 1542*0d199e43SFlorian Westphal * is recovering after a failover. In that event, this re-sends 1543*0d199e43SFlorian Westphal * old segments. 1544*0d199e43SFlorian Westphal * 1545*0d199e43SFlorian Westphal * Thus compute snd_nxt_new candidate based on 1546*0d199e43SFlorian Westphal * the dfrag->data_seq that was sent and the data 1547*0d199e43SFlorian Westphal * that has been handed to the subflow for transmission 1548*0d199e43SFlorian Westphal * and skip update in case it was old dfrag. 1549*0d199e43SFlorian Westphal */ 1550*0d199e43SFlorian Westphal if (likely(after64(snd_nxt_new, msk->snd_nxt))) 1551*0d199e43SFlorian Westphal msk->snd_nxt = snd_nxt_new; 1552*0d199e43SFlorian Westphal } 1553*0d199e43SFlorian Westphal 1554ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1555d9ca1de8SPaolo Abeni { 1556d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1557d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1558d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1559d9ca1de8SPaolo Abeni .flags = flags, 1560d9ca1de8SPaolo Abeni }; 1561d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1562d9ca1de8SPaolo Abeni int len, copied = 0; 1563d9ca1de8SPaolo Abeni 1564d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1565d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1566d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1567d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1568d9ca1de8SPaolo Abeni while (len > 0) { 1569d9ca1de8SPaolo Abeni int ret = 0; 1570d9ca1de8SPaolo Abeni 1571d9ca1de8SPaolo Abeni prev_ssk = ssk; 157278962489SFlorian Westphal mptcp_flush_join_list(msk); 15735cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1574d9ca1de8SPaolo Abeni 15759758f40eSPaolo Abeni /* First check. If the ssk has changed since 15769758f40eSPaolo Abeni * the last round, release prev_ssk 1577d9ca1de8SPaolo Abeni */ 1578d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1579d9ca1de8SPaolo Abeni mptcp_push_release(sk, prev_ssk, &info); 1580d9ca1de8SPaolo Abeni if (!ssk) 1581d9ca1de8SPaolo Abeni goto out; 1582d9ca1de8SPaolo Abeni 15839758f40eSPaolo Abeni /* Need to lock the new subflow only if different 15849758f40eSPaolo Abeni * from the previous one, otherwise we are still 15859758f40eSPaolo Abeni * helding the relevant lock 15869758f40eSPaolo Abeni */ 15879758f40eSPaolo Abeni if (ssk != prev_ssk) 1588d9ca1de8SPaolo Abeni lock_sock(ssk); 1589d9ca1de8SPaolo Abeni 1590d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1591d9ca1de8SPaolo Abeni if (ret <= 0) { 1592d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1593d9ca1de8SPaolo Abeni goto out; 1594d9ca1de8SPaolo Abeni } 1595d9ca1de8SPaolo Abeni 1596d9ca1de8SPaolo Abeni info.sent += ret; 1597d9ca1de8SPaolo Abeni copied += ret; 1598d9ca1de8SPaolo Abeni len -= ret; 1599*0d199e43SFlorian Westphal 1600*0d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 1601d9ca1de8SPaolo Abeni } 1602d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1603d9ca1de8SPaolo Abeni } 1604d9ca1de8SPaolo Abeni 1605d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1606d9ca1de8SPaolo Abeni if (ssk) 1607d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1608d9ca1de8SPaolo Abeni 1609d9ca1de8SPaolo Abeni out: 161033d41c9cSPaolo Abeni /* ensure the rtx timer is running */ 1611d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1612d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 161333d41c9cSPaolo Abeni if (copied) 1614d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1615d9ca1de8SPaolo Abeni } 1616d9ca1de8SPaolo Abeni 16176e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 16186e628cd3SPaolo Abeni { 16196e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 16201094c6feSPaolo Abeni struct mptcp_sendmsg_info info = { 16211094c6feSPaolo Abeni .data_lock_held = true, 16221094c6feSPaolo Abeni }; 16236e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1624b19bc294SPaolo Abeni struct sock *xmit_ssk; 16256e628cd3SPaolo Abeni int len, copied = 0; 1626b19bc294SPaolo Abeni bool first = true; 16276e628cd3SPaolo Abeni 16286e628cd3SPaolo Abeni info.flags = 0; 16296e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 16306e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 16316e628cd3SPaolo Abeni info.limit = dfrag->data_len; 16326e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 16336e628cd3SPaolo Abeni while (len > 0) { 16346e628cd3SPaolo Abeni int ret = 0; 16356e628cd3SPaolo Abeni 1636b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1637b19bc294SPaolo Abeni * check for a different subflow usage only after 1638b19bc294SPaolo Abeni * spooling the first chunk of data 1639b19bc294SPaolo Abeni */ 1640b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1641b19bc294SPaolo Abeni if (!xmit_ssk) 1642b19bc294SPaolo Abeni goto out; 1643b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1644b19bc294SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 1645b19bc294SPaolo Abeni goto out; 1646b19bc294SPaolo Abeni } 1647b19bc294SPaolo Abeni 16486e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 16496e628cd3SPaolo Abeni if (ret <= 0) 16506e628cd3SPaolo Abeni goto out; 16516e628cd3SPaolo Abeni 16526e628cd3SPaolo Abeni info.sent += ret; 16536e628cd3SPaolo Abeni copied += ret; 16546e628cd3SPaolo Abeni len -= ret; 1655b19bc294SPaolo Abeni first = false; 1656*0d199e43SFlorian Westphal 1657*0d199e43SFlorian Westphal mptcp_update_post_push(msk, dfrag, ret); 16586e628cd3SPaolo Abeni } 16596e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 16606e628cd3SPaolo Abeni } 16616e628cd3SPaolo Abeni 16626e628cd3SPaolo Abeni out: 16636e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 16646e628cd3SPaolo Abeni * not going to flush it via release_sock() 16656e628cd3SPaolo Abeni */ 16666e628cd3SPaolo Abeni __mptcp_update_wmem(sk); 16676e628cd3SPaolo Abeni if (copied) { 16686e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 16696e628cd3SPaolo Abeni info.size_goal); 1670d09d818eSPaolo Abeni if (!mptcp_timer_pending(sk)) 1671d09d818eSPaolo Abeni mptcp_reset_timer(sk); 1672d09d818eSPaolo Abeni 16736e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 16746e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 16756e628cd3SPaolo Abeni mptcp_schedule_work(sk); 16766e628cd3SPaolo Abeni } 16776e628cd3SPaolo Abeni } 16786e628cd3SPaolo Abeni 16795cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 16805cf92bbaSPaolo Abeni { 16815cf92bbaSPaolo Abeni /* enable autotune */ 16825cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16835cf92bbaSPaolo Abeni 16845cf92bbaSPaolo Abeni /* will be cleared on avail space */ 16855cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 16865cf92bbaSPaolo Abeni } 16875cf92bbaSPaolo Abeni 1688f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1689f870fa0bSMat Martineau { 1690f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 169117091708SFlorian Westphal struct page_frag *pfrag; 16926d0060f6SMat Martineau size_t copied = 0; 1693caf971dfSPaolo Abeni int ret = 0; 16946d0060f6SMat Martineau long timeo; 1695f870fa0bSMat Martineau 1696987858e5SPaolo Abeni /* we don't support FASTOPEN yet */ 1697987858e5SPaolo Abeni if (msg->msg_flags & MSG_FASTOPEN) 1698f870fa0bSMat Martineau return -EOPNOTSUPP; 1699f870fa0bSMat Martineau 1700987858e5SPaolo Abeni /* silently ignore everything else */ 1701987858e5SPaolo Abeni msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL; 1702987858e5SPaolo Abeni 1703e7579d5dSDavide Caratti mptcp_lock_sock(sk, __mptcp_wmem_reserve(sk, min_t(size_t, 1 << 20, len))); 17041954b860SMat Martineau 17051954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 17061954b860SMat Martineau 17071954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 17081954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 17091954b860SMat Martineau if (ret) 17101954b860SMat Martineau goto out; 17111954b860SMat Martineau } 17121954b860SMat Martineau 171317091708SFlorian Westphal pfrag = sk_page_frag(sk); 171418b683bfSPaolo Abeni 1715d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1716724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1717d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1718d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1719d9ca1de8SPaolo Abeni size_t psize, offset; 1720d9ca1de8SPaolo Abeni 172157baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 172257baaf28SMat Martineau ret = -EPIPE; 172357baaf28SMat Martineau goto out; 172457baaf28SMat Martineau } 172557baaf28SMat Martineau 1726d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1727d9ca1de8SPaolo Abeni * page allocator 1728fb529e62SFlorian Westphal */ 1729d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1730d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1731d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1732d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1733d9ca1de8SPaolo Abeni goto wait_for_memory; 17346e628cd3SPaolo Abeni 1735d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1736d9ca1de8SPaolo Abeni goto wait_for_memory; 1737d9ca1de8SPaolo Abeni 1738d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1739d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1740fb529e62SFlorian Westphal } 1741fb529e62SFlorian Westphal 1742d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1743d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1744d9ca1de8SPaolo Abeni * anyway 1745d9ca1de8SPaolo Abeni */ 1746d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1747d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1748d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1749724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 17508ce568edSPaolo Abeni 17518ce568edSPaolo Abeni if (!mptcp_wmem_alloc(sk, total_ts)) 1752d9ca1de8SPaolo Abeni goto wait_for_memory; 1753d9ca1de8SPaolo Abeni 1754d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1755d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 175687952603SPaolo Abeni mptcp_wmem_uncharge(sk, psize + frag_truesize); 1757d9ca1de8SPaolo Abeni ret = -EFAULT; 1758d9ca1de8SPaolo Abeni goto out; 1759d9ca1de8SPaolo Abeni } 1760d9ca1de8SPaolo Abeni 1761d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 1762d9ca1de8SPaolo Abeni copied += psize; 1763d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1764d9ca1de8SPaolo Abeni frag_truesize += psize; 1765d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1766d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 176713e16037SPaolo Abeni msk->tx_pending_data += psize; 1768d9ca1de8SPaolo Abeni 1769d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1770d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1771d9ca1de8SPaolo Abeni */ 1772d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1773d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1774d9ca1de8SPaolo Abeni get_page(dfrag->page); 1775d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1776d9ca1de8SPaolo Abeni if (!msk->first_pending) 1777d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1778d9ca1de8SPaolo Abeni } 1779e4b61351SGeliang Tang pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1780d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1781d9ca1de8SPaolo Abeni !dfrag_collapsed); 1782d9ca1de8SPaolo Abeni 1783d9ca1de8SPaolo Abeni continue; 1784d9ca1de8SPaolo Abeni 1785d9ca1de8SPaolo Abeni wait_for_memory: 17865cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 1787c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1788f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1789f296234cSPeter Krystad if (ret) 1790f296234cSPeter Krystad goto out; 1791cec37a6eSPeter Krystad } 1792cec37a6eSPeter Krystad 179313e16037SPaolo Abeni if (copied) 1794c2e6048fSPaolo Abeni __mptcp_push_pending(sk, msg->msg_flags); 1795da51aef5SPaolo Abeni 17961954b860SMat Martineau out: 1797cec37a6eSPeter Krystad release_sock(sk); 17988555c6bfSPaolo Abeni return copied ? : ret; 1799f870fa0bSMat Martineau } 1800f870fa0bSMat Martineau 18017a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 18027a6a6cbcSPaolo Abeni { 18037a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 18047a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 18057a6a6cbcSPaolo Abeni 18067a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 18077a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 18087a6a6cbcSPaolo Abeni 18097a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 18103c90e377SPaolo Abeni test_bit(MPTCP_DATA_READY, &msk->flags), &wait); 18117a6a6cbcSPaolo Abeni 18127a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 18137a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 18147a6a6cbcSPaolo Abeni } 18157a6a6cbcSPaolo Abeni 18166771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 18176771bfd9SFlorian Westphal struct msghdr *msg, 1818b7f653b2SFlorian Westphal size_t len, int flags, 1819b7f653b2SFlorian Westphal struct scm_timestamping_internal *tss, 1820b7f653b2SFlorian Westphal int *cmsg_flags) 18216771bfd9SFlorian Westphal { 1822ca4fb892SYonglong Li struct sk_buff *skb, *tmp; 18236771bfd9SFlorian Westphal int copied = 0; 18246771bfd9SFlorian Westphal 1825ca4fb892SYonglong Li skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 18266771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 18276771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 18286771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 18296771bfd9SFlorian Westphal int err; 18306771bfd9SFlorian Westphal 1831d976092cSPaolo Abeni if (!(flags & MSG_TRUNC)) { 18326771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 18336771bfd9SFlorian Westphal if (unlikely(err < 0)) { 18346771bfd9SFlorian Westphal if (!copied) 18356771bfd9SFlorian Westphal return err; 18366771bfd9SFlorian Westphal break; 18376771bfd9SFlorian Westphal } 1838d976092cSPaolo Abeni } 18396771bfd9SFlorian Westphal 1840b7f653b2SFlorian Westphal if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1841b7f653b2SFlorian Westphal tcp_update_recv_tstamps(skb, tss); 1842b7f653b2SFlorian Westphal *cmsg_flags |= MPTCP_CMSG_TS; 1843b7f653b2SFlorian Westphal } 1844b7f653b2SFlorian Westphal 18456771bfd9SFlorian Westphal copied += count; 18466771bfd9SFlorian Westphal 18476771bfd9SFlorian Westphal if (count < data_len) { 1848ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 18496771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 18506771bfd9SFlorian Westphal break; 18516771bfd9SFlorian Westphal } 18526771bfd9SFlorian Westphal 1853ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) { 185487952603SPaolo Abeni /* we will bulk release the skb memory later */ 185587952603SPaolo Abeni skb->destructor = NULL; 1856ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 185787952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 18586771bfd9SFlorian Westphal __kfree_skb(skb); 1859ca4fb892SYonglong Li } 18606771bfd9SFlorian Westphal 18616771bfd9SFlorian Westphal if (copied >= len) 18626771bfd9SFlorian Westphal break; 18636771bfd9SFlorian Westphal } 18646771bfd9SFlorian Westphal 18656771bfd9SFlorian Westphal return copied; 18666771bfd9SFlorian Westphal } 18676771bfd9SFlorian Westphal 1868a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1869a6b118feSFlorian Westphal * 1870a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1871a6b118feSFlorian Westphal */ 1872a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1873a6b118feSFlorian Westphal { 1874a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1875a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1876a6b118feSFlorian Westphal u32 time, advmss = 1; 1877a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1878a6b118feSFlorian Westphal 1879a6b118feSFlorian Westphal sock_owned_by_me(sk); 1880a6b118feSFlorian Westphal 1881a6b118feSFlorian Westphal if (copied <= 0) 1882a6b118feSFlorian Westphal return; 1883a6b118feSFlorian Westphal 1884a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1885a6b118feSFlorian Westphal 1886a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1887a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1888a6b118feSFlorian Westphal 1889a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1890a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1891a6b118feSFlorian Westphal return; 1892a6b118feSFlorian Westphal 1893a6b118feSFlorian Westphal rtt_us = 0; 1894a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1895a6b118feSFlorian Westphal const struct tcp_sock *tp; 1896a6b118feSFlorian Westphal u64 sf_rtt_us; 1897a6b118feSFlorian Westphal u32 sf_advmss; 1898a6b118feSFlorian Westphal 1899a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1900a6b118feSFlorian Westphal 1901a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1902a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1903a6b118feSFlorian Westphal 1904a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1905a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1906a6b118feSFlorian Westphal } 1907a6b118feSFlorian Westphal 1908a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1909a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1910a6b118feSFlorian Westphal return; 1911a6b118feSFlorian Westphal 1912a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1913a6b118feSFlorian Westphal goto new_measure; 1914a6b118feSFlorian Westphal 1915a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1916a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1917a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1918a6b118feSFlorian Westphal u64 rcvwin, grow; 1919a6b118feSFlorian Westphal 1920a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1921a6b118feSFlorian Westphal 1922a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1923a6b118feSFlorian Westphal 1924a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1925a6b118feSFlorian Westphal rcvwin += (grow << 1); 1926a6b118feSFlorian Westphal 1927a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1928a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1929a6b118feSFlorian Westphal rcvmem += 128; 1930a6b118feSFlorian Westphal 1931a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1932a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1933a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1934a6b118feSFlorian Westphal 1935a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1936a6b118feSFlorian Westphal u32 window_clamp; 1937a6b118feSFlorian Westphal 1938a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1939a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1940a6b118feSFlorian Westphal 1941a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1942a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1943a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1944a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1945a6b118feSFlorian Westphal */ 1946a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1947a6b118feSFlorian Westphal struct sock *ssk; 1948c76c6956SPaolo Abeni bool slow; 1949a6b118feSFlorian Westphal 1950a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1951c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1952a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1953a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1954c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1955c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1956a6b118feSFlorian Westphal } 1957a6b118feSFlorian Westphal } 1958a6b118feSFlorian Westphal } 1959a6b118feSFlorian Westphal 1960a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1961a6b118feSFlorian Westphal new_measure: 1962a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1963a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1964a6b118feSFlorian Westphal } 1965a6b118feSFlorian Westphal 196687952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 196787952603SPaolo Abeni { 196887952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 196987952603SPaolo Abeni 197087952603SPaolo Abeni if (!msk->rmem_released) 197187952603SPaolo Abeni return; 197287952603SPaolo Abeni 197387952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 197487952603SPaolo Abeni sk_mem_uncharge(sk, msk->rmem_released); 1975ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 197687952603SPaolo Abeni } 197787952603SPaolo Abeni 197887952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 197987952603SPaolo Abeni { 198087952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 198187952603SPaolo Abeni 198287952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 198387952603SPaolo Abeni } 198487952603SPaolo Abeni 1985e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk) 19866771bfd9SFlorian Westphal { 198787952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 19886771bfd9SFlorian Westphal unsigned int moved = 0; 198987952603SPaolo Abeni bool ret, done; 1990d5f49190SPaolo Abeni 199178962489SFlorian Westphal mptcp_flush_join_list(msk); 19926771bfd9SFlorian Westphal do { 19936771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 199465f49fe7SFlorian Westphal bool slowpath; 19956771bfd9SFlorian Westphal 199687952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 199787952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 199887952603SPaolo Abeni * that is an unlikely slow path. 199987952603SPaolo Abeni */ 200087952603SPaolo Abeni if (likely(!ssk)) 20016771bfd9SFlorian Westphal break; 20026771bfd9SFlorian Westphal 200365f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 200487952603SPaolo Abeni mptcp_data_lock(sk); 2005e3859603SPaolo Abeni __mptcp_update_rmem(sk); 20066771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 200787952603SPaolo Abeni mptcp_data_unlock(sk); 2008499ada50SPaolo Abeni 2009499ada50SPaolo Abeni if (unlikely(ssk->sk_err)) 2010499ada50SPaolo Abeni __mptcp_error_report(sk); 201165f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 20126771bfd9SFlorian Westphal } while (!done); 20136771bfd9SFlorian Westphal 201487952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 201587952603SPaolo Abeni ret = moved > 0; 201687952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 201787952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 201887952603SPaolo Abeni mptcp_data_lock(sk); 201987952603SPaolo Abeni __mptcp_update_rmem(sk); 202087952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 202187952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 202287952603SPaolo Abeni mptcp_data_unlock(sk); 2023ab174ad8SPaolo Abeni } 202487952603SPaolo Abeni if (ret) 202587952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 202687952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 20276771bfd9SFlorian Westphal } 20286771bfd9SFlorian Westphal 2029f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2030f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 2031f870fa0bSMat Martineau { 2032f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2033b7f653b2SFlorian Westphal struct scm_timestamping_internal tss; 2034b7f653b2SFlorian Westphal int copied = 0, cmsg_flags = 0; 20357a6a6cbcSPaolo Abeni int target; 20367a6a6cbcSPaolo Abeni long timeo; 2037f870fa0bSMat Martineau 2038cb9d80f4SPaolo Abeni /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2039cb9d80f4SPaolo Abeni if (unlikely(flags & MSG_ERRQUEUE)) 2040cb9d80f4SPaolo Abeni return inet_recv_error(sk, msg, len, addr_len); 2041cb9d80f4SPaolo Abeni 204287952603SPaolo Abeni mptcp_lock_sock(sk, __mptcp_splice_receive_queue(sk)); 2043fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 2044fd897679SPaolo Abeni copied = -ENOTCONN; 2045fd897679SPaolo Abeni goto out_err; 2046fd897679SPaolo Abeni } 2047fd897679SPaolo Abeni 20487a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 20497a6a6cbcSPaolo Abeni 20507a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 20517a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 20527a6a6cbcSPaolo Abeni 205305e3eceaSEric Dumazet while (copied < len) { 2054e3859603SPaolo Abeni int bytes_read; 20557a6a6cbcSPaolo Abeni 2056b7f653b2SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 20576771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 20587a6a6cbcSPaolo Abeni if (!copied) 20597a6a6cbcSPaolo Abeni copied = bytes_read; 20606771bfd9SFlorian Westphal goto out_err; 2061cec37a6eSPeter Krystad } 2062cec37a6eSPeter Krystad 20637a6a6cbcSPaolo Abeni copied += bytes_read; 20647a6a6cbcSPaolo Abeni 2065ea4ca586SPaolo Abeni /* be sure to advertise window change */ 2066fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 2067ea4ca586SPaolo Abeni 2068e3859603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2069e3859603SPaolo Abeni continue; 2070e3859603SPaolo Abeni 20717a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 20727a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 20737a6a6cbcSPaolo Abeni */ 20747a6a6cbcSPaolo Abeni if (copied >= target) 20757a6a6cbcSPaolo Abeni break; 20767a6a6cbcSPaolo Abeni 20777a6a6cbcSPaolo Abeni if (copied) { 20787a6a6cbcSPaolo Abeni if (sk->sk_err || 20797a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 20807a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 20817a6a6cbcSPaolo Abeni !timeo || 20827a6a6cbcSPaolo Abeni signal_pending(current)) 20837a6a6cbcSPaolo Abeni break; 20847a6a6cbcSPaolo Abeni } else { 20857a6a6cbcSPaolo Abeni if (sk->sk_err) { 20867a6a6cbcSPaolo Abeni copied = sock_error(sk); 20877a6a6cbcSPaolo Abeni break; 20887a6a6cbcSPaolo Abeni } 20897a6a6cbcSPaolo Abeni 20905969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 20915969856aSPaolo Abeni mptcp_check_for_eof(msk); 20925969856aSPaolo Abeni 209387952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 209487952603SPaolo Abeni /* race breaker: the shutdown could be after the 209587952603SPaolo Abeni * previous receive queue check 209687952603SPaolo Abeni */ 2097e3859603SPaolo Abeni if (__mptcp_move_skbs(msk)) 209887952603SPaolo Abeni continue; 20997a6a6cbcSPaolo Abeni break; 210087952603SPaolo Abeni } 21017a6a6cbcSPaolo Abeni 21027a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 21037a6a6cbcSPaolo Abeni copied = -ENOTCONN; 21047a6a6cbcSPaolo Abeni break; 21057a6a6cbcSPaolo Abeni } 21067a6a6cbcSPaolo Abeni 21077a6a6cbcSPaolo Abeni if (!timeo) { 21087a6a6cbcSPaolo Abeni copied = -EAGAIN; 21097a6a6cbcSPaolo Abeni break; 21107a6a6cbcSPaolo Abeni } 21117a6a6cbcSPaolo Abeni 21127a6a6cbcSPaolo Abeni if (signal_pending(current)) { 21137a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 21147a6a6cbcSPaolo Abeni break; 21157a6a6cbcSPaolo Abeni } 21167a6a6cbcSPaolo Abeni } 21177a6a6cbcSPaolo Abeni 21187a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 21197a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 21207a6a6cbcSPaolo Abeni } 21217a6a6cbcSPaolo Abeni 212287952603SPaolo Abeni if (skb_queue_empty_lockless(&sk->sk_receive_queue) && 212387952603SPaolo Abeni skb_queue_empty(&msk->receive_queue)) { 21246771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 21257a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 21267a6a6cbcSPaolo Abeni 21276771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 21286771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 21297a6a6cbcSPaolo Abeni */ 2130e3859603SPaolo Abeni if (unlikely(__mptcp_move_skbs(msk))) 21316771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 21327a6a6cbcSPaolo Abeni } 21333c90e377SPaolo Abeni 21346771bfd9SFlorian Westphal out_err: 2135b7f653b2SFlorian Westphal if (cmsg_flags && copied >= 0) { 2136b7f653b2SFlorian Westphal if (cmsg_flags & MPTCP_CMSG_TS) 2137b7f653b2SFlorian Westphal tcp_recv_timestamp(msg, sk, &tss); 2138b7f653b2SFlorian Westphal } 2139b7f653b2SFlorian Westphal 21406719331cSPaolo Abeni pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d", 21416719331cSPaolo Abeni msk, test_bit(MPTCP_DATA_READY, &msk->flags), 214287952603SPaolo Abeni skb_queue_empty_lockless(&sk->sk_receive_queue), copied); 2143ca4fb892SYonglong Li if (!(flags & MSG_PEEK)) 2144a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2145a6b118feSFlorian Westphal 2146cec37a6eSPeter Krystad release_sock(sk); 2147cec37a6eSPeter Krystad return copied; 2148cec37a6eSPeter Krystad } 2149cec37a6eSPeter Krystad 2150b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2151b51f9b80SPaolo Abeni { 2152b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2153b51f9b80SPaolo Abeni icsk_retransmit_timer); 2154b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 21552d6f5a2bSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2156b51f9b80SPaolo Abeni 2157b51f9b80SPaolo Abeni bh_lock_sock(sk); 2158b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 21592d6f5a2bSPaolo Abeni /* we need a process context to retransmit */ 21602d6f5a2bSPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 21612d6f5a2bSPaolo Abeni mptcp_schedule_work(sk); 2162b51f9b80SPaolo Abeni } else { 2163b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 21642d6f5a2bSPaolo Abeni set_bit(MPTCP_RETRANSMIT, &msk->flags); 2165b51f9b80SPaolo Abeni } 2166b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2167b51f9b80SPaolo Abeni sock_put(sk); 2168b51f9b80SPaolo Abeni } 2169b51f9b80SPaolo Abeni 2170e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2171e16163b6SPaolo Abeni { 2172e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2173e16163b6SPaolo Abeni 2174e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2175b6d69fc8SFlorian Westphal sock_put(sk); 2176e16163b6SPaolo Abeni } 2177e16163b6SPaolo Abeni 21783b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 21793b1d6210SPaolo Abeni * level. 21803b1d6210SPaolo Abeni * 21813b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 21823b1d6210SPaolo Abeni */ 2183ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 21843b1d6210SPaolo Abeni { 218571b7dec2SPaolo Abeni struct sock *backup = NULL, *pick = NULL; 21863b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 218771b7dec2SPaolo Abeni int min_stale_count = INT_MAX; 21883b1d6210SPaolo Abeni 21893b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 21903b1d6210SPaolo Abeni 2191d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2192d9ca1de8SPaolo Abeni return NULL; 2193d5f49190SPaolo Abeni 21943b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 21953b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21963b1d6210SPaolo Abeni 2197ff5a0b42SPaolo Abeni if (!__mptcp_subflow_active(subflow)) 2198d5f49190SPaolo Abeni continue; 2199d5f49190SPaolo Abeni 220071b7dec2SPaolo Abeni /* still data outstanding at TCP level? skip this */ 220171b7dec2SPaolo Abeni if (!tcp_rtx_and_write_queues_empty(ssk)) { 220271b7dec2SPaolo Abeni mptcp_pm_subflow_chk_stale(msk, ssk); 220371b7dec2SPaolo Abeni min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2204860975c6SFlorian Westphal continue; 2205860975c6SFlorian Westphal } 22063b1d6210SPaolo Abeni 22073b1d6210SPaolo Abeni if (subflow->backup) { 22083b1d6210SPaolo Abeni if (!backup) 22093b1d6210SPaolo Abeni backup = ssk; 22103b1d6210SPaolo Abeni continue; 22113b1d6210SPaolo Abeni } 22123b1d6210SPaolo Abeni 221371b7dec2SPaolo Abeni if (!pick) 221471b7dec2SPaolo Abeni pick = ssk; 22153b1d6210SPaolo Abeni } 22163b1d6210SPaolo Abeni 221771b7dec2SPaolo Abeni if (pick) 221871b7dec2SPaolo Abeni return pick; 221971b7dec2SPaolo Abeni 222071b7dec2SPaolo Abeni /* use backup only if there are no progresses anywhere */ 222171b7dec2SPaolo Abeni return min_stale_count > 1 ? backup : NULL; 22223b1d6210SPaolo Abeni } 22233b1d6210SPaolo Abeni 222417aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 222517aee05dSFlorian Westphal { 222617aee05dSFlorian Westphal if (msk->subflow) { 222717aee05dSFlorian Westphal iput(SOCK_INODE(msk->subflow)); 222817aee05dSFlorian Westphal msk->subflow = NULL; 222917aee05dSFlorian Westphal } 223017aee05dSFlorian Westphal } 223117aee05dSFlorian Westphal 22321e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk) 22331e1d9d6fSPaolo Abeni { 22341e1d9d6fSPaolo Abeni struct mptcp_data_frag *cur, *rtx_head; 22351e1d9d6fSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 22361e1d9d6fSPaolo Abeni 22371e1d9d6fSPaolo Abeni if (__mptcp_check_fallback(mptcp_sk(sk))) 22381e1d9d6fSPaolo Abeni return false; 22391e1d9d6fSPaolo Abeni 22401e1d9d6fSPaolo Abeni if (tcp_rtx_and_write_queues_empty(sk)) 22411e1d9d6fSPaolo Abeni return false; 22421e1d9d6fSPaolo Abeni 22431e1d9d6fSPaolo Abeni /* the closing socket has some data untransmitted and/or unacked: 22441e1d9d6fSPaolo Abeni * some data in the mptcp rtx queue has not really xmitted yet. 22451e1d9d6fSPaolo Abeni * keep it simple and re-inject the whole mptcp level rtx queue 22461e1d9d6fSPaolo Abeni */ 22471e1d9d6fSPaolo Abeni mptcp_data_lock(sk); 22481e1d9d6fSPaolo Abeni __mptcp_clean_una_wakeup(sk); 22491e1d9d6fSPaolo Abeni rtx_head = mptcp_rtx_head(sk); 22501e1d9d6fSPaolo Abeni if (!rtx_head) { 22511e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22521e1d9d6fSPaolo Abeni return false; 22531e1d9d6fSPaolo Abeni } 22541e1d9d6fSPaolo Abeni 22551e1d9d6fSPaolo Abeni msk->recovery_snd_nxt = msk->snd_nxt; 22561e1d9d6fSPaolo Abeni msk->recovery = true; 22571e1d9d6fSPaolo Abeni mptcp_data_unlock(sk); 22581e1d9d6fSPaolo Abeni 22591e1d9d6fSPaolo Abeni msk->first_pending = rtx_head; 22601e1d9d6fSPaolo Abeni msk->tx_pending_data += msk->snd_nxt - rtx_head->data_seq; 22611e1d9d6fSPaolo Abeni msk->snd_burst = 0; 22621e1d9d6fSPaolo Abeni 22631e1d9d6fSPaolo Abeni /* be sure to clear the "sent status" on all re-injected fragments */ 22641e1d9d6fSPaolo Abeni list_for_each_entry(cur, &msk->rtx_queue, list) { 22651e1d9d6fSPaolo Abeni if (!cur->already_sent) 22661e1d9d6fSPaolo Abeni break; 22671e1d9d6fSPaolo Abeni cur->already_sent = 0; 22681e1d9d6fSPaolo Abeni } 22691e1d9d6fSPaolo Abeni 22701e1d9d6fSPaolo Abeni return true; 22711e1d9d6fSPaolo Abeni } 22721e1d9d6fSPaolo Abeni 2273cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2274cec37a6eSPeter Krystad * (accept). 2275cec37a6eSPeter Krystad * 2276cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2277cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2278cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2279cec37a6eSPeter Krystad * parent socket. 2280cec37a6eSPeter Krystad */ 2281a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2282e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow) 2283cec37a6eSPeter Krystad { 2284e0be4931SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 22851e1d9d6fSPaolo Abeni bool need_push; 2286e0be4931SFlorian Westphal 2287cec37a6eSPeter Krystad list_del(&subflow->node); 2288cec37a6eSPeter Krystad 22893f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2290e16163b6SPaolo Abeni 2291e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2292e16163b6SPaolo Abeni * already orphaned 2293e16163b6SPaolo Abeni */ 2294866f26f2SPaolo Abeni if (ssk->sk_socket) 2295e16163b6SPaolo Abeni sock_orphan(ssk); 2296e16163b6SPaolo Abeni 22971e1d9d6fSPaolo Abeni need_push = __mptcp_retransmit_pending_data(sk); 2298d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2299d7b1bfd0SPaolo Abeni 2300e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2301e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2302e16163b6SPaolo Abeni * reference owned by msk; 2303e16163b6SPaolo Abeni */ 2304e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2305e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2306e16163b6SPaolo Abeni } else { 2307d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2308e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2309e16163b6SPaolo Abeni 2310e16163b6SPaolo Abeni /* close acquired an extra ref */ 2311e16163b6SPaolo Abeni __sock_put(ssk); 2312e16163b6SPaolo Abeni } 2313e16163b6SPaolo Abeni release_sock(ssk); 2314e16163b6SPaolo Abeni 2315e16163b6SPaolo Abeni sock_put(ssk); 2316e0be4931SFlorian Westphal 2317e0be4931SFlorian Westphal if (ssk == msk->last_snd) 2318e0be4931SFlorian Westphal msk->last_snd = NULL; 231917aee05dSFlorian Westphal 2320c8fe62f0SFlorian Westphal if (ssk == msk->first) 2321c8fe62f0SFlorian Westphal msk->first = NULL; 2322c8fe62f0SFlorian Westphal 232317aee05dSFlorian Westphal if (msk->subflow && ssk == msk->subflow->sk) 232417aee05dSFlorian Westphal mptcp_dispose_initial_subflow(msk); 23251e1d9d6fSPaolo Abeni 23261e1d9d6fSPaolo Abeni if (need_push) 23271e1d9d6fSPaolo Abeni __mptcp_push_pending(sk, 0); 2328f870fa0bSMat Martineau } 2329f870fa0bSMat Martineau 2330a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2331a141e02eSFlorian Westphal struct mptcp_subflow_context *subflow) 2332a141e02eSFlorian Westphal { 2333b911c97cSFlorian Westphal if (sk->sk_state == TCP_ESTABLISHED) 2334b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2335a141e02eSFlorian Westphal __mptcp_close_ssk(sk, ssk, subflow); 2336a141e02eSFlorian Westphal } 2337a141e02eSFlorian Westphal 2338dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2339dc24f8b4SPaolo Abeni { 2340dc24f8b4SPaolo Abeni return 0; 2341dc24f8b4SPaolo Abeni } 2342dc24f8b4SPaolo Abeni 23430e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 23440e4f35d7SPaolo Abeni { 23450e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 23460e4f35d7SPaolo Abeni 23473abc05d9SFlorian Westphal might_sleep(); 23483abc05d9SFlorian Westphal 23490e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 23500e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 23510e4f35d7SPaolo Abeni 23520e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 23530e4f35d7SPaolo Abeni continue; 23540e4f35d7SPaolo Abeni 235540947e13SFlorian Westphal /* 'subflow_data_ready' will re-sched once rx queue is empty */ 235640947e13SFlorian Westphal if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 235740947e13SFlorian Westphal continue; 235840947e13SFlorian Westphal 2359a141e02eSFlorian Westphal mptcp_close_ssk((struct sock *)msk, ssk, subflow); 23600e4f35d7SPaolo Abeni } 23610e4f35d7SPaolo Abeni } 23620e4f35d7SPaolo Abeni 2363e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2364e16163b6SPaolo Abeni { 2365e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2366e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2367e16163b6SPaolo Abeni 2368e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2369e16163b6SPaolo Abeni return true; 2370e16163b6SPaolo Abeni 2371e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2372e16163b6SPaolo Abeni * timeout 2373e16163b6SPaolo Abeni */ 2374e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2375e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2376e16163b6SPaolo Abeni TCP_CLOSE) 2377e16163b6SPaolo Abeni return false; 2378e16163b6SPaolo Abeni } 2379e16163b6SPaolo Abeni return true; 2380e16163b6SPaolo Abeni } 2381e16163b6SPaolo Abeni 238250c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 238350c504a2SFlorian Westphal { 238450c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 238550c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 238650c504a2SFlorian Westphal 238750c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 238850c504a2SFlorian Westphal return; 238950c504a2SFlorian Westphal 239050c504a2SFlorian Westphal mptcp_token_destroy(msk); 239150c504a2SFlorian Westphal 239250c504a2SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 239350c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 239475e908c3SPaolo Abeni bool slow; 239550c504a2SFlorian Westphal 239675e908c3SPaolo Abeni slow = lock_sock_fast(tcp_sk); 239750c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 239850c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 239950c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 240050c504a2SFlorian Westphal } 240175e908c3SPaolo Abeni unlock_sock_fast(tcp_sk, slow); 240250c504a2SFlorian Westphal } 240350c504a2SFlorian Westphal 240450c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 240550c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 240650c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 240750c504a2SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 240850c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 240950c504a2SFlorian Westphal 241050c504a2SFlorian Westphal mptcp_close_wake_up(sk); 241150c504a2SFlorian Westphal } 241250c504a2SFlorian Westphal 24132948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk) 241480992017SPaolo Abeni { 24152948d0a1SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2416caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 24173b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 24183b1d6210SPaolo Abeni size_t copied = 0; 24192948d0a1SPaolo Abeni struct sock *ssk; 24202948d0a1SPaolo Abeni int ret; 24212948d0a1SPaolo Abeni 2422b5941f06SPaolo Abeni mptcp_clean_una_wakeup(sk); 24232948d0a1SPaolo Abeni dfrag = mptcp_rtx_head(sk); 24246477dd39SMat Martineau if (!dfrag) { 24256477dd39SMat Martineau if (mptcp_data_fin_enabled(msk)) { 24266477dd39SMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 24276477dd39SMat Martineau 24286477dd39SMat Martineau icsk->icsk_retransmits++; 24296477dd39SMat Martineau mptcp_set_datafin_timeout(sk); 24306477dd39SMat Martineau mptcp_send_ack(msk); 24316477dd39SMat Martineau 24326477dd39SMat Martineau goto reset_timer; 24336477dd39SMat Martineau } 24346477dd39SMat Martineau 24352948d0a1SPaolo Abeni return; 24366477dd39SMat Martineau } 24372948d0a1SPaolo Abeni 24382948d0a1SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 24392948d0a1SPaolo Abeni if (!ssk) 24402948d0a1SPaolo Abeni goto reset_timer; 24412948d0a1SPaolo Abeni 24422948d0a1SPaolo Abeni lock_sock(ssk); 24432948d0a1SPaolo Abeni 24442948d0a1SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 24452948d0a1SPaolo Abeni info.sent = 0; 24464e14867dSPaolo Abeni info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 24474e14867dSPaolo Abeni while (info.sent < info.limit) { 24482948d0a1SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 24492948d0a1SPaolo Abeni if (ret <= 0) 24502948d0a1SPaolo Abeni break; 24512948d0a1SPaolo Abeni 24522948d0a1SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 24532948d0a1SPaolo Abeni copied += ret; 24542948d0a1SPaolo Abeni info.sent += ret; 24552948d0a1SPaolo Abeni } 24564e14867dSPaolo Abeni if (copied) { 24574e14867dSPaolo Abeni dfrag->already_sent = max(dfrag->already_sent, info.sent); 24582948d0a1SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 24592948d0a1SPaolo Abeni info.size_goal); 24604e14867dSPaolo Abeni } 24612948d0a1SPaolo Abeni 24622948d0a1SPaolo Abeni release_sock(ssk); 24632948d0a1SPaolo Abeni 24642948d0a1SPaolo Abeni reset_timer: 24652948d0a1SPaolo Abeni if (!mptcp_timer_pending(sk)) 24662948d0a1SPaolo Abeni mptcp_reset_timer(sk); 24672948d0a1SPaolo Abeni } 24682948d0a1SPaolo Abeni 24692948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work) 24702948d0a1SPaolo Abeni { 24712948d0a1SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 24722948d0a1SPaolo Abeni struct sock *sk = &msk->sk.icsk_inet.sk; 24732948d0a1SPaolo Abeni int state; 247480992017SPaolo Abeni 247580992017SPaolo Abeni lock_sock(sk); 2476e16163b6SPaolo Abeni state = sk->sk_state; 2477e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2478e16163b6SPaolo Abeni goto unlock; 2479e16163b6SPaolo Abeni 248043b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 248178962489SFlorian Westphal mptcp_flush_join_list(msk); 248250c504a2SFlorian Westphal 248350c504a2SFlorian Westphal mptcp_check_fastclose(msk); 248450c504a2SFlorian Westphal 2485b416268bSFlorian Westphal if (msk->pm.status) 2486e9801430SFlorian Westphal mptcp_pm_nl_work(msk); 2487b416268bSFlorian Westphal 248859832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 248959832e24SFlorian Westphal mptcp_check_for_eof(msk); 249059832e24SFlorian Westphal 24916e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 249243b54c6eSMat Martineau mptcp_check_data_fin(sk); 249343b54c6eSMat Martineau 2494341c6524SPaolo Abeni /* There is no point in keeping around an orphaned sk timedout or 2495341c6524SPaolo Abeni * closed, but we need the msk around to reply to incoming DATA_FIN, 2496341c6524SPaolo Abeni * even if it is orphaned and in FIN_WAIT2 state 2497e16163b6SPaolo Abeni */ 2498e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2499341c6524SPaolo Abeni (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2500e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2501e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2502e16163b6SPaolo Abeni goto unlock; 2503e16163b6SPaolo Abeni } 2504e16163b6SPaolo Abeni 2505b263b0d7SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2506b263b0d7SFlorian Westphal __mptcp_close_subflow(msk); 2507b263b0d7SFlorian Westphal 25082948d0a1SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 25092948d0a1SPaolo Abeni __mptcp_retrans(sk); 25103b1d6210SPaolo Abeni 25113b1d6210SPaolo Abeni unlock: 251280992017SPaolo Abeni release_sock(sk); 251380992017SPaolo Abeni sock_put(sk); 251480992017SPaolo Abeni } 251580992017SPaolo Abeni 2516784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2517f870fa0bSMat Martineau { 2518cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2519cec37a6eSPeter Krystad 2520ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 2521ec3edaa7SPeter Krystad 2522cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2523ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 252418b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 252580992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 252687952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2527ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2528f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 2529e93da928SPaolo Abeni msk->wmem_reserved = 0; 2530ce599c51SPaolo Abeni WRITE_ONCE(msk->rmem_released, 0); 2531724cfd2eSPaolo Abeni msk->tx_pending_data = 0; 253233d41c9cSPaolo Abeni msk->timer_ival = TCP_RTO_MIN; 2533cec37a6eSPeter Krystad 25348ab183deSPaolo Abeni msk->first = NULL; 2535dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2536752e9067SGeliang Tang WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 25371e1d9d6fSPaolo Abeni msk->recovery = false; 25388ab183deSPaolo Abeni 25391b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 25401b1c7a0eSPeter Krystad 2541b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2542b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2543e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2544aa1fbd94SFlorian Westphal 2545f870fa0bSMat Martineau return 0; 2546f870fa0bSMat Martineau } 2547f870fa0bSMat Martineau 2548784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 2549784325e9SMatthieu Baerts { 255020b5759fSPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 2551fc518953SFlorian Westphal struct net *net = sock_net(sk); 2552fc518953SFlorian Westphal int ret; 255318b683bfSPaolo Abeni 2554b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2555b6c08380SGeliang Tang if (ret) 2556b6c08380SGeliang Tang return ret; 2557b6c08380SGeliang Tang 2558fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2559fc518953SFlorian Westphal return -ENOPROTOOPT; 2560fc518953SFlorian Westphal 2561fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2562fc518953SFlorian Westphal return -ENOMEM; 2563fc518953SFlorian Westphal 2564fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2565fa68018dSPaolo Abeni if (ret) 2566fa68018dSPaolo Abeni return ret; 2567fa68018dSPaolo Abeni 256820b5759fSPaolo Abeni /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 256920b5759fSPaolo Abeni * propagate the correct value 257020b5759fSPaolo Abeni */ 257120b5759fSPaolo Abeni tcp_assign_congestion_control(sk); 257220b5759fSPaolo Abeni strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 257320b5759fSPaolo Abeni 257420b5759fSPaolo Abeni /* no need to keep a reference to the ops, the name will suffice */ 257520b5759fSPaolo Abeni tcp_cleanup_congestion_control(sk); 257620b5759fSPaolo Abeni icsk->icsk_ca_ops = NULL; 257720b5759fSPaolo Abeni 2578d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2579a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2580da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2581d027236cSPaolo Abeni 258218b683bfSPaolo Abeni return 0; 258318b683bfSPaolo Abeni } 258418b683bfSPaolo Abeni 258518b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 258618b683bfSPaolo Abeni { 258718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 258818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 258918b683bfSPaolo Abeni 2590d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 259118b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2592d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2593784325e9SMatthieu Baerts } 2594784325e9SMatthieu Baerts 259580992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 259680992017SPaolo Abeni { 259780992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 259880992017SPaolo Abeni 2599b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2600e16163b6SPaolo Abeni __sock_put(sk); 260180992017SPaolo Abeni } 260280992017SPaolo Abeni 2603d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 260421498490SPeter Krystad { 260521498490SPeter Krystad lock_sock(ssk); 260621498490SPeter Krystad 260721498490SPeter Krystad switch (ssk->sk_state) { 260821498490SPeter Krystad case TCP_LISTEN: 260921498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 261021498490SPeter Krystad break; 2611df561f66SGustavo A. R. Silva fallthrough; 261221498490SPeter Krystad case TCP_SYN_SENT: 261321498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 261421498490SPeter Krystad break; 261521498490SPeter Krystad default: 261643b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 261743b54c6eSMat Martineau pr_debug("Fallback"); 261821498490SPeter Krystad ssk->sk_shutdown |= how; 261921498490SPeter Krystad tcp_shutdown(ssk, how); 262043b54c6eSMat Martineau } else { 262143b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 262243b54c6eSMat Martineau tcp_send_ack(ssk); 26236477dd39SMat Martineau if (!mptcp_timer_pending(sk)) 26246477dd39SMat Martineau mptcp_reset_timer(sk); 262543b54c6eSMat Martineau } 262621498490SPeter Krystad break; 262721498490SPeter Krystad } 262821498490SPeter Krystad 262921498490SPeter Krystad release_sock(ssk); 263021498490SPeter Krystad } 263121498490SPeter Krystad 26326920b851SMat Martineau static const unsigned char new_state[16] = { 26336920b851SMat Martineau /* current state: new state: action: */ 26346920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 26356920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26366920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 26376920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 26386920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 26396920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 26406920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 26416920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 26426920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 26436920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 26446920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 26456920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 26466920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 26476920b851SMat Martineau }; 26486920b851SMat Martineau 26496920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 26506920b851SMat Martineau { 26516920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 26526920b851SMat Martineau int ns = next & TCP_STATE_MASK; 26536920b851SMat Martineau 26546920b851SMat Martineau inet_sk_state_store(sk, ns); 26556920b851SMat Martineau 26566920b851SMat Martineau return next & TCP_ACTION_FIN; 26576920b851SMat Martineau } 26586920b851SMat Martineau 2659e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2660e16163b6SPaolo Abeni { 2661e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2662e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2663e16163b6SPaolo Abeni 2664e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2665e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2666e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2667e16163b6SPaolo Abeni 2668e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2669e16163b6SPaolo Abeni * skip this 2670e16163b6SPaolo Abeni */ 2671e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2672e16163b6SPaolo Abeni mptcp_send_head(sk)) 2673e16163b6SPaolo Abeni return; 2674e16163b6SPaolo Abeni 2675e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2676e16163b6SPaolo Abeni 267726aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 267826aa2314SPaolo Abeni * state now 267926aa2314SPaolo Abeni */ 268026aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 268126aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2682e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2683e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 268426aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 268526aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 268626aa2314SPaolo Abeni } 2687e16163b6SPaolo Abeni } 2688e16163b6SPaolo Abeni 268978962489SFlorian Westphal mptcp_flush_join_list(msk); 2690e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2691e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2692e16163b6SPaolo Abeni 2693e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2694e16163b6SPaolo Abeni } 2695e16163b6SPaolo Abeni } 2696e16163b6SPaolo Abeni 2697e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2698e16163b6SPaolo Abeni { 2699e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2700e16163b6SPaolo Abeni 2701e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2702e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2703e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2704e16163b6SPaolo Abeni 2705e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2706e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2707e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2708e16163b6SPaolo Abeni 2709e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2710e16163b6SPaolo Abeni } 2711e16163b6SPaolo Abeni 2712e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2713f870fa0bSMat Martineau { 2714cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2715f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2716b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2717f870fa0bSMat Martineau 2718e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 27192c22c06cSFlorian Westphal 27203abc05d9SFlorian Westphal might_sleep(); 27213abc05d9SFlorian Westphal 272210f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 272310f6d46cSPaolo Abeni * mptcp_finish_join(). 272410f6d46cSPaolo Abeni */ 272510f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 272610f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 272710f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2728b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2729b2c5b614SFlorian Westphal 27306e628cd3SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2731e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2732e16163b6SPaolo Abeni msk->pm.status = 0; 2733b2c5b614SFlorian Westphal 2734b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2735cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2736e16163b6SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow); 2737f870fa0bSMat Martineau } 2738f870fa0bSMat Martineau 2739e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2740e16163b6SPaolo Abeni 2741e93da928SPaolo Abeni WARN_ON_ONCE(msk->wmem_reserved); 274287952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2743e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2744e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2745aa1fbd94SFlorian Westphal 2746e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 274717aee05dSFlorian Westphal mptcp_dispose_initial_subflow(msk); 2748e16163b6SPaolo Abeni sock_put(sk); 2749e16163b6SPaolo Abeni } 2750e16163b6SPaolo Abeni 2751e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2752e16163b6SPaolo Abeni { 2753e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2754e16163b6SPaolo Abeni bool do_cancel_work = false; 2755e16163b6SPaolo Abeni 2756e16163b6SPaolo Abeni lock_sock(sk); 2757e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2758e16163b6SPaolo Abeni 2759e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2760e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2761e16163b6SPaolo Abeni goto cleanup; 2762e16163b6SPaolo Abeni } 2763e16163b6SPaolo Abeni 2764e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2765e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2766e16163b6SPaolo Abeni 2767e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2768e16163b6SPaolo Abeni 2769e16163b6SPaolo Abeni cleanup: 2770e16163b6SPaolo Abeni /* orphan all the subflows */ 2771e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 277244227915SGeliang Tang mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2773e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2774866f26f2SPaolo Abeni bool slow = lock_sock_fast(ssk); 2775e16163b6SPaolo Abeni 2776e16163b6SPaolo Abeni sock_orphan(ssk); 2777e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2778e16163b6SPaolo Abeni } 2779e16163b6SPaolo Abeni sock_orphan(sk); 2780e16163b6SPaolo Abeni 2781e16163b6SPaolo Abeni sock_hold(sk); 2782e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2783e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2784e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2785e16163b6SPaolo Abeni do_cancel_work = true; 2786e16163b6SPaolo Abeni } else { 2787e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2788e16163b6SPaolo Abeni } 2789e16163b6SPaolo Abeni release_sock(sk); 2790e16163b6SPaolo Abeni if (do_cancel_work) 279180992017SPaolo Abeni mptcp_cancel_work(sk); 2792b911c97cSFlorian Westphal 2793b911c97cSFlorian Westphal if (mptcp_sk(sk)->token) 2794b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2795b911c97cSFlorian Westphal 2796e16163b6SPaolo Abeni sock_put(sk); 2797f870fa0bSMat Martineau } 2798f870fa0bSMat Martineau 2799cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2800cf7da0d6SPeter Krystad { 2801cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2802cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2803cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2804cf7da0d6SPeter Krystad 2805cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2806cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2807cf7da0d6SPeter Krystad 2808cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2809cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2810cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2811cf7da0d6SPeter Krystad } 2812cf7da0d6SPeter Krystad #endif 2813cf7da0d6SPeter Krystad 2814cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2815cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2816cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2817cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2818cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2819cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2820cf7da0d6SPeter Krystad } 2821cf7da0d6SPeter Krystad 282218b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 282318b683bfSPaolo Abeni { 282476e2a55dSPaolo Abeni struct mptcp_subflow_context *subflow; 282576e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 282676e2a55dSPaolo Abeni 282778962489SFlorian Westphal mptcp_do_flush_join_list(msk); 282878962489SFlorian Westphal 282913a9499eSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 283013a9499eSPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 283113a9499eSPaolo Abeni 283213a9499eSPaolo Abeni lock_sock(ssk); 283313a9499eSPaolo Abeni tcp_disconnect(ssk, flags); 283413a9499eSPaolo Abeni release_sock(ssk); 283513a9499eSPaolo Abeni } 283642c556feSFlorian Westphal return 0; 283718b683bfSPaolo Abeni } 283818b683bfSPaolo Abeni 2839b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2840b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2841b0519de8SFlorian Westphal { 2842b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2843b0519de8SFlorian Westphal 2844b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2845b0519de8SFlorian Westphal } 2846b0519de8SFlorian Westphal #endif 2847b0519de8SFlorian Westphal 2848fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2849cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2850fca5c82cSPaolo Abeni struct request_sock *req) 2851b0519de8SFlorian Westphal { 285258b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2853b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 285458b09919SPaolo Abeni struct mptcp_sock *msk; 285558b09919SPaolo Abeni u64 ack_seq; 2856b0519de8SFlorian Westphal 2857b0519de8SFlorian Westphal if (!nsk) 2858b0519de8SFlorian Westphal return NULL; 2859b0519de8SFlorian Westphal 2860b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2861b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2862b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2863b0519de8SFlorian Westphal #endif 2864b0519de8SFlorian Westphal 286558b09919SPaolo Abeni __mptcp_init_sock(nsk); 286658b09919SPaolo Abeni 286758b09919SPaolo Abeni msk = mptcp_sk(nsk); 286858b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 286958b09919SPaolo Abeni msk->token = subflow_req->token; 287058b09919SPaolo Abeni msk->subflow = NULL; 2871b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 287274c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 287306251181SGeliang Tang WRITE_ONCE(msk->csum_enabled, true); 287458b09919SPaolo Abeni 287558b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2876eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 28777439d687SPaolo Abeni msk->snd_una = msk->write_seq; 28787439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2879df00b087SFlorian Westphal msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 28806f8a612aSFlorian Westphal 288174c7dfbeSPaolo Abeni if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 288258b09919SPaolo Abeni msk->can_ack = true; 2883cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 288458b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 288558b09919SPaolo Abeni ack_seq++; 2886917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2887fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 288858b09919SPaolo Abeni } 28897f20d5fcSPaolo Abeni 28905e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 28917f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 28927f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 28930c148460SPaolo Abeni 28940c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 289558b09919SPaolo Abeni bh_unlock_sock(nsk); 289658b09919SPaolo Abeni 289758b09919SPaolo Abeni /* keep a single reference */ 289858b09919SPaolo Abeni __sock_put(nsk); 2899b0519de8SFlorian Westphal return nsk; 2900b0519de8SFlorian Westphal } 2901b0519de8SFlorian Westphal 2902a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2903a6b118feSFlorian Westphal { 2904a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2905a6b118feSFlorian Westphal 2906a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2907a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2908a6b118feSFlorian Westphal 2909a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2910a6b118feSFlorian Westphal 2911a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2912a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2913a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2914a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2915a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 29166f8a612aSFlorian Westphal 29177439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2918a6b118feSFlorian Westphal } 2919a6b118feSFlorian Westphal 2920cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2921cf7da0d6SPeter Krystad bool kern) 2922cf7da0d6SPeter Krystad { 2923cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2924cf7da0d6SPeter Krystad struct socket *listener; 2925cf7da0d6SPeter Krystad struct sock *newsk; 2926cf7da0d6SPeter Krystad 2927cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2928cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2929cf7da0d6SPeter Krystad *err = -EINVAL; 2930cf7da0d6SPeter Krystad return NULL; 2931cf7da0d6SPeter Krystad } 2932cf7da0d6SPeter Krystad 2933cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2934cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2935cf7da0d6SPeter Krystad if (!newsk) 2936cf7da0d6SPeter Krystad return NULL; 2937cf7da0d6SPeter Krystad 2938cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2939cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2940cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2941cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2942cf7da0d6SPeter Krystad 2943cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 294458b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 294558b09919SPaolo Abeni 294658b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 294758b09919SPaolo Abeni * subflow_syn_recv_sock() 294858b09919SPaolo Abeni */ 294958b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 295058b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 295158b09919SPaolo Abeni return newsk; 295258b09919SPaolo Abeni } 295358b09919SPaolo Abeni 295458b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 295558b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2956cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 29570397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2958fc518953SFlorian Westphal } else { 2959fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2960fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2961cf7da0d6SPeter Krystad } 2962cf7da0d6SPeter Krystad 2963cf7da0d6SPeter Krystad return newsk; 2964cf7da0d6SPeter Krystad } 2965cf7da0d6SPeter Krystad 29665c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 29675c8c1640SGeliang Tang { 296887952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 296987952603SPaolo Abeni 29706e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 29716e628cd3SPaolo Abeni 297287952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 297387952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 297487952603SPaolo Abeni 29755c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 29765c8c1640SGeliang Tang mptcp_token_destroy(msk); 29775c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 29785c8c1640SGeliang Tang } 29795c8c1640SGeliang Tang 298079c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 298179c0949eSPeter Krystad { 2982c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2983c9fd9c5fSFlorian Westphal 29845c8c1640SGeliang Tang mptcp_destroy_common(msk); 2985d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 298679c0949eSPeter Krystad } 298779c0949eSPeter Krystad 29886e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 29896e628cd3SPaolo Abeni { 29906e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 29916e628cd3SPaolo Abeni __mptcp_clean_una(sk); 29926e628cd3SPaolo Abeni else 29936e628cd3SPaolo Abeni set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 29946e628cd3SPaolo Abeni 29956e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 29966e628cd3SPaolo Abeni mptcp_schedule_work(sk); 29976e628cd3SPaolo Abeni } 29986e628cd3SPaolo Abeni 2999219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 30006e628cd3SPaolo Abeni { 30016e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 30026e628cd3SPaolo Abeni return; 30036e628cd3SPaolo Abeni 300440dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 3005b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 3006b19bc294SPaolo Abeni 3007b19bc294SPaolo Abeni if (xmit_ssk == ssk) 30086e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3009b19bc294SPaolo Abeni else if (xmit_ssk) 3010b19bc294SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 301140dc9416SPaolo Abeni } else { 30126e628cd3SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 30136e628cd3SPaolo Abeni } 301440dc9416SPaolo Abeni } 30156e628cd3SPaolo Abeni 3016e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 301714c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 301814c441b5SPaolo Abeni { 3019c2e6048fSPaolo Abeni for (;;) { 30202d6f5a2bSPaolo Abeni unsigned long flags = 0; 30212d6f5a2bSPaolo Abeni 3022c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 30232e5de7e0SDan Carpenter flags |= BIT(MPTCP_PUSH_PENDING); 30242d6f5a2bSPaolo Abeni if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) 30252d6f5a2bSPaolo Abeni flags |= BIT(MPTCP_RETRANSMIT); 3026c2e6048fSPaolo Abeni if (!flags) 3027c2e6048fSPaolo Abeni break; 3028c2e6048fSPaolo Abeni 3029c2e6048fSPaolo Abeni /* the following actions acquire the subflow socket lock 30306e628cd3SPaolo Abeni * 30316e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 30326e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 30336e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 30346e628cd3SPaolo Abeni * the subflow socket lock 30356e628cd3SPaolo Abeni */ 30366e628cd3SPaolo Abeni 30376e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 30382e5de7e0SDan Carpenter if (flags & BIT(MPTCP_PUSH_PENDING)) 3039c2e6048fSPaolo Abeni __mptcp_push_pending(sk, 0); 30402d6f5a2bSPaolo Abeni if (flags & BIT(MPTCP_RETRANSMIT)) 30412d6f5a2bSPaolo Abeni __mptcp_retrans(sk); 3042c2e6048fSPaolo Abeni 3043c2e6048fSPaolo Abeni cond_resched(); 30446e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 30456e628cd3SPaolo Abeni } 3046c2e6048fSPaolo Abeni 3047490274b4SPaolo Abeni /* be sure to set the current sk state before tacking actions 3048490274b4SPaolo Abeni * depending on sk_state 3049490274b4SPaolo Abeni */ 3050490274b4SPaolo Abeni if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags)) 3051490274b4SPaolo Abeni __mptcp_set_connected(sk); 3052c2e6048fSPaolo Abeni if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 3053417789dfSPaolo Abeni __mptcp_clean_una_wakeup(sk); 305415cc1045SPaolo Abeni if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) 305515cc1045SPaolo Abeni __mptcp_error_report(sk); 30566e628cd3SPaolo Abeni 3057c2e6048fSPaolo Abeni /* push_pending may touch wmem_reserved, ensure we do the cleanup 3058c2e6048fSPaolo Abeni * later 3059c2e6048fSPaolo Abeni */ 3060e93da928SPaolo Abeni __mptcp_update_wmem(sk); 306187952603SPaolo Abeni __mptcp_update_rmem(sk); 306214c441b5SPaolo Abeni } 306314c441b5SPaolo Abeni 3064b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 3065b19bc294SPaolo Abeni { 3066b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3067b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 3068b19bc294SPaolo Abeni 3069b19bc294SPaolo Abeni mptcp_data_lock(sk); 3070b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 3071b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 3072b19bc294SPaolo Abeni else 3073b19bc294SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 3074b19bc294SPaolo Abeni mptcp_data_unlock(sk); 3075b19bc294SPaolo Abeni mptcp_subflow_delegated_done(subflow); 3076b19bc294SPaolo Abeni } 3077b19bc294SPaolo Abeni 30782c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 30792c5ebd00SPaolo Abeni { 30802c5ebd00SPaolo Abeni /* should never be called, 30812c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 30822c5ebd00SPaolo Abeni */ 30832c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 30842c5ebd00SPaolo Abeni return 0; 30852c5ebd00SPaolo Abeni } 30862c5ebd00SPaolo Abeni 30872c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 30882c5ebd00SPaolo Abeni { 30892c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 30902c5ebd00SPaolo Abeni } 30912c5ebd00SPaolo Abeni 3092cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3093f870fa0bSMat Martineau { 3094f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3095cec37a6eSPeter Krystad struct socket *ssock; 3096f870fa0bSMat Martineau 3097cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3098cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3099cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3100cec37a6eSPeter Krystad return -EINVAL; 3101f870fa0bSMat Martineau 3102cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3103cec37a6eSPeter Krystad } 3104f870fa0bSMat Martineau 3105cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3106cec37a6eSPeter Krystad { 3107cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3108cec37a6eSPeter Krystad struct mptcp_sock *msk; 3109cec37a6eSPeter Krystad struct sock *sk; 31106d0060f6SMat Martineau u64 ack_seq; 3111f870fa0bSMat Martineau 3112cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3113cec37a6eSPeter Krystad sk = subflow->conn; 3114cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3115cec37a6eSPeter Krystad 3116648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3117648ef4b8SMat Martineau 31186d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 31196d0060f6SMat Martineau ack_seq++; 3120648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3121648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 31226d0060f6SMat Martineau 3123cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3124cec37a6eSPeter Krystad * accessing the field below 3125cec37a6eSPeter Krystad */ 3126cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3127cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 31286d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3129eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 31306d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3131fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3132d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 31337439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 31341b1c7a0eSPeter Krystad 31356c714f1bSFlorian Westphal mptcp_pm_new_connection(msk, ssk, 0); 3136a6b118feSFlorian Westphal 3137a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3138f870fa0bSMat Martineau } 3139f870fa0bSMat Martineau 3140866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3141cf7da0d6SPeter Krystad { 3142cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3143cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3144cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3145cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3146cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3147cf7da0d6SPeter Krystad } 3148cf7da0d6SPeter Krystad 3149e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3150f296234cSPeter Krystad { 3151e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3152f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3153f296234cSPeter Krystad struct sock *parent = (void *)msk; 3154f296234cSPeter Krystad struct socket *parent_sock; 3155ec3edaa7SPeter Krystad bool ret; 3156f296234cSPeter Krystad 3157f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3158f296234cSPeter Krystad 3159f296234cSPeter Krystad /* mptcp socket already closing? */ 3160dc87efdbSFlorian Westphal if (!mptcp_is_fully_established(parent)) { 3161dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EMPTCP; 3162f296234cSPeter Krystad return false; 3163dc87efdbSFlorian Westphal } 3164f296234cSPeter Krystad 3165f296234cSPeter Krystad if (!msk->pm.server_side) 3166b911c97cSFlorian Westphal goto out; 3167f296234cSPeter Krystad 3168dc87efdbSFlorian Westphal if (!mptcp_pm_allow_new_subflow(msk)) { 3169dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 317010f6d46cSPaolo Abeni return false; 3171dc87efdbSFlorian Westphal } 317210f6d46cSPaolo Abeni 317310f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 317410f6d46cSPaolo Abeni * msk lock here. 317510f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 3176e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 317710f6d46cSPaolo Abeni */ 317810f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 317910f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3180e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 318110f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3182e16163b6SPaolo Abeni sock_hold(ssk); 3183e16163b6SPaolo Abeni } 318410f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 3185dc87efdbSFlorian Westphal if (!ret) { 3186dc87efdbSFlorian Westphal subflow->reset_reason = MPTCP_RST_EPROHIBIT; 318710f6d46cSPaolo Abeni return false; 3188dc87efdbSFlorian Westphal } 318910f6d46cSPaolo Abeni 319010f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 319110f6d46cSPaolo Abeni * at close time 319210f6d46cSPaolo Abeni */ 3193f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 3194e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 3195e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 3196917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 3197b911c97cSFlorian Westphal out: 3198b911c97cSFlorian Westphal mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 319910f6d46cSPaolo Abeni return true; 3200f296234cSPeter Krystad } 3201f296234cSPeter Krystad 320276e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 320376e2a55dSPaolo Abeni { 320476e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 320576e2a55dSPaolo Abeni 320676e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 320776e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 320876e2a55dSPaolo Abeni } 320976e2a55dSPaolo Abeni 3210f870fa0bSMat Martineau static struct proto mptcp_prot = { 3211f870fa0bSMat Martineau .name = "MPTCP", 3212f870fa0bSMat Martineau .owner = THIS_MODULE, 3213f870fa0bSMat Martineau .init = mptcp_init_sock, 321418b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3215f870fa0bSMat Martineau .close = mptcp_close, 3216cf7da0d6SPeter Krystad .accept = mptcp_accept, 3217717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3218717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 321976e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 322079c0949eSPeter Krystad .destroy = mptcp_destroy, 3221f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3222f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 322314c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 32242c5ebd00SPaolo Abeni .hash = mptcp_hash, 32252c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3226cec37a6eSPeter Krystad .get_port = mptcp_get_port, 3227d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 3228d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 3229d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3230d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3231989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3232d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3233f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 32342c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3235f870fa0bSMat Martineau .no_autobind = true, 3236f870fa0bSMat Martineau }; 3237f870fa0bSMat Martineau 32382303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 32392303f994SPeter Krystad { 32402303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 32412303f994SPeter Krystad struct socket *ssock; 3242cf7da0d6SPeter Krystad int err; 32432303f994SPeter Krystad 32442303f994SPeter Krystad lock_sock(sock->sk); 3245fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3246fa68018dSPaolo Abeni if (!ssock) { 3247fa68018dSPaolo Abeni err = -EINVAL; 32482303f994SPeter Krystad goto unlock; 32492303f994SPeter Krystad } 32502303f994SPeter Krystad 32512303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3252cf7da0d6SPeter Krystad if (!err) 3253cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 32542303f994SPeter Krystad 32552303f994SPeter Krystad unlock: 32562303f994SPeter Krystad release_sock(sock->sk); 32572303f994SPeter Krystad return err; 32582303f994SPeter Krystad } 32592303f994SPeter Krystad 32600235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 32610235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 32620235d075SPaolo Abeni { 32630235d075SPaolo Abeni subflow->request_mptcp = 0; 32640235d075SPaolo Abeni __mptcp_do_fallback(msk); 32650235d075SPaolo Abeni } 32660235d075SPaolo Abeni 32672303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 32682303f994SPeter Krystad int addr_len, int flags) 32692303f994SPeter Krystad { 32702303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 32712c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 32722303f994SPeter Krystad struct socket *ssock; 32732303f994SPeter Krystad int err; 32742303f994SPeter Krystad 32752303f994SPeter Krystad lock_sock(sock->sk); 327641be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 327741be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 327841be81a8SPaolo Abeni * cope with that 327941be81a8SPaolo Abeni */ 328041be81a8SPaolo Abeni ssock = msk->subflow; 328141be81a8SPaolo Abeni goto do_connect; 328241be81a8SPaolo Abeni } 328341be81a8SPaolo Abeni 3284fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3285fa68018dSPaolo Abeni if (!ssock) { 3286fa68018dSPaolo Abeni err = -EINVAL; 32872303f994SPeter Krystad goto unlock; 32882303f994SPeter Krystad } 32892303f994SPeter Krystad 3290fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3291fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 32922c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3293cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3294cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3295cf7da0d6SPeter Krystad * TCP option space. 3296cf7da0d6SPeter Krystad */ 3297cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 32980235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3299cf7da0d6SPeter Krystad #endif 3300a16195e3SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3301a16195e3SPaolo Abeni MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 33020235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3303a16195e3SPaolo Abeni } 33045695eb88SPaolo Abeni if (likely(!__mptcp_check_fallback(msk))) 33055695eb88SPaolo Abeni MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3306cf7da0d6SPeter Krystad 330741be81a8SPaolo Abeni do_connect: 33082303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 330941be81a8SPaolo Abeni sock->state = ssock->state; 331041be81a8SPaolo Abeni 331141be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 331241be81a8SPaolo Abeni * subflow_finish_connect() 331341be81a8SPaolo Abeni */ 3314367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3315cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 331641be81a8SPaolo Abeni else 331741be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 33182303f994SPeter Krystad 33192303f994SPeter Krystad unlock: 33202303f994SPeter Krystad release_sock(sock->sk); 33212303f994SPeter Krystad return err; 33222303f994SPeter Krystad } 33232303f994SPeter Krystad 3324cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3325cf7da0d6SPeter Krystad { 3326cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3327cf7da0d6SPeter Krystad struct socket *ssock; 3328cf7da0d6SPeter Krystad int err; 3329cf7da0d6SPeter Krystad 3330cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3331cf7da0d6SPeter Krystad 3332cf7da0d6SPeter Krystad lock_sock(sock->sk); 3333fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3334fa68018dSPaolo Abeni if (!ssock) { 3335fa68018dSPaolo Abeni err = -EINVAL; 3336cf7da0d6SPeter Krystad goto unlock; 3337cf7da0d6SPeter Krystad } 3338cf7da0d6SPeter Krystad 3339fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3340fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 33415e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 33425e20087dSFlorian Westphal 3343cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3344cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3345cf7da0d6SPeter Krystad if (!err) 3346cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3347cf7da0d6SPeter Krystad 3348cf7da0d6SPeter Krystad unlock: 3349cf7da0d6SPeter Krystad release_sock(sock->sk); 3350cf7da0d6SPeter Krystad return err; 3351cf7da0d6SPeter Krystad } 3352cf7da0d6SPeter Krystad 3353cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3354cf7da0d6SPeter Krystad int flags, bool kern) 3355cf7da0d6SPeter Krystad { 3356cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3357cf7da0d6SPeter Krystad struct socket *ssock; 3358cf7da0d6SPeter Krystad int err; 3359cf7da0d6SPeter Krystad 3360cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3361cf7da0d6SPeter Krystad 3362cf7da0d6SPeter Krystad lock_sock(sock->sk); 3363cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 3364cf7da0d6SPeter Krystad goto unlock_fail; 3365cf7da0d6SPeter Krystad 3366cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3367cf7da0d6SPeter Krystad if (!ssock) 3368cf7da0d6SPeter Krystad goto unlock_fail; 3369cf7da0d6SPeter Krystad 33708a05661bSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 3371cf7da0d6SPeter Krystad sock_hold(ssock->sk); 3372cf7da0d6SPeter Krystad release_sock(sock->sk); 3373cf7da0d6SPeter Krystad 3374cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3375d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3376cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3377cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 33780397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 33790397c6d8SPaolo Abeni 33804d54cc32SFlorian Westphal lock_sock(newsk); 33815b950ff4SPaolo Abeni 33825b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 33835b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 33845b950ff4SPaolo Abeni * we can notify it, if needed. 3385c8fe62f0SFlorian Westphal * 3386c8fe62f0SFlorian Westphal * Even if remote has reset the initial subflow by now 3387c8fe62f0SFlorian Westphal * the refcnt is still at least one. 33885b950ff4SPaolo Abeni */ 33895b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 33905b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 33915b950ff4SPaolo Abeni sock_hold(msk->first); 33925b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 33936c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 33945b950ff4SPaolo Abeni 33950397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 33960397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 33975cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3398cf7da0d6SPeter Krystad 3399cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3400cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3401cf7da0d6SPeter Krystad */ 340278962489SFlorian Westphal mptcp_flush_join_list(msk); 3403190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3404cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3405cf7da0d6SPeter Krystad 3406cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3407cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3408cf7da0d6SPeter Krystad } 34094d54cc32SFlorian Westphal release_sock(newsk); 3410cf7da0d6SPeter Krystad } 3411cf7da0d6SPeter Krystad 34128a05661bSPaolo Abeni if (inet_csk_listen_poll(ssock->sk)) 34138a05661bSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 3414cf7da0d6SPeter Krystad sock_put(ssock->sk); 3415cf7da0d6SPeter Krystad return err; 3416cf7da0d6SPeter Krystad 3417cf7da0d6SPeter Krystad unlock_fail: 3418cf7da0d6SPeter Krystad release_sock(sock->sk); 3419cf7da0d6SPeter Krystad return -EINVAL; 3420cf7da0d6SPeter Krystad } 3421cf7da0d6SPeter Krystad 34228a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 34238a05661bSPaolo Abeni { 34248a05661bSPaolo Abeni return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 34258a05661bSPaolo Abeni 0; 34268a05661bSPaolo Abeni } 34278a05661bSPaolo Abeni 34288edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 34298edf0864SFlorian Westphal { 34308edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 34318edf0864SFlorian Westphal 34328edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3433dd913410SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 34348edf0864SFlorian Westphal 34358edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 34368edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 34378edf0864SFlorian Westphal 34385cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 34396e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 34406e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 34416e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 34428edf0864SFlorian Westphal 34436e628cd3SPaolo Abeni return 0; 34448edf0864SFlorian Westphal } 34458edf0864SFlorian Westphal 34462303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 34472303f994SPeter Krystad struct poll_table_struct *wait) 34482303f994SPeter Krystad { 34491891c4a0SFlorian Westphal struct sock *sk = sock->sk; 34508ab183deSPaolo Abeni struct mptcp_sock *msk; 34512303f994SPeter Krystad __poll_t mask = 0; 34528a05661bSPaolo Abeni int state; 34532303f994SPeter Krystad 34541891c4a0SFlorian Westphal msk = mptcp_sk(sk); 34551891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 34561891c4a0SFlorian Westphal 34578a05661bSPaolo Abeni state = inet_sk_state_load(sk); 34586719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 34598a05661bSPaolo Abeni if (state == TCP_LISTEN) 34608a05661bSPaolo Abeni return mptcp_check_readable(msk); 34618a05661bSPaolo Abeni 34628a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 34638a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 34648edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 34658a05661bSPaolo Abeni } 3466dd913410SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3467dd913410SPaolo Abeni mask |= EPOLLHUP; 34681891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 34691891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 34701891c4a0SFlorian Westphal 347115cc1045SPaolo Abeni /* This barrier is coupled with smp_wmb() in tcp_reset() */ 347215cc1045SPaolo Abeni smp_rmb(); 347315cc1045SPaolo Abeni if (sk->sk_err) 347415cc1045SPaolo Abeni mask |= EPOLLERR; 347515cc1045SPaolo Abeni 34762303f994SPeter Krystad return mask; 34772303f994SPeter Krystad } 34782303f994SPeter Krystad 3479e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3480e42f1ac6SFlorian Westphal .family = PF_INET, 3481e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 34820a3cc579SPaolo Abeni .release = inet_release, 3483e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3484e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3485e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3486e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3487d2f77c53SPaolo Abeni .getname = inet_getname, 3488e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3489e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3490e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3491e42f1ac6SFlorian Westphal .listen = mptcp_listen, 349276e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3493e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3494e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3495e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3496e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3497e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3498e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3499e42f1ac6SFlorian Westphal }; 35002303f994SPeter Krystad 3501f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3502f870fa0bSMat Martineau .type = SOCK_STREAM, 3503f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3504f870fa0bSMat Martineau .prot = &mptcp_prot, 35052303f994SPeter Krystad .ops = &mptcp_stream_ops, 35062303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3507f870fa0bSMat Martineau }; 3508f870fa0bSMat Martineau 3509b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3510b19bc294SPaolo Abeni { 3511b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3512b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3513b19bc294SPaolo Abeni int work_done = 0; 3514b19bc294SPaolo Abeni 3515b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3516b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3517b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3518b19bc294SPaolo Abeni 3519b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3520b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3521b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3522b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3523b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3524b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3525b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3526b19bc294SPaolo Abeni * CPU that scheduled it. 3527b19bc294SPaolo Abeni */ 3528b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3529b19bc294SPaolo Abeni sock_put(ssk); 3530b19bc294SPaolo Abeni 3531b19bc294SPaolo Abeni if (++work_done == budget) 3532b19bc294SPaolo Abeni return budget; 3533b19bc294SPaolo Abeni } 3534b19bc294SPaolo Abeni 3535b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3536b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3537b19bc294SPaolo Abeni */ 3538b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3539b19bc294SPaolo Abeni return work_done; 3540b19bc294SPaolo Abeni } 3541b19bc294SPaolo Abeni 3542d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3543f870fa0bSMat Martineau { 3544b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3545b19bc294SPaolo Abeni int cpu; 3546b19bc294SPaolo Abeni 35472303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 35482303f994SPeter Krystad 3549d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3550d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3551d027236cSPaolo Abeni 3552b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3553b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3554b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3555b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 3556b19bc294SPaolo Abeni netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3557b19bc294SPaolo Abeni NAPI_POLL_WEIGHT); 3558b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3559b19bc294SPaolo Abeni } 3560b19bc294SPaolo Abeni 35612303f994SPeter Krystad mptcp_subflow_init(); 35621b1c7a0eSPeter Krystad mptcp_pm_init(); 35632c5ebd00SPaolo Abeni mptcp_token_init(); 35642303f994SPeter Krystad 3565f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3566f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3567f870fa0bSMat Martineau 3568f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 35696771bfd9SFlorian Westphal 35706771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3571f870fa0bSMat Martineau } 3572f870fa0bSMat Martineau 3573f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3574e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3575e42f1ac6SFlorian Westphal .family = PF_INET6, 3576e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 35770a3cc579SPaolo Abeni .release = inet6_release, 3578e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3579e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3580e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3581e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3582d2f77c53SPaolo Abeni .getname = inet6_getname, 3583e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3584e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3585e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3586e42f1ac6SFlorian Westphal .listen = mptcp_listen, 358776e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3588e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3589e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3590e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3591e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3592e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3593e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3594e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 35953986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3596e42f1ac6SFlorian Westphal #endif 3597e42f1ac6SFlorian Westphal }; 3598e42f1ac6SFlorian Westphal 3599f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3600f870fa0bSMat Martineau 360179c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 360279c0949eSPeter Krystad { 360379c0949eSPeter Krystad mptcp_destroy(sk); 360479c0949eSPeter Krystad inet6_destroy_sock(sk); 360579c0949eSPeter Krystad } 360679c0949eSPeter Krystad 3607f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3608f870fa0bSMat Martineau .type = SOCK_STREAM, 3609f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3610f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 36112303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3612f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3613f870fa0bSMat Martineau }; 3614f870fa0bSMat Martineau 3615d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3616f870fa0bSMat Martineau { 3617f870fa0bSMat Martineau int err; 3618f870fa0bSMat Martineau 3619f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3620f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3621f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 362279c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3623b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3624f870fa0bSMat Martineau 3625f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3626f870fa0bSMat Martineau if (err) 3627f870fa0bSMat Martineau return err; 3628f870fa0bSMat Martineau 3629f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3630f870fa0bSMat Martineau if (err) 3631f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3632f870fa0bSMat Martineau 3633f870fa0bSMat Martineau return err; 3634f870fa0bSMat Martineau } 3635f870fa0bSMat Martineau #endif 3636