options.c 35.8 KB
Newer Older
P
Peter Krystad 已提交
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
/* Multipath TCP
 *
 * Copyright (c) 2017 - 2019, Intel Corporation.
 */

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

P
Peter Krystad 已提交
9
#include <linux/kernel.h>
10
#include <crypto/sha2.h>
P
Peter Krystad 已提交
11 12 13
#include <net/tcp.h>
#include <net/mptcp.h>
#include "protocol.h"
G
Geliang Tang 已提交
14
#include "mib.h"
P
Peter Krystad 已提交
15

16 17
#include <trace/events/mptcp.h>

18 19 20 21 22
static bool mptcp_cap_flag_sha256(u8 flags)
{
	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
}

23 24 25
static void mptcp_parse_option(const struct sk_buff *skb,
			       const unsigned char *ptr, int opsize,
			       struct mptcp_options_received *mp_opt)
P
Peter Krystad 已提交
26 27
{
	u8 subtype = *ptr >> 4;
28
	int expected_opsize;
P
Peter Krystad 已提交
29 30
	u8 version;
	u8 flags;
31
	u8 i;
P
Peter Krystad 已提交
32 33 34

	switch (subtype) {
	case MPTCPOPT_MP_CAPABLE:
35 36 37 38 39 40 41 42 43 44 45 46 47
		/* strict size checking */
		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
			if (skb->len > tcp_hdr(skb)->doff << 2)
				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
			else
				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
		} else {
			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
			else
				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
		}
		if (opsize != expected_opsize)
P
Peter Krystad 已提交
48 49
			break;

50
		/* try to be gentle vs future versions on the initial syn */
P
Peter Krystad 已提交
51
		version = *ptr++ & MPTCP_VERSION_MASK;
52 53 54 55
		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
			if (version != MPTCP_SUPPORTED_VERSION)
				break;
		} else if (version < MPTCP_SUPPORTED_VERSION) {
P
Peter Krystad 已提交
56
			break;
57
		}
P
Peter Krystad 已提交
58 59

		flags = *ptr++;
60
		if (!mptcp_cap_flag_sha256(flags) ||
P
Peter Krystad 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		    (flags & MPTCP_CAP_EXTENSIBILITY))
			break;

		/* RFC 6824, Section 3.1:
		 * "For the Checksum Required bit (labeled "A"), if either
		 * host requires the use of checksums, checksums MUST be used.
		 * In other words, the only way for checksums not to be used
		 * is if both hosts in their SYNs set A=0."
		 *
		 * Section 3.3.0:
		 * "If a checksum is not present when its use has been
		 * negotiated, the receiver MUST close the subflow with a RST as
		 * it is considered broken."
		 *
		 * We don't implement DSS checksum - fall back to TCP.
		 */
		if (flags & MPTCP_CAP_CHECKSUM_REQD)
			break;

		mp_opt->mp_capable = 1;
81 82 83 84 85
		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
			mp_opt->sndr_key = get_unaligned_be64(ptr);
			ptr += 8;
		}
		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
P
Peter Krystad 已提交
86 87 88
			mp_opt->rcvr_key = get_unaligned_be64(ptr);
			ptr += 8;
		}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
			/* Section 3.1.:
			 * "the data parameters in a MP_CAPABLE are semantically
			 * equivalent to those in a DSS option and can be used
			 * interchangeably."
			 */
			mp_opt->dss = 1;
			mp_opt->use_map = 1;
			mp_opt->mpc_map = 1;
			mp_opt->data_len = get_unaligned_be16(ptr);
			ptr += 2;
		}
		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
			 version, flags, opsize, mp_opt->sndr_key,
			 mp_opt->rcvr_key, mp_opt->data_len);
P
Peter Krystad 已提交
104 105
		break;

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	case MPTCPOPT_MP_JOIN:
		mp_opt->mp_join = 1;
		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
			mp_opt->join_id = *ptr++;
			mp_opt->token = get_unaligned_be32(ptr);
			ptr += 4;
			mp_opt->nonce = get_unaligned_be32(ptr);
			ptr += 4;
			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
				 mp_opt->backup, mp_opt->join_id,
				 mp_opt->token, mp_opt->nonce);
		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
			mp_opt->join_id = *ptr++;
			mp_opt->thmac = get_unaligned_be64(ptr);
			ptr += 8;
			mp_opt->nonce = get_unaligned_be32(ptr);
			ptr += 4;
			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
				 mp_opt->backup, mp_opt->join_id,
				 mp_opt->thmac, mp_opt->nonce);
		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
			ptr += 2;
			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
			pr_debug("MP_JOIN hmac");
		} else {
			mp_opt->mp_join = 0;
		}
		break;

P
Peter Krystad 已提交
137 138
	case MPTCPOPT_DSS:
		pr_debug("DSS");
139 140
		ptr++;

