subflow.c 45.6 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
/* Multipath TCP
 *
 * Copyright (c) 2017 - 2019, Intel Corporation.
 */

7 8
#define pr_fmt(fmt) "MPTCP: " fmt

9 10 11
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
12
#include <crypto/algapi.h>
13
#include <crypto/sha2.h>
14 15 16 17 18
#include <net/sock.h>
#include <net/inet_common.h>
#include <net/inet_hashtables.h>
#include <net/protocol.h>
#include <net/tcp.h>
19 20
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
#include <net/ip6_route.h>
P
Paolo Abeni 已提交
21
#include <net/transp_v6.h>
22
#endif
23
#include <net/mptcp.h>
24
#include <uapi/linux/mptcp.h>
25
#include "protocol.h"
26 27
#include "mib.h"

28 29
#include <trace/events/mptcp.h>

P
Paolo Abeni 已提交
30 31
static void mptcp_subflow_ops_undo_override(struct sock *ssk);

32 33 34 35 36
static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
				  enum linux_mptcp_mib_field field)
{
	MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
}
37

38 39 40 41 42 43
static void subflow_req_destructor(struct request_sock *req)
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);

	pr_debug("subflow_req=%p", subflow_req);

44 45 46
	if (subflow_req->msk)
		sock_put((struct sock *)subflow_req->msk);

P
Paolo Abeni 已提交
47
	mptcp_token_destroy_request(req);
48 49 50
	tcp_request_sock_ops.destructor(req);
}

51 52 53 54 55 56 57 58 59 60 61
static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
				  void *hmac)
{
	u8 msg[8];

	put_unaligned_be32(nonce1, &msg[0]);
	put_unaligned_be32(nonce2, &msg[4]);

	mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
}

62 63 64 65 66 67
static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
{
	return mptcp_is_fully_established((void *)msk) &&
	       READ_ONCE(msk->pm.accept_subflow);
}

68
/* validate received token and create truncated hmac and nonce for SYN-ACK */
69 70 71 72 73 74 75 76 77 78 79 80 81 82
static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
{
	struct mptcp_sock *msk = subflow_req->msk;
	u8 hmac[SHA256_DIGEST_SIZE];

	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));

	subflow_generate_hmac(msk->local_key, msk->remote_key,
			      subflow_req->local_nonce,
			      subflow_req->remote_nonce, hmac);

	subflow_req->thmac = get_unaligned_be64(hmac);
}

83
static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
84 85 86 87 88 89 90
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
	struct mptcp_sock *msk;
	int local_id;

	msk = mptcp_token_get_sock(subflow_req->token);
	if (!msk) {
91
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
92
		return NULL;
93 94 95 96 97
	}

	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
	if (local_id < 0) {
		sock_put((struct sock *)msk);
98
		return NULL;
99 100 101
	}
	subflow_req->local_id = local_id;

102
	return msk;
103 104
}

105
static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
106 107 108 109
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);

	subflow_req->mp_capable = 0;
110
	subflow_req->mp_join = 0;
111
	subflow_req->msk = NULL;
P
Paolo Abeni 已提交
112
	mptcp_token_init_request(req);
113 114
}

115 116 117 118 119
static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
{
	return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
}

120 121 122 123 124 125 126 127 128 129
static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
{
	struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);

	if (mpext) {
		memset(mpext, 0, sizeof(*mpext));
		mpext->reset_reason = reason;
	}
}

130 131 132 133 134
/* Init mptcp request socket.
 *
 * Returns an error code if a JOIN has failed and a TCP reset
 * should be sent.
 */
135 136 137
static int subflow_check_req(struct request_sock *req,
			     const struct sock *sk_listener,
			     struct sk_buff *skb)
138 139 140 141 142 143 144
{
	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
	struct mptcp_options_received mp_opt;

	pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);

145 146 147 148 149 150 151
#ifdef CONFIG_TCP_MD5SIG
	/* no MPTCP if MD5SIG is enabled on this socket or we may run out of
	 * TCP option space.
	 */
	if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
		return -EINVAL;
#endif
152 153 154

	mptcp_get_options(skb, &mp_opt);

155
	if (mp_opt.mp_capable) {
156 157
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);

158
		if (mp_opt.mp_join)
159
			return 0;
160
	} else if (mp_opt.mp_join) {
161 162
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
	}
163

164
	if (mp_opt.mp_capable && listener->request_mptcp) {
165 166
		int err, retries = 4;

167
		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
168 169 170 171
again:
		do {
			get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
		} while (subflow_req->local_key == 0);
172

173 174 175 176 177 178 179
		if (unlikely(req->syncookie)) {
			mptcp_crypto_key_sha(subflow_req->local_key,
					     &subflow_req->token,
					     &subflow_req->idsn);
			if (mptcp_token_exists(subflow_req->token)) {
				if (retries-- > 0)
					goto again;
180
				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
181 182 183
			} else {
				subflow_req->mp_capable = 1;
			}
184
			return 0;
185 186
		}

187 188 189
		err = mptcp_token_new_request(req);
		if (err == 0)
			subflow_req->mp_capable = 1;
190 191
		else if (retries-- > 0)
			goto again;
192 193
		else
			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 199 200 201
		subflow_req->backup = mp_opt.backup;
		subflow_req->remote_id = mp_opt.join_id;
		subflow_req->token = mp_opt.token;
		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 206
		if (!subflow_req->msk) {
			subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
207
			return -EPERM;
208
		}
209

210 211 212 213 214 215 216 217 218 219
		if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
			pr_debug("syn inet_sport=%d %d",
				 ntohs(inet_sk(sk_listener)->inet_sport),
				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
				sock_put((struct sock *)subflow_req->msk);
				mptcp_token_destroy_request(req);
				tcp_request_sock_ops.destructor(req);
				subflow_req->msk = NULL;
				subflow_req->mp_join = 0;
220
				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
221 222
				return -EPERM;
			}
223
			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
224 225
		}

226 227
		subflow_req_create_thmac(subflow_req);

228
		if (unlikely(req->syncookie)) {
229 230 231 232
			if (mptcp_can_accept_new_subflow(subflow_req->msk))
				subflow_init_req_cookie_join_save(subflow_req, skb);
		}

233 234
		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
			 subflow_req->remote_nonce, subflow_req->msk);
235
	}
236 237

	return 0;
238 239
}

240 241 242 243 244 245 246 247 248
int mptcp_subflow_init_cookie_req(struct request_sock *req,
				  const struct sock *sk_listener,
				  struct sk_buff *skb)
{
	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
	struct mptcp_options_received mp_opt;
	int err;

249
	subflow_init_req(req, sk_listener);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	mptcp_get_options(skb, &mp_opt);

