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 <linux/module.h> 11 #include <linux/netdevice.h> 12 #include <crypto/sha2.h> 13 #include <crypto/utils.h> 14 #include <net/sock.h> 15 #include <net/inet_common.h> 16 #include <net/inet_hashtables.h> 17 #include <net/protocol.h> 18 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 19 #include <net/ip6_route.h> 20 #include <net/transp_v6.h> 21 #endif 22 #include <net/mptcp.h> 23 24 #include "protocol.h" 25 #include "mib.h" 26 27 #include <trace/events/mptcp.h> 28 #include <trace/events/sock.h> 29 30 static void mptcp_subflow_ops_undo_override(struct sock *ssk); 31 32 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req, 33 enum linux_mptcp_mib_field field) 34 { 35 MPTCP_INC_STATS(sock_net(req_to_sk(req)), field); 36 } 37 38 static void subflow_req_destructor(struct request_sock *req) 39 { 40 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 41 42 pr_debug("subflow_req=%p\n", subflow_req); 43 44 if (subflow_req->msk) 45 sock_put((struct sock *)subflow_req->msk); 46 47 mptcp_token_destroy_request(req); 48 } 49 50 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2, 51 void *hmac) 52 { 53 u8 msg[8]; 54 55 put_unaligned_be32(nonce1, &msg[0]); 56 put_unaligned_be32(nonce2, &msg[4]); 57 58 mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac); 59 } 60 61 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk) 62 { 63 return mptcp_is_fully_established((void *)msk) && 64 ((mptcp_pm_is_userspace(msk) && 65 mptcp_userspace_pm_active(msk)) || 66 READ_ONCE(msk->pm.accept_subflow)); 67 } 68 69 /* validate received token and create truncated hmac and nonce for SYN-ACK */ 70 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req) 71 { 72 struct mptcp_sock *msk = subflow_req->msk; 73 u8 hmac[SHA256_DIGEST_SIZE]; 74 75 get_random_bytes(&subflow_req->local_nonce, sizeof(u32)); 76 77 subflow_generate_hmac(READ_ONCE(msk->local_key), 78 READ_ONCE(msk->remote_key), 79 subflow_req->local_nonce, 80 subflow_req->remote_nonce, hmac); 81 82 subflow_req->thmac = get_unaligned_be64(hmac); 83 } 84 85 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req) 86 { 87 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 88 struct mptcp_sock *msk; 89 int local_id; 90 91 msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token); 92 if (!msk) { 93 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN); 94 return NULL; 95 } 96 97 local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req); 98 if (local_id < 0) { 99 sock_put((struct sock *)msk); 100 return NULL; 101 } 102 subflow_req->local_id = local_id; 103 subflow_req->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)req); 104 105 return msk; 106 } 107 108 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener) 109 { 110 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 111 112 subflow_req->mp_capable = 0; 113 subflow_req->mp_join = 0; 114 subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener)); 115 subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener)); 116 subflow_req->msk = NULL; 117 mptcp_token_init_request(req); 118 } 119 120 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk) 121 { 122 return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport; 123 } 124 125 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason) 126 { 127 struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 128 129 if (mpext) { 130 memset(mpext, 0, sizeof(*mpext)); 131 mpext->reset_reason = reason; 132 } 133 } 134 135 static int subflow_reset_req_endp(struct request_sock *req, struct sk_buff *skb) 136 { 137 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEENDPATTEMPT); 138 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 139 return -EPERM; 140 } 141 142 /* Init mptcp request socket. 143 * 144 * Returns an error code if a JOIN has failed and a TCP reset 145 * should be sent. 146 */ 147 static int subflow_check_req(struct request_sock *req, 148 const struct sock *sk_listener, 149 struct sk_buff *skb) 150 { 151 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 152 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 153 struct mptcp_options_received mp_opt; 154 bool opt_mp_capable, opt_mp_join; 155 156 pr_debug("subflow_req=%p, listener=%p\n", subflow_req, listener); 157 158 #ifdef CONFIG_TCP_MD5SIG 159 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 160 * TCP option space. 161 */ 162 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) { 163 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 164 return -EINVAL; 165 } 166 #endif 167 168 mptcp_get_options(skb, &mp_opt); 169 170 opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYN); 171 opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYN); 172 if (opt_mp_capable) { 173 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE); 174 175 if (unlikely(listener->pm_listener)) 176 return subflow_reset_req_endp(req, skb); 177 if (opt_mp_join) 178 return 0; 179 } else if (opt_mp_join) { 180 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX); 181 182 if (mp_opt.backup) 183 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNBACKUPRX); 184 } else if (unlikely(listener->pm_listener)) { 185 return subflow_reset_req_endp(req, skb); 186 } 187 188 if (opt_mp_capable && listener->request_mptcp) { 189 int err, retries = MPTCP_TOKEN_MAX_RETRIES; 190 191 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 192 again: 193 do { 194 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key)); 195 } while (subflow_req->local_key == 0); 196 197 if (unlikely(req->syncookie)) { 198 mptcp_crypto_key_sha(subflow_req->local_key, 199 &subflow_req->token, 200 &subflow_req->idsn); 201 if (mptcp_token_exists(subflow_req->token)) { 202 if (retries-- > 0) 203 goto again; 204 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 205 } else { 206 subflow_req->mp_capable = 1; 207 } 208 return 0; 209 } 210 211 err = mptcp_token_new_request(req); 212 if (err == 0) 213 subflow_req->mp_capable = 1; 214 else if (retries-- > 0) 215 goto again; 216 else 217 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 218 219 } else if (opt_mp_join && listener->request_mptcp) { 220 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 221 subflow_req->mp_join = 1; 222 subflow_req->backup = mp_opt.backup; 223 subflow_req->remote_id = mp_opt.join_id; 224 subflow_req->token = mp_opt.token; 225 subflow_req->remote_nonce = mp_opt.nonce; 226 subflow_req->msk = subflow_token_join_request(req); 227 228 /* Can't fall back to TCP in this case. */ 229 if (!subflow_req->msk) { 230 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 231 return -EPERM; 232 } 233 234 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) { 235 pr_debug("syn inet_sport=%d %d\n", 236 ntohs(inet_sk(sk_listener)->inet_sport), 237 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport)); 238 if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) { 239 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX); 240 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 241 return -EPERM; 242 } 243 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX); 244 } 245 246 subflow_req_create_thmac(subflow_req); 247 248 if (unlikely(req->syncookie)) { 249 if (!mptcp_can_accept_new_subflow(subflow_req->msk)) { 250 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED); 251 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 252 return -EPERM; 253 } 254 255 subflow_init_req_cookie_join_save(subflow_req, skb); 256 } 257 258 pr_debug("token=%u, remote_nonce=%u msk=%p\n", subflow_req->token, 259 subflow_req->remote_nonce, subflow_req->msk); 260 } 261 262 return 0; 263 } 264 265 int mptcp_subflow_init_cookie_req(struct request_sock *req, 266 const struct sock *sk_listener, 267 struct sk_buff *skb) 268 { 269 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 270 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 271 struct mptcp_options_received mp_opt; 272 bool opt_mp_capable, opt_mp_join; 273 int err; 274 275 subflow_init_req(req, sk_listener); 276 mptcp_get_options(skb, &mp_opt); 277 278 opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK); 279 opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK); 280 if (opt_mp_capable && opt_mp_join) 281 return -EINVAL; 282 283 if (opt_mp_capable && listener->request_mptcp) { 284 if (mp_opt.sndr_key == 0) 285 return -EINVAL; 286 287 subflow_req->local_key = mp_opt.rcvr_key; 288 err = mptcp_token_new_request(req); 289 if (err) 290 return err; 291 292 subflow_req->mp_capable = 1; 293 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 294 } else if (opt_mp_join && listener->request_mptcp) { 295 if (!mptcp_token_join_cookie_init_state(subflow_req, skb)) 296 return -EINVAL; 297 298 subflow_req->mp_join = 1; 299 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 300 } 301 302 return 0; 303 } 304 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req); 305 306 static enum sk_rst_reason mptcp_get_rst_reason(const struct sk_buff *skb) 307 { 308 const struct mptcp_ext *mpext = mptcp_get_ext(skb); 309 310 if (!mpext) 311 return SK_RST_REASON_NOT_SPECIFIED; 312 313 return sk_rst_convert_mptcp_reason(mpext->reset_reason); 314 } 315 316 static struct dst_entry *subflow_v4_route_req(const struct sock *sk, 317 struct sk_buff *skb, 318 struct flowi *fl, 319 struct request_sock *req, 320 u32 tw_isn) 321 { 322 struct dst_entry *dst; 323 int err; 324 325 tcp_rsk(req)->is_mptcp = 1; 326 subflow_init_req(req, sk); 327 328 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req, tw_isn); 329 if (!dst) 330 return NULL; 331 332 err = subflow_check_req(req, sk, skb); 333 if (err == 0) 334 return dst; 335 336 dst_release(dst); 337 if (!req->syncookie) 338 tcp_request_sock_ops.send_reset(sk, skb, 339 mptcp_get_rst_reason(skb)); 340 return NULL; 341 } 342 343 static void subflow_prep_synack(const struct sock *sk, struct request_sock *req, 344 struct tcp_fastopen_cookie *foc, 345 enum tcp_synack_type synack_type) 346 { 347 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 348 struct inet_request_sock *ireq = inet_rsk(req); 349 350 /* clear tstamp_ok, as needed depending on cookie */ 351 if (foc && foc->len > -1) 352 ireq->tstamp_ok = 0; 353 354 if (synack_type == TCP_SYNACK_FASTOPEN) 355 mptcp_fastopen_subflow_synack_set_params(subflow, req); 356 } 357 358 static int subflow_v4_send_synack(const struct sock *sk, struct dst_entry *dst, 359 struct flowi *fl, 360 struct request_sock *req, 361 struct tcp_fastopen_cookie *foc, 362 enum tcp_synack_type synack_type, 363 struct sk_buff *syn_skb) 364 { 365 subflow_prep_synack(sk, req, foc, synack_type); 366 367 return tcp_request_sock_ipv4_ops.send_synack(sk, dst, fl, req, foc, 368 synack_type, syn_skb); 369 } 370 371 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 372 static int subflow_v6_send_synack(const struct sock *sk, struct dst_entry *dst, 373 struct flowi *fl, 374 struct request_sock *req, 375 struct tcp_fastopen_cookie *foc, 376 enum tcp_synack_type synack_type, 377 struct sk_buff *syn_skb) 378 { 379 subflow_prep_synack(sk, req, foc, synack_type); 380 381 return tcp_request_sock_ipv6_ops.send_synack(sk, dst, fl, req, foc, 382 synack_type, syn_skb); 383 } 384 385 static struct dst_entry *subflow_v6_route_req(const struct sock *sk, 386 struct sk_buff *skb, 387 struct flowi *fl, 388 struct request_sock *req, 389 u32 tw_isn) 390 { 391 struct dst_entry *dst; 392 int err; 393 394 tcp_rsk(req)->is_mptcp = 1; 395 subflow_init_req(req, sk); 396 397 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req, tw_isn); 398 if (!dst) 399 return NULL; 400 401 err = subflow_check_req(req, sk, skb); 402 if (err == 0) 403 return dst; 404 405 dst_release(dst); 406 if (!req->syncookie) 407 tcp6_request_sock_ops.send_reset(sk, skb, 408 mptcp_get_rst_reason(skb)); 409 return NULL; 410 } 411 #endif 412 413 /* validate received truncated hmac and create hmac for third ACK */ 414 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow) 415 { 416 u8 hmac[SHA256_DIGEST_SIZE]; 417 u64 thmac; 418 419 subflow_generate_hmac(subflow->remote_key, subflow->local_key, 420 subflow->remote_nonce, subflow->local_nonce, 421 hmac); 422 423 thmac = get_unaligned_be64(hmac); 424 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n", 425 subflow, subflow->token, thmac, subflow->thmac); 426 427 return thmac == subflow->thmac; 428 } 429 430 void mptcp_subflow_reset(struct sock *ssk) 431 { 432 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 433 struct sock *sk = subflow->conn; 434 435 /* mptcp_mp_fail_no_response() can reach here on an already closed 436 * socket 437 */ 438 if (ssk->sk_state == TCP_CLOSE) 439 return; 440 441 /* must hold: tcp_done() could drop last reference on parent */ 442 sock_hold(sk); 443 444 mptcp_send_active_reset_reason(ssk); 445 tcp_done(ssk); 446 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags)) 447 mptcp_schedule_work(sk); 448 449 sock_put(sk); 450 } 451 452 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk) 453 { 454 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport; 455 } 456 457 void __mptcp_sync_state(struct sock *sk, int state) 458 { 459 struct mptcp_subflow_context *subflow; 460 struct mptcp_sock *msk = mptcp_sk(sk); 461 struct sock *ssk = msk->first; 462 463 subflow = mptcp_subflow_ctx(ssk); 464 __mptcp_propagate_sndbuf(sk, ssk); 465 if (!msk->rcvspace_init) 466 mptcp_rcv_space_init(msk, ssk); 467 468 if (sk->sk_state == TCP_SYN_SENT) { 469 /* subflow->idsn is always available is TCP_SYN_SENT state, 470 * even for the FASTOPEN scenarios 471 */ 472 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 473 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 474 mptcp_set_state(sk, state); 475 sk->sk_state_change(sk); 476 } 477 } 478 479 static void subflow_set_remote_key(struct mptcp_sock *msk, 480 struct mptcp_subflow_context *subflow, 481 const struct mptcp_options_received *mp_opt) 482 { 483 /* active MPC subflow will reach here multiple times: 484 * at subflow_finish_connect() time and at 4th ack time 485 */ 486 if (subflow->remote_key_valid) 487 return; 488 489 subflow->remote_key_valid = 1; 490 subflow->remote_key = mp_opt->sndr_key; 491 mptcp_crypto_key_sha(subflow->remote_key, NULL, &subflow->iasn); 492 subflow->iasn++; 493 494 WRITE_ONCE(msk->remote_key, subflow->remote_key); 495 WRITE_ONCE(msk->ack_seq, subflow->iasn); 496 WRITE_ONCE(msk->can_ack, true); 497 atomic64_set(&msk->rcv_wnd_sent, subflow->iasn); 498 } 499 500 static void mptcp_propagate_state(struct sock *sk, struct sock *ssk, 501 struct mptcp_subflow_context *subflow, 502 const struct mptcp_options_received *mp_opt) 503 { 504 struct mptcp_sock *msk = mptcp_sk(sk); 505 506 mptcp_data_lock(sk); 507 if (mp_opt) { 508 /* Options are available only in the non fallback cases 509 * avoid updating rx path fields otherwise 510 */ 511 WRITE_ONCE(msk->snd_una, subflow->idsn + 1); 512 WRITE_ONCE(msk->wnd_end, subflow->idsn + 1 + tcp_sk(ssk)->snd_wnd); 513 subflow_set_remote_key(msk, subflow, mp_opt); 514 } 515 516 if (!sock_owned_by_user(sk)) { 517 __mptcp_sync_state(sk, ssk->sk_state); 518 } else { 519 msk->pending_state = ssk->sk_state; 520 __set_bit(MPTCP_SYNC_STATE, &msk->cb_flags); 521 } 522 mptcp_data_unlock(sk); 523 } 524 525 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) 526 { 527 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 528 struct mptcp_options_received mp_opt; 529 struct sock *parent = subflow->conn; 530 struct mptcp_sock *msk; 531 532 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb); 533 534 /* be sure no special action on any packet other than syn-ack */ 535 if (subflow->conn_finished) 536 return; 537 538 msk = mptcp_sk(parent); 539 subflow->rel_write_seq = 1; 540 subflow->conn_finished = 1; 541 subflow->ssn_offset = TCP_SKB_CB(skb)->seq; 542 pr_debug("subflow=%p synack seq=%x\n", subflow, subflow->ssn_offset); 543 544 mptcp_get_options(skb, &mp_opt); 545 if (subflow->request_mptcp) { 546 if (!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYNACK)) { 547 if (!mptcp_try_fallback(sk, 548 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK)) { 549 MPTCP_INC_STATS(sock_net(sk), 550 MPTCP_MIB_FALLBACKFAILED); 551 goto do_reset; 552 } 553 554 goto fallback; 555 } 556 557 if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD) 558 WRITE_ONCE(msk->csum_enabled, true); 559 if (mp_opt.deny_join_id0) 560 WRITE_ONCE(msk->pm.remote_deny_join_id0, true); 561 subflow->mp_capable = 1; 562 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK); 563 mptcp_finish_connect(sk); 564 mptcp_active_enable(parent); 565 mptcp_propagate_state(parent, sk, subflow, &mp_opt); 566 } else if (subflow->request_join) { 567 u8 hmac[SHA256_DIGEST_SIZE]; 568 569 if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYNACK)) { 570 subflow->reset_reason = MPTCP_RST_EMPTCP; 571 goto do_reset; 572 } 573 574 subflow->backup = mp_opt.backup; 575 subflow->thmac = mp_opt.thmac; 576 subflow->remote_nonce = mp_opt.nonce; 577 WRITE_ONCE(subflow->remote_id, mp_opt.join_id); 578 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d\n", 579 subflow, subflow->thmac, subflow->remote_nonce, 580 subflow->backup); 581 582 if (!subflow_thmac_valid(subflow)) { 583 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC); 584 subflow->reset_reason = MPTCP_RST_EMPTCP; 585 goto do_reset; 586 } 587 588 if (!mptcp_finish_join(sk)) 589 goto do_reset; 590 591 subflow_generate_hmac(subflow->local_key, subflow->remote_key, 592 subflow->local_nonce, 593 subflow->remote_nonce, 594 hmac); 595 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN); 596 597 subflow->mp_join = 1; 598 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX); 599 600 if (subflow->backup) 601 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKBACKUPRX); 602 603 if (subflow_use_different_dport(msk, sk)) { 604 pr_debug("synack inet_dport=%d %d\n", 605 ntohs(inet_sk(sk)->inet_dport), 606 ntohs(inet_sk(parent)->inet_dport)); 607 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX); 608 } 609 } else if (mptcp_check_fallback(sk)) { 610 /* It looks like MPTCP is blocked, while TCP is not */ 611 if (subflow->mpc_drop) 612 mptcp_active_disable(parent); 613 fallback: 614 mptcp_propagate_state(parent, sk, subflow, NULL); 615 } 616 return; 617 618 do_reset: 619 subflow->reset_transient = 0; 620 mptcp_subflow_reset(sk); 621 } 622 623 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id) 624 { 625 WARN_ON_ONCE(local_id < 0 || local_id > 255); 626 WRITE_ONCE(subflow->local_id, local_id); 627 } 628 629 static int subflow_chk_local_id(struct sock *sk) 630 { 631 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 632 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 633 int err; 634 635 if (likely(subflow->local_id >= 0)) 636 return 0; 637 638 err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk); 639 if (err < 0) 640 return err; 641 642 subflow_set_local_id(subflow, err); 643 subflow->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)sk); 644 645 return 0; 646 } 647 648 static int subflow_rebuild_header(struct sock *sk) 649 { 650 int err = subflow_chk_local_id(sk); 651 652 if (unlikely(err < 0)) 653 return err; 654 655 return inet_sk_rebuild_header(sk); 656 } 657 658 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 659 static int subflow_v6_rebuild_header(struct sock *sk) 660 { 661 int err = subflow_chk_local_id(sk); 662 663 if (unlikely(err < 0)) 664 return err; 665 666 return inet6_sk_rebuild_header(sk); 667 } 668 #endif 669 670 static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init; 671 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init; 672 673 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb) 674 { 675 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 676 677 pr_debug("subflow=%p\n", subflow); 678 679 /* Never answer to SYNs sent to broadcast or multicast */ 680 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 681 goto drop; 682 683 return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops, 684 &subflow_request_sock_ipv4_ops, 685 sk, skb); 686 drop: 687 tcp_listendrop(sk); 688 return 0; 689 } 690 691 static void subflow_v4_req_destructor(struct request_sock *req) 692 { 693 subflow_req_destructor(req); 694 tcp_request_sock_ops.destructor(req); 695 } 696 697 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 698 static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init; 699 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init; 700 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init; 701 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init; 702 static struct proto tcpv6_prot_override __ro_after_init; 703 704 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb) 705 { 706 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 707 708 pr_debug("subflow=%p\n", subflow); 709 710 if (skb->protocol == htons(ETH_P_IP)) 711 return subflow_v4_conn_request(sk, skb); 712 713 if (!ipv6_unicast_destination(skb)) 714 goto drop; 715 716 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) { 717 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS); 718 return 0; 719 } 720 721 return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops, 722 &subflow_request_sock_ipv6_ops, sk, skb); 723 724 drop: 725 tcp_listendrop(sk); 726 return 0; /* don't send reset */ 727 } 728 729 static void subflow_v6_req_destructor(struct request_sock *req) 730 { 731 subflow_req_destructor(req); 732 tcp6_request_sock_ops.destructor(req); 733 } 734 #endif 735 736 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops, 737 struct sock *sk_listener, 738 bool attach_listener) 739 { 740 if (ops->family == AF_INET) 741 ops = &mptcp_subflow_v4_request_sock_ops; 742 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 743 else if (ops->family == AF_INET6) 744 ops = &mptcp_subflow_v6_request_sock_ops; 745 #endif 746 747 return inet_reqsk_alloc(ops, sk_listener, attach_listener); 748 } 749 EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc); 750 751 /* validate hmac received in third ACK */ 752 static bool subflow_hmac_valid(const struct mptcp_subflow_request_sock *subflow_req, 753 const struct mptcp_options_received *mp_opt) 754 { 755 struct mptcp_sock *msk = subflow_req->msk; 756 u8 hmac[SHA256_DIGEST_SIZE]; 757 758 subflow_generate_hmac(READ_ONCE(msk->remote_key), 759 READ_ONCE(msk->local_key), 760 subflow_req->remote_nonce, 761 subflow_req->local_nonce, hmac); 762 763 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN); 764 } 765 766 static void subflow_ulp_fallback(struct sock *sk, 767 struct mptcp_subflow_context *old_ctx) 768 { 769 struct inet_connection_sock *icsk = inet_csk(sk); 770 771 mptcp_subflow_tcp_fallback(sk, old_ctx); 772 icsk->icsk_ulp_ops = NULL; 773 rcu_assign_pointer(icsk->icsk_ulp_data, NULL); 774 tcp_sk(sk)->is_mptcp = 0; 775 776 mptcp_subflow_ops_undo_override(sk); 777 } 778 779 void mptcp_subflow_drop_ctx(struct sock *ssk) 780 { 781 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 782 783 if (!ctx) 784 return; 785 786 list_del(&mptcp_subflow_ctx(ssk)->node); 787 if (inet_csk(ssk)->icsk_ulp_ops) { 788 subflow_ulp_fallback(ssk, ctx); 789 if (ctx->conn) 790 sock_put(ctx->conn); 791 } 792 793 kfree_rcu(ctx, rcu); 794 } 795 796 void __mptcp_subflow_fully_established(struct mptcp_sock *msk, 797 struct mptcp_subflow_context *subflow, 798 const struct mptcp_options_received *mp_opt) 799 { 800 subflow_set_remote_key(msk, subflow, mp_opt); 801 WRITE_ONCE(subflow->fully_established, true); 802 WRITE_ONCE(msk->fully_established, true); 803 } 804 805 static struct sock *subflow_syn_recv_sock(const struct sock *sk, 806 struct sk_buff *skb, 807 struct request_sock *req, 808 struct dst_entry *dst, 809 struct request_sock *req_unhash, 810 bool *own_req) 811 { 812 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk); 813 struct mptcp_subflow_request_sock *subflow_req; 814 struct mptcp_options_received mp_opt; 815 bool fallback, fallback_is_fatal; 816 enum sk_rst_reason reason; 817 struct mptcp_sock *owner; 818 struct sock *child; 819 820 pr_debug("listener=%p, req=%p, conn=%p\n", listener, req, listener->conn); 821 822 /* After child creation we must look for MPC even when options 823 * are not parsed 824 */ 825 mp_opt.suboptions = 0; 826 827 /* hopefully temporary handling for MP_JOIN+syncookie */ 828 subflow_req = mptcp_subflow_rsk(req); 829 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join; 830 fallback = !tcp_rsk(req)->is_mptcp; 831 if (fallback) 832 goto create_child; 833 834 /* if the sk is MP_CAPABLE, we try to fetch the client key */ 835 if (subflow_req->mp_capable) { 836 /* we can receive and accept an in-window, out-of-order pkt, 837 * which may not carry the MP_CAPABLE opt even on mptcp enabled 838 * paths: always try to extract the peer key, and fallback 839 * for packets missing it. 840 * Even OoO DSS packets coming legitly after dropped or 841 * reordered MPC will cause fallback, but we don't have other 842 * options. 843 */ 844 mptcp_get_options(skb, &mp_opt); 845 if (!(mp_opt.suboptions & 846 (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_ACK))) 847 fallback = true; 848 849 } else if (subflow_req->mp_join) { 850 mptcp_get_options(skb, &mp_opt); 851 if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK)) 852 fallback = true; 853 } 854 855 create_child: 856 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst, 857 req_unhash, own_req); 858 859 if (child && *own_req) { 860 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child); 861 862 tcp_rsk(req)->drop_req = false; 863 864 /* we need to fallback on ctx allocation failure and on pre-reqs 865 * checking above. In the latter scenario we additionally need 866 * to reset the context to non MPTCP status. 867 */ 868 if (!ctx || fallback) { 869 if (fallback_is_fatal) { 870 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 871 goto dispose_child; 872 } 873 goto fallback; 874 } 875 876 /* ssk inherits options of listener sk */ 877 ctx->setsockopt_seq = listener->setsockopt_seq; 878 879 if (ctx->mp_capable) { 880 ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req); 881 if (!ctx->conn) 882 goto fallback; 883 884 ctx->subflow_id = 1; 885 owner = mptcp_sk(ctx->conn); 886 mptcp_pm_new_connection(owner, child, 1); 887 888 /* with OoO packets we can reach here without ingress 889 * mpc option 890 */ 891 if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK) { 892 mptcp_pm_fully_established(owner, child); 893 ctx->pm_notified = 1; 894 } 895 } else if (ctx->mp_join) { 896 owner = subflow_req->msk; 897 if (!owner) { 898 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 899 goto dispose_child; 900 } 901 902 if (!subflow_hmac_valid(subflow_req, &mp_opt)) { 903 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC); 904 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 905 goto dispose_child; 906 } 907 908 if (!mptcp_can_accept_new_subflow(owner)) { 909 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED); 910 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 911 goto dispose_child; 912 } 913 914 /* move the msk reference ownership to the subflow */ 915 subflow_req->msk = NULL; 916 ctx->conn = (struct sock *)owner; 917 918 if (subflow_use_different_sport(owner, sk)) { 919 pr_debug("ack inet_sport=%d %d\n", 920 ntohs(inet_sk(sk)->inet_sport), 921 ntohs(inet_sk((struct sock *)owner)->inet_sport)); 922 if (!mptcp_pm_sport_in_anno_list(owner, sk)) { 923 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX); 924 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 925 goto dispose_child; 926 } 927 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX); 928 } 929 930 if (!mptcp_finish_join(child)) { 931 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(child); 932 933 subflow_add_reset_reason(skb, subflow->reset_reason); 934 goto dispose_child; 935 } 936 937 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX); 938 tcp_rsk(req)->drop_req = true; 939 } 940 } 941 942 /* check for expected invariant - should never trigger, just help 943 * catching earlier subtle bugs 944 */ 945 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp && 946 (!mptcp_subflow_ctx(child) || 947 !mptcp_subflow_ctx(child)->conn)); 948 return child; 949 950 dispose_child: 951 mptcp_subflow_drop_ctx(child); 952 tcp_rsk(req)->drop_req = true; 953 inet_csk_prepare_for_destroy_sock(child); 954 tcp_done(child); 955 reason = mptcp_get_rst_reason(skb); 956 req->rsk_ops->send_reset(sk, skb, reason); 957 958 /* The last child reference will be released by the caller */ 959 return child; 960 961 fallback: 962 if (fallback) 963 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 964 mptcp_subflow_drop_ctx(child); 965 return child; 966 } 967 968 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init; 969 static struct proto tcp_prot_override __ro_after_init; 970 971 enum mapping_status { 972 MAPPING_OK, 973 MAPPING_INVALID, 974 MAPPING_EMPTY, 975 MAPPING_DATA_FIN, 976 MAPPING_DUMMY, 977 MAPPING_BAD_CSUM, 978 MAPPING_NODSS 979 }; 980 981 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn) 982 { 983 pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d\n", 984 ssn, subflow->map_subflow_seq, subflow->map_data_len); 985 } 986 987 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb) 988 { 989 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 990 unsigned int skb_consumed; 991 992 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq; 993 if (unlikely(skb_consumed >= skb->len)) { 994 DEBUG_NET_WARN_ON_ONCE(1); 995 return true; 996 } 997 998 return skb->len - skb_consumed <= subflow->map_data_len - 999 mptcp_subflow_get_map_offset(subflow); 1000 } 1001 1002 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb) 1003 { 1004 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1005 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1006 1007 if (unlikely(before(ssn, subflow->map_subflow_seq))) { 1008 /* Mapping covers data later in the subflow stream, 1009 * currently unsupported. 1010 */ 1011 dbg_bad_map(subflow, ssn); 1012 return false; 1013 } 1014 if (unlikely(!before(ssn, subflow->map_subflow_seq + 1015 subflow->map_data_len))) { 1016 /* Mapping does covers past subflow data, invalid */ 1017 dbg_bad_map(subflow, ssn); 1018 return false; 1019 } 1020 return true; 1021 } 1022 1023 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb, 1024 bool csum_reqd) 1025 { 1026 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1027 u32 offset, seq, delta; 1028 __sum16 csum; 1029 int len; 1030 1031 if (!csum_reqd) 1032 return MAPPING_OK; 1033 1034 /* mapping already validated on previous traversal */ 1035 if (subflow->map_csum_len == subflow->map_data_len) 1036 return MAPPING_OK; 1037 1038 /* traverse the receive queue, ensuring it contains a full 1039 * DSS mapping and accumulating the related csum. 1040 * Preserve the accoumlate csum across multiple calls, to compute 1041 * the csum only once 1042 */ 1043 delta = subflow->map_data_len - subflow->map_csum_len; 1044 for (;;) { 1045 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len; 1046 offset = seq - TCP_SKB_CB(skb)->seq; 1047 1048 /* if the current skb has not been accounted yet, csum its contents 1049 * up to the amount covered by the current DSS 1050 */ 1051 if (offset < skb->len) { 1052 __wsum csum; 1053 1054 len = min(skb->len - offset, delta); 1055 csum = skb_checksum(skb, offset, len, 0); 1056 subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum, 1057 subflow->map_csum_len); 1058 1059 delta -= len; 1060 subflow->map_csum_len += len; 1061 } 1062 if (delta == 0) 1063 break; 1064 1065 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) { 1066 /* if this subflow is closed, the partial mapping 1067 * will be never completed; flush the pending skbs, so 1068 * that subflow_sched_work_if_closed() can kick in 1069 */ 1070 if (unlikely(ssk->sk_state == TCP_CLOSE)) 1071 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1072 sk_eat_skb(ssk, skb); 1073 1074 /* not enough data to validate the csum */ 1075 return MAPPING_EMPTY; 1076 } 1077 1078 /* the DSS mapping for next skbs will be validated later, 1079 * when a get_mapping_status call will process such skb 1080 */ 1081 skb = skb->next; 1082 } 1083 1084 /* note that 'map_data_len' accounts only for the carried data, does 1085 * not include the eventual seq increment due to the data fin, 1086 * while the pseudo header requires the original DSS data len, 1087 * including that 1088 */ 1089 csum = __mptcp_make_csum(subflow->map_seq, 1090 subflow->map_subflow_seq, 1091 subflow->map_data_len + subflow->map_data_fin, 1092 subflow->map_data_csum); 1093 if (unlikely(csum)) { 1094 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR); 1095 return MAPPING_BAD_CSUM; 1096 } 1097 1098 subflow->valid_csum_seen = 1; 1099 return MAPPING_OK; 1100 } 1101 1102 static enum mapping_status get_mapping_status(struct sock *ssk, 1103 struct mptcp_sock *msk) 1104 { 1105 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1106 bool csum_reqd = READ_ONCE(msk->csum_enabled); 1107 struct mptcp_ext *mpext; 1108 struct sk_buff *skb; 1109 u16 data_len; 1110 u64 map_seq; 1111 1112 skb = skb_peek(&ssk->sk_receive_queue); 1113 if (!skb) 1114 return MAPPING_EMPTY; 1115 1116 if (mptcp_check_fallback(ssk)) 1117 return MAPPING_DUMMY; 1118 1119 mpext = mptcp_get_ext(skb); 1120 if (!mpext || !mpext->use_map) { 1121 if (!subflow->map_valid && !skb->len) { 1122 /* the TCP stack deliver 0 len FIN pkt to the receive 1123 * queue, that is the only 0len pkts ever expected here, 1124 * and we can admit no mapping only for 0 len pkts 1125 */ 1126 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 1127 WARN_ONCE(1, "0len seq %d:%d flags %x", 1128 TCP_SKB_CB(skb)->seq, 1129 TCP_SKB_CB(skb)->end_seq, 1130 TCP_SKB_CB(skb)->tcp_flags); 1131 sk_eat_skb(ssk, skb); 1132 return MAPPING_EMPTY; 1133 } 1134 1135 /* If the required DSS has likely been dropped by a middlebox */ 1136 if (!subflow->map_valid) 1137 return MAPPING_NODSS; 1138 1139 goto validate_seq; 1140 } 1141 1142 trace_get_mapping_status(mpext); 1143 1144 data_len = mpext->data_len; 1145 if (data_len == 0) { 1146 pr_debug("infinite mapping received\n"); 1147 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX); 1148 return MAPPING_INVALID; 1149 } 1150 1151 if (mpext->data_fin == 1) { 1152 u64 data_fin_seq; 1153 1154 if (data_len == 1) { 1155 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq, 1156 mpext->dsn64); 1157 pr_debug("DATA_FIN with no payload seq=%llu\n", mpext->data_seq); 1158 if (subflow->map_valid) { 1159 /* A DATA_FIN might arrive in a DSS 1160 * option before the previous mapping 1161 * has been fully consumed. Continue 1162 * handling the existing mapping. 1163 */ 1164 skb_ext_del(skb, SKB_EXT_MPTCP); 1165 return MAPPING_OK; 1166 } 1167 1168 if (updated) 1169 mptcp_schedule_work((struct sock *)msk); 1170 1171 return MAPPING_DATA_FIN; 1172 } 1173 1174 data_fin_seq = mpext->data_seq + data_len - 1; 1175 1176 /* If mpext->data_seq is a 32-bit value, data_fin_seq must also 1177 * be limited to 32 bits. 1178 */ 1179 if (!mpext->dsn64) 1180 data_fin_seq &= GENMASK_ULL(31, 0); 1181 1182 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64); 1183 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d\n", 1184 data_fin_seq, mpext->dsn64); 1185 1186 /* Adjust for DATA_FIN using 1 byte of sequence space */ 1187 data_len--; 1188 } 1189 1190 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64); 1191 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64); 1192 1193 if (subflow->map_valid) { 1194 /* Allow replacing only with an identical map */ 1195 if (subflow->map_seq == map_seq && 1196 subflow->map_subflow_seq == mpext->subflow_seq && 1197 subflow->map_data_len == data_len && 1198 subflow->map_csum_reqd == mpext->csum_reqd) { 1199 skb_ext_del(skb, SKB_EXT_MPTCP); 1200 goto validate_csum; 1201 } 1202 1203 /* If this skb data are fully covered by the current mapping, 1204 * the new map would need caching, which is not supported 1205 */ 1206 if (skb_is_fully_mapped(ssk, skb)) { 1207 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH); 1208 return MAPPING_INVALID; 1209 } 1210 1211 /* will validate the next map after consuming the current one */ 1212 goto validate_csum; 1213 } 1214 1215 subflow->map_seq = map_seq; 1216 subflow->map_subflow_seq = mpext->subflow_seq; 1217 subflow->map_data_len = data_len; 1218 subflow->map_valid = 1; 1219 subflow->map_data_fin = mpext->data_fin; 1220 subflow->mpc_map = mpext->mpc_map; 1221 subflow->map_csum_reqd = mpext->csum_reqd; 1222 subflow->map_csum_len = 0; 1223 subflow->map_data_csum = csum_unfold(mpext->csum); 1224 1225 /* Cfr RFC 8684 Section 3.3.0 */ 1226 if (unlikely(subflow->map_csum_reqd != csum_reqd)) 1227 return MAPPING_INVALID; 1228 1229 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n", 1230 subflow->map_seq, subflow->map_subflow_seq, 1231 subflow->map_data_len, subflow->map_csum_reqd, 1232 subflow->map_data_csum); 1233 1234 validate_seq: 1235 /* we revalidate valid mapping on new skb, because we must ensure 1236 * the current skb is completely covered by the available mapping 1237 */ 1238 if (!validate_mapping(ssk, skb)) { 1239 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH); 1240 return MAPPING_INVALID; 1241 } 1242 1243 skb_ext_del(skb, SKB_EXT_MPTCP); 1244 1245 validate_csum: 1246 return validate_data_csum(ssk, skb, csum_reqd); 1247 } 1248 1249 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb, 1250 u64 limit) 1251 { 1252 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1253 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 1254 struct tcp_sock *tp = tcp_sk(ssk); 1255 u32 offset, incr, avail_len; 1256 1257 offset = tp->copied_seq - TCP_SKB_CB(skb)->seq; 1258 if (WARN_ON_ONCE(offset > skb->len)) 1259 goto out; 1260 1261 avail_len = skb->len - offset; 1262 incr = limit >= avail_len ? avail_len + fin : limit; 1263 1264 pr_debug("discarding=%d len=%d offset=%d seq=%d\n", incr, skb->len, 1265 offset, subflow->map_subflow_seq); 1266 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA); 1267 tcp_sk(ssk)->copied_seq += incr; 1268 1269 out: 1270 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq)) 1271 sk_eat_skb(ssk, skb); 1272 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) 1273 subflow->map_valid = 0; 1274 } 1275 1276 static bool subflow_is_done(const struct sock *sk) 1277 { 1278 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE; 1279 } 1280 1281 /* sched mptcp worker for subflow cleanup if no more data is pending */ 1282 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk) 1283 { 1284 struct sock *sk = (struct sock *)msk; 1285 1286 if (likely(ssk->sk_state != TCP_CLOSE && 1287 (ssk->sk_state != TCP_CLOSE_WAIT || 1288 inet_sk_state_load(sk) != TCP_ESTABLISHED))) 1289 return; 1290 1291 if (!skb_queue_empty(&ssk->sk_receive_queue)) 1292 return; 1293 1294 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 1295 mptcp_schedule_work(sk); 1296 1297 /* when the fallback subflow closes the rx side, trigger a 'dummy' 1298 * ingress data fin, so that the msk state will follow along 1299 */ 1300 if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) && 1301 msk->first == ssk && 1302 mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true)) 1303 mptcp_schedule_work(sk); 1304 } 1305 1306 static bool mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk) 1307 { 1308 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1309 unsigned long fail_tout; 1310 1311 /* we are really failing, prevent any later subflow join */ 1312 spin_lock_bh(&msk->fallback_lock); 1313 if (!msk->allow_infinite_fallback) { 1314 spin_unlock_bh(&msk->fallback_lock); 1315 return false; 1316 } 1317 msk->allow_subflows = false; 1318 spin_unlock_bh(&msk->fallback_lock); 1319 1320 /* graceful failure can happen only on the MPC subflow */ 1321 if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first))) 1322 return false; 1323 1324 /* since the close timeout take precedence on the fail one, 1325 * no need to start the latter when the first is already set 1326 */ 1327 if (sock_flag((struct sock *)msk, SOCK_DEAD)) 1328 return true; 1329 1330 /* we don't need extreme accuracy here, use a zero fail_tout as special 1331 * value meaning no fail timeout at all; 1332 */ 1333 fail_tout = jiffies + TCP_RTO_MAX; 1334 if (!fail_tout) 1335 fail_tout = 1; 1336 WRITE_ONCE(subflow->fail_tout, fail_tout); 1337 tcp_send_ack(ssk); 1338 1339 mptcp_reset_tout_timer(msk, subflow->fail_tout); 1340 return true; 1341 } 1342 1343 static bool subflow_check_data_avail(struct sock *ssk) 1344 { 1345 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1346 enum mapping_status status; 1347 struct mptcp_sock *msk; 1348 struct sk_buff *skb; 1349 1350 if (!skb_peek(&ssk->sk_receive_queue)) 1351 WRITE_ONCE(subflow->data_avail, false); 1352 if (subflow->data_avail) 1353 return true; 1354 1355 msk = mptcp_sk(subflow->conn); 1356 for (;;) { 1357 u64 ack_seq; 1358 u64 old_ack; 1359 1360 status = get_mapping_status(ssk, msk); 1361 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue)); 1362 if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY || 1363 status == MAPPING_BAD_CSUM || status == MAPPING_NODSS)) 1364 goto fallback; 1365 1366 if (status != MAPPING_OK) 1367 goto no_data; 1368 1369 skb = skb_peek(&ssk->sk_receive_queue); 1370 if (WARN_ON_ONCE(!skb)) 1371 goto no_data; 1372 1373 if (unlikely(!READ_ONCE(msk->can_ack))) 1374 goto fallback; 1375 1376 old_ack = READ_ONCE(msk->ack_seq); 1377 ack_seq = mptcp_subflow_get_mapped_dsn(subflow); 1378 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx\n", old_ack, 1379 ack_seq); 1380 if (unlikely(before64(ack_seq, old_ack))) { 1381 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq); 1382 continue; 1383 } 1384 1385 WRITE_ONCE(subflow->data_avail, true); 1386 break; 1387 } 1388 return true; 1389 1390 no_data: 1391 subflow_sched_work_if_closed(msk, ssk); 1392 return false; 1393 1394 fallback: 1395 if (!__mptcp_check_fallback(msk)) { 1396 /* RFC 8684 section 3.7. */ 1397 if (status == MAPPING_BAD_CSUM && 1398 (subflow->mp_join || subflow->valid_csum_seen)) { 1399 subflow->send_mp_fail = 1; 1400 1401 if (!mptcp_subflow_fail(msk, ssk)) { 1402 subflow->reset_transient = 0; 1403 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX; 1404 goto reset; 1405 } 1406 WRITE_ONCE(subflow->data_avail, true); 1407 return true; 1408 } 1409 1410 if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSFALLBACK)) { 1411 /* fatal protocol error, close the socket. 1412 * subflow_error_report() will introduce the appropriate barriers 1413 */ 1414 subflow->reset_transient = 0; 1415 subflow->reset_reason = status == MAPPING_NODSS ? 1416 MPTCP_RST_EMIDDLEBOX : 1417 MPTCP_RST_EMPTCP; 1418 1419 reset: 1420 WRITE_ONCE(ssk->sk_err, EBADMSG); 1421 tcp_set_state(ssk, TCP_CLOSE); 1422 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1423 sk_eat_skb(ssk, skb); 1424 mptcp_send_active_reset_reason(ssk); 1425 WRITE_ONCE(subflow->data_avail, false); 1426 return false; 1427 } 1428 } 1429 1430 skb = skb_peek(&ssk->sk_receive_queue); 1431 subflow->map_valid = 1; 1432 subflow->map_seq = READ_ONCE(msk->ack_seq); 1433 subflow->map_data_len = skb->len; 1434 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1435 WRITE_ONCE(subflow->data_avail, true); 1436 return true; 1437 } 1438 1439 bool mptcp_subflow_data_available(struct sock *sk) 1440 { 1441 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1442 1443 /* check if current mapping is still valid */ 1444 if (subflow->map_valid && 1445 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) { 1446 subflow->map_valid = 0; 1447 WRITE_ONCE(subflow->data_avail, false); 1448 1449 pr_debug("Done with mapping: seq=%u data_len=%u\n", 1450 subflow->map_subflow_seq, 1451 subflow->map_data_len); 1452 } 1453 1454 return subflow_check_data_avail(sk); 1455 } 1456 1457 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy, 1458 * not the ssk one. 1459 * 1460 * In mptcp, rwin is about the mptcp-level connection data. 1461 * 1462 * Data that is still on the ssk rx queue can thus be ignored, 1463 * as far as mptcp peer is concerned that data is still inflight. 1464 * DSS ACK is updated when skb is moved to the mptcp rx queue. 1465 */ 1466 void mptcp_space(const struct sock *ssk, int *space, int *full_space) 1467 { 1468 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1469 const struct sock *sk = subflow->conn; 1470 1471 *space = __mptcp_space(sk); 1472 *full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf)); 1473 } 1474 1475 static void subflow_error_report(struct sock *ssk) 1476 { 1477 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1478 1479 /* bail early if this is a no-op, so that we avoid introducing a 1480 * problematic lockdep dependency between TCP accept queue lock 1481 * and msk socket spinlock 1482 */ 1483 if (!sk->sk_socket) 1484 return; 1485 1486 mptcp_data_lock(sk); 1487 if (!sock_owned_by_user(sk)) 1488 __mptcp_error_report(sk); 1489 else 1490 __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags); 1491 mptcp_data_unlock(sk); 1492 } 1493 1494 static void subflow_data_ready(struct sock *sk) 1495 { 1496 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1497 u16 state = 1 << inet_sk_state_load(sk); 1498 struct sock *parent = subflow->conn; 1499 struct mptcp_sock *msk; 1500 1501 trace_sk_data_ready(sk); 1502 1503 msk = mptcp_sk(parent); 1504 if (state & TCPF_LISTEN) { 1505 /* MPJ subflow are removed from accept queue before reaching here, 1506 * avoid stray wakeups 1507 */ 1508 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue)) 1509 return; 1510 1511 parent->sk_data_ready(parent); 1512 return; 1513 } 1514 1515 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable && 1516 !subflow->mp_join && !(state & TCPF_CLOSE)); 1517 1518 if (mptcp_subflow_data_available(sk)) { 1519 mptcp_data_ready(parent, sk); 1520 1521 /* subflow-level lowat test are not relevant. 1522 * respect the msk-level threshold eventually mandating an immediate ack 1523 */ 1524 if (mptcp_data_avail(msk) < parent->sk_rcvlowat && 1525 (tcp_sk(sk)->rcv_nxt - tcp_sk(sk)->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss) 1526 inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW; 1527 } else if (unlikely(sk->sk_err)) { 1528 subflow_error_report(sk); 1529 } 1530 } 1531 1532 static void subflow_write_space(struct sock *ssk) 1533 { 1534 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1535 1536 mptcp_propagate_sndbuf(sk, ssk); 1537 mptcp_write_space(sk); 1538 } 1539 1540 static const struct inet_connection_sock_af_ops * 1541 subflow_default_af_ops(struct sock *sk) 1542 { 1543 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1544 if (sk->sk_family == AF_INET6) 1545 return &subflow_v6_specific; 1546 #endif 1547 return &subflow_specific; 1548 } 1549 1550 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1551 void mptcpv6_handle_mapped(struct sock *sk, bool mapped) 1552 { 1553 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1554 struct inet_connection_sock *icsk = inet_csk(sk); 1555 const struct inet_connection_sock_af_ops *target; 1556 1557 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk); 1558 1559 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d\n", 1560 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped); 1561 1562 if (likely(icsk->icsk_af_ops == target)) 1563 return; 1564 1565 subflow->icsk_af_ops = icsk->icsk_af_ops; 1566 icsk->icsk_af_ops = target; 1567 } 1568 #endif 1569 1570 void mptcp_info2sockaddr(const struct mptcp_addr_info *info, 1571 struct sockaddr_storage *addr, 1572 unsigned short family) 1573 { 1574 memset(addr, 0, sizeof(*addr)); 1575 addr->ss_family = family; 1576 if (addr->ss_family == AF_INET) { 1577 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr; 1578 1579 if (info->family == AF_INET) 1580 in_addr->sin_addr = info->addr; 1581 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1582 else if (ipv6_addr_v4mapped(&info->addr6)) 1583 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3]; 1584 #endif 1585 in_addr->sin_port = info->port; 1586 } 1587 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1588 else if (addr->ss_family == AF_INET6) { 1589 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr; 1590 1591 if (info->family == AF_INET) 1592 ipv6_addr_set_v4mapped(info->addr.s_addr, 1593 &in6_addr->sin6_addr); 1594 else 1595 in6_addr->sin6_addr = info->addr6; 1596 in6_addr->sin6_port = info->port; 1597 } 1598 #endif 1599 } 1600 1601 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local, 1602 const struct mptcp_addr_info *remote) 1603 { 1604 struct mptcp_sock *msk = mptcp_sk(sk); 1605 struct mptcp_subflow_context *subflow; 1606 int local_id = local->addr.id; 1607 struct sockaddr_storage addr; 1608 int remote_id = remote->id; 1609 int err = -ENOTCONN; 1610 struct socket *sf; 1611 struct sock *ssk; 1612 u32 remote_token; 1613 int addrlen; 1614 1615 /* The userspace PM sent the request too early? */ 1616 if (!mptcp_is_fully_established(sk)) 1617 goto err_out; 1618 1619 err = mptcp_subflow_create_socket(sk, local->addr.family, &sf); 1620 if (err) { 1621 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCREATSKERR); 1622 pr_debug("msk=%p local=%d remote=%d create sock error: %d\n", 1623 msk, local_id, remote_id, err); 1624 goto err_out; 1625 } 1626 1627 ssk = sf->sk; 1628 subflow = mptcp_subflow_ctx(ssk); 1629 do { 1630 get_random_bytes(&subflow->local_nonce, sizeof(u32)); 1631 } while (!subflow->local_nonce); 1632 1633 /* if 'IPADDRANY', the ID will be set later, after the routing */ 1634 if (local->addr.family == AF_INET) { 1635 if (!local->addr.addr.s_addr) 1636 local_id = -1; 1637 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1638 } else if (sk->sk_family == AF_INET6) { 1639 if (ipv6_addr_any(&local->addr.addr6)) 1640 local_id = -1; 1641 #endif 1642 } 1643 1644 if (local_id >= 0) 1645 subflow_set_local_id(subflow, local_id); 1646 1647 subflow->remote_key_valid = 1; 1648 subflow->remote_key = READ_ONCE(msk->remote_key); 1649 subflow->local_key = READ_ONCE(msk->local_key); 1650 subflow->token = msk->token; 1651 mptcp_info2sockaddr(&local->addr, &addr, ssk->sk_family); 1652 1653 addrlen = sizeof(struct sockaddr_in); 1654 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1655 if (addr.ss_family == AF_INET6) 1656 addrlen = sizeof(struct sockaddr_in6); 1657 #endif 1658 ssk->sk_bound_dev_if = local->ifindex; 1659 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen); 1660 if (err) { 1661 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXBINDERR); 1662 pr_debug("msk=%p local=%d remote=%d bind error: %d\n", 1663 msk, local_id, remote_id, err); 1664 goto failed; 1665 } 1666 1667 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL); 1668 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d\n", msk, 1669 remote_token, local_id, remote_id); 1670 subflow->remote_token = remote_token; 1671 WRITE_ONCE(subflow->remote_id, remote_id); 1672 subflow->request_join = 1; 1673 subflow->request_bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP); 1674 subflow->subflow_id = msk->subflow_id++; 1675 mptcp_info2sockaddr(remote, &addr, ssk->sk_family); 1676 1677 sock_hold(ssk); 1678 list_add_tail(&subflow->node, &msk->conn_list); 1679 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK); 1680 if (err && err != -EINPROGRESS) { 1681 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCONNECTERR); 1682 pr_debug("msk=%p local=%d remote=%d connect error: %d\n", 1683 msk, local_id, remote_id, err); 1684 goto failed_unlink; 1685 } 1686 1687 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTX); 1688 1689 /* discard the subflow socket */ 1690 mptcp_sock_graft(ssk, sk->sk_socket); 1691 iput(SOCK_INODE(sf)); 1692 mptcp_stop_tout_timer(sk); 1693 return 0; 1694 1695 failed_unlink: 1696 list_del(&subflow->node); 1697 sock_put(mptcp_subflow_tcp_sock(subflow)); 1698 1699 failed: 1700 subflow->disposable = 1; 1701 sock_release(sf); 1702 1703 err_out: 1704 /* we account subflows before the creation, and this failures will not 1705 * be caught by sk_state_change() 1706 */ 1707 mptcp_pm_close_subflow(msk); 1708 return err; 1709 } 1710 1711 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child) 1712 { 1713 #ifdef CONFIG_SOCK_CGROUP_DATA 1714 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, 1715 *child_skcd = &child->sk_cgrp_data; 1716 1717 /* only the additional subflows created by kworkers have to be modified */ 1718 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) != 1719 cgroup_id(sock_cgroup_ptr(child_skcd))) { 1720 #ifdef CONFIG_MEMCG 1721 struct mem_cgroup *memcg = parent->sk_memcg; 1722 1723 mem_cgroup_sk_free(child); 1724 if (memcg && css_tryget(&memcg->css)) 1725 child->sk_memcg = memcg; 1726 #endif /* CONFIG_MEMCG */ 1727 1728 cgroup_sk_free(child_skcd); 1729 *child_skcd = *parent_skcd; 1730 cgroup_sk_clone(child_skcd); 1731 } 1732 #endif /* CONFIG_SOCK_CGROUP_DATA */ 1733 } 1734 1735 static void mptcp_subflow_ops_override(struct sock *ssk) 1736 { 1737 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1738 if (ssk->sk_prot == &tcpv6_prot) 1739 ssk->sk_prot = &tcpv6_prot_override; 1740 else 1741 #endif 1742 ssk->sk_prot = &tcp_prot_override; 1743 } 1744 1745 static void mptcp_subflow_ops_undo_override(struct sock *ssk) 1746 { 1747 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1748 if (ssk->sk_prot == &tcpv6_prot_override) 1749 ssk->sk_prot = &tcpv6_prot; 1750 else 1751 #endif 1752 ssk->sk_prot = &tcp_prot; 1753 } 1754 1755 int mptcp_subflow_create_socket(struct sock *sk, unsigned short family, 1756 struct socket **new_sock) 1757 { 1758 struct mptcp_subflow_context *subflow; 1759 struct net *net = sock_net(sk); 1760 struct socket *sf; 1761 int err; 1762 1763 /* un-accepted server sockets can reach here - on bad configuration 1764 * bail early to avoid greater trouble later 1765 */ 1766 if (unlikely(!sk->sk_socket)) 1767 return -EINVAL; 1768 1769 err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf); 1770 if (err) 1771 return err; 1772 1773 lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING); 1774 1775 err = security_mptcp_add_subflow(sk, sf->sk); 1776 if (err) 1777 goto err_free; 1778 1779 /* the newly created socket has to be in the same cgroup as its parent */ 1780 mptcp_attach_cgroup(sk, sf->sk); 1781 1782 /* kernel sockets do not by default acquire net ref, but TCP timer 1783 * needs it. 1784 * Update ns_tracker to current stack trace and refcounted tracker. 1785 */ 1786 sk_net_refcnt_upgrade(sf->sk); 1787 err = tcp_set_ulp(sf->sk, "mptcp"); 1788 if (err) 1789 goto err_free; 1790 1791 mptcp_sockopt_sync_locked(mptcp_sk(sk), sf->sk); 1792 release_sock(sf->sk); 1793 1794 /* the newly created socket really belongs to the owning MPTCP 1795 * socket, even if for additional subflows the allocation is performed 1796 * by a kernel workqueue. Adjust inode references, so that the 1797 * procfs/diag interfaces really show this one belonging to the correct 1798 * user. 1799 */ 1800 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino; 1801 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid; 1802 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid; 1803 1804 subflow = mptcp_subflow_ctx(sf->sk); 1805 pr_debug("subflow=%p\n", subflow); 1806 1807 *new_sock = sf; 1808 sock_hold(sk); 1809 subflow->conn = sk; 1810 mptcp_subflow_ops_override(sf->sk); 1811 1812 return 0; 1813 1814 err_free: 1815 release_sock(sf->sk); 1816 sock_release(sf); 1817 return err; 1818 } 1819 1820 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, 1821 gfp_t priority) 1822 { 1823 struct inet_connection_sock *icsk = inet_csk(sk); 1824 struct mptcp_subflow_context *ctx; 1825 1826 ctx = kzalloc(sizeof(*ctx), priority); 1827 if (!ctx) 1828 return NULL; 1829 1830 rcu_assign_pointer(icsk->icsk_ulp_data, ctx); 1831 INIT_LIST_HEAD(&ctx->node); 1832 INIT_LIST_HEAD(&ctx->delegated_node); 1833 1834 pr_debug("subflow=%p\n", ctx); 1835 1836 ctx->tcp_sock = sk; 1837 WRITE_ONCE(ctx->local_id, -1); 1838 1839 return ctx; 1840 } 1841 1842 static void __subflow_state_change(struct sock *sk) 1843 { 1844 struct socket_wq *wq; 1845 1846 rcu_read_lock(); 1847 wq = rcu_dereference(sk->sk_wq); 1848 if (skwq_has_sleeper(wq)) 1849 wake_up_interruptible_all(&wq->wait); 1850 rcu_read_unlock(); 1851 } 1852 1853 static void subflow_state_change(struct sock *sk) 1854 { 1855 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1856 struct sock *parent = subflow->conn; 1857 1858 __subflow_state_change(sk); 1859 1860 if (subflow_simultaneous_connect(sk)) { 1861 WARN_ON_ONCE(!mptcp_try_fallback(sk, MPTCP_MIB_SIMULTCONNFALLBACK)); 1862 subflow->conn_finished = 1; 1863 mptcp_propagate_state(parent, sk, subflow, NULL); 1864 } 1865 1866 /* as recvmsg() does not acquire the subflow socket for ssk selection 1867 * a fin packet carrying a DSS can be unnoticed if we don't trigger 1868 * the data available machinery here. 1869 */ 1870 if (mptcp_subflow_data_available(sk)) 1871 mptcp_data_ready(parent, sk); 1872 else if (unlikely(sk->sk_err)) 1873 subflow_error_report(sk); 1874 1875 subflow_sched_work_if_closed(mptcp_sk(parent), sk); 1876 } 1877 1878 void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk) 1879 { 1880 struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue; 1881 struct request_sock *req, *head, *tail; 1882 struct mptcp_subflow_context *subflow; 1883 struct sock *sk, *ssk; 1884 1885 /* Due to lock dependencies no relevant lock can be acquired under rskq_lock. 1886 * Splice the req list, so that accept() can not reach the pending ssk after 1887 * the listener socket is released below. 1888 */ 1889 spin_lock_bh(&queue->rskq_lock); 1890 head = queue->rskq_accept_head; 1891 tail = queue->rskq_accept_tail; 1892 queue->rskq_accept_head = NULL; 1893 queue->rskq_accept_tail = NULL; 1894 spin_unlock_bh(&queue->rskq_lock); 1895 1896 if (!head) 1897 return; 1898 1899 /* can't acquire the msk socket lock under the subflow one, 1900 * or will cause ABBA deadlock 1901 */ 1902 release_sock(listener_ssk); 1903 1904 for (req = head; req; req = req->dl_next) { 1905 ssk = req->sk; 1906 if (!sk_is_mptcp(ssk)) 1907 continue; 1908 1909 subflow = mptcp_subflow_ctx(ssk); 1910 if (!subflow || !subflow->conn) 1911 continue; 1912 1913 sk = subflow->conn; 1914 sock_hold(sk); 1915 1916 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 1917 __mptcp_unaccepted_force_close(sk); 1918 release_sock(sk); 1919 1920 /* lockdep will report a false positive ABBA deadlock 1921 * between cancel_work_sync and the listener socket. 1922 * The involved locks belong to different sockets WRT 1923 * the existing AB chain. 1924 * Using a per socket key is problematic as key 1925 * deregistration requires process context and must be 1926 * performed at socket disposal time, in atomic 1927 * context. 1928 * Just tell lockdep to consider the listener socket 1929 * released here. 1930 */ 1931 mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_); 1932 mptcp_cancel_work(sk); 1933 mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_); 1934 1935 sock_put(sk); 1936 } 1937 1938 /* we are still under the listener msk socket lock */ 1939 lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING); 1940 1941 /* restore the listener queue, to let the TCP code clean it up */ 1942 spin_lock_bh(&queue->rskq_lock); 1943 WARN_ON_ONCE(queue->rskq_accept_head); 1944 queue->rskq_accept_head = head; 1945 queue->rskq_accept_tail = tail; 1946 spin_unlock_bh(&queue->rskq_lock); 1947 } 1948 1949 static int subflow_ulp_init(struct sock *sk) 1950 { 1951 struct inet_connection_sock *icsk = inet_csk(sk); 1952 struct mptcp_subflow_context *ctx; 1953 struct tcp_sock *tp = tcp_sk(sk); 1954 int err = 0; 1955 1956 /* disallow attaching ULP to a socket unless it has been 1957 * created with sock_create_kern() 1958 */ 1959 if (!sk->sk_kern_sock) { 1960 err = -EOPNOTSUPP; 1961 goto out; 1962 } 1963 1964 ctx = subflow_create_ctx(sk, GFP_KERNEL); 1965 if (!ctx) { 1966 err = -ENOMEM; 1967 goto out; 1968 } 1969 1970 pr_debug("subflow=%p, family=%d\n", ctx, sk->sk_family); 1971 1972 tp->is_mptcp = 1; 1973 ctx->icsk_af_ops = icsk->icsk_af_ops; 1974 icsk->icsk_af_ops = subflow_default_af_ops(sk); 1975 ctx->tcp_state_change = sk->sk_state_change; 1976 ctx->tcp_error_report = sk->sk_error_report; 1977 1978 WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable); 1979 WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space); 1980 1981 sk->sk_data_ready = subflow_data_ready; 1982 sk->sk_write_space = subflow_write_space; 1983 sk->sk_state_change = subflow_state_change; 1984 sk->sk_error_report = subflow_error_report; 1985 out: 1986 return err; 1987 } 1988 1989 static void subflow_ulp_release(struct sock *ssk) 1990 { 1991 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 1992 bool release = true; 1993 struct sock *sk; 1994 1995 if (!ctx) 1996 return; 1997 1998 sk = ctx->conn; 1999 if (sk) { 2000 /* if the msk has been orphaned, keep the ctx 2001 * alive, will be freed by __mptcp_close_ssk(), 2002 * when the subflow is still unaccepted 2003 */ 2004 release = ctx->disposable || list_empty(&ctx->node); 2005 2006 /* inet_child_forget() does not call sk_state_change(), 2007 * explicitly trigger the socket close machinery 2008 */ 2009 if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, 2010 &mptcp_sk(sk)->flags)) 2011 mptcp_schedule_work(sk); 2012 sock_put(sk); 2013 } 2014 2015 mptcp_subflow_ops_undo_override(ssk); 2016 if (release) 2017 kfree_rcu(ctx, rcu); 2018 } 2019 2020 static void subflow_ulp_clone(const struct request_sock *req, 2021 struct sock *newsk, 2022 const gfp_t priority) 2023 { 2024 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2025 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk); 2026 struct mptcp_subflow_context *new_ctx; 2027 2028 if (!tcp_rsk(req)->is_mptcp || 2029 (!subflow_req->mp_capable && !subflow_req->mp_join)) { 2030 subflow_ulp_fallback(newsk, old_ctx); 2031 return; 2032 } 2033 2034 new_ctx = subflow_create_ctx(newsk, priority); 2035 if (!new_ctx) { 2036 subflow_ulp_fallback(newsk, old_ctx); 2037 return; 2038 } 2039 2040 new_ctx->conn_finished = 1; 2041 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops; 2042 new_ctx->tcp_state_change = old_ctx->tcp_state_change; 2043 new_ctx->tcp_error_report = old_ctx->tcp_error_report; 2044 new_ctx->rel_write_seq = 1; 2045 2046 if (subflow_req->mp_capable) { 2047 /* see comments in subflow_syn_recv_sock(), MPTCP connection 2048 * is fully established only after we receive the remote key 2049 */ 2050 new_ctx->mp_capable = 1; 2051 new_ctx->local_key = subflow_req->local_key; 2052 new_ctx->token = subflow_req->token; 2053 new_ctx->ssn_offset = subflow_req->ssn_offset; 2054 new_ctx->idsn = subflow_req->idsn; 2055 2056 /* this is the first subflow, id is always 0 */ 2057 subflow_set_local_id(new_ctx, 0); 2058 } else if (subflow_req->mp_join) { 2059 new_ctx->ssn_offset = subflow_req->ssn_offset; 2060 new_ctx->mp_join = 1; 2061 WRITE_ONCE(new_ctx->fully_established, true); 2062 new_ctx->remote_key_valid = 1; 2063 new_ctx->backup = subflow_req->backup; 2064 new_ctx->request_bkup = subflow_req->request_bkup; 2065 WRITE_ONCE(new_ctx->remote_id, subflow_req->remote_id); 2066 new_ctx->token = subflow_req->token; 2067 new_ctx->thmac = subflow_req->thmac; 2068 2069 /* the subflow req id is valid, fetched via subflow_check_req() 2070 * and subflow_token_join_request() 2071 */ 2072 subflow_set_local_id(new_ctx, subflow_req->local_id); 2073 } 2074 } 2075 2076 static void tcp_release_cb_override(struct sock *ssk) 2077 { 2078 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2079 long status; 2080 2081 /* process and clear all the pending actions, but leave the subflow into 2082 * the napi queue. To respect locking, only the same CPU that originated 2083 * the action can touch the list. mptcp_napi_poll will take care of it. 2084 */ 2085 status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0); 2086 if (status) 2087 mptcp_subflow_process_delegated(ssk, status); 2088 2089 tcp_release_cb(ssk); 2090 } 2091 2092 static int tcp_abort_override(struct sock *ssk, int err) 2093 { 2094 /* closing a listener subflow requires a great deal of care. 2095 * keep it simple and just prevent such operation 2096 */ 2097 if (inet_sk_state_load(ssk) == TCP_LISTEN) 2098 return -EINVAL; 2099 2100 return tcp_abort(ssk, err); 2101 } 2102 2103 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = { 2104 .name = "mptcp", 2105 .owner = THIS_MODULE, 2106 .init = subflow_ulp_init, 2107 .release = subflow_ulp_release, 2108 .clone = subflow_ulp_clone, 2109 }; 2110 2111 static int subflow_ops_init(struct request_sock_ops *subflow_ops) 2112 { 2113 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock); 2114 2115 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name, 2116 subflow_ops->obj_size, 0, 2117 SLAB_ACCOUNT | 2118 SLAB_TYPESAFE_BY_RCU, 2119 NULL); 2120 if (!subflow_ops->slab) 2121 return -ENOMEM; 2122 2123 return 0; 2124 } 2125 2126 void __init mptcp_subflow_init(void) 2127 { 2128 mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops; 2129 mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4"; 2130 mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor; 2131 2132 if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0) 2133 panic("MPTCP: failed to init subflow v4 request sock ops\n"); 2134 2135 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops; 2136 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req; 2137 subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack; 2138 2139 subflow_specific = ipv4_specific; 2140 subflow_specific.conn_request = subflow_v4_conn_request; 2141 subflow_specific.syn_recv_sock = subflow_syn_recv_sock; 2142 subflow_specific.sk_rx_dst_set = subflow_finish_connect; 2143 subflow_specific.rebuild_header = subflow_rebuild_header; 2144 2145 tcp_prot_override = tcp_prot; 2146 tcp_prot_override.release_cb = tcp_release_cb_override; 2147 tcp_prot_override.diag_destroy = tcp_abort_override; 2148 2149 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2150 /* In struct mptcp_subflow_request_sock, we assume the TCP request sock 2151 * structures for v4 and v6 have the same size. It should not changed in 2152 * the future but better to make sure to be warned if it is no longer 2153 * the case. 2154 */ 2155 BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock)); 2156 2157 mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops; 2158 mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6"; 2159 mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor; 2160 2161 if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0) 2162 panic("MPTCP: failed to init subflow v6 request sock ops\n"); 2163 2164 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops; 2165 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req; 2166 subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack; 2167 2168 subflow_v6_specific = ipv6_specific; 2169 subflow_v6_specific.conn_request = subflow_v6_conn_request; 2170 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock; 2171 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect; 2172 subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header; 2173 2174 subflow_v6m_specific = subflow_v6_specific; 2175 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit; 2176 subflow_v6m_specific.send_check = ipv4_specific.send_check; 2177 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len; 2178 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced; 2179 subflow_v6m_specific.rebuild_header = subflow_rebuild_header; 2180 2181 tcpv6_prot_override = tcpv6_prot; 2182 tcpv6_prot_override.release_cb = tcp_release_cb_override; 2183 tcpv6_prot_override.diag_destroy = tcp_abort_override; 2184 #endif 2185 2186 mptcp_diag_subflow_init(&subflow_ulp_ops); 2187 2188 if (tcp_register_ulp(&subflow_ulp_ops) != 0) 2189 panic("MPTCP: failed to register subflows to ULP\n"); 2190 } 2191