xref: /openbmc/linux/net/mptcp/subflow.c (revision 226bf980)
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 <crypto/algapi.h>
13 #include <crypto/sha2.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 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/ip6_route.h>
21 #include <net/transp_v6.h>
22 #endif
23 #include <net/mptcp.h>
24 #include <uapi/linux/mptcp.h>
25 #include "protocol.h"
26 #include "mib.h"
27 
28 #include <trace/events/mptcp.h>
29 
30 static void mptcp_subflow_ops_undo_override(struct sock *ssk);
31 
32 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
33 				  enum linux_mptcp_mib_field field)
34 {
35 	MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
36 }
37 
38 static void subflow_req_destructor(struct request_sock *req)
39 {
40 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
41 
42 	pr_debug("subflow_req=%p", subflow_req);
43 
44 	if (subflow_req->msk)
45 		sock_put((struct sock *)subflow_req->msk);
46 
47 	mptcp_token_destroy_request(req);
48 	tcp_request_sock_ops.destructor(req);
49 }
50 
51 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
52 				  void *hmac)
53 {
54 	u8 msg[8];
55 
56 	put_unaligned_be32(nonce1, &msg[0]);
57 	put_unaligned_be32(nonce2, &msg[4]);
58 
59 	mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
60 }
61 
62 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
63 {
64 	return mptcp_is_fully_established((void *)msk) &&
65 		((mptcp_pm_is_userspace(msk) &&
66 		  mptcp_userspace_pm_active(msk)) ||
67 		 READ_ONCE(msk->pm.accept_subflow));
68 }
69 
70 /* validate received token and create truncated hmac and nonce for SYN-ACK */
71 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
72 {
73 	struct mptcp_sock *msk = subflow_req->msk;
74 	u8 hmac[SHA256_DIGEST_SIZE];
75 
76 	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
77 
78 	subflow_generate_hmac(msk->local_key, msk->remote_key,
79 			      subflow_req->local_nonce,
80 			      subflow_req->remote_nonce, hmac);
81 
82 	subflow_req->thmac = get_unaligned_be64(hmac);
83 }
84 
85 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
86 {
87 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
88 	struct mptcp_sock *msk;
89 	int local_id;
90 
91 	msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token);
92 	if (!msk) {
93 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
94 		return NULL;
95 	}
96 
97 	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
98 	if (local_id < 0) {
99 		sock_put((struct sock *)msk);
100 		return NULL;
101 	}
102 	subflow_req->local_id = local_id;
103 
104 	return msk;
105 }
106 
107 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
108 {
109 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
110 
111 	subflow_req->mp_capable = 0;
112 	subflow_req->mp_join = 0;
113 	subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener));
114 	subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
115 	subflow_req->msk = NULL;
116 	mptcp_token_init_request(req);
117 }
118 
119 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
120 {
121 	return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
122 }
123 
124 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
125 {
126 	struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
127 
128 	if (mpext) {
129 		memset(mpext, 0, sizeof(*mpext));
130 		mpext->reset_reason = reason;
131 	}
132 }
133 
134 /* Init mptcp request socket.
135  *
136  * Returns an error code if a JOIN has failed and a TCP reset
137  * should be sent.
138  */
139 static int subflow_check_req(struct request_sock *req,
140 			     const struct sock *sk_listener,
141 			     struct sk_buff *skb)
142 {
143 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
144 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
145 	struct mptcp_options_received mp_opt;
146 	bool opt_mp_capable, opt_mp_join;
147 
148 	pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
149 
150 #ifdef CONFIG_TCP_MD5SIG
151 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
152 	 * TCP option space.
153 	 */
154 	if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
155 		return -EINVAL;
156 #endif
157 
158 	mptcp_get_options(skb, &mp_opt);
159 
160 	opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC);
161 	opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
162 	if (opt_mp_capable) {
163 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
164 
165 		if (opt_mp_join)
166 			return 0;
167 	} else if (opt_mp_join) {
168 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
169 	}
170 
171 	if (opt_mp_capable && listener->request_mptcp) {
172 		int err, retries = MPTCP_TOKEN_MAX_RETRIES;
173 
174 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
175 again:
176 		do {
177 			get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
178 		} while (subflow_req->local_key == 0);
179 
180 		if (unlikely(req->syncookie)) {
181 			mptcp_crypto_key_sha(subflow_req->local_key,
182 					     &subflow_req->token,
183 					     &subflow_req->idsn);
184 			if (mptcp_token_exists(subflow_req->token)) {
185 				if (retries-- > 0)
186 					goto again;
187 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
188 			} else {
189 				subflow_req->mp_capable = 1;
190 			}
191 			return 0;
192 		}
193 
194 		err = mptcp_token_new_request(req);
195 		if (err == 0)
196 			subflow_req->mp_capable = 1;
197 		else if (retries-- > 0)
198 			goto again;
199 		else
200 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
201 
202 	} else if (opt_mp_join && listener->request_mptcp) {
203 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
204 		subflow_req->mp_join = 1;
205 		subflow_req->backup = mp_opt.backup;
206 		subflow_req->remote_id = mp_opt.join_id;
207 		subflow_req->token = mp_opt.token;
208 		subflow_req->remote_nonce = mp_opt.nonce;
209 		subflow_req->msk = subflow_token_join_request(req);
210 
211 		/* Can't fall back to TCP in this case. */
212 		if (!subflow_req->msk) {
213 			subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
214 			return -EPERM;
215 		}
216 
217 		if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
218 			pr_debug("syn inet_sport=%d %d",
219 				 ntohs(inet_sk(sk_listener)->inet_sport),
220 				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
221 			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
222 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
223 				return -EPERM;
224 			}
225 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
226 		}
227 
228 		subflow_req_create_thmac(subflow_req);
229 
230 		if (unlikely(req->syncookie)) {
231 			if (mptcp_can_accept_new_subflow(subflow_req->msk))
232 				subflow_init_req_cookie_join_save(subflow_req, skb);
233 			else
234 				return -EPERM;
235 		}
236 
237 		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
238 			 subflow_req->remote_nonce, subflow_req->msk);
239 	}
240 
241 	return 0;
242 }
243 
244 int mptcp_subflow_init_cookie_req(struct request_sock *req,
245 				  const struct sock *sk_listener,
246 				  struct sk_buff *skb)
247 {
248 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
249 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
250 	struct mptcp_options_received mp_opt;
251 	bool opt_mp_capable, opt_mp_join;
252 	int err;
253 
254 	subflow_init_req(req, sk_listener);
255 	mptcp_get_options(skb, &mp_opt);
256 
257 	opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC);
258 	opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
259 	if (opt_mp_capable && opt_mp_join)
260 		return -EINVAL;
261 
262 	if (opt_mp_capable && listener->request_mptcp) {
263 		if (mp_opt.sndr_key == 0)
264 			return -EINVAL;
265 
266 		subflow_req->local_key = mp_opt.rcvr_key;
267 		err = mptcp_token_new_request(req);
268 		if (err)
269 			return err;
270 
271 		subflow_req->mp_capable = 1;
272 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
273 	} else if (opt_mp_join && listener->request_mptcp) {
274 		if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
275 			return -EINVAL;
276 
277 		subflow_req->mp_join = 1;
278 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
279 	}
280 
281 	return 0;
282 }
283 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
284 
285 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
286 					      struct sk_buff *skb,
287 					      struct flowi *fl,
288 					      struct request_sock *req)
289 {
290 	struct dst_entry *dst;
291 	int err;
292 
293 	tcp_rsk(req)->is_mptcp = 1;
294 	subflow_init_req(req, sk);
295 
296 	dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
297 	if (!dst)
298 		return NULL;
299 
300 	err = subflow_check_req(req, sk, skb);
301 	if (err == 0)
302 		return dst;
303 
304 	dst_release(dst);
305 	if (!req->syncookie)
306 		tcp_request_sock_ops.send_reset(sk, skb);
307 	return NULL;
308 }
309 
310 static void subflow_prep_synack(const struct sock *sk, struct request_sock *req,
311 				struct tcp_fastopen_cookie *foc,
312 				enum tcp_synack_type synack_type)
313 {
314 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
315 	struct inet_request_sock *ireq = inet_rsk(req);
316 
317 	/* clear tstamp_ok, as needed depending on cookie */
318 	if (foc && foc->len > -1)
319 		ireq->tstamp_ok = 0;
320 
321 	if (synack_type == TCP_SYNACK_FASTOPEN)
322 		mptcp_fastopen_subflow_synack_set_params(subflow, req);
323 }
324 
325 static int subflow_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
326 				  struct flowi *fl,
327 				  struct request_sock *req,
328 				  struct tcp_fastopen_cookie *foc,
329 				  enum tcp_synack_type synack_type,
330 				  struct sk_buff *syn_skb)
331 {
332 	subflow_prep_synack(sk, req, foc, synack_type);
333 
334 	return tcp_request_sock_ipv4_ops.send_synack(sk, dst, fl, req, foc,
335 						     synack_type, syn_skb);
336 }
337 
338 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
339 static int subflow_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
340 				  struct flowi *fl,
341 				  struct request_sock *req,
342 				  struct tcp_fastopen_cookie *foc,
343 				  enum tcp_synack_type synack_type,
344 				  struct sk_buff *syn_skb)
345 {
346 	subflow_prep_synack(sk, req, foc, synack_type);
347 
348 	return tcp_request_sock_ipv6_ops.send_synack(sk, dst, fl, req, foc,
349 						     synack_type, syn_skb);
350 }
351 
352 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
353 					      struct sk_buff *skb,
354 					      struct flowi *fl,
355 					      struct request_sock *req)
356 {
357 	struct dst_entry *dst;
358 	int err;
359 
360 	tcp_rsk(req)->is_mptcp = 1;
361 	subflow_init_req(req, sk);
362 
363 	dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
364 	if (!dst)
365 		return NULL;
366 
367 	err = subflow_check_req(req, sk, skb);
368 	if (err == 0)
369 		return dst;
370 
371 	dst_release(dst);
372 	if (!req->syncookie)
373 		tcp6_request_sock_ops.send_reset(sk, skb);
374 	return NULL;
375 }
376 #endif
377 
378 /* validate received truncated hmac and create hmac for third ACK */
379 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
380 {
381 	u8 hmac[SHA256_DIGEST_SIZE];
382 	u64 thmac;
383 
384 	subflow_generate_hmac(subflow->remote_key, subflow->local_key,
385 			      subflow->remote_nonce, subflow->local_nonce,
386 			      hmac);
387 
388 	thmac = get_unaligned_be64(hmac);
389 	pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
390 		 subflow, subflow->token, thmac, subflow->thmac);
391 
392 	return thmac == subflow->thmac;
393 }
394 
395 void mptcp_subflow_reset(struct sock *ssk)
396 {
397 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
398 	struct sock *sk = subflow->conn;
399 
400 	/* must hold: tcp_done() could drop last reference on parent */
401 	sock_hold(sk);
402 
403 	tcp_set_state(ssk, TCP_CLOSE);
404 	tcp_send_active_reset(ssk, GFP_ATOMIC);
405 	tcp_done(ssk);
406 	if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
407 	    schedule_work(&mptcp_sk(sk)->work))
408 		return; /* worker will put sk for us */
409 
410 	sock_put(sk);
411 }
412 
413 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
414 {
415 	return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
416 }
417 
418 void __mptcp_set_connected(struct sock *sk)
419 {
420 	if (sk->sk_state == TCP_SYN_SENT) {
421 		inet_sk_state_store(sk, TCP_ESTABLISHED);
422 		sk->sk_state_change(sk);
423 	}
424 }
425 
426 static void mptcp_set_connected(struct sock *sk)
427 {
428 	mptcp_data_lock(sk);
429 	if (!sock_owned_by_user(sk))
430 		__mptcp_set_connected(sk);
431 	else
432 		__set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags);
433 	mptcp_data_unlock(sk);
434 }
435 
436 static void subflow_set_remote_key(struct mptcp_sock *msk,
437 				   struct mptcp_subflow_context *subflow,
438 				   const struct mptcp_options_received *mp_opt)
439 {
440 	/* active MPC subflow will reach here multiple times:
441 	 * at subflow_finish_connect() time and at 4th ack time
442 	 */
443 	if (subflow->remote_key_valid)
444 		return;
445 
446 	subflow->remote_key_valid = 1;
447 	subflow->remote_key = mp_opt->sndr_key;
448 	mptcp_crypto_key_sha(subflow->remote_key, NULL, &subflow->iasn);
449 	subflow->iasn++;
450 
451 	WRITE_ONCE(msk->remote_key, subflow->remote_key);
452 	WRITE_ONCE(msk->ack_seq, subflow->iasn);
453 	WRITE_ONCE(msk->can_ack, true);
454 	atomic64_set(&msk->rcv_wnd_sent, subflow->iasn);
455 }
456 
457 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
458 {
459 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
460 	struct mptcp_options_received mp_opt;
461 	struct sock *parent = subflow->conn;
462 	struct mptcp_sock *msk;
463 
464 	subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
465 
466 	/* be sure no special action on any packet other than syn-ack */
467 	if (subflow->conn_finished)
468 		return;
469 
470 	msk = mptcp_sk(parent);
471 	mptcp_propagate_sndbuf(parent, sk);
472 	subflow->rel_write_seq = 1;
473 	subflow->conn_finished = 1;
474 	subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
475 	pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
476 
477 	mptcp_get_options(skb, &mp_opt);
478 	if (subflow->request_mptcp) {
479 		if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) {
480 			MPTCP_INC_STATS(sock_net(sk),
481 					MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
482 			mptcp_do_fallback(sk);
483 			pr_fallback(msk);
484 			goto fallback;
485 		}
486 
487 		if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
488 			WRITE_ONCE(msk->csum_enabled, true);
489 		if (mp_opt.deny_join_id0)
490 			WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
491 		subflow->mp_capable = 1;
492 		subflow_set_remote_key(msk, subflow, &mp_opt);
493 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
494 		mptcp_finish_connect(sk);
495 		mptcp_set_connected(parent);
496 	} else if (subflow->request_join) {
497 		u8 hmac[SHA256_DIGEST_SIZE];
498 
499 		if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ)) {
500 			subflow->reset_reason = MPTCP_RST_EMPTCP;
501 			goto do_reset;
502 		}
503 
504 		subflow->backup = mp_opt.backup;
505 		subflow->thmac = mp_opt.thmac;
506 		subflow->remote_nonce = mp_opt.nonce;
507 		subflow->remote_id = mp_opt.join_id;
508 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d",
509 			 subflow, subflow->thmac, subflow->remote_nonce,
510 			 subflow->backup);
511 
512 		if (!subflow_thmac_valid(subflow)) {
513 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
514 			subflow->reset_reason = MPTCP_RST_EMPTCP;
515 			goto do_reset;
516 		}
517 
518 		if (!mptcp_finish_join(sk))
519 			goto do_reset;
520 
521 		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
522 				      subflow->local_nonce,
523 				      subflow->remote_nonce,
524 				      hmac);
525 		memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
526 
527 		subflow->mp_join = 1;
528 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
529 
530 		if (subflow_use_different_dport(msk, sk)) {
531 			pr_debug("synack inet_dport=%d %d",
532 				 ntohs(inet_sk(sk)->inet_dport),
533 				 ntohs(inet_sk(parent)->inet_dport));
534 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
535 		}
536 	} else if (mptcp_check_fallback(sk)) {
537 fallback:
538 		mptcp_rcv_space_init(msk, sk);
539 		mptcp_set_connected(parent);
540 	}
541 	return;
542 
543 do_reset:
544 	subflow->reset_transient = 0;
545 	mptcp_subflow_reset(sk);
546 }
547 
548 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
549 {
550 	subflow->local_id = local_id;
551 	subflow->local_id_valid = 1;
552 }
553 
554 static int subflow_chk_local_id(struct sock *sk)
555 {
556 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
557 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
558 	int err;
559 
560 	if (likely(subflow->local_id_valid))
561 		return 0;
562 
563 	err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
564 	if (err < 0)
565 		return err;
566 
567 	subflow_set_local_id(subflow, err);
568 	return 0;
569 }
570 
571 static int subflow_rebuild_header(struct sock *sk)
572 {
573 	int err = subflow_chk_local_id(sk);
574 
575 	if (unlikely(err < 0))
576 		return err;
577 
578 	return inet_sk_rebuild_header(sk);
579 }
580 
581 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
582 static int subflow_v6_rebuild_header(struct sock *sk)
583 {
584 	int err = subflow_chk_local_id(sk);
585 
586 	if (unlikely(err < 0))
587 		return err;
588 
589 	return inet6_sk_rebuild_header(sk);
590 }
591 #endif
592 
593 struct request_sock_ops mptcp_subflow_request_sock_ops;
594 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
595 
596 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
597 {
598 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
599 
600 	pr_debug("subflow=%p", subflow);
601 
602 	/* Never answer to SYNs sent to broadcast or multicast */
603 	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
604 		goto drop;
605 
606 	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
607 				&subflow_request_sock_ipv4_ops,
608 				sk, skb);
609 drop:
610 	tcp_listendrop(sk);
611 	return 0;
612 }
613 
614 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
615 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
616 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
617 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
618 static struct proto tcpv6_prot_override;
619 
620 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
621 {
622 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
623 
624 	pr_debug("subflow=%p", subflow);
625 
626 	if (skb->protocol == htons(ETH_P_IP))
627 		return subflow_v4_conn_request(sk, skb);
628 
629 	if (!ipv6_unicast_destination(skb))
630 		goto drop;
631 
632 	if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
633 		__IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
634 		return 0;
635 	}
636 
637 	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
638 				&subflow_request_sock_ipv6_ops, sk, skb);
639 
640 drop:
641 	tcp_listendrop(sk);
642 	return 0; /* don't send reset */
643 }
644 #endif
645 
646 /* validate hmac received in third ACK */
647 static bool subflow_hmac_valid(const struct request_sock *req,
648 			       const struct mptcp_options_received *mp_opt)
649 {
650 	const struct mptcp_subflow_request_sock *subflow_req;
651 	u8 hmac[SHA256_DIGEST_SIZE];
652 	struct mptcp_sock *msk;
653 
654 	subflow_req = mptcp_subflow_rsk(req);
655 	msk = subflow_req->msk;
656 	if (!msk)
657 		return false;
658 
659 	subflow_generate_hmac(msk->remote_key, msk->local_key,
660 			      subflow_req->remote_nonce,
661 			      subflow_req->local_nonce, hmac);
662 
663 	return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
664 }
665 
666 static void mptcp_force_close(struct sock *sk)
667 {
668 	/* the msk is not yet exposed to user-space */
669 	inet_sk_state_store(sk, TCP_CLOSE);
670 	sk_common_release(sk);
671 }
672 
673 static void subflow_ulp_fallback(struct sock *sk,
674 				 struct mptcp_subflow_context *old_ctx)
675 {
676 	struct inet_connection_sock *icsk = inet_csk(sk);
677 
678 	mptcp_subflow_tcp_fallback(sk, old_ctx);
679 	icsk->icsk_ulp_ops = NULL;
680 	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
681 	tcp_sk(sk)->is_mptcp = 0;
682 
683 	mptcp_subflow_ops_undo_override(sk);
684 }
685 
686 static void subflow_drop_ctx(struct sock *ssk)
687 {
688 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
689 
690 	if (!ctx)
691 		return;
692 
693 	subflow_ulp_fallback(ssk, ctx);
694 	if (ctx->conn)
695 		sock_put(ctx->conn);
696 
697 	kfree_rcu(ctx, rcu);
698 }
699 
700 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
701 				     const struct mptcp_options_received *mp_opt)
702 {
703 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
704 
705 	subflow_set_remote_key(msk, subflow, mp_opt);
706 	subflow->fully_established = 1;
707 	WRITE_ONCE(msk->fully_established, true);
708 
709 	if (subflow->is_mptfo)
710 		mptcp_fastopen_gen_msk_ackseq(msk, subflow, mp_opt);
711 }
712 
713 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
714 					  struct sk_buff *skb,
715 					  struct request_sock *req,
716 					  struct dst_entry *dst,
717 					  struct request_sock *req_unhash,
718 					  bool *own_req)
719 {
720 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
721 	struct mptcp_subflow_request_sock *subflow_req;
722 	struct mptcp_options_received mp_opt;
723 	bool fallback, fallback_is_fatal;
724 	struct sock *new_msk = NULL;
725 	struct sock *child;
726 
727 	pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
728 
729 	/* After child creation we must look for MPC even when options
730 	 * are not parsed
731 	 */
732 	mp_opt.suboptions = 0;
733 
734 	/* hopefully temporary handling for MP_JOIN+syncookie */
735 	subflow_req = mptcp_subflow_rsk(req);
736 	fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
737 	fallback = !tcp_rsk(req)->is_mptcp;
738 	if (fallback)
739 		goto create_child;
740 
741 	/* if the sk is MP_CAPABLE, we try to fetch the client key */
742 	if (subflow_req->mp_capable) {
743 		/* we can receive and accept an in-window, out-of-order pkt,
744 		 * which may not carry the MP_CAPABLE opt even on mptcp enabled
745 		 * paths: always try to extract the peer key, and fallback
746 		 * for packets missing it.
747 		 * Even OoO DSS packets coming legitly after dropped or
748 		 * reordered MPC will cause fallback, but we don't have other
749 		 * options.
750 		 */
751 		mptcp_get_options(skb, &mp_opt);
752 		if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) {
753 			fallback = true;
754 			goto create_child;
755 		}
756 
757 		new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
758 		if (!new_msk)
759 			fallback = true;
760 	} else if (subflow_req->mp_join) {
761 		mptcp_get_options(skb, &mp_opt);
762 		if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ) ||
763 		    !subflow_hmac_valid(req, &mp_opt) ||
764 		    !mptcp_can_accept_new_subflow(subflow_req->msk)) {
765 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
766 			fallback = true;
767 		}
768 	}
769 
770 create_child:
771 	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
772 						     req_unhash, own_req);
773 
774 	if (child && *own_req) {
775 		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
776 
777 		tcp_rsk(req)->drop_req = false;
778 
779 		/* we need to fallback on ctx allocation failure and on pre-reqs
780 		 * checking above. In the latter scenario we additionally need
781 		 * to reset the context to non MPTCP status.
782 		 */
783 		if (!ctx || fallback) {
784 			if (fallback_is_fatal) {
785 				subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
786 				goto dispose_child;
787 			}
788 
789 			if (new_msk)
790 				mptcp_copy_inaddrs(new_msk, child);
791 			subflow_drop_ctx(child);
792 			goto out;
793 		}
794 
795 		/* ssk inherits options of listener sk */
796 		ctx->setsockopt_seq = listener->setsockopt_seq;
797 
798 		if (ctx->mp_capable) {
799 			/* this can't race with mptcp_close(), as the msk is
800 			 * not yet exposted to user-space
801 			 */
802 			inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
803 
804 			/* record the newly created socket as the first msk
805 			 * subflow, but don't link it yet into conn_list
806 			 */
807 			WRITE_ONCE(mptcp_sk(new_msk)->first, child);
808 
809 			/* new mpc subflow takes ownership of the newly
810 			 * created mptcp socket
811 			 */
812 			mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
813 			mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
814 			mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
815 			ctx->conn = new_msk;
816 			new_msk = NULL;
817 
818 			/* set msk addresses early to ensure mptcp_pm_get_local_id()
819 			 * uses the correct data
820 			 */
821 			mptcp_copy_inaddrs(ctx->conn, child);
822 
823 			/* with OoO packets we can reach here without ingress
824 			 * mpc option
825 			 */
826 			if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK)
827 				mptcp_subflow_fully_established(ctx, &mp_opt);
828 		} else if (ctx->mp_join) {
829 			struct mptcp_sock *owner;
830 
831 			owner = subflow_req->msk;
832 			if (!owner) {
833 				subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
834 				goto dispose_child;
835 			}
836 
837 			/* move the msk reference ownership to the subflow */
838 			subflow_req->msk = NULL;
839 			ctx->conn = (struct sock *)owner;
840 
841 			if (subflow_use_different_sport(owner, sk)) {
842 				pr_debug("ack inet_sport=%d %d",
843 					 ntohs(inet_sk(sk)->inet_sport),
844 					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
845 				if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
846 					SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
847 					goto dispose_child;
848 				}
849 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
850 			}
851 
852 			if (!mptcp_finish_join(child))
853 				goto dispose_child;
854 
855 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
856 			tcp_rsk(req)->drop_req = true;
857 		}
858 	}
859 
860 out:
861 	/* dispose of the left over mptcp master, if any */
862 	if (unlikely(new_msk))
863 		mptcp_force_close(new_msk);
864 
865 	/* check for expected invariant - should never trigger, just help
866 	 * catching eariler subtle bugs
867 	 */
868 	WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
869 		     (!mptcp_subflow_ctx(child) ||
870 		      !mptcp_subflow_ctx(child)->conn));
871 	return child;
872 
873 dispose_child:
874 	subflow_drop_ctx(child);
875 	tcp_rsk(req)->drop_req = true;
876 	inet_csk_prepare_for_destroy_sock(child);
877 	tcp_done(child);
878 	req->rsk_ops->send_reset(sk, skb);
879 
880 	/* The last child reference will be released by the caller */
881 	return child;
882 }
883 
884 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
885 static struct proto tcp_prot_override;
886 
887 enum mapping_status {
888 	MAPPING_OK,
889 	MAPPING_INVALID,
890 	MAPPING_EMPTY,
891 	MAPPING_DATA_FIN,
892 	MAPPING_DUMMY,
893 	MAPPING_BAD_CSUM
894 };
895 
896 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
897 {
898 	pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
899 		 ssn, subflow->map_subflow_seq, subflow->map_data_len);
900 }
901 
902 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
903 {
904 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
905 	unsigned int skb_consumed;
906 
907 	skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
908 	if (WARN_ON_ONCE(skb_consumed >= skb->len))
909 		return true;
910 
911 	return skb->len - skb_consumed <= subflow->map_data_len -
912 					  mptcp_subflow_get_map_offset(subflow);
913 }
914 
915 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
916 {
917 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
918 	u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
919 
920 	if (unlikely(before(ssn, subflow->map_subflow_seq))) {
921 		/* Mapping covers data later in the subflow stream,
922 		 * currently unsupported.
923 		 */
924 		dbg_bad_map(subflow, ssn);
925 		return false;
926 	}
927 	if (unlikely(!before(ssn, subflow->map_subflow_seq +
928 				  subflow->map_data_len))) {
929 		/* Mapping does covers past subflow data, invalid */
930 		dbg_bad_map(subflow, ssn);
931 		return false;
932 	}
933 	return true;
934 }
935 
936 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
937 					      bool csum_reqd)
938 {
939 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
940 	u32 offset, seq, delta;
941 	__sum16 csum;
942 	int len;
943 
944 	if (!csum_reqd)
945 		return MAPPING_OK;
946 
947 	/* mapping already validated on previous traversal */
948 	if (subflow->map_csum_len == subflow->map_data_len)
949 		return MAPPING_OK;
950 
951 	/* traverse the receive queue, ensuring it contains a full
952 	 * DSS mapping and accumulating the related csum.
953 	 * Preserve the accoumlate csum across multiple calls, to compute
954 	 * the csum only once
955 	 */
956 	delta = subflow->map_data_len - subflow->map_csum_len;
957 	for (;;) {
958 		seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
959 		offset = seq - TCP_SKB_CB(skb)->seq;
960 
961 		/* if the current skb has not been accounted yet, csum its contents
962 		 * up to the amount covered by the current DSS
963 		 */
964 		if (offset < skb->len) {
965 			__wsum csum;
966 
967 			len = min(skb->len - offset, delta);
968 			csum = skb_checksum(skb, offset, len, 0);
969 			subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
970 								subflow->map_csum_len);
971 
972 			delta -= len;
973 			subflow->map_csum_len += len;
974 		}
975 		if (delta == 0)
976 			break;
977 
978 		if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
979 			/* if this subflow is closed, the partial mapping
980 			 * will be never completed; flush the pending skbs, so
981 			 * that subflow_sched_work_if_closed() can kick in
982 			 */
983 			if (unlikely(ssk->sk_state == TCP_CLOSE))
984 				while ((skb = skb_peek(&ssk->sk_receive_queue)))
985 					sk_eat_skb(ssk, skb);
986 
987 			/* not enough data to validate the csum */
988 			return MAPPING_EMPTY;
989 		}
990 
991 		/* the DSS mapping for next skbs will be validated later,
992 		 * when a get_mapping_status call will process such skb
993 		 */
994 		skb = skb->next;
995 	}
996 
997 	/* note that 'map_data_len' accounts only for the carried data, does
998 	 * not include the eventual seq increment due to the data fin,
999 	 * while the pseudo header requires the original DSS data len,
1000 	 * including that
1001 	 */
1002 	csum = __mptcp_make_csum(subflow->map_seq,
1003 				 subflow->map_subflow_seq,
1004 				 subflow->map_data_len + subflow->map_data_fin,
1005 				 subflow->map_data_csum);
1006 	if (unlikely(csum)) {
1007 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
1008 		return MAPPING_BAD_CSUM;
1009 	}
1010 
1011 	subflow->valid_csum_seen = 1;
1012 	return MAPPING_OK;
1013 }
1014 
1015 static enum mapping_status get_mapping_status(struct sock *ssk,
1016 					      struct mptcp_sock *msk)
1017 {
1018 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1019 	bool csum_reqd = READ_ONCE(msk->csum_enabled);
1020 	struct mptcp_ext *mpext;
1021 	struct sk_buff *skb;
1022 	u16 data_len;
1023 	u64 map_seq;
1024 
1025 	skb = skb_peek(&ssk->sk_receive_queue);
1026 	if (!skb)
1027 		return MAPPING_EMPTY;
1028 
1029 	if (mptcp_check_fallback(ssk))
1030 		return MAPPING_DUMMY;
1031 
1032 	mpext = mptcp_get_ext(skb);
1033 	if (!mpext || !mpext->use_map) {
1034 		if (!subflow->map_valid && !skb->len) {
1035 			/* the TCP stack deliver 0 len FIN pkt to the receive
1036 			 * queue, that is the only 0len pkts ever expected here,
1037 			 * and we can admit no mapping only for 0 len pkts
1038 			 */
1039 			if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
1040 				WARN_ONCE(1, "0len seq %d:%d flags %x",
1041 					  TCP_SKB_CB(skb)->seq,
1042 					  TCP_SKB_CB(skb)->end_seq,
1043 					  TCP_SKB_CB(skb)->tcp_flags);
1044 			sk_eat_skb(ssk, skb);
1045 			return MAPPING_EMPTY;
1046 		}
1047 
1048 		if (!subflow->map_valid)
1049 			return MAPPING_INVALID;
1050 
1051 		goto validate_seq;
1052 	}
1053 
1054 	trace_get_mapping_status(mpext);
1055 
1056 	data_len = mpext->data_len;
1057 	if (data_len == 0) {
1058 		pr_debug("infinite mapping received");
1059 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
1060 		subflow->map_data_len = 0;
1061 		return MAPPING_INVALID;
1062 	}
1063 
1064 	if (mpext->data_fin == 1) {
1065 		if (data_len == 1) {
1066 			bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1067 								 mpext->dsn64);
1068 			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
1069 			if (subflow->map_valid) {
1070 				/* A DATA_FIN might arrive in a DSS
1071 				 * option before the previous mapping
1072 				 * has been fully consumed. Continue
1073 				 * handling the existing mapping.
1074 				 */
1075 				skb_ext_del(skb, SKB_EXT_MPTCP);
1076 				return MAPPING_OK;
1077 			} else {
1078 				if (updated && schedule_work(&msk->work))
1079 					sock_hold((struct sock *)msk);
1080 
1081 				return MAPPING_DATA_FIN;
1082 			}
1083 		} else {
1084 			u64 data_fin_seq = mpext->data_seq + data_len - 1;
1085 
1086 			/* If mpext->data_seq is a 32-bit value, data_fin_seq
1087 			 * must also be limited to 32 bits.
1088 			 */
1089 			if (!mpext->dsn64)
1090 				data_fin_seq &= GENMASK_ULL(31, 0);
1091 
1092 			mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1093 			pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
1094 				 data_fin_seq, mpext->dsn64);
1095 		}
1096 
1097 		/* Adjust for DATA_FIN using 1 byte of sequence space */
1098 		data_len--;
1099 	}
1100 
1101 	map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1102 	WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1103 
1104 	if (subflow->map_valid) {
1105 		/* Allow replacing only with an identical map */
1106 		if (subflow->map_seq == map_seq &&
1107 		    subflow->map_subflow_seq == mpext->subflow_seq &&
1108 		    subflow->map_data_len == data_len &&
1109 		    subflow->map_csum_reqd == mpext->csum_reqd) {
1110 			skb_ext_del(skb, SKB_EXT_MPTCP);
1111 			goto validate_csum;
1112 		}
1113 
1114 		/* If this skb data are fully covered by the current mapping,
1115 		 * the new map would need caching, which is not supported
1116 		 */
1117 		if (skb_is_fully_mapped(ssk, skb)) {
1118 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1119 			return MAPPING_INVALID;
1120 		}
1121 
1122 		/* will validate the next map after consuming the current one */
1123 		goto validate_csum;
1124 	}
1125 
1126 	subflow->map_seq = map_seq;
1127 	subflow->map_subflow_seq = mpext->subflow_seq;
1128 	subflow->map_data_len = data_len;
1129 	subflow->map_valid = 1;
1130 	subflow->map_data_fin = mpext->data_fin;
1131 	subflow->mpc_map = mpext->mpc_map;
1132 	subflow->map_csum_reqd = mpext->csum_reqd;
1133 	subflow->map_csum_len = 0;
1134 	subflow->map_data_csum = csum_unfold(mpext->csum);
1135 
1136 	/* Cfr RFC 8684 Section 3.3.0 */
1137 	if (unlikely(subflow->map_csum_reqd != csum_reqd))
1138 		return MAPPING_INVALID;
1139 
1140 	pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1141 		 subflow->map_seq, subflow->map_subflow_seq,
1142 		 subflow->map_data_len, subflow->map_csum_reqd,
1143 		 subflow->map_data_csum);
1144 
1145 validate_seq:
1146 	/* we revalidate valid mapping on new skb, because we must ensure
1147 	 * the current skb is completely covered by the available mapping
1148 	 */
1149 	if (!validate_mapping(ssk, skb)) {
1150 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1151 		return MAPPING_INVALID;
1152 	}
1153 
1154 	skb_ext_del(skb, SKB_EXT_MPTCP);
1155 
1156 validate_csum:
1157 	return validate_data_csum(ssk, skb, csum_reqd);
1158 }
1159 
1160 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1161 				       u64 limit)
1162 {
1163 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1164 	bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1165 	u32 incr;
1166 
1167 	incr = limit >= skb->len ? skb->len + fin : limit;
1168 
1169 	pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1170 		 subflow->map_subflow_seq);
1171 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1172 	tcp_sk(ssk)->copied_seq += incr;
1173 	if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1174 		sk_eat_skb(ssk, skb);
1175 	if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1176 		subflow->map_valid = 0;
1177 }
1178 
1179 /* sched mptcp worker to remove the subflow if no more data is pending */
1180 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1181 {
1182 	struct sock *sk = (struct sock *)msk;
1183 
1184 	if (likely(ssk->sk_state != TCP_CLOSE))
1185 		return;
1186 
1187 	if (skb_queue_empty(&ssk->sk_receive_queue) &&
1188 	    !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
1189 		sock_hold(sk);
1190 		if (!schedule_work(&msk->work))
1191 			sock_put(sk);
1192 	}
1193 }
1194 
1195 static bool subflow_can_fallback(struct mptcp_subflow_context *subflow)
1196 {
1197 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1198 
1199 	if (subflow->mp_join)
1200 		return false;
1201 	else if (READ_ONCE(msk->csum_enabled))
1202 		return !subflow->valid_csum_seen;
1203 	else
1204 		return !subflow->fully_established;
1205 }
1206 
1207 static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1208 {
1209 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1210 	unsigned long fail_tout;
1211 
1212 	/* greceful failure can happen only on the MPC subflow */
1213 	if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1214 		return;
1215 
1216 	/* since the close timeout take precedence on the fail one,
1217 	 * no need to start the latter when the first is already set
1218 	 */
1219 	if (sock_flag((struct sock *)msk, SOCK_DEAD))
1220 		return;
1221 
1222 	/* we don't need extreme accuracy here, use a zero fail_tout as special
1223 	 * value meaning no fail timeout at all;
1224 	 */
1225 	fail_tout = jiffies + TCP_RTO_MAX;
1226 	if (!fail_tout)
1227 		fail_tout = 1;
1228 	WRITE_ONCE(subflow->fail_tout, fail_tout);
1229 	tcp_send_ack(ssk);
1230 
1231 	mptcp_reset_timeout(msk, subflow->fail_tout);
1232 }
1233 
1234 static bool subflow_check_data_avail(struct sock *ssk)
1235 {
1236 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1237 	enum mapping_status status;
1238 	struct mptcp_sock *msk;
1239 	struct sk_buff *skb;
1240 
1241 	if (!skb_peek(&ssk->sk_receive_queue))
1242 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1243 	if (subflow->data_avail)
1244 		return true;
1245 
1246 	msk = mptcp_sk(subflow->conn);
1247 	for (;;) {
1248 		u64 ack_seq;
1249 		u64 old_ack;
1250 
1251 		status = get_mapping_status(ssk, msk);
1252 		trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1253 		if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1254 			     status == MAPPING_BAD_CSUM))
1255 			goto fallback;
1256 
1257 		if (status != MAPPING_OK)
1258 			goto no_data;
1259 
1260 		skb = skb_peek(&ssk->sk_receive_queue);
1261 		if (WARN_ON_ONCE(!skb))
1262 			goto no_data;
1263 
1264 		if (unlikely(!READ_ONCE(msk->can_ack)))
1265 			goto fallback;
1266 
1267 		old_ack = READ_ONCE(msk->ack_seq);
1268 		ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1269 		pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1270 			 ack_seq);
1271 		if (unlikely(before64(ack_seq, old_ack))) {
1272 			mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1273 			continue;
1274 		}
1275 
1276 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1277 		break;
1278 	}
1279 	return true;
1280 
1281 no_data:
1282 	subflow_sched_work_if_closed(msk, ssk);
1283 	return false;
1284 
1285 fallback:
1286 	if (!__mptcp_check_fallback(msk)) {
1287 		/* RFC 8684 section 3.7. */
1288 		if (status == MAPPING_BAD_CSUM &&
1289 		    (subflow->mp_join || subflow->valid_csum_seen)) {
1290 			subflow->send_mp_fail = 1;
1291 
1292 			if (!READ_ONCE(msk->allow_infinite_fallback)) {
1293 				subflow->reset_transient = 0;
1294 				subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1295 				goto reset;
1296 			}
1297 			mptcp_subflow_fail(msk, ssk);
1298 			WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1299 			return true;
1300 		}
1301 
1302 		if (!subflow_can_fallback(subflow) && subflow->map_data_len) {
1303 			/* fatal protocol error, close the socket.
1304 			 * subflow_error_report() will introduce the appropriate barriers
1305 			 */
1306 			subflow->reset_transient = 0;
1307 			subflow->reset_reason = MPTCP_RST_EMPTCP;
1308 
1309 reset:
1310 			ssk->sk_err = EBADMSG;
1311 			tcp_set_state(ssk, TCP_CLOSE);
1312 			while ((skb = skb_peek(&ssk->sk_receive_queue)))
1313 				sk_eat_skb(ssk, skb);
1314 			tcp_send_active_reset(ssk, GFP_ATOMIC);
1315 			WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1316 			return false;
1317 		}
1318 
1319 		mptcp_do_fallback(ssk);
1320 	}
1321 
1322 	skb = skb_peek(&ssk->sk_receive_queue);
1323 	subflow->map_valid = 1;
1324 	subflow->map_seq = READ_ONCE(msk->ack_seq);
1325 	subflow->map_data_len = skb->len;
1326 	subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1327 	WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1328 	return true;
1329 }
1330 
1331 bool mptcp_subflow_data_available(struct sock *sk)
1332 {
1333 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1334 
1335 	/* check if current mapping is still valid */
1336 	if (subflow->map_valid &&
1337 	    mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1338 		subflow->map_valid = 0;
1339 		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1340 
1341 		pr_debug("Done with mapping: seq=%u data_len=%u",
1342 			 subflow->map_subflow_seq,
1343 			 subflow->map_data_len);
1344 	}
1345 
1346 	return subflow_check_data_avail(sk);
1347 }
1348 
1349 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1350  * not the ssk one.
1351  *
1352  * In mptcp, rwin is about the mptcp-level connection data.
1353  *
1354  * Data that is still on the ssk rx queue can thus be ignored,
1355  * as far as mptcp peer is concerned that data is still inflight.
1356  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1357  */
1358 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1359 {
1360 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1361 	const struct sock *sk = subflow->conn;
1362 
1363 	*space = __mptcp_space(sk);
1364 	*full_space = tcp_full_space(sk);
1365 }
1366 
1367 void __mptcp_error_report(struct sock *sk)
1368 {
1369 	struct mptcp_subflow_context *subflow;
1370 	struct mptcp_sock *msk = mptcp_sk(sk);
1371 
1372 	mptcp_for_each_subflow(msk, subflow) {
1373 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1374 		int err = sock_error(ssk);
1375 
1376 		if (!err)
1377 			continue;
1378 
1379 		/* only propagate errors on fallen-back sockets or
1380 		 * on MPC connect
1381 		 */
1382 		if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1383 			continue;
1384 
1385 		inet_sk_state_store(sk, inet_sk_state_load(ssk));
1386 		sk->sk_err = -err;
1387 
1388 		/* This barrier is coupled with smp_rmb() in mptcp_poll() */
1389 		smp_wmb();
1390 		sk_error_report(sk);
1391 		break;
1392 	}
1393 }
1394 
1395 static void subflow_error_report(struct sock *ssk)
1396 {
1397 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1398 
1399 	mptcp_data_lock(sk);
1400 	if (!sock_owned_by_user(sk))
1401 		__mptcp_error_report(sk);
1402 	else
1403 		__set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
1404 	mptcp_data_unlock(sk);
1405 }
1406 
1407 static void subflow_data_ready(struct sock *sk)
1408 {
1409 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1410 	u16 state = 1 << inet_sk_state_load(sk);
1411 	struct sock *parent = subflow->conn;
1412 	struct mptcp_sock *msk;
1413 
1414 	msk = mptcp_sk(parent);
1415 	if (state & TCPF_LISTEN) {
1416 		/* MPJ subflow are removed from accept queue before reaching here,
1417 		 * avoid stray wakeups
1418 		 */
1419 		if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1420 			return;
1421 
1422 		parent->sk_data_ready(parent);
1423 		return;
1424 	}
1425 
1426 	WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1427 		     !subflow->mp_join && !(state & TCPF_CLOSE));
1428 
1429 	if (mptcp_subflow_data_available(sk))
1430 		mptcp_data_ready(parent, sk);
1431 	else if (unlikely(sk->sk_err))
1432 		subflow_error_report(sk);
1433 }
1434 
1435 static void subflow_write_space(struct sock *ssk)
1436 {
1437 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1438 
1439 	mptcp_propagate_sndbuf(sk, ssk);
1440 	mptcp_write_space(sk);
1441 }
1442 
1443 static const struct inet_connection_sock_af_ops *
1444 subflow_default_af_ops(struct sock *sk)
1445 {
1446 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1447 	if (sk->sk_family == AF_INET6)
1448 		return &subflow_v6_specific;
1449 #endif
1450 	return &subflow_specific;
1451 }
1452 
1453 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1454 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1455 {
1456 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1457 	struct inet_connection_sock *icsk = inet_csk(sk);
1458 	const struct inet_connection_sock_af_ops *target;
1459 
1460 	target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1461 
1462 	pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1463 		 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1464 
1465 	if (likely(icsk->icsk_af_ops == target))
1466 		return;
1467 
1468 	subflow->icsk_af_ops = icsk->icsk_af_ops;
1469 	icsk->icsk_af_ops = target;
1470 }
1471 #endif
1472 
1473 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1474 			 struct sockaddr_storage *addr,
1475 			 unsigned short family)
1476 {
1477 	memset(addr, 0, sizeof(*addr));
1478 	addr->ss_family = family;
1479 	if (addr->ss_family == AF_INET) {
1480 		struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1481 
1482 		if (info->family == AF_INET)
1483 			in_addr->sin_addr = info->addr;
1484 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1485 		else if (ipv6_addr_v4mapped(&info->addr6))
1486 			in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1487 #endif
1488 		in_addr->sin_port = info->port;
1489 	}
1490 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1491 	else if (addr->ss_family == AF_INET6) {
1492 		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1493 
1494 		if (info->family == AF_INET)
1495 			ipv6_addr_set_v4mapped(info->addr.s_addr,
1496 					       &in6_addr->sin6_addr);
1497 		else
1498 			in6_addr->sin6_addr = info->addr6;
1499 		in6_addr->sin6_port = info->port;
1500 	}
1501 #endif
1502 }
1503 
1504 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1505 			    const struct mptcp_addr_info *remote)
1506 {
1507 	struct mptcp_sock *msk = mptcp_sk(sk);
1508 	struct mptcp_subflow_context *subflow;
1509 	struct sockaddr_storage addr;
1510 	int remote_id = remote->id;
1511 	int local_id = loc->id;
1512 	int err = -ENOTCONN;
1513 	struct socket *sf;
1514 	struct sock *ssk;
1515 	u32 remote_token;
1516 	int addrlen;
1517 	int ifindex;
1518 	u8 flags;
1519 
1520 	if (!mptcp_is_fully_established(sk))
1521 		goto err_out;
1522 
1523 	err = mptcp_subflow_create_socket(sk, &sf);
1524 	if (err)
1525 		goto err_out;
1526 
1527 	ssk = sf->sk;
1528 	subflow = mptcp_subflow_ctx(ssk);
1529 	do {
1530 		get_random_bytes(&subflow->local_nonce, sizeof(u32));
1531 	} while (!subflow->local_nonce);
1532 
1533 	if (local_id)
1534 		subflow_set_local_id(subflow, local_id);
1535 
1536 	mptcp_pm_get_flags_and_ifindex_by_id(msk, local_id,
1537 					     &flags, &ifindex);
1538 	subflow->remote_key_valid = 1;
1539 	subflow->remote_key = msk->remote_key;
1540 	subflow->local_key = msk->local_key;
1541 	subflow->token = msk->token;
1542 	mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1543 
1544 	addrlen = sizeof(struct sockaddr_in);
1545 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1546 	if (addr.ss_family == AF_INET6)
1547 		addrlen = sizeof(struct sockaddr_in6);
1548 #endif
1549 	mptcp_sockopt_sync(msk, ssk);
1550 
1551 	ssk->sk_bound_dev_if = ifindex;
1552 	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1553 	if (err)
1554 		goto failed;
1555 
1556 	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1557 	pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1558 		 remote_token, local_id, remote_id);
1559 	subflow->remote_token = remote_token;
1560 	subflow->remote_id = remote_id;
1561 	subflow->request_join = 1;
1562 	subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1563 	mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1564 
1565 	sock_hold(ssk);
1566 	list_add_tail(&subflow->node, &msk->conn_list);
1567 	err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1568 	if (err && err != -EINPROGRESS)
1569 		goto failed_unlink;
1570 
1571 	/* discard the subflow socket */
1572 	mptcp_sock_graft(ssk, sk->sk_socket);
1573 	iput(SOCK_INODE(sf));
1574 	WRITE_ONCE(msk->allow_infinite_fallback, false);
1575 	return 0;
1576 
1577 failed_unlink:
1578 	list_del(&subflow->node);
1579 	sock_put(mptcp_subflow_tcp_sock(subflow));
1580 
1581 failed:
1582 	subflow->disposable = 1;
1583 	sock_release(sf);
1584 
1585 err_out:
1586 	/* we account subflows before the creation, and this failures will not
1587 	 * be caught by sk_state_change()
1588 	 */
1589 	mptcp_pm_close_subflow(msk);
1590 	return err;
1591 }
1592 
1593 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1594 {
1595 #ifdef CONFIG_SOCK_CGROUP_DATA
1596 	struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1597 				*child_skcd = &child->sk_cgrp_data;
1598 
1599 	/* only the additional subflows created by kworkers have to be modified */
1600 	if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1601 	    cgroup_id(sock_cgroup_ptr(child_skcd))) {
1602 #ifdef CONFIG_MEMCG
1603 		struct mem_cgroup *memcg = parent->sk_memcg;
1604 
1605 		mem_cgroup_sk_free(child);
1606 		if (memcg && css_tryget(&memcg->css))
1607 			child->sk_memcg = memcg;
1608 #endif /* CONFIG_MEMCG */
1609 
1610 		cgroup_sk_free(child_skcd);
1611 		*child_skcd = *parent_skcd;
1612 		cgroup_sk_clone(child_skcd);
1613 	}
1614 #endif /* CONFIG_SOCK_CGROUP_DATA */
1615 }
1616 
1617 static void mptcp_subflow_ops_override(struct sock *ssk)
1618 {
1619 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1620 	if (ssk->sk_prot == &tcpv6_prot)
1621 		ssk->sk_prot = &tcpv6_prot_override;
1622 	else
1623 #endif
1624 		ssk->sk_prot = &tcp_prot_override;
1625 }
1626 
1627 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1628 {
1629 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1630 	if (ssk->sk_prot == &tcpv6_prot_override)
1631 		ssk->sk_prot = &tcpv6_prot;
1632 	else
1633 #endif
1634 		ssk->sk_prot = &tcp_prot;
1635 }
1636 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1637 {
1638 	struct mptcp_subflow_context *subflow;
1639 	struct net *net = sock_net(sk);
1640 	struct socket *sf;
1641 	int err;
1642 
1643 	/* un-accepted server sockets can reach here - on bad configuration
1644 	 * bail early to avoid greater trouble later
1645 	 */
1646 	if (unlikely(!sk->sk_socket))
1647 		return -EINVAL;
1648 
1649 	err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1650 			       &sf);
1651 	if (err)
1652 		return err;
1653 
1654 	lock_sock(sf->sk);
1655 
1656 	/* the newly created socket has to be in the same cgroup as its parent */
1657 	mptcp_attach_cgroup(sk, sf->sk);
1658 
1659 	/* kernel sockets do not by default acquire net ref, but TCP timer
1660 	 * needs it.
1661 	 * Update ns_tracker to current stack trace and refcounted tracker.
1662 	 */
1663 	__netns_tracker_free(net, &sf->sk->ns_tracker, false);
1664 	sf->sk->sk_net_refcnt = 1;
1665 	get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
1666 	sock_inuse_add(net, 1);
1667 	err = tcp_set_ulp(sf->sk, "mptcp");
1668 	release_sock(sf->sk);
1669 
1670 	if (err) {
1671 		sock_release(sf);
1672 		return err;
1673 	}
1674 
1675 	/* the newly created socket really belongs to the owning MPTCP master
1676 	 * socket, even if for additional subflows the allocation is performed
1677 	 * by a kernel workqueue. Adjust inode references, so that the
1678 	 * procfs/diag interfaces really show this one belonging to the correct
1679 	 * user.
1680 	 */
1681 	SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1682 	SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1683 	SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1684 
1685 	subflow = mptcp_subflow_ctx(sf->sk);
1686 	pr_debug("subflow=%p", subflow);
1687 
1688 	*new_sock = sf;
1689 	sock_hold(sk);
1690 	subflow->conn = sk;
1691 	mptcp_subflow_ops_override(sf->sk);
1692 
1693 	return 0;
1694 }
1695 
1696 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1697 							gfp_t priority)
1698 {
1699 	struct inet_connection_sock *icsk = inet_csk(sk);
1700 	struct mptcp_subflow_context *ctx;
1701 
1702 	ctx = kzalloc(sizeof(*ctx), priority);
1703 	if (!ctx)
1704 		return NULL;
1705 
1706 	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1707 	INIT_LIST_HEAD(&ctx->node);
1708 	INIT_LIST_HEAD(&ctx->delegated_node);
1709 
1710 	pr_debug("subflow=%p", ctx);
1711 
1712 	ctx->tcp_sock = sk;
1713 
1714 	return ctx;
1715 }
1716 
1717 static void __subflow_state_change(struct sock *sk)
1718 {
1719 	struct socket_wq *wq;
1720 
1721 	rcu_read_lock();
1722 	wq = rcu_dereference(sk->sk_wq);
1723 	if (skwq_has_sleeper(wq))
1724 		wake_up_interruptible_all(&wq->wait);
1725 	rcu_read_unlock();
1726 }
1727 
1728 static bool subflow_is_done(const struct sock *sk)
1729 {
1730 	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1731 }
1732 
1733 static void subflow_state_change(struct sock *sk)
1734 {
1735 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1736 	struct sock *parent = subflow->conn;
1737 
1738 	__subflow_state_change(sk);
1739 
1740 	if (subflow_simultaneous_connect(sk)) {
1741 		mptcp_propagate_sndbuf(parent, sk);
1742 		mptcp_do_fallback(sk);
1743 		mptcp_rcv_space_init(mptcp_sk(parent), sk);
1744 		pr_fallback(mptcp_sk(parent));
1745 		subflow->conn_finished = 1;
1746 		mptcp_set_connected(parent);
1747 	}
1748 
1749 	/* as recvmsg() does not acquire the subflow socket for ssk selection
1750 	 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1751 	 * the data available machinery here.
1752 	 */
1753 	if (mptcp_subflow_data_available(sk))
1754 		mptcp_data_ready(parent, sk);
1755 	else if (unlikely(sk->sk_err))
1756 		subflow_error_report(sk);
1757 
1758 	subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1759 
1760 	if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1761 	    !subflow->rx_eof && subflow_is_done(sk)) {
1762 		subflow->rx_eof = 1;
1763 		mptcp_subflow_eof(parent);
1764 	}
1765 }
1766 
1767 void mptcp_subflow_queue_clean(struct sock *listener_ssk)
1768 {
1769 	struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1770 	struct mptcp_sock *msk, *next, *head = NULL;
1771 	struct request_sock *req;
1772 
1773 	/* build a list of all unaccepted mptcp sockets */
1774 	spin_lock_bh(&queue->rskq_lock);
1775 	for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1776 		struct mptcp_subflow_context *subflow;
1777 		struct sock *ssk = req->sk;
1778 		struct mptcp_sock *msk;
1779 
1780 		if (!sk_is_mptcp(ssk))
1781 			continue;
1782 
1783 		subflow = mptcp_subflow_ctx(ssk);
1784 		if (!subflow || !subflow->conn)
1785 			continue;
1786 
1787 		/* skip if already in list */
1788 		msk = mptcp_sk(subflow->conn);
1789 		if (msk->dl_next || msk == head)
1790 			continue;
1791 
1792 		msk->dl_next = head;
1793 		head = msk;
1794 	}
1795 	spin_unlock_bh(&queue->rskq_lock);
1796 	if (!head)
1797 		return;
1798 
1799 	/* can't acquire the msk socket lock under the subflow one,
1800 	 * or will cause ABBA deadlock
1801 	 */
1802 	release_sock(listener_ssk);
1803 
1804 	for (msk = head; msk; msk = next) {
1805 		struct sock *sk = (struct sock *)msk;
1806 		bool do_cancel_work;
1807 
1808 		sock_hold(sk);
1809 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1810 		next = msk->dl_next;
1811 		msk->first = NULL;
1812 		msk->dl_next = NULL;
1813 
1814 		do_cancel_work = __mptcp_close(sk, 0);
1815 		release_sock(sk);
1816 		if (do_cancel_work)
1817 			mptcp_cancel_work(sk);
1818 		sock_put(sk);
1819 	}
1820 
1821 	/* we are still under the listener msk socket lock */
1822 	lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1823 }
1824 
1825 static int subflow_ulp_init(struct sock *sk)
1826 {
1827 	struct inet_connection_sock *icsk = inet_csk(sk);
1828 	struct mptcp_subflow_context *ctx;
1829 	struct tcp_sock *tp = tcp_sk(sk);
1830 	int err = 0;
1831 
1832 	/* disallow attaching ULP to a socket unless it has been
1833 	 * created with sock_create_kern()
1834 	 */
1835 	if (!sk->sk_kern_sock) {
1836 		err = -EOPNOTSUPP;
1837 		goto out;
1838 	}
1839 
1840 	ctx = subflow_create_ctx(sk, GFP_KERNEL);
1841 	if (!ctx) {
1842 		err = -ENOMEM;
1843 		goto out;
1844 	}
1845 
1846 	pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1847 
1848 	tp->is_mptcp = 1;
1849 	ctx->icsk_af_ops = icsk->icsk_af_ops;
1850 	icsk->icsk_af_ops = subflow_default_af_ops(sk);
1851 	ctx->tcp_state_change = sk->sk_state_change;
1852 	ctx->tcp_error_report = sk->sk_error_report;
1853 
1854 	WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1855 	WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1856 
1857 	sk->sk_data_ready = subflow_data_ready;
1858 	sk->sk_write_space = subflow_write_space;
1859 	sk->sk_state_change = subflow_state_change;
1860 	sk->sk_error_report = subflow_error_report;
1861 out:
1862 	return err;
1863 }
1864 
1865 static void subflow_ulp_release(struct sock *ssk)
1866 {
1867 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1868 	bool release = true;
1869 	struct sock *sk;
1870 
1871 	if (!ctx)
1872 		return;
1873 
1874 	sk = ctx->conn;
1875 	if (sk) {
1876 		/* if the msk has been orphaned, keep the ctx
1877 		 * alive, will be freed by __mptcp_close_ssk(),
1878 		 * when the subflow is still unaccepted
1879 		 */
1880 		release = ctx->disposable || list_empty(&ctx->node);
1881 		sock_put(sk);
1882 	}
1883 
1884 	mptcp_subflow_ops_undo_override(ssk);
1885 	if (release)
1886 		kfree_rcu(ctx, rcu);
1887 }
1888 
1889 static void subflow_ulp_clone(const struct request_sock *req,
1890 			      struct sock *newsk,
1891 			      const gfp_t priority)
1892 {
1893 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1894 	struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1895 	struct mptcp_subflow_context *new_ctx;
1896 
1897 	if (!tcp_rsk(req)->is_mptcp ||
1898 	    (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1899 		subflow_ulp_fallback(newsk, old_ctx);
1900 		return;
1901 	}
1902 
1903 	new_ctx = subflow_create_ctx(newsk, priority);
1904 	if (!new_ctx) {
1905 		subflow_ulp_fallback(newsk, old_ctx);
1906 		return;
1907 	}
1908 
1909 	new_ctx->conn_finished = 1;
1910 	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1911 	new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1912 	new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1913 	new_ctx->rel_write_seq = 1;
1914 	new_ctx->tcp_sock = newsk;
1915 
1916 	if (subflow_req->mp_capable) {
1917 		/* see comments in subflow_syn_recv_sock(), MPTCP connection
1918 		 * is fully established only after we receive the remote key
1919 		 */
1920 		new_ctx->mp_capable = 1;
1921 		new_ctx->local_key = subflow_req->local_key;
1922 		new_ctx->token = subflow_req->token;
1923 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1924 		new_ctx->idsn = subflow_req->idsn;
1925 
1926 		/* this is the first subflow, id is always 0 */
1927 		new_ctx->local_id_valid = 1;
1928 	} else if (subflow_req->mp_join) {
1929 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1930 		new_ctx->mp_join = 1;
1931 		new_ctx->fully_established = 1;
1932 		new_ctx->remote_key_valid = 1;
1933 		new_ctx->backup = subflow_req->backup;
1934 		new_ctx->remote_id = subflow_req->remote_id;
1935 		new_ctx->token = subflow_req->token;
1936 		new_ctx->thmac = subflow_req->thmac;
1937 
1938 		/* the subflow req id is valid, fetched via subflow_check_req()
1939 		 * and subflow_token_join_request()
1940 		 */
1941 		subflow_set_local_id(new_ctx, subflow_req->local_id);
1942 	}
1943 }
1944 
1945 static void tcp_release_cb_override(struct sock *ssk)
1946 {
1947 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1948 
1949 	if (mptcp_subflow_has_delegated_action(subflow))
1950 		mptcp_subflow_process_delegated(ssk);
1951 
1952 	tcp_release_cb(ssk);
1953 }
1954 
1955 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1956 	.name		= "mptcp",
1957 	.owner		= THIS_MODULE,
1958 	.init		= subflow_ulp_init,
1959 	.release	= subflow_ulp_release,
1960 	.clone		= subflow_ulp_clone,
1961 };
1962 
1963 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1964 {
1965 	subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1966 	subflow_ops->slab_name = "request_sock_subflow";
1967 
1968 	subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1969 					      subflow_ops->obj_size, 0,
1970 					      SLAB_ACCOUNT |
1971 					      SLAB_TYPESAFE_BY_RCU,
1972 					      NULL);
1973 	if (!subflow_ops->slab)
1974 		return -ENOMEM;
1975 
1976 	subflow_ops->destructor = subflow_req_destructor;
1977 
1978 	return 0;
1979 }
1980 
1981 void __init mptcp_subflow_init(void)
1982 {
1983 	mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1984 	if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1985 		panic("MPTCP: failed to init subflow request sock ops\n");
1986 
1987 	subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1988 	subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1989 	subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack;
1990 
1991 	subflow_specific = ipv4_specific;
1992 	subflow_specific.conn_request = subflow_v4_conn_request;
1993 	subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1994 	subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1995 	subflow_specific.rebuild_header = subflow_rebuild_header;
1996 
1997 	tcp_prot_override = tcp_prot;
1998 	tcp_prot_override.release_cb = tcp_release_cb_override;
1999 
2000 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2001 	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
2002 	subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
2003 	subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack;
2004 
2005 	subflow_v6_specific = ipv6_specific;
2006 	subflow_v6_specific.conn_request = subflow_v6_conn_request;
2007 	subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
2008 	subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
2009 	subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
2010 
2011 	subflow_v6m_specific = subflow_v6_specific;
2012 	subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
2013 	subflow_v6m_specific.send_check = ipv4_specific.send_check;
2014 	subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
2015 	subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
2016 	subflow_v6m_specific.net_frag_header_len = 0;
2017 	subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
2018 
2019 	tcpv6_prot_override = tcpv6_prot;
2020 	tcpv6_prot_override.release_cb = tcp_release_cb_override;
2021 #endif
2022 
2023 	mptcp_diag_subflow_init(&subflow_ulp_ops);
2024 
2025 	if (tcp_register_ulp(&subflow_ulp_ops) != 0)
2026 		panic("MPTCP: failed to register subflows to ULP\n");
2027 }
2028