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