Linux Audio

Check our new training course

Loading...
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 <crypto/sha.h>
  11#include <net/tcp.h>
  12#include <net/mptcp.h>
  13#include "protocol.h"
 
 
 
  14
  15static bool mptcp_cap_flag_sha256(u8 flags)
  16{
  17	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
  18}
  19
  20static void mptcp_parse_option(const struct sk_buff *skb,
  21			       const unsigned char *ptr, int opsize,
  22			       struct mptcp_options_received *mp_opt)
  23{
  24	u8 subtype = *ptr >> 4;
  25	int expected_opsize;
 
  26	u8 version;
  27	u8 flags;
 
  28
  29	switch (subtype) {
  30	case MPTCPOPT_MP_CAPABLE:
  31		/* strict size checking */
  32		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  33			if (skb->len > tcp_hdr(skb)->doff << 2)
  34				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
  35			else
  36				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
 
  37		} else {
  38			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
  39				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
  40			else
 
  41				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
 
 
  42		}
  43		if (opsize != expected_opsize)
 
 
 
 
 
 
 
 
 
 
 
 
 
  44			break;
  45
  46		/* try to be gentle vs future versions on the initial syn */
  47		version = *ptr++ & MPTCP_VERSION_MASK;
  48		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
  49			if (version != MPTCP_SUPPORTED_VERSION)
  50				break;
  51		} else if (version < MPTCP_SUPPORTED_VERSION) {
  52			break;
  53		}
  54
  55		flags = *ptr++;
  56		if (!mptcp_cap_flag_sha256(flags) ||
  57		    (flags & MPTCP_CAP_EXTENSIBILITY))
  58			break;
  59
  60		/* RFC 6824, Section 3.1:
  61		 * "For the Checksum Required bit (labeled "A"), if either
  62		 * host requires the use of checksums, checksums MUST be used.
  63		 * In other words, the only way for checksums not to be used
  64		 * is if both hosts in their SYNs set A=0."
  65		 *
  66		 * Section 3.3.0:
  67		 * "If a checksum is not present when its use has been
  68		 * negotiated, the receiver MUST close the subflow with a RST as
  69		 * it is considered broken."
  70		 *
  71		 * We don't implement DSS checksum - fall back to TCP.
  72		 */
  73		if (flags & MPTCP_CAP_CHECKSUM_REQD)
  74			break;
 
 
  75
  76		mp_opt->mp_capable = 1;
  77		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
  78			mp_opt->sndr_key = get_unaligned_be64(ptr);
  79			ptr += 8;
  80		}
  81		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
  82			mp_opt->rcvr_key = get_unaligned_be64(ptr);
  83			ptr += 8;
  84		}
  85		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
  86			/* Section 3.1.:
  87			 * "the data parameters in a MP_CAPABLE are semantically
  88			 * equivalent to those in a DSS option and can be used
  89			 * interchangeably."
  90			 */
  91			mp_opt->dss = 1;
  92			mp_opt->use_map = 1;
  93			mp_opt->mpc_map = 1;
 
  94			mp_opt->data_len = get_unaligned_be16(ptr);
  95			ptr += 2;
  96		}
  97		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
 
 
 
 
 
  98			 version, flags, opsize, mp_opt->sndr_key,
  99			 mp_opt->rcvr_key, mp_opt->data_len);
 100		break;
 101
 102	case MPTCPOPT_MP_JOIN:
 103		mp_opt->mp_join = 1;
 104		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
 
 105			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
 106			mp_opt->join_id = *ptr++;
 107			mp_opt->token = get_unaligned_be32(ptr);
 108			ptr += 4;
 109			mp_opt->nonce = get_unaligned_be32(ptr);
 110			ptr += 4;
 111			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
 112				 mp_opt->backup, mp_opt->join_id,
 113				 mp_opt->token, mp_opt->nonce);
 114		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
 
 115			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
 116			mp_opt->join_id = *ptr++;
 117			mp_opt->thmac = get_unaligned_be64(ptr);
 118			ptr += 8;
 119			mp_opt->nonce = get_unaligned_be32(ptr);
 120			ptr += 4;
 121			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
 122				 mp_opt->backup, mp_opt->join_id,
 123				 mp_opt->thmac, mp_opt->nonce);
 124		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
 
 125			ptr += 2;
 126			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
 127			pr_debug("MP_JOIN hmac");
 128		} else {
 129			pr_warn("MP_JOIN bad option size");
 130			mp_opt->mp_join = 0;
 131		}
 132		break;
 133
 134	case MPTCPOPT_DSS:
 135		pr_debug("DSS");
 136		ptr++;
 137
 138		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
 139		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
 140		 * map info accordingly
 141		 */
 142		mp_opt->mpc_map = 0;
 143		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
 144		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
 145		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
 146		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
 147		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
 148		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
 149
 150		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
 151			 mp_opt->data_fin, mp_opt->dsn64,
 152			 mp_opt->use_map, mp_opt->ack64,
 153			 mp_opt->use_ack);
 154
 155		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
 156
 157		if (mp_opt->use_ack) {
 158			if (mp_opt->ack64)
 159				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
 160			else
 161				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
 162		}
 163
 164		if (mp_opt->use_map) {
 165			if (mp_opt->dsn64)
 166				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
 167			else
 168				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
 169		}
 170
 171		/* RFC 6824, Section 3.3:
 172		 * If a checksum is present, but its use had
 173		 * not been negotiated in the MP_CAPABLE handshake,
 174		 * the checksum field MUST be ignored.
 175		 */
 176		if (opsize != expected_opsize &&
 177		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
 178			break;
 179
 180		mp_opt->dss = 1;
 181
 182		if (mp_opt->use_ack) {
 183			if (mp_opt->ack64) {
 184				mp_opt->data_ack = get_unaligned_be64(ptr);
 185				ptr += 8;
 186			} else {
 187				mp_opt->data_ack = get_unaligned_be32(ptr);
 188				ptr += 4;
 189			}
 190
 191			pr_debug("data_ack=%llu", mp_opt->data_ack);
 192		}
 193
 194		if (mp_opt->use_map) {
 195			if (mp_opt->dsn64) {
 196				mp_opt->data_seq = get_unaligned_be64(ptr);
 197				ptr += 8;
 198			} else {
 199				mp_opt->data_seq = get_unaligned_be32(ptr);
 200				ptr += 4;
 201			}
 202
 203			mp_opt->subflow_seq = get_unaligned_be32(ptr);
 204			ptr += 4;
 205
 206			mp_opt->data_len = get_unaligned_be16(ptr);
 207			ptr += 2;
 208
 209			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
 
 
 
 
 
 
 210				 mp_opt->data_seq, mp_opt->subflow_seq,
 211				 mp_opt->data_len);
 
 212		}
 213
 214		break;
 215
 216	case MPTCPOPT_ADD_ADDR:
 217		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
 218		if (!mp_opt->echo) {
 219			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
 220			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
 221				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
 222#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 223			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
 224				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
 225				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
 226#endif
 227			else
 228				break;
 229		} else {
 230			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
 231			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
 232				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
 233#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 234			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
 235				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
 236				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
 237#endif
 238			else
 239				break;
 240		}
 241
 242		mp_opt->add_addr = 1;
 243		mp_opt->port = 0;
 244		mp_opt->addr_id = *ptr++;
 245		pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id);
 246		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
 247			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
 248			ptr += 4;
 249			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
 250			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
 251				mp_opt->port = get_unaligned_be16(ptr);
 252				ptr += 2;
 253			}
 254		}
 255#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 256		else {
 257			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
 258			ptr += 16;
 259			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
 260			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
 261				mp_opt->port = get_unaligned_be16(ptr);
 262				ptr += 2;
 263			}
 264		}
 265#endif
 266		if (!mp_opt->echo) {
 267			mp_opt->ahmac = get_unaligned_be64(ptr);
 268			ptr += 8;
 269		}
 
 
 
 270		break;
 271
 272	case MPTCPOPT_RM_ADDR:
 273		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
 
 274			break;
 275
 276		ptr++;
 277
 278		mp_opt->rm_addr = 1;
 279		mp_opt->rm_id = *ptr++;
 280		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 281		break;
 282
 283	default:
 284		break;
 285	}
 286}
 287
 288void mptcp_get_options(const struct sk_buff *skb,
 289		       struct mptcp_options_received *mp_opt)
 290{
 291	const struct tcphdr *th = tcp_hdr(skb);
 292	const unsigned char *ptr;
 293	int length;
 294
 295	/* initialize option status */
 296	mp_opt->mp_capable = 0;
 297	mp_opt->mp_join = 0;
 298	mp_opt->add_addr = 0;
 299	mp_opt->rm_addr = 0;
 300	mp_opt->dss = 0;
 301
 302	length = (th->doff * 4) - sizeof(struct tcphdr);
 303	ptr = (const unsigned char *)(th + 1);
 304
 305	while (length > 0) {
 306		int opcode = *ptr++;
 307		int opsize;
 308
 309		switch (opcode) {
 310		case TCPOPT_EOL:
 311			return;
 312		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
 313			length--;
 314			continue;
 315		default:
 
 
 316			opsize = *ptr++;
 317			if (opsize < 2) /* "silly options" */
 318				return;
 319			if (opsize > length)
 320				return;	/* don't parse partial options */
 321			if (opcode == TCPOPT_MPTCP)
 322				mptcp_parse_option(skb, ptr, opsize, mp_opt);
 323			ptr += opsize - 2;
 324			length -= opsize;
 325		}
 326	}
 327}
 328
 329bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
 330		       unsigned int *size, struct mptcp_out_options *opts)
 331{
 332	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 333
 334	/* we will use snd_isn to detect first pkt [re]transmission
 335	 * in mptcp_established_options_mp()
 336	 */
 337	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
 338	if (subflow->request_mptcp) {
 339		opts->suboptions = OPTION_MPTCP_MPC_SYN;
 
 
 340		*size = TCPOLEN_MPTCP_MPC_SYN;
 341		return true;
 342	} else if (subflow->request_join) {
 343		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
 344			 subflow->local_nonce);
 345		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
 346		opts->join_id = subflow->local_id;
 347		opts->token = subflow->remote_token;
 348		opts->nonce = subflow->local_nonce;
 349		opts->backup = subflow->request_bkup;
 350		*size = TCPOLEN_MPTCP_MPJ_SYN;
 351		return true;
 352	}
 353	return false;
 354}
 355
 356/* MP_JOIN client subflow must wait for 4th ack before sending any data:
 357 * TCP can't schedule delack timer before the subflow is fully established.
 358 * MPTCP uses the delack timer to do 3rd ack retransmissions
 359 */
 360static void schedule_3rdack_retransmission(struct sock *sk)
 361{
 362	struct inet_connection_sock *icsk = inet_csk(sk);
 363	struct tcp_sock *tp = tcp_sk(sk);
 364	unsigned long timeout;
 365
 366	/* reschedule with a timeout above RTT, as we must look only for drop */
 367	if (tp->srtt_us)
 368		timeout = tp->srtt_us << 1;
 369	else
 370		timeout = TCP_TIMEOUT_INIT;
 371
 372	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
 373	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
 374	icsk->icsk_ack.timeout = timeout;
 375	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
 376}
 377
 378static void clear_3rdack_retransmission(struct sock *sk)
 379{
 380	struct inet_connection_sock *icsk = inet_csk(sk);
 381
 382	sk_stop_timer(sk, &icsk->icsk_delack_timer);
 383	icsk->icsk_ack.timeout = 0;
 384	icsk->icsk_ack.ato = 0;
 385	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
 386}
 387
 388static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
 
 389					 unsigned int *size,
 390					 unsigned int remaining,
 391					 struct mptcp_out_options *opts)
 392{
 393	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 
 394	struct mptcp_ext *mpext;
 395	unsigned int data_len;
 
 396
 397	/* When skb is not available, we better over-estimate the emitted
 398	 * options len. A full DSS option (28 bytes) is longer than
 399	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
 400	 * tell the caller to defer the estimate to
 401	 * mptcp_established_options_dss(), which will reserve enough space.
 402	 */
 403	if (!skb)
 404		return false;
 405
 406	/* MPC/MPJ needed only on 3rd ack packet */
 407	if (subflow->fully_established ||
 408	    subflow->snd_isn != TCP_SKB_CB(skb)->seq)
 
 409		return false;
 410
 411	if (subflow->mp_capable) {
 412		mpext = mptcp_get_ext(skb);
 413		data_len = mpext ? mpext->data_len : 0;
 414
 415		/* we will check ext_copy.data_len in mptcp_write_options() to
 416		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
 417		 * TCPOLEN_MPTCP_MPC_ACK
 418		 */
 419		opts->ext_copy.data_len = data_len;
 420		opts->suboptions = OPTION_MPTCP_MPC_ACK;
 421		opts->sndr_key = subflow->local_key;
 422		opts->rcvr_key = subflow->remote_key;
 
 
 423
 424		/* Section 3.1.
 425		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
 426		 * packets that start the first subflow of an MPTCP connection,
 427		 * as well as the first packet that carries data
 428		 */
 429		if (data_len > 0)
 430			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
 431		else
 
 
 
 
 
 
 
 
 432			*size = TCPOLEN_MPTCP_MPC_ACK;
 
 433
 434		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
 435			 subflow, subflow->local_key, subflow->remote_key,
 436			 data_len);
 437
 438		return true;
 439	} else if (subflow->mp_join) {
 440		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
 441		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
 442		*size = TCPOLEN_MPTCP_MPJ_ACK;
 443		pr_debug("subflow=%p", subflow);
 444
 445		schedule_3rdack_retransmission(sk);
 
 
 
 
 
 
 
 
 446		return true;
 447	}
 448	return false;
 449}
 450
 451static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
 452				 struct sk_buff *skb, struct mptcp_ext *ext)
 453{
 454	/* The write_seq value has already been incremented, so the actual
 455	 * sequence number for the DATA_FIN is one less.
 456	 */
 457	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
 458
 459	if (!ext->use_map || !skb->len) {
 460		/* RFC6824 requires a DSS mapping with specific values
 461		 * if DATA_FIN is set but no data payload is mapped
 462		 */
 463		ext->data_fin = 1;
 464		ext->use_map = 1;
 465		ext->dsn64 = 1;
 466		ext->data_seq = data_fin_tx_seq;
 467		ext->subflow_seq = 0;
 468		ext->data_len = 1;
 469	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
 470		/* If there's an existing DSS mapping and it is the
 471		 * final mapping, DATA_FIN consumes 1 additional byte of
 472		 * mapping space.
 473		 */
 474		ext->data_fin = 1;
 475		ext->data_len++;
 476	}
 477}
 478
 479static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 
 480					  unsigned int *size,
 481					  unsigned int remaining,
 482					  struct mptcp_out_options *opts)
 483{
 484	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 485	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 486	unsigned int dss_size = 0;
 487	u64 snd_data_fin_enable;
 488	struct mptcp_ext *mpext;
 489	unsigned int ack_size;
 490	bool ret = false;
 
 491
 
 492	mpext = skb ? mptcp_get_ext(skb) : NULL;
 493	snd_data_fin_enable = READ_ONCE(msk->snd_data_fin_enable);
 494
 495	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
 496		unsigned int map_size;
 497
 498		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
 
 
 499
 500		remaining -= map_size;
 501		dss_size = map_size;
 502		if (mpext)
 503			opts->ext_copy = *mpext;
 
 504
 
 505		if (skb && snd_data_fin_enable)
 506			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
 
 507		ret = true;
 508	}
 509
 510	/* passive sockets msk will set the 'can_ack' after accept(), even
 511	 * if the first subflow may have the already the remote key handy
 512	 */
 513	opts->ext_copy.use_ack = 0;
 514	if (!READ_ONCE(msk->can_ack)) {
 515		*size = ALIGN(dss_size, 4);
 516		return ret;
 517	}
 518
 519	if (subflow->use_64bit_ack) {
 
 520		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
 521		opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
 522		opts->ext_copy.ack64 = 1;
 523	} else {
 524		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
 525		opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
 526		opts->ext_copy.ack64 = 0;
 527	}
 528	opts->ext_copy.use_ack = 1;
 
 
 529
 530	/* Add kind/length/subtype/flag overhead if mapping is not populated */
 531	if (dss_size == 0)
 532		ack_size += TCPOLEN_MPTCP_DSS_BASE;
 533
 534	dss_size += ack_size;
 535
 536	*size = ALIGN(dss_size, 4);
 537	return true;
 538}
 539
 540static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
 541				  struct in_addr *addr)
 542{
 543	u8 hmac[SHA256_DIGEST_SIZE];
 544	u8 msg[7];
 545
 546	msg[0] = addr_id;
 547	memcpy(&msg[1], &addr->s_addr, 4);
 548	msg[5] = 0;
 549	msg[6] = 0;
 550
 551	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
 552
 553	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
 554}
 555
 556#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 557static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
 558				   struct in6_addr *addr)
 559{
 
 560	u8 hmac[SHA256_DIGEST_SIZE];
 561	u8 msg[19];
 
 562
 563	msg[0] = addr_id;
 564	memcpy(&msg[1], &addr->s6_addr, 16);
 565	msg[17] = 0;
 566	msg[18] = 0;
 
 
 
 
 
 
 
 
 
 567
 568	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
 569
 570	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
 571}
 572#endif
 573
 574static bool mptcp_established_options_addr(struct sock *sk,
 575					   unsigned int *size,
 576					   unsigned int remaining,
 577					   struct mptcp_out_options *opts)
 578{
 579	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 580	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 581	struct mptcp_addr_info saddr;
 
 
 582	int len;
 583
 584	if (!mptcp_pm_should_signal(msk) ||
 585	    !(mptcp_pm_addr_signal(msk, remaining, &saddr)))
 
 
 
 
 
 586		return false;
 587
 588	len = mptcp_add_addr_len(saddr.family);
 
 
 589	if (remaining < len)
 590		return false;
 591
 592	*size = len;
 593	opts->addr_id = saddr.id;
 594	if (saddr.family == AF_INET) {
 595		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
 596		opts->addr = saddr.addr;
 
 
 
 
 
 
 
 
 
 
 
 597		opts->ahmac = add_addr_generate_hmac(msk->local_key,
 598						     msk->remote_key,
 599						     opts->addr_id,
 600						     &opts->addr);
 
 
 601	}
 602#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 603	else if (saddr.family == AF_INET6) {
 604		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
 605		opts->addr6 = saddr.addr6;
 606		opts->ahmac = add_addr6_generate_hmac(msk->local_key,
 607						      msk->remote_key,
 608						      opts->addr_id,
 609						      &opts->addr6);
 610	}
 611#endif
 612	pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 613
 614	return true;
 615}
 616
 617bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
 618			       unsigned int *size, unsigned int remaining,
 619			       struct mptcp_out_options *opts)
 620{
 
 
 621	unsigned int opt_size = 0;
 
 622	bool ret = false;
 623
 624	opts->suboptions = 0;
 625
 626	if (unlikely(mptcp_check_fallback(sk)))
 627		return false;
 628
 629	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 630		ret = true;
 631	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
 632					       opts))
 
 633		ret = true;
 
 
 
 
 
 
 
 634
 635	/* we reserved enough space for the above options, and exceeding the
 636	 * TCP option space would be fatal
 637	 */
 638	if (WARN_ON_ONCE(opt_size > remaining))
 639		return false;
 640
 641	*size += opt_size;
 642	remaining -= opt_size;
 643	if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
 
 
 
 
 
 
 
 
 
 
 644		*size += opt_size;
 645		remaining -= opt_size;
 646		ret = true;
 647	}
 648
 649	return ret;
 650}
 651
 652bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
 653			  struct mptcp_out_options *opts)
 654{
 655	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
 656
 657	if (subflow_req->mp_capable) {
 658		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
 659		opts->sndr_key = subflow_req->local_key;
 
 
 660		*size = TCPOLEN_MPTCP_MPC_SYNACK;
 661		pr_debug("subflow_req=%p, local_key=%llu",
 662			 subflow_req, subflow_req->local_key);
 663		return true;
 664	} else if (subflow_req->mp_join) {
 665		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
 666		opts->backup = subflow_req->backup;
 667		opts->join_id = subflow_req->local_id;
 668		opts->thmac = subflow_req->thmac;
 669		opts->nonce = subflow_req->local_nonce;
 670		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
 671			 subflow_req, opts->backup, opts->join_id,
 672			 opts->thmac, opts->nonce);
 673		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
 674		return true;
 675	}
 676	return false;
 677}
 678
 679static bool check_fully_established(struct mptcp_sock *msk, struct sock *sk,
 680				    struct mptcp_subflow_context *subflow,
 681				    struct sk_buff *skb,
 682				    struct mptcp_options_received *mp_opt)
 683{
 684	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
 685	 * will make the subflow fully established
 686	 */
 687	if (likely(subflow->fully_established)) {
 688		/* on passive sockets, check for 3rd ack retransmission
 689		 * note that msk is always set by subflow_syn_recv_sock()
 690		 * for mp_join subflows
 691		 */
 692		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
 693		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
 694		    subflow->mp_join && mp_opt->mp_join &&
 695		    READ_ONCE(msk->pm.server_side))
 696			tcp_send_ack(sk);
 697		goto fully_established;
 698	}
 699
 700	/* we should process OoO packets before the first subflow is fully
 701	 * established, but not expected for MP_JOIN subflows
 
 
 702	 */
 703	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1)
 
 
 
 
 704		return subflow->mp_capable;
 
 705
 706	if (mp_opt->dss && mp_opt->use_ack) {
 
 
 707		/* subflows are fully established as soon as we get any
 708		 * additional ack.
 709		 */
 710		subflow->fully_established = 1;
 711		WRITE_ONCE(msk->fully_established, true);
 712		goto fully_established;
 713	}
 714
 715	/* If the first established packet does not contain MP_CAPABLE + data
 716	 * then fallback to TCP
 
 717	 */
 718	if (!mp_opt->mp_capable) {
 
 
 719		subflow->mp_capable = 0;
 720		pr_fallback(msk);
 721		__mptcp_do_fallback(msk);
 722		return false;
 723	}
 724
 
 
 
 725	if (unlikely(!READ_ONCE(msk->pm.server_side)))
 726		pr_warn_once("bogus mpc option on established client sk");
 727	mptcp_subflow_fully_established(subflow, mp_opt);
 728
 729fully_established:
 730	if (likely(subflow->pm_notified))
 
 
 
 
 
 
 
 
 
 
 731		return true;
 732
 733	subflow->pm_notified = 1;
 734	if (subflow->mp_join) {
 735		clear_3rdack_retransmission(sk);
 736		mptcp_pm_subflow_established(msk, subflow);
 737	} else {
 738		mptcp_pm_fully_established(msk);
 739	}
 740	return true;
 
 
 
 
 741}
 742
 743static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
 744{
 745	u32 old_ack32, cur_ack32;
 746
 747	if (use_64bit)
 748		return cur_ack;
 
 
 
 749
 750	old_ack32 = (u32)old_ack;
 751	cur_ack32 = (u32)cur_ack;
 752	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
 753	if (unlikely(before(cur_ack32, old_ack32)))
 754		return cur_ack + (1LL << 32);
 755	return cur_ack;
 756}
 757
 758static void update_una(struct mptcp_sock *msk,
 759		       struct mptcp_options_received *mp_opt)
 760{
 761	u64 new_snd_una, snd_una, old_snd_una = atomic64_read(&msk->snd_una);
 762	u64 write_seq = READ_ONCE(msk->write_seq);
 
 
 
 
 
 
 
 
 
 
 
 763
 764	/* avoid ack expansion on update conflict, to reduce the risk of
 765	 * wrongly expanding to a future ack sequence number, which is way
 766	 * more dangerous than missing an ack
 767	 */
 768	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
 
 769
 770	/* ACK for data not even sent yet? Ignore. */
 771	if (after64(new_snd_una, write_seq))
 772		new_snd_una = old_snd_una;
 773
 774	while (after64(new_snd_una, old_snd_una)) {
 775		snd_una = old_snd_una;
 776		old_snd_una = atomic64_cmpxchg(&msk->snd_una, snd_una,
 777					       new_snd_una);
 778		if (old_snd_una == snd_una) {
 779			mptcp_data_acked((struct sock *)msk);
 780			break;
 781		}
 
 
 
 
 782	}
 
 
 
 
 
 783}
 784
 785bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
 786{
 787	/* Skip if DATA_FIN was already received.
 788	 * If updating simultaneously with the recvmsg loop, values
 789	 * should match. If they mismatch, the peer is misbehaving and
 790	 * we will prefer the most recent information.
 791	 */
 792	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
 793		return false;
 794
 795	WRITE_ONCE(msk->rcv_data_fin_seq,
 796		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
 797	WRITE_ONCE(msk->rcv_data_fin, 1);
 798
 799	return true;
 800}
 801
 802static bool add_addr_hmac_valid(struct mptcp_sock *msk,
 803				struct mptcp_options_received *mp_opt)
 804{
 805	u64 hmac = 0;
 806
 807	if (mp_opt->echo)
 808		return true;
 809
 810	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
 811		hmac = add_addr_generate_hmac(msk->remote_key,
 812					      msk->local_key,
 813					      mp_opt->addr_id, &mp_opt->addr);
 814#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 815	else
 816		hmac = add_addr6_generate_hmac(msk->remote_key,
 817					       msk->local_key,
 818					       mp_opt->addr_id, &mp_opt->addr6);
 819#endif
 820
 821	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
 822		 msk, (unsigned long long)hmac,
 823		 (unsigned long long)mp_opt->ahmac);
 824
 825	return hmac == mp_opt->ahmac;
 826}
 827
 828void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 829			    struct tcp_options_received *opt_rx)
 830{
 831	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 832	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 833	struct mptcp_options_received mp_opt;
 834	struct mptcp_ext *mpext;
 835
 836	if (__mptcp_check_fallback(msk))
 837		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 838
 839	mptcp_get_options(skb, &mp_opt);
 
 
 
 
 840	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
 841		return;
 842
 843	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
 844		struct mptcp_addr_info addr;
 
 
 
 
 
 845
 846		addr.port = htons(mp_opt.port);
 847		addr.id = mp_opt.addr_id;
 848		if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
 849			addr.family = AF_INET;
 850			addr.addr = mp_opt.addr;
 
 
 
 
 
 
 
 
 851		}
 852#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 853		else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
 854			addr.family = AF_INET6;
 855			addr.addr6 = mp_opt.addr6;
 
 
 
 856		}
 857#endif
 858		if (!mp_opt.echo)
 859			mptcp_pm_add_addr_received(msk, &addr);
 860		mp_opt.add_addr = 0;
 861	}
 862
 863	if (!mp_opt.dss)
 864		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 865
 866	/* we can't wait for recvmsg() to update the ack_seq, otherwise
 867	 * monodirectional flows will stuck
 868	 */
 869	if (mp_opt.use_ack)
 870		update_una(msk, &mp_opt);
 871
 872	/* Zero-data-length packets are dropped by the caller and not
 873	 * propagated to the MPTCP layer, so the skb extension does not
 874	 * need to be allocated or populated. DATA_FIN information, if
 875	 * present, needs to be updated here before the skb is freed.
 876	 */
 877	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
 878		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
 879		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
 880		    schedule_work(&msk->work))
 881			sock_hold(subflow->conn);
 882
 883		return;
 884	}
 885
 886	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 887	if (!mpext)
 888		return;
 889
 890	memset(mpext, 0, sizeof(*mpext));
 891
 892	if (mp_opt.use_map) {
 893		if (mp_opt.mpc_map) {
 894			/* this is an MP_CAPABLE carrying MPTCP data
 895			 * we know this map the first chunk of data
 896			 */
 897			mptcp_crypto_key_sha(subflow->remote_key, NULL,
 898					     &mpext->data_seq);
 899			mpext->data_seq++;
 900			mpext->subflow_seq = 1;
 901			mpext->dsn64 = 1;
 902			mpext->mpc_map = 1;
 903			mpext->data_fin = 0;
 904		} else {
 905			mpext->data_seq = mp_opt.data_seq;
 906			mpext->subflow_seq = mp_opt.subflow_seq;
 907			mpext->dsn64 = mp_opt.dsn64;
 908			mpext->data_fin = mp_opt.data_fin;
 909		}
 910		mpext->data_len = mp_opt.data_len;
 911		mpext->use_map = 1;
 
 
 
 
 912	}
 
 
 913}
 914
 915void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
 916{
 917	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
 918	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
 919		u8 len;
 
 
 
 920
 921		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
 922			len = TCPOLEN_MPTCP_MPC_SYN;
 923		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
 924			len = TCPOLEN_MPTCP_MPC_SYNACK;
 925		else if (opts->ext_copy.data_len)
 926			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
 927		else
 928			len = TCPOLEN_MPTCP_MPC_ACK;
 929
 930		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
 931				      MPTCP_SUPPORTED_VERSION,
 932				      MPTCP_CAP_HMAC_SHA256);
 933
 934		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
 935		    opts->suboptions))
 936			goto mp_capable_done;
 937
 938		put_unaligned_be64(opts->sndr_key, ptr);
 939		ptr += 2;
 940		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
 941			goto mp_capable_done;
 942
 943		put_unaligned_be64(opts->rcvr_key, ptr);
 944		ptr += 2;
 945		if (!opts->ext_copy.data_len)
 946			goto mp_capable_done;
 947
 948		put_unaligned_be32(opts->ext_copy.data_len << 16 |
 949				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
 950		ptr += 1;
 951	}
 952
 953mp_capable_done:
 954	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
 955		if (opts->ahmac)
 956			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
 957					      TCPOLEN_MPTCP_ADD_ADDR, 0,
 958					      opts->addr_id);
 959		else
 960			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
 961					      TCPOLEN_MPTCP_ADD_ADDR_BASE,
 962					      MPTCP_ADDR_ECHO,
 963					      opts->addr_id);
 964		memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
 965		ptr += 1;
 966		if (opts->ahmac) {
 967			put_unaligned_be64(opts->ahmac, ptr);
 968			ptr += 2;
 969		}
 
 970	}
 971
 972#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 973	if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
 974		if (opts->ahmac)
 975			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
 976					      TCPOLEN_MPTCP_ADD_ADDR6, 0,
 977					      opts->addr_id);
 978		else
 979			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
 980					      TCPOLEN_MPTCP_ADD_ADDR6_BASE,
 981					      MPTCP_ADDR_ECHO,
 982					      opts->addr_id);
 983		memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
 984		ptr += 4;
 985		if (opts->ahmac) {
 986			put_unaligned_be64(opts->ahmac, ptr);
 987			ptr += 2;
 988		}
 989	}
 990#endif
 991
 992	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
 993		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
 994				      TCPOLEN_MPTCP_RM_ADDR_BASE,
 995				      0, opts->rm_id);
 996	}
 
 
 
 
 
 997
 998	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
 999		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1000				      TCPOLEN_MPTCP_MPJ_SYN,
