xref: /linux/net/mptcp/protocol.c (revision 2fe3c78a2c26dd5ee811024a1b7d6cfb4d654319)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2017 - 2019, Intel Corporation.
5  */
6 
7 #define pr_fmt(fmt) "MPTCP: " fmt
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sched/signal.h>
13 #include <linux/atomic.h>
14 #include <net/sock.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
18 #include <net/tcp_states.h>
19 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/transp_v6.h>
21 #endif
22 #include <net/mptcp.h>
23 #include <net/hotdata.h>
24 #include <net/xfrm.h>
25 #include <asm/ioctls.h>
26 #include "protocol.h"
27 #include "mib.h"
28 
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
31 
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33 struct mptcp6_sock {
34 	struct mptcp_sock msk;
35 	struct ipv6_pinfo np;
36 };
37 #endif
38 
39 enum {
40 	MPTCP_CMSG_TS = BIT(0),
41 	MPTCP_CMSG_INQ = BIT(1),
42 };
43 
44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
45 
46 static void __mptcp_destroy_sock(struct sock *sk);
47 static void mptcp_check_send_data_fin(struct sock *sk);
48 
49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 static struct net_device mptcp_napi_dev;
51 
52 /* Returns end sequence number of the receiver's advertised window */
53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
54 {
55 	return READ_ONCE(msk->wnd_end);
56 }
57 
58 static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
59 {
60 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
61 	if (sk->sk_prot == &tcpv6_prot)
62 		return &inet6_stream_ops;
63 #endif
64 	WARN_ON_ONCE(sk->sk_prot != &tcp_prot);
65 	return &inet_stream_ops;
66 }
67 
68 static int __mptcp_socket_create(struct mptcp_sock *msk)
69 {
70 	struct mptcp_subflow_context *subflow;
71 	struct sock *sk = (struct sock *)msk;
72 	struct socket *ssock;
73 	int err;
74 
75 	err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
76 	if (err)
77 		return err;
78 
79 	msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
80 	WRITE_ONCE(msk->first, ssock->sk);
81 	subflow = mptcp_subflow_ctx(ssock->sk);
82 	list_add(&subflow->node, &msk->conn_list);
83 	sock_hold(ssock->sk);
84 	subflow->request_mptcp = 1;
85 	subflow->subflow_id = msk->subflow_id++;
86 
87 	/* This is the first subflow, always with id 0 */
88 	WRITE_ONCE(subflow->local_id, 0);
89 	mptcp_sock_graft(msk->first, sk->sk_socket);
90 	iput(SOCK_INODE(ssock));
91 
92 	return 0;
93 }
94 
95 /* If the MPC handshake is not started, returns the first subflow,
96  * eventually allocating it.
97  */
98 struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
99 {
100 	struct sock *sk = (struct sock *)msk;
101 	int ret;
102 
103 	if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
104 		return ERR_PTR(-EINVAL);
105 
106 	if (!msk->first) {
107 		ret = __mptcp_socket_create(msk);
108 		if (ret)
109 			return ERR_PTR(ret);
110 	}
111 
112 	return msk->first;
113 }
114 
115 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
116 {
117 	sk_drops_add(sk, skb);
118 	__kfree_skb(skb);
119 }
120 
121 static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size)
122 {
123 	WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc,
124 		   mptcp_sk(sk)->rmem_fwd_alloc + size);
125 }
126 
127 static void mptcp_rmem_charge(struct sock *sk, int size)
128 {
129 	mptcp_rmem_fwd_alloc_add(sk, -size);
130 }
131 
132 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
133 			       struct sk_buff *from)
134 {
135 	bool fragstolen;
136 	int delta;
137 
138 	if (MPTCP_SKB_CB(from)->offset ||
139 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
140 		return false;
141 
142 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
143 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
144 		 to->len, MPTCP_SKB_CB(from)->end_seq);
145 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
146 
147 	/* note the fwd memory can reach a negative value after accounting
148 	 * for the delta, but the later skb free will restore a non
149 	 * negative one
150 	 */
151 	atomic_add(delta, &sk->sk_rmem_alloc);
152 	mptcp_rmem_charge(sk, delta);
153 	kfree_skb_partial(from, fragstolen);
154 
155 	return true;
156 }
157 
158 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
159 				   struct sk_buff *from)
160 {
161 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
162 		return false;
163 
164 	return mptcp_try_coalesce((struct sock *)msk, to, from);
165 }
166 
167 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
168 {
169 	amount >>= PAGE_SHIFT;
170 	mptcp_rmem_charge(sk, amount << PAGE_SHIFT);
171 	__sk_mem_reduce_allocated(sk, amount);
172 }
173 
174 static void mptcp_rmem_uncharge(struct sock *sk, int size)
175 {
176 	struct mptcp_sock *msk = mptcp_sk(sk);
177 	int reclaimable;
178 
179 	mptcp_rmem_fwd_alloc_add(sk, size);
180 	reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
181 
182 	/* see sk_mem_uncharge() for the rationale behind the following schema */
183 	if (unlikely(reclaimable >= PAGE_SIZE))
184 		__mptcp_rmem_reclaim(sk, reclaimable);
185 }
186 
187 static void mptcp_rfree(struct sk_buff *skb)
188 {
189 	unsigned int len = skb->truesize;
190 	struct sock *sk = skb->sk;
191 
192 	atomic_sub(len, &sk->sk_rmem_alloc);
193 	mptcp_rmem_uncharge(sk, len);
194 }
195 
196 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
197 {
198 	skb_orphan(skb);
199 	skb->sk = sk;
200 	skb->destructor = mptcp_rfree;
201 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
202 	mptcp_rmem_charge(sk, skb->truesize);
203 }
204 
205 /* "inspired" by tcp_data_queue_ofo(), main differences:
206  * - use mptcp seqs
207  * - don't cope with sacks
208  */
209 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
210 {
211 	struct sock *sk = (struct sock *)msk;
212 	struct rb_node **p, *parent;
213 	u64 seq, end_seq, max_seq;
214 	struct sk_buff *skb1;
215 
216 	seq = MPTCP_SKB_CB(skb)->map_seq;
217 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
218 	max_seq = atomic64_read(&msk->rcv_wnd_sent);
219 
220 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
221 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
222 	if (after64(end_seq, max_seq)) {
223 		/* out of window */
224 		mptcp_drop(sk, skb);
225 		pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
226 			 (unsigned long long)end_seq - (unsigned long)max_seq,
227 			 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
228 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
229 		return;
230 	}
231 
232 	p = &msk->out_of_order_queue.rb_node;
233 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
234 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
235 		rb_link_node(&skb->rbnode, NULL, p);
236 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
237 		msk->ooo_last_skb = skb;
238 		goto end;
239 	}
240 
241 	/* with 2 subflows, adding at end of ooo queue is quite likely
242 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
243 	 */
244 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
245 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
246 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
247 		return;
248 	}
249 
250 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
251 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
252 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
253 		parent = &msk->ooo_last_skb->rbnode;
254 		p = &parent->rb_right;
255 		goto insert;
256 	}
257 
258 	/* Find place to insert this segment. Handle overlaps on the way. */
259 	parent = NULL;
260 	while (*p) {
261 		parent = *p;
262 		skb1 = rb_to_skb(parent);
263 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
264 			p = &parent->rb_left;
265 			continue;
266 		}
267 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
268 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
269 				/* All the bits are present. Drop. */
270 				mptcp_drop(sk, skb);
271 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
272 				return;
273 			}
274 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
275 				/* partial overlap:
276 				 *     |     skb      |
277 				 *  |     skb1    |
278 				 * continue traversing
279 				 */
280 			} else {
281 				/* skb's seq == skb1's seq and skb covers skb1.
282 				 * Replace skb1 with skb.
283 				 */
284 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
285 						&msk->out_of_order_queue);
286 				mptcp_drop(sk, skb1);
287 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
288 				goto merge_right;
289 			}
290 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
291 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
292 			return;
293 		}
294 		p = &parent->rb_right;
295 	}
296 
297 insert:
298 	/* Insert segment into RB tree. */
299 	rb_link_node(&skb->rbnode, parent, p);
300 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
301 
302 merge_right:
303 	/* Remove other segments covered by skb. */
304 	while ((skb1 = skb_rb_next(skb)) != NULL) {
305 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
306 			break;
307 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
308 		mptcp_drop(sk, skb1);
309 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
310 	}
311 	/* If there is no skb after us, we are the last_skb ! */
312 	if (!skb1)
313 		msk->ooo_last_skb = skb;
314 
315 end:
316 	skb_condense(skb);
317 	mptcp_set_owner_r(skb, sk);
318 }
319 
320 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
321 {
322 	struct mptcp_sock *msk = mptcp_sk(sk);
323 	int amt, amount;
324 
325 	if (size <= msk->rmem_fwd_alloc)
326 		return true;
327 
328 	size -= msk->rmem_fwd_alloc;
329 	amt = sk_mem_pages(size);
330 	amount = amt << PAGE_SHIFT;
331 	if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
332 		return false;
333 
334 	mptcp_rmem_fwd_alloc_add(sk, amount);
335 	return true;
336 }
337 
338 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
339 			     struct sk_buff *skb, unsigned int offset,
340 			     size_t copy_len)
341 {
342 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
343 	struct sock *sk = (struct sock *)msk;
344 	struct sk_buff *tail;
345 	bool has_rxtstamp;
346 
347 	__skb_unlink(skb, &ssk->sk_receive_queue);
348 
349 	skb_ext_reset(skb);
350 	skb_orphan(skb);
351 
352 	/* try to fetch required memory from subflow */
353 	if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) {
354 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
355 		goto drop;
356 	}
357 
358 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
359 
360 	/* the skb map_seq accounts for the skb offset:
361 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
362 	 * value
363 	 */
364 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
365 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
366 	MPTCP_SKB_CB(skb)->offset = offset;
367 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
368 
369 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
370 		/* in sequence */
371 		msk->bytes_received += copy_len;
372 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
373 		tail = skb_peek_tail(&sk->sk_receive_queue);
374 		if (tail && mptcp_try_coalesce(sk, tail, skb))
375 			return true;
376 
377 		mptcp_set_owner_r(skb, sk);
378 		__skb_queue_tail(&sk->sk_receive_queue, skb);
379 		return true;
380 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
381 		mptcp_data_queue_ofo(msk, skb);
382 		return false;
383 	}
384 
385 	/* old data, keep it simple and drop the whole pkt, sender
386 	 * will retransmit as needed, if needed.
387 	 */
388 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
389 drop:
390 	mptcp_drop(sk, skb);
391 	return false;
392 }
393 
394 static void mptcp_stop_rtx_timer(struct sock *sk)
395 {
396 	struct inet_connection_sock *icsk = inet_csk(sk);
397 
398 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
399 	mptcp_sk(sk)->timer_ival = 0;
400 }
401 
402 static void mptcp_close_wake_up(struct sock *sk)
403 {
404 	if (sock_flag(sk, SOCK_DEAD))
405 		return;
406 
407 	sk->sk_state_change(sk);
408 	if (sk->sk_shutdown == SHUTDOWN_MASK ||
409 	    sk->sk_state == TCP_CLOSE)
410 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
411 	else
412 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
413 }
414 
415 /* called under the msk socket lock */
416 static bool mptcp_pending_data_fin_ack(struct sock *sk)
417 {
418 	struct mptcp_sock *msk = mptcp_sk(sk);
419 
420 	return ((1 << sk->sk_state) &
421 		(TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
422 	       msk->write_seq == READ_ONCE(msk->snd_una);
423 }
424 
425 static void mptcp_check_data_fin_ack(struct sock *sk)
426 {
427 	struct mptcp_sock *msk = mptcp_sk(sk);
428 
429 	/* Look for an acknowledged DATA_FIN */
430 	if (mptcp_pending_data_fin_ack(sk)) {
431 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
432 
433 		switch (sk->sk_state) {
434 		case TCP_FIN_WAIT1:
435 			mptcp_set_state(sk, TCP_FIN_WAIT2);
436 			break;
437 		case TCP_CLOSING:
438 		case TCP_LAST_ACK:
439 			mptcp_set_state(sk, TCP_CLOSE);
440 			break;
441 		}
442 
443 		mptcp_close_wake_up(sk);
444 	}
445 }
446 
447 /* can be called with no lock acquired */
448 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
449 {
450 	struct mptcp_sock *msk = mptcp_sk(sk);
451 
452 	if (READ_ONCE(msk->rcv_data_fin) &&
453 	    ((1 << inet_sk_state_load(sk)) &
454 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
455 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
456 
457 		if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
458 			if (seq)
459 				*seq = rcv_data_fin_seq;
460 
461 			return true;
462 		}
463 	}
464 
465 	return false;
466 }
467 
468 static void mptcp_set_datafin_timeout(struct sock *sk)
469 {
470 	struct inet_connection_sock *icsk = inet_csk(sk);
471 	u32 retransmits;
472 
473 	retransmits = min_t(u32, icsk->icsk_retransmits,
474 			    ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
475 
476 	mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
477 }
478 
479 static void __mptcp_set_timeout(struct sock *sk, long tout)
480 {
481 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
482 }
483 
484 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
485 {
486 	const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
487 
488 	return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
489 	       inet_csk(ssk)->icsk_timeout - jiffies : 0;
490 }
491 
492 static void mptcp_set_timeout(struct sock *sk)
493 {
494 	struct mptcp_subflow_context *subflow;
495 	long tout = 0;
496 
497 	mptcp_for_each_subflow(mptcp_sk(sk), subflow)
498 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
499 	__mptcp_set_timeout(sk, tout);
500 }
501 
502 static inline bool tcp_can_send_ack(const struct sock *ssk)
503 {
504 	return !((1 << inet_sk_state_load(ssk)) &
505 	       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
506 }
507 
508 void __mptcp_subflow_send_ack(struct sock *ssk)
509 {
510 	if (tcp_can_send_ack(ssk))
511 		tcp_send_ack(ssk);
512 }
513 
514 static void mptcp_subflow_send_ack(struct sock *ssk)
515 {
516 	bool slow;
517 
518 	slow = lock_sock_fast(ssk);
519 	__mptcp_subflow_send_ack(ssk);
520 	unlock_sock_fast(ssk, slow);
521 }
522 
523 static void mptcp_send_ack(struct mptcp_sock *msk)
524 {
525 	struct mptcp_subflow_context *subflow;
526 
527 	mptcp_for_each_subflow(msk, subflow)
528 		mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
529 }
530 
531 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
532 {
533 	bool slow;
534 
535 	slow = lock_sock_fast(ssk);
536 	if (tcp_can_send_ack(ssk))
537 		tcp_cleanup_rbuf(ssk, 1);
538 	unlock_sock_fast(ssk, slow);
539 }
540 
541 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
542 {
543 	const struct inet_connection_sock *icsk = inet_csk(ssk);
544 	u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
545 	const struct tcp_sock *tp = tcp_sk(ssk);
546 
547 	return (ack_pending & ICSK_ACK_SCHED) &&
548 		((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
549 		  READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
550 		 (rx_empty && ack_pending &
551 			      (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
552 }
553 
554 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
555 {
556 	int old_space = READ_ONCE(msk->old_wspace);
557 	struct mptcp_subflow_context *subflow;
558 	struct sock *sk = (struct sock *)msk;
559 	int space =  __mptcp_space(sk);
560 	bool cleanup, rx_empty;
561 
562 	cleanup = (space > 0) && (space >= (old_space << 1));
563 	rx_empty = !__mptcp_rmem(sk);
564 
565 	mptcp_for_each_subflow(msk, subflow) {
566 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
567 
568 		if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
569 			mptcp_subflow_cleanup_rbuf(ssk);
570 	}
571 }
572 
573 static bool mptcp_check_data_fin(struct sock *sk)
574 {
575 	struct mptcp_sock *msk = mptcp_sk(sk);
576 	u64 rcv_data_fin_seq;
577 	bool ret = false;
578 
579 	/* Need to ack a DATA_FIN received from a peer while this side
580 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
581 	 * msk->rcv_data_fin was set when parsing the incoming options
582 	 * at the subflow level and the msk lock was not held, so this
583 	 * is the first opportunity to act on the DATA_FIN and change
584 	 * the msk state.
585 	 *
586 	 * If we are caught up to the sequence number of the incoming
587 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
588 	 * not caught up, do nothing and let the recv code send DATA_ACK
589 	 * when catching up.
590 	 */
591 
592 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
593 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
594 		WRITE_ONCE(msk->rcv_data_fin, 0);
595 
596 		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
597 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
598 
599 		switch (sk->sk_state) {
600 		case TCP_ESTABLISHED:
601 			mptcp_set_state(sk, TCP_CLOSE_WAIT);
602 			break;
603 		case TCP_FIN_WAIT1:
604 			mptcp_set_state(sk, TCP_CLOSING);
605 			break;
606 		case TCP_FIN_WAIT2:
607 			mptcp_set_state(sk, TCP_CLOSE);
608 			break;
609 		default:
610 			/* Other states not expected */
611 			WARN_ON_ONCE(1);
612 			break;
613 		}
614 
615 		ret = true;
616 		if (!__mptcp_check_fallback(msk))
617 			mptcp_send_ack(msk);
618 		mptcp_close_wake_up(sk);
619 	}
620 	return ret;
621 }
622 
623 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
624 					   struct sock *ssk,
625 					   unsigned int *bytes)
626 {
627 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
628 	struct sock *sk = (struct sock *)msk;
629 	unsigned int moved = 0;
630 	bool more_data_avail;
631 	struct tcp_sock *tp;
632 	bool done = false;
633 	int sk_rbuf;
634 
635 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
636 
637 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
638 		int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
639 
640 		if (unlikely(ssk_rbuf > sk_rbuf)) {
641 			WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
642 			sk_rbuf = ssk_rbuf;
643 		}
644 	}
645 
646 	pr_debug("msk=%p ssk=%p\n", msk, ssk);
647 	tp = tcp_sk(ssk);
648 	do {
649 		u32 map_remaining, offset;
650 		u32 seq = tp->copied_seq;
651 		struct sk_buff *skb;
652 		bool fin;
653 
654 		/* try to move as much data as available */
655 		map_remaining = subflow->map_data_len -
656 				mptcp_subflow_get_map_offset(subflow);
657 
658 		skb = skb_peek(&ssk->sk_receive_queue);
659 		if (!skb) {
660 			/* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
661 			 * a different CPU can have already processed the pending
662 			 * data, stop here or we can enter an infinite loop
663 			 */
664 			if (!moved)
665 				done = true;
666 			break;
667 		}
668 
669 		if (__mptcp_check_fallback(msk)) {
670 			/* Under fallback skbs have no MPTCP extension and TCP could
671 			 * collapse them between the dummy map creation and the
672 			 * current dequeue. Be sure to adjust the map size.
673 			 */
674 			map_remaining = skb->len;
675 			subflow->map_data_len = skb->len;
676 		}
677 
678 		offset = seq - TCP_SKB_CB(skb)->seq;
679 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
680 		if (fin) {
681 			done = true;
682 			seq++;
683 		}
684 
685 		if (offset < skb->len) {
686 			size_t len = skb->len - offset;
687 
688 			if (tp->urg_data)
689 				done = true;
690 
691 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
692 				moved += len;
693 			seq += len;
694 
695 			if (WARN_ON_ONCE(map_remaining < len))
696 				break;
697 		} else {
698 			WARN_ON_ONCE(!fin);
699 			sk_eat_skb(ssk, skb);
700 			done = true;
701 		}
702 
703 		WRITE_ONCE(tp->copied_seq, seq);
704 		more_data_avail = mptcp_subflow_data_available(ssk);
705 
706 		if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
707 			done = true;
708 			break;
709 		}
710 	} while (more_data_avail);
711 
712 	if (moved > 0)
713 		msk->last_data_recv = tcp_jiffies32;
714 	*bytes += moved;
715 	return done;
716 }
717 
718 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
719 {
720 	struct sock *sk = (struct sock *)msk;
721 	struct sk_buff *skb, *tail;
722 	bool moved = false;
723 	struct rb_node *p;
724 	u64 end_seq;
725 
726 	p = rb_first(&msk->out_of_order_queue);
727 	pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
728 	while (p) {
729 		skb = rb_to_skb(p);
730 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
731 			break;
732 
733 		p = rb_next(p);
734 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
735 
736 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
737 				      msk->ack_seq))) {
738 			mptcp_drop(sk, skb);
739 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
740 			continue;
741 		}
742 
743 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
744 		tail = skb_peek_tail(&sk->sk_receive_queue);
745 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
746 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
747 
748 			/* skip overlapping data, if any */
749 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
750 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
751 				 delta);
752 			MPTCP_SKB_CB(skb)->offset += delta;
753 			MPTCP_SKB_CB(skb)->map_seq += delta;
754 			__skb_queue_tail(&sk->sk_receive_queue, skb);
755 		}
756 		msk->bytes_received += end_seq - msk->ack_seq;
757 		WRITE_ONCE(msk->ack_seq, end_seq);
758 		moved = true;
759 	}
760 	return moved;
761 }
762 
763 static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
764 {
765 	int err = sock_error(ssk);
766 	int ssk_state;
767 
768 	if (!err)
769 		return false;
770 
771 	/* only propagate errors on fallen-back sockets or
772 	 * on MPC connect
773 	 */
774 	if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
775 		return false;
776 
777 	/* We need to propagate only transition to CLOSE state.
778 	 * Orphaned socket will see such state change via
779 	 * subflow_sched_work_if_closed() and that path will properly
780 	 * destroy the msk as needed.
781 	 */
782 	ssk_state = inet_sk_state_load(ssk);
783 	if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
784 		mptcp_set_state(sk, ssk_state);
785 	WRITE_ONCE(sk->sk_err, -err);
786 
787 	/* This barrier is coupled with smp_rmb() in mptcp_poll() */
788 	smp_wmb();
789 	sk_error_report(sk);
790 	return true;
791 }
792 
793 void __mptcp_error_report(struct sock *sk)
794 {
795 	struct mptcp_subflow_context *subflow;
796 	struct mptcp_sock *msk = mptcp_sk(sk);
797 
798 	mptcp_for_each_subflow(msk, subflow)
799 		if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
800 			break;
801 }
802 
803 /* In most cases we will be able to lock the mptcp socket.  If its already
804  * owned, we need to defer to the work queue to avoid ABBA deadlock.
805  */
806 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
807 {
808 	struct sock *sk = (struct sock *)msk;
809 	unsigned int moved = 0;
810 
811 	__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
812 	__mptcp_ofo_queue(msk);
813 	if (unlikely(ssk->sk_err)) {
814 		if (!sock_owned_by_user(sk))
815 			__mptcp_error_report(sk);
816 		else
817 			__set_bit(MPTCP_ERROR_REPORT,  &msk->cb_flags);
818 	}
819 
820 	/* If the moves have caught up with the DATA_FIN sequence number
821 	 * it's time to ack the DATA_FIN and change socket state, but
822 	 * this is not a good place to change state. Let the workqueue
823 	 * do it.
824 	 */
825 	if (mptcp_pending_data_fin(sk, NULL))
826 		mptcp_schedule_work(sk);
827 	return moved > 0;
828 }
829 
830 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
831 {
832 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
833 	struct mptcp_sock *msk = mptcp_sk(sk);
834 	int sk_rbuf, ssk_rbuf;
835 
836 	/* The peer can send data while we are shutting down this
837 	 * subflow at msk destruction time, but we must avoid enqueuing
838 	 * more data to the msk receive queue
839 	 */
840 	if (unlikely(subflow->disposable))
841 		return;
842 
843 	ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
844 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
845 	if (unlikely(ssk_rbuf > sk_rbuf))
846 		sk_rbuf = ssk_rbuf;
847 
848 	/* over limit? can't append more skbs to msk, Also, no need to wake-up*/
849 	if (__mptcp_rmem(sk) > sk_rbuf)
850 		return;
851 
852 	/* Wake-up the reader only for in-sequence data */
853 	mptcp_data_lock(sk);
854 	if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
855 		sk->sk_data_ready(sk);
856 	mptcp_data_unlock(sk);
857 }
858 
859 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
860 {
861 	mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
862 	WRITE_ONCE(msk->allow_infinite_fallback, false);
863 	mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
864 }
865 
866 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
867 {
868 	struct sock *sk = (struct sock *)msk;
869 
870 	if (sk->sk_state != TCP_ESTABLISHED)
871 		return false;
872 
873 	/* attach to msk socket only after we are sure we will deal with it
874 	 * at close time
875 	 */
876 	if (sk->sk_socket && !ssk->sk_socket)
877 		mptcp_sock_graft(ssk, sk->sk_socket);
878 
879 	mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
880 	mptcp_sockopt_sync_locked(msk, ssk);
881 	mptcp_subflow_joined(msk, ssk);
882 	mptcp_stop_tout_timer(sk);
883 	__mptcp_propagate_sndbuf(sk, ssk);
884 	return true;
885 }
886 
887 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
888 {
889 	struct mptcp_subflow_context *tmp, *subflow;
890 	struct mptcp_sock *msk = mptcp_sk(sk);
891 
892 	list_for_each_entry_safe(subflow, tmp, join_list, node) {
893 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
894 		bool slow = lock_sock_fast(ssk);
895 
896 		list_move_tail(&subflow->node, &msk->conn_list);
897 		if (!__mptcp_finish_join(msk, ssk))
898 			mptcp_subflow_reset(ssk);
899 		unlock_sock_fast(ssk, slow);
900 	}
901 }
902 
903 static bool mptcp_rtx_timer_pending(struct sock *sk)
904 {
905 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
906 }
907 
908 static void mptcp_reset_rtx_timer(struct sock *sk)
909 {
910 	struct inet_connection_sock *icsk = inet_csk(sk);
911 	unsigned long tout;
912 
913 	/* prevent rescheduling on close */
914 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
915 		return;
916 
917 	tout = mptcp_sk(sk)->timer_ival;
918 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
919 }
920 
921 bool mptcp_schedule_work(struct sock *sk)
922 {
923 	if (inet_sk_state_load(sk) != TCP_CLOSE &&
924 	    schedule_work(&mptcp_sk(sk)->work)) {
925 		/* each subflow already holds a reference to the sk, and the
926 		 * workqueue is invoked by a subflow, so sk can't go away here.
927 		 */
928 		sock_hold(sk);
929 		return true;
930 	}
931 	return false;
932 }
933 
934 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
935 {
936 	struct mptcp_subflow_context *subflow;
937 
938 	msk_owned_by_me(msk);
939 
940 	mptcp_for_each_subflow(msk, subflow) {
941 		if (READ_ONCE(subflow->data_avail))
942 			return mptcp_subflow_tcp_sock(subflow);
943 	}
944 
945 	return NULL;
946 }
947 
948 static bool mptcp_skb_can_collapse_to(u64 write_seq,
949 				      const struct sk_buff *skb,
950 				      const struct mptcp_ext *mpext)
951 {
952 	if (!tcp_skb_can_collapse_to(skb))
953 		return false;
954 
955 	/* can collapse only if MPTCP level sequence is in order and this
956 	 * mapping has not been xmitted yet
957 	 */
958 	return mpext && mpext->data_seq + mpext->data_len == write_seq &&
959 	       !mpext->frozen;
960 }
961 
962 /* we can append data to the given data frag if:
963  * - there is space available in the backing page_frag
964  * - the data frag tail matches the current page_frag free offset
965  * - the data frag end sequence number matches the current write seq
966  */
967 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
968 				       const struct page_frag *pfrag,
969 				       const struct mptcp_data_frag *df)
970 {
971 	return df && pfrag->page == df->page &&
972 		pfrag->size - pfrag->offset > 0 &&
973 		pfrag->offset == (df->offset + df->data_len) &&
974 		df->data_seq + df->data_len == msk->write_seq;
975 }
976 
977 static void dfrag_uncharge(struct sock *sk, int len)
978 {
979 	sk_mem_uncharge(sk, len);
980 	sk_wmem_queued_add(sk, -len);
981 }
982 
983 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
984 {
985 	int len = dfrag->data_len + dfrag->overhead;
986 
987 	list_del(&dfrag->list);
988 	dfrag_uncharge(sk, len);
989 	put_page(dfrag->page);
990 }
991 
992 /* called under both the msk socket lock and the data lock */
993 static void __mptcp_clean_una(struct sock *sk)
994 {
995 	struct mptcp_sock *msk = mptcp_sk(sk);
996 	struct mptcp_data_frag *dtmp, *dfrag;
997 	u64 snd_una;
998 
999 	snd_una = msk->snd_una;
1000 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1001 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1002 			break;
1003 
1004 		if (unlikely(dfrag == msk->first_pending)) {
1005 			/* in recovery mode can see ack after the current snd head */
1006 			if (WARN_ON_ONCE(!msk->recovery))
1007 				break;
1008 
1009 			WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1010 		}
1011 
1012 		dfrag_clear(sk, dfrag);
1013 	}
1014 
1015 	dfrag = mptcp_rtx_head(sk);
1016 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
1017 		u64 delta = snd_una - dfrag->data_seq;
1018 
1019 		/* prevent wrap around in recovery mode */
1020 		if (unlikely(delta > dfrag->already_sent)) {
1021 			if (WARN_ON_ONCE(!msk->recovery))
1022 				goto out;
1023 			if (WARN_ON_ONCE(delta > dfrag->data_len))
1024 				goto out;
1025 			dfrag->already_sent += delta - dfrag->already_sent;
1026 		}
1027 
1028 		dfrag->data_seq += delta;
1029 		dfrag->offset += delta;
1030 		dfrag->data_len -= delta;
1031 		dfrag->already_sent -= delta;
1032 
1033 		dfrag_uncharge(sk, delta);
1034 	}
1035 
1036 	/* all retransmitted data acked, recovery completed */
1037 	if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1038 		msk->recovery = false;
1039 
1040 out:
1041 	if (snd_una == msk->snd_nxt && snd_una == msk->write_seq) {
1042 		if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1043 			mptcp_stop_rtx_timer(sk);
1044 	} else {
1045 		mptcp_reset_rtx_timer(sk);
1046 	}
1047 
1048 	if (mptcp_pending_data_fin_ack(sk))
1049 		mptcp_schedule_work(sk);
1050 }
1051 
1052 static void __mptcp_clean_una_wakeup(struct sock *sk)
1053 {
1054 	lockdep_assert_held_once(&sk->sk_lock.slock);
1055 
1056 	__mptcp_clean_una(sk);
1057 	mptcp_write_space(sk);
1058 }
1059 
1060 static void mptcp_clean_una_wakeup(struct sock *sk)
1061 {
1062 	mptcp_data_lock(sk);
1063 	__mptcp_clean_una_wakeup(sk);
1064 	mptcp_data_unlock(sk);
1065 }
1066 
1067 static void mptcp_enter_memory_pressure(struct sock *sk)
1068 {
1069 	struct mptcp_subflow_context *subflow;
1070 	struct mptcp_sock *msk = mptcp_sk(sk);
1071 	bool first = true;
1072 
1073 	mptcp_for_each_subflow(msk, subflow) {
1074 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1075 
1076 		if (first)
1077 			tcp_enter_memory_pressure(ssk);
1078 		sk_stream_moderate_sndbuf(ssk);
1079 
1080 		first = false;
1081 	}
1082 	__mptcp_sync_sndbuf(sk);
1083 }
1084 
1085 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1086  * data
1087  */
1088 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1089 {
1090 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1091 					pfrag, sk->sk_allocation)))
1092 		return true;
1093 
1094 	mptcp_enter_memory_pressure(sk);
1095 	return false;
1096 }
1097 
1098 static struct mptcp_data_frag *
1099 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1100 		      int orig_offset)
1101 {
1102 	int offset = ALIGN(orig_offset, sizeof(long));
1103 	struct mptcp_data_frag *dfrag;
1104 
1105 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1106 	dfrag->data_len = 0;
1107 	dfrag->data_seq = msk->write_seq;
1108 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1109 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1110 	dfrag->already_sent = 0;
1111 	dfrag->page = pfrag->page;
1112 
1113 	return dfrag;
1114 }
1115 
1116 struct mptcp_sendmsg_info {
1117 	int mss_now;
1118 	int size_goal;
1119 	u16 limit;
1120 	u16 sent;
1121 	unsigned int flags;
1122 	bool data_lock_held;
1123 };
1124 
1125 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1126 				    u64 data_seq, int avail_size)
1127 {
1128 	u64 window_end = mptcp_wnd_end(msk);
1129 	u64 mptcp_snd_wnd;
1130 
1131 	if (__mptcp_check_fallback(msk))
1132 		return avail_size;
1133 
1134 	mptcp_snd_wnd = window_end - data_seq;
1135 	avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1136 
1137 	if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1138 		tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1139 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1140 	}
1141 
1142 	return avail_size;
1143 }
1144 
1145 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1146 {
1147 	struct skb_ext *mpext = __skb_ext_alloc(gfp);
1148 
1149 	if (!mpext)
1150 		return false;
1151 	__skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1152 	return true;
1153 }
1154 
1155 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1156 {
1157 	struct sk_buff *skb;
1158 
1159 	skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1160 	if (likely(skb)) {
1161 		if (likely(__mptcp_add_ext(skb, gfp))) {
1162 			skb_reserve(skb, MAX_TCP_HEADER);
1163 			skb->ip_summed = CHECKSUM_PARTIAL;
1164 			INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1165 			return skb;
1166 		}
1167 		__kfree_skb(skb);
1168 	} else {
1169 		mptcp_enter_memory_pressure(sk);
1170 	}
1171 	return NULL;
1172 }
1173 
1174 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1175 {
1176 	struct sk_buff *skb;
1177 
1178 	skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1179 	if (!skb)
1180 		return NULL;
1181 
1182 	if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1183 		tcp_skb_entail(ssk, skb);
1184 		return skb;
1185 	}
1186 	tcp_skb_tsorted_anchor_cleanup(skb);
1187 	kfree_skb(skb);
1188 	return NULL;
1189 }
1190 
1191 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1192 {
1193 	gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1194 
1195 	return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1196 }
1197 
1198 /* note: this always recompute the csum on the whole skb, even
1199  * if we just appended a single frag. More status info needed
1200  */
1201 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1202 {
1203 	struct mptcp_ext *mpext = mptcp_get_ext(skb);
1204 	__wsum csum = ~csum_unfold(mpext->csum);
1205 	int offset = skb->len - added;
1206 
1207 	mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1208 }
1209 
1210 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1211 				      struct sock *ssk,
1212 				      struct mptcp_ext *mpext)
1213 {
1214 	if (!mpext)
1215 		return;
1216 
1217 	mpext->infinite_map = 1;
1218 	mpext->data_len = 0;
1219 
1220 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1221 	mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1222 	pr_fallback(msk);
1223 	mptcp_do_fallback(ssk);
1224 }
1225 
1226 #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
1227 
1228 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1229 			      struct mptcp_data_frag *dfrag,
1230 			      struct mptcp_sendmsg_info *info)
1231 {
1232 	u64 data_seq = dfrag->data_seq + info->sent;
1233 	int offset = dfrag->offset + info->sent;
1234 	struct mptcp_sock *msk = mptcp_sk(sk);
1235 	bool zero_window_probe = false;
1236 	struct mptcp_ext *mpext = NULL;
1237 	bool can_coalesce = false;
1238 	bool reuse_skb = true;
1239 	struct sk_buff *skb;
1240 	size_t copy;
1241 	int i;
1242 
1243 	pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
1244 		 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1245 
1246 	if (WARN_ON_ONCE(info->sent > info->limit ||
1247 			 info->limit > dfrag->data_len))
1248 		return 0;
1249 
1250 	if (unlikely(!__tcp_can_send(ssk)))
1251 		return -EAGAIN;
1252 
1253 	/* compute send limit */
1254 	if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
1255 		ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1256 	info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1257 	copy = info->size_goal;
1258 
1259 	skb = tcp_write_queue_tail(ssk);
1260 	if (skb && copy > skb->len) {
1261 		/* Limit the write to the size available in the
1262 		 * current skb, if any, so that we create at most a new skb.
1263 		 * Explicitly tells TCP internals to avoid collapsing on later
1264 		 * queue management operation, to avoid breaking the ext <->
1265 		 * SSN association set here
1266 		 */
1267 		mpext = mptcp_get_ext(skb);
1268 		if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1269 			TCP_SKB_CB(skb)->eor = 1;
1270 			tcp_mark_push(tcp_sk(ssk), skb);
1271 			goto alloc_skb;
1272 		}
1273 
1274 		i = skb_shinfo(skb)->nr_frags;
1275 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1276 		if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
1277 			tcp_mark_push(tcp_sk(ssk), skb);
1278 			goto alloc_skb;
1279 		}
1280 
1281 		copy -= skb->len;
1282 	} else {
1283 alloc_skb:
1284 		skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1285 		if (!skb)
1286 			return -ENOMEM;
1287 
1288 		i = skb_shinfo(skb)->nr_frags;
1289 		reuse_skb = false;
1290 		mpext = mptcp_get_ext(skb);
1291 	}
1292 
1293 	/* Zero window and all data acked? Probe. */
1294 	copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1295 	if (copy == 0) {
1296 		u64 snd_una = READ_ONCE(msk->snd_una);
1297 
1298 		if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) {
1299 			tcp_remove_empty_skb(ssk);
1300 			return 0;
1301 		}
1302 
1303 		zero_window_probe = true;
1304 		data_seq = snd_una - 1;
1305 		copy = 1;
1306 	}
1307 
1308 	copy = min_t(size_t, copy, info->limit - info->sent);
1309 	if (!sk_wmem_schedule(ssk, copy)) {
1310 		tcp_remove_empty_skb(ssk);
1311 		return -ENOMEM;
1312 	}
1313 
1314 	if (can_coalesce) {
1315 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1316 	} else {
1317 		get_page(dfrag->page);
1318 		skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1319 	}
1320 
1321 	skb->len += copy;
1322 	skb->data_len += copy;
1323 	skb->truesize += copy;
1324 	sk_wmem_queued_add(ssk, copy);
1325 	sk_mem_charge(ssk, copy);
1326 	WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1327 	TCP_SKB_CB(skb)->end_seq += copy;
1328 	tcp_skb_pcount_set(skb, 0);
1329 
1330 	/* on skb reuse we just need to update the DSS len */
1331 	if (reuse_skb) {
1332 		TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1333 		mpext->data_len += copy;
1334 		goto out;
1335 	}
1336 
1337 	memset(mpext, 0, sizeof(*mpext));
1338 	mpext->data_seq = data_seq;
1339 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1340 	mpext->data_len = copy;
1341 	mpext->use_map = 1;
1342 	mpext->dsn64 = 1;
1343 
1344 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
1345 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1346 		 mpext->dsn64);
1347 
1348 	if (zero_window_probe) {
1349 		mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1350 		mpext->frozen = 1;
1351 		if (READ_ONCE(msk->csum_enabled))
1352 			mptcp_update_data_checksum(skb, copy);
1353 		tcp_push_pending_frames(ssk);
1354 		return 0;
1355 	}
1356 out:
1357 	if (READ_ONCE(msk->csum_enabled))
1358 		mptcp_update_data_checksum(skb, copy);
1359 	if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1360 		mptcp_update_infinite_map(msk, ssk, mpext);
1361 	trace_mptcp_sendmsg_frag(mpext);
1362 	mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1363 	return copy;
1364 }
1365 
1366 #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
1367 					 sizeof(struct tcphdr) - \
1368 					 MAX_TCP_OPTION_SPACE - \
1369 					 sizeof(struct ipv6hdr) - \
1370 					 sizeof(struct frag_hdr))
1371 
1372 struct subflow_send_info {
1373 	struct sock *ssk;
1374 	u64 linger_time;
1375 };
1376 
1377 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1378 {
1379 	if (!subflow->stale)
1380 		return;
1381 
1382 	subflow->stale = 0;
1383 	MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1384 }
1385 
1386 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1387 {
1388 	if (unlikely(subflow->stale)) {
1389 		u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1390 
1391 		if (subflow->stale_rcv_tstamp == rcv_tstamp)
1392 			return false;
1393 
1394 		mptcp_subflow_set_active(subflow);
1395 	}
1396 	return __mptcp_subflow_active(subflow);
1397 }
1398 
1399 #define SSK_MODE_ACTIVE	0
1400 #define SSK_MODE_BACKUP	1
1401 #define SSK_MODE_MAX	2
1402 
1403 /* implement the mptcp packet scheduler;
1404  * returns the subflow that will transmit the next DSS
1405  * additionally updates the rtx timeout
1406  */
1407 struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1408 {
1409 	struct subflow_send_info send_info[SSK_MODE_MAX];
1410 	struct mptcp_subflow_context *subflow;
1411 	struct sock *sk = (struct sock *)msk;
1412 	u32 pace, burst, wmem;
1413 	int i, nr_active = 0;
1414 	struct sock *ssk;
1415 	u64 linger_time;
1416 	long tout = 0;
1417 
1418 	/* pick the subflow with the lower wmem/wspace ratio */
1419 	for (i = 0; i < SSK_MODE_MAX; ++i) {
1420 		send_info[i].ssk = NULL;
1421 		send_info[i].linger_time = -1;
1422 	}
1423 
1424 	mptcp_for_each_subflow(msk, subflow) {
1425 		bool backup = subflow->backup || subflow->request_bkup;
1426 
1427 		trace_mptcp_subflow_get_send(subflow);
1428 		ssk =  mptcp_subflow_tcp_sock(subflow);
1429 		if (!mptcp_subflow_active(subflow))
1430 			continue;
1431 
1432 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
1433 		nr_active += !backup;
1434 		pace = subflow->avg_pacing_rate;
1435 		if (unlikely(!pace)) {
1436 			/* init pacing rate from socket */
1437 			subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1438 			pace = subflow->avg_pacing_rate;
1439 			if (!pace)
1440 				continue;
1441 		}
1442 
1443 		linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1444 		if (linger_time < send_info[backup].linger_time) {
1445 			send_info[backup].ssk = ssk;
1446 			send_info[backup].linger_time = linger_time;
1447 		}
1448 	}
1449 	__mptcp_set_timeout(sk, tout);
1450 
1451 	/* pick the best backup if no other subflow is active */
1452 	if (!nr_active)
1453 		send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1454 
1455 	/* According to the blest algorithm, to avoid HoL blocking for the
1456 	 * faster flow, we need to:
1457 	 * - estimate the faster flow linger time
1458 	 * - use the above to estimate the amount of byte transferred
1459 	 *   by the faster flow
1460 	 * - check that the amount of queued data is greter than the above,
1461 	 *   otherwise do not use the picked, slower, subflow
1462 	 * We select the subflow with the shorter estimated time to flush
1463 	 * the queued mem, which basically ensure the above. We just need
1464 	 * to check that subflow has a non empty cwin.
1465 	 */
1466 	ssk = send_info[SSK_MODE_ACTIVE].ssk;
1467 	if (!ssk || !sk_stream_memory_free(ssk))
1468 		return NULL;
1469 
1470 	burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1471 	wmem = READ_ONCE(ssk->sk_wmem_queued);
1472 	if (!burst)
1473 		return ssk;
1474 
1475 	subflow = mptcp_subflow_ctx(ssk);
1476 	subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1477 					   READ_ONCE(ssk->sk_pacing_rate) * burst,
1478 					   burst + wmem);
1479 	msk->snd_burst = burst;
1480 	return ssk;
1481 }
1482 
1483 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1484 {
1485 	tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1486 	release_sock(ssk);
1487 }
1488 
1489 static void mptcp_update_post_push(struct mptcp_sock *msk,
1490 				   struct mptcp_data_frag *dfrag,
1491 				   u32 sent)
1492 {
1493 	u64 snd_nxt_new = dfrag->data_seq;
1494 
1495 	dfrag->already_sent += sent;
1496 
1497 	msk->snd_burst -= sent;
1498 
1499 	snd_nxt_new += dfrag->already_sent;
1500 
1501 	/* snd_nxt_new can be smaller than snd_nxt in case mptcp
1502 	 * is recovering after a failover. In that event, this re-sends
1503 	 * old segments.
1504 	 *
1505 	 * Thus compute snd_nxt_new candidate based on
1506 	 * the dfrag->data_seq that was sent and the data
1507 	 * that has been handed to the subflow for transmission
1508 	 * and skip update in case it was old dfrag.
1509 	 */
1510 	if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1511 		msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1512 		WRITE_ONCE(msk->snd_nxt, snd_nxt_new);
1513 	}
1514 }
1515 
1516 void mptcp_check_and_set_pending(struct sock *sk)
1517 {
1518 	if (mptcp_send_head(sk)) {
1519 		mptcp_data_lock(sk);
1520 		mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1521 		mptcp_data_unlock(sk);
1522 	}
1523 }
1524 
1525 static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1526 				  struct mptcp_sendmsg_info *info)
1527 {
1528 	struct mptcp_sock *msk = mptcp_sk(sk);
1529 	struct mptcp_data_frag *dfrag;
1530 	int len, copied = 0, err = 0;
1531 
1532 	while ((dfrag = mptcp_send_head(sk))) {
1533 		info->sent = dfrag->already_sent;
1534 		info->limit = dfrag->data_len;
1535 		len = dfrag->data_len - dfrag->already_sent;
1536 		while (len > 0) {
1537 			int ret = 0;
1538 
1539 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1540 			if (ret <= 0) {
1541 				err = copied ? : ret;
1542 				goto out;
1543 			}
1544 
1545 			info->sent += ret;
1546 			copied += ret;
1547 			len -= ret;
1548 
1549 			mptcp_update_post_push(msk, dfrag, ret);
1550 		}
1551 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1552 
1553 		if (msk->snd_burst <= 0 ||
1554 		    !sk_stream_memory_free(ssk) ||
1555 		    !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1556 			err = copied;
1557 			goto out;
1558 		}
1559 		mptcp_set_timeout(sk);
1560 	}
1561 	err = copied;
1562 
1563 out:
1564 	if (err > 0)
1565 		msk->last_data_sent = tcp_jiffies32;
1566 	return err;
1567 }
1568 
1569 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1570 {
1571 	struct sock *prev_ssk = NULL, *ssk = NULL;
1572 	struct mptcp_sock *msk = mptcp_sk(sk);
1573 	struct mptcp_sendmsg_info info = {
1574 				.flags = flags,
1575 	};
1576 	bool do_check_data_fin = false;
1577 	int push_count = 1;
1578 
1579 	while (mptcp_send_head(sk) && (push_count > 0)) {
1580 		struct mptcp_subflow_context *subflow;
1581 		int ret = 0;
1582 
1583 		if (mptcp_sched_get_send(msk))
1584 			break;
1585 
1586 		push_count = 0;
1587 
1588 		mptcp_for_each_subflow(msk, subflow) {
1589 			if (READ_ONCE(subflow->scheduled)) {
1590 				mptcp_subflow_set_scheduled(subflow, false);
1591 
1592 				prev_ssk = ssk;
1593 				ssk = mptcp_subflow_tcp_sock(subflow);
1594 				if (ssk != prev_ssk) {
1595 					/* First check. If the ssk has changed since
1596 					 * the last round, release prev_ssk
1597 					 */
1598 					if (prev_ssk)
1599 						mptcp_push_release(prev_ssk, &info);
1600 
1601 					/* Need to lock the new subflow only if different
1602 					 * from the previous one, otherwise we are still
1603 					 * helding the relevant lock
1604 					 */
1605 					lock_sock(ssk);
1606 				}
1607 
1608 				push_count++;
1609 
1610 				ret = __subflow_push_pending(sk, ssk, &info);
1611 				if (ret <= 0) {
1612 					if (ret != -EAGAIN ||
1613 					    (1 << ssk->sk_state) &
1614 					     (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1615 						push_count--;
1616 					continue;
1617 				}
1618 				do_check_data_fin = true;
1619 			}
1620 		}
1621 	}
1622 
1623 	/* at this point we held the socket lock for the last subflow we used */
1624 	if (ssk)
1625 		mptcp_push_release(ssk, &info);
1626 
1627 	/* ensure the rtx timer is running */
1628 	if (!mptcp_rtx_timer_pending(sk))
1629 		mptcp_reset_rtx_timer(sk);
1630 	if (do_check_data_fin)
1631 		mptcp_check_send_data_fin(sk);
1632 }
1633 
1634 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1635 {
1636 	struct mptcp_sock *msk = mptcp_sk(sk);
1637 	struct mptcp_sendmsg_info info = {
1638 		.data_lock_held = true,
1639 	};
1640 	bool keep_pushing = true;
1641 	struct sock *xmit_ssk;
1642 	int copied = 0;
1643 
1644 	info.flags = 0;
1645 	while (mptcp_send_head(sk) && keep_pushing) {
1646 		struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1647 		int ret = 0;
1648 
1649 		/* check for a different subflow usage only after
1650 		 * spooling the first chunk of data
1651 		 */
1652 		if (first) {
1653 			mptcp_subflow_set_scheduled(subflow, false);
1654 			ret = __subflow_push_pending(sk, ssk, &info);
1655 			first = false;
1656 			if (ret <= 0)
1657 				break;
1658 			copied += ret;
1659 			continue;
1660 		}
1661 
1662 		if (mptcp_sched_get_send(msk))
1663 			goto out;
1664 
1665 		if (READ_ONCE(subflow->scheduled)) {
1666 			mptcp_subflow_set_scheduled(subflow, false);
1667 			ret = __subflow_push_pending(sk, ssk, &info);
1668 			if (ret <= 0)
1669 				keep_pushing = false;
1670 			copied += ret;
1671 		}
1672 
1673 		mptcp_for_each_subflow(msk, subflow) {
1674 			if (READ_ONCE(subflow->scheduled)) {
1675 				xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1676 				if (xmit_ssk != ssk) {
1677 					mptcp_subflow_delegate(subflow,
1678 							       MPTCP_DELEGATE_SEND);
1679 					keep_pushing = false;
1680 				}
1681 			}
1682 		}
1683 	}
1684 
1685 out:
1686 	/* __mptcp_alloc_tx_skb could have released some wmem and we are
1687 	 * not going to flush it via release_sock()
1688 	 */
1689 	if (copied) {
1690 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1691 			 info.size_goal);
1692 		if (!mptcp_rtx_timer_pending(sk))
1693 			mptcp_reset_rtx_timer(sk);
1694 
1695 		if (msk->snd_data_fin_enable &&
1696 		    msk->snd_nxt + 1 == msk->write_seq)
1697 			mptcp_schedule_work(sk);
1698 	}
1699 }
1700 
1701 static int mptcp_disconnect(struct sock *sk, int flags);
1702 
1703 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1704 				  size_t len, int *copied_syn)
1705 {
1706 	unsigned int saved_flags = msg->msg_flags;
1707 	struct mptcp_sock *msk = mptcp_sk(sk);
1708 	struct sock *ssk;
1709 	int ret;
1710 
1711 	/* on flags based fastopen the mptcp is supposed to create the
1712 	 * first subflow right now. Otherwise we are in the defer_connect
1713 	 * path, and the first subflow must be already present.
1714 	 * Since the defer_connect flag is cleared after the first succsful
1715 	 * fastopen attempt, no need to check for additional subflow status.
1716 	 */
1717 	if (msg->msg_flags & MSG_FASTOPEN) {
1718 		ssk = __mptcp_nmpc_sk(msk);
1719 		if (IS_ERR(ssk))
1720 			return PTR_ERR(ssk);
1721 	}
1722 	if (!msk->first)
1723 		return -EINVAL;
1724 
1725 	ssk = msk->first;
1726 
1727 	lock_sock(ssk);
1728 	msg->msg_flags |= MSG_DONTWAIT;
1729 	msk->fastopening = 1;
1730 	ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1731 	msk->fastopening = 0;
1732 	msg->msg_flags = saved_flags;
1733 	release_sock(ssk);
1734 
1735 	/* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1736 	if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1737 		ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1738 					    msg->msg_namelen, msg->msg_flags, 1);
1739 
1740 		/* Keep the same behaviour of plain TCP: zero the copied bytes in
1741 		 * case of any error, except timeout or signal
1742 		 */
1743 		if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1744 			*copied_syn = 0;
1745 	} else if (ret && ret != -EINPROGRESS) {
1746 		/* The disconnect() op called by tcp_sendmsg_fastopen()/
1747 		 * __inet_stream_connect() can fail, due to looking check,
1748 		 * see mptcp_disconnect().
1749 		 * Attempt it again outside the problematic scope.
1750 		 */
1751 		if (!mptcp_disconnect(sk, 0))
1752 			sk->sk_socket->state = SS_UNCONNECTED;
1753 	}
1754 	inet_clear_bit(DEFER_CONNECT, sk);
1755 
1756 	return ret;
1757 }
1758 
1759 static int do_copy_data_nocache(struct sock *sk, int copy,
1760 				struct iov_iter *from, char *to)
1761 {
1762 	if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
1763 		if (!copy_from_iter_full_nocache(to, copy, from))
1764 			return -EFAULT;
1765 	} else if (!copy_from_iter_full(to, copy, from)) {
1766 		return -EFAULT;
1767 	}
1768 	return 0;
1769 }
1770 
1771 /* open-code sk_stream_memory_free() plus sent limit computation to
1772  * avoid indirect calls in fast-path.
1773  * Called under the msk socket lock, so we can avoid a bunch of ONCE
1774  * annotations.
1775  */
1776 static u32 mptcp_send_limit(const struct sock *sk)
1777 {
1778 	const struct mptcp_sock *msk = mptcp_sk(sk);
1779 	u32 limit, not_sent;
1780 
1781 	if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
1782 		return 0;
1783 
1784 	limit = mptcp_notsent_lowat(sk);
1785 	if (limit == UINT_MAX)
1786 		return UINT_MAX;
1787 
1788 	not_sent = msk->write_seq - msk->snd_nxt;
1789 	if (not_sent >= limit)
1790 		return 0;
1791 
1792 	return limit - not_sent;
1793 }
1794 
1795 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1796 {
1797 	struct mptcp_sock *msk = mptcp_sk(sk);
1798 	struct page_frag *pfrag;
1799 	size_t copied = 0;
1800 	int ret = 0;
1801 	long timeo;
1802 
1803 	/* silently ignore everything else */
1804 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1805 
1806 	lock_sock(sk);
1807 
1808 	if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1809 		     msg->msg_flags & MSG_FASTOPEN)) {
1810 		int copied_syn = 0;
1811 
1812 		ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1813 		copied += copied_syn;
1814 		if (ret == -EINPROGRESS && copied_syn > 0)
1815 			goto out;
1816 		else if (ret)
1817 			goto do_error;
1818 	}
1819 
1820 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1821 
1822 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1823 		ret = sk_stream_wait_connect(sk, &timeo);
1824 		if (ret)
1825 			goto do_error;
1826 	}
1827 
1828 	ret = -EPIPE;
1829 	if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1830 		goto do_error;
1831 
1832 	pfrag = sk_page_frag(sk);
1833 
1834 	while (msg_data_left(msg)) {
1835 		int total_ts, frag_truesize = 0;
1836 		struct mptcp_data_frag *dfrag;
1837 		bool dfrag_collapsed;
1838 		size_t psize, offset;
1839 		u32 copy_limit;
1840 
1841 		/* ensure fitting the notsent_lowat() constraint */
1842 		copy_limit = mptcp_send_limit(sk);
1843 		if (!copy_limit)
1844 			goto wait_for_memory;
1845 
1846 		/* reuse tail pfrag, if possible, or carve a new one from the
1847 		 * page allocator
1848 		 */
1849 		dfrag = mptcp_pending_tail(sk);
1850 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1851 		if (!dfrag_collapsed) {
1852 			if (!mptcp_page_frag_refill(sk, pfrag))
1853 				goto wait_for_memory;
1854 
1855 			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1856 			frag_truesize = dfrag->overhead;
1857 		}
1858 
1859 		/* we do not bound vs wspace, to allow a single packet.
1860 		 * memory accounting will prevent execessive memory usage
1861 		 * anyway
1862 		 */
1863 		offset = dfrag->offset + dfrag->data_len;
1864 		psize = pfrag->size - offset;
1865 		psize = min_t(size_t, psize, msg_data_left(msg));
1866 		psize = min_t(size_t, psize, copy_limit);
1867 		total_ts = psize + frag_truesize;
1868 
1869 		if (!sk_wmem_schedule(sk, total_ts))
1870 			goto wait_for_memory;
1871 
1872 		ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
1873 					   page_address(dfrag->page) + offset);
1874 		if (ret)
1875 			goto do_error;
1876 
1877 		/* data successfully copied into the write queue */
1878 		sk_forward_alloc_add(sk, -total_ts);
1879 		copied += psize;
1880 		dfrag->data_len += psize;
1881 		frag_truesize += psize;
1882 		pfrag->offset += frag_truesize;
1883 		WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1884 
1885 		/* charge data on mptcp pending queue to the msk socket
1886 		 * Note: we charge such data both to sk and ssk
1887 		 */
1888 		sk_wmem_queued_add(sk, frag_truesize);
1889 		if (!dfrag_collapsed) {
1890 			get_page(dfrag->page);
1891 			list_add_tail(&dfrag->list, &msk->rtx_queue);
1892 			if (!msk->first_pending)
1893 				WRITE_ONCE(msk->first_pending, dfrag);
1894 		}
1895 		pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
1896 			 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1897 			 !dfrag_collapsed);
1898 
1899 		continue;
1900 
1901 wait_for_memory:
1902 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1903 		__mptcp_push_pending(sk, msg->msg_flags);
1904 		ret = sk_stream_wait_memory(sk, &timeo);
1905 		if (ret)
1906 			goto do_error;
1907 	}
1908 
1909 	if (copied)
1910 		__mptcp_push_pending(sk, msg->msg_flags);
1911 
1912 out:
1913 	release_sock(sk);
1914 	return copied;
1915 
1916 do_error:
1917 	if (copied)
1918 		goto out;
1919 
1920 	copied = sk_stream_error(sk, msg->msg_flags, ret);
1921 	goto out;
1922 }
1923 
1924 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1925 				struct msghdr *msg,
1926 				size_t len, int flags,
1927 				struct scm_timestamping_internal *tss,
1928 				int *cmsg_flags)
1929 {
1930 	struct sk_buff *skb, *tmp;
1931 	int copied = 0;
1932 
1933 	skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1934 		u32 offset = MPTCP_SKB_CB(skb)->offset;
1935 		u32 data_len = skb->len - offset;
1936 		u32 count = min_t(size_t, len - copied, data_len);
1937 		int err;
1938 
1939 		if (!(flags & MSG_TRUNC)) {
1940 			err = skb_copy_datagram_msg(skb, offset, msg, count);
1941 			if (unlikely(err < 0)) {
1942 				if (!copied)
1943 					return err;
1944 				break;
1945 			}
1946 		}
1947 
1948 		if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1949 			tcp_update_recv_tstamps(skb, tss);
1950 			*cmsg_flags |= MPTCP_CMSG_TS;
1951 		}
1952 
1953 		copied += count;
1954 
1955 		if (count < data_len) {
1956 			if (!(flags & MSG_PEEK)) {
1957 				MPTCP_SKB_CB(skb)->offset += count;
1958 				MPTCP_SKB_CB(skb)->map_seq += count;
1959 				msk->bytes_consumed += count;
1960 			}
1961 			break;
1962 		}
1963 
1964 		if (!(flags & MSG_PEEK)) {
1965 			/* we will bulk release the skb memory later */
1966 			skb->destructor = NULL;
1967 			WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1968 			__skb_unlink(skb, &msk->receive_queue);
1969 			__kfree_skb(skb);
1970 			msk->bytes_consumed += count;
1971 		}
1972 
1973 		if (copied >= len)
1974 			break;
1975 	}
1976 
1977 	return copied;
1978 }
1979 
1980 /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
1981  *
1982  * Only difference: Use highest rtt estimate of the subflows in use.
1983  */
1984 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1985 {
1986 	struct mptcp_subflow_context *subflow;
1987 	struct sock *sk = (struct sock *)msk;
1988 	u8 scaling_ratio = U8_MAX;
1989 	u32 time, advmss = 1;
1990 	u64 rtt_us, mstamp;
1991 
1992 	msk_owned_by_me(msk);
1993 
1994 	if (copied <= 0)
1995 		return;
1996 
1997 	if (!msk->rcvspace_init)
1998 		mptcp_rcv_space_init(msk, msk->first);
1999 
2000 	msk->rcvq_space.copied += copied;
2001 
2002 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
2003 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
2004 
2005 	rtt_us = msk->rcvq_space.rtt_us;
2006 	if (rtt_us && time < (rtt_us >> 3))
2007 		return;
2008 
2009 	rtt_us = 0;
2010 	mptcp_for_each_subflow(msk, subflow) {
2011 		const struct tcp_sock *tp;
2012 		u64 sf_rtt_us;
2013 		u32 sf_advmss;
2014 
2015 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
2016 
2017 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
2018 		sf_advmss = READ_ONCE(tp->advmss);
2019 
2020 		rtt_us = max(sf_rtt_us, rtt_us);
2021 		advmss = max(sf_advmss, advmss);
2022 		scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
2023 	}
2024 
2025 	msk->rcvq_space.rtt_us = rtt_us;
2026 	msk->scaling_ratio = scaling_ratio;
2027 	if (time < (rtt_us >> 3) || rtt_us == 0)
2028 		return;
2029 
2030 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
2031 		goto new_measure;
2032 
2033 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
2034 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
2035 		u64 rcvwin, grow;
2036 		int rcvbuf;
2037 
2038 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
2039 
2040 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
2041 
2042 		do_div(grow, msk->rcvq_space.space);
2043 		rcvwin += (grow << 1);
2044 
2045 		rcvbuf = min_t(u64, mptcp_space_from_win(sk, rcvwin),
2046 			       READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
2047 
2048 		if (rcvbuf > sk->sk_rcvbuf) {
2049 			u32 window_clamp;
2050 
2051 			window_clamp = mptcp_win_from_space(sk, rcvbuf);
2052 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
2053 
2054 			/* Make subflows follow along.  If we do not do this, we
2055 			 * get drops at subflow level if skbs can't be moved to
2056 			 * the mptcp rx queue fast enough (announced rcv_win can
2057 			 * exceed ssk->sk_rcvbuf).
2058 			 */
2059 			mptcp_for_each_subflow(msk, subflow) {
2060 				struct sock *ssk;
2061 				bool slow;
2062 
2063 				ssk = mptcp_subflow_tcp_sock(subflow);
2064 				slow = lock_sock_fast(ssk);
2065 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
2066 				WRITE_ONCE(tcp_sk(ssk)->window_clamp, window_clamp);
2067 				tcp_cleanup_rbuf(ssk, 1);
2068 				unlock_sock_fast(ssk, slow);
2069 			}
2070 		}
2071 	}
2072 
2073 	msk->rcvq_space.space = msk->rcvq_space.copied;
2074 new_measure:
2075 	msk->rcvq_space.copied = 0;
2076 	msk->rcvq_space.time = mstamp;
2077 }
2078 
2079 static void __mptcp_update_rmem(struct sock *sk)
2080 {
2081 	struct mptcp_sock *msk = mptcp_sk(sk);
2082 
2083 	if (!msk->rmem_released)
2084 		return;
2085 
2086 	atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2087 	mptcp_rmem_uncharge(sk, msk->rmem_released);
2088 	WRITE_ONCE(msk->rmem_released, 0);
2089 }
2090 
2091 static void __mptcp_splice_receive_queue(struct sock *sk)
2092 {
2093 	struct mptcp_sock *msk = mptcp_sk(sk);
2094 
2095 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2096 }
2097 
2098 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2099 {
2100 	struct sock *sk = (struct sock *)msk;
2101 	unsigned int moved = 0;
2102 	bool ret, done;
2103 
2104 	do {
2105 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2106 		bool slowpath;
2107 
2108 		/* we can have data pending in the subflows only if the msk
2109 		 * receive buffer was full at subflow_data_ready() time,
2110 		 * that is an unlikely slow path.
2111 		 */
2112 		if (likely(!ssk))
2113 			break;
2114 
2115 		slowpath = lock_sock_fast(ssk);
2116 		mptcp_data_lock(sk);
2117 		__mptcp_update_rmem(sk);
2118 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2119 		mptcp_data_unlock(sk);
2120 
2121 		if (unlikely(ssk->sk_err))
2122 			__mptcp_error_report(sk);
2123 		unlock_sock_fast(ssk, slowpath);
2124 	} while (!done);
2125 
2126 	/* acquire the data lock only if some input data is pending */
2127 	ret = moved > 0;
2128 	if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2129 	    !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2130 		mptcp_data_lock(sk);
2131 		__mptcp_update_rmem(sk);
2132 		ret |= __mptcp_ofo_queue(msk);
2133 		__mptcp_splice_receive_queue(sk);
2134 		mptcp_data_unlock(sk);
2135 	}
2136 	if (ret)
2137 		mptcp_check_data_fin((struct sock *)msk);
2138 	return !skb_queue_empty(&msk->receive_queue);
2139 }
2140 
2141 static unsigned int mptcp_inq_hint(const struct sock *sk)
2142 {
2143 	const struct mptcp_sock *msk = mptcp_sk(sk);
2144 	const struct sk_buff *skb;
2145 
2146 	skb = skb_peek(&msk->receive_queue);
2147 	if (skb) {
2148 		u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
2149 
2150 		if (hint_val >= INT_MAX)
2151 			return INT_MAX;
2152 
2153 		return (unsigned int)hint_val;
2154 	}
2155 
2156 	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2157 		return 1;
2158 
2159 	return 0;
2160 }
2161 
2162 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2163 			 int flags, int *addr_len)
2164 {
2165 	struct mptcp_sock *msk = mptcp_sk(sk);
2166 	struct scm_timestamping_internal tss;
2167 	int copied = 0, cmsg_flags = 0;
2168 	int target;
2169 	long timeo;
2170 
2171 	/* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2172 	if (unlikely(flags & MSG_ERRQUEUE))
2173 		return inet_recv_error(sk, msg, len, addr_len);
2174 
2175 	lock_sock(sk);
2176 	if (unlikely(sk->sk_state == TCP_LISTEN)) {
2177 		copied = -ENOTCONN;
2178 		goto out_err;
2179 	}
2180 
2181 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2182 
2183 	len = min_t(size_t, len, INT_MAX);
2184 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2185 
2186 	if (unlikely(msk->recvmsg_inq))
2187 		cmsg_flags = MPTCP_CMSG_INQ;
2188 
2189 	while (copied < len) {
2190 		int bytes_read;
2191 
2192 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2193 		if (unlikely(bytes_read < 0)) {
2194 			if (!copied)
2195 				copied = bytes_read;
2196 			goto out_err;
2197 		}
2198 
2199 		copied += bytes_read;
2200 
2201 		/* be sure to advertise window change */
2202 		mptcp_cleanup_rbuf(msk);
2203 
2204 		if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2205 			continue;
2206 
2207 		/* only the MPTCP socket status is relevant here. The exit
2208 		 * conditions mirror closely tcp_recvmsg()
2209 		 */
2210 		if (copied >= target)
2211 			break;
2212 
2213 		if (copied) {
2214 			if (sk->sk_err ||
2215 			    sk->sk_state == TCP_CLOSE ||
2216 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
2217 			    !timeo ||
2218 			    signal_pending(current))
2219 				break;
2220 		} else {
2221 			if (sk->sk_err) {
2222 				copied = sock_error(sk);
2223 				break;
2224 			}
2225 
2226 			if (sk->sk_shutdown & RCV_SHUTDOWN) {
2227 				/* race breaker: the shutdown could be after the
2228 				 * previous receive queue check
2229 				 */
2230 				if (__mptcp_move_skbs(msk))
2231 					continue;
2232 				break;
2233 			}
2234 
2235 			if (sk->sk_state == TCP_CLOSE) {
2236 				copied = -ENOTCONN;
2237 				break;
2238 			}
2239 
2240 			if (!timeo) {
2241 				copied = -EAGAIN;
2242 				break;
2243 			}
2244 
2245 			if (signal_pending(current)) {
2246 				copied = sock_intr_errno(timeo);
2247 				break;
2248 			}
2249 		}
2250 
2251 		pr_debug("block timeout %ld\n", timeo);
2252 		sk_wait_data(sk, &timeo, NULL);
2253 	}
2254 
2255 out_err:
2256 	if (cmsg_flags && copied >= 0) {
2257 		if (cmsg_flags & MPTCP_CMSG_TS)
2258 			tcp_recv_timestamp(msg, sk, &tss);
2259 
2260 		if (cmsg_flags & MPTCP_CMSG_INQ) {
2261 			unsigned int inq = mptcp_inq_hint(sk);
2262 
2263 			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2264 		}
2265 	}
2266 
2267 	pr_debug("msk=%p rx queue empty=%d:%d copied=%d\n",
2268 		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2269 		 skb_queue_empty(&msk->receive_queue), copied);
2270 	if (!(flags & MSG_PEEK))
2271 		mptcp_rcv_space_adjust(msk, copied);
2272 
2273 	release_sock(sk);
2274 	return copied;
2275 }
2276 
2277 static void mptcp_retransmit_timer(struct timer_list *t)
2278 {
2279 	struct inet_connection_sock *icsk = from_timer(icsk, t,
2280 						       icsk_retransmit_timer);
2281 	struct sock *sk = &icsk->icsk_inet.sk;
2282 	struct mptcp_sock *msk = mptcp_sk(sk);
2283 
2284 	bh_lock_sock(sk);
2285 	if (!sock_owned_by_user(sk)) {
2286 		/* we need a process context to retransmit */
2287 		if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2288 			mptcp_schedule_work(sk);
2289 	} else {
2290 		/* delegate our work to tcp_release_cb() */
2291 		__set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2292 	}
2293 	bh_unlock_sock(sk);
2294 	sock_put(sk);
2295 }
2296 
2297 static void mptcp_tout_timer(struct timer_list *t)
2298 {
2299 	struct sock *sk = from_timer(sk, t, sk_timer);
2300 
2301 	mptcp_schedule_work(sk);
2302 	sock_put(sk);
2303 }
2304 
2305 /* Find an idle subflow.  Return NULL if there is unacked data at tcp
2306  * level.
2307  *
2308  * A backup subflow is returned only if that is the only kind available.
2309  */
2310 struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2311 {
2312 	struct sock *backup = NULL, *pick = NULL;
2313 	struct mptcp_subflow_context *subflow;
2314 	int min_stale_count = INT_MAX;
2315 
2316 	mptcp_for_each_subflow(msk, subflow) {
2317 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2318 
2319 		if (!__mptcp_subflow_active(subflow))
2320 			continue;
2321 
2322 		/* still data outstanding at TCP level? skip this */
2323 		if (!tcp_rtx_and_write_queues_empty(ssk)) {
2324 			mptcp_pm_subflow_chk_stale(msk, ssk);
2325 			min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2326 			continue;
2327 		}
2328 
2329 		if (subflow->backup || subflow->request_bkup) {
2330 			if (!backup)
2331 				backup = ssk;
2332 			continue;
2333 		}
2334 
2335 		if (!pick)
2336 			pick = ssk;
2337 	}
2338 
2339 	if (pick)
2340 		return pick;
2341 
2342 	/* use backup only if there are no progresses anywhere */
2343 	return min_stale_count > 1 ? backup : NULL;
2344 }
2345 
2346 bool __mptcp_retransmit_pending_data(struct sock *sk)
2347 {
2348 	struct mptcp_data_frag *cur, *rtx_head;
2349 	struct mptcp_sock *msk = mptcp_sk(sk);
2350 
2351 	if (__mptcp_check_fallback(msk))
2352 		return false;
2353 
2354 	/* the closing socket has some data untransmitted and/or unacked:
2355 	 * some data in the mptcp rtx queue has not really xmitted yet.
2356 	 * keep it simple and re-inject the whole mptcp level rtx queue
2357 	 */
2358 	mptcp_data_lock(sk);
2359 	__mptcp_clean_una_wakeup(sk);
2360 	rtx_head = mptcp_rtx_head(sk);
2361 	if (!rtx_head) {
2362 		mptcp_data_unlock(sk);
2363 		return false;
2364 	}
2365 
2366 	msk->recovery_snd_nxt = msk->snd_nxt;
2367 	msk->recovery = true;
2368 	mptcp_data_unlock(sk);
2369 
2370 	msk->first_pending = rtx_head;
2371 	msk->snd_burst = 0;
2372 
2373 	/* be sure to clear the "sent status" on all re-injected fragments */
2374 	list_for_each_entry(cur, &msk->rtx_queue, list) {
2375 		if (!cur->already_sent)
2376 			break;
2377 		cur->already_sent = 0;
2378 	}
2379 
2380 	return true;
2381 }
2382 
2383 /* flags for __mptcp_close_ssk() */
2384 #define MPTCP_CF_PUSH		BIT(1)
2385 #define MPTCP_CF_FASTCLOSE	BIT(2)
2386 
2387 /* be sure to send a reset only if the caller asked for it, also
2388  * clean completely the subflow status when the subflow reaches
2389  * TCP_CLOSE state
2390  */
2391 static void __mptcp_subflow_disconnect(struct sock *ssk,
2392 				       struct mptcp_subflow_context *subflow,
2393 				       unsigned int flags)
2394 {
2395 	if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
2396 	    (flags & MPTCP_CF_FASTCLOSE)) {
2397 		/* The MPTCP code never wait on the subflow sockets, TCP-level
2398 		 * disconnect should never fail
2399 		 */
2400 		WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2401 		mptcp_subflow_ctx_reset(subflow);
2402 	} else {
2403 		tcp_shutdown(ssk, SEND_SHUTDOWN);
2404 	}
2405 }
2406 
2407 /* subflow sockets can be either outgoing (connect) or incoming
2408  * (accept).
2409  *
2410  * Outgoing subflows use in-kernel sockets.
2411  * Incoming subflows do not have their own 'struct socket' allocated,
2412  * so we need to use tcp_close() after detaching them from the mptcp
2413  * parent socket.
2414  */
2415 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2416 			      struct mptcp_subflow_context *subflow,
2417 			      unsigned int flags)
2418 {
2419 	struct mptcp_sock *msk = mptcp_sk(sk);
2420 	bool dispose_it, need_push = false;
2421 
2422 	/* If the first subflow moved to a close state before accept, e.g. due
2423 	 * to an incoming reset or listener shutdown, the subflow socket is
2424 	 * already deleted by inet_child_forget() and the mptcp socket can't
2425 	 * survive too.
2426 	 */
2427 	if (msk->in_accept_queue && msk->first == ssk &&
2428 	    (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
2429 		/* ensure later check in mptcp_worker() will dispose the msk */
2430 		sock_set_flag(sk, SOCK_DEAD);
2431 		mptcp_set_close_tout(sk, tcp_jiffies32 - (mptcp_close_timeout(sk) + 1));
2432 		lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2433 		mptcp_subflow_drop_ctx(ssk);
2434 		goto out_release;
2435 	}
2436 
2437 	dispose_it = msk->free_first || ssk != msk->first;
2438 	if (dispose_it)
2439 		list_del(&subflow->node);
2440 
2441 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2442 
2443 	if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2444 		/* be sure to force the tcp_close path
2445 		 * to generate the egress reset
2446 		 */
2447 		ssk->sk_lingertime = 0;
2448 		sock_set_flag(ssk, SOCK_LINGER);
2449 		subflow->send_fastclose = 1;
2450 	}
2451 
2452 	need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2453 	if (!dispose_it) {
2454 		__mptcp_subflow_disconnect(ssk, subflow, flags);
2455 		release_sock(ssk);
2456 
2457 		goto out;
2458 	}
2459 
2460 	subflow->disposable = 1;
2461 
2462 	/* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2463 	 * the ssk has been already destroyed, we just need to release the
2464 	 * reference owned by msk;
2465 	 */
2466 	if (!inet_csk(ssk)->icsk_ulp_ops) {
2467 		WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2468 		kfree_rcu(subflow, rcu);
2469 	} else {
2470 		/* otherwise tcp will dispose of the ssk and subflow ctx */
2471 		__tcp_close(ssk, 0);
2472 
2473 		/* close acquired an extra ref */
2474 		__sock_put(ssk);
2475 	}
2476 
2477 out_release:
2478 	__mptcp_subflow_error_report(sk, ssk);
2479 	release_sock(ssk);
2480 
2481 	sock_put(ssk);
2482 
2483 	if (ssk == msk->first)
2484 		WRITE_ONCE(msk->first, NULL);
2485 
2486 out:
2487 	__mptcp_sync_sndbuf(sk);
2488 	if (need_push)
2489 		__mptcp_push_pending(sk, 0);
2490 
2491 	/* Catch every 'all subflows closed' scenario, including peers silently
2492 	 * closing them, e.g. due to timeout.
2493 	 * For established sockets, allow an additional timeout before closing,
2494 	 * as the protocol can still create more subflows.
2495 	 */
2496 	if (list_is_singular(&msk->conn_list) && msk->first &&
2497 	    inet_sk_state_load(msk->first) == TCP_CLOSE) {
2498 		if (sk->sk_state != TCP_ESTABLISHED ||
2499 		    msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2500 			mptcp_set_state(sk, TCP_CLOSE);
2501 			mptcp_close_wake_up(sk);
2502 		} else {
2503 			mptcp_start_tout_timer(sk);
2504 		}
2505 	}
2506 }
2507 
2508 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2509 		     struct mptcp_subflow_context *subflow)
2510 {
2511 	/* The first subflow can already be closed and still in the list */
2512 	if (subflow->close_event_done)
2513 		return;
2514 
2515 	subflow->close_event_done = true;
2516 
2517 	if (sk->sk_state == TCP_ESTABLISHED)
2518 		mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2519 
2520 	/* subflow aborted before reaching the fully_established status
2521 	 * attempt the creation of the next subflow
2522 	 */
2523 	mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow);
2524 
2525 	__mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2526 }
2527 
2528 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2529 {
2530 	return 0;
2531 }
2532 
2533 static void __mptcp_close_subflow(struct sock *sk)
2534 {
2535 	struct mptcp_subflow_context *subflow, *tmp;
2536 	struct mptcp_sock *msk = mptcp_sk(sk);
2537 
2538 	might_sleep();
2539 
2540 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2541 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2542 		int ssk_state = inet_sk_state_load(ssk);
2543 
2544 		if (ssk_state != TCP_CLOSE &&
2545 		    (ssk_state != TCP_CLOSE_WAIT ||
2546 		     inet_sk_state_load(sk) != TCP_ESTABLISHED))
2547 			continue;
2548 
2549 		/* 'subflow_data_ready' will re-sched once rx queue is empty */
2550 		if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2551 			continue;
2552 
2553 		mptcp_close_ssk(sk, ssk, subflow);
2554 	}
2555 
2556 }
2557 
2558 static bool mptcp_close_tout_expired(const struct sock *sk)
2559 {
2560 	if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2561 	    sk->sk_state == TCP_CLOSE)
2562 		return false;
2563 
2564 	return time_after32(tcp_jiffies32,
2565 		  inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
2566 }
2567 
2568 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2569 {
2570 	struct mptcp_subflow_context *subflow, *tmp;
2571 	struct sock *sk = (struct sock *)msk;
2572 
2573 	if (likely(!READ_ONCE(msk->rcv_fastclose)))
2574 		return;
2575 
2576 	mptcp_token_destroy(msk);
2577 
2578 	mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2579 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2580 		bool slow;
2581 
2582 		slow = lock_sock_fast(tcp_sk);
2583 		if (tcp_sk->sk_state != TCP_CLOSE) {
2584 			mptcp_send_active_reset_reason(tcp_sk);
2585 			tcp_set_state(tcp_sk, TCP_CLOSE);
2586 		}
2587 		unlock_sock_fast(tcp_sk, slow);
2588 	}
2589 
2590 	/* Mirror the tcp_reset() error propagation */
2591 	switch (sk->sk_state) {
2592 	case TCP_SYN_SENT:
2593 		WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2594 		break;
2595 	case TCP_CLOSE_WAIT:
2596 		WRITE_ONCE(sk->sk_err, EPIPE);
2597 		break;
2598 	case TCP_CLOSE:
2599 		return;
2600 	default:
2601 		WRITE_ONCE(sk->sk_err, ECONNRESET);
2602 	}
2603 
2604 	mptcp_set_state(sk, TCP_CLOSE);
2605 	WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2606 	smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2607 	set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2608 
2609 	/* the calling mptcp_worker will properly destroy the socket */
2610 	if (sock_flag(sk, SOCK_DEAD))
2611 		return;
2612 
2613 	sk->sk_state_change(sk);
2614 	sk_error_report(sk);
2615 }
2616 
2617 static void __mptcp_retrans(struct sock *sk)
2618 {
2619 	struct mptcp_sock *msk = mptcp_sk(sk);
2620 	struct mptcp_subflow_context *subflow;
2621 	struct mptcp_sendmsg_info info = {};
2622 	struct mptcp_data_frag *dfrag;
2623 	struct sock *ssk;
2624 	int ret, err;
2625 	u16 len = 0;
2626 
2627 	mptcp_clean_una_wakeup(sk);
2628 
2629 	/* first check ssk: need to kick "stale" logic */
2630 	err = mptcp_sched_get_retrans(msk);
2631 	dfrag = mptcp_rtx_head(sk);
2632 	if (!dfrag) {
2633 		if (mptcp_data_fin_enabled(msk)) {
2634 			struct inet_connection_sock *icsk = inet_csk(sk);
2635 
2636 			icsk->icsk_retransmits++;
2637 			mptcp_set_datafin_timeout(sk);
2638 			mptcp_send_ack(msk);
2639 
2640 			goto reset_timer;
2641 		}
2642 
2643 		if (!mptcp_send_head(sk))
2644 			return;
2645 
2646 		goto reset_timer;
2647 	}
2648 
2649 	if (err)
2650 		goto reset_timer;
2651 
2652 	mptcp_for_each_subflow(msk, subflow) {
2653 		if (READ_ONCE(subflow->scheduled)) {
2654 			u16 copied = 0;
2655 
2656 			mptcp_subflow_set_scheduled(subflow, false);
2657 
2658 			ssk = mptcp_subflow_tcp_sock(subflow);
2659 
2660 			lock_sock(ssk);
2661 
2662 			/* limit retransmission to the bytes already sent on some subflows */
2663 			info.sent = 0;
2664 			info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2665 								    dfrag->already_sent;
2666 			while (info.sent < info.limit) {
2667 				ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2668 				if (ret <= 0)
2669 					break;
2670 
2671 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2672 				copied += ret;
2673 				info.sent += ret;
2674 			}
2675 			if (copied) {
2676 				len = max(copied, len);
2677 				tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2678 					 info.size_goal);
2679 				WRITE_ONCE(msk->allow_infinite_fallback, false);
2680 			}
2681 
2682 			release_sock(ssk);
2683 		}
2684 	}
2685 
2686 	msk->bytes_retrans += len;
2687 	dfrag->already_sent = max(dfrag->already_sent, len);
2688 
2689 reset_timer:
2690 	mptcp_check_and_set_pending(sk);
2691 
2692 	if (!mptcp_rtx_timer_pending(sk))
2693 		mptcp_reset_rtx_timer(sk);
2694 }
2695 
2696 /* schedule the timeout timer for the relevant event: either close timeout
2697  * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2698  */
2699 void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2700 {
2701 	struct sock *sk = (struct sock *)msk;
2702 	unsigned long timeout, close_timeout;
2703 
2704 	if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2705 		return;
2706 
2707 	close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies +
2708 			mptcp_close_timeout(sk);
2709 
2710 	/* the close timeout takes precedence on the fail one, and here at least one of
2711 	 * them is active
2712 	 */
2713 	timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2714 
2715 	sk_reset_timer(sk, &sk->sk_timer, timeout);
2716 }
2717 
2718 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2719 {
2720 	struct sock *ssk = msk->first;
2721 	bool slow;
2722 
2723 	if (!ssk)
2724 		return;
2725 
2726 	pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
2727 
2728 	slow = lock_sock_fast(ssk);
2729 	mptcp_subflow_reset(ssk);
2730 	WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2731 	unlock_sock_fast(ssk, slow);
2732 }
2733 
2734 static void mptcp_do_fastclose(struct sock *sk)
2735 {
2736 	struct mptcp_subflow_context *subflow, *tmp;
2737 	struct mptcp_sock *msk = mptcp_sk(sk);
2738 
2739 	mptcp_set_state(sk, TCP_CLOSE);
2740 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
2741 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2742 				  subflow, MPTCP_CF_FASTCLOSE);
2743 }
2744 
2745 static void mptcp_worker(struct work_struct *work)
2746 {
2747 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2748 	struct sock *sk = (struct sock *)msk;
2749 	unsigned long fail_tout;
2750 	int state;
2751 
2752 	lock_sock(sk);
2753 	state = sk->sk_state;
2754 	if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2755 		goto unlock;
2756 
2757 	mptcp_check_fastclose(msk);
2758 
2759 	mptcp_pm_nl_work(msk);
2760 
2761 	mptcp_check_send_data_fin(sk);
2762 	mptcp_check_data_fin_ack(sk);
2763 	mptcp_check_data_fin(sk);
2764 
2765 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2766 		__mptcp_close_subflow(sk);
2767 
2768 	if (mptcp_close_tout_expired(sk)) {
2769 		mptcp_do_fastclose(sk);
2770 		mptcp_close_wake_up(sk);
2771 	}
2772 
2773 	if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2774 		__mptcp_destroy_sock(sk);
2775 		goto unlock;
2776 	}
2777 
2778 	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2779 		__mptcp_retrans(sk);
2780 
2781 	fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2782 	if (fail_tout && time_after(jiffies, fail_tout))
2783 		mptcp_mp_fail_no_response(msk);
2784 
2785 unlock:
2786 	release_sock(sk);
2787 	sock_put(sk);
2788 }
2789 
2790 static void __mptcp_init_sock(struct sock *sk)
2791 {
2792 	struct mptcp_sock *msk = mptcp_sk(sk);
2793 
2794 	INIT_LIST_HEAD(&msk->conn_list);
2795 	INIT_LIST_HEAD(&msk->join_list);
2796 	INIT_LIST_HEAD(&msk->rtx_queue);
2797 	INIT_WORK(&msk->work, mptcp_worker);
2798 	__skb_queue_head_init(&msk->receive_queue);
2799 	msk->out_of_order_queue = RB_ROOT;
2800 	msk->first_pending = NULL;
2801 	WRITE_ONCE(msk->rmem_fwd_alloc, 0);
2802 	WRITE_ONCE(msk->rmem_released, 0);
2803 	msk->timer_ival = TCP_RTO_MIN;
2804 	msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2805 
2806 	WRITE_ONCE(msk->first, NULL);
2807 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2808 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2809 	WRITE_ONCE(msk->allow_infinite_fallback, true);
2810 	msk->recovery = false;
2811 	msk->subflow_id = 1;
2812 	msk->last_data_sent = tcp_jiffies32;
2813 	msk->last_data_recv = tcp_jiffies32;
2814 	msk->last_ack_recv = tcp_jiffies32;
2815 
2816 	mptcp_pm_data_init(msk);
2817 
2818 	/* re-use the csk retrans timer for MPTCP-level retrans */
2819 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2820 	timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2821 }
2822 
2823 static void mptcp_ca_reset(struct sock *sk)
2824 {
2825 	struct inet_connection_sock *icsk = inet_csk(sk);
2826 
2827 	tcp_assign_congestion_control(sk);
2828 	strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
2829 		sizeof(mptcp_sk(sk)->ca_name));
2830 
2831 	/* no need to keep a reference to the ops, the name will suffice */
2832 	tcp_cleanup_congestion_control(sk);
2833 	icsk->icsk_ca_ops = NULL;
2834 }
2835 
2836 static int mptcp_init_sock(struct sock *sk)
2837 {
2838 	struct net *net = sock_net(sk);
2839 	int ret;
2840 
2841 	__mptcp_init_sock(sk);
2842 
2843 	if (!mptcp_is_enabled(net))
2844 		return -ENOPROTOOPT;
2845 
2846 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2847 		return -ENOMEM;
2848 
2849 	ret = mptcp_init_sched(mptcp_sk(sk),
2850 			       mptcp_sched_find(mptcp_get_scheduler(net)));
2851 	if (ret)
2852 		return ret;
2853 
2854 	set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2855 
2856 	/* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2857 	 * propagate the correct value
2858 	 */
2859 	mptcp_ca_reset(sk);
2860 
2861 	sk_sockets_allocated_inc(sk);
2862 	sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2863 	sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2864 
2865 	return 0;
2866 }
2867 
2868 static void __mptcp_clear_xmit(struct sock *sk)
2869 {
2870 	struct mptcp_sock *msk = mptcp_sk(sk);
2871 	struct mptcp_data_frag *dtmp, *dfrag;
2872 
2873 	WRITE_ONCE(msk->first_pending, NULL);
2874 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2875 		dfrag_clear(sk, dfrag);
2876 }
2877 
2878 void mptcp_cancel_work(struct sock *sk)
2879 {
2880 	struct mptcp_sock *msk = mptcp_sk(sk);
2881 
2882 	if (cancel_work_sync(&msk->work))
2883 		__sock_put(sk);
2884 }
2885 
2886 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2887 {
2888 	lock_sock(ssk);
2889 
2890 	switch (ssk->sk_state) {
2891 	case TCP_LISTEN:
2892 		if (!(how & RCV_SHUTDOWN))
2893 			break;
2894 		fallthrough;
2895 	case TCP_SYN_SENT:
2896 		WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2897 		break;
2898 	default:
2899 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
2900 			pr_debug("Fallback\n");
2901 			ssk->sk_shutdown |= how;
2902 			tcp_shutdown(ssk, how);
2903 
2904 			/* simulate the data_fin ack reception to let the state
2905 			 * machine move forward
2906 			 */
2907 			WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2908 			mptcp_schedule_work(sk);
2909 		} else {
2910 			pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
2911 			tcp_send_ack(ssk);
2912 			if (!mptcp_rtx_timer_pending(sk))
2913 				mptcp_reset_rtx_timer(sk);
2914 		}
2915 		break;
2916 	}
2917 
2918 	release_sock(ssk);
2919 }
2920 
2921 void mptcp_set_state(struct sock *sk, int state)
2922 {
2923 	int oldstate = sk->sk_state;
2924 
2925 	switch (state) {
2926 	case TCP_ESTABLISHED:
2927 		if (oldstate != TCP_ESTABLISHED)
2928 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2929 		break;
2930 	case TCP_CLOSE_WAIT:
2931 		/* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
2932 		 * MPTCP "accepted" sockets will be created later on. So no
2933 		 * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
2934 		 */
2935 		break;
2936 	default:
2937 		if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
2938 			MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2939 	}
2940 
2941 	inet_sk_state_store(sk, state);
2942 }
2943 
2944 static const unsigned char new_state[16] = {
2945 	/* current state:     new state:      action:	*/
2946 	[0 /* (Invalid) */] = TCP_CLOSE,
2947 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2948 	[TCP_SYN_SENT]      = TCP_CLOSE,
2949 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2950 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
2951 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
2952 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
2953 	[TCP_CLOSE]         = TCP_CLOSE,
2954 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
2955 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
2956 	[TCP_LISTEN]        = TCP_CLOSE,
2957 	[TCP_CLOSING]       = TCP_CLOSING,
2958 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
2959 };
2960 
2961 static int mptcp_close_state(struct sock *sk)
2962 {
2963 	int next = (int)new_state[sk->sk_state];
2964 	int ns = next & TCP_STATE_MASK;
2965 
2966 	mptcp_set_state(sk, ns);
2967 
2968 	return next & TCP_ACTION_FIN;
2969 }
2970 
2971 static void mptcp_check_send_data_fin(struct sock *sk)
2972 {
2973 	struct mptcp_subflow_context *subflow;
2974 	struct mptcp_sock *msk = mptcp_sk(sk);
2975 
2976 	pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
2977 		 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2978 		 msk->snd_nxt, msk->write_seq);
2979 
2980 	/* we still need to enqueue subflows or not really shutting down,
2981 	 * skip this
2982 	 */
2983 	if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2984 	    mptcp_send_head(sk))
2985 		return;
2986 
2987 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2988 
2989 	mptcp_for_each_subflow(msk, subflow) {
2990 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2991 
2992 		mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2993 	}
2994 }
2995 
2996 static void __mptcp_wr_shutdown(struct sock *sk)
2997 {
2998 	struct mptcp_sock *msk = mptcp_sk(sk);
2999 
3000 	pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
3001 		 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
3002 		 !!mptcp_send_head(sk));
3003 
3004 	/* will be ignored by fallback sockets */
3005 	WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
3006 	WRITE_ONCE(msk->snd_data_fin_enable, 1);
3007 
3008 	mptcp_check_send_data_fin(sk);
3009 }
3010 
3011 static void __mptcp_destroy_sock(struct sock *sk)
3012 {
3013 	struct mptcp_sock *msk = mptcp_sk(sk);
3014 
3015 	pr_debug("msk=%p\n", msk);
3016 
3017 	might_sleep();
3018 
3019 	mptcp_stop_rtx_timer(sk);
3020 	sk_stop_timer(sk, &sk->sk_timer);
3021 	msk->pm.status = 0;
3022 	mptcp_release_sched(msk);
3023 
3024 	sk->sk_prot->destroy(sk);
3025 
3026 	WARN_ON_ONCE(READ_ONCE(msk->rmem_fwd_alloc));
3027 	WARN_ON_ONCE(msk->rmem_released);
3028 	sk_stream_kill_queues(sk);
3029 	xfrm_sk_free_policy(sk);
3030 
3031 	sock_put(sk);
3032 }
3033 
3034 void __mptcp_unaccepted_force_close(struct sock *sk)
3035 {
3036 	sock_set_flag(sk, SOCK_DEAD);
3037 	mptcp_do_fastclose(sk);
3038 	__mptcp_destroy_sock(sk);
3039 }
3040 
3041 static __poll_t mptcp_check_readable(struct sock *sk)
3042 {
3043 	return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3044 }
3045 
3046 static void mptcp_check_listen_stop(struct sock *sk)
3047 {
3048 	struct sock *ssk;
3049 
3050 	if (inet_sk_state_load(sk) != TCP_LISTEN)
3051 		return;
3052 
3053 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3054 	ssk = mptcp_sk(sk)->first;
3055 	if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3056 		return;
3057 
3058 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3059 	tcp_set_state(ssk, TCP_CLOSE);
3060 	mptcp_subflow_queue_clean(sk, ssk);
3061 	inet_csk_listen_stop(ssk);
3062 	mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
3063 	release_sock(ssk);
3064 }
3065 
3066 bool __mptcp_close(struct sock *sk, long timeout)
3067 {
3068 	struct mptcp_subflow_context *subflow;
3069 	struct mptcp_sock *msk = mptcp_sk(sk);
3070 	bool do_cancel_work = false;
3071 	int subflows_alive = 0;
3072 
3073 	WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3074 
3075 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3076 		mptcp_check_listen_stop(sk);
3077 		mptcp_set_state(sk, TCP_CLOSE);
3078 		goto cleanup;
3079 	}
3080 
3081 	if (mptcp_data_avail(msk) || timeout < 0) {
3082 		/* If the msk has read data, or the caller explicitly ask it,
3083 		 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3084 		 */
3085 		mptcp_do_fastclose(sk);
3086 		timeout = 0;
3087 	} else if (mptcp_close_state(sk)) {
3088 		__mptcp_wr_shutdown(sk);
3089 	}
3090 
3091 	sk_stream_wait_close(sk, timeout);
3092 
3093 cleanup:
3094 	/* orphan all the subflows */
3095 	mptcp_for_each_subflow(msk, subflow) {
3096 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3097 		bool slow = lock_sock_fast_nested(ssk);
3098 
3099 		subflows_alive += ssk->sk_state != TCP_CLOSE;
3100 
3101 		/* since the close timeout takes precedence on the fail one,
3102 		 * cancel the latter
3103 		 */
3104 		if (ssk == msk->first)
3105 			subflow->fail_tout = 0;
3106 
3107 		/* detach from the parent socket, but allow data_ready to
3108 		 * push incoming data into the mptcp stack, to properly ack it
3109 		 */
3110 		ssk->sk_socket = NULL;
3111 		ssk->sk_wq = NULL;
3112 		unlock_sock_fast(ssk, slow);
3113 	}
3114 	sock_orphan(sk);
3115 
3116 	/* all the subflows are closed, only timeout can change the msk
3117 	 * state, let's not keep resources busy for no reasons
3118 	 */
3119 	if (subflows_alive == 0)
3120 		mptcp_set_state(sk, TCP_CLOSE);
3121 
3122 	sock_hold(sk);
3123 	pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3124 	if (msk->token)
3125 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3126 
3127 	if (sk->sk_state == TCP_CLOSE) {
3128 		__mptcp_destroy_sock(sk);
3129 		do_cancel_work = true;
3130 	} else {
3131 		mptcp_start_tout_timer(sk);
3132 	}
3133 
3134 	return do_cancel_work;
3135 }
3136 
3137 static void mptcp_close(struct sock *sk, long timeout)
3138 {
3139 	bool do_cancel_work;
3140 
3141 	lock_sock(sk);
3142 
3143 	do_cancel_work = __mptcp_close(sk, timeout);
3144 	release_sock(sk);
3145 	if (do_cancel_work)
3146 		mptcp_cancel_work(sk);
3147 
3148 	sock_put(sk);
3149 }
3150 
3151 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3152 {
3153 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3154 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3155 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
3156 
3157 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
3158 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3159 
3160 	if (msk6 && ssk6) {
3161 		msk6->saddr = ssk6->saddr;
3162 		msk6->flow_label = ssk6->flow_label;
3163 	}
3164 #endif
3165 
3166 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3167 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3168 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3169 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3170 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3171 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3172 }
3173 
3174 static int mptcp_disconnect(struct sock *sk, int flags)
3175 {
3176 	struct mptcp_sock *msk = mptcp_sk(sk);
3177 
3178 	/* We are on the fastopen error path. We can't call straight into the
3179 	 * subflows cleanup code due to lock nesting (we are already under
3180 	 * msk->firstsocket lock).
3181 	 */
3182 	if (msk->fastopening)
3183 		return -EBUSY;
3184 
3185 	mptcp_check_listen_stop(sk);
3186 	mptcp_set_state(sk, TCP_CLOSE);
3187 
3188 	mptcp_stop_rtx_timer(sk);
3189 	mptcp_stop_tout_timer(sk);
3190 
3191 	if (msk->token)
3192 		mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3193 
3194 	/* msk->subflow is still intact, the following will not free the first
3195 	 * subflow
3196 	 */
3197 	mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3198 	WRITE_ONCE(msk->flags, 0);
3199 	msk->cb_flags = 0;
3200 	msk->recovery = false;
3201 	WRITE_ONCE(msk->can_ack, false);
3202 	WRITE_ONCE(msk->fully_established, false);
3203 	WRITE_ONCE(msk->rcv_data_fin, false);
3204 	WRITE_ONCE(msk->snd_data_fin_enable, false);
3205 	WRITE_ONCE(msk->rcv_fastclose, false);
3206 	WRITE_ONCE(msk->use_64bit_ack, false);
3207 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3208 	mptcp_pm_data_reset(msk);
3209 	mptcp_ca_reset(sk);
3210 	msk->bytes_consumed = 0;
3211 	msk->bytes_acked = 0;
3212 	msk->bytes_received = 0;
3213 	msk->bytes_sent = 0;
3214 	msk->bytes_retrans = 0;
3215 	msk->rcvspace_init = 0;
3216 
3217 	WRITE_ONCE(sk->sk_shutdown, 0);
3218 	sk_error_report(sk);
3219 	return 0;
3220 }
3221 
3222 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3223 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3224 {
3225 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3226 
3227 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3228 }
3229 
3230 static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3231 {
3232 	const struct ipv6_pinfo *np = inet6_sk(sk);
3233 	struct ipv6_txoptions *opt;
3234 	struct ipv6_pinfo *newnp;
3235 
3236 	newnp = inet6_sk(newsk);
3237 
3238 	rcu_read_lock();
3239 	opt = rcu_dereference(np->opt);
3240 	if (opt) {
3241 		opt = ipv6_dup_options(newsk, opt);
3242 		if (!opt)
3243 			net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3244 	}
3245 	RCU_INIT_POINTER(newnp->opt, opt);
3246 	rcu_read_unlock();
3247 }
3248 #endif
3249 
3250 static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3251 {
3252 	struct ip_options_rcu *inet_opt, *newopt = NULL;
3253 	const struct inet_sock *inet = inet_sk(sk);
3254 	struct inet_sock *newinet;
3255 
3256 	newinet = inet_sk(newsk);
3257 
3258 	rcu_read_lock();
3259 	inet_opt = rcu_dereference(inet->inet_opt);
3260 	if (inet_opt) {
3261 		newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
3262 				      inet_opt->opt.optlen, GFP_ATOMIC);
3263 		if (newopt)
3264 			memcpy(newopt, inet_opt, sizeof(*inet_opt) +
3265 			       inet_opt->opt.optlen);
3266 		else
3267 			net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3268 	}
3269 	RCU_INIT_POINTER(newinet->inet_opt, newopt);
3270 	rcu_read_unlock();
3271 }
3272 
3273 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3274 				 const struct mptcp_options_received *mp_opt,
3275 				 struct sock *ssk,
3276 				 struct request_sock *req)
3277 {
3278 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3279 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3280 	struct mptcp_subflow_context *subflow;
3281 	struct mptcp_sock *msk;
3282 
3283 	if (!nsk)
3284 		return NULL;
3285 
3286 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3287 	if (nsk->sk_family == AF_INET6)
3288 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3289 #endif
3290 
3291 	__mptcp_init_sock(nsk);
3292 
3293 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3294 	if (nsk->sk_family == AF_INET6)
3295 		mptcp_copy_ip6_options(nsk, sk);
3296 	else
3297 #endif
3298 		mptcp_copy_ip_options(nsk, sk);
3299 
3300 	msk = mptcp_sk(nsk);
3301 	WRITE_ONCE(msk->local_key, subflow_req->local_key);
3302 	WRITE_ONCE(msk->token, subflow_req->token);
3303 	msk->in_accept_queue = 1;
3304 	WRITE_ONCE(msk->fully_established, false);
3305 	if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3306 		WRITE_ONCE(msk->csum_enabled, true);
3307 
3308 	WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
3309 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3310 	WRITE_ONCE(msk->snd_una, msk->write_seq);
3311 	WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3312 	msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3313 	mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3314 
3315 	/* passive msk is created after the first/MPC subflow */
3316 	msk->subflow_id = 2;
3317 
3318 	sock_reset_flag(nsk, SOCK_RCU_FREE);
3319 	security_inet_csk_clone(nsk, req);
3320 
3321 	/* this can't race with mptcp_close(), as the msk is
3322 	 * not yet exposted to user-space
3323 	 */
3324 	mptcp_set_state(nsk, TCP_ESTABLISHED);
3325 
3326 	/* The msk maintain a ref to each subflow in the connections list */
3327 	WRITE_ONCE(msk->first, ssk);
3328 	subflow = mptcp_subflow_ctx(ssk);
3329 	list_add(&subflow->node, &msk->conn_list);
3330 	sock_hold(ssk);
3331 
3332 	/* new mpc subflow takes ownership of the newly
3333 	 * created mptcp socket
3334 	 */
3335 	mptcp_token_accept(subflow_req, msk);
3336 
3337 	/* set msk addresses early to ensure mptcp_pm_get_local_id()
3338 	 * uses the correct data
3339 	 */
3340 	mptcp_copy_inaddrs(nsk, ssk);
3341 	__mptcp_propagate_sndbuf(nsk, ssk);
3342 
3343 	mptcp_rcv_space_init(msk, ssk);
3344 
3345 	if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3346 		__mptcp_subflow_fully_established(msk, subflow, mp_opt);
3347 	bh_unlock_sock(nsk);
3348 
3349 	/* note: the newly allocated socket refcount is 2 now */
3350 	return nsk;
3351 }
3352 
3353 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3354 {
3355 	const struct tcp_sock *tp = tcp_sk(ssk);
3356 
3357 	msk->rcvspace_init = 1;
3358 	msk->rcvq_space.copied = 0;
3359 	msk->rcvq_space.rtt_us = 0;
3360 
3361 	msk->rcvq_space.time = tp->tcp_mstamp;
3362 
3363 	/* initial rcv_space offering made to peer */
3364 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3365 				      TCP_INIT_CWND * tp->advmss);
3366 	if (msk->rcvq_space.space == 0)
3367 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3368 }
3369 
3370 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3371 {
3372 	struct mptcp_subflow_context *subflow, *tmp;
3373 	struct sock *sk = (struct sock *)msk;
3374 
3375 	__mptcp_clear_xmit(sk);
3376 
3377 	/* join list will be eventually flushed (with rst) at sock lock release time */
3378 	mptcp_for_each_subflow_safe(msk, subflow, tmp)
3379 		__mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3380 
3381 	/* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3382 	mptcp_data_lock(sk);
3383 	skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3384 	__skb_queue_purge(&sk->sk_receive_queue);
3385 	skb_rbtree_purge(&msk->out_of_order_queue);
3386 	mptcp_data_unlock(sk);
3387 
3388 	/* move all the rx fwd alloc into the sk_mem_reclaim_final in
3389 	 * inet_sock_destruct() will dispose it
3390 	 */
3391 	sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
3392 	WRITE_ONCE(msk->rmem_fwd_alloc, 0);
3393 	mptcp_token_destroy(msk);
3394 	mptcp_pm_free_anno_list(msk);
3395 	mptcp_free_local_addr_list(msk);
3396 }
3397 
3398 static void mptcp_destroy(struct sock *sk)
3399 {
3400 	struct mptcp_sock *msk = mptcp_sk(sk);
3401 
3402 	/* allow the following to close even the initial subflow */
3403 	msk->free_first = 1;
3404 	mptcp_destroy_common(msk, 0);
3405 	sk_sockets_allocated_dec(sk);
3406 }
3407 
3408 void __mptcp_data_acked(struct sock *sk)
3409 {
3410 	if (!sock_owned_by_user(sk))
3411 		__mptcp_clean_una(sk);
3412 	else
3413 		__set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3414 }
3415 
3416 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3417 {
3418 	if (!mptcp_send_head(sk))
3419 		return;
3420 
3421 	if (!sock_owned_by_user(sk))
3422 		__mptcp_subflow_push_pending(sk, ssk, false);
3423 	else
3424 		__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3425 }
3426 
3427 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3428 				      BIT(MPTCP_RETRANSMIT) | \
3429 				      BIT(MPTCP_FLUSH_JOIN_LIST))
3430 
3431 /* processes deferred events and flush wmem */
3432 static void mptcp_release_cb(struct sock *sk)
3433 	__must_hold(&sk->sk_lock.slock)
3434 {
3435 	struct mptcp_sock *msk = mptcp_sk(sk);
3436 
3437 	for (;;) {
3438 		unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3439 		struct list_head join_list;
3440 
3441 		if (!flags)
3442 			break;
3443 
3444 		INIT_LIST_HEAD(&join_list);
3445 		list_splice_init(&msk->join_list, &join_list);
3446 
3447 		/* the following actions acquire the subflow socket lock
3448 		 *
3449 		 * 1) can't be invoked in atomic scope
3450 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3451 		 *    datapath acquires the msk socket spinlock while helding
3452 		 *    the subflow socket lock
3453 		 */
3454 		msk->cb_flags &= ~flags;
3455 		spin_unlock_bh(&sk->sk_lock.slock);
3456 
3457 		if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3458 			__mptcp_flush_join_list(sk, &join_list);
3459 		if (flags & BIT(MPTCP_PUSH_PENDING))
3460 			__mptcp_push_pending(sk, 0);
3461 		if (flags & BIT(MPTCP_RETRANSMIT))
3462 			__mptcp_retrans(sk);
3463 
3464 		cond_resched();
3465 		spin_lock_bh(&sk->sk_lock.slock);
3466 	}
3467 
3468 	if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3469 		__mptcp_clean_una_wakeup(sk);
3470 	if (unlikely(msk->cb_flags)) {
3471 		/* be sure to sync the msk state before taking actions
3472 		 * depending on sk_state (MPTCP_ERROR_REPORT)
3473 		 * On sk release avoid actions depending on the first subflow
3474 		 */
3475 		if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
3476 			__mptcp_sync_state(sk, msk->pending_state);
3477 		if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3478 			__mptcp_error_report(sk);
3479 		if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
3480 			__mptcp_sync_sndbuf(sk);
3481 	}
3482 
3483 	__mptcp_update_rmem(sk);
3484 }
3485 
3486 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3487  * TCP can't schedule delack timer before the subflow is fully established.
3488  * MPTCP uses the delack timer to do 3rd ack retransmissions
3489  */
3490 static void schedule_3rdack_retransmission(struct sock *ssk)
3491 {
3492 	struct inet_connection_sock *icsk = inet_csk(ssk);
3493 	struct tcp_sock *tp = tcp_sk(ssk);
3494 	unsigned long timeout;
3495 
3496 	if (mptcp_subflow_ctx(ssk)->fully_established)
3497 		return;
3498 
3499 	/* reschedule with a timeout above RTT, as we must look only for drop */
3500 	if (tp->srtt_us)
3501 		timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3502 	else
3503 		timeout = TCP_TIMEOUT_INIT;
3504 	timeout += jiffies;
3505 
3506 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3507 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3508 	icsk->icsk_ack.timeout = timeout;
3509 	sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3510 }
3511 
3512 void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3513 {
3514 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3515 	struct sock *sk = subflow->conn;
3516 
3517 	if (status & BIT(MPTCP_DELEGATE_SEND)) {
3518 		mptcp_data_lock(sk);
3519 		if (!sock_owned_by_user(sk))
3520 			__mptcp_subflow_push_pending(sk, ssk, true);
3521 		else
3522 			__set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3523 		mptcp_data_unlock(sk);
3524 	}
3525 	if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3526 		mptcp_data_lock(sk);
3527 		if (!sock_owned_by_user(sk))
3528 			__mptcp_sync_sndbuf(sk);
3529 		else
3530 			__set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3531 		mptcp_data_unlock(sk);
3532 	}
3533 	if (status & BIT(MPTCP_DELEGATE_ACK))
3534 		schedule_3rdack_retransmission(ssk);
3535 }
3536 
3537 static int mptcp_hash(struct sock *sk)
3538 {
3539 	/* should never be called,
3540 	 * we hash the TCP subflows not the MPTCP socket
3541 	 */
3542 	WARN_ON_ONCE(1);
3543 	return 0;
3544 }
3545 
3546 static void mptcp_unhash(struct sock *sk)
3547 {
3548 	/* called from sk_common_release(), but nothing to do here */
3549 }
3550 
3551 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3552 {
3553 	struct mptcp_sock *msk = mptcp_sk(sk);
3554 
3555 	pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3556 	if (WARN_ON_ONCE(!msk->first))
3557 		return -EINVAL;
3558 
3559 	return inet_csk_get_port(msk->first, snum);
3560 }
3561 
3562 void mptcp_finish_connect(struct sock *ssk)
3563 {
3564 	struct mptcp_subflow_context *subflow;
3565 	struct mptcp_sock *msk;
3566 	struct sock *sk;
3567 
3568 	subflow = mptcp_subflow_ctx(ssk);
3569 	sk = subflow->conn;
3570 	msk = mptcp_sk(sk);
3571 
3572 	pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3573 
3574 	subflow->map_seq = subflow->iasn;
3575 	subflow->map_subflow_seq = 1;
3576 
3577 	/* the socket is not connected yet, no msk/subflow ops can access/race
3578 	 * accessing the field below
3579 	 */
3580 	WRITE_ONCE(msk->local_key, subflow->local_key);
3581 
3582 	mptcp_pm_new_connection(msk, ssk, 0);
3583 }
3584 
3585 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3586 {
3587 	write_lock_bh(&sk->sk_callback_lock);
3588 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
3589 	sk_set_socket(sk, parent);
3590 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
3591 	write_unlock_bh(&sk->sk_callback_lock);
3592 }
3593 
3594 bool mptcp_finish_join(struct sock *ssk)
3595 {
3596 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3597 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3598 	struct sock *parent = (void *)msk;
3599 	bool ret = true;
3600 
3601 	pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3602 
3603 	/* mptcp socket already closing? */
3604 	if (!mptcp_is_fully_established(parent)) {
3605 		subflow->reset_reason = MPTCP_RST_EMPTCP;
3606 		return false;
3607 	}
3608 
3609 	/* active subflow, already present inside the conn_list */
3610 	if (!list_empty(&subflow->node)) {
3611 		mptcp_subflow_joined(msk, ssk);
3612 		mptcp_propagate_sndbuf(parent, ssk);
3613 		return true;
3614 	}
3615 
3616 	if (!mptcp_pm_allow_new_subflow(msk))
3617 		goto err_prohibited;
3618 
3619 	/* If we can't acquire msk socket lock here, let the release callback
3620 	 * handle it
3621 	 */
3622 	mptcp_data_lock(parent);
3623 	if (!sock_owned_by_user(parent)) {
3624 		ret = __mptcp_finish_join(msk, ssk);
3625 		if (ret) {
3626 			sock_hold(ssk);
3627 			list_add_tail(&subflow->node, &msk->conn_list);
3628 		}
3629 	} else {
3630 		sock_hold(ssk);
3631 		list_add_tail(&subflow->node, &msk->join_list);
3632 		__set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3633 	}
3634 	mptcp_data_unlock(parent);
3635 
3636 	if (!ret) {
3637 err_prohibited:
3638 		subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3639 		return false;
3640 	}
3641 
3642 	return true;
3643 }
3644 
3645 static void mptcp_shutdown(struct sock *sk, int how)
3646 {
3647 	pr_debug("sk=%p, how=%d\n", sk, how);
3648 
3649 	if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3650 		__mptcp_wr_shutdown(sk);
3651 }
3652 
3653 static int mptcp_forward_alloc_get(const struct sock *sk)
3654 {
3655 	return READ_ONCE(sk->sk_forward_alloc) +
3656 	       READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc);
3657 }
3658 
3659 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3660 {
3661 	const struct sock *sk = (void *)msk;
3662 	u64 delta;
3663 
3664 	if (sk->sk_state == TCP_LISTEN)
3665 		return -EINVAL;
3666 
3667 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3668 		return 0;
3669 
3670 	delta = msk->write_seq - v;
3671 	if (__mptcp_check_fallback(msk) && msk->first) {
3672 		struct tcp_sock *tp = tcp_sk(msk->first);
3673 
3674 		/* the first subflow is disconnected after close - see
3675 		 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3676 		 * so ignore that status, too.
3677 		 */
3678 		if (!((1 << msk->first->sk_state) &
3679 		      (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3680 			delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3681 	}
3682 	if (delta > INT_MAX)
3683 		delta = INT_MAX;
3684 
3685 	return (int)delta;
3686 }
3687 
3688 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3689 {
3690 	struct mptcp_sock *msk = mptcp_sk(sk);
3691 	bool slow;
3692 
3693 	switch (cmd) {
3694 	case SIOCINQ:
3695 		if (sk->sk_state == TCP_LISTEN)
3696 			return -EINVAL;
3697 
3698 		lock_sock(sk);
3699 		__mptcp_move_skbs(msk);
3700 		*karg = mptcp_inq_hint(sk);
3701 		release_sock(sk);
3702 		break;
3703 	case SIOCOUTQ:
3704 		slow = lock_sock_fast(sk);
3705 		*karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3706 		unlock_sock_fast(sk, slow);
3707 		break;
3708 	case SIOCOUTQNSD:
3709 		slow = lock_sock_fast(sk);
3710 		*karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3711 		unlock_sock_fast(sk, slow);
3712 		break;
3713 	default:
3714 		return -ENOIOCTLCMD;
3715 	}
3716 
3717 	return 0;
3718 }
3719 
3720 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3721 {
3722 	struct mptcp_subflow_context *subflow;
3723 	struct mptcp_sock *msk = mptcp_sk(sk);
3724 	int err = -EINVAL;
3725 	struct sock *ssk;
3726 
3727 	ssk = __mptcp_nmpc_sk(msk);
3728 	if (IS_ERR(ssk))
3729 		return PTR_ERR(ssk);
3730 
3731 	mptcp_set_state(sk, TCP_SYN_SENT);
3732 	subflow = mptcp_subflow_ctx(ssk);
3733 #ifdef CONFIG_TCP_MD5SIG
3734 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3735 	 * TCP option space.
3736 	 */
3737 	if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3738 		mptcp_subflow_early_fallback(msk, subflow);
3739 #endif
3740 	if (subflow->request_mptcp) {
3741 		if (mptcp_active_should_disable(sk)) {
3742 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
3743 			mptcp_subflow_early_fallback(msk, subflow);
3744 		} else if (mptcp_token_new_connect(ssk) < 0) {
3745 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
3746 			mptcp_subflow_early_fallback(msk, subflow);
3747 		}
3748 	}
3749 
3750 	WRITE_ONCE(msk->write_seq, subflow->idsn);
3751 	WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3752 	WRITE_ONCE(msk->snd_una, subflow->idsn);
3753 	if (likely(!__mptcp_check_fallback(msk)))
3754 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3755 
3756 	/* if reaching here via the fastopen/sendmsg path, the caller already
3757 	 * acquired the subflow socket lock, too.
3758 	 */
3759 	if (!msk->fastopening)
3760 		lock_sock(ssk);
3761 
3762 	/* the following mirrors closely a very small chunk of code from
3763 	 * __inet_stream_connect()
3764 	 */
3765 	if (ssk->sk_state != TCP_CLOSE)
3766 		goto out;
3767 
3768 	if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3769 		err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3770 		if (err)
3771 			goto out;
3772 	}
3773 
3774 	err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3775 	if (err < 0)
3776 		goto out;
3777 
3778 	inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3779 
3780 out:
3781 	if (!msk->fastopening)
3782 		release_sock(ssk);
3783 
3784 	/* on successful connect, the msk state will be moved to established by
3785 	 * subflow_finish_connect()
3786 	 */
3787 	if (unlikely(err)) {
3788 		/* avoid leaving a dangling token in an unconnected socket */
3789 		mptcp_token_destroy(msk);
3790 		mptcp_set_state(sk, TCP_CLOSE);
3791 		return err;
3792 	}
3793 
3794 	mptcp_copy_inaddrs(sk, ssk);
3795 	return 0;
3796 }
3797 
3798 static struct proto mptcp_prot = {
3799 	.name		= "MPTCP",
3800 	.owner		= THIS_MODULE,
3801 	.init		= mptcp_init_sock,
3802 	.connect	= mptcp_connect,
3803 	.disconnect	= mptcp_disconnect,
3804 	.close		= mptcp_close,
3805 	.setsockopt	= mptcp_setsockopt,
3806 	.getsockopt	= mptcp_getsockopt,
3807 	.shutdown	= mptcp_shutdown,
3808 	.destroy	= mptcp_destroy,
3809 	.sendmsg	= mptcp_sendmsg,
3810 	.ioctl		= mptcp_ioctl,
3811 	.recvmsg	= mptcp_recvmsg,
3812 	.release_cb	= mptcp_release_cb,
3813 	.hash		= mptcp_hash,
3814 	.unhash		= mptcp_unhash,
3815 	.get_port	= mptcp_get_port,
3816 	.forward_alloc_get	= mptcp_forward_alloc_get,
3817 	.stream_memory_free	= mptcp_stream_memory_free,
3818 	.sockets_allocated	= &mptcp_sockets_allocated,
3819 
3820 	.memory_allocated	= &tcp_memory_allocated,
3821 	.per_cpu_fw_alloc	= &tcp_memory_per_cpu_fw_alloc,
3822 
3823 	.memory_pressure	= &tcp_memory_pressure,
3824 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
3825 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
3826 	.sysctl_mem	= sysctl_tcp_mem,
3827 	.obj_size	= sizeof(struct mptcp_sock),
3828 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
3829 	.no_autobind	= true,
3830 };
3831 
3832 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3833 {
3834 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3835 	struct sock *ssk, *sk = sock->sk;
3836 	int err = -EINVAL;
3837 
3838 	lock_sock(sk);
3839 	ssk = __mptcp_nmpc_sk(msk);
3840 	if (IS_ERR(ssk)) {
3841 		err = PTR_ERR(ssk);
3842 		goto unlock;
3843 	}
3844 
3845 	if (sk->sk_family == AF_INET)
3846 		err = inet_bind_sk(ssk, uaddr, addr_len);
3847 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3848 	else if (sk->sk_family == AF_INET6)
3849 		err = inet6_bind_sk(ssk, uaddr, addr_len);
3850 #endif
3851 	if (!err)
3852 		mptcp_copy_inaddrs(sk, ssk);
3853 
3854 unlock:
3855 	release_sock(sk);
3856 	return err;
3857 }
3858 
3859 static int mptcp_listen(struct socket *sock, int backlog)
3860 {
3861 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3862 	struct sock *sk = sock->sk;
3863 	struct sock *ssk;
3864 	int err;
3865 
3866 	pr_debug("msk=%p\n", msk);
3867 
3868 	lock_sock(sk);
3869 
3870 	err = -EINVAL;
3871 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3872 		goto unlock;
3873 
3874 	ssk = __mptcp_nmpc_sk(msk);
3875 	if (IS_ERR(ssk)) {
3876 		err = PTR_ERR(ssk);
3877 		goto unlock;
3878 	}
3879 
3880 	mptcp_set_state(sk, TCP_LISTEN);
3881 	sock_set_flag(sk, SOCK_RCU_FREE);
3882 
3883 	lock_sock(ssk);
3884 	err = __inet_listen_sk(ssk, backlog);
3885 	release_sock(ssk);
3886 	mptcp_set_state(sk, inet_sk_state_load(ssk));
3887 
3888 	if (!err) {
3889 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3890 		mptcp_copy_inaddrs(sk, ssk);
3891 		mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3892 	}
3893 
3894 unlock:
3895 	release_sock(sk);
3896 	return err;
3897 }
3898 
3899 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3900 			       struct proto_accept_arg *arg)
3901 {
3902 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3903 	struct sock *ssk, *newsk;
3904 
3905 	pr_debug("msk=%p\n", msk);
3906 
3907 	/* Buggy applications can call accept on socket states other then LISTEN
3908 	 * but no need to allocate the first subflow just to error out.
3909 	 */
3910 	ssk = READ_ONCE(msk->first);
3911 	if (!ssk)
3912 		return -EINVAL;
3913 
3914 	pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
3915 	newsk = inet_csk_accept(ssk, arg);
3916 	if (!newsk)
3917 		return arg->err;
3918 
3919 	pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
3920 	if (sk_is_mptcp(newsk)) {
3921 		struct mptcp_subflow_context *subflow;
3922 		struct sock *new_mptcp_sock;
3923 
3924 		subflow = mptcp_subflow_ctx(newsk);
3925 		new_mptcp_sock = subflow->conn;
3926 
3927 		/* is_mptcp should be false if subflow->conn is missing, see
3928 		 * subflow_syn_recv_sock()
3929 		 */
3930 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
3931 			tcp_sk(newsk)->is_mptcp = 0;
3932 			goto tcpfallback;
3933 		}
3934 
3935 		newsk = new_mptcp_sock;
3936 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3937 
3938 		newsk->sk_kern_sock = arg->kern;
3939 		lock_sock(newsk);
3940 		__inet_accept(sock, newsock, newsk);
3941 
3942 		set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3943 		msk = mptcp_sk(newsk);
3944 		msk->in_accept_queue = 0;
3945 
3946 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
3947 		 * This is needed so NOSPACE flag can be set from tcp stack.
3948 		 */
3949 		mptcp_for_each_subflow(msk, subflow) {
3950 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3951 
3952 			if (!ssk->sk_socket)
3953 				mptcp_sock_graft(ssk, newsock);
3954 		}
3955 
3956 		/* Do late cleanup for the first subflow as necessary. Also
3957 		 * deal with bad peers not doing a complete shutdown.
3958 		 */
3959 		if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3960 			__mptcp_close_ssk(newsk, msk->first,
3961 					  mptcp_subflow_ctx(msk->first), 0);
3962 			if (unlikely(list_is_singular(&msk->conn_list)))
3963 				mptcp_set_state(newsk, TCP_CLOSE);
3964 		}
3965 	} else {
3966 tcpfallback:
3967 		newsk->sk_kern_sock = arg->kern;
3968 		lock_sock(newsk);
3969 		__inet_accept(sock, newsock, newsk);
3970 		/* we are being invoked after accepting a non-mp-capable
3971 		 * flow: sk is a tcp_sk, not an mptcp one.
3972 		 *
3973 		 * Hand the socket over to tcp so all further socket ops
3974 		 * bypass mptcp.
3975 		 */
3976 		WRITE_ONCE(newsock->sk->sk_socket->ops,
3977 			   mptcp_fallback_tcp_ops(newsock->sk));
3978 	}
3979 	release_sock(newsk);
3980 
3981 	return 0;
3982 }
3983 
3984 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3985 {
3986 	struct sock *sk = (struct sock *)msk;
3987 
3988 	if (__mptcp_stream_is_writeable(sk, 1))
3989 		return EPOLLOUT | EPOLLWRNORM;
3990 
3991 	set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
3992 	smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
3993 	if (__mptcp_stream_is_writeable(sk, 1))
3994 		return EPOLLOUT | EPOLLWRNORM;
3995 
3996 	return 0;
3997 }
3998 
3999 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
4000 			   struct poll_table_struct *wait)
4001 {
4002 	struct sock *sk = sock->sk;
4003 	struct mptcp_sock *msk;
4004 	__poll_t mask = 0;
4005 	u8 shutdown;
4006 	int state;
4007 
4008 	msk = mptcp_sk(sk);
4009 	sock_poll_wait(file, sock, wait);
4010 
4011 	state = inet_sk_state_load(sk);
4012 	pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
4013 	if (state == TCP_LISTEN) {
4014 		struct sock *ssk = READ_ONCE(msk->first);
4015 
4016 		if (WARN_ON_ONCE(!ssk))
4017 			return 0;
4018 
4019 		return inet_csk_listen_poll(ssk);
4020 	}
4021 
4022 	shutdown = READ_ONCE(sk->sk_shutdown);
4023 	if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
4024 		mask |= EPOLLHUP;
4025 	if (shutdown & RCV_SHUTDOWN)
4026 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
4027 
4028 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4029 		mask |= mptcp_check_readable(sk);
4030 		if (shutdown & SEND_SHUTDOWN)
4031 			mask |= EPOLLOUT | EPOLLWRNORM;
4032 		else
4033 			mask |= mptcp_check_writeable(msk);
4034 	} else if (state == TCP_SYN_SENT &&
4035 		   inet_test_bit(DEFER_CONNECT, sk)) {
4036 		/* cf tcp_poll() note about TFO */
4037 		mask |= EPOLLOUT | EPOLLWRNORM;
4038 	}
4039 
4040 	/* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
4041 	smp_rmb();
4042 	if (READ_ONCE(sk->sk_err))
4043 		mask |= EPOLLERR;
4044 
4045 	return mask;
4046 }
4047 
4048 static const struct proto_ops mptcp_stream_ops = {
4049 	.family		   = PF_INET,
4050 	.owner		   = THIS_MODULE,
4051 	.release	   = inet_release,
4052 	.bind		   = mptcp_bind,
4053 	.connect	   = inet_stream_connect,
4054 	.socketpair	   = sock_no_socketpair,
4055 	.accept		   = mptcp_stream_accept,
4056 	.getname	   = inet_getname,
4057 	.poll		   = mptcp_poll,
4058 	.ioctl		   = inet_ioctl,
4059 	.gettstamp	   = sock_gettstamp,
4060 	.listen		   = mptcp_listen,
4061 	.shutdown	   = inet_shutdown,
4062 	.setsockopt	   = sock_common_setsockopt,
4063 	.getsockopt	   = sock_common_getsockopt,
4064 	.sendmsg	   = inet_sendmsg,
4065 	.recvmsg	   = inet_recvmsg,
4066 	.mmap		   = sock_no_mmap,
4067 	.set_rcvlowat	   = mptcp_set_rcvlowat,
4068 };
4069 
4070 static struct inet_protosw mptcp_protosw = {
4071 	.type		= SOCK_STREAM,
4072 	.protocol	= IPPROTO_MPTCP,
4073 	.prot		= &mptcp_prot,
4074 	.ops		= &mptcp_stream_ops,
4075 	.flags		= INET_PROTOSW_ICSK,
4076 };
4077 
4078 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4079 {
4080 	struct mptcp_delegated_action *delegated;
4081 	struct mptcp_subflow_context *subflow;
4082 	int work_done = 0;
4083 
4084 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
4085 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4086 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4087 
4088 		bh_lock_sock_nested(ssk);
4089 		if (!sock_owned_by_user(ssk)) {
4090 			mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4091 		} else {
4092 			/* tcp_release_cb_override already processed
4093 			 * the action or will do at next release_sock().
4094 			 * In both case must dequeue the subflow here - on the same
4095 			 * CPU that scheduled it.
4096 			 */
4097 			smp_wmb();
4098 			clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4099 		}
4100 		bh_unlock_sock(ssk);
4101 		sock_put(ssk);
4102 
4103 		if (++work_done == budget)
4104 			return budget;
4105 	}
4106 
4107 	/* always provide a 0 'work_done' argument, so that napi_complete_done
4108 	 * will not try accessing the NULL napi->dev ptr
4109 	 */
4110 	napi_complete_done(napi, 0);
4111 	return work_done;
4112 }
4113 
4114 void __init mptcp_proto_init(void)
4115 {
4116 	struct mptcp_delegated_action *delegated;
4117 	int cpu;
4118 
4119 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4120 
4121 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4122 		panic("Failed to allocate MPTCP pcpu counter\n");
4123 
4124 	init_dummy_netdev(&mptcp_napi_dev);
4125 	for_each_possible_cpu(cpu) {
4126 		delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4127 		INIT_LIST_HEAD(&delegated->head);
4128 		netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
4129 				  mptcp_napi_poll);
4130 		napi_enable(&delegated->napi);
4131 	}
4132 
4133 	mptcp_subflow_init();
4134 	mptcp_pm_init();
4135 	mptcp_sched_init();
4136 	mptcp_token_init();
4137 
4138 	if (proto_register(&mptcp_prot, 1) != 0)
4139 		panic("Failed to register MPTCP proto.\n");
4140 
4141 	inet_register_protosw(&mptcp_protosw);
4142 
4143 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4144 }
4145 
4146 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4147 static const struct proto_ops mptcp_v6_stream_ops = {
4148 	.family		   = PF_INET6,
4149 	.owner		   = THIS_MODULE,
4150 	.release	   = inet6_release,
4151 	.bind		   = mptcp_bind,
4152 	.connect	   = inet_stream_connect,
4153 	.socketpair	   = sock_no_socketpair,
4154 	.accept		   = mptcp_stream_accept,
4155 	.getname	   = inet6_getname,
4156 	.poll		   = mptcp_poll,
4157 	.ioctl		   = inet6_ioctl,
4158 	.gettstamp	   = sock_gettstamp,
4159 	.listen		   = mptcp_listen,
4160 	.shutdown	   = inet_shutdown,
4161 	.setsockopt	   = sock_common_setsockopt,
4162 	.getsockopt	   = sock_common_getsockopt,
4163 	.sendmsg	   = inet6_sendmsg,
4164 	.recvmsg	   = inet6_recvmsg,
4165 	.mmap		   = sock_no_mmap,
4166 #ifdef CONFIG_COMPAT
4167 	.compat_ioctl	   = inet6_compat_ioctl,
4168 #endif
4169 	.set_rcvlowat	   = mptcp_set_rcvlowat,
4170 };
4171 
4172 static struct proto mptcp_v6_prot;
4173 
4174 static struct inet_protosw mptcp_v6_protosw = {
4175 	.type		= SOCK_STREAM,
4176 	.protocol	= IPPROTO_MPTCP,
4177 	.prot		= &mptcp_v6_prot,
4178 	.ops		= &mptcp_v6_stream_ops,
4179 	.flags		= INET_PROTOSW_ICSK,
4180 };
4181 
4182 int __init mptcp_proto_v6_init(void)
4183 {
4184 	int err;
4185 
4186 	mptcp_v6_prot = mptcp_prot;
4187 	strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
4188 	mptcp_v6_prot.slab = NULL;
4189 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4190 	mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4191 
4192 	err = proto_register(&mptcp_v6_prot, 1);
4193 	if (err)
4194 		return err;
4195 
4196 	err = inet6_register_protosw(&mptcp_v6_protosw);
4197 	if (err)
4198 		proto_unregister(&mptcp_v6_prot);
4199 
4200 	return err;
4201 }
4202 #endif
4203