subflow.c 37.4 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/sha.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 21
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
#include <net/ip6_route.h>
#endif
22 23
#include <net/mptcp.h>
#include "protocol.h"
24 25 26 27 28 29 30
#include "mib.h"

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);
}
31

32 33 34 35 36 37
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);

38 39 40
	if (subflow_req->msk)
		sock_put((struct sock *)subflow_req->msk);

P
Paolo Abeni 已提交
41
	mptcp_token_destroy_request(req);
42 43 44
	tcp_request_sock_ops.destructor(req);
}

45 46 47 48 49 50 51 52 53 54 55
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);
}

56 57 58 59 60 61
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);
}

62
/* validate received token and create truncated hmac and nonce for SYN-ACK */
63 64
static struct mptcp_sock *subflow_token_join_request(struct request_sock *req,
						     const struct sk_buff *skb)
65 66
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
67
	u8 hmac[SHA256_DIGEST_SIZE];
68 69 70 71 72
	struct mptcp_sock *msk;
	int local_id;

	msk = mptcp_token_get_sock(subflow_req->token);
	if (!msk) {
73
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
74
		return NULL;
75 76 77 78 79
	}

	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
	if (local_id < 0) {
		sock_put((struct sock *)msk);
80
		return NULL;
81 82 83 84 85 86 87 88 89 90
	}
	subflow_req->local_id = local_id;

	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);
91
	return msk;
92 93
}

94
static int __subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
95 96 97 98
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);

	subflow_req->mp_capable = 0;
99
	subflow_req->mp_join = 0;
100
	subflow_req->msk = NULL;
P
Paolo Abeni 已提交
101
	mptcp_token_init_request(req);
102 103 104 105 106 107

#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))
108
		return -EINVAL;
109 110
#endif

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	return 0;
}

static void subflow_init_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 ret;

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

	ret = __subflow_init_req(req, sk_listener);
	if (ret)
		return;

	mptcp_get_options(skb, &mp_opt);

131
	if (mp_opt.mp_capable) {
132 133
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);

134
		if (mp_opt.mp_join)
135
			return;
136
	} else if (mp_opt.mp_join) {
137 138
		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
	}
139

140
	if (mp_opt.mp_capable && listener->request_mptcp) {
141 142
		int err, retries = 4;

143
		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
144 145 146 147
again:
		do {
			get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
		} while (subflow_req->local_key == 0);
148

149 150 151 152 153 154 155 156 157 158 159 160 161
		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;
			} else {
				subflow_req->mp_capable = 1;
			}
			return;
		}

162 163 164
		err = mptcp_token_new_request(req);
		if (err == 0)
			subflow_req->mp_capable = 1;
165 166
		else if (retries-- > 0)
			goto again;
167

168
	} else if (mp_opt.mp_join && listener->request_mptcp) {
169
		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
170
		subflow_req->mp_join = 1;
171 172 173 174
		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;
175
		subflow_req->msk = subflow_token_join_request(req, skb);
176 177 178 179 180 181

		if (unlikely(req->syncookie) && subflow_req->msk) {
			if (mptcp_can_accept_new_subflow(subflow_req->msk))
				subflow_init_req_cookie_join_save(subflow_req, skb);
		}

182 183
		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
			 subflow_req->remote_nonce, subflow_req->msk);
184 185 186
	}
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
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;

	err = __subflow_init_req(req, sk_listener);
	if (err)
		return err;

	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;
216 217 218 219 220 221 222 223
	} 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;
224 225 226 227 228 229
	}

	return 0;
}
EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void subflow_v4_init_req(struct request_sock *req,
				const struct sock *sk_listener,
				struct sk_buff *skb)
{
	tcp_rsk(req)->is_mptcp = 1;

	tcp_request_sock_ipv4_ops.init_req(req, sk_listener, skb);

	subflow_init_req(req, sk_listener, skb);
}

#if IS_ENABLED(CONFIG_MPTCP_IPV6)
static void subflow_v6_init_req(struct request_sock *req,
				const struct sock *sk_listener,
				struct sk_buff *skb)
{
	tcp_rsk(req)->is_mptcp = 1;

	tcp_request_sock_ipv6_ops.init_req(req, sk_listener, skb);