	if (mp_opt.mp_capable && mp_opt.mp_join)
		return -EINVAL;

	if (mp_opt.mp_capable && listener->request_mptcp) {
		if (mp_opt.sndr_key == 0)
			return -EINVAL;

		subflow_req->local_key = mp_opt.rcvr_key;
		err = mptcp_token_new_request(req);
		if (err)
			return err;

		subflow_req->mp_capable = 1;
		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
266 267 268 269 270 271 272 273
	} else if (mp_opt.mp_join && listener->request_mptcp) {
		if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
			return -EINVAL;

		if (mptcp_can_accept_new_subflow(subflow_req->msk))
			subflow_req->mp_join = 1;

		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
274 275 276 277 278 279
	}

	return 0;
}
EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);

280 281 282 283
static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
					      struct sk_buff *skb,
					      struct flowi *fl,
					      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 292 293
	dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
	if (!dst)
		return NULL;
294

295
	err = subflow_check_req(req, sk, skb);
296 297
	if (err == 0)
		return dst;
298

299 300 301 302
	dst_release(dst);
	if (!req->syncookie)
		tcp_request_sock_ops.send_reset(sk, skb);
	return NULL;
303 304 305
}

#if IS_ENABLED(CONFIG_MPTCP_IPV6)
306 307 308 309
static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
					      struct sk_buff *skb,
					      struct flowi *fl,
					      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 318 319
	dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
	if (!dst)
		return NULL;
320

321
	err = subflow_check_req(req, sk, skb);
322 323 324 325 326 327 328
	if (err == 0)
		return dst;

	dst_release(dst);
	if (!req->syncookie)
		tcp6_request_sock_ops.send_reset(sk, skb);
	return NULL;
329 330 331
}
#endif

332 333 334
/* validate received truncated hmac and create hmac for third ACK */
static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
{
335
	u8 hmac[SHA256_DIGEST_SIZE];
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	u64 thmac;

	subflow_generate_hmac(subflow->remote_key, subflow->local_key,
			      subflow->remote_nonce, subflow->local_nonce,
			      hmac);

	thmac = get_unaligned_be64(hmac);
	pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
		 subflow, subflow->token,
		 (unsigned long long)thmac,
		 (unsigned long long)subflow->thmac);

	return thmac == subflow->thmac;
}

351 352
void mptcp_subflow_reset(struct sock *ssk)
{
P
Paolo Abeni 已提交
353 354 355
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	struct sock *sk = subflow->conn;

356 357 358
	/* must hold: tcp_done() could drop last reference on parent */
	sock_hold(sk);

359 360 361
	tcp_set_state(ssk, TCP_CLOSE);
	tcp_send_active_reset(ssk, GFP_ATOMIC);
	tcp_done(ssk);
P
Paolo Abeni 已提交
362 363
	if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
	    schedule_work(&mptcp_sk(sk)->work))
364 365 366
		return; /* worker will put sk for us */

	sock_put(sk);
367 368
}

369 370 371 372 373
static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
{
	return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
void __mptcp_set_connected(struct sock *sk)
{
	if (sk->sk_state == TCP_SYN_SENT) {
		inet_sk_state_store(sk, TCP_ESTABLISHED);
		sk->sk_state_change(sk);
	}
}

static void mptcp_set_connected(struct sock *sk)
{
	mptcp_data_lock(sk);
	if (!sock_owned_by_user(sk))
		__mptcp_set_connected(sk);
	else
		set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags);
	mptcp_data_unlock(sk);
}

392 393 394
static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
395
	struct mptcp_options_received mp_opt;
396
	struct sock *parent = subflow->conn;
397 398 399

	subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);

400

401 402 403 404
	/* be sure no special action on any packet other than syn-ack */
	if (subflow->conn_finished)
		return;

P
Paolo Abeni 已提交
405
	mptcp_propagate_sndbuf(parent, sk);
406
	subflow->rel_write_seq = 1;
407
	subflow->conn_finished = 1;
408 409
	subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
	pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
410

411
	mptcp_get_options(skb, &mp_opt);
412 413 414 415 416 417 418 419 420
	if (subflow->request_mptcp) {
		if (!mp_opt.mp_capable) {
			MPTCP_INC_STATS(sock_net(sk),
					MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
			mptcp_do_fallback(sk);
			pr_fallback(mptcp_sk(subflow->conn));
			goto fallback;
		}

421 422
		subflow->mp_capable = 1;
		subflow->can_ack = 1;
423
		subflow->remote_key = mp_opt.sndr_key;
424 425
		pr_debug("subflow=%p, remote_key=%llu", subflow,
			 subflow->remote_key);
P
Paolo Abeni 已提交
426
		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
427
		mptcp_finish_connect(sk);
428
		mptcp_set_connected(parent);
429 430 431
	} else if (subflow->request_join) {
		u8 hmac[SHA256_DIGEST_SIZE];

432 433
		if (!mp_opt.mp_join) {
			subflow->reset_reason = MPTCP_RST_EMPTCP;
434
			goto do_reset;
435
		}
436

437 438
		subflow->thmac = mp_opt.thmac;
		subflow->remote_nonce = mp_opt.nonce;
439 440 441
		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
			 subflow->thmac, subflow->remote_nonce);

442
		if (!subflow_thmac_valid(subflow)) {
443
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
444
			subflow->reset_reason = MPTCP_RST_EMPTCP;
445 446 447 448 449 450
			goto do_reset;
		}

		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
				      subflow->local_nonce,
				      subflow->remote_nonce,
451 452
				      hmac);
		memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
453 454 455 456

		if (!mptcp_finish_join(sk))
			goto do_reset;

457
		subflow->mp_join = 1;
458
		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
459 460 461 462 463

		if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
			pr_debug("synack inet_dport=%d %d",
				 ntohs(inet_sk(sk)->inet_dport),
				 ntohs(inet_sk(parent)->inet_dport));
464
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
465
		}
466 467 468
	} else if (mptcp_check_fallback(sk)) {
fallback:
		mptcp_rcv_space_init(mptcp_sk(parent), sk);
469
		mptcp_set_connected(parent);
470
	}
471 472 473
	return;

do_reset:
474
	subflow->reset_transient = 0;
475
	mptcp_subflow_reset(sk);
476 477
}

478 479
struct request_sock_ops mptcp_subflow_request_sock_ops;
EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
480 481 482 483 484 485 486 487 488 489 490 491
static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;

static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

	pr_debug("subflow=%p", subflow);

	/* Never answer to SYNs sent to broadcast or multicast */
	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
		goto drop;

