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