	subflow_init_req(req, sk_listener, skb);
}
#endif

254 255 256
/* validate received truncated hmac and create hmac for third ACK */
static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
{
257
	u8 hmac[SHA256_DIGEST_SIZE];
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	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;
}

273 274 275
static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
276
	struct mptcp_options_received mp_opt;
277
	struct sock *parent = subflow->conn;
278 279 280

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

281
	if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
282 283 284 285
		inet_sk_state_store(parent, TCP_ESTABLISHED);
		parent->sk_state_change(parent);
	}

286 287 288 289
	/* be sure no special action on any packet other than syn-ack */
	if (subflow->conn_finished)
		return;

290
	subflow->rel_write_seq = 1;
291
	subflow->conn_finished = 1;
292 293
	subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
	pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
294

295
	mptcp_get_options(skb, &mp_opt);
296 297 298 299 300 301 302 303 304
	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;
		}

305 306
		subflow->mp_capable = 1;
		subflow->can_ack = 1;
307
		subflow->remote_key = mp_opt.sndr_key;
308 309
		pr_debug("subflow=%p, remote_key=%llu", subflow,
			 subflow->remote_key);
310 311 312 313 314 315 316
		mptcp_finish_connect(sk);
	} else if (subflow->request_join) {
		u8 hmac[SHA256_DIGEST_SIZE];

		if (!mp_opt.mp_join)
			goto do_reset;

317 318
		subflow->thmac = mp_opt.thmac;
		subflow->remote_nonce = mp_opt.nonce;
319 320 321
		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
			 subflow->thmac, subflow->remote_nonce);

322
		if (!subflow_thmac_valid(subflow)) {
323
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
324 325 326 327 328 329
			goto do_reset;
		}

		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
				      subflow->local_nonce,
				      subflow->remote_nonce,
330 331
				      hmac);
		memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
332 333 334 335

		if (!mptcp_finish_join(sk))
			goto do_reset;

336
		subflow->mp_join = 1;
337
		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
338 339 340
	} else if (mptcp_check_fallback(sk)) {
fallback:
		mptcp_rcv_space_init(mptcp_sk(parent), sk);
341
	}
342 343 344 345 346
	return;

do_reset:
	tcp_send_active_reset(sk, GFP_ATOMIC);
	tcp_done(sk);
347 348
}

349 350
struct request_sock_ops mptcp_subflow_request_sock_ops;
EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
351 352 353 354 355 356 357 358 359 360 361 362
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;

363
	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
				&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;

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;

388
	return tcp_conn_request(&mptcp_subflow_request_sock_ops,
389 390 391 392 393 394 395 396
				&subflow_request_sock_ipv6_ops, sk, skb);

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

397 398
/* validate hmac received in third ACK */
static bool subflow_hmac_valid(const struct request_sock *req,
399
			       const struct mptcp_options_received *mp_opt)
400 401
{
	const struct mptcp_subflow_request_sock *subflow_req;
402
	u8 hmac[SHA256_DIGEST_SIZE];
403 404 405
	struct mptcp_sock *msk;

	subflow_req = mptcp_subflow_rsk(req);
406
	msk = subflow_req->msk;
407 408 409 410 411 412 413
	if (!msk)
		return false;

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

414
	return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
415 416
}

417 418 419 420 421 422 423 424 425
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).
	 *
426 427
	 * Problem is that the mptcp socket will be in
	 * ESTABLISHED state and will not have the SOCK_DEAD flag.
428 429 430
	 * Both result in warnings from inet_sock_destruct.
	 */

431
	if (sk->sk_state == TCP_ESTABLISHED) {
432 433 434 435 436
		sk->sk_state = TCP_CLOSE;
		WARN_ON_ONCE(sk->sk_socket);
		sock_orphan(sk);
	}

437
	skb_rbtree_purge(&mptcp_sk(sk)->out_of_order_queue);
P
Paolo Abeni 已提交
438
	mptcp_token_destroy(mptcp_sk(sk));
439 440 441
	inet_sock_destruct(sk);
}

442 443 444 445 446 447
static void mptcp_force_close(struct sock *sk)
{
	inet_sk_state_store(sk, TCP_CLOSE);
	sk_common_release(sk);
}

