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