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