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