1001				      opts->backup, opts->join_id);
1002		put_unaligned_be32(opts->token, ptr);
1003		ptr += 1;
1004		put_unaligned_be32(opts->nonce, ptr);
1005		ptr += 1;
1006	}
1007
1008	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1009		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1010				      TCPOLEN_MPTCP_MPJ_SYNACK,
1011				      opts->backup, opts->join_id);
1012		put_unaligned_be64(opts->thmac, ptr);
1013		ptr += 2;
1014		put_unaligned_be32(opts->nonce, ptr);
1015		ptr += 1;
1016	}
 
1017
1018	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1019		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1020				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1021		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1022		ptr += 5;
1023	}
1024
1025	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1026		struct mptcp_ext *mpext = &opts->ext_copy;
1027		u8 len = TCPOLEN_MPTCP_DSS_BASE;
1028		u8 flags = 0;
1029
1030		if (mpext->use_ack) {
1031			flags = MPTCP_DSS_HAS_ACK;
1032			if (mpext->ack64) {
1033				len += TCPOLEN_MPTCP_DSS_ACK64;
1034				flags |= MPTCP_DSS_ACK64;
1035			} else {
1036				len += TCPOLEN_MPTCP_DSS_ACK32;
1037			}
1038		}
1039
1040		if (mpext->use_map) {
1041			len += TCPOLEN_MPTCP_DSS_MAP64;
1042
1043			/* Use only 64-bit mapping flags for now, add
1044			 * support for optional 32-bit mappings later.
1045			 */
1046			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1047			if (mpext->data_fin)
1048				flags |= MPTCP_DSS_DATA_FIN;
 
 
 
1049		}
1050
1051		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1052
1053		if (mpext->use_ack) {
1054			if (mpext->ack64) {
1055				put_unaligned_be64(mpext->data_ack, ptr);
1056				ptr += 2;
1057			} else {
1058				put_unaligned_be32(mpext->data_ack32, ptr);
1059				ptr += 1;
1060			}
1061		}
1062
1063		if (mpext->use_map) {
1064			put_unaligned_be64(mpext->data_seq, ptr);
1065			ptr += 2;
1066			put_unaligned_be32(mpext->subflow_seq, ptr);
1067			ptr += 1;
1068			put_unaligned_be32(mpext->data_len << 16 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1069					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1070		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1071	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1072}
v6.8
   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 <crypto/sha2.h>
  11#include <net/tcp.h>
  12#include <net/mptcp.h>
  13#include "protocol.h"
  14#include "mib.h"
  15
  16#include <trace/events/mptcp.h>
  17
  18static bool mptcp_cap_flag_sha256(u8 flags)
  19{
  20	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
  21}
  22
  23static void mptcp_parse_option(const struct sk_buff *skb,
  24			       const unsigned char *ptr, int opsize,
  25			       struct mptcp_options_received *mp_opt)
  26{
  27	u8 subtype = *ptr >> 4;
  28	int expected_opsize;
  29	u16 subopt;
  30	u8 version;
  31	u8 flags;
  32	u8 i;
  33
  34	switch (subtype) {
  35	case MPTCPOPT_MP_CAPABLE:
  36		/* strict size checking */
  37		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  38			if (skb->len > tcp_hdr(skb)->doff << 2)
  39				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
  40			else
  41				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
  42			subopt = OPTION_MPTCP_MPC_ACK;
  43		} else {
  44			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
  45				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
  46				subopt = OPTION_MPTCP_MPC_SYNACK;
  47			} else {
  48				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
  49				subopt = OPTION_MPTCP_MPC_SYN;
  50			}
  51		}
  52
  53		/* Cfr RFC 8684 Section 3.3.0:
  54		 * If a checksum is present but its use had
  55		 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
  56		 * close the subflow with a RST, as it is not behaving as negotiated.
  57		 * If a checksum is not present when its use has been negotiated, the
  58		 * receiver MUST close the subflow with a RST, as it is considered
  59		 * broken
  60		 * We parse even option with mismatching csum presence, so that
  61		 * later in subflow_data_ready we can trigger the reset.
  62		 */
  63		if (opsize != expected_opsize &&
  64		    (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
  65		     opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
  66			break;
  67
  68		/* try to be gentle vs future versions on the initial syn */
  69		version = *ptr++ & MPTCP_VERSION_MASK;
  70		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
  71			if (version != MPTCP_SUPPORTED_VERSION)
  72				break;
  73		} else if (version < MPTCP_SUPPORTED_VERSION) {
  74			break;
  75		}
  76
  77		flags = *ptr++;
  78		if (!mptcp_cap_flag_sha256(flags) ||
  79		    (flags & MPTCP_CAP_EXTENSIBILITY))
  80			break;
  81
  82		/* RFC 6824, Section 3.1:
  83		 * "For the Checksum Required bit (labeled "A"), if either
  84		 * host requires the use of checksums, checksums MUST be used.
  85		 * In other words, the only way for checksums not to be used
  86		 * is if both hosts in their SYNs set A=0."
 
 
 
 
 
 
 
  87		 */
  88		if (flags & MPTCP_CAP_CHECKSUM_REQD)
  89			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
  90
  91		mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
  92
  93		mp_opt->suboptions |= subopt;
  94		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
  95			mp_opt->sndr_key = get_unaligned_be64(ptr);
  96			ptr += 8;
  97		}
  98		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
  99			mp_opt->rcvr_key = get_unaligned_be64(ptr);
 100			ptr += 8;
 101		}
 102		if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
 103			/* Section 3.1.:
 104			 * "the data parameters in a MP_CAPABLE are semantically
 105			 * equivalent to those in a DSS option and can be used
 106			 * interchangeably."
 107			 */
 108			mp_opt->suboptions |= OPTION_MPTCP_DSS;
 109			mp_opt->use_map = 1;
 110			mp_opt->mpc_map = 1;
 111			mp_opt->use_ack = 0;
 112			mp_opt->data_len = get_unaligned_be16(ptr);
 113			ptr += 2;
 114		}
 115		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
 116			mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
 117			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
 118			ptr += 2;
 119		}
 120		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
 121			 version, flags, opsize, mp_opt->sndr_key,
 122			 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
 123		break;
 124
 125	case MPTCPOPT_MP_JOIN:
 
 126		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
 127			mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
 128			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
 129			mp_opt->join_id = *ptr++;
 130			mp_opt->token = get_unaligned_be32(ptr);
 131			ptr += 4;
 132			mp_opt->nonce = get_unaligned_be32(ptr);
 133			ptr += 4;
 134			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
 135				 mp_opt->backup, mp_opt->join_id,
 136				 mp_opt->token, mp_opt->nonce);
 137		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
 138			mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
 139			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
 140			mp_opt->join_id = *ptr++;
 141			mp_opt->thmac = get_unaligned_be64(ptr);
 142			ptr += 8;
 143			mp_opt->nonce = get_unaligned_be32(ptr);
 144			ptr += 4;
 145			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
 146				 mp_opt->backup, mp_opt->join_id,
 147				 mp_opt->thmac, mp_opt->nonce);
 148		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
 149			mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
 150			ptr += 2;
 151			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
 152			pr_debug("MP_JOIN hmac");
 
 
 
 153		}
 154		break;
 155
 156	case MPTCPOPT_DSS:
 157		pr_debug("DSS");
 158		ptr++;
 159
 160		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
 161		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
 162		 * map info accordingly
 163		 */
 164		mp_opt->mpc_map = 0;
 165		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
 166		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
 167		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
 168		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
 169		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
 170		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
 171
 172		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
 173			 mp_opt->data_fin, mp_opt->dsn64,
 174			 mp_opt->use_map, mp_opt->ack64,
 175			 mp_opt->use_ack);
 176
 177		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
 178
 179		if (mp_opt->use_ack) {
 180			if (mp_opt->ack64)
 181				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
 182			else
 183				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
 184		}
 185
 186		if (mp_opt->use_map) {
 187			if (mp_opt->dsn64)
 188				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
 189			else
 190				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
 191		}
 192
 193		/* Always parse any csum presence combination, we will enforce
 194		 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
 
 
 195		 */
 196		if (opsize != expected_opsize &&
 197		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
 198			break;
 199
 200		mp_opt->suboptions |= OPTION_MPTCP_DSS;
 
 201		if (mp_opt->use_ack) {
 202			if (mp_opt->ack64) {
 203				mp_opt->data_ack = get_unaligned_be64(ptr);
 204				ptr += 8;
 205			} else {
 206				mp_opt->data_ack = get_unaligned_be32(ptr);
 207				ptr += 4;
 208			}
 209
 210			pr_debug("data_ack=%llu", mp_opt->data_ack);
 211		}
 212
 213		if (mp_opt->use_map) {
 214			if (mp_opt->dsn64) {
 215				mp_opt->data_seq = get_unaligned_be64(ptr);
 216				ptr += 8;
 217			} else {
 218				mp_opt->data_seq = get_unaligned_be32(ptr);
 219				ptr += 4;
 220			}
 221
 222			mp_opt->subflow_seq = get_unaligned_be32(ptr);
 223			ptr += 4;
 224
 225			mp_opt->data_len = get_unaligned_be16(ptr);
 226			ptr += 2;
 227
 228			if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
 229				mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
 230				mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
 231				ptr += 2;
 232			}
 233
 234			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
 235				 mp_opt->data_seq, mp_opt->subflow_seq,
 236				 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
 237				 mp_opt->csum);
 238		}
 239
 240		break;
 241
 242	case MPTCPOPT_ADD_ADDR:
 243		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
 244		if (!mp_opt->echo) {
 245			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
 246			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
 247				mp_opt->addr.family = AF_INET;
 248#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 249			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
 250				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
 251				mp_opt->addr.family = AF_INET6;
 252#endif
 253			else
 254				break;
 255		} else {
 256			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
 257			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
 258				mp_opt->addr.family = AF_INET;
 259#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 260			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
 261				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
 262				mp_opt->addr.family = AF_INET6;
 263#endif
 264			else
 265				break;
 266		}
 267
 268		mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
 269		mp_opt->addr.id = *ptr++;
 270		mp_opt->addr.port = 0;
 271		mp_opt->ahmac = 0;
 272		if (mp_opt->addr.family == AF_INET) {
 273			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
 274			ptr += 4;
 275			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
 276			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
 277				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
 278				ptr += 2;
 279			}
 280		}
 281#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 282		else {
 283			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
 284			ptr += 16;
 285			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
 286			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
 287				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
 288				ptr += 2;
 289			}
 290		}
 291#endif
 292		if (!mp_opt->echo) {
 293			mp_opt->ahmac = get_unaligned_be64(ptr);
 294			ptr += 8;
 295		}
 296		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
 297			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
 298			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
 299		break;
 300
 301	case MPTCPOPT_RM_ADDR:
 302		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
 303		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
 304			break;
 305
 306		ptr++;
 307
 308		mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
 309		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
 310		for (i = 0; i < mp_opt->rm_list.nr; i++)
 311			mp_opt->rm_list.ids[i] = *ptr++;
 312		pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
 313		break;
 314
 315	case MPTCPOPT_MP_PRIO:
 316		if (opsize != TCPOLEN_MPTCP_PRIO)
 317			break;
 318
 319		mp_opt->suboptions |= OPTION_MPTCP_PRIO;
 320		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
 321		pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
 322		break;
 323
 324	case MPTCPOPT_MP_FASTCLOSE:
 325		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
 326			break;
 327
 328		ptr += 2;
 329		mp_opt->rcvr_key = get_unaligned_be64(ptr);
 330		ptr += 8;
 331		mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
 332		pr_debug("MP_FASTCLOSE: recv_key=%llu", mp_opt->rcvr_key);
 333		break;
 334
 335	case MPTCPOPT_RST:
 336		if (opsize != TCPOLEN_MPTCP_RST)
 337			break;
 338
 339		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
 340			break;
 341
 342		mp_opt->suboptions |= OPTION_MPTCP_RST;
 343		flags = *ptr++;
 344		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
 345		mp_opt->reset_reason = *ptr;
 346		pr_debug("MP_RST: transient=%u reason=%u",
 347			 mp_opt->reset_transient, mp_opt->reset_reason);
 348		break;
 349
 350	case MPTCPOPT_MP_FAIL:
 351		if (opsize != TCPOLEN_MPTCP_FAIL)
 352			break;
 353
 354		ptr += 2;
 355		mp_opt->suboptions |= OPTION_MPTCP_FAIL;
 356		mp_opt->fail_seq = get_unaligned_be64(ptr);
 357		pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
 358		break;
 359
 360	default:
 361		break;
 362	}
 363}
 364
 365void mptcp_get_options(const struct sk_buff *skb,
 366		       struct mptcp_options_received *mp_opt)
 367{
 368	const struct tcphdr *th = tcp_hdr(skb);
 369	const unsigned char *ptr;
 370	int length;
 371
 372	/* initialize option status */
 373	mp_opt->suboptions = 0;
 
 
 
 
 374
 375	length = (th->doff * 4) - sizeof(struct tcphdr);
 376	ptr = (const unsigned char *)(th + 1);
 377
 378	while (length > 0) {
 379		int opcode = *ptr++;
 380		int opsize;
 381
 382		switch (opcode) {
 383		case TCPOPT_EOL:
 384			return;
 385		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
 386			length--;
 387			continue;
 388		default:
 389			if (length < 2)
 390				return;
 391			opsize = *ptr++;
 392			if (opsize < 2) /* "silly options" */
 393				return;
 394			if (opsize > length)
 395				return;	/* don't parse partial options */
 396			if (opcode == TCPOPT_MPTCP)
 397				mptcp_parse_option(skb, ptr, opsize, mp_opt);
 398			ptr += opsize - 2;
 399			length -= opsize;
 400		}
 401	}
 402}
 403
 404bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
 405		       unsigned int *size, struct mptcp_out_options *opts)
 406{
 407	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 408
 409	/* we will use snd_isn to detect first pkt [re]transmission
 410	 * in mptcp_established_options_mp()
 411	 */
 412	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
 413	if (subflow->request_mptcp) {
 414		opts->suboptions = OPTION_MPTCP_MPC_SYN;
 415		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
 416		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
 417		*size = TCPOLEN_MPTCP_MPC_SYN;
 418		return true;
 419	} else if (subflow->request_join) {
 420		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
 421			 subflow->local_nonce);
 422		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
 423		opts->join_id = subflow->local_id;
 424		opts->token = subflow->remote_token;
 425		opts->nonce = subflow->local_nonce;
 426		opts->backup = subflow->request_bkup;
 427		*size = TCPOLEN_MPTCP_MPJ_SYN;
 428		return true;
 429	}
 430	return false;
 431}
 432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 433static void clear_3rdack_retransmission(struct sock *sk)
 434{
 435	struct inet_connection_sock *icsk = inet_csk(sk);
 436
 437	sk_stop_timer(sk, &icsk->icsk_delack_timer);
 438	icsk->icsk_ack.timeout = 0;
 439	icsk->icsk_ack.ato = 0;
 440	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
 441}
 442
 443static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
 444					 bool snd_data_fin_enable,
 445					 unsigned int *size,
 
 446					 struct mptcp_out_options *opts)
 447{
 448	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 449	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 450	struct mptcp_ext *mpext;
 451	unsigned int data_len;
 452	u8 len;
 453
 454	/* When skb is not available, we better over-estimate the emitted
 455	 * options len. A full DSS option (28 bytes) is longer than
 456	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
 457	 * tell the caller to defer the estimate to
 458	 * mptcp_established_options_dss(), which will reserve enough space.
 459	 */
 460	if (!skb)
 461		return false;
 462
 463	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
 464	if (subflow->fully_established || snd_data_fin_enable ||
 465	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
 466	    sk->sk_state != TCP_ESTABLISHED)
 467		return false;
 468
 469	if (subflow->mp_capable) {
 470		mpext = mptcp_get_ext(skb);
 471		data_len = mpext ? mpext->data_len : 0;
 472
 473		/* we will check ops->data_len in mptcp_write_options() to
 474		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
 475		 * TCPOLEN_MPTCP_MPC_ACK
 476		 */
 477		opts->data_len = data_len;
 478		opts->suboptions = OPTION_MPTCP_MPC_ACK;
 479		opts->sndr_key = subflow->local_key;
 480		opts->rcvr_key = subflow->remote_key;
 481		opts->csum_reqd = READ_ONCE(msk->csum_enabled);
 482		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
 483
 484		/* Section 3.1.
 485		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
 486		 * packets that start the first subflow of an MPTCP connection,
 487		 * as well as the first packet that carries data
 488		 */
 489		if (data_len > 0) {
 490			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
 491			if (opts->csum_reqd) {
 492				/* we need to propagate more info to csum the pseudo hdr */
 493				opts->data_seq = mpext->data_seq;
 494				opts->subflow_seq = mpext->subflow_seq;
 495				opts->csum = mpext->csum;
 496				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
 497			}
 498			*size = ALIGN(len, 4);
 499		} else {
 500			*size = TCPOLEN_MPTCP_MPC_ACK;
 501		}
 502
 503		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
 504			 subflow, subflow->local_key, subflow->remote_key,
 505			 data_len);
 506
 507		return true;
 508	} else if (subflow->mp_join) {
 509		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
 510		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
 511		*size = TCPOLEN_MPTCP_MPJ_ACK;
 512		pr_debug("subflow=%p", subflow);
 513
 514		/* we can use the full delegate action helper only from BH context
 515		 * If we are in process context - sk is flushing the backlog at
 516		 * socket lock release time - just set the appropriate flag, will
 517		 * be handled by the release callback
 518		 */
 519		if (sock_owned_by_user(sk))
 520			set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
 521		else
 522			mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
 523		return true;
 524	}
 525	return false;
 526}
 527
 528static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
 529				 struct sk_buff *skb, struct mptcp_ext *ext)
 530{
 531	/* The write_seq value has already been incremented, so the actual
 532	 * sequence number for the DATA_FIN is one less.
 533	 */
 534	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
 535
 536	if (!ext->use_map || !skb->len) {
 537		/* RFC6824 requires a DSS mapping with specific values
 538		 * if DATA_FIN is set but no data payload is mapped
 539		 */
 540		ext->data_fin = 1;
 541		ext->use_map = 1;
 542		ext->dsn64 = 1;
 543		ext->data_seq = data_fin_tx_seq;
 544		ext->subflow_seq = 0;
 545		ext->data_len = 1;
 546	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
 547		/* If there's an existing DSS mapping and it is the
 548		 * final mapping, DATA_FIN consumes 1 additional byte of
 549		 * mapping space.
 550		 */
 551		ext->data_fin = 1;
 552		ext->data_len++;
 553	}
 554}
 555
 556static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 557					  bool snd_data_fin_enable,
 558					  unsigned int *size,
 
 559					  struct mptcp_out_options *opts)
 560{
 561	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 562	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 563	unsigned int dss_size = 0;
 
 564	struct mptcp_ext *mpext;
 565	unsigned int ack_size;
 566	bool ret = false;
 567	u64 ack_seq;
 568
 569	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
 570	mpext = skb ? mptcp_get_ext(skb) : NULL;
 
 571
 572	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
 573		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
 574
 575		if (mpext) {
 576			if (opts->csum_reqd)
 577				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
 578
 
 
 
 579			opts->ext_copy = *mpext;
 580		}
 581
 582		dss_size = map_size;
 583		if (skb && snd_data_fin_enable)
 584			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
 585		opts->suboptions = OPTION_MPTCP_DSS;
 586		ret = true;
 587	}
 588
 589	/* passive sockets msk will set the 'can_ack' after accept(), even
 590	 * if the first subflow may have the already the remote key handy
 591	 */
 592	opts->ext_copy.use_ack = 0;
 593	if (!READ_ONCE(msk->can_ack)) {
 594		*size = ALIGN(dss_size, 4);
 595		return ret;
 596	}
 597
 598	ack_seq = READ_ONCE(msk->ack_seq);
 599	if (READ_ONCE(msk->use_64bit_ack)) {
 600		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
 601		opts->ext_copy.data_ack = ack_seq;
 602		opts->ext_copy.ack64 = 1;
 603	} else {
 604		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
 605		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
 606		opts->ext_copy.ack64 = 0;
 607	}
 608	opts->ext_copy.use_ack = 1;
 609	opts->suboptions = OPTION_MPTCP_DSS;
 610	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
 611
 612	/* Add kind/length/subtype/flag overhead if mapping is not populated */
 613	if (dss_size == 0)
 614		ack_size += TCPOLEN_MPTCP_DSS_BASE;
 615
 616	dss_size += ack_size;
 617
 618	*size = ALIGN(dss_size, 4);
 619	return true;
 620}
 621
 622static u64 add_addr_generate_hmac(u64 key1, u64 key2,
 623				  struct mptcp_addr_info *addr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 624{
 625	u16 port = ntohs(addr->port);
 626	u8 hmac[SHA256_DIGEST_SIZE];
 627	u8 msg[19];
 628	int i = 0;
 629
 630	msg[i++] = addr->id;
 631	if (addr->family == AF_INET) {
 632		memcpy(&msg[i], &addr->addr.s_addr, 4);
 633		i += 4;
 634	}
 635#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 636	else if (addr->family == AF_INET6) {
 637		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
 638		i += 16;
 639	}
 640#endif
 641	msg[i++] = port >> 8;
 642	msg[i++] = port & 0xFF;
 643
 644	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
 645
 646	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
 647}
 
 648
 649static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
 650					       unsigned int *size,
 651					       unsigned int remaining,
 652					       struct mptcp_out_options *opts)
 653{
 654	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 655	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 656	bool drop_other_suboptions = false;
 657	unsigned int opt_size = *size;
 658	bool echo;
 659	int len;
 660
 661	/* add addr will strip the existing options, be sure to avoid breaking
 662	 * MPC/MPJ handshakes
 663	 */
 664	if (!mptcp_pm_should_add_signal(msk) ||
 665	    (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
 666	    !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
 667		    &echo, &drop_other_suboptions))
 668		return false;
 669
 670	if (drop_other_suboptions)
 671		remaining += opt_size;
 672	len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
 673	if (remaining < len)
 674		return false;
 675
 676	*size = len;
 677	if (drop_other_suboptions) {
 678		pr_debug("drop other suboptions");
 679		opts->suboptions = 0;
 680
 681		/* note that e.g. DSS could have written into the memory
 682		 * aliased by ahmac, we must reset the field here
 683		 * to avoid appending the hmac even for ADD_ADDR echo
 684		 * options
 685		 */
 686		opts->ahmac = 0;
 687		*size -= opt_size;
 688	}
 689	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
 690	if (!echo) {
 691		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
 692		opts->ahmac = add_addr_generate_hmac(msk->local_key,
 693						     msk->remote_key,
 
 694						     &opts->addr);
 695	} else {
 696		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
 697	}
 698	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
 699		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
 700
 701	return true;
 702}
 703
 704static bool mptcp_established_options_rm_addr(struct sock *sk,
 705					      unsigned int *size,
 706					      unsigned int remaining,
 707					      struct mptcp_out_options *opts)
 708{
 709	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 710	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 711	struct mptcp_rm_list rm_list;
 712	int i, len;
 713
 714	if (!mptcp_pm_should_rm_signal(msk) ||
 715	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
 716		return false;
 717
 718	len = mptcp_rm_addr_len(&rm_list);
 719	if (len < 0)
 720		return false;
 721	if (remaining < len)
 722		return false;
 723
 724	*size = len;
 725	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
 726	opts->rm_list = rm_list;
 727
 728	for (i = 0; i < opts->rm_list.nr; i++)
 729		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
 730	MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
 731	return true;
 732}
 733
 734static bool mptcp_established_options_mp_prio(struct sock *sk,
 735					      unsigned int *size,
 736					      unsigned int remaining,
 737					      struct mptcp_out_options *opts)
 738{
 739	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 740
 741	/* can't send MP_PRIO with MPC, as they share the same option space:
 742	 * 'backup'. Also it makes no sense at all
 743	 */
 744	if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
 745		return false;
 746
 747	/* account for the trailing 'nop' option */
 748	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
 749		return false;
 750
 751	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
 752	opts->suboptions |= OPTION_MPTCP_PRIO;
 753	opts->backup = subflow->request_bkup;
 754
 755	pr_debug("prio=%d", opts->backup);
 756
 757	return true;
 758}
 759
 760static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
 761						   unsigned int *size,
 762						   unsigned int remaining,
 763						   struct mptcp_out_options *opts)
 764{
 765	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 766
 767	if (remaining < TCPOLEN_MPTCP_RST)
 768		return false;
 769
 770	*size = TCPOLEN_MPTCP_RST;
 771	opts->suboptions |= OPTION_MPTCP_RST;
 772	opts->reset_transient = subflow->reset_transient;
 773	opts->reset_reason = subflow->reset_reason;
 774	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
 775
 776	return true;
 777}
 778
 779static bool mptcp_established_options_fastclose(struct sock *sk,
 780						unsigned int *size,
 781						unsigned int remaining,
 782						struct mptcp_out_options *opts)
 783{
 784	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 785	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 786
 787	if (likely(!subflow->send_fastclose))
 788		return false;
 789
 790	if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
 791		return false;
 792
 793	*size = TCPOLEN_MPTCP_FASTCLOSE;
 794	opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
 795	opts->rcvr_key = msk->remote_key;
 796
 797	pr_debug("FASTCLOSE key=%llu", opts->rcvr_key);
 798	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
 799	return true;
 800}
 801
 802static bool mptcp_established_options_mp_fail(struct sock *sk,
 803					      unsigned int *size,
 804					      unsigned int remaining,
 805					      struct mptcp_out_options *opts)
 806{
 807	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 808
 809	if (likely(!subflow->send_mp_fail))
 810		return false;
 811
 812	if (remaining < TCPOLEN_MPTCP_FAIL)
 813		return false;
 814
 815	*size = TCPOLEN_MPTCP_FAIL;
 816	opts->suboptions |= OPTION_MPTCP_FAIL;
 817	opts->fail_seq = subflow->map_seq;
 818
 819	pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
 820	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
 821
 822	return true;
 823}
 824
 825bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
 826			       unsigned int *size, unsigned int remaining,
 827			       struct mptcp_out_options *opts)
 828{
 829	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 830	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 831	unsigned int opt_size = 0;
 832	bool snd_data_fin;
 833	bool ret = false;
 834
 835	opts->suboptions = 0;
 836
 837	if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
 838		return false;
 839
 840	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
 841		if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
 842		    mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
 843			*size += opt_size;
 844			remaining -= opt_size;
 845		}
 846		/* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
 847		if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
 848			*size += opt_size;
 849			remaining -= opt_size;
 850		}
 851		return true;
 852	}
 853
 854	snd_data_fin = mptcp_data_fin_enabled(msk);
 855	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
 856		ret = true;
 857	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
 858		unsigned int mp_fail_size;
 859
 860		ret = true;
 861		if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
 862						      remaining - opt_size, opts)) {
 863			*size += opt_size + mp_fail_size;
 864			remaining -= opt_size - mp_fail_size;
 865			return true;
 866		}
 867	}
 868
 869	/* we reserved enough space for the above options, and exceeding the
 870	 * TCP option space would be fatal
 871	 */
 872	if (WARN_ON_ONCE(opt_size > remaining))
 873		return false;
 874
 875	*size += opt_size;
 876	remaining -= opt_size;
 877	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
 878		*size += opt_size;
 879		remaining -= opt_size;
 880		ret = true;
 881	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
 882		*size += opt_size;
 883		remaining -= opt_size;
 884		ret = true;
 885	}
 886
 887	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
 888		*size += opt_size;
 889		remaining -= opt_size;
 890		ret = true;
 891	}
 892
 893	return ret;
 894}
 895
 896bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
 897			  struct mptcp_out_options *opts)
 898{
 899	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
 900
 901	if (subflow_req->mp_capable) {
 902		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
 903		opts->sndr_key = subflow_req->local_key;
 904		opts->csum_reqd = subflow_req->csum_reqd;
 905		opts->allow_join_id0 = subflow_req->allow_join_id0;
 906		*size = TCPOLEN_MPTCP_MPC_SYNACK;
 907		pr_debug("subflow_req=%p, local_key=%llu",
 908			 subflow_req, subflow_req->local_key);
 909		return true;
 910	} else if (subflow_req->mp_join) {
 911		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
 912		opts->backup = subflow_req->backup;
 913		opts->join_id = subflow_req->local_id;
 914		opts->thmac = subflow_req->thmac;
 915		opts->nonce = subflow_req->local_nonce;
 916		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
 917			 subflow_req, opts->backup, opts->join_id,
 918			 opts->thmac, opts->nonce);
 919		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
 920		return true;
 921	}
 922	return false;
 923}
 924
 925static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
 926				    struct mptcp_subflow_context *subflow,
 927				    struct sk_buff *skb,
 928				    struct mptcp_options_received *mp_opt)
 929{
 930	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
 931	 * will make the subflow fully established
 932	 */
 933	if (likely(subflow->fully_established)) {
 934		/* on passive sockets, check for 3rd ack retransmission
 935		 * note that msk is always set by subflow_syn_recv_sock()
 936		 * for mp_join subflows
 937		 */
 938		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
 939		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
 940		    subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
 941		    !subflow->request_join)
 942			tcp_send_ack(ssk);
 943		goto check_notify;
 944	}
 945
 946	/* we must process OoO packets before the first subflow is fully
 947	 * established. OoO packets are instead a protocol violation
 948	 * for MP_JOIN subflows as the peer must not send any data
 949	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
 950	 */
 951	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
 952		if (subflow->mp_join)
 953			goto reset;
 954		if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
 955			goto set_fully_established;
 956		return subflow->mp_capable;
 957	}
 958
 959	if (subflow->remote_key_valid &&
 960	    (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
 961	     ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo))) {
 962		/* subflows are fully established as soon as we get any
 963		 * additional ack, including ADD_ADDR.
 964		 */
 965		goto set_fully_established;
 
 
 966	}
 967
 968	/* If the first established packet does not contain MP_CAPABLE + data
 969	 * then fallback to TCP. Fallback scenarios requires a reset for
 970	 * MP_JOIN subflows.
 971	 */
 972	if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
 973		if (subflow->mp_join)
 974			goto reset;
 975		subflow->mp_capable = 0;
 976		pr_fallback(msk);
 977		mptcp_do_fallback(ssk);
 978		return false;
 979	}
 980
 981	if (mp_opt->deny_join_id0)
 982		WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
 983
 984	if (unlikely(!READ_ONCE(msk->pm.server_side)))
 985		pr_warn_once("bogus mpc option on established client sk");
 
 986
 987set_fully_established:
 988	mptcp_data_lock((struct sock *)msk);
 989	__mptcp_subflow_fully_established(msk, subflow, mp_opt);
 990	mptcp_data_unlock((struct sock *)msk);
 991
 992check_notify:
 993	/* if the subflow is not already linked into the conn_list, we can't
 994	 * notify the PM: this subflow is still on the listener queue
 995	 * and the PM possibly acquiring the subflow lock could race with
 996	 * the listener close
 997	 */
 998	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
 999		return true;
