xref: /openbmc/linux/net/mptcp/protocol.c (revision de6da33e)
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.h>
19 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
22 #endif
23 #include <net/mptcp.h>
24 #include <net/xfrm.h>
25 #include "protocol.h"
26 #include "mib.h"
27 
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/mptcp.h>
30 
31 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
32 struct mptcp6_sock {
33 	struct mptcp_sock msk;
34 	struct ipv6_pinfo np;
35 };
36 #endif
37 
38 struct mptcp_skb_cb {
39 	u64 map_seq;
40 	u64 end_seq;
41 	u32 offset;
42 	u8  has_rxtstamp:1;
43 };
44 
45 #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
46 
47 enum {
48 	MPTCP_CMSG_TS = BIT(0),
49 };
50 
51 static struct percpu_counter mptcp_sockets_allocated;
52 
53 static void __mptcp_destroy_sock(struct sock *sk);
54 static void __mptcp_check_send_data_fin(struct sock *sk);
55 
56 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
57 static struct net_device mptcp_napi_dev;
58 
59 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
60  * completed yet or has failed, return the subflow socket.
61  * Otherwise return NULL.
62  */
63 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
64 {
65 	if (!msk->subflow || READ_ONCE(msk->can_ack))
66 		return NULL;
67 
68 	return msk->subflow;
69 }
70 
71 /* Returns end sequence number of the receiver's advertised window */
72 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
73 {
74 	return READ_ONCE(msk->wnd_end);
75 }
76 
77 static bool mptcp_is_tcpsk(struct sock *sk)
78 {
79 	struct socket *sock = sk->sk_socket;
80 
81 	if (unlikely(sk->sk_prot == &tcp_prot)) {
82 		/* we are being invoked after mptcp_accept() has
83 		 * accepted a non-mp-capable flow: sk is a tcp_sk,
84 		 * not an mptcp one.
85 		 *
86 		 * Hand the socket over to tcp so all further socket ops
87 		 * bypass mptcp.
88 		 */
89 		sock->ops = &inet_stream_ops;
90 		return true;
91 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
92 	} else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
93 		sock->ops = &inet6_stream_ops;
94 		return true;
95 #endif
96 	}
97 
98 	return false;
99 }
100 
101 static int __mptcp_socket_create(struct mptcp_sock *msk)
102 {
103 	struct mptcp_subflow_context *subflow;
104 	struct sock *sk = (struct sock *)msk;
105 	struct socket *ssock;
106 	int err;
107 
108 	err = mptcp_subflow_create_socket(sk, &ssock);
109 	if (err)
110 		return err;
111 
112 	msk->first = ssock->sk;
113 	msk->subflow = ssock;
114 	subflow = mptcp_subflow_ctx(ssock->sk);
115 	list_add(&subflow->node, &msk->conn_list);
116 	sock_hold(ssock->sk);
117 	subflow->request_mptcp = 1;
118 	mptcp_sock_graft(msk->first, sk->sk_socket);
119 
120 	return 0;
121 }
122 
123 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
124 {
125 	sk_drops_add(sk, skb);
126 	__kfree_skb(skb);
127 }
128 
129 static void mptcp_rmem_charge(struct sock *sk, int size)
130 {
131 	mptcp_sk(sk)->rmem_fwd_alloc -= size;
132 }
133 
134 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
135 			       struct sk_buff *from)
136 {
137 	bool fragstolen;
138 	int delta;
139 
140 	if (MPTCP_SKB_CB(from)->offset ||
141 	    !skb_try_coalesce(to, from, &fragstolen, &delta))
142 		return false;
143 
144 	pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
145 		 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
146 		 to->len, MPTCP_SKB_CB(from)->end_seq);
147 	MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
148 	kfree_skb_partial(from, fragstolen);
149 	atomic_add(delta, &sk->sk_rmem_alloc);
150 	mptcp_rmem_charge(sk, delta);
151 	return true;
152 }
153 
154 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
155 				   struct sk_buff *from)
156 {
157 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
158 		return false;
159 
160 	return mptcp_try_coalesce((struct sock *)msk, to, from);
161 }
162 
163 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
164 {
165 	amount >>= SK_MEM_QUANTUM_SHIFT;
166 	mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
167 	__sk_mem_reduce_allocated(sk, amount);
168 }
169 
170 static void mptcp_rmem_uncharge(struct sock *sk, int size)
171 {
172 	struct mptcp_sock *msk = mptcp_sk(sk);
173 	int reclaimable;
174 
175 	msk->rmem_fwd_alloc += size;
176 	reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
177 
178 	/* see sk_mem_uncharge() for the rationale behind the following schema */
179 	if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD))
180 		__mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK);
181 }
182 
183 static void mptcp_rfree(struct sk_buff *skb)
184 {
185 	unsigned int len = skb->truesize;
186 	struct sock *sk = skb->sk;
187 
188 	atomic_sub(len, &sk->sk_rmem_alloc);
189 	mptcp_rmem_uncharge(sk, len);
190 }
191 
192 static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
193 {
194 	skb_orphan(skb);
195 	skb->sk = sk;
196 	skb->destructor = mptcp_rfree;
197 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
198 	mptcp_rmem_charge(sk, skb->truesize);
199 }
200 
201 /* "inspired" by tcp_data_queue_ofo(), main differences:
202  * - use mptcp seqs
203  * - don't cope with sacks
204  */
205 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
206 {
207 	struct sock *sk = (struct sock *)msk;
208 	struct rb_node **p, *parent;
209 	u64 seq, end_seq, max_seq;
210 	struct sk_buff *skb1;
211 
212 	seq = MPTCP_SKB_CB(skb)->map_seq;
213 	end_seq = MPTCP_SKB_CB(skb)->end_seq;
214 	max_seq = READ_ONCE(msk->rcv_wnd_sent);
215 
216 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
217 		 RB_EMPTY_ROOT(&msk->out_of_order_queue));
218 	if (after64(end_seq, max_seq)) {
219 		/* out of window */
220 		mptcp_drop(sk, skb);
221 		pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
222 			 (unsigned long long)end_seq - (unsigned long)max_seq,
223 			 (unsigned long long)msk->rcv_wnd_sent);
224 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
225 		return;
226 	}
227 
228 	p = &msk->out_of_order_queue.rb_node;
229 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
230 	if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
231 		rb_link_node(&skb->rbnode, NULL, p);
232 		rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
233 		msk->ooo_last_skb = skb;
234 		goto end;
235 	}
236 
237 	/* with 2 subflows, adding at end of ooo queue is quite likely
238 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
239 	 */
240 	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
241 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
242 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
243 		return;
244 	}
245 
246 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
247 	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
248 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
249 		parent = &msk->ooo_last_skb->rbnode;
250 		p = &parent->rb_right;
251 		goto insert;
252 	}
253 
254 	/* Find place to insert this segment. Handle overlaps on the way. */
255 	parent = NULL;
256 	while (*p) {
257 		parent = *p;
258 		skb1 = rb_to_skb(parent);
259 		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
260 			p = &parent->rb_left;
261 			continue;
262 		}
263 		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
264 			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
265 				/* All the bits are present. Drop. */
266 				mptcp_drop(sk, skb);
267 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
268 				return;
269 			}
270 			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
271 				/* partial overlap:
272 				 *     |     skb      |
273 				 *  |     skb1    |
274 				 * continue traversing
275 				 */
276 			} else {
277 				/* skb's seq == skb1's seq and skb covers skb1.
278 				 * Replace skb1 with skb.
279 				 */
280 				rb_replace_node(&skb1->rbnode, &skb->rbnode,
281 						&msk->out_of_order_queue);
282 				mptcp_drop(sk, skb1);
283 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
284 				goto merge_right;
285 			}
286 		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
287 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
288 			return;
289 		}
290 		p = &parent->rb_right;
291 	}
292 
293 insert:
294 	/* Insert segment into RB tree. */
295 	rb_link_node(&skb->rbnode, parent, p);
296 	rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
297 
298 merge_right:
299 	/* Remove other segments covered by skb. */
300 	while ((skb1 = skb_rb_next(skb)) != NULL) {
301 		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
302 			break;
303 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
304 		mptcp_drop(sk, skb1);
305 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
306 	}
307 	/* If there is no skb after us, we are the last_skb ! */
308 	if (!skb1)
309 		msk->ooo_last_skb = skb;
310 
311 end:
312 	skb_condense(skb);
313 	mptcp_set_owner_r(skb, sk);
314 }
315 
316 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
317 {
318 	struct mptcp_sock *msk = mptcp_sk(sk);
319 	int amt, amount;
320 
321 	if (size < msk->rmem_fwd_alloc)
322 		return true;
323 
324 	amt = sk_mem_pages(size);
325 	amount = amt << SK_MEM_QUANTUM_SHIFT;
326 	msk->rmem_fwd_alloc += amount;
327 	if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) {
328 		if (ssk->sk_forward_alloc < amount) {
329 			msk->rmem_fwd_alloc -= amount;
330 			return false;
331 		}
332 
333 		ssk->sk_forward_alloc -= amount;
334 	}
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 		goto drop;
355 
356 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
357 
358 	/* the skb map_seq accounts for the skb offset:
359 	 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
360 	 * value
361 	 */
362 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
363 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
364 	MPTCP_SKB_CB(skb)->offset = offset;
365 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
366 
367 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
368 		/* in sequence */
369 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
370 		tail = skb_peek_tail(&sk->sk_receive_queue);
371 		if (tail && mptcp_try_coalesce(sk, tail, skb))
372 			return true;
373 
374 		mptcp_set_owner_r(skb, sk);
375 		__skb_queue_tail(&sk->sk_receive_queue, skb);
376 		return true;
377 	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
378 		mptcp_data_queue_ofo(msk, skb);
379 		return false;
380 	}
381 
382 	/* old data, keep it simple and drop the whole pkt, sender
383 	 * will retransmit as needed, if needed.
384 	 */
385 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
386 drop:
387 	mptcp_drop(sk, skb);
388 	return false;
389 }
390 
391 static void mptcp_stop_timer(struct sock *sk)
392 {
393 	struct inet_connection_sock *icsk = inet_csk(sk);
394 
395 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
396 	mptcp_sk(sk)->timer_ival = 0;
397 }
398 
399 static void mptcp_close_wake_up(struct sock *sk)
400 {
401 	if (sock_flag(sk, SOCK_DEAD))
402 		return;
403 
404 	sk->sk_state_change(sk);
405 	if (sk->sk_shutdown == SHUTDOWN_MASK ||
406 	    sk->sk_state == TCP_CLOSE)
407 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
408 	else
409 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
410 }
411 
412 static bool mptcp_pending_data_fin_ack(struct sock *sk)
413 {
414 	struct mptcp_sock *msk = mptcp_sk(sk);
415 
416 	return !__mptcp_check_fallback(msk) &&
417 	       ((1 << sk->sk_state) &
418 		(TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
419 	       msk->write_seq == READ_ONCE(msk->snd_una);
420 }
421 
422 static void mptcp_check_data_fin_ack(struct sock *sk)
423 {
424 	struct mptcp_sock *msk = mptcp_sk(sk);
425 
426 	/* Look for an acknowledged DATA_FIN */
427 	if (mptcp_pending_data_fin_ack(sk)) {
428 		WRITE_ONCE(msk->snd_data_fin_enable, 0);
429 
430 		switch (sk->sk_state) {
431 		case TCP_FIN_WAIT1:
432 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
433 			break;
434 		case TCP_CLOSING:
435 		case TCP_LAST_ACK:
436 			inet_sk_state_store(sk, TCP_CLOSE);
437 			break;
438 		}
439 
440 		mptcp_close_wake_up(sk);
441 	}
442 }
443 
444 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
445 {
446 	struct mptcp_sock *msk = mptcp_sk(sk);
447 
448 	if (READ_ONCE(msk->rcv_data_fin) &&
449 	    ((1 << sk->sk_state) &
450 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
451 		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
452 
453 		if (msk->ack_seq == rcv_data_fin_seq) {
454 			if (seq)
455 				*seq = rcv_data_fin_seq;
456 
457 			return true;
458 		}
459 	}
460 
461 	return false;
462 }
463 
464 static void mptcp_set_datafin_timeout(const struct sock *sk)
465 {
466 	struct inet_connection_sock *icsk = inet_csk(sk);
467 
468 	mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX,
469 				       TCP_RTO_MIN << icsk->icsk_retransmits);
470 }
471 
472 static void __mptcp_set_timeout(struct sock *sk, long tout)
473 {
474 	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
475 }
476 
477 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
478 {
479 	const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
480 
481 	return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
482 	       inet_csk(ssk)->icsk_timeout - jiffies : 0;
483 }
484 
485 static void mptcp_set_timeout(struct sock *sk)
486 {
487 	struct mptcp_subflow_context *subflow;
488 	long tout = 0;
489 
490 	mptcp_for_each_subflow(mptcp_sk(sk), subflow)
491 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
492 	__mptcp_set_timeout(sk, tout);
493 }
494 
495 static bool tcp_can_send_ack(const struct sock *ssk)
496 {
497 	return !((1 << inet_sk_state_load(ssk)) &
498 	       (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
499 }
500 
501 void mptcp_subflow_send_ack(struct sock *ssk)
502 {
503 	bool slow;
504 
505 	slow = lock_sock_fast(ssk);
506 	if (tcp_can_send_ack(ssk))
507 		tcp_send_ack(ssk);
508 	unlock_sock_fast(ssk, slow);
509 }
510 
511 static void mptcp_send_ack(struct mptcp_sock *msk)
512 {
513 	struct mptcp_subflow_context *subflow;
514 
515 	mptcp_for_each_subflow(msk, subflow)
516 		mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
517 }
518 
519 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
520 {
521 	bool slow;
522 
523 	slow = lock_sock_fast(ssk);
524 	if (tcp_can_send_ack(ssk))
525 		tcp_cleanup_rbuf(ssk, 1);
526 	unlock_sock_fast(ssk, slow);
527 }
528 
529 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
530 {
531 	const struct inet_connection_sock *icsk = inet_csk(ssk);
532 	u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
533 	const struct tcp_sock *tp = tcp_sk(ssk);
534 
535 	return (ack_pending & ICSK_ACK_SCHED) &&
536 		((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
537 		  READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
538 		 (rx_empty && ack_pending &
539 			      (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
540 }
541 
542 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
543 {
544 	int old_space = READ_ONCE(msk->old_wspace);
545 	struct mptcp_subflow_context *subflow;
546 	struct sock *sk = (struct sock *)msk;
547 	int space =  __mptcp_space(sk);
548 	bool cleanup, rx_empty;
549 
550 	cleanup = (space > 0) && (space >= (old_space << 1));
551 	rx_empty = !__mptcp_rmem(sk);
552 
553 	mptcp_for_each_subflow(msk, subflow) {
554 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
555 
556 		if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
557 			mptcp_subflow_cleanup_rbuf(ssk);
558 	}
559 }
560 
561 static bool mptcp_check_data_fin(struct sock *sk)
562 {
563 	struct mptcp_sock *msk = mptcp_sk(sk);
564 	u64 rcv_data_fin_seq;
565 	bool ret = false;
566 
567 	if (__mptcp_check_fallback(msk))
568 		return ret;
569 
570 	/* Need to ack a DATA_FIN received from a peer while this side
571 	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
572 	 * msk->rcv_data_fin was set when parsing the incoming options
573 	 * at the subflow level and the msk lock was not held, so this
574 	 * is the first opportunity to act on the DATA_FIN and change
575 	 * the msk state.
576 	 *
577 	 * If we are caught up to the sequence number of the incoming
578 	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
579 	 * not caught up, do nothing and let the recv code send DATA_ACK
580 	 * when catching up.
581 	 */
582 
583 	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
584 		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
585 		WRITE_ONCE(msk->rcv_data_fin, 0);
586 
587 		sk->sk_shutdown |= RCV_SHUTDOWN;
588 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
589 
590 		switch (sk->sk_state) {
591 		case TCP_ESTABLISHED:
592 			inet_sk_state_store(sk, TCP_CLOSE_WAIT);
593 			break;
594 		case TCP_FIN_WAIT1:
595 			inet_sk_state_store(sk, TCP_CLOSING);
596 			break;
597 		case TCP_FIN_WAIT2:
598 			inet_sk_state_store(sk, TCP_CLOSE);
599 			break;
600 		default:
601 			/* Other states not expected */
602 			WARN_ON_ONCE(1);
603 			break;
604 		}
605 
606 		ret = true;
607 		mptcp_send_ack(msk);
608 		mptcp_close_wake_up(sk);
609 	}
610 	return ret;
611 }
612 
613 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
614 					   struct sock *ssk,
615 					   unsigned int *bytes)
616 {
617 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
618 	struct sock *sk = (struct sock *)msk;
619 	unsigned int moved = 0;
620 	bool more_data_avail;
621 	struct tcp_sock *tp;
622 	bool done = false;
623 	int sk_rbuf;
624 
625 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
626 
627 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
628 		int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
629 
630 		if (unlikely(ssk_rbuf > sk_rbuf)) {
631 			WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
632 			sk_rbuf = ssk_rbuf;
633 		}
634 	}
635 
636 	pr_debug("msk=%p ssk=%p", msk, ssk);
637 	tp = tcp_sk(ssk);
638 	do {
639 		u32 map_remaining, offset;
640 		u32 seq = tp->copied_seq;
641 		struct sk_buff *skb;
642 		bool fin;
643 
644 		/* try to move as much data as available */
645 		map_remaining = subflow->map_data_len -
646 				mptcp_subflow_get_map_offset(subflow);
647 
648 		skb = skb_peek(&ssk->sk_receive_queue);
649 		if (!skb) {
650 			/* if no data is found, a racing workqueue/recvmsg
651 			 * already processed the new data, stop here or we
652 			 * can enter an infinite loop
653 			 */
654 			if (!moved)
655 				done = true;
656 			break;
657 		}
658 
659 		if (__mptcp_check_fallback(msk)) {
660 			/* if we are running under the workqueue, TCP could have
661 			 * collapsed skbs between dummy map creation and now
662 			 * be sure to adjust the size
663 			 */
664 			map_remaining = skb->len;
665 			subflow->map_data_len = skb->len;
666 		}
667 
668 		offset = seq - TCP_SKB_CB(skb)->seq;
669 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
670 		if (fin) {
671 			done = true;
672 			seq++;
673 		}
674 
675 		if (offset < skb->len) {
676 			size_t len = skb->len - offset;
677 
678 			if (tp->urg_data)
679 				done = true;
680 
681 			if (__mptcp_move_skb(msk, ssk, skb, offset, len))
682 				moved += len;
683 			seq += len;
684 
685 			if (WARN_ON_ONCE(map_remaining < len))
686 				break;
687 		} else {
688 			WARN_ON_ONCE(!fin);
689 			sk_eat_skb(ssk, skb);
690 			done = true;
691 		}
692 
693 		WRITE_ONCE(tp->copied_seq, seq);
694 		more_data_avail = mptcp_subflow_data_available(ssk);
695 
696 		if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
697 			done = true;
698 			break;
699 		}
700 	} while (more_data_avail);
701 
702 	*bytes += moved;
703 	return done;
704 }
705 
706 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
707 {
708 	struct sock *sk = (struct sock *)msk;
709 	struct sk_buff *skb, *tail;
710 	bool moved = false;
711 	struct rb_node *p;
712 	u64 end_seq;
713 
714 	p = rb_first(&msk->out_of_order_queue);
715 	pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
716 	while (p) {
717 		skb = rb_to_skb(p);
718 		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
719 			break;
720 
721 		p = rb_next(p);
722 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
723 
724 		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
725 				      msk->ack_seq))) {
726 			mptcp_drop(sk, skb);
727 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
728 			continue;
729 		}
730 
731 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
732 		tail = skb_peek_tail(&sk->sk_receive_queue);
733 		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
734 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
735 
736 			/* skip overlapping data, if any */
737 			pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
738 				 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
739 				 delta);
740 			MPTCP_SKB_CB(skb)->offset += delta;
741 			__skb_queue_tail(&sk->sk_receive_queue, skb);
742 		}
743 		msk->ack_seq = end_seq;
744 		moved = true;
745 	}
746 	return moved;
747 }
748 
749 /* In most cases we will be able to lock the mptcp socket.  If its already
750  * owned, we need to defer to the work queue to avoid ABBA deadlock.
751  */
752 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
753 {
754 	struct sock *sk = (struct sock *)msk;
755 	unsigned int moved = 0;
756 
757 	__mptcp_move_skbs_from_subflow(msk, ssk, &moved);
758 	__mptcp_ofo_queue(msk);
759 	if (unlikely(ssk->sk_err)) {
760 		if (!sock_owned_by_user(sk))
761 			__mptcp_error_report(sk);
762 		else
763 			set_bit(MPTCP_ERROR_REPORT,  &msk->flags);
764 	}
765 
766 	/* If the moves have caught up with the DATA_FIN sequence number
767 	 * it's time to ack the DATA_FIN and change socket state, but
768 	 * this is not a good place to change state. Let the workqueue
769 	 * do it.
770 	 */
771 	if (mptcp_pending_data_fin(sk, NULL))
772 		mptcp_schedule_work(sk);
773 	return moved > 0;
774 }
775 
776 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
777 {
778 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
779 	struct mptcp_sock *msk = mptcp_sk(sk);
780 	int sk_rbuf, ssk_rbuf;
781 
782 	/* The peer can send data while we are shutting down this
783 	 * subflow at msk destruction time, but we must avoid enqueuing
784 	 * more data to the msk receive queue
785 	 */
786 	if (unlikely(subflow->disposable))
787 		return;
788 
789 	ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
790 	sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
791 	if (unlikely(ssk_rbuf > sk_rbuf))
792 		sk_rbuf = ssk_rbuf;
793 
794 	/* over limit? can't append more skbs to msk, Also, no need to wake-up*/
795 	if (__mptcp_rmem(sk) > sk_rbuf) {
796 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
797 		return;
798 	}
799 
800 	/* Wake-up the reader only for in-sequence data */
801 	mptcp_data_lock(sk);
802 	if (move_skbs_to_msk(msk, ssk))
803 		sk->sk_data_ready(sk);
804 
805 	mptcp_data_unlock(sk);
806 }
807 
808 static bool mptcp_do_flush_join_list(struct mptcp_sock *msk)
809 {
810 	struct mptcp_subflow_context *subflow;
811 	bool ret = false;
812 
813 	if (likely(list_empty(&msk->join_list)))
814 		return false;
815 
816 	spin_lock_bh(&msk->join_list_lock);
817 	list_for_each_entry(subflow, &msk->join_list, node) {
818 		u32 sseq = READ_ONCE(subflow->setsockopt_seq);
819 
820 		mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow));
821 		if (READ_ONCE(msk->setsockopt_seq) != sseq)
822 			ret = true;
823 	}
824 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
825 	spin_unlock_bh(&msk->join_list_lock);
826 
827 	return ret;
828 }
829 
830 void __mptcp_flush_join_list(struct mptcp_sock *msk)
831 {
832 	if (likely(!mptcp_do_flush_join_list(msk)))
833 		return;
834 
835 	if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags))
836 		mptcp_schedule_work((struct sock *)msk);
837 }
838 
839 static void mptcp_flush_join_list(struct mptcp_sock *msk)
840 {
841 	bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags);
842 
843 	might_sleep();
844 
845 	if (!mptcp_do_flush_join_list(msk) && !sync_needed)
846 		return;
847 
848 	mptcp_sockopt_sync_all(msk);
849 }
850 
851 static bool mptcp_timer_pending(struct sock *sk)
852 {
853 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
854 }
855 
856 static void mptcp_reset_timer(struct sock *sk)
857 {
858 	struct inet_connection_sock *icsk = inet_csk(sk);
859 	unsigned long tout;
860 
861 	/* prevent rescheduling on close */
862 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
863 		return;
864 
865 	tout = mptcp_sk(sk)->timer_ival;
866 	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
867 }
868 
869 bool mptcp_schedule_work(struct sock *sk)
870 {
871 	if (inet_sk_state_load(sk) != TCP_CLOSE &&
872 	    schedule_work(&mptcp_sk(sk)->work)) {
873 		/* each subflow already holds a reference to the sk, and the
874 		 * workqueue is invoked by a subflow, so sk can't go away here.
875 		 */
876 		sock_hold(sk);
877 		return true;
878 	}
879 	return false;
880 }
881 
882 void mptcp_subflow_eof(struct sock *sk)
883 {
884 	if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
885 		mptcp_schedule_work(sk);
886 }
887 
888 static void mptcp_check_for_eof(struct mptcp_sock *msk)
889 {
890 	struct mptcp_subflow_context *subflow;
891 	struct sock *sk = (struct sock *)msk;
892 	int receivers = 0;
893 
894 	mptcp_for_each_subflow(msk, subflow)
895 		receivers += !subflow->rx_eof;
896 	if (receivers)
897 		return;
898 
899 	if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
900 		/* hopefully temporary hack: propagate shutdown status
901 		 * to msk, when all subflows agree on it
902 		 */
903 		sk->sk_shutdown |= RCV_SHUTDOWN;
904 
905 		smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
906 		sk->sk_data_ready(sk);
907 	}
908 
909 	switch (sk->sk_state) {
910 	case TCP_ESTABLISHED:
911 		inet_sk_state_store(sk, TCP_CLOSE_WAIT);
912 		break;
913 	case TCP_FIN_WAIT1:
914 		inet_sk_state_store(sk, TCP_CLOSING);
915 		break;
916 	case TCP_FIN_WAIT2:
917 		inet_sk_state_store(sk, TCP_CLOSE);
918 		break;
919 	default:
920 		return;
921 	}
922 	mptcp_close_wake_up(sk);
923 }
924 
925 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
926 {
927 	struct mptcp_subflow_context *subflow;
928 	struct sock *sk = (struct sock *)msk;
929 
930 	sock_owned_by_me(sk);
931 
932 	mptcp_for_each_subflow(msk, subflow) {
933 		if (READ_ONCE(subflow->data_avail))
934 			return mptcp_subflow_tcp_sock(subflow);
935 	}
936 
937 	return NULL;
938 }
939 
940 static bool mptcp_skb_can_collapse_to(u64 write_seq,
941 				      const struct sk_buff *skb,
942 				      const struct mptcp_ext *mpext)
943 {
944 	if (!tcp_skb_can_collapse_to(skb))
945 		return false;
946 
947 	/* can collapse only if MPTCP level sequence is in order and this
948 	 * mapping has not been xmitted yet
949 	 */
950 	return mpext && mpext->data_seq + mpext->data_len == write_seq &&
951 	       !mpext->frozen;
952 }
953 
954 /* we can append data to the given data frag if:
955  * - there is space available in the backing page_frag
956  * - the data frag tail matches the current page_frag free offset
957  * - the data frag end sequence number matches the current write seq
958  */
959 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
960 				       const struct page_frag *pfrag,
961 				       const struct mptcp_data_frag *df)
962 {
963 	return df && pfrag->page == df->page &&
964 		pfrag->size - pfrag->offset > 0 &&
965 		pfrag->offset == (df->offset + df->data_len) &&
966 		df->data_seq + df->data_len == msk->write_seq;
967 }
968 
969 static void __mptcp_mem_reclaim_partial(struct sock *sk)
970 {
971 	int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
972 
973 	lockdep_assert_held_once(&sk->sk_lock.slock);
974 
975 	__mptcp_rmem_reclaim(sk, reclaimable - 1);
976 	sk_mem_reclaim_partial(sk);
977 }
978 
979 static void mptcp_mem_reclaim_partial(struct sock *sk)
980 {
981 	mptcp_data_lock(sk);
982 	__mptcp_mem_reclaim_partial(sk);
983 	mptcp_data_unlock(sk);
984 }
985 
986 static void dfrag_uncharge(struct sock *sk, int len)
987 {
988 	sk_mem_uncharge(sk, len);
989 	sk_wmem_queued_add(sk, -len);
990 }
991 
992 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
993 {
994 	int len = dfrag->data_len + dfrag->overhead;
995 
996 	list_del(&dfrag->list);
997 	dfrag_uncharge(sk, len);
998 	put_page(dfrag->page);
999 }
1000 
1001 static void __mptcp_clean_una(struct sock *sk)
1002 {
1003 	struct mptcp_sock *msk = mptcp_sk(sk);
1004 	struct mptcp_data_frag *dtmp, *dfrag;
1005 	bool cleaned = false;
1006 	u64 snd_una;
1007 
1008 	/* on fallback we just need to ignore snd_una, as this is really
1009 	 * plain TCP
1010 	 */
1011 	if (__mptcp_check_fallback(msk))
1012 		msk->snd_una = READ_ONCE(msk->snd_nxt);
1013 
1014 	snd_una = msk->snd_una;
1015 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1016 		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1017 			break;
1018 
1019 		if (unlikely(dfrag == msk->first_pending)) {
1020 			/* in recovery mode can see ack after the current snd head */
1021 			if (WARN_ON_ONCE(!msk->recovery))
1022 				break;
1023 
1024 			WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1025 		}
1026 
1027 		dfrag_clear(sk, dfrag);
1028 		cleaned = true;
1029 	}
1030 
1031 	dfrag = mptcp_rtx_head(sk);
1032 	if (dfrag && after64(snd_una, dfrag->data_seq)) {
1033 		u64 delta = snd_una - dfrag->data_seq;
1034 
1035 		/* prevent wrap around in recovery mode */
1036 		if (unlikely(delta > dfrag->already_sent)) {
1037 			if (WARN_ON_ONCE(!msk->recovery))
1038 				goto out;
1039 			if (WARN_ON_ONCE(delta > dfrag->data_len))
1040 				goto out;
1041 			dfrag->already_sent += delta - dfrag->already_sent;
1042 		}
1043 
1044 		dfrag->data_seq += delta;
1045 		dfrag->offset += delta;
1046 		dfrag->data_len -= delta;
1047 		dfrag->already_sent -= delta;
1048 
1049 		dfrag_uncharge(sk, delta);
1050 		cleaned = true;
1051 	}
1052 
1053 	/* all retransmitted data acked, recovery completed */
1054 	if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1055 		msk->recovery = false;
1056 
1057 out:
1058 	if (cleaned && tcp_under_memory_pressure(sk))
1059 		__mptcp_mem_reclaim_partial(sk);
1060 
1061 	if (snd_una == READ_ONCE(msk->snd_nxt) &&
1062 	    snd_una == READ_ONCE(msk->write_seq)) {
1063 		if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1064 			mptcp_stop_timer(sk);
1065 	} else {
1066 		mptcp_reset_timer(sk);
1067 	}
1068 }
1069 
1070 static void __mptcp_clean_una_wakeup(struct sock *sk)
1071 {
1072 	lockdep_assert_held_once(&sk->sk_lock.slock);
1073 
1074 	__mptcp_clean_una(sk);
1075 	mptcp_write_space(sk);
1076 }
1077 
1078 static void mptcp_clean_una_wakeup(struct sock *sk)
1079 {
1080 	mptcp_data_lock(sk);
1081 	__mptcp_clean_una_wakeup(sk);
1082 	mptcp_data_unlock(sk);
1083 }
1084 
1085 static void mptcp_enter_memory_pressure(struct sock *sk)
1086 {
1087 	struct mptcp_subflow_context *subflow;
1088 	struct mptcp_sock *msk = mptcp_sk(sk);
1089 	bool first = true;
1090 
1091 	sk_stream_moderate_sndbuf(sk);
1092 	mptcp_for_each_subflow(msk, subflow) {
1093 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1094 
1095 		if (first)
1096 			tcp_enter_memory_pressure(ssk);
1097 		sk_stream_moderate_sndbuf(ssk);
1098 		first = false;
1099 	}
1100 }
1101 
1102 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1103  * data
1104  */
1105 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1106 {
1107 	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1108 					pfrag, sk->sk_allocation)))
1109 		return true;
1110 
1111 	mptcp_enter_memory_pressure(sk);
1112 	return false;
1113 }
1114 
1115 static struct mptcp_data_frag *
1116 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1117 		      int orig_offset)
1118 {
1119 	int offset = ALIGN(orig_offset, sizeof(long));
1120 	struct mptcp_data_frag *dfrag;
1121 
1122 	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1123 	dfrag->data_len = 0;
1124 	dfrag->data_seq = msk->write_seq;
1125 	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1126 	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1127 	dfrag->already_sent = 0;
1128 	dfrag->page = pfrag->page;
1129 
1130 	return dfrag;
1131 }
1132 
1133 struct mptcp_sendmsg_info {
1134 	int mss_now;
1135 	int size_goal;
1136 	u16 limit;
1137 	u16 sent;
1138 	unsigned int flags;
1139 	bool data_lock_held;
1140 };
1141 
1142 static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1143 				    int avail_size)
1144 {
1145 	u64 window_end = mptcp_wnd_end(msk);
1146 
1147 	if (__mptcp_check_fallback(msk))
1148 		return avail_size;
1149 
1150 	if (!before64(data_seq + avail_size, window_end)) {
1151 		u64 allowed_size = window_end - data_seq;
1152 
1153 		return min_t(unsigned int, allowed_size, avail_size);
1154 	}
1155 
1156 	return avail_size;
1157 }
1158 
1159 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1160 {
1161 	struct skb_ext *mpext = __skb_ext_alloc(gfp);
1162 
1163 	if (!mpext)
1164 		return false;
1165 	__skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1166 	return true;
1167 }
1168 
1169 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1170 {
1171 	struct sk_buff *skb;
1172 
1173 	skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1174 	if (likely(skb)) {
1175 		if (likely(__mptcp_add_ext(skb, gfp))) {
1176 			skb_reserve(skb, MAX_TCP_HEADER);
1177 			skb->ip_summed = CHECKSUM_PARTIAL;
1178 			INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1179 			return skb;
1180 		}
1181 		__kfree_skb(skb);
1182 	} else {
1183 		mptcp_enter_memory_pressure(sk);
1184 	}
1185 	return NULL;
1186 }
1187 
1188 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1189 {
1190 	struct sk_buff *skb;
1191 
1192 	skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1193 	if (!skb)
1194 		return NULL;
1195 
1196 	if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1197 		tcp_skb_entail(ssk, skb);
1198 		return skb;
1199 	}
1200 	kfree_skb(skb);
1201 	return NULL;
1202 }
1203 
1204 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1205 {
1206 	gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1207 
1208 	if (unlikely(tcp_under_memory_pressure(sk))) {
1209 		if (data_lock_held)
1210 			__mptcp_mem_reclaim_partial(sk);
1211 		else
1212 			mptcp_mem_reclaim_partial(sk);
1213 	}
1214 	return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1215 }
1216 
1217 /* note: this always recompute the csum on the whole skb, even
1218  * if we just appended a single frag. More status info needed
1219  */
1220 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1221 {
1222 	struct mptcp_ext *mpext = mptcp_get_ext(skb);
1223 	__wsum csum = ~csum_unfold(mpext->csum);
1224 	int offset = skb->len - added;
1225 
1226 	mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1227 }
1228 
1229 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1230 			      struct mptcp_data_frag *dfrag,
1231 			      struct mptcp_sendmsg_info *info)
1232 {
1233 	u64 data_seq = dfrag->data_seq + info->sent;
1234 	int offset = dfrag->offset + info->sent;
1235 	struct mptcp_sock *msk = mptcp_sk(sk);
1236 	bool zero_window_probe = false;
1237 	struct mptcp_ext *mpext = NULL;
1238 	bool can_coalesce = false;
1239 	bool reuse_skb = true;
1240 	struct sk_buff *skb;
1241 	size_t copy;
1242 	int i;
1243 
1244 	pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1245 		 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1246 
1247 	if (WARN_ON_ONCE(info->sent > info->limit ||
1248 			 info->limit > dfrag->data_len))
1249 		return 0;
1250 
1251 	/* compute send limit */
1252 	info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1253 	copy = info->size_goal;
1254 
1255 	skb = tcp_write_queue_tail(ssk);
1256 	if (skb && copy > skb->len) {
1257 		/* Limit the write to the size available in the
1258 		 * current skb, if any, so that we create at most a new skb.
1259 		 * Explicitly tells TCP internals to avoid collapsing on later
1260 		 * queue management operation, to avoid breaking the ext <->
1261 		 * SSN association set here
1262 		 */
1263 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1264 		if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1265 			TCP_SKB_CB(skb)->eor = 1;
1266 			goto alloc_skb;
1267 		}
1268 
1269 		i = skb_shinfo(skb)->nr_frags;
1270 		can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1271 		if (!can_coalesce && i >= sysctl_max_skb_frags) {
1272 			tcp_mark_push(tcp_sk(ssk), skb);
1273 			goto alloc_skb;
1274 		}
1275 
1276 		copy -= skb->len;
1277 	} else {
1278 alloc_skb:
1279 		skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1280 		if (!skb)
1281 			return -ENOMEM;
1282 
1283 		i = skb_shinfo(skb)->nr_frags;
1284 		reuse_skb = false;
1285 		mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1286 	}
1287 
1288 	/* Zero window and all data acked? Probe. */
1289 	copy = mptcp_check_allowed_size(msk, data_seq, copy);
1290 	if (copy == 0) {
1291 		u64 snd_una = READ_ONCE(msk->snd_una);
1292 
1293 		if (snd_una != msk->snd_nxt) {
1294 			tcp_remove_empty_skb(ssk);
1295 			return 0;
1296 		}
1297 
1298 		zero_window_probe = true;
1299 		data_seq = snd_una - 1;
1300 		copy = 1;
1301 
1302 		/* all mptcp-level data is acked, no skbs should be present into the
1303 		 * ssk write queue
1304 		 */
1305 		WARN_ON_ONCE(reuse_skb);
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 		WARN_ON_ONCE(zero_window_probe);
1335 		goto out;
1336 	}
1337 
1338 	memset(mpext, 0, sizeof(*mpext));
1339 	mpext->data_seq = data_seq;
1340 	mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1341 	mpext->data_len = copy;
1342 	mpext->use_map = 1;
1343 	mpext->dsn64 = 1;
1344 
1345 	pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1346 		 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1347 		 mpext->dsn64);
1348 
1349 	if (zero_window_probe) {
1350 		mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1351 		mpext->frozen = 1;
1352 		if (READ_ONCE(msk->csum_enabled))
1353 			mptcp_update_data_checksum(skb, copy);
1354 		tcp_push_pending_frames(ssk);
1355 		return 0;
1356 	}
1357 out:
1358 	if (READ_ONCE(msk->csum_enabled))
1359 		mptcp_update_data_checksum(skb, copy);
1360 	mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1361 	return copy;
1362 }
1363 
1364 #define MPTCP_SEND_BURST_SIZE		((1 << 16) - \
1365 					 sizeof(struct tcphdr) - \
1366 					 MAX_TCP_OPTION_SPACE - \
1367 					 sizeof(struct ipv6hdr) - \
1368 					 sizeof(struct frag_hdr))
1369 
1370 struct subflow_send_info {
1371 	struct sock *ssk;
1372 	u64 ratio;
1373 };
1374 
1375 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1376 {
1377 	if (!subflow->stale)
1378 		return;
1379 
1380 	subflow->stale = 0;
1381 	MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1382 }
1383 
1384 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1385 {
1386 	if (unlikely(subflow->stale)) {
1387 		u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1388 
1389 		if (subflow->stale_rcv_tstamp == rcv_tstamp)
1390 			return false;
1391 
1392 		mptcp_subflow_set_active(subflow);
1393 	}
1394 	return __mptcp_subflow_active(subflow);
1395 }
1396 
1397 /* implement the mptcp packet scheduler;
1398  * returns the subflow that will transmit the next DSS
1399  * additionally updates the rtx timeout
1400  */
1401 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1402 {
1403 	struct subflow_send_info send_info[2];
1404 	struct mptcp_subflow_context *subflow;
1405 	struct sock *sk = (struct sock *)msk;
1406 	int i, nr_active = 0;
1407 	struct sock *ssk;
1408 	long tout = 0;
1409 	u64 ratio;
1410 	u32 pace;
1411 
1412 	sock_owned_by_me(sk);
1413 
1414 	if (__mptcp_check_fallback(msk)) {
1415 		if (!msk->first)
1416 			return NULL;
1417 		return sk_stream_memory_free(msk->first) ? msk->first : NULL;
1418 	}
1419 
1420 	/* re-use last subflow, if the burst allow that */
1421 	if (msk->last_snd && msk->snd_burst > 0 &&
1422 	    sk_stream_memory_free(msk->last_snd) &&
1423 	    mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1424 		mptcp_set_timeout(sk);
1425 		return msk->last_snd;
1426 	}
1427 
1428 	/* pick the subflow with the lower wmem/wspace ratio */
1429 	for (i = 0; i < 2; ++i) {
1430 		send_info[i].ssk = NULL;
1431 		send_info[i].ratio = -1;
1432 	}
1433 	mptcp_for_each_subflow(msk, subflow) {
1434 		trace_mptcp_subflow_get_send(subflow);
1435 		ssk =  mptcp_subflow_tcp_sock(subflow);
1436 		if (!mptcp_subflow_active(subflow))
1437 			continue;
1438 
1439 		tout = max(tout, mptcp_timeout_from_subflow(subflow));
1440 		nr_active += !subflow->backup;
1441 		if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd)
1442 			continue;
1443 
1444 		pace = READ_ONCE(ssk->sk_pacing_rate);
1445 		if (!pace)
1446 			continue;
1447 
1448 		ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1449 				pace);
1450 		if (ratio < send_info[subflow->backup].ratio) {
1451 			send_info[subflow->backup].ssk = ssk;
1452 			send_info[subflow->backup].ratio = ratio;
1453 		}
1454 	}
1455 	__mptcp_set_timeout(sk, tout);
1456 
1457 	/* pick the best backup if no other subflow is active */
1458 	if (!nr_active)
1459 		send_info[0].ssk = send_info[1].ssk;
1460 
1461 	if (send_info[0].ssk) {
1462 		msk->last_snd = send_info[0].ssk;
1463 		msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
1464 				       tcp_sk(msk->last_snd)->snd_wnd);
1465 		return msk->last_snd;
1466 	}
1467 
1468 	return NULL;
1469 }
1470 
1471 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1472 {
1473 	tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1474 	release_sock(ssk);
1475 }
1476 
1477 static void mptcp_update_post_push(struct mptcp_sock *msk,
1478 				   struct mptcp_data_frag *dfrag,
1479 				   u32 sent)
1480 {
1481 	u64 snd_nxt_new = dfrag->data_seq;
1482 
1483 	dfrag->already_sent += sent;
1484 
1485 	msk->snd_burst -= sent;
1486 
1487 	snd_nxt_new += dfrag->already_sent;
1488 
1489 	/* snd_nxt_new can be smaller than snd_nxt in case mptcp
1490 	 * is recovering after a failover. In that event, this re-sends
1491 	 * old segments.
1492 	 *
1493 	 * Thus compute snd_nxt_new candidate based on
1494 	 * the dfrag->data_seq that was sent and the data
1495 	 * that has been handed to the subflow for transmission
1496 	 * and skip update in case it was old dfrag.
1497 	 */
1498 	if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1499 		msk->snd_nxt = snd_nxt_new;
1500 }
1501 
1502 static void mptcp_check_and_set_pending(struct sock *sk)
1503 {
1504 	if (mptcp_send_head(sk) &&
1505 	    !test_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
1506 		set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
1507 }
1508 
1509 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1510 {
1511 	struct sock *prev_ssk = NULL, *ssk = NULL;
1512 	struct mptcp_sock *msk = mptcp_sk(sk);
1513 	struct mptcp_sendmsg_info info = {
1514 				.flags = flags,
1515 	};
1516 	struct mptcp_data_frag *dfrag;
1517 	int len, copied = 0;
1518 
1519 	while ((dfrag = mptcp_send_head(sk))) {
1520 		info.sent = dfrag->already_sent;
1521 		info.limit = dfrag->data_len;
1522 		len = dfrag->data_len - dfrag->already_sent;
1523 		while (len > 0) {
1524 			int ret = 0;
1525 
1526 			prev_ssk = ssk;
1527 			mptcp_flush_join_list(msk);
1528 			ssk = mptcp_subflow_get_send(msk);
1529 
1530 			/* First check. If the ssk has changed since
1531 			 * the last round, release prev_ssk
1532 			 */
1533 			if (ssk != prev_ssk && prev_ssk)
1534 				mptcp_push_release(prev_ssk, &info);
1535 			if (!ssk)
1536 				goto out;
1537 
1538 			/* Need to lock the new subflow only if different
1539 			 * from the previous one, otherwise we are still
1540 			 * helding the relevant lock
1541 			 */
1542 			if (ssk != prev_ssk)
1543 				lock_sock(ssk);
1544 
1545 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1546 			if (ret <= 0) {
1547 				mptcp_push_release(ssk, &info);
1548 				goto out;
1549 			}
1550 
1551 			info.sent += ret;
1552 			copied += ret;
1553 			len -= ret;
1554 
1555 			mptcp_update_post_push(msk, dfrag, ret);
1556 		}
1557 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1558 	}
1559 
1560 	/* at this point we held the socket lock for the last subflow we used */
1561 	if (ssk)
1562 		mptcp_push_release(ssk, &info);
1563 
1564 out:
1565 	/* ensure the rtx timer is running */
1566 	if (!mptcp_timer_pending(sk))
1567 		mptcp_reset_timer(sk);
1568 	if (copied)
1569 		__mptcp_check_send_data_fin(sk);
1570 }
1571 
1572 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1573 {
1574 	struct mptcp_sock *msk = mptcp_sk(sk);
1575 	struct mptcp_sendmsg_info info = {
1576 		.data_lock_held = true,
1577 	};
1578 	struct mptcp_data_frag *dfrag;
1579 	struct sock *xmit_ssk;
1580 	int len, copied = 0;
1581 	bool first = true;
1582 
1583 	info.flags = 0;
1584 	while ((dfrag = mptcp_send_head(sk))) {
1585 		info.sent = dfrag->already_sent;
1586 		info.limit = dfrag->data_len;
1587 		len = dfrag->data_len - dfrag->already_sent;
1588 		while (len > 0) {
1589 			int ret = 0;
1590 
1591 			/* the caller already invoked the packet scheduler,
1592 			 * check for a different subflow usage only after
1593 			 * spooling the first chunk of data
1594 			 */
1595 			xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1596 			if (!xmit_ssk)
1597 				goto out;
1598 			if (xmit_ssk != ssk) {
1599 				mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
1600 				goto out;
1601 			}
1602 
1603 			ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1604 			if (ret <= 0)
1605 				goto out;
1606 
1607 			info.sent += ret;
1608 			copied += ret;
1609 			len -= ret;
1610 			first = false;
1611 
1612 			mptcp_update_post_push(msk, dfrag, ret);
1613 		}
1614 		WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1615 	}
1616 
1617 out:
1618 	/* __mptcp_alloc_tx_skb could have released some wmem and we are
1619 	 * not going to flush it via release_sock()
1620 	 */
1621 	if (copied) {
1622 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1623 			 info.size_goal);
1624 		if (!mptcp_timer_pending(sk))
1625 			mptcp_reset_timer(sk);
1626 
1627 		if (msk->snd_data_fin_enable &&
1628 		    msk->snd_nxt + 1 == msk->write_seq)
1629 			mptcp_schedule_work(sk);
1630 	}
1631 }
1632 
1633 static void mptcp_set_nospace(struct sock *sk)
1634 {
1635 	/* enable autotune */
1636 	set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1637 
1638 	/* will be cleared on avail space */
1639 	set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1640 }
1641 
1642 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1643 {
1644 	struct mptcp_sock *msk = mptcp_sk(sk);
1645 	struct page_frag *pfrag;
1646 	size_t copied = 0;
1647 	int ret = 0;
1648 	long timeo;
1649 
1650 	/* we don't support FASTOPEN yet */
1651 	if (msg->msg_flags & MSG_FASTOPEN)
1652 		return -EOPNOTSUPP;
1653 
1654 	/* silently ignore everything else */
1655 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1656 
1657 	lock_sock(sk);
1658 
1659 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1660 
1661 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1662 		ret = sk_stream_wait_connect(sk, &timeo);
1663 		if (ret)
1664 			goto out;
1665 	}
1666 
1667 	pfrag = sk_page_frag(sk);
1668 
1669 	while (msg_data_left(msg)) {
1670 		int total_ts, frag_truesize = 0;
1671 		struct mptcp_data_frag *dfrag;
1672 		bool dfrag_collapsed;
1673 		size_t psize, offset;
1674 
1675 		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1676 			ret = -EPIPE;
1677 			goto out;
1678 		}
1679 
1680 		/* reuse tail pfrag, if possible, or carve a new one from the
1681 		 * page allocator
1682 		 */
1683 		dfrag = mptcp_pending_tail(sk);
1684 		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1685 		if (!dfrag_collapsed) {
1686 			if (!sk_stream_memory_free(sk))
1687 				goto wait_for_memory;
1688 
1689 			if (!mptcp_page_frag_refill(sk, pfrag))
1690 				goto wait_for_memory;
1691 
1692 			dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1693 			frag_truesize = dfrag->overhead;
1694 		}
1695 
1696 		/* we do not bound vs wspace, to allow a single packet.
1697 		 * memory accounting will prevent execessive memory usage
1698 		 * anyway
1699 		 */
1700 		offset = dfrag->offset + dfrag->data_len;
1701 		psize = pfrag->size - offset;
1702 		psize = min_t(size_t, psize, msg_data_left(msg));
1703 		total_ts = psize + frag_truesize;
1704 
1705 		if (!sk_wmem_schedule(sk, total_ts))
1706 			goto wait_for_memory;
1707 
1708 		if (copy_page_from_iter(dfrag->page, offset, psize,
1709 					&msg->msg_iter) != psize) {
1710 			ret = -EFAULT;
1711 			goto out;
1712 		}
1713 
1714 		/* data successfully copied into the write queue */
1715 		sk->sk_forward_alloc -= total_ts;
1716 		copied += psize;
1717 		dfrag->data_len += psize;
1718 		frag_truesize += psize;
1719 		pfrag->offset += frag_truesize;
1720 		WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1721 
1722 		/* charge data on mptcp pending queue to the msk socket
1723 		 * Note: we charge such data both to sk and ssk
1724 		 */
1725 		sk_wmem_queued_add(sk, frag_truesize);
1726 		if (!dfrag_collapsed) {
1727 			get_page(dfrag->page);
1728 			list_add_tail(&dfrag->list, &msk->rtx_queue);
1729 			if (!msk->first_pending)
1730 				WRITE_ONCE(msk->first_pending, dfrag);
1731 		}
1732 		pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1733 			 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1734 			 !dfrag_collapsed);
1735 
1736 		continue;
1737 
1738 wait_for_memory:
1739 		mptcp_set_nospace(sk);
1740 		__mptcp_push_pending(sk, msg->msg_flags);
1741 		ret = sk_stream_wait_memory(sk, &timeo);
1742 		if (ret)
1743 			goto out;
1744 	}
1745 
1746 	if (copied)
1747 		__mptcp_push_pending(sk, msg->msg_flags);
1748 
1749 out:
1750 	release_sock(sk);
1751 	return copied ? : ret;
1752 }
1753 
1754 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1755 				struct msghdr *msg,
1756 				size_t len, int flags,
1757 				struct scm_timestamping_internal *tss,
1758 				int *cmsg_flags)
1759 {
1760 	struct sk_buff *skb, *tmp;
1761 	int copied = 0;
1762 
1763 	skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1764 		u32 offset = MPTCP_SKB_CB(skb)->offset;
1765 		u32 data_len = skb->len - offset;
1766 		u32 count = min_t(size_t, len - copied, data_len);
1767 		int err;
1768 
1769 		if (!(flags & MSG_TRUNC)) {
1770 			err = skb_copy_datagram_msg(skb, offset, msg, count);
1771 			if (unlikely(err < 0)) {
1772 				if (!copied)
1773 					return err;
1774 				break;
1775 			}
1776 		}
1777 
1778 		if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1779 			tcp_update_recv_tstamps(skb, tss);
1780 			*cmsg_flags |= MPTCP_CMSG_TS;
1781 		}
1782 
1783 		copied += count;
1784 
1785 		if (count < data_len) {
1786 			if (!(flags & MSG_PEEK))
1787 				MPTCP_SKB_CB(skb)->offset += count;
1788 			break;
1789 		}
1790 
1791 		if (!(flags & MSG_PEEK)) {
1792 			/* we will bulk release the skb memory later */
1793 			skb->destructor = NULL;
1794 			WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1795 			__skb_unlink(skb, &msk->receive_queue);
1796 			__kfree_skb(skb);
1797 		}
1798 
1799 		if (copied >= len)
1800 			break;
1801 	}
1802 
1803 	return copied;
1804 }
1805 
1806 /* receive buffer autotuning.  See tcp_rcv_space_adjust for more information.
1807  *
1808  * Only difference: Use highest rtt estimate of the subflows in use.
1809  */
1810 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1811 {
1812 	struct mptcp_subflow_context *subflow;
1813 	struct sock *sk = (struct sock *)msk;
1814 	u32 time, advmss = 1;
1815 	u64 rtt_us, mstamp;
1816 
1817 	sock_owned_by_me(sk);
1818 
1819 	if (copied <= 0)
1820 		return;
1821 
1822 	msk->rcvq_space.copied += copied;
1823 
1824 	mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1825 	time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1826 
1827 	rtt_us = msk->rcvq_space.rtt_us;
1828 	if (rtt_us && time < (rtt_us >> 3))
1829 		return;
1830 
1831 	rtt_us = 0;
1832 	mptcp_for_each_subflow(msk, subflow) {
1833 		const struct tcp_sock *tp;
1834 		u64 sf_rtt_us;
1835 		u32 sf_advmss;
1836 
1837 		tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1838 
1839 		sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1840 		sf_advmss = READ_ONCE(tp->advmss);
1841 
1842 		rtt_us = max(sf_rtt_us, rtt_us);
1843 		advmss = max(sf_advmss, advmss);
1844 	}
1845 
1846 	msk->rcvq_space.rtt_us = rtt_us;
1847 	if (time < (rtt_us >> 3) || rtt_us == 0)
1848 		return;
1849 
1850 	if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1851 		goto new_measure;
1852 
1853 	if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1854 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1855 		int rcvmem, rcvbuf;
1856 		u64 rcvwin, grow;
1857 
1858 		rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1859 
1860 		grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1861 
1862 		do_div(grow, msk->rcvq_space.space);
1863 		rcvwin += (grow << 1);
1864 
1865 		rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1866 		while (tcp_win_from_space(sk, rcvmem) < advmss)
1867 			rcvmem += 128;
1868 
1869 		do_div(rcvwin, advmss);
1870 		rcvbuf = min_t(u64, rcvwin * rcvmem,
1871 			       sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1872 
1873 		if (rcvbuf > sk->sk_rcvbuf) {
1874 			u32 window_clamp;
1875 
1876 			window_clamp = tcp_win_from_space(sk, rcvbuf);
1877 			WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1878 
1879 			/* Make subflows follow along.  If we do not do this, we
1880 			 * get drops at subflow level if skbs can't be moved to
1881 			 * the mptcp rx queue fast enough (announced rcv_win can
1882 			 * exceed ssk->sk_rcvbuf).
1883 			 */
1884 			mptcp_for_each_subflow(msk, subflow) {
1885 				struct sock *ssk;
1886 				bool slow;
1887 
1888 				ssk = mptcp_subflow_tcp_sock(subflow);
1889 				slow = lock_sock_fast(ssk);
1890 				WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1891 				tcp_sk(ssk)->window_clamp = window_clamp;
1892 				tcp_cleanup_rbuf(ssk, 1);
1893 				unlock_sock_fast(ssk, slow);
1894 			}
1895 		}
1896 	}
1897 
1898 	msk->rcvq_space.space = msk->rcvq_space.copied;
1899 new_measure:
1900 	msk->rcvq_space.copied = 0;
1901 	msk->rcvq_space.time = mstamp;
1902 }
1903 
1904 static void __mptcp_update_rmem(struct sock *sk)
1905 {
1906 	struct mptcp_sock *msk = mptcp_sk(sk);
1907 
1908 	if (!msk->rmem_released)
1909 		return;
1910 
1911 	atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
1912 	mptcp_rmem_uncharge(sk, msk->rmem_released);
1913 	WRITE_ONCE(msk->rmem_released, 0);
1914 }
1915 
1916 static void __mptcp_splice_receive_queue(struct sock *sk)
1917 {
1918 	struct mptcp_sock *msk = mptcp_sk(sk);
1919 
1920 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1921 }
1922 
1923 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
1924 {
1925 	struct sock *sk = (struct sock *)msk;
1926 	unsigned int moved = 0;
1927 	bool ret, done;
1928 
1929 	mptcp_flush_join_list(msk);
1930 	do {
1931 		struct sock *ssk = mptcp_subflow_recv_lookup(msk);
1932 		bool slowpath;
1933 
1934 		/* we can have data pending in the subflows only if the msk
1935 		 * receive buffer was full at subflow_data_ready() time,
1936 		 * that is an unlikely slow path.
1937 		 */
1938 		if (likely(!ssk))
1939 			break;
1940 
1941 		slowpath = lock_sock_fast(ssk);
1942 		mptcp_data_lock(sk);
1943 		__mptcp_update_rmem(sk);
1944 		done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
1945 		mptcp_data_unlock(sk);
1946 
1947 		if (unlikely(ssk->sk_err))
1948 			__mptcp_error_report(sk);
1949 		unlock_sock_fast(ssk, slowpath);
1950 	} while (!done);
1951 
1952 	/* acquire the data lock only if some input data is pending */
1953 	ret = moved > 0;
1954 	if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1955 	    !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
1956 		mptcp_data_lock(sk);
1957 		__mptcp_update_rmem(sk);
1958 		ret |= __mptcp_ofo_queue(msk);
1959 		__mptcp_splice_receive_queue(sk);
1960 		mptcp_data_unlock(sk);
1961 	}
1962 	if (ret)
1963 		mptcp_check_data_fin((struct sock *)msk);
1964 	return !skb_queue_empty(&msk->receive_queue);
1965 }
1966 
1967 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1968 			 int nonblock, int flags, int *addr_len)
1969 {
1970 	struct mptcp_sock *msk = mptcp_sk(sk);
1971 	struct scm_timestamping_internal tss;
1972 	int copied = 0, cmsg_flags = 0;
1973 	int target;
1974 	long timeo;
1975 
1976 	/* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
1977 	if (unlikely(flags & MSG_ERRQUEUE))
1978 		return inet_recv_error(sk, msg, len, addr_len);
1979 
1980 	lock_sock(sk);
1981 	if (unlikely(sk->sk_state == TCP_LISTEN)) {
1982 		copied = -ENOTCONN;
1983 		goto out_err;
1984 	}
1985 
1986 	timeo = sock_rcvtimeo(sk, nonblock);
1987 
1988 	len = min_t(size_t, len, INT_MAX);
1989 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1990 
1991 	while (copied < len) {
1992 		int bytes_read;
1993 
1994 		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
1995 		if (unlikely(bytes_read < 0)) {
1996 			if (!copied)
1997 				copied = bytes_read;
1998 			goto out_err;
1999 		}
2000 
2001 		copied += bytes_read;
2002 
2003 		/* be sure to advertise window change */
2004 		mptcp_cleanup_rbuf(msk);
2005 
2006 		if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2007 			continue;
2008 
2009 		/* only the master socket status is relevant here. The exit
2010 		 * conditions mirror closely tcp_recvmsg()
2011 		 */
2012 		if (copied >= target)
2013 			break;
2014 
2015 		if (copied) {
2016 			if (sk->sk_err ||
2017 			    sk->sk_state == TCP_CLOSE ||
2018 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
2019 			    !timeo ||
2020 			    signal_pending(current))
2021 				break;
2022 		} else {
2023 			if (sk->sk_err) {
2024 				copied = sock_error(sk);
2025 				break;
2026 			}
2027 
2028 			if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2029 				mptcp_check_for_eof(msk);
2030 
2031 			if (sk->sk_shutdown & RCV_SHUTDOWN) {
2032 				/* race breaker: the shutdown could be after the
2033 				 * previous receive queue check
2034 				 */
2035 				if (__mptcp_move_skbs(msk))
2036 					continue;
2037 				break;
2038 			}
2039 
2040 			if (sk->sk_state == TCP_CLOSE) {
2041 				copied = -ENOTCONN;
2042 				break;
2043 			}
2044 
2045 			if (!timeo) {
2046 				copied = -EAGAIN;
2047 				break;
2048 			}
2049 
2050 			if (signal_pending(current)) {
2051 				copied = sock_intr_errno(timeo);
2052 				break;
2053 			}
2054 		}
2055 
2056 		pr_debug("block timeout %ld", timeo);
2057 		sk_wait_data(sk, &timeo, NULL);
2058 	}
2059 
2060 out_err:
2061 	if (cmsg_flags && copied >= 0) {
2062 		if (cmsg_flags & MPTCP_CMSG_TS)
2063 			tcp_recv_timestamp(msg, sk, &tss);
2064 	}
2065 
2066 	pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2067 		 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2068 		 skb_queue_empty(&msk->receive_queue), copied);
2069 	if (!(flags & MSG_PEEK))
2070 		mptcp_rcv_space_adjust(msk, copied);
2071 
2072 	release_sock(sk);
2073 	return copied;
2074 }
2075 
2076 static void mptcp_retransmit_timer(struct timer_list *t)
2077 {
2078 	struct inet_connection_sock *icsk = from_timer(icsk, t,
2079 						       icsk_retransmit_timer);
2080 	struct sock *sk = &icsk->icsk_inet.sk;
2081 	struct mptcp_sock *msk = mptcp_sk(sk);
2082 
2083 	bh_lock_sock(sk);
2084 	if (!sock_owned_by_user(sk)) {
2085 		/* we need a process context to retransmit */
2086 		if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2087 			mptcp_schedule_work(sk);
2088 	} else {
2089 		/* delegate our work to tcp_release_cb() */
2090 		set_bit(MPTCP_RETRANSMIT, &msk->flags);
2091 	}
2092 	bh_unlock_sock(sk);
2093 	sock_put(sk);
2094 }
2095 
2096 static void mptcp_timeout_timer(struct timer_list *t)
2097 {
2098 	struct sock *sk = from_timer(sk, t, sk_timer);
2099 
2100 	mptcp_schedule_work(sk);
2101 	sock_put(sk);
2102 }
2103 
2104 /* Find an idle subflow.  Return NULL if there is unacked data at tcp
2105  * level.
2106  *
2107  * A backup subflow is returned only if that is the only kind available.
2108  */
2109 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2110 {
2111 	struct sock *backup = NULL, *pick = NULL;
2112 	struct mptcp_subflow_context *subflow;
2113 	int min_stale_count = INT_MAX;
2114 
2115 	sock_owned_by_me((const struct sock *)msk);
2116 
2117 	if (__mptcp_check_fallback(msk))
2118 		return NULL;
2119 
2120 	mptcp_for_each_subflow(msk, subflow) {
2121 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2122 
2123 		if (!__mptcp_subflow_active(subflow))
2124 			continue;
2125 
2126 		/* still data outstanding at TCP level? skip this */
2127 		if (!tcp_rtx_and_write_queues_empty(ssk)) {
2128 			mptcp_pm_subflow_chk_stale(msk, ssk);
2129 			min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2130 			continue;
2131 		}
2132 
2133 		if (subflow->backup) {
2134 			if (!backup)
2135 				backup = ssk;
2136 			continue;
2137 		}
2138 
2139 		if (!pick)
2140 			pick = ssk;
2141 	}
2142 
2143 	if (pick)
2144 		return pick;
2145 
2146 	/* use backup only if there are no progresses anywhere */
2147 	return min_stale_count > 1 ? backup : NULL;
2148 }
2149 
2150 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2151 {
2152 	if (msk->subflow) {
2153 		iput(SOCK_INODE(msk->subflow));
2154 		msk->subflow = NULL;
2155 	}
2156 }
2157 
2158 bool __mptcp_retransmit_pending_data(struct sock *sk)
2159 {
2160 	struct mptcp_data_frag *cur, *rtx_head;
2161 	struct mptcp_sock *msk = mptcp_sk(sk);
2162 
2163 	if (__mptcp_check_fallback(mptcp_sk(sk)))
2164 		return false;
2165 
2166 	if (tcp_rtx_and_write_queues_empty(sk))
2167 		return false;
2168 
2169 	/* the closing socket has some data untransmitted and/or unacked:
2170 	 * some data in the mptcp rtx queue has not really xmitted yet.
2171 	 * keep it simple and re-inject the whole mptcp level rtx queue
2172 	 */
2173 	mptcp_data_lock(sk);
2174 	__mptcp_clean_una_wakeup(sk);
2175 	rtx_head = mptcp_rtx_head(sk);
2176 	if (!rtx_head) {
2177 		mptcp_data_unlock(sk);
2178 		return false;
2179 	}
2180 
2181 	msk->recovery_snd_nxt = msk->snd_nxt;
2182 	msk->recovery = true;
2183 	mptcp_data_unlock(sk);
2184 
2185 	msk->first_pending = rtx_head;
2186 	msk->snd_burst = 0;
2187 
2188 	/* be sure to clear the "sent status" on all re-injected fragments */
2189 	list_for_each_entry(cur, &msk->rtx_queue, list) {
2190 		if (!cur->already_sent)
2191 			break;
2192 		cur->already_sent = 0;
2193 	}
2194 
2195 	return true;
2196 }
2197 
2198 /* subflow sockets can be either outgoing (connect) or incoming
2199  * (accept).
2200  *
2201  * Outgoing subflows use in-kernel sockets.
2202  * Incoming subflows do not have their own 'struct socket' allocated,
2203  * so we need to use tcp_close() after detaching them from the mptcp
2204  * parent socket.
2205  */
2206 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2207 			      struct mptcp_subflow_context *subflow)
2208 {
2209 	struct mptcp_sock *msk = mptcp_sk(sk);
2210 	bool need_push;
2211 
2212 	list_del(&subflow->node);
2213 
2214 	lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2215 
2216 	/* if we are invoked by the msk cleanup code, the subflow is
2217 	 * already orphaned
2218 	 */
2219 	if (ssk->sk_socket)
2220 		sock_orphan(ssk);
2221 
2222 	need_push = __mptcp_retransmit_pending_data(sk);
2223 	subflow->disposable = 1;
2224 
2225 	/* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2226 	 * the ssk has been already destroyed, we just need to release the
2227 	 * reference owned by msk;
2228 	 */
2229 	if (!inet_csk(ssk)->icsk_ulp_ops) {
2230 		kfree_rcu(subflow, rcu);
2231 	} else {
2232 		/* otherwise tcp will dispose of the ssk and subflow ctx */
2233 		__tcp_close(ssk, 0);
2234 
2235 		/* close acquired an extra ref */
2236 		__sock_put(ssk);
2237 	}
2238 	release_sock(ssk);
2239 
2240 	sock_put(ssk);
2241 
2242 	if (ssk == msk->last_snd)
2243 		msk->last_snd = NULL;
2244 
2245 	if (ssk == msk->first)
2246 		msk->first = NULL;
2247 
2248 	if (msk->subflow && ssk == msk->subflow->sk)
2249 		mptcp_dispose_initial_subflow(msk);
2250 
2251 	if (need_push)
2252 		__mptcp_push_pending(sk, 0);
2253 }
2254 
2255 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2256 		     struct mptcp_subflow_context *subflow)
2257 {
2258 	if (sk->sk_state == TCP_ESTABLISHED)
2259 		mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2260 	__mptcp_close_ssk(sk, ssk, subflow);
2261 }
2262 
2263 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2264 {
2265 	return 0;
2266 }
2267 
2268 static void __mptcp_close_subflow(struct mptcp_sock *msk)
2269 {
2270 	struct mptcp_subflow_context *subflow, *tmp;
2271 
2272 	might_sleep();
2273 
2274 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2275 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2276 
2277 		if (inet_sk_state_load(ssk) != TCP_CLOSE)
2278 			continue;
2279 
2280 		/* 'subflow_data_ready' will re-sched once rx queue is empty */
2281 		if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2282 			continue;
2283 
2284 		mptcp_close_ssk((struct sock *)msk, ssk, subflow);
2285 	}
2286 }
2287 
2288 static bool mptcp_check_close_timeout(const struct sock *sk)
2289 {
2290 	s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2291 	struct mptcp_subflow_context *subflow;
2292 
2293 	if (delta >= TCP_TIMEWAIT_LEN)
2294 		return true;
2295 
2296 	/* if all subflows are in closed status don't bother with additional
2297 	 * timeout
2298 	 */
2299 	mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2300 		if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2301 		    TCP_CLOSE)
2302 			return false;
2303 	}
2304 	return true;
2305 }
2306 
2307 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2308 {
2309 	struct mptcp_subflow_context *subflow, *tmp;
2310 	struct sock *sk = &msk->sk.icsk_inet.sk;
2311 
2312 	if (likely(!READ_ONCE(msk->rcv_fastclose)))
2313 		return;
2314 
2315 	mptcp_token_destroy(msk);
2316 
2317 	list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2318 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2319 		bool slow;
2320 
2321 		slow = lock_sock_fast(tcp_sk);
2322 		if (tcp_sk->sk_state != TCP_CLOSE) {
2323 			tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2324 			tcp_set_state(tcp_sk, TCP_CLOSE);
2325 		}
2326 		unlock_sock_fast(tcp_sk, slow);
2327 	}
2328 
2329 	inet_sk_state_store(sk, TCP_CLOSE);
2330 	sk->sk_shutdown = SHUTDOWN_MASK;
2331 	smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2332 	set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2333 
2334 	mptcp_close_wake_up(sk);
2335 }
2336 
2337 static void __mptcp_retrans(struct sock *sk)
2338 {
2339 	struct mptcp_sock *msk = mptcp_sk(sk);
2340 	struct mptcp_sendmsg_info info = {};
2341 	struct mptcp_data_frag *dfrag;
2342 	size_t copied = 0;
2343 	struct sock *ssk;
2344 	int ret;
2345 
2346 	mptcp_clean_una_wakeup(sk);
2347 
2348 	/* first check ssk: need to kick "stale" logic */
2349 	ssk = mptcp_subflow_get_retrans(msk);
2350 	dfrag = mptcp_rtx_head(sk);
2351 	if (!dfrag) {
2352 		if (mptcp_data_fin_enabled(msk)) {
2353 			struct inet_connection_sock *icsk = inet_csk(sk);
2354 
2355 			icsk->icsk_retransmits++;
2356 			mptcp_set_datafin_timeout(sk);
2357 			mptcp_send_ack(msk);
2358 
2359 			goto reset_timer;
2360 		}
2361 
2362 		if (!mptcp_send_head(sk))
2363 			return;
2364 
2365 		goto reset_timer;
2366 	}
2367 
2368 	if (!ssk)
2369 		goto reset_timer;
2370 
2371 	lock_sock(ssk);
2372 
2373 	/* limit retransmission to the bytes already sent on some subflows */
2374 	info.sent = 0;
2375 	info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2376 	while (info.sent < info.limit) {
2377 		ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2378 		if (ret <= 0)
2379 			break;
2380 
2381 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2382 		copied += ret;
2383 		info.sent += ret;
2384 	}
2385 	if (copied) {
2386 		dfrag->already_sent = max(dfrag->already_sent, info.sent);
2387 		tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2388 			 info.size_goal);
2389 	}
2390 
2391 	release_sock(ssk);
2392 
2393 reset_timer:
2394 	mptcp_check_and_set_pending(sk);
2395 
2396 	if (!mptcp_timer_pending(sk))
2397 		mptcp_reset_timer(sk);
2398 }
2399 
2400 static void mptcp_worker(struct work_struct *work)
2401 {
2402 	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2403 	struct sock *sk = &msk->sk.icsk_inet.sk;
2404 	int state;
2405 
2406 	lock_sock(sk);
2407 	state = sk->sk_state;
2408 	if (unlikely(state == TCP_CLOSE))
2409 		goto unlock;
2410 
2411 	mptcp_check_data_fin_ack(sk);
2412 	mptcp_flush_join_list(msk);
2413 
2414 	mptcp_check_fastclose(msk);
2415 
2416 	if (msk->pm.status)
2417 		mptcp_pm_nl_work(msk);
2418 
2419 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2420 		mptcp_check_for_eof(msk);
2421 
2422 	__mptcp_check_send_data_fin(sk);
2423 	mptcp_check_data_fin(sk);
2424 
2425 	/* There is no point in keeping around an orphaned sk timedout or
2426 	 * closed, but we need the msk around to reply to incoming DATA_FIN,
2427 	 * even if it is orphaned and in FIN_WAIT2 state
2428 	 */
2429 	if (sock_flag(sk, SOCK_DEAD) &&
2430 	    (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
2431 		inet_sk_state_store(sk, TCP_CLOSE);
2432 		__mptcp_destroy_sock(sk);
2433 		goto unlock;
2434 	}
2435 
2436 	if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2437 		__mptcp_close_subflow(msk);
2438 
2439 	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2440 		__mptcp_retrans(sk);
2441 
2442 unlock:
2443 	release_sock(sk);
2444 	sock_put(sk);
2445 }
2446 
2447 static int __mptcp_init_sock(struct sock *sk)
2448 {
2449 	struct mptcp_sock *msk = mptcp_sk(sk);
2450 
2451 	spin_lock_init(&msk->join_list_lock);
2452 
2453 	INIT_LIST_HEAD(&msk->conn_list);
2454 	INIT_LIST_HEAD(&msk->join_list);
2455 	INIT_LIST_HEAD(&msk->rtx_queue);
2456 	INIT_WORK(&msk->work, mptcp_worker);
2457 	__skb_queue_head_init(&msk->receive_queue);
2458 	msk->out_of_order_queue = RB_ROOT;
2459 	msk->first_pending = NULL;
2460 	msk->rmem_fwd_alloc = 0;
2461 	WRITE_ONCE(msk->rmem_released, 0);
2462 	msk->timer_ival = TCP_RTO_MIN;
2463 
2464 	msk->first = NULL;
2465 	inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2466 	WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2467 	msk->recovery = false;
2468 
2469 	mptcp_pm_data_init(msk);
2470 
2471 	/* re-use the csk retrans timer for MPTCP-level retrans */
2472 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2473 	timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2474 
2475 	return 0;
2476 }
2477 
2478 static int mptcp_init_sock(struct sock *sk)
2479 {
2480 	struct inet_connection_sock *icsk = inet_csk(sk);
2481 	struct net *net = sock_net(sk);
2482 	int ret;
2483 
2484 	ret = __mptcp_init_sock(sk);
2485 	if (ret)
2486 		return ret;
2487 
2488 	if (!mptcp_is_enabled(net))
2489 		return -ENOPROTOOPT;
2490 
2491 	if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2492 		return -ENOMEM;
2493 
2494 	ret = __mptcp_socket_create(mptcp_sk(sk));
2495 	if (ret)
2496 		return ret;
2497 
2498 	/* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2499 	 * propagate the correct value
2500 	 */
2501 	tcp_assign_congestion_control(sk);
2502 	strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2503 
2504 	/* no need to keep a reference to the ops, the name will suffice */
2505 	tcp_cleanup_congestion_control(sk);
2506 	icsk->icsk_ca_ops = NULL;
2507 
2508 	sk_sockets_allocated_inc(sk);
2509 	sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
2510 	sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
2511 
2512 	return 0;
2513 }
2514 
2515 static void __mptcp_clear_xmit(struct sock *sk)
2516 {
2517 	struct mptcp_sock *msk = mptcp_sk(sk);
2518 	struct mptcp_data_frag *dtmp, *dfrag;
2519 
2520 	WRITE_ONCE(msk->first_pending, NULL);
2521 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2522 		dfrag_clear(sk, dfrag);
2523 }
2524 
2525 static void mptcp_cancel_work(struct sock *sk)
2526 {
2527 	struct mptcp_sock *msk = mptcp_sk(sk);
2528 
2529 	if (cancel_work_sync(&msk->work))
2530 		__sock_put(sk);
2531 }
2532 
2533 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2534 {
2535 	lock_sock(ssk);
2536 
2537 	switch (ssk->sk_state) {
2538 	case TCP_LISTEN:
2539 		if (!(how & RCV_SHUTDOWN))
2540 			break;
2541 		fallthrough;
2542 	case TCP_SYN_SENT:
2543 		tcp_disconnect(ssk, O_NONBLOCK);
2544 		break;
2545 	default:
2546 		if (__mptcp_check_fallback(mptcp_sk(sk))) {
2547 			pr_debug("Fallback");
2548 			ssk->sk_shutdown |= how;
2549 			tcp_shutdown(ssk, how);
2550 		} else {
2551 			pr_debug("Sending DATA_FIN on subflow %p", ssk);
2552 			tcp_send_ack(ssk);
2553 			if (!mptcp_timer_pending(sk))
2554 				mptcp_reset_timer(sk);
2555 		}
2556 		break;
2557 	}
2558 
2559 	release_sock(ssk);
2560 }
2561 
2562 static const unsigned char new_state[16] = {
2563 	/* current state:     new state:      action:	*/
2564 	[0 /* (Invalid) */] = TCP_CLOSE,
2565 	[TCP_ESTABLISHED]   = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2566 	[TCP_SYN_SENT]      = TCP_CLOSE,
2567 	[TCP_SYN_RECV]      = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2568 	[TCP_FIN_WAIT1]     = TCP_FIN_WAIT1,
2569 	[TCP_FIN_WAIT2]     = TCP_FIN_WAIT2,
2570 	[TCP_TIME_WAIT]     = TCP_CLOSE,	/* should not happen ! */
2571 	[TCP_CLOSE]         = TCP_CLOSE,
2572 	[TCP_CLOSE_WAIT]    = TCP_LAST_ACK  | TCP_ACTION_FIN,
2573 	[TCP_LAST_ACK]      = TCP_LAST_ACK,
2574 	[TCP_LISTEN]        = TCP_CLOSE,
2575 	[TCP_CLOSING]       = TCP_CLOSING,
2576 	[TCP_NEW_SYN_RECV]  = TCP_CLOSE,	/* should not happen ! */
2577 };
2578 
2579 static int mptcp_close_state(struct sock *sk)
2580 {
2581 	int next = (int)new_state[sk->sk_state];
2582 	int ns = next & TCP_STATE_MASK;
2583 
2584 	inet_sk_state_store(sk, ns);
2585 
2586 	return next & TCP_ACTION_FIN;
2587 }
2588 
2589 static void __mptcp_check_send_data_fin(struct sock *sk)
2590 {
2591 	struct mptcp_subflow_context *subflow;
2592 	struct mptcp_sock *msk = mptcp_sk(sk);
2593 
2594 	pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2595 		 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2596 		 msk->snd_nxt, msk->write_seq);
2597 
2598 	/* we still need to enqueue subflows or not really shutting down,
2599 	 * skip this
2600 	 */
2601 	if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2602 	    mptcp_send_head(sk))
2603 		return;
2604 
2605 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2606 
2607 	/* fallback socket will not get data_fin/ack, can move to the next
2608 	 * state now
2609 	 */
2610 	if (__mptcp_check_fallback(msk)) {
2611 		if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2612 			inet_sk_state_store(sk, TCP_CLOSE);
2613 			mptcp_close_wake_up(sk);
2614 		} else if (sk->sk_state == TCP_FIN_WAIT1) {
2615 			inet_sk_state_store(sk, TCP_FIN_WAIT2);
2616 		}
2617 	}
2618 
2619 	mptcp_flush_join_list(msk);
2620 	mptcp_for_each_subflow(msk, subflow) {
2621 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2622 
2623 		mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2624 	}
2625 }
2626 
2627 static void __mptcp_wr_shutdown(struct sock *sk)
2628 {
2629 	struct mptcp_sock *msk = mptcp_sk(sk);
2630 
2631 	pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2632 		 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2633 		 !!mptcp_send_head(sk));
2634 
2635 	/* will be ignored by fallback sockets */
2636 	WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2637 	WRITE_ONCE(msk->snd_data_fin_enable, 1);
2638 
2639 	__mptcp_check_send_data_fin(sk);
2640 }
2641 
2642 static void __mptcp_destroy_sock(struct sock *sk)
2643 {
2644 	struct mptcp_subflow_context *subflow, *tmp;
2645 	struct mptcp_sock *msk = mptcp_sk(sk);
2646 	LIST_HEAD(conn_list);
2647 
2648 	pr_debug("msk=%p", msk);
2649 
2650 	might_sleep();
2651 
2652 	/* be sure to always acquire the join list lock, to sync vs
2653 	 * mptcp_finish_join().
2654 	 */
2655 	spin_lock_bh(&msk->join_list_lock);
2656 	list_splice_tail_init(&msk->join_list, &msk->conn_list);
2657 	spin_unlock_bh(&msk->join_list_lock);
2658 	list_splice_init(&msk->conn_list, &conn_list);
2659 
2660 	sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
2661 	sk_stop_timer(sk, &sk->sk_timer);
2662 	msk->pm.status = 0;
2663 
2664 	list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
2665 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2666 		__mptcp_close_ssk(sk, ssk, subflow);
2667 	}
2668 
2669 	sk->sk_prot->destroy(sk);
2670 
2671 	WARN_ON_ONCE(msk->rmem_fwd_alloc);
2672 	WARN_ON_ONCE(msk->rmem_released);
2673 	sk_stream_kill_queues(sk);
2674 	xfrm_sk_free_policy(sk);
2675 
2676 	sk_refcnt_debug_release(sk);
2677 	mptcp_dispose_initial_subflow(msk);
2678 	sock_put(sk);
2679 }
2680 
2681 static void mptcp_close(struct sock *sk, long timeout)
2682 {
2683 	struct mptcp_subflow_context *subflow;
2684 	bool do_cancel_work = false;
2685 
2686 	lock_sock(sk);
2687 	sk->sk_shutdown = SHUTDOWN_MASK;
2688 
2689 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2690 		inet_sk_state_store(sk, TCP_CLOSE);
2691 		goto cleanup;
2692 	}
2693 
2694 	if (mptcp_close_state(sk))
2695 		__mptcp_wr_shutdown(sk);
2696 
2697 	sk_stream_wait_close(sk, timeout);
2698 
2699 cleanup:
2700 	/* orphan all the subflows */
2701 	inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2702 	mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2703 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2704 		bool slow = lock_sock_fast_nested(ssk);
2705 
2706 		sock_orphan(ssk);
2707 		unlock_sock_fast(ssk, slow);
2708 	}
2709 	sock_orphan(sk);
2710 
2711 	sock_hold(sk);
2712 	pr_debug("msk=%p state=%d", sk, sk->sk_state);
2713 	if (sk->sk_state == TCP_CLOSE) {
2714 		__mptcp_destroy_sock(sk);
2715 		do_cancel_work = true;
2716 	} else {
2717 		sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2718 	}
2719 	release_sock(sk);
2720 	if (do_cancel_work)
2721 		mptcp_cancel_work(sk);
2722 
2723 	if (mptcp_sk(sk)->token)
2724 		mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2725 
2726 	sock_put(sk);
2727 }
2728 
2729 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2730 {
2731 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2732 	const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2733 	struct ipv6_pinfo *msk6 = inet6_sk(msk);
2734 
2735 	msk->sk_v6_daddr = ssk->sk_v6_daddr;
2736 	msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2737 
2738 	if (msk6 && ssk6) {
2739 		msk6->saddr = ssk6->saddr;
2740 		msk6->flow_label = ssk6->flow_label;
2741 	}
2742 #endif
2743 
2744 	inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2745 	inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2746 	inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2747 	inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2748 	inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2749 	inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2750 }
2751 
2752 static int mptcp_disconnect(struct sock *sk, int flags)
2753 {
2754 	struct mptcp_subflow_context *subflow;
2755 	struct mptcp_sock *msk = mptcp_sk(sk);
2756 
2757 	mptcp_do_flush_join_list(msk);
2758 
2759 	mptcp_for_each_subflow(msk, subflow) {
2760 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2761 
2762 		lock_sock(ssk);
2763 		tcp_disconnect(ssk, flags);
2764 		release_sock(ssk);
2765 	}
2766 	return 0;
2767 }
2768 
2769 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2770 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2771 {
2772 	unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2773 
2774 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2775 }
2776 #endif
2777 
2778 struct sock *mptcp_sk_clone(const struct sock *sk,
2779 			    const struct mptcp_options_received *mp_opt,
2780 			    struct request_sock *req)
2781 {
2782 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2783 	struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
2784 	struct mptcp_sock *msk;
2785 	u64 ack_seq;
2786 
2787 	if (!nsk)
2788 		return NULL;
2789 
2790 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2791 	if (nsk->sk_family == AF_INET6)
2792 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2793 #endif
2794 
2795 	__mptcp_init_sock(nsk);
2796 
2797 	msk = mptcp_sk(nsk);
2798 	msk->local_key = subflow_req->local_key;
2799 	msk->token = subflow_req->token;
2800 	msk->subflow = NULL;
2801 	WRITE_ONCE(msk->fully_established, false);
2802 	if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
2803 		WRITE_ONCE(msk->csum_enabled, true);
2804 
2805 	msk->write_seq = subflow_req->idsn + 1;
2806 	msk->snd_nxt = msk->write_seq;
2807 	msk->snd_una = msk->write_seq;
2808 	msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
2809 	msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
2810 
2811 	if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
2812 		msk->can_ack = true;
2813 		msk->remote_key = mp_opt->sndr_key;
2814 		mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2815 		ack_seq++;
2816 		WRITE_ONCE(msk->ack_seq, ack_seq);
2817 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
2818 	}
2819 
2820 	sock_reset_flag(nsk, SOCK_RCU_FREE);
2821 	/* will be fully established after successful MPC subflow creation */
2822 	inet_sk_state_store(nsk, TCP_SYN_RECV);
2823 
2824 	security_inet_csk_clone(nsk, req);
2825 	bh_unlock_sock(nsk);
2826 
2827 	/* keep a single reference */
2828 	__sock_put(nsk);
2829 	return nsk;
2830 }
2831 
2832 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2833 {
2834 	const struct tcp_sock *tp = tcp_sk(ssk);
2835 
2836 	msk->rcvq_space.copied = 0;
2837 	msk->rcvq_space.rtt_us = 0;
2838 
2839 	msk->rcvq_space.time = tp->tcp_mstamp;
2840 
2841 	/* initial rcv_space offering made to peer */
2842 	msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2843 				      TCP_INIT_CWND * tp->advmss);
2844 	if (msk->rcvq_space.space == 0)
2845 		msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
2846 
2847 	WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
2848 }
2849 
2850 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2851 				 bool kern)
2852 {
2853 	struct mptcp_sock *msk = mptcp_sk(sk);
2854 	struct socket *listener;
2855 	struct sock *newsk;
2856 
2857 	listener = __mptcp_nmpc_socket(msk);
2858 	if (WARN_ON_ONCE(!listener)) {
2859 		*err = -EINVAL;
2860 		return NULL;
2861 	}
2862 
2863 	pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2864 	newsk = inet_csk_accept(listener->sk, flags, err, kern);
2865 	if (!newsk)
2866 		return NULL;
2867 
2868 	pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
2869 	if (sk_is_mptcp(newsk)) {
2870 		struct mptcp_subflow_context *subflow;
2871 		struct sock *new_mptcp_sock;
2872 
2873 		subflow = mptcp_subflow_ctx(newsk);
2874 		new_mptcp_sock = subflow->conn;
2875 
2876 		/* is_mptcp should be false if subflow->conn is missing, see
2877 		 * subflow_syn_recv_sock()
2878 		 */
2879 		if (WARN_ON_ONCE(!new_mptcp_sock)) {
2880 			tcp_sk(newsk)->is_mptcp = 0;
2881 			return newsk;
2882 		}
2883 
2884 		/* acquire the 2nd reference for the owning socket */
2885 		sock_hold(new_mptcp_sock);
2886 		newsk = new_mptcp_sock;
2887 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
2888 	} else {
2889 		MPTCP_INC_STATS(sock_net(sk),
2890 				MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
2891 	}
2892 
2893 	return newsk;
2894 }
2895 
2896 void mptcp_destroy_common(struct mptcp_sock *msk)
2897 {
2898 	struct sock *sk = (struct sock *)msk;
2899 
2900 	__mptcp_clear_xmit(sk);
2901 
2902 	/* move to sk_receive_queue, sk_stream_kill_queues will purge it */
2903 	skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
2904 	__skb_queue_purge(&sk->sk_receive_queue);
2905 	skb_rbtree_purge(&msk->out_of_order_queue);
2906 
2907 	/* move all the rx fwd alloc into the sk_mem_reclaim_final in
2908 	 * inet_sock_destruct() will dispose it
2909 	 */
2910 	sk->sk_forward_alloc += msk->rmem_fwd_alloc;
2911 	msk->rmem_fwd_alloc = 0;
2912 	mptcp_token_destroy(msk);
2913 	mptcp_pm_free_anno_list(msk);
2914 }
2915 
2916 static void mptcp_destroy(struct sock *sk)
2917 {
2918 	struct mptcp_sock *msk = mptcp_sk(sk);
2919 
2920 	mptcp_destroy_common(msk);
2921 	sk_sockets_allocated_dec(sk);
2922 }
2923 
2924 void __mptcp_data_acked(struct sock *sk)
2925 {
2926 	if (!sock_owned_by_user(sk))
2927 		__mptcp_clean_una(sk);
2928 	else
2929 		set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
2930 
2931 	if (mptcp_pending_data_fin_ack(sk))
2932 		mptcp_schedule_work(sk);
2933 }
2934 
2935 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
2936 {
2937 	if (!mptcp_send_head(sk))
2938 		return;
2939 
2940 	if (!sock_owned_by_user(sk)) {
2941 		struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
2942 
2943 		if (xmit_ssk == ssk)
2944 			__mptcp_subflow_push_pending(sk, ssk);
2945 		else if (xmit_ssk)
2946 			mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
2947 	} else {
2948 		set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
2949 	}
2950 }
2951 
2952 /* processes deferred events and flush wmem */
2953 static void mptcp_release_cb(struct sock *sk)
2954 {
2955 	for (;;) {
2956 		unsigned long flags = 0;
2957 
2958 		if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
2959 			flags |= BIT(MPTCP_PUSH_PENDING);
2960 		if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
2961 			flags |= BIT(MPTCP_RETRANSMIT);
2962 		if (!flags)
2963 			break;
2964 
2965 		/* the following actions acquire the subflow socket lock
2966 		 *
2967 		 * 1) can't be invoked in atomic scope
2968 		 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
2969 		 *    datapath acquires the msk socket spinlock while helding
2970 		 *    the subflow socket lock
2971 		 */
2972 
2973 		spin_unlock_bh(&sk->sk_lock.slock);
2974 		if (flags & BIT(MPTCP_PUSH_PENDING))
2975 			__mptcp_push_pending(sk, 0);
2976 		if (flags & BIT(MPTCP_RETRANSMIT))
2977 			__mptcp_retrans(sk);
2978 
2979 		cond_resched();
2980 		spin_lock_bh(&sk->sk_lock.slock);
2981 	}
2982 
2983 	/* be sure to set the current sk state before tacking actions
2984 	 * depending on sk_state
2985 	 */
2986 	if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags))
2987 		__mptcp_set_connected(sk);
2988 	if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
2989 		__mptcp_clean_una_wakeup(sk);
2990 	if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
2991 		__mptcp_error_report(sk);
2992 
2993 	__mptcp_update_rmem(sk);
2994 }
2995 
2996 void mptcp_subflow_process_delegated(struct sock *ssk)
2997 {
2998 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2999 	struct sock *sk = subflow->conn;
3000 
3001 	mptcp_data_lock(sk);
3002 	if (!sock_owned_by_user(sk))
3003 		__mptcp_subflow_push_pending(sk, ssk);
3004 	else
3005 		set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
3006 	mptcp_data_unlock(sk);
3007 	mptcp_subflow_delegated_done(subflow);
3008 }
3009 
3010 static int mptcp_hash(struct sock *sk)
3011 {
3012 	/* should never be called,
3013 	 * we hash the TCP subflows not the master socket
3014 	 */
3015 	WARN_ON_ONCE(1);
3016 	return 0;
3017 }
3018 
3019 static void mptcp_unhash(struct sock *sk)
3020 {
3021 	/* called from sk_common_release(), but nothing to do here */
3022 }
3023 
3024 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3025 {
3026 	struct mptcp_sock *msk = mptcp_sk(sk);
3027 	struct socket *ssock;
3028 
3029 	ssock = __mptcp_nmpc_socket(msk);
3030 	pr_debug("msk=%p, subflow=%p", msk, ssock);
3031 	if (WARN_ON_ONCE(!ssock))
3032 		return -EINVAL;
3033 
3034 	return inet_csk_get_port(ssock->sk, snum);
3035 }
3036 
3037 void mptcp_finish_connect(struct sock *ssk)
3038 {
3039 	struct mptcp_subflow_context *subflow;
3040 	struct mptcp_sock *msk;
3041 	struct sock *sk;
3042 	u64 ack_seq;
3043 
3044 	subflow = mptcp_subflow_ctx(ssk);
3045 	sk = subflow->conn;
3046 	msk = mptcp_sk(sk);
3047 
3048 	pr_debug("msk=%p, token=%u", sk, subflow->token);
3049 
3050 	mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3051 	ack_seq++;
3052 	subflow->map_seq = ack_seq;
3053 	subflow->map_subflow_seq = 1;
3054 
3055 	/* the socket is not connected yet, no msk/subflow ops can access/race
3056 	 * accessing the field below
3057 	 */
3058 	WRITE_ONCE(msk->remote_key, subflow->remote_key);
3059 	WRITE_ONCE(msk->local_key, subflow->local_key);
3060 	WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3061 	WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3062 	WRITE_ONCE(msk->ack_seq, ack_seq);
3063 	WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
3064 	WRITE_ONCE(msk->can_ack, 1);
3065 	WRITE_ONCE(msk->snd_una, msk->write_seq);
3066 
3067 	mptcp_pm_new_connection(msk, ssk, 0);
3068 
3069 	mptcp_rcv_space_init(msk, ssk);
3070 }
3071 
3072 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3073 {
3074 	write_lock_bh(&sk->sk_callback_lock);
3075 	rcu_assign_pointer(sk->sk_wq, &parent->wq);
3076 	sk_set_socket(sk, parent);
3077 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
3078 	write_unlock_bh(&sk->sk_callback_lock);
3079 }
3080 
3081 bool mptcp_finish_join(struct sock *ssk)
3082 {
3083 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3084 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3085 	struct sock *parent = (void *)msk;
3086 	struct socket *parent_sock;
3087 	bool ret;
3088 
3089 	pr_debug("msk=%p, subflow=%p", msk, subflow);
3090 
3091 	/* mptcp socket already closing? */
3092 	if (!mptcp_is_fully_established(parent)) {
3093 		subflow->reset_reason = MPTCP_RST_EMPTCP;
3094 		return false;
3095 	}
3096 
3097 	if (!msk->pm.server_side)
3098 		goto out;
3099 
3100 	if (!mptcp_pm_allow_new_subflow(msk)) {
3101 		subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3102 		return false;
3103 	}
3104 
3105 	/* active connections are already on conn_list, and we can't acquire
3106 	 * msk lock here.
3107 	 * use the join list lock as synchronization point and double-check
3108 	 * msk status to avoid racing with __mptcp_destroy_sock()
3109 	 */
3110 	spin_lock_bh(&msk->join_list_lock);
3111 	ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
3112 	if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
3113 		list_add_tail(&subflow->node, &msk->join_list);
3114 		sock_hold(ssk);
3115 	}
3116 	spin_unlock_bh(&msk->join_list_lock);
3117 	if (!ret) {
3118 		subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3119 		return false;
3120 	}
3121 
3122 	/* attach to msk socket only after we are sure he will deal with us
3123 	 * at close time
3124 	 */
3125 	parent_sock = READ_ONCE(parent->sk_socket);
3126 	if (parent_sock && !ssk->sk_socket)
3127 		mptcp_sock_graft(ssk, parent_sock);
3128 	subflow->map_seq = READ_ONCE(msk->ack_seq);
3129 out:
3130 	mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
3131 	return true;
3132 }
3133 
3134 static void mptcp_shutdown(struct sock *sk, int how)
3135 {
3136 	pr_debug("sk=%p, how=%d", sk, how);
3137 
3138 	if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3139 		__mptcp_wr_shutdown(sk);
3140 }
3141 
3142 static int mptcp_forward_alloc_get(const struct sock *sk)
3143 {
3144 	return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3145 }
3146 
3147 static struct proto mptcp_prot = {
3148 	.name		= "MPTCP",
3149 	.owner		= THIS_MODULE,
3150 	.init		= mptcp_init_sock,
3151 	.disconnect	= mptcp_disconnect,
3152 	.close		= mptcp_close,
3153 	.accept		= mptcp_accept,
3154 	.setsockopt	= mptcp_setsockopt,
3155 	.getsockopt	= mptcp_getsockopt,
3156 	.shutdown	= mptcp_shutdown,
3157 	.destroy	= mptcp_destroy,
3158 	.sendmsg	= mptcp_sendmsg,
3159 	.recvmsg	= mptcp_recvmsg,
3160 	.release_cb	= mptcp_release_cb,
3161 	.hash		= mptcp_hash,
3162 	.unhash		= mptcp_unhash,
3163 	.get_port	= mptcp_get_port,
3164 	.forward_alloc_get	= mptcp_forward_alloc_get,
3165 	.sockets_allocated	= &mptcp_sockets_allocated,
3166 	.memory_allocated	= &tcp_memory_allocated,
3167 	.memory_pressure	= &tcp_memory_pressure,
3168 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
3169 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
3170 	.sysctl_mem	= sysctl_tcp_mem,
3171 	.obj_size	= sizeof(struct mptcp_sock),
3172 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
3173 	.no_autobind	= true,
3174 };
3175 
3176 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3177 {
3178 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3179 	struct socket *ssock;
3180 	int err;
3181 
3182 	lock_sock(sock->sk);
3183 	ssock = __mptcp_nmpc_socket(msk);
3184 	if (!ssock) {
3185 		err = -EINVAL;
3186 		goto unlock;
3187 	}
3188 
3189 	err = ssock->ops->bind(ssock, uaddr, addr_len);
3190 	if (!err)
3191 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
3192 
3193 unlock:
3194 	release_sock(sock->sk);
3195 	return err;
3196 }
3197 
3198 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3199 					 struct mptcp_subflow_context *subflow)
3200 {
3201 	subflow->request_mptcp = 0;
3202 	__mptcp_do_fallback(msk);
3203 }
3204 
3205 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3206 				int addr_len, int flags)
3207 {
3208 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3209 	struct mptcp_subflow_context *subflow;
3210 	struct socket *ssock;
3211 	int err;
3212 
3213 	lock_sock(sock->sk);
3214 	if (sock->state != SS_UNCONNECTED && msk->subflow) {
3215 		/* pending connection or invalid state, let existing subflow
3216 		 * cope with that
3217 		 */
3218 		ssock = msk->subflow;
3219 		goto do_connect;
3220 	}
3221 
3222 	ssock = __mptcp_nmpc_socket(msk);
3223 	if (!ssock) {
3224 		err = -EINVAL;
3225 		goto unlock;
3226 	}
3227 
3228 	mptcp_token_destroy(msk);
3229 	inet_sk_state_store(sock->sk, TCP_SYN_SENT);
3230 	subflow = mptcp_subflow_ctx(ssock->sk);
3231 #ifdef CONFIG_TCP_MD5SIG
3232 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3233 	 * TCP option space.
3234 	 */
3235 	if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3236 		mptcp_subflow_early_fallback(msk, subflow);
3237 #endif
3238 	if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3239 		MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3240 		mptcp_subflow_early_fallback(msk, subflow);
3241 	}
3242 	if (likely(!__mptcp_check_fallback(msk)))
3243 		MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
3244 
3245 do_connect:
3246 	err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
3247 	sock->state = ssock->state;
3248 
3249 	/* on successful connect, the msk state will be moved to established by
3250 	 * subflow_finish_connect()
3251 	 */
3252 	if (!err || err == -EINPROGRESS)
3253 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
3254 	else
3255 		inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3256 
3257 unlock:
3258 	release_sock(sock->sk);
3259 	return err;
3260 }
3261 
3262 static int mptcp_listen(struct socket *sock, int backlog)
3263 {
3264 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3265 	struct socket *ssock;
3266 	int err;
3267 
3268 	pr_debug("msk=%p", msk);
3269 
3270 	lock_sock(sock->sk);
3271 	ssock = __mptcp_nmpc_socket(msk);
3272 	if (!ssock) {
3273 		err = -EINVAL;
3274 		goto unlock;
3275 	}
3276 
3277 	mptcp_token_destroy(msk);
3278 	inet_sk_state_store(sock->sk, TCP_LISTEN);
3279 	sock_set_flag(sock->sk, SOCK_RCU_FREE);
3280 
3281 	err = ssock->ops->listen(ssock, backlog);
3282 	inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3283 	if (!err)
3284 		mptcp_copy_inaddrs(sock->sk, ssock->sk);
3285 
3286 unlock:
3287 	release_sock(sock->sk);
3288 	return err;
3289 }
3290 
3291 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3292 			       int flags, bool kern)
3293 {
3294 	struct mptcp_sock *msk = mptcp_sk(sock->sk);
3295 	struct socket *ssock;
3296 	int err;
3297 
3298 	pr_debug("msk=%p", msk);
3299 
3300 	lock_sock(sock->sk);
3301 	if (sock->sk->sk_state != TCP_LISTEN)
3302 		goto unlock_fail;
3303 
3304 	ssock = __mptcp_nmpc_socket(msk);
3305 	if (!ssock)
3306 		goto unlock_fail;
3307 
3308 	clear_bit(MPTCP_DATA_READY, &msk->flags);
3309 	sock_hold(ssock->sk);
3310 	release_sock(sock->sk);
3311 
3312 	err = ssock->ops->accept(sock, newsock, flags, kern);
3313 	if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3314 		struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3315 		struct mptcp_subflow_context *subflow;
3316 		struct sock *newsk = newsock->sk;
3317 
3318 		lock_sock(newsk);
3319 
3320 		/* PM/worker can now acquire the first subflow socket
3321 		 * lock without racing with listener queue cleanup,
3322 		 * we can notify it, if needed.
3323 		 *
3324 		 * Even if remote has reset the initial subflow by now
3325 		 * the refcnt is still at least one.
3326 		 */
3327 		subflow = mptcp_subflow_ctx(msk->first);
3328 		list_add(&subflow->node, &msk->conn_list);
3329 		sock_hold(msk->first);
3330 		if (mptcp_is_fully_established(newsk))
3331 			mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
3332 
3333 		mptcp_copy_inaddrs(newsk, msk->first);
3334 		mptcp_rcv_space_init(msk, msk->first);
3335 		mptcp_propagate_sndbuf(newsk, msk->first);
3336 
3337 		/* set ssk->sk_socket of accept()ed flows to mptcp socket.
3338 		 * This is needed so NOSPACE flag can be set from tcp stack.
3339 		 */
3340 		mptcp_flush_join_list(msk);
3341 		mptcp_for_each_subflow(msk, subflow) {
3342 			struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3343 
3344 			if (!ssk->sk_socket)
3345 				mptcp_sock_graft(ssk, newsock);
3346 		}
3347 		release_sock(newsk);
3348 	}
3349 
3350 	if (inet_csk_listen_poll(ssock->sk))
3351 		set_bit(MPTCP_DATA_READY, &msk->flags);
3352 	sock_put(ssock->sk);
3353 	return err;
3354 
3355 unlock_fail:
3356 	release_sock(sock->sk);
3357 	return -EINVAL;
3358 }
3359 
3360 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3361 {
3362 	/* Concurrent splices from sk_receive_queue into receive_queue will
3363 	 * always show at least one non-empty queue when checked in this order.
3364 	 */
3365 	if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
3366 	    skb_queue_empty_lockless(&msk->receive_queue))
3367 		return 0;
3368 
3369 	return EPOLLIN | EPOLLRDNORM;
3370 }
3371 
3372 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3373 {
3374 	struct sock *sk = (struct sock *)msk;
3375 
3376 	if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
3377 		return EPOLLOUT | EPOLLWRNORM;
3378 
3379 	if (sk_stream_is_writeable(sk))
3380 		return EPOLLOUT | EPOLLWRNORM;
3381 
3382 	mptcp_set_nospace(sk);
3383 	smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3384 	if (sk_stream_is_writeable(sk))
3385 		return EPOLLOUT | EPOLLWRNORM;
3386 
3387 	return 0;
3388 }
3389 
3390 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3391 			   struct poll_table_struct *wait)
3392 {
3393 	struct sock *sk = sock->sk;
3394 	struct mptcp_sock *msk;
3395 	__poll_t mask = 0;
3396 	int state;
3397 
3398 	msk = mptcp_sk(sk);
3399 	sock_poll_wait(file, sock, wait);
3400 
3401 	state = inet_sk_state_load(sk);
3402 	pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3403 	if (state == TCP_LISTEN)
3404 		return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 0;
3405 
3406 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3407 		mask |= mptcp_check_readable(msk);
3408 		mask |= mptcp_check_writeable(msk);
3409 	}
3410 	if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3411 		mask |= EPOLLHUP;
3412 	if (sk->sk_shutdown & RCV_SHUTDOWN)
3413 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3414 
3415 	/* This barrier is coupled with smp_wmb() in tcp_reset() */
3416 	smp_rmb();
3417 	if (sk->sk_err)
3418 		mask |= EPOLLERR;
3419 
3420 	return mask;
3421 }
3422 
3423 static const struct proto_ops mptcp_stream_ops = {
3424 	.family		   = PF_INET,
3425 	.owner		   = THIS_MODULE,
3426 	.release	   = inet_release,
3427 	.bind		   = mptcp_bind,
3428 	.connect	   = mptcp_stream_connect,
3429 	.socketpair	   = sock_no_socketpair,
3430 	.accept		   = mptcp_stream_accept,
3431 	.getname	   = inet_getname,
3432 	.poll		   = mptcp_poll,
3433 	.ioctl		   = inet_ioctl,
3434 	.gettstamp	   = sock_gettstamp,
3435 	.listen		   = mptcp_listen,
3436 	.shutdown	   = inet_shutdown,
3437 	.setsockopt	   = sock_common_setsockopt,
3438 	.getsockopt	   = sock_common_getsockopt,
3439 	.sendmsg	   = inet_sendmsg,
3440 	.recvmsg	   = inet_recvmsg,
3441 	.mmap		   = sock_no_mmap,
3442 	.sendpage	   = inet_sendpage,
3443 };
3444 
3445 static struct inet_protosw mptcp_protosw = {
3446 	.type		= SOCK_STREAM,
3447 	.protocol	= IPPROTO_MPTCP,
3448 	.prot		= &mptcp_prot,
3449 	.ops		= &mptcp_stream_ops,
3450 	.flags		= INET_PROTOSW_ICSK,
3451 };
3452 
3453 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3454 {
3455 	struct mptcp_delegated_action *delegated;
3456 	struct mptcp_subflow_context *subflow;
3457 	int work_done = 0;
3458 
3459 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
3460 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3461 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3462 
3463 		bh_lock_sock_nested(ssk);
3464 		if (!sock_owned_by_user(ssk) &&
3465 		    mptcp_subflow_has_delegated_action(subflow))
3466 			mptcp_subflow_process_delegated(ssk);
3467 		/* ... elsewhere tcp_release_cb_override already processed
3468 		 * the action or will do at next release_sock().
3469 		 * In both case must dequeue the subflow here - on the same
3470 		 * CPU that scheduled it.
3471 		 */
3472 		bh_unlock_sock(ssk);
3473 		sock_put(ssk);
3474 
3475 		if (++work_done == budget)
3476 			return budget;
3477 	}
3478 
3479 	/* always provide a 0 'work_done' argument, so that napi_complete_done
3480 	 * will not try accessing the NULL napi->dev ptr
3481 	 */
3482 	napi_complete_done(napi, 0);
3483 	return work_done;
3484 }
3485 
3486 void __init mptcp_proto_init(void)
3487 {
3488 	struct mptcp_delegated_action *delegated;
3489 	int cpu;
3490 
3491 	mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3492 
3493 	if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3494 		panic("Failed to allocate MPTCP pcpu counter\n");
3495 
3496 	init_dummy_netdev(&mptcp_napi_dev);
3497 	for_each_possible_cpu(cpu) {
3498 		delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3499 		INIT_LIST_HEAD(&delegated->head);
3500 		netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3501 				  NAPI_POLL_WEIGHT);
3502 		napi_enable(&delegated->napi);
3503 	}
3504 
3505 	mptcp_subflow_init();
3506 	mptcp_pm_init();
3507 	mptcp_token_init();
3508 
3509 	if (proto_register(&mptcp_prot, 1) != 0)
3510 		panic("Failed to register MPTCP proto.\n");
3511 
3512 	inet_register_protosw(&mptcp_protosw);
3513 
3514 	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3515 }
3516 
3517 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3518 static const struct proto_ops mptcp_v6_stream_ops = {
3519 	.family		   = PF_INET6,
3520 	.owner		   = THIS_MODULE,
3521 	.release	   = inet6_release,
3522 	.bind		   = mptcp_bind,
3523 	.connect	   = mptcp_stream_connect,
3524 	.socketpair	   = sock_no_socketpair,
3525 	.accept		   = mptcp_stream_accept,
3526 	.getname	   = inet6_getname,
3527 	.poll		   = mptcp_poll,
3528 	.ioctl		   = inet6_ioctl,
3529 	.gettstamp	   = sock_gettstamp,
3530 	.listen		   = mptcp_listen,
3531 	.shutdown	   = inet_shutdown,
3532 	.setsockopt	   = sock_common_setsockopt,
3533 	.getsockopt	   = sock_common_getsockopt,
3534 	.sendmsg	   = inet6_sendmsg,
3535 	.recvmsg	   = inet6_recvmsg,
3536 	.mmap		   = sock_no_mmap,
3537 	.sendpage	   = inet_sendpage,
3538 #ifdef CONFIG_COMPAT
3539 	.compat_ioctl	   = inet6_compat_ioctl,
3540 #endif
3541 };
3542 
3543 static struct proto mptcp_v6_prot;
3544 
3545 static void mptcp_v6_destroy(struct sock *sk)
3546 {
3547 	mptcp_destroy(sk);
3548 	inet6_destroy_sock(sk);
3549 }
3550 
3551 static struct inet_protosw mptcp_v6_protosw = {
3552 	.type		= SOCK_STREAM,
3553 	.protocol	= IPPROTO_MPTCP,
3554 	.prot		= &mptcp_v6_prot,
3555 	.ops		= &mptcp_v6_stream_ops,
3556 	.flags		= INET_PROTOSW_ICSK,
3557 };
3558 
3559 int __init mptcp_proto_v6_init(void)
3560 {
3561 	int err;
3562 
3563 	mptcp_v6_prot = mptcp_prot;
3564 	strcpy(mptcp_v6_prot.name, "MPTCPv6");
3565 	mptcp_v6_prot.slab = NULL;
3566 	mptcp_v6_prot.destroy = mptcp_v6_destroy;
3567 	mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3568 
3569 	err = proto_register(&mptcp_v6_prot, 1);
3570 	if (err)
3571 		return err;
3572 
3573 	err = inet6_register_protosw(&mptcp_v6_protosw);
3574 	if (err)
3575 		proto_unregister(&mptcp_v6_prot);
3576 
3577 	return err;
3578 }
3579 #endif
3580