141 142 143 144 145
		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
		 * map info accordingly
		 */
		mp_opt->mpc_map = 0;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);

		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
			 mp_opt->data_fin, mp_opt->dsn64,
			 mp_opt->use_map, mp_opt->ack64,
			 mp_opt->use_ack);

		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;

		if (mp_opt->use_ack) {
			if (mp_opt->ack64)
				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
			else
				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
		}

		if (mp_opt->use_map) {
			if (mp_opt->dsn64)
				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
			else
				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
		}

		/* RFC 6824, Section 3.3:
		 * If a checksum is present, but its use had
		 * not been negotiated in the MP_CAPABLE handshake,
		 * the checksum field MUST be ignored.
		 */
		if (opsize != expected_opsize &&
		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
			break;

P
Peter Krystad 已提交
183
		mp_opt->dss = 1;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

		if (mp_opt->use_ack) {
			if (mp_opt->ack64) {
				mp_opt->data_ack = get_unaligned_be64(ptr);
				ptr += 8;
			} else {
				mp_opt->data_ack = get_unaligned_be32(ptr);
				ptr += 4;
			}

			pr_debug("data_ack=%llu", mp_opt->data_ack);
		}

		if (mp_opt->use_map) {
			if (mp_opt->dsn64) {
				mp_opt->data_seq = get_unaligned_be64(ptr);
				ptr += 8;
			} else {
				mp_opt->data_seq = get_unaligned_be32(ptr);
				ptr += 4;
			}

			mp_opt->subflow_seq = get_unaligned_be32(ptr);
			ptr += 4;

			mp_opt->data_len = get_unaligned_be16(ptr);
			ptr += 2;

			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
				 mp_opt->data_seq, mp_opt->subflow_seq,
				 mp_opt->data_len);
		}

P
Peter Krystad 已提交
217 218
		break;

P
Peter Krystad 已提交
219 220 221 222 223
	case MPTCPOPT_ADD_ADDR:
		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
		if (!mp_opt->echo) {
			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
224
				mp_opt->addr.family = AF_INET;
P
Peter Krystad 已提交
225 226 227
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
228
				mp_opt->addr.family = AF_INET6;
P
Peter Krystad 已提交
229 230 231 232 233 234
#endif
			else
				break;
		} else {
			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
235
				mp_opt->addr.family = AF_INET;
P
Peter Krystad 已提交
236 237 238
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
239
				mp_opt->addr.family = AF_INET6;
P
Peter Krystad 已提交
240 241 242 243 244 245
#endif
			else
				break;
		}

		mp_opt->add_addr = 1;
246
		mp_opt->addr.id = *ptr++;
247
		if (mp_opt->addr.family == AF_INET) {
248
			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
P
Peter Krystad 已提交
249 250 251
			ptr += 4;
			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
252
				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
P
Peter Krystad 已提交
253 254 255 256 257
				ptr += 2;
			}
		}
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
		else {
258
			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
P
Peter Krystad 已提交
259 260 261
			ptr += 16;
			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
262
				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
P
Peter Krystad 已提交
263 264 265 266 267 268 269 270
				ptr += 2;
			}
		}
#endif
		if (!mp_opt->echo) {
			mp_opt->ahmac = get_unaligned_be64(ptr);
			ptr += 8;
		}
271
		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
272
			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
273
			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
P
Peter Krystad 已提交
274 275 276
		break;

	case MPTCPOPT_RM_ADDR:
277 278
		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
P
Peter Krystad 已提交
279 280
			break;

281 282
		ptr++;

P
Peter Krystad 已提交
283
		mp_opt->rm_addr = 1;
284 285 286 287
		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
		for (i = 0; i < mp_opt->rm_list.nr; i++)
			mp_opt->rm_list.ids[i] = *ptr++;
		pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
P
Peter Krystad 已提交
288 289
		break;

290 291 292 293 294 295 296 297 298
	case MPTCPOPT_MP_PRIO:
		if (opsize != TCPOLEN_MPTCP_PRIO)
			break;

		mp_opt->mp_prio = 1;
		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
		pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
		break;

299 300 301 302 303 304 305 306 307 308
	case MPTCPOPT_MP_FASTCLOSE:
		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
			break;

		ptr += 2;
		mp_opt->rcvr_key = get_unaligned_be64(ptr);
		ptr += 8;
		mp_opt->fastclose = 1;
		break;

309 310 311 312 313 314 315 316 317 318 319 320
	case MPTCPOPT_RST:
		if (opsize != TCPOLEN_MPTCP_RST)
			break;

		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
			break;
		mp_opt->reset = 1;
		flags = *ptr++;
		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
		mp_opt->reset_reason = *ptr;
		break;

P
Peter Krystad 已提交
321 322 323 324 325
	default:
		break;
	}
}

326
void mptcp_get_options(const struct sk_buff *skb,
327
		       struct mptcp_options_received *mp_opt)
328 329
{
	const struct tcphdr *th = tcp_hdr(skb);
330 331
	const unsigned char *ptr;
	int length;
332

333 334 335 336
	/* initialize option status */
	mp_opt->mp_capable = 0;
	mp_opt->mp_join = 0;
	mp_opt->add_addr = 0;
337
	mp_opt->ahmac = 0;
338
	mp_opt->fastclose = 0;
339
	mp_opt->addr.port = 0;
340 341
	mp_opt->rm_addr = 0;
	mp_opt->dss = 0;
342
	mp_opt->mp_prio = 0;
343
	mp_opt->reset = 0;
344 345

	length = (th->doff * 4) - sizeof(struct tcphdr);
346 347 348 349 350 351 352 353 354 355 356 357 358
	ptr = (const unsigned char *)(th + 1);

	while (length > 0) {
		int opcode = *ptr++;
		int opsize;

		switch (opcode) {
		case TCPOPT_EOL:
			return;
		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
			length--;
			continue;
		default:
359 360
			if (length < 2)
				return;
361 362 363 364 365 366
			opsize = *ptr++;
			if (opsize < 2) /* "silly options" */
				return;
			if (opsize > length)
				return;	/* don't parse partial options */
			if (opcode == TCPOPT_MPTCP)
367
				mptcp_parse_option(skb, ptr, opsize, mp_opt);
368 369 370 371 372 373
			ptr += opsize - 2;
			length -= opsize;
		}
	}
}

