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