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