374 375
bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
		       unsigned int *size, struct mptcp_out_options *opts)
376 377 378
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

379 380 381 382
	/* we will use snd_isn to detect first pkt [re]transmission
	 * in mptcp_established_options_mp()
	 */
	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
383 384 385 386
	if (subflow->request_mptcp) {
		opts->suboptions = OPTION_MPTCP_MPC_SYN;
		*size = TCPOLEN_MPTCP_MPC_SYN;
		return true;
387 388 389 390 391 392 393 394 395 396
	} else if (subflow->request_join) {
		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
			 subflow->local_nonce);
		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
		opts->join_id = subflow->local_id;
		opts->token = subflow->remote_token;
		opts->nonce = subflow->local_nonce;
		opts->backup = subflow->request_bkup;
		*size = TCPOLEN_MPTCP_MPJ_SYN;
		return true;
397 398 399 400
	}
	return false;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/* MP_JOIN client subflow must wait for 4th ack before sending any data:
 * TCP can't schedule delack timer before the subflow is fully established.
 * MPTCP uses the delack timer to do 3rd ack retransmissions
 */
static void schedule_3rdack_retransmission(struct sock *sk)
{
	struct inet_connection_sock *icsk = inet_csk(sk);
	struct tcp_sock *tp = tcp_sk(sk);
	unsigned long timeout;

	/* reschedule with a timeout above RTT, as we must look only for drop */
	if (tp->srtt_us)
		timeout = tp->srtt_us << 1;
	else
		timeout = TCP_TIMEOUT_INIT;

	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
	icsk->icsk_ack.timeout = timeout;
	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
}

static void clear_3rdack_retransmission(struct sock *sk)
{
	struct inet_connection_sock *icsk = inet_csk(sk);

	sk_stop_timer(sk, &icsk->icsk_delack_timer);
	icsk->icsk_ack.timeout = 0;
	icsk->icsk_ack.ato = 0;
	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
}

433
static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
434
					 bool snd_data_fin_enable,
435
					 unsigned int *size,
436 437
					 unsigned int remaining,
					 struct mptcp_out_options *opts)
438 439
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
440 441 442
	struct mptcp_ext *mpext;
	unsigned int data_len;

443 444 445 446 447 448 449 450
	/* When skb is not available, we better over-estimate the emitted
	 * options len. A full DSS option (28 bytes) is longer than
	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
	 * tell the caller to defer the estimate to
	 * mptcp_established_options_dss(), which will reserve enough space.
	 */
	if (!skb)
		return false;
451

452 453 454 455
	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
	if (subflow->fully_established || snd_data_fin_enable ||
	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
	    sk->sk_state != TCP_ESTABLISHED)
456 457 458
		return false;

	if (subflow->mp_capable) {
459 460
		mpext = mptcp_get_ext(skb);
		data_len = mpext ? mpext->data_len : 0;
461

462 463 464 465 466
		/* we will check ext_copy.data_len in mptcp_write_options() to
		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
		 * TCPOLEN_MPTCP_MPC_ACK
		 */
		opts->ext_copy.data_len = data_len;
467 468 469
		opts->suboptions = OPTION_MPTCP_MPC_ACK;
		opts->sndr_key = subflow->local_key;
		opts->rcvr_key = subflow->remote_key;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

		/* Section 3.1.
		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
		 * packets that start the first subflow of an MPTCP connection,
		 * as well as the first packet that carries data
		 */
		if (data_len > 0)
			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
		else
			*size = TCPOLEN_MPTCP_MPC_ACK;

		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
			 subflow, subflow->local_key, subflow->remote_key,
			 data_len);

485
		return true;
486 487 488 489 490 491 492 493
	} else if (subflow->mp_join) {
		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
		*size = TCPOLEN_MPTCP_MPJ_ACK;
		pr_debug("subflow=%p", subflow);

		schedule_3rdack_retransmission(sk);
		return true;
494 495 496 497
	}
	return false;
}

498
static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
499
				 struct sk_buff *skb, struct mptcp_ext *ext)
500
{
P
Paolo Abeni 已提交
501 502 503 504
	/* The write_seq value has already been incremented, so the actual
	 * sequence number for the DATA_FIN is one less.
	 */
	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
505

506
	if (!ext->use_map || !skb->len) {
507 508 509
		/* RFC6824 requires a DSS mapping with specific values
		 * if DATA_FIN is set but no data payload is mapped
		 */
510
		ext->data_fin = 1;
511 512
		ext->use_map = 1;
		ext->dsn64 = 1;
P
Paolo Abeni 已提交
513
		ext->data_seq = data_fin_tx_seq;
514 515
		ext->subflow_seq = 0;
		ext->data_len = 1;
516
	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
517 518 519
		/* If there's an existing DSS mapping and it is the
		 * final mapping, DATA_FIN consumes 1 additional byte of
		 * mapping space.
520
		 */
521
		ext->data_fin = 1;
522 523 524 525 526
		ext->data_len++;
	}
}