492
	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
493 494 495 496 497 498 499 500 501 502 503
				&subflow_request_sock_ipv4_ops,
				sk, skb);
drop:
	tcp_listendrop(sk);
	return 0;
}

#if IS_ENABLED(CONFIG_MPTCP_IPV6)
static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
static struct inet_connection_sock_af_ops subflow_v6_specific;
static struct inet_connection_sock_af_ops subflow_v6m_specific;
P
Paolo Abeni 已提交
504
static struct proto tcpv6_prot_override;
505 506 507 508 509 510 511 512 513 514 515 516 517

static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

	pr_debug("subflow=%p", subflow);

	if (skb->protocol == htons(ETH_P_IP))
		return subflow_v4_conn_request(sk, skb);

	if (!ipv6_unicast_destination(skb))
		goto drop;

518 519 520 521 522
	if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
		__IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
		return 0;
	}

523
	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
524 525 526 527 528 529 530 531
				&subflow_request_sock_ipv6_ops, sk, skb);

drop:
	tcp_listendrop(sk);
	return 0; /* don't send reset */
}
#endif

532 533
/* validate hmac received in third ACK */
static bool subflow_hmac_valid(const struct request_sock *req,
534
			       const struct mptcp_options_received *mp_opt)
535 536
{
	const struct mptcp_subflow_request_sock *subflow_req;
537
	u8 hmac[SHA256_DIGEST_SIZE];
538 539 540
	struct mptcp_sock *msk;

	subflow_req = mptcp_subflow_rsk(req);
541
	msk = subflow_req->msk;
542 543 544 545 546 547 548
	if (!msk)
		return false;

	subflow_generate_hmac(msk->remote_key, msk->local_key,
			      subflow_req->remote_nonce,
			      subflow_req->local_nonce, hmac);

549
	return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
550 551
}

552 553 554 555 556 557 558 559 560
static void mptcp_sock_destruct(struct sock *sk)
{
	/* if new mptcp socket isn't accepted, it is free'd
	 * from the tcp listener sockets request queue, linked
	 * from req->sk.  The tcp socket is released.
	 * This calls the ULP release function which will
	 * also remove the mptcp socket, via
	 * sock_put(ctx->conn).
	 *
561 562
	 * Problem is that the mptcp socket will be in
	 * ESTABLISHED state and will not have the SOCK_DEAD flag.
563 564
	 * Both result in warnings from inet_sock_destruct.
	 */
565
	if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
566 567 568 569 570
		sk->sk_state = TCP_CLOSE;
		WARN_ON_ONCE(sk->sk_socket);
		sock_orphan(sk);
	}

571
	mptcp_destroy_common(mptcp_sk(sk));
572 573 574
	inet_sock_destruct(sk);
}

575 576
static void mptcp_force_close(struct sock *sk)
{
577
	/* the msk is not yet exposed to user-space */
578 579 580 581
	inet_sk_state_store(sk, TCP_CLOSE);
	sk_common_release(sk);
}

582 583 584 585 586 587 588 589 590
static void subflow_ulp_fallback(struct sock *sk,
				 struct mptcp_subflow_context *old_ctx)
{
	struct inet_connection_sock *icsk = inet_csk(sk);

	mptcp_subflow_tcp_fallback(sk, old_ctx);
	icsk->icsk_ulp_ops = NULL;
	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
	tcp_sk(sk)->is_mptcp = 0;
P
Paolo Abeni 已提交
591 592

	mptcp_subflow_ops_undo_override(sk);
593 594
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608
static void subflow_drop_ctx(struct sock *ssk)
{
	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);

	if (!ctx)
		return;

	subflow_ulp_fallback(ssk, ctx);
	if (ctx->conn)
		sock_put(ctx->conn);

	kfree_rcu(ctx, rcu);
}

609 610 611 612 613 614 615 616 617 618 619
void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
				     struct mptcp_options_received *mp_opt)
{
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);

	subflow->remote_key = mp_opt->sndr_key;
	subflow->fully_established = 1;
	subflow->can_ack = 1;
	WRITE_ONCE(msk->fully_established, true);
}

620 621 622 623 624 625 626 627
static struct sock *subflow_syn_recv_sock(const struct sock *sk,
					  struct sk_buff *skb,
					  struct request_sock *req,
					  struct dst_entry *dst,
					  struct request_sock *req_unhash,
					  bool *own_req)
{
	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
628
	struct mptcp_subflow_request_sock *subflow_req;
629
	struct mptcp_options_received mp_opt;
630
	bool fallback, fallback_is_fatal;
P
Paolo Abeni 已提交
631
	struct sock *new_msk = NULL;
632 633 634 635
	struct sock *child;

	pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);

636 637
	/* After child creation we must look for 'mp_capable' even when options
	 * are not parsed
638 639
	 */
	mp_opt.mp_capable = 0;
640 641 642

	/* hopefully temporary handling for MP_JOIN+syncookie */
	subflow_req = mptcp_subflow_rsk(req);
643
	fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
644 645
	fallback = !tcp_rsk(req)->is_mptcp;
	if (fallback)
646 647
		goto create_child;

648
	/* if the sk is MP_CAPABLE, we try to fetch the client key */
649
	if (subflow_req->mp_capable) {
650 651 652 653 654 655 656 657
		/* we can receive and accept an in-window, out-of-order pkt,
		 * which may not carry the MP_CAPABLE opt even on mptcp enabled
		 * paths: always try to extract the peer key, and fallback
		 * for packets missing it.
		 * Even OoO DSS packets coming legitly after dropped or
		 * reordered MPC will cause fallback, but we don't have other
		 * options.
		 */
658 659
		mptcp_get_options(skb, &mp_opt);
		if (!mp_opt.mp_capable) {
660
			fallback = true;
P
Paolo Abeni 已提交
661
			goto create_child;
662
		}
P
Paolo Abeni 已提交
663

664
		new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
P
Paolo Abeni 已提交
665
		if (!new_msk)
666
			fallback = true;
667
	} else if (subflow_req->mp_join) {
668
		mptcp_get_options(skb, &mp_opt);
669 670
		if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
		    !mptcp_can_accept_new_subflow(subflow_req->msk)) {
671
			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
672
			fallback = true;
673
		}
674
	}
675

676
create_child:
677 678 679 680
	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
						     req_unhash, own_req);

	if (child && *own_req) {
681 682
		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);

683 684
		tcp_rsk(req)->drop_req = false;

685 686 687
		/* we need to fallback on ctx allocation failure and on pre-reqs
		 * checking above. In the latter scenario we additionally need
		 * to reset the context to non MPTCP status.
688
		 */
