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 subflow->send_mp_fail = 1; 914 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPFAILTX); 915 return subflow->mp_join ? MAPPING_INVALID : MAPPING_DUMMY; 916 } 917 918 return MAPPING_OK; 919 } 920 921 static enum mapping_status get_mapping_status(struct sock *ssk, 922 struct mptcp_sock *msk) 923 { 924 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 925 bool csum_reqd = READ_ONCE(msk->csum_enabled); 926 struct mptcp_ext *mpext; 927 struct sk_buff *skb; 928 u16 data_len; 929 u64 map_seq; 930 931 skb = skb_peek(&ssk->sk_receive_queue); 932 if (!skb) 933 return MAPPING_EMPTY; 934 935 if (mptcp_check_fallback(ssk)) 936 return MAPPING_DUMMY; 937 938 mpext = mptcp_get_ext(skb); 939 if (!mpext || !mpext->use_map) { 940 if (!subflow->map_valid && !skb->len) { 941 /* the TCP stack deliver 0 len FIN pkt to the receive 942 * queue, that is the only 0len pkts ever expected here, 943 * and we can admit no mapping only for 0 len pkts 944 */ 945 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 946 WARN_ONCE(1, "0len seq %d:%d flags %x", 947 TCP_SKB_CB(skb)->seq, 948 TCP_SKB_CB(skb)->end_seq, 949 TCP_SKB_CB(skb)->tcp_flags); 950 sk_eat_skb(ssk, skb); 951 return MAPPING_EMPTY; 952 } 953 954 if (!subflow->map_valid) 955 return MAPPING_INVALID; 956 957 goto validate_seq; 958 } 959 960 trace_get_mapping_status(mpext); 961 962 data_len = mpext->data_len; 963 if (data_len == 0) { 964 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX); 965 return MAPPING_INVALID; 966 } 967 968 if (mpext->data_fin == 1) { 969 if (data_len == 1) { 970 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq, 971 mpext->dsn64); 972 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq); 973 if (subflow->map_valid) { 974 /* A DATA_FIN might arrive in a DSS 975 * option before the previous mapping 976 * has been fully consumed. Continue 977 * handling the existing mapping. 978 */ 979 skb_ext_del(skb, SKB_EXT_MPTCP); 980 return MAPPING_OK; 981 } else { 982 if (updated && schedule_work(&msk->work)) 983 sock_hold((struct sock *)msk); 984 985 return MAPPING_DATA_FIN; 986 } 987 } else { 988 u64 data_fin_seq = mpext->data_seq + data_len - 1; 989 990 /* If mpext->data_seq is a 32-bit value, data_fin_seq 991 * must also be limited to 32 bits. 992 */ 993 if (!mpext->dsn64) 994 data_fin_seq &= GENMASK_ULL(31, 0); 995 996 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64); 997 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d", 998 data_fin_seq, mpext->dsn64); 999 } 1000 1001 /* Adjust for DATA_FIN using 1 byte of sequence space */ 1002 data_len--; 1003 } 1004 1005 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64); 1006 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64); 1007 1008 if (subflow->map_valid) { 1009 /* Allow replacing only with an identical map */ 1010 if (subflow->map_seq == map_seq && 1011 subflow->map_subflow_seq == mpext->subflow_seq && 1012 subflow->map_data_len == data_len && 1013 subflow->map_csum_reqd == mpext->csum_reqd) { 1014 skb_ext_del(skb, SKB_EXT_MPTCP); 1015 goto validate_csum; 1016 } 1017 1018 /* If this skb data are fully covered by the current mapping, 1019 * the new map would need caching, which is not supported 1020 */ 1021 if (skb_is_fully_mapped(ssk, skb)) { 1022 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH); 1023 return MAPPING_INVALID; 1024 } 1025 1026 /* will validate the next map after consuming the current one */ 1027 goto validate_csum; 1028 } 1029 1030 subflow->map_seq = map_seq; 1031 subflow->map_subflow_seq = mpext->subflow_seq; 1032 subflow->map_data_len = data_len; 1033 subflow->map_valid = 1; 1034 subflow->map_data_fin = mpext->data_fin; 1035 subflow->mpc_map = mpext->mpc_map; 1036 subflow->map_csum_reqd = mpext->csum_reqd; 1037 subflow->map_csum_len = 0; 1038 subflow->map_data_csum = csum_unfold(mpext->csum); 1039 1040 /* Cfr RFC 8684 Section 3.3.0 */ 1041 if (unlikely(subflow->map_csum_reqd != csum_reqd)) 1042 return MAPPING_INVALID; 1043 1044 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u", 1045 subflow->map_seq, subflow->map_subflow_seq, 1046 subflow->map_data_len, subflow->map_csum_reqd, 1047 subflow->map_data_csum); 1048 1049 validate_seq: 1050 /* we revalidate valid mapping on new skb, because we must ensure 1051 * the current skb is completely covered by the available mapping 1052 */ 1053 if (!validate_mapping(ssk, skb)) { 1054 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH); 1055 return MAPPING_INVALID; 1056 } 1057 1058 skb_ext_del(skb, SKB_EXT_MPTCP); 1059 1060 validate_csum: 1061 return validate_data_csum(ssk, skb, csum_reqd); 1062 } 1063 1064 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb, 1065 u64 limit) 1066 { 1067 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1068 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 1069 u32 incr; 1070 1071 incr = limit >= skb->len ? skb->len + fin : limit; 1072 1073 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len, 1074 subflow->map_subflow_seq); 1075 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA); 1076 tcp_sk(ssk)->copied_seq += incr; 1077 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq)) 1078 sk_eat_skb(ssk, skb); 1079 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) 1080 subflow->map_valid = 0; 1081 } 1082 1083 /* sched mptcp worker to remove the subflow if no more data is pending */ 1084 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk) 1085 { 1086 struct sock *sk = (struct sock *)msk; 1087 1088 if (likely(ssk->sk_state != TCP_CLOSE)) 1089 return; 1090 1091 if (skb_queue_empty(&ssk->sk_receive_queue) && 1092 !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) { 1093 sock_hold(sk); 1094 if (!schedule_work(&msk->work)) 1095 sock_put(sk); 1096 } 1097 } 1098 1099 static bool subflow_check_data_avail(struct sock *ssk) 1100 { 1101 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1102 enum mapping_status status; 1103 struct mptcp_sock *msk; 1104 struct sk_buff *skb; 1105 1106 if (!skb_peek(&ssk->sk_receive_queue)) 1107 WRITE_ONCE(subflow->data_avail, 0); 1108 if (subflow->data_avail) 1109 return true; 1110 1111 msk = mptcp_sk(subflow->conn); 1112 for (;;) { 1113 u64 ack_seq; 1114 u64 old_ack; 1115 1116 status = get_mapping_status(ssk, msk); 1117 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue)); 1118 if (unlikely(status == MAPPING_INVALID)) 1119 goto fallback; 1120 1121 if (unlikely(status == MAPPING_DUMMY)) 1122 goto fallback; 1123 1124 if (status != MAPPING_OK) 1125 goto no_data; 1126 1127 skb = skb_peek(&ssk->sk_receive_queue); 1128 if (WARN_ON_ONCE(!skb)) 1129 goto no_data; 1130 1131 /* if msk lacks the remote key, this subflow must provide an 1132 * MP_CAPABLE-based mapping 1133 */ 1134 if (unlikely(!READ_ONCE(msk->can_ack))) { 1135 if (!subflow->mpc_map) 1136 goto fallback; 1137 WRITE_ONCE(msk->remote_key, subflow->remote_key); 1138 WRITE_ONCE(msk->ack_seq, subflow->map_seq); 1139 WRITE_ONCE(msk->can_ack, true); 1140 } 1141 1142 old_ack = READ_ONCE(msk->ack_seq); 1143 ack_seq = mptcp_subflow_get_mapped_dsn(subflow); 1144 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack, 1145 ack_seq); 1146 if (unlikely(before64(ack_seq, old_ack))) { 1147 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq); 1148 continue; 1149 } 1150 1151 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); 1152 break; 1153 } 1154 return true; 1155 1156 no_data: 1157 subflow_sched_work_if_closed(msk, ssk); 1158 return false; 1159 1160 fallback: 1161 /* RFC 8684 section 3.7. */ 1162 if (subflow->send_mp_fail) { 1163 if (mptcp_has_another_subflow(ssk)) { 1164 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1165 sk_eat_skb(ssk, skb); 1166 } 1167 ssk->sk_err = EBADMSG; 1168 tcp_set_state(ssk, TCP_CLOSE); 1169 subflow->reset_transient = 0; 1170 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX; 1171 tcp_send_active_reset(ssk, GFP_ATOMIC); 1172 WRITE_ONCE(subflow->data_avail, 0); 1173 return true; 1174 } 1175 1176 if (subflow->mp_join || subflow->fully_established) { 1177 /* fatal protocol error, close the socket. 1178 * subflow_error_report() will introduce the appropriate barriers 1179 */ 1180 ssk->sk_err = EBADMSG; 1181 tcp_set_state(ssk, TCP_CLOSE); 1182 subflow->reset_transient = 0; 1183 subflow->reset_reason = MPTCP_RST_EMPTCP; 1184 tcp_send_active_reset(ssk, GFP_ATOMIC); 1185 WRITE_ONCE(subflow->data_avail, 0); 1186 return false; 1187 } 1188 1189 __mptcp_do_fallback(msk); 1190 skb = skb_peek(&ssk->sk_receive_queue); 1191 subflow->map_valid = 1; 1192 subflow->map_seq = READ_ONCE(msk->ack_seq); 1193 subflow->map_data_len = skb->len; 1194 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1195 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL); 1196 return true; 1197 } 1198 1199 bool mptcp_subflow_data_available(struct sock *sk) 1200 { 1201 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1202 1203 /* check if current mapping is still valid */ 1204 if (subflow->map_valid && 1205 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) { 1206 subflow->map_valid = 0; 1207 WRITE_ONCE(subflow->data_avail, 0); 1208 1209 pr_debug("Done with mapping: seq=%u data_len=%u", 1210 subflow->map_subflow_seq, 1211 subflow->map_data_len); 1212 } 1213 1214 return subflow_check_data_avail(sk); 1215 } 1216 1217 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy, 1218 * not the ssk one. 1219 * 1220 * In mptcp, rwin is about the mptcp-level connection data. 1221 * 1222 * Data that is still on the ssk rx queue can thus be ignored, 1223 * as far as mptcp peer is concerned that data is still inflight. 1224 * DSS ACK is updated when skb is moved to the mptcp rx queue. 1225 */ 1226 void mptcp_space(const struct sock *ssk, int *space, int *full_space) 1227 { 1228 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1229 const struct sock *sk = subflow->conn; 1230 1231 *space = __mptcp_space(sk); 1232 *full_space = tcp_full_space(sk); 1233 } 1234 1235 void __mptcp_error_report(struct sock *sk) 1236 { 1237 struct mptcp_subflow_context *subflow; 1238 struct mptcp_sock *msk = mptcp_sk(sk); 1239 1240 mptcp_for_each_subflow(msk, subflow) { 1241 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1242 int err = sock_error(ssk); 1243 1244 if (!err) 1245 continue; 1246 1247 /* only propagate errors on fallen-back sockets or 1248 * on MPC connect 1249 */ 1250 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk)) 1251 continue; 1252 1253 inet_sk_state_store(sk, inet_sk_state_load(ssk)); 1254 sk->sk_err = -err; 1255 1256 /* This barrier is coupled with smp_rmb() in mptcp_poll() */ 1257 smp_wmb(); 1258 sk_error_report(sk); 1259 break; 1260 } 1261 } 1262 1263 static void subflow_error_report(struct sock *ssk) 1264 { 1265 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1266 1267 mptcp_data_lock(sk); 1268 if (!sock_owned_by_user(sk)) 1269 __mptcp_error_report(sk); 1270 else 1271 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags); 1272 mptcp_data_unlock(sk); 1273 } 1274 1275 static void subflow_data_ready(struct sock *sk) 1276 { 1277 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1278 u16 state = 1 << inet_sk_state_load(sk); 1279 struct sock *parent = subflow->conn; 1280 struct mptcp_sock *msk; 1281 1282 msk = mptcp_sk(parent); 1283 if (state & TCPF_LISTEN) { 1284 /* MPJ subflow are removed from accept queue before reaching here, 1285 * avoid stray wakeups 1286 */ 1287 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue)) 1288 return; 1289 1290 set_bit(MPTCP_DATA_READY, &msk->flags); 1291 parent->sk_data_ready(parent); 1292 return; 1293 } 1294 1295 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable && 1296 !subflow->mp_join && !(state & TCPF_CLOSE)); 1297 1298 if (mptcp_subflow_data_available(sk)) 1299 mptcp_data_ready(parent, sk); 1300 else if (unlikely(sk->sk_err)) 1301 subflow_error_report(sk); 1302 } 1303 1304 static void subflow_write_space(struct sock *ssk) 1305 { 1306 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1307 1308 mptcp_propagate_sndbuf(sk, ssk); 1309 mptcp_write_space(sk); 1310 } 1311 1312 static struct inet_connection_sock_af_ops * 1313 subflow_default_af_ops(struct sock *sk) 1314 { 1315 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1316 if (sk->sk_family == AF_INET6) 1317 return &subflow_v6_specific; 1318 #endif 1319 return &subflow_specific; 1320 } 1321 1322 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1323 void mptcpv6_handle_mapped(struct sock *sk, bool mapped) 1324 { 1325 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1326 struct inet_connection_sock *icsk = inet_csk(sk); 1327 struct inet_connection_sock_af_ops *target; 1328 1329 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk); 1330 1331 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d", 1332 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped); 1333 1334 if (likely(icsk->icsk_af_ops == target)) 1335 return; 1336 1337 subflow->icsk_af_ops = icsk->icsk_af_ops; 1338 icsk->icsk_af_ops = target; 1339 } 1340 #endif 1341 1342 void mptcp_info2sockaddr(const struct mptcp_addr_info *info, 1343 struct sockaddr_storage *addr, 1344 unsigned short family) 1345 { 1346 memset(addr, 0, sizeof(*addr)); 1347 addr->ss_family = family; 1348 if (addr->ss_family == AF_INET) { 1349 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr; 1350 1351 if (info->family == AF_INET) 1352 in_addr->sin_addr = info->addr; 1353 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1354 else if (ipv6_addr_v4mapped(&info->addr6)) 1355 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3]; 1356 #endif 1357 in_addr->sin_port = info->port; 1358 } 1359 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1360 else if (addr->ss_family == AF_INET6) { 1361 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr; 1362 1363 if (info->family == AF_INET) 1364 ipv6_addr_set_v4mapped(info->addr.s_addr, 1365 &in6_addr->sin6_addr); 1366 else 1367 in6_addr->sin6_addr = info->addr6; 1368 in6_addr->sin6_port = info->port; 1369 } 1370 #endif 1371 } 1372 1373 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc, 1374 const struct mptcp_addr_info *remote) 1375 { 1376 struct mptcp_sock *msk = mptcp_sk(sk); 1377 struct mptcp_subflow_context *subflow; 1378 struct sockaddr_storage addr; 1379 int remote_id = remote->id; 1380 int local_id = loc->id; 1381 struct socket *sf; 1382 struct sock *ssk; 1383 u32 remote_token; 1384 int addrlen; 1385 int ifindex; 1386 u8 flags; 1387 int err; 1388 1389 if (!mptcp_is_fully_established(sk)) 1390 return -ENOTCONN; 1391 1392 err = mptcp_subflow_create_socket(sk, &sf); 1393 if (err) 1394 return err; 1395 1396 ssk = sf->sk; 1397 subflow = mptcp_subflow_ctx(ssk); 1398 do { 1399 get_random_bytes(&subflow->local_nonce, sizeof(u32)); 1400 } while (!subflow->local_nonce); 1401 1402 if (!local_id) { 1403 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk); 1404 if (err < 0) 1405 goto failed; 1406 1407 local_id = err; 1408 } 1409 1410 mptcp_pm_get_flags_and_ifindex_by_id(sock_net(sk), local_id, 1411 &flags, &ifindex); 1412 subflow->remote_key = msk->remote_key; 1413 subflow->local_key = msk->local_key; 1414 subflow->token = msk->token; 1415 mptcp_info2sockaddr(loc, &addr, ssk->sk_family); 1416 1417 addrlen = sizeof(struct sockaddr_in); 1418 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1419 if (addr.ss_family == AF_INET6) 1420 addrlen = sizeof(struct sockaddr_in6); 1421 #endif 1422 ssk->sk_bound_dev_if = ifindex; 1423 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen); 1424 if (err) 1425 goto failed; 1426 1427 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL); 1428 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk, 1429 remote_token, local_id, remote_id); 1430 subflow->remote_token = remote_token; 1431 subflow->local_id = local_id; 1432 subflow->remote_id = remote_id; 1433 subflow->request_join = 1; 1434 subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP); 1435 mptcp_info2sockaddr(remote, &addr, ssk->sk_family); 1436 1437 mptcp_add_pending_subflow(msk, subflow); 1438 mptcp_sockopt_sync(msk, ssk); 1439 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK); 1440 if (err && err != -EINPROGRESS) 1441 goto failed_unlink; 1442 1443 /* discard the subflow socket */ 1444 mptcp_sock_graft(ssk, sk->sk_socket); 1445 iput(SOCK_INODE(sf)); 1446 return err; 1447 1448 failed_unlink: 1449 spin_lock_bh(&msk->join_list_lock); 1450 list_del(&subflow->node); 1451 spin_unlock_bh(&msk->join_list_lock); 1452 sock_put(mptcp_subflow_tcp_sock(subflow)); 1453 1454 failed: 1455 subflow->disposable = 1; 1456 sock_release(sf); 1457 return err; 1458 } 1459 1460 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child) 1461 { 1462 #ifdef CONFIG_SOCK_CGROUP_DATA 1463 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data, 1464 *child_skcd = &child->sk_cgrp_data; 1465 1466 /* only the additional subflows created by kworkers have to be modified */ 1467 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) != 1468 cgroup_id(sock_cgroup_ptr(child_skcd))) { 1469 #ifdef CONFIG_MEMCG 1470 struct mem_cgroup *memcg = parent->sk_memcg; 1471 1472 mem_cgroup_sk_free(child); 1473 if (memcg && css_tryget(&memcg->css)) 1474 child->sk_memcg = memcg; 1475 #endif /* CONFIG_MEMCG */ 1476 1477 cgroup_sk_free(child_skcd); 1478 *child_skcd = *parent_skcd; 1479 cgroup_sk_clone(child_skcd); 1480 } 1481 #endif /* CONFIG_SOCK_CGROUP_DATA */ 1482 } 1483 1484 static void mptcp_subflow_ops_override(struct sock *ssk) 1485 { 1486 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1487 if (ssk->sk_prot == &tcpv6_prot) 1488 ssk->sk_prot = &tcpv6_prot_override; 1489 else 1490 #endif 1491 ssk->sk_prot = &tcp_prot_override; 1492 } 1493 1494 static void mptcp_subflow_ops_undo_override(struct sock *ssk) 1495 { 1496 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1497 if (ssk->sk_prot == &tcpv6_prot_override) 1498 ssk->sk_prot = &tcpv6_prot; 1499 else 1500 #endif 1501 ssk->sk_prot = &tcp_prot; 1502 } 1503 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock) 1504 { 1505 struct mptcp_subflow_context *subflow; 1506 struct net *net = sock_net(sk); 1507 struct socket *sf; 1508 int err; 1509 1510 /* un-accepted server sockets can reach here - on bad configuration 1511 * bail early to avoid greater trouble later 1512 */ 1513 if (unlikely(!sk->sk_socket)) 1514 return -EINVAL; 1515 1516 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP, 1517 &sf); 1518 if (err) 1519 return err; 1520 1521 lock_sock(sf->sk); 1522 1523 /* the newly created socket has to be in the same cgroup as its parent */ 1524 mptcp_attach_cgroup(sk, sf->sk); 1525 1526 /* kernel sockets do not by default acquire net ref, but TCP timer 1527 * needs it. 1528 */ 1529 sf->sk->sk_net_refcnt = 1; 1530 get_net(net); 1531 #ifdef CONFIG_PROC_FS 1532 this_cpu_add(*net->core.sock_inuse, 1); 1533 #endif 1534 err = tcp_set_ulp(sf->sk, "mptcp"); 1535 release_sock(sf->sk); 1536 1537 if (err) { 1538 sock_release(sf); 1539 return err; 1540 } 1541 1542 /* the newly created socket really belongs to the owning MPTCP master 1543 * socket, even if for additional subflows the allocation is performed 1544 * by a kernel workqueue. Adjust inode references, so that the 1545 * procfs/diag interaces really show this one belonging to the correct 1546 * user. 1547 */ 1548 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino; 1549 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid; 1550 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid; 1551 1552 subflow = mptcp_subflow_ctx(sf->sk); 1553 pr_debug("subflow=%p", subflow); 1554 1555 *new_sock = sf; 1556 sock_hold(sk); 1557 subflow->conn = sk; 1558 mptcp_subflow_ops_override(sf->sk); 1559 1560 return 0; 1561 } 1562 1563 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, 1564 gfp_t priority) 1565 { 1566 struct inet_connection_sock *icsk = inet_csk(sk); 1567 struct mptcp_subflow_context *ctx; 1568 1569 ctx = kzalloc(sizeof(*ctx), priority); 1570 if (!ctx) 1571 return NULL; 1572 1573 rcu_assign_pointer(icsk->icsk_ulp_data, ctx); 1574 INIT_LIST_HEAD(&ctx->node); 1575 INIT_LIST_HEAD(&ctx->delegated_node); 1576 1577 pr_debug("subflow=%p", ctx); 1578 1579 ctx->tcp_sock = sk; 1580 1581 return ctx; 1582 } 1583 1584 static void __subflow_state_change(struct sock *sk) 1585 { 1586 struct socket_wq *wq; 1587 1588 rcu_read_lock(); 1589 wq = rcu_dereference(sk->sk_wq); 1590 if (skwq_has_sleeper(wq)) 1591 wake_up_interruptible_all(&wq->wait); 1592 rcu_read_unlock(); 1593 } 1594 1595 static bool subflow_is_done(const struct sock *sk) 1596 { 1597 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE; 1598 } 1599 1600 static void subflow_state_change(struct sock *sk) 1601 { 1602 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1603 struct sock *parent = subflow->conn; 1604 1605 __subflow_state_change(sk); 1606 1607 if (subflow_simultaneous_connect(sk)) { 1608 mptcp_propagate_sndbuf(parent, sk); 1609 mptcp_do_fallback(sk); 1610 mptcp_rcv_space_init(mptcp_sk(parent), sk); 1611 pr_fallback(mptcp_sk(parent)); 1612 subflow->conn_finished = 1; 1613 mptcp_set_connected(parent); 1614 } 1615 1616 /* as recvmsg() does not acquire the subflow socket for ssk selection 1617 * a fin packet carrying a DSS can be unnoticed if we don't trigger 1618 * the data available machinery here. 1619 */ 1620 if (mptcp_subflow_data_available(sk)) 1621 mptcp_data_ready(parent, sk); 1622 else if (unlikely(sk->sk_err)) 1623 subflow_error_report(sk); 1624 1625 subflow_sched_work_if_closed(mptcp_sk(parent), sk); 1626 1627 if (__mptcp_check_fallback(mptcp_sk(parent)) && 1628 !subflow->rx_eof && subflow_is_done(sk)) { 1629 subflow->rx_eof = 1; 1630 mptcp_subflow_eof(parent); 1631 } 1632 } 1633 1634 static int subflow_ulp_init(struct sock *sk) 1635 { 1636 struct inet_connection_sock *icsk = inet_csk(sk); 1637 struct mptcp_subflow_context *ctx; 1638 struct tcp_sock *tp = tcp_sk(sk); 1639 int err = 0; 1640 1641 /* disallow attaching ULP to a socket unless it has been 1642 * created with sock_create_kern() 1643 */ 1644 if (!sk->sk_kern_sock) { 1645 err = -EOPNOTSUPP; 1646 goto out; 1647 } 1648 1649 ctx = subflow_create_ctx(sk, GFP_KERNEL); 1650 if (!ctx) { 1651 err = -ENOMEM; 1652 goto out; 1653 } 1654 1655 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family); 1656 1657 tp->is_mptcp = 1; 1658 ctx->icsk_af_ops = icsk->icsk_af_ops; 1659 icsk->icsk_af_ops = subflow_default_af_ops(sk); 1660 ctx->tcp_data_ready = sk->sk_data_ready; 1661 ctx->tcp_state_change = sk->sk_state_change; 1662 ctx->tcp_write_space = sk->sk_write_space; 1663 ctx->tcp_error_report = sk->sk_error_report; 1664 sk->sk_data_ready = subflow_data_ready; 1665 sk->sk_write_space = subflow_write_space; 1666 sk->sk_state_change = subflow_state_change; 1667 sk->sk_error_report = subflow_error_report; 1668 out: 1669 return err; 1670 } 1671 1672 static void subflow_ulp_release(struct sock *ssk) 1673 { 1674 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 1675 bool release = true; 1676 struct sock *sk; 1677 1678 if (!ctx) 1679 return; 1680 1681 sk = ctx->conn; 1682 if (sk) { 1683 /* if the msk has been orphaned, keep the ctx 1684 * alive, will be freed by __mptcp_close_ssk(), 1685 * when the subflow is still unaccepted 1686 */ 1687 release = ctx->disposable || list_empty(&ctx->node); 1688 sock_put(sk); 1689 } 1690 1691 mptcp_subflow_ops_undo_override(ssk); 1692 if (release) 1693 kfree_rcu(ctx, rcu); 1694 } 1695 1696 static void subflow_ulp_clone(const struct request_sock *req, 1697 struct sock *newsk, 1698 const gfp_t priority) 1699 { 1700 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1701 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk); 1702 struct mptcp_subflow_context *new_ctx; 1703 1704 if (!tcp_rsk(req)->is_mptcp || 1705 (!subflow_req->mp_capable && !subflow_req->mp_join)) { 1706 subflow_ulp_fallback(newsk, old_ctx); 1707 return; 1708 } 1709 1710 new_ctx = subflow_create_ctx(newsk, priority); 1711 if (!new_ctx) { 1712 subflow_ulp_fallback(newsk, old_ctx); 1713 return; 1714 } 1715 1716 new_ctx->conn_finished = 1; 1717 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops; 1718 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready; 1719 new_ctx->tcp_state_change = old_ctx->tcp_state_change; 1720 new_ctx->tcp_write_space = old_ctx->tcp_write_space; 1721 new_ctx->tcp_error_report = old_ctx->tcp_error_report; 1722 new_ctx->rel_write_seq = 1; 1723 new_ctx->tcp_sock = newsk; 1724 1725 if (subflow_req->mp_capable) { 1726 /* see comments in subflow_syn_recv_sock(), MPTCP connection 1727 * is fully established only after we receive the remote key 1728 */ 1729 new_ctx->mp_capable = 1; 1730 new_ctx->local_key = subflow_req->local_key; 1731 new_ctx->token = subflow_req->token; 1732 new_ctx->ssn_offset = subflow_req->ssn_offset; 1733 new_ctx->idsn = subflow_req->idsn; 1734 } else if (subflow_req->mp_join) { 1735 new_ctx->ssn_offset = subflow_req->ssn_offset; 1736 new_ctx->mp_join = 1; 1737 new_ctx->fully_established = 1; 1738 new_ctx->backup = subflow_req->backup; 1739 new_ctx->local_id = subflow_req->local_id; 1740 new_ctx->remote_id = subflow_req->remote_id; 1741 new_ctx->token = subflow_req->token; 1742 new_ctx->thmac = subflow_req->thmac; 1743 } 1744 } 1745 1746 static void tcp_release_cb_override(struct sock *ssk) 1747 { 1748 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1749 1750 if (mptcp_subflow_has_delegated_action(subflow)) 1751 mptcp_subflow_process_delegated(ssk); 1752 1753 tcp_release_cb(ssk); 1754 } 1755 1756 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = { 1757 .name = "mptcp", 1758 .owner = THIS_MODULE, 1759 .init = subflow_ulp_init, 1760 .release = subflow_ulp_release, 1761 .clone = subflow_ulp_clone, 1762 }; 1763 1764 static int subflow_ops_init(struct request_sock_ops *subflow_ops) 1765 { 1766 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock); 1767 subflow_ops->slab_name = "request_sock_subflow"; 1768 1769 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name, 1770 subflow_ops->obj_size, 0, 1771 SLAB_ACCOUNT | 1772 SLAB_TYPESAFE_BY_RCU, 1773 NULL); 1774 if (!subflow_ops->slab) 1775 return -ENOMEM; 1776 1777 subflow_ops->destructor = subflow_req_destructor; 1778 1779 return 0; 1780 } 1781 1782 void __init mptcp_subflow_init(void) 1783 { 1784 mptcp_subflow_request_sock_ops = tcp_request_sock_ops; 1785 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0) 1786 panic("MPTCP: failed to init subflow request sock ops\n"); 1787 1788 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops; 1789 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req; 1790 1791 subflow_specific = ipv4_specific; 1792 subflow_specific.conn_request = subflow_v4_conn_request; 1793 subflow_specific.syn_recv_sock = subflow_syn_recv_sock; 1794 subflow_specific.sk_rx_dst_set = subflow_finish_connect; 1795 1796 tcp_prot_override = tcp_prot; 1797 tcp_prot_override.release_cb = tcp_release_cb_override; 1798 1799 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1800 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops; 1801 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req; 1802 1803 subflow_v6_specific = ipv6_specific; 1804 subflow_v6_specific.conn_request = subflow_v6_conn_request; 1805 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock; 1806 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect; 1807 1808 subflow_v6m_specific = subflow_v6_specific; 1809 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit; 1810 subflow_v6m_specific.send_check = ipv4_specific.send_check; 1811 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len; 1812 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced; 1813 subflow_v6m_specific.net_frag_header_len = 0; 1814 1815 tcpv6_prot_override = tcpv6_prot; 1816 tcpv6_prot_override.release_cb = tcp_release_cb_override; 1817 #endif 1818 1819 mptcp_diag_subflow_init(&subflow_ulp_ops); 1820 1821 if (tcp_register_ulp(&subflow_ulp_ops) != 0) 1822 panic("MPTCP: failed to register subflows to ULP\n"); 1823 } 1824