static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
527
					  bool snd_data_fin_enable,
528 529 530 531 532
					  unsigned int *size,
					  unsigned int remaining,
					  struct mptcp_out_options *opts)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
533
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
534 535 536
	unsigned int dss_size = 0;
	struct mptcp_ext *mpext;
	unsigned int ack_size;
537
	bool ret = false;
538
	u64 ack_seq;
539

540
	mpext = skb ? mptcp_get_ext(skb) : NULL;
541

542
	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
543 544 545 546 547 548 549 550 551
		unsigned int map_size;

		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;

		remaining -= map_size;
		dss_size = map_size;
		if (mpext)
			opts->ext_copy = *mpext;

552
		if (skb && snd_data_fin_enable)
553
			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
554 555 556
		ret = true;
	}

557 558 559
	/* passive sockets msk will set the 'can_ack' after accept(), even
	 * if the first subflow may have the already the remote key handy
	 */
560
	opts->ext_copy.use_ack = 0;
P
Paolo Abeni 已提交
561
	if (!READ_ONCE(msk->can_ack)) {
562 563
		*size = ALIGN(dss_size, 4);
		return ret;
564 565
	}

566
	ack_seq = READ_ONCE(msk->ack_seq);
567
	if (READ_ONCE(msk->use_64bit_ack)) {
568
		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
569
		opts->ext_copy.data_ack = ack_seq;
570 571 572
		opts->ext_copy.ack64 = 1;
	} else {
		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
573
		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
574 575 576
		opts->ext_copy.ack64 = 0;
	}
	opts->ext_copy.use_ack = 1;
577
	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
578 579 580 581 582 583 584 585 586 587 588

	/* Add kind/length/subtype/flag overhead if mapping is not populated */
	if (dss_size == 0)
		ack_size += TCPOLEN_MPTCP_DSS_BASE;

	dss_size += ack_size;

	*size = ALIGN(dss_size, 4);
	return true;
}

589 590
static u64 add_addr_generate_hmac(u64 key1, u64 key2,
				  struct mptcp_addr_info *addr)
P
Peter Krystad 已提交
591
{
592
	u16 port = ntohs(addr->port);
593
	u8 hmac[SHA256_DIGEST_SIZE];
P
Peter Krystad 已提交
594
	u8 msg[19];
595
	int i = 0;
P
Peter Krystad 已提交
596

597 598 599 600 601 602 603 604 605 606 607 608 609
	msg[i++] = addr->id;
	if (addr->family == AF_INET) {
		memcpy(&msg[i], &addr->addr.s_addr, 4);
		i += 4;
	}
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
	else if (addr->family == AF_INET6) {
		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
		i += 16;
	}
#endif
	msg[i++] = port >> 8;
	msg[i++] = port & 0xFF;
P
Peter Krystad 已提交
610

611
	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
P
Peter Krystad 已提交
612

613
	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
P
Peter Krystad 已提交
614 615
}

616
static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
617 618 619
					       unsigned int *size,
					       unsigned int remaining,
					       struct mptcp_out_options *opts)
P
Peter Krystad 已提交
620 621 622
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
623 624
	bool drop_other_suboptions = false;
	unsigned int opt_size = *size;
625
	bool echo;
626
	bool port;
627
	int len;
P
Peter Krystad 已提交
628

629
	if ((mptcp_pm_should_add_signal_ipv6(msk) ||
630 631
	     mptcp_pm_should_add_signal_port(msk) ||
	     mptcp_pm_should_add_signal_echo(msk)) &&
632 633 634
	    skb && skb_is_tcp_pure_ack(skb)) {
		pr_debug("drop other suboptions");
		opts->suboptions = 0;
635 636
		opts->ext_copy.use_ack = 0;
		opts->ext_copy.use_map = 0;
637 638 639 640
		remaining += opt_size;
		drop_other_suboptions = true;
	}

641
	if (!mptcp_pm_should_add_signal(msk) ||
642
	    !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port)))
643 644
		return false;

645
	len = mptcp_add_addr_len(opts->addr.family, echo, port);
646 647
	if (remaining < len)
		return false;
P
Peter Krystad 已提交
648

649
	*size = len;
650 651
	if (drop_other_suboptions)
		*size -= opt_size;
652
	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
653 654 655 656
	if (!echo) {
		opts->ahmac = add_addr_generate_hmac(msk->local_key,
						     msk->remote_key,
						     &opts->addr);
P
Peter Krystad 已提交
657
	}
658
	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
659
		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
P
Peter Krystad 已提交
660 661 662 663

	return true;
}

664 665 666 667 668 669 670
static bool mptcp_established_options_rm_addr(struct sock *sk,
					      unsigned int *size,
					      unsigned int remaining,
					      struct mptcp_out_options *opts)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
671 672
	struct mptcp_rm_list rm_list;
	int i, len;