689
		if (!ctx || fallback) {
690 691
			if (fallback_is_fatal) {
				subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
692
				goto dispose_child;
693
			}
694

695
			subflow_drop_ctx(child);
P
Paolo Abeni 已提交
696
			goto out;
697
		}
698

699 700 701
		/* ssk inherits options of listener sk */
		ctx->setsockopt_seq = listener->setsockopt_seq;

702
		if (ctx->mp_capable) {
703 704 705 706 707
			/* this can't race with mptcp_close(), as the msk is
			 * not yet exposted to user-space
			 */
			inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);

708 709 710
			/* record the newly created socket as the first msk
			 * subflow, but don't link it yet into conn_list
			 */
711 712
			WRITE_ONCE(mptcp_sk(new_msk)->first, child);

P
Paolo Abeni 已提交
713 714 715
			/* new mpc subflow takes ownership of the newly
			 * created mptcp socket
			 */
716
			new_msk->sk_destruct = mptcp_sock_destruct;
717
			mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
718
			mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
P
Paolo Abeni 已提交
719
			mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
P
Paolo Abeni 已提交
720 721
			ctx->conn = new_msk;
			new_msk = NULL;
722 723 724 725

			/* with OoO packets we can reach here without ingress
			 * mpc option
			 */
726 727
			if (mp_opt.mp_capable)
				mptcp_subflow_fully_established(ctx, &mp_opt);
728 729 730
		} else if (ctx->mp_join) {
			struct mptcp_sock *owner;

731
			owner = subflow_req->msk;
732 733
			if (!owner) {
				subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
734
				goto dispose_child;
735
			}
736

737 738
			/* move the msk reference ownership to the subflow */
			subflow_req->msk = NULL;
739
			ctx->conn = (struct sock *)owner;
740 741 742 743 744

			if (subflow_use_different_sport(owner, sk)) {
				pr_debug("ack inet_sport=%d %d",
					 ntohs(inet_sk(sk)->inet_sport),
					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
745 746
				if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
					SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
747
					goto dispose_child;
748 749
				}
				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
750
			}
751 752 753 754 755 756

			if (!mptcp_finish_join(child))
				goto dispose_child;

			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
			tcp_rsk(req)->drop_req = true;
757 758 759
		}
	}

P
Paolo Abeni 已提交
760 761 762
out:
	/* dispose of the left over mptcp master, if any */
	if (unlikely(new_msk))
763
		mptcp_force_close(new_msk);
764 765 766 767

	/* check for expected invariant - should never trigger, just help
	 * catching eariler subtle bugs
	 */
768
	WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
769 770
		     (!mptcp_subflow_ctx(child) ||
		      !mptcp_subflow_ctx(child)->conn));
771
	return child;
772

773
dispose_child:
774
	subflow_drop_ctx(child);
775 776
	tcp_rsk(req)->drop_req = true;
	inet_csk_prepare_for_destroy_sock(child);
777
	tcp_done(child);
P
Paolo Abeni 已提交
778
	req->rsk_ops->send_reset(sk, skb);
779 780 781

	/* The last child reference will be released by the caller */
	return child;
782 783 784
}

static struct inet_connection_sock_af_ops subflow_specific;
P
Paolo Abeni 已提交
785
static struct proto tcp_prot_override;
786

787 788 789 790
enum mapping_status {
	MAPPING_OK,
	MAPPING_INVALID,
	MAPPING_EMPTY,
791 792
	MAPPING_DATA_FIN,
	MAPPING_DUMMY
793 794
};

795
static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
796
{
797 798
	pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
		 ssn, subflow->map_subflow_seq, subflow->map_data_len);
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
}

static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	unsigned int skb_consumed;

	skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
	if (WARN_ON_ONCE(skb_consumed >= skb->len))
		return true;

	return skb->len - skb_consumed <= subflow->map_data_len -
					  mptcp_subflow_get_map_offset(subflow);
}

static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;

	if (unlikely(before(ssn, subflow->map_subflow_seq))) {
		/* Mapping covers data later in the subflow stream,
		 * currently unsupported.
		 */
823
		dbg_bad_map(subflow, ssn);
824 825 826 827 828
		return false;
	}
	if (unlikely(!before(ssn, subflow->map_subflow_seq +
				  subflow->map_data_len))) {
		/* Mapping does covers past subflow data, invalid */
829
		dbg_bad_map(subflow, ssn);
830 831 832 833 834
		return false;
	}
	return true;
}

835 836
static enum mapping_status get_mapping_status(struct sock *ssk,
					      struct mptcp_sock *msk)
837 838 839 840 841 842 843 844 845 846 847
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	struct mptcp_ext *mpext;
	struct sk_buff *skb;
	u16 data_len;
	u64 map_seq;

	skb = skb_peek(&ssk->sk_receive_queue);
	if (!skb)
		return MAPPING_EMPTY;

848 849 850
	if (mptcp_check_fallback(ssk))
		return MAPPING_DUMMY;

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	mpext = mptcp_get_ext(skb);
	if (!mpext || !mpext->use_map) {
		if (!subflow->map_valid && !skb->len) {
			/* the TCP stack deliver 0 len FIN pkt to the receive
			 * queue, that is the only 0len pkts ever expected here,
			 * and we can admit no mapping only for 0 len pkts
			 */
			if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
				WARN_ONCE(1, "0len seq %d:%d flags %x",
					  TCP_SKB_CB(skb)->seq,
					  TCP_SKB_CB(skb)->end_seq,
					  TCP_SKB_CB(skb)->tcp_flags);
			sk_eat_skb(ssk, skb);
			return MAPPING_EMPTY;
		}

		if (!subflow->map_valid)
			return MAPPING_INVALID;

		goto validate_seq;
	}

873
	trace_get_mapping_status(mpext);
874 875 876

	data_len = mpext->data_len;
	if (data_len == 0) {
877
		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
878 879 880 881 882
		return MAPPING_INVALID;
	}

	if (mpext->data_fin == 1) {
		if (data_len == 1) {
883 884
			bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
								 mpext->dsn64);
885
			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
886 887 888 889 890 891 892 893 894
			if (subflow->map_valid) {
				/* A DATA_FIN might arrive in a DSS
				 * option before the previous mapping
				 * has been fully consumed. Continue
				 * handling the existing mapping.
				 */
				skb_ext_del(skb, SKB_EXT_MPTCP);
				return MAPPING_OK;
			} else {
895 896 897
				if (updated && schedule_work(&msk->work))
					sock_hold((struct sock *)msk);

898 899
				return MAPPING_DATA_FIN;
			}
900
		} else {
P
Paolo Abeni 已提交
901
			u64 data_fin_seq = mpext->data_seq + data_len - 1;
902 903 904 905 906 907 908 909 910 911

			/* If mpext->data_seq is a 32-bit value, data_fin_seq
			 * must also be limited to 32 bits.
			 */
			if (!mpext->dsn64)
				data_fin_seq &= GENMASK_ULL(31, 0);

			mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
			pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
				 data_fin_seq, mpext->dsn64);
