xref: /openbmc/linux/net/mptcp/subflow.c (revision 842ca547)
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 
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", subflow_req);
43 
44 	if (subflow_req->msk)
45 		sock_put((struct sock *)subflow_req->msk);
46 
47 	mptcp_token_destroy_request(req);
48 	tcp_request_sock_ops.destructor(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 	       READ_ONCE(msk->pm.accept_subflow);
66 }
67 
68 /* validate received token and create truncated hmac and nonce for SYN-ACK */
69 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
70 {
71 	struct mptcp_sock *msk = subflow_req->msk;
72 	u8 hmac[SHA256_DIGEST_SIZE];
73 
74 	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
75 
76 	subflow_generate_hmac(msk->local_key, msk->remote_key,
77 			      subflow_req->local_nonce,
78 			      subflow_req->remote_nonce, hmac);
79 
80 	subflow_req->thmac = get_unaligned_be64(hmac);
81 }
82 
83 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
84 {
85 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
86 	struct mptcp_sock *msk;
87 	int local_id;
88 
89 	msk = mptcp_token_get_sock(subflow_req->token);
90 	if (!msk) {
91 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
92 		return NULL;
93 	}
94 
95 	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
96 	if (local_id < 0) {
97 		sock_put((struct sock *)msk);
98 		return NULL;
99 	}
100 	subflow_req->local_id = local_id;
101 
102 	return msk;
103 }
104 
105 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
106 {
107 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
108 
109 	subflow_req->mp_capable = 0;
110 	subflow_req->mp_join = 0;
111 	subflow_req->msk = NULL;
112 	mptcp_token_init_request(req);
113 }
114 
115 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
116 {
117 	return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
118 }
119 
120 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
121 {
122 	struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
123 
124 	if (mpext) {
125 		memset(mpext, 0, sizeof(*mpext));
126 		mpext->reset_reason = reason;
127 	}
128 }
129 
130 /* Init mptcp request socket.
131  *
132  * Returns an error code if a JOIN has failed and a TCP reset
133  * should be sent.
134  */
135 static int subflow_check_req(struct request_sock *req,
136 			     const struct sock *sk_listener,
137 			     struct sk_buff *skb)
138 {
139 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
140 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
141 	struct mptcp_options_received mp_opt;
142 
143 	pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
144 
145 #ifdef CONFIG_TCP_MD5SIG
146 	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
147 	 * TCP option space.
148 	 */
149 	if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
150 		return -EINVAL;
151 #endif
152 
153 	mptcp_get_options(skb, &mp_opt);
154 
155 	if (mp_opt.mp_capable) {
156 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
157 
158 		if (mp_opt.mp_join)
159 			return 0;
160 	} else if (mp_opt.mp_join) {
161 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
162 	}
163 
164 	if (mp_opt.mp_capable && listener->request_mptcp) {
165 		int err, retries = 4;
166 
167 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
168 again:
169 		do {
170 			get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
171 		} while (subflow_req->local_key == 0);
172 
173 		if (unlikely(req->syncookie)) {
174 			mptcp_crypto_key_sha(subflow_req->local_key,
175 					     &subflow_req->token,
176 					     &subflow_req->idsn);
177 			if (mptcp_token_exists(subflow_req->token)) {
178 				if (retries-- > 0)
179 					goto again;
180 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
181 			} else {
182 				subflow_req->mp_capable = 1;
183 			}
184 			return 0;
185 		}
186 
187 		err = mptcp_token_new_request(req);
188 		if (err == 0)
189 			subflow_req->mp_capable = 1;
190 		else if (retries-- > 0)
191 			goto again;
192 		else
193 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
194 
195 	} else if (mp_opt.mp_join && listener->request_mptcp) {
196 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
197 		subflow_req->mp_join = 1;
198 		subflow_req->backup = mp_opt.backup;
199 		subflow_req->remote_id = mp_opt.join_id;
200 		subflow_req->token = mp_opt.token;
201 		subflow_req->remote_nonce = mp_opt.nonce;
202 		subflow_req->msk = subflow_token_join_request(req);
203 
204 		/* Can't fall back to TCP in this case. */
205 		if (!subflow_req->msk) {
206 			subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
207 			return -EPERM;
208 		}
209 
210 		if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
211 			pr_debug("syn inet_sport=%d %d",
212 				 ntohs(inet_sk(sk_listener)->inet_sport),
213 				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
214 			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
215 				sock_put((struct sock *)subflow_req->msk);
216 				mptcp_token_destroy_request(req);
217 				tcp_request_sock_ops.destructor(req);
218 				subflow_req->msk = NULL;
219 				subflow_req->mp_join = 0;
220 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
221 				return -EPERM;
222 			}
223 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
224 		}
225 
226 		subflow_req_create_thmac(subflow_req);
227 
228 		if (unlikely(req->syncookie)) {
229 			if (mptcp_can_accept_new_subflow(subflow_req->msk))
230 				subflow_init_req_cookie_join_save(subflow_req, skb);
231 		}
232 
233 		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
234 			 subflow_req->remote_nonce, subflow_req->msk);
235 	}
236 
237 	return 0;
238 }
239 
240 int mptcp_subflow_init_cookie_req(struct request_sock *req,
241 				  const struct sock *sk_listener,
242 				  struct sk_buff *skb)
243 {
244 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
245 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
246 	struct mptcp_options_received mp_opt;
247 	int err;
248 
249 	subflow_init_req(req, sk_listener);
250 	mptcp_get_options(skb, &mp_opt);
251 
252 	if (mp_opt.mp_capable && mp_opt.mp_join)
253 		return -EINVAL;
254 
255 	if (mp_opt.mp_capable && listener->request_mptcp) {
256 		if (mp_opt.sndr_key == 0)
257 			return -EINVAL;
258 
259 		subflow_req->local_key = mp_opt.rcvr_key;
260 		err = mptcp_token_new_request(req);
261 		if (err)
262 			return err;
263 
264 		subflow_req->mp_capable = 1;
265 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
266 	} else if (mp_opt.mp_join && listener->request_mptcp) {
267 		if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
268 			return -EINVAL;
269 
270 		if (mptcp_can_accept_new_subflow(subflow_req->msk))
271 			subflow_req->mp_join = 1;
272 
273 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
274 	}
275 
276 	return 0;
277 }
278 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
279 
280 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
281 					      struct sk_buff *skb,
282 					      struct flowi *fl,
283 					      struct request_sock *req)
284 {
285 	struct dst_entry *dst;
286 	int err;
287 
288 	tcp_rsk(req)->is_mptcp = 1;
289 	subflow_init_req(req, sk);
290 
291 	dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
292 	if (!dst)
293 		return NULL;
294 
295 	err = subflow_check_req(req, sk, skb);
296 	if (err == 0)
297 		return dst;
298 
299 	dst_release(dst);
300 	if (!req->syncookie)
301 		tcp_request_sock_ops.send_reset(sk, skb);
302 	return NULL;
303 }
304 
305 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
306 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
307 					      struct sk_buff *skb,
308 					      struct flowi *fl,
309 					      struct request_sock *req)
310 {
311 	struct dst_entry *dst;
312 	int err;
313 
314 	tcp_rsk(req)->is_mptcp = 1;
315 	subflow_init_req(req, sk);
316 
317 	dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
318 	if (!dst)
319 		return NULL;
320 
321 	err = subflow_check_req(req, sk, skb);
322 	if (err == 0)
323 		return dst;
324 
325 	dst_release(dst);
326 	if (!req->syncookie)
327 		tcp6_request_sock_ops.send_reset(sk, skb);
328 	return NULL;
329 }
330 #endif
331 
332 /* validate received truncated hmac and create hmac for third ACK */
333 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
334 {
335 	u8 hmac[SHA256_DIGEST_SIZE];
336 	u64 thmac;
337 
338 	subflow_generate_hmac(subflow->remote_key, subflow->local_key,
339 			      subflow->remote_nonce, subflow->local_nonce,
340 			      hmac);
341 
342 	thmac = get_unaligned_be64(hmac);
343 	pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
344 		 subflow, subflow->token,
345 		 (unsigned long long)thmac,
346 		 (unsigned long long)subflow->thmac);
347 
348 	return thmac == subflow->thmac;
349 }
350 
351 void mptcp_subflow_reset(struct sock *ssk)
352 {
353 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
354 	struct sock *sk = subflow->conn;
355 
356 	/* must hold: tcp_done() could drop last reference on parent */
357 	sock_hold(sk);
358 
359 	tcp_set_state(ssk, TCP_CLOSE);
360 	tcp_send_active_reset(ssk, GFP_ATOMIC);
361 	tcp_done(ssk);
362 	if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
363 	    schedule_work(&mptcp_sk(sk)->work))
364 		return; /* worker will put sk for us */
365 
366 	sock_put(sk);
367 }
368 
369 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
370 {
371 	return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
372 }
373 
374 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
375 {
376 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
377 	struct mptcp_options_received mp_opt;
378 	struct sock *parent = subflow->conn;
379 
380 	subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
381 
382 	if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
383 		inet_sk_state_store(parent, TCP_ESTABLISHED);
384 		parent->sk_state_change(parent);
385 	}
386 
387 	/* be sure no special action on any packet other than syn-ack */
388 	if (subflow->conn_finished)
389 		return;
390 
391 	mptcp_propagate_sndbuf(parent, sk);
392 	subflow->rel_write_seq = 1;
393 	subflow->conn_finished = 1;
394 	subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
395 	pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
396 
397 	mptcp_get_options(skb, &mp_opt);
398 	if (subflow->request_mptcp) {
399 		if (!mp_opt.mp_capable) {
400 			MPTCP_INC_STATS(sock_net(sk),
401 					MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
402 			mptcp_do_fallback(sk);
403 			pr_fallback(mptcp_sk(subflow->conn));
404 			goto fallback;
405 		}
406 
407 		subflow->mp_capable = 1;
408 		subflow->can_ack = 1;
409 		subflow->remote_key = mp_opt.sndr_key;
410 		pr_debug("subflow=%p, remote_key=%llu", subflow,
411 			 subflow->remote_key);
412 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
413 		mptcp_finish_connect(sk);
414 	} else if (subflow->request_join) {
415 		u8 hmac[SHA256_DIGEST_SIZE];
416 
417 		if (!mp_opt.mp_join) {
418 			subflow->reset_reason = MPTCP_RST_EMPTCP;
419 			goto do_reset;
420 		}
421 
422 		subflow->thmac = mp_opt.thmac;
423 		subflow->remote_nonce = mp_opt.nonce;
424 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
425 			 subflow->thmac, subflow->remote_nonce);
426 
427 		if (!subflow_thmac_valid(subflow)) {
428 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
429 			subflow->reset_reason = MPTCP_RST_EMPTCP;
430 			goto do_reset;
431 		}
432 
433 		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
434 				      subflow->local_nonce,
435 				      subflow->remote_nonce,
436 				      hmac);
437 		memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
438 
439 		if (!mptcp_finish_join(sk))
440 			goto do_reset;
441 
442 		subflow->mp_join = 1;
443 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
444 
445 		if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
446 			pr_debug("synack inet_dport=%d %d",
447 				 ntohs(inet_sk(sk)->inet_dport),
448 				 ntohs(inet_sk(parent)->inet_dport));
449 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
450 		}
451 	} else if (mptcp_check_fallback(sk)) {
452 fallback:
453 		mptcp_rcv_space_init(mptcp_sk(parent), sk);
454 	}
455 	return;
456 
457 do_reset:
458 	subflow->reset_transient = 0;
459 	mptcp_subflow_reset(sk);
460 }
461 
462 struct request_sock_ops mptcp_subflow_request_sock_ops;
463 EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
464 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
465 
466 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
467 {
468 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
469 
470 	pr_debug("subflow=%p", subflow);
471 
472 	/* Never answer to SYNs sent to broadcast or multicast */
473 	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
474 		goto drop;
475 
476 	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
477 				&subflow_request_sock_ipv4_ops,
478 				sk, skb);
479 drop:
480 	tcp_listendrop(sk);
481 	return 0;
482 }
483 
484 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
485 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
486 static struct inet_connection_sock_af_ops subflow_v6_specific;
487 static struct inet_connection_sock_af_ops subflow_v6m_specific;
488 static struct proto tcpv6_prot_override;
489 
490 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
491 {
492 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
493 
494 	pr_debug("subflow=%p", subflow);
495 
496 	if (skb->protocol == htons(ETH_P_IP))
497 		return subflow_v4_conn_request(sk, skb);
498 
499 	if (!ipv6_unicast_destination(skb))
500 		goto drop;
501 
502 	if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
503 		__IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
504 		return 0;
505 	}
506 
507 	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
508 				&subflow_request_sock_ipv6_ops, sk, skb);
509 
510 drop:
511 	tcp_listendrop(sk);
512 	return 0; /* don't send reset */
513 }
514 #endif
515 
516 /* validate hmac received in third ACK */
517 static bool subflow_hmac_valid(const struct request_sock *req,
518 			       const struct mptcp_options_received *mp_opt)
519 {
520 	const struct mptcp_subflow_request_sock *subflow_req;
521 	u8 hmac[SHA256_DIGEST_SIZE];
522 	struct mptcp_sock *msk;
523 
524 	subflow_req = mptcp_subflow_rsk(req);
525 	msk = subflow_req->msk;
526 	if (!msk)
527 		return false;
528 
529 	subflow_generate_hmac(msk->remote_key, msk->local_key,
530 			      subflow_req->remote_nonce,
531 			      subflow_req->local_nonce, hmac);
532 
533 	return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
534 }
535 
536 static void mptcp_sock_destruct(struct sock *sk)
537 {
538 	/* if new mptcp socket isn't accepted, it is free'd
539 	 * from the tcp listener sockets request queue, linked
540 	 * from req->sk.  The tcp socket is released.
541 	 * This calls the ULP release function which will
542 	 * also remove the mptcp socket, via
543 	 * sock_put(ctx->conn).
544 	 *
545 	 * Problem is that the mptcp socket will be in
546 	 * ESTABLISHED state and will not have the SOCK_DEAD flag.
547 	 * Both result in warnings from inet_sock_destruct.
548 	 */
549 
550 	if (sk->sk_state == TCP_ESTABLISHED) {
551 		sk->sk_state = TCP_CLOSE;
552 		WARN_ON_ONCE(sk->sk_socket);
553 		sock_orphan(sk);
554 	}
555 
556 	mptcp_destroy_common(mptcp_sk(sk));
557 	inet_sock_destruct(sk);
558 }
559 
560 static void mptcp_force_close(struct sock *sk)
561 {
562 	inet_sk_state_store(sk, TCP_CLOSE);
563 	sk_common_release(sk);
564 }
565 
566 static void subflow_ulp_fallback(struct sock *sk,
567 				 struct mptcp_subflow_context *old_ctx)
568 {
569 	struct inet_connection_sock *icsk = inet_csk(sk);
570 
571 	mptcp_subflow_tcp_fallback(sk, old_ctx);
572 	icsk->icsk_ulp_ops = NULL;
573 	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
574 	tcp_sk(sk)->is_mptcp = 0;
575 
576 	mptcp_subflow_ops_undo_override(sk);
577 }
578 
579 static void subflow_drop_ctx(struct sock *ssk)
580 {
581 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
582 
583 	if (!ctx)
584 		return;
585 
586 	subflow_ulp_fallback(ssk, ctx);
587 	if (ctx->conn)
588 		sock_put(ctx->conn);
589 
590 	kfree_rcu(ctx, rcu);
591 }
592 
593 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
594 				     struct mptcp_options_received *mp_opt)
595 {
596 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
597 
598 	subflow->remote_key = mp_opt->sndr_key;
599 	subflow->fully_established = 1;
600 	subflow->can_ack = 1;
601 	WRITE_ONCE(msk->fully_established, true);
602 }
603 
604 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
605 					  struct sk_buff *skb,
606 					  struct request_sock *req,
607 					  struct dst_entry *dst,
608 					  struct request_sock *req_unhash,
609 					  bool *own_req)
610 {
611 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
612 	struct mptcp_subflow_request_sock *subflow_req;
613 	struct mptcp_options_received mp_opt;
614 	bool fallback, fallback_is_fatal;
615 	struct sock *new_msk = NULL;
616 	struct sock *child;
617 
618 	pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
619 
620 	/* After child creation we must look for 'mp_capable' even when options
621 	 * are not parsed
622 	 */
623 	mp_opt.mp_capable = 0;
624 
625 	/* hopefully temporary handling for MP_JOIN+syncookie */
626 	subflow_req = mptcp_subflow_rsk(req);
627 	fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
628 	fallback = !tcp_rsk(req)->is_mptcp;
629 	if (fallback)
630 		goto create_child;
631 
632 	/* if the sk is MP_CAPABLE, we try to fetch the client key */
633 	if (subflow_req->mp_capable) {
634 		if (TCP_SKB_CB(skb)->seq != subflow_req->ssn_offset + 1) {
635 			/* here we can receive and accept an in-window,
636 			 * out-of-order pkt, which will not carry the MP_CAPABLE
637 			 * opt even on mptcp enabled paths
638 			 */
639 			goto create_msk;
640 		}
641 
642 		mptcp_get_options(skb, &mp_opt);
643 		if (!mp_opt.mp_capable) {
644 			fallback = true;
645 			goto create_child;
646 		}
647 
648 create_msk:
649 		new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
650 		if (!new_msk)
651 			fallback = true;
652 	} else if (subflow_req->mp_join) {
653 		mptcp_get_options(skb, &mp_opt);
654 		if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
655 		    !mptcp_can_accept_new_subflow(subflow_req->msk)) {
656 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
657 			fallback = true;
658 		}
659 	}
660 
661 create_child:
662 	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
663 						     req_unhash, own_req);
664 
665 	if (child && *own_req) {
666 		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
667 
668 		tcp_rsk(req)->drop_req = false;
669 
670 		/* we need to fallback on ctx allocation failure and on pre-reqs
671 		 * checking above. In the latter scenario we additionally need
672 		 * to reset the context to non MPTCP status.
673 		 */
674 		if (!ctx || fallback) {
675 			if (fallback_is_fatal) {
676 				subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
677 				goto dispose_child;
678 			}
679 
680 			subflow_drop_ctx(child);
681 			goto out;
682 		}
683 
684 		/* ssk inherits options of listener sk */
685 		ctx->setsockopt_seq = listener->setsockopt_seq;
686 
687 		if (ctx->mp_capable) {
688 			/* this can't race with mptcp_close(), as the msk is
689 			 * not yet exposted to user-space
690 			 */
691 			inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
692 
693 			/* record the newly created socket as the first msk
694 			 * subflow, but don't link it yet into conn_list
695 			 */
696 			WRITE_ONCE(mptcp_sk(new_msk)->first, child);
697 
698 			/* new mpc subflow takes ownership of the newly
699 			 * created mptcp socket
700 			 */
701 			new_msk->sk_destruct = mptcp_sock_destruct;
702 			mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
703 			mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
704 			mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
705 			ctx->conn = new_msk;
706 			new_msk = NULL;
707 
708 			/* with OoO packets we can reach here without ingress
709 			 * mpc option
710 			 */
711 			if (mp_opt.mp_capable)
712 				mptcp_subflow_fully_established(ctx, &mp_opt);
713 		} else if (ctx->mp_join) {
714 			struct mptcp_sock *owner;
715 
716 			owner = subflow_req->msk;
717 			if (!owner) {
718 				subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
719 				goto dispose_child;
720 			}
721 
722 			/* move the msk reference ownership to the subflow */
723 			subflow_req->msk = NULL;
724 			ctx->conn = (struct sock *)owner;
725 
726 			if (subflow_use_different_sport(owner, sk)) {
727 				pr_debug("ack inet_sport=%d %d",
728 					 ntohs(inet_sk(sk)->inet_sport),
729 					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
730 				if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
731 					SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
732 					goto dispose_child;
733 				}
734 				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
735 			}
736 
737 			if (!mptcp_finish_join(child))
738 				goto dispose_child;
739 
740 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
741 			tcp_rsk(req)->drop_req = true;
742 		}
743 	}
744 
745 out:
746 	/* dispose of the left over mptcp master, if any */
747 	if (unlikely(new_msk))
748 		mptcp_force_close(new_msk);
749 
750 	/* check for expected invariant - should never trigger, just help
751 	 * catching eariler subtle bugs
752 	 */
753 	WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
754 		     (!mptcp_subflow_ctx(child) ||
755 		      !mptcp_subflow_ctx(child)->conn));
756 	return child;
757 
758 dispose_child:
759 	subflow_drop_ctx(child);
760 	tcp_rsk(req)->drop_req = true;
761 	inet_csk_prepare_for_destroy_sock(child);
762 	tcp_done(child);
763 	req->rsk_ops->send_reset(sk, skb);
764 
765 	/* The last child reference will be released by the caller */
766 	return child;
767 }
768 
769 static struct inet_connection_sock_af_ops subflow_specific;
770 static struct proto tcp_prot_override;
771 
772 enum mapping_status {
773 	MAPPING_OK,
774 	MAPPING_INVALID,
775 	MAPPING_EMPTY,
776 	MAPPING_DATA_FIN,
777 	MAPPING_DUMMY
778 };
779 
780 static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
781 {
782 	if ((u32)seq == (u32)old_seq)
783 		return old_seq;
784 
785 	/* Assume map covers data not mapped yet. */
786 	return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
787 }
788 
789 static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
790 {
791 	WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
792 		  ssn, subflow->map_subflow_seq, subflow->map_data_len);
793 }
794 
795 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
796 {
797 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
798 	unsigned int skb_consumed;
799 
800 	skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
801 	if (WARN_ON_ONCE(skb_consumed >= skb->len))
802 		return true;
803 
804 	return skb->len - skb_consumed <= subflow->map_data_len -
805 					  mptcp_subflow_get_map_offset(subflow);
806 }
807 
808 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
809 {
810 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
811 	u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
812 
813 	if (unlikely(before(ssn, subflow->map_subflow_seq))) {
814 		/* Mapping covers data later in the subflow stream,
815 		 * currently unsupported.
816 		 */
817 		warn_bad_map(subflow, ssn);
818 		return false;
819 	}
820 	if (unlikely(!before(ssn, subflow->map_subflow_seq +
821 				  subflow->map_data_len))) {
822 		/* Mapping does covers past subflow data, invalid */
823 		warn_bad_map(subflow, ssn + skb->len);
824 		return false;
825 	}
826 	return true;
827 }
828 
829 static enum mapping_status get_mapping_status(struct sock *ssk,
830 					      struct mptcp_sock *msk)
831 {
832 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
833 	struct mptcp_ext *mpext;
834 	struct sk_buff *skb;
835 	u16 data_len;
836 	u64 map_seq;
837 
838 	skb = skb_peek(&ssk->sk_receive_queue);
839 	if (!skb)
840 		return MAPPING_EMPTY;
841 
842 	if (mptcp_check_fallback(ssk))
843 		return MAPPING_DUMMY;
844 
845 	mpext = mptcp_get_ext(skb);
846 	if (!mpext || !mpext->use_map) {
847 		if (!subflow->map_valid && !skb->len) {
848 			/* the TCP stack deliver 0 len FIN pkt to the receive
849 			 * queue, that is the only 0len pkts ever expected here,
850 			 * and we can admit no mapping only for 0 len pkts
851 			 */
852 			if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
853 				WARN_ONCE(1, "0len seq %d:%d flags %x",
854 					  TCP_SKB_CB(skb)->seq,
855 					  TCP_SKB_CB(skb)->end_seq,
856 					  TCP_SKB_CB(skb)->tcp_flags);
857 			sk_eat_skb(ssk, skb);
858 			return MAPPING_EMPTY;
859 		}
860 
861 		if (!subflow->map_valid)
862 			return MAPPING_INVALID;
863 
864 		goto validate_seq;
865 	}
866 
867 	trace_get_mapping_status(mpext);
868 
869 	data_len = mpext->data_len;
870 	if (data_len == 0) {
871 		pr_err("Infinite mapping not handled");
872 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
873 		return MAPPING_INVALID;
874 	}
875 
876 	if (mpext->data_fin == 1) {
877 		if (data_len == 1) {
878 			bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
879 								 mpext->dsn64);
880 			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
881 			if (subflow->map_valid) {
882 				/* A DATA_FIN might arrive in a DSS
883 				 * option before the previous mapping
884 				 * has been fully consumed. Continue
885 				 * handling the existing mapping.
886 				 */
887 				skb_ext_del(skb, SKB_EXT_MPTCP);
888 				return MAPPING_OK;
889 			} else {
890 				if (updated && schedule_work(&msk->work))
891 					sock_hold((struct sock *)msk);
892 
893 				return MAPPING_DATA_FIN;
894 			}
895 		} else {
896 			u64 data_fin_seq = mpext->data_seq + data_len - 1;
897 
898 			/* If mpext->data_seq is a 32-bit value, data_fin_seq
899 			 * must also be limited to 32 bits.
900 			 */
901 			if (!mpext->dsn64)
902 				data_fin_seq &= GENMASK_ULL(31, 0);
903 
904 			mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
905 			pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
906 				 data_fin_seq, mpext->dsn64);
907 		}
908 
909 		/* Adjust for DATA_FIN using 1 byte of sequence space */
910 		data_len--;
911 	}
912 
913 	if (!mpext->dsn64) {
914 		map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
915 				     mpext->data_seq);
916 		pr_debug("expanded seq=%llu", subflow->map_seq);
917 	} else {
918 		map_seq = mpext->data_seq;
919 	}
920 	WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
921 
922 	if (subflow->map_valid) {
923 		/* Allow replacing only with an identical map */
924 		if (subflow->map_seq == map_seq &&
925 		    subflow->map_subflow_seq == mpext->subflow_seq &&
926 		    subflow->map_data_len == data_len) {
927 			skb_ext_del(skb, SKB_EXT_MPTCP);
928 			return MAPPING_OK;
929 		}
930 
931 		/* If this skb data are fully covered by the current mapping,
932 		 * the new map would need caching, which is not supported
933 		 */
934 		if (skb_is_fully_mapped(ssk, skb)) {
935 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
936 			return MAPPING_INVALID;
937 		}
938 
939 		/* will validate the next map after consuming the current one */
940 		return MAPPING_OK;
941 	}
942 
943 	subflow->map_seq = map_seq;
944 	subflow->map_subflow_seq = mpext->subflow_seq;
945 	subflow->map_data_len = data_len;
946 	subflow->map_valid = 1;
947 	subflow->mpc_map = mpext->mpc_map;
948 	pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
949 		 subflow->map_seq, subflow->map_subflow_seq,
950 		 subflow->map_data_len);
951 
952 validate_seq:
953 	/* we revalidate valid mapping on new skb, because we must ensure
954 	 * the current skb is completely covered by the available mapping
955 	 */
956 	if (!validate_mapping(ssk, skb))
957 		return MAPPING_INVALID;
958 
959 	skb_ext_del(skb, SKB_EXT_MPTCP);
960 	return MAPPING_OK;
961 }
962 
963 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
964 				       u64 limit)
965 {
966 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
967 	bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
968 	u32 incr;
969 
970 	incr = limit >= skb->len ? skb->len + fin : limit;
971 
972 	pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
973 		 subflow->map_subflow_seq);
974 	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
975 	tcp_sk(ssk)->copied_seq += incr;
976 	if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
977 		sk_eat_skb(ssk, skb);
978 	if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
979 		subflow->map_valid = 0;
980 }
981 
982 /* sched mptcp worker to remove the subflow if no more data is pending */
983 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
984 {
985 	struct sock *sk = (struct sock *)msk;
986 
987 	if (likely(ssk->sk_state != TCP_CLOSE))
988 		return;
989 
990 	if (skb_queue_empty(&ssk->sk_receive_queue) &&
991 	    !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
992 		sock_hold(sk);
993 		if (!schedule_work(&msk->work))
994 			sock_put(sk);
995 	}
996 }
997 
998 static bool subflow_check_data_avail(struct sock *ssk)
999 {
1000 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1001 	enum mapping_status status;
1002 	struct mptcp_sock *msk;
1003 	struct sk_buff *skb;
1004 
1005 	if (!skb_peek(&ssk->sk_receive_queue))
1006 		subflow->data_avail = 0;
1007 	if (subflow->data_avail)
1008 		return true;
1009 
1010 	msk = mptcp_sk(subflow->conn);
1011 	for (;;) {
1012 		u64 ack_seq;
1013 		u64 old_ack;
1014 
1015 		status = get_mapping_status(ssk, msk);
1016 		trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1017 		if (status == MAPPING_INVALID) {
1018 			ssk->sk_err = EBADMSG;
1019 			goto fatal;
1020 		}
1021 		if (status == MAPPING_DUMMY) {
1022 			__mptcp_do_fallback(msk);
1023 			skb = skb_peek(&ssk->sk_receive_queue);
1024 			subflow->map_valid = 1;
1025 			subflow->map_seq = READ_ONCE(msk->ack_seq);
1026 			subflow->map_data_len = skb->len;
1027 			subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq -
1028 						   subflow->ssn_offset;
1029 			subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1030 			return true;
1031 		}
1032 
1033 		if (status != MAPPING_OK)
1034 			goto no_data;
1035 
1036 		skb = skb_peek(&ssk->sk_receive_queue);
1037 		if (WARN_ON_ONCE(!skb))
1038 			goto no_data;
1039 
1040 		/* if msk lacks the remote key, this subflow must provide an
1041 		 * MP_CAPABLE-based mapping
1042 		 */
1043 		if (unlikely(!READ_ONCE(msk->can_ack))) {
1044 			if (!subflow->mpc_map) {
1045 				ssk->sk_err = EBADMSG;
1046 				goto fatal;
1047 			}
1048 			WRITE_ONCE(msk->remote_key, subflow->remote_key);
1049 			WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1050 			WRITE_ONCE(msk->can_ack, true);
1051 		}
1052 
1053 		old_ack = READ_ONCE(msk->ack_seq);
1054 		ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1055 		pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1056 			 ack_seq);
1057 		if (ack_seq == old_ack) {
1058 			subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
1059 			break;
1060 		} else if (after64(ack_seq, old_ack)) {
1061 			subflow->data_avail = MPTCP_SUBFLOW_OOO_DATA;
1062 			break;
1063 		}
1064 
1065 		/* only accept in-sequence mapping. Old values are spurious
1066 		 * retransmission
1067 		 */
1068 		mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1069 	}
1070 	return true;
1071 
1072 no_data:
1073 	subflow_sched_work_if_closed(msk, ssk);
1074 	return false;
1075 fatal:
1076 	/* fatal protocol error, close the socket */
1077 	/* This barrier is coupled with smp_rmb() in tcp_poll() */
1078 	smp_wmb();
1079 	ssk->sk_error_report(ssk);
1080 	tcp_set_state(ssk, TCP_CLOSE);
1081 	subflow->reset_transient = 0;
1082 	subflow->reset_reason = MPTCP_RST_EMPTCP;
1083 	tcp_send_active_reset(ssk, GFP_ATOMIC);
1084 	subflow->data_avail = 0;
1085 	return false;
1086 }
1087 
1088 bool mptcp_subflow_data_available(struct sock *sk)
1089 {
1090 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1091 
1092 	/* check if current mapping is still valid */
1093 	if (subflow->map_valid &&
1094 	    mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1095 		subflow->map_valid = 0;
1096 		subflow->data_avail = 0;
1097 
1098 		pr_debug("Done with mapping: seq=%u data_len=%u",
1099 			 subflow->map_subflow_seq,
1100 			 subflow->map_data_len);
1101 	}
1102 
1103 	return subflow_check_data_avail(sk);
1104 }
1105 
1106 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1107  * not the ssk one.
1108  *
1109  * In mptcp, rwin is about the mptcp-level connection data.
1110  *
1111  * Data that is still on the ssk rx queue can thus be ignored,
1112  * as far as mptcp peer is concerned that data is still inflight.
1113  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1114  */
1115 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1116 {
1117 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1118 	const struct sock *sk = subflow->conn;
1119 
1120 	*space = __mptcp_space(sk);
1121 	*full_space = tcp_full_space(sk);
1122 }
1123 
1124 static void subflow_data_ready(struct sock *sk)
1125 {
1126 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1127 	u16 state = 1 << inet_sk_state_load(sk);
1128 	struct sock *parent = subflow->conn;
1129 	struct mptcp_sock *msk;
1130 
1131 	msk = mptcp_sk(parent);
1132 	if (state & TCPF_LISTEN) {
1133 		/* MPJ subflow are removed from accept queue before reaching here,
1134 		 * avoid stray wakeups
1135 		 */
1136 		if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1137 			return;
1138 
1139 		set_bit(MPTCP_DATA_READY, &msk->flags);
1140 		parent->sk_data_ready(parent);
1141 		return;
1142 	}
1143 
1144 	WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1145 		     !subflow->mp_join && !(state & TCPF_CLOSE));
1146 
1147 	if (mptcp_subflow_data_available(sk))
1148 		mptcp_data_ready(parent, sk);
1149 }
1150 
1151 static void subflow_write_space(struct sock *ssk)
1152 {
1153 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1154 
1155 	mptcp_propagate_sndbuf(sk, ssk);
1156 	mptcp_write_space(sk);
1157 }
1158 
1159 void __mptcp_error_report(struct sock *sk)
1160 {
1161 	struct mptcp_subflow_context *subflow;
1162 	struct mptcp_sock *msk = mptcp_sk(sk);
1163 
1164 	mptcp_for_each_subflow(msk, subflow) {
1165 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1166 		int err = sock_error(ssk);
1167 
1168 		if (!err)
1169 			continue;
1170 
1171 		/* only propagate errors on fallen-back sockets or
1172 		 * on MPC connect
1173 		 */
1174 		if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1175 			continue;
1176 
1177 		inet_sk_state_store(sk, inet_sk_state_load(ssk));
1178 		sk->sk_err = -err;
1179 
1180 		/* This barrier is coupled with smp_rmb() in mptcp_poll() */
1181 		smp_wmb();
1182 		sk->sk_error_report(sk);
1183 		break;
1184 	}
1185 }
1186 
1187 static void subflow_error_report(struct sock *ssk)
1188 {
1189 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1190 
1191 	mptcp_data_lock(sk);
1192 	if (!sock_owned_by_user(sk))
1193 		__mptcp_error_report(sk);
1194 	else
1195 		set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->flags);
1196 	mptcp_data_unlock(sk);
1197 }
1198 
1199 static struct inet_connection_sock_af_ops *
1200 subflow_default_af_ops(struct sock *sk)
1201 {
1202 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1203 	if (sk->sk_family == AF_INET6)
1204 		return &subflow_v6_specific;
1205 #endif
1206 	return &subflow_specific;
1207 }
1208 
1209 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1210 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1211 {
1212 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1213 	struct inet_connection_sock *icsk = inet_csk(sk);
1214 	struct inet_connection_sock_af_ops *target;
1215 
1216 	target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1217 
1218 	pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1219 		 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1220 
1221 	if (likely(icsk->icsk_af_ops == target))
1222 		return;
1223 
1224 	subflow->icsk_af_ops = icsk->icsk_af_ops;
1225 	icsk->icsk_af_ops = target;
1226 }
1227 #endif
1228 
1229 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1230 			 struct sockaddr_storage *addr,
1231 			 unsigned short family)
1232 {
1233 	memset(addr, 0, sizeof(*addr));
1234 	addr->ss_family = family;
1235 	if (addr->ss_family == AF_INET) {
1236 		struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1237 
1238 		if (info->family == AF_INET)
1239 			in_addr->sin_addr = info->addr;
1240 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1241 		else if (ipv6_addr_v4mapped(&info->addr6))
1242 			in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1243 #endif
1244 		in_addr->sin_port = info->port;
1245 	}
1246 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1247 	else if (addr->ss_family == AF_INET6) {
1248 		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1249 
1250 		if (info->family == AF_INET)
1251 			ipv6_addr_set_v4mapped(info->addr.s_addr,
1252 					       &in6_addr->sin6_addr);
1253 		else
1254 			in6_addr->sin6_addr = info->addr6;
1255 		in6_addr->sin6_port = info->port;
1256 	}
1257 #endif
1258 }
1259 
1260 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1261 			    const struct mptcp_addr_info *remote,
1262 			    u8 flags, int ifindex)
1263 {
1264 	struct mptcp_sock *msk = mptcp_sk(sk);
1265 	struct mptcp_subflow_context *subflow;
1266 	struct sockaddr_storage addr;
1267 	int remote_id = remote->id;
1268 	int local_id = loc->id;
1269 	struct socket *sf;
1270 	struct sock *ssk;
1271 	u32 remote_token;
1272 	int addrlen;
1273 	int err;
1274 
1275 	if (!mptcp_is_fully_established(sk))
1276 		return -ENOTCONN;
1277 
1278 	err = mptcp_subflow_create_socket(sk, &sf);
1279 	if (err)
1280 		return err;
1281 
1282 	ssk = sf->sk;
1283 	subflow = mptcp_subflow_ctx(ssk);
1284 	do {
1285 		get_random_bytes(&subflow->local_nonce, sizeof(u32));
1286 	} while (!subflow->local_nonce);
1287 
1288 	if (!local_id) {
1289 		err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1290 		if (err < 0)
1291 			goto failed;
1292 
1293 		local_id = err;
1294 	}
1295 
1296 	subflow->remote_key = msk->remote_key;
1297 	subflow->local_key = msk->local_key;
1298 	subflow->token = msk->token;
1299 	mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1300 
1301 	addrlen = sizeof(struct sockaddr_in);
1302 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1303 	if (addr.ss_family == AF_INET6)
1304 		addrlen = sizeof(struct sockaddr_in6);
1305 #endif
1306 	ssk->sk_bound_dev_if = ifindex;
1307 	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1308 	if (err)
1309 		goto failed;
1310 
1311 	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1312 	pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1313 		 remote_token, local_id, remote_id);
1314 	subflow->remote_token = remote_token;
1315 	subflow->local_id = local_id;
1316 	subflow->remote_id = remote_id;
1317 	subflow->request_join = 1;
1318 	subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1319 	mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1320 
1321 	mptcp_add_pending_subflow(msk, subflow);
1322 	mptcp_sockopt_sync(msk, ssk);
1323 	err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1324 	if (err && err != -EINPROGRESS)
1325 		goto failed_unlink;
1326 
1327 	/* discard the subflow socket */
1328 	mptcp_sock_graft(ssk, sk->sk_socket);
1329 	iput(SOCK_INODE(sf));
1330 	return err;
1331 
1332 failed_unlink:
1333 	spin_lock_bh(&msk->join_list_lock);
1334 	list_del(&subflow->node);
1335 	spin_unlock_bh(&msk->join_list_lock);
1336 	sock_put(mptcp_subflow_tcp_sock(subflow));
1337 
1338 failed:
1339 	subflow->disposable = 1;
1340 	sock_release(sf);
1341 	return err;
1342 }
1343 
1344 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1345 {
1346 #ifdef CONFIG_SOCK_CGROUP_DATA
1347 	struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1348 				*child_skcd = &child->sk_cgrp_data;
1349 
1350 	/* only the additional subflows created by kworkers have to be modified */
1351 	if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1352 	    cgroup_id(sock_cgroup_ptr(child_skcd))) {
1353 #ifdef CONFIG_MEMCG
1354 		struct mem_cgroup *memcg = parent->sk_memcg;
1355 
1356 		mem_cgroup_sk_free(child);
1357 		if (memcg && css_tryget(&memcg->css))
1358 			child->sk_memcg = memcg;
1359 #endif /* CONFIG_MEMCG */
1360 
1361 		cgroup_sk_free(child_skcd);
1362 		*child_skcd = *parent_skcd;
1363 		cgroup_sk_clone(child_skcd);
1364 	}
1365 #endif /* CONFIG_SOCK_CGROUP_DATA */
1366 }
1367 
1368 static void mptcp_subflow_ops_override(struct sock *ssk)
1369 {
1370 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1371 	if (ssk->sk_prot == &tcpv6_prot)
1372 		ssk->sk_prot = &tcpv6_prot_override;
1373 	else
1374 #endif
1375 		ssk->sk_prot = &tcp_prot_override;
1376 }
1377 
1378 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1379 {
1380 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1381 	if (ssk->sk_prot == &tcpv6_prot_override)
1382 		ssk->sk_prot = &tcpv6_prot;
1383 	else
1384 #endif
1385 		ssk->sk_prot = &tcp_prot;
1386 }
1387 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1388 {
1389 	struct mptcp_subflow_context *subflow;
1390 	struct net *net = sock_net(sk);
1391 	struct socket *sf;
1392 	int err;
1393 
1394 	/* un-accepted server sockets can reach here - on bad configuration
1395 	 * bail early to avoid greater trouble later
1396 	 */
1397 	if (unlikely(!sk->sk_socket))
1398 		return -EINVAL;
1399 
1400 	err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1401 			       &sf);
1402 	if (err)
1403 		return err;
1404 
1405 	lock_sock(sf->sk);
1406 
1407 	/* the newly created socket has to be in the same cgroup as its parent */
1408 	mptcp_attach_cgroup(sk, sf->sk);
1409 
1410 	/* kernel sockets do not by default acquire net ref, but TCP timer
1411 	 * needs it.
1412 	 */
1413 	sf->sk->sk_net_refcnt = 1;
1414 	get_net(net);
1415 #ifdef CONFIG_PROC_FS
1416 	this_cpu_add(*net->core.sock_inuse, 1);
1417 #endif
1418 	err = tcp_set_ulp(sf->sk, "mptcp");
1419 	release_sock(sf->sk);
1420 
1421 	if (err) {
1422 		sock_release(sf);
1423 		return err;
1424 	}
1425 
1426 	/* the newly created socket really belongs to the owning MPTCP master
1427 	 * socket, even if for additional subflows the allocation is performed
1428 	 * by a kernel workqueue. Adjust inode references, so that the
1429 	 * procfs/diag interaces really show this one belonging to the correct
1430 	 * user.
1431 	 */
1432 	SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1433 	SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1434 	SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1435 
1436 	subflow = mptcp_subflow_ctx(sf->sk);
1437 	pr_debug("subflow=%p", subflow);
1438 
1439 	*new_sock = sf;
1440 	sock_hold(sk);
1441 	subflow->conn = sk;
1442 	mptcp_subflow_ops_override(sf->sk);
1443 
1444 	return 0;
1445 }
1446 
1447 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1448 							gfp_t priority)
1449 {
1450 	struct inet_connection_sock *icsk = inet_csk(sk);
1451 	struct mptcp_subflow_context *ctx;
1452 
1453 	ctx = kzalloc(sizeof(*ctx), priority);
1454 	if (!ctx)
1455 		return NULL;
1456 
1457 	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1458 	INIT_LIST_HEAD(&ctx->node);
1459 	INIT_LIST_HEAD(&ctx->delegated_node);
1460 
1461 	pr_debug("subflow=%p", ctx);
1462 
1463 	ctx->tcp_sock = sk;
1464 
1465 	return ctx;
1466 }
1467 
1468 static void __subflow_state_change(struct sock *sk)
1469 {
1470 	struct socket_wq *wq;
1471 
1472 	rcu_read_lock();
1473 	wq = rcu_dereference(sk->sk_wq);
1474 	if (skwq_has_sleeper(wq))
1475 		wake_up_interruptible_all(&wq->wait);
1476 	rcu_read_unlock();
1477 }
1478 
1479 static bool subflow_is_done(const struct sock *sk)
1480 {
1481 	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1482 }
1483 
1484 static void subflow_state_change(struct sock *sk)
1485 {
1486 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1487 	struct sock *parent = subflow->conn;
1488 
1489 	__subflow_state_change(sk);
1490 
1491 	if (subflow_simultaneous_connect(sk)) {
1492 		mptcp_propagate_sndbuf(parent, sk);
1493 		mptcp_do_fallback(sk);
1494 		mptcp_rcv_space_init(mptcp_sk(parent), sk);
1495 		pr_fallback(mptcp_sk(parent));
1496 		subflow->conn_finished = 1;
1497 		if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
1498 			inet_sk_state_store(parent, TCP_ESTABLISHED);
1499 			parent->sk_state_change(parent);
1500 		}
1501 	}
1502 
1503 	/* as recvmsg() does not acquire the subflow socket for ssk selection
1504 	 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1505 	 * the data available machinery here.
1506 	 */
1507 	if (mptcp_subflow_data_available(sk))
1508 		mptcp_data_ready(parent, sk);
1509 
1510 	subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1511 
1512 	if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1513 	    !subflow->rx_eof && subflow_is_done(sk)) {
1514 		subflow->rx_eof = 1;
1515 		mptcp_subflow_eof(parent);
1516 	}
1517 }
1518 
1519 static int subflow_ulp_init(struct sock *sk)
1520 {
1521 	struct inet_connection_sock *icsk = inet_csk(sk);
1522 	struct mptcp_subflow_context *ctx;
1523 	struct tcp_sock *tp = tcp_sk(sk);
1524 	int err = 0;
1525 
1526 	/* disallow attaching ULP to a socket unless it has been
1527 	 * created with sock_create_kern()
1528 	 */
1529 	if (!sk->sk_kern_sock) {
1530 		err = -EOPNOTSUPP;
1531 		goto out;
1532 	}
1533 
1534 	ctx = subflow_create_ctx(sk, GFP_KERNEL);
1535 	if (!ctx) {
1536 		err = -ENOMEM;
1537 		goto out;
1538 	}
1539 
1540 	pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1541 
1542 	tp->is_mptcp = 1;
1543 	ctx->icsk_af_ops = icsk->icsk_af_ops;
1544 	icsk->icsk_af_ops = subflow_default_af_ops(sk);
1545 	ctx->tcp_data_ready = sk->sk_data_ready;
1546 	ctx->tcp_state_change = sk->sk_state_change;
1547 	ctx->tcp_write_space = sk->sk_write_space;
1548 	ctx->tcp_error_report = sk->sk_error_report;
1549 	sk->sk_data_ready = subflow_data_ready;
1550 	sk->sk_write_space = subflow_write_space;
1551 	sk->sk_state_change = subflow_state_change;
1552 	sk->sk_error_report = subflow_error_report;
1553 out:
1554 	return err;
1555 }
1556 
1557 static void subflow_ulp_release(struct sock *ssk)
1558 {
1559 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1560 	bool release = true;
1561 	struct sock *sk;
1562 
1563 	if (!ctx)
1564 		return;
1565 
1566 	sk = ctx->conn;
1567 	if (sk) {
1568 		/* if the msk has been orphaned, keep the ctx
1569 		 * alive, will be freed by __mptcp_close_ssk(),
1570 		 * when the subflow is still unaccepted
1571 		 */
1572 		release = ctx->disposable || list_empty(&ctx->node);
1573 		sock_put(sk);
1574 	}
1575 
1576 	mptcp_subflow_ops_undo_override(ssk);
1577 	if (release)
1578 		kfree_rcu(ctx, rcu);
1579 }
1580 
1581 static void subflow_ulp_clone(const struct request_sock *req,
1582 			      struct sock *newsk,
1583 			      const gfp_t priority)
1584 {
1585 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1586 	struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1587 	struct mptcp_subflow_context *new_ctx;
1588 
1589 	if (!tcp_rsk(req)->is_mptcp ||
1590 	    (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1591 		subflow_ulp_fallback(newsk, old_ctx);
1592 		return;
1593 	}
1594 
1595 	new_ctx = subflow_create_ctx(newsk, priority);
1596 	if (!new_ctx) {
1597 		subflow_ulp_fallback(newsk, old_ctx);
1598 		return;
1599 	}
1600 
1601 	new_ctx->conn_finished = 1;
1602 	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1603 	new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1604 	new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1605 	new_ctx->tcp_write_space = old_ctx->tcp_write_space;
1606 	new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1607 	new_ctx->rel_write_seq = 1;
1608 	new_ctx->tcp_sock = newsk;
1609 
1610 	if (subflow_req->mp_capable) {
1611 		/* see comments in subflow_syn_recv_sock(), MPTCP connection
1612 		 * is fully established only after we receive the remote key
1613 		 */
1614 		new_ctx->mp_capable = 1;
1615 		new_ctx->local_key = subflow_req->local_key;
1616 		new_ctx->token = subflow_req->token;
1617 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1618 		new_ctx->idsn = subflow_req->idsn;
1619 	} else if (subflow_req->mp_join) {
1620 		new_ctx->ssn_offset = subflow_req->ssn_offset;
1621 		new_ctx->mp_join = 1;
1622 		new_ctx->fully_established = 1;
1623 		new_ctx->backup = subflow_req->backup;
1624 		new_ctx->local_id = subflow_req->local_id;
1625 		new_ctx->remote_id = subflow_req->remote_id;
1626 		new_ctx->token = subflow_req->token;
1627 		new_ctx->thmac = subflow_req->thmac;
1628 	}
1629 }
1630 
1631 static void tcp_release_cb_override(struct sock *ssk)
1632 {
1633 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1634 
1635 	if (mptcp_subflow_has_delegated_action(subflow))
1636 		mptcp_subflow_process_delegated(ssk);
1637 
1638 	tcp_release_cb(ssk);
1639 }
1640 
1641 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1642 	.name		= "mptcp",
1643 	.owner		= THIS_MODULE,
1644 	.init		= subflow_ulp_init,
1645 	.release	= subflow_ulp_release,
1646 	.clone		= subflow_ulp_clone,
1647 };
1648 
1649 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1650 {
1651 	subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1652 	subflow_ops->slab_name = "request_sock_subflow";
1653 
1654 	subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1655 					      subflow_ops->obj_size, 0,
1656 					      SLAB_ACCOUNT |
1657 					      SLAB_TYPESAFE_BY_RCU,
1658 					      NULL);
1659 	if (!subflow_ops->slab)
1660 		return -ENOMEM;
1661 
1662 	subflow_ops->destructor = subflow_req_destructor;
1663 
1664 	return 0;
1665 }
1666 
1667 void __init mptcp_subflow_init(void)
1668 {
1669 	mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1670 	if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1671 		panic("MPTCP: failed to init subflow request sock ops\n");
1672 
1673 	subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1674 	subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1675 
1676 	subflow_specific = ipv4_specific;
1677 	subflow_specific.conn_request = subflow_v4_conn_request;
1678 	subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1679 	subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1680 
1681 	tcp_prot_override = tcp_prot;
1682 	tcp_prot_override.release_cb = tcp_release_cb_override;
1683 
1684 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1685 	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1686 	subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1687 
1688 	subflow_v6_specific = ipv6_specific;
1689 	subflow_v6_specific.conn_request = subflow_v6_conn_request;
1690 	subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1691 	subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1692 
1693 	subflow_v6m_specific = subflow_v6_specific;
1694 	subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1695 	subflow_v6m_specific.send_check = ipv4_specific.send_check;
1696 	subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1697 	subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1698 	subflow_v6m_specific.net_frag_header_len = 0;
1699 
1700 	tcpv6_prot_override = tcpv6_prot;
1701 	tcpv6_prot_override.release_cb = tcp_release_cb_override;
1702 #endif
1703 
1704 	mptcp_diag_subflow_init(&subflow_ulp_ops);
1705 
1706 	if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1707 		panic("MPTCP: failed to register subflows to ULP\n");
1708 }
1709