448 449 450 451 452 453 454 455 456 457 458
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;
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472
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);
}

473 474 475 476 477 478 479 480 481 482 483
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);
}

484 485 486 487 488 489 490 491
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);
492
	struct mptcp_subflow_request_sock *subflow_req;
493
	struct mptcp_options_received mp_opt;
494
	bool fallback, fallback_is_fatal;
P
Paolo Abeni 已提交
495
	struct sock *new_msk = NULL;
496 497 498 499
	struct sock *child;

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

500 501
	/* After child creation we must look for 'mp_capable' even when options
	 * are not parsed
502 503
	 */
	mp_opt.mp_capable = 0;
504 505 506

	/* hopefully temporary handling for MP_JOIN+syncookie */
	subflow_req = mptcp_subflow_rsk(req);
507
	fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
508 509
	fallback = !tcp_rsk(req)->is_mptcp;
	if (fallback)
510 511
		goto create_child;

512
	/* if the sk is MP_CAPABLE, we try to fetch the client key */
513
	if (subflow_req->mp_capable) {
514 515 516 517 518
		if (TCP_SKB_CB(skb)->seq != subflow_req->ssn_offset + 1) {
			/* here we can receive and accept an in-window,
			 * out-of-order pkt, which will not carry the MP_CAPABLE
			 * opt even on mptcp enabled paths
			 */
P
Paolo Abeni 已提交
519
			goto create_msk;
520 521
		}

522 523
		mptcp_get_options(skb, &mp_opt);
		if (!mp_opt.mp_capable) {
524
			fallback = true;
P
Paolo Abeni 已提交
525
			goto create_child;
526
		}
P
Paolo Abeni 已提交
527 528

create_msk:
529
		new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
P
Paolo Abeni 已提交
530
		if (!new_msk)
531
			fallback = true;
532
	} else if (subflow_req->mp_join) {
533 534
		mptcp_get_options(skb, &mp_opt);
		if (!mp_opt.mp_join ||
535
		    !mptcp_can_accept_new_subflow(subflow_req->msk) ||
536
		    !subflow_hmac_valid(req, &mp_opt)) {
537
			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
538
			fallback = true;
539
		}
540
	}
541

542
create_child:
543 544 545 546
	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
						     req_unhash, own_req);

	if (child && *own_req) {
547 548
		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);

549 550
		tcp_rsk(req)->drop_req = false;

551 552 553
		/* 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.
554
		 */
555
		if (!ctx || fallback) {
556
			if (fallback_is_fatal)
557
				goto dispose_child;
558

559
			subflow_drop_ctx(child);
P
Paolo Abeni 已提交
560
			goto out;
561
		}
562 563

		if (ctx->mp_capable) {
564 565 566 567 568
			/* 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);

P
Paolo Abeni 已提交
569 570 571
			/* new mpc subflow takes ownership of the newly
			 * created mptcp socket
			 */
572
			new_msk->sk_destruct = mptcp_sock_destruct;
573
			mptcp_pm_new_connection(mptcp_sk(new_msk), 1);
P
Paolo Abeni 已提交
574
			mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
P
Paolo Abeni 已提交
575 576
			ctx->conn = new_msk;
			new_msk = NULL;
577 578 579 580

			/* with OoO packets we can reach here without ingress
			 * mpc option
			 */
581 582
			if (mp_opt.mp_capable)
				mptcp_subflow_fully_established(ctx, &mp_opt);
583 584 585
		} else if (ctx->mp_join) {
			struct mptcp_sock *owner;

586
			owner = subflow_req->msk;
587
			if (!owner)
588
				goto dispose_child;
589

590 591
			/* move the msk reference ownership to the subflow */
			subflow_req->msk = NULL;
592 593
			ctx->conn = (struct sock *)owner;
			if (!mptcp_finish_join(child))
594
				goto dispose_child;
595 596

			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
597
			tcp_rsk(req)->drop_req = true;
598 599 600
		}
	}

P
Paolo Abeni 已提交
601 602 603
out:
	/* dispose of the left over mptcp master, if any */
	if (unlikely(new_msk))
604
		mptcp_force_close(new_msk);
605 606 607 608

	/* check for expected invariant - should never trigger, just help
	 * catching eariler subtle bugs
	 */