912 913 914 915 916 917
		}

		/* Adjust for DATA_FIN using 1 byte of sequence space */
		data_len--;
	}

P
Paolo Abeni 已提交
918
	map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
919
	WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
920 921 922 923 924 925 926 927 928 929 930 931 932

	if (subflow->map_valid) {
		/* Allow replacing only with an identical map */
		if (subflow->map_seq == map_seq &&
		    subflow->map_subflow_seq == mpext->subflow_seq &&
		    subflow->map_data_len == data_len) {
			skb_ext_del(skb, SKB_EXT_MPTCP);
			return MAPPING_OK;
		}

		/* If this skb data are fully covered by the current mapping,
		 * the new map would need caching, which is not supported
		 */
933 934
		if (skb_is_fully_mapped(ssk, skb)) {
			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
935
			return MAPPING_INVALID;
936
		}
937 938 939 940 941 942 943 944 945

		/* will validate the next map after consuming the current one */
		return MAPPING_OK;
	}

	subflow->map_seq = map_seq;
	subflow->map_subflow_seq = mpext->subflow_seq;
	subflow->map_data_len = data_len;
	subflow->map_valid = 1;
946
	subflow->mpc_map = mpext->mpc_map;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
		 subflow->map_seq, subflow->map_subflow_seq,
		 subflow->map_data_len);

validate_seq:
	/* we revalidate valid mapping on new skb, because we must ensure
	 * the current skb is completely covered by the available mapping
	 */
	if (!validate_mapping(ssk, skb))
		return MAPPING_INVALID;

	skb_ext_del(skb, SKB_EXT_MPTCP);
	return MAPPING_OK;
}

962
static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
963
				       u64 limit)
964 965
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
966 967 968 969 970 971 972
	bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
	u32 incr;

	incr = limit >= skb->len ? skb->len + fin : limit;

	pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
		 subflow->map_subflow_seq);
P
Paolo Abeni 已提交
973
	MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
974 975 976 977 978
	tcp_sk(ssk)->copied_seq += incr;
	if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
		sk_eat_skb(ssk, skb);
	if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
		subflow->map_valid = 0;
979 980
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
/* sched mptcp worker to remove the subflow if no more data is pending */
static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
{
	struct sock *sk = (struct sock *)msk;

	if (likely(ssk->sk_state != TCP_CLOSE))
		return;

	if (skb_queue_empty(&ssk->sk_receive_queue) &&
	    !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
		sock_hold(sk);
		if (!schedule_work(&msk->work))
			sock_put(sk);
	}
}

997 998 999 1000 1001 1002 1003
static bool subflow_check_data_avail(struct sock *ssk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	enum mapping_status status;
	struct mptcp_sock *msk;
	struct sk_buff *skb;

1004
	if (!skb_peek(&ssk->sk_receive_queue))
1005
		WRITE_ONCE(subflow->data_avail, 0);
1006 1007 1008 1009 1010 1011 1012 1013
	if (subflow->data_avail)
		return true;

	msk = mptcp_sk(subflow->conn);
	for (;;) {
		u64 ack_seq;
		u64 old_ack;

1014
		status = get_mapping_status(ssk, msk);
1015
		trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1016 1017 1018 1019 1020
		if (unlikely(status == MAPPING_INVALID))
			goto fallback;

		if (unlikely(status == MAPPING_DUMMY))
			goto fallback;
1021 1022

		if (status != MAPPING_OK)
1023
			goto no_data;
1024 1025 1026

		skb = skb_peek(&ssk->sk_receive_queue);
		if (WARN_ON_ONCE(!skb))
1027
			goto no_data;
1028

1029 1030 1031 1032
		/* if msk lacks the remote key, this subflow must provide an
		 * MP_CAPABLE-based mapping
		 */
		if (unlikely(!READ_ONCE(msk->can_ack))) {
1033 1034
			if (!subflow->mpc_map)
				goto fallback;
1035 1036 1037 1038 1039
			WRITE_ONCE(msk->remote_key, subflow->remote_key);
			WRITE_ONCE(msk->ack_seq, subflow->map_seq);
			WRITE_ONCE(msk->can_ack, true);
		}

1040 1041 1042 1043
		old_ack = READ_ONCE(msk->ack_seq);
		ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
		pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
			 ack_seq);
1044 1045 1046
		if (unlikely(before64(ack_seq, old_ack))) {
			mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
			continue;
1047
		}
1048

1049 1050
		WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
		break;
1051 1052 1053
	}
	return true;

1054 1055 1056
no_data:
	subflow_sched_work_if_closed(msk, ssk);
	return false;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

fallback:
	/* RFC 8684 section 3.7. */
	if (subflow->mp_join || subflow->fully_established) {
		/* fatal protocol error, close the socket.
		 * subflow_error_report() will introduce the appropriate barriers
		 */
		ssk->sk_err = EBADMSG;
		tcp_set_state(ssk, TCP_CLOSE);
		subflow->reset_transient = 0;
		subflow->reset_reason = MPTCP_RST_EMPTCP;
		tcp_send_active_reset(ssk, GFP_ATOMIC);
1069
		WRITE_ONCE(subflow->data_avail, 0);
1070 1071 1072 1073 1074 1075 1076 1077 1078
		return false;
	}

	__mptcp_do_fallback(msk);
	skb = skb_peek(&ssk->sk_receive_queue);
	subflow->map_valid = 1;
	subflow->map_seq = READ_ONCE(msk->ack_seq);
	subflow->map_data_len = skb->len;
	subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1079
	WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1080
	return true;
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
}

bool mptcp_subflow_data_available(struct sock *sk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

	/* check if current mapping is still valid */
	if (subflow->map_valid &&
	    mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
		subflow->map_valid = 0;
1091
		WRITE_ONCE(subflow->data_avail, 0);
1092 1093 1094 1095 1096 1097

		pr_debug("Done with mapping: seq=%u data_len=%u",
			 subflow->map_subflow_seq,
			 subflow->map_data_len);
	}

1098
	return subflow_check_data_avail(sk);
1099 1100
}

