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