609
	WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
610 611
		     (!mptcp_subflow_ctx(child) ||
		      !mptcp_subflow_ctx(child)->conn));
612
	return child;
613

614
dispose_child:
615
	subflow_drop_ctx(child);
616 617
	tcp_rsk(req)->drop_req = true;
	inet_csk_prepare_for_destroy_sock(child);
618
	tcp_done(child);
P
Paolo Abeni 已提交
619
	req->rsk_ops->send_reset(sk, skb);
620 621 622

	/* The last child reference will be released by the caller */
	return child;
623 624 625 626
}

static struct inet_connection_sock_af_ops subflow_specific;

627 628 629 630
enum mapping_status {
	MAPPING_OK,
	MAPPING_INVALID,
	MAPPING_EMPTY,
631 632
	MAPPING_DATA_FIN,
	MAPPING_DUMMY
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
};

static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
{
	if ((u32)seq == (u32)old_seq)
		return old_seq;

	/* Assume map covers data not mapped yet. */
	return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
}

static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
{
	WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
		  ssn, subflow->map_subflow_seq, subflow->map_data_len);
}

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.
		 */
		warn_bad_map(subflow, ssn);
		return false;
	}
	if (unlikely(!before(ssn, subflow->map_subflow_seq +
				  subflow->map_data_len))) {
		/* Mapping does covers past subflow data, invalid */
		warn_bad_map(subflow, ssn + skb->len);
		return false;
	}
	return true;
}

684 685
static enum mapping_status get_mapping_status(struct sock *ssk,
					      struct mptcp_sock *msk)
686 687 688 689 690 691 692 693 694 695 696
{
	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;

697 698 699
	if (mptcp_check_fallback(ssk))
		return MAPPING_DUMMY;

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	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;
	}

	pr_debug("seq=%llu is64=%d ssn=%u data_len=%u data_fin=%d",
		 mpext->data_seq, mpext->dsn64, mpext->subflow_seq,
		 mpext->data_len, mpext->data_fin);

	data_len = mpext->data_len;
	if (data_len == 0) {
		pr_err("Infinite mapping not handled");
729
		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
730 731 732 733 734
		return MAPPING_INVALID;
	}

	if (mpext->data_fin == 1) {
		if (data_len == 1) {
735 736
			mptcp_update_rcv_data_fin(msk, mpext->data_seq);
			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
737 738 739 740 741 742 743 744 745 746 747
			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 {
				return MAPPING_DATA_FIN;
			}
748 749 750
		} else {
			mptcp_update_rcv_data_fin(msk, mpext->data_seq + data_len);
			pr_debug("DATA_FIN with mapping seq=%llu", mpext->data_seq + data_len);
751 752 753 754 755 756 757 758 759
		}

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

	if (!mpext->dsn64) {
		map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
				     mpext->data_seq);
760
		subflow->use_64bit_ack = 0;
761 762 763
		pr_debug("expanded seq=%llu", subflow->map_seq);
	} else {
		map_seq = mpext->data_seq;
764
		subflow->use_64bit_ack = 1;
765 766 767 768 769 770 771 772 773 774 775 776 777 778
	}

	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
		 */
779 780
		if (skb_is_fully_mapped(ssk, skb)) {
			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
781
			return MAPPING_INVALID;
782
		}
783 784 785 786 787 788 789 790 791

		/* 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;
792
	subflow->mpc_map = mpext->mpc_map;
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	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;
}

808 809 810 811 812 813 814 815 816 817 818 819
static int subflow_read_actor(read_descriptor_t *desc,
			      struct sk_buff *skb,
			      unsigned int offset, size_t len)
{
	size_t copy_len = min(desc->count, len);

	desc->count -= copy_len;

	pr_debug("flushed %zu bytes, %zu left", copy_len, desc->count);
	return copy_len;
}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
int mptcp_subflow_discard_data(struct sock *ssk, unsigned int limit)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
	u32 map_remaining;
	size_t delta;

	map_remaining = subflow->map_data_len -
			mptcp_subflow_get_map_offset(subflow);
	delta = min_t(size_t, limit, map_remaining);

	/* discard mapped data */
	pr_debug("discarding %zu bytes, current map len=%d", delta,
		 map_remaining);
	if (delta) {
		read_descriptor_t desc = {
			.count = delta,
		};
		int ret;

		ret = tcp_read_sock(ssk, &desc, subflow_read_actor);
		if (ret < 0) {
			ssk->sk_err = -ret;
			return ret;
		}
		if (ret < delta)
			return 0;
		if (delta == map_remaining) {
			subflow->data_avail = 0;
			subflow->map_valid = 0;
		}
	}
	return 0;
}

