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