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/sha2.h> 13 #include <crypto/utils.h> 14 #include <net/sock.h> 15 #include <net/inet_common.h> 16 #include <net/inet_hashtables.h> 17 #include <net/protocol.h> 18 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 19 #include <net/ip6_route.h> 20 #include <net/transp_v6.h> 21 #endif 22 #include <net/mptcp.h> 23 24 #include "protocol.h" 25 #include "mib.h" 26 27 #include <trace/events/mptcp.h> 28 #include <trace/events/sock.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\n", subflow_req); 43 44 if (subflow_req->msk) 45 sock_put((struct sock *)subflow_req->msk); 46 47 mptcp_token_destroy_request(req); 48 } 49 50 void mptcp_subflow_reqsk_clone(struct request_sock *req, 51 struct request_sock *new_req) 52 { 53 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 54 struct mptcp_subflow_request_sock *new_subflow_req; 55 struct mptcp_sock *msk; 56 57 new_subflow_req = mptcp_subflow_rsk(new_req); 58 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(), 59 "MPTCP reqsk clone called without RCU"); 60 61 /* A non-NULL ->msk means the request owns one reference. The clone 62 * copied only the pointer, while the original request can concurrently 63 * transfer its reference to the child. Acquire a reference for the 64 * clone, then verify that the original request still owns the same msk. 65 * MPTCP sockets use SLAB_TYPESAFE_BY_RCU and all clone callers run in 66 * an RCU read-side critical section, keeping the memory stable here. 67 */ 68 msk = READ_ONCE(subflow_req->msk); 69 if (msk) { 70 struct sock *sk = (struct sock *)msk; 71 72 if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt)) { 73 msk = NULL; 74 } else if (READ_ONCE(subflow_req->msk) != msk) { 75 sock_put(sk); 76 msk = NULL; 77 } 78 } 79 80 new_subflow_req->msk = msk; 81 82 mptcp_token_move_request(req, new_req); 83 } 84 85 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2, 86 void *hmac) 87 { 88 u8 msg[8]; 89 90 put_unaligned_be32(nonce1, &msg[0]); 91 put_unaligned_be32(nonce2, &msg[4]); 92 93 mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac); 94 } 95 96 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk) 97 { 98 return mptcp_is_fully_established((void *)msk) && 99 ((mptcp_pm_is_userspace(msk) && 100 mptcp_userspace_pm_active(msk)) || 101 READ_ONCE(msk->pm.accept_subflow)); 102 } 103 104 /* validate received token and create truncated hmac and nonce for SYN-ACK */ 105 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req) 106 { 107 struct mptcp_sock *msk = subflow_req->msk; 108 u8 hmac[SHA256_DIGEST_SIZE]; 109 110 subflow_req->local_nonce = get_random_u32(); 111 112 subflow_generate_hmac(READ_ONCE(msk->local_key), 113 READ_ONCE(msk->remote_key), 114 subflow_req->local_nonce, 115 subflow_req->remote_nonce, hmac); 116 117 subflow_req->thmac = get_unaligned_be64(hmac); 118 } 119 120 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req) 121 { 122 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 123 struct mptcp_sock *msk; 124 int local_id; 125 126 msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token); 127 if (!msk) { 128 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN); 129 return NULL; 130 } 131 132 local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req); 133 if (local_id < 0) { 134 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPJOINNOIDFOUND); 135 sock_put((struct sock *)msk); 136 return NULL; 137 } 138 subflow_req->local_id = local_id; 139 subflow_req->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)req); 140 141 return msk; 142 } 143 144 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener) 145 { 146 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 147 148 subflow_req->mp_capable = 0; 149 subflow_req->mp_join = 0; 150 subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener)); 151 subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener)); 152 subflow_req->msk = NULL; 153 mptcp_token_init_request(req); 154 } 155 156 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk) 157 { 158 return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport; 159 } 160 161 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason) 162 { 163 struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 164 165 if (mpext) { 166 memset(mpext, 0, sizeof(*mpext)); 167 mpext->reset_reason = reason; 168 } 169 } 170 171 static int subflow_reset_req_endp(struct request_sock *req, struct sk_buff *skb) 172 { 173 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEENDPATTEMPT); 174 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 175 return -EPERM; 176 } 177 178 /* Init mptcp request socket. 179 * 180 * Returns an error code if a JOIN has failed and a TCP reset 181 * should be sent. 182 */ 183 static int subflow_check_req(struct request_sock *req, 184 const struct sock *sk_listener, 185 struct sk_buff *skb) 186 { 187 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 188 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 189 struct mptcp_options_received mp_opt; 190 bool opt_mp_capable, opt_mp_join; 191 192 pr_debug("subflow_req=%p, listener=%p\n", subflow_req, listener); 193 194 #ifdef CONFIG_TCP_MD5SIG 195 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 196 * TCP option space. 197 */ 198 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) { 199 MPTCP_INC_STATS(sock_net(sk_listener), MPTCP_MIB_MD5SIGRESET); 200 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 201 return -EINVAL; 202 } 203 #endif 204 205 mptcp_get_options(skb, &mp_opt); 206 207 opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYN); 208 opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYN); 209 if (opt_mp_capable) { 210 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE); 211 212 if (unlikely(listener->pm_listener)) 213 return subflow_reset_req_endp(req, skb); 214 } else if (opt_mp_join) { 215 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX); 216 217 if (mp_opt.backup) 218 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNBACKUPRX); 219 } else if (unlikely(listener->pm_listener)) { 220 return subflow_reset_req_endp(req, skb); 221 } 222 223 if (opt_mp_capable && listener->request_mptcp) { 224 int err, retries = MPTCP_TOKEN_MAX_RETRIES; 225 226 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 227 again: 228 do { 229 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key)); 230 } while (subflow_req->local_key == 0); 231 232 if (unlikely(req->syncookie)) { 233 mptcp_crypto_key_sha(subflow_req->local_key, 234 &subflow_req->token, 235 &subflow_req->idsn); 236 if (mptcp_token_exists(subflow_req->token)) { 237 if (retries-- > 0) 238 goto again; 239 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 240 } else { 241 subflow_req->mp_capable = 1; 242 } 243 return 0; 244 } 245 246 err = mptcp_token_new_request(req); 247 if (err == 0) 248 subflow_req->mp_capable = 1; 249 else if (retries-- > 0) 250 goto again; 251 else 252 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT); 253 254 } else if (opt_mp_join && listener->request_mptcp) { 255 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq; 256 subflow_req->mp_join = 1; 257 subflow_req->backup = mp_opt.backup; 258 subflow_req->remote_id = mp_opt.join_id; 259 subflow_req->token = mp_opt.token; 260 subflow_req->remote_nonce = mp_opt.nonce; 261 subflow_req->msk = subflow_token_join_request(req); 262 263 /* Can't fall back to TCP in this case. */ 264 if (!subflow_req->msk) { 265 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 266 return -EPERM; 267 } 268 269 if (subflow_use_different_sport(subflow_req->msk, sk_listener)) { 270 pr_debug("syn inet_sport=%d %d\n", 271 ntohs(inet_sk(sk_listener)->inet_sport), 272 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport)); 273 if (!mptcp_pm_announced_has_ssk(subflow_req->msk, sk_listener)) { 274 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX); 275 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 276 return -EPERM; 277 } 278 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX); 279 } 280 281 subflow_req_create_thmac(subflow_req); 282 283 if (unlikely(req->syncookie)) { 284 if (!mptcp_can_accept_new_subflow(subflow_req->msk)) { 285 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED); 286 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 287 return -EPERM; 288 } 289 290 subflow_init_req_cookie_join_save(subflow_req, skb); 291 } 292 293 pr_debug("token=%u, remote_nonce=%u msk=%p\n", subflow_req->token, 294 subflow_req->remote_nonce, subflow_req->msk); 295 } 296 297 return 0; 298 } 299 300 int mptcp_subflow_init_cookie_req(struct request_sock *req, 301 const struct sock *sk_listener, 302 struct sk_buff *skb) 303 { 304 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener); 305 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 306 struct mptcp_options_received mp_opt; 307 bool opt_mp_capable, opt_mp_join; 308 int err; 309 310 subflow_init_req(req, sk_listener); 311 mptcp_get_options(skb, &mp_opt); 312 313 opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK); 314 opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK); 315 if (opt_mp_capable && listener->request_mptcp) { 316 if (mp_opt.sndr_key == 0) 317 return -EINVAL; 318 319 subflow_req->local_key = mp_opt.rcvr_key; 320 err = mptcp_token_new_request(req); 321 if (err) 322 return err; 323 324 subflow_req->mp_capable = 1; 325 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 326 } else if (opt_mp_join && listener->request_mptcp) { 327 if (!mptcp_token_join_cookie_init_state(subflow_req, skb)) 328 return -EINVAL; 329 330 subflow_req->mp_join = 1; 331 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1; 332 } 333 334 return 0; 335 } 336 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req); 337 338 static enum sk_rst_reason mptcp_get_rst_reason(const struct sk_buff *skb) 339 { 340 const struct mptcp_ext *mpext = mptcp_get_ext(skb); 341 342 if (!mpext) 343 return SK_RST_REASON_NOT_SPECIFIED; 344 345 return sk_rst_convert_mptcp_reason(mpext->reset_reason); 346 } 347 348 static struct dst_entry *subflow_v4_route_req(const struct sock *sk, 349 struct sk_buff *skb, 350 struct flowi *fl, 351 struct request_sock *req, 352 u32 tw_isn) 353 { 354 struct dst_entry *dst; 355 int err; 356 357 tcp_rsk(req)->is_mptcp = 1; 358 subflow_init_req(req, sk); 359 360 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req, tw_isn); 361 if (!dst) 362 return NULL; 363 364 err = subflow_check_req(req, sk, skb); 365 if (err == 0) 366 return dst; 367 368 dst_release(dst); 369 if (!req->syncookie) 370 tcp_request_sock_ops.send_reset(sk, skb, 371 mptcp_get_rst_reason(skb)); 372 return NULL; 373 } 374 375 static void subflow_prep_synack(const struct sock *sk, struct request_sock *req, 376 struct tcp_fastopen_cookie *foc, 377 enum tcp_synack_type synack_type) 378 { 379 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 380 struct inet_request_sock *ireq = inet_rsk(req); 381 382 /* MP_JOIN cannot use TFO, do not send a cookie in the SYN/ACK */ 383 if (foc && mptcp_subflow_rsk(req)->mp_join) 384 foc->len = -1; 385 386 /* clear tstamp_ok, as needed depending on cookie */ 387 if (foc && foc->len > -1) 388 ireq->tstamp_ok = 0; 389 390 if (synack_type == TCP_SYNACK_FASTOPEN) 391 mptcp_fastopen_subflow_synack_set_params(subflow, req); 392 } 393 394 static int subflow_v4_send_synack(const struct sock *sk, struct dst_entry *dst, 395 struct flowi *fl, 396 struct request_sock *req, 397 struct tcp_fastopen_cookie *foc, 398 enum tcp_synack_type synack_type, 399 struct sk_buff *syn_skb) 400 { 401 subflow_prep_synack(sk, req, foc, synack_type); 402 403 return tcp_request_sock_ipv4_ops.send_synack(sk, dst, fl, req, foc, 404 synack_type, syn_skb); 405 } 406 407 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 408 static int subflow_v6_send_synack(const struct sock *sk, struct dst_entry *dst, 409 struct flowi *fl, 410 struct request_sock *req, 411 struct tcp_fastopen_cookie *foc, 412 enum tcp_synack_type synack_type, 413 struct sk_buff *syn_skb) 414 { 415 subflow_prep_synack(sk, req, foc, synack_type); 416 417 return tcp_request_sock_ipv6_ops.send_synack(sk, dst, fl, req, foc, 418 synack_type, syn_skb); 419 } 420 421 static struct dst_entry *subflow_v6_route_req(const struct sock *sk, 422 struct sk_buff *skb, 423 struct flowi *fl, 424 struct request_sock *req, 425 u32 tw_isn) 426 { 427 struct dst_entry *dst; 428 int err; 429 430 tcp_rsk(req)->is_mptcp = 1; 431 subflow_init_req(req, sk); 432 433 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req, tw_isn); 434 if (!dst) 435 return NULL; 436 437 err = subflow_check_req(req, sk, skb); 438 if (err == 0) 439 return dst; 440 441 dst_release(dst); 442 if (!req->syncookie) 443 tcp6_request_sock_ops.send_reset(sk, skb, 444 mptcp_get_rst_reason(skb)); 445 return NULL; 446 } 447 #endif 448 449 /* validate received truncated hmac and create hmac for third ACK */ 450 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow, 451 u64 thmac) 452 { 453 u8 hmac[SHA256_DIGEST_SIZE]; 454 u64 expected_thmac; 455 456 subflow_generate_hmac(subflow->remote_key, subflow->local_key, 457 subflow->remote_nonce, subflow->local_nonce, 458 hmac); 459 460 expected_thmac = get_unaligned_be64(hmac); 461 pr_debug("subflow=%p, token=%u, expected_thmac=%llu, thmac=%llu\n", 462 subflow, subflow->token, expected_thmac, thmac); 463 464 return expected_thmac == thmac; 465 } 466 467 void mptcp_subflow_reset(struct sock *ssk) 468 { 469 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 470 struct sock *sk = subflow->conn; 471 472 /* mptcp_mp_fail_no_response() can reach here on an already closed 473 * socket 474 */ 475 if (ssk->sk_state == TCP_CLOSE) 476 return; 477 478 /* must hold: tcp_done() could drop last reference on parent */ 479 sock_hold(sk); 480 481 subflow->resetting = 1; 482 483 /* No need to delay the actual close for to-be discarded data. */ 484 __skb_queue_purge(&ssk->sk_receive_queue); 485 mptcp_send_active_reset_reason(ssk); 486 tcp_done(ssk); 487 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags)) 488 mptcp_schedule_work(sk); 489 490 sock_put(sk); 491 } 492 493 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk) 494 { 495 return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport; 496 } 497 498 void __mptcp_sync_state(struct sock *sk, int state) 499 { 500 struct mptcp_subflow_context *subflow; 501 struct mptcp_sock *msk = mptcp_sk(sk); 502 struct sock *ssk = msk->first; 503 504 subflow = mptcp_subflow_ctx(ssk); 505 __mptcp_propagate_sndbuf(sk, ssk); 506 507 if (sk->sk_state == TCP_SYN_SENT) { 508 /* subflow->idsn is always available is TCP_SYN_SENT state, 509 * even for the FASTOPEN scenarios 510 */ 511 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 512 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 513 mptcp_set_state(sk, state); 514 sk->sk_state_change(sk); 515 } 516 } 517 518 static void subflow_set_remote_key(struct mptcp_sock *msk, 519 struct mptcp_subflow_context *subflow, 520 const struct mptcp_options_received *mp_opt) 521 { 522 /* active MPC subflow will reach here multiple times: 523 * at subflow_finish_connect() time and at 4th ack time 524 */ 525 if (subflow->remote_key_valid) 526 return; 527 528 subflow->remote_key_valid = 1; 529 subflow->remote_key = mp_opt->sndr_key; 530 mptcp_crypto_key_sha(subflow->remote_key, NULL, &subflow->iasn); 531 subflow->iasn++; 532 533 /* for fallback's sake */ 534 subflow->map_seq = subflow->iasn; 535 536 WRITE_ONCE(msk->remote_key, subflow->remote_key); 537 WRITE_ONCE(msk->ack_seq, subflow->iasn); 538 WRITE_ONCE(msk->can_ack, true); 539 atomic64_set(&msk->rcv_wnd_sent, subflow->iasn); 540 } 541 542 static void mptcp_propagate_state(struct sock *sk, struct sock *ssk, 543 struct mptcp_subflow_context *subflow, 544 const struct mptcp_options_received *mp_opt) 545 { 546 struct mptcp_sock *msk = mptcp_sk(sk); 547 548 mptcp_data_lock(sk); 549 if (mp_opt) { 550 /* Options are available only in the non fallback cases 551 * avoid updating rx path fields otherwise 552 */ 553 WRITE_ONCE(msk->snd_una, subflow->idsn + 1); 554 WRITE_ONCE(msk->wnd_end, subflow->idsn + 1 + tcp_sk(ssk)->snd_wnd); 555 subflow_set_remote_key(msk, subflow, mp_opt); 556 } 557 558 if (!sock_owned_by_user(sk)) { 559 __mptcp_sync_state(sk, ssk->sk_state); 560 } else { 561 msk->pending_state = ssk->sk_state; 562 __set_bit(MPTCP_SYNC_STATE, &msk->cb_flags); 563 } 564 mptcp_data_unlock(sk); 565 } 566 567 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) 568 { 569 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 570 struct mptcp_options_received mp_opt; 571 struct sock *parent = subflow->conn; 572 struct mptcp_sock *msk; 573 574 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb); 575 576 /* be sure no special action on any packet other than syn-ack */ 577 if (subflow->conn_finished) 578 return; 579 580 msk = mptcp_sk(parent); 581 subflow->rel_write_seq = 1; 582 subflow->conn_finished = 1; 583 subflow->ssn_offset = TCP_SKB_CB(skb)->seq; 584 pr_debug("subflow=%p synack seq=%x\n", subflow, subflow->ssn_offset); 585 586 mptcp_get_options(skb, &mp_opt); 587 if (subflow->request_mptcp) { 588 if (!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYNACK)) { 589 if (!mptcp_try_fallback(sk, 590 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK)) { 591 MPTCP_INC_STATS(sock_net(sk), 592 MPTCP_MIB_FALLBACKFAILED); 593 goto do_reset; 594 } 595 596 goto fallback; 597 } 598 599 if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD) 600 WRITE_ONCE(msk->csum_enabled, true); 601 if (mp_opt.deny_join_id0) 602 WRITE_ONCE(msk->pm.remote_deny_join_id0, true); 603 subflow->mp_capable = 1; 604 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK); 605 mptcp_finish_connect(sk); 606 mptcp_active_enable(parent); 607 mptcp_propagate_state(parent, sk, subflow, &mp_opt); 608 } else if (subflow->request_join) { 609 u8 hmac[SHA256_DIGEST_SIZE]; 610 611 if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYNACK)) { 612 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPJOINSYNACKNOMPJOIN); 613 subflow->reset_reason = MPTCP_RST_EMPTCP; 614 goto do_reset; 615 } 616 617 subflow->backup = mp_opt.backup; 618 subflow->remote_nonce = mp_opt.nonce; 619 WRITE_ONCE(subflow->remote_id, mp_opt.join_id); 620 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d\n", 621 subflow, mp_opt.thmac, subflow->remote_nonce, 622 subflow->backup); 623 624 if (!subflow_thmac_valid(subflow, mp_opt.thmac)) { 625 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKMAC); 626 subflow->reset_reason = MPTCP_RST_EMPTCP; 627 goto do_reset; 628 } 629 630 if (!mptcp_finish_join(sk)) 631 goto do_reset; 632 633 subflow_generate_hmac(subflow->local_key, subflow->remote_key, 634 subflow->local_nonce, 635 subflow->remote_nonce, 636 hmac); 637 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN); 638 639 subflow->mp_join = 1; 640 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX); 641 642 if (subflow->backup) 643 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKBACKUPRX); 644 645 if (subflow_use_different_dport(msk, sk)) { 646 pr_debug("synack inet_dport=%d %d\n", 647 ntohs(inet_sk(sk)->inet_dport), 648 ntohs(inet_sk(parent)->inet_dport)); 649 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX); 650 } 651 } else if (mptcp_check_fallback(sk)) { 652 /* It looks like MPTCP is blocked, while TCP is not */ 653 if (subflow->mpc_drop) 654 mptcp_active_disable(parent); 655 fallback: 656 mptcp_propagate_state(parent, sk, subflow, NULL); 657 } 658 return; 659 660 do_reset: 661 subflow->reset_transient = 0; 662 mptcp_subflow_reset(sk); 663 } 664 665 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id) 666 { 667 WARN_ON_ONCE(local_id < 0 || local_id > 255); 668 WRITE_ONCE(subflow->local_id, local_id); 669 } 670 671 static int subflow_chk_local_id(struct sock *sk) 672 { 673 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 674 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 675 int err; 676 677 if (likely(subflow->local_id >= 0)) 678 return 0; 679 680 err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk); 681 if (err < 0) 682 return err; 683 684 subflow_set_local_id(subflow, err); 685 subflow->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)sk); 686 687 return 0; 688 } 689 690 static int subflow_rebuild_header(struct sock *sk) 691 { 692 int err = subflow_chk_local_id(sk); 693 694 if (unlikely(err < 0)) 695 return err; 696 697 return inet_sk_rebuild_header(sk); 698 } 699 700 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 701 static int subflow_v6_rebuild_header(struct sock *sk) 702 { 703 int err = subflow_chk_local_id(sk); 704 705 if (unlikely(err < 0)) 706 return err; 707 708 return inet6_sk_rebuild_header(sk); 709 } 710 #endif 711 712 static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init; 713 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init; 714 715 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb) 716 { 717 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 718 719 pr_debug("subflow=%p\n", subflow); 720 721 /* Never answer to SYNs sent to broadcast or multicast */ 722 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 723 goto drop; 724 725 return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops, 726 &subflow_request_sock_ipv4_ops, 727 sk, skb); 728 drop: 729 tcp_listendrop(sk); 730 return 0; 731 } 732 733 static void subflow_v4_req_destructor(struct request_sock *req) 734 { 735 subflow_req_destructor(req); 736 tcp_request_sock_ops.destructor(req); 737 } 738 739 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 740 static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init; 741 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init; 742 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init; 743 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init; 744 static struct proto tcpv6_prot_override __ro_after_init; 745 746 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb) 747 { 748 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 749 750 pr_debug("subflow=%p\n", subflow); 751 752 if (skb->protocol == htons(ETH_P_IP)) 753 return subflow_v4_conn_request(sk, skb); 754 755 if (!ipv6_unicast_destination(skb)) 756 goto drop; 757 758 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) { 759 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS); 760 return 0; 761 } 762 763 return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops, 764 &subflow_request_sock_ipv6_ops, sk, skb); 765 766 drop: 767 tcp_listendrop(sk); 768 return 0; /* don't send reset */ 769 } 770 771 static void subflow_v6_req_destructor(struct request_sock *req) 772 { 773 subflow_req_destructor(req); 774 tcp6_request_sock_ops.destructor(req); 775 } 776 #endif 777 778 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops, 779 struct sock *sk_listener, 780 bool attach_listener) 781 { 782 if (ops->family == AF_INET) 783 ops = &mptcp_subflow_v4_request_sock_ops; 784 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 785 else if (ops->family == AF_INET6) 786 ops = &mptcp_subflow_v6_request_sock_ops; 787 #endif 788 789 return inet_reqsk_alloc(ops, sk_listener, attach_listener); 790 } 791 EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc); 792 793 /* validate hmac received in third ACK */ 794 static bool subflow_hmac_valid(const struct mptcp_subflow_request_sock *subflow_req, 795 const struct mptcp_options_received *mp_opt) 796 { 797 struct mptcp_sock *msk = subflow_req->msk; 798 u8 hmac[SHA256_DIGEST_SIZE]; 799 800 subflow_generate_hmac(READ_ONCE(msk->remote_key), 801 READ_ONCE(msk->local_key), 802 subflow_req->remote_nonce, 803 subflow_req->local_nonce, hmac); 804 805 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN); 806 } 807 808 static void subflow_ulp_fallback(struct sock *sk, 809 struct mptcp_subflow_context *old_ctx) 810 { 811 struct inet_connection_sock *icsk = inet_csk(sk); 812 813 mptcp_subflow_tcp_fallback(sk, old_ctx); 814 icsk->icsk_ulp_ops = NULL; 815 rcu_assign_pointer(icsk->icsk_ulp_data, NULL); 816 tcp_sk(sk)->is_mptcp = 0; 817 818 mptcp_subflow_ops_undo_override(sk); 819 } 820 821 void mptcp_subflow_drop_ctx(struct sock *ssk) 822 { 823 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 824 825 if (!ctx) 826 return; 827 828 list_del(&mptcp_subflow_ctx(ssk)->node); 829 if (inet_csk(ssk)->icsk_ulp_ops) { 830 subflow_ulp_fallback(ssk, ctx); 831 if (ctx->conn) 832 sock_put(ctx->conn); 833 } 834 835 kfree_rcu(ctx, rcu); 836 } 837 838 void __mptcp_subflow_fully_established(struct mptcp_sock *msk, 839 struct mptcp_subflow_context *subflow, 840 const struct mptcp_options_received *mp_opt) 841 { 842 subflow_set_remote_key(msk, subflow, mp_opt); 843 WRITE_ONCE(subflow->fully_established, true); 844 WRITE_ONCE(msk->fully_established, true); 845 } 846 847 static struct sock *subflow_syn_recv_sock(const struct sock *sk, 848 struct sk_buff *skb, 849 struct request_sock *req, 850 struct dst_entry *dst, 851 struct request_sock *req_unhash, 852 bool *own_req, 853 void (*opt_child_init)(struct sock *newsk, 854 const struct sock *sk)) 855 { 856 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk); 857 struct mptcp_subflow_request_sock *subflow_req; 858 struct mptcp_options_received mp_opt; 859 bool fallback, fallback_is_fatal; 860 enum sk_rst_reason reason; 861 struct mptcp_sock *owner; 862 struct sock *child; 863 864 pr_debug("listener=%p, req=%p, conn=%p\n", listener, req, listener->conn); 865 866 /* After child creation we must look for MPC even when options 867 * are not parsed 868 */ 869 mp_opt.suboptions = 0; 870 871 /* hopefully temporary handling for MP_JOIN+syncookie */ 872 subflow_req = mptcp_subflow_rsk(req); 873 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join; 874 fallback = !tcp_rsk(req)->is_mptcp; 875 if (fallback) 876 goto create_child; 877 878 /* if the sk is MP_CAPABLE, we try to fetch the client key */ 879 if (subflow_req->mp_capable) { 880 /* we can receive and accept an in-window, out-of-order pkt, 881 * which may not carry the MP_CAPABLE opt even on mptcp enabled 882 * paths: always try to extract the peer key, and fallback 883 * for packets missing it. 884 * Even OoO DSS packets coming legitly after dropped or 885 * reordered MPC will cause fallback, but we don't have other 886 * options. 887 */ 888 mptcp_get_options(skb, &mp_opt); 889 if (!(mp_opt.suboptions & 890 (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_ACK))) 891 fallback = true; 892 893 } else if (subflow_req->mp_join) { 894 /* a SYN skb here comes from TFO, which MP_JOIN cannot use: just 895 * fall back to the regular path. 896 */ 897 if ((TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_ACK)) == 898 TCPHDR_SYN) 899 return NULL; 900 901 mptcp_get_options(skb, &mp_opt); 902 if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK)) 903 fallback = true; 904 } 905 906 create_child: 907 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst, 908 req_unhash, own_req, opt_child_init); 909 910 if (child && *own_req) { 911 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child); 912 913 tcp_rsk(req)->drop_req = false; 914 915 /* we need to fallback on ctx allocation failure and on pre-reqs 916 * checking above. In the latter scenario we additionally need 917 * to reset the context to non MPTCP status. 918 */ 919 if (!ctx || fallback) { 920 if (fallback_is_fatal) { 921 if (!ctx) 922 MPTCP_INC_STATS(sock_net(sk), 923 MPTCP_MIB_MPJOINACKNOCTX); 924 else 925 MPTCP_INC_STATS(sock_net(sk), 926 MPTCP_MIB_MPJOINACKNOMPJOIN); 927 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 928 goto dispose_child; 929 } 930 goto fallback; 931 } 932 933 /* ssk inherits options of listener sk */ 934 ctx->setsockopt_seq = listener->setsockopt_seq; 935 936 if (ctx->mp_capable) { 937 ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req); 938 if (!ctx->conn) { 939 fallback = true; 940 goto fallback; 941 } 942 943 ctx->subflow_id = 1; 944 owner = mptcp_sk(ctx->conn); 945 946 if (mp_opt.deny_join_id0) 947 WRITE_ONCE(owner->pm.remote_deny_join_id0, true); 948 949 mptcp_pm_new_connection(owner, child, 1); 950 951 /* with OoO packets we can reach here without ingress 952 * mpc option 953 */ 954 if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK) { 955 mptcp_pm_fully_established(owner, child); 956 ctx->pm_notified = 1; 957 } 958 } else if (ctx->mp_join) { 959 owner = subflow_req->msk; 960 if (!owner) { 961 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 962 goto dispose_child; 963 } 964 965 if (!subflow_hmac_valid(subflow_req, &mp_opt)) { 966 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC); 967 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP); 968 goto dispose_child; 969 } 970 971 if (!mptcp_can_accept_new_subflow(owner)) { 972 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED); 973 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 974 goto dispose_child; 975 } 976 977 /* move the msk reference ownership to the subflow */ 978 WRITE_ONCE(subflow_req->msk, NULL); 979 ctx->conn = (struct sock *)owner; 980 981 if (subflow_use_different_sport(owner, sk)) { 982 pr_debug("ack inet_sport=%d %d\n", 983 ntohs(inet_sk(sk)->inet_sport), 984 ntohs(inet_sk((struct sock *)owner)->inet_sport)); 985 if (!mptcp_pm_announced_has_ssk(owner, sk)) { 986 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX); 987 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT); 988 goto dispose_child; 989 } 990 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX); 991 } 992 993 if (!mptcp_finish_join(child)) { 994 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(child); 995 996 subflow_add_reset_reason(skb, subflow->reset_reason); 997 goto dispose_child; 998 } 999 1000 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX); 1001 tcp_rsk(req)->drop_req = true; 1002 } 1003 } 1004 1005 /* check for expected invariant - should never trigger, just help 1006 * catching earlier subtle bugs 1007 */ 1008 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp && 1009 (!mptcp_subflow_ctx(child) || 1010 !mptcp_subflow_ctx(child)->conn)); 1011 return child; 1012 1013 dispose_child: 1014 mptcp_subflow_drop_ctx(child); 1015 tcp_rsk(req)->drop_req = true; 1016 inet_csk_prepare_for_destroy_sock(child); 1017 tcp_done(child); 1018 reason = mptcp_get_rst_reason(skb); 1019 req->rsk_ops->send_reset(sk, skb, reason); 1020 1021 /* The last child reference will be released by the caller */ 1022 return child; 1023 1024 fallback: 1025 if (fallback) 1026 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 1027 mptcp_subflow_drop_ctx(child); 1028 return child; 1029 } 1030 1031 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init; 1032 static struct proto tcp_prot_override __ro_after_init; 1033 1034 enum mapping_status { 1035 MAPPING_OK, 1036 MAPPING_INVALID, 1037 MAPPING_EMPTY, 1038 MAPPING_DATA_FIN, 1039 MAPPING_DUMMY, 1040 MAPPING_BAD_CSUM, 1041 MAPPING_NODSS 1042 }; 1043 1044 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn) 1045 { 1046 pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d\n", 1047 ssn, subflow->map_subflow_seq, subflow->map_data_len); 1048 } 1049 1050 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb) 1051 { 1052 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1053 unsigned int skb_consumed; 1054 1055 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq; 1056 if (unlikely(skb_consumed >= skb->len)) { 1057 DEBUG_NET_WARN_ON_ONCE(1); 1058 return true; 1059 } 1060 1061 return skb->len - skb_consumed <= subflow->map_data_len - 1062 mptcp_subflow_get_map_offset(subflow); 1063 } 1064 1065 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb) 1066 { 1067 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1068 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1069 1070 if (unlikely(before(ssn, subflow->map_subflow_seq))) { 1071 /* Mapping covers data later in the subflow stream, 1072 * currently unsupported. 1073 */ 1074 dbg_bad_map(subflow, ssn); 1075 return false; 1076 } 1077 if (unlikely(!before(ssn, subflow->map_subflow_seq + 1078 subflow->map_data_len))) { 1079 /* Mapping does covers past subflow data, invalid */ 1080 dbg_bad_map(subflow, ssn); 1081 return false; 1082 } 1083 return true; 1084 } 1085 1086 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb, 1087 bool csum_reqd) 1088 { 1089 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1090 u32 offset, seq, delta; 1091 __sum16 csum; 1092 int len; 1093 1094 if (!csum_reqd) 1095 return MAPPING_OK; 1096 1097 /* mapping already validated on previous traversal */ 1098 if (subflow->map_csum_len == subflow->map_data_len) 1099 return MAPPING_OK; 1100 1101 /* traverse the receive queue, ensuring it contains a full 1102 * DSS mapping and accumulating the related csum. 1103 * Preserve the accoumlate csum across multiple calls, to compute 1104 * the csum only once 1105 */ 1106 delta = subflow->map_data_len - subflow->map_csum_len; 1107 for (;;) { 1108 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len; 1109 offset = seq - TCP_SKB_CB(skb)->seq; 1110 1111 /* if the current skb has not been accounted yet, csum its contents 1112 * up to the amount covered by the current DSS 1113 */ 1114 if (offset < skb->len) { 1115 __wsum csum; 1116 1117 len = min(skb->len - offset, delta); 1118 csum = skb_checksum(skb, offset, len, 0); 1119 subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum, 1120 subflow->map_csum_len); 1121 1122 delta -= len; 1123 subflow->map_csum_len += len; 1124 } 1125 if (delta == 0) 1126 break; 1127 1128 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) { 1129 /* if this subflow is closed, the partial mapping 1130 * will be never completed; flush the pending skbs, so 1131 * that subflow_sched_work_if_closed() can kick in 1132 */ 1133 if (unlikely(ssk->sk_state == TCP_CLOSE)) 1134 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1135 sk_eat_skb(ssk, skb); 1136 1137 /* not enough data to validate the csum */ 1138 return MAPPING_EMPTY; 1139 } 1140 1141 /* the DSS mapping for next skbs will be validated later, 1142 * when a get_mapping_status call will process such skb 1143 */ 1144 skb = skb->next; 1145 } 1146 1147 /* note that 'map_data_len' accounts only for the carried data, does 1148 * not include the eventual seq increment due to the data fin, 1149 * while the pseudo header requires the original DSS data len, 1150 * including that 1151 */ 1152 csum = __mptcp_make_csum(subflow->map_seq, 1153 subflow->map_subflow_seq, 1154 subflow->map_data_len + subflow->map_data_fin, 1155 subflow->map_data_csum); 1156 if (unlikely(csum)) { 1157 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR); 1158 return MAPPING_BAD_CSUM; 1159 } 1160 1161 subflow->valid_csum_seen = 1; 1162 return MAPPING_OK; 1163 } 1164 1165 static enum mapping_status get_mapping_status(struct sock *ssk, 1166 struct mptcp_sock *msk) 1167 { 1168 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1169 bool csum_reqd = READ_ONCE(msk->csum_enabled); 1170 struct mptcp_ext *mpext; 1171 struct sk_buff *skb; 1172 u16 data_len; 1173 u64 map_seq; 1174 1175 skb = skb_peek(&ssk->sk_receive_queue); 1176 if (!skb) 1177 return MAPPING_EMPTY; 1178 1179 if (mptcp_check_fallback(ssk)) 1180 return MAPPING_DUMMY; 1181 1182 mpext = mptcp_get_ext(skb); 1183 if (!mpext || !mpext->use_map) { 1184 if (!subflow->map_valid && !skb->len) { 1185 /* the TCP stack deliver 0 len FIN pkt to the receive 1186 * queue, that is the only 0len pkts ever expected here, 1187 * and we can admit no mapping only for 0 len pkts 1188 */ 1189 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 1190 WARN_ONCE(1, "0len seq %d:%d flags %x", 1191 TCP_SKB_CB(skb)->seq, 1192 TCP_SKB_CB(skb)->end_seq, 1193 TCP_SKB_CB(skb)->tcp_flags); 1194 sk_eat_skb(ssk, skb); 1195 return MAPPING_EMPTY; 1196 } 1197 1198 /* If the required DSS has likely been dropped by a middlebox */ 1199 if (!subflow->map_valid) 1200 return MAPPING_NODSS; 1201 1202 goto validate_seq; 1203 } 1204 1205 trace_get_mapping_status(mpext); 1206 1207 data_len = mpext->data_len; 1208 if (data_len == 0) { 1209 pr_debug("infinite mapping received\n"); 1210 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX); 1211 return MAPPING_INVALID; 1212 } 1213 1214 if (mpext->data_fin == 1) { 1215 u64 data_fin_seq; 1216 1217 if (data_len == 1) { 1218 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq, 1219 mpext->dsn64); 1220 pr_debug("DATA_FIN with no payload seq=%llu\n", mpext->data_seq); 1221 if (subflow->map_valid) { 1222 /* A DATA_FIN might arrive in a DSS 1223 * option before the previous mapping 1224 * has been fully consumed. Continue 1225 * handling the existing mapping. 1226 */ 1227 skb_ext_del(skb, SKB_EXT_MPTCP); 1228 return MAPPING_OK; 1229 } 1230 1231 if (updated) 1232 mptcp_schedule_work((struct sock *)msk); 1233 1234 return MAPPING_DATA_FIN; 1235 } 1236 1237 data_fin_seq = mpext->data_seq + data_len - 1; 1238 1239 /* If mpext->data_seq is a 32-bit value, data_fin_seq must also 1240 * be limited to 32 bits. 1241 */ 1242 if (!mpext->dsn64) 1243 data_fin_seq &= GENMASK_ULL(31, 0); 1244 1245 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64); 1246 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d\n", 1247 data_fin_seq, mpext->dsn64); 1248 1249 /* Adjust for DATA_FIN using 1 byte of sequence space */ 1250 data_len--; 1251 } 1252 1253 map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64); 1254 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64); 1255 1256 if (subflow->map_valid) { 1257 /* Allow replacing only with an identical map */ 1258 if (subflow->map_seq == map_seq && 1259 subflow->map_subflow_seq == mpext->subflow_seq && 1260 subflow->map_data_len == data_len && 1261 subflow->map_csum_reqd == mpext->csum_reqd) { 1262 skb_ext_del(skb, SKB_EXT_MPTCP); 1263 goto validate_csum; 1264 } 1265 1266 /* If this skb data are fully covered by the current mapping, 1267 * the new map would need caching, which is not supported 1268 */ 1269 if (skb_is_fully_mapped(ssk, skb)) { 1270 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH); 1271 return MAPPING_INVALID; 1272 } 1273 1274 /* will validate the next map after consuming the current one */ 1275 goto validate_csum; 1276 } 1277 1278 subflow->map_seq = map_seq; 1279 subflow->map_subflow_seq = mpext->subflow_seq; 1280 subflow->map_data_len = data_len; 1281 subflow->map_valid = 1; 1282 subflow->map_data_fin = mpext->data_fin; 1283 subflow->mpc_map = mpext->mpc_map; 1284 subflow->map_csum_reqd = mpext->csum_reqd; 1285 subflow->map_csum_len = 0; 1286 subflow->map_data_csum = csum_unfold(mpext->csum); 1287 1288 /* Cfr RFC 8684 Section 3.3.0 */ 1289 if (unlikely(subflow->map_csum_reqd != csum_reqd)) 1290 return MAPPING_INVALID; 1291 1292 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n", 1293 subflow->map_seq, subflow->map_subflow_seq, 1294 subflow->map_data_len, subflow->map_csum_reqd, 1295 subflow->map_data_csum); 1296 1297 validate_seq: 1298 /* we revalidate valid mapping on new skb, because we must ensure 1299 * the current skb is completely covered by the available mapping 1300 */ 1301 if (!validate_mapping(ssk, skb)) { 1302 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH); 1303 return MAPPING_INVALID; 1304 } 1305 1306 skb_ext_del(skb, SKB_EXT_MPTCP); 1307 1308 validate_csum: 1309 return validate_data_csum(ssk, skb, csum_reqd); 1310 } 1311 1312 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb, 1313 u64 limit) 1314 { 1315 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1316 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 1317 struct tcp_sock *tp = tcp_sk(ssk); 1318 u32 offset, incr, avail_len; 1319 1320 offset = tp->copied_seq - TCP_SKB_CB(skb)->seq; 1321 if (WARN_ON_ONCE(offset > skb->len)) 1322 goto out; 1323 1324 avail_len = skb->len - offset; 1325 incr = limit >= avail_len ? avail_len + fin : limit; 1326 1327 pr_debug("discarding=%d len=%d offset=%d seq=%d\n", incr, skb->len, 1328 offset, subflow->map_subflow_seq); 1329 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA); 1330 tcp_sk(ssk)->copied_seq += incr; 1331 1332 out: 1333 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq)) 1334 sk_eat_skb(ssk, skb); 1335 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) 1336 subflow->map_valid = 0; 1337 } 1338 1339 static bool subflow_is_done(const struct sock *sk) 1340 { 1341 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE; 1342 } 1343 1344 /* sched mptcp worker for subflow cleanup if no more data is pending */ 1345 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk) 1346 { 1347 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1348 struct sock *sk = (struct sock *)msk; 1349 1350 if (likely(ssk->sk_state != TCP_CLOSE && 1351 (ssk->sk_state != TCP_CLOSE_WAIT || 1352 inet_sk_state_load(sk) != TCP_ESTABLISHED))) 1353 return; 1354 1355 if (!skb_queue_empty(&ssk->sk_receive_queue)) 1356 return; 1357 1358 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 1359 mptcp_schedule_work(sk); 1360 1361 /* when the fallback subflow closes the rx side, trigger a 'dummy' 1362 * ingress data fin, so that the msk state will follow along 1363 */ 1364 if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) && 1365 msk->first == ssk && 1366 mptcp_update_rcv_data_fin(msk, subflow->map_seq + 1367 subflow->map_data_len, true)) 1368 mptcp_schedule_work(sk); 1369 } 1370 1371 static bool mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk) 1372 { 1373 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1374 unsigned long fail_tout; 1375 1376 /* we are really failing, prevent any later subflow join */ 1377 spin_lock_bh(&msk->fallback_lock); 1378 if (!msk->allow_infinite_fallback) { 1379 spin_unlock_bh(&msk->fallback_lock); 1380 return false; 1381 } 1382 msk->allow_subflows = false; 1383 spin_unlock_bh(&msk->fallback_lock); 1384 1385 /* graceful failure can happen only on the MPC subflow */ 1386 if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first))) 1387 return false; 1388 1389 /* since the close timeout take precedence on the fail one, 1390 * no need to start the latter when the first is already set 1391 */ 1392 if (sock_flag((struct sock *)msk, SOCK_DEAD)) 1393 return true; 1394 1395 /* we don't need extreme accuracy here, use a zero fail_tout as special 1396 * value meaning no fail timeout at all; 1397 */ 1398 fail_tout = jiffies + TCP_RTO_MAX; 1399 if (!fail_tout) 1400 fail_tout = 1; 1401 WRITE_ONCE(subflow->fail_tout, fail_tout); 1402 tcp_send_ack(ssk); 1403 1404 mptcp_reset_tout_timer(msk, subflow->fail_tout); 1405 return true; 1406 } 1407 1408 static bool subflow_check_data_avail(struct sock *ssk) 1409 { 1410 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1411 enum mapping_status status; 1412 struct mptcp_sock *msk; 1413 struct sk_buff *skb; 1414 1415 if (!skb_peek(&ssk->sk_receive_queue)) 1416 WRITE_ONCE(subflow->data_avail, false); 1417 if (subflow->data_avail) 1418 return true; 1419 1420 msk = mptcp_sk(subflow->conn); 1421 for (;;) { 1422 u64 ack_seq; 1423 u64 old_ack; 1424 1425 status = get_mapping_status(ssk, msk); 1426 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue)); 1427 if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY || 1428 status == MAPPING_BAD_CSUM || status == MAPPING_NODSS)) 1429 goto fallback; 1430 1431 if (status != MAPPING_OK) 1432 goto no_data; 1433 1434 skb = skb_peek(&ssk->sk_receive_queue); 1435 if (WARN_ON_ONCE(!skb)) 1436 goto no_data; 1437 1438 if (unlikely(!READ_ONCE(msk->can_ack))) 1439 goto fallback; 1440 1441 old_ack = READ_ONCE(msk->ack_seq); 1442 ack_seq = mptcp_subflow_get_mapped_dsn(subflow); 1443 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx\n", old_ack, 1444 ack_seq); 1445 if (unlikely(before64(ack_seq, old_ack))) { 1446 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq); 1447 continue; 1448 } 1449 1450 WRITE_ONCE(subflow->data_avail, true); 1451 break; 1452 } 1453 return true; 1454 1455 no_data: 1456 subflow_sched_work_if_closed(msk, ssk); 1457 return false; 1458 1459 fallback: 1460 if (!__mptcp_check_fallback(msk)) { 1461 /* RFC 8684 section 3.7. */ 1462 if (status == MAPPING_BAD_CSUM && 1463 (subflow->mp_join || subflow->valid_csum_seen)) { 1464 subflow->send_mp_fail = 1; 1465 1466 if (!mptcp_subflow_fail(msk, ssk)) { 1467 subflow->reset_transient = 0; 1468 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX; 1469 goto reset; 1470 } 1471 WRITE_ONCE(subflow->data_avail, true); 1472 return true; 1473 } 1474 1475 if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSFALLBACK)) { 1476 /* fatal protocol error, close the socket. 1477 * subflow_error_report() will introduce the appropriate barriers 1478 */ 1479 subflow->reset_transient = 0; 1480 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSRESET); 1481 subflow->reset_reason = status == MAPPING_NODSS ? 1482 MPTCP_RST_EMIDDLEBOX : 1483 MPTCP_RST_EMPTCP; 1484 1485 reset: 1486 WRITE_ONCE(ssk->sk_err, EBADMSG); 1487 tcp_set_state(ssk, TCP_CLOSE); 1488 while ((skb = skb_peek(&ssk->sk_receive_queue))) 1489 sk_eat_skb(ssk, skb); 1490 mptcp_send_active_reset_reason(ssk); 1491 WRITE_ONCE(subflow->data_avail, false); 1492 return false; 1493 } 1494 } 1495 1496 skb = skb_peek(&ssk->sk_receive_queue); 1497 subflow->map_valid = 1; 1498 subflow->map_data_len = skb->len; 1499 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset; 1500 subflow->map_seq = __mptcp_expand_seq(subflow->map_seq, 1501 subflow->iasn + 1502 TCP_SKB_CB(skb)->seq - 1503 subflow->ssn_offset - 1); 1504 WRITE_ONCE(subflow->data_avail, true); 1505 return true; 1506 } 1507 1508 bool mptcp_subflow_data_available(struct sock *sk) 1509 { 1510 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1511 1512 /* check if current mapping is still valid */ 1513 if (subflow->map_valid && 1514 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) { 1515 subflow->map_valid = 0; 1516 WRITE_ONCE(subflow->data_avail, false); 1517 1518 pr_debug("Done with mapping: seq=%u data_len=%u\n", 1519 subflow->map_subflow_seq, 1520 subflow->map_data_len); 1521 } 1522 1523 return subflow_check_data_avail(sk); 1524 } 1525 1526 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy, 1527 * not the ssk one. 1528 * 1529 * In mptcp, rwin is about the mptcp-level connection data. 1530 * 1531 * Data that is still on the ssk rx queue can thus be ignored, 1532 * as far as mptcp peer is concerned that data is still inflight. 1533 * DSS ACK is updated when skb is moved to the mptcp rx queue. 1534 */ 1535 void mptcp_space(const struct sock *ssk, int *space, int *full_space) 1536 { 1537 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 1538 const struct sock *sk = subflow->conn; 1539 1540 *space = __mptcp_space(sk); 1541 *full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf)); 1542 } 1543 1544 static void subflow_error_report(struct sock *ssk) 1545 { 1546 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1547 1548 /* bail early if this is a no-op, so that we avoid introducing a 1549 * problematic lockdep dependency between TCP accept queue lock 1550 * and msk socket spinlock 1551 */ 1552 if (!sk->sk_socket) 1553 return; 1554 1555 mptcp_data_lock(sk); 1556 if (!sock_owned_by_user(sk)) 1557 __mptcp_error_report(sk); 1558 else 1559 __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags); 1560 mptcp_data_unlock(sk); 1561 } 1562 1563 static void subflow_data_ready(struct sock *sk) 1564 { 1565 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1566 u16 state = 1 << inet_sk_state_load(sk); 1567 struct sock *parent = subflow->conn; 1568 struct mptcp_sock *msk; 1569 1570 trace_sk_data_ready(sk); 1571 1572 msk = mptcp_sk(parent); 1573 if (state & TCPF_LISTEN) { 1574 /* MPJ subflow are removed from accept queue before reaching here, 1575 * avoid stray wakeups 1576 */ 1577 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue)) 1578 return; 1579 1580 parent->sk_data_ready(parent); 1581 return; 1582 } 1583 1584 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable && 1585 !subflow->mp_join && !(state & TCPF_CLOSE)); 1586 1587 if (mptcp_subflow_data_available(sk)) { 1588 mptcp_data_ready(parent, sk); 1589 1590 /* subflow-level lowat test are not relevant. 1591 * respect the msk-level threshold eventually mandating an immediate ack 1592 */ 1593 if (mptcp_data_avail(msk) < parent->sk_rcvlowat && 1594 (tcp_sk(sk)->rcv_nxt - tcp_sk(sk)->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss) 1595 inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW; 1596 } else if (unlikely(READ_ONCE(sk->sk_err))) { 1597 subflow_error_report(sk); 1598 } 1599 } 1600 1601 static void subflow_write_space(struct sock *ssk) 1602 { 1603 struct sock *sk = mptcp_subflow_ctx(ssk)->conn; 1604 1605 mptcp_propagate_sndbuf(sk, ssk); 1606 mptcp_write_space(sk); 1607 } 1608 1609 static const struct inet_connection_sock_af_ops * 1610 subflow_default_af_ops(struct sock *sk) 1611 { 1612 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1613 if (sk->sk_family == AF_INET6) 1614 return &subflow_v6_specific; 1615 #endif 1616 return &subflow_specific; 1617 } 1618 1619 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1620 void mptcpv6_handle_mapped(struct sock *sk, bool mapped) 1621 { 1622 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1623 struct inet_connection_sock *icsk = inet_csk(sk); 1624 const struct inet_connection_sock_af_ops *target; 1625 1626 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk); 1627 1628 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d\n", 1629 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped); 1630 1631 if (likely(icsk->icsk_af_ops == target)) 1632 return; 1633 1634 subflow->icsk_af_ops = icsk->icsk_af_ops; 1635 icsk->icsk_af_ops = target; 1636 } 1637 #endif 1638 1639 void mptcp_info2sockaddr(const struct mptcp_addr_info *info, 1640 struct sockaddr_storage *addr, 1641 unsigned short family) 1642 { 1643 memset(addr, 0, sizeof(*addr)); 1644 addr->ss_family = family; 1645 if (addr->ss_family == AF_INET) { 1646 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr; 1647 1648 if (info->family == AF_INET) 1649 in_addr->sin_addr = info->addr; 1650 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1651 else if (ipv6_addr_v4mapped(&info->addr6)) 1652 in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3]; 1653 #endif 1654 in_addr->sin_port = info->port; 1655 } 1656 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1657 else if (addr->ss_family == AF_INET6) { 1658 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr; 1659 1660 if (info->family == AF_INET) 1661 ipv6_addr_set_v4mapped(info->addr.s_addr, 1662 &in6_addr->sin6_addr); 1663 else 1664 in6_addr->sin6_addr = info->addr6; 1665 in6_addr->sin6_port = info->port; 1666 } 1667 #endif 1668 } 1669 1670 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local, 1671 const struct mptcp_addr_info *remote) 1672 { 1673 struct mptcp_sock *msk = mptcp_sk(sk); 1674 struct mptcp_subflow_context *subflow; 1675 int local_id = local->addr.id; 1676 struct sockaddr_storage addr; 1677 int remote_id = remote->id; 1678 int err = -ENOTCONN; 1679 struct socket *sf; 1680 struct sock *ssk; 1681 u32 remote_token; 1682 int addrlen; 1683 1684 /* The userspace PM sent the request too early? */ 1685 if (!mptcp_is_fully_established(sk)) 1686 goto err_out; 1687 1688 err = mptcp_subflow_create_socket(sk, local->addr.family, &sf); 1689 if (err) { 1690 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCREATSKERR); 1691 pr_debug("msk=%p local=%d remote=%d create sock error: %d\n", 1692 msk, local_id, remote_id, err); 1693 goto err_out; 1694 } 1695 1696 ssk = sf->sk; 1697 subflow = mptcp_subflow_ctx(ssk); 1698 do { 1699 subflow->local_nonce = get_random_u32(); 1700 } while (!subflow->local_nonce); 1701 1702 /* if 'IPADDRANY', the ID will be set later, after the routing */ 1703 if (local->addr.family == AF_INET) { 1704 if (!local->addr.addr.s_addr) 1705 local_id = -1; 1706 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1707 } else if (sk->sk_family == AF_INET6) { 1708 if (ipv6_addr_any(&local->addr.addr6)) 1709 local_id = -1; 1710 #endif 1711 } 1712 1713 if (local_id >= 0) 1714 subflow_set_local_id(subflow, local_id); 1715 1716 subflow->remote_key_valid = 1; 1717 subflow->remote_key = READ_ONCE(msk->remote_key); 1718 subflow->local_key = READ_ONCE(msk->local_key); 1719 subflow->token = msk->token; 1720 mptcp_info2sockaddr(&local->addr, &addr, ssk->sk_family); 1721 1722 addrlen = sizeof(struct sockaddr_in); 1723 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1724 if (addr.ss_family == AF_INET6) 1725 addrlen = sizeof(struct sockaddr_in6); 1726 #endif 1727 ssk->sk_bound_dev_if = local->ifindex; 1728 err = kernel_bind(sf, (struct sockaddr_unsized *)&addr, addrlen); 1729 if (err) { 1730 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXBINDERR); 1731 pr_debug("msk=%p local=%d remote=%d bind error: %d\n", 1732 msk, local_id, remote_id, err); 1733 goto failed; 1734 } 1735 1736 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL); 1737 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d\n", msk, 1738 remote_token, local_id, remote_id); 1739 subflow->remote_token = remote_token; 1740 WRITE_ONCE(subflow->remote_id, remote_id); 1741 subflow->request_join = 1; 1742 subflow->request_bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP); 1743 subflow->subflow_id = msk->subflow_id++; 1744 mptcp_info2sockaddr(remote, &addr, ssk->sk_family); 1745 1746 sock_hold(ssk); 1747 list_add_tail(&subflow->node, &msk->conn_list); 1748 err = kernel_connect(sf, (struct sockaddr_unsized *)&addr, addrlen, O_NONBLOCK); 1749 if (err && err != -EINPROGRESS) { 1750 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCONNECTERR); 1751 pr_debug("msk=%p local=%d remote=%d connect error: %d\n", 1752 msk, local_id, remote_id, err); 1753 goto failed_unlink; 1754 } 1755 1756 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTX); 1757 1758 /* discard the subflow socket */ 1759 mptcp_sock_graft(ssk, sk->sk_socket); 1760 iput(SOCK_INODE(sf)); 1761 mptcp_stop_tout_timer(sk); 1762 return 0; 1763 1764 failed_unlink: 1765 list_del(&subflow->node); 1766 sock_put(mptcp_subflow_tcp_sock(subflow)); 1767 1768 failed: 1769 subflow->disposable = 1; 1770 sock_release(sf); 1771 1772 err_out: 1773 /* we account subflows before the creation, and this failures will not 1774 * be caught by sk_state_change() 1775 */ 1776 mptcp_pm_close_subflow(msk); 1777 return err; 1778 } 1779 1780 void __mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp) 1781 { 1782 /* Only if the msk has been accepted already (and not orphaned).*/ 1783 if (!mem_cgroup_sockets_enabled || !sk->sk_socket) 1784 return; 1785 1786 mem_cgroup_sk_inherit(sk, ssk); 1787 __sk_charge(ssk, gfp); 1788 } 1789 1790 void __mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk) 1791 { 1792 #ifdef CONFIG_SOCK_CGROUP_DATA 1793 struct sock_cgroup_data *sk_cd = &sk->sk_cgrp_data, 1794 *ssk_cd = &ssk->sk_cgrp_data; 1795 1796 /* only the additional subflows created by kworkers have to be modified */ 1797 if (cgroup_id(sock_cgroup_ptr(sk_cd)) != 1798 cgroup_id(sock_cgroup_ptr(ssk_cd))) { 1799 cgroup_sk_free(ssk_cd); 1800 *ssk_cd = *sk_cd; 1801 cgroup_sk_clone(sk_cd); 1802 } 1803 #endif /* CONFIG_SOCK_CGROUP_DATA */ 1804 } 1805 1806 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child) 1807 { 1808 __mptcp_inherit_cgrp_data(parent, child); 1809 if (mem_cgroup_sockets_enabled) 1810 mem_cgroup_sk_inherit(parent, child); 1811 } 1812 1813 static void mptcp_subflow_ops_override(struct sock *ssk) 1814 { 1815 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1816 if (ssk->sk_prot == &tcpv6_prot) 1817 ssk->sk_prot = &tcpv6_prot_override; 1818 else 1819 #endif 1820 ssk->sk_prot = &tcp_prot_override; 1821 } 1822 1823 static void mptcp_subflow_ops_undo_override(struct sock *ssk) 1824 { 1825 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1826 if (ssk->sk_prot == &tcpv6_prot_override) 1827 ssk->sk_prot = &tcpv6_prot; 1828 else 1829 #endif 1830 ssk->sk_prot = &tcp_prot; 1831 } 1832 1833 int mptcp_subflow_create_socket(struct sock *sk, unsigned short family, 1834 struct socket **new_sock) 1835 { 1836 struct mptcp_subflow_context *subflow; 1837 struct net *net = sock_net(sk); 1838 struct socket *sf; 1839 int err; 1840 1841 /* un-accepted server sockets can reach here - on bad configuration 1842 * bail early to avoid greater trouble later 1843 */ 1844 if (unlikely(!sk->sk_socket)) 1845 return -EINVAL; 1846 1847 err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf); 1848 if (err) 1849 return err; 1850 1851 lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING); 1852 1853 err = security_mptcp_add_subflow(sk, sf->sk); 1854 if (err) 1855 goto err_free; 1856 1857 /* the newly created socket has to be in the same cgroup as its parent */ 1858 mptcp_attach_cgroup(sk, sf->sk); 1859 1860 /* kernel sockets do not by default acquire net ref, but TCP timer 1861 * needs it. 1862 * Update ns_tracker to current stack trace and refcounted tracker. 1863 */ 1864 sk_net_refcnt_upgrade(sf->sk); 1865 err = tcp_set_ulp(sf->sk, "mptcp"); 1866 if (err) 1867 goto err_free; 1868 1869 mptcp_sockopt_sync_locked(mptcp_sk(sk), sf->sk); 1870 release_sock(sf->sk); 1871 1872 /* the newly created socket really belongs to the owning MPTCP 1873 * socket, even if for additional subflows the allocation is performed 1874 * by a kernel workqueue. Adjust inode references, so that the 1875 * procfs/diag interfaces really show this one belonging to the correct 1876 * user. 1877 */ 1878 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino; 1879 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid; 1880 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid; 1881 1882 subflow = mptcp_subflow_ctx(sf->sk); 1883 pr_debug("subflow=%p\n", subflow); 1884 1885 *new_sock = sf; 1886 sock_hold(sk); 1887 subflow->conn = sk; 1888 mptcp_subflow_ops_override(sf->sk); 1889 1890 return 0; 1891 1892 err_free: 1893 release_sock(sf->sk); 1894 sock_release(sf); 1895 return err; 1896 } 1897 1898 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, 1899 gfp_t priority) 1900 { 1901 struct inet_connection_sock *icsk = inet_csk(sk); 1902 struct mptcp_subflow_context *ctx; 1903 1904 ctx = kzalloc_obj(*ctx, priority); 1905 if (!ctx) 1906 return NULL; 1907 1908 rcu_assign_pointer(icsk->icsk_ulp_data, ctx); 1909 INIT_LIST_HEAD(&ctx->node); 1910 INIT_LIST_HEAD(&ctx->delegated_node); 1911 1912 pr_debug("subflow=%p\n", ctx); 1913 1914 ctx->tcp_sock = sk; 1915 WRITE_ONCE(ctx->local_id, -1); 1916 1917 return ctx; 1918 } 1919 1920 static void __subflow_state_change(struct sock *sk) 1921 { 1922 struct socket_wq *wq; 1923 1924 rcu_read_lock(); 1925 wq = rcu_dereference(sk->sk_wq); 1926 if (skwq_has_sleeper(wq)) 1927 wake_up_interruptible_all(&wq->wait); 1928 rcu_read_unlock(); 1929 } 1930 1931 static void subflow_state_change(struct sock *sk) 1932 { 1933 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1934 struct sock *parent = subflow->conn; 1935 1936 __subflow_state_change(sk); 1937 1938 /* Rx queue processing is unneeded, error reporting will take place at 1939 * __mptcp_close_ssk() time and subflow reset can't happen in case of 1940 * fallback: subflow_sched_work_if_closed() would be a no-op. 1941 */ 1942 if (subflow->resetting) 1943 return; 1944 1945 /* as recvmsg() does not acquire the subflow socket for ssk selection 1946 * a fin packet carrying a DSS can be unnoticed if we don't trigger 1947 * the data available machinery here. 1948 */ 1949 if (mptcp_subflow_data_available(sk)) 1950 mptcp_data_ready(parent, sk); 1951 else if (unlikely(READ_ONCE(sk->sk_err))) 1952 subflow_error_report(sk); 1953 1954 subflow_sched_work_if_closed(mptcp_sk(parent), sk); 1955 } 1956 1957 void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk) 1958 { 1959 struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue; 1960 struct request_sock *req, *head, *tail; 1961 struct mptcp_subflow_context *subflow; 1962 struct sock *sk, *ssk; 1963 1964 /* Due to lock dependencies no relevant lock can be acquired under rskq_lock. 1965 * Splice the req list, so that accept() can not reach the pending ssk after 1966 * the listener socket is released below. 1967 */ 1968 spin_lock_bh(&queue->rskq_lock); 1969 head = queue->rskq_accept_head; 1970 tail = queue->rskq_accept_tail; 1971 queue->rskq_accept_head = NULL; 1972 queue->rskq_accept_tail = NULL; 1973 spin_unlock_bh(&queue->rskq_lock); 1974 1975 if (!head) 1976 return; 1977 1978 /* can't acquire the msk socket lock under the subflow one, 1979 * or will cause ABBA deadlock 1980 */ 1981 release_sock(listener_ssk); 1982 1983 for (req = head; req; req = req->dl_next) { 1984 ssk = req->sk; 1985 if (!sk_is_mptcp(ssk)) 1986 continue; 1987 1988 subflow = mptcp_subflow_ctx(ssk); 1989 if (!subflow || !subflow->conn) 1990 continue; 1991 1992 sk = subflow->conn; 1993 sock_hold(sk); 1994 1995 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 1996 __mptcp_unaccepted_force_close(sk); 1997 release_sock(sk); 1998 1999 /* lockdep will report a false positive ABBA deadlock 2000 * between cancel_work_sync and the listener socket. 2001 * The involved locks belong to different sockets WRT 2002 * the existing AB chain. 2003 * Using a per socket key is problematic as key 2004 * deregistration requires process context and must be 2005 * performed at socket disposal time, in atomic 2006 * context. 2007 * Just tell lockdep to consider the listener socket 2008 * released here. 2009 */ 2010 mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_); 2011 mptcp_cancel_work(sk); 2012 mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_); 2013 2014 sock_put(sk); 2015 } 2016 2017 /* we are still under the listener msk socket lock */ 2018 lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING); 2019 2020 /* restore the listener queue, to let the TCP code clean it up */ 2021 spin_lock_bh(&queue->rskq_lock); 2022 WARN_ON_ONCE(queue->rskq_accept_head); 2023 queue->rskq_accept_head = head; 2024 queue->rskq_accept_tail = tail; 2025 spin_unlock_bh(&queue->rskq_lock); 2026 } 2027 2028 static int subflow_ulp_init(struct sock *sk) 2029 { 2030 struct inet_connection_sock *icsk = inet_csk(sk); 2031 struct mptcp_subflow_context *ctx; 2032 struct tcp_sock *tp = tcp_sk(sk); 2033 int err = 0; 2034 2035 /* disallow attaching ULP to a socket unless it has been 2036 * created with sock_create_kern() 2037 */ 2038 if (!sk->sk_kern_sock) { 2039 err = -EOPNOTSUPP; 2040 goto out; 2041 } 2042 2043 ctx = subflow_create_ctx(sk, GFP_KERNEL); 2044 if (!ctx) { 2045 err = -ENOMEM; 2046 goto out; 2047 } 2048 2049 pr_debug("subflow=%p, family=%d\n", ctx, sk->sk_family); 2050 2051 tp->is_mptcp = 1; 2052 /* Subflows share the MPTCP socket, and thus its SOCK_NOSPACE bit, 2053 * which tcp_check_space() can not mirror. Pin the mirror so that 2054 * __tcp_check_space() always tests the shared bit. 2055 */ 2056 tp->tcp_nospace = 1; 2057 ctx->icsk_af_ops = icsk->icsk_af_ops; 2058 icsk->icsk_af_ops = subflow_default_af_ops(sk); 2059 ctx->tcp_state_change = sk->sk_state_change; 2060 ctx->tcp_error_report = sk->sk_error_report; 2061 2062 WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable); 2063 WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space); 2064 2065 sk->sk_data_ready = subflow_data_ready; 2066 sk->sk_write_space = subflow_write_space; 2067 sk->sk_state_change = subflow_state_change; 2068 sk->sk_error_report = subflow_error_report; 2069 out: 2070 return err; 2071 } 2072 2073 static void subflow_ulp_release(struct sock *ssk) 2074 { 2075 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); 2076 bool release = true; 2077 struct sock *sk; 2078 2079 if (!ctx) 2080 return; 2081 2082 sk = ctx->conn; 2083 if (sk) { 2084 /* if the msk has been orphaned, keep the ctx 2085 * alive, will be freed by __mptcp_close_ssk(), 2086 * when the subflow is still unaccepted 2087 */ 2088 release = ctx->disposable || list_empty(&ctx->node); 2089 2090 /* inet_child_forget() does not call sk_state_change(), 2091 * explicitly trigger the socket close machinery 2092 */ 2093 if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, 2094 &mptcp_sk(sk)->flags)) 2095 mptcp_schedule_work(sk); 2096 sock_put(sk); 2097 } 2098 2099 mptcp_subflow_ops_undo_override(ssk); 2100 if (release) 2101 kfree_rcu(ctx, rcu); 2102 } 2103 2104 static void subflow_ulp_clone(const struct request_sock *req, 2105 struct sock *newsk, 2106 const gfp_t priority) 2107 { 2108 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2109 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk); 2110 struct mptcp_subflow_context *new_ctx; 2111 2112 if (!tcp_rsk(req)->is_mptcp || 2113 (!subflow_req->mp_capable && !subflow_req->mp_join)) { 2114 subflow_ulp_fallback(newsk, old_ctx); 2115 return; 2116 } 2117 2118 new_ctx = subflow_create_ctx(newsk, priority); 2119 if (!new_ctx) { 2120 subflow_ulp_fallback(newsk, old_ctx); 2121 return; 2122 } 2123 2124 new_ctx->conn_finished = 1; 2125 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops; 2126 new_ctx->tcp_state_change = old_ctx->tcp_state_change; 2127 new_ctx->tcp_error_report = old_ctx->tcp_error_report; 2128 new_ctx->rel_write_seq = 1; 2129 2130 if (subflow_req->mp_capable) { 2131 /* see comments in subflow_syn_recv_sock(), MPTCP connection 2132 * is fully established only after we receive the remote key 2133 */ 2134 new_ctx->mp_capable = 1; 2135 new_ctx->local_key = subflow_req->local_key; 2136 new_ctx->token = subflow_req->token; 2137 new_ctx->ssn_offset = subflow_req->ssn_offset; 2138 new_ctx->idsn = subflow_req->idsn; 2139 2140 /* this is the first subflow, id is always 0 */ 2141 subflow_set_local_id(new_ctx, 0); 2142 } else if (subflow_req->mp_join) { 2143 new_ctx->ssn_offset = subflow_req->ssn_offset; 2144 new_ctx->mp_join = 1; 2145 WRITE_ONCE(new_ctx->fully_established, true); 2146 new_ctx->remote_key_valid = 1; 2147 new_ctx->backup = subflow_req->backup; 2148 new_ctx->request_bkup = subflow_req->request_bkup; 2149 WRITE_ONCE(new_ctx->remote_id, subflow_req->remote_id); 2150 new_ctx->token = subflow_req->token; 2151 2152 /* the subflow req id is valid, fetched via subflow_check_req() 2153 * and subflow_token_join_request() 2154 */ 2155 subflow_set_local_id(new_ctx, subflow_req->local_id); 2156 } 2157 } 2158 2159 static void tcp_release_cb_override(struct sock *ssk) 2160 { 2161 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2162 long status; 2163 2164 /* process and clear all the pending actions, but leave the subflow into 2165 * the napi queue. To respect locking, only the same CPU that originated 2166 * the action can touch the list. mptcp_napi_poll will take care of it. 2167 */ 2168 status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0); 2169 if (status) 2170 mptcp_subflow_process_delegated(ssk, status); 2171 2172 tcp_release_cb(ssk); 2173 } 2174 2175 static int tcp_abort_override(struct sock *ssk, int err) 2176 { 2177 /* closing a listener subflow requires a great deal of care. 2178 * keep it simple and just prevent such operation 2179 */ 2180 if (inet_sk_state_load(ssk) == TCP_LISTEN) 2181 return -EINVAL; 2182 2183 return tcp_abort(ssk, err); 2184 } 2185 2186 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = { 2187 .name = "mptcp", 2188 .owner = THIS_MODULE, 2189 .init = subflow_ulp_init, 2190 .release = subflow_ulp_release, 2191 .clone = subflow_ulp_clone, 2192 }; 2193 2194 static int subflow_ops_init(struct request_sock_ops *subflow_ops) 2195 { 2196 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock); 2197 2198 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name, 2199 subflow_ops->obj_size, 0, 2200 SLAB_ACCOUNT | 2201 SLAB_TYPESAFE_BY_RCU, 2202 NULL); 2203 if (!subflow_ops->slab) 2204 return -ENOMEM; 2205 2206 return 0; 2207 } 2208 2209 void __init mptcp_subflow_init(void) 2210 { 2211 mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops; 2212 mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4"; 2213 mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor; 2214 2215 if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0) 2216 panic("MPTCP: failed to init subflow v4 request sock ops\n"); 2217 2218 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops; 2219 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req; 2220 subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack; 2221 2222 subflow_specific = ipv4_specific; 2223 subflow_specific.conn_request = subflow_v4_conn_request; 2224 subflow_specific.syn_recv_sock = subflow_syn_recv_sock; 2225 subflow_specific.sk_rx_dst_set = subflow_finish_connect; 2226 subflow_specific.rebuild_header = subflow_rebuild_header; 2227 2228 tcp_prot_override = tcp_prot; 2229 tcp_prot_override.release_cb = tcp_release_cb_override; 2230 tcp_prot_override.diag_destroy = tcp_abort_override; 2231 #ifdef CONFIG_BPF_SYSCALL 2232 /* Disable sockmap processing for subflows */ 2233 tcp_prot_override.psock_update_sk_prot = NULL; 2234 #endif 2235 2236 mptcp_diag_subflow_init(&subflow_ulp_ops); 2237 2238 if (tcp_register_ulp(&subflow_ulp_ops) != 0) 2239 panic("MPTCP: failed to register subflows to ULP\n"); 2240 } 2241 2242 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2243 void __init mptcp_subflow_v6_init(void) 2244 { 2245 /* In struct mptcp_subflow_request_sock, we assume the TCP request sock 2246 * structures for v4 and v6 have the same size. It should not changed in 2247 * the future but better to make sure to be warned if it is no longer 2248 * the case. 2249 */ 2250 BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock)); 2251 2252 mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops; 2253 mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6"; 2254 mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor; 2255 2256 if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0) 2257 panic("MPTCP: failed to init subflow v6 request sock ops\n"); 2258 2259 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops; 2260 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req; 2261 subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack; 2262 2263 subflow_v6_specific = ipv6_specific; 2264 subflow_v6_specific.conn_request = subflow_v6_conn_request; 2265 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock; 2266 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect; 2267 subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header; 2268 2269 subflow_v6m_specific = subflow_v6_specific; 2270 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit; 2271 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len; 2272 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced; 2273 subflow_v6m_specific.rebuild_header = subflow_rebuild_header; 2274 2275 tcpv6_prot_override = tcpv6_prot; 2276 tcpv6_prot_override.release_cb = tcp_release_cb_override; 2277 tcpv6_prot_override.diag_destroy = tcp_abort_override; 2278 #ifdef CONFIG_BPF_SYSCALL 2279 /* Disable sockmap processing for subflows */ 2280 tcpv6_prot_override.psock_update_sk_prot = NULL; 2281 #endif 2282 } 2283 #endif 2284