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