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