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