673 674

	if (!mptcp_pm_should_rm_signal(msk) ||
675
	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
676 677
		return false;

678 679 680 681
	len = mptcp_rm_addr_len(&rm_list);
	if (len < 0)
		return false;
	if (remaining < len)
682 683
		return false;

684
	*size = len;
685
	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
686
	opts->rm_list = rm_list;
687

688 689
	for (i = 0; i < opts->rm_list.nr; i++)
		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
690 691 692 693

	return true;
}

694 695 696 697 698 699 700 701 702 703
static bool mptcp_established_options_mp_prio(struct sock *sk,
					      unsigned int *size,
					      unsigned int remaining,
					      struct mptcp_out_options *opts)
{
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

	if (!subflow->send_mp_prio)
		return false;

704 705
	/* account for the trailing 'nop' option */
	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
706 707
		return false;

708
	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
709 710 711 712 713 714 715 716
	opts->suboptions |= OPTION_MPTCP_PRIO;
	opts->backup = subflow->request_bkup;

	pr_debug("prio=%d", opts->backup);

	return true;
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
						   unsigned int *size,
						   unsigned int remaining,
						   struct mptcp_out_options *opts)
{
	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);

	if (remaining < TCPOLEN_MPTCP_RST)
		return;

	*size = TCPOLEN_MPTCP_RST;
	opts->suboptions |= OPTION_MPTCP_RST;
	opts->reset_transient = subflow->reset_transient;
	opts->reset_reason = subflow->reset_reason;
}

733 734 735 736
bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
			       unsigned int *size, unsigned int remaining,
			       struct mptcp_out_options *opts)
{
737 738
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
739
	unsigned int opt_size = 0;
740
	bool snd_data_fin;
741 742
	bool ret = false;

P
Peter Krystad 已提交
743 744
	opts->suboptions = 0;

745
	if (unlikely(__mptcp_check_fallback(msk)))
746 747
		return false;

748 749 750 751
	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
		mptcp_established_options_rst(sk, skb, size, remaining, opts);
		return true;
	}
752

753 754
	snd_data_fin = mptcp_data_fin_enabled(msk);
	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
755
		ret = true;
756
	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts))
757 758 759 760 761 762 763 764 765 766
		ret = true;

	/* we reserved enough space for the above options, and exceeding the
	 * TCP option space would be fatal
	 */
	if (WARN_ON_ONCE(opt_size > remaining))
		return false;

	*size += opt_size;
	remaining -= opt_size;
767
	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
P
Peter Krystad 已提交
768 769 770
		*size += opt_size;
		remaining -= opt_size;
		ret = true;
771 772 773 774
	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
		*size += opt_size;
		remaining -= opt_size;
		ret = true;
P
Peter Krystad 已提交
775
	}
776

777 778 779 780 781 782
	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
		*size += opt_size;
		remaining -= opt_size;
		ret = true;
	}

783 784 785
	return ret;
}

786 787 788 789 790 791 792 793 794 795 796 797
bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
			  struct mptcp_out_options *opts)
{
	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);

	if (subflow_req->mp_capable) {
		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
		opts->sndr_key = subflow_req->local_key;
		*size = TCPOLEN_MPTCP_MPC_SYNACK;
		pr_debug("subflow_req=%p, local_key=%llu",
			 subflow_req, subflow_req->local_key);
		return true;
798 799 800 801 802 803 804 805 806 807 808
	} else if (subflow_req->mp_join) {
		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
		opts->backup = subflow_req->backup;
		opts->join_id = subflow_req->local_id;
		opts->thmac = subflow_req->thmac;
		opts->nonce = subflow_req->local_nonce;
		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
			 subflow_req, opts->backup, opts->join_id,
			 opts->thmac, opts->nonce);
		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
		return true;
809 810 811 812
	}
	return false;
}

813
static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
814
				    struct mptcp_subflow_context *subflow,
P
Paolo Abeni 已提交
815 816
				    struct sk_buff *skb,
				    struct mptcp_options_received *mp_opt)
817 818
{
	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
819
	 * will make the subflow fully established
820
	 */
821 822 823 824 825 826 827 828 829
	if (likely(subflow->fully_established)) {
		/* on passive sockets, check for 3rd ack retransmission
		 * note that msk is always set by subflow_syn_recv_sock()
		 * for mp_join subflows
		 */
		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
		    subflow->mp_join && mp_opt->mp_join &&
		    READ_ONCE(msk->pm.server_side))
830
			tcp_send_ack(ssk);
831 832 833
		goto fully_established;
	}

834 835 836 837
	/* we must process OoO packets before the first subflow is fully
	 * established. OoO packets are instead a protocol violation
	 * for MP_JOIN subflows as the peer must not send any data
	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
838
	 */
839 840 841
	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
		if (subflow->mp_join)
			goto reset;
842
		return subflow->mp_capable;
843
	}
844

845
	if (mp_opt->dss && mp_opt->use_ack) {
846 847 848
		/* subflows are fully established as soon as we get any
		 * additional ack.
		 */
P
Paolo Abeni 已提交
849
		subflow->fully_established = 1;
850
		WRITE_ONCE(msk->fully_established, true);
851 852
		goto fully_established;
	}
853

854 855 856 857 858
	if (mp_opt->add_addr) {
		WRITE_ONCE(msk->fully_established, true);
		return true;
	}

