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