xref: /openbmc/linux/net/mptcp/protocol.c (revision 15cc10453398c22f78f6c2b897119ecce5e5dd89)
1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0
2f870fa0bSMat Martineau /* Multipath TCP
3f870fa0bSMat Martineau  *
4f870fa0bSMat Martineau  * Copyright (c) 2017 - 2019, Intel Corporation.
5f870fa0bSMat Martineau  */
6f870fa0bSMat Martineau 
7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt
8f870fa0bSMat Martineau 
9f870fa0bSMat Martineau #include <linux/kernel.h>
10f870fa0bSMat Martineau #include <linux/module.h>
11f870fa0bSMat Martineau #include <linux/netdevice.h>
127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h>
137a6a6cbcSPaolo Abeni #include <linux/atomic.h>
14f870fa0bSMat Martineau #include <net/sock.h>
15f870fa0bSMat Martineau #include <net/inet_common.h>
16f870fa0bSMat Martineau #include <net/inet_hashtables.h>
17f870fa0bSMat Martineau #include <net/protocol.h>
18f870fa0bSMat Martineau #include <net/tcp.h>
193721b9b6SMat Martineau #include <net/tcp_states.h>
20cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21cf7da0d6SPeter Krystad #include <net/transp_v6.h>
22cf7da0d6SPeter Krystad #endif
23f870fa0bSMat Martineau #include <net/mptcp.h>
24e16163b6SPaolo Abeni #include <net/xfrm.h>
25f870fa0bSMat Martineau #include "protocol.h"
26fc518953SFlorian Westphal #include "mib.h"
27f870fa0bSMat Martineau 
28b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
29b0519de8SFlorian Westphal struct mptcp6_sock {
30b0519de8SFlorian Westphal 	struct mptcp_sock msk;
31b0519de8SFlorian Westphal 	struct ipv6_pinfo np;
32b0519de8SFlorian Westphal };
33b0519de8SFlorian Westphal #endif
34b0519de8SFlorian Westphal 
356771bfd9SFlorian Westphal struct mptcp_skb_cb {
36ab174ad8SPaolo Abeni 	u64 map_seq;
37ab174ad8SPaolo Abeni 	u64 end_seq;
386771bfd9SFlorian Westphal 	u32 offset;
396771bfd9SFlorian Westphal };
406771bfd9SFlorian Westphal 
416771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
426771bfd9SFlorian Westphal 
43d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated;
44d027236cSPaolo Abeni 
45e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk);
46d9ca1de8SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk);
47e16163b6SPaolo Abeni 
482303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
492303f994SPeter Krystad  * completed yet or has failed, return the subflow socket.
502303f994SPeter Krystad  * Otherwise return NULL.
512303f994SPeter Krystad  */
522303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
532303f994SPeter Krystad {
54d22f4988SChristoph Paasch 	if (!msk->subflow || READ_ONCE(msk->can_ack))
552303f994SPeter Krystad 		return NULL;
562303f994SPeter Krystad 
572303f994SPeter Krystad 	return msk->subflow;
582303f994SPeter Krystad }
592303f994SPeter Krystad 
606f8a612aSFlorian Westphal /* Returns end sequence number of the receiver's advertised window */
616f8a612aSFlorian Westphal static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
626f8a612aSFlorian Westphal {
637439d687SPaolo Abeni 	return READ_ONCE(msk->wnd_end);
646f8a612aSFlorian Westphal }
656f8a612aSFlorian Westphal 
66d2f77c53SPaolo Abeni static bool mptcp_is_tcpsk(struct sock *sk)
670b4f33deSFlorian Westphal {
680b4f33deSFlorian Westphal 	struct socket *sock = sk->sk_socket;
690b4f33deSFlorian Westphal 
700b4f33deSFlorian Westphal 	if (unlikely(sk->sk_prot == &tcp_prot)) {
710b4f33deSFlorian Westphal 		/* we are being invoked after mptcp_accept() has
720b4f33deSFlorian Westphal 		 * accepted a non-mp-capable flow: sk is a tcp_sk,
730b4f33deSFlorian Westphal 		 * not an mptcp one.
740b4f33deSFlorian Westphal 		 *
750b4f33deSFlorian Westphal 		 * Hand the socket over to tcp so all further socket ops
760b4f33deSFlorian Westphal 		 * bypass mptcp.
770b4f33deSFlorian Westphal 		 */
780b4f33deSFlorian Westphal 		sock->ops = &inet_stream_ops;
79d2f77c53SPaolo Abeni 		return true;
800b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
810b4f33deSFlorian Westphal 	} else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
820b4f33deSFlorian Westphal 		sock->ops = &inet6_stream_ops;
83d2f77c53SPaolo Abeni 		return true;
840b4f33deSFlorian Westphal #endif
850b4f33deSFlorian Westphal 	}
860b4f33deSFlorian Westphal 
87d2f77c53SPaolo Abeni 	return false;
880b4f33deSFlorian Westphal }
890b4f33deSFlorian Westphal 
9076660afbSPaolo Abeni static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
91cec37a6eSPeter Krystad {
92cec37a6eSPeter Krystad 	sock_owned_by_me((const struct sock *)msk);
93cec37a6eSPeter Krystad 
94e1ff9e82SDavide Caratti 	if (likely(!__mptcp_check_fallback(msk)))
95cec37a6eSPeter Krystad 		return NULL;
96cec37a6eSPeter Krystad 
9776660afbSPaolo Abeni 	return msk->first;
988ab183deSPaolo Abeni }
998ab183deSPaolo Abeni 
100fa68018dSPaolo Abeni static int __mptcp_socket_create(struct mptcp_sock *msk)
1012303f994SPeter Krystad {
1022303f994SPeter Krystad 	struct mptcp_subflow_context *subflow;
1032303f994SPeter Krystad 	struct sock *sk = (struct sock *)msk;
1042303f994SPeter Krystad 	struct socket *ssock;
1052303f994SPeter Krystad 	int err;
1062303f994SPeter Krystad 
1072303f994SPeter Krystad 	err = mptcp_subflow_create_socket(sk, &ssock);
1082303f994SPeter Krystad 	if (err)
109fa68018dSPaolo Abeni 		return err;
1102303f994SPeter Krystad 
1118ab183deSPaolo Abeni 	msk->first = ssock->sk;
1122303f994SPeter Krystad 	msk->subflow = ssock;
1132303f994SPeter Krystad 	subflow = mptcp_subflow_ctx(ssock->sk);
114cec37a6eSPeter Krystad 	list_add(&subflow->node, &msk->conn_list);
115e16163b6SPaolo Abeni 	sock_hold(ssock->sk);
1162303f994SPeter Krystad 	subflow->request_mptcp = 1;
1172303f994SPeter Krystad 
118e1ff9e82SDavide Caratti 	/* accept() will wait on first subflow sk_wq, and we always wakes up
119e1ff9e82SDavide Caratti 	 * via msk->sk_socket
120e1ff9e82SDavide Caratti 	 */
121e1ff9e82SDavide Caratti 	RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq);
122e1ff9e82SDavide Caratti 
123fa68018dSPaolo Abeni 	return 0;
1242303f994SPeter Krystad }
1252303f994SPeter Krystad 
126ab174ad8SPaolo Abeni static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
127ab174ad8SPaolo Abeni {
128ab174ad8SPaolo Abeni 	sk_drops_add(sk, skb);
129ab174ad8SPaolo Abeni 	__kfree_skb(skb);
130ab174ad8SPaolo Abeni }
131ab174ad8SPaolo Abeni 
1328268ed4cSPaolo Abeni static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
1338268ed4cSPaolo Abeni 			       struct sk_buff *from)
1348268ed4cSPaolo Abeni {
1358268ed4cSPaolo Abeni 	bool fragstolen;
1368268ed4cSPaolo Abeni 	int delta;
1378268ed4cSPaolo Abeni 
1388268ed4cSPaolo Abeni 	if (MPTCP_SKB_CB(from)->offset ||
1398268ed4cSPaolo Abeni 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
1408268ed4cSPaolo Abeni 		return false;
1418268ed4cSPaolo Abeni 
14206242e44SPaolo Abeni 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
14306242e44SPaolo Abeni 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
14406242e44SPaolo Abeni 		 to->len, MPTCP_SKB_CB(from)->end_seq);
145ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
1468268ed4cSPaolo Abeni 	kfree_skb_partial(from, fragstolen);
1478268ed4cSPaolo Abeni 	atomic_add(delta, &sk->sk_rmem_alloc);
1488268ed4cSPaolo Abeni 	sk_mem_charge(sk, delta);
1498268ed4cSPaolo Abeni 	return true;
1508268ed4cSPaolo Abeni }
1518268ed4cSPaolo Abeni 
152ab174ad8SPaolo Abeni static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
153ab174ad8SPaolo Abeni 				   struct sk_buff *from)
1546771bfd9SFlorian Westphal {
155ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
156ab174ad8SPaolo Abeni 		return false;
157ab174ad8SPaolo Abeni 
158ab174ad8SPaolo Abeni 	return mptcp_try_coalesce((struct sock *)msk, to, from);
159ab174ad8SPaolo Abeni }
160ab174ad8SPaolo Abeni 
161ab174ad8SPaolo Abeni /* "inspired" by tcp_data_queue_ofo(), main differences:
162ab174ad8SPaolo Abeni  * - use mptcp seqs
163ab174ad8SPaolo Abeni  * - don't cope with sacks
164ab174ad8SPaolo Abeni  */
165ab174ad8SPaolo Abeni static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
166ab174ad8SPaolo Abeni {
167ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
168ab174ad8SPaolo Abeni 	struct rb_node **p, *parent;
169ab174ad8SPaolo Abeni 	u64 seq, end_seq, max_seq;
170ab174ad8SPaolo Abeni 	struct sk_buff *skb1;
171ab174ad8SPaolo Abeni 
172ab174ad8SPaolo Abeni 	seq = MPTCP_SKB_CB(skb)->map_seq;
173ab174ad8SPaolo Abeni 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
174fa3fe2b1SFlorian Westphal 	max_seq = READ_ONCE(msk->rcv_wnd_sent);
175ab174ad8SPaolo Abeni 
17606242e44SPaolo Abeni 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
17706242e44SPaolo Abeni 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
178fa3fe2b1SFlorian Westphal 	if (after64(end_seq, max_seq)) {
179ab174ad8SPaolo Abeni 		/* out of window */
180ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb);
181fa3fe2b1SFlorian Westphal 		pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
182fa3fe2b1SFlorian Westphal 			 (unsigned long long)end_seq - (unsigned long)max_seq,
183fa3fe2b1SFlorian Westphal 			 (unsigned long long)msk->rcv_wnd_sent);
18406242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
185ab174ad8SPaolo Abeni 		return;
186ab174ad8SPaolo Abeni 	}
187ab174ad8SPaolo Abeni 
188ab174ad8SPaolo Abeni 	p = &msk->out_of_order_queue.rb_node;
18906242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
190ab174ad8SPaolo Abeni 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
191ab174ad8SPaolo Abeni 		rb_link_node(&skb->rbnode, NULL, p);
192ab174ad8SPaolo Abeni 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
193ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
194ab174ad8SPaolo Abeni 		goto end;
195ab174ad8SPaolo Abeni 	}
196ab174ad8SPaolo Abeni 
197ab174ad8SPaolo Abeni 	/* with 2 subflows, adding at end of ooo queue is quite likely
198ab174ad8SPaolo Abeni 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
199ab174ad8SPaolo Abeni 	 */
20006242e44SPaolo Abeni 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
20106242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
20206242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
203ab174ad8SPaolo Abeni 		return;
20406242e44SPaolo Abeni 	}
205ab174ad8SPaolo Abeni 
206ab174ad8SPaolo Abeni 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
207ab174ad8SPaolo Abeni 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
20806242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
209ab174ad8SPaolo Abeni 		parent = &msk->ooo_last_skb->rbnode;
210ab174ad8SPaolo Abeni 		p = &parent->rb_right;
211ab174ad8SPaolo Abeni 		goto insert;
212ab174ad8SPaolo Abeni 	}
213ab174ad8SPaolo Abeni 
214ab174ad8SPaolo Abeni 	/* Find place to insert this segment. Handle overlaps on the way. */
215ab174ad8SPaolo Abeni 	parent = NULL;
216ab174ad8SPaolo Abeni 	while (*p) {
217ab174ad8SPaolo Abeni 		parent = *p;
218ab174ad8SPaolo Abeni 		skb1 = rb_to_skb(parent);
219ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
220ab174ad8SPaolo Abeni 			p = &parent->rb_left;
221ab174ad8SPaolo Abeni 			continue;
222ab174ad8SPaolo Abeni 		}
223ab174ad8SPaolo Abeni 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
224ab174ad8SPaolo Abeni 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
225ab174ad8SPaolo Abeni 				/* All the bits are present. Drop. */
226ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb);
22706242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
228ab174ad8SPaolo Abeni 				return;
229ab174ad8SPaolo Abeni 			}
230ab174ad8SPaolo Abeni 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
231ab174ad8SPaolo Abeni 				/* partial overlap:
232ab174ad8SPaolo Abeni 				 *     |     skb      |
233ab174ad8SPaolo Abeni 				 *  |     skb1    |
234ab174ad8SPaolo Abeni 				 * continue traversing
235ab174ad8SPaolo Abeni 				 */
236ab174ad8SPaolo Abeni 			} else {
237ab174ad8SPaolo Abeni 				/* skb's seq == skb1's seq and skb covers skb1.
238ab174ad8SPaolo Abeni 				 * Replace skb1 with skb.
239ab174ad8SPaolo Abeni 				 */
240ab174ad8SPaolo Abeni 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
241ab174ad8SPaolo Abeni 						&msk->out_of_order_queue);
242ab174ad8SPaolo Abeni 				mptcp_drop(sk, skb1);
24306242e44SPaolo Abeni 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
244ab174ad8SPaolo Abeni 				goto merge_right;
245ab174ad8SPaolo Abeni 			}
246ab174ad8SPaolo Abeni 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
24706242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
248ab174ad8SPaolo Abeni 			return;
249ab174ad8SPaolo Abeni 		}
250ab174ad8SPaolo Abeni 		p = &parent->rb_right;
251ab174ad8SPaolo Abeni 	}
25206242e44SPaolo Abeni 
253ab174ad8SPaolo Abeni insert:
254ab174ad8SPaolo Abeni 	/* Insert segment into RB tree. */
255ab174ad8SPaolo Abeni 	rb_link_node(&skb->rbnode, parent, p);
256ab174ad8SPaolo Abeni 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
257ab174ad8SPaolo Abeni 
258ab174ad8SPaolo Abeni merge_right:
259ab174ad8SPaolo Abeni 	/* Remove other segments covered by skb. */
260ab174ad8SPaolo Abeni 	while ((skb1 = skb_rb_next(skb)) != NULL) {
261ab174ad8SPaolo Abeni 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
262ab174ad8SPaolo Abeni 			break;
263ab174ad8SPaolo Abeni 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
264ab174ad8SPaolo Abeni 		mptcp_drop(sk, skb1);
26506242e44SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
266ab174ad8SPaolo Abeni 	}
267ab174ad8SPaolo Abeni 	/* If there is no skb after us, we are the last_skb ! */
268ab174ad8SPaolo Abeni 	if (!skb1)
269ab174ad8SPaolo Abeni 		msk->ooo_last_skb = skb;
270ab174ad8SPaolo Abeni 
271ab174ad8SPaolo Abeni end:
272ab174ad8SPaolo Abeni 	skb_condense(skb);
273ab174ad8SPaolo Abeni 	skb_set_owner_r(skb, sk);
274ab174ad8SPaolo Abeni }
275ab174ad8SPaolo Abeni 
276ab174ad8SPaolo Abeni static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
277ab174ad8SPaolo Abeni 			     struct sk_buff *skb, unsigned int offset,
278ab174ad8SPaolo Abeni 			     size_t copy_len)
279ab174ad8SPaolo Abeni {
280ab174ad8SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2816771bfd9SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
2824e637c70SFlorian Westphal 	struct sk_buff *tail;
2836771bfd9SFlorian Westphal 
2846771bfd9SFlorian Westphal 	__skb_unlink(skb, &ssk->sk_receive_queue);
2854e637c70SFlorian Westphal 
2864e637c70SFlorian Westphal 	skb_ext_reset(skb);
2874e637c70SFlorian Westphal 	skb_orphan(skb);
2884e637c70SFlorian Westphal 
2899c3f94e1SPaolo Abeni 	/* try to fetch required memory from subflow */
2909c3f94e1SPaolo Abeni 	if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
2919c3f94e1SPaolo Abeni 		if (ssk->sk_forward_alloc < skb->truesize)
2929c3f94e1SPaolo Abeni 			goto drop;
2939c3f94e1SPaolo Abeni 		__sk_mem_reclaim(ssk, skb->truesize);
2949c3f94e1SPaolo Abeni 		if (!sk_rmem_schedule(sk, skb, skb->truesize))
2959c3f94e1SPaolo Abeni 			goto drop;
2969c3f94e1SPaolo Abeni 	}
2979c3f94e1SPaolo Abeni 
298ab174ad8SPaolo Abeni 	/* the skb map_seq accounts for the skb offset:
299ab174ad8SPaolo Abeni 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
300ab174ad8SPaolo Abeni 	 * value
301ab174ad8SPaolo Abeni 	 */
302ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
303ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
304ab174ad8SPaolo Abeni 	MPTCP_SKB_CB(skb)->offset = offset;
305ab174ad8SPaolo Abeni 
306ab174ad8SPaolo Abeni 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
307ab174ad8SPaolo Abeni 		/* in sequence */
308917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
3094e637c70SFlorian Westphal 		tail = skb_peek_tail(&sk->sk_receive_queue);
3108268ed4cSPaolo Abeni 		if (tail && mptcp_try_coalesce(sk, tail, skb))
311ab174ad8SPaolo Abeni 			return true;
3124e637c70SFlorian Westphal 
313600911ffSFlorian Westphal 		skb_set_owner_r(skb, sk);
3146771bfd9SFlorian Westphal 		__skb_queue_tail(&sk->sk_receive_queue, skb);
315ab174ad8SPaolo Abeni 		return true;
316ab174ad8SPaolo Abeni 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
317ab174ad8SPaolo Abeni 		mptcp_data_queue_ofo(msk, skb);
318ab174ad8SPaolo Abeni 		return false;
319ab174ad8SPaolo Abeni 	}
320ab174ad8SPaolo Abeni 
321ab174ad8SPaolo Abeni 	/* old data, keep it simple and drop the whole pkt, sender
322ab174ad8SPaolo Abeni 	 * will retransmit as needed, if needed.
323ab174ad8SPaolo Abeni 	 */
32406242e44SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
3259c3f94e1SPaolo Abeni drop:
326ab174ad8SPaolo Abeni 	mptcp_drop(sk, skb);
327ab174ad8SPaolo Abeni 	return false;
3286771bfd9SFlorian Westphal }
3296771bfd9SFlorian Westphal 
33016a9a9daSMat Martineau static void mptcp_stop_timer(struct sock *sk)
33116a9a9daSMat Martineau {
33216a9a9daSMat Martineau 	struct inet_connection_sock *icsk = inet_csk(sk);
33316a9a9daSMat Martineau 
33416a9a9daSMat Martineau 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
33516a9a9daSMat Martineau 	mptcp_sk(sk)->timer_ival = 0;
33616a9a9daSMat Martineau }
33716a9a9daSMat Martineau 
338e16163b6SPaolo Abeni static void mptcp_close_wake_up(struct sock *sk)
339e16163b6SPaolo Abeni {
340e16163b6SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD))
341e16163b6SPaolo Abeni 		return;
342e16163b6SPaolo Abeni 
343e16163b6SPaolo Abeni 	sk->sk_state_change(sk);
344e16163b6SPaolo Abeni 	if (sk->sk_shutdown == SHUTDOWN_MASK ||
345e16163b6SPaolo Abeni 	    sk->sk_state == TCP_CLOSE)
346e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
347e16163b6SPaolo Abeni 	else
348e16163b6SPaolo Abeni 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
349e16163b6SPaolo Abeni }
350e16163b6SPaolo Abeni 
3516e628cd3SPaolo Abeni static bool mptcp_pending_data_fin_ack(struct sock *sk)
3526e628cd3SPaolo Abeni {
3536e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
3546e628cd3SPaolo Abeni 
3556e628cd3SPaolo Abeni 	return !__mptcp_check_fallback(msk) &&
3566e628cd3SPaolo Abeni 	       ((1 << sk->sk_state) &
3576e628cd3SPaolo Abeni 		(TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
3586e628cd3SPaolo Abeni 	       msk->write_seq == READ_ONCE(msk->snd_una);
3596e628cd3SPaolo Abeni }
3606e628cd3SPaolo Abeni 
36116a9a9daSMat Martineau static void mptcp_check_data_fin_ack(struct sock *sk)
36216a9a9daSMat Martineau {
36316a9a9daSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
36416a9a9daSMat Martineau 
36516a9a9daSMat Martineau 	/* Look for an acknowledged DATA_FIN */
3666e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk)) {
36716a9a9daSMat Martineau 		mptcp_stop_timer(sk);
36816a9a9daSMat Martineau 
36916a9a9daSMat Martineau 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
37016a9a9daSMat Martineau 
37116a9a9daSMat Martineau 		switch (sk->sk_state) {
37216a9a9daSMat Martineau 		case TCP_FIN_WAIT1:
37316a9a9daSMat Martineau 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
37416a9a9daSMat Martineau 			break;
37516a9a9daSMat Martineau 		case TCP_CLOSING:
37616a9a9daSMat Martineau 		case TCP_LAST_ACK:
37716a9a9daSMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE);
37816a9a9daSMat Martineau 			break;
37916a9a9daSMat Martineau 		}
38016a9a9daSMat Martineau 
381e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
38216a9a9daSMat Martineau 	}
38316a9a9daSMat Martineau }
38416a9a9daSMat Martineau 
3853721b9b6SMat Martineau static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
3863721b9b6SMat Martineau {
3873721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
3883721b9b6SMat Martineau 
3893721b9b6SMat Martineau 	if (READ_ONCE(msk->rcv_data_fin) &&
3903721b9b6SMat Martineau 	    ((1 << sk->sk_state) &
3913721b9b6SMat Martineau 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
3923721b9b6SMat Martineau 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
3933721b9b6SMat Martineau 
3943721b9b6SMat Martineau 		if (msk->ack_seq == rcv_data_fin_seq) {
3953721b9b6SMat Martineau 			if (seq)
3963721b9b6SMat Martineau 				*seq = rcv_data_fin_seq;
3973721b9b6SMat Martineau 
3983721b9b6SMat Martineau 			return true;
3993721b9b6SMat Martineau 		}
4003721b9b6SMat Martineau 	}
4013721b9b6SMat Martineau 
4023721b9b6SMat Martineau 	return false;
4033721b9b6SMat Martineau }
4043721b9b6SMat Martineau 
4053721b9b6SMat Martineau static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
4063721b9b6SMat Martineau {
4073721b9b6SMat Martineau 	long tout = ssk && inet_csk(ssk)->icsk_pending ?
4083721b9b6SMat Martineau 				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
4093721b9b6SMat Martineau 
4103721b9b6SMat Martineau 	if (tout <= 0)
4113721b9b6SMat Martineau 		tout = mptcp_sk(sk)->timer_ival;
4123721b9b6SMat Martineau 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
4133721b9b6SMat Martineau }
4143721b9b6SMat Martineau 
415ea4ca586SPaolo Abeni static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
416ea4ca586SPaolo Abeni {
417ea4ca586SPaolo Abeni 	struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
418ea4ca586SPaolo Abeni 
419ea4ca586SPaolo Abeni 	/* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
420ea4ca586SPaolo Abeni 	if (subflow->request_join && !subflow->fully_established)
421ea4ca586SPaolo Abeni 		return false;
422ea4ca586SPaolo Abeni 
423ea4ca586SPaolo Abeni 	/* only send if our side has not closed yet */
424ea4ca586SPaolo Abeni 	return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
425ea4ca586SPaolo Abeni }
426ea4ca586SPaolo Abeni 
427fd897679SPaolo Abeni static bool tcp_can_send_ack(const struct sock *ssk)
428fd897679SPaolo Abeni {
429fd897679SPaolo Abeni 	return !((1 << inet_sk_state_load(ssk)) &
43020bc80b6SPaolo Abeni 	       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
431fd897679SPaolo Abeni }
432fd897679SPaolo Abeni 
433fd897679SPaolo Abeni static void mptcp_send_ack(struct mptcp_sock *msk)
4347ed90803SPaolo Abeni {
4357ed90803SPaolo Abeni 	struct mptcp_subflow_context *subflow;
4367ed90803SPaolo Abeni 
4377ed90803SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
4387ed90803SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4397ed90803SPaolo Abeni 
4407ed90803SPaolo Abeni 		lock_sock(ssk);
441fd897679SPaolo Abeni 		if (tcp_can_send_ack(ssk))
4427ed90803SPaolo Abeni 			tcp_send_ack(ssk);
4437ed90803SPaolo Abeni 		release_sock(ssk);
444fd897679SPaolo Abeni 	}
445ea4ca586SPaolo Abeni }
446ea4ca586SPaolo Abeni 
447fd897679SPaolo Abeni static bool mptcp_subflow_cleanup_rbuf(struct sock *ssk)
448fd897679SPaolo Abeni {
449fd897679SPaolo Abeni 	int ret;
450fd897679SPaolo Abeni 
451fd897679SPaolo Abeni 	lock_sock(ssk);
452fd897679SPaolo Abeni 	ret = tcp_can_send_ack(ssk);
453fd897679SPaolo Abeni 	if (ret)
454fd897679SPaolo Abeni 		tcp_cleanup_rbuf(ssk, 1);
455fd897679SPaolo Abeni 	release_sock(ssk);
456fd897679SPaolo Abeni 	return ret;
457ea4ca586SPaolo Abeni }
458fd897679SPaolo Abeni 
459fd897679SPaolo Abeni static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
460fd897679SPaolo Abeni {
46187952603SPaolo Abeni 	struct sock *ack_hint = READ_ONCE(msk->ack_hint);
462fd897679SPaolo Abeni 	struct mptcp_subflow_context *subflow;
463fd897679SPaolo Abeni 
464fd897679SPaolo Abeni 	/* if the hinted ssk is still active, try to use it */
46587952603SPaolo Abeni 	if (likely(ack_hint)) {
466fd897679SPaolo Abeni 		mptcp_for_each_subflow(msk, subflow) {
467fd897679SPaolo Abeni 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
468fd897679SPaolo Abeni 
46987952603SPaolo Abeni 			if (ack_hint == ssk && mptcp_subflow_cleanup_rbuf(ssk))
470fd897679SPaolo Abeni 				return;
4717ed90803SPaolo Abeni 		}
4727ed90803SPaolo Abeni 	}
4737ed90803SPaolo Abeni 
474fd897679SPaolo Abeni 	/* otherwise pick the first active subflow */
475fd897679SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow)
476fd897679SPaolo Abeni 		if (mptcp_subflow_cleanup_rbuf(mptcp_subflow_tcp_sock(subflow)))
477fd897679SPaolo Abeni 			return;
478fd897679SPaolo Abeni }
479fd897679SPaolo Abeni 
4807ed90803SPaolo Abeni static bool mptcp_check_data_fin(struct sock *sk)
4813721b9b6SMat Martineau {
4823721b9b6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
4833721b9b6SMat Martineau 	u64 rcv_data_fin_seq;
4847ed90803SPaolo Abeni 	bool ret = false;
4853721b9b6SMat Martineau 
4863721b9b6SMat Martineau 	if (__mptcp_check_fallback(msk) || !msk->first)
4877ed90803SPaolo Abeni 		return ret;
4883721b9b6SMat Martineau 
4893721b9b6SMat Martineau 	/* Need to ack a DATA_FIN received from a peer while this side
4903721b9b6SMat Martineau 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
4913721b9b6SMat Martineau 	 * msk->rcv_data_fin was set when parsing the incoming options
4923721b9b6SMat Martineau 	 * at the subflow level and the msk lock was not held, so this
4933721b9b6SMat Martineau 	 * is the first opportunity to act on the DATA_FIN and change
4943721b9b6SMat Martineau 	 * the msk state.
4953721b9b6SMat Martineau 	 *
4963721b9b6SMat Martineau 	 * If we are caught up to the sequence number of the incoming
4973721b9b6SMat Martineau 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
4983721b9b6SMat Martineau 	 * not caught up, do nothing and let the recv code send DATA_ACK
4993721b9b6SMat Martineau 	 * when catching up.
5003721b9b6SMat Martineau 	 */
5013721b9b6SMat Martineau 
5023721b9b6SMat Martineau 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
503917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
5043721b9b6SMat Martineau 		WRITE_ONCE(msk->rcv_data_fin, 0);
5053721b9b6SMat Martineau 
5063721b9b6SMat Martineau 		sk->sk_shutdown |= RCV_SHUTDOWN;
50716a9a9daSMat Martineau 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
50816a9a9daSMat Martineau 		set_bit(MPTCP_DATA_READY, &msk->flags);
5093721b9b6SMat Martineau 
5103721b9b6SMat Martineau 		switch (sk->sk_state) {
5113721b9b6SMat Martineau 		case TCP_ESTABLISHED:
5123721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE_WAIT);
5133721b9b6SMat Martineau 			break;
5143721b9b6SMat Martineau 		case TCP_FIN_WAIT1:
5153721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSING);
5163721b9b6SMat Martineau 			break;
5173721b9b6SMat Martineau 		case TCP_FIN_WAIT2:
5183721b9b6SMat Martineau 			inet_sk_state_store(sk, TCP_CLOSE);
5193721b9b6SMat Martineau 			break;
5203721b9b6SMat Martineau 		default:
5213721b9b6SMat Martineau 			/* Other states not expected */
5223721b9b6SMat Martineau 			WARN_ON_ONCE(1);
5233721b9b6SMat Martineau 			break;
5243721b9b6SMat Martineau 		}
5253721b9b6SMat Martineau 
5267ed90803SPaolo Abeni 		ret = true;
5273721b9b6SMat Martineau 		mptcp_set_timeout(sk, NULL);
528fd897679SPaolo Abeni 		mptcp_send_ack(msk);
529e16163b6SPaolo Abeni 		mptcp_close_wake_up(sk);
5303721b9b6SMat Martineau 	}
5317ed90803SPaolo Abeni 	return ret;
5323721b9b6SMat Martineau }
5333721b9b6SMat Martineau 
5346771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
5356771bfd9SFlorian Westphal 					   struct sock *ssk,
5366771bfd9SFlorian Westphal 					   unsigned int *bytes)
5376771bfd9SFlorian Westphal {
5386771bfd9SFlorian Westphal 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
539600911ffSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
5406771bfd9SFlorian Westphal 	unsigned int moved = 0;
5416771bfd9SFlorian Westphal 	bool more_data_avail;
5426771bfd9SFlorian Westphal 	struct tcp_sock *tp;
5436771bfd9SFlorian Westphal 	bool done = false;
54413c7ba0cSFlorian Westphal 	int sk_rbuf;
54513c7ba0cSFlorian Westphal 
54613c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
54713c7ba0cSFlorian Westphal 
54813c7ba0cSFlorian Westphal 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
54913c7ba0cSFlorian Westphal 		int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
55013c7ba0cSFlorian Westphal 
55113c7ba0cSFlorian Westphal 		if (unlikely(ssk_rbuf > sk_rbuf)) {
55213c7ba0cSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
55313c7ba0cSFlorian Westphal 			sk_rbuf = ssk_rbuf;
55413c7ba0cSFlorian Westphal 		}
55513c7ba0cSFlorian Westphal 	}
556600911ffSFlorian Westphal 
557ab174ad8SPaolo Abeni 	pr_debug("msk=%p ssk=%p", msk, ssk);
5586771bfd9SFlorian Westphal 	tp = tcp_sk(ssk);
5596771bfd9SFlorian Westphal 	do {
5606771bfd9SFlorian Westphal 		u32 map_remaining, offset;
5616771bfd9SFlorian Westphal 		u32 seq = tp->copied_seq;
5626771bfd9SFlorian Westphal 		struct sk_buff *skb;
5636771bfd9SFlorian Westphal 		bool fin;
5646771bfd9SFlorian Westphal 
5656771bfd9SFlorian Westphal 		/* try to move as much data as available */
5666771bfd9SFlorian Westphal 		map_remaining = subflow->map_data_len -
5676771bfd9SFlorian Westphal 				mptcp_subflow_get_map_offset(subflow);
5686771bfd9SFlorian Westphal 
5696771bfd9SFlorian Westphal 		skb = skb_peek(&ssk->sk_receive_queue);
570d9fb8c50SPaolo Abeni 		if (!skb) {
571d9fb8c50SPaolo Abeni 			/* if no data is found, a racing workqueue/recvmsg
572d9fb8c50SPaolo Abeni 			 * already processed the new data, stop here or we
573d9fb8c50SPaolo Abeni 			 * can enter an infinite loop
574d9fb8c50SPaolo Abeni 			 */
575d9fb8c50SPaolo Abeni 			if (!moved)
576d9fb8c50SPaolo Abeni 				done = true;
5776771bfd9SFlorian Westphal 			break;
578d9fb8c50SPaolo Abeni 		}
5796771bfd9SFlorian Westphal 
580e1ff9e82SDavide Caratti 		if (__mptcp_check_fallback(msk)) {
581e1ff9e82SDavide Caratti 			/* if we are running under the workqueue, TCP could have
582e1ff9e82SDavide Caratti 			 * collapsed skbs between dummy map creation and now
583e1ff9e82SDavide Caratti 			 * be sure to adjust the size
584e1ff9e82SDavide Caratti 			 */
585e1ff9e82SDavide Caratti 			map_remaining = skb->len;
586e1ff9e82SDavide Caratti 			subflow->map_data_len = skb->len;
587e1ff9e82SDavide Caratti 		}
588e1ff9e82SDavide Caratti 
5896771bfd9SFlorian Westphal 		offset = seq - TCP_SKB_CB(skb)->seq;
5906771bfd9SFlorian Westphal 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
5916771bfd9SFlorian Westphal 		if (fin) {
5926771bfd9SFlorian Westphal 			done = true;
5936771bfd9SFlorian Westphal 			seq++;
5946771bfd9SFlorian Westphal 		}
5956771bfd9SFlorian Westphal 
5966771bfd9SFlorian Westphal 		if (offset < skb->len) {
5976771bfd9SFlorian Westphal 			size_t len = skb->len - offset;
5986771bfd9SFlorian Westphal 
5996771bfd9SFlorian Westphal 			if (tp->urg_data)
6006771bfd9SFlorian Westphal 				done = true;
6016771bfd9SFlorian Westphal 
602ab174ad8SPaolo Abeni 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
6036771bfd9SFlorian Westphal 				moved += len;
604ab174ad8SPaolo Abeni 			seq += len;
6056771bfd9SFlorian Westphal 
6066771bfd9SFlorian Westphal 			if (WARN_ON_ONCE(map_remaining < len))
6076771bfd9SFlorian Westphal 				break;
6086771bfd9SFlorian Westphal 		} else {
6096771bfd9SFlorian Westphal 			WARN_ON_ONCE(!fin);
6106771bfd9SFlorian Westphal 			sk_eat_skb(ssk, skb);
6116771bfd9SFlorian Westphal 			done = true;
6126771bfd9SFlorian Westphal 		}
6136771bfd9SFlorian Westphal 
6146771bfd9SFlorian Westphal 		WRITE_ONCE(tp->copied_seq, seq);
6156771bfd9SFlorian Westphal 		more_data_avail = mptcp_subflow_data_available(ssk);
616600911ffSFlorian Westphal 
61713c7ba0cSFlorian Westphal 		if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
618600911ffSFlorian Westphal 			done = true;
619600911ffSFlorian Westphal 			break;
620600911ffSFlorian Westphal 		}
6216771bfd9SFlorian Westphal 	} while (more_data_avail);
62287952603SPaolo Abeni 	WRITE_ONCE(msk->ack_hint, ssk);
6236771bfd9SFlorian Westphal 
6246719331cSPaolo Abeni 	*bytes += moved;
6256771bfd9SFlorian Westphal 	return done;
6266771bfd9SFlorian Westphal }
6276771bfd9SFlorian Westphal 
62887952603SPaolo Abeni static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
629ab174ad8SPaolo Abeni {
630ab174ad8SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
631ab174ad8SPaolo Abeni 	struct sk_buff *skb, *tail;
632ab174ad8SPaolo Abeni 	bool moved = false;
633ab174ad8SPaolo Abeni 	struct rb_node *p;
634ab174ad8SPaolo Abeni 	u64 end_seq;
635ab174ad8SPaolo Abeni 
636ab174ad8SPaolo Abeni 	p = rb_first(&msk->out_of_order_queue);
63706242e44SPaolo Abeni 	pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
638ab174ad8SPaolo Abeni 	while (p) {
639ab174ad8SPaolo Abeni 		skb = rb_to_skb(p);
640ab174ad8SPaolo Abeni 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
641ab174ad8SPaolo Abeni 			break;
642ab174ad8SPaolo Abeni 
643ab174ad8SPaolo Abeni 		p = rb_next(p);
644ab174ad8SPaolo Abeni 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
645ab174ad8SPaolo Abeni 
646ab174ad8SPaolo Abeni 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
647ab174ad8SPaolo Abeni 				      msk->ack_seq))) {
648ab174ad8SPaolo Abeni 			mptcp_drop(sk, skb);
64906242e44SPaolo Abeni 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
650ab174ad8SPaolo Abeni 			continue;
651ab174ad8SPaolo Abeni 		}
652ab174ad8SPaolo Abeni 
653ab174ad8SPaolo Abeni 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
654ab174ad8SPaolo Abeni 		tail = skb_peek_tail(&sk->sk_receive_queue);
655ab174ad8SPaolo Abeni 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
656ab174ad8SPaolo Abeni 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
657ab174ad8SPaolo Abeni 
658ab174ad8SPaolo Abeni 			/* skip overlapping data, if any */
65906242e44SPaolo Abeni 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
66006242e44SPaolo Abeni 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
66106242e44SPaolo Abeni 				 delta);
662ab174ad8SPaolo Abeni 			MPTCP_SKB_CB(skb)->offset += delta;
663ab174ad8SPaolo Abeni 			__skb_queue_tail(&sk->sk_receive_queue, skb);
664ab174ad8SPaolo Abeni 		}
665ab174ad8SPaolo Abeni 		msk->ack_seq = end_seq;
666ab174ad8SPaolo Abeni 		moved = true;
667ab174ad8SPaolo Abeni 	}
668ab174ad8SPaolo Abeni 	return moved;
669ab174ad8SPaolo Abeni }
670ab174ad8SPaolo Abeni 
6712e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket.  If its already
6722e52213cSFlorian Westphal  * owned, we need to defer to the work queue to avoid ABBA deadlock.
6732e52213cSFlorian Westphal  */
67487952603SPaolo Abeni static void move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
6752e52213cSFlorian Westphal {
6762e52213cSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
6772e52213cSFlorian Westphal 	unsigned int moved = 0;
6782e52213cSFlorian Westphal 
67987952603SPaolo Abeni 	if (inet_sk_state_load(sk) == TCP_CLOSE)
68087952603SPaolo Abeni 		return;
6812e52213cSFlorian Westphal 
68287952603SPaolo Abeni 	mptcp_data_lock(sk);
6832e52213cSFlorian Westphal 
6842e52213cSFlorian Westphal 	__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
68587952603SPaolo Abeni 	__mptcp_ofo_queue(msk);
686ab174ad8SPaolo Abeni 
687ab174ad8SPaolo Abeni 	/* If the moves have caught up with the DATA_FIN sequence number
688ab174ad8SPaolo Abeni 	 * it's time to ack the DATA_FIN and change socket state, but
689ab174ad8SPaolo Abeni 	 * this is not a good place to change state. Let the workqueue
690ab174ad8SPaolo Abeni 	 * do it.
691ab174ad8SPaolo Abeni 	 */
692ba8f48f7SPaolo Abeni 	if (mptcp_pending_data_fin(sk, NULL))
693ba8f48f7SPaolo Abeni 		mptcp_schedule_work(sk);
69487952603SPaolo Abeni 	mptcp_data_unlock(sk);
6952e52213cSFlorian Westphal }
6962e52213cSFlorian Westphal 
6972e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk)
698101f6f85SFlorian Westphal {
6996719331cSPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
700101f6f85SFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
70113c7ba0cSFlorian Westphal 	int sk_rbuf, ssk_rbuf;
7026719331cSPaolo Abeni 	bool wake;
703101f6f85SFlorian Westphal 
704d7b1bfd0SPaolo Abeni 	/* The peer can send data while we are shutting down this
705d7b1bfd0SPaolo Abeni 	 * subflow at msk destruction time, but we must avoid enqueuing
706d7b1bfd0SPaolo Abeni 	 * more data to the msk receive queue
707d7b1bfd0SPaolo Abeni 	 */
708d7b1bfd0SPaolo Abeni 	if (unlikely(subflow->disposable))
709d7b1bfd0SPaolo Abeni 		return;
710d7b1bfd0SPaolo Abeni 
7116719331cSPaolo Abeni 	/* move_skbs_to_msk below can legitly clear the data_avail flag,
7126719331cSPaolo Abeni 	 * but we will need later to properly woke the reader, cache its
7136719331cSPaolo Abeni 	 * value
7146719331cSPaolo Abeni 	 */
7156719331cSPaolo Abeni 	wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL;
7166719331cSPaolo Abeni 	if (wake)
717101f6f85SFlorian Westphal 		set_bit(MPTCP_DATA_READY, &msk->flags);
7186771bfd9SFlorian Westphal 
71913c7ba0cSFlorian Westphal 	ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
72013c7ba0cSFlorian Westphal 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
72113c7ba0cSFlorian Westphal 	if (unlikely(ssk_rbuf > sk_rbuf))
72213c7ba0cSFlorian Westphal 		sk_rbuf = ssk_rbuf;
72313c7ba0cSFlorian Westphal 
72413c7ba0cSFlorian Westphal 	/* over limit? can't append more skbs to msk */
72513c7ba0cSFlorian Westphal 	if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf)
7262e52213cSFlorian Westphal 		goto wake;
7272e52213cSFlorian Westphal 
72814c441b5SPaolo Abeni 	move_skbs_to_msk(msk, ssk);
729ea4ca586SPaolo Abeni 
730600911ffSFlorian Westphal wake:
7316719331cSPaolo Abeni 	if (wake)
732101f6f85SFlorian Westphal 		sk->sk_data_ready(sk);
733101f6f85SFlorian Westphal }
734101f6f85SFlorian Westphal 
73584dfe367SGeliang Tang void __mptcp_flush_join_list(struct mptcp_sock *msk)
736ec3edaa7SPeter Krystad {
737ec3edaa7SPeter Krystad 	if (likely(list_empty(&msk->join_list)))
738ec3edaa7SPeter Krystad 		return;
739ec3edaa7SPeter Krystad 
740ec3edaa7SPeter Krystad 	spin_lock_bh(&msk->join_list_lock);
741ec3edaa7SPeter Krystad 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
742ec3edaa7SPeter Krystad 	spin_unlock_bh(&msk->join_list_lock);
743ec3edaa7SPeter Krystad }
744ec3edaa7SPeter Krystad 
745b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk)
746b51f9b80SPaolo Abeni {
747b51f9b80SPaolo Abeni 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
748b51f9b80SPaolo Abeni }
749b51f9b80SPaolo Abeni 
750b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk)
751b51f9b80SPaolo Abeni {
752b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = inet_csk(sk);
753b51f9b80SPaolo Abeni 	unsigned long tout;
754b51f9b80SPaolo Abeni 
755e16163b6SPaolo Abeni 	/* prevent rescheduling on close */
756e16163b6SPaolo Abeni 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
757e16163b6SPaolo Abeni 		return;
758e16163b6SPaolo Abeni 
759b51f9b80SPaolo Abeni 	/* should never be called with mptcp level timer cleared */
760b51f9b80SPaolo Abeni 	tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
761b51f9b80SPaolo Abeni 	if (WARN_ON_ONCE(!tout))
762b51f9b80SPaolo Abeni 		tout = TCP_RTO_MIN;
763b51f9b80SPaolo Abeni 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
764b51f9b80SPaolo Abeni }
765b51f9b80SPaolo Abeni 
766ba8f48f7SPaolo Abeni bool mptcp_schedule_work(struct sock *sk)
767ba8f48f7SPaolo Abeni {
768ba8f48f7SPaolo Abeni 	if (inet_sk_state_load(sk) != TCP_CLOSE &&
769ba8f48f7SPaolo Abeni 	    schedule_work(&mptcp_sk(sk)->work)) {
770ba8f48f7SPaolo Abeni 		/* each subflow already holds a reference to the sk, and the
771ba8f48f7SPaolo Abeni 		 * workqueue is invoked by a subflow, so sk can't go away here.
772ba8f48f7SPaolo Abeni 		 */
773ba8f48f7SPaolo Abeni 		sock_hold(sk);
774ba8f48f7SPaolo Abeni 		return true;
775ba8f48f7SPaolo Abeni 	}
776ba8f48f7SPaolo Abeni 	return false;
777ba8f48f7SPaolo Abeni }
778ba8f48f7SPaolo Abeni 
77959832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk)
78059832e24SFlorian Westphal {
781ba8f48f7SPaolo Abeni 	if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
782ba8f48f7SPaolo Abeni 		mptcp_schedule_work(sk);
78359832e24SFlorian Westphal }
78459832e24SFlorian Westphal 
7855969856aSPaolo Abeni static void mptcp_check_for_eof(struct mptcp_sock *msk)
7865969856aSPaolo Abeni {
7875969856aSPaolo Abeni 	struct mptcp_subflow_context *subflow;
7885969856aSPaolo Abeni 	struct sock *sk = (struct sock *)msk;
7895969856aSPaolo Abeni 	int receivers = 0;
7905969856aSPaolo Abeni 
7915969856aSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow)
7925969856aSPaolo Abeni 		receivers += !subflow->rx_eof;
793e16163b6SPaolo Abeni 	if (receivers)
794e16163b6SPaolo Abeni 		return;
7955969856aSPaolo Abeni 
796e16163b6SPaolo Abeni 	if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
7975969856aSPaolo Abeni 		/* hopefully temporary hack: propagate shutdown status
7985969856aSPaolo Abeni 		 * to msk, when all subflows agree on it
7995969856aSPaolo Abeni 		 */
8005969856aSPaolo Abeni 		sk->sk_shutdown |= RCV_SHUTDOWN;
8015969856aSPaolo Abeni 
8025969856aSPaolo Abeni 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
8035969856aSPaolo Abeni 		set_bit(MPTCP_DATA_READY, &msk->flags);
8045969856aSPaolo Abeni 		sk->sk_data_ready(sk);
8055969856aSPaolo Abeni 	}
806e16163b6SPaolo Abeni 
807e16163b6SPaolo Abeni 	switch (sk->sk_state) {
808e16163b6SPaolo Abeni 	case TCP_ESTABLISHED:
809e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE_WAIT);
810e16163b6SPaolo Abeni 		break;
811e16163b6SPaolo Abeni 	case TCP_FIN_WAIT1:
81226aa2314SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSING);
81326aa2314SPaolo Abeni 		break;
81426aa2314SPaolo Abeni 	case TCP_FIN_WAIT2:
815e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
816e16163b6SPaolo Abeni 		break;
817e16163b6SPaolo Abeni 	default:
818e16163b6SPaolo Abeni 		return;
819e16163b6SPaolo Abeni 	}
820e16163b6SPaolo Abeni 	mptcp_close_wake_up(sk);
8215969856aSPaolo Abeni }
8225969856aSPaolo Abeni 
8237a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
8247a6a6cbcSPaolo Abeni {
8257a6a6cbcSPaolo Abeni 	struct mptcp_subflow_context *subflow;
8267a6a6cbcSPaolo Abeni 	struct sock *sk = (struct sock *)msk;
8277a6a6cbcSPaolo Abeni 
8287a6a6cbcSPaolo Abeni 	sock_owned_by_me(sk);
8297a6a6cbcSPaolo Abeni 
8307a6a6cbcSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
8317a6a6cbcSPaolo Abeni 		if (subflow->data_avail)
8327a6a6cbcSPaolo Abeni 			return mptcp_subflow_tcp_sock(subflow);
8337a6a6cbcSPaolo Abeni 	}
8347a6a6cbcSPaolo Abeni 
8357a6a6cbcSPaolo Abeni 	return NULL;
8367a6a6cbcSPaolo Abeni }
8377a6a6cbcSPaolo Abeni 
8383f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq,
83957040755SPaolo Abeni 				      const struct sk_buff *skb,
84057040755SPaolo Abeni 				      const struct mptcp_ext *mpext)
8416d0060f6SMat Martineau {
84257040755SPaolo Abeni 	if (!tcp_skb_can_collapse_to(skb))
84357040755SPaolo Abeni 		return false;
84457040755SPaolo Abeni 
8455a369ca6SPaolo Abeni 	/* can collapse only if MPTCP level sequence is in order and this
8465a369ca6SPaolo Abeni 	 * mapping has not been xmitted yet
8475a369ca6SPaolo Abeni 	 */
8485a369ca6SPaolo Abeni 	return mpext && mpext->data_seq + mpext->data_len == write_seq &&
8495a369ca6SPaolo Abeni 	       !mpext->frozen;
85057040755SPaolo Abeni }
85157040755SPaolo Abeni 
85218b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
85318b683bfSPaolo Abeni 				       const struct page_frag *pfrag,
85418b683bfSPaolo Abeni 				       const struct mptcp_data_frag *df)
85518b683bfSPaolo Abeni {
85618b683bfSPaolo Abeni 	return df && pfrag->page == df->page &&
857d9ca1de8SPaolo Abeni 		pfrag->size - pfrag->offset > 0 &&
85818b683bfSPaolo Abeni 		df->data_seq + df->data_len == msk->write_seq;
85918b683bfSPaolo Abeni }
86018b683bfSPaolo Abeni 
861724cfd2eSPaolo Abeni static int mptcp_wmem_with_overhead(struct sock *sk, int size)
862e93da928SPaolo Abeni {
863724cfd2eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
864724cfd2eSPaolo Abeni 	int ret, skbs;
865724cfd2eSPaolo Abeni 
866724cfd2eSPaolo Abeni 	ret = size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT);
867724cfd2eSPaolo Abeni 	skbs = (msk->tx_pending_data + size) / msk->size_goal_cache;
868724cfd2eSPaolo Abeni 	if (skbs < msk->skb_tx_cache.qlen)
869724cfd2eSPaolo Abeni 		return ret;
870724cfd2eSPaolo Abeni 
871724cfd2eSPaolo Abeni 	return ret + (skbs - msk->skb_tx_cache.qlen) * SKB_TRUESIZE(MAX_TCP_HEADER);
872e93da928SPaolo Abeni }
873e93da928SPaolo Abeni 
874e93da928SPaolo Abeni static void __mptcp_wmem_reserve(struct sock *sk, int size)
875e93da928SPaolo Abeni {
876724cfd2eSPaolo Abeni 	int amount = mptcp_wmem_with_overhead(sk, size);
877e93da928SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
878e93da928SPaolo Abeni 
879e93da928SPaolo Abeni 	WARN_ON_ONCE(msk->wmem_reserved);
880e7579d5dSDavide Caratti 	if (WARN_ON_ONCE(amount < 0))
881e7579d5dSDavide Caratti 		amount = 0;
882e7579d5dSDavide Caratti 
883e93da928SPaolo Abeni 	if (amount <= sk->sk_forward_alloc)
884e93da928SPaolo Abeni 		goto reserve;
885e93da928SPaolo Abeni 
886e93da928SPaolo Abeni 	/* under memory pressure try to reserve at most a single page
887e93da928SPaolo Abeni 	 * otherwise try to reserve the full estimate and fallback
888e93da928SPaolo Abeni 	 * to a single page before entering the error path
889e93da928SPaolo Abeni 	 */
890e93da928SPaolo Abeni 	if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) ||
891e93da928SPaolo Abeni 	    !sk_wmem_schedule(sk, amount)) {
892e93da928SPaolo Abeni 		if (amount <= PAGE_SIZE)
893e93da928SPaolo Abeni 			goto nomem;
894e93da928SPaolo Abeni 
895e93da928SPaolo Abeni 		amount = PAGE_SIZE;
896e93da928SPaolo Abeni 		if (!sk_wmem_schedule(sk, amount))
897e93da928SPaolo Abeni 			goto nomem;
898e93da928SPaolo Abeni 	}
899e93da928SPaolo Abeni 
900e93da928SPaolo Abeni reserve:
901e93da928SPaolo Abeni 	msk->wmem_reserved = amount;
902e93da928SPaolo Abeni 	sk->sk_forward_alloc -= amount;
903e93da928SPaolo Abeni 	return;
904e93da928SPaolo Abeni 
905e93da928SPaolo Abeni nomem:
906e93da928SPaolo Abeni 	/* we will wait for memory on next allocation */
907e93da928SPaolo Abeni 	msk->wmem_reserved = -1;
908e93da928SPaolo Abeni }
909e93da928SPaolo Abeni 
910e93da928SPaolo Abeni static void __mptcp_update_wmem(struct sock *sk)
911e93da928SPaolo Abeni {
912e93da928SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
913e93da928SPaolo Abeni 
914e93da928SPaolo Abeni 	if (!msk->wmem_reserved)
915e93da928SPaolo Abeni 		return;
916e93da928SPaolo Abeni 
917e93da928SPaolo Abeni 	if (msk->wmem_reserved < 0)
918e93da928SPaolo Abeni 		msk->wmem_reserved = 0;
919e93da928SPaolo Abeni 	if (msk->wmem_reserved > 0) {
920e93da928SPaolo Abeni 		sk->sk_forward_alloc += msk->wmem_reserved;
921e93da928SPaolo Abeni 		msk->wmem_reserved = 0;
922e93da928SPaolo Abeni 	}
923e93da928SPaolo Abeni }
924e93da928SPaolo Abeni 
925e93da928SPaolo Abeni static bool mptcp_wmem_alloc(struct sock *sk, int size)
926e93da928SPaolo Abeni {
927e93da928SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
928e93da928SPaolo Abeni 
929e93da928SPaolo Abeni 	/* check for pre-existing error condition */
930e93da928SPaolo Abeni 	if (msk->wmem_reserved < 0)
931e93da928SPaolo Abeni 		return false;
932e93da928SPaolo Abeni 
933e93da928SPaolo Abeni 	if (msk->wmem_reserved >= size)
934e93da928SPaolo Abeni 		goto account;
935e93da928SPaolo Abeni 
93687952603SPaolo Abeni 	mptcp_data_lock(sk);
93787952603SPaolo Abeni 	if (!sk_wmem_schedule(sk, size)) {
93887952603SPaolo Abeni 		mptcp_data_unlock(sk);
939e93da928SPaolo Abeni 		return false;
94087952603SPaolo Abeni 	}
941e93da928SPaolo Abeni 
942e93da928SPaolo Abeni 	sk->sk_forward_alloc -= size;
943e93da928SPaolo Abeni 	msk->wmem_reserved += size;
94487952603SPaolo Abeni 	mptcp_data_unlock(sk);
945e93da928SPaolo Abeni 
946e93da928SPaolo Abeni account:
947e93da928SPaolo Abeni 	msk->wmem_reserved -= size;
948e93da928SPaolo Abeni 	return true;
949e93da928SPaolo Abeni }
950e93da928SPaolo Abeni 
95187952603SPaolo Abeni static void mptcp_wmem_uncharge(struct sock *sk, int size)
95287952603SPaolo Abeni {
95387952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
95487952603SPaolo Abeni 
95587952603SPaolo Abeni 	if (msk->wmem_reserved < 0)
95687952603SPaolo Abeni 		msk->wmem_reserved = 0;
95787952603SPaolo Abeni 	msk->wmem_reserved += size;
95887952603SPaolo Abeni }
95987952603SPaolo Abeni 
960724cfd2eSPaolo Abeni static void mptcp_mem_reclaim_partial(struct sock *sk)
961724cfd2eSPaolo Abeni {
962724cfd2eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
963724cfd2eSPaolo Abeni 
964724cfd2eSPaolo Abeni 	/* if we are experiencing a transint allocation error,
965724cfd2eSPaolo Abeni 	 * the forward allocation memory has been already
966724cfd2eSPaolo Abeni 	 * released
967724cfd2eSPaolo Abeni 	 */
968724cfd2eSPaolo Abeni 	if (msk->wmem_reserved < 0)
969724cfd2eSPaolo Abeni 		return;
970724cfd2eSPaolo Abeni 
971724cfd2eSPaolo Abeni 	mptcp_data_lock(sk);
972724cfd2eSPaolo Abeni 	sk->sk_forward_alloc += msk->wmem_reserved;
973724cfd2eSPaolo Abeni 	sk_mem_reclaim_partial(sk);
974724cfd2eSPaolo Abeni 	msk->wmem_reserved = sk->sk_forward_alloc;
975724cfd2eSPaolo Abeni 	sk->sk_forward_alloc = 0;
976724cfd2eSPaolo Abeni 	mptcp_data_unlock(sk);
977724cfd2eSPaolo Abeni }
978724cfd2eSPaolo Abeni 
979d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len)
98018b683bfSPaolo Abeni {
981d027236cSPaolo Abeni 	sk_mem_uncharge(sk, len);
9827948f6ccSFlorian Westphal 	sk_wmem_queued_add(sk, -len);
983d027236cSPaolo Abeni }
984d027236cSPaolo Abeni 
985d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
986d027236cSPaolo Abeni {
987d027236cSPaolo Abeni 	int len = dfrag->data_len + dfrag->overhead;
988d027236cSPaolo Abeni 
98918b683bfSPaolo Abeni 	list_del(&dfrag->list);
990d027236cSPaolo Abeni 	dfrag_uncharge(sk, len);
99118b683bfSPaolo Abeni 	put_page(dfrag->page);
99218b683bfSPaolo Abeni }
99318b683bfSPaolo Abeni 
9946e628cd3SPaolo Abeni static void __mptcp_clean_una(struct sock *sk)
99518b683bfSPaolo Abeni {
99618b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
99718b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
998d027236cSPaolo Abeni 	bool cleaned = false;
999e1ff9e82SDavide Caratti 	u64 snd_una;
1000e1ff9e82SDavide Caratti 
1001e1ff9e82SDavide Caratti 	/* on fallback we just need to ignore snd_una, as this is really
1002e1ff9e82SDavide Caratti 	 * plain TCP
1003e1ff9e82SDavide Caratti 	 */
1004e1ff9e82SDavide Caratti 	if (__mptcp_check_fallback(msk))
10057439d687SPaolo Abeni 		msk->snd_una = READ_ONCE(msk->snd_nxt);
10067439d687SPaolo Abeni 
10077439d687SPaolo Abeni 	snd_una = msk->snd_una;
100818b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
100918b683bfSPaolo Abeni 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
101018b683bfSPaolo Abeni 			break;
101118b683bfSPaolo Abeni 
1012d9ca1de8SPaolo Abeni 		if (WARN_ON_ONCE(dfrag == msk->first_pending))
1013d9ca1de8SPaolo Abeni 			break;
1014d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
1015d027236cSPaolo Abeni 		cleaned = true;
1016d027236cSPaolo Abeni 	}
1017d027236cSPaolo Abeni 
10187948f6ccSFlorian Westphal 	dfrag = mptcp_rtx_head(sk);
10197948f6ccSFlorian Westphal 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
102053eb4c38SPaolo Abeni 		u64 delta = snd_una - dfrag->data_seq;
102153eb4c38SPaolo Abeni 
1022d9ca1de8SPaolo Abeni 		if (WARN_ON_ONCE(delta > dfrag->already_sent))
102353eb4c38SPaolo Abeni 			goto out;
10247948f6ccSFlorian Westphal 
10257948f6ccSFlorian Westphal 		dfrag->data_seq += delta;
102653eb4c38SPaolo Abeni 		dfrag->offset += delta;
10277948f6ccSFlorian Westphal 		dfrag->data_len -= delta;
1028d9ca1de8SPaolo Abeni 		dfrag->already_sent -= delta;
10297948f6ccSFlorian Westphal 
10307948f6ccSFlorian Westphal 		dfrag_uncharge(sk, delta);
10317948f6ccSFlorian Westphal 		cleaned = true;
10327948f6ccSFlorian Westphal 	}
10337948f6ccSFlorian Westphal 
103453eb4c38SPaolo Abeni out:
10356e628cd3SPaolo Abeni 	if (cleaned) {
10366e628cd3SPaolo Abeni 		if (tcp_under_memory_pressure(sk)) {
10376e628cd3SPaolo Abeni 			__mptcp_update_wmem(sk);
1038d027236cSPaolo Abeni 			sk_mem_reclaim_partial(sk);
103995ed690eSFlorian Westphal 		}
104095ed690eSFlorian Westphal 
10418edf0864SFlorian Westphal 		if (sk_stream_is_writeable(sk)) {
10426e628cd3SPaolo Abeni 			/* pairs with memory barrier in mptcp_poll */
10436e628cd3SPaolo Abeni 			smp_mb();
10446e628cd3SPaolo Abeni 			if (test_and_clear_bit(MPTCP_NOSPACE, &msk->flags))
10457948f6ccSFlorian Westphal 				sk_stream_write_space(sk);
104618b683bfSPaolo Abeni 		}
104718b683bfSPaolo Abeni 	}
104818b683bfSPaolo Abeni 
10496e628cd3SPaolo Abeni 	if (snd_una == READ_ONCE(msk->snd_nxt)) {
10506e628cd3SPaolo Abeni 		if (msk->timer_ival)
10516e628cd3SPaolo Abeni 			mptcp_stop_timer(sk);
10526e628cd3SPaolo Abeni 	} else {
10536e628cd3SPaolo Abeni 		mptcp_reset_timer(sk);
10546e628cd3SPaolo Abeni 	}
10556e628cd3SPaolo Abeni }
10566e628cd3SPaolo Abeni 
1057724cfd2eSPaolo Abeni static void mptcp_enter_memory_pressure(struct sock *sk)
105818b683bfSPaolo Abeni {
1059d9ca1de8SPaolo Abeni 	struct mptcp_subflow_context *subflow;
1060d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1061d9ca1de8SPaolo Abeni 	bool first = true;
1062d9ca1de8SPaolo Abeni 
106318b683bfSPaolo Abeni 	sk_stream_moderate_sndbuf(sk);
1064d9ca1de8SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1065d9ca1de8SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1066d9ca1de8SPaolo Abeni 
1067d9ca1de8SPaolo Abeni 		if (first)
1068d9ca1de8SPaolo Abeni 			tcp_enter_memory_pressure(ssk);
1069d9ca1de8SPaolo Abeni 		sk_stream_moderate_sndbuf(ssk);
1070d9ca1de8SPaolo Abeni 		first = false;
1071d9ca1de8SPaolo Abeni 	}
1072724cfd2eSPaolo Abeni }
1073724cfd2eSPaolo Abeni 
1074724cfd2eSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1075724cfd2eSPaolo Abeni  * data
1076724cfd2eSPaolo Abeni  */
1077724cfd2eSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1078724cfd2eSPaolo Abeni {
1079724cfd2eSPaolo Abeni 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1080724cfd2eSPaolo Abeni 					pfrag, sk->sk_allocation)))
1081724cfd2eSPaolo Abeni 		return true;
1082724cfd2eSPaolo Abeni 
1083724cfd2eSPaolo Abeni 	mptcp_enter_memory_pressure(sk);
108418b683bfSPaolo Abeni 	return false;
108518b683bfSPaolo Abeni }
108618b683bfSPaolo Abeni 
108718b683bfSPaolo Abeni static struct mptcp_data_frag *
108818b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
108918b683bfSPaolo Abeni 		      int orig_offset)
109018b683bfSPaolo Abeni {
109118b683bfSPaolo Abeni 	int offset = ALIGN(orig_offset, sizeof(long));
109218b683bfSPaolo Abeni 	struct mptcp_data_frag *dfrag;
109318b683bfSPaolo Abeni 
109418b683bfSPaolo Abeni 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
109518b683bfSPaolo Abeni 	dfrag->data_len = 0;
109618b683bfSPaolo Abeni 	dfrag->data_seq = msk->write_seq;
109718b683bfSPaolo Abeni 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
109818b683bfSPaolo Abeni 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1099d9ca1de8SPaolo Abeni 	dfrag->already_sent = 0;
110018b683bfSPaolo Abeni 	dfrag->page = pfrag->page;
110118b683bfSPaolo Abeni 
110218b683bfSPaolo Abeni 	return dfrag;
110318b683bfSPaolo Abeni }
110418b683bfSPaolo Abeni 
1105caf971dfSPaolo Abeni struct mptcp_sendmsg_info {
1106caf971dfSPaolo Abeni 	int mss_now;
1107caf971dfSPaolo Abeni 	int size_goal;
1108d9ca1de8SPaolo Abeni 	u16 limit;
1109d9ca1de8SPaolo Abeni 	u16 sent;
1110d9ca1de8SPaolo Abeni 	unsigned int flags;
1111caf971dfSPaolo Abeni };
1112caf971dfSPaolo Abeni 
11136f8a612aSFlorian Westphal static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
11146f8a612aSFlorian Westphal 				    int avail_size)
11156f8a612aSFlorian Westphal {
11166f8a612aSFlorian Westphal 	u64 window_end = mptcp_wnd_end(msk);
11176f8a612aSFlorian Westphal 
11186f8a612aSFlorian Westphal 	if (__mptcp_check_fallback(msk))
11196f8a612aSFlorian Westphal 		return avail_size;
11206f8a612aSFlorian Westphal 
11216f8a612aSFlorian Westphal 	if (!before64(data_seq + avail_size, window_end)) {
11226f8a612aSFlorian Westphal 		u64 allowed_size = window_end - data_seq;
11236f8a612aSFlorian Westphal 
11246f8a612aSFlorian Westphal 		return min_t(unsigned int, allowed_size, avail_size);
11256f8a612aSFlorian Westphal 	}
11266f8a612aSFlorian Westphal 
11276f8a612aSFlorian Westphal 	return avail_size;
11286f8a612aSFlorian Westphal }
11296f8a612aSFlorian Westphal 
1130724cfd2eSPaolo Abeni static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1131724cfd2eSPaolo Abeni {
1132724cfd2eSPaolo Abeni 	struct skb_ext *mpext = __skb_ext_alloc(gfp);
1133724cfd2eSPaolo Abeni 
1134724cfd2eSPaolo Abeni 	if (!mpext)
1135724cfd2eSPaolo Abeni 		return false;
1136724cfd2eSPaolo Abeni 	__skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1137724cfd2eSPaolo Abeni 	return true;
1138724cfd2eSPaolo Abeni }
1139724cfd2eSPaolo Abeni 
11406e628cd3SPaolo Abeni static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1141724cfd2eSPaolo Abeni {
1142724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1143724cfd2eSPaolo Abeni 
11446e628cd3SPaolo Abeni 	skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1145724cfd2eSPaolo Abeni 	if (likely(skb)) {
11466e628cd3SPaolo Abeni 		if (likely(__mptcp_add_ext(skb, gfp))) {
1147724cfd2eSPaolo Abeni 			skb_reserve(skb, MAX_TCP_HEADER);
1148724cfd2eSPaolo Abeni 			skb->reserved_tailroom = skb->end - skb->tail;
1149724cfd2eSPaolo Abeni 			return skb;
1150724cfd2eSPaolo Abeni 		}
1151724cfd2eSPaolo Abeni 		__kfree_skb(skb);
1152724cfd2eSPaolo Abeni 	} else {
1153724cfd2eSPaolo Abeni 		mptcp_enter_memory_pressure(sk);
1154724cfd2eSPaolo Abeni 	}
1155724cfd2eSPaolo Abeni 	return NULL;
1156724cfd2eSPaolo Abeni }
1157724cfd2eSPaolo Abeni 
1158724cfd2eSPaolo Abeni static bool mptcp_tx_cache_refill(struct sock *sk, int size,
1159724cfd2eSPaolo Abeni 				  struct sk_buff_head *skbs, int *total_ts)
1160724cfd2eSPaolo Abeni {
1161724cfd2eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1162724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1163724cfd2eSPaolo Abeni 	int space_needed;
1164724cfd2eSPaolo Abeni 
1165724cfd2eSPaolo Abeni 	if (unlikely(tcp_under_memory_pressure(sk))) {
1166724cfd2eSPaolo Abeni 		mptcp_mem_reclaim_partial(sk);
1167724cfd2eSPaolo Abeni 
1168724cfd2eSPaolo Abeni 		/* under pressure pre-allocate at most a single skb */
1169724cfd2eSPaolo Abeni 		if (msk->skb_tx_cache.qlen)
1170724cfd2eSPaolo Abeni 			return true;
1171724cfd2eSPaolo Abeni 		space_needed = msk->size_goal_cache;
1172724cfd2eSPaolo Abeni 	} else {
1173724cfd2eSPaolo Abeni 		space_needed = msk->tx_pending_data + size -
1174724cfd2eSPaolo Abeni 			       msk->skb_tx_cache.qlen * msk->size_goal_cache;
1175724cfd2eSPaolo Abeni 	}
1176724cfd2eSPaolo Abeni 
1177724cfd2eSPaolo Abeni 	while (space_needed > 0) {
11786e628cd3SPaolo Abeni 		skb = __mptcp_do_alloc_tx_skb(sk, sk->sk_allocation);
1179724cfd2eSPaolo Abeni 		if (unlikely(!skb)) {
1180724cfd2eSPaolo Abeni 			/* under memory pressure, try to pass the caller a
1181724cfd2eSPaolo Abeni 			 * single skb to allow forward progress
1182724cfd2eSPaolo Abeni 			 */
1183724cfd2eSPaolo Abeni 			while (skbs->qlen > 1) {
1184724cfd2eSPaolo Abeni 				skb = __skb_dequeue_tail(skbs);
1185724cfd2eSPaolo Abeni 				__kfree_skb(skb);
1186724cfd2eSPaolo Abeni 			}
1187724cfd2eSPaolo Abeni 			return skbs->qlen > 0;
1188724cfd2eSPaolo Abeni 		}
1189724cfd2eSPaolo Abeni 
1190724cfd2eSPaolo Abeni 		*total_ts += skb->truesize;
1191724cfd2eSPaolo Abeni 		__skb_queue_tail(skbs, skb);
1192724cfd2eSPaolo Abeni 		space_needed -= msk->size_goal_cache;
1193724cfd2eSPaolo Abeni 	}
1194724cfd2eSPaolo Abeni 	return true;
1195724cfd2eSPaolo Abeni }
1196724cfd2eSPaolo Abeni 
11976e628cd3SPaolo Abeni static bool __mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1198724cfd2eSPaolo Abeni {
1199724cfd2eSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1200724cfd2eSPaolo Abeni 	struct sk_buff *skb;
1201724cfd2eSPaolo Abeni 
1202724cfd2eSPaolo Abeni 	if (ssk->sk_tx_skb_cache) {
1203724cfd2eSPaolo Abeni 		skb = ssk->sk_tx_skb_cache;
1204724cfd2eSPaolo Abeni 		if (unlikely(!skb_ext_find(skb, SKB_EXT_MPTCP) &&
12056e628cd3SPaolo Abeni 			     !__mptcp_add_ext(skb, gfp)))
1206724cfd2eSPaolo Abeni 			return false;
1207724cfd2eSPaolo Abeni 		return true;
1208724cfd2eSPaolo Abeni 	}
1209724cfd2eSPaolo Abeni 
1210724cfd2eSPaolo Abeni 	skb = skb_peek(&msk->skb_tx_cache);
1211724cfd2eSPaolo Abeni 	if (skb) {
1212724cfd2eSPaolo Abeni 		if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1213724cfd2eSPaolo Abeni 			skb = __skb_dequeue(&msk->skb_tx_cache);
1214724cfd2eSPaolo Abeni 			if (WARN_ON_ONCE(!skb))
1215724cfd2eSPaolo Abeni 				return false;
1216724cfd2eSPaolo Abeni 
1217724cfd2eSPaolo Abeni 			mptcp_wmem_uncharge(sk, skb->truesize);
1218724cfd2eSPaolo Abeni 			ssk->sk_tx_skb_cache = skb;
1219724cfd2eSPaolo Abeni 			return true;
1220724cfd2eSPaolo Abeni 		}
1221724cfd2eSPaolo Abeni 
1222724cfd2eSPaolo Abeni 		/* over memory limit, no point to try to allocate a new skb */
1223724cfd2eSPaolo Abeni 		return false;
1224724cfd2eSPaolo Abeni 	}
1225724cfd2eSPaolo Abeni 
12266e628cd3SPaolo Abeni 	skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1227724cfd2eSPaolo Abeni 	if (!skb)
1228724cfd2eSPaolo Abeni 		return false;
1229724cfd2eSPaolo Abeni 
1230724cfd2eSPaolo Abeni 	if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1231724cfd2eSPaolo Abeni 		ssk->sk_tx_skb_cache = skb;
1232724cfd2eSPaolo Abeni 		return true;
1233724cfd2eSPaolo Abeni 	}
1234724cfd2eSPaolo Abeni 	kfree_skb(skb);
1235724cfd2eSPaolo Abeni 	return false;
1236724cfd2eSPaolo Abeni }
1237724cfd2eSPaolo Abeni 
1238724cfd2eSPaolo Abeni static bool mptcp_must_reclaim_memory(struct sock *sk, struct sock *ssk)
1239724cfd2eSPaolo Abeni {
1240724cfd2eSPaolo Abeni 	return !ssk->sk_tx_skb_cache &&
1241724cfd2eSPaolo Abeni 	       !skb_peek(&mptcp_sk(sk)->skb_tx_cache) &&
1242724cfd2eSPaolo Abeni 	       tcp_under_memory_pressure(sk);
1243724cfd2eSPaolo Abeni }
1244724cfd2eSPaolo Abeni 
1245724cfd2eSPaolo Abeni static bool mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk)
1246724cfd2eSPaolo Abeni {
1247724cfd2eSPaolo Abeni 	if (unlikely(mptcp_must_reclaim_memory(sk, ssk)))
1248724cfd2eSPaolo Abeni 		mptcp_mem_reclaim_partial(sk);
12496e628cd3SPaolo Abeni 	return __mptcp_alloc_tx_skb(sk, ssk, sk->sk_allocation);
1250724cfd2eSPaolo Abeni }
1251724cfd2eSPaolo Abeni 
125257040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1253d9ca1de8SPaolo Abeni 			      struct mptcp_data_frag *dfrag,
1254caf971dfSPaolo Abeni 			      struct mptcp_sendmsg_info *info)
125557040755SPaolo Abeni {
1256d9ca1de8SPaolo Abeni 	u64 data_seq = dfrag->data_seq + info->sent;
12576d0060f6SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
12586f8a612aSFlorian Westphal 	bool zero_window_probe = false;
12596d0060f6SMat Martineau 	struct mptcp_ext *mpext = NULL;
126057040755SPaolo Abeni 	struct sk_buff *skb, *tail;
1261d9ca1de8SPaolo Abeni 	bool can_collapse = false;
126215e6ca97SPaolo Abeni 	int size_bias = 0;
1263d9ca1de8SPaolo Abeni 	int avail_size;
1264724cfd2eSPaolo Abeni 	size_t ret = 0;
12656d0060f6SMat Martineau 
1266d9ca1de8SPaolo Abeni 	pr_debug("msk=%p ssk=%p sending dfrag at seq=%lld len=%d already sent=%d",
1267d9ca1de8SPaolo Abeni 		 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
12686d0060f6SMat Martineau 
1269d9ca1de8SPaolo Abeni 	/* compute send limit */
1270d9ca1de8SPaolo Abeni 	info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1271caf971dfSPaolo Abeni 	avail_size = info->size_goal;
1272724cfd2eSPaolo Abeni 	msk->size_goal_cache = info->size_goal;
127357040755SPaolo Abeni 	skb = tcp_write_queue_tail(ssk);
127457040755SPaolo Abeni 	if (skb) {
127557040755SPaolo Abeni 		/* Limit the write to the size available in the
127657040755SPaolo Abeni 		 * current skb, if any, so that we create at most a new skb.
127757040755SPaolo Abeni 		 * Explicitly tells TCP internals to avoid collapsing on later
127857040755SPaolo Abeni 		 * queue management operation, to avoid breaking the ext <->
127957040755SPaolo Abeni 		 * SSN association set here
128057040755SPaolo Abeni 		 */
1281d9ca1de8SPaolo Abeni 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1282caf971dfSPaolo Abeni 		can_collapse = (info->size_goal - skb->len > 0) &&
1283d9ca1de8SPaolo Abeni 			 mptcp_skb_can_collapse_to(data_seq, skb, mpext);
128415e6ca97SPaolo Abeni 		if (!can_collapse) {
128557040755SPaolo Abeni 			TCP_SKB_CB(skb)->eor = 1;
128615e6ca97SPaolo Abeni 		} else {
128715e6ca97SPaolo Abeni 			size_bias = skb->len;
1288caf971dfSPaolo Abeni 			avail_size = info->size_goal - skb->len;
128957040755SPaolo Abeni 		}
129015e6ca97SPaolo Abeni 	}
129118b683bfSPaolo Abeni 
12926f8a612aSFlorian Westphal 	/* Zero window and all data acked? Probe. */
12936f8a612aSFlorian Westphal 	avail_size = mptcp_check_allowed_size(msk, data_seq, avail_size);
12946f8a612aSFlorian Westphal 	if (avail_size == 0) {
12957439d687SPaolo Abeni 		u64 snd_una = READ_ONCE(msk->snd_una);
12967439d687SPaolo Abeni 
12977439d687SPaolo Abeni 		if (skb || snd_una != msk->snd_nxt)
12986f8a612aSFlorian Westphal 			return 0;
12996f8a612aSFlorian Westphal 		zero_window_probe = true;
13007439d687SPaolo Abeni 		data_seq = snd_una - 1;
13016f8a612aSFlorian Westphal 		avail_size = 1;
13026f8a612aSFlorian Westphal 	}
13036f8a612aSFlorian Westphal 
1304d9ca1de8SPaolo Abeni 	if (WARN_ON_ONCE(info->sent > info->limit ||
1305d9ca1de8SPaolo Abeni 			 info->limit > dfrag->data_len))
1306d9ca1de8SPaolo Abeni 		return 0;
130757040755SPaolo Abeni 
1308d9ca1de8SPaolo Abeni 	ret = info->limit - info->sent;
130915e6ca97SPaolo Abeni 	tail = tcp_build_frag(ssk, avail_size + size_bias, info->flags,
131015e6ca97SPaolo Abeni 			      dfrag->page, dfrag->offset + info->sent, &ret);
1311e2223995SPaolo Abeni 	if (!tail) {
1312e2223995SPaolo Abeni 		tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk));
1313e2223995SPaolo Abeni 		return -ENOMEM;
131435759383SFlorian Westphal 	}
131518b683bfSPaolo Abeni 
1316e2223995SPaolo Abeni 	/* if the tail skb is still the cached one, collapsing really happened.
131757040755SPaolo Abeni 	 */
1318e2223995SPaolo Abeni 	if (skb == tail) {
131915e6ca97SPaolo Abeni 		TCP_SKB_CB(tail)->tcp_flags &= ~TCPHDR_PSH;
132057040755SPaolo Abeni 		mpext->data_len += ret;
132115e6ca97SPaolo Abeni 		WARN_ON_ONCE(!can_collapse);
13226f8a612aSFlorian Westphal 		WARN_ON_ONCE(zero_window_probe);
132357040755SPaolo Abeni 		goto out;
132457040755SPaolo Abeni 	}
132557040755SPaolo Abeni 
1326724cfd2eSPaolo Abeni 	mpext = skb_ext_find(tail, SKB_EXT_MPTCP);
1327724cfd2eSPaolo Abeni 	if (WARN_ON_ONCE(!mpext)) {
1328724cfd2eSPaolo Abeni 		/* should never reach here, stream corrupted */
1329724cfd2eSPaolo Abeni 		return -EINVAL;
1330724cfd2eSPaolo Abeni 	}
13316d0060f6SMat Martineau 
13326d0060f6SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1333d9ca1de8SPaolo Abeni 	mpext->data_seq = data_seq;
13346d0060f6SMat Martineau 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
13356d0060f6SMat Martineau 	mpext->data_len = ret;
13366d0060f6SMat Martineau 	mpext->use_map = 1;
13376d0060f6SMat Martineau 	mpext->dsn64 = 1;
13386d0060f6SMat Martineau 
13396d0060f6SMat Martineau 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
13406d0060f6SMat Martineau 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
13416d0060f6SMat Martineau 		 mpext->dsn64);
13426d0060f6SMat Martineau 
13436f8a612aSFlorian Westphal 	if (zero_window_probe) {
13446f8a612aSFlorian Westphal 		mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
13456f8a612aSFlorian Westphal 		mpext->frozen = 1;
13466f8a612aSFlorian Westphal 		ret = 0;
13476f8a612aSFlorian Westphal 		tcp_push_pending_frames(ssk);
13486f8a612aSFlorian Westphal 	}
134957040755SPaolo Abeni out:
13506d0060f6SMat Martineau 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
13516d0060f6SMat Martineau 	return ret;
13526d0060f6SMat Martineau }
13536d0060f6SMat Martineau 
1354d5f49190SPaolo Abeni #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
1355d5f49190SPaolo Abeni 					 sizeof(struct tcphdr) - \
1356d5f49190SPaolo Abeni 					 MAX_TCP_OPTION_SPACE - \
1357d5f49190SPaolo Abeni 					 sizeof(struct ipv6hdr) - \
1358d5f49190SPaolo Abeni 					 sizeof(struct frag_hdr))
1359d5f49190SPaolo Abeni 
1360d5f49190SPaolo Abeni struct subflow_send_info {
1361d5f49190SPaolo Abeni 	struct sock *ssk;
1362d5f49190SPaolo Abeni 	u64 ratio;
1363d5f49190SPaolo Abeni };
1364d5f49190SPaolo Abeni 
1365da51aef5SPaolo Abeni static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk,
1366da51aef5SPaolo Abeni 					   u32 *sndbuf)
1367f296234cSPeter Krystad {
1368d5f49190SPaolo Abeni 	struct subflow_send_info send_info[2];
1369f296234cSPeter Krystad 	struct mptcp_subflow_context *subflow;
1370d5f49190SPaolo Abeni 	int i, nr_active = 0;
1371d5f49190SPaolo Abeni 	struct sock *ssk;
1372d5f49190SPaolo Abeni 	u64 ratio;
1373d5f49190SPaolo Abeni 	u32 pace;
1374f296234cSPeter Krystad 
1375d5f49190SPaolo Abeni 	sock_owned_by_me((struct sock *)msk);
1376f296234cSPeter Krystad 
1377da51aef5SPaolo Abeni 	*sndbuf = 0;
1378d5f49190SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
1379d5f49190SPaolo Abeni 		if (!msk->first)
1380f296234cSPeter Krystad 			return NULL;
1381d5f49190SPaolo Abeni 		*sndbuf = msk->first->sk_sndbuf;
1382d5f49190SPaolo Abeni 		return sk_stream_memory_free(msk->first) ? msk->first : NULL;
1383f296234cSPeter Krystad 	}
1384f296234cSPeter Krystad 
1385d5f49190SPaolo Abeni 	/* re-use last subflow, if the burst allow that */
1386d5f49190SPaolo Abeni 	if (msk->last_snd && msk->snd_burst > 0 &&
1387d5f49190SPaolo Abeni 	    sk_stream_memory_free(msk->last_snd) &&
1388d5f49190SPaolo Abeni 	    mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1389d5f49190SPaolo Abeni 		mptcp_for_each_subflow(msk, subflow) {
1390d5f49190SPaolo Abeni 			ssk =  mptcp_subflow_tcp_sock(subflow);
1391da51aef5SPaolo Abeni 			*sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
1392d5f49190SPaolo Abeni 		}
1393d5f49190SPaolo Abeni 		return msk->last_snd;
1394d5f49190SPaolo Abeni 	}
1395f296234cSPeter Krystad 
1396d5f49190SPaolo Abeni 	/* pick the subflow with the lower wmem/wspace ratio */
1397d5f49190SPaolo Abeni 	for (i = 0; i < 2; ++i) {
1398d5f49190SPaolo Abeni 		send_info[i].ssk = NULL;
1399d5f49190SPaolo Abeni 		send_info[i].ratio = -1;
1400d5f49190SPaolo Abeni 	}
1401d5f49190SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
1402d5f49190SPaolo Abeni 		ssk =  mptcp_subflow_tcp_sock(subflow);
1403d5f49190SPaolo Abeni 		if (!mptcp_subflow_active(subflow))
1404f296234cSPeter Krystad 			continue;
1405d5f49190SPaolo Abeni 
1406d5f49190SPaolo Abeni 		nr_active += !subflow->backup;
1407d5f49190SPaolo Abeni 		*sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
1408d5f49190SPaolo Abeni 		if (!sk_stream_memory_free(subflow->tcp_sock))
1409d5f49190SPaolo Abeni 			continue;
1410d5f49190SPaolo Abeni 
1411d5f49190SPaolo Abeni 		pace = READ_ONCE(ssk->sk_pacing_rate);
1412d5f49190SPaolo Abeni 		if (!pace)
1413d5f49190SPaolo Abeni 			continue;
1414d5f49190SPaolo Abeni 
1415d5f49190SPaolo Abeni 		ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1416d5f49190SPaolo Abeni 				pace);
1417d5f49190SPaolo Abeni 		if (ratio < send_info[subflow->backup].ratio) {
1418d5f49190SPaolo Abeni 			send_info[subflow->backup].ssk = ssk;
1419d5f49190SPaolo Abeni 			send_info[subflow->backup].ratio = ratio;
1420d5f49190SPaolo Abeni 		}
1421f296234cSPeter Krystad 	}
1422f296234cSPeter Krystad 
1423d5f49190SPaolo Abeni 	pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld",
1424d5f49190SPaolo Abeni 		 msk, nr_active, send_info[0].ssk, send_info[0].ratio,
1425d5f49190SPaolo Abeni 		 send_info[1].ssk, send_info[1].ratio);
1426f296234cSPeter Krystad 
1427d5f49190SPaolo Abeni 	/* pick the best backup if no other subflow is active */
1428d5f49190SPaolo Abeni 	if (!nr_active)
1429d5f49190SPaolo Abeni 		send_info[0].ssk = send_info[1].ssk;
1430d5f49190SPaolo Abeni 
1431d5f49190SPaolo Abeni 	if (send_info[0].ssk) {
1432d5f49190SPaolo Abeni 		msk->last_snd = send_info[0].ssk;
1433d5f49190SPaolo Abeni 		msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
1434d5f49190SPaolo Abeni 				       sk_stream_wspace(msk->last_snd));
1435d5f49190SPaolo Abeni 		return msk->last_snd;
1436d5f49190SPaolo Abeni 	}
1437d5f49190SPaolo Abeni 	return NULL;
1438f296234cSPeter Krystad }
1439f296234cSPeter Krystad 
1440d9ca1de8SPaolo Abeni static void mptcp_push_release(struct sock *sk, struct sock *ssk,
1441d9ca1de8SPaolo Abeni 			       struct mptcp_sendmsg_info *info)
1442d9ca1de8SPaolo Abeni {
1443d9ca1de8SPaolo Abeni 	mptcp_set_timeout(sk, ssk);
1444d9ca1de8SPaolo Abeni 	tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1445d9ca1de8SPaolo Abeni 	release_sock(ssk);
1446d9ca1de8SPaolo Abeni }
1447d9ca1de8SPaolo Abeni 
1448d9ca1de8SPaolo Abeni static void mptcp_push_pending(struct sock *sk, unsigned int flags)
1449d9ca1de8SPaolo Abeni {
1450d9ca1de8SPaolo Abeni 	struct sock *prev_ssk = NULL, *ssk = NULL;
1451d9ca1de8SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
1452d9ca1de8SPaolo Abeni 	struct mptcp_sendmsg_info info = {
1453d9ca1de8SPaolo Abeni 				.flags = flags,
1454d9ca1de8SPaolo Abeni 	};
1455d9ca1de8SPaolo Abeni 	struct mptcp_data_frag *dfrag;
1456d9ca1de8SPaolo Abeni 	int len, copied = 0;
1457d9ca1de8SPaolo Abeni 	u32 sndbuf;
1458d9ca1de8SPaolo Abeni 
1459d9ca1de8SPaolo Abeni 	while ((dfrag = mptcp_send_head(sk))) {
1460d9ca1de8SPaolo Abeni 		info.sent = dfrag->already_sent;
1461d9ca1de8SPaolo Abeni 		info.limit = dfrag->data_len;
1462d9ca1de8SPaolo Abeni 		len = dfrag->data_len - dfrag->already_sent;
1463d9ca1de8SPaolo Abeni 		while (len > 0) {
1464d9ca1de8SPaolo Abeni 			int ret = 0;
1465d9ca1de8SPaolo Abeni 
1466d9ca1de8SPaolo Abeni 			prev_ssk = ssk;
1467d9ca1de8SPaolo Abeni 			__mptcp_flush_join_list(msk);
1468d9ca1de8SPaolo Abeni 			ssk = mptcp_subflow_get_send(msk, &sndbuf);
1469d9ca1de8SPaolo Abeni 
1470d9ca1de8SPaolo Abeni 			/* do auto tuning */
1471d9ca1de8SPaolo Abeni 			if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) &&
1472d9ca1de8SPaolo Abeni 			    sndbuf > READ_ONCE(sk->sk_sndbuf))
1473d9ca1de8SPaolo Abeni 				WRITE_ONCE(sk->sk_sndbuf, sndbuf);
1474d9ca1de8SPaolo Abeni 
1475d9ca1de8SPaolo Abeni 			/* try to keep the subflow socket lock across
1476d9ca1de8SPaolo Abeni 			 * consecutive xmit on the same socket
1477d9ca1de8SPaolo Abeni 			 */
1478d9ca1de8SPaolo Abeni 			if (ssk != prev_ssk && prev_ssk)
1479d9ca1de8SPaolo Abeni 				mptcp_push_release(sk, prev_ssk, &info);
1480d9ca1de8SPaolo Abeni 			if (!ssk)
1481d9ca1de8SPaolo Abeni 				goto out;
1482d9ca1de8SPaolo Abeni 
1483d9ca1de8SPaolo Abeni 			if (ssk != prev_ssk || !prev_ssk)
1484d9ca1de8SPaolo Abeni 				lock_sock(ssk);
1485d9ca1de8SPaolo Abeni 
1486724cfd2eSPaolo Abeni 			/* keep it simple and always provide a new skb for the
1487724cfd2eSPaolo Abeni 			 * subflow, even if we will not use it when collapsing
1488724cfd2eSPaolo Abeni 			 * on the pending one
1489724cfd2eSPaolo Abeni 			 */
1490724cfd2eSPaolo Abeni 			if (!mptcp_alloc_tx_skb(sk, ssk)) {
1491724cfd2eSPaolo Abeni 				mptcp_push_release(sk, ssk, &info);
1492724cfd2eSPaolo Abeni 				goto out;
1493724cfd2eSPaolo Abeni 			}
1494724cfd2eSPaolo Abeni 
1495d9ca1de8SPaolo Abeni 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1496d9ca1de8SPaolo Abeni 			if (ret <= 0) {
1497d9ca1de8SPaolo Abeni 				mptcp_push_release(sk, ssk, &info);
1498d9ca1de8SPaolo Abeni 				goto out;
1499d9ca1de8SPaolo Abeni 			}
1500d9ca1de8SPaolo Abeni 
1501d9ca1de8SPaolo Abeni 			info.sent += ret;
1502d9ca1de8SPaolo Abeni 			dfrag->already_sent += ret;
1503d9ca1de8SPaolo Abeni 			msk->snd_nxt += ret;
1504d9ca1de8SPaolo Abeni 			msk->snd_burst -= ret;
1505724cfd2eSPaolo Abeni 			msk->tx_pending_data -= ret;
1506d9ca1de8SPaolo Abeni 			copied += ret;
1507d9ca1de8SPaolo Abeni 			len -= ret;
1508d9ca1de8SPaolo Abeni 		}
1509d9ca1de8SPaolo Abeni 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1510d9ca1de8SPaolo Abeni 	}
1511d9ca1de8SPaolo Abeni 
1512d9ca1de8SPaolo Abeni 	/* at this point we held the socket lock for the last subflow we used */
1513d9ca1de8SPaolo Abeni 	if (ssk)
1514d9ca1de8SPaolo Abeni 		mptcp_push_release(sk, ssk, &info);
1515d9ca1de8SPaolo Abeni 
1516d9ca1de8SPaolo Abeni out:
1517b680a214SPaolo Abeni 	if (copied) {
1518d9ca1de8SPaolo Abeni 		/* start the timer, if it's not pending */
1519d9ca1de8SPaolo Abeni 		if (!mptcp_timer_pending(sk))
1520d9ca1de8SPaolo Abeni 			mptcp_reset_timer(sk);
1521d9ca1de8SPaolo Abeni 		__mptcp_check_send_data_fin(sk);
1522d9ca1de8SPaolo Abeni 	}
1523b680a214SPaolo Abeni }
1524d9ca1de8SPaolo Abeni 
15256e628cd3SPaolo Abeni static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
15266e628cd3SPaolo Abeni {
15276e628cd3SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
15286e628cd3SPaolo Abeni 	struct mptcp_sendmsg_info info;
15296e628cd3SPaolo Abeni 	struct mptcp_data_frag *dfrag;
15306e628cd3SPaolo Abeni 	int len, copied = 0;
15316e628cd3SPaolo Abeni 
15326e628cd3SPaolo Abeni 	info.flags = 0;
15336e628cd3SPaolo Abeni 	while ((dfrag = mptcp_send_head(sk))) {
15346e628cd3SPaolo Abeni 		info.sent = dfrag->already_sent;
15356e628cd3SPaolo Abeni 		info.limit = dfrag->data_len;
15366e628cd3SPaolo Abeni 		len = dfrag->data_len - dfrag->already_sent;
15376e628cd3SPaolo Abeni 		while (len > 0) {
15386e628cd3SPaolo Abeni 			int ret = 0;
15396e628cd3SPaolo Abeni 
15406e628cd3SPaolo Abeni 			/* do auto tuning */
15416e628cd3SPaolo Abeni 			if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) &&
15426e628cd3SPaolo Abeni 			    ssk->sk_sndbuf > READ_ONCE(sk->sk_sndbuf))
15436e628cd3SPaolo Abeni 				WRITE_ONCE(sk->sk_sndbuf, ssk->sk_sndbuf);
15446e628cd3SPaolo Abeni 
15456e628cd3SPaolo Abeni 			if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) {
15466e628cd3SPaolo Abeni 				__mptcp_update_wmem(sk);
15476e628cd3SPaolo Abeni 				sk_mem_reclaim_partial(sk);
15486e628cd3SPaolo Abeni 			}
15496e628cd3SPaolo Abeni 			if (!__mptcp_alloc_tx_skb(sk, ssk, GFP_ATOMIC))
15506e628cd3SPaolo Abeni 				goto out;
15516e628cd3SPaolo Abeni 
15526e628cd3SPaolo Abeni 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
15536e628cd3SPaolo Abeni 			if (ret <= 0)
15546e628cd3SPaolo Abeni 				goto out;
15556e628cd3SPaolo Abeni 
15566e628cd3SPaolo Abeni 			info.sent += ret;
15576e628cd3SPaolo Abeni 			dfrag->already_sent += ret;
15586e628cd3SPaolo Abeni 			msk->snd_nxt += ret;
15596e628cd3SPaolo Abeni 			msk->snd_burst -= ret;
15606e628cd3SPaolo Abeni 			msk->tx_pending_data -= ret;
15616e628cd3SPaolo Abeni 			copied += ret;
15626e628cd3SPaolo Abeni 			len -= ret;
15636e628cd3SPaolo Abeni 		}
15646e628cd3SPaolo Abeni 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
15656e628cd3SPaolo Abeni 	}
15666e628cd3SPaolo Abeni 
15676e628cd3SPaolo Abeni out:
15686e628cd3SPaolo Abeni 	/* __mptcp_alloc_tx_skb could have released some wmem and we are
15696e628cd3SPaolo Abeni 	 * not going to flush it via release_sock()
15706e628cd3SPaolo Abeni 	 */
15716e628cd3SPaolo Abeni 	__mptcp_update_wmem(sk);
15726e628cd3SPaolo Abeni 	if (copied) {
15736e628cd3SPaolo Abeni 		mptcp_set_timeout(sk, ssk);
15746e628cd3SPaolo Abeni 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
15756e628cd3SPaolo Abeni 			 info.size_goal);
15766e628cd3SPaolo Abeni 		if (msk->snd_data_fin_enable &&
15776e628cd3SPaolo Abeni 		    msk->snd_nxt + 1 == msk->write_seq)
15786e628cd3SPaolo Abeni 			mptcp_schedule_work(sk);
15796e628cd3SPaolo Abeni 	}
15806e628cd3SPaolo Abeni }
15816e628cd3SPaolo Abeni 
1582f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1583f870fa0bSMat Martineau {
1584f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
158517091708SFlorian Westphal 	struct page_frag *pfrag;
15866d0060f6SMat Martineau 	size_t copied = 0;
1587caf971dfSPaolo Abeni 	int ret = 0;
15886d0060f6SMat Martineau 	long timeo;
1589f870fa0bSMat Martineau 
1590f870fa0bSMat Martineau 	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
1591f870fa0bSMat Martineau 		return -EOPNOTSUPP;
1592f870fa0bSMat Martineau 
1593e7579d5dSDavide Caratti 	mptcp_lock_sock(sk, __mptcp_wmem_reserve(sk, min_t(size_t, 1 << 20, len)));
15941954b860SMat Martineau 
15951954b860SMat Martineau 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
15961954b860SMat Martineau 
15971954b860SMat Martineau 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
15981954b860SMat Martineau 		ret = sk_stream_wait_connect(sk, &timeo);
15991954b860SMat Martineau 		if (ret)
16001954b860SMat Martineau 			goto out;
16011954b860SMat Martineau 	}
16021954b860SMat Martineau 
160317091708SFlorian Westphal 	pfrag = sk_page_frag(sk);
160418b683bfSPaolo Abeni 
1605d9ca1de8SPaolo Abeni 	while (msg_data_left(msg)) {
1606724cfd2eSPaolo Abeni 		int total_ts, frag_truesize = 0;
1607d9ca1de8SPaolo Abeni 		struct mptcp_data_frag *dfrag;
1608724cfd2eSPaolo Abeni 		struct sk_buff_head skbs;
1609d9ca1de8SPaolo Abeni 		bool dfrag_collapsed;
1610d9ca1de8SPaolo Abeni 		size_t psize, offset;
1611d9ca1de8SPaolo Abeni 
161257baaf28SMat Martineau 		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
161357baaf28SMat Martineau 			ret = -EPIPE;
161457baaf28SMat Martineau 			goto out;
161557baaf28SMat Martineau 		}
161657baaf28SMat Martineau 
1617d9ca1de8SPaolo Abeni 		/* reuse tail pfrag, if possible, or carve a new one from the
1618d9ca1de8SPaolo Abeni 		 * page allocator
1619fb529e62SFlorian Westphal 		 */
1620d9ca1de8SPaolo Abeni 		dfrag = mptcp_pending_tail(sk);
1621d9ca1de8SPaolo Abeni 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1622d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1623d9ca1de8SPaolo Abeni 			if (!sk_stream_memory_free(sk))
1624d9ca1de8SPaolo Abeni 				goto wait_for_memory;
16256e628cd3SPaolo Abeni 
1626d9ca1de8SPaolo Abeni 			if (!mptcp_page_frag_refill(sk, pfrag))
1627d9ca1de8SPaolo Abeni 				goto wait_for_memory;
1628d9ca1de8SPaolo Abeni 
1629d9ca1de8SPaolo Abeni 			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1630d9ca1de8SPaolo Abeni 			frag_truesize = dfrag->overhead;
1631fb529e62SFlorian Westphal 		}
1632fb529e62SFlorian Westphal 
1633d9ca1de8SPaolo Abeni 		/* we do not bound vs wspace, to allow a single packet.
1634d9ca1de8SPaolo Abeni 		 * memory accounting will prevent execessive memory usage
1635d9ca1de8SPaolo Abeni 		 * anyway
1636d9ca1de8SPaolo Abeni 		 */
1637d9ca1de8SPaolo Abeni 		offset = dfrag->offset + dfrag->data_len;
1638d9ca1de8SPaolo Abeni 		psize = pfrag->size - offset;
1639d9ca1de8SPaolo Abeni 		psize = min_t(size_t, psize, msg_data_left(msg));
1640724cfd2eSPaolo Abeni 		total_ts = psize + frag_truesize;
1641724cfd2eSPaolo Abeni 		__skb_queue_head_init(&skbs);
1642724cfd2eSPaolo Abeni 		if (!mptcp_tx_cache_refill(sk, psize, &skbs, &total_ts))
1643d9ca1de8SPaolo Abeni 			goto wait_for_memory;
1644d9ca1de8SPaolo Abeni 
1645724cfd2eSPaolo Abeni 		if (!mptcp_wmem_alloc(sk, total_ts)) {
1646724cfd2eSPaolo Abeni 			__skb_queue_purge(&skbs);
1647724cfd2eSPaolo Abeni 			goto wait_for_memory;
1648724cfd2eSPaolo Abeni 		}
1649724cfd2eSPaolo Abeni 
1650724cfd2eSPaolo Abeni 		skb_queue_splice_tail(&skbs, &msk->skb_tx_cache);
1651d9ca1de8SPaolo Abeni 		if (copy_page_from_iter(dfrag->page, offset, psize,
1652d9ca1de8SPaolo Abeni 					&msg->msg_iter) != psize) {
165387952603SPaolo Abeni 			mptcp_wmem_uncharge(sk, psize + frag_truesize);
1654d9ca1de8SPaolo Abeni 			ret = -EFAULT;
1655d9ca1de8SPaolo Abeni 			goto out;
1656d9ca1de8SPaolo Abeni 		}
1657d9ca1de8SPaolo Abeni 
1658d9ca1de8SPaolo Abeni 		/* data successfully copied into the write queue */
1659d9ca1de8SPaolo Abeni 		copied += psize;
1660d9ca1de8SPaolo Abeni 		dfrag->data_len += psize;
1661d9ca1de8SPaolo Abeni 		frag_truesize += psize;
1662d9ca1de8SPaolo Abeni 		pfrag->offset += frag_truesize;
1663d9ca1de8SPaolo Abeni 		WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
166413e16037SPaolo Abeni 		msk->tx_pending_data += psize;
1665d9ca1de8SPaolo Abeni 
1666d9ca1de8SPaolo Abeni 		/* charge data on mptcp pending queue to the msk socket
1667d9ca1de8SPaolo Abeni 		 * Note: we charge such data both to sk and ssk
1668d9ca1de8SPaolo Abeni 		 */
1669d9ca1de8SPaolo Abeni 		sk_wmem_queued_add(sk, frag_truesize);
1670d9ca1de8SPaolo Abeni 		if (!dfrag_collapsed) {
1671d9ca1de8SPaolo Abeni 			get_page(dfrag->page);
1672d9ca1de8SPaolo Abeni 			list_add_tail(&dfrag->list, &msk->rtx_queue);
1673d9ca1de8SPaolo Abeni 			if (!msk->first_pending)
1674d9ca1de8SPaolo Abeni 				WRITE_ONCE(msk->first_pending, dfrag);
1675d9ca1de8SPaolo Abeni 		}
1676d9ca1de8SPaolo Abeni 		pr_debug("msk=%p dfrag at seq=%lld len=%d sent=%d new=%d", msk,
1677d9ca1de8SPaolo Abeni 			 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1678d9ca1de8SPaolo Abeni 			 !dfrag_collapsed);
1679d9ca1de8SPaolo Abeni 
1680d9ca1de8SPaolo Abeni 		continue;
1681d9ca1de8SPaolo Abeni 
1682d9ca1de8SPaolo Abeni wait_for_memory:
16836e628cd3SPaolo Abeni 		set_bit(MPTCP_NOSPACE, &msk->flags);
16846e628cd3SPaolo Abeni 		mptcp_push_pending(sk, msg->msg_flags);
1685f296234cSPeter Krystad 		ret = sk_stream_wait_memory(sk, &timeo);
1686f296234cSPeter Krystad 		if (ret)
1687f296234cSPeter Krystad 			goto out;
1688cec37a6eSPeter Krystad 	}
1689cec37a6eSPeter Krystad 
169013e16037SPaolo Abeni 	if (copied)
1691d9ca1de8SPaolo Abeni 		mptcp_push_pending(sk, msg->msg_flags);
1692da51aef5SPaolo Abeni 
16931954b860SMat Martineau out:
1694cec37a6eSPeter Krystad 	release_sock(sk);
16958555c6bfSPaolo Abeni 	return copied ? : ret;
1696f870fa0bSMat Martineau }
1697f870fa0bSMat Martineau 
16987a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo)
16997a6a6cbcSPaolo Abeni {
17007a6a6cbcSPaolo Abeni 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
17017a6a6cbcSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
17027a6a6cbcSPaolo Abeni 
17037a6a6cbcSPaolo Abeni 	add_wait_queue(sk_sleep(sk), &wait);
17047a6a6cbcSPaolo Abeni 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
17057a6a6cbcSPaolo Abeni 
17067a6a6cbcSPaolo Abeni 	sk_wait_event(sk, timeo,
17077a6a6cbcSPaolo Abeni 		      test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
17087a6a6cbcSPaolo Abeni 
17097a6a6cbcSPaolo Abeni 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
17107a6a6cbcSPaolo Abeni 	remove_wait_queue(sk_sleep(sk), &wait);
17117a6a6cbcSPaolo Abeni }
17127a6a6cbcSPaolo Abeni 
17136771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
17146771bfd9SFlorian Westphal 				struct msghdr *msg,
17156771bfd9SFlorian Westphal 				size_t len)
17166771bfd9SFlorian Westphal {
17176771bfd9SFlorian Westphal 	struct sk_buff *skb;
17186771bfd9SFlorian Westphal 	int copied = 0;
17196771bfd9SFlorian Westphal 
172087952603SPaolo Abeni 	while ((skb = skb_peek(&msk->receive_queue)) != NULL) {
17216771bfd9SFlorian Westphal 		u32 offset = MPTCP_SKB_CB(skb)->offset;
17226771bfd9SFlorian Westphal 		u32 data_len = skb->len - offset;
17236771bfd9SFlorian Westphal 		u32 count = min_t(size_t, len - copied, data_len);
17246771bfd9SFlorian Westphal 		int err;
17256771bfd9SFlorian Westphal 
17266771bfd9SFlorian Westphal 		err = skb_copy_datagram_msg(skb, offset, msg, count);
17276771bfd9SFlorian Westphal 		if (unlikely(err < 0)) {
17286771bfd9SFlorian Westphal 			if (!copied)
17296771bfd9SFlorian Westphal 				return err;
17306771bfd9SFlorian Westphal 			break;
17316771bfd9SFlorian Westphal 		}
17326771bfd9SFlorian Westphal 
17336771bfd9SFlorian Westphal 		copied += count;
17346771bfd9SFlorian Westphal 
17356771bfd9SFlorian Westphal 		if (count < data_len) {
17366771bfd9SFlorian Westphal 			MPTCP_SKB_CB(skb)->offset += count;
17376771bfd9SFlorian Westphal 			break;
17386771bfd9SFlorian Westphal 		}
17396771bfd9SFlorian Westphal 
174087952603SPaolo Abeni 		/* we will bulk release the skb memory later */
174187952603SPaolo Abeni 		skb->destructor = NULL;
174287952603SPaolo Abeni 		msk->rmem_released += skb->truesize;
174387952603SPaolo Abeni 		__skb_unlink(skb, &msk->receive_queue);
17446771bfd9SFlorian Westphal 		__kfree_skb(skb);
17456771bfd9SFlorian Westphal 
17466771bfd9SFlorian Westphal 		if (copied >= len)
17476771bfd9SFlorian Westphal 			break;
17486771bfd9SFlorian Westphal 	}
17496771bfd9SFlorian Westphal 
17506771bfd9SFlorian Westphal 	return copied;
17516771bfd9SFlorian Westphal }
17526771bfd9SFlorian Westphal 
1753a6b118feSFlorian Westphal /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
1754a6b118feSFlorian Westphal  *
1755a6b118feSFlorian Westphal  * Only difference: Use highest rtt estimate of the subflows in use.
1756a6b118feSFlorian Westphal  */
1757a6b118feSFlorian Westphal static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1758a6b118feSFlorian Westphal {
1759a6b118feSFlorian Westphal 	struct mptcp_subflow_context *subflow;
1760a6b118feSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
1761a6b118feSFlorian Westphal 	u32 time, advmss = 1;
1762a6b118feSFlorian Westphal 	u64 rtt_us, mstamp;
1763a6b118feSFlorian Westphal 
1764a6b118feSFlorian Westphal 	sock_owned_by_me(sk);
1765a6b118feSFlorian Westphal 
1766a6b118feSFlorian Westphal 	if (copied <= 0)
1767a6b118feSFlorian Westphal 		return;
1768a6b118feSFlorian Westphal 
1769a6b118feSFlorian Westphal 	msk->rcvq_space.copied += copied;
1770a6b118feSFlorian Westphal 
1771a6b118feSFlorian Westphal 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1772a6b118feSFlorian Westphal 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1773a6b118feSFlorian Westphal 
1774a6b118feSFlorian Westphal 	rtt_us = msk->rcvq_space.rtt_us;
1775a6b118feSFlorian Westphal 	if (rtt_us && time < (rtt_us >> 3))
1776a6b118feSFlorian Westphal 		return;
1777a6b118feSFlorian Westphal 
1778a6b118feSFlorian Westphal 	rtt_us = 0;
1779a6b118feSFlorian Westphal 	mptcp_for_each_subflow(msk, subflow) {
1780a6b118feSFlorian Westphal 		const struct tcp_sock *tp;
1781a6b118feSFlorian Westphal 		u64 sf_rtt_us;
1782a6b118feSFlorian Westphal 		u32 sf_advmss;
1783a6b118feSFlorian Westphal 
1784a6b118feSFlorian Westphal 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1785a6b118feSFlorian Westphal 
1786a6b118feSFlorian Westphal 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1787a6b118feSFlorian Westphal 		sf_advmss = READ_ONCE(tp->advmss);
1788a6b118feSFlorian Westphal 
1789a6b118feSFlorian Westphal 		rtt_us = max(sf_rtt_us, rtt_us);
1790a6b118feSFlorian Westphal 		advmss = max(sf_advmss, advmss);
1791a6b118feSFlorian Westphal 	}
1792a6b118feSFlorian Westphal 
1793a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = rtt_us;
1794a6b118feSFlorian Westphal 	if (time < (rtt_us >> 3) || rtt_us == 0)
1795a6b118feSFlorian Westphal 		return;
1796a6b118feSFlorian Westphal 
1797a6b118feSFlorian Westphal 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1798a6b118feSFlorian Westphal 		goto new_measure;
1799a6b118feSFlorian Westphal 
1800a6b118feSFlorian Westphal 	if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1801a6b118feSFlorian Westphal 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1802a6b118feSFlorian Westphal 		int rcvmem, rcvbuf;
1803a6b118feSFlorian Westphal 		u64 rcvwin, grow;
1804a6b118feSFlorian Westphal 
1805a6b118feSFlorian Westphal 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1806a6b118feSFlorian Westphal 
1807a6b118feSFlorian Westphal 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1808a6b118feSFlorian Westphal 
1809a6b118feSFlorian Westphal 		do_div(grow, msk->rcvq_space.space);
1810a6b118feSFlorian Westphal 		rcvwin += (grow << 1);
1811a6b118feSFlorian Westphal 
1812a6b118feSFlorian Westphal 		rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1813a6b118feSFlorian Westphal 		while (tcp_win_from_space(sk, rcvmem) < advmss)
1814a6b118feSFlorian Westphal 			rcvmem += 128;
1815a6b118feSFlorian Westphal 
1816a6b118feSFlorian Westphal 		do_div(rcvwin, advmss);
1817a6b118feSFlorian Westphal 		rcvbuf = min_t(u64, rcvwin * rcvmem,
1818a6b118feSFlorian Westphal 			       sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1819a6b118feSFlorian Westphal 
1820a6b118feSFlorian Westphal 		if (rcvbuf > sk->sk_rcvbuf) {
1821a6b118feSFlorian Westphal 			u32 window_clamp;
1822a6b118feSFlorian Westphal 
1823a6b118feSFlorian Westphal 			window_clamp = tcp_win_from_space(sk, rcvbuf);
1824a6b118feSFlorian Westphal 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1825a6b118feSFlorian Westphal 
1826a6b118feSFlorian Westphal 			/* Make subflows follow along.  If we do not do this, we
1827a6b118feSFlorian Westphal 			 * get drops at subflow level if skbs can't be moved to
1828a6b118feSFlorian Westphal 			 * the mptcp rx queue fast enough (announced rcv_win can
1829a6b118feSFlorian Westphal 			 * exceed ssk->sk_rcvbuf).
1830a6b118feSFlorian Westphal 			 */
1831a6b118feSFlorian Westphal 			mptcp_for_each_subflow(msk, subflow) {
1832a6b118feSFlorian Westphal 				struct sock *ssk;
1833c76c6956SPaolo Abeni 				bool slow;
1834a6b118feSFlorian Westphal 
1835a6b118feSFlorian Westphal 				ssk = mptcp_subflow_tcp_sock(subflow);
1836c76c6956SPaolo Abeni 				slow = lock_sock_fast(ssk);
1837a6b118feSFlorian Westphal 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1838a6b118feSFlorian Westphal 				tcp_sk(ssk)->window_clamp = window_clamp;
1839c76c6956SPaolo Abeni 				tcp_cleanup_rbuf(ssk, 1);
1840c76c6956SPaolo Abeni 				unlock_sock_fast(ssk, slow);
1841a6b118feSFlorian Westphal 			}
1842a6b118feSFlorian Westphal 		}
1843a6b118feSFlorian Westphal 	}
1844a6b118feSFlorian Westphal 
1845a6b118feSFlorian Westphal 	msk->rcvq_space.space = msk->rcvq_space.copied;
1846a6b118feSFlorian Westphal new_measure:
1847a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
1848a6b118feSFlorian Westphal 	msk->rcvq_space.time = mstamp;
1849a6b118feSFlorian Westphal }
1850a6b118feSFlorian Westphal 
185187952603SPaolo Abeni static void __mptcp_update_rmem(struct sock *sk)
185287952603SPaolo Abeni {
185387952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
185487952603SPaolo Abeni 
185587952603SPaolo Abeni 	if (!msk->rmem_released)
185687952603SPaolo Abeni 		return;
185787952603SPaolo Abeni 
185887952603SPaolo Abeni 	atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
185987952603SPaolo Abeni 	sk_mem_uncharge(sk, msk->rmem_released);
186087952603SPaolo Abeni 	msk->rmem_released = 0;
186187952603SPaolo Abeni }
186287952603SPaolo Abeni 
186387952603SPaolo Abeni static void __mptcp_splice_receive_queue(struct sock *sk)
186487952603SPaolo Abeni {
186587952603SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
186687952603SPaolo Abeni 
186787952603SPaolo Abeni 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
186887952603SPaolo Abeni }
186987952603SPaolo Abeni 
1870ea4ca586SPaolo Abeni static bool __mptcp_move_skbs(struct mptcp_sock *msk, unsigned int rcv)
18716771bfd9SFlorian Westphal {
187287952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
18736771bfd9SFlorian Westphal 	unsigned int moved = 0;
187487952603SPaolo Abeni 	bool ret, done;
1875d5f49190SPaolo Abeni 
1876d5f49190SPaolo Abeni 	__mptcp_flush_join_list(msk);
18776771bfd9SFlorian Westphal 	do {
18786771bfd9SFlorian Westphal 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
187965f49fe7SFlorian Westphal 		bool slowpath;
18806771bfd9SFlorian Westphal 
188187952603SPaolo Abeni 		/* we can have data pending in the subflows only if the msk
188287952603SPaolo Abeni 		 * receive buffer was full at subflow_data_ready() time,
188387952603SPaolo Abeni 		 * that is an unlikely slow path.
188487952603SPaolo Abeni 		 */
188587952603SPaolo Abeni 		if (likely(!ssk))
18866771bfd9SFlorian Westphal 			break;
18876771bfd9SFlorian Westphal 
188865f49fe7SFlorian Westphal 		slowpath = lock_sock_fast(ssk);
188987952603SPaolo Abeni 		mptcp_data_lock(sk);
18906771bfd9SFlorian Westphal 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
189187952603SPaolo Abeni 		mptcp_data_unlock(sk);
1892ea4ca586SPaolo Abeni 		if (moved && rcv) {
1893ea4ca586SPaolo Abeni 			WRITE_ONCE(msk->rmem_pending, min(rcv, moved));
1894ea4ca586SPaolo Abeni 			tcp_cleanup_rbuf(ssk, 1);
1895ea4ca586SPaolo Abeni 			WRITE_ONCE(msk->rmem_pending, 0);
1896ea4ca586SPaolo Abeni 		}
189765f49fe7SFlorian Westphal 		unlock_sock_fast(ssk, slowpath);
18986771bfd9SFlorian Westphal 	} while (!done);
18996771bfd9SFlorian Westphal 
190087952603SPaolo Abeni 	/* acquire the data lock only if some input data is pending */
190187952603SPaolo Abeni 	ret = moved > 0;
190287952603SPaolo Abeni 	if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
190387952603SPaolo Abeni 	    !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
190487952603SPaolo Abeni 		mptcp_data_lock(sk);
190587952603SPaolo Abeni 		__mptcp_update_rmem(sk);
190687952603SPaolo Abeni 		ret |= __mptcp_ofo_queue(msk);
190787952603SPaolo Abeni 		__mptcp_splice_receive_queue(sk);
190887952603SPaolo Abeni 		mptcp_data_unlock(sk);
1909ab174ad8SPaolo Abeni 	}
191087952603SPaolo Abeni 	if (ret)
191187952603SPaolo Abeni 		mptcp_check_data_fin((struct sock *)msk);
191287952603SPaolo Abeni 	return !skb_queue_empty(&msk->receive_queue);
19136771bfd9SFlorian Westphal }
19146771bfd9SFlorian Westphal 
1915f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1916f870fa0bSMat Martineau 			 int nonblock, int flags, int *addr_len)
1917f870fa0bSMat Martineau {
1918f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
1919cec37a6eSPeter Krystad 	int copied = 0;
19207a6a6cbcSPaolo Abeni 	int target;
19217a6a6cbcSPaolo Abeni 	long timeo;
1922f870fa0bSMat Martineau 
1923f870fa0bSMat Martineau 	if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
1924f870fa0bSMat Martineau 		return -EOPNOTSUPP;
1925f870fa0bSMat Martineau 
192687952603SPaolo Abeni 	mptcp_lock_sock(sk, __mptcp_splice_receive_queue(sk));
1927fd897679SPaolo Abeni 	if (unlikely(sk->sk_state == TCP_LISTEN)) {
1928fd897679SPaolo Abeni 		copied = -ENOTCONN;
1929fd897679SPaolo Abeni 		goto out_err;
1930fd897679SPaolo Abeni 	}
1931fd897679SPaolo Abeni 
19327a6a6cbcSPaolo Abeni 	timeo = sock_rcvtimeo(sk, nonblock);
19337a6a6cbcSPaolo Abeni 
19347a6a6cbcSPaolo Abeni 	len = min_t(size_t, len, INT_MAX);
19357a6a6cbcSPaolo Abeni 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
19367a6a6cbcSPaolo Abeni 
193705e3eceaSEric Dumazet 	while (copied < len) {
1938ea4ca586SPaolo Abeni 		int bytes_read, old_space;
19397a6a6cbcSPaolo Abeni 
19406771bfd9SFlorian Westphal 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
19416771bfd9SFlorian Westphal 		if (unlikely(bytes_read < 0)) {
19427a6a6cbcSPaolo Abeni 			if (!copied)
19437a6a6cbcSPaolo Abeni 				copied = bytes_read;
19446771bfd9SFlorian Westphal 			goto out_err;
1945cec37a6eSPeter Krystad 		}
1946cec37a6eSPeter Krystad 
19477a6a6cbcSPaolo Abeni 		copied += bytes_read;
19487a6a6cbcSPaolo Abeni 
194987952603SPaolo Abeni 		if (skb_queue_empty(&msk->receive_queue) &&
1950ea4ca586SPaolo Abeni 		    __mptcp_move_skbs(msk, len - copied))
19516771bfd9SFlorian Westphal 			continue;
19527a6a6cbcSPaolo Abeni 
1953ea4ca586SPaolo Abeni 		/* be sure to advertise window change */
1954ea4ca586SPaolo Abeni 		old_space = READ_ONCE(msk->old_wspace);
1955ea4ca586SPaolo Abeni 		if ((tcp_space(sk) - old_space) >= old_space)
1956fd897679SPaolo Abeni 			mptcp_cleanup_rbuf(msk);
1957ea4ca586SPaolo Abeni 
19587a6a6cbcSPaolo Abeni 		/* only the master socket status is relevant here. The exit
19597a6a6cbcSPaolo Abeni 		 * conditions mirror closely tcp_recvmsg()
19607a6a6cbcSPaolo Abeni 		 */
19617a6a6cbcSPaolo Abeni 		if (copied >= target)
19627a6a6cbcSPaolo Abeni 			break;
19637a6a6cbcSPaolo Abeni 
19647a6a6cbcSPaolo Abeni 		if (copied) {
19657a6a6cbcSPaolo Abeni 			if (sk->sk_err ||
19667a6a6cbcSPaolo Abeni 			    sk->sk_state == TCP_CLOSE ||
19677a6a6cbcSPaolo Abeni 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
19687a6a6cbcSPaolo Abeni 			    !timeo ||
19697a6a6cbcSPaolo Abeni 			    signal_pending(current))
19707a6a6cbcSPaolo Abeni 				break;
19717a6a6cbcSPaolo Abeni 		} else {
19727a6a6cbcSPaolo Abeni 			if (sk->sk_err) {
19737a6a6cbcSPaolo Abeni 				copied = sock_error(sk);
19747a6a6cbcSPaolo Abeni 				break;
19757a6a6cbcSPaolo Abeni 			}
19767a6a6cbcSPaolo Abeni 
19775969856aSPaolo Abeni 			if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
19785969856aSPaolo Abeni 				mptcp_check_for_eof(msk);
19795969856aSPaolo Abeni 
198087952603SPaolo Abeni 			if (sk->sk_shutdown & RCV_SHUTDOWN) {
198187952603SPaolo Abeni 				/* race breaker: the shutdown could be after the
198287952603SPaolo Abeni 				 * previous receive queue check
198387952603SPaolo Abeni 				 */
198487952603SPaolo Abeni 				if (__mptcp_move_skbs(msk, len - copied))
198587952603SPaolo Abeni 					continue;
19867a6a6cbcSPaolo Abeni 				break;
198787952603SPaolo Abeni 			}
19887a6a6cbcSPaolo Abeni 
19897a6a6cbcSPaolo Abeni 			if (sk->sk_state == TCP_CLOSE) {
19907a6a6cbcSPaolo Abeni 				copied = -ENOTCONN;
19917a6a6cbcSPaolo Abeni 				break;
19927a6a6cbcSPaolo Abeni 			}
19937a6a6cbcSPaolo Abeni 
19947a6a6cbcSPaolo Abeni 			if (!timeo) {
19957a6a6cbcSPaolo Abeni 				copied = -EAGAIN;
19967a6a6cbcSPaolo Abeni 				break;
19977a6a6cbcSPaolo Abeni 			}
19987a6a6cbcSPaolo Abeni 
19997a6a6cbcSPaolo Abeni 			if (signal_pending(current)) {
20007a6a6cbcSPaolo Abeni 				copied = sock_intr_errno(timeo);
20017a6a6cbcSPaolo Abeni 				break;
20027a6a6cbcSPaolo Abeni 			}
20037a6a6cbcSPaolo Abeni 		}
20047a6a6cbcSPaolo Abeni 
20057a6a6cbcSPaolo Abeni 		pr_debug("block timeout %ld", timeo);
20067a6a6cbcSPaolo Abeni 		mptcp_wait_data(sk, &timeo);
20077a6a6cbcSPaolo Abeni 	}
20087a6a6cbcSPaolo Abeni 
200987952603SPaolo Abeni 	if (skb_queue_empty_lockless(&sk->sk_receive_queue) &&
201087952603SPaolo Abeni 	    skb_queue_empty(&msk->receive_queue)) {
20116771bfd9SFlorian Westphal 		/* entire backlog drained, clear DATA_READY. */
20127a6a6cbcSPaolo Abeni 		clear_bit(MPTCP_DATA_READY, &msk->flags);
20137a6a6cbcSPaolo Abeni 
20146771bfd9SFlorian Westphal 		/* .. race-breaker: ssk might have gotten new data
20156771bfd9SFlorian Westphal 		 * after last __mptcp_move_skbs() returned false.
20167a6a6cbcSPaolo Abeni 		 */
2017ea4ca586SPaolo Abeni 		if (unlikely(__mptcp_move_skbs(msk, 0)))
20186771bfd9SFlorian Westphal 			set_bit(MPTCP_DATA_READY, &msk->flags);
20196771bfd9SFlorian Westphal 	} else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
20206771bfd9SFlorian Westphal 		/* data to read but mptcp_wait_data() cleared DATA_READY */
20217a6a6cbcSPaolo Abeni 		set_bit(MPTCP_DATA_READY, &msk->flags);
20227a6a6cbcSPaolo Abeni 	}
20236771bfd9SFlorian Westphal out_err:
20246719331cSPaolo Abeni 	pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d",
20256719331cSPaolo Abeni 		 msk, test_bit(MPTCP_DATA_READY, &msk->flags),
202687952603SPaolo Abeni 		 skb_queue_empty_lockless(&sk->sk_receive_queue), copied);
2027a6b118feSFlorian Westphal 	mptcp_rcv_space_adjust(msk, copied);
2028a6b118feSFlorian Westphal 
2029cec37a6eSPeter Krystad 	release_sock(sk);
2030cec37a6eSPeter Krystad 	return copied;
2031cec37a6eSPeter Krystad }
2032cec37a6eSPeter Krystad 
2033b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk)
2034b51f9b80SPaolo Abeni {
2035b51f9b80SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2036b51f9b80SPaolo Abeni 
20373b1d6210SPaolo Abeni 	set_bit(MPTCP_WORK_RTX, &msk->flags);
2038ba8f48f7SPaolo Abeni 	mptcp_schedule_work(sk);
20393b1d6210SPaolo Abeni }
2040b51f9b80SPaolo Abeni 
2041b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t)
2042b51f9b80SPaolo Abeni {
2043b51f9b80SPaolo Abeni 	struct inet_connection_sock *icsk = from_timer(icsk, t,
2044b51f9b80SPaolo Abeni 						       icsk_retransmit_timer);
2045b51f9b80SPaolo Abeni 	struct sock *sk = &icsk->icsk_inet.sk;
2046b51f9b80SPaolo Abeni 
2047b51f9b80SPaolo Abeni 	bh_lock_sock(sk);
2048b51f9b80SPaolo Abeni 	if (!sock_owned_by_user(sk)) {
2049b51f9b80SPaolo Abeni 		mptcp_retransmit_handler(sk);
2050b51f9b80SPaolo Abeni 	} else {
2051b51f9b80SPaolo Abeni 		/* delegate our work to tcp_release_cb() */
2052b51f9b80SPaolo Abeni 		if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
2053b51f9b80SPaolo Abeni 				      &sk->sk_tsq_flags))
2054b51f9b80SPaolo Abeni 			sock_hold(sk);
2055b51f9b80SPaolo Abeni 	}
2056b51f9b80SPaolo Abeni 	bh_unlock_sock(sk);
2057b51f9b80SPaolo Abeni 	sock_put(sk);
2058b51f9b80SPaolo Abeni }
2059b51f9b80SPaolo Abeni 
2060e16163b6SPaolo Abeni static void mptcp_timeout_timer(struct timer_list *t)
2061e16163b6SPaolo Abeni {
2062e16163b6SPaolo Abeni 	struct sock *sk = from_timer(sk, t, sk_timer);
2063e16163b6SPaolo Abeni 
2064e16163b6SPaolo Abeni 	mptcp_schedule_work(sk);
2065b6d69fc8SFlorian Westphal 	sock_put(sk);
2066e16163b6SPaolo Abeni }
2067e16163b6SPaolo Abeni 
20683b1d6210SPaolo Abeni /* Find an idle subflow.  Return NULL if there is unacked data at tcp
20693b1d6210SPaolo Abeni  * level.
20703b1d6210SPaolo Abeni  *
20713b1d6210SPaolo Abeni  * A backup subflow is returned only if that is the only kind available.
20723b1d6210SPaolo Abeni  */
20733b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
20743b1d6210SPaolo Abeni {
20753b1d6210SPaolo Abeni 	struct mptcp_subflow_context *subflow;
20763b1d6210SPaolo Abeni 	struct sock *backup = NULL;
20773b1d6210SPaolo Abeni 
20783b1d6210SPaolo Abeni 	sock_owned_by_me((const struct sock *)msk);
20793b1d6210SPaolo Abeni 
2080d5f49190SPaolo Abeni 	if (__mptcp_check_fallback(msk))
2081d9ca1de8SPaolo Abeni 		return NULL;
2082d5f49190SPaolo Abeni 
20833b1d6210SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
20843b1d6210SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
20853b1d6210SPaolo Abeni 
2086d5f49190SPaolo Abeni 		if (!mptcp_subflow_active(subflow))
2087d5f49190SPaolo Abeni 			continue;
2088d5f49190SPaolo Abeni 
20893b1d6210SPaolo Abeni 		/* still data outstanding at TCP level?  Don't retransmit. */
2090860975c6SFlorian Westphal 		if (!tcp_write_queue_empty(ssk)) {
2091860975c6SFlorian Westphal 			if (inet_csk(ssk)->icsk_ca_state >= TCP_CA_Loss)
2092860975c6SFlorian Westphal 				continue;
20933b1d6210SPaolo Abeni 			return NULL;
2094860975c6SFlorian Westphal 		}
20953b1d6210SPaolo Abeni 
20963b1d6210SPaolo Abeni 		if (subflow->backup) {
20973b1d6210SPaolo Abeni 			if (!backup)
20983b1d6210SPaolo Abeni 				backup = ssk;
20993b1d6210SPaolo Abeni 			continue;
21003b1d6210SPaolo Abeni 		}
21013b1d6210SPaolo Abeni 
21023b1d6210SPaolo Abeni 		return ssk;
21033b1d6210SPaolo Abeni 	}
21043b1d6210SPaolo Abeni 
21053b1d6210SPaolo Abeni 	return backup;
21063b1d6210SPaolo Abeni }
21073b1d6210SPaolo Abeni 
2108cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming
2109cec37a6eSPeter Krystad  * (accept).
2110cec37a6eSPeter Krystad  *
2111cec37a6eSPeter Krystad  * Outgoing subflows use in-kernel sockets.
2112cec37a6eSPeter Krystad  * Incoming subflows do not have their own 'struct socket' allocated,
2113cec37a6eSPeter Krystad  * so we need to use tcp_close() after detaching them from the mptcp
2114cec37a6eSPeter Krystad  * parent socket.
2115cec37a6eSPeter Krystad  */
2116d0876b22SGeliang Tang void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2117e16163b6SPaolo Abeni 		       struct mptcp_subflow_context *subflow)
2118cec37a6eSPeter Krystad {
2119e16163b6SPaolo Abeni 	bool dispose_socket = false;
2120e16163b6SPaolo Abeni 	struct socket *sock;
2121cec37a6eSPeter Krystad 
2122cec37a6eSPeter Krystad 	list_del(&subflow->node);
2123cec37a6eSPeter Krystad 
21243f8b2667SPaolo Abeni 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2125e16163b6SPaolo Abeni 
2126e16163b6SPaolo Abeni 	/* if we are invoked by the msk cleanup code, the subflow is
2127e16163b6SPaolo Abeni 	 * already orphaned
2128e16163b6SPaolo Abeni 	 */
2129e16163b6SPaolo Abeni 	sock = ssk->sk_socket;
2130e16163b6SPaolo Abeni 	if (sock) {
2131e16163b6SPaolo Abeni 		dispose_socket = sock != sk->sk_socket;
2132e16163b6SPaolo Abeni 		sock_orphan(ssk);
2133cec37a6eSPeter Krystad 	}
2134e16163b6SPaolo Abeni 
2135d7b1bfd0SPaolo Abeni 	subflow->disposable = 1;
2136d7b1bfd0SPaolo Abeni 
2137e16163b6SPaolo Abeni 	/* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2138e16163b6SPaolo Abeni 	 * the ssk has been already destroyed, we just need to release the
2139e16163b6SPaolo Abeni 	 * reference owned by msk;
2140e16163b6SPaolo Abeni 	 */
2141e16163b6SPaolo Abeni 	if (!inet_csk(ssk)->icsk_ulp_ops) {
2142e16163b6SPaolo Abeni 		kfree_rcu(subflow, rcu);
2143e16163b6SPaolo Abeni 	} else {
2144d7b1bfd0SPaolo Abeni 		/* otherwise tcp will dispose of the ssk and subflow ctx */
2145e16163b6SPaolo Abeni 		__tcp_close(ssk, 0);
2146e16163b6SPaolo Abeni 
2147e16163b6SPaolo Abeni 		/* close acquired an extra ref */
2148e16163b6SPaolo Abeni 		__sock_put(ssk);
2149e16163b6SPaolo Abeni 	}
2150e16163b6SPaolo Abeni 	release_sock(ssk);
2151e16163b6SPaolo Abeni 	if (dispose_socket)
2152e16163b6SPaolo Abeni 		iput(SOCK_INODE(sock));
2153e16163b6SPaolo Abeni 
2154e16163b6SPaolo Abeni 	sock_put(ssk);
2155f870fa0bSMat Martineau }
2156f870fa0bSMat Martineau 
2157dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2158dc24f8b4SPaolo Abeni {
2159dc24f8b4SPaolo Abeni 	return 0;
2160dc24f8b4SPaolo Abeni }
2161dc24f8b4SPaolo Abeni 
2162b416268bSFlorian Westphal static void pm_work(struct mptcp_sock *msk)
2163b416268bSFlorian Westphal {
2164b416268bSFlorian Westphal 	struct mptcp_pm_data *pm = &msk->pm;
2165b416268bSFlorian Westphal 
2166b416268bSFlorian Westphal 	spin_lock_bh(&msk->pm.lock);
2167b416268bSFlorian Westphal 
2168b416268bSFlorian Westphal 	pr_debug("msk=%p status=%x", msk, pm->status);
2169b416268bSFlorian Westphal 	if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
2170b416268bSFlorian Westphal 		pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
2171b416268bSFlorian Westphal 		mptcp_pm_nl_add_addr_received(msk);
2172b416268bSFlorian Westphal 	}
217384dfe367SGeliang Tang 	if (pm->status & BIT(MPTCP_PM_ADD_ADDR_SEND_ACK)) {
217484dfe367SGeliang Tang 		pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_SEND_ACK);
217584dfe367SGeliang Tang 		mptcp_pm_nl_add_addr_send_ack(msk);
217684dfe367SGeliang Tang 	}
2177d0876b22SGeliang Tang 	if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
2178d0876b22SGeliang Tang 		pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
2179d0876b22SGeliang Tang 		mptcp_pm_nl_rm_addr_received(msk);
2180d0876b22SGeliang Tang 	}
2181b416268bSFlorian Westphal 	if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
2182b416268bSFlorian Westphal 		pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
2183b416268bSFlorian Westphal 		mptcp_pm_nl_fully_established(msk);
2184b416268bSFlorian Westphal 	}
2185b416268bSFlorian Westphal 	if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
2186b416268bSFlorian Westphal 		pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
2187b416268bSFlorian Westphal 		mptcp_pm_nl_subflow_established(msk);
2188b416268bSFlorian Westphal 	}
2189b416268bSFlorian Westphal 
2190b416268bSFlorian Westphal 	spin_unlock_bh(&msk->pm.lock);
2191b416268bSFlorian Westphal }
2192b416268bSFlorian Westphal 
21930e4f35d7SPaolo Abeni static void __mptcp_close_subflow(struct mptcp_sock *msk)
21940e4f35d7SPaolo Abeni {
21950e4f35d7SPaolo Abeni 	struct mptcp_subflow_context *subflow, *tmp;
21960e4f35d7SPaolo Abeni 
21970e4f35d7SPaolo Abeni 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
21980e4f35d7SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
21990e4f35d7SPaolo Abeni 
22000e4f35d7SPaolo Abeni 		if (inet_sk_state_load(ssk) != TCP_CLOSE)
22010e4f35d7SPaolo Abeni 			continue;
22020e4f35d7SPaolo Abeni 
2203e16163b6SPaolo Abeni 		__mptcp_close_ssk((struct sock *)msk, ssk, subflow);
22040e4f35d7SPaolo Abeni 	}
22050e4f35d7SPaolo Abeni }
22060e4f35d7SPaolo Abeni 
2207e16163b6SPaolo Abeni static bool mptcp_check_close_timeout(const struct sock *sk)
2208e16163b6SPaolo Abeni {
2209e16163b6SPaolo Abeni 	s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2210e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2211e16163b6SPaolo Abeni 
2212e16163b6SPaolo Abeni 	if (delta >= TCP_TIMEWAIT_LEN)
2213e16163b6SPaolo Abeni 		return true;
2214e16163b6SPaolo Abeni 
2215e16163b6SPaolo Abeni 	/* if all subflows are in closed status don't bother with additional
2216e16163b6SPaolo Abeni 	 * timeout
2217e16163b6SPaolo Abeni 	 */
2218e16163b6SPaolo Abeni 	mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2219e16163b6SPaolo Abeni 		if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2220e16163b6SPaolo Abeni 		    TCP_CLOSE)
2221e16163b6SPaolo Abeni 			return false;
2222e16163b6SPaolo Abeni 	}
2223e16163b6SPaolo Abeni 	return true;
2224e16163b6SPaolo Abeni }
2225e16163b6SPaolo Abeni 
222650c504a2SFlorian Westphal static void mptcp_check_fastclose(struct mptcp_sock *msk)
222750c504a2SFlorian Westphal {
222850c504a2SFlorian Westphal 	struct mptcp_subflow_context *subflow, *tmp;
222950c504a2SFlorian Westphal 	struct sock *sk = &msk->sk.icsk_inet.sk;
223050c504a2SFlorian Westphal 
223150c504a2SFlorian Westphal 	if (likely(!READ_ONCE(msk->rcv_fastclose)))
223250c504a2SFlorian Westphal 		return;
223350c504a2SFlorian Westphal 
223450c504a2SFlorian Westphal 	mptcp_token_destroy(msk);
223550c504a2SFlorian Westphal 
223650c504a2SFlorian Westphal 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
223750c504a2SFlorian Westphal 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
223850c504a2SFlorian Westphal 
223950c504a2SFlorian Westphal 		lock_sock(tcp_sk);
224050c504a2SFlorian Westphal 		if (tcp_sk->sk_state != TCP_CLOSE) {
224150c504a2SFlorian Westphal 			tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
224250c504a2SFlorian Westphal 			tcp_set_state(tcp_sk, TCP_CLOSE);
224350c504a2SFlorian Westphal 		}
224450c504a2SFlorian Westphal 		release_sock(tcp_sk);
224550c504a2SFlorian Westphal 	}
224650c504a2SFlorian Westphal 
224750c504a2SFlorian Westphal 	inet_sk_state_store(sk, TCP_CLOSE);
224850c504a2SFlorian Westphal 	sk->sk_shutdown = SHUTDOWN_MASK;
224950c504a2SFlorian Westphal 	smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
225050c504a2SFlorian Westphal 	set_bit(MPTCP_DATA_READY, &msk->flags);
225150c504a2SFlorian Westphal 	set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
225250c504a2SFlorian Westphal 
225350c504a2SFlorian Westphal 	mptcp_close_wake_up(sk);
225450c504a2SFlorian Westphal }
225550c504a2SFlorian Westphal 
225680992017SPaolo Abeni static void mptcp_worker(struct work_struct *work)
225780992017SPaolo Abeni {
225880992017SPaolo Abeni 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
22593b1d6210SPaolo Abeni 	struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
2260caf971dfSPaolo Abeni 	struct mptcp_sendmsg_info info = {};
22613b1d6210SPaolo Abeni 	struct mptcp_data_frag *dfrag;
22623b1d6210SPaolo Abeni 	size_t copied = 0;
2263e16163b6SPaolo Abeni 	int state, ret;
226480992017SPaolo Abeni 
226580992017SPaolo Abeni 	lock_sock(sk);
2266e16163b6SPaolo Abeni 	state = sk->sk_state;
2267e16163b6SPaolo Abeni 	if (unlikely(state == TCP_CLOSE))
2268e16163b6SPaolo Abeni 		goto unlock;
2269e16163b6SPaolo Abeni 
227043b54c6eSMat Martineau 	mptcp_check_data_fin_ack(sk);
2271ec3edaa7SPeter Krystad 	__mptcp_flush_join_list(msk);
227250c504a2SFlorian Westphal 
227350c504a2SFlorian Westphal 	mptcp_check_fastclose(msk);
227450c504a2SFlorian Westphal 
22750e4f35d7SPaolo Abeni 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
22760e4f35d7SPaolo Abeni 		__mptcp_close_subflow(msk);
22770e4f35d7SPaolo Abeni 
2278b416268bSFlorian Westphal 	if (msk->pm.status)
2279b416268bSFlorian Westphal 		pm_work(msk);
2280b416268bSFlorian Westphal 
228159832e24SFlorian Westphal 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
228259832e24SFlorian Westphal 		mptcp_check_for_eof(msk);
228359832e24SFlorian Westphal 
22846e628cd3SPaolo Abeni 	__mptcp_check_send_data_fin(sk);
228543b54c6eSMat Martineau 	mptcp_check_data_fin(sk);
228643b54c6eSMat Martineau 
2287e16163b6SPaolo Abeni 	/* if the msk data is completely acked, or the socket timedout,
2288e16163b6SPaolo Abeni 	 * there is no point in keeping around an orphaned sk
2289e16163b6SPaolo Abeni 	 */
2290e16163b6SPaolo Abeni 	if (sock_flag(sk, SOCK_DEAD) &&
2291e16163b6SPaolo Abeni 	    (mptcp_check_close_timeout(sk) ||
2292e16163b6SPaolo Abeni 	    (state != sk->sk_state &&
2293e16163b6SPaolo Abeni 	    ((1 << inet_sk_state_load(sk)) & (TCPF_CLOSE | TCPF_FIN_WAIT2))))) {
2294e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
2295e16163b6SPaolo Abeni 		__mptcp_destroy_sock(sk);
2296e16163b6SPaolo Abeni 		goto unlock;
2297e16163b6SPaolo Abeni 	}
2298e16163b6SPaolo Abeni 
22993b1d6210SPaolo Abeni 	if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
23003b1d6210SPaolo Abeni 		goto unlock;
23013b1d6210SPaolo Abeni 
23023b1d6210SPaolo Abeni 	dfrag = mptcp_rtx_head(sk);
23033b1d6210SPaolo Abeni 	if (!dfrag)
23043b1d6210SPaolo Abeni 		goto unlock;
23053b1d6210SPaolo Abeni 
23063b1d6210SPaolo Abeni 	ssk = mptcp_subflow_get_retrans(msk);
23073b1d6210SPaolo Abeni 	if (!ssk)
23083b1d6210SPaolo Abeni 		goto reset_unlock;
23093b1d6210SPaolo Abeni 
23103b1d6210SPaolo Abeni 	lock_sock(ssk);
23113b1d6210SPaolo Abeni 
2312d9ca1de8SPaolo Abeni 	/* limit retransmission to the bytes already sent on some subflows */
2313d9ca1de8SPaolo Abeni 	info.sent = 0;
2314d9ca1de8SPaolo Abeni 	info.limit = dfrag->already_sent;
2315d9ca1de8SPaolo Abeni 	while (info.sent < dfrag->already_sent) {
2316724cfd2eSPaolo Abeni 		if (!mptcp_alloc_tx_skb(sk, ssk))
2317724cfd2eSPaolo Abeni 			break;
2318724cfd2eSPaolo Abeni 
2319d9ca1de8SPaolo Abeni 		ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
23206f8a612aSFlorian Westphal 		if (ret <= 0)
23213b1d6210SPaolo Abeni 			break;
23223b1d6210SPaolo Abeni 
2323fc518953SFlorian Westphal 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
23243b1d6210SPaolo Abeni 		copied += ret;
2325d9ca1de8SPaolo Abeni 		info.sent += ret;
23263b1d6210SPaolo Abeni 	}
23273b1d6210SPaolo Abeni 	if (copied)
2328caf971dfSPaolo Abeni 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2329caf971dfSPaolo Abeni 			 info.size_goal);
23303b1d6210SPaolo Abeni 
23313b1d6210SPaolo Abeni 	mptcp_set_timeout(sk, ssk);
23323b1d6210SPaolo Abeni 	release_sock(ssk);
23333b1d6210SPaolo Abeni 
23343b1d6210SPaolo Abeni reset_unlock:
23353b1d6210SPaolo Abeni 	if (!mptcp_timer_pending(sk))
23363b1d6210SPaolo Abeni 		mptcp_reset_timer(sk);
23373b1d6210SPaolo Abeni 
23383b1d6210SPaolo Abeni unlock:
233980992017SPaolo Abeni 	release_sock(sk);
234080992017SPaolo Abeni 	sock_put(sk);
234180992017SPaolo Abeni }
234280992017SPaolo Abeni 
2343784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk)
2344f870fa0bSMat Martineau {
2345cec37a6eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
2346cec37a6eSPeter Krystad 
2347ec3edaa7SPeter Krystad 	spin_lock_init(&msk->join_list_lock);
2348ec3edaa7SPeter Krystad 
2349cec37a6eSPeter Krystad 	INIT_LIST_HEAD(&msk->conn_list);
2350ec3edaa7SPeter Krystad 	INIT_LIST_HEAD(&msk->join_list);
235118b683bfSPaolo Abeni 	INIT_LIST_HEAD(&msk->rtx_queue);
235280992017SPaolo Abeni 	INIT_WORK(&msk->work, mptcp_worker);
235387952603SPaolo Abeni 	__skb_queue_head_init(&msk->receive_queue);
2354724cfd2eSPaolo Abeni 	__skb_queue_head_init(&msk->skb_tx_cache);
2355ab174ad8SPaolo Abeni 	msk->out_of_order_queue = RB_ROOT;
2356f0e6a4cfSPaolo Abeni 	msk->first_pending = NULL;
2357e93da928SPaolo Abeni 	msk->wmem_reserved = 0;
235887952603SPaolo Abeni 	msk->rmem_released = 0;
2359724cfd2eSPaolo Abeni 	msk->tx_pending_data = 0;
2360724cfd2eSPaolo Abeni 	msk->size_goal_cache = TCP_BASE_MSS;
2361cec37a6eSPeter Krystad 
2362ea4ca586SPaolo Abeni 	msk->ack_hint = NULL;
23638ab183deSPaolo Abeni 	msk->first = NULL;
2364dc24f8b4SPaolo Abeni 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
23658ab183deSPaolo Abeni 
23661b1c7a0eSPeter Krystad 	mptcp_pm_data_init(msk);
23671b1c7a0eSPeter Krystad 
2368b51f9b80SPaolo Abeni 	/* re-use the csk retrans timer for MPTCP-level retrans */
2369b51f9b80SPaolo Abeni 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2370e16163b6SPaolo Abeni 	timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2371f870fa0bSMat Martineau 	return 0;
2372f870fa0bSMat Martineau }
2373f870fa0bSMat Martineau 
2374784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk)
2375784325e9SMatthieu Baerts {
2376fc518953SFlorian Westphal 	struct net *net = sock_net(sk);
2377fc518953SFlorian Westphal 	int ret;
237818b683bfSPaolo Abeni 
2379b6c08380SGeliang Tang 	ret = __mptcp_init_sock(sk);
2380b6c08380SGeliang Tang 	if (ret)
2381b6c08380SGeliang Tang 		return ret;
2382b6c08380SGeliang Tang 
2383fc518953SFlorian Westphal 	if (!mptcp_is_enabled(net))
2384fc518953SFlorian Westphal 		return -ENOPROTOOPT;
2385fc518953SFlorian Westphal 
2386fc518953SFlorian Westphal 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2387fc518953SFlorian Westphal 		return -ENOMEM;
2388fc518953SFlorian Westphal 
2389fa68018dSPaolo Abeni 	ret = __mptcp_socket_create(mptcp_sk(sk));
2390fa68018dSPaolo Abeni 	if (ret)
2391fa68018dSPaolo Abeni 		return ret;
2392fa68018dSPaolo Abeni 
2393d027236cSPaolo Abeni 	sk_sockets_allocated_inc(sk);
2394a6b118feSFlorian Westphal 	sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
2395da51aef5SPaolo Abeni 	sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
2396d027236cSPaolo Abeni 
239718b683bfSPaolo Abeni 	return 0;
239818b683bfSPaolo Abeni }
239918b683bfSPaolo Abeni 
240018b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk)
240118b683bfSPaolo Abeni {
240218b683bfSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
240318b683bfSPaolo Abeni 	struct mptcp_data_frag *dtmp, *dfrag;
2404724cfd2eSPaolo Abeni 	struct sk_buff *skb;
240518b683bfSPaolo Abeni 
2406d9ca1de8SPaolo Abeni 	WRITE_ONCE(msk->first_pending, NULL);
240718b683bfSPaolo Abeni 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2408d027236cSPaolo Abeni 		dfrag_clear(sk, dfrag);
2409724cfd2eSPaolo Abeni 	while ((skb = __skb_dequeue(&msk->skb_tx_cache)) != NULL) {
2410724cfd2eSPaolo Abeni 		sk->sk_forward_alloc += skb->truesize;
2411724cfd2eSPaolo Abeni 		kfree_skb(skb);
2412724cfd2eSPaolo Abeni 	}
2413784325e9SMatthieu Baerts }
2414784325e9SMatthieu Baerts 
241580992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk)
241680992017SPaolo Abeni {
241780992017SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
241880992017SPaolo Abeni 
2419b2771d24SPaolo Abeni 	if (cancel_work_sync(&msk->work))
2420e16163b6SPaolo Abeni 		__sock_put(sk);
242180992017SPaolo Abeni }
242280992017SPaolo Abeni 
2423d0876b22SGeliang Tang void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
242421498490SPeter Krystad {
242521498490SPeter Krystad 	lock_sock(ssk);
242621498490SPeter Krystad 
242721498490SPeter Krystad 	switch (ssk->sk_state) {
242821498490SPeter Krystad 	case TCP_LISTEN:
242921498490SPeter Krystad 		if (!(how & RCV_SHUTDOWN))
243021498490SPeter Krystad 			break;
2431df561f66SGustavo A. R. Silva 		fallthrough;
243221498490SPeter Krystad 	case TCP_SYN_SENT:
243321498490SPeter Krystad 		tcp_disconnect(ssk, O_NONBLOCK);
243421498490SPeter Krystad 		break;
243521498490SPeter Krystad 	default:
243643b54c6eSMat Martineau 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
243743b54c6eSMat Martineau 			pr_debug("Fallback");
243821498490SPeter Krystad 			ssk->sk_shutdown |= how;
243921498490SPeter Krystad 			tcp_shutdown(ssk, how);
244043b54c6eSMat Martineau 		} else {
244143b54c6eSMat Martineau 			pr_debug("Sending DATA_FIN on subflow %p", ssk);
244243b54c6eSMat Martineau 			mptcp_set_timeout(sk, ssk);
244343b54c6eSMat Martineau 			tcp_send_ack(ssk);
244443b54c6eSMat Martineau 		}
244521498490SPeter Krystad 		break;
244621498490SPeter Krystad 	}
244721498490SPeter Krystad 
244821498490SPeter Krystad 	release_sock(ssk);
244921498490SPeter Krystad }
245021498490SPeter Krystad 
24516920b851SMat Martineau static const unsigned char new_state[16] = {
24526920b851SMat Martineau 	/* current state:     new state:      action:	*/
24536920b851SMat Martineau 	[0 /* (Invalid) */] = TCP_CLOSE,
24546920b851SMat Martineau 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
24556920b851SMat Martineau 	[TCP_SYN_SENT]      = TCP_CLOSE,
24566920b851SMat Martineau 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
24576920b851SMat Martineau 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
24586920b851SMat Martineau 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
24596920b851SMat Martineau 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
24606920b851SMat Martineau 	[TCP_CLOSE]         = TCP_CLOSE,
24616920b851SMat Martineau 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
24626920b851SMat Martineau 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
24636920b851SMat Martineau 	[TCP_LISTEN]        = TCP_CLOSE,
24646920b851SMat Martineau 	[TCP_CLOSING]       = TCP_CLOSING,
24656920b851SMat Martineau 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
24666920b851SMat Martineau };
24676920b851SMat Martineau 
24686920b851SMat Martineau static int mptcp_close_state(struct sock *sk)
24696920b851SMat Martineau {
24706920b851SMat Martineau 	int next = (int)new_state[sk->sk_state];
24716920b851SMat Martineau 	int ns = next & TCP_STATE_MASK;
24726920b851SMat Martineau 
24736920b851SMat Martineau 	inet_sk_state_store(sk, ns);
24746920b851SMat Martineau 
24756920b851SMat Martineau 	return next & TCP_ACTION_FIN;
24766920b851SMat Martineau }
24776920b851SMat Martineau 
2478e16163b6SPaolo Abeni static void __mptcp_check_send_data_fin(struct sock *sk)
2479e16163b6SPaolo Abeni {
2480e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2481e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2482e16163b6SPaolo Abeni 
2483e16163b6SPaolo Abeni 	pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2484e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2485e16163b6SPaolo Abeni 		 msk->snd_nxt, msk->write_seq);
2486e16163b6SPaolo Abeni 
2487e16163b6SPaolo Abeni 	/* we still need to enqueue subflows or not really shutting down,
2488e16163b6SPaolo Abeni 	 * skip this
2489e16163b6SPaolo Abeni 	 */
2490e16163b6SPaolo Abeni 	if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2491e16163b6SPaolo Abeni 	    mptcp_send_head(sk))
2492e16163b6SPaolo Abeni 		return;
2493e16163b6SPaolo Abeni 
2494e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2495e16163b6SPaolo Abeni 
249626aa2314SPaolo Abeni 	/* fallback socket will not get data_fin/ack, can move to the next
249726aa2314SPaolo Abeni 	 * state now
249826aa2314SPaolo Abeni 	 */
249926aa2314SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
250026aa2314SPaolo Abeni 		if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2501e16163b6SPaolo Abeni 			inet_sk_state_store(sk, TCP_CLOSE);
2502e16163b6SPaolo Abeni 			mptcp_close_wake_up(sk);
250326aa2314SPaolo Abeni 		} else if (sk->sk_state == TCP_FIN_WAIT1) {
250426aa2314SPaolo Abeni 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
250526aa2314SPaolo Abeni 		}
2506e16163b6SPaolo Abeni 	}
2507e16163b6SPaolo Abeni 
2508e16163b6SPaolo Abeni 	__mptcp_flush_join_list(msk);
2509e16163b6SPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
2510e16163b6SPaolo Abeni 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2511e16163b6SPaolo Abeni 
2512e16163b6SPaolo Abeni 		mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2513e16163b6SPaolo Abeni 	}
2514e16163b6SPaolo Abeni }
2515e16163b6SPaolo Abeni 
2516e16163b6SPaolo Abeni static void __mptcp_wr_shutdown(struct sock *sk)
2517e16163b6SPaolo Abeni {
2518e16163b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
2519e16163b6SPaolo Abeni 
2520e16163b6SPaolo Abeni 	pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2521e16163b6SPaolo Abeni 		 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2522e16163b6SPaolo Abeni 		 !!mptcp_send_head(sk));
2523e16163b6SPaolo Abeni 
2524e16163b6SPaolo Abeni 	/* will be ignored by fallback sockets */
2525e16163b6SPaolo Abeni 	WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2526e16163b6SPaolo Abeni 	WRITE_ONCE(msk->snd_data_fin_enable, 1);
2527e16163b6SPaolo Abeni 
2528e16163b6SPaolo Abeni 	__mptcp_check_send_data_fin(sk);
2529e16163b6SPaolo Abeni }
2530e16163b6SPaolo Abeni 
2531e16163b6SPaolo Abeni static void __mptcp_destroy_sock(struct sock *sk)
2532f870fa0bSMat Martineau {
2533cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow, *tmp;
2534f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
2535b2c5b614SFlorian Westphal 	LIST_HEAD(conn_list);
2536f870fa0bSMat Martineau 
2537e16163b6SPaolo Abeni 	pr_debug("msk=%p", msk);
25382c22c06cSFlorian Westphal 
253910f6d46cSPaolo Abeni 	/* be sure to always acquire the join list lock, to sync vs
254010f6d46cSPaolo Abeni 	 * mptcp_finish_join().
254110f6d46cSPaolo Abeni 	 */
254210f6d46cSPaolo Abeni 	spin_lock_bh(&msk->join_list_lock);
254310f6d46cSPaolo Abeni 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
254410f6d46cSPaolo Abeni 	spin_unlock_bh(&msk->join_list_lock);
2545b2c5b614SFlorian Westphal 	list_splice_init(&msk->conn_list, &conn_list);
2546b2c5b614SFlorian Westphal 
25476e628cd3SPaolo Abeni 	sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
2548e16163b6SPaolo Abeni 	sk_stop_timer(sk, &sk->sk_timer);
2549e16163b6SPaolo Abeni 	msk->pm.status = 0;
2550b2c5b614SFlorian Westphal 
2551b2c5b614SFlorian Westphal 	list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
2552cec37a6eSPeter Krystad 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2553e16163b6SPaolo Abeni 		__mptcp_close_ssk(sk, ssk, subflow);
2554f870fa0bSMat Martineau 	}
2555f870fa0bSMat Martineau 
2556e16163b6SPaolo Abeni 	sk->sk_prot->destroy(sk);
2557e16163b6SPaolo Abeni 
2558e93da928SPaolo Abeni 	WARN_ON_ONCE(msk->wmem_reserved);
255987952603SPaolo Abeni 	WARN_ON_ONCE(msk->rmem_released);
2560e16163b6SPaolo Abeni 	sk_stream_kill_queues(sk);
2561e16163b6SPaolo Abeni 	xfrm_sk_free_policy(sk);
2562e16163b6SPaolo Abeni 	sk_refcnt_debug_release(sk);
2563e16163b6SPaolo Abeni 	sock_put(sk);
2564e16163b6SPaolo Abeni }
2565e16163b6SPaolo Abeni 
2566e16163b6SPaolo Abeni static void mptcp_close(struct sock *sk, long timeout)
2567e16163b6SPaolo Abeni {
2568e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow;
2569e16163b6SPaolo Abeni 	bool do_cancel_work = false;
2570e16163b6SPaolo Abeni 
2571e16163b6SPaolo Abeni 	lock_sock(sk);
2572e16163b6SPaolo Abeni 	sk->sk_shutdown = SHUTDOWN_MASK;
2573e16163b6SPaolo Abeni 
2574e16163b6SPaolo Abeni 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2575e16163b6SPaolo Abeni 		inet_sk_state_store(sk, TCP_CLOSE);
2576e16163b6SPaolo Abeni 		goto cleanup;
2577e16163b6SPaolo Abeni 	}
2578e16163b6SPaolo Abeni 
2579e16163b6SPaolo Abeni 	if (mptcp_close_state(sk))
2580e16163b6SPaolo Abeni 		__mptcp_wr_shutdown(sk);
2581e16163b6SPaolo Abeni 
2582e16163b6SPaolo Abeni 	sk_stream_wait_close(sk, timeout);
2583e16163b6SPaolo Abeni 
2584e16163b6SPaolo Abeni cleanup:
2585e16163b6SPaolo Abeni 	/* orphan all the subflows */
2586e16163b6SPaolo Abeni 	inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2587e16163b6SPaolo Abeni 	list_for_each_entry(subflow, &mptcp_sk(sk)->conn_list, node) {
2588e16163b6SPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2589e16163b6SPaolo Abeni 		bool slow, dispose_socket;
2590e16163b6SPaolo Abeni 		struct socket *sock;
2591e16163b6SPaolo Abeni 
2592e16163b6SPaolo Abeni 		slow = lock_sock_fast(ssk);
2593e16163b6SPaolo Abeni 		sock = ssk->sk_socket;
2594e16163b6SPaolo Abeni 		dispose_socket = sock && sock != sk->sk_socket;
2595e16163b6SPaolo Abeni 		sock_orphan(ssk);
2596e16163b6SPaolo Abeni 		unlock_sock_fast(ssk, slow);
2597e16163b6SPaolo Abeni 
2598e16163b6SPaolo Abeni 		/* for the outgoing subflows we additionally need to free
2599e16163b6SPaolo Abeni 		 * the associated socket
2600e16163b6SPaolo Abeni 		 */
2601e16163b6SPaolo Abeni 		if (dispose_socket)
2602e16163b6SPaolo Abeni 			iput(SOCK_INODE(sock));
2603e16163b6SPaolo Abeni 	}
2604e16163b6SPaolo Abeni 	sock_orphan(sk);
2605e16163b6SPaolo Abeni 
2606e16163b6SPaolo Abeni 	sock_hold(sk);
2607e16163b6SPaolo Abeni 	pr_debug("msk=%p state=%d", sk, sk->sk_state);
2608e16163b6SPaolo Abeni 	if (sk->sk_state == TCP_CLOSE) {
2609e16163b6SPaolo Abeni 		__mptcp_destroy_sock(sk);
2610e16163b6SPaolo Abeni 		do_cancel_work = true;
2611e16163b6SPaolo Abeni 	} else {
2612e16163b6SPaolo Abeni 		sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2613e16163b6SPaolo Abeni 	}
2614e16163b6SPaolo Abeni 	release_sock(sk);
2615e16163b6SPaolo Abeni 	if (do_cancel_work)
261680992017SPaolo Abeni 		mptcp_cancel_work(sk);
2617e16163b6SPaolo Abeni 	sock_put(sk);
2618f870fa0bSMat Martineau }
2619f870fa0bSMat Martineau 
2620cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2621cf7da0d6SPeter Krystad {
2622cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2623cf7da0d6SPeter Krystad 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2624cf7da0d6SPeter Krystad 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
2625cf7da0d6SPeter Krystad 
2626cf7da0d6SPeter Krystad 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
2627cf7da0d6SPeter Krystad 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2628cf7da0d6SPeter Krystad 
2629cf7da0d6SPeter Krystad 	if (msk6 && ssk6) {
2630cf7da0d6SPeter Krystad 		msk6->saddr = ssk6->saddr;
2631cf7da0d6SPeter Krystad 		msk6->flow_label = ssk6->flow_label;
2632cf7da0d6SPeter Krystad 	}
2633cf7da0d6SPeter Krystad #endif
2634cf7da0d6SPeter Krystad 
2635cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2636cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2637cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2638cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2639cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2640cf7da0d6SPeter Krystad 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2641cf7da0d6SPeter Krystad }
2642cf7da0d6SPeter Krystad 
264318b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags)
264418b683bfSPaolo Abeni {
264576e2a55dSPaolo Abeni 	struct mptcp_subflow_context *subflow;
264676e2a55dSPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(sk);
264776e2a55dSPaolo Abeni 
264876e2a55dSPaolo Abeni 	__mptcp_flush_join_list(msk);
264913a9499eSPaolo Abeni 	mptcp_for_each_subflow(msk, subflow) {
265013a9499eSPaolo Abeni 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
265113a9499eSPaolo Abeni 
265213a9499eSPaolo Abeni 		lock_sock(ssk);
265313a9499eSPaolo Abeni 		tcp_disconnect(ssk, flags);
265413a9499eSPaolo Abeni 		release_sock(ssk);
265513a9499eSPaolo Abeni 	}
265642c556feSFlorian Westphal 	return 0;
265718b683bfSPaolo Abeni }
265818b683bfSPaolo Abeni 
2659b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2660b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2661b0519de8SFlorian Westphal {
2662b0519de8SFlorian Westphal 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2663b0519de8SFlorian Westphal 
2664b0519de8SFlorian Westphal 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2665b0519de8SFlorian Westphal }
2666b0519de8SFlorian Westphal #endif
2667b0519de8SFlorian Westphal 
2668fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk,
2669cfde141eSPaolo Abeni 			    const struct mptcp_options_received *mp_opt,
2670fca5c82cSPaolo Abeni 			    struct request_sock *req)
2671b0519de8SFlorian Westphal {
267258b09919SPaolo Abeni 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2673b0519de8SFlorian Westphal 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
267458b09919SPaolo Abeni 	struct mptcp_sock *msk;
267558b09919SPaolo Abeni 	u64 ack_seq;
2676b0519de8SFlorian Westphal 
2677b0519de8SFlorian Westphal 	if (!nsk)
2678b0519de8SFlorian Westphal 		return NULL;
2679b0519de8SFlorian Westphal 
2680b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2681b0519de8SFlorian Westphal 	if (nsk->sk_family == AF_INET6)
2682b0519de8SFlorian Westphal 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2683b0519de8SFlorian Westphal #endif
2684b0519de8SFlorian Westphal 
268558b09919SPaolo Abeni 	__mptcp_init_sock(nsk);
268658b09919SPaolo Abeni 
268758b09919SPaolo Abeni 	msk = mptcp_sk(nsk);
268858b09919SPaolo Abeni 	msk->local_key = subflow_req->local_key;
268958b09919SPaolo Abeni 	msk->token = subflow_req->token;
269058b09919SPaolo Abeni 	msk->subflow = NULL;
2691b93df08cSPaolo Abeni 	WRITE_ONCE(msk->fully_established, false);
269258b09919SPaolo Abeni 
269358b09919SPaolo Abeni 	msk->write_seq = subflow_req->idsn + 1;
2694eaa2ffabSPaolo Abeni 	msk->snd_nxt = msk->write_seq;
26957439d687SPaolo Abeni 	msk->snd_una = msk->write_seq;
26967439d687SPaolo Abeni 	msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
26976f8a612aSFlorian Westphal 
2698cfde141eSPaolo Abeni 	if (mp_opt->mp_capable) {
269958b09919SPaolo Abeni 		msk->can_ack = true;
2700cfde141eSPaolo Abeni 		msk->remote_key = mp_opt->sndr_key;
270158b09919SPaolo Abeni 		mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
270258b09919SPaolo Abeni 		ack_seq++;
2703917944daSMat Martineau 		WRITE_ONCE(msk->ack_seq, ack_seq);
2704fa3fe2b1SFlorian Westphal 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
270558b09919SPaolo Abeni 	}
27067f20d5fcSPaolo Abeni 
27075e20087dSFlorian Westphal 	sock_reset_flag(nsk, SOCK_RCU_FREE);
27087f20d5fcSPaolo Abeni 	/* will be fully established after successful MPC subflow creation */
27097f20d5fcSPaolo Abeni 	inet_sk_state_store(nsk, TCP_SYN_RECV);
27100c148460SPaolo Abeni 
27110c148460SPaolo Abeni 	security_inet_csk_clone(nsk, req);
271258b09919SPaolo Abeni 	bh_unlock_sock(nsk);
271358b09919SPaolo Abeni 
271458b09919SPaolo Abeni 	/* keep a single reference */
271558b09919SPaolo Abeni 	__sock_put(nsk);
2716b0519de8SFlorian Westphal 	return nsk;
2717b0519de8SFlorian Westphal }
2718b0519de8SFlorian Westphal 
2719a6b118feSFlorian Westphal void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2720a6b118feSFlorian Westphal {
2721a6b118feSFlorian Westphal 	const struct tcp_sock *tp = tcp_sk(ssk);
2722a6b118feSFlorian Westphal 
2723a6b118feSFlorian Westphal 	msk->rcvq_space.copied = 0;
2724a6b118feSFlorian Westphal 	msk->rcvq_space.rtt_us = 0;
2725a6b118feSFlorian Westphal 
2726a6b118feSFlorian Westphal 	msk->rcvq_space.time = tp->tcp_mstamp;
2727a6b118feSFlorian Westphal 
2728a6b118feSFlorian Westphal 	/* initial rcv_space offering made to peer */
2729a6b118feSFlorian Westphal 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2730a6b118feSFlorian Westphal 				      TCP_INIT_CWND * tp->advmss);
2731a6b118feSFlorian Westphal 	if (msk->rcvq_space.space == 0)
2732a6b118feSFlorian Westphal 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
27336f8a612aSFlorian Westphal 
27347439d687SPaolo Abeni 	WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
2735a6b118feSFlorian Westphal }
2736a6b118feSFlorian Westphal 
2737cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2738cf7da0d6SPeter Krystad 				 bool kern)
2739cf7da0d6SPeter Krystad {
2740cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
2741cf7da0d6SPeter Krystad 	struct socket *listener;
2742cf7da0d6SPeter Krystad 	struct sock *newsk;
2743cf7da0d6SPeter Krystad 
2744cf7da0d6SPeter Krystad 	listener = __mptcp_nmpc_socket(msk);
2745cf7da0d6SPeter Krystad 	if (WARN_ON_ONCE(!listener)) {
2746cf7da0d6SPeter Krystad 		*err = -EINVAL;
2747cf7da0d6SPeter Krystad 		return NULL;
2748cf7da0d6SPeter Krystad 	}
2749cf7da0d6SPeter Krystad 
2750cf7da0d6SPeter Krystad 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2751cf7da0d6SPeter Krystad 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
2752cf7da0d6SPeter Krystad 	if (!newsk)
2753cf7da0d6SPeter Krystad 		return NULL;
2754cf7da0d6SPeter Krystad 
2755cf7da0d6SPeter Krystad 	pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
2756cf7da0d6SPeter Krystad 	if (sk_is_mptcp(newsk)) {
2757cf7da0d6SPeter Krystad 		struct mptcp_subflow_context *subflow;
2758cf7da0d6SPeter Krystad 		struct sock *new_mptcp_sock;
2759cf7da0d6SPeter Krystad 
2760cf7da0d6SPeter Krystad 		subflow = mptcp_subflow_ctx(newsk);
276158b09919SPaolo Abeni 		new_mptcp_sock = subflow->conn;
276258b09919SPaolo Abeni 
276358b09919SPaolo Abeni 		/* is_mptcp should be false if subflow->conn is missing, see
276458b09919SPaolo Abeni 		 * subflow_syn_recv_sock()
276558b09919SPaolo Abeni 		 */
276658b09919SPaolo Abeni 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
276758b09919SPaolo Abeni 			tcp_sk(newsk)->is_mptcp = 0;
276858b09919SPaolo Abeni 			return newsk;
276958b09919SPaolo Abeni 		}
277058b09919SPaolo Abeni 
277158b09919SPaolo Abeni 		/* acquire the 2nd reference for the owning socket */
277258b09919SPaolo Abeni 		sock_hold(new_mptcp_sock);
2773cf7da0d6SPeter Krystad 		newsk = new_mptcp_sock;
27740397c6d8SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
2775fc518953SFlorian Westphal 	} else {
2776fc518953SFlorian Westphal 		MPTCP_INC_STATS(sock_net(sk),
2777fc518953SFlorian Westphal 				MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
2778cf7da0d6SPeter Krystad 	}
2779cf7da0d6SPeter Krystad 
2780cf7da0d6SPeter Krystad 	return newsk;
2781cf7da0d6SPeter Krystad }
2782cf7da0d6SPeter Krystad 
27835c8c1640SGeliang Tang void mptcp_destroy_common(struct mptcp_sock *msk)
27845c8c1640SGeliang Tang {
278587952603SPaolo Abeni 	struct sock *sk = (struct sock *)msk;
278687952603SPaolo Abeni 
27876e628cd3SPaolo Abeni 	__mptcp_clear_xmit(sk);
27886e628cd3SPaolo Abeni 
278987952603SPaolo Abeni 	/* move to sk_receive_queue, sk_stream_kill_queues will purge it */
279087952603SPaolo Abeni 	skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
279187952603SPaolo Abeni 
27925c8c1640SGeliang Tang 	skb_rbtree_purge(&msk->out_of_order_queue);
27935c8c1640SGeliang Tang 	mptcp_token_destroy(msk);
27945c8c1640SGeliang Tang 	mptcp_pm_free_anno_list(msk);
27955c8c1640SGeliang Tang }
27965c8c1640SGeliang Tang 
279779c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk)
279879c0949eSPeter Krystad {
2799c9fd9c5fSFlorian Westphal 	struct mptcp_sock *msk = mptcp_sk(sk);
2800c9fd9c5fSFlorian Westphal 
28015c8c1640SGeliang Tang 	mptcp_destroy_common(msk);
2802d027236cSPaolo Abeni 	sk_sockets_allocated_dec(sk);
280379c0949eSPeter Krystad }
280479c0949eSPeter Krystad 
2805fd1452d8SFlorian Westphal static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
2806a7b75c5aSChristoph Hellwig 				       sockptr_t optval, unsigned int optlen)
2807fd1452d8SFlorian Westphal {
2808fd1452d8SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
2809fd1452d8SFlorian Westphal 	struct socket *ssock;
2810fd1452d8SFlorian Westphal 	int ret;
2811fd1452d8SFlorian Westphal 
2812fd1452d8SFlorian Westphal 	switch (optname) {
2813fd1452d8SFlorian Westphal 	case SO_REUSEPORT:
2814fd1452d8SFlorian Westphal 	case SO_REUSEADDR:
2815fd1452d8SFlorian Westphal 		lock_sock(sk);
2816fd1452d8SFlorian Westphal 		ssock = __mptcp_nmpc_socket(msk);
2817fd1452d8SFlorian Westphal 		if (!ssock) {
2818fd1452d8SFlorian Westphal 			release_sock(sk);
2819fd1452d8SFlorian Westphal 			return -EINVAL;
2820fd1452d8SFlorian Westphal 		}
2821fd1452d8SFlorian Westphal 
2822a7b75c5aSChristoph Hellwig 		ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen);
2823fd1452d8SFlorian Westphal 		if (ret == 0) {
2824fd1452d8SFlorian Westphal 			if (optname == SO_REUSEPORT)
2825fd1452d8SFlorian Westphal 				sk->sk_reuseport = ssock->sk->sk_reuseport;
2826fd1452d8SFlorian Westphal 			else if (optname == SO_REUSEADDR)
2827fd1452d8SFlorian Westphal 				sk->sk_reuse = ssock->sk->sk_reuse;
2828fd1452d8SFlorian Westphal 		}
2829fd1452d8SFlorian Westphal 		release_sock(sk);
2830fd1452d8SFlorian Westphal 		return ret;
2831fd1452d8SFlorian Westphal 	}
2832fd1452d8SFlorian Westphal 
2833a7b75c5aSChristoph Hellwig 	return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen);
2834fd1452d8SFlorian Westphal }
2835fd1452d8SFlorian Westphal 
2836c9b95a13SFlorian Westphal static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
2837a7b75c5aSChristoph Hellwig 			       sockptr_t optval, unsigned int optlen)
2838c9b95a13SFlorian Westphal {
2839c9b95a13SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
2840c9b95a13SFlorian Westphal 	int ret = -EOPNOTSUPP;
2841c9b95a13SFlorian Westphal 	struct socket *ssock;
2842c9b95a13SFlorian Westphal 
2843c9b95a13SFlorian Westphal 	switch (optname) {
2844c9b95a13SFlorian Westphal 	case IPV6_V6ONLY:
2845c9b95a13SFlorian Westphal 		lock_sock(sk);
2846c9b95a13SFlorian Westphal 		ssock = __mptcp_nmpc_socket(msk);
2847c9b95a13SFlorian Westphal 		if (!ssock) {
2848c9b95a13SFlorian Westphal 			release_sock(sk);
2849c9b95a13SFlorian Westphal 			return -EINVAL;
2850c9b95a13SFlorian Westphal 		}
2851c9b95a13SFlorian Westphal 
2852c9b95a13SFlorian Westphal 		ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
2853c9b95a13SFlorian Westphal 		if (ret == 0)
2854c9b95a13SFlorian Westphal 			sk->sk_ipv6only = ssock->sk->sk_ipv6only;
2855c9b95a13SFlorian Westphal 
2856c9b95a13SFlorian Westphal 		release_sock(sk);
2857c9b95a13SFlorian Westphal 		break;
2858c9b95a13SFlorian Westphal 	}
2859c9b95a13SFlorian Westphal 
2860c9b95a13SFlorian Westphal 	return ret;
2861c9b95a13SFlorian Westphal }
2862c9b95a13SFlorian Westphal 
2863717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname,
2864a7b75c5aSChristoph Hellwig 			    sockptr_t optval, unsigned int optlen)
2865717e79c8SPeter Krystad {
2866717e79c8SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
286776660afbSPaolo Abeni 	struct sock *ssk;
2868717e79c8SPeter Krystad 
2869717e79c8SPeter Krystad 	pr_debug("msk=%p", msk);
2870717e79c8SPeter Krystad 
287183f0c10bSFlorian Westphal 	if (level == SOL_SOCKET)
2872fd1452d8SFlorian Westphal 		return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
287383f0c10bSFlorian Westphal 
2874717e79c8SPeter Krystad 	/* @@ the meaning of setsockopt() when the socket is connected and
2875b6e4a1aeSMat Martineau 	 * there are multiple subflows is not yet defined. It is up to the
2876b6e4a1aeSMat Martineau 	 * MPTCP-level socket to configure the subflows until the subflow
2877b6e4a1aeSMat Martineau 	 * is in TCP fallback, when TCP socket options are passed through
2878b6e4a1aeSMat Martineau 	 * to the one remaining subflow.
2879717e79c8SPeter Krystad 	 */
2880717e79c8SPeter Krystad 	lock_sock(sk);
288176660afbSPaolo Abeni 	ssk = __mptcp_tcp_fallback(msk);
2882e154659bSFlorian Westphal 	release_sock(sk);
288376660afbSPaolo Abeni 	if (ssk)
288476660afbSPaolo Abeni 		return tcp_setsockopt(ssk, level, optname, optval, optlen);
288550e741bbSFlorian Westphal 
2886c9b95a13SFlorian Westphal 	if (level == SOL_IPV6)
2887c9b95a13SFlorian Westphal 		return mptcp_setsockopt_v6(msk, optname, optval, optlen);
2888c9b95a13SFlorian Westphal 
2889b6e4a1aeSMat Martineau 	return -EOPNOTSUPP;
2890717e79c8SPeter Krystad }
2891717e79c8SPeter Krystad 
2892717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname,
289350e741bbSFlorian Westphal 			    char __user *optval, int __user *option)
2894717e79c8SPeter Krystad {
2895717e79c8SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sk);
289676660afbSPaolo Abeni 	struct sock *ssk;
2897717e79c8SPeter Krystad 
2898717e79c8SPeter Krystad 	pr_debug("msk=%p", msk);
2899717e79c8SPeter Krystad 
2900b6e4a1aeSMat Martineau 	/* @@ the meaning of setsockopt() when the socket is connected and
2901b6e4a1aeSMat Martineau 	 * there are multiple subflows is not yet defined. It is up to the
2902b6e4a1aeSMat Martineau 	 * MPTCP-level socket to configure the subflows until the subflow
2903b6e4a1aeSMat Martineau 	 * is in TCP fallback, when socket options are passed through
2904b6e4a1aeSMat Martineau 	 * to the one remaining subflow.
2905717e79c8SPeter Krystad 	 */
2906717e79c8SPeter Krystad 	lock_sock(sk);
290776660afbSPaolo Abeni 	ssk = __mptcp_tcp_fallback(msk);
2908e154659bSFlorian Westphal 	release_sock(sk);
290976660afbSPaolo Abeni 	if (ssk)
291076660afbSPaolo Abeni 		return tcp_getsockopt(ssk, level, optname, optval, option);
291150e741bbSFlorian Westphal 
2912b6e4a1aeSMat Martineau 	return -EOPNOTSUPP;
2913717e79c8SPeter Krystad }
2914717e79c8SPeter Krystad 
29156e628cd3SPaolo Abeni void __mptcp_data_acked(struct sock *sk)
29166e628cd3SPaolo Abeni {
29176e628cd3SPaolo Abeni 	if (!sock_owned_by_user(sk))
29186e628cd3SPaolo Abeni 		__mptcp_clean_una(sk);
29196e628cd3SPaolo Abeni 	else
29206e628cd3SPaolo Abeni 		set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
29216e628cd3SPaolo Abeni 
29226e628cd3SPaolo Abeni 	if (mptcp_pending_data_fin_ack(sk))
29236e628cd3SPaolo Abeni 		mptcp_schedule_work(sk);
29246e628cd3SPaolo Abeni }
29256e628cd3SPaolo Abeni 
2926219d0499SPaolo Abeni void __mptcp_check_push(struct sock *sk, struct sock *ssk)
29276e628cd3SPaolo Abeni {
29286e628cd3SPaolo Abeni 	if (!mptcp_send_head(sk))
29296e628cd3SPaolo Abeni 		return;
29306e628cd3SPaolo Abeni 
29316e628cd3SPaolo Abeni 	if (!sock_owned_by_user(sk))
29326e628cd3SPaolo Abeni 		__mptcp_subflow_push_pending(sk, ssk);
29336e628cd3SPaolo Abeni 	else
29346e628cd3SPaolo Abeni 		set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
29356e628cd3SPaolo Abeni }
29366e628cd3SPaolo Abeni 
2937ea4ca586SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_WRITE_TIMER_DEFERRED)
293814c441b5SPaolo Abeni 
2939e93da928SPaolo Abeni /* processes deferred events and flush wmem */
294014c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk)
294114c441b5SPaolo Abeni {
294214c441b5SPaolo Abeni 	unsigned long flags, nflags;
294314c441b5SPaolo Abeni 
29446e628cd3SPaolo Abeni 	/* push_pending may touch wmem_reserved, do it before the later
29456e628cd3SPaolo Abeni 	 * cleanup
29466e628cd3SPaolo Abeni 	 */
29476e628cd3SPaolo Abeni 	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
29486e628cd3SPaolo Abeni 		__mptcp_clean_una(sk);
29496e628cd3SPaolo Abeni 	if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) {
29506e628cd3SPaolo Abeni 		/* mptcp_push_pending() acquires the subflow socket lock
29516e628cd3SPaolo Abeni 		 *
29526e628cd3SPaolo Abeni 		 * 1) can't be invoked in atomic scope
29536e628cd3SPaolo Abeni 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
29546e628cd3SPaolo Abeni 		 *    datapath acquires the msk socket spinlock while helding
29556e628cd3SPaolo Abeni 		 *    the subflow socket lock
29566e628cd3SPaolo Abeni 		 */
29576e628cd3SPaolo Abeni 
29586e628cd3SPaolo Abeni 		spin_unlock_bh(&sk->sk_lock.slock);
29596e628cd3SPaolo Abeni 		mptcp_push_pending(sk, 0);
29606e628cd3SPaolo Abeni 		spin_lock_bh(&sk->sk_lock.slock);
29616e628cd3SPaolo Abeni 	}
2962*15cc1045SPaolo Abeni 	if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
2963*15cc1045SPaolo Abeni 		__mptcp_error_report(sk);
29646e628cd3SPaolo Abeni 
2965e93da928SPaolo Abeni 	/* clear any wmem reservation and errors */
2966e93da928SPaolo Abeni 	__mptcp_update_wmem(sk);
296787952603SPaolo Abeni 	__mptcp_update_rmem(sk);
2968e93da928SPaolo Abeni 
296914c441b5SPaolo Abeni 	do {
297014c441b5SPaolo Abeni 		flags = sk->sk_tsq_flags;
297114c441b5SPaolo Abeni 		if (!(flags & MPTCP_DEFERRED_ALL))
297214c441b5SPaolo Abeni 			return;
297314c441b5SPaolo Abeni 		nflags = flags & ~MPTCP_DEFERRED_ALL;
297414c441b5SPaolo Abeni 	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
297514c441b5SPaolo Abeni 
2976b51f9b80SPaolo Abeni 	sock_release_ownership(sk);
2977b51f9b80SPaolo Abeni 
2978b51f9b80SPaolo Abeni 	if (flags & TCPF_WRITE_TIMER_DEFERRED) {
2979b51f9b80SPaolo Abeni 		mptcp_retransmit_handler(sk);
2980b51f9b80SPaolo Abeni 		__sock_put(sk);
2981b51f9b80SPaolo Abeni 	}
298214c441b5SPaolo Abeni }
298314c441b5SPaolo Abeni 
29842c5ebd00SPaolo Abeni static int mptcp_hash(struct sock *sk)
29852c5ebd00SPaolo Abeni {
29862c5ebd00SPaolo Abeni 	/* should never be called,
29872c5ebd00SPaolo Abeni 	 * we hash the TCP subflows not the master socket
29882c5ebd00SPaolo Abeni 	 */
29892c5ebd00SPaolo Abeni 	WARN_ON_ONCE(1);
29902c5ebd00SPaolo Abeni 	return 0;
29912c5ebd00SPaolo Abeni }
29922c5ebd00SPaolo Abeni 
29932c5ebd00SPaolo Abeni static void mptcp_unhash(struct sock *sk)
29942c5ebd00SPaolo Abeni {
29952c5ebd00SPaolo Abeni 	/* called from sk_common_release(), but nothing to do here */
29962c5ebd00SPaolo Abeni }
29972c5ebd00SPaolo Abeni 
2998cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum)
2999f870fa0bSMat Martineau {
3000f870fa0bSMat Martineau 	struct mptcp_sock *msk = mptcp_sk(sk);
3001cec37a6eSPeter Krystad 	struct socket *ssock;
3002f870fa0bSMat Martineau 
3003cec37a6eSPeter Krystad 	ssock = __mptcp_nmpc_socket(msk);
3004cec37a6eSPeter Krystad 	pr_debug("msk=%p, subflow=%p", msk, ssock);
3005cec37a6eSPeter Krystad 	if (WARN_ON_ONCE(!ssock))
3006cec37a6eSPeter Krystad 		return -EINVAL;
3007f870fa0bSMat Martineau 
3008cec37a6eSPeter Krystad 	return inet_csk_get_port(ssock->sk, snum);
3009cec37a6eSPeter Krystad }
3010f870fa0bSMat Martineau 
3011cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk)
3012cec37a6eSPeter Krystad {
3013cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow;
3014cec37a6eSPeter Krystad 	struct mptcp_sock *msk;
3015cec37a6eSPeter Krystad 	struct sock *sk;
30166d0060f6SMat Martineau 	u64 ack_seq;
3017f870fa0bSMat Martineau 
3018cec37a6eSPeter Krystad 	subflow = mptcp_subflow_ctx(ssk);
3019cec37a6eSPeter Krystad 	sk = subflow->conn;
3020cec37a6eSPeter Krystad 	msk = mptcp_sk(sk);
3021cec37a6eSPeter Krystad 
3022648ef4b8SMat Martineau 	pr_debug("msk=%p, token=%u", sk, subflow->token);
3023648ef4b8SMat Martineau 
30246d0060f6SMat Martineau 	mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
30256d0060f6SMat Martineau 	ack_seq++;
3026648ef4b8SMat Martineau 	subflow->map_seq = ack_seq;
3027648ef4b8SMat Martineau 	subflow->map_subflow_seq = 1;
30286d0060f6SMat Martineau 
3029cec37a6eSPeter Krystad 	/* the socket is not connected yet, no msk/subflow ops can access/race
3030cec37a6eSPeter Krystad 	 * accessing the field below
3031cec37a6eSPeter Krystad 	 */
3032cec37a6eSPeter Krystad 	WRITE_ONCE(msk->remote_key, subflow->remote_key);
3033cec37a6eSPeter Krystad 	WRITE_ONCE(msk->local_key, subflow->local_key);
30346d0060f6SMat Martineau 	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3035eaa2ffabSPaolo Abeni 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
30366d0060f6SMat Martineau 	WRITE_ONCE(msk->ack_seq, ack_seq);
3037fa3fe2b1SFlorian Westphal 	WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
3038d22f4988SChristoph Paasch 	WRITE_ONCE(msk->can_ack, 1);
30397439d687SPaolo Abeni 	WRITE_ONCE(msk->snd_una, msk->write_seq);
30401b1c7a0eSPeter Krystad 
30411b1c7a0eSPeter Krystad 	mptcp_pm_new_connection(msk, 0);
3042a6b118feSFlorian Westphal 
3043a6b118feSFlorian Westphal 	mptcp_rcv_space_init(msk, ssk);
3044f870fa0bSMat Martineau }
3045f870fa0bSMat Martineau 
3046cf7da0d6SPeter Krystad static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3047cf7da0d6SPeter Krystad {
3048cf7da0d6SPeter Krystad 	write_lock_bh(&sk->sk_callback_lock);
3049cf7da0d6SPeter Krystad 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
3050cf7da0d6SPeter Krystad 	sk_set_socket(sk, parent);
3051cf7da0d6SPeter Krystad 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
3052cf7da0d6SPeter Krystad 	write_unlock_bh(&sk->sk_callback_lock);
3053cf7da0d6SPeter Krystad }
3054cf7da0d6SPeter Krystad 
3055e16163b6SPaolo Abeni bool mptcp_finish_join(struct sock *ssk)
3056f296234cSPeter Krystad {
3057e16163b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3058f296234cSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3059f296234cSPeter Krystad 	struct sock *parent = (void *)msk;
3060f296234cSPeter Krystad 	struct socket *parent_sock;
3061ec3edaa7SPeter Krystad 	bool ret;
3062f296234cSPeter Krystad 
3063f296234cSPeter Krystad 	pr_debug("msk=%p, subflow=%p", msk, subflow);
3064f296234cSPeter Krystad 
3065f296234cSPeter Krystad 	/* mptcp socket already closing? */
3066b93df08cSPaolo Abeni 	if (!mptcp_is_fully_established(parent))
3067f296234cSPeter Krystad 		return false;
3068f296234cSPeter Krystad 
3069f296234cSPeter Krystad 	if (!msk->pm.server_side)
3070f296234cSPeter Krystad 		return true;
3071f296234cSPeter Krystad 
307210f6d46cSPaolo Abeni 	if (!mptcp_pm_allow_new_subflow(msk))
307310f6d46cSPaolo Abeni 		return false;
307410f6d46cSPaolo Abeni 
307510f6d46cSPaolo Abeni 	/* active connections are already on conn_list, and we can't acquire
307610f6d46cSPaolo Abeni 	 * msk lock here.
307710f6d46cSPaolo Abeni 	 * use the join list lock as synchronization point and double-check
3078e16163b6SPaolo Abeni 	 * msk status to avoid racing with __mptcp_destroy_sock()
307910f6d46cSPaolo Abeni 	 */
308010f6d46cSPaolo Abeni 	spin_lock_bh(&msk->join_list_lock);
308110f6d46cSPaolo Abeni 	ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
3082e16163b6SPaolo Abeni 	if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
308310f6d46cSPaolo Abeni 		list_add_tail(&subflow->node, &msk->join_list);
3084e16163b6SPaolo Abeni 		sock_hold(ssk);
3085e16163b6SPaolo Abeni 	}
308610f6d46cSPaolo Abeni 	spin_unlock_bh(&msk->join_list_lock);
308710f6d46cSPaolo Abeni 	if (!ret)
308810f6d46cSPaolo Abeni 		return false;
308910f6d46cSPaolo Abeni 
309010f6d46cSPaolo Abeni 	/* attach to msk socket only after we are sure he will deal with us
309110f6d46cSPaolo Abeni 	 * at close time
309210f6d46cSPaolo Abeni 	 */
3093f296234cSPeter Krystad 	parent_sock = READ_ONCE(parent->sk_socket);
3094e16163b6SPaolo Abeni 	if (parent_sock && !ssk->sk_socket)
3095e16163b6SPaolo Abeni 		mptcp_sock_graft(ssk, parent_sock);
3096917944daSMat Martineau 	subflow->map_seq = READ_ONCE(msk->ack_seq);
309710f6d46cSPaolo Abeni 	return true;
3098f296234cSPeter Krystad }
3099f296234cSPeter Krystad 
310076e2a55dSPaolo Abeni static void mptcp_shutdown(struct sock *sk, int how)
310176e2a55dSPaolo Abeni {
310276e2a55dSPaolo Abeni 	pr_debug("sk=%p, how=%d", sk, how);
310376e2a55dSPaolo Abeni 
310476e2a55dSPaolo Abeni 	if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
310576e2a55dSPaolo Abeni 		__mptcp_wr_shutdown(sk);
310676e2a55dSPaolo Abeni }
310776e2a55dSPaolo Abeni 
3108f870fa0bSMat Martineau static struct proto mptcp_prot = {
3109f870fa0bSMat Martineau 	.name		= "MPTCP",
3110f870fa0bSMat Martineau 	.owner		= THIS_MODULE,
3111f870fa0bSMat Martineau 	.init		= mptcp_init_sock,
311218b683bfSPaolo Abeni 	.disconnect	= mptcp_disconnect,
3113f870fa0bSMat Martineau 	.close		= mptcp_close,
3114cf7da0d6SPeter Krystad 	.accept		= mptcp_accept,
3115717e79c8SPeter Krystad 	.setsockopt	= mptcp_setsockopt,
3116717e79c8SPeter Krystad 	.getsockopt	= mptcp_getsockopt,
311776e2a55dSPaolo Abeni 	.shutdown	= mptcp_shutdown,
311879c0949eSPeter Krystad 	.destroy	= mptcp_destroy,
3119f870fa0bSMat Martineau 	.sendmsg	= mptcp_sendmsg,
3120f870fa0bSMat Martineau 	.recvmsg	= mptcp_recvmsg,
312114c441b5SPaolo Abeni 	.release_cb	= mptcp_release_cb,
31222c5ebd00SPaolo Abeni 	.hash		= mptcp_hash,
31232c5ebd00SPaolo Abeni 	.unhash		= mptcp_unhash,
3124cec37a6eSPeter Krystad 	.get_port	= mptcp_get_port,
3125d027236cSPaolo Abeni 	.sockets_allocated	= &mptcp_sockets_allocated,
3126d027236cSPaolo Abeni 	.memory_allocated	= &tcp_memory_allocated,
3127d027236cSPaolo Abeni 	.memory_pressure	= &tcp_memory_pressure,
3128d027236cSPaolo Abeni 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
3129989ef49bSPaolo Abeni 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
3130d027236cSPaolo Abeni 	.sysctl_mem	= sysctl_tcp_mem,
3131f870fa0bSMat Martineau 	.obj_size	= sizeof(struct mptcp_sock),
31322c5ebd00SPaolo Abeni 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
3133f870fa0bSMat Martineau 	.no_autobind	= true,
3134f870fa0bSMat Martineau };
3135f870fa0bSMat Martineau 
31362303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
31372303f994SPeter Krystad {
31382303f994SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
31392303f994SPeter Krystad 	struct socket *ssock;
3140cf7da0d6SPeter Krystad 	int err;
31412303f994SPeter Krystad 
31422303f994SPeter Krystad 	lock_sock(sock->sk);
3143fa68018dSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
3144fa68018dSPaolo Abeni 	if (!ssock) {
3145fa68018dSPaolo Abeni 		err = -EINVAL;
31462303f994SPeter Krystad 		goto unlock;
31472303f994SPeter Krystad 	}
31482303f994SPeter Krystad 
31492303f994SPeter Krystad 	err = ssock->ops->bind(ssock, uaddr, addr_len);
3150cf7da0d6SPeter Krystad 	if (!err)
3151cf7da0d6SPeter Krystad 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
31522303f994SPeter Krystad 
31532303f994SPeter Krystad unlock:
31542303f994SPeter Krystad 	release_sock(sock->sk);
31552303f994SPeter Krystad 	return err;
31562303f994SPeter Krystad }
31572303f994SPeter Krystad 
31580235d075SPaolo Abeni static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
31590235d075SPaolo Abeni 					 struct mptcp_subflow_context *subflow)
31600235d075SPaolo Abeni {
31610235d075SPaolo Abeni 	subflow->request_mptcp = 0;
31620235d075SPaolo Abeni 	__mptcp_do_fallback(msk);
31630235d075SPaolo Abeni }
31640235d075SPaolo Abeni 
31652303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
31662303f994SPeter Krystad 				int addr_len, int flags)
31672303f994SPeter Krystad {
31682303f994SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
31692c5ebd00SPaolo Abeni 	struct mptcp_subflow_context *subflow;
31702303f994SPeter Krystad 	struct socket *ssock;
31712303f994SPeter Krystad 	int err;
31722303f994SPeter Krystad 
31732303f994SPeter Krystad 	lock_sock(sock->sk);
317441be81a8SPaolo Abeni 	if (sock->state != SS_UNCONNECTED && msk->subflow) {
317541be81a8SPaolo Abeni 		/* pending connection or invalid state, let existing subflow
317641be81a8SPaolo Abeni 		 * cope with that
317741be81a8SPaolo Abeni 		 */
317841be81a8SPaolo Abeni 		ssock = msk->subflow;
317941be81a8SPaolo Abeni 		goto do_connect;
318041be81a8SPaolo Abeni 	}
318141be81a8SPaolo Abeni 
3182fa68018dSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
3183fa68018dSPaolo Abeni 	if (!ssock) {
3184fa68018dSPaolo Abeni 		err = -EINVAL;
31852303f994SPeter Krystad 		goto unlock;
31862303f994SPeter Krystad 	}
31872303f994SPeter Krystad 
3188fa68018dSPaolo Abeni 	mptcp_token_destroy(msk);
3189fa68018dSPaolo Abeni 	inet_sk_state_store(sock->sk, TCP_SYN_SENT);
31902c5ebd00SPaolo Abeni 	subflow = mptcp_subflow_ctx(ssock->sk);
3191cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG
3192cf7da0d6SPeter Krystad 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3193cf7da0d6SPeter Krystad 	 * TCP option space.
3194cf7da0d6SPeter Krystad 	 */
3195cf7da0d6SPeter Krystad 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
31960235d075SPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
3197cf7da0d6SPeter Krystad #endif
31982c5ebd00SPaolo Abeni 	if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk))
31990235d075SPaolo Abeni 		mptcp_subflow_early_fallback(msk, subflow);
3200cf7da0d6SPeter Krystad 
320141be81a8SPaolo Abeni do_connect:
32022303f994SPeter Krystad 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
320341be81a8SPaolo Abeni 	sock->state = ssock->state;
320441be81a8SPaolo Abeni 
320541be81a8SPaolo Abeni 	/* on successful connect, the msk state will be moved to established by
320641be81a8SPaolo Abeni 	 * subflow_finish_connect()
320741be81a8SPaolo Abeni 	 */
3208367fe04eSMatthieu Baerts 	if (!err || err == -EINPROGRESS)
3209cf7da0d6SPeter Krystad 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
321041be81a8SPaolo Abeni 	else
321141be81a8SPaolo Abeni 		inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
32122303f994SPeter Krystad 
32132303f994SPeter Krystad unlock:
32142303f994SPeter Krystad 	release_sock(sock->sk);
32152303f994SPeter Krystad 	return err;
32162303f994SPeter Krystad }
32172303f994SPeter Krystad 
3218cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog)
3219cf7da0d6SPeter Krystad {
3220cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3221cf7da0d6SPeter Krystad 	struct socket *ssock;
3222cf7da0d6SPeter Krystad 	int err;
3223cf7da0d6SPeter Krystad 
3224cf7da0d6SPeter Krystad 	pr_debug("msk=%p", msk);
3225cf7da0d6SPeter Krystad 
3226cf7da0d6SPeter Krystad 	lock_sock(sock->sk);
3227fa68018dSPaolo Abeni 	ssock = __mptcp_nmpc_socket(msk);
3228fa68018dSPaolo Abeni 	if (!ssock) {
3229fa68018dSPaolo Abeni 		err = -EINVAL;
3230cf7da0d6SPeter Krystad 		goto unlock;
3231cf7da0d6SPeter Krystad 	}
3232cf7da0d6SPeter Krystad 
3233fa68018dSPaolo Abeni 	mptcp_token_destroy(msk);
3234fa68018dSPaolo Abeni 	inet_sk_state_store(sock->sk, TCP_LISTEN);
32355e20087dSFlorian Westphal 	sock_set_flag(sock->sk, SOCK_RCU_FREE);
32365e20087dSFlorian Westphal 
3237cf7da0d6SPeter Krystad 	err = ssock->ops->listen(ssock, backlog);
3238cf7da0d6SPeter Krystad 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3239cf7da0d6SPeter Krystad 	if (!err)
3240cf7da0d6SPeter Krystad 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
3241cf7da0d6SPeter Krystad 
3242cf7da0d6SPeter Krystad unlock:
3243cf7da0d6SPeter Krystad 	release_sock(sock->sk);
3244cf7da0d6SPeter Krystad 	return err;
3245cf7da0d6SPeter Krystad }
3246cf7da0d6SPeter Krystad 
3247cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3248cf7da0d6SPeter Krystad 			       int flags, bool kern)
3249cf7da0d6SPeter Krystad {
3250cf7da0d6SPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3251cf7da0d6SPeter Krystad 	struct socket *ssock;
3252cf7da0d6SPeter Krystad 	int err;
3253cf7da0d6SPeter Krystad 
3254cf7da0d6SPeter Krystad 	pr_debug("msk=%p", msk);
3255cf7da0d6SPeter Krystad 
3256cf7da0d6SPeter Krystad 	lock_sock(sock->sk);
3257cf7da0d6SPeter Krystad 	if (sock->sk->sk_state != TCP_LISTEN)
3258cf7da0d6SPeter Krystad 		goto unlock_fail;
3259cf7da0d6SPeter Krystad 
3260cf7da0d6SPeter Krystad 	ssock = __mptcp_nmpc_socket(msk);
3261cf7da0d6SPeter Krystad 	if (!ssock)
3262cf7da0d6SPeter Krystad 		goto unlock_fail;
3263cf7da0d6SPeter Krystad 
32648a05661bSPaolo Abeni 	clear_bit(MPTCP_DATA_READY, &msk->flags);
3265cf7da0d6SPeter Krystad 	sock_hold(ssock->sk);
3266cf7da0d6SPeter Krystad 	release_sock(sock->sk);
3267cf7da0d6SPeter Krystad 
3268cf7da0d6SPeter Krystad 	err = ssock->ops->accept(sock, newsock, flags, kern);
3269d2f77c53SPaolo Abeni 	if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3270cf7da0d6SPeter Krystad 		struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3271cf7da0d6SPeter Krystad 		struct mptcp_subflow_context *subflow;
32720397c6d8SPaolo Abeni 		struct sock *newsk = newsock->sk;
32730397c6d8SPaolo Abeni 		bool slowpath;
32740397c6d8SPaolo Abeni 
32750397c6d8SPaolo Abeni 		slowpath = lock_sock_fast(newsk);
32765b950ff4SPaolo Abeni 
32775b950ff4SPaolo Abeni 		/* PM/worker can now acquire the first subflow socket
32785b950ff4SPaolo Abeni 		 * lock without racing with listener queue cleanup,
32795b950ff4SPaolo Abeni 		 * we can notify it, if needed.
32805b950ff4SPaolo Abeni 		 */
32815b950ff4SPaolo Abeni 		subflow = mptcp_subflow_ctx(msk->first);
32825b950ff4SPaolo Abeni 		list_add(&subflow->node, &msk->conn_list);
32835b950ff4SPaolo Abeni 		sock_hold(msk->first);
32845b950ff4SPaolo Abeni 		if (mptcp_is_fully_established(newsk))
32855b950ff4SPaolo Abeni 			mptcp_pm_fully_established(msk);
32865b950ff4SPaolo Abeni 
32870397c6d8SPaolo Abeni 		mptcp_copy_inaddrs(newsk, msk->first);
32880397c6d8SPaolo Abeni 		mptcp_rcv_space_init(msk, msk->first);
3289cf7da0d6SPeter Krystad 
3290cf7da0d6SPeter Krystad 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
3291cf7da0d6SPeter Krystad 		 * This is needed so NOSPACE flag can be set from tcp stack.
3292cf7da0d6SPeter Krystad 		 */
3293ec3edaa7SPeter Krystad 		__mptcp_flush_join_list(msk);
3294190f8b06SGeliang Tang 		mptcp_for_each_subflow(msk, subflow) {
3295cf7da0d6SPeter Krystad 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3296cf7da0d6SPeter Krystad 
3297cf7da0d6SPeter Krystad 			if (!ssk->sk_socket)
3298cf7da0d6SPeter Krystad 				mptcp_sock_graft(ssk, newsock);
3299cf7da0d6SPeter Krystad 		}
33000397c6d8SPaolo Abeni 		unlock_sock_fast(newsk, slowpath);
3301cf7da0d6SPeter Krystad 	}
3302cf7da0d6SPeter Krystad 
33038a05661bSPaolo Abeni 	if (inet_csk_listen_poll(ssock->sk))
33048a05661bSPaolo Abeni 		set_bit(MPTCP_DATA_READY, &msk->flags);
3305cf7da0d6SPeter Krystad 	sock_put(ssock->sk);
3306cf7da0d6SPeter Krystad 	return err;
3307cf7da0d6SPeter Krystad 
3308cf7da0d6SPeter Krystad unlock_fail:
3309cf7da0d6SPeter Krystad 	release_sock(sock->sk);
3310cf7da0d6SPeter Krystad 	return -EINVAL;
3311cf7da0d6SPeter Krystad }
3312cf7da0d6SPeter Krystad 
33138a05661bSPaolo Abeni static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
33148a05661bSPaolo Abeni {
33158a05661bSPaolo Abeni 	return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
33168a05661bSPaolo Abeni 	       0;
33178a05661bSPaolo Abeni }
33188a05661bSPaolo Abeni 
33198edf0864SFlorian Westphal static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
33208edf0864SFlorian Westphal {
33218edf0864SFlorian Westphal 	struct sock *sk = (struct sock *)msk;
33228edf0864SFlorian Westphal 
33238edf0864SFlorian Westphal 	if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
33248edf0864SFlorian Westphal 		return 0;
33258edf0864SFlorian Westphal 
33268edf0864SFlorian Westphal 	if (sk_stream_is_writeable(sk))
33278edf0864SFlorian Westphal 		return EPOLLOUT | EPOLLWRNORM;
33288edf0864SFlorian Westphal 
33296e628cd3SPaolo Abeni 	set_bit(MPTCP_NOSPACE, &msk->flags);
33306e628cd3SPaolo Abeni 	smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
33316e628cd3SPaolo Abeni 	if (sk_stream_is_writeable(sk))
33326e628cd3SPaolo Abeni 		return EPOLLOUT | EPOLLWRNORM;
33338edf0864SFlorian Westphal 
33346e628cd3SPaolo Abeni 	return 0;
33358edf0864SFlorian Westphal }
33368edf0864SFlorian Westphal 
33372303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock,
33382303f994SPeter Krystad 			   struct poll_table_struct *wait)
33392303f994SPeter Krystad {
33401891c4a0SFlorian Westphal 	struct sock *sk = sock->sk;
33418ab183deSPaolo Abeni 	struct mptcp_sock *msk;
33422303f994SPeter Krystad 	__poll_t mask = 0;
33438a05661bSPaolo Abeni 	int state;
33442303f994SPeter Krystad 
33451891c4a0SFlorian Westphal 	msk = mptcp_sk(sk);
33461891c4a0SFlorian Westphal 	sock_poll_wait(file, sock, wait);
33471891c4a0SFlorian Westphal 
33488a05661bSPaolo Abeni 	state = inet_sk_state_load(sk);
33496719331cSPaolo Abeni 	pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
33508a05661bSPaolo Abeni 	if (state == TCP_LISTEN)
33518a05661bSPaolo Abeni 		return mptcp_check_readable(msk);
33528a05661bSPaolo Abeni 
33538a05661bSPaolo Abeni 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
33548a05661bSPaolo Abeni 		mask |= mptcp_check_readable(msk);
33558edf0864SFlorian Westphal 		mask |= mptcp_check_writeable(msk);
33568a05661bSPaolo Abeni 	}
33571891c4a0SFlorian Westphal 	if (sk->sk_shutdown & RCV_SHUTDOWN)
33581891c4a0SFlorian Westphal 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
33591891c4a0SFlorian Westphal 
3360*15cc1045SPaolo Abeni 	/* This barrier is coupled with smp_wmb() in tcp_reset() */
3361*15cc1045SPaolo Abeni 	smp_rmb();
3362*15cc1045SPaolo Abeni 	if (sk->sk_err)
3363*15cc1045SPaolo Abeni 		mask |= EPOLLERR;
3364*15cc1045SPaolo Abeni 
33652303f994SPeter Krystad 	return mask;
33662303f994SPeter Krystad }
33672303f994SPeter Krystad 
3368e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = {
3369e42f1ac6SFlorian Westphal 	.family		   = PF_INET,
3370e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
3371e42f1ac6SFlorian Westphal 	.release	   = inet_release,
3372e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
3373e42f1ac6SFlorian Westphal 	.connect	   = mptcp_stream_connect,
3374e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
3375e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
3376d2f77c53SPaolo Abeni 	.getname	   = inet_getname,
3377e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
3378e42f1ac6SFlorian Westphal 	.ioctl		   = inet_ioctl,
3379e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
3380e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
338176e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
3382e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
3383e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
3384e42f1ac6SFlorian Westphal 	.sendmsg	   = inet_sendmsg,
3385e42f1ac6SFlorian Westphal 	.recvmsg	   = inet_recvmsg,
3386e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
3387e42f1ac6SFlorian Westphal 	.sendpage	   = inet_sendpage,
3388e42f1ac6SFlorian Westphal };
33892303f994SPeter Krystad 
3390f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = {
3391f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
3392f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
3393f870fa0bSMat Martineau 	.prot		= &mptcp_prot,
33942303f994SPeter Krystad 	.ops		= &mptcp_stream_ops,
33952303f994SPeter Krystad 	.flags		= INET_PROTOSW_ICSK,
3396f870fa0bSMat Martineau };
3397f870fa0bSMat Martineau 
3398d39dcecaSPaolo Abeni void __init mptcp_proto_init(void)
3399f870fa0bSMat Martineau {
34002303f994SPeter Krystad 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
34012303f994SPeter Krystad 
3402d027236cSPaolo Abeni 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3403d027236cSPaolo Abeni 		panic("Failed to allocate MPTCP pcpu counter\n");
3404d027236cSPaolo Abeni 
34052303f994SPeter Krystad 	mptcp_subflow_init();
34061b1c7a0eSPeter Krystad 	mptcp_pm_init();
34072c5ebd00SPaolo Abeni 	mptcp_token_init();
34082303f994SPeter Krystad 
3409f870fa0bSMat Martineau 	if (proto_register(&mptcp_prot, 1) != 0)
3410f870fa0bSMat Martineau 		panic("Failed to register MPTCP proto.\n");
3411f870fa0bSMat Martineau 
3412f870fa0bSMat Martineau 	inet_register_protosw(&mptcp_protosw);
34136771bfd9SFlorian Westphal 
34146771bfd9SFlorian Westphal 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3415f870fa0bSMat Martineau }
3416f870fa0bSMat Martineau 
3417f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3418e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = {
3419e42f1ac6SFlorian Westphal 	.family		   = PF_INET6,
3420e42f1ac6SFlorian Westphal 	.owner		   = THIS_MODULE,
3421e42f1ac6SFlorian Westphal 	.release	   = inet6_release,
3422e42f1ac6SFlorian Westphal 	.bind		   = mptcp_bind,
3423e42f1ac6SFlorian Westphal 	.connect	   = mptcp_stream_connect,
3424e42f1ac6SFlorian Westphal 	.socketpair	   = sock_no_socketpair,
3425e42f1ac6SFlorian Westphal 	.accept		   = mptcp_stream_accept,
3426d2f77c53SPaolo Abeni 	.getname	   = inet6_getname,
3427e42f1ac6SFlorian Westphal 	.poll		   = mptcp_poll,
3428e42f1ac6SFlorian Westphal 	.ioctl		   = inet6_ioctl,
3429e42f1ac6SFlorian Westphal 	.gettstamp	   = sock_gettstamp,
3430e42f1ac6SFlorian Westphal 	.listen		   = mptcp_listen,
343176e2a55dSPaolo Abeni 	.shutdown	   = inet_shutdown,
3432e42f1ac6SFlorian Westphal 	.setsockopt	   = sock_common_setsockopt,
3433e42f1ac6SFlorian Westphal 	.getsockopt	   = sock_common_getsockopt,
3434e42f1ac6SFlorian Westphal 	.sendmsg	   = inet6_sendmsg,
3435e42f1ac6SFlorian Westphal 	.recvmsg	   = inet6_recvmsg,
3436e42f1ac6SFlorian Westphal 	.mmap		   = sock_no_mmap,
3437e42f1ac6SFlorian Westphal 	.sendpage	   = inet_sendpage,
3438e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT
34393986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
3440e42f1ac6SFlorian Westphal #endif
3441e42f1ac6SFlorian Westphal };
3442e42f1ac6SFlorian Westphal 
3443f870fa0bSMat Martineau static struct proto mptcp_v6_prot;
3444f870fa0bSMat Martineau 
344579c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk)
344679c0949eSPeter Krystad {
344779c0949eSPeter Krystad 	mptcp_destroy(sk);
344879c0949eSPeter Krystad 	inet6_destroy_sock(sk);
344979c0949eSPeter Krystad }
345079c0949eSPeter Krystad 
3451f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = {
3452f870fa0bSMat Martineau 	.type		= SOCK_STREAM,
3453f870fa0bSMat Martineau 	.protocol	= IPPROTO_MPTCP,
3454f870fa0bSMat Martineau 	.prot		= &mptcp_v6_prot,
34552303f994SPeter Krystad 	.ops		= &mptcp_v6_stream_ops,
3456f870fa0bSMat Martineau 	.flags		= INET_PROTOSW_ICSK,
3457f870fa0bSMat Martineau };
3458f870fa0bSMat Martineau 
3459d39dcecaSPaolo Abeni int __init mptcp_proto_v6_init(void)
3460f870fa0bSMat Martineau {
3461f870fa0bSMat Martineau 	int err;
3462f870fa0bSMat Martineau 
3463f870fa0bSMat Martineau 	mptcp_v6_prot = mptcp_prot;
3464f870fa0bSMat Martineau 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
3465f870fa0bSMat Martineau 	mptcp_v6_prot.slab = NULL;
346679c0949eSPeter Krystad 	mptcp_v6_prot.destroy = mptcp_v6_destroy;
3467b0519de8SFlorian Westphal 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3468f870fa0bSMat Martineau 
3469f870fa0bSMat Martineau 	err = proto_register(&mptcp_v6_prot, 1);
3470f870fa0bSMat Martineau 	if (err)
3471f870fa0bSMat Martineau 		return err;
3472f870fa0bSMat Martineau 
3473f870fa0bSMat Martineau 	err = inet6_register_protosw(&mptcp_v6_protosw);
3474f870fa0bSMat Martineau 	if (err)
3475f870fa0bSMat Martineau 		proto_unregister(&mptcp_v6_prot);
3476f870fa0bSMat Martineau 
3477f870fa0bSMat Martineau 	return err;
3478f870fa0bSMat Martineau }
3479f870fa0bSMat Martineau #endif
3480