xref: /openbmc/linux/net/mptcp/protocol.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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);
4781c1d029SPaolo 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 
526f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */
mptcp_wnd_end(const struct mptcp_sock * msk)536f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
546f8a612aSFlorian Westphal {
557439d687SPaolo Abeni 	return READ_ONCE(msk->wnd_end);
566f8a612aSFlorian Westphal }
576f8a612aSFlorian Westphal 
mptcp_fallback_tcp_ops(const struct sock * sk)5812f353faSDavide Caratti static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
590b4f33deSFlorian Westphal {
600b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
6112f353faSDavide Caratti 	if (sk->sk_prot == &tcpv6_prot)
6212f353faSDavide Caratti 		return &inet6_stream_ops;
630b4f33deSFlorian Westphal #endif
6412f353faSDavide Caratti 	WARN_ON_ONCE(sk->sk_prot != &tcp_prot);
6512f353faSDavide Caratti 	return &inet_stream_ops;
660b4f33deSFlorian Westphal }
670b4f33deSFlorian Westphal 
__mptcp_socket_create(struct mptcp_sock * msk)68fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk)
692303f994SPeter Krystad {
702303f994SPeter Krystad 	struct mptcp_subflow_context *subflow;
712303f994SPeter Krystad 	struct sock *sk = (struct sock *)msk;
722303f994SPeter Krystad 	struct socket *ssock;
732303f994SPeter Krystad 	int err;
742303f994SPeter Krystad 
756bc1fe7dSPaolo Abeni 	err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
762303f994SPeter Krystad 	if (err)
77fa68018dSPaolo Abeni 		return err;
782303f994SPeter Krystad 
79b8dc6d6cSPaolo Abeni 	msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
801b1b43eeSPaolo Abeni 	WRITE_ONCE(msk->first, ssock->sk);
812303f994SPeter Krystad 	subflow = mptcp_subflow_ctx(ssock->sk);
82cec37a6eSPeter Krystad 	list_add(&subflow->node, &msk->conn_list);
83e16163b6SPaolo Abeni 	sock_hold(ssock->sk);
842303f994SPeter Krystad 	subflow->request_mptcp = 1;
856f06b4d4SPaolo Abeni 	subflow->subflow_id = msk->subflow_id++;
864cf86ae8SPaolo Abeni 
874cf86ae8SPaolo Abeni 	/* This is the first subflow, always with id 0 */
88ba2cf922SPaolo Abeni 	WRITE_ONCE(subflow->local_id, 0);
89866f26f2SPaolo Abeni 	mptcp_sock_graft(msk->first, sk->sk_socket);
9039880bd8SPaolo Abeni 	iput(SOCK_INODE(ssock));
91e1ff9e82SDavide Caratti 
92fa68018dSPaolo Abeni 	return 0;
932303f994SPeter Krystad }
942303f994SPeter Krystad 
95ddb1a072SPaolo Abeni /* If the MPC handshake is not started, returns the first subflow,
96ddb1a072SPaolo Abeni  * eventually allocating it.
97ddb1a072SPaolo Abeni  */
__mptcp_nmpc_sk(struct mptcp_sock * msk)983f326a82SPaolo Abeni struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
99ddb1a072SPaolo Abeni {
100ddb1a072SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
101ddb1a072SPaolo Abeni 	int ret;
102ddb1a072SPaolo Abeni 
103ddb1a072SPaolo Abeni 	if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
104ddb1a072SPaolo Abeni 		return ERR_PTR(-EINVAL);
105ddb1a072SPaolo Abeni 
1063f326a82SPaolo Abeni 	if (!msk->first) {
107ddb1a072SPaolo Abeni 		ret = __mptcp_socket_create(msk);
108ddb1a072SPaolo Abeni 		if (ret)
109ddb1a072SPaolo Abeni 			return ERR_PTR(ret);
110ddb1a072SPaolo Abeni 
111ddb1a072SPaolo Abeni 		mptcp_sockopt_sync(msk, msk->first);
112ddb1a072SPaolo Abeni 	}
113ddb1a072SPaolo Abeni 
1143f326a82SPaolo Abeni 	return msk->first;
115ddb1a072SPaolo Abeni }
116ddb1a072SPaolo Abeni 
mptcp_drop(struct sock * sk,struct sk_buff * skb)117ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
118ab174ad8SPaolo Abeni {
119ab174ad8SPaolo Abeni 	sk_drops_add(sk, skb);
120ab174ad8SPaolo Abeni 	__kfree_skb(skb);
121ab174ad8SPaolo Abeni }
122ab174ad8SPaolo Abeni 
mptcp_rmem_fwd_alloc_add(struct sock * sk,int size)1239531e4a8SEric Dumazet static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size)
1249531e4a8SEric Dumazet {
1259531e4a8SEric Dumazet 	WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc,
1269531e4a8SEric Dumazet 		   mptcp_sk(sk)->rmem_fwd_alloc + size);
1279531e4a8SEric Dumazet }
1289531e4a8SEric Dumazet 
mptcp_rmem_charge(struct sock * sk,int size)1296511882cSPaolo Abeni static void mptcp_rmem_charge(struct sock *sk, int size)
1306511882cSPaolo Abeni {
1319531e4a8SEric Dumazet 	mptcp_rmem_fwd_alloc_add(sk, -size);
1326511882cSPaolo Abeni }
1336511882cSPaolo Abeni 
mptcp_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from)1348268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
1358268ed4cSPaolo Abeni 			       struct sk_buff *from)
1368268ed4cSPaolo Abeni {
1378268ed4cSPaolo Abeni 	bool fragstolen;
1388268ed4cSPaolo Abeni 	int delta;
1398268ed4cSPaolo Abeni 
1408268ed4cSPaolo Abeni 	if (MPTCP_SKB_CB(from)->offset ||
1418268ed4cSPaolo Abeni 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
1428268ed4cSPaolo Abeni 		return false;
1438268ed4cSPaolo Abeni 
1443d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
14506242e44SPaolo Abeni 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
14606242e44SPaolo Abeni 		 to->len, MPTCP_SKB_CB(from)->end_seq);
147ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
1487288ff6eSPaolo Abeni 
1497288ff6eSPaolo Abeni 	/* note the fwd memory can reach a negative value after accounting
1507288ff6eSPaolo Abeni 	 * for the delta, but the later skb free will restore a non
1517288ff6eSPaolo Abeni 	 * negative one
1527288ff6eSPaolo Abeni 	 */
1538268ed4cSPaolo Abeni 	atomic_add(delta, &sk->sk_rmem_alloc);
1546511882cSPaolo Abeni 	mptcp_rmem_charge(sk, delta);
1557288ff6eSPaolo Abeni 	kfree_skb_partial(from, fragstolen);
1567288ff6eSPaolo Abeni 
1578268ed4cSPaolo Abeni 	return true;
1588268ed4cSPaolo Abeni }
1598268ed4cSPaolo Abeni 
mptcp_ooo_try_coalesce(struct mptcp_sock * msk,struct sk_buff * to,struct sk_buff * from)160ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
161ab174ad8SPaolo Abeni 				   struct sk_buff *from)
1626771bfd9SFlorian Westphal {
163ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
164ab174ad8SPaolo Abeni 		return false;
165ab174ad8SPaolo Abeni 
166ab174ad8SPaolo Abeni 	return mptcp_try_coalesce((struct sock *)msk, to, from);
167ab174ad8SPaolo Abeni }
168ab174ad8SPaolo Abeni 
__mptcp_rmem_reclaim(struct sock * sk,int amount)1696511882cSPaolo Abeni static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
1706511882cSPaolo Abeni {
171100fdd1fSEric Dumazet 	amount >>= PAGE_SHIFT;
1729531e4a8SEric Dumazet 	mptcp_rmem_charge(sk, amount << PAGE_SHIFT);
1736511882cSPaolo Abeni 	__sk_mem_reduce_allocated(sk, amount);
1746511882cSPaolo Abeni }
1756511882cSPaolo Abeni 
mptcp_rmem_uncharge(struct sock * sk,int size)1766511882cSPaolo Abeni static void mptcp_rmem_uncharge(struct sock *sk, int size)
1776511882cSPaolo Abeni {
1786511882cSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1796511882cSPaolo Abeni 	int reclaimable;
1806511882cSPaolo Abeni 
1819531e4a8SEric Dumazet 	mptcp_rmem_fwd_alloc_add(sk, size);
1826511882cSPaolo Abeni 	reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
1836511882cSPaolo Abeni 
1846511882cSPaolo Abeni 	/* see sk_mem_uncharge() for the rationale behind the following schema */
185d24141feSPaolo Abeni 	if (unlikely(reclaimable >= PAGE_SIZE))
186d24141feSPaolo Abeni 		__mptcp_rmem_reclaim(sk, reclaimable);
1876511882cSPaolo Abeni }
1886511882cSPaolo Abeni 
mptcp_rfree(struct sk_buff * skb)1896511882cSPaolo Abeni static void mptcp_rfree(struct sk_buff *skb)
1906511882cSPaolo Abeni {
1916511882cSPaolo Abeni 	unsigned int len = skb->truesize;
1926511882cSPaolo Abeni 	struct sock *sk = skb->sk;
1936511882cSPaolo Abeni 
1946511882cSPaolo Abeni 	atomic_sub(len, &sk->sk_rmem_alloc);
1956511882cSPaolo Abeni 	mptcp_rmem_uncharge(sk, len);
1966511882cSPaolo Abeni }
1976511882cSPaolo Abeni 
mptcp_set_owner_r(struct sk_buff * skb,struct sock * sk)19836b122baSDmytro Shytyi void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
1996511882cSPaolo Abeni {
2006511882cSPaolo Abeni 	skb_orphan(skb);
2016511882cSPaolo Abeni 	skb->sk = sk;
2026511882cSPaolo Abeni 	skb->destructor = mptcp_rfree;
2036511882cSPaolo Abeni 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
2046511882cSPaolo Abeni 	mptcp_rmem_charge(sk, skb->truesize);
2056511882cSPaolo Abeni }
2066511882cSPaolo Abeni 
207ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences:
208ab174ad8SPaolo Abeni  * - use mptcp seqs
209ab174ad8SPaolo Abeni  * - don't cope with sacks
210ab174ad8SPaolo Abeni  */
mptcp_data_queue_ofo(struct mptcp_sock * msk,struct sk_buff * skb)211ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
212ab174ad8SPaolo Abeni {
213ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
214ab174ad8SPaolo Abeni 	struct rb_node **p, *parent;
215ab174ad8SPaolo Abeni 	u64 seq, end_seq, max_seq;
216ab174ad8SPaolo Abeni 	struct sk_buff *skb1;
217ab174ad8SPaolo Abeni 
218ab174ad8SPaolo Abeni 	seq = MPTCP_SKB_CB(skb)->map_seq;
219ab174ad8SPaolo Abeni 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
220f3589be0SPaolo Abeni 	max_seq = atomic64_read(&msk->rcv_wnd_sent);
221ab174ad8SPaolo Abeni 
2223d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
22306242e44SPaolo Abeni 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
224fa3fe2b1SFlorian Westphal 	if (after64(end_seq, max_seq)) {
225ab174ad8SPaolo Abeni 		/* out of window */
226ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb);
227fa3fe2b1SFlorian Westphal 		pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
228fa3fe2b1SFlorian Westphal 			 (unsigned long long)end_seq - (unsigned long)max_seq,
229f3589be0SPaolo Abeni 			 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
23006242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
231ab174ad8SPaolo Abeni 		return;
232ab174ad8SPaolo Abeni 	}
233ab174ad8SPaolo Abeni 
234ab174ad8SPaolo Abeni 	p = &msk->out_of_order_queue.rb_node;
23506242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
236ab174ad8SPaolo Abeni 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
237ab174ad8SPaolo Abeni 		rb_link_node(&skb->rbnode, NULL, p);
238ab174ad8SPaolo Abeni 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
239ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
240ab174ad8SPaolo Abeni 		goto end;
241ab174ad8SPaolo Abeni 	}
242ab174ad8SPaolo Abeni 
243ab174ad8SPaolo Abeni 	/* with 2 subflows, adding at end of ooo queue is quite likely
244ab174ad8SPaolo Abeni 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
245ab174ad8SPaolo Abeni 	 */
24606242e44SPaolo Abeni 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
24706242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
24806242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
249ab174ad8SPaolo Abeni 		return;
25006242e44SPaolo Abeni 	}
251ab174ad8SPaolo Abeni 
252ab174ad8SPaolo Abeni 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
253ab174ad8SPaolo Abeni 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
25406242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
255ab174ad8SPaolo Abeni 		parent = &msk->ooo_last_skb->rbnode;
256ab174ad8SPaolo Abeni 		p = &parent->rb_right;
257ab174ad8SPaolo Abeni 		goto insert;
258ab174ad8SPaolo Abeni 	}
259ab174ad8SPaolo Abeni 
260ab174ad8SPaolo Abeni 	/* Find place to insert this segment. Handle overlaps on the way. */
261ab174ad8SPaolo Abeni 	parent = NULL;
262ab174ad8SPaolo Abeni 	while (*p) {
263ab174ad8SPaolo Abeni 		parent = *p;
264ab174ad8SPaolo Abeni 		skb1 = rb_to_skb(parent);
265ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
266ab174ad8SPaolo Abeni 			p = &parent->rb_left;
267ab174ad8SPaolo Abeni 			continue;
268ab174ad8SPaolo Abeni 		}
269ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
270ab174ad8SPaolo Abeni 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
271ab174ad8SPaolo Abeni 				/* All the bits are present. Drop. */
272ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb);
27306242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
274ab174ad8SPaolo Abeni 				return;
275ab174ad8SPaolo Abeni 			}
276ab174ad8SPaolo Abeni 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
277ab174ad8SPaolo Abeni 				/* partial overlap:
278ab174ad8SPaolo Abeni 				 *     |     skb      |
279ab174ad8SPaolo Abeni 				 *  |     skb1    |
280ab174ad8SPaolo Abeni 				 * continue traversing
281ab174ad8SPaolo Abeni 				 */
282ab174ad8SPaolo Abeni 			} else {
283ab174ad8SPaolo Abeni 				/* skb's seq == skb1's seq and skb covers skb1.
284ab174ad8SPaolo Abeni 				 * Replace skb1 with skb.
285ab174ad8SPaolo Abeni 				 */
286ab174ad8SPaolo Abeni 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
287ab174ad8SPaolo Abeni 						&msk->out_of_order_queue);
288ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb1);
28906242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
290ab174ad8SPaolo Abeni 				goto merge_right;
291ab174ad8SPaolo Abeni 			}
292ab174ad8SPaolo Abeni 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
29306242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
294ab174ad8SPaolo Abeni 			return;
295ab174ad8SPaolo Abeni 		}
296ab174ad8SPaolo Abeni 		p = &parent->rb_right;
297ab174ad8SPaolo Abeni 	}
29806242e44SPaolo Abeni 
299ab174ad8SPaolo Abeni insert:
300ab174ad8SPaolo Abeni 	/* Insert segment into RB tree. */
301ab174ad8SPaolo Abeni 	rb_link_node(&skb->rbnode, parent, p);
302ab174ad8SPaolo Abeni 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
303ab174ad8SPaolo Abeni 
304ab174ad8SPaolo Abeni merge_right:
305ab174ad8SPaolo Abeni 	/* Remove other segments covered by skb. */
306ab174ad8SPaolo Abeni 	while ((skb1 = skb_rb_next(skb)) != NULL) {
307ab174ad8SPaolo Abeni 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
308ab174ad8SPaolo Abeni 			break;
309ab174ad8SPaolo Abeni 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
310ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb1);
31106242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
312ab174ad8SPaolo Abeni 	}
313ab174ad8SPaolo Abeni 	/* If there is no skb after us, we are the last_skb ! */
314ab174ad8SPaolo Abeni 	if (!skb1)
315ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
316ab174ad8SPaolo Abeni 
317ab174ad8SPaolo Abeni end:
318ab174ad8SPaolo Abeni 	skb_condense(skb);
3196511882cSPaolo Abeni 	mptcp_set_owner_r(skb, sk);
3206511882cSPaolo Abeni }
3216511882cSPaolo Abeni 
mptcp_rmem_schedule(struct sock * sk,struct sock * ssk,int size)3226511882cSPaolo Abeni static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
3236511882cSPaolo Abeni {
3246511882cSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
3256511882cSPaolo Abeni 	int amt, amount;
3266511882cSPaolo Abeni 
32769d93daeSPaolo Abeni 	if (size <= msk->rmem_fwd_alloc)
3286511882cSPaolo Abeni 		return true;
3296511882cSPaolo Abeni 
33069d93daeSPaolo Abeni 	size -= msk->rmem_fwd_alloc;
3316511882cSPaolo Abeni 	amt = sk_mem_pages(size);
332100fdd1fSEric Dumazet 	amount = amt << PAGE_SHIFT;
3334aaa1685SPaolo Abeni 	if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
3346511882cSPaolo Abeni 		return false;
3356511882cSPaolo Abeni 
3369531e4a8SEric Dumazet 	mptcp_rmem_fwd_alloc_add(sk, amount);
3376511882cSPaolo Abeni 	return true;
338ab174ad8SPaolo Abeni }
339ab174ad8SPaolo Abeni 
__mptcp_move_skb(struct mptcp_sock * msk,struct sock * ssk,struct sk_buff * skb,unsigned int offset,size_t copy_len)340ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
341ab174ad8SPaolo Abeni 			     struct sk_buff *skb, unsigned int offset,
342ab174ad8SPaolo Abeni 			     size_t copy_len)
343ab174ad8SPaolo Abeni {
344ab174ad8SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3456771bfd9SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
3464e637c70SFlorian Westphal 	struct sk_buff *tail;
347b7f653b2SFlorian Westphal 	bool has_rxtstamp;
3486771bfd9SFlorian Westphal 
3496771bfd9SFlorian Westphal 	__skb_unlink(skb, &ssk->sk_receive_queue);
3504e637c70SFlorian Westphal 
3514e637c70SFlorian Westphal 	skb_ext_reset(skb);
3524e637c70SFlorian Westphal 	skb_orphan(skb);
3534e637c70SFlorian Westphal 
3549c3f94e1SPaolo Abeni 	/* try to fetch required memory from subflow */
35519bf3292SPaolo Abeni 	if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) {
35619bf3292SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
3579c3f94e1SPaolo Abeni 		goto drop;
35819bf3292SPaolo Abeni 	}
35972f96132SPaolo Abeni 
360b7f653b2SFlorian Westphal 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
361b7f653b2SFlorian Westphal 
362ab174ad8SPaolo Abeni 	/* the skb map_seq accounts for the skb offset:
363ab174ad8SPaolo Abeni 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
364ab174ad8SPaolo Abeni 	 * value
365ab174ad8SPaolo Abeni 	 */
366ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
367ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
368ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->offset = offset;
369b7f653b2SFlorian Westphal 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
370ab174ad8SPaolo Abeni 
371ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
372ab174ad8SPaolo Abeni 		/* in sequence */
37338967f42SPaolo Abeni 		msk->bytes_received += copy_len;
374917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
3754e637c70SFlorian Westphal 		tail = skb_peek_tail(&sk->sk_receive_queue);
3768268ed4cSPaolo Abeni 		if (tail && mptcp_try_coalesce(sk, tail, skb))
377ab174ad8SPaolo Abeni 			return true;
3784e637c70SFlorian Westphal 
3796511882cSPaolo Abeni 		mptcp_set_owner_r(skb, sk);
3806771bfd9SFlorian Westphal 		__skb_queue_tail(&sk->sk_receive_queue, skb);
381ab174ad8SPaolo Abeni 		return true;
382ab174ad8SPaolo Abeni 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
383ab174ad8SPaolo Abeni 		mptcp_data_queue_ofo(msk, skb);
384ab174ad8SPaolo Abeni 		return false;
385ab174ad8SPaolo Abeni 	}
386ab174ad8SPaolo Abeni 
387ab174ad8SPaolo Abeni 	/* old data, keep it simple and drop the whole pkt, sender
388ab174ad8SPaolo Abeni 	 * will retransmit as needed, if needed.
389ab174ad8SPaolo Abeni 	 */
39006242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
3919c3f94e1SPaolo Abeni drop:
392ab174ad8SPaolo Abeni 	mptcp_drop(sk, skb);
393ab174ad8SPaolo Abeni 	return false;
3946771bfd9SFlorian Westphal }
3956771bfd9SFlorian Westphal 
mptcp_stop_rtx_timer(struct sock * sk)396f6909dc1SPaolo Abeni static void mptcp_stop_rtx_timer(struct sock *sk)
39716a9a9daSMat Martineau {
39816a9a9daSMat Martineau 	struct inet_connection_sock *icsk = inet_csk(sk);
39916a9a9daSMat Martineau 
40016a9a9daSMat Martineau 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
40116a9a9daSMat Martineau 	mptcp_sk(sk)->timer_ival = 0;
40216a9a9daSMat Martineau }
40316a9a9daSMat Martineau 
mptcp_close_wake_up(struct sock * sk)404e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk)
405e16163b6SPaolo Abeni {
406e16163b6SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD))
407e16163b6SPaolo Abeni 		return;
408e16163b6SPaolo Abeni 
409e16163b6SPaolo Abeni 	sk->sk_state_change(sk);
410e16163b6SPaolo Abeni 	if (sk->sk_shutdown == SHUTDOWN_MASK ||
411e16163b6SPaolo Abeni 	    sk->sk_state == TCP_CLOSE)
412e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
413e16163b6SPaolo Abeni 	else
414e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
415e16163b6SPaolo Abeni }
416e16163b6SPaolo Abeni 
mptcp_pending_data_fin_ack(struct sock * sk)4176e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk)
4186e628cd3SPaolo Abeni {
4196e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
4206e628cd3SPaolo Abeni 
42181c1d029SPaolo Abeni 	return ((1 << sk->sk_state) &
4226e628cd3SPaolo Abeni 		(TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
4236e628cd3SPaolo Abeni 	       msk->write_seq == READ_ONCE(msk->snd_una);
4246e628cd3SPaolo Abeni }
4256e628cd3SPaolo Abeni 
mptcp_check_data_fin_ack(struct sock * sk)42616a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk)
42716a9a9daSMat Martineau {
42816a9a9daSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
42916a9a9daSMat Martineau 
43016a9a9daSMat Martineau 	/* Look for an acknowledged DATA_FIN */
4316e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk)) {
43216a9a9daSMat Martineau 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
43316a9a9daSMat Martineau 
43416a9a9daSMat Martineau 		switch (sk->sk_state) {
43516a9a9daSMat Martineau 		case TCP_FIN_WAIT1:
436d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_FIN_WAIT2);
43716a9a9daSMat Martineau 			break;
43816a9a9daSMat Martineau 		case TCP_CLOSING:
43916a9a9daSMat Martineau 		case TCP_LAST_ACK:
440d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_CLOSE);
44116a9a9daSMat Martineau 			break;
44216a9a9daSMat Martineau 		}
44316a9a9daSMat Martineau 
444e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
44516a9a9daSMat Martineau 	}
44616a9a9daSMat Martineau }
44716a9a9daSMat Martineau 
mptcp_pending_data_fin(struct sock * sk,u64 * seq)4483721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
4493721b9b6SMat Martineau {
4503721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
4513721b9b6SMat Martineau 
4523721b9b6SMat Martineau 	if (READ_ONCE(msk->rcv_data_fin) &&
4533721b9b6SMat Martineau 	    ((1 << sk->sk_state) &
4543721b9b6SMat Martineau 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
4553721b9b6SMat Martineau 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
4563721b9b6SMat Martineau 
4573721b9b6SMat Martineau 		if (msk->ack_seq == rcv_data_fin_seq) {
4583721b9b6SMat Martineau 			if (seq)
4593721b9b6SMat Martineau 				*seq = rcv_data_fin_seq;
4603721b9b6SMat Martineau 
4613721b9b6SMat Martineau 			return true;
4623721b9b6SMat Martineau 		}
4633721b9b6SMat Martineau 	}
4643721b9b6SMat Martineau 
4653721b9b6SMat Martineau 	return false;
4663721b9b6SMat Martineau }
4673721b9b6SMat Martineau 
mptcp_set_datafin_timeout(struct sock * sk)468403a40f2SEric Dumazet static void mptcp_set_datafin_timeout(struct sock *sk)
4696477dd39SMat Martineau {
4706477dd39SMat Martineau 	struct inet_connection_sock *icsk = inet_csk(sk);
471877d11f0SMat Martineau 	u32 retransmits;
4726477dd39SMat Martineau 
473877d11f0SMat Martineau 	retransmits = min_t(u32, icsk->icsk_retransmits,
474877d11f0SMat Martineau 			    ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
475877d11f0SMat Martineau 
476877d11f0SMat Martineau 	mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
4776477dd39SMat Martineau }
4786477dd39SMat Martineau 
__mptcp_set_timeout(struct sock * sk,long tout)47933d41c9cSPaolo Abeni static void __mptcp_set_timeout(struct sock *sk, long tout)
4803721b9b6SMat Martineau {
4813721b9b6SMat Martineau 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
4823721b9b6SMat Martineau }
4833721b9b6SMat Martineau 
mptcp_timeout_from_subflow(const struct mptcp_subflow_context * subflow)48433d41c9cSPaolo Abeni static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
48533d41c9cSPaolo Abeni {
48633d41c9cSPaolo Abeni 	const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
48733d41c9cSPaolo Abeni 
48871b7dec2SPaolo Abeni 	return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
48971b7dec2SPaolo Abeni 	       inet_csk(ssk)->icsk_timeout - jiffies : 0;
49033d41c9cSPaolo Abeni }
49133d41c9cSPaolo Abeni 
mptcp_set_timeout(struct sock * sk)49233d41c9cSPaolo Abeni static void mptcp_set_timeout(struct sock *sk)
49333d41c9cSPaolo Abeni {
49433d41c9cSPaolo Abeni 	struct mptcp_subflow_context *subflow;
49533d41c9cSPaolo Abeni 	long tout = 0;
49633d41c9cSPaolo Abeni 
49733d41c9cSPaolo Abeni 	mptcp_for_each_subflow(mptcp_sk(sk), subflow)
49833d41c9cSPaolo Abeni 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
49933d41c9cSPaolo Abeni 	__mptcp_set_timeout(sk, tout);
50033d41c9cSPaolo Abeni }
50133d41c9cSPaolo Abeni 
tcp_can_send_ack(const struct sock * ssk)50276a13b31SGeliang Tang static inline bool tcp_can_send_ack(const struct sock *ssk)
503fd897679SPaolo Abeni {
504fd897679SPaolo Abeni 	return !((1 << inet_sk_state_load(ssk)) &
50520bc80b6SPaolo Abeni 	       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
506fd897679SPaolo Abeni }
507fd897679SPaolo Abeni 
__mptcp_subflow_send_ack(struct sock * ssk)508a6574302SMat Martineau void __mptcp_subflow_send_ack(struct sock *ssk)
509a6574302SMat Martineau {
510a6574302SMat Martineau 	if (tcp_can_send_ack(ssk))
511a6574302SMat Martineau 		tcp_send_ack(ssk);
512a6574302SMat Martineau }
513a6574302SMat Martineau 
mptcp_subflow_send_ack(struct sock * ssk)514f5360e9bSPaolo Abeni static void mptcp_subflow_send_ack(struct sock *ssk)
5157ed90803SPaolo Abeni {
51675e908c3SPaolo Abeni 	bool slow;
5177ed90803SPaolo Abeni 
51875e908c3SPaolo Abeni 	slow = lock_sock_fast(ssk);
519a6574302SMat Martineau 	__mptcp_subflow_send_ack(ssk);
52075e908c3SPaolo Abeni 	unlock_sock_fast(ssk, slow);
521fd897679SPaolo Abeni }
522340fa666SMat Martineau 
mptcp_send_ack(struct mptcp_sock * msk)523340fa666SMat Martineau static void mptcp_send_ack(struct mptcp_sock *msk)
524340fa666SMat Martineau {
525340fa666SMat Martineau 	struct mptcp_subflow_context *subflow;
526340fa666SMat Martineau 
527340fa666SMat Martineau 	mptcp_for_each_subflow(msk, subflow)
528340fa666SMat Martineau 		mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
529ea4ca586SPaolo Abeni }
530ea4ca586SPaolo Abeni 
mptcp_subflow_cleanup_rbuf(struct sock * ssk)531fde56eeaSPaolo Abeni static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
532fd897679SPaolo Abeni {
53375e908c3SPaolo Abeni 	bool slow;
534fd897679SPaolo Abeni 
53575e908c3SPaolo Abeni 	slow = lock_sock_fast(ssk);
536fde56eeaSPaolo Abeni 	if (tcp_can_send_ack(ssk))
537fd897679SPaolo Abeni 		tcp_cleanup_rbuf(ssk, 1);
53875e908c3SPaolo Abeni 	unlock_sock_fast(ssk, slow);
539fde56eeaSPaolo Abeni }
540fde56eeaSPaolo Abeni 
mptcp_subflow_could_cleanup(const struct sock * ssk,bool rx_empty)541fde56eeaSPaolo Abeni static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
542fde56eeaSPaolo Abeni {
543fde56eeaSPaolo Abeni 	const struct inet_connection_sock *icsk = inet_csk(ssk);
544c4512c63SMatthieu Baerts 	u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
545fde56eeaSPaolo Abeni 	const struct tcp_sock *tp = tcp_sk(ssk);
546fde56eeaSPaolo Abeni 
547fde56eeaSPaolo Abeni 	return (ack_pending & ICSK_ACK_SCHED) &&
548fde56eeaSPaolo Abeni 		((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
549fde56eeaSPaolo Abeni 		  READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
550fde56eeaSPaolo Abeni 		 (rx_empty && ack_pending &
551fde56eeaSPaolo Abeni 			      (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
552ea4ca586SPaolo Abeni }
553fd897679SPaolo Abeni 
mptcp_cleanup_rbuf(struct mptcp_sock * msk)554fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
555fd897679SPaolo Abeni {
556e3859603SPaolo Abeni 	int old_space = READ_ONCE(msk->old_wspace);
557fd897679SPaolo Abeni 	struct mptcp_subflow_context *subflow;
558e3859603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
559fde56eeaSPaolo Abeni 	int space =  __mptcp_space(sk);
560fde56eeaSPaolo Abeni 	bool cleanup, rx_empty;
561e3859603SPaolo Abeni 
562fde56eeaSPaolo Abeni 	cleanup = (space > 0) && (space >= (old_space << 1));
563ce599c51SPaolo Abeni 	rx_empty = !__mptcp_rmem(sk);
564fd897679SPaolo Abeni 
565fd897679SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
566fd897679SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
567fd897679SPaolo Abeni 
568fde56eeaSPaolo Abeni 		if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
569fde56eeaSPaolo Abeni 			mptcp_subflow_cleanup_rbuf(ssk);
5707ed90803SPaolo Abeni 	}
5717ed90803SPaolo Abeni }
5727ed90803SPaolo Abeni 
mptcp_check_data_fin(struct sock * sk)5737ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk)
5743721b9b6SMat Martineau {
5753721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
5763721b9b6SMat Martineau 	u64 rcv_data_fin_seq;
5777ed90803SPaolo Abeni 	bool ret = false;
5783721b9b6SMat Martineau 
5793721b9b6SMat Martineau 	/* Need to ack a DATA_FIN received from a peer while this side
5803721b9b6SMat Martineau 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
5813721b9b6SMat Martineau 	 * msk->rcv_data_fin was set when parsing the incoming options
5823721b9b6SMat Martineau 	 * at the subflow level and the msk lock was not held, so this
5833721b9b6SMat Martineau 	 * is the first opportunity to act on the DATA_FIN and change
5843721b9b6SMat Martineau 	 * the msk state.
5853721b9b6SMat Martineau 	 *
5863721b9b6SMat Martineau 	 * If we are caught up to the sequence number of the incoming
5873721b9b6SMat Martineau 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
5883721b9b6SMat Martineau 	 * not caught up, do nothing and let the recv code send DATA_ACK
5893721b9b6SMat Martineau 	 * when catching up.
5903721b9b6SMat Martineau 	 */
5913721b9b6SMat Martineau 
5923721b9b6SMat Martineau 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
593917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
5943721b9b6SMat Martineau 		WRITE_ONCE(msk->rcv_data_fin, 0);
5953721b9b6SMat Martineau 
5966b9831bfSPaolo Abeni 		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
59716a9a9daSMat Martineau 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
5983721b9b6SMat Martineau 
5993721b9b6SMat Martineau 		switch (sk->sk_state) {
6003721b9b6SMat Martineau 		case TCP_ESTABLISHED:
601d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_CLOSE_WAIT);
6023721b9b6SMat Martineau 			break;
6033721b9b6SMat Martineau 		case TCP_FIN_WAIT1:
604d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_CLOSING);
6053721b9b6SMat Martineau 			break;
6063721b9b6SMat Martineau 		case TCP_FIN_WAIT2:
607d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_CLOSE);
6083721b9b6SMat Martineau 			break;
6093721b9b6SMat Martineau 		default:
6103721b9b6SMat Martineau 			/* Other states not expected */
6113721b9b6SMat Martineau 			WARN_ON_ONCE(1);
6123721b9b6SMat Martineau 			break;
6133721b9b6SMat Martineau 		}
6143721b9b6SMat Martineau 
6157ed90803SPaolo Abeni 		ret = true;
61681c1d029SPaolo Abeni 		if (!__mptcp_check_fallback(msk))
617fd897679SPaolo Abeni 			mptcp_send_ack(msk);
618e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
6193721b9b6SMat Martineau 	}
6207ed90803SPaolo Abeni 	return ret;
6213721b9b6SMat Martineau }
6223721b9b6SMat Martineau 
mptcp_dss_corruption(struct mptcp_sock * msk,struct sock * ssk)623b8be15d1SPaolo Abeni static void mptcp_dss_corruption(struct mptcp_sock *msk, struct sock *ssk)
624b8be15d1SPaolo Abeni {
625b8be15d1SPaolo Abeni 	if (READ_ONCE(msk->allow_infinite_fallback)) {
626b8be15d1SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk),
627b8be15d1SPaolo Abeni 				MPTCP_MIB_DSSCORRUPTIONFALLBACK);
628b8be15d1SPaolo Abeni 		mptcp_do_fallback(ssk);
629b8be15d1SPaolo Abeni 	} else {
630b8be15d1SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSCORRUPTIONRESET);
631b8be15d1SPaolo Abeni 		mptcp_subflow_reset(ssk);
632b8be15d1SPaolo Abeni 	}
633b8be15d1SPaolo Abeni }
634b8be15d1SPaolo Abeni 
__mptcp_move_skbs_from_subflow(struct mptcp_sock * msk,struct sock * ssk,unsigned int * bytes)6356771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
6366771bfd9SFlorian Westphal 					   struct sock *ssk,
6376771bfd9SFlorian Westphal 					   unsigned int *bytes)
6386771bfd9SFlorian Westphal {
6396771bfd9SFlorian Westphal 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
640600911ffSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
6416771bfd9SFlorian Westphal 	unsigned int moved = 0;
6426771bfd9SFlorian Westphal 	bool more_data_avail;
6436771bfd9SFlorian Westphal 	struct tcp_sock *tp;
6446771bfd9SFlorian Westphal 	bool done = false;
64513c7ba0cSFlorian Westphal 	int sk_rbuf;
64613c7ba0cSFlorian Westphal 
64713c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
64813c7ba0cSFlorian Westphal 
64913c7ba0cSFlorian Westphal 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
65013c7ba0cSFlorian Westphal 		int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
65113c7ba0cSFlorian Westphal 
65213c7ba0cSFlorian Westphal 		if (unlikely(ssk_rbuf > sk_rbuf)) {
65313c7ba0cSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
65413c7ba0cSFlorian Westphal 			sk_rbuf = ssk_rbuf;
65513c7ba0cSFlorian Westphal 		}
65613c7ba0cSFlorian Westphal 	}
657600911ffSFlorian Westphal 
6583d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p ssk=%p\n", msk, ssk);
6596771bfd9SFlorian Westphal 	tp = tcp_sk(ssk);
6606771bfd9SFlorian Westphal 	do {
6616771bfd9SFlorian Westphal 		u32 map_remaining, offset;
6626771bfd9SFlorian Westphal 		u32 seq = tp->copied_seq;
6636771bfd9SFlorian Westphal 		struct sk_buff *skb;
6646771bfd9SFlorian Westphal 		bool fin;
6656771bfd9SFlorian Westphal 
6666771bfd9SFlorian Westphal 		/* try to move as much data as available */
6676771bfd9SFlorian Westphal 		map_remaining = subflow->map_data_len -
6686771bfd9SFlorian Westphal 				mptcp_subflow_get_map_offset(subflow);
6696771bfd9SFlorian Westphal 
6706771bfd9SFlorian Westphal 		skb = skb_peek(&ssk->sk_receive_queue);
671d9fb8c50SPaolo Abeni 		if (!skb) {
672d89e3ed7SPaolo Abeni 			/* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
673d89e3ed7SPaolo Abeni 			 * a different CPU can have already processed the pending
674d89e3ed7SPaolo Abeni 			 * data, stop here or we can enter an infinite loop
675d9fb8c50SPaolo Abeni 			 */
676d9fb8c50SPaolo Abeni 			if (!moved)
677d9fb8c50SPaolo Abeni 				done = true;
6786771bfd9SFlorian Westphal 			break;
679d9fb8c50SPaolo Abeni 		}
6806771bfd9SFlorian Westphal 
681e1ff9e82SDavide Caratti 		if (__mptcp_check_fallback(msk)) {
682d89e3ed7SPaolo Abeni 			/* Under fallback skbs have no MPTCP extension and TCP could
683d89e3ed7SPaolo Abeni 			 * collapse them between the dummy map creation and the
684d89e3ed7SPaolo Abeni 			 * current dequeue. Be sure to adjust the map size.
685e1ff9e82SDavide Caratti 			 */
686e1ff9e82SDavide Caratti 			map_remaining = skb->len;
687e1ff9e82SDavide Caratti 			subflow->map_data_len = skb->len;
688e1ff9e82SDavide Caratti 		}
689e1ff9e82SDavide Caratti 
6906771bfd9SFlorian Westphal 		offset = seq - TCP_SKB_CB(skb)->seq;
6916771bfd9SFlorian Westphal 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
6926771bfd9SFlorian Westphal 		if (fin) {
6936771bfd9SFlorian Westphal 			done = true;
6946771bfd9SFlorian Westphal 			seq++;
6956771bfd9SFlorian Westphal 		}
6966771bfd9SFlorian Westphal 
6976771bfd9SFlorian Westphal 		if (offset < skb->len) {
6986771bfd9SFlorian Westphal 			size_t len = skb->len - offset;
6996771bfd9SFlorian Westphal 
7006771bfd9SFlorian Westphal 			if (tp->urg_data)
7016771bfd9SFlorian Westphal 				done = true;
7026771bfd9SFlorian Westphal 
703ab174ad8SPaolo Abeni 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
7046771bfd9SFlorian Westphal 				moved += len;
705ab174ad8SPaolo Abeni 			seq += len;
7066771bfd9SFlorian Westphal 
707b8be15d1SPaolo Abeni 			if (unlikely(map_remaining < len)) {
708b8be15d1SPaolo Abeni 				DEBUG_NET_WARN_ON_ONCE(1);
709b8be15d1SPaolo Abeni 				mptcp_dss_corruption(msk, ssk);
710b8be15d1SPaolo Abeni 			}
7116771bfd9SFlorian Westphal 		} else {
712b8be15d1SPaolo Abeni 			if (unlikely(!fin)) {
713b8be15d1SPaolo Abeni 				DEBUG_NET_WARN_ON_ONCE(1);
714b8be15d1SPaolo Abeni 				mptcp_dss_corruption(msk, ssk);
715b8be15d1SPaolo Abeni 			}
716b8be15d1SPaolo Abeni 
7176771bfd9SFlorian Westphal 			sk_eat_skb(ssk, skb);
7186771bfd9SFlorian Westphal 			done = true;
7196771bfd9SFlorian Westphal 		}
7206771bfd9SFlorian Westphal 
7216771bfd9SFlorian Westphal 		WRITE_ONCE(tp->copied_seq, seq);
7226771bfd9SFlorian Westphal 		more_data_avail = mptcp_subflow_data_available(ssk);
723600911ffSFlorian Westphal 
72413c7ba0cSFlorian Westphal 		if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
725600911ffSFlorian Westphal 			done = true;
726600911ffSFlorian Westphal 			break;
727600911ffSFlorian Westphal 		}
7286771bfd9SFlorian Westphal 	} while (more_data_avail);
7296771bfd9SFlorian Westphal 
7306719331cSPaolo Abeni 	*bytes += moved;
7316771bfd9SFlorian Westphal 	return done;
7326771bfd9SFlorian Westphal }
7336771bfd9SFlorian Westphal 
__mptcp_ofo_queue(struct mptcp_sock * msk)73487952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
735ab174ad8SPaolo Abeni {
736ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
737ab174ad8SPaolo Abeni 	struct sk_buff *skb, *tail;
738ab174ad8SPaolo Abeni 	bool moved = false;
739ab174ad8SPaolo Abeni 	struct rb_node *p;
740ab174ad8SPaolo Abeni 	u64 end_seq;
741ab174ad8SPaolo Abeni 
742ab174ad8SPaolo Abeni 	p = rb_first(&msk->out_of_order_queue);
7433d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
744ab174ad8SPaolo Abeni 	while (p) {
745ab174ad8SPaolo Abeni 		skb = rb_to_skb(p);
746ab174ad8SPaolo Abeni 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
747ab174ad8SPaolo Abeni 			break;
748ab174ad8SPaolo Abeni 
749ab174ad8SPaolo Abeni 		p = rb_next(p);
750ab174ad8SPaolo Abeni 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
751ab174ad8SPaolo Abeni 
752ab174ad8SPaolo Abeni 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
753ab174ad8SPaolo Abeni 				      msk->ack_seq))) {
754ab174ad8SPaolo Abeni 			mptcp_drop(sk, skb);
75506242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
756ab174ad8SPaolo Abeni 			continue;
757ab174ad8SPaolo Abeni 		}
758ab174ad8SPaolo Abeni 
759ab174ad8SPaolo Abeni 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
760ab174ad8SPaolo Abeni 		tail = skb_peek_tail(&sk->sk_receive_queue);
761ab174ad8SPaolo Abeni 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
762ab174ad8SPaolo Abeni 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
763ab174ad8SPaolo Abeni 
764ab174ad8SPaolo Abeni 			/* skip overlapping data, if any */
7653d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
76606242e44SPaolo Abeni 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
76706242e44SPaolo Abeni 				 delta);
768ab174ad8SPaolo Abeni 			MPTCP_SKB_CB(skb)->offset += delta;
7692c9e7765SFlorian Westphal 			MPTCP_SKB_CB(skb)->map_seq += delta;
770ab174ad8SPaolo Abeni 			__skb_queue_tail(&sk->sk_receive_queue, skb);
771ab174ad8SPaolo Abeni 		}
77238967f42SPaolo Abeni 		msk->bytes_received += end_seq - msk->ack_seq;
773ab174ad8SPaolo Abeni 		msk->ack_seq = end_seq;
774ab174ad8SPaolo Abeni 		moved = true;
775ab174ad8SPaolo Abeni 	}
776ab174ad8SPaolo Abeni 	return moved;
777ab174ad8SPaolo Abeni }
778ab174ad8SPaolo Abeni 
__mptcp_subflow_error_report(struct sock * sk,struct sock * ssk)7799f1a9881SPaolo Abeni static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
780d5fbeff1SPaolo Abeni {
781d5fbeff1SPaolo Abeni 	int err = sock_error(ssk);
782d5fbeff1SPaolo Abeni 	int ssk_state;
783d5fbeff1SPaolo Abeni 
784d5fbeff1SPaolo Abeni 	if (!err)
7859f1a9881SPaolo Abeni 		return false;
786d5fbeff1SPaolo Abeni 
787d5fbeff1SPaolo Abeni 	/* only propagate errors on fallen-back sockets or
788d5fbeff1SPaolo Abeni 	 * on MPC connect
789d5fbeff1SPaolo Abeni 	 */
7909f1a9881SPaolo Abeni 	if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
7919f1a9881SPaolo Abeni 		return false;
792d5fbeff1SPaolo Abeni 
793d5fbeff1SPaolo Abeni 	/* We need to propagate only transition to CLOSE state.
794d5fbeff1SPaolo Abeni 	 * Orphaned socket will see such state change via
795d5fbeff1SPaolo Abeni 	 * subflow_sched_work_if_closed() and that path will properly
796d5fbeff1SPaolo Abeni 	 * destroy the msk as needed.
797d5fbeff1SPaolo Abeni 	 */
798d5fbeff1SPaolo Abeni 	ssk_state = inet_sk_state_load(ssk);
799d5fbeff1SPaolo Abeni 	if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
800d52b3c2bSGeliang Tang 		mptcp_set_state(sk, ssk_state);
801d5fbeff1SPaolo Abeni 	WRITE_ONCE(sk->sk_err, -err);
802d5fbeff1SPaolo Abeni 
803d5fbeff1SPaolo Abeni 	/* This barrier is coupled with smp_rmb() in mptcp_poll() */
804d5fbeff1SPaolo Abeni 	smp_wmb();
805d5fbeff1SPaolo Abeni 	sk_error_report(sk);
8069f1a9881SPaolo Abeni 	return true;
807d5fbeff1SPaolo Abeni }
8089f1a9881SPaolo Abeni 
__mptcp_error_report(struct sock * sk)8099f1a9881SPaolo Abeni void __mptcp_error_report(struct sock *sk)
8109f1a9881SPaolo Abeni {
8119f1a9881SPaolo Abeni 	struct mptcp_subflow_context *subflow;
8129f1a9881SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
8139f1a9881SPaolo Abeni 
8149f1a9881SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow)
8159f1a9881SPaolo Abeni 		if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
8169f1a9881SPaolo Abeni 			break;
817d5fbeff1SPaolo Abeni }
818d5fbeff1SPaolo Abeni 
8192e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket.  If its already
8202e52213cSFlorian Westphal  * owned, we need to defer to the work queue to avoid ABBA deadlock.
8212e52213cSFlorian Westphal  */
move_skbs_to_msk(struct mptcp_sock * msk,struct sock * ssk)82299d1055cSPaolo Abeni static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
8232e52213cSFlorian Westphal {
8242e52213cSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
8252e52213cSFlorian Westphal 	unsigned int moved = 0;
8262e52213cSFlorian Westphal 
8272e52213cSFlorian Westphal 	__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
82887952603SPaolo Abeni 	__mptcp_ofo_queue(msk);
829499ada50SPaolo Abeni 	if (unlikely(ssk->sk_err)) {
830499ada50SPaolo Abeni 		if (!sock_owned_by_user(sk))
831499ada50SPaolo Abeni 			__mptcp_error_report(sk);
832499ada50SPaolo Abeni 		else
833e9d09bacSPaolo Abeni 			__set_bit(MPTCP_ERROR_REPORT,  &msk->cb_flags);
834499ada50SPaolo Abeni 	}
835ab174ad8SPaolo Abeni 
836ab174ad8SPaolo Abeni 	/* If the moves have caught up with the DATA_FIN sequence number
837ab174ad8SPaolo Abeni 	 * it's time to ack the DATA_FIN and change socket state, but
838ab174ad8SPaolo Abeni 	 * this is not a good place to change state. Let the workqueue
839ab174ad8SPaolo Abeni 	 * do it.
840ab174ad8SPaolo Abeni 	 */
841ba8f48f7SPaolo Abeni 	if (mptcp_pending_data_fin(sk, NULL))
842ba8f48f7SPaolo Abeni 		mptcp_schedule_work(sk);
84399d1055cSPaolo Abeni 	return moved > 0;
8442e52213cSFlorian Westphal }
8452e52213cSFlorian Westphal 
mptcp_data_ready(struct sock * sk,struct sock * ssk)8462e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk)
847101f6f85SFlorian Westphal {
8486719331cSPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
849101f6f85SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
85013c7ba0cSFlorian Westphal 	int sk_rbuf, ssk_rbuf;
851101f6f85SFlorian Westphal 
852d7b1bfd0SPaolo Abeni 	/* The peer can send data while we are shutting down this
853d7b1bfd0SPaolo Abeni 	 * subflow at msk destruction time, but we must avoid enqueuing
854d7b1bfd0SPaolo Abeni 	 * more data to the msk receive queue
855d7b1bfd0SPaolo Abeni 	 */
856d7b1bfd0SPaolo Abeni 	if (unlikely(subflow->disposable))
857d7b1bfd0SPaolo Abeni 		return;
858d7b1bfd0SPaolo Abeni 
85913c7ba0cSFlorian Westphal 	ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
86013c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
86113c7ba0cSFlorian Westphal 	if (unlikely(ssk_rbuf > sk_rbuf))
86213c7ba0cSFlorian Westphal 		sk_rbuf = ssk_rbuf;
86313c7ba0cSFlorian Westphal 
86499d1055cSPaolo Abeni 	/* over limit? can't append more skbs to msk, Also, no need to wake-up*/
86519bf3292SPaolo Abeni 	if (__mptcp_rmem(sk) > sk_rbuf)
86699d1055cSPaolo Abeni 		return;
8672e52213cSFlorian Westphal 
86899d1055cSPaolo Abeni 	/* Wake-up the reader only for in-sequence data */
86999d1055cSPaolo Abeni 	mptcp_data_lock(sk);
870124a4885SPaolo Abeni 	if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
871101f6f85SFlorian Westphal 		sk->sk_data_ready(sk);
87299d1055cSPaolo Abeni 	mptcp_data_unlock(sk);
87399d1055cSPaolo Abeni }
874101f6f85SFlorian Westphal 
mptcp_subflow_joined(struct mptcp_sock * msk,struct sock * ssk)87555b47ca7SPaolo Abeni static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
87655b47ca7SPaolo Abeni {
87755b47ca7SPaolo Abeni 	mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
87855b47ca7SPaolo Abeni 	WRITE_ONCE(msk->allow_infinite_fallback, false);
87955b47ca7SPaolo Abeni 	mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
88055b47ca7SPaolo Abeni }
88155b47ca7SPaolo Abeni 
__mptcp_finish_join(struct mptcp_sock * msk,struct sock * ssk)8823e501490SPaolo Abeni static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
883ec3edaa7SPeter Krystad {
8843e501490SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
8855cf92bbaSPaolo Abeni 
8863e501490SPaolo Abeni 	if (sk->sk_state != TCP_ESTABLISHED)
88778962489SFlorian Westphal 		return false;
888ec3edaa7SPeter Krystad 
8893e501490SPaolo Abeni 	/* attach to msk socket only after we are sure we will deal with it
8903e501490SPaolo Abeni 	 * at close time
8913e501490SPaolo Abeni 	 */
8923e501490SPaolo Abeni 	if (sk->sk_socket && !ssk->sk_socket)
8933e501490SPaolo Abeni 		mptcp_sock_graft(ssk, sk->sk_socket);
89478962489SFlorian Westphal 
8956f06b4d4SPaolo Abeni 	mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
8963e501490SPaolo Abeni 	mptcp_sockopt_sync_locked(msk, ssk);
89755b47ca7SPaolo Abeni 	mptcp_subflow_joined(msk, ssk);
89827e5ccc2SPaolo Abeni 	mptcp_stop_tout_timer(sk);
89934c7757aSPaolo Abeni 	__mptcp_propagate_sndbuf(sk, ssk);
9003e501490SPaolo Abeni 	return true;
90178962489SFlorian Westphal }
90278962489SFlorian Westphal 
__mptcp_flush_join_list(struct sock * sk,struct list_head * join_list)90356a666c4SPaolo Abeni static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
90478962489SFlorian Westphal {
9053e501490SPaolo Abeni 	struct mptcp_subflow_context *tmp, *subflow;
9063e501490SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
90778962489SFlorian Westphal 
90856a666c4SPaolo Abeni 	list_for_each_entry_safe(subflow, tmp, join_list, node) {
9093e501490SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
9103e501490SPaolo Abeni 		bool slow = lock_sock_fast(ssk);
9113e501490SPaolo Abeni 
9123e501490SPaolo Abeni 		list_move_tail(&subflow->node, &msk->conn_list);
9133e501490SPaolo Abeni 		if (!__mptcp_finish_join(msk, ssk))
9143e501490SPaolo Abeni 			mptcp_subflow_reset(ssk);
9153e501490SPaolo Abeni 		unlock_sock_fast(ssk, slow);
91678962489SFlorian Westphal 	}
917ec3edaa7SPeter Krystad }
918ec3edaa7SPeter Krystad 
mptcp_rtx_timer_pending(struct sock * sk)919f6909dc1SPaolo Abeni static bool mptcp_rtx_timer_pending(struct sock *sk)
920b51f9b80SPaolo Abeni {
921b51f9b80SPaolo Abeni 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
922b51f9b80SPaolo Abeni }
923b51f9b80SPaolo Abeni 
mptcp_reset_rtx_timer(struct sock * sk)924f6909dc1SPaolo Abeni static void mptcp_reset_rtx_timer(struct sock *sk)
925b51f9b80SPaolo Abeni {
926b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(sk);
927b51f9b80SPaolo Abeni 	unsigned long tout;
928b51f9b80SPaolo Abeni 
929e16163b6SPaolo Abeni 	/* prevent rescheduling on close */
930e16163b6SPaolo Abeni 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
931e16163b6SPaolo Abeni 		return;
932e16163b6SPaolo Abeni 
93333d41c9cSPaolo Abeni 	tout = mptcp_sk(sk)->timer_ival;
934b51f9b80SPaolo Abeni 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
935b51f9b80SPaolo Abeni }
936b51f9b80SPaolo Abeni 
mptcp_schedule_work(struct sock * sk)937ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk)
938ba8f48f7SPaolo Abeni {
939ba8f48f7SPaolo Abeni 	if (inet_sk_state_load(sk) != TCP_CLOSE &&
940ba8f48f7SPaolo Abeni 	    schedule_work(&mptcp_sk(sk)->work)) {
941ba8f48f7SPaolo Abeni 		/* each subflow already holds a reference to the sk, and the
942ba8f48f7SPaolo Abeni 		 * workqueue is invoked by a subflow, so sk can't go away here.
943ba8f48f7SPaolo Abeni 		 */
944ba8f48f7SPaolo Abeni 		sock_hold(sk);
945ba8f48f7SPaolo Abeni 		return true;
946ba8f48f7SPaolo Abeni 	}
947ba8f48f7SPaolo Abeni 	return false;
948ba8f48f7SPaolo Abeni }
949ba8f48f7SPaolo Abeni 
mptcp_subflow_recv_lookup(const struct mptcp_sock * msk)9507a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
9517a6a6cbcSPaolo Abeni {
9527a6a6cbcSPaolo Abeni 	struct mptcp_subflow_context *subflow;
9537a6a6cbcSPaolo Abeni 
954109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
9557a6a6cbcSPaolo Abeni 
9567a6a6cbcSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
95799d1055cSPaolo Abeni 		if (READ_ONCE(subflow->data_avail))
9587a6a6cbcSPaolo Abeni 			return mptcp_subflow_tcp_sock(subflow);
9597a6a6cbcSPaolo Abeni 	}
9607a6a6cbcSPaolo Abeni 
9617a6a6cbcSPaolo Abeni 	return NULL;
9627a6a6cbcSPaolo Abeni }
9637a6a6cbcSPaolo Abeni 
mptcp_skb_can_collapse_to(u64 write_seq,const struct sk_buff * skb,const struct mptcp_ext * mpext)9643f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq,
96557040755SPaolo Abeni 				      const struct sk_buff *skb,
96657040755SPaolo Abeni 				      const struct mptcp_ext *mpext)
9676d0060f6SMat Martineau {
96857040755SPaolo Abeni 	if (!tcp_skb_can_collapse_to(skb))
96957040755SPaolo Abeni 		return false;
97057040755SPaolo Abeni 
9715a369ca6SPaolo Abeni 	/* can collapse only if MPTCP level sequence is in order and this
9725a369ca6SPaolo Abeni 	 * mapping has not been xmitted yet
9735a369ca6SPaolo Abeni 	 */
9745a369ca6SPaolo Abeni 	return mpext && mpext->data_seq + mpext->data_len == write_seq &&
9755a369ca6SPaolo Abeni 	       !mpext->frozen;
97657040755SPaolo Abeni }
97757040755SPaolo Abeni 
97829249eacSPaolo Abeni /* we can append data to the given data frag if:
97929249eacSPaolo Abeni  * - there is space available in the backing page_frag
98029249eacSPaolo Abeni  * - the data frag tail matches the current page_frag free offset
98129249eacSPaolo Abeni  * - the data frag end sequence number matches the current write seq
98229249eacSPaolo Abeni  */
mptcp_frag_can_collapse_to(const struct mptcp_sock * msk,const struct page_frag * pfrag,const struct mptcp_data_frag * df)98318b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
98418b683bfSPaolo Abeni 				       const struct page_frag *pfrag,
98518b683bfSPaolo Abeni 				       const struct mptcp_data_frag *df)
98618b683bfSPaolo Abeni {
98718b683bfSPaolo Abeni 	return df && pfrag->page == df->page &&
988d9ca1de8SPaolo Abeni 		pfrag->size - pfrag->offset > 0 &&
98929249eacSPaolo Abeni 		pfrag->offset == (df->offset + df->data_len) &&
99018b683bfSPaolo Abeni 		df->data_seq + df->data_len == msk->write_seq;
99118b683bfSPaolo Abeni }
99218b683bfSPaolo Abeni 
dfrag_uncharge(struct sock * sk,int len)993d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len)
99418b683bfSPaolo Abeni {
995d027236cSPaolo Abeni 	sk_mem_uncharge(sk, len);
9967948f6ccSFlorian Westphal 	sk_wmem_queued_add(sk, -len);
997d027236cSPaolo Abeni }
998d027236cSPaolo Abeni 
dfrag_clear(struct sock * sk,struct mptcp_data_frag * dfrag)999d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
1000d027236cSPaolo Abeni {
1001d027236cSPaolo Abeni 	int len = dfrag->data_len + dfrag->overhead;
1002d027236cSPaolo Abeni 
100318b683bfSPaolo Abeni 	list_del(&dfrag->list);
1004d027236cSPaolo Abeni 	dfrag_uncharge(sk, len);
100518b683bfSPaolo Abeni 	put_page(dfrag->page);
100618b683bfSPaolo Abeni }
100718b683bfSPaolo Abeni 
__mptcp_clean_una(struct sock * sk)10086e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk)
100918b683bfSPaolo Abeni {
101018b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
101118b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
1012e1ff9e82SDavide Caratti 	u64 snd_una;
1013e1ff9e82SDavide Caratti 
10147439d687SPaolo Abeni 	snd_una = msk->snd_una;
101518b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
101618b683bfSPaolo Abeni 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
101718b683bfSPaolo Abeni 			break;
101818b683bfSPaolo Abeni 
10191e1d9d6fSPaolo Abeni 		if (unlikely(dfrag == msk->first_pending)) {
10201e1d9d6fSPaolo Abeni 			/* in recovery mode can see ack after the current snd head */
10211e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(!msk->recovery))
1022d9ca1de8SPaolo Abeni 				break;
10231e1d9d6fSPaolo Abeni 
10241e1d9d6fSPaolo Abeni 			WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
10251e1d9d6fSPaolo Abeni 		}
10261e1d9d6fSPaolo Abeni 
1027d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
1028d027236cSPaolo Abeni 	}
1029d027236cSPaolo Abeni 
10307948f6ccSFlorian Westphal 	dfrag = mptcp_rtx_head(sk);
10317948f6ccSFlorian Westphal 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
103253eb4c38SPaolo Abeni 		u64 delta = snd_una - dfrag->data_seq;
103353eb4c38SPaolo Abeni 
10341e1d9d6fSPaolo Abeni 		/* prevent wrap around in recovery mode */
10351e1d9d6fSPaolo Abeni 		if (unlikely(delta > dfrag->already_sent)) {
10361e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(!msk->recovery))
103753eb4c38SPaolo Abeni 				goto out;
10381e1d9d6fSPaolo Abeni 			if (WARN_ON_ONCE(delta > dfrag->data_len))
10391e1d9d6fSPaolo Abeni 				goto out;
10401e1d9d6fSPaolo Abeni 			dfrag->already_sent += delta - dfrag->already_sent;
10411e1d9d6fSPaolo Abeni 		}
10427948f6ccSFlorian Westphal 
10437948f6ccSFlorian Westphal 		dfrag->data_seq += delta;
104453eb4c38SPaolo Abeni 		dfrag->offset += delta;
10457948f6ccSFlorian Westphal 		dfrag->data_len -= delta;
1046d9ca1de8SPaolo Abeni 		dfrag->already_sent -= delta;
10477948f6ccSFlorian Westphal 
10487948f6ccSFlorian Westphal 		dfrag_uncharge(sk, delta);
10497948f6ccSFlorian Westphal 	}
10507948f6ccSFlorian Westphal 
10511e1d9d6fSPaolo Abeni 	/* all retransmitted data acked, recovery completed */
10521e1d9d6fSPaolo Abeni 	if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
10531e1d9d6fSPaolo Abeni 		msk->recovery = false;
10541e1d9d6fSPaolo Abeni 
105553eb4c38SPaolo Abeni out:
10563241a9c0SFlorian Westphal 	if (snd_una == READ_ONCE(msk->snd_nxt) &&
10573241a9c0SFlorian Westphal 	    snd_una == READ_ONCE(msk->write_seq)) {
1058f6909dc1SPaolo Abeni 		if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1059f6909dc1SPaolo Abeni 			mptcp_stop_rtx_timer(sk);
10606e628cd3SPaolo Abeni 	} else {
1061f6909dc1SPaolo Abeni 		mptcp_reset_rtx_timer(sk);
10626e628cd3SPaolo Abeni 	}
10636e628cd3SPaolo Abeni }
10646e628cd3SPaolo Abeni 
__mptcp_clean_una_wakeup(struct sock * sk)1065417789dfSPaolo Abeni static void __mptcp_clean_una_wakeup(struct sock *sk)
1066417789dfSPaolo Abeni {
1067765ff425SPaolo Abeni 	lockdep_assert_held_once(&sk->sk_lock.slock);
1068765ff425SPaolo Abeni 
1069417789dfSPaolo Abeni 	__mptcp_clean_una(sk);
1070417789dfSPaolo Abeni 	mptcp_write_space(sk);
1071417789dfSPaolo Abeni }
1072417789dfSPaolo Abeni 
mptcp_clean_una_wakeup(struct sock * sk)1073b5941f06SPaolo Abeni static void mptcp_clean_una_wakeup(struct sock *sk)
1074b5941f06SPaolo Abeni {
1075b5941f06SPaolo Abeni 	mptcp_data_lock(sk);
1076b5941f06SPaolo Abeni 	__mptcp_clean_una_wakeup(sk);
1077b5941f06SPaolo Abeni 	mptcp_data_unlock(sk);
1078b5941f06SPaolo Abeni }
1079b5941f06SPaolo Abeni 
mptcp_enter_memory_pressure(struct sock * sk)1080724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk)
108118b683bfSPaolo Abeni {
1082d9ca1de8SPaolo Abeni 	struct mptcp_subflow_context *subflow;
1083d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1084d9ca1de8SPaolo Abeni 	bool first = true;
1085d9ca1de8SPaolo Abeni 
1086d9ca1de8SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1087d9ca1de8SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1088d9ca1de8SPaolo Abeni 
1089d9ca1de8SPaolo Abeni 		if (first)
1090d9ca1de8SPaolo Abeni 			tcp_enter_memory_pressure(ssk);
1091d9ca1de8SPaolo Abeni 		sk_stream_moderate_sndbuf(ssk);
109234c7757aSPaolo Abeni 
1093d9ca1de8SPaolo Abeni 		first = false;
1094d9ca1de8SPaolo Abeni 	}
109534c7757aSPaolo Abeni 	__mptcp_sync_sndbuf(sk);
1096724cfd2eSPaolo Abeni }
1097724cfd2eSPaolo Abeni 
1098724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1099724cfd2eSPaolo Abeni  * data
1100724cfd2eSPaolo Abeni  */
mptcp_page_frag_refill(struct sock * sk,struct page_frag * pfrag)1101724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1102724cfd2eSPaolo Abeni {
1103724cfd2eSPaolo Abeni 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1104724cfd2eSPaolo Abeni 					pfrag, sk->sk_allocation)))
1105724cfd2eSPaolo Abeni 		return true;
1106724cfd2eSPaolo Abeni 
1107724cfd2eSPaolo Abeni 	mptcp_enter_memory_pressure(sk);
110818b683bfSPaolo Abeni 	return false;
110918b683bfSPaolo Abeni }
111018b683bfSPaolo Abeni 
111118b683bfSPaolo Abeni static struct mptcp_data_frag *
mptcp_carve_data_frag(const struct mptcp_sock * msk,struct page_frag * pfrag,int orig_offset)111218b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
111318b683bfSPaolo Abeni 		      int orig_offset)
111418b683bfSPaolo Abeni {
111518b683bfSPaolo Abeni 	int offset = ALIGN(orig_offset, sizeof(long));
111618b683bfSPaolo Abeni 	struct mptcp_data_frag *dfrag;
111718b683bfSPaolo Abeni 
111818b683bfSPaolo Abeni 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
111918b683bfSPaolo Abeni 	dfrag->data_len = 0;
112018b683bfSPaolo Abeni 	dfrag->data_seq = msk->write_seq;
112118b683bfSPaolo Abeni 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
112218b683bfSPaolo Abeni 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1123d9ca1de8SPaolo Abeni 	dfrag->already_sent = 0;
112418b683bfSPaolo Abeni 	dfrag->page = pfrag->page;
112518b683bfSPaolo Abeni 
112618b683bfSPaolo Abeni 	return dfrag;
112718b683bfSPaolo Abeni }
112818b683bfSPaolo Abeni 
1129caf971dfSPaolo Abeni struct mptcp_sendmsg_info {
1130caf971dfSPaolo Abeni 	int mss_now;
1131caf971dfSPaolo Abeni 	int size_goal;
1132d9ca1de8SPaolo Abeni 	u16 limit;
1133d9ca1de8SPaolo Abeni 	u16 sent;
1134d9ca1de8SPaolo Abeni 	unsigned int flags;
11351094c6feSPaolo Abeni 	bool data_lock_held;
1136caf971dfSPaolo Abeni };
1137caf971dfSPaolo Abeni 
mptcp_check_allowed_size(const struct mptcp_sock * msk,struct sock * ssk,u64 data_seq,int avail_size)1138b713d006SPaolo Abeni static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1139b713d006SPaolo Abeni 				    u64 data_seq, int avail_size)
11406f8a612aSFlorian Westphal {
11416f8a612aSFlorian Westphal 	u64 window_end = mptcp_wnd_end(msk);
1142b713d006SPaolo Abeni 	u64 mptcp_snd_wnd;
11436f8a612aSFlorian Westphal 
11446f8a612aSFlorian Westphal 	if (__mptcp_check_fallback(msk))
11456f8a612aSFlorian Westphal 		return avail_size;
11466f8a612aSFlorian Westphal 
1147b713d006SPaolo Abeni 	mptcp_snd_wnd = window_end - data_seq;
1148b713d006SPaolo Abeni 	avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
11496f8a612aSFlorian Westphal 
115092be2f52SPaolo Abeni 	if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1151b713d006SPaolo Abeni 		tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
115292be2f52SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
115392be2f52SPaolo Abeni 	}
11546f8a612aSFlorian Westphal 
11556f8a612aSFlorian Westphal 	return avail_size;
11566f8a612aSFlorian Westphal }
11576f8a612aSFlorian Westphal 
__mptcp_add_ext(struct sk_buff * skb,gfp_t gfp)1158724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1159724cfd2eSPaolo Abeni {
1160724cfd2eSPaolo Abeni 	struct skb_ext *mpext = __skb_ext_alloc(gfp);
1161724cfd2eSPaolo Abeni 
1162724cfd2eSPaolo Abeni 	if (!mpext)
1163724cfd2eSPaolo Abeni 		return false;
1164724cfd2eSPaolo Abeni 	__skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1165724cfd2eSPaolo Abeni 	return true;
1166724cfd2eSPaolo Abeni }
1167724cfd2eSPaolo Abeni 
__mptcp_do_alloc_tx_skb(struct sock * sk,gfp_t gfp)11686e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1169724cfd2eSPaolo Abeni {
1170724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1171724cfd2eSPaolo Abeni 
11726e628cd3SPaolo Abeni 	skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1173724cfd2eSPaolo Abeni 	if (likely(skb)) {
11746e628cd3SPaolo Abeni 		if (likely(__mptcp_add_ext(skb, gfp))) {
1175724cfd2eSPaolo Abeni 			skb_reserve(skb, MAX_TCP_HEADER);
1176a52fe46eSEric Dumazet 			skb->ip_summed = CHECKSUM_PARTIAL;
1177f70cad10SPaolo Abeni 			INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1178724cfd2eSPaolo Abeni 			return skb;
1179724cfd2eSPaolo Abeni 		}
1180724cfd2eSPaolo Abeni 		__kfree_skb(skb);
1181724cfd2eSPaolo Abeni 	} else {
1182724cfd2eSPaolo Abeni 		mptcp_enter_memory_pressure(sk);
1183724cfd2eSPaolo Abeni 	}
1184724cfd2eSPaolo Abeni 	return NULL;
1185724cfd2eSPaolo Abeni }
1186724cfd2eSPaolo Abeni 
__mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,gfp_t gfp)1187f70cad10SPaolo Abeni static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1188724cfd2eSPaolo Abeni {
1189724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1190724cfd2eSPaolo Abeni 
11916e628cd3SPaolo Abeni 	skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1192724cfd2eSPaolo Abeni 	if (!skb)
1193f70cad10SPaolo Abeni 		return NULL;
1194724cfd2eSPaolo Abeni 
1195724cfd2eSPaolo Abeni 	if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1196f70cad10SPaolo Abeni 		tcp_skb_entail(ssk, skb);
1197f70cad10SPaolo Abeni 		return skb;
1198724cfd2eSPaolo Abeni 	}
11993ef3905aSYonglong Li 	tcp_skb_tsorted_anchor_cleanup(skb);
1200724cfd2eSPaolo Abeni 	kfree_skb(skb);
1201f70cad10SPaolo Abeni 	return NULL;
1202724cfd2eSPaolo Abeni }
1203724cfd2eSPaolo Abeni 
mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,bool data_lock_held)1204f70cad10SPaolo Abeni static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1205724cfd2eSPaolo Abeni {
12061094c6feSPaolo Abeni 	gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1207724cfd2eSPaolo Abeni 
12081094c6feSPaolo Abeni 	return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1209724cfd2eSPaolo Abeni }
1210724cfd2eSPaolo Abeni 
1211d0cc2987SGeliang Tang /* note: this always recompute the csum on the whole skb, even
1212d0cc2987SGeliang Tang  * if we just appended a single frag. More status info needed
1213d0cc2987SGeliang Tang  */
mptcp_update_data_checksum(struct sk_buff * skb,int added)1214d0cc2987SGeliang Tang static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1215d0cc2987SGeliang Tang {
1216d0cc2987SGeliang Tang 	struct mptcp_ext *mpext = mptcp_get_ext(skb);
1217d0cc2987SGeliang Tang 	__wsum csum = ~csum_unfold(mpext->csum);
1218d0cc2987SGeliang Tang 	int offset = skb->len - added;
1219d0cc2987SGeliang Tang 
1220d0cc2987SGeliang Tang 	mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1221d0cc2987SGeliang Tang }
1222d0cc2987SGeliang Tang 
mptcp_update_infinite_map(struct mptcp_sock * msk,struct sock * ssk,struct mptcp_ext * mpext)12231e39e5a3SGeliang Tang static void mptcp_update_infinite_map(struct mptcp_sock *msk,
12241e39e5a3SGeliang Tang 				      struct sock *ssk,
12251e39e5a3SGeliang Tang 				      struct mptcp_ext *mpext)
12261e39e5a3SGeliang Tang {
12271e39e5a3SGeliang Tang 	if (!mpext)
12281e39e5a3SGeliang Tang 		return;
12291e39e5a3SGeliang Tang 
12301e39e5a3SGeliang Tang 	mpext->infinite_map = 1;
12311e39e5a3SGeliang Tang 	mpext->data_len = 0;
12321e39e5a3SGeliang Tang 
1233104125b8SGeliang Tang 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
12341e39e5a3SGeliang Tang 	mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
12351e39e5a3SGeliang Tang 	pr_fallback(msk);
1236d51991e2SPaolo Abeni 	mptcp_do_fallback(ssk);
12371e39e5a3SGeliang Tang }
12381e39e5a3SGeliang Tang 
123957ced2ebSPaolo Abeni #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
124057ced2ebSPaolo Abeni 
mptcp_sendmsg_frag(struct sock * sk,struct sock * ssk,struct mptcp_data_frag * dfrag,struct mptcp_sendmsg_info * info)124157040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1242d9ca1de8SPaolo Abeni 			      struct mptcp_data_frag *dfrag,
1243caf971dfSPaolo Abeni 			      struct mptcp_sendmsg_info *info)
124457040755SPaolo Abeni {
1245d9ca1de8SPaolo Abeni 	u64 data_seq = dfrag->data_seq + info->sent;
1246f70cad10SPaolo Abeni 	int offset = dfrag->offset + info->sent;
12476d0060f6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
12486f8a612aSFlorian Westphal 	bool zero_window_probe = false;
12496d0060f6SMat Martineau 	struct mptcp_ext *mpext = NULL;
1250f70cad10SPaolo Abeni 	bool can_coalesce = false;
1251f70cad10SPaolo Abeni 	bool reuse_skb = true;
1252f70cad10SPaolo Abeni 	struct sk_buff *skb;
1253f70cad10SPaolo Abeni 	size_t copy;
1254f70cad10SPaolo Abeni 	int i;
12556d0060f6SMat Martineau 
12563d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
1257d9ca1de8SPaolo Abeni 		 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
12586d0060f6SMat Martineau 
1259f70cad10SPaolo Abeni 	if (WARN_ON_ONCE(info->sent > info->limit ||
1260f70cad10SPaolo Abeni 			 info->limit > dfrag->data_len))
1261f70cad10SPaolo Abeni 		return 0;
1262f70cad10SPaolo Abeni 
1263c886d702SPaolo Abeni 	if (unlikely(!__tcp_can_send(ssk)))
1264c886d702SPaolo Abeni 		return -EAGAIN;
1265c886d702SPaolo Abeni 
1266d9ca1de8SPaolo Abeni 	/* compute send limit */
126757ced2ebSPaolo Abeni 	if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
126857ced2ebSPaolo Abeni 		ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1269d9ca1de8SPaolo Abeni 	info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1270f70cad10SPaolo Abeni 	copy = info->size_goal;
1271f70cad10SPaolo Abeni 
127257040755SPaolo Abeni 	skb = tcp_write_queue_tail(ssk);
1273f70cad10SPaolo Abeni 	if (skb && copy > skb->len) {
127457040755SPaolo Abeni 		/* Limit the write to the size available in the
127557040755SPaolo Abeni 		 * current skb, if any, so that we create at most a new skb.
127657040755SPaolo Abeni 		 * Explicitly tells TCP internals to avoid collapsing on later
127757040755SPaolo Abeni 		 * queue management operation, to avoid breaking the ext <->
127857040755SPaolo Abeni 		 * SSN association set here
127957040755SPaolo Abeni 		 */
1280d9ca1de8SPaolo Abeni 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
12811094c6feSPaolo Abeni 		if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
128257040755SPaolo Abeni 			TCP_SKB_CB(skb)->eor = 1;
128348428b07SPaolo Abeni 			tcp_mark_push(tcp_sk(ssk), skb);
12841094c6feSPaolo Abeni 			goto alloc_skb;
12851094c6feSPaolo Abeni 		}
12861094c6feSPaolo Abeni 
1287f70cad10SPaolo Abeni 		i = skb_shinfo(skb)->nr_frags;
1288f70cad10SPaolo Abeni 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1289657b991aSKuniyuki Iwashima 		if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1290f70cad10SPaolo Abeni 			tcp_mark_push(tcp_sk(ssk), skb);
1291f70cad10SPaolo Abeni 			goto alloc_skb;
129215e6ca97SPaolo Abeni 		}
129318b683bfSPaolo Abeni 
1294f70cad10SPaolo Abeni 		copy -= skb->len;
1295f70cad10SPaolo Abeni 	} else {
12961094c6feSPaolo Abeni alloc_skb:
1297f70cad10SPaolo Abeni 		skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1298f70cad10SPaolo Abeni 		if (!skb)
1299f70cad10SPaolo Abeni 			return -ENOMEM;
1300f70cad10SPaolo Abeni 
1301f70cad10SPaolo Abeni 		i = skb_shinfo(skb)->nr_frags;
1302f70cad10SPaolo Abeni 		reuse_skb = false;
1303f70cad10SPaolo Abeni 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1304f70cad10SPaolo Abeni 	}
13051094c6feSPaolo Abeni 
13066f8a612aSFlorian Westphal 	/* Zero window and all data acked? Probe. */
1307b713d006SPaolo Abeni 	copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1308f70cad10SPaolo Abeni 	if (copy == 0) {
13097439d687SPaolo Abeni 		u64 snd_una = READ_ONCE(msk->snd_una);
13107439d687SPaolo Abeni 
131172377ab2SPaolo Abeni 		if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) {
131227728ba8SEric Dumazet 			tcp_remove_empty_skb(ssk);
13136f8a612aSFlorian Westphal 			return 0;
13146f8a612aSFlorian Westphal 		}
13156f8a612aSFlorian Westphal 
1316f70cad10SPaolo Abeni 		zero_window_probe = true;
1317f70cad10SPaolo Abeni 		data_seq = snd_una - 1;
1318f70cad10SPaolo Abeni 		copy = 1;
1319f70cad10SPaolo Abeni 	}
1320f70cad10SPaolo Abeni 
1321f70cad10SPaolo Abeni 	copy = min_t(size_t, copy, info->limit - info->sent);
1322f70cad10SPaolo Abeni 	if (!sk_wmem_schedule(ssk, copy)) {
132327728ba8SEric Dumazet 		tcp_remove_empty_skb(ssk);
1324e2223995SPaolo Abeni 		return -ENOMEM;
132535759383SFlorian Westphal 	}
132618b683bfSPaolo Abeni 
1327f70cad10SPaolo Abeni 	if (can_coalesce) {
1328f70cad10SPaolo Abeni 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1329f70cad10SPaolo Abeni 	} else {
1330f70cad10SPaolo Abeni 		get_page(dfrag->page);
1331f70cad10SPaolo Abeni 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
133257040755SPaolo Abeni 	}
133357040755SPaolo Abeni 
1334f70cad10SPaolo Abeni 	skb->len += copy;
1335f70cad10SPaolo Abeni 	skb->data_len += copy;
1336f70cad10SPaolo Abeni 	skb->truesize += copy;
1337f70cad10SPaolo Abeni 	sk_wmem_queued_add(ssk, copy);
1338f70cad10SPaolo Abeni 	sk_mem_charge(ssk, copy);
1339f70cad10SPaolo Abeni 	WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1340f70cad10SPaolo Abeni 	TCP_SKB_CB(skb)->end_seq += copy;
1341f70cad10SPaolo Abeni 	tcp_skb_pcount_set(skb, 0);
1342f70cad10SPaolo Abeni 
1343f70cad10SPaolo Abeni 	/* on skb reuse we just need to update the DSS len */
1344f70cad10SPaolo Abeni 	if (reuse_skb) {
1345f70cad10SPaolo Abeni 		TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1346f70cad10SPaolo Abeni 		mpext->data_len += copy;
1347f70cad10SPaolo Abeni 		goto out;
1348724cfd2eSPaolo Abeni 	}
13496d0060f6SMat Martineau 
13506d0060f6SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1351d9ca1de8SPaolo Abeni 	mpext->data_seq = data_seq;
13526d0060f6SMat Martineau 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1353f70cad10SPaolo Abeni 	mpext->data_len = copy;
13546d0060f6SMat Martineau 	mpext->use_map = 1;
13556d0060f6SMat Martineau 	mpext->dsn64 = 1;
13566d0060f6SMat Martineau 
13573d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
13586d0060f6SMat Martineau 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
13596d0060f6SMat Martineau 		 mpext->dsn64);
13606d0060f6SMat Martineau 
13616f8a612aSFlorian Westphal 	if (zero_window_probe) {
1362f70cad10SPaolo Abeni 		mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
13636f8a612aSFlorian Westphal 		mpext->frozen = 1;
1364d0cc2987SGeliang Tang 		if (READ_ONCE(msk->csum_enabled))
1365f70cad10SPaolo Abeni 			mptcp_update_data_checksum(skb, copy);
13666f8a612aSFlorian Westphal 		tcp_push_pending_frames(ssk);
1367d0cc2987SGeliang Tang 		return 0;
13686f8a612aSFlorian Westphal 	}
136957040755SPaolo Abeni out:
1370d0cc2987SGeliang Tang 	if (READ_ONCE(msk->csum_enabled))
1371f70cad10SPaolo Abeni 		mptcp_update_data_checksum(skb, copy);
13721e39e5a3SGeliang Tang 	if (mptcp_subflow_ctx(ssk)->send_infinite_map)
13731e39e5a3SGeliang Tang 		mptcp_update_infinite_map(msk, ssk, mpext);
13740eb4e7eeSGeliang Tang 	trace_mptcp_sendmsg_frag(mpext);
1375f70cad10SPaolo Abeni 	mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1376f70cad10SPaolo Abeni 	return copy;
13776d0060f6SMat Martineau }
13786d0060f6SMat Martineau 
1379d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
1380d5f49190SPaolo Abeni 					 sizeof(struct tcphdr) - \
1381d5f49190SPaolo Abeni 					 MAX_TCP_OPTION_SPACE - \
1382d5f49190SPaolo Abeni 					 sizeof(struct ipv6hdr) - \
1383d5f49190SPaolo Abeni 					 sizeof(struct frag_hdr))
1384d5f49190SPaolo Abeni 
1385d5f49190SPaolo Abeni struct subflow_send_info {
1386d5f49190SPaolo Abeni 	struct sock *ssk;
13873ce0852cSPaolo Abeni 	u64 linger_time;
1388d5f49190SPaolo Abeni };
1389d5f49190SPaolo Abeni 
mptcp_subflow_set_active(struct mptcp_subflow_context * subflow)1390ff5a0b42SPaolo Abeni void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1391ff5a0b42SPaolo Abeni {
1392ff5a0b42SPaolo Abeni 	if (!subflow->stale)
1393ff5a0b42SPaolo Abeni 		return;
1394ff5a0b42SPaolo Abeni 
1395ff5a0b42SPaolo Abeni 	subflow->stale = 0;
1396fc1b4e3bSPaolo Abeni 	MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1397ff5a0b42SPaolo Abeni }
1398ff5a0b42SPaolo Abeni 
mptcp_subflow_active(struct mptcp_subflow_context * subflow)1399ff5a0b42SPaolo Abeni bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1400ff5a0b42SPaolo Abeni {
1401ff5a0b42SPaolo Abeni 	if (unlikely(subflow->stale)) {
1402ff5a0b42SPaolo Abeni 		u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1403ff5a0b42SPaolo Abeni 
1404ff5a0b42SPaolo Abeni 		if (subflow->stale_rcv_tstamp == rcv_tstamp)
1405ff5a0b42SPaolo Abeni 			return false;
1406ff5a0b42SPaolo Abeni 
1407ff5a0b42SPaolo Abeni 		mptcp_subflow_set_active(subflow);
1408ff5a0b42SPaolo Abeni 	}
1409ff5a0b42SPaolo Abeni 	return __mptcp_subflow_active(subflow);
1410ff5a0b42SPaolo Abeni }
1411ff5a0b42SPaolo Abeni 
14123ce0852cSPaolo Abeni #define SSK_MODE_ACTIVE	0
14133ce0852cSPaolo Abeni #define SSK_MODE_BACKUP	1
14143ce0852cSPaolo Abeni #define SSK_MODE_MAX	2
14153ce0852cSPaolo Abeni 
141633d41c9cSPaolo Abeni /* implement the mptcp packet scheduler;
141733d41c9cSPaolo Abeni  * returns the subflow that will transmit the next DSS
141833d41c9cSPaolo Abeni  * additionally updates the rtx timeout
141933d41c9cSPaolo Abeni  */
mptcp_subflow_get_send(struct mptcp_sock * msk)142007336a87SGeliang Tang struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1421f296234cSPeter Krystad {
14223ce0852cSPaolo Abeni 	struct subflow_send_info send_info[SSK_MODE_MAX];
1423f296234cSPeter Krystad 	struct mptcp_subflow_context *subflow;
142433d41c9cSPaolo Abeni 	struct sock *sk = (struct sock *)msk;
14253ce0852cSPaolo Abeni 	u32 pace, burst, wmem;
1426d5f49190SPaolo Abeni 	int i, nr_active = 0;
1427d5f49190SPaolo Abeni 	struct sock *ssk;
14283ce0852cSPaolo Abeni 	u64 linger_time;
142933d41c9cSPaolo Abeni 	long tout = 0;
1430f296234cSPeter Krystad 
1431d5f49190SPaolo Abeni 	/* pick the subflow with the lower wmem/wspace ratio */
14323ce0852cSPaolo Abeni 	for (i = 0; i < SSK_MODE_MAX; ++i) {
1433d5f49190SPaolo Abeni 		send_info[i].ssk = NULL;
14343ce0852cSPaolo Abeni 		send_info[i].linger_time = -1;
1435d5f49190SPaolo Abeni 	}
14363ce0852cSPaolo Abeni 
1437d5f49190SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1438add243b7SMatthieu Baerts (NGI0) 		bool backup = subflow->backup || subflow->request_bkup;
1439add243b7SMatthieu Baerts (NGI0) 
1440e10a9892SGeliang Tang 		trace_mptcp_subflow_get_send(subflow);
1441d5f49190SPaolo Abeni 		ssk =  mptcp_subflow_tcp_sock(subflow);
1442d5f49190SPaolo Abeni 		if (!mptcp_subflow_active(subflow))
1443f296234cSPeter Krystad 			continue;
1444d5f49190SPaolo Abeni 
144533d41c9cSPaolo Abeni 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
1446add243b7SMatthieu Baerts (NGI0) 		nr_active += !backup;
14473ce0852cSPaolo Abeni 		pace = subflow->avg_pacing_rate;
14483ce0852cSPaolo Abeni 		if (unlikely(!pace)) {
14493ce0852cSPaolo Abeni 			/* init pacing rate from socket */
14503ce0852cSPaolo Abeni 			subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
14513ce0852cSPaolo Abeni 			pace = subflow->avg_pacing_rate;
1452d5f49190SPaolo Abeni 			if (!pace)
1453d5f49190SPaolo Abeni 				continue;
14543ce0852cSPaolo Abeni 		}
1455d5f49190SPaolo Abeni 
14563ce0852cSPaolo Abeni 		linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1457add243b7SMatthieu Baerts (NGI0) 		if (linger_time < send_info[backup].linger_time) {
1458add243b7SMatthieu Baerts (NGI0) 			send_info[backup].ssk = ssk;
1459add243b7SMatthieu Baerts (NGI0) 			send_info[backup].linger_time = linger_time;
1460d5f49190SPaolo Abeni 		}
1461f296234cSPeter Krystad 	}
146233d41c9cSPaolo Abeni 	__mptcp_set_timeout(sk, tout);
1463f296234cSPeter Krystad 
1464d5f49190SPaolo Abeni 	/* pick the best backup if no other subflow is active */
1465d5f49190SPaolo Abeni 	if (!nr_active)
14663ce0852cSPaolo Abeni 		send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1467d5f49190SPaolo Abeni 
14683ce0852cSPaolo Abeni 	/* According to the blest algorithm, to avoid HoL blocking for the
14693ce0852cSPaolo Abeni 	 * faster flow, we need to:
14703ce0852cSPaolo Abeni 	 * - estimate the faster flow linger time
14713ce0852cSPaolo Abeni 	 * - use the above to estimate the amount of byte transferred
14723ce0852cSPaolo Abeni 	 *   by the faster flow
14733ce0852cSPaolo Abeni 	 * - check that the amount of queued data is greter than the above,
14743ce0852cSPaolo Abeni 	 *   otherwise do not use the picked, slower, subflow
14753ce0852cSPaolo Abeni 	 * We select the subflow with the shorter estimated time to flush
14763ce0852cSPaolo Abeni 	 * the queued mem, which basically ensure the above. We just need
14773ce0852cSPaolo Abeni 	 * to check that subflow has a non empty cwin.
14783ce0852cSPaolo Abeni 	 */
14793ce0852cSPaolo Abeni 	ssk = send_info[SSK_MODE_ACTIVE].ssk;
1480b713d006SPaolo Abeni 	if (!ssk || !sk_stream_memory_free(ssk))
1481d5f49190SPaolo Abeni 		return NULL;
14823ce0852cSPaolo Abeni 
1483b713d006SPaolo Abeni 	burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
14843ce0852cSPaolo Abeni 	wmem = READ_ONCE(ssk->sk_wmem_queued);
1485ebc1e08fSGeliang Tang 	if (!burst)
1486b713d006SPaolo Abeni 		return ssk;
1487b713d006SPaolo Abeni 
14883ce0852cSPaolo Abeni 	subflow = mptcp_subflow_ctx(ssk);
14893ce0852cSPaolo Abeni 	subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
14903ce0852cSPaolo Abeni 					   READ_ONCE(ssk->sk_pacing_rate) * burst,
14913ce0852cSPaolo Abeni 					   burst + wmem);
14923ce0852cSPaolo Abeni 	msk->snd_burst = burst;
14933ce0852cSPaolo Abeni 	return ssk;
1494f296234cSPeter Krystad }
1495f296234cSPeter Krystad 
mptcp_push_release(struct sock * ssk,struct mptcp_sendmsg_info * info)1496b8e0def3SGeliang Tang static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1497d9ca1de8SPaolo Abeni {
1498d9ca1de8SPaolo Abeni 	tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1499d9ca1de8SPaolo Abeni 	release_sock(ssk);
1500d9ca1de8SPaolo Abeni }
1501d9ca1de8SPaolo Abeni 
mptcp_update_post_push(struct mptcp_sock * msk,struct mptcp_data_frag * dfrag,u32 sent)15020d199e43SFlorian Westphal static void mptcp_update_post_push(struct mptcp_sock *msk,
15030d199e43SFlorian Westphal 				   struct mptcp_data_frag *dfrag,
15040d199e43SFlorian Westphal 				   u32 sent)
15050d199e43SFlorian Westphal {
15060d199e43SFlorian Westphal 	u64 snd_nxt_new = dfrag->data_seq;
15070d199e43SFlorian Westphal 
15080d199e43SFlorian Westphal 	dfrag->already_sent += sent;
15090d199e43SFlorian Westphal 
15100d199e43SFlorian Westphal 	msk->snd_burst -= sent;
15110d199e43SFlorian Westphal 
15120d199e43SFlorian Westphal 	snd_nxt_new += dfrag->already_sent;
15130d199e43SFlorian Westphal 
15140d199e43SFlorian Westphal 	/* snd_nxt_new can be smaller than snd_nxt in case mptcp
15150d199e43SFlorian Westphal 	 * is recovering after a failover. In that event, this re-sends
15160d199e43SFlorian Westphal 	 * old segments.
15170d199e43SFlorian Westphal 	 *
15180d199e43SFlorian Westphal 	 * Thus compute snd_nxt_new candidate based on
15190d199e43SFlorian Westphal 	 * the dfrag->data_seq that was sent and the data
15200d199e43SFlorian Westphal 	 * that has been handed to the subflow for transmission
15210d199e43SFlorian Westphal 	 * and skip update in case it was old dfrag.
15220d199e43SFlorian Westphal 	 */
152338967f42SPaolo Abeni 	if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
152438967f42SPaolo Abeni 		msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
15250d199e43SFlorian Westphal 		msk->snd_nxt = snd_nxt_new;
15260d199e43SFlorian Westphal 	}
152738967f42SPaolo Abeni }
15280d199e43SFlorian Westphal 
mptcp_check_and_set_pending(struct sock * sk)15298b38217aSMaxim Galaganov void mptcp_check_and_set_pending(struct sock *sk)
15303241a9c0SFlorian Westphal {
1531877a72e3SPaolo Abeni 	if (mptcp_send_head(sk)) {
1532877a72e3SPaolo Abeni 		mptcp_data_lock(sk);
1533877a72e3SPaolo Abeni 		mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1534877a72e3SPaolo Abeni 		mptcp_data_unlock(sk);
1535877a72e3SPaolo Abeni 	}
15363241a9c0SFlorian Westphal }
15373241a9c0SFlorian Westphal 
__subflow_push_pending(struct sock * sk,struct sock * ssk,struct mptcp_sendmsg_info * info)1538c5b4297dSGeliang Tang static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1539c5b4297dSGeliang Tang 				  struct mptcp_sendmsg_info *info)
1540c5b4297dSGeliang Tang {
1541c5b4297dSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(sk);
1542c5b4297dSGeliang Tang 	struct mptcp_data_frag *dfrag;
1543c5b4297dSGeliang Tang 	int len, copied = 0, err = 0;
1544c5b4297dSGeliang Tang 
1545c5b4297dSGeliang Tang 	while ((dfrag = mptcp_send_head(sk))) {
1546c5b4297dSGeliang Tang 		info->sent = dfrag->already_sent;
1547c5b4297dSGeliang Tang 		info->limit = dfrag->data_len;
1548c5b4297dSGeliang Tang 		len = dfrag->data_len - dfrag->already_sent;
1549c5b4297dSGeliang Tang 		while (len > 0) {
1550c5b4297dSGeliang Tang 			int ret = 0;
1551c5b4297dSGeliang Tang 
1552c5b4297dSGeliang Tang 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1553c5b4297dSGeliang Tang 			if (ret <= 0) {
1554c5b4297dSGeliang Tang 				err = copied ? : ret;
1555c5b4297dSGeliang Tang 				goto out;
1556c5b4297dSGeliang Tang 			}
1557c5b4297dSGeliang Tang 
1558c5b4297dSGeliang Tang 			info->sent += ret;
1559c5b4297dSGeliang Tang 			copied += ret;
1560c5b4297dSGeliang Tang 			len -= ret;
1561c5b4297dSGeliang Tang 
1562c5b4297dSGeliang Tang 			mptcp_update_post_push(msk, dfrag, ret);
1563c5b4297dSGeliang Tang 		}
1564c5b4297dSGeliang Tang 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1565c5b4297dSGeliang Tang 
1566c5b4297dSGeliang Tang 		if (msk->snd_burst <= 0 ||
1567c5b4297dSGeliang Tang 		    !sk_stream_memory_free(ssk) ||
1568c5b4297dSGeliang Tang 		    !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1569c5b4297dSGeliang Tang 			err = copied;
1570c5b4297dSGeliang Tang 			goto out;
1571c5b4297dSGeliang Tang 		}
1572c5b4297dSGeliang Tang 		mptcp_set_timeout(sk);
1573c5b4297dSGeliang Tang 	}
1574c5b4297dSGeliang Tang 	err = copied;
1575c5b4297dSGeliang Tang 
1576c5b4297dSGeliang Tang out:
1577c5b4297dSGeliang Tang 	return err;
1578c5b4297dSGeliang Tang }
1579c5b4297dSGeliang Tang 
__mptcp_push_pending(struct sock * sk,unsigned int flags)1580ff5a0b42SPaolo Abeni void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1581d9ca1de8SPaolo Abeni {
1582d9ca1de8SPaolo Abeni 	struct sock *prev_ssk = NULL, *ssk = NULL;
1583d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1584d9ca1de8SPaolo Abeni 	struct mptcp_sendmsg_info info = {
1585d9ca1de8SPaolo Abeni 				.flags = flags,
1586d9ca1de8SPaolo Abeni 	};
15870522b424SGeliang Tang 	bool do_check_data_fin = false;
15880fa1b378SGeliang Tang 	int push_count = 1;
1589d9ca1de8SPaolo Abeni 
15900fa1b378SGeliang Tang 	while (mptcp_send_head(sk) && (push_count > 0)) {
15910fa1b378SGeliang Tang 		struct mptcp_subflow_context *subflow;
1592d9ca1de8SPaolo Abeni 		int ret = 0;
1593d9ca1de8SPaolo Abeni 
15940fa1b378SGeliang Tang 		if (mptcp_sched_get_send(msk))
15950fa1b378SGeliang Tang 			break;
1596d9ca1de8SPaolo Abeni 
15970fa1b378SGeliang Tang 		push_count = 0;
15980fa1b378SGeliang Tang 
15990fa1b378SGeliang Tang 		mptcp_for_each_subflow(msk, subflow) {
16000fa1b378SGeliang Tang 			if (READ_ONCE(subflow->scheduled)) {
16010fa1b378SGeliang Tang 				mptcp_subflow_set_scheduled(subflow, false);
16020fa1b378SGeliang Tang 
16030fa1b378SGeliang Tang 				prev_ssk = ssk;
16040fa1b378SGeliang Tang 				ssk = mptcp_subflow_tcp_sock(subflow);
16050fa1b378SGeliang Tang 				if (ssk != prev_ssk) {
16069758f40eSPaolo Abeni 					/* First check. If the ssk has changed since
16079758f40eSPaolo Abeni 					 * the last round, release prev_ssk
1608d9ca1de8SPaolo Abeni 					 */
16090fa1b378SGeliang Tang 					if (prev_ssk)
1610b8e0def3SGeliang Tang 						mptcp_push_release(prev_ssk, &info);
1611d9ca1de8SPaolo Abeni 
16129758f40eSPaolo Abeni 					/* Need to lock the new subflow only if different
16139758f40eSPaolo Abeni 					 * from the previous one, otherwise we are still
16149758f40eSPaolo Abeni 					 * helding the relevant lock
16159758f40eSPaolo Abeni 					 */
1616d9ca1de8SPaolo Abeni 					lock_sock(ssk);
16170fa1b378SGeliang Tang 				}
16180fa1b378SGeliang Tang 
16190fa1b378SGeliang Tang 				push_count++;
1620d9ca1de8SPaolo Abeni 
1621c5b4297dSGeliang Tang 				ret = __subflow_push_pending(sk, ssk, &info);
1622d9ca1de8SPaolo Abeni 				if (ret <= 0) {
16230fa1b378SGeliang Tang 					if (ret != -EAGAIN ||
16240fa1b378SGeliang Tang 					    (1 << ssk->sk_state) &
16250fa1b378SGeliang Tang 					     (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
16260fa1b378SGeliang Tang 						push_count--;
1627c886d702SPaolo Abeni 					continue;
1628d9ca1de8SPaolo Abeni 				}
16290522b424SGeliang Tang 				do_check_data_fin = true;
1630d9ca1de8SPaolo Abeni 			}
16310fa1b378SGeliang Tang 		}
16320fa1b378SGeliang Tang 	}
1633d9ca1de8SPaolo Abeni 
1634d9ca1de8SPaolo Abeni 	/* at this point we held the socket lock for the last subflow we used */
1635d9ca1de8SPaolo Abeni 	if (ssk)
1636b8e0def3SGeliang Tang 		mptcp_push_release(ssk, &info);
1637d9ca1de8SPaolo Abeni 
163833d41c9cSPaolo Abeni 	/* ensure the rtx timer is running */
1639f6909dc1SPaolo Abeni 	if (!mptcp_rtx_timer_pending(sk))
1640f6909dc1SPaolo Abeni 		mptcp_reset_rtx_timer(sk);
16410522b424SGeliang Tang 	if (do_check_data_fin)
164281c1d029SPaolo Abeni 		mptcp_check_send_data_fin(sk);
1643d9ca1de8SPaolo Abeni }
1644d9ca1de8SPaolo Abeni 
__mptcp_subflow_push_pending(struct sock * sk,struct sock * ssk,bool first)164573a0052aSGeliang Tang static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
16466e628cd3SPaolo Abeni {
16476e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
16481094c6feSPaolo Abeni 	struct mptcp_sendmsg_info info = {
16491094c6feSPaolo Abeni 		.data_lock_held = true,
16501094c6feSPaolo Abeni 	};
16510fa1b378SGeliang Tang 	bool keep_pushing = true;
1652b19bc294SPaolo Abeni 	struct sock *xmit_ssk;
1653c5b4297dSGeliang Tang 	int copied = 0;
16546e628cd3SPaolo Abeni 
16556e628cd3SPaolo Abeni 	info.flags = 0;
16560fa1b378SGeliang Tang 	while (mptcp_send_head(sk) && keep_pushing) {
16570fa1b378SGeliang Tang 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
16586e628cd3SPaolo Abeni 		int ret = 0;
16596e628cd3SPaolo Abeni 
166073a0052aSGeliang Tang 		/* check for a different subflow usage only after
1661b19bc294SPaolo Abeni 		 * spooling the first chunk of data
1662b19bc294SPaolo Abeni 		 */
16630fa1b378SGeliang Tang 		if (first) {
16640fa1b378SGeliang Tang 			mptcp_subflow_set_scheduled(subflow, false);
1665c5b4297dSGeliang Tang 			ret = __subflow_push_pending(sk, ssk, &info);
1666b19bc294SPaolo Abeni 			first = false;
1667c5b4297dSGeliang Tang 			if (ret <= 0)
1668c5b4297dSGeliang Tang 				break;
1669c5b4297dSGeliang Tang 			copied += ret;
16700fa1b378SGeliang Tang 			continue;
16710fa1b378SGeliang Tang 		}
16720fa1b378SGeliang Tang 
16730fa1b378SGeliang Tang 		if (mptcp_sched_get_send(msk))
16740fa1b378SGeliang Tang 			goto out;
16750fa1b378SGeliang Tang 
16760fa1b378SGeliang Tang 		if (READ_ONCE(subflow->scheduled)) {
16770fa1b378SGeliang Tang 			mptcp_subflow_set_scheduled(subflow, false);
16780fa1b378SGeliang Tang 			ret = __subflow_push_pending(sk, ssk, &info);
16790fa1b378SGeliang Tang 			if (ret <= 0)
16800fa1b378SGeliang Tang 				keep_pushing = false;
16810fa1b378SGeliang Tang 			copied += ret;
16820fa1b378SGeliang Tang 		}
16830fa1b378SGeliang Tang 
16840fa1b378SGeliang Tang 		mptcp_for_each_subflow(msk, subflow) {
16850fa1b378SGeliang Tang 			if (READ_ONCE(subflow->scheduled)) {
16860fa1b378SGeliang Tang 				xmit_ssk = mptcp_subflow_tcp_sock(subflow);
16870fa1b378SGeliang Tang 				if (xmit_ssk != ssk) {
16880fa1b378SGeliang Tang 					mptcp_subflow_delegate(subflow,
16890fa1b378SGeliang Tang 							       MPTCP_DELEGATE_SEND);
16900fa1b378SGeliang Tang 					keep_pushing = false;
16910fa1b378SGeliang Tang 				}
16920fa1b378SGeliang Tang 			}
16930fa1b378SGeliang Tang 		}
16946e628cd3SPaolo Abeni 	}
16956e628cd3SPaolo Abeni 
16966e628cd3SPaolo Abeni out:
16976e628cd3SPaolo Abeni 	/* __mptcp_alloc_tx_skb could have released some wmem and we are
16986e628cd3SPaolo Abeni 	 * not going to flush it via release_sock()
16996e628cd3SPaolo Abeni 	 */
17006e628cd3SPaolo Abeni 	if (copied) {
17016e628cd3SPaolo Abeni 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
17026e628cd3SPaolo Abeni 			 info.size_goal);
1703f6909dc1SPaolo Abeni 		if (!mptcp_rtx_timer_pending(sk))
1704f6909dc1SPaolo Abeni 			mptcp_reset_rtx_timer(sk);
1705d09d818eSPaolo Abeni 
17066e628cd3SPaolo Abeni 		if (msk->snd_data_fin_enable &&
17076e628cd3SPaolo Abeni 		    msk->snd_nxt + 1 == msk->write_seq)
17086e628cd3SPaolo Abeni 			mptcp_schedule_work(sk);
17096e628cd3SPaolo Abeni 	}
17106e628cd3SPaolo Abeni }
17116e628cd3SPaolo Abeni 
mptcp_set_nospace(struct sock * sk)17125cf92bbaSPaolo Abeni static void mptcp_set_nospace(struct sock *sk)
17135cf92bbaSPaolo Abeni {
17145cf92bbaSPaolo Abeni 	/* enable autotune */
17155cf92bbaSPaolo Abeni 	set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
17165cf92bbaSPaolo Abeni 
17175cf92bbaSPaolo Abeni 	/* will be cleared on avail space */
17185cf92bbaSPaolo Abeni 	set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
17195cf92bbaSPaolo Abeni }
17205cf92bbaSPaolo Abeni 
17217d803344SPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags);
17227d803344SPaolo Abeni 
mptcp_sendmsg_fastopen(struct sock * sk,struct msghdr * msg,size_t len,int * copied_syn)1723a2702a07SPaolo Abeni static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1724fa9e5746SPaolo Abeni 				  size_t len, int *copied_syn)
1725fa9e5746SPaolo Abeni {
1726fa9e5746SPaolo Abeni 	unsigned int saved_flags = msg->msg_flags;
1727fa9e5746SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1728a2702a07SPaolo Abeni 	struct sock *ssk;
1729fa9e5746SPaolo Abeni 	int ret;
1730fa9e5746SPaolo Abeni 
1731a2702a07SPaolo Abeni 	/* on flags based fastopen the mptcp is supposed to create the
1732a2702a07SPaolo Abeni 	 * first subflow right now. Otherwise we are in the defer_connect
1733a2702a07SPaolo Abeni 	 * path, and the first subflow must be already present.
1734a2702a07SPaolo Abeni 	 * Since the defer_connect flag is cleared after the first succsful
1735a2702a07SPaolo Abeni 	 * fastopen attempt, no need to check for additional subflow status.
1736a2702a07SPaolo Abeni 	 */
1737ddb1a072SPaolo Abeni 	if (msg->msg_flags & MSG_FASTOPEN) {
17383f326a82SPaolo Abeni 		ssk = __mptcp_nmpc_sk(msk);
17393f326a82SPaolo Abeni 		if (IS_ERR(ssk))
17403f326a82SPaolo Abeni 			return PTR_ERR(ssk);
1741ddb1a072SPaolo Abeni 	}
1742a2702a07SPaolo Abeni 	if (!msk->first)
1743a2702a07SPaolo Abeni 		return -EINVAL;
1744a2702a07SPaolo Abeni 
1745a2702a07SPaolo Abeni 	ssk = msk->first;
1746a2702a07SPaolo Abeni 
1747fa9e5746SPaolo Abeni 	lock_sock(ssk);
1748fa9e5746SPaolo Abeni 	msg->msg_flags |= MSG_DONTWAIT;
17497d803344SPaolo Abeni 	msk->fastopening = 1;
1750fa9e5746SPaolo Abeni 	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
17517d803344SPaolo Abeni 	msk->fastopening = 0;
1752fa9e5746SPaolo Abeni 	msg->msg_flags = saved_flags;
1753fa9e5746SPaolo Abeni 	release_sock(ssk);
1754fa9e5746SPaolo Abeni 
1755fa9e5746SPaolo Abeni 	/* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1756fa9e5746SPaolo Abeni 	if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1757fa9e5746SPaolo Abeni 		ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1758fa9e5746SPaolo Abeni 					    msg->msg_namelen, msg->msg_flags, 1);
1759fa9e5746SPaolo Abeni 
1760fa9e5746SPaolo Abeni 		/* Keep the same behaviour of plain TCP: zero the copied bytes in
1761fa9e5746SPaolo Abeni 		 * case of any error, except timeout or signal
1762fa9e5746SPaolo Abeni 		 */
1763fa9e5746SPaolo Abeni 		if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1764fa9e5746SPaolo Abeni 			*copied_syn = 0;
17657d803344SPaolo Abeni 	} else if (ret && ret != -EINPROGRESS) {
1766c2b2ae39SPaolo Abeni 		/* The disconnect() op called by tcp_sendmsg_fastopen()/
1767c2b2ae39SPaolo Abeni 		 * __inet_stream_connect() can fail, due to looking check,
1768c2b2ae39SPaolo Abeni 		 * see mptcp_disconnect().
1769c2b2ae39SPaolo Abeni 		 * Attempt it again outside the problematic scope.
1770c2b2ae39SPaolo Abeni 		 */
1771c2b2ae39SPaolo Abeni 		if (!mptcp_disconnect(sk, 0))
1772c2b2ae39SPaolo Abeni 			sk->sk_socket->state = SS_UNCONNECTED;
1773fa9e5746SPaolo Abeni 	}
177408e39c0dSEric Dumazet 	inet_clear_bit(DEFER_CONNECT, sk);
1775fa9e5746SPaolo Abeni 
1776fa9e5746SPaolo Abeni 	return ret;
1777fa9e5746SPaolo Abeni }
1778fa9e5746SPaolo Abeni 
mptcp_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)1779f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1780f870fa0bSMat Martineau {
1781f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
178217091708SFlorian Westphal 	struct page_frag *pfrag;
17836d0060f6SMat Martineau 	size_t copied = 0;
1784caf971dfSPaolo Abeni 	int ret = 0;
17856d0060f6SMat Martineau 	long timeo;
1786f870fa0bSMat Martineau 
1787987858e5SPaolo Abeni 	/* silently ignore everything else */
17881e777f39SDmytro Shytyi 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1789987858e5SPaolo Abeni 
17906511882cSPaolo Abeni 	lock_sock(sk);
17911954b860SMat Martineau 
179208e39c0dSEric Dumazet 	if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
179308e39c0dSEric Dumazet 		     msg->msg_flags & MSG_FASTOPEN)) {
1794d98a82a6SDmytro Shytyi 		int copied_syn = 0;
1795d98a82a6SDmytro Shytyi 
1796a2702a07SPaolo Abeni 		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1797d98a82a6SDmytro Shytyi 		copied += copied_syn;
1798fa9e5746SPaolo Abeni 		if (ret == -EINPROGRESS && copied_syn > 0)
1799d98a82a6SDmytro Shytyi 			goto out;
1800fa9e5746SPaolo Abeni 		else if (ret)
180169800e51SPaolo Abeni 			goto do_error;
1802d98a82a6SDmytro Shytyi 	}
1803d98a82a6SDmytro Shytyi 
18041954b860SMat Martineau 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
18051954b860SMat Martineau 
18061954b860SMat Martineau 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
18071954b860SMat Martineau 		ret = sk_stream_wait_connect(sk, &timeo);
18081954b860SMat Martineau 		if (ret)
180969800e51SPaolo Abeni 			goto do_error;
18101954b860SMat Martineau 	}
18111954b860SMat Martineau 
181269800e51SPaolo Abeni 	ret = -EPIPE;
181369800e51SPaolo Abeni 	if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
181469800e51SPaolo Abeni 		goto do_error;
181569800e51SPaolo Abeni 
181617091708SFlorian Westphal 	pfrag = sk_page_frag(sk);
181718b683bfSPaolo Abeni 
1818d9ca1de8SPaolo Abeni 	while (msg_data_left(msg)) {
1819724cfd2eSPaolo Abeni 		int total_ts, frag_truesize = 0;
1820d9ca1de8SPaolo Abeni 		struct mptcp_data_frag *dfrag;
1821d9ca1de8SPaolo Abeni 		bool dfrag_collapsed;
1822d9ca1de8SPaolo Abeni 		size_t psize, offset;
1823d9ca1de8SPaolo Abeni 
1824d9ca1de8SPaolo Abeni 		/* reuse tail pfrag, if possible, or carve a new one from the
1825d9ca1de8SPaolo Abeni 		 * page allocator
1826fb529e62SFlorian Westphal 		 */
1827d9ca1de8SPaolo Abeni 		dfrag = mptcp_pending_tail(sk);
1828d9ca1de8SPaolo Abeni 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1829d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1830d9ca1de8SPaolo Abeni 			if (!sk_stream_memory_free(sk))
1831d9ca1de8SPaolo Abeni 				goto wait_for_memory;
18326e628cd3SPaolo Abeni 
1833d9ca1de8SPaolo Abeni 			if (!mptcp_page_frag_refill(sk, pfrag))
1834d9ca1de8SPaolo Abeni 				goto wait_for_memory;
1835d9ca1de8SPaolo Abeni 
1836d9ca1de8SPaolo Abeni 			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1837d9ca1de8SPaolo Abeni 			frag_truesize = dfrag->overhead;
1838fb529e62SFlorian Westphal 		}
1839fb529e62SFlorian Westphal 
1840d9ca1de8SPaolo Abeni 		/* we do not bound vs wspace, to allow a single packet.
1841d9ca1de8SPaolo Abeni 		 * memory accounting will prevent execessive memory usage
1842d9ca1de8SPaolo Abeni 		 * anyway
1843d9ca1de8SPaolo Abeni 		 */
1844d9ca1de8SPaolo Abeni 		offset = dfrag->offset + dfrag->data_len;
1845d9ca1de8SPaolo Abeni 		psize = pfrag->size - offset;
1846d9ca1de8SPaolo Abeni 		psize = min_t(size_t, psize, msg_data_left(msg));
1847724cfd2eSPaolo Abeni 		total_ts = psize + frag_truesize;
18488ce568edSPaolo Abeni 
18496511882cSPaolo Abeni 		if (!sk_wmem_schedule(sk, total_ts))
1850d9ca1de8SPaolo Abeni 			goto wait_for_memory;
1851d9ca1de8SPaolo Abeni 
1852d9ca1de8SPaolo Abeni 		if (copy_page_from_iter(dfrag->page, offset, psize,
1853d9ca1de8SPaolo Abeni 					&msg->msg_iter) != psize) {
1854d9ca1de8SPaolo Abeni 			ret = -EFAULT;
185569800e51SPaolo Abeni 			goto do_error;
1856d9ca1de8SPaolo Abeni 		}
1857d9ca1de8SPaolo Abeni 
1858d9ca1de8SPaolo Abeni 		/* data successfully copied into the write queue */
18595e6300e7SEric Dumazet 		sk_forward_alloc_add(sk, -total_ts);
1860d9ca1de8SPaolo Abeni 		copied += psize;
1861d9ca1de8SPaolo Abeni 		dfrag->data_len += psize;
1862d9ca1de8SPaolo Abeni 		frag_truesize += psize;
1863d9ca1de8SPaolo Abeni 		pfrag->offset += frag_truesize;
1864d9ca1de8SPaolo Abeni 		WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1865d9ca1de8SPaolo Abeni 
1866d9ca1de8SPaolo Abeni 		/* charge data on mptcp pending queue to the msk socket
1867d9ca1de8SPaolo Abeni 		 * Note: we charge such data both to sk and ssk
1868d9ca1de8SPaolo Abeni 		 */
1869d9ca1de8SPaolo Abeni 		sk_wmem_queued_add(sk, frag_truesize);
1870d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1871d9ca1de8SPaolo Abeni 			get_page(dfrag->page);
1872d9ca1de8SPaolo Abeni 			list_add_tail(&dfrag->list, &msk->rtx_queue);
1873d9ca1de8SPaolo Abeni 			if (!msk->first_pending)
1874d9ca1de8SPaolo Abeni 				WRITE_ONCE(msk->first_pending, dfrag);
1875d9ca1de8SPaolo Abeni 		}
18763d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
1877d9ca1de8SPaolo Abeni 			 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1878d9ca1de8SPaolo Abeni 			 !dfrag_collapsed);
1879d9ca1de8SPaolo Abeni 
1880d9ca1de8SPaolo Abeni 		continue;
1881d9ca1de8SPaolo Abeni 
1882d9ca1de8SPaolo Abeni wait_for_memory:
18835cf92bbaSPaolo Abeni 		mptcp_set_nospace(sk);
1884c2e6048fSPaolo Abeni 		__mptcp_push_pending(sk, msg->msg_flags);
1885f296234cSPeter Krystad 		ret = sk_stream_wait_memory(sk, &timeo);
1886f296234cSPeter Krystad 		if (ret)
188769800e51SPaolo Abeni 			goto do_error;
1888cec37a6eSPeter Krystad 	}
1889cec37a6eSPeter Krystad 
189013e16037SPaolo Abeni 	if (copied)
1891c2e6048fSPaolo Abeni 		__mptcp_push_pending(sk, msg->msg_flags);
1892da51aef5SPaolo Abeni 
18931954b860SMat Martineau out:
1894cec37a6eSPeter Krystad 	release_sock(sk);
189569800e51SPaolo Abeni 	return copied;
189669800e51SPaolo Abeni 
189769800e51SPaolo Abeni do_error:
189869800e51SPaolo Abeni 	if (copied)
189969800e51SPaolo Abeni 		goto out;
190069800e51SPaolo Abeni 
190169800e51SPaolo Abeni 	copied = sk_stream_error(sk, msg->msg_flags, ret);
190269800e51SPaolo Abeni 	goto out;
1903f870fa0bSMat Martineau }
1904f870fa0bSMat Martineau 
__mptcp_recvmsg_mskq(struct mptcp_sock * msk,struct msghdr * msg,size_t len,int flags,struct scm_timestamping_internal * tss,int * cmsg_flags)19056771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
19066771bfd9SFlorian Westphal 				struct msghdr *msg,
1907b7f653b2SFlorian Westphal 				size_t len, int flags,
1908b7f653b2SFlorian Westphal 				struct scm_timestamping_internal *tss,
1909b7f653b2SFlorian Westphal 				int *cmsg_flags)
19106771bfd9SFlorian Westphal {
1911ca4fb892SYonglong Li 	struct sk_buff *skb, *tmp;
19126771bfd9SFlorian Westphal 	int copied = 0;
19136771bfd9SFlorian Westphal 
1914ca4fb892SYonglong Li 	skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
19156771bfd9SFlorian Westphal 		u32 offset = MPTCP_SKB_CB(skb)->offset;
19166771bfd9SFlorian Westphal 		u32 data_len = skb->len - offset;
19176771bfd9SFlorian Westphal 		u32 count = min_t(size_t, len - copied, data_len);
19186771bfd9SFlorian Westphal 		int err;
19196771bfd9SFlorian Westphal 
1920d976092cSPaolo Abeni 		if (!(flags & MSG_TRUNC)) {
19216771bfd9SFlorian Westphal 			err = skb_copy_datagram_msg(skb, offset, msg, count);
19226771bfd9SFlorian Westphal 			if (unlikely(err < 0)) {
19236771bfd9SFlorian Westphal 				if (!copied)
19246771bfd9SFlorian Westphal 					return err;
19256771bfd9SFlorian Westphal 				break;
19266771bfd9SFlorian Westphal 			}
1927d976092cSPaolo Abeni 		}
19286771bfd9SFlorian Westphal 
1929b7f653b2SFlorian Westphal 		if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1930b7f653b2SFlorian Westphal 			tcp_update_recv_tstamps(skb, tss);
1931b7f653b2SFlorian Westphal 			*cmsg_flags |= MPTCP_CMSG_TS;
1932b7f653b2SFlorian Westphal 		}
1933b7f653b2SFlorian Westphal 
19346771bfd9SFlorian Westphal 		copied += count;
19356771bfd9SFlorian Westphal 
19366771bfd9SFlorian Westphal 		if (count < data_len) {
19372c9e7765SFlorian Westphal 			if (!(flags & MSG_PEEK)) {
19386771bfd9SFlorian Westphal 				MPTCP_SKB_CB(skb)->offset += count;
19392c9e7765SFlorian Westphal 				MPTCP_SKB_CB(skb)->map_seq += count;
1940124a4885SPaolo Abeni 				msk->bytes_consumed += count;
19412c9e7765SFlorian Westphal 			}
19426771bfd9SFlorian Westphal 			break;
19436771bfd9SFlorian Westphal 		}
19446771bfd9SFlorian Westphal 
1945ca4fb892SYonglong Li 		if (!(flags & MSG_PEEK)) {
194687952603SPaolo Abeni 			/* we will bulk release the skb memory later */
194787952603SPaolo Abeni 			skb->destructor = NULL;
1948ce599c51SPaolo Abeni 			WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
194987952603SPaolo Abeni 			__skb_unlink(skb, &msk->receive_queue);
19506771bfd9SFlorian Westphal 			__kfree_skb(skb);
1951124a4885SPaolo Abeni 			msk->bytes_consumed += count;
1952ca4fb892SYonglong Li 		}
19536771bfd9SFlorian Westphal 
19546771bfd9SFlorian Westphal 		if (copied >= len)
19556771bfd9SFlorian Westphal 			break;
19566771bfd9SFlorian Westphal 	}
19576771bfd9SFlorian Westphal 
19586771bfd9SFlorian Westphal 	return copied;
19596771bfd9SFlorian Westphal }
19606771bfd9SFlorian Westphal 
1961a6b118feSFlorian Westphal /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
1962a6b118feSFlorian Westphal  *
1963a6b118feSFlorian Westphal  * Only difference: Use highest rtt estimate of the subflows in use.
1964a6b118feSFlorian Westphal  */
mptcp_rcv_space_adjust(struct mptcp_sock * msk,int copied)1965a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1966a6b118feSFlorian Westphal {
1967a6b118feSFlorian Westphal 	struct mptcp_subflow_context *subflow;
1968a6b118feSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
1969b8dc6d6cSPaolo Abeni 	u8 scaling_ratio = U8_MAX;
1970a6b118feSFlorian Westphal 	u32 time, advmss = 1;
1971a6b118feSFlorian Westphal 	u64 rtt_us, mstamp;
1972a6b118feSFlorian Westphal 
1973109cdeb8SGeliang Tang 	msk_owned_by_me(msk);
1974a6b118feSFlorian Westphal 
1975a6b118feSFlorian Westphal 	if (copied <= 0)
1976a6b118feSFlorian Westphal 		return;
1977a6b118feSFlorian Westphal 
1978a2d74374SPaolo Abeni 	if (!msk->rcvspace_init)
1979a2d74374SPaolo Abeni 		mptcp_rcv_space_init(msk, msk->first);
1980a2d74374SPaolo Abeni 
1981a6b118feSFlorian Westphal 	msk->rcvq_space.copied += copied;
1982a6b118feSFlorian Westphal 
1983a6b118feSFlorian Westphal 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1984a6b118feSFlorian Westphal 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1985a6b118feSFlorian Westphal 
1986a6b118feSFlorian Westphal 	rtt_us = msk->rcvq_space.rtt_us;
1987a6b118feSFlorian Westphal 	if (rtt_us && time < (rtt_us >> 3))
1988a6b118feSFlorian Westphal 		return;
1989a6b118feSFlorian Westphal 
1990a6b118feSFlorian Westphal 	rtt_us = 0;
1991a6b118feSFlorian Westphal 	mptcp_for_each_subflow(msk, subflow) {
1992a6b118feSFlorian Westphal 		const struct tcp_sock *tp;
1993a6b118feSFlorian Westphal 		u64 sf_rtt_us;
1994a6b118feSFlorian Westphal 		u32 sf_advmss;
1995a6b118feSFlorian Westphal 
1996a6b118feSFlorian Westphal 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1997a6b118feSFlorian Westphal 
1998a6b118feSFlorian Westphal 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1999a6b118feSFlorian Westphal 		sf_advmss = READ_ONCE(tp->advmss);
2000a6b118feSFlorian Westphal 
2001a6b118feSFlorian Westphal 		rtt_us = max(sf_rtt_us, rtt_us);
2002a6b118feSFlorian Westphal 		advmss = max(sf_advmss, advmss);
2003b8dc6d6cSPaolo Abeni 		scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
2004a6b118feSFlorian Westphal 	}
2005a6b118feSFlorian Westphal 
2006a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = rtt_us;
2007b8dc6d6cSPaolo Abeni 	msk->scaling_ratio = scaling_ratio;
2008a6b118feSFlorian Westphal 	if (time < (rtt_us >> 3) || rtt_us == 0)
2009a6b118feSFlorian Westphal 		return;
2010a6b118feSFlorian Westphal 
2011a6b118feSFlorian Westphal 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
2012a6b118feSFlorian Westphal 		goto new_measure;
2013a6b118feSFlorian Westphal 
201478047648SKuniyuki Iwashima 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
2015a6b118feSFlorian Westphal 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
2016a6b118feSFlorian Westphal 		u64 rcvwin, grow;
2017b8dc6d6cSPaolo Abeni 		int rcvbuf;
2018a6b118feSFlorian Westphal 
2019a6b118feSFlorian Westphal 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
2020a6b118feSFlorian Westphal 
2021a6b118feSFlorian Westphal 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
2022a6b118feSFlorian Westphal 
2023a6b118feSFlorian Westphal 		do_div(grow, msk->rcvq_space.space);
2024a6b118feSFlorian Westphal 		rcvwin += (grow << 1);
2025a6b118feSFlorian Westphal 
2026b8dc6d6cSPaolo Abeni 		rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin),
202702739545SKuniyuki Iwashima 			       READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
2028a6b118feSFlorian Westphal 
2029a6b118feSFlorian Westphal 		if (rcvbuf > sk->sk_rcvbuf) {
2030a6b118feSFlorian Westphal 			u32 window_clamp;
2031a6b118feSFlorian Westphal 
2032b8dc6d6cSPaolo Abeni 			window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf);
2033a6b118feSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
2034a6b118feSFlorian Westphal 
2035a6b118feSFlorian Westphal 			/* Make subflows follow along.  If we do not do this, we
2036a6b118feSFlorian Westphal 			 * get drops at subflow level if skbs can't be moved to
2037a6b118feSFlorian Westphal 			 * the mptcp rx queue fast enough (announced rcv_win can
2038a6b118feSFlorian Westphal 			 * exceed ssk->sk_rcvbuf).
2039a6b118feSFlorian Westphal 			 */
2040a6b118feSFlorian Westphal 			mptcp_for_each_subflow(msk, subflow) {
2041a6b118feSFlorian Westphal 				struct sock *ssk;
2042c76c6956SPaolo Abeni 				bool slow;
2043a6b118feSFlorian Westphal 
2044a6b118feSFlorian Westphal 				ssk = mptcp_subflow_tcp_sock(subflow);
2045c76c6956SPaolo Abeni 				slow = lock_sock_fast(ssk);
2046a6b118feSFlorian Westphal 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
2047f9fef23aSEric Dumazet 				WRITE_ONCE(tcp_sk(ssk)->window_clamp, window_clamp);
2048ff825ab2SPaolo Abeni 				if (tcp_can_send_ack(ssk))
2049c76c6956SPaolo Abeni 					tcp_cleanup_rbuf(ssk, 1);
2050c76c6956SPaolo Abeni 				unlock_sock_fast(ssk, slow);
2051a6b118feSFlorian Westphal 			}
2052a6b118feSFlorian Westphal 		}
2053a6b118feSFlorian Westphal 	}
2054a6b118feSFlorian Westphal 
2055a6b118feSFlorian Westphal 	msk->rcvq_space.space = msk->rcvq_space.copied;
2056a6b118feSFlorian Westphal new_measure:
2057a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
2058a6b118feSFlorian Westphal 	msk->rcvq_space.time = mstamp;
2059a6b118feSFlorian Westphal }
2060a6b118feSFlorian Westphal 
__mptcp_update_rmem(struct sock * sk)206187952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk)
206287952603SPaolo Abeni {
206387952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
206487952603SPaolo Abeni 
206587952603SPaolo Abeni 	if (!msk->rmem_released)
206687952603SPaolo Abeni 		return;
206787952603SPaolo Abeni 
206887952603SPaolo Abeni 	atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
20696511882cSPaolo Abeni 	mptcp_rmem_uncharge(sk, msk->rmem_released);
2070ce599c51SPaolo Abeni 	WRITE_ONCE(msk->rmem_released, 0);
207187952603SPaolo Abeni }
207287952603SPaolo Abeni 
__mptcp_splice_receive_queue(struct sock * sk)207387952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk)
207487952603SPaolo Abeni {
207587952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
207687952603SPaolo Abeni 
207787952603SPaolo Abeni 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
207887952603SPaolo Abeni }
207987952603SPaolo Abeni 
__mptcp_move_skbs(struct mptcp_sock * msk)2080e3859603SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk)
20816771bfd9SFlorian Westphal {
208287952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
20836771bfd9SFlorian Westphal 	unsigned int moved = 0;
208487952603SPaolo Abeni 	bool ret, done;
2085d5f49190SPaolo Abeni 
20866771bfd9SFlorian Westphal 	do {
20876771bfd9SFlorian Westphal 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
208865f49fe7SFlorian Westphal 		bool slowpath;
20896771bfd9SFlorian Westphal 
209087952603SPaolo Abeni 		/* we can have data pending in the subflows only if the msk
209187952603SPaolo Abeni 		 * receive buffer was full at subflow_data_ready() time,
209287952603SPaolo Abeni 		 * that is an unlikely slow path.
209387952603SPaolo Abeni 		 */
209487952603SPaolo Abeni 		if (likely(!ssk))
20956771bfd9SFlorian Westphal 			break;
20966771bfd9SFlorian Westphal 
209765f49fe7SFlorian Westphal 		slowpath = lock_sock_fast(ssk);
209887952603SPaolo Abeni 		mptcp_data_lock(sk);
2099e3859603SPaolo Abeni 		__mptcp_update_rmem(sk);
21006771bfd9SFlorian Westphal 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
210187952603SPaolo Abeni 		mptcp_data_unlock(sk);
2102499ada50SPaolo Abeni 
2103499ada50SPaolo Abeni 		if (unlikely(ssk->sk_err))
2104499ada50SPaolo Abeni 			__mptcp_error_report(sk);
210565f49fe7SFlorian Westphal 		unlock_sock_fast(ssk, slowpath);
21066771bfd9SFlorian Westphal 	} while (!done);
21076771bfd9SFlorian Westphal 
210887952603SPaolo Abeni 	/* acquire the data lock only if some input data is pending */
210987952603SPaolo Abeni 	ret = moved > 0;
211087952603SPaolo Abeni 	if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
211187952603SPaolo Abeni 	    !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
211287952603SPaolo Abeni 		mptcp_data_lock(sk);
211387952603SPaolo Abeni 		__mptcp_update_rmem(sk);
211487952603SPaolo Abeni 		ret |= __mptcp_ofo_queue(msk);
211587952603SPaolo Abeni 		__mptcp_splice_receive_queue(sk);
211687952603SPaolo Abeni 		mptcp_data_unlock(sk);
2117ab174ad8SPaolo Abeni 	}
211887952603SPaolo Abeni 	if (ret)
211987952603SPaolo Abeni 		mptcp_check_data_fin((struct sock *)msk);
212087952603SPaolo Abeni 	return !skb_queue_empty(&msk->receive_queue);
21216771bfd9SFlorian Westphal }
21226771bfd9SFlorian Westphal 
mptcp_inq_hint(const struct sock * sk)21232c9e7765SFlorian Westphal static unsigned int mptcp_inq_hint(const struct sock *sk)
21242c9e7765SFlorian Westphal {
21252c9e7765SFlorian Westphal 	const struct mptcp_sock *msk = mptcp_sk(sk);
21262c9e7765SFlorian Westphal 	const struct sk_buff *skb;
21272c9e7765SFlorian Westphal 
21282c9e7765SFlorian Westphal 	skb = skb_peek(&msk->receive_queue);
21292c9e7765SFlorian Westphal 	if (skb) {
21302c9e7765SFlorian Westphal 		u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
21312c9e7765SFlorian Westphal 
21322c9e7765SFlorian Westphal 		if (hint_val >= INT_MAX)
21332c9e7765SFlorian Westphal 			return INT_MAX;
21342c9e7765SFlorian Westphal 
21352c9e7765SFlorian Westphal 		return (unsigned int)hint_val;
21362c9e7765SFlorian Westphal 	}
21372c9e7765SFlorian Westphal 
21382c9e7765SFlorian Westphal 	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
21392c9e7765SFlorian Westphal 		return 1;
21402c9e7765SFlorian Westphal 
21412c9e7765SFlorian Westphal 	return 0;
21422c9e7765SFlorian Westphal }
21432c9e7765SFlorian Westphal 
mptcp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)2144f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2145ec095263SOliver Hartkopp 			 int flags, int *addr_len)
2146f870fa0bSMat Martineau {
2147f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
2148b7f653b2SFlorian Westphal 	struct scm_timestamping_internal tss;
2149b7f653b2SFlorian Westphal 	int copied = 0, cmsg_flags = 0;
21507a6a6cbcSPaolo Abeni 	int target;
21517a6a6cbcSPaolo Abeni 	long timeo;
2152f870fa0bSMat Martineau 
2153cb9d80f4SPaolo Abeni 	/* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2154cb9d80f4SPaolo Abeni 	if (unlikely(flags & MSG_ERRQUEUE))
2155cb9d80f4SPaolo Abeni 		return inet_recv_error(sk, msg, len, addr_len);
2156cb9d80f4SPaolo Abeni 
21576511882cSPaolo Abeni 	lock_sock(sk);
2158fd897679SPaolo Abeni 	if (unlikely(sk->sk_state == TCP_LISTEN)) {
2159fd897679SPaolo Abeni 		copied = -ENOTCONN;
2160fd897679SPaolo Abeni 		goto out_err;
2161fd897679SPaolo Abeni 	}
2162fd897679SPaolo Abeni 
2163ec095263SOliver Hartkopp 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
21647a6a6cbcSPaolo Abeni 
21657a6a6cbcSPaolo Abeni 	len = min_t(size_t, len, INT_MAX);
21667a6a6cbcSPaolo Abeni 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
21677a6a6cbcSPaolo Abeni 
21682c9e7765SFlorian Westphal 	if (unlikely(msk->recvmsg_inq))
21692c9e7765SFlorian Westphal 		cmsg_flags = MPTCP_CMSG_INQ;
21702c9e7765SFlorian Westphal 
217105e3eceaSEric Dumazet 	while (copied < len) {
2172a66805c9SPaolo Abeni 		int err, bytes_read;
21737a6a6cbcSPaolo Abeni 
2174b7f653b2SFlorian Westphal 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
21756771bfd9SFlorian Westphal 		if (unlikely(bytes_read < 0)) {
21767a6a6cbcSPaolo Abeni 			if (!copied)
21777a6a6cbcSPaolo Abeni 				copied = bytes_read;
21786771bfd9SFlorian Westphal 			goto out_err;
2179cec37a6eSPeter Krystad 		}
2180cec37a6eSPeter Krystad 
21817a6a6cbcSPaolo Abeni 		copied += bytes_read;
21827a6a6cbcSPaolo Abeni 
2183ea4ca586SPaolo Abeni 		/* be sure to advertise window change */
2184fd897679SPaolo Abeni 		mptcp_cleanup_rbuf(msk);
2185ea4ca586SPaolo Abeni 
2186e3859603SPaolo Abeni 		if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2187e3859603SPaolo Abeni 			continue;
2188e3859603SPaolo Abeni 
21897a6a6cbcSPaolo Abeni 		/* only the master socket status is relevant here. The exit
21907a6a6cbcSPaolo Abeni 		 * conditions mirror closely tcp_recvmsg()
21917a6a6cbcSPaolo Abeni 		 */
21927a6a6cbcSPaolo Abeni 		if (copied >= target)
21937a6a6cbcSPaolo Abeni 			break;
21947a6a6cbcSPaolo Abeni 
21957a6a6cbcSPaolo Abeni 		if (copied) {
21967a6a6cbcSPaolo Abeni 			if (sk->sk_err ||
21977a6a6cbcSPaolo Abeni 			    sk->sk_state == TCP_CLOSE ||
21987a6a6cbcSPaolo Abeni 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
21997a6a6cbcSPaolo Abeni 			    !timeo ||
22007a6a6cbcSPaolo Abeni 			    signal_pending(current))
22017a6a6cbcSPaolo Abeni 				break;
22027a6a6cbcSPaolo Abeni 		} else {
22037a6a6cbcSPaolo Abeni 			if (sk->sk_err) {
22047a6a6cbcSPaolo Abeni 				copied = sock_error(sk);
22057a6a6cbcSPaolo Abeni 				break;
22067a6a6cbcSPaolo Abeni 			}
22077a6a6cbcSPaolo Abeni 
220887952603SPaolo Abeni 			if (sk->sk_shutdown & RCV_SHUTDOWN) {
220987952603SPaolo Abeni 				/* race breaker: the shutdown could be after the
221087952603SPaolo Abeni 				 * previous receive queue check
221187952603SPaolo Abeni 				 */
2212e3859603SPaolo Abeni 				if (__mptcp_move_skbs(msk))
221387952603SPaolo Abeni 					continue;
22147a6a6cbcSPaolo Abeni 				break;
221587952603SPaolo Abeni 			}
22167a6a6cbcSPaolo Abeni 
22177a6a6cbcSPaolo Abeni 			if (sk->sk_state == TCP_CLOSE) {
22187a6a6cbcSPaolo Abeni 				copied = -ENOTCONN;
22197a6a6cbcSPaolo Abeni 				break;
22207a6a6cbcSPaolo Abeni 			}
22217a6a6cbcSPaolo Abeni 
22227a6a6cbcSPaolo Abeni 			if (!timeo) {
22237a6a6cbcSPaolo Abeni 				copied = -EAGAIN;
22247a6a6cbcSPaolo Abeni 				break;
22257a6a6cbcSPaolo Abeni 			}
22267a6a6cbcSPaolo Abeni 
22277a6a6cbcSPaolo Abeni 			if (signal_pending(current)) {
22287a6a6cbcSPaolo Abeni 				copied = sock_intr_errno(timeo);
22297a6a6cbcSPaolo Abeni 				break;
22307a6a6cbcSPaolo Abeni 			}
22317a6a6cbcSPaolo Abeni 		}
22327a6a6cbcSPaolo Abeni 
22333d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("block timeout %ld\n", timeo);
2234a66805c9SPaolo Abeni 		mptcp_rcv_space_adjust(msk, copied);
2235a66805c9SPaolo Abeni 		err = sk_wait_data(sk, &timeo, NULL);
2236a66805c9SPaolo Abeni 		if (err < 0) {
2237a66805c9SPaolo Abeni 			err = copied ? : err;
2238a66805c9SPaolo Abeni 			goto out_err;
22397a6a6cbcSPaolo Abeni 		}
2240a66805c9SPaolo Abeni 	}
2241a66805c9SPaolo Abeni 
2242a66805c9SPaolo Abeni 	mptcp_rcv_space_adjust(msk, copied);
22433c90e377SPaolo Abeni 
22446771bfd9SFlorian Westphal out_err:
2245b7f653b2SFlorian Westphal 	if (cmsg_flags && copied >= 0) {
2246b7f653b2SFlorian Westphal 		if (cmsg_flags & MPTCP_CMSG_TS)
2247b7f653b2SFlorian Westphal 			tcp_recv_timestamp(msg, sk, &tss);
22482c9e7765SFlorian Westphal 
22492c9e7765SFlorian Westphal 		if (cmsg_flags & MPTCP_CMSG_INQ) {
22502c9e7765SFlorian Westphal 			unsigned int inq = mptcp_inq_hint(sk);
22512c9e7765SFlorian Westphal 
22522c9e7765SFlorian Westphal 			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
22532c9e7765SFlorian Westphal 		}
2254b7f653b2SFlorian Westphal 	}
2255b7f653b2SFlorian Westphal 
22563d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p rx queue empty=%d:%d copied=%d\n",
2257612f71d7SPaolo Abeni 		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2258612f71d7SPaolo Abeni 		 skb_queue_empty(&msk->receive_queue), copied);
2259a6b118feSFlorian Westphal 
2260cec37a6eSPeter Krystad 	release_sock(sk);
2261cec37a6eSPeter Krystad 	return copied;
2262cec37a6eSPeter Krystad }
2263cec37a6eSPeter Krystad 
mptcp_retransmit_timer(struct timer_list * t)2264b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t)
2265b51f9b80SPaolo Abeni {
2266b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = from_timer(icsk, t,
2267b51f9b80SPaolo Abeni 						       icsk_retransmit_timer);
2268b51f9b80SPaolo Abeni 	struct sock *sk = &icsk->icsk_inet.sk;
22692d6f5a2bSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2270b51f9b80SPaolo Abeni 
2271b51f9b80SPaolo Abeni 	bh_lock_sock(sk);
2272b51f9b80SPaolo Abeni 	if (!sock_owned_by_user(sk)) {
22732d6f5a2bSPaolo Abeni 		/* we need a process context to retransmit */
22742d6f5a2bSPaolo Abeni 		if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
22752d6f5a2bSPaolo Abeni 			mptcp_schedule_work(sk);
2276b51f9b80SPaolo Abeni 	} else {
2277b51f9b80SPaolo Abeni 		/* delegate our work to tcp_release_cb() */
2278e9d09bacSPaolo Abeni 		__set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2279b51f9b80SPaolo Abeni 	}
2280b51f9b80SPaolo Abeni 	bh_unlock_sock(sk);
2281b51f9b80SPaolo Abeni 	sock_put(sk);
2282b51f9b80SPaolo Abeni }
2283b51f9b80SPaolo Abeni 
mptcp_tout_timer(struct timer_list * t)2284f6909dc1SPaolo Abeni static void mptcp_tout_timer(struct timer_list *t)
2285e16163b6SPaolo Abeni {
2286e16163b6SPaolo Abeni 	struct sock *sk = from_timer(sk, t, sk_timer);
2287e16163b6SPaolo Abeni 
2288e16163b6SPaolo Abeni 	mptcp_schedule_work(sk);
2289b6d69fc8SFlorian Westphal 	sock_put(sk);
2290e16163b6SPaolo Abeni }
2291e16163b6SPaolo Abeni 
22923b1d6210SPaolo Abeni /* Find an idle subflow.  Return NULL if there is unacked data at tcp
22933b1d6210SPaolo Abeni  * level.
22943b1d6210SPaolo Abeni  *
22953b1d6210SPaolo Abeni  * A backup subflow is returned only if that is the only kind available.
22963b1d6210SPaolo Abeni  */
mptcp_subflow_get_retrans(struct mptcp_sock * msk)229707336a87SGeliang Tang struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
22983b1d6210SPaolo Abeni {
229971b7dec2SPaolo Abeni 	struct sock *backup = NULL, *pick = NULL;
23003b1d6210SPaolo Abeni 	struct mptcp_subflow_context *subflow;
230171b7dec2SPaolo Abeni 	int min_stale_count = INT_MAX;
23023b1d6210SPaolo Abeni 
23033b1d6210SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
23043b1d6210SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
23053b1d6210SPaolo Abeni 
2306ff5a0b42SPaolo Abeni 		if (!__mptcp_subflow_active(subflow))
2307d5f49190SPaolo Abeni 			continue;
2308d5f49190SPaolo Abeni 
230971b7dec2SPaolo Abeni 		/* still data outstanding at TCP level? skip this */
231071b7dec2SPaolo Abeni 		if (!tcp_rtx_and_write_queues_empty(ssk)) {
231171b7dec2SPaolo Abeni 			mptcp_pm_subflow_chk_stale(msk, ssk);
231271b7dec2SPaolo Abeni 			min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2313860975c6SFlorian Westphal 			continue;
2314860975c6SFlorian Westphal 		}
23153b1d6210SPaolo Abeni 
23169e40cd79SMatthieu Baerts (NGI0) 		if (subflow->backup || subflow->request_bkup) {
23173b1d6210SPaolo Abeni 			if (!backup)
23183b1d6210SPaolo Abeni 				backup = ssk;
23193b1d6210SPaolo Abeni 			continue;
23203b1d6210SPaolo Abeni 		}
23213b1d6210SPaolo Abeni 
232271b7dec2SPaolo Abeni 		if (!pick)
232371b7dec2SPaolo Abeni 			pick = ssk;
23243b1d6210SPaolo Abeni 	}
23253b1d6210SPaolo Abeni 
232671b7dec2SPaolo Abeni 	if (pick)
232771b7dec2SPaolo Abeni 		return pick;
232871b7dec2SPaolo Abeni 
232971b7dec2SPaolo Abeni 	/* use backup only if there are no progresses anywhere */
233071b7dec2SPaolo Abeni 	return min_stale_count > 1 ? backup : NULL;
23313b1d6210SPaolo Abeni }
23323b1d6210SPaolo Abeni 
__mptcp_retransmit_pending_data(struct sock * sk)23331e1d9d6fSPaolo Abeni bool __mptcp_retransmit_pending_data(struct sock *sk)
23341e1d9d6fSPaolo Abeni {
23351e1d9d6fSPaolo Abeni 	struct mptcp_data_frag *cur, *rtx_head;
23361e1d9d6fSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
23371e1d9d6fSPaolo Abeni 
233800df24f1SGeliang Tang 	if (__mptcp_check_fallback(msk))
23391e1d9d6fSPaolo Abeni 		return false;
23401e1d9d6fSPaolo Abeni 
23411e1d9d6fSPaolo Abeni 	/* the closing socket has some data untransmitted and/or unacked:
23421e1d9d6fSPaolo Abeni 	 * some data in the mptcp rtx queue has not really xmitted yet.
23431e1d9d6fSPaolo Abeni 	 * keep it simple and re-inject the whole mptcp level rtx queue
23441e1d9d6fSPaolo Abeni 	 */
23451e1d9d6fSPaolo Abeni 	mptcp_data_lock(sk);
23461e1d9d6fSPaolo Abeni 	__mptcp_clean_una_wakeup(sk);
23471e1d9d6fSPaolo Abeni 	rtx_head = mptcp_rtx_head(sk);
23481e1d9d6fSPaolo Abeni 	if (!rtx_head) {
23491e1d9d6fSPaolo Abeni 		mptcp_data_unlock(sk);
23501e1d9d6fSPaolo Abeni 		return false;
23511e1d9d6fSPaolo Abeni 	}
23521e1d9d6fSPaolo Abeni 
23531e1d9d6fSPaolo Abeni 	msk->recovery_snd_nxt = msk->snd_nxt;
23541e1d9d6fSPaolo Abeni 	msk->recovery = true;
23551e1d9d6fSPaolo Abeni 	mptcp_data_unlock(sk);
23561e1d9d6fSPaolo Abeni 
23571e1d9d6fSPaolo Abeni 	msk->first_pending = rtx_head;
23581e1d9d6fSPaolo Abeni 	msk->snd_burst = 0;
23591e1d9d6fSPaolo Abeni 
23601e1d9d6fSPaolo Abeni 	/* be sure to clear the "sent status" on all re-injected fragments */
23611e1d9d6fSPaolo Abeni 	list_for_each_entry(cur, &msk->rtx_queue, list) {
23621e1d9d6fSPaolo Abeni 		if (!cur->already_sent)
23631e1d9d6fSPaolo Abeni 			break;
23641e1d9d6fSPaolo Abeni 		cur->already_sent = 0;
23651e1d9d6fSPaolo Abeni 	}
23661e1d9d6fSPaolo Abeni 
23671e1d9d6fSPaolo Abeni 	return true;
23681e1d9d6fSPaolo Abeni }
23691e1d9d6fSPaolo Abeni 
2370b29fcfb5SPaolo Abeni /* flags for __mptcp_close_ssk() */
2371b29fcfb5SPaolo Abeni #define MPTCP_CF_PUSH		BIT(1)
2372b29fcfb5SPaolo Abeni #define MPTCP_CF_FASTCLOSE	BIT(2)
2373b29fcfb5SPaolo Abeni 
237414c56686SGeliang Tang /* be sure to send a reset only if the caller asked for it, also
237514c56686SGeliang Tang  * clean completely the subflow status when the subflow reaches
237614c56686SGeliang Tang  * TCP_CLOSE state
237714c56686SGeliang Tang  */
__mptcp_subflow_disconnect(struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)237814c56686SGeliang Tang static void __mptcp_subflow_disconnect(struct sock *ssk,
237914c56686SGeliang Tang 				       struct mptcp_subflow_context *subflow,
238014c56686SGeliang Tang 				       unsigned int flags)
238114c56686SGeliang Tang {
238214c56686SGeliang Tang 	if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
238314c56686SGeliang Tang 	    (flags & MPTCP_CF_FASTCLOSE)) {
238414c56686SGeliang Tang 		/* The MPTCP code never wait on the subflow sockets, TCP-level
238514c56686SGeliang Tang 		 * disconnect should never fail
238614c56686SGeliang Tang 		 */
238714c56686SGeliang Tang 		WARN_ON_ONCE(tcp_disconnect(ssk, 0));
238814c56686SGeliang Tang 		mptcp_subflow_ctx_reset(subflow);
238914c56686SGeliang Tang 	} else {
239014c56686SGeliang Tang 		tcp_shutdown(ssk, SEND_SHUTDOWN);
239114c56686SGeliang Tang 	}
239214c56686SGeliang Tang }
239314c56686SGeliang Tang 
2394cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming
2395cec37a6eSPeter Krystad  * (accept).
2396cec37a6eSPeter Krystad  *
2397cec37a6eSPeter Krystad  * Outgoing subflows use in-kernel sockets.
2398cec37a6eSPeter Krystad  * Incoming subflows do not have their own 'struct socket' allocated,
2399cec37a6eSPeter Krystad  * so we need to use tcp_close() after detaching them from the mptcp
2400cec37a6eSPeter Krystad  * parent socket.
2401cec37a6eSPeter Krystad  */
__mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2402a141e02eSFlorian Westphal static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2403b29fcfb5SPaolo Abeni 			      struct mptcp_subflow_context *subflow,
2404b29fcfb5SPaolo Abeni 			      unsigned int flags)
2405cec37a6eSPeter Krystad {
2406e0be4931SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
240763740448SPaolo Abeni 	bool dispose_it, need_push = false;
240863740448SPaolo Abeni 
240963740448SPaolo Abeni 	/* If the first subflow moved to a close state before accept, e.g. due
241027e5ccc2SPaolo Abeni 	 * to an incoming reset or listener shutdown, the subflow socket is
241127e5ccc2SPaolo Abeni 	 * already deleted by inet_child_forget() and the mptcp socket can't
241227e5ccc2SPaolo Abeni 	 * survive too.
241363740448SPaolo Abeni 	 */
241427e5ccc2SPaolo Abeni 	if (msk->in_accept_queue && msk->first == ssk &&
241527e5ccc2SPaolo Abeni 	    (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
241663740448SPaolo Abeni 		/* ensure later check in mptcp_worker() will dispose the msk */
241727e5ccc2SPaolo Abeni 		mptcp_set_close_tout(sk, tcp_jiffies32 - (TCP_TIMEWAIT_LEN + 1));
241863740448SPaolo Abeni 		sock_set_flag(sk, SOCK_DEAD);
241963740448SPaolo Abeni 		lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
242063740448SPaolo Abeni 		mptcp_subflow_drop_ctx(ssk);
242163740448SPaolo Abeni 		goto out_release;
242263740448SPaolo Abeni 	}
2423e0be4931SFlorian Westphal 
242439880bd8SPaolo Abeni 	dispose_it = msk->free_first || ssk != msk->first;
2425b29fcfb5SPaolo Abeni 	if (dispose_it)
2426cec37a6eSPeter Krystad 		list_del(&subflow->node);
2427cec37a6eSPeter Krystad 
24283f8b2667SPaolo Abeni 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2429e16163b6SPaolo Abeni 
2430ff18f9efSPaolo Abeni 	if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
243114c56686SGeliang Tang 		/* be sure to force the tcp_close path
2432d21f8348SPaolo Abeni 		 * to generate the egress reset
2433d21f8348SPaolo Abeni 		 */
2434d21f8348SPaolo Abeni 		ssk->sk_lingertime = 0;
2435d21f8348SPaolo Abeni 		sock_set_flag(ssk, SOCK_LINGER);
2436b29fcfb5SPaolo Abeni 		subflow->send_fastclose = 1;
2437d21f8348SPaolo Abeni 	}
2438b29fcfb5SPaolo Abeni 
2439b29fcfb5SPaolo Abeni 	need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2440b29fcfb5SPaolo Abeni 	if (!dispose_it) {
244114c56686SGeliang Tang 		__mptcp_subflow_disconnect(ssk, subflow, flags);
2442b29fcfb5SPaolo Abeni 		release_sock(ssk);
2443b29fcfb5SPaolo Abeni 
2444b29fcfb5SPaolo Abeni 		goto out;
2445b29fcfb5SPaolo Abeni 	}
2446b29fcfb5SPaolo Abeni 
2447d7b1bfd0SPaolo Abeni 	subflow->disposable = 1;
2448d7b1bfd0SPaolo Abeni 
2449e16163b6SPaolo Abeni 	/* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2450e16163b6SPaolo Abeni 	 * the ssk has been already destroyed, we just need to release the
2451e16163b6SPaolo Abeni 	 * reference owned by msk;
2452e16163b6SPaolo Abeni 	 */
2453e16163b6SPaolo Abeni 	if (!inet_csk(ssk)->icsk_ulp_ops) {
2454b6985b9bSPaolo Abeni 		WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2455e16163b6SPaolo Abeni 		kfree_rcu(subflow, rcu);
2456e16163b6SPaolo Abeni 	} else {
2457d7b1bfd0SPaolo Abeni 		/* otherwise tcp will dispose of the ssk and subflow ctx */
2458e16163b6SPaolo Abeni 		__tcp_close(ssk, 0);
2459e16163b6SPaolo Abeni 
2460e16163b6SPaolo Abeni 		/* close acquired an extra ref */
2461e16163b6SPaolo Abeni 		__sock_put(ssk);
2462e16163b6SPaolo Abeni 	}
246363740448SPaolo Abeni 
246463740448SPaolo Abeni out_release:
24659f1a9881SPaolo Abeni 	__mptcp_subflow_error_report(sk, ssk);
2466e16163b6SPaolo Abeni 	release_sock(ssk);
2467e16163b6SPaolo Abeni 
2468e16163b6SPaolo Abeni 	sock_put(ssk);
2469e0be4931SFlorian Westphal 
2470c8fe62f0SFlorian Westphal 	if (ssk == msk->first)
24711b1b43eeSPaolo Abeni 		WRITE_ONCE(msk->first, NULL);
2472c8fe62f0SFlorian Westphal 
2473b29fcfb5SPaolo Abeni out:
247434c7757aSPaolo Abeni 	__mptcp_sync_sndbuf(sk);
24751e1d9d6fSPaolo Abeni 	if (need_push)
24761e1d9d6fSPaolo Abeni 		__mptcp_push_pending(sk, 0);
247727e5ccc2SPaolo Abeni 
247827e5ccc2SPaolo Abeni 	/* Catch every 'all subflows closed' scenario, including peers silently
247927e5ccc2SPaolo Abeni 	 * closing them, e.g. due to timeout.
248027e5ccc2SPaolo Abeni 	 * For established sockets, allow an additional timeout before closing,
248127e5ccc2SPaolo Abeni 	 * as the protocol can still create more subflows.
248227e5ccc2SPaolo Abeni 	 */
248327e5ccc2SPaolo Abeni 	if (list_is_singular(&msk->conn_list) && msk->first &&
248427e5ccc2SPaolo Abeni 	    inet_sk_state_load(msk->first) == TCP_CLOSE) {
248527e5ccc2SPaolo Abeni 		if (sk->sk_state != TCP_ESTABLISHED ||
248627e5ccc2SPaolo Abeni 		    msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2487d52b3c2bSGeliang Tang 			mptcp_set_state(sk, TCP_CLOSE);
248827e5ccc2SPaolo Abeni 			mptcp_close_wake_up(sk);
248927e5ccc2SPaolo Abeni 		} else {
249027e5ccc2SPaolo Abeni 			mptcp_start_tout_timer(sk);
249127e5ccc2SPaolo Abeni 		}
249227e5ccc2SPaolo Abeni 	}
2493f870fa0bSMat Martineau }
2494f870fa0bSMat Martineau 
mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow)2495a141e02eSFlorian Westphal void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2496a141e02eSFlorian Westphal 		     struct mptcp_subflow_context *subflow)
2497a141e02eSFlorian Westphal {
2498a088190fSMatthieu Baerts (NGI0) 	/* The first subflow can already be closed and still in the list */
2499a088190fSMatthieu Baerts (NGI0) 	if (subflow->close_event_done)
2500a088190fSMatthieu Baerts (NGI0) 		return;
2501a088190fSMatthieu Baerts (NGI0) 
2502a088190fSMatthieu Baerts (NGI0) 	subflow->close_event_done = true;
2503a088190fSMatthieu Baerts (NGI0) 
2504b911c97cSFlorian Westphal 	if (sk->sk_state == TCP_ESTABLISHED)
2505b911c97cSFlorian Westphal 		mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2506a88c9e49SPaolo Abeni 
2507a88c9e49SPaolo Abeni 	/* subflow aborted before reaching the fully_established status
2508a88c9e49SPaolo Abeni 	 * attempt the creation of the next subflow
2509a88c9e49SPaolo Abeni 	 */
2510a88c9e49SPaolo Abeni 	mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2511a88c9e49SPaolo Abeni 
2512b29fcfb5SPaolo Abeni 	__mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2513a141e02eSFlorian Westphal }
2514a141e02eSFlorian Westphal 
mptcp_sync_mss(struct sock * sk,u32 pmtu)2515dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2516dc24f8b4SPaolo Abeni {
2517dc24f8b4SPaolo Abeni 	return 0;
2518dc24f8b4SPaolo Abeni }
2519dc24f8b4SPaolo Abeni 
__mptcp_close_subflow(struct sock * sk)2520b6985b9bSPaolo Abeni static void __mptcp_close_subflow(struct sock *sk)
25210e4f35d7SPaolo Abeni {
25220e4f35d7SPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
2523b6985b9bSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
25240e4f35d7SPaolo Abeni 
25253abc05d9SFlorian Westphal 	might_sleep();
25263abc05d9SFlorian Westphal 
25275efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
25280e4f35d7SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2529255bc4fcSMatthieu Baerts (NGI0) 		int ssk_state = inet_sk_state_load(ssk);
25300e4f35d7SPaolo Abeni 
2531255bc4fcSMatthieu Baerts (NGI0) 		if (ssk_state != TCP_CLOSE &&
2532255bc4fcSMatthieu Baerts (NGI0) 		    (ssk_state != TCP_CLOSE_WAIT ||
2533255bc4fcSMatthieu Baerts (NGI0) 		     inet_sk_state_load(sk) != TCP_ESTABLISHED))
25340e4f35d7SPaolo Abeni 			continue;
25350e4f35d7SPaolo Abeni 
253640947e13SFlorian Westphal 		/* 'subflow_data_ready' will re-sched once rx queue is empty */
253740947e13SFlorian Westphal 		if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
253840947e13SFlorian Westphal 			continue;
253940947e13SFlorian Westphal 
2540b6985b9bSPaolo Abeni 		mptcp_close_ssk(sk, ssk, subflow);
2541b6985b9bSPaolo Abeni 	}
2542b6985b9bSPaolo Abeni 
25430e4f35d7SPaolo Abeni }
25440e4f35d7SPaolo Abeni 
mptcp_close_tout_expired(const struct sock * sk)254527e5ccc2SPaolo Abeni static bool mptcp_close_tout_expired(const struct sock *sk)
2546e16163b6SPaolo Abeni {
254727e5ccc2SPaolo Abeni 	if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
254827e5ccc2SPaolo Abeni 	    sk->sk_state == TCP_CLOSE)
2549e16163b6SPaolo Abeni 		return false;
255027e5ccc2SPaolo Abeni 
255127e5ccc2SPaolo Abeni 	return time_after32(tcp_jiffies32,
255227e5ccc2SPaolo Abeni 		  inet_csk(sk)->icsk_mtup.probe_timestamp + TCP_TIMEWAIT_LEN);
2553e16163b6SPaolo Abeni }
2554e16163b6SPaolo Abeni 
mptcp_check_fastclose(struct mptcp_sock * msk)255550c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk)
255650c504a2SFlorian Westphal {
255750c504a2SFlorian Westphal 	struct mptcp_subflow_context *subflow, *tmp;
255880638684SGeliang Tang 	struct sock *sk = (struct sock *)msk;
255950c504a2SFlorian Westphal 
256050c504a2SFlorian Westphal 	if (likely(!READ_ONCE(msk->rcv_fastclose)))
256150c504a2SFlorian Westphal 		return;
256250c504a2SFlorian Westphal 
256350c504a2SFlorian Westphal 	mptcp_token_destroy(msk);
256450c504a2SFlorian Westphal 
25655efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
256650c504a2SFlorian Westphal 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
256775e908c3SPaolo Abeni 		bool slow;
256850c504a2SFlorian Westphal 
256975e908c3SPaolo Abeni 		slow = lock_sock_fast(tcp_sk);
257050c504a2SFlorian Westphal 		if (tcp_sk->sk_state != TCP_CLOSE) {
257150c504a2SFlorian Westphal 			tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
257250c504a2SFlorian Westphal 			tcp_set_state(tcp_sk, TCP_CLOSE);
257350c504a2SFlorian Westphal 		}
257475e908c3SPaolo Abeni 		unlock_sock_fast(tcp_sk, slow);
257550c504a2SFlorian Westphal 	}
257650c504a2SFlorian Westphal 
257769800e51SPaolo Abeni 	/* Mirror the tcp_reset() error propagation */
257869800e51SPaolo Abeni 	switch (sk->sk_state) {
257969800e51SPaolo Abeni 	case TCP_SYN_SENT:
25809ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, ECONNREFUSED);
258169800e51SPaolo Abeni 		break;
258269800e51SPaolo Abeni 	case TCP_CLOSE_WAIT:
25839ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, EPIPE);
258469800e51SPaolo Abeni 		break;
258569800e51SPaolo Abeni 	case TCP_CLOSE:
258669800e51SPaolo Abeni 		return;
258769800e51SPaolo Abeni 	default:
25889ae8e5adSEric Dumazet 		WRITE_ONCE(sk->sk_err, ECONNRESET);
258969800e51SPaolo Abeni 	}
259069800e51SPaolo Abeni 
2591d52b3c2bSGeliang Tang 	mptcp_set_state(sk, TCP_CLOSE);
25926b9831bfSPaolo Abeni 	WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
259350c504a2SFlorian Westphal 	smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
259450c504a2SFlorian Westphal 	set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
259550c504a2SFlorian Westphal 
259669800e51SPaolo Abeni 	/* the calling mptcp_worker will properly destroy the socket */
259769800e51SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD))
259869800e51SPaolo Abeni 		return;
259969800e51SPaolo Abeni 
260069800e51SPaolo Abeni 	sk->sk_state_change(sk);
260169800e51SPaolo Abeni 	sk_error_report(sk);
260250c504a2SFlorian Westphal }
260350c504a2SFlorian Westphal 
__mptcp_retrans(struct sock * sk)26042948d0a1SPaolo Abeni static void __mptcp_retrans(struct sock *sk)
260580992017SPaolo Abeni {
26062948d0a1SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2607ee2708aeSGeliang Tang 	struct mptcp_subflow_context *subflow;
2608caf971dfSPaolo Abeni 	struct mptcp_sendmsg_info info = {};
26093b1d6210SPaolo Abeni 	struct mptcp_data_frag *dfrag;
26102948d0a1SPaolo Abeni 	struct sock *ssk;
2611ee2708aeSGeliang Tang 	int ret, err;
2612ee2708aeSGeliang Tang 	u16 len = 0;
26132948d0a1SPaolo Abeni 
2614b5941f06SPaolo Abeni 	mptcp_clean_una_wakeup(sk);
26153241a9c0SFlorian Westphal 
26163241a9c0SFlorian Westphal 	/* first check ssk: need to kick "stale" logic */
2617ee2708aeSGeliang Tang 	err = mptcp_sched_get_retrans(msk);
26182948d0a1SPaolo Abeni 	dfrag = mptcp_rtx_head(sk);
26196477dd39SMat Martineau 	if (!dfrag) {
26206477dd39SMat Martineau 		if (mptcp_data_fin_enabled(msk)) {
26216477dd39SMat Martineau 			struct inet_connection_sock *icsk = inet_csk(sk);
26226477dd39SMat Martineau 
26236477dd39SMat Martineau 			icsk->icsk_retransmits++;
26246477dd39SMat Martineau 			mptcp_set_datafin_timeout(sk);
26256477dd39SMat Martineau 			mptcp_send_ack(msk);
26266477dd39SMat Martineau 
26276477dd39SMat Martineau 			goto reset_timer;
26286477dd39SMat Martineau 		}
26296477dd39SMat Martineau 
26303241a9c0SFlorian Westphal 		if (!mptcp_send_head(sk))
26312948d0a1SPaolo Abeni 			return;
26323241a9c0SFlorian Westphal 
26333241a9c0SFlorian Westphal 		goto reset_timer;
26346477dd39SMat Martineau 	}
26352948d0a1SPaolo Abeni 
2636ee2708aeSGeliang Tang 	if (err)
26372948d0a1SPaolo Abeni 		goto reset_timer;
26382948d0a1SPaolo Abeni 
2639ee2708aeSGeliang Tang 	mptcp_for_each_subflow(msk, subflow) {
2640ee2708aeSGeliang Tang 		if (READ_ONCE(subflow->scheduled)) {
2641ee2708aeSGeliang Tang 			u16 copied = 0;
2642ee2708aeSGeliang Tang 
2643ee2708aeSGeliang Tang 			mptcp_subflow_set_scheduled(subflow, false);
2644ee2708aeSGeliang Tang 
2645ee2708aeSGeliang Tang 			ssk = mptcp_subflow_tcp_sock(subflow);
2646ee2708aeSGeliang Tang 
26472948d0a1SPaolo Abeni 			lock_sock(ssk);
26482948d0a1SPaolo Abeni 
26492948d0a1SPaolo Abeni 			/* limit retransmission to the bytes already sent on some subflows */
26502948d0a1SPaolo Abeni 			info.sent = 0;
2651ee2708aeSGeliang Tang 			info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2652ee2708aeSGeliang Tang 								    dfrag->already_sent;
26534e14867dSPaolo Abeni 			while (info.sent < info.limit) {
26542948d0a1SPaolo Abeni 				ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
26552948d0a1SPaolo Abeni 				if (ret <= 0)
26562948d0a1SPaolo Abeni 					break;
26572948d0a1SPaolo Abeni 
26582948d0a1SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
26592948d0a1SPaolo Abeni 				copied += ret;
26602948d0a1SPaolo Abeni 				info.sent += ret;
26612948d0a1SPaolo Abeni 			}
26624e14867dSPaolo Abeni 			if (copied) {
2663ee2708aeSGeliang Tang 				len = max(copied, len);
26642948d0a1SPaolo Abeni 				tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
26652948d0a1SPaolo Abeni 					 info.size_goal);
26660530020aSGeliang Tang 				WRITE_ONCE(msk->allow_infinite_fallback, false);
26674e14867dSPaolo Abeni 			}
26682948d0a1SPaolo Abeni 
26692948d0a1SPaolo Abeni 			release_sock(ssk);
2670ee2708aeSGeliang Tang 		}
2671ee2708aeSGeliang Tang 	}
2672ee2708aeSGeliang Tang 
2673ee2708aeSGeliang Tang 	msk->bytes_retrans += len;
2674ee2708aeSGeliang Tang 	dfrag->already_sent = max(dfrag->already_sent, len);
26752948d0a1SPaolo Abeni 
26762948d0a1SPaolo Abeni reset_timer:
26773241a9c0SFlorian Westphal 	mptcp_check_and_set_pending(sk);
26783241a9c0SFlorian Westphal 
2679f6909dc1SPaolo Abeni 	if (!mptcp_rtx_timer_pending(sk))
2680f6909dc1SPaolo Abeni 		mptcp_reset_rtx_timer(sk);
26812948d0a1SPaolo Abeni }
26822948d0a1SPaolo Abeni 
268376a13b31SGeliang Tang /* schedule the timeout timer for the relevant event: either close timeout
268476a13b31SGeliang Tang  * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
268576a13b31SGeliang Tang  */
mptcp_reset_tout_timer(struct mptcp_sock * msk,unsigned long fail_tout)2686f6909dc1SPaolo Abeni void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
268776a13b31SGeliang Tang {
268876a13b31SGeliang Tang 	struct sock *sk = (struct sock *)msk;
268976a13b31SGeliang Tang 	unsigned long timeout, close_timeout;
269076a13b31SGeliang Tang 
269127e5ccc2SPaolo Abeni 	if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
269276a13b31SGeliang Tang 		return;
269376a13b31SGeliang Tang 
2694*44b1bfb5SDmitry Kandybka 	close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
2695*44b1bfb5SDmitry Kandybka 			tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
269676a13b31SGeliang Tang 
269776a13b31SGeliang Tang 	/* the close timeout takes precedence on the fail one, and here at least one of
269876a13b31SGeliang Tang 	 * them is active
269976a13b31SGeliang Tang 	 */
270027e5ccc2SPaolo Abeni 	timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
270176a13b31SGeliang Tang 
270276a13b31SGeliang Tang 	sk_reset_timer(sk, &sk->sk_timer, timeout);
270376a13b31SGeliang Tang }
270476a13b31SGeliang Tang 
mptcp_mp_fail_no_response(struct mptcp_sock * msk)270549fa1919SGeliang Tang static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
270649fa1919SGeliang Tang {
270776a13b31SGeliang Tang 	struct sock *ssk = msk->first;
270849fa1919SGeliang Tang 	bool slow;
270949fa1919SGeliang Tang 
271076a13b31SGeliang Tang 	if (!ssk)
271176a13b31SGeliang Tang 		return;
271276a13b31SGeliang Tang 
27133d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
271449fa1919SGeliang Tang 
271549fa1919SGeliang Tang 	slow = lock_sock_fast(ssk);
271649fa1919SGeliang Tang 	mptcp_subflow_reset(ssk);
271776a13b31SGeliang Tang 	WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
271849fa1919SGeliang Tang 	unlock_sock_fast(ssk, slow);
271949fa1919SGeliang Tang }
272049fa1919SGeliang Tang 
mptcp_do_fastclose(struct sock * sk)2721d21f8348SPaolo Abeni static void mptcp_do_fastclose(struct sock *sk)
2722d21f8348SPaolo Abeni {
2723d21f8348SPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
2724d21f8348SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2725d21f8348SPaolo Abeni 
2726d52b3c2bSGeliang Tang 	mptcp_set_state(sk, TCP_CLOSE);
2727d21f8348SPaolo Abeni 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
2728d21f8348SPaolo Abeni 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2729d21f8348SPaolo Abeni 				  subflow, MPTCP_CF_FASTCLOSE);
2730d21f8348SPaolo Abeni }
2731d21f8348SPaolo Abeni 
mptcp_worker(struct work_struct * work)27322948d0a1SPaolo Abeni static void mptcp_worker(struct work_struct *work)
27332948d0a1SPaolo Abeni {
27342948d0a1SPaolo Abeni 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
273580638684SGeliang Tang 	struct sock *sk = (struct sock *)msk;
273676a13b31SGeliang Tang 	unsigned long fail_tout;
27372948d0a1SPaolo Abeni 	int state;
273880992017SPaolo Abeni 
273980992017SPaolo Abeni 	lock_sock(sk);
2740e16163b6SPaolo Abeni 	state = sk->sk_state;
2741d6a04437SPaolo Abeni 	if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2742e16163b6SPaolo Abeni 		goto unlock;
2743e16163b6SPaolo Abeni 
274450c504a2SFlorian Westphal 	mptcp_check_fastclose(msk);
274550c504a2SFlorian Westphal 
2746e9801430SFlorian Westphal 	mptcp_pm_nl_work(msk);
2747b416268bSFlorian Westphal 
274881c1d029SPaolo Abeni 	mptcp_check_send_data_fin(sk);
274981c1d029SPaolo Abeni 	mptcp_check_data_fin_ack(sk);
275043b54c6eSMat Martineau 	mptcp_check_data_fin(sk);
275143b54c6eSMat Martineau 
2752b6985b9bSPaolo Abeni 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2753b6985b9bSPaolo Abeni 		__mptcp_close_subflow(sk);
2754b6985b9bSPaolo Abeni 
275527e5ccc2SPaolo Abeni 	if (mptcp_close_tout_expired(sk)) {
2756d21f8348SPaolo Abeni 		mptcp_do_fastclose(sk);
275727e5ccc2SPaolo Abeni 		mptcp_close_wake_up(sk);
275827e5ccc2SPaolo Abeni 	}
2759bbd49d11SPaolo Abeni 
276027e5ccc2SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2761e16163b6SPaolo Abeni 		__mptcp_destroy_sock(sk);
2762e16163b6SPaolo Abeni 		goto unlock;
2763e16163b6SPaolo Abeni 	}
2764e16163b6SPaolo Abeni 
27652948d0a1SPaolo Abeni 	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
27662948d0a1SPaolo Abeni 		__mptcp_retrans(sk);
27673b1d6210SPaolo Abeni 
276876a13b31SGeliang Tang 	fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
276976a13b31SGeliang Tang 	if (fail_tout && time_after(jiffies, fail_tout))
277049fa1919SGeliang Tang 		mptcp_mp_fail_no_response(msk);
277149fa1919SGeliang Tang 
27723b1d6210SPaolo Abeni unlock:
277380992017SPaolo Abeni 	release_sock(sk);
277480992017SPaolo Abeni 	sock_put(sk);
277580992017SPaolo Abeni }
277680992017SPaolo Abeni 
__mptcp_init_sock(struct sock * sk)2777e2636917SKuniyuki Iwashima static void __mptcp_init_sock(struct sock *sk)
2778f870fa0bSMat Martineau {
2779cec37a6eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
2780cec37a6eSPeter Krystad 
2781cec37a6eSPeter Krystad 	INIT_LIST_HEAD(&msk->conn_list);
2782ec3edaa7SPeter Krystad 	INIT_LIST_HEAD(&msk->join_list);
278318b683bfSPaolo Abeni 	INIT_LIST_HEAD(&msk->rtx_queue);
278480992017SPaolo Abeni 	INIT_WORK(&msk->work, mptcp_worker);
278587952603SPaolo Abeni 	__skb_queue_head_init(&msk->receive_queue);
2786ab174ad8SPaolo Abeni 	msk->out_of_order_queue = RB_ROOT;
2787f0e6a4cfSPaolo Abeni 	msk->first_pending = NULL;
27886511882cSPaolo Abeni 	msk->rmem_fwd_alloc = 0;
2789ce599c51SPaolo Abeni 	WRITE_ONCE(msk->rmem_released, 0);
279033d41c9cSPaolo Abeni 	msk->timer_ival = TCP_RTO_MIN;
2791124a4885SPaolo Abeni 	msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2792cec37a6eSPeter Krystad 
27931b1b43eeSPaolo Abeni 	WRITE_ONCE(msk->first, NULL);
2794dc24f8b4SPaolo Abeni 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2795752e9067SGeliang Tang 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
27960530020aSGeliang Tang 	WRITE_ONCE(msk->allow_infinite_fallback, true);
27971e1d9d6fSPaolo Abeni 	msk->recovery = false;
27986f06b4d4SPaolo Abeni 	msk->subflow_id = 1;
27998ab183deSPaolo Abeni 
28001b1c7a0eSPeter Krystad 	mptcp_pm_data_init(msk);
28011b1c7a0eSPeter Krystad 
2802b51f9b80SPaolo Abeni 	/* re-use the csk retrans timer for MPTCP-level retrans */
2803b51f9b80SPaolo Abeni 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2804f6909dc1SPaolo Abeni 	timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2805f870fa0bSMat Martineau }
2806f870fa0bSMat Martineau 
mptcp_ca_reset(struct sock * sk)2807b29fcfb5SPaolo Abeni static void mptcp_ca_reset(struct sock *sk)
2808784325e9SMatthieu Baerts {
280920b5759fSPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(sk);
2810b29fcfb5SPaolo Abeni 
2811b29fcfb5SPaolo Abeni 	tcp_assign_congestion_control(sk);
2812b29fcfb5SPaolo Abeni 	strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2813b29fcfb5SPaolo Abeni 
2814b29fcfb5SPaolo Abeni 	/* no need to keep a reference to the ops, the name will suffice */
2815b29fcfb5SPaolo Abeni 	tcp_cleanup_congestion_control(sk);
2816b29fcfb5SPaolo Abeni 	icsk->icsk_ca_ops = NULL;
2817b29fcfb5SPaolo Abeni }
2818b29fcfb5SPaolo Abeni 
mptcp_init_sock(struct sock * sk)2819b29fcfb5SPaolo Abeni static int mptcp_init_sock(struct sock *sk)
2820b29fcfb5SPaolo Abeni {
2821fc518953SFlorian Westphal 	struct net *net = sock_net(sk);
28221730b2b2SGeliang Tang 	int ret;
282318b683bfSPaolo Abeni 
2824e2636917SKuniyuki Iwashima 	__mptcp_init_sock(sk);
2825b6c08380SGeliang Tang 
2826fc518953SFlorian Westphal 	if (!mptcp_is_enabled(net))
2827fc518953SFlorian Westphal 		return -ENOPROTOOPT;
2828fc518953SFlorian Westphal 
2829fc518953SFlorian Westphal 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2830fc518953SFlorian Westphal 		return -ENOMEM;
2831fc518953SFlorian Westphal 
2832cb8b81adSMatthieu Baerts (NGI0) 	rcu_read_lock();
28331730b2b2SGeliang Tang 	ret = mptcp_init_sched(mptcp_sk(sk),
28341730b2b2SGeliang Tang 			       mptcp_sched_find(mptcp_get_scheduler(net)));
2835cb8b81adSMatthieu Baerts (NGI0) 	rcu_read_unlock();
28361730b2b2SGeliang Tang 	if (ret)
28371730b2b2SGeliang Tang 		return ret;
28381730b2b2SGeliang Tang 
2839a5ef058dSPaolo Abeni 	set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2840a5ef058dSPaolo Abeni 
284120b5759fSPaolo Abeni 	/* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
284220b5759fSPaolo Abeni 	 * propagate the correct value
284320b5759fSPaolo Abeni 	 */
2844b29fcfb5SPaolo Abeni 	mptcp_ca_reset(sk);
284520b5759fSPaolo Abeni 
2846d027236cSPaolo Abeni 	sk_sockets_allocated_inc(sk);
2847a963853fSGeliang Tang 	sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2848a963853fSGeliang Tang 	sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2849d027236cSPaolo Abeni 
285018b683bfSPaolo Abeni 	return 0;
285118b683bfSPaolo Abeni }
285218b683bfSPaolo Abeni 
__mptcp_clear_xmit(struct sock * sk)285318b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk)
285418b683bfSPaolo Abeni {
285518b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
285618b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
285718b683bfSPaolo Abeni 
2858d9ca1de8SPaolo Abeni 	WRITE_ONCE(msk->first_pending, NULL);
285918b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2860d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
2861784325e9SMatthieu Baerts }
2862784325e9SMatthieu Baerts 
mptcp_cancel_work(struct sock * sk)286330e51b92SMenglong Dong void mptcp_cancel_work(struct sock *sk)
286480992017SPaolo Abeni {
286580992017SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
286680992017SPaolo Abeni 
2867b2771d24SPaolo Abeni 	if (cancel_work_sync(&msk->work))
2868e16163b6SPaolo Abeni 		__sock_put(sk);
286980992017SPaolo Abeni }
287080992017SPaolo Abeni 
mptcp_subflow_shutdown(struct sock * sk,struct sock * ssk,int how)2871d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
287221498490SPeter Krystad {
287321498490SPeter Krystad 	lock_sock(ssk);
287421498490SPeter Krystad 
287521498490SPeter Krystad 	switch (ssk->sk_state) {
287621498490SPeter Krystad 	case TCP_LISTEN:
287721498490SPeter Krystad 		if (!(how & RCV_SHUTDOWN))
287821498490SPeter Krystad 			break;
2879df561f66SGustavo A. R. Silva 		fallthrough;
288021498490SPeter Krystad 	case TCP_SYN_SENT:
2881c2b2ae39SPaolo Abeni 		WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
288221498490SPeter Krystad 		break;
288321498490SPeter Krystad 	default:
288443b54c6eSMat Martineau 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
28853d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("Fallback\n");
288621498490SPeter Krystad 			ssk->sk_shutdown |= how;
288721498490SPeter Krystad 			tcp_shutdown(ssk, how);
288881c1d029SPaolo Abeni 
288981c1d029SPaolo Abeni 			/* simulate the data_fin ack reception to let the state
289081c1d029SPaolo Abeni 			 * machine move forward
289181c1d029SPaolo Abeni 			 */
289281c1d029SPaolo Abeni 			WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
289381c1d029SPaolo Abeni 			mptcp_schedule_work(sk);
289443b54c6eSMat Martineau 		} else {
28953d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
289643b54c6eSMat Martineau 			tcp_send_ack(ssk);
2897f6909dc1SPaolo Abeni 			if (!mptcp_rtx_timer_pending(sk))
2898f6909dc1SPaolo Abeni 				mptcp_reset_rtx_timer(sk);
289943b54c6eSMat Martineau 		}
290021498490SPeter Krystad 		break;
290121498490SPeter Krystad 	}
290221498490SPeter Krystad 
290321498490SPeter Krystad 	release_sock(ssk);
290421498490SPeter Krystad }
290521498490SPeter Krystad 
mptcp_set_state(struct sock * sk,int state)290656557547SGeliang Tang void mptcp_set_state(struct sock *sk, int state)
290756557547SGeliang Tang {
290856557547SGeliang Tang 	int oldstate = sk->sk_state;
290956557547SGeliang Tang 
291056557547SGeliang Tang 	switch (state) {
291156557547SGeliang Tang 	case TCP_ESTABLISHED:
291256557547SGeliang Tang 		if (oldstate != TCP_ESTABLISHED)
291356557547SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
291456557547SGeliang Tang 		break;
29153dd41669SJason Xing 	case TCP_CLOSE_WAIT:
29163dd41669SJason Xing 		/* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
29173dd41669SJason Xing 		 * MPTCP "accepted" sockets will be created later on. So no
29183dd41669SJason Xing 		 * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
29193dd41669SJason Xing 		 */
29203dd41669SJason Xing 		break;
292156557547SGeliang Tang 	default:
29223dd41669SJason Xing 		if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
292356557547SGeliang Tang 			MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
292456557547SGeliang Tang 	}
292556557547SGeliang Tang 
292656557547SGeliang Tang 	inet_sk_state_store(sk, state);
292756557547SGeliang Tang }
292856557547SGeliang Tang 
29296920b851SMat Martineau static const unsigned char new_state[16] = {
29306920b851SMat Martineau 	/* current state:     new state:      action:	*/
29316920b851SMat Martineau 	[0 /* (Invalid) */] = TCP_CLOSE,
29326920b851SMat Martineau 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
29336920b851SMat Martineau 	[TCP_SYN_SENT]      = TCP_CLOSE,
29346920b851SMat Martineau 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
29356920b851SMat Martineau 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
29366920b851SMat Martineau 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
29376920b851SMat Martineau 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
29386920b851SMat Martineau 	[TCP_CLOSE]         = TCP_CLOSE,
29396920b851SMat Martineau 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
29406920b851SMat Martineau 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
29416920b851SMat Martineau 	[TCP_LISTEN]        = TCP_CLOSE,
29426920b851SMat Martineau 	[TCP_CLOSING]       = TCP_CLOSING,
29436920b851SMat Martineau 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
29446920b851SMat Martineau };
29456920b851SMat Martineau 
mptcp_close_state(struct sock * sk)29466920b851SMat Martineau static int mptcp_close_state(struct sock *sk)
29476920b851SMat Martineau {
29486920b851SMat Martineau 	int next = (int)new_state[sk->sk_state];
29496920b851SMat Martineau 	int ns = next & TCP_STATE_MASK;
29506920b851SMat Martineau 
2951d52b3c2bSGeliang Tang 	mptcp_set_state(sk, ns);
29526920b851SMat Martineau 
29536920b851SMat Martineau 	return next & TCP_ACTION_FIN;
29546920b851SMat Martineau }
29556920b851SMat Martineau 
mptcp_check_send_data_fin(struct sock * sk)295681c1d029SPaolo Abeni static void mptcp_check_send_data_fin(struct sock *sk)
2957e16163b6SPaolo Abeni {
2958e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2959e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2960e16163b6SPaolo Abeni 
29613d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
2962e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2963e16163b6SPaolo Abeni 		 msk->snd_nxt, msk->write_seq);
2964e16163b6SPaolo Abeni 
2965e16163b6SPaolo Abeni 	/* we still need to enqueue subflows or not really shutting down,
2966e16163b6SPaolo Abeni 	 * skip this
2967e16163b6SPaolo Abeni 	 */
2968e16163b6SPaolo Abeni 	if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2969e16163b6SPaolo Abeni 	    mptcp_send_head(sk))
2970e16163b6SPaolo Abeni 		return;
2971e16163b6SPaolo Abeni 
2972e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2973e16163b6SPaolo Abeni 
2974e16163b6SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
2975e16163b6SPaolo Abeni 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2976e16163b6SPaolo Abeni 
2977e16163b6SPaolo Abeni 		mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2978e16163b6SPaolo Abeni 	}
2979e16163b6SPaolo Abeni }
2980e16163b6SPaolo Abeni 
__mptcp_wr_shutdown(struct sock * sk)2981e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk)
2982e16163b6SPaolo Abeni {
2983e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2984e16163b6SPaolo Abeni 
29853d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
2986e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2987e16163b6SPaolo Abeni 		 !!mptcp_send_head(sk));
2988e16163b6SPaolo Abeni 
2989e16163b6SPaolo Abeni 	/* will be ignored by fallback sockets */
2990e16163b6SPaolo Abeni 	WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2991e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_data_fin_enable, 1);
2992e16163b6SPaolo Abeni 
299381c1d029SPaolo Abeni 	mptcp_check_send_data_fin(sk);
2994e16163b6SPaolo Abeni }
2995e16163b6SPaolo Abeni 
__mptcp_destroy_sock(struct sock * sk)2996e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk)
2997f870fa0bSMat Martineau {
2998f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
2999f870fa0bSMat Martineau 
30003d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p\n", msk);
30012c22c06cSFlorian Westphal 
30023abc05d9SFlorian Westphal 	might_sleep();
30033abc05d9SFlorian Westphal 
3004f6909dc1SPaolo Abeni 	mptcp_stop_rtx_timer(sk);
3005e16163b6SPaolo Abeni 	sk_stop_timer(sk, &sk->sk_timer);
3006e16163b6SPaolo Abeni 	msk->pm.status = 0;
30071730b2b2SGeliang Tang 	mptcp_release_sched(msk);
3008b2c5b614SFlorian Westphal 
3009e16163b6SPaolo Abeni 	sk->sk_prot->destroy(sk);
3010e16163b6SPaolo Abeni 
30116511882cSPaolo Abeni 	WARN_ON_ONCE(msk->rmem_fwd_alloc);
301287952603SPaolo Abeni 	WARN_ON_ONCE(msk->rmem_released);
3013e16163b6SPaolo Abeni 	sk_stream_kill_queues(sk);
3014e16163b6SPaolo Abeni 	xfrm_sk_free_policy(sk);
3015aa1fbd94SFlorian Westphal 
3016e16163b6SPaolo Abeni 	sock_put(sk);
3017e16163b6SPaolo Abeni }
3018e16163b6SPaolo Abeni 
__mptcp_unaccepted_force_close(struct sock * sk)301963740448SPaolo Abeni void __mptcp_unaccepted_force_close(struct sock *sk)
302063740448SPaolo Abeni {
302163740448SPaolo Abeni 	sock_set_flag(sk, SOCK_DEAD);
302263740448SPaolo Abeni 	mptcp_do_fastclose(sk);
302363740448SPaolo Abeni 	__mptcp_destroy_sock(sk);
302463740448SPaolo Abeni }
302563740448SPaolo Abeni 
mptcp_check_readable(struct sock * sk)3026124a4885SPaolo Abeni static __poll_t mptcp_check_readable(struct sock *sk)
3027d21f8348SPaolo Abeni {
3028124a4885SPaolo Abeni 	return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3029d21f8348SPaolo Abeni }
3030d21f8348SPaolo Abeni 
mptcp_check_listen_stop(struct sock * sk)303157fc0f1cSPaolo Abeni static void mptcp_check_listen_stop(struct sock *sk)
3032c558246eSMenglong Dong {
303357fc0f1cSPaolo Abeni 	struct sock *ssk;
303457fc0f1cSPaolo Abeni 
303557fc0f1cSPaolo Abeni 	if (inet_sk_state_load(sk) != TCP_LISTEN)
303657fc0f1cSPaolo Abeni 		return;
303757fc0f1cSPaolo Abeni 
3038c558246eSMenglong Dong 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
303957fc0f1cSPaolo Abeni 	ssk = mptcp_sk(sk)->first;
304057fc0f1cSPaolo Abeni 	if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
304157fc0f1cSPaolo Abeni 		return;
304257fc0f1cSPaolo Abeni 
304357fc0f1cSPaolo Abeni 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
30443fffa15bSPaolo Abeni 	tcp_set_state(ssk, TCP_CLOSE);
304557fc0f1cSPaolo Abeni 	mptcp_subflow_queue_clean(sk, ssk);
304657fc0f1cSPaolo Abeni 	inet_csk_listen_stop(ssk);
304757fc0f1cSPaolo Abeni 	mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
304857fc0f1cSPaolo Abeni 	release_sock(ssk);
3049c558246eSMenglong Dong }
3050c558246eSMenglong Dong 
__mptcp_close(struct sock * sk,long timeout)305126d3e21cSMenglong Dong bool __mptcp_close(struct sock *sk, long timeout)
3052e16163b6SPaolo Abeni {
3053e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
305476a13b31SGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(sk);
3055e16163b6SPaolo Abeni 	bool do_cancel_work = false;
3056d4e85922SPaolo Abeni 	int subflows_alive = 0;
3057e16163b6SPaolo Abeni 
30586b9831bfSPaolo Abeni 	WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3059e16163b6SPaolo Abeni 
3060e16163b6SPaolo Abeni 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
306157fc0f1cSPaolo Abeni 		mptcp_check_listen_stop(sk);
3062d52b3c2bSGeliang Tang 		mptcp_set_state(sk, TCP_CLOSE);
3063e16163b6SPaolo Abeni 		goto cleanup;
3064e16163b6SPaolo Abeni 	}
3065e16163b6SPaolo Abeni 
3066124a4885SPaolo Abeni 	if (mptcp_data_avail(msk) || timeout < 0) {
30678d547809SPaolo Abeni 		/* If the msk has read data, or the caller explicitly ask it,
30688d547809SPaolo Abeni 		 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
30698d547809SPaolo Abeni 		 */
3070d21f8348SPaolo Abeni 		mptcp_do_fastclose(sk);
30718d547809SPaolo Abeni 		timeout = 0;
3072d21f8348SPaolo Abeni 	} else if (mptcp_close_state(sk)) {
3073e16163b6SPaolo Abeni 		__mptcp_wr_shutdown(sk);
3074d21f8348SPaolo Abeni 	}
3075e16163b6SPaolo Abeni 
3076e16163b6SPaolo Abeni 	sk_stream_wait_close(sk, timeout);
3077e16163b6SPaolo Abeni 
3078e16163b6SPaolo Abeni cleanup:
3079e16163b6SPaolo Abeni 	/* orphan all the subflows */
308076a13b31SGeliang Tang 	mptcp_for_each_subflow(msk, subflow) {
3081e16163b6SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
308249054556SPaolo Abeni 		bool slow = lock_sock_fast_nested(ssk);
3083e16163b6SPaolo Abeni 
3084d4e85922SPaolo Abeni 		subflows_alive += ssk->sk_state != TCP_CLOSE;
3085d4e85922SPaolo Abeni 
308676a13b31SGeliang Tang 		/* since the close timeout takes precedence on the fail one,
308776a13b31SGeliang Tang 		 * cancel the latter
308876a13b31SGeliang Tang 		 */
308976a13b31SGeliang Tang 		if (ssk == msk->first)
309076a13b31SGeliang Tang 			subflow->fail_tout = 0;
309176a13b31SGeliang Tang 
3092fe948001SMenglong Dong 		/* detach from the parent socket, but allow data_ready to
3093fe948001SMenglong Dong 		 * push incoming data into the mptcp stack, to properly ack it
3094fe948001SMenglong Dong 		 */
3095fe948001SMenglong Dong 		ssk->sk_socket = NULL;
3096fe948001SMenglong Dong 		ssk->sk_wq = NULL;
3097e16163b6SPaolo Abeni 		unlock_sock_fast(ssk, slow);
3098e16163b6SPaolo Abeni 	}
3099e16163b6SPaolo Abeni 	sock_orphan(sk);
3100e16163b6SPaolo Abeni 
3101d4e85922SPaolo Abeni 	/* all the subflows are closed, only timeout can change the msk
3102d4e85922SPaolo Abeni 	 * state, let's not keep resources busy for no reasons
3103d4e85922SPaolo Abeni 	 */
3104d4e85922SPaolo Abeni 	if (subflows_alive == 0)
3105d52b3c2bSGeliang Tang 		mptcp_set_state(sk, TCP_CLOSE);
3106d4e85922SPaolo Abeni 
3107e16163b6SPaolo Abeni 	sock_hold(sk);
31083d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
310900df24f1SGeliang Tang 	if (msk->token)
311076a13b31SGeliang Tang 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3111b29fcfb5SPaolo Abeni 
3112e16163b6SPaolo Abeni 	if (sk->sk_state == TCP_CLOSE) {
3113e16163b6SPaolo Abeni 		__mptcp_destroy_sock(sk);
3114e16163b6SPaolo Abeni 		do_cancel_work = true;
3115e16163b6SPaolo Abeni 	} else {
311627e5ccc2SPaolo Abeni 		mptcp_start_tout_timer(sk);
3117e16163b6SPaolo Abeni 	}
311826d3e21cSMenglong Dong 
311926d3e21cSMenglong Dong 	return do_cancel_work;
312026d3e21cSMenglong Dong }
312126d3e21cSMenglong Dong 
mptcp_close(struct sock * sk,long timeout)312226d3e21cSMenglong Dong static void mptcp_close(struct sock *sk, long timeout)
312326d3e21cSMenglong Dong {
312426d3e21cSMenglong Dong 	bool do_cancel_work;
312526d3e21cSMenglong Dong 
312626d3e21cSMenglong Dong 	lock_sock(sk);
312726d3e21cSMenglong Dong 
312826d3e21cSMenglong Dong 	do_cancel_work = __mptcp_close(sk, timeout);
3129e16163b6SPaolo Abeni 	release_sock(sk);
3130e16163b6SPaolo Abeni 	if (do_cancel_work)
313180992017SPaolo Abeni 		mptcp_cancel_work(sk);
3132b911c97cSFlorian Westphal 
3133e16163b6SPaolo Abeni 	sock_put(sk);
3134f870fa0bSMat Martineau }
3135f870fa0bSMat Martineau 
mptcp_copy_inaddrs(struct sock * msk,const struct sock * ssk)31367e8b88ecSPaolo Abeni static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3137cf7da0d6SPeter Krystad {
3138cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3139cf7da0d6SPeter Krystad 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3140cf7da0d6SPeter Krystad 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
3141cf7da0d6SPeter Krystad 
3142cf7da0d6SPeter Krystad 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
3143cf7da0d6SPeter Krystad 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3144cf7da0d6SPeter Krystad 
3145cf7da0d6SPeter Krystad 	if (msk6 && ssk6) {
3146cf7da0d6SPeter Krystad 		msk6->saddr = ssk6->saddr;
3147cf7da0d6SPeter Krystad 		msk6->flow_label = ssk6->flow_label;
3148cf7da0d6SPeter Krystad 	}
3149cf7da0d6SPeter Krystad #endif
3150cf7da0d6SPeter Krystad 
3151cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3152cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3153cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3154cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3155cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3156cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3157cf7da0d6SPeter Krystad }
3158cf7da0d6SPeter Krystad 
mptcp_disconnect(struct sock * sk,int flags)315918b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags)
316018b683bfSPaolo Abeni {
316176e2a55dSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
316276e2a55dSPaolo Abeni 
31637d803344SPaolo Abeni 	/* We are on the fastopen error path. We can't call straight into the
31647d803344SPaolo Abeni 	 * subflows cleanup code due to lock nesting (we are already under
3165c2b2ae39SPaolo Abeni 	 * msk->firstsocket lock).
31667d803344SPaolo Abeni 	 */
31677d803344SPaolo Abeni 	if (msk->fastopening)
3168c2b2ae39SPaolo Abeni 		return -EBUSY;
31697d803344SPaolo Abeni 
317057fc0f1cSPaolo Abeni 	mptcp_check_listen_stop(sk);
3171d52b3c2bSGeliang Tang 	mptcp_set_state(sk, TCP_CLOSE);
3172b29fcfb5SPaolo Abeni 
3173f6909dc1SPaolo Abeni 	mptcp_stop_rtx_timer(sk);
317427e5ccc2SPaolo Abeni 	mptcp_stop_tout_timer(sk);
3175b29fcfb5SPaolo Abeni 
317600df24f1SGeliang Tang 	if (msk->token)
317700df24f1SGeliang Tang 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3178b29fcfb5SPaolo Abeni 
3179c0bf3c6aSPaolo Abeni 	/* msk->subflow is still intact, the following will not free the first
3180c0bf3c6aSPaolo Abeni 	 * subflow
3181c0bf3c6aSPaolo Abeni 	 */
3182c0bf3c6aSPaolo Abeni 	mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3183e9d09bacSPaolo Abeni 	WRITE_ONCE(msk->flags, 0);
3184e9d09bacSPaolo Abeni 	msk->cb_flags = 0;
3185b29fcfb5SPaolo Abeni 	msk->recovery = false;
3186b29fcfb5SPaolo Abeni 	msk->can_ack = false;
3187b29fcfb5SPaolo Abeni 	msk->fully_established = false;
3188b29fcfb5SPaolo Abeni 	msk->rcv_data_fin = false;
3189b29fcfb5SPaolo Abeni 	msk->snd_data_fin_enable = false;
3190b29fcfb5SPaolo Abeni 	msk->rcv_fastclose = false;
3191b29fcfb5SPaolo Abeni 	msk->use_64bit_ack = false;
3192124a4885SPaolo Abeni 	msk->bytes_consumed = 0;
3193b29fcfb5SPaolo Abeni 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3194b29fcfb5SPaolo Abeni 	mptcp_pm_data_reset(msk);
3195b29fcfb5SPaolo Abeni 	mptcp_ca_reset(sk);
319638967f42SPaolo Abeni 	msk->bytes_acked = 0;
319738967f42SPaolo Abeni 	msk->bytes_received = 0;
319838967f42SPaolo Abeni 	msk->bytes_sent = 0;
319938967f42SPaolo Abeni 	msk->bytes_retrans = 0;
3200a2d74374SPaolo Abeni 	msk->rcvspace_init = 0;
3201b29fcfb5SPaolo Abeni 
32026b9831bfSPaolo Abeni 	WRITE_ONCE(sk->sk_shutdown, 0);
3203b29fcfb5SPaolo Abeni 	sk_error_report(sk);
320442c556feSFlorian Westphal 	return 0;
320518b683bfSPaolo Abeni }
320618b683bfSPaolo Abeni 
3207b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcp_inet6_sk(const struct sock * sk)3208b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3209b0519de8SFlorian Westphal {
3210b0519de8SFlorian Westphal 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3211b0519de8SFlorian Westphal 
3212b0519de8SFlorian Westphal 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3213b0519de8SFlorian Westphal }
3214ce0809adSDavide Caratti 
mptcp_copy_ip6_options(struct sock * newsk,const struct sock * sk)3215ce0809adSDavide Caratti static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3216ce0809adSDavide Caratti {
3217ce0809adSDavide Caratti 	const struct ipv6_pinfo *np = inet6_sk(sk);
3218ce0809adSDavide Caratti 	struct ipv6_txoptions *opt;
3219ce0809adSDavide Caratti 	struct ipv6_pinfo *newnp;
3220ce0809adSDavide Caratti 
3221ce0809adSDavide Caratti 	newnp = inet6_sk(newsk);
3222ce0809adSDavide Caratti 
3223ce0809adSDavide Caratti 	rcu_read_lock();
3224ce0809adSDavide Caratti 	opt = rcu_dereference(np->opt);
3225ce0809adSDavide Caratti 	if (opt) {
3226ce0809adSDavide Caratti 		opt = ipv6_dup_options(newsk, opt);
3227ce0809adSDavide Caratti 		if (!opt)
3228ce0809adSDavide Caratti 			net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3229ce0809adSDavide Caratti 	}
3230ce0809adSDavide Caratti 	RCU_INIT_POINTER(newnp->opt, opt);
3231ce0809adSDavide Caratti 	rcu_read_unlock();
3232ce0809adSDavide Caratti }
3233b0519de8SFlorian Westphal #endif
3234b0519de8SFlorian Westphal 
mptcp_copy_ip_options(struct sock * newsk,const struct sock * sk)3235ce0809adSDavide Caratti static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3236ce0809adSDavide Caratti {
3237ce0809adSDavide Caratti 	struct ip_options_rcu *inet_opt, *newopt = NULL;
3238ce0809adSDavide Caratti 	const struct inet_sock *inet = inet_sk(sk);
3239ce0809adSDavide Caratti 	struct inet_sock *newinet;
3240ce0809adSDavide Caratti 
3241ce0809adSDavide Caratti 	newinet = inet_sk(newsk);
3242ce0809adSDavide Caratti 
3243ce0809adSDavide Caratti 	rcu_read_lock();
3244ce0809adSDavide Caratti 	inet_opt = rcu_dereference(inet->inet_opt);
3245ce0809adSDavide Caratti 	if (inet_opt) {
3246ce0809adSDavide Caratti 		newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
3247ce0809adSDavide Caratti 				      inet_opt->opt.optlen, GFP_ATOMIC);
3248ce0809adSDavide Caratti 		if (newopt)
3249ce0809adSDavide Caratti 			memcpy(newopt, inet_opt, sizeof(*inet_opt) +
3250ce0809adSDavide Caratti 			       inet_opt->opt.optlen);
3251ce0809adSDavide Caratti 		else
3252ce0809adSDavide Caratti 			net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3253ce0809adSDavide Caratti 	}
3254ce0809adSDavide Caratti 	RCU_INIT_POINTER(newinet->inet_opt, newopt);
3255ce0809adSDavide Caratti 	rcu_read_unlock();
3256ce0809adSDavide Caratti }
3257ce0809adSDavide Caratti 
mptcp_sk_clone_init(const struct sock * sk,const struct mptcp_options_received * mp_opt,struct sock * ssk,struct request_sock * req)32587e8b88ecSPaolo Abeni struct sock *mptcp_sk_clone_init(const struct sock *sk,
3259cfde141eSPaolo Abeni 				 const struct mptcp_options_received *mp_opt,
32607e8b88ecSPaolo Abeni 				 struct sock *ssk,
3261fca5c82cSPaolo Abeni 				 struct request_sock *req)
3262b0519de8SFlorian Westphal {
326358b09919SPaolo Abeni 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3264b0519de8SFlorian Westphal 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3265b45df837SPaolo Abeni 	struct mptcp_subflow_context *subflow;
326658b09919SPaolo Abeni 	struct mptcp_sock *msk;
3267b0519de8SFlorian Westphal 
3268b0519de8SFlorian Westphal 	if (!nsk)
3269b0519de8SFlorian Westphal 		return NULL;
3270b0519de8SFlorian Westphal 
3271b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3272b0519de8SFlorian Westphal 	if (nsk->sk_family == AF_INET6)
3273b0519de8SFlorian Westphal 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3274b0519de8SFlorian Westphal #endif
3275b0519de8SFlorian Westphal 
327658b09919SPaolo Abeni 	__mptcp_init_sock(nsk);
327758b09919SPaolo Abeni 
3278ce0809adSDavide Caratti #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3279ce0809adSDavide Caratti 	if (nsk->sk_family == AF_INET6)
3280ce0809adSDavide Caratti 		mptcp_copy_ip6_options(nsk, sk);
3281ce0809adSDavide Caratti 	else
3282ce0809adSDavide Caratti #endif
3283ce0809adSDavide Caratti 		mptcp_copy_ip_options(nsk, sk);
3284ce0809adSDavide Caratti 
328558b09919SPaolo Abeni 	msk = mptcp_sk(nsk);
328658b09919SPaolo Abeni 	msk->local_key = subflow_req->local_key;
328758b09919SPaolo Abeni 	msk->token = subflow_req->token;
3288b6985b9bSPaolo Abeni 	msk->in_accept_queue = 1;
3289b93df08cSPaolo Abeni 	WRITE_ONCE(msk->fully_established, false);
329074c7dfbeSPaolo Abeni 	if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
329106251181SGeliang Tang 		WRITE_ONCE(msk->csum_enabled, true);
329258b09919SPaolo Abeni 
329358b09919SPaolo Abeni 	msk->write_seq = subflow_req->idsn + 1;
3294eaa2ffabSPaolo Abeni 	msk->snd_nxt = msk->write_seq;
32957439d687SPaolo Abeni 	msk->snd_una = msk->write_seq;
3296ae5530ccSPaolo Abeni 	msk->wnd_end = msk->snd_nxt + tcp_sk(ssk)->snd_wnd;
3297df00b087SFlorian Westphal 	msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
32981730b2b2SGeliang Tang 	mptcp_init_sched(msk, mptcp_sk(sk)->sched);
32996f8a612aSFlorian Westphal 
33006f06b4d4SPaolo Abeni 	/* passive msk is created after the first/MPC subflow */
33016f06b4d4SPaolo Abeni 	msk->subflow_id = 2;
33026f06b4d4SPaolo Abeni 
33035e20087dSFlorian Westphal 	sock_reset_flag(nsk, SOCK_RCU_FREE);
33040c148460SPaolo Abeni 	security_inet_csk_clone(nsk, req);
33057e8b88ecSPaolo Abeni 
33067e8b88ecSPaolo Abeni 	/* this can't race with mptcp_close(), as the msk is
33077e8b88ecSPaolo Abeni 	 * not yet exposted to user-space
33087e8b88ecSPaolo Abeni 	 */
3309d52b3c2bSGeliang Tang 	mptcp_set_state(nsk, TCP_ESTABLISHED);
33107e8b88ecSPaolo Abeni 
33117e8b88ecSPaolo Abeni 	/* The msk maintain a ref to each subflow in the connections list */
33127e8b88ecSPaolo Abeni 	WRITE_ONCE(msk->first, ssk);
3313b45df837SPaolo Abeni 	subflow = mptcp_subflow_ctx(ssk);
3314b45df837SPaolo Abeni 	list_add(&subflow->node, &msk->conn_list);
33157e8b88ecSPaolo Abeni 	sock_hold(ssk);
33167e8b88ecSPaolo Abeni 
33177e8b88ecSPaolo Abeni 	/* new mpc subflow takes ownership of the newly
33187e8b88ecSPaolo Abeni 	 * created mptcp socket
33197e8b88ecSPaolo Abeni 	 */
33207e8b88ecSPaolo Abeni 	mptcp_token_accept(subflow_req, msk);
33217e8b88ecSPaolo Abeni 
33227e8b88ecSPaolo Abeni 	/* set msk addresses early to ensure mptcp_pm_get_local_id()
33237e8b88ecSPaolo Abeni 	 * uses the correct data
33247e8b88ecSPaolo Abeni 	 */
33257e8b88ecSPaolo Abeni 	mptcp_copy_inaddrs(nsk, ssk);
332634c7757aSPaolo Abeni 	__mptcp_propagate_sndbuf(nsk, ssk);
33277e8b88ecSPaolo Abeni 
33287e8b88ecSPaolo Abeni 	mptcp_rcv_space_init(msk, ssk);
3329b45df837SPaolo Abeni 
3330b45df837SPaolo Abeni 	if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3331b45df837SPaolo Abeni 		__mptcp_subflow_fully_established(msk, subflow, mp_opt);
333258b09919SPaolo Abeni 	bh_unlock_sock(nsk);
333358b09919SPaolo Abeni 
3334b6985b9bSPaolo Abeni 	/* note: the newly allocated socket refcount is 2 now */
3335b0519de8SFlorian Westphal 	return nsk;
3336b0519de8SFlorian Westphal }
3337b0519de8SFlorian Westphal 
mptcp_rcv_space_init(struct mptcp_sock * msk,const struct sock * ssk)3338a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3339a6b118feSFlorian Westphal {
3340a6b118feSFlorian Westphal 	const struct tcp_sock *tp = tcp_sk(ssk);
3341a6b118feSFlorian Westphal 
3342a2d74374SPaolo Abeni 	msk->rcvspace_init = 1;
3343a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
3344a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = 0;
3345a6b118feSFlorian Westphal 
3346a6b118feSFlorian Westphal 	msk->rcvq_space.time = tp->tcp_mstamp;
3347a6b118feSFlorian Westphal 
3348a6b118feSFlorian Westphal 	/* initial rcv_space offering made to peer */
3349a6b118feSFlorian Westphal 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3350a6b118feSFlorian Westphal 				      TCP_INIT_CWND * tp->advmss);
3351a6b118feSFlorian Westphal 	if (msk->rcvq_space.space == 0)
3352a6b118feSFlorian Westphal 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3353a6b118feSFlorian Westphal }
3354a6b118feSFlorian Westphal 
mptcp_destroy_common(struct mptcp_sock * msk,unsigned int flags)3355c0bf3c6aSPaolo Abeni void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
33565c8c1640SGeliang Tang {
3357c0bf3c6aSPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
335887952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
335987952603SPaolo Abeni 
33606e628cd3SPaolo Abeni 	__mptcp_clear_xmit(sk);
33616e628cd3SPaolo Abeni 
3362c0bf3c6aSPaolo Abeni 	/* join list will be eventually flushed (with rst) at sock lock release time */
33635efbf6f7SMatthieu Baerts 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
3364c0bf3c6aSPaolo Abeni 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3365c0bf3c6aSPaolo Abeni 
336687952603SPaolo Abeni 	/* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3367b29fcfb5SPaolo Abeni 	mptcp_data_lock(sk);
336887952603SPaolo Abeni 	skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
33696511882cSPaolo Abeni 	__skb_queue_purge(&sk->sk_receive_queue);
33705c8c1640SGeliang Tang 	skb_rbtree_purge(&msk->out_of_order_queue);
3371b29fcfb5SPaolo Abeni 	mptcp_data_unlock(sk);
33726511882cSPaolo Abeni 
33736511882cSPaolo Abeni 	/* move all the rx fwd alloc into the sk_mem_reclaim_final in
33746511882cSPaolo Abeni 	 * inet_sock_destruct() will dispose it
33756511882cSPaolo Abeni 	 */
33765e6300e7SEric Dumazet 	sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
33779531e4a8SEric Dumazet 	WRITE_ONCE(msk->rmem_fwd_alloc, 0);
33785c8c1640SGeliang Tang 	mptcp_token_destroy(msk);
33795c8c1640SGeliang Tang 	mptcp_pm_free_anno_list(msk);
33804638de5aSKishen Maloor 	mptcp_free_local_addr_list(msk);
33815c8c1640SGeliang Tang }
33825c8c1640SGeliang Tang 
mptcp_destroy(struct sock * sk)338379c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk)
338479c0949eSPeter Krystad {
3385c9fd9c5fSFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
3386c9fd9c5fSFlorian Westphal 
338739880bd8SPaolo Abeni 	/* allow the following to close even the initial subflow */
338839880bd8SPaolo Abeni 	msk->free_first = 1;
3389c0bf3c6aSPaolo Abeni 	mptcp_destroy_common(msk, 0);
3390d027236cSPaolo Abeni 	sk_sockets_allocated_dec(sk);
339179c0949eSPeter Krystad }
339279c0949eSPeter Krystad 
__mptcp_data_acked(struct sock * sk)33936e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk)
33946e628cd3SPaolo Abeni {
33956e628cd3SPaolo Abeni 	if (!sock_owned_by_user(sk))
33966e628cd3SPaolo Abeni 		__mptcp_clean_una(sk);
33976e628cd3SPaolo Abeni 	else
3398e9d09bacSPaolo Abeni 		__set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
33996e628cd3SPaolo Abeni 
34006e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk))
34016e628cd3SPaolo Abeni 		mptcp_schedule_work(sk);
34026e628cd3SPaolo Abeni }
34036e628cd3SPaolo Abeni 
__mptcp_check_push(struct sock * sk,struct sock * ssk)3404219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk)
34056e628cd3SPaolo Abeni {
34066e628cd3SPaolo Abeni 	if (!mptcp_send_head(sk))
34076e628cd3SPaolo Abeni 		return;
34086e628cd3SPaolo Abeni 
340973a0052aSGeliang Tang 	if (!sock_owned_by_user(sk))
341073a0052aSGeliang Tang 		__mptcp_subflow_push_pending(sk, ssk, false);
341173a0052aSGeliang Tang 	else
3412e9d09bacSPaolo Abeni 		__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
34136e628cd3SPaolo Abeni }
34146e628cd3SPaolo Abeni 
3415e9d09bacSPaolo Abeni #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3416e9d09bacSPaolo Abeni 				      BIT(MPTCP_RETRANSMIT) | \
3417e9d09bacSPaolo Abeni 				      BIT(MPTCP_FLUSH_JOIN_LIST))
3418e9d09bacSPaolo Abeni 
3419e93da928SPaolo Abeni /* processes deferred events and flush wmem */
mptcp_release_cb(struct sock * sk)342014c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk)
3421e9d09bacSPaolo Abeni 	__must_hold(&sk->sk_lock.slock)
342214c441b5SPaolo Abeni {
3423e9d09bacSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
34242d6f5a2bSPaolo Abeni 
3425e9d09bacSPaolo Abeni 	for (;;) {
3426877a72e3SPaolo Abeni 		unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
342756a666c4SPaolo Abeni 		struct list_head join_list;
342856a666c4SPaolo Abeni 
3429c2e6048fSPaolo Abeni 		if (!flags)
3430c2e6048fSPaolo Abeni 			break;
3431c2e6048fSPaolo Abeni 
343256a666c4SPaolo Abeni 		INIT_LIST_HEAD(&join_list);
343356a666c4SPaolo Abeni 		list_splice_init(&msk->join_list, &join_list);
343456a666c4SPaolo Abeni 
3435c2e6048fSPaolo Abeni 		/* the following actions acquire the subflow socket lock
34366e628cd3SPaolo Abeni 		 *
34376e628cd3SPaolo Abeni 		 * 1) can't be invoked in atomic scope
34386e628cd3SPaolo Abeni 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
34396e628cd3SPaolo Abeni 		 *    datapath acquires the msk socket spinlock while helding
34406e628cd3SPaolo Abeni 		 *    the subflow socket lock
34416e628cd3SPaolo Abeni 		 */
3442e9d09bacSPaolo Abeni 		msk->cb_flags &= ~flags;
34436e628cd3SPaolo Abeni 		spin_unlock_bh(&sk->sk_lock.slock);
344456a666c4SPaolo Abeni 
34453e501490SPaolo Abeni 		if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
344656a666c4SPaolo Abeni 			__mptcp_flush_join_list(sk, &join_list);
34472e5de7e0SDan Carpenter 		if (flags & BIT(MPTCP_PUSH_PENDING))
3448c2e6048fSPaolo Abeni 			__mptcp_push_pending(sk, 0);
34492d6f5a2bSPaolo Abeni 		if (flags & BIT(MPTCP_RETRANSMIT))
34502d6f5a2bSPaolo Abeni 			__mptcp_retrans(sk);
3451c2e6048fSPaolo Abeni 
3452c2e6048fSPaolo Abeni 		cond_resched();
34536e628cd3SPaolo Abeni 		spin_lock_bh(&sk->sk_lock.slock);
34546e628cd3SPaolo Abeni 	}
3455c2e6048fSPaolo Abeni 
345665a569b0SPaolo Abeni 	if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
345765a569b0SPaolo Abeni 		__mptcp_clean_una_wakeup(sk);
345817ebf8a4SXiang Yang 	if (unlikely(msk->cb_flags)) {
3459cf742d09SPaolo Abeni 		/* be sure to sync the msk state before taking actions
346044ee4764SPaolo Abeni 		 * depending on sk_state (MPTCP_ERROR_REPORT)
346144ee4764SPaolo Abeni 		 * On sk release avoid actions depending on the first subflow
3462490274b4SPaolo Abeni 		 */
3463cf742d09SPaolo Abeni 		if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
3464cf742d09SPaolo Abeni 			__mptcp_sync_state(sk, msk->pending_state);
3465e9d09bacSPaolo Abeni 		if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
346615cc1045SPaolo Abeni 			__mptcp_error_report(sk);
346734c7757aSPaolo Abeni 		if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
346834c7757aSPaolo Abeni 			__mptcp_sync_sndbuf(sk);
346965a569b0SPaolo Abeni 	}
34706e628cd3SPaolo Abeni 
347187952603SPaolo Abeni 	__mptcp_update_rmem(sk);
347214c441b5SPaolo Abeni }
347314c441b5SPaolo Abeni 
3474bcd97734SPaolo Abeni /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3475bcd97734SPaolo Abeni  * TCP can't schedule delack timer before the subflow is fully established.
3476bcd97734SPaolo Abeni  * MPTCP uses the delack timer to do 3rd ack retransmissions
3477bcd97734SPaolo Abeni  */
schedule_3rdack_retransmission(struct sock * ssk)3478bcd97734SPaolo Abeni static void schedule_3rdack_retransmission(struct sock *ssk)
3479bcd97734SPaolo Abeni {
3480bcd97734SPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(ssk);
3481bcd97734SPaolo Abeni 	struct tcp_sock *tp = tcp_sk(ssk);
3482bcd97734SPaolo Abeni 	unsigned long timeout;
3483bcd97734SPaolo Abeni 
3484bcd97734SPaolo Abeni 	if (mptcp_subflow_ctx(ssk)->fully_established)
3485bcd97734SPaolo Abeni 		return;
3486bcd97734SPaolo Abeni 
3487bcd97734SPaolo Abeni 	/* reschedule with a timeout above RTT, as we must look only for drop */
3488bcd97734SPaolo Abeni 	if (tp->srtt_us)
3489bcd97734SPaolo Abeni 		timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3490bcd97734SPaolo Abeni 	else
3491bcd97734SPaolo Abeni 		timeout = TCP_TIMEOUT_INIT;
3492bcd97734SPaolo Abeni 	timeout += jiffies;
3493bcd97734SPaolo Abeni 
3494bcd97734SPaolo Abeni 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3495bcd97734SPaolo Abeni 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3496bcd97734SPaolo Abeni 	icsk->icsk_ack.timeout = timeout;
3497bcd97734SPaolo Abeni 	sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3498bcd97734SPaolo Abeni }
3499bcd97734SPaolo Abeni 
mptcp_subflow_process_delegated(struct sock * ssk,long status)3500a5efdbceSPaolo Abeni void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3501b19bc294SPaolo Abeni {
3502b19bc294SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3503b19bc294SPaolo Abeni 	struct sock *sk = subflow->conn;
3504b19bc294SPaolo Abeni 
3505a5efdbceSPaolo Abeni 	if (status & BIT(MPTCP_DELEGATE_SEND)) {
3506b19bc294SPaolo Abeni 		mptcp_data_lock(sk);
3507b19bc294SPaolo Abeni 		if (!sock_owned_by_user(sk))
350873a0052aSGeliang Tang 			__mptcp_subflow_push_pending(sk, ssk, true);
3509b19bc294SPaolo Abeni 		else
3510e9d09bacSPaolo Abeni 			__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3511b19bc294SPaolo Abeni 		mptcp_data_unlock(sk);
3512bcd97734SPaolo Abeni 	}
351334c7757aSPaolo Abeni 	if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
351434c7757aSPaolo Abeni 		mptcp_data_lock(sk);
351534c7757aSPaolo Abeni 		if (!sock_owned_by_user(sk))
351634c7757aSPaolo Abeni 			__mptcp_sync_sndbuf(sk);
351734c7757aSPaolo Abeni 		else
351834c7757aSPaolo Abeni 			__set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
351934c7757aSPaolo Abeni 		mptcp_data_unlock(sk);
352034c7757aSPaolo Abeni 	}
3521a5efdbceSPaolo Abeni 	if (status & BIT(MPTCP_DELEGATE_ACK))
3522bcd97734SPaolo Abeni 		schedule_3rdack_retransmission(ssk);
3523b19bc294SPaolo Abeni }
3524b19bc294SPaolo Abeni 
mptcp_hash(struct sock * sk)35252c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk)
35262c5ebd00SPaolo Abeni {
35272c5ebd00SPaolo Abeni 	/* should never be called,
35282c5ebd00SPaolo Abeni 	 * we hash the TCP subflows not the master socket
35292c5ebd00SPaolo Abeni 	 */
35302c5ebd00SPaolo Abeni 	WARN_ON_ONCE(1);
35312c5ebd00SPaolo Abeni 	return 0;
35322c5ebd00SPaolo Abeni }
35332c5ebd00SPaolo Abeni 
mptcp_unhash(struct sock * sk)35342c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk)
35352c5ebd00SPaolo Abeni {
35362c5ebd00SPaolo Abeni 	/* called from sk_common_release(), but nothing to do here */
35372c5ebd00SPaolo Abeni }
35382c5ebd00SPaolo Abeni 
mptcp_get_port(struct sock * sk,unsigned short snum)3539cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum)
3540f870fa0bSMat Martineau {
3541f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
3542f870fa0bSMat Martineau 
35433d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3544cfb63e50SPaolo Abeni 	if (WARN_ON_ONCE(!msk->first))
3545cec37a6eSPeter Krystad 		return -EINVAL;
3546f870fa0bSMat Martineau 
3547cfb63e50SPaolo Abeni 	return inet_csk_get_port(msk->first, snum);
3548cec37a6eSPeter Krystad }
3549f870fa0bSMat Martineau 
mptcp_finish_connect(struct sock * ssk)3550cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk)
3551cec37a6eSPeter Krystad {
3552cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow;
3553cec37a6eSPeter Krystad 	struct mptcp_sock *msk;
3554cec37a6eSPeter Krystad 	struct sock *sk;
3555f870fa0bSMat Martineau 
3556cec37a6eSPeter Krystad 	subflow = mptcp_subflow_ctx(ssk);
3557cec37a6eSPeter Krystad 	sk = subflow->conn;
3558cec37a6eSPeter Krystad 	msk = mptcp_sk(sk);
3559cec37a6eSPeter Krystad 
35603d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3561648ef4b8SMat Martineau 
3562b3ea6b27SPaolo Abeni 	subflow->map_seq = subflow->iasn;
3563648ef4b8SMat Martineau 	subflow->map_subflow_seq = 1;
35646d0060f6SMat Martineau 
3565cec37a6eSPeter Krystad 	/* the socket is not connected yet, no msk/subflow ops can access/race
3566cec37a6eSPeter Krystad 	 * accessing the field below
3567cec37a6eSPeter Krystad 	 */
3568cec37a6eSPeter Krystad 	WRITE_ONCE(msk->local_key, subflow->local_key);
35691b1c7a0eSPeter Krystad 
35706c714f1bSFlorian Westphal 	mptcp_pm_new_connection(msk, ssk, 0);
3571f870fa0bSMat Martineau }
3572f870fa0bSMat Martineau 
mptcp_sock_graft(struct sock * sk,struct socket * parent)3573866f26f2SPaolo Abeni void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3574cf7da0d6SPeter Krystad {
3575cf7da0d6SPeter Krystad 	write_lock_bh(&sk->sk_callback_lock);
3576cf7da0d6SPeter Krystad 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
3577cf7da0d6SPeter Krystad 	sk_set_socket(sk, parent);
3578cf7da0d6SPeter Krystad 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
3579cf7da0d6SPeter Krystad 	write_unlock_bh(&sk->sk_callback_lock);
3580cf7da0d6SPeter Krystad }
3581cf7da0d6SPeter Krystad 
mptcp_finish_join(struct sock * ssk)3582e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk)
3583f296234cSPeter Krystad {
3584e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3585f296234cSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3586f296234cSPeter Krystad 	struct sock *parent = (void *)msk;
35873e501490SPaolo Abeni 	bool ret = true;
3588f296234cSPeter Krystad 
35893d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3590f296234cSPeter Krystad 
3591f296234cSPeter Krystad 	/* mptcp socket already closing? */
3592dc87efdbSFlorian Westphal 	if (!mptcp_is_fully_established(parent)) {
3593dc87efdbSFlorian Westphal 		subflow->reset_reason = MPTCP_RST_EMPTCP;
3594f296234cSPeter Krystad 		return false;
3595dc87efdbSFlorian Westphal 	}
3596f296234cSPeter Krystad 
359755b47ca7SPaolo Abeni 	/* active subflow, already present inside the conn_list */
359855b47ca7SPaolo Abeni 	if (!list_empty(&subflow->node)) {
359955b47ca7SPaolo Abeni 		mptcp_subflow_joined(msk, ssk);
360034c7757aSPaolo Abeni 		mptcp_propagate_sndbuf(parent, ssk);
360155b47ca7SPaolo Abeni 		return true;
360255b47ca7SPaolo Abeni 	}
3603f296234cSPeter Krystad 
36043e501490SPaolo Abeni 	if (!mptcp_pm_allow_new_subflow(msk))
36053e501490SPaolo Abeni 		goto err_prohibited;
360610f6d46cSPaolo Abeni 
360755b47ca7SPaolo Abeni 	/* If we can't acquire msk socket lock here, let the release callback
36083e501490SPaolo Abeni 	 * handle it
360910f6d46cSPaolo Abeni 	 */
36103e501490SPaolo Abeni 	mptcp_data_lock(parent);
36113e501490SPaolo Abeni 	if (!sock_owned_by_user(parent)) {
36123e501490SPaolo Abeni 		ret = __mptcp_finish_join(msk, ssk);
36133e501490SPaolo Abeni 		if (ret) {
3614e16163b6SPaolo Abeni 			sock_hold(ssk);
36153e501490SPaolo Abeni 			list_add_tail(&subflow->node, &msk->conn_list);
3616e16163b6SPaolo Abeni 		}
36173e501490SPaolo Abeni 	} else {
36183e501490SPaolo Abeni 		sock_hold(ssk);
36193e501490SPaolo Abeni 		list_add_tail(&subflow->node, &msk->join_list);
3620e9d09bacSPaolo Abeni 		__set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
36213e501490SPaolo Abeni 	}
36223e501490SPaolo Abeni 	mptcp_data_unlock(parent);
36233e501490SPaolo Abeni 
3624dc87efdbSFlorian Westphal 	if (!ret) {
36253e501490SPaolo Abeni err_prohibited:
3626dc87efdbSFlorian Westphal 		subflow->reset_reason = MPTCP_RST_EPROHIBIT;
362710f6d46cSPaolo Abeni 		return false;
3628dc87efdbSFlorian Westphal 	}
362910f6d46cSPaolo Abeni 
363010f6d46cSPaolo Abeni 	return true;
3631f296234cSPeter Krystad }
3632f296234cSPeter Krystad 
mptcp_shutdown(struct sock * sk,int how)363376e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how)
363476e2a55dSPaolo Abeni {
36353d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("sk=%p, how=%d\n", sk, how);
363676e2a55dSPaolo Abeni 
363776e2a55dSPaolo Abeni 	if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
363876e2a55dSPaolo Abeni 		__mptcp_wr_shutdown(sk);
363976e2a55dSPaolo Abeni }
364076e2a55dSPaolo Abeni 
mptcp_forward_alloc_get(const struct sock * sk)36416511882cSPaolo Abeni static int mptcp_forward_alloc_get(const struct sock *sk)
36426511882cSPaolo Abeni {
36439531e4a8SEric Dumazet 	return READ_ONCE(sk->sk_forward_alloc) +
36449531e4a8SEric Dumazet 	       READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc);
36456511882cSPaolo Abeni }
36466511882cSPaolo Abeni 
mptcp_ioctl_outq(const struct mptcp_sock * msk,u64 v)3647644807e3SFlorian Westphal static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3648644807e3SFlorian Westphal {
3649644807e3SFlorian Westphal 	const struct sock *sk = (void *)msk;
3650644807e3SFlorian Westphal 	u64 delta;
3651644807e3SFlorian Westphal 
3652644807e3SFlorian Westphal 	if (sk->sk_state == TCP_LISTEN)
3653644807e3SFlorian Westphal 		return -EINVAL;
3654644807e3SFlorian Westphal 
3655644807e3SFlorian Westphal 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3656644807e3SFlorian Westphal 		return 0;
3657644807e3SFlorian Westphal 
3658644807e3SFlorian Westphal 	delta = msk->write_seq - v;
365907c2c7a3SPaolo Abeni 	if (__mptcp_check_fallback(msk) && msk->first) {
366007c2c7a3SPaolo Abeni 		struct tcp_sock *tp = tcp_sk(msk->first);
366107c2c7a3SPaolo Abeni 
366207c2c7a3SPaolo Abeni 		/* the first subflow is disconnected after close - see
366307c2c7a3SPaolo Abeni 		 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
366407c2c7a3SPaolo Abeni 		 * so ignore that status, too.
366507c2c7a3SPaolo Abeni 		 */
366607c2c7a3SPaolo Abeni 		if (!((1 << msk->first->sk_state) &
366707c2c7a3SPaolo Abeni 		      (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
366807c2c7a3SPaolo Abeni 			delta += READ_ONCE(tp->write_seq) - tp->snd_una;
366907c2c7a3SPaolo Abeni 	}
3670644807e3SFlorian Westphal 	if (delta > INT_MAX)
3671644807e3SFlorian Westphal 		delta = INT_MAX;
3672644807e3SFlorian Westphal 
3673644807e3SFlorian Westphal 	return (int)delta;
3674644807e3SFlorian Westphal }
3675644807e3SFlorian Westphal 
mptcp_ioctl(struct sock * sk,int cmd,int * karg)3676e1d001faSBreno Leitao static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3677644807e3SFlorian Westphal {
3678644807e3SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
3679644807e3SFlorian Westphal 	bool slow;
3680644807e3SFlorian Westphal 
3681644807e3SFlorian Westphal 	switch (cmd) {
3682644807e3SFlorian Westphal 	case SIOCINQ:
3683644807e3SFlorian Westphal 		if (sk->sk_state == TCP_LISTEN)
3684644807e3SFlorian Westphal 			return -EINVAL;
3685644807e3SFlorian Westphal 
3686644807e3SFlorian Westphal 		lock_sock(sk);
3687644807e3SFlorian Westphal 		__mptcp_move_skbs(msk);
3688e1d001faSBreno Leitao 		*karg = mptcp_inq_hint(sk);
3689644807e3SFlorian Westphal 		release_sock(sk);
3690644807e3SFlorian Westphal 		break;
3691644807e3SFlorian Westphal 	case SIOCOUTQ:
3692644807e3SFlorian Westphal 		slow = lock_sock_fast(sk);
3693e1d001faSBreno Leitao 		*karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3694644807e3SFlorian Westphal 		unlock_sock_fast(sk, slow);
3695644807e3SFlorian Westphal 		break;
3696644807e3SFlorian Westphal 	case SIOCOUTQNSD:
3697644807e3SFlorian Westphal 		slow = lock_sock_fast(sk);
3698e1d001faSBreno Leitao 		*karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3699644807e3SFlorian Westphal 		unlock_sock_fast(sk, slow);
3700644807e3SFlorian Westphal 		break;
3701644807e3SFlorian Westphal 	default:
3702644807e3SFlorian Westphal 		return -ENOIOCTLCMD;
3703644807e3SFlorian Westphal 	}
3704644807e3SFlorian Westphal 
3705e1d001faSBreno Leitao 	return 0;
3706644807e3SFlorian Westphal }
3707644807e3SFlorian Westphal 
mptcp_subflow_early_fallback(struct mptcp_sock * msk,struct mptcp_subflow_context * subflow)370854f1944eSPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
370954f1944eSPaolo Abeni 					 struct mptcp_subflow_context *subflow)
371054f1944eSPaolo Abeni {
371154f1944eSPaolo Abeni 	subflow->request_mptcp = 0;
371254f1944eSPaolo Abeni 	__mptcp_do_fallback(msk);
371354f1944eSPaolo Abeni }
371454f1944eSPaolo Abeni 
mptcp_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)371554f1944eSPaolo Abeni static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
371654f1944eSPaolo Abeni {
371754f1944eSPaolo Abeni 	struct mptcp_subflow_context *subflow;
371854f1944eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
371954f1944eSPaolo Abeni 	int err = -EINVAL;
3720ccae357cSPaolo Abeni 	struct sock *ssk;
372154f1944eSPaolo Abeni 
37223f326a82SPaolo Abeni 	ssk = __mptcp_nmpc_sk(msk);
37233f326a82SPaolo Abeni 	if (IS_ERR(ssk))
37243f326a82SPaolo Abeni 		return PTR_ERR(ssk);
372554f1944eSPaolo Abeni 
3726d52b3c2bSGeliang Tang 	mptcp_set_state(sk, TCP_SYN_SENT);
3727ccae357cSPaolo Abeni 	subflow = mptcp_subflow_ctx(ssk);
372854f1944eSPaolo Abeni #ifdef CONFIG_TCP_MD5SIG
372954f1944eSPaolo Abeni 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
373054f1944eSPaolo Abeni 	 * TCP option space.
373154f1944eSPaolo Abeni 	 */
3732ccae357cSPaolo Abeni 	if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
373354f1944eSPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
373454f1944eSPaolo Abeni #endif
3735ccae357cSPaolo Abeni 	if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) {
3736ccae357cSPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
373754f1944eSPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
373854f1944eSPaolo Abeni 	}
3739aa0c07c1SPaolo Abeni 
3740aa0c07c1SPaolo Abeni 	WRITE_ONCE(msk->write_seq, subflow->idsn);
3741aa0c07c1SPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3742f1f0a46fSPaolo Abeni 	WRITE_ONCE(msk->snd_una, subflow->idsn);
374354f1944eSPaolo Abeni 	if (likely(!__mptcp_check_fallback(msk)))
374454f1944eSPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
374554f1944eSPaolo Abeni 
374654f1944eSPaolo Abeni 	/* if reaching here via the fastopen/sendmsg path, the caller already
374754f1944eSPaolo Abeni 	 * acquired the subflow socket lock, too.
374854f1944eSPaolo Abeni 	 */
3749ccae357cSPaolo Abeni 	if (!msk->fastopening)
3750ccae357cSPaolo Abeni 		lock_sock(ssk);
3751ccae357cSPaolo Abeni 
3752ccae357cSPaolo Abeni 	/* the following mirrors closely a very small chunk of code from
3753ccae357cSPaolo Abeni 	 * __inet_stream_connect()
3754ccae357cSPaolo Abeni 	 */
3755ccae357cSPaolo Abeni 	if (ssk->sk_state != TCP_CLOSE)
3756ccae357cSPaolo Abeni 		goto out;
3757ccae357cSPaolo Abeni 
3758ccae357cSPaolo Abeni 	if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3759ccae357cSPaolo Abeni 		err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3760ccae357cSPaolo Abeni 		if (err)
3761ccae357cSPaolo Abeni 			goto out;
3762ccae357cSPaolo Abeni 	}
3763ccae357cSPaolo Abeni 
3764ccae357cSPaolo Abeni 	err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3765ccae357cSPaolo Abeni 	if (err < 0)
3766ccae357cSPaolo Abeni 		goto out;
3767ccae357cSPaolo Abeni 
376808e39c0dSEric Dumazet 	inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3769ccae357cSPaolo Abeni 
3770ccae357cSPaolo Abeni out:
3771ccae357cSPaolo Abeni 	if (!msk->fastopening)
3772ccae357cSPaolo Abeni 		release_sock(ssk);
377354f1944eSPaolo Abeni 
377454f1944eSPaolo Abeni 	/* on successful connect, the msk state will be moved to established by
377554f1944eSPaolo Abeni 	 * subflow_finish_connect()
377654f1944eSPaolo Abeni 	 */
3777ccae357cSPaolo Abeni 	if (unlikely(err)) {
3778131a6277SPaolo Abeni 		/* avoid leaving a dangling token in an unconnected socket */
3779131a6277SPaolo Abeni 		mptcp_token_destroy(msk);
3780d52b3c2bSGeliang Tang 		mptcp_set_state(sk, TCP_CLOSE);
378154f1944eSPaolo Abeni 		return err;
378254f1944eSPaolo Abeni 	}
378354f1944eSPaolo Abeni 
3784ccae357cSPaolo Abeni 	mptcp_copy_inaddrs(sk, ssk);
3785786fc124SPaolo Abeni 	return 0;
378654f1944eSPaolo Abeni }
378754f1944eSPaolo Abeni 
3788f870fa0bSMat Martineau static struct proto mptcp_prot = {
3789f870fa0bSMat Martineau 	.name		= "MPTCP",
3790f870fa0bSMat Martineau 	.owner		= THIS_MODULE,
3791f870fa0bSMat Martineau 	.init		= mptcp_init_sock,
379254f1944eSPaolo Abeni 	.connect	= mptcp_connect,
379318b683bfSPaolo Abeni 	.disconnect	= mptcp_disconnect,
3794f870fa0bSMat Martineau 	.close		= mptcp_close,
3795717e79c8SPeter Krystad 	.setsockopt	= mptcp_setsockopt,
3796717e79c8SPeter Krystad 	.getsockopt	= mptcp_getsockopt,
379776e2a55dSPaolo Abeni 	.shutdown	= mptcp_shutdown,
379879c0949eSPeter Krystad 	.destroy	= mptcp_destroy,
3799f870fa0bSMat Martineau 	.sendmsg	= mptcp_sendmsg,
3800644807e3SFlorian Westphal 	.ioctl		= mptcp_ioctl,
3801f870fa0bSMat Martineau 	.recvmsg	= mptcp_recvmsg,
380214c441b5SPaolo Abeni 	.release_cb	= mptcp_release_cb,
38032c5ebd00SPaolo Abeni 	.hash		= mptcp_hash,
38042c5ebd00SPaolo Abeni 	.unhash		= mptcp_unhash,
3805cec37a6eSPeter Krystad 	.get_port	= mptcp_get_port,
38066511882cSPaolo Abeni 	.forward_alloc_get	= mptcp_forward_alloc_get,
3807d027236cSPaolo Abeni 	.sockets_allocated	= &mptcp_sockets_allocated,
38080defbb0aSEric Dumazet 
3809d027236cSPaolo Abeni 	.memory_allocated	= &tcp_memory_allocated,
38100defbb0aSEric Dumazet 	.per_cpu_fw_alloc	= &tcp_memory_per_cpu_fw_alloc,
38110defbb0aSEric Dumazet 
3812d027236cSPaolo Abeni 	.memory_pressure	= &tcp_memory_pressure,
3813d027236cSPaolo Abeni 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
3814989ef49bSPaolo Abeni 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
3815d027236cSPaolo Abeni 	.sysctl_mem	= sysctl_tcp_mem,
3816f870fa0bSMat Martineau 	.obj_size	= sizeof(struct mptcp_sock),
38172c5ebd00SPaolo Abeni 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
3818f870fa0bSMat Martineau 	.no_autobind	= true,
3819f870fa0bSMat Martineau };
3820f870fa0bSMat Martineau 
mptcp_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)38212303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
38222303f994SPeter Krystad {
38232303f994SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
38248cf2ebdcSPaolo Abeni 	struct sock *ssk, *sk = sock->sk;
38258cf2ebdcSPaolo Abeni 	int err = -EINVAL;
38262303f994SPeter Krystad 
38278cf2ebdcSPaolo Abeni 	lock_sock(sk);
38283f326a82SPaolo Abeni 	ssk = __mptcp_nmpc_sk(msk);
38293f326a82SPaolo Abeni 	if (IS_ERR(ssk)) {
38303f326a82SPaolo Abeni 		err = PTR_ERR(ssk);
38312303f994SPeter Krystad 		goto unlock;
38322303f994SPeter Krystad 	}
38332303f994SPeter Krystad 
38348cf2ebdcSPaolo Abeni 	if (sk->sk_family == AF_INET)
38358cf2ebdcSPaolo Abeni 		err = inet_bind_sk(ssk, uaddr, addr_len);
38368cf2ebdcSPaolo Abeni #if IS_ENABLED(CONFIG_MPTCP_IPV6)
38378cf2ebdcSPaolo Abeni 	else if (sk->sk_family == AF_INET6)
38388cf2ebdcSPaolo Abeni 		err = inet6_bind_sk(ssk, uaddr, addr_len);
38398cf2ebdcSPaolo Abeni #endif
3840cf7da0d6SPeter Krystad 	if (!err)
38418cf2ebdcSPaolo Abeni 		mptcp_copy_inaddrs(sk, ssk);
38422303f994SPeter Krystad 
38432303f994SPeter Krystad unlock:
38448cf2ebdcSPaolo Abeni 	release_sock(sk);
38452303f994SPeter Krystad 	return err;
38462303f994SPeter Krystad }
38472303f994SPeter Krystad 
mptcp_listen(struct socket * sock,int backlog)3848cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog)
3849cf7da0d6SPeter Krystad {
3850cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3851cfdcfeedSMenglong Dong 	struct sock *sk = sock->sk;
385240f56d0cSPaolo Abeni 	struct sock *ssk;
3853cf7da0d6SPeter Krystad 	int err;
3854cf7da0d6SPeter Krystad 
38553d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p\n", msk);
3856cf7da0d6SPeter Krystad 
3857cfdcfeedSMenglong Dong 	lock_sock(sk);
38580226436aSPaolo Abeni 
38590226436aSPaolo Abeni 	err = -EINVAL;
38600226436aSPaolo Abeni 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
38610226436aSPaolo Abeni 		goto unlock;
38620226436aSPaolo Abeni 
38633f326a82SPaolo Abeni 	ssk = __mptcp_nmpc_sk(msk);
38643f326a82SPaolo Abeni 	if (IS_ERR(ssk)) {
38653f326a82SPaolo Abeni 		err = PTR_ERR(ssk);
3866cf7da0d6SPeter Krystad 		goto unlock;
3867cf7da0d6SPeter Krystad 	}
3868cf7da0d6SPeter Krystad 
3869d52b3c2bSGeliang Tang 	mptcp_set_state(sk, TCP_LISTEN);
3870cfdcfeedSMenglong Dong 	sock_set_flag(sk, SOCK_RCU_FREE);
38715e20087dSFlorian Westphal 
387240f56d0cSPaolo Abeni 	lock_sock(ssk);
387340f56d0cSPaolo Abeni 	err = __inet_listen_sk(ssk, backlog);
387440f56d0cSPaolo Abeni 	release_sock(ssk);
3875d52b3c2bSGeliang Tang 	mptcp_set_state(sk, inet_sk_state_load(ssk));
387640f56d0cSPaolo Abeni 
3877c558246eSMenglong Dong 	if (!err) {
3878c558246eSMenglong Dong 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
387940f56d0cSPaolo Abeni 		mptcp_copy_inaddrs(sk, ssk);
388040f56d0cSPaolo Abeni 		mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
388121d9b73aSPaolo Abeni 	}
3882f8c9dfbdSGeliang Tang 
3883cf7da0d6SPeter Krystad unlock:
3884cfdcfeedSMenglong Dong 	release_sock(sk);
3885cf7da0d6SPeter Krystad 	return err;
3886cf7da0d6SPeter Krystad }
3887cf7da0d6SPeter Krystad 
mptcp_stream_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)3888cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3889cf7da0d6SPeter Krystad 			       int flags, bool kern)
3890cf7da0d6SPeter Krystad {
3891cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
38921f6610b9SPaolo Abeni 	struct sock *ssk, *newsk;
3893cf7da0d6SPeter Krystad 	int err;
3894cf7da0d6SPeter Krystad 
38953d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p\n", msk);
3896cf7da0d6SPeter Krystad 
38975b825727SPaolo Abeni 	/* Buggy applications can call accept on socket states other then LISTEN
389861761231SPaolo Abeni 	 * but no need to allocate the first subflow just to error out.
389961761231SPaolo Abeni 	 */
39001f6610b9SPaolo Abeni 	ssk = READ_ONCE(msk->first);
39011f6610b9SPaolo Abeni 	if (!ssk)
390271ba088cSPaolo Abeni 		return -EINVAL;
3903cf7da0d6SPeter Krystad 
39043d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
390512f353faSDavide Caratti 	newsk = inet_csk_accept(ssk, flags, &err, kern);
3906e76c8ef5SPaolo Abeni 	if (!newsk)
3907e76c8ef5SPaolo Abeni 		return err;
3908e76c8ef5SPaolo Abeni 
39093d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
391012f353faSDavide Caratti 	if (sk_is_mptcp(newsk)) {
3911cf7da0d6SPeter Krystad 		struct mptcp_subflow_context *subflow;
391212f353faSDavide Caratti 		struct sock *new_mptcp_sock;
391312f353faSDavide Caratti 
391412f353faSDavide Caratti 		subflow = mptcp_subflow_ctx(newsk);
391512f353faSDavide Caratti 		new_mptcp_sock = subflow->conn;
391612f353faSDavide Caratti 
391712f353faSDavide Caratti 		/* is_mptcp should be false if subflow->conn is missing, see
391812f353faSDavide Caratti 		 * subflow_syn_recv_sock()
391912f353faSDavide Caratti 		 */
392012f353faSDavide Caratti 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
392112f353faSDavide Caratti 			tcp_sk(newsk)->is_mptcp = 0;
392212f353faSDavide Caratti 			goto tcpfallback;
392312f353faSDavide Caratti 		}
392412f353faSDavide Caratti 
392512f353faSDavide Caratti 		newsk = new_mptcp_sock;
392612f353faSDavide Caratti 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
392712f353faSDavide Caratti 
392812f353faSDavide Caratti 		newsk->sk_kern_sock = kern;
392912f353faSDavide Caratti 		lock_sock(newsk);
393012f353faSDavide Caratti 		__inet_accept(sock, newsock, newsk);
39310397c6d8SPaolo Abeni 
3932a5ef058dSPaolo Abeni 		set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
393312f353faSDavide Caratti 		msk = mptcp_sk(newsk);
3934b6985b9bSPaolo Abeni 		msk->in_accept_queue = 0;
3935a5ef058dSPaolo Abeni 
3936cf7da0d6SPeter Krystad 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
3937cf7da0d6SPeter Krystad 		 * This is needed so NOSPACE flag can be set from tcp stack.
3938cf7da0d6SPeter Krystad 		 */
3939190f8b06SGeliang Tang 		mptcp_for_each_subflow(msk, subflow) {
3940cf7da0d6SPeter Krystad 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3941cf7da0d6SPeter Krystad 
3942cf7da0d6SPeter Krystad 			if (!ssk->sk_socket)
3943cf7da0d6SPeter Krystad 				mptcp_sock_graft(ssk, newsock);
3944cf7da0d6SPeter Krystad 		}
394563740448SPaolo Abeni 
394663740448SPaolo Abeni 		/* Do late cleanup for the first subflow as necessary. Also
394763740448SPaolo Abeni 		 * deal with bad peers not doing a complete shutdown.
394863740448SPaolo Abeni 		 */
394939880bd8SPaolo Abeni 		if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
395063740448SPaolo Abeni 			__mptcp_close_ssk(newsk, msk->first,
395163740448SPaolo Abeni 					  mptcp_subflow_ctx(msk->first), 0);
395239880bd8SPaolo Abeni 			if (unlikely(list_is_singular(&msk->conn_list)))
3953d52b3c2bSGeliang Tang 				mptcp_set_state(newsk, TCP_CLOSE);
395463740448SPaolo Abeni 		}
395512f353faSDavide Caratti 	} else {
395612f353faSDavide Caratti tcpfallback:
395712f353faSDavide Caratti 		newsk->sk_kern_sock = kern;
395812f353faSDavide Caratti 		lock_sock(newsk);
395912f353faSDavide Caratti 		__inet_accept(sock, newsock, newsk);
396012f353faSDavide Caratti 		/* we are being invoked after accepting a non-mp-capable
396112f353faSDavide Caratti 		 * flow: sk is a tcp_sk, not an mptcp one.
396212f353faSDavide Caratti 		 *
396312f353faSDavide Caratti 		 * Hand the socket over to tcp so all further socket ops
396412f353faSDavide Caratti 		 * bypass mptcp.
396512f353faSDavide Caratti 		 */
396612f353faSDavide Caratti 		WRITE_ONCE(newsock->sk->sk_socket->ops,
396712f353faSDavide Caratti 			   mptcp_fallback_tcp_ops(newsock->sk));
3968cf7da0d6SPeter Krystad 	}
3969e76c8ef5SPaolo Abeni 	release_sock(newsk);
3970cf7da0d6SPeter Krystad 
3971e76c8ef5SPaolo Abeni 	return 0;
3972cf7da0d6SPeter Krystad }
3973cf7da0d6SPeter Krystad 
mptcp_check_writeable(struct mptcp_sock * msk)39748edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
39758edf0864SFlorian Westphal {
39768edf0864SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
39778edf0864SFlorian Westphal 
39788edf0864SFlorian Westphal 	if (sk_stream_is_writeable(sk))
39798edf0864SFlorian Westphal 		return EPOLLOUT | EPOLLWRNORM;
39808edf0864SFlorian Westphal 
39815cf92bbaSPaolo Abeni 	mptcp_set_nospace(sk);
39826e628cd3SPaolo Abeni 	smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
39836e628cd3SPaolo Abeni 	if (sk_stream_is_writeable(sk))
39846e628cd3SPaolo Abeni 		return EPOLLOUT | EPOLLWRNORM;
39858edf0864SFlorian Westphal 
39866e628cd3SPaolo Abeni 	return 0;
39878edf0864SFlorian Westphal }
39888edf0864SFlorian Westphal 
mptcp_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)39892303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock,
39902303f994SPeter Krystad 			   struct poll_table_struct *wait)
39912303f994SPeter Krystad {
39921891c4a0SFlorian Westphal 	struct sock *sk = sock->sk;
39938ab183deSPaolo Abeni 	struct mptcp_sock *msk;
39942303f994SPeter Krystad 	__poll_t mask = 0;
39956b9831bfSPaolo Abeni 	u8 shutdown;
39968a05661bSPaolo Abeni 	int state;
39972303f994SPeter Krystad 
39981891c4a0SFlorian Westphal 	msk = mptcp_sk(sk);
39991891c4a0SFlorian Westphal 	sock_poll_wait(file, sock, wait);
40001891c4a0SFlorian Westphal 
40018a05661bSPaolo Abeni 	state = inet_sk_state_load(sk);
40023d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
400371ba088cSPaolo Abeni 	if (state == TCP_LISTEN) {
40045426a4efSPaolo Abeni 		struct sock *ssk = READ_ONCE(msk->first);
40055b825727SPaolo Abeni 
40065426a4efSPaolo Abeni 		if (WARN_ON_ONCE(!ssk))
400771ba088cSPaolo Abeni 			return 0;
400871ba088cSPaolo Abeni 
40095426a4efSPaolo Abeni 		return inet_csk_listen_poll(ssk);
401071ba088cSPaolo Abeni 	}
40118a05661bSPaolo Abeni 
40126b9831bfSPaolo Abeni 	shutdown = READ_ONCE(sk->sk_shutdown);
40136b9831bfSPaolo Abeni 	if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
40146b9831bfSPaolo Abeni 		mask |= EPOLLHUP;
40156b9831bfSPaolo Abeni 	if (shutdown & RCV_SHUTDOWN)
40166b9831bfSPaolo Abeni 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
40176b9831bfSPaolo Abeni 
40188a05661bSPaolo Abeni 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4019124a4885SPaolo Abeni 		mask |= mptcp_check_readable(sk);
40206b9831bfSPaolo Abeni 		if (shutdown & SEND_SHUTDOWN)
40216b9831bfSPaolo Abeni 			mask |= EPOLLOUT | EPOLLWRNORM;
40226b9831bfSPaolo Abeni 		else
40238edf0864SFlorian Westphal 			mask |= mptcp_check_writeable(msk);
402408e39c0dSEric Dumazet 	} else if (state == TCP_SYN_SENT &&
402508e39c0dSEric Dumazet 		   inet_test_bit(DEFER_CONNECT, sk)) {
4026a42cf9d1SBenjamin Hesmans 		/* cf tcp_poll() note about TFO */
4027a42cf9d1SBenjamin Hesmans 		mask |= EPOLLOUT | EPOLLWRNORM;
40288a05661bSPaolo Abeni 	}
40291891c4a0SFlorian Westphal 
4030d89e3ed7SPaolo Abeni 	/* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
403115cc1045SPaolo Abeni 	smp_rmb();
40329ae8e5adSEric Dumazet 	if (READ_ONCE(sk->sk_err))
403315cc1045SPaolo Abeni 		mask |= EPOLLERR;
403415cc1045SPaolo Abeni 
40352303f994SPeter Krystad 	return mask;
40362303f994SPeter Krystad }
40372303f994SPeter Krystad 
4038e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = {
4039e42f1ac6SFlorian Westphal 	.family		   = PF_INET,
4040e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
40410a3cc579SPaolo Abeni 	.release	   = inet_release,
4042e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
4043786fc124SPaolo Abeni 	.connect	   = inet_stream_connect,
4044e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
4045e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
4046d2f77c53SPaolo Abeni 	.getname	   = inet_getname,
4047e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
4048e42f1ac6SFlorian Westphal 	.ioctl		   = inet_ioctl,
4049e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
4050e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
405176e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
4052e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
4053e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
4054e42f1ac6SFlorian Westphal 	.sendmsg	   = inet_sendmsg,
4055e42f1ac6SFlorian Westphal 	.recvmsg	   = inet_recvmsg,
4056e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
4057124a4885SPaolo Abeni 	.set_rcvlowat	   = mptcp_set_rcvlowat,
4058e42f1ac6SFlorian Westphal };
40592303f994SPeter Krystad 
4060f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = {
4061f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
4062f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
4063f870fa0bSMat Martineau 	.prot		= &mptcp_prot,
40642303f994SPeter Krystad 	.ops		= &mptcp_stream_ops,
40652303f994SPeter Krystad 	.flags		= INET_PROTOSW_ICSK,
4066f870fa0bSMat Martineau };
4067f870fa0bSMat Martineau 
mptcp_napi_poll(struct napi_struct * napi,int budget)4068b19bc294SPaolo Abeni static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4069b19bc294SPaolo Abeni {
4070b19bc294SPaolo Abeni 	struct mptcp_delegated_action *delegated;
4071b19bc294SPaolo Abeni 	struct mptcp_subflow_context *subflow;
4072b19bc294SPaolo Abeni 	int work_done = 0;
4073b19bc294SPaolo Abeni 
4074b19bc294SPaolo Abeni 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
4075b19bc294SPaolo Abeni 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4076b19bc294SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4077b19bc294SPaolo Abeni 
4078b19bc294SPaolo Abeni 		bh_lock_sock_nested(ssk);
4079a5efdbceSPaolo Abeni 		if (!sock_owned_by_user(ssk)) {
4080a5efdbceSPaolo Abeni 			mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4081a5efdbceSPaolo Abeni 		} else {
4082a5efdbceSPaolo Abeni 			/* tcp_release_cb_override already processed
4083b19bc294SPaolo Abeni 			 * the action or will do at next release_sock().
4084b19bc294SPaolo Abeni 			 * In both case must dequeue the subflow here - on the same
4085b19bc294SPaolo Abeni 			 * CPU that scheduled it.
4086b19bc294SPaolo Abeni 			 */
4087a5efdbceSPaolo Abeni 			smp_wmb();
4088a5efdbceSPaolo Abeni 			clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4089a5efdbceSPaolo Abeni 		}
4090b19bc294SPaolo Abeni 		bh_unlock_sock(ssk);
4091b19bc294SPaolo Abeni 		sock_put(ssk);
4092b19bc294SPaolo Abeni 
4093b19bc294SPaolo Abeni 		if (++work_done == budget)
4094b19bc294SPaolo Abeni 			return budget;
4095b19bc294SPaolo Abeni 	}
4096b19bc294SPaolo Abeni 
4097b19bc294SPaolo Abeni 	/* always provide a 0 'work_done' argument, so that napi_complete_done
4098b19bc294SPaolo Abeni 	 * will not try accessing the NULL napi->dev ptr
4099b19bc294SPaolo Abeni 	 */
4100b19bc294SPaolo Abeni 	napi_complete_done(napi, 0);
4101b19bc294SPaolo Abeni 	return work_done;
4102b19bc294SPaolo Abeni }
4103b19bc294SPaolo Abeni 
mptcp_proto_init(void)4104d39dcecaSPaolo Abeni void __init mptcp_proto_init(void)
4105f870fa0bSMat Martineau {
4106b19bc294SPaolo Abeni 	struct mptcp_delegated_action *delegated;
4107b19bc294SPaolo Abeni 	int cpu;
4108b19bc294SPaolo Abeni 
41092303f994SPeter Krystad 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
41102303f994SPeter Krystad 
4111d027236cSPaolo Abeni 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4112d027236cSPaolo Abeni 		panic("Failed to allocate MPTCP pcpu counter\n");
4113d027236cSPaolo Abeni 
4114b19bc294SPaolo Abeni 	init_dummy_netdev(&mptcp_napi_dev);
4115b19bc294SPaolo Abeni 	for_each_possible_cpu(cpu) {
4116b19bc294SPaolo Abeni 		delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4117b19bc294SPaolo Abeni 		INIT_LIST_HEAD(&delegated->head);
411816d083e2SJakub Kicinski 		netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
411916d083e2SJakub Kicinski 				  mptcp_napi_poll);
4120b19bc294SPaolo Abeni 		napi_enable(&delegated->napi);
4121b19bc294SPaolo Abeni 	}
4122b19bc294SPaolo Abeni 
41232303f994SPeter Krystad 	mptcp_subflow_init();
41241b1c7a0eSPeter Krystad 	mptcp_pm_init();
4125ed1ad86bSGeliang Tang 	mptcp_sched_init();
41262c5ebd00SPaolo Abeni 	mptcp_token_init();
41272303f994SPeter Krystad 
4128f870fa0bSMat Martineau 	if (proto_register(&mptcp_prot, 1) != 0)
4129f870fa0bSMat Martineau 		panic("Failed to register MPTCP proto.\n");
4130f870fa0bSMat Martineau 
4131f870fa0bSMat Martineau 	inet_register_protosw(&mptcp_protosw);
41326771bfd9SFlorian Westphal 
41336771bfd9SFlorian Westphal 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4134f870fa0bSMat Martineau }
4135f870fa0bSMat Martineau 
4136f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4137e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = {
4138e42f1ac6SFlorian Westphal 	.family		   = PF_INET6,
4139e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
41400a3cc579SPaolo Abeni 	.release	   = inet6_release,
4141e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
4142786fc124SPaolo Abeni 	.connect	   = inet_stream_connect,
4143e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
4144e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
4145d2f77c53SPaolo Abeni 	.getname	   = inet6_getname,
4146e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
4147e42f1ac6SFlorian Westphal 	.ioctl		   = inet6_ioctl,
4148e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
4149e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
415076e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
4151e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
4152e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
4153e42f1ac6SFlorian Westphal 	.sendmsg	   = inet6_sendmsg,
4154e42f1ac6SFlorian Westphal 	.recvmsg	   = inet6_recvmsg,
4155e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
4156e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT
41573986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
4158e42f1ac6SFlorian Westphal #endif
4159124a4885SPaolo Abeni 	.set_rcvlowat	   = mptcp_set_rcvlowat,
4160e42f1ac6SFlorian Westphal };
4161e42f1ac6SFlorian Westphal 
4162f870fa0bSMat Martineau static struct proto mptcp_v6_prot;
4163f870fa0bSMat Martineau 
4164f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = {
4165f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
4166f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
4167f870fa0bSMat Martineau 	.prot		= &mptcp_v6_prot,
41682303f994SPeter Krystad 	.ops		= &mptcp_v6_stream_ops,
4169f870fa0bSMat Martineau 	.flags		= INET_PROTOSW_ICSK,
4170f870fa0bSMat Martineau };
4171f870fa0bSMat Martineau 
mptcp_proto_v6_init(void)4172d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void)
4173f870fa0bSMat Martineau {
4174f870fa0bSMat Martineau 	int err;
4175f870fa0bSMat Martineau 
4176f870fa0bSMat Martineau 	mptcp_v6_prot = mptcp_prot;
4177f870fa0bSMat Martineau 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
4178f870fa0bSMat Martineau 	mptcp_v6_prot.slab = NULL;
4179b0519de8SFlorian Westphal 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4180f5f80e32SEric Dumazet 	mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4181f870fa0bSMat Martineau 
4182f870fa0bSMat Martineau 	err = proto_register(&mptcp_v6_prot, 1);
4183f870fa0bSMat Martineau 	if (err)
4184f870fa0bSMat Martineau 		return err;
4185f870fa0bSMat Martineau 
4186f870fa0bSMat Martineau 	err = inet6_register_protosw(&mptcp_v6_protosw);
4187f870fa0bSMat Martineau 	if (err)
4188f870fa0bSMat Martineau 		proto_unregister(&mptcp_v6_prot);
4189f870fa0bSMat Martineau 
4190f870fa0bSMat Martineau 	return err;
4191f870fa0bSMat Martineau }
4192f870fa0bSMat Martineau #endif
4193