1101 1102 1103 1104 1105 1106
/* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
 * not the ssk one.
 *
 * In mptcp, rwin is about the mptcp-level connection data.
 *
 * Data that is still on the ssk rx queue can thus be ignored,
B
Bhaskar Chowdhury 已提交
1107
 * as far as mptcp peer is concerned that data is still inflight.
1108 1109 1110 1111 1112 1113 1114
 * DSS ACK is updated when skb is moved to the mptcp rx queue.
 */
void mptcp_space(const struct sock *ssk, int *space, int *full_space)
{
	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	const struct sock *sk = subflow->conn;

1115
	*space = __mptcp_space(sk);
1116 1117 1118
	*full_space = tcp_full_space(sk);
}

P
Paolo Abeni 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
void __mptcp_error_report(struct sock *sk)
{
	struct mptcp_subflow_context *subflow;
	struct mptcp_sock *msk = mptcp_sk(sk);

	mptcp_for_each_subflow(msk, subflow) {
		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
		int err = sock_error(ssk);

		if (!err)
			continue;

		/* only propagate errors on fallen-back sockets or
		 * on MPC connect
		 */
		if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
			continue;

		inet_sk_state_store(sk, inet_sk_state_load(ssk));
		sk->sk_err = -err;

		/* This barrier is coupled with smp_rmb() in mptcp_poll() */
		smp_wmb();
		sk->sk_error_report(sk);
		break;
	}
}

static void subflow_error_report(struct sock *ssk)
{
	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;

	mptcp_data_lock(sk);
	if (!sock_owned_by_user(sk))
		__mptcp_error_report(sk);
	else
		set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->flags);
	mptcp_data_unlock(sk);
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
static void subflow_data_ready(struct sock *sk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
	u16 state = 1 << inet_sk_state_load(sk);
	struct sock *parent = subflow->conn;
	struct mptcp_sock *msk;

	msk = mptcp_sk(parent);
	if (state & TCPF_LISTEN) {
		/* MPJ subflow are removed from accept queue before reaching here,
		 * avoid stray wakeups
		 */
		if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
			return;

		set_bit(MPTCP_DATA_READY, &msk->flags);
		parent->sk_data_ready(parent);
		return;
	}

	WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
		     !subflow->mp_join && !(state & TCPF_CLOSE));

	if (mptcp_subflow_data_available(sk))
		mptcp_data_ready(parent, sk);
	else if (unlikely(sk->sk_err))
		subflow_error_report(sk);
}

static void subflow_write_space(struct sock *ssk)
{
	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;

	mptcp_propagate_sndbuf(sk, ssk);
	mptcp_write_space(sk);
}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
static struct inet_connection_sock_af_ops *
subflow_default_af_ops(struct sock *sk)
{
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	if (sk->sk_family == AF_INET6)
		return &subflow_v6_specific;
#endif
	return &subflow_specific;
}

#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1207 1208
void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
{
1209 1210 1211 1212 1213 1214 1215
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
	struct inet_connection_sock *icsk = inet_csk(sk);
	struct inet_connection_sock_af_ops *target;

	target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);

	pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
M
Mat Martineau 已提交
1216
		 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1217 1218 1219 1220 1221 1222 1223

	if (likely(icsk->icsk_af_ops == target))
		return;

	subflow->icsk_af_ops = icsk->icsk_af_ops;
	icsk->icsk_af_ops = target;
}
1224
#endif
1225

1226 1227 1228
void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
			 struct sockaddr_storage *addr,
			 unsigned short family)
1229 1230
{
	memset(addr, 0, sizeof(*addr));
1231
	addr->ss_family = family;
1232 1233 1234
	if (addr->ss_family == AF_INET) {
		struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;

1235 1236 1237 1238 1239 1240
		if (info->family == AF_INET)
			in_addr->sin_addr = info->addr;
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
		else if (ipv6_addr_v4mapped(&info->addr6))
			in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
#endif
1241 1242 1243 1244 1245 1246
		in_addr->sin_port = info->port;
	}
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	else if (addr->ss_family == AF_INET6) {
		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;

1247 1248 1249 1250 1251
		if (info->family == AF_INET)
			ipv6_addr_set_v4mapped(info->addr.s_addr,
					       &in6_addr->sin6_addr);
		else
			in6_addr->sin6_addr = info->addr6;
1252 1253 1254 1255 1256
		in6_addr->sin6_port = info->port;
	}
#endif
}

1257
int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1258 1259
			    const struct mptcp_addr_info *remote,
			    u8 flags, int ifindex)
1260 1261 1262 1263
{
	struct mptcp_sock *msk = mptcp_sk(sk);
	struct mptcp_subflow_context *subflow;
	struct sockaddr_storage addr;
1264
	int remote_id = remote->id;
1265
	int local_id = loc->id;
1266
	struct socket *sf;
1267
	struct sock *ssk;
1268 1269 1270 1271
	u32 remote_token;
	int addrlen;
	int err;

1272
	if (!mptcp_is_fully_established(sk))
1273 1274 1275 1276 1277 1278
		return -ENOTCONN;

	err = mptcp_subflow_create_socket(sk, &sf);
	if (err)
		return err;

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
	ssk = sf->sk;
	subflow = mptcp_subflow_ctx(ssk);
	do {
		get_random_bytes(&subflow->local_nonce, sizeof(u32));
	} while (!subflow->local_nonce);

	if (!local_id) {
		err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
		if (err < 0)
			goto failed;

		local_id = err;
	}

1293 1294 1295
	subflow->remote_key = msk->remote_key;
	subflow->local_key = msk->local_key;
	subflow->token = msk->token;
1296
	mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1297 1298 1299

	addrlen = sizeof(struct sockaddr_in);
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1300
	if (addr.ss_family == AF_INET6)
1301 1302
		addrlen = sizeof(struct sockaddr_in6);
#endif
1303
	ssk->sk_bound_dev_if = ifindex;
1304 1305 1306 1307 1308
	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
	if (err)
		goto failed;

	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1309 1310
	pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
		 remote_token, local_id, remote_id);
1311
	subflow->remote_token = remote_token;
1312
	subflow->local_id = local_id;
1313
	subflow->remote_id = remote_id;
1314
	subflow->request_join = 1;
1315
	subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1316
	mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1317

1318
	mptcp_add_pending_subflow(msk, subflow);
1319
	mptcp_sockopt_sync(msk, ssk);
1320 1321
	err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
	if (err && err != -EINPROGRESS)
1322
		goto failed_unlink;
1323

1324 1325 1326
	/* discard the subflow socket */
	mptcp_sock_graft(ssk, sk->sk_socket);
	iput(SOCK_INODE(sf));
1327 1328
	return err;

1329
failed_unlink:
1330
	spin_lock_bh(&msk->join_list_lock);