854 855 856 857 858 859 860 861 862
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;

	pr_debug("msk=%p ssk=%p data_avail=%d skb=%p", subflow->conn, ssk,
		 subflow->data_avail, skb_peek(&ssk->sk_receive_queue));
863 864
	if (!skb_peek(&ssk->sk_receive_queue))
		subflow->data_avail = 0;
865 866 867 868 869 870 871 872
	if (subflow->data_avail)
		return true;

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

873
		status = get_mapping_status(ssk, msk);
874 875 876 877 878
		pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
		if (status == MAPPING_INVALID) {
			ssk->sk_err = EBADMSG;
			goto fatal;
		}
879 880 881 882 883 884 885 886
		if (status == MAPPING_DUMMY) {
			__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;
887
			subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
888 889
			return true;
		}
890 891 892 893 894 895 896 897

		if (status != MAPPING_OK)
			return false;

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

898 899 900 901 902 903 904 905 906 907 908 909 910
		/* if msk lacks the remote key, this subflow must provide an
		 * MP_CAPABLE-based mapping
		 */
		if (unlikely(!READ_ONCE(msk->can_ack))) {
			if (!subflow->mpc_map) {
				ssk->sk_err = EBADMSG;
				goto fatal;
			}
			WRITE_ONCE(msk->remote_key, subflow->remote_key);
			WRITE_ONCE(msk->ack_seq, subflow->map_seq);
			WRITE_ONCE(msk->can_ack, true);
		}

911 912 913 914
		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);
915
		if (ack_seq == old_ack) {
916 917 918 919
			subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
			break;
		} else if (after64(ack_seq, old_ack)) {
			subflow->data_avail = MPTCP_SUBFLOW_OOO_DATA;
920
			break;
921
		}
922 923

		/* only accept in-sequence mapping. Old values are spurious
924
		 * retransmission
925
		 */
926 927 928
		if (mptcp_subflow_discard_data(ssk, old_ack - ack_seq))
			goto fatal;
		return false;
929 930 931 932 933 934 935 936 937 938
	}
	return true;

fatal:
	/* fatal protocol error, close the socket */
	/* This barrier is coupled with smp_rmb() in tcp_poll() */
	smp_wmb();
	ssk->sk_error_report(ssk);
	tcp_set_state(ssk, TCP_CLOSE);
	tcp_send_active_reset(ssk, GFP_ATOMIC);
939
	subflow->data_avail = 0;
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	return false;
}

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;
		subflow->data_avail = 0;

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

958
	return subflow_check_data_avail(sk);
959 960
}

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
/* 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,
 * as far as mptcp peer is concerened that data is still inflight.
 * 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;

	*space = tcp_space(sk);
	*full_space = tcp_full_space(sk);
}

979 980 981
static void subflow_data_ready(struct sock *sk)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
982
	u16 state = 1 << inet_sk_state_load(sk);
983
	struct sock *parent = subflow->conn;
984
	struct mptcp_sock *msk;
985

986
	msk = mptcp_sk(parent);
987
	if (state & TCPF_LISTEN) {
988
		set_bit(MPTCP_DATA_READY, &msk->flags);
P
Paolo Abeni 已提交
989
		parent->sk_data_ready(parent);
990 991 992
		return;
	}

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

996
	if (mptcp_subflow_data_available(sk))
997
		mptcp_data_ready(parent, sk);
998 999 1000 1001 1002 1003 1004
}

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

1005 1006 1007 1008
	if (!sk_stream_is_writeable(sk))
		return;

	if (sk_stream_is_writeable(parent)) {
1009 1010 1011
		set_bit(MPTCP_SEND_SPACE, &mptcp_sk(parent)->flags);
		smp_mb__after_atomic();
		/* set SEND_SPACE before sk_stream_write_space clears NOSPACE */