859
	/* If the first established packet does not contain MP_CAPABLE + data
860 861
	 * then fallback to TCP. Fallback scenarios requires a reset for
	 * MP_JOIN subflows.
862 863
	 */
	if (!mp_opt->mp_capable) {
864 865
		if (subflow->mp_join)
			goto reset;
866
		subflow->mp_capable = 0;
867 868
		pr_fallback(msk);
		__mptcp_do_fallback(msk);
869 870
		return false;
	}
871

P
Paolo Abeni 已提交
872 873
	if (unlikely(!READ_ONCE(msk->pm.server_side)))
		pr_warn_once("bogus mpc option on established client sk");
874
	mptcp_subflow_fully_established(subflow, mp_opt);
875 876

fully_established:
877 878 879 880 881 882
	/* if the subflow is not already linked into the conn_list, we can't
	 * notify the PM: this subflow is still on the listener queue
	 * and the PM possibly acquiring the subflow lock could race with
	 * the listener close
	 */
	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
883 884 885
		return true;

	subflow->pm_notified = 1;
886
	if (subflow->mp_join) {
887
		clear_3rdack_retransmission(ssk);
888
		mptcp_pm_subflow_established(msk);
889
	} else {
890
		mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
891
	}
892
	return true;
893 894 895 896

reset:
	mptcp_subflow_reset(ssk);
	return false;
897 898
}

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
{
	u32 old_ack32, cur_ack32;

	if (use_64bit)
		return cur_ack;

	old_ack32 = (u32)old_ack;
	cur_ack32 = (u32)cur_ack;
	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
	if (unlikely(before(cur_ack32, old_ack32)))
		return cur_ack + (1LL << 32);
	return cur_ack;
}

914
static void ack_update_msk(struct mptcp_sock *msk,
915
			   struct sock *ssk,
916
			   struct mptcp_options_received *mp_opt)
917
{
918
	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
919
	struct sock *sk = (struct sock *)msk;
920 921 922
	u64 old_snd_una;

	mptcp_data_lock(sk);
923 924 925 926 927

	/* avoid ack expansion on update conflict, to reduce the risk of
	 * wrongly expanding to a future ack sequence number, which is way
	 * more dangerous than missing an ack
	 */
928
	old_snd_una = msk->snd_una;
929 930 931
	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);

	/* ACK for data not even sent yet? Ignore. */
P
Paolo Abeni 已提交
932
	if (after64(new_snd_una, snd_nxt))
933 934
		new_snd_una = old_snd_una;

935 936
	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;

937
	if (after64(new_wnd_end, msk->wnd_end))
938
		msk->wnd_end = new_wnd_end;
939 940

	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
941
	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
942
		__mptcp_check_push(sk, ssk);
943

944 945 946
	if (after64(new_snd_una, old_snd_una)) {
		msk->snd_una = new_snd_una;
		__mptcp_data_acked(sk);
947
	}
948
	mptcp_data_unlock(sk);
949 950 951 952

	trace_ack_update_msk(mp_opt->data_ack,
			     old_snd_una, new_snd_una,
			     new_wnd_end, msk->wnd_end);
953 954
}

955
bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
956 957 958 959 960 961
{
	/* Skip if DATA_FIN was already received.
	 * If updating simultaneously with the recvmsg loop, values
	 * should match. If they mismatch, the peer is misbehaving and
	 * we will prefer the most recent information.
	 */
962
	if (READ_ONCE(msk->rcv_data_fin))
963 964
		return false;

965 966
	WRITE_ONCE(msk->rcv_data_fin_seq,
		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
967 968 969 970 971
	WRITE_ONCE(msk->rcv_data_fin, 1);

	return true;
}

972 973 974 975 976 977 978 979
static bool add_addr_hmac_valid(struct mptcp_sock *msk,
				struct mptcp_options_received *mp_opt)
{
	u64 hmac = 0;

	if (mp_opt->echo)
		return true;

980 981 982
	hmac = add_addr_generate_hmac(msk->remote_key,
				      msk->local_key,
				      &mp_opt->addr);
983 984 985 986 987 988 989 990

	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
		 msk, (unsigned long long)hmac,
		 (unsigned long long)mp_opt->ahmac);

	return hmac == mp_opt->ahmac;
}

991
void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
992
{
993
	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
994
	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
995
	struct mptcp_options_received mp_opt;
996 997
	struct mptcp_ext *mpext;

998 999 1000 1001 1002 1003 1004
	if (__mptcp_check_fallback(msk)) {
		/* Keep it simple and unconditionally trigger send data cleanup and
		 * pending queue spooling. We will need to acquire the data lock
		 * for more accurate checks, and once the lock is acquired, such
		 * helpers are cheap.
		 */
		mptcp_data_lock(subflow->conn);
1005 1006
		if (sk_stream_memory_free(sk))
			__mptcp_check_push(subflow->conn, sk);
1007 1008
		__mptcp_data_acked(subflow->conn);
		mptcp_data_unlock(subflow->conn);
1009
		return;
1010
	}
1011

1012 1013
	mptcp_get_options(skb, &mp_opt);
	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1014
		return;
1015

1016 1017 1018 1019 1020 1021
	if (mp_opt.fastclose &&
	    msk->local_key == mp_opt.rcvr_key) {
		WRITE_ONCE(msk->rcv_fastclose, true);
		mptcp_schedule_work((struct sock *)msk);
	}

1022
	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
G
Geliang Tang 已提交
1023
		if (!mp_opt.echo) {
1024
			mptcp_pm_add_addr_received(msk, &mp_opt.addr);
G
Geliang Tang 已提交
1025 1026
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
		} else {
1027
			mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1028
			mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
G
Geliang Tang 已提交
1029 1030
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
		}
1031

1032
		if (mp_opt.addr.port)
1033 1034
			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);

