1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 193721b9b6SMat Martineau #include <net/tcp_states.h> 20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21cf7da0d6SPeter Krystad #include <net/transp_v6.h> 22cf7da0d6SPeter Krystad #endif 23f870fa0bSMat Martineau #include <net/mptcp.h> 24e16163b6SPaolo Abeni #include <net/xfrm.h> 25f870fa0bSMat Martineau #include "protocol.h" 26fc518953SFlorian Westphal #include "mib.h" 27f870fa0bSMat Martineau 28b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 29b0519de8SFlorian Westphal struct mptcp6_sock { 30b0519de8SFlorian Westphal struct mptcp_sock msk; 31b0519de8SFlorian Westphal struct ipv6_pinfo np; 32b0519de8SFlorian Westphal }; 33b0519de8SFlorian Westphal #endif 34b0519de8SFlorian Westphal 356771bfd9SFlorian Westphal struct mptcp_skb_cb { 36ab174ad8SPaolo Abeni u64 map_seq; 37ab174ad8SPaolo Abeni u64 end_seq; 386771bfd9SFlorian Westphal u32 offset; 396771bfd9SFlorian Westphal }; 406771bfd9SFlorian Westphal 416771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 426771bfd9SFlorian Westphal 43d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated; 44d027236cSPaolo Abeni 45e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk); 46d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk); 47e16163b6SPaolo Abeni 48*b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 49*b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev; 50*b19bc294SPaolo Abeni 512303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 522303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 532303f994SPeter Krystad * Otherwise return NULL. 542303f994SPeter Krystad */ 552303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 562303f994SPeter Krystad { 57d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 582303f994SPeter Krystad return NULL; 592303f994SPeter Krystad 602303f994SPeter Krystad return msk->subflow; 612303f994SPeter Krystad } 622303f994SPeter Krystad 636f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */ 646f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 656f8a612aSFlorian Westphal { 667439d687SPaolo Abeni return READ_ONCE(msk->wnd_end); 676f8a612aSFlorian Westphal } 686f8a612aSFlorian Westphal 69d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk) 700b4f33deSFlorian Westphal { 710b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 720b4f33deSFlorian Westphal 730b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 740b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 750b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 760b4f33deSFlorian Westphal * not an mptcp one. 770b4f33deSFlorian Westphal * 780b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 790b4f33deSFlorian Westphal * bypass mptcp. 800b4f33deSFlorian Westphal */ 810b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 82d2f77c53SPaolo Abeni return true; 830b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 840b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 850b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 86d2f77c53SPaolo Abeni return true; 870b4f33deSFlorian Westphal #endif 880b4f33deSFlorian Westphal } 890b4f33deSFlorian Westphal 90d2f77c53SPaolo Abeni return false; 910b4f33deSFlorian Westphal } 920b4f33deSFlorian Westphal 9376660afbSPaolo Abeni static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk) 94cec37a6eSPeter Krystad { 95cec37a6eSPeter Krystad sock_owned_by_me((const struct sock *)msk); 96cec37a6eSPeter Krystad 97e1ff9e82SDavide Caratti if (likely(!__mptcp_check_fallback(msk))) 98cec37a6eSPeter Krystad return NULL; 99cec37a6eSPeter Krystad 10076660afbSPaolo Abeni return msk->first; 1018ab183deSPaolo Abeni } 1028ab183deSPaolo Abeni 103fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk) 1042303f994SPeter Krystad { 1052303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1062303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1072303f994SPeter Krystad struct socket *ssock; 1082303f994SPeter Krystad int err; 1092303f994SPeter Krystad 1102303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1112303f994SPeter Krystad if (err) 112fa68018dSPaolo Abeni return err; 1132303f994SPeter Krystad 1148ab183deSPaolo Abeni msk->first = ssock->sk; 1152303f994SPeter Krystad msk->subflow = ssock; 1162303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 117cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 118e16163b6SPaolo Abeni sock_hold(ssock->sk); 1192303f994SPeter Krystad subflow->request_mptcp = 1; 120866f26f2SPaolo Abeni mptcp_sock_graft(msk->first, sk->sk_socket); 121e1ff9e82SDavide Caratti 122fa68018dSPaolo Abeni return 0; 1232303f994SPeter Krystad } 1242303f994SPeter Krystad 125ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 126ab174ad8SPaolo Abeni { 127ab174ad8SPaolo Abeni sk_drops_add(sk, skb); 128ab174ad8SPaolo Abeni __kfree_skb(skb); 129ab174ad8SPaolo Abeni } 130ab174ad8SPaolo Abeni 1318268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 1328268ed4cSPaolo Abeni struct sk_buff *from) 1338268ed4cSPaolo Abeni { 1348268ed4cSPaolo Abeni bool fragstolen; 1358268ed4cSPaolo Abeni int delta; 1368268ed4cSPaolo Abeni 1378268ed4cSPaolo Abeni if (MPTCP_SKB_CB(from)->offset || 1388268ed4cSPaolo Abeni !skb_try_coalesce(to, from, &fragstolen, &delta)) 1398268ed4cSPaolo Abeni return false; 1408268ed4cSPaolo Abeni 14106242e44SPaolo Abeni pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 14206242e44SPaolo Abeni MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 14306242e44SPaolo Abeni to->len, MPTCP_SKB_CB(from)->end_seq); 144ab174ad8SPaolo Abeni MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 1458268ed4cSPaolo Abeni kfree_skb_partial(from, fragstolen); 1468268ed4cSPaolo Abeni atomic_add(delta, &sk->sk_rmem_alloc); 1478268ed4cSPaolo Abeni sk_mem_charge(sk, delta); 1488268ed4cSPaolo Abeni return true; 1498268ed4cSPaolo Abeni } 1508268ed4cSPaolo Abeni 151ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 152ab174ad8SPaolo Abeni struct sk_buff *from) 1536771bfd9SFlorian Westphal { 154ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 155ab174ad8SPaolo Abeni return false; 156ab174ad8SPaolo Abeni 157ab174ad8SPaolo Abeni return mptcp_try_coalesce((struct sock *)msk, to, from); 158ab174ad8SPaolo Abeni } 159ab174ad8SPaolo Abeni 160ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences: 161ab174ad8SPaolo Abeni * - use mptcp seqs 162ab174ad8SPaolo Abeni * - don't cope with sacks 163ab174ad8SPaolo Abeni */ 164ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 165ab174ad8SPaolo Abeni { 166ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 167ab174ad8SPaolo Abeni struct rb_node **p, *parent; 168ab174ad8SPaolo Abeni u64 seq, end_seq, max_seq; 169ab174ad8SPaolo Abeni struct sk_buff *skb1; 170ab174ad8SPaolo Abeni 171ab174ad8SPaolo Abeni seq = MPTCP_SKB_CB(skb)->map_seq; 172ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 173fa3fe2b1SFlorian Westphal max_seq = READ_ONCE(msk->rcv_wnd_sent); 174ab174ad8SPaolo Abeni 17506242e44SPaolo Abeni pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 17606242e44SPaolo Abeni RB_EMPTY_ROOT(&msk->out_of_order_queue)); 177fa3fe2b1SFlorian Westphal if (after64(end_seq, max_seq)) { 178ab174ad8SPaolo Abeni /* out of window */ 179ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 180fa3fe2b1SFlorian Westphal pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 181fa3fe2b1SFlorian Westphal (unsigned long long)end_seq - (unsigned long)max_seq, 182fa3fe2b1SFlorian Westphal (unsigned long long)msk->rcv_wnd_sent); 18306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 184ab174ad8SPaolo Abeni return; 185ab174ad8SPaolo Abeni } 186ab174ad8SPaolo Abeni 187ab174ad8SPaolo Abeni p = &msk->out_of_order_queue.rb_node; 18806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 189ab174ad8SPaolo Abeni if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 190ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, NULL, p); 191ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 192ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 193ab174ad8SPaolo Abeni goto end; 194ab174ad8SPaolo Abeni } 195ab174ad8SPaolo Abeni 196ab174ad8SPaolo Abeni /* with 2 subflows, adding at end of ooo queue is quite likely 197ab174ad8SPaolo Abeni * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 198ab174ad8SPaolo Abeni */ 19906242e44SPaolo Abeni if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 20006242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 20106242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 202ab174ad8SPaolo Abeni return; 20306242e44SPaolo Abeni } 204ab174ad8SPaolo Abeni 205ab174ad8SPaolo Abeni /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 206ab174ad8SPaolo Abeni if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 20706242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 208ab174ad8SPaolo Abeni parent = &msk->ooo_last_skb->rbnode; 209ab174ad8SPaolo Abeni p = &parent->rb_right; 210ab174ad8SPaolo Abeni goto insert; 211ab174ad8SPaolo Abeni } 212ab174ad8SPaolo Abeni 213ab174ad8SPaolo Abeni /* Find place to insert this segment. Handle overlaps on the way. */ 214ab174ad8SPaolo Abeni parent = NULL; 215ab174ad8SPaolo Abeni while (*p) { 216ab174ad8SPaolo Abeni parent = *p; 217ab174ad8SPaolo Abeni skb1 = rb_to_skb(parent); 218ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 219ab174ad8SPaolo Abeni p = &parent->rb_left; 220ab174ad8SPaolo Abeni continue; 221ab174ad8SPaolo Abeni } 222ab174ad8SPaolo Abeni if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 223ab174ad8SPaolo Abeni if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 224ab174ad8SPaolo Abeni /* All the bits are present. Drop. */ 225ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 22606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 227ab174ad8SPaolo Abeni return; 228ab174ad8SPaolo Abeni } 229ab174ad8SPaolo Abeni if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 230ab174ad8SPaolo Abeni /* partial overlap: 231ab174ad8SPaolo Abeni * | skb | 232ab174ad8SPaolo Abeni * | skb1 | 233ab174ad8SPaolo Abeni * continue traversing 234ab174ad8SPaolo Abeni */ 235ab174ad8SPaolo Abeni } else { 236ab174ad8SPaolo Abeni /* skb's seq == skb1's seq and skb covers skb1. 237ab174ad8SPaolo Abeni * Replace skb1 with skb. 238ab174ad8SPaolo Abeni */ 239ab174ad8SPaolo Abeni rb_replace_node(&skb1->rbnode, &skb->rbnode, 240ab174ad8SPaolo Abeni &msk->out_of_order_queue); 241ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 24206242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 243ab174ad8SPaolo Abeni goto merge_right; 244ab174ad8SPaolo Abeni } 245ab174ad8SPaolo Abeni } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 24606242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 247ab174ad8SPaolo Abeni return; 248ab174ad8SPaolo Abeni } 249ab174ad8SPaolo Abeni p = &parent->rb_right; 250ab174ad8SPaolo Abeni } 25106242e44SPaolo Abeni 252ab174ad8SPaolo Abeni insert: 253ab174ad8SPaolo Abeni /* Insert segment into RB tree. */ 254ab174ad8SPaolo Abeni rb_link_node(&skb->rbnode, parent, p); 255ab174ad8SPaolo Abeni rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 256ab174ad8SPaolo Abeni 257ab174ad8SPaolo Abeni merge_right: 258ab174ad8SPaolo Abeni /* Remove other segments covered by skb. */ 259ab174ad8SPaolo Abeni while ((skb1 = skb_rb_next(skb)) != NULL) { 260ab174ad8SPaolo Abeni if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 261ab174ad8SPaolo Abeni break; 262ab174ad8SPaolo Abeni rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 263ab174ad8SPaolo Abeni mptcp_drop(sk, skb1); 26406242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 265ab174ad8SPaolo Abeni } 266ab174ad8SPaolo Abeni /* If there is no skb after us, we are the last_skb ! */ 267ab174ad8SPaolo Abeni if (!skb1) 268ab174ad8SPaolo Abeni msk->ooo_last_skb = skb; 269ab174ad8SPaolo Abeni 270ab174ad8SPaolo Abeni end: 271ab174ad8SPaolo Abeni skb_condense(skb); 272ab174ad8SPaolo Abeni skb_set_owner_r(skb, sk); 273ab174ad8SPaolo Abeni } 274ab174ad8SPaolo Abeni 275ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 276ab174ad8SPaolo Abeni struct sk_buff *skb, unsigned int offset, 277ab174ad8SPaolo Abeni size_t copy_len) 278ab174ad8SPaolo Abeni { 279ab174ad8SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2806771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 2814e637c70SFlorian Westphal struct sk_buff *tail; 2826771bfd9SFlorian Westphal 2836771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 2844e637c70SFlorian Westphal 2854e637c70SFlorian Westphal skb_ext_reset(skb); 2864e637c70SFlorian Westphal skb_orphan(skb); 2874e637c70SFlorian Westphal 2889c3f94e1SPaolo Abeni /* try to fetch required memory from subflow */ 2899c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) { 2909c3f94e1SPaolo Abeni if (ssk->sk_forward_alloc < skb->truesize) 2919c3f94e1SPaolo Abeni goto drop; 2929c3f94e1SPaolo Abeni __sk_mem_reclaim(ssk, skb->truesize); 2939c3f94e1SPaolo Abeni if (!sk_rmem_schedule(sk, skb, skb->truesize)) 2949c3f94e1SPaolo Abeni goto drop; 2959c3f94e1SPaolo Abeni } 2969c3f94e1SPaolo Abeni 297ab174ad8SPaolo Abeni /* the skb map_seq accounts for the skb offset: 298ab174ad8SPaolo Abeni * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 299ab174ad8SPaolo Abeni * value 300ab174ad8SPaolo Abeni */ 301ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 302ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 303ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset = offset; 304ab174ad8SPaolo Abeni 305ab174ad8SPaolo Abeni if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 306ab174ad8SPaolo Abeni /* in sequence */ 307917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 3084e637c70SFlorian Westphal tail = skb_peek_tail(&sk->sk_receive_queue); 3098268ed4cSPaolo Abeni if (tail && mptcp_try_coalesce(sk, tail, skb)) 310ab174ad8SPaolo Abeni return true; 3114e637c70SFlorian Westphal 312600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 3136771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 314ab174ad8SPaolo Abeni return true; 315ab174ad8SPaolo Abeni } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 316ab174ad8SPaolo Abeni mptcp_data_queue_ofo(msk, skb); 317ab174ad8SPaolo Abeni return false; 318ab174ad8SPaolo Abeni } 319ab174ad8SPaolo Abeni 320ab174ad8SPaolo Abeni /* old data, keep it simple and drop the whole pkt, sender 321ab174ad8SPaolo Abeni * will retransmit as needed, if needed. 322ab174ad8SPaolo Abeni */ 32306242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 3249c3f94e1SPaolo Abeni drop: 325ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 326ab174ad8SPaolo Abeni return false; 3276771bfd9SFlorian Westphal } 3286771bfd9SFlorian Westphal 32916a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk) 33016a9a9daSMat Martineau { 33116a9a9daSMat Martineau struct inet_connection_sock *icsk = inet_csk(sk); 33216a9a9daSMat Martineau 33316a9a9daSMat Martineau sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 33416a9a9daSMat Martineau mptcp_sk(sk)->timer_ival = 0; 33516a9a9daSMat Martineau } 33616a9a9daSMat Martineau 337e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk) 338e16163b6SPaolo Abeni { 339e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 340e16163b6SPaolo Abeni return; 341e16163b6SPaolo Abeni 342e16163b6SPaolo Abeni sk->sk_state_change(sk); 343e16163b6SPaolo Abeni if (sk->sk_shutdown == SHUTDOWN_MASK || 344e16163b6SPaolo Abeni sk->sk_state == TCP_CLOSE) 345e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 346e16163b6SPaolo Abeni else 347e16163b6SPaolo Abeni sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 348e16163b6SPaolo Abeni } 349e16163b6SPaolo Abeni 3506e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk) 3516e628cd3SPaolo Abeni { 3526e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 3536e628cd3SPaolo Abeni 3546e628cd3SPaolo Abeni return !__mptcp_check_fallback(msk) && 3556e628cd3SPaolo Abeni ((1 << sk->sk_state) & 3566e628cd3SPaolo Abeni (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 3576e628cd3SPaolo Abeni msk->write_seq == READ_ONCE(msk->snd_una); 3586e628cd3SPaolo Abeni } 3596e628cd3SPaolo Abeni 36016a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk) 36116a9a9daSMat Martineau { 36216a9a9daSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 36316a9a9daSMat Martineau 36416a9a9daSMat Martineau /* Look for an acknowledged DATA_FIN */ 3656e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) { 36616a9a9daSMat Martineau mptcp_stop_timer(sk); 36716a9a9daSMat Martineau 36816a9a9daSMat Martineau WRITE_ONCE(msk->snd_data_fin_enable, 0); 36916a9a9daSMat Martineau 37016a9a9daSMat Martineau switch (sk->sk_state) { 37116a9a9daSMat Martineau case TCP_FIN_WAIT1: 37216a9a9daSMat Martineau inet_sk_state_store(sk, TCP_FIN_WAIT2); 37316a9a9daSMat Martineau break; 37416a9a9daSMat Martineau case TCP_CLOSING: 37516a9a9daSMat Martineau case TCP_LAST_ACK: 37616a9a9daSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 37716a9a9daSMat Martineau break; 37816a9a9daSMat Martineau } 37916a9a9daSMat Martineau 380e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 38116a9a9daSMat Martineau } 38216a9a9daSMat Martineau } 38316a9a9daSMat Martineau 3843721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 3853721b9b6SMat Martineau { 3863721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3873721b9b6SMat Martineau 3883721b9b6SMat Martineau if (READ_ONCE(msk->rcv_data_fin) && 3893721b9b6SMat Martineau ((1 << sk->sk_state) & 3903721b9b6SMat Martineau (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 3913721b9b6SMat Martineau u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 3923721b9b6SMat Martineau 3933721b9b6SMat Martineau if (msk->ack_seq == rcv_data_fin_seq) { 3943721b9b6SMat Martineau if (seq) 3953721b9b6SMat Martineau *seq = rcv_data_fin_seq; 3963721b9b6SMat Martineau 3973721b9b6SMat Martineau return true; 3983721b9b6SMat Martineau } 3993721b9b6SMat Martineau } 4003721b9b6SMat Martineau 4013721b9b6SMat Martineau return false; 4023721b9b6SMat Martineau } 4033721b9b6SMat Martineau 4043721b9b6SMat Martineau static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 4053721b9b6SMat Martineau { 4063721b9b6SMat Martineau long tout = ssk && inet_csk(ssk)->icsk_pending ? 4073721b9b6SMat Martineau inet_csk(ssk)->icsk_timeout - jiffies : 0; 4083721b9b6SMat Martineau 4093721b9b6SMat Martineau if (tout <= 0) 4103721b9b6SMat Martineau tout = mptcp_sk(sk)->timer_ival; 4113721b9b6SMat Martineau mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 4123721b9b6SMat Martineau } 4133721b9b6SMat Martineau 414ea4ca586SPaolo Abeni static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 415ea4ca586SPaolo Abeni { 416ea4ca586SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 417ea4ca586SPaolo Abeni 418ea4ca586SPaolo Abeni /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */ 419ea4ca586SPaolo Abeni if (subflow->request_join && !subflow->fully_established) 420ea4ca586SPaolo Abeni return false; 421ea4ca586SPaolo Abeni 422ea4ca586SPaolo Abeni /* only send if our side has not closed yet */ 423ea4ca586SPaolo Abeni return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)); 424ea4ca586SPaolo Abeni } 425ea4ca586SPaolo Abeni 426fd897679SPaolo Abeni static bool tcp_can_send_ack(const struct sock *ssk) 427fd897679SPaolo Abeni { 428fd897679SPaolo Abeni return !((1 << inet_sk_state_load(ssk)) & 42920bc80b6SPaolo Abeni (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 430fd897679SPaolo Abeni } 431fd897679SPaolo Abeni 432fd897679SPaolo Abeni static void mptcp_send_ack(struct mptcp_sock *msk) 4337ed90803SPaolo Abeni { 4347ed90803SPaolo Abeni struct mptcp_subflow_context *subflow; 4357ed90803SPaolo Abeni 4367ed90803SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 4377ed90803SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 4387ed90803SPaolo Abeni 4397ed90803SPaolo Abeni lock_sock(ssk); 440fd897679SPaolo Abeni if (tcp_can_send_ack(ssk)) 4417ed90803SPaolo Abeni tcp_send_ack(ssk); 4427ed90803SPaolo Abeni release_sock(ssk); 443fd897679SPaolo Abeni } 444ea4ca586SPaolo Abeni } 445ea4ca586SPaolo Abeni 446fd897679SPaolo Abeni static bool mptcp_subflow_cleanup_rbuf(struct sock *ssk) 447fd897679SPaolo Abeni { 448fd897679SPaolo Abeni int ret; 449fd897679SPaolo Abeni 450fd897679SPaolo Abeni lock_sock(ssk); 451fd897679SPaolo Abeni ret = tcp_can_send_ack(ssk); 452fd897679SPaolo Abeni if (ret) 453fd897679SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 454fd897679SPaolo Abeni release_sock(ssk); 455fd897679SPaolo Abeni return ret; 456ea4ca586SPaolo Abeni } 457fd897679SPaolo Abeni 458fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 459fd897679SPaolo Abeni { 46087952603SPaolo Abeni struct sock *ack_hint = READ_ONCE(msk->ack_hint); 461fd897679SPaolo Abeni struct mptcp_subflow_context *subflow; 462fd897679SPaolo Abeni 463fd897679SPaolo Abeni /* if the hinted ssk is still active, try to use it */ 46487952603SPaolo Abeni if (likely(ack_hint)) { 465fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 466fd897679SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 467fd897679SPaolo Abeni 46887952603SPaolo Abeni if (ack_hint == ssk && mptcp_subflow_cleanup_rbuf(ssk)) 469fd897679SPaolo Abeni return; 4707ed90803SPaolo Abeni } 4717ed90803SPaolo Abeni } 4727ed90803SPaolo Abeni 473fd897679SPaolo Abeni /* otherwise pick the first active subflow */ 474fd897679SPaolo Abeni mptcp_for_each_subflow(msk, subflow) 475fd897679SPaolo Abeni if (mptcp_subflow_cleanup_rbuf(mptcp_subflow_tcp_sock(subflow))) 476fd897679SPaolo Abeni return; 477fd897679SPaolo Abeni } 478fd897679SPaolo Abeni 4797ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk) 4803721b9b6SMat Martineau { 4813721b9b6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4823721b9b6SMat Martineau u64 rcv_data_fin_seq; 4837ed90803SPaolo Abeni bool ret = false; 4843721b9b6SMat Martineau 4853721b9b6SMat Martineau if (__mptcp_check_fallback(msk) || !msk->first) 4867ed90803SPaolo Abeni return ret; 4873721b9b6SMat Martineau 4883721b9b6SMat Martineau /* Need to ack a DATA_FIN received from a peer while this side 4893721b9b6SMat Martineau * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 4903721b9b6SMat Martineau * msk->rcv_data_fin was set when parsing the incoming options 4913721b9b6SMat Martineau * at the subflow level and the msk lock was not held, so this 4923721b9b6SMat Martineau * is the first opportunity to act on the DATA_FIN and change 4933721b9b6SMat Martineau * the msk state. 4943721b9b6SMat Martineau * 4953721b9b6SMat Martineau * If we are caught up to the sequence number of the incoming 4963721b9b6SMat Martineau * DATA_FIN, send the DATA_ACK now and do state transition. If 4973721b9b6SMat Martineau * not caught up, do nothing and let the recv code send DATA_ACK 4983721b9b6SMat Martineau * when catching up. 4993721b9b6SMat Martineau */ 5003721b9b6SMat Martineau 5013721b9b6SMat Martineau if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 502917944daSMat Martineau WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 5033721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 0); 5043721b9b6SMat Martineau 5053721b9b6SMat Martineau sk->sk_shutdown |= RCV_SHUTDOWN; 50616a9a9daSMat Martineau smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 50716a9a9daSMat Martineau set_bit(MPTCP_DATA_READY, &msk->flags); 5083721b9b6SMat Martineau 5093721b9b6SMat Martineau switch (sk->sk_state) { 5103721b9b6SMat Martineau case TCP_ESTABLISHED: 5113721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE_WAIT); 5123721b9b6SMat Martineau break; 5133721b9b6SMat Martineau case TCP_FIN_WAIT1: 5143721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSING); 5153721b9b6SMat Martineau break; 5163721b9b6SMat Martineau case TCP_FIN_WAIT2: 5173721b9b6SMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 5183721b9b6SMat Martineau break; 5193721b9b6SMat Martineau default: 5203721b9b6SMat Martineau /* Other states not expected */ 5213721b9b6SMat Martineau WARN_ON_ONCE(1); 5223721b9b6SMat Martineau break; 5233721b9b6SMat Martineau } 5243721b9b6SMat Martineau 5257ed90803SPaolo Abeni ret = true; 5263721b9b6SMat Martineau mptcp_set_timeout(sk, NULL); 527fd897679SPaolo Abeni mptcp_send_ack(msk); 528e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 5293721b9b6SMat Martineau } 5307ed90803SPaolo Abeni return ret; 5313721b9b6SMat Martineau } 5323721b9b6SMat Martineau 5336771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 5346771bfd9SFlorian Westphal struct sock *ssk, 5356771bfd9SFlorian Westphal unsigned int *bytes) 5366771bfd9SFlorian Westphal { 5376771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 538600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 5396771bfd9SFlorian Westphal unsigned int moved = 0; 5406771bfd9SFlorian Westphal bool more_data_avail; 5416771bfd9SFlorian Westphal struct tcp_sock *tp; 5426771bfd9SFlorian Westphal bool done = false; 54313c7ba0cSFlorian Westphal int sk_rbuf; 54413c7ba0cSFlorian Westphal 54513c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 54613c7ba0cSFlorian Westphal 54713c7ba0cSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 54813c7ba0cSFlorian Westphal int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 54913c7ba0cSFlorian Westphal 55013c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) { 55113c7ba0cSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 55213c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 55313c7ba0cSFlorian Westphal } 55413c7ba0cSFlorian Westphal } 555600911ffSFlorian Westphal 556ab174ad8SPaolo Abeni pr_debug("msk=%p ssk=%p", msk, ssk); 5576771bfd9SFlorian Westphal tp = tcp_sk(ssk); 5586771bfd9SFlorian Westphal do { 5596771bfd9SFlorian Westphal u32 map_remaining, offset; 5606771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 5616771bfd9SFlorian Westphal struct sk_buff *skb; 5626771bfd9SFlorian Westphal bool fin; 5636771bfd9SFlorian Westphal 5646771bfd9SFlorian Westphal /* try to move as much data as available */ 5656771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 5666771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 5676771bfd9SFlorian Westphal 5686771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 569d9fb8c50SPaolo Abeni if (!skb) { 570d9fb8c50SPaolo Abeni /* if no data is found, a racing workqueue/recvmsg 571d9fb8c50SPaolo Abeni * already processed the new data, stop here or we 572d9fb8c50SPaolo Abeni * can enter an infinite loop 573d9fb8c50SPaolo Abeni */ 574d9fb8c50SPaolo Abeni if (!moved) 575d9fb8c50SPaolo Abeni done = true; 5766771bfd9SFlorian Westphal break; 577d9fb8c50SPaolo Abeni } 5786771bfd9SFlorian Westphal 579e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) { 580e1ff9e82SDavide Caratti /* if we are running under the workqueue, TCP could have 581e1ff9e82SDavide Caratti * collapsed skbs between dummy map creation and now 582e1ff9e82SDavide Caratti * be sure to adjust the size 583e1ff9e82SDavide Caratti */ 584e1ff9e82SDavide Caratti map_remaining = skb->len; 585e1ff9e82SDavide Caratti subflow->map_data_len = skb->len; 586e1ff9e82SDavide Caratti } 587e1ff9e82SDavide Caratti 5886771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 5896771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 5906771bfd9SFlorian Westphal if (fin) { 5916771bfd9SFlorian Westphal done = true; 5926771bfd9SFlorian Westphal seq++; 5936771bfd9SFlorian Westphal } 5946771bfd9SFlorian Westphal 5956771bfd9SFlorian Westphal if (offset < skb->len) { 5966771bfd9SFlorian Westphal size_t len = skb->len - offset; 5976771bfd9SFlorian Westphal 5986771bfd9SFlorian Westphal if (tp->urg_data) 5996771bfd9SFlorian Westphal done = true; 6006771bfd9SFlorian Westphal 601ab174ad8SPaolo Abeni if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 6026771bfd9SFlorian Westphal moved += len; 603ab174ad8SPaolo Abeni seq += len; 6046771bfd9SFlorian Westphal 6056771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 6066771bfd9SFlorian Westphal break; 6076771bfd9SFlorian Westphal } else { 6086771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 6096771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 6106771bfd9SFlorian Westphal done = true; 6116771bfd9SFlorian Westphal } 6126771bfd9SFlorian Westphal 6136771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 6146771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 615600911ffSFlorian Westphal 61613c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 617600911ffSFlorian Westphal done = true; 618600911ffSFlorian Westphal break; 619600911ffSFlorian Westphal } 6206771bfd9SFlorian Westphal } while (more_data_avail); 62187952603SPaolo Abeni WRITE_ONCE(msk->ack_hint, ssk); 6226771bfd9SFlorian Westphal 6236719331cSPaolo Abeni *bytes += moved; 6246771bfd9SFlorian Westphal return done; 6256771bfd9SFlorian Westphal } 6266771bfd9SFlorian Westphal 62787952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 628ab174ad8SPaolo Abeni { 629ab174ad8SPaolo Abeni struct sock *sk = (struct sock *)msk; 630ab174ad8SPaolo Abeni struct sk_buff *skb, *tail; 631ab174ad8SPaolo Abeni bool moved = false; 632ab174ad8SPaolo Abeni struct rb_node *p; 633ab174ad8SPaolo Abeni u64 end_seq; 634ab174ad8SPaolo Abeni 635ab174ad8SPaolo Abeni p = rb_first(&msk->out_of_order_queue); 63606242e44SPaolo Abeni pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 637ab174ad8SPaolo Abeni while (p) { 638ab174ad8SPaolo Abeni skb = rb_to_skb(p); 639ab174ad8SPaolo Abeni if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 640ab174ad8SPaolo Abeni break; 641ab174ad8SPaolo Abeni 642ab174ad8SPaolo Abeni p = rb_next(p); 643ab174ad8SPaolo Abeni rb_erase(&skb->rbnode, &msk->out_of_order_queue); 644ab174ad8SPaolo Abeni 645ab174ad8SPaolo Abeni if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 646ab174ad8SPaolo Abeni msk->ack_seq))) { 647ab174ad8SPaolo Abeni mptcp_drop(sk, skb); 64806242e44SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 649ab174ad8SPaolo Abeni continue; 650ab174ad8SPaolo Abeni } 651ab174ad8SPaolo Abeni 652ab174ad8SPaolo Abeni end_seq = MPTCP_SKB_CB(skb)->end_seq; 653ab174ad8SPaolo Abeni tail = skb_peek_tail(&sk->sk_receive_queue); 654ab174ad8SPaolo Abeni if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 655ab174ad8SPaolo Abeni int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 656ab174ad8SPaolo Abeni 657ab174ad8SPaolo Abeni /* skip overlapping data, if any */ 65806242e44SPaolo Abeni pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 65906242e44SPaolo Abeni MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 66006242e44SPaolo Abeni delta); 661ab174ad8SPaolo Abeni MPTCP_SKB_CB(skb)->offset += delta; 662ab174ad8SPaolo Abeni __skb_queue_tail(&sk->sk_receive_queue, skb); 663ab174ad8SPaolo Abeni } 664ab174ad8SPaolo Abeni msk->ack_seq = end_seq; 665ab174ad8SPaolo Abeni moved = true; 666ab174ad8SPaolo Abeni } 667ab174ad8SPaolo Abeni return moved; 668ab174ad8SPaolo Abeni } 669ab174ad8SPaolo Abeni 6702e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 6712e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 6722e52213cSFlorian Westphal */ 67387952603SPaolo Abeni static void move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 6742e52213cSFlorian Westphal { 6752e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 6762e52213cSFlorian Westphal unsigned int moved = 0; 6772e52213cSFlorian Westphal 67887952603SPaolo Abeni if (inet_sk_state_load(sk) == TCP_CLOSE) 67987952603SPaolo Abeni return; 6802e52213cSFlorian Westphal 68187952603SPaolo Abeni mptcp_data_lock(sk); 6822e52213cSFlorian Westphal 6832e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 68487952603SPaolo Abeni __mptcp_ofo_queue(msk); 685ab174ad8SPaolo Abeni 686ab174ad8SPaolo Abeni /* If the moves have caught up with the DATA_FIN sequence number 687ab174ad8SPaolo Abeni * it's time to ack the DATA_FIN and change socket state, but 688ab174ad8SPaolo Abeni * this is not a good place to change state. Let the workqueue 689ab174ad8SPaolo Abeni * do it. 690ab174ad8SPaolo Abeni */ 691ba8f48f7SPaolo Abeni if (mptcp_pending_data_fin(sk, NULL)) 692ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 69387952603SPaolo Abeni mptcp_data_unlock(sk); 6942e52213cSFlorian Westphal } 6952e52213cSFlorian Westphal 6962e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 697101f6f85SFlorian Westphal { 6986719331cSPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 699101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 70013c7ba0cSFlorian Westphal int sk_rbuf, ssk_rbuf; 7016719331cSPaolo Abeni bool wake; 702101f6f85SFlorian Westphal 703d7b1bfd0SPaolo Abeni /* The peer can send data while we are shutting down this 704d7b1bfd0SPaolo Abeni * subflow at msk destruction time, but we must avoid enqueuing 705d7b1bfd0SPaolo Abeni * more data to the msk receive queue 706d7b1bfd0SPaolo Abeni */ 707d7b1bfd0SPaolo Abeni if (unlikely(subflow->disposable)) 708d7b1bfd0SPaolo Abeni return; 709d7b1bfd0SPaolo Abeni 7106719331cSPaolo Abeni /* move_skbs_to_msk below can legitly clear the data_avail flag, 7116719331cSPaolo Abeni * but we will need later to properly woke the reader, cache its 7126719331cSPaolo Abeni * value 7136719331cSPaolo Abeni */ 7146719331cSPaolo Abeni wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL; 7156719331cSPaolo Abeni if (wake) 716101f6f85SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 7176771bfd9SFlorian Westphal 71813c7ba0cSFlorian Westphal ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 71913c7ba0cSFlorian Westphal sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 72013c7ba0cSFlorian Westphal if (unlikely(ssk_rbuf > sk_rbuf)) 72113c7ba0cSFlorian Westphal sk_rbuf = ssk_rbuf; 72213c7ba0cSFlorian Westphal 72313c7ba0cSFlorian Westphal /* over limit? can't append more skbs to msk */ 72413c7ba0cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) 7252e52213cSFlorian Westphal goto wake; 7262e52213cSFlorian Westphal 72714c441b5SPaolo Abeni move_skbs_to_msk(msk, ssk); 728ea4ca586SPaolo Abeni 729600911ffSFlorian Westphal wake: 7306719331cSPaolo Abeni if (wake) 731101f6f85SFlorian Westphal sk->sk_data_ready(sk); 732101f6f85SFlorian Westphal } 733101f6f85SFlorian Westphal 73484dfe367SGeliang Tang void __mptcp_flush_join_list(struct mptcp_sock *msk) 735ec3edaa7SPeter Krystad { 7365cf92bbaSPaolo Abeni struct mptcp_subflow_context *subflow; 7375cf92bbaSPaolo Abeni 738ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 739ec3edaa7SPeter Krystad return; 740ec3edaa7SPeter Krystad 741ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 7425cf92bbaSPaolo Abeni list_for_each_entry(subflow, &msk->join_list, node) 7435cf92bbaSPaolo Abeni mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow)); 744ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 745ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 746ec3edaa7SPeter Krystad } 747ec3edaa7SPeter Krystad 748b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 749b51f9b80SPaolo Abeni { 750b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 751b51f9b80SPaolo Abeni } 752b51f9b80SPaolo Abeni 753b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 754b51f9b80SPaolo Abeni { 755b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 756b51f9b80SPaolo Abeni unsigned long tout; 757b51f9b80SPaolo Abeni 758e16163b6SPaolo Abeni /* prevent rescheduling on close */ 759e16163b6SPaolo Abeni if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 760e16163b6SPaolo Abeni return; 761e16163b6SPaolo Abeni 762b51f9b80SPaolo Abeni /* should never be called with mptcp level timer cleared */ 763b51f9b80SPaolo Abeni tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 764b51f9b80SPaolo Abeni if (WARN_ON_ONCE(!tout)) 765b51f9b80SPaolo Abeni tout = TCP_RTO_MIN; 766b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 767b51f9b80SPaolo Abeni } 768b51f9b80SPaolo Abeni 769ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk) 770ba8f48f7SPaolo Abeni { 771ba8f48f7SPaolo Abeni if (inet_sk_state_load(sk) != TCP_CLOSE && 772ba8f48f7SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) { 773ba8f48f7SPaolo Abeni /* each subflow already holds a reference to the sk, and the 774ba8f48f7SPaolo Abeni * workqueue is invoked by a subflow, so sk can't go away here. 775ba8f48f7SPaolo Abeni */ 776ba8f48f7SPaolo Abeni sock_hold(sk); 777ba8f48f7SPaolo Abeni return true; 778ba8f48f7SPaolo Abeni } 779ba8f48f7SPaolo Abeni return false; 780ba8f48f7SPaolo Abeni } 781ba8f48f7SPaolo Abeni 78259832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 78359832e24SFlorian Westphal { 784ba8f48f7SPaolo Abeni if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 785ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 78659832e24SFlorian Westphal } 78759832e24SFlorian Westphal 7885969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk) 7895969856aSPaolo Abeni { 7905969856aSPaolo Abeni struct mptcp_subflow_context *subflow; 7915969856aSPaolo Abeni struct sock *sk = (struct sock *)msk; 7925969856aSPaolo Abeni int receivers = 0; 7935969856aSPaolo Abeni 7945969856aSPaolo Abeni mptcp_for_each_subflow(msk, subflow) 7955969856aSPaolo Abeni receivers += !subflow->rx_eof; 796e16163b6SPaolo Abeni if (receivers) 797e16163b6SPaolo Abeni return; 7985969856aSPaolo Abeni 799e16163b6SPaolo Abeni if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 8005969856aSPaolo Abeni /* hopefully temporary hack: propagate shutdown status 8015969856aSPaolo Abeni * to msk, when all subflows agree on it 8025969856aSPaolo Abeni */ 8035969856aSPaolo Abeni sk->sk_shutdown |= RCV_SHUTDOWN; 8045969856aSPaolo Abeni 8055969856aSPaolo Abeni smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 8065969856aSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 8075969856aSPaolo Abeni sk->sk_data_ready(sk); 8085969856aSPaolo Abeni } 809e16163b6SPaolo Abeni 810e16163b6SPaolo Abeni switch (sk->sk_state) { 811e16163b6SPaolo Abeni case TCP_ESTABLISHED: 812e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE_WAIT); 813e16163b6SPaolo Abeni break; 814e16163b6SPaolo Abeni case TCP_FIN_WAIT1: 81526aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSING); 81626aa2314SPaolo Abeni break; 81726aa2314SPaolo Abeni case TCP_FIN_WAIT2: 818e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 819e16163b6SPaolo Abeni break; 820e16163b6SPaolo Abeni default: 821e16163b6SPaolo Abeni return; 822e16163b6SPaolo Abeni } 823e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 8245969856aSPaolo Abeni } 8255969856aSPaolo Abeni 8267a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 8277a6a6cbcSPaolo Abeni { 8287a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 8297a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 8307a6a6cbcSPaolo Abeni 8317a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 8327a6a6cbcSPaolo Abeni 8337a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 8347a6a6cbcSPaolo Abeni if (subflow->data_avail) 8357a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 8367a6a6cbcSPaolo Abeni } 8377a6a6cbcSPaolo Abeni 8387a6a6cbcSPaolo Abeni return NULL; 8397a6a6cbcSPaolo Abeni } 8407a6a6cbcSPaolo Abeni 8413f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 84257040755SPaolo Abeni const struct sk_buff *skb, 84357040755SPaolo Abeni const struct mptcp_ext *mpext) 8446d0060f6SMat Martineau { 84557040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 84657040755SPaolo Abeni return false; 84757040755SPaolo Abeni 8485a369ca6SPaolo Abeni /* can collapse only if MPTCP level sequence is in order and this 8495a369ca6SPaolo Abeni * mapping has not been xmitted yet 8505a369ca6SPaolo Abeni */ 8515a369ca6SPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq && 8525a369ca6SPaolo Abeni !mpext->frozen; 85357040755SPaolo Abeni } 85457040755SPaolo Abeni 85518b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 85618b683bfSPaolo Abeni const struct page_frag *pfrag, 85718b683bfSPaolo Abeni const struct mptcp_data_frag *df) 85818b683bfSPaolo Abeni { 85918b683bfSPaolo Abeni return df && pfrag->page == df->page && 860d9ca1de8SPaolo Abeni pfrag->size - pfrag->offset > 0 && 86118b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 86218b683bfSPaolo Abeni } 86318b683bfSPaolo Abeni 864724cfd2eSPaolo Abeni static int mptcp_wmem_with_overhead(struct sock *sk, int size) 865e93da928SPaolo Abeni { 866724cfd2eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 867724cfd2eSPaolo Abeni int ret, skbs; 868724cfd2eSPaolo Abeni 869724cfd2eSPaolo Abeni ret = size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT); 870724cfd2eSPaolo Abeni skbs = (msk->tx_pending_data + size) / msk->size_goal_cache; 871724cfd2eSPaolo Abeni if (skbs < msk->skb_tx_cache.qlen) 872724cfd2eSPaolo Abeni return ret; 873724cfd2eSPaolo Abeni 874724cfd2eSPaolo Abeni return ret + (skbs - msk->skb_tx_cache.qlen) * SKB_TRUESIZE(MAX_TCP_HEADER); 875e93da928SPaolo Abeni } 876e93da928SPaolo Abeni 877e93da928SPaolo Abeni static void __mptcp_wmem_reserve(struct sock *sk, int size) 878e93da928SPaolo Abeni { 879724cfd2eSPaolo Abeni int amount = mptcp_wmem_with_overhead(sk, size); 880e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 881e93da928SPaolo Abeni 882e93da928SPaolo Abeni WARN_ON_ONCE(msk->wmem_reserved); 883e7579d5dSDavide Caratti if (WARN_ON_ONCE(amount < 0)) 884e7579d5dSDavide Caratti amount = 0; 885e7579d5dSDavide Caratti 886e93da928SPaolo Abeni if (amount <= sk->sk_forward_alloc) 887e93da928SPaolo Abeni goto reserve; 888e93da928SPaolo Abeni 889e93da928SPaolo Abeni /* under memory pressure try to reserve at most a single page 890e93da928SPaolo Abeni * otherwise try to reserve the full estimate and fallback 891e93da928SPaolo Abeni * to a single page before entering the error path 892e93da928SPaolo Abeni */ 893e93da928SPaolo Abeni if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) || 894e93da928SPaolo Abeni !sk_wmem_schedule(sk, amount)) { 895e93da928SPaolo Abeni if (amount <= PAGE_SIZE) 896e93da928SPaolo Abeni goto nomem; 897e93da928SPaolo Abeni 898e93da928SPaolo Abeni amount = PAGE_SIZE; 899e93da928SPaolo Abeni if (!sk_wmem_schedule(sk, amount)) 900e93da928SPaolo Abeni goto nomem; 901e93da928SPaolo Abeni } 902e93da928SPaolo Abeni 903e93da928SPaolo Abeni reserve: 904e93da928SPaolo Abeni msk->wmem_reserved = amount; 905e93da928SPaolo Abeni sk->sk_forward_alloc -= amount; 906e93da928SPaolo Abeni return; 907e93da928SPaolo Abeni 908e93da928SPaolo Abeni nomem: 909e93da928SPaolo Abeni /* we will wait for memory on next allocation */ 910e93da928SPaolo Abeni msk->wmem_reserved = -1; 911e93da928SPaolo Abeni } 912e93da928SPaolo Abeni 913e93da928SPaolo Abeni static void __mptcp_update_wmem(struct sock *sk) 914e93da928SPaolo Abeni { 915e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 916e93da928SPaolo Abeni 917e93da928SPaolo Abeni if (!msk->wmem_reserved) 918e93da928SPaolo Abeni return; 919e93da928SPaolo Abeni 920e93da928SPaolo Abeni if (msk->wmem_reserved < 0) 921e93da928SPaolo Abeni msk->wmem_reserved = 0; 922e93da928SPaolo Abeni if (msk->wmem_reserved > 0) { 923e93da928SPaolo Abeni sk->sk_forward_alloc += msk->wmem_reserved; 924e93da928SPaolo Abeni msk->wmem_reserved = 0; 925e93da928SPaolo Abeni } 926e93da928SPaolo Abeni } 927e93da928SPaolo Abeni 928e93da928SPaolo Abeni static bool mptcp_wmem_alloc(struct sock *sk, int size) 929e93da928SPaolo Abeni { 930e93da928SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 931e93da928SPaolo Abeni 932e93da928SPaolo Abeni /* check for pre-existing error condition */ 933e93da928SPaolo Abeni if (msk->wmem_reserved < 0) 934e93da928SPaolo Abeni return false; 935e93da928SPaolo Abeni 936e93da928SPaolo Abeni if (msk->wmem_reserved >= size) 937e93da928SPaolo Abeni goto account; 938e93da928SPaolo Abeni 93987952603SPaolo Abeni mptcp_data_lock(sk); 94087952603SPaolo Abeni if (!sk_wmem_schedule(sk, size)) { 94187952603SPaolo Abeni mptcp_data_unlock(sk); 942e93da928SPaolo Abeni return false; 94387952603SPaolo Abeni } 944e93da928SPaolo Abeni 945e93da928SPaolo Abeni sk->sk_forward_alloc -= size; 946e93da928SPaolo Abeni msk->wmem_reserved += size; 94787952603SPaolo Abeni mptcp_data_unlock(sk); 948e93da928SPaolo Abeni 949e93da928SPaolo Abeni account: 950e93da928SPaolo Abeni msk->wmem_reserved -= size; 951e93da928SPaolo Abeni return true; 952e93da928SPaolo Abeni } 953e93da928SPaolo Abeni 95487952603SPaolo Abeni static void mptcp_wmem_uncharge(struct sock *sk, int size) 95587952603SPaolo Abeni { 95687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 95787952603SPaolo Abeni 95887952603SPaolo Abeni if (msk->wmem_reserved < 0) 95987952603SPaolo Abeni msk->wmem_reserved = 0; 96087952603SPaolo Abeni msk->wmem_reserved += size; 96187952603SPaolo Abeni } 96287952603SPaolo Abeni 963724cfd2eSPaolo Abeni static void mptcp_mem_reclaim_partial(struct sock *sk) 964724cfd2eSPaolo Abeni { 965724cfd2eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 966724cfd2eSPaolo Abeni 967724cfd2eSPaolo Abeni /* if we are experiencing a transint allocation error, 968724cfd2eSPaolo Abeni * the forward allocation memory has been already 969724cfd2eSPaolo Abeni * released 970724cfd2eSPaolo Abeni */ 971724cfd2eSPaolo Abeni if (msk->wmem_reserved < 0) 972724cfd2eSPaolo Abeni return; 973724cfd2eSPaolo Abeni 974724cfd2eSPaolo Abeni mptcp_data_lock(sk); 975724cfd2eSPaolo Abeni sk->sk_forward_alloc += msk->wmem_reserved; 976724cfd2eSPaolo Abeni sk_mem_reclaim_partial(sk); 977724cfd2eSPaolo Abeni msk->wmem_reserved = sk->sk_forward_alloc; 978724cfd2eSPaolo Abeni sk->sk_forward_alloc = 0; 979724cfd2eSPaolo Abeni mptcp_data_unlock(sk); 980724cfd2eSPaolo Abeni } 981724cfd2eSPaolo Abeni 982d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 98318b683bfSPaolo Abeni { 984d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 9857948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 986d027236cSPaolo Abeni } 987d027236cSPaolo Abeni 988d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 989d027236cSPaolo Abeni { 990d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 991d027236cSPaolo Abeni 99218b683bfSPaolo Abeni list_del(&dfrag->list); 993d027236cSPaolo Abeni dfrag_uncharge(sk, len); 99418b683bfSPaolo Abeni put_page(dfrag->page); 99518b683bfSPaolo Abeni } 99618b683bfSPaolo Abeni 9976e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk) 99818b683bfSPaolo Abeni { 99918b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 100018b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 1001d027236cSPaolo Abeni bool cleaned = false; 1002e1ff9e82SDavide Caratti u64 snd_una; 1003e1ff9e82SDavide Caratti 1004e1ff9e82SDavide Caratti /* on fallback we just need to ignore snd_una, as this is really 1005e1ff9e82SDavide Caratti * plain TCP 1006e1ff9e82SDavide Caratti */ 1007e1ff9e82SDavide Caratti if (__mptcp_check_fallback(msk)) 10087439d687SPaolo Abeni msk->snd_una = READ_ONCE(msk->snd_nxt); 10097439d687SPaolo Abeni 10107439d687SPaolo Abeni snd_una = msk->snd_una; 101118b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 101218b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 101318b683bfSPaolo Abeni break; 101418b683bfSPaolo Abeni 1015d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(dfrag == msk->first_pending)) 1016d9ca1de8SPaolo Abeni break; 1017d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1018d027236cSPaolo Abeni cleaned = true; 1019d027236cSPaolo Abeni } 1020d027236cSPaolo Abeni 10217948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 10227948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 102353eb4c38SPaolo Abeni u64 delta = snd_una - dfrag->data_seq; 102453eb4c38SPaolo Abeni 1025d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(delta > dfrag->already_sent)) 102653eb4c38SPaolo Abeni goto out; 10277948f6ccSFlorian Westphal 10287948f6ccSFlorian Westphal dfrag->data_seq += delta; 102953eb4c38SPaolo Abeni dfrag->offset += delta; 10307948f6ccSFlorian Westphal dfrag->data_len -= delta; 1031d9ca1de8SPaolo Abeni dfrag->already_sent -= delta; 10327948f6ccSFlorian Westphal 10337948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 10347948f6ccSFlorian Westphal cleaned = true; 10357948f6ccSFlorian Westphal } 10367948f6ccSFlorian Westphal 103753eb4c38SPaolo Abeni out: 10386e628cd3SPaolo Abeni if (cleaned) { 10396e628cd3SPaolo Abeni if (tcp_under_memory_pressure(sk)) { 10406e628cd3SPaolo Abeni __mptcp_update_wmem(sk); 1041d027236cSPaolo Abeni sk_mem_reclaim_partial(sk); 104295ed690eSFlorian Westphal } 104318b683bfSPaolo Abeni } 104418b683bfSPaolo Abeni 10456e628cd3SPaolo Abeni if (snd_una == READ_ONCE(msk->snd_nxt)) { 10466e628cd3SPaolo Abeni if (msk->timer_ival) 10476e628cd3SPaolo Abeni mptcp_stop_timer(sk); 10486e628cd3SPaolo Abeni } else { 10496e628cd3SPaolo Abeni mptcp_reset_timer(sk); 10506e628cd3SPaolo Abeni } 10516e628cd3SPaolo Abeni } 10526e628cd3SPaolo Abeni 1053724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk) 105418b683bfSPaolo Abeni { 1055d9ca1de8SPaolo Abeni struct mptcp_subflow_context *subflow; 1056d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1057d9ca1de8SPaolo Abeni bool first = true; 1058d9ca1de8SPaolo Abeni 105918b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 1060d9ca1de8SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1061d9ca1de8SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1062d9ca1de8SPaolo Abeni 1063d9ca1de8SPaolo Abeni if (first) 1064d9ca1de8SPaolo Abeni tcp_enter_memory_pressure(ssk); 1065d9ca1de8SPaolo Abeni sk_stream_moderate_sndbuf(ssk); 1066d9ca1de8SPaolo Abeni first = false; 1067d9ca1de8SPaolo Abeni } 1068724cfd2eSPaolo Abeni } 1069724cfd2eSPaolo Abeni 1070724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1071724cfd2eSPaolo Abeni * data 1072724cfd2eSPaolo Abeni */ 1073724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1074724cfd2eSPaolo Abeni { 1075724cfd2eSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1076724cfd2eSPaolo Abeni pfrag, sk->sk_allocation))) 1077724cfd2eSPaolo Abeni return true; 1078724cfd2eSPaolo Abeni 1079724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 108018b683bfSPaolo Abeni return false; 108118b683bfSPaolo Abeni } 108218b683bfSPaolo Abeni 108318b683bfSPaolo Abeni static struct mptcp_data_frag * 108418b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 108518b683bfSPaolo Abeni int orig_offset) 108618b683bfSPaolo Abeni { 108718b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 108818b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 108918b683bfSPaolo Abeni 109018b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 109118b683bfSPaolo Abeni dfrag->data_len = 0; 109218b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 109318b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 109418b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1095d9ca1de8SPaolo Abeni dfrag->already_sent = 0; 109618b683bfSPaolo Abeni dfrag->page = pfrag->page; 109718b683bfSPaolo Abeni 109818b683bfSPaolo Abeni return dfrag; 109918b683bfSPaolo Abeni } 110018b683bfSPaolo Abeni 1101caf971dfSPaolo Abeni struct mptcp_sendmsg_info { 1102caf971dfSPaolo Abeni int mss_now; 1103caf971dfSPaolo Abeni int size_goal; 1104d9ca1de8SPaolo Abeni u16 limit; 1105d9ca1de8SPaolo Abeni u16 sent; 1106d9ca1de8SPaolo Abeni unsigned int flags; 1107caf971dfSPaolo Abeni }; 1108caf971dfSPaolo Abeni 11096f8a612aSFlorian Westphal static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq, 11106f8a612aSFlorian Westphal int avail_size) 11116f8a612aSFlorian Westphal { 11126f8a612aSFlorian Westphal u64 window_end = mptcp_wnd_end(msk); 11136f8a612aSFlorian Westphal 11146f8a612aSFlorian Westphal if (__mptcp_check_fallback(msk)) 11156f8a612aSFlorian Westphal return avail_size; 11166f8a612aSFlorian Westphal 11176f8a612aSFlorian Westphal if (!before64(data_seq + avail_size, window_end)) { 11186f8a612aSFlorian Westphal u64 allowed_size = window_end - data_seq; 11196f8a612aSFlorian Westphal 11206f8a612aSFlorian Westphal return min_t(unsigned int, allowed_size, avail_size); 11216f8a612aSFlorian Westphal } 11226f8a612aSFlorian Westphal 11236f8a612aSFlorian Westphal return avail_size; 11246f8a612aSFlorian Westphal } 11256f8a612aSFlorian Westphal 1126724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1127724cfd2eSPaolo Abeni { 1128724cfd2eSPaolo Abeni struct skb_ext *mpext = __skb_ext_alloc(gfp); 1129724cfd2eSPaolo Abeni 1130724cfd2eSPaolo Abeni if (!mpext) 1131724cfd2eSPaolo Abeni return false; 1132724cfd2eSPaolo Abeni __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1133724cfd2eSPaolo Abeni return true; 1134724cfd2eSPaolo Abeni } 1135724cfd2eSPaolo Abeni 11366e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1137724cfd2eSPaolo Abeni { 1138724cfd2eSPaolo Abeni struct sk_buff *skb; 1139724cfd2eSPaolo Abeni 11406e628cd3SPaolo Abeni skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1141724cfd2eSPaolo Abeni if (likely(skb)) { 11426e628cd3SPaolo Abeni if (likely(__mptcp_add_ext(skb, gfp))) { 1143724cfd2eSPaolo Abeni skb_reserve(skb, MAX_TCP_HEADER); 1144724cfd2eSPaolo Abeni skb->reserved_tailroom = skb->end - skb->tail; 1145724cfd2eSPaolo Abeni return skb; 1146724cfd2eSPaolo Abeni } 1147724cfd2eSPaolo Abeni __kfree_skb(skb); 1148724cfd2eSPaolo Abeni } else { 1149724cfd2eSPaolo Abeni mptcp_enter_memory_pressure(sk); 1150724cfd2eSPaolo Abeni } 1151724cfd2eSPaolo Abeni return NULL; 1152724cfd2eSPaolo Abeni } 1153724cfd2eSPaolo Abeni 1154724cfd2eSPaolo Abeni static bool mptcp_tx_cache_refill(struct sock *sk, int size, 1155724cfd2eSPaolo Abeni struct sk_buff_head *skbs, int *total_ts) 1156724cfd2eSPaolo Abeni { 1157724cfd2eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1158724cfd2eSPaolo Abeni struct sk_buff *skb; 1159724cfd2eSPaolo Abeni int space_needed; 1160724cfd2eSPaolo Abeni 1161724cfd2eSPaolo Abeni if (unlikely(tcp_under_memory_pressure(sk))) { 1162724cfd2eSPaolo Abeni mptcp_mem_reclaim_partial(sk); 1163724cfd2eSPaolo Abeni 1164724cfd2eSPaolo Abeni /* under pressure pre-allocate at most a single skb */ 1165724cfd2eSPaolo Abeni if (msk->skb_tx_cache.qlen) 1166724cfd2eSPaolo Abeni return true; 1167724cfd2eSPaolo Abeni space_needed = msk->size_goal_cache; 1168724cfd2eSPaolo Abeni } else { 1169724cfd2eSPaolo Abeni space_needed = msk->tx_pending_data + size - 1170724cfd2eSPaolo Abeni msk->skb_tx_cache.qlen * msk->size_goal_cache; 1171724cfd2eSPaolo Abeni } 1172724cfd2eSPaolo Abeni 1173724cfd2eSPaolo Abeni while (space_needed > 0) { 11746e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, sk->sk_allocation); 1175724cfd2eSPaolo Abeni if (unlikely(!skb)) { 1176724cfd2eSPaolo Abeni /* under memory pressure, try to pass the caller a 1177724cfd2eSPaolo Abeni * single skb to allow forward progress 1178724cfd2eSPaolo Abeni */ 1179724cfd2eSPaolo Abeni while (skbs->qlen > 1) { 1180724cfd2eSPaolo Abeni skb = __skb_dequeue_tail(skbs); 1181724cfd2eSPaolo Abeni __kfree_skb(skb); 1182724cfd2eSPaolo Abeni } 1183724cfd2eSPaolo Abeni return skbs->qlen > 0; 1184724cfd2eSPaolo Abeni } 1185724cfd2eSPaolo Abeni 1186724cfd2eSPaolo Abeni *total_ts += skb->truesize; 1187724cfd2eSPaolo Abeni __skb_queue_tail(skbs, skb); 1188724cfd2eSPaolo Abeni space_needed -= msk->size_goal_cache; 1189724cfd2eSPaolo Abeni } 1190724cfd2eSPaolo Abeni return true; 1191724cfd2eSPaolo Abeni } 1192724cfd2eSPaolo Abeni 11936e628cd3SPaolo Abeni static bool __mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1194724cfd2eSPaolo Abeni { 1195724cfd2eSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1196724cfd2eSPaolo Abeni struct sk_buff *skb; 1197724cfd2eSPaolo Abeni 1198724cfd2eSPaolo Abeni if (ssk->sk_tx_skb_cache) { 1199724cfd2eSPaolo Abeni skb = ssk->sk_tx_skb_cache; 1200724cfd2eSPaolo Abeni if (unlikely(!skb_ext_find(skb, SKB_EXT_MPTCP) && 12016e628cd3SPaolo Abeni !__mptcp_add_ext(skb, gfp))) 1202724cfd2eSPaolo Abeni return false; 1203724cfd2eSPaolo Abeni return true; 1204724cfd2eSPaolo Abeni } 1205724cfd2eSPaolo Abeni 1206724cfd2eSPaolo Abeni skb = skb_peek(&msk->skb_tx_cache); 1207724cfd2eSPaolo Abeni if (skb) { 1208724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1209724cfd2eSPaolo Abeni skb = __skb_dequeue(&msk->skb_tx_cache); 1210724cfd2eSPaolo Abeni if (WARN_ON_ONCE(!skb)) 1211724cfd2eSPaolo Abeni return false; 1212724cfd2eSPaolo Abeni 1213724cfd2eSPaolo Abeni mptcp_wmem_uncharge(sk, skb->truesize); 1214724cfd2eSPaolo Abeni ssk->sk_tx_skb_cache = skb; 1215724cfd2eSPaolo Abeni return true; 1216724cfd2eSPaolo Abeni } 1217724cfd2eSPaolo Abeni 1218724cfd2eSPaolo Abeni /* over memory limit, no point to try to allocate a new skb */ 1219724cfd2eSPaolo Abeni return false; 1220724cfd2eSPaolo Abeni } 1221724cfd2eSPaolo Abeni 12226e628cd3SPaolo Abeni skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1223724cfd2eSPaolo Abeni if (!skb) 1224724cfd2eSPaolo Abeni return false; 1225724cfd2eSPaolo Abeni 1226724cfd2eSPaolo Abeni if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1227724cfd2eSPaolo Abeni ssk->sk_tx_skb_cache = skb; 1228724cfd2eSPaolo Abeni return true; 1229724cfd2eSPaolo Abeni } 1230724cfd2eSPaolo Abeni kfree_skb(skb); 1231724cfd2eSPaolo Abeni return false; 1232724cfd2eSPaolo Abeni } 1233724cfd2eSPaolo Abeni 1234724cfd2eSPaolo Abeni static bool mptcp_must_reclaim_memory(struct sock *sk, struct sock *ssk) 1235724cfd2eSPaolo Abeni { 1236724cfd2eSPaolo Abeni return !ssk->sk_tx_skb_cache && 1237724cfd2eSPaolo Abeni !skb_peek(&mptcp_sk(sk)->skb_tx_cache) && 1238724cfd2eSPaolo Abeni tcp_under_memory_pressure(sk); 1239724cfd2eSPaolo Abeni } 1240724cfd2eSPaolo Abeni 1241724cfd2eSPaolo Abeni static bool mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk) 1242724cfd2eSPaolo Abeni { 1243724cfd2eSPaolo Abeni if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) 1244724cfd2eSPaolo Abeni mptcp_mem_reclaim_partial(sk); 12456e628cd3SPaolo Abeni return __mptcp_alloc_tx_skb(sk, ssk, sk->sk_allocation); 1246724cfd2eSPaolo Abeni } 1247724cfd2eSPaolo Abeni 124857040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1249d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag, 1250caf971dfSPaolo Abeni struct mptcp_sendmsg_info *info) 125157040755SPaolo Abeni { 1252d9ca1de8SPaolo Abeni u64 data_seq = dfrag->data_seq + info->sent; 12536d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 12546f8a612aSFlorian Westphal bool zero_window_probe = false; 12556d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 125657040755SPaolo Abeni struct sk_buff *skb, *tail; 1257d9ca1de8SPaolo Abeni bool can_collapse = false; 125815e6ca97SPaolo Abeni int size_bias = 0; 1259d9ca1de8SPaolo Abeni int avail_size; 1260724cfd2eSPaolo Abeni size_t ret = 0; 12616d0060f6SMat Martineau 1262d9ca1de8SPaolo Abeni pr_debug("msk=%p ssk=%p sending dfrag at seq=%lld len=%d already sent=%d", 1263d9ca1de8SPaolo Abeni msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 12646d0060f6SMat Martineau 1265d9ca1de8SPaolo Abeni /* compute send limit */ 1266d9ca1de8SPaolo Abeni info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1267caf971dfSPaolo Abeni avail_size = info->size_goal; 1268724cfd2eSPaolo Abeni msk->size_goal_cache = info->size_goal; 126957040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 127057040755SPaolo Abeni if (skb) { 127157040755SPaolo Abeni /* Limit the write to the size available in the 127257040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 127357040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 127457040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 127557040755SPaolo Abeni * SSN association set here 127657040755SPaolo Abeni */ 1277d9ca1de8SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1278caf971dfSPaolo Abeni can_collapse = (info->size_goal - skb->len > 0) && 1279d9ca1de8SPaolo Abeni mptcp_skb_can_collapse_to(data_seq, skb, mpext); 128015e6ca97SPaolo Abeni if (!can_collapse) { 128157040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 128215e6ca97SPaolo Abeni } else { 128315e6ca97SPaolo Abeni size_bias = skb->len; 1284caf971dfSPaolo Abeni avail_size = info->size_goal - skb->len; 128557040755SPaolo Abeni } 128615e6ca97SPaolo Abeni } 128718b683bfSPaolo Abeni 12886f8a612aSFlorian Westphal /* Zero window and all data acked? Probe. */ 12896f8a612aSFlorian Westphal avail_size = mptcp_check_allowed_size(msk, data_seq, avail_size); 12906f8a612aSFlorian Westphal if (avail_size == 0) { 12917439d687SPaolo Abeni u64 snd_una = READ_ONCE(msk->snd_una); 12927439d687SPaolo Abeni 12937439d687SPaolo Abeni if (skb || snd_una != msk->snd_nxt) 12946f8a612aSFlorian Westphal return 0; 12956f8a612aSFlorian Westphal zero_window_probe = true; 12967439d687SPaolo Abeni data_seq = snd_una - 1; 12976f8a612aSFlorian Westphal avail_size = 1; 12986f8a612aSFlorian Westphal } 12996f8a612aSFlorian Westphal 1300d9ca1de8SPaolo Abeni if (WARN_ON_ONCE(info->sent > info->limit || 1301d9ca1de8SPaolo Abeni info->limit > dfrag->data_len)) 1302d9ca1de8SPaolo Abeni return 0; 130357040755SPaolo Abeni 1304d9ca1de8SPaolo Abeni ret = info->limit - info->sent; 130515e6ca97SPaolo Abeni tail = tcp_build_frag(ssk, avail_size + size_bias, info->flags, 130615e6ca97SPaolo Abeni dfrag->page, dfrag->offset + info->sent, &ret); 1307e2223995SPaolo Abeni if (!tail) { 1308e2223995SPaolo Abeni tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk)); 1309e2223995SPaolo Abeni return -ENOMEM; 131035759383SFlorian Westphal } 131118b683bfSPaolo Abeni 1312e2223995SPaolo Abeni /* if the tail skb is still the cached one, collapsing really happened. 131357040755SPaolo Abeni */ 1314e2223995SPaolo Abeni if (skb == tail) { 131515e6ca97SPaolo Abeni TCP_SKB_CB(tail)->tcp_flags &= ~TCPHDR_PSH; 131657040755SPaolo Abeni mpext->data_len += ret; 131715e6ca97SPaolo Abeni WARN_ON_ONCE(!can_collapse); 13186f8a612aSFlorian Westphal WARN_ON_ONCE(zero_window_probe); 131957040755SPaolo Abeni goto out; 132057040755SPaolo Abeni } 132157040755SPaolo Abeni 1322724cfd2eSPaolo Abeni mpext = skb_ext_find(tail, SKB_EXT_MPTCP); 1323724cfd2eSPaolo Abeni if (WARN_ON_ONCE(!mpext)) { 1324724cfd2eSPaolo Abeni /* should never reach here, stream corrupted */ 1325724cfd2eSPaolo Abeni return -EINVAL; 1326724cfd2eSPaolo Abeni } 13276d0060f6SMat Martineau 13286d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1329d9ca1de8SPaolo Abeni mpext->data_seq = data_seq; 13306d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 13316d0060f6SMat Martineau mpext->data_len = ret; 13326d0060f6SMat Martineau mpext->use_map = 1; 13336d0060f6SMat Martineau mpext->dsn64 = 1; 13346d0060f6SMat Martineau 13356d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 13366d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 13376d0060f6SMat Martineau mpext->dsn64); 13386d0060f6SMat Martineau 13396f8a612aSFlorian Westphal if (zero_window_probe) { 13406f8a612aSFlorian Westphal mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 13416f8a612aSFlorian Westphal mpext->frozen = 1; 13426f8a612aSFlorian Westphal ret = 0; 13436f8a612aSFlorian Westphal tcp_push_pending_frames(ssk); 13446f8a612aSFlorian Westphal } 134557040755SPaolo Abeni out: 13466d0060f6SMat Martineau mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 13476d0060f6SMat Martineau return ret; 13486d0060f6SMat Martineau } 13496d0060f6SMat Martineau 1350d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1351d5f49190SPaolo Abeni sizeof(struct tcphdr) - \ 1352d5f49190SPaolo Abeni MAX_TCP_OPTION_SPACE - \ 1353d5f49190SPaolo Abeni sizeof(struct ipv6hdr) - \ 1354d5f49190SPaolo Abeni sizeof(struct frag_hdr)) 1355d5f49190SPaolo Abeni 1356d5f49190SPaolo Abeni struct subflow_send_info { 1357d5f49190SPaolo Abeni struct sock *ssk; 1358d5f49190SPaolo Abeni u64 ratio; 1359d5f49190SPaolo Abeni }; 1360d5f49190SPaolo Abeni 13615cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1362f296234cSPeter Krystad { 1363d5f49190SPaolo Abeni struct subflow_send_info send_info[2]; 1364f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 1365d5f49190SPaolo Abeni int i, nr_active = 0; 1366d5f49190SPaolo Abeni struct sock *ssk; 1367d5f49190SPaolo Abeni u64 ratio; 1368d5f49190SPaolo Abeni u32 pace; 1369f296234cSPeter Krystad 1370d5f49190SPaolo Abeni sock_owned_by_me((struct sock *)msk); 1371f296234cSPeter Krystad 1372d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) { 1373d5f49190SPaolo Abeni if (!msk->first) 1374f296234cSPeter Krystad return NULL; 1375d5f49190SPaolo Abeni return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1376f296234cSPeter Krystad } 1377f296234cSPeter Krystad 1378d5f49190SPaolo Abeni /* re-use last subflow, if the burst allow that */ 1379d5f49190SPaolo Abeni if (msk->last_snd && msk->snd_burst > 0 && 1380d5f49190SPaolo Abeni sk_stream_memory_free(msk->last_snd) && 13815cf92bbaSPaolo Abeni mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) 1382d5f49190SPaolo Abeni return msk->last_snd; 1383f296234cSPeter Krystad 1384d5f49190SPaolo Abeni /* pick the subflow with the lower wmem/wspace ratio */ 1385d5f49190SPaolo Abeni for (i = 0; i < 2; ++i) { 1386d5f49190SPaolo Abeni send_info[i].ssk = NULL; 1387d5f49190SPaolo Abeni send_info[i].ratio = -1; 1388d5f49190SPaolo Abeni } 1389d5f49190SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 1390d5f49190SPaolo Abeni ssk = mptcp_subflow_tcp_sock(subflow); 1391d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 1392f296234cSPeter Krystad continue; 1393d5f49190SPaolo Abeni 1394d5f49190SPaolo Abeni nr_active += !subflow->backup; 1395ec369c3aSPaolo Abeni if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd) 1396d5f49190SPaolo Abeni continue; 1397d5f49190SPaolo Abeni 1398d5f49190SPaolo Abeni pace = READ_ONCE(ssk->sk_pacing_rate); 1399d5f49190SPaolo Abeni if (!pace) 1400d5f49190SPaolo Abeni continue; 1401d5f49190SPaolo Abeni 1402d5f49190SPaolo Abeni ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1403d5f49190SPaolo Abeni pace); 1404d5f49190SPaolo Abeni if (ratio < send_info[subflow->backup].ratio) { 1405d5f49190SPaolo Abeni send_info[subflow->backup].ssk = ssk; 1406d5f49190SPaolo Abeni send_info[subflow->backup].ratio = ratio; 1407d5f49190SPaolo Abeni } 1408f296234cSPeter Krystad } 1409f296234cSPeter Krystad 1410d5f49190SPaolo Abeni pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld", 1411d5f49190SPaolo Abeni msk, nr_active, send_info[0].ssk, send_info[0].ratio, 1412d5f49190SPaolo Abeni send_info[1].ssk, send_info[1].ratio); 1413f296234cSPeter Krystad 1414d5f49190SPaolo Abeni /* pick the best backup if no other subflow is active */ 1415d5f49190SPaolo Abeni if (!nr_active) 1416d5f49190SPaolo Abeni send_info[0].ssk = send_info[1].ssk; 1417d5f49190SPaolo Abeni 1418d5f49190SPaolo Abeni if (send_info[0].ssk) { 1419d5f49190SPaolo Abeni msk->last_snd = send_info[0].ssk; 1420d5f49190SPaolo Abeni msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1421ec369c3aSPaolo Abeni tcp_sk(msk->last_snd)->snd_wnd); 1422d5f49190SPaolo Abeni return msk->last_snd; 1423d5f49190SPaolo Abeni } 14245cf92bbaSPaolo Abeni 1425d5f49190SPaolo Abeni return NULL; 1426f296234cSPeter Krystad } 1427f296234cSPeter Krystad 1428d9ca1de8SPaolo Abeni static void mptcp_push_release(struct sock *sk, struct sock *ssk, 1429d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info *info) 1430d9ca1de8SPaolo Abeni { 1431d9ca1de8SPaolo Abeni mptcp_set_timeout(sk, ssk); 1432d9ca1de8SPaolo Abeni tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1433d9ca1de8SPaolo Abeni release_sock(ssk); 1434d9ca1de8SPaolo Abeni } 1435d9ca1de8SPaolo Abeni 1436d9ca1de8SPaolo Abeni static void mptcp_push_pending(struct sock *sk, unsigned int flags) 1437d9ca1de8SPaolo Abeni { 1438d9ca1de8SPaolo Abeni struct sock *prev_ssk = NULL, *ssk = NULL; 1439d9ca1de8SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 1440d9ca1de8SPaolo Abeni struct mptcp_sendmsg_info info = { 1441d9ca1de8SPaolo Abeni .flags = flags, 1442d9ca1de8SPaolo Abeni }; 1443d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1444d9ca1de8SPaolo Abeni int len, copied = 0; 1445d9ca1de8SPaolo Abeni 1446d9ca1de8SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 1447d9ca1de8SPaolo Abeni info.sent = dfrag->already_sent; 1448d9ca1de8SPaolo Abeni info.limit = dfrag->data_len; 1449d9ca1de8SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 1450d9ca1de8SPaolo Abeni while (len > 0) { 1451d9ca1de8SPaolo Abeni int ret = 0; 1452d9ca1de8SPaolo Abeni 1453d9ca1de8SPaolo Abeni prev_ssk = ssk; 1454d9ca1de8SPaolo Abeni __mptcp_flush_join_list(msk); 14555cf92bbaSPaolo Abeni ssk = mptcp_subflow_get_send(msk); 1456d9ca1de8SPaolo Abeni 1457d9ca1de8SPaolo Abeni /* try to keep the subflow socket lock across 1458d9ca1de8SPaolo Abeni * consecutive xmit on the same socket 1459d9ca1de8SPaolo Abeni */ 1460d9ca1de8SPaolo Abeni if (ssk != prev_ssk && prev_ssk) 1461d9ca1de8SPaolo Abeni mptcp_push_release(sk, prev_ssk, &info); 1462d9ca1de8SPaolo Abeni if (!ssk) 1463d9ca1de8SPaolo Abeni goto out; 1464d9ca1de8SPaolo Abeni 1465d9ca1de8SPaolo Abeni if (ssk != prev_ssk || !prev_ssk) 1466d9ca1de8SPaolo Abeni lock_sock(ssk); 1467d9ca1de8SPaolo Abeni 1468724cfd2eSPaolo Abeni /* keep it simple and always provide a new skb for the 1469724cfd2eSPaolo Abeni * subflow, even if we will not use it when collapsing 1470724cfd2eSPaolo Abeni * on the pending one 1471724cfd2eSPaolo Abeni */ 1472724cfd2eSPaolo Abeni if (!mptcp_alloc_tx_skb(sk, ssk)) { 1473724cfd2eSPaolo Abeni mptcp_push_release(sk, ssk, &info); 1474724cfd2eSPaolo Abeni goto out; 1475724cfd2eSPaolo Abeni } 1476724cfd2eSPaolo Abeni 1477d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1478d9ca1de8SPaolo Abeni if (ret <= 0) { 1479d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1480d9ca1de8SPaolo Abeni goto out; 1481d9ca1de8SPaolo Abeni } 1482d9ca1de8SPaolo Abeni 1483d9ca1de8SPaolo Abeni info.sent += ret; 1484d9ca1de8SPaolo Abeni dfrag->already_sent += ret; 1485d9ca1de8SPaolo Abeni msk->snd_nxt += ret; 1486d9ca1de8SPaolo Abeni msk->snd_burst -= ret; 1487724cfd2eSPaolo Abeni msk->tx_pending_data -= ret; 1488d9ca1de8SPaolo Abeni copied += ret; 1489d9ca1de8SPaolo Abeni len -= ret; 1490d9ca1de8SPaolo Abeni } 1491d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1492d9ca1de8SPaolo Abeni } 1493d9ca1de8SPaolo Abeni 1494d9ca1de8SPaolo Abeni /* at this point we held the socket lock for the last subflow we used */ 1495d9ca1de8SPaolo Abeni if (ssk) 1496d9ca1de8SPaolo Abeni mptcp_push_release(sk, ssk, &info); 1497d9ca1de8SPaolo Abeni 1498d9ca1de8SPaolo Abeni out: 1499b680a214SPaolo Abeni if (copied) { 1500d9ca1de8SPaolo Abeni /* start the timer, if it's not pending */ 1501d9ca1de8SPaolo Abeni if (!mptcp_timer_pending(sk)) 1502d9ca1de8SPaolo Abeni mptcp_reset_timer(sk); 1503d9ca1de8SPaolo Abeni __mptcp_check_send_data_fin(sk); 1504d9ca1de8SPaolo Abeni } 1505b680a214SPaolo Abeni } 1506d9ca1de8SPaolo Abeni 15076e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 15086e628cd3SPaolo Abeni { 15096e628cd3SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 15106e628cd3SPaolo Abeni struct mptcp_sendmsg_info info; 15116e628cd3SPaolo Abeni struct mptcp_data_frag *dfrag; 1512*b19bc294SPaolo Abeni struct sock *xmit_ssk; 15136e628cd3SPaolo Abeni int len, copied = 0; 1514*b19bc294SPaolo Abeni bool first = true; 15156e628cd3SPaolo Abeni 15166e628cd3SPaolo Abeni info.flags = 0; 15176e628cd3SPaolo Abeni while ((dfrag = mptcp_send_head(sk))) { 15186e628cd3SPaolo Abeni info.sent = dfrag->already_sent; 15196e628cd3SPaolo Abeni info.limit = dfrag->data_len; 15206e628cd3SPaolo Abeni len = dfrag->data_len - dfrag->already_sent; 15216e628cd3SPaolo Abeni while (len > 0) { 15226e628cd3SPaolo Abeni int ret = 0; 15236e628cd3SPaolo Abeni 1524*b19bc294SPaolo Abeni /* the caller already invoked the packet scheduler, 1525*b19bc294SPaolo Abeni * check for a different subflow usage only after 1526*b19bc294SPaolo Abeni * spooling the first chunk of data 1527*b19bc294SPaolo Abeni */ 1528*b19bc294SPaolo Abeni xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1529*b19bc294SPaolo Abeni if (!xmit_ssk) 1530*b19bc294SPaolo Abeni goto out; 1531*b19bc294SPaolo Abeni if (xmit_ssk != ssk) { 1532*b19bc294SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 1533*b19bc294SPaolo Abeni goto out; 1534*b19bc294SPaolo Abeni } 1535*b19bc294SPaolo Abeni 15366e628cd3SPaolo Abeni if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) { 15376e628cd3SPaolo Abeni __mptcp_update_wmem(sk); 15386e628cd3SPaolo Abeni sk_mem_reclaim_partial(sk); 15396e628cd3SPaolo Abeni } 15406e628cd3SPaolo Abeni if (!__mptcp_alloc_tx_skb(sk, ssk, GFP_ATOMIC)) 15416e628cd3SPaolo Abeni goto out; 15426e628cd3SPaolo Abeni 15436e628cd3SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 15446e628cd3SPaolo Abeni if (ret <= 0) 15456e628cd3SPaolo Abeni goto out; 15466e628cd3SPaolo Abeni 15476e628cd3SPaolo Abeni info.sent += ret; 15486e628cd3SPaolo Abeni dfrag->already_sent += ret; 15496e628cd3SPaolo Abeni msk->snd_nxt += ret; 15506e628cd3SPaolo Abeni msk->snd_burst -= ret; 15516e628cd3SPaolo Abeni msk->tx_pending_data -= ret; 15526e628cd3SPaolo Abeni copied += ret; 15536e628cd3SPaolo Abeni len -= ret; 1554*b19bc294SPaolo Abeni first = false; 15556e628cd3SPaolo Abeni } 15566e628cd3SPaolo Abeni WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 15576e628cd3SPaolo Abeni } 15586e628cd3SPaolo Abeni 15596e628cd3SPaolo Abeni out: 15606e628cd3SPaolo Abeni /* __mptcp_alloc_tx_skb could have released some wmem and we are 15616e628cd3SPaolo Abeni * not going to flush it via release_sock() 15626e628cd3SPaolo Abeni */ 15636e628cd3SPaolo Abeni __mptcp_update_wmem(sk); 15646e628cd3SPaolo Abeni if (copied) { 15656e628cd3SPaolo Abeni mptcp_set_timeout(sk, ssk); 15666e628cd3SPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 15676e628cd3SPaolo Abeni info.size_goal); 15686e628cd3SPaolo Abeni if (msk->snd_data_fin_enable && 15696e628cd3SPaolo Abeni msk->snd_nxt + 1 == msk->write_seq) 15706e628cd3SPaolo Abeni mptcp_schedule_work(sk); 15716e628cd3SPaolo Abeni } 15726e628cd3SPaolo Abeni } 15736e628cd3SPaolo Abeni 15745cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk) 15755cf92bbaSPaolo Abeni { 15765cf92bbaSPaolo Abeni /* enable autotune */ 15775cf92bbaSPaolo Abeni set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15785cf92bbaSPaolo Abeni 15795cf92bbaSPaolo Abeni /* will be cleared on avail space */ 15805cf92bbaSPaolo Abeni set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 15815cf92bbaSPaolo Abeni } 15825cf92bbaSPaolo Abeni 1583f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1584f870fa0bSMat Martineau { 1585f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 158617091708SFlorian Westphal struct page_frag *pfrag; 15876d0060f6SMat Martineau size_t copied = 0; 1588caf971dfSPaolo Abeni int ret = 0; 15896d0060f6SMat Martineau long timeo; 1590f870fa0bSMat Martineau 1591f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 1592f870fa0bSMat Martineau return -EOPNOTSUPP; 1593f870fa0bSMat Martineau 1594e7579d5dSDavide Caratti mptcp_lock_sock(sk, __mptcp_wmem_reserve(sk, min_t(size_t, 1 << 20, len))); 15951954b860SMat Martineau 15961954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 15971954b860SMat Martineau 15981954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 15991954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 16001954b860SMat Martineau if (ret) 16011954b860SMat Martineau goto out; 16021954b860SMat Martineau } 16031954b860SMat Martineau 160417091708SFlorian Westphal pfrag = sk_page_frag(sk); 160518b683bfSPaolo Abeni 1606d9ca1de8SPaolo Abeni while (msg_data_left(msg)) { 1607724cfd2eSPaolo Abeni int total_ts, frag_truesize = 0; 1608d9ca1de8SPaolo Abeni struct mptcp_data_frag *dfrag; 1609724cfd2eSPaolo Abeni struct sk_buff_head skbs; 1610d9ca1de8SPaolo Abeni bool dfrag_collapsed; 1611d9ca1de8SPaolo Abeni size_t psize, offset; 1612d9ca1de8SPaolo Abeni 161357baaf28SMat Martineau if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 161457baaf28SMat Martineau ret = -EPIPE; 161557baaf28SMat Martineau goto out; 161657baaf28SMat Martineau } 161757baaf28SMat Martineau 1618d9ca1de8SPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 1619d9ca1de8SPaolo Abeni * page allocator 1620fb529e62SFlorian Westphal */ 1621d9ca1de8SPaolo Abeni dfrag = mptcp_pending_tail(sk); 1622d9ca1de8SPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1623d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1624d9ca1de8SPaolo Abeni if (!sk_stream_memory_free(sk)) 1625d9ca1de8SPaolo Abeni goto wait_for_memory; 16266e628cd3SPaolo Abeni 1627d9ca1de8SPaolo Abeni if (!mptcp_page_frag_refill(sk, pfrag)) 1628d9ca1de8SPaolo Abeni goto wait_for_memory; 1629d9ca1de8SPaolo Abeni 1630d9ca1de8SPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1631d9ca1de8SPaolo Abeni frag_truesize = dfrag->overhead; 1632fb529e62SFlorian Westphal } 1633fb529e62SFlorian Westphal 1634d9ca1de8SPaolo Abeni /* we do not bound vs wspace, to allow a single packet. 1635d9ca1de8SPaolo Abeni * memory accounting will prevent execessive memory usage 1636d9ca1de8SPaolo Abeni * anyway 1637d9ca1de8SPaolo Abeni */ 1638d9ca1de8SPaolo Abeni offset = dfrag->offset + dfrag->data_len; 1639d9ca1de8SPaolo Abeni psize = pfrag->size - offset; 1640d9ca1de8SPaolo Abeni psize = min_t(size_t, psize, msg_data_left(msg)); 1641724cfd2eSPaolo Abeni total_ts = psize + frag_truesize; 1642724cfd2eSPaolo Abeni __skb_queue_head_init(&skbs); 1643724cfd2eSPaolo Abeni if (!mptcp_tx_cache_refill(sk, psize, &skbs, &total_ts)) 1644d9ca1de8SPaolo Abeni goto wait_for_memory; 1645d9ca1de8SPaolo Abeni 1646724cfd2eSPaolo Abeni if (!mptcp_wmem_alloc(sk, total_ts)) { 1647724cfd2eSPaolo Abeni __skb_queue_purge(&skbs); 1648724cfd2eSPaolo Abeni goto wait_for_memory; 1649724cfd2eSPaolo Abeni } 1650724cfd2eSPaolo Abeni 1651724cfd2eSPaolo Abeni skb_queue_splice_tail(&skbs, &msk->skb_tx_cache); 1652d9ca1de8SPaolo Abeni if (copy_page_from_iter(dfrag->page, offset, psize, 1653d9ca1de8SPaolo Abeni &msg->msg_iter) != psize) { 165487952603SPaolo Abeni mptcp_wmem_uncharge(sk, psize + frag_truesize); 1655d9ca1de8SPaolo Abeni ret = -EFAULT; 1656d9ca1de8SPaolo Abeni goto out; 1657d9ca1de8SPaolo Abeni } 1658d9ca1de8SPaolo Abeni 1659d9ca1de8SPaolo Abeni /* data successfully copied into the write queue */ 1660d9ca1de8SPaolo Abeni copied += psize; 1661d9ca1de8SPaolo Abeni dfrag->data_len += psize; 1662d9ca1de8SPaolo Abeni frag_truesize += psize; 1663d9ca1de8SPaolo Abeni pfrag->offset += frag_truesize; 1664d9ca1de8SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 166513e16037SPaolo Abeni msk->tx_pending_data += psize; 1666d9ca1de8SPaolo Abeni 1667d9ca1de8SPaolo Abeni /* charge data on mptcp pending queue to the msk socket 1668d9ca1de8SPaolo Abeni * Note: we charge such data both to sk and ssk 1669d9ca1de8SPaolo Abeni */ 1670d9ca1de8SPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 1671d9ca1de8SPaolo Abeni if (!dfrag_collapsed) { 1672d9ca1de8SPaolo Abeni get_page(dfrag->page); 1673d9ca1de8SPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 1674d9ca1de8SPaolo Abeni if (!msk->first_pending) 1675d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, dfrag); 1676d9ca1de8SPaolo Abeni } 1677d9ca1de8SPaolo Abeni pr_debug("msk=%p dfrag at seq=%lld len=%d sent=%d new=%d", msk, 1678d9ca1de8SPaolo Abeni dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1679d9ca1de8SPaolo Abeni !dfrag_collapsed); 1680d9ca1de8SPaolo Abeni 1681d9ca1de8SPaolo Abeni continue; 1682d9ca1de8SPaolo Abeni 1683d9ca1de8SPaolo Abeni wait_for_memory: 16845cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 16856e628cd3SPaolo Abeni mptcp_push_pending(sk, msg->msg_flags); 1686f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 1687f296234cSPeter Krystad if (ret) 1688f296234cSPeter Krystad goto out; 1689cec37a6eSPeter Krystad } 1690cec37a6eSPeter Krystad 169113e16037SPaolo Abeni if (copied) 1692d9ca1de8SPaolo Abeni mptcp_push_pending(sk, msg->msg_flags); 1693da51aef5SPaolo Abeni 16941954b860SMat Martineau out: 1695cec37a6eSPeter Krystad release_sock(sk); 16968555c6bfSPaolo Abeni return copied ? : ret; 1697f870fa0bSMat Martineau } 1698f870fa0bSMat Martineau 16997a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 17007a6a6cbcSPaolo Abeni { 17017a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 17027a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 17037a6a6cbcSPaolo Abeni 17047a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 17057a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 17067a6a6cbcSPaolo Abeni 17077a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 17087a6a6cbcSPaolo Abeni test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 17097a6a6cbcSPaolo Abeni 17107a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 17117a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 17127a6a6cbcSPaolo Abeni } 17137a6a6cbcSPaolo Abeni 17146771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 17156771bfd9SFlorian Westphal struct msghdr *msg, 17166771bfd9SFlorian Westphal size_t len) 17176771bfd9SFlorian Westphal { 17186771bfd9SFlorian Westphal struct sk_buff *skb; 17196771bfd9SFlorian Westphal int copied = 0; 17206771bfd9SFlorian Westphal 172187952603SPaolo Abeni while ((skb = skb_peek(&msk->receive_queue)) != NULL) { 17226771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 17236771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 17246771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 17256771bfd9SFlorian Westphal int err; 17266771bfd9SFlorian Westphal 17276771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 17286771bfd9SFlorian Westphal if (unlikely(err < 0)) { 17296771bfd9SFlorian Westphal if (!copied) 17306771bfd9SFlorian Westphal return err; 17316771bfd9SFlorian Westphal break; 17326771bfd9SFlorian Westphal } 17336771bfd9SFlorian Westphal 17346771bfd9SFlorian Westphal copied += count; 17356771bfd9SFlorian Westphal 17366771bfd9SFlorian Westphal if (count < data_len) { 17376771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 17386771bfd9SFlorian Westphal break; 17396771bfd9SFlorian Westphal } 17406771bfd9SFlorian Westphal 174187952603SPaolo Abeni /* we will bulk release the skb memory later */ 174287952603SPaolo Abeni skb->destructor = NULL; 174387952603SPaolo Abeni msk->rmem_released += skb->truesize; 174487952603SPaolo Abeni __skb_unlink(skb, &msk->receive_queue); 17456771bfd9SFlorian Westphal __kfree_skb(skb); 17466771bfd9SFlorian Westphal 17476771bfd9SFlorian Westphal if (copied >= len) 17486771bfd9SFlorian Westphal break; 17496771bfd9SFlorian Westphal } 17506771bfd9SFlorian Westphal 17516771bfd9SFlorian Westphal return copied; 17526771bfd9SFlorian Westphal } 17536771bfd9SFlorian Westphal 1754a6b118feSFlorian Westphal /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1755a6b118feSFlorian Westphal * 1756a6b118feSFlorian Westphal * Only difference: Use highest rtt estimate of the subflows in use. 1757a6b118feSFlorian Westphal */ 1758a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1759a6b118feSFlorian Westphal { 1760a6b118feSFlorian Westphal struct mptcp_subflow_context *subflow; 1761a6b118feSFlorian Westphal struct sock *sk = (struct sock *)msk; 1762a6b118feSFlorian Westphal u32 time, advmss = 1; 1763a6b118feSFlorian Westphal u64 rtt_us, mstamp; 1764a6b118feSFlorian Westphal 1765a6b118feSFlorian Westphal sock_owned_by_me(sk); 1766a6b118feSFlorian Westphal 1767a6b118feSFlorian Westphal if (copied <= 0) 1768a6b118feSFlorian Westphal return; 1769a6b118feSFlorian Westphal 1770a6b118feSFlorian Westphal msk->rcvq_space.copied += copied; 1771a6b118feSFlorian Westphal 1772a6b118feSFlorian Westphal mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1773a6b118feSFlorian Westphal time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1774a6b118feSFlorian Westphal 1775a6b118feSFlorian Westphal rtt_us = msk->rcvq_space.rtt_us; 1776a6b118feSFlorian Westphal if (rtt_us && time < (rtt_us >> 3)) 1777a6b118feSFlorian Westphal return; 1778a6b118feSFlorian Westphal 1779a6b118feSFlorian Westphal rtt_us = 0; 1780a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1781a6b118feSFlorian Westphal const struct tcp_sock *tp; 1782a6b118feSFlorian Westphal u64 sf_rtt_us; 1783a6b118feSFlorian Westphal u32 sf_advmss; 1784a6b118feSFlorian Westphal 1785a6b118feSFlorian Westphal tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1786a6b118feSFlorian Westphal 1787a6b118feSFlorian Westphal sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1788a6b118feSFlorian Westphal sf_advmss = READ_ONCE(tp->advmss); 1789a6b118feSFlorian Westphal 1790a6b118feSFlorian Westphal rtt_us = max(sf_rtt_us, rtt_us); 1791a6b118feSFlorian Westphal advmss = max(sf_advmss, advmss); 1792a6b118feSFlorian Westphal } 1793a6b118feSFlorian Westphal 1794a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = rtt_us; 1795a6b118feSFlorian Westphal if (time < (rtt_us >> 3) || rtt_us == 0) 1796a6b118feSFlorian Westphal return; 1797a6b118feSFlorian Westphal 1798a6b118feSFlorian Westphal if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1799a6b118feSFlorian Westphal goto new_measure; 1800a6b118feSFlorian Westphal 1801a6b118feSFlorian Westphal if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1802a6b118feSFlorian Westphal !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1803a6b118feSFlorian Westphal int rcvmem, rcvbuf; 1804a6b118feSFlorian Westphal u64 rcvwin, grow; 1805a6b118feSFlorian Westphal 1806a6b118feSFlorian Westphal rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1807a6b118feSFlorian Westphal 1808a6b118feSFlorian Westphal grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1809a6b118feSFlorian Westphal 1810a6b118feSFlorian Westphal do_div(grow, msk->rcvq_space.space); 1811a6b118feSFlorian Westphal rcvwin += (grow << 1); 1812a6b118feSFlorian Westphal 1813a6b118feSFlorian Westphal rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1814a6b118feSFlorian Westphal while (tcp_win_from_space(sk, rcvmem) < advmss) 1815a6b118feSFlorian Westphal rcvmem += 128; 1816a6b118feSFlorian Westphal 1817a6b118feSFlorian Westphal do_div(rcvwin, advmss); 1818a6b118feSFlorian Westphal rcvbuf = min_t(u64, rcvwin * rcvmem, 1819a6b118feSFlorian Westphal sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1820a6b118feSFlorian Westphal 1821a6b118feSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) { 1822a6b118feSFlorian Westphal u32 window_clamp; 1823a6b118feSFlorian Westphal 1824a6b118feSFlorian Westphal window_clamp = tcp_win_from_space(sk, rcvbuf); 1825a6b118feSFlorian Westphal WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1826a6b118feSFlorian Westphal 1827a6b118feSFlorian Westphal /* Make subflows follow along. If we do not do this, we 1828a6b118feSFlorian Westphal * get drops at subflow level if skbs can't be moved to 1829a6b118feSFlorian Westphal * the mptcp rx queue fast enough (announced rcv_win can 1830a6b118feSFlorian Westphal * exceed ssk->sk_rcvbuf). 1831a6b118feSFlorian Westphal */ 1832a6b118feSFlorian Westphal mptcp_for_each_subflow(msk, subflow) { 1833a6b118feSFlorian Westphal struct sock *ssk; 1834c76c6956SPaolo Abeni bool slow; 1835a6b118feSFlorian Westphal 1836a6b118feSFlorian Westphal ssk = mptcp_subflow_tcp_sock(subflow); 1837c76c6956SPaolo Abeni slow = lock_sock_fast(ssk); 1838a6b118feSFlorian Westphal WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1839a6b118feSFlorian Westphal tcp_sk(ssk)->window_clamp = window_clamp; 1840c76c6956SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1841c76c6956SPaolo Abeni unlock_sock_fast(ssk, slow); 1842a6b118feSFlorian Westphal } 1843a6b118feSFlorian Westphal } 1844a6b118feSFlorian Westphal } 1845a6b118feSFlorian Westphal 1846a6b118feSFlorian Westphal msk->rcvq_space.space = msk->rcvq_space.copied; 1847a6b118feSFlorian Westphal new_measure: 1848a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 1849a6b118feSFlorian Westphal msk->rcvq_space.time = mstamp; 1850a6b118feSFlorian Westphal } 1851a6b118feSFlorian Westphal 185287952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk) 185387952603SPaolo Abeni { 185487952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 185587952603SPaolo Abeni 185687952603SPaolo Abeni if (!msk->rmem_released) 185787952603SPaolo Abeni return; 185887952603SPaolo Abeni 185987952603SPaolo Abeni atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 186087952603SPaolo Abeni sk_mem_uncharge(sk, msk->rmem_released); 186187952603SPaolo Abeni msk->rmem_released = 0; 186287952603SPaolo Abeni } 186387952603SPaolo Abeni 186487952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk) 186587952603SPaolo Abeni { 186687952603SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 186787952603SPaolo Abeni 186887952603SPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 186987952603SPaolo Abeni } 187087952603SPaolo Abeni 1871ea4ca586SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk, unsigned int rcv) 18726771bfd9SFlorian Westphal { 187387952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 18746771bfd9SFlorian Westphal unsigned int moved = 0; 187587952603SPaolo Abeni bool ret, done; 1876d5f49190SPaolo Abeni 1877d5f49190SPaolo Abeni __mptcp_flush_join_list(msk); 18786771bfd9SFlorian Westphal do { 18796771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 188065f49fe7SFlorian Westphal bool slowpath; 18816771bfd9SFlorian Westphal 188287952603SPaolo Abeni /* we can have data pending in the subflows only if the msk 188387952603SPaolo Abeni * receive buffer was full at subflow_data_ready() time, 188487952603SPaolo Abeni * that is an unlikely slow path. 188587952603SPaolo Abeni */ 188687952603SPaolo Abeni if (likely(!ssk)) 18876771bfd9SFlorian Westphal break; 18886771bfd9SFlorian Westphal 188965f49fe7SFlorian Westphal slowpath = lock_sock_fast(ssk); 189087952603SPaolo Abeni mptcp_data_lock(sk); 18916771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 189287952603SPaolo Abeni mptcp_data_unlock(sk); 1893ea4ca586SPaolo Abeni if (moved && rcv) { 1894ea4ca586SPaolo Abeni WRITE_ONCE(msk->rmem_pending, min(rcv, moved)); 1895ea4ca586SPaolo Abeni tcp_cleanup_rbuf(ssk, 1); 1896ea4ca586SPaolo Abeni WRITE_ONCE(msk->rmem_pending, 0); 1897ea4ca586SPaolo Abeni } 189865f49fe7SFlorian Westphal unlock_sock_fast(ssk, slowpath); 18996771bfd9SFlorian Westphal } while (!done); 19006771bfd9SFlorian Westphal 190187952603SPaolo Abeni /* acquire the data lock only if some input data is pending */ 190287952603SPaolo Abeni ret = moved > 0; 190387952603SPaolo Abeni if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 190487952603SPaolo Abeni !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 190587952603SPaolo Abeni mptcp_data_lock(sk); 190687952603SPaolo Abeni __mptcp_update_rmem(sk); 190787952603SPaolo Abeni ret |= __mptcp_ofo_queue(msk); 190887952603SPaolo Abeni __mptcp_splice_receive_queue(sk); 190987952603SPaolo Abeni mptcp_data_unlock(sk); 1910ab174ad8SPaolo Abeni } 191187952603SPaolo Abeni if (ret) 191287952603SPaolo Abeni mptcp_check_data_fin((struct sock *)msk); 191387952603SPaolo Abeni return !skb_queue_empty(&msk->receive_queue); 19146771bfd9SFlorian Westphal } 19156771bfd9SFlorian Westphal 1916f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 1917f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 1918f870fa0bSMat Martineau { 1919f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1920cec37a6eSPeter Krystad int copied = 0; 19217a6a6cbcSPaolo Abeni int target; 19227a6a6cbcSPaolo Abeni long timeo; 1923f870fa0bSMat Martineau 1924f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 1925f870fa0bSMat Martineau return -EOPNOTSUPP; 1926f870fa0bSMat Martineau 192787952603SPaolo Abeni mptcp_lock_sock(sk, __mptcp_splice_receive_queue(sk)); 1928fd897679SPaolo Abeni if (unlikely(sk->sk_state == TCP_LISTEN)) { 1929fd897679SPaolo Abeni copied = -ENOTCONN; 1930fd897679SPaolo Abeni goto out_err; 1931fd897679SPaolo Abeni } 1932fd897679SPaolo Abeni 19337a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 19347a6a6cbcSPaolo Abeni 19357a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 19367a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 19377a6a6cbcSPaolo Abeni 193805e3eceaSEric Dumazet while (copied < len) { 1939ea4ca586SPaolo Abeni int bytes_read, old_space; 19407a6a6cbcSPaolo Abeni 19416771bfd9SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 19426771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 19437a6a6cbcSPaolo Abeni if (!copied) 19447a6a6cbcSPaolo Abeni copied = bytes_read; 19456771bfd9SFlorian Westphal goto out_err; 1946cec37a6eSPeter Krystad } 1947cec37a6eSPeter Krystad 19487a6a6cbcSPaolo Abeni copied += bytes_read; 19497a6a6cbcSPaolo Abeni 195087952603SPaolo Abeni if (skb_queue_empty(&msk->receive_queue) && 1951ea4ca586SPaolo Abeni __mptcp_move_skbs(msk, len - copied)) 19526771bfd9SFlorian Westphal continue; 19537a6a6cbcSPaolo Abeni 1954ea4ca586SPaolo Abeni /* be sure to advertise window change */ 1955ea4ca586SPaolo Abeni old_space = READ_ONCE(msk->old_wspace); 1956ea4ca586SPaolo Abeni if ((tcp_space(sk) - old_space) >= old_space) 1957fd897679SPaolo Abeni mptcp_cleanup_rbuf(msk); 1958ea4ca586SPaolo Abeni 19597a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 19607a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 19617a6a6cbcSPaolo Abeni */ 19627a6a6cbcSPaolo Abeni if (copied >= target) 19637a6a6cbcSPaolo Abeni break; 19647a6a6cbcSPaolo Abeni 19657a6a6cbcSPaolo Abeni if (copied) { 19667a6a6cbcSPaolo Abeni if (sk->sk_err || 19677a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 19687a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 19697a6a6cbcSPaolo Abeni !timeo || 19707a6a6cbcSPaolo Abeni signal_pending(current)) 19717a6a6cbcSPaolo Abeni break; 19727a6a6cbcSPaolo Abeni } else { 19737a6a6cbcSPaolo Abeni if (sk->sk_err) { 19747a6a6cbcSPaolo Abeni copied = sock_error(sk); 19757a6a6cbcSPaolo Abeni break; 19767a6a6cbcSPaolo Abeni } 19777a6a6cbcSPaolo Abeni 19785969856aSPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 19795969856aSPaolo Abeni mptcp_check_for_eof(msk); 19805969856aSPaolo Abeni 198187952603SPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) { 198287952603SPaolo Abeni /* race breaker: the shutdown could be after the 198387952603SPaolo Abeni * previous receive queue check 198487952603SPaolo Abeni */ 198587952603SPaolo Abeni if (__mptcp_move_skbs(msk, len - copied)) 198687952603SPaolo Abeni continue; 19877a6a6cbcSPaolo Abeni break; 198887952603SPaolo Abeni } 19897a6a6cbcSPaolo Abeni 19907a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 19917a6a6cbcSPaolo Abeni copied = -ENOTCONN; 19927a6a6cbcSPaolo Abeni break; 19937a6a6cbcSPaolo Abeni } 19947a6a6cbcSPaolo Abeni 19957a6a6cbcSPaolo Abeni if (!timeo) { 19967a6a6cbcSPaolo Abeni copied = -EAGAIN; 19977a6a6cbcSPaolo Abeni break; 19987a6a6cbcSPaolo Abeni } 19997a6a6cbcSPaolo Abeni 20007a6a6cbcSPaolo Abeni if (signal_pending(current)) { 20017a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 20027a6a6cbcSPaolo Abeni break; 20037a6a6cbcSPaolo Abeni } 20047a6a6cbcSPaolo Abeni } 20057a6a6cbcSPaolo Abeni 20067a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 20077a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 20087a6a6cbcSPaolo Abeni } 20097a6a6cbcSPaolo Abeni 201087952603SPaolo Abeni if (skb_queue_empty_lockless(&sk->sk_receive_queue) && 201187952603SPaolo Abeni skb_queue_empty(&msk->receive_queue)) { 20126771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 20137a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 20147a6a6cbcSPaolo Abeni 20156771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 20166771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 20177a6a6cbcSPaolo Abeni */ 2018ea4ca586SPaolo Abeni if (unlikely(__mptcp_move_skbs(msk, 0))) 20196771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 20206771bfd9SFlorian Westphal } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 20216771bfd9SFlorian Westphal /* data to read but mptcp_wait_data() cleared DATA_READY */ 20227a6a6cbcSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 20237a6a6cbcSPaolo Abeni } 20246771bfd9SFlorian Westphal out_err: 20256719331cSPaolo Abeni pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d", 20266719331cSPaolo Abeni msk, test_bit(MPTCP_DATA_READY, &msk->flags), 202787952603SPaolo Abeni skb_queue_empty_lockless(&sk->sk_receive_queue), copied); 2028a6b118feSFlorian Westphal mptcp_rcv_space_adjust(msk, copied); 2029a6b118feSFlorian Westphal 2030cec37a6eSPeter Krystad release_sock(sk); 2031cec37a6eSPeter Krystad return copied; 2032cec37a6eSPeter Krystad } 2033cec37a6eSPeter Krystad 2034b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk) 2035b51f9b80SPaolo Abeni { 2036b51f9b80SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2037b51f9b80SPaolo Abeni 20383b1d6210SPaolo Abeni set_bit(MPTCP_WORK_RTX, &msk->flags); 2039ba8f48f7SPaolo Abeni mptcp_schedule_work(sk); 20403b1d6210SPaolo Abeni } 2041b51f9b80SPaolo Abeni 2042b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 2043b51f9b80SPaolo Abeni { 2044b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 2045b51f9b80SPaolo Abeni icsk_retransmit_timer); 2046b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 2047b51f9b80SPaolo Abeni 2048b51f9b80SPaolo Abeni bh_lock_sock(sk); 2049b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 2050b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 2051b51f9b80SPaolo Abeni } else { 2052b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 2053b51f9b80SPaolo Abeni if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 2054b51f9b80SPaolo Abeni &sk->sk_tsq_flags)) 2055b51f9b80SPaolo Abeni sock_hold(sk); 2056b51f9b80SPaolo Abeni } 2057b51f9b80SPaolo Abeni bh_unlock_sock(sk); 2058b51f9b80SPaolo Abeni sock_put(sk); 2059b51f9b80SPaolo Abeni } 2060b51f9b80SPaolo Abeni 2061e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t) 2062e16163b6SPaolo Abeni { 2063e16163b6SPaolo Abeni struct sock *sk = from_timer(sk, t, sk_timer); 2064e16163b6SPaolo Abeni 2065e16163b6SPaolo Abeni mptcp_schedule_work(sk); 2066b6d69fc8SFlorian Westphal sock_put(sk); 2067e16163b6SPaolo Abeni } 2068e16163b6SPaolo Abeni 20693b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 20703b1d6210SPaolo Abeni * level. 20713b1d6210SPaolo Abeni * 20723b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 20733b1d6210SPaolo Abeni */ 20743b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 20753b1d6210SPaolo Abeni { 20763b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 20773b1d6210SPaolo Abeni struct sock *backup = NULL; 20783b1d6210SPaolo Abeni 20793b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 20803b1d6210SPaolo Abeni 2081d5f49190SPaolo Abeni if (__mptcp_check_fallback(msk)) 2082d9ca1de8SPaolo Abeni return NULL; 2083d5f49190SPaolo Abeni 20843b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 20853b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 20863b1d6210SPaolo Abeni 2087d5f49190SPaolo Abeni if (!mptcp_subflow_active(subflow)) 2088d5f49190SPaolo Abeni continue; 2089d5f49190SPaolo Abeni 20903b1d6210SPaolo Abeni /* still data outstanding at TCP level? Don't retransmit. */ 2091860975c6SFlorian Westphal if (!tcp_write_queue_empty(ssk)) { 2092860975c6SFlorian Westphal if (inet_csk(ssk)->icsk_ca_state >= TCP_CA_Loss) 2093860975c6SFlorian Westphal continue; 20943b1d6210SPaolo Abeni return NULL; 2095860975c6SFlorian Westphal } 20963b1d6210SPaolo Abeni 20973b1d6210SPaolo Abeni if (subflow->backup) { 20983b1d6210SPaolo Abeni if (!backup) 20993b1d6210SPaolo Abeni backup = ssk; 21003b1d6210SPaolo Abeni continue; 21013b1d6210SPaolo Abeni } 21023b1d6210SPaolo Abeni 21033b1d6210SPaolo Abeni return ssk; 21043b1d6210SPaolo Abeni } 21053b1d6210SPaolo Abeni 21063b1d6210SPaolo Abeni return backup; 21073b1d6210SPaolo Abeni } 21083b1d6210SPaolo Abeni 2109cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 2110cec37a6eSPeter Krystad * (accept). 2111cec37a6eSPeter Krystad * 2112cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 2113cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 2114cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 2115cec37a6eSPeter Krystad * parent socket. 2116cec37a6eSPeter Krystad */ 2117d0876b22SGeliang Tang void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2118e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow) 2119cec37a6eSPeter Krystad { 2120cec37a6eSPeter Krystad list_del(&subflow->node); 2121cec37a6eSPeter Krystad 21223f8b2667SPaolo Abeni lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2123e16163b6SPaolo Abeni 2124e16163b6SPaolo Abeni /* if we are invoked by the msk cleanup code, the subflow is 2125e16163b6SPaolo Abeni * already orphaned 2126e16163b6SPaolo Abeni */ 2127866f26f2SPaolo Abeni if (ssk->sk_socket) 2128e16163b6SPaolo Abeni sock_orphan(ssk); 2129e16163b6SPaolo Abeni 2130d7b1bfd0SPaolo Abeni subflow->disposable = 1; 2131d7b1bfd0SPaolo Abeni 2132e16163b6SPaolo Abeni /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2133e16163b6SPaolo Abeni * the ssk has been already destroyed, we just need to release the 2134e16163b6SPaolo Abeni * reference owned by msk; 2135e16163b6SPaolo Abeni */ 2136e16163b6SPaolo Abeni if (!inet_csk(ssk)->icsk_ulp_ops) { 2137e16163b6SPaolo Abeni kfree_rcu(subflow, rcu); 2138e16163b6SPaolo Abeni } else { 2139d7b1bfd0SPaolo Abeni /* otherwise tcp will dispose of the ssk and subflow ctx */ 2140e16163b6SPaolo Abeni __tcp_close(ssk, 0); 2141e16163b6SPaolo Abeni 2142e16163b6SPaolo Abeni /* close acquired an extra ref */ 2143e16163b6SPaolo Abeni __sock_put(ssk); 2144e16163b6SPaolo Abeni } 2145e16163b6SPaolo Abeni release_sock(ssk); 2146e16163b6SPaolo Abeni 2147e16163b6SPaolo Abeni sock_put(ssk); 2148f870fa0bSMat Martineau } 2149f870fa0bSMat Martineau 2150dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2151dc24f8b4SPaolo Abeni { 2152dc24f8b4SPaolo Abeni return 0; 2153dc24f8b4SPaolo Abeni } 2154dc24f8b4SPaolo Abeni 2155b416268bSFlorian Westphal static void pm_work(struct mptcp_sock *msk) 2156b416268bSFlorian Westphal { 2157b416268bSFlorian Westphal struct mptcp_pm_data *pm = &msk->pm; 2158b416268bSFlorian Westphal 2159b416268bSFlorian Westphal spin_lock_bh(&msk->pm.lock); 2160b416268bSFlorian Westphal 2161b416268bSFlorian Westphal pr_debug("msk=%p status=%x", msk, pm->status); 2162b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 2163b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 2164b416268bSFlorian Westphal mptcp_pm_nl_add_addr_received(msk); 2165b416268bSFlorian Westphal } 216684dfe367SGeliang Tang if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) { 216784dfe367SGeliang Tang pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK); 216884dfe367SGeliang Tang mptcp_pm_nl_add_addr_send_ack(msk); 216984dfe367SGeliang Tang } 2170d0876b22SGeliang Tang if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) { 2171d0876b22SGeliang Tang pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED); 2172d0876b22SGeliang Tang mptcp_pm_nl_rm_addr_received(msk); 2173d0876b22SGeliang Tang } 2174b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 2175b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 2176b416268bSFlorian Westphal mptcp_pm_nl_fully_established(msk); 2177b416268bSFlorian Westphal } 2178b416268bSFlorian Westphal if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 2179b416268bSFlorian Westphal pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 2180b416268bSFlorian Westphal mptcp_pm_nl_subflow_established(msk); 2181b416268bSFlorian Westphal } 2182b416268bSFlorian Westphal 2183b416268bSFlorian Westphal spin_unlock_bh(&msk->pm.lock); 2184b416268bSFlorian Westphal } 2185b416268bSFlorian Westphal 21860e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk) 21870e4f35d7SPaolo Abeni { 21880e4f35d7SPaolo Abeni struct mptcp_subflow_context *subflow, *tmp; 21890e4f35d7SPaolo Abeni 21900e4f35d7SPaolo Abeni list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 21910e4f35d7SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 21920e4f35d7SPaolo Abeni 21930e4f35d7SPaolo Abeni if (inet_sk_state_load(ssk) != TCP_CLOSE) 21940e4f35d7SPaolo Abeni continue; 21950e4f35d7SPaolo Abeni 2196e16163b6SPaolo Abeni __mptcp_close_ssk((struct sock *)msk, ssk, subflow); 21970e4f35d7SPaolo Abeni } 21980e4f35d7SPaolo Abeni } 21990e4f35d7SPaolo Abeni 2200e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk) 2201e16163b6SPaolo Abeni { 2202e16163b6SPaolo Abeni s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2203e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2204e16163b6SPaolo Abeni 2205e16163b6SPaolo Abeni if (delta >= TCP_TIMEWAIT_LEN) 2206e16163b6SPaolo Abeni return true; 2207e16163b6SPaolo Abeni 2208e16163b6SPaolo Abeni /* if all subflows are in closed status don't bother with additional 2209e16163b6SPaolo Abeni * timeout 2210e16163b6SPaolo Abeni */ 2211e16163b6SPaolo Abeni mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2212e16163b6SPaolo Abeni if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2213e16163b6SPaolo Abeni TCP_CLOSE) 2214e16163b6SPaolo Abeni return false; 2215e16163b6SPaolo Abeni } 2216e16163b6SPaolo Abeni return true; 2217e16163b6SPaolo Abeni } 2218e16163b6SPaolo Abeni 221950c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk) 222050c504a2SFlorian Westphal { 222150c504a2SFlorian Westphal struct mptcp_subflow_context *subflow, *tmp; 222250c504a2SFlorian Westphal struct sock *sk = &msk->sk.icsk_inet.sk; 222350c504a2SFlorian Westphal 222450c504a2SFlorian Westphal if (likely(!READ_ONCE(msk->rcv_fastclose))) 222550c504a2SFlorian Westphal return; 222650c504a2SFlorian Westphal 222750c504a2SFlorian Westphal mptcp_token_destroy(msk); 222850c504a2SFlorian Westphal 222950c504a2SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) { 223050c504a2SFlorian Westphal struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 223150c504a2SFlorian Westphal 223250c504a2SFlorian Westphal lock_sock(tcp_sk); 223350c504a2SFlorian Westphal if (tcp_sk->sk_state != TCP_CLOSE) { 223450c504a2SFlorian Westphal tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 223550c504a2SFlorian Westphal tcp_set_state(tcp_sk, TCP_CLOSE); 223650c504a2SFlorian Westphal } 223750c504a2SFlorian Westphal release_sock(tcp_sk); 223850c504a2SFlorian Westphal } 223950c504a2SFlorian Westphal 224050c504a2SFlorian Westphal inet_sk_state_store(sk, TCP_CLOSE); 224150c504a2SFlorian Westphal sk->sk_shutdown = SHUTDOWN_MASK; 224250c504a2SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 224350c504a2SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 224450c504a2SFlorian Westphal set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 224550c504a2SFlorian Westphal 224650c504a2SFlorian Westphal mptcp_close_wake_up(sk); 224750c504a2SFlorian Westphal } 224850c504a2SFlorian Westphal 224980992017SPaolo Abeni static void mptcp_worker(struct work_struct *work) 225080992017SPaolo Abeni { 225180992017SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 22523b1d6210SPaolo Abeni struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 2253caf971dfSPaolo Abeni struct mptcp_sendmsg_info info = {}; 22543b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 22553b1d6210SPaolo Abeni size_t copied = 0; 2256e16163b6SPaolo Abeni int state, ret; 225780992017SPaolo Abeni 225880992017SPaolo Abeni lock_sock(sk); 2259e16163b6SPaolo Abeni state = sk->sk_state; 2260e16163b6SPaolo Abeni if (unlikely(state == TCP_CLOSE)) 2261e16163b6SPaolo Abeni goto unlock; 2262e16163b6SPaolo Abeni 226343b54c6eSMat Martineau mptcp_check_data_fin_ack(sk); 2264ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 226550c504a2SFlorian Westphal 226650c504a2SFlorian Westphal mptcp_check_fastclose(msk); 226750c504a2SFlorian Westphal 22680e4f35d7SPaolo Abeni if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 22690e4f35d7SPaolo Abeni __mptcp_close_subflow(msk); 22700e4f35d7SPaolo Abeni 2271b416268bSFlorian Westphal if (msk->pm.status) 2272b416268bSFlorian Westphal pm_work(msk); 2273b416268bSFlorian Westphal 227459832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 227559832e24SFlorian Westphal mptcp_check_for_eof(msk); 227659832e24SFlorian Westphal 22776e628cd3SPaolo Abeni __mptcp_check_send_data_fin(sk); 227843b54c6eSMat Martineau mptcp_check_data_fin(sk); 227943b54c6eSMat Martineau 2280e16163b6SPaolo Abeni /* if the msk data is completely acked, or the socket timedout, 2281e16163b6SPaolo Abeni * there is no point in keeping around an orphaned sk 2282e16163b6SPaolo Abeni */ 2283e16163b6SPaolo Abeni if (sock_flag(sk, SOCK_DEAD) && 2284e16163b6SPaolo Abeni (mptcp_check_close_timeout(sk) || 2285e16163b6SPaolo Abeni (state != sk->sk_state && 2286e16163b6SPaolo Abeni ((1 << inet_sk_state_load(sk)) & (TCPF_CLOSE | TCPF_FIN_WAIT2))))) { 2287e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2288e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2289e16163b6SPaolo Abeni goto unlock; 2290e16163b6SPaolo Abeni } 2291e16163b6SPaolo Abeni 22923b1d6210SPaolo Abeni if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 22933b1d6210SPaolo Abeni goto unlock; 22943b1d6210SPaolo Abeni 22953b1d6210SPaolo Abeni dfrag = mptcp_rtx_head(sk); 22963b1d6210SPaolo Abeni if (!dfrag) 22973b1d6210SPaolo Abeni goto unlock; 22983b1d6210SPaolo Abeni 22993b1d6210SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 23003b1d6210SPaolo Abeni if (!ssk) 23013b1d6210SPaolo Abeni goto reset_unlock; 23023b1d6210SPaolo Abeni 23033b1d6210SPaolo Abeni lock_sock(ssk); 23043b1d6210SPaolo Abeni 2305d9ca1de8SPaolo Abeni /* limit retransmission to the bytes already sent on some subflows */ 2306d9ca1de8SPaolo Abeni info.sent = 0; 2307d9ca1de8SPaolo Abeni info.limit = dfrag->already_sent; 2308d9ca1de8SPaolo Abeni while (info.sent < dfrag->already_sent) { 2309724cfd2eSPaolo Abeni if (!mptcp_alloc_tx_skb(sk, ssk)) 2310724cfd2eSPaolo Abeni break; 2311724cfd2eSPaolo Abeni 2312d9ca1de8SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 23136f8a612aSFlorian Westphal if (ret <= 0) 23143b1d6210SPaolo Abeni break; 23153b1d6210SPaolo Abeni 2316fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 23173b1d6210SPaolo Abeni copied += ret; 2318d9ca1de8SPaolo Abeni info.sent += ret; 23193b1d6210SPaolo Abeni } 23203b1d6210SPaolo Abeni if (copied) 2321caf971dfSPaolo Abeni tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 2322caf971dfSPaolo Abeni info.size_goal); 23233b1d6210SPaolo Abeni 23243b1d6210SPaolo Abeni mptcp_set_timeout(sk, ssk); 23253b1d6210SPaolo Abeni release_sock(ssk); 23263b1d6210SPaolo Abeni 23273b1d6210SPaolo Abeni reset_unlock: 23283b1d6210SPaolo Abeni if (!mptcp_timer_pending(sk)) 23293b1d6210SPaolo Abeni mptcp_reset_timer(sk); 23303b1d6210SPaolo Abeni 23313b1d6210SPaolo Abeni unlock: 233280992017SPaolo Abeni release_sock(sk); 233380992017SPaolo Abeni sock_put(sk); 233480992017SPaolo Abeni } 233580992017SPaolo Abeni 2336784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 2337f870fa0bSMat Martineau { 2338cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2339cec37a6eSPeter Krystad 2340ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 2341ec3edaa7SPeter Krystad 2342cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 2343ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 234418b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 234580992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 234687952603SPaolo Abeni __skb_queue_head_init(&msk->receive_queue); 2347724cfd2eSPaolo Abeni __skb_queue_head_init(&msk->skb_tx_cache); 2348ab174ad8SPaolo Abeni msk->out_of_order_queue = RB_ROOT; 2349f0e6a4cfSPaolo Abeni msk->first_pending = NULL; 2350e93da928SPaolo Abeni msk->wmem_reserved = 0; 235187952603SPaolo Abeni msk->rmem_released = 0; 2352724cfd2eSPaolo Abeni msk->tx_pending_data = 0; 2353724cfd2eSPaolo Abeni msk->size_goal_cache = TCP_BASE_MSS; 2354cec37a6eSPeter Krystad 2355ea4ca586SPaolo Abeni msk->ack_hint = NULL; 23568ab183deSPaolo Abeni msk->first = NULL; 2357dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 23588ab183deSPaolo Abeni 23591b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 23601b1c7a0eSPeter Krystad 2361b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 2362b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2363e16163b6SPaolo Abeni timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2364f870fa0bSMat Martineau return 0; 2365f870fa0bSMat Martineau } 2366f870fa0bSMat Martineau 2367784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 2368784325e9SMatthieu Baerts { 2369fc518953SFlorian Westphal struct net *net = sock_net(sk); 2370fc518953SFlorian Westphal int ret; 237118b683bfSPaolo Abeni 2372b6c08380SGeliang Tang ret = __mptcp_init_sock(sk); 2373b6c08380SGeliang Tang if (ret) 2374b6c08380SGeliang Tang return ret; 2375b6c08380SGeliang Tang 2376fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 2377fc518953SFlorian Westphal return -ENOPROTOOPT; 2378fc518953SFlorian Westphal 2379fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2380fc518953SFlorian Westphal return -ENOMEM; 2381fc518953SFlorian Westphal 2382fa68018dSPaolo Abeni ret = __mptcp_socket_create(mptcp_sk(sk)); 2383fa68018dSPaolo Abeni if (ret) 2384fa68018dSPaolo Abeni return ret; 2385fa68018dSPaolo Abeni 2386d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 2387a6b118feSFlorian Westphal sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2388da51aef5SPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2389d027236cSPaolo Abeni 239018b683bfSPaolo Abeni return 0; 239118b683bfSPaolo Abeni } 239218b683bfSPaolo Abeni 239318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 239418b683bfSPaolo Abeni { 239518b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 239618b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 2397724cfd2eSPaolo Abeni struct sk_buff *skb; 239818b683bfSPaolo Abeni 2399d9ca1de8SPaolo Abeni WRITE_ONCE(msk->first_pending, NULL); 240018b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2401d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 2402724cfd2eSPaolo Abeni while ((skb = __skb_dequeue(&msk->skb_tx_cache)) != NULL) { 2403724cfd2eSPaolo Abeni sk->sk_forward_alloc += skb->truesize; 2404724cfd2eSPaolo Abeni kfree_skb(skb); 2405724cfd2eSPaolo Abeni } 2406784325e9SMatthieu Baerts } 2407784325e9SMatthieu Baerts 240880992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 240980992017SPaolo Abeni { 241080992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 241180992017SPaolo Abeni 2412b2771d24SPaolo Abeni if (cancel_work_sync(&msk->work)) 2413e16163b6SPaolo Abeni __sock_put(sk); 241480992017SPaolo Abeni } 241580992017SPaolo Abeni 2416d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 241721498490SPeter Krystad { 241821498490SPeter Krystad lock_sock(ssk); 241921498490SPeter Krystad 242021498490SPeter Krystad switch (ssk->sk_state) { 242121498490SPeter Krystad case TCP_LISTEN: 242221498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 242321498490SPeter Krystad break; 2424df561f66SGustavo A. R. Silva fallthrough; 242521498490SPeter Krystad case TCP_SYN_SENT: 242621498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 242721498490SPeter Krystad break; 242821498490SPeter Krystad default: 242943b54c6eSMat Martineau if (__mptcp_check_fallback(mptcp_sk(sk))) { 243043b54c6eSMat Martineau pr_debug("Fallback"); 243121498490SPeter Krystad ssk->sk_shutdown |= how; 243221498490SPeter Krystad tcp_shutdown(ssk, how); 243343b54c6eSMat Martineau } else { 243443b54c6eSMat Martineau pr_debug("Sending DATA_FIN on subflow %p", ssk); 243543b54c6eSMat Martineau mptcp_set_timeout(sk, ssk); 243643b54c6eSMat Martineau tcp_send_ack(ssk); 243743b54c6eSMat Martineau } 243821498490SPeter Krystad break; 243921498490SPeter Krystad } 244021498490SPeter Krystad 244121498490SPeter Krystad release_sock(ssk); 244221498490SPeter Krystad } 244321498490SPeter Krystad 24446920b851SMat Martineau static const unsigned char new_state[16] = { 24456920b851SMat Martineau /* current state: new state: action: */ 24466920b851SMat Martineau [0 /* (Invalid) */] = TCP_CLOSE, 24476920b851SMat Martineau [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 24486920b851SMat Martineau [TCP_SYN_SENT] = TCP_CLOSE, 24496920b851SMat Martineau [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 24506920b851SMat Martineau [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 24516920b851SMat Martineau [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 24526920b851SMat Martineau [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 24536920b851SMat Martineau [TCP_CLOSE] = TCP_CLOSE, 24546920b851SMat Martineau [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 24556920b851SMat Martineau [TCP_LAST_ACK] = TCP_LAST_ACK, 24566920b851SMat Martineau [TCP_LISTEN] = TCP_CLOSE, 24576920b851SMat Martineau [TCP_CLOSING] = TCP_CLOSING, 24586920b851SMat Martineau [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 24596920b851SMat Martineau }; 24606920b851SMat Martineau 24616920b851SMat Martineau static int mptcp_close_state(struct sock *sk) 24626920b851SMat Martineau { 24636920b851SMat Martineau int next = (int)new_state[sk->sk_state]; 24646920b851SMat Martineau int ns = next & TCP_STATE_MASK; 24656920b851SMat Martineau 24666920b851SMat Martineau inet_sk_state_store(sk, ns); 24676920b851SMat Martineau 24686920b851SMat Martineau return next & TCP_ACTION_FIN; 24696920b851SMat Martineau } 24706920b851SMat Martineau 2471e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk) 2472e16163b6SPaolo Abeni { 2473e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2474e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2475e16163b6SPaolo Abeni 2476e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2477e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2478e16163b6SPaolo Abeni msk->snd_nxt, msk->write_seq); 2479e16163b6SPaolo Abeni 2480e16163b6SPaolo Abeni /* we still need to enqueue subflows or not really shutting down, 2481e16163b6SPaolo Abeni * skip this 2482e16163b6SPaolo Abeni */ 2483e16163b6SPaolo Abeni if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2484e16163b6SPaolo Abeni mptcp_send_head(sk)) 2485e16163b6SPaolo Abeni return; 2486e16163b6SPaolo Abeni 2487e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2488e16163b6SPaolo Abeni 248926aa2314SPaolo Abeni /* fallback socket will not get data_fin/ack, can move to the next 249026aa2314SPaolo Abeni * state now 249126aa2314SPaolo Abeni */ 249226aa2314SPaolo Abeni if (__mptcp_check_fallback(msk)) { 249326aa2314SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2494e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2495e16163b6SPaolo Abeni mptcp_close_wake_up(sk); 249626aa2314SPaolo Abeni } else if (sk->sk_state == TCP_FIN_WAIT1) { 249726aa2314SPaolo Abeni inet_sk_state_store(sk, TCP_FIN_WAIT2); 249826aa2314SPaolo Abeni } 2499e16163b6SPaolo Abeni } 2500e16163b6SPaolo Abeni 2501e16163b6SPaolo Abeni __mptcp_flush_join_list(msk); 2502e16163b6SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 2503e16163b6SPaolo Abeni struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2504e16163b6SPaolo Abeni 2505e16163b6SPaolo Abeni mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2506e16163b6SPaolo Abeni } 2507e16163b6SPaolo Abeni } 2508e16163b6SPaolo Abeni 2509e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk) 2510e16163b6SPaolo Abeni { 2511e16163b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 2512e16163b6SPaolo Abeni 2513e16163b6SPaolo Abeni pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2514e16163b6SPaolo Abeni msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2515e16163b6SPaolo Abeni !!mptcp_send_head(sk)); 2516e16163b6SPaolo Abeni 2517e16163b6SPaolo Abeni /* will be ignored by fallback sockets */ 2518e16163b6SPaolo Abeni WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2519e16163b6SPaolo Abeni WRITE_ONCE(msk->snd_data_fin_enable, 1); 2520e16163b6SPaolo Abeni 2521e16163b6SPaolo Abeni __mptcp_check_send_data_fin(sk); 2522e16163b6SPaolo Abeni } 2523e16163b6SPaolo Abeni 2524e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk) 2525f870fa0bSMat Martineau { 2526cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 2527f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 2528b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 2529f870fa0bSMat Martineau 2530e16163b6SPaolo Abeni pr_debug("msk=%p", msk); 25312c22c06cSFlorian Westphal 2532866f26f2SPaolo Abeni /* dispose the ancillatory tcp socket, if any */ 2533866f26f2SPaolo Abeni if (msk->subflow) { 2534866f26f2SPaolo Abeni iput(SOCK_INODE(msk->subflow)); 2535866f26f2SPaolo Abeni msk->subflow = NULL; 2536866f26f2SPaolo Abeni } 2537866f26f2SPaolo Abeni 253810f6d46cSPaolo Abeni /* be sure to always acquire the join list lock, to sync vs 253910f6d46cSPaolo Abeni * mptcp_finish_join(). 254010f6d46cSPaolo Abeni */ 254110f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 254210f6d46cSPaolo Abeni list_splice_tail_init(&msk->join_list, &msk->conn_list); 254310f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 2544b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 2545b2c5b614SFlorian Westphal 25466e628cd3SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2547e16163b6SPaolo Abeni sk_stop_timer(sk, &sk->sk_timer); 2548e16163b6SPaolo Abeni msk->pm.status = 0; 2549b2c5b614SFlorian Westphal 2550b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2551cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2552e16163b6SPaolo Abeni __mptcp_close_ssk(sk, ssk, subflow); 2553f870fa0bSMat Martineau } 2554f870fa0bSMat Martineau 2555e16163b6SPaolo Abeni sk->sk_prot->destroy(sk); 2556e16163b6SPaolo Abeni 2557e93da928SPaolo Abeni WARN_ON_ONCE(msk->wmem_reserved); 255887952603SPaolo Abeni WARN_ON_ONCE(msk->rmem_released); 2559e16163b6SPaolo Abeni sk_stream_kill_queues(sk); 2560e16163b6SPaolo Abeni xfrm_sk_free_policy(sk); 2561e16163b6SPaolo Abeni sk_refcnt_debug_release(sk); 2562e16163b6SPaolo Abeni sock_put(sk); 2563e16163b6SPaolo Abeni } 2564e16163b6SPaolo Abeni 2565e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout) 2566e16163b6SPaolo Abeni { 2567e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow; 2568e16163b6SPaolo Abeni bool do_cancel_work = false; 2569e16163b6SPaolo Abeni 2570e16163b6SPaolo Abeni lock_sock(sk); 2571e16163b6SPaolo Abeni sk->sk_shutdown = SHUTDOWN_MASK; 2572e16163b6SPaolo Abeni 2573e16163b6SPaolo Abeni if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2574e16163b6SPaolo Abeni inet_sk_state_store(sk, TCP_CLOSE); 2575e16163b6SPaolo Abeni goto cleanup; 2576e16163b6SPaolo Abeni } 2577e16163b6SPaolo Abeni 2578e16163b6SPaolo Abeni if (mptcp_close_state(sk)) 2579e16163b6SPaolo Abeni __mptcp_wr_shutdown(sk); 2580e16163b6SPaolo Abeni 2581e16163b6SPaolo Abeni sk_stream_wait_close(sk, timeout); 2582e16163b6SPaolo Abeni 2583e16163b6SPaolo Abeni cleanup: 2584e16163b6SPaolo Abeni /* orphan all the subflows */ 2585e16163b6SPaolo Abeni inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2586e16163b6SPaolo Abeni list_for_each_entry(subflow, &mptcp_sk(sk)->conn_list, node) { 2587e16163b6SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2588866f26f2SPaolo Abeni bool slow = lock_sock_fast(ssk); 2589e16163b6SPaolo Abeni 2590e16163b6SPaolo Abeni sock_orphan(ssk); 2591e16163b6SPaolo Abeni unlock_sock_fast(ssk, slow); 2592e16163b6SPaolo Abeni } 2593e16163b6SPaolo Abeni sock_orphan(sk); 2594e16163b6SPaolo Abeni 2595e16163b6SPaolo Abeni sock_hold(sk); 2596e16163b6SPaolo Abeni pr_debug("msk=%p state=%d", sk, sk->sk_state); 2597e16163b6SPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 2598e16163b6SPaolo Abeni __mptcp_destroy_sock(sk); 2599e16163b6SPaolo Abeni do_cancel_work = true; 2600e16163b6SPaolo Abeni } else { 2601e16163b6SPaolo Abeni sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2602e16163b6SPaolo Abeni } 2603e16163b6SPaolo Abeni release_sock(sk); 2604e16163b6SPaolo Abeni if (do_cancel_work) 260580992017SPaolo Abeni mptcp_cancel_work(sk); 2606e16163b6SPaolo Abeni sock_put(sk); 2607f870fa0bSMat Martineau } 2608f870fa0bSMat Martineau 2609cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2610cf7da0d6SPeter Krystad { 2611cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2612cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2613cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 2614cf7da0d6SPeter Krystad 2615cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 2616cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2617cf7da0d6SPeter Krystad 2618cf7da0d6SPeter Krystad if (msk6 && ssk6) { 2619cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 2620cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 2621cf7da0d6SPeter Krystad } 2622cf7da0d6SPeter Krystad #endif 2623cf7da0d6SPeter Krystad 2624cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2625cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2626cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2627cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2628cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2629cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2630cf7da0d6SPeter Krystad } 2631cf7da0d6SPeter Krystad 263218b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 263318b683bfSPaolo Abeni { 263476e2a55dSPaolo Abeni struct mptcp_subflow_context *subflow; 263576e2a55dSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 263676e2a55dSPaolo Abeni 263776e2a55dSPaolo Abeni __mptcp_flush_join_list(msk); 263813a9499eSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 263913a9499eSPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 264013a9499eSPaolo Abeni 264113a9499eSPaolo Abeni lock_sock(ssk); 264213a9499eSPaolo Abeni tcp_disconnect(ssk, flags); 264313a9499eSPaolo Abeni release_sock(ssk); 264413a9499eSPaolo Abeni } 264542c556feSFlorian Westphal return 0; 264618b683bfSPaolo Abeni } 264718b683bfSPaolo Abeni 2648b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2649b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2650b0519de8SFlorian Westphal { 2651b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2652b0519de8SFlorian Westphal 2653b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2654b0519de8SFlorian Westphal } 2655b0519de8SFlorian Westphal #endif 2656b0519de8SFlorian Westphal 2657fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 2658cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 2659fca5c82cSPaolo Abeni struct request_sock *req) 2660b0519de8SFlorian Westphal { 266158b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2662b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 266358b09919SPaolo Abeni struct mptcp_sock *msk; 266458b09919SPaolo Abeni u64 ack_seq; 2665b0519de8SFlorian Westphal 2666b0519de8SFlorian Westphal if (!nsk) 2667b0519de8SFlorian Westphal return NULL; 2668b0519de8SFlorian Westphal 2669b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2670b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 2671b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2672b0519de8SFlorian Westphal #endif 2673b0519de8SFlorian Westphal 267458b09919SPaolo Abeni __mptcp_init_sock(nsk); 267558b09919SPaolo Abeni 267658b09919SPaolo Abeni msk = mptcp_sk(nsk); 267758b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 267858b09919SPaolo Abeni msk->token = subflow_req->token; 267958b09919SPaolo Abeni msk->subflow = NULL; 2680b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, false); 268158b09919SPaolo Abeni 268258b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 2683eaa2ffabSPaolo Abeni msk->snd_nxt = msk->write_seq; 26847439d687SPaolo Abeni msk->snd_una = msk->write_seq; 26857439d687SPaolo Abeni msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 26866f8a612aSFlorian Westphal 2687cfde141eSPaolo Abeni if (mp_opt->mp_capable) { 268858b09919SPaolo Abeni msk->can_ack = true; 2689cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 269058b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 269158b09919SPaolo Abeni ack_seq++; 2692917944daSMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 2693fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 269458b09919SPaolo Abeni } 26957f20d5fcSPaolo Abeni 26965e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 26977f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 26987f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 26990c148460SPaolo Abeni 27000c148460SPaolo Abeni security_inet_csk_clone(nsk, req); 270158b09919SPaolo Abeni bh_unlock_sock(nsk); 270258b09919SPaolo Abeni 270358b09919SPaolo Abeni /* keep a single reference */ 270458b09919SPaolo Abeni __sock_put(nsk); 2705b0519de8SFlorian Westphal return nsk; 2706b0519de8SFlorian Westphal } 2707b0519de8SFlorian Westphal 2708a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2709a6b118feSFlorian Westphal { 2710a6b118feSFlorian Westphal const struct tcp_sock *tp = tcp_sk(ssk); 2711a6b118feSFlorian Westphal 2712a6b118feSFlorian Westphal msk->rcvq_space.copied = 0; 2713a6b118feSFlorian Westphal msk->rcvq_space.rtt_us = 0; 2714a6b118feSFlorian Westphal 2715a6b118feSFlorian Westphal msk->rcvq_space.time = tp->tcp_mstamp; 2716a6b118feSFlorian Westphal 2717a6b118feSFlorian Westphal /* initial rcv_space offering made to peer */ 2718a6b118feSFlorian Westphal msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2719a6b118feSFlorian Westphal TCP_INIT_CWND * tp->advmss); 2720a6b118feSFlorian Westphal if (msk->rcvq_space.space == 0) 2721a6b118feSFlorian Westphal msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 27226f8a612aSFlorian Westphal 27237439d687SPaolo Abeni WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2724a6b118feSFlorian Westphal } 2725a6b118feSFlorian Westphal 2726cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2727cf7da0d6SPeter Krystad bool kern) 2728cf7da0d6SPeter Krystad { 2729cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 2730cf7da0d6SPeter Krystad struct socket *listener; 2731cf7da0d6SPeter Krystad struct sock *newsk; 2732cf7da0d6SPeter Krystad 2733cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 2734cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 2735cf7da0d6SPeter Krystad *err = -EINVAL; 2736cf7da0d6SPeter Krystad return NULL; 2737cf7da0d6SPeter Krystad } 2738cf7da0d6SPeter Krystad 2739cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2740cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 2741cf7da0d6SPeter Krystad if (!newsk) 2742cf7da0d6SPeter Krystad return NULL; 2743cf7da0d6SPeter Krystad 2744cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2745cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 2746cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 2747cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 2748cf7da0d6SPeter Krystad 2749cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 275058b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 275158b09919SPaolo Abeni 275258b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 275358b09919SPaolo Abeni * subflow_syn_recv_sock() 275458b09919SPaolo Abeni */ 275558b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 275658b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 275758b09919SPaolo Abeni return newsk; 275858b09919SPaolo Abeni } 275958b09919SPaolo Abeni 276058b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 276158b09919SPaolo Abeni sock_hold(new_mptcp_sock); 2762cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 27630397c6d8SPaolo Abeni MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2764fc518953SFlorian Westphal } else { 2765fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 2766fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2767cf7da0d6SPeter Krystad } 2768cf7da0d6SPeter Krystad 2769cf7da0d6SPeter Krystad return newsk; 2770cf7da0d6SPeter Krystad } 2771cf7da0d6SPeter Krystad 27725c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk) 27735c8c1640SGeliang Tang { 277487952603SPaolo Abeni struct sock *sk = (struct sock *)msk; 277587952603SPaolo Abeni 27766e628cd3SPaolo Abeni __mptcp_clear_xmit(sk); 27776e628cd3SPaolo Abeni 277887952603SPaolo Abeni /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 277987952603SPaolo Abeni skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 278087952603SPaolo Abeni 27815c8c1640SGeliang Tang skb_rbtree_purge(&msk->out_of_order_queue); 27825c8c1640SGeliang Tang mptcp_token_destroy(msk); 27835c8c1640SGeliang Tang mptcp_pm_free_anno_list(msk); 27845c8c1640SGeliang Tang } 27855c8c1640SGeliang Tang 278679c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 278779c0949eSPeter Krystad { 2788c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 2789c9fd9c5fSFlorian Westphal 27905c8c1640SGeliang Tang mptcp_destroy_common(msk); 2791d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 279279c0949eSPeter Krystad } 279379c0949eSPeter Krystad 2794fd1452d8SFlorian Westphal static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname, 2795a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2796fd1452d8SFlorian Westphal { 2797fd1452d8SFlorian Westphal struct sock *sk = (struct sock *)msk; 2798fd1452d8SFlorian Westphal struct socket *ssock; 2799fd1452d8SFlorian Westphal int ret; 2800fd1452d8SFlorian Westphal 2801fd1452d8SFlorian Westphal switch (optname) { 2802fd1452d8SFlorian Westphal case SO_REUSEPORT: 2803fd1452d8SFlorian Westphal case SO_REUSEADDR: 2804fd1452d8SFlorian Westphal lock_sock(sk); 2805fd1452d8SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2806fd1452d8SFlorian Westphal if (!ssock) { 2807fd1452d8SFlorian Westphal release_sock(sk); 2808fd1452d8SFlorian Westphal return -EINVAL; 2809fd1452d8SFlorian Westphal } 2810fd1452d8SFlorian Westphal 2811a7b75c5aSChristoph Hellwig ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen); 2812fd1452d8SFlorian Westphal if (ret == 0) { 2813fd1452d8SFlorian Westphal if (optname == SO_REUSEPORT) 2814fd1452d8SFlorian Westphal sk->sk_reuseport = ssock->sk->sk_reuseport; 2815fd1452d8SFlorian Westphal else if (optname == SO_REUSEADDR) 2816fd1452d8SFlorian Westphal sk->sk_reuse = ssock->sk->sk_reuse; 2817fd1452d8SFlorian Westphal } 2818fd1452d8SFlorian Westphal release_sock(sk); 2819fd1452d8SFlorian Westphal return ret; 2820fd1452d8SFlorian Westphal } 2821fd1452d8SFlorian Westphal 2822a7b75c5aSChristoph Hellwig return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen); 2823fd1452d8SFlorian Westphal } 2824fd1452d8SFlorian Westphal 2825c9b95a13SFlorian Westphal static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname, 2826a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2827c9b95a13SFlorian Westphal { 2828c9b95a13SFlorian Westphal struct sock *sk = (struct sock *)msk; 2829c9b95a13SFlorian Westphal int ret = -EOPNOTSUPP; 2830c9b95a13SFlorian Westphal struct socket *ssock; 2831c9b95a13SFlorian Westphal 2832c9b95a13SFlorian Westphal switch (optname) { 2833c9b95a13SFlorian Westphal case IPV6_V6ONLY: 2834c9b95a13SFlorian Westphal lock_sock(sk); 2835c9b95a13SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 2836c9b95a13SFlorian Westphal if (!ssock) { 2837c9b95a13SFlorian Westphal release_sock(sk); 2838c9b95a13SFlorian Westphal return -EINVAL; 2839c9b95a13SFlorian Westphal } 2840c9b95a13SFlorian Westphal 2841c9b95a13SFlorian Westphal ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen); 2842c9b95a13SFlorian Westphal if (ret == 0) 2843c9b95a13SFlorian Westphal sk->sk_ipv6only = ssock->sk->sk_ipv6only; 2844c9b95a13SFlorian Westphal 2845c9b95a13SFlorian Westphal release_sock(sk); 2846c9b95a13SFlorian Westphal break; 2847c9b95a13SFlorian Westphal } 2848c9b95a13SFlorian Westphal 2849c9b95a13SFlorian Westphal return ret; 2850c9b95a13SFlorian Westphal } 2851c9b95a13SFlorian Westphal 2852717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname, 2853a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 2854717e79c8SPeter Krystad { 2855717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 285676660afbSPaolo Abeni struct sock *ssk; 2857717e79c8SPeter Krystad 2858717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2859717e79c8SPeter Krystad 286083f0c10bSFlorian Westphal if (level == SOL_SOCKET) 2861fd1452d8SFlorian Westphal return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen); 286283f0c10bSFlorian Westphal 2863717e79c8SPeter Krystad /* @@ the meaning of setsockopt() when the socket is connected and 2864b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2865b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2866b6e4a1aeSMat Martineau * is in TCP fallback, when TCP socket options are passed through 2867b6e4a1aeSMat Martineau * to the one remaining subflow. 2868717e79c8SPeter Krystad */ 2869717e79c8SPeter Krystad lock_sock(sk); 287076660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2871e154659bSFlorian Westphal release_sock(sk); 287276660afbSPaolo Abeni if (ssk) 287376660afbSPaolo Abeni return tcp_setsockopt(ssk, level, optname, optval, optlen); 287450e741bbSFlorian Westphal 2875c9b95a13SFlorian Westphal if (level == SOL_IPV6) 2876c9b95a13SFlorian Westphal return mptcp_setsockopt_v6(msk, optname, optval, optlen); 2877c9b95a13SFlorian Westphal 2878b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2879717e79c8SPeter Krystad } 2880717e79c8SPeter Krystad 2881717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname, 288250e741bbSFlorian Westphal char __user *optval, int __user *option) 2883717e79c8SPeter Krystad { 2884717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 288576660afbSPaolo Abeni struct sock *ssk; 2886717e79c8SPeter Krystad 2887717e79c8SPeter Krystad pr_debug("msk=%p", msk); 2888717e79c8SPeter Krystad 2889b6e4a1aeSMat Martineau /* @@ the meaning of setsockopt() when the socket is connected and 2890b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 2891b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 2892b6e4a1aeSMat Martineau * is in TCP fallback, when socket options are passed through 2893b6e4a1aeSMat Martineau * to the one remaining subflow. 2894717e79c8SPeter Krystad */ 2895717e79c8SPeter Krystad lock_sock(sk); 289676660afbSPaolo Abeni ssk = __mptcp_tcp_fallback(msk); 2897e154659bSFlorian Westphal release_sock(sk); 289876660afbSPaolo Abeni if (ssk) 289976660afbSPaolo Abeni return tcp_getsockopt(ssk, level, optname, optval, option); 290050e741bbSFlorian Westphal 2901b6e4a1aeSMat Martineau return -EOPNOTSUPP; 2902717e79c8SPeter Krystad } 2903717e79c8SPeter Krystad 29046e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk) 29056e628cd3SPaolo Abeni { 29066e628cd3SPaolo Abeni if (!sock_owned_by_user(sk)) 29076e628cd3SPaolo Abeni __mptcp_clean_una(sk); 29086e628cd3SPaolo Abeni else 29096e628cd3SPaolo Abeni set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 29106e628cd3SPaolo Abeni 29116e628cd3SPaolo Abeni if (mptcp_pending_data_fin_ack(sk)) 29126e628cd3SPaolo Abeni mptcp_schedule_work(sk); 29136e628cd3SPaolo Abeni } 29146e628cd3SPaolo Abeni 2915219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk) 29166e628cd3SPaolo Abeni { 29176e628cd3SPaolo Abeni if (!mptcp_send_head(sk)) 29186e628cd3SPaolo Abeni return; 29196e628cd3SPaolo Abeni 292040dc9416SPaolo Abeni if (!sock_owned_by_user(sk)) { 2921*b19bc294SPaolo Abeni struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 2922*b19bc294SPaolo Abeni 2923*b19bc294SPaolo Abeni if (xmit_ssk == ssk) 29246e628cd3SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 2925*b19bc294SPaolo Abeni else if (xmit_ssk) 2926*b19bc294SPaolo Abeni mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 292740dc9416SPaolo Abeni } else { 29286e628cd3SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 29296e628cd3SPaolo Abeni } 293040dc9416SPaolo Abeni } 29316e628cd3SPaolo Abeni 2932ea4ca586SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_WRITE_TIMER_DEFERRED) 293314c441b5SPaolo Abeni 2934e93da928SPaolo Abeni /* processes deferred events and flush wmem */ 293514c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 293614c441b5SPaolo Abeni { 293714c441b5SPaolo Abeni unsigned long flags, nflags; 293814c441b5SPaolo Abeni 29396e628cd3SPaolo Abeni /* push_pending may touch wmem_reserved, do it before the later 29406e628cd3SPaolo Abeni * cleanup 29416e628cd3SPaolo Abeni */ 29426e628cd3SPaolo Abeni if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 29436e628cd3SPaolo Abeni __mptcp_clean_una(sk); 29446e628cd3SPaolo Abeni if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) { 29456e628cd3SPaolo Abeni /* mptcp_push_pending() acquires the subflow socket lock 29466e628cd3SPaolo Abeni * 29476e628cd3SPaolo Abeni * 1) can't be invoked in atomic scope 29486e628cd3SPaolo Abeni * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 29496e628cd3SPaolo Abeni * datapath acquires the msk socket spinlock while helding 29506e628cd3SPaolo Abeni * the subflow socket lock 29516e628cd3SPaolo Abeni */ 29526e628cd3SPaolo Abeni 29536e628cd3SPaolo Abeni spin_unlock_bh(&sk->sk_lock.slock); 29546e628cd3SPaolo Abeni mptcp_push_pending(sk, 0); 29556e628cd3SPaolo Abeni spin_lock_bh(&sk->sk_lock.slock); 29566e628cd3SPaolo Abeni } 29576e628cd3SPaolo Abeni 2958e93da928SPaolo Abeni /* clear any wmem reservation and errors */ 2959e93da928SPaolo Abeni __mptcp_update_wmem(sk); 296087952603SPaolo Abeni __mptcp_update_rmem(sk); 2961e93da928SPaolo Abeni 296214c441b5SPaolo Abeni do { 296314c441b5SPaolo Abeni flags = sk->sk_tsq_flags; 296414c441b5SPaolo Abeni if (!(flags & MPTCP_DEFERRED_ALL)) 296514c441b5SPaolo Abeni return; 296614c441b5SPaolo Abeni nflags = flags & ~MPTCP_DEFERRED_ALL; 296714c441b5SPaolo Abeni } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 296814c441b5SPaolo Abeni 2969b51f9b80SPaolo Abeni sock_release_ownership(sk); 2970b51f9b80SPaolo Abeni 2971b51f9b80SPaolo Abeni if (flags & TCPF_WRITE_TIMER_DEFERRED) { 2972b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 2973b51f9b80SPaolo Abeni __sock_put(sk); 2974b51f9b80SPaolo Abeni } 297514c441b5SPaolo Abeni } 297614c441b5SPaolo Abeni 2977*b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk) 2978*b19bc294SPaolo Abeni { 2979*b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2980*b19bc294SPaolo Abeni struct sock *sk = subflow->conn; 2981*b19bc294SPaolo Abeni 2982*b19bc294SPaolo Abeni mptcp_data_lock(sk); 2983*b19bc294SPaolo Abeni if (!sock_owned_by_user(sk)) 2984*b19bc294SPaolo Abeni __mptcp_subflow_push_pending(sk, ssk); 2985*b19bc294SPaolo Abeni else 2986*b19bc294SPaolo Abeni set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 2987*b19bc294SPaolo Abeni mptcp_data_unlock(sk); 2988*b19bc294SPaolo Abeni mptcp_subflow_delegated_done(subflow); 2989*b19bc294SPaolo Abeni } 2990*b19bc294SPaolo Abeni 29912c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk) 29922c5ebd00SPaolo Abeni { 29932c5ebd00SPaolo Abeni /* should never be called, 29942c5ebd00SPaolo Abeni * we hash the TCP subflows not the master socket 29952c5ebd00SPaolo Abeni */ 29962c5ebd00SPaolo Abeni WARN_ON_ONCE(1); 29972c5ebd00SPaolo Abeni return 0; 29982c5ebd00SPaolo Abeni } 29992c5ebd00SPaolo Abeni 30002c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk) 30012c5ebd00SPaolo Abeni { 30022c5ebd00SPaolo Abeni /* called from sk_common_release(), but nothing to do here */ 30032c5ebd00SPaolo Abeni } 30042c5ebd00SPaolo Abeni 3005cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 3006f870fa0bSMat Martineau { 3007f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 3008cec37a6eSPeter Krystad struct socket *ssock; 3009f870fa0bSMat Martineau 3010cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3011cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 3012cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 3013cec37a6eSPeter Krystad return -EINVAL; 3014f870fa0bSMat Martineau 3015cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 3016cec37a6eSPeter Krystad } 3017f870fa0bSMat Martineau 3018cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 3019cec37a6eSPeter Krystad { 3020cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 3021cec37a6eSPeter Krystad struct mptcp_sock *msk; 3022cec37a6eSPeter Krystad struct sock *sk; 30236d0060f6SMat Martineau u64 ack_seq; 3024f870fa0bSMat Martineau 3025cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 3026cec37a6eSPeter Krystad sk = subflow->conn; 3027cec37a6eSPeter Krystad msk = mptcp_sk(sk); 3028cec37a6eSPeter Krystad 3029648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 3030648ef4b8SMat Martineau 30316d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 30326d0060f6SMat Martineau ack_seq++; 3033648ef4b8SMat Martineau subflow->map_seq = ack_seq; 3034648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 30356d0060f6SMat Martineau 3036cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 3037cec37a6eSPeter Krystad * accessing the field below 3038cec37a6eSPeter Krystad */ 3039cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 3040cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 30416d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3042eaa2ffabSPaolo Abeni WRITE_ONCE(msk->snd_nxt, msk->write_seq); 30436d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 3044fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3045d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 30467439d687SPaolo Abeni WRITE_ONCE(msk->snd_una, msk->write_seq); 30471b1c7a0eSPeter Krystad 30481b1c7a0eSPeter Krystad mptcp_pm_new_connection(msk, 0); 3049a6b118feSFlorian Westphal 3050a6b118feSFlorian Westphal mptcp_rcv_space_init(msk, ssk); 3051f870fa0bSMat Martineau } 3052f870fa0bSMat Martineau 3053866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3054cf7da0d6SPeter Krystad { 3055cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 3056cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 3057cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 3058cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 3059cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 3060cf7da0d6SPeter Krystad } 3061cf7da0d6SPeter Krystad 3062e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk) 3063f296234cSPeter Krystad { 3064e16163b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3065f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3066f296234cSPeter Krystad struct sock *parent = (void *)msk; 3067f296234cSPeter Krystad struct socket *parent_sock; 3068ec3edaa7SPeter Krystad bool ret; 3069f296234cSPeter Krystad 3070f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 3071f296234cSPeter Krystad 3072f296234cSPeter Krystad /* mptcp socket already closing? */ 3073b93df08cSPaolo Abeni if (!mptcp_is_fully_established(parent)) 3074f296234cSPeter Krystad return false; 3075f296234cSPeter Krystad 3076f296234cSPeter Krystad if (!msk->pm.server_side) 3077f296234cSPeter Krystad return true; 3078f296234cSPeter Krystad 307910f6d46cSPaolo Abeni if (!mptcp_pm_allow_new_subflow(msk)) 308010f6d46cSPaolo Abeni return false; 308110f6d46cSPaolo Abeni 308210f6d46cSPaolo Abeni /* active connections are already on conn_list, and we can't acquire 308310f6d46cSPaolo Abeni * msk lock here. 308410f6d46cSPaolo Abeni * use the join list lock as synchronization point and double-check 3085e16163b6SPaolo Abeni * msk status to avoid racing with __mptcp_destroy_sock() 308610f6d46cSPaolo Abeni */ 308710f6d46cSPaolo Abeni spin_lock_bh(&msk->join_list_lock); 308810f6d46cSPaolo Abeni ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3089e16163b6SPaolo Abeni if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 309010f6d46cSPaolo Abeni list_add_tail(&subflow->node, &msk->join_list); 3091e16163b6SPaolo Abeni sock_hold(ssk); 3092e16163b6SPaolo Abeni } 309310f6d46cSPaolo Abeni spin_unlock_bh(&msk->join_list_lock); 309410f6d46cSPaolo Abeni if (!ret) 309510f6d46cSPaolo Abeni return false; 309610f6d46cSPaolo Abeni 309710f6d46cSPaolo Abeni /* attach to msk socket only after we are sure he will deal with us 309810f6d46cSPaolo Abeni * at close time 309910f6d46cSPaolo Abeni */ 3100f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 3101e16163b6SPaolo Abeni if (parent_sock && !ssk->sk_socket) 3102e16163b6SPaolo Abeni mptcp_sock_graft(ssk, parent_sock); 3103917944daSMat Martineau subflow->map_seq = READ_ONCE(msk->ack_seq); 310410f6d46cSPaolo Abeni return true; 3105f296234cSPeter Krystad } 3106f296234cSPeter Krystad 310776e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how) 310876e2a55dSPaolo Abeni { 310976e2a55dSPaolo Abeni pr_debug("sk=%p, how=%d", sk, how); 311076e2a55dSPaolo Abeni 311176e2a55dSPaolo Abeni if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 311276e2a55dSPaolo Abeni __mptcp_wr_shutdown(sk); 311376e2a55dSPaolo Abeni } 311476e2a55dSPaolo Abeni 3115f870fa0bSMat Martineau static struct proto mptcp_prot = { 3116f870fa0bSMat Martineau .name = "MPTCP", 3117f870fa0bSMat Martineau .owner = THIS_MODULE, 3118f870fa0bSMat Martineau .init = mptcp_init_sock, 311918b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 3120f870fa0bSMat Martineau .close = mptcp_close, 3121cf7da0d6SPeter Krystad .accept = mptcp_accept, 3122717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 3123717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 312476e2a55dSPaolo Abeni .shutdown = mptcp_shutdown, 312579c0949eSPeter Krystad .destroy = mptcp_destroy, 3126f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 3127f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 312814c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 31292c5ebd00SPaolo Abeni .hash = mptcp_hash, 31302c5ebd00SPaolo Abeni .unhash = mptcp_unhash, 3131cec37a6eSPeter Krystad .get_port = mptcp_get_port, 3132d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 3133d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 3134d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 3135d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3136989ef49bSPaolo Abeni .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3137d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 3138f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 31392c5ebd00SPaolo Abeni .slab_flags = SLAB_TYPESAFE_BY_RCU, 3140f870fa0bSMat Martineau .no_autobind = true, 3141f870fa0bSMat Martineau }; 3142f870fa0bSMat Martineau 31432303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 31442303f994SPeter Krystad { 31452303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 31462303f994SPeter Krystad struct socket *ssock; 3147cf7da0d6SPeter Krystad int err; 31482303f994SPeter Krystad 31492303f994SPeter Krystad lock_sock(sock->sk); 3150fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3151fa68018dSPaolo Abeni if (!ssock) { 3152fa68018dSPaolo Abeni err = -EINVAL; 31532303f994SPeter Krystad goto unlock; 31542303f994SPeter Krystad } 31552303f994SPeter Krystad 31562303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 3157cf7da0d6SPeter Krystad if (!err) 3158cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 31592303f994SPeter Krystad 31602303f994SPeter Krystad unlock: 31612303f994SPeter Krystad release_sock(sock->sk); 31622303f994SPeter Krystad return err; 31632303f994SPeter Krystad } 31642303f994SPeter Krystad 31650235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 31660235d075SPaolo Abeni struct mptcp_subflow_context *subflow) 31670235d075SPaolo Abeni { 31680235d075SPaolo Abeni subflow->request_mptcp = 0; 31690235d075SPaolo Abeni __mptcp_do_fallback(msk); 31700235d075SPaolo Abeni } 31710235d075SPaolo Abeni 31722303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 31732303f994SPeter Krystad int addr_len, int flags) 31742303f994SPeter Krystad { 31752303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 31762c5ebd00SPaolo Abeni struct mptcp_subflow_context *subflow; 31772303f994SPeter Krystad struct socket *ssock; 31782303f994SPeter Krystad int err; 31792303f994SPeter Krystad 31802303f994SPeter Krystad lock_sock(sock->sk); 318141be81a8SPaolo Abeni if (sock->state != SS_UNCONNECTED && msk->subflow) { 318241be81a8SPaolo Abeni /* pending connection or invalid state, let existing subflow 318341be81a8SPaolo Abeni * cope with that 318441be81a8SPaolo Abeni */ 318541be81a8SPaolo Abeni ssock = msk->subflow; 318641be81a8SPaolo Abeni goto do_connect; 318741be81a8SPaolo Abeni } 318841be81a8SPaolo Abeni 3189fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3190fa68018dSPaolo Abeni if (!ssock) { 3191fa68018dSPaolo Abeni err = -EINVAL; 31922303f994SPeter Krystad goto unlock; 31932303f994SPeter Krystad } 31942303f994SPeter Krystad 3195fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3196fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_SYN_SENT); 31972c5ebd00SPaolo Abeni subflow = mptcp_subflow_ctx(ssock->sk); 3198cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 3199cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3200cf7da0d6SPeter Krystad * TCP option space. 3201cf7da0d6SPeter Krystad */ 3202cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 32030235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3204cf7da0d6SPeter Krystad #endif 32052c5ebd00SPaolo Abeni if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) 32060235d075SPaolo Abeni mptcp_subflow_early_fallback(msk, subflow); 3207cf7da0d6SPeter Krystad 320841be81a8SPaolo Abeni do_connect: 32092303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 321041be81a8SPaolo Abeni sock->state = ssock->state; 321141be81a8SPaolo Abeni 321241be81a8SPaolo Abeni /* on successful connect, the msk state will be moved to established by 321341be81a8SPaolo Abeni * subflow_finish_connect() 321441be81a8SPaolo Abeni */ 3215367fe04eSMatthieu Baerts if (!err || err == -EINPROGRESS) 3216cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 321741be81a8SPaolo Abeni else 321841be81a8SPaolo Abeni inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 32192303f994SPeter Krystad 32202303f994SPeter Krystad unlock: 32212303f994SPeter Krystad release_sock(sock->sk); 32222303f994SPeter Krystad return err; 32232303f994SPeter Krystad } 32242303f994SPeter Krystad 3225cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 3226cf7da0d6SPeter Krystad { 3227cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3228cf7da0d6SPeter Krystad struct socket *ssock; 3229cf7da0d6SPeter Krystad int err; 3230cf7da0d6SPeter Krystad 3231cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3232cf7da0d6SPeter Krystad 3233cf7da0d6SPeter Krystad lock_sock(sock->sk); 3234fa68018dSPaolo Abeni ssock = __mptcp_nmpc_socket(msk); 3235fa68018dSPaolo Abeni if (!ssock) { 3236fa68018dSPaolo Abeni err = -EINVAL; 3237cf7da0d6SPeter Krystad goto unlock; 3238cf7da0d6SPeter Krystad } 3239cf7da0d6SPeter Krystad 3240fa68018dSPaolo Abeni mptcp_token_destroy(msk); 3241fa68018dSPaolo Abeni inet_sk_state_store(sock->sk, TCP_LISTEN); 32425e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 32435e20087dSFlorian Westphal 3244cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 3245cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3246cf7da0d6SPeter Krystad if (!err) 3247cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 3248cf7da0d6SPeter Krystad 3249cf7da0d6SPeter Krystad unlock: 3250cf7da0d6SPeter Krystad release_sock(sock->sk); 3251cf7da0d6SPeter Krystad return err; 3252cf7da0d6SPeter Krystad } 3253cf7da0d6SPeter Krystad 3254cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3255cf7da0d6SPeter Krystad int flags, bool kern) 3256cf7da0d6SPeter Krystad { 3257cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 3258cf7da0d6SPeter Krystad struct socket *ssock; 3259cf7da0d6SPeter Krystad int err; 3260cf7da0d6SPeter Krystad 3261cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 3262cf7da0d6SPeter Krystad 3263cf7da0d6SPeter Krystad lock_sock(sock->sk); 3264cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 3265cf7da0d6SPeter Krystad goto unlock_fail; 3266cf7da0d6SPeter Krystad 3267cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 3268cf7da0d6SPeter Krystad if (!ssock) 3269cf7da0d6SPeter Krystad goto unlock_fail; 3270cf7da0d6SPeter Krystad 32718a05661bSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 3272cf7da0d6SPeter Krystad sock_hold(ssock->sk); 3273cf7da0d6SPeter Krystad release_sock(sock->sk); 3274cf7da0d6SPeter Krystad 3275cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 3276d2f77c53SPaolo Abeni if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3277cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3278cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 32790397c6d8SPaolo Abeni struct sock *newsk = newsock->sk; 32800397c6d8SPaolo Abeni bool slowpath; 32810397c6d8SPaolo Abeni 32820397c6d8SPaolo Abeni slowpath = lock_sock_fast(newsk); 32835b950ff4SPaolo Abeni 32845b950ff4SPaolo Abeni /* PM/worker can now acquire the first subflow socket 32855b950ff4SPaolo Abeni * lock without racing with listener queue cleanup, 32865b950ff4SPaolo Abeni * we can notify it, if needed. 32875b950ff4SPaolo Abeni */ 32885b950ff4SPaolo Abeni subflow = mptcp_subflow_ctx(msk->first); 32895b950ff4SPaolo Abeni list_add(&subflow->node, &msk->conn_list); 32905b950ff4SPaolo Abeni sock_hold(msk->first); 32915b950ff4SPaolo Abeni if (mptcp_is_fully_established(newsk)) 32925b950ff4SPaolo Abeni mptcp_pm_fully_established(msk); 32935b950ff4SPaolo Abeni 32940397c6d8SPaolo Abeni mptcp_copy_inaddrs(newsk, msk->first); 32950397c6d8SPaolo Abeni mptcp_rcv_space_init(msk, msk->first); 32965cf92bbaSPaolo Abeni mptcp_propagate_sndbuf(newsk, msk->first); 3297cf7da0d6SPeter Krystad 3298cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3299cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 3300cf7da0d6SPeter Krystad */ 3301ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 3302190f8b06SGeliang Tang mptcp_for_each_subflow(msk, subflow) { 3303cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3304cf7da0d6SPeter Krystad 3305cf7da0d6SPeter Krystad if (!ssk->sk_socket) 3306cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 3307cf7da0d6SPeter Krystad } 33080397c6d8SPaolo Abeni unlock_sock_fast(newsk, slowpath); 3309cf7da0d6SPeter Krystad } 3310cf7da0d6SPeter Krystad 33118a05661bSPaolo Abeni if (inet_csk_listen_poll(ssock->sk)) 33128a05661bSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 3313cf7da0d6SPeter Krystad sock_put(ssock->sk); 3314cf7da0d6SPeter Krystad return err; 3315cf7da0d6SPeter Krystad 3316cf7da0d6SPeter Krystad unlock_fail: 3317cf7da0d6SPeter Krystad release_sock(sock->sk); 3318cf7da0d6SPeter Krystad return -EINVAL; 3319cf7da0d6SPeter Krystad } 3320cf7da0d6SPeter Krystad 33218a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 33228a05661bSPaolo Abeni { 33238a05661bSPaolo Abeni return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 33248a05661bSPaolo Abeni 0; 33258a05661bSPaolo Abeni } 33268a05661bSPaolo Abeni 33278edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 33288edf0864SFlorian Westphal { 33298edf0864SFlorian Westphal struct sock *sk = (struct sock *)msk; 33308edf0864SFlorian Westphal 33318edf0864SFlorian Westphal if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 33328edf0864SFlorian Westphal return 0; 33338edf0864SFlorian Westphal 33348edf0864SFlorian Westphal if (sk_stream_is_writeable(sk)) 33358edf0864SFlorian Westphal return EPOLLOUT | EPOLLWRNORM; 33368edf0864SFlorian Westphal 33375cf92bbaSPaolo Abeni mptcp_set_nospace(sk); 33386e628cd3SPaolo Abeni smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 33396e628cd3SPaolo Abeni if (sk_stream_is_writeable(sk)) 33406e628cd3SPaolo Abeni return EPOLLOUT | EPOLLWRNORM; 33418edf0864SFlorian Westphal 33426e628cd3SPaolo Abeni return 0; 33438edf0864SFlorian Westphal } 33448edf0864SFlorian Westphal 33452303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 33462303f994SPeter Krystad struct poll_table_struct *wait) 33472303f994SPeter Krystad { 33481891c4a0SFlorian Westphal struct sock *sk = sock->sk; 33498ab183deSPaolo Abeni struct mptcp_sock *msk; 33502303f994SPeter Krystad __poll_t mask = 0; 33518a05661bSPaolo Abeni int state; 33522303f994SPeter Krystad 33531891c4a0SFlorian Westphal msk = mptcp_sk(sk); 33541891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 33551891c4a0SFlorian Westphal 33568a05661bSPaolo Abeni state = inet_sk_state_load(sk); 33576719331cSPaolo Abeni pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 33588a05661bSPaolo Abeni if (state == TCP_LISTEN) 33598a05661bSPaolo Abeni return mptcp_check_readable(msk); 33608a05661bSPaolo Abeni 33618a05661bSPaolo Abeni if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 33628a05661bSPaolo Abeni mask |= mptcp_check_readable(msk); 33638edf0864SFlorian Westphal mask |= mptcp_check_writeable(msk); 33648a05661bSPaolo Abeni } 33651891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 33661891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 33671891c4a0SFlorian Westphal 33682303f994SPeter Krystad return mask; 33692303f994SPeter Krystad } 33702303f994SPeter Krystad 3371e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 3372e42f1ac6SFlorian Westphal .family = PF_INET, 3373e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 3374e42f1ac6SFlorian Westphal .release = inet_release, 3375e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3376e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3377e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3378e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3379d2f77c53SPaolo Abeni .getname = inet_getname, 3380e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3381e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 3382e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3383e42f1ac6SFlorian Westphal .listen = mptcp_listen, 338476e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3385e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3386e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3387e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 3388e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 3389e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3390e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3391e42f1ac6SFlorian Westphal }; 33922303f994SPeter Krystad 3393f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 3394f870fa0bSMat Martineau .type = SOCK_STREAM, 3395f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3396f870fa0bSMat Martineau .prot = &mptcp_prot, 33972303f994SPeter Krystad .ops = &mptcp_stream_ops, 33982303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 3399f870fa0bSMat Martineau }; 3400f870fa0bSMat Martineau 3401*b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3402*b19bc294SPaolo Abeni { 3403*b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3404*b19bc294SPaolo Abeni struct mptcp_subflow_context *subflow; 3405*b19bc294SPaolo Abeni int work_done = 0; 3406*b19bc294SPaolo Abeni 3407*b19bc294SPaolo Abeni delegated = container_of(napi, struct mptcp_delegated_action, napi); 3408*b19bc294SPaolo Abeni while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3409*b19bc294SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3410*b19bc294SPaolo Abeni 3411*b19bc294SPaolo Abeni bh_lock_sock_nested(ssk); 3412*b19bc294SPaolo Abeni if (!sock_owned_by_user(ssk) && 3413*b19bc294SPaolo Abeni mptcp_subflow_has_delegated_action(subflow)) 3414*b19bc294SPaolo Abeni mptcp_subflow_process_delegated(ssk); 3415*b19bc294SPaolo Abeni /* ... elsewhere tcp_release_cb_override already processed 3416*b19bc294SPaolo Abeni * the action or will do at next release_sock(). 3417*b19bc294SPaolo Abeni * In both case must dequeue the subflow here - on the same 3418*b19bc294SPaolo Abeni * CPU that scheduled it. 3419*b19bc294SPaolo Abeni */ 3420*b19bc294SPaolo Abeni bh_unlock_sock(ssk); 3421*b19bc294SPaolo Abeni sock_put(ssk); 3422*b19bc294SPaolo Abeni 3423*b19bc294SPaolo Abeni if (++work_done == budget) 3424*b19bc294SPaolo Abeni return budget; 3425*b19bc294SPaolo Abeni } 3426*b19bc294SPaolo Abeni 3427*b19bc294SPaolo Abeni /* always provide a 0 'work_done' argument, so that napi_complete_done 3428*b19bc294SPaolo Abeni * will not try accessing the NULL napi->dev ptr 3429*b19bc294SPaolo Abeni */ 3430*b19bc294SPaolo Abeni napi_complete_done(napi, 0); 3431*b19bc294SPaolo Abeni return work_done; 3432*b19bc294SPaolo Abeni } 3433*b19bc294SPaolo Abeni 3434d39dcecaSPaolo Abeni void __init mptcp_proto_init(void) 3435f870fa0bSMat Martineau { 3436*b19bc294SPaolo Abeni struct mptcp_delegated_action *delegated; 3437*b19bc294SPaolo Abeni int cpu; 3438*b19bc294SPaolo Abeni 34392303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 34402303f994SPeter Krystad 3441d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3442d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 3443d027236cSPaolo Abeni 3444*b19bc294SPaolo Abeni init_dummy_netdev(&mptcp_napi_dev); 3445*b19bc294SPaolo Abeni for_each_possible_cpu(cpu) { 3446*b19bc294SPaolo Abeni delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3447*b19bc294SPaolo Abeni INIT_LIST_HEAD(&delegated->head); 3448*b19bc294SPaolo Abeni netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3449*b19bc294SPaolo Abeni NAPI_POLL_WEIGHT); 3450*b19bc294SPaolo Abeni napi_enable(&delegated->napi); 3451*b19bc294SPaolo Abeni } 3452*b19bc294SPaolo Abeni 34532303f994SPeter Krystad mptcp_subflow_init(); 34541b1c7a0eSPeter Krystad mptcp_pm_init(); 34552c5ebd00SPaolo Abeni mptcp_token_init(); 34562303f994SPeter Krystad 3457f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 3458f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 3459f870fa0bSMat Martineau 3460f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 34616771bfd9SFlorian Westphal 34626771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3463f870fa0bSMat Martineau } 3464f870fa0bSMat Martineau 3465f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3466e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 3467e42f1ac6SFlorian Westphal .family = PF_INET6, 3468e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 3469e42f1ac6SFlorian Westphal .release = inet6_release, 3470e42f1ac6SFlorian Westphal .bind = mptcp_bind, 3471e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 3472e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 3473e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 3474d2f77c53SPaolo Abeni .getname = inet6_getname, 3475e42f1ac6SFlorian Westphal .poll = mptcp_poll, 3476e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 3477e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 3478e42f1ac6SFlorian Westphal .listen = mptcp_listen, 347976e2a55dSPaolo Abeni .shutdown = inet_shutdown, 3480e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 3481e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 3482e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 3483e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 3484e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 3485e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 3486e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 34873986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 3488e42f1ac6SFlorian Westphal #endif 3489e42f1ac6SFlorian Westphal }; 3490e42f1ac6SFlorian Westphal 3491f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 3492f870fa0bSMat Martineau 349379c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 349479c0949eSPeter Krystad { 349579c0949eSPeter Krystad mptcp_destroy(sk); 349679c0949eSPeter Krystad inet6_destroy_sock(sk); 349779c0949eSPeter Krystad } 349879c0949eSPeter Krystad 3499f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 3500f870fa0bSMat Martineau .type = SOCK_STREAM, 3501f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 3502f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 35032303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 3504f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 3505f870fa0bSMat Martineau }; 3506f870fa0bSMat Martineau 3507d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void) 3508f870fa0bSMat Martineau { 3509f870fa0bSMat Martineau int err; 3510f870fa0bSMat Martineau 3511f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 3512f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3513f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 351479c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 3515b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3516f870fa0bSMat Martineau 3517f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 3518f870fa0bSMat Martineau if (err) 3519f870fa0bSMat Martineau return err; 3520f870fa0bSMat Martineau 3521f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 3522f870fa0bSMat Martineau if (err) 3523f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 3524f870fa0bSMat Martineau 3525f870fa0bSMat Martineau return err; 3526f870fa0bSMat Martineau } 3527f870fa0bSMat Martineau #endif 3528