1000
1001	subflow->pm_notified = 1;
1002	if (subflow->mp_join) {
1003		clear_3rdack_retransmission(ssk);
1004		mptcp_pm_subflow_established(msk);
1005	} else {
1006		mptcp_pm_fully_established(msk, ssk);
1007	}
1008	return true;
1009
1010reset:
1011	mptcp_subflow_reset(ssk);
1012	return false;
1013}
1014
1015u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1016{
1017	u32 old_seq32, cur_seq32;
1018
1019	old_seq32 = (u32)old_seq;
1020	cur_seq32 = (u32)cur_seq;
1021	cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1022	if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1023		return cur_seq + (1LL << 32);
1024
1025	/* reverse wrap could happen, too */
1026	if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1027		return cur_seq - (1LL << 32);
1028	return cur_seq;
 
 
1029}
1030
1031static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
 
1032{
1033	msk->bytes_acked += new_snd_una - msk->snd_una;
1034	msk->snd_una = new_snd_una;
1035}
1036
1037static void ack_update_msk(struct mptcp_sock *msk,
1038			   struct sock *ssk,
1039			   struct mptcp_options_received *mp_opt)
1040{
1041	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1042	struct sock *sk = (struct sock *)msk;
1043	u64 old_snd_una;
1044
1045	mptcp_data_lock(sk);
1046
1047	/* avoid ack expansion on update conflict, to reduce the risk of
1048	 * wrongly expanding to a future ack sequence number, which is way
1049	 * more dangerous than missing an ack
1050	 */
1051	old_snd_una = msk->snd_una;
1052	new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1053
1054	/* ACK for data not even sent yet? Ignore.*/
1055	if (unlikely(after64(new_snd_una, snd_nxt)))
1056		new_snd_una = old_snd_una;
1057
1058	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1059
1060	if (after64(new_wnd_end, msk->wnd_end))
1061		msk->wnd_end = new_wnd_end;
1062
1063	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1064	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1065		__mptcp_check_push(sk, ssk);
1066
1067	if (after64(new_snd_una, old_snd_una)) {
1068		__mptcp_snd_una_update(msk, new_snd_una);
1069		__mptcp_data_acked(sk);
1070	}
1071	mptcp_data_unlock(sk);
1072
1073	trace_ack_update_msk(mp_opt->data_ack,
1074			     old_snd_una, new_snd_una,
1075			     new_wnd_end, msk->wnd_end);
1076}
1077
1078bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1079{
1080	/* Skip if DATA_FIN was already received.
1081	 * If updating simultaneously with the recvmsg loop, values
1082	 * should match. If they mismatch, the peer is misbehaving and
1083	 * we will prefer the most recent information.
1084	 */
1085	if (READ_ONCE(msk->rcv_data_fin))
1086		return false;
1087
1088	WRITE_ONCE(msk->rcv_data_fin_seq,
1089		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1090	WRITE_ONCE(msk->rcv_data_fin, 1);
1091
1092	return true;
1093}
1094
1095static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1096				struct mptcp_options_received *mp_opt)
1097{
1098	u64 hmac = 0;
1099
1100	if (mp_opt->echo)
1101		return true;
1102
1103	hmac = add_addr_generate_hmac(msk->remote_key,
1104				      msk->local_key,
1105				      &mp_opt->addr);
 
 
 
 
 
 
 
1106
1107	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1108		 msk, hmac, mp_opt->ahmac);
 
