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 <linux/sched/signal.h> 13 #include <linux/atomic.h> 14 #include <net/sock.h> 15 #include <net/inet_common.h> 16 #include <net/inet_hashtables.h> 17 #include <net/protocol.h> 18 #include <net/tcp.h> 19 #include <net/tcp_states.h> 20 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 21 #include <net/transp_v6.h> 22 #endif 23 #include <net/mptcp.h> 24 #include "protocol.h" 25 #include "mib.h" 26 27 #define MPTCP_SAME_STATE TCP_MAX_STATES 28 29 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 30 struct mptcp6_sock { 31 struct mptcp_sock msk; 32 struct ipv6_pinfo np; 33 }; 34 #endif 35 36 struct mptcp_skb_cb { 37 u32 offset; 38 }; 39 40 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 41 42 static struct percpu_counter mptcp_sockets_allocated; 43 44 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 45 * completed yet or has failed, return the subflow socket. 46 * Otherwise return NULL. 47 */ 48 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 49 { 50 if (!msk->subflow || READ_ONCE(msk->can_ack)) 51 return NULL; 52 53 return msk->subflow; 54 } 55 56 static bool mptcp_is_tcpsk(struct sock *sk) 57 { 58 struct socket *sock = sk->sk_socket; 59 60 if (unlikely(sk->sk_prot == &tcp_prot)) { 61 /* we are being invoked after mptcp_accept() has 62 * accepted a non-mp-capable flow: sk is a tcp_sk, 63 * not an mptcp one. 64 * 65 * Hand the socket over to tcp so all further socket ops 66 * bypass mptcp. 67 */ 68 sock->ops = &inet_stream_ops; 69 return true; 70 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 71 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 72 sock->ops = &inet6_stream_ops; 73 return true; 74 #endif 75 } 76 77 return false; 78 } 79 80 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk) 81 { 82 sock_owned_by_me((const struct sock *)msk); 83 84 if (likely(!__mptcp_check_fallback(msk))) 85 return NULL; 86 87 return msk->first; 88 } 89 90 static int __mptcp_socket_create(struct mptcp_sock *msk) 91 { 92 struct mptcp_subflow_context *subflow; 93 struct sock *sk = (struct sock *)msk; 94 struct socket *ssock; 95 int err; 96 97 err = mptcp_subflow_create_socket(sk, &ssock); 98 if (err) 99 return err; 100 101 msk->first = ssock->sk; 102 msk->subflow = ssock; 103 subflow = mptcp_subflow_ctx(ssock->sk); 104 list_add(&subflow->node, &msk->conn_list); 105 subflow->request_mptcp = 1; 106 107 /* accept() will wait on first subflow sk_wq, and we always wakes up 108 * via msk->sk_socket 109 */ 110 RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq); 111 112 return 0; 113 } 114 115 static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 116 struct sk_buff *skb, 117 unsigned int offset, size_t copy_len) 118 { 119 struct sock *sk = (struct sock *)msk; 120 struct sk_buff *tail; 121 122 __skb_unlink(skb, &ssk->sk_receive_queue); 123 124 skb_ext_reset(skb); 125 skb_orphan(skb); 126 msk->ack_seq += copy_len; 127 128 tail = skb_peek_tail(&sk->sk_receive_queue); 129 if (offset == 0 && tail) { 130 bool fragstolen; 131 int delta; 132 133 if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) { 134 kfree_skb_partial(skb, fragstolen); 135 atomic_add(delta, &sk->sk_rmem_alloc); 136 sk_mem_charge(sk, delta); 137 return; 138 } 139 } 140 141 skb_set_owner_r(skb, sk); 142 __skb_queue_tail(&sk->sk_receive_queue, skb); 143 MPTCP_SKB_CB(skb)->offset = offset; 144 } 145 146 static void mptcp_stop_timer(struct sock *sk) 147 { 148 struct inet_connection_sock *icsk = inet_csk(sk); 149 150 sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 151 mptcp_sk(sk)->timer_ival = 0; 152 } 153 154 /* both sockets must be locked */ 155 static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, 156 struct sock *ssk) 157 { 158 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 159 u64 dsn = mptcp_subflow_get_mapped_dsn(subflow); 160 161 /* revalidate data sequence number. 162 * 163 * mptcp_subflow_data_available() is usually called 164 * without msk lock. Its unlikely (but possible) 165 * that msk->ack_seq has been advanced since the last 166 * call found in-sequence data. 167 */ 168 if (likely(dsn == msk->ack_seq)) 169 return true; 170 171 subflow->data_avail = 0; 172 return mptcp_subflow_data_available(ssk); 173 } 174 175 static void mptcp_check_data_fin_ack(struct sock *sk) 176 { 177 struct mptcp_sock *msk = mptcp_sk(sk); 178 179 if (__mptcp_check_fallback(msk)) 180 return; 181 182 /* Look for an acknowledged DATA_FIN */ 183 if (((1 << sk->sk_state) & 184 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 185 msk->write_seq == atomic64_read(&msk->snd_una)) { 186 mptcp_stop_timer(sk); 187 188 WRITE_ONCE(msk->snd_data_fin_enable, 0); 189 190 switch (sk->sk_state) { 191 case TCP_FIN_WAIT1: 192 inet_sk_state_store(sk, TCP_FIN_WAIT2); 193 sk->sk_state_change(sk); 194 break; 195 case TCP_CLOSING: 196 case TCP_LAST_ACK: 197 inet_sk_state_store(sk, TCP_CLOSE); 198 sk->sk_state_change(sk); 199 break; 200 } 201 202 if (sk->sk_shutdown == SHUTDOWN_MASK || 203 sk->sk_state == TCP_CLOSE) 204 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 205 else 206 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 207 } 208 } 209 210 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 211 { 212 struct mptcp_sock *msk = mptcp_sk(sk); 213 214 if (READ_ONCE(msk->rcv_data_fin) && 215 ((1 << sk->sk_state) & 216 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 217 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 218 219 if (msk->ack_seq == rcv_data_fin_seq) { 220 if (seq) 221 *seq = rcv_data_fin_seq; 222 223 return true; 224 } 225 } 226 227 return false; 228 } 229 230 static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 231 { 232 long tout = ssk && inet_csk(ssk)->icsk_pending ? 233 inet_csk(ssk)->icsk_timeout - jiffies : 0; 234 235 if (tout <= 0) 236 tout = mptcp_sk(sk)->timer_ival; 237 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 238 } 239 240 static void mptcp_check_data_fin(struct sock *sk) 241 { 242 struct mptcp_sock *msk = mptcp_sk(sk); 243 u64 rcv_data_fin_seq; 244 245 if (__mptcp_check_fallback(msk) || !msk->first) 246 return; 247 248 /* Need to ack a DATA_FIN received from a peer while this side 249 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 250 * msk->rcv_data_fin was set when parsing the incoming options 251 * at the subflow level and the msk lock was not held, so this 252 * is the first opportunity to act on the DATA_FIN and change 253 * the msk state. 254 * 255 * If we are caught up to the sequence number of the incoming 256 * DATA_FIN, send the DATA_ACK now and do state transition. If 257 * not caught up, do nothing and let the recv code send DATA_ACK 258 * when catching up. 259 */ 260 261 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 262 struct mptcp_subflow_context *subflow; 263 264 msk->ack_seq++; 265 WRITE_ONCE(msk->rcv_data_fin, 0); 266 267 sk->sk_shutdown |= RCV_SHUTDOWN; 268 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 269 set_bit(MPTCP_DATA_READY, &msk->flags); 270 271 switch (sk->sk_state) { 272 case TCP_ESTABLISHED: 273 inet_sk_state_store(sk, TCP_CLOSE_WAIT); 274 break; 275 case TCP_FIN_WAIT1: 276 inet_sk_state_store(sk, TCP_CLOSING); 277 break; 278 case TCP_FIN_WAIT2: 279 inet_sk_state_store(sk, TCP_CLOSE); 280 // @@ Close subflows now? 281 break; 282 default: 283 /* Other states not expected */ 284 WARN_ON_ONCE(1); 285 break; 286 } 287 288 mptcp_set_timeout(sk, NULL); 289 mptcp_for_each_subflow(msk, subflow) { 290 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 291 292 lock_sock(ssk); 293 tcp_send_ack(ssk); 294 release_sock(ssk); 295 } 296 297 sk->sk_state_change(sk); 298 299 if (sk->sk_shutdown == SHUTDOWN_MASK || 300 sk->sk_state == TCP_CLOSE) 301 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 302 else 303 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 304 } 305 } 306 307 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 308 struct sock *ssk, 309 unsigned int *bytes) 310 { 311 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 312 struct sock *sk = (struct sock *)msk; 313 unsigned int moved = 0; 314 bool more_data_avail; 315 struct tcp_sock *tp; 316 bool done = false; 317 318 if (!mptcp_subflow_dsn_valid(msk, ssk)) { 319 *bytes = 0; 320 return false; 321 } 322 323 tp = tcp_sk(ssk); 324 do { 325 u32 map_remaining, offset; 326 u32 seq = tp->copied_seq; 327 struct sk_buff *skb; 328 bool fin; 329 330 /* try to move as much data as available */ 331 map_remaining = subflow->map_data_len - 332 mptcp_subflow_get_map_offset(subflow); 333 334 skb = skb_peek(&ssk->sk_receive_queue); 335 if (!skb) 336 break; 337 338 if (__mptcp_check_fallback(msk)) { 339 /* if we are running under the workqueue, TCP could have 340 * collapsed skbs between dummy map creation and now 341 * be sure to adjust the size 342 */ 343 map_remaining = skb->len; 344 subflow->map_data_len = skb->len; 345 } 346 347 offset = seq - TCP_SKB_CB(skb)->seq; 348 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 349 if (fin) { 350 done = true; 351 seq++; 352 } 353 354 if (offset < skb->len) { 355 size_t len = skb->len - offset; 356 357 if (tp->urg_data) 358 done = true; 359 360 __mptcp_move_skb(msk, ssk, skb, offset, len); 361 seq += len; 362 moved += len; 363 364 if (WARN_ON_ONCE(map_remaining < len)) 365 break; 366 } else { 367 WARN_ON_ONCE(!fin); 368 sk_eat_skb(ssk, skb); 369 done = true; 370 } 371 372 WRITE_ONCE(tp->copied_seq, seq); 373 more_data_avail = mptcp_subflow_data_available(ssk); 374 375 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) { 376 done = true; 377 break; 378 } 379 } while (more_data_avail); 380 381 *bytes = moved; 382 383 /* If the moves have caught up with the DATA_FIN sequence number 384 * it's time to ack the DATA_FIN and change socket state, but 385 * this is not a good place to change state. Let the workqueue 386 * do it. 387 */ 388 if (mptcp_pending_data_fin(sk, NULL) && 389 schedule_work(&msk->work)) 390 sock_hold(sk); 391 392 return done; 393 } 394 395 /* In most cases we will be able to lock the mptcp socket. If its already 396 * owned, we need to defer to the work queue to avoid ABBA deadlock. 397 */ 398 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 399 { 400 struct sock *sk = (struct sock *)msk; 401 unsigned int moved = 0; 402 403 if (READ_ONCE(sk->sk_lock.owned)) 404 return false; 405 406 if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 407 return false; 408 409 /* must re-check after taking the lock */ 410 if (!READ_ONCE(sk->sk_lock.owned)) 411 __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 412 413 spin_unlock_bh(&sk->sk_lock.slock); 414 415 return moved > 0; 416 } 417 418 void mptcp_data_ready(struct sock *sk, struct sock *ssk) 419 { 420 struct mptcp_sock *msk = mptcp_sk(sk); 421 422 set_bit(MPTCP_DATA_READY, &msk->flags); 423 424 if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) && 425 move_skbs_to_msk(msk, ssk)) 426 goto wake; 427 428 /* don't schedule if mptcp sk is (still) over limit */ 429 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) 430 goto wake; 431 432 /* mptcp socket is owned, release_cb should retry */ 433 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 434 &sk->sk_tsq_flags)) { 435 sock_hold(sk); 436 437 /* need to try again, its possible release_cb() has already 438 * been called after the test_and_set_bit() above. 439 */ 440 move_skbs_to_msk(msk, ssk); 441 } 442 wake: 443 sk->sk_data_ready(sk); 444 } 445 446 static void __mptcp_flush_join_list(struct mptcp_sock *msk) 447 { 448 if (likely(list_empty(&msk->join_list))) 449 return; 450 451 spin_lock_bh(&msk->join_list_lock); 452 list_splice_tail_init(&msk->join_list, &msk->conn_list); 453 spin_unlock_bh(&msk->join_list_lock); 454 } 455 456 static bool mptcp_timer_pending(struct sock *sk) 457 { 458 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 459 } 460 461 static void mptcp_reset_timer(struct sock *sk) 462 { 463 struct inet_connection_sock *icsk = inet_csk(sk); 464 unsigned long tout; 465 466 /* should never be called with mptcp level timer cleared */ 467 tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 468 if (WARN_ON_ONCE(!tout)) 469 tout = TCP_RTO_MIN; 470 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 471 } 472 473 void mptcp_data_acked(struct sock *sk) 474 { 475 mptcp_reset_timer(sk); 476 477 if ((!sk_stream_is_writeable(sk) || 478 (inet_sk_state_load(sk) != TCP_ESTABLISHED)) && 479 schedule_work(&mptcp_sk(sk)->work)) 480 sock_hold(sk); 481 } 482 483 void mptcp_subflow_eof(struct sock *sk) 484 { 485 struct mptcp_sock *msk = mptcp_sk(sk); 486 487 if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) && 488 schedule_work(&msk->work)) 489 sock_hold(sk); 490 } 491 492 static void mptcp_check_for_eof(struct mptcp_sock *msk) 493 { 494 struct mptcp_subflow_context *subflow; 495 struct sock *sk = (struct sock *)msk; 496 int receivers = 0; 497 498 mptcp_for_each_subflow(msk, subflow) 499 receivers += !subflow->rx_eof; 500 501 if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 502 /* hopefully temporary hack: propagate shutdown status 503 * to msk, when all subflows agree on it 504 */ 505 sk->sk_shutdown |= RCV_SHUTDOWN; 506 507 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 508 set_bit(MPTCP_DATA_READY, &msk->flags); 509 sk->sk_data_ready(sk); 510 } 511 } 512 513 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 514 { 515 const struct sock *sk = (const struct sock *)msk; 516 517 if (!msk->cached_ext) 518 msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); 519 520 return !!msk->cached_ext; 521 } 522 523 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 524 { 525 struct mptcp_subflow_context *subflow; 526 struct sock *sk = (struct sock *)msk; 527 528 sock_owned_by_me(sk); 529 530 mptcp_for_each_subflow(msk, subflow) { 531 if (subflow->data_avail) 532 return mptcp_subflow_tcp_sock(subflow); 533 } 534 535 return NULL; 536 } 537 538 static bool mptcp_skb_can_collapse_to(u64 write_seq, 539 const struct sk_buff *skb, 540 const struct mptcp_ext *mpext) 541 { 542 if (!tcp_skb_can_collapse_to(skb)) 543 return false; 544 545 /* can collapse only if MPTCP level sequence is in order */ 546 return mpext && mpext->data_seq + mpext->data_len == write_seq; 547 } 548 549 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 550 const struct page_frag *pfrag, 551 const struct mptcp_data_frag *df) 552 { 553 return df && pfrag->page == df->page && 554 df->data_seq + df->data_len == msk->write_seq; 555 } 556 557 static void dfrag_uncharge(struct sock *sk, int len) 558 { 559 sk_mem_uncharge(sk, len); 560 sk_wmem_queued_add(sk, -len); 561 } 562 563 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 564 { 565 int len = dfrag->data_len + dfrag->overhead; 566 567 list_del(&dfrag->list); 568 dfrag_uncharge(sk, len); 569 put_page(dfrag->page); 570 } 571 572 static void mptcp_clean_una(struct sock *sk) 573 { 574 struct mptcp_sock *msk = mptcp_sk(sk); 575 struct mptcp_data_frag *dtmp, *dfrag; 576 bool cleaned = false; 577 u64 snd_una; 578 579 /* on fallback we just need to ignore snd_una, as this is really 580 * plain TCP 581 */ 582 if (__mptcp_check_fallback(msk)) 583 atomic64_set(&msk->snd_una, msk->write_seq); 584 snd_una = atomic64_read(&msk->snd_una); 585 586 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 587 if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 588 break; 589 590 dfrag_clear(sk, dfrag); 591 cleaned = true; 592 } 593 594 dfrag = mptcp_rtx_head(sk); 595 if (dfrag && after64(snd_una, dfrag->data_seq)) { 596 u64 delta = snd_una - dfrag->data_seq; 597 598 if (WARN_ON_ONCE(delta > dfrag->data_len)) 599 goto out; 600 601 dfrag->data_seq += delta; 602 dfrag->offset += delta; 603 dfrag->data_len -= delta; 604 605 dfrag_uncharge(sk, delta); 606 cleaned = true; 607 } 608 609 out: 610 if (cleaned) { 611 sk_mem_reclaim_partial(sk); 612 613 /* Only wake up writers if a subflow is ready */ 614 if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 615 sk_stream_write_space(sk); 616 } 617 } 618 619 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 620 * data 621 */ 622 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 623 { 624 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 625 pfrag, sk->sk_allocation))) 626 return true; 627 628 sk->sk_prot->enter_memory_pressure(sk); 629 sk_stream_moderate_sndbuf(sk); 630 return false; 631 } 632 633 static struct mptcp_data_frag * 634 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 635 int orig_offset) 636 { 637 int offset = ALIGN(orig_offset, sizeof(long)); 638 struct mptcp_data_frag *dfrag; 639 640 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 641 dfrag->data_len = 0; 642 dfrag->data_seq = msk->write_seq; 643 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 644 dfrag->offset = offset + sizeof(struct mptcp_data_frag); 645 dfrag->page = pfrag->page; 646 647 return dfrag; 648 } 649 650 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 651 struct msghdr *msg, struct mptcp_data_frag *dfrag, 652 long *timeo, int *pmss_now, 653 int *ps_goal) 654 { 655 int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0; 656 bool dfrag_collapsed, can_collapse = false; 657 struct mptcp_sock *msk = mptcp_sk(sk); 658 struct mptcp_ext *mpext = NULL; 659 bool retransmission = !!dfrag; 660 struct sk_buff *skb, *tail; 661 struct page_frag *pfrag; 662 struct page *page; 663 u64 *write_seq; 664 size_t psize; 665 666 /* use the mptcp page cache so that we can easily move the data 667 * from one substream to another, but do per subflow memory accounting 668 * Note: pfrag is used only !retransmission, but the compiler if 669 * fooled into a warning if we don't init here 670 */ 671 pfrag = sk_page_frag(sk); 672 if (!retransmission) { 673 write_seq = &msk->write_seq; 674 page = pfrag->page; 675 } else { 676 write_seq = &dfrag->data_seq; 677 page = dfrag->page; 678 } 679 680 /* compute copy limit */ 681 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); 682 *pmss_now = mss_now; 683 *ps_goal = size_goal; 684 avail_size = size_goal; 685 skb = tcp_write_queue_tail(ssk); 686 if (skb) { 687 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 688 689 /* Limit the write to the size available in the 690 * current skb, if any, so that we create at most a new skb. 691 * Explicitly tells TCP internals to avoid collapsing on later 692 * queue management operation, to avoid breaking the ext <-> 693 * SSN association set here 694 */ 695 can_collapse = (size_goal - skb->len > 0) && 696 mptcp_skb_can_collapse_to(*write_seq, skb, mpext); 697 if (!can_collapse) 698 TCP_SKB_CB(skb)->eor = 1; 699 else 700 avail_size = size_goal - skb->len; 701 } 702 703 if (!retransmission) { 704 /* reuse tail pfrag, if possible, or carve a new one from the 705 * page allocator 706 */ 707 dfrag = mptcp_rtx_tail(sk); 708 offset = pfrag->offset; 709 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 710 if (!dfrag_collapsed) { 711 dfrag = mptcp_carve_data_frag(msk, pfrag, offset); 712 offset = dfrag->offset; 713 frag_truesize = dfrag->overhead; 714 } 715 psize = min_t(size_t, pfrag->size - offset, avail_size); 716 717 /* Copy to page */ 718 pr_debug("left=%zu", msg_data_left(msg)); 719 psize = copy_page_from_iter(pfrag->page, offset, 720 min_t(size_t, msg_data_left(msg), 721 psize), 722 &msg->msg_iter); 723 pr_debug("left=%zu", msg_data_left(msg)); 724 if (!psize) 725 return -EINVAL; 726 727 if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) { 728 iov_iter_revert(&msg->msg_iter, psize); 729 return -ENOMEM; 730 } 731 } else { 732 offset = dfrag->offset; 733 psize = min_t(size_t, dfrag->data_len, avail_size); 734 } 735 736 /* tell the TCP stack to delay the push so that we can safely 737 * access the skb after the sendpages call 738 */ 739 ret = do_tcp_sendpages(ssk, page, offset, psize, 740 msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT); 741 if (ret <= 0) { 742 if (!retransmission) 743 iov_iter_revert(&msg->msg_iter, psize); 744 return ret; 745 } 746 747 frag_truesize += ret; 748 if (!retransmission) { 749 if (unlikely(ret < psize)) 750 iov_iter_revert(&msg->msg_iter, psize - ret); 751 752 /* send successful, keep track of sent data for mptcp-level 753 * retransmission 754 */ 755 dfrag->data_len += ret; 756 if (!dfrag_collapsed) { 757 get_page(dfrag->page); 758 list_add_tail(&dfrag->list, &msk->rtx_queue); 759 sk_wmem_queued_add(sk, frag_truesize); 760 } else { 761 sk_wmem_queued_add(sk, ret); 762 } 763 764 /* charge data on mptcp rtx queue to the master socket 765 * Note: we charge such data both to sk and ssk 766 */ 767 sk->sk_forward_alloc -= frag_truesize; 768 } 769 770 /* if the tail skb extension is still the cached one, collapsing 771 * really happened. Note: we can't check for 'same skb' as the sk_buff 772 * hdr on tail can be transmitted, freed and re-allocated by the 773 * do_tcp_sendpages() call 774 */ 775 tail = tcp_write_queue_tail(ssk); 776 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { 777 WARN_ON_ONCE(!can_collapse); 778 mpext->data_len += ret; 779 goto out; 780 } 781 782 skb = tcp_write_queue_tail(ssk); 783 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); 784 msk->cached_ext = NULL; 785 786 memset(mpext, 0, sizeof(*mpext)); 787 mpext->data_seq = *write_seq; 788 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 789 mpext->data_len = ret; 790 mpext->use_map = 1; 791 mpext->dsn64 = 1; 792 793 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 794 mpext->data_seq, mpext->subflow_seq, mpext->data_len, 795 mpext->dsn64); 796 797 out: 798 if (!retransmission) 799 pfrag->offset += frag_truesize; 800 WRITE_ONCE(*write_seq, *write_seq + ret); 801 mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 802 803 return ret; 804 } 805 806 static void mptcp_nospace(struct mptcp_sock *msk, struct socket *sock) 807 { 808 clear_bit(MPTCP_SEND_SPACE, &msk->flags); 809 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 810 811 /* enables sk->write_space() callbacks */ 812 set_bit(SOCK_NOSPACE, &sock->flags); 813 } 814 815 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 816 { 817 struct mptcp_subflow_context *subflow; 818 struct sock *backup = NULL; 819 820 sock_owned_by_me((const struct sock *)msk); 821 822 if (!mptcp_ext_cache_refill(msk)) 823 return NULL; 824 825 mptcp_for_each_subflow(msk, subflow) { 826 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 827 828 if (!sk_stream_memory_free(ssk)) { 829 struct socket *sock = ssk->sk_socket; 830 831 if (sock) 832 mptcp_nospace(msk, sock); 833 834 return NULL; 835 } 836 837 if (subflow->backup) { 838 if (!backup) 839 backup = ssk; 840 841 continue; 842 } 843 844 return ssk; 845 } 846 847 return backup; 848 } 849 850 static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) 851 { 852 struct socket *sock; 853 854 if (likely(sk_stream_is_writeable(ssk))) 855 return; 856 857 sock = READ_ONCE(ssk->sk_socket); 858 if (sock) 859 mptcp_nospace(msk, sock); 860 } 861 862 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 863 { 864 int mss_now = 0, size_goal = 0, ret = 0; 865 struct mptcp_sock *msk = mptcp_sk(sk); 866 struct page_frag *pfrag; 867 size_t copied = 0; 868 struct sock *ssk; 869 bool tx_ok; 870 long timeo; 871 872 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 873 return -EOPNOTSUPP; 874 875 lock_sock(sk); 876 877 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 878 879 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 880 ret = sk_stream_wait_connect(sk, &timeo); 881 if (ret) 882 goto out; 883 } 884 885 pfrag = sk_page_frag(sk); 886 restart: 887 mptcp_clean_una(sk); 888 889 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { 890 ret = -EPIPE; 891 goto out; 892 } 893 894 wait_for_sndbuf: 895 __mptcp_flush_join_list(msk); 896 ssk = mptcp_subflow_get_send(msk); 897 while (!sk_stream_memory_free(sk) || 898 !ssk || 899 !mptcp_page_frag_refill(ssk, pfrag)) { 900 if (ssk) { 901 /* make sure retransmit timer is 902 * running before we wait for memory. 903 * 904 * The retransmit timer might be needed 905 * to make the peer send an up-to-date 906 * MPTCP Ack. 907 */ 908 mptcp_set_timeout(sk, ssk); 909 if (!mptcp_timer_pending(sk)) 910 mptcp_reset_timer(sk); 911 } 912 913 ret = sk_stream_wait_memory(sk, &timeo); 914 if (ret) 915 goto out; 916 917 mptcp_clean_una(sk); 918 919 ssk = mptcp_subflow_get_send(msk); 920 if (list_empty(&msk->conn_list)) { 921 ret = -ENOTCONN; 922 goto out; 923 } 924 } 925 926 pr_debug("conn_list->subflow=%p", ssk); 927 928 lock_sock(ssk); 929 tx_ok = msg_data_left(msg); 930 while (tx_ok) { 931 ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now, 932 &size_goal); 933 if (ret < 0) { 934 if (ret == -EAGAIN && timeo > 0) { 935 mptcp_set_timeout(sk, ssk); 936 release_sock(ssk); 937 goto restart; 938 } 939 break; 940 } 941 942 copied += ret; 943 944 tx_ok = msg_data_left(msg); 945 if (!tx_ok) 946 break; 947 948 if (!sk_stream_memory_free(ssk) || 949 !mptcp_page_frag_refill(ssk, pfrag) || 950 !mptcp_ext_cache_refill(msk)) { 951 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 952 tcp_push(ssk, msg->msg_flags, mss_now, 953 tcp_sk(ssk)->nonagle, size_goal); 954 mptcp_set_timeout(sk, ssk); 955 release_sock(ssk); 956 goto restart; 957 } 958 959 /* memory is charged to mptcp level socket as well, i.e. 960 * if msg is very large, mptcp socket may run out of buffer 961 * space. mptcp_clean_una() will release data that has 962 * been acked at mptcp level in the mean time, so there is 963 * a good chance we can continue sending data right away. 964 * 965 * Normally, when the tcp subflow can accept more data, then 966 * so can the MPTCP socket. However, we need to cope with 967 * peers that might lag behind in their MPTCP-level 968 * acknowledgements, i.e. data might have been acked at 969 * tcp level only. So, we must also check the MPTCP socket 970 * limits before we send more data. 971 */ 972 if (unlikely(!sk_stream_memory_free(sk))) { 973 tcp_push(ssk, msg->msg_flags, mss_now, 974 tcp_sk(ssk)->nonagle, size_goal); 975 mptcp_clean_una(sk); 976 if (!sk_stream_memory_free(sk)) { 977 /* can't send more for now, need to wait for 978 * MPTCP-level ACKs from peer. 979 * 980 * Wakeup will happen via mptcp_clean_una(). 981 */ 982 mptcp_set_timeout(sk, ssk); 983 release_sock(ssk); 984 goto wait_for_sndbuf; 985 } 986 } 987 } 988 989 mptcp_set_timeout(sk, ssk); 990 if (copied) { 991 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, 992 size_goal); 993 994 /* start the timer, if it's not pending */ 995 if (!mptcp_timer_pending(sk)) 996 mptcp_reset_timer(sk); 997 } 998 999 ssk_check_wmem(msk, ssk); 1000 release_sock(ssk); 1001 out: 1002 release_sock(sk); 1003 return copied ? : ret; 1004 } 1005 1006 static void mptcp_wait_data(struct sock *sk, long *timeo) 1007 { 1008 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1009 struct mptcp_sock *msk = mptcp_sk(sk); 1010 1011 add_wait_queue(sk_sleep(sk), &wait); 1012 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 1013 1014 sk_wait_event(sk, timeo, 1015 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 1016 1017 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 1018 remove_wait_queue(sk_sleep(sk), &wait); 1019 } 1020 1021 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 1022 struct msghdr *msg, 1023 size_t len) 1024 { 1025 struct sock *sk = (struct sock *)msk; 1026 struct sk_buff *skb; 1027 int copied = 0; 1028 1029 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 1030 u32 offset = MPTCP_SKB_CB(skb)->offset; 1031 u32 data_len = skb->len - offset; 1032 u32 count = min_t(size_t, len - copied, data_len); 1033 int err; 1034 1035 err = skb_copy_datagram_msg(skb, offset, msg, count); 1036 if (unlikely(err < 0)) { 1037 if (!copied) 1038 return err; 1039 break; 1040 } 1041 1042 copied += count; 1043 1044 if (count < data_len) { 1045 MPTCP_SKB_CB(skb)->offset += count; 1046 break; 1047 } 1048 1049 __skb_unlink(skb, &sk->sk_receive_queue); 1050 __kfree_skb(skb); 1051 1052 if (copied >= len) 1053 break; 1054 } 1055 1056 return copied; 1057 } 1058 1059 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1060 * 1061 * Only difference: Use highest rtt estimate of the subflows in use. 1062 */ 1063 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1064 { 1065 struct mptcp_subflow_context *subflow; 1066 struct sock *sk = (struct sock *)msk; 1067 u32 time, advmss = 1; 1068 u64 rtt_us, mstamp; 1069 1070 sock_owned_by_me(sk); 1071 1072 if (copied <= 0) 1073 return; 1074 1075 msk->rcvq_space.copied += copied; 1076 1077 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1078 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1079 1080 rtt_us = msk->rcvq_space.rtt_us; 1081 if (rtt_us && time < (rtt_us >> 3)) 1082 return; 1083 1084 rtt_us = 0; 1085 mptcp_for_each_subflow(msk, subflow) { 1086 const struct tcp_sock *tp; 1087 u64 sf_rtt_us; 1088 u32 sf_advmss; 1089 1090 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1091 1092 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1093 sf_advmss = READ_ONCE(tp->advmss); 1094 1095 rtt_us = max(sf_rtt_us, rtt_us); 1096 advmss = max(sf_advmss, advmss); 1097 } 1098 1099 msk->rcvq_space.rtt_us = rtt_us; 1100 if (time < (rtt_us >> 3) || rtt_us == 0) 1101 return; 1102 1103 if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1104 goto new_measure; 1105 1106 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf && 1107 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1108 int rcvmem, rcvbuf; 1109 u64 rcvwin, grow; 1110 1111 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1112 1113 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1114 1115 do_div(grow, msk->rcvq_space.space); 1116 rcvwin += (grow << 1); 1117 1118 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1119 while (tcp_win_from_space(sk, rcvmem) < advmss) 1120 rcvmem += 128; 1121 1122 do_div(rcvwin, advmss); 1123 rcvbuf = min_t(u64, rcvwin * rcvmem, 1124 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); 1125 1126 if (rcvbuf > sk->sk_rcvbuf) { 1127 u32 window_clamp; 1128 1129 window_clamp = tcp_win_from_space(sk, rcvbuf); 1130 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1131 1132 /* Make subflows follow along. If we do not do this, we 1133 * get drops at subflow level if skbs can't be moved to 1134 * the mptcp rx queue fast enough (announced rcv_win can 1135 * exceed ssk->sk_rcvbuf). 1136 */ 1137 mptcp_for_each_subflow(msk, subflow) { 1138 struct sock *ssk; 1139 1140 ssk = mptcp_subflow_tcp_sock(subflow); 1141 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1142 tcp_sk(ssk)->window_clamp = window_clamp; 1143 } 1144 } 1145 } 1146 1147 msk->rcvq_space.space = msk->rcvq_space.copied; 1148 new_measure: 1149 msk->rcvq_space.copied = 0; 1150 msk->rcvq_space.time = mstamp; 1151 } 1152 1153 static bool __mptcp_move_skbs(struct mptcp_sock *msk) 1154 { 1155 unsigned int moved = 0; 1156 bool done; 1157 1158 do { 1159 struct sock *ssk = mptcp_subflow_recv_lookup(msk); 1160 1161 if (!ssk) 1162 break; 1163 1164 lock_sock(ssk); 1165 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 1166 release_sock(ssk); 1167 } while (!done); 1168 1169 return moved > 0; 1170 } 1171 1172 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 1173 int nonblock, int flags, int *addr_len) 1174 { 1175 struct mptcp_sock *msk = mptcp_sk(sk); 1176 int copied = 0; 1177 int target; 1178 long timeo; 1179 1180 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 1181 return -EOPNOTSUPP; 1182 1183 lock_sock(sk); 1184 timeo = sock_rcvtimeo(sk, nonblock); 1185 1186 len = min_t(size_t, len, INT_MAX); 1187 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 1188 __mptcp_flush_join_list(msk); 1189 1190 while (len > (size_t)copied) { 1191 int bytes_read; 1192 1193 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 1194 if (unlikely(bytes_read < 0)) { 1195 if (!copied) 1196 copied = bytes_read; 1197 goto out_err; 1198 } 1199 1200 copied += bytes_read; 1201 1202 if (skb_queue_empty(&sk->sk_receive_queue) && 1203 __mptcp_move_skbs(msk)) 1204 continue; 1205 1206 /* only the master socket status is relevant here. The exit 1207 * conditions mirror closely tcp_recvmsg() 1208 */ 1209 if (copied >= target) 1210 break; 1211 1212 if (copied) { 1213 if (sk->sk_err || 1214 sk->sk_state == TCP_CLOSE || 1215 (sk->sk_shutdown & RCV_SHUTDOWN) || 1216 !timeo || 1217 signal_pending(current)) 1218 break; 1219 } else { 1220 if (sk->sk_err) { 1221 copied = sock_error(sk); 1222 break; 1223 } 1224 1225 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 1226 mptcp_check_for_eof(msk); 1227 1228 if (sk->sk_shutdown & RCV_SHUTDOWN) 1229 break; 1230 1231 if (sk->sk_state == TCP_CLOSE) { 1232 copied = -ENOTCONN; 1233 break; 1234 } 1235 1236 if (!timeo) { 1237 copied = -EAGAIN; 1238 break; 1239 } 1240 1241 if (signal_pending(current)) { 1242 copied = sock_intr_errno(timeo); 1243 break; 1244 } 1245 } 1246 1247 pr_debug("block timeout %ld", timeo); 1248 mptcp_wait_data(sk, &timeo); 1249 } 1250 1251 if (skb_queue_empty(&sk->sk_receive_queue)) { 1252 /* entire backlog drained, clear DATA_READY. */ 1253 clear_bit(MPTCP_DATA_READY, &msk->flags); 1254 1255 /* .. race-breaker: ssk might have gotten new data 1256 * after last __mptcp_move_skbs() returned false. 1257 */ 1258 if (unlikely(__mptcp_move_skbs(msk))) 1259 set_bit(MPTCP_DATA_READY, &msk->flags); 1260 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 1261 /* data to read but mptcp_wait_data() cleared DATA_READY */ 1262 set_bit(MPTCP_DATA_READY, &msk->flags); 1263 } 1264 out_err: 1265 mptcp_rcv_space_adjust(msk, copied); 1266 1267 release_sock(sk); 1268 return copied; 1269 } 1270 1271 static void mptcp_retransmit_handler(struct sock *sk) 1272 { 1273 struct mptcp_sock *msk = mptcp_sk(sk); 1274 1275 if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->write_seq)) { 1276 mptcp_stop_timer(sk); 1277 } else { 1278 set_bit(MPTCP_WORK_RTX, &msk->flags); 1279 if (schedule_work(&msk->work)) 1280 sock_hold(sk); 1281 } 1282 } 1283 1284 static void mptcp_retransmit_timer(struct timer_list *t) 1285 { 1286 struct inet_connection_sock *icsk = from_timer(icsk, t, 1287 icsk_retransmit_timer); 1288 struct sock *sk = &icsk->icsk_inet.sk; 1289 1290 bh_lock_sock(sk); 1291 if (!sock_owned_by_user(sk)) { 1292 mptcp_retransmit_handler(sk); 1293 } else { 1294 /* delegate our work to tcp_release_cb() */ 1295 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1296 &sk->sk_tsq_flags)) 1297 sock_hold(sk); 1298 } 1299 bh_unlock_sock(sk); 1300 sock_put(sk); 1301 } 1302 1303 /* Find an idle subflow. Return NULL if there is unacked data at tcp 1304 * level. 1305 * 1306 * A backup subflow is returned only if that is the only kind available. 1307 */ 1308 static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 1309 { 1310 struct mptcp_subflow_context *subflow; 1311 struct sock *backup = NULL; 1312 1313 sock_owned_by_me((const struct sock *)msk); 1314 1315 mptcp_for_each_subflow(msk, subflow) { 1316 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1317 1318 /* still data outstanding at TCP level? Don't retransmit. */ 1319 if (!tcp_write_queue_empty(ssk)) 1320 return NULL; 1321 1322 if (subflow->backup) { 1323 if (!backup) 1324 backup = ssk; 1325 continue; 1326 } 1327 1328 return ssk; 1329 } 1330 1331 return backup; 1332 } 1333 1334 /* subflow sockets can be either outgoing (connect) or incoming 1335 * (accept). 1336 * 1337 * Outgoing subflows use in-kernel sockets. 1338 * Incoming subflows do not have their own 'struct socket' allocated, 1339 * so we need to use tcp_close() after detaching them from the mptcp 1340 * parent socket. 1341 */ 1342 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1343 struct mptcp_subflow_context *subflow, 1344 long timeout) 1345 { 1346 struct socket *sock = READ_ONCE(ssk->sk_socket); 1347 1348 list_del(&subflow->node); 1349 1350 if (sock && sock != sk->sk_socket) { 1351 /* outgoing subflow */ 1352 sock_release(sock); 1353 } else { 1354 /* incoming subflow */ 1355 tcp_close(ssk, timeout); 1356 } 1357 } 1358 1359 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1360 { 1361 return 0; 1362 } 1363 1364 static void pm_work(struct mptcp_sock *msk) 1365 { 1366 struct mptcp_pm_data *pm = &msk->pm; 1367 1368 spin_lock_bh(&msk->pm.lock); 1369 1370 pr_debug("msk=%p status=%x", msk, pm->status); 1371 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) { 1372 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED); 1373 mptcp_pm_nl_add_addr_received(msk); 1374 } 1375 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) { 1376 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED); 1377 mptcp_pm_nl_fully_established(msk); 1378 } 1379 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) { 1380 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED); 1381 mptcp_pm_nl_subflow_established(msk); 1382 } 1383 1384 spin_unlock_bh(&msk->pm.lock); 1385 } 1386 1387 static void mptcp_worker(struct work_struct *work) 1388 { 1389 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 1390 struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 1391 int orig_len, orig_offset, mss_now = 0, size_goal = 0; 1392 struct mptcp_data_frag *dfrag; 1393 u64 orig_write_seq; 1394 size_t copied = 0; 1395 struct msghdr msg = { 1396 .msg_flags = MSG_DONTWAIT, 1397 }; 1398 long timeo = 0; 1399 1400 lock_sock(sk); 1401 mptcp_clean_una(sk); 1402 mptcp_check_data_fin_ack(sk); 1403 __mptcp_flush_join_list(msk); 1404 __mptcp_move_skbs(msk); 1405 1406 if (msk->pm.status) 1407 pm_work(msk); 1408 1409 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 1410 mptcp_check_for_eof(msk); 1411 1412 mptcp_check_data_fin(sk); 1413 1414 if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 1415 goto unlock; 1416 1417 dfrag = mptcp_rtx_head(sk); 1418 if (!dfrag) 1419 goto unlock; 1420 1421 if (!mptcp_ext_cache_refill(msk)) 1422 goto reset_unlock; 1423 1424 ssk = mptcp_subflow_get_retrans(msk); 1425 if (!ssk) 1426 goto reset_unlock; 1427 1428 lock_sock(ssk); 1429 1430 orig_len = dfrag->data_len; 1431 orig_offset = dfrag->offset; 1432 orig_write_seq = dfrag->data_seq; 1433 while (dfrag->data_len > 0) { 1434 int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, 1435 &mss_now, &size_goal); 1436 if (ret < 0) 1437 break; 1438 1439 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 1440 copied += ret; 1441 dfrag->data_len -= ret; 1442 dfrag->offset += ret; 1443 1444 if (!mptcp_ext_cache_refill(msk)) 1445 break; 1446 } 1447 if (copied) 1448 tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle, 1449 size_goal); 1450 1451 dfrag->data_seq = orig_write_seq; 1452 dfrag->offset = orig_offset; 1453 dfrag->data_len = orig_len; 1454 1455 mptcp_set_timeout(sk, ssk); 1456 release_sock(ssk); 1457 1458 reset_unlock: 1459 if (!mptcp_timer_pending(sk)) 1460 mptcp_reset_timer(sk); 1461 1462 unlock: 1463 release_sock(sk); 1464 sock_put(sk); 1465 } 1466 1467 static int __mptcp_init_sock(struct sock *sk) 1468 { 1469 struct mptcp_sock *msk = mptcp_sk(sk); 1470 1471 spin_lock_init(&msk->join_list_lock); 1472 1473 INIT_LIST_HEAD(&msk->conn_list); 1474 INIT_LIST_HEAD(&msk->join_list); 1475 INIT_LIST_HEAD(&msk->rtx_queue); 1476 __set_bit(MPTCP_SEND_SPACE, &msk->flags); 1477 INIT_WORK(&msk->work, mptcp_worker); 1478 1479 msk->first = NULL; 1480 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 1481 1482 mptcp_pm_data_init(msk); 1483 1484 /* re-use the csk retrans timer for MPTCP-level retrans */ 1485 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1486 1487 return 0; 1488 } 1489 1490 static int mptcp_init_sock(struct sock *sk) 1491 { 1492 struct net *net = sock_net(sk); 1493 int ret; 1494 1495 if (!mptcp_is_enabled(net)) 1496 return -ENOPROTOOPT; 1497 1498 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1499 return -ENOMEM; 1500 1501 ret = __mptcp_init_sock(sk); 1502 if (ret) 1503 return ret; 1504 1505 ret = __mptcp_socket_create(mptcp_sk(sk)); 1506 if (ret) 1507 return ret; 1508 1509 sk_sockets_allocated_inc(sk); 1510 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 1511 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2]; 1512 1513 return 0; 1514 } 1515 1516 static void __mptcp_clear_xmit(struct sock *sk) 1517 { 1518 struct mptcp_sock *msk = mptcp_sk(sk); 1519 struct mptcp_data_frag *dtmp, *dfrag; 1520 1521 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1522 1523 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 1524 dfrag_clear(sk, dfrag); 1525 } 1526 1527 static void mptcp_cancel_work(struct sock *sk) 1528 { 1529 struct mptcp_sock *msk = mptcp_sk(sk); 1530 1531 if (cancel_work_sync(&msk->work)) 1532 sock_put(sk); 1533 } 1534 1535 static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 1536 { 1537 lock_sock(ssk); 1538 1539 switch (ssk->sk_state) { 1540 case TCP_LISTEN: 1541 if (!(how & RCV_SHUTDOWN)) 1542 break; 1543 fallthrough; 1544 case TCP_SYN_SENT: 1545 tcp_disconnect(ssk, O_NONBLOCK); 1546 break; 1547 default: 1548 if (__mptcp_check_fallback(mptcp_sk(sk))) { 1549 pr_debug("Fallback"); 1550 ssk->sk_shutdown |= how; 1551 tcp_shutdown(ssk, how); 1552 } else { 1553 pr_debug("Sending DATA_FIN on subflow %p", ssk); 1554 mptcp_set_timeout(sk, ssk); 1555 tcp_send_ack(ssk); 1556 } 1557 break; 1558 } 1559 1560 release_sock(ssk); 1561 } 1562 1563 static const unsigned char new_state[16] = { 1564 /* current state: new state: action: */ 1565 [0 /* (Invalid) */] = TCP_CLOSE, 1566 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 1567 [TCP_SYN_SENT] = TCP_CLOSE, 1568 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 1569 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 1570 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 1571 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 1572 [TCP_CLOSE] = TCP_CLOSE, 1573 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 1574 [TCP_LAST_ACK] = TCP_LAST_ACK, 1575 [TCP_LISTEN] = TCP_CLOSE, 1576 [TCP_CLOSING] = TCP_CLOSING, 1577 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 1578 }; 1579 1580 static int mptcp_close_state(struct sock *sk) 1581 { 1582 int next = (int)new_state[sk->sk_state]; 1583 int ns = next & TCP_STATE_MASK; 1584 1585 inet_sk_state_store(sk, ns); 1586 1587 return next & TCP_ACTION_FIN; 1588 } 1589 1590 static void mptcp_close(struct sock *sk, long timeout) 1591 { 1592 struct mptcp_subflow_context *subflow, *tmp; 1593 struct mptcp_sock *msk = mptcp_sk(sk); 1594 LIST_HEAD(conn_list); 1595 1596 lock_sock(sk); 1597 sk->sk_shutdown = SHUTDOWN_MASK; 1598 1599 if (sk->sk_state == TCP_LISTEN) { 1600 inet_sk_state_store(sk, TCP_CLOSE); 1601 goto cleanup; 1602 } else if (sk->sk_state == TCP_CLOSE) { 1603 goto cleanup; 1604 } 1605 1606 if (__mptcp_check_fallback(msk)) { 1607 goto update_state; 1608 } else if (mptcp_close_state(sk)) { 1609 pr_debug("Sending DATA_FIN sk=%p", sk); 1610 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 1611 WRITE_ONCE(msk->snd_data_fin_enable, 1); 1612 1613 mptcp_for_each_subflow(msk, subflow) { 1614 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 1615 1616 mptcp_subflow_shutdown(sk, tcp_sk, SHUTDOWN_MASK); 1617 } 1618 } 1619 1620 sk_stream_wait_close(sk, timeout); 1621 1622 update_state: 1623 inet_sk_state_store(sk, TCP_CLOSE); 1624 1625 cleanup: 1626 /* be sure to always acquire the join list lock, to sync vs 1627 * mptcp_finish_join(). 1628 */ 1629 spin_lock_bh(&msk->join_list_lock); 1630 list_splice_tail_init(&msk->join_list, &msk->conn_list); 1631 spin_unlock_bh(&msk->join_list_lock); 1632 list_splice_init(&msk->conn_list, &conn_list); 1633 1634 __mptcp_clear_xmit(sk); 1635 1636 release_sock(sk); 1637 1638 list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 1639 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1640 __mptcp_close_ssk(sk, ssk, subflow, timeout); 1641 } 1642 1643 mptcp_cancel_work(sk); 1644 1645 __skb_queue_purge(&sk->sk_receive_queue); 1646 1647 sk_common_release(sk); 1648 } 1649 1650 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 1651 { 1652 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1653 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 1654 struct ipv6_pinfo *msk6 = inet6_sk(msk); 1655 1656 msk->sk_v6_daddr = ssk->sk_v6_daddr; 1657 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 1658 1659 if (msk6 && ssk6) { 1660 msk6->saddr = ssk6->saddr; 1661 msk6->flow_label = ssk6->flow_label; 1662 } 1663 #endif 1664 1665 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 1666 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 1667 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 1668 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 1669 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 1670 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 1671 } 1672 1673 static int mptcp_disconnect(struct sock *sk, int flags) 1674 { 1675 /* Should never be called. 1676 * inet_stream_connect() calls ->disconnect, but that 1677 * refers to the subflow socket, not the mptcp one. 1678 */ 1679 WARN_ON_ONCE(1); 1680 return 0; 1681 } 1682 1683 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1684 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 1685 { 1686 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 1687 1688 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 1689 } 1690 #endif 1691 1692 struct sock *mptcp_sk_clone(const struct sock *sk, 1693 const struct mptcp_options_received *mp_opt, 1694 struct request_sock *req) 1695 { 1696 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1697 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 1698 struct mptcp_sock *msk; 1699 u64 ack_seq; 1700 1701 if (!nsk) 1702 return NULL; 1703 1704 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1705 if (nsk->sk_family == AF_INET6) 1706 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 1707 #endif 1708 1709 __mptcp_init_sock(nsk); 1710 1711 msk = mptcp_sk(nsk); 1712 msk->local_key = subflow_req->local_key; 1713 msk->token = subflow_req->token; 1714 msk->subflow = NULL; 1715 WRITE_ONCE(msk->fully_established, false); 1716 1717 msk->write_seq = subflow_req->idsn + 1; 1718 atomic64_set(&msk->snd_una, msk->write_seq); 1719 if (mp_opt->mp_capable) { 1720 msk->can_ack = true; 1721 msk->remote_key = mp_opt->sndr_key; 1722 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 1723 ack_seq++; 1724 msk->ack_seq = ack_seq; 1725 } 1726 1727 sock_reset_flag(nsk, SOCK_RCU_FREE); 1728 /* will be fully established after successful MPC subflow creation */ 1729 inet_sk_state_store(nsk, TCP_SYN_RECV); 1730 bh_unlock_sock(nsk); 1731 1732 /* keep a single reference */ 1733 __sock_put(nsk); 1734 return nsk; 1735 } 1736 1737 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 1738 { 1739 const struct tcp_sock *tp = tcp_sk(ssk); 1740 1741 msk->rcvq_space.copied = 0; 1742 msk->rcvq_space.rtt_us = 0; 1743 1744 msk->rcvq_space.time = tp->tcp_mstamp; 1745 1746 /* initial rcv_space offering made to peer */ 1747 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 1748 TCP_INIT_CWND * tp->advmss); 1749 if (msk->rcvq_space.space == 0) 1750 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 1751 } 1752 1753 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 1754 bool kern) 1755 { 1756 struct mptcp_sock *msk = mptcp_sk(sk); 1757 struct socket *listener; 1758 struct sock *newsk; 1759 1760 listener = __mptcp_nmpc_socket(msk); 1761 if (WARN_ON_ONCE(!listener)) { 1762 *err = -EINVAL; 1763 return NULL; 1764 } 1765 1766 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 1767 newsk = inet_csk_accept(listener->sk, flags, err, kern); 1768 if (!newsk) 1769 return NULL; 1770 1771 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 1772 if (sk_is_mptcp(newsk)) { 1773 struct mptcp_subflow_context *subflow; 1774 struct sock *new_mptcp_sock; 1775 struct sock *ssk = newsk; 1776 1777 subflow = mptcp_subflow_ctx(newsk); 1778 new_mptcp_sock = subflow->conn; 1779 1780 /* is_mptcp should be false if subflow->conn is missing, see 1781 * subflow_syn_recv_sock() 1782 */ 1783 if (WARN_ON_ONCE(!new_mptcp_sock)) { 1784 tcp_sk(newsk)->is_mptcp = 0; 1785 return newsk; 1786 } 1787 1788 /* acquire the 2nd reference for the owning socket */ 1789 sock_hold(new_mptcp_sock); 1790 1791 local_bh_disable(); 1792 bh_lock_sock(new_mptcp_sock); 1793 msk = mptcp_sk(new_mptcp_sock); 1794 msk->first = newsk; 1795 1796 newsk = new_mptcp_sock; 1797 mptcp_copy_inaddrs(newsk, ssk); 1798 list_add(&subflow->node, &msk->conn_list); 1799 1800 mptcp_rcv_space_init(msk, ssk); 1801 bh_unlock_sock(new_mptcp_sock); 1802 1803 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 1804 local_bh_enable(); 1805 } else { 1806 MPTCP_INC_STATS(sock_net(sk), 1807 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 1808 } 1809 1810 return newsk; 1811 } 1812 1813 static void mptcp_destroy(struct sock *sk) 1814 { 1815 struct mptcp_sock *msk = mptcp_sk(sk); 1816 1817 mptcp_token_destroy(msk); 1818 if (msk->cached_ext) 1819 __skb_ext_put(msk->cached_ext); 1820 1821 sk_sockets_allocated_dec(sk); 1822 } 1823 1824 static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname, 1825 sockptr_t optval, unsigned int optlen) 1826 { 1827 struct sock *sk = (struct sock *)msk; 1828 struct socket *ssock; 1829 int ret; 1830 1831 switch (optname) { 1832 case SO_REUSEPORT: 1833 case SO_REUSEADDR: 1834 lock_sock(sk); 1835 ssock = __mptcp_nmpc_socket(msk); 1836 if (!ssock) { 1837 release_sock(sk); 1838 return -EINVAL; 1839 } 1840 1841 ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen); 1842 if (ret == 0) { 1843 if (optname == SO_REUSEPORT) 1844 sk->sk_reuseport = ssock->sk->sk_reuseport; 1845 else if (optname == SO_REUSEADDR) 1846 sk->sk_reuse = ssock->sk->sk_reuse; 1847 } 1848 release_sock(sk); 1849 return ret; 1850 } 1851 1852 return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen); 1853 } 1854 1855 static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname, 1856 sockptr_t optval, unsigned int optlen) 1857 { 1858 struct sock *sk = (struct sock *)msk; 1859 int ret = -EOPNOTSUPP; 1860 struct socket *ssock; 1861 1862 switch (optname) { 1863 case IPV6_V6ONLY: 1864 lock_sock(sk); 1865 ssock = __mptcp_nmpc_socket(msk); 1866 if (!ssock) { 1867 release_sock(sk); 1868 return -EINVAL; 1869 } 1870 1871 ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen); 1872 if (ret == 0) 1873 sk->sk_ipv6only = ssock->sk->sk_ipv6only; 1874 1875 release_sock(sk); 1876 break; 1877 } 1878 1879 return ret; 1880 } 1881 1882 static int mptcp_setsockopt(struct sock *sk, int level, int optname, 1883 sockptr_t optval, unsigned int optlen) 1884 { 1885 struct mptcp_sock *msk = mptcp_sk(sk); 1886 struct sock *ssk; 1887 1888 pr_debug("msk=%p", msk); 1889 1890 if (level == SOL_SOCKET) 1891 return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen); 1892 1893 /* @@ the meaning of setsockopt() when the socket is connected and 1894 * there are multiple subflows is not yet defined. It is up to the 1895 * MPTCP-level socket to configure the subflows until the subflow 1896 * is in TCP fallback, when TCP socket options are passed through 1897 * to the one remaining subflow. 1898 */ 1899 lock_sock(sk); 1900 ssk = __mptcp_tcp_fallback(msk); 1901 release_sock(sk); 1902 if (ssk) 1903 return tcp_setsockopt(ssk, level, optname, optval, optlen); 1904 1905 if (level == SOL_IPV6) 1906 return mptcp_setsockopt_v6(msk, optname, optval, optlen); 1907 1908 return -EOPNOTSUPP; 1909 } 1910 1911 static int mptcp_getsockopt(struct sock *sk, int level, int optname, 1912 char __user *optval, int __user *option) 1913 { 1914 struct mptcp_sock *msk = mptcp_sk(sk); 1915 struct sock *ssk; 1916 1917 pr_debug("msk=%p", msk); 1918 1919 /* @@ the meaning of setsockopt() when the socket is connected and 1920 * there are multiple subflows is not yet defined. It is up to the 1921 * MPTCP-level socket to configure the subflows until the subflow 1922 * is in TCP fallback, when socket options are passed through 1923 * to the one remaining subflow. 1924 */ 1925 lock_sock(sk); 1926 ssk = __mptcp_tcp_fallback(msk); 1927 release_sock(sk); 1928 if (ssk) 1929 return tcp_getsockopt(ssk, level, optname, optval, option); 1930 1931 return -EOPNOTSUPP; 1932 } 1933 1934 #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 1935 TCPF_WRITE_TIMER_DEFERRED) 1936 1937 /* this is very alike tcp_release_cb() but we must handle differently a 1938 * different set of events 1939 */ 1940 static void mptcp_release_cb(struct sock *sk) 1941 { 1942 unsigned long flags, nflags; 1943 1944 do { 1945 flags = sk->sk_tsq_flags; 1946 if (!(flags & MPTCP_DEFERRED_ALL)) 1947 return; 1948 nflags = flags & ~MPTCP_DEFERRED_ALL; 1949 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 1950 1951 sock_release_ownership(sk); 1952 1953 if (flags & TCPF_DELACK_TIMER_DEFERRED) { 1954 struct mptcp_sock *msk = mptcp_sk(sk); 1955 struct sock *ssk; 1956 1957 ssk = mptcp_subflow_recv_lookup(msk); 1958 if (!ssk || !schedule_work(&msk->work)) 1959 __sock_put(sk); 1960 } 1961 1962 if (flags & TCPF_WRITE_TIMER_DEFERRED) { 1963 mptcp_retransmit_handler(sk); 1964 __sock_put(sk); 1965 } 1966 } 1967 1968 static int mptcp_hash(struct sock *sk) 1969 { 1970 /* should never be called, 1971 * we hash the TCP subflows not the master socket 1972 */ 1973 WARN_ON_ONCE(1); 1974 return 0; 1975 } 1976 1977 static void mptcp_unhash(struct sock *sk) 1978 { 1979 /* called from sk_common_release(), but nothing to do here */ 1980 } 1981 1982 static int mptcp_get_port(struct sock *sk, unsigned short snum) 1983 { 1984 struct mptcp_sock *msk = mptcp_sk(sk); 1985 struct socket *ssock; 1986 1987 ssock = __mptcp_nmpc_socket(msk); 1988 pr_debug("msk=%p, subflow=%p", msk, ssock); 1989 if (WARN_ON_ONCE(!ssock)) 1990 return -EINVAL; 1991 1992 return inet_csk_get_port(ssock->sk, snum); 1993 } 1994 1995 void mptcp_finish_connect(struct sock *ssk) 1996 { 1997 struct mptcp_subflow_context *subflow; 1998 struct mptcp_sock *msk; 1999 struct sock *sk; 2000 u64 ack_seq; 2001 2002 subflow = mptcp_subflow_ctx(ssk); 2003 sk = subflow->conn; 2004 msk = mptcp_sk(sk); 2005 2006 pr_debug("msk=%p, token=%u", sk, subflow->token); 2007 2008 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 2009 ack_seq++; 2010 subflow->map_seq = ack_seq; 2011 subflow->map_subflow_seq = 1; 2012 2013 /* the socket is not connected yet, no msk/subflow ops can access/race 2014 * accessing the field below 2015 */ 2016 WRITE_ONCE(msk->remote_key, subflow->remote_key); 2017 WRITE_ONCE(msk->local_key, subflow->local_key); 2018 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 2019 WRITE_ONCE(msk->ack_seq, ack_seq); 2020 WRITE_ONCE(msk->can_ack, 1); 2021 atomic64_set(&msk->snd_una, msk->write_seq); 2022 2023 mptcp_pm_new_connection(msk, 0); 2024 2025 mptcp_rcv_space_init(msk, ssk); 2026 } 2027 2028 static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 2029 { 2030 write_lock_bh(&sk->sk_callback_lock); 2031 rcu_assign_pointer(sk->sk_wq, &parent->wq); 2032 sk_set_socket(sk, parent); 2033 sk->sk_uid = SOCK_INODE(parent)->i_uid; 2034 write_unlock_bh(&sk->sk_callback_lock); 2035 } 2036 2037 bool mptcp_finish_join(struct sock *sk) 2038 { 2039 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 2040 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 2041 struct sock *parent = (void *)msk; 2042 struct socket *parent_sock; 2043 bool ret; 2044 2045 pr_debug("msk=%p, subflow=%p", msk, subflow); 2046 2047 /* mptcp socket already closing? */ 2048 if (!mptcp_is_fully_established(parent)) 2049 return false; 2050 2051 if (!msk->pm.server_side) 2052 return true; 2053 2054 if (!mptcp_pm_allow_new_subflow(msk)) 2055 return false; 2056 2057 /* active connections are already on conn_list, and we can't acquire 2058 * msk lock here. 2059 * use the join list lock as synchronization point and double-check 2060 * msk status to avoid racing with mptcp_close() 2061 */ 2062 spin_lock_bh(&msk->join_list_lock); 2063 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 2064 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) 2065 list_add_tail(&subflow->node, &msk->join_list); 2066 spin_unlock_bh(&msk->join_list_lock); 2067 if (!ret) 2068 return false; 2069 2070 /* attach to msk socket only after we are sure he will deal with us 2071 * at close time 2072 */ 2073 parent_sock = READ_ONCE(parent->sk_socket); 2074 if (parent_sock && !sk->sk_socket) 2075 mptcp_sock_graft(sk, parent_sock); 2076 subflow->map_seq = msk->ack_seq; 2077 return true; 2078 } 2079 2080 static bool mptcp_memory_free(const struct sock *sk, int wake) 2081 { 2082 struct mptcp_sock *msk = mptcp_sk(sk); 2083 2084 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 2085 } 2086 2087 static struct proto mptcp_prot = { 2088 .name = "MPTCP", 2089 .owner = THIS_MODULE, 2090 .init = mptcp_init_sock, 2091 .disconnect = mptcp_disconnect, 2092 .close = mptcp_close, 2093 .accept = mptcp_accept, 2094 .setsockopt = mptcp_setsockopt, 2095 .getsockopt = mptcp_getsockopt, 2096 .shutdown = tcp_shutdown, 2097 .destroy = mptcp_destroy, 2098 .sendmsg = mptcp_sendmsg, 2099 .recvmsg = mptcp_recvmsg, 2100 .release_cb = mptcp_release_cb, 2101 .hash = mptcp_hash, 2102 .unhash = mptcp_unhash, 2103 .get_port = mptcp_get_port, 2104 .sockets_allocated = &mptcp_sockets_allocated, 2105 .memory_allocated = &tcp_memory_allocated, 2106 .memory_pressure = &tcp_memory_pressure, 2107 .stream_memory_free = mptcp_memory_free, 2108 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 2109 .sysctl_mem = sysctl_tcp_mem, 2110 .obj_size = sizeof(struct mptcp_sock), 2111 .slab_flags = SLAB_TYPESAFE_BY_RCU, 2112 .no_autobind = true, 2113 }; 2114 2115 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 2116 { 2117 struct mptcp_sock *msk = mptcp_sk(sock->sk); 2118 struct socket *ssock; 2119 int err; 2120 2121 lock_sock(sock->sk); 2122 ssock = __mptcp_nmpc_socket(msk); 2123 if (!ssock) { 2124 err = -EINVAL; 2125 goto unlock; 2126 } 2127 2128 err = ssock->ops->bind(ssock, uaddr, addr_len); 2129 if (!err) 2130 mptcp_copy_inaddrs(sock->sk, ssock->sk); 2131 2132 unlock: 2133 release_sock(sock->sk); 2134 return err; 2135 } 2136 2137 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 2138 struct mptcp_subflow_context *subflow) 2139 { 2140 subflow->request_mptcp = 0; 2141 __mptcp_do_fallback(msk); 2142 } 2143 2144 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 2145 int addr_len, int flags) 2146 { 2147 struct mptcp_sock *msk = mptcp_sk(sock->sk); 2148 struct mptcp_subflow_context *subflow; 2149 struct socket *ssock; 2150 int err; 2151 2152 lock_sock(sock->sk); 2153 if (sock->state != SS_UNCONNECTED && msk->subflow) { 2154 /* pending connection or invalid state, let existing subflow 2155 * cope with that 2156 */ 2157 ssock = msk->subflow; 2158 goto do_connect; 2159 } 2160 2161 ssock = __mptcp_nmpc_socket(msk); 2162 if (!ssock) { 2163 err = -EINVAL; 2164 goto unlock; 2165 } 2166 2167 mptcp_token_destroy(msk); 2168 inet_sk_state_store(sock->sk, TCP_SYN_SENT); 2169 subflow = mptcp_subflow_ctx(ssock->sk); 2170 #ifdef CONFIG_TCP_MD5SIG 2171 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 2172 * TCP option space. 2173 */ 2174 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 2175 mptcp_subflow_early_fallback(msk, subflow); 2176 #endif 2177 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) 2178 mptcp_subflow_early_fallback(msk, subflow); 2179 2180 do_connect: 2181 err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 2182 sock->state = ssock->state; 2183 2184 /* on successful connect, the msk state will be moved to established by 2185 * subflow_finish_connect() 2186 */ 2187 if (!err || err == -EINPROGRESS) 2188 mptcp_copy_inaddrs(sock->sk, ssock->sk); 2189 else 2190 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 2191 2192 unlock: 2193 release_sock(sock->sk); 2194 return err; 2195 } 2196 2197 static int mptcp_listen(struct socket *sock, int backlog) 2198 { 2199 struct mptcp_sock *msk = mptcp_sk(sock->sk); 2200 struct socket *ssock; 2201 int err; 2202 2203 pr_debug("msk=%p", msk); 2204 2205 lock_sock(sock->sk); 2206 ssock = __mptcp_nmpc_socket(msk); 2207 if (!ssock) { 2208 err = -EINVAL; 2209 goto unlock; 2210 } 2211 2212 mptcp_token_destroy(msk); 2213 inet_sk_state_store(sock->sk, TCP_LISTEN); 2214 sock_set_flag(sock->sk, SOCK_RCU_FREE); 2215 2216 err = ssock->ops->listen(ssock, backlog); 2217 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 2218 if (!err) 2219 mptcp_copy_inaddrs(sock->sk, ssock->sk); 2220 2221 unlock: 2222 release_sock(sock->sk); 2223 return err; 2224 } 2225 2226 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 2227 int flags, bool kern) 2228 { 2229 struct mptcp_sock *msk = mptcp_sk(sock->sk); 2230 struct socket *ssock; 2231 int err; 2232 2233 pr_debug("msk=%p", msk); 2234 2235 lock_sock(sock->sk); 2236 if (sock->sk->sk_state != TCP_LISTEN) 2237 goto unlock_fail; 2238 2239 ssock = __mptcp_nmpc_socket(msk); 2240 if (!ssock) 2241 goto unlock_fail; 2242 2243 clear_bit(MPTCP_DATA_READY, &msk->flags); 2244 sock_hold(ssock->sk); 2245 release_sock(sock->sk); 2246 2247 err = ssock->ops->accept(sock, newsock, flags, kern); 2248 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 2249 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 2250 struct mptcp_subflow_context *subflow; 2251 2252 /* set ssk->sk_socket of accept()ed flows to mptcp socket. 2253 * This is needed so NOSPACE flag can be set from tcp stack. 2254 */ 2255 __mptcp_flush_join_list(msk); 2256 mptcp_for_each_subflow(msk, subflow) { 2257 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2258 2259 if (!ssk->sk_socket) 2260 mptcp_sock_graft(ssk, newsock); 2261 } 2262 } 2263 2264 if (inet_csk_listen_poll(ssock->sk)) 2265 set_bit(MPTCP_DATA_READY, &msk->flags); 2266 sock_put(ssock->sk); 2267 return err; 2268 2269 unlock_fail: 2270 release_sock(sock->sk); 2271 return -EINVAL; 2272 } 2273 2274 static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2275 { 2276 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 2277 0; 2278 } 2279 2280 static __poll_t mptcp_poll(struct file *file, struct socket *sock, 2281 struct poll_table_struct *wait) 2282 { 2283 struct sock *sk = sock->sk; 2284 struct mptcp_sock *msk; 2285 __poll_t mask = 0; 2286 int state; 2287 2288 msk = mptcp_sk(sk); 2289 sock_poll_wait(file, sock, wait); 2290 2291 state = inet_sk_state_load(sk); 2292 if (state == TCP_LISTEN) 2293 return mptcp_check_readable(msk); 2294 2295 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 2296 mask |= mptcp_check_readable(msk); 2297 if (sk_stream_is_writeable(sk) && 2298 test_bit(MPTCP_SEND_SPACE, &msk->flags)) 2299 mask |= EPOLLOUT | EPOLLWRNORM; 2300 } 2301 if (sk->sk_shutdown & RCV_SHUTDOWN) 2302 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 2303 2304 return mask; 2305 } 2306 2307 static int mptcp_shutdown(struct socket *sock, int how) 2308 { 2309 struct mptcp_sock *msk = mptcp_sk(sock->sk); 2310 struct mptcp_subflow_context *subflow; 2311 int ret = 0; 2312 2313 pr_debug("sk=%p, how=%d", msk, how); 2314 2315 lock_sock(sock->sk); 2316 2317 how++; 2318 if ((how & ~SHUTDOWN_MASK) || !how) { 2319 ret = -EINVAL; 2320 goto out_unlock; 2321 } 2322 2323 if (sock->state == SS_CONNECTING) { 2324 if ((1 << sock->sk->sk_state) & 2325 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 2326 sock->state = SS_DISCONNECTING; 2327 else 2328 sock->state = SS_CONNECTED; 2329 } 2330 2331 /* If we've already sent a FIN, or it's a closed state, skip this. */ 2332 if (__mptcp_check_fallback(msk)) { 2333 if (how == SHUT_WR || how == SHUT_RDWR) 2334 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); 2335 2336 mptcp_for_each_subflow(msk, subflow) { 2337 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2338 2339 mptcp_subflow_shutdown(sock->sk, tcp_sk, how); 2340 } 2341 } else if ((how & SEND_SHUTDOWN) && 2342 ((1 << sock->sk->sk_state) & 2343 (TCPF_ESTABLISHED | TCPF_SYN_SENT | 2344 TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) && 2345 mptcp_close_state(sock->sk)) { 2346 __mptcp_flush_join_list(msk); 2347 2348 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2349 WRITE_ONCE(msk->snd_data_fin_enable, 1); 2350 2351 mptcp_for_each_subflow(msk, subflow) { 2352 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2353 2354 mptcp_subflow_shutdown(sock->sk, tcp_sk, how); 2355 } 2356 } 2357 2358 /* Wake up anyone sleeping in poll. */ 2359 sock->sk->sk_state_change(sock->sk); 2360 2361 out_unlock: 2362 release_sock(sock->sk); 2363 2364 return ret; 2365 } 2366 2367 static const struct proto_ops mptcp_stream_ops = { 2368 .family = PF_INET, 2369 .owner = THIS_MODULE, 2370 .release = inet_release, 2371 .bind = mptcp_bind, 2372 .connect = mptcp_stream_connect, 2373 .socketpair = sock_no_socketpair, 2374 .accept = mptcp_stream_accept, 2375 .getname = inet_getname, 2376 .poll = mptcp_poll, 2377 .ioctl = inet_ioctl, 2378 .gettstamp = sock_gettstamp, 2379 .listen = mptcp_listen, 2380 .shutdown = mptcp_shutdown, 2381 .setsockopt = sock_common_setsockopt, 2382 .getsockopt = sock_common_getsockopt, 2383 .sendmsg = inet_sendmsg, 2384 .recvmsg = inet_recvmsg, 2385 .mmap = sock_no_mmap, 2386 .sendpage = inet_sendpage, 2387 }; 2388 2389 static struct inet_protosw mptcp_protosw = { 2390 .type = SOCK_STREAM, 2391 .protocol = IPPROTO_MPTCP, 2392 .prot = &mptcp_prot, 2393 .ops = &mptcp_stream_ops, 2394 .flags = INET_PROTOSW_ICSK, 2395 }; 2396 2397 void __init mptcp_proto_init(void) 2398 { 2399 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 2400 2401 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 2402 panic("Failed to allocate MPTCP pcpu counter\n"); 2403 2404 mptcp_subflow_init(); 2405 mptcp_pm_init(); 2406 mptcp_token_init(); 2407 2408 if (proto_register(&mptcp_prot, 1) != 0) 2409 panic("Failed to register MPTCP proto.\n"); 2410 2411 inet_register_protosw(&mptcp_protosw); 2412 2413 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 2414 } 2415 2416 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2417 static const struct proto_ops mptcp_v6_stream_ops = { 2418 .family = PF_INET6, 2419 .owner = THIS_MODULE, 2420 .release = inet6_release, 2421 .bind = mptcp_bind, 2422 .connect = mptcp_stream_connect, 2423 .socketpair = sock_no_socketpair, 2424 .accept = mptcp_stream_accept, 2425 .getname = inet6_getname, 2426 .poll = mptcp_poll, 2427 .ioctl = inet6_ioctl, 2428 .gettstamp = sock_gettstamp, 2429 .listen = mptcp_listen, 2430 .shutdown = mptcp_shutdown, 2431 .setsockopt = sock_common_setsockopt, 2432 .getsockopt = sock_common_getsockopt, 2433 .sendmsg = inet6_sendmsg, 2434 .recvmsg = inet6_recvmsg, 2435 .mmap = sock_no_mmap, 2436 .sendpage = inet_sendpage, 2437 #ifdef CONFIG_COMPAT 2438 .compat_ioctl = inet6_compat_ioctl, 2439 #endif 2440 }; 2441 2442 static struct proto mptcp_v6_prot; 2443 2444 static void mptcp_v6_destroy(struct sock *sk) 2445 { 2446 mptcp_destroy(sk); 2447 inet6_destroy_sock(sk); 2448 } 2449 2450 static struct inet_protosw mptcp_v6_protosw = { 2451 .type = SOCK_STREAM, 2452 .protocol = IPPROTO_MPTCP, 2453 .prot = &mptcp_v6_prot, 2454 .ops = &mptcp_v6_stream_ops, 2455 .flags = INET_PROTOSW_ICSK, 2456 }; 2457 2458 int __init mptcp_proto_v6_init(void) 2459 { 2460 int err; 2461 2462 mptcp_v6_prot = mptcp_prot; 2463 strcpy(mptcp_v6_prot.name, "MPTCPv6"); 2464 mptcp_v6_prot.slab = NULL; 2465 mptcp_v6_prot.destroy = mptcp_v6_destroy; 2466 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 2467 2468 err = proto_register(&mptcp_v6_prot, 1); 2469 if (err) 2470 return err; 2471 2472 err = inet6_register_protosw(&mptcp_v6_protosw); 2473 if (err) 2474 proto_unregister(&mptcp_v6_prot); 2475 2476 return err; 2477 } 2478 #endif 2479