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