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