1035
		mp_opt.add_addr = 0;
1036 1037
	}

1038
	if (mp_opt.rm_addr) {
1039
		mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1040 1041 1042
		mp_opt.rm_addr = 0;
	}

1043 1044
	if (mp_opt.mp_prio) {
		mptcp_pm_mp_prio_received(sk, mp_opt.backup);
G
Geliang Tang 已提交
1045
		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1046 1047 1048
		mp_opt.mp_prio = 0;
	}

1049 1050 1051 1052 1053 1054
	if (mp_opt.reset) {
		subflow->reset_seen = 1;
		subflow->reset_reason = mp_opt.reset_reason;
		subflow->reset_transient = mp_opt.reset_transient;
	}

1055
	if (!mp_opt.dss)
1056 1057
		return;

1058 1059 1060
	/* we can't wait for recvmsg() to update the ack_seq, otherwise
	 * monodirectional flows will stuck
	 */
1061
	if (mp_opt.use_ack)
1062
		ack_update_msk(msk, sk, &mp_opt);
1063

1064 1065 1066 1067
	/* Zero-data-length packets are dropped by the caller and not
	 * propagated to the MPTCP layer, so the skb extension does not
	 * need to be allocated or populated. DATA_FIN information, if
	 * present, needs to be updated here before the skb is freed.
1068 1069 1070
	 */
	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1071
		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1072 1073
		    schedule_work(&msk->work))
			sock_hold(subflow->conn);
1074 1075

		return;
1076 1077
	}

1078 1079 1080 1081 1082 1083
	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
	if (!mpext)
		return;

	memset(mpext, 0, sizeof(*mpext));

1084 1085
	if (mp_opt.use_map) {
		if (mp_opt.mpc_map) {
1086 1087 1088 1089 1090 1091 1092 1093 1094
			/* this is an MP_CAPABLE carrying MPTCP data
			 * we know this map the first chunk of data
			 */
			mptcp_crypto_key_sha(subflow->remote_key, NULL,
					     &mpext->data_seq);
			mpext->data_seq++;
			mpext->subflow_seq = 1;
			mpext->dsn64 = 1;
			mpext->mpc_map = 1;
1095
			mpext->data_fin = 0;
1096
		} else {
1097 1098 1099 1100
			mpext->data_seq = mp_opt.data_seq;
			mpext->subflow_seq = mp_opt.subflow_seq;
			mpext->dsn64 = mp_opt.dsn64;
			mpext->data_fin = mp_opt.data_fin;
1101
		}
1102
		mpext->data_len = mp_opt.data_len;
1103 1104 1105 1106
		mpext->use_map = 1;
	}
}

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static void mptcp_set_rwin(const struct tcp_sock *tp)
{
	const struct sock *ssk = (const struct sock *)tp;
	const struct mptcp_subflow_context *subflow;
	struct mptcp_sock *msk;
	u64 ack_seq;

	subflow = mptcp_subflow_ctx(ssk);
	msk = mptcp_sk(subflow->conn);

	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;

	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
}

void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
			 struct mptcp_out_options *opts)
P
Peter Krystad 已提交
1125
{
1126
	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
P
Peter Krystad 已提交
1127 1128 1129 1130 1131
	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
		u8 len;

		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
			len = TCPOLEN_MPTCP_MPC_SYN;
1132 1133
		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
			len = TCPOLEN_MPTCP_MPC_SYNACK;
1134 1135
		else if (opts->ext_copy.data_len)
			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
P
Peter Krystad 已提交
1136 1137 1138
		else
			len = TCPOLEN_MPTCP_MPC_ACK;

P
Peter Krystad 已提交
1139 1140 1141
		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
				      MPTCP_SUPPORTED_VERSION,
				      MPTCP_CAP_HMAC_SHA256);
1142 1143 1144 1145 1146

		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
		    opts->suboptions))
			goto mp_capable_done;

P
Peter Krystad 已提交
1147 1148
		put_unaligned_be64(opts->sndr_key, ptr);
		ptr += 2;
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
			goto mp_capable_done;

		put_unaligned_be64(opts->rcvr_key, ptr);
		ptr += 2;
		if (!opts->ext_copy.data_len)
			goto mp_capable_done;

		put_unaligned_be32(opts->ext_copy.data_len << 16 |
				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
		ptr += 1;
P
Peter Krystad 已提交
1160
	}
1161

1162
mp_capable_done:
1163
	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1164 1165 1166
		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
		u8 echo = MPTCP_ADDR_ECHO;

1167
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1168
		if (opts->addr.family == AF_INET6)
1169 1170 1171
			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
#endif

1172
		if (opts->addr.port)
1173 1174
			len += TCPOLEN_MPTCP_PORT_LEN;

P
Peter Krystad 已提交
1175
		if (opts->ahmac) {
1176 1177
			len += sizeof(opts->ahmac);
			echo = 0;
P
Peter Krystad 已提交
1178 1179
		}

