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