xref: /openbmc/linux/net/mptcp/protocol.c (revision a2702a076e73787db660fb8fd07d26a1a3108358)
1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0
2f870fa0bSMat Martineau /* Multipath TCP
3f870fa0bSMat Martineau  *
4f870fa0bSMat Martineau  * Copyright (c) 2017 - 2019, Intel Corporation.
5f870fa0bSMat Martineau  */
6f870fa0bSMat Martineau 
7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt
8f870fa0bSMat Martineau 
9f870fa0bSMat Martineau #include <linux/kernel.h>
10f870fa0bSMat Martineau #include <linux/module.h>
11f870fa0bSMat Martineau #include <linux/netdevice.h>
127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h>
137a6a6cbcSPaolo Abeni #include <linux/atomic.h>
14f870fa0bSMat Martineau #include <net/sock.h>
15f870fa0bSMat Martineau #include <net/inet_common.h>
16f870fa0bSMat Martineau #include <net/inet_hashtables.h>
17f870fa0bSMat Martineau #include <net/protocol.h>
18f870fa0bSMat Martineau #include <net/tcp.h>
193721b9b6SMat Martineau #include <net/tcp_states.h>
20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21cf7da0d6SPeter Krystad #include <net/transp_v6.h>
22cf7da0d6SPeter Krystad #endif
23f870fa0bSMat Martineau #include <net/mptcp.h>
24e16163b6SPaolo Abeni #include <net/xfrm.h>
25644807e3SFlorian Westphal #include <asm/ioctls.h>
26f870fa0bSMat Martineau #include "protocol.h"
27fc518953SFlorian Westphal #include "mib.h"
28f870fa0bSMat Martineau 
29e10a9892SGeliang Tang #define CREATE_TRACE_POINTS
30e10a9892SGeliang Tang #include <trace/events/mptcp.h>
31e10a9892SGeliang Tang 
32b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33b0519de8SFlorian Westphal struct mptcp6_sock {
34b0519de8SFlorian Westphal 	struct mptcp_sock msk;
35b0519de8SFlorian Westphal 	struct ipv6_pinfo np;
36b0519de8SFlorian Westphal };
37b0519de8SFlorian Westphal #endif
38b0519de8SFlorian Westphal 
39b7f653b2SFlorian Westphal enum {
40b7f653b2SFlorian Westphal 	MPTCP_CMSG_TS = BIT(0),
412c9e7765SFlorian Westphal 	MPTCP_CMSG_INQ = BIT(1),
42b7f653b2SFlorian Westphal };
43b7f653b2SFlorian Westphal 
4491b6d325SEric Dumazet static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
45d027236cSPaolo Abeni 
46e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk);
47d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk);
48e16163b6SPaolo Abeni 
49b19bc294SPaolo Abeni DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50b19bc294SPaolo Abeni static struct net_device mptcp_napi_dev;
51b19bc294SPaolo Abeni 
522303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
532303f994SPeter Krystad  * completed yet or has failed, return the subflow socket.
542303f994SPeter Krystad  * Otherwise return NULL.
552303f994SPeter Krystad  */
561729cf18SGeliang Tang struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
572303f994SPeter Krystad {
58d22f4988SChristoph Paasch 	if (!msk->subflow || READ_ONCE(msk->can_ack))
592303f994SPeter Krystad 		return NULL;
602303f994SPeter Krystad 
612303f994SPeter Krystad 	return msk->subflow;
622303f994SPeter Krystad }
632303f994SPeter Krystad 
646f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */
656f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
666f8a612aSFlorian Westphal {
677439d687SPaolo Abeni 	return READ_ONCE(msk->wnd_end);
686f8a612aSFlorian Westphal }
696f8a612aSFlorian Westphal 
70d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk)
710b4f33deSFlorian Westphal {
720b4f33deSFlorian Westphal 	struct socket *sock = sk->sk_socket;
730b4f33deSFlorian Westphal 
740b4f33deSFlorian Westphal 	if (unlikely(sk->sk_prot == &tcp_prot)) {
750b4f33deSFlorian Westphal 		/* we are being invoked after mptcp_accept() has
760b4f33deSFlorian Westphal 		 * accepted a non-mp-capable flow: sk is a tcp_sk,
770b4f33deSFlorian Westphal 		 * not an mptcp one.
780b4f33deSFlorian Westphal 		 *
790b4f33deSFlorian Westphal 		 * Hand the socket over to tcp so all further socket ops
800b4f33deSFlorian Westphal 		 * bypass mptcp.
810b4f33deSFlorian Westphal 		 */
820b4f33deSFlorian Westphal 		sock->ops = &inet_stream_ops;
83d2f77c53SPaolo Abeni 		return true;
840b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
850b4f33deSFlorian Westphal 	} else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
860b4f33deSFlorian Westphal 		sock->ops = &inet6_stream_ops;
87d2f77c53SPaolo Abeni 		return true;
880b4f33deSFlorian Westphal #endif
890b4f33deSFlorian Westphal 	}
900b4f33deSFlorian Westphal 
91d2f77c53SPaolo Abeni 	return false;
920b4f33deSFlorian Westphal }
930b4f33deSFlorian Westphal 
94fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk)
952303f994SPeter Krystad {
962303f994SPeter Krystad 	struct mptcp_subflow_context *subflow;
972303f994SPeter Krystad 	struct sock *sk = (struct sock *)msk;
982303f994SPeter Krystad 	struct socket *ssock;
992303f994SPeter Krystad 	int err;
1002303f994SPeter Krystad 
1016bc1fe7dSPaolo Abeni 	err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
1022303f994SPeter Krystad 	if (err)
103fa68018dSPaolo Abeni 		return err;
1042303f994SPeter Krystad 
1058ab183deSPaolo Abeni 	msk->first = ssock->sk;
1062303f994SPeter Krystad 	msk->subflow = ssock;
1072303f994SPeter Krystad 	subflow = mptcp_subflow_ctx(ssock->sk);
108cec37a6eSPeter Krystad 	list_add(&subflow->node, &msk->conn_list);
109e16163b6SPaolo Abeni 	sock_hold(ssock->sk);
1102303f994SPeter Krystad 	subflow->request_mptcp = 1;
1114cf86ae8SPaolo Abeni 
1124cf86ae8SPaolo Abeni 	/* This is the first subflow, always with id 0 */
1134cf86ae8SPaolo Abeni 	subflow->local_id_valid = 1;
114866f26f2SPaolo Abeni 	mptcp_sock_graft(msk->first, sk->sk_socket);
115e1ff9e82SDavide Caratti 
116fa68018dSPaolo Abeni 	return 0;
1172303f994SPeter Krystad }
1182303f994SPeter Krystad 
119ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
120ab174ad8SPaolo Abeni {
121ab174ad8SPaolo Abeni 	sk_drops_add(sk, skb);
122ab174ad8SPaolo Abeni 	__kfree_skb(skb);
123ab174ad8SPaolo Abeni }
124ab174ad8SPaolo Abeni 
1256511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size)
1266511882cSPaolo Abeni {
1276511882cSPaolo Abeni 	mptcp_sk(sk)->rmem_fwd_alloc -= size;
1286511882cSPaolo Abeni }
1296511882cSPaolo Abeni 
1308268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
1318268ed4cSPaolo Abeni 			       struct sk_buff *from)
1328268ed4cSPaolo Abeni {
1338268ed4cSPaolo Abeni 	bool fragstolen;
1348268ed4cSPaolo Abeni 	int delta;
1358268ed4cSPaolo Abeni 
1368268ed4cSPaolo Abeni 	if (MPTCP_SKB_CB(from)->offset ||
1378268ed4cSPaolo Abeni 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
1388268ed4cSPaolo Abeni 		return false;
1398268ed4cSPaolo Abeni 
14006242e44SPaolo Abeni 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
14106242e44SPaolo Abeni 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
14206242e44SPaolo Abeni 		 to->len, MPTCP_SKB_CB(from)->end_seq);
143ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
1447288ff6eSPaolo Abeni 
1457288ff6eSPaolo Abeni 	/* note the fwd memory can reach a negative value after accounting
1467288ff6eSPaolo Abeni 	 * for the delta, but the later skb free will restore a non
1477288ff6eSPaolo Abeni 	 * negative one
1487288ff6eSPaolo Abeni 	 */
1498268ed4cSPaolo Abeni 	atomic_add(delta, &sk->sk_rmem_alloc);
1506511882cSPaolo Abeni 	mptcp_rmem_charge(sk, delta);
1517288ff6eSPaolo Abeni 	kfree_skb_partial(from, fragstolen);
1527288ff6eSPaolo Abeni 
1538268ed4cSPaolo Abeni 	return true;
1548268ed4cSPaolo Abeni }
1558268ed4cSPaolo Abeni 
156ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
157ab174ad8SPaolo Abeni 				   struct sk_buff *from)
1586771bfd9SFlorian Westphal {
159ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
160ab174ad8SPaolo Abeni 		return false;
161ab174ad8SPaolo Abeni 
162ab174ad8SPaolo Abeni 	return mptcp_try_coalesce((struct sock *)msk, to, from);
163ab174ad8SPaolo Abeni }
164ab174ad8SPaolo Abeni 
1656511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
1666511882cSPaolo Abeni {
167100fdd1fSEric Dumazet 	amount >>= PAGE_SHIFT;
168100fdd1fSEric Dumazet 	mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT;
1696511882cSPaolo Abeni 	__sk_mem_reduce_allocated(sk, amount);
1706511882cSPaolo Abeni }
1716511882cSPaolo Abeni 
1726511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size)
1736511882cSPaolo Abeni {
1746511882cSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1756511882cSPaolo Abeni 	int reclaimable;
1766511882cSPaolo Abeni 
1776511882cSPaolo Abeni 	msk->rmem_fwd_alloc += size;
1786511882cSPaolo Abeni 	reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
1796511882cSPaolo Abeni 
1806511882cSPaolo Abeni 	/* see sk_mem_uncharge() for the rationale behind the following schema */
181d24141feSPaolo Abeni 	if (unlikely(reclaimable >= PAGE_SIZE))
182d24141feSPaolo Abeni 		__mptcp_rmem_reclaim(sk, reclaimable);
1836511882cSPaolo Abeni }
1846511882cSPaolo Abeni 
1856511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb)
1866511882cSPaolo Abeni {
1876511882cSPaolo Abeni 	unsigned int len = skb->truesize;
1886511882cSPaolo Abeni 	struct sock *sk = skb->sk;
1896511882cSPaolo Abeni 
1906511882cSPaolo Abeni 	atomic_sub(len, &sk->sk_rmem_alloc);
1916511882cSPaolo Abeni 	mptcp_rmem_uncharge(sk, len);
1926511882cSPaolo Abeni }
1936511882cSPaolo Abeni 
19436b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
1956511882cSPaolo Abeni {
1966511882cSPaolo Abeni 	skb_orphan(skb);
1976511882cSPaolo Abeni 	skb->sk = sk;
1986511882cSPaolo Abeni 	skb->destructor = mptcp_rfree;
1996511882cSPaolo Abeni 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
2006511882cSPaolo Abeni 	mptcp_rmem_charge(sk, skb->truesize);
2016511882cSPaolo Abeni }
2026511882cSPaolo Abeni 
203ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences:
204ab174ad8SPaolo Abeni  * - use mptcp seqs
205ab174ad8SPaolo Abeni  * - don't cope with sacks
206ab174ad8SPaolo Abeni  */
207ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
208ab174ad8SPaolo Abeni {
209ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
210ab174ad8SPaolo Abeni 	struct rb_node **p, *parent;
211ab174ad8SPaolo Abeni 	u64 seq, end_seq, max_seq;
212ab174ad8SPaolo Abeni 	struct sk_buff *skb1;
213ab174ad8SPaolo Abeni 
214ab174ad8SPaolo Abeni 	seq = MPTCP_SKB_CB(skb)->map_seq;
215ab174ad8SPaolo Abeni 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
216f3589be0SPaolo Abeni 	max_seq = atomic64_read(&msk->rcv_wnd_sent);
217ab174ad8SPaolo Abeni 
21806242e44SPaolo Abeni 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
21906242e44SPaolo Abeni 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
220fa3fe2b1SFlorian Westphal 	if (after64(end_seq, max_seq)) {
221ab174ad8SPaolo Abeni 		/* out of window */
222ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb);
223fa3fe2b1SFlorian Westphal 		pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
224fa3fe2b1SFlorian Westphal 			 (unsigned long long)end_seq - (unsigned long)max_seq,
225f3589be0SPaolo Abeni 			 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
22606242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
227ab174ad8SPaolo Abeni 		return;
228ab174ad8SPaolo Abeni 	}
229ab174ad8SPaolo Abeni 
230ab174ad8SPaolo Abeni 	p = &msk->out_of_order_queue.rb_node;
23106242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
232ab174ad8SPaolo Abeni 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
233ab174ad8SPaolo Abeni 		rb_link_node(&skb->rbnode, NULL, p);
234ab174ad8SPaolo Abeni 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
235ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
236ab174ad8SPaolo Abeni 		goto end;
237ab174ad8SPaolo Abeni 	}
238ab174ad8SPaolo Abeni 
239ab174ad8SPaolo Abeni 	/* with 2 subflows, adding at end of ooo queue is quite likely
240ab174ad8SPaolo Abeni 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
241ab174ad8SPaolo Abeni 	 */
24206242e44SPaolo Abeni 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
24306242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
24406242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
245ab174ad8SPaolo Abeni 		return;
24606242e44SPaolo Abeni 	}
247ab174ad8SPaolo Abeni 
248ab174ad8SPaolo Abeni 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
249ab174ad8SPaolo Abeni 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
25006242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
251ab174ad8SPaolo Abeni 		parent = &msk->ooo_last_skb->rbnode;
252ab174ad8SPaolo Abeni 		p = &parent->rb_right;
253ab174ad8SPaolo Abeni 		goto insert;
254ab174ad8SPaolo Abeni 	}
255ab174ad8SPaolo Abeni 
256ab174ad8SPaolo Abeni 	/* Find place to insert this segment. Handle overlaps on the way. */
257ab174ad8SPaolo Abeni 	parent = NULL;
258ab174ad8SPaolo Abeni 	while (*p) {
259ab174ad8SPaolo Abeni 		parent = *p;
260ab174ad8SPaolo Abeni 		skb1 = rb_to_skb(parent);
261ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
262ab174ad8SPaolo Abeni 			p = &parent->rb_left;
263ab174ad8SPaolo Abeni 			continue;
264ab174ad8SPaolo Abeni 		}
265ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
266ab174ad8SPaolo Abeni 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
267ab174ad8SPaolo Abeni 				/* All the bits are present. Drop. */
268ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb);
26906242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
270ab174ad8SPaolo Abeni 				return;
271ab174ad8SPaolo Abeni 			}
272ab174ad8SPaolo Abeni 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
273ab174ad8SPaolo Abeni 				/* partial overlap:
274ab174ad8SPaolo Abeni 				 *     |     skb      |
275ab174ad8SPaolo Abeni 				 *  |     skb1    |
276ab174ad8SPaolo Abeni 				 * continue traversing
277ab174ad8SPaolo Abeni 				 */
278ab174ad8SPaolo Abeni 			} else {
279ab174ad8SPaolo Abeni 				/* skb's seq == skb1's seq and skb covers skb1.
280ab174ad8SPaolo Abeni 				 * Replace skb1 with skb.
281ab174ad8SPaolo Abeni 				 */
282ab174ad8SPaolo Abeni 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
283ab174ad8SPaolo Abeni 						&msk->out_of_order_queue);
284ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb1);
28506242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
286ab174ad8SPaolo Abeni 				goto merge_right;
287ab174ad8SPaolo Abeni 			}
288ab174ad8SPaolo Abeni 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
28906242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
290ab174ad8SPaolo Abeni 			return;
291ab174ad8SPaolo Abeni 		}
292ab174ad8SPaolo Abeni 		p = &parent->rb_right;
293ab174ad8SPaolo Abeni 	}
29406242e44SPaolo Abeni 
295ab174ad8SPaolo Abeni insert:
296ab174ad8SPaolo Abeni 	/* Insert segment into RB tree. */
297ab174ad8SPaolo Abeni 	rb_link_node(&skb->rbnode, parent, p);
298ab174ad8SPaolo Abeni 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
299ab174ad8SPaolo Abeni 
300ab174ad8SPaolo Abeni merge_right:
301ab174ad8SPaolo Abeni 	/* Remove other segments covered by skb. */
302ab174ad8SPaolo Abeni 	while ((skb1 = skb_rb_next(skb)) != NULL) {
303ab174ad8SPaolo Abeni 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
304ab174ad8SPaolo Abeni 			break;
305ab174ad8SPaolo Abeni 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
306ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb1);
30706242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
308ab174ad8SPaolo Abeni 	}
309ab174ad8SPaolo Abeni 	/* If there is no skb after us, we are the last_skb ! */
310ab174ad8SPaolo Abeni 	if (!skb1)
311ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
312ab174ad8SPaolo Abeni 
313ab174ad8SPaolo Abeni end:
314ab174ad8SPaolo Abeni 	skb_condense(skb);
3156511882cSPaolo Abeni 	mptcp_set_owner_r(skb, sk);
3166511882cSPaolo Abeni }
3176511882cSPaolo Abeni 
3186511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
3196511882cSPaolo Abeni {
3206511882cSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
3216511882cSPaolo Abeni 	int amt, amount;
3226511882cSPaolo Abeni 
32369d93daeSPaolo Abeni 	if (size <= msk->rmem_fwd_alloc)
3246511882cSPaolo Abeni 		return true;
3256511882cSPaolo Abeni 
32669d93daeSPaolo Abeni 	size -= msk->rmem_fwd_alloc;
3276511882cSPaolo Abeni 	amt = sk_mem_pages(size);
328100fdd1fSEric Dumazet 	amount = amt << PAGE_SHIFT;
3294aaa1685SPaolo Abeni 	if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
3306511882cSPaolo Abeni 		return false;
3316511882cSPaolo Abeni 
3324aaa1685SPaolo Abeni 	msk->rmem_fwd_alloc += amount;
3336511882cSPaolo Abeni 	return true;
334ab174ad8SPaolo Abeni }
335ab174ad8SPaolo Abeni 
336ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
337ab174ad8SPaolo Abeni 			     struct sk_buff *skb, unsigned int offset,
338ab174ad8SPaolo Abeni 			     size_t copy_len)
339ab174ad8SPaolo Abeni {
340ab174ad8SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3416771bfd9SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
3424e637c70SFlorian Westphal 	struct sk_buff *tail;
343b7f653b2SFlorian Westphal 	bool has_rxtstamp;
3446771bfd9SFlorian Westphal 
3456771bfd9SFlorian Westphal 	__skb_unlink(skb, &ssk->sk_receive_queue);
3464e637c70SFlorian Westphal 
3474e637c70SFlorian Westphal 	skb_ext_reset(skb);
3484e637c70SFlorian Westphal 	skb_orphan(skb);
3494e637c70SFlorian Westphal 
3509c3f94e1SPaolo Abeni 	/* try to fetch required memory from subflow */
3516511882cSPaolo Abeni 	if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
3529c3f94e1SPaolo Abeni 		goto drop;
35372f96132SPaolo Abeni 
354b7f653b2SFlorian Westphal 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
355b7f653b2SFlorian Westphal 
356ab174ad8SPaolo Abeni 	/* the skb map_seq accounts for the skb offset:
357ab174ad8SPaolo Abeni 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
358ab174ad8SPaolo Abeni 	 * value
359ab174ad8SPaolo Abeni 	 */
360ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
361ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
362ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->offset = offset;
363b7f653b2SFlorian Westphal 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
364ab174ad8SPaolo Abeni 
365ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
366ab174ad8SPaolo Abeni 		/* in sequence */
367917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
3684e637c70SFlorian Westphal 		tail = skb_peek_tail(&sk->sk_receive_queue);
3698268ed4cSPaolo Abeni 		if (tail && mptcp_try_coalesce(sk, tail, skb))
370ab174ad8SPaolo Abeni 			return true;
3714e637c70SFlorian Westphal 
3726511882cSPaolo Abeni 		mptcp_set_owner_r(skb, sk);
3736771bfd9SFlorian Westphal 		__skb_queue_tail(&sk->sk_receive_queue, skb);
374ab174ad8SPaolo Abeni 		return true;
375ab174ad8SPaolo Abeni 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
376ab174ad8SPaolo Abeni 		mptcp_data_queue_ofo(msk, skb);
377ab174ad8SPaolo Abeni 		return false;
378ab174ad8SPaolo Abeni 	}
379ab174ad8SPaolo Abeni 
380ab174ad8SPaolo Abeni 	/* old data, keep it simple and drop the whole pkt, sender
381ab174ad8SPaolo Abeni 	 * will retransmit as needed, if needed.
382ab174ad8SPaolo Abeni 	 */
38306242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
3849c3f94e1SPaolo Abeni drop:
385ab174ad8SPaolo Abeni 	mptcp_drop(sk, skb);
386ab174ad8SPaolo Abeni 	return false;
3876771bfd9SFlorian Westphal }
3886771bfd9SFlorian Westphal 
38916a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk)
39016a9a9daSMat Martineau {
39116a9a9daSMat Martineau 	struct inet_connection_sock *icsk = inet_csk(sk);
39216a9a9daSMat Martineau 
39316a9a9daSMat Martineau 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
39416a9a9daSMat Martineau 	mptcp_sk(sk)->timer_ival = 0;
39516a9a9daSMat Martineau }
39616a9a9daSMat Martineau 
397e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk)
398e16163b6SPaolo Abeni {
399e16163b6SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD))
400e16163b6SPaolo Abeni 		return;
401e16163b6SPaolo Abeni 
402e16163b6SPaolo Abeni 	sk->sk_state_change(sk);
403e16163b6SPaolo Abeni 	if (sk->sk_shutdown == SHUTDOWN_MASK ||
404e16163b6SPaolo Abeni 	    sk->sk_state == TCP_CLOSE)
405e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
406e16163b6SPaolo Abeni 	else
407e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
408e16163b6SPaolo Abeni }
409e16163b6SPaolo Abeni 
4106e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk)
4116e628cd3SPaolo Abeni {
4126e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
4136e628cd3SPaolo Abeni 
4146e628cd3SPaolo Abeni 	return !__mptcp_check_fallback(msk) &&
4156e628cd3SPaolo Abeni 	       ((1 << sk->sk_state) &
4166e628cd3SPaolo Abeni 		(TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
4176e628cd3SPaolo Abeni 	       msk->write_seq == READ_ONCE(msk->snd_una);
4186e628cd3SPaolo Abeni }
4196e628cd3SPaolo Abeni 
42016a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk)
42116a9a9daSMat Martineau {
42216a9a9daSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
42316a9a9daSMat Martineau 
42416a9a9daSMat Martineau 	/* Look for an acknowledged DATA_FIN */
4256e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk)) {
42616a9a9daSMat Martineau 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
42716a9a9daSMat Martineau 
42816a9a9daSMat Martineau 		switch (sk->sk_state) {
42916a9a9daSMat Martineau 		case TCP_FIN_WAIT1:
43016a9a9daSMat Martineau 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
43116a9a9daSMat Martineau 			break;
43216a9a9daSMat Martineau 		case TCP_CLOSING:
43316a9a9daSMat Martineau 		case TCP_LAST_ACK:
43416a9a9daSMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE);
43516a9a9daSMat Martineau 			break;
43616a9a9daSMat Martineau 		}
43716a9a9daSMat Martineau 
438e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
43916a9a9daSMat Martineau 	}
44016a9a9daSMat Martineau }
44116a9a9daSMat Martineau 
4423721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
4433721b9b6SMat Martineau {
4443721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
4453721b9b6SMat Martineau 
4463721b9b6SMat Martineau 	if (READ_ONCE(msk->rcv_data_fin) &&
4473721b9b6SMat Martineau 	    ((1 << sk->sk_state) &
4483721b9b6SMat Martineau 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
4493721b9b6SMat Martineau 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
4503721b9b6SMat Martineau 
4513721b9b6SMat Martineau 		if (msk->ack_seq == rcv_data_fin_seq) {
4523721b9b6SMat Martineau 			if (seq)
4533721b9b6SMat Martineau 				*seq = rcv_data_fin_seq;
4543721b9b6SMat Martineau 
4553721b9b6SMat Martineau 			return true;
4563721b9b6SMat Martineau 		}
4573721b9b6SMat Martineau 	}
4583721b9b6SMat Martineau 
4593721b9b6SMat Martineau 	return false;
4603721b9b6SMat Martineau }
4613721b9b6SMat Martineau 
462403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk)
4636477dd39SMat Martineau {
4646477dd39SMat Martineau 	struct inet_connection_sock *icsk = inet_csk(sk);
465877d11f0SMat Martineau 	u32 retransmits;
4666477dd39SMat Martineau 
467877d11f0SMat Martineau 	retransmits = min_t(u32, icsk->icsk_retransmits,
468877d11f0SMat Martineau 			    ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
469877d11f0SMat Martineau 
470877d11f0SMat Martineau 	mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
4716477dd39SMat Martineau }
4726477dd39SMat Martineau 
47333d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout)
4743721b9b6SMat Martineau {
4753721b9b6SMat Martineau 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
4763721b9b6SMat Martineau }
4773721b9b6SMat Martineau 
47833d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
47933d41c9cSPaolo Abeni {
48033d41c9cSPaolo Abeni 	const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
48133d41c9cSPaolo Abeni 
48271b7dec2SPaolo Abeni 	return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
48371b7dec2SPaolo Abeni 	       inet_csk(ssk)->icsk_timeout - jiffies : 0;
48433d41c9cSPaolo Abeni }
48533d41c9cSPaolo Abeni 
48633d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk)
48733d41c9cSPaolo Abeni {
48833d41c9cSPaolo Abeni 	struct mptcp_subflow_context *subflow;
48933d41c9cSPaolo Abeni 	long tout = 0;
49033d41c9cSPaolo Abeni 
49133d41c9cSPaolo Abeni 	mptcp_for_each_subflow(mptcp_sk(sk), subflow)
49233d41c9cSPaolo Abeni 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
49333d41c9cSPaolo Abeni 	__mptcp_set_timeout(sk, tout);
49433d41c9cSPaolo Abeni }
49533d41c9cSPaolo Abeni 
49676a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk)
497fd897679SPaolo Abeni {
498fd897679SPaolo Abeni 	return !((1 << inet_sk_state_load(ssk)) &
49920bc80b6SPaolo Abeni 	       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
500fd897679SPaolo Abeni }
501fd897679SPaolo Abeni 
502a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk)
503a6574302SMat Martineau {
504a6574302SMat Martineau 	if (tcp_can_send_ack(ssk))
505a6574302SMat Martineau 		tcp_send_ack(ssk);
506a6574302SMat Martineau }
507a6574302SMat Martineau 
508f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk)
5097ed90803SPaolo Abeni {
51075e908c3SPaolo Abeni 	bool slow;
5117ed90803SPaolo Abeni 
51275e908c3SPaolo Abeni 	slow = lock_sock_fast(ssk);
513a6574302SMat Martineau 	__mptcp_subflow_send_ack(ssk);
51475e908c3SPaolo Abeni 	unlock_sock_fast(ssk, slow);
515fd897679SPaolo Abeni }
516340fa666SMat Martineau 
517340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk)
518340fa666SMat Martineau {
519340fa666SMat Martineau 	struct mptcp_subflow_context *subflow;
520340fa666SMat Martineau 
521340fa666SMat Martineau 	mptcp_for_each_subflow(msk, subflow)
522340fa666SMat Martineau 		mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
523ea4ca586SPaolo Abeni }
524ea4ca586SPaolo Abeni 
525fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
526fd897679SPaolo Abeni {
52775e908c3SPaolo Abeni 	bool slow;
528fd897679SPaolo Abeni 
52975e908c3SPaolo Abeni 	slow = lock_sock_fast(ssk);
530fde56eeaSPaolo Abeni 	if (tcp_can_send_ack(ssk))
531fd897679SPaolo Abeni 		tcp_cleanup_rbuf(ssk, 1);
53275e908c3SPaolo Abeni 	unlock_sock_fast(ssk, slow);
533fde56eeaSPaolo Abeni }
534fde56eeaSPaolo Abeni 
535fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
536fde56eeaSPaolo Abeni {
537fde56eeaSPaolo Abeni 	const struct inet_connection_sock *icsk = inet_csk(ssk);
538c4512c63SMatthieu Baerts 	u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
539fde56eeaSPaolo Abeni 	const struct tcp_sock *tp = tcp_sk(ssk);
540fde56eeaSPaolo Abeni 
541fde56eeaSPaolo Abeni 	return (ack_pending & ICSK_ACK_SCHED) &&
542fde56eeaSPaolo Abeni 		((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
543fde56eeaSPaolo Abeni 		  READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
544fde56eeaSPaolo Abeni 		 (rx_empty && ack_pending &
545fde56eeaSPaolo Abeni 			      (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
546ea4ca586SPaolo Abeni }
547fd897679SPaolo Abeni 
548fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
549fd897679SPaolo Abeni {
550e3859603SPaolo Abeni 	int old_space = READ_ONCE(msk->old_wspace);
551fd897679SPaolo Abeni 	struct mptcp_subflow_context *subflow;
552e3859603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
553fde56eeaSPaolo Abeni 	int space =  __mptcp_space(sk);
554fde56eeaSPaolo Abeni 	bool cleanup, rx_empty;
555e3859603SPaolo Abeni 
556fde56eeaSPaolo Abeni 	cleanup = (space > 0) && (space >= (old_space << 1));
557ce599c51SPaolo Abeni 	rx_empty = !__mptcp_rmem(sk);
558fd897679SPaolo Abeni 
559fd897679SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
560fd897679SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
561fd897679SPaolo Abeni 
562fde56eeaSPaolo Abeni 		if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
563fde56eeaSPaolo Abeni 			mptcp_subflow_cleanup_rbuf(ssk);
5647ed90803SPaolo Abeni 	}
5657ed90803SPaolo Abeni }
5667ed90803SPaolo Abeni 
5677ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk)
5683721b9b6SMat Martineau {
5693721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
5703721b9b6SMat Martineau 	u64 rcv_data_fin_seq;
5717ed90803SPaolo Abeni 	bool ret = false;
5723721b9b6SMat Martineau 
573781bf13dSPaolo Abeni 	if (__mptcp_check_fallback(msk))
5747ed90803SPaolo Abeni 		return ret;
5753721b9b6SMat Martineau 
5763721b9b6SMat Martineau 	/* Need to ack a DATA_FIN received from a peer while this side
5773721b9b6SMat Martineau 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
5783721b9b6SMat Martineau 	 * msk->rcv_data_fin was set when parsing the incoming options
5793721b9b6SMat Martineau 	 * at the subflow level and the msk lock was not held, so this
5803721b9b6SMat Martineau 	 * is the first opportunity to act on the DATA_FIN and change
5813721b9b6SMat Martineau 	 * the msk state.
5823721b9b6SMat Martineau 	 *
5833721b9b6SMat Martineau 	 * If we are caught up to the sequence number of the incoming
5843721b9b6SMat Martineau 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
5853721b9b6SMat Martineau 	 * not caught up, do nothing and let the recv code send DATA_ACK
5863721b9b6SMat Martineau 	 * when catching up.
5873721b9b6SMat Martineau 	 */
5883721b9b6SMat Martineau 
5893721b9b6SMat Martineau 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
590917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
5913721b9b6SMat Martineau 		WRITE_ONCE(msk->rcv_data_fin, 0);
5923721b9b6SMat Martineau 
5933721b9b6SMat Martineau 		sk->sk_shutdown |= RCV_SHUTDOWN;
59416a9a9daSMat Martineau 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
5953721b9b6SMat Martineau 
5963721b9b6SMat Martineau 		switch (sk->sk_state) {
5973721b9b6SMat Martineau 		case TCP_ESTABLISHED:
5983721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE_WAIT);
5993721b9b6SMat Martineau 			break;
6003721b9b6SMat Martineau 		case TCP_FIN_WAIT1:
6013721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSING);
6023721b9b6SMat Martineau 			break;
6033721b9b6SMat Martineau 		case TCP_FIN_WAIT2:
6043721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE);
6053721b9b6SMat Martineau 			break;
6063721b9b6SMat Martineau 		default:
6073721b9b6SMat Martineau 			/* Other states not expected */
6083721b9b6SMat Martineau 			WARN_ON_ONCE(1);
6093721b9b6SMat Martineau 			break;
6103721b9b6SMat Martineau 		}
6113721b9b6SMat Martineau 
6127ed90803SPaolo Abeni 		ret = true;
613fd897679SPaolo Abeni 		mptcp_send_ack(msk);
614e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
6153721b9b6SMat Martineau 	}
6167ed90803SPaolo Abeni 	return ret;
6173721b9b6SMat Martineau }
6183721b9b6SMat Martineau 
6196771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
6206771bfd9SFlorian Westphal 					   struct sock *ssk,
6216771bfd9SFlorian Westphal 					   unsigned int *bytes)
6226771bfd9SFlorian Westphal {
6236771bfd9SFlorian Westphal 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
624600911ffSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
6256771bfd9SFlorian Westphal 	unsigned int moved = 0;
6266771bfd9SFlorian Westphal 	bool more_data_avail;
6276771bfd9SFlorian Westphal 	struct tcp_sock *tp;
6286771bfd9SFlorian Westphal 	bool done = false;
62913c7ba0cSFlorian Westphal 	int sk_rbuf;
63013c7ba0cSFlorian Westphal 
63113c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
63213c7ba0cSFlorian Westphal 
63313c7ba0cSFlorian Westphal 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
63413c7ba0cSFlorian Westphal 		int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
63513c7ba0cSFlorian Westphal 
63613c7ba0cSFlorian Westphal 		if (unlikely(ssk_rbuf > sk_rbuf)) {
63713c7ba0cSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
63813c7ba0cSFlorian Westphal 			sk_rbuf = ssk_rbuf;
63913c7ba0cSFlorian Westphal 		}
64013c7ba0cSFlorian Westphal 	}
641600911ffSFlorian Westphal 
642ab174ad8SPaolo Abeni 	pr_debug("msk=%p ssk=%p", msk, ssk);
6436771bfd9SFlorian Westphal 	tp = tcp_sk(ssk);
6446771bfd9SFlorian Westphal 	do {
6456771bfd9SFlorian Westphal 		u32 map_remaining, offset;
6466771bfd9SFlorian Westphal 		u32 seq = tp->copied_seq;
6476771bfd9SFlorian Westphal 		struct sk_buff *skb;
6486771bfd9SFlorian Westphal 		bool fin;
6496771bfd9SFlorian Westphal 
6506771bfd9SFlorian Westphal 		/* try to move as much data as available */
6516771bfd9SFlorian Westphal 		map_remaining = subflow->map_data_len -
6526771bfd9SFlorian Westphal 				mptcp_subflow_get_map_offset(subflow);
6536771bfd9SFlorian Westphal 
6546771bfd9SFlorian Westphal 		skb = skb_peek(&ssk->sk_receive_queue);
655d9fb8c50SPaolo Abeni 		if (!skb) {
656d89e3ed7SPaolo Abeni 			/* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
657d89e3ed7SPaolo Abeni 			 * a different CPU can have already processed the pending
658d89e3ed7SPaolo Abeni 			 * data, stop here or we can enter an infinite loop
659d9fb8c50SPaolo Abeni 			 */
660d9fb8c50SPaolo Abeni 			if (!moved)
661d9fb8c50SPaolo Abeni 				done = true;
6626771bfd9SFlorian Westphal 			break;
663d9fb8c50SPaolo Abeni 		}
6646771bfd9SFlorian Westphal 
665e1ff9e82SDavide Caratti 		if (__mptcp_check_fallback(msk)) {
666d89e3ed7SPaolo Abeni 			/* Under fallback skbs have no MPTCP extension and TCP could
667d89e3ed7SPaolo Abeni 			 * collapse them between the dummy map creation and the
668d89e3ed7SPaolo Abeni 			 * current dequeue. Be sure to adjust the map size.
669e1ff9e82SDavide Caratti 			 */
670e1ff9e82SDavide Caratti 			map_remaining = skb->len;
671e1ff9e82SDavide Caratti 			subflow->map_data_len = skb->len;
672e1ff9e82SDavide Caratti 		}
673e1ff9e82SDavide Caratti 
6746771bfd9SFlorian Westphal 		offset = seq - TCP_SKB_CB(skb)->seq;
6756771bfd9SFlorian Westphal 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
6766771bfd9SFlorian Westphal 		if (fin) {
6776771bfd9SFlorian Westphal 			done = true;
6786771bfd9SFlorian Westphal 			seq++;
6796771bfd9SFlorian Westphal 		}
6806771bfd9SFlorian Westphal 
6816771bfd9SFlorian Westphal 		if (offset < skb->len) {
6826771bfd9SFlorian Westphal 			size_t len = skb->len - offset;
6836771bfd9SFlorian Westphal 
6846771bfd9SFlorian Westphal 			if (tp->urg_data)
6856771bfd9SFlorian Westphal 				done = true;
6866771bfd9SFlorian Westphal 
687ab174ad8SPaolo Abeni 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
6886771bfd9SFlorian Westphal 				moved += len;
689ab174ad8SPaolo Abeni 			seq += len;
6906771bfd9SFlorian Westphal 
6916771bfd9SFlorian Westphal 			if (WARN_ON_ONCE(map_remaining < len))
6926771bfd9SFlorian Westphal 				break;
6936771bfd9SFlorian Westphal 		} else {
6946771bfd9SFlorian Westphal 			WARN_ON_ONCE(!fin);
6956771bfd9SFlorian Westphal 			sk_eat_skb(ssk, skb);
6966771bfd9SFlorian Westphal 			done = true;
6976771bfd9SFlorian Westphal 		}
6986771bfd9SFlorian Westphal 
6996771bfd9SFlorian Westphal 		WRITE_ONCE(tp->copied_seq, seq);
7006771bfd9SFlorian Westphal 		more_data_avail = mptcp_subflow_data_available(ssk);
701600911ffSFlorian Westphal 
70213c7ba0cSFlorian Westphal 		if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
703600911ffSFlorian Westphal 			done = true;
704600911ffSFlorian Westphal 			break;
705600911ffSFlorian Westphal 		}
7066771bfd9SFlorian Westphal 	} while (more_data_avail);
7076771bfd9SFlorian Westphal 
7086719331cSPaolo Abeni 	*bytes += moved;
7096771bfd9SFlorian Westphal 	return done;
7106771bfd9SFlorian Westphal }
7116771bfd9SFlorian Westphal 
71287952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
713ab174ad8SPaolo Abeni {
714ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
715ab174ad8SPaolo Abeni 	struct sk_buff *skb, *tail;
716ab174ad8SPaolo Abeni 	bool moved = false;
717ab174ad8SPaolo Abeni 	struct rb_node *p;
718ab174ad8SPaolo Abeni 	u64 end_seq;
719ab174ad8SPaolo Abeni 
720ab174ad8SPaolo Abeni 	p = rb_first(&msk->out_of_order_queue);
72106242e44SPaolo Abeni 	pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
722ab174ad8SPaolo Abeni 	while (p) {
723ab174ad8SPaolo Abeni 		skb = rb_to_skb(p);
724ab174ad8SPaolo Abeni 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
725ab174ad8SPaolo Abeni 			break;
726ab174ad8SPaolo Abeni 
727ab174ad8SPaolo Abeni 		p = rb_next(p);
728ab174ad8SPaolo Abeni 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
729ab174ad8SPaolo Abeni 
730ab174ad8SPaolo Abeni 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
731ab174ad8SPaolo Abeni 				      msk->ack_seq))) {
732ab174ad8SPaolo Abeni 			mptcp_drop(sk, skb);
73306242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
734ab174ad8SPaolo Abeni 			continue;
735ab174ad8SPaolo Abeni 		}
736ab174ad8SPaolo Abeni 
737ab174ad8SPaolo Abeni 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
738ab174ad8SPaolo Abeni 		tail = skb_peek_tail(&sk->sk_receive_queue);
739ab174ad8SPaolo Abeni 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
740ab174ad8SPaolo Abeni 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
741ab174ad8SPaolo Abeni 
742ab174ad8SPaolo Abeni 			/* skip overlapping data, if any */
74306242e44SPaolo Abeni 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
74406242e44SPaolo Abeni 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
74506242e44SPaolo Abeni 				 delta);
746ab174ad8SPaolo Abeni 			MPTCP_SKB_CB(skb)->offset += delta;
7472c9e7765SFlorian Westphal 			MPTCP_SKB_CB(skb)->map_seq += delta;
748ab174ad8SPaolo Abeni 			__skb_queue_tail(&sk->sk_receive_queue, skb);
749ab174ad8SPaolo Abeni 		}
750ab174ad8SPaolo Abeni 		msk->ack_seq = end_seq;
751ab174ad8SPaolo Abeni 		moved = true;
752ab174ad8SPaolo Abeni 	}
753ab174ad8SPaolo Abeni 	return moved;
754ab174ad8SPaolo Abeni }
755ab174ad8SPaolo Abeni 
7562e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket.  If its already
7572e52213cSFlorian Westphal  * owned, we need to defer to the work queue to avoid ABBA deadlock.
7582e52213cSFlorian Westphal  */
75999d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
7602e52213cSFlorian Westphal {
7612e52213cSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
7622e52213cSFlorian Westphal 	unsigned int moved = 0;
7632e52213cSFlorian Westphal 
7642e52213cSFlorian Westphal 	__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
76587952603SPaolo Abeni 	__mptcp_ofo_queue(msk);
766499ada50SPaolo Abeni 	if (unlikely(ssk->sk_err)) {
767499ada50SPaolo Abeni 		if (!sock_owned_by_user(sk))
768499ada50SPaolo Abeni 			__mptcp_error_report(sk);
769499ada50SPaolo Abeni 		else
770e9d09bacSPaolo Abeni 			__set_bit(MPTCP_ERROR_REPORT,  &msk->cb_flags);
771499ada50SPaolo Abeni 	}
772ab174ad8SPaolo Abeni 
773ab174ad8SPaolo Abeni 	/* If the moves have caught up with the DATA_FIN sequence number
774ab174ad8SPaolo Abeni 	 * it's time to ack the DATA_FIN and change socket state, but
775ab174ad8SPaolo Abeni 	 * this is not a good place to change state. Let the workqueue
776ab174ad8SPaolo Abeni 	 * do it.
777ab174ad8SPaolo Abeni 	 */
778ba8f48f7SPaolo Abeni 	if (mptcp_pending_data_fin(sk, NULL))
779ba8f48f7SPaolo Abeni 		mptcp_schedule_work(sk);
78099d1055cSPaolo Abeni 	return moved > 0;
7812e52213cSFlorian Westphal }
7822e52213cSFlorian Westphal 
7832e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk)
784101f6f85SFlorian Westphal {
7856719331cSPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
786101f6f85SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
78713c7ba0cSFlorian Westphal 	int sk_rbuf, ssk_rbuf;
788101f6f85SFlorian Westphal 
789d7b1bfd0SPaolo Abeni 	/* The peer can send data while we are shutting down this
790d7b1bfd0SPaolo Abeni 	 * subflow at msk destruction time, but we must avoid enqueuing
791d7b1bfd0SPaolo Abeni 	 * more data to the msk receive queue
792d7b1bfd0SPaolo Abeni 	 */
793d7b1bfd0SPaolo Abeni 	if (unlikely(subflow->disposable))
794d7b1bfd0SPaolo Abeni 		return;
795d7b1bfd0SPaolo Abeni 
79613c7ba0cSFlorian Westphal 	ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
79713c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
79813c7ba0cSFlorian Westphal 	if (unlikely(ssk_rbuf > sk_rbuf))
79913c7ba0cSFlorian Westphal 		sk_rbuf = ssk_rbuf;
80013c7ba0cSFlorian Westphal 
80199d1055cSPaolo Abeni 	/* over limit? can't append more skbs to msk, Also, no need to wake-up*/
802ce599c51SPaolo Abeni 	if (__mptcp_rmem(sk) > sk_rbuf) {
803ce599c51SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
80499d1055cSPaolo Abeni 		return;
805ce599c51SPaolo Abeni 	}
8062e52213cSFlorian Westphal 
80799d1055cSPaolo Abeni 	/* Wake-up the reader only for in-sequence data */
80899d1055cSPaolo Abeni 	mptcp_data_lock(sk);
809612f71d7SPaolo Abeni 	if (move_skbs_to_msk(msk, ssk))
810101f6f85SFlorian Westphal 		sk->sk_data_ready(sk);
811612f71d7SPaolo Abeni 
81299d1055cSPaolo Abeni 	mptcp_data_unlock(sk);
81399d1055cSPaolo Abeni }
814101f6f85SFlorian Westphal 
8153e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
816ec3edaa7SPeter Krystad {
8173e501490SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
8185cf92bbaSPaolo Abeni 
8193e501490SPaolo Abeni 	if (sk->sk_state != TCP_ESTABLISHED)
82078962489SFlorian Westphal 		return false;
821ec3edaa7SPeter Krystad 
8223e501490SPaolo Abeni 	/* attach to msk socket only after we are sure we will deal with it
8233e501490SPaolo Abeni 	 * at close time
8243e501490SPaolo Abeni 	 */
8253e501490SPaolo Abeni 	if (sk->sk_socket && !ssk->sk_socket)
8263e501490SPaolo Abeni 		mptcp_sock_graft(ssk, sk->sk_socket);
82778962489SFlorian Westphal 
8283e501490SPaolo Abeni 	mptcp_sockopt_sync_locked(msk, ssk);
8293e501490SPaolo Abeni 	return true;
83078962489SFlorian Westphal }
83178962489SFlorian Westphal 
8323e501490SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk)
83378962489SFlorian Westphal {
8343e501490SPaolo Abeni 	struct mptcp_subflow_context *tmp, *subflow;
8353e501490SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
83678962489SFlorian Westphal 
8373e501490SPaolo Abeni 	list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) {
8383e501490SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
8393e501490SPaolo Abeni 		bool slow = lock_sock_fast(ssk);
8403e501490SPaolo Abeni 
8413e501490SPaolo Abeni 		list_move_tail(&subflow->node, &msk->conn_list);
8423e501490SPaolo Abeni 		if (!__mptcp_finish_join(msk, ssk))
8433e501490SPaolo Abeni 			mptcp_subflow_reset(ssk);
8443e501490SPaolo Abeni 		unlock_sock_fast(ssk, slow);
84578962489SFlorian Westphal 	}
846ec3edaa7SPeter Krystad }
847ec3edaa7SPeter Krystad 
848b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk)
849b51f9b80SPaolo Abeni {
850b51f9b80SPaolo Abeni 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
851b51f9b80SPaolo Abeni }
852b51f9b80SPaolo Abeni 
853b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk)
854b51f9b80SPaolo Abeni {
855b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(sk);
856b51f9b80SPaolo Abeni 	unsigned long tout;
857b51f9b80SPaolo Abeni 
858e16163b6SPaolo Abeni 	/* prevent rescheduling on close */
859e16163b6SPaolo Abeni 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
860e16163b6SPaolo Abeni 		return;
861e16163b6SPaolo Abeni 
86233d41c9cSPaolo Abeni 	tout = mptcp_sk(sk)->timer_ival;
863b51f9b80SPaolo Abeni 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
864b51f9b80SPaolo Abeni }
865b51f9b80SPaolo Abeni 
866ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk)
867ba8f48f7SPaolo Abeni {
868ba8f48f7SPaolo Abeni 	if (inet_sk_state_load(sk) != TCP_CLOSE &&
869ba8f48f7SPaolo Abeni 	    schedule_work(&mptcp_sk(sk)->work)) {
870ba8f48f7SPaolo Abeni 		/* each subflow already holds a reference to the sk, and the
871ba8f48f7SPaolo Abeni 		 * workqueue is invoked by a subflow, so sk can't go away here.
872ba8f48f7SPaolo Abeni 		 */
873ba8f48f7SPaolo Abeni 		sock_hold(sk);
874ba8f48f7SPaolo Abeni 		return true;
875ba8f48f7SPaolo Abeni 	}
876ba8f48f7SPaolo Abeni 	return false;
877ba8f48f7SPaolo Abeni }
878ba8f48f7SPaolo Abeni 
87959832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk)
88059832e24SFlorian Westphal {
881ba8f48f7SPaolo Abeni 	if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
882ba8f48f7SPaolo Abeni 		mptcp_schedule_work(sk);
88359832e24SFlorian Westphal }
88459832e24SFlorian Westphal 
8855969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk)
8865969856aSPaolo Abeni {
8875969856aSPaolo Abeni 	struct mptcp_subflow_context *subflow;
8885969856aSPaolo Abeni 	struct sock *sk = (struct sock *)msk;
8895969856aSPaolo Abeni 	int receivers = 0;
8905969856aSPaolo Abeni 
8915969856aSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow)
8925969856aSPaolo Abeni 		receivers += !subflow->rx_eof;
893e16163b6SPaolo Abeni 	if (receivers)
894e16163b6SPaolo Abeni 		return;
8955969856aSPaolo Abeni 
896e16163b6SPaolo Abeni 	if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
8975969856aSPaolo Abeni 		/* hopefully temporary hack: propagate shutdown status
8985969856aSPaolo Abeni 		 * to msk, when all subflows agree on it
8995969856aSPaolo Abeni 		 */
9005969856aSPaolo Abeni 		sk->sk_shutdown |= RCV_SHUTDOWN;
9015969856aSPaolo Abeni 
9025969856aSPaolo Abeni 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
9035969856aSPaolo Abeni 		sk->sk_data_ready(sk);
9045969856aSPaolo Abeni 	}
905e16163b6SPaolo Abeni 
906e16163b6SPaolo Abeni 	switch (sk->sk_state) {
907e16163b6SPaolo Abeni 	case TCP_ESTABLISHED:
908e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE_WAIT);
909e16163b6SPaolo Abeni 		break;
910e16163b6SPaolo Abeni 	case TCP_FIN_WAIT1:
91126aa2314SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSING);
91226aa2314SPaolo Abeni 		break;
91326aa2314SPaolo Abeni 	case TCP_FIN_WAIT2:
914e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
915e16163b6SPaolo Abeni 		break;
916e16163b6SPaolo Abeni 	default:
917e16163b6SPaolo Abeni 		return;
918e16163b6SPaolo Abeni 	}
919e16163b6SPaolo Abeni 	mptcp_close_wake_up(sk);
9205969856aSPaolo Abeni }
9215969856aSPaolo Abeni 
9227a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
9237a6a6cbcSPaolo Abeni {
9247a6a6cbcSPaolo Abeni 	struct mptcp_subflow_context *subflow;
9257a6a6cbcSPaolo Abeni 
926109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
9277a6a6cbcSPaolo Abeni 
9287a6a6cbcSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
92999d1055cSPaolo Abeni 		if (READ_ONCE(subflow->data_avail))
9307a6a6cbcSPaolo Abeni 			return mptcp_subflow_tcp_sock(subflow);
9317a6a6cbcSPaolo Abeni 	}
9327a6a6cbcSPaolo Abeni 
9337a6a6cbcSPaolo Abeni 	return NULL;
9347a6a6cbcSPaolo Abeni }
9357a6a6cbcSPaolo Abeni 
9363f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq,
93757040755SPaolo Abeni 				      const struct sk_buff *skb,
93857040755SPaolo Abeni 				      const struct mptcp_ext *mpext)
9396d0060f6SMat Martineau {
94057040755SPaolo Abeni 	if (!tcp_skb_can_collapse_to(skb))
94157040755SPaolo Abeni 		return false;
94257040755SPaolo Abeni 
9435a369ca6SPaolo Abeni 	/* can collapse only if MPTCP level sequence is in order and this
9445a369ca6SPaolo Abeni 	 * mapping has not been xmitted yet
9455a369ca6SPaolo Abeni 	 */
9465a369ca6SPaolo Abeni 	return mpext && mpext->data_seq + mpext->data_len == write_seq &&
9475a369ca6SPaolo Abeni 	       !mpext->frozen;
94857040755SPaolo Abeni }
94957040755SPaolo Abeni 
95029249eacSPaolo Abeni /* we can append data to the given data frag if:
95129249eacSPaolo Abeni  * - there is space available in the backing page_frag
95229249eacSPaolo Abeni  * - the data frag tail matches the current page_frag free offset
95329249eacSPaolo Abeni  * - the data frag end sequence number matches the current write seq
95429249eacSPaolo Abeni  */
95518b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
95618b683bfSPaolo Abeni 				       const struct page_frag *pfrag,
95718b683bfSPaolo Abeni 				       const struct mptcp_data_frag *df)
95818b683bfSPaolo Abeni {
95918b683bfSPaolo Abeni 	return df && pfrag->page == df->page &&
960d9ca1de8SPaolo Abeni 		pfrag->size - pfrag->offset > 0 &&
96129249eacSPaolo Abeni 		pfrag->offset == (df->offset + df->data_len) &&
96218b683bfSPaolo Abeni 		df->data_seq + df->data_len == msk->write_seq;
96318b683bfSPaolo Abeni }
96418b683bfSPaolo Abeni 
965d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len)
96618b683bfSPaolo Abeni {
967d027236cSPaolo Abeni 	sk_mem_uncharge(sk, len);
9687948f6ccSFlorian Westphal 	sk_wmem_queued_add(sk, -len);
969d027236cSPaolo Abeni }
970d027236cSPaolo Abeni 
971d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
972d027236cSPaolo Abeni {
973d027236cSPaolo Abeni 	int len = dfrag->data_len + dfrag->overhead;
974d027236cSPaolo Abeni 
97518b683bfSPaolo Abeni 	list_del(&dfrag->list);
976d027236cSPaolo Abeni 	dfrag_uncharge(sk, len);
97718b683bfSPaolo Abeni 	put_page(dfrag->page);
97818b683bfSPaolo Abeni }
97918b683bfSPaolo Abeni 
9806e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk)
98118b683bfSPaolo Abeni {
98218b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
98318b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
984e1ff9e82SDavide Caratti 	u64 snd_una;
985e1ff9e82SDavide Caratti 
986e1ff9e82SDavide Caratti 	/* on fallback we just need to ignore snd_una, as this is really
987e1ff9e82SDavide Caratti 	 * plain TCP
988e1ff9e82SDavide Caratti 	 */
989e1ff9e82SDavide Caratti 	if (__mptcp_check_fallback(msk))
9907439d687SPaolo Abeni 		msk->snd_una = READ_ONCE(msk->snd_nxt);
9917439d687SPaolo Abeni 
9927439d687SPaolo Abeni 	snd_una = msk->snd_una;
99318b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
99418b683bfSPaolo Abeni 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
99518b683bfSPaolo Abeni 			break;
99618b683bfSPaolo Abeni 
9971e1d9d6fSPaolo Abeni 		if (unlikely(dfrag == msk->first_pending)) {
9981e1d9d6fSPaolo Abeni 			/* in recovery mode can see ack after the current snd head */
9991e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(!msk->recovery))
1000d9ca1de8SPaolo Abeni 				break;
10011e1d9d6fSPaolo Abeni 
10021e1d9d6fSPaolo Abeni 			WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
10031e1d9d6fSPaolo Abeni 		}
10041e1d9d6fSPaolo Abeni 
1005d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
1006d027236cSPaolo Abeni 	}
1007d027236cSPaolo Abeni 
10087948f6ccSFlorian Westphal 	dfrag = mptcp_rtx_head(sk);
10097948f6ccSFlorian Westphal 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
101053eb4c38SPaolo Abeni 		u64 delta = snd_una - dfrag->data_seq;
101153eb4c38SPaolo Abeni 
10121e1d9d6fSPaolo Abeni 		/* prevent wrap around in recovery mode */
10131e1d9d6fSPaolo Abeni 		if (unlikely(delta > dfrag->already_sent)) {
10141e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(!msk->recovery))
101553eb4c38SPaolo Abeni 				goto out;
10161e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(delta > dfrag->data_len))
10171e1d9d6fSPaolo Abeni 				goto out;
10181e1d9d6fSPaolo Abeni 			dfrag->already_sent += delta - dfrag->already_sent;
10191e1d9d6fSPaolo Abeni 		}
10207948f6ccSFlorian Westphal 
10217948f6ccSFlorian Westphal 		dfrag->data_seq += delta;
102253eb4c38SPaolo Abeni 		dfrag->offset += delta;
10237948f6ccSFlorian Westphal 		dfrag->data_len -= delta;
1024d9ca1de8SPaolo Abeni 		dfrag->already_sent -= delta;
10257948f6ccSFlorian Westphal 
10267948f6ccSFlorian Westphal 		dfrag_uncharge(sk, delta);
10277948f6ccSFlorian Westphal 	}
10287948f6ccSFlorian Westphal 
10291e1d9d6fSPaolo Abeni 	/* all retransmitted data acked, recovery completed */
10301e1d9d6fSPaolo Abeni 	if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
10311e1d9d6fSPaolo Abeni 		msk->recovery = false;
10321e1d9d6fSPaolo Abeni 
103353eb4c38SPaolo Abeni out:
10343241a9c0SFlorian Westphal 	if (snd_una == READ_ONCE(msk->snd_nxt) &&
10353241a9c0SFlorian Westphal 	    snd_una == READ_ONCE(msk->write_seq)) {
103633d41c9cSPaolo Abeni 		if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
10376e628cd3SPaolo Abeni 			mptcp_stop_timer(sk);
10386e628cd3SPaolo Abeni 	} else {
10396e628cd3SPaolo Abeni 		mptcp_reset_timer(sk);
10406e628cd3SPaolo Abeni 	}
10416e628cd3SPaolo Abeni }
10426e628cd3SPaolo Abeni 
1043417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk)
1044417789dfSPaolo Abeni {
1045765ff425SPaolo Abeni 	lockdep_assert_held_once(&sk->sk_lock.slock);
1046765ff425SPaolo Abeni 
1047417789dfSPaolo Abeni 	__mptcp_clean_una(sk);
1048417789dfSPaolo Abeni 	mptcp_write_space(sk);
1049417789dfSPaolo Abeni }
1050417789dfSPaolo Abeni 
1051b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk)
1052b5941f06SPaolo Abeni {
1053b5941f06SPaolo Abeni 	mptcp_data_lock(sk);
1054b5941f06SPaolo Abeni 	__mptcp_clean_una_wakeup(sk);
1055b5941f06SPaolo Abeni 	mptcp_data_unlock(sk);
1056b5941f06SPaolo Abeni }
1057b5941f06SPaolo Abeni 
1058724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk)
105918b683bfSPaolo Abeni {
1060d9ca1de8SPaolo Abeni 	struct mptcp_subflow_context *subflow;
1061d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1062d9ca1de8SPaolo Abeni 	bool first = true;
1063d9ca1de8SPaolo Abeni 
106418b683bfSPaolo Abeni 	sk_stream_moderate_sndbuf(sk);
1065d9ca1de8SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1066d9ca1de8SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1067d9ca1de8SPaolo Abeni 
1068d9ca1de8SPaolo Abeni 		if (first)
1069d9ca1de8SPaolo Abeni 			tcp_enter_memory_pressure(ssk);
1070d9ca1de8SPaolo Abeni 		sk_stream_moderate_sndbuf(ssk);
1071d9ca1de8SPaolo Abeni 		first = false;
1072d9ca1de8SPaolo Abeni 	}
1073724cfd2eSPaolo Abeni }
1074724cfd2eSPaolo Abeni 
1075724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1076724cfd2eSPaolo Abeni  * data
1077724cfd2eSPaolo Abeni  */
1078724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1079724cfd2eSPaolo Abeni {
1080724cfd2eSPaolo Abeni 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1081724cfd2eSPaolo Abeni 					pfrag, sk->sk_allocation)))
1082724cfd2eSPaolo Abeni 		return true;
1083724cfd2eSPaolo Abeni 
1084724cfd2eSPaolo Abeni 	mptcp_enter_memory_pressure(sk);
108518b683bfSPaolo Abeni 	return false;
108618b683bfSPaolo Abeni }
108718b683bfSPaolo Abeni 
108818b683bfSPaolo Abeni static struct mptcp_data_frag *
108918b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
109018b683bfSPaolo Abeni 		      int orig_offset)
109118b683bfSPaolo Abeni {
109218b683bfSPaolo Abeni 	int offset = ALIGN(orig_offset, sizeof(long));
109318b683bfSPaolo Abeni 	struct mptcp_data_frag *dfrag;
109418b683bfSPaolo Abeni 
109518b683bfSPaolo Abeni 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
109618b683bfSPaolo Abeni 	dfrag->data_len = 0;
109718b683bfSPaolo Abeni 	dfrag->data_seq = msk->write_seq;
109818b683bfSPaolo Abeni 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
109918b683bfSPaolo Abeni 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1100d9ca1de8SPaolo Abeni 	dfrag->already_sent = 0;
110118b683bfSPaolo Abeni 	dfrag->page = pfrag->page;
110218b683bfSPaolo Abeni 
110318b683bfSPaolo Abeni 	return dfrag;
110418b683bfSPaolo Abeni }
110518b683bfSPaolo Abeni 
1106caf971dfSPaolo Abeni struct mptcp_sendmsg_info {
1107caf971dfSPaolo Abeni 	int mss_now;
1108caf971dfSPaolo Abeni 	int size_goal;
1109d9ca1de8SPaolo Abeni 	u16 limit;
1110d9ca1de8SPaolo Abeni 	u16 sent;
1111d9ca1de8SPaolo Abeni 	unsigned int flags;
11121094c6feSPaolo Abeni 	bool data_lock_held;
1113caf971dfSPaolo Abeni };
1114caf971dfSPaolo Abeni 
1115b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1116b713d006SPaolo Abeni 				    u64 data_seq, int avail_size)
11176f8a612aSFlorian Westphal {
11186f8a612aSFlorian Westphal 	u64 window_end = mptcp_wnd_end(msk);
1119b713d006SPaolo Abeni 	u64 mptcp_snd_wnd;
11206f8a612aSFlorian Westphal 
11216f8a612aSFlorian Westphal 	if (__mptcp_check_fallback(msk))
11226f8a612aSFlorian Westphal 		return avail_size;
11236f8a612aSFlorian Westphal 
1124b713d006SPaolo Abeni 	mptcp_snd_wnd = window_end - data_seq;
1125b713d006SPaolo Abeni 	avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
11266f8a612aSFlorian Westphal 
112792be2f52SPaolo Abeni 	if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1128b713d006SPaolo Abeni 		tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
112992be2f52SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
113092be2f52SPaolo Abeni 	}
11316f8a612aSFlorian Westphal 
11326f8a612aSFlorian Westphal 	return avail_size;
11336f8a612aSFlorian Westphal }
11346f8a612aSFlorian Westphal 
1135724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1136724cfd2eSPaolo Abeni {
1137724cfd2eSPaolo Abeni 	struct skb_ext *mpext = __skb_ext_alloc(gfp);
1138724cfd2eSPaolo Abeni 
1139724cfd2eSPaolo Abeni 	if (!mpext)
1140724cfd2eSPaolo Abeni 		return false;
1141724cfd2eSPaolo Abeni 	__skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1142724cfd2eSPaolo Abeni 	return true;
1143724cfd2eSPaolo Abeni }
1144724cfd2eSPaolo Abeni 
11456e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1146724cfd2eSPaolo Abeni {
1147724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1148724cfd2eSPaolo Abeni 
11496e628cd3SPaolo Abeni 	skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1150724cfd2eSPaolo Abeni 	if (likely(skb)) {
11516e628cd3SPaolo Abeni 		if (likely(__mptcp_add_ext(skb, gfp))) {
1152724cfd2eSPaolo Abeni 			skb_reserve(skb, MAX_TCP_HEADER);
1153a52fe46eSEric Dumazet 			skb->ip_summed = CHECKSUM_PARTIAL;
1154f70cad10SPaolo Abeni 			INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1155724cfd2eSPaolo Abeni 			return skb;
1156724cfd2eSPaolo Abeni 		}
1157724cfd2eSPaolo Abeni 		__kfree_skb(skb);
1158724cfd2eSPaolo Abeni 	} else {
1159724cfd2eSPaolo Abeni 		mptcp_enter_memory_pressure(sk);
1160724cfd2eSPaolo Abeni 	}
1161724cfd2eSPaolo Abeni 	return NULL;
1162724cfd2eSPaolo Abeni }
1163724cfd2eSPaolo Abeni 
1164f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1165724cfd2eSPaolo Abeni {
1166724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1167724cfd2eSPaolo Abeni 
11686e628cd3SPaolo Abeni 	skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1169724cfd2eSPaolo Abeni 	if (!skb)
1170f70cad10SPaolo Abeni 		return NULL;
1171724cfd2eSPaolo Abeni 
1172724cfd2eSPaolo Abeni 	if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1173f70cad10SPaolo Abeni 		tcp_skb_entail(ssk, skb);
1174f70cad10SPaolo Abeni 		return skb;
1175724cfd2eSPaolo Abeni 	}
11763ef3905aSYonglong Li 	tcp_skb_tsorted_anchor_cleanup(skb);
1177724cfd2eSPaolo Abeni 	kfree_skb(skb);
1178f70cad10SPaolo Abeni 	return NULL;
1179724cfd2eSPaolo Abeni }
1180724cfd2eSPaolo Abeni 
1181f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1182724cfd2eSPaolo Abeni {
11831094c6feSPaolo Abeni 	gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1184724cfd2eSPaolo Abeni 
11851094c6feSPaolo Abeni 	return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1186724cfd2eSPaolo Abeni }
1187724cfd2eSPaolo Abeni 
1188d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even
1189d0cc2987SGeliang Tang  * if we just appended a single frag. More status info needed
1190d0cc2987SGeliang Tang  */
1191d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1192d0cc2987SGeliang Tang {
1193d0cc2987SGeliang Tang 	struct mptcp_ext *mpext = mptcp_get_ext(skb);
1194d0cc2987SGeliang Tang 	__wsum csum = ~csum_unfold(mpext->csum);
1195d0cc2987SGeliang Tang 	int offset = skb->len - added;
1196d0cc2987SGeliang Tang 
1197d0cc2987SGeliang Tang 	mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1198d0cc2987SGeliang Tang }
1199d0cc2987SGeliang Tang 
12001e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk,
12011e39e5a3SGeliang Tang 				      struct sock *ssk,
12021e39e5a3SGeliang Tang 				      struct mptcp_ext *mpext)
12031e39e5a3SGeliang Tang {
12041e39e5a3SGeliang Tang 	if (!mpext)
12051e39e5a3SGeliang Tang 		return;
12061e39e5a3SGeliang Tang 
12071e39e5a3SGeliang Tang 	mpext->infinite_map = 1;
12081e39e5a3SGeliang Tang 	mpext->data_len = 0;
12091e39e5a3SGeliang Tang 
1210104125b8SGeliang Tang 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
12111e39e5a3SGeliang Tang 	mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
12121e39e5a3SGeliang Tang 	pr_fallback(msk);
1213d51991e2SPaolo Abeni 	mptcp_do_fallback(ssk);
12141e39e5a3SGeliang Tang }
12151e39e5a3SGeliang Tang 
121657040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1217d9ca1de8SPaolo Abeni 			      struct mptcp_data_frag *dfrag,
1218caf971dfSPaolo Abeni 			      struct mptcp_sendmsg_info *info)
121957040755SPaolo Abeni {
1220d9ca1de8SPaolo Abeni 	u64 data_seq = dfrag->data_seq + info->sent;
1221f70cad10SPaolo Abeni 	int offset = dfrag->offset + info->sent;
12226d0060f6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
12236f8a612aSFlorian Westphal 	bool zero_window_probe = false;
12246d0060f6SMat Martineau 	struct mptcp_ext *mpext = NULL;
1225f70cad10SPaolo Abeni 	bool can_coalesce = false;
1226f70cad10SPaolo Abeni 	bool reuse_skb = true;
1227f70cad10SPaolo Abeni 	struct sk_buff *skb;
1228f70cad10SPaolo Abeni 	size_t copy;
1229f70cad10SPaolo Abeni 	int i;
12306d0060f6SMat Martineau 
1231e4b61351SGeliang Tang 	pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1232d9ca1de8SPaolo Abeni 		 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
12336d0060f6SMat Martineau 
1234f70cad10SPaolo Abeni 	if (WARN_ON_ONCE(info->sent > info->limit ||
1235f70cad10SPaolo Abeni 			 info->limit > dfrag->data_len))
1236f70cad10SPaolo Abeni 		return 0;
1237f70cad10SPaolo Abeni 
1238c886d702SPaolo Abeni 	if (unlikely(!__tcp_can_send(ssk)))
1239c886d702SPaolo Abeni 		return -EAGAIN;
1240c886d702SPaolo Abeni 
1241d9ca1de8SPaolo Abeni 	/* compute send limit */
1242d9ca1de8SPaolo Abeni 	info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1243f70cad10SPaolo Abeni 	copy = info->size_goal;
1244f70cad10SPaolo Abeni 
124557040755SPaolo Abeni 	skb = tcp_write_queue_tail(ssk);
1246f70cad10SPaolo Abeni 	if (skb && copy > skb->len) {
124757040755SPaolo Abeni 		/* Limit the write to the size available in the
124857040755SPaolo Abeni 		 * current skb, if any, so that we create at most a new skb.
124957040755SPaolo Abeni 		 * Explicitly tells TCP internals to avoid collapsing on later
125057040755SPaolo Abeni 		 * queue management operation, to avoid breaking the ext <->
125157040755SPaolo Abeni 		 * SSN association set here
125257040755SPaolo Abeni 		 */
1253d9ca1de8SPaolo Abeni 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
12541094c6feSPaolo Abeni 		if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
125557040755SPaolo Abeni 			TCP_SKB_CB(skb)->eor = 1;
12561094c6feSPaolo Abeni 			goto alloc_skb;
12571094c6feSPaolo Abeni 		}
12581094c6feSPaolo Abeni 
1259f70cad10SPaolo Abeni 		i = skb_shinfo(skb)->nr_frags;
1260f70cad10SPaolo Abeni 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1261657b991aSKuniyuki Iwashima 		if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1262f70cad10SPaolo Abeni 			tcp_mark_push(tcp_sk(ssk), skb);
1263f70cad10SPaolo Abeni 			goto alloc_skb;
126415e6ca97SPaolo Abeni 		}
126518b683bfSPaolo Abeni 
1266f70cad10SPaolo Abeni 		copy -= skb->len;
1267f70cad10SPaolo Abeni 	} else {
12681094c6feSPaolo Abeni alloc_skb:
1269f70cad10SPaolo Abeni 		skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1270f70cad10SPaolo Abeni 		if (!skb)
1271f70cad10SPaolo Abeni 			return -ENOMEM;
1272f70cad10SPaolo Abeni 
1273f70cad10SPaolo Abeni 		i = skb_shinfo(skb)->nr_frags;
1274f70cad10SPaolo Abeni 		reuse_skb = false;
1275f70cad10SPaolo Abeni 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1276f70cad10SPaolo Abeni 	}
12771094c6feSPaolo Abeni 
12786f8a612aSFlorian Westphal 	/* Zero window and all data acked? Probe. */
1279b713d006SPaolo Abeni 	copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1280f70cad10SPaolo Abeni 	if (copy == 0) {
12817439d687SPaolo Abeni 		u64 snd_una = READ_ONCE(msk->snd_una);
12827439d687SPaolo Abeni 
1283f70cad10SPaolo Abeni 		if (snd_una != msk->snd_nxt) {
128427728ba8SEric Dumazet 			tcp_remove_empty_skb(ssk);
12856f8a612aSFlorian Westphal 			return 0;
12866f8a612aSFlorian Westphal 		}
12876f8a612aSFlorian Westphal 
1288f70cad10SPaolo Abeni 		zero_window_probe = true;
1289f70cad10SPaolo Abeni 		data_seq = snd_una - 1;
1290f70cad10SPaolo Abeni 		copy = 1;
129157040755SPaolo Abeni 
1292f70cad10SPaolo Abeni 		/* all mptcp-level data is acked, no skbs should be present into the
1293f70cad10SPaolo Abeni 		 * ssk write queue
1294f70cad10SPaolo Abeni 		 */
1295f70cad10SPaolo Abeni 		WARN_ON_ONCE(reuse_skb);
1296f70cad10SPaolo Abeni 	}
1297f70cad10SPaolo Abeni 
1298f70cad10SPaolo Abeni 	copy = min_t(size_t, copy, info->limit - info->sent);
1299f70cad10SPaolo Abeni 	if (!sk_wmem_schedule(ssk, copy)) {
130027728ba8SEric Dumazet 		tcp_remove_empty_skb(ssk);
1301e2223995SPaolo Abeni 		return -ENOMEM;
130235759383SFlorian Westphal 	}
130318b683bfSPaolo Abeni 
1304f70cad10SPaolo Abeni 	if (can_coalesce) {
1305f70cad10SPaolo Abeni 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1306f70cad10SPaolo Abeni 	} else {
1307f70cad10SPaolo Abeni 		get_page(dfrag->page);
1308f70cad10SPaolo Abeni 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
130957040755SPaolo Abeni 	}
131057040755SPaolo Abeni 
1311f70cad10SPaolo Abeni 	skb->len += copy;
1312f70cad10SPaolo Abeni 	skb->data_len += copy;
1313f70cad10SPaolo Abeni 	skb->truesize += copy;
1314f70cad10SPaolo Abeni 	sk_wmem_queued_add(ssk, copy);
1315f70cad10SPaolo Abeni 	sk_mem_charge(ssk, copy);
1316f70cad10SPaolo Abeni 	WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1317f70cad10SPaolo Abeni 	TCP_SKB_CB(skb)->end_seq += copy;
1318f70cad10SPaolo Abeni 	tcp_skb_pcount_set(skb, 0);
1319f70cad10SPaolo Abeni 
1320f70cad10SPaolo Abeni 	/* on skb reuse we just need to update the DSS len */
1321f70cad10SPaolo Abeni 	if (reuse_skb) {
1322f70cad10SPaolo Abeni 		TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1323f70cad10SPaolo Abeni 		mpext->data_len += copy;
1324f70cad10SPaolo Abeni 		WARN_ON_ONCE(zero_window_probe);
1325f70cad10SPaolo Abeni 		goto out;
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;
1331f70cad10SPaolo Abeni 	mpext->data_len = copy;
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) {
1340f70cad10SPaolo Abeni 		mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
13416f8a612aSFlorian Westphal 		mpext->frozen = 1;
1342d0cc2987SGeliang Tang 		if (READ_ONCE(msk->csum_enabled))
1343f70cad10SPaolo Abeni 			mptcp_update_data_checksum(skb, copy);
13446f8a612aSFlorian Westphal 		tcp_push_pending_frames(ssk);
1345d0cc2987SGeliang Tang 		return 0;
13466f8a612aSFlorian Westphal 	}
134757040755SPaolo Abeni out:
1348d0cc2987SGeliang Tang 	if (READ_ONCE(msk->csum_enabled))
1349f70cad10SPaolo Abeni 		mptcp_update_data_checksum(skb, copy);
13501e39e5a3SGeliang Tang 	if (mptcp_subflow_ctx(ssk)->send_infinite_map)
13511e39e5a3SGeliang Tang 		mptcp_update_infinite_map(msk, ssk, mpext);
13520eb4e7eeSGeliang Tang 	trace_mptcp_sendmsg_frag(mpext);
1353f70cad10SPaolo Abeni 	mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1354f70cad10SPaolo Abeni 	return copy;
13556d0060f6SMat Martineau }
13566d0060f6SMat Martineau 
1357d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
1358d5f49190SPaolo Abeni 					 sizeof(struct tcphdr) - \
1359d5f49190SPaolo Abeni 					 MAX_TCP_OPTION_SPACE - \
1360d5f49190SPaolo Abeni 					 sizeof(struct ipv6hdr) - \
1361d5f49190SPaolo Abeni 					 sizeof(struct frag_hdr))
1362d5f49190SPaolo Abeni 
1363d5f49190SPaolo Abeni struct subflow_send_info {
1364d5f49190SPaolo Abeni 	struct sock *ssk;
13653ce0852cSPaolo Abeni 	u64 linger_time;
1366d5f49190SPaolo Abeni };
1367d5f49190SPaolo Abeni 
1368ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1369ff5a0b42SPaolo Abeni {
1370ff5a0b42SPaolo Abeni 	if (!subflow->stale)
1371ff5a0b42SPaolo Abeni 		return;
1372ff5a0b42SPaolo Abeni 
1373ff5a0b42SPaolo Abeni 	subflow->stale = 0;
1374fc1b4e3bSPaolo Abeni 	MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1375ff5a0b42SPaolo Abeni }
1376ff5a0b42SPaolo Abeni 
1377ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1378ff5a0b42SPaolo Abeni {
1379ff5a0b42SPaolo Abeni 	if (unlikely(subflow->stale)) {
1380ff5a0b42SPaolo Abeni 		u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1381ff5a0b42SPaolo Abeni 
1382ff5a0b42SPaolo Abeni 		if (subflow->stale_rcv_tstamp == rcv_tstamp)
1383ff5a0b42SPaolo Abeni 			return false;
1384ff5a0b42SPaolo Abeni 
1385ff5a0b42SPaolo Abeni 		mptcp_subflow_set_active(subflow);
1386ff5a0b42SPaolo Abeni 	}
1387ff5a0b42SPaolo Abeni 	return __mptcp_subflow_active(subflow);
1388ff5a0b42SPaolo Abeni }
1389ff5a0b42SPaolo Abeni 
13903ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE	0
13913ce0852cSPaolo Abeni #define SSK_MODE_BACKUP	1
13923ce0852cSPaolo Abeni #define SSK_MODE_MAX	2
13933ce0852cSPaolo Abeni 
139433d41c9cSPaolo Abeni /* implement the mptcp packet scheduler;
139533d41c9cSPaolo Abeni  * returns the subflow that will transmit the next DSS
139633d41c9cSPaolo Abeni  * additionally updates the rtx timeout
139733d41c9cSPaolo Abeni  */
13985cf92bbaSPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1399f296234cSPeter Krystad {
14003ce0852cSPaolo Abeni 	struct subflow_send_info send_info[SSK_MODE_MAX];
1401f296234cSPeter Krystad 	struct mptcp_subflow_context *subflow;
140233d41c9cSPaolo Abeni 	struct sock *sk = (struct sock *)msk;
14033ce0852cSPaolo Abeni 	u32 pace, burst, wmem;
1404d5f49190SPaolo Abeni 	int i, nr_active = 0;
1405d5f49190SPaolo Abeni 	struct sock *ssk;
14063ce0852cSPaolo Abeni 	u64 linger_time;
140733d41c9cSPaolo Abeni 	long tout = 0;
1408f296234cSPeter Krystad 
1409109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
1410f296234cSPeter Krystad 
1411d5f49190SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
1412d5f49190SPaolo Abeni 		if (!msk->first)
1413f296234cSPeter Krystad 			return NULL;
1414c886d702SPaolo Abeni 		return __tcp_can_send(msk->first) &&
1415c886d702SPaolo Abeni 		       sk_stream_memory_free(msk->first) ? msk->first : NULL;
1416f296234cSPeter Krystad 	}
1417f296234cSPeter Krystad 
1418d5f49190SPaolo Abeni 	/* re-use last subflow, if the burst allow that */
1419d5f49190SPaolo Abeni 	if (msk->last_snd && msk->snd_burst > 0 &&
1420d5f49190SPaolo Abeni 	    sk_stream_memory_free(msk->last_snd) &&
142133d41c9cSPaolo Abeni 	    mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
142233d41c9cSPaolo Abeni 		mptcp_set_timeout(sk);
1423d5f49190SPaolo Abeni 		return msk->last_snd;
142433d41c9cSPaolo Abeni 	}
1425f296234cSPeter Krystad 
1426d5f49190SPaolo Abeni 	/* pick the subflow with the lower wmem/wspace ratio */
14273ce0852cSPaolo Abeni 	for (i = 0; i < SSK_MODE_MAX; ++i) {
1428d5f49190SPaolo Abeni 		send_info[i].ssk = NULL;
14293ce0852cSPaolo Abeni 		send_info[i].linger_time = -1;
1430d5f49190SPaolo Abeni 	}
14313ce0852cSPaolo Abeni 
1432d5f49190SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1433e10a9892SGeliang Tang 		trace_mptcp_subflow_get_send(subflow);
1434d5f49190SPaolo Abeni 		ssk =  mptcp_subflow_tcp_sock(subflow);
1435d5f49190SPaolo Abeni 		if (!mptcp_subflow_active(subflow))
1436f296234cSPeter Krystad 			continue;
1437d5f49190SPaolo Abeni 
143833d41c9cSPaolo Abeni 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
1439d5f49190SPaolo Abeni 		nr_active += !subflow->backup;
14403ce0852cSPaolo Abeni 		pace = subflow->avg_pacing_rate;
14413ce0852cSPaolo Abeni 		if (unlikely(!pace)) {
14423ce0852cSPaolo Abeni 			/* init pacing rate from socket */
14433ce0852cSPaolo Abeni 			subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
14443ce0852cSPaolo Abeni 			pace = subflow->avg_pacing_rate;
1445d5f49190SPaolo Abeni 			if (!pace)
1446d5f49190SPaolo Abeni 				continue;
14473ce0852cSPaolo Abeni 		}
1448d5f49190SPaolo Abeni 
14493ce0852cSPaolo Abeni 		linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
14503ce0852cSPaolo Abeni 		if (linger_time < send_info[subflow->backup].linger_time) {
1451d5f49190SPaolo Abeni 			send_info[subflow->backup].ssk = ssk;
14523ce0852cSPaolo Abeni 			send_info[subflow->backup].linger_time = linger_time;
1453d5f49190SPaolo Abeni 		}
1454f296234cSPeter Krystad 	}
145533d41c9cSPaolo Abeni 	__mptcp_set_timeout(sk, tout);
1456f296234cSPeter Krystad 
1457d5f49190SPaolo Abeni 	/* pick the best backup if no other subflow is active */
1458d5f49190SPaolo Abeni 	if (!nr_active)
14593ce0852cSPaolo Abeni 		send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1460d5f49190SPaolo Abeni 
14613ce0852cSPaolo Abeni 	/* According to the blest algorithm, to avoid HoL blocking for the
14623ce0852cSPaolo Abeni 	 * faster flow, we need to:
14633ce0852cSPaolo Abeni 	 * - estimate the faster flow linger time
14643ce0852cSPaolo Abeni 	 * - use the above to estimate the amount of byte transferred
14653ce0852cSPaolo Abeni 	 *   by the faster flow
14663ce0852cSPaolo Abeni 	 * - check that the amount of queued data is greter than the above,
14673ce0852cSPaolo Abeni 	 *   otherwise do not use the picked, slower, subflow
14683ce0852cSPaolo Abeni 	 * We select the subflow with the shorter estimated time to flush
14693ce0852cSPaolo Abeni 	 * the queued mem, which basically ensure the above. We just need
14703ce0852cSPaolo Abeni 	 * to check that subflow has a non empty cwin.
14713ce0852cSPaolo Abeni 	 */
14723ce0852cSPaolo Abeni 	ssk = send_info[SSK_MODE_ACTIVE].ssk;
1473b713d006SPaolo Abeni 	if (!ssk || !sk_stream_memory_free(ssk))
1474d5f49190SPaolo Abeni 		return NULL;
14753ce0852cSPaolo Abeni 
1476b713d006SPaolo Abeni 	burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
14773ce0852cSPaolo Abeni 	wmem = READ_ONCE(ssk->sk_wmem_queued);
1478b713d006SPaolo Abeni 	if (!burst) {
1479b713d006SPaolo Abeni 		msk->last_snd = NULL;
1480b713d006SPaolo Abeni 		return ssk;
1481b713d006SPaolo Abeni 	}
1482b713d006SPaolo Abeni 
14833ce0852cSPaolo Abeni 	subflow = mptcp_subflow_ctx(ssk);
14843ce0852cSPaolo Abeni 	subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
14853ce0852cSPaolo Abeni 					   READ_ONCE(ssk->sk_pacing_rate) * burst,
14863ce0852cSPaolo Abeni 					   burst + wmem);
14873ce0852cSPaolo Abeni 	msk->last_snd = ssk;
14883ce0852cSPaolo Abeni 	msk->snd_burst = burst;
14893ce0852cSPaolo Abeni 	return ssk;
1490f296234cSPeter Krystad }
1491f296234cSPeter Krystad 
1492b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1493d9ca1de8SPaolo Abeni {
1494d9ca1de8SPaolo Abeni 	tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1495d9ca1de8SPaolo Abeni 	release_sock(ssk);
1496d9ca1de8SPaolo Abeni }
1497d9ca1de8SPaolo Abeni 
14980d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk,
14990d199e43SFlorian Westphal 				   struct mptcp_data_frag *dfrag,
15000d199e43SFlorian Westphal 				   u32 sent)
15010d199e43SFlorian Westphal {
15020d199e43SFlorian Westphal 	u64 snd_nxt_new = dfrag->data_seq;
15030d199e43SFlorian Westphal 
15040d199e43SFlorian Westphal 	dfrag->already_sent += sent;
15050d199e43SFlorian Westphal 
15060d199e43SFlorian Westphal 	msk->snd_burst -= sent;
15070d199e43SFlorian Westphal 
15080d199e43SFlorian Westphal 	snd_nxt_new += dfrag->already_sent;
15090d199e43SFlorian Westphal 
15100d199e43SFlorian Westphal 	/* snd_nxt_new can be smaller than snd_nxt in case mptcp
15110d199e43SFlorian Westphal 	 * is recovering after a failover. In that event, this re-sends
15120d199e43SFlorian Westphal 	 * old segments.
15130d199e43SFlorian Westphal 	 *
15140d199e43SFlorian Westphal 	 * Thus compute snd_nxt_new candidate based on
15150d199e43SFlorian Westphal 	 * the dfrag->data_seq that was sent and the data
15160d199e43SFlorian Westphal 	 * that has been handed to the subflow for transmission
15170d199e43SFlorian Westphal 	 * and skip update in case it was old dfrag.
15180d199e43SFlorian Westphal 	 */
15190d199e43SFlorian Westphal 	if (likely(after64(snd_nxt_new, msk->snd_nxt)))
15200d199e43SFlorian Westphal 		msk->snd_nxt = snd_nxt_new;
15210d199e43SFlorian Westphal }
15220d199e43SFlorian Westphal 
15238b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk)
15243241a9c0SFlorian Westphal {
1525e9d09bacSPaolo Abeni 	if (mptcp_send_head(sk))
1526e9d09bacSPaolo Abeni 		mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
15273241a9c0SFlorian Westphal }
15283241a9c0SFlorian Westphal 
1529ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1530d9ca1de8SPaolo Abeni {
1531d9ca1de8SPaolo Abeni 	struct sock *prev_ssk = NULL, *ssk = NULL;
1532d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1533d9ca1de8SPaolo Abeni 	struct mptcp_sendmsg_info info = {
1534d9ca1de8SPaolo Abeni 				.flags = flags,
1535d9ca1de8SPaolo Abeni 	};
15360522b424SGeliang Tang 	bool do_check_data_fin = false;
1537d9ca1de8SPaolo Abeni 	struct mptcp_data_frag *dfrag;
15380522b424SGeliang Tang 	int len;
1539d9ca1de8SPaolo Abeni 
1540d9ca1de8SPaolo Abeni 	while ((dfrag = mptcp_send_head(sk))) {
1541d9ca1de8SPaolo Abeni 		info.sent = dfrag->already_sent;
1542d9ca1de8SPaolo Abeni 		info.limit = dfrag->data_len;
1543d9ca1de8SPaolo Abeni 		len = dfrag->data_len - dfrag->already_sent;
1544d9ca1de8SPaolo Abeni 		while (len > 0) {
1545d9ca1de8SPaolo Abeni 			int ret = 0;
1546d9ca1de8SPaolo Abeni 
1547d9ca1de8SPaolo Abeni 			prev_ssk = ssk;
15485cf92bbaSPaolo Abeni 			ssk = mptcp_subflow_get_send(msk);
1549d9ca1de8SPaolo Abeni 
15509758f40eSPaolo Abeni 			/* First check. If the ssk has changed since
15519758f40eSPaolo Abeni 			 * the last round, release prev_ssk
1552d9ca1de8SPaolo Abeni 			 */
1553d9ca1de8SPaolo Abeni 			if (ssk != prev_ssk && prev_ssk)
1554b8e0def3SGeliang Tang 				mptcp_push_release(prev_ssk, &info);
1555d9ca1de8SPaolo Abeni 			if (!ssk)
1556d9ca1de8SPaolo Abeni 				goto out;
1557d9ca1de8SPaolo Abeni 
15589758f40eSPaolo Abeni 			/* Need to lock the new subflow only if different
15599758f40eSPaolo Abeni 			 * from the previous one, otherwise we are still
15609758f40eSPaolo Abeni 			 * helding the relevant lock
15619758f40eSPaolo Abeni 			 */
15629758f40eSPaolo Abeni 			if (ssk != prev_ssk)
1563d9ca1de8SPaolo Abeni 				lock_sock(ssk);
1564d9ca1de8SPaolo Abeni 
1565d9ca1de8SPaolo Abeni 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1566d9ca1de8SPaolo Abeni 			if (ret <= 0) {
1567c886d702SPaolo Abeni 				if (ret == -EAGAIN)
1568c886d702SPaolo Abeni 					continue;
1569b8e0def3SGeliang Tang 				mptcp_push_release(ssk, &info);
1570d9ca1de8SPaolo Abeni 				goto out;
1571d9ca1de8SPaolo Abeni 			}
1572d9ca1de8SPaolo Abeni 
15730522b424SGeliang Tang 			do_check_data_fin = true;
1574d9ca1de8SPaolo Abeni 			info.sent += ret;
1575d9ca1de8SPaolo Abeni 			len -= ret;
15760d199e43SFlorian Westphal 
15770d199e43SFlorian Westphal 			mptcp_update_post_push(msk, dfrag, ret);
1578d9ca1de8SPaolo Abeni 		}
1579d9ca1de8SPaolo Abeni 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1580d9ca1de8SPaolo Abeni 	}
1581d9ca1de8SPaolo Abeni 
1582d9ca1de8SPaolo Abeni 	/* at this point we held the socket lock for the last subflow we used */
1583d9ca1de8SPaolo Abeni 	if (ssk)
1584b8e0def3SGeliang Tang 		mptcp_push_release(ssk, &info);
1585d9ca1de8SPaolo Abeni 
1586d9ca1de8SPaolo Abeni out:
158733d41c9cSPaolo Abeni 	/* ensure the rtx timer is running */
1588d9ca1de8SPaolo Abeni 	if (!mptcp_timer_pending(sk))
1589d9ca1de8SPaolo Abeni 		mptcp_reset_timer(sk);
15900522b424SGeliang Tang 	if (do_check_data_fin)
1591d9ca1de8SPaolo Abeni 		__mptcp_check_send_data_fin(sk);
1592d9ca1de8SPaolo Abeni }
1593d9ca1de8SPaolo Abeni 
159473a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
15956e628cd3SPaolo Abeni {
15966e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
15971094c6feSPaolo Abeni 	struct mptcp_sendmsg_info info = {
15981094c6feSPaolo Abeni 		.data_lock_held = true,
15991094c6feSPaolo Abeni 	};
16006e628cd3SPaolo Abeni 	struct mptcp_data_frag *dfrag;
1601b19bc294SPaolo Abeni 	struct sock *xmit_ssk;
16026e628cd3SPaolo Abeni 	int len, copied = 0;
16036e628cd3SPaolo Abeni 
16046e628cd3SPaolo Abeni 	info.flags = 0;
16056e628cd3SPaolo Abeni 	while ((dfrag = mptcp_send_head(sk))) {
16066e628cd3SPaolo Abeni 		info.sent = dfrag->already_sent;
16076e628cd3SPaolo Abeni 		info.limit = dfrag->data_len;
16086e628cd3SPaolo Abeni 		len = dfrag->data_len - dfrag->already_sent;
16096e628cd3SPaolo Abeni 		while (len > 0) {
16106e628cd3SPaolo Abeni 			int ret = 0;
16116e628cd3SPaolo Abeni 
161273a0052aSGeliang Tang 			/* check for a different subflow usage only after
1613b19bc294SPaolo Abeni 			 * spooling the first chunk of data
1614b19bc294SPaolo Abeni 			 */
161500df24f1SGeliang Tang 			xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk);
1616b19bc294SPaolo Abeni 			if (!xmit_ssk)
1617b19bc294SPaolo Abeni 				goto out;
1618b19bc294SPaolo Abeni 			if (xmit_ssk != ssk) {
1619bcd97734SPaolo Abeni 				mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1620bcd97734SPaolo Abeni 						       MPTCP_DELEGATE_SEND);
1621b19bc294SPaolo Abeni 				goto out;
1622b19bc294SPaolo Abeni 			}
1623b19bc294SPaolo Abeni 
16246e628cd3SPaolo Abeni 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
16256e628cd3SPaolo Abeni 			if (ret <= 0)
16266e628cd3SPaolo Abeni 				goto out;
16276e628cd3SPaolo Abeni 
16286e628cd3SPaolo Abeni 			info.sent += ret;
16296e628cd3SPaolo Abeni 			copied += ret;
16306e628cd3SPaolo Abeni 			len -= ret;
1631b19bc294SPaolo Abeni 			first = false;
16320d199e43SFlorian Westphal 
16330d199e43SFlorian Westphal 			mptcp_update_post_push(msk, dfrag, ret);
16346e628cd3SPaolo Abeni 		}
16356e628cd3SPaolo Abeni 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
16366e628cd3SPaolo Abeni 	}
16376e628cd3SPaolo Abeni 
16386e628cd3SPaolo Abeni out:
16396e628cd3SPaolo Abeni 	/* __mptcp_alloc_tx_skb could have released some wmem and we are
16406e628cd3SPaolo Abeni 	 * not going to flush it via release_sock()
16416e628cd3SPaolo Abeni 	 */
16426e628cd3SPaolo Abeni 	if (copied) {
16436e628cd3SPaolo Abeni 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
16446e628cd3SPaolo Abeni 			 info.size_goal);
1645d09d818eSPaolo Abeni 		if (!mptcp_timer_pending(sk))
1646d09d818eSPaolo Abeni 			mptcp_reset_timer(sk);
1647d09d818eSPaolo Abeni 
16486e628cd3SPaolo Abeni 		if (msk->snd_data_fin_enable &&
16496e628cd3SPaolo Abeni 		    msk->snd_nxt + 1 == msk->write_seq)
16506e628cd3SPaolo Abeni 			mptcp_schedule_work(sk);
16516e628cd3SPaolo Abeni 	}
16526e628cd3SPaolo Abeni }
16536e628cd3SPaolo Abeni 
16545cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk)
16555cf92bbaSPaolo Abeni {
16565cf92bbaSPaolo Abeni 	/* enable autotune */
16575cf92bbaSPaolo Abeni 	set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
16585cf92bbaSPaolo Abeni 
16595cf92bbaSPaolo Abeni 	/* will be cleared on avail space */
16605cf92bbaSPaolo Abeni 	set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
16615cf92bbaSPaolo Abeni }
16625cf92bbaSPaolo Abeni 
16637d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags);
16647d803344SPaolo Abeni 
1665*a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1666fa9e5746SPaolo Abeni 				  size_t len, int *copied_syn)
1667fa9e5746SPaolo Abeni {
1668fa9e5746SPaolo Abeni 	unsigned int saved_flags = msg->msg_flags;
1669fa9e5746SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1670*a2702a07SPaolo Abeni 	struct sock *ssk;
1671fa9e5746SPaolo Abeni 	int ret;
1672fa9e5746SPaolo Abeni 
1673*a2702a07SPaolo Abeni 	/* on flags based fastopen the mptcp is supposed to create the
1674*a2702a07SPaolo Abeni 	 * first subflow right now. Otherwise we are in the defer_connect
1675*a2702a07SPaolo Abeni 	 * path, and the first subflow must be already present.
1676*a2702a07SPaolo Abeni 	 * Since the defer_connect flag is cleared after the first succsful
1677*a2702a07SPaolo Abeni 	 * fastopen attempt, no need to check for additional subflow status.
1678*a2702a07SPaolo Abeni 	 */
1679*a2702a07SPaolo Abeni 	if (msg->msg_flags & MSG_FASTOPEN && !__mptcp_nmpc_socket(msk))
1680*a2702a07SPaolo Abeni 		return -EINVAL;
1681*a2702a07SPaolo Abeni 	if (!msk->first)
1682*a2702a07SPaolo Abeni 		return -EINVAL;
1683*a2702a07SPaolo Abeni 
1684*a2702a07SPaolo Abeni 	ssk = msk->first;
1685*a2702a07SPaolo Abeni 
1686fa9e5746SPaolo Abeni 	lock_sock(ssk);
1687fa9e5746SPaolo Abeni 	msg->msg_flags |= MSG_DONTWAIT;
1688fa9e5746SPaolo Abeni 	msk->connect_flags = O_NONBLOCK;
16897d803344SPaolo Abeni 	msk->fastopening = 1;
1690fa9e5746SPaolo Abeni 	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
16917d803344SPaolo Abeni 	msk->fastopening = 0;
1692fa9e5746SPaolo Abeni 	msg->msg_flags = saved_flags;
1693fa9e5746SPaolo Abeni 	release_sock(ssk);
1694fa9e5746SPaolo Abeni 
1695fa9e5746SPaolo Abeni 	/* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1696fa9e5746SPaolo Abeni 	if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1697fa9e5746SPaolo Abeni 		ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1698fa9e5746SPaolo Abeni 					    msg->msg_namelen, msg->msg_flags, 1);
1699fa9e5746SPaolo Abeni 
1700fa9e5746SPaolo Abeni 		/* Keep the same behaviour of plain TCP: zero the copied bytes in
1701fa9e5746SPaolo Abeni 		 * case of any error, except timeout or signal
1702fa9e5746SPaolo Abeni 		 */
1703fa9e5746SPaolo Abeni 		if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1704fa9e5746SPaolo Abeni 			*copied_syn = 0;
17057d803344SPaolo Abeni 	} else if (ret && ret != -EINPROGRESS) {
17067d803344SPaolo Abeni 		mptcp_disconnect(sk, 0);
1707fa9e5746SPaolo Abeni 	}
1708*a2702a07SPaolo Abeni 	inet_sk(sk)->defer_connect = 0;
1709fa9e5746SPaolo Abeni 
1710fa9e5746SPaolo Abeni 	return ret;
1711fa9e5746SPaolo Abeni }
1712fa9e5746SPaolo Abeni 
1713f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1714f870fa0bSMat Martineau {
1715f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
171617091708SFlorian Westphal 	struct page_frag *pfrag;
17176d0060f6SMat Martineau 	size_t copied = 0;
1718caf971dfSPaolo Abeni 	int ret = 0;
17196d0060f6SMat Martineau 	long timeo;
1720f870fa0bSMat Martineau 
1721987858e5SPaolo Abeni 	/* silently ignore everything else */
17221e777f39SDmytro Shytyi 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1723987858e5SPaolo Abeni 
17246511882cSPaolo Abeni 	lock_sock(sk);
17251954b860SMat Martineau 
1726*a2702a07SPaolo Abeni 	if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) {
1727d98a82a6SDmytro Shytyi 		int copied_syn = 0;
1728d98a82a6SDmytro Shytyi 
1729*a2702a07SPaolo Abeni 		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1730d98a82a6SDmytro Shytyi 		copied += copied_syn;
1731fa9e5746SPaolo Abeni 		if (ret == -EINPROGRESS && copied_syn > 0)
1732d98a82a6SDmytro Shytyi 			goto out;
1733fa9e5746SPaolo Abeni 		else if (ret)
173469800e51SPaolo Abeni 			goto do_error;
1735d98a82a6SDmytro Shytyi 	}
1736d98a82a6SDmytro Shytyi 
17371954b860SMat Martineau 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
17381954b860SMat Martineau 
17391954b860SMat Martineau 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
17401954b860SMat Martineau 		ret = sk_stream_wait_connect(sk, &timeo);
17411954b860SMat Martineau 		if (ret)
174269800e51SPaolo Abeni 			goto do_error;
17431954b860SMat Martineau 	}
17441954b860SMat Martineau 
174569800e51SPaolo Abeni 	ret = -EPIPE;
174669800e51SPaolo Abeni 	if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
174769800e51SPaolo Abeni 		goto do_error;
174869800e51SPaolo Abeni 
174917091708SFlorian Westphal 	pfrag = sk_page_frag(sk);
175018b683bfSPaolo Abeni 
1751d9ca1de8SPaolo Abeni 	while (msg_data_left(msg)) {
1752724cfd2eSPaolo Abeni 		int total_ts, frag_truesize = 0;
1753d9ca1de8SPaolo Abeni 		struct mptcp_data_frag *dfrag;
1754d9ca1de8SPaolo Abeni 		bool dfrag_collapsed;
1755d9ca1de8SPaolo Abeni 		size_t psize, offset;
1756d9ca1de8SPaolo Abeni 
1757d9ca1de8SPaolo Abeni 		/* reuse tail pfrag, if possible, or carve a new one from the
1758d9ca1de8SPaolo Abeni 		 * page allocator
1759fb529e62SFlorian Westphal 		 */
1760d9ca1de8SPaolo Abeni 		dfrag = mptcp_pending_tail(sk);
1761d9ca1de8SPaolo Abeni 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1762d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1763d9ca1de8SPaolo Abeni 			if (!sk_stream_memory_free(sk))
1764d9ca1de8SPaolo Abeni 				goto wait_for_memory;
17656e628cd3SPaolo Abeni 
1766d9ca1de8SPaolo Abeni 			if (!mptcp_page_frag_refill(sk, pfrag))
1767d9ca1de8SPaolo Abeni 				goto wait_for_memory;
1768d9ca1de8SPaolo Abeni 
1769d9ca1de8SPaolo Abeni 			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1770d9ca1de8SPaolo Abeni 			frag_truesize = dfrag->overhead;
1771fb529e62SFlorian Westphal 		}
1772fb529e62SFlorian Westphal 
1773d9ca1de8SPaolo Abeni 		/* we do not bound vs wspace, to allow a single packet.
1774d9ca1de8SPaolo Abeni 		 * memory accounting will prevent execessive memory usage
1775d9ca1de8SPaolo Abeni 		 * anyway
1776d9ca1de8SPaolo Abeni 		 */
1777d9ca1de8SPaolo Abeni 		offset = dfrag->offset + dfrag->data_len;
1778d9ca1de8SPaolo Abeni 		psize = pfrag->size - offset;
1779d9ca1de8SPaolo Abeni 		psize = min_t(size_t, psize, msg_data_left(msg));
1780724cfd2eSPaolo Abeni 		total_ts = psize + frag_truesize;
17818ce568edSPaolo Abeni 
17826511882cSPaolo Abeni 		if (!sk_wmem_schedule(sk, total_ts))
1783d9ca1de8SPaolo Abeni 			goto wait_for_memory;
1784d9ca1de8SPaolo Abeni 
1785d9ca1de8SPaolo Abeni 		if (copy_page_from_iter(dfrag->page, offset, psize,
1786d9ca1de8SPaolo Abeni 					&msg->msg_iter) != psize) {
1787d9ca1de8SPaolo Abeni 			ret = -EFAULT;
178869800e51SPaolo Abeni 			goto do_error;
1789d9ca1de8SPaolo Abeni 		}
1790d9ca1de8SPaolo Abeni 
1791d9ca1de8SPaolo Abeni 		/* data successfully copied into the write queue */
17926511882cSPaolo Abeni 		sk->sk_forward_alloc -= total_ts;
1793d9ca1de8SPaolo Abeni 		copied += psize;
1794d9ca1de8SPaolo Abeni 		dfrag->data_len += psize;
1795d9ca1de8SPaolo Abeni 		frag_truesize += psize;
1796d9ca1de8SPaolo Abeni 		pfrag->offset += frag_truesize;
1797d9ca1de8SPaolo Abeni 		WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1798d9ca1de8SPaolo Abeni 
1799d9ca1de8SPaolo Abeni 		/* charge data on mptcp pending queue to the msk socket
1800d9ca1de8SPaolo Abeni 		 * Note: we charge such data both to sk and ssk
1801d9ca1de8SPaolo Abeni 		 */
1802d9ca1de8SPaolo Abeni 		sk_wmem_queued_add(sk, frag_truesize);
1803d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1804d9ca1de8SPaolo Abeni 			get_page(dfrag->page);
1805d9ca1de8SPaolo Abeni 			list_add_tail(&dfrag->list, &msk->rtx_queue);
1806d9ca1de8SPaolo Abeni 			if (!msk->first_pending)
1807d9ca1de8SPaolo Abeni 				WRITE_ONCE(msk->first_pending, dfrag);
1808d9ca1de8SPaolo Abeni 		}
1809e4b61351SGeliang Tang 		pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1810d9ca1de8SPaolo Abeni 			 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1811d9ca1de8SPaolo Abeni 			 !dfrag_collapsed);
1812d9ca1de8SPaolo Abeni 
1813d9ca1de8SPaolo Abeni 		continue;
1814d9ca1de8SPaolo Abeni 
1815d9ca1de8SPaolo Abeni wait_for_memory:
18165cf92bbaSPaolo Abeni 		mptcp_set_nospace(sk);
1817c2e6048fSPaolo Abeni 		__mptcp_push_pending(sk, msg->msg_flags);
1818f296234cSPeter Krystad 		ret = sk_stream_wait_memory(sk, &timeo);
1819f296234cSPeter Krystad 		if (ret)
182069800e51SPaolo Abeni 			goto do_error;
1821cec37a6eSPeter Krystad 	}
1822cec37a6eSPeter Krystad 
182313e16037SPaolo Abeni 	if (copied)
1824c2e6048fSPaolo Abeni 		__mptcp_push_pending(sk, msg->msg_flags);
1825da51aef5SPaolo Abeni 
18261954b860SMat Martineau out:
1827cec37a6eSPeter Krystad 	release_sock(sk);
182869800e51SPaolo Abeni 	return copied;
182969800e51SPaolo Abeni 
183069800e51SPaolo Abeni do_error:
183169800e51SPaolo Abeni 	if (copied)
183269800e51SPaolo Abeni 		goto out;
183369800e51SPaolo Abeni 
183469800e51SPaolo Abeni 	copied = sk_stream_error(sk, msg->msg_flags, ret);
183569800e51SPaolo Abeni 	goto out;
1836f870fa0bSMat Martineau }
1837f870fa0bSMat Martineau 
18386771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
18396771bfd9SFlorian Westphal 				struct msghdr *msg,
1840b7f653b2SFlorian Westphal 				size_t len, int flags,
1841b7f653b2SFlorian Westphal 				struct scm_timestamping_internal *tss,
1842b7f653b2SFlorian Westphal 				int *cmsg_flags)
18436771bfd9SFlorian Westphal {
1844ca4fb892SYonglong Li 	struct sk_buff *skb, *tmp;
18456771bfd9SFlorian Westphal 	int copied = 0;
18466771bfd9SFlorian Westphal 
1847ca4fb892SYonglong Li 	skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
18486771bfd9SFlorian Westphal 		u32 offset = MPTCP_SKB_CB(skb)->offset;
18496771bfd9SFlorian Westphal 		u32 data_len = skb->len - offset;
18506771bfd9SFlorian Westphal 		u32 count = min_t(size_t, len - copied, data_len);
18516771bfd9SFlorian Westphal 		int err;
18526771bfd9SFlorian Westphal 
1853d976092cSPaolo Abeni 		if (!(flags & MSG_TRUNC)) {
18546771bfd9SFlorian Westphal 			err = skb_copy_datagram_msg(skb, offset, msg, count);
18556771bfd9SFlorian Westphal 			if (unlikely(err < 0)) {
18566771bfd9SFlorian Westphal 				if (!copied)
18576771bfd9SFlorian Westphal 					return err;
18586771bfd9SFlorian Westphal 				break;
18596771bfd9SFlorian Westphal 			}
1860d976092cSPaolo Abeni 		}
18616771bfd9SFlorian Westphal 
1862b7f653b2SFlorian Westphal 		if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1863b7f653b2SFlorian Westphal 			tcp_update_recv_tstamps(skb, tss);
1864b7f653b2SFlorian Westphal 			*cmsg_flags |= MPTCP_CMSG_TS;
1865b7f653b2SFlorian Westphal 		}
1866b7f653b2SFlorian Westphal 
18676771bfd9SFlorian Westphal 		copied += count;
18686771bfd9SFlorian Westphal 
18696771bfd9SFlorian Westphal 		if (count < data_len) {
18702c9e7765SFlorian Westphal 			if (!(flags & MSG_PEEK)) {
18716771bfd9SFlorian Westphal 				MPTCP_SKB_CB(skb)->offset += count;
18722c9e7765SFlorian Westphal 				MPTCP_SKB_CB(skb)->map_seq += count;
18732c9e7765SFlorian Westphal 			}
18746771bfd9SFlorian Westphal 			break;
18756771bfd9SFlorian Westphal 		}
18766771bfd9SFlorian Westphal 
1877ca4fb892SYonglong Li 		if (!(flags & MSG_PEEK)) {
187887952603SPaolo Abeni 			/* we will bulk release the skb memory later */
187987952603SPaolo Abeni 			skb->destructor = NULL;
1880ce599c51SPaolo Abeni 			WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
188187952603SPaolo Abeni 			__skb_unlink(skb, &msk->receive_queue);
18826771bfd9SFlorian Westphal 			__kfree_skb(skb);
1883ca4fb892SYonglong Li 		}
18846771bfd9SFlorian Westphal 
18856771bfd9SFlorian Westphal 		if (copied >= len)
18866771bfd9SFlorian Westphal 			break;
18876771bfd9SFlorian Westphal 	}
18886771bfd9SFlorian Westphal 
18896771bfd9SFlorian Westphal 	return copied;
18906771bfd9SFlorian Westphal }
18916771bfd9SFlorian Westphal 
1892a6b118feSFlorian Westphal /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
1893a6b118feSFlorian Westphal  *
1894a6b118feSFlorian Westphal  * Only difference: Use highest rtt estimate of the subflows in use.
1895a6b118feSFlorian Westphal  */
1896a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1897a6b118feSFlorian Westphal {
1898a6b118feSFlorian Westphal 	struct mptcp_subflow_context *subflow;
1899a6b118feSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
1900a6b118feSFlorian Westphal 	u32 time, advmss = 1;
1901a6b118feSFlorian Westphal 	u64 rtt_us, mstamp;
1902a6b118feSFlorian Westphal 
1903109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
1904a6b118feSFlorian Westphal 
1905a6b118feSFlorian Westphal 	if (copied <= 0)
1906a6b118feSFlorian Westphal 		return;
1907a6b118feSFlorian Westphal 
1908a6b118feSFlorian Westphal 	msk->rcvq_space.copied += copied;
1909a6b118feSFlorian Westphal 
1910a6b118feSFlorian Westphal 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1911a6b118feSFlorian Westphal 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1912a6b118feSFlorian Westphal 
1913a6b118feSFlorian Westphal 	rtt_us = msk->rcvq_space.rtt_us;
1914a6b118feSFlorian Westphal 	if (rtt_us && time < (rtt_us >> 3))
1915a6b118feSFlorian Westphal 		return;
1916a6b118feSFlorian Westphal 
1917a6b118feSFlorian Westphal 	rtt_us = 0;
1918a6b118feSFlorian Westphal 	mptcp_for_each_subflow(msk, subflow) {
1919a6b118feSFlorian Westphal 		const struct tcp_sock *tp;
1920a6b118feSFlorian Westphal 		u64 sf_rtt_us;
1921a6b118feSFlorian Westphal 		u32 sf_advmss;
1922a6b118feSFlorian Westphal 
1923a6b118feSFlorian Westphal 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1924a6b118feSFlorian Westphal 
1925a6b118feSFlorian Westphal 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1926a6b118feSFlorian Westphal 		sf_advmss = READ_ONCE(tp->advmss);
1927a6b118feSFlorian Westphal 
1928a6b118feSFlorian Westphal 		rtt_us = max(sf_rtt_us, rtt_us);
1929a6b118feSFlorian Westphal 		advmss = max(sf_advmss, advmss);
1930a6b118feSFlorian Westphal 	}
1931a6b118feSFlorian Westphal 
1932a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = rtt_us;
1933a6b118feSFlorian Westphal 	if (time < (rtt_us >> 3) || rtt_us == 0)
1934a6b118feSFlorian Westphal 		return;
1935a6b118feSFlorian Westphal 
1936a6b118feSFlorian Westphal 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1937a6b118feSFlorian Westphal 		goto new_measure;
1938a6b118feSFlorian Westphal 
193978047648SKuniyuki Iwashima 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
1940a6b118feSFlorian Westphal 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1941a6b118feSFlorian Westphal 		int rcvmem, rcvbuf;
1942a6b118feSFlorian Westphal 		u64 rcvwin, grow;
1943a6b118feSFlorian Westphal 
1944a6b118feSFlorian Westphal 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1945a6b118feSFlorian Westphal 
1946a6b118feSFlorian Westphal 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1947a6b118feSFlorian Westphal 
1948a6b118feSFlorian Westphal 		do_div(grow, msk->rcvq_space.space);
1949a6b118feSFlorian Westphal 		rcvwin += (grow << 1);
1950a6b118feSFlorian Westphal 
1951a6b118feSFlorian Westphal 		rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1952a6b118feSFlorian Westphal 		while (tcp_win_from_space(sk, rcvmem) < advmss)
1953a6b118feSFlorian Westphal 			rcvmem += 128;
1954a6b118feSFlorian Westphal 
1955a6b118feSFlorian Westphal 		do_div(rcvwin, advmss);
1956a6b118feSFlorian Westphal 		rcvbuf = min_t(u64, rcvwin * rcvmem,
195702739545SKuniyuki Iwashima 			       READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
1958a6b118feSFlorian Westphal 
1959a6b118feSFlorian Westphal 		if (rcvbuf > sk->sk_rcvbuf) {
1960a6b118feSFlorian Westphal 			u32 window_clamp;
1961a6b118feSFlorian Westphal 
1962a6b118feSFlorian Westphal 			window_clamp = tcp_win_from_space(sk, rcvbuf);
1963a6b118feSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1964a6b118feSFlorian Westphal 
1965a6b118feSFlorian Westphal 			/* Make subflows follow along.  If we do not do this, we
1966a6b118feSFlorian Westphal 			 * get drops at subflow level if skbs can't be moved to
1967a6b118feSFlorian Westphal 			 * the mptcp rx queue fast enough (announced rcv_win can
1968a6b118feSFlorian Westphal 			 * exceed ssk->sk_rcvbuf).
1969a6b118feSFlorian Westphal 			 */
1970a6b118feSFlorian Westphal 			mptcp_for_each_subflow(msk, subflow) {
1971a6b118feSFlorian Westphal 				struct sock *ssk;
1972c76c6956SPaolo Abeni 				bool slow;
1973a6b118feSFlorian Westphal 
1974a6b118feSFlorian Westphal 				ssk = mptcp_subflow_tcp_sock(subflow);
1975c76c6956SPaolo Abeni 				slow = lock_sock_fast(ssk);
1976a6b118feSFlorian Westphal 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1977a6b118feSFlorian Westphal 				tcp_sk(ssk)->window_clamp = window_clamp;
1978c76c6956SPaolo Abeni 				tcp_cleanup_rbuf(ssk, 1);
1979c76c6956SPaolo Abeni 				unlock_sock_fast(ssk, slow);
1980a6b118feSFlorian Westphal 			}
1981a6b118feSFlorian Westphal 		}
1982a6b118feSFlorian Westphal 	}
1983a6b118feSFlorian Westphal 
1984a6b118feSFlorian Westphal 	msk->rcvq_space.space = msk->rcvq_space.copied;
1985a6b118feSFlorian Westphal new_measure:
1986a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
1987a6b118feSFlorian Westphal 	msk->rcvq_space.time = mstamp;
1988a6b118feSFlorian Westphal }
1989a6b118feSFlorian Westphal 
199087952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk)
199187952603SPaolo Abeni {
199287952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
199387952603SPaolo Abeni 
199487952603SPaolo Abeni 	if (!msk->rmem_released)
199587952603SPaolo Abeni 		return;
199687952603SPaolo Abeni 
199787952603SPaolo Abeni 	atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
19986511882cSPaolo Abeni 	mptcp_rmem_uncharge(sk, msk->rmem_released);
1999ce599c51SPaolo Abeni 	WRITE_ONCE(msk->rmem_released, 0);
200087952603SPaolo Abeni }
200187952603SPaolo Abeni 
200287952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk)
200387952603SPaolo Abeni {
200487952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
200587952603SPaolo Abeni 
200687952603SPaolo Abeni 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
200787952603SPaolo Abeni }
200887952603SPaolo Abeni 
2009e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk)
20106771bfd9SFlorian Westphal {
201187952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
20126771bfd9SFlorian Westphal 	unsigned int moved = 0;
201387952603SPaolo Abeni 	bool ret, done;
2014d5f49190SPaolo Abeni 
20156771bfd9SFlorian Westphal 	do {
20166771bfd9SFlorian Westphal 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
201765f49fe7SFlorian Westphal 		bool slowpath;
20186771bfd9SFlorian Westphal 
201987952603SPaolo Abeni 		/* we can have data pending in the subflows only if the msk
202087952603SPaolo Abeni 		 * receive buffer was full at subflow_data_ready() time,
202187952603SPaolo Abeni 		 * that is an unlikely slow path.
202287952603SPaolo Abeni 		 */
202387952603SPaolo Abeni 		if (likely(!ssk))
20246771bfd9SFlorian Westphal 			break;
20256771bfd9SFlorian Westphal 
202665f49fe7SFlorian Westphal 		slowpath = lock_sock_fast(ssk);
202787952603SPaolo Abeni 		mptcp_data_lock(sk);
2028e3859603SPaolo Abeni 		__mptcp_update_rmem(sk);
20296771bfd9SFlorian Westphal 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
203087952603SPaolo Abeni 		mptcp_data_unlock(sk);
2031499ada50SPaolo Abeni 
2032499ada50SPaolo Abeni 		if (unlikely(ssk->sk_err))
2033499ada50SPaolo Abeni 			__mptcp_error_report(sk);
203465f49fe7SFlorian Westphal 		unlock_sock_fast(ssk, slowpath);
20356771bfd9SFlorian Westphal 	} while (!done);
20366771bfd9SFlorian Westphal 
203787952603SPaolo Abeni 	/* acquire the data lock only if some input data is pending */
203887952603SPaolo Abeni 	ret = moved > 0;
203987952603SPaolo Abeni 	if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
204087952603SPaolo Abeni 	    !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
204187952603SPaolo Abeni 		mptcp_data_lock(sk);
204287952603SPaolo Abeni 		__mptcp_update_rmem(sk);
204387952603SPaolo Abeni 		ret |= __mptcp_ofo_queue(msk);
204487952603SPaolo Abeni 		__mptcp_splice_receive_queue(sk);
204587952603SPaolo Abeni 		mptcp_data_unlock(sk);
2046ab174ad8SPaolo Abeni 	}
204787952603SPaolo Abeni 	if (ret)
204887952603SPaolo Abeni 		mptcp_check_data_fin((struct sock *)msk);
204987952603SPaolo Abeni 	return !skb_queue_empty(&msk->receive_queue);
20506771bfd9SFlorian Westphal }
20516771bfd9SFlorian Westphal 
20522c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk)
20532c9e7765SFlorian Westphal {
20542c9e7765SFlorian Westphal 	const struct mptcp_sock *msk = mptcp_sk(sk);
20552c9e7765SFlorian Westphal 	const struct sk_buff *skb;
20562c9e7765SFlorian Westphal 
20572c9e7765SFlorian Westphal 	skb = skb_peek(&msk->receive_queue);
20582c9e7765SFlorian Westphal 	if (skb) {
20592c9e7765SFlorian Westphal 		u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
20602c9e7765SFlorian Westphal 
20612c9e7765SFlorian Westphal 		if (hint_val >= INT_MAX)
20622c9e7765SFlorian Westphal 			return INT_MAX;
20632c9e7765SFlorian Westphal 
20642c9e7765SFlorian Westphal 		return (unsigned int)hint_val;
20652c9e7765SFlorian Westphal 	}
20662c9e7765SFlorian Westphal 
20672c9e7765SFlorian Westphal 	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
20682c9e7765SFlorian Westphal 		return 1;
20692c9e7765SFlorian Westphal 
20702c9e7765SFlorian Westphal 	return 0;
20712c9e7765SFlorian Westphal }
20722c9e7765SFlorian Westphal 
2073f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2074ec095263SOliver Hartkopp 			 int flags, int *addr_len)
2075f870fa0bSMat Martineau {
2076f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
2077b7f653b2SFlorian Westphal 	struct scm_timestamping_internal tss;
2078b7f653b2SFlorian Westphal 	int copied = 0, cmsg_flags = 0;
20797a6a6cbcSPaolo Abeni 	int target;
20807a6a6cbcSPaolo Abeni 	long timeo;
2081f870fa0bSMat Martineau 
2082cb9d80f4SPaolo Abeni 	/* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2083cb9d80f4SPaolo Abeni 	if (unlikely(flags & MSG_ERRQUEUE))
2084cb9d80f4SPaolo Abeni 		return inet_recv_error(sk, msg, len, addr_len);
2085cb9d80f4SPaolo Abeni 
20866511882cSPaolo Abeni 	lock_sock(sk);
2087fd897679SPaolo Abeni 	if (unlikely(sk->sk_state == TCP_LISTEN)) {
2088fd897679SPaolo Abeni 		copied = -ENOTCONN;
2089fd897679SPaolo Abeni 		goto out_err;
2090fd897679SPaolo Abeni 	}
2091fd897679SPaolo Abeni 
2092ec095263SOliver Hartkopp 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
20937a6a6cbcSPaolo Abeni 
20947a6a6cbcSPaolo Abeni 	len = min_t(size_t, len, INT_MAX);
20957a6a6cbcSPaolo Abeni 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
20967a6a6cbcSPaolo Abeni 
20972c9e7765SFlorian Westphal 	if (unlikely(msk->recvmsg_inq))
20982c9e7765SFlorian Westphal 		cmsg_flags = MPTCP_CMSG_INQ;
20992c9e7765SFlorian Westphal 
210005e3eceaSEric Dumazet 	while (copied < len) {
2101e3859603SPaolo Abeni 		int bytes_read;
21027a6a6cbcSPaolo Abeni 
2103b7f653b2SFlorian Westphal 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
21046771bfd9SFlorian Westphal 		if (unlikely(bytes_read < 0)) {
21057a6a6cbcSPaolo Abeni 			if (!copied)
21067a6a6cbcSPaolo Abeni 				copied = bytes_read;
21076771bfd9SFlorian Westphal 			goto out_err;
2108cec37a6eSPeter Krystad 		}
2109cec37a6eSPeter Krystad 
21107a6a6cbcSPaolo Abeni 		copied += bytes_read;
21117a6a6cbcSPaolo Abeni 
2112ea4ca586SPaolo Abeni 		/* be sure to advertise window change */
2113fd897679SPaolo Abeni 		mptcp_cleanup_rbuf(msk);
2114ea4ca586SPaolo Abeni 
2115e3859603SPaolo Abeni 		if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2116e3859603SPaolo Abeni 			continue;
2117e3859603SPaolo Abeni 
21187a6a6cbcSPaolo Abeni 		/* only the master socket status is relevant here. The exit
21197a6a6cbcSPaolo Abeni 		 * conditions mirror closely tcp_recvmsg()
21207a6a6cbcSPaolo Abeni 		 */
21217a6a6cbcSPaolo Abeni 		if (copied >= target)
21227a6a6cbcSPaolo Abeni 			break;
21237a6a6cbcSPaolo Abeni 
21247a6a6cbcSPaolo Abeni 		if (copied) {
21257a6a6cbcSPaolo Abeni 			if (sk->sk_err ||
21267a6a6cbcSPaolo Abeni 			    sk->sk_state == TCP_CLOSE ||
21277a6a6cbcSPaolo Abeni 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
21287a6a6cbcSPaolo Abeni 			    !timeo ||
21297a6a6cbcSPaolo Abeni 			    signal_pending(current))
21307a6a6cbcSPaolo Abeni 				break;
21317a6a6cbcSPaolo Abeni 		} else {
21327a6a6cbcSPaolo Abeni 			if (sk->sk_err) {
21337a6a6cbcSPaolo Abeni 				copied = sock_error(sk);
21347a6a6cbcSPaolo Abeni 				break;
21357a6a6cbcSPaolo Abeni 			}
21367a6a6cbcSPaolo Abeni 
21375969856aSPaolo Abeni 			if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
21385969856aSPaolo Abeni 				mptcp_check_for_eof(msk);
21395969856aSPaolo Abeni 
214087952603SPaolo Abeni 			if (sk->sk_shutdown & RCV_SHUTDOWN) {
214187952603SPaolo Abeni 				/* race breaker: the shutdown could be after the
214287952603SPaolo Abeni 				 * previous receive queue check
214387952603SPaolo Abeni 				 */
2144e3859603SPaolo Abeni 				if (__mptcp_move_skbs(msk))
214587952603SPaolo Abeni 					continue;
21467a6a6cbcSPaolo Abeni 				break;
214787952603SPaolo Abeni 			}
21487a6a6cbcSPaolo Abeni 
21497a6a6cbcSPaolo Abeni 			if (sk->sk_state == TCP_CLOSE) {
21507a6a6cbcSPaolo Abeni 				copied = -ENOTCONN;
21517a6a6cbcSPaolo Abeni 				break;
21527a6a6cbcSPaolo Abeni 			}
21537a6a6cbcSPaolo Abeni 
21547a6a6cbcSPaolo Abeni 			if (!timeo) {
21557a6a6cbcSPaolo Abeni 				copied = -EAGAIN;
21567a6a6cbcSPaolo Abeni 				break;
21577a6a6cbcSPaolo Abeni 			}
21587a6a6cbcSPaolo Abeni 
21597a6a6cbcSPaolo Abeni 			if (signal_pending(current)) {
21607a6a6cbcSPaolo Abeni 				copied = sock_intr_errno(timeo);
21617a6a6cbcSPaolo Abeni 				break;
21627a6a6cbcSPaolo Abeni 			}
21637a6a6cbcSPaolo Abeni 		}
21647a6a6cbcSPaolo Abeni 
21657a6a6cbcSPaolo Abeni 		pr_debug("block timeout %ld", timeo);
2166612f71d7SPaolo Abeni 		sk_wait_data(sk, &timeo, NULL);
21677a6a6cbcSPaolo Abeni 	}
21683c90e377SPaolo Abeni 
21696771bfd9SFlorian Westphal out_err:
2170b7f653b2SFlorian Westphal 	if (cmsg_flags && copied >= 0) {
2171b7f653b2SFlorian Westphal 		if (cmsg_flags & MPTCP_CMSG_TS)
2172b7f653b2SFlorian Westphal 			tcp_recv_timestamp(msg, sk, &tss);
21732c9e7765SFlorian Westphal 
21742c9e7765SFlorian Westphal 		if (cmsg_flags & MPTCP_CMSG_INQ) {
21752c9e7765SFlorian Westphal 			unsigned int inq = mptcp_inq_hint(sk);
21762c9e7765SFlorian Westphal 
21772c9e7765SFlorian Westphal 			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
21782c9e7765SFlorian Westphal 		}
2179b7f653b2SFlorian Westphal 	}
2180b7f653b2SFlorian Westphal 
2181612f71d7SPaolo Abeni 	pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2182612f71d7SPaolo Abeni 		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2183612f71d7SPaolo Abeni 		 skb_queue_empty(&msk->receive_queue), copied);
2184ca4fb892SYonglong Li 	if (!(flags & MSG_PEEK))
2185a6b118feSFlorian Westphal 		mptcp_rcv_space_adjust(msk, copied);
2186a6b118feSFlorian Westphal 
2187cec37a6eSPeter Krystad 	release_sock(sk);
2188cec37a6eSPeter Krystad 	return copied;
2189cec37a6eSPeter Krystad }
2190cec37a6eSPeter Krystad 
2191b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t)
2192b51f9b80SPaolo Abeni {
2193b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = from_timer(icsk, t,
2194b51f9b80SPaolo Abeni 						       icsk_retransmit_timer);
2195b51f9b80SPaolo Abeni 	struct sock *sk = &icsk->icsk_inet.sk;
21962d6f5a2bSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2197b51f9b80SPaolo Abeni 
2198b51f9b80SPaolo Abeni 	bh_lock_sock(sk);
2199b51f9b80SPaolo Abeni 	if (!sock_owned_by_user(sk)) {
22002d6f5a2bSPaolo Abeni 		/* we need a process context to retransmit */
22012d6f5a2bSPaolo Abeni 		if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
22022d6f5a2bSPaolo Abeni 			mptcp_schedule_work(sk);
2203b51f9b80SPaolo Abeni 	} else {
2204b51f9b80SPaolo Abeni 		/* delegate our work to tcp_release_cb() */
2205e9d09bacSPaolo Abeni 		__set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2206b51f9b80SPaolo Abeni 	}
2207b51f9b80SPaolo Abeni 	bh_unlock_sock(sk);
2208b51f9b80SPaolo Abeni 	sock_put(sk);
2209b51f9b80SPaolo Abeni }
2210b51f9b80SPaolo Abeni 
2211e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t)
2212e16163b6SPaolo Abeni {
2213e16163b6SPaolo Abeni 	struct sock *sk = from_timer(sk, t, sk_timer);
2214e16163b6SPaolo Abeni 
2215e16163b6SPaolo Abeni 	mptcp_schedule_work(sk);
2216b6d69fc8SFlorian Westphal 	sock_put(sk);
2217e16163b6SPaolo Abeni }
2218e16163b6SPaolo Abeni 
22193b1d6210SPaolo Abeni /* Find an idle subflow.  Return NULL if there is unacked data at tcp
22203b1d6210SPaolo Abeni  * level.
22213b1d6210SPaolo Abeni  *
22223b1d6210SPaolo Abeni  * A backup subflow is returned only if that is the only kind available.
22233b1d6210SPaolo Abeni  */
2224ff5a0b42SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
22253b1d6210SPaolo Abeni {
222671b7dec2SPaolo Abeni 	struct sock *backup = NULL, *pick = NULL;
22273b1d6210SPaolo Abeni 	struct mptcp_subflow_context *subflow;
222871b7dec2SPaolo Abeni 	int min_stale_count = INT_MAX;
22293b1d6210SPaolo Abeni 
2230109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
22313b1d6210SPaolo Abeni 
2232d5f49190SPaolo Abeni 	if (__mptcp_check_fallback(msk))
2233d9ca1de8SPaolo Abeni 		return NULL;
2234d5f49190SPaolo Abeni 
22353b1d6210SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
22363b1d6210SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
22373b1d6210SPaolo Abeni 
2238ff5a0b42SPaolo Abeni 		if (!__mptcp_subflow_active(subflow))
2239d5f49190SPaolo Abeni 			continue;
2240d5f49190SPaolo Abeni 
224171b7dec2SPaolo Abeni 		/* still data outstanding at TCP level? skip this */
224271b7dec2SPaolo Abeni 		if (!tcp_rtx_and_write_queues_empty(ssk)) {
224371b7dec2SPaolo Abeni 			mptcp_pm_subflow_chk_stale(msk, ssk);
224471b7dec2SPaolo Abeni 			min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2245860975c6SFlorian Westphal 			continue;
2246860975c6SFlorian Westphal 		}
22473b1d6210SPaolo Abeni 
22483b1d6210SPaolo Abeni 		if (subflow->backup) {
22493b1d6210SPaolo Abeni 			if (!backup)
22503b1d6210SPaolo Abeni 				backup = ssk;
22513b1d6210SPaolo Abeni 			continue;
22523b1d6210SPaolo Abeni 		}
22533b1d6210SPaolo Abeni 
225471b7dec2SPaolo Abeni 		if (!pick)
225571b7dec2SPaolo Abeni 			pick = ssk;
22563b1d6210SPaolo Abeni 	}
22573b1d6210SPaolo Abeni 
225871b7dec2SPaolo Abeni 	if (pick)
225971b7dec2SPaolo Abeni 		return pick;
226071b7dec2SPaolo Abeni 
226171b7dec2SPaolo Abeni 	/* use backup only if there are no progresses anywhere */
226271b7dec2SPaolo Abeni 	return min_stale_count > 1 ? backup : NULL;
22633b1d6210SPaolo Abeni }
22643b1d6210SPaolo Abeni 
226517aee05dSFlorian Westphal static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
226617aee05dSFlorian Westphal {
226717aee05dSFlorian Westphal 	if (msk->subflow) {
226817aee05dSFlorian Westphal 		iput(SOCK_INODE(msk->subflow));
226917aee05dSFlorian Westphal 		msk->subflow = NULL;
227017aee05dSFlorian Westphal 	}
227117aee05dSFlorian Westphal }
227217aee05dSFlorian Westphal 
22731e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk)
22741e1d9d6fSPaolo Abeni {
22751e1d9d6fSPaolo Abeni 	struct mptcp_data_frag *cur, *rtx_head;
22761e1d9d6fSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
22771e1d9d6fSPaolo Abeni 
227800df24f1SGeliang Tang 	if (__mptcp_check_fallback(msk))
22791e1d9d6fSPaolo Abeni 		return false;
22801e1d9d6fSPaolo Abeni 
22811e1d9d6fSPaolo Abeni 	if (tcp_rtx_and_write_queues_empty(sk))
22821e1d9d6fSPaolo Abeni 		return false;
22831e1d9d6fSPaolo Abeni 
22841e1d9d6fSPaolo Abeni 	/* the closing socket has some data untransmitted and/or unacked:
22851e1d9d6fSPaolo Abeni 	 * some data in the mptcp rtx queue has not really xmitted yet.
22861e1d9d6fSPaolo Abeni 	 * keep it simple and re-inject the whole mptcp level rtx queue
22871e1d9d6fSPaolo Abeni 	 */
22881e1d9d6fSPaolo Abeni 	mptcp_data_lock(sk);
22891e1d9d6fSPaolo Abeni 	__mptcp_clean_una_wakeup(sk);
22901e1d9d6fSPaolo Abeni 	rtx_head = mptcp_rtx_head(sk);
22911e1d9d6fSPaolo Abeni 	if (!rtx_head) {
22921e1d9d6fSPaolo Abeni 		mptcp_data_unlock(sk);
22931e1d9d6fSPaolo Abeni 		return false;
22941e1d9d6fSPaolo Abeni 	}
22951e1d9d6fSPaolo Abeni 
22961e1d9d6fSPaolo Abeni 	msk->recovery_snd_nxt = msk->snd_nxt;
22971e1d9d6fSPaolo Abeni 	msk->recovery = true;
22981e1d9d6fSPaolo Abeni 	mptcp_data_unlock(sk);
22991e1d9d6fSPaolo Abeni 
23001e1d9d6fSPaolo Abeni 	msk->first_pending = rtx_head;
23011e1d9d6fSPaolo Abeni 	msk->snd_burst = 0;
23021e1d9d6fSPaolo Abeni 
23031e1d9d6fSPaolo Abeni 	/* be sure to clear the "sent status" on all re-injected fragments */
23041e1d9d6fSPaolo Abeni 	list_for_each_entry(cur, &msk->rtx_queue, list) {
23051e1d9d6fSPaolo Abeni 		if (!cur->already_sent)
23061e1d9d6fSPaolo Abeni 			break;
23071e1d9d6fSPaolo Abeni 		cur->already_sent = 0;
23081e1d9d6fSPaolo Abeni 	}
23091e1d9d6fSPaolo Abeni 
23101e1d9d6fSPaolo Abeni 	return true;
23111e1d9d6fSPaolo Abeni }
23121e1d9d6fSPaolo Abeni 
2313b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */
2314b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH		BIT(1)
2315b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE	BIT(2)
2316b29fcfb5SPaolo Abeni 
2317cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming
2318cec37a6eSPeter Krystad  * (accept).
2319cec37a6eSPeter Krystad  *
2320cec37a6eSPeter Krystad  * Outgoing subflows use in-kernel sockets.
2321cec37a6eSPeter Krystad  * Incoming subflows do not have their own 'struct socket' allocated,
2322cec37a6eSPeter Krystad  * so we need to use tcp_close() after detaching them from the mptcp
2323cec37a6eSPeter Krystad  * parent socket.
2324cec37a6eSPeter Krystad  */
2325a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2326b29fcfb5SPaolo Abeni 			      struct mptcp_subflow_context *subflow,
2327b29fcfb5SPaolo Abeni 			      unsigned int flags)
2328cec37a6eSPeter Krystad {
2329e0be4931SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
2330b29fcfb5SPaolo Abeni 	bool need_push, dispose_it;
2331e0be4931SFlorian Westphal 
2332b29fcfb5SPaolo Abeni 	dispose_it = !msk->subflow || ssk != msk->subflow->sk;
2333b29fcfb5SPaolo Abeni 	if (dispose_it)
2334cec37a6eSPeter Krystad 		list_del(&subflow->node);
2335cec37a6eSPeter Krystad 
23363f8b2667SPaolo Abeni 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2337e16163b6SPaolo Abeni 
2338d21f8348SPaolo Abeni 	if (flags & MPTCP_CF_FASTCLOSE) {
2339d21f8348SPaolo Abeni 		/* be sure to force the tcp_disconnect() path,
2340d21f8348SPaolo Abeni 		 * to generate the egress reset
2341d21f8348SPaolo Abeni 		 */
2342d21f8348SPaolo Abeni 		ssk->sk_lingertime = 0;
2343d21f8348SPaolo Abeni 		sock_set_flag(ssk, SOCK_LINGER);
2344b29fcfb5SPaolo Abeni 		subflow->send_fastclose = 1;
2345d21f8348SPaolo Abeni 	}
2346b29fcfb5SPaolo Abeni 
2347b29fcfb5SPaolo Abeni 	need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2348b29fcfb5SPaolo Abeni 	if (!dispose_it) {
2349b29fcfb5SPaolo Abeni 		tcp_disconnect(ssk, 0);
2350b29fcfb5SPaolo Abeni 		msk->subflow->state = SS_UNCONNECTED;
2351b29fcfb5SPaolo Abeni 		mptcp_subflow_ctx_reset(subflow);
2352b29fcfb5SPaolo Abeni 		release_sock(ssk);
2353b29fcfb5SPaolo Abeni 
2354b29fcfb5SPaolo Abeni 		goto out;
2355b29fcfb5SPaolo Abeni 	}
2356b29fcfb5SPaolo Abeni 
2357d7b1bfd0SPaolo Abeni 	subflow->disposable = 1;
2358d7b1bfd0SPaolo Abeni 
2359e16163b6SPaolo Abeni 	/* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2360e16163b6SPaolo Abeni 	 * the ssk has been already destroyed, we just need to release the
2361e16163b6SPaolo Abeni 	 * reference owned by msk;
2362e16163b6SPaolo Abeni 	 */
2363e16163b6SPaolo Abeni 	if (!inet_csk(ssk)->icsk_ulp_ops) {
2364b6985b9bSPaolo Abeni 		WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2365e16163b6SPaolo Abeni 		kfree_rcu(subflow, rcu);
2366b6985b9bSPaolo Abeni 	} else if (msk->in_accept_queue && msk->first == ssk) {
2367b6985b9bSPaolo Abeni 		/* if the first subflow moved to a close state, e.g. due to
2368b6985b9bSPaolo Abeni 		 * incoming reset and we reach here before inet_child_forget()
2369b6985b9bSPaolo Abeni 		 * the TCP stack could later try to close it via
2370b6985b9bSPaolo Abeni 		 * inet_csk_listen_stop(), or deliver it to the user space via
2371b6985b9bSPaolo Abeni 		 * accept().
2372b6985b9bSPaolo Abeni 		 * We can't delete the subflow - or risk a double free - nor let
2373b6985b9bSPaolo Abeni 		 * the msk survive - or will be leaked in the non accept scenario:
2374b6985b9bSPaolo Abeni 		 * fallback and let TCP cope with the subflow cleanup.
2375b6985b9bSPaolo Abeni 		 */
2376b6985b9bSPaolo Abeni 		WARN_ON_ONCE(sock_flag(ssk, SOCK_DEAD));
2377b6985b9bSPaolo Abeni 		mptcp_subflow_drop_ctx(ssk);
2378e16163b6SPaolo Abeni 	} else {
2379d7b1bfd0SPaolo Abeni 		/* otherwise tcp will dispose of the ssk and subflow ctx */
23800a3f4f1fSPaolo Abeni 		if (ssk->sk_state == TCP_LISTEN)
2381f8c9dfbdSGeliang Tang 			mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
23820a3f4f1fSPaolo Abeni 
2383e16163b6SPaolo Abeni 		__tcp_close(ssk, 0);
2384e16163b6SPaolo Abeni 
2385e16163b6SPaolo Abeni 		/* close acquired an extra ref */
2386e16163b6SPaolo Abeni 		__sock_put(ssk);
2387e16163b6SPaolo Abeni 	}
2388e16163b6SPaolo Abeni 	release_sock(ssk);
2389e16163b6SPaolo Abeni 
2390e16163b6SPaolo Abeni 	sock_put(ssk);
2391e0be4931SFlorian Westphal 
2392c8fe62f0SFlorian Westphal 	if (ssk == msk->first)
2393c8fe62f0SFlorian Westphal 		msk->first = NULL;
2394c8fe62f0SFlorian Westphal 
2395b29fcfb5SPaolo Abeni out:
2396b29fcfb5SPaolo Abeni 	if (ssk == msk->last_snd)
2397b29fcfb5SPaolo Abeni 		msk->last_snd = NULL;
23981e1d9d6fSPaolo Abeni 
23991e1d9d6fSPaolo Abeni 	if (need_push)
24001e1d9d6fSPaolo Abeni 		__mptcp_push_pending(sk, 0);
2401f870fa0bSMat Martineau }
2402f870fa0bSMat Martineau 
2403a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2404a141e02eSFlorian Westphal 		     struct mptcp_subflow_context *subflow)
2405a141e02eSFlorian Westphal {
2406b911c97cSFlorian Westphal 	if (sk->sk_state == TCP_ESTABLISHED)
2407b911c97cSFlorian Westphal 		mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2408a88c9e49SPaolo Abeni 
2409a88c9e49SPaolo Abeni 	/* subflow aborted before reaching the fully_established status
2410a88c9e49SPaolo Abeni 	 * attempt the creation of the next subflow
2411a88c9e49SPaolo Abeni 	 */
2412a88c9e49SPaolo Abeni 	mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2413a88c9e49SPaolo Abeni 
2414b29fcfb5SPaolo Abeni 	__mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2415a141e02eSFlorian Westphal }
2416a141e02eSFlorian Westphal 
2417dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2418dc24f8b4SPaolo Abeni {
2419dc24f8b4SPaolo Abeni 	return 0;
2420dc24f8b4SPaolo Abeni }
2421dc24f8b4SPaolo Abeni 
2422b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk)
24230e4f35d7SPaolo Abeni {
24240e4f35d7SPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
2425b6985b9bSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
24260e4f35d7SPaolo Abeni 
24273abc05d9SFlorian Westphal 	might_sleep();
24283abc05d9SFlorian Westphal 
24295efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
24300e4f35d7SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
24310e4f35d7SPaolo Abeni 
24320e4f35d7SPaolo Abeni 		if (inet_sk_state_load(ssk) != TCP_CLOSE)
24330e4f35d7SPaolo Abeni 			continue;
24340e4f35d7SPaolo Abeni 
243540947e13SFlorian Westphal 		/* 'subflow_data_ready' will re-sched once rx queue is empty */
243640947e13SFlorian Westphal 		if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
243740947e13SFlorian Westphal 			continue;
243840947e13SFlorian Westphal 
2439b6985b9bSPaolo Abeni 		mptcp_close_ssk(sk, ssk, subflow);
2440b6985b9bSPaolo Abeni 	}
2441b6985b9bSPaolo Abeni 
2442b6985b9bSPaolo Abeni 	/* if the MPC subflow has been closed before the msk is accepted,
2443b6985b9bSPaolo Abeni 	 * msk will never be accept-ed, close it now
2444b6985b9bSPaolo Abeni 	 */
2445b6985b9bSPaolo Abeni 	if (!msk->first && msk->in_accept_queue) {
2446b6985b9bSPaolo Abeni 		sock_set_flag(sk, SOCK_DEAD);
2447b6985b9bSPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
24480e4f35d7SPaolo Abeni 	}
24490e4f35d7SPaolo Abeni }
24500e4f35d7SPaolo Abeni 
2451e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk)
2452e16163b6SPaolo Abeni {
2453e16163b6SPaolo Abeni 	s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2454e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2455e16163b6SPaolo Abeni 
2456e16163b6SPaolo Abeni 	if (delta >= TCP_TIMEWAIT_LEN)
2457e16163b6SPaolo Abeni 		return true;
2458e16163b6SPaolo Abeni 
2459e16163b6SPaolo Abeni 	/* if all subflows are in closed status don't bother with additional
2460e16163b6SPaolo Abeni 	 * timeout
2461e16163b6SPaolo Abeni 	 */
2462e16163b6SPaolo Abeni 	mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2463e16163b6SPaolo Abeni 		if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2464e16163b6SPaolo Abeni 		    TCP_CLOSE)
2465e16163b6SPaolo Abeni 			return false;
2466e16163b6SPaolo Abeni 	}
2467e16163b6SPaolo Abeni 	return true;
2468e16163b6SPaolo Abeni }
2469e16163b6SPaolo Abeni 
247050c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk)
247150c504a2SFlorian Westphal {
247250c504a2SFlorian Westphal 	struct mptcp_subflow_context *subflow, *tmp;
247380638684SGeliang Tang 	struct sock *sk = (struct sock *)msk;
247450c504a2SFlorian Westphal 
247550c504a2SFlorian Westphal 	if (likely(!READ_ONCE(msk->rcv_fastclose)))
247650c504a2SFlorian Westphal 		return;
247750c504a2SFlorian Westphal 
247850c504a2SFlorian Westphal 	mptcp_token_destroy(msk);
247950c504a2SFlorian Westphal 
24805efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
248150c504a2SFlorian Westphal 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
248275e908c3SPaolo Abeni 		bool slow;
248350c504a2SFlorian Westphal 
248475e908c3SPaolo Abeni 		slow = lock_sock_fast(tcp_sk);
248550c504a2SFlorian Westphal 		if (tcp_sk->sk_state != TCP_CLOSE) {
248650c504a2SFlorian Westphal 			tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
248750c504a2SFlorian Westphal 			tcp_set_state(tcp_sk, TCP_CLOSE);
248850c504a2SFlorian Westphal 		}
248975e908c3SPaolo Abeni 		unlock_sock_fast(tcp_sk, slow);
249050c504a2SFlorian Westphal 	}
249150c504a2SFlorian Westphal 
249269800e51SPaolo Abeni 	/* Mirror the tcp_reset() error propagation */
249369800e51SPaolo Abeni 	switch (sk->sk_state) {
249469800e51SPaolo Abeni 	case TCP_SYN_SENT:
24959ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, ECONNREFUSED);
249669800e51SPaolo Abeni 		break;
249769800e51SPaolo Abeni 	case TCP_CLOSE_WAIT:
24989ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, EPIPE);
249969800e51SPaolo Abeni 		break;
250069800e51SPaolo Abeni 	case TCP_CLOSE:
250169800e51SPaolo Abeni 		return;
250269800e51SPaolo Abeni 	default:
25039ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, ECONNRESET);
250469800e51SPaolo Abeni 	}
250569800e51SPaolo Abeni 
250650c504a2SFlorian Westphal 	inet_sk_state_store(sk, TCP_CLOSE);
250750c504a2SFlorian Westphal 	sk->sk_shutdown = SHUTDOWN_MASK;
250850c504a2SFlorian Westphal 	smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
250950c504a2SFlorian Westphal 	set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
251050c504a2SFlorian Westphal 
251169800e51SPaolo Abeni 	/* the calling mptcp_worker will properly destroy the socket */
251269800e51SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD))
251369800e51SPaolo Abeni 		return;
251469800e51SPaolo Abeni 
251569800e51SPaolo Abeni 	sk->sk_state_change(sk);
251669800e51SPaolo Abeni 	sk_error_report(sk);
251750c504a2SFlorian Westphal }
251850c504a2SFlorian Westphal 
25192948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk)
252080992017SPaolo Abeni {
25212948d0a1SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2522caf971dfSPaolo Abeni 	struct mptcp_sendmsg_info info = {};
25233b1d6210SPaolo Abeni 	struct mptcp_data_frag *dfrag;
25243b1d6210SPaolo Abeni 	size_t copied = 0;
25252948d0a1SPaolo Abeni 	struct sock *ssk;
25262948d0a1SPaolo Abeni 	int ret;
25272948d0a1SPaolo Abeni 
2528b5941f06SPaolo Abeni 	mptcp_clean_una_wakeup(sk);
25293241a9c0SFlorian Westphal 
25303241a9c0SFlorian Westphal 	/* first check ssk: need to kick "stale" logic */
25313241a9c0SFlorian Westphal 	ssk = mptcp_subflow_get_retrans(msk);
25322948d0a1SPaolo Abeni 	dfrag = mptcp_rtx_head(sk);
25336477dd39SMat Martineau 	if (!dfrag) {
25346477dd39SMat Martineau 		if (mptcp_data_fin_enabled(msk)) {
25356477dd39SMat Martineau 			struct inet_connection_sock *icsk = inet_csk(sk);
25366477dd39SMat Martineau 
25376477dd39SMat Martineau 			icsk->icsk_retransmits++;
25386477dd39SMat Martineau 			mptcp_set_datafin_timeout(sk);
25396477dd39SMat Martineau 			mptcp_send_ack(msk);
25406477dd39SMat Martineau 
25416477dd39SMat Martineau 			goto reset_timer;
25426477dd39SMat Martineau 		}
25436477dd39SMat Martineau 
25443241a9c0SFlorian Westphal 		if (!mptcp_send_head(sk))
25452948d0a1SPaolo Abeni 			return;
25463241a9c0SFlorian Westphal 
25473241a9c0SFlorian Westphal 		goto reset_timer;
25486477dd39SMat Martineau 	}
25492948d0a1SPaolo Abeni 
25502948d0a1SPaolo Abeni 	if (!ssk)
25512948d0a1SPaolo Abeni 		goto reset_timer;
25522948d0a1SPaolo Abeni 
25532948d0a1SPaolo Abeni 	lock_sock(ssk);
25542948d0a1SPaolo Abeni 
25552948d0a1SPaolo Abeni 	/* limit retransmission to the bytes already sent on some subflows */
25562948d0a1SPaolo Abeni 	info.sent = 0;
25574e14867dSPaolo Abeni 	info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
25584e14867dSPaolo Abeni 	while (info.sent < info.limit) {
25592948d0a1SPaolo Abeni 		ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
25602948d0a1SPaolo Abeni 		if (ret <= 0)
25612948d0a1SPaolo Abeni 			break;
25622948d0a1SPaolo Abeni 
25632948d0a1SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
25642948d0a1SPaolo Abeni 		copied += ret;
25652948d0a1SPaolo Abeni 		info.sent += ret;
25662948d0a1SPaolo Abeni 	}
25674e14867dSPaolo Abeni 	if (copied) {
25684e14867dSPaolo Abeni 		dfrag->already_sent = max(dfrag->already_sent, info.sent);
25692948d0a1SPaolo Abeni 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
25702948d0a1SPaolo Abeni 			 info.size_goal);
25710530020aSGeliang Tang 		WRITE_ONCE(msk->allow_infinite_fallback, false);
25724e14867dSPaolo Abeni 	}
25732948d0a1SPaolo Abeni 
25742948d0a1SPaolo Abeni 	release_sock(ssk);
25752948d0a1SPaolo Abeni 
25762948d0a1SPaolo Abeni reset_timer:
25773241a9c0SFlorian Westphal 	mptcp_check_and_set_pending(sk);
25783241a9c0SFlorian Westphal 
25792948d0a1SPaolo Abeni 	if (!mptcp_timer_pending(sk))
25802948d0a1SPaolo Abeni 		mptcp_reset_timer(sk);
25812948d0a1SPaolo Abeni }
25822948d0a1SPaolo Abeni 
258376a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout
258476a13b31SGeliang Tang  * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
258576a13b31SGeliang Tang  */
258676a13b31SGeliang Tang void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout)
258776a13b31SGeliang Tang {
258876a13b31SGeliang Tang 	struct sock *sk = (struct sock *)msk;
258976a13b31SGeliang Tang 	unsigned long timeout, close_timeout;
259076a13b31SGeliang Tang 
259176a13b31SGeliang Tang 	if (!fail_tout && !sock_flag(sk, SOCK_DEAD))
259276a13b31SGeliang Tang 		return;
259376a13b31SGeliang Tang 
259476a13b31SGeliang Tang 	close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
259576a13b31SGeliang Tang 
259676a13b31SGeliang Tang 	/* the close timeout takes precedence on the fail one, and here at least one of
259776a13b31SGeliang Tang 	 * them is active
259876a13b31SGeliang Tang 	 */
259976a13b31SGeliang Tang 	timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout;
260076a13b31SGeliang Tang 
260176a13b31SGeliang Tang 	sk_reset_timer(sk, &sk->sk_timer, timeout);
260276a13b31SGeliang Tang }
260376a13b31SGeliang Tang 
260449fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
260549fa1919SGeliang Tang {
260676a13b31SGeliang Tang 	struct sock *ssk = msk->first;
260749fa1919SGeliang Tang 	bool slow;
260849fa1919SGeliang Tang 
260976a13b31SGeliang Tang 	if (!ssk)
261076a13b31SGeliang Tang 		return;
261176a13b31SGeliang Tang 
261249fa1919SGeliang Tang 	pr_debug("MP_FAIL doesn't respond, reset the subflow");
261349fa1919SGeliang Tang 
261449fa1919SGeliang Tang 	slow = lock_sock_fast(ssk);
261549fa1919SGeliang Tang 	mptcp_subflow_reset(ssk);
261676a13b31SGeliang Tang 	WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
261749fa1919SGeliang Tang 	unlock_sock_fast(ssk, slow);
261876a13b31SGeliang Tang 
261976a13b31SGeliang Tang 	mptcp_reset_timeout(msk, 0);
262049fa1919SGeliang Tang }
262149fa1919SGeliang Tang 
2622d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk)
2623d21f8348SPaolo Abeni {
2624d21f8348SPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
2625d21f8348SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2626d21f8348SPaolo Abeni 
2627d21f8348SPaolo Abeni 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
2628d21f8348SPaolo Abeni 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2629d21f8348SPaolo Abeni 				  subflow, MPTCP_CF_FASTCLOSE);
2630d21f8348SPaolo Abeni }
2631d21f8348SPaolo Abeni 
26322948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work)
26332948d0a1SPaolo Abeni {
26342948d0a1SPaolo Abeni 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
263580638684SGeliang Tang 	struct sock *sk = (struct sock *)msk;
263676a13b31SGeliang Tang 	unsigned long fail_tout;
26372948d0a1SPaolo Abeni 	int state;
263880992017SPaolo Abeni 
263980992017SPaolo Abeni 	lock_sock(sk);
2640e16163b6SPaolo Abeni 	state = sk->sk_state;
2641d6a04437SPaolo Abeni 	if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2642e16163b6SPaolo Abeni 		goto unlock;
2643e16163b6SPaolo Abeni 
264443b54c6eSMat Martineau 	mptcp_check_data_fin_ack(sk);
264550c504a2SFlorian Westphal 
264650c504a2SFlorian Westphal 	mptcp_check_fastclose(msk);
264750c504a2SFlorian Westphal 
2648e9801430SFlorian Westphal 	mptcp_pm_nl_work(msk);
2649b416268bSFlorian Westphal 
265059832e24SFlorian Westphal 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
265159832e24SFlorian Westphal 		mptcp_check_for_eof(msk);
265259832e24SFlorian Westphal 
26536e628cd3SPaolo Abeni 	__mptcp_check_send_data_fin(sk);
265443b54c6eSMat Martineau 	mptcp_check_data_fin(sk);
265543b54c6eSMat Martineau 
2656b6985b9bSPaolo Abeni 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2657b6985b9bSPaolo Abeni 		__mptcp_close_subflow(sk);
2658b6985b9bSPaolo Abeni 
2659341c6524SPaolo Abeni 	/* There is no point in keeping around an orphaned sk timedout or
2660341c6524SPaolo Abeni 	 * closed, but we need the msk around to reply to incoming DATA_FIN,
2661341c6524SPaolo Abeni 	 * even if it is orphaned and in FIN_WAIT2 state
2662e16163b6SPaolo Abeni 	 */
2663d21f8348SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD)) {
2664d21f8348SPaolo Abeni 		if (mptcp_check_close_timeout(sk)) {
2665e16163b6SPaolo Abeni 			inet_sk_state_store(sk, TCP_CLOSE);
2666d21f8348SPaolo Abeni 			mptcp_do_fastclose(sk);
2667d21f8348SPaolo Abeni 		}
2668d21f8348SPaolo Abeni 		if (sk->sk_state == TCP_CLOSE) {
2669e16163b6SPaolo Abeni 			__mptcp_destroy_sock(sk);
2670e16163b6SPaolo Abeni 			goto unlock;
2671e16163b6SPaolo Abeni 		}
2672d21f8348SPaolo Abeni 	}
2673e16163b6SPaolo Abeni 
26742948d0a1SPaolo Abeni 	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
26752948d0a1SPaolo Abeni 		__mptcp_retrans(sk);
26763b1d6210SPaolo Abeni 
267776a13b31SGeliang Tang 	fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
267876a13b31SGeliang Tang 	if (fail_tout && time_after(jiffies, fail_tout))
267949fa1919SGeliang Tang 		mptcp_mp_fail_no_response(msk);
268049fa1919SGeliang Tang 
26813b1d6210SPaolo Abeni unlock:
268280992017SPaolo Abeni 	release_sock(sk);
268380992017SPaolo Abeni 	sock_put(sk);
268480992017SPaolo Abeni }
268580992017SPaolo Abeni 
2686784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk)
2687f870fa0bSMat Martineau {
2688cec37a6eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
2689cec37a6eSPeter Krystad 
2690cec37a6eSPeter Krystad 	INIT_LIST_HEAD(&msk->conn_list);
2691ec3edaa7SPeter Krystad 	INIT_LIST_HEAD(&msk->join_list);
269218b683bfSPaolo Abeni 	INIT_LIST_HEAD(&msk->rtx_queue);
269380992017SPaolo Abeni 	INIT_WORK(&msk->work, mptcp_worker);
269487952603SPaolo Abeni 	__skb_queue_head_init(&msk->receive_queue);
2695ab174ad8SPaolo Abeni 	msk->out_of_order_queue = RB_ROOT;
2696f0e6a4cfSPaolo Abeni 	msk->first_pending = NULL;
26976511882cSPaolo Abeni 	msk->rmem_fwd_alloc = 0;
2698ce599c51SPaolo Abeni 	WRITE_ONCE(msk->rmem_released, 0);
269933d41c9cSPaolo Abeni 	msk->timer_ival = TCP_RTO_MIN;
2700cec37a6eSPeter Krystad 
27018ab183deSPaolo Abeni 	msk->first = NULL;
2702dc24f8b4SPaolo Abeni 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2703752e9067SGeliang Tang 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
27040530020aSGeliang Tang 	WRITE_ONCE(msk->allow_infinite_fallback, true);
27051e1d9d6fSPaolo Abeni 	msk->recovery = false;
27068ab183deSPaolo Abeni 
27071b1c7a0eSPeter Krystad 	mptcp_pm_data_init(msk);
27081b1c7a0eSPeter Krystad 
2709b51f9b80SPaolo Abeni 	/* re-use the csk retrans timer for MPTCP-level retrans */
2710b51f9b80SPaolo Abeni 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2711e16163b6SPaolo Abeni 	timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2712aa1fbd94SFlorian Westphal 
2713f870fa0bSMat Martineau 	return 0;
2714f870fa0bSMat Martineau }
2715f870fa0bSMat Martineau 
2716b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk)
2717784325e9SMatthieu Baerts {
271820b5759fSPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(sk);
2719b29fcfb5SPaolo Abeni 
2720b29fcfb5SPaolo Abeni 	tcp_assign_congestion_control(sk);
2721b29fcfb5SPaolo Abeni 	strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2722b29fcfb5SPaolo Abeni 
2723b29fcfb5SPaolo Abeni 	/* no need to keep a reference to the ops, the name will suffice */
2724b29fcfb5SPaolo Abeni 	tcp_cleanup_congestion_control(sk);
2725b29fcfb5SPaolo Abeni 	icsk->icsk_ca_ops = NULL;
2726b29fcfb5SPaolo Abeni }
2727b29fcfb5SPaolo Abeni 
2728b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk)
2729b29fcfb5SPaolo Abeni {
2730fc518953SFlorian Westphal 	struct net *net = sock_net(sk);
2731fc518953SFlorian Westphal 	int ret;
273218b683bfSPaolo Abeni 
2733b6c08380SGeliang Tang 	ret = __mptcp_init_sock(sk);
2734b6c08380SGeliang Tang 	if (ret)
2735b6c08380SGeliang Tang 		return ret;
2736b6c08380SGeliang Tang 
2737fc518953SFlorian Westphal 	if (!mptcp_is_enabled(net))
2738fc518953SFlorian Westphal 		return -ENOPROTOOPT;
2739fc518953SFlorian Westphal 
2740fc518953SFlorian Westphal 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2741fc518953SFlorian Westphal 		return -ENOMEM;
2742fc518953SFlorian Westphal 
2743fa68018dSPaolo Abeni 	ret = __mptcp_socket_create(mptcp_sk(sk));
2744fa68018dSPaolo Abeni 	if (ret)
2745fa68018dSPaolo Abeni 		return ret;
2746fa68018dSPaolo Abeni 
2747a5ef058dSPaolo Abeni 	set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2748a5ef058dSPaolo Abeni 
274920b5759fSPaolo Abeni 	/* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
275020b5759fSPaolo Abeni 	 * propagate the correct value
275120b5759fSPaolo Abeni 	 */
2752b29fcfb5SPaolo Abeni 	mptcp_ca_reset(sk);
275320b5759fSPaolo Abeni 
2754d027236cSPaolo Abeni 	sk_sockets_allocated_inc(sk);
2755a963853fSGeliang Tang 	sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2756a963853fSGeliang Tang 	sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2757d027236cSPaolo Abeni 
275818b683bfSPaolo Abeni 	return 0;
275918b683bfSPaolo Abeni }
276018b683bfSPaolo Abeni 
276118b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk)
276218b683bfSPaolo Abeni {
276318b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
276418b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
276518b683bfSPaolo Abeni 
2766d9ca1de8SPaolo Abeni 	WRITE_ONCE(msk->first_pending, NULL);
276718b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2768d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
2769784325e9SMatthieu Baerts }
2770784325e9SMatthieu Baerts 
277130e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk)
277280992017SPaolo Abeni {
277380992017SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
277480992017SPaolo Abeni 
2775b2771d24SPaolo Abeni 	if (cancel_work_sync(&msk->work))
2776e16163b6SPaolo Abeni 		__sock_put(sk);
277780992017SPaolo Abeni }
277880992017SPaolo Abeni 
2779d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
278021498490SPeter Krystad {
278121498490SPeter Krystad 	lock_sock(ssk);
278221498490SPeter Krystad 
278321498490SPeter Krystad 	switch (ssk->sk_state) {
278421498490SPeter Krystad 	case TCP_LISTEN:
278521498490SPeter Krystad 		if (!(how & RCV_SHUTDOWN))
278621498490SPeter Krystad 			break;
2787df561f66SGustavo A. R. Silva 		fallthrough;
278821498490SPeter Krystad 	case TCP_SYN_SENT:
278921498490SPeter Krystad 		tcp_disconnect(ssk, O_NONBLOCK);
279021498490SPeter Krystad 		break;
279121498490SPeter Krystad 	default:
279243b54c6eSMat Martineau 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
279343b54c6eSMat Martineau 			pr_debug("Fallback");
279421498490SPeter Krystad 			ssk->sk_shutdown |= how;
279521498490SPeter Krystad 			tcp_shutdown(ssk, how);
279643b54c6eSMat Martineau 		} else {
279743b54c6eSMat Martineau 			pr_debug("Sending DATA_FIN on subflow %p", ssk);
279843b54c6eSMat Martineau 			tcp_send_ack(ssk);
27996477dd39SMat Martineau 			if (!mptcp_timer_pending(sk))
28006477dd39SMat Martineau 				mptcp_reset_timer(sk);
280143b54c6eSMat Martineau 		}
280221498490SPeter Krystad 		break;
280321498490SPeter Krystad 	}
280421498490SPeter Krystad 
280521498490SPeter Krystad 	release_sock(ssk);
280621498490SPeter Krystad }
280721498490SPeter Krystad 
28086920b851SMat Martineau static const unsigned char new_state[16] = {
28096920b851SMat Martineau 	/* current state:     new state:      action:	*/
28106920b851SMat Martineau 	[0 /* (Invalid) */] = TCP_CLOSE,
28116920b851SMat Martineau 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
28126920b851SMat Martineau 	[TCP_SYN_SENT]      = TCP_CLOSE,
28136920b851SMat Martineau 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
28146920b851SMat Martineau 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
28156920b851SMat Martineau 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
28166920b851SMat Martineau 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
28176920b851SMat Martineau 	[TCP_CLOSE]         = TCP_CLOSE,
28186920b851SMat Martineau 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
28196920b851SMat Martineau 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
28206920b851SMat Martineau 	[TCP_LISTEN]        = TCP_CLOSE,
28216920b851SMat Martineau 	[TCP_CLOSING]       = TCP_CLOSING,
28226920b851SMat Martineau 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
28236920b851SMat Martineau };
28246920b851SMat Martineau 
28256920b851SMat Martineau static int mptcp_close_state(struct sock *sk)
28266920b851SMat Martineau {
28276920b851SMat Martineau 	int next = (int)new_state[sk->sk_state];
28286920b851SMat Martineau 	int ns = next & TCP_STATE_MASK;
28296920b851SMat Martineau 
28306920b851SMat Martineau 	inet_sk_state_store(sk, ns);
28316920b851SMat Martineau 
28326920b851SMat Martineau 	return next & TCP_ACTION_FIN;
28336920b851SMat Martineau }
28346920b851SMat Martineau 
2835e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk)
2836e16163b6SPaolo Abeni {
2837e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2838e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2839e16163b6SPaolo Abeni 
2840e16163b6SPaolo Abeni 	pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2841e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2842e16163b6SPaolo Abeni 		 msk->snd_nxt, msk->write_seq);
2843e16163b6SPaolo Abeni 
2844e16163b6SPaolo Abeni 	/* we still need to enqueue subflows or not really shutting down,
2845e16163b6SPaolo Abeni 	 * skip this
2846e16163b6SPaolo Abeni 	 */
2847e16163b6SPaolo Abeni 	if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2848e16163b6SPaolo Abeni 	    mptcp_send_head(sk))
2849e16163b6SPaolo Abeni 		return;
2850e16163b6SPaolo Abeni 
2851e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2852e16163b6SPaolo Abeni 
285326aa2314SPaolo Abeni 	/* fallback socket will not get data_fin/ack, can move to the next
285426aa2314SPaolo Abeni 	 * state now
285526aa2314SPaolo Abeni 	 */
285626aa2314SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
285758cd405bSPaolo Abeni 		WRITE_ONCE(msk->snd_una, msk->write_seq);
285826aa2314SPaolo Abeni 		if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2859e16163b6SPaolo Abeni 			inet_sk_state_store(sk, TCP_CLOSE);
2860e16163b6SPaolo Abeni 			mptcp_close_wake_up(sk);
286126aa2314SPaolo Abeni 		} else if (sk->sk_state == TCP_FIN_WAIT1) {
286226aa2314SPaolo Abeni 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
286326aa2314SPaolo Abeni 		}
2864e16163b6SPaolo Abeni 	}
2865e16163b6SPaolo Abeni 
2866e16163b6SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
2867e16163b6SPaolo Abeni 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2868e16163b6SPaolo Abeni 
2869e16163b6SPaolo Abeni 		mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2870e16163b6SPaolo Abeni 	}
2871e16163b6SPaolo Abeni }
2872e16163b6SPaolo Abeni 
2873e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk)
2874e16163b6SPaolo Abeni {
2875e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2876e16163b6SPaolo Abeni 
2877e16163b6SPaolo Abeni 	pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2878e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2879e16163b6SPaolo Abeni 		 !!mptcp_send_head(sk));
2880e16163b6SPaolo Abeni 
2881e16163b6SPaolo Abeni 	/* will be ignored by fallback sockets */
2882e16163b6SPaolo Abeni 	WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2883e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_data_fin_enable, 1);
2884e16163b6SPaolo Abeni 
2885e16163b6SPaolo Abeni 	__mptcp_check_send_data_fin(sk);
2886e16163b6SPaolo Abeni }
2887e16163b6SPaolo Abeni 
2888e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk)
2889f870fa0bSMat Martineau {
2890f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
2891f870fa0bSMat Martineau 
2892e16163b6SPaolo Abeni 	pr_debug("msk=%p", msk);
28932c22c06cSFlorian Westphal 
28943abc05d9SFlorian Westphal 	might_sleep();
28953abc05d9SFlorian Westphal 
2896bcf3cf93SGeliang Tang 	mptcp_stop_timer(sk);
2897e16163b6SPaolo Abeni 	sk_stop_timer(sk, &sk->sk_timer);
2898e16163b6SPaolo Abeni 	msk->pm.status = 0;
2899b2c5b614SFlorian Westphal 
2900e16163b6SPaolo Abeni 	sk->sk_prot->destroy(sk);
2901e16163b6SPaolo Abeni 
29026511882cSPaolo Abeni 	WARN_ON_ONCE(msk->rmem_fwd_alloc);
290387952603SPaolo Abeni 	WARN_ON_ONCE(msk->rmem_released);
2904e16163b6SPaolo Abeni 	sk_stream_kill_queues(sk);
2905e16163b6SPaolo Abeni 	xfrm_sk_free_policy(sk);
2906aa1fbd94SFlorian Westphal 
2907e16163b6SPaolo Abeni 	sock_put(sk);
2908e16163b6SPaolo Abeni }
2909e16163b6SPaolo Abeni 
2910d21f8348SPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2911d21f8348SPaolo Abeni {
2912d21f8348SPaolo Abeni 	/* Concurrent splices from sk_receive_queue into receive_queue will
2913d21f8348SPaolo Abeni 	 * always show at least one non-empty queue when checked in this order.
2914d21f8348SPaolo Abeni 	 */
2915d21f8348SPaolo Abeni 	if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2916d21f8348SPaolo Abeni 	    skb_queue_empty_lockless(&msk->receive_queue))
2917d21f8348SPaolo Abeni 		return 0;
2918d21f8348SPaolo Abeni 
2919d21f8348SPaolo Abeni 	return EPOLLIN | EPOLLRDNORM;
2920d21f8348SPaolo Abeni }
2921d21f8348SPaolo Abeni 
2922c558246eSMenglong Dong static void mptcp_listen_inuse_dec(struct sock *sk)
2923c558246eSMenglong Dong {
2924c558246eSMenglong Dong 	if (inet_sk_state_load(sk) == TCP_LISTEN)
2925c558246eSMenglong Dong 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2926c558246eSMenglong Dong }
2927c558246eSMenglong Dong 
292826d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout)
2929e16163b6SPaolo Abeni {
2930e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
293176a13b31SGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(sk);
2932e16163b6SPaolo Abeni 	bool do_cancel_work = false;
2933d4e85922SPaolo Abeni 	int subflows_alive = 0;
2934e16163b6SPaolo Abeni 
2935e16163b6SPaolo Abeni 	sk->sk_shutdown = SHUTDOWN_MASK;
2936e16163b6SPaolo Abeni 
2937e16163b6SPaolo Abeni 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2938c558246eSMenglong Dong 		mptcp_listen_inuse_dec(sk);
2939e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
2940e16163b6SPaolo Abeni 		goto cleanup;
2941e16163b6SPaolo Abeni 	}
2942e16163b6SPaolo Abeni 
2943d21f8348SPaolo Abeni 	if (mptcp_check_readable(msk)) {
2944d21f8348SPaolo Abeni 		/* the msk has read data, do the MPTCP equivalent of TCP reset */
2945d21f8348SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
2946d21f8348SPaolo Abeni 		mptcp_do_fastclose(sk);
2947d21f8348SPaolo Abeni 	} else if (mptcp_close_state(sk)) {
2948e16163b6SPaolo Abeni 		__mptcp_wr_shutdown(sk);
2949d21f8348SPaolo Abeni 	}
2950e16163b6SPaolo Abeni 
2951e16163b6SPaolo Abeni 	sk_stream_wait_close(sk, timeout);
2952e16163b6SPaolo Abeni 
2953e16163b6SPaolo Abeni cleanup:
2954e16163b6SPaolo Abeni 	/* orphan all the subflows */
2955e16163b6SPaolo Abeni 	inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
295676a13b31SGeliang Tang 	mptcp_for_each_subflow(msk, subflow) {
2957e16163b6SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
295849054556SPaolo Abeni 		bool slow = lock_sock_fast_nested(ssk);
2959e16163b6SPaolo Abeni 
2960d4e85922SPaolo Abeni 		subflows_alive += ssk->sk_state != TCP_CLOSE;
2961d4e85922SPaolo Abeni 
296276a13b31SGeliang Tang 		/* since the close timeout takes precedence on the fail one,
296376a13b31SGeliang Tang 		 * cancel the latter
296476a13b31SGeliang Tang 		 */
296576a13b31SGeliang Tang 		if (ssk == msk->first)
296676a13b31SGeliang Tang 			subflow->fail_tout = 0;
296776a13b31SGeliang Tang 
2968fe948001SMenglong Dong 		/* detach from the parent socket, but allow data_ready to
2969fe948001SMenglong Dong 		 * push incoming data into the mptcp stack, to properly ack it
2970fe948001SMenglong Dong 		 */
2971fe948001SMenglong Dong 		ssk->sk_socket = NULL;
2972fe948001SMenglong Dong 		ssk->sk_wq = NULL;
2973e16163b6SPaolo Abeni 		unlock_sock_fast(ssk, slow);
2974e16163b6SPaolo Abeni 	}
2975e16163b6SPaolo Abeni 	sock_orphan(sk);
2976e16163b6SPaolo Abeni 
2977d4e85922SPaolo Abeni 	/* all the subflows are closed, only timeout can change the msk
2978d4e85922SPaolo Abeni 	 * state, let's not keep resources busy for no reasons
2979d4e85922SPaolo Abeni 	 */
2980d4e85922SPaolo Abeni 	if (subflows_alive == 0)
2981d4e85922SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
2982d4e85922SPaolo Abeni 
2983e16163b6SPaolo Abeni 	sock_hold(sk);
2984e16163b6SPaolo Abeni 	pr_debug("msk=%p state=%d", sk, sk->sk_state);
298500df24f1SGeliang Tang 	if (msk->token)
298676a13b31SGeliang Tang 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
2987b29fcfb5SPaolo Abeni 
2988e16163b6SPaolo Abeni 	if (sk->sk_state == TCP_CLOSE) {
2989e16163b6SPaolo Abeni 		__mptcp_destroy_sock(sk);
2990e16163b6SPaolo Abeni 		do_cancel_work = true;
2991e16163b6SPaolo Abeni 	} else {
299276a13b31SGeliang Tang 		mptcp_reset_timeout(msk, 0);
2993e16163b6SPaolo Abeni 	}
299426d3e21cSMenglong Dong 
299526d3e21cSMenglong Dong 	return do_cancel_work;
299626d3e21cSMenglong Dong }
299726d3e21cSMenglong Dong 
299826d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout)
299926d3e21cSMenglong Dong {
300026d3e21cSMenglong Dong 	bool do_cancel_work;
300126d3e21cSMenglong Dong 
300226d3e21cSMenglong Dong 	lock_sock(sk);
300326d3e21cSMenglong Dong 
300426d3e21cSMenglong Dong 	do_cancel_work = __mptcp_close(sk, timeout);
3005e16163b6SPaolo Abeni 	release_sock(sk);
3006e16163b6SPaolo Abeni 	if (do_cancel_work)
300780992017SPaolo Abeni 		mptcp_cancel_work(sk);
3008b911c97cSFlorian Westphal 
3009e16163b6SPaolo Abeni 	sock_put(sk);
3010f870fa0bSMat Martineau }
3011f870fa0bSMat Martineau 
3012e72e4032SPaolo Abeni void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3013cf7da0d6SPeter Krystad {
3014cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3015cf7da0d6SPeter Krystad 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3016cf7da0d6SPeter Krystad 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
3017cf7da0d6SPeter Krystad 
3018cf7da0d6SPeter Krystad 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
3019cf7da0d6SPeter Krystad 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3020cf7da0d6SPeter Krystad 
3021cf7da0d6SPeter Krystad 	if (msk6 && ssk6) {
3022cf7da0d6SPeter Krystad 		msk6->saddr = ssk6->saddr;
3023cf7da0d6SPeter Krystad 		msk6->flow_label = ssk6->flow_label;
3024cf7da0d6SPeter Krystad 	}
3025cf7da0d6SPeter Krystad #endif
3026cf7da0d6SPeter Krystad 
3027cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3028cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3029cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3030cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3031cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3032cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3033cf7da0d6SPeter Krystad }
3034cf7da0d6SPeter Krystad 
303518b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags)
303618b683bfSPaolo Abeni {
303776e2a55dSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
303876e2a55dSPaolo Abeni 
30397d803344SPaolo Abeni 	/* We are on the fastopen error path. We can't call straight into the
30407d803344SPaolo Abeni 	 * subflows cleanup code due to lock nesting (we are already under
30417d803344SPaolo Abeni 	 * msk->firstsocket lock). Do nothing and leave the cleanup to the
30427d803344SPaolo Abeni 	 * caller.
30437d803344SPaolo Abeni 	 */
30447d803344SPaolo Abeni 	if (msk->fastopening)
30457d803344SPaolo Abeni 		return 0;
30467d803344SPaolo Abeni 
3047c558246eSMenglong Dong 	mptcp_listen_inuse_dec(sk);
3048b29fcfb5SPaolo Abeni 	inet_sk_state_store(sk, TCP_CLOSE);
3049b29fcfb5SPaolo Abeni 
3050bcf3cf93SGeliang Tang 	mptcp_stop_timer(sk);
3051b29fcfb5SPaolo Abeni 	sk_stop_timer(sk, &sk->sk_timer);
3052b29fcfb5SPaolo Abeni 
305300df24f1SGeliang Tang 	if (msk->token)
305400df24f1SGeliang Tang 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3055b29fcfb5SPaolo Abeni 
3056c0bf3c6aSPaolo Abeni 	/* msk->subflow is still intact, the following will not free the first
3057c0bf3c6aSPaolo Abeni 	 * subflow
3058c0bf3c6aSPaolo Abeni 	 */
3059c0bf3c6aSPaolo Abeni 	mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3060b29fcfb5SPaolo Abeni 	msk->last_snd = NULL;
3061e9d09bacSPaolo Abeni 	WRITE_ONCE(msk->flags, 0);
3062e9d09bacSPaolo Abeni 	msk->cb_flags = 0;
3063e9d09bacSPaolo Abeni 	msk->push_pending = 0;
3064b29fcfb5SPaolo Abeni 	msk->recovery = false;
3065b29fcfb5SPaolo Abeni 	msk->can_ack = false;
3066b29fcfb5SPaolo Abeni 	msk->fully_established = false;
3067b29fcfb5SPaolo Abeni 	msk->rcv_data_fin = false;
3068b29fcfb5SPaolo Abeni 	msk->snd_data_fin_enable = false;
3069b29fcfb5SPaolo Abeni 	msk->rcv_fastclose = false;
3070b29fcfb5SPaolo Abeni 	msk->use_64bit_ack = false;
3071b29fcfb5SPaolo Abeni 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3072b29fcfb5SPaolo Abeni 	mptcp_pm_data_reset(msk);
3073b29fcfb5SPaolo Abeni 	mptcp_ca_reset(sk);
3074b29fcfb5SPaolo Abeni 
3075b29fcfb5SPaolo Abeni 	sk->sk_shutdown = 0;
3076b29fcfb5SPaolo Abeni 	sk_error_report(sk);
307742c556feSFlorian Westphal 	return 0;
307818b683bfSPaolo Abeni }
307918b683bfSPaolo Abeni 
3080b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3081b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3082b0519de8SFlorian Westphal {
3083b0519de8SFlorian Westphal 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3084b0519de8SFlorian Westphal 
3085b0519de8SFlorian Westphal 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3086b0519de8SFlorian Westphal }
3087b0519de8SFlorian Westphal #endif
3088b0519de8SFlorian Westphal 
3089fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk,
3090cfde141eSPaolo Abeni 			    const struct mptcp_options_received *mp_opt,
3091fca5c82cSPaolo Abeni 			    struct request_sock *req)
3092b0519de8SFlorian Westphal {
309358b09919SPaolo Abeni 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3094b0519de8SFlorian Westphal 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
309558b09919SPaolo Abeni 	struct mptcp_sock *msk;
3096b0519de8SFlorian Westphal 
3097b0519de8SFlorian Westphal 	if (!nsk)
3098b0519de8SFlorian Westphal 		return NULL;
3099b0519de8SFlorian Westphal 
3100b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3101b0519de8SFlorian Westphal 	if (nsk->sk_family == AF_INET6)
3102b0519de8SFlorian Westphal 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3103b0519de8SFlorian Westphal #endif
3104b0519de8SFlorian Westphal 
310558b09919SPaolo Abeni 	__mptcp_init_sock(nsk);
310658b09919SPaolo Abeni 
310758b09919SPaolo Abeni 	msk = mptcp_sk(nsk);
310858b09919SPaolo Abeni 	msk->local_key = subflow_req->local_key;
310958b09919SPaolo Abeni 	msk->token = subflow_req->token;
311058b09919SPaolo Abeni 	msk->subflow = NULL;
3111b6985b9bSPaolo Abeni 	msk->in_accept_queue = 1;
3112b93df08cSPaolo Abeni 	WRITE_ONCE(msk->fully_established, false);
311374c7dfbeSPaolo Abeni 	if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
311406251181SGeliang Tang 		WRITE_ONCE(msk->csum_enabled, true);
311558b09919SPaolo Abeni 
311658b09919SPaolo Abeni 	msk->write_seq = subflow_req->idsn + 1;
3117eaa2ffabSPaolo Abeni 	msk->snd_nxt = msk->write_seq;
31187439d687SPaolo Abeni 	msk->snd_una = msk->write_seq;
31197439d687SPaolo Abeni 	msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3120df00b087SFlorian Westphal 	msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
31216f8a612aSFlorian Westphal 
31225e20087dSFlorian Westphal 	sock_reset_flag(nsk, SOCK_RCU_FREE);
31237f20d5fcSPaolo Abeni 	/* will be fully established after successful MPC subflow creation */
31247f20d5fcSPaolo Abeni 	inet_sk_state_store(nsk, TCP_SYN_RECV);
31250c148460SPaolo Abeni 
31260c148460SPaolo Abeni 	security_inet_csk_clone(nsk, req);
312758b09919SPaolo Abeni 	bh_unlock_sock(nsk);
312858b09919SPaolo Abeni 
3129b6985b9bSPaolo Abeni 	/* note: the newly allocated socket refcount is 2 now */
3130b0519de8SFlorian Westphal 	return nsk;
3131b0519de8SFlorian Westphal }
3132b0519de8SFlorian Westphal 
3133a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3134a6b118feSFlorian Westphal {
3135a6b118feSFlorian Westphal 	const struct tcp_sock *tp = tcp_sk(ssk);
3136a6b118feSFlorian Westphal 
3137a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
3138a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = 0;
3139a6b118feSFlorian Westphal 
3140a6b118feSFlorian Westphal 	msk->rcvq_space.time = tp->tcp_mstamp;
3141a6b118feSFlorian Westphal 
3142a6b118feSFlorian Westphal 	/* initial rcv_space offering made to peer */
3143a6b118feSFlorian Westphal 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3144a6b118feSFlorian Westphal 				      TCP_INIT_CWND * tp->advmss);
3145a6b118feSFlorian Westphal 	if (msk->rcvq_space.space == 0)
3146a6b118feSFlorian Westphal 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
31476f8a612aSFlorian Westphal 
31487439d687SPaolo Abeni 	WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3149a6b118feSFlorian Westphal }
3150a6b118feSFlorian Westphal 
3151cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
3152cf7da0d6SPeter Krystad 				 bool kern)
3153cf7da0d6SPeter Krystad {
3154cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
3155cf7da0d6SPeter Krystad 	struct socket *listener;
3156cf7da0d6SPeter Krystad 	struct sock *newsk;
3157cf7da0d6SPeter Krystad 
315861761231SPaolo Abeni 	listener = msk->subflow;
3159cf7da0d6SPeter Krystad 	if (WARN_ON_ONCE(!listener)) {
3160cf7da0d6SPeter Krystad 		*err = -EINVAL;
3161cf7da0d6SPeter Krystad 		return NULL;
3162cf7da0d6SPeter Krystad 	}
3163cf7da0d6SPeter Krystad 
3164cf7da0d6SPeter Krystad 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
3165cf7da0d6SPeter Krystad 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
3166cf7da0d6SPeter Krystad 	if (!newsk)
3167cf7da0d6SPeter Krystad 		return NULL;
3168cf7da0d6SPeter Krystad 
3169cf7da0d6SPeter Krystad 	pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
3170cf7da0d6SPeter Krystad 	if (sk_is_mptcp(newsk)) {
3171cf7da0d6SPeter Krystad 		struct mptcp_subflow_context *subflow;
3172cf7da0d6SPeter Krystad 		struct sock *new_mptcp_sock;
3173cf7da0d6SPeter Krystad 
3174cf7da0d6SPeter Krystad 		subflow = mptcp_subflow_ctx(newsk);
317558b09919SPaolo Abeni 		new_mptcp_sock = subflow->conn;
317658b09919SPaolo Abeni 
317758b09919SPaolo Abeni 		/* is_mptcp should be false if subflow->conn is missing, see
317858b09919SPaolo Abeni 		 * subflow_syn_recv_sock()
317958b09919SPaolo Abeni 		 */
318058b09919SPaolo Abeni 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
318158b09919SPaolo Abeni 			tcp_sk(newsk)->is_mptcp = 0;
3182d6692b3bSFlorian Westphal 			goto out;
318358b09919SPaolo Abeni 		}
318458b09919SPaolo Abeni 
3185cf7da0d6SPeter Krystad 		newsk = new_mptcp_sock;
31860397c6d8SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3187fc518953SFlorian Westphal 	} else {
3188fc518953SFlorian Westphal 		MPTCP_INC_STATS(sock_net(sk),
3189fc518953SFlorian Westphal 				MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3190cf7da0d6SPeter Krystad 	}
3191cf7da0d6SPeter Krystad 
3192d6692b3bSFlorian Westphal out:
3193d6692b3bSFlorian Westphal 	newsk->sk_kern_sock = kern;
3194cf7da0d6SPeter Krystad 	return newsk;
3195cf7da0d6SPeter Krystad }
3196cf7da0d6SPeter Krystad 
3197c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
31985c8c1640SGeliang Tang {
3199c0bf3c6aSPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
320087952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
320187952603SPaolo Abeni 
32026e628cd3SPaolo Abeni 	__mptcp_clear_xmit(sk);
32036e628cd3SPaolo Abeni 
3204c0bf3c6aSPaolo Abeni 	/* join list will be eventually flushed (with rst) at sock lock release time */
32055efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
3206c0bf3c6aSPaolo Abeni 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3207c0bf3c6aSPaolo Abeni 
320887952603SPaolo Abeni 	/* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3209b29fcfb5SPaolo Abeni 	mptcp_data_lock(sk);
321087952603SPaolo Abeni 	skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
32116511882cSPaolo Abeni 	__skb_queue_purge(&sk->sk_receive_queue);
32125c8c1640SGeliang Tang 	skb_rbtree_purge(&msk->out_of_order_queue);
3213b29fcfb5SPaolo Abeni 	mptcp_data_unlock(sk);
32146511882cSPaolo Abeni 
32156511882cSPaolo Abeni 	/* move all the rx fwd alloc into the sk_mem_reclaim_final in
32166511882cSPaolo Abeni 	 * inet_sock_destruct() will dispose it
32176511882cSPaolo Abeni 	 */
32186511882cSPaolo Abeni 	sk->sk_forward_alloc += msk->rmem_fwd_alloc;
32196511882cSPaolo Abeni 	msk->rmem_fwd_alloc = 0;
32205c8c1640SGeliang Tang 	mptcp_token_destroy(msk);
32215c8c1640SGeliang Tang 	mptcp_pm_free_anno_list(msk);
32224638de5aSKishen Maloor 	mptcp_free_local_addr_list(msk);
32235c8c1640SGeliang Tang }
32245c8c1640SGeliang Tang 
322579c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk)
322679c0949eSPeter Krystad {
3227c9fd9c5fSFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
3228c9fd9c5fSFlorian Westphal 
3229c0bf3c6aSPaolo Abeni 	/* clears msk->subflow, allowing the following to close
3230c0bf3c6aSPaolo Abeni 	 * even the initial subflow
3231c0bf3c6aSPaolo Abeni 	 */
3232c0bf3c6aSPaolo Abeni 	mptcp_dispose_initial_subflow(msk);
3233c0bf3c6aSPaolo Abeni 	mptcp_destroy_common(msk, 0);
3234d027236cSPaolo Abeni 	sk_sockets_allocated_dec(sk);
323579c0949eSPeter Krystad }
323679c0949eSPeter Krystad 
32376e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk)
32386e628cd3SPaolo Abeni {
32396e628cd3SPaolo Abeni 	if (!sock_owned_by_user(sk))
32406e628cd3SPaolo Abeni 		__mptcp_clean_una(sk);
32416e628cd3SPaolo Abeni 	else
3242e9d09bacSPaolo Abeni 		__set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
32436e628cd3SPaolo Abeni 
32446e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk))
32456e628cd3SPaolo Abeni 		mptcp_schedule_work(sk);
32466e628cd3SPaolo Abeni }
32476e628cd3SPaolo Abeni 
3248219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk)
32496e628cd3SPaolo Abeni {
32506e628cd3SPaolo Abeni 	if (!mptcp_send_head(sk))
32516e628cd3SPaolo Abeni 		return;
32526e628cd3SPaolo Abeni 
325373a0052aSGeliang Tang 	if (!sock_owned_by_user(sk))
325473a0052aSGeliang Tang 		__mptcp_subflow_push_pending(sk, ssk, false);
325573a0052aSGeliang Tang 	else
3256e9d09bacSPaolo Abeni 		__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
32576e628cd3SPaolo Abeni }
32586e628cd3SPaolo Abeni 
3259e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3260e9d09bacSPaolo Abeni 				      BIT(MPTCP_RETRANSMIT) | \
3261e9d09bacSPaolo Abeni 				      BIT(MPTCP_FLUSH_JOIN_LIST))
3262e9d09bacSPaolo Abeni 
3263e93da928SPaolo Abeni /* processes deferred events and flush wmem */
326414c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk)
3265e9d09bacSPaolo Abeni 	__must_hold(&sk->sk_lock.slock)
326614c441b5SPaolo Abeni {
3267e9d09bacSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
32682d6f5a2bSPaolo Abeni 
3269e9d09bacSPaolo Abeni 	for (;;) {
3270e9d09bacSPaolo Abeni 		unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3271e9d09bacSPaolo Abeni 				      msk->push_pending;
3272c2e6048fSPaolo Abeni 		if (!flags)
3273c2e6048fSPaolo Abeni 			break;
3274c2e6048fSPaolo Abeni 
3275c2e6048fSPaolo Abeni 		/* the following actions acquire the subflow socket lock
32766e628cd3SPaolo Abeni 		 *
32776e628cd3SPaolo Abeni 		 * 1) can't be invoked in atomic scope
32786e628cd3SPaolo Abeni 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
32796e628cd3SPaolo Abeni 		 *    datapath acquires the msk socket spinlock while helding
32806e628cd3SPaolo Abeni 		 *    the subflow socket lock
32816e628cd3SPaolo Abeni 		 */
3282e9d09bacSPaolo Abeni 		msk->push_pending = 0;
3283e9d09bacSPaolo Abeni 		msk->cb_flags &= ~flags;
32846e628cd3SPaolo Abeni 		spin_unlock_bh(&sk->sk_lock.slock);
32853e501490SPaolo Abeni 		if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
32863e501490SPaolo Abeni 			__mptcp_flush_join_list(sk);
32872e5de7e0SDan Carpenter 		if (flags & BIT(MPTCP_PUSH_PENDING))
3288c2e6048fSPaolo Abeni 			__mptcp_push_pending(sk, 0);
32892d6f5a2bSPaolo Abeni 		if (flags & BIT(MPTCP_RETRANSMIT))
32902d6f5a2bSPaolo Abeni 			__mptcp_retrans(sk);
3291c2e6048fSPaolo Abeni 
3292c2e6048fSPaolo Abeni 		cond_resched();
32936e628cd3SPaolo Abeni 		spin_lock_bh(&sk->sk_lock.slock);
32946e628cd3SPaolo Abeni 	}
3295c2e6048fSPaolo Abeni 
329665a569b0SPaolo Abeni 	if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
329765a569b0SPaolo Abeni 		__mptcp_clean_una_wakeup(sk);
329865a569b0SPaolo Abeni 	if (unlikely(&msk->cb_flags)) {
3299490274b4SPaolo Abeni 		/* be sure to set the current sk state before tacking actions
330065a569b0SPaolo Abeni 		 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3301490274b4SPaolo Abeni 		 */
3302e9d09bacSPaolo Abeni 		if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3303490274b4SPaolo Abeni 			__mptcp_set_connected(sk);
3304e9d09bacSPaolo Abeni 		if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
330515cc1045SPaolo Abeni 			__mptcp_error_report(sk);
330643f5b111SPaolo Abeni 		if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags))
330743f5b111SPaolo Abeni 			msk->last_snd = NULL;
330865a569b0SPaolo Abeni 	}
33096e628cd3SPaolo Abeni 
331087952603SPaolo Abeni 	__mptcp_update_rmem(sk);
331114c441b5SPaolo Abeni }
331214c441b5SPaolo Abeni 
3313bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3314bcd97734SPaolo Abeni  * TCP can't schedule delack timer before the subflow is fully established.
3315bcd97734SPaolo Abeni  * MPTCP uses the delack timer to do 3rd ack retransmissions
3316bcd97734SPaolo Abeni  */
3317bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk)
3318bcd97734SPaolo Abeni {
3319bcd97734SPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(ssk);
3320bcd97734SPaolo Abeni 	struct tcp_sock *tp = tcp_sk(ssk);
3321bcd97734SPaolo Abeni 	unsigned long timeout;
3322bcd97734SPaolo Abeni 
3323bcd97734SPaolo Abeni 	if (mptcp_subflow_ctx(ssk)->fully_established)
3324bcd97734SPaolo Abeni 		return;
3325bcd97734SPaolo Abeni 
3326bcd97734SPaolo Abeni 	/* reschedule with a timeout above RTT, as we must look only for drop */
3327bcd97734SPaolo Abeni 	if (tp->srtt_us)
3328bcd97734SPaolo Abeni 		timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3329bcd97734SPaolo Abeni 	else
3330bcd97734SPaolo Abeni 		timeout = TCP_TIMEOUT_INIT;
3331bcd97734SPaolo Abeni 	timeout += jiffies;
3332bcd97734SPaolo Abeni 
3333bcd97734SPaolo Abeni 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3334bcd97734SPaolo Abeni 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3335bcd97734SPaolo Abeni 	icsk->icsk_ack.timeout = timeout;
3336bcd97734SPaolo Abeni 	sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3337bcd97734SPaolo Abeni }
3338bcd97734SPaolo Abeni 
3339b19bc294SPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk)
3340b19bc294SPaolo Abeni {
3341b19bc294SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3342b19bc294SPaolo Abeni 	struct sock *sk = subflow->conn;
3343b19bc294SPaolo Abeni 
3344bcd97734SPaolo Abeni 	if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3345b19bc294SPaolo Abeni 		mptcp_data_lock(sk);
3346b19bc294SPaolo Abeni 		if (!sock_owned_by_user(sk))
334773a0052aSGeliang Tang 			__mptcp_subflow_push_pending(sk, ssk, true);
3348b19bc294SPaolo Abeni 		else
3349e9d09bacSPaolo Abeni 			__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3350b19bc294SPaolo Abeni 		mptcp_data_unlock(sk);
3351bcd97734SPaolo Abeni 		mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3352bcd97734SPaolo Abeni 	}
3353bcd97734SPaolo Abeni 	if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3354bcd97734SPaolo Abeni 		schedule_3rdack_retransmission(ssk);
3355bcd97734SPaolo Abeni 		mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3356bcd97734SPaolo Abeni 	}
3357b19bc294SPaolo Abeni }
3358b19bc294SPaolo Abeni 
33592c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk)
33602c5ebd00SPaolo Abeni {
33612c5ebd00SPaolo Abeni 	/* should never be called,
33622c5ebd00SPaolo Abeni 	 * we hash the TCP subflows not the master socket
33632c5ebd00SPaolo Abeni 	 */
33642c5ebd00SPaolo Abeni 	WARN_ON_ONCE(1);
33652c5ebd00SPaolo Abeni 	return 0;
33662c5ebd00SPaolo Abeni }
33672c5ebd00SPaolo Abeni 
33682c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk)
33692c5ebd00SPaolo Abeni {
33702c5ebd00SPaolo Abeni 	/* called from sk_common_release(), but nothing to do here */
33712c5ebd00SPaolo Abeni }
33722c5ebd00SPaolo Abeni 
3373cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum)
3374f870fa0bSMat Martineau {
3375f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
3376cec37a6eSPeter Krystad 	struct socket *ssock;
3377f870fa0bSMat Martineau 
337861761231SPaolo Abeni 	ssock = msk->subflow;
3379cec37a6eSPeter Krystad 	pr_debug("msk=%p, subflow=%p", msk, ssock);
3380cec37a6eSPeter Krystad 	if (WARN_ON_ONCE(!ssock))
3381cec37a6eSPeter Krystad 		return -EINVAL;
3382f870fa0bSMat Martineau 
3383cec37a6eSPeter Krystad 	return inet_csk_get_port(ssock->sk, snum);
3384cec37a6eSPeter Krystad }
3385f870fa0bSMat Martineau 
3386cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk)
3387cec37a6eSPeter Krystad {
3388cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow;
3389cec37a6eSPeter Krystad 	struct mptcp_sock *msk;
3390cec37a6eSPeter Krystad 	struct sock *sk;
3391f870fa0bSMat Martineau 
3392cec37a6eSPeter Krystad 	subflow = mptcp_subflow_ctx(ssk);
3393cec37a6eSPeter Krystad 	sk = subflow->conn;
3394cec37a6eSPeter Krystad 	msk = mptcp_sk(sk);
3395cec37a6eSPeter Krystad 
3396648ef4b8SMat Martineau 	pr_debug("msk=%p, token=%u", sk, subflow->token);
3397648ef4b8SMat Martineau 
3398b3ea6b27SPaolo Abeni 	subflow->map_seq = subflow->iasn;
3399648ef4b8SMat Martineau 	subflow->map_subflow_seq = 1;
34006d0060f6SMat Martineau 
3401cec37a6eSPeter Krystad 	/* the socket is not connected yet, no msk/subflow ops can access/race
3402cec37a6eSPeter Krystad 	 * accessing the field below
3403cec37a6eSPeter Krystad 	 */
3404cec37a6eSPeter Krystad 	WRITE_ONCE(msk->local_key, subflow->local_key);
34056d0060f6SMat Martineau 	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3406eaa2ffabSPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
34077439d687SPaolo Abeni 	WRITE_ONCE(msk->snd_una, msk->write_seq);
34081b1c7a0eSPeter Krystad 
34096c714f1bSFlorian Westphal 	mptcp_pm_new_connection(msk, ssk, 0);
3410a6b118feSFlorian Westphal 
3411a6b118feSFlorian Westphal 	mptcp_rcv_space_init(msk, ssk);
3412f870fa0bSMat Martineau }
3413f870fa0bSMat Martineau 
3414866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3415cf7da0d6SPeter Krystad {
3416cf7da0d6SPeter Krystad 	write_lock_bh(&sk->sk_callback_lock);
3417cf7da0d6SPeter Krystad 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
3418cf7da0d6SPeter Krystad 	sk_set_socket(sk, parent);
3419cf7da0d6SPeter Krystad 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
3420cf7da0d6SPeter Krystad 	write_unlock_bh(&sk->sk_callback_lock);
3421cf7da0d6SPeter Krystad }
3422cf7da0d6SPeter Krystad 
3423e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk)
3424f296234cSPeter Krystad {
3425e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3426f296234cSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3427f296234cSPeter Krystad 	struct sock *parent = (void *)msk;
34283e501490SPaolo Abeni 	bool ret = true;
3429f296234cSPeter Krystad 
3430f296234cSPeter Krystad 	pr_debug("msk=%p, subflow=%p", msk, subflow);
3431f296234cSPeter Krystad 
3432f296234cSPeter Krystad 	/* mptcp socket already closing? */
3433dc87efdbSFlorian Westphal 	if (!mptcp_is_fully_established(parent)) {
3434dc87efdbSFlorian Westphal 		subflow->reset_reason = MPTCP_RST_EMPTCP;
3435f296234cSPeter Krystad 		return false;
3436dc87efdbSFlorian Westphal 	}
3437f296234cSPeter Krystad 
343870c708e8SKishen Maloor 	if (!list_empty(&subflow->node))
3439b911c97cSFlorian Westphal 		goto out;
3440f296234cSPeter Krystad 
34413e501490SPaolo Abeni 	if (!mptcp_pm_allow_new_subflow(msk))
34423e501490SPaolo Abeni 		goto err_prohibited;
344310f6d46cSPaolo Abeni 
34443e501490SPaolo Abeni 	/* active connections are already on conn_list.
34453e501490SPaolo Abeni 	 * If we can't acquire msk socket lock here, let the release callback
34463e501490SPaolo Abeni 	 * handle it
344710f6d46cSPaolo Abeni 	 */
34483e501490SPaolo Abeni 	mptcp_data_lock(parent);
34493e501490SPaolo Abeni 	if (!sock_owned_by_user(parent)) {
34503e501490SPaolo Abeni 		ret = __mptcp_finish_join(msk, ssk);
34513e501490SPaolo Abeni 		if (ret) {
3452e16163b6SPaolo Abeni 			sock_hold(ssk);
34533e501490SPaolo Abeni 			list_add_tail(&subflow->node, &msk->conn_list);
3454e16163b6SPaolo Abeni 		}
34553e501490SPaolo Abeni 	} else {
34563e501490SPaolo Abeni 		sock_hold(ssk);
34573e501490SPaolo Abeni 		list_add_tail(&subflow->node, &msk->join_list);
3458e9d09bacSPaolo Abeni 		__set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
34593e501490SPaolo Abeni 	}
34603e501490SPaolo Abeni 	mptcp_data_unlock(parent);
34613e501490SPaolo Abeni 
3462dc87efdbSFlorian Westphal 	if (!ret) {
34633e501490SPaolo Abeni err_prohibited:
3464dc87efdbSFlorian Westphal 		subflow->reset_reason = MPTCP_RST_EPROHIBIT;
346510f6d46cSPaolo Abeni 		return false;
3466dc87efdbSFlorian Westphal 	}
346710f6d46cSPaolo Abeni 
3468917944daSMat Martineau 	subflow->map_seq = READ_ONCE(msk->ack_seq);
34690530020aSGeliang Tang 	WRITE_ONCE(msk->allow_infinite_fallback, false);
34703e501490SPaolo Abeni 
3471b911c97cSFlorian Westphal out:
3472b911c97cSFlorian Westphal 	mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
347310f6d46cSPaolo Abeni 	return true;
3474f296234cSPeter Krystad }
3475f296234cSPeter Krystad 
347676e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how)
347776e2a55dSPaolo Abeni {
347876e2a55dSPaolo Abeni 	pr_debug("sk=%p, how=%d", sk, how);
347976e2a55dSPaolo Abeni 
348076e2a55dSPaolo Abeni 	if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
348176e2a55dSPaolo Abeni 		__mptcp_wr_shutdown(sk);
348276e2a55dSPaolo Abeni }
348376e2a55dSPaolo Abeni 
34846511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk)
34856511882cSPaolo Abeni {
34866511882cSPaolo Abeni 	return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
34876511882cSPaolo Abeni }
34886511882cSPaolo Abeni 
3489644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3490644807e3SFlorian Westphal {
3491644807e3SFlorian Westphal 	const struct sock *sk = (void *)msk;
3492644807e3SFlorian Westphal 	u64 delta;
3493644807e3SFlorian Westphal 
3494644807e3SFlorian Westphal 	if (sk->sk_state == TCP_LISTEN)
3495644807e3SFlorian Westphal 		return -EINVAL;
3496644807e3SFlorian Westphal 
3497644807e3SFlorian Westphal 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3498644807e3SFlorian Westphal 		return 0;
3499644807e3SFlorian Westphal 
3500644807e3SFlorian Westphal 	delta = msk->write_seq - v;
350107c2c7a3SPaolo Abeni 	if (__mptcp_check_fallback(msk) && msk->first) {
350207c2c7a3SPaolo Abeni 		struct tcp_sock *tp = tcp_sk(msk->first);
350307c2c7a3SPaolo Abeni 
350407c2c7a3SPaolo Abeni 		/* the first subflow is disconnected after close - see
350507c2c7a3SPaolo Abeni 		 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
350607c2c7a3SPaolo Abeni 		 * so ignore that status, too.
350707c2c7a3SPaolo Abeni 		 */
350807c2c7a3SPaolo Abeni 		if (!((1 << msk->first->sk_state) &
350907c2c7a3SPaolo Abeni 		      (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
351007c2c7a3SPaolo Abeni 			delta += READ_ONCE(tp->write_seq) - tp->snd_una;
351107c2c7a3SPaolo Abeni 	}
3512644807e3SFlorian Westphal 	if (delta > INT_MAX)
3513644807e3SFlorian Westphal 		delta = INT_MAX;
3514644807e3SFlorian Westphal 
3515644807e3SFlorian Westphal 	return (int)delta;
3516644807e3SFlorian Westphal }
3517644807e3SFlorian Westphal 
3518644807e3SFlorian Westphal static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3519644807e3SFlorian Westphal {
3520644807e3SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
3521644807e3SFlorian Westphal 	bool slow;
3522644807e3SFlorian Westphal 	int answ;
3523644807e3SFlorian Westphal 
3524644807e3SFlorian Westphal 	switch (cmd) {
3525644807e3SFlorian Westphal 	case SIOCINQ:
3526644807e3SFlorian Westphal 		if (sk->sk_state == TCP_LISTEN)
3527644807e3SFlorian Westphal 			return -EINVAL;
3528644807e3SFlorian Westphal 
3529644807e3SFlorian Westphal 		lock_sock(sk);
3530644807e3SFlorian Westphal 		__mptcp_move_skbs(msk);
3531644807e3SFlorian Westphal 		answ = mptcp_inq_hint(sk);
3532644807e3SFlorian Westphal 		release_sock(sk);
3533644807e3SFlorian Westphal 		break;
3534644807e3SFlorian Westphal 	case SIOCOUTQ:
3535644807e3SFlorian Westphal 		slow = lock_sock_fast(sk);
3536644807e3SFlorian Westphal 		answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3537644807e3SFlorian Westphal 		unlock_sock_fast(sk, slow);
3538644807e3SFlorian Westphal 		break;
3539644807e3SFlorian Westphal 	case SIOCOUTQNSD:
3540644807e3SFlorian Westphal 		slow = lock_sock_fast(sk);
3541644807e3SFlorian Westphal 		answ = mptcp_ioctl_outq(msk, msk->snd_nxt);
3542644807e3SFlorian Westphal 		unlock_sock_fast(sk, slow);
3543644807e3SFlorian Westphal 		break;
3544644807e3SFlorian Westphal 	default:
3545644807e3SFlorian Westphal 		return -ENOIOCTLCMD;
3546644807e3SFlorian Westphal 	}
3547644807e3SFlorian Westphal 
3548644807e3SFlorian Westphal 	return put_user(answ, (int __user *)arg);
3549644807e3SFlorian Westphal }
3550644807e3SFlorian Westphal 
355154f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
355254f1944eSPaolo Abeni 					 struct mptcp_subflow_context *subflow)
355354f1944eSPaolo Abeni {
355454f1944eSPaolo Abeni 	subflow->request_mptcp = 0;
355554f1944eSPaolo Abeni 	__mptcp_do_fallback(msk);
355654f1944eSPaolo Abeni }
355754f1944eSPaolo Abeni 
355854f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
355954f1944eSPaolo Abeni {
356054f1944eSPaolo Abeni 	struct mptcp_subflow_context *subflow;
356154f1944eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
356254f1944eSPaolo Abeni 	struct socket *ssock;
356354f1944eSPaolo Abeni 	int err = -EINVAL;
356454f1944eSPaolo Abeni 
356554f1944eSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
356654f1944eSPaolo Abeni 	if (!ssock)
356754f1944eSPaolo Abeni 		return -EINVAL;
356854f1944eSPaolo Abeni 
356954f1944eSPaolo Abeni 	mptcp_token_destroy(msk);
357054f1944eSPaolo Abeni 	inet_sk_state_store(sk, TCP_SYN_SENT);
357154f1944eSPaolo Abeni 	subflow = mptcp_subflow_ctx(ssock->sk);
357254f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG
357354f1944eSPaolo Abeni 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
357454f1944eSPaolo Abeni 	 * TCP option space.
357554f1944eSPaolo Abeni 	 */
357654f1944eSPaolo Abeni 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
357754f1944eSPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
357854f1944eSPaolo Abeni #endif
357954f1944eSPaolo Abeni 	if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
358054f1944eSPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
358154f1944eSPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
358254f1944eSPaolo Abeni 	}
358354f1944eSPaolo Abeni 	if (likely(!__mptcp_check_fallback(msk)))
358454f1944eSPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
358554f1944eSPaolo Abeni 
358654f1944eSPaolo Abeni 	/* if reaching here via the fastopen/sendmsg path, the caller already
358754f1944eSPaolo Abeni 	 * acquired the subflow socket lock, too.
358854f1944eSPaolo Abeni 	 */
35897d803344SPaolo Abeni 	if (msk->fastopening)
359054f1944eSPaolo Abeni 		err = __inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags, 1);
359154f1944eSPaolo Abeni 	else
359254f1944eSPaolo Abeni 		err = inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags);
359354f1944eSPaolo Abeni 	inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect;
359454f1944eSPaolo Abeni 
359554f1944eSPaolo Abeni 	/* on successful connect, the msk state will be moved to established by
359654f1944eSPaolo Abeni 	 * subflow_finish_connect()
359754f1944eSPaolo Abeni 	 */
359854f1944eSPaolo Abeni 	if (unlikely(err && err != -EINPROGRESS)) {
359954f1944eSPaolo Abeni 		inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
360054f1944eSPaolo Abeni 		return err;
360154f1944eSPaolo Abeni 	}
360254f1944eSPaolo Abeni 
360354f1944eSPaolo Abeni 	mptcp_copy_inaddrs(sk, ssock->sk);
360454f1944eSPaolo Abeni 
360554f1944eSPaolo Abeni 	/* unblocking connect, mptcp-level inet_stream_connect will error out
360654f1944eSPaolo Abeni 	 * without changing the socket state, update it here.
360754f1944eSPaolo Abeni 	 */
360854f1944eSPaolo Abeni 	if (err == -EINPROGRESS)
360954f1944eSPaolo Abeni 		sk->sk_socket->state = ssock->state;
361054f1944eSPaolo Abeni 	return err;
361154f1944eSPaolo Abeni }
361254f1944eSPaolo Abeni 
3613f870fa0bSMat Martineau static struct proto mptcp_prot = {
3614f870fa0bSMat Martineau 	.name		= "MPTCP",
3615f870fa0bSMat Martineau 	.owner		= THIS_MODULE,
3616f870fa0bSMat Martineau 	.init		= mptcp_init_sock,
361754f1944eSPaolo Abeni 	.connect	= mptcp_connect,
361818b683bfSPaolo Abeni 	.disconnect	= mptcp_disconnect,
3619f870fa0bSMat Martineau 	.close		= mptcp_close,
3620cf7da0d6SPeter Krystad 	.accept		= mptcp_accept,
3621717e79c8SPeter Krystad 	.setsockopt	= mptcp_setsockopt,
3622717e79c8SPeter Krystad 	.getsockopt	= mptcp_getsockopt,
362376e2a55dSPaolo Abeni 	.shutdown	= mptcp_shutdown,
362479c0949eSPeter Krystad 	.destroy	= mptcp_destroy,
3625f870fa0bSMat Martineau 	.sendmsg	= mptcp_sendmsg,
3626644807e3SFlorian Westphal 	.ioctl		= mptcp_ioctl,
3627f870fa0bSMat Martineau 	.recvmsg	= mptcp_recvmsg,
362814c441b5SPaolo Abeni 	.release_cb	= mptcp_release_cb,
36292c5ebd00SPaolo Abeni 	.hash		= mptcp_hash,
36302c5ebd00SPaolo Abeni 	.unhash		= mptcp_unhash,
3631cec37a6eSPeter Krystad 	.get_port	= mptcp_get_port,
36326511882cSPaolo Abeni 	.forward_alloc_get	= mptcp_forward_alloc_get,
3633d027236cSPaolo Abeni 	.sockets_allocated	= &mptcp_sockets_allocated,
36340defbb0aSEric Dumazet 
3635d027236cSPaolo Abeni 	.memory_allocated	= &tcp_memory_allocated,
36360defbb0aSEric Dumazet 	.per_cpu_fw_alloc	= &tcp_memory_per_cpu_fw_alloc,
36370defbb0aSEric Dumazet 
3638d027236cSPaolo Abeni 	.memory_pressure	= &tcp_memory_pressure,
3639d027236cSPaolo Abeni 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
3640989ef49bSPaolo Abeni 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
3641d027236cSPaolo Abeni 	.sysctl_mem	= sysctl_tcp_mem,
3642f870fa0bSMat Martineau 	.obj_size	= sizeof(struct mptcp_sock),
36432c5ebd00SPaolo Abeni 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
3644f870fa0bSMat Martineau 	.no_autobind	= true,
3645f870fa0bSMat Martineau };
3646f870fa0bSMat Martineau 
36472303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
36482303f994SPeter Krystad {
36492303f994SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
36502303f994SPeter Krystad 	struct socket *ssock;
3651cf7da0d6SPeter Krystad 	int err;
36522303f994SPeter Krystad 
36532303f994SPeter Krystad 	lock_sock(sock->sk);
3654fa68018dSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
3655fa68018dSPaolo Abeni 	if (!ssock) {
3656fa68018dSPaolo Abeni 		err = -EINVAL;
36572303f994SPeter Krystad 		goto unlock;
36582303f994SPeter Krystad 	}
36592303f994SPeter Krystad 
36602303f994SPeter Krystad 	err = ssock->ops->bind(ssock, uaddr, addr_len);
3661cf7da0d6SPeter Krystad 	if (!err)
3662cf7da0d6SPeter Krystad 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
36632303f994SPeter Krystad 
36642303f994SPeter Krystad unlock:
36652303f994SPeter Krystad 	release_sock(sock->sk);
36662303f994SPeter Krystad 	return err;
36672303f994SPeter Krystad }
36682303f994SPeter Krystad 
36692303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
36702303f994SPeter Krystad 				int addr_len, int flags)
36712303f994SPeter Krystad {
367254f1944eSPaolo Abeni 	int ret;
36732303f994SPeter Krystad 
36742303f994SPeter Krystad 	lock_sock(sock->sk);
367554f1944eSPaolo Abeni 	mptcp_sk(sock->sk)->connect_flags = flags;
367654f1944eSPaolo Abeni 	ret = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
36772303f994SPeter Krystad 	release_sock(sock->sk);
367854f1944eSPaolo Abeni 	return ret;
36792303f994SPeter Krystad }
36802303f994SPeter Krystad 
3681cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog)
3682cf7da0d6SPeter Krystad {
3683cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3684cfdcfeedSMenglong Dong 	struct sock *sk = sock->sk;
3685cf7da0d6SPeter Krystad 	struct socket *ssock;
3686cf7da0d6SPeter Krystad 	int err;
3687cf7da0d6SPeter Krystad 
3688cf7da0d6SPeter Krystad 	pr_debug("msk=%p", msk);
3689cf7da0d6SPeter Krystad 
3690cfdcfeedSMenglong Dong 	lock_sock(sk);
3691fa68018dSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
3692fa68018dSPaolo Abeni 	if (!ssock) {
3693fa68018dSPaolo Abeni 		err = -EINVAL;
3694cf7da0d6SPeter Krystad 		goto unlock;
3695cf7da0d6SPeter Krystad 	}
3696cf7da0d6SPeter Krystad 
3697fa68018dSPaolo Abeni 	mptcp_token_destroy(msk);
3698cfdcfeedSMenglong Dong 	inet_sk_state_store(sk, TCP_LISTEN);
3699cfdcfeedSMenglong Dong 	sock_set_flag(sk, SOCK_RCU_FREE);
37005e20087dSFlorian Westphal 
3701cf7da0d6SPeter Krystad 	err = ssock->ops->listen(ssock, backlog);
3702cfdcfeedSMenglong Dong 	inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3703c558246eSMenglong Dong 	if (!err) {
3704c558246eSMenglong Dong 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3705cfdcfeedSMenglong Dong 		mptcp_copy_inaddrs(sk, ssock->sk);
3706c558246eSMenglong Dong 	}
3707cf7da0d6SPeter Krystad 
3708f8c9dfbdSGeliang Tang 	mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED);
3709f8c9dfbdSGeliang Tang 
3710cf7da0d6SPeter Krystad unlock:
3711cfdcfeedSMenglong Dong 	release_sock(sk);
3712cf7da0d6SPeter Krystad 	return err;
3713cf7da0d6SPeter Krystad }
3714cf7da0d6SPeter Krystad 
3715cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3716cf7da0d6SPeter Krystad 			       int flags, bool kern)
3717cf7da0d6SPeter Krystad {
3718cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3719cf7da0d6SPeter Krystad 	struct socket *ssock;
3720cf7da0d6SPeter Krystad 	int err;
3721cf7da0d6SPeter Krystad 
3722cf7da0d6SPeter Krystad 	pr_debug("msk=%p", msk);
3723cf7da0d6SPeter Krystad 
372461761231SPaolo Abeni 	/* buggy applications can call accept on socket states other then LISTEN
372561761231SPaolo Abeni 	 * but no need to allocate the first subflow just to error out.
372661761231SPaolo Abeni 	 */
372761761231SPaolo Abeni 	ssock = msk->subflow;
3728cf7da0d6SPeter Krystad 	if (!ssock)
372971ba088cSPaolo Abeni 		return -EINVAL;
3730cf7da0d6SPeter Krystad 
3731cf7da0d6SPeter Krystad 	err = ssock->ops->accept(sock, newsock, flags, kern);
3732d2f77c53SPaolo Abeni 	if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3733cf7da0d6SPeter Krystad 		struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3734cf7da0d6SPeter Krystad 		struct mptcp_subflow_context *subflow;
37350397c6d8SPaolo Abeni 		struct sock *newsk = newsock->sk;
37360397c6d8SPaolo Abeni 
3737a5ef058dSPaolo Abeni 		set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3738b6985b9bSPaolo Abeni 		msk->in_accept_queue = 0;
3739a5ef058dSPaolo Abeni 
37404d54cc32SFlorian Westphal 		lock_sock(newsk);
37415b950ff4SPaolo Abeni 
3742cf7da0d6SPeter Krystad 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
3743cf7da0d6SPeter Krystad 		 * This is needed so NOSPACE flag can be set from tcp stack.
3744cf7da0d6SPeter Krystad 		 */
3745190f8b06SGeliang Tang 		mptcp_for_each_subflow(msk, subflow) {
3746cf7da0d6SPeter Krystad 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3747cf7da0d6SPeter Krystad 
3748cf7da0d6SPeter Krystad 			if (!ssk->sk_socket)
3749cf7da0d6SPeter Krystad 				mptcp_sock_graft(ssk, newsock);
3750cf7da0d6SPeter Krystad 		}
37514d54cc32SFlorian Westphal 		release_sock(newsk);
3752cf7da0d6SPeter Krystad 	}
3753cf7da0d6SPeter Krystad 
3754cf7da0d6SPeter Krystad 	return err;
3755cf7da0d6SPeter Krystad }
3756cf7da0d6SPeter Krystad 
37578edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
37588edf0864SFlorian Westphal {
37598edf0864SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
37608edf0864SFlorian Westphal 
37618edf0864SFlorian Westphal 	if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
3762dd913410SPaolo Abeni 		return EPOLLOUT | EPOLLWRNORM;
37638edf0864SFlorian Westphal 
37648edf0864SFlorian Westphal 	if (sk_stream_is_writeable(sk))
37658edf0864SFlorian Westphal 		return EPOLLOUT | EPOLLWRNORM;
37668edf0864SFlorian Westphal 
37675cf92bbaSPaolo Abeni 	mptcp_set_nospace(sk);
37686e628cd3SPaolo Abeni 	smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
37696e628cd3SPaolo Abeni 	if (sk_stream_is_writeable(sk))
37706e628cd3SPaolo Abeni 		return EPOLLOUT | EPOLLWRNORM;
37718edf0864SFlorian Westphal 
37726e628cd3SPaolo Abeni 	return 0;
37738edf0864SFlorian Westphal }
37748edf0864SFlorian Westphal 
37752303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock,
37762303f994SPeter Krystad 			   struct poll_table_struct *wait)
37772303f994SPeter Krystad {
37781891c4a0SFlorian Westphal 	struct sock *sk = sock->sk;
37798ab183deSPaolo Abeni 	struct mptcp_sock *msk;
37802303f994SPeter Krystad 	__poll_t mask = 0;
37818a05661bSPaolo Abeni 	int state;
37822303f994SPeter Krystad 
37831891c4a0SFlorian Westphal 	msk = mptcp_sk(sk);
37841891c4a0SFlorian Westphal 	sock_poll_wait(file, sock, wait);
37851891c4a0SFlorian Westphal 
37868a05661bSPaolo Abeni 	state = inet_sk_state_load(sk);
37876719331cSPaolo Abeni 	pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
378871ba088cSPaolo Abeni 	if (state == TCP_LISTEN) {
378971ba088cSPaolo Abeni 		if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk))
379071ba088cSPaolo Abeni 			return 0;
379171ba088cSPaolo Abeni 
379271ba088cSPaolo Abeni 		return inet_csk_listen_poll(msk->subflow->sk);
379371ba088cSPaolo Abeni 	}
37948a05661bSPaolo Abeni 
37958a05661bSPaolo Abeni 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
37968a05661bSPaolo Abeni 		mask |= mptcp_check_readable(msk);
37978edf0864SFlorian Westphal 		mask |= mptcp_check_writeable(msk);
3798a42cf9d1SBenjamin Hesmans 	} else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
3799a42cf9d1SBenjamin Hesmans 		/* cf tcp_poll() note about TFO */
3800a42cf9d1SBenjamin Hesmans 		mask |= EPOLLOUT | EPOLLWRNORM;
38018a05661bSPaolo Abeni 	}
3802dd913410SPaolo Abeni 	if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3803dd913410SPaolo Abeni 		mask |= EPOLLHUP;
38041891c4a0SFlorian Westphal 	if (sk->sk_shutdown & RCV_SHUTDOWN)
38051891c4a0SFlorian Westphal 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
38061891c4a0SFlorian Westphal 
3807d89e3ed7SPaolo Abeni 	/* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
380815cc1045SPaolo Abeni 	smp_rmb();
38099ae8e5adSEric Dumazet 	if (READ_ONCE(sk->sk_err))
381015cc1045SPaolo Abeni 		mask |= EPOLLERR;
381115cc1045SPaolo Abeni 
38122303f994SPeter Krystad 	return mask;
38132303f994SPeter Krystad }
38142303f994SPeter Krystad 
3815e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = {
3816e42f1ac6SFlorian Westphal 	.family		   = PF_INET,
3817e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
38180a3cc579SPaolo Abeni 	.release	   = inet_release,
3819e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
3820e42f1ac6SFlorian Westphal 	.connect	   = mptcp_stream_connect,
3821e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
3822e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
3823d2f77c53SPaolo Abeni 	.getname	   = inet_getname,
3824e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
3825e42f1ac6SFlorian Westphal 	.ioctl		   = inet_ioctl,
3826e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
3827e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
382876e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
3829e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
3830e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
3831e42f1ac6SFlorian Westphal 	.sendmsg	   = inet_sendmsg,
3832e42f1ac6SFlorian Westphal 	.recvmsg	   = inet_recvmsg,
3833e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
3834e42f1ac6SFlorian Westphal 	.sendpage	   = inet_sendpage,
3835e42f1ac6SFlorian Westphal };
38362303f994SPeter Krystad 
3837f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = {
3838f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
3839f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
3840f870fa0bSMat Martineau 	.prot		= &mptcp_prot,
38412303f994SPeter Krystad 	.ops		= &mptcp_stream_ops,
38422303f994SPeter Krystad 	.flags		= INET_PROTOSW_ICSK,
3843f870fa0bSMat Martineau };
3844f870fa0bSMat Martineau 
3845b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3846b19bc294SPaolo Abeni {
3847b19bc294SPaolo Abeni 	struct mptcp_delegated_action *delegated;
3848b19bc294SPaolo Abeni 	struct mptcp_subflow_context *subflow;
3849b19bc294SPaolo Abeni 	int work_done = 0;
3850b19bc294SPaolo Abeni 
3851b19bc294SPaolo Abeni 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
3852b19bc294SPaolo Abeni 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3853b19bc294SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3854b19bc294SPaolo Abeni 
3855b19bc294SPaolo Abeni 		bh_lock_sock_nested(ssk);
3856b19bc294SPaolo Abeni 		if (!sock_owned_by_user(ssk) &&
3857b19bc294SPaolo Abeni 		    mptcp_subflow_has_delegated_action(subflow))
3858b19bc294SPaolo Abeni 			mptcp_subflow_process_delegated(ssk);
3859b19bc294SPaolo Abeni 		/* ... elsewhere tcp_release_cb_override already processed
3860b19bc294SPaolo Abeni 		 * the action or will do at next release_sock().
3861b19bc294SPaolo Abeni 		 * In both case must dequeue the subflow here - on the same
3862b19bc294SPaolo Abeni 		 * CPU that scheduled it.
3863b19bc294SPaolo Abeni 		 */
3864b19bc294SPaolo Abeni 		bh_unlock_sock(ssk);
3865b19bc294SPaolo Abeni 		sock_put(ssk);
3866b19bc294SPaolo Abeni 
3867b19bc294SPaolo Abeni 		if (++work_done == budget)
3868b19bc294SPaolo Abeni 			return budget;
3869b19bc294SPaolo Abeni 	}
3870b19bc294SPaolo Abeni 
3871b19bc294SPaolo Abeni 	/* always provide a 0 'work_done' argument, so that napi_complete_done
3872b19bc294SPaolo Abeni 	 * will not try accessing the NULL napi->dev ptr
3873b19bc294SPaolo Abeni 	 */
3874b19bc294SPaolo Abeni 	napi_complete_done(napi, 0);
3875b19bc294SPaolo Abeni 	return work_done;
3876b19bc294SPaolo Abeni }
3877b19bc294SPaolo Abeni 
3878d39dcecaSPaolo Abeni void __init mptcp_proto_init(void)
3879f870fa0bSMat Martineau {
3880b19bc294SPaolo Abeni 	struct mptcp_delegated_action *delegated;
3881b19bc294SPaolo Abeni 	int cpu;
3882b19bc294SPaolo Abeni 
38832303f994SPeter Krystad 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
38842303f994SPeter Krystad 
3885d027236cSPaolo Abeni 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3886d027236cSPaolo Abeni 		panic("Failed to allocate MPTCP pcpu counter\n");
3887d027236cSPaolo Abeni 
3888b19bc294SPaolo Abeni 	init_dummy_netdev(&mptcp_napi_dev);
3889b19bc294SPaolo Abeni 	for_each_possible_cpu(cpu) {
3890b19bc294SPaolo Abeni 		delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3891b19bc294SPaolo Abeni 		INIT_LIST_HEAD(&delegated->head);
389216d083e2SJakub Kicinski 		netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
389316d083e2SJakub Kicinski 				  mptcp_napi_poll);
3894b19bc294SPaolo Abeni 		napi_enable(&delegated->napi);
3895b19bc294SPaolo Abeni 	}
3896b19bc294SPaolo Abeni 
38972303f994SPeter Krystad 	mptcp_subflow_init();
38981b1c7a0eSPeter Krystad 	mptcp_pm_init();
38992c5ebd00SPaolo Abeni 	mptcp_token_init();
39002303f994SPeter Krystad 
3901f870fa0bSMat Martineau 	if (proto_register(&mptcp_prot, 1) != 0)
3902f870fa0bSMat Martineau 		panic("Failed to register MPTCP proto.\n");
3903f870fa0bSMat Martineau 
3904f870fa0bSMat Martineau 	inet_register_protosw(&mptcp_protosw);
39056771bfd9SFlorian Westphal 
39066771bfd9SFlorian Westphal 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3907f870fa0bSMat Martineau }
3908f870fa0bSMat Martineau 
3909f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3910e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = {
3911e42f1ac6SFlorian Westphal 	.family		   = PF_INET6,
3912e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
39130a3cc579SPaolo Abeni 	.release	   = inet6_release,
3914e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
3915e42f1ac6SFlorian Westphal 	.connect	   = mptcp_stream_connect,
3916e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
3917e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
3918d2f77c53SPaolo Abeni 	.getname	   = inet6_getname,
3919e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
3920e42f1ac6SFlorian Westphal 	.ioctl		   = inet6_ioctl,
3921e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
3922e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
392376e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
3924e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
3925e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
3926e42f1ac6SFlorian Westphal 	.sendmsg	   = inet6_sendmsg,
3927e42f1ac6SFlorian Westphal 	.recvmsg	   = inet6_recvmsg,
3928e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
3929e42f1ac6SFlorian Westphal 	.sendpage	   = inet_sendpage,
3930e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT
39313986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
3932e42f1ac6SFlorian Westphal #endif
3933e42f1ac6SFlorian Westphal };
3934e42f1ac6SFlorian Westphal 
3935f870fa0bSMat Martineau static struct proto mptcp_v6_prot;
3936f870fa0bSMat Martineau 
3937f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = {
3938f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
3939f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
3940f870fa0bSMat Martineau 	.prot		= &mptcp_v6_prot,
39412303f994SPeter Krystad 	.ops		= &mptcp_v6_stream_ops,
3942f870fa0bSMat Martineau 	.flags		= INET_PROTOSW_ICSK,
3943f870fa0bSMat Martineau };
3944f870fa0bSMat Martineau 
3945d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void)
3946f870fa0bSMat Martineau {
3947f870fa0bSMat Martineau 	int err;
3948f870fa0bSMat Martineau 
3949f870fa0bSMat Martineau 	mptcp_v6_prot = mptcp_prot;
3950f870fa0bSMat Martineau 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
3951f870fa0bSMat Martineau 	mptcp_v6_prot.slab = NULL;
3952b0519de8SFlorian Westphal 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3953f870fa0bSMat Martineau 
3954f870fa0bSMat Martineau 	err = proto_register(&mptcp_v6_prot, 1);
3955f870fa0bSMat Martineau 	if (err)
3956f870fa0bSMat Martineau 		return err;
3957f870fa0bSMat Martineau 
3958f870fa0bSMat Martineau 	err = inet6_register_protosw(&mptcp_v6_protosw);
3959f870fa0bSMat Martineau 	if (err)
3960f870fa0bSMat Martineau 		proto_unregister(&mptcp_v6_prot);
3961f870fa0bSMat Martineau 
3962f870fa0bSMat Martineau 	return err;
3963f870fa0bSMat Martineau }
3964f870fa0bSMat Martineau #endif
3965