1012 1013 1014 1015
		sk_stream_write_space(parent);
	}
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
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)
1027 1028
void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
{
1029 1030 1031 1032 1033 1034 1035
	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 已提交
1036
		 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1037 1038 1039 1040 1041 1042 1043

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

	subflow->icsk_af_ops = icsk->icsk_af_ops;
	icsk->icsk_af_ops = target;
}
1044
#endif
1045

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
				struct sockaddr_storage *addr)
{
	memset(addr, 0, sizeof(*addr));
	addr->ss_family = info->family;
	if (addr->ss_family == AF_INET) {
		struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;

		in_addr->sin_addr = info->addr;
		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;

		in6_addr->sin6_addr = info->addr6;
		in6_addr->sin6_port = info->port;
	}
#endif
}

int __mptcp_subflow_connect(struct sock *sk, int ifindex,
			    const struct mptcp_addr_info *loc,
			    const struct mptcp_addr_info *remote)
{
	struct mptcp_sock *msk = mptcp_sk(sk);
	struct mptcp_subflow_context *subflow;
	struct sockaddr_storage addr;
1074
	int local_id = loc->id;
1075
	struct socket *sf;
1076
	struct sock *ssk;
1077 1078 1079 1080
	u32 remote_token;
	int addrlen;
	int err;

1081
	if (!mptcp_is_fully_established(sk))
1082 1083 1084 1085 1086 1087
		return -ENOTCONN;

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

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	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;
	}

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	subflow->remote_key = msk->remote_key;
	subflow->local_key = msk->local_key;
	subflow->token = msk->token;
	mptcp_info2sockaddr(loc, &addr);

	addrlen = sizeof(struct sockaddr_in);
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	if (loc->family == AF_INET6)
		addrlen = sizeof(struct sockaddr_in6);
#endif
1112
	ssk->sk_bound_dev_if = ifindex;
1113 1114 1115 1116 1117
	err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
	if (err)
		goto failed;

	mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1118 1119
	pr_debug("msk=%p remote_token=%u local_id=%d", msk, remote_token,
		 local_id);
1120
	subflow->remote_token = remote_token;
1121
	subflow->local_id = local_id;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	subflow->request_join = 1;
	subflow->request_bkup = 1;
	mptcp_info2sockaddr(remote, &addr);

	err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
	if (err && err != -EINPROGRESS)
		goto failed;

	spin_lock_bh(&msk->join_list_lock);
	list_add_tail(&subflow->node, &msk->join_list);
	spin_unlock_bh(&msk->join_list_lock);

	return err;

failed:
	sock_release(sf);
	return err;
}

1141 1142 1143 1144 1145 1146 1147
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;

1148 1149 1150 1151 1152 1153
	/* un-accepted server sockets can reach here - on bad configuration
	 * bail early to avoid greater trouble later
	 */
	if (unlikely(!sk->sk_socket))
		return -EINVAL;

1154 1155
	err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
			       &sf);
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	if (err)
		return err;

	lock_sock(sf->sk);

	/* kernel sockets do not by default acquire net ref, but TCP timer
	 * needs it.
	 */
	sf->sk->sk_net_refcnt = 1;
	get_net(net);
1166
#ifdef CONFIG_PROC_FS
1167
	this_cpu_add(*net->core.sock_inuse, 1);
1168
#endif
1169 1170 1171
	err = tcp_set_ulp(sf->sk, "mptcp");
	release_sock(sf->sk);

1172 1173
	if (err) {
		sock_release(sf);
1174
		return err;
1175
	}
1176

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	/* 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;

1187 1188 1189 1190
	subflow = mptcp_subflow_ctx(sf->sk);
	pr_debug("subflow=%p", subflow);

	*new_sock = sf;
1191
	sock_hold(sk);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	subflow->conn = sk;

	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);
1208
	INIT_LIST_HEAD(&ctx->node);
1209 1210 1211 1212 1213 1214 1215 1216

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

	ctx->tcp_sock = sk;

	return ctx;
}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
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 已提交
1236
	struct sock *parent = subflow->conn;
1237 1238 1239

	__subflow_state_change(sk);

1240 1241
	if (subflow_simultaneous_connect(sk)) {
		mptcp_do_fallback(sk);
1242
		mptcp_rcv_space_init(mptcp_sk(parent), sk);
1243 1244 1245 1246 1247 1248 1249 1250
		pr_fallback(mptcp_sk(parent));
		subflow->conn_finished = 1;
		if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
			inet_sk_state_store(parent, TCP_ESTABLISHED);
			parent->sk_state_change(parent);
		}
	}

1251 1252 1253 1254
	/* 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.
	 */
