Linux Audio

Check our new training course

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