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