1109
1110	return hmac == mp_opt->ahmac;
1111}
1112
1113/* Return false if a subflow has been reset, else return true */
1114bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1115{
1116	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1117	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1118	struct mptcp_options_received mp_opt;
1119	struct mptcp_ext *mpext;
1120
1121	if (__mptcp_check_fallback(msk)) {
1122		/* Keep it simple and unconditionally trigger send data cleanup and
1123		 * pending queue spooling. We will need to acquire the data lock
1124		 * for more accurate checks, and once the lock is acquired, such
1125		 * helpers are cheap.
1126		 */
1127		mptcp_data_lock(subflow->conn);
1128		if (sk_stream_memory_free(sk))
1129			__mptcp_check_push(subflow->conn, sk);
1130
1131		/* on fallback we just need to ignore the msk-level snd_una, as
1132		 * this is really plain TCP
1133		 */
1134		__mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1135
1136		__mptcp_data_acked(subflow->conn);
1137		mptcp_data_unlock(subflow->conn);
1138		return true;
1139	}
1140
1141	mptcp_get_options(skb, &mp_opt);
1142
1143	/* The subflow can be in close state only if check_fully_established()
1144	 * just sent a reset. If so, tell the caller to ignore the current packet.
1145	 */
1146	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1147		return sk->sk_state != TCP_CLOSE;
1148
1149	if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1150		if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1151		    msk->local_key == mp_opt.rcvr_key) {
1152			WRITE_ONCE(msk->rcv_fastclose, true);
1153			mptcp_schedule_work((struct sock *)msk);
1154			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1155		}
1156
1157		if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1158		    add_addr_hmac_valid(msk, &mp_opt)) {
1159			if (!mp_opt.echo) {
1160				mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1161				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1162			} else {
1163				mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1164				mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1165				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1166			}
1167
1168			if (mp_opt.addr.port)
1169				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1170		}
1171
1172		if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1173			mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1174
1175		if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1176			mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1177			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1178		}
 
 
 
 
 