1180
		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1181
				      len, echo, opts->addr.id);
1182
		if (opts->addr.family == AF_INET) {
1183
			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1184
			ptr += 1;
P
Peter Krystad 已提交
1185 1186
		}
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1187
		else if (opts->addr.family == AF_INET6) {
1188
			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1189
			ptr += 4;
P
Peter Krystad 已提交
1190 1191 1192
		}
#endif

1193
		if (!opts->addr.port) {
1194 1195 1196 1197 1198
			if (opts->ahmac) {
				put_unaligned_be64(opts->ahmac, ptr);
				ptr += 2;
			}
		} else {
1199 1200
			u16 port = ntohs(opts->addr.port);

1201 1202 1203
			if (opts->ahmac) {
				u8 *bptr = (u8 *)ptr;

1204
				put_unaligned_be16(port, bptr);
1205 1206 1207 1208 1209 1210 1211 1212
				bptr += 2;
				put_unaligned_be64(opts->ahmac, bptr);
				bptr += 8;
				put_unaligned_be16(TCPOPT_NOP << 8 |
						   TCPOPT_NOP, bptr);

				ptr += 3;
			} else {
1213
				put_unaligned_be32(port << 16 |
1214 1215 1216 1217
						   TCPOPT_NOP << 8 |
						   TCPOPT_NOP, ptr);
				ptr += 1;
			}
P
Peter Krystad 已提交
1218 1219 1220 1221
		}
	}

	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1222 1223
		u8 i = 1;

P
Peter Krystad 已提交
1224
		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
				      0, opts->rm_list.ids[0]);

		while (i < opts->rm_list.nr) {
			u8 id1, id2, id3, id4;

			id1 = opts->rm_list.ids[i];
			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
			ptr += 1;
			i += 4;
		}
P
Peter Krystad 已提交
1239 1240
	}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	if (OPTION_MPTCP_PRIO & opts->suboptions) {
		const struct sock *ssk = (const struct sock *)tp;
		struct mptcp_subflow_context *subflow;

		subflow = mptcp_subflow_ctx(ssk);
		subflow->send_mp_prio = 0;

		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
				      TCPOLEN_MPTCP_PRIO,
				      opts->backup, TCPOPT_NOP);
	}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
				      TCPOLEN_MPTCP_MPJ_SYN,
				      opts->backup, opts->join_id);
		put_unaligned_be32(opts->token, ptr);
		ptr += 1;
		put_unaligned_be32(opts->nonce, ptr);
		ptr += 1;
	}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
				      TCPOLEN_MPTCP_MPJ_SYNACK,
				      opts->backup, opts->join_id);
		put_unaligned_be64(opts->thmac, ptr);
		ptr += 2;
		put_unaligned_be32(opts->nonce, ptr);
		ptr += 1;
	}

1273 1274 1275 1276 1277 1278 1279
	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
		ptr += 5;
	}

1280 1281 1282 1283 1284 1285
	if (OPTION_MPTCP_RST & opts->suboptions)
		*ptr++ = mptcp_option(MPTCPOPT_RST,
				      TCPOLEN_MPTCP_RST,
				      opts->reset_transient,
				      opts->reset_reason);

1286 1287 1288 1289 1290 1291
	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
		struct mptcp_ext *mpext = &opts->ext_copy;
		u8 len = TCPOLEN_MPTCP_DSS_BASE;
		u8 flags = 0;

		if (mpext->use_ack) {
1292 1293 1294 1295 1296 1297 1298
			flags = MPTCP_DSS_HAS_ACK;
			if (mpext->ack64) {
				len += TCPOLEN_MPTCP_DSS_ACK64;
				flags |= MPTCP_DSS_ACK64;
			} else {
				len += TCPOLEN_MPTCP_DSS_ACK32;
			}
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
		}

		if (mpext->use_map) {
			len += TCPOLEN_MPTCP_DSS_MAP64;

			/* Use only 64-bit mapping flags for now, add
			 * support for optional 32-bit mappings later.
			 */
			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
			if (mpext->data_fin)
				flags |= MPTCP_DSS_DATA_FIN;
		}

P
Peter Krystad 已提交
1312
		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1313 1314

		if (mpext->use_ack) {
1315 1316 1317 1318 1319 1320 1321
			if (mpext->ack64) {
				put_unaligned_be64(mpext->data_ack, ptr);
				ptr += 2;
			} else {
				put_unaligned_be32(mpext->data_ack32, ptr);
				ptr += 1;
			}
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
		}

		if (mpext->use_map) {
			put_unaligned_be64(mpext->data_seq, ptr);
			ptr += 2;
			put_unaligned_be32(mpext->subflow_seq, ptr);
			ptr += 1;
			put_unaligned_be32(mpext->data_len << 16 |
					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
		}
	}
1333 1334 1335

	if (tp)
		mptcp_set_rwin(tp);
P
Peter Krystad 已提交
1336
}
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

__be32 mptcp_get_reset_option(const struct sk_buff *skb)
{
	const struct mptcp_ext *ext = mptcp_get_ext(skb);
	u8 flags, reason;

	if (ext) {
		flags = ext->reset_transient;
		reason = ext->reset_reason;

		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
				    flags, reason);
	}

	return htonl(0u);
}
EXPORT_SYMBOL_GPL(mptcp_get_reset_option);