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