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