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