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 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 tcp_request_sock_ops.destructor(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 READ_ONCE(msk->pm.accept_subflow); 66 } 67 68 /* validate received token and create truncated hmac and nonce for SYN-ACK */ 69 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req) 70 { 71 struct mptcp_sock *msk = subflow_req->msk; 72 u8 hmac[SHA256_DIGEST_SIZE]; 73 74 get_random_bytes(&subflow_req->local_nonce, sizeof(u32)); 75 76 subflow_generate_hmac(msk->local_key, msk->remote_key, 77 subflow_req->local_nonce, 78 subflow_req->remote_nonce, hmac); 79 80 subflow_req->thmac = get_unaligned_be64(hmac); 81 } 82 83 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req) 84 { 85 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 86 struct mptcp_sock *msk; 87 int local_id; 88 89 msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token); 90 if (!msk) { 91 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN); 92 return NULL; 93 } 94 95 local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req); 96 if (local_id < 0) { 97 sock_put((struct sock *)msk); 98 return NULL; 99 } 100 subflow_req->local_id = local_id; 101 102 return msk; 103 } 104 105 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener) 106 { 107 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 108 109 subflow_req->mp_capable = 0; 110 subflow_req->mp_join = 0; 111 subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener)); 112 subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener)); 113 subflow_req->msk = NULL; 114 mptcp_token_init_request(req); 115 } 116 117 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk) 118 { 119 return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport; 120 } 121 122 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason) 123 { 124 struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 125 126 if (mpext) { 127 memset(mpext, 0, sizeof(*mpext)); 128 mpext->reset_reason = reason; 129 } 130 } 131 132 /* Init mptcp request socket. 133 * 134 * Returns an error code if a JOIN has failed and a TCP reset 135 * should be sent. 136 */ 137 static int subflow_check_req(struct request_sock *req, 138 const struct sock *sk_listener, 139 struct sk_buff *skb) 140 { 141 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 142 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 143 struct mptcp_options_received mp_opt; 144 bool opt_mp_capable, opt_mp_join; 145 146 pr_debug("subflow_req=%p, listener=%p", subflow_req, listener); 147 148 #ifdef CONFIG_TCP_MD5SIG 149 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 150 * TCP option space. 151 */ 152 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) 153 return -EINVAL; 154 #endif 155 156 mptcp_get_options(skb, &mp_opt); 157 158 opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC); 159 opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ); 160 if (opt_mp_capable) { 161 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE); 162 163 if (opt_mp_join) 164 return 0; 165 } else if (opt_mp_join) { 166 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX); 167 } 168 169 if (opt_mp_capable && listener->request_mptcp) { 170 int err, retries = MPTCP_TOKEN_MAX_RETRIES; 171 172 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 173 again: 174 do { 175 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key)); 176 } while (subflow_req->local_key == 0); 177 178 if (unlikely(req->syncookie)) { 179 mptcp_crypto_key_sha(subflow_req->local_key, 180 &subflow_req->token, 181 &subflow_req->idsn); 182 if (mptcp_token_exists(subflow_req->token)) { 183 if (retries-- > 0) 184 goto again; 185 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 186 } else { 187 subflow_req->mp_capable = 1; 188 } 189 return 0; 190 } 191 192 err = mptcp_token_new_request(req); 193 if (err == 0) 194 subflow_req->mp_capable = 1; 195 else if (retries-- > 0) 196 goto again; 197 else 198 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 199 200 } else if (opt_mp_join && listener->request_mptcp) { 201 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 202 subflow_req->mp_join = 1; 203 subflow_req->backup = mp_opt.backup; 204 subflow_req->remote_id = mp_opt.join_id; 205 subflow_req->token = mp_opt.token; 206 subflow_req->remote_nonce = mp_opt.nonce; 207 subflow_req->msk = subflow_token_join_request(req); 208 209 /* Can't fall back to TCP in this case. */ 210 if (!subflow_req->msk) { 211 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 212 return -EPERM; 213 } 214 215 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) { 216 pr_debug("syn inet_sport=%d %d", 217 ntohs(inet_sk(sk_listener)->inet_sport), 218 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport)); 219 if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) { 220 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX); 221 return -EPERM; 222 } 223 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX); 224 } 225 226 subflow_req_create_thmac(subflow_req); 227 228 if (unlikely(req->syncookie)) { 229 if (mptcp_can_accept_new_subflow(subflow_req->msk)) 230 subflow_init_req_cookie_join_save(subflow_req, skb); 231 else 232 return -EPERM; 233 } 234 235 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token, 236 subflow_req->remote_nonce, subflow_req->msk); 237 } 238 239 return 0; 240 } 241 242 int mptcp_subflow_init_cookie_req(struct request_sock *req, 243 const struct sock *sk_listener, 244 struct sk_buff *skb) 245 { 246 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 247 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 248 struct mptcp_options_received mp_opt; 249 bool opt_mp_capable, opt_mp_join; 250 int err; 251 252 subflow_init_req(req, sk_listener); 253 mptcp_get_options(skb, &mp_opt); 254 255 opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC); 256 opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ); 257 if (opt_mp_capable && opt_mp_join) 258 return -EINVAL; 259 260 if (opt_mp_capable && listener->request_mptcp) { 261 if (mp_opt.sndr_key == 0) 262 return -EINVAL; 263 264 subflow_req->local_key = mp_opt.rcvr_key; 265 err = mptcp_token_new_request(req); 266 if (err) 267 return err; 268 269 subflow_req->mp_capable = 1; 270 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 271 } else if (opt_mp_join && listener->request_mptcp) { 272 if (!mptcp_token_join_cookie_init_state(subflow_req, skb)) 273 return -EINVAL; 274 275 subflow_req->mp_join = 1; 276 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 277 } 278 279 return 0; 280 } 281 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req); 282 283 static struct dst_entry *subflow_v4_route_req(const struct sock *sk, 284 struct sk_buff *skb, 285 struct flowi *fl, 286 struct request_sock *req) 287 { 288 struct dst_entry *dst; 289 int err; 290 291 tcp_rsk(req)->is_mptcp = 1; 292 subflow_init_req(req, sk); 293 294 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req); 295 if (!dst) 296 return NULL; 297 298 err = subflow_check_req(req, sk, skb); 299 if (err == 0) 300 return dst; 301 302 dst_release(dst); 303 if (!req->syncookie) 304 tcp_request_sock_ops.send_reset(sk, skb); 305 return NULL; 306 } 307 308 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 309 static struct dst_entry *subflow_v6_route_req(const struct sock *sk, 310 struct sk_buff *skb, 311 struct flowi *fl, 312 struct request_sock *req) 313 { 314 struct dst_entry *dst; 315 int err; 316 317 tcp_rsk(req)->is_mptcp = 1; 318 subflow_init_req(req, sk); 319 320 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req); 321 if (!dst) 322 return NULL; 323 324 err = subflow_check_req(req, sk, skb); 325 if (err == 0) 326 return dst; 327 328 dst_release(dst); 329 if (!req->syncookie) 330 tcp6_request_sock_ops.send_reset(sk, skb); 331 return NULL; 332 } 333 #endif 334 335 /* validate received truncated hmac and create hmac for third ACK */ 336 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow) 337 { 338 u8 hmac[SHA256_DIGEST_SIZE]; 339 u64 thmac; 340 341 subflow_generate_hmac(subflow->remote_key, subflow->local_key, 342 subflow->remote_nonce, subflow->local_nonce, 343 hmac); 344 345 thmac = get_unaligned_be64(hmac); 346 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n", 347 subflow, subflow->token, thmac, subflow->thmac); 348 349 return thmac == subflow->thmac; 350 } 351 352 void mptcp_subflow_reset(struct sock *ssk) 353 { 354 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 355 struct sock *sk = subflow->conn; 356 357 /* must hold: tcp_done() could drop last reference on parent */ 358 sock_hold(sk); 359 360 tcp_set_state(ssk, TCP_CLOSE); 361 tcp_send_active_reset(ssk, GFP_ATOMIC); 362 tcp_done(ssk); 363 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) && 364 schedule_work(&mptcp_sk(sk)->work)) 365 return; /* worker will put sk for us */ 366 367 sock_put(sk); 368 } 369 370 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk) 371 { 372 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport; 373 } 374 375 void __mptcp_set_connected(struct sock *sk) 376 { 377 if (sk->sk_state == TCP_SYN_SENT) { 378 inet_sk_state_store(sk, TCP_ESTABLISHED); 379 sk->sk_state_change(sk); 380 } 381 } 382 383 static void mptcp_set_connected(struct sock *sk) 384 { 385 mptcp_data_lock(sk); 386 if (!sock_owned_by_user(sk)) 387 __mptcp_set_connected(sk); 388 else 389 __set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags); 390 mptcp_data_unlock(sk); 391 } 392 393 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) 394 { 395 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 396 struct mptcp_options_received mp_opt; 397 struct sock *parent = subflow->conn; 398 399 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb); 400 401 /* be sure no special action on any packet other than syn-ack */ 402 if (subflow->conn_finished) 403 return; 404 405 mptcp_propagate_sndbuf(parent, sk); 406 subflow->rel_write_seq = 1; 407 subflow->conn_finished = 1; 408 subflow->ssn_offset = TCP_SKB_CB(skb)->seq; 409 pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset); 410 411 mptcp_get_options(skb, &mp_opt); 412 if (subflow->request_mptcp) { 413 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) { 414 MPTCP_INC_STATS(sock_net(sk), 415 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); 416 mptcp_do_fallback(sk); 417 pr_fallback(mptcp_sk(subflow->conn)); 418 goto fallback; 419 } 420 421 if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD) 422 WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true); 423 if (mp_opt.deny_join_id0) 424 WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true); 425 subflow->mp_capable = 1; 426 subflow->can_ack = 1; 427 subflow->remote_key = mp_opt.sndr_key; 428 pr_debug("subflow=%p, remote_key=%llu", subflow, 429 subflow->remote_key); 430 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK); 431 mptcp_finish_connect(sk); 432 mptcp_set_connected(parent); 433 } else if (subflow->request_join) { 434 u8 hmac[SHA256_DIGEST_SIZE]; 435 436 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ)) { 437 subflow->reset_reason = MPTCP_RST_EMPTCP; 438 goto do_reset; 439 } 440 441 subflow->backup = mp_opt.backup; 442 subflow->thmac = mp_opt.thmac; 443 subflow->remote_nonce = mp_opt.nonce; 444 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d", 445 subflow, subflow->thmac, subflow->remote_nonce, 446 subflow->backup); 447 448 if (!subflow_thmac_valid(subflow)) { 449 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC); 450 subflow->reset_reason = MPTCP_RST_EMPTCP; 451 goto do_reset; 452 } 453 454 if (!mptcp_finish_join(sk)) 455 goto do_reset; 456 457 subflow_generate_hmac(subflow->local_key, subflow->remote_key, 458 subflow->local_nonce, 459 subflow->remote_nonce, 460 hmac); 461 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN); 462 463 subflow->mp_join = 1; 464 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX); 465 466 if (subflow_use_different_dport(mptcp_sk(parent), sk)) { 467 pr_debug("synack inet_dport=%d %d", 468 ntohs(inet_sk(sk)->inet_dport), 469 ntohs(inet_sk(parent)->inet_dport)); 470 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX); 471 } 472 } else if (mptcp_check_fallback(sk)) { 473 fallback: 474 mptcp_rcv_space_init(mptcp_sk(parent), sk); 475 mptcp_set_connected(parent); 476 } 477 return; 478 479 do_reset: 480 subflow->reset_transient = 0; 481 mptcp_subflow_reset(sk); 482 } 483 484 struct request_sock_ops mptcp_subflow_request_sock_ops; 485 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init; 486 487 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb) 488 { 489 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 490 491 pr_debug("subflow=%p", subflow); 492 493 /* Never answer to SYNs sent to broadcast or multicast */ 494 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 495 goto drop; 496 497 return tcp_conn_request(&mptcp_subflow_request_sock_ops, 498 &subflow_request_sock_ipv4_ops, 499 sk, skb); 500 drop: 501 tcp_listendrop(sk); 502 return 0; 503 } 504 505 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 506 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init; 507 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init; 508 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init; 509 static struct proto tcpv6_prot_override; 510 511 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb) 512 { 513 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 514 515 pr_debug("subflow=%p", subflow); 516 517 if (skb->protocol == htons(ETH_P_IP)) 518 return subflow_v4_conn_request(sk, skb); 519 520 if (!ipv6_unicast_destination(skb)) 521 goto drop; 522 523 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) { 524 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS); 525 return 0; 526 } 527 528 return tcp_conn_request(&mptcp_subflow_request_sock_ops, 529 &subflow_request_sock_ipv6_ops, sk, skb); 530 531 drop: 532 tcp_listendrop(sk); 533 return 0; /* don't send reset */ 534 } 535 #endif 536 537 /* validate hmac received in third ACK */ 538 static bool subflow_hmac_valid(const struct request_sock *req, 539 const struct mptcp_options_received *mp_opt) 540 { 541 const struct mptcp_subflow_request_sock *subflow_req; 542 u8 hmac[SHA256_DIGEST_SIZE]; 543 struct mptcp_sock *msk; 544 545 subflow_req = mptcp_subflow_rsk(req); 546 msk = subflow_req->msk; 547 if (!msk) 548 return false; 549 550 subflow_generate_hmac(msk->remote_key, msk->local_key, 551 subflow_req->remote_nonce, 552 subflow_req->local_nonce, hmac); 553 554 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN); 555 } 556 557 static void mptcp_sock_destruct(struct sock *sk) 558 { 559 /* if new mptcp socket isn't accepted, it is free'd 560 * from the tcp listener sockets request queue, linked 561 * from req->sk. The tcp socket is released. 562 * This calls the ULP release function which will 563 * also remove the mptcp socket, via 564 * sock_put(ctx->conn). 565 * 566 * Problem is that the mptcp socket will be in 567 * ESTABLISHED state and will not have the SOCK_DEAD flag. 568 * Both result in warnings from inet_sock_destruct. 569 */ 570 if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 571 sk->sk_state = TCP_CLOSE; 572 WARN_ON_ONCE(sk->sk_socket); 573 sock_orphan(sk); 574 } 575 576 mptcp_destroy_common(mptcp_sk(sk)); 577 inet_sock_destruct(sk); 578 } 579 580 static void mptcp_force_close(struct sock *sk) 581 { 582 /* the msk is not yet exposed to user-space */ 583 inet_sk_state_store(sk, TCP_CLOSE); 584 sk_common_release(sk); 585 } 586 587 static void subflow_ulp_fallback(struct sock *sk, 588 struct mptcp_subflow_context *old_ctx) 589 { 590 struct inet_connection_sock *icsk = inet_csk(sk); 591 592 mptcp_subflow_tcp_fallback(sk, old_ctx); 593 icsk->icsk_ulp_ops = NULL; 594 rcu_assign_pointer(icsk->icsk_ulp_data, NULL); 595 tcp_sk(sk)->is_mptcp = 0; 596 597 mptcp_subflow_ops_undo_override(sk); 598 } 599 600 static void subflow_drop_ctx(struct sock *ssk) 601 { 602 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 603 604 if (!ctx) 605 return; 606 607 subflow_ulp_fallback(ssk, ctx); 608 if (ctx->conn) 609 sock_put(ctx->conn); 610 611 kfree_rcu(ctx, rcu); 612 } 613 614 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow, 615 struct mptcp_options_received *mp_opt) 616 { 617 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 618 619 subflow->remote_key = mp_opt->sndr_key; 620 subflow->fully_established = 1; 621 subflow->can_ack = 1; 622 WRITE_ONCE(msk->fully_established, true); 623 } 624 625 static struct sock *subflow_syn_recv_sock(const struct sock *sk, 626 struct sk_buff *skb, 627 struct request_sock *req, 628 struct dst_entry *dst, 629 struct request_sock *req_unhash, 630 bool *own_req) 631 { 632 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk); 633 struct mptcp_subflow_request_sock *subflow_req; 634 struct mptcp_options_received mp_opt; 635 bool fallback, fallback_is_fatal; 636 struct sock *new_msk = NULL; 637 struct sock *child; 638 639 pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn); 640 641 /* After child creation we must look for MPC even when options 642 * are not parsed 643 */ 644 mp_opt.suboptions = 0; 645 646 /* hopefully temporary handling for MP_JOIN+syncookie */ 647 subflow_req = mptcp_subflow_rsk(req); 648 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join; 649 fallback = !tcp_rsk(req)->is_mptcp; 650 if (fallback) 651 goto create_child; 652 653 /* if the sk is MP_CAPABLE, we try to fetch the client key */ 654 if (subflow_req->mp_capable) { 655 /* we can receive and accept an in-window, out-of-order pkt, 656 * which may not carry the MP_CAPABLE opt even on mptcp enabled 657 * paths: always try to extract the peer key, and fallback 658 * for packets missing it. 659 * Even OoO DSS packets coming legitly after dropped or 660 * reordered MPC will cause fallback, but we don't have other 661 * options. 662 */ 663 mptcp_get_options(skb, &mp_opt); 664 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) { 665 fallback = true; 666 goto create_child; 667 } 668 669 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req); 670 if (!new_msk) 671 fallback = true; 672 } else if (subflow_req->mp_join) { 673 mptcp_get_options(skb, &mp_opt); 674 if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ) || 675 !subflow_hmac_valid(req, &mp_opt) || 676 !mptcp_can_accept_new_subflow(subflow_req->msk)) { 677 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC); 678 fallback = true; 679 } 680 } 681 682 create_child: 683 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst, 684 req_unhash, own_req); 685 686 if (child && *own_req) { 687 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child); 688 689 tcp_rsk(req)->drop_req = false; 690 691 /* we need to fallback on ctx allocation failure and on pre-reqs 692 * checking above. In the latter scenario we additionally need 693 * to reset the context to non MPTCP status. 694 */ 695 if (!ctx || fallback) { 696 if (fallback_is_fatal) { 697 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 698 goto dispose_child; 699 } 700 701 subflow_drop_ctx(child); 702 goto out; 703 } 704 705 /* ssk inherits options of listener sk */ 706 ctx->setsockopt_seq = listener->setsockopt_seq; 707 708 if (ctx->mp_capable) { 709 /* this can't race with mptcp_close(), as the msk is 710 * not yet exposted to user-space 711 */ 712 inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED); 713 714 /* record the newly created socket as the first msk 715 * subflow, but don't link it yet into conn_list 716 */ 717 WRITE_ONCE(mptcp_sk(new_msk)->first, child); 718 719 /* new mpc subflow takes ownership of the newly 720 * created mptcp socket 721 */ 722 new_msk->sk_destruct = mptcp_sock_destruct; 723 mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq; 724 mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1); 725 mptcp_token_accept(subflow_req, mptcp_sk(new_msk)); 726 ctx->conn = new_msk; 727 new_msk = NULL; 728 729 /* with OoO packets we can reach here without ingress 730 * mpc option 731 */ 732 if (mp_opt.suboptions & OPTIONS_MPTCP_MPC) 733 mptcp_subflow_fully_established(ctx, &mp_opt); 734 } else if (ctx->mp_join) { 735 struct mptcp_sock *owner; 736 737 owner = subflow_req->msk; 738 if (!owner) { 739 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 740 goto dispose_child; 741 } 742 743 /* move the msk reference ownership to the subflow */ 744 subflow_req->msk = NULL; 745 ctx->conn = (struct sock *)owner; 746 747 if (subflow_use_different_sport(owner, sk)) { 748 pr_debug("ack inet_sport=%d %d", 749 ntohs(inet_sk(sk)->inet_sport), 750 ntohs(inet_sk((struct sock *)owner)->inet_sport)); 751 if (!mptcp_pm_sport_in_anno_list(owner, sk)) { 752 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX); 753 goto dispose_child; 754 } 755 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX); 756 } 757 758 if (!mptcp_finish_join(child)) 759 goto dispose_child; 760 761 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX); 762 tcp_rsk(req)->drop_req = true; 763 } 764 } 765 766 out: 767 /* dispose of the left over mptcp master, if any */ 768 if (unlikely(new_msk)) 769 mptcp_force_close(new_msk); 770 771 /* check for expected invariant - should never trigger, just help 772 * catching eariler subtle bugs 773 */ 774 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp && 775 (!mptcp_subflow_ctx(child) || 776 !mptcp_subflow_ctx(child)->conn)); 777 return child; 778 779 dispose_child: 780 subflow_drop_ctx(child); 781 tcp_rsk(req)->drop_req = true; 782 inet_csk_prepare_for_destroy_sock(child); 783 tcp_done(child); 784 req->rsk_ops->send_reset(sk, skb); 785 786 /* The last child reference will be released by the caller */ 787 return child; 788 } 789 790 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init; 791 static struct proto tcp_prot_override; 792 793 enum mapping_status { 794 MAPPING_OK, 795 MAPPING_INVALID, 796 MAPPING_EMPTY, 797 MAPPING_DATA_FIN, 798 MAPPING_DUMMY 799 }; 800 801 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn) 802 { 803 pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d", 804 ssn, subflow->map_subflow_seq, subflow->map_data_len); 805 } 806 807 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb) 808 { 809 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 810 unsigned int skb_consumed; 811 812 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq; 813 if (WARN_ON_ONCE(skb_consumed >= skb->len)) 814 return true; 815 816 return skb->len - skb_consumed <= subflow->map_data_len - 817 mptcp_subflow_get_map_offset(subflow); 818 } 819 820 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb) 821 { 822 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 823 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 824 825 if (unlikely(before(ssn, subflow->map_subflow_seq))) { 826 /* Mapping covers data later in the subflow stream, 827 * currently unsupported. 828 */ 829 dbg_bad_map(subflow, ssn); 830 return false; 831 } 832 if (unlikely(!before(ssn, subflow->map_subflow_seq + 833 subflow->map_data_len))) { 834 /* Mapping does covers past subflow data, invalid */ 835 dbg_bad_map(subflow, ssn); 836 return false; 837 } 838 return true; 839 } 840 841 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb, 842 bool csum_reqd) 843 { 844 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 845 u32 offset, seq, delta; 846 u16 csum; 847 int len; 848 849 if (!csum_reqd) 850 return MAPPING_OK; 851 852 /* mapping already validated on previous traversal */ 853 if (subflow->map_csum_len == subflow->map_data_len) 854 return MAPPING_OK; 855 856 /* traverse the receive queue, ensuring it contains a full 857 * DSS mapping and accumulating the related csum. 858 * Preserve the accoumlate csum across multiple calls, to compute 859 * the csum only once 860 */ 861 delta = subflow->map_data_len - subflow->map_csum_len; 862 for (;;) { 863 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len; 864 offset = seq - TCP_SKB_CB(skb)->seq; 865 866 /* if the current skb has not been accounted yet, csum its contents 867 * up to the amount covered by the current DSS 868 */ 869 if (offset < skb->len) { 870 __wsum csum; 871 872 len = min(skb->len - offset, delta); 873 csum = skb_checksum(skb, offset, len, 0); 874 subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum, 875 subflow->map_csum_len); 876 877 delta -= len; 878 subflow->map_csum_len += len; 879 } 880 if (delta == 0) 881 break; 882 883 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) { 884 /* if this subflow is closed, the partial mapping 885 * will be never completed; flush the pending skbs, so 886 * that subflow_sched_work_if_closed() can kick in 887 */ 888 if (unlikely(ssk->sk_state == TCP_CLOSE)) 889 while ((skb = skb_peek(&ssk->sk_receive_queue))) 890 sk_eat_skb(ssk, skb); 891 892 /* not enough data to validate the csum */ 893 return MAPPING_EMPTY; 894 } 895 896 /* the DSS mapping for next skbs will be validated later, 897 * when a get_mapping_status call will process such skb 898 */ 899 skb = skb->next; 900 } 901 902 /* note that 'map_data_len' accounts only for the carried data, does 903 * not include the eventual seq increment due to the data fin, 904 * while the pseudo header requires the original DSS data len, 905 * including that 906 */ 907 csum = __mptcp_make_csum(subflow->map_seq, 908 subflow->map_subflow_seq, 909 subflow->map_data_len + subflow->map_data_fin, 910 subflow->map_data_csum); 911 if (unlikely(csum)) { 912 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR); 913 subflow->send_mp_fail = 1; 914 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPFAILTX); 915 return subflow->mp_join ? MAPPING_INVALID : MAPPING_DUMMY; 916 } 917 918 return MAPPING_OK; 919 } 920 921 static enum mapping_status get_mapping_status(struct sock *ssk, 922 struct mptcp_sock *msk) 923 { 924 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 925 bool csum_reqd = READ_ONCE(msk->csum_enabled); 926 struct mptcp_ext *mpext; 927 struct sk_buff *skb; 928 u16 data_len; 929 u64 map_seq; 930 931 skb = skb_peek(&ssk->sk_receive_queue); 932 if (!skb) 933 return MAPPING_EMPTY; 934 935 if (mptcp_check_fallback(ssk)) 936 return MAPPING_DUMMY; 937 938 mpext = mptcp_get_ext(skb); 939 if (!mpext || !mpext->use_map) { 940 if (!subflow->map_valid && !skb->len) { 941 /* the TCP stack deliver 0 len FIN pkt to the receive 942 * queue, that is the only 0len pkts ever expected here, 943 * and we can admit no mapping only for 0 len pkts 944 */ 945 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 946 WARN_ONCE(1, "0len seq %d:%d flags %x", 947 TCP_SKB_CB(skb)->seq, 948 TCP_SKB_CB(skb)->end_seq, 949 TCP_SKB_CB(skb)->tcp_flags); 950 sk_eat_skb(ssk, skb); 951 return MAPPING_EMPTY; 952 } 953 954 if (!subflow->map_valid) 955 return MAPPING_INVALID; 956 957 goto validate_seq; 958 } 959 960 trace_get_mapping_status(mpext); 961 962 data_len = mpext->data_len; 963 if (data_len == 0) { 964 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX); 965 return MAPPING_INVALID; 966 } 967 968 if (mpext->data_fin == 1) { 969 if (data_len == 1) { 970 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq, 971 mpext->dsn64); 972 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq); 973 if (subflow->map_valid) { 974 /* A DATA_FIN might arrive in a DSS 975 * option before the previous mapping 976 * has been fully consumed. Continue 977 * handling the existing mapping. 978 */ 979 skb_ext_del(skb, SKB_EXT_MPTCP); 980 return MAPPING_OK; 981 } else { 982 if (updated && schedule_work(&msk->work)) 983 sock_hold((struct sock *)msk); 984 985 return MAPPING_DATA_FIN; 986 } 987 } else { 988 u64 data_fin_seq = mpext->data_seq + data_len - 1; 989 990 /* If mpext->data_seq is a 32-bit value, data_fin_seq 991 * must also be limited to 32 bits. 992 */ 993 if (!mpext->dsn64) 994 data_fin_seq &= GENMASK_ULL(31, 0); 995 996 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64); 997 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d", 998 data_fin_seq, mpext->dsn64); 999 } 1000 1001 /* Adjust for DATA_FIN using 1 byte of sequence space */ 1002 data_len--; 1003 } 1004 1005 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64); 1006 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64); 1007 1008 if (subflow->map_valid) { 1009 /* Allow replacing only with an identical map */ 1010 if (subflow->map_seq == map_seq && 1011 subflow->map_subflow_seq == mpext->subflow_seq && 1012 subflow->map_data_len == data_len && 1013 subflow->map_csum_reqd == mpext->csum_reqd) { 1014 skb_ext_del(skb, SKB_EXT_MPTCP); 1015 goto validate_csum; 1016 } 1017 1018 /* If this skb data are fully covered by the current mapping, 1019 * the new map would need caching, which is not supported 1020 */ 1021 if (skb_is_fully_mapped(ssk, skb)) { 1022 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH); 1023 return MAPPING_INVALID; 1024 } 1025 1026 /* will validate the next map after consuming the current one */ 1027 goto validate_csum; 1028 } 1029 1030 subflow->map_seq = map_seq; 1031 subflow->map_subflow_seq = mpext->subflow_seq; 1032 subflow->map_data_len = data_len; 1033 subflow->map_valid = 1; 1034 subflow->map_data_fin = mpext->data_fin; 1035 subflow->mpc_map = mpext->mpc_map; 1036 subflow->map_csum_reqd = mpext->csum_reqd; 1037 subflow->map_csum_len = 0; 1038 subflow->map_data_csum = csum_unfold(mpext->csum); 1039 1040 /* Cfr RFC 8684 Section 3.3.0 */ 1041 if (unlikely(subflow->map_csum_reqd != csum_reqd)) 1042 return MAPPING_INVALID; 1043 1044 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u", 1045 subflow->map_seq, subflow->map_subflow_seq, 1046 subflow->map_data_len, subflow->map_csum_reqd, 1047 subflow->map_data_csum); 1048 1049 validate_seq: 1050 /* we revalidate valid mapping on new skb, because we must ensure 1051 * the current skb is completely covered by the available mapping 1052 */ 1053 if (!validate_mapping(ssk, skb)) { 1054 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH); 1055 return MAPPING_INVALID; 1056 } 1057 1058 skb_ext_del(skb, SKB_EXT_MPTCP); 1059 1060 validate_csum: 1061 return validate_data_csum(ssk, skb, csum_reqd); 1062 } 1063 1064 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb, 1065 u64 limit) 1066 { 1067 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1068 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 1069 u32 incr; 1070 1071 incr = limit >= skb->len ? skb->len + fin : limit; 1072 1073 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len, 1074 subflow->map_subflow_seq); 1075 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA); 1076 tcp_sk(ssk)->copied_seq += incr; 1077 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq)) 1078 sk_eat_skb(ssk, skb); 1079 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) 1080 subflow->map_valid = 0; 1081 } 1082 1083 /* sched mptcp worker to remove the subflow if no more data is pending */ 1084 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk) 1085 { 1086 struct sock *sk = (struct sock *)msk; 1087 1088 if (likely(ssk->sk_state != TCP_CLOSE)) 1089 return; 1090 1091 if (skb_queue_empty(&ssk->sk_receive_queue) && 1092 !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) { 1093 sock_hold(sk); 1094 if (!schedule_work(&msk->work)) 1095 sock_put(sk); 1096 } 1097 } 1098 1099 static bool subflow_check_data_avail(struct sock *ssk) 1100 { 1101 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1102 enum mapping_status status; 1103 struct mptcp_sock *msk; 1104 struct sk_buff *skb; 1105 1106 if (!skb_peek(&ssk->sk_receive_queue)) 1107 WRITE_ONCE(subflow->data_avail, 0); 1108 if (subflow->data_avail) 1109 return true; 1110 1111 msk = mptcp_sk(subflow->conn); 1112 for (;;) { 1113 u64 ack_seq; 1114 u64 old_ack; 1115 1116 status = get_mapping_status(ssk, msk); 1117 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue)); 1118 if (unlikely(status == MAPPING_INVALID)) 1119 goto fallback; 1120 1121 if (unlikely(status == MAPPING_DUMMY)) 1122 goto fallback; 1123 1124 if (status != MAPPING_OK) 1125 goto no_data; 1126 1127 skb = skb_peek(&ssk->sk_receive_queue); 1128 if (WARN_ON_ONCE(!skb)) 1129 goto no_data; 1130 1131 /* if msk lacks the remote key, this subflow must provide an 1132 * MP_CAPABLE-based mapping 1133 */ 1134 if (unlikely(!READ_ONCE(msk->can_ack))) { 1135 if (!subflow->mpc_map) 1136 goto fallback; 1137 WRITE_ONCE(msk->remote_key, subflow->remote_key); 1138 WRITE_ONCE(msk->ack_seq, subflow->map_seq); 1139 WRITE_ONCE(msk->can_ack, true); 1140 } 1141 1142 old_ack = READ_ONCE(msk->ack_seq); 1143 ack_seq = mptcp_subflow_get_mapped_dsn(subflow); 1144 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack, 1145 ack_seq); 1146 if (unlikely(before64(ack_seq, old_ack))) { 1147 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq); 1148 continue; 1149 } 1150 1151 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); 1152 break; 1153 } 1154 return true; 1155 1156 no_data: 1157 subflow_sched_work_if_closed(msk, ssk); 1158 return false; 1159 1160 fallback: 1161 /* RFC 8684 section 3.7. */ 1162 if (subflow->send_mp_fail) { 1163 if (mptcp_has_another_subflow(ssk)) { 1164 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1165 sk_eat_skb(ssk, skb); 1166 } 1167 ssk->sk_err = EBADMSG; 1168 tcp_set_state(ssk, TCP_CLOSE); 1169 subflow->reset_transient = 0; 1170 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX; 1171 tcp_send_active_reset(ssk, GFP_ATOMIC); 1172 WRITE_ONCE(subflow->data_avail, 0); 1173 return true; 1174 } 1175 1176 if (subflow->mp_join || subflow->fully_established) { 1177 /* fatal protocol error, close the socket. 1178 * subflow_error_report() will introduce the appropriate barriers 1179 */ 1180 ssk->sk_err = EBADMSG; 1181 tcp_set_state(ssk, TCP_CLOSE); 1182 subflow->reset_transient = 0; 1183 subflow->reset_reason = MPTCP_RST_EMPTCP; 1184 tcp_send_active_reset(ssk, GFP_ATOMIC); 1185 WRITE_ONCE(subflow->data_avail, 0); 1186 return false; 1187 } 1188 1189 __mptcp_do_fallback(msk); 1190 skb = skb_peek(&ssk->sk_receive_queue); 1191 subflow->map_valid = 1; 1192 subflow->map_seq = READ_ONCE(msk->ack_seq); 1193 subflow->map_data_len = skb->len; 1194 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1195 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); 1196 return true; 1197 } 1198 1199 bool mptcp_subflow_data_available(struct sock *sk) 1200 { 1201 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1202 1203 /* check if current mapping is still valid */ 1204 if (subflow->map_valid && 1205 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) { 1206 subflow->map_valid = 0; 1207 WRITE_ONCE(subflow->data_avail, 0); 1208 1209 pr_debug("Done with mapping: seq=%u data_len=%u", 1210 subflow->map_subflow_seq, 1211 subflow->map_data_len); 1212 } 1213 1214 return subflow_check_data_avail(sk); 1215 } 1216 1217 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy, 1218 * not the ssk one. 1219 * 1220 * In mptcp, rwin is about the mptcp-level connection data. 1221 * 1222 * Data that is still on the ssk rx queue can thus be ignored, 1223 * as far as mptcp peer is concerned that data is still inflight. 1224 * DSS ACK is updated when skb is moved to the mptcp rx queue. 1225 */ 1226 void mptcp_space(const struct sock *ssk, int *space, int *full_space) 1227 { 1228 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1229 const struct sock *sk = subflow->conn; 1230 1231 *space = __mptcp_space(sk); 1232 *full_space = tcp_full_space(sk); 1233 } 1234 1235 void __mptcp_error_report(struct sock *sk) 1236 { 1237 struct mptcp_subflow_context *subflow; 1238 struct mptcp_sock *msk = mptcp_sk(sk); 1239 1240 mptcp_for_each_subflow(msk, subflow) { 1241 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1242 int err = sock_error(ssk); 1243 1244 if (!err) 1245 continue; 1246 1247 /* only propagate errors on fallen-back sockets or 1248 * on MPC connect 1249 */ 1250 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk)) 1251 continue; 1252 1253 inet_sk_state_store(sk, inet_sk_state_load(ssk)); 1254 sk->sk_err = -err; 1255 1256 /* This barrier is coupled with smp_rmb() in mptcp_poll() */ 1257 smp_wmb(); 1258 sk_error_report(sk); 1259 break; 1260 } 1261 } 1262 1263 static void subflow_error_report(struct sock *ssk) 1264 { 1265 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1266 1267 mptcp_data_lock(sk); 1268 if (!sock_owned_by_user(sk)) 1269 __mptcp_error_report(sk); 1270 else 1271 __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags); 1272 mptcp_data_unlock(sk); 1273 } 1274 1275 static void subflow_data_ready(struct sock *sk) 1276 { 1277 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1278 u16 state = 1 << inet_sk_state_load(sk); 1279 struct sock *parent = subflow->conn; 1280 struct mptcp_sock *msk; 1281 1282 msk = mptcp_sk(parent); 1283 if (state & TCPF_LISTEN) { 1284 /* MPJ subflow are removed from accept queue before reaching here, 1285 * avoid stray wakeups 1286 */ 1287 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue)) 1288 return; 1289 1290 parent->sk_data_ready(parent); 1291 return; 1292 } 1293 1294 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable && 1295 !subflow->mp_join && !(state & TCPF_CLOSE)); 1296 1297 if (mptcp_subflow_data_available(sk)) 1298 mptcp_data_ready(parent, sk); 1299 else if (unlikely(sk->sk_err)) 1300 subflow_error_report(sk); 1301 } 1302 1303 static void subflow_write_space(struct sock *ssk) 1304 { 1305 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1306 1307 mptcp_propagate_sndbuf(sk, ssk); 1308 mptcp_write_space(sk); 1309 } 1310 1311 static const struct inet_connection_sock_af_ops * 1312 subflow_default_af_ops(struct sock *sk) 1313 { 1314 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1315 if (sk->sk_family == AF_INET6) 1316 return &subflow_v6_specific; 1317 #endif 1318 return &subflow_specific; 1319 } 1320 1321 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1322 void mptcpv6_handle_mapped(struct sock *sk, bool mapped) 1323 { 1324 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1325 struct inet_connection_sock *icsk = inet_csk(sk); 1326 const struct inet_connection_sock_af_ops *target; 1327 1328 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk); 1329 1330 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d", 1331 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped); 1332 1333 if (likely(icsk->icsk_af_ops == target)) 1334 return; 1335 1336 subflow->icsk_af_ops = icsk->icsk_af_ops; 1337 icsk->icsk_af_ops = target; 1338 } 1339 #endif 1340 1341 void mptcp_info2sockaddr(const struct mptcp_addr_info *info, 1342 struct sockaddr_storage *addr, 1343 unsigned short family) 1344 { 1345 memset(addr, 0, sizeof(*addr)); 1346 addr->ss_family = family; 1347 if (addr->ss_family == AF_INET) { 1348 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr; 1349 1350 if (info->family == AF_INET) 1351 in_addr->sin_addr = info->addr; 1352 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1353 else if (ipv6_addr_v4mapped(&info->addr6)) 1354 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3]; 1355 #endif 1356 in_addr->sin_port = info->port; 1357 } 1358 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1359 else if (addr->ss_family == AF_INET6) { 1360 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr; 1361 1362 if (info->family == AF_INET) 1363 ipv6_addr_set_v4mapped(info->addr.s_addr, 1364 &in6_addr->sin6_addr); 1365 else 1366 in6_addr->sin6_addr = info->addr6; 1367 in6_addr->sin6_port = info->port; 1368 } 1369 #endif 1370 } 1371 1372 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc, 1373 const struct mptcp_addr_info *remote) 1374 { 1375 struct mptcp_sock *msk = mptcp_sk(sk); 1376 struct mptcp_subflow_context *subflow; 1377 struct sockaddr_storage addr; 1378 int remote_id = remote->id; 1379 int local_id = loc->id; 1380 struct socket *sf; 1381 struct sock *ssk; 1382 u32 remote_token; 1383 int addrlen; 1384 int ifindex; 1385 u8 flags; 1386 int err; 1387 1388 if (!mptcp_is_fully_established(sk)) 1389 return -ENOTCONN; 1390 1391 err = mptcp_subflow_create_socket(sk, &sf); 1392 if (err) 1393 return err; 1394 1395 ssk = sf->sk; 1396 subflow = mptcp_subflow_ctx(ssk); 1397 do { 1398 get_random_bytes(&subflow->local_nonce, sizeof(u32)); 1399 } while (!subflow->local_nonce); 1400 1401 if (!local_id) { 1402 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk); 1403 if (err < 0) 1404 goto failed; 1405 1406 local_id = err; 1407 } 1408 1409 mptcp_pm_get_flags_and_ifindex_by_id(sock_net(sk), local_id, 1410 &flags, &ifindex); 1411 subflow->remote_key = msk->remote_key; 1412 subflow->local_key = msk->local_key; 1413 subflow->token = msk->token; 1414 mptcp_info2sockaddr(loc, &addr, ssk->sk_family); 1415 1416 addrlen = sizeof(struct sockaddr_in); 1417 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1418 if (addr.ss_family == AF_INET6) 1419 addrlen = sizeof(struct sockaddr_in6); 1420 #endif 1421 mptcp_sockopt_sync(msk, ssk); 1422 1423 ssk->sk_bound_dev_if = ifindex; 1424 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen); 1425 if (err) 1426 goto failed; 1427 1428 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL); 1429 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk, 1430 remote_token, local_id, remote_id); 1431 subflow->remote_token = remote_token; 1432 subflow->local_id = local_id; 1433 subflow->remote_id = remote_id; 1434 subflow->request_join = 1; 1435 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP); 1436 mptcp_info2sockaddr(remote, &addr, ssk->sk_family); 1437 1438 sock_hold(ssk); 1439 list_add_tail(&subflow->node, &msk->conn_list); 1440 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK); 1441 if (err && err != -EINPROGRESS) 1442 goto failed_unlink; 1443 1444 /* discard the subflow socket */ 1445 mptcp_sock_graft(ssk, sk->sk_socket); 1446 iput(SOCK_INODE(sf)); 1447 return err; 1448 1449 failed_unlink: 1450 list_del(&subflow->node); 1451 sock_put(mptcp_subflow_tcp_sock(subflow)); 1452 1453 failed: 1454 subflow->disposable = 1; 1455 sock_release(sf); 1456 return err; 1457 } 1458 1459 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child) 1460 { 1461 #ifdef CONFIG_SOCK_CGROUP_DATA 1462 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, 1463 *child_skcd = &child->sk_cgrp_data; 1464 1465 /* only the additional subflows created by kworkers have to be modified */ 1466 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) != 1467 cgroup_id(sock_cgroup_ptr(child_skcd))) { 1468 #ifdef CONFIG_MEMCG 1469 struct mem_cgroup *memcg = parent->sk_memcg; 1470 1471 mem_cgroup_sk_free(child); 1472 if (memcg && css_tryget(&memcg->css)) 1473 child->sk_memcg = memcg; 1474 #endif /* CONFIG_MEMCG */ 1475 1476 cgroup_sk_free(child_skcd); 1477 *child_skcd = *parent_skcd; 1478 cgroup_sk_clone(child_skcd); 1479 } 1480 #endif /* CONFIG_SOCK_CGROUP_DATA */ 1481 } 1482 1483 static void mptcp_subflow_ops_override(struct sock *ssk) 1484 { 1485 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1486 if (ssk->sk_prot == &tcpv6_prot) 1487 ssk->sk_prot = &tcpv6_prot_override; 1488 else 1489 #endif 1490 ssk->sk_prot = &tcp_prot_override; 1491 } 1492 1493 static void mptcp_subflow_ops_undo_override(struct sock *ssk) 1494 { 1495 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1496 if (ssk->sk_prot == &tcpv6_prot_override) 1497 ssk->sk_prot = &tcpv6_prot; 1498 else 1499 #endif 1500 ssk->sk_prot = &tcp_prot; 1501 } 1502 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock) 1503 { 1504 struct mptcp_subflow_context *subflow; 1505 struct net *net = sock_net(sk); 1506 struct socket *sf; 1507 int err; 1508 1509 /* un-accepted server sockets can reach here - on bad configuration 1510 * bail early to avoid greater trouble later 1511 */ 1512 if (unlikely(!sk->sk_socket)) 1513 return -EINVAL; 1514 1515 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP, 1516 &sf); 1517 if (err) 1518 return err; 1519 1520 lock_sock(sf->sk); 1521 1522 /* the newly created socket has to be in the same cgroup as its parent */ 1523 mptcp_attach_cgroup(sk, sf->sk); 1524 1525 /* kernel sockets do not by default acquire net ref, but TCP timer 1526 * needs it. 1527 */ 1528 sf->sk->sk_net_refcnt = 1; 1529 get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL); 1530 sock_inuse_add(net, 1); 1531 err = tcp_set_ulp(sf->sk, "mptcp"); 1532 release_sock(sf->sk); 1533 1534 if (err) { 1535 sock_release(sf); 1536 return err; 1537 } 1538 1539 /* the newly created socket really belongs to the owning MPTCP master 1540 * socket, even if for additional subflows the allocation is performed 1541 * by a kernel workqueue. Adjust inode references, so that the 1542 * procfs/diag interaces really show this one belonging to the correct 1543 * user. 1544 */ 1545 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino; 1546 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid; 1547 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid; 1548 1549 subflow = mptcp_subflow_ctx(sf->sk); 1550 pr_debug("subflow=%p", subflow); 1551 1552 *new_sock = sf; 1553 sock_hold(sk); 1554 subflow->conn = sk; 1555 mptcp_subflow_ops_override(sf->sk); 1556 1557 return 0; 1558 } 1559 1560 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, 1561 gfp_t priority) 1562 { 1563 struct inet_connection_sock *icsk = inet_csk(sk); 1564 struct mptcp_subflow_context *ctx; 1565 1566 ctx = kzalloc(sizeof(*ctx), priority); 1567 if (!ctx) 1568 return NULL; 1569 1570 rcu_assign_pointer(icsk->icsk_ulp_data, ctx); 1571 INIT_LIST_HEAD(&ctx->node); 1572 INIT_LIST_HEAD(&ctx->delegated_node); 1573 1574 pr_debug("subflow=%p", ctx); 1575 1576 ctx->tcp_sock = sk; 1577 1578 return ctx; 1579 } 1580 1581 static void __subflow_state_change(struct sock *sk) 1582 { 1583 struct socket_wq *wq; 1584 1585 rcu_read_lock(); 1586 wq = rcu_dereference(sk->sk_wq); 1587 if (skwq_has_sleeper(wq)) 1588 wake_up_interruptible_all(&wq->wait); 1589 rcu_read_unlock(); 1590 } 1591 1592 static bool subflow_is_done(const struct sock *sk) 1593 { 1594 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE; 1595 } 1596 1597 static void subflow_state_change(struct sock *sk) 1598 { 1599 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1600 struct sock *parent = subflow->conn; 1601 1602 __subflow_state_change(sk); 1603 1604 if (subflow_simultaneous_connect(sk)) { 1605 mptcp_propagate_sndbuf(parent, sk); 1606 mptcp_do_fallback(sk); 1607 mptcp_rcv_space_init(mptcp_sk(parent), sk); 1608 pr_fallback(mptcp_sk(parent)); 1609 subflow->conn_finished = 1; 1610 mptcp_set_connected(parent); 1611 } 1612 1613 /* as recvmsg() does not acquire the subflow socket for ssk selection 1614 * a fin packet carrying a DSS can be unnoticed if we don't trigger 1615 * the data available machinery here. 1616 */ 1617 if (mptcp_subflow_data_available(sk)) 1618 mptcp_data_ready(parent, sk); 1619 else if (unlikely(sk->sk_err)) 1620 subflow_error_report(sk); 1621 1622 subflow_sched_work_if_closed(mptcp_sk(parent), sk); 1623 1624 if (__mptcp_check_fallback(mptcp_sk(parent)) && 1625 !subflow->rx_eof && subflow_is_done(sk)) { 1626 subflow->rx_eof = 1; 1627 mptcp_subflow_eof(parent); 1628 } 1629 } 1630 1631 static int subflow_ulp_init(struct sock *sk) 1632 { 1633 struct inet_connection_sock *icsk = inet_csk(sk); 1634 struct mptcp_subflow_context *ctx; 1635 struct tcp_sock *tp = tcp_sk(sk); 1636 int err = 0; 1637 1638 /* disallow attaching ULP to a socket unless it has been 1639 * created with sock_create_kern() 1640 */ 1641 if (!sk->sk_kern_sock) { 1642 err = -EOPNOTSUPP; 1643 goto out; 1644 } 1645 1646 ctx = subflow_create_ctx(sk, GFP_KERNEL); 1647 if (!ctx) { 1648 err = -ENOMEM; 1649 goto out; 1650 } 1651 1652 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family); 1653 1654 tp->is_mptcp = 1; 1655 ctx->icsk_af_ops = icsk->icsk_af_ops; 1656 icsk->icsk_af_ops = subflow_default_af_ops(sk); 1657 ctx->tcp_state_change = sk->sk_state_change; 1658 ctx->tcp_error_report = sk->sk_error_report; 1659 1660 WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable); 1661 WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space); 1662 1663 sk->sk_data_ready = subflow_data_ready; 1664 sk->sk_write_space = subflow_write_space; 1665 sk->sk_state_change = subflow_state_change; 1666 sk->sk_error_report = subflow_error_report; 1667 out: 1668 return err; 1669 } 1670 1671 static void subflow_ulp_release(struct sock *ssk) 1672 { 1673 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 1674 bool release = true; 1675 struct sock *sk; 1676 1677 if (!ctx) 1678 return; 1679 1680 sk = ctx->conn; 1681 if (sk) { 1682 /* if the msk has been orphaned, keep the ctx 1683 * alive, will be freed by __mptcp_close_ssk(), 1684 * when the subflow is still unaccepted 1685 */ 1686 release = ctx->disposable || list_empty(&ctx->node); 1687 sock_put(sk); 1688 } 1689 1690 mptcp_subflow_ops_undo_override(ssk); 1691 if (release) 1692 kfree_rcu(ctx, rcu); 1693 } 1694 1695 static void subflow_ulp_clone(const struct request_sock *req, 1696 struct sock *newsk, 1697 const gfp_t priority) 1698 { 1699 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1700 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk); 1701 struct mptcp_subflow_context *new_ctx; 1702 1703 if (!tcp_rsk(req)->is_mptcp || 1704 (!subflow_req->mp_capable && !subflow_req->mp_join)) { 1705 subflow_ulp_fallback(newsk, old_ctx); 1706 return; 1707 } 1708 1709 new_ctx = subflow_create_ctx(newsk, priority); 1710 if (!new_ctx) { 1711 subflow_ulp_fallback(newsk, old_ctx); 1712 return; 1713 } 1714 1715 new_ctx->conn_finished = 1; 1716 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops; 1717 new_ctx->tcp_state_change = old_ctx->tcp_state_change; 1718 new_ctx->tcp_error_report = old_ctx->tcp_error_report; 1719 new_ctx->rel_write_seq = 1; 1720 new_ctx->tcp_sock = newsk; 1721 1722 if (subflow_req->mp_capable) { 1723 /* see comments in subflow_syn_recv_sock(), MPTCP connection 1724 * is fully established only after we receive the remote key 1725 */ 1726 new_ctx->mp_capable = 1; 1727 new_ctx->local_key = subflow_req->local_key; 1728 new_ctx->token = subflow_req->token; 1729 new_ctx->ssn_offset = subflow_req->ssn_offset; 1730 new_ctx->idsn = subflow_req->idsn; 1731 } else if (subflow_req->mp_join) { 1732 new_ctx->ssn_offset = subflow_req->ssn_offset; 1733 new_ctx->mp_join = 1; 1734 new_ctx->fully_established = 1; 1735 new_ctx->backup = subflow_req->backup; 1736 new_ctx->local_id = subflow_req->local_id; 1737 new_ctx->remote_id = subflow_req->remote_id; 1738 new_ctx->token = subflow_req->token; 1739 new_ctx->thmac = subflow_req->thmac; 1740 } 1741 } 1742 1743 static void tcp_release_cb_override(struct sock *ssk) 1744 { 1745 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1746 1747 if (mptcp_subflow_has_delegated_action(subflow)) 1748 mptcp_subflow_process_delegated(ssk); 1749 1750 tcp_release_cb(ssk); 1751 } 1752 1753 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = { 1754 .name = "mptcp", 1755 .owner = THIS_MODULE, 1756 .init = subflow_ulp_init, 1757 .release = subflow_ulp_release, 1758 .clone = subflow_ulp_clone, 1759 }; 1760 1761 static int subflow_ops_init(struct request_sock_ops *subflow_ops) 1762 { 1763 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock); 1764 subflow_ops->slab_name = "request_sock_subflow"; 1765 1766 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name, 1767 subflow_ops->obj_size, 0, 1768 SLAB_ACCOUNT | 1769 SLAB_TYPESAFE_BY_RCU, 1770 NULL); 1771 if (!subflow_ops->slab) 1772 return -ENOMEM; 1773 1774 subflow_ops->destructor = subflow_req_destructor; 1775 1776 return 0; 1777 } 1778 1779 void __init mptcp_subflow_init(void) 1780 { 1781 mptcp_subflow_request_sock_ops = tcp_request_sock_ops; 1782 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0) 1783 panic("MPTCP: failed to init subflow request sock ops\n"); 1784 1785 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops; 1786 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req; 1787 1788 subflow_specific = ipv4_specific; 1789 subflow_specific.conn_request = subflow_v4_conn_request; 1790 subflow_specific.syn_recv_sock = subflow_syn_recv_sock; 1791 subflow_specific.sk_rx_dst_set = subflow_finish_connect; 1792 1793 tcp_prot_override = tcp_prot; 1794 tcp_prot_override.release_cb = tcp_release_cb_override; 1795 1796 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1797 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops; 1798 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req; 1799 1800 subflow_v6_specific = ipv6_specific; 1801 subflow_v6_specific.conn_request = subflow_v6_conn_request; 1802 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock; 1803 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect; 1804 1805 subflow_v6m_specific = subflow_v6_specific; 1806 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit; 1807 subflow_v6m_specific.send_check = ipv4_specific.send_check; 1808 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len; 1809 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced; 1810 subflow_v6m_specific.net_frag_header_len = 0; 1811 1812 tcpv6_prot_override = tcpv6_prot; 1813 tcpv6_prot_override.release_cb = tcp_release_cb_override; 1814 #endif 1815 1816 mptcp_diag_subflow_init(&subflow_ulp_ops); 1817 1818 if (tcp_register_ulp(&subflow_ulp_ops) != 0) 1819 panic("MPTCP: failed to register subflows to ULP\n"); 1820 } 1821