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