1255
	if (mptcp_subflow_data_available(sk))
1256
		mptcp_data_ready(parent, sk);
1257

1258 1259
	if (__mptcp_check_fallback(mptcp_sk(parent)) &&
	    !(parent->sk_shutdown & RCV_SHUTDOWN) &&
1260 1261
	    !subflow->rx_eof && subflow_is_done(sk)) {
		subflow->rx_eof = 1;
1262
		mptcp_subflow_eof(parent);
1263 1264 1265
	}
}

1266 1267
static int subflow_ulp_init(struct sock *sk)
{
1268
	struct inet_connection_sock *icsk = inet_csk(sk);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	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;
1290 1291
	ctx->icsk_af_ops = icsk->icsk_af_ops;
	icsk->icsk_af_ops = subflow_default_af_ops(sk);
1292 1293 1294 1295 1296 1297
	ctx->tcp_data_ready = sk->sk_data_ready;
	ctx->tcp_state_change = sk->sk_state_change;
	ctx->tcp_write_space = sk->sk_write_space;
	sk->sk_data_ready = subflow_data_ready;
	sk->sk_write_space = subflow_write_space;
	sk->sk_state_change = subflow_state_change;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
out:
	return err;
}

static void subflow_ulp_release(struct sock *sk)
{
	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);

	if (!ctx)
		return;

1309 1310 1311
	if (ctx->conn)
		sock_put(ctx->conn);

1312 1313 1314
	kfree_rcu(ctx, rcu);
}

1315 1316 1317 1318 1319 1320 1321 1322
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;

1323 1324
	if (!tcp_rsk(req)->is_mptcp ||
	    (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1325
		subflow_ulp_fallback(newsk, old_ctx);
1326 1327 1328 1329
		return;
	}

	new_ctx = subflow_create_ctx(newsk, priority);
M
Mat Martineau 已提交
1330
	if (!new_ctx) {
1331
		subflow_ulp_fallback(newsk, old_ctx);
1332 1333 1334 1335 1336
		return;
	}

	new_ctx->conn_finished = 1;
	new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1337 1338 1339
	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 已提交
1340 1341 1342
	new_ctx->rel_write_seq = 1;
	new_ctx->tcp_sock = newsk;

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	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) {
1353
		new_ctx->ssn_offset = subflow_req->ssn_offset;
1354 1355 1356 1357 1358 1359 1360
		new_ctx->mp_join = 1;
		new_ctx->fully_established = 1;
		new_ctx->backup = subflow_req->backup;
		new_ctx->local_id = subflow_req->local_id;
		new_ctx->token = subflow_req->token;
		new_ctx->thmac = subflow_req->thmac;
	}
1361 1362
}

1363 1364 1365 1366 1367
static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
	.name		= "mptcp",
	.owner		= THIS_MODULE,
	.init		= subflow_ulp_init,
	.release	= subflow_ulp_release,
1368
	.clone		= subflow_ulp_clone,
1369 1370
};

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
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;

1384 1385
	subflow_ops->destructor = subflow_req_destructor;

1386 1387 1388
	return 0;
}

1389
void __init mptcp_subflow_init(void)
1390
{
1391 1392
	mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
	if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
		panic("MPTCP: failed to init subflow request sock ops\n");

	subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
	subflow_request_sock_ipv4_ops.init_req = subflow_v4_init_req;

	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;

#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
	subflow_request_sock_ipv6_ops.init_req = subflow_v6_init_req;

	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;
#endif

1420 1421
	mptcp_diag_subflow_init(&subflow_ulp_ops);

1422 1423 1424
	if (tcp_register_ulp(&subflow_ulp_ops) != 0)
		panic("MPTCP: failed to register subflows to ULP\n");
}