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