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