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