1331
	list_del(&subflow->node);
1332
	spin_unlock_bh(&msk->join_list_lock);
1333
	sock_put(mptcp_subflow_tcp_sock(subflow));
1334 1335

failed:
P
Paolo Abeni 已提交
1336
	subflow->disposable = 1;
1337 1338 1339 1340
	sock_release(sf);
	return err;
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
{
#ifdef CONFIG_SOCK_CGROUP_DATA
	struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
				*child_skcd = &child->sk_cgrp_data;

	/* only the additional subflows created by kworkers have to be modified */
	if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
	    cgroup_id(sock_cgroup_ptr(child_skcd))) {
#ifdef CONFIG_MEMCG
		struct mem_cgroup *memcg = parent->sk_memcg;

		mem_cgroup_sk_free(child);
		if (memcg && css_tryget(&memcg->css))
			child->sk_memcg = memcg;
#endif /* CONFIG_MEMCG */

		cgroup_sk_free(child_skcd);
		*child_skcd = *parent_skcd;
		cgroup_sk_clone(child_skcd);
	}
#endif /* CONFIG_SOCK_CGROUP_DATA */
}

P
Paolo Abeni 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
static void mptcp_subflow_ops_override(struct sock *ssk)
{
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	if (ssk->sk_prot == &tcpv6_prot)
		ssk->sk_prot = &tcpv6_prot_override;
	else
#endif
		ssk->sk_prot = &tcp_prot_override;
}

static void mptcp_subflow_ops_undo_override(struct sock *ssk)
{
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	if (ssk->sk_prot == &tcpv6_prot_override)
		ssk->sk_prot = &tcpv6_prot;
	else
#endif
		ssk->sk_prot = &tcp_prot;
}
1384 1385 1386 1387 1388 1389 1390
int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
{
	struct mptcp_subflow_context *subflow;
	struct net *net = sock_net(sk);
	struct socket *sf;
	int err;

1391 1392 1393 1394 1395 1396
	/* un-accepted server sockets can reach here - on bad configuration
	 * bail early to avoid greater trouble later
	 */
	if (unlikely(!sk->sk_socket))
		return -EINVAL;

1397 1398
	err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
			       &sf);
1399 1400 1401 1402 1403
	if (err)
		return err;

	lock_sock(sf->sk);

1404 1405 1406
	/* the newly created socket has to be in the same cgroup as its parent */
	mptcp_attach_cgroup(sk, sf->sk);

1407 1408 1409 1410 1411
	/* kernel sockets do not by default acquire net ref, but TCP timer
	 * needs it.
	 */
	sf->sk->sk_net_refcnt = 1;
	get_net(net);
1412
#ifdef CONFIG_PROC_FS
1413
	this_cpu_add(*net->core.sock_inuse, 1);
1414
#endif
1415 1416 1417
	err = tcp_set_ulp(sf->sk, "mptcp");
	release_sock(sf->sk);

1418 1419
	if (err) {
		sock_release(sf);
1420
		return err;
1421
	}
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	/* the newly created socket really belongs to the owning MPTCP master
	 * socket, even if for additional subflows the allocation is performed
	 * by a kernel workqueue. Adjust inode references, so that the
	 * procfs/diag interaces really show this one belonging to the correct
	 * user.
	 */
	SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
	SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
	SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;

1433 1434 1435 1436
	subflow = mptcp_subflow_ctx(sf->sk);
	pr_debug("subflow=%p", subflow);

	*new_sock = sf;
1437
	sock_hold(sk);
1438
	subflow->conn = sk;
P
Paolo Abeni 已提交
1439
	mptcp_subflow_ops_override(sf->sk);
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

	return 0;
}

static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
							gfp_t priority)
{
	struct inet_connection_sock *icsk = inet_csk(sk);
	struct mptcp_subflow_context *ctx;

	ctx = kzalloc(sizeof(*ctx), priority);
	if (!ctx)
		return NULL;

	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1455
	INIT_LIST_HEAD(&ctx->node);
P
Paolo Abeni 已提交
1456
	INIT_LIST_HEAD(&ctx->delegated_node);
1457 1458 1459 1460 1461 1462 1463 1464

	pr_debug("subflow=%p", ctx);

	ctx->tcp_sock = sk;

	return ctx;
}

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
static void __subflow_state_change(struct sock *sk)
{
	struct socket_wq *wq;

	rcu_read_lock();
	wq = rcu_dereference(sk->sk_wq);
	if (skwq_has_sleeper(wq))
		wake_up_interruptible_all(&wq->wait);
	rcu_read_unlock();
}

static bool subflow_is_done(const struct sock *sk)
{
	return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
}

static void subflow_state_change(struct sock *sk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
P
Paolo Abeni 已提交
1484
	struct sock *parent = subflow->conn;
1485 1486 1487

	__subflow_state_change(sk);

1488
	if (subflow_simultaneous_connect(sk)) {
P
Paolo Abeni 已提交
1489
		mptcp_propagate_sndbuf(parent, sk);
1490
		mptcp_do_fallback(sk);
1491
		mptcp_rcv_space_init(mptcp_sk(parent), sk);
1492 1493
		pr_fallback(mptcp_sk(parent));
		subflow->conn_finished = 1;
1494
		mptcp_set_connected(parent);
1495 1496
	}

1497 1498 1499 1500
	/* as recvmsg() does not acquire the subflow socket for ssk selection
	 * a fin packet carrying a DSS can be unnoticed if we don't trigger
	 * the data available machinery here.
	 */
1501
	if (mptcp_subflow_data_available(sk))
1502
		mptcp_data_ready(parent, sk);
1503 1504
	else if (unlikely(sk->sk_err))
		subflow_error_report(sk);
1505

1506 1507
	subflow_sched_work_if_closed(mptcp_sk(parent), sk);

1508
	if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1509 1510
	    !subflow->rx_eof && subflow_is_done(sk)) {
		subflow->rx_eof = 1;
1511
		mptcp_subflow_eof(parent);
1512 1513 1514
	}
}

1515 1516
static int subflow_ulp_init(struct sock *sk)
{
1517
	struct inet_connection_sock *icsk = inet_csk(sk);
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	struct mptcp_subflow_context *ctx;
	struct tcp_sock *tp = tcp_sk(sk);
	int err = 0;

	/* disallow attaching ULP to a socket unless it has been
	 * created with sock_create_kern()
	 */
	if (!sk->sk_kern_sock) {
		err = -EOPNOTSUPP;
		goto out;
	}

	ctx = subflow_create_ctx(sk, GFP_KERNEL);
	if (!ctx) {
		err = -ENOMEM;
		goto out;
	}

	pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);

	tp->is_mptcp = 1;
