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