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