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> 24*e16163b6SPaolo 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 45*e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 46*e16163b6SPaolo Abeni 472303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 482303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 492303f994SPeter Krystad * Otherwise return NULL. 502303f994SPeter Krystad */ 512303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 522303f994SPeter Krystad { 53d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 542303f994SPeter Krystad return NULL; 552303f994SPeter Krystad 562303f994SPeter Krystad return msk->subflow; 572303f994SPeter Krystad } 582303f994SPeter Krystad 59d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 600b4f33deSFlorian Westphal { 610b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 620b4f33deSFlorian Westphal 630b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 640b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 650b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 660b4f33deSFlorian Westphal * not an mptcp one. 670b4f33deSFlorian Westphal * 680b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 690b4f33deSFlorian Westphal * bypass mptcp. 700b4f33deSFlorian Westphal */ 710b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 72d2f77c53SPaolo Abeni return true; 730b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 740b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 750b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 76d2f77c53SPaolo Abeni return true; 770b4f33deSFlorian Westphal #endif 780b4f33deSFlorian Westphal } 790b4f33deSFlorian Westphal 80d2f77c53SPaolo Abeni return false; 810b4f33deSFlorian Westphal } 820b4f33deSFlorian Westphal 8376660afbSPaolo Abeni static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk) 84cec37a6eSPeter Krystad { 85cec37a6eSPeter Krystad sock_owned_by_me((const struct sock *)msk); 86cec37a6eSPeter Krystad 87e1ff9e82SDavide Caratti if (likely(!__mptcp_check_fallback(msk))) 88cec37a6eSPeter Krystad return NULL; 89cec37a6eSPeter Krystad 9076660afbSPaolo Abeni return msk->first; 918ab183deSPaolo Abeni } 928ab183deSPaolo Abeni 93fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 942303f994SPeter Krystad { 952303f994SPeter Krystad struct mptcp_subflow_context *subflow; 962303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 972303f994SPeter Krystad struct socket *ssock; 982303f994SPeter Krystad int err; 992303f994SPeter Krystad 1002303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1012303f994SPeter Krystad if (err) 102fa68018dSPaolo Abeni return err; 1032303f994SPeter Krystad 1048ab183deSPaolo Abeni msk->first = ssock->sk; 1052303f994SPeter Krystad msk->subflow = ssock; 1062303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 107cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 108*e16163b6SPaolo Abeni sock_hold(ssock->sk); 1092303f994SPeter Krystad subflow->request_mptcp = 1; 1102303f994SPeter Krystad 111e1ff9e82SDavide Caratti /* accept() will wait on first subflow sk_wq, and we always wakes up 112e1ff9e82SDavide Caratti * via msk->sk_socket 113e1ff9e82SDavide Caratti */ 114e1ff9e82SDavide Caratti RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq); 115e1ff9e82SDavide Caratti 116fa68018dSPaolo Abeni return 0; 1172303f994SPeter Krystad } 1182303f994SPeter Krystad 119ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 120ab174ad8SPaolo Abeni { 121ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 122ab174ad8SPaolo Abeni __kfree_skb(skb); 123ab174ad8SPaolo Abeni } 124ab174ad8SPaolo Abeni 1258268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1268268ed4cSPaolo Abeni struct sk_buff *from) 1278268ed4cSPaolo Abeni { 1288268ed4cSPaolo Abeni bool fragstolen; 1298268ed4cSPaolo Abeni int delta; 1308268ed4cSPaolo Abeni 1318268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1328268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1338268ed4cSPaolo Abeni return false; 1348268ed4cSPaolo Abeni 13506242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 13606242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 13706242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 138ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1398268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1408268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1418268ed4cSPaolo Abeni sk_mem_charge(sk, delta); 1428268ed4cSPaolo Abeni return true; 1438268ed4cSPaolo Abeni } 1448268ed4cSPaolo Abeni 145ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 146ab174ad8SPaolo Abeni struct sk_buff *from) 1476771bfd9SFlorian Westphal { 148ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 149ab174ad8SPaolo Abeni return false; 150ab174ad8SPaolo Abeni 151ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 152ab174ad8SPaolo Abeni } 153ab174ad8SPaolo Abeni 154ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 155ab174ad8SPaolo Abeni * - use mptcp seqs 156ab174ad8SPaolo Abeni * - don't cope with sacks 157ab174ad8SPaolo Abeni */ 158ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 159ab174ad8SPaolo Abeni { 160ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 161ab174ad8SPaolo Abeni struct rb_node **p, *parent; 162ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 163ab174ad8SPaolo Abeni struct sk_buff *skb1; 164c2ec6bc0SYe Bin int space; 165ab174ad8SPaolo Abeni 166ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 167ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 168c2ec6bc0SYe Bin space = tcp_space(sk); 169c2ec6bc0SYe Bin max_seq = space > 0 ? space + msk->ack_seq : msk->ack_seq; 170ab174ad8SPaolo Abeni 17106242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 17206242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 173ab174ad8SPaolo Abeni if (after64(seq, max_seq)) { 174ab174ad8SPaolo Abeni /* out of window */ 175ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 17606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 177ab174ad8SPaolo Abeni return; 178ab174ad8SPaolo Abeni } 179ab174ad8SPaolo Abeni 180ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 18106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 182ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 183ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 184ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 185ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 186ab174ad8SPaolo Abeni goto end; 187ab174ad8SPaolo Abeni } 188ab174ad8SPaolo Abeni 189ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 190ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 191ab174ad8SPaolo Abeni */ 19206242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 19306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 19406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 195ab174ad8SPaolo Abeni return; 19606242e44SPaolo Abeni } 197ab174ad8SPaolo Abeni 198ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 199ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 20006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 201ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 202ab174ad8SPaolo Abeni p = &parent->rb_right; 203ab174ad8SPaolo Abeni goto insert; 204ab174ad8SPaolo Abeni } 205ab174ad8SPaolo Abeni 206ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 207ab174ad8SPaolo Abeni parent = NULL; 208ab174ad8SPaolo Abeni while (*p) { 209ab174ad8SPaolo Abeni parent = *p; 210ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 211ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 212ab174ad8SPaolo Abeni p = &parent->rb_left; 213ab174ad8SPaolo Abeni continue; 214ab174ad8SPaolo Abeni } 215ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 216ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 217ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 218ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 21906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 220ab174ad8SPaolo Abeni return; 221ab174ad8SPaolo Abeni } 222ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 223ab174ad8SPaolo Abeni /* partial overlap: 224ab174ad8SPaolo Abeni * | skb | 225ab174ad8SPaolo Abeni * | skb1 | 226ab174ad8SPaolo Abeni * continue traversing 227ab174ad8SPaolo Abeni */ 228ab174ad8SPaolo Abeni } else { 229ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 230ab174ad8SPaolo Abeni * Replace skb1 with skb. 231ab174ad8SPaolo Abeni */ 232ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 233ab174ad8SPaolo Abeni &msk->out_of_order_queue); 234ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 23506242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 236ab174ad8SPaolo Abeni goto merge_right; 237ab174ad8SPaolo Abeni } 238ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 23906242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 240ab174ad8SPaolo Abeni return; 241ab174ad8SPaolo Abeni } 242ab174ad8SPaolo Abeni p = &parent->rb_right; 243ab174ad8SPaolo Abeni } 24406242e44SPaolo Abeni 245ab174ad8SPaolo Abeni insert: 246ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 247ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 248ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 249ab174ad8SPaolo Abeni 250ab174ad8SPaolo Abeni merge_right: 251ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 252ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 253ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 254ab174ad8SPaolo Abeni break; 255ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 256ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 25706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 258ab174ad8SPaolo Abeni } 259ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 260ab174ad8SPaolo Abeni if (!skb1) 261ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 262ab174ad8SPaolo Abeni 263ab174ad8SPaolo Abeni end: 264ab174ad8SPaolo Abeni skb_condense(skb); 265ab174ad8SPaolo Abeni skb_set_owner_r(skb, sk); 266ab174ad8SPaolo Abeni } 267ab174ad8SPaolo Abeni 268ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 269ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 270ab174ad8SPaolo Abeni size_t copy_len) 271ab174ad8SPaolo Abeni { 272ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2736771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 2744e637c70SFlorian Westphal struct sk_buff *tail; 2756771bfd9SFlorian Westphal 2766771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 2774e637c70SFlorian Westphal 2784e637c70SFlorian Westphal skb_ext_reset(skb); 2794e637c70SFlorian Westphal skb_orphan(skb); 2804e637c70SFlorian Westphal 2819c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 2829c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) { 2839c3f94e1SPaolo Abeni if (ssk->sk_forward_alloc < skb->truesize) 2849c3f94e1SPaolo Abeni goto drop; 2859c3f94e1SPaolo Abeni __sk_mem_reclaim(ssk, skb->truesize); 2869c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) 2879c3f94e1SPaolo Abeni goto drop; 2889c3f94e1SPaolo Abeni } 2899c3f94e1SPaolo Abeni 290ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 291ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 292ab174ad8SPaolo Abeni * value 293ab174ad8SPaolo Abeni */ 294ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 295ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 296ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 297ab174ad8SPaolo Abeni 298ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 299ab174ad8SPaolo Abeni /* in sequence */ 300917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3014e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3028268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 303ab174ad8SPaolo Abeni return true; 3044e637c70SFlorian Westphal 305600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 3066771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 307ab174ad8SPaolo Abeni return true; 308ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 309ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 310ab174ad8SPaolo Abeni return false; 311ab174ad8SPaolo Abeni } 312ab174ad8SPaolo Abeni 313ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 314ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 315ab174ad8SPaolo Abeni */ 31606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3179c3f94e1SPaolo Abeni drop: 318ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 319ab174ad8SPaolo Abeni return false; 3206771bfd9SFlorian Westphal } 3216771bfd9SFlorian Westphal 32216a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 32316a9a9daSMat Martineau { 32416a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 32516a9a9daSMat Martineau 32616a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 32716a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 32816a9a9daSMat Martineau } 32916a9a9daSMat Martineau 330*e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 331*e16163b6SPaolo Abeni { 332*e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 333*e16163b6SPaolo Abeni return; 334*e16163b6SPaolo Abeni 335*e16163b6SPaolo Abeni sk->sk_state_change(sk); 336*e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 337*e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 338*e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 339*e16163b6SPaolo Abeni else 340*e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 341*e16163b6SPaolo Abeni } 342*e16163b6SPaolo Abeni 34316a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 34416a9a9daSMat Martineau { 34516a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 34616a9a9daSMat Martineau 34716a9a9daSMat Martineau if (__mptcp_check_fallback(msk)) 34816a9a9daSMat Martineau return; 34916a9a9daSMat Martineau 35016a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 35116a9a9daSMat Martineau if (((1 << sk->sk_state) & 35216a9a9daSMat Martineau (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 35316a9a9daSMat Martineau msk->write_seq == atomic64_read(&msk->snd_una)) { 35416a9a9daSMat Martineau mptcp_stop_timer(sk); 35516a9a9daSMat Martineau 35616a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 35716a9a9daSMat Martineau 35816a9a9daSMat Martineau switch (sk->sk_state) { 35916a9a9daSMat Martineau case TCP_FIN_WAIT1: 36016a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 36116a9a9daSMat Martineau break; 36216a9a9daSMat Martineau case TCP_CLOSING: 36316a9a9daSMat Martineau case TCP_LAST_ACK: 36416a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 36516a9a9daSMat Martineau break; 36616a9a9daSMat Martineau } 36716a9a9daSMat Martineau 368*e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 36916a9a9daSMat Martineau } 37016a9a9daSMat Martineau } 37116a9a9daSMat Martineau 3723721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 3733721b9b6SMat Martineau { 3743721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3753721b9b6SMat Martineau 3763721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 3773721b9b6SMat Martineau ((1 << sk->sk_state) & 3783721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 3793721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 3803721b9b6SMat Martineau 3813721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 3823721b9b6SMat Martineau if (seq) 3833721b9b6SMat Martineau *seq = rcv_data_fin_seq; 3843721b9b6SMat Martineau 3853721b9b6SMat Martineau return true; 3863721b9b6SMat Martineau } 3873721b9b6SMat Martineau } 3883721b9b6SMat Martineau 3893721b9b6SMat Martineau return false; 3903721b9b6SMat Martineau } 3913721b9b6SMat Martineau 3923721b9b6SMat Martineau static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 3933721b9b6SMat Martineau { 3943721b9b6SMat Martineau long tout = ssk && inet_csk(ssk)->icsk_pending ? 3953721b9b6SMat Martineau inet_csk(ssk)->icsk_timeout - jiffies : 0; 3963721b9b6SMat Martineau 3973721b9b6SMat Martineau if (tout <= 0) 3983721b9b6SMat Martineau tout = mptcp_sk(sk)->timer_ival; 3993721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4003721b9b6SMat Martineau } 4013721b9b6SMat Martineau 4023721b9b6SMat Martineau static void mptcp_check_data_fin(struct sock *sk) 4033721b9b6SMat Martineau { 4043721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4053721b9b6SMat Martineau u64 rcv_data_fin_seq; 4063721b9b6SMat Martineau 4073721b9b6SMat Martineau if (__mptcp_check_fallback(msk) || !msk->first) 4083721b9b6SMat Martineau return; 4093721b9b6SMat Martineau 4103721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 4113721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 4123721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 4133721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 4143721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 4153721b9b6SMat Martineau * the msk state. 4163721b9b6SMat Martineau * 4173721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 4183721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 4193721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 4203721b9b6SMat Martineau * when catching up. 4213721b9b6SMat Martineau */ 4223721b9b6SMat Martineau 4233721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 4243721b9b6SMat Martineau struct mptcp_subflow_context *subflow; 4253721b9b6SMat Martineau 426917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 4273721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 4283721b9b6SMat Martineau 4293721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 43016a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 43116a9a9daSMat Martineau set_bit(MPTCP_DATA_READY, &msk->flags); 4323721b9b6SMat Martineau 4333721b9b6SMat Martineau switch (sk->sk_state) { 4343721b9b6SMat Martineau case TCP_ESTABLISHED: 4353721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 4363721b9b6SMat Martineau break; 4373721b9b6SMat Martineau case TCP_FIN_WAIT1: 4383721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 4393721b9b6SMat Martineau break; 4403721b9b6SMat Martineau case TCP_FIN_WAIT2: 4413721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 4423721b9b6SMat Martineau break; 4433721b9b6SMat Martineau default: 4443721b9b6SMat Martineau /* Other states not expected */ 4453721b9b6SMat Martineau WARN_ON_ONCE(1); 4463721b9b6SMat Martineau break; 4473721b9b6SMat Martineau } 4483721b9b6SMat Martineau 4493721b9b6SMat Martineau mptcp_set_timeout(sk, NULL); 4503721b9b6SMat Martineau mptcp_for_each_subflow(msk, subflow) { 4513721b9b6SMat Martineau struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 4523721b9b6SMat Martineau 4533721b9b6SMat Martineau lock_sock(ssk); 4543721b9b6SMat Martineau tcp_send_ack(ssk); 4553721b9b6SMat Martineau release_sock(ssk); 4563721b9b6SMat Martineau } 4573721b9b6SMat Martineau 458*e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 4593721b9b6SMat Martineau } 4603721b9b6SMat Martineau } 4613721b9b6SMat Martineau 4626771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 4636771bfd9SFlorian Westphal struct sock *ssk, 4646771bfd9SFlorian Westphal unsigned int *bytes) 4656771bfd9SFlorian Westphal { 4666771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 467600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 4686771bfd9SFlorian Westphal unsigned int moved = 0; 4696771bfd9SFlorian Westphal bool more_data_avail; 4706771bfd9SFlorian Westphal struct tcp_sock *tp; 471717f2034SPaolo Abeni u32 old_copied_seq; 4726771bfd9SFlorian Westphal bool done = false; 47313c7ba0cSFlorian Westphal int sk_rbuf; 47413c7ba0cSFlorian Westphal 47513c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 47613c7ba0cSFlorian Westphal 47713c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 47813c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 47913c7ba0cSFlorian Westphal 48013c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 48113c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 48213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 48313c7ba0cSFlorian Westphal } 48413c7ba0cSFlorian Westphal } 485600911ffSFlorian Westphal 486ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 4876771bfd9SFlorian Westphal tp = tcp_sk(ssk); 488717f2034SPaolo Abeni old_copied_seq = tp->copied_seq; 4896771bfd9SFlorian Westphal do { 4906771bfd9SFlorian Westphal u32 map_remaining, offset; 4916771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 4926771bfd9SFlorian Westphal struct sk_buff *skb; 4936771bfd9SFlorian Westphal bool fin; 4946771bfd9SFlorian Westphal 4956771bfd9SFlorian Westphal /* try to move as much data as available */ 4966771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 4976771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 4986771bfd9SFlorian Westphal 4996771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 500d9fb8c50SPaolo Abeni if (!skb) { 501d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 502d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 503d9fb8c50SPaolo Abeni * can enter an infinite loop 504d9fb8c50SPaolo Abeni */ 505d9fb8c50SPaolo Abeni if (!moved) 506d9fb8c50SPaolo Abeni done = true; 5076771bfd9SFlorian Westphal break; 508d9fb8c50SPaolo Abeni } 5096771bfd9SFlorian Westphal 510e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 511e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 512e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 513e1ff9e82SDavide Caratti * be sure to adjust the size 514e1ff9e82SDavide Caratti */ 515e1ff9e82SDavide Caratti map_remaining = skb->len; 516e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 517e1ff9e82SDavide Caratti } 518e1ff9e82SDavide Caratti 5196771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 5206771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 5216771bfd9SFlorian Westphal if (fin) { 5226771bfd9SFlorian Westphal done = true; 5236771bfd9SFlorian Westphal seq++; 5246771bfd9SFlorian Westphal } 5256771bfd9SFlorian Westphal 5266771bfd9SFlorian Westphal if (offset < skb->len) { 5276771bfd9SFlorian Westphal size_t len = skb->len - offset; 5286771bfd9SFlorian Westphal 5296771bfd9SFlorian Westphal if (tp->urg_data) 5306771bfd9SFlorian Westphal done = true; 5316771bfd9SFlorian Westphal 532ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 5336771bfd9SFlorian Westphal moved += len; 534ab174ad8SPaolo Abeni seq += len; 5356771bfd9SFlorian Westphal 5366771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 5376771bfd9SFlorian Westphal break; 5386771bfd9SFlorian Westphal } else { 5396771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 5406771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 5416771bfd9SFlorian Westphal done = true; 5426771bfd9SFlorian Westphal } 5436771bfd9SFlorian Westphal 5446771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 5456771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 546600911ffSFlorian Westphal 54713c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 548600911ffSFlorian Westphal done = true; 549600911ffSFlorian Westphal break; 550600911ffSFlorian Westphal } 5516771bfd9SFlorian Westphal } while (more_data_avail); 5526771bfd9SFlorian Westphal 5536719331cSPaolo Abeni *bytes += moved; 554717f2034SPaolo Abeni if (tp->copied_seq != old_copied_seq) 555717f2034SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 5566771bfd9SFlorian Westphal 5576771bfd9SFlorian Westphal return done; 5586771bfd9SFlorian Westphal } 5596771bfd9SFlorian Westphal 560ab174ad8SPaolo Abeni static bool mptcp_ofo_queue(struct mptcp_sock *msk) 561ab174ad8SPaolo Abeni { 562ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 563ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 564ab174ad8SPaolo Abeni bool moved = false; 565ab174ad8SPaolo Abeni struct rb_node *p; 566ab174ad8SPaolo Abeni u64 end_seq; 567ab174ad8SPaolo Abeni 568ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 56906242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 570ab174ad8SPaolo Abeni while (p) { 571ab174ad8SPaolo Abeni skb = rb_to_skb(p); 572ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 573ab174ad8SPaolo Abeni break; 574ab174ad8SPaolo Abeni 575ab174ad8SPaolo Abeni p = rb_next(p); 576ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 577ab174ad8SPaolo Abeni 578ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 579ab174ad8SPaolo Abeni msk->ack_seq))) { 580ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 58106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 582ab174ad8SPaolo Abeni continue; 583ab174ad8SPaolo Abeni } 584ab174ad8SPaolo Abeni 585ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 586ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 587ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 588ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 589ab174ad8SPaolo Abeni 590ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 59106242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 59206242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 59306242e44SPaolo Abeni delta); 594ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 595ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 596ab174ad8SPaolo Abeni } 597ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 598ab174ad8SPaolo Abeni moved = true; 599ab174ad8SPaolo Abeni } 600ab174ad8SPaolo Abeni return moved; 601ab174ad8SPaolo Abeni } 602ab174ad8SPaolo Abeni 6032e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 6042e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 6052e52213cSFlorian Westphal */ 6062e52213cSFlorian Westphal static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 6072e52213cSFlorian Westphal { 6082e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 6092e52213cSFlorian Westphal unsigned int moved = 0; 6102e52213cSFlorian Westphal 6112e52213cSFlorian Westphal if (READ_ONCE(sk->sk_lock.owned)) 6122e52213cSFlorian Westphal return false; 6132e52213cSFlorian Westphal 6142e52213cSFlorian Westphal if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 6152e52213cSFlorian Westphal return false; 6162e52213cSFlorian Westphal 6172e52213cSFlorian Westphal /* must re-check after taking the lock */ 618ab174ad8SPaolo Abeni if (!READ_ONCE(sk->sk_lock.owned)) { 6192e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 620ab174ad8SPaolo Abeni mptcp_ofo_queue(msk); 621ab174ad8SPaolo Abeni 622ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 623ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 624ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 625ab174ad8SPaolo Abeni * do it. 626ab174ad8SPaolo Abeni */ 627ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 628ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 629ab174ad8SPaolo Abeni } 6302e52213cSFlorian Westphal 6312e52213cSFlorian Westphal spin_unlock_bh(&sk->sk_lock.slock); 6322e52213cSFlorian Westphal 6332e52213cSFlorian Westphal return moved > 0; 6342e52213cSFlorian Westphal } 6352e52213cSFlorian Westphal 6362e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 637101f6f85SFlorian Westphal { 6386719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 639101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 64013c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 6416719331cSPaolo Abeni bool wake; 642101f6f85SFlorian Westphal 6436719331cSPaolo Abeni /* move_skbs_to_msk below can legitly clear the data_avail flag, 6446719331cSPaolo Abeni * but we will need later to properly woke the reader, cache its 6456719331cSPaolo Abeni * value 6466719331cSPaolo Abeni */ 6476719331cSPaolo Abeni wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL; 6486719331cSPaolo Abeni if (wake) 649101f6f85SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 6506771bfd9SFlorian Westphal 65113c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 65213c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 65313c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 65413c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 65513c7ba0cSFlorian Westphal 65613c7ba0cSFlorian Westphal /* over limit? can't append more skbs to msk */ 65713c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) 6582e52213cSFlorian Westphal goto wake; 6592e52213cSFlorian Westphal 66013c7ba0cSFlorian Westphal if (move_skbs_to_msk(msk, ssk)) 661600911ffSFlorian Westphal goto wake; 662600911ffSFlorian Westphal 66314c441b5SPaolo Abeni /* mptcp socket is owned, release_cb should retry */ 66414c441b5SPaolo Abeni if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 66514c441b5SPaolo Abeni &sk->sk_tsq_flags)) { 66614c441b5SPaolo Abeni sock_hold(sk); 6676771bfd9SFlorian Westphal 66814c441b5SPaolo Abeni /* need to try again, its possible release_cb() has already 66914c441b5SPaolo Abeni * been called after the test_and_set_bit() above. 67014c441b5SPaolo Abeni */ 67114c441b5SPaolo Abeni move_skbs_to_msk(msk, ssk); 67214c441b5SPaolo Abeni } 673600911ffSFlorian Westphal wake: 6746719331cSPaolo Abeni if (wake) 675101f6f85SFlorian Westphal sk->sk_data_ready(sk); 676101f6f85SFlorian Westphal } 677101f6f85SFlorian Westphal 678ec3edaa7SPeter Krystad static void __mptcp_flush_join_list(struct mptcp_sock *msk) 679ec3edaa7SPeter Krystad { 680ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 681ec3edaa7SPeter Krystad return; 682ec3edaa7SPeter Krystad 683ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 684ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 685ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 686ec3edaa7SPeter Krystad } 687ec3edaa7SPeter Krystad 688b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 689b51f9b80SPaolo Abeni { 690b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 691b51f9b80SPaolo Abeni } 692b51f9b80SPaolo Abeni 693b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 694b51f9b80SPaolo Abeni { 695b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 696b51f9b80SPaolo Abeni unsigned long tout; 697b51f9b80SPaolo Abeni 698*e16163b6SPaolo Abeni /* prevent rescheduling on close */ 699*e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 700*e16163b6SPaolo Abeni return; 701*e16163b6SPaolo Abeni 702b51f9b80SPaolo Abeni /* should never be called with mptcp level timer cleared */ 703b51f9b80SPaolo Abeni tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 704b51f9b80SPaolo Abeni if (WARN_ON_ONCE(!tout)) 705b51f9b80SPaolo Abeni tout = TCP_RTO_MIN; 706b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 707b51f9b80SPaolo Abeni } 708b51f9b80SPaolo Abeni 709ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 710ba8f48f7SPaolo Abeni { 711ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 712ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 713ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 714ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 715ba8f48f7SPaolo Abeni */ 716ba8f48f7SPaolo Abeni sock_hold(sk); 717ba8f48f7SPaolo Abeni return true; 718ba8f48f7SPaolo Abeni } 719ba8f48f7SPaolo Abeni return false; 720ba8f48f7SPaolo Abeni } 721ba8f48f7SPaolo Abeni 722b51f9b80SPaolo Abeni void mptcp_data_acked(struct sock *sk) 723b51f9b80SPaolo Abeni { 724b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 7253b1d6210SPaolo Abeni 72663561a40SPaolo Abeni if ((!test_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags) || 727ba8f48f7SPaolo Abeni (inet_sk_state_load(sk) != TCP_ESTABLISHED))) 728ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 729b51f9b80SPaolo Abeni } 730b51f9b80SPaolo Abeni 73159832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 73259832e24SFlorian Westphal { 733ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 734ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 73559832e24SFlorian Westphal } 73659832e24SFlorian Westphal 7375969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 7385969856aSPaolo Abeni { 7395969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 7405969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 7415969856aSPaolo Abeni int receivers = 0; 7425969856aSPaolo Abeni 7435969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 7445969856aSPaolo Abeni receivers += !subflow->rx_eof; 745*e16163b6SPaolo Abeni if (receivers) 746*e16163b6SPaolo Abeni return; 7475969856aSPaolo Abeni 748*e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 7495969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 7505969856aSPaolo Abeni * to msk, when all subflows agree on it 7515969856aSPaolo Abeni */ 7525969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 7535969856aSPaolo Abeni 7545969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 7555969856aSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 7565969856aSPaolo Abeni sk->sk_data_ready(sk); 7575969856aSPaolo Abeni } 758*e16163b6SPaolo Abeni 759*e16163b6SPaolo Abeni switch (sk->sk_state) { 760*e16163b6SPaolo Abeni case TCP_ESTABLISHED: 761*e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 762*e16163b6SPaolo Abeni break; 763*e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 764*e16163b6SPaolo Abeni /* fallback sockets skip TCP_CLOSING - TCP will take care */ 765*e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 766*e16163b6SPaolo Abeni break; 767*e16163b6SPaolo Abeni default: 768*e16163b6SPaolo Abeni return; 769*e16163b6SPaolo Abeni } 770*e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 7715969856aSPaolo Abeni } 7725969856aSPaolo Abeni 7736d0060f6SMat Martineau static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 7746d0060f6SMat Martineau { 7754930f483SFlorian Westphal const struct sock *sk = (const struct sock *)msk; 7764930f483SFlorian Westphal 7776d0060f6SMat Martineau if (!msk->cached_ext) 7784930f483SFlorian Westphal msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); 7796d0060f6SMat Martineau 7806d0060f6SMat Martineau return !!msk->cached_ext; 7816d0060f6SMat Martineau } 7826d0060f6SMat Martineau 7837a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 7847a6a6cbcSPaolo Abeni { 7857a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 7867a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 7877a6a6cbcSPaolo Abeni 7887a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 7897a6a6cbcSPaolo Abeni 7907a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 7917a6a6cbcSPaolo Abeni if (subflow->data_avail) 7927a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 7937a6a6cbcSPaolo Abeni } 7947a6a6cbcSPaolo Abeni 7957a6a6cbcSPaolo Abeni return NULL; 7967a6a6cbcSPaolo Abeni } 7977a6a6cbcSPaolo Abeni 7983f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 79957040755SPaolo Abeni const struct sk_buff *skb, 80057040755SPaolo Abeni const struct mptcp_ext *mpext) 8016d0060f6SMat Martineau { 80257040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 80357040755SPaolo Abeni return false; 80457040755SPaolo Abeni 8055a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 8065a369ca6SPaolo Abeni * mapping has not been xmitted yet 8075a369ca6SPaolo Abeni */ 8085a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 8095a369ca6SPaolo Abeni !mpext->frozen; 81057040755SPaolo Abeni } 81157040755SPaolo Abeni 81218b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 81318b683bfSPaolo Abeni const struct page_frag *pfrag, 81418b683bfSPaolo Abeni const struct mptcp_data_frag *df) 81518b683bfSPaolo Abeni { 81618b683bfSPaolo Abeni return df && pfrag->page == df->page && 81718b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 81818b683bfSPaolo Abeni } 81918b683bfSPaolo Abeni 820d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 82118b683bfSPaolo Abeni { 822d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 8237948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 824d027236cSPaolo Abeni } 825d027236cSPaolo Abeni 826d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 827d027236cSPaolo Abeni { 828d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 829d027236cSPaolo Abeni 83018b683bfSPaolo Abeni list_del(&dfrag->list); 831d027236cSPaolo Abeni dfrag_uncharge(sk, len); 83218b683bfSPaolo Abeni put_page(dfrag->page); 83318b683bfSPaolo Abeni } 83418b683bfSPaolo Abeni 83563561a40SPaolo Abeni static bool mptcp_is_writeable(struct mptcp_sock *msk) 83663561a40SPaolo Abeni { 83763561a40SPaolo Abeni struct mptcp_subflow_context *subflow; 83863561a40SPaolo Abeni 83963561a40SPaolo Abeni if (!sk_stream_is_writeable((struct sock *)msk)) 84063561a40SPaolo Abeni return false; 84163561a40SPaolo Abeni 84263561a40SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 84363561a40SPaolo Abeni if (sk_stream_is_writeable(subflow->tcp_sock)) 84463561a40SPaolo Abeni return true; 84563561a40SPaolo Abeni } 84663561a40SPaolo Abeni return false; 84763561a40SPaolo Abeni } 84863561a40SPaolo Abeni 84918b683bfSPaolo Abeni static void mptcp_clean_una(struct sock *sk) 85018b683bfSPaolo Abeni { 85118b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 85218b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 853d027236cSPaolo Abeni bool cleaned = false; 854e1ff9e82SDavide Caratti u64 snd_una; 855e1ff9e82SDavide Caratti 856e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 857e1ff9e82SDavide Caratti * plain TCP 858e1ff9e82SDavide Caratti */ 859e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 860eaa2ffabSPaolo Abeni atomic64_set(&msk->snd_una, msk->snd_nxt); 861e1ff9e82SDavide Caratti snd_una = atomic64_read(&msk->snd_una); 86218b683bfSPaolo Abeni 86318b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 86418b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 86518b683bfSPaolo Abeni break; 86618b683bfSPaolo Abeni 867d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 868d027236cSPaolo Abeni cleaned = true; 869d027236cSPaolo Abeni } 870d027236cSPaolo Abeni 8717948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 8727948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 87353eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 87453eb4c38SPaolo Abeni 87553eb4c38SPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->data_len)) 87653eb4c38SPaolo Abeni goto out; 8777948f6ccSFlorian Westphal 8787948f6ccSFlorian Westphal dfrag->data_seq += delta; 87953eb4c38SPaolo Abeni dfrag->offset += delta; 8807948f6ccSFlorian Westphal dfrag->data_len -= delta; 8817948f6ccSFlorian Westphal 8827948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 8837948f6ccSFlorian Westphal cleaned = true; 8847948f6ccSFlorian Westphal } 8857948f6ccSFlorian Westphal 88653eb4c38SPaolo Abeni out: 88795ed690eSFlorian Westphal if (cleaned) 888d027236cSPaolo Abeni sk_mem_reclaim_partial(sk); 88995ed690eSFlorian Westphal } 89095ed690eSFlorian Westphal 89195ed690eSFlorian Westphal static void mptcp_clean_una_wakeup(struct sock *sk) 89295ed690eSFlorian Westphal { 89395ed690eSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 89495ed690eSFlorian Westphal 89595ed690eSFlorian Westphal mptcp_clean_una(sk); 8967948f6ccSFlorian Westphal 8977948f6ccSFlorian Westphal /* Only wake up writers if a subflow is ready */ 89863561a40SPaolo Abeni if (mptcp_is_writeable(msk)) { 89995ed690eSFlorian Westphal set_bit(MPTCP_SEND_SPACE, &msk->flags); 90063561a40SPaolo Abeni smp_mb__after_atomic(); 90163561a40SPaolo Abeni 90263561a40SPaolo Abeni /* set SEND_SPACE before sk_stream_write_space clears 90363561a40SPaolo Abeni * NOSPACE 90463561a40SPaolo Abeni */ 9057948f6ccSFlorian Westphal sk_stream_write_space(sk); 90618b683bfSPaolo Abeni } 90718b683bfSPaolo Abeni } 90818b683bfSPaolo Abeni 90918b683bfSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 91018b683bfSPaolo Abeni * data 91118b683bfSPaolo Abeni */ 91218b683bfSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 91318b683bfSPaolo Abeni { 91418b683bfSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 91518b683bfSPaolo Abeni pfrag, sk->sk_allocation))) 91618b683bfSPaolo Abeni return true; 91718b683bfSPaolo Abeni 91818b683bfSPaolo Abeni sk->sk_prot->enter_memory_pressure(sk); 91918b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 92018b683bfSPaolo Abeni return false; 92118b683bfSPaolo Abeni } 92218b683bfSPaolo Abeni 92318b683bfSPaolo Abeni static struct mptcp_data_frag * 92418b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 92518b683bfSPaolo Abeni int orig_offset) 92618b683bfSPaolo Abeni { 92718b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 92818b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 92918b683bfSPaolo Abeni 93018b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 93118b683bfSPaolo Abeni dfrag->data_len = 0; 93218b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 93318b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 93418b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 93518b683bfSPaolo Abeni dfrag->page = pfrag->page; 93618b683bfSPaolo Abeni 93718b683bfSPaolo Abeni return dfrag; 93818b683bfSPaolo Abeni } 93918b683bfSPaolo Abeni 940caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 941caf971dfSPaolo Abeni int mss_now; 942caf971dfSPaolo Abeni int size_goal; 943caf971dfSPaolo Abeni }; 944caf971dfSPaolo Abeni 94557040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 9463f8e0aaeSPaolo Abeni struct msghdr *msg, struct mptcp_data_frag *dfrag, 947caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 94857040755SPaolo Abeni { 949caf971dfSPaolo Abeni int avail_size, offset, ret, frag_truesize = 0; 95018b683bfSPaolo Abeni bool dfrag_collapsed, can_collapse = false; 9516d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 9526d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 9533f8e0aaeSPaolo Abeni bool retransmission = !!dfrag; 95457040755SPaolo Abeni struct sk_buff *skb, *tail; 9556d0060f6SMat Martineau struct page_frag *pfrag; 9563f8e0aaeSPaolo Abeni struct page *page; 9573f8e0aaeSPaolo Abeni u64 *write_seq; 9586d0060f6SMat Martineau size_t psize; 9596d0060f6SMat Martineau 9606d0060f6SMat Martineau /* use the mptcp page cache so that we can easily move the data 9616d0060f6SMat Martineau * from one substream to another, but do per subflow memory accounting 9623f8e0aaeSPaolo Abeni * Note: pfrag is used only !retransmission, but the compiler if 9633f8e0aaeSPaolo Abeni * fooled into a warning if we don't init here 9646d0060f6SMat Martineau */ 9656d0060f6SMat Martineau pfrag = sk_page_frag(sk); 9663f8e0aaeSPaolo Abeni if (!retransmission) { 9673f8e0aaeSPaolo Abeni write_seq = &msk->write_seq; 9683f8e0aaeSPaolo Abeni page = pfrag->page; 9693f8e0aaeSPaolo Abeni } else { 9703f8e0aaeSPaolo Abeni write_seq = &dfrag->data_seq; 9713f8e0aaeSPaolo Abeni page = dfrag->page; 9723f8e0aaeSPaolo Abeni } 9736d0060f6SMat Martineau 9746d0060f6SMat Martineau /* compute copy limit */ 975caf971dfSPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, msg->msg_flags); 976caf971dfSPaolo Abeni avail_size = info->size_goal; 97757040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 97857040755SPaolo Abeni if (skb) { 97957040755SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 9806d0060f6SMat Martineau 98157040755SPaolo Abeni /* Limit the write to the size available in the 98257040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 98357040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 98457040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 98557040755SPaolo Abeni * SSN association set here 98657040755SPaolo Abeni */ 987caf971dfSPaolo Abeni can_collapse = (info->size_goal - skb->len > 0) && 9883f8e0aaeSPaolo Abeni mptcp_skb_can_collapse_to(*write_seq, skb, mpext); 98957040755SPaolo Abeni if (!can_collapse) 99057040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 99157040755SPaolo Abeni else 992caf971dfSPaolo Abeni avail_size = info->size_goal - skb->len; 99357040755SPaolo Abeni } 99418b683bfSPaolo Abeni 9953f8e0aaeSPaolo Abeni if (!retransmission) { 9963f8e0aaeSPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 9973f8e0aaeSPaolo Abeni * page allocator 99818b683bfSPaolo Abeni */ 99918b683bfSPaolo Abeni dfrag = mptcp_rtx_tail(sk); 100018b683bfSPaolo Abeni offset = pfrag->offset; 100118b683bfSPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 100218b683bfSPaolo Abeni if (!dfrag_collapsed) { 100318b683bfSPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, offset); 100418b683bfSPaolo Abeni offset = dfrag->offset; 100518b683bfSPaolo Abeni frag_truesize = dfrag->overhead; 100618b683bfSPaolo Abeni } 100718b683bfSPaolo Abeni psize = min_t(size_t, pfrag->size - offset, avail_size); 100857040755SPaolo Abeni 100957040755SPaolo Abeni /* Copy to page */ 10106d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 101118b683bfSPaolo Abeni psize = copy_page_from_iter(pfrag->page, offset, 10123f8e0aaeSPaolo Abeni min_t(size_t, msg_data_left(msg), 10133f8e0aaeSPaolo Abeni psize), 10146d0060f6SMat Martineau &msg->msg_iter); 10156d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 10166d0060f6SMat Martineau if (!psize) 10176d0060f6SMat Martineau return -EINVAL; 10186d0060f6SMat Martineau 101935759383SFlorian Westphal if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) { 102035759383SFlorian Westphal iov_iter_revert(&msg->msg_iter, psize); 1021d027236cSPaolo Abeni return -ENOMEM; 102235759383SFlorian Westphal } 10233f8e0aaeSPaolo Abeni } else { 10243f8e0aaeSPaolo Abeni offset = dfrag->offset; 10253f8e0aaeSPaolo Abeni psize = min_t(size_t, dfrag->data_len, avail_size); 10263f8e0aaeSPaolo Abeni } 1027d027236cSPaolo Abeni 1028e2223995SPaolo Abeni tail = tcp_build_frag(ssk, psize, msg->msg_flags, page, offset, &psize); 1029e2223995SPaolo Abeni if (!tail) { 1030e2223995SPaolo Abeni tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk)); 1031e2223995SPaolo Abeni return -ENOMEM; 103235759383SFlorian Westphal } 103318b683bfSPaolo Abeni 1034e2223995SPaolo Abeni ret = psize; 103518b683bfSPaolo Abeni frag_truesize += ret; 10363f8e0aaeSPaolo Abeni if (!retransmission) { 10376d0060f6SMat Martineau if (unlikely(ret < psize)) 10386d0060f6SMat Martineau iov_iter_revert(&msg->msg_iter, psize - ret); 10396d0060f6SMat Martineau 104018b683bfSPaolo Abeni /* send successful, keep track of sent data for mptcp-level 104118b683bfSPaolo Abeni * retransmission 104218b683bfSPaolo Abeni */ 104318b683bfSPaolo Abeni dfrag->data_len += ret; 104418b683bfSPaolo Abeni if (!dfrag_collapsed) { 104518b683bfSPaolo Abeni get_page(dfrag->page); 104618b683bfSPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 10473f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 10483f8e0aaeSPaolo Abeni } else { 10493f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, ret); 105018b683bfSPaolo Abeni } 105118b683bfSPaolo Abeni 1052d027236cSPaolo Abeni /* charge data on mptcp rtx queue to the master socket 1053d027236cSPaolo Abeni * Note: we charge such data both to sk and ssk 1054d027236cSPaolo Abeni */ 1055d027236cSPaolo Abeni sk->sk_forward_alloc -= frag_truesize; 10563f8e0aaeSPaolo Abeni } 1057d027236cSPaolo Abeni 1058e2223995SPaolo Abeni /* if the tail skb is still the cached one, collapsing really happened. 105957040755SPaolo Abeni */ 1060e2223995SPaolo Abeni if (skb == tail) { 106157040755SPaolo Abeni WARN_ON_ONCE(!can_collapse); 106257040755SPaolo Abeni mpext->data_len += ret; 106357040755SPaolo Abeni goto out; 106457040755SPaolo Abeni } 106557040755SPaolo Abeni 1066e2223995SPaolo Abeni mpext = __skb_ext_set(tail, SKB_EXT_MPTCP, msk->cached_ext); 10676d0060f6SMat Martineau msk->cached_ext = NULL; 10686d0060f6SMat Martineau 10696d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 10703f8e0aaeSPaolo Abeni mpext->data_seq = *write_seq; 10716d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 10726d0060f6SMat Martineau mpext->data_len = ret; 10736d0060f6SMat Martineau mpext->use_map = 1; 10746d0060f6SMat Martineau mpext->dsn64 = 1; 10756d0060f6SMat Martineau 10766d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 10776d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 10786d0060f6SMat Martineau mpext->dsn64); 10796d0060f6SMat Martineau 108057040755SPaolo Abeni out: 10813f8e0aaeSPaolo Abeni if (!retransmission) 108218b683bfSPaolo Abeni pfrag->offset += frag_truesize; 1083721e9089SMat Martineau WRITE_ONCE(*write_seq, *write_seq + ret); 10846d0060f6SMat Martineau mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 10856d0060f6SMat Martineau 10866d0060f6SMat Martineau return ret; 10876d0060f6SMat Martineau } 10886d0060f6SMat Martineau 108963561a40SPaolo Abeni static void mptcp_nospace(struct mptcp_sock *msk) 1090a0e17064SFlorian Westphal { 109163561a40SPaolo Abeni struct mptcp_subflow_context *subflow; 109263561a40SPaolo Abeni 1093a0e17064SFlorian Westphal clear_bit(MPTCP_SEND_SPACE, &msk->flags); 1094a0e17064SFlorian Westphal smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 1095a0e17064SFlorian Westphal 109663561a40SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 109763561a40SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 109863561a40SPaolo Abeni struct socket *sock = READ_ONCE(ssk->sk_socket); 109963561a40SPaolo Abeni 110063561a40SPaolo Abeni /* enables ssk->write_space() callbacks */ 110163561a40SPaolo Abeni if (sock) 1102a0e17064SFlorian Westphal set_bit(SOCK_NOSPACE, &sock->flags); 1103a0e17064SFlorian Westphal } 110463561a40SPaolo Abeni } 1105a0e17064SFlorian Westphal 1106d5f49190SPaolo Abeni static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1107d5f49190SPaolo Abeni { 1108d5f49190SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1109d5f49190SPaolo Abeni 1110d5f49190SPaolo Abeni /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */ 1111d5f49190SPaolo Abeni if (subflow->request_join && !subflow->fully_established) 1112d5f49190SPaolo Abeni return false; 1113d5f49190SPaolo Abeni 1114d5f49190SPaolo Abeni /* only send if our side has not closed yet */ 1115d5f49190SPaolo Abeni return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)); 1116d5f49190SPaolo Abeni } 1117d5f49190SPaolo Abeni 1118d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1119d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1120d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1121d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1122d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1123d5f49190SPaolo Abeni 1124d5f49190SPaolo Abeni struct subflow_send_info { 1125d5f49190SPaolo Abeni struct sock *ssk; 1126d5f49190SPaolo Abeni u64 ratio; 1127d5f49190SPaolo Abeni }; 1128d5f49190SPaolo Abeni 1129da51aef5SPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk, 1130da51aef5SPaolo Abeni u32 *sndbuf) 1131f296234cSPeter Krystad { 1132d5f49190SPaolo Abeni struct subflow_send_info send_info[2]; 1133f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 1134d5f49190SPaolo Abeni int i, nr_active = 0; 1135d5f49190SPaolo Abeni struct sock *ssk; 1136d5f49190SPaolo Abeni u64 ratio; 1137d5f49190SPaolo Abeni u32 pace; 1138f296234cSPeter Krystad 1139d5f49190SPaolo Abeni sock_owned_by_me((struct sock *)msk); 1140f296234cSPeter Krystad 1141da51aef5SPaolo Abeni *sndbuf = 0; 1142149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1143149f7c71SFlorian Westphal return NULL; 1144149f7c71SFlorian Westphal 1145d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1146d5f49190SPaolo Abeni if (!msk->first) 1147f296234cSPeter Krystad return NULL; 1148d5f49190SPaolo Abeni *sndbuf = msk->first->sk_sndbuf; 1149d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1150f296234cSPeter Krystad } 1151f296234cSPeter Krystad 1152d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1153d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1154d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 1155d5f49190SPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 1156d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1157d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1158da51aef5SPaolo Abeni *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf); 1159d5f49190SPaolo Abeni } 1160d5f49190SPaolo Abeni return msk->last_snd; 1161d5f49190SPaolo Abeni } 1162f296234cSPeter Krystad 1163d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 1164d5f49190SPaolo Abeni for (i = 0; i < 2; ++i) { 1165d5f49190SPaolo Abeni send_info[i].ssk = NULL; 1166d5f49190SPaolo Abeni send_info[i].ratio = -1; 1167d5f49190SPaolo Abeni } 1168d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1169d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1170d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1171f296234cSPeter Krystad continue; 1172d5f49190SPaolo Abeni 1173d5f49190SPaolo Abeni nr_active += !subflow->backup; 1174d5f49190SPaolo Abeni *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf); 1175d5f49190SPaolo Abeni if (!sk_stream_memory_free(subflow->tcp_sock)) 1176d5f49190SPaolo Abeni continue; 1177d5f49190SPaolo Abeni 1178d5f49190SPaolo Abeni pace = READ_ONCE(ssk->sk_pacing_rate); 1179d5f49190SPaolo Abeni if (!pace) 1180d5f49190SPaolo Abeni continue; 1181d5f49190SPaolo Abeni 1182d5f49190SPaolo Abeni ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1183d5f49190SPaolo Abeni pace); 1184d5f49190SPaolo Abeni if (ratio < send_info[subflow->backup].ratio) { 1185d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 1186d5f49190SPaolo Abeni send_info[subflow->backup].ratio = ratio; 1187d5f49190SPaolo Abeni } 1188f296234cSPeter Krystad } 1189f296234cSPeter Krystad 1190d5f49190SPaolo Abeni pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld", 1191d5f49190SPaolo Abeni msk, nr_active, send_info[0].ssk, send_info[0].ratio, 1192d5f49190SPaolo Abeni send_info[1].ssk, send_info[1].ratio); 1193f296234cSPeter Krystad 1194d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1195d5f49190SPaolo Abeni if (!nr_active) 1196d5f49190SPaolo Abeni send_info[0].ssk = send_info[1].ssk; 1197d5f49190SPaolo Abeni 1198d5f49190SPaolo Abeni if (send_info[0].ssk) { 1199d5f49190SPaolo Abeni msk->last_snd = send_info[0].ssk; 1200d5f49190SPaolo Abeni msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1201d5f49190SPaolo Abeni sk_stream_wspace(msk->last_snd)); 1202d5f49190SPaolo Abeni return msk->last_snd; 1203d5f49190SPaolo Abeni } 1204d5f49190SPaolo Abeni return NULL; 1205f296234cSPeter Krystad } 1206f296234cSPeter Krystad 120763561a40SPaolo Abeni static void ssk_check_wmem(struct mptcp_sock *msk) 12081891c4a0SFlorian Westphal { 120963561a40SPaolo Abeni if (unlikely(!mptcp_is_writeable(msk))) 121063561a40SPaolo Abeni mptcp_nospace(msk); 12111891c4a0SFlorian Westphal } 12121891c4a0SFlorian Westphal 1213f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1214f870fa0bSMat Martineau { 1215f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1216caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = { 1217caf971dfSPaolo Abeni .mss_now = 0, 1218caf971dfSPaolo Abeni .size_goal = 0, 1219caf971dfSPaolo Abeni }; 122017091708SFlorian Westphal struct page_frag *pfrag; 12216d0060f6SMat Martineau size_t copied = 0; 1222cec37a6eSPeter Krystad struct sock *ssk; 1223caf971dfSPaolo Abeni int ret = 0; 1224da51aef5SPaolo Abeni u32 sndbuf; 122572511aabSFlorian Westphal bool tx_ok; 12266d0060f6SMat Martineau long timeo; 1227f870fa0bSMat Martineau 1228f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 1229f870fa0bSMat Martineau return -EOPNOTSUPP; 1230f870fa0bSMat Martineau 1231cec37a6eSPeter Krystad lock_sock(sk); 12321954b860SMat Martineau 12331954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 12341954b860SMat Martineau 12351954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 12361954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 12371954b860SMat Martineau if (ret) 12381954b860SMat Martineau goto out; 12391954b860SMat Martineau } 12401954b860SMat Martineau 124117091708SFlorian Westphal pfrag = sk_page_frag(sk); 124272511aabSFlorian Westphal restart: 124318b683bfSPaolo Abeni mptcp_clean_una(sk); 124418b683bfSPaolo Abeni 124557baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 124657baaf28SMat Martineau ret = -EPIPE; 124757baaf28SMat Martineau goto out; 124857baaf28SMat Martineau } 124957baaf28SMat Martineau 1250ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 1251da51aef5SPaolo Abeni ssk = mptcp_subflow_get_send(msk, &sndbuf); 125217091708SFlorian Westphal while (!sk_stream_memory_free(sk) || 125317091708SFlorian Westphal !ssk || 125417091708SFlorian Westphal !mptcp_page_frag_refill(ssk, pfrag)) { 1255fb529e62SFlorian Westphal if (ssk) { 1256fb529e62SFlorian Westphal /* make sure retransmit timer is 1257fb529e62SFlorian Westphal * running before we wait for memory. 1258fb529e62SFlorian Westphal * 1259fb529e62SFlorian Westphal * The retransmit timer might be needed 1260fb529e62SFlorian Westphal * to make the peer send an up-to-date 1261fb529e62SFlorian Westphal * MPTCP Ack. 1262fb529e62SFlorian Westphal */ 1263fb529e62SFlorian Westphal mptcp_set_timeout(sk, ssk); 1264fb529e62SFlorian Westphal if (!mptcp_timer_pending(sk)) 1265fb529e62SFlorian Westphal mptcp_reset_timer(sk); 1266fb529e62SFlorian Westphal } 1267fb529e62SFlorian Westphal 126863561a40SPaolo Abeni mptcp_nospace(msk); 1269f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1270f296234cSPeter Krystad if (ret) 1271f296234cSPeter Krystad goto out; 1272f296234cSPeter Krystad 127318b683bfSPaolo Abeni mptcp_clean_una(sk); 127418b683bfSPaolo Abeni 1275da51aef5SPaolo Abeni ssk = mptcp_subflow_get_send(msk, &sndbuf); 1276f296234cSPeter Krystad if (list_empty(&msk->conn_list)) { 1277f296234cSPeter Krystad ret = -ENOTCONN; 1278f296234cSPeter Krystad goto out; 1279f296234cSPeter Krystad } 1280cec37a6eSPeter Krystad } 1281cec37a6eSPeter Krystad 1282da51aef5SPaolo Abeni /* do auto tuning */ 1283da51aef5SPaolo Abeni if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) && 1284da51aef5SPaolo Abeni sndbuf > READ_ONCE(sk->sk_sndbuf)) 1285da51aef5SPaolo Abeni WRITE_ONCE(sk->sk_sndbuf, sndbuf); 1286da51aef5SPaolo Abeni 12876d0060f6SMat Martineau pr_debug("conn_list->subflow=%p", ssk); 1288cec37a6eSPeter Krystad 12896d0060f6SMat Martineau lock_sock(ssk); 129072511aabSFlorian Westphal tx_ok = msg_data_left(msg); 129172511aabSFlorian Westphal while (tx_ok) { 1292caf971dfSPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &info); 129372511aabSFlorian Westphal if (ret < 0) { 129472511aabSFlorian Westphal if (ret == -EAGAIN && timeo > 0) { 129572511aabSFlorian Westphal mptcp_set_timeout(sk, ssk); 129672511aabSFlorian Westphal release_sock(ssk); 129772511aabSFlorian Westphal goto restart; 129872511aabSFlorian Westphal } 12996d0060f6SMat Martineau break; 130072511aabSFlorian Westphal } 13016d0060f6SMat Martineau 1302d5f49190SPaolo Abeni /* burst can be negative, we will try move to the next subflow 1303d5f49190SPaolo Abeni * at selection time, if possible. 1304d5f49190SPaolo Abeni */ 1305d5f49190SPaolo Abeni msk->snd_burst -= ret; 13066d0060f6SMat Martineau copied += ret; 1307fb529e62SFlorian Westphal 130872511aabSFlorian Westphal tx_ok = msg_data_left(msg); 130972511aabSFlorian Westphal if (!tx_ok) 131072511aabSFlorian Westphal break; 131172511aabSFlorian Westphal 1312149f7c71SFlorian Westphal if (!sk_stream_memory_free(ssk) || 131317091708SFlorian Westphal !mptcp_page_frag_refill(ssk, pfrag) || 1314149f7c71SFlorian Westphal !mptcp_ext_cache_refill(msk)) { 1315caf971dfSPaolo Abeni tcp_push(ssk, msg->msg_flags, info.mss_now, 1316caf971dfSPaolo Abeni tcp_sk(ssk)->nonagle, info.size_goal); 131772511aabSFlorian Westphal mptcp_set_timeout(sk, ssk); 131872511aabSFlorian Westphal release_sock(ssk); 131972511aabSFlorian Westphal goto restart; 132072511aabSFlorian Westphal } 132172511aabSFlorian Westphal 1322fb529e62SFlorian Westphal /* memory is charged to mptcp level socket as well, i.e. 1323fb529e62SFlorian Westphal * if msg is very large, mptcp socket may run out of buffer 1324fb529e62SFlorian Westphal * space. mptcp_clean_una() will release data that has 1325fb529e62SFlorian Westphal * been acked at mptcp level in the mean time, so there is 1326fb529e62SFlorian Westphal * a good chance we can continue sending data right away. 132772511aabSFlorian Westphal * 132872511aabSFlorian Westphal * Normally, when the tcp subflow can accept more data, then 132972511aabSFlorian Westphal * so can the MPTCP socket. However, we need to cope with 133072511aabSFlorian Westphal * peers that might lag behind in their MPTCP-level 133172511aabSFlorian Westphal * acknowledgements, i.e. data might have been acked at 133272511aabSFlorian Westphal * tcp level only. So, we must also check the MPTCP socket 133372511aabSFlorian Westphal * limits before we send more data. 1334fb529e62SFlorian Westphal */ 1335fb529e62SFlorian Westphal if (unlikely(!sk_stream_memory_free(sk))) { 1336caf971dfSPaolo Abeni tcp_push(ssk, msg->msg_flags, info.mss_now, 1337caf971dfSPaolo Abeni tcp_sk(ssk)->nonagle, info.size_goal); 1338fb529e62SFlorian Westphal mptcp_clean_una(sk); 1339fb529e62SFlorian Westphal if (!sk_stream_memory_free(sk)) { 1340fb529e62SFlorian Westphal /* can't send more for now, need to wait for 1341fb529e62SFlorian Westphal * MPTCP-level ACKs from peer. 1342fb529e62SFlorian Westphal * 1343fb529e62SFlorian Westphal * Wakeup will happen via mptcp_clean_una(). 1344fb529e62SFlorian Westphal */ 1345fb529e62SFlorian Westphal mptcp_set_timeout(sk, ssk); 1346fb529e62SFlorian Westphal release_sock(ssk); 13471cec170dSFlorian Westphal goto restart; 1348fb529e62SFlorian Westphal } 1349fb529e62SFlorian Westphal } 13506d0060f6SMat Martineau } 13516d0060f6SMat Martineau 1352b51f9b80SPaolo Abeni mptcp_set_timeout(sk, ssk); 135357040755SPaolo Abeni if (copied) { 1354caf971dfSPaolo Abeni tcp_push(ssk, msg->msg_flags, info.mss_now, 1355caf971dfSPaolo Abeni tcp_sk(ssk)->nonagle, info.size_goal); 1356b51f9b80SPaolo Abeni 1357b51f9b80SPaolo Abeni /* start the timer, if it's not pending */ 1358b51f9b80SPaolo Abeni if (!mptcp_timer_pending(sk)) 1359b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 136057040755SPaolo Abeni } 13616d0060f6SMat Martineau 13626d0060f6SMat Martineau release_sock(ssk); 13631954b860SMat Martineau out: 1364eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 136563561a40SPaolo Abeni ssk_check_wmem(msk); 1366cec37a6eSPeter Krystad release_sock(sk); 13678555c6bfSPaolo Abeni return copied ? : ret; 1368f870fa0bSMat Martineau } 1369f870fa0bSMat Martineau 13707a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 13717a6a6cbcSPaolo Abeni { 13727a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 13737a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 13747a6a6cbcSPaolo Abeni 13757a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 13767a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 13777a6a6cbcSPaolo Abeni 13787a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 13797a6a6cbcSPaolo Abeni test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 13807a6a6cbcSPaolo Abeni 13817a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 13827a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 13837a6a6cbcSPaolo Abeni } 13847a6a6cbcSPaolo Abeni 13856771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 13866771bfd9SFlorian Westphal struct msghdr *msg, 13876771bfd9SFlorian Westphal size_t len) 13886771bfd9SFlorian Westphal { 13896771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 13906771bfd9SFlorian Westphal struct sk_buff *skb; 13916771bfd9SFlorian Westphal int copied = 0; 13926771bfd9SFlorian Westphal 13936771bfd9SFlorian Westphal while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 13946771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 13956771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 13966771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 13976771bfd9SFlorian Westphal int err; 13986771bfd9SFlorian Westphal 13996771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 14006771bfd9SFlorian Westphal if (unlikely(err < 0)) { 14016771bfd9SFlorian Westphal if (!copied) 14026771bfd9SFlorian Westphal return err; 14036771bfd9SFlorian Westphal break; 14046771bfd9SFlorian Westphal } 14056771bfd9SFlorian Westphal 14066771bfd9SFlorian Westphal copied += count; 14076771bfd9SFlorian Westphal 14086771bfd9SFlorian Westphal if (count < data_len) { 14096771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 14106771bfd9SFlorian Westphal break; 14116771bfd9SFlorian Westphal } 14126771bfd9SFlorian Westphal 14136771bfd9SFlorian Westphal __skb_unlink(skb, &sk->sk_receive_queue); 14146771bfd9SFlorian Westphal __kfree_skb(skb); 14156771bfd9SFlorian Westphal 14166771bfd9SFlorian Westphal if (copied >= len) 14176771bfd9SFlorian Westphal break; 14186771bfd9SFlorian Westphal } 14196771bfd9SFlorian Westphal 14206771bfd9SFlorian Westphal return copied; 14216771bfd9SFlorian Westphal } 14226771bfd9SFlorian Westphal 1423a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1424a6b118feSFlorian Westphal * 1425a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1426a6b118feSFlorian Westphal */ 1427a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1428a6b118feSFlorian Westphal { 1429a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1430a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1431a6b118feSFlorian Westphal u32 time, advmss = 1; 1432a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1433a6b118feSFlorian Westphal 1434a6b118feSFlorian Westphal sock_owned_by_me(sk); 1435a6b118feSFlorian Westphal 1436a6b118feSFlorian Westphal if (copied <= 0) 1437a6b118feSFlorian Westphal return; 1438a6b118feSFlorian Westphal 1439a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1440a6b118feSFlorian Westphal 1441a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1442a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1443a6b118feSFlorian Westphal 1444a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1445a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1446a6b118feSFlorian Westphal return; 1447a6b118feSFlorian Westphal 1448a6b118feSFlorian Westphal rtt_us = 0; 1449a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1450a6b118feSFlorian Westphal const struct tcp_sock *tp; 1451a6b118feSFlorian Westphal u64 sf_rtt_us; 1452a6b118feSFlorian Westphal u32 sf_advmss; 1453a6b118feSFlorian Westphal 1454a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1455a6b118feSFlorian Westphal 1456a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1457a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1458a6b118feSFlorian Westphal 1459a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1460a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1461a6b118feSFlorian Westphal } 1462a6b118feSFlorian Westphal 1463a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1464a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1465a6b118feSFlorian Westphal return; 1466a6b118feSFlorian Westphal 1467a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1468a6b118feSFlorian Westphal goto new_measure; 1469a6b118feSFlorian Westphal 1470a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1471a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1472a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1473a6b118feSFlorian Westphal u64 rcvwin, grow; 1474a6b118feSFlorian Westphal 1475a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1476a6b118feSFlorian Westphal 1477a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1478a6b118feSFlorian Westphal 1479a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1480a6b118feSFlorian Westphal rcvwin += (grow << 1); 1481a6b118feSFlorian Westphal 1482a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1483a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1484a6b118feSFlorian Westphal rcvmem += 128; 1485a6b118feSFlorian Westphal 1486a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1487a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1488a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1489a6b118feSFlorian Westphal 1490a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1491a6b118feSFlorian Westphal u32 window_clamp; 1492a6b118feSFlorian Westphal 1493a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1494a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1495a6b118feSFlorian Westphal 1496a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1497a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1498a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1499a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1500a6b118feSFlorian Westphal */ 1501a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1502a6b118feSFlorian Westphal struct sock *ssk; 1503c76c6956SPaolo Abeni bool slow; 1504a6b118feSFlorian Westphal 1505a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1506c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1507a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1508a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1509c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1510c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1511a6b118feSFlorian Westphal } 1512a6b118feSFlorian Westphal } 1513a6b118feSFlorian Westphal } 1514a6b118feSFlorian Westphal 1515a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1516a6b118feSFlorian Westphal new_measure: 1517a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1518a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1519a6b118feSFlorian Westphal } 1520a6b118feSFlorian Westphal 15216771bfd9SFlorian Westphal static bool __mptcp_move_skbs(struct mptcp_sock *msk) 15226771bfd9SFlorian Westphal { 15236771bfd9SFlorian Westphal unsigned int moved = 0; 15246771bfd9SFlorian Westphal bool done; 15256771bfd9SFlorian Westphal 1526d5f49190SPaolo Abeni /* avoid looping forever below on racing close */ 1527d5f49190SPaolo Abeni if (((struct sock *)msk)->sk_state == TCP_CLOSE) 1528d5f49190SPaolo Abeni return false; 1529d5f49190SPaolo Abeni 1530d5f49190SPaolo Abeni __mptcp_flush_join_list(msk); 15316771bfd9SFlorian Westphal do { 15326771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 153365f49fe7SFlorian Westphal bool slowpath; 15346771bfd9SFlorian Westphal 15356771bfd9SFlorian Westphal if (!ssk) 15366771bfd9SFlorian Westphal break; 15376771bfd9SFlorian Westphal 153865f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 15396771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 154065f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 15416771bfd9SFlorian Westphal } while (!done); 15426771bfd9SFlorian Westphal 1543ab174ad8SPaolo Abeni if (mptcp_ofo_queue(msk) || moved > 0) { 1544ab174ad8SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 1545ab174ad8SPaolo Abeni return true; 1546ab174ad8SPaolo Abeni } 1547ab174ad8SPaolo Abeni return false; 15486771bfd9SFlorian Westphal } 15496771bfd9SFlorian Westphal 1550f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 1551f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 1552f870fa0bSMat Martineau { 1553f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1554cec37a6eSPeter Krystad int copied = 0; 15557a6a6cbcSPaolo Abeni int target; 15567a6a6cbcSPaolo Abeni long timeo; 1557f870fa0bSMat Martineau 1558f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 1559f870fa0bSMat Martineau return -EOPNOTSUPP; 1560f870fa0bSMat Martineau 1561cec37a6eSPeter Krystad lock_sock(sk); 15627a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 15637a6a6cbcSPaolo Abeni 15647a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 15657a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 1566ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 15677a6a6cbcSPaolo Abeni 15686771bfd9SFlorian Westphal while (len > (size_t)copied) { 15697a6a6cbcSPaolo Abeni int bytes_read; 15707a6a6cbcSPaolo Abeni 15716771bfd9SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 15726771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 15737a6a6cbcSPaolo Abeni if (!copied) 15747a6a6cbcSPaolo Abeni copied = bytes_read; 15756771bfd9SFlorian Westphal goto out_err; 1576cec37a6eSPeter Krystad } 1577cec37a6eSPeter Krystad 15787a6a6cbcSPaolo Abeni copied += bytes_read; 15797a6a6cbcSPaolo Abeni 15806771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue) && 15816771bfd9SFlorian Westphal __mptcp_move_skbs(msk)) 15826771bfd9SFlorian Westphal continue; 15837a6a6cbcSPaolo Abeni 15847a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 15857a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 15867a6a6cbcSPaolo Abeni */ 15877a6a6cbcSPaolo Abeni if (copied >= target) 15887a6a6cbcSPaolo Abeni break; 15897a6a6cbcSPaolo Abeni 15907a6a6cbcSPaolo Abeni if (copied) { 15917a6a6cbcSPaolo Abeni if (sk->sk_err || 15927a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 15937a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 15947a6a6cbcSPaolo Abeni !timeo || 15957a6a6cbcSPaolo Abeni signal_pending(current)) 15967a6a6cbcSPaolo Abeni break; 15977a6a6cbcSPaolo Abeni } else { 15987a6a6cbcSPaolo Abeni if (sk->sk_err) { 15997a6a6cbcSPaolo Abeni copied = sock_error(sk); 16007a6a6cbcSPaolo Abeni break; 16017a6a6cbcSPaolo Abeni } 16027a6a6cbcSPaolo Abeni 16035969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 16045969856aSPaolo Abeni mptcp_check_for_eof(msk); 16055969856aSPaolo Abeni 16067a6a6cbcSPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) 16077a6a6cbcSPaolo Abeni break; 16087a6a6cbcSPaolo Abeni 16097a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 16107a6a6cbcSPaolo Abeni copied = -ENOTCONN; 16117a6a6cbcSPaolo Abeni break; 16127a6a6cbcSPaolo Abeni } 16137a6a6cbcSPaolo Abeni 16147a6a6cbcSPaolo Abeni if (!timeo) { 16157a6a6cbcSPaolo Abeni copied = -EAGAIN; 16167a6a6cbcSPaolo Abeni break; 16177a6a6cbcSPaolo Abeni } 16187a6a6cbcSPaolo Abeni 16197a6a6cbcSPaolo Abeni if (signal_pending(current)) { 16207a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 16217a6a6cbcSPaolo Abeni break; 16227a6a6cbcSPaolo Abeni } 16237a6a6cbcSPaolo Abeni } 16247a6a6cbcSPaolo Abeni 16257a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 16267a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 16277a6a6cbcSPaolo Abeni } 16287a6a6cbcSPaolo Abeni 16296771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue)) { 16306771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 16317a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 16327a6a6cbcSPaolo Abeni 16336771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 16346771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 16357a6a6cbcSPaolo Abeni */ 16366771bfd9SFlorian Westphal if (unlikely(__mptcp_move_skbs(msk))) 16376771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 16386771bfd9SFlorian Westphal } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 16396771bfd9SFlorian Westphal /* data to read but mptcp_wait_data() cleared DATA_READY */ 16407a6a6cbcSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 16417a6a6cbcSPaolo Abeni } 16426771bfd9SFlorian Westphal out_err: 16436719331cSPaolo Abeni pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d", 16446719331cSPaolo Abeni msk, test_bit(MPTCP_DATA_READY, &msk->flags), 16456719331cSPaolo Abeni skb_queue_empty(&sk->sk_receive_queue), copied); 1646a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 1647a6b118feSFlorian Westphal 1648cec37a6eSPeter Krystad release_sock(sk); 1649cec37a6eSPeter Krystad return copied; 1650cec37a6eSPeter Krystad } 1651cec37a6eSPeter Krystad 1652b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk) 1653b51f9b80SPaolo Abeni { 1654b51f9b80SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1655b51f9b80SPaolo Abeni 1656eaa2ffabSPaolo Abeni if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->snd_nxt)) { 1657b51f9b80SPaolo Abeni mptcp_stop_timer(sk); 16583b1d6210SPaolo Abeni } else { 16593b1d6210SPaolo Abeni set_bit(MPTCP_WORK_RTX, &msk->flags); 1660ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 16613b1d6210SPaolo Abeni } 1662b51f9b80SPaolo Abeni } 1663b51f9b80SPaolo Abeni 1664b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 1665b51f9b80SPaolo Abeni { 1666b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 1667b51f9b80SPaolo Abeni icsk_retransmit_timer); 1668b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 1669b51f9b80SPaolo Abeni 1670b51f9b80SPaolo Abeni bh_lock_sock(sk); 1671b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 1672b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1673b51f9b80SPaolo Abeni } else { 1674b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 1675b51f9b80SPaolo Abeni if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1676b51f9b80SPaolo Abeni &sk->sk_tsq_flags)) 1677b51f9b80SPaolo Abeni sock_hold(sk); 1678b51f9b80SPaolo Abeni } 1679b51f9b80SPaolo Abeni bh_unlock_sock(sk); 1680b51f9b80SPaolo Abeni sock_put(sk); 1681b51f9b80SPaolo Abeni } 1682b51f9b80SPaolo Abeni 1683*e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 1684*e16163b6SPaolo Abeni { 1685*e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 1686*e16163b6SPaolo Abeni 1687*e16163b6SPaolo Abeni mptcp_schedule_work(sk); 1688*e16163b6SPaolo Abeni } 1689*e16163b6SPaolo Abeni 16903b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 16913b1d6210SPaolo Abeni * level. 16923b1d6210SPaolo Abeni * 16933b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 16943b1d6210SPaolo Abeni */ 16953b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 16963b1d6210SPaolo Abeni { 16973b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 16983b1d6210SPaolo Abeni struct sock *backup = NULL; 16993b1d6210SPaolo Abeni 17003b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 17013b1d6210SPaolo Abeni 1702d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 1703d5f49190SPaolo Abeni return msk->first; 1704d5f49190SPaolo Abeni 17053b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 17063b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 17073b1d6210SPaolo Abeni 1708d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1709d5f49190SPaolo Abeni continue; 1710d5f49190SPaolo Abeni 17113b1d6210SPaolo Abeni /* still data outstanding at TCP level? Don't retransmit. */ 17123b1d6210SPaolo Abeni if (!tcp_write_queue_empty(ssk)) 17133b1d6210SPaolo Abeni return NULL; 17143b1d6210SPaolo Abeni 17153b1d6210SPaolo Abeni if (subflow->backup) { 17163b1d6210SPaolo Abeni if (!backup) 17173b1d6210SPaolo Abeni backup = ssk; 17183b1d6210SPaolo Abeni continue; 17193b1d6210SPaolo Abeni } 17203b1d6210SPaolo Abeni 17213b1d6210SPaolo Abeni return ssk; 17223b1d6210SPaolo Abeni } 17233b1d6210SPaolo Abeni 17243b1d6210SPaolo Abeni return backup; 17253b1d6210SPaolo Abeni } 17263b1d6210SPaolo Abeni 1727cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 1728cec37a6eSPeter Krystad * (accept). 1729cec37a6eSPeter Krystad * 1730cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 1731cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 1732cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 1733cec37a6eSPeter Krystad * parent socket. 1734cec37a6eSPeter Krystad */ 1735d0876b22SGeliang Tang void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1736*e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow) 1737cec37a6eSPeter Krystad { 1738*e16163b6SPaolo Abeni bool dispose_socket = false; 1739*e16163b6SPaolo Abeni struct socket *sock; 1740cec37a6eSPeter Krystad 1741cec37a6eSPeter Krystad list_del(&subflow->node); 1742cec37a6eSPeter Krystad 1743*e16163b6SPaolo Abeni lock_sock(ssk); 1744*e16163b6SPaolo Abeni 1745*e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 1746*e16163b6SPaolo Abeni * already orphaned 1747*e16163b6SPaolo Abeni */ 1748*e16163b6SPaolo Abeni sock = ssk->sk_socket; 1749*e16163b6SPaolo Abeni if (sock) { 1750*e16163b6SPaolo Abeni dispose_socket = sock != sk->sk_socket; 1751*e16163b6SPaolo Abeni sock_orphan(ssk); 1752cec37a6eSPeter Krystad } 1753*e16163b6SPaolo Abeni 1754*e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 1755*e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 1756*e16163b6SPaolo Abeni * reference owned by msk; 1757*e16163b6SPaolo Abeni */ 1758*e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 1759*e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 1760*e16163b6SPaolo Abeni } else { 1761*e16163b6SPaolo Abeni /* otherwise ask tcp do dispose of ssk and subflow ctx */ 1762*e16163b6SPaolo Abeni subflow->disposable = 1; 1763*e16163b6SPaolo Abeni __tcp_close(ssk, 0); 1764*e16163b6SPaolo Abeni 1765*e16163b6SPaolo Abeni /* close acquired an extra ref */ 1766*e16163b6SPaolo Abeni __sock_put(ssk); 1767*e16163b6SPaolo Abeni } 1768*e16163b6SPaolo Abeni release_sock(ssk); 1769*e16163b6SPaolo Abeni if (dispose_socket) 1770*e16163b6SPaolo Abeni iput(SOCK_INODE(sock)); 1771*e16163b6SPaolo Abeni 1772*e16163b6SPaolo Abeni sock_put(ssk); 1773f870fa0bSMat Martineau } 1774f870fa0bSMat Martineau 1775dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1776dc24f8b4SPaolo Abeni { 1777dc24f8b4SPaolo Abeni return 0; 1778dc24f8b4SPaolo Abeni } 1779dc24f8b4SPaolo Abeni 1780b416268bSFlorian Westphal static void pm_work(struct mptcp_sock *msk) 1781b416268bSFlorian Westphal { 1782b416268bSFlorian Westphal struct mptcp_pm_data *pm = &msk->pm; 1783b416268bSFlorian Westphal 1784b416268bSFlorian Westphal spin_lock_bh(&msk->pm.lock); 1785b416268bSFlorian Westphal 1786b416268bSFlorian Westphal pr_debug("msk=%p status=%x", msk, pm->status); 1787b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 1788b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 1789b416268bSFlorian Westphal mptcp_pm_nl_add_addr_received(msk); 1790b416268bSFlorian Westphal } 1791d0876b22SGeliang Tang if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 1792d0876b22SGeliang Tang pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 1793d0876b22SGeliang Tang mptcp_pm_nl_rm_addr_received(msk); 1794d0876b22SGeliang Tang } 1795b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 1796b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 1797b416268bSFlorian Westphal mptcp_pm_nl_fully_established(msk); 1798b416268bSFlorian Westphal } 1799b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 1800b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 1801b416268bSFlorian Westphal mptcp_pm_nl_subflow_established(msk); 1802b416268bSFlorian Westphal } 1803b416268bSFlorian Westphal 1804b416268bSFlorian Westphal spin_unlock_bh(&msk->pm.lock); 1805b416268bSFlorian Westphal } 1806b416268bSFlorian Westphal 18070e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 18080e4f35d7SPaolo Abeni { 18090e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 18100e4f35d7SPaolo Abeni 18110e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 18120e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 18130e4f35d7SPaolo Abeni 18140e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 18150e4f35d7SPaolo Abeni continue; 18160e4f35d7SPaolo Abeni 1817*e16163b6SPaolo Abeni __mptcp_close_ssk((struct sock *)msk, ssk, subflow); 18180e4f35d7SPaolo Abeni } 18190e4f35d7SPaolo Abeni } 18200e4f35d7SPaolo Abeni 1821*e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 1822*e16163b6SPaolo Abeni { 1823*e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 1824*e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 1825*e16163b6SPaolo Abeni 1826*e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 1827*e16163b6SPaolo Abeni return true; 1828*e16163b6SPaolo Abeni 1829*e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 1830*e16163b6SPaolo Abeni * timeout 1831*e16163b6SPaolo Abeni */ 1832*e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 1833*e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 1834*e16163b6SPaolo Abeni TCP_CLOSE) 1835*e16163b6SPaolo Abeni return false; 1836*e16163b6SPaolo Abeni } 1837*e16163b6SPaolo Abeni return true; 1838*e16163b6SPaolo Abeni } 1839*e16163b6SPaolo Abeni 184080992017SPaolo Abeni static void mptcp_worker(struct work_struct *work) 184180992017SPaolo Abeni { 184280992017SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 18433b1d6210SPaolo Abeni struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 1844caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 18453b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 1846caf971dfSPaolo Abeni int orig_len, orig_offset; 18473b1d6210SPaolo Abeni u64 orig_write_seq; 18483b1d6210SPaolo Abeni size_t copied = 0; 1849b3b2854dSFlorian Westphal struct msghdr msg = { 1850b3b2854dSFlorian Westphal .msg_flags = MSG_DONTWAIT, 1851b3b2854dSFlorian Westphal }; 1852*e16163b6SPaolo Abeni int state, ret; 185380992017SPaolo Abeni 185480992017SPaolo Abeni lock_sock(sk); 1855*e16163b6SPaolo Abeni set_bit(MPTCP_WORKER_RUNNING, &msk->flags); 1856*e16163b6SPaolo Abeni state = sk->sk_state; 1857*e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 1858*e16163b6SPaolo Abeni goto unlock; 1859*e16163b6SPaolo Abeni 186095ed690eSFlorian Westphal mptcp_clean_una_wakeup(sk); 186143b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 1862ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 18630e4f35d7SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 18640e4f35d7SPaolo Abeni __mptcp_close_subflow(msk); 18650e4f35d7SPaolo Abeni 18666771bfd9SFlorian Westphal __mptcp_move_skbs(msk); 18673b1d6210SPaolo Abeni 1868b416268bSFlorian Westphal if (msk->pm.status) 1869b416268bSFlorian Westphal pm_work(msk); 1870b416268bSFlorian Westphal 187159832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 187259832e24SFlorian Westphal mptcp_check_for_eof(msk); 187359832e24SFlorian Westphal 187443b54c6eSMat Martineau mptcp_check_data_fin(sk); 187543b54c6eSMat Martineau 1876*e16163b6SPaolo Abeni /* if the msk data is completely acked, or the socket timedout, 1877*e16163b6SPaolo Abeni * there is no point in keeping around an orphaned sk 1878*e16163b6SPaolo Abeni */ 1879*e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 1880*e16163b6SPaolo Abeni (mptcp_check_close_timeout(sk) || 1881*e16163b6SPaolo Abeni (state != sk->sk_state && 1882*e16163b6SPaolo Abeni ((1 << inet_sk_state_load(sk)) & (TCPF_CLOSE | TCPF_FIN_WAIT2))))) { 1883*e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 1884*e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 1885*e16163b6SPaolo Abeni goto unlock; 1886*e16163b6SPaolo Abeni } 1887*e16163b6SPaolo Abeni 18883b1d6210SPaolo Abeni if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 18893b1d6210SPaolo Abeni goto unlock; 18903b1d6210SPaolo Abeni 18913b1d6210SPaolo Abeni dfrag = mptcp_rtx_head(sk); 18923b1d6210SPaolo Abeni if (!dfrag) 18933b1d6210SPaolo Abeni goto unlock; 18943b1d6210SPaolo Abeni 1895149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1896149f7c71SFlorian Westphal goto reset_unlock; 1897149f7c71SFlorian Westphal 18983b1d6210SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 18993b1d6210SPaolo Abeni if (!ssk) 19003b1d6210SPaolo Abeni goto reset_unlock; 19013b1d6210SPaolo Abeni 19023b1d6210SPaolo Abeni lock_sock(ssk); 19033b1d6210SPaolo Abeni 19043b1d6210SPaolo Abeni orig_len = dfrag->data_len; 19053b1d6210SPaolo Abeni orig_offset = dfrag->offset; 19063b1d6210SPaolo Abeni orig_write_seq = dfrag->data_seq; 19073b1d6210SPaolo Abeni while (dfrag->data_len > 0) { 1908caf971dfSPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &info); 19093b1d6210SPaolo Abeni if (ret < 0) 19103b1d6210SPaolo Abeni break; 19113b1d6210SPaolo Abeni 1912fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 19133b1d6210SPaolo Abeni copied += ret; 19143b1d6210SPaolo Abeni dfrag->data_len -= ret; 19153b1d6210SPaolo Abeni dfrag->offset += ret; 1916149f7c71SFlorian Westphal 1917149f7c71SFlorian Westphal if (!mptcp_ext_cache_refill(msk)) 1918149f7c71SFlorian Westphal break; 19193b1d6210SPaolo Abeni } 19203b1d6210SPaolo Abeni if (copied) 1921caf971dfSPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 1922caf971dfSPaolo Abeni info.size_goal); 19233b1d6210SPaolo Abeni 19243b1d6210SPaolo Abeni dfrag->data_seq = orig_write_seq; 19253b1d6210SPaolo Abeni dfrag->offset = orig_offset; 19263b1d6210SPaolo Abeni dfrag->data_len = orig_len; 19273b1d6210SPaolo Abeni 19283b1d6210SPaolo Abeni mptcp_set_timeout(sk, ssk); 19293b1d6210SPaolo Abeni release_sock(ssk); 19303b1d6210SPaolo Abeni 19313b1d6210SPaolo Abeni reset_unlock: 19323b1d6210SPaolo Abeni if (!mptcp_timer_pending(sk)) 19333b1d6210SPaolo Abeni mptcp_reset_timer(sk); 19343b1d6210SPaolo Abeni 19353b1d6210SPaolo Abeni unlock: 1936*e16163b6SPaolo Abeni clear_bit(MPTCP_WORKER_RUNNING, &msk->flags); 193780992017SPaolo Abeni release_sock(sk); 193880992017SPaolo Abeni sock_put(sk); 193980992017SPaolo Abeni } 194080992017SPaolo Abeni 1941784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 1942f870fa0bSMat Martineau { 1943cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1944cec37a6eSPeter Krystad 1945ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 1946ec3edaa7SPeter Krystad 1947cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 1948ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 194918b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 19501891c4a0SFlorian Westphal __set_bit(MPTCP_SEND_SPACE, &msk->flags); 195180992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 1952ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 1953f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 1954cec37a6eSPeter Krystad 19558ab183deSPaolo Abeni msk->first = NULL; 1956dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 19578ab183deSPaolo Abeni 19581b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 19591b1c7a0eSPeter Krystad 1960b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 1961b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1962*e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 1963f870fa0bSMat Martineau return 0; 1964f870fa0bSMat Martineau } 1965f870fa0bSMat Martineau 1966784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 1967784325e9SMatthieu Baerts { 1968fc518953SFlorian Westphal struct net *net = sock_net(sk); 1969fc518953SFlorian Westphal int ret; 197018b683bfSPaolo Abeni 1971b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 1972b6c08380SGeliang Tang if (ret) 1973b6c08380SGeliang Tang return ret; 1974b6c08380SGeliang Tang 1975fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 1976fc518953SFlorian Westphal return -ENOPROTOOPT; 1977fc518953SFlorian Westphal 1978fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1979fc518953SFlorian Westphal return -ENOMEM; 1980fc518953SFlorian Westphal 1981fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 1982fa68018dSPaolo Abeni if (ret) 1983fa68018dSPaolo Abeni return ret; 1984fa68018dSPaolo Abeni 1985d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 1986a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 1987da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 1988d027236cSPaolo Abeni 198918b683bfSPaolo Abeni return 0; 199018b683bfSPaolo Abeni } 199118b683bfSPaolo Abeni 199218b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 199318b683bfSPaolo Abeni { 199418b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 199518b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 199618b683bfSPaolo Abeni 1997b51f9b80SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1998b51f9b80SPaolo Abeni 199918b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2000d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2001784325e9SMatthieu Baerts } 2002784325e9SMatthieu Baerts 200380992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 200480992017SPaolo Abeni { 200580992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 200680992017SPaolo Abeni 2007*e16163b6SPaolo Abeni /* if called by the work itself, do not try to cancel the work, or 2008*e16163b6SPaolo Abeni * we will hang. 2009*e16163b6SPaolo Abeni */ 2010*e16163b6SPaolo Abeni if (!test_bit(MPTCP_WORKER_RUNNING, &msk->flags) && 2011*e16163b6SPaolo Abeni cancel_work_sync(&msk->work)) 2012*e16163b6SPaolo Abeni __sock_put(sk); 201380992017SPaolo Abeni } 201480992017SPaolo Abeni 2015d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 201621498490SPeter Krystad { 201721498490SPeter Krystad lock_sock(ssk); 201821498490SPeter Krystad 201921498490SPeter Krystad switch (ssk->sk_state) { 202021498490SPeter Krystad case TCP_LISTEN: 202121498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 202221498490SPeter Krystad break; 2023df561f66SGustavo A. R. Silva fallthrough; 202421498490SPeter Krystad case TCP_SYN_SENT: 202521498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 202621498490SPeter Krystad break; 202721498490SPeter Krystad default: 202843b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 202943b54c6eSMat Martineau pr_debug("Fallback"); 203021498490SPeter Krystad ssk->sk_shutdown |= how; 203121498490SPeter Krystad tcp_shutdown(ssk, how); 203243b54c6eSMat Martineau } else { 203343b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 203443b54c6eSMat Martineau mptcp_set_timeout(sk, ssk); 203543b54c6eSMat Martineau tcp_send_ack(ssk); 203643b54c6eSMat Martineau } 203721498490SPeter Krystad break; 203821498490SPeter Krystad } 203921498490SPeter Krystad 204021498490SPeter Krystad release_sock(ssk); 204121498490SPeter Krystad } 204221498490SPeter Krystad 20436920b851SMat Martineau static const unsigned char new_state[16] = { 20446920b851SMat Martineau /* current state: new state: action: */ 20456920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 20466920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 20476920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 20486920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 20496920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 20506920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 20516920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 20526920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 20536920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 20546920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 20556920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 20566920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 20576920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 20586920b851SMat Martineau }; 20596920b851SMat Martineau 20606920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 20616920b851SMat Martineau { 20626920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 20636920b851SMat Martineau int ns = next & TCP_STATE_MASK; 20646920b851SMat Martineau 20656920b851SMat Martineau inet_sk_state_store(sk, ns); 20666920b851SMat Martineau 20676920b851SMat Martineau return next & TCP_ACTION_FIN; 20686920b851SMat Martineau } 20696920b851SMat Martineau 2070*e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2071*e16163b6SPaolo Abeni { 2072*e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2073*e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2074*e16163b6SPaolo Abeni 2075*e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2076*e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2077*e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2078*e16163b6SPaolo Abeni 2079*e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2080*e16163b6SPaolo Abeni * skip this 2081*e16163b6SPaolo Abeni */ 2082*e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2083*e16163b6SPaolo Abeni mptcp_send_head(sk)) 2084*e16163b6SPaolo Abeni return; 2085*e16163b6SPaolo Abeni 2086*e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2087*e16163b6SPaolo Abeni 2088*e16163b6SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to close now */ 2089*e16163b6SPaolo Abeni if (__mptcp_check_fallback(msk) && sk->sk_state == TCP_LAST_ACK) { 2090*e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2091*e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 2092*e16163b6SPaolo Abeni } 2093*e16163b6SPaolo Abeni 2094*e16163b6SPaolo Abeni __mptcp_flush_join_list(msk); 2095*e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2096*e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2097*e16163b6SPaolo Abeni 2098*e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2099*e16163b6SPaolo Abeni } 2100*e16163b6SPaolo Abeni } 2101*e16163b6SPaolo Abeni 2102*e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2103*e16163b6SPaolo Abeni { 2104*e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2105*e16163b6SPaolo Abeni 2106*e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2107*e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2108*e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2109*e16163b6SPaolo Abeni 2110*e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2111*e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2112*e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2113*e16163b6SPaolo Abeni 2114*e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2115*e16163b6SPaolo Abeni } 2116*e16163b6SPaolo Abeni 2117*e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2118f870fa0bSMat Martineau { 2119cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2120f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2121b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2122f870fa0bSMat Martineau 2123*e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 21242c22c06cSFlorian Westphal 212510f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 212610f6d46cSPaolo Abeni * mptcp_finish_join(). 212710f6d46cSPaolo Abeni */ 212810f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 212910f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 213010f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2131b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2132b2c5b614SFlorian Westphal 213318b683bfSPaolo Abeni __mptcp_clear_xmit(sk); 2134*e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2135*e16163b6SPaolo Abeni msk->pm.status = 0; 2136b2c5b614SFlorian Westphal 2137b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2138cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2139*e16163b6SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow); 2140f870fa0bSMat Martineau } 2141f870fa0bSMat Martineau 2142*e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2143*e16163b6SPaolo Abeni 2144*e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2145*e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2146*e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2147*e16163b6SPaolo Abeni sock_put(sk); 2148*e16163b6SPaolo Abeni } 2149*e16163b6SPaolo Abeni 2150*e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2151*e16163b6SPaolo Abeni { 2152*e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2153*e16163b6SPaolo Abeni bool do_cancel_work = false; 2154*e16163b6SPaolo Abeni 2155*e16163b6SPaolo Abeni lock_sock(sk); 2156*e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2157*e16163b6SPaolo Abeni 2158*e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2159*e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2160*e16163b6SPaolo Abeni goto cleanup; 2161*e16163b6SPaolo Abeni } 2162*e16163b6SPaolo Abeni 2163*e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2164*e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2165*e16163b6SPaolo Abeni 2166*e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2167*e16163b6SPaolo Abeni 2168*e16163b6SPaolo Abeni cleanup: 2169*e16163b6SPaolo Abeni /* orphan all the subflows */ 2170*e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2171*e16163b6SPaolo Abeni list_for_each_entry(subflow, &mptcp_sk(sk)->conn_list, node) { 2172*e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2173*e16163b6SPaolo Abeni bool slow, dispose_socket; 2174*e16163b6SPaolo Abeni struct socket *sock; 2175*e16163b6SPaolo Abeni 2176*e16163b6SPaolo Abeni slow = lock_sock_fast(ssk); 2177*e16163b6SPaolo Abeni sock = ssk->sk_socket; 2178*e16163b6SPaolo Abeni dispose_socket = sock && sock != sk->sk_socket; 2179*e16163b6SPaolo Abeni sock_orphan(ssk); 2180*e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2181*e16163b6SPaolo Abeni 2182*e16163b6SPaolo Abeni /* for the outgoing subflows we additionally need to free 2183*e16163b6SPaolo Abeni * the associated socket 2184*e16163b6SPaolo Abeni */ 2185*e16163b6SPaolo Abeni if (dispose_socket) 2186*e16163b6SPaolo Abeni iput(SOCK_INODE(sock)); 2187*e16163b6SPaolo Abeni } 2188*e16163b6SPaolo Abeni sock_orphan(sk); 2189*e16163b6SPaolo Abeni 2190*e16163b6SPaolo Abeni sock_hold(sk); 2191*e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2192*e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2193*e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2194*e16163b6SPaolo Abeni do_cancel_work = true; 2195*e16163b6SPaolo Abeni } else { 2196*e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2197*e16163b6SPaolo Abeni } 2198*e16163b6SPaolo Abeni release_sock(sk); 2199*e16163b6SPaolo Abeni if (do_cancel_work) 220080992017SPaolo Abeni mptcp_cancel_work(sk); 2201*e16163b6SPaolo Abeni sock_put(sk); 2202f870fa0bSMat Martineau } 2203f870fa0bSMat Martineau 2204cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2205cf7da0d6SPeter Krystad { 2206cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2207cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2208cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2209cf7da0d6SPeter Krystad 2210cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2211cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2212cf7da0d6SPeter Krystad 2213cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2214cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2215cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2216cf7da0d6SPeter Krystad } 2217cf7da0d6SPeter Krystad #endif 2218cf7da0d6SPeter Krystad 2219cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2220cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2221cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2222cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2223cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2224cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2225cf7da0d6SPeter Krystad } 2226cf7da0d6SPeter Krystad 222718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 222818b683bfSPaolo Abeni { 222942c556feSFlorian Westphal /* Should never be called. 223042c556feSFlorian Westphal * inet_stream_connect() calls ->disconnect, but that 223142c556feSFlorian Westphal * refers to the subflow socket, not the mptcp one. 223242c556feSFlorian Westphal */ 223342c556feSFlorian Westphal WARN_ON_ONCE(1); 223442c556feSFlorian Westphal return 0; 223518b683bfSPaolo Abeni } 223618b683bfSPaolo Abeni 2237b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2238b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2239b0519de8SFlorian Westphal { 2240b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2241b0519de8SFlorian Westphal 2242b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2243b0519de8SFlorian Westphal } 2244b0519de8SFlorian Westphal #endif 2245b0519de8SFlorian Westphal 2246fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2247cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2248fca5c82cSPaolo Abeni struct request_sock *req) 2249b0519de8SFlorian Westphal { 225058b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2251b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 225258b09919SPaolo Abeni struct mptcp_sock *msk; 225358b09919SPaolo Abeni u64 ack_seq; 2254b0519de8SFlorian Westphal 2255b0519de8SFlorian Westphal if (!nsk) 2256b0519de8SFlorian Westphal return NULL; 2257b0519de8SFlorian Westphal 2258b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2259b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2260b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2261b0519de8SFlorian Westphal #endif 2262b0519de8SFlorian Westphal 226358b09919SPaolo Abeni __mptcp_init_sock(nsk); 226458b09919SPaolo Abeni 226558b09919SPaolo Abeni msk = mptcp_sk(nsk); 226658b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 226758b09919SPaolo Abeni msk->token = subflow_req->token; 226858b09919SPaolo Abeni msk->subflow = NULL; 2269b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 227058b09919SPaolo Abeni 227158b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2272eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 2273cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 2274cfde141eSPaolo Abeni if (mp_opt->mp_capable) { 227558b09919SPaolo Abeni msk->can_ack = true; 2276cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 227758b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 227858b09919SPaolo Abeni ack_seq++; 2279917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 228058b09919SPaolo Abeni } 22817f20d5fcSPaolo Abeni 22825e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 22837f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 22847f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 228558b09919SPaolo Abeni bh_unlock_sock(nsk); 228658b09919SPaolo Abeni 228758b09919SPaolo Abeni /* keep a single reference */ 228858b09919SPaolo Abeni __sock_put(nsk); 2289b0519de8SFlorian Westphal return nsk; 2290b0519de8SFlorian Westphal } 2291b0519de8SFlorian Westphal 2292a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2293a6b118feSFlorian Westphal { 2294a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2295a6b118feSFlorian Westphal 2296a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2297a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2298a6b118feSFlorian Westphal 2299a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2300a6b118feSFlorian Westphal 2301a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2302a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2303a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2304a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2305a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 2306a6b118feSFlorian Westphal } 2307a6b118feSFlorian Westphal 2308cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2309cf7da0d6SPeter Krystad bool kern) 2310cf7da0d6SPeter Krystad { 2311cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2312cf7da0d6SPeter Krystad struct socket *listener; 2313cf7da0d6SPeter Krystad struct sock *newsk; 2314cf7da0d6SPeter Krystad 2315cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2316cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2317cf7da0d6SPeter Krystad *err = -EINVAL; 2318cf7da0d6SPeter Krystad return NULL; 2319cf7da0d6SPeter Krystad } 2320cf7da0d6SPeter Krystad 2321cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2322cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2323cf7da0d6SPeter Krystad if (!newsk) 2324cf7da0d6SPeter Krystad return NULL; 2325cf7da0d6SPeter Krystad 2326cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2327cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2328cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2329cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2330cf7da0d6SPeter Krystad struct sock *ssk = newsk; 2331cf7da0d6SPeter Krystad 2332cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 233358b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 233458b09919SPaolo Abeni 233558b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 233658b09919SPaolo Abeni * subflow_syn_recv_sock() 233758b09919SPaolo Abeni */ 233858b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 233958b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 234058b09919SPaolo Abeni return newsk; 234158b09919SPaolo Abeni } 234258b09919SPaolo Abeni 234358b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 234458b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2345cf7da0d6SPeter Krystad 2346cf7da0d6SPeter Krystad local_bh_disable(); 234758b09919SPaolo Abeni bh_lock_sock(new_mptcp_sock); 2348cf7da0d6SPeter Krystad msk = mptcp_sk(new_mptcp_sock); 23498ab183deSPaolo Abeni msk->first = newsk; 2350cf7da0d6SPeter Krystad 2351cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 2352cf7da0d6SPeter Krystad mptcp_copy_inaddrs(newsk, ssk); 2353cf7da0d6SPeter Krystad list_add(&subflow->node, &msk->conn_list); 2354*e16163b6SPaolo Abeni sock_hold(ssk); 2355cf7da0d6SPeter Krystad 2356a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 2357cf7da0d6SPeter Krystad bh_unlock_sock(new_mptcp_sock); 2358fc518953SFlorian Westphal 2359fc518953SFlorian Westphal __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2360cf7da0d6SPeter Krystad local_bh_enable(); 2361fc518953SFlorian Westphal } else { 2362fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2363fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2364cf7da0d6SPeter Krystad } 2365cf7da0d6SPeter Krystad 2366cf7da0d6SPeter Krystad return newsk; 2367cf7da0d6SPeter Krystad } 2368cf7da0d6SPeter Krystad 23695c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 23705c8c1640SGeliang Tang { 23715c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 23725c8c1640SGeliang Tang mptcp_token_destroy(msk); 23735c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 23745c8c1640SGeliang Tang } 23755c8c1640SGeliang Tang 237679c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 237779c0949eSPeter Krystad { 2378c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2379c9fd9c5fSFlorian Westphal 2380c9fd9c5fSFlorian Westphal if (msk->cached_ext) 2381c9fd9c5fSFlorian Westphal __skb_ext_put(msk->cached_ext); 2382d027236cSPaolo Abeni 23835c8c1640SGeliang Tang mptcp_destroy_common(msk); 2384d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 238579c0949eSPeter Krystad } 238679c0949eSPeter Krystad 2387fd1452d8SFlorian Westphal static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname, 2388a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2389fd1452d8SFlorian Westphal { 2390fd1452d8SFlorian Westphal struct sock *sk = (struct sock *)msk; 2391fd1452d8SFlorian Westphal struct socket *ssock; 2392fd1452d8SFlorian Westphal int ret; 2393fd1452d8SFlorian Westphal 2394fd1452d8SFlorian Westphal switch (optname) { 2395fd1452d8SFlorian Westphal case SO_REUSEPORT: 2396fd1452d8SFlorian Westphal case SO_REUSEADDR: 2397fd1452d8SFlorian Westphal lock_sock(sk); 2398fd1452d8SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2399fd1452d8SFlorian Westphal if (!ssock) { 2400fd1452d8SFlorian Westphal release_sock(sk); 2401fd1452d8SFlorian Westphal return -EINVAL; 2402fd1452d8SFlorian Westphal } 2403fd1452d8SFlorian Westphal 2404a7b75c5aSChristoph Hellwig ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen); 2405fd1452d8SFlorian Westphal if (ret == 0) { 2406fd1452d8SFlorian Westphal if (optname == SO_REUSEPORT) 2407fd1452d8SFlorian Westphal sk->sk_reuseport = ssock->sk->sk_reuseport; 2408fd1452d8SFlorian Westphal else if (optname == SO_REUSEADDR) 2409fd1452d8SFlorian Westphal sk->sk_reuse = ssock->sk->sk_reuse; 2410fd1452d8SFlorian Westphal } 2411fd1452d8SFlorian Westphal release_sock(sk); 2412fd1452d8SFlorian Westphal return ret; 2413fd1452d8SFlorian Westphal } 2414fd1452d8SFlorian Westphal 2415a7b75c5aSChristoph Hellwig return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen); 2416fd1452d8SFlorian Westphal } 2417fd1452d8SFlorian Westphal 2418c9b95a13SFlorian Westphal static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname, 2419a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2420c9b95a13SFlorian Westphal { 2421c9b95a13SFlorian Westphal struct sock *sk = (struct sock *)msk; 2422c9b95a13SFlorian Westphal int ret = -EOPNOTSUPP; 2423c9b95a13SFlorian Westphal struct socket *ssock; 2424c9b95a13SFlorian Westphal 2425c9b95a13SFlorian Westphal switch (optname) { 2426c9b95a13SFlorian Westphal case IPV6_V6ONLY: 2427c9b95a13SFlorian Westphal lock_sock(sk); 2428c9b95a13SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2429c9b95a13SFlorian Westphal if (!ssock) { 2430c9b95a13SFlorian Westphal release_sock(sk); 2431c9b95a13SFlorian Westphal return -EINVAL; 2432c9b95a13SFlorian Westphal } 2433c9b95a13SFlorian Westphal 2434c9b95a13SFlorian Westphal ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen); 2435c9b95a13SFlorian Westphal if (ret == 0) 2436c9b95a13SFlorian Westphal sk->sk_ipv6only = ssock->sk->sk_ipv6only; 2437c9b95a13SFlorian Westphal 2438c9b95a13SFlorian Westphal release_sock(sk); 2439c9b95a13SFlorian Westphal break; 2440c9b95a13SFlorian Westphal } 2441c9b95a13SFlorian Westphal 2442c9b95a13SFlorian Westphal return ret; 2443c9b95a13SFlorian Westphal } 2444c9b95a13SFlorian Westphal 2445717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname, 2446a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2447717e79c8SPeter Krystad { 2448717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 244976660afbSPaolo Abeni struct sock *ssk; 2450717e79c8SPeter Krystad 2451717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2452717e79c8SPeter Krystad 245383f0c10bSFlorian Westphal if (level == SOL_SOCKET) 2454fd1452d8SFlorian Westphal return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen); 245583f0c10bSFlorian Westphal 2456717e79c8SPeter Krystad /* @@ the meaning of setsockopt() when the socket is connected and 2457b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2458b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2459b6e4a1aeSMat Martineau * is in TCP fallback, when TCP socket options are passed through 2460b6e4a1aeSMat Martineau * to the one remaining subflow. 2461717e79c8SPeter Krystad */ 2462717e79c8SPeter Krystad lock_sock(sk); 246376660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2464e154659bSFlorian Westphal release_sock(sk); 246576660afbSPaolo Abeni if (ssk) 246676660afbSPaolo Abeni return tcp_setsockopt(ssk, level, optname, optval, optlen); 246750e741bbSFlorian Westphal 2468c9b95a13SFlorian Westphal if (level == SOL_IPV6) 2469c9b95a13SFlorian Westphal return mptcp_setsockopt_v6(msk, optname, optval, optlen); 2470c9b95a13SFlorian Westphal 2471b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2472717e79c8SPeter Krystad } 2473717e79c8SPeter Krystad 2474717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname, 247550e741bbSFlorian Westphal char __user *optval, int __user *option) 2476717e79c8SPeter Krystad { 2477717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 247876660afbSPaolo Abeni struct sock *ssk; 2479717e79c8SPeter Krystad 2480717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2481717e79c8SPeter Krystad 2482b6e4a1aeSMat Martineau /* @@ the meaning of setsockopt() when the socket is connected and 2483b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2484b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2485b6e4a1aeSMat Martineau * is in TCP fallback, when socket options are passed through 2486b6e4a1aeSMat Martineau * to the one remaining subflow. 2487717e79c8SPeter Krystad */ 2488717e79c8SPeter Krystad lock_sock(sk); 248976660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2490e154659bSFlorian Westphal release_sock(sk); 249176660afbSPaolo Abeni if (ssk) 249276660afbSPaolo Abeni return tcp_getsockopt(ssk, level, optname, optval, option); 249350e741bbSFlorian Westphal 2494b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2495717e79c8SPeter Krystad } 2496717e79c8SPeter Krystad 2497b51f9b80SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 2498b51f9b80SPaolo Abeni TCPF_WRITE_TIMER_DEFERRED) 249914c441b5SPaolo Abeni 250014c441b5SPaolo Abeni /* this is very alike tcp_release_cb() but we must handle differently a 250114c441b5SPaolo Abeni * different set of events 250214c441b5SPaolo Abeni */ 250314c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 250414c441b5SPaolo Abeni { 250514c441b5SPaolo Abeni unsigned long flags, nflags; 250614c441b5SPaolo Abeni 250714c441b5SPaolo Abeni do { 250814c441b5SPaolo Abeni flags = sk->sk_tsq_flags; 250914c441b5SPaolo Abeni if (!(flags & MPTCP_DEFERRED_ALL)) 251014c441b5SPaolo Abeni return; 251114c441b5SPaolo Abeni nflags = flags & ~MPTCP_DEFERRED_ALL; 251214c441b5SPaolo Abeni } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 251314c441b5SPaolo Abeni 2514b51f9b80SPaolo Abeni sock_release_ownership(sk); 2515b51f9b80SPaolo Abeni 251614c441b5SPaolo Abeni if (flags & TCPF_DELACK_TIMER_DEFERRED) { 251714c441b5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 251814c441b5SPaolo Abeni struct sock *ssk; 251914c441b5SPaolo Abeni 252014c441b5SPaolo Abeni ssk = mptcp_subflow_recv_lookup(msk); 2521ba8f48f7SPaolo Abeni if (!ssk || sk->sk_state == TCP_CLOSE || 2522ba8f48f7SPaolo Abeni !schedule_work(&msk->work)) 252314c441b5SPaolo Abeni __sock_put(sk); 252414c441b5SPaolo Abeni } 2525b51f9b80SPaolo Abeni 2526b51f9b80SPaolo Abeni if (flags & TCPF_WRITE_TIMER_DEFERRED) { 2527b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 2528b51f9b80SPaolo Abeni __sock_put(sk); 2529b51f9b80SPaolo Abeni } 253014c441b5SPaolo Abeni } 253114c441b5SPaolo Abeni 25322c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 25332c5ebd00SPaolo Abeni { 25342c5ebd00SPaolo Abeni /* should never be called, 25352c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 25362c5ebd00SPaolo Abeni */ 25372c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 25382c5ebd00SPaolo Abeni return 0; 25392c5ebd00SPaolo Abeni } 25402c5ebd00SPaolo Abeni 25412c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 25422c5ebd00SPaolo Abeni { 25432c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 25442c5ebd00SPaolo Abeni } 25452c5ebd00SPaolo Abeni 2546cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 2547f870fa0bSMat Martineau { 2548f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2549cec37a6eSPeter Krystad struct socket *ssock; 2550f870fa0bSMat Martineau 2551cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 2552cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 2553cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 2554cec37a6eSPeter Krystad return -EINVAL; 2555f870fa0bSMat Martineau 2556cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 2557cec37a6eSPeter Krystad } 2558f870fa0bSMat Martineau 2559cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 2560cec37a6eSPeter Krystad { 2561cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 2562cec37a6eSPeter Krystad struct mptcp_sock *msk; 2563cec37a6eSPeter Krystad struct sock *sk; 25646d0060f6SMat Martineau u64 ack_seq; 2565f870fa0bSMat Martineau 2566cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 2567cec37a6eSPeter Krystad sk = subflow->conn; 2568cec37a6eSPeter Krystad msk = mptcp_sk(sk); 2569cec37a6eSPeter Krystad 2570648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 2571648ef4b8SMat Martineau 25726d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 25736d0060f6SMat Martineau ack_seq++; 2574648ef4b8SMat Martineau subflow->map_seq = ack_seq; 2575648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 25766d0060f6SMat Martineau 2577cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 2578cec37a6eSPeter Krystad * accessing the field below 2579cec37a6eSPeter Krystad */ 2580cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 2581cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 25826d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 2583eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 25846d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2585d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 2586cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 25871b1c7a0eSPeter Krystad 25881b1c7a0eSPeter Krystad mptcp_pm_new_connection(msk, 0); 2589a6b118feSFlorian Westphal 2590a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 2591f870fa0bSMat Martineau } 2592f870fa0bSMat Martineau 2593cf7da0d6SPeter Krystad static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 2594cf7da0d6SPeter Krystad { 2595cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 2596cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 2597cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 2598cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 2599cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 2600cf7da0d6SPeter Krystad } 2601cf7da0d6SPeter Krystad 2602*e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 2603f296234cSPeter Krystad { 2604*e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2605f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 2606f296234cSPeter Krystad struct sock *parent = (void *)msk; 2607f296234cSPeter Krystad struct socket *parent_sock; 2608ec3edaa7SPeter Krystad bool ret; 2609f296234cSPeter Krystad 2610f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 2611f296234cSPeter Krystad 2612f296234cSPeter Krystad /* mptcp socket already closing? */ 2613b93df08cSPaolo Abeni if (!mptcp_is_fully_established(parent)) 2614f296234cSPeter Krystad return false; 2615f296234cSPeter Krystad 2616f296234cSPeter Krystad if (!msk->pm.server_side) 2617f296234cSPeter Krystad return true; 2618f296234cSPeter Krystad 261910f6d46cSPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 262010f6d46cSPaolo Abeni return false; 262110f6d46cSPaolo Abeni 262210f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 262310f6d46cSPaolo Abeni * msk lock here. 262410f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 2625*e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 262610f6d46cSPaolo Abeni */ 262710f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 262810f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 2629*e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 263010f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 2631*e16163b6SPaolo Abeni sock_hold(ssk); 2632*e16163b6SPaolo Abeni } 263310f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 263410f6d46cSPaolo Abeni if (!ret) 263510f6d46cSPaolo Abeni return false; 263610f6d46cSPaolo Abeni 263710f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 263810f6d46cSPaolo Abeni * at close time 263910f6d46cSPaolo Abeni */ 2640f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 2641*e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 2642*e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 2643917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 264410f6d46cSPaolo Abeni return true; 2645f296234cSPeter Krystad } 2646f296234cSPeter Krystad 26471891c4a0SFlorian Westphal static bool mptcp_memory_free(const struct sock *sk, int wake) 26481891c4a0SFlorian Westphal { 26491891c4a0SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 26501891c4a0SFlorian Westphal 26511891c4a0SFlorian Westphal return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 26521891c4a0SFlorian Westphal } 26531891c4a0SFlorian Westphal 2654f870fa0bSMat Martineau static struct proto mptcp_prot = { 2655f870fa0bSMat Martineau .name = "MPTCP", 2656f870fa0bSMat Martineau .owner = THIS_MODULE, 2657f870fa0bSMat Martineau .init = mptcp_init_sock, 265818b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 2659f870fa0bSMat Martineau .close = mptcp_close, 2660cf7da0d6SPeter Krystad .accept = mptcp_accept, 2661717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 2662717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 2663f870fa0bSMat Martineau .shutdown = tcp_shutdown, 266479c0949eSPeter Krystad .destroy = mptcp_destroy, 2665f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 2666f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 266714c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 26682c5ebd00SPaolo Abeni .hash = mptcp_hash, 26692c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 2670cec37a6eSPeter Krystad .get_port = mptcp_get_port, 2671d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 2672d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 2673d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 26741891c4a0SFlorian Westphal .stream_memory_free = mptcp_memory_free, 2675d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 2676989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 2677d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 2678f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 26792c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 2680f870fa0bSMat Martineau .no_autobind = true, 2681f870fa0bSMat Martineau }; 2682f870fa0bSMat Martineau 26832303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 26842303f994SPeter Krystad { 26852303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 26862303f994SPeter Krystad struct socket *ssock; 2687cf7da0d6SPeter Krystad int err; 26882303f994SPeter Krystad 26892303f994SPeter Krystad lock_sock(sock->sk); 2690fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2691fa68018dSPaolo Abeni if (!ssock) { 2692fa68018dSPaolo Abeni err = -EINVAL; 26932303f994SPeter Krystad goto unlock; 26942303f994SPeter Krystad } 26952303f994SPeter Krystad 26962303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 2697cf7da0d6SPeter Krystad if (!err) 2698cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 26992303f994SPeter Krystad 27002303f994SPeter Krystad unlock: 27012303f994SPeter Krystad release_sock(sock->sk); 27022303f994SPeter Krystad return err; 27032303f994SPeter Krystad } 27042303f994SPeter Krystad 27050235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 27060235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 27070235d075SPaolo Abeni { 27080235d075SPaolo Abeni subflow->request_mptcp = 0; 27090235d075SPaolo Abeni __mptcp_do_fallback(msk); 27100235d075SPaolo Abeni } 27110235d075SPaolo Abeni 27122303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 27132303f994SPeter Krystad int addr_len, int flags) 27142303f994SPeter Krystad { 27152303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 27162c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 27172303f994SPeter Krystad struct socket *ssock; 27182303f994SPeter Krystad int err; 27192303f994SPeter Krystad 27202303f994SPeter Krystad lock_sock(sock->sk); 272141be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 272241be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 272341be81a8SPaolo Abeni * cope with that 272441be81a8SPaolo Abeni */ 272541be81a8SPaolo Abeni ssock = msk->subflow; 272641be81a8SPaolo Abeni goto do_connect; 272741be81a8SPaolo Abeni } 272841be81a8SPaolo Abeni 2729fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2730fa68018dSPaolo Abeni if (!ssock) { 2731fa68018dSPaolo Abeni err = -EINVAL; 27322303f994SPeter Krystad goto unlock; 27332303f994SPeter Krystad } 27342303f994SPeter Krystad 2735fa68018dSPaolo Abeni mptcp_token_destroy(msk); 2736fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 27372c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 2738cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 2739cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 2740cf7da0d6SPeter Krystad * TCP option space. 2741cf7da0d6SPeter Krystad */ 2742cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 27430235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 2744cf7da0d6SPeter Krystad #endif 27452c5ebd00SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) 27460235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 2747cf7da0d6SPeter Krystad 274841be81a8SPaolo Abeni do_connect: 27492303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 275041be81a8SPaolo Abeni sock->state = ssock->state; 275141be81a8SPaolo Abeni 275241be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 275341be81a8SPaolo Abeni * subflow_finish_connect() 275441be81a8SPaolo Abeni */ 2755367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 2756cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 275741be81a8SPaolo Abeni else 275841be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 27592303f994SPeter Krystad 27602303f994SPeter Krystad unlock: 27612303f994SPeter Krystad release_sock(sock->sk); 27622303f994SPeter Krystad return err; 27632303f994SPeter Krystad } 27642303f994SPeter Krystad 2765cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 2766cf7da0d6SPeter Krystad { 2767cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2768cf7da0d6SPeter Krystad struct socket *ssock; 2769cf7da0d6SPeter Krystad int err; 2770cf7da0d6SPeter Krystad 2771cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 2772cf7da0d6SPeter Krystad 2773cf7da0d6SPeter Krystad lock_sock(sock->sk); 2774fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 2775fa68018dSPaolo Abeni if (!ssock) { 2776fa68018dSPaolo Abeni err = -EINVAL; 2777cf7da0d6SPeter Krystad goto unlock; 2778cf7da0d6SPeter Krystad } 2779cf7da0d6SPeter Krystad 2780fa68018dSPaolo Abeni mptcp_token_destroy(msk); 2781fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 27825e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 27835e20087dSFlorian Westphal 2784cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 2785cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 2786cf7da0d6SPeter Krystad if (!err) 2787cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 2788cf7da0d6SPeter Krystad 2789cf7da0d6SPeter Krystad unlock: 2790cf7da0d6SPeter Krystad release_sock(sock->sk); 2791cf7da0d6SPeter Krystad return err; 2792cf7da0d6SPeter Krystad } 2793cf7da0d6SPeter Krystad 2794cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 2795cf7da0d6SPeter Krystad int flags, bool kern) 2796cf7da0d6SPeter Krystad { 2797cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2798cf7da0d6SPeter Krystad struct socket *ssock; 2799cf7da0d6SPeter Krystad int err; 2800cf7da0d6SPeter Krystad 2801cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 2802cf7da0d6SPeter Krystad 2803cf7da0d6SPeter Krystad lock_sock(sock->sk); 2804cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 2805cf7da0d6SPeter Krystad goto unlock_fail; 2806cf7da0d6SPeter Krystad 2807cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 2808cf7da0d6SPeter Krystad if (!ssock) 2809cf7da0d6SPeter Krystad goto unlock_fail; 2810cf7da0d6SPeter Krystad 28118a05661bSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 2812cf7da0d6SPeter Krystad sock_hold(ssock->sk); 2813cf7da0d6SPeter Krystad release_sock(sock->sk); 2814cf7da0d6SPeter Krystad 2815cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 2816d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 2817cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 2818cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2819cf7da0d6SPeter Krystad 2820cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 2821cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 2822cf7da0d6SPeter Krystad */ 2823ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 2824190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 2825cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2826cf7da0d6SPeter Krystad 2827cf7da0d6SPeter Krystad if (!ssk->sk_socket) 2828cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 2829cf7da0d6SPeter Krystad } 2830cf7da0d6SPeter Krystad } 2831cf7da0d6SPeter Krystad 28328a05661bSPaolo Abeni if (inet_csk_listen_poll(ssock->sk)) 28338a05661bSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 2834cf7da0d6SPeter Krystad sock_put(ssock->sk); 2835cf7da0d6SPeter Krystad return err; 2836cf7da0d6SPeter Krystad 2837cf7da0d6SPeter Krystad unlock_fail: 2838cf7da0d6SPeter Krystad release_sock(sock->sk); 2839cf7da0d6SPeter Krystad return -EINVAL; 2840cf7da0d6SPeter Krystad } 2841cf7da0d6SPeter Krystad 28428a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 28438a05661bSPaolo Abeni { 28448a05661bSPaolo Abeni return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 28458a05661bSPaolo Abeni 0; 28468a05661bSPaolo Abeni } 28478a05661bSPaolo Abeni 28482303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 28492303f994SPeter Krystad struct poll_table_struct *wait) 28502303f994SPeter Krystad { 28511891c4a0SFlorian Westphal struct sock *sk = sock->sk; 28528ab183deSPaolo Abeni struct mptcp_sock *msk; 28532303f994SPeter Krystad __poll_t mask = 0; 28548a05661bSPaolo Abeni int state; 28552303f994SPeter Krystad 28561891c4a0SFlorian Westphal msk = mptcp_sk(sk); 28571891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 28581891c4a0SFlorian Westphal 28598a05661bSPaolo Abeni state = inet_sk_state_load(sk); 28606719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 28618a05661bSPaolo Abeni if (state == TCP_LISTEN) 28628a05661bSPaolo Abeni return mptcp_check_readable(msk); 28638a05661bSPaolo Abeni 28648a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 28658a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 286663561a40SPaolo Abeni if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 28671891c4a0SFlorian Westphal mask |= EPOLLOUT | EPOLLWRNORM; 28688a05661bSPaolo Abeni } 28691891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 28701891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 28711891c4a0SFlorian Westphal 28722303f994SPeter Krystad return mask; 28732303f994SPeter Krystad } 28742303f994SPeter Krystad 287521498490SPeter Krystad static int mptcp_shutdown(struct socket *sock, int how) 287621498490SPeter Krystad { 287721498490SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 2878*e16163b6SPaolo Abeni struct sock *sk = sock->sk; 287921498490SPeter Krystad int ret = 0; 288021498490SPeter Krystad 288121498490SPeter Krystad pr_debug("sk=%p, how=%d", msk, how); 288221498490SPeter Krystad 2883*e16163b6SPaolo Abeni lock_sock(sk); 288421498490SPeter Krystad 288521498490SPeter Krystad how++; 288621498490SPeter Krystad if ((how & ~SHUTDOWN_MASK) || !how) { 288721498490SPeter Krystad ret = -EINVAL; 288821498490SPeter Krystad goto out_unlock; 288921498490SPeter Krystad } 289021498490SPeter Krystad 289121498490SPeter Krystad if (sock->state == SS_CONNECTING) { 2892*e16163b6SPaolo Abeni if ((1 << sk->sk_state) & 289321498490SPeter Krystad (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 289421498490SPeter Krystad sock->state = SS_DISCONNECTING; 289521498490SPeter Krystad else 289621498490SPeter Krystad sock->state = SS_CONNECTED; 289721498490SPeter Krystad } 289821498490SPeter Krystad 2899*e16163b6SPaolo Abeni sk->sk_shutdown |= how; 2900*e16163b6SPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 2901*e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 290221498490SPeter Krystad 2903e1ff9e82SDavide Caratti /* Wake up anyone sleeping in poll. */ 2904*e16163b6SPaolo Abeni sk->sk_state_change(sk); 2905e1ff9e82SDavide Caratti 290621498490SPeter Krystad out_unlock: 2907*e16163b6SPaolo Abeni release_sock(sk); 290821498490SPeter Krystad 290921498490SPeter Krystad return ret; 291021498490SPeter Krystad } 291121498490SPeter Krystad 2912e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 2913e42f1ac6SFlorian Westphal .family = PF_INET, 2914e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2915e42f1ac6SFlorian Westphal .release = inet_release, 2916e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2917e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2918e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2919e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2920d2f77c53SPaolo Abeni .getname = inet_getname, 2921e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2922e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 2923e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2924e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2925e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2926e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2927e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2928e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 2929e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 2930e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2931e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2932e42f1ac6SFlorian Westphal }; 29332303f994SPeter Krystad 2934f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 2935f870fa0bSMat Martineau .type = SOCK_STREAM, 2936f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2937f870fa0bSMat Martineau .prot = &mptcp_prot, 29382303f994SPeter Krystad .ops = &mptcp_stream_ops, 29392303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 2940f870fa0bSMat Martineau }; 2941f870fa0bSMat Martineau 2942d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 2943f870fa0bSMat Martineau { 29442303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 29452303f994SPeter Krystad 2946d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 2947d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 2948d027236cSPaolo Abeni 29492303f994SPeter Krystad mptcp_subflow_init(); 29501b1c7a0eSPeter Krystad mptcp_pm_init(); 29512c5ebd00SPaolo Abeni mptcp_token_init(); 29522303f994SPeter Krystad 2953f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 2954f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 2955f870fa0bSMat Martineau 2956f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 29576771bfd9SFlorian Westphal 29586771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 2959f870fa0bSMat Martineau } 2960f870fa0bSMat Martineau 2961f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2962e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 2963e42f1ac6SFlorian Westphal .family = PF_INET6, 2964e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 2965e42f1ac6SFlorian Westphal .release = inet6_release, 2966e42f1ac6SFlorian Westphal .bind = mptcp_bind, 2967e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 2968e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 2969e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 2970d2f77c53SPaolo Abeni .getname = inet6_getname, 2971e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2972e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 2973e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2974e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2975e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2976e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2977e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2978e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 2979e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 2980e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2981e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2982e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 29833986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 2984e42f1ac6SFlorian Westphal #endif 2985e42f1ac6SFlorian Westphal }; 2986e42f1ac6SFlorian Westphal 2987f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 2988f870fa0bSMat Martineau 298979c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 299079c0949eSPeter Krystad { 299179c0949eSPeter Krystad mptcp_destroy(sk); 299279c0949eSPeter Krystad inet6_destroy_sock(sk); 299379c0949eSPeter Krystad } 299479c0949eSPeter Krystad 2995f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 2996f870fa0bSMat Martineau .type = SOCK_STREAM, 2997f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2998f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 29992303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3000f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3001f870fa0bSMat Martineau }; 3002f870fa0bSMat Martineau 3003d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3004f870fa0bSMat Martineau { 3005f870fa0bSMat Martineau int err; 3006f870fa0bSMat Martineau 3007f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3008f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3009f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 301079c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3011b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3012f870fa0bSMat Martineau 3013f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3014f870fa0bSMat Martineau if (err) 3015f870fa0bSMat Martineau return err; 3016f870fa0bSMat Martineau 3017f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3018f870fa0bSMat Martineau if (err) 3019f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3020f870fa0bSMat Martineau 3021f870fa0bSMat Martineau return err; 3022f870fa0bSMat Martineau } 3023f870fa0bSMat Martineau #endif 3024