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