1 // SPDX-License-Identifier: GPL-2.0 2 /* Multipath TCP 3 * 4 * Copyright (c) 2017 - 2019, Intel Corporation. 5 */ 6 7 #define pr_fmt(fmt) "MPTCP: " fmt 8 9 #include <linux/kernel.h> 10 #include <crypto/sha.h> 11 #include <net/tcp.h> 12 #include <net/mptcp.h> 13 #include "protocol.h" 14 15 static bool mptcp_cap_flag_sha256(u8 flags) 16 { 17 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256; 18 } 19 20 static void mptcp_parse_option(const struct sk_buff *skb, 21 const unsigned char *ptr, int opsize, 22 struct mptcp_options_received *mp_opt) 23 { 24 u8 subtype = *ptr >> 4; 25 int expected_opsize; 26 u8 version; 27 u8 flags; 28 29 switch (subtype) { 30 case MPTCPOPT_MP_CAPABLE: 31 /* strict size checking */ 32 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 33 if (skb->len > tcp_hdr(skb)->doff << 2) 34 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA; 35 else 36 expected_opsize = TCPOLEN_MPTCP_MPC_ACK; 37 } else { 38 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) 39 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK; 40 else 41 expected_opsize = TCPOLEN_MPTCP_MPC_SYN; 42 } 43 if (opsize != expected_opsize) 44 break; 45 46 /* try to be gentle vs future versions on the initial syn */ 47 version = *ptr++ & MPTCP_VERSION_MASK; 48 if (opsize != TCPOLEN_MPTCP_MPC_SYN) { 49 if (version != MPTCP_SUPPORTED_VERSION) 50 break; 51 } else if (version < MPTCP_SUPPORTED_VERSION) { 52 break; 53 } 54 55 flags = *ptr++; 56 if (!mptcp_cap_flag_sha256(flags) || 57 (flags & MPTCP_CAP_EXTENSIBILITY)) 58 break; 59 60 /* RFC 6824, Section 3.1: 61 * "For the Checksum Required bit (labeled "A"), if either 62 * host requires the use of checksums, checksums MUST be used. 63 * In other words, the only way for checksums not to be used 64 * is if both hosts in their SYNs set A=0." 65 * 66 * Section 3.3.0: 67 * "If a checksum is not present when its use has been 68 * negotiated, the receiver MUST close the subflow with a RST as 69 * it is considered broken." 70 * 71 * We don't implement DSS checksum - fall back to TCP. 72 */ 73 if (flags & MPTCP_CAP_CHECKSUM_REQD) 74 break; 75 76 mp_opt->mp_capable = 1; 77 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) { 78 mp_opt->sndr_key = get_unaligned_be64(ptr); 79 ptr += 8; 80 } 81 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) { 82 mp_opt->rcvr_key = get_unaligned_be64(ptr); 83 ptr += 8; 84 } 85 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) { 86 /* Section 3.1.: 87 * "the data parameters in a MP_CAPABLE are semantically 88 * equivalent to those in a DSS option and can be used 89 * interchangeably." 90 */ 91 mp_opt->dss = 1; 92 mp_opt->use_map = 1; 93 mp_opt->mpc_map = 1; 94 mp_opt->data_len = get_unaligned_be16(ptr); 95 ptr += 2; 96 } 97 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d", 98 version, flags, opsize, mp_opt->sndr_key, 99 mp_opt->rcvr_key, mp_opt->data_len); 100 break; 101 102 case MPTCPOPT_MP_JOIN: 103 mp_opt->mp_join = 1; 104 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) { 105 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 106 mp_opt->join_id = *ptr++; 107 mp_opt->token = get_unaligned_be32(ptr); 108 ptr += 4; 109 mp_opt->nonce = get_unaligned_be32(ptr); 110 ptr += 4; 111 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u", 112 mp_opt->backup, mp_opt->join_id, 113 mp_opt->token, mp_opt->nonce); 114 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) { 115 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 116 mp_opt->join_id = *ptr++; 117 mp_opt->thmac = get_unaligned_be64(ptr); 118 ptr += 8; 119 mp_opt->nonce = get_unaligned_be32(ptr); 120 ptr += 4; 121 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u", 122 mp_opt->backup, mp_opt->join_id, 123 mp_opt->thmac, mp_opt->nonce); 124 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) { 125 ptr += 2; 126 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN); 127 pr_debug("MP_JOIN hmac"); 128 } else { 129 pr_warn("MP_JOIN bad option size"); 130 mp_opt->mp_join = 0; 131 } 132 break; 133 134 case MPTCPOPT_DSS: 135 pr_debug("DSS"); 136 ptr++; 137 138 /* we must clear 'mpc_map' be able to detect MP_CAPABLE 139 * map vs DSS map in mptcp_incoming_options(), and reconstruct 140 * map info accordingly 141 */ 142 mp_opt->mpc_map = 0; 143 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK; 144 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0; 145 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0; 146 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0; 147 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0; 148 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK); 149 150 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d", 151 mp_opt->data_fin, mp_opt->dsn64, 152 mp_opt->use_map, mp_opt->ack64, 153 mp_opt->use_ack); 154 155 expected_opsize = TCPOLEN_MPTCP_DSS_BASE; 156 157 if (mp_opt->use_ack) { 158 if (mp_opt->ack64) 159 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64; 160 else 161 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32; 162 } 163 164 if (mp_opt->use_map) { 165 if (mp_opt->dsn64) 166 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64; 167 else 168 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32; 169 } 170 171 /* RFC 6824, Section 3.3: 172 * If a checksum is present, but its use had 173 * not been negotiated in the MP_CAPABLE handshake, 174 * the checksum field MUST be ignored. 175 */ 176 if (opsize != expected_opsize && 177 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) 178 break; 179 180 mp_opt->dss = 1; 181 182 if (mp_opt->use_ack) { 183 if (mp_opt->ack64) { 184 mp_opt->data_ack = get_unaligned_be64(ptr); 185 ptr += 8; 186 } else { 187 mp_opt->data_ack = get_unaligned_be32(ptr); 188 ptr += 4; 189 } 190 191 pr_debug("data_ack=%llu", mp_opt->data_ack); 192 } 193 194 if (mp_opt->use_map) { 195 if (mp_opt->dsn64) { 196 mp_opt->data_seq = get_unaligned_be64(ptr); 197 ptr += 8; 198 } else { 199 mp_opt->data_seq = get_unaligned_be32(ptr); 200 ptr += 4; 201 } 202 203 mp_opt->subflow_seq = get_unaligned_be32(ptr); 204 ptr += 4; 205 206 mp_opt->data_len = get_unaligned_be16(ptr); 207 ptr += 2; 208 209 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u", 210 mp_opt->data_seq, mp_opt->subflow_seq, 211 mp_opt->data_len); 212 } 213 214 break; 215 216 case MPTCPOPT_ADD_ADDR: 217 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO; 218 if (!mp_opt->echo) { 219 if (opsize == TCPOLEN_MPTCP_ADD_ADDR || 220 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT) 221 mp_opt->family = MPTCP_ADDR_IPVERSION_4; 222 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 223 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 || 224 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT) 225 mp_opt->family = MPTCP_ADDR_IPVERSION_6; 226 #endif 227 else 228 break; 229 } else { 230 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE || 231 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) 232 mp_opt->family = MPTCP_ADDR_IPVERSION_4; 233 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 234 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE || 235 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) 236 mp_opt->family = MPTCP_ADDR_IPVERSION_6; 237 #endif 238 else 239 break; 240 } 241 242 mp_opt->add_addr = 1; 243 mp_opt->port = 0; 244 mp_opt->addr_id = *ptr++; 245 pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id); 246 if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) { 247 memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4); 248 ptr += 4; 249 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT || 250 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) { 251 mp_opt->port = get_unaligned_be16(ptr); 252 ptr += 2; 253 } 254 } 255 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 256 else { 257 memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16); 258 ptr += 16; 259 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT || 260 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) { 261 mp_opt->port = get_unaligned_be16(ptr); 262 ptr += 2; 263 } 264 } 265 #endif 266 if (!mp_opt->echo) { 267 mp_opt->ahmac = get_unaligned_be64(ptr); 268 ptr += 8; 269 } 270 break; 271 272 case MPTCPOPT_RM_ADDR: 273 if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE) 274 break; 275 276 mp_opt->rm_addr = 1; 277 mp_opt->rm_id = *ptr++; 278 pr_debug("RM_ADDR: id=%d", mp_opt->rm_id); 279 break; 280 281 default: 282 break; 283 } 284 } 285 286 void mptcp_get_options(const struct sk_buff *skb, 287 struct mptcp_options_received *mp_opt) 288 { 289 const struct tcphdr *th = tcp_hdr(skb); 290 const unsigned char *ptr; 291 int length; 292 293 /* initialize option status */ 294 mp_opt->mp_capable = 0; 295 mp_opt->mp_join = 0; 296 mp_opt->add_addr = 0; 297 mp_opt->rm_addr = 0; 298 mp_opt->dss = 0; 299 300 length = (th->doff * 4) - sizeof(struct tcphdr); 301 ptr = (const unsigned char *)(th + 1); 302 303 while (length > 0) { 304 int opcode = *ptr++; 305 int opsize; 306 307 switch (opcode) { 308 case TCPOPT_EOL: 309 return; 310 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 311 length--; 312 continue; 313 default: 314 opsize = *ptr++; 315 if (opsize < 2) /* "silly options" */ 316 return; 317 if (opsize > length) 318 return; /* don't parse partial options */ 319 if (opcode == TCPOPT_MPTCP) 320 mptcp_parse_option(skb, ptr, opsize, mp_opt); 321 ptr += opsize - 2; 322 length -= opsize; 323 } 324 } 325 } 326 327 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb, 328 unsigned int *size, struct mptcp_out_options *opts) 329 { 330 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 331 332 /* we will use snd_isn to detect first pkt [re]transmission 333 * in mptcp_established_options_mp() 334 */ 335 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq; 336 if (subflow->request_mptcp) { 337 pr_debug("local_key=%llu", subflow->local_key); 338 opts->suboptions = OPTION_MPTCP_MPC_SYN; 339 opts->sndr_key = subflow->local_key; 340 *size = TCPOLEN_MPTCP_MPC_SYN; 341 return true; 342 } else if (subflow->request_join) { 343 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token, 344 subflow->local_nonce); 345 opts->suboptions = OPTION_MPTCP_MPJ_SYN; 346 opts->join_id = subflow->local_id; 347 opts->token = subflow->remote_token; 348 opts->nonce = subflow->local_nonce; 349 opts->backup = subflow->request_bkup; 350 *size = TCPOLEN_MPTCP_MPJ_SYN; 351 return true; 352 } 353 return false; 354 } 355 356 /* MP_JOIN client subflow must wait for 4th ack before sending any data: 357 * TCP can't schedule delack timer before the subflow is fully established. 358 * MPTCP uses the delack timer to do 3rd ack retransmissions 359 */ 360 static void schedule_3rdack_retransmission(struct sock *sk) 361 { 362 struct inet_connection_sock *icsk = inet_csk(sk); 363 struct tcp_sock *tp = tcp_sk(sk); 364 unsigned long timeout; 365 366 /* reschedule with a timeout above RTT, as we must look only for drop */ 367 if (tp->srtt_us) 368 timeout = tp->srtt_us << 1; 369 else 370 timeout = TCP_TIMEOUT_INIT; 371 372 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 373 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 374 icsk->icsk_ack.timeout = timeout; 375 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 376 } 377 378 static void clear_3rdack_retransmission(struct sock *sk) 379 { 380 struct inet_connection_sock *icsk = inet_csk(sk); 381 382 sk_stop_timer(sk, &icsk->icsk_delack_timer); 383 icsk->icsk_ack.timeout = 0; 384 icsk->icsk_ack.ato = 0; 385 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER); 386 } 387 388 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb, 389 unsigned int *size, 390 unsigned int remaining, 391 struct mptcp_out_options *opts) 392 { 393 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 394 struct mptcp_ext *mpext; 395 unsigned int data_len; 396 397 /* When skb is not available, we better over-estimate the emitted 398 * options len. A full DSS option (28 bytes) is longer than 399 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so 400 * tell the caller to defer the estimate to 401 * mptcp_established_options_dss(), which will reserve enough space. 402 */ 403 if (!skb) 404 return false; 405 406 /* MPC/MPJ needed only on 3rd ack packet */ 407 if (subflow->fully_established || 408 subflow->snd_isn != TCP_SKB_CB(skb)->seq) 409 return false; 410 411 if (subflow->mp_capable) { 412 mpext = mptcp_get_ext(skb); 413 data_len = mpext ? mpext->data_len : 0; 414 415 /* we will check ext_copy.data_len in mptcp_write_options() to 416 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and 417 * TCPOLEN_MPTCP_MPC_ACK 418 */ 419 opts->ext_copy.data_len = data_len; 420 opts->suboptions = OPTION_MPTCP_MPC_ACK; 421 opts->sndr_key = subflow->local_key; 422 opts->rcvr_key = subflow->remote_key; 423 424 /* Section 3.1. 425 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK 426 * packets that start the first subflow of an MPTCP connection, 427 * as well as the first packet that carries data 428 */ 429 if (data_len > 0) 430 *size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4); 431 else 432 *size = TCPOLEN_MPTCP_MPC_ACK; 433 434 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d", 435 subflow, subflow->local_key, subflow->remote_key, 436 data_len); 437 438 return true; 439 } else if (subflow->mp_join) { 440 opts->suboptions = OPTION_MPTCP_MPJ_ACK; 441 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN); 442 *size = TCPOLEN_MPTCP_MPJ_ACK; 443 pr_debug("subflow=%p", subflow); 444 445 schedule_3rdack_retransmission(sk); 446 return true; 447 } 448 return false; 449 } 450 451 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow, 452 struct mptcp_ext *ext) 453 { 454 if (!ext->use_map) { 455 /* RFC6824 requires a DSS mapping with specific values 456 * if DATA_FIN is set but no data payload is mapped 457 */ 458 ext->data_fin = 1; 459 ext->use_map = 1; 460 ext->dsn64 = 1; 461 ext->data_seq = subflow->data_fin_tx_seq; 462 ext->subflow_seq = 0; 463 ext->data_len = 1; 464 } else if (ext->data_seq + ext->data_len == subflow->data_fin_tx_seq) { 465 /* If there's an existing DSS mapping and it is the 466 * final mapping, DATA_FIN consumes 1 additional byte of 467 * mapping space. 468 */ 469 ext->data_fin = 1; 470 ext->data_len++; 471 } 472 } 473 474 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb, 475 unsigned int *size, 476 unsigned int remaining, 477 struct mptcp_out_options *opts) 478 { 479 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 480 unsigned int dss_size = 0; 481 struct mptcp_ext *mpext; 482 struct mptcp_sock *msk; 483 unsigned int ack_size; 484 bool ret = false; 485 u8 tcp_fin; 486 487 if (skb) { 488 mpext = mptcp_get_ext(skb); 489 tcp_fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 490 } else { 491 mpext = NULL; 492 tcp_fin = 0; 493 } 494 495 if (!skb || (mpext && mpext->use_map) || tcp_fin) { 496 unsigned int map_size; 497 498 map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64; 499 500 remaining -= map_size; 501 dss_size = map_size; 502 if (mpext) 503 opts->ext_copy = *mpext; 504 505 if (skb && tcp_fin && subflow->data_fin_tx_enable) 506 mptcp_write_data_fin(subflow, &opts->ext_copy); 507 ret = true; 508 } 509 510 /* passive sockets msk will set the 'can_ack' after accept(), even 511 * if the first subflow may have the already the remote key handy 512 */ 513 opts->ext_copy.use_ack = 0; 514 msk = mptcp_sk(subflow->conn); 515 if (!READ_ONCE(msk->can_ack)) { 516 *size = ALIGN(dss_size, 4); 517 return ret; 518 } 519 520 ack_size = TCPOLEN_MPTCP_DSS_ACK64; 521 522 /* Add kind/length/subtype/flag overhead if mapping is not populated */ 523 if (dss_size == 0) 524 ack_size += TCPOLEN_MPTCP_DSS_BASE; 525 526 dss_size += ack_size; 527 528 opts->ext_copy.data_ack = msk->ack_seq; 529 opts->ext_copy.ack64 = 1; 530 opts->ext_copy.use_ack = 1; 531 532 *size = ALIGN(dss_size, 4); 533 return true; 534 } 535 536 static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id, 537 struct in_addr *addr) 538 { 539 u8 hmac[SHA256_DIGEST_SIZE]; 540 u8 msg[7]; 541 542 msg[0] = addr_id; 543 memcpy(&msg[1], &addr->s_addr, 4); 544 msg[5] = 0; 545 msg[6] = 0; 546 547 mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac); 548 549 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); 550 } 551 552 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 553 static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id, 554 struct in6_addr *addr) 555 { 556 u8 hmac[SHA256_DIGEST_SIZE]; 557 u8 msg[19]; 558 559 msg[0] = addr_id; 560 memcpy(&msg[1], &addr->s6_addr, 16); 561 msg[17] = 0; 562 msg[18] = 0; 563 564 mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac); 565 566 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); 567 } 568 #endif 569 570 static bool mptcp_established_options_addr(struct sock *sk, 571 unsigned int *size, 572 unsigned int remaining, 573 struct mptcp_out_options *opts) 574 { 575 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 576 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 577 struct mptcp_addr_info saddr; 578 int len; 579 580 if (!mptcp_pm_should_signal(msk) || 581 !(mptcp_pm_addr_signal(msk, remaining, &saddr))) 582 return false; 583 584 len = mptcp_add_addr_len(saddr.family); 585 if (remaining < len) 586 return false; 587 588 *size = len; 589 opts->addr_id = saddr.id; 590 if (saddr.family == AF_INET) { 591 opts->suboptions |= OPTION_MPTCP_ADD_ADDR; 592 opts->addr = saddr.addr; 593 opts->ahmac = add_addr_generate_hmac(msk->local_key, 594 msk->remote_key, 595 opts->addr_id, 596 &opts->addr); 597 } 598 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 599 else if (saddr.family == AF_INET6) { 600 opts->suboptions |= OPTION_MPTCP_ADD_ADDR6; 601 opts->addr6 = saddr.addr6; 602 opts->ahmac = add_addr6_generate_hmac(msk->local_key, 603 msk->remote_key, 604 opts->addr_id, 605 &opts->addr6); 606 } 607 #endif 608 pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac); 609 610 return true; 611 } 612 613 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb, 614 unsigned int *size, unsigned int remaining, 615 struct mptcp_out_options *opts) 616 { 617 unsigned int opt_size = 0; 618 bool ret = false; 619 620 opts->suboptions = 0; 621 622 if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts)) 623 ret = true; 624 else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining, 625 opts)) 626 ret = true; 627 628 /* we reserved enough space for the above options, and exceeding the 629 * TCP option space would be fatal 630 */ 631 if (WARN_ON_ONCE(opt_size > remaining)) 632 return false; 633 634 *size += opt_size; 635 remaining -= opt_size; 636 if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) { 637 *size += opt_size; 638 remaining -= opt_size; 639 ret = true; 640 } 641 642 return ret; 643 } 644 645 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, 646 struct mptcp_out_options *opts) 647 { 648 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 649 650 if (subflow_req->mp_capable) { 651 opts->suboptions = OPTION_MPTCP_MPC_SYNACK; 652 opts->sndr_key = subflow_req->local_key; 653 *size = TCPOLEN_MPTCP_MPC_SYNACK; 654 pr_debug("subflow_req=%p, local_key=%llu", 655 subflow_req, subflow_req->local_key); 656 return true; 657 } else if (subflow_req->mp_join) { 658 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK; 659 opts->backup = subflow_req->backup; 660 opts->join_id = subflow_req->local_id; 661 opts->thmac = subflow_req->thmac; 662 opts->nonce = subflow_req->local_nonce; 663 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u", 664 subflow_req, opts->backup, opts->join_id, 665 opts->thmac, opts->nonce); 666 *size = TCPOLEN_MPTCP_MPJ_SYNACK; 667 return true; 668 } 669 return false; 670 } 671 672 static bool check_fully_established(struct mptcp_sock *msk, struct sock *sk, 673 struct mptcp_subflow_context *subflow, 674 struct sk_buff *skb, 675 struct mptcp_options_received *mp_opt) 676 { 677 /* here we can process OoO, in-window pkts, only in-sequence 4th ack 678 * will make the subflow fully established 679 */ 680 if (likely(subflow->fully_established)) { 681 /* on passive sockets, check for 3rd ack retransmission 682 * note that msk is always set by subflow_syn_recv_sock() 683 * for mp_join subflows 684 */ 685 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 && 686 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq && 687 subflow->mp_join && mp_opt->mp_join && 688 READ_ONCE(msk->pm.server_side)) 689 tcp_send_ack(sk); 690 goto fully_established; 691 } 692 693 /* we should process OoO packets before the first subflow is fully 694 * established, but not expected for MP_JOIN subflows 695 */ 696 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) 697 return subflow->mp_capable; 698 699 if (mp_opt->dss && mp_opt->use_ack) { 700 /* subflows are fully established as soon as we get any 701 * additional ack. 702 */ 703 subflow->fully_established = 1; 704 goto fully_established; 705 } 706 707 /* If the first established packet does not contain MP_CAPABLE + data 708 * then fallback to TCP 709 */ 710 if (!mp_opt->mp_capable) { 711 subflow->mp_capable = 0; 712 tcp_sk(sk)->is_mptcp = 0; 713 return false; 714 } 715 716 if (unlikely(!READ_ONCE(msk->pm.server_side))) 717 pr_warn_once("bogus mpc option on established client sk"); 718 subflow->fully_established = 1; 719 subflow->remote_key = mp_opt->sndr_key; 720 subflow->can_ack = 1; 721 722 fully_established: 723 if (likely(subflow->pm_notified)) 724 return true; 725 726 subflow->pm_notified = 1; 727 if (subflow->mp_join) { 728 clear_3rdack_retransmission(sk); 729 mptcp_pm_subflow_established(msk, subflow); 730 } else { 731 mptcp_pm_fully_established(msk); 732 } 733 return true; 734 } 735 736 static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit) 737 { 738 u32 old_ack32, cur_ack32; 739 740 if (use_64bit) 741 return cur_ack; 742 743 old_ack32 = (u32)old_ack; 744 cur_ack32 = (u32)cur_ack; 745 cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32; 746 if (unlikely(before(cur_ack32, old_ack32))) 747 return cur_ack + (1LL << 32); 748 return cur_ack; 749 } 750 751 static void update_una(struct mptcp_sock *msk, 752 struct mptcp_options_received *mp_opt) 753 { 754 u64 new_snd_una, snd_una, old_snd_una = atomic64_read(&msk->snd_una); 755 u64 write_seq = READ_ONCE(msk->write_seq); 756 757 /* avoid ack expansion on update conflict, to reduce the risk of 758 * wrongly expanding to a future ack sequence number, which is way 759 * more dangerous than missing an ack 760 */ 761 new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64); 762 763 /* ACK for data not even sent yet? Ignore. */ 764 if (after64(new_snd_una, write_seq)) 765 new_snd_una = old_snd_una; 766 767 while (after64(new_snd_una, old_snd_una)) { 768 snd_una = old_snd_una; 769 old_snd_una = atomic64_cmpxchg(&msk->snd_una, snd_una, 770 new_snd_una); 771 if (old_snd_una == snd_una) { 772 mptcp_data_acked((struct sock *)msk); 773 break; 774 } 775 } 776 } 777 778 static bool add_addr_hmac_valid(struct mptcp_sock *msk, 779 struct mptcp_options_received *mp_opt) 780 { 781 u64 hmac = 0; 782 783 if (mp_opt->echo) 784 return true; 785 786 if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) 787 hmac = add_addr_generate_hmac(msk->remote_key, 788 msk->local_key, 789 mp_opt->addr_id, &mp_opt->addr); 790 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 791 else 792 hmac = add_addr6_generate_hmac(msk->remote_key, 793 msk->local_key, 794 mp_opt->addr_id, &mp_opt->addr6); 795 #endif 796 797 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n", 798 msk, (unsigned long long)hmac, 799 (unsigned long long)mp_opt->ahmac); 800 801 return hmac == mp_opt->ahmac; 802 } 803 804 void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb, 805 struct tcp_options_received *opt_rx) 806 { 807 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 808 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 809 struct mptcp_options_received mp_opt; 810 struct mptcp_ext *mpext; 811 812 mptcp_get_options(skb, &mp_opt); 813 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt)) 814 return; 815 816 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) { 817 struct mptcp_addr_info addr; 818 819 addr.port = htons(mp_opt.port); 820 addr.id = mp_opt.addr_id; 821 if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) { 822 addr.family = AF_INET; 823 addr.addr = mp_opt.addr; 824 } 825 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 826 else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) { 827 addr.family = AF_INET6; 828 addr.addr6 = mp_opt.addr6; 829 } 830 #endif 831 if (!mp_opt.echo) 832 mptcp_pm_add_addr_received(msk, &addr); 833 mp_opt.add_addr = 0; 834 } 835 836 if (!mp_opt.dss) 837 return; 838 839 /* we can't wait for recvmsg() to update the ack_seq, otherwise 840 * monodirectional flows will stuck 841 */ 842 if (mp_opt.use_ack) 843 update_una(msk, &mp_opt); 844 845 mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 846 if (!mpext) 847 return; 848 849 memset(mpext, 0, sizeof(*mpext)); 850 851 if (mp_opt.use_map) { 852 if (mp_opt.mpc_map) { 853 /* this is an MP_CAPABLE carrying MPTCP data 854 * we know this map the first chunk of data 855 */ 856 mptcp_crypto_key_sha(subflow->remote_key, NULL, 857 &mpext->data_seq); 858 mpext->data_seq++; 859 mpext->subflow_seq = 1; 860 mpext->dsn64 = 1; 861 mpext->mpc_map = 1; 862 mpext->data_fin = 0; 863 } else { 864 mpext->data_seq = mp_opt.data_seq; 865 mpext->subflow_seq = mp_opt.subflow_seq; 866 mpext->dsn64 = mp_opt.dsn64; 867 mpext->data_fin = mp_opt.data_fin; 868 } 869 mpext->data_len = mp_opt.data_len; 870 mpext->use_map = 1; 871 } 872 } 873 874 void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts) 875 { 876 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK | 877 OPTION_MPTCP_MPC_ACK) & opts->suboptions) { 878 u8 len; 879 880 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) 881 len = TCPOLEN_MPTCP_MPC_SYN; 882 else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) 883 len = TCPOLEN_MPTCP_MPC_SYNACK; 884 else if (opts->ext_copy.data_len) 885 len = TCPOLEN_MPTCP_MPC_ACK_DATA; 886 else 887 len = TCPOLEN_MPTCP_MPC_ACK; 888 889 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len, 890 MPTCP_SUPPORTED_VERSION, 891 MPTCP_CAP_HMAC_SHA256); 892 893 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) & 894 opts->suboptions)) 895 goto mp_capable_done; 896 897 put_unaligned_be64(opts->sndr_key, ptr); 898 ptr += 2; 899 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions)) 900 goto mp_capable_done; 901 902 put_unaligned_be64(opts->rcvr_key, ptr); 903 ptr += 2; 904 if (!opts->ext_copy.data_len) 905 goto mp_capable_done; 906 907 put_unaligned_be32(opts->ext_copy.data_len << 16 | 908 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 909 ptr += 1; 910 } 911 912 mp_capable_done: 913 if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) { 914 if (opts->ahmac) 915 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 916 TCPOLEN_MPTCP_ADD_ADDR, 0, 917 opts->addr_id); 918 else 919 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 920 TCPOLEN_MPTCP_ADD_ADDR_BASE, 921 MPTCP_ADDR_ECHO, 922 opts->addr_id); 923 memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4); 924 ptr += 1; 925 if (opts->ahmac) { 926 put_unaligned_be64(opts->ahmac, ptr); 927 ptr += 2; 928 } 929 } 930 931 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 932 if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) { 933 if (opts->ahmac) 934 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 935 TCPOLEN_MPTCP_ADD_ADDR6, 0, 936 opts->addr_id); 937 else 938 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 939 TCPOLEN_MPTCP_ADD_ADDR6_BASE, 940 MPTCP_ADDR_ECHO, 941 opts->addr_id); 942 memcpy((u8 *)ptr, opts->addr6.s6_addr, 16); 943 ptr += 4; 944 if (opts->ahmac) { 945 put_unaligned_be64(opts->ahmac, ptr); 946 ptr += 2; 947 } 948 } 949 #endif 950 951 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) { 952 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR, 953 TCPOLEN_MPTCP_RM_ADDR_BASE, 954 0, opts->rm_id); 955 } 956 957 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) { 958 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 959 TCPOLEN_MPTCP_MPJ_SYN, 960 opts->backup, opts->join_id); 961 put_unaligned_be32(opts->token, ptr); 962 ptr += 1; 963 put_unaligned_be32(opts->nonce, ptr); 964 ptr += 1; 965 } 966 967 if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) { 968 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 969 TCPOLEN_MPTCP_MPJ_SYNACK, 970 opts->backup, opts->join_id); 971 put_unaligned_be64(opts->thmac, ptr); 972 ptr += 2; 973 put_unaligned_be32(opts->nonce, ptr); 974 ptr += 1; 975 } 976 977 if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) { 978 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 979 TCPOLEN_MPTCP_MPJ_ACK, 0, 0); 980 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN); 981 ptr += 5; 982 } 983 984 if (opts->ext_copy.use_ack || opts->ext_copy.use_map) { 985 struct mptcp_ext *mpext = &opts->ext_copy; 986 u8 len = TCPOLEN_MPTCP_DSS_BASE; 987 u8 flags = 0; 988 989 if (mpext->use_ack) { 990 len += TCPOLEN_MPTCP_DSS_ACK64; 991 flags = MPTCP_DSS_HAS_ACK | MPTCP_DSS_ACK64; 992 } 993 994 if (mpext->use_map) { 995 len += TCPOLEN_MPTCP_DSS_MAP64; 996 997 /* Use only 64-bit mapping flags for now, add 998 * support for optional 32-bit mappings later. 999 */ 1000 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64; 1001 if (mpext->data_fin) 1002 flags |= MPTCP_DSS_DATA_FIN; 1003 } 1004 1005 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags); 1006 1007 if (mpext->use_ack) { 1008 put_unaligned_be64(mpext->data_ack, ptr); 1009 ptr += 2; 1010 } 1011 1012 if (mpext->use_map) { 1013 put_unaligned_be64(mpext->data_seq, ptr); 1014 ptr += 2; 1015 put_unaligned_be32(mpext->subflow_seq, ptr); 1016 ptr += 1; 1017 put_unaligned_be32(mpext->data_len << 16 | 1018 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 1019 } 1020 } 1021 } 1022