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 28b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 29b0519de8SFlorian Westphal struct mptcp6_sock { 30b0519de8SFlorian Westphal struct mptcp_sock msk; 31b0519de8SFlorian Westphal struct ipv6_pinfo np; 32b0519de8SFlorian Westphal }; 33b0519de8SFlorian Westphal #endif 34b0519de8SFlorian Westphal 356771bfd9SFlorian Westphal struct mptcp_skb_cb { 36ab174ad8SPaolo Abeni u64 map_seq; 37ab174ad8SPaolo Abeni u64 end_seq; 386771bfd9SFlorian Westphal u32 offset; 396771bfd9SFlorian Westphal }; 406771bfd9SFlorian Westphal 416771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 426771bfd9SFlorian Westphal 43d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated; 44d027236cSPaolo Abeni 45e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 46*d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 47e16163b6SPaolo Abeni 482303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 492303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 502303f994SPeter Krystad * Otherwise return NULL. 512303f994SPeter Krystad */ 522303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 532303f994SPeter Krystad { 54d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 552303f994SPeter Krystad return NULL; 562303f994SPeter Krystad 572303f994SPeter Krystad return msk->subflow; 582303f994SPeter Krystad } 592303f994SPeter Krystad 60d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 610b4f33deSFlorian Westphal { 620b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 630b4f33deSFlorian Westphal 640b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 650b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 660b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 670b4f33deSFlorian Westphal * not an mptcp one. 680b4f33deSFlorian Westphal * 690b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 700b4f33deSFlorian Westphal * bypass mptcp. 710b4f33deSFlorian Westphal */ 720b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 73d2f77c53SPaolo Abeni return true; 740b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 750b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 760b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 77d2f77c53SPaolo Abeni return true; 780b4f33deSFlorian Westphal #endif 790b4f33deSFlorian Westphal } 800b4f33deSFlorian Westphal 81d2f77c53SPaolo Abeni return false; 820b4f33deSFlorian Westphal } 830b4f33deSFlorian Westphal 8476660afbSPaolo Abeni static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk) 85cec37a6eSPeter Krystad { 86cec37a6eSPeter Krystad sock_owned_by_me((const struct sock *)msk); 87cec37a6eSPeter Krystad 88e1ff9e82SDavide Caratti if (likely(!__mptcp_check_fallback(msk))) 89cec37a6eSPeter Krystad return NULL; 90cec37a6eSPeter Krystad 9176660afbSPaolo Abeni return msk->first; 928ab183deSPaolo Abeni } 938ab183deSPaolo Abeni 94fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 952303f994SPeter Krystad { 962303f994SPeter Krystad struct mptcp_subflow_context *subflow; 972303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 982303f994SPeter Krystad struct socket *ssock; 992303f994SPeter Krystad int err; 1002303f994SPeter Krystad 1012303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1022303f994SPeter Krystad if (err) 103fa68018dSPaolo Abeni return err; 1042303f994SPeter Krystad 1058ab183deSPaolo Abeni msk->first = ssock->sk; 1062303f994SPeter Krystad msk->subflow = ssock; 1072303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 108cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 109e16163b6SPaolo Abeni sock_hold(ssock->sk); 1102303f994SPeter Krystad subflow->request_mptcp = 1; 1112303f994SPeter Krystad 112e1ff9e82SDavide Caratti /* accept() will wait on first subflow sk_wq, and we always wakes up 113e1ff9e82SDavide Caratti * via msk->sk_socket 114e1ff9e82SDavide Caratti */ 115e1ff9e82SDavide Caratti RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq); 116e1ff9e82SDavide Caratti 117fa68018dSPaolo Abeni return 0; 1182303f994SPeter Krystad } 1192303f994SPeter Krystad 120ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 121ab174ad8SPaolo Abeni { 122ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 123ab174ad8SPaolo Abeni __kfree_skb(skb); 124ab174ad8SPaolo Abeni } 125ab174ad8SPaolo Abeni 1268268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1278268ed4cSPaolo Abeni struct sk_buff *from) 1288268ed4cSPaolo Abeni { 1298268ed4cSPaolo Abeni bool fragstolen; 1308268ed4cSPaolo Abeni int delta; 1318268ed4cSPaolo Abeni 1328268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1338268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1348268ed4cSPaolo Abeni return false; 1358268ed4cSPaolo Abeni 13606242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 13706242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 13806242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 139ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1408268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1418268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1428268ed4cSPaolo Abeni sk_mem_charge(sk, delta); 1438268ed4cSPaolo Abeni return true; 1448268ed4cSPaolo Abeni } 1458268ed4cSPaolo Abeni 146ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 147ab174ad8SPaolo Abeni struct sk_buff *from) 1486771bfd9SFlorian Westphal { 149ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 150ab174ad8SPaolo Abeni return false; 151ab174ad8SPaolo Abeni 152ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 153ab174ad8SPaolo Abeni } 154ab174ad8SPaolo Abeni 155ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 156ab174ad8SPaolo Abeni * - use mptcp seqs 157ab174ad8SPaolo Abeni * - don't cope with sacks 158ab174ad8SPaolo Abeni */ 159ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 160ab174ad8SPaolo Abeni { 161ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 162ab174ad8SPaolo Abeni struct rb_node **p, *parent; 163ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 164ab174ad8SPaolo Abeni struct sk_buff *skb1; 165c2ec6bc0SYe Bin int space; 166ab174ad8SPaolo Abeni 167ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 168ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 169c2ec6bc0SYe Bin space = tcp_space(sk); 170c2ec6bc0SYe Bin max_seq = space > 0 ? space + msk->ack_seq : msk->ack_seq; 171ab174ad8SPaolo Abeni 17206242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 17306242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 174ab174ad8SPaolo Abeni if (after64(seq, max_seq)) { 175ab174ad8SPaolo Abeni /* out of window */ 176ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 17706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 178ab174ad8SPaolo Abeni return; 179ab174ad8SPaolo Abeni } 180ab174ad8SPaolo Abeni 181ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 18206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 183ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 184ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 185ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 186ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 187ab174ad8SPaolo Abeni goto end; 188ab174ad8SPaolo Abeni } 189ab174ad8SPaolo Abeni 190ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 191ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 192ab174ad8SPaolo Abeni */ 19306242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 19406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 19506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 196ab174ad8SPaolo Abeni return; 19706242e44SPaolo Abeni } 198ab174ad8SPaolo Abeni 199ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 200ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 20106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 202ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 203ab174ad8SPaolo Abeni p = &parent->rb_right; 204ab174ad8SPaolo Abeni goto insert; 205ab174ad8SPaolo Abeni } 206ab174ad8SPaolo Abeni 207ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 208ab174ad8SPaolo Abeni parent = NULL; 209ab174ad8SPaolo Abeni while (*p) { 210ab174ad8SPaolo Abeni parent = *p; 211ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 212ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 213ab174ad8SPaolo Abeni p = &parent->rb_left; 214ab174ad8SPaolo Abeni continue; 215ab174ad8SPaolo Abeni } 216ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 217ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 218ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 219ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 22006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 221ab174ad8SPaolo Abeni return; 222ab174ad8SPaolo Abeni } 223ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 224ab174ad8SPaolo Abeni /* partial overlap: 225ab174ad8SPaolo Abeni * | skb | 226ab174ad8SPaolo Abeni * | skb1 | 227ab174ad8SPaolo Abeni * continue traversing 228ab174ad8SPaolo Abeni */ 229ab174ad8SPaolo Abeni } else { 230ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 231ab174ad8SPaolo Abeni * Replace skb1 with skb. 232ab174ad8SPaolo Abeni */ 233ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 234ab174ad8SPaolo Abeni &msk->out_of_order_queue); 235ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 23606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 237ab174ad8SPaolo Abeni goto merge_right; 238ab174ad8SPaolo Abeni } 239ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 24006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 241ab174ad8SPaolo Abeni return; 242ab174ad8SPaolo Abeni } 243ab174ad8SPaolo Abeni p = &parent->rb_right; 244ab174ad8SPaolo Abeni } 24506242e44SPaolo Abeni 246ab174ad8SPaolo Abeni insert: 247ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 248ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 249ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 250ab174ad8SPaolo Abeni 251ab174ad8SPaolo Abeni merge_right: 252ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 253ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 254ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 255ab174ad8SPaolo Abeni break; 256ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 257ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 25806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 259ab174ad8SPaolo Abeni } 260ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 261ab174ad8SPaolo Abeni if (!skb1) 262ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 263ab174ad8SPaolo Abeni 264ab174ad8SPaolo Abeni end: 265ab174ad8SPaolo Abeni skb_condense(skb); 266ab174ad8SPaolo Abeni skb_set_owner_r(skb, sk); 267ab174ad8SPaolo Abeni } 268ab174ad8SPaolo Abeni 269ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 270ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 271ab174ad8SPaolo Abeni size_t copy_len) 272ab174ad8SPaolo Abeni { 273ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2746771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 2754e637c70SFlorian Westphal struct sk_buff *tail; 2766771bfd9SFlorian Westphal 2776771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 2784e637c70SFlorian Westphal 2794e637c70SFlorian Westphal skb_ext_reset(skb); 2804e637c70SFlorian Westphal skb_orphan(skb); 2814e637c70SFlorian Westphal 2829c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 2839c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) { 2849c3f94e1SPaolo Abeni if (ssk->sk_forward_alloc < skb->truesize) 2859c3f94e1SPaolo Abeni goto drop; 2869c3f94e1SPaolo Abeni __sk_mem_reclaim(ssk, skb->truesize); 2879c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) 2889c3f94e1SPaolo Abeni goto drop; 2899c3f94e1SPaolo Abeni } 2909c3f94e1SPaolo Abeni 291ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 292ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 293ab174ad8SPaolo Abeni * value 294ab174ad8SPaolo Abeni */ 295ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 296ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 297ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 298ab174ad8SPaolo Abeni 299ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 300ab174ad8SPaolo Abeni /* in sequence */ 301917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3024e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3038268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 304ab174ad8SPaolo Abeni return true; 3054e637c70SFlorian Westphal 306600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 3076771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 308ab174ad8SPaolo Abeni return true; 309ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 310ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 311ab174ad8SPaolo Abeni return false; 312ab174ad8SPaolo Abeni } 313ab174ad8SPaolo Abeni 314ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 315ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 316ab174ad8SPaolo Abeni */ 31706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3189c3f94e1SPaolo Abeni drop: 319ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 320ab174ad8SPaolo Abeni return false; 3216771bfd9SFlorian Westphal } 3226771bfd9SFlorian Westphal 32316a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 32416a9a9daSMat Martineau { 32516a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 32616a9a9daSMat Martineau 32716a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 32816a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 32916a9a9daSMat Martineau } 33016a9a9daSMat Martineau 331e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 332e16163b6SPaolo Abeni { 333e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 334e16163b6SPaolo Abeni return; 335e16163b6SPaolo Abeni 336e16163b6SPaolo Abeni sk->sk_state_change(sk); 337e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 338e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 339e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 340e16163b6SPaolo Abeni else 341e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 342e16163b6SPaolo Abeni } 343e16163b6SPaolo Abeni 34416a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 34516a9a9daSMat Martineau { 34616a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 34716a9a9daSMat Martineau 34816a9a9daSMat Martineau if (__mptcp_check_fallback(msk)) 34916a9a9daSMat Martineau return; 35016a9a9daSMat Martineau 35116a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 35216a9a9daSMat Martineau if (((1 << sk->sk_state) & 35316a9a9daSMat Martineau (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 35416a9a9daSMat Martineau msk->write_seq == atomic64_read(&msk->snd_una)) { 35516a9a9daSMat Martineau mptcp_stop_timer(sk); 35616a9a9daSMat Martineau 35716a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 35816a9a9daSMat Martineau 35916a9a9daSMat Martineau switch (sk->sk_state) { 36016a9a9daSMat Martineau case TCP_FIN_WAIT1: 36116a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 36216a9a9daSMat Martineau break; 36316a9a9daSMat Martineau case TCP_CLOSING: 36416a9a9daSMat Martineau case TCP_LAST_ACK: 36516a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 36616a9a9daSMat Martineau break; 36716a9a9daSMat Martineau } 36816a9a9daSMat Martineau 369e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 37016a9a9daSMat Martineau } 37116a9a9daSMat Martineau } 37216a9a9daSMat Martineau 3733721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 3743721b9b6SMat Martineau { 3753721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3763721b9b6SMat Martineau 3773721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 3783721b9b6SMat Martineau ((1 << sk->sk_state) & 3793721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 3803721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 3813721b9b6SMat Martineau 3823721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 3833721b9b6SMat Martineau if (seq) 3843721b9b6SMat Martineau *seq = rcv_data_fin_seq; 3853721b9b6SMat Martineau 3863721b9b6SMat Martineau return true; 3873721b9b6SMat Martineau } 3883721b9b6SMat Martineau } 3893721b9b6SMat Martineau 3903721b9b6SMat Martineau return false; 3913721b9b6SMat Martineau } 3923721b9b6SMat Martineau 3933721b9b6SMat Martineau static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 3943721b9b6SMat Martineau { 3953721b9b6SMat Martineau long tout = ssk && inet_csk(ssk)->icsk_pending ? 3963721b9b6SMat Martineau inet_csk(ssk)->icsk_timeout - jiffies : 0; 3973721b9b6SMat Martineau 3983721b9b6SMat Martineau if (tout <= 0) 3993721b9b6SMat Martineau tout = mptcp_sk(sk)->timer_ival; 4003721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4013721b9b6SMat Martineau } 4023721b9b6SMat Martineau 4033721b9b6SMat Martineau static void mptcp_check_data_fin(struct sock *sk) 4043721b9b6SMat Martineau { 4053721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4063721b9b6SMat Martineau u64 rcv_data_fin_seq; 4073721b9b6SMat Martineau 4083721b9b6SMat Martineau if (__mptcp_check_fallback(msk) || !msk->first) 4093721b9b6SMat Martineau return; 4103721b9b6SMat Martineau 4113721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 4123721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 4133721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 4143721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 4153721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 4163721b9b6SMat Martineau * the msk state. 4173721b9b6SMat Martineau * 4183721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 4193721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 4203721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 4213721b9b6SMat Martineau * when catching up. 4223721b9b6SMat Martineau */ 4233721b9b6SMat Martineau 4243721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 4253721b9b6SMat Martineau struct mptcp_subflow_context *subflow; 4263721b9b6SMat Martineau 427917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 4283721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 4293721b9b6SMat Martineau 4303721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 43116a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 43216a9a9daSMat Martineau set_bit(MPTCP_DATA_READY, &msk->flags); 4333721b9b6SMat Martineau 4343721b9b6SMat Martineau switch (sk->sk_state) { 4353721b9b6SMat Martineau case TCP_ESTABLISHED: 4363721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 4373721b9b6SMat Martineau break; 4383721b9b6SMat Martineau case TCP_FIN_WAIT1: 4393721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 4403721b9b6SMat Martineau break; 4413721b9b6SMat Martineau case TCP_FIN_WAIT2: 4423721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 4433721b9b6SMat Martineau break; 4443721b9b6SMat Martineau default: 4453721b9b6SMat Martineau /* Other states not expected */ 4463721b9b6SMat Martineau WARN_ON_ONCE(1); 4473721b9b6SMat Martineau break; 4483721b9b6SMat Martineau } 4493721b9b6SMat Martineau 4503721b9b6SMat Martineau mptcp_set_timeout(sk, NULL); 4513721b9b6SMat Martineau mptcp_for_each_subflow(msk, subflow) { 4523721b9b6SMat Martineau struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 4533721b9b6SMat Martineau 4543721b9b6SMat Martineau lock_sock(ssk); 4553721b9b6SMat Martineau tcp_send_ack(ssk); 4563721b9b6SMat Martineau release_sock(ssk); 4573721b9b6SMat Martineau } 4583721b9b6SMat Martineau 459e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 4603721b9b6SMat Martineau } 4613721b9b6SMat Martineau } 4623721b9b6SMat Martineau 4636771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 4646771bfd9SFlorian Westphal struct sock *ssk, 4656771bfd9SFlorian Westphal unsigned int *bytes) 4666771bfd9SFlorian Westphal { 4676771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 468600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 4696771bfd9SFlorian Westphal unsigned int moved = 0; 4706771bfd9SFlorian Westphal bool more_data_avail; 4716771bfd9SFlorian Westphal struct tcp_sock *tp; 472717f2034SPaolo Abeni u32 old_copied_seq; 4736771bfd9SFlorian Westphal bool done = false; 47413c7ba0cSFlorian Westphal int sk_rbuf; 47513c7ba0cSFlorian Westphal 47613c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 47713c7ba0cSFlorian Westphal 47813c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 47913c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 48013c7ba0cSFlorian Westphal 48113c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 48213c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 48313c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 48413c7ba0cSFlorian Westphal } 48513c7ba0cSFlorian Westphal } 486600911ffSFlorian Westphal 487ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 4886771bfd9SFlorian Westphal tp = tcp_sk(ssk); 489717f2034SPaolo Abeni old_copied_seq = tp->copied_seq; 4906771bfd9SFlorian Westphal do { 4916771bfd9SFlorian Westphal u32 map_remaining, offset; 4926771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 4936771bfd9SFlorian Westphal struct sk_buff *skb; 4946771bfd9SFlorian Westphal bool fin; 4956771bfd9SFlorian Westphal 4966771bfd9SFlorian Westphal /* try to move as much data as available */ 4976771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 4986771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 4996771bfd9SFlorian Westphal 5006771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 501d9fb8c50SPaolo Abeni if (!skb) { 502d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 503d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 504d9fb8c50SPaolo Abeni * can enter an infinite loop 505d9fb8c50SPaolo Abeni */ 506d9fb8c50SPaolo Abeni if (!moved) 507d9fb8c50SPaolo Abeni done = true; 5086771bfd9SFlorian Westphal break; 509d9fb8c50SPaolo Abeni } 5106771bfd9SFlorian Westphal 511e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 512e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 513e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 514e1ff9e82SDavide Caratti * be sure to adjust the size 515e1ff9e82SDavide Caratti */ 516e1ff9e82SDavide Caratti map_remaining = skb->len; 517e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 518e1ff9e82SDavide Caratti } 519e1ff9e82SDavide Caratti 5206771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 5216771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 5226771bfd9SFlorian Westphal if (fin) { 5236771bfd9SFlorian Westphal done = true; 5246771bfd9SFlorian Westphal seq++; 5256771bfd9SFlorian Westphal } 5266771bfd9SFlorian Westphal 5276771bfd9SFlorian Westphal if (offset < skb->len) { 5286771bfd9SFlorian Westphal size_t len = skb->len - offset; 5296771bfd9SFlorian Westphal 5306771bfd9SFlorian Westphal if (tp->urg_data) 5316771bfd9SFlorian Westphal done = true; 5326771bfd9SFlorian Westphal 533ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 5346771bfd9SFlorian Westphal moved += len; 535ab174ad8SPaolo Abeni seq += len; 5366771bfd9SFlorian Westphal 5376771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 5386771bfd9SFlorian Westphal break; 5396771bfd9SFlorian Westphal } else { 5406771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 5416771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 5426771bfd9SFlorian Westphal done = true; 5436771bfd9SFlorian Westphal } 5446771bfd9SFlorian Westphal 5456771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 5466771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 547600911ffSFlorian Westphal 54813c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 549600911ffSFlorian Westphal done = true; 550600911ffSFlorian Westphal break; 551600911ffSFlorian Westphal } 5526771bfd9SFlorian Westphal } while (more_data_avail); 5536771bfd9SFlorian Westphal 5546719331cSPaolo Abeni *bytes += moved; 555717f2034SPaolo Abeni if (tp->copied_seq != old_copied_seq) 556717f2034SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 5576771bfd9SFlorian Westphal 5586771bfd9SFlorian Westphal return done; 5596771bfd9SFlorian Westphal } 5606771bfd9SFlorian Westphal 561ab174ad8SPaolo Abeni static bool mptcp_ofo_queue(struct mptcp_sock *msk) 562ab174ad8SPaolo Abeni { 563ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 564ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 565ab174ad8SPaolo Abeni bool moved = false; 566ab174ad8SPaolo Abeni struct rb_node *p; 567ab174ad8SPaolo Abeni u64 end_seq; 568ab174ad8SPaolo Abeni 569ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 57006242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 571ab174ad8SPaolo Abeni while (p) { 572ab174ad8SPaolo Abeni skb = rb_to_skb(p); 573ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 574ab174ad8SPaolo Abeni break; 575ab174ad8SPaolo Abeni 576ab174ad8SPaolo Abeni p = rb_next(p); 577ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 578ab174ad8SPaolo Abeni 579ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 580ab174ad8SPaolo Abeni msk->ack_seq))) { 581ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 58206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 583ab174ad8SPaolo Abeni continue; 584ab174ad8SPaolo Abeni } 585ab174ad8SPaolo Abeni 586ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 587ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 588ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 589ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 590ab174ad8SPaolo Abeni 591ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 59206242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 59306242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 59406242e44SPaolo Abeni delta); 595ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 596ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 597ab174ad8SPaolo Abeni } 598ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 599ab174ad8SPaolo Abeni moved = true; 600ab174ad8SPaolo Abeni } 601ab174ad8SPaolo Abeni return moved; 602ab174ad8SPaolo Abeni } 603ab174ad8SPaolo Abeni 6042e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 6052e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 6062e52213cSFlorian Westphal */ 6072e52213cSFlorian Westphal static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 6082e52213cSFlorian Westphal { 6092e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 6102e52213cSFlorian Westphal unsigned int moved = 0; 6112e52213cSFlorian Westphal 6122e52213cSFlorian Westphal if (READ_ONCE(sk->sk_lock.owned)) 6132e52213cSFlorian Westphal return false; 6142e52213cSFlorian Westphal 6152e52213cSFlorian Westphal if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 6162e52213cSFlorian Westphal return false; 6172e52213cSFlorian Westphal 6182e52213cSFlorian Westphal /* must re-check after taking the lock */ 619ab174ad8SPaolo Abeni if (!READ_ONCE(sk->sk_lock.owned)) { 6202e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 621ab174ad8SPaolo Abeni mptcp_ofo_queue(msk); 622ab174ad8SPaolo Abeni 623ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 624ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 625ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 626ab174ad8SPaolo Abeni * do it. 627ab174ad8SPaolo Abeni */ 628ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 629ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 630ab174ad8SPaolo Abeni } 6312e52213cSFlorian Westphal 6322e52213cSFlorian Westphal spin_unlock_bh(&sk->sk_lock.slock); 6332e52213cSFlorian Westphal 6342e52213cSFlorian Westphal return moved > 0; 6352e52213cSFlorian Westphal } 6362e52213cSFlorian Westphal 6372e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 638101f6f85SFlorian Westphal { 6396719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 640101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 64113c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 6426719331cSPaolo Abeni bool wake; 643101f6f85SFlorian Westphal 6446719331cSPaolo Abeni /* move_skbs_to_msk below can legitly clear the data_avail flag, 6456719331cSPaolo Abeni * but we will need later to properly woke the reader, cache its 6466719331cSPaolo Abeni * value 6476719331cSPaolo Abeni */ 6486719331cSPaolo Abeni wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL; 6496719331cSPaolo Abeni if (wake) 650101f6f85SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 6516771bfd9SFlorian Westphal 65213c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 65313c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 65413c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 65513c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 65613c7ba0cSFlorian Westphal 65713c7ba0cSFlorian Westphal /* over limit? can't append more skbs to msk */ 65813c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) 6592e52213cSFlorian Westphal goto wake; 6602e52213cSFlorian Westphal 66113c7ba0cSFlorian Westphal if (move_skbs_to_msk(msk, ssk)) 662600911ffSFlorian Westphal goto wake; 663600911ffSFlorian Westphal 66414c441b5SPaolo Abeni /* mptcp socket is owned, release_cb should retry */ 66514c441b5SPaolo Abeni if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 66614c441b5SPaolo Abeni &sk->sk_tsq_flags)) { 66714c441b5SPaolo Abeni sock_hold(sk); 6686771bfd9SFlorian Westphal 66914c441b5SPaolo Abeni /* need to try again, its possible release_cb() has already 67014c441b5SPaolo Abeni * been called after the test_and_set_bit() above. 67114c441b5SPaolo Abeni */ 67214c441b5SPaolo Abeni move_skbs_to_msk(msk, ssk); 67314c441b5SPaolo Abeni } 674600911ffSFlorian Westphal wake: 6756719331cSPaolo Abeni if (wake) 676101f6f85SFlorian Westphal sk->sk_data_ready(sk); 677101f6f85SFlorian Westphal } 678101f6f85SFlorian Westphal 679ec3edaa7SPeter Krystad static void __mptcp_flush_join_list(struct mptcp_sock *msk) 680ec3edaa7SPeter Krystad { 681ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 682ec3edaa7SPeter Krystad return; 683ec3edaa7SPeter Krystad 684ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 685ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 686ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 687ec3edaa7SPeter Krystad } 688ec3edaa7SPeter Krystad 689b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 690b51f9b80SPaolo Abeni { 691b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 692b51f9b80SPaolo Abeni } 693b51f9b80SPaolo Abeni 694b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 695b51f9b80SPaolo Abeni { 696b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 697b51f9b80SPaolo Abeni unsigned long tout; 698b51f9b80SPaolo Abeni 699e16163b6SPaolo Abeni /* prevent rescheduling on close */ 700e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 701e16163b6SPaolo Abeni return; 702e16163b6SPaolo Abeni 703b51f9b80SPaolo Abeni /* should never be called with mptcp level timer cleared */ 704b51f9b80SPaolo Abeni tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 705b51f9b80SPaolo Abeni if (WARN_ON_ONCE(!tout)) 706b51f9b80SPaolo Abeni tout = TCP_RTO_MIN; 707b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 708b51f9b80SPaolo Abeni } 709b51f9b80SPaolo Abeni 710ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 711ba8f48f7SPaolo Abeni { 712ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 713ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 714ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 715ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 716ba8f48f7SPaolo Abeni */ 717ba8f48f7SPaolo Abeni sock_hold(sk); 718ba8f48f7SPaolo Abeni return true; 719ba8f48f7SPaolo Abeni } 720ba8f48f7SPaolo Abeni return false; 721ba8f48f7SPaolo Abeni } 722ba8f48f7SPaolo Abeni 723b51f9b80SPaolo Abeni void mptcp_data_acked(struct sock *sk) 724b51f9b80SPaolo Abeni { 725b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 7263b1d6210SPaolo Abeni 72763561a40SPaolo Abeni if ((!test_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags) || 728ba8f48f7SPaolo Abeni (inet_sk_state_load(sk) != TCP_ESTABLISHED))) 729ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 730b51f9b80SPaolo Abeni } 731b51f9b80SPaolo Abeni 73259832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 73359832e24SFlorian Westphal { 734ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 735ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 73659832e24SFlorian Westphal } 73759832e24SFlorian Westphal 7385969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 7395969856aSPaolo Abeni { 7405969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 7415969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 7425969856aSPaolo Abeni int receivers = 0; 7435969856aSPaolo Abeni 7445969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 7455969856aSPaolo Abeni receivers += !subflow->rx_eof; 746e16163b6SPaolo Abeni if (receivers) 747e16163b6SPaolo Abeni return; 7485969856aSPaolo Abeni 749e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 7505969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 7515969856aSPaolo Abeni * to msk, when all subflows agree on it 7525969856aSPaolo Abeni */ 7535969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 7545969856aSPaolo Abeni 7555969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 7565969856aSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 7575969856aSPaolo Abeni sk->sk_data_ready(sk); 7585969856aSPaolo Abeni } 759e16163b6SPaolo Abeni 760e16163b6SPaolo Abeni switch (sk->sk_state) { 761e16163b6SPaolo Abeni case TCP_ESTABLISHED: 762e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 763e16163b6SPaolo Abeni break; 764e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 765e16163b6SPaolo Abeni /* fallback sockets skip TCP_CLOSING - TCP will take care */ 766e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 767e16163b6SPaolo Abeni break; 768e16163b6SPaolo Abeni default: 769e16163b6SPaolo Abeni return; 770e16163b6SPaolo Abeni } 771e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 7725969856aSPaolo Abeni } 7735969856aSPaolo Abeni 7746d0060f6SMat Martineau static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 7756d0060f6SMat Martineau { 7764930f483SFlorian Westphal const struct sock *sk = (const struct sock *)msk; 7774930f483SFlorian Westphal 7786d0060f6SMat Martineau if (!msk->cached_ext) 7794930f483SFlorian Westphal msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); 7806d0060f6SMat Martineau 7816d0060f6SMat Martineau return !!msk->cached_ext; 7826d0060f6SMat Martineau } 7836d0060f6SMat Martineau 7847a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 7857a6a6cbcSPaolo Abeni { 7867a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 7877a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 7887a6a6cbcSPaolo Abeni 7897a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 7907a6a6cbcSPaolo Abeni 7917a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 7927a6a6cbcSPaolo Abeni if (subflow->data_avail) 7937a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 7947a6a6cbcSPaolo Abeni } 7957a6a6cbcSPaolo Abeni 7967a6a6cbcSPaolo Abeni return NULL; 7977a6a6cbcSPaolo Abeni } 7987a6a6cbcSPaolo Abeni 7993f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 80057040755SPaolo Abeni const struct sk_buff *skb, 80157040755SPaolo Abeni const struct mptcp_ext *mpext) 8026d0060f6SMat Martineau { 80357040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 80457040755SPaolo Abeni return false; 80557040755SPaolo Abeni 8065a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 8075a369ca6SPaolo Abeni * mapping has not been xmitted yet 8085a369ca6SPaolo Abeni */ 8095a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 8105a369ca6SPaolo Abeni !mpext->frozen; 81157040755SPaolo Abeni } 81257040755SPaolo Abeni 81318b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 81418b683bfSPaolo Abeni const struct page_frag *pfrag, 81518b683bfSPaolo Abeni const struct mptcp_data_frag *df) 81618b683bfSPaolo Abeni { 81718b683bfSPaolo Abeni return df && pfrag->page == df->page && 818*d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 81918b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 82018b683bfSPaolo Abeni } 82118b683bfSPaolo Abeni 822d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 82318b683bfSPaolo Abeni { 824d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 8257948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 826d027236cSPaolo Abeni } 827d027236cSPaolo Abeni 828d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 829d027236cSPaolo Abeni { 830d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 831d027236cSPaolo Abeni 83218b683bfSPaolo Abeni list_del(&dfrag->list); 833d027236cSPaolo Abeni dfrag_uncharge(sk, len); 83418b683bfSPaolo Abeni put_page(dfrag->page); 83518b683bfSPaolo Abeni } 83618b683bfSPaolo Abeni 83763561a40SPaolo Abeni static bool mptcp_is_writeable(struct mptcp_sock *msk) 83863561a40SPaolo Abeni { 83963561a40SPaolo Abeni struct mptcp_subflow_context *subflow; 84063561a40SPaolo Abeni 84163561a40SPaolo Abeni if (!sk_stream_is_writeable((struct sock *)msk)) 84263561a40SPaolo Abeni return false; 84363561a40SPaolo Abeni 84463561a40SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 84563561a40SPaolo Abeni if (sk_stream_is_writeable(subflow->tcp_sock)) 84663561a40SPaolo Abeni return true; 84763561a40SPaolo Abeni } 84863561a40SPaolo Abeni return false; 84963561a40SPaolo Abeni } 85063561a40SPaolo Abeni 85118b683bfSPaolo Abeni static void mptcp_clean_una(struct sock *sk) 85218b683bfSPaolo Abeni { 85318b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 85418b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 855d027236cSPaolo Abeni bool cleaned = false; 856e1ff9e82SDavide Caratti u64 snd_una; 857e1ff9e82SDavide Caratti 858e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 859e1ff9e82SDavide Caratti * plain TCP 860e1ff9e82SDavide Caratti */ 861e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 862eaa2ffabSPaolo Abeni atomic64_set(&msk->snd_una, msk->snd_nxt); 863e1ff9e82SDavide Caratti snd_una = atomic64_read(&msk->snd_una); 86418b683bfSPaolo Abeni 86518b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 86618b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 86718b683bfSPaolo Abeni break; 86818b683bfSPaolo Abeni 869*d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(dfrag == msk->first_pending)) 870*d9ca1de8SPaolo Abeni break; 871d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 872d027236cSPaolo Abeni cleaned = true; 873d027236cSPaolo Abeni } 874d027236cSPaolo Abeni 8757948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 8767948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 87753eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 87853eb4c38SPaolo Abeni 879*d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->already_sent)) 88053eb4c38SPaolo Abeni goto out; 8817948f6ccSFlorian Westphal 8827948f6ccSFlorian Westphal dfrag->data_seq += delta; 88353eb4c38SPaolo Abeni dfrag->offset += delta; 8847948f6ccSFlorian Westphal dfrag->data_len -= delta; 885*d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 8867948f6ccSFlorian Westphal 8877948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 8887948f6ccSFlorian Westphal cleaned = true; 8897948f6ccSFlorian Westphal } 8907948f6ccSFlorian Westphal 89153eb4c38SPaolo Abeni out: 89295ed690eSFlorian Westphal if (cleaned) 893d027236cSPaolo Abeni sk_mem_reclaim_partial(sk); 89495ed690eSFlorian Westphal } 89595ed690eSFlorian Westphal 89695ed690eSFlorian Westphal static void mptcp_clean_una_wakeup(struct sock *sk) 89795ed690eSFlorian Westphal { 89895ed690eSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 89995ed690eSFlorian Westphal 90095ed690eSFlorian Westphal mptcp_clean_una(sk); 9017948f6ccSFlorian Westphal 9027948f6ccSFlorian Westphal /* Only wake up writers if a subflow is ready */ 90363561a40SPaolo Abeni if (mptcp_is_writeable(msk)) { 90495ed690eSFlorian Westphal set_bit(MPTCP_SEND_SPACE, &msk->flags); 90563561a40SPaolo Abeni smp_mb__after_atomic(); 90663561a40SPaolo Abeni 90763561a40SPaolo Abeni /* set SEND_SPACE before sk_stream_write_space clears 90863561a40SPaolo Abeni * NOSPACE 90963561a40SPaolo Abeni */ 9107948f6ccSFlorian Westphal sk_stream_write_space(sk); 91118b683bfSPaolo Abeni } 91218b683bfSPaolo Abeni } 91318b683bfSPaolo Abeni 91418b683bfSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 91518b683bfSPaolo Abeni * data 91618b683bfSPaolo Abeni */ 91718b683bfSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 91818b683bfSPaolo Abeni { 919*d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 920*d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 921*d9ca1de8SPaolo Abeni bool first = true; 922*d9ca1de8SPaolo Abeni 92318b683bfSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 92418b683bfSPaolo Abeni pfrag, sk->sk_allocation))) 92518b683bfSPaolo Abeni return true; 92618b683bfSPaolo Abeni 92718b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 928*d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 929*d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 930*d9ca1de8SPaolo Abeni 931*d9ca1de8SPaolo Abeni if (first) 932*d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 933*d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 934*d9ca1de8SPaolo Abeni first = false; 935*d9ca1de8SPaolo Abeni } 93618b683bfSPaolo Abeni return false; 93718b683bfSPaolo Abeni } 93818b683bfSPaolo Abeni 93918b683bfSPaolo Abeni static struct mptcp_data_frag * 94018b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 94118b683bfSPaolo Abeni int orig_offset) 94218b683bfSPaolo Abeni { 94318b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 94418b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 94518b683bfSPaolo Abeni 94618b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 94718b683bfSPaolo Abeni dfrag->data_len = 0; 94818b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 94918b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 95018b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 951*d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 95218b683bfSPaolo Abeni dfrag->page = pfrag->page; 95318b683bfSPaolo Abeni 95418b683bfSPaolo Abeni return dfrag; 95518b683bfSPaolo Abeni } 95618b683bfSPaolo Abeni 957caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 958caf971dfSPaolo Abeni int mss_now; 959caf971dfSPaolo Abeni int size_goal; 960*d9ca1de8SPaolo Abeni u16 limit; 961*d9ca1de8SPaolo Abeni u16 sent; 962*d9ca1de8SPaolo Abeni unsigned int flags; 963caf971dfSPaolo Abeni }; 964caf971dfSPaolo Abeni 96557040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 966*d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 967caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 96857040755SPaolo Abeni { 969*d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 9706d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 9716d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 97257040755SPaolo Abeni struct sk_buff *skb, *tail; 973*d9ca1de8SPaolo Abeni bool can_collapse = false; 974*d9ca1de8SPaolo Abeni int avail_size; 975*d9ca1de8SPaolo Abeni size_t ret; 9766d0060f6SMat Martineau 977*d9ca1de8SPaolo Abeni pr_debug("msk=%p ssk=%p sending dfrag at seq=%lld len=%d already sent=%d", 978*d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 9796d0060f6SMat Martineau 980*d9ca1de8SPaolo Abeni /* compute send limit */ 981*d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 982caf971dfSPaolo Abeni avail_size = info->size_goal; 98357040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 98457040755SPaolo Abeni if (skb) { 98557040755SPaolo Abeni /* Limit the write to the size available in the 98657040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 98757040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 98857040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 98957040755SPaolo Abeni * SSN association set here 99057040755SPaolo Abeni */ 991*d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 992caf971dfSPaolo Abeni can_collapse = (info->size_goal - skb->len > 0) && 993*d9ca1de8SPaolo Abeni mptcp_skb_can_collapse_to(data_seq, skb, mpext); 99457040755SPaolo Abeni if (!can_collapse) 99557040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 99657040755SPaolo Abeni else 997caf971dfSPaolo Abeni avail_size = info->size_goal - skb->len; 99857040755SPaolo Abeni } 99918b683bfSPaolo Abeni 1000*d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1001*d9ca1de8SPaolo Abeni info->limit > dfrag->data_len)) 1002*d9ca1de8SPaolo Abeni return 0; 100357040755SPaolo Abeni 1004*d9ca1de8SPaolo Abeni ret = info->limit - info->sent; 1005*d9ca1de8SPaolo Abeni tail = tcp_build_frag(ssk, avail_size, info->flags, dfrag->page, 1006*d9ca1de8SPaolo Abeni dfrag->offset + info->sent, &ret); 1007e2223995SPaolo Abeni if (!tail) { 1008e2223995SPaolo Abeni tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk)); 1009e2223995SPaolo Abeni return -ENOMEM; 101035759383SFlorian Westphal } 101118b683bfSPaolo Abeni 1012e2223995SPaolo Abeni /* if the tail skb is still the cached one, collapsing really happened. 101357040755SPaolo Abeni */ 1014e2223995SPaolo Abeni if (skb == tail) { 101557040755SPaolo Abeni WARN_ON_ONCE(!can_collapse); 101657040755SPaolo Abeni mpext->data_len += ret; 101757040755SPaolo Abeni goto out; 101857040755SPaolo Abeni } 101957040755SPaolo Abeni 1020e2223995SPaolo Abeni mpext = __skb_ext_set(tail, SKB_EXT_MPTCP, msk->cached_ext); 10216d0060f6SMat Martineau msk->cached_ext = NULL; 10226d0060f6SMat Martineau 10236d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1024*d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 10256d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 10266d0060f6SMat Martineau mpext->data_len = ret; 10276d0060f6SMat Martineau mpext->use_map = 1; 10286d0060f6SMat Martineau mpext->dsn64 = 1; 10296d0060f6SMat Martineau 10306d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 10316d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 10326d0060f6SMat Martineau mpext->dsn64); 10336d0060f6SMat Martineau 103457040755SPaolo Abeni out: 10356d0060f6SMat Martineau mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 10366d0060f6SMat Martineau return ret; 10376d0060f6SMat Martineau } 10386d0060f6SMat Martineau 103963561a40SPaolo Abeni static void mptcp_nospace(struct mptcp_sock *msk) 1040a0e17064SFlorian Westphal { 104163561a40SPaolo Abeni struct mptcp_subflow_context *subflow; 104263561a40SPaolo Abeni 1043a0e17064SFlorian Westphal clear_bit(MPTCP_SEND_SPACE, &msk->flags); 1044a0e17064SFlorian Westphal smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 1045a0e17064SFlorian Westphal 104663561a40SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 104763561a40SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 104863561a40SPaolo Abeni struct socket *sock = READ_ONCE(ssk->sk_socket); 104963561a40SPaolo Abeni 105063561a40SPaolo Abeni /* enables ssk->write_space() callbacks */ 105163561a40SPaolo Abeni if (sock) 1052a0e17064SFlorian Westphal set_bit(SOCK_NOSPACE, &sock->flags); 1053a0e17064SFlorian Westphal } 105463561a40SPaolo Abeni } 1055a0e17064SFlorian Westphal 1056d5f49190SPaolo Abeni static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1057d5f49190SPaolo Abeni { 1058d5f49190SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1059d5f49190SPaolo Abeni 1060d5f49190SPaolo Abeni /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */ 1061d5f49190SPaolo Abeni if (subflow->request_join && !subflow->fully_established) 1062d5f49190SPaolo Abeni return false; 1063d5f49190SPaolo Abeni 1064d5f49190SPaolo Abeni /* only send if our side has not closed yet */ 1065d5f49190SPaolo Abeni return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)); 1066d5f49190SPaolo Abeni } 1067d5f49190SPaolo Abeni 1068d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1069d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1070d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1071d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1072d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1073d5f49190SPaolo Abeni 1074d5f49190SPaolo Abeni struct subflow_send_info { 1075d5f49190SPaolo Abeni struct sock *ssk; 1076d5f49190SPaolo Abeni u64 ratio; 1077d5f49190SPaolo Abeni }; 1078d5f49190SPaolo Abeni 1079da51aef5SPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk, 1080da51aef5SPaolo Abeni u32 *sndbuf) 1081f296234cSPeter Krystad { 1082d5f49190SPaolo Abeni struct subflow_send_info send_info[2]; 1083f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 1084d5f49190SPaolo Abeni int i, nr_active = 0; 1085d5f49190SPaolo Abeni struct sock *ssk; 1086d5f49190SPaolo Abeni u64 ratio; 1087d5f49190SPaolo Abeni u32 pace; 1088f296234cSPeter Krystad 1089d5f49190SPaolo Abeni sock_owned_by_me((struct sock *)msk); 1090f296234cSPeter Krystad 1091da51aef5SPaolo Abeni *sndbuf = 0; 1092149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1093149f7c71SFlorian Westphal return NULL; 1094149f7c71SFlorian Westphal 1095d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1096d5f49190SPaolo Abeni if (!msk->first) 1097f296234cSPeter Krystad return NULL; 1098d5f49190SPaolo Abeni *sndbuf = msk->first->sk_sndbuf; 1099d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1100f296234cSPeter Krystad } 1101f296234cSPeter Krystad 1102d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1103d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1104d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 1105d5f49190SPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 1106d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1107d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1108da51aef5SPaolo Abeni *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf); 1109d5f49190SPaolo Abeni } 1110d5f49190SPaolo Abeni return msk->last_snd; 1111d5f49190SPaolo Abeni } 1112f296234cSPeter Krystad 1113d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 1114d5f49190SPaolo Abeni for (i = 0; i < 2; ++i) { 1115d5f49190SPaolo Abeni send_info[i].ssk = NULL; 1116d5f49190SPaolo Abeni send_info[i].ratio = -1; 1117d5f49190SPaolo Abeni } 1118d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1119d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1120d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1121f296234cSPeter Krystad continue; 1122d5f49190SPaolo Abeni 1123d5f49190SPaolo Abeni nr_active += !subflow->backup; 1124d5f49190SPaolo Abeni *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf); 1125d5f49190SPaolo Abeni if (!sk_stream_memory_free(subflow->tcp_sock)) 1126d5f49190SPaolo Abeni continue; 1127d5f49190SPaolo Abeni 1128d5f49190SPaolo Abeni pace = READ_ONCE(ssk->sk_pacing_rate); 1129d5f49190SPaolo Abeni if (!pace) 1130d5f49190SPaolo Abeni continue; 1131d5f49190SPaolo Abeni 1132d5f49190SPaolo Abeni ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1133d5f49190SPaolo Abeni pace); 1134d5f49190SPaolo Abeni if (ratio < send_info[subflow->backup].ratio) { 1135d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 1136d5f49190SPaolo Abeni send_info[subflow->backup].ratio = ratio; 1137d5f49190SPaolo Abeni } 1138f296234cSPeter Krystad } 1139f296234cSPeter Krystad 1140d5f49190SPaolo Abeni pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld", 1141d5f49190SPaolo Abeni msk, nr_active, send_info[0].ssk, send_info[0].ratio, 1142d5f49190SPaolo Abeni send_info[1].ssk, send_info[1].ratio); 1143f296234cSPeter Krystad 1144d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1145d5f49190SPaolo Abeni if (!nr_active) 1146d5f49190SPaolo Abeni send_info[0].ssk = send_info[1].ssk; 1147d5f49190SPaolo Abeni 1148d5f49190SPaolo Abeni if (send_info[0].ssk) { 1149d5f49190SPaolo Abeni msk->last_snd = send_info[0].ssk; 1150d5f49190SPaolo Abeni msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1151d5f49190SPaolo Abeni sk_stream_wspace(msk->last_snd)); 1152d5f49190SPaolo Abeni return msk->last_snd; 1153d5f49190SPaolo Abeni } 1154d5f49190SPaolo Abeni return NULL; 1155f296234cSPeter Krystad } 1156f296234cSPeter Krystad 115763561a40SPaolo Abeni static void ssk_check_wmem(struct mptcp_sock *msk) 11581891c4a0SFlorian Westphal { 115963561a40SPaolo Abeni if (unlikely(!mptcp_is_writeable(msk))) 116063561a40SPaolo Abeni mptcp_nospace(msk); 11611891c4a0SFlorian Westphal } 11621891c4a0SFlorian Westphal 1163*d9ca1de8SPaolo Abeni static void mptcp_push_release(struct sock *sk, struct sock *ssk, 1164*d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info *info) 1165*d9ca1de8SPaolo Abeni { 1166*d9ca1de8SPaolo Abeni mptcp_set_timeout(sk, ssk); 1167*d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1168*d9ca1de8SPaolo Abeni release_sock(ssk); 1169*d9ca1de8SPaolo Abeni } 1170*d9ca1de8SPaolo Abeni 1171*d9ca1de8SPaolo Abeni static void mptcp_push_pending(struct sock *sk, unsigned int flags) 1172*d9ca1de8SPaolo Abeni { 1173*d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1174*d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1175*d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1176*d9ca1de8SPaolo Abeni .flags = flags, 1177*d9ca1de8SPaolo Abeni }; 1178*d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1179*d9ca1de8SPaolo Abeni int len, copied = 0; 1180*d9ca1de8SPaolo Abeni u32 sndbuf; 1181*d9ca1de8SPaolo Abeni 1182*d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1183*d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1184*d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1185*d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1186*d9ca1de8SPaolo Abeni while (len > 0) { 1187*d9ca1de8SPaolo Abeni int ret = 0; 1188*d9ca1de8SPaolo Abeni 1189*d9ca1de8SPaolo Abeni prev_ssk = ssk; 1190*d9ca1de8SPaolo Abeni __mptcp_flush_join_list(msk); 1191*d9ca1de8SPaolo Abeni ssk = mptcp_subflow_get_send(msk, &sndbuf); 1192*d9ca1de8SPaolo Abeni 1193*d9ca1de8SPaolo Abeni /* do auto tuning */ 1194*d9ca1de8SPaolo Abeni if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) && 1195*d9ca1de8SPaolo Abeni sndbuf > READ_ONCE(sk->sk_sndbuf)) 1196*d9ca1de8SPaolo Abeni WRITE_ONCE(sk->sk_sndbuf, sndbuf); 1197*d9ca1de8SPaolo Abeni 1198*d9ca1de8SPaolo Abeni /* try to keep the subflow socket lock across 1199*d9ca1de8SPaolo Abeni * consecutive xmit on the same socket 1200*d9ca1de8SPaolo Abeni */ 1201*d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1202*d9ca1de8SPaolo Abeni mptcp_push_release(sk, prev_ssk, &info); 1203*d9ca1de8SPaolo Abeni if (!ssk) 1204*d9ca1de8SPaolo Abeni goto out; 1205*d9ca1de8SPaolo Abeni 1206*d9ca1de8SPaolo Abeni if (ssk != prev_ssk || !prev_ssk) 1207*d9ca1de8SPaolo Abeni lock_sock(ssk); 1208*d9ca1de8SPaolo Abeni 1209*d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1210*d9ca1de8SPaolo Abeni if (ret <= 0) { 1211*d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1212*d9ca1de8SPaolo Abeni goto out; 1213*d9ca1de8SPaolo Abeni } 1214*d9ca1de8SPaolo Abeni 1215*d9ca1de8SPaolo Abeni info.sent += ret; 1216*d9ca1de8SPaolo Abeni dfrag->already_sent += ret; 1217*d9ca1de8SPaolo Abeni msk->snd_nxt += ret; 1218*d9ca1de8SPaolo Abeni msk->snd_burst -= ret; 1219*d9ca1de8SPaolo Abeni copied += ret; 1220*d9ca1de8SPaolo Abeni len -= ret; 1221*d9ca1de8SPaolo Abeni } 1222*d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1223*d9ca1de8SPaolo Abeni } 1224*d9ca1de8SPaolo Abeni 1225*d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1226*d9ca1de8SPaolo Abeni if (ssk) 1227*d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1228*d9ca1de8SPaolo Abeni 1229*d9ca1de8SPaolo Abeni out: 1230*d9ca1de8SPaolo Abeni /* start the timer, if it's not pending */ 1231*d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1232*d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 1233*d9ca1de8SPaolo Abeni if (copied) 1234*d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1235*d9ca1de8SPaolo Abeni } 1236*d9ca1de8SPaolo Abeni 1237f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1238f870fa0bSMat Martineau { 1239f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 124017091708SFlorian Westphal struct page_frag *pfrag; 12416d0060f6SMat Martineau size_t copied = 0; 1242caf971dfSPaolo Abeni int ret = 0; 12436d0060f6SMat Martineau long timeo; 1244f870fa0bSMat Martineau 1245f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 1246f870fa0bSMat Martineau return -EOPNOTSUPP; 1247f870fa0bSMat Martineau 1248cec37a6eSPeter Krystad lock_sock(sk); 12491954b860SMat Martineau 12501954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 12511954b860SMat Martineau 12521954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 12531954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 12541954b860SMat Martineau if (ret) 12551954b860SMat Martineau goto out; 12561954b860SMat Martineau } 12571954b860SMat Martineau 125817091708SFlorian Westphal pfrag = sk_page_frag(sk); 125918b683bfSPaolo Abeni mptcp_clean_una(sk); 126018b683bfSPaolo Abeni 1261*d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1262*d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1263*d9ca1de8SPaolo Abeni int frag_truesize = 0; 1264*d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1265*d9ca1de8SPaolo Abeni size_t psize, offset; 1266*d9ca1de8SPaolo Abeni 126757baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 126857baaf28SMat Martineau ret = -EPIPE; 126957baaf28SMat Martineau goto out; 127057baaf28SMat Martineau } 127157baaf28SMat Martineau 1272*d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1273*d9ca1de8SPaolo Abeni * page allocator 1274fb529e62SFlorian Westphal */ 1275*d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1276*d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1277*d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1278*d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) { 1279*d9ca1de8SPaolo Abeni mptcp_push_pending(sk, msg->msg_flags); 1280*d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1281*d9ca1de8SPaolo Abeni goto wait_for_memory; 1282*d9ca1de8SPaolo Abeni } 1283*d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1284*d9ca1de8SPaolo Abeni goto wait_for_memory; 1285*d9ca1de8SPaolo Abeni 1286*d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1287*d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1288fb529e62SFlorian Westphal } 1289fb529e62SFlorian Westphal 1290*d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1291*d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1292*d9ca1de8SPaolo Abeni * anyway 1293*d9ca1de8SPaolo Abeni */ 1294*d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1295*d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1296*d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1297*d9ca1de8SPaolo Abeni if (!sk_wmem_schedule(sk, psize + frag_truesize)) 1298*d9ca1de8SPaolo Abeni goto wait_for_memory; 1299*d9ca1de8SPaolo Abeni 1300*d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1301*d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 1302*d9ca1de8SPaolo Abeni ret = -EFAULT; 1303*d9ca1de8SPaolo Abeni goto out; 1304*d9ca1de8SPaolo Abeni } 1305*d9ca1de8SPaolo Abeni 1306*d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 1307*d9ca1de8SPaolo Abeni copied += psize; 1308*d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1309*d9ca1de8SPaolo Abeni frag_truesize += psize; 1310*d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1311*d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1312*d9ca1de8SPaolo Abeni 1313*d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1314*d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1315*d9ca1de8SPaolo Abeni */ 1316*d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1317*d9ca1de8SPaolo Abeni sk->sk_forward_alloc -= frag_truesize; 1318*d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1319*d9ca1de8SPaolo Abeni get_page(dfrag->page); 1320*d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1321*d9ca1de8SPaolo Abeni if (!msk->first_pending) 1322*d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1323*d9ca1de8SPaolo Abeni } 1324*d9ca1de8SPaolo Abeni pr_debug("msk=%p dfrag at seq=%lld len=%d sent=%d new=%d", msk, 1325*d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1326*d9ca1de8SPaolo Abeni !dfrag_collapsed); 1327*d9ca1de8SPaolo Abeni 1328*d9ca1de8SPaolo Abeni if (!mptcp_ext_cache_refill(msk)) 1329*d9ca1de8SPaolo Abeni goto wait_for_memory; 1330*d9ca1de8SPaolo Abeni continue; 1331*d9ca1de8SPaolo Abeni 1332*d9ca1de8SPaolo Abeni wait_for_memory: 133363561a40SPaolo Abeni mptcp_nospace(msk); 1334*d9ca1de8SPaolo Abeni mptcp_clean_una(sk); 1335*d9ca1de8SPaolo Abeni if (mptcp_timer_pending(sk)) 1336*d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 1337f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1338f296234cSPeter Krystad if (ret) 1339f296234cSPeter Krystad goto out; 1340cec37a6eSPeter Krystad } 1341cec37a6eSPeter Krystad 1342*d9ca1de8SPaolo Abeni if (copied) 1343*d9ca1de8SPaolo Abeni mptcp_push_pending(sk, msg->msg_flags); 1344da51aef5SPaolo Abeni 13451954b860SMat Martineau out: 134663561a40SPaolo Abeni ssk_check_wmem(msk); 1347cec37a6eSPeter Krystad release_sock(sk); 13488555c6bfSPaolo Abeni return copied ? : ret; 1349f870fa0bSMat Martineau } 1350f870fa0bSMat Martineau 13517a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 13527a6a6cbcSPaolo Abeni { 13537a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 13547a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 13557a6a6cbcSPaolo Abeni 13567a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 13577a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 13587a6a6cbcSPaolo Abeni 13597a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 13607a6a6cbcSPaolo Abeni test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 13617a6a6cbcSPaolo Abeni 13627a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 13637a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 13647a6a6cbcSPaolo Abeni } 13657a6a6cbcSPaolo Abeni 13666771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 13676771bfd9SFlorian Westphal struct msghdr *msg, 13686771bfd9SFlorian Westphal size_t len) 13696771bfd9SFlorian Westphal { 13706771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 13716771bfd9SFlorian Westphal struct sk_buff *skb; 13726771bfd9SFlorian Westphal int copied = 0; 13736771bfd9SFlorian Westphal 13746771bfd9SFlorian Westphal while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 13756771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 13766771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 13776771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 13786771bfd9SFlorian Westphal int err; 13796771bfd9SFlorian Westphal 13806771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 13816771bfd9SFlorian Westphal if (unlikely(err < 0)) { 13826771bfd9SFlorian Westphal if (!copied) 13836771bfd9SFlorian Westphal return err; 13846771bfd9SFlorian Westphal break; 13856771bfd9SFlorian Westphal } 13866771bfd9SFlorian Westphal 13876771bfd9SFlorian Westphal copied += count; 13886771bfd9SFlorian Westphal 13896771bfd9SFlorian Westphal if (count < data_len) { 13906771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 13916771bfd9SFlorian Westphal break; 13926771bfd9SFlorian Westphal } 13936771bfd9SFlorian Westphal 13946771bfd9SFlorian Westphal __skb_unlink(skb, &sk->sk_receive_queue); 13956771bfd9SFlorian Westphal __kfree_skb(skb); 13966771bfd9SFlorian Westphal 13976771bfd9SFlorian Westphal if (copied >= len) 13986771bfd9SFlorian Westphal break; 13996771bfd9SFlorian Westphal } 14006771bfd9SFlorian Westphal 14016771bfd9SFlorian Westphal return copied; 14026771bfd9SFlorian Westphal } 14036771bfd9SFlorian Westphal 1404a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1405a6b118feSFlorian Westphal * 1406a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1407a6b118feSFlorian Westphal */ 1408a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1409a6b118feSFlorian Westphal { 1410a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1411a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1412a6b118feSFlorian Westphal u32 time, advmss = 1; 1413a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1414a6b118feSFlorian Westphal 1415a6b118feSFlorian Westphal sock_owned_by_me(sk); 1416a6b118feSFlorian Westphal 1417a6b118feSFlorian Westphal if (copied <= 0) 1418a6b118feSFlorian Westphal return; 1419a6b118feSFlorian Westphal 1420a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1421a6b118feSFlorian Westphal 1422a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1423a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1424a6b118feSFlorian Westphal 1425a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1426a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1427a6b118feSFlorian Westphal return; 1428a6b118feSFlorian Westphal 1429a6b118feSFlorian Westphal rtt_us = 0; 1430a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1431a6b118feSFlorian Westphal const struct tcp_sock *tp; 1432a6b118feSFlorian Westphal u64 sf_rtt_us; 1433a6b118feSFlorian Westphal u32 sf_advmss; 1434a6b118feSFlorian Westphal 1435a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1436a6b118feSFlorian Westphal 1437a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1438a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1439a6b118feSFlorian Westphal 1440a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1441a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1442a6b118feSFlorian Westphal } 1443a6b118feSFlorian Westphal 1444a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1445a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1446a6b118feSFlorian Westphal return; 1447a6b118feSFlorian Westphal 1448a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1449a6b118feSFlorian Westphal goto new_measure; 1450a6b118feSFlorian Westphal 1451a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1452a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1453a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1454a6b118feSFlorian Westphal u64 rcvwin, grow; 1455a6b118feSFlorian Westphal 1456a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1457a6b118feSFlorian Westphal 1458a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1459a6b118feSFlorian Westphal 1460a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1461a6b118feSFlorian Westphal rcvwin += (grow << 1); 1462a6b118feSFlorian Westphal 1463a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1464a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1465a6b118feSFlorian Westphal rcvmem += 128; 1466a6b118feSFlorian Westphal 1467a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1468a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1469a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1470a6b118feSFlorian Westphal 1471a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1472a6b118feSFlorian Westphal u32 window_clamp; 1473a6b118feSFlorian Westphal 1474a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1475a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1476a6b118feSFlorian Westphal 1477a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1478a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1479a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1480a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1481a6b118feSFlorian Westphal */ 1482a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1483a6b118feSFlorian Westphal struct sock *ssk; 1484c76c6956SPaolo Abeni bool slow; 1485a6b118feSFlorian Westphal 1486a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1487c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1488a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1489a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1490c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1491c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1492a6b118feSFlorian Westphal } 1493a6b118feSFlorian Westphal } 1494a6b118feSFlorian Westphal } 1495a6b118feSFlorian Westphal 1496a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1497a6b118feSFlorian Westphal new_measure: 1498a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1499a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1500a6b118feSFlorian Westphal } 1501a6b118feSFlorian Westphal 15026771bfd9SFlorian Westphal static bool __mptcp_move_skbs(struct mptcp_sock *msk) 15036771bfd9SFlorian Westphal { 15046771bfd9SFlorian Westphal unsigned int moved = 0; 15056771bfd9SFlorian Westphal bool done; 15066771bfd9SFlorian Westphal 1507d5f49190SPaolo Abeni /* avoid looping forever below on racing close */ 1508d5f49190SPaolo Abeni if (((struct sock *)msk)->sk_state == TCP_CLOSE) 1509d5f49190SPaolo Abeni return false; 1510d5f49190SPaolo Abeni 1511d5f49190SPaolo Abeni __mptcp_flush_join_list(msk); 15126771bfd9SFlorian Westphal do { 15136771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 151465f49fe7SFlorian Westphal bool slowpath; 15156771bfd9SFlorian Westphal 15166771bfd9SFlorian Westphal if (!ssk) 15176771bfd9SFlorian Westphal break; 15186771bfd9SFlorian Westphal 151965f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 15206771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 152165f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 15226771bfd9SFlorian Westphal } while (!done); 15236771bfd9SFlorian Westphal 1524ab174ad8SPaolo Abeni if (mptcp_ofo_queue(msk) || moved > 0) { 1525ab174ad8SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 1526ab174ad8SPaolo Abeni return true; 1527ab174ad8SPaolo Abeni } 1528ab174ad8SPaolo Abeni return false; 15296771bfd9SFlorian Westphal } 15306771bfd9SFlorian Westphal 1531f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 1532f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 1533f870fa0bSMat Martineau { 1534f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1535cec37a6eSPeter Krystad int copied = 0; 15367a6a6cbcSPaolo Abeni int target; 15377a6a6cbcSPaolo Abeni long timeo; 1538f870fa0bSMat Martineau 1539f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 1540f870fa0bSMat Martineau return -EOPNOTSUPP; 1541f870fa0bSMat Martineau 1542cec37a6eSPeter Krystad lock_sock(sk); 15437a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 15447a6a6cbcSPaolo Abeni 15457a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 15467a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 1547ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 15487a6a6cbcSPaolo Abeni 15496771bfd9SFlorian Westphal while (len > (size_t)copied) { 15507a6a6cbcSPaolo Abeni int bytes_read; 15517a6a6cbcSPaolo Abeni 15526771bfd9SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 15536771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 15547a6a6cbcSPaolo Abeni if (!copied) 15557a6a6cbcSPaolo Abeni copied = bytes_read; 15566771bfd9SFlorian Westphal goto out_err; 1557cec37a6eSPeter Krystad } 1558cec37a6eSPeter Krystad 15597a6a6cbcSPaolo Abeni copied += bytes_read; 15607a6a6cbcSPaolo Abeni 15616771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue) && 15626771bfd9SFlorian Westphal __mptcp_move_skbs(msk)) 15636771bfd9SFlorian Westphal continue; 15647a6a6cbcSPaolo Abeni 15657a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 15667a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 15677a6a6cbcSPaolo Abeni */ 15687a6a6cbcSPaolo Abeni if (copied >= target) 15697a6a6cbcSPaolo Abeni break; 15707a6a6cbcSPaolo Abeni 15717a6a6cbcSPaolo Abeni if (copied) { 15727a6a6cbcSPaolo Abeni if (sk->sk_err || 15737a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 15747a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 15757a6a6cbcSPaolo Abeni !timeo || 15767a6a6cbcSPaolo Abeni signal_pending(current)) 15777a6a6cbcSPaolo Abeni break; 15787a6a6cbcSPaolo Abeni } else { 15797a6a6cbcSPaolo Abeni if (sk->sk_err) { 15807a6a6cbcSPaolo Abeni copied = sock_error(sk); 15817a6a6cbcSPaolo Abeni break; 15827a6a6cbcSPaolo Abeni } 15837a6a6cbcSPaolo Abeni 15845969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 15855969856aSPaolo Abeni mptcp_check_for_eof(msk); 15865969856aSPaolo Abeni 15877a6a6cbcSPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) 15887a6a6cbcSPaolo Abeni break; 15897a6a6cbcSPaolo Abeni 15907a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 15917a6a6cbcSPaolo Abeni copied = -ENOTCONN; 15927a6a6cbcSPaolo Abeni break; 15937a6a6cbcSPaolo Abeni } 15947a6a6cbcSPaolo Abeni 15957a6a6cbcSPaolo Abeni if (!timeo) { 15967a6a6cbcSPaolo Abeni copied = -EAGAIN; 15977a6a6cbcSPaolo Abeni break; 15987a6a6cbcSPaolo Abeni } 15997a6a6cbcSPaolo Abeni 16007a6a6cbcSPaolo Abeni if (signal_pending(current)) { 16017a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 16027a6a6cbcSPaolo Abeni break; 16037a6a6cbcSPaolo Abeni } 16047a6a6cbcSPaolo Abeni } 16057a6a6cbcSPaolo Abeni 16067a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 16077a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 16087a6a6cbcSPaolo Abeni } 16097a6a6cbcSPaolo Abeni 16106771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue)) { 16116771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 16127a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 16137a6a6cbcSPaolo Abeni 16146771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 16156771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 16167a6a6cbcSPaolo Abeni */ 16176771bfd9SFlorian Westphal if (unlikely(__mptcp_move_skbs(msk))) 16186771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 16196771bfd9SFlorian Westphal } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 16206771bfd9SFlorian Westphal /* data to read but mptcp_wait_data() cleared DATA_READY */ 16217a6a6cbcSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 16227a6a6cbcSPaolo Abeni } 16236771bfd9SFlorian Westphal out_err: 16246719331cSPaolo Abeni pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d", 16256719331cSPaolo Abeni msk, test_bit(MPTCP_DATA_READY, &msk->flags), 16266719331cSPaolo Abeni skb_queue_empty(&sk->sk_receive_queue), copied); 1627a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 1628a6b118feSFlorian Westphal 1629cec37a6eSPeter Krystad release_sock(sk); 1630cec37a6eSPeter Krystad return copied; 1631cec37a6eSPeter Krystad } 1632cec37a6eSPeter Krystad 1633b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk) 1634b51f9b80SPaolo Abeni { 1635b51f9b80SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1636b51f9b80SPaolo Abeni 1637eaa2ffabSPaolo Abeni if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->snd_nxt)) { 1638b51f9b80SPaolo Abeni mptcp_stop_timer(sk); 16393b1d6210SPaolo Abeni } else { 16403b1d6210SPaolo Abeni set_bit(MPTCP_WORK_RTX, &msk->flags); 1641ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 16423b1d6210SPaolo Abeni } 1643b51f9b80SPaolo Abeni } 1644b51f9b80SPaolo Abeni 1645b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 1646b51f9b80SPaolo Abeni { 1647b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 1648b51f9b80SPaolo Abeni icsk_retransmit_timer); 1649b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 1650b51f9b80SPaolo Abeni 1651b51f9b80SPaolo Abeni bh_lock_sock(sk); 1652b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 1653b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1654b51f9b80SPaolo Abeni } else { 1655b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 1656b51f9b80SPaolo Abeni if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1657b51f9b80SPaolo Abeni &sk->sk_tsq_flags)) 1658b51f9b80SPaolo Abeni sock_hold(sk); 1659b51f9b80SPaolo Abeni } 1660b51f9b80SPaolo Abeni bh_unlock_sock(sk); 1661b51f9b80SPaolo Abeni sock_put(sk); 1662b51f9b80SPaolo Abeni } 1663b51f9b80SPaolo Abeni 1664e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 1665e16163b6SPaolo Abeni { 1666e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 1667e16163b6SPaolo Abeni 1668e16163b6SPaolo Abeni mptcp_schedule_work(sk); 1669e16163b6SPaolo Abeni } 1670e16163b6SPaolo Abeni 16713b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 16723b1d6210SPaolo Abeni * level. 16733b1d6210SPaolo Abeni * 16743b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 16753b1d6210SPaolo Abeni */ 16763b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 16773b1d6210SPaolo Abeni { 16783b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 16793b1d6210SPaolo Abeni struct sock *backup = NULL; 16803b1d6210SPaolo Abeni 16813b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 16823b1d6210SPaolo Abeni 1683d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 1684*d9ca1de8SPaolo Abeni return NULL; 1685d5f49190SPaolo Abeni 16863b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 16873b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 16883b1d6210SPaolo Abeni 1689d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1690d5f49190SPaolo Abeni continue; 1691d5f49190SPaolo Abeni 16923b1d6210SPaolo Abeni /* still data outstanding at TCP level? Don't retransmit. */ 16933b1d6210SPaolo Abeni if (!tcp_write_queue_empty(ssk)) 16943b1d6210SPaolo Abeni return NULL; 16953b1d6210SPaolo Abeni 16963b1d6210SPaolo Abeni if (subflow->backup) { 16973b1d6210SPaolo Abeni if (!backup) 16983b1d6210SPaolo Abeni backup = ssk; 16993b1d6210SPaolo Abeni continue; 17003b1d6210SPaolo Abeni } 17013b1d6210SPaolo Abeni 17023b1d6210SPaolo Abeni return ssk; 17033b1d6210SPaolo Abeni } 17043b1d6210SPaolo Abeni 17053b1d6210SPaolo Abeni return backup; 17063b1d6210SPaolo Abeni } 17073b1d6210SPaolo Abeni 1708cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 1709cec37a6eSPeter Krystad * (accept). 1710cec37a6eSPeter Krystad * 1711cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 1712cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 1713cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 1714cec37a6eSPeter Krystad * parent socket. 1715cec37a6eSPeter Krystad */ 1716d0876b22SGeliang Tang void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1717e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow) 1718cec37a6eSPeter Krystad { 1719e16163b6SPaolo Abeni bool dispose_socket = false; 1720e16163b6SPaolo Abeni struct socket *sock; 1721cec37a6eSPeter Krystad 1722cec37a6eSPeter Krystad list_del(&subflow->node); 1723cec37a6eSPeter Krystad 1724e16163b6SPaolo Abeni lock_sock(ssk); 1725e16163b6SPaolo Abeni 1726e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 1727e16163b6SPaolo Abeni * already orphaned 1728e16163b6SPaolo Abeni */ 1729e16163b6SPaolo Abeni sock = ssk->sk_socket; 1730e16163b6SPaolo Abeni if (sock) { 1731e16163b6SPaolo Abeni dispose_socket = sock != sk->sk_socket; 1732e16163b6SPaolo Abeni sock_orphan(ssk); 1733cec37a6eSPeter Krystad } 1734e16163b6SPaolo Abeni 1735e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 1736e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 1737e16163b6SPaolo Abeni * reference owned by msk; 1738e16163b6SPaolo Abeni */ 1739e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 1740e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 1741e16163b6SPaolo Abeni } else { 1742e16163b6SPaolo Abeni /* otherwise ask tcp do dispose of ssk and subflow ctx */ 1743e16163b6SPaolo Abeni subflow->disposable = 1; 1744e16163b6SPaolo Abeni __tcp_close(ssk, 0); 1745e16163b6SPaolo Abeni 1746e16163b6SPaolo Abeni /* close acquired an extra ref */ 1747e16163b6SPaolo Abeni __sock_put(ssk); 1748e16163b6SPaolo Abeni } 1749e16163b6SPaolo Abeni release_sock(ssk); 1750e16163b6SPaolo Abeni if (dispose_socket) 1751e16163b6SPaolo Abeni iput(SOCK_INODE(sock)); 1752e16163b6SPaolo Abeni 1753e16163b6SPaolo Abeni sock_put(ssk); 1754f870fa0bSMat Martineau } 1755f870fa0bSMat Martineau 1756dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1757dc24f8b4SPaolo Abeni { 1758dc24f8b4SPaolo Abeni return 0; 1759dc24f8b4SPaolo Abeni } 1760dc24f8b4SPaolo Abeni 1761b416268bSFlorian Westphal static void pm_work(struct mptcp_sock *msk) 1762b416268bSFlorian Westphal { 1763b416268bSFlorian Westphal struct mptcp_pm_data *pm = &msk->pm; 1764b416268bSFlorian Westphal 1765b416268bSFlorian Westphal spin_lock_bh(&msk->pm.lock); 1766b416268bSFlorian Westphal 1767b416268bSFlorian Westphal pr_debug("msk=%p status=%x", msk, pm->status); 1768b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 1769b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 1770b416268bSFlorian Westphal mptcp_pm_nl_add_addr_received(msk); 1771b416268bSFlorian Westphal } 1772d0876b22SGeliang Tang if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 1773d0876b22SGeliang Tang pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 1774d0876b22SGeliang Tang mptcp_pm_nl_rm_addr_received(msk); 1775d0876b22SGeliang Tang } 1776b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 1777b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 1778b416268bSFlorian Westphal mptcp_pm_nl_fully_established(msk); 1779b416268bSFlorian Westphal } 1780b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 1781b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 1782b416268bSFlorian Westphal mptcp_pm_nl_subflow_established(msk); 1783b416268bSFlorian Westphal } 1784b416268bSFlorian Westphal 1785b416268bSFlorian Westphal spin_unlock_bh(&msk->pm.lock); 1786b416268bSFlorian Westphal } 1787b416268bSFlorian Westphal 17880e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 17890e4f35d7SPaolo Abeni { 17900e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 17910e4f35d7SPaolo Abeni 17920e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 17930e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 17940e4f35d7SPaolo Abeni 17950e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 17960e4f35d7SPaolo Abeni continue; 17970e4f35d7SPaolo Abeni 1798e16163b6SPaolo Abeni __mptcp_close_ssk((struct sock *)msk, ssk, subflow); 17990e4f35d7SPaolo Abeni } 18000e4f35d7SPaolo Abeni } 18010e4f35d7SPaolo Abeni 1802e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 1803e16163b6SPaolo Abeni { 1804e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 1805e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 1806e16163b6SPaolo Abeni 1807e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 1808e16163b6SPaolo Abeni return true; 1809e16163b6SPaolo Abeni 1810e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 1811e16163b6SPaolo Abeni * timeout 1812e16163b6SPaolo Abeni */ 1813e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 1814e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 1815e16163b6SPaolo Abeni TCP_CLOSE) 1816e16163b6SPaolo Abeni return false; 1817e16163b6SPaolo Abeni } 1818e16163b6SPaolo Abeni return true; 1819e16163b6SPaolo Abeni } 1820e16163b6SPaolo Abeni 182180992017SPaolo Abeni static void mptcp_worker(struct work_struct *work) 182280992017SPaolo Abeni { 182380992017SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 18243b1d6210SPaolo Abeni struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 1825caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 18263b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 18273b1d6210SPaolo Abeni size_t copied = 0; 1828e16163b6SPaolo Abeni int state, ret; 182980992017SPaolo Abeni 183080992017SPaolo Abeni lock_sock(sk); 1831e16163b6SPaolo Abeni set_bit(MPTCP_WORKER_RUNNING, &msk->flags); 1832e16163b6SPaolo Abeni state = sk->sk_state; 1833e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 1834e16163b6SPaolo Abeni goto unlock; 1835e16163b6SPaolo Abeni 183695ed690eSFlorian Westphal mptcp_clean_una_wakeup(sk); 183743b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 1838ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 18390e4f35d7SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 18400e4f35d7SPaolo Abeni __mptcp_close_subflow(msk); 18410e4f35d7SPaolo Abeni 18426771bfd9SFlorian Westphal __mptcp_move_skbs(msk); 18433b1d6210SPaolo Abeni 1844b416268bSFlorian Westphal if (msk->pm.status) 1845b416268bSFlorian Westphal pm_work(msk); 1846b416268bSFlorian Westphal 184759832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 184859832e24SFlorian Westphal mptcp_check_for_eof(msk); 184959832e24SFlorian Westphal 185043b54c6eSMat Martineau mptcp_check_data_fin(sk); 185143b54c6eSMat Martineau 1852e16163b6SPaolo Abeni /* if the msk data is completely acked, or the socket timedout, 1853e16163b6SPaolo Abeni * there is no point in keeping around an orphaned sk 1854e16163b6SPaolo Abeni */ 1855e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 1856e16163b6SPaolo Abeni (mptcp_check_close_timeout(sk) || 1857e16163b6SPaolo Abeni (state != sk->sk_state && 1858e16163b6SPaolo Abeni ((1 << inet_sk_state_load(sk)) & (TCPF_CLOSE | TCPF_FIN_WAIT2))))) { 1859e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 1860e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 1861e16163b6SPaolo Abeni goto unlock; 1862e16163b6SPaolo Abeni } 1863e16163b6SPaolo Abeni 18643b1d6210SPaolo Abeni if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 18653b1d6210SPaolo Abeni goto unlock; 18663b1d6210SPaolo Abeni 18673b1d6210SPaolo Abeni dfrag = mptcp_rtx_head(sk); 18683b1d6210SPaolo Abeni if (!dfrag) 18693b1d6210SPaolo Abeni goto unlock; 18703b1d6210SPaolo Abeni 1871149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1872149f7c71SFlorian Westphal goto reset_unlock; 1873149f7c71SFlorian Westphal 18743b1d6210SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 18753b1d6210SPaolo Abeni if (!ssk) 18763b1d6210SPaolo Abeni goto reset_unlock; 18773b1d6210SPaolo Abeni 18783b1d6210SPaolo Abeni lock_sock(ssk); 18793b1d6210SPaolo Abeni 1880*d9ca1de8SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 1881*d9ca1de8SPaolo Abeni info.sent = 0; 1882*d9ca1de8SPaolo Abeni info.limit = dfrag->already_sent; 1883*d9ca1de8SPaolo Abeni while (info.sent < dfrag->already_sent) { 1884*d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 18853b1d6210SPaolo Abeni if (ret < 0) 18863b1d6210SPaolo Abeni break; 18873b1d6210SPaolo Abeni 1888fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 18893b1d6210SPaolo Abeni copied += ret; 1890*d9ca1de8SPaolo Abeni info.sent += ret; 1891149f7c71SFlorian Westphal 1892149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1893149f7c71SFlorian Westphal break; 18943b1d6210SPaolo Abeni } 18953b1d6210SPaolo Abeni if (copied) 1896caf971dfSPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 1897caf971dfSPaolo Abeni info.size_goal); 18983b1d6210SPaolo Abeni 18993b1d6210SPaolo Abeni mptcp_set_timeout(sk, ssk); 19003b1d6210SPaolo Abeni release_sock(ssk); 19013b1d6210SPaolo Abeni 19023b1d6210SPaolo Abeni reset_unlock: 19033b1d6210SPaolo Abeni if (!mptcp_timer_pending(sk)) 19043b1d6210SPaolo Abeni mptcp_reset_timer(sk); 19053b1d6210SPaolo Abeni 19063b1d6210SPaolo Abeni unlock: 1907e16163b6SPaolo Abeni clear_bit(MPTCP_WORKER_RUNNING, &msk->flags); 190880992017SPaolo Abeni release_sock(sk); 190980992017SPaolo Abeni sock_put(sk); 191080992017SPaolo Abeni } 191180992017SPaolo Abeni 1912784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 1913f870fa0bSMat Martineau { 1914cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1915cec37a6eSPeter Krystad 1916ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 1917ec3edaa7SPeter Krystad 1918cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 1919ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 192018b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 19211891c4a0SFlorian Westphal __set_bit(MPTCP_SEND_SPACE, &msk->flags); 192280992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 1923ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 1924f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 1925cec37a6eSPeter Krystad 19268ab183deSPaolo Abeni msk->first = NULL; 1927dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 19288ab183deSPaolo Abeni 19291b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 19301b1c7a0eSPeter Krystad 1931b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 1932b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1933e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 1934f870fa0bSMat Martineau return 0; 1935f870fa0bSMat Martineau } 1936f870fa0bSMat Martineau 1937784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 1938784325e9SMatthieu Baerts { 1939fc518953SFlorian Westphal struct net *net = sock_net(sk); 1940fc518953SFlorian Westphal int ret; 194118b683bfSPaolo Abeni 1942b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 1943b6c08380SGeliang Tang if (ret) 1944b6c08380SGeliang Tang return ret; 1945b6c08380SGeliang Tang 1946fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 1947fc518953SFlorian Westphal return -ENOPROTOOPT; 1948fc518953SFlorian Westphal 1949fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1950fc518953SFlorian Westphal return -ENOMEM; 1951fc518953SFlorian Westphal 1952fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 1953fa68018dSPaolo Abeni if (ret) 1954fa68018dSPaolo Abeni return ret; 1955fa68018dSPaolo Abeni 1956d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 1957a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 1958da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 1959d027236cSPaolo Abeni 196018b683bfSPaolo Abeni return 0; 196118b683bfSPaolo Abeni } 196218b683bfSPaolo Abeni 196318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 196418b683bfSPaolo Abeni { 196518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 196618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 196718b683bfSPaolo Abeni 1968b51f9b80SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1969b51f9b80SPaolo Abeni 1970*d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 197118b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 1972d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1973784325e9SMatthieu Baerts } 1974784325e9SMatthieu Baerts 197580992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 197680992017SPaolo Abeni { 197780992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 197880992017SPaolo Abeni 1979e16163b6SPaolo Abeni /* if called by the work itself, do not try to cancel the work, or 1980e16163b6SPaolo Abeni * we will hang. 1981e16163b6SPaolo Abeni */ 1982e16163b6SPaolo Abeni if (!test_bit(MPTCP_WORKER_RUNNING, &msk->flags) && 1983e16163b6SPaolo Abeni cancel_work_sync(&msk->work)) 1984e16163b6SPaolo Abeni __sock_put(sk); 198580992017SPaolo Abeni } 198680992017SPaolo Abeni 1987d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 198821498490SPeter Krystad { 198921498490SPeter Krystad lock_sock(ssk); 199021498490SPeter Krystad 199121498490SPeter Krystad switch (ssk->sk_state) { 199221498490SPeter Krystad case TCP_LISTEN: 199321498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 199421498490SPeter Krystad break; 1995df561f66SGustavo A. R. Silva fallthrough; 199621498490SPeter Krystad case TCP_SYN_SENT: 199721498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 199821498490SPeter Krystad break; 199921498490SPeter Krystad default: 200043b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 200143b54c6eSMat Martineau pr_debug("Fallback"); 200221498490SPeter Krystad ssk->sk_shutdown |= how; 200321498490SPeter Krystad tcp_shutdown(ssk, how); 200443b54c6eSMat Martineau } else { 200543b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 200643b54c6eSMat Martineau mptcp_set_timeout(sk, ssk); 200743b54c6eSMat Martineau tcp_send_ack(ssk); 200843b54c6eSMat Martineau } 200921498490SPeter Krystad break; 201021498490SPeter Krystad } 201121498490SPeter Krystad 201221498490SPeter Krystad release_sock(ssk); 201321498490SPeter Krystad } 201421498490SPeter Krystad 20156920b851SMat Martineau static const unsigned char new_state[16] = { 20166920b851SMat Martineau /* current state: new state: action: */ 20176920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 20186920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 20196920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 20206920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 20216920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 20226920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 20236920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 20246920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 20256920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 20266920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 20276920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 20286920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 20296920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 20306920b851SMat Martineau }; 20316920b851SMat Martineau 20326920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 20336920b851SMat Martineau { 20346920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 20356920b851SMat Martineau int ns = next & TCP_STATE_MASK; 20366920b851SMat Martineau 20376920b851SMat Martineau inet_sk_state_store(sk, ns); 20386920b851SMat Martineau 20396920b851SMat Martineau return next & TCP_ACTION_FIN; 20406920b851SMat Martineau } 20416920b851SMat Martineau 2042e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2043e16163b6SPaolo Abeni { 2044e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2045e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2046e16163b6SPaolo Abeni 2047e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2048e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2049e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2050e16163b6SPaolo Abeni 2051e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2052e16163b6SPaolo Abeni * skip this 2053e16163b6SPaolo Abeni */ 2054e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2055e16163b6SPaolo Abeni mptcp_send_head(sk)) 2056e16163b6SPaolo Abeni return; 2057e16163b6SPaolo Abeni 2058e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2059e16163b6SPaolo Abeni 2060e16163b6SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to close now */ 2061e16163b6SPaolo Abeni if (__mptcp_check_fallback(msk) && sk->sk_state == TCP_LAST_ACK) { 2062e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2063e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 2064e16163b6SPaolo Abeni } 2065e16163b6SPaolo Abeni 2066e16163b6SPaolo Abeni __mptcp_flush_join_list(msk); 2067e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2068e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2069e16163b6SPaolo Abeni 2070e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2071e16163b6SPaolo Abeni } 2072e16163b6SPaolo Abeni } 2073e16163b6SPaolo Abeni 2074e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2075e16163b6SPaolo Abeni { 2076e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2077e16163b6SPaolo Abeni 2078e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2079e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2080e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2081e16163b6SPaolo Abeni 2082e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2083e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2084e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2085e16163b6SPaolo Abeni 2086e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2087e16163b6SPaolo Abeni } 2088e16163b6SPaolo Abeni 2089e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2090f870fa0bSMat Martineau { 2091cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2092f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2093b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2094f870fa0bSMat Martineau 2095e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 20962c22c06cSFlorian Westphal 209710f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 209810f6d46cSPaolo Abeni * mptcp_finish_join(). 209910f6d46cSPaolo Abeni */ 210010f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 210110f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 210210f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2103b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2104b2c5b614SFlorian Westphal 210518b683bfSPaolo Abeni __mptcp_clear_xmit(sk); 2106e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2107e16163b6SPaolo Abeni msk->pm.status = 0; 2108b2c5b614SFlorian Westphal 2109b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2110cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2111e16163b6SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow); 2112f870fa0bSMat Martineau } 2113f870fa0bSMat Martineau 2114e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2115e16163b6SPaolo Abeni 2116e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2117e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2118e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2119e16163b6SPaolo Abeni sock_put(sk); 2120e16163b6SPaolo Abeni } 2121e16163b6SPaolo Abeni 2122e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2123e16163b6SPaolo Abeni { 2124e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2125e16163b6SPaolo Abeni bool do_cancel_work = false; 2126e16163b6SPaolo Abeni 2127e16163b6SPaolo Abeni lock_sock(sk); 2128e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2129e16163b6SPaolo Abeni 2130e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2131e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2132e16163b6SPaolo Abeni goto cleanup; 2133e16163b6SPaolo Abeni } 2134e16163b6SPaolo Abeni 2135e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2136e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2137e16163b6SPaolo Abeni 2138e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2139e16163b6SPaolo Abeni 2140e16163b6SPaolo Abeni cleanup: 2141e16163b6SPaolo Abeni /* orphan all the subflows */ 2142e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2143e16163b6SPaolo Abeni list_for_each_entry(subflow, &mptcp_sk(sk)->conn_list, node) { 2144e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2145e16163b6SPaolo Abeni bool slow, dispose_socket; 2146e16163b6SPaolo Abeni struct socket *sock; 2147e16163b6SPaolo Abeni 2148e16163b6SPaolo Abeni slow = lock_sock_fast(ssk); 2149e16163b6SPaolo Abeni sock = ssk->sk_socket; 2150e16163b6SPaolo Abeni dispose_socket = sock && sock != sk->sk_socket; 2151e16163b6SPaolo Abeni sock_orphan(ssk); 2152e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2153e16163b6SPaolo Abeni 2154e16163b6SPaolo Abeni /* for the outgoing subflows we additionally need to free 2155e16163b6SPaolo Abeni * the associated socket 2156e16163b6SPaolo Abeni */ 2157e16163b6SPaolo Abeni if (dispose_socket) 2158e16163b6SPaolo Abeni iput(SOCK_INODE(sock)); 2159e16163b6SPaolo Abeni } 2160e16163b6SPaolo Abeni sock_orphan(sk); 2161e16163b6SPaolo Abeni 2162e16163b6SPaolo Abeni sock_hold(sk); 2163e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2164e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2165e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2166e16163b6SPaolo Abeni do_cancel_work = true; 2167e16163b6SPaolo Abeni } else { 2168e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2169e16163b6SPaolo Abeni } 2170e16163b6SPaolo Abeni release_sock(sk); 2171e16163b6SPaolo Abeni if (do_cancel_work) 217280992017SPaolo Abeni mptcp_cancel_work(sk); 2173e16163b6SPaolo Abeni sock_put(sk); 2174f870fa0bSMat Martineau } 2175f870fa0bSMat Martineau 2176cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2177cf7da0d6SPeter Krystad { 2178cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2179cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2180cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2181cf7da0d6SPeter Krystad 2182cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2183cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2184cf7da0d6SPeter Krystad 2185cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2186cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2187cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2188cf7da0d6SPeter Krystad } 2189cf7da0d6SPeter Krystad #endif 2190cf7da0d6SPeter Krystad 2191cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2192cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2193cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2194cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2195cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2196cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2197cf7da0d6SPeter Krystad } 2198cf7da0d6SPeter Krystad 219918b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 220018b683bfSPaolo Abeni { 220142c556feSFlorian Westphal /* Should never be called. 220242c556feSFlorian Westphal * inet_stream_connect() calls ->disconnect, but that 220342c556feSFlorian Westphal * refers to the subflow socket, not the mptcp one. 220442c556feSFlorian Westphal */ 220542c556feSFlorian Westphal WARN_ON_ONCE(1); 220642c556feSFlorian Westphal return 0; 220718b683bfSPaolo Abeni } 220818b683bfSPaolo Abeni 2209b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2210b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2211b0519de8SFlorian Westphal { 2212b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2213b0519de8SFlorian Westphal 2214b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2215b0519de8SFlorian Westphal } 2216b0519de8SFlorian Westphal #endif 2217b0519de8SFlorian Westphal 2218fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2219cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2220fca5c82cSPaolo Abeni struct request_sock *req) 2221b0519de8SFlorian Westphal { 222258b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2223b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 222458b09919SPaolo Abeni struct mptcp_sock *msk; 222558b09919SPaolo Abeni u64 ack_seq; 2226b0519de8SFlorian Westphal 2227b0519de8SFlorian Westphal if (!nsk) 2228b0519de8SFlorian Westphal return NULL; 2229b0519de8SFlorian Westphal 2230b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2231b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2232b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2233b0519de8SFlorian Westphal #endif 2234b0519de8SFlorian Westphal 223558b09919SPaolo Abeni __mptcp_init_sock(nsk); 223658b09919SPaolo Abeni 223758b09919SPaolo Abeni msk = mptcp_sk(nsk); 223858b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 223958b09919SPaolo Abeni msk->token = subflow_req->token; 224058b09919SPaolo Abeni msk->subflow = NULL; 2241b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 224258b09919SPaolo Abeni 224358b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2244eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 2245cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 2246cfde141eSPaolo Abeni if (mp_opt->mp_capable) { 224758b09919SPaolo Abeni msk->can_ack = true; 2248cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 224958b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 225058b09919SPaolo Abeni ack_seq++; 2251917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 225258b09919SPaolo Abeni } 22537f20d5fcSPaolo Abeni 22545e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 22557f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 22567f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 225758b09919SPaolo Abeni bh_unlock_sock(nsk); 225858b09919SPaolo Abeni 225958b09919SPaolo Abeni /* keep a single reference */ 226058b09919SPaolo Abeni __sock_put(nsk); 2261b0519de8SFlorian Westphal return nsk; 2262b0519de8SFlorian Westphal } 2263b0519de8SFlorian Westphal 2264a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2265a6b118feSFlorian Westphal { 2266a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2267a6b118feSFlorian Westphal 2268a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2269a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2270a6b118feSFlorian Westphal 2271a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2272a6b118feSFlorian Westphal 2273a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2274a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2275a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2276a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2277a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 2278a6b118feSFlorian Westphal } 2279a6b118feSFlorian Westphal 2280cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2281cf7da0d6SPeter Krystad bool kern) 2282cf7da0d6SPeter Krystad { 2283cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2284cf7da0d6SPeter Krystad struct socket *listener; 2285cf7da0d6SPeter Krystad struct sock *newsk; 2286cf7da0d6SPeter Krystad 2287cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2288cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2289cf7da0d6SPeter Krystad *err = -EINVAL; 2290cf7da0d6SPeter Krystad return NULL; 2291cf7da0d6SPeter Krystad } 2292cf7da0d6SPeter Krystad 2293cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2294cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2295cf7da0d6SPeter Krystad if (!newsk) 2296cf7da0d6SPeter Krystad return NULL; 2297cf7da0d6SPeter Krystad 2298cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2299cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2300cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2301cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2302cf7da0d6SPeter Krystad struct sock *ssk = newsk; 2303cf7da0d6SPeter Krystad 2304cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 230558b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 230658b09919SPaolo Abeni 230758b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 230858b09919SPaolo Abeni * subflow_syn_recv_sock() 230958b09919SPaolo Abeni */ 231058b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 231158b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 231258b09919SPaolo Abeni return newsk; 231358b09919SPaolo Abeni } 231458b09919SPaolo Abeni 231558b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 231658b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2317cf7da0d6SPeter Krystad 2318cf7da0d6SPeter Krystad local_bh_disable(); 231958b09919SPaolo Abeni bh_lock_sock(new_mptcp_sock); 2320cf7da0d6SPeter Krystad msk = mptcp_sk(new_mptcp_sock); 23218ab183deSPaolo Abeni msk->first = newsk; 2322cf7da0d6SPeter Krystad 2323cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 2324cf7da0d6SPeter Krystad mptcp_copy_inaddrs(newsk, ssk); 2325cf7da0d6SPeter Krystad list_add(&subflow->node, &msk->conn_list); 2326e16163b6SPaolo Abeni sock_hold(ssk); 2327cf7da0d6SPeter Krystad 2328a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 2329cf7da0d6SPeter Krystad bh_unlock_sock(new_mptcp_sock); 2330fc518953SFlorian Westphal 2331fc518953SFlorian Westphal __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2332cf7da0d6SPeter Krystad local_bh_enable(); 2333fc518953SFlorian Westphal } else { 2334fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2335fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2336cf7da0d6SPeter Krystad } 2337cf7da0d6SPeter Krystad 2338cf7da0d6SPeter Krystad return newsk; 2339cf7da0d6SPeter Krystad } 2340cf7da0d6SPeter Krystad 23415c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 23425c8c1640SGeliang Tang { 23435c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 23445c8c1640SGeliang Tang mptcp_token_destroy(msk); 23455c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 23465c8c1640SGeliang Tang } 23475c8c1640SGeliang Tang 234879c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 234979c0949eSPeter Krystad { 2350c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2351c9fd9c5fSFlorian Westphal 2352c9fd9c5fSFlorian Westphal if (msk->cached_ext) 2353c9fd9c5fSFlorian Westphal __skb_ext_put(msk->cached_ext); 2354d027236cSPaolo Abeni 23555c8c1640SGeliang Tang mptcp_destroy_common(msk); 2356d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 235779c0949eSPeter Krystad } 235879c0949eSPeter Krystad 2359fd1452d8SFlorian Westphal static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname, 2360a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2361fd1452d8SFlorian Westphal { 2362fd1452d8SFlorian Westphal struct sock *sk = (struct sock *)msk; 2363fd1452d8SFlorian Westphal struct socket *ssock; 2364fd1452d8SFlorian Westphal int ret; 2365fd1452d8SFlorian Westphal 2366fd1452d8SFlorian Westphal switch (optname) { 2367fd1452d8SFlorian Westphal case SO_REUSEPORT: 2368fd1452d8SFlorian Westphal case SO_REUSEADDR: 2369fd1452d8SFlorian Westphal lock_sock(sk); 2370fd1452d8SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2371fd1452d8SFlorian Westphal if (!ssock) { 2372fd1452d8SFlorian Westphal release_sock(sk); 2373fd1452d8SFlorian Westphal return -EINVAL; 2374fd1452d8SFlorian Westphal } 2375fd1452d8SFlorian Westphal 2376a7b75c5aSChristoph Hellwig ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen); 2377fd1452d8SFlorian Westphal if (ret == 0) { 2378fd1452d8SFlorian Westphal if (optname == SO_REUSEPORT) 2379fd1452d8SFlorian Westphal sk->sk_reuseport = ssock->sk->sk_reuseport; 2380fd1452d8SFlorian Westphal else if (optname == SO_REUSEADDR) 2381fd1452d8SFlorian Westphal sk->sk_reuse = ssock->sk->sk_reuse; 2382fd1452d8SFlorian Westphal } 2383fd1452d8SFlorian Westphal release_sock(sk); 2384fd1452d8SFlorian Westphal return ret; 2385fd1452d8SFlorian Westphal } 2386fd1452d8SFlorian Westphal 2387a7b75c5aSChristoph Hellwig return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen); 2388fd1452d8SFlorian Westphal } 2389fd1452d8SFlorian Westphal 2390c9b95a13SFlorian Westphal static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname, 2391a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2392c9b95a13SFlorian Westphal { 2393c9b95a13SFlorian Westphal struct sock *sk = (struct sock *)msk; 2394c9b95a13SFlorian Westphal int ret = -EOPNOTSUPP; 2395c9b95a13SFlorian Westphal struct socket *ssock; 2396c9b95a13SFlorian Westphal 2397c9b95a13SFlorian Westphal switch (optname) { 2398c9b95a13SFlorian Westphal case IPV6_V6ONLY: 2399c9b95a13SFlorian Westphal lock_sock(sk); 2400c9b95a13SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2401c9b95a13SFlorian Westphal if (!ssock) { 2402c9b95a13SFlorian Westphal release_sock(sk); 2403c9b95a13SFlorian Westphal return -EINVAL; 2404c9b95a13SFlorian Westphal } 2405c9b95a13SFlorian Westphal 2406c9b95a13SFlorian Westphal ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen); 2407c9b95a13SFlorian Westphal if (ret == 0) 2408c9b95a13SFlorian Westphal sk->sk_ipv6only = ssock->sk->sk_ipv6only; 2409c9b95a13SFlorian Westphal 2410c9b95a13SFlorian Westphal release_sock(sk); 2411c9b95a13SFlorian Westphal break; 2412c9b95a13SFlorian Westphal } 2413c9b95a13SFlorian Westphal 2414c9b95a13SFlorian Westphal return ret; 2415c9b95a13SFlorian Westphal } 2416c9b95a13SFlorian Westphal 2417717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname, 2418a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2419717e79c8SPeter Krystad { 2420717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 242176660afbSPaolo Abeni struct sock *ssk; 2422717e79c8SPeter Krystad 2423717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2424717e79c8SPeter Krystad 242583f0c10bSFlorian Westphal if (level == SOL_SOCKET) 2426fd1452d8SFlorian Westphal return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen); 242783f0c10bSFlorian Westphal 2428717e79c8SPeter Krystad /* @@ the meaning of setsockopt() when the socket is connected and 2429b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2430b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2431b6e4a1aeSMat Martineau * is in TCP fallback, when TCP socket options are passed through 2432b6e4a1aeSMat Martineau * to the one remaining subflow. 2433717e79c8SPeter Krystad */ 2434717e79c8SPeter Krystad lock_sock(sk); 243576660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2436e154659bSFlorian Westphal release_sock(sk); 243776660afbSPaolo Abeni if (ssk) 243876660afbSPaolo Abeni return tcp_setsockopt(ssk, level, optname, optval, optlen); 243950e741bbSFlorian Westphal 2440c9b95a13SFlorian Westphal if (level == SOL_IPV6) 2441c9b95a13SFlorian Westphal return mptcp_setsockopt_v6(msk, optname, optval, optlen); 2442c9b95a13SFlorian Westphal 2443b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2444717e79c8SPeter Krystad } 2445717e79c8SPeter Krystad 2446717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname, 244750e741bbSFlorian Westphal char __user *optval, int __user *option) 2448717e79c8SPeter Krystad { 2449717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 245076660afbSPaolo Abeni struct sock *ssk; 2451717e79c8SPeter Krystad 2452717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2453717e79c8SPeter Krystad 2454b6e4a1aeSMat Martineau /* @@ the meaning of setsockopt() when the socket is connected and 2455b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2456b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2457b6e4a1aeSMat Martineau * is in TCP fallback, when socket options are passed through 2458b6e4a1aeSMat Martineau * to the one remaining subflow. 2459717e79c8SPeter Krystad */ 2460717e79c8SPeter Krystad lock_sock(sk); 246176660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2462e154659bSFlorian Westphal release_sock(sk); 246376660afbSPaolo Abeni if (ssk) 246476660afbSPaolo Abeni return tcp_getsockopt(ssk, level, optname, optval, option); 246550e741bbSFlorian Westphal 2466b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2467717e79c8SPeter Krystad } 2468717e79c8SPeter Krystad 2469b51f9b80SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 2470b51f9b80SPaolo Abeni TCPF_WRITE_TIMER_DEFERRED) 247114c441b5SPaolo Abeni 247214c441b5SPaolo Abeni /* this is very alike tcp_release_cb() but we must handle differently a 247314c441b5SPaolo Abeni * different set of events 247414c441b5SPaolo Abeni */ 247514c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 247614c441b5SPaolo Abeni { 247714c441b5SPaolo Abeni unsigned long flags, nflags; 247814c441b5SPaolo Abeni 247914c441b5SPaolo Abeni do { 248014c441b5SPaolo Abeni flags = sk->sk_tsq_flags; 248114c441b5SPaolo Abeni if (!(flags & MPTCP_DEFERRED_ALL)) 248214c441b5SPaolo Abeni return; 248314c441b5SPaolo Abeni nflags = flags & ~MPTCP_DEFERRED_ALL; 248414c441b5SPaolo Abeni } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 248514c441b5SPaolo Abeni 2486b51f9b80SPaolo Abeni sock_release_ownership(sk); 2487b51f9b80SPaolo Abeni 248814c441b5SPaolo Abeni if (flags & TCPF_DELACK_TIMER_DEFERRED) { 248914c441b5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 249014c441b5SPaolo Abeni struct sock *ssk; 249114c441b5SPaolo Abeni 249214c441b5SPaolo Abeni ssk = mptcp_subflow_recv_lookup(msk); 2493ba8f48f7SPaolo Abeni if (!ssk || sk->sk_state == TCP_CLOSE || 2494ba8f48f7SPaolo Abeni !schedule_work(&msk->work)) 249514c441b5SPaolo Abeni __sock_put(sk); 249614c441b5SPaolo Abeni } 2497b51f9b80SPaolo Abeni 2498b51f9b80SPaolo Abeni if (flags & TCPF_WRITE_TIMER_DEFERRED) { 2499b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 2500b51f9b80SPaolo Abeni __sock_put(sk); 2501b51f9b80SPaolo Abeni } 250214c441b5SPaolo Abeni } 250314c441b5SPaolo Abeni 25042c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 25052c5ebd00SPaolo Abeni { 25062c5ebd00SPaolo Abeni /* should never be called, 25072c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 25082c5ebd00SPaolo Abeni */ 25092c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 25102c5ebd00SPaolo Abeni return 0; 25112c5ebd00SPaolo Abeni } 25122c5ebd00SPaolo Abeni 25132c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 25142c5ebd00SPaolo Abeni { 25152c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 25162c5ebd00SPaolo Abeni } 25172c5ebd00SPaolo Abeni 2518cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 2519f870fa0bSMat Martineau { 2520f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2521cec37a6eSPeter Krystad struct socket *ssock; 2522f870fa0bSMat Martineau 2523cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 2524cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 2525cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 2526cec37a6eSPeter Krystad return -EINVAL; 2527f870fa0bSMat Martineau 2528cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 2529cec37a6eSPeter Krystad } 2530f870fa0bSMat Martineau 2531cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 2532cec37a6eSPeter Krystad { 2533cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 2534cec37a6eSPeter Krystad struct mptcp_sock *msk; 2535cec37a6eSPeter Krystad struct sock *sk; 25366d0060f6SMat Martineau u64 ack_seq; 2537f870fa0bSMat Martineau 2538cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 2539cec37a6eSPeter Krystad sk = subflow->conn; 2540cec37a6eSPeter Krystad msk = mptcp_sk(sk); 2541cec37a6eSPeter Krystad 2542648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 2543648ef4b8SMat Martineau 25446d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 25456d0060f6SMat Martineau ack_seq++; 2546648ef4b8SMat Martineau subflow->map_seq = ack_seq; 2547648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 25486d0060f6SMat Martineau 2549cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 2550cec37a6eSPeter Krystad * accessing the field below 2551cec37a6eSPeter Krystad */ 2552cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 2553cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 25546d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 2555eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 25566d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2557d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 2558cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 25591b1c7a0eSPeter Krystad 25601b1c7a0eSPeter Krystad mptcp_pm_new_connection(msk, 0); 2561a6b118feSFlorian Westphal 2562a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 2563f870fa0bSMat Martineau } 2564f870fa0bSMat Martineau 2565cf7da0d6SPeter Krystad static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 2566cf7da0d6SPeter Krystad { 2567cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 2568cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 2569cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 2570cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 2571cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 2572cf7da0d6SPeter Krystad } 2573cf7da0d6SPeter Krystad 2574e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 2575f296234cSPeter Krystad { 2576e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2577f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 2578f296234cSPeter Krystad struct sock *parent = (void *)msk; 2579f296234cSPeter Krystad struct socket *parent_sock; 2580ec3edaa7SPeter Krystad bool ret; 2581f296234cSPeter Krystad 2582f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 2583f296234cSPeter Krystad 2584f296234cSPeter Krystad /* mptcp socket already closing? */ 2585b93df08cSPaolo Abeni if (!mptcp_is_fully_established(parent)) 2586f296234cSPeter Krystad return false; 2587f296234cSPeter Krystad 2588f296234cSPeter Krystad if (!msk->pm.server_side) 2589f296234cSPeter Krystad return true; 2590f296234cSPeter Krystad 259110f6d46cSPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 259210f6d46cSPaolo Abeni return false; 259310f6d46cSPaolo Abeni 259410f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 259510f6d46cSPaolo Abeni * msk lock here. 259610f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 2597e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 259810f6d46cSPaolo Abeni */ 259910f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 260010f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 2601e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 260210f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 2603e16163b6SPaolo Abeni sock_hold(ssk); 2604e16163b6SPaolo Abeni } 260510f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 260610f6d46cSPaolo Abeni if (!ret) 260710f6d46cSPaolo Abeni return false; 260810f6d46cSPaolo Abeni 260910f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 261010f6d46cSPaolo Abeni * at close time 261110f6d46cSPaolo Abeni */ 2612f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 2613e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 2614e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 2615917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 261610f6d46cSPaolo Abeni return true; 2617f296234cSPeter Krystad } 2618f296234cSPeter Krystad 26191891c4a0SFlorian Westphal static bool mptcp_memory_free(const struct sock *sk, int wake) 26201891c4a0SFlorian Westphal { 26211891c4a0SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 26221891c4a0SFlorian Westphal 26231891c4a0SFlorian Westphal return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 26241891c4a0SFlorian Westphal } 26251891c4a0SFlorian Westphal 2626f870fa0bSMat Martineau static struct proto mptcp_prot = { 2627f870fa0bSMat Martineau .name = "MPTCP", 2628f870fa0bSMat Martineau .owner = THIS_MODULE, 2629f870fa0bSMat Martineau .init = mptcp_init_sock, 263018b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 2631f870fa0bSMat Martineau .close = mptcp_close, 2632cf7da0d6SPeter Krystad .accept = mptcp_accept, 2633717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 2634717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 2635f870fa0bSMat Martineau .shutdown = tcp_shutdown, 263679c0949eSPeter Krystad .destroy = mptcp_destroy, 2637f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 2638f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 263914c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 26402c5ebd00SPaolo Abeni .hash = mptcp_hash, 26412c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 2642cec37a6eSPeter Krystad .get_port = mptcp_get_port, 2643d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 2644d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 2645d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 26461891c4a0SFlorian Westphal .stream_memory_free = mptcp_memory_free, 2647d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 2648989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 2649d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 2650f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 26512c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 2652f870fa0bSMat Martineau .no_autobind = true, 2653f870fa0bSMat Martineau }; 2654f870fa0bSMat Martineau 26552303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 26562303f994SPeter Krystad { 26572303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 26582303f994SPeter Krystad struct socket *ssock; 2659cf7da0d6SPeter Krystad int err; 26602303f994SPeter Krystad 26612303f994SPeter Krystad lock_sock(sock->sk); 2662fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2663fa68018dSPaolo Abeni if (!ssock) { 2664fa68018dSPaolo Abeni err = -EINVAL; 26652303f994SPeter Krystad goto unlock; 26662303f994SPeter Krystad } 26672303f994SPeter Krystad 26682303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 2669cf7da0d6SPeter Krystad if (!err) 2670cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 26712303f994SPeter Krystad 26722303f994SPeter Krystad unlock: 26732303f994SPeter Krystad release_sock(sock->sk); 26742303f994SPeter Krystad return err; 26752303f994SPeter Krystad } 26762303f994SPeter Krystad 26770235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 26780235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 26790235d075SPaolo Abeni { 26800235d075SPaolo Abeni subflow->request_mptcp = 0; 26810235d075SPaolo Abeni __mptcp_do_fallback(msk); 26820235d075SPaolo Abeni } 26830235d075SPaolo Abeni 26842303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 26852303f994SPeter Krystad int addr_len, int flags) 26862303f994SPeter Krystad { 26872303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 26882c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 26892303f994SPeter Krystad struct socket *ssock; 26902303f994SPeter Krystad int err; 26912303f994SPeter Krystad 26922303f994SPeter Krystad lock_sock(sock->sk); 269341be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 269441be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 269541be81a8SPaolo Abeni * cope with that 269641be81a8SPaolo Abeni */ 269741be81a8SPaolo Abeni ssock = msk->subflow; 269841be81a8SPaolo Abeni goto do_connect; 269941be81a8SPaolo Abeni } 270041be81a8SPaolo Abeni 2701fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2702fa68018dSPaolo Abeni if (!ssock) { 2703fa68018dSPaolo Abeni err = -EINVAL; 27042303f994SPeter Krystad goto unlock; 27052303f994SPeter Krystad } 27062303f994SPeter Krystad 2707fa68018dSPaolo Abeni mptcp_token_destroy(msk); 2708fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 27092c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 2710cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 2711cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 2712cf7da0d6SPeter Krystad * TCP option space. 2713cf7da0d6SPeter Krystad */ 2714cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 27150235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 2716cf7da0d6SPeter Krystad #endif 27172c5ebd00SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) 27180235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 2719cf7da0d6SPeter Krystad 272041be81a8SPaolo Abeni do_connect: 27212303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 272241be81a8SPaolo Abeni sock->state = ssock->state; 272341be81a8SPaolo Abeni 272441be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 272541be81a8SPaolo Abeni * subflow_finish_connect() 272641be81a8SPaolo Abeni */ 2727367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 2728cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 272941be81a8SPaolo Abeni else 273041be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 27312303f994SPeter Krystad 27322303f994SPeter Krystad unlock: 27332303f994SPeter Krystad release_sock(sock->sk); 27342303f994SPeter Krystad return err; 27352303f994SPeter Krystad } 27362303f994SPeter Krystad 2737cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 2738cf7da0d6SPeter Krystad { 2739cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2740cf7da0d6SPeter Krystad struct socket *ssock; 2741cf7da0d6SPeter Krystad int err; 2742cf7da0d6SPeter Krystad 2743cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 2744cf7da0d6SPeter Krystad 2745cf7da0d6SPeter Krystad lock_sock(sock->sk); 2746fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2747fa68018dSPaolo Abeni if (!ssock) { 2748fa68018dSPaolo Abeni err = -EINVAL; 2749cf7da0d6SPeter Krystad goto unlock; 2750cf7da0d6SPeter Krystad } 2751cf7da0d6SPeter Krystad 2752fa68018dSPaolo Abeni mptcp_token_destroy(msk); 2753fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 27545e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 27555e20087dSFlorian Westphal 2756cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 2757cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 2758cf7da0d6SPeter Krystad if (!err) 2759cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 2760cf7da0d6SPeter Krystad 2761cf7da0d6SPeter Krystad unlock: 2762cf7da0d6SPeter Krystad release_sock(sock->sk); 2763cf7da0d6SPeter Krystad return err; 2764cf7da0d6SPeter Krystad } 2765cf7da0d6SPeter Krystad 2766cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 2767cf7da0d6SPeter Krystad int flags, bool kern) 2768cf7da0d6SPeter Krystad { 2769cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2770cf7da0d6SPeter Krystad struct socket *ssock; 2771cf7da0d6SPeter Krystad int err; 2772cf7da0d6SPeter Krystad 2773cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 2774cf7da0d6SPeter Krystad 2775cf7da0d6SPeter Krystad lock_sock(sock->sk); 2776cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 2777cf7da0d6SPeter Krystad goto unlock_fail; 2778cf7da0d6SPeter Krystad 2779cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 2780cf7da0d6SPeter Krystad if (!ssock) 2781cf7da0d6SPeter Krystad goto unlock_fail; 2782cf7da0d6SPeter Krystad 27838a05661bSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 2784cf7da0d6SPeter Krystad sock_hold(ssock->sk); 2785cf7da0d6SPeter Krystad release_sock(sock->sk); 2786cf7da0d6SPeter Krystad 2787cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 2788d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 2789cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 2790cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2791cf7da0d6SPeter Krystad 2792cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 2793cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 2794cf7da0d6SPeter Krystad */ 2795ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 2796190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2797cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2798cf7da0d6SPeter Krystad 2799cf7da0d6SPeter Krystad if (!ssk->sk_socket) 2800cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 2801cf7da0d6SPeter Krystad } 2802cf7da0d6SPeter Krystad } 2803cf7da0d6SPeter Krystad 28048a05661bSPaolo Abeni if (inet_csk_listen_poll(ssock->sk)) 28058a05661bSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 2806cf7da0d6SPeter Krystad sock_put(ssock->sk); 2807cf7da0d6SPeter Krystad return err; 2808cf7da0d6SPeter Krystad 2809cf7da0d6SPeter Krystad unlock_fail: 2810cf7da0d6SPeter Krystad release_sock(sock->sk); 2811cf7da0d6SPeter Krystad return -EINVAL; 2812cf7da0d6SPeter Krystad } 2813cf7da0d6SPeter Krystad 28148a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 28158a05661bSPaolo Abeni { 28168a05661bSPaolo Abeni return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 28178a05661bSPaolo Abeni 0; 28188a05661bSPaolo Abeni } 28198a05661bSPaolo Abeni 28202303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 28212303f994SPeter Krystad struct poll_table_struct *wait) 28222303f994SPeter Krystad { 28231891c4a0SFlorian Westphal struct sock *sk = sock->sk; 28248ab183deSPaolo Abeni struct mptcp_sock *msk; 28252303f994SPeter Krystad __poll_t mask = 0; 28268a05661bSPaolo Abeni int state; 28272303f994SPeter Krystad 28281891c4a0SFlorian Westphal msk = mptcp_sk(sk); 28291891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 28301891c4a0SFlorian Westphal 28318a05661bSPaolo Abeni state = inet_sk_state_load(sk); 28326719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 28338a05661bSPaolo Abeni if (state == TCP_LISTEN) 28348a05661bSPaolo Abeni return mptcp_check_readable(msk); 28358a05661bSPaolo Abeni 28368a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 28378a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 283863561a40SPaolo Abeni if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 28391891c4a0SFlorian Westphal mask |= EPOLLOUT | EPOLLWRNORM; 28408a05661bSPaolo Abeni } 28411891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 28421891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 28431891c4a0SFlorian Westphal 28442303f994SPeter Krystad return mask; 28452303f994SPeter Krystad } 28462303f994SPeter Krystad 284721498490SPeter Krystad static int mptcp_shutdown(struct socket *sock, int how) 284821498490SPeter Krystad { 284921498490SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2850e16163b6SPaolo Abeni struct sock *sk = sock->sk; 285121498490SPeter Krystad int ret = 0; 285221498490SPeter Krystad 285321498490SPeter Krystad pr_debug("sk=%p, how=%d", msk, how); 285421498490SPeter Krystad 2855e16163b6SPaolo Abeni lock_sock(sk); 285621498490SPeter Krystad 285721498490SPeter Krystad how++; 285821498490SPeter Krystad if ((how & ~SHUTDOWN_MASK) || !how) { 285921498490SPeter Krystad ret = -EINVAL; 286021498490SPeter Krystad goto out_unlock; 286121498490SPeter Krystad } 286221498490SPeter Krystad 286321498490SPeter Krystad if (sock->state == SS_CONNECTING) { 2864e16163b6SPaolo Abeni if ((1 << sk->sk_state) & 286521498490SPeter Krystad (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 286621498490SPeter Krystad sock->state = SS_DISCONNECTING; 286721498490SPeter Krystad else 286821498490SPeter Krystad sock->state = SS_CONNECTED; 286921498490SPeter Krystad } 287021498490SPeter Krystad 2871e16163b6SPaolo Abeni sk->sk_shutdown |= how; 2872e16163b6SPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 2873e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 287421498490SPeter Krystad 2875e1ff9e82SDavide Caratti /* Wake up anyone sleeping in poll. */ 2876e16163b6SPaolo Abeni sk->sk_state_change(sk); 2877e1ff9e82SDavide Caratti 287821498490SPeter Krystad out_unlock: 2879e16163b6SPaolo Abeni release_sock(sk); 288021498490SPeter Krystad 288121498490SPeter Krystad return ret; 288221498490SPeter Krystad } 288321498490SPeter Krystad 2884e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 2885e42f1ac6SFlorian Westphal .family = PF_INET, 2886e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2887e42f1ac6SFlorian Westphal .release = inet_release, 2888e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2889e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2890e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2891e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2892d2f77c53SPaolo Abeni .getname = inet_getname, 2893e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2894e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 2895e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2896e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2897e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2898e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2899e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2900e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 2901e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 2902e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2903e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2904e42f1ac6SFlorian Westphal }; 29052303f994SPeter Krystad 2906f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 2907f870fa0bSMat Martineau .type = SOCK_STREAM, 2908f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2909f870fa0bSMat Martineau .prot = &mptcp_prot, 29102303f994SPeter Krystad .ops = &mptcp_stream_ops, 29112303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 2912f870fa0bSMat Martineau }; 2913f870fa0bSMat Martineau 2914d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 2915f870fa0bSMat Martineau { 29162303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 29172303f994SPeter Krystad 2918d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 2919d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 2920d027236cSPaolo Abeni 29212303f994SPeter Krystad mptcp_subflow_init(); 29221b1c7a0eSPeter Krystad mptcp_pm_init(); 29232c5ebd00SPaolo Abeni mptcp_token_init(); 29242303f994SPeter Krystad 2925f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 2926f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 2927f870fa0bSMat Martineau 2928f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 29296771bfd9SFlorian Westphal 29306771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 2931f870fa0bSMat Martineau } 2932f870fa0bSMat Martineau 2933f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2934e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 2935e42f1ac6SFlorian Westphal .family = PF_INET6, 2936e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2937e42f1ac6SFlorian Westphal .release = inet6_release, 2938e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2939e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2940e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2941e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2942d2f77c53SPaolo Abeni .getname = inet6_getname, 2943e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2944e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 2945e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2946e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2947e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2948e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2949e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2950e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 2951e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 2952e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2953e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2954e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 29553986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 2956e42f1ac6SFlorian Westphal #endif 2957e42f1ac6SFlorian Westphal }; 2958e42f1ac6SFlorian Westphal 2959f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 2960f870fa0bSMat Martineau 296179c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 296279c0949eSPeter Krystad { 296379c0949eSPeter Krystad mptcp_destroy(sk); 296479c0949eSPeter Krystad inet6_destroy_sock(sk); 296579c0949eSPeter Krystad } 296679c0949eSPeter Krystad 2967f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 2968f870fa0bSMat Martineau .type = SOCK_STREAM, 2969f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2970f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 29712303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 2972f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 2973f870fa0bSMat Martineau }; 2974f870fa0bSMat Martineau 2975d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 2976f870fa0bSMat Martineau { 2977f870fa0bSMat Martineau int err; 2978f870fa0bSMat Martineau 2979f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 2980f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 2981f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 298279c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 2983b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 2984f870fa0bSMat Martineau 2985f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 2986f870fa0bSMat Martineau if (err) 2987f870fa0bSMat Martineau return err; 2988f870fa0bSMat Martineau 2989f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 2990f870fa0bSMat Martineau if (err) 2991f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 2992f870fa0bSMat Martineau 2993f870fa0bSMat Martineau return err; 2994f870fa0bSMat Martineau } 2995f870fa0bSMat Martineau #endif 2996