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