1539 1540
	ctx->icsk_af_ops = icsk->icsk_af_ops;
	icsk->icsk_af_ops = subflow_default_af_ops(sk);
1541 1542 1543
	ctx->tcp_data_ready = sk->sk_data_ready;
	ctx->tcp_state_change = sk->sk_state_change;
	ctx->tcp_write_space = sk->sk_write_space;
P
Paolo Abeni 已提交
1544
	ctx->tcp_error_report = sk->sk_error_report;
1545 1546 1547
	sk->sk_data_ready = subflow_data_ready;
	sk->sk_write_space = subflow_write_space;
	sk->sk_state_change = subflow_state_change;
P
Paolo Abeni 已提交
1548
	sk->sk_error_report = subflow_error_report;
1549 1550 1551 1552
out:
	return err;
}

P
Paolo Abeni 已提交
1553
static void subflow_ulp_release(struct sock *ssk)
1554
{
P
Paolo Abeni 已提交
1555 1556 1557
	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
	bool release = true;
	struct sock *sk;
1558 1559 1560 1561

	if (!ctx)
		return;

P
Paolo Abeni 已提交
1562 1563 1564
	sk = ctx->conn;
	if (sk) {
		/* if the msk has been orphaned, keep the ctx
1565 1566
		 * alive, will be freed by __mptcp_close_ssk(),
		 * when the subflow is still unaccepted
P
Paolo Abeni 已提交
1567
		 */
1568
		release = ctx->disposable || list_empty(&ctx->node);
P
Paolo Abeni 已提交
1569 1570
		sock_put(sk);
	}
1571

P
Paolo Abeni 已提交
1572
	mptcp_subflow_ops_undo_override(ssk);
P
Paolo Abeni 已提交
1573 1574
	if (release)
		kfree_rcu(ctx, rcu);
1575 1576
}

1577 1578 1579 1580 1581 1582 1583 1584
static void subflow_ulp_clone(const struct request_sock *req,
			      struct sock *newsk,
			      const gfp_t priority)
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
	struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
	struct mptcp_subflow_context *new_ctx;

1585 1586
	if (!tcp_rsk(req)->is_mptcp ||
	    (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1587
		subflow_ulp_fallback(newsk, old_ctx);
1588 1589 1590 1591
		return;
	}

	new_ctx = subflow_create_ctx(newsk, priority);
M
Mat Martineau 已提交
1592
	if (!new_ctx) {
1593
		subflow_ulp_fallback(newsk, old_ctx);
1594 1595 1596 1597 1598
		return;
	}

	new_ctx->conn_finished = 1;
	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1599 1600 1601
	new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
	new_ctx->tcp_state_change = old_ctx->tcp_state_change;
	new_ctx->tcp_write_space = old_ctx->tcp_write_space;
P
Paolo Abeni 已提交
1602
	new_ctx->tcp_error_report = old_ctx->tcp_error_report;
P
Paolo Abeni 已提交
1603 1604 1605
	new_ctx->rel_write_seq = 1;
	new_ctx->tcp_sock = newsk;

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
	if (subflow_req->mp_capable) {
		/* see comments in subflow_syn_recv_sock(), MPTCP connection
		 * is fully established only after we receive the remote key
		 */
		new_ctx->mp_capable = 1;
		new_ctx->local_key = subflow_req->local_key;
		new_ctx->token = subflow_req->token;
		new_ctx->ssn_offset = subflow_req->ssn_offset;
		new_ctx->idsn = subflow_req->idsn;
	} else if (subflow_req->mp_join) {
1616
		new_ctx->ssn_offset = subflow_req->ssn_offset;
1617 1618 1619 1620
		new_ctx->mp_join = 1;
		new_ctx->fully_established = 1;
		new_ctx->backup = subflow_req->backup;
		new_ctx->local_id = subflow_req->local_id;
1621
		new_ctx->remote_id = subflow_req->remote_id;
1622 1623 1624
		new_ctx->token = subflow_req->token;
		new_ctx->thmac = subflow_req->thmac;
	}
1625 1626
}

P
Paolo Abeni 已提交
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
static void tcp_release_cb_override(struct sock *ssk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);

	if (mptcp_subflow_has_delegated_action(subflow))
		mptcp_subflow_process_delegated(ssk);

	tcp_release_cb(ssk);
}

1637 1638 1639 1640 1641
static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
	.name		= "mptcp",
	.owner		= THIS_MODULE,
	.init		= subflow_ulp_init,
	.release	= subflow_ulp_release,
1642
	.clone		= subflow_ulp_clone,
1643 1644
};

1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
static int subflow_ops_init(struct request_sock_ops *subflow_ops)
{
	subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
	subflow_ops->slab_name = "request_sock_subflow";

	subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
					      subflow_ops->obj_size, 0,
					      SLAB_ACCOUNT |
					      SLAB_TYPESAFE_BY_RCU,
					      NULL);
	if (!subflow_ops->slab)
		return -ENOMEM;

1658 1659
	subflow_ops->destructor = subflow_req_destructor;

1660 1661 1662
	return 0;
}

1663
void __init mptcp_subflow_init(void)
1664
{
1665 1666
	mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
	if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1667 1668 1669
		panic("MPTCP: failed to init subflow request sock ops\n");

	subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1670
	subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1671 1672 1673 1674 1675 1676

	subflow_specific = ipv4_specific;
	subflow_specific.conn_request = subflow_v4_conn_request;
	subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
	subflow_specific.sk_rx_dst_set = subflow_finish_connect;

P
Paolo Abeni 已提交
1677 1678 1679
	tcp_prot_override = tcp_prot;
	tcp_prot_override.release_cb = tcp_release_cb_override;

1680 1681
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1682
	subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694

	subflow_v6_specific = ipv6_specific;
	subflow_v6_specific.conn_request = subflow_v6_conn_request;
	subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
	subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;

	subflow_v6m_specific = subflow_v6_specific;
	subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
	subflow_v6m_specific.send_check = ipv4_specific.send_check;
	subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
	subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
	subflow_v6m_specific.net_frag_header_len = 0;
P
Paolo Abeni 已提交
1695 1696 1697

	tcpv6_prot_override = tcpv6_prot;
	tcpv6_prot_override.release_cb = tcp_release_cb_override;
1698 1699
#endif

1700 1701
	mptcp_diag_subflow_init(&subflow_ulp_ops);

1702 1703 1704
	if (tcp_register_ulp(&subflow_ulp_ops) != 0)
		panic("MPTCP: failed to register subflows to ULP\n");
}