1179
1180		if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1181			mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1182			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1183		}
1184
1185		if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1186			subflow->reset_seen = 1;
1187			subflow->reset_reason = mp_opt.reset_reason;
1188			subflow->reset_transient = mp_opt.reset_transient;
1189			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1190		}
1191
1192		if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1193			return true;
1194	}
1195
1196	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1197	 * monodirectional flows will stuck
1198	 */
1199	if (mp_opt.use_ack)
1200		ack_update_msk(msk, sk, &mp_opt);
1201
1202	/* Zero-data-length packets are dropped by the caller and not
1203	 * propagated to the MPTCP layer, so the skb extension does not
1204	 * need to be allocated or populated. DATA_FIN information, if
1205	 * present, needs to be updated here before the skb is freed.
1206	 */
1207	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1208		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1209		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1210			mptcp_schedule_work((struct sock *)msk);
 
1211
1212		return true;
1213	}
1214
1215	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1216	if (!mpext)
1217		return true;
1218
1219	memset(mpext, 0, sizeof(*mpext));
1220
1221	if (likely(mp_opt.use_map)) {
1222		if (mp_opt.mpc_map) {
1223			/* this is an MP_CAPABLE carrying MPTCP data
1224			 * we know this map the first chunk of data
1225			 */
1226			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1227					     &mpext->data_seq);
1228			mpext->data_seq++;
1229			mpext->subflow_seq = 1;
1230			mpext->dsn64 = 1;
1231			mpext->mpc_map = 1;
1232			mpext->data_fin = 0;
1233		} else {
1234			mpext->data_seq = mp_opt.data_seq;
1235			mpext->subflow_seq = mp_opt.subflow_seq;
1236			mpext->dsn64 = mp_opt.dsn64;
1237			mpext->data_fin = mp_opt.data_fin;
1238		}
1239		mpext->data_len = mp_opt.data_len;
1240		mpext->use_map = 1;
1241		mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1242
1243		if (mpext->csum_reqd)
1244			mpext->csum = mp_opt.csum;
1245	}
1246
1247	return true;
1248}
1249
1250static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1251{
1252	const struct sock *ssk = (const struct sock *)tp;
1253	struct mptcp_subflow_context *subflow;
1254	u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1255	struct mptcp_sock *msk;
1256	u32 new_win;
1257	u64 win;
1258
1259	subflow = mptcp_subflow_ctx(ssk);
1260	msk = mptcp_sk(subflow->conn);
 
 
 
 
 
 
1261
1262	ack_seq = READ_ONCE(msk->ack_seq);
1263	rcv_wnd_new = ack_seq + tp->rcv_wnd;
 
1264
1265	rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1266	if (after64(rcv_wnd_new, rcv_wnd_old)) {
1267		u64 rcv_wnd;
1268
1269		for (;;) {
1270			rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
 
 
1271
1272			if (rcv_wnd == rcv_wnd_old)
1273				break;
 
 
 
 
 
 
 
1274
1275			rcv_wnd_old = rcv_wnd;
1276			if (before64(rcv_wnd_new, rcv_wnd_old)) {
1277				MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1278				goto raise_win;
1279			}
1280			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
 
 
 
 
 
 
 
 
 
 
1281		}
1282		return;
1283	}
1284
1285	if (rcv_wnd_new != rcv_wnd_old) {
1286raise_win:
1287		win = rcv_wnd_old - ack_seq;
1288		tp->rcv_wnd = min_t(u64, win, U32_MAX);
1289		new_win = tp->rcv_wnd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1290
1291		/* Make sure we do not exceed the maximum possible
1292		 * scaled window.
1293		 */
1294		if (unlikely(th->syn))
1295			new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1296		if (!tp->rx_opt.rcv_wscale &&
1297		    READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1298			new_win = min(new_win, MAX_TCP_WINDOW);
1299		else
1300			new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1301
1302		/* RFC1323 scaling applied */
1303		new_win >>= tp->rx_opt.rcv_wscale;
1304		th->window = htons(new_win);
1305		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1306	}
1307}
1308
1309__sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1310{
1311	struct csum_pseudo_header header;
1312	__wsum csum;
 
 
1313
1314	/* cfr RFC 8684 3.3.1.:
1315	 * the data sequence number used in the pseudo-header is
1316	 * always the 64-bit value, irrespective of what length is used in the
1317	 * DSS option itself.
1318	 */
1319	header.data_seq = cpu_to_be64(data_seq);
1320	header.subflow_seq = htonl(subflow_seq);
1321	header.data_len = htons(data_len);
1322	header.csum = 0;
1323
1324	csum = csum_partial(&header, sizeof(header), sum);
1325	return csum_fold(csum);
1326}
1327
1328static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1329{
1330	return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1331				 ~csum_unfold(mpext->csum));
1332}
1333
1334static void put_len_csum(u16 len, __sum16 csum, void *data)
1335{
1336	__sum16 *sumptr = data + 2;
1337	__be16 *ptr = data;
1338
1339	put_unaligned_be16(len, ptr);
1340
1341	put_unaligned(csum, sumptr);
1342}
1343
1344void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1345			 struct mptcp_out_options *opts)
1346{
1347	const struct sock *ssk = (const struct sock *)tp;
1348	struct mptcp_subflow_context *subflow;
1349
1350	/* Which options can be used together?
1351	 *
1352	 * X: mutually exclusive
1353	 * O: often used together
1354	 * C: can be used together in some cases
1355	 * P: could be used together but we prefer not to (optimisations)
1356	 *
1357	 *  Opt: | MPC  | MPJ  | DSS  | ADD  |  RM  | PRIO | FAIL |  FC  |
1358	 * ------|------|------|------|------|------|------|------|------|
1359	 *  MPC  |------|------|------|------|------|------|------|------|
1360	 *  MPJ  |  X   |------|------|------|------|------|------|------|
1361	 *  DSS  |  X   |  X   |------|------|------|------|------|------|
1362	 *  ADD  |  X   |  X   |  P   |------|------|------|------|------|
1363	 *  RM   |  C   |  C   |  C   |  P   |------|------|------|------|
1364	 *  PRIO |  X   |  C   |  C   |  C   |  C   |------|------|------|
1365	 *  FAIL |  X   |  X   |  C   |  X   |  X   |  X   |------|------|
1366	 *  FC   |  X   |  X   |  X   |  X   |  X   |  X   |  X   |------|
1367	 *  RST  |  X   |  X   |  X   |  X   |  X   |  X   |  O   |  O   |
1368	 * ------|------|------|------|------|------|------|------|------|
1369	 *
1370	 * The same applies in mptcp_established_options() function.
1371	 */
1372	if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1373		struct mptcp_ext *mpext = &opts->ext_copy;
1374		u8 len = TCPOLEN_MPTCP_DSS_BASE;
1375		u8 flags = 0;
1376
1377		if (mpext->use_ack) {
1378			flags = MPTCP_DSS_HAS_ACK;
1379			if (mpext->ack64) {
1380				len += TCPOLEN_MPTCP_DSS_ACK64;
1381				flags |= MPTCP_DSS_ACK64;
1382			} else {
1383				len += TCPOLEN_MPTCP_DSS_ACK32;
1384			}
1385		}
1386
1387		if (mpext->use_map) {
1388			len += TCPOLEN_MPTCP_DSS_MAP64;
1389
1390			/* Use only 64-bit mapping flags for now, add
1391			 * support for optional 32-bit mappings later.
1392			 */
1393			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1394			if (mpext->data_fin)
1395				flags |= MPTCP_DSS_DATA_FIN;
1396
1397			if (opts->csum_reqd)
1398				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1399		}
1400
1401		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1402
1403		if (mpext->use_ack) {
1404			if (mpext->ack64) {
1405				put_unaligned_be64(mpext->data_ack, ptr);
1406				ptr += 2;
1407			} else {
1408				put_unaligned_be32(mpext->data_ack32, ptr);
1409				ptr += 1;
1410			}
1411		}
1412
1413		if (mpext->use_map) {
1414			put_unaligned_be64(mpext->data_seq, ptr);
1415			ptr += 2;
1416			put_unaligned_be32(mpext->subflow_seq, ptr);
1417			ptr += 1;
1418			if (opts->csum_reqd) {
1419				/* data_len == 0 is reserved for the infinite mapping,
1420				 * the checksum will also be set to 0.
1421				 */
1422				put_len_csum(mpext->data_len,
1423					     (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1424					     ptr);
1425			} else {
1426				put_unaligned_be32(mpext->data_len << 16 |
1427						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1428			}
1429			ptr += 1;
1430		}
1431
1432		/* We might need to add MP_FAIL options in rare cases */
1433		if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1434			goto mp_fail;
1435	} else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1436		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1437
1438		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1439			len = TCPOLEN_MPTCP_MPC_SYN;
1440		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1441			len = TCPOLEN_MPTCP_MPC_SYNACK;
1442		} else if (opts->data_len) {
1443			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1444			if (opts->csum_reqd)
1445				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1446		} else {
1447			len = TCPOLEN_MPTCP_MPC_ACK;
1448		}
1449
1450		if (opts->csum_reqd)
1451			flag |= MPTCP_CAP_CHECKSUM_REQD;
1452
1453		if (!opts->allow_join_id0)
1454			flag |= MPTCP_CAP_DENY_JOIN_ID0;
1455
1456		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1457				      MPTCP_SUPPORTED_VERSION,
1458				      flag);
1459
1460		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1461		    opts->suboptions))
1462			goto mp_capable_done;
1463
1464		put_unaligned_be64(opts->sndr_key, ptr);
1465		ptr += 2;
1466		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1467			goto mp_capable_done;
1468
1469		put_unaligned_be64(opts->rcvr_key, ptr);
1470		ptr += 2;
1471		if (!opts->data_len)
1472			goto mp_capable_done;
1473
1474		if (opts->csum_reqd) {
1475			put_len_csum(opts->data_len,
1476				     __mptcp_make_csum(opts->data_seq,
1477						       opts->subflow_seq,
1478						       opts->data_len,
1479						       ~csum_unfold(opts->csum)),
1480				     ptr);
1481		} else {
1482			put_unaligned_be32(opts->data_len << 16 |
1483					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1484		}
1485		ptr += 1;
1486
1487		/* MPC is additionally mutually exclusive with MP_PRIO */
1488		goto mp_capable_done;
1489	} else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1490		if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1491			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1492					      TCPOLEN_MPTCP_MPJ_SYN,
1493					      opts->backup, opts->join_id);
1494			put_unaligned_be32(opts->token, ptr);
1495			ptr += 1;
1496			put_unaligned_be32(opts->nonce, ptr);
1497			ptr += 1;
1498		} else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1499			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1500					      TCPOLEN_MPTCP_MPJ_SYNACK,
1501					      opts->backup, opts->join_id);
1502			put_unaligned_be64(opts->thmac, ptr);
1503			ptr += 2;
1504			put_unaligned_be32(opts->nonce, ptr);
1505			ptr += 1;
1506		} else {
1507			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1508					      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1509			memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1510			ptr += 5;
1511		}
1512	} else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1513		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1514		u8 echo = MPTCP_ADDR_ECHO;
1515
1516#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1517		if (opts->addr.family == AF_INET6)
1518			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1519#endif
1520
1521		if (opts->addr.port)
1522			len += TCPOLEN_MPTCP_PORT_LEN;
1523
1524		if (opts->ahmac) {
1525			len += sizeof(opts->ahmac);
1526			echo = 0;
1527		}
1528
1529		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1530				      len, echo, opts->addr.id);
1531		if (opts->addr.family == AF_INET) {
1532			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1533			ptr += 1;
1534		}
1535#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1536		else if (opts->addr.family == AF_INET6) {
1537			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1538			ptr += 4;
1539		}
1540#endif
1541
1542		if (!opts->addr.port) {
1543			if (opts->ahmac) {
1544				put_unaligned_be64(opts->ahmac, ptr);
1545				ptr += 2;
1546			}
1547		} else {
1548			u16 port = ntohs(opts->addr.port);
1549
1550			if (opts->ahmac) {
1551				u8 *bptr = (u8 *)ptr;
1552
1553				put_unaligned_be16(port, bptr);
1554				bptr += 2;
1555				put_unaligned_be64(opts->ahmac, bptr);
1556				bptr += 8;
1557				put_unaligned_be16(TCPOPT_NOP << 8 |
1558						   TCPOPT_NOP, bptr);
1559
1560				ptr += 3;
1561			} else {
1562				put_unaligned_be32(port << 16 |
1563						   TCPOPT_NOP << 8 |
1564						   TCPOPT_NOP, ptr);
1565				ptr += 1;
1566			}
1567		}
1568	} else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1569		/* FASTCLOSE is mutually exclusive with others except RST */
1570		*ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1571				      TCPOLEN_MPTCP_FASTCLOSE,
1572				      0, 0);
1573		put_unaligned_be64(opts->rcvr_key, ptr);
1574		ptr += 2;
1575
1576		if (OPTION_MPTCP_RST & opts->suboptions)
1577			goto mp_rst;
1578		return;
1579	} else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1580mp_fail:
1581		/* MP_FAIL is mutually exclusive with others except RST */
1582		subflow = mptcp_subflow_ctx(ssk);
1583		subflow->send_mp_fail = 0;
1584
1585		*ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1586				      TCPOLEN_MPTCP_FAIL,
1587				      0, 0);
1588		put_unaligned_be64(opts->fail_seq, ptr);
1589		ptr += 2;
1590
1591		if (OPTION_MPTCP_RST & opts->suboptions)
1592			goto mp_rst;
1593		return;
1594	} else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1595mp_rst:
1596		*ptr++ = mptcp_option(MPTCPOPT_RST,
1597				      TCPOLEN_MPTCP_RST,
1598				      opts->reset_transient,
1599				      opts->reset_reason);
1600		return;
1601	}
1602
1603	if (OPTION_MPTCP_PRIO & opts->suboptions) {
1604		subflow = mptcp_subflow_ctx(ssk);
1605		subflow->send_mp_prio = 0;
1606
1607		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1608				      TCPOLEN_MPTCP_PRIO,
1609				      opts->backup, TCPOPT_NOP);
1610
1611		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1612	}
1613
1614mp_capable_done:
1615	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1616		u8 i = 1;
1617
1618		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1619				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1620				      0, opts->rm_list.ids[0]);
1621
1622		while (i < opts->rm_list.nr) {
1623			u8 id1, id2, id3, id4;
1624
1625			id1 = opts->rm_list.ids[i];
1626			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1627			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1628			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1629			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1630			ptr += 1;
1631			i += 4;
1632		}
1633	}
1634
1635	if (tp)
1636		mptcp_set_rwin(tp, th);
1637}
1638
1639__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1640{
1641	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1642	u8 flags, reason;
1643
1644	if (ext) {
1645		flags = ext->reset_transient;
1646		reason = ext->reset_reason;
1647
1648		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1649				    flags, reason);
1650	}
1651
1652	return htonl(0u);
1653}
1654EXPORT_SYMBOL